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