This source file includes following definitions.
- wdt_ctr_mode
- wdt_ctr_load
- wdt_start
- wdt_stop
- wdt_ping
- wdt_set_heartbeat
- wdt_get_status
- wdt_get_temperature
- wdt_decode_501
- wdt_interrupt
- wdt_write
- wdt_ioctl
- wdt_open
- wdt_release
- wdt_temp_read
- wdt_temp_open
- wdt_temp_release
- wdt_notify_sys
- wdt_exit
- wdt_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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 #include <linux/interrupt.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/types.h>
37 #include <linux/miscdevice.h>
38 #include <linux/watchdog.h>
39 #include <linux/fs.h>
40 #include <linux/ioport.h>
41 #include <linux/notifier.h>
42 #include <linux/reboot.h>
43 #include <linux/init.h>
44 #include <linux/io.h>
45 #include <linux/uaccess.h>
46
47 #include "wd501p.h"
48
49 static unsigned long wdt_is_open;
50 static char expect_close;
51
52
53
54
55
56 #define WD_TIMO 60
57
58 static int heartbeat = WD_TIMO;
59 static int wd_heartbeat;
60 module_param(heartbeat, int, 0);
61 MODULE_PARM_DESC(heartbeat,
62 "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
63 __MODULE_STRING(WD_TIMO) ")");
64
65 static bool nowayout = WATCHDOG_NOWAYOUT;
66 module_param(nowayout, bool, 0);
67 MODULE_PARM_DESC(nowayout,
68 "Watchdog cannot be stopped once started (default="
69 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
70
71
72 static int io = 0x240;
73 static int irq = 11;
74
75 static DEFINE_SPINLOCK(wdt_lock);
76
77 module_param_hw(io, int, ioport, 0);
78 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
79 module_param_hw(irq, int, irq, 0);
80 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
81
82
83 static int tachometer;
84 module_param(tachometer, int, 0);
85 MODULE_PARM_DESC(tachometer,
86 "WDT501-P Fan Tachometer support (0=disable, default=0)");
87
88 static int type = 500;
89 module_param(type, int, 0);
90 MODULE_PARM_DESC(type,
91 "WDT501-P Card type (500 or 501, default=500)");
92
93
94
95
96
97 static void wdt_ctr_mode(int ctr, int mode)
98 {
99 ctr <<= 6;
100 ctr |= 0x30;
101 ctr |= (mode << 1);
102 outb_p(ctr, WDT_CR);
103 }
104
105 static void wdt_ctr_load(int ctr, int val)
106 {
107 outb_p(val&0xFF, WDT_COUNT0+ctr);
108 outb_p(val>>8, WDT_COUNT0+ctr);
109 }
110
111
112
113
114
115
116
117 static int wdt_start(void)
118 {
119 unsigned long flags;
120 spin_lock_irqsave(&wdt_lock, flags);
121 inb_p(WDT_DC);
122 wdt_ctr_mode(0, 3);
123
124 wdt_ctr_mode(1, 2);
125
126 wdt_ctr_mode(2, 0);
127
128 wdt_ctr_load(0, 8948);
129 wdt_ctr_load(1, wd_heartbeat);
130 wdt_ctr_load(2, 65535);
131 outb_p(0, WDT_DC);
132 spin_unlock_irqrestore(&wdt_lock, flags);
133 return 0;
134 }
135
136
137
138
139
140
141
142 static int wdt_stop(void)
143 {
144 unsigned long flags;
145 spin_lock_irqsave(&wdt_lock, flags);
146
147 inb_p(WDT_DC);
148 wdt_ctr_load(2, 0);
149 spin_unlock_irqrestore(&wdt_lock, flags);
150 return 0;
151 }
152
153
154
155
156
157
158
159
160 static void wdt_ping(void)
161 {
162 unsigned long flags;
163 spin_lock_irqsave(&wdt_lock, flags);
164
165 inb_p(WDT_DC);
166 wdt_ctr_mode(1, 2);
167
168 wdt_ctr_load(1, wd_heartbeat);
169 outb_p(0, WDT_DC);
170 spin_unlock_irqrestore(&wdt_lock, flags);
171 }
172
173
174
175
176
177
178
179
180
181
182 static int wdt_set_heartbeat(int t)
183 {
184 if (t < 1 || t > 65535)
185 return -EINVAL;
186
187 heartbeat = t;
188 wd_heartbeat = t * 100;
189 return 0;
190 }
191
192
193
194
195
196
197
198
199
200
201
202 static int wdt_get_status(void)
203 {
204 unsigned char new_status;
205 int status = 0;
206 unsigned long flags;
207
208 spin_lock_irqsave(&wdt_lock, flags);
209 new_status = inb_p(WDT_SR);
210 spin_unlock_irqrestore(&wdt_lock, flags);
211
212 if (new_status & WDC_SR_ISOI0)
213 status |= WDIOF_EXTERN1;
214 if (new_status & WDC_SR_ISII1)
215 status |= WDIOF_EXTERN2;
216 if (type == 501) {
217 if (!(new_status & WDC_SR_TGOOD))
218 status |= WDIOF_OVERHEAT;
219 if (!(new_status & WDC_SR_PSUOVER))
220 status |= WDIOF_POWEROVER;
221 if (!(new_status & WDC_SR_PSUUNDR))
222 status |= WDIOF_POWERUNDER;
223 if (tachometer) {
224 if (!(new_status & WDC_SR_FANGOOD))
225 status |= WDIOF_FANFAULT;
226 }
227 }
228 return status;
229 }
230
231
232
233
234
235
236
237
238 static int wdt_get_temperature(void)
239 {
240 unsigned short c;
241 unsigned long flags;
242
243 spin_lock_irqsave(&wdt_lock, flags);
244 c = inb_p(WDT_RT);
245 spin_unlock_irqrestore(&wdt_lock, flags);
246 return (c * 11 / 15) + 7;
247 }
248
249 static void wdt_decode_501(int status)
250 {
251 if (!(status & WDC_SR_TGOOD))
252 pr_crit("Overheat alarm (%d)\n", inb_p(WDT_RT));
253 if (!(status & WDC_SR_PSUOVER))
254 pr_crit("PSU over voltage\n");
255 if (!(status & WDC_SR_PSUUNDR))
256 pr_crit("PSU under voltage\n");
257 }
258
259
260
261
262
263
264
265
266
267
268
269 static irqreturn_t wdt_interrupt(int irq, void *dev_id)
270 {
271
272
273
274
275 unsigned char status;
276
277 spin_lock(&wdt_lock);
278 status = inb_p(WDT_SR);
279
280 pr_crit("WDT status %d\n", status);
281
282 if (type == 501) {
283 wdt_decode_501(status);
284 if (tachometer) {
285 if (!(status & WDC_SR_FANGOOD))
286 pr_crit("Possible fan fault\n");
287 }
288 }
289 if (!(status & WDC_SR_WCCR)) {
290 #ifdef SOFTWARE_REBOOT
291 #ifdef ONLY_TESTING
292 pr_crit("Would Reboot\n");
293 #else
294 pr_crit("Initiating system reboot\n");
295 emergency_restart();
296 #endif
297 #else
298 pr_crit("Reset in 5ms\n");
299 #endif
300 }
301 spin_unlock(&wdt_lock);
302 return IRQ_HANDLED;
303 }
304
305
306
307
308
309
310
311
312
313
314
315
316
317 static ssize_t wdt_write(struct file *file, const char __user *buf,
318 size_t count, loff_t *ppos)
319 {
320 if (count) {
321 if (!nowayout) {
322 size_t i;
323
324
325 expect_close = 0;
326
327 for (i = 0; i != count; i++) {
328 char c;
329 if (get_user(c, buf + i))
330 return -EFAULT;
331 if (c == 'V')
332 expect_close = 42;
333 }
334 }
335 wdt_ping();
336 }
337 return count;
338 }
339
340
341
342
343
344
345
346
347
348
349
350
351 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
352 {
353 void __user *argp = (void __user *)arg;
354 int __user *p = argp;
355 int new_heartbeat;
356 int status;
357
358 struct watchdog_info ident = {
359 .options = WDIOF_SETTIMEOUT|
360 WDIOF_MAGICCLOSE|
361 WDIOF_KEEPALIVEPING,
362 .firmware_version = 1,
363 .identity = "WDT500/501",
364 };
365
366
367 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
368 if (type == 501) {
369 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
370 WDIOF_POWEROVER);
371 if (tachometer)
372 ident.options |= WDIOF_FANFAULT;
373 }
374
375 switch (cmd) {
376 case WDIOC_GETSUPPORT:
377 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
378 case WDIOC_GETSTATUS:
379 status = wdt_get_status();
380 return put_user(status, p);
381 case WDIOC_GETBOOTSTATUS:
382 return put_user(0, p);
383 case WDIOC_KEEPALIVE:
384 wdt_ping();
385 return 0;
386 case WDIOC_SETTIMEOUT:
387 if (get_user(new_heartbeat, p))
388 return -EFAULT;
389 if (wdt_set_heartbeat(new_heartbeat))
390 return -EINVAL;
391 wdt_ping();
392
393 case WDIOC_GETTIMEOUT:
394 return put_user(heartbeat, p);
395 default:
396 return -ENOTTY;
397 }
398 }
399
400
401
402
403
404
405
406
407
408
409
410
411
412 static int wdt_open(struct inode *inode, struct file *file)
413 {
414 if (test_and_set_bit(0, &wdt_is_open))
415 return -EBUSY;
416
417
418
419 wdt_start();
420 return stream_open(inode, file);
421 }
422
423
424
425
426
427
428
429
430
431
432
433
434
435 static int wdt_release(struct inode *inode, struct file *file)
436 {
437 if (expect_close == 42) {
438 wdt_stop();
439 clear_bit(0, &wdt_is_open);
440 } else {
441 pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
442 wdt_ping();
443 }
444 expect_close = 0;
445 return 0;
446 }
447
448
449
450
451
452
453
454
455
456
457
458
459 static ssize_t wdt_temp_read(struct file *file, char __user *buf,
460 size_t count, loff_t *ptr)
461 {
462 int temperature = wdt_get_temperature();
463
464 if (copy_to_user(buf, &temperature, 1))
465 return -EFAULT;
466
467 return 1;
468 }
469
470
471
472
473
474
475
476
477
478 static int wdt_temp_open(struct inode *inode, struct file *file)
479 {
480 return stream_open(inode, file);
481 }
482
483
484
485
486
487
488
489
490
491 static int wdt_temp_release(struct inode *inode, struct file *file)
492 {
493 return 0;
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507
508 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
509 void *unused)
510 {
511 if (code == SYS_DOWN || code == SYS_HALT)
512 wdt_stop();
513 return NOTIFY_DONE;
514 }
515
516
517
518
519
520
521 static const struct file_operations wdt_fops = {
522 .owner = THIS_MODULE,
523 .llseek = no_llseek,
524 .write = wdt_write,
525 .unlocked_ioctl = wdt_ioctl,
526 .open = wdt_open,
527 .release = wdt_release,
528 };
529
530 static struct miscdevice wdt_miscdev = {
531 .minor = WATCHDOG_MINOR,
532 .name = "watchdog",
533 .fops = &wdt_fops,
534 };
535
536 static const struct file_operations wdt_temp_fops = {
537 .owner = THIS_MODULE,
538 .llseek = no_llseek,
539 .read = wdt_temp_read,
540 .open = wdt_temp_open,
541 .release = wdt_temp_release,
542 };
543
544 static struct miscdevice temp_miscdev = {
545 .minor = TEMP_MINOR,
546 .name = "temperature",
547 .fops = &wdt_temp_fops,
548 };
549
550
551
552
553
554
555 static struct notifier_block wdt_notifier = {
556 .notifier_call = wdt_notify_sys,
557 };
558
559
560
561
562
563
564
565
566
567
568
569 static void __exit wdt_exit(void)
570 {
571 misc_deregister(&wdt_miscdev);
572 if (type == 501)
573 misc_deregister(&temp_miscdev);
574 unregister_reboot_notifier(&wdt_notifier);
575 free_irq(irq, NULL);
576 release_region(io, 8);
577 }
578
579
580
581
582
583
584
585
586
587 static int __init wdt_init(void)
588 {
589 int ret;
590
591 if (type != 500 && type != 501) {
592 pr_err("unknown card type '%d'\n", type);
593 return -ENODEV;
594 }
595
596
597
598 if (wdt_set_heartbeat(heartbeat)) {
599 wdt_set_heartbeat(WD_TIMO);
600 pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n",
601 WD_TIMO);
602 }
603
604 if (!request_region(io, 8, "wdt501p")) {
605 pr_err("I/O address 0x%04x already in use\n", io);
606 ret = -EBUSY;
607 goto out;
608 }
609
610 ret = request_irq(irq, wdt_interrupt, 0, "wdt501p", NULL);
611 if (ret) {
612 pr_err("IRQ %d is not free\n", irq);
613 goto outreg;
614 }
615
616 ret = register_reboot_notifier(&wdt_notifier);
617 if (ret) {
618 pr_err("cannot register reboot notifier (err=%d)\n", ret);
619 goto outirq;
620 }
621
622 if (type == 501) {
623 ret = misc_register(&temp_miscdev);
624 if (ret) {
625 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
626 TEMP_MINOR, ret);
627 goto outrbt;
628 }
629 }
630
631 ret = misc_register(&wdt_miscdev);
632 if (ret) {
633 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
634 WATCHDOG_MINOR, ret);
635 goto outmisc;
636 }
637
638 pr_info("WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
639 io, irq, heartbeat, nowayout);
640 if (type == 501)
641 pr_info("Fan Tachometer is %s\n",
642 tachometer ? "Enabled" : "Disabled");
643 return 0;
644
645 outmisc:
646 if (type == 501)
647 misc_deregister(&temp_miscdev);
648 outrbt:
649 unregister_reboot_notifier(&wdt_notifier);
650 outirq:
651 free_irq(irq, NULL);
652 outreg:
653 release_region(io, 8);
654 out:
655 return ret;
656 }
657
658 module_init(wdt_init);
659 module_exit(wdt_exit);
660
661 MODULE_AUTHOR("Alan Cox");
662 MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
663 MODULE_LICENSE("GPL");