[PATCH] Kprobes: preempt_disable/enable() simplification
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / watchdog / wdt977.c
blob44d49dfacbb36db7d6d7dd14cafd586b611d77ea
1 /*
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
20 * nwwatchdog_init.
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>
28 #include <linux/fs.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>
35 #include <asm/io.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;
48 static int testmode;
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)");
61 * Start the watchdog
64 static int wdt977_start(void)
66 /* unlock the SuperIO chip */
67 outb(0x87,0x370);
68 outb(0x87,0x370);
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)
76 outb(0x07,0x370);
77 outb(0x08,0x371);
78 outb(0xF2,0x370);
79 outb(timeoutM,0x371);
80 outb(0xF3,0x370);
81 outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */
82 outb(0xF4,0x370);
83 outb(0x00,0x371);
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" */
87 if (!testmode)
89 outb(0x07,0x370);
90 outb(0x07,0x371);
91 outb(0xE6,0x370);
92 outb(0x08,0x371);
95 /* lock the SuperIO chip */
96 outb(0xAA,0x370);
98 printk(KERN_INFO PFX "activated.\n");
100 return 0;
104 * Stop the watchdog
107 static int wdt977_stop(void)
109 /* unlock the SuperIO chip */
110 outb(0x87,0x370);
111 outb(0x87,0x370);
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
118 outb(0x07,0x370);
119 outb(0x08,0x371);
120 outb(0xF2,0x370);
121 outb(0xFF,0x371);
122 outb(0xF3,0x370);
123 outb(0x00,0x371);
124 outb(0xF4,0x370);
125 outb(0x00,0x371);
126 outb(0xF2,0x370);
127 outb(0x00,0x371);
129 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
130 outb(0x07,0x370);
131 outb(0x07,0x371);
132 outb(0xE6,0x370);
133 outb(0x08,0x371);
135 /* lock the SuperIO chip */
136 outb(0xAA,0x370);
138 printk(KERN_INFO PFX "shutdown.\n");
140 return 0;
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 */
151 outb(0x87,0x370);
152 outb(0x87,0x370);
154 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
155 /* F2 has the timeout in minutes */
156 outb(0x07,0x370);
157 outb(0x08,0x371);
158 outb(0xF2,0x370);
159 outb(timeoutM,0x371);
161 /* lock the SuperIO chip */
162 outb(0xAA,0x370);
164 return 0;
168 * Set the watchdog timeout value
171 static int wdt977_set_timeout(int t)
173 int tmrval;
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...
182 tmrval += tmrval;
185 if ((tmrval < 1) || (tmrval > 255))
186 return -EINVAL;
188 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
189 timeout = t;
190 timeoutM = tmrval;
191 return 0;
195 * Get the watchdog status
198 static int wdt977_get_status(int *status)
200 int new_status;
202 *status=0;
204 /* unlock the SuperIO chip */
205 outb(0x87,0x370);
206 outb(0x87,0x370);
208 /* select device Aux2 (device=8) and read watchdog reg F4 */
209 outb(0x07,0x370);
210 outb(0x08,0x371);
211 outb(0xF4,0x370);
212 new_status = inb(0x371);
214 /* lock the SuperIO chip */
215 outb(0xAA,0x370);
217 if (new_status & 1)
218 *status |= WDIOF_CARDRESET;
220 return 0;
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) )
232 return -EBUSY;
234 if (nowayout)
235 __module_get(THIS_MODULE);
237 wdt977_start();
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)
249 wdt977_stop();
250 clear_bit(0,&timer_alive);
251 } else {
252 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
253 wdt977_keepalive();
255 expect_close = 0;
256 return 0;
261 * wdt977_write:
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)
274 if (count) {
275 if (!nowayout) {
276 size_t i;
278 /* In case it was set long ago */
279 expect_close = 0;
281 for (i = 0; i != count; i++) {
282 char c;
283 if (get_user(c, buf + i))
284 return -EFAULT;
285 if (c == 'V')
286 expect_close = 42;
290 wdt977_keepalive();
292 return count;
296 * wdt977_ioctl:
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 |
308 WDIOF_MAGICCLOSE |
309 WDIOF_KEEPALIVEPING,
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)
317 int status;
318 int new_options, retval = -EINVAL;
319 int new_timeout;
320 union {
321 struct watchdog_info __user *ident;
322 int __user *i;
323 } uarg;
325 uarg.i = (int __user *)arg;
327 switch(cmd)
329 default:
330 return -ENOIOCTLCMD;
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:
344 wdt977_keepalive();
345 return 0;
347 case WDIOC_SETOPTIONS:
348 if (get_user (new_options, uarg.i))
349 return -EFAULT;
351 if (new_options & WDIOS_DISABLECARD) {
352 wdt977_stop();
353 retval = 0;
356 if (new_options & WDIOS_ENABLECARD) {
357 wdt977_start();
358 retval = 0;
361 return retval;
363 case WDIOC_SETTIMEOUT:
364 if (get_user(new_timeout, uarg.i))
365 return -EFAULT;
367 if (wdt977_set_timeout(new_timeout))
368 return -EINVAL;
370 wdt977_keepalive();
371 /* Fall */
373 case WDIOC_GETTIMEOUT:
374 return put_user(timeout, uarg.i);
379 static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
380 void *unused)
382 if(code==SYS_DOWN || code==SYS_HALT)
383 wdt977_stop();
384 return NOTIFY_DONE;
387 static struct file_operations wdt977_fops=
389 .owner = THIS_MODULE,
390 .llseek = no_llseek,
391 .write = wdt977_write,
392 .ioctl = wdt977_ioctl,
393 .open = wdt977_open,
394 .release = wdt977_release,
397 static struct miscdevice wdt977_miscdev=
399 .minor = WATCHDOG_MINOR,
400 .name = "watchdog",
401 .fops = &wdt977_fops,
404 static struct notifier_block wdt977_notifier = {
405 .notifier_call = wdt977_notify_sys,
408 static int __init nwwatchdog_init(void)
410 int retval;
411 if (!machine_is_netwinder())
412 return -ENODEV;
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",
418 DEFAULT_TIMEOUT);
421 retval = register_reboot_notifier(&wdt977_notifier);
422 if (retval) {
423 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
424 retval);
425 return retval;
428 retval = misc_register(&wdt977_miscdev);
429 if (retval) {
430 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
431 WATCHDOG_MINOR, retval);
432 unregister_reboot_notifier(&wdt977_notifier);
433 return retval;
436 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
437 timeout, nowayout, testmode);
439 return 0;
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);