pcwd_usb.c 22 KB

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