1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52
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>
65#include <linux/io.h>
66#include <linux/uaccess.h>
67
68
69#define OUR_NAME "sbc60xxwdt"
70#define PFX OUR_NAME ": "
71
72
73
74
75
76static int wdt_stop = 0x45;
77module_param(wdt_stop, int, 0);
78MODULE_PARM_DESC(wdt_stop, "SBC60xx WDT 'stop' io port (default 0x45)");
79
80static int wdt_start = 0x443;
81module_param(wdt_start, int, 0);
82MODULE_PARM_DESC(wdt_start, "SBC60xx WDT 'start' io port (default 0x443)");
83
84
85
86
87
88
89
90#define WDT_INTERVAL (HZ/4+1)
91
92
93
94
95
96
97
98
99
100
101#define WATCHDOG_TIMEOUT 30
102static int timeout = WATCHDOG_TIMEOUT;
103
104module_param(timeout, int, 0);
105MODULE_PARM_DESC(timeout,
106 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
107 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
108
109static bool nowayout = WATCHDOG_NOWAYOUT;
110module_param(nowayout, bool, 0);
111MODULE_PARM_DESC(nowayout,
112 "Watchdog cannot be stopped once started (default="
113 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
114
115static void wdt_timer_ping(unsigned long);
116static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
117static unsigned long next_heartbeat;
118static unsigned long wdt_is_open;
119static char wdt_expect_close;
120
121
122
123
124
125static void wdt_timer_ping(unsigned long data)
126{
127
128
129
130 if (time_before(jiffies, next_heartbeat)) {
131
132 inb_p(wdt_start);
133
134 mod_timer(&timer, jiffies + WDT_INTERVAL);
135 } else
136 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
137}
138
139
140
141
142
143static void wdt_startup(void)
144{
145 next_heartbeat = jiffies + (timeout * HZ);
146
147
148 mod_timer(&timer, jiffies + WDT_INTERVAL);
149 pr_info("Watchdog timer is now enabled\n");
150}
151
152static void wdt_turnoff(void)
153{
154
155 del_timer(&timer);
156 inb_p(wdt_stop);
157 pr_info("Watchdog timer is now disabled...\n");
158}
159
160static void wdt_keepalive(void)
161{
162
163 next_heartbeat = jiffies + (timeout * HZ);
164}
165
166
167
168
169
170static ssize_t fop_write(struct file *file, const char __user *buf,
171 size_t count, loff_t *ppos)
172{
173
174 if (count) {
175 if (!nowayout) {
176 size_t ofs;
177
178
179
180 wdt_expect_close = 0;
181
182
183
184 for (ofs = 0; ofs != count; ofs++) {
185 char c;
186 if (get_user(c, buf + ofs))
187 return -EFAULT;
188 if (c == 'V')
189 wdt_expect_close = 42;
190 }
191 }
192
193
194
195 wdt_keepalive();
196 }
197 return count;
198}
199
200static int fop_open(struct inode *inode, struct file *file)
201{
202
203 if (test_and_set_bit(0, &wdt_is_open))
204 return -EBUSY;
205
206 if (nowayout)
207 __module_get(THIS_MODULE);
208
209
210 wdt_startup();
211 return nonseekable_open(inode, file);
212}
213
214static int fop_close(struct inode *inode, struct file *file)
215{
216 if (wdt_expect_close == 42)
217 wdt_turnoff();
218 else {
219 del_timer(&timer);
220 pr_crit("device file closed unexpectedly. Will not stop the WDT!\n");
221 }
222 clear_bit(0, &wdt_is_open);
223 wdt_expect_close = 0;
224 return 0;
225}
226
227static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
228{
229 void __user *argp = (void __user *)arg;
230 int __user *p = argp;
231 static const struct watchdog_info ident = {
232 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
233 WDIOF_MAGICCLOSE,
234 .firmware_version = 1,
235 .identity = "SBC60xx",
236 };
237
238 switch (cmd) {
239 case WDIOC_GETSUPPORT:
240 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
241 case WDIOC_GETSTATUS:
242 case WDIOC_GETBOOTSTATUS:
243 return put_user(0, p);
244 case WDIOC_SETOPTIONS:
245 {
246 int new_options, retval = -EINVAL;
247 if (get_user(new_options, p))
248 return -EFAULT;
249 if (new_options & WDIOS_DISABLECARD) {
250 wdt_turnoff();
251 retval = 0;
252 }
253 if (new_options & WDIOS_ENABLECARD) {
254 wdt_startup();
255 retval = 0;
256 }
257 return retval;
258 }
259 case WDIOC_KEEPALIVE:
260 wdt_keepalive();
261 return 0;
262 case WDIOC_SETTIMEOUT:
263 {
264 int new_timeout;
265 if (get_user(new_timeout, p))
266 return -EFAULT;
267
268 if (new_timeout < 1 || new_timeout > 3600)
269 return -EINVAL;
270
271 timeout = new_timeout;
272 wdt_keepalive();
273
274 }
275 case WDIOC_GETTIMEOUT:
276 return put_user(timeout, p);
277 default:
278 return -ENOTTY;
279 }
280}
281
282static const struct file_operations wdt_fops = {
283 .owner = THIS_MODULE,
284 .llseek = no_llseek,
285 .write = fop_write,
286 .open = fop_open,
287 .release = fop_close,
288 .unlocked_ioctl = fop_ioctl,
289};
290
291static struct miscdevice wdt_miscdev = {
292 .minor = WATCHDOG_MINOR,
293 .name = "watchdog",
294 .fops = &wdt_fops,
295};
296
297
298
299
300
301static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
302 void *unused)
303{
304 if (code == SYS_DOWN || code == SYS_HALT)
305 wdt_turnoff();
306 return NOTIFY_DONE;
307}
308
309
310
311
312
313
314static struct notifier_block wdt_notifier = {
315 .notifier_call = wdt_notify_sys,
316};
317
318static void __exit sbc60xxwdt_unload(void)
319{
320 wdt_turnoff();
321
322
323 misc_deregister(&wdt_miscdev);
324
325 unregister_reboot_notifier(&wdt_notifier);
326 if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
327 release_region(wdt_stop, 1);
328 release_region(wdt_start, 1);
329}
330
331static int __init sbc60xxwdt_init(void)
332{
333 int rc = -EBUSY;
334
335 if (timeout < 1 || timeout > 3600) {
336 timeout = WATCHDOG_TIMEOUT;
337 pr_info("timeout value must be 1 <= x <= 3600, using %d\n",
338 timeout);
339 }
340
341 if (!request_region(wdt_start, 1, "SBC 60XX WDT")) {
342 pr_err("I/O address 0x%04x already in use\n", wdt_start);
343 rc = -EIO;
344 goto err_out;
345 }
346
347
348 if (wdt_stop != 0x45 && wdt_stop != wdt_start) {
349 if (!request_region(wdt_stop, 1, "SBC 60XX WDT")) {
350 pr_err("I/O address 0x%04x already in use\n", wdt_stop);
351 rc = -EIO;
352 goto err_out_region1;
353 }
354 }
355
356 rc = register_reboot_notifier(&wdt_notifier);
357 if (rc) {
358 pr_err("cannot register reboot notifier (err=%d)\n", rc);
359 goto err_out_region2;
360 }
361
362 rc = misc_register(&wdt_miscdev);
363 if (rc) {
364 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
365 wdt_miscdev.minor, rc);
366 goto err_out_reboot;
367 }
368 pr_info("WDT driver for 60XX single board computer initialised. timeout=%d sec (nowayout=%d)\n",
369 timeout, nowayout);
370
371 return 0;
372
373err_out_reboot:
374 unregister_reboot_notifier(&wdt_notifier);
375err_out_region2:
376 if (wdt_stop != 0x45 && wdt_stop != wdt_start)
377 release_region(wdt_stop, 1);
378err_out_region1:
379 release_region(wdt_start, 1);
380err_out:
381 return rc;
382}
383
384module_init(sbc60xxwdt_init);
385module_exit(sbc60xxwdt_unload);
386
387MODULE_AUTHOR("Jakob Oestergaard <jakob@unthought.net>");
388MODULE_DESCRIPTION("60xx Single Board Computer Watchdog Timer driver");
389MODULE_LICENSE("GPL");
390