pcwd_usb.c 21 KB

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