ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / w83877f_wdt.c
1 /*
2  *      W83877F Computer Watchdog Timer driver
3  *
4  *      Based on acquirewdt.c by Alan Cox,
5  *           and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  *      The authors do NOT admit liability nor provide warranty for
13  *      any of this software. This material is provided "AS-IS" in
14  *      the hope that it may be useful for others.
15  *
16  *      (c) Copyright 2001    Scott Jennings <linuxdrivers@oro.net>
17  *
18  *           4/19 - 2001      [Initial revision]
19  *           9/27 - 2001      Added spinlocking
20  *           4/12 - 2002      [rob@osinvestor.com] Eliminate extra comments
21  *                            Eliminate fop_read
22  *                            Eliminate extra spin_unlock
23  *                            Added KERN_* tags to printks
24  *                            add CONFIG_WATCHDOG_NOWAYOUT support
25  *                            fix possible wdt_is_open race
26  *                            changed watchdog_info to correctly reflect what the driver offers
27  *                            added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS, WDIOC_SETTIMEOUT,
28  *                            WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls
29  *           09/8 - 2003      [wim@iguana.be] cleanup of trailing spaces
30  *                            added extra printk's for startup problems
31  *                            use module_param
32  *                            made timeout (the emulated heartbeat) a module_param
33  *                            made the keepalive ping an internal subroutine
34  *
35  *  This WDT driver is different from most other Linux WDT
36  *  drivers in that the driver will ping the watchdog by itself,
37  *  because this particular WDT has a very short timeout (1.6
38  *  seconds) and it would be insane to count on any userspace
39  *  daemon always getting scheduled within that time frame.
40  */
41
42 #include <linux/module.h>
43 #include <linux/moduleparam.h>
44 #include <linux/types.h>
45 #include <linux/timer.h>
46 #include <linux/jiffies.h>
47 #include <linux/miscdevice.h>
48 #include <linux/watchdog.h>
49 #include <linux/fs.h>
50 #include <linux/ioport.h>
51 #include <linux/notifier.h>
52 #include <linux/reboot.h>
53 #include <linux/init.h>
54 #include <asm/io.h>
55 #include <asm/uaccess.h>
56 #include <asm/system.h>
57
58 #define OUR_NAME "w83877f_wdt"
59 #define PFX OUR_NAME ": "
60
61 #define ENABLE_W83877F_PORT 0x3F0
62 #define ENABLE_W83877F 0x87
63 #define DISABLE_W83877F 0xAA
64 #define WDT_PING 0x443
65 #define WDT_REGISTER 0x14
66 #define WDT_ENABLE 0x9C
67 #define WDT_DISABLE 0x8C
68
69 /*
70  * The W83877F seems to be fixed at 1.6s timeout (at least on the
71  * EMACS PC-104 board I'm using). If we reset the watchdog every
72  * ~250ms we should be safe.  */
73
74 #define WDT_INTERVAL (HZ/4+1)
75
76 /*
77  * We must not require too good response from the userspace daemon.
78  * Here we require the userspace daemon to send us a heartbeat
79  * char to /dev/watchdog every 30 seconds.
80  */
81
82 #define WATCHDOG_TIMEOUT 30            /* 30 sec default timeout */
83 static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
84 module_param(timeout, int, 0);
85 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
86
87
88 #ifdef CONFIG_WATCHDOG_NOWAYOUT
89 static int nowayout = 1;
90 #else
91 static int nowayout = 0;
92 #endif
93
94 module_param(nowayout, int, 0);
95 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
96
97 static void wdt_timer_ping(unsigned long);
98 static struct timer_list timer;
99 static unsigned long next_heartbeat;
100 static unsigned long wdt_is_open;
101 static char wdt_expect_close;
102 static spinlock_t wdt_spinlock;
103
104 /*
105  *      Whack the dog
106  */
107
108 static void wdt_timer_ping(unsigned long data)
109 {
110         /* If we got a heartbeat pulse within the WDT_US_INTERVAL
111          * we agree to ping the WDT
112          */
113         if(time_before(jiffies, next_heartbeat))
114         {
115                 /* Ping the WDT */
116                 spin_lock(&wdt_spinlock);
117
118                 /* Ping the WDT by reading from WDT_PING */
119                 inb_p(WDT_PING);
120
121                 /* Re-set the timer interval */
122                 timer.expires = jiffies + WDT_INTERVAL;
123                 add_timer(&timer);
124
125                 spin_unlock(&wdt_spinlock);
126
127         } else {
128                 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
129         }
130 }
131
132 /*
133  * Utility routines
134  */
135
136 static void wdt_change(int writeval)
137 {
138         unsigned long flags;
139         spin_lock_irqsave(&wdt_spinlock, flags);
140
141         /* buy some time */
142         inb_p(WDT_PING);
143
144         /* make W83877F available */
145         outb_p(ENABLE_W83877F,  ENABLE_W83877F_PORT);
146         outb_p(ENABLE_W83877F,  ENABLE_W83877F_PORT);
147
148         /* enable watchdog */
149         outb_p(WDT_REGISTER,    ENABLE_W83877F_PORT);
150         outb_p(writeval,        ENABLE_W83877F_PORT+1);
151
152         /* lock the W8387FF away */
153         outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
154
155         spin_unlock_irqrestore(&wdt_spinlock, flags);
156 }
157
158 static void wdt_startup(void)
159 {
160         next_heartbeat = jiffies + (timeout * HZ);
161
162         /* Start the timer */
163         timer.expires = jiffies + WDT_INTERVAL;
164         add_timer(&timer);
165
166         wdt_change(WDT_ENABLE);
167
168         printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
169 }
170
171 static void wdt_turnoff(void)
172 {
173         /* Stop the timer */
174         del_timer(&timer);
175
176         wdt_change(WDT_DISABLE);
177
178         printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
179 }
180
181 static void wdt_keepalive(void)
182 {
183         /* user land ping */
184         next_heartbeat = jiffies + (timeout * HZ);
185 }
186
187 /*
188  * /dev/watchdog handling
189  */
190
191 static ssize_t fop_write(struct file * file, const char * buf, size_t count, loff_t * ppos)
192 {
193         /* We can't seek */
194         if(ppos != &file->f_pos)
195                 return -ESPIPE;
196
197         /* See if we got the magic character 'V' and reload the timer */
198         if(count)
199         {
200                 if (!nowayout)
201                 {
202                         size_t ofs;
203
204                         /* note: just in case someone wrote the magic character
205                          * five months ago... */
206                         wdt_expect_close = 0;
207
208                         /* scan to see whether or not we got the magic character */
209                         for(ofs = 0; ofs != count; ofs++)
210                         {
211                                 char c;
212                                 if (get_user(c, buf + ofs))
213                                         return -EFAULT;
214                                 if (c == 'V')
215                                         wdt_expect_close = 42;
216                         }
217                 }
218
219                 /* someone wrote to us, we should restart timer */
220                 wdt_keepalive();
221         }
222         return count;
223 }
224
225 static int fop_open(struct inode * inode, struct file * file)
226 {
227         /* Just in case we're already talking to someone... */
228         if(test_and_set_bit(0, &wdt_is_open))
229                 return -EBUSY;
230
231         /* Good, fire up the show */
232         wdt_startup();
233         return 0;
234 }
235
236 static int fop_close(struct inode * inode, struct file * file)
237 {
238         if(wdt_expect_close == 42)
239                 wdt_turnoff();
240         else {
241                 del_timer(&timer);
242                 printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n");
243         }
244         clear_bit(0, &wdt_is_open);
245         wdt_expect_close = 0;
246         return 0;
247 }
248
249 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
250         unsigned long arg)
251 {
252         static struct watchdog_info ident=
253         {
254                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
255                 .firmware_version = 1,
256                 .identity = "W83877F",
257         };
258
259         switch(cmd)
260         {
261                 default:
262                         return -ENOIOCTLCMD;
263                 case WDIOC_GETSUPPORT:
264                         return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
265                 case WDIOC_GETSTATUS:
266                 case WDIOC_GETBOOTSTATUS:
267                         return put_user(0, (int *)arg);
268                 case WDIOC_KEEPALIVE:
269                         wdt_keepalive();
270                         return 0;
271                 case WDIOC_SETOPTIONS:
272                 {
273                         int new_options, retval = -EINVAL;
274
275                         if(get_user(new_options, (int *)arg))
276                                 return -EFAULT;
277
278                         if(new_options & WDIOS_DISABLECARD) {
279                                 wdt_turnoff();
280                                 retval = 0;
281                         }
282
283                         if(new_options & WDIOS_ENABLECARD) {
284                                 wdt_startup();
285                                 retval = 0;
286                         }
287
288                         return retval;
289                 }
290                 case WDIOC_SETTIMEOUT:
291                 {
292                         int new_timeout;
293
294                         if(get_user(new_timeout, (int *)arg))
295                                 return -EFAULT;
296
297                         if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */
298                                 return -EINVAL;
299
300                         timeout = new_timeout;
301                         wdt_keepalive();
302                         /* Fall through */
303                 }
304                 case WDIOC_GETTIMEOUT:
305                         return put_user(timeout, (int *)arg);
306         }
307 }
308
309 static struct file_operations wdt_fops = {
310         .owner          = THIS_MODULE,
311         .llseek         = no_llseek,
312         .write          = fop_write,
313         .open           = fop_open,
314         .release        = fop_close,
315         .ioctl          = fop_ioctl,
316 };
317
318 static struct miscdevice wdt_miscdev = {
319         .minor  = WATCHDOG_MINOR,
320         .name   = "watchdog",
321         .fops   = &wdt_fops,
322 };
323
324 /*
325  *      Notifier for system down
326  */
327
328 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
329         void *unused)
330 {
331         if(code==SYS_DOWN || code==SYS_HALT)
332                 wdt_turnoff();
333         return NOTIFY_DONE;
334 }
335
336 /*
337  *      The WDT needs to learn about soft shutdowns in order to
338  *      turn the timebomb registers off.
339  */
340
341 static struct notifier_block wdt_notifier=
342 {
343         .notifier_call = wdt_notify_sys,
344 };
345
346 static void __exit w83877f_wdt_unload(void)
347 {
348         wdt_turnoff();
349
350         /* Deregister */
351         misc_deregister(&wdt_miscdev);
352
353         unregister_reboot_notifier(&wdt_notifier);
354         release_region(WDT_PING,1);
355         release_region(ENABLE_W83877F_PORT,2);
356 }
357
358 static int __init w83877f_wdt_init(void)
359 {
360         int rc = -EBUSY;
361
362         spin_lock_init(&wdt_spinlock);
363
364         if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */
365         {
366                 timeout = WATCHDOG_TIMEOUT;
367                 printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n",
368                         timeout);
369         }
370
371         if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT"))
372         {
373                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
374                         ENABLE_W83877F_PORT);
375                 rc = -EIO;
376                 goto err_out;
377         }
378
379         if (!request_region(WDT_PING, 1, "W8387FF WDT"))
380         {
381                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
382                         WDT_PING);
383                 rc = -EIO;
384                 goto err_out_region1;
385         }
386
387         init_timer(&timer);
388         timer.function = wdt_timer_ping;
389         timer.data = 0;
390
391         rc = misc_register(&wdt_miscdev);
392         if (rc)
393         {
394                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
395                         wdt_miscdev.minor, rc);
396                 goto err_out_region2;
397         }
398
399         rc = register_reboot_notifier(&wdt_notifier);
400         if (rc)
401         {
402                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
403                         rc);
404                 goto err_out_miscdev;
405         }
406
407         printk(KERN_INFO PFX "WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n",
408                 timeout, nowayout);
409
410         return 0;
411
412 err_out_miscdev:
413         misc_deregister(&wdt_miscdev);
414 err_out_region2:
415         release_region(WDT_PING,1);
416 err_out_region1:
417         release_region(ENABLE_W83877F_PORT,2);
418 err_out:
419         return rc;
420 }
421
422 module_init(w83877f_wdt_init);
423 module_exit(w83877f_wdt_unload);
424
425 MODULE_AUTHOR("Scott and Bill Jennings");
426 MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
427 MODULE_LICENSE("GPL");
428 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);