pcwd_usb.c 21 KB

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