Linux 2.4.0-test9pre1
[davej-history.git] / drivers / char / wdt.c
blob9f1940aff9b971607237af63a62534d2207aa8fb
1 /*
2 * Industrial Computer Source WDT500/501 driver for Linux 2.1.x
4 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
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.
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.
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
18 * Release 0.08.
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
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/smp_lock.h>
40 #include <linux/miscdevice.h>
41 #include <linux/watchdog.h>
42 #include "wd501p.h"
43 #include <linux/malloc.h>
44 #include <linux/ioport.h>
45 #include <linux/fcntl.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48 #include <asm/system.h>
49 #include <linux/notifier.h>
50 #include <linux/reboot.h>
51 #include <linux/init.h>
53 static int wdt_is_open;
56 * You must set these - there is no sane way to probe for this board.
57 * You can use wdt=x,y to set these now.
60 static int io=0x240;
61 static int irq=11;
63 #define WD_TIMO (100*60) /* 1 minute */
65 #ifndef MODULE
67 /**
68 * wdt_setup:
69 * @str: command line string
71 * Setup options. The board isn't really probe-able so we have to
72 * get the user to tell us the configuration. Sane people build it
73 * modular but the others come here.
76 static int __init wdt_setup(char *str)
78 int ints[4];
80 str = get_options (str, ARRAY_SIZE(ints), ints);
82 if (ints[0] > 0)
84 io = ints[1];
85 if(ints[0] > 1)
86 irq = ints[2];
89 return 1;
92 __setup("wdt=", wdt_setup);
94 #endif /* !MODULE */
96 MODULE_PARM(io, "i");
97 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
98 MODULE_PARM(irq, "i");
99 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
102 * Programming support
105 static void wdt_ctr_mode(int ctr, int mode)
107 ctr<<=6;
108 ctr|=0x30;
109 ctr|=(mode<<1);
110 outb_p(ctr, WDT_CR);
113 static void wdt_ctr_load(int ctr, int val)
115 outb_p(val&0xFF, WDT_COUNT0+ctr);
116 outb_p(val>>8, WDT_COUNT0+ctr);
120 * Kernel methods.
125 * wdt_status:
127 * Extract the status information from a WDT watchdog device. There are
128 * several board variants so we have to know which bits are valid. Some
129 * bits default to one and some to zero in order to be maximally painful.
131 * we then map the bits onto the status ioctl flags.
134 static int wdt_status(void)
137 * Status register to bit flags
140 int flag=0;
141 unsigned char status=inb_p(WDT_SR);
142 status|=FEATUREMAP1;
143 status&=~FEATUREMAP2;
145 if(!(status&WDC_SR_TGOOD))
146 flag|=WDIOF_OVERHEAT;
147 if(!(status&WDC_SR_PSUOVER))
148 flag|=WDIOF_POWEROVER;
149 if(!(status&WDC_SR_PSUUNDR))
150 flag|=WDIOF_POWERUNDER;
151 if(!(status&WDC_SR_FANGOOD))
152 flag|=WDIOF_FANFAULT;
153 if(status&WDC_SR_ISOI0)
154 flag|=WDIOF_EXTERN1;
155 if(status&WDC_SR_ISII1)
156 flag|=WDIOF_EXTERN2;
157 return flag;
161 * wdt_interrupt:
162 * @irq: Interrupt number
163 * @dev_id: Unused as we don't allow multiple devices.
164 * @regs: Unused.
166 * Handle an interrupt from the board. These are raised when the status
167 * map changes in what the board considers an interesting way. That means
168 * a failure condition occuring.
171 void wdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
174 * Read the status register see what is up and
175 * then printk it.
178 unsigned char status=inb_p(WDT_SR);
180 status|=FEATUREMAP1;
181 status&=~FEATUREMAP2;
183 printk(KERN_CRIT "WDT status %d\n", status);
185 if(!(status&WDC_SR_TGOOD))
186 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
187 if(!(status&WDC_SR_PSUOVER))
188 printk(KERN_CRIT "PSU over voltage.\n");
189 if(!(status&WDC_SR_PSUUNDR))
190 printk(KERN_CRIT "PSU under voltage.\n");
191 if(!(status&WDC_SR_FANGOOD))
192 printk(KERN_CRIT "Possible fan fault.\n");
193 if(!(status&WDC_SR_WCCR))
194 #ifdef SOFTWARE_REBOOT
195 #ifdef ONLY_TESTING
196 printk(KERN_CRIT "Would Reboot.\n");
197 #else
198 printk(KERN_CRIT "Initiating system reboot.\n");
199 machine_restart(NULL);
200 #endif
201 #else
202 printk(KERN_CRIT "Reset in 5ms.\n");
203 #endif
207 static long long wdt_llseek(struct file *file, long long offset, int origin)
209 return -ESPIPE;
213 * wdt_ping:
215 * Reload counter one with the watchdog timeout. We don't bother reloading
216 * the cascade counter.
219 static void wdt_ping(void)
221 /* Write a watchdog value */
222 inb_p(WDT_DC);
223 wdt_ctr_mode(1,2);
224 wdt_ctr_load(1,WD_TIMO); /* Timeout */
225 outb_p(0, WDT_DC);
229 * wdt_write:
230 * @file: file handle to the watchdog
231 * @buf: buffer to write (unused as data does not matter here
232 * @count: count of bytes
233 * @ppos: pointer to the position to write. No seeks allowed
235 * A write to a watchdog device is defined as a keepalive signal. Any
236 * write of data will do, as we we don't define content meaning.
239 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
241 /* Can't seek (pwrite) on this device */
242 if (ppos != &file->f_pos)
243 return -ESPIPE;
245 if(count)
247 wdt_ping();
248 return 1;
250 return 0;
254 * wdt_read:
255 * @file: file handle to the watchdog board
256 * @buf: buffer to write 1 byte into
257 * @count: length of buffer
258 * @ptr: offset (no seek allowed)
260 * Read reports the temperature in degrees Fahrenheit. The API is in
261 * farenheit. It was designed by an imperial measurement luddite.
264 static ssize_t wdt_read(struct file *file, char *buf, size_t count, loff_t *ptr)
266 unsigned short c=inb_p(WDT_RT);
267 unsigned char cp;
269 /* Can't seek (pread) on this device */
270 if (ptr != &file->f_pos)
271 return -ESPIPE;
273 switch(MINOR(file->f_dentry->d_inode->i_rdev))
275 case TEMP_MINOR:
276 c*=11;
277 c/=15;
278 cp=c+7;
279 if(copy_to_user(buf,&cp,1))
280 return -EFAULT;
281 return 1;
282 default:
283 return -EINVAL;
288 * wdt_ioctl:
289 * @inode: inode of the device
290 * @file: file handle to the device
291 * @cmd: watchdog command
292 * @arg: argument pointer
294 * The watchdog API defines a common set of functions for all watchdogs
295 * according to their available features. We only actually usefully support
296 * querying capabilities and current status.
299 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
300 unsigned long arg)
302 static struct watchdog_info ident=
304 WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
305 |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT,
307 "WDT500/501"
310 ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
311 switch(cmd)
313 default:
314 return -ENOIOCTLCMD;
315 case WDIOC_GETSUPPORT:
316 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
318 case WDIOC_GETSTATUS:
319 return put_user(wdt_status(),(int *)arg);
320 case WDIOC_GETBOOTSTATUS:
321 return put_user(0, (int *)arg);
322 case WDIOC_KEEPALIVE:
323 wdt_ping();
324 return 0;
329 * wdt_open:
330 * @inode: inode of device
331 * @file: file handle to device
333 * One of our two misc devices has been opened. The watchdog device is
334 * single open and on opening we load the counters. Counter zero is a
335 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
336 * counter triggers counter 2 downcounts the length of the reset pulse
337 * which set set to be as long as possible.
340 static int wdt_open(struct inode *inode, struct file *file)
342 switch(MINOR(inode->i_rdev))
344 case WATCHDOG_MINOR:
345 if(wdt_is_open)
346 return -EBUSY;
348 * Activate
351 wdt_is_open=1;
352 inb_p(WDT_DC); /* Disable */
353 wdt_ctr_mode(0,3);
354 wdt_ctr_mode(1,2);
355 wdt_ctr_mode(2,0);
356 wdt_ctr_load(0, 8948); /* count at 100Hz */
357 wdt_ctr_load(1,WD_TIMO); /* Timeout 120 seconds */
358 wdt_ctr_load(2,65535);
359 outb_p(0, WDT_DC); /* Enable */
360 return 0;
361 case TEMP_MINOR:
362 return 0;
363 default:
364 return -ENODEV;
369 * wdt_close:
370 * @inode: inode to board
371 * @file: file handle to board
373 * The watchdog has a configurable API. There is a religious dispute
374 * between people who want their watchdog to be able to shut down and
375 * those who want to be sure if the watchdog manager dies the machine
376 * reboots. In the former case we disable the counters, in the latter
377 * case you have to open it again very soon.
380 static int wdt_release(struct inode *inode, struct file *file)
382 lock_kernel();
383 if(MINOR(inode->i_rdev)==WATCHDOG_MINOR)
385 #ifndef CONFIG_WATCHDOG_NOWAYOUT
386 inb_p(WDT_DC); /* Disable counters */
387 wdt_ctr_load(2,0); /* 0 length reset pulses now */
388 #endif
389 wdt_is_open=0;
391 unlock_kernel();
392 return 0;
396 * notify_sys:
397 * @this: our notifier block
398 * @code: the event being reported
399 * @unused: unused
401 * Our notifier is called on system shutdowns. We want to turn the card
402 * off at reboot otherwise the machine will reboot again during memory
403 * test or worse yet during the following fsck. This would suck, in fact
404 * trust me - if it happens it does suck.
407 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
408 void *unused)
410 if(code==SYS_DOWN || code==SYS_HALT)
412 /* Turn the card off */
413 inb_p(WDT_DC);
414 wdt_ctr_load(2,0);
416 return NOTIFY_DONE;
420 * Kernel Interfaces
424 static struct file_operations wdt_fops = {
425 owner: THIS_MODULE,
426 llseek: wdt_llseek,
427 read: wdt_read,
428 write: wdt_write,
429 ioctl: wdt_ioctl,
430 open: wdt_open,
431 release: wdt_release,
434 static struct miscdevice wdt_miscdev=
436 WATCHDOG_MINOR,
437 "watchdog",
438 &wdt_fops
441 #ifdef CONFIG_WDT_501
442 static struct miscdevice temp_miscdev=
444 TEMP_MINOR,
445 "temperature",
446 &wdt_fops
448 #endif
451 * The WDT card needs to learn about soft shutdowns in order to
452 * turn the timebomb registers off.
455 static struct notifier_block wdt_notifier=
457 wdt_notify_sys,
458 NULL,
462 #ifdef MODULE
464 #define wdt_init init_module
467 * cleanup_module:
469 * Unload the watchdog. You cannot do this with any file handles open.
470 * If your watchdog is set to continue ticking on close and you unload
471 * it, well it keeps ticking. We won't get the interrupt but the board
472 * will not touch PC memory so all is fine. You just have to load a new
473 * module in 60 seconds or reboot.
476 void cleanup_module(void)
478 misc_deregister(&wdt_miscdev);
479 #ifdef CONFIG_WDT_501
480 misc_deregister(&temp_miscdev);
481 #endif
482 unregister_reboot_notifier(&wdt_notifier);
483 release_region(io,8);
484 free_irq(irq, NULL);
487 #endif
490 * wdt_init:
492 * Set up the WDT watchdog board. All we have to do is grab the
493 * resources we require and bitch if anyone beat us to them.
494 * The open() function will actually kick the board off.
497 int __init wdt_init(void)
499 int ret;
501 ret = misc_register(&wdt_miscdev);
502 if (ret) {
503 printk(KERN_ERR "wdt: can't misc_register on minor=%d\n", WATCHDOG_MINOR);
504 goto out;
506 ret = request_irq(irq, wdt_interrupt, SA_INTERRUPT, "wdt501p", NULL);
507 if(ret) {
508 printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
509 goto outmisc;
511 if (!request_region(io, 8, "wdt501p")) {
512 printk(KERN_ERR "wdt: IO %X is not free.\n", io);
513 ret = -EBUSY;
514 goto outirq;
516 ret = register_reboot_notifier(&wdt_notifier);
517 if(ret) {
518 printk(KERN_ERR "wdt: can't register reboot notifier (err=%d)\n", ret);
519 goto outreg;
522 #ifdef CONFIG_WDT_501
523 ret = misc_register(&temp_miscdev);
524 if (ret) {
525 printk(KERN_ERR "wdt: can't misc_register (temp) on minor=%d\n", TEMP_MINOR);
526 goto outrbt;
528 #endif
530 ret = 0;
531 printk(KERN_INFO "WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io, irq);
532 out:
533 return ret;
535 #ifdef CONFIG_WDT_501
536 outrbt:
537 unregister_reboot_notifier(&wdt_notifier);
538 #endif
540 outreg:
541 release_region(io,8);
542 outirq:
543 free_irq(irq, NULL);
544 outmisc:
545 misc_deregister(&wdt_miscdev);
546 goto out;