pcwd.c 27.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * PC Watchdog Driver
 * by Ken Hollis (khollis@bitgate.com)
 *
5
 * Permission granted from Simon Machell (smachell@berkprod.com)
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
 * Written for the Linux Kernel, and GPLed by Ken Hollis
 *
 * 960107	Added request_region routines, modulized the whole thing.
 * 960108	Fixed end-of-file pointer (Thanks to Dan Hollis), added
 *		WD_TIMEOUT define.
 * 960216	Added eof marker on the file, and changed verbose messages.
 * 960716	Made functional and cosmetic changes to the source for
 *		inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
 * 960717	Removed read/seek routines, replaced with ioctl.  Also, added
 *		check_region command due to Alan's suggestion.
 * 960821	Made changes to compile in newer 2.0.x kernels.  Added
 *		"cold reboot sense" entry.
 * 960825	Made a few changes to code, deleted some defines and made
 *		typedefs to replace them.  Made heartbeat reset only available
 *		via ioctl, and removed the write routine.
 * 960828	Added new items for PC Watchdog Rev.C card.
 * 960829	Changed around all of the IOCTLs, added new features,
 *		added watchdog disable/re-enable routines.  Added firmware
 *		version reporting.  Added read routine for temperature.
 *		Removed some extra defines, added an autodetect Revision
 *		routine.
 * 961006       Revised some documentation, fixed some cosmetic bugs.  Made
 *              drivers to panic the system if it's overheating at bootup.
 * 961118	Changed some verbiage on some of the output, tidied up
 *		code bits, and added compatibility to 2.1.x.
 * 970912       Enabled board on open and disable on close.
 * 971107	Took account of recent VFS changes (broke read).
 * 971210       Disable board on initialisation in case board already ticking.
 * 971222       Changed open/close for temperature handling
 *              Michael Meskes <meskes@debian.org>.
 * 980112       Used minor numbers from include/linux/miscdevice.h
 * 990403       Clear reset status after reading control status register in
 *              pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
 * 990605	Made changes to code to support Firmware 1.22a, added
 *		fairly useless proc entry.
 * 990610	removed said useless proc code for the merge <alan>
 * 000403	Removed last traces of proc code. <davej>
43 44
 * 011214	Added nowayout module option to override
 *		CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
L
Linus Torvalds 已提交
45 46 47 48 49
 *              Added timeout module option to override default
 */

/*
 *	A bells and whistles driver is available from http://www.pcwd.de/
50 51
 *	More info available at http://www.berkprod.com/ or
 *	http://www.pcwatchdog.com/
L
Linus Torvalds 已提交
52 53
 */

54 55 56 57 58 59 60 61 62 63
#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/timer.h>	/* For timer related operations */
#include <linux/jiffies.h>	/* For jiffies stuff */
#include <linux/miscdevice.h>	/* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
#include <linux/watchdog.h>	/* For the watchdog specific items */
64
#include <linux/reboot.h>	/* For kernel_power_off() */
65 66
#include <linux/init.h>		/* For __init/__exit/... */
#include <linux/fs.h>		/* For file operations */
67
#include <linux/isa.h>		/* For isa devices */
68 69
#include <linux/ioport.h>	/* For io-port access */
#include <linux/spinlock.h>	/* For spin_lock/spin_unlock/... */
70 71
#include <linux/uaccess.h>	/* For copy_to_user/put_user/... */
#include <linux/io.h>		/* For inb/outb/... */
72 73

/* Module and version information */
74 75
#define WATCHDOG_VERSION "1.20"
#define WATCHDOG_DATE "18 Feb 2007"
76 77 78 79 80
#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
#define WATCHDOG_NAME "pcwd"
#define PFX WATCHDOG_NAME ": "
#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
#define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91

/*
 * It should be noted that PCWD_REVISION_B was removed because A and B
 * are essentially the same types of card, with the exception that B
 * has temperature reporting.  Since I didn't receive a Rev.B card,
 * the Rev.B card is not supported.  (It's a good thing too, as they
 * are no longer in production.)
 */
#define	PCWD_REVISION_A		1
#define	PCWD_REVISION_C		2

92 93 94 95 96 97 98 99 100
/*
 * These are the auto-probe addresses available.
 *
 * Revision A only uses ports 0x270 and 0x370.  Revision C introduced 0x350.
 * Revision A has an address range of 2 addresses, while Revision C has 4.
 */
#define PCWD_ISA_NR_CARDS	3
static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };

