[WATCHDOG] advantechwdt.c - cleanup before platform_device patches
[deliverable/linux.git] / drivers / char / watchdog / advantechwdt.c
CommitLineData
1da177e4
LT
1/*
2 * Advantech Single Board Computer WDT driver
3 *
4 * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
5 *
6 * Based on acquirewdt.c which is based on wdt.c.
7 * Original copyright messages:
8 *
9 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
10 * http://www.redhat.com
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 *
17 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
18 * warranty for any of this software. This material is provided
19 * "AS-IS" and at no charge.
20 *
21 * (c) Copyright 1995 Alan Cox <alan@redhat.com>
22 *
23 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
24 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
25 *
26 * 16-Oct-2002 Rob Radez <rob@osinvestor.com>
27 * Clean up ioctls, clean up init + exit, add expect close support,
28 * add wdt_start and wdt_stop as parameters.
29 */
30
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/types.h>
34#include <linux/miscdevice.h>
35#include <linux/watchdog.h>
36#include <linux/fs.h>
37#include <linux/ioport.h>
38#include <linux/notifier.h>
39#include <linux/reboot.h>
40#include <linux/init.h>
41
42#include <asm/io.h>
43#include <asm/uaccess.h>
44#include <asm/system.h>
45
1d747be6
WVS
46#define DRV_NAME "advantechwdt"
47#define PFX DRV_NAME ": "
1da177e4 48#define WATCHDOG_NAME "Advantech WDT"
1da177e4
LT
49#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
50
51static unsigned long advwdt_is_open;
52static char adv_expect_close;
53
54/*
55 * You must set these - there is no sane way to probe for this board.
56 *
57 * To enable or restart, write the timeout value in seconds (1 to 63)
58 * to I/O port wdt_start. To disable, read I/O port wdt_stop.
59 * Both are 0x443 for most boards (tested on a PCA-6276VE-00B1), but
60 * check your manual (at least the PCA-6159 seems to be different -
61 * the manual says wdt_stop is 0x43, not 0x443).
62 * (0x43 is also a write-only control register for the 8254 timer!)
63 */
64
65static int wdt_stop = 0x443;
66module_param(wdt_stop, int, 0);
67MODULE_PARM_DESC(wdt_stop, "Advantech WDT 'stop' io port (default 0x443)");
68
69static int wdt_start = 0x443;
70module_param(wdt_start, int, 0);
71MODULE_PARM_DESC(wdt_start, "Advantech WDT 'start' io port (default 0x443)");
72
73static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
74module_param(timeout, int, 0);
75MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=63, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
76
4bfdf378 77static int nowayout = WATCHDOG_NOWAYOUT;
1da177e4 78module_param(nowayout, int, 0);
1d747be6 79MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
1da177e4
LT
80
81/*
1d747be6 82 * Watchdog Operations
1da177e4
LT
83 */
84
85static void
86advwdt_ping(void)
87{
88 /* Write a watchdog value */
89 outb_p(timeout, wdt_start);
90}
91
92static void
93advwdt_disable(void)
94{
95 inb_p(wdt_stop);
96}
97
1d747be6
WVS
98/*
99 * /dev/watchdog handling
100 */
101
1da177e4
LT
102static ssize_t
103advwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
104{
105 if (count) {
106 if (!nowayout) {
107 size_t i;
108
109 adv_expect_close = 0;
110
111 for (i = 0; i != count; i++) {
112 char c;
113 if (get_user(c, buf+i))
114 return -EFAULT;
115 if (c == 'V')
116 adv_expect_close = 42;
117 }
118 }
119 advwdt_ping();
120 }
121 return count;
122}
123
124static int
125advwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
126 unsigned long arg)
127{
128 int new_timeout;
129 void __user *argp = (void __user *)arg;
130 int __user *p = argp;
131 static struct watchdog_info ident = {
132 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
133 .firmware_version = 1,
1d747be6 134 .identity = WATCHDOG_NAME,
1da177e4
LT
135 };
136
137 switch (cmd) {
138 case WDIOC_GETSUPPORT:
139 if (copy_to_user(argp, &ident, sizeof(ident)))
140 return -EFAULT;
141 break;
142
143 case WDIOC_GETSTATUS:
144 case WDIOC_GETBOOTSTATUS:
145 return put_user(0, p);
146
147 case WDIOC_KEEPALIVE:
148 advwdt_ping();
149 break;
150
151 case WDIOC_SETTIMEOUT:
152 if (get_user(new_timeout, p))
153 return -EFAULT;
154 if ((new_timeout < 1) || (new_timeout > 63))
155 return -EINVAL;
156 timeout = new_timeout;
157 advwdt_ping();
158 /* Fall */
159
160 case WDIOC_GETTIMEOUT:
161 return put_user(timeout, p);
162
163 case WDIOC_SETOPTIONS:
164 {
165 int options, retval = -EINVAL;
166
167 if (get_user(options, p))
168 return -EFAULT;
169
170 if (options & WDIOS_DISABLECARD) {
171 advwdt_disable();
172 retval = 0;
173 }
174
175 if (options & WDIOS_ENABLECARD) {
176 advwdt_ping();
177 retval = 0;
178 }
179
180 return retval;
181 }
182
183 default:
795b89d2 184 return -ENOTTY;
1da177e4
LT
185 }
186 return 0;
187}
188
189static int
190advwdt_open(struct inode *inode, struct file *file)
191{
192 if (test_and_set_bit(0, &advwdt_is_open))
193 return -EBUSY;
194 /*
195 * Activate
196 */
197
198 advwdt_ping();
199 return nonseekable_open(inode, file);
200}
201
202static int
203advwdt_close(struct inode *inode, struct file *file)
204{
205 if (adv_expect_close == 42) {
206 advwdt_disable();
207 } else {
208 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
209 advwdt_ping();
210 }
211 clear_bit(0, &advwdt_is_open);
212 adv_expect_close = 0;
213 return 0;
214}
215
216/*
217 * Notifier for system down
218 */
219
220static int
221advwdt_notify_sys(struct notifier_block *this, unsigned long code,
222 void *unused)
223{
224 if (code == SYS_DOWN || code == SYS_HALT) {
225 /* Turn the WDT off */
226 advwdt_disable();
227 }
228 return NOTIFY_DONE;
229}
230
231/*
232 * Kernel Interfaces
233 */
234
62322d25 235static const struct file_operations advwdt_fops = {
1da177e4
LT
236 .owner = THIS_MODULE,
237 .llseek = no_llseek,
238 .write = advwdt_write,
239 .ioctl = advwdt_ioctl,
240 .open = advwdt_open,
241 .release = advwdt_close,
242};
243
244static struct miscdevice advwdt_miscdev = {
1d747be6
WVS
245 .minor = WATCHDOG_MINOR,
246 .name = "watchdog",
247 .fops = &advwdt_fops,
1da177e4
LT
248};
249
250/*
251 * The WDT needs to learn about soft shutdowns in order to
252 * turn the timebomb registers off.
253 */
254
255static struct notifier_block advwdt_notifier = {
256 .notifier_call = advwdt_notify_sys,
257};
258
1d747be6
WVS
259/*
260 * Init & exit routines
261 */
262
1da177e4
LT
263static int __init
264advwdt_init(void)
265{
266 int ret;
267
268 printk(KERN_INFO "WDT driver for Advantech single board computer initialising.\n");
269
270 if (timeout < 1 || timeout > 63) {
271 timeout = WATCHDOG_TIMEOUT;
272 printk (KERN_INFO PFX "timeout value must be 1<=x<=63, using %d\n",
273 timeout);
274 }
275
276 if (wdt_stop != wdt_start) {
277 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
278 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
279 wdt_stop);
280 ret = -EIO;
281 goto out;
282 }
283 }
284
285 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
286 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
287 wdt_start);
288 ret = -EIO;
289 goto unreg_stop;
290 }
291
292 ret = register_reboot_notifier(&advwdt_notifier);
293 if (ret != 0) {
294 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
295 ret);
296 goto unreg_regions;
297 }
298
299 ret = misc_register(&advwdt_miscdev);
300 if (ret != 0) {
301 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
302 WATCHDOG_MINOR, ret);
303 goto unreg_reboot;
304 }
305
306 printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
307 timeout, nowayout);
308
309out:
310 return ret;
311unreg_reboot:
312 unregister_reboot_notifier(&advwdt_notifier);
313unreg_regions:
314 release_region(wdt_start, 1);
315unreg_stop:
316 if (wdt_stop != wdt_start)
317 release_region(wdt_stop, 1);
318 goto out;
319}
320
321static void __exit
322advwdt_exit(void)
323{
324 misc_deregister(&advwdt_miscdev);
325 unregister_reboot_notifier(&advwdt_notifier);
1d747be6 326 release_region(wdt_start,1);
1da177e4
LT
327 if(wdt_stop != wdt_start)
328 release_region(wdt_stop,1);
1da177e4
LT
329}
330
331module_init(advwdt_init);
332module_exit(advwdt_exit);
333
334MODULE_LICENSE("GPL");
335MODULE_AUTHOR("Marek Michalkiewicz <marekm@linux.org.pl>");
336MODULE_DESCRIPTION("Advantech Single Board Computer WDT driver");
337MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
This page took 0.19209 seconds and 5 git commands to generate.