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