L
Linus Torvalds 已提交
101
/*
102 103 104 105
 * These are the defines that describe the control status bits for the
 * PCI-PC Watchdog card.
*/
/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
106 107 108 109 110
#define WD_WDRST		0x01	/* Previously reset state */
#define WD_T110			0x02	/* Temperature overheat sense */
#define WD_HRTBT		0x04	/* Heartbeat sense */
#define WD_RLY2			0x08	/* External relay triggered */
#define WD_SRLY2		0x80	/* Software external relay triggered */
111
/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
112 113 114
#define WD_REVC_WTRP		0x01	/* Watchdog Trip status */
#define WD_REVC_HRBT		0x02	/* Watchdog Heartbeat */
#define WD_REVC_TTRP		0x04	/* Temperature Trip status */
115 116
#define WD_REVC_RL2A		0x08	/* Relay 2 activated by
							on-board processor */
117 118 119
#define WD_REVC_RL1A		0x10	/* Relay 1 active */
#define WD_REVC_R2DS		0x40	/* Relay 2 disable */
#define WD_REVC_RLY2		0x80	/* Relay 2 activated? */
120 121 122
/* Port 2 : Control Status #2 */
#define WD_WDIS			0x10	/* Watchdog Disabled */
#define WD_ENTP			0x20	/* Watchdog Enable Temperature Trip */
123 124
#define WD_SSEL			0x40	/* Watchdog Switch Select
							(1:SW1 <-> 0:SW2) */
125
#define WD_WCMD			0x80	/* Watchdog Command Mode */
L
Linus Torvalds 已提交
126 127 128 129 130 131

/* max. time we give an ISA watchdog card to process a command */
/* 500ms for each 4 bit response (according to spec.) */
#define ISA_COMMAND_TIMEOUT     1000

/* Watchdog's internal commands */
132 133 134 135 136 137
#define CMD_ISA_IDLE			0x00
#define CMD_ISA_VERSION_INTEGER		0x01
#define CMD_ISA_VERSION_TENTH		0x02
#define CMD_ISA_VERSION_HUNDRETH	0x03
#define CMD_ISA_VERSION_MINOR		0x04
#define CMD_ISA_SWITCH_SETTINGS		0x05
138 139 140 141
#define CMD_ISA_RESET_PC		0x06
#define CMD_ISA_ARM_0			0x07
#define CMD_ISA_ARM_30			0x08
#define CMD_ISA_ARM_60			0x09
142 143 144
#define CMD_ISA_DELAY_TIME_2SECS	0x0A
#define CMD_ISA_DELAY_TIME_4SECS	0x0B
#define CMD_ISA_DELAY_TIME_8SECS	0x0C
145
#define CMD_ISA_RESET_RELAYS		0x0D
L
Linus Torvalds 已提交
146

147 148 149 150 151 152 153 154 155 156 157 158
/* Watchdog's Dip Switch heartbeat values */
static const int heartbeat_tbl [] = {
	20,	/* OFF-OFF-OFF	= 20 Sec  */
	40,	/* OFF-OFF-ON	= 40 Sec  */
	60,	/* OFF-ON-OFF	=  1 Min  */
	300,	/* OFF-ON-ON	=  5 Min  */
	600,	/* ON-OFF-OFF	= 10 Min  */
	1800,	/* ON-OFF-ON	= 30 Min  */
	3600,	/* ON-ON-OFF	=  1 Hour */
	7200,	/* ON-ON-ON	=  2 hour */
};

L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/*
 * We are using an kernel timer to do the pinging of the watchdog
 * every ~500ms. We try to set the internal heartbeat of the
 * watchdog to 2 ms.
 */

#define WDT_INTERVAL (HZ/2+1)

/* We can only use 1 card due to the /dev/watchdog restriction */
static int cards_found;

/* internal variables */
static atomic_t open_allowed = ATOMIC_INIT(1);
static char expect_close;
static int temp_panic;
174 175 176

/* this is private data for each ISA-PC watchdog card */
static struct {
177
	char fw_ver_str[6];		/* The cards firmware version */
178
	int revision;			/* The card's revision */
179 180 181 182
	int supports_temp;		/* Whether or not the card has
						a temperature device */
	int command_mode;		/* Whether or not the card is in
						command mode */
183 184 185 186 187 188
	int boot_status;		/* The card's boot status */
	int io_addr;			/* The cards I/O address */
	spinlock_t io_lock;		/* the lock for io operations */
	struct timer_list timer;	/* The timer that pings the watchdog */
	unsigned long next_heartbeat;	/* the next_heartbeat for the timer */
} pcwd_private;
L
Linus Torvalds 已提交
189 190

