ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / watchdog / wafer5823wdt.c
1 /*
2  *      ICP Wafer 5823 Single Board Computer WDT driver
3  *      http://www.icpamerica.com/wafer_5823.php
4  *      May also work on other similar models
5  *
6  *      (c) Copyright 2002 Justin Cormack <justin@street-vision.com>
7  *
8  *      Release 0.02
9  *
10  *      Based on advantechwdt.c which is based on wdt.c.
11  *      Original copyright messages:
12  *
13  *      (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
14  *                              http://www.redhat.com
15  *
16  *      This program is free software; you can redistribute it and/or
17  *      modify it under the terms of the GNU General Public License
18  *      as published by the Free Software Foundation; either version
19  *      2 of the License, or (at your option) any later version.
20  *
21  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
22  *      warranty for any of this software. This material is provided
23  *      "AS-IS" and at no charge.
24  *
25  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
26  *
27  */
28
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/miscdevice.h>
32 #include <linux/watchdog.h>
33 #include <linux/fs.h>
34 #include <linux/ioport.h>
35 #include <linux/notifier.h>
36 #include <linux/reboot.h>
37 #include <linux/init.h>
38 #include <linux/spinlock.h>
39 #include <asm/io.h>
40 #include <asm/uaccess.h>
41
42 #define WATCHDOG_NAME "Wafer 5823 WDT"
43 #define PFX WATCHDOG_NAME ": "
44 #define WD_TIMO 60                      /* 60 sec default timeout */
45
46 static unsigned long wafwdt_is_open;
47 static char expect_close;
48 static spinlock_t wafwdt_lock;
49
50 /*
51  *      You must set these - there is no sane way to probe for this board.
52  *
53  *      To enable, write the timeout value in seconds (1 to 255) to I/O
54  *      port WDT_START, then read the port to start the watchdog. To pat
55  *      the dog, read port WDT_STOP to stop the timer, then read WDT_START
56  *      to restart it again.
57  */
58
59 static int wdt_stop = 0x843;
60 static int wdt_start = 0x443;
61
62 static int timeout = WD_TIMO;  /* in seconds */
63 module_param(timeout, int, 0);
64 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WD_TIMO) ".");
65
66 #ifdef CONFIG_WATCHDOG_NOWAYOUT
67 static int nowayout = 1;
68 #else
69 static int nowayout = 0;
70 #endif
71
72 module_param(nowayout, int, 0);
73 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
74
75 static void wafwdt_ping(void)
76 {
77         /* pat watchdog */
78         spin_lock(&wafwdt_lock);
79         inb_p(wdt_stop);
80         inb_p(wdt_start);
81         spin_unlock(&wafwdt_lock);
82 }
83
84 static void wafwdt_start(void)
85 {
86         /* start up watchdog */
87         outb_p(timeout, wdt_start);
88         inb_p(wdt_start);
89 }
90
91 static void
92 wafwdt_stop(void)
93 {
94         /* stop watchdog */
95         inb_p(wdt_stop);
96 }
97
98 static ssize_t wafwdt_write(struct file *file, const char *buf, size_t count, loff_t * ppos)
99 {
100         /*  Can't seek (pwrite) on this device  */
101         if (ppos != &file->f_pos)
102                 return -ESPIPE;
103
104         /* See if we got the magic character 'V' and reload the timer */
105         if (count) {
106                 if (!nowayout) {
107                         size_t i;
108
109                         /* In case it was set long ago */
110                         expect_close = 0;
111
112                         /* scan to see whether or not we got the magic character */
113                         for (i = 0; i != count; i++) {
114                                 char c;
115                                 if (get_user(c, buf + i))
116                                         return -EFAULT;
117                                 if (c == 'V')
118                                         expect_close = 42;
119                         }
120                 }
121                 /* Well, anyhow someone wrote to us, we should return that favour */
122                 wafwdt_ping();
123         }
124         return count;
125 }
126
127 static int wafwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
128              unsigned long arg)
129 {
130         int new_timeout;
131         static struct watchdog_info ident = {
132                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
133                 .firmware_version = 1,
134                 .identity = "Wafer 5823 WDT",
135         };
136
137         switch (cmd) {
138         case WDIOC_GETSUPPORT:
139                 if (copy_to_user
140                     ((struct watchdog_info *) arg, &ident, sizeof (ident)))
141                         return -EFAULT;
142                 break;
143
144         case WDIOC_GETSTATUS:
145         case WDIOC_GETBOOTSTATUS:
146                 return put_user(0, (int *)arg);
147
148         case WDIOC_KEEPALIVE:
149                 wafwdt_ping();
150                 break;
151
152         case WDIOC_SETTIMEOUT:
153                 if (get_user(new_timeout, (int *)arg))
154                         return -EFAULT;
155                 if ((new_timeout < 1) || (new_timeout > 255))
156                         return -EINVAL;
157                 timeout = new_timeout;
158                 wafwdt_stop();
159                 wafwdt_start();
160                 /* Fall */
161         case WDIOC_GETTIMEOUT:
162                 return put_user(timeout, (int *)arg);
163
164         case WDIOC_SETOPTIONS:
165         {
166                 int options, retval = -EINVAL;
167
168                 if (get_user(options, (int *)arg))
169                         return -EFAULT;
170
171                 if (options & WDIOS_DISABLECARD) {
172                         wafwdt_start();
173                         retval = 0;
174                 }
175
176                 if (options & WDIOS_ENABLECARD) {
177                         wafwdt_stop();
178                         retval = 0;
179                 }
180
181                 return retval;
182         }
183
184         default:
185                 return -ENOIOCTLCMD;
186         }
187         return 0;
188 }
189
190 static int wafwdt_open(struct inode *inode, struct file *file)
191 {
192         if (test_and_set_bit(0, &wafwdt_is_open))
193                 return -EBUSY;
194
195         /*
196          *      Activate
197          */
198         wafwdt_start();
199         return 0;
200 }
201
202 static int
203 wafwdt_close(struct inode *inode, struct file *file)
204 {
205         if (expect_close == 42) {
206                 wafwdt_stop();
207         } else {
208                 printk(KERN_CRIT PFX "WDT device closed unexpectedly.  WDT will not stop!\n");
209                 wafwdt_ping();
210         }
211         clear_bit(0, &wafwdt_is_open);
212         expect_close = 0;
213         return 0;
214 }
215
216 /*
217  *      Notifier for system down
218  */
219
220 static int wafwdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
221 {
222         if (code == SYS_DOWN || code == SYS_HALT) {
223                 /* Turn the WDT off */
224                 wafwdt_stop();
225         }
226         return NOTIFY_DONE;
227 }
228
229 /*
230  *      Kernel Interfaces
231  */
232
233 static struct file_operations wafwdt_fops = {
234         .owner          = THIS_MODULE,
235         .llseek         = no_llseek,
236         .write          = wafwdt_write,
237         .ioctl          = wafwdt_ioctl,
238         .open           = wafwdt_open,
239         .release        = wafwdt_close,
240 };
241
242 static struct miscdevice wafwdt_miscdev = {
243         .minor  = WATCHDOG_MINOR,
244         .name   = "watchdog",
245         .fops   = &wafwdt_fops,
246 };
247
248 /*
249  *      The WDT needs to learn about soft shutdowns in order to
250  *      turn the timebomb registers off.
251  */
252
253 static struct notifier_block wafwdt_notifier = {
254         .notifier_call = wafwdt_notify_sys,
255 };
256
257 static int __init wafwdt_init(void)
258 {
259         int ret;
260
261         printk(KERN_INFO "WDT driver for Wafer 5823 single board computer initialising.\n");
262
263         spin_lock_init(&wafwdt_lock);
264
265         if (timeout < 1 || timeout > 255) {
266                 timeout = WD_TIMO;
267                 printk (KERN_INFO PFX "timeout value must be 1<=x<=255, using %d\n",
268                         timeout);
269         }
270
271         if (wdt_stop != wdt_start) {
272                 if(!request_region(wdt_stop, 1, "Wafer 5823 WDT")) {
273                         printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
274                         wdt_stop);
275                         ret = -EIO;
276                         goto error;
277                 }
278         }
279
280         if(!request_region(wdt_start, 1, "Wafer 5823 WDT")) {
281                 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
282                         wdt_start);
283                 ret = -EIO;
284                 goto error2;
285         }
286
287         ret = register_reboot_notifier(&wafwdt_notifier);
288         if (ret != 0) {
289                 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
290                         ret);
291                 goto error3;
292         }
293
294         ret = misc_register(&wafwdt_miscdev);
295         if (ret != 0) {
296                 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
297                         WATCHDOG_MINOR, ret);
298                 goto error4;
299         }
300
301         printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
302                 timeout, nowayout);
303
304         return ret;
305 error4:
306         unregister_reboot_notifier(&wafwdt_notifier);
307 error3:
308         release_region(wdt_start, 1);
309 error2:
310         if (wdt_stop != wdt_start)
311                 release_region(wdt_stop, 1);
312 error:
313         return ret;
314 }
315
316 static void __exit wafwdt_exit(void)
317 {
318         misc_deregister(&wafwdt_miscdev);
319         unregister_reboot_notifier(&wafwdt_notifier);
320         if(wdt_stop != wdt_start)
321                 release_region(wdt_stop, 1);
322         release_region(wdt_start, 1);
323 }
324
325 module_init(wafwdt_init);
326 module_exit(wafwdt_exit);
327
328 MODULE_AUTHOR("Justin Cormack");
329 MODULE_DESCRIPTION("ICP Wafer 5823 Single Board Computer WDT driver");
330 MODULE_LICENSE("GPL");
331 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
332
333 /* end of wafer5823wdt.c */