Kill st_fstype member.
[linux-2.6/linux-mips.git] / drivers / char / wdt.c
blobe55d94fe3d231e77d792ea9bd521b693cf5d49ca
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
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/smp_lock.h>
39 #include <linux/miscdevice.h>
40 #include <linux/watchdog.h>
41 #include "wd501p.h"
42 #include <linux/malloc.h>
43 #include <linux/ioport.h>
44 #include <linux/fcntl.h>
45 #include <asm/io.h>
46 #include <asm/uaccess.h>
47 #include <asm/system.h>
48 #include <linux/notifier.h>
49 #include <linux/reboot.h>
50 #include <linux/init.h>
52 static int wdt_is_open=0;
55 * You must set these - there is no sane way to probe for this board.
56 * You can use wdt=x,y to set these now.
59 static int io=0x240;
60 static int irq=11;
62 #define WD_TIMO (100*60) /* 1 minute */
64 #ifndef MODULE
66 /**
67 * wdt_setup:
68 * @str: command line string
70 * Setup options. The board isn't really probe-able so we have to
71 * get the user to tell us the configuration. Sane people build it
72 * modular but the others come here.
75 static int __init wdt_setup(char *str)
77 int ints[4];
79 str = get_options (str, ARRAY_SIZE(ints), ints);
81 if (ints[0] > 0)
83 io = ints[1];
84 if(ints[0] > 1)
85 irq = ints[2];
88 return 1;
91 __setup("wdt=", wdt_setup);
93 #endif /* !MODULE */
95 MODULE_PARM(io, "i");
96 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
97 MODULE_PARM(irq, "i");
98 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
101 * Programming support
104 static void wdt_ctr_mode(int ctr, int mode)
106 ctr<<=6;
107 ctr|=0x30;
108 ctr|=(mode<<1);
109 outb_p(ctr, WDT_CR);
112 static void wdt_ctr_load(int ctr, int val)
114 outb_p(val&0xFF, WDT_COUNT0+ctr);
115 outb_p(val>>8, WDT_COUNT0+ctr);
119 * Kernel methods.
124 * wdt_status:
126 * Extract the status information from a WDT watchdog device. There are
127 * several board variants so we have to know which bits are valid. Some
128 * bits default to one and some to zero in order to be maximally painful.
130 * we then map the bits onto the status ioctl flags.
133 static int wdt_status(void)
136 * Status register to bit flags
139 int flag=0;
140 unsigned char status=inb_p(WDT_SR);
141 status|=FEATUREMAP1;
142 status&=~FEATUREMAP2;
144 if(!(status&WDC_SR_TGOOD))
145 flag|=WDIOF_OVERHEAT;
146 if(!(status&WDC_SR_PSUOVER))
147 flag|=WDIOF_POWEROVER;
148 if(!(status&WDC_SR_PSUUNDR))
149 flag|=WDIOF_POWERUNDER;
150 if(!(status&WDC_SR_FANGOOD))
151 flag|=WDIOF_FANFAULT;
152 if(status&WDC_SR_ISOI0)
153 flag|=WDIOF_EXTERN1;
154 if(status&WDC_SR_ISII1)
155 flag|=WDIOF_EXTERN2;
156 return flag;
160 * wdt_interrupt:
161 * @irq: Interrupt number
162 * @dev_id: Unused as we don't allow multiple devices.
163 * @regs: Unused.
165 * Handle an interrupt from the board. These are raised when the status
166 * map changes in what the board considers an interesting way. That means
167 * a failure condition occuring.
170 void wdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
173 * Read the status register see what is up and
174 * then printk it.
177 unsigned char status=inb_p(WDT_SR);
179 status|=FEATUREMAP1;
180 status&=~FEATUREMAP2;
182 printk(KERN_CRIT "WDT status %d\n", status);
184 if(!(status&WDC_SR_TGOOD))
185 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
186 if(!(status&WDC_SR_PSUOVER))
187 printk(KERN_CRIT "PSU over voltage.\n");
188 if(!(status&WDC_SR_PSUUNDR))
189 printk(KERN_CRIT "PSU under voltage.\n");
190 if(!(status&WDC_SR_FANGOOD))
191 printk(KERN_CRIT "Possible fan fault.\n");
192 if(!(status&WDC_SR_WCCR))
193 #ifdef SOFTWARE_REBOOT
194 #ifdef ONLY_TESTING
195 printk(KERN_CRIT "Would Reboot.\n");
196 #else
197 printk(KERN_CRIT "Initiating system reboot.\n");
198 machine_restart(NULL);
199 #endif
200 #else
201 printk(KERN_CRIT "Reset in 5ms.\n");
202 #endif
206 static long long wdt_llseek(struct file *file, long long offset, int origin)
208 return -ESPIPE;
212 * wdt_ping:
214 * Reload counter one with the watchdog timeout. We don't bother reloading
215 * the cascade counter.
218 static void wdt_ping(void)
220 /* Write a watchdog value */
221 inb_p(WDT_DC);
222 wdt_ctr_mode(1,2);
223 wdt_ctr_load(1,WD_TIMO); /* Timeout */
224 outb_p(0, WDT_DC);
228 * wdt_write:
229 * @file: file handle to the watchdog
230 * @buf: buffer to write (unused as data does not matter here
231 * @count: count of bytes
232 * @ppos: pointer to the position to write. No seeks allowed
234 * A write to a watchdog device is defined as a keepalive signal. Any
235 * write of data will do, as we we don't define content meaning.
238 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
240 /* Can't seek (pwrite) on this device */
241 if (ppos != &file->f_pos)
242 return -ESPIPE;
244 if(count)
246 wdt_ping();
247 return 1;
249 return 0;
253 * wdt_read:
254 * @file: file handle to the watchdog board
255 * @buf: buffer to write 1 byte into
256 * @count: length of buffer
257 * @ptr: offset (no seek allowed)
259 * Read reports the temperature in degrees Fahrenheit. The API is in
260 * farenheit. It was designed by an imperial measurement luddite.
263 static ssize_t wdt_read(struct file *file, char *buf, size_t count, loff_t *ptr)
265 unsigned short c=inb_p(WDT_RT);
266 unsigned char cp;
268 /* Can't seek (pread) on this device */
269 if (ptr != &file->f_pos)
270 return -ESPIPE;
272 switch(MINOR(file->f_dentry->d_inode->i_rdev))
274 case TEMP_MINOR:
275 c*=11;
276 c/=15;
277 cp=c+7;
278 if(copy_to_user(buf,&cp,1))
279 return -EFAULT;
280 return 1;
281 default:
282 return -EINVAL;
287 * wdt_ioctl:
288 * @inode: inode of the device
289 * @file: file handle to the device
290 * @cmd: watchdog command
291 * @arg: argument pointer
293 * The watchdog API defines a common set of functions for all watchdogs
294 * according to their available features. We only actually usefully support
295 * querying capabilities and current status.
298 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
299 unsigned long arg)
301 static struct watchdog_info ident=
303 WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
304 |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT,
306 "WDT500/501"
309 ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
310 switch(cmd)
312 default:
313 return -ENOIOCTLCMD;
314 case WDIOC_GETSUPPORT:
315 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
317 case WDIOC_GETSTATUS:
318 return put_user(wdt_status(),(int *)arg);
319 case WDIOC_GETBOOTSTATUS:
320 return put_user(0, (int *)arg);
321 case WDIOC_KEEPALIVE:
322 wdt_ping();
323 return 0;
328 * wdt_open:
329 * @inode: inode of device
330 * @file: file handle to device
332 * One of our two misc devices has been opened. The watchdog device is
333 * single open and on opening we load the counters. Counter zero is a
334 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
335 * counter triggers counter 2 downcounts the length of the reset pulse
336 * which set set to be as long as possible.
339 static int wdt_open(struct inode *inode, struct file *file)
341 switch(MINOR(inode->i_rdev))
343 case WATCHDOG_MINOR:
344 if(wdt_is_open)
345 return -EBUSY;
347 * Activate
350 wdt_is_open=1;
351 inb_p(WDT_DC); /* Disable */
352 wdt_ctr_mode(0,3);
353 wdt_ctr_mode(1,2);
354 wdt_ctr_mode(2,0);
355 wdt_ctr_load(0, 8948); /* count at 100Hz */
356 wdt_ctr_load(1,WD_TIMO); /* Timeout 120 seconds */
357 wdt_ctr_load(2,65535);
358 outb_p(0, WDT_DC); /* Enable */
359 return 0;
360 case TEMP_MINOR:
361 return 0;
362 default:
363 return -ENODEV;
368 * wdt_close:
369 * @inode: inode to board
370 * @file: file handle to board
372 * The watchdog has a configurable API. There is a religious dispute
373 * between people who want their watchdog to be able to shut down and
374 * those who want to be sure if the watchdog manager dies the machine
375 * reboots. In the former case we disable the counters, in the latter
376 * case you have to open it again very soon.
379 static int wdt_release(struct inode *inode, struct file *file)
381 lock_kernel();
382 if(MINOR(inode->i_rdev)==WATCHDOG_MINOR)
384 #ifndef CONFIG_WATCHDOG_NOWAYOUT
385 inb_p(WDT_DC); /* Disable counters */
386 wdt_ctr_load(2,0); /* 0 length reset pulses now */
387 #endif
388 wdt_is_open=0;
390 unlock_kernel();
391 return 0;
395 * notify_sys:
396 * @this: our notifier block
397 * @code: the event being reported
398 * @unused: unused
400 * Our notifier is called on system shutdowns. We want to turn the card
401 * off at reboot otherwise the machine will reboot again during memory
402 * test or worse yet during the following fsck. This would suck, in fact
403 * trust me - if it happens it does suck.
406 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
407 void *unused)
409 if(code==SYS_DOWN || code==SYS_HALT)
411 /* Turn the card off */
412 inb_p(WDT_DC);
413 wdt_ctr_load(2,0);
415 return NOTIFY_DONE;
419 * Kernel Interfaces
423 static struct file_operations wdt_fops = {
424 owner: THIS_MODULE,
425 llseek: wdt_llseek,
426 read: wdt_read,
427 write: wdt_write,
428 ioctl: wdt_ioctl,
429 open: wdt_open,
430 release: wdt_release,
433 static struct miscdevice wdt_miscdev=
435 WATCHDOG_MINOR,
436 "watchdog",
437 &wdt_fops
440 #ifdef CONFIG_WDT_501
441 static struct miscdevice temp_miscdev=
443 TEMP_MINOR,
444 "temperature",
445 &wdt_fops
447 #endif
450 * The WDT card needs to learn about soft shutdowns in order to
451 * turn the timebomb registers off.
454 static struct notifier_block wdt_notifier=
456 wdt_notify_sys,
457 NULL,
461 #ifdef MODULE
463 #define wdt_init init_module
466 * cleanup_module:
468 * Unload the watchdog. You cannot do this with any file handles open.
469 * If your watchdog is set to continue ticking on close and you unload
470 * it, well it keeps ticking. We won't get the interrupt but the board
471 * will not touch PC memory so all is fine. You just have to load a new
472 * module in 60 seconds or reboot.
475 void cleanup_module(void)
477 misc_deregister(&wdt_miscdev);
478 #ifdef CONFIG_WDT_501
479 misc_deregister(&temp_miscdev);
480 #endif
481 unregister_reboot_notifier(&wdt_notifier);
482 release_region(io,8);
483 free_irq(irq, NULL);
486 #endif
489 * wdt_init:
491 * Set up the WDT watchdog board. All we have to do is grab the
492 * resources we require and bitch if anyone beat us to them.
493 * The open() function will actually kick the board off.
496 int __init wdt_init(void)
498 printk(KERN_INFO "WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io,irq);
499 if(request_irq(irq, wdt_interrupt, SA_INTERRUPT, "wdt501p", &wdt_miscdev))
501 printk(KERN_ERR "IRQ %d is not free.\n", irq);
502 return -EIO;
504 misc_register(&wdt_miscdev);
505 #ifdef CONFIG_WDT_501
506 misc_register(&temp_miscdev);
507 #endif
508 request_region(io, 8, "wdt501p");
509 register_reboot_notifier(&wdt_notifier);
510 return 0;