/* module parameters */
191 192 193 194 195
#define QUIET	0	/* Default */
#define VERBOSE	1	/* Verbose */
#define DEBUG	2	/* print fancy stuff too */
static int debug = QUIET;
module_param(debug, int, 0);
196 197
MODULE_PARM_DESC(debug,
		"Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
198

199 200
/* default heartbeat = delay-time from dip-switches */
#define WATCHDOG_HEARTBEAT 0
L
Linus Torvalds 已提交
201 202
static int heartbeat = WATCHDOG_HEARTBEAT;
module_param(heartbeat, int, 0);
203
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2 <= heartbeat <= 7200 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
L
Linus Torvalds 已提交
204

205
static int nowayout = WATCHDOG_NOWAYOUT;
L
Linus Torvalds 已提交
206
module_param(nowayout, int, 0);
207 208 209
MODULE_PARM_DESC(nowayout,
		"Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218 219 220

/*
 *	Internal functions
 */

static int send_isa_command(int cmd)
{
	int i;
	int control_status;
	int port0, last_port0;	/* Double read for stabilising */

221 222 223 224
	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
			cmd);

L
Linus Torvalds 已提交
225
	/* The WCMD bit must be 1 and the command is only 4 bits in size */
226
	control_status = (cmd & 0x0F) | WD_WCMD;
227
	outb_p(control_status, pcwd_private.io_addr + 2);
L
Linus Torvalds 已提交
228 229
	udelay(ISA_COMMAND_TIMEOUT);

230
	port0 = inb_p(pcwd_private.io_addr);
L
Linus Torvalds 已提交
231 232
	for (i = 0; i < 25; ++i) {
		last_port0 = port0;
233
		port0 = inb_p(pcwd_private.io_addr);
L
Linus Torvalds 已提交
234 235 236 237

		if (port0 == last_port0)
			break;	/* Data is stable */

238
		udelay(250);
L
Linus Torvalds 已提交
239 240
	}

241 242 243 244
	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
			cmd, port0, last_port0);

L
Linus Torvalds 已提交
245 246 247 248 249
	return port0;
}

static int set_command_mode(void)
{
250
	int i, found = 0, count = 0;
L
Linus Torvalds 已提交
251 252

	/* Set the card into command mode */
253
	spin_lock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
254 255 256 257 258 259 260
	while ((!found) && (count < 3)) {
		i = send_isa_command(CMD_ISA_IDLE);

		if (i == 0x00)
			found = 1;
		else if (i == 0xF3) {
			/* Card does not like what we've done to it */
261
			outb_p(0x00, pcwd_private.io_addr + 2);
L
Linus Torvalds 已提交
262
			udelay(1200);	/* Spec says wait 1ms */
263
			outb_p(0x00, pcwd_private.io_addr + 2);
L
Linus Torvalds 已提交
264 265 266 267
			udelay(ISA_COMMAND_TIMEOUT);
		}
		count++;
	}
268 269
	spin_unlock(&pcwd_private.io_lock);
	pcwd_private.command_mode = found;
L
Linus Torvalds 已提交
270

271 272 273 274
	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "command_mode=%d\n",
				pcwd_private.command_mode);

L
Linus Torvalds 已提交
275 276 277 278 279 280
	return(found);
}

static void unset_command_mode(void)
{
	/* Set the card into normal mode */
281 282
	spin_lock(&pcwd_private.io_lock);
	outb_p(0x00, pcwd_private.io_addr + 2);
L
Linus Torvalds 已提交
283
	udelay(ISA_COMMAND_TIMEOUT);
284
	spin_unlock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
285

286
	pcwd_private.command_mode = 0;
287 288 289 290

	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "command_mode=%d\n",
				pcwd_private.command_mode);
L
Linus Torvalds 已提交
291 292
}

293 294 295 296 297 298
static inline void pcwd_check_temperature_support(void)
{
	if (inb(pcwd_private.io_addr) != 0xF0)
		pcwd_private.supports_temp = 1;
}

299
static inline void pcwd_get_firmware(void)
300 301 302
{
	int one, ten, hund, minor;

303
	strcpy(pcwd_private.fw_ver_str, "ERROR");
304 305 306 307 308 309

	if (set_command_mode()) {
		one = send_isa_command(CMD_ISA_VERSION_INTEGER);
		ten = send_isa_command(CMD_ISA_VERSION_TENTH);
		hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
		minor = send_isa_command(CMD_ISA_VERSION_MINOR);
310 311
		sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
					one, ten, hund, minor);
312 313
	}
	unset_command_mode();
