Remove all #inclusions of asm/system.h
[linux-2.6.git] / drivers / watchdog / sbc60xxwdt.c
blob4b53756542feddc9b1a6a03facaee2f8b6365f26
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"
20 * on success
21 * 12/4 - 2002 [rob@osinvestor.com] eliminate fop_read
22 * fix possible wdt_is_open race
23 * add CONFIG_WATCHDOG_NOWAYOUT support
24 * remove lock_kernel/unlock_kernel pairs
25 * added KERN_* to printk's
26 * got rid of extraneous comments
27 * changed watchdog_info to correctly reflect what
28 * the driver offers
29 * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
30 * WDIOC_SETTIMEOUT, WDIOC_GETTIMEOUT, and
31 * WDIOC_SETOPTIONS ioctls
32 * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
33 * use module_param
34 * made timeout (the emulated heartbeat) a
35 * module_param
36 * made the keepalive ping an internal subroutine
37 * made wdt_stop and wdt_start module params
38 * added extra printk's for startup problems
39 * added MODULE_AUTHOR and MODULE_DESCRIPTION info
42 * This WDT driver is different from the other Linux WDT
43 * drivers in the following ways:
44 * *) The driver will ping the watchdog by itself, because this
45 * particular WDT has a very short timeout (one second) and it
46 * would be insane to count on any userspace daemon always
47 * getting scheduled within that time frame.
51 #include <linux/module.h>
52 #include <linux/moduleparam.h>
53 #include <linux/types.h>
54 #include <linux/timer.h>
55 #include <linux/jiffies.h>
56 #include <linux/miscdevice.h>
57 #include <linux/watchdog.h>
58 #include <linux/fs.h>
59 #include <linux/ioport.h>
60 #include <linux/notifier.h>
61 #include <linux/reboot.h>
62 #include <linux/init.h>
63 #include <linux/io.h>
64 #include <linux/uaccess.h>
67 #define OUR_NAME "sbc60xxwdt"
68 #define PFX OUR_NAME ": "
71 * You must set these - The driver cannot probe for the settings
74 static int wdt_stop = 0x45;
75 module_param(wdt_stop, int, 0);
76 MODULE_PARM_DESC(wdt_stop, "SBC60xx WDT 'stop' io port (default 0x45)");
78 static int wdt_start = 0x443;
79 module_param(wdt_start, int, 0);
80 MODULE_PARM_DESC(wdt_start, "SBC60xx WDT 'start' io port (default 0x443)");
83 * The 60xx board can use watchdog timeout values from one second
84 * to several minutes. The default is one second, so if we reset
85 * the watchdog every ~250ms we should be safe.
88 #define WDT_INTERVAL (HZ/4+1)
91 * We must not require too good response from the userspace daemon.
92 * Here we require the userspace daemon to send us a heartbeat
93 * char to /dev/watchdog every 30 seconds.
94 * If the daemon pulses us every 25 seconds, we can still afford
95 * a 5 second scheduling delay on the (high priority) daemon. That
96 * should be sufficient for a box under any load.
99 #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
100 static int timeout = WATCHDOG_TIMEOUT; /* in seconds, multiplied by HZ to
101 get seconds to wait for a ping */
102 module_param(timeout, int, 0);
103 MODULE_PARM_DESC(timeout,
104 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
105 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
107 static int nowayout = WATCHDOG_NOWAYOUT;
108 module_param(nowayout, int, 0);
109 MODULE_PARM_DESC(nowayout,
110 "Watchdog cannot be stopped once started (default="
111 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
113 static void wdt_timer_ping(unsigned long);
114 static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
115 static unsigned long next_heartbeat;
116 static unsigned long wdt_is_open;
117 static char wdt_expect_close;
120 * Whack the dog
123 static void wdt_timer_ping(unsigned long data)
125 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
126 * we agree to ping the WDT
128 if (time_before(jiffies, next_heartbeat)) {
129 /* Ping the WDT by reading from wdt_start */
130 inb_p(wdt_start);
131 /* Re-set the timer interval */
132 mod_timer(&timer, jiffies + WDT_INTERVAL);
133 } else
134 printk(KERN_WARNING PFX
135 "Heartbeat lost! Will not ping the watchdog\n");
139 * Utility routines
142 static void wdt_startup(void)
144 next_heartbeat = jiffies + (timeout * HZ);
146 /* Start the timer */
147 mod_timer(&timer, jiffies + WDT_INTERVAL);
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,
170 size_t count, loff_t *ppos)
172 /* See if we got the magic character 'V' and reload the timer */
173 if (count) {
174 if (!nowayout) {
175 size_t ofs;
177 /* note: just in case someone wrote the
178 magic character five months ago... */
179 wdt_expect_close = 0;
181 /* scan to see whether or not we got the
182 magic character */
183 for (ofs = 0; ofs != count; ofs++) {
184 char c;
185 if (get_user(c, buf + ofs))
186 return -EFAULT;
187 if (c == 'V')
188 wdt_expect_close = 42;
192 /* Well, anyhow someone wrote to us, we should
193 return that favour */
194 wdt_keepalive();
196 return count;
199 static int fop_open(struct inode *inode, struct file *file)
201 /* Just in case we're already talking to someone... */
202 if (test_and_set_bit(0, &wdt_is_open))
203 return -EBUSY;
205 if (nowayout)
206 __module_get(THIS_MODULE);
208 /* Good, fire up the show */
209 wdt_startup();
210 return nonseekable_open(inode, file);
213 static int fop_close(struct inode *inode, struct file *file)
215 if (wdt_expect_close == 42)
216 wdt_turnoff();
217 else {
218 del_timer(&timer);
219 printk(KERN_CRIT PFX
220 "device file closed unexpectedly. Will not stop the WDT!\n");
222 clear_bit(0, &wdt_is_open);
223 wdt_expect_close = 0;
224 return 0;
227 static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
229 void __user *argp = (void __user *)arg;
230 int __user *p = argp;
231 static const struct watchdog_info ident = {
232 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
233 WDIOF_MAGICCLOSE,
234 .firmware_version = 1,
235 .identity = "SBC60xx",
238 switch (cmd) {
239 case WDIOC_GETSUPPORT:
240 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
241 case WDIOC_GETSTATUS:
242 case WDIOC_GETBOOTSTATUS:
243 return put_user(0, p);
244 case WDIOC_SETOPTIONS:
246 int new_options, retval = -EINVAL;
247 if (get_user(new_options, p))
248 return -EFAULT;
249 if (new_options & WDIOS_DISABLECARD) {
250 wdt_turnoff();
251 retval = 0;
253 if (new_options & WDIOS_ENABLECARD) {
254 wdt_startup();
255 retval = 0;
257 return retval;
259 case WDIOC_KEEPALIVE:
260 wdt_keepalive();
261 return 0;
262 case WDIOC_SETTIMEOUT:
264 int new_timeout;
265 if (get_user(new_timeout, p))
266 return -EFAULT;
267 /* arbitrary upper limit */
268 if (new_timeout < 1 || new_timeout > 3600)
269 return -EINVAL;
271 timeout = new_timeout;
272 wdt_keepalive();
273 /* Fall through */
275 case WDIOC_GETTIMEOUT:
276 return put_user(timeout, p);
277 default:
278 return -ENOTTY;
282 static const struct file_operations wdt_fops = {
283 .owner = THIS_MODULE,
284 .llseek = no_llseek,
285 .write = fop_write,
286 .open = fop_open,
287 .release = fop_close,
288 .unlocked_ioctl = fop_ioctl,
291 static struct miscdevice wdt_miscdev = {
292 .minor = WATCHDOG_MINOR,
293 .name = "watchdog",
294 .fops = &wdt_fops,
298 * Notifier for system down
301 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
302 void *unused)
304 if (code == SYS_DOWN || code == SYS_HALT)
305 wdt_turnoff();
306 return NOTIFY_DONE;
310 * The WDT needs to learn about soft shutdowns in order to
311 * turn the timebomb registers off.
314 static struct notifier_block wdt_notifier = {
315 .notifier_call = wdt_notify_sys,
318 static void __exit sbc60xxwdt_unload(void)
320 wdt_turnoff();
322 /* Deregister */
323 misc_deregister(&wdt_miscdev);
325 unregister_reboot_notifier(&wdt_notifier);
326 if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
327 release_region(wdt_stop, 1);
328 release_region(wdt_start, 1);
331 static int __init sbc60xxwdt_init(void)
333 int rc = -EBUSY;
335 if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
336 timeout = WATCHDOG_TIMEOUT;
337 printk(KERN_INFO PFX
338 "timeout value must be 1 <= x <= 3600, using %d\n",
339 timeout);
342 if (!request_region(wdt_start, 1, "SBC 60XX WDT")) {
343 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
344 wdt_start);
345 rc = -EIO;
346 goto err_out;
349 /* We cannot reserve 0x45 - the kernel already has! */
350 if (wdt_stop != 0x45 && wdt_stop != wdt_start) {
351 if (!request_region(wdt_stop, 1, "SBC 60XX WDT")) {
352 printk(KERN_ERR PFX
353 "I/O address 0x%04x already in use\n",
354 wdt_stop);
355 rc = -EIO;
356 goto err_out_region1;
360 rc = register_reboot_notifier(&wdt_notifier);
361 if (rc) {
362 printk(KERN_ERR PFX
363 "cannot register reboot notifier (err=%d)\n", rc);
364 goto err_out_region2;
367 rc = misc_register(&wdt_miscdev);
368 if (rc) {
369 printk(KERN_ERR PFX
370 "cannot register miscdev on minor=%d (err=%d)\n",
371 wdt_miscdev.minor, rc);
372 goto err_out_reboot;
374 printk(KERN_INFO PFX
375 "WDT driver for 60XX single board computer initialised. "
376 "timeout=%d sec (nowayout=%d)\n", timeout, nowayout);
378 return 0;
380 err_out_reboot:
381 unregister_reboot_notifier(&wdt_notifier);
382 err_out_region2:
383 if (wdt_stop != 0x45 && wdt_stop != wdt_start)
384 release_region(wdt_stop, 1);
385 err_out_region1:
386 release_region(wdt_start, 1);
387 err_out:
388 return rc;
391 module_init(sbc60xxwdt_init);
392 module_exit(sbc60xxwdt_unload);
394 MODULE_AUTHOR("Jakob Oestergaard <jakob@unthought.net>");
395 MODULE_DESCRIPTION("60xx Single Board Computer Watchdog Timer driver");
396 MODULE_LICENSE("GPL");
397 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);