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