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