Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / wdt.c
blob3ac86bf3ae5075054c0b61032f4503e4eb14adce
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/miscdevice.h>
39 #include <linux/watchdog.h>
40 #include "wd501p.h"
41 #include <linux/malloc.h>
42 #include <linux/ioport.h>
43 #include <linux/fcntl.h>
44 #include <asm/io.h>
45 #include <asm/uaccess.h>
46 #include <asm/system.h>
47 #include <linux/notifier.h>
48 #include <linux/reboot.h>
49 #include <linux/init.h>
51 static int wdt_is_open=0;
54 * You must set these - there is no sane way to probe for this board.
55 * You can use wdt=x,y to set these now.
58 static int io=0x240;
59 static int irq=11;
61 #define WD_TIMO (100*60) /* 1 minute */
63 #ifndef MODULE
65 /**
66 * wdt_setup:
67 * @str: command line string
69 * Setup options. The board isn't really probe-able so we have to
70 * get the user to tell us the configuration. Sane people build it
71 * modular but the others come here.
74 static int __init wdt_setup(char *str)
76 int ints[4];
78 str = get_options (str, ARRAY_SIZE(ints), ints);
80 if (ints[0] > 0)
82 io = ints[1];
83 if(ints[0] > 1)
84 irq = ints[2];
87 return 1;
90 __setup("wdt=", wdt_setup);
92 #endif /* !MODULE */
95 * Programming support
98 static void wdt_ctr_mode(int ctr, int mode)
100 ctr<<=6;
101 ctr|=0x30;
102 ctr|=(mode<<1);
103 outb_p(ctr, WDT_CR);
106 static void wdt_ctr_load(int ctr, int val)
108 outb_p(val&0xFF, WDT_COUNT0+ctr);
109 outb_p(val>>8, WDT_COUNT0+ctr);
113 * Kernel methods.
118 * wdt_status:
120 * Extract the status information from a WDT watchdog device. There are
121 * several board variants so we have to know which bits are valid. Some
122 * bits default to one and some to zero in order to be maximally painful.
124 * we then map the bits onto the status ioctl flags.
127 static int wdt_status(void)
130 * Status register to bit flags
133 int flag=0;
134 unsigned char status=inb_p(WDT_SR);
135 status|=FEATUREMAP1;
136 status&=~FEATUREMAP2;
138 if(!(status&WDC_SR_TGOOD))
139 flag|=WDIOF_OVERHEAT;
140 if(!(status&WDC_SR_PSUOVER))
141 flag|=WDIOF_POWEROVER;
142 if(!(status&WDC_SR_PSUUNDR))
143 flag|=WDIOF_POWERUNDER;
144 if(!(status&WDC_SR_FANGOOD))
145 flag|=WDIOF_FANFAULT;
146 if(status&WDC_SR_ISOI0)
147 flag|=WDIOF_EXTERN1;
148 if(status&WDC_SR_ISII1)
149 flag|=WDIOF_EXTERN2;
150 return flag;
154 * wdt_interrupt:
155 * @irq: Interrupt number
156 * @dev_id: Unused as we don't allow multiple devices.
157 * @regs: Unused.
159 * Handle an interrupt from the board. These are raised when the status
160 * map changes in what the board considers an interesting way. That means
161 * a failure condition occuring.
164 void wdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
167 * Read the status register see what is up and
168 * then printk it.
171 unsigned char status=inb_p(WDT_SR);
173 status|=FEATUREMAP1;
174 status&=~FEATUREMAP2;
176 printk(KERN_CRIT "WDT status %d\n", status);
178 if(!(status&WDC_SR_TGOOD))
179 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
180 if(!(status&WDC_SR_PSUOVER))
181 printk(KERN_CRIT "PSU over voltage.\n");
182 if(!(status&WDC_SR_PSUUNDR))
183 printk(KERN_CRIT "PSU under voltage.\n");
184 if(!(status&WDC_SR_FANGOOD))
185 printk(KERN_CRIT "Possible fan fault.\n");
186 if(!(status&WDC_SR_WCCR))
187 #ifdef SOFTWARE_REBOOT
188 #ifdef ONLY_TESTING
189 printk(KERN_CRIT "Would Reboot.\n");
190 #else
191 printk(KERN_CRIT "Initiating system reboot.\n");
192 machine_restart(NULL);
193 #endif
194 #else
195 printk(KERN_CRIT "Reset in 5ms.\n");
196 #endif
200 static long long wdt_llseek(struct file *file, long long offset, int origin)
202 return -ESPIPE;
206 * wdt_ping:
208 * Reload counter one with the watchdog timeout. We don't bother reloading
209 * the cascade counter.
212 static void wdt_ping(void)
214 /* Write a watchdog value */
215 inb_p(WDT_DC);
216 wdt_ctr_mode(1,2);
217 wdt_ctr_load(1,WD_TIMO); /* Timeout */
218 outb_p(0, WDT_DC);
222 * wdt_write:
223 * @file: file handle to the watchdog
224 * @buf: buffer to write (unused as data does not matter here
225 * @count: count of bytes
226 * @ppos: pointer to the position to write. No seeks allowed
228 * A write to a watchdog device is defined as a keepalive signal. Any
229 * write of data will do, as we we don't define content meaning.
232 static ssize_t wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
234 /* Can't seek (pwrite) on this device */
235 if (ppos != &file->f_pos)
236 return -ESPIPE;
238 if(count)
240 wdt_ping();
241 return 1;
243 return 0;
247 * wdt_read:
248 * @file: file handle to the watchdog board
249 * @buf: buffer to write 1 byte into
250 * @count: length of buffer
251 * @ptr: offset (no seek allowed)
253 * Read reports the temperature in degrees Fahrenheit. The API is in
254 * farenheit. It was designed by an imperial measurement luddite.
257 static ssize_t wdt_read(struct file *file, char *buf, size_t count, loff_t *ptr)
259 unsigned short c=inb_p(WDT_RT);
260 unsigned char cp;
262 /* Can't seek (pread) on this device */
263 if (ptr != &file->f_pos)
264 return -ESPIPE;
266 switch(MINOR(file->f_dentry->d_inode->i_rdev))
268 case TEMP_MINOR:
269 c*=11;
270 c/=15;
271 cp=c+7;
272 if(copy_to_user(buf,&cp,1))
273 return -EFAULT;
274 return 1;
275 default:
276 return -EINVAL;
281 * wdt_ioctl:
282 * @inode: inode of the device
283 * @file: file handle to the device
284 * @cmd: watchdog command
285 * @arg: argument pointer
287 * The watchdog API defines a common set of functions for all watchdogs
288 * according to their available features. We only actually usefully support
289 * querying capabilities and current status.
292 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
293 unsigned long arg)
295 static struct watchdog_info ident=
297 WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
298 |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT,
300 "WDT500/501"
303 ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
304 switch(cmd)
306 default:
307 return -ENOIOCTLCMD;
308 case WDIOC_GETSUPPORT:
309 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
311 case WDIOC_GETSTATUS:
312 return put_user(wdt_status(),(int *)arg);
313 case WDIOC_GETBOOTSTATUS:
314 return put_user(0, (int *)arg);
315 case WDIOC_KEEPALIVE:
316 wdt_ping();
317 return 0;
322 * wdt_open:
323 * @inode: inode of device
324 * @file: file handle to device
326 * One of our two misc devices has been opened. The watchdog device is
327 * single open and on opening we load the counters. Counter zero is a
328 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
329 * counter triggers counter 2 downcounts the length of the reset pulse
330 * which set set to be as long as possible.
333 static int wdt_open(struct inode *inode, struct file *file)
335 switch(MINOR(inode->i_rdev))
337 case WATCHDOG_MINOR:
338 if(wdt_is_open)
339 return -EBUSY;
341 * Activate
344 wdt_is_open=1;
345 inb_p(WDT_DC); /* Disable */
346 wdt_ctr_mode(0,3);
347 wdt_ctr_mode(1,2);
348 wdt_ctr_mode(2,0);
349 wdt_ctr_load(0, 8948); /* count at 100Hz */
350 wdt_ctr_load(1,WD_TIMO); /* Timeout 120 seconds */
351 wdt_ctr_load(2,65535);
352 outb_p(0, WDT_DC); /* Enable */
353 return 0;
354 case TEMP_MINOR:
355 return 0;
356 default:
357 return -ENODEV;
362 * wdt_close:
363 * @inode: inode to board
364 * @file: file handle to board
366 * The watchdog has a configurable API. There is a religious dispute
367 * between people who want their watchdog to be able to shut down and
368 * those who want to be sure if the watchdog manager dies the machine
369 * reboots. In the former case we disable the counters, in the latter
370 * case you have to open it again very soon.
373 static int wdt_release(struct inode *inode, struct file *file)
375 if(MINOR(inode->i_rdev)==WATCHDOG_MINOR)
377 #ifndef CONFIG_WATCHDOG_NOWAYOUT
378 inb_p(WDT_DC); /* Disable counters */
379 wdt_ctr_load(2,0); /* 0 length reset pulses now */
380 #endif
381 wdt_is_open=0;
383 return 0;
387 * notify_sys:
388 * @this: our notifier block
389 * @code: the event being reported
390 * @unused: unused
392 * Our notifier is called on system shutdowns. We want to turn the card
393 * off at reboot otherwise the machine will reboot again during memory
394 * test or worse yet during the following fsck. This would suck, in fact
395 * trust me - if it happens it does suck.
398 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
399 void *unused)
401 if(code==SYS_DOWN || code==SYS_HALT)
403 /* Turn the card off */
404 inb_p(WDT_DC);
405 wdt_ctr_load(2,0);
407 return NOTIFY_DONE;
411 * Kernel Interfaces
415 static struct file_operations wdt_fops = {
416 owner: THIS_MODULE,
417 llseek: wdt_llseek,
418 read: wdt_read,
419 write: wdt_write,
420 ioctl: wdt_ioctl,
421 open: wdt_open,
422 release: wdt_release,
425 static struct miscdevice wdt_miscdev=
427 WATCHDOG_MINOR,
428 "watchdog",
429 &wdt_fops
432 #ifdef CONFIG_WDT_501
433 static struct miscdevice temp_miscdev=
435 TEMP_MINOR,
436 "temperature",
437 &wdt_fops
439 #endif
442 * The WDT card needs to learn about soft shutdowns in order to
443 * turn the timebomb registers off.
446 static struct notifier_block wdt_notifier=
448 wdt_notify_sys,
449 NULL,
453 #ifdef MODULE
455 #define wdt_init init_module
458 * cleanup_module:
460 * Unload the watchdog. You cannot do this with any file handles open.
461 * If your watchdog is set to continue ticking on close and you unload
462 * it, well it keeps ticking. We won't get the interrupt but the board
463 * will not touch PC memory so all is fine. You just have to load a new
464 * module in 60 seconds or reboot.
467 void cleanup_module(void)
469 misc_deregister(&wdt_miscdev);
470 #ifdef CONFIG_WDT_501
471 misc_deregister(&temp_miscdev);
472 #endif
473 unregister_reboot_notifier(&wdt_notifier);
474 release_region(io,8);
475 free_irq(irq, NULL);
478 #endif
481 * wdt_init:
483 * Set up the WDT watchdog board. All we have to do is grab the
484 * resources we require and bitch if anyone beat us to them.
485 * The open() function will actually kick the board off.
488 int __init wdt_init(void)
490 printk(KERN_INFO "WDT500/501-P driver 0.07 at %X (Interrupt %d)\n", io,irq);
491 if(request_irq(irq, wdt_interrupt, SA_INTERRUPT, "wdt501p", &wdt_miscdev))
493 printk(KERN_ERR "IRQ %d is not free.\n", irq);
494 return -EIO;
496 misc_register(&wdt_miscdev);
497 #ifdef CONFIG_WDT_501
498 misc_register(&temp_miscdev);
499 #endif
500 request_region(io, 8, "wdt501p");
501 register_reboot_notifier(&wdt_notifier);
502 return 0;