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