314 315

	return;
316 317 318 319
}

static inline int pcwd_get_option_switches(void)
{
320
	int option_switches = 0;
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

	if (set_command_mode()) {
		/* Get switch settings */
		option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
	}

	unset_command_mode();
	return(option_switches);
}

static void pcwd_show_card_info(void)
{
	int option_switches;

	/* Get some extra info from the hardware (in command/debug/diag mode) */
	if (pcwd_private.revision == PCWD_REVISION_A)
337 338 339
		printk(KERN_INFO PFX
			"ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
							pcwd_private.io_addr);
340
	else if (pcwd_private.revision == PCWD_REVISION_C) {
341
		pcwd_get_firmware();
342
		printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
343
			pcwd_private.io_addr, pcwd_private.fw_ver_str);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
		option_switches = pcwd_get_option_switches();
		printk(KERN_INFO PFX "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"));

		/* Reprogram internal heartbeat to 2 seconds */
		if (set_command_mode()) {
			send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
			unset_command_mode();
		}
	}

	if (pcwd_private.supports_temp)
		printk(KERN_INFO PFX "Temperature Option Detected\n");

	if (pcwd_private.boot_status & WDIOF_CARDRESET)
		printk(KERN_INFO PFX "Previous reboot was caused by the card\n");

	if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
364 365 366 367
		printk(KERN_EMERG PFX
			"Card senses a CPU Overheat. Panicking!\n");
		printk(KERN_EMERG PFX
			"CPU Overheat\n");
368 369 370
	}

	if (pcwd_private.boot_status == 0)
371 372
		printk(KERN_INFO PFX
			"No previous trip detected - Cold boot or reset\n");
373 374
}

L
Linus Torvalds 已提交
375 376 377 378 379 380
static void pcwd_timer_ping(unsigned long data)
{
	int wdrst_stat;

	/* If we got a heartbeat pulse within the WDT_INTERVAL
	 * we agree to ping the WDT */
381
	if (time_before(jiffies, pcwd_private.next_heartbeat)) {
L
Linus Torvalds 已提交
382
		/* Ping the watchdog */
383 384
		spin_lock(&pcwd_private.io_lock);
		if (pcwd_private.revision == PCWD_REVISION_A) {
385 386
			/*  Rev A cards are reset by setting the
			    WD_WDRST bit in register 1 */
387
			wdrst_stat = inb_p(pcwd_private.io_addr);
L
Linus Torvalds 已提交
388 389 390
			wdrst_stat &= 0x0F;
			wdrst_stat |= WD_WDRST;

391
			outb_p(wdrst_stat, pcwd_private.io_addr + 1);
L
Linus Torvalds 已提交
392 393
		} else {
			/* Re-trigger watchdog by writing to port 0 */
394
			outb_p(0x00, pcwd_private.io_addr);
L
Linus Torvalds 已提交
395 396 397
		}

		/* Re-set the timer interval */
398
		mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
L
Linus Torvalds 已提交
399

400
		spin_unlock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
401
	} else {
402 403
		printk(KERN_WARNING PFX
			"Heartbeat lost! Will not ping the watchdog\n");
L
Linus Torvalds 已提交
404 405 406 407 408 409 410
	}
}

static int pcwd_start(void)
{
	int stat_reg;

411
	pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
L
Linus Torvalds 已提交
412 413

	/* Start the timer */
414
	mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
L
Linus Torvalds 已提交
415 416

	/* Enable the port */
417 418 419
	if (pcwd_private.revision == PCWD_REVISION_C) {
		spin_lock(&pcwd_private.io_lock);
		outb_p(0x00, pcwd_private.io_addr + 3);
L
Linus Torvalds 已提交
420
		udelay(ISA_COMMAND_TIMEOUT);
421 422
		stat_reg = inb_p(pcwd_private.io_addr + 2);
		spin_unlock(&pcwd_private.io_lock);
423
		if (stat_reg & WD_WDIS) {
L
Linus Torvalds 已提交
424 425 426 427
			printk(KERN_INFO PFX "Could not start watchdog\n");
			return -EIO;
		}
	}
428 429 430 431

	if (debug >= VERBOSE)
		printk(KERN_DEBUG PFX "Watchdog started\n");

L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439
	return 0;
}

