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