This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / char / watchdog / acquirewdt.c
1 /*
2  *      Acquire Single Board Computer Watchdog Timer driver
3  *
4  *      Based on wdt.c. Original copyright messages:
5  *
6  *      (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
7  *                              http://www.redhat.com
8  *
9  *      This program is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU General Public License
11  *      as published by the Free Software Foundation; either version
12  *      2 of the License, or (at your option) any later version.
13  *
14  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
15  *      warranty for any of this software. This material is provided
16  *      "AS-IS" and at no charge.
17  *
18  *      (c) Copyright 1995    Alan Cox <alan@redhat.com>
19  *
20  *      14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
21  *          Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
22  *          Can't add timeout - driver doesn't allow changing value
23  */
24
25 /*
26  *      Theory of Operation:
27  *              The Watch-Dog Timer is provided to ensure that standalone
28  *              Systems can always recover from catastrophic conditions that
29  *              caused the CPU to crash. This condition may have occured by
30  *              external EMI or a software bug. When the CPU stops working
31  *              correctly, hardware on the board will either perform a hardware
32  *              reset (cold boot) or a non-maskable interrupt (NMI) to bring the
33  *              system back to a known state.
34  *
35  *              The Watch-Dog Timer is controlled by two I/O Ports.
36  *                443 hex       - Read  - Enable or refresh the Watch-Dog Timer
37  *                043 hex       - Read  - Disable the Watch-Dog Timer
38  *
39  *              To enable the Watch-Dog Timer, a read from I/O port 443h must
40  *              be performed. This will enable and activate the countdown timer
41  *              which will eventually time out and either reset the CPU or cause
42  *              an NMI depending on the setting of a jumper. To ensure that this
43  *              reset condition does not occur, the Watch-Dog Timer must be
44  *              periodically refreshed by reading the same I/O port 443h.
45  *              The Watch-Dog Timer is disabled by reading I/O port 043h.
46  *
47  *              The Watch-Dog Timer Time-Out Period is set via jumpers.
48  *              It can be 1, 2, 10, 20, 110 or 220 seconds.
49  */
50
51 #include <linux/module.h>
52 #include <linux/moduleparam.h>
53 #include <linux/types.h>
54 #include <linux/miscdevice.h>
55 #include <linux/watchdog.h>
56 #include <linux/fs.h>
57 #include <linux/ioport.h>
58 #include <linux/notifier.h>
59 #include <linux/reboot.h>
60 #include <linux/init.h>
61
62 #include <asm/io.h>
63 #include <asm/uaccess.h>
64 #include <asm/system.h>
65
66 #define WATCHDOG_NAME "Acquire WDT"
67 #define PFX WATCHDOG_NAME ": "
68 #define WATCHDOG_HEARTBEAT 0    /* There is no way to see what the correct time-out period is */
69
70 static unsigned long acq_is_open;
71 static char expect_close;
72
73 /*
74  *      You must set these - there is no sane way to probe for this board.
75  */
76
77 static int wdt_stop = 0x43;
78 module_param(wdt_stop, int, 0);
79 MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
80
81 static int wdt_start = 0x443;
82 module_param(wdt_start, int, 0);
83 MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
84
85 #ifdef CONFIG_WATCHDOG_NOWAYOUT
86 static int nowayout = 1;
87 #else
88 static int nowayout = 0;
89 #endif
90
91 module_param(nowayout, int, 0);
92 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
93
94 /*
95  *      Kernel methods.
96  */
97
98 static void acq_keepalive(void)
99 {
100         /* Write a watchdog value */
101         inb_p(wdt_start);
102 }
103
104 static void acq_stop(void)
105 {
106         /* Turn the card off */
107         inb_p(wdt_stop);
108 }
109
110 /*
111  *      /dev/watchdog handling.
112  */
113
114 static ssize_t acq_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
115 {
116         /*  Can't seek (pwrite) on this device  */
117         if (ppos != &file->f_pos)
118                 return -ESPIPE;
119
120         /* See if we got the magic character 'V' and reload the timer */
121         if(count) {
122                 if (!nowayout) {
123                         size_t i;
124
125                         /* note: just in case someone wrote the magic character
126                          * five months ago... */
127                         expect_close = 0;
128
129                         /* scan to see whether or not we got the magic character */
130                         for (i = 0; i != count; i++) {
131                                 char c;
132                                 if (get_user(c, buf + i))
133                                         return -EFAULT;
134                                 if (c == 'V')
135                                         expect_close = 42;
136                         }
137                 }
138
139                 /* Well, anyhow someone wrote to us, we should return that favour */
140                 acq_keepalive();
141         }
142         return count;
143 }
144
145 static int acq_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
146         unsigned long arg)
147 {
148         int options, retval = -EINVAL;
149         void __user *argp = (void __user *)arg;
150         int __user *p = argp;
151         static struct watchdog_info ident =
152         {
153                 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
154                 .firmware_version = 1,
155                 .identity = "Acquire WDT",
156         };
157
158         switch(cmd)
159         {
160         case WDIOC_GETSUPPORT:
161           return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
162
163         case WDIOC_GETSTATUS:
164         case WDIOC_GETBOOTSTATUS:
165           return put_user(0, p);
166
167         case WDIOC_KEEPALIVE:
168           acq_keepalive();
169           return 0;
170
171         case WDIOC_GETTIMEOUT:
172           return put_user(WATCHDOG_HEARTBEAT, p);
173
174         case WDIOC_SETOPTIONS:
175         {
176             if (get_user(options, p))
177               return -EFAULT;
178
179             if (options & WDIOS_DISABLECARD)
180             {
181               acq_stop();
182               retval = 0;
183             }
184
185             if (options & WDIOS_ENABLECARD)
186             {
187               acq_keepalive();
188               retval = 0;
189             }
190
191             return retval;
192         }
193
194         default:
195           return -ENOIOCTLCMD;
196         }
197 }
198
199 static int acq_open(struct inode *inode, struct file *file)
200 {
201         if (test_and_set_bit(0, &acq_is_open))
202                 return -EBUSY;
203
204         if (nowayout)
205                 __module_get(THIS_MODULE);
206
207         /* Activate */
208         acq_keepalive();
209         return 0;
210 }
211
212 static int acq_close(struct inode *inode, struct file *file)
213 {
214         if (expect_close == 42) {
215                 acq_stop();
216         } else {
217                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
218                 acq_keepalive();
219         }
220         clear_bit(0, &acq_is_open);
221         expect_close = 0;
222         return 0;
223 }
224
225 /*
226  *      Notifier for system down
227  */
228
229 static int acq_notify_sys(struct notifier_block *this, unsigned long code,
230         void *unused)
231 {
232         if(code==SYS_DOWN || code==SYS_HALT) {
233                 /* Turn the WDT off */
234                 acq_stop();
235         }
236         return NOTIFY_DONE;
237 }
238
239 /*
240  *      Kernel Interfaces
241  */
242
243 static struct file_operations acq_fops = {
244         .owner          = THIS_MODULE,
245         .llseek         = no_llseek,
246         .write          = acq_write,
247         .ioctl          = acq_ioctl,
248         .open           = acq_open,
249         .release        = acq_close,
250 };
251
252 static struct miscdevice acq_miscdev=
253 {
254         .minor = WATCHDOG_MINOR,
255         .name = "watchdog",
256         .fops = &acq_fops,
257 };
258
259 /*
260  *      The WDT card needs to learn about soft shutdowns in order to
261  *      turn the timebomb registers off.
262  */
263
264 static struct notifier_block acq_notifier =
265 {
266         .notifier_call = acq_notify_sys,
267 };
268
269 static int __init acq_init(void)
270 {
271         int ret;
272
273         printk(KERN_INFO "WDT driver for Acquire single board computer initialising.\n");
274
275         if (wdt_stop != wdt_start) {
276                 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
277                         printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
278                                 wdt_stop);
279                         ret = -EIO;
280                         goto out;
281                 }
282         }
283
284         if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
285                 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
286                         wdt_start);
287                 ret = -EIO;
288                 goto unreg_stop;
289         }
290
291         ret = register_reboot_notifier(&acq_notifier);
292         if (ret != 0) {
293                 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
294                         ret);
295                 goto unreg_regions;
296         }
297
298         ret = misc_register(&acq_miscdev);
299         if (ret != 0) {
300                 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
301                         WATCHDOG_MINOR, ret);
302                 goto unreg_reboot;
303         }
304
305         printk (KERN_INFO PFX "initialized. (nowayout=%d)\n",
306                 nowayout);
307
308         return 0;
309
310 unreg_reboot:
311         unregister_reboot_notifier(&acq_notifier);
312 unreg_regions:
313         release_region(wdt_start, 1);
314 unreg_stop:
315         if (wdt_stop != wdt_start)
316                 release_region(wdt_stop, 1);
317 out:
318         return ret;
319 }
320
321 static void __exit acq_exit(void)
322 {
323         misc_deregister(&acq_miscdev);
324         unregister_reboot_notifier(&acq_notifier);
325         if(wdt_stop != wdt_start)
326                 release_region(wdt_stop,1);
327         release_region(wdt_start,1);
328 }
329
330 module_init(acq_init);
331 module_exit(acq_exit);
332
333 MODULE_AUTHOR("David Woodhouse");
334 MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
335 MODULE_LICENSE("GPL");
336 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);