patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / char / watchdog / wdt.c
1 /*
2  *      Industrial Computer Source WDT500/501 driver
3  *
4  *      (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5  *                              http://www.redhat.com
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13  *      warranty for any of this software. This material is provided
14  *      "AS-IS" and at no charge.
15  *
16  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
17  *
18  *      Release 0.10.
19  *
20  *      Fixes
21  *              Dave Gregorich  :       Modularisation and minor bugs
22  *              Alan Cox        :       Added the watchdog ioctl() stuff
23  *              Alan Cox        :       Fixed the reboot problem (as noted by
24  *                                      Matt Crocker).
25  *              Alan Cox        :       Added wdt= boot option
26  *              Alan Cox        :       Cleaned up copy/user stuff
27  *              Tim Hockin      :       Added insmod parameters, comment cleanup
28  *                                      Parameterized timeout
29  *              Tigran Aivazian :       Restructured wdt_init() to handle failures
30  *              Joel Becker     :       Added WDIOC_GET/SETTIMEOUT
31  *              Matt Domsch     :       Added nowayout module option
32  */
33
34 #include <linux/config.h>
35 #include <linux/interrupt.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/types.h>
39 #include <linux/miscdevice.h>
40 #include <linux/watchdog.h>
41 #include <linux/fs.h>
42 #include <linux/ioport.h>
43 #include <linux/notifier.h>
44 #include <linux/reboot.h>
45 #include <linux/init.h>
46
47 #include <asm/io.h>
48 #include <asm/uaccess.h>
49 #include <asm/system.h>
50 #include "wd501p.h"
51
52 static unsigned long wdt_is_open;
53 static char expect_close;
54
55 /*
56  *      Module parameters
57  */
58
59 #define WD_TIMO 60                      /* Default heartbeat = 60 seconds */
60
61 static int heartbeat = WD_TIMO;
62 static int wd_heartbeat;
63 module_param(heartbeat, int, 0);
64 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WD_TIMO) ")");
65
66 #ifdef CONFIG_WATCHDOG_NOWAYOUT
67 static int nowayout = 1;
68 #else
69 static int nowayout = 0;
70 #endif
71
72 module_param(nowayout, int, 0);
73 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
74
75 /* You must set these - there is no sane way to probe for this board. */
76 static int io=0x240;
77 static int irq=11;
78
79 module_param(io, int, 0);
80 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
81 module_param(irq, int, 0);
82 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
83
84 #ifdef CONFIG_WDT_501
85 /* Support for the Fan Tachometer on the WDT501-P */
86 static int tachometer;
87
88 module_param(tachometer, int, 0);
89 MODULE_PARM_DESC(tachometer, "WDT501-P Fan Tachometer support (0=disable, default=0)");
90 #endif /* CONFIG_WDT_501 */
91
92 /*
93  *      Programming support
94  */
95
96 static void wdt_ctr_mode(int ctr, int mode)
97 {
98         ctr<<=6;
99         ctr|=0x30;
100         ctr|=(mode<<1);
101         outb_p(ctr, WDT_CR);
102 }
103
104 static void wdt_ctr_load(int ctr, int val)
105 {
106         outb_p(val&0xFF, WDT_COUNT0+ctr);
107         outb_p(val>>8, WDT_COUNT0+ctr);
108 }
109
110 /**
111  *      wdt_start:
112  *
113  *      Start the watchdog driver.
114  */
115
116 static int wdt_start(void)
117 {
118         inb_p(WDT_DC);                  /* Disable watchdog */
119         wdt_ctr_mode(0,3);              /* Program CTR0 for Mode 3: Square Wave Generator */
120         wdt_ctr_mode(1,2);              /* Program CTR1 for Mode 2: Rate Generator */
121         wdt_ctr_mode(2,0);              /* Program CTR2 for Mode 0: Pulse on Terminal Count */
122         wdt_ctr_load(0, 8948);          /* Count at 100Hz */
123         wdt_ctr_load(1,wd_heartbeat);   /* Heartbeat */
124         wdt_ctr_load(2,65535);          /* Length of reset pulse */
125         outb_p(0, WDT_DC);              /* Enable watchdog */
126         return 0;
127 }
128
129 /**
130  *      wdt_stop:
131  *
132  *      Stop the watchdog driver.
133  */
134
135 static int wdt_stop (void)
136 {
137         /* Turn the card off */
138         inb_p(WDT_DC);                  /* Disable watchdog */
139         wdt_ctr_load(2,0);              /* 0 length reset pulses now */
140         return 0;
141 }
142
143 /**
144  *      wdt_ping:
145  *
146  *      Reload counter one with the watchdog heartbeat. We don't bother reloading
147  *      the cascade counter.
148  */
149
150 static int wdt_ping(void)
151 {
152         /* Write a watchdog value */
153         inb_p(WDT_DC);                  /* Disable watchdog */
154         wdt_ctr_mode(1,2);              /* Re-Program CTR1 for Mode 2: Rate Generator */
155         wdt_ctr_load(1,wd_heartbeat);   /* Heartbeat */
156         outb_p(0, WDT_DC);              /* Enable watchdog */
157         return 0;
158 }
159
160 /**
161  *      wdt_set_heartbeat:
162  *      @t:             the new heartbeat value that needs to be set.
163  *
164  *      Set a new heartbeat value for the watchdog device. If the heartbeat value is
165  *      incorrect we keep the old value and return -EINVAL. If successfull we
166  *      return 0.
167  */
168 static int wdt_set_heartbeat(int t)
169 {
170         if ((t < 1) || (t > 65535))
171                 return -EINVAL;
172
173         heartbeat = t;
174         wd_heartbeat = t * 100;
175         return 0;
176 }
177
178 /**
179  *      wdt_get_status:
180  *      @status:                the new status.
181  *
182  *      Extract the status information from a WDT watchdog device. There are
183  *      several board variants so we have to know which bits are valid. Some
184  *      bits default to one and some to zero in order to be maximally painful.
185  *
186  *      we then map the bits onto the status ioctl flags.
187  */
188
189 static int wdt_get_status(int *status)
190 {
191         unsigned char new_status=inb_p(WDT_SR);
192
193         *status=0;
194         if (new_status & WDC_SR_ISOI0)
195                 *status |= WDIOF_EXTERN1;
196         if (new_status & WDC_SR_ISII1)
197                 *status |= WDIOF_EXTERN2;
198 #ifdef CONFIG_WDT_501
199         if (!(new_status & WDC_SR_TGOOD))
200                 *status |= WDIOF_OVERHEAT;
201         if (!(new_status & WDC_SR_PSUOVER))
202                 *status |= WDIOF_POWEROVER;
203         if (!(new_status & WDC_SR_PSUUNDR))
204                 *status |= WDIOF_POWERUNDER;
205         if (tachometer) {
206                 if (!(new_status & WDC_SR_FANGOOD))
207                         *status |= WDIOF_FANFAULT;
208         }
209 #endif /* CONFIG_WDT_501 */
210         return 0;
211 }
212
213 #ifdef CONFIG_WDT_501
214 /**
215  *      wdt_get_temperature:
216  *
217  *      Reports the temperature in degrees Fahrenheit. The API is in
218  *      farenheit. It was designed by an imperial measurement luddite.
219  */
220
221 static int wdt_get_temperature(int *temperature)
222 {
223         unsigned short c=inb_p(WDT_RT);
224
225         *temperature = (c * 11 / 15) + 7;
226         return 0;
227 }
228 #endif /* CONFIG_WDT_501 */
229
230 /**
231  *      wdt_interrupt:
232  *      @irq:           Interrupt number
233  *      @dev_id:        Unused as we don't allow multiple devices.
234  *      @regs:          Unused.
235  *
236  *      Handle an interrupt from the board. These are raised when the status
237  *      map changes in what the board considers an interesting way. That means
238  *      a failure condition occurring.
239  */
240
241 static irqreturn_t wdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
242 {
243         /*
244          *      Read the status register see what is up and
245          *      then printk it.
246          */
247         unsigned char status=inb_p(WDT_SR);
248
249         printk(KERN_CRIT "WDT status %d\n", status);
250
251 #ifdef CONFIG_WDT_501
252         if (!(status & WDC_SR_TGOOD))
253                 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
254         if (!(status & WDC_SR_PSUOVER))
255                 printk(KERN_CRIT "PSU over voltage.\n");
256         if (!(status & WDC_SR_PSUUNDR))
257                 printk(KERN_CRIT "PSU under voltage.\n");
258         if (tachometer) {
259                 if (!(status & WDC_SR_FANGOOD))
260                         printk(KERN_CRIT "Possible fan fault.\n");
261         }
262 #endif /* CONFIG_WDT_501 */
263         if (!(status & WDC_SR_WCCR))
264 #ifdef SOFTWARE_REBOOT
265 #ifdef ONLY_TESTING
266                 printk(KERN_CRIT "Would Reboot.\n");
267 #else
268                 printk(KERN_CRIT "Initiating system reboot.\n");
269                 machine_restart(NULL);
270 #endif
271 #else
272                 printk(KERN_CRIT "Reset in 5ms.\n");
273 #endif
274         return IRQ_HANDLED;
275 }
276
277
278 /**
279  *      wdt_write:
280  *      @file: file handle to the watchdog
281  *      @buf: buffer to write (unused as data does not matter here
282  *      @count: count of bytes
283  *      @ppos: pointer to the position to write. No seeks allowed
284  *
285  *      A write to a watchdog device is defined as a keepalive signal. Any
286  *      write of data will do, as we we don't define content meaning.
287  */
288
289 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
290 {
291         /*  Can't seek (pwrite) on this device  */
292         if (ppos != &file->f_pos)
293                 return -ESPIPE;
294
295         if(count) {
296                 if (!nowayout) {
297                         size_t i;
298
299                         /* In case it was set long ago */
300                         expect_close = 0;
301
302                         for (i = 0; i != count; i++) {
303                                 char c;
304                                 if (get_user(c, buf + i))
305                                         return -EFAULT;
306                                 if (c == 'V')
307                                         expect_close = 42;
308                         }
309                 }
310                 wdt_ping();
311         }
312         return count;
313 }
314
315 /**
316  *      wdt_ioctl:
317  *      @inode: inode of the device
318  *      @file: file handle to the device
319  *      @cmd: watchdog command
320  *      @arg: argument pointer
321  *
322  *      The watchdog API defines a common set of functions for all watchdogs
323  *      according to their available features. We only actually usefully support
324  *      querying capabilities and current status.
325  */
326
327 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
328         unsigned long arg)
329 {
330         int new_heartbeat;
331         int status;
332
333         static struct watchdog_info ident = {
334                 .options =              WDIOF_SETTIMEOUT|
335                                         WDIOF_MAGICCLOSE|
336                                         WDIOF_KEEPALIVEPING,
337                 .firmware_version =     1,
338                 .identity =             "WDT500/501",
339         };
340
341         /* Add options according to the card we have */
342         ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
343 #ifdef CONFIG_WDT_501
344         ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
345         if (tachometer)
346                 ident.options |= WDIOF_FANFAULT;
347 #endif /* CONFIG_WDT_501 */
348
349         switch(cmd)
350         {
351                 default:
352                         return -ENOIOCTLCMD;
353                 case WDIOC_GETSUPPORT:
354                         return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
355
356                 case WDIOC_GETSTATUS:
357                         wdt_get_status(&status);
358                         return put_user(status,(int *)arg);
359                 case WDIOC_GETBOOTSTATUS:
360                         return put_user(0, (int *)arg);
361                 case WDIOC_KEEPALIVE:
362                         wdt_ping();
363                         return 0;
364                 case WDIOC_SETTIMEOUT:
365                         if (get_user(new_heartbeat, (int *)arg))
366                                 return -EFAULT;
367
368                         if (wdt_set_heartbeat(new_heartbeat))
369                                 return -EINVAL;
370
371                         wdt_ping();
372                         /* Fall */
373                 case WDIOC_GETTIMEOUT:
374                         return put_user(heartbeat, (int *)arg);
375         }
376 }
377
378 /**
379  *      wdt_open:
380  *      @inode: inode of device
381  *      @file: file handle to device
382  *
383  *      The watchdog device has been opened. The watchdog device is single
384  *      open and on opening we load the counters. Counter zero is a 100Hz
385  *      cascade, into counter 1 which downcounts to reboot. When the counter
386  *      triggers counter 2 downcounts the length of the reset pulse which
387  *      set set to be as long as possible.
388  */
389
390 static int wdt_open(struct inode *inode, struct file *file)
391 {
392         if(test_and_set_bit(0, &wdt_is_open))
393                 return -EBUSY;
394         /*
395          *      Activate
396          */
397         wdt_start();
398         return 0;
399 }
400
401 /**
402  *      wdt_release:
403  *      @inode: inode to board
404  *      @file: file handle to board
405  *
406  *      The watchdog has a configurable API. There is a religious dispute
407  *      between people who want their watchdog to be able to shut down and
408  *      those who want to be sure if the watchdog manager dies the machine
409  *      reboots. In the former case we disable the counters, in the latter
410  *      case you have to open it again very soon.
411  */
412
413 static int wdt_release(struct inode *inode, struct file *file)
414 {
415         if (expect_close == 42) {
416                 wdt_stop();
417                 clear_bit(0, &wdt_is_open);
418         } else {
419                 printk(KERN_CRIT "wdt: WDT device closed unexpectedly.  WDT will not stop!\n");
420                 wdt_ping();
421         }
422         expect_close = 0;
423         return 0;
424 }
425
426 #ifdef CONFIG_WDT_501
427 /**
428  *      wdt_temp_read:
429  *      @file: file handle to the watchdog board
430  *      @buf: buffer to write 1 byte into
431  *      @count: length of buffer
432  *      @ptr: offset (no seek allowed)
433  *
434  *      Temp_read reports the temperature in degrees Fahrenheit. The API is in
435  *      farenheit. It was designed by an imperial measurement luddite.
436  */
437
438 static ssize_t wdt_temp_read(struct file *file, char *buf, size_t count, loff_t *ptr)
439 {
440         int temperature;
441
442         /*  Can't seek (pread) on this device  */
443         if (ptr != &file->f_pos)
444                 return -ESPIPE;
445
446         if (wdt_get_temperature(&temperature))
447                 return -EFAULT;
448
449         if (copy_to_user (buf, &temperature, 1))
450                 return -EFAULT;
451
452         return 1;
453 }
454
455 /**
456  *      wdt_temp_open:
457  *      @inode: inode of device
458  *      @file: file handle to device
459  *
460  *      The temperature device has been opened.
461  */
462
463 static int wdt_temp_open(struct inode *inode, struct file *file)
464 {
465         return 0;
466 }
467
468 /**
469  *      wdt_temp_release:
470  *      @inode: inode to board
471  *      @file: file handle to board
472  *
473  *      The temperature device has been closed.
474  */
475
476 static int wdt_temp_release(struct inode *inode, struct file *file)
477 {
478         return 0;
479 }
480 #endif /* CONFIG_WDT_501 */
481
482 /**
483  *      notify_sys:
484  *      @this: our notifier block
485  *      @code: the event being reported
486  *      @unused: unused
487  *
488  *      Our notifier is called on system shutdowns. We want to turn the card
489  *      off at reboot otherwise the machine will reboot again during memory
490  *      test or worse yet during the following fsck. This would suck, in fact
491  *      trust me - if it happens it does suck.
492  */
493
494 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
495         void *unused)
496 {
497         if(code==SYS_DOWN || code==SYS_HALT) {
498                 /* Turn the card off */
499                 wdt_stop();
500         }
501         return NOTIFY_DONE;
502 }
503
504 /*
505  *      Kernel Interfaces
506  */
507
508
509 static struct file_operations wdt_fops = {
510         .owner          = THIS_MODULE,
511         .llseek         = no_llseek,
512         .write          = wdt_write,
513         .ioctl          = wdt_ioctl,
514         .open           = wdt_open,
515         .release        = wdt_release,
516 };
517
518 static struct miscdevice wdt_miscdev = {
519         .minor  = WATCHDOG_MINOR,
520         .name   = "watchdog",
521         .fops   = &wdt_fops,
522 };
523
524 #ifdef CONFIG_WDT_501
525 static struct file_operations wdt_temp_fops = {
526         .owner          = THIS_MODULE,
527         .llseek         = no_llseek,
528         .read           = wdt_temp_read,
529         .open           = wdt_temp_open,
530         .release        = wdt_temp_release,
531 };
532
533 static struct miscdevice temp_miscdev = {
534         .minor  = TEMP_MINOR,
535         .name   = "temperature",
536         .fops   = &wdt_temp_fops,
537 };
538 #endif /* CONFIG_WDT_501 */
539
540 /*
541  *      The WDT card needs to learn about soft shutdowns in order to
542  *      turn the timebomb registers off.
543  */
544
545 static struct notifier_block wdt_notifier = {
546         .notifier_call = wdt_notify_sys,
547 };
548
549 /**
550  *      cleanup_module:
551  *
552  *      Unload the watchdog. You cannot do this with any file handles open.
553  *      If your watchdog is set to continue ticking on close and you unload
554  *      it, well it keeps ticking. We won't get the interrupt but the board
555  *      will not touch PC memory so all is fine. You just have to load a new
556  *      module in 60 seconds or reboot.
557  */
558
559 static void __exit wdt_exit(void)
560 {
561         misc_deregister(&wdt_miscdev);
562 #ifdef CONFIG_WDT_501
563         misc_deregister(&temp_miscdev);
564 #endif /* CONFIG_WDT_501 */
565         unregister_reboot_notifier(&wdt_notifier);
566         free_irq(irq, NULL);
567         release_region(io,8);
568 }
569
570 /**
571  *      wdt_init:
572  *
573  *      Set up the WDT watchdog board. All we have to do is grab the
574  *      resources we require and bitch if anyone beat us to them.
575  *      The open() function will actually kick the board off.
576  */
577
578 static int __init wdt_init(void)
579 {
580         int ret;
581
582         /* Check that the heartbeat value is within it's range ; if not reset to the default */
583         if (wdt_set_heartbeat(heartbeat)) {
584                 wdt_set_heartbeat(WD_TIMO);
585                 printk(KERN_INFO "wdt: heartbeat value must be 0<heartbeat<65536, using %d\n",
586                         WD_TIMO);
587         }
588
589         if (!request_region(io, 8, "wdt501p")) {
590                 printk(KERN_ERR "wdt: I/O address 0x%04x already in use\n", io);
591                 ret = -EBUSY;
592                 goto out;
593         }
594
595         ret = request_irq(irq, wdt_interrupt, SA_INTERRUPT, "wdt501p", NULL);
596         if(ret) {
597                 printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
598                 goto outreg;
599         }
600
601         ret = register_reboot_notifier(&wdt_notifier);
602         if(ret) {
603                 printk(KERN_ERR "wdt: cannot register reboot notifier (err=%d)\n", ret);
604                 goto outirq;
605         }
606
607 #ifdef CONFIG_WDT_501
608         ret = misc_register(&temp_miscdev);
609         if (ret) {
610                 printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
611                         TEMP_MINOR, ret);
612                 goto outrbt;
613         }
614 #endif /* CONFIG_WDT_501 */
615
616         ret = misc_register(&wdt_miscdev);
617         if (ret) {
618                 printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
619                         WATCHDOG_MINOR, ret);
620                 goto outmisc;
621         }
622
623         ret = 0;
624         printk(KERN_INFO "WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
625                 io, irq, heartbeat, nowayout);
626 #ifdef CONFIG_WDT_501
627         printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
628 #endif /* CONFIG_WDT_501 */
629
630 out:
631         return ret;
632
633 outmisc:
634 #ifdef CONFIG_WDT_501
635         misc_deregister(&temp_miscdev);
636 outrbt:
637 #endif /* CONFIG_WDT_501 */
638         unregister_reboot_notifier(&wdt_notifier);
639 outirq:
640         free_irq(irq, NULL);
641 outreg:
642         release_region(io,8);
643         goto out;
644 }
645
646 module_init(wdt_init);
647 module_exit(wdt_exit);
648
649 MODULE_AUTHOR("Alan Cox");
650 MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
651 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
652 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
653 MODULE_LICENSE("GPL");