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