pcwd_usb.c 21 KB

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