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