pcwd_usb.c 22.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *	Berkshire USB-PC Watchdog Card Driver
 *
4
 *	(c) Copyright 2004-2007 Wim Van Sebroeck <wim@iguana.be>.
L
Linus Torvalds 已提交
5 6 7
 *
 *	Based on source code of the following authors:
 *	  Ken Hollis <kenji@bitgate.com>,
8
 *	  Alan Cox <alan@lxorguk.ukuu.org.uk>,
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *	  Matt Domsch <Matt_Domsch@dell.com>,
 *	  Rob Radez <rob@osinvestor.com>,
 *	  Greg Kroah-Hartman <greg@kroah.com>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *	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/
 */

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#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 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
#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 */
42
#include <linux/hid.h>		/* For HID_REQ_SET_REPORT & HID_DT_REPORT */
W
Wim Van Sebroeck 已提交
43
#include <linux/uaccess.h>	/* For copy_to_user/put_user/... */
44

L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53

#ifdef CONFIG_USB_DEBUG
	static int debug = 1;
#else
	static int debug;
#endif

/* Use our own dbg macro */
#undef dbg
54 55
#define dbg(format, arg...) \
	do { if (debug) printk(KERN_DEBUG PFX format "\n" , ## arg); } while (0)
L
Linus Torvalds 已提交
56 57

/* Module and Version Information */
58
#define DRIVER_VERSION "1.02"
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define DRIVER_AUTHOR "Wim Van Sebroeck <wim@iguana.be>"
#define DRIVER_DESC "Berkshire USB-PC Watchdog driver"
#define DRIVER_LICENSE "GPL"
#define DRIVER_NAME "pcwd_usb"
#define PFX DRIVER_NAME ": "

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
MODULE_ALIAS_MISCDEV(TEMP_MINOR);

/* Module Parameters */
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Debug enabled or not");

75 76
#define WATCHDOG_HEARTBEAT 0	/* default heartbeat =
						delay-time from dip-switches */
L
Linus Torvalds 已提交
77 78
static int heartbeat = WATCHDOG_HEARTBEAT;
module_param(heartbeat, int, 0);
79 80 81
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
	"(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
L
Linus Torvalds 已提交
82

83
static int nowayout = WATCHDOG_NOWAYOUT;
L
Linus Torvalds 已提交
84
module_param(nowayout, int, 0);
85 86
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
L
Linus Torvalds 已提交
87 88 89 90 91 92

/* 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 */
93
static struct usb_device_id usb_pcwd_table[] = {
L
Linus Torvalds 已提交
94 95 96
	{ USB_DEVICE(USB_PCWD_VENDOR_ID, USB_PCWD_PRODUCT_ID) },
	{ }					/* Terminating entry */
};
97
MODULE_DEVICE_TABLE(usb, usb_pcwd_table);
L
Linus Torvalds 已提交
98 99 100 101 102 103

/* 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 */
104 105
#define CMD_READ_TEMP			0x02	/* Read Temperature;
							Re-trigger Watchdog */
L
Linus Torvalds 已提交
106 107 108 109 110
#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 */
111
#define CMD_WRITE_WATCHDOG_TIMEOUT	0x19	/* Write Current WatchdogTime */
L
Linus Torvalds 已提交
112 113 114
#define CMD_ENABLE_WATCHDOG		0x30	/* Enable / Disable Watchdog */
#define CMD_DISABLE_WATCHDOG		CMD_ENABLE_WATCHDOG

115
/* Watchdog's Dip Switch heartbeat values */
116
static const int heartbeat_tbl[] = {
117 118 119 120 121 122 123 124 125 126
	5,	/* OFF-OFF-OFF	=  5 Sec  */
	10,	/* OFF-OFF-ON	= 10 Sec  */
	30,	/* OFF-ON-OFF	= 30 Sec  */
	60,	/* OFF-ON-ON	=  1 Min  */
	300,	/* ON-OFF-OFF	=  5 Min  */
	600,	/* ON-OFF-ON	= 10 Min  */
	1800,	/* ON-ON-OFF	= 30 Min  */
	3600,	/* ON-ON-ON	=  1 hour */
};

L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135
/* We can only use 1 card due to the /dev/watchdog restriction */
static int cards_found;

/* some internal variables */
static unsigned long is_active;
static char expect_release;

/* Structure to hold all of our device specific stuff */
struct usb_pcwd_private {
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	/* 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 */
	unsigned int		interface_number;

	/* the buffer to intr data */
	unsigned char		*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 */
	unsigned char		cmd_command;
	/* The data MSB that is reported back */
	unsigned char		cmd_data_msb;
	/* The data LSB that is reported back */
	unsigned char		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;
L
Linus Torvalds 已提交
166 167 168 169
};
static struct usb_pcwd_private *usb_pcwd_device;

/* prevent races between open() and disconnect() */
I
Ingo Molnar 已提交
170
static DEFINE_MUTEX(disconnect_mutex);
L
Linus Torvalds 已提交
171 172

/* local function prototypes */
173 174
static int usb_pcwd_probe(struct usb_interface *interface,
						const struct usb_device_id *id);
175
static void usb_pcwd_disconnect(struct usb_interface *interface);
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185

/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver usb_pcwd_driver = {
	.name =		DRIVER_NAME,
	.probe =	usb_pcwd_probe,
	.disconnect =	usb_pcwd_disconnect,
	.id_table =	usb_pcwd_table,
};


186
static void usb_pcwd_intr_done(struct urb *urb)
L
Linus Torvalds 已提交
187
{
188 189
	struct usb_pcwd_private *usb_pcwd =
				(struct usb_pcwd_private *)urb->context;
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198 199
	unsigned char *data = usb_pcwd->intr_buffer;
	int retval;

	switch (urb->status) {
	case 0:			/* success */
		break;
	case -ECONNRESET:	/* unlink */
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
200 201
		dbg("%s - urb shutting down with status: %d", __func__,
								urb->status);
L
Linus Torvalds 已提交
202 203 204
		return;
	/* -EPIPE:  should clear the halt */
	default:		/* error */
205 206
		dbg("%s - nonzero urb status received: %d", __func__,
								urb->status);
L
Linus Torvalds 已提交
207 208 209 210 211 212 213 214 215 216 217
		goto resubmit;
	}

	dbg("received following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
		data[0], data[1], data[2]);

	usb_pcwd->cmd_command  = data[0];
	usb_pcwd->cmd_data_msb = data[1];
	usb_pcwd->cmd_data_lsb = data[2];

	/* notify anyone waiting that the cmd has finished */
218
	atomic_set(&usb_pcwd->cmd_received, 1);
L
Linus Torvalds 已提交
219 220

resubmit:
221
	retval = usb_submit_urb(urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
222
	if (retval)
223 224
		printk(KERN_ERR PFX "can't resubmit intr, "
			"usb_submit_urb failed with result %d\n", retval);
L
Linus Torvalds 已提交
225 226
}

227 228
static int usb_pcwd_send_command(struct usb_pcwd_private *usb_pcwd,
		unsigned char cmd, unsigned char *msb, unsigned char *lsb)
L
Linus Torvalds 已提交
229 230 231 232
{
	int got_response, count;
	unsigned char buf[6];

233 234
	/* We will not send any commands if the USB PCWD device does
	 * not exist */
L
Linus Torvalds 已提交
235 236 237
	if ((!usb_pcwd) || (!usb_pcwd->exists))
		return -1;

238 239
	/* The USB PC Watchdog uses a 6 byte report format.
	 * The board currently uses only 3 of the six bytes of the report. */
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247
	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 */

	dbg("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x",
		buf[0], buf[1], buf[2]);

248
	atomic_set(&usb_pcwd->cmd_received, 0);
L
Linus Torvalds 已提交
249 250 251 252 253

	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, sizeof(buf),
			USB_COMMAND_TIMEOUT) != sizeof(buf)) {
254 255
		dbg("usb_pcwd_send_command: error in usb_control_msg for "
				"cmd 0x%x 0x%x 0x%x\n", cmd, *msb, *lsb);
L
Linus Torvalds 已提交
256 257 258 259
	}
	/* wait till the usb card processed the command,
	 * with a max. timeout of USB_COMMAND_TIMEOUT */
	got_response = 0;
260 261
	for (count = 0; (count < USB_COMMAND_TIMEOUT) && (!got_response);
								count++) {
L
Linus Torvalds 已提交
262
		mdelay(1);
263
		if (atomic_read(&usb_pcwd->cmd_received))
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
			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;
	}

	return got_response;
}

static int usb_pcwd_start(struct usb_pcwd_private *usb_pcwd)
{
	unsigned char msb = 0x00;
	unsigned char lsb = 0x00;
	int retval;

	/* Enable Watchdog */
283 284
	retval = usb_pcwd_send_command(usb_pcwd, CMD_ENABLE_WATCHDOG,
								&msb, &lsb);
L
Linus Torvalds 已提交
285 286

	if ((retval == 0) || (lsb == 0)) {
287 288
		printk(KERN_ERR PFX
				"Card did not acknowledge enable attempt\n");
L
Linus Torvalds 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301
		return -1;
	}

	return 0;
}

static int usb_pcwd_stop(struct usb_pcwd_private *usb_pcwd)
{
	unsigned char msb = 0xA5;
	unsigned char lsb = 0xC3;
	int retval;

	/* Disable Watchdog */
302 303
	retval = usb_pcwd_send_command(usb_pcwd, CMD_DISABLE_WATCHDOG,
								&msb, &lsb);
L
Linus Torvalds 已提交
304 305

	if ((retval == 0) || (lsb != 0)) {
306 307
		printk(KERN_ERR PFX
			"Card did not acknowledge disable attempt\n");
L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		return -1;
	}

	return 0;
}

static int usb_pcwd_keepalive(struct usb_pcwd_private *usb_pcwd)
{
	unsigned char dummy;

	/* Re-trigger Watchdog */
	usb_pcwd_send_command(usb_pcwd, CMD_TRIGGER, &dummy, &dummy);

	return 0;
}

static int usb_pcwd_set_heartbeat(struct usb_pcwd_private *usb_pcwd, int t)
{
	unsigned char msb = t / 256;
	unsigned char lsb = t % 256;

	if ((t < 0x0001) || (t > 0xFFFF))
		return -EINVAL;

	/* Write new heartbeat to watchdog */
	usb_pcwd_send_command(usb_pcwd, CMD_WRITE_WATCHDOG_TIMEOUT, &msb, &lsb);

	heartbeat = t;
	return 0;
}

339 340
static int usb_pcwd_get_temperature(struct usb_pcwd_private *usb_pcwd,
							int *temperature)
L
Linus Torvalds 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
	unsigned char msb, lsb;

	usb_pcwd_send_command(usb_pcwd, CMD_READ_TEMP, &msb, &lsb);

	/*
	 * Convert celsius to fahrenheit, since this was
	 * the decided 'standard' for this return value.
	 */
	*temperature = (lsb * 9 / 5) + 32;

	return 0;
}

