pcwd_usb.c 21 KB

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