static int pcwd_stop(void)
{
	int stat_reg;

	/* Stop the timer */
440
	del_timer(&pcwd_private.timer);
L
Linus Torvalds 已提交
441 442

	/*  Disable the board  */
443 444 445
	if (pcwd_private.revision == PCWD_REVISION_C) {
		spin_lock(&pcwd_private.io_lock);
		outb_p(0xA5, pcwd_private.io_addr + 3);
L
Linus Torvalds 已提交
446
		udelay(ISA_COMMAND_TIMEOUT);
447
		outb_p(0xA5, pcwd_private.io_addr + 3);
L
Linus Torvalds 已提交
448
		udelay(ISA_COMMAND_TIMEOUT);
449 450
		stat_reg = inb_p(pcwd_private.io_addr + 2);
		spin_unlock(&pcwd_private.io_lock);
451
		if ((stat_reg & WD_WDIS) == 0) {
L
Linus Torvalds 已提交
452 453 454 455
			printk(KERN_INFO PFX "Could not stop watchdog\n");
			return -EIO;
		}
	}
456 457 458 459

	if (debug >= VERBOSE)
		printk(KERN_DEBUG PFX "Watchdog stopped\n");

L
Linus Torvalds 已提交
460 461 462 463 464 465
	return 0;
}

static int pcwd_keepalive(void)
{
	/* user land ping */
466
	pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
467 468 469 470

	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");

L
Linus Torvalds 已提交
471 472 473 474 475
	return 0;
}

static int pcwd_set_heartbeat(int t)
{
476
	if (t < 2 || t > 7200) /* arbitrary upper limit */
L
Linus Torvalds 已提交
477 478 479
		return -EINVAL;

	heartbeat = t;
480 481 482 483 484

	if (debug >= VERBOSE)
		printk(KERN_DEBUG PFX "New heartbeat: %d\n",
		       heartbeat);

L
Linus Torvalds 已提交
485 486 487 488 489
	return 0;
}

static int pcwd_get_status(int *status)
{
490
	int control_status;
L
Linus Torvalds 已提交
491

492
	*status = 0;
493 494
	spin_lock(&pcwd_private.io_lock);
	if (pcwd_private.revision == PCWD_REVISION_A)
L
Linus Torvalds 已提交
495 496 497
		/* Rev A cards return status information from
		 * the base register, which is used for the
		 * temperature in other cards. */
498
		control_status = inb(pcwd_private.io_addr);
L
Linus Torvalds 已提交
499 500 501 502 503 504
	else {
		/* Rev C cards return card status in the base
		 * address + 1 register. And use different bits
		 * to indicate a card initiated reset, and an
		 * over-temperature condition. And the reboot
		 * status can be reset. */
505
		control_status = inb(pcwd_private.io_addr + 1);
L
Linus Torvalds 已提交
506
	}
507
	spin_unlock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
508

509
	if (pcwd_private.revision == PCWD_REVISION_A) {
510
		if (control_status & WD_WDRST)
L
Linus Torvalds 已提交
511 512
			*status |= WDIOF_CARDRESET;

513
		if (control_status & WD_T110) {
L
Linus Torvalds 已提交
514 515
			*status |= WDIOF_OVERHEAT;
			if (temp_panic) {
516 517
				printk(KERN_INFO PFX
					"Temperature overheat trip!\n");
518
				kernel_power_off();
L
Linus Torvalds 已提交
519 520 521
			}
		}
	} else {
522
		if (control_status & WD_REVC_WTRP)
L
Linus Torvalds 已提交
523 524
			*status |= WDIOF_CARDRESET;

525
		if (control_status & WD_REVC_TTRP) {
L
Linus Torvalds 已提交
526 527
			*status |= WDIOF_OVERHEAT;
			if (temp_panic) {
528 529
				printk(KERN_INFO PFX
					"Temperature overheat trip!\n");
530
				kernel_power_off();
L
Linus Torvalds 已提交
531 532 533 534 535 536 537 538 539
			}
		}
	}

	return 0;
}

static int pcwd_clear_status(void)
{
540 541
	int control_status;

542 543
	if (pcwd_private.revision == PCWD_REVISION_C) {
		spin_lock(&pcwd_private.io_lock);
544

545
		if (debug >= VERBOSE)
546 547
			printk(KERN_INFO PFX
					"clearing watchdog trip status\n");
548

549 550
		control_status = inb_p(pcwd_private.io_addr + 1);

551
		if (debug >= DEBUG) {
552 553
			printk(KERN_DEBUG PFX "status was: 0x%02x\n",
				control_status);
554 555 556 557
			printk(KERN_DEBUG PFX "sending: 0x%02x\n",
				(control_status & WD_REVC_R2DS));
		}

558
		/* clear reset status & Keep Relay 2 disable state as it is */
559 560
		outb_p((control_status & WD_REVC_R2DS),
						pcwd_private.io_addr + 1);
561

562
		spin_unlock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
563 564 565 566 567 568 569
	}
	return 0;
}

