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#include <linux/interrupt.h>
49#include <linux/module.h>
50#include <linux/moduleparam.h>
51#include <linux/types.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
64static unsigned long eurwdt_is_open;
65static int eurwdt_timeout;
66static char eur_expect_close;
67
68
69
70
71
72
73static int io = 0x3f0;
74static int irq = 10;
75static char *ev = "int";
76
77#define WDT_TIMEOUT 60
78
79static int nowayout = WATCHDOG_NOWAYOUT;
80module_param(nowayout, int, 0);
81MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
82
83
84
85
86
87#define WDT_CTRL_REG 0x30
88#define WDT_OUTPIN_CFG 0xe2
89#define WDT_EVENT_INT 0x00
90#define WDT_EVENT_REBOOT 0x08
91#define WDT_UNIT_SEL 0xf1
92#define WDT_UNIT_SECS 0x80
93#define WDT_TIMEOUT_VAL 0xf2
94#define WDT_TIMER_CFG 0xf3
95
96
97module_param(io, int, 0);
98MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)");
99module_param(irq, int, 0);
100MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)");
101module_param(ev, charp, 0);
102MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')");
103
104
105
106
107
108
109static inline void eurwdt_write_reg(u8 index, u8 data)
110{
111 outb(index, io);
112 outb(data, io+1);
113}
114
115static inline void eurwdt_lock_chip(void)
116{
117 outb(0xaa, io);
118}
119
120static inline void eurwdt_unlock_chip(void)
121{
122 outb(0x55, io);
123 eurwdt_write_reg(0x07, 0x08);
124}
125
126static inline void eurwdt_set_timeout(int timeout)
127{
128 eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout);
129}
130
131static inline void eurwdt_disable_timer(void)
132{
133 eurwdt_set_timeout(0);
134}
135
136static void eurwdt_activate_timer(void)
137{
138 eurwdt_disable_timer();
139 eurwdt_write_reg(WDT_CTRL_REG, 0x01);
140 eurwdt_write_reg(WDT_OUTPIN_CFG, !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT);
141
142
143 if (irq == 2 || irq > 15 || irq < 0) {
144 printk(KERN_ERR ": invalid irq number\n");
145 irq = 0;
146 }
147 if (irq == 0)
148 printk(KERN_INFO ": interrupt disabled\n");
149
150 eurwdt_write_reg(WDT_TIMER_CFG, irq<<4);
151
152 eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS);
153 eurwdt_set_timeout(0);
154}
155
156
157
158
159
160
161static irqreturn_t eurwdt_interrupt(int irq, void *dev_id)
162{
163 printk(KERN_CRIT "timeout WDT timeout\n");
164
165#ifdef ONLY_TESTING
166 printk(KERN_CRIT "Would Reboot.\n");
167#else
168 printk(KERN_CRIT "Initiating system reboot.\n");
169 emergency_restart();
170#endif
171 return IRQ_HANDLED;
172}
173
174
175
176
177
178
179
180
181static void eurwdt_ping(void)
182{
183
184 eurwdt_set_timeout(eurwdt_timeout);
185}
186
187
188
189
190
191
192
193
194
195
196
197
198static ssize_t eurwdt_write(struct file *file, const char __user *buf,
199size_t count, loff_t *ppos)
200{
201 if (count) {
202 if (!nowayout) {
203 size_t i;
204
205 eur_expect_close = 0;
206
207 for (i = 0; i != count; i++) {
208 char c;
209 if(get_user(c, buf+i))
210 return -EFAULT;
211 if (c == 'V')
212 eur_expect_close = 42;
213 }
214 }
215 eurwdt_ping();
216 }
217
218 return count;
219}
220
221
222
223
224
225
226
227
228
229
230
231
232static int eurwdt_ioctl(struct inode *inode, struct file *file,
233 unsigned int cmd, unsigned long arg)
234{
235 void __user *argp = (void __user *)arg;
236 int __user *p = argp;
237 static struct watchdog_info ident = {
238 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
239 .firmware_version = 1,
240 .identity = "WDT Eurotech CPU-1220/1410",
241 };
242
243 int time;
244 int options, retval = -EINVAL;
245
246 switch(cmd) {
247 default:
248 return -ENOTTY;
249
250 case WDIOC_GETSUPPORT:
251 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
252
253 case WDIOC_GETSTATUS:
254 case WDIOC_GETBOOTSTATUS:
255 return put_user(0, p);
256
257 case WDIOC_KEEPALIVE:
258 eurwdt_ping();
259 return 0;
260
261 case WDIOC_SETTIMEOUT:
262 if (copy_from_user(&time, p, sizeof(int)))
263 return -EFAULT;
264
265
266 if (time < 0 || time > 255)
267 return -EINVAL;
268
269 eurwdt_timeout = time;
270 eurwdt_set_timeout(time);
271
272
273 case WDIOC_GETTIMEOUT:
274 return put_user(eurwdt_timeout, p);
275
276 case WDIOC_SETOPTIONS:
277 if (get_user(options, p))
278 return -EFAULT;
279 if (options & WDIOS_DISABLECARD) {
280 eurwdt_disable_timer();
281 retval = 0;
282 }
283 if (options & WDIOS_ENABLECARD) {
284 eurwdt_activate_timer();
285 eurwdt_ping();
286 retval = 0;
287 }
288 return retval;
289 }
290}
291
292
293
294
295
296
297
298
299
300
301static int eurwdt_open(struct inode *inode, struct file *file)
302{
303 if (test_and_set_bit(0, &eurwdt_is_open))
304 return -EBUSY;
305 eurwdt_timeout = WDT_TIMEOUT;
306
307 eurwdt_activate_timer();
308 return nonseekable_open(inode, file);
309}
310
311
312
313
314
315
316
317
318
319
320
321
322
323static int eurwdt_release(struct inode *inode, struct file *file)
324{
325 if (eur_expect_close == 42) {
326 eurwdt_disable_timer();
327 } else {
328 printk(KERN_CRIT "eurwdt: Unexpected close, not stopping watchdog!\n");
329 eurwdt_ping();
330 }
331 clear_bit(0, &eurwdt_is_open);
332 eur_expect_close = 0;
333 return 0;
334}
335
336
337
338
339
340
341
342
343
344
345
346
347
348static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code,
349 void *unused)
350{
351 if (code == SYS_DOWN || code == SYS_HALT) {
352
353 eurwdt_disable_timer();
354 }
355
356 return NOTIFY_DONE;
357}
358
359
360
361
362
363
364static const struct file_operations eurwdt_fops = {
365 .owner = THIS_MODULE,
366 .llseek = no_llseek,
367 .write = eurwdt_write,
368 .ioctl = eurwdt_ioctl,
369 .open = eurwdt_open,
370 .release = eurwdt_release,
371};
372
373static struct miscdevice eurwdt_miscdev = {
374 .minor = WATCHDOG_MINOR,
375 .name = "watchdog",
376 .fops = &eurwdt_fops,
377};
378
379
380
381
382
383
384static struct notifier_block eurwdt_notifier = {
385 .notifier_call = eurwdt_notify_sys,
386};
387
388
389
390
391
392
393
394
395
396
397
398static void __exit eurwdt_exit(void)
399{
400 eurwdt_lock_chip();
401
402 misc_deregister(&eurwdt_miscdev);
403
404 unregister_reboot_notifier(&eurwdt_notifier);
405 release_region(io, 2);
406 free_irq(irq, NULL);
407}
408
409
410
411
412
413
414
415
416
417static int __init eurwdt_init(void)
418{
419 int ret;
420
421 ret = request_irq(irq, eurwdt_interrupt, IRQF_DISABLED, "eurwdt", NULL);
422 if(ret) {
423 printk(KERN_ERR "eurwdt: IRQ %d is not free.\n", irq);
424 goto out;
425 }
426
427 if (!request_region(io, 2, "eurwdt")) {
428 printk(KERN_ERR "eurwdt: IO %X is not free.\n", io);
429 ret = -EBUSY;
430 goto outirq;
431 }
432
433 ret = register_reboot_notifier(&eurwdt_notifier);
434 if (ret) {
435 printk(KERN_ERR "eurwdt: can't register reboot notifier (err=%d)\n", ret);
436 goto outreg;
437 }
438
439 ret = misc_register(&eurwdt_miscdev);
440 if (ret) {
441 printk(KERN_ERR "eurwdt: can't misc_register on minor=%d\n",
442 WATCHDOG_MINOR);
443 goto outreboot;
444 }
445
446 eurwdt_unlock_chip();
447
448 ret = 0;
449 printk(KERN_INFO "Eurotech WDT driver 0.01 at %X (Interrupt %d)"
450 " - timeout event: %s\n",
451 io, irq, (!strcmp("int", ev) ? "int" : "reboot"));
452
453out:
454 return ret;
455
456outreboot:
457 unregister_reboot_notifier(&eurwdt_notifier);
458
459outreg:
460 release_region(io, 2);
461
462outirq:
463 free_irq(irq, NULL);
464 goto out;
465}
466
467module_init(eurwdt_init);
468module_exit(eurwdt_exit);
469
470MODULE_AUTHOR("Rodolfo Giometti");
471MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog");
472MODULE_LICENSE("GPL");
473MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
474