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