Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / char / watchdog / sbc60xxwdt.c
blobd7de9880605a576e3edb7821b02e01daf93f49bf
1 /*
2 * 60xx Single Board Computer Watchdog Timer driver for Linux 2.2.x
4 * Based on acquirewdt.c by Alan Cox.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
11 * The author does NOT admit liability nor provide warranty for
12 * any of this software. This material is provided "AS-IS" in
13 * the hope that it may be useful for others.
15 * (c) Copyright 2000 Jakob Oestergaard <jakob@unthought.net>
17 * 12/4 - 2000 [Initial revision]
18 * 25/4 - 2000 Added /dev/watchdog support
19 * 09/5 - 2001 [smj@oro.net] fixed fop_write to "return 1" on success
20 * 12/4 - 2002 [rob@osinvestor.com] eliminate fop_read
21 * fix possible wdt_is_open race
22 * add CONFIG_WATCHDOG_NOWAYOUT support
23 * remove lock_kernel/unlock_kernel pairs
24 * added KERN_* to printk's
25 * got rid of extraneous comments
26 * changed watchdog_info to correctly reflect what the driver offers
27 * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS, WDIOC_SETTIMEOUT,
28 * WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls
29 * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
30 * use module_param
31 * made timeout (the emulated heartbeat) a module_param
32 * made the keepalive ping an internal subroutine
33 * made wdt_stop and wdt_start module params
34 * added extra printk's for startup problems
35 * added MODULE_AUTHOR and MODULE_DESCRIPTION info
38 * This WDT driver is different from the other Linux WDT
39 * drivers in the following ways:
40 * *) The driver will ping the watchdog by itself, because this
41 * particular WDT has a very short timeout (one second) and it
42 * would be insane to count on any userspace daemon always
43 * getting scheduled within that time frame.
47 #include <linux/module.h>
48 #include <linux/moduleparam.h>
49 #include <linux/types.h>
50 #include <linux/timer.h>
51 #include <linux/jiffies.h>
52 #include <linux/miscdevice.h>
53 #include <linux/watchdog.h>
54 #include <linux/fs.h>
55 #include <linux/ioport.h>
56 #include <linux/notifier.h>
57 #include <linux/reboot.h>
58 #include <linux/init.h>
60 #include <asm/io.h>
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
64 #define OUR_NAME "sbc60xxwdt"
65 #define PFX OUR_NAME ": "
68 * You must set these - The driver cannot probe for the settings
71 static int wdt_stop = 0x45;
72 module_param(wdt_stop, int, 0);
73 MODULE_PARM_DESC(wdt_stop, "SBC60xx WDT 'stop' io port (default 0x45)");
75 static int wdt_start = 0x443;
76 module_param(wdt_start, int, 0);
77 MODULE_PARM_DESC(wdt_start, "SBC60xx WDT 'start' io port (default 0x443)");
80 * The 60xx board can use watchdog timeout values from one second
81 * to several minutes. The default is one second, so if we reset
82 * the watchdog every ~250ms we should be safe.
85 #define WDT_INTERVAL (HZ/4+1)
88 * We must not require too good response from the userspace daemon.
89 * Here we require the userspace daemon to send us a heartbeat
90 * char to /dev/watchdog every 30 seconds.
91 * If the daemon pulses us every 25 seconds, we can still afford
92 * a 5 second scheduling delay on the (high priority) daemon. That
93 * should be sufficient for a box under any load.
96 #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
97 static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
98 module_param(timeout, int, 0);
99 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
101 #ifdef CONFIG_WATCHDOG_NOWAYOUT
102 static int nowayout = 1;
103 #else
104 static int nowayout = 0;
105 #endif
107 module_param(nowayout, int, 0);
108 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
110 static void wdt_timer_ping(unsigned long);
111 static struct timer_list timer;
112 static unsigned long next_heartbeat;
113 static unsigned long wdt_is_open;
114 static char wdt_expect_close;
117 * Whack the dog
120 static void wdt_timer_ping(unsigned long data)
122 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
123 * we agree to ping the WDT
125 if(time_before(jiffies, next_heartbeat))
127 /* Ping the WDT by reading from wdt_start */
128 inb_p(wdt_start);
129 /* Re-set the timer interval */
130 timer.expires = jiffies + WDT_INTERVAL;
131 add_timer(&timer);
132 } else {
133 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
138 * Utility routines
141 static void wdt_startup(void)
143 next_heartbeat = jiffies + (timeout * HZ);
145 /* Start the timer */
146 timer.expires = jiffies + WDT_INTERVAL;
147 add_timer(&timer);
148 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
151 static void wdt_turnoff(void)
153 /* Stop the timer */
154 del_timer(&timer);
155 inb_p(wdt_stop);
156 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
159 static void wdt_keepalive(void)
161 /* user land ping */
162 next_heartbeat = jiffies + (timeout * HZ);
166 * /dev/watchdog handling
169 static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
171 /* See if we got the magic character 'V' and reload the timer */
172 if(count)
174 if (!nowayout)
176 size_t ofs;
178 /* note: just in case someone wrote the magic character
179 * five months ago... */
180 wdt_expect_close = 0;
182 /* scan to see whether or not we got the magic character */
183 for(ofs = 0; ofs != count; ofs++)
185 char c;
186 if(get_user(c, buf+ofs))
187 return -EFAULT;
188 if(c == 'V')
189 wdt_expect_close = 42;
193 /* Well, anyhow someone wrote to us, we should return that favour */
194 wdt_keepalive();
196 return count;
199 static int fop_open(struct inode * inode, struct file * file)
201 nonseekable_open(inode, file);
203 /* Just in case we're already talking to someone... */
204 if(test_and_set_bit(0, &wdt_is_open))
205 return -EBUSY;
207 if (nowayout)
208 __module_get(THIS_MODULE);
210 /* Good, fire up the show */
211 wdt_startup();
212 return 0;
215 static int fop_close(struct inode * inode, struct file * file)
217 if(wdt_expect_close == 42)
218 wdt_turnoff();
219 else {
220 del_timer(&timer);
221 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
223 clear_bit(0, &wdt_is_open);
224 wdt_expect_close = 0;
225 return 0;
228 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
229 unsigned long arg)
231 void __user *argp = (void __user *)arg;
232 int __user *p = argp;
233 static struct watchdog_info ident=
235 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
236 .firmware_version = 1,
237 .identity = "SBC60xx",
240 switch(cmd)
242 default:
243 return -ENOIOCTLCMD;
244 case WDIOC_GETSUPPORT:
245 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
246 case WDIOC_GETSTATUS:
247 case WDIOC_GETBOOTSTATUS:
248 return put_user(0, p);
249 case WDIOC_KEEPALIVE:
250 wdt_keepalive();
251 return 0;
252 case WDIOC_SETOPTIONS:
254 int new_options, retval = -EINVAL;
256 if(get_user(new_options, p))
257 return -EFAULT;
259 if(new_options & WDIOS_DISABLECARD) {
260 wdt_turnoff();
261 retval = 0;
264 if(new_options & WDIOS_ENABLECARD) {
265 wdt_startup();
266 retval = 0;
269 return retval;
271 case WDIOC_SETTIMEOUT:
273 int new_timeout;
275 if(get_user(new_timeout, p))
276 return -EFAULT;
278 if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
279 return -EINVAL;
281 timeout = new_timeout;
282 wdt_keepalive();
283 /* Fall through */
285 case WDIOC_GETTIMEOUT:
286 return put_user(timeout, p);
290 static struct file_operations wdt_fops = {
291 .owner = THIS_MODULE,
292 .llseek = no_llseek,
293 .write = fop_write,
294 .open = fop_open,
295 .release = fop_close,
296 .ioctl = fop_ioctl,
299 static struct miscdevice wdt_miscdev = {
300 .minor = WATCHDOG_MINOR,
301 .name = "watchdog",
302 .fops = &wdt_fops,
306 * Notifier for system down
309 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
310 void *unused)
312 if(code==SYS_DOWN || code==SYS_HALT)
313 wdt_turnoff();
314 return NOTIFY_DONE;
318 * The WDT needs to learn about soft shutdowns in order to
319 * turn the timebomb registers off.
322 static struct notifier_block wdt_notifier=
324 .notifier_call = wdt_notify_sys,
327 static void __exit sbc60xxwdt_unload(void)
329 wdt_turnoff();
331 /* Deregister */
332 misc_deregister(&wdt_miscdev);
334 unregister_reboot_notifier(&wdt_notifier);
335 if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
336 release_region(wdt_stop,1);
337 release_region(wdt_start,1);
340 static int __init sbc60xxwdt_init(void)
342 int rc = -EBUSY;
344 if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
346 timeout = WATCHDOG_TIMEOUT;
347 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
348 timeout);
351 if (!request_region(wdt_start, 1, "SBC 60XX WDT"))
353 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
354 wdt_start);
355 rc = -EIO;
356 goto err_out;
359 /* We cannot reserve 0x45 - the kernel already has! */
360 if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
362 if (!request_region(wdt_stop, 1, "SBC 60XX WDT"))
364 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
365 wdt_stop);
366 rc = -EIO;
367 goto err_out_region1;
371 init_timer(&timer);
372 timer.function = wdt_timer_ping;
373 timer.data = 0;
375 rc = misc_register(&wdt_miscdev);
376 if (rc)
378 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
379 wdt_miscdev.minor, rc);
380 goto err_out_region2;
383 rc = register_reboot_notifier(&wdt_notifier);
384 if (rc)
386 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
387 rc);
388 goto err_out_miscdev;
391 printk(KERN_INFO PFX "WDT driver for 60XX single board computer initialised. timeout=%d sec (nowayout=%d)\n",
392 timeout, nowayout);
394 return 0;
396 err_out_miscdev:
397 misc_deregister(&wdt_miscdev);
398 err_out_region2:
399 if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
400 release_region(wdt_stop,1);
401 err_out_region1:
402 release_region(wdt_start,1);
403 err_out:
404 return rc;
407 module_init(sbc60xxwdt_init);
408 module_exit(sbc60xxwdt_unload);
410 MODULE_AUTHOR("Jakob Oestergaard <jakob@unthought.net>");
411 MODULE_DESCRIPTION("60xx Single Board Computer Watchdog Timer driver");
412 MODULE_LICENSE("GPL");
413 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);