blob: 1112ec8e61f36449d44c593e08cfff544d1533b5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PC Watchdog Driver
3 * by Ken Hollis (khollis@bitgate.com)
4 *
5 * Permission granted from Simon Machell (73244.1270@compuserve.com)
6 * Written for the Linux Kernel, and GPLed by Ken Hollis
7 *
8 * 960107 Added request_region routines, modulized the whole thing.
9 * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
10 * WD_TIMEOUT define.
11 * 960216 Added eof marker on the file, and changed verbose messages.
12 * 960716 Made functional and cosmetic changes to the source for
13 * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
14 * 960717 Removed read/seek routines, replaced with ioctl. Also, added
15 * check_region command due to Alan's suggestion.
16 * 960821 Made changes to compile in newer 2.0.x kernels. Added
17 * "cold reboot sense" entry.
18 * 960825 Made a few changes to code, deleted some defines and made
19 * typedefs to replace them. Made heartbeat reset only available
20 * via ioctl, and removed the write routine.
21 * 960828 Added new items for PC Watchdog Rev.C card.
22 * 960829 Changed around all of the IOCTLs, added new features,
23 * added watchdog disable/re-enable routines. Added firmware
24 * version reporting. Added read routine for temperature.
25 * Removed some extra defines, added an autodetect Revision
26 * routine.
27 * 961006 Revised some documentation, fixed some cosmetic bugs. Made
28 * drivers to panic the system if it's overheating at bootup.
29 * 961118 Changed some verbiage on some of the output, tidied up
30 * code bits, and added compatibility to 2.1.x.
31 * 970912 Enabled board on open and disable on close.
32 * 971107 Took account of recent VFS changes (broke read).
33 * 971210 Disable board on initialisation in case board already ticking.
34 * 971222 Changed open/close for temperature handling
35 * Michael Meskes <meskes@debian.org>.
36 * 980112 Used minor numbers from include/linux/miscdevice.h
37 * 990403 Clear reset status after reading control status register in
38 * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
39 * 990605 Made changes to code to support Firmware 1.22a, added
40 * fairly useless proc entry.
41 * 990610 removed said useless proc code for the merge <alan>
42 * 000403 Removed last traces of proc code. <davej>
43 * 011214 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
44 * Added timeout module option to override default
45 */
46
47/*
48 * A bells and whistles driver is available from http://www.pcwd.de/
49 * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
50 */
51
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010052#include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
53#include <linux/module.h> /* For module specific items */
54#include <linux/moduleparam.h> /* For new moduleparam's */
55#include <linux/types.h> /* For standard types (like size_t) */
56#include <linux/errno.h> /* For the -ENODEV/... values */
57#include <linux/kernel.h> /* For printk/panic/... */
58#include <linux/delay.h> /* For mdelay function */
59#include <linux/timer.h> /* For timer related operations */
60#include <linux/jiffies.h> /* For jiffies stuff */
61#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
62#include <linux/watchdog.h> /* For the watchdog specific items */
63#include <linux/notifier.h> /* For notifier support */
64#include <linux/reboot.h> /* For reboot_notifier stuff */
65#include <linux/init.h> /* For __init/__exit/... */
66#include <linux/fs.h> /* For file operations */
67#include <linux/ioport.h> /* For io-port access */
68#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Tim Schmielau4e57b682005-10-30 15:03:48 -080069#include <linux/sched.h> /* TASK_INTERRUPTIBLE, set_current_state() and friends */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010070#include <linux/slab.h> /* For kmalloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010072#include <asm/uaccess.h> /* For copy_to_user/put_user/... */
73#include <asm/io.h> /* For inb/outb/... */
74
75/* Module and version information */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +010076#define WD_VER "1.16 (03/01/2006)"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define PFX "pcwd: "
78
79/*
80 * It should be noted that PCWD_REVISION_B was removed because A and B
81 * are essentially the same types of card, with the exception that B
82 * has temperature reporting. Since I didn't receive a Rev.B card,
83 * the Rev.B card is not supported. (It's a good thing too, as they
84 * are no longer in production.)
85 */
86#define PCWD_REVISION_A 1
87#define PCWD_REVISION_C 2
88
89/*
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010090 * These are the defines that describe the control status #1 bits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * PC Watchdog card, revision A.
92 */
93#define WD_WDRST 0x01 /* Previously reset state */
94#define WD_T110 0x02 /* Temperature overheat sense */
95#define WD_HRTBT 0x04 /* Heartbeat sense */
96#define WD_RLY2 0x08 /* External relay triggered */
97#define WD_SRLY2 0x80 /* Software external relay triggered */
98
99/*
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100100 * These are the defines that describe the control status #1 bits for the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * PC Watchdog card, revision C.
102 */
103#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
104#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
105#define WD_REVC_TTRP 0x04 /* Temperature Trip status */
106
107/* max. time we give an ISA watchdog card to process a command */
108/* 500ms for each 4 bit response (according to spec.) */
109#define ISA_COMMAND_TIMEOUT 1000
110
111/* Watchdog's internal commands */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100112#define CMD_ISA_IDLE 0x00
113#define CMD_ISA_VERSION_INTEGER 0x01
114#define CMD_ISA_VERSION_TENTH 0x02
115#define CMD_ISA_VERSION_HUNDRETH 0x03
116#define CMD_ISA_VERSION_MINOR 0x04
117#define CMD_ISA_SWITCH_SETTINGS 0x05
118#define CMD_ISA_DELAY_TIME_2SECS 0x0A
119#define CMD_ISA_DELAY_TIME_4SECS 0x0B
120#define CMD_ISA_DELAY_TIME_8SECS 0x0C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/*
123 * We are using an kernel timer to do the pinging of the watchdog
124 * every ~500ms. We try to set the internal heartbeat of the
125 * watchdog to 2 ms.
126 */
127
128#define WDT_INTERVAL (HZ/2+1)
129
130/* We can only use 1 card due to the /dev/watchdog restriction */
131static int cards_found;
132
133/* internal variables */
134static atomic_t open_allowed = ATOMIC_INIT(1);
135static char expect_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static int temp_panic;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100137static struct { /* this is private data for each ISA-PC watchdog card */
138 int revision; /* The card's revision */
139 int supports_temp; /* Wether or not the card has a temperature device */
140 int command_mode; /* Wether or not the card is in command mode */
141 int boot_status; /* The card's boot status */
142 int io_addr; /* The cards I/O address */
143 spinlock_t io_lock; /* the lock for io operations */
144 struct timer_list timer; /* The timer that pings the watchdog */
145 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
146} pcwd_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* module parameters */
149#define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
150static int heartbeat = WATCHDOG_HEARTBEAT;
151module_param(heartbeat, int, 0);
152MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
153
Andrey Panin4bfdf372005-07-27 11:43:58 -0700154static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155module_param(nowayout, int, 0);
156MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
157
158/*
159 * Internal functions
160 */
161
162static int send_isa_command(int cmd)
163{
164 int i;
165 int control_status;
166 int port0, last_port0; /* Double read for stabilising */
167
168 /* The WCMD bit must be 1 and the command is only 4 bits in size */
169 control_status = (cmd & 0x0F) | 0x80;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100170 outb_p(control_status, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 udelay(ISA_COMMAND_TIMEOUT);
172
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100173 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 for (i = 0; i < 25; ++i) {
175 last_port0 = port0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100176 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (port0 == last_port0)
179 break; /* Data is stable */
180
181 udelay (250);
182 }
183
184 return port0;
185}
186
187static int set_command_mode(void)
188{
189 int i, found=0, count=0;
190
191 /* Set the card into command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100192 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 while ((!found) && (count < 3)) {
194 i = send_isa_command(CMD_ISA_IDLE);
195
196 if (i == 0x00)
197 found = 1;
198 else if (i == 0xF3) {
199 /* Card does not like what we've done to it */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100200 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 udelay(1200); /* Spec says wait 1ms */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100202 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 udelay(ISA_COMMAND_TIMEOUT);
204 }
205 count++;
206 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100207 spin_unlock(&pcwd_private.io_lock);
208 pcwd_private.command_mode = found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 return(found);
211}
212
213static void unset_command_mode(void)
214{
215 /* Set the card into normal mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100216 spin_lock(&pcwd_private.io_lock);
217 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100219 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100221 pcwd_private.command_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224static void pcwd_timer_ping(unsigned long data)
225{
226 int wdrst_stat;
227
228 /* If we got a heartbeat pulse within the WDT_INTERVAL
229 * we agree to ping the WDT */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100230 if(time_before(jiffies, pcwd_private.next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* Ping the watchdog */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100232 spin_lock(&pcwd_private.io_lock);
233 if (pcwd_private.revision == PCWD_REVISION_A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100235 wdrst_stat = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 wdrst_stat &= 0x0F;
237 wdrst_stat |= WD_WDRST;
238
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100239 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 } else {
241 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100242 outb_p(0x00, pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 /* Re-set the timer interval */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100246 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100248 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 } else {
250 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
251 }
252}
253
254static int pcwd_start(void)
255{
256 int stat_reg;
257
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100258 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* Start the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100261 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Enable the port */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100264 if (pcwd_private.revision == PCWD_REVISION_C) {
265 spin_lock(&pcwd_private.io_lock);
266 outb_p(0x00, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100268 stat_reg = inb_p(pcwd_private.io_addr + 2);
269 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (stat_reg & 0x10) {
271 printk(KERN_INFO PFX "Could not start watchdog\n");
272 return -EIO;
273 }
274 }
275 return 0;
276}
277
278static int pcwd_stop(void)
279{
280 int stat_reg;
281
282 /* Stop the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100283 del_timer(&pcwd_private.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Disable the board */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100286 if (pcwd_private.revision == PCWD_REVISION_C) {
287 spin_lock(&pcwd_private.io_lock);
288 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100290 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100292 stat_reg = inb_p(pcwd_private.io_addr + 2);
293 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if ((stat_reg & 0x10) == 0) {
295 printk(KERN_INFO PFX "Could not stop watchdog\n");
296 return -EIO;
297 }
298 }
299 return 0;
300}
301
302static int pcwd_keepalive(void)
303{
304 /* user land ping */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100305 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307}
308
309static int pcwd_set_heartbeat(int t)
310{
311 if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
312 return -EINVAL;
313
314 heartbeat = t;
315 return 0;
316}
317
318static int pcwd_get_status(int *status)
319{
320 int card_status;
321
322 *status=0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100323 spin_lock(&pcwd_private.io_lock);
324 if (pcwd_private.revision == PCWD_REVISION_A)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Rev A cards return status information from
326 * the base register, which is used for the
327 * temperature in other cards. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100328 card_status = inb(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 else {
330 /* Rev C cards return card status in the base
331 * address + 1 register. And use different bits
332 * to indicate a card initiated reset, and an
333 * over-temperature condition. And the reboot
334 * status can be reset. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100335 card_status = inb(pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100337 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100339 if (pcwd_private.revision == PCWD_REVISION_A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (card_status & WD_WDRST)
341 *status |= WDIOF_CARDRESET;
342
343 if (card_status & WD_T110) {
344 *status |= WDIOF_OVERHEAT;
345 if (temp_panic) {
346 printk (KERN_INFO PFX "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600347 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 }
350 } else {
351 if (card_status & WD_REVC_WTRP)
352 *status |= WDIOF_CARDRESET;
353
354 if (card_status & WD_REVC_TTRP) {
355 *status |= WDIOF_OVERHEAT;
356 if (temp_panic) {
357 printk (KERN_INFO PFX "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600358 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 }
361 }
362
363 return 0;
364}
365
366static int pcwd_clear_status(void)
367{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100368 if (pcwd_private.revision == PCWD_REVISION_C) {
369 spin_lock(&pcwd_private.io_lock);
370 outb_p(0x00, pcwd_private.io_addr + 1); /* clear reset status */
371 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 return 0;
374}
375
376static int pcwd_get_temperature(int *temperature)
377{
378 /* check that port 0 gives temperature info and no command results */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100379 if (pcwd_private.command_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -1;
381
382 *temperature = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100383 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -ENODEV;
385
386 /*
387 * Convert celsius to fahrenheit, since this was
388 * the decided 'standard' for this return value.
389 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100390 spin_lock(&pcwd_private.io_lock);
391 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
392 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 return 0;
395}
396
397/*
398 * /dev/watchdog handling
399 */
400
401static int pcwd_ioctl(struct inode *inode, struct file *file,
402 unsigned int cmd, unsigned long arg)
403{
404 int rv;
405 int status;
406 int temperature;
407 int new_heartbeat;
408 int __user *argp = (int __user *)arg;
409 static struct watchdog_info ident = {
410 .options = WDIOF_OVERHEAT |
411 WDIOF_CARDRESET |
412 WDIOF_KEEPALIVEPING |
413 WDIOF_SETTIMEOUT |
414 WDIOF_MAGICCLOSE,
415 .firmware_version = 1,
416 .identity = "PCWD",
417 };
418
419 switch(cmd) {
420 default:
421 return -ENOIOCTLCMD;
422
423 case WDIOC_GETSUPPORT:
424 if(copy_to_user(argp, &ident, sizeof(ident)))
425 return -EFAULT;
426 return 0;
427
428 case WDIOC_GETSTATUS:
429 pcwd_get_status(&status);
430 return put_user(status, argp);
431
432 case WDIOC_GETBOOTSTATUS:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100433 return put_user(pcwd_private.boot_status, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 case WDIOC_GETTEMP:
436 if (pcwd_get_temperature(&temperature))
437 return -EFAULT;
438
439 return put_user(temperature, argp);
440
441 case WDIOC_SETOPTIONS:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100442 if (pcwd_private.revision == PCWD_REVISION_C)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 {
444 if(copy_from_user(&rv, argp, sizeof(int)))
445 return -EFAULT;
446
447 if (rv & WDIOS_DISABLECARD)
448 {
449 return pcwd_stop();
450 }
451
452 if (rv & WDIOS_ENABLECARD)
453 {
454 return pcwd_start();
455 }
456
457 if (rv & WDIOS_TEMPPANIC)
458 {
459 temp_panic = 1;
460 }
461 }
462 return -EINVAL;
463
464 case WDIOC_KEEPALIVE:
465 pcwd_keepalive();
466 return 0;
467
468 case WDIOC_SETTIMEOUT:
469 if (get_user(new_heartbeat, argp))
470 return -EFAULT;
471
472 if (pcwd_set_heartbeat(new_heartbeat))
473 return -EINVAL;
474
475 pcwd_keepalive();
476 /* Fall */
477
478 case WDIOC_GETTIMEOUT:
479 return put_user(heartbeat, argp);
480 }
481
482 return 0;
483}
484
485static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
486 loff_t *ppos)
487{
488 if (len) {
489 if (!nowayout) {
490 size_t i;
491
492 /* In case it was set long ago */
493 expect_close = 0;
494
495 for (i = 0; i != len; i++) {
496 char c;
497
498 if (get_user(c, buf + i))
499 return -EFAULT;
500 if (c == 'V')
501 expect_close = 42;
502 }
503 }
504 pcwd_keepalive();
505 }
506 return len;
507}
508
509static int pcwd_open(struct inode *inode, struct file *file)
510{
511 if (!atomic_dec_and_test(&open_allowed) ) {
512 atomic_inc( &open_allowed );
513 return -EBUSY;
514 }
515
516 if (nowayout)
517 __module_get(THIS_MODULE);
518
519 /* Activate */
520 pcwd_start();
521 pcwd_keepalive();
522 return nonseekable_open(inode, file);
523}
524
525static int pcwd_close(struct inode *inode, struct file *file)
526{
527 if (expect_close == 42) {
528 pcwd_stop();
529 } else {
530 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
531 pcwd_keepalive();
532 }
533 expect_close = 0;
534 atomic_inc( &open_allowed );
535 return 0;
536}
537
538/*
539 * /dev/temperature handling
540 */
541
542static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
543 loff_t *ppos)
544{
545 int temperature;
546
547 if (pcwd_get_temperature(&temperature))
548 return -EFAULT;
549
550 if (copy_to_user(buf, &temperature, 1))
551 return -EFAULT;
552
553 return 1;
554}
555
556static int pcwd_temp_open(struct inode *inode, struct file *file)
557{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100558 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENODEV;
560
561 return nonseekable_open(inode, file);
562}
563
564static int pcwd_temp_close(struct inode *inode, struct file *file)
565{
566 return 0;
567}
568
569/*
570 * Notify system
571 */
572
573static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
574{
575 if (code==SYS_DOWN || code==SYS_HALT) {
576 /* Turn the WDT off */
577 pcwd_stop();
578 }
579
580 return NOTIFY_DONE;
581}
582
583/*
584 * Kernel Interfaces
585 */
586
587static struct file_operations pcwd_fops = {
588 .owner = THIS_MODULE,
589 .llseek = no_llseek,
590 .write = pcwd_write,
591 .ioctl = pcwd_ioctl,
592 .open = pcwd_open,
593 .release = pcwd_close,
594};
595
596static struct miscdevice pcwd_miscdev = {
597 .minor = WATCHDOG_MINOR,
598 .name = "watchdog",
599 .fops = &pcwd_fops,
600};
601
602static struct file_operations pcwd_temp_fops = {
603 .owner = THIS_MODULE,
604 .llseek = no_llseek,
605 .read = pcwd_temp_read,
606 .open = pcwd_temp_open,
607 .release = pcwd_temp_close,
608};
609
610static struct miscdevice temp_miscdev = {
611 .minor = TEMP_MINOR,
612 .name = "temperature",
613 .fops = &pcwd_temp_fops,
614};
615
616static struct notifier_block pcwd_notifier = {
617 .notifier_call = pcwd_notify_sys,
618};
619
620/*
621 * Init & exit routines
622 */
623
624static inline void get_support(void)
625{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100626 if (inb(pcwd_private.io_addr) != 0xF0)
627 pcwd_private.supports_temp = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630static inline int get_revision(void)
631{
632 int r = PCWD_REVISION_C;
633
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100634 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* REV A cards use only 2 io ports; test
636 * presumes a floating bus reads as 0xff. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100637 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
638 (inb(pcwd_private.io_addr + 3) == 0xFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 r=PCWD_REVISION_A;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100640 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 return r;
643}
644
645static inline char *get_firmware(void)
646{
647 int one, ten, hund, minor;
648 char *ret;
649
650 ret = kmalloc(6, GFP_KERNEL);
651 if(ret == NULL)
652 return NULL;
653
654 if (set_command_mode()) {
655 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
656 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
657 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
658 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
659 sprintf(ret, "%c.%c%c%c", one, ten, hund, minor);
660 }
661 else
662 sprintf(ret, "ERROR");
663
664 unset_command_mode();
665 return(ret);
666}
667
668static inline int get_option_switches(void)
669{
670 int rv=0;
671
672 if (set_command_mode()) {
673 /* Get switch settings */
674 rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
675 }
676
677 unset_command_mode();
678 return(rv);
679}
680
681static int __devinit pcwatchdog_init(int base_addr)
682{
683 int ret;
684 char *firmware;
685 int option_switches;
686
687 cards_found++;
688 if (cards_found == 1)
689 printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
690
691 if (cards_found > 1) {
692 printk(KERN_ERR PFX "This driver only supports 1 device\n");
693 return -ENODEV;
694 }
695
696 if (base_addr == 0x0000) {
697 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
698 return -ENODEV;
699 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100700 pcwd_private.io_addr = base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* Check card's revision */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100703 pcwd_private.revision = get_revision();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100705 if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100707 pcwd_private.io_addr);
708 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -EIO;
710 }
711
712 /* Initial variables */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100713 pcwd_private.supports_temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 temp_panic = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100715 pcwd_private.boot_status = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 /* get the boot_status */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100718 pcwd_get_status(&pcwd_private.boot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* clear the "card caused reboot" flag */
721 pcwd_clear_status();
722
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100723 init_timer(&pcwd_private.timer);
724 pcwd_private.timer.function = pcwd_timer_ping;
725 pcwd_private.timer.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* Disable the board */
728 pcwd_stop();
729
730 /* Check whether or not the card supports the temperature device */
731 get_support();
732
733 /* Get some extra info from the hardware (in command/debug/diag mode) */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100734 if (pcwd_private.revision == PCWD_REVISION_A)
735 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
736 else if (pcwd_private.revision == PCWD_REVISION_C) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 firmware = get_firmware();
738 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100739 pcwd_private.io_addr, firmware);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 kfree(firmware);
741 option_switches = get_option_switches();
742 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
743 option_switches,
744 ((option_switches & 0x10) ? "ON" : "OFF"),
745 ((option_switches & 0x08) ? "ON" : "OFF"));
746
747 /* Reprogram internal heartbeat to 2 seconds */
748 if (set_command_mode()) {
749 send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
750 unset_command_mode();
751 }
752 } else {
753 /* Should NEVER happen, unless get_revision() fails. */
754 printk(KERN_INFO PFX "Unable to get revision\n");
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100755 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
756 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -1;
758 }
759
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100760 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 printk(KERN_INFO PFX "Temperature Option Detected\n");
762
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100763 if (pcwd_private.boot_status & WDIOF_CARDRESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
765
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100766 if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
768 printk(KERN_EMERG PFX "CPU Overheat\n");
769 }
770
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100771 if (pcwd_private.boot_status == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
773
774 /* Check that the heartbeat value is within it's range ; if not reset to the default */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100775 if (pcwd_set_heartbeat(heartbeat)) {
776 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
777 printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
778 WATCHDOG_HEARTBEAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 ret = register_reboot_notifier(&pcwd_notifier);
782 if (ret) {
783 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
784 ret);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100785 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
786 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return ret;
788 }
789
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100790 if (pcwd_private.supports_temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 ret = misc_register(&temp_miscdev);
792 if (ret) {
793 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
794 TEMP_MINOR, ret);
795 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100796 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
797 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return ret;
799 }
800 }
801
802 ret = misc_register(&pcwd_miscdev);
803 if (ret) {
804 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
805 WATCHDOG_MINOR, ret);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100806 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 misc_deregister(&temp_miscdev);
808 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100809 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
810 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return ret;
812 }
813
814 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
815 heartbeat, nowayout);
816
817 return 0;
818}
819
820static void __devexit pcwatchdog_exit(void)
821{
822 /* Disable the board */
823 if (!nowayout)
824 pcwd_stop();
825
826 /* Deregister */
827 misc_deregister(&pcwd_miscdev);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100828 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 misc_deregister(&temp_miscdev);
830 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100831 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
832 pcwd_private.io_addr = 0x0000;
Wim Van Sebroeckf1c3a052005-12-10 14:36:24 +0100833 cards_found--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836/*
837 * The ISA cards have a heartbeat bit in one of the registers, which
838 * register is card dependent. The heartbeat bit is monitored, and if
839 * found, is considered proof that a Berkshire card has been found.
840 * The initial rate is once per second at board start up, then twice
841 * per second for normal operation.
842 */
843static int __init pcwd_checkcard(int base_addr)
844{
845 int port0, last_port0; /* Reg 0, in case it's REV A */
846 int port1, last_port1; /* Register 1 for REV C cards */
847 int i;
848 int retval;
849
850 if (!request_region (base_addr, 4, "PCWD")) {
851 printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
852 return 0;
853 }
854
855 retval = 0;
856
857 port0 = inb_p(base_addr); /* For REV A boards */
858 port1 = inb_p(base_addr + 1); /* For REV C boards */
859 if (port0 != 0xff || port1 != 0xff) {
860 /* Not an 'ff' from a floating bus, so must be a card! */
861 for (i = 0; i < 4; ++i) {
862
863 msleep(500);
864
865 last_port0 = port0;
866 last_port1 = port1;
867
868 port0 = inb_p(base_addr);
869 port1 = inb_p(base_addr + 1);
870
871 /* Has either hearbeat bit changed? */
872 if ((port0 ^ last_port0) & WD_HRTBT ||
873 (port1 ^ last_port1) & WD_REVC_HRBT) {
874 retval = 1;
875 break;
876 }
877 }
878 }
879 release_region (base_addr, 4);
880
881 return retval;
882}
883
884/*
885 * These are the auto-probe addresses available.
886 *
887 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
888 * Revision A has an address range of 2 addresses, while Revision C has 4.
889 */
890static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
891
892static int __init pcwd_init_module(void)
893{
894 int i, found = 0;
895
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100896 spin_lock_init(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 for (i = 0; pcwd_ioports[i] != 0; i++) {
899 if (pcwd_checkcard(pcwd_ioports[i])) {
900 if (!(pcwatchdog_init(pcwd_ioports[i])))
901 found++;
902 }
903 }
904
905 if (!found) {
906 printk (KERN_INFO PFX "No card detected, or port not available\n");
907 return -ENODEV;
908 }
909
910 return 0;
911}
912
913static void __exit pcwd_cleanup_module(void)
914{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100915 if (pcwd_private.io_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 pcwatchdog_exit();
917 return;
918}
919
920module_init(pcwd_init_module);
921module_exit(pcwd_cleanup_module);
922
923MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
924MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
925MODULE_LICENSE("GPL");
926MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
927MODULE_ALIAS_MISCDEV(TEMP_MINOR);