// SPDX-License-Identifier: GPL-2.0+ /* * Berkshire USB-PC Watchdog Card Driver * * (c) Copyright 2004-2007 Wim Van Sebroeck <wim@iguana.be>. * * Based on source code of the following authors: * Ken Hollis <kenji@bitgate.com>, * Alan Cox <alan@lxorguk.ukuu.org.uk>, * Matt Domsch <Matt_Domsch@dell.com>, * Rob Radez <rob@osinvestor.com>, * Greg Kroah-Hartman <greg@kroah.com> * * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor * provide warranty for any of this software. This material is * provided "AS-IS" and at no charge. * * Thanks also to Simon Machell at Berkshire Products Inc. for * providing the test hardware. More info is available at * http://www.berkprod.com/ or http://www.pcwatchdog.com/
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h> /* For module specific items */ #include <linux/moduleparam.h> /* For new moduleparam's */ #include <linux/types.h> /* For standard types (like size_t) */ #include <linux/errno.h> /* For the -ENODEV/... values */ #include <linux/kernel.h> /* For printk/panic/... */ #include <linux/delay.h> /* For mdelay function */ #include <linux/miscdevice.h> /* For struct miscdevice */ #include <linux/watchdog.h> /* For the watchdog specific items */ #include <linux/notifier.h> /* For notifier support */ #include <linux/reboot.h> /* For reboot_notifier stuff */ #include <linux/init.h> /* For __init/__exit/... */ #include <linux/fs.h> /* For file operations */ #include <linux/usb.h> /* For USB functions */ #include <linux/slab.h> /* For kmalloc, ... */ #include <linux/mutex.h> /* For mutex locking */ #include <linux/hid.h> /* For HID_REQ_SET_REPORT & HID_DT_REPORT */ #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
/* Module and Version Information */ #define DRIVER_VERSION "1.02" #define DRIVER_AUTHOR "Wim Van Sebroeck " #define DRIVER_DESC "Berkshire USB-PC Watchdog driver" #define DRIVER_NAME "pcwd_usb"
staticbool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
/* The vendor and product id's for the USB-PC Watchdog card */ #define USB_PCWD_VENDOR_ID 0x0c98 #define USB_PCWD_PRODUCT_ID 0x1140
/* table of devices that work with this driver */ staticconststruct usb_device_id usb_pcwd_table[] = {
{ USB_DEVICE(USB_PCWD_VENDOR_ID, USB_PCWD_PRODUCT_ID) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_pcwd_table);
/* according to documentation max. time to process a command for the USB
* watchdog card is 100 or 200 ms, so we give it 250 ms to do it's job */ #define USB_COMMAND_TIMEOUT 250
/* Watchdog's internal commands */ #define CMD_READ_TEMP 0x02 /* Read Temperature;
Re-trigger Watchdog */ #define CMD_TRIGGER CMD_READ_TEMP #define CMD_GET_STATUS 0x04 /* Get Status Information */ #define CMD_GET_FIRMWARE_VERSION 0x08 /* Get Firmware Version */ #define CMD_GET_DIP_SWITCH_SETTINGS 0x0c /* Get Dip Switch Settings */ #define CMD_READ_WATCHDOG_TIMEOUT 0x18 /* Read Current Watchdog Time */ #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19 /* Write Current WatchdogTime */ #define CMD_ENABLE_WATCHDOG 0x30 /* Enable / Disable Watchdog */ #define CMD_DISABLE_WATCHDOG CMD_ENABLE_WATCHDOG
/* We can only use 1 card due to the /dev/watchdog restriction */ staticint cards_found;
/* some internal variables */ staticunsignedlong is_active; staticchar expect_release;
/* Structure to hold all of our device specific stuff */ struct usb_pcwd_private { /* save off the usb device pointer */ struct usb_device *udev; /* the interface for this device */ struct usb_interface *interface;
/* the interface number used for cmd's */ unsignedint interface_number;
/* the buffer to intr data */ unsignedchar *intr_buffer; /* the dma address for the intr buffer */
dma_addr_t intr_dma; /* the size of the intr buffer */
size_t intr_size; /* the urb used for the intr pipe */ struct urb *intr_urb;
/* The command that is reported back */ unsignedchar cmd_command; /* The data MSB that is reported back */ unsignedchar cmd_data_msb; /* The data LSB that is reported back */ unsignedchar cmd_data_lsb; /* true if we received a report after a command */
atomic_t cmd_received;
/* Wether or not the device exists */ int exists; /* locks this structure */ struct mutex mtx;
}; staticstruct usb_pcwd_private *usb_pcwd_device;
/* prevent races between open() and disconnect() */ static DEFINE_MUTEX(disconnect_mutex);
/* local function prototypes */ staticint usb_pcwd_probe(struct usb_interface *interface, conststruct usb_device_id *id); staticvoid usb_pcwd_disconnect(struct usb_interface *interface);
/* usb specific object needed to register this driver with the usb subsystem */ staticstruct usb_driver usb_pcwd_driver = {
.name = DRIVER_NAME,
.probe = usb_pcwd_probe,
.disconnect = usb_pcwd_disconnect,
.id_table = usb_pcwd_table,
};
switch (urb->status) { case 0: /* success */ break; case -ECONNRESET: /* unlink */ case -ENOENT: case -ESHUTDOWN: /* this urb is terminated, clean up */
dev_dbg(dev, "%s - urb shutting down with status: %d",
__func__, urb->status); return; /* -EPIPE: should clear the halt */ default: /* error */
dev_dbg(dev, "%s - nonzero urb status received: %d",
__func__, urb->status); goto resubmit;
}
dev_dbg(dev, "received following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
data[0], data[1], data[2]);
/* We will not send any commands if the USB PCWD device does
* not exist */ if ((!usb_pcwd) || (!usb_pcwd->exists)) return -1;
buf = kmalloc(6, GFP_KERNEL); if (buf == NULL) return 0;
/* The USB PC Watchdog uses a 6 byte report format.
* The board currently uses only 3 of the six bytes of the report. */
buf[0] = cmd; /* Byte 0 = CMD */
buf[1] = *msb; /* Byte 1 = Data MSB */
buf[2] = *lsb; /* Byte 2 = Data LSB */
buf[3] = buf[4] = buf[5] = 0; /* All other bytes not used */
dev_dbg(&usb_pcwd->interface->dev, "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
buf[0], buf[1], buf[2]);
atomic_set(&usb_pcwd->cmd_received, 0);
if (usb_control_msg(usb_pcwd->udev, usb_sndctrlpipe(usb_pcwd->udev, 0),
HID_REQ_SET_REPORT, HID_DT_REPORT,
0x0200, usb_pcwd->interface_number, buf, 6,
USB_COMMAND_TIMEOUT) != 6) {
dev_dbg(&usb_pcwd->interface->dev, "usb_pcwd_send_command: error in usb_control_msg for cmd 0x%x 0x%x 0x%x\n",
cmd, *msb, *lsb);
} /* wait till the usb card processed the command,
* with a max. timeout of USB_COMMAND_TIMEOUT */
got_response = 0; for (count = 0; (count < USB_COMMAND_TIMEOUT) && (!got_response);
count++) {
mdelay(1); if (atomic_read(&usb_pcwd->cmd_received))
got_response = 1;
}
if ((got_response) && (cmd == usb_pcwd->cmd_command)) { /* read back response */
*msb = usb_pcwd->cmd_data_msb;
*lsb = usb_pcwd->cmd_data_lsb;
}
/* Read the time that's left before rebooting */ /* Note: if the board is not yet armed then we will read 0xFFFF */
usb_pcwd_send_command(usb_pcwd, CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
*time_left = (msb << 8) + lsb;
return 0;
}
/* * /dev/watchdog handling
*/
static ssize_t usb_pcwd_write(struct file *file, constchar __user *data,
size_t len, loff_t *ppos)
{ /* See if we got the magic character 'V' and reload the timer */ if (len) { if (!nowayout) {
size_t i;
/* note: just in case someone wrote the magic character
* five months ago... */
expect_release = 0;
/* scan to see whether or not we got the
* magic character */ for (i = 0; i != len; i++) { char c; if (get_user(c, data + i)) return -EFAULT; if (c == 'V')
expect_release = 42;
}
}
/* someone wrote to us, we should reload the timer */
usb_pcwd_keepalive(usb_pcwd_device);
} return len;
}
case WDIOC_GETTIMEOUT: return put_user(heartbeat, p);
case WDIOC_GETTIMELEFT:
{ int time_left;
if (usb_pcwd_get_timeleft(usb_pcwd_device, &time_left)) return -EFAULT;
return put_user(time_left, p);
}
default: return -ENOTTY;
}
}
staticint usb_pcwd_open(struct inode *inode, struct file *file)
{ /* /dev/watchdog can only be opened once */ if (test_and_set_bit(0, &is_active)) return -EBUSY;
/* * usb_pcwd_probe * * Called by the usb core when a new device is connected that it thinks * this driver might be interested in.
*/ staticint usb_pcwd_probe(struct usb_interface *interface, conststruct usb_device_id *id)
{ struct usb_device *udev = interface_to_usbdev(interface); struct usb_host_interface *iface_desc; struct usb_endpoint_descriptor *endpoint; struct usb_pcwd_private *usb_pcwd = NULL; int pipe; int retval = -ENOMEM; int got_fw_rev; unsignedchar fw_rev_major, fw_rev_minor; char fw_ver_str[20]; unsignedchar option_switches, dummy;
cards_found++; if (cards_found > 1) {
pr_err("This driver only supports 1 device\n"); return -ENODEV;
}
/* get the active interface descriptor */
iface_desc = interface->cur_altsetting;
/* check out that we have a HID device */ if (!(iface_desc->desc.bInterfaceClass == USB_CLASS_HID)) {
pr_err("The device isn't a Human Interface Device\n"); return -ENODEV;
}
if (iface_desc->desc.bNumEndpoints < 1) return -ENODEV;
/* check out the endpoint: it has to be Interrupt & IN */
endpoint = &iface_desc->endpoint[0].desc;
if (!usb_endpoint_is_int_in(endpoint)) { /* we didn't find a Interrupt endpoint with direction IN */
pr_err("Couldn't find an INTR & IN endpoint\n"); return -ENODEV;
}
/* get a handle to the interrupt data pipe */
pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
/* allocate memory for our device and initialize it */
usb_pcwd = kzalloc(sizeof(struct usb_pcwd_private), GFP_KERNEL); if (usb_pcwd == NULL) goto error;
/* set up the memory buffer's */
usb_pcwd->intr_buffer = usb_alloc_coherent(udev, usb_pcwd->intr_size,
GFP_KERNEL, &usb_pcwd->intr_dma); if (!usb_pcwd->intr_buffer) {
pr_err("Out of memory\n"); goto error;
}
/* allocate the urb's */
usb_pcwd->intr_urb = usb_alloc_urb(0, GFP_KERNEL); if (!usb_pcwd->intr_urb) goto error;
/* register our interrupt URB with the USB system */ if (usb_submit_urb(usb_pcwd->intr_urb, GFP_KERNEL)) {
pr_err("Problem registering interrupt URB\n");
retval = -EIO; /* failure */ goto error;
}
/* The device exists and can be communicated with */
usb_pcwd->exists = 1;
/* disable card */
usb_pcwd_stop(usb_pcwd);
/* Get the Firmware Version */
got_fw_rev = usb_pcwd_send_command(usb_pcwd, CMD_GET_FIRMWARE_VERSION,
&fw_rev_major, &fw_rev_minor); if (got_fw_rev)
sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor); else
sprintf(fw_ver_str, "");
pr_info("Found card (Firmware: %s) with temp option\n", fw_ver_str);
/* Get switch settings */
usb_pcwd_send_command(usb_pcwd, CMD_GET_DIP_SWITCH_SETTINGS, &dummy,
&option_switches);
pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
option_switches,
((option_switches & 0x10) ? "ON" : "OFF"),
((option_switches & 0x08) ? "ON" : "OFF"));
/* If heartbeat = 0 then we use the heartbeat from the dip-switches */ if (heartbeat == 0)
heartbeat = heartbeat_tbl[(option_switches & 0x07)];
/* Check that the heartbeat value is within it's range ;
* if not reset to the default */ if (usb_pcwd_set_heartbeat(usb_pcwd, heartbeat)) {
usb_pcwd_set_heartbeat(usb_pcwd, WATCHDOG_HEARTBEAT);
pr_info("heartbeat value must be 0,
WATCHDOG_HEARTBEAT);
}
/* * usb_pcwd_disconnect * * Called by the usb core when the device is removed from the system. * * This routine guarantees that the driver will not submit any more urbs * by clearing dev->udev.
*/ staticvoid usb_pcwd_disconnect(struct usb_interface *interface)
{ struct usb_pcwd_private *usb_pcwd;
/* prevent races with open() */
mutex_lock(&disconnect_mutex);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.