OSDN Git Service

[PATCH] wdt83627: fix wdt_init() return code
[linux-kernel-docs/linux-2.4.36.git] / drivers / char / wdt83627.c
1 /*
2  *      w83627hf WDT driver
3  *
4  *      backported from w83627hf_wdt.c kernel 2.6.20.1
5  *      (c) Copyright 2007 Orpak Systems Ltd. (Tal Kelrich <tal@orpak.com>)
6  *
7  *      (c) Copyright 2003 Padraig Brady <P@draigBrady.com>
8  *
9  *      Based on advantechwdt.c which is based on wdt.c.
10  *      Original copyright messages:
11  *
12  *      (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
13  *
14  *      (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
15  *                              http://www.redhat.com
16  *
17  *      This program is free software; you can redistribute it and/or
18  *      modify it under the terms of the GNU General Public License
19  *      as published by the Free Software Foundation; either version
20  *      2 of the License, or (at your option) any later version.
21  *
22  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
23  *      warranty for any of this software. This material is provided
24  *      "AS-IS" and at no charge.
25  *
26  *      (c) Copyright 1995    Alan Cox <alan@redhat.com>
27  */
28
29 #include <linux/module.h>
30 #include <linux/types.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 <linux/proc_fs.h>
40
41 #include <asm/io.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <asm/page.h>
45
46 #define WATCHDOG_NAME "W83627HF WDT"
47 #define PFX WATCHDOG_NAME ": "
48 #define WATCHDOG_TIMEOUT 120            /* 120 sec default timeout */
49
50 #ifdef CONFIG_WATCHDOG_NOWAYOUT
51 #define WATCHDOG_NOWAYOUT 1
52 #else
53 #define WATCHDOG_NOWAYOUT 0
54 #endif
55
56 static unsigned long wdt_is_open;
57 static char expect_close;
58 static spinlock_t io_lock;
59
60 /* You must set this - there is no sane way to probe for this board. */
61 static int wdt_io = 0x2E;
62 MODULE_PARM(wdt_io, "i");
63 MODULE_PARM_DESC(wdt_io, "w83627hf WDT io port (default 0x2E)");
64
65 static int timeout = WATCHDOG_TIMEOUT;  /* in seconds */
66 MODULE_PARM(timeout, "i");
67 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=255, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
68
69 static int nowayout = WATCHDOG_NOWAYOUT;
70 MODULE_PARM(nowayout, "i");
71 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(CONFIG_WATCHDOG_NOWAYOUT)")");
72
73 /*
74  *      Kernel methods.
75  */
76
77 #define WDT_EFER (wdt_io+0)   /* Extended Function Enable Registers */
78 #define WDT_EFIR (wdt_io+0)   /* Extended Function Index Register (same as EFER) */
79 #define WDT_EFDR (WDT_EFIR+1) /* Extended Function Data Register */
80
81 /* Non standard proc bits, added by request, wanted some feedback */
82
83 static void
84 w83627hf_select_wd_register(void)
85 {
86         outb_p(0x87, WDT_EFER); /* Enter extended function mode */
87         outb_p(0x87, WDT_EFER); /* Again according to manual */
88
89         outb_p(0x07, WDT_EFER); /* point to logical device number reg */
90         outb_p(0x08, WDT_EFDR); /* select logical device 8 (GPIO2) */
91         outb_p(0x30, WDT_EFER); /* select CR30 */
92         outb_p(0x01, WDT_EFDR); /* set bit 0 to activate GPIO2 */
93 }
94
95 static void
96 w83627hf_unselect_wd_register(void)
97 {
98         outb_p(0xAA, WDT_EFER); /* Leave extended function mode */
99 }
100
101 /* tyan motherboards seem to set F5 to 0x4C ?
102  * So explicitly init to appropriate value. */
103 static void
104 w83627hf_init(void)
105 {
106         unsigned char t;
107
108         w83627hf_select_wd_register();
109
110         outb_p(0xF6, WDT_EFER); /* Select CRF6 */
111         outb_p(0, WDT_EFDR);    /* set to 0 initially */
112         outb_p(0xF5, WDT_EFER); /* Select CRF5 */
113         t=inb_p(WDT_EFDR);      /* read CRF5 */
114         t&=~0x0C;               /* set second mode & disable keyboard turning off watchdog */
115         outb_p(t, WDT_EFDR);    /* Write back to CRF5 */
116
117         w83627hf_unselect_wd_register();
118 }
119
120 static int wdt_readproc(char *page, char **start, off_t off, int count,
121                 int *eof, void *data)
122 {
123         int len;
124         unsigned char remaining;
125         unsigned char fired;
126         spin_lock(&io_lock);
127         w83627hf_select_wd_register();
128         outb_p(0xF6, WDT_EFIR);/* get current timer val */
129         remaining=inb_p(WDT_EFDR);
130         outb_p(0xF7, WDT_EFIR);
131         fired=inb_p(WDT_EFDR);
132         /* clear that bit (bit 4) */
133         outb_p(fired&(~0x10),WDT_EFDR);
134         w83627hf_unselect_wd_register();
135         spin_unlock(&io_lock);
136         fired=(fired&0x10)!=0;
137         len=snprintf(page,PAGE_SIZE,
138                         "W83627HF WDT\n"
139                         "active=%d\n"
140                         "iobase=%04X\n"
141                         "nowayout=%d\n"
142                         "timeout=%d\n"
143                         "remaining=%d\n"
144                         "fired=%d\n",
145                         wdt_is_open,wdt_io,nowayout,timeout,remaining,fired);
146         *eof=1;
147         return len;
148 }
149
150 static void
151 wdt_ctrl(int timeout)
152 {
153         spin_lock(&io_lock);
154         
155         w83627hf_select_wd_register();
156
157         outb_p(0xF6, WDT_EFER);    /* Select CRF6 */
158         outb_p(timeout, WDT_EFDR); /* Write Timeout counter to CRF6 */
159
160         w83627hf_unselect_wd_register();
161
162         spin_unlock(&io_lock);
163 }
164
165 static int
166 wdt_ping(void)
167 {
168         wdt_ctrl(timeout);
169         return 0;
170 }
171
172 static int
173 wdt_disable(void)
174 {
175         wdt_ctrl(0);
176         return 0;
177 }
178
179 static int
180 wdt_set_heartbeat(int t)
181 {
182         if ((t < 1) || (t > 255))
183                 return -EINVAL;
184
185         timeout = t;
186         return 0;
187 }
188
189 static ssize_t
190 wdt_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
191 {
192         /*  Can't seek (pwrite) on this device  */
193         if (ppos != &file->f_pos)
194                 return -ESPIPE;
195         if (count) {
196                 if (!nowayout) {
197                         size_t i;
198
199                         expect_close = 0;
200
201                         for (i = 0; i != count; i++) {
202                                 char c;
203                                 if (get_user(c, buf+i))
204                                         return -EFAULT;
205                                 if (c == 'V')
206                                         expect_close = 42;
207                         }
208                 }
209                 wdt_ping();
210         }
211         return count;
212 }
213
214 static int
215 wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
216           unsigned long arg)
217 {
218         void *argp = (void *)arg;
219         int *p = argp;
220         int new_timeout;
221         static struct watchdog_info ident = {
222                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
223                 .firmware_version = 1,
224                 .identity = "W83627HF WDT",
225         };
226
227         switch (cmd) {
228         case WDIOC_GETSUPPORT:
229           if (copy_to_user(argp, &ident, sizeof(ident)))
230             return -EFAULT;
231           break;
232
233         case WDIOC_GETSTATUS:
234         case WDIOC_GETBOOTSTATUS:
235           return put_user(0, p);
236
237         case WDIOC_KEEPALIVE:
238           wdt_ping();
239           break;
240
241         case WDIOC_SETTIMEOUT:
242           if (get_user(new_timeout, p))
243                   return -EFAULT;
244           if (wdt_set_heartbeat(new_timeout))
245                   return -EINVAL;
246           wdt_ping();
247           /* Fall */
248
249         case WDIOC_GETTIMEOUT:
250           return put_user(timeout, p);
251
252         case WDIOC_SETOPTIONS:
253         {
254           int options, retval = -EINVAL;
255
256           if (get_user(options, p))
257             return -EFAULT;
258
259           if (options & WDIOS_DISABLECARD) {
260             wdt_disable();
261             retval = 0;
262           }
263
264           if (options & WDIOS_ENABLECARD) {
265             wdt_ping();
266             retval = 0;
267           }
268
269           return retval;
270         }
271
272         default:
273           return -ENOTTY;
274         }
275         return 0;
276 }
277
278 static int
279 wdt_open(struct inode *inode, struct file *file)
280 {
281         if (test_and_set_bit(0, &wdt_is_open))
282                 return -EBUSY;
283         /*
284          *      Activate
285          */
286
287         wdt_ping();
288         return 0;
289 }
290
291 static int
292 wdt_close(struct inode *inode, struct file *file)
293 {
294         if (expect_close == 42) {
295                 wdt_disable();
296         } else {
297                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
298                 wdt_ping();
299         }
300         expect_close = 0;
301         clear_bit(0, &wdt_is_open);
302         return 0;
303 }
304
305 /*
306  *      Notifier for system down
307  */
308
309 static int
310 wdt_notify_sys(struct notifier_block *this, unsigned long code,
311         void *unused)
312 {
313         if (code == SYS_DOWN || code == SYS_HALT) {
314                 /* Turn the WDT off */
315                 wdt_disable();
316         }
317         return NOTIFY_DONE;
318 }
319
320 /*
321  *      Kernel Interfaces
322  */
323
324 static const struct file_operations wdt_fops = {
325         .owner          = THIS_MODULE,
326         .write          = wdt_write,
327         .ioctl          = wdt_ioctl,
328         .open           = wdt_open,
329         .release        = wdt_close,
330 };
331
332 static struct miscdevice wdt_miscdev = {
333         .minor = WATCHDOG_MINOR,
334         .name = "watchdog",
335         .fops = &wdt_fops,
336 };
337
338 /*
339  *      The WDT needs to learn about soft shutdowns in order to
340  *      turn the timebomb registers off.
341  */
342
343 static struct notifier_block wdt_notifier = {
344         .notifier_call = wdt_notify_sys,
345 };
346
347 static int __init
348 wdt_init(void)
349 {
350         int ret = -EBUSY;
351
352         spin_lock_init(&io_lock);
353
354         printk(KERN_INFO "WDT driver for the Winbond(TM) W83627HF Super I/O chip initialising.\n");
355
356         if (wdt_set_heartbeat(timeout)) {
357                 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
358                 printk (KERN_INFO PFX "timeout value must be 1<=timeout<=63, using %d\n",
359                         WATCHDOG_TIMEOUT);
360         }
361         /* on the board I was working on, wdt_io consistently coincided with things already
362          * being used, hence the continuing here.
363          * there's probably a better solution to this */
364         if (!request_region(wdt_io, 1, WATCHDOG_NAME)) {
365                 printk (KERN_WARNING PFX "I/O address 0x%04x already in use, continuing anyway\n",
366                         wdt_io);
367         }
368         if(!create_proc_read_entry("watchdog",0,NULL,&wdt_readproc,NULL))
369                 goto unreg_regions;
370
371         w83627hf_init();
372
373         ret = register_reboot_notifier(&wdt_notifier);
374         if (ret != 0) {
375                 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
376                         ret);
377                 goto unreg_proc;
378         }
379
380         ret = misc_register(&wdt_miscdev);
381         if (ret != 0) {
382                 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
383                         WATCHDOG_MINOR, ret);
384                 goto unreg_reboot;
385         }
386
387         printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
388                 timeout, nowayout);
389
390 out:
391         return ret;
392 unreg_reboot:
393         unregister_reboot_notifier(&wdt_notifier);
394 unreg_proc:
395         remove_proc_entry("watchdog",NULL);
396 unreg_regions:
397         release_region(wdt_io, 1);
398         goto out;
399 }
400
401 static void __exit
402 wdt_exit(void)
403 {
404         remove_proc_entry("watchdog",NULL);
405         misc_deregister(&wdt_miscdev);
406         unregister_reboot_notifier(&wdt_notifier);
407         release_region(wdt_io,1);
408         /* we might not have gotten it... is this safe? */
409 }
410
411 module_init(wdt_init);
412 module_exit(wdt_exit);
413
414 MODULE_LICENSE("GPL");
415 MODULE_AUTHOR("Padraig Brady <P@draigBrady.com>");
416 MODULE_DESCRIPTION("w83627hf WDT driver");