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 #ifdef CONFIG_WATCHDOG_NOWAYOUT
57 static int nowayout
= 1;
59 static int nowayout
= 0;
62 module_param(nowayout
, int, 0);
63 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
69 static int wdt977_start(void)
71 /* unlock the SuperIO chip */
75 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
76 * F2 has the timeout in minutes
77 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
78 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
79 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
86 outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */
90 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
91 /* in test mode watch the bit 1 on F4 to indicate "triggered" */
100 /* lock the SuperIO chip */
103 printk(KERN_INFO PFX
"activated.\n");
112 static int wdt977_stop(void)
114 /* unlock the SuperIO chip */
118 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
119 * F3 is reset to its default state
120 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
121 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
134 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
140 /* lock the SuperIO chip */
143 printk(KERN_INFO PFX
"shutdown.\n");
149 * Send a keepalive ping to the watchdog
150 * This is done by simply re-writing the timeout to reg. 0xF2
153 static int wdt977_keepalive(void)
155 /* unlock the SuperIO chip */
159 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
160 /* F2 has the timeout in minutes */
164 outb(timeoutM
,0x371);
166 /* lock the SuperIO chip */
173 * Set the watchdog timeout value
176 static int wdt977_set_timeout(int t
)
180 /* convert seconds to minutes, rounding up */
181 tmrval
= (t
+ 59) / 60;
183 if (machine_is_netwinder()) {
184 /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
185 * this limits the max timeout to half of device max of 255 minutes...
190 if ((tmrval
< 1) || (tmrval
> 255))
193 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
200 * Get the watchdog status
203 static int wdt977_get_status(int *status
)
209 /* unlock the SuperIO chip */
213 /* select device Aux2 (device=8) and read watchdog reg F4 */
217 new_status
= inb(0x371);
219 /* lock the SuperIO chip */
223 *status
|= WDIOF_CARDRESET
;
230 * /dev/watchdog handling
233 static int wdt977_open(struct inode
*inode
, struct file
*file
)
235 /* If the watchdog is alive we don't need to start it again */
236 if( test_and_set_bit(0,&timer_alive
) )
240 __module_get(THIS_MODULE
);
243 return nonseekable_open(inode
, file
);
246 static int wdt977_release(struct inode
*inode
, struct file
*file
)
249 * Shut off the timer.
250 * Lock it in if it's a module and we set nowayout
252 if (expect_close
== 42)
255 clear_bit(0,&timer_alive
);
257 printk(KERN_CRIT PFX
"Unexpected close, not stopping watchdog!\n");
267 * @file: file handle to the watchdog
268 * @buf: buffer to write (unused as data does not matter here
269 * @count: count of bytes
270 * @ppos: pointer to the position to write. No seeks allowed
272 * A write to a watchdog device is defined as a keepalive signal. Any
273 * write of data will do, as we we don't define content meaning.
276 static ssize_t
wdt977_write(struct file
*file
, const char __user
*buf
,
277 size_t count
, loff_t
*ppos
)
283 /* In case it was set long ago */
286 for (i
= 0; i
!= count
; i
++) {
288 if (get_user(c
, buf
+ i
))
302 * @inode: inode of the device
303 * @file: file handle to the device
304 * @cmd: watchdog command
305 * @arg: argument pointer
307 * The watchdog API defines a common set of functions for all watchdogs
308 * according to their available features.
311 static struct watchdog_info ident
= {
312 .options
= WDIOF_SETTIMEOUT
|
315 .firmware_version
= 1,
316 .identity
= "Winbond 83977",
319 static int wdt977_ioctl(struct inode
*inode
, struct file
*file
,
320 unsigned int cmd
, unsigned long arg
)
323 int new_options
, retval
= -EINVAL
;
326 struct watchdog_info __user
*ident
;
330 uarg
.i
= (int __user
*)arg
;
337 case WDIOC_GETSUPPORT
:
338 return copy_to_user(uarg
.ident
, &ident
,
339 sizeof(ident
)) ? -EFAULT
: 0;
341 case WDIOC_GETSTATUS
:
342 wdt977_get_status(&status
);
343 return put_user(status
, uarg
.i
);
345 case WDIOC_GETBOOTSTATUS
:
346 return put_user(0, uarg
.i
);
348 case WDIOC_KEEPALIVE
:
352 case WDIOC_SETOPTIONS
:
353 if (get_user (new_options
, uarg
.i
))
356 if (new_options
& WDIOS_DISABLECARD
) {
361 if (new_options
& WDIOS_ENABLECARD
) {
368 case WDIOC_SETTIMEOUT
:
369 if (get_user(new_timeout
, uarg
.i
))
372 if (wdt977_set_timeout(new_timeout
))
378 case WDIOC_GETTIMEOUT
:
379 return put_user(timeout
, uarg
.i
);
384 static int wdt977_notify_sys(struct notifier_block
*this, unsigned long code
,
387 if(code
==SYS_DOWN
|| code
==SYS_HALT
)
392 static struct file_operations wdt977_fops
=
394 .owner
= THIS_MODULE
,
396 .write
= wdt977_write
,
397 .ioctl
= wdt977_ioctl
,
399 .release
= wdt977_release
,
402 static struct miscdevice wdt977_miscdev
=
404 .minor
= WATCHDOG_MINOR
,
406 .fops
= &wdt977_fops
,
409 static struct notifier_block wdt977_notifier
= {
410 .notifier_call
= wdt977_notify_sys
,
413 static int __init
nwwatchdog_init(void)
416 if (!machine_is_netwinder())
419 /* Check that the timeout value is within it's range ; if not reset to the default */
420 if (wdt977_set_timeout(timeout
)) {
421 wdt977_set_timeout(DEFAULT_TIMEOUT
);
422 printk(KERN_INFO PFX
"timeout value must be 60<timeout<15300, using %d\n",
426 retval
= register_reboot_notifier(&wdt977_notifier
);
428 printk(KERN_ERR PFX
"cannot register reboot notifier (err=%d)\n",
433 retval
= misc_register(&wdt977_miscdev
);
435 printk(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
436 WATCHDOG_MINOR
, retval
);
437 unregister_reboot_notifier(&wdt977_notifier
);
441 printk(KERN_INFO PFX
"initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
442 timeout
, nowayout
, testmode
);
447 static void __exit
nwwatchdog_exit(void)
449 misc_deregister(&wdt977_miscdev
);
450 unregister_reboot_notifier(&wdt977_notifier
);
453 module_init(nwwatchdog_init
);
454 module_exit(nwwatchdog_exit
);
456 MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>");
457 MODULE_DESCRIPTION("W83977AF Watchdog driver");
458 MODULE_LICENSE("GPL");
459 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);