1d0173278SGuenter Roeck // SPDX-License-Identifier: GPL-2.0+
2b7e04f8cSWim Van Sebroeck /*
3b7e04f8cSWim Van Sebroeck * Berkshire USB-PC Watchdog Card Driver
4b7e04f8cSWim Van Sebroeck *
5b7e04f8cSWim Van Sebroeck * (c) Copyright 2004-2007 Wim Van Sebroeck <wim@iguana.be>.
6b7e04f8cSWim Van Sebroeck *
7b7e04f8cSWim Van Sebroeck * Based on source code of the following authors:
8b7e04f8cSWim Van Sebroeck * Ken Hollis <kenji@bitgate.com>,
929fa0586SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk>,
10b7e04f8cSWim Van Sebroeck * Matt Domsch <Matt_Domsch@dell.com>,
11b7e04f8cSWim Van Sebroeck * Rob Radez <rob@osinvestor.com>,
12b7e04f8cSWim Van Sebroeck * Greg Kroah-Hartman <greg@kroah.com>
13b7e04f8cSWim Van Sebroeck *
14b7e04f8cSWim Van Sebroeck * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
15b7e04f8cSWim Van Sebroeck * provide warranty for any of this software. This material is
16b7e04f8cSWim Van Sebroeck * provided "AS-IS" and at no charge.
17b7e04f8cSWim Van Sebroeck *
18b7e04f8cSWim Van Sebroeck * Thanks also to Simon Machell at Berkshire Products Inc. for
19b7e04f8cSWim Van Sebroeck * providing the test hardware. More info is available at
20b7e04f8cSWim Van Sebroeck * http://www.berkprod.com/ or http://www.pcwatchdog.com/
21b7e04f8cSWim Van Sebroeck */
22b7e04f8cSWim Van Sebroeck
2327c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2427c766aaSJoe Perches
25b7e04f8cSWim Van Sebroeck #include <linux/module.h> /* For module specific items */
26b7e04f8cSWim Van Sebroeck #include <linux/moduleparam.h> /* For new moduleparam's */
27b7e04f8cSWim Van Sebroeck #include <linux/types.h> /* For standard types (like size_t) */
28b7e04f8cSWim Van Sebroeck #include <linux/errno.h> /* For the -ENODEV/... values */
29b7e04f8cSWim Van Sebroeck #include <linux/kernel.h> /* For printk/panic/... */
30b7e04f8cSWim Van Sebroeck #include <linux/delay.h> /* For mdelay function */
31487722cfSJean Delvare #include <linux/miscdevice.h> /* For struct miscdevice */
32b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h> /* For the watchdog specific items */
33b7e04f8cSWim Van Sebroeck #include <linux/notifier.h> /* For notifier support */
34b7e04f8cSWim Van Sebroeck #include <linux/reboot.h> /* For reboot_notifier stuff */
35b7e04f8cSWim Van Sebroeck #include <linux/init.h> /* For __init/__exit/... */
36b7e04f8cSWim Van Sebroeck #include <linux/fs.h> /* For file operations */
37b7e04f8cSWim Van Sebroeck #include <linux/usb.h> /* For USB functions */
38b7e04f8cSWim Van Sebroeck #include <linux/slab.h> /* For kmalloc, ... */
39b7e04f8cSWim Van Sebroeck #include <linux/mutex.h> /* For mutex locking */
40b7e04f8cSWim Van Sebroeck #include <linux/hid.h> /* For HID_REQ_SET_REPORT & HID_DT_REPORT */
41089ab079SWim Van Sebroeck #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
42b7e04f8cSWim Van Sebroeck
43b7e04f8cSWim Van Sebroeck
44b7e04f8cSWim Van Sebroeck /* Module and Version Information */
45b7e04f8cSWim Van Sebroeck #define DRIVER_VERSION "1.02"
46b7e04f8cSWim Van Sebroeck #define DRIVER_AUTHOR "Wim Van Sebroeck <wim@iguana.be>"
47b7e04f8cSWim Van Sebroeck #define DRIVER_DESC "Berkshire USB-PC Watchdog driver"
48b7e04f8cSWim Van Sebroeck #define DRIVER_NAME "pcwd_usb"
49b7e04f8cSWim Van Sebroeck
50b7e04f8cSWim Van Sebroeck MODULE_AUTHOR(DRIVER_AUTHOR);
51b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION(DRIVER_DESC);
52bf020974SGreg Kroah-Hartman MODULE_LICENSE("GPL");
53b7e04f8cSWim Van Sebroeck
54143a2e54SWim Van Sebroeck #define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
55143a2e54SWim Van Sebroeck delay-time from dip-switches */
56b7e04f8cSWim Van Sebroeck static int heartbeat = WATCHDOG_HEARTBEAT;
57b7e04f8cSWim Van Sebroeck module_param(heartbeat, int, 0);
58143a2e54SWim Van Sebroeck MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
59143a2e54SWim Van Sebroeck "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
60143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
61b7e04f8cSWim Van Sebroeck
6286a1e189SWim Van Sebroeck static bool nowayout = WATCHDOG_NOWAYOUT;
6386a1e189SWim Van Sebroeck module_param(nowayout, bool, 0);
64143a2e54SWim Van Sebroeck MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
65143a2e54SWim Van Sebroeck __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
66b7e04f8cSWim Van Sebroeck
67b7e04f8cSWim Van Sebroeck /* The vendor and product id's for the USB-PC Watchdog card */
68b7e04f8cSWim Van Sebroeck #define USB_PCWD_VENDOR_ID 0x0c98
69b7e04f8cSWim Van Sebroeck #define USB_PCWD_PRODUCT_ID 0x1140
70b7e04f8cSWim Van Sebroeck
71b7e04f8cSWim Van Sebroeck /* table of devices that work with this driver */
72cc53e1c2SArvind Yadav static const struct usb_device_id usb_pcwd_table[] = {
73b7e04f8cSWim Van Sebroeck { USB_DEVICE(USB_PCWD_VENDOR_ID, USB_PCWD_PRODUCT_ID) },
74b7e04f8cSWim Van Sebroeck { } /* Terminating entry */
75b7e04f8cSWim Van Sebroeck };
76b7e04f8cSWim Van Sebroeck MODULE_DEVICE_TABLE(usb, usb_pcwd_table);
77b7e04f8cSWim Van Sebroeck
78b7e04f8cSWim Van Sebroeck /* according to documentation max. time to process a command for the USB
79b7e04f8cSWim Van Sebroeck * watchdog card is 100 or 200 ms, so we give it 250 ms to do it's job */
80b7e04f8cSWim Van Sebroeck #define USB_COMMAND_TIMEOUT 250
81b7e04f8cSWim Van Sebroeck
82b7e04f8cSWim Van Sebroeck /* Watchdog's internal commands */
83143a2e54SWim Van Sebroeck #define CMD_READ_TEMP 0x02 /* Read Temperature;
84143a2e54SWim Van Sebroeck Re-trigger Watchdog */
85b7e04f8cSWim Van Sebroeck #define CMD_TRIGGER CMD_READ_TEMP
86b7e04f8cSWim Van Sebroeck #define CMD_GET_STATUS 0x04 /* Get Status Information */
87b7e04f8cSWim Van Sebroeck #define CMD_GET_FIRMWARE_VERSION 0x08 /* Get Firmware Version */
88b7e04f8cSWim Van Sebroeck #define CMD_GET_DIP_SWITCH_SETTINGS 0x0c /* Get Dip Switch Settings */
89b7e04f8cSWim Van Sebroeck #define CMD_READ_WATCHDOG_TIMEOUT 0x18 /* Read Current Watchdog Time */
90b7e04f8cSWim Van Sebroeck #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19 /* Write Current WatchdogTime */
91b7e04f8cSWim Van Sebroeck #define CMD_ENABLE_WATCHDOG 0x30 /* Enable / Disable Watchdog */
92b7e04f8cSWim Van Sebroeck #define CMD_DISABLE_WATCHDOG CMD_ENABLE_WATCHDOG
93b7e04f8cSWim Van Sebroeck
94b7e04f8cSWim Van Sebroeck /* Watchdog's Dip Switch heartbeat values */
95b7e04f8cSWim Van Sebroeck static const int heartbeat_tbl[] = {
96b7e04f8cSWim Van Sebroeck 5, /* OFF-OFF-OFF = 5 Sec */
97b7e04f8cSWim Van Sebroeck 10, /* OFF-OFF-ON = 10 Sec */
98b7e04f8cSWim Van Sebroeck 30, /* OFF-ON-OFF = 30 Sec */
99b7e04f8cSWim Van Sebroeck 60, /* OFF-ON-ON = 1 Min */
100b7e04f8cSWim Van Sebroeck 300, /* ON-OFF-OFF = 5 Min */
101b7e04f8cSWim Van Sebroeck 600, /* ON-OFF-ON = 10 Min */
102b7e04f8cSWim Van Sebroeck 1800, /* ON-ON-OFF = 30 Min */
103b7e04f8cSWim Van Sebroeck 3600, /* ON-ON-ON = 1 hour */
104b7e04f8cSWim Van Sebroeck };
105b7e04f8cSWim Van Sebroeck
106b7e04f8cSWim Van Sebroeck /* We can only use 1 card due to the /dev/watchdog restriction */
107b7e04f8cSWim Van Sebroeck static int cards_found;
108b7e04f8cSWim Van Sebroeck
109b7e04f8cSWim Van Sebroeck /* some internal variables */
110b7e04f8cSWim Van Sebroeck static unsigned long is_active;
111b7e04f8cSWim Van Sebroeck static char expect_release;
112b7e04f8cSWim Van Sebroeck
113b7e04f8cSWim Van Sebroeck /* Structure to hold all of our device specific stuff */
114b7e04f8cSWim Van Sebroeck struct usb_pcwd_private {
115143a2e54SWim Van Sebroeck /* save off the usb device pointer */
116143a2e54SWim Van Sebroeck struct usb_device *udev;
117143a2e54SWim Van Sebroeck /* the interface for this device */
118143a2e54SWim Van Sebroeck struct usb_interface *interface;
119b7e04f8cSWim Van Sebroeck
120143a2e54SWim Van Sebroeck /* the interface number used for cmd's */
121143a2e54SWim Van Sebroeck unsigned int interface_number;
122b7e04f8cSWim Van Sebroeck
123143a2e54SWim Van Sebroeck /* the buffer to intr data */
124143a2e54SWim Van Sebroeck unsigned char *intr_buffer;
125143a2e54SWim Van Sebroeck /* the dma address for the intr buffer */
126143a2e54SWim Van Sebroeck dma_addr_t intr_dma;
127143a2e54SWim Van Sebroeck /* the size of the intr buffer */
128143a2e54SWim Van Sebroeck size_t intr_size;
129143a2e54SWim Van Sebroeck /* the urb used for the intr pipe */
130143a2e54SWim Van Sebroeck struct urb *intr_urb;
131b7e04f8cSWim Van Sebroeck
132143a2e54SWim Van Sebroeck /* The command that is reported back */
133143a2e54SWim Van Sebroeck unsigned char cmd_command;
134143a2e54SWim Van Sebroeck /* The data MSB that is reported back */
135143a2e54SWim Van Sebroeck unsigned char cmd_data_msb;
136143a2e54SWim Van Sebroeck /* The data LSB that is reported back */
137143a2e54SWim Van Sebroeck unsigned char cmd_data_lsb;
138143a2e54SWim Van Sebroeck /* true if we received a report after a command */
139143a2e54SWim Van Sebroeck atomic_t cmd_received;
140b7e04f8cSWim Van Sebroeck
141143a2e54SWim Van Sebroeck /* Wether or not the device exists */
142143a2e54SWim Van Sebroeck int exists;
143143a2e54SWim Van Sebroeck /* locks this structure */
144143a2e54SWim Van Sebroeck struct mutex mtx;
145b7e04f8cSWim Van Sebroeck };
146b7e04f8cSWim Van Sebroeck static struct usb_pcwd_private *usb_pcwd_device;
147b7e04f8cSWim Van Sebroeck
148b7e04f8cSWim Van Sebroeck /* prevent races between open() and disconnect() */
149b7e04f8cSWim Van Sebroeck static DEFINE_MUTEX(disconnect_mutex);
150b7e04f8cSWim Van Sebroeck
151b7e04f8cSWim Van Sebroeck /* local function prototypes */
152143a2e54SWim Van Sebroeck static int usb_pcwd_probe(struct usb_interface *interface,
153143a2e54SWim Van Sebroeck const struct usb_device_id *id);
154b7e04f8cSWim Van Sebroeck static void usb_pcwd_disconnect(struct usb_interface *interface);
155b7e04f8cSWim Van Sebroeck
156b7e04f8cSWim Van Sebroeck /* usb specific object needed to register this driver with the usb subsystem */
157b7e04f8cSWim Van Sebroeck static struct usb_driver usb_pcwd_driver = {
158b7e04f8cSWim Van Sebroeck .name = DRIVER_NAME,
159b7e04f8cSWim Van Sebroeck .probe = usb_pcwd_probe,
160b7e04f8cSWim Van Sebroeck .disconnect = usb_pcwd_disconnect,
161b7e04f8cSWim Van Sebroeck .id_table = usb_pcwd_table,
162b7e04f8cSWim Van Sebroeck };
163b7e04f8cSWim Van Sebroeck
164b7e04f8cSWim Van Sebroeck
usb_pcwd_intr_done(struct urb * urb)165b7e04f8cSWim Van Sebroeck static void usb_pcwd_intr_done(struct urb *urb)
166b7e04f8cSWim Van Sebroeck {
167143a2e54SWim Van Sebroeck struct usb_pcwd_private *usb_pcwd =
168143a2e54SWim Van Sebroeck (struct usb_pcwd_private *)urb->context;
169b7e04f8cSWim Van Sebroeck unsigned char *data = usb_pcwd->intr_buffer;
170d7e92f7fSGreg Kroah-Hartman struct device *dev = &usb_pcwd->interface->dev;
171b7e04f8cSWim Van Sebroeck int retval;
172b7e04f8cSWim Van Sebroeck
173b7e04f8cSWim Van Sebroeck switch (urb->status) {
174b7e04f8cSWim Van Sebroeck case 0: /* success */
175b7e04f8cSWim Van Sebroeck break;
176b7e04f8cSWim Van Sebroeck case -ECONNRESET: /* unlink */
177b7e04f8cSWim Van Sebroeck case -ENOENT:
178b7e04f8cSWim Van Sebroeck case -ESHUTDOWN:
179b7e04f8cSWim Van Sebroeck /* this urb is terminated, clean up */
180d7e92f7fSGreg Kroah-Hartman dev_dbg(dev, "%s - urb shutting down with status: %d",
181d7e92f7fSGreg Kroah-Hartman __func__, urb->status);
182b7e04f8cSWim Van Sebroeck return;
183b7e04f8cSWim Van Sebroeck /* -EPIPE: should clear the halt */
184b7e04f8cSWim Van Sebroeck default: /* error */
185d7e92f7fSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero urb status received: %d",
186d7e92f7fSGreg Kroah-Hartman __func__, urb->status);
187b7e04f8cSWim Van Sebroeck goto resubmit;
188b7e04f8cSWim Van Sebroeck }
189b7e04f8cSWim Van Sebroeck
190d7e92f7fSGreg Kroah-Hartman dev_dbg(dev, "received following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
191b7e04f8cSWim Van Sebroeck data[0], data[1], data[2]);
192b7e04f8cSWim Van Sebroeck
193b7e04f8cSWim Van Sebroeck usb_pcwd->cmd_command = data[0];
194b7e04f8cSWim Van Sebroeck usb_pcwd->cmd_data_msb = data[1];
195b7e04f8cSWim Van Sebroeck usb_pcwd->cmd_data_lsb = data[2];
196b7e04f8cSWim Van Sebroeck
197b7e04f8cSWim Van Sebroeck /* notify anyone waiting that the cmd has finished */
198b7e04f8cSWim Van Sebroeck atomic_set(&usb_pcwd->cmd_received, 1);
199b7e04f8cSWim Van Sebroeck
200b7e04f8cSWim Van Sebroeck resubmit:
201b7e04f8cSWim Van Sebroeck retval = usb_submit_urb(urb, GFP_ATOMIC);
202b7e04f8cSWim Van Sebroeck if (retval)
20327c766aaSJoe Perches pr_err("can't resubmit intr, usb_submit_urb failed with result %d\n",
20427c766aaSJoe Perches retval);
205b7e04f8cSWim Van Sebroeck }
206b7e04f8cSWim Van Sebroeck
usb_pcwd_send_command(struct usb_pcwd_private * usb_pcwd,unsigned char cmd,unsigned char * msb,unsigned char * lsb)207143a2e54SWim Van Sebroeck static int usb_pcwd_send_command(struct usb_pcwd_private *usb_pcwd,
208143a2e54SWim Van Sebroeck unsigned char cmd, unsigned char *msb, unsigned char *lsb)
209b7e04f8cSWim Van Sebroeck {
210b7e04f8cSWim Van Sebroeck int got_response, count;
2115412df0bSGuenter Roeck unsigned char *buf;
212b7e04f8cSWim Van Sebroeck
213143a2e54SWim Van Sebroeck /* We will not send any commands if the USB PCWD device does
214143a2e54SWim Van Sebroeck * not exist */
215b7e04f8cSWim Van Sebroeck if ((!usb_pcwd) || (!usb_pcwd->exists))
216b7e04f8cSWim Van Sebroeck return -1;
217b7e04f8cSWim Van Sebroeck
2185412df0bSGuenter Roeck buf = kmalloc(6, GFP_KERNEL);
2195412df0bSGuenter Roeck if (buf == NULL)
2205412df0bSGuenter Roeck return 0;
2215412df0bSGuenter Roeck
222143a2e54SWim Van Sebroeck /* The USB PC Watchdog uses a 6 byte report format.
223143a2e54SWim Van Sebroeck * The board currently uses only 3 of the six bytes of the report. */
224b7e04f8cSWim Van Sebroeck buf[0] = cmd; /* Byte 0 = CMD */
225b7e04f8cSWim Van Sebroeck buf[1] = *msb; /* Byte 1 = Data MSB */
226b7e04f8cSWim Van Sebroeck buf[2] = *lsb; /* Byte 2 = Data LSB */
227b7e04f8cSWim Van Sebroeck buf[3] = buf[4] = buf[5] = 0; /* All other bytes not used */
228b7e04f8cSWim Van Sebroeck
229d7e92f7fSGreg Kroah-Hartman dev_dbg(&usb_pcwd->interface->dev,
230d7e92f7fSGreg Kroah-Hartman "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
231b7e04f8cSWim Van Sebroeck buf[0], buf[1], buf[2]);
232b7e04f8cSWim Van Sebroeck
233b7e04f8cSWim Van Sebroeck atomic_set(&usb_pcwd->cmd_received, 0);
234b7e04f8cSWim Van Sebroeck
235b7e04f8cSWim Van Sebroeck if (usb_control_msg(usb_pcwd->udev, usb_sndctrlpipe(usb_pcwd->udev, 0),
236b7e04f8cSWim Van Sebroeck HID_REQ_SET_REPORT, HID_DT_REPORT,
237ab5bbdc7SDan Carpenter 0x0200, usb_pcwd->interface_number, buf, 6,
238ab5bbdc7SDan Carpenter USB_COMMAND_TIMEOUT) != 6) {
239d7e92f7fSGreg Kroah-Hartman dev_dbg(&usb_pcwd->interface->dev,
240d7e92f7fSGreg Kroah-Hartman "usb_pcwd_send_command: error in usb_control_msg for cmd 0x%x 0x%x 0x%x\n",
241d7e92f7fSGreg Kroah-Hartman cmd, *msb, *lsb);
242b7e04f8cSWim Van Sebroeck }
243b7e04f8cSWim Van Sebroeck /* wait till the usb card processed the command,
244b7e04f8cSWim Van Sebroeck * with a max. timeout of USB_COMMAND_TIMEOUT */
245b7e04f8cSWim Van Sebroeck got_response = 0;
246143a2e54SWim Van Sebroeck for (count = 0; (count < USB_COMMAND_TIMEOUT) && (!got_response);
247143a2e54SWim Van Sebroeck count++) {
248b7e04f8cSWim Van Sebroeck mdelay(1);
249b7e04f8cSWim Van Sebroeck if (atomic_read(&usb_pcwd->cmd_received))
250b7e04f8cSWim Van Sebroeck got_response = 1;
251b7e04f8cSWim Van Sebroeck }
252b7e04f8cSWim Van Sebroeck
253b7e04f8cSWim Van Sebroeck if ((got_response) && (cmd == usb_pcwd->cmd_command)) {
254b7e04f8cSWim Van Sebroeck /* read back response */
255b7e04f8cSWim Van Sebroeck *msb = usb_pcwd->cmd_data_msb;
256b7e04f8cSWim Van Sebroeck *lsb = usb_pcwd->cmd_data_lsb;
257b7e04f8cSWim Van Sebroeck }
258b7e04f8cSWim Van Sebroeck
2595412df0bSGuenter Roeck kfree(buf);
2605412df0bSGuenter Roeck
261b7e04f8cSWim Van Sebroeck return got_response;
262b7e04f8cSWim Van Sebroeck }
263b7e04f8cSWim Van Sebroeck
usb_pcwd_start(struct usb_pcwd_private * usb_pcwd)264b7e04f8cSWim Van Sebroeck static int usb_pcwd_start(struct usb_pcwd_private *usb_pcwd)
265b7e04f8cSWim Van Sebroeck {
266b7e04f8cSWim Van Sebroeck unsigned char msb = 0x00;
267b7e04f8cSWim Van Sebroeck unsigned char lsb = 0x00;
268b7e04f8cSWim Van Sebroeck int retval;
269b7e04f8cSWim Van Sebroeck
270b7e04f8cSWim Van Sebroeck /* Enable Watchdog */
271143a2e54SWim Van Sebroeck retval = usb_pcwd_send_command(usb_pcwd, CMD_ENABLE_WATCHDOG,
272143a2e54SWim Van Sebroeck &msb, &lsb);
273b7e04f8cSWim Van Sebroeck
274b7e04f8cSWim Van Sebroeck if ((retval == 0) || (lsb == 0)) {
27527c766aaSJoe Perches pr_err("Card did not acknowledge enable attempt\n");
276b7e04f8cSWim Van Sebroeck return -1;
277b7e04f8cSWim Van Sebroeck }
278b7e04f8cSWim Van Sebroeck
279b7e04f8cSWim Van Sebroeck return 0;
280b7e04f8cSWim Van Sebroeck }
281b7e04f8cSWim Van Sebroeck
usb_pcwd_stop(struct usb_pcwd_private * usb_pcwd)282b7e04f8cSWim Van Sebroeck static int usb_pcwd_stop(struct usb_pcwd_private *usb_pcwd)
283b7e04f8cSWim Van Sebroeck {
284b7e04f8cSWim Van Sebroeck unsigned char msb = 0xA5;
285b7e04f8cSWim Van Sebroeck unsigned char lsb = 0xC3;
286b7e04f8cSWim Van Sebroeck int retval;
287b7e04f8cSWim Van Sebroeck
288b7e04f8cSWim Van Sebroeck /* Disable Watchdog */
289143a2e54SWim Van Sebroeck retval = usb_pcwd_send_command(usb_pcwd, CMD_DISABLE_WATCHDOG,
290143a2e54SWim Van Sebroeck &msb, &lsb);
291b7e04f8cSWim Van Sebroeck
292b7e04f8cSWim Van Sebroeck if ((retval == 0) || (lsb != 0)) {
29327c766aaSJoe Perches pr_err("Card did not acknowledge disable attempt\n");
294b7e04f8cSWim Van Sebroeck return -1;
295b7e04f8cSWim Van Sebroeck }
296b7e04f8cSWim Van Sebroeck
297b7e04f8cSWim Van Sebroeck return 0;
298b7e04f8cSWim Van Sebroeck }
299b7e04f8cSWim Van Sebroeck
usb_pcwd_keepalive(struct usb_pcwd_private * usb_pcwd)300b7e04f8cSWim Van Sebroeck static int usb_pcwd_keepalive(struct usb_pcwd_private *usb_pcwd)
301b7e04f8cSWim Van Sebroeck {
302b7e04f8cSWim Van Sebroeck unsigned char dummy;
303b7e04f8cSWim Van Sebroeck
304b7e04f8cSWim Van Sebroeck /* Re-trigger Watchdog */
305b7e04f8cSWim Van Sebroeck usb_pcwd_send_command(usb_pcwd, CMD_TRIGGER, &dummy, &dummy);
306b7e04f8cSWim Van Sebroeck
307b7e04f8cSWim Van Sebroeck return 0;
308b7e04f8cSWim Van Sebroeck }
309b7e04f8cSWim Van Sebroeck
usb_pcwd_set_heartbeat(struct usb_pcwd_private * usb_pcwd,int t)310b7e04f8cSWim Van Sebroeck static int usb_pcwd_set_heartbeat(struct usb_pcwd_private *usb_pcwd, int t)
311b7e04f8cSWim Van Sebroeck {
312b7e04f8cSWim Van Sebroeck unsigned char msb = t / 256;
313b7e04f8cSWim Van Sebroeck unsigned char lsb = t % 256;
314b7e04f8cSWim Van Sebroeck
315b7e04f8cSWim Van Sebroeck if ((t < 0x0001) || (t > 0xFFFF))
316b7e04f8cSWim Van Sebroeck return -EINVAL;
317b7e04f8cSWim Van Sebroeck
318b7e04f8cSWim Van Sebroeck /* Write new heartbeat to watchdog */
319b7e04f8cSWim Van Sebroeck usb_pcwd_send_command(usb_pcwd, CMD_WRITE_WATCHDOG_TIMEOUT, &msb, &lsb);
320b7e04f8cSWim Van Sebroeck
321b7e04f8cSWim Van Sebroeck heartbeat = t;
322b7e04f8cSWim Van Sebroeck return 0;
323b7e04f8cSWim Van Sebroeck }
324b7e04f8cSWim Van Sebroeck
usb_pcwd_get_temperature(struct usb_pcwd_private * usb_pcwd,int * temperature)325143a2e54SWim Van Sebroeck static int usb_pcwd_get_temperature(struct usb_pcwd_private *usb_pcwd,
326143a2e54SWim Van Sebroeck int *temperature)
327b7e04f8cSWim Van Sebroeck {
328*7d06c07cSLi Hua unsigned char msb = 0x00;
329*7d06c07cSLi Hua unsigned char lsb = 0x00;
330b7e04f8cSWim Van Sebroeck
331b7e04f8cSWim Van Sebroeck usb_pcwd_send_command(usb_pcwd, CMD_READ_TEMP, &msb, &lsb);
332b7e04f8cSWim Van Sebroeck
333b7e04f8cSWim Van Sebroeck /*
334b7e04f8cSWim Van Sebroeck * Convert celsius to fahrenheit, since this was
335b7e04f8cSWim Van Sebroeck * the decided 'standard' for this return value.
336b7e04f8cSWim Van Sebroeck */
337b7e04f8cSWim Van Sebroeck *temperature = (lsb * 9 / 5) + 32;
338b7e04f8cSWim Van Sebroeck
339b7e04f8cSWim Van Sebroeck return 0;
340b7e04f8cSWim Van Sebroeck }
341b7e04f8cSWim Van Sebroeck
usb_pcwd_get_timeleft(struct usb_pcwd_private * usb_pcwd,int * time_left)342143a2e54SWim Van Sebroeck static int usb_pcwd_get_timeleft(struct usb_pcwd_private *usb_pcwd,
343143a2e54SWim Van Sebroeck int *time_left)
344b7e04f8cSWim Van Sebroeck {
345*7d06c07cSLi Hua unsigned char msb = 0x00;
346*7d06c07cSLi Hua unsigned char lsb = 0x00;
347b7e04f8cSWim Van Sebroeck
348b7e04f8cSWim Van Sebroeck /* Read the time that's left before rebooting */
349b7e04f8cSWim Van Sebroeck /* Note: if the board is not yet armed then we will read 0xFFFF */
350b7e04f8cSWim Van Sebroeck usb_pcwd_send_command(usb_pcwd, CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
351b7e04f8cSWim Van Sebroeck
352b7e04f8cSWim Van Sebroeck *time_left = (msb << 8) + lsb;
353b7e04f8cSWim Van Sebroeck
354b7e04f8cSWim Van Sebroeck return 0;
355b7e04f8cSWim Van Sebroeck }
356b7e04f8cSWim Van Sebroeck
357b7e04f8cSWim Van Sebroeck /*
358b7e04f8cSWim Van Sebroeck * /dev/watchdog handling
359b7e04f8cSWim Van Sebroeck */
360b7e04f8cSWim Van Sebroeck
usb_pcwd_write(struct file * file,const char __user * data,size_t len,loff_t * ppos)361b7e04f8cSWim Van Sebroeck static ssize_t usb_pcwd_write(struct file *file, const char __user *data,
362b7e04f8cSWim Van Sebroeck size_t len, loff_t *ppos)
363b7e04f8cSWim Van Sebroeck {
364b7e04f8cSWim Van Sebroeck /* See if we got the magic character 'V' and reload the timer */
365b7e04f8cSWim Van Sebroeck if (len) {
366b7e04f8cSWim Van Sebroeck if (!nowayout) {
367b7e04f8cSWim Van Sebroeck size_t i;
368b7e04f8cSWim Van Sebroeck
369b7e04f8cSWim Van Sebroeck /* note: just in case someone wrote the magic character
370b7e04f8cSWim Van Sebroeck * five months ago... */
371b7e04f8cSWim Van Sebroeck expect_release = 0;
372b7e04f8cSWim Van Sebroeck
373143a2e54SWim Van Sebroeck /* scan to see whether or not we got the
374143a2e54SWim Van Sebroeck * magic character */
375b7e04f8cSWim Van Sebroeck for (i = 0; i != len; i++) {
376b7e04f8cSWim Van Sebroeck char c;
377b7e04f8cSWim Van Sebroeck if (get_user(c, data + i))
378b7e04f8cSWim Van Sebroeck return -EFAULT;
379b7e04f8cSWim Van Sebroeck if (c == 'V')
380b7e04f8cSWim Van Sebroeck expect_release = 42;
381b7e04f8cSWim Van Sebroeck }
382b7e04f8cSWim Van Sebroeck }
383b7e04f8cSWim Van Sebroeck
384b7e04f8cSWim Van Sebroeck /* someone wrote to us, we should reload the timer */
385b7e04f8cSWim Van Sebroeck usb_pcwd_keepalive(usb_pcwd_device);
386b7e04f8cSWim Van Sebroeck }
387b7e04f8cSWim Van Sebroeck return len;
388b7e04f8cSWim Van Sebroeck }
389b7e04f8cSWim Van Sebroeck
usb_pcwd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)390c9488520SAlan Cox static long usb_pcwd_ioctl(struct file *file, unsigned int cmd,
391c9488520SAlan Cox unsigned long arg)
392b7e04f8cSWim Van Sebroeck {
393b7e04f8cSWim Van Sebroeck void __user *argp = (void __user *)arg;
394b7e04f8cSWim Van Sebroeck int __user *p = argp;
39542747d71SWim Van Sebroeck static const struct watchdog_info ident = {
396b7e04f8cSWim Van Sebroeck .options = WDIOF_KEEPALIVEPING |
397b7e04f8cSWim Van Sebroeck WDIOF_SETTIMEOUT |
398b7e04f8cSWim Van Sebroeck WDIOF_MAGICCLOSE,
399b7e04f8cSWim Van Sebroeck .firmware_version = 1,
400b7e04f8cSWim Van Sebroeck .identity = DRIVER_NAME,
401b7e04f8cSWim Van Sebroeck };
402b7e04f8cSWim Van Sebroeck
403b7e04f8cSWim Van Sebroeck switch (cmd) {
404b7e04f8cSWim Van Sebroeck case WDIOC_GETSUPPORT:
4057944d3a5SWim Van Sebroeck return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
406b7e04f8cSWim Van Sebroeck
407b7e04f8cSWim Van Sebroeck case WDIOC_GETSTATUS:
408b7e04f8cSWim Van Sebroeck case WDIOC_GETBOOTSTATUS:
409b7e04f8cSWim Van Sebroeck return put_user(0, p);
410b7e04f8cSWim Van Sebroeck
411b7e04f8cSWim Van Sebroeck case WDIOC_GETTEMP:
412b7e04f8cSWim Van Sebroeck {
413b7e04f8cSWim Van Sebroeck int temperature;
414b7e04f8cSWim Van Sebroeck
415b7e04f8cSWim Van Sebroeck if (usb_pcwd_get_temperature(usb_pcwd_device, &temperature))
416b7e04f8cSWim Van Sebroeck return -EFAULT;
417b7e04f8cSWim Van Sebroeck
418b7e04f8cSWim Van Sebroeck return put_user(temperature, p);
419b7e04f8cSWim Van Sebroeck }
420b7e04f8cSWim Van Sebroeck
421b7e04f8cSWim Van Sebroeck case WDIOC_SETOPTIONS:
422b7e04f8cSWim Van Sebroeck {
423b7e04f8cSWim Van Sebroeck int new_options, retval = -EINVAL;
424b7e04f8cSWim Van Sebroeck
425b7e04f8cSWim Van Sebroeck if (get_user(new_options, p))
426b7e04f8cSWim Van Sebroeck return -EFAULT;
427b7e04f8cSWim Van Sebroeck
428b7e04f8cSWim Van Sebroeck if (new_options & WDIOS_DISABLECARD) {
429b7e04f8cSWim Van Sebroeck usb_pcwd_stop(usb_pcwd_device);
430b7e04f8cSWim Van Sebroeck retval = 0;
431b7e04f8cSWim Van Sebroeck }
432b7e04f8cSWim Van Sebroeck
433b7e04f8cSWim Van Sebroeck if (new_options & WDIOS_ENABLECARD) {
434b7e04f8cSWim Van Sebroeck usb_pcwd_start(usb_pcwd_device);
435b7e04f8cSWim Van Sebroeck retval = 0;
436b7e04f8cSWim Van Sebroeck }
437b7e04f8cSWim Van Sebroeck
438b7e04f8cSWim Van Sebroeck return retval;
439b7e04f8cSWim Van Sebroeck }
440b7e04f8cSWim Van Sebroeck
4410c06090cSWim Van Sebroeck case WDIOC_KEEPALIVE:
4420c06090cSWim Van Sebroeck usb_pcwd_keepalive(usb_pcwd_device);
4430c06090cSWim Van Sebroeck return 0;
4440c06090cSWim Van Sebroeck
445b7e04f8cSWim Van Sebroeck case WDIOC_SETTIMEOUT:
446b7e04f8cSWim Van Sebroeck {
447b7e04f8cSWim Van Sebroeck int new_heartbeat;
448b7e04f8cSWim Van Sebroeck
449b7e04f8cSWim Van Sebroeck if (get_user(new_heartbeat, p))
450b7e04f8cSWim Van Sebroeck return -EFAULT;
451b7e04f8cSWim Van Sebroeck
452b7e04f8cSWim Van Sebroeck if (usb_pcwd_set_heartbeat(usb_pcwd_device, new_heartbeat))
453b7e04f8cSWim Van Sebroeck return -EINVAL;
454b7e04f8cSWim Van Sebroeck
455b7e04f8cSWim Van Sebroeck usb_pcwd_keepalive(usb_pcwd_device);
456b7e04f8cSWim Van Sebroeck }
457bd490f82SGustavo A. R. Silva fallthrough;
458b7e04f8cSWim Van Sebroeck
459b7e04f8cSWim Van Sebroeck case WDIOC_GETTIMEOUT:
460b7e04f8cSWim Van Sebroeck return put_user(heartbeat, p);
461b7e04f8cSWim Van Sebroeck
462b7e04f8cSWim Van Sebroeck case WDIOC_GETTIMELEFT:
463b7e04f8cSWim Van Sebroeck {
464b7e04f8cSWim Van Sebroeck int time_left;
465b7e04f8cSWim Van Sebroeck
466b7e04f8cSWim Van Sebroeck if (usb_pcwd_get_timeleft(usb_pcwd_device, &time_left))
467b7e04f8cSWim Van Sebroeck return -EFAULT;
468b7e04f8cSWim Van Sebroeck
469b7e04f8cSWim Van Sebroeck return put_user(time_left, p);
470b7e04f8cSWim Van Sebroeck }
471b7e04f8cSWim Van Sebroeck
472b7e04f8cSWim Van Sebroeck default:
473b7e04f8cSWim Van Sebroeck return -ENOTTY;
474b7e04f8cSWim Van Sebroeck }
475b7e04f8cSWim Van Sebroeck }
476b7e04f8cSWim Van Sebroeck
usb_pcwd_open(struct inode * inode,struct file * file)477b7e04f8cSWim Van Sebroeck static int usb_pcwd_open(struct inode *inode, struct file *file)
478b7e04f8cSWim Van Sebroeck {
479b7e04f8cSWim Van Sebroeck /* /dev/watchdog can only be opened once */
480b7e04f8cSWim Van Sebroeck if (test_and_set_bit(0, &is_active))
481b7e04f8cSWim Van Sebroeck return -EBUSY;
482b7e04f8cSWim Van Sebroeck
483b7e04f8cSWim Van Sebroeck /* Activate */
484b7e04f8cSWim Van Sebroeck usb_pcwd_start(usb_pcwd_device);
485b7e04f8cSWim Van Sebroeck usb_pcwd_keepalive(usb_pcwd_device);
486c5bf68feSKirill Smelkov return stream_open(inode, file);
487b7e04f8cSWim Van Sebroeck }
488b7e04f8cSWim Van Sebroeck
usb_pcwd_release(struct inode * inode,struct file * file)489b7e04f8cSWim Van Sebroeck static int usb_pcwd_release(struct inode *inode, struct file *file)
490b7e04f8cSWim Van Sebroeck {
491b7e04f8cSWim Van Sebroeck /*
492b7e04f8cSWim Van Sebroeck * Shut off the timer.
493b7e04f8cSWim Van Sebroeck */
494b7e04f8cSWim Van Sebroeck if (expect_release == 42) {
495b7e04f8cSWim Van Sebroeck usb_pcwd_stop(usb_pcwd_device);
496b7e04f8cSWim Van Sebroeck } else {
49727c766aaSJoe Perches pr_crit("Unexpected close, not stopping watchdog!\n");
498b7e04f8cSWim Van Sebroeck usb_pcwd_keepalive(usb_pcwd_device);
499b7e04f8cSWim Van Sebroeck }
500b7e04f8cSWim Van Sebroeck expect_release = 0;
501b7e04f8cSWim Van Sebroeck clear_bit(0, &is_active);
502b7e04f8cSWim Van Sebroeck return 0;
503b7e04f8cSWim Van Sebroeck }
504b7e04f8cSWim Van Sebroeck
505b7e04f8cSWim Van Sebroeck /*
506b7e04f8cSWim Van Sebroeck * /dev/temperature handling
507b7e04f8cSWim Van Sebroeck */
508b7e04f8cSWim Van Sebroeck
usb_pcwd_temperature_read(struct file * file,char __user * data,size_t len,loff_t * ppos)509b7e04f8cSWim Van Sebroeck static ssize_t usb_pcwd_temperature_read(struct file *file, char __user *data,
510b7e04f8cSWim Van Sebroeck size_t len, loff_t *ppos)
511b7e04f8cSWim Van Sebroeck {
512b7e04f8cSWim Van Sebroeck int temperature;
513b7e04f8cSWim Van Sebroeck
514b7e04f8cSWim Van Sebroeck if (usb_pcwd_get_temperature(usb_pcwd_device, &temperature))
515b7e04f8cSWim Van Sebroeck return -EFAULT;
516b7e04f8cSWim Van Sebroeck
517b7e04f8cSWim Van Sebroeck if (copy_to_user(data, &temperature, 1))
518b7e04f8cSWim Van Sebroeck return -EFAULT;
519b7e04f8cSWim Van Sebroeck
520b7e04f8cSWim Van Sebroeck return 1;
521b7e04f8cSWim Van Sebroeck }
522b7e04f8cSWim Van Sebroeck
usb_pcwd_temperature_open(struct inode * inode,struct file * file)523b7e04f8cSWim Van Sebroeck static int usb_pcwd_temperature_open(struct inode *inode, struct file *file)
524b7e04f8cSWim Van Sebroeck {
525c5bf68feSKirill Smelkov return stream_open(inode, file);
526b7e04f8cSWim Van Sebroeck }
527b7e04f8cSWim Van Sebroeck
usb_pcwd_temperature_release(struct inode * inode,struct file * file)528b7e04f8cSWim Van Sebroeck static int usb_pcwd_temperature_release(struct inode *inode, struct file *file)
529b7e04f8cSWim Van Sebroeck {
530b7e04f8cSWim Van Sebroeck return 0;
531b7e04f8cSWim Van Sebroeck }
532b7e04f8cSWim Van Sebroeck
533b7e04f8cSWim Van Sebroeck /*
534b7e04f8cSWim Van Sebroeck * Notify system
535b7e04f8cSWim Van Sebroeck */
536b7e04f8cSWim Van Sebroeck
usb_pcwd_notify_sys(struct notifier_block * this,unsigned long code,void * unused)537143a2e54SWim Van Sebroeck static int usb_pcwd_notify_sys(struct notifier_block *this, unsigned long code,
538143a2e54SWim Van Sebroeck void *unused)
539b7e04f8cSWim Van Sebroeck {
5407944d3a5SWim Van Sebroeck if (code == SYS_DOWN || code == SYS_HALT)
5417944d3a5SWim Van Sebroeck usb_pcwd_stop(usb_pcwd_device); /* Turn the WDT off */
542b7e04f8cSWim Van Sebroeck
543b7e04f8cSWim Van Sebroeck return NOTIFY_DONE;
544b7e04f8cSWim Van Sebroeck }
545b7e04f8cSWim Van Sebroeck
546b7e04f8cSWim Van Sebroeck /*
547b7e04f8cSWim Van Sebroeck * Kernel Interfaces
548b7e04f8cSWim Van Sebroeck */
549b7e04f8cSWim Van Sebroeck
550b7e04f8cSWim Van Sebroeck static const struct file_operations usb_pcwd_fops = {
551b7e04f8cSWim Van Sebroeck .owner = THIS_MODULE,
552b7e04f8cSWim Van Sebroeck .llseek = no_llseek,
553b7e04f8cSWim Van Sebroeck .write = usb_pcwd_write,
554c9488520SAlan Cox .unlocked_ioctl = usb_pcwd_ioctl,
555b6dfb247SArnd Bergmann .compat_ioctl = compat_ptr_ioctl,
556b7e04f8cSWim Van Sebroeck .open = usb_pcwd_open,
557b7e04f8cSWim Van Sebroeck .release = usb_pcwd_release,
558b7e04f8cSWim Van Sebroeck };
559b7e04f8cSWim Van Sebroeck
560b7e04f8cSWim Van Sebroeck static struct miscdevice usb_pcwd_miscdev = {
561b7e04f8cSWim Van Sebroeck .minor = WATCHDOG_MINOR,
562b7e04f8cSWim Van Sebroeck .name = "watchdog",
563b7e04f8cSWim Van Sebroeck .fops = &usb_pcwd_fops,
564b7e04f8cSWim Van Sebroeck };
565b7e04f8cSWim Van Sebroeck
566b7e04f8cSWim Van Sebroeck static const struct file_operations usb_pcwd_temperature_fops = {
567b7e04f8cSWim Van Sebroeck .owner = THIS_MODULE,
568b7e04f8cSWim Van Sebroeck .llseek = no_llseek,
569b7e04f8cSWim Van Sebroeck .read = usb_pcwd_temperature_read,
570b7e04f8cSWim Van Sebroeck .open = usb_pcwd_temperature_open,
571b7e04f8cSWim Van Sebroeck .release = usb_pcwd_temperature_release,
572b7e04f8cSWim Van Sebroeck };
573b7e04f8cSWim Van Sebroeck
574b7e04f8cSWim Van Sebroeck static struct miscdevice usb_pcwd_temperature_miscdev = {
575b7e04f8cSWim Van Sebroeck .minor = TEMP_MINOR,
576b7e04f8cSWim Van Sebroeck .name = "temperature",
577b7e04f8cSWim Van Sebroeck .fops = &usb_pcwd_temperature_fops,
578b7e04f8cSWim Van Sebroeck };
579b7e04f8cSWim Van Sebroeck
580b7e04f8cSWim Van Sebroeck static struct notifier_block usb_pcwd_notifier = {
581b7e04f8cSWim Van Sebroeck .notifier_call = usb_pcwd_notify_sys,
582b7e04f8cSWim Van Sebroeck };
583b7e04f8cSWim Van Sebroeck
584b7e04f8cSWim Van Sebroeck /**
585b7e04f8cSWim Van Sebroeck * usb_pcwd_delete
586b7e04f8cSWim Van Sebroeck */
usb_pcwd_delete(struct usb_pcwd_private * usb_pcwd)587b7e04f8cSWim Van Sebroeck static inline void usb_pcwd_delete(struct usb_pcwd_private *usb_pcwd)
588b7e04f8cSWim Van Sebroeck {
589b7e04f8cSWim Van Sebroeck usb_free_urb(usb_pcwd->intr_urb);
590997ea58eSDaniel Mack usb_free_coherent(usb_pcwd->udev, usb_pcwd->intr_size,
591b7e04f8cSWim Van Sebroeck usb_pcwd->intr_buffer, usb_pcwd->intr_dma);
592b7e04f8cSWim Van Sebroeck kfree(usb_pcwd);
593b7e04f8cSWim Van Sebroeck }
594b7e04f8cSWim Van Sebroeck
595b7e04f8cSWim Van Sebroeck /**
596b7e04f8cSWim Van Sebroeck * usb_pcwd_probe
597b7e04f8cSWim Van Sebroeck *
598b7e04f8cSWim Van Sebroeck * Called by the usb core when a new device is connected that it thinks
599b7e04f8cSWim Van Sebroeck * this driver might be interested in.
600b7e04f8cSWim Van Sebroeck */
usb_pcwd_probe(struct usb_interface * interface,const struct usb_device_id * id)601143a2e54SWim Van Sebroeck static int usb_pcwd_probe(struct usb_interface *interface,
602143a2e54SWim Van Sebroeck const struct usb_device_id *id)
603b7e04f8cSWim Van Sebroeck {
604b7e04f8cSWim Van Sebroeck struct usb_device *udev = interface_to_usbdev(interface);
605b7e04f8cSWim Van Sebroeck struct usb_host_interface *iface_desc;
606b7e04f8cSWim Van Sebroeck struct usb_endpoint_descriptor *endpoint;
607b7e04f8cSWim Van Sebroeck struct usb_pcwd_private *usb_pcwd = NULL;
60899f08ca8STomas Winkler int pipe;
609b7e04f8cSWim Van Sebroeck int retval = -ENOMEM;
610b7e04f8cSWim Van Sebroeck int got_fw_rev;
611b7e04f8cSWim Van Sebroeck unsigned char fw_rev_major, fw_rev_minor;
612b7e04f8cSWim Van Sebroeck char fw_ver_str[20];
613b7e04f8cSWim Van Sebroeck unsigned char option_switches, dummy;
614b7e04f8cSWim Van Sebroeck
615b7e04f8cSWim Van Sebroeck cards_found++;
616b7e04f8cSWim Van Sebroeck if (cards_found > 1) {
61727c766aaSJoe Perches pr_err("This driver only supports 1 device\n");
618b7e04f8cSWim Van Sebroeck return -ENODEV;
619b7e04f8cSWim Van Sebroeck }
620b7e04f8cSWim Van Sebroeck
621b7e04f8cSWim Van Sebroeck /* get the active interface descriptor */
622b7e04f8cSWim Van Sebroeck iface_desc = interface->cur_altsetting;
623b7e04f8cSWim Van Sebroeck
624b7e04f8cSWim Van Sebroeck /* check out that we have a HID device */
625b7e04f8cSWim Van Sebroeck if (!(iface_desc->desc.bInterfaceClass == USB_CLASS_HID)) {
62627c766aaSJoe Perches pr_err("The device isn't a Human Interface Device\n");
627b7e04f8cSWim Van Sebroeck return -ENODEV;
628b7e04f8cSWim Van Sebroeck }
629b7e04f8cSWim Van Sebroeck
63046c319b8SJohan Hovold if (iface_desc->desc.bNumEndpoints < 1)
63146c319b8SJohan Hovold return -ENODEV;
63246c319b8SJohan Hovold
633b7e04f8cSWim Van Sebroeck /* check out the endpoint: it has to be Interrupt & IN */
634b7e04f8cSWim Van Sebroeck endpoint = &iface_desc->endpoint[0].desc;
635b7e04f8cSWim Van Sebroeck
6362855d28aSJulia Lawall if (!usb_endpoint_is_int_in(endpoint)) {
637b7e04f8cSWim Van Sebroeck /* we didn't find a Interrupt endpoint with direction IN */
63827c766aaSJoe Perches pr_err("Couldn't find an INTR & IN endpoint\n");
639b7e04f8cSWim Van Sebroeck return -ENODEV;
640b7e04f8cSWim Van Sebroeck }
641b7e04f8cSWim Van Sebroeck
642b7e04f8cSWim Van Sebroeck /* get a handle to the interrupt data pipe */
643b7e04f8cSWim Van Sebroeck pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
644b7e04f8cSWim Van Sebroeck
645b7e04f8cSWim Van Sebroeck /* allocate memory for our device and initialize it */
646b7e04f8cSWim Van Sebroeck usb_pcwd = kzalloc(sizeof(struct usb_pcwd_private), GFP_KERNEL);
6478deea830SJingoo Han if (usb_pcwd == NULL)
648b7e04f8cSWim Van Sebroeck goto error;
649b7e04f8cSWim Van Sebroeck
650b7e04f8cSWim Van Sebroeck usb_pcwd_device = usb_pcwd;
651b7e04f8cSWim Van Sebroeck
652b7e04f8cSWim Van Sebroeck mutex_init(&usb_pcwd->mtx);
653b7e04f8cSWim Van Sebroeck usb_pcwd->udev = udev;
654b7e04f8cSWim Van Sebroeck usb_pcwd->interface = interface;
655b7e04f8cSWim Van Sebroeck usb_pcwd->interface_number = iface_desc->desc.bInterfaceNumber;
656143a2e54SWim Van Sebroeck usb_pcwd->intr_size = (le16_to_cpu(endpoint->wMaxPacketSize) > 8 ?
657143a2e54SWim Van Sebroeck le16_to_cpu(endpoint->wMaxPacketSize) : 8);
658b7e04f8cSWim Van Sebroeck
659b7e04f8cSWim Van Sebroeck /* set up the memory buffer's */
660997ea58eSDaniel Mack usb_pcwd->intr_buffer = usb_alloc_coherent(udev, usb_pcwd->intr_size,
661b6576210SChristophe JAILLET GFP_KERNEL, &usb_pcwd->intr_dma);
6627944d3a5SWim Van Sebroeck if (!usb_pcwd->intr_buffer) {
66327c766aaSJoe Perches pr_err("Out of memory\n");
664b7e04f8cSWim Van Sebroeck goto error;
665b7e04f8cSWim Van Sebroeck }
666b7e04f8cSWim Van Sebroeck
667b7e04f8cSWim Van Sebroeck /* allocate the urb's */
668b7e04f8cSWim Van Sebroeck usb_pcwd->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
66978a4a0d2SWolfram Sang if (!usb_pcwd->intr_urb)
670b7e04f8cSWim Van Sebroeck goto error;
671b7e04f8cSWim Van Sebroeck
672b7e04f8cSWim Van Sebroeck /* initialise the intr urb's */
673b7e04f8cSWim Van Sebroeck usb_fill_int_urb(usb_pcwd->intr_urb, udev, pipe,
674b7e04f8cSWim Van Sebroeck usb_pcwd->intr_buffer, usb_pcwd->intr_size,
675b7e04f8cSWim Van Sebroeck usb_pcwd_intr_done, usb_pcwd, endpoint->bInterval);
676b7e04f8cSWim Van Sebroeck usb_pcwd->intr_urb->transfer_dma = usb_pcwd->intr_dma;
677b7e04f8cSWim Van Sebroeck usb_pcwd->intr_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
678b7e04f8cSWim Van Sebroeck
679b7e04f8cSWim Van Sebroeck /* register our interrupt URB with the USB system */
680b7e04f8cSWim Van Sebroeck if (usb_submit_urb(usb_pcwd->intr_urb, GFP_KERNEL)) {
68127c766aaSJoe Perches pr_err("Problem registering interrupt URB\n");
682b7e04f8cSWim Van Sebroeck retval = -EIO; /* failure */
683b7e04f8cSWim Van Sebroeck goto error;
684b7e04f8cSWim Van Sebroeck }
685b7e04f8cSWim Van Sebroeck
686b7e04f8cSWim Van Sebroeck /* The device exists and can be communicated with */
687b7e04f8cSWim Van Sebroeck usb_pcwd->exists = 1;
688b7e04f8cSWim Van Sebroeck
689b7e04f8cSWim Van Sebroeck /* disable card */
690b7e04f8cSWim Van Sebroeck usb_pcwd_stop(usb_pcwd);
691b7e04f8cSWim Van Sebroeck
692b7e04f8cSWim Van Sebroeck /* Get the Firmware Version */
693143a2e54SWim Van Sebroeck got_fw_rev = usb_pcwd_send_command(usb_pcwd, CMD_GET_FIRMWARE_VERSION,
694143a2e54SWim Van Sebroeck &fw_rev_major, &fw_rev_minor);
6957944d3a5SWim Van Sebroeck if (got_fw_rev)
696b7e04f8cSWim Van Sebroeck sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
6977944d3a5SWim Van Sebroeck else
698b7e04f8cSWim Van Sebroeck sprintf(fw_ver_str, "<card no answer>");
699b7e04f8cSWim Van Sebroeck
70027c766aaSJoe Perches pr_info("Found card (Firmware: %s) with temp option\n", fw_ver_str);
701b7e04f8cSWim Van Sebroeck
702b7e04f8cSWim Van Sebroeck /* Get switch settings */
703143a2e54SWim Van Sebroeck usb_pcwd_send_command(usb_pcwd, CMD_GET_DIP_SWITCH_SETTINGS, &dummy,
704143a2e54SWim Van Sebroeck &option_switches);
705b7e04f8cSWim Van Sebroeck
70627c766aaSJoe Perches pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
707b7e04f8cSWim Van Sebroeck option_switches,
708b7e04f8cSWim Van Sebroeck ((option_switches & 0x10) ? "ON" : "OFF"),
709b7e04f8cSWim Van Sebroeck ((option_switches & 0x08) ? "ON" : "OFF"));
710b7e04f8cSWim Van Sebroeck
711b7e04f8cSWim Van Sebroeck /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
712b7e04f8cSWim Van Sebroeck if (heartbeat == 0)
713b7e04f8cSWim Van Sebroeck heartbeat = heartbeat_tbl[(option_switches & 0x07)];
714b7e04f8cSWim Van Sebroeck
715143a2e54SWim Van Sebroeck /* Check that the heartbeat value is within it's range ;
716143a2e54SWim Van Sebroeck * if not reset to the default */
717b7e04f8cSWim Van Sebroeck if (usb_pcwd_set_heartbeat(usb_pcwd, heartbeat)) {
718b7e04f8cSWim Van Sebroeck usb_pcwd_set_heartbeat(usb_pcwd, WATCHDOG_HEARTBEAT);
71927c766aaSJoe Perches pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
720b7e04f8cSWim Van Sebroeck WATCHDOG_HEARTBEAT);
721b7e04f8cSWim Van Sebroeck }
722b7e04f8cSWim Van Sebroeck
723b7e04f8cSWim Van Sebroeck retval = register_reboot_notifier(&usb_pcwd_notifier);
724b7e04f8cSWim Van Sebroeck if (retval != 0) {
72527c766aaSJoe Perches pr_err("cannot register reboot notifier (err=%d)\n", retval);
726b7e04f8cSWim Van Sebroeck goto error;
727b7e04f8cSWim Van Sebroeck }
728b7e04f8cSWim Van Sebroeck
729b7e04f8cSWim Van Sebroeck retval = misc_register(&usb_pcwd_temperature_miscdev);
730b7e04f8cSWim Van Sebroeck if (retval != 0) {
73127c766aaSJoe Perches pr_err("cannot register miscdev on minor=%d (err=%d)\n",
732b7e04f8cSWim Van Sebroeck TEMP_MINOR, retval);
733b7e04f8cSWim Van Sebroeck goto err_out_unregister_reboot;
734b7e04f8cSWim Van Sebroeck }
735b7e04f8cSWim Van Sebroeck
736b7e04f8cSWim Van Sebroeck retval = misc_register(&usb_pcwd_miscdev);
737b7e04f8cSWim Van Sebroeck if (retval != 0) {
73827c766aaSJoe Perches pr_err("cannot register miscdev on minor=%d (err=%d)\n",
739b7e04f8cSWim Van Sebroeck WATCHDOG_MINOR, retval);
740b7e04f8cSWim Van Sebroeck goto err_out_misc_deregister;
741b7e04f8cSWim Van Sebroeck }
742b7e04f8cSWim Van Sebroeck
743b7e04f8cSWim Van Sebroeck /* we can register the device now, as it is ready */
744b7e04f8cSWim Van Sebroeck usb_set_intfdata(interface, usb_pcwd);
745b7e04f8cSWim Van Sebroeck
74627c766aaSJoe Perches pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
747b7e04f8cSWim Van Sebroeck heartbeat, nowayout);
748b7e04f8cSWim Van Sebroeck
749b7e04f8cSWim Van Sebroeck return 0;
750b7e04f8cSWim Van Sebroeck
751b7e04f8cSWim Van Sebroeck err_out_misc_deregister:
752b7e04f8cSWim Van Sebroeck misc_deregister(&usb_pcwd_temperature_miscdev);
753b7e04f8cSWim Van Sebroeck err_out_unregister_reboot:
754b7e04f8cSWim Van Sebroeck unregister_reboot_notifier(&usb_pcwd_notifier);
755b7e04f8cSWim Van Sebroeck error:
756b7e04f8cSWim Van Sebroeck if (usb_pcwd)
757b7e04f8cSWim Van Sebroeck usb_pcwd_delete(usb_pcwd);
758b7e04f8cSWim Van Sebroeck usb_pcwd_device = NULL;
759b7e04f8cSWim Van Sebroeck return retval;
760b7e04f8cSWim Van Sebroeck }
761b7e04f8cSWim Van Sebroeck
762b7e04f8cSWim Van Sebroeck
763b7e04f8cSWim Van Sebroeck /**
764b7e04f8cSWim Van Sebroeck * usb_pcwd_disconnect
765b7e04f8cSWim Van Sebroeck *
766b7e04f8cSWim Van Sebroeck * Called by the usb core when the device is removed from the system.
767b7e04f8cSWim Van Sebroeck *
768b7e04f8cSWim Van Sebroeck * This routine guarantees that the driver will not submit any more urbs
769b7e04f8cSWim Van Sebroeck * by clearing dev->udev.
770b7e04f8cSWim Van Sebroeck */
usb_pcwd_disconnect(struct usb_interface * interface)771b7e04f8cSWim Van Sebroeck static void usb_pcwd_disconnect(struct usb_interface *interface)
772b7e04f8cSWim Van Sebroeck {
773b7e04f8cSWim Van Sebroeck struct usb_pcwd_private *usb_pcwd;
774b7e04f8cSWim Van Sebroeck
775b7e04f8cSWim Van Sebroeck /* prevent races with open() */
776b7e04f8cSWim Van Sebroeck mutex_lock(&disconnect_mutex);
777b7e04f8cSWim Van Sebroeck
778b7e04f8cSWim Van Sebroeck usb_pcwd = usb_get_intfdata(interface);
779b7e04f8cSWim Van Sebroeck usb_set_intfdata(interface, NULL);
780b7e04f8cSWim Van Sebroeck
781b7e04f8cSWim Van Sebroeck mutex_lock(&usb_pcwd->mtx);
782b7e04f8cSWim Van Sebroeck
783b7e04f8cSWim Van Sebroeck /* Stop the timer before we leave */
784b7e04f8cSWim Van Sebroeck if (!nowayout)
785b7e04f8cSWim Van Sebroeck usb_pcwd_stop(usb_pcwd);
786b7e04f8cSWim Van Sebroeck
787b7e04f8cSWim Van Sebroeck /* We should now stop communicating with the USB PCWD device */
788b7e04f8cSWim Van Sebroeck usb_pcwd->exists = 0;
789b7e04f8cSWim Van Sebroeck
790b7e04f8cSWim Van Sebroeck /* Deregister */
791b7e04f8cSWim Van Sebroeck misc_deregister(&usb_pcwd_miscdev);
792b7e04f8cSWim Van Sebroeck misc_deregister(&usb_pcwd_temperature_miscdev);
793b7e04f8cSWim Van Sebroeck unregister_reboot_notifier(&usb_pcwd_notifier);
794b7e04f8cSWim Van Sebroeck
795b7e04f8cSWim Van Sebroeck mutex_unlock(&usb_pcwd->mtx);
796b7e04f8cSWim Van Sebroeck
797b7e04f8cSWim Van Sebroeck /* Delete the USB PCWD device */
798b7e04f8cSWim Van Sebroeck usb_pcwd_delete(usb_pcwd);
799b7e04f8cSWim Van Sebroeck
800b7e04f8cSWim Van Sebroeck cards_found--;
801b7e04f8cSWim Van Sebroeck
802b7e04f8cSWim Van Sebroeck mutex_unlock(&disconnect_mutex);
803b7e04f8cSWim Van Sebroeck
80427c766aaSJoe Perches pr_info("USB PC Watchdog disconnected\n");
805b7e04f8cSWim Van Sebroeck }
806b7e04f8cSWim Van Sebroeck
807fe748483SGreg Kroah-Hartman module_usb_driver(usb_pcwd_driver);
808