Remove all #inclusions of asm/system.h
[linux-2.6.git] / drivers / watchdog / w83877f_wdt.c
blob5f3b9455f294b994151ad60c89c1fa29f4c4afa0
1 /*
2 * W83877F Computer Watchdog Timer driver
4 * Based on acquirewdt.c by Alan Cox,
5 * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
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 * The authors do NOT admit liability nor provide warranty for
13 * any of this software. This material is provided "AS-IS" in
14 * the hope that it may be useful for others.
16 * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
18 * 4/19 - 2001 [Initial revision]
19 * 9/27 - 2001 Added spinlocking
20 * 4/12 - 2002 [rob@osinvestor.com] Eliminate extra comments
21 * Eliminate fop_read
22 * Eliminate extra spin_unlock
23 * Added KERN_* tags to printks
24 * add CONFIG_WATCHDOG_NOWAYOUT support
25 * fix possible wdt_is_open race
26 * changed watchdog_info to correctly reflect what
27 * the driver offers
28 * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
29 * WDIOC_SETTIMEOUT,
30 * WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls
31 * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
32 * added extra printk's for startup problems
33 * use module_param
34 * made timeout (the emulated heartbeat) a
35 * module_param
36 * made the keepalive ping an internal subroutine
38 * This WDT driver is different from most other Linux WDT
39 * drivers in that the driver will ping the watchdog by itself,
40 * because this particular WDT has a very short timeout (1.6
41 * seconds) and it would be insane to count on any userspace
42 * daemon always getting scheduled within that time frame.
45 #include <linux/module.h>
46 #include <linux/moduleparam.h>
47 #include <linux/types.h>
48 #include <linux/timer.h>
49 #include <linux/jiffies.h>
50 #include <linux/miscdevice.h>
51 #include <linux/watchdog.h>
52 #include <linux/fs.h>
53 #include <linux/ioport.h>
54 #include <linux/notifier.h>
55 #include <linux/reboot.h>
56 #include <linux/init.h>
57 #include <linux/io.h>
58 #include <linux/uaccess.h>
60 #define OUR_NAME "w83877f_wdt"
61 #define PFX OUR_NAME ": "
63 #define ENABLE_W83877F_PORT 0x3F0
64 #define ENABLE_W83877F 0x87
65 #define DISABLE_W83877F 0xAA
66 #define WDT_PING 0x443
67 #define WDT_REGISTER 0x14
68 #define WDT_ENABLE 0x9C
69 #define WDT_DISABLE 0x8C
72 * The W83877F seems to be fixed at 1.6s timeout (at least on the
73 * EMACS PC-104 board I'm using). If we reset the watchdog every
74 * ~250ms we should be safe. */
76 #define WDT_INTERVAL (HZ/4+1)
79 * We must not require too good response from the userspace daemon.
80 * Here we require the userspace daemon to send us a heartbeat
81 * char to /dev/watchdog every 30 seconds.
84 #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
85 /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
86 static int timeout = WATCHDOG_TIMEOUT;
87 module_param(timeout, int, 0);
88 MODULE_PARM_DESC(timeout,
89 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
90 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
93 static int nowayout = WATCHDOG_NOWAYOUT;
94 module_param(nowayout, int, 0);
95 MODULE_PARM_DESC(nowayout,
96 "Watchdog cannot be stopped once started (default="
97 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
99 static void wdt_timer_ping(unsigned long);
100 static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
101 static unsigned long next_heartbeat;
102 static unsigned long wdt_is_open;
103 static char wdt_expect_close;
104 static DEFINE_SPINLOCK(wdt_spinlock);
107 * Whack the dog
110 static void wdt_timer_ping(unsigned long data)
112 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
113 * we agree to ping the WDT
115 if (time_before(jiffies, next_heartbeat)) {
116 /* Ping the WDT */
117 spin_lock(&wdt_spinlock);
119 /* Ping the WDT by reading from WDT_PING */
120 inb_p(WDT_PING);
122 /* Re-set the timer interval */
123 mod_timer(&timer, jiffies + WDT_INTERVAL);
125 spin_unlock(&wdt_spinlock);
127 } else
128 printk(KERN_WARNING PFX
129 "Heartbeat lost! Will not ping the watchdog\n");
133 * Utility routines
136 static void wdt_change(int writeval)
138 unsigned long flags;
139 spin_lock_irqsave(&wdt_spinlock, flags);
141 /* buy some time */
142 inb_p(WDT_PING);
144 /* make W83877F available */
145 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
146 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
148 /* enable watchdog */
149 outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
150 outb_p(writeval, ENABLE_W83877F_PORT+1);
152 /* lock the W8387FF away */
153 outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
155 spin_unlock_irqrestore(&wdt_spinlock, flags);
158 static void wdt_startup(void)
160 next_heartbeat = jiffies + (timeout * HZ);
162 /* Start the timer */
163 mod_timer(&timer, jiffies + WDT_INTERVAL);
165 wdt_change(WDT_ENABLE);
167 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
170 static void wdt_turnoff(void)
172 /* Stop the timer */
173 del_timer(&timer);
175 wdt_change(WDT_DISABLE);
177 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
180 static void wdt_keepalive(void)
182 /* user land ping */
183 next_heartbeat = jiffies + (timeout * HZ);
187 * /dev/watchdog handling
190 static ssize_t fop_write(struct file *file, const char __user *buf,
191 size_t count, loff_t *ppos)
193 /* See if we got the magic character 'V' and reload the timer */
194 if (count) {
195 if (!nowayout) {
196 size_t ofs;
198 /* note: just in case someone wrote the magic
199 character five months ago... */
200 wdt_expect_close = 0;
202 /* scan to see whether or not we got the
203 magic character */
204 for (ofs = 0; ofs != count; ofs++) {
205 char c;
206 if (get_user(c, buf + ofs))
207 return -EFAULT;
208 if (c == 'V')
209 wdt_expect_close = 42;
213 /* someone wrote to us, we should restart timer */
214 wdt_keepalive();
216 return count;
219 static int fop_open(struct inode *inode, struct file *file)
221 /* Just in case we're already talking to someone... */
222 if (test_and_set_bit(0, &wdt_is_open))
223 return -EBUSY;
225 /* Good, fire up the show */
226 wdt_startup();
227 return nonseekable_open(inode, file);
230 static int fop_close(struct inode *inode, struct file *file)
232 if (wdt_expect_close == 42)
233 wdt_turnoff();
234 else {
235 del_timer(&timer);
236 printk(KERN_CRIT PFX
237 "device file closed unexpectedly. Will not stop the WDT!\n");
239 clear_bit(0, &wdt_is_open);
240 wdt_expect_close = 0;
241 return 0;
244 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
246 void __user *argp = (void __user *)arg;
247 int __user *p = argp;
248 static const struct watchdog_info ident = {
249 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
250 | WDIOF_MAGICCLOSE,
251 .firmware_version = 1,
252 .identity = "W83877F",
255 switch (cmd) {
256 case WDIOC_GETSUPPORT:
257 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
258 case WDIOC_GETSTATUS:
259 case WDIOC_GETBOOTSTATUS:
260 return put_user(0, p);
261 case WDIOC_SETOPTIONS:
263 int new_options, retval = -EINVAL;
265 if (get_user(new_options, p))
266 return -EFAULT;
268 if (new_options & WDIOS_DISABLECARD) {
269 wdt_turnoff();
270 retval = 0;
273 if (new_options & WDIOS_ENABLECARD) {
274 wdt_startup();
275 retval = 0;
278 return retval;
280 case WDIOC_KEEPALIVE:
281 wdt_keepalive();
282 return 0;
283 case WDIOC_SETTIMEOUT:
285 int new_timeout;
287 if (get_user(new_timeout, p))
288 return -EFAULT;
290 /* arbitrary upper limit */
291 if (new_timeout < 1 || new_timeout > 3600)
292 return -EINVAL;
294 timeout = new_timeout;
295 wdt_keepalive();
296 /* Fall through */
298 case WDIOC_GETTIMEOUT:
299 return put_user(timeout, p);
300 default:
301 return -ENOTTY;
305 static const struct file_operations wdt_fops = {
306 .owner = THIS_MODULE,
307 .llseek = no_llseek,
308 .write = fop_write,
309 .open = fop_open,
310 .release = fop_close,
311 .unlocked_ioctl = fop_ioctl,
314 static struct miscdevice wdt_miscdev = {
315 .minor = WATCHDOG_MINOR,
316 .name = "watchdog",
317 .fops = &wdt_fops,
321 * Notifier for system down
324 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
325 void *unused)
327 if (code == SYS_DOWN || code == SYS_HALT)
328 wdt_turnoff();
329 return NOTIFY_DONE;
333 * The WDT needs to learn about soft shutdowns in order to
334 * turn the timebomb registers off.
337 static struct notifier_block wdt_notifier = {
338 .notifier_call = wdt_notify_sys,
341 static void __exit w83877f_wdt_unload(void)
343 wdt_turnoff();
345 /* Deregister */
346 misc_deregister(&wdt_miscdev);
348 unregister_reboot_notifier(&wdt_notifier);
349 release_region(WDT_PING, 1);
350 release_region(ENABLE_W83877F_PORT, 2);
353 static int __init w83877f_wdt_init(void)
355 int rc = -EBUSY;
357 if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
358 timeout = WATCHDOG_TIMEOUT;
359 printk(KERN_INFO PFX
360 "timeout value must be 1 <= x <= 3600, using %d\n",
361 timeout);
364 if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT")) {
365 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
366 ENABLE_W83877F_PORT);
367 rc = -EIO;
368 goto err_out;
371 if (!request_region(WDT_PING, 1, "W8387FF WDT")) {
372 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
373 WDT_PING);
374 rc = -EIO;
375 goto err_out_region1;
378 rc = register_reboot_notifier(&wdt_notifier);
379 if (rc) {
380 printk(KERN_ERR PFX
381 "cannot register reboot notifier (err=%d)\n", rc);
382 goto err_out_region2;
385 rc = misc_register(&wdt_miscdev);
386 if (rc) {
387 printk(KERN_ERR PFX
388 "cannot register miscdev on minor=%d (err=%d)\n",
389 wdt_miscdev.minor, rc);
390 goto err_out_reboot;
393 printk(KERN_INFO PFX
394 "WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
395 timeout, nowayout);
397 return 0;
399 err_out_reboot:
400 unregister_reboot_notifier(&wdt_notifier);
401 err_out_region2:
402 release_region(WDT_PING, 1);
403 err_out_region1:
404 release_region(ENABLE_W83877F_PORT, 2);
405 err_out:
406 return rc;
409 module_init(w83877f_wdt_init);
410 module_exit(w83877f_wdt_unload);
412 MODULE_AUTHOR("Scott and Bill Jennings");
413 MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
414 MODULE_LICENSE("GPL");
415 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);