static int pcwd_get_temperature(int *temperature)
{
	/* check that port 0 gives temperature info and no command results */
570
	if (pcwd_private.command_mode)
L
Linus Torvalds 已提交
571 572 573
		return -1;

	*temperature = 0;
574
	if (!pcwd_private.supports_temp)
L
Linus Torvalds 已提交
575 576 577 578 579 580
		return -ENODEV;

	/*
	 * Convert celsius to fahrenheit, since this was
	 * the decided 'standard' for this return value.
	 */
581 582 583
	spin_lock(&pcwd_private.io_lock);
	*temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
	spin_unlock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
584

585 586 587 588 589
	if (debug >= DEBUG) {
		printk(KERN_DEBUG PFX "temperature is: %d F\n",
			*temperature);
	}

L
Linus Torvalds 已提交
590 591 592 593 594 595 596
	return 0;
}

/*
 *	/dev/watchdog handling
 */

597
static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
{
	int rv;
	int status;
	int temperature;
	int new_heartbeat;
	int __user *argp = (int __user *)arg;
	static struct watchdog_info ident = {
		.options =		WDIOF_OVERHEAT |
					WDIOF_CARDRESET |
					WDIOF_KEEPALIVEPING |
					WDIOF_SETTIMEOUT |
					WDIOF_MAGICCLOSE,
		.firmware_version =	1,
		.identity =		"PCWD",
	};

614
	switch (cmd) {
L
Linus Torvalds 已提交
615
	default:
616
		return -ENOTTY;
L
Linus Torvalds 已提交
617 618

	case WDIOC_GETSUPPORT:
619
		if (copy_to_user(argp, &ident, sizeof(ident)))
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627
			return -EFAULT;
		return 0;

	case WDIOC_GETSTATUS:
		pcwd_get_status(&status);
		return put_user(status, argp);

	case WDIOC_GETBOOTSTATUS:
628
		return put_user(pcwd_private.boot_status, argp);
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636

	case WDIOC_GETTEMP:
		if (pcwd_get_temperature(&temperature))
			return -EFAULT;

		return put_user(temperature, argp);

	case WDIOC_SETOPTIONS:
637 638
		if (pcwd_private.revision == PCWD_REVISION_C) {
			if (get_user(rv, argp))
L
Linus Torvalds 已提交
639 640
				return -EFAULT;

641 642 643 644
			if (rv & WDIOS_DISABLECARD) {
				status = pcwd_stop();
				if (status < 0)
					return status;
L
Linus Torvalds 已提交
645
			}
646 647 648 649
			if (rv & WDIOS_ENABLECARD) {
				status = pcwd_start();
				if (status < 0)
					return status;
L
Linus Torvalds 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 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
			}
			if (rv & WDIOS_TEMPPANIC)
				temp_panic = 1;
		}
		return -EINVAL;

	case WDIOC_KEEPALIVE:
		pcwd_keepalive();
		return 0;

	case WDIOC_SETTIMEOUT:
		if (get_user(new_heartbeat, argp))
			return -EFAULT;

		if (pcwd_set_heartbeat(new_heartbeat))
			return -EINVAL;

		pcwd_keepalive();
		/* Fall */

	case WDIOC_GETTIMEOUT:
		return put_user(heartbeat, argp);
	}

	return 0;
}

static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
			  loff_t *ppos)
{
	if (len) {
		if (!nowayout) {
			size_t i;

			/* In case it was set long ago */
			expect_close = 0;

			for (i = 0; i != len; i++) {
				char c;

				if (get_user(c, buf + i))
					return -EFAULT;
				if (c == 'V')
					expect_close = 42;
			}
		}
		pcwd_keepalive();
	}
	return len;
}

static int pcwd_open(struct inode *inode, struct file *file)
{
703
	if (test_and_set_bit(0, &open_allowed))
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712 713 714
		return -EBUSY;
	if (nowayout)
		__module_get(THIS_MODULE);
	/* Activate */
	pcwd_start();
	pcwd_keepalive();
	return nonseekable_open(inode, file);
}

static int pcwd_close(struct inode *inode, struct file *file)
{
715
	if (expect_close == 42)
L
Linus Torvalds 已提交
716
		pcwd_stop();
717 718 719
	else {
		printk(KERN_CRIT PFX
			"Unexpected close, not stopping watchdog!\n");
L
Linus Torvalds 已提交
720 721 722
		pcwd_keepalive();
	}
	expect_close = 0;
723
	clear_bit(0, &open_allowed);
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
	return 0;
}

