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