ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / pcwd_usb.c
1 /*
2  *      Berkshire USB-PC Watchdog Card Driver
3  *
4  *      (c) Copyright 2004 Wim Van Sebroeck <wim@iguana.be>.
5  *
6  *      Based on source code of the following authors:
7  *        Ken Hollis <kenji@bitgate.com>,
8  *        Alan Cox <alan@redhat.com>,
9  *        Matt Domsch <Matt_Domsch@dell.com>,
10  *        Rob Radez <rob@osinvestor.com>,
11  *        Greg Kroah-Hartman <greg@kroah.com>
12  *
13  *      This program is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU General Public License
15  *      as published by the Free Software Foundation; either version
16  *      2 of the License, or (at your option) any later version.
17  *
18  *      Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
19  *      provide warranty for any of this software. This material is
20  *      provided "AS-IS" and at no charge.
21  *
22  *      Thanks also to Simon Machell at Berkshire Products Inc. for
23  *      providing the test hardware. More info is available at
24  *      http://www.berkprod.com/ or http://www.pcwatchdog.com/
25  */
26
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/types.h>
35 #include <linux/delay.h>
36 #include <linux/miscdevice.h>
37 #include <linux/watchdog.h>
38 #include <linux/notifier.h>
39 #include <linux/reboot.h>
40 #include <linux/fs.h>
41 #include <linux/smp_lock.h>
42 #include <linux/completion.h>
43 #include <asm/uaccess.h>
44 #include <linux/usb.h>
45
46
47 #ifdef CONFIG_USB_DEBUG
48         static int debug = 1;
49 #else
50         static int debug;
51 #endif
52
53 /* Use our own dbg macro */
54 #undef dbg
55 #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG PFX format "\n" , ## arg); } while (0)
56
57
58 /* Module and Version Information */
59 #define DRIVER_VERSION "v1.00 (28/02/2004)"
60 #define DRIVER_AUTHOR "Wim Van Sebroeck <wim@iguana.be>"
61 #define DRIVER_DESC "Berkshire USB-PC Watchdog driver"
62 #define DRIVER_LICENSE "GPL"
63 #define DRIVER_NAME "pcwd_usb"
64 #define PFX DRIVER_NAME ": "
65
66 MODULE_AUTHOR(DRIVER_AUTHOR);
67 MODULE_DESCRIPTION(DRIVER_DESC);
68 MODULE_LICENSE(DRIVER_LICENSE);
69 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
70 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
71
72 /* Module Parameters */
73 module_param(debug, int, 0);
74 MODULE_PARM_DESC(debug, "Debug enabled or not");
75
76 #define WATCHDOG_HEARTBEAT 2    /* 2 sec default heartbeat */
77 static int heartbeat = WATCHDOG_HEARTBEAT;
78 module_param(heartbeat, int, 0);
79 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
80
81 #ifdef CONFIG_WATCHDOG_NOWAYOUT
82 static int nowayout = 1;
83 #else
84 static int nowayout = 0;
85 #endif
86
87 module_param(nowayout, int, 0);
88 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
89
90 /* The vendor and product id's for the USB-PC Watchdog card */
91 #define USB_PCWD_VENDOR_ID      0x0c98
92 #define USB_PCWD_PRODUCT_ID     0x1140
93
94 /* table of devices that work with this driver */
95 static struct usb_device_id usb_pcwd_table [] = {
96         { USB_DEVICE(USB_PCWD_VENDOR_ID, USB_PCWD_PRODUCT_ID) },
97         { }                                     /* Terminating entry */
98 };
99 MODULE_DEVICE_TABLE (usb, usb_pcwd_table);
100
101 /* according to documentation max. time to process a command for the USB
102  * watchdog card is 100 or 200 ms, so we give it 250 ms to do it's job */
103 #define USB_COMMAND_TIMEOUT     250
104
105 /* Watchdog's internal commands */
106 #define CMD_READ_TEMP                   0x02    /* Read Temperature; Re-trigger Watchdog */
107 #define CMD_TRIGGER                     CMD_READ_TEMP
108 #define CMD_GET_STATUS                  0x04    /* Get Status Information */
109 #define CMD_GET_FIRMWARE_VERSION        0x08    /* Get Firmware Version */
110 #define CMD_GET_DIP_SWITCH_SETTINGS     0x0c    /* Get Dip Switch Settings */
111 #define CMD_READ_WATCHDOG_TIMEOUT       0x18    /* Read Current Watchdog Time */
112 #define CMD_WRITE_WATCHDOG_TIMEOUT      0x19    /* Write Current Watchdog Time */
113 #define CMD_ENABLE_WATCHDOG             0x30    /* Enable / Disable Watchdog */
114 #define CMD_DISABLE_WATCHDOG            CMD_ENABLE_WATCHDOG
115
116 /* Some defines that I like to be somewhere else like include/linux/usb_hid.h */
117 #define HID_REQ_SET_REPORT              0x09
118 #define HID_DT_REPORT                   (USB_TYPE_CLASS | 0x02)
119
120 /* We can only use 1 card due to the /dev/watchdog restriction */
121 static int cards_found;
122
123 /* some internal variables */
124 static unsigned long is_active;
125 static char expect_release;
126
127 /* Structure to hold all of our device specific stuff */
128 struct usb_pcwd_private {
129         struct usb_device *     udev;                   /* save off the usb device pointer */
130         struct usb_interface *  interface;              /* the interface for this device */
131
132         unsigned int            interface_number;       /* the interface number used for cmd's */
133
134         unsigned char *         intr_buffer;            /* the buffer to intr data */
135         dma_addr_t              intr_dma;               /* the dma address for the intr buffer */
136         size_t                  intr_size;              /* the size of the intr buffer */
137         struct urb *            intr_urb;               /* the urb used for the intr pipe */
138
139         unsigned char           cmd_command;            /* The command that is reported back */
140         unsigned char           cmd_data_msb;           /* The data MSB that is reported back */
141         unsigned char           cmd_data_lsb;           /* The data LSB that is reported back */
142         atomic_t                cmd_received;           /* true if we received a report after a command */
143
144         int                     exists;                 /* Wether or not the device exists */
145         struct semaphore        sem;                    /* locks this structure */
146 };
147 static struct usb_pcwd_private *usb_pcwd_device;
148
149 /* prevent races between open() and disconnect() */
150 static DECLARE_MUTEX (disconnect_sem);
151
152 /* local function prototypes */
153 static int usb_pcwd_probe       (struct usb_interface *interface, const struct usb_device_id *id);
154 static void usb_pcwd_disconnect (struct usb_interface *interface);
155
156 /* usb specific object needed to register this driver with the usb subsystem */
157 static struct usb_driver usb_pcwd_driver = {
158         .owner =        THIS_MODULE,
159         .name =         DRIVER_NAME,
160         .probe =        usb_pcwd_probe,
161         .disconnect =   usb_pcwd_disconnect,
162         .id_table =     usb_pcwd_table,
163 };
164
165
166 static void usb_pcwd_intr_done(struct urb *urb, struct pt_regs *regs)
167 {
168         struct usb_pcwd_private *usb_pcwd = (struct usb_pcwd_private *)urb->context;
169         unsigned char *data = usb_pcwd->intr_buffer;
170         int retval;
171
172         switch (urb->status) {
173         case 0:                 /* success */
174                 break;
175         case -ECONNRESET:       /* unlink */
176         case -ENOENT:
177         case -ESHUTDOWN:
178                 /* this urb is terminated, clean up */
179                 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
180                 return;
181         /* -EPIPE:  should clear the halt */
182         default:                /* error */
183                 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
184                 goto resubmit;
185         }
186
187         dbg("received following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
188                 data[0], data[1], data[2]);
189
190         usb_pcwd->cmd_command  = data[0];
191         usb_pcwd->cmd_data_msb = data[1];
192         usb_pcwd->cmd_data_lsb = data[2];
193
194         /* notify anyone waiting that the cmd has finished */
195         atomic_set (&usb_pcwd->cmd_received, 1);
196
197 resubmit:
198         retval = usb_submit_urb (urb, GFP_ATOMIC);
199         if (retval)
200                 printk(KERN_ERR PFX "can't resubmit intr, usb_submit_urb failed with result %d\n",
201                         retval);
202 }
203
204 static int usb_pcwd_send_command(struct usb_pcwd_private *usb_pcwd, unsigned char cmd,
205         unsigned char *msb, unsigned char *lsb)
206 {
207         int got_response, count;
208         unsigned char buf[6];
209
210         /* We will not send any commands if the USB PCWD device does not exist */
211         if ((!usb_pcwd) || (!usb_pcwd->exists))
212                 return -1;
213
214         /* The USB PC Watchdog uses a 6 byte report format. The board currently uses
215          * only 3 of the six bytes of the report. */
216         buf[0] = cmd;                   /* Byte 0 = CMD */
217         buf[1] = *msb;                  /* Byte 1 = Data MSB */
218         buf[2] = *lsb;                  /* Byte 2 = Data LSB */
219         buf[3] = buf[4] = buf[5] = 0;   /* All other bytes not used */
220
221         dbg("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
222                 buf[0], buf[1], buf[2]);
223
224         atomic_set (&usb_pcwd->cmd_received, 0);
225
226         if (usb_control_msg(usb_pcwd->udev, usb_sndctrlpipe(usb_pcwd->udev, 0),
227                         HID_REQ_SET_REPORT, HID_DT_REPORT,
228                         0x0200, usb_pcwd->interface_number, buf, sizeof(buf),
229                         HZ) != sizeof(buf)) {
230                 dbg("usb_pcwd_send_command: error in usb_control_msg for cmd 0x%x 0x%x 0x%x\n", cmd, *msb, *lsb);
231         }
232         /* wait till the usb card processed the command,
233          * with a max. timeout of USB_COMMAND_TIMEOUT */
234         got_response = 0;
235         for (count = 0; (count < USB_COMMAND_TIMEOUT) && (!got_response); count++) {
236                 mdelay(1);
237                 if (atomic_read (&usb_pcwd->cmd_received))
238                         got_response = 1;
239         }
240
241         if ((got_response) && (cmd == usb_pcwd->cmd_command)) {
242                 /* read back response */
243                 *msb = usb_pcwd->cmd_data_msb;
244                 *lsb = usb_pcwd->cmd_data_lsb;
245         }
246
247         return got_response;
248 }
249
250 static int usb_pcwd_start(struct usb_pcwd_private *usb_pcwd)
251 {
252         unsigned char msb = 0x00;
253         unsigned char lsb = 0x00;
254         int retval;
255
256         /* Enable Watchdog */
257         retval = usb_pcwd_send_command(usb_pcwd, CMD_ENABLE_WATCHDOG, &msb, &lsb);
258
259         if ((retval == 0) || (lsb == 0)) {
260                 printk(KERN_ERR PFX "Card did not acknowledge enable attempt\n");
261                 return -1;
262         }
263
264         return 0;
265 }
266
267 static int usb_pcwd_stop(struct usb_pcwd_private *usb_pcwd)
268 {
269         unsigned char msb = 0xA5;
270         unsigned char lsb = 0xC3;
271         int retval;
272
273         /* Disable Watchdog */
274         retval = usb_pcwd_send_command(usb_pcwd, CMD_DISABLE_WATCHDOG, &msb, &lsb);
275
276         if ((retval == 0) || (lsb != 0)) {
277                 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
278                 return -1;
279         }
280
281         return 0;
282 }
283
284 static int usb_pcwd_keepalive(struct usb_pcwd_private *usb_pcwd)
285 {
286         unsigned char dummy;
287
288         /* Re-trigger Watchdog */
289         usb_pcwd_send_command(usb_pcwd, CMD_TRIGGER, &dummy, &dummy);
290
291         return 0;
292 }
293
294 static int usb_pcwd_set_heartbeat(struct usb_pcwd_private *usb_pcwd, int t)
295 {
296         unsigned char msb = t / 256;
297         unsigned char lsb = t % 256;
298
299         if ((t < 0x0001) || (t > 0xFFFF))
300                 return -EINVAL;
301
302         /* Write new heartbeat to watchdog */
303         usb_pcwd_send_command(usb_pcwd, CMD_WRITE_WATCHDOG_TIMEOUT, &msb, &lsb);
304
305         heartbeat = t;
306         return 0;
307 }
308
309 static int usb_pcwd_get_temperature(struct usb_pcwd_private *usb_pcwd, int *temperature)
310 {
311         unsigned char msb, lsb;
312
313         usb_pcwd_send_command(usb_pcwd, CMD_READ_TEMP, &msb, &lsb);
314
315         /*
316          * Convert celsius to fahrenheit, since this was
317          * the decided 'standard' for this return value.
318          */
319         *temperature = (lsb * 9 / 5) + 32;
320
321         return 0;
322 }
323
324 /*
325  *      /dev/watchdog handling
326  */
327
328 static ssize_t usb_pcwd_write(struct file *file, const char *data,
329                               size_t len, loff_t *ppos)
330 {
331         /* Can't seek (pwrite) on this device  */
332         if (ppos != &file->f_pos)
333                 return -ESPIPE;
334
335         /* See if we got the magic character 'V' and reload the timer */
336         if (len) {
337                 if (!nowayout) {
338                         size_t i;
339
340                         /* note: just in case someone wrote the magic character
341                          * five months ago... */
342                         expect_release = 0;
343
344                         /* scan to see whether or not we got the magic character */
345                         for (i = 0; i != len; i++) {
346                                 char c;
347                                 if(get_user(c, data+i))
348                                         return -EFAULT;
349                                 if (c == 'V')
350                                         expect_release = 42;
351                         }
352                 }
353
354                 /* someone wrote to us, we should reload the timer */
355                 usb_pcwd_keepalive(usb_pcwd_device);
356         }
357         return len;
358 }
359
360 static int usb_pcwd_ioctl(struct inode *inode, struct file *file,
361                           unsigned int cmd, unsigned long arg)
362 {
363         static struct watchdog_info ident = {
364                 .options =              WDIOF_KEEPALIVEPING |
365                                         WDIOF_SETTIMEOUT |
366                                         WDIOF_MAGICCLOSE,
367                 .firmware_version =     1,
368                 .identity =             DRIVER_NAME,
369         };
370
371         switch (cmd) {
372                 case WDIOC_GETSUPPORT:
373                         return copy_to_user((struct watchdog_info *) arg, &ident,
374                                 sizeof (ident)) ? -EFAULT : 0;
375
376                 case WDIOC_GETSTATUS:
377                 case WDIOC_GETBOOTSTATUS:
378                         return put_user(0, (int *) arg);
379
380                 case WDIOC_GETTEMP:
381                 {
382                         int temperature;
383
384                         if (usb_pcwd_get_temperature(usb_pcwd_device, &temperature))
385                                 return -EFAULT;
386
387                         return put_user(temperature, (int *) arg);
388                 }
389
390                 case WDIOC_KEEPALIVE:
391                         usb_pcwd_keepalive(usb_pcwd_device);
392                         return 0;
393
394                 case WDIOC_SETOPTIONS:
395                 {
396                         int new_options, retval = -EINVAL;
397
398                         if (get_user (new_options, (int *) arg))
399                                 return -EFAULT;
400
401                         if (new_options & WDIOS_DISABLECARD) {
402                                 usb_pcwd_stop(usb_pcwd_device);
403                                 retval = 0;
404                         }
405
406                         if (new_options & WDIOS_ENABLECARD) {
407                                 usb_pcwd_start(usb_pcwd_device);
408                                 retval = 0;
409                         }
410
411                         return retval;
412                 }
413
414                 case WDIOC_SETTIMEOUT:
415                 {
416                         int new_heartbeat;
417
418                         if (get_user(new_heartbeat, (int *) arg))
419                                 return -EFAULT;
420
421                         if (usb_pcwd_set_heartbeat(usb_pcwd_device, new_heartbeat))
422                             return -EINVAL;
423
424                         usb_pcwd_keepalive(usb_pcwd_device);
425                         /* Fall */
426                 }
427
428                 case WDIOC_GETTIMEOUT:
429                         return put_user(heartbeat, (int *)arg);
430
431                 default:
432                         return -ENOIOCTLCMD;
433         }
434 }
435
436 static int usb_pcwd_open(struct inode *inode, struct file *file)
437 {
438         /* /dev/watchdog can only be opened once */
439         if (test_and_set_bit(0, &is_active))
440                 return -EBUSY;
441
442         /* Activate */
443         usb_pcwd_start(usb_pcwd_device);
444         usb_pcwd_keepalive(usb_pcwd_device);
445         return 0;
446 }
447
448 static int usb_pcwd_release(struct inode *inode, struct file *file)
449 {
450         /*
451          *      Shut off the timer.
452          */
453         if (expect_release == 42) {
454                 usb_pcwd_stop(usb_pcwd_device);
455         } else {
456                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
457                 usb_pcwd_keepalive(usb_pcwd_device);
458         }
459         clear_bit(0, &is_active);
460         expect_release = 0;
461         return 0;
462 }
463
464 /*
465  *      /dev/temperature handling
466  */
467
468 static ssize_t usb_pcwd_temperature_read(struct file *file, char *data,
469                                 size_t len, loff_t *ppos)
470 {
471         int temperature;
472
473         /* Can't seek (pwrite) on this device  */
474         if (ppos != &file->f_pos)
475                 return -ESPIPE;
476
477         if (usb_pcwd_get_temperature(usb_pcwd_device, &temperature))
478                 return -EFAULT;
479
480         if (copy_to_user (data, &temperature, 1))
481                 return -EFAULT;
482
483         return 1;
484 }
485
486 static int usb_pcwd_temperature_open(struct inode *inode, struct file *file)
487 {
488         return 0;
489 }
490
491 static int usb_pcwd_temperature_release(struct inode *inode, struct file *file)
492 {
493         return 0;
494 }
495
496 /*
497  *      Notify system
498  */
499
500 static int usb_pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
501 {
502         if (code==SYS_DOWN || code==SYS_HALT) {
503                 /* Turn the WDT off */
504                 usb_pcwd_stop(usb_pcwd_device);
505         }
506
507         return NOTIFY_DONE;
508 }
509
510 /*
511  *      Kernel Interfaces
512  */
513
514 static struct file_operations usb_pcwd_fops = {
515         .owner =        THIS_MODULE,
516         .llseek =       no_llseek,
517         .write =        usb_pcwd_write,
518         .ioctl =        usb_pcwd_ioctl,
519         .open =         usb_pcwd_open,
520         .release =      usb_pcwd_release,
521 };
522
523 static struct miscdevice usb_pcwd_miscdev = {
524         .minor =        WATCHDOG_MINOR,
525         .name =         "watchdog",
526         .fops =         &usb_pcwd_fops,
527 };
528
529 static struct file_operations usb_pcwd_temperature_fops = {
530         .owner =        THIS_MODULE,
531         .llseek =       no_llseek,
532         .read =         usb_pcwd_temperature_read,
533         .open =         usb_pcwd_temperature_open,
534         .release =      usb_pcwd_temperature_release,
535 };
536
537 static struct miscdevice usb_pcwd_temperature_miscdev = {
538         .minor =        TEMP_MINOR,
539         .name =         "temperature",
540         .fops =         &usb_pcwd_temperature_fops,
541 };
542
543 static struct notifier_block usb_pcwd_notifier = {
544         .notifier_call =        usb_pcwd_notify_sys,
545 };
546
547 /**
548  *      usb_pcwd_delete
549  */
550 static inline void usb_pcwd_delete (struct usb_pcwd_private *usb_pcwd)
551 {
552         if (usb_pcwd->intr_urb != NULL)
553                 usb_free_urb (usb_pcwd->intr_urb);
554         if (usb_pcwd->intr_buffer != NULL)
555                 usb_buffer_free(usb_pcwd->udev, usb_pcwd->intr_size,
556                                 usb_pcwd->intr_buffer, usb_pcwd->intr_dma);
557         kfree (usb_pcwd);
558 }
559
560 /**
561  *      usb_pcwd_probe
562  *
563  *      Called by the usb core when a new device is connected that it thinks
564  *      this driver might be interested in.
565  */
566 static int usb_pcwd_probe(struct usb_interface *interface, const struct usb_device_id *id)
567 {
568         struct usb_device *udev = interface_to_usbdev(interface);
569         struct usb_host_interface *iface_desc;
570         struct usb_endpoint_descriptor *endpoint;
571         struct usb_pcwd_private *usb_pcwd = NULL;
572         int pipe, maxp;
573         int retval = -ENOMEM;
574         int got_fw_rev;
575         unsigned char fw_rev_major, fw_rev_minor;
576         char fw_ver_str[20];
577         unsigned char option_switches, dummy;
578
579         /* See if the device offered us matches what we can accept */
580         if ((udev->descriptor.idVendor != USB_PCWD_VENDOR_ID) ||
581             (udev->descriptor.idProduct != USB_PCWD_PRODUCT_ID)) {
582                 return -ENODEV;
583         }
584
585         cards_found++;
586         if (cards_found > 1) {
587                 printk(KERN_ERR PFX "This driver only supports 1 device\n");
588                 return -ENODEV;
589         }
590
591         /* get the active interface descriptor */
592         iface_desc = interface->cur_altsetting;
593
594         /* check out that we have a HID device */
595         if (!(iface_desc->desc.bInterfaceClass == USB_CLASS_HID)) {
596                 printk(KERN_ERR PFX "The device isn't a Human Interface Device\n");
597                 return -ENODEV;
598         }
599
600         /* check out the endpoint: it has to be Interrupt & IN */
601         endpoint = &iface_desc->endpoint[0].desc;
602
603         if (!((endpoint->bEndpointAddress & USB_DIR_IN) &&
604              ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
605                                 == USB_ENDPOINT_XFER_INT))) {
606                 /* we didn't find a Interrupt endpoint with direction IN */
607                 printk(KERN_ERR PFX "Couldn't find an INTR & IN endpoint\n");
608                 return -ENODEV;
609         }
610
611         /* get a handle to the interrupt data pipe */
612         pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
613         maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
614
615         /* allocate memory for our device and initialize it */
616         usb_pcwd = kmalloc (sizeof(struct usb_pcwd_private), GFP_KERNEL);
617         if (usb_pcwd == NULL) {
618                 printk(KERN_ERR PFX "Out of memory\n");
619                 goto error;
620         }
621         memset (usb_pcwd, 0x00, sizeof (*usb_pcwd));
622
623         usb_pcwd_device = usb_pcwd;
624
625         init_MUTEX (&usb_pcwd->sem);
626         usb_pcwd->udev = udev;
627         usb_pcwd->interface = interface;
628         usb_pcwd->interface_number = iface_desc->desc.bInterfaceNumber;
629         usb_pcwd->intr_size = (endpoint->wMaxPacketSize > 8 ? endpoint->wMaxPacketSize : 8);
630
631         /* set up the memory buffer's */
632         if (!(usb_pcwd->intr_buffer = usb_buffer_alloc(udev, usb_pcwd->intr_size, SLAB_ATOMIC, &usb_pcwd->intr_dma))) {
633                 printk(KERN_ERR PFX "Out of memory\n");
634                 goto error;
635         }
636
637         /* allocate the urb's */
638         usb_pcwd->intr_urb = usb_alloc_urb(0, GFP_KERNEL);
639         if (!usb_pcwd->intr_urb) {
640                 printk(KERN_ERR PFX "Out of memory\n");
641                 goto error;
642         }
643
644         /* initialise the intr urb's */
645         usb_fill_int_urb(usb_pcwd->intr_urb, udev, pipe,
646                         usb_pcwd->intr_buffer, usb_pcwd->intr_size,
647                         usb_pcwd_intr_done, usb_pcwd, endpoint->bInterval);
648         usb_pcwd->intr_urb->transfer_dma = usb_pcwd->intr_dma;
649         usb_pcwd->intr_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
650
651         /* register our interrupt URB with the USB system */
652         if (usb_submit_urb(usb_pcwd->intr_urb, GFP_KERNEL)) {
653                 printk(KERN_ERR PFX "Problem registering interrupt URB\n");
654                 retval = -EIO; /* failure */
655                 goto error;
656         }
657
658         /* The device exists and can be communicated with */
659         usb_pcwd->exists = 1;
660
661         /* disable card */
662         usb_pcwd_stop(usb_pcwd);
663
664         /* Get the Firmware Version */
665         got_fw_rev = usb_pcwd_send_command(usb_pcwd, CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
666         if (got_fw_rev) {
667                 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
668         } else {
669                 sprintf(fw_ver_str, "<card no answer>");
670         }
671
672         printk(KERN_INFO PFX "Found card (Firmware: %s) with temp option\n",
673                 fw_ver_str);
674
675         /* Get switch settings */
676         usb_pcwd_send_command(usb_pcwd, CMD_GET_DIP_SWITCH_SETTINGS, &dummy, &option_switches);
677
678         printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
679                 option_switches,
680                 ((option_switches & 0x10) ? "ON" : "OFF"),
681                 ((option_switches & 0x08) ? "ON" : "OFF"));
682
683         /* Check that the heartbeat value is within it's range ; if not reset to the default */
684         if (heartbeat < 1 || heartbeat > 0xFFFF) {
685                 heartbeat = WATCHDOG_HEARTBEAT;
686                 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
687                         heartbeat);
688         }
689
690         /* Calculate the watchdog's heartbeat */
691         usb_pcwd_set_heartbeat(usb_pcwd, heartbeat);
692
693         retval = register_reboot_notifier(&usb_pcwd_notifier);
694         if (retval != 0) {
695                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
696                         retval);
697                 goto error;
698         }
699
700         retval = misc_register(&usb_pcwd_miscdev);
701         if (retval != 0) {
702                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
703                         WATCHDOG_MINOR, retval);
704                 goto err_out_unregister_reboot;
705         }
706
707         retval = misc_register(&usb_pcwd_temperature_miscdev);
708         if (retval != 0) {
709                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
710                         TEMP_MINOR, retval);
711                 goto err_out_misc_deregister;
712         }
713
714         /* we can register the device now, as it is ready */
715         usb_set_intfdata (interface, usb_pcwd);
716
717         printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
718                 heartbeat, nowayout);
719
720         return 0;
721
722 err_out_misc_deregister:
723         misc_deregister(&usb_pcwd_miscdev);
724 err_out_unregister_reboot:
725         unregister_reboot_notifier(&usb_pcwd_notifier);
726 error:
727         usb_pcwd_delete (usb_pcwd);
728         usb_pcwd_device = NULL;
729         return retval;
730 }
731
732
733 /**
734  *      usb_pcwd_disconnect
735  *
736  *      Called by the usb core when the device is removed from the system.
737  *
738  *      This routine guarantees that the driver will not submit any more urbs
739  *      by clearing dev->udev.
740  */
741 static void usb_pcwd_disconnect(struct usb_interface *interface)
742 {
743         struct usb_pcwd_private *usb_pcwd;
744
745         /* prevent races with open() */
746         down (&disconnect_sem);
747
748         usb_pcwd = usb_get_intfdata (interface);
749         usb_set_intfdata (interface, NULL);
750
751         down (&usb_pcwd->sem);
752
753         /* Stop the timer before we leave */
754         if (!nowayout)
755                 usb_pcwd_stop(usb_pcwd);
756
757         /* We should now stop communicating with the USB PCWD device */
758         usb_pcwd->exists = 0;
759
760         /* Deregister */
761         misc_deregister(&usb_pcwd_temperature_miscdev);
762         misc_deregister(&usb_pcwd_miscdev);
763         unregister_reboot_notifier(&usb_pcwd_notifier);
764
765         up (&usb_pcwd->sem);
766
767         /* Delete the USB PCWD device */
768         usb_pcwd_delete(usb_pcwd);
769
770         cards_found--;
771
772         up (&disconnect_sem);
773
774         printk(KERN_INFO PFX "USB PC Watchdog disconnected\n");
775 }
776
777
778
779 /**
780  *      usb_pcwd_init
781  */
782 static int __init usb_pcwd_init(void)
783 {
784         int result;
785
786         /* register this driver with the USB subsystem */
787         result = usb_register(&usb_pcwd_driver);
788         if (result) {
789                 printk(KERN_ERR PFX "usb_register failed. Error number %d\n",
790                     result);
791                 return result;
792         }
793
794         printk(KERN_INFO PFX DRIVER_DESC " " DRIVER_VERSION "\n");
795         return 0;
796 }
797
798
799 /**
800  *      usb_pcwd_exit
801  */
802 static void __exit usb_pcwd_exit(void)
803 {
804         /* deregister this driver with the USB subsystem */
805         usb_deregister(&usb_pcwd_driver);
806 }
807
808
809 module_init (usb_pcwd_init);
810 module_exit (usb_pcwd_exit);