2 * Wdt977 0.03: A Watchdog Device for Netwinder W83977AF chip
4 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
6 * -----------------------
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * -----------------------
14 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
15 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
16 * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
17 * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
18 * from minutes to seconds.
19 * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/config.h>
26 #include <linux/types.h>
27 #include <linux/kernel.h>
29 #include <linux/miscdevice.h>
30 #include <linux/init.h>
31 #include <linux/watchdog.h>
32 #include <linux/notifier.h>
33 #include <linux/reboot.h>
36 #include <asm/system.h>
37 #include <asm/mach-types.h>
38 #include <asm/uaccess.h>
40 #define PFX "Wdt977: "
41 #define WATCHDOG_MINOR 130
43 #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
45 static int timeout = DEFAULT_TIMEOUT;
46 static int timeoutM; /* timeout in minutes */
47 static unsigned long timer_alive;
49 static char expect_close;
51 module_param(timeout, int, 0);
52 MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
53 module_param(testmode, int, 0);
54 MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
56 static int nowayout = WATCHDOG_NOWAYOUT;
57 module_param(nowayout, int, 0);
58 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
64 static int wdt977_start(void)
66 /* unlock the SuperIO chip */
70 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
71 * F2 has the timeout in minutes
72 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
73 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
74 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
81 outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */
85 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
86 /* in test mode watch the bit 1 on F4 to indicate "triggered" */
95 /* lock the SuperIO chip */
98 printk(KERN_INFO PFX "activated.\n");
107 static int wdt977_stop(void)
109 /* unlock the SuperIO chip */
113 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
114 * F3 is reset to its default state
115 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
116 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
129 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
135 /* lock the SuperIO chip */
138 printk(KERN_INFO PFX "shutdown.\n");
144 * Send a keepalive ping to the watchdog
145 * This is done by simply re-writing the timeout to reg. 0xF2
148 static int wdt977_keepalive(void)
150 /* unlock the SuperIO chip */
154 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
155 /* F2 has the timeout in minutes */
159 outb(timeoutM,0x371);
161 /* lock the SuperIO chip */
168 * Set the watchdog timeout value
171 static int wdt977_set_timeout(int t)
175 /* convert seconds to minutes, rounding up */
176 tmrval = (t + 59) / 60;
178 if (machine_is_netwinder()) {
179 /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
180 * this limits the max timeout to half of device max of 255 minutes...
185 if ((tmrval < 1) || (tmrval > 255))
188 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
195 * Get the watchdog status
198 static int wdt977_get_status(int *status)
204 /* unlock the SuperIO chip */
208 /* select device Aux2 (device=8) and read watchdog reg F4 */
212 new_status = inb(0x371);
214 /* lock the SuperIO chip */
218 *status |= WDIOF_CARDRESET;
225 * /dev/watchdog handling
228 static int wdt977_open(struct inode *inode, struct file *file)
230 /* If the watchdog is alive we don't need to start it again */
231 if( test_and_set_bit(0,&timer_alive) )
235 __module_get(THIS_MODULE);
238 return nonseekable_open(inode, file);
241 static int wdt977_release(struct inode *inode, struct file *file)
244 * Shut off the timer.
245 * Lock it in if it's a module and we set nowayout
247 if (expect_close == 42)
250 clear_bit(0,&timer_alive);
252 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
262 * @file: file handle to the watchdog
263 * @buf: buffer to write (unused as data does not matter here
264 * @count: count of bytes
265 * @ppos: pointer to the position to write. No seeks allowed
267 * A write to a watchdog device is defined as a keepalive signal. Any
268 * write of data will do, as we we don't define content meaning.
271 static ssize_t wdt977_write(struct file *file, const char __user *buf,
272 size_t count, loff_t *ppos)
278 /* In case it was set long ago */
281 for (i = 0; i != count; i++) {
283 if (get_user(c, buf + i))
297 * @inode: inode of the device
298 * @file: file handle to the device
299 * @cmd: watchdog command
300 * @arg: argument pointer
302 * The watchdog API defines a common set of functions for all watchdogs
303 * according to their available features.
306 static struct watchdog_info ident = {
307 .options = WDIOF_SETTIMEOUT |
310 .firmware_version = 1,
311 .identity = "Winbond 83977",
314 static int wdt977_ioctl(struct inode *inode, struct file *file,
315 unsigned int cmd, unsigned long arg)
318 int new_options, retval = -EINVAL;
321 struct watchdog_info __user *ident;
325 uarg.i = (int __user *)arg;
332 case WDIOC_GETSUPPORT:
333 return copy_to_user(uarg.ident, &ident,
334 sizeof(ident)) ? -EFAULT : 0;
336 case WDIOC_GETSTATUS:
337 wdt977_get_status(&status);
338 return put_user(status, uarg.i);
340 case WDIOC_GETBOOTSTATUS:
341 return put_user(0, uarg.i);
343 case WDIOC_KEEPALIVE:
347 case WDIOC_SETOPTIONS:
348 if (get_user (new_options, uarg.i))
351 if (new_options & WDIOS_DISABLECARD) {
356 if (new_options & WDIOS_ENABLECARD) {
363 case WDIOC_SETTIMEOUT:
364 if (get_user(new_timeout, uarg.i))
367 if (wdt977_set_timeout(new_timeout))
373 case WDIOC_GETTIMEOUT:
374 return put_user(timeout, uarg.i);
379 static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
382 if(code==SYS_DOWN || code==SYS_HALT)
387 static struct file_operations wdt977_fops=
389 .owner = THIS_MODULE,
391 .write = wdt977_write,
392 .ioctl = wdt977_ioctl,
394 .release = wdt977_release,
397 static struct miscdevice wdt977_miscdev=
399 .minor = WATCHDOG_MINOR,
401 .fops = &wdt977_fops,
404 static struct notifier_block wdt977_notifier = {
405 .notifier_call = wdt977_notify_sys,
408 static int __init nwwatchdog_init(void)
411 if (!machine_is_netwinder())
414 /* Check that the timeout value is within it's range ; if not reset to the default */
415 if (wdt977_set_timeout(timeout)) {
416 wdt977_set_timeout(DEFAULT_TIMEOUT);
417 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
421 retval = register_reboot_notifier(&wdt977_notifier);
423 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
428 retval = misc_register(&wdt977_miscdev);
430 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
431 WATCHDOG_MINOR, retval);
432 unregister_reboot_notifier(&wdt977_notifier);
436 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
437 timeout, nowayout, testmode);
442 static void __exit nwwatchdog_exit(void)
444 misc_deregister(&wdt977_miscdev);
445 unregister_reboot_notifier(&wdt977_notifier);
448 module_init(nwwatchdog_init);
449 module_exit(nwwatchdog_exit);
451 MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>");
452 MODULE_DESCRIPTION("W83977AF Watchdog driver");
453 MODULE_LICENSE("GPL");
454 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);