pcwd_usb.c 21 KB

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