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