/*
 *	/dev/temperature handling
 */

static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
			 loff_t *ppos)
{
	int temperature;

	if (pcwd_get_temperature(&temperature))
		return -EFAULT;

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

	return 1;
}

static int pcwd_temp_open(struct inode *inode, struct file *file)
{
747
	if (!pcwd_private.supports_temp)
L
Linus Torvalds 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761
		return -ENODEV;

	return nonseekable_open(inode, file);
}

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

/*
 *	Kernel Interfaces
 */

762
static const struct file_operations pcwd_fops = {
L
Linus Torvalds 已提交
763 764 765
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.write		= pcwd_write,
766
	.unlocked_ioctl	= pcwd_ioctl,
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776
	.open		= pcwd_open,
	.release	= pcwd_close,
};

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

777
static const struct file_operations pcwd_temp_fops = {
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= pcwd_temp_read,
	.open		= pcwd_temp_open,
	.release	= pcwd_temp_close,
};

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

/*
 *	Init & exit routines
 */

static inline int get_revision(void)
{
	int r = PCWD_REVISION_C;

799
	spin_lock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
800 801
	/* REV A cards use only 2 io ports; test
	 * presumes a floating bus reads as 0xff. */
802 803
	if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
	    (inb(pcwd_private.io_addr + 3) == 0xFF))
804
		r = PCWD_REVISION_A;
805
	spin_unlock(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
806 807 808 809

	return r;
}

810 811 812 813 814 815 816 817 818
/*
 *  The ISA cards have a heartbeat bit in one of the registers, which
 *  register is card dependent.  The heartbeat bit is monitored, and if
 *  found, is considered proof that a Berkshire card has been found.
 *  The initial rate is once per second at board start up, then twice
 *  per second for normal operation.
 */
static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
{
819
	int base_addr = pcwd_ioports[id];
820 821 822 823 824 825 826 827 828
	int port0, last_port0;	/* Reg 0, in case it's REV A */
	int port1, last_port1;	/* Register 1 for REV C cards */
	int i;
	int retval;

	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n",
			id);

829
	if (!request_region(base_addr, 4, "PCWD")) {
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
		printk(KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
		return 0;
	}

	retval = 0;

	port0 = inb_p(base_addr);	/* For REV A boards */
	port1 = inb_p(base_addr + 1);	/* For REV C boards */
	if (port0 != 0xff || port1 != 0xff) {
		/* Not an 'ff' from a floating bus, so must be a card! */
		for (i = 0; i < 4; ++i) {

			msleep(500);

			last_port0 = port0;
			last_port1 = port1;

			port0 = inb_p(base_addr);
			port1 = inb_p(base_addr + 1);

			/* Has either hearbeat bit changed?  */
			if ((port0 ^ last_port0) & WD_HRTBT ||
			    (port1 ^ last_port1) & WD_REVC_HRBT) {
				retval = 1;
				break;
			}
		}
	}
858
	release_region(base_addr, 4);
859 860 861 862 863

	return retval;
}

static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
L
Linus Torvalds 已提交
864 865 866
{
	int ret;

867 868 869 870
	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n",
			id);

L
Linus Torvalds 已提交
871 872
	cards_found++;
	if (cards_found == 1)
873 874
		printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n",
								WD_VER);
L
Linus Torvalds 已提交
875 876 877 878 879 880

	if (cards_found > 1) {
		printk(KERN_ERR PFX "This driver only supports 1 device\n");
		return -ENODEV;
	}

881
	if (pcwd_ioports[id] == 0x0000) {
L
Linus Torvalds 已提交
882 883 884
		printk(KERN_ERR PFX "No I/O-Address for card detected\n");
		return -ENODEV;
	}
885 886 887
	pcwd_private.io_addr = pcwd_ioports[id];

	spin_lock_init(&pcwd_private.io_lock);
L
Linus Torvalds 已提交
888 889

	/* Check card's revision */
890
	pcwd_private.revision = get_revision();
L
Linus Torvalds 已提交
891

