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