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