regulator: core - Rework machine API to remove string based functions.
[linux-2.6] / drivers / watchdog / pcwd.c
1 /*
2  * PC Watchdog Driver
3  * by Ken Hollis (khollis@bitgate.com)
4  *
5  * Permission granted from Simon Machell (smachell@berkprod.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
44  *              CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
45  *              Added timeout module option to override default
46  */
47
48 /*
49  *      A bells and whistles driver is available from http://www.pcwd.de/
50  *      More info available at http://www.berkprod.com/ or
51  *      http://www.pcwatchdog.com/
52  */
53
54 #include <linux/module.h>       /* For module specific items */
55 #include <linux/moduleparam.h>  /* For new moduleparam's */
56 #include <linux/types.h>        /* For standard types (like size_t) */
57 #include <linux/errno.h>        /* For the -ENODEV/... values */
58 #include <linux/kernel.h>       /* For printk/panic/... */
59 #include <linux/delay.h>        /* For mdelay function */
60 #include <linux/timer.h>        /* For timer related operations */
61 #include <linux/jiffies.h>      /* For jiffies stuff */
62 #include <linux/miscdevice.h>   /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
63 #include <linux/watchdog.h>     /* For the watchdog specific items */
64 #include <linux/reboot.h>       /* For kernel_power_off() */
65 #include <linux/init.h>         /* For __init/__exit/... */
66 #include <linux/fs.h>           /* For file operations */
67 #include <linux/isa.h>          /* For isa devices */
68 #include <linux/ioport.h>       /* For io-port access */
69 #include <linux/spinlock.h>     /* For spin_lock/spin_unlock/... */
70 #include <linux/uaccess.h>      /* For copy_to_user/put_user/... */
71 #include <linux/io.h>           /* For inb/outb/... */
72
73 /* Module and version information */
74 #define WATCHDOG_VERSION "1.20"
75 #define WATCHDOG_DATE "18 Feb 2007"
76 #define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
77 #define WATCHDOG_NAME "pcwd"
78 #define PFX WATCHDOG_NAME ": "
79 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
80 #define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
81
82 /*
83  * It should be noted that PCWD_REVISION_B was removed because A and B
84  * are essentially the same types of card, with the exception that B
85  * has temperature reporting.  Since I didn't receive a Rev.B card,
86  * the Rev.B card is not supported.  (It's a good thing too, as they
87  * are no longer in production.)
88  */
89 #define PCWD_REVISION_A         1
90 #define PCWD_REVISION_C         2
91
92 /*
93  * These are the auto-probe addresses available.
94  *
95  * Revision A only uses ports 0x270 and 0x370.  Revision C introduced 0x350.
96  * Revision A has an address range of 2 addresses, while Revision C has 4.
97  */
98 #define PCWD_ISA_NR_CARDS       3
99 static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
100
101 /*
102  * These are the defines that describe the control status bits for the
103  * PCI-PC Watchdog card.
104 */
105 /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
106 #define WD_WDRST                0x01    /* Previously reset state */
107 #define WD_T110                 0x02    /* Temperature overheat sense */
108 #define WD_HRTBT                0x04    /* Heartbeat sense */
109 #define WD_RLY2                 0x08    /* External relay triggered */
110 #define WD_SRLY2                0x80    /* Software external relay triggered */
111 /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
112 #define WD_REVC_WTRP            0x01    /* Watchdog Trip status */
113 #define WD_REVC_HRBT            0x02    /* Watchdog Heartbeat */
114 #define WD_REVC_TTRP            0x04    /* Temperature Trip status */
115 #define WD_REVC_RL2A            0x08    /* Relay 2 activated by
116                                                         on-board processor */
117 #define WD_REVC_RL1A            0x10    /* Relay 1 active */
118 #define WD_REVC_R2DS            0x40    /* Relay 2 disable */
119 #define WD_REVC_RLY2            0x80    /* Relay 2 activated? */
120 /* Port 2 : Control Status #2 */
121 #define WD_WDIS                 0x10    /* Watchdog Disabled */
122 #define WD_ENTP                 0x20    /* Watchdog Enable Temperature Trip */
123 #define WD_SSEL                 0x40    /* Watchdog Switch Select
124                                                         (1:SW1 <-> 0:SW2) */
125 #define WD_WCMD                 0x80    /* Watchdog Command Mode */
126
127 /* max. time we give an ISA watchdog card to process a command */
128 /* 500ms for each 4 bit response (according to spec.) */
129 #define ISA_COMMAND_TIMEOUT     1000
130
131 /* Watchdog's internal commands */
132 #define CMD_ISA_IDLE                    0x00
133 #define CMD_ISA_VERSION_INTEGER         0x01
134 #define CMD_ISA_VERSION_TENTH           0x02
135 #define CMD_ISA_VERSION_HUNDRETH        0x03
136 #define CMD_ISA_VERSION_MINOR           0x04
137 #define CMD_ISA_SWITCH_SETTINGS         0x05
138 #define CMD_ISA_RESET_PC                0x06
139 #define CMD_ISA_ARM_0                   0x07
140 #define CMD_ISA_ARM_30                  0x08
141 #define CMD_ISA_ARM_60                  0x09
142 #define CMD_ISA_DELAY_TIME_2SECS        0x0A
143 #define CMD_ISA_DELAY_TIME_4SECS        0x0B
144 #define CMD_ISA_DELAY_TIME_8SECS        0x0C
145 #define CMD_ISA_RESET_RELAYS            0x0D
146
147 /* Watchdog's Dip Switch heartbeat values */
148 static const int heartbeat_tbl[] = {
149         20,     /* OFF-OFF-OFF  = 20 Sec  */
150         40,     /* OFF-OFF-ON   = 40 Sec  */
151         60,     /* OFF-ON-OFF   =  1 Min  */
152         300,    /* OFF-ON-ON    =  5 Min  */
153         600,    /* ON-OFF-OFF   = 10 Min  */
154         1800,   /* ON-OFF-ON    = 30 Min  */
155         3600,   /* ON-ON-OFF    =  1 Hour */
156         7200,   /* ON-ON-ON     =  2 hour */
157 };
158
159 /*
160  * We are using an kernel timer to do the pinging of the watchdog
161  * every ~500ms. We try to set the internal heartbeat of the
162  * watchdog to 2 ms.
163  */
164
165 #define WDT_INTERVAL (HZ/2+1)
166
167 /* We can only use 1 card due to the /dev/watchdog restriction */
168 static int cards_found;
169
170 /* internal variables */
171 static unsigned long open_allowed;
172 static char expect_close;
173 static int temp_panic;
174
175 /* this is private data for each ISA-PC watchdog card */
176 static struct {
177         char fw_ver_str[6];             /* The cards firmware version */
178         int revision;                   /* The card's revision */
179         int supports_temp;              /* Whether or not the card has
180                                                 a temperature device */
181         int command_mode;               /* Whether or not the card is in
182                                                 command mode */
183         int boot_status;                /* The card's boot status */
184         int io_addr;                    /* The cards I/O address */
185         spinlock_t io_lock;             /* the lock for io operations */
186         struct timer_list timer;        /* The timer that pings the watchdog */
187         unsigned long next_heartbeat;   /* the next_heartbeat for the timer */
188 } pcwd_private;
189
190 /* module parameters */
191 #define QUIET   0       /* Default */
192 #define VERBOSE 1       /* Verbose */
193 #define DEBUG   2       /* print fancy stuff too */
194 static int debug = QUIET;
195 module_param(debug, int, 0);
196 MODULE_PARM_DESC(debug,
197                 "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
198
199 /* default heartbeat = delay-time from dip-switches */
200 #define WATCHDOG_HEARTBEAT 0
201 static int heartbeat = WATCHDOG_HEARTBEAT;
202 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) ")");
204
205 static int nowayout = WATCHDOG_NOWAYOUT;
206 module_param(nowayout, int, 0);
207 MODULE_PARM_DESC(nowayout,
208                 "Watchdog cannot be stopped once started (default="
209                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
210
211 /*
212  *      Internal functions
213  */
214
215 static int send_isa_command(int cmd)
216 {
217         int i;
218         int control_status;
219         int port0, last_port0;  /* Double read for stabilising */
220
221         if (debug >= DEBUG)
222                 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
223                         cmd);
224
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);
228         udelay(ISA_COMMAND_TIMEOUT);
229
230         port0 = inb_p(pcwd_private.io_addr);
231         for (i = 0; i < 25; ++i) {
232                 last_port0 = port0;
233                 port0 = inb_p(pcwd_private.io_addr);
234
235                 if (port0 == last_port0)
236                         break;  /* Data is stable */
237
238                 udelay(250);
239         }
240
241         if (debug >= DEBUG)
242                 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
243                         cmd, port0, last_port0);
244
245         return port0;
246 }
247
248 static int set_command_mode(void)
249 {
250         int i, found = 0, count = 0;
251
252         /* Set the card into command mode */
253         spin_lock(&pcwd_private.io_lock);
254         while ((!found) && (count < 3)) {
255                 i = send_isa_command(CMD_ISA_IDLE);
256
257                 if (i == 0x00)
258                         found = 1;
259                 else if (i == 0xF3) {
260                         /* Card does not like what we've done to it */
261                         outb_p(0x00, pcwd_private.io_addr + 2);
262                         udelay(1200);   /* Spec says wait 1ms */
263                         outb_p(0x00, pcwd_private.io_addr + 2);
264                         udelay(ISA_COMMAND_TIMEOUT);
265                 }
266                 count++;
267         }
268         spin_unlock(&pcwd_private.io_lock);
269         pcwd_private.command_mode = found;
270
271         if (debug >= DEBUG)
272                 printk(KERN_DEBUG PFX "command_mode=%d\n",
273                                 pcwd_private.command_mode);
274
275         return found;
276 }
277
278 static void unset_command_mode(void)
279 {
280         /* Set the card into normal mode */
281         spin_lock(&pcwd_private.io_lock);
282         outb_p(0x00, pcwd_private.io_addr + 2);
283         udelay(ISA_COMMAND_TIMEOUT);
284         spin_unlock(&pcwd_private.io_lock);
285
286         pcwd_private.command_mode = 0;
287
288         if (debug >= DEBUG)
289                 printk(KERN_DEBUG PFX "command_mode=%d\n",
290                                 pcwd_private.command_mode);
291 }
292
293 static inline void pcwd_check_temperature_support(void)
294 {
295         if (inb(pcwd_private.io_addr) != 0xF0)
296                 pcwd_private.supports_temp = 1;
297 }
298
299 static inline void pcwd_get_firmware(void)
300 {
301         int one, ten, hund, minor;
302
303         strcpy(pcwd_private.fw_ver_str, "ERROR");
304
305         if (set_command_mode()) {
306                 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
307                 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
308                 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
309                 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
310                 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
311                                         one, ten, hund, minor);
312         }
313         unset_command_mode();
314
315         return;
316 }
317
318 static inline int pcwd_get_option_switches(void)
319 {
320         int option_switches = 0;
321
322         if (set_command_mode()) {
323                 /* Get switch settings */
324                 option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
325         }
326
327         unset_command_mode();
328         return option_switches;
329 }
330
331 static void pcwd_show_card_info(void)
332 {
333         int option_switches;
334
335         /* Get some extra info from the hardware (in command/debug/diag mode) */
336         if (pcwd_private.revision == PCWD_REVISION_A)
337                 printk(KERN_INFO PFX
338                         "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
339                                                         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                 option_switches = pcwd_get_option_switches();
345                 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
346                         option_switches,
347                         ((option_switches & 0x10) ? "ON" : "OFF"),
348                         ((option_switches & 0x08) ? "ON" : "OFF"));
349
350                 /* Reprogram internal heartbeat to 2 seconds */
351                 if (set_command_mode()) {
352                         send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
353                         unset_command_mode();
354                 }
355         }
356
357         if (pcwd_private.supports_temp)
358                 printk(KERN_INFO PFX "Temperature Option Detected\n");
359
360         if (pcwd_private.boot_status & WDIOF_CARDRESET)
361                 printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
362
363         if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
364                 printk(KERN_EMERG PFX
365                         "Card senses a CPU Overheat. Panicking!\n");
366                 printk(KERN_EMERG PFX
367                         "CPU Overheat\n");
368         }
369
370         if (pcwd_private.boot_status == 0)
371                 printk(KERN_INFO PFX
372                         "No previous trip detected - Cold boot or reset\n");
373 }
374
375 static void pcwd_timer_ping(unsigned long data)
376 {
377         int wdrst_stat;
378
379         /* If we got a heartbeat pulse within the WDT_INTERVAL
380          * we agree to ping the WDT */
381         if (time_before(jiffies, pcwd_private.next_heartbeat)) {
382                 /* Ping the watchdog */
383                 spin_lock(&pcwd_private.io_lock);
384                 if (pcwd_private.revision == PCWD_REVISION_A) {
385                         /*  Rev A cards are reset by setting the
386                             WD_WDRST bit in register 1 */
387                         wdrst_stat = inb_p(pcwd_private.io_addr);
388                         wdrst_stat &= 0x0F;
389                         wdrst_stat |= WD_WDRST;
390
391                         outb_p(wdrst_stat, pcwd_private.io_addr + 1);
392                 } else {
393                         /* Re-trigger watchdog by writing to port 0 */
394                         outb_p(0x00, pcwd_private.io_addr);
395                 }
396
397                 /* Re-set the timer interval */
398                 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
399
400                 spin_unlock(&pcwd_private.io_lock);
401         } else {
402                 printk(KERN_WARNING PFX
403                         "Heartbeat lost! Will not ping the watchdog\n");
404         }
405 }
406
407 static int pcwd_start(void)
408 {
409         int stat_reg;
410
411         pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
412
413         /* Start the timer */
414         mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
415
416         /* Enable the port */
417         if (pcwd_private.revision == PCWD_REVISION_C) {
418                 spin_lock(&pcwd_private.io_lock);
419                 outb_p(0x00, pcwd_private.io_addr + 3);
420                 udelay(ISA_COMMAND_TIMEOUT);
421                 stat_reg = inb_p(pcwd_private.io_addr + 2);
422                 spin_unlock(&pcwd_private.io_lock);
423                 if (stat_reg & WD_WDIS) {
424                         printk(KERN_INFO PFX "Could not start watchdog\n");
425                         return -EIO;
426                 }
427         }
428
429         if (debug >= VERBOSE)
430                 printk(KERN_DEBUG PFX "Watchdog started\n");
431
432         return 0;
433 }
434
435 static int pcwd_stop(void)
436 {
437         int stat_reg;
438
439         /* Stop the timer */
440         del_timer(&pcwd_private.timer);
441
442         /*  Disable the board  */
443         if (pcwd_private.revision == PCWD_REVISION_C) {
444                 spin_lock(&pcwd_private.io_lock);
445                 outb_p(0xA5, pcwd_private.io_addr + 3);
446                 udelay(ISA_COMMAND_TIMEOUT);
447                 outb_p(0xA5, pcwd_private.io_addr + 3);
448                 udelay(ISA_COMMAND_TIMEOUT);
449                 stat_reg = inb_p(pcwd_private.io_addr + 2);
450                 spin_unlock(&pcwd_private.io_lock);
451                 if ((stat_reg & WD_WDIS) == 0) {
452                         printk(KERN_INFO PFX "Could not stop watchdog\n");
453                         return -EIO;
454                 }
455         }
456
457         if (debug >= VERBOSE)
458                 printk(KERN_DEBUG PFX "Watchdog stopped\n");
459
460         return 0;
461 }
462
463 static int pcwd_keepalive(void)
464 {
465         /* user land ping */
466         pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
467
468         if (debug >= DEBUG)
469                 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
470
471         return 0;
472 }
473
474 static int pcwd_set_heartbeat(int t)
475 {
476         if (t < 2 || t > 7200) /* arbitrary upper limit */
477                 return -EINVAL;
478
479         heartbeat = t;
480
481         if (debug >= VERBOSE)
482                 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
483                        heartbeat);
484
485         return 0;
486 }
487
488 static int pcwd_get_status(int *status)
489 {
490         int control_status;
491
492         *status = 0;
493         spin_lock(&pcwd_private.io_lock);
494         if (pcwd_private.revision == PCWD_REVISION_A)
495                 /* Rev A cards return status information from
496                  * the base register, which is used for the
497                  * temperature in other cards. */
498                 control_status = inb(pcwd_private.io_addr);
499         else {
500                 /* Rev C cards return card status in the base
501                  * address + 1 register. And use different bits
502                  * to indicate a card initiated reset, and an
503                  * over-temperature condition. And the reboot
504                  * status can be reset. */
505                 control_status = inb(pcwd_private.io_addr + 1);
506         }
507         spin_unlock(&pcwd_private.io_lock);
508
509         if (pcwd_private.revision == PCWD_REVISION_A) {
510                 if (control_status & WD_WDRST)
511                         *status |= WDIOF_CARDRESET;
512
513                 if (control_status & WD_T110) {
514                         *status |= WDIOF_OVERHEAT;
515                         if (temp_panic) {
516                                 printk(KERN_INFO PFX
517                                         "Temperature overheat trip!\n");
518                                 kernel_power_off();
519                         }
520                 }
521         } else {
522                 if (control_status & WD_REVC_WTRP)
523                         *status |= WDIOF_CARDRESET;
524
525                 if (control_status & WD_REVC_TTRP) {
526                         *status |= WDIOF_OVERHEAT;
527                         if (temp_panic) {
528                                 printk(KERN_INFO PFX
529                                         "Temperature overheat trip!\n");
530                                 kernel_power_off();
531                         }
532                 }
533         }
534
535         return 0;
536 }
537
538 static int pcwd_clear_status(void)
539 {
540         int control_status;
541
542         if (pcwd_private.revision == PCWD_REVISION_C) {
543                 spin_lock(&pcwd_private.io_lock);
544
545                 if (debug >= VERBOSE)
546                         printk(KERN_INFO PFX
547                                         "clearing watchdog trip status\n");
548
549                 control_status = inb_p(pcwd_private.io_addr + 1);
550
551                 if (debug >= DEBUG) {
552                         printk(KERN_DEBUG PFX "status was: 0x%02x\n",
553                                 control_status);
554                         printk(KERN_DEBUG PFX "sending: 0x%02x\n",
555                                 (control_status & WD_REVC_R2DS));
556                 }
557
558                 /* clear reset status & Keep Relay 2 disable state as it is */
559                 outb_p((control_status & WD_REVC_R2DS),
560                                                 pcwd_private.io_addr + 1);
561
562                 spin_unlock(&pcwd_private.io_lock);
563         }
564         return 0;
565 }
566
567 static int pcwd_get_temperature(int *temperature)
568 {
569         /* check that port 0 gives temperature info and no command results */
570         if (pcwd_private.command_mode)
571                 return -1;
572
573         *temperature = 0;
574         if (!pcwd_private.supports_temp)
575                 return -ENODEV;
576
577         /*
578          * Convert celsius to fahrenheit, since this was
579          * the decided 'standard' for this return value.
580          */
581         spin_lock(&pcwd_private.io_lock);
582         *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
583         spin_unlock(&pcwd_private.io_lock);
584
585         if (debug >= DEBUG) {
586                 printk(KERN_DEBUG PFX "temperature is: %d F\n",
587                         *temperature);
588         }
589
590         return 0;
591 }
592
593 /*
594  *      /dev/watchdog handling
595  */
596
597 static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
598 {
599         int rv;
600         int status;
601         int temperature;
602         int new_heartbeat;
603         int __user *argp = (int __user *)arg;
604         static struct watchdog_info ident = {
605                 .options =              WDIOF_OVERHEAT |
606                                         WDIOF_CARDRESET |
607                                         WDIOF_KEEPALIVEPING |
608                                         WDIOF_SETTIMEOUT |
609                                         WDIOF_MAGICCLOSE,
610                 .firmware_version =     1,
611                 .identity =             "PCWD",
612         };
613
614         switch (cmd) {
615         case WDIOC_GETSUPPORT:
616                 if (copy_to_user(argp, &ident, sizeof(ident)))
617                         return -EFAULT;
618                 return 0;
619
620         case WDIOC_GETSTATUS:
621                 pcwd_get_status(&status);
622                 return put_user(status, argp);
623
624         case WDIOC_GETBOOTSTATUS:
625                 return put_user(pcwd_private.boot_status, argp);
626
627         case WDIOC_GETTEMP:
628                 if (pcwd_get_temperature(&temperature))
629                         return -EFAULT;
630
631                 return put_user(temperature, argp);
632
633         case WDIOC_SETOPTIONS:
634                 if (pcwd_private.revision == PCWD_REVISION_C) {
635                         if (get_user(rv, argp))
636                                 return -EFAULT;
637
638                         if (rv & WDIOS_DISABLECARD) {
639                                 status = pcwd_stop();
640                                 if (status < 0)
641                                         return status;
642                         }
643                         if (rv & WDIOS_ENABLECARD) {
644                                 status = pcwd_start();
645                                 if (status < 0)
646                                         return status;
647                         }
648                         if (rv & WDIOS_TEMPPANIC)
649                                 temp_panic = 1;
650                 }
651                 return -EINVAL;
652
653         case WDIOC_KEEPALIVE:
654                 pcwd_keepalive();
655                 return 0;
656
657         case WDIOC_SETTIMEOUT:
658                 if (get_user(new_heartbeat, argp))
659                         return -EFAULT;
660
661                 if (pcwd_set_heartbeat(new_heartbeat))
662                         return -EINVAL;
663
664                 pcwd_keepalive();
665                 /* Fall */
666
667         case WDIOC_GETTIMEOUT:
668                 return put_user(heartbeat, argp);
669
670         default:
671                 return -ENOTTY;
672         }
673
674         return 0;
675 }
676
677 static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
678                           loff_t *ppos)
679 {
680         if (len) {
681                 if (!nowayout) {
682                         size_t i;
683
684                         /* In case it was set long ago */
685                         expect_close = 0;
686
687                         for (i = 0; i != len; i++) {
688                                 char c;
689
690                                 if (get_user(c, buf + i))
691                                         return -EFAULT;
692                                 if (c == 'V')
693                                         expect_close = 42;
694                         }
695                 }
696                 pcwd_keepalive();
697         }
698         return len;
699 }
700
701 static int pcwd_open(struct inode *inode, struct file *file)
702 {
703         if (test_and_set_bit(0, &open_allowed))
704                 return -EBUSY;
705         if (nowayout)
706                 __module_get(THIS_MODULE);
707         /* Activate */
708         pcwd_start();
709         pcwd_keepalive();
710         return nonseekable_open(inode, file);
711 }
712
713 static int pcwd_close(struct inode *inode, struct file *file)
714 {
715         if (expect_close == 42)
716                 pcwd_stop();
717         else {
718                 printk(KERN_CRIT PFX
719                         "Unexpected close, not stopping watchdog!\n");
720                 pcwd_keepalive();
721         }
722         expect_close = 0;
723         clear_bit(0, &open_allowed);
724         return 0;
725 }
726
727 /*
728  *      /dev/temperature handling
729  */
730
731 static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
732                          loff_t *ppos)
733 {
734         int temperature;
735
736         if (pcwd_get_temperature(&temperature))
737                 return -EFAULT;
738
739         if (copy_to_user(buf, &temperature, 1))
740                 return -EFAULT;
741
742         return 1;
743 }
744
745 static int pcwd_temp_open(struct inode *inode, struct file *file)
746 {
747         if (!pcwd_private.supports_temp)
748                 return -ENODEV;
749
750         return nonseekable_open(inode, file);
751 }
752
753 static int pcwd_temp_close(struct inode *inode, struct file *file)
754 {
755         return 0;
756 }
757
758 /*
759  *      Kernel Interfaces
760  */
761
762 static const struct file_operations pcwd_fops = {
763         .owner          = THIS_MODULE,
764         .llseek         = no_llseek,
765         .write          = pcwd_write,
766         .unlocked_ioctl = pcwd_ioctl,
767         .open           = pcwd_open,
768         .release        = pcwd_close,
769 };
770
771 static struct miscdevice pcwd_miscdev = {
772         .minor =        WATCHDOG_MINOR,
773         .name =         "watchdog",
774         .fops =         &pcwd_fops,
775 };
776
777 static const struct file_operations pcwd_temp_fops = {
778         .owner          = THIS_MODULE,
779         .llseek         = no_llseek,
780         .read           = pcwd_temp_read,
781         .open           = pcwd_temp_open,
782         .release        = pcwd_temp_close,
783 };
784
785 static struct miscdevice temp_miscdev = {
786         .minor =        TEMP_MINOR,
787         .name =         "temperature",
788         .fops =         &pcwd_temp_fops,
789 };
790
791 /*
792  *      Init & exit routines
793  */
794
795 static inline int get_revision(void)
796 {
797         int r = PCWD_REVISION_C;
798
799         spin_lock(&pcwd_private.io_lock);
800         /* REV A cards use only 2 io ports; test
801          * presumes a floating bus reads as 0xff. */
802         if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
803             (inb(pcwd_private.io_addr + 3) == 0xFF))
804                 r = PCWD_REVISION_A;
805         spin_unlock(&pcwd_private.io_lock);
806
807         return r;
808 }
809
810 /*
811  *  The ISA cards have a heartbeat bit in one of the registers, which
812  *  register is card dependent.  The heartbeat bit is monitored, and if
813  *  found, is considered proof that a Berkshire card has been found.
814  *  The initial rate is once per second at board start up, then twice
815  *  per second for normal operation.
816  */
817 static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
818 {
819         int base_addr = pcwd_ioports[id];
820         int port0, last_port0;  /* Reg 0, in case it's REV A */
821         int port1, last_port1;  /* Register 1 for REV C cards */
822         int i;
823         int retval;
824
825         if (debug >= DEBUG)
826                 printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n",
827                         id);
828
829         if (!request_region(base_addr, 4, "PCWD")) {
830                 printk(KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
831                 return 0;
832         }
833
834         retval = 0;
835
836         port0 = inb_p(base_addr);       /* For REV A boards */
837         port1 = inb_p(base_addr + 1);   /* For REV C boards */
838         if (port0 != 0xff || port1 != 0xff) {
839                 /* Not an 'ff' from a floating bus, so must be a card! */
840                 for (i = 0; i < 4; ++i) {
841
842                         msleep(500);
843
844                         last_port0 = port0;
845                         last_port1 = port1;
846
847                         port0 = inb_p(base_addr);
848                         port1 = inb_p(base_addr + 1);
849
850                         /* Has either hearbeat bit changed?  */
851                         if ((port0 ^ last_port0) & WD_HRTBT ||
852                             (port1 ^ last_port1) & WD_REVC_HRBT) {
853                                 retval = 1;
854                                 break;
855                         }
856                 }
857         }
858         release_region(base_addr, 4);
859
860         return retval;
861 }
862
863 static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
864 {
865         int ret;
866
867         if (debug >= DEBUG)
868                 printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n",
869                         id);
870
871         cards_found++;
872         if (cards_found == 1)
873                 printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n",
874                                                                 WD_VER);
875
876         if (cards_found > 1) {
877                 printk(KERN_ERR PFX "This driver only supports 1 device\n");
878                 return -ENODEV;
879         }
880
881         if (pcwd_ioports[id] == 0x0000) {
882                 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
883                 return -ENODEV;
884         }
885         pcwd_private.io_addr = pcwd_ioports[id];
886
887         spin_lock_init(&pcwd_private.io_lock);
888
889         /* Check card's revision */
890         pcwd_private.revision = get_revision();
891
892         if (!request_region(pcwd_private.io_addr,
893                 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
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;
898         }
899
900         /* Initial variables */
901         pcwd_private.supports_temp = 0;
902         temp_panic = 0;
903         pcwd_private.boot_status = 0x0000;
904
905         /* get the boot_status */
906         pcwd_get_status(&pcwd_private.boot_status);
907
908         /* clear the "card caused reboot" flag */
909         pcwd_clear_status();
910
911         setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
912
913         /*  Disable the board  */
914         pcwd_stop();
915
916         /*  Check whether or not the card supports the temperature device */
917         pcwd_check_temperature_support();
918
919         /* Show info about the card itself */
920         pcwd_show_card_info();
921
922         /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
923         if (heartbeat == 0)
924                 heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
925
926         /* Check that the heartbeat value is within it's range;
927            if not reset to the default */
928         if (pcwd_set_heartbeat(heartbeat)) {
929                 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
930                 printk(KERN_INFO PFX
931                   "heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
932                                                         WATCHDOG_HEARTBEAT);
933         }
934
935         if (pcwd_private.supports_temp) {
936                 ret = misc_register(&temp_miscdev);
937                 if (ret) {
938                         printk(KERN_ERR PFX
939                             "cannot register miscdev on minor=%d (err=%d)\n",
940                                                         TEMP_MINOR, ret);
941                         goto error_misc_register_temp;
942                 }
943         }
944
945         ret = misc_register(&pcwd_miscdev);
946         if (ret) {
947                 printk(KERN_ERR PFX
948                         "cannot register miscdev on minor=%d (err=%d)\n",
949                                         WATCHDOG_MINOR, ret);
950                 goto error_misc_register_watchdog;
951         }
952
953         printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
954                 heartbeat, nowayout);
955
956         return 0;
957
958 error_misc_register_watchdog:
959         if (pcwd_private.supports_temp)
960                 misc_deregister(&temp_miscdev);
961 error_misc_register_temp:
962         release_region(pcwd_private.io_addr,
963                         (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
964 error_request_region:
965         pcwd_private.io_addr = 0x0000;
966         cards_found--;
967         return ret;
968 }
969
970 static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
971 {
972         if (debug >= DEBUG)
973                 printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n",
974                         id);
975
976         if (!pcwd_private.io_addr)
977                 return 1;
978
979         /*  Disable the board  */
980         if (!nowayout)
981                 pcwd_stop();
982
983         /* Deregister */
984         misc_deregister(&pcwd_miscdev);
985         if (pcwd_private.supports_temp)
986                 misc_deregister(&temp_miscdev);
987         release_region(pcwd_private.io_addr,
988                         (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
989         pcwd_private.io_addr = 0x0000;
990         cards_found--;
991
992         return 0;
993 }
994
995 static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
996 {
997         if (debug >= DEBUG)
998                 printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n",
999                         id);
1000
1001         pcwd_stop();
1002 }
1003
1004 static struct isa_driver pcwd_isa_driver = {
1005         .match          = pcwd_isa_match,
1006         .probe          = pcwd_isa_probe,
1007         .remove         = __devexit_p(pcwd_isa_remove),
1008         .shutdown       = pcwd_isa_shutdown,
1009         .driver         = {
1010                 .owner  = THIS_MODULE,
1011                 .name   = WATCHDOG_NAME,
1012         },
1013 };
1014
1015 static int __init pcwd_init_module(void)
1016 {
1017         return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
1018 }
1019
1020 static void __exit pcwd_cleanup_module(void)
1021 {
1022         isa_unregister_driver(&pcwd_isa_driver);
1023         printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
1024 }
1025
1026 module_init(pcwd_init_module);
1027 module_exit(pcwd_cleanup_module);
1028
1029 MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");
1030 MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
1031 MODULE_VERSION(WATCHDOG_VERSION);
1032 MODULE_LICENSE("GPL");
1033 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
1034 MODULE_ALIAS_MISCDEV(TEMP_MINOR);