PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/watchdog/pcwd_usb.c

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