ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / wdt977.c
1 /*
2  *      Wdt977  0.03:   A Watchdog Device for Netwinder W83977AF chip
3  *
4  *      (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
5  *
6  *                      -----------------------
7  *
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.
12  *
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.
21  */
22
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>
34
35 #include <asm/io.h>
36 #include <asm/system.h>
37 #include <asm/mach-types.h>
38 #include <asm/uaccess.h>
39
40 #define PFX "Wdt977: "
41 #define WATCHDOG_MINOR  130
42
43 #define DEFAULT_TIMEOUT 60                      /* default timeout in seconds */
44
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;
50
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");
55
56 #ifdef CONFIG_WATCHDOG_NOWAYOUT
57 static int nowayout = 1;
58 #else
59 static int nowayout = 0;
60 #endif
61
62 module_param(nowayout, int, 0);
63 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
64
65 /*
66  * Start the watchdog
67  */
68
69 static int wdt977_start(void)
70 {
71         /* unlock the SuperIO chip */
72         outb(0x87,0x370);
73         outb(0x87,0x370);
74
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)
80          */
81         outb(0x07,0x370);
82         outb(0x08,0x371);
83         outb(0xF2,0x370);
84         outb(timeoutM,0x371);
85         outb(0xF3,0x370);
86         outb(0x00,0x371);       /* another setting is 0E for kbd/mouse/LED */
87         outb(0xF4,0x370);
88         outb(0x00,0x371);
89
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" */
92         if (!testmode)
93         {
94                 outb(0x07,0x370);
95                 outb(0x07,0x371);
96                 outb(0xE6,0x370);
97                 outb(0x08,0x371);
98         }
99
100         /* lock the SuperIO chip */
101         outb(0xAA,0x370);
102
103         printk(KERN_INFO PFX "activated.\n");
104
105         return 0;
106 }
107
108 /*
109  * Stop the watchdog
110  */
111
112 static int wdt977_stop(void)
113 {
114         /* unlock the SuperIO chip */
115         outb(0x87,0x370);
116         outb(0x87,0x370);
117
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
122         */
123         outb(0x07,0x370);
124         outb(0x08,0x371);
125         outb(0xF2,0x370);
126         outb(0xFF,0x371);
127         outb(0xF3,0x370);
128         outb(0x00,0x371);
129         outb(0xF4,0x370);
130         outb(0x00,0x371);
131         outb(0xF2,0x370);
132         outb(0x00,0x371);
133
134         /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
135         outb(0x07,0x370);
136         outb(0x07,0x371);
137         outb(0xE6,0x370);
138         outb(0x08,0x371);
139
140         /* lock the SuperIO chip */
141         outb(0xAA,0x370);
142
143         printk(KERN_INFO PFX "shutdown.\n");
144
145         return 0;
146 }
147
148 /*
149  * Send a keepalive ping to the watchdog
150  * This is done by simply re-writing the timeout to reg. 0xF2
151  */
152
153 static int wdt977_keepalive(void)
154 {
155         /* unlock the SuperIO chip */
156         outb(0x87,0x370);
157         outb(0x87,0x370);
158
159         /* select device Aux2 (device=8) and kicks watchdog reg F2 */
160         /* F2 has the timeout in minutes */
161         outb(0x07,0x370);
162         outb(0x08,0x371);
163         outb(0xF2,0x370);
164         outb(timeoutM,0x371);
165
166         /* lock the SuperIO chip */
167         outb(0xAA,0x370);
168
169         return 0;
170 }
171
172 /*
173  * Set the watchdog timeout value
174  */
175
176 static int wdt977_set_timeout(int t)
177 {
178         int tmrval;
179
180         /* convert seconds to minutes, rounding up */
181         tmrval = (t + 59) / 60;
182
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...
186                  */
187                 tmrval += tmrval;
188         }
189
190         if ((tmrval < 1) || (tmrval > 255))
191                 return -EINVAL;
192
193         /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
194         timeout = t;
195         timeoutM = tmrval;
196         return 0;
197 }
198
199 /*
200  * Get the watchdog status
201  */
202
203 static int wdt977_get_status(int *status)
204 {
205         int new_status;
206
207         *status=0;
208
209         /* unlock the SuperIO chip */
210         outb(0x87,0x370);
211         outb(0x87,0x370);
212
213         /* select device Aux2 (device=8) and read watchdog reg F4 */
214         outb(0x07,0x370);
215         outb(0x08,0x371);
216         outb(0xF4,0x370);
217         new_status = inb(0x371);
218
219         /* lock the SuperIO chip */
220         outb(0xAA,0x370);
221
222         if (new_status & 1)
223                 *status |= WDIOF_CARDRESET;
224
225         return 0;
226 }
227
228
229 /*
230  *      /dev/watchdog handling
231  */
232
233 static int wdt977_open(struct inode *inode, struct file *file)
234 {
235         /* If the watchdog is alive we don't need to start it again */
236         if( test_and_set_bit(0,&timer_alive) )
237                 return -EBUSY;
238
239         if (nowayout)
240                 __module_get(THIS_MODULE);
241
242         wdt977_start();
243         return 0;
244 }
245
246 static int wdt977_release(struct inode *inode, struct file *file)
247 {
248         /*
249          *      Shut off the timer.
250          *      Lock it in if it's a module and we set nowayout
251          */
252         if (expect_close == 42)
253         {
254                 wdt977_stop();
255                 clear_bit(0,&timer_alive);
256         } else {
257                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
258                 wdt977_keepalive();
259         }
260         expect_close = 0;
261         return 0;
262 }
263
264
265 /*
266  *      wdt977_write:
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
271  *
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.
274  */
275
276 static ssize_t wdt977_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
277 {
278         /* Can't seek (pwrite) on this device  */
279         if (ppos != &file->f_pos)
280                 return -ESPIPE;
281
282         if (count) {
283                 if (!nowayout) {
284                         size_t i;
285
286                         /* In case it was set long ago */
287                         expect_close = 0;
288
289                         for (i = 0; i != count; i++) {
290                                 char c;
291                                 if (get_user(c, buf + i))
292                                         return -EFAULT;
293                                 if (c == 'V')
294                                         expect_close = 42;
295                         }
296                 }
297
298                 wdt977_keepalive();
299         }
300         return count;
301 }
302
303 /*
304  *      wdt977_ioctl:
305  *      @inode: inode of the device
306  *      @file: file handle to the device
307  *      @cmd: watchdog command
308  *      @arg: argument pointer
309  *
310  *      The watchdog API defines a common set of functions for all watchdogs
311  *      according to their available features.
312  */
313
314 static struct watchdog_info ident = {
315         .options =              WDIOF_SETTIMEOUT |
316                                 WDIOF_MAGICCLOSE |
317                                 WDIOF_KEEPALIVEPING,
318         .firmware_version =     1,
319         .identity =             "Winbond 83977",
320 };
321
322 static int wdt977_ioctl(struct inode *inode, struct file *file,
323         unsigned int cmd, unsigned long arg)
324 {
325         int status;
326         int new_options, retval = -EINVAL;
327         int new_timeout;
328
329         switch(cmd)
330         {
331         default:
332                 return -ENOIOCTLCMD;
333
334         case WDIOC_GETSUPPORT:
335                 return copy_to_user((struct watchdog_info *)arg, &ident,
336                         sizeof(ident)) ? -EFAULT : 0;
337
338         case WDIOC_GETSTATUS:
339                 wdt977_get_status(&status);
340                 return put_user(status, (int *) arg);
341
342         case WDIOC_GETBOOTSTATUS:
343                 return put_user(0, (int *) arg);
344
345         case WDIOC_KEEPALIVE:
346                 wdt977_keepalive();
347                 return 0;
348
349         case WDIOC_SETOPTIONS:
350                 if (get_user (new_options, (int *) arg))
351                         return -EFAULT;
352
353                 if (new_options & WDIOS_DISABLECARD) {
354                         wdt977_stop();
355                         retval = 0;
356                 }
357
358                 if (new_options & WDIOS_ENABLECARD) {
359                         wdt977_start();
360                         retval = 0;
361                 }
362
363                 return retval;
364
365         case WDIOC_SETTIMEOUT:
366                 if (get_user(new_timeout, (int *) arg))
367                         return -EFAULT;
368
369                 if (wdt977_set_timeout(new_timeout))
370                     return -EINVAL;
371
372                 wdt977_keepalive();
373                 /* Fall */
374
375         case WDIOC_GETTIMEOUT:
376                 return put_user(timeout, (int *)arg);
377
378         }
379 }
380
381 static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
382         void *unused)
383 {
384         if(code==SYS_DOWN || code==SYS_HALT)
385                 wdt977_stop();
386         return NOTIFY_DONE;
387 }
388
389 static struct file_operations wdt977_fops=
390 {
391         .owner          = THIS_MODULE,
392         .write          = wdt977_write,
393         .ioctl          = wdt977_ioctl,
394         .open           = wdt977_open,
395         .release        = wdt977_release,
396 };
397
398 static struct miscdevice wdt977_miscdev=
399 {
400         .minor          = WATCHDOG_MINOR,
401         .name           = "watchdog",
402         .fops           = &wdt977_fops,
403 };
404
405 static struct notifier_block wdt977_notifier = {
406         .notifier_call = wdt977_notify_sys,
407 };
408
409 static int __init nwwatchdog_init(void)
410 {
411         int retval;
412         if (!machine_is_netwinder())
413                 return -ENODEV;
414
415         /* Check that the timeout value is within it's range ; if not reset to the default */
416         if (wdt977_set_timeout(timeout)) {
417                 wdt977_set_timeout(DEFAULT_TIMEOUT);
418                 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
419                         DEFAULT_TIMEOUT);
420         }
421
422         retval = register_reboot_notifier(&wdt977_notifier);
423         if (retval) {
424                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
425                         retval);
426                 return retval;
427         }
428
429         retval = misc_register(&wdt977_miscdev);
430         if (retval) {
431                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
432                         WATCHDOG_MINOR, retval);
433                 unregister_reboot_notifier(&wdt977_notifier);
434                 return retval;
435         }
436
437         printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
438                 timeout, nowayout, testmode);
439
440         return 0;
441 }
442
443 static void __exit nwwatchdog_exit(void)
444 {
445         misc_deregister(&wdt977_miscdev);
446         unregister_reboot_notifier(&wdt977_notifier);
447 }
448
449 module_init(nwwatchdog_init);
450 module_exit(nwwatchdog_exit);
451
452 MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>");
453 MODULE_DESCRIPTION("W83977AF Watchdog driver");
454 MODULE_LICENSE("GPL");
455 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);