Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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