355 356
static int usb_pcwd_get_timeleft(struct usb_pcwd_private *usb_pcwd,
								int *time_left)
357 358 359 360 361 362 363 364 365 366 367 368
{
	unsigned char msb, 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;
}

L
Linus Torvalds 已提交
369 370 371 372 373
/*
 *	/dev/watchdog handling
 */

static ssize_t usb_pcwd_write(struct file *file, const char __user *data,
374
						size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384
{
	/* 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;

385 386
			/* scan to see whether or not we got the
			 * magic character */
L
Linus Torvalds 已提交
387 388
			for (i = 0; i != len; i++) {
				char c;
389
				if (get_user(c, data + i))
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397 398 399 400 401
					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;
}

402 403
static long usb_pcwd_ioctl(struct file *file, unsigned int cmd,
						unsigned long arg)
L
Linus Torvalds 已提交
404 405 406
{
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
407
	static const struct watchdog_info ident = {
L
Linus Torvalds 已提交
408 409 410 411 412 413 414 415
		.options =		WDIOF_KEEPALIVEPING |
					WDIOF_SETTIMEOUT |
					WDIOF_MAGICCLOSE,
		.firmware_version =	1,
		.identity =		DRIVER_NAME,
	};

	switch (cmd) {
416
	case WDIOC_GETSUPPORT:
417
		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
L
Linus Torvalds 已提交
418

419 420 421
	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		return put_user(0, p);
L
Linus Torvalds 已提交
422

423 424 425
	case WDIOC_GETTEMP:
	{
		int temperature;
L
Linus Torvalds 已提交
426

427 428
		if (usb_pcwd_get_temperature(usb_pcwd_device, &temperature))
			return -EFAULT;
L
Linus Torvalds 已提交
429

430 431
		return put_user(temperature, p);
	}
L
Linus Torvalds 已提交
432

433 434 435
	case WDIOC_SETOPTIONS:
	{
		int new_options, retval = -EINVAL;
L
Linus Torvalds 已提交
436

437
		if (get_user(new_options, p))
438
			return -EFAULT;
L
Linus Torvalds 已提交
439

440 441 442 443
		if (new_options & WDIOS_DISABLECARD) {
			usb_pcwd_stop(usb_pcwd_device);
			retval = 0;
		}
L
Linus Torvalds 已提交
444

445 446 447
		if (new_options & WDIOS_ENABLECARD) {
			usb_pcwd_start(usb_pcwd_device);
			retval = 0;
L
Linus Torvalds 已提交
448 449
		}

450 451
		return retval;
	}
L
Linus Torvalds 已提交
452

453 454 455 456
	case WDIOC_KEEPALIVE:
		usb_pcwd_keepalive(usb_pcwd_device);
		return 0;

457 458 459
	case WDIOC_SETTIMEOUT:
	{
		int new_heartbeat;
L
Linus Torvalds 已提交
460

461 462
		if (get_user(new_heartbeat, p))
			return -EFAULT;
L
Linus Torvalds 已提交
463

464
		if (usb_pcwd_set_heartbeat(usb_pcwd_device, new_heartbeat))
465
			return -EINVAL;
L
Linus Torvalds 已提交
466

467 468 469
		usb_pcwd_keepalive(usb_pcwd_device);
		/* Fall */
	}
L
Linus Torvalds 已提交
470

471 472
	case WDIOC_GETTIMEOUT:
		return put_user(heartbeat, p);
473

474 475 476
	case WDIOC_GETTIMELEFT:
	{
		int time_left;
477

478 479 480 481 482
		if (usb_pcwd_get_timeleft(usb_pcwd_device, &time_left))
			return -EFAULT;

		return put_user(time_left, p);
	}
483

484 485
	default:
		return -ENOTTY;
L
Linus Torvalds 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
	}
}

static int 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;

	/* Activate */
	usb_pcwd_start(usb_pcwd_device);
	usb_pcwd_keepalive(usb_pcwd_device);
	return nonseekable_open(inode, file);
}

static int usb_pcwd_release(struct inode *inode, struct file *file)
{
	/*
	 *      Shut off the timer.
	 */
	if (expect_release == 42) {
		usb_pcwd_stop(usb_pcwd_device);
	} else {
509 510
		printk(KERN_CRIT PFX
			"Unexpected close, not stopping watchdog!\n");
L
Linus Torvalds 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		usb_pcwd_keepalive(usb_pcwd_device);
	}
	expect_release = 0;
	clear_bit(0, &is_active);
	return 0;
}

/*
 *	/dev/temperature handling
 */

static ssize_t usb_pcwd_temperature_read(struct file *file, char __user *data,
				size_t len, loff_t *ppos)
{
	int temperature;

	if (usb_pcwd_get_temperature(usb_pcwd_device, &temperature))
		return -EFAULT;

	if (copy_to_user(data, &temperature, 1))
		return -EFAULT;

	return 1;
}

static int usb_pcwd_temperature_open(struct inode *inode, struct file *file)
{
	return nonseekable_open(inode, file);
}

static int usb_pcwd_temperature_release(struct inode *inode, struct file *file)
{
	return 0;
}

/*
 *	Notify system
 */

550 551
static int usb_pcwd_notify_sys(struct notifier_block *this, unsigned long code,
								void *unused)
L
Linus Torvalds 已提交
552
{
553 554
	if (code == SYS_DOWN || code == SYS_HALT)
		usb_pcwd_stop(usb_pcwd_device);	/* Turn the WDT off */
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562

	return NOTIFY_DONE;
}

/*
 *	Kernel Interfaces
 */

563
static const struct file_operations usb_pcwd_fops = {
L
Linus Torvalds 已提交
564 565 566
	.owner =	THIS_MODULE,
	.llseek =	no_llseek,
	.write =	usb_pcwd_write,
567
	.unlocked_ioctl = usb_pcwd_ioctl,
L
Linus Torvalds 已提交
568 569 570 571 572 573 574 575 576 577
	.open =		usb_pcwd_open,
	.release =	usb_pcwd_release,
};

static struct miscdevice usb_pcwd_miscdev = {
	.minor =	WATCHDOG_MINOR,
	.name =		"watchdog",
	.fops =		&usb_pcwd_fops,
};

578
static const struct file_operations usb_pcwd_temperature_fops = {
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	.owner =	THIS_MODULE,
	.llseek =	no_llseek,
	.read =		usb_pcwd_temperature_read,
	.open =		usb_pcwd_temperature_open,
	.release =	usb_pcwd_temperature_release,
};

static struct miscdevice usb_pcwd_temperature_miscdev = {
	.minor =	TEMP_MINOR,
	.name =		"temperature",
	.fops =		&usb_pcwd_temperature_fops,
};

static struct notifier_block usb_pcwd_notifier = {
	.notifier_call =	usb_pcwd_notify_sys,
};

/**
 *	usb_pcwd_delete
 */
599
static inline void usb_pcwd_delete(struct usb_pcwd_private *usb_pcwd)
L
Linus Torvalds 已提交
600
{
M
Mariusz Kozlowski 已提交
601
	usb_free_urb(usb_pcwd->intr_urb);
L
Linus Torvalds 已提交
602
	if (usb_pcwd->intr_buffer != NULL)
603 604
		usb_free_coherent(usb_pcwd->udev, usb_pcwd->intr_size,
				  usb_pcwd->intr_buffer, usb_pcwd->intr_dma);
605
	kfree(usb_pcwd);
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613
}

/**
 *	usb_pcwd_probe
 *
 *	Called by the usb core when a new device is connected that it thinks
 *	this driver might be interested in.
 */
614 615
static int usb_pcwd_probe(struct usb_interface *interface,
						const struct usb_device_id *id)
L
Linus Torvalds 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
{
	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, maxp;
	int retval = -ENOMEM;
	int got_fw_rev;
	unsigned char fw_rev_major, fw_rev_minor;
	char fw_ver_str[20];
	unsigned char option_switches, dummy;

	cards_found++;
	if (cards_found > 1) {
		printk(KERN_ERR PFX "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)) {
639 640
		printk(KERN_ERR PFX
			"The device isn't a Human Interface Device\n");
L
Linus Torvalds 已提交
641 642 643 644 645 646
		return -ENODEV;
	}

	/* check out the endpoint: it has to be Interrupt & IN */
	endpoint = &iface_desc->endpoint[0].desc;

647
	if (!usb_endpoint_is_int_in(endpoint)) {
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657
		/* we didn't find a Interrupt endpoint with direction IN */
		printk(KERN_ERR PFX "Couldn't find an INTR & IN endpoint\n");
		return -ENODEV;
	}

	/* get a handle to the interrupt data pipe */
	pipe = usb_rcvintpipe(udev, endpoint->bEndpointAddress);
	maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));

	/* allocate memory for our device and initialize it */
658
	usb_pcwd = kzalloc(sizeof(struct usb_pcwd_private), GFP_KERNEL);
L
Linus Torvalds 已提交
659 660 661 662 663 664 665
	if (usb_pcwd == NULL) {
		printk(KERN_ERR PFX "Out of memory\n");
		goto error;
	}

	usb_pcwd_device = usb_pcwd;

666
	mutex_init(&usb_pcwd->mtx);
L
Linus Torvalds 已提交
667 668 669
	usb_pcwd->udev = udev;
	usb_pcwd->interface = interface;
	usb_pcwd->interface_number = iface_desc->desc.bInterfaceNumber;
670 671
	usb_pcwd->intr_size = (le16_to_cpu(endpoint->wMaxPacketSize) > 8 ?
				le16_to_cpu(endpoint->wMaxPacketSize) : 8);
L
Linus Torvalds 已提交
672 673

	/* set up the memory buffer's */
674
	usb_pcwd->intr_buffer = usb_alloc_coherent(udev, usb_pcwd->intr_size,
675
					GFP_ATOMIC, &usb_pcwd->intr_dma);
676
	if (!usb_pcwd->intr_buffer) {
L
Linus Torvalds 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
		printk(KERN_ERR PFX "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) {
		printk(KERN_ERR PFX "Out of memory\n");
		goto error;
	}

	/* initialise the intr urb's */
	usb_fill_int_urb(usb_pcwd->intr_urb, udev, pipe,
			usb_pcwd->intr_buffer, usb_pcwd->intr_size,
			usb_pcwd_intr_done, usb_pcwd, endpoint->bInterval);
	usb_pcwd->intr_urb->transfer_dma = usb_pcwd->intr_dma;
	usb_pcwd->intr_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

	/* register our interrupt URB with the USB system */
	if (usb_submit_urb(usb_pcwd->intr_urb, GFP_KERNEL)) {
		printk(KERN_ERR PFX "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 */
709 710
	got_fw_rev = usb_pcwd_send_command(usb_pcwd, CMD_GET_FIRMWARE_VERSION,
						&fw_rev_major, &fw_rev_minor);
711
	if (got_fw_rev)
L
Linus Torvalds 已提交
712
		sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
713
	else
L
Linus Torvalds 已提交
714 715 716 717 718 719
		sprintf(fw_ver_str, "<card no answer>");

	printk(KERN_INFO PFX "Found card (Firmware: %s) with temp option\n",
		fw_ver_str);

	/* Get switch settings */
720 721
	usb_pcwd_send_command(usb_pcwd, CMD_GET_DIP_SWITCH_SETTINGS, &dummy,
							&option_switches);
L
Linus Torvalds 已提交
722

723 724
	printk(KERN_INFO PFX "Option switches (0x%02x): "
		"Temperature Reset Enable=%s, Power On Delay=%s\n",
L
Linus Torvalds 已提交
725 726 727 728
		option_switches,
		((option_switches & 0x10) ? "ON" : "OFF"),
		((option_switches & 0x08) ? "ON" : "OFF"));

729 730 731 732
	/* If heartbeat = 0 then we use the heartbeat from the dip-switches */
	if (heartbeat == 0)
		heartbeat = heartbeat_tbl[(option_switches & 0x07)];

733 734
	/* Check that the heartbeat value is within it's range ;
	 * if not reset to the default */
L
Linus Torvalds 已提交
735 736
	if (usb_pcwd_set_heartbeat(usb_pcwd, heartbeat)) {
		usb_pcwd_set_heartbeat(usb_pcwd, WATCHDOG_HEARTBEAT);
737 738
		printk(KERN_INFO PFX
			"heartbeat value must be 0<heartbeat<65536, using %d\n",
L
Linus Torvalds 已提交
739 740 741 742 743
			WATCHDOG_HEARTBEAT);
	}

	retval = register_reboot_notifier(&usb_pcwd_notifier);
	if (retval != 0) {
744 745
		printk(KERN_ERR PFX
			"cannot register reboot notifier (err=%d)\n",
L
Linus Torvalds 已提交
746 747 748 749 750 751
			retval);
		goto error;
	}

	retval = misc_register(&usb_pcwd_temperature_miscdev);
	if (retval != 0) {
752 753
		printk(KERN_ERR PFX
			"cannot register miscdev on minor=%d (err=%d)\n",
L
Linus Torvalds 已提交
754 755 756 757 758 759
			TEMP_MINOR, retval);
		goto err_out_unregister_reboot;
	}

	retval = misc_register(&usb_pcwd_miscdev);
	if (retval != 0) {
760 761
		printk(KERN_ERR PFX
			"cannot register miscdev on minor=%d (err=%d)\n",
L
Linus Torvalds 已提交
762 763 764 765 766
			WATCHDOG_MINOR, retval);
		goto err_out_misc_deregister;
	}

	/* we can register the device now, as it is ready */
767
	usb_set_intfdata(interface, usb_pcwd);
L
Linus Torvalds 已提交
768 769 770 771 772 773 774 775 776 777 778

	printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
		heartbeat, nowayout);

	return 0;

err_out_misc_deregister:
	misc_deregister(&usb_pcwd_temperature_miscdev);
err_out_unregister_reboot:
	unregister_reboot_notifier(&usb_pcwd_notifier);
error:
779 780
	if (usb_pcwd)
		usb_pcwd_delete(usb_pcwd);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	usb_pcwd_device = NULL;
	return retval;
}


/**
 *	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.
 */
static void usb_pcwd_disconnect(struct usb_interface *interface)
{
	struct usb_pcwd_private *usb_pcwd;

	/* prevent races with open() */
I
Ingo Molnar 已提交
799
	mutex_lock(&disconnect_mutex);
L
Linus Torvalds 已提交
800

801 802
	usb_pcwd = usb_get_intfdata(interface);
	usb_set_intfdata(interface, NULL);
L
Linus Torvalds 已提交
803

804
	mutex_lock(&usb_pcwd->mtx);
L
Linus Torvalds 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817

	/* Stop the timer before we leave */
	if (!nowayout)
		usb_pcwd_stop(usb_pcwd);

	/* We should now stop communicating with the USB PCWD device */
	usb_pcwd->exists = 0;

	/* Deregister */
	misc_deregister(&usb_pcwd_miscdev);
	misc_deregister(&usb_pcwd_temperature_miscdev);
	unregister_reboot_notifier(&usb_pcwd_notifier);

818
	mutex_unlock(&usb_pcwd->mtx);
L
Linus Torvalds 已提交
819 820 821 822 823 824

	/* Delete the USB PCWD device */
	usb_pcwd_delete(usb_pcwd);

	cards_found--;

I
Ingo Molnar 已提交
825
	mutex_unlock(&disconnect_mutex);
L
Linus Torvalds 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

	printk(KERN_INFO PFX "USB PC Watchdog disconnected\n");
}



/**
 *	usb_pcwd_init
 */
static int __init usb_pcwd_init(void)
{
	int result;

	/* register this driver with the USB subsystem */
	result = usb_register(&usb_pcwd_driver);
	if (result) {
		printk(KERN_ERR PFX "usb_register failed. Error number %d\n",
		    result);
		return result;
	}

847
	printk(KERN_INFO PFX DRIVER_DESC " v" DRIVER_VERSION "\n");
L
Linus Torvalds 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861
	return 0;
}


/**
 *	usb_pcwd_exit
 */
static void __exit usb_pcwd_exit(void)
{
	/* deregister this driver with the USB subsystem */
	usb_deregister(&usb_pcwd_driver);
}


862 863
module_init(usb_pcwd_init);
module_exit(usb_pcwd_exit);