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