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