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