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