892 893
	if (!request_region(pcwd_private.io_addr,
		(pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
L
Linus Torvalds 已提交
894
		printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
895
			pcwd_private.io_addr);
896
		ret = -EIO;
897
		goto error_request_region;
L
Linus Torvalds 已提交
898 899 900
	}

	/* Initial variables */
901
	pcwd_private.supports_temp = 0;
L
Linus Torvalds 已提交
902
	temp_panic = 0;
903
	pcwd_private.boot_status = 0x0000;
L
Linus Torvalds 已提交
904 905

	/* get the boot_status */
906
	pcwd_get_status(&pcwd_private.boot_status);
L
Linus Torvalds 已提交
907 908 909 910

	/* clear the "card caused reboot" flag */
	pcwd_clear_status();

J
Jiri Slaby 已提交
911
	setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
L
Linus Torvalds 已提交
912 913 914 915 916

	/*  Disable the board  */
	pcwd_stop();

	/*  Check whether or not the card supports the temperature device */
917
	pcwd_check_temperature_support();
L
Linus Torvalds 已提交
918

919 920
	/* Show info about the card itself */
	pcwd_show_card_info();
L
Linus Torvalds 已提交
921

922 923 924 925
	/* If heartbeat = 0 then we use the heartbeat from the dip-switches */
	if (heartbeat == 0)
		heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];

926 927
	/* Check that the heartbeat value is within it's range;
	   if not reset to the default */
928 929
	if (pcwd_set_heartbeat(heartbeat)) {
		pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
930 931 932
		printk(KERN_INFO PFX
		  "heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
							WATCHDOG_HEARTBEAT);
L
Linus Torvalds 已提交
933 934
	}

935
	if (pcwd_private.supports_temp) {
L
Linus Torvalds 已提交
936 937
		ret = misc_register(&temp_miscdev);
		if (ret) {
938 939 940
			printk(KERN_ERR PFX
			    "cannot register miscdev on minor=%d (err=%d)\n",
							TEMP_MINOR, ret);
941
			goto error_misc_register_temp;
L
Linus Torvalds 已提交
942 943 944 945 946
		}
	}

	ret = misc_register(&pcwd_miscdev);
	if (ret) {
947 948 949
		printk(KERN_ERR PFX
			"cannot register miscdev on minor=%d (err=%d)\n",
					WATCHDOG_MINOR, ret);
950
		goto error_misc_register_watchdog;
L
Linus Torvalds 已提交
951 952 953 954 955 956
	}

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

	return 0;
957 958 959 960 961

error_misc_register_watchdog:
	if (pcwd_private.supports_temp)
		misc_deregister(&temp_miscdev);
error_misc_register_temp:
962 963
	release_region(pcwd_private.io_addr,
			(pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
964 965 966 967
error_request_region:
	pcwd_private.io_addr = 0x0000;
	cards_found--;
	return ret;
L
Linus Torvalds 已提交
968 969
}

970
static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
L
Linus Torvalds 已提交
971
{
972 973 974 975 976 977 978
	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n",
			id);

	if (!pcwd_private.io_addr)
		return 1;

L
Linus Torvalds 已提交
979 980 981 982 983 984
	/*  Disable the board  */
	if (!nowayout)
		pcwd_stop();

	/* Deregister */
	misc_deregister(&pcwd_miscdev);
985
	if (pcwd_private.supports_temp)
L
Linus Torvalds 已提交
986
		misc_deregister(&temp_miscdev);
987 988
	release_region(pcwd_private.io_addr,
			(pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
989
	pcwd_private.io_addr = 0x0000;
990
	cards_found--;
991 992

	return 0;
L
Linus Torvalds 已提交
993 994
}

995
static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
L
Linus Torvalds 已提交
996
{
997 998 999
	if (debug >= DEBUG)
		printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n",
			id);
L
Linus Torvalds 已提交
1000

1001
	pcwd_stop();
L
Linus Torvalds 已提交
1002 1003
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
static struct isa_driver pcwd_isa_driver = {
	.match		= pcwd_isa_match,
	.probe		= pcwd_isa_probe,
	.remove		= __devexit_p(pcwd_isa_remove),
	.shutdown	= pcwd_isa_shutdown,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= WATCHDOG_NAME,
	},
};
L
Linus Torvalds 已提交
1014 1015 1016

static int __init pcwd_init_module(void)
{
1017
	return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
L
Linus Torvalds 已提交
1018 1019 1020 1021
}

static void __exit pcwd_cleanup_module(void)
{
1022
	isa_unregister_driver(&pcwd_isa_driver);
1023
	printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
L
Linus Torvalds 已提交
1024 1025 1026 1027 1028
}

module_init(pcwd_init_module);
module_exit(pcwd_cleanup_module);

1029
MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");
L
Linus Torvalds 已提交
1030
MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
1031
MODULE_VERSION(WATCHDOG_VERSION);
L
Linus Torvalds 已提交
1032 1033 1034
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
MODULE_ALIAS_MISCDEV(TEMP_MINOR);