This source file includes following definitions.
- wdtpci_ctr_mode
- wdtpci_ctr_load
- wdtpci_start
- wdtpci_stop
- wdtpci_ping
- wdtpci_set_heartbeat
- wdtpci_get_status
- wdtpci_get_temperature
- wdtpci_interrupt
- wdtpci_write
- wdtpci_ioctl
- wdtpci_open
- wdtpci_release
- wdtpci_temp_read
- wdtpci_temp_open
- wdtpci_temp_release
- wdtpci_notify_sys
- wdtpci_init_one
- wdtpci_remove_one
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
38 #include <linux/interrupt.h>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41 #include <linux/types.h>
42 #include <linux/miscdevice.h>
43 #include <linux/watchdog.h>
44 #include <linux/ioport.h>
45 #include <linux/delay.h>
46 #include <linux/notifier.h>
47 #include <linux/reboot.h>
48 #include <linux/fs.h>
49 #include <linux/pci.h>
50 #include <linux/io.h>
51 #include <linux/uaccess.h>
52
53
54 #define WDT_IS_PCI
55 #include "wd501p.h"
56
57
58 static int dev_count;
59
60 static unsigned long open_lock;
61 static DEFINE_SPINLOCK(wdtpci_lock);
62 static char expect_close;
63
64 static resource_size_t io;
65 static int irq;
66
67
68 #define WD_TIMO 60
69
70 static int heartbeat = WD_TIMO;
71 static int wd_heartbeat;
72 module_param(heartbeat, int, 0);
73 MODULE_PARM_DESC(heartbeat,
74 "Watchdog heartbeat in seconds. (0<heartbeat<65536, default="
75 __MODULE_STRING(WD_TIMO) ")");
76
77 static bool nowayout = WATCHDOG_NOWAYOUT;
78 module_param(nowayout, bool, 0);
79 MODULE_PARM_DESC(nowayout,
80 "Watchdog cannot be stopped once started (default="
81 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
82
83
84 static int tachometer;
85 module_param(tachometer, int, 0);
86 MODULE_PARM_DESC(tachometer,
87 "PCI-WDT501 Fan Tachometer support (0=disable, default=0)");
88
89 static int type = 500;
90 module_param(type, int, 0);
91 MODULE_PARM_DESC(type,
92 "PCI-WDT501 Card type (500 or 501 , default=500)");
93
94
95
96
97
98 static void wdtpci_ctr_mode(int ctr, int mode)
99 {
100 ctr <<= 6;
101 ctr |= 0x30;
102 ctr |= (mode << 1);
103 outb(ctr, WDT_CR);
104 udelay(8);
105 }
106
107 static void wdtpci_ctr_load(int ctr, int val)
108 {
109 outb(val & 0xFF, WDT_COUNT0 + ctr);
110 udelay(8);
111 outb(val >> 8, WDT_COUNT0 + ctr);
112 udelay(8);
113 }
114
115
116
117
118
119
120
121 static int wdtpci_start(void)
122 {
123 unsigned long flags;
124
125 spin_lock_irqsave(&wdtpci_lock, flags);
126
127
128
129
130
131 inb(WDT_DC);
132 udelay(8);
133 wdtpci_ctr_mode(2, 0);
134
135 outb(0, WDT_DC);
136 udelay(8);
137 inb(WDT_DC);
138 udelay(8);
139 outb(0, WDT_CLOCK);
140 udelay(8);
141 inb(WDT_BUZZER);
142 udelay(8);
143 inb(WDT_OPTONOTRST);
144 udelay(8);
145 inb(WDT_OPTORST);
146 udelay(8);
147 inb(WDT_PROGOUT);
148 udelay(8);
149 wdtpci_ctr_mode(0, 3);
150
151 wdtpci_ctr_mode(1, 2);
152
153 wdtpci_ctr_mode(2, 1);
154
155 wdtpci_ctr_load(0, 20833);
156 wdtpci_ctr_load(1, wd_heartbeat);
157
158 outb(0, WDT_DC);
159 udelay(8);
160
161 spin_unlock_irqrestore(&wdtpci_lock, flags);
162 return 0;
163 }
164
165
166
167
168
169
170
171 static int wdtpci_stop(void)
172 {
173 unsigned long flags;
174
175
176 spin_lock_irqsave(&wdtpci_lock, flags);
177 inb(WDT_DC);
178 udelay(8);
179 wdtpci_ctr_load(2, 0);
180 spin_unlock_irqrestore(&wdtpci_lock, flags);
181 return 0;
182 }
183
184
185
186
187
188
189
190
191 static int wdtpci_ping(void)
192 {
193 unsigned long flags;
194
195 spin_lock_irqsave(&wdtpci_lock, flags);
196
197 inb(WDT_DC);
198 udelay(8);
199 wdtpci_ctr_mode(1, 2);
200
201 wdtpci_ctr_load(1, wd_heartbeat);
202 outb(0, WDT_DC);
203 udelay(8);
204 spin_unlock_irqrestore(&wdtpci_lock, flags);
205 return 0;
206 }
207
208
209
210
211
212
213
214
215
216 static int wdtpci_set_heartbeat(int t)
217 {
218
219 if (t < 1 || t > 65535)
220 return -EINVAL;
221
222 heartbeat = t;
223 wd_heartbeat = t * 100;
224 return 0;
225 }
226
227
228
229
230
231
232
233
234
235
236
237
238 static int wdtpci_get_status(int *status)
239 {
240 unsigned char new_status;
241 unsigned long flags;
242
243 spin_lock_irqsave(&wdtpci_lock, flags);
244 new_status = inb(WDT_SR);
245 spin_unlock_irqrestore(&wdtpci_lock, flags);
246
247 *status = 0;
248 if (new_status & WDC_SR_ISOI0)
249 *status |= WDIOF_EXTERN1;
250 if (new_status & WDC_SR_ISII1)
251 *status |= WDIOF_EXTERN2;
252 if (type == 501) {
253 if (!(new_status & WDC_SR_TGOOD))
254 *status |= WDIOF_OVERHEAT;
255 if (!(new_status & WDC_SR_PSUOVER))
256 *status |= WDIOF_POWEROVER;
257 if (!(new_status & WDC_SR_PSUUNDR))
258 *status |= WDIOF_POWERUNDER;
259 if (tachometer) {
260 if (!(new_status & WDC_SR_FANGOOD))
261 *status |= WDIOF_FANFAULT;
262 }
263 }
264 return 0;
265 }
266
267
268
269
270
271
272
273
274 static int wdtpci_get_temperature(int *temperature)
275 {
276 unsigned short c;
277 unsigned long flags;
278 spin_lock_irqsave(&wdtpci_lock, flags);
279 c = inb(WDT_RT);
280 udelay(8);
281 spin_unlock_irqrestore(&wdtpci_lock, flags);
282 *temperature = (c * 11 / 15) + 7;
283 return 0;
284 }
285
286
287
288
289
290
291
292
293
294
295
296 static irqreturn_t wdtpci_interrupt(int irq, void *dev_id)
297 {
298
299
300
301
302 unsigned char status;
303
304 spin_lock(&wdtpci_lock);
305
306 status = inb(WDT_SR);
307 udelay(8);
308
309 pr_crit("status %d\n", status);
310
311 if (type == 501) {
312 if (!(status & WDC_SR_TGOOD)) {
313 pr_crit("Overheat alarm (%d)\n", inb(WDT_RT));
314 udelay(8);
315 }
316 if (!(status & WDC_SR_PSUOVER))
317 pr_crit("PSU over voltage\n");
318 if (!(status & WDC_SR_PSUUNDR))
319 pr_crit("PSU under voltage\n");
320 if (tachometer) {
321 if (!(status & WDC_SR_FANGOOD))
322 pr_crit("Possible fan fault\n");
323 }
324 }
325 if (!(status & WDC_SR_WCCR)) {
326 #ifdef SOFTWARE_REBOOT
327 #ifdef ONLY_TESTING
328 pr_crit("Would Reboot\n");
329 #else
330 pr_crit("Initiating system reboot\n");
331 emergency_restart();
332 #endif
333 #else
334 pr_crit("Reset in 5ms\n");
335 #endif
336 }
337 spin_unlock(&wdtpci_lock);
338 return IRQ_HANDLED;
339 }
340
341
342
343
344
345
346
347
348
349
350
351
352
353 static ssize_t wdtpci_write(struct file *file, const char __user *buf,
354 size_t count, loff_t *ppos)
355 {
356 if (count) {
357 if (!nowayout) {
358 size_t i;
359
360
361 expect_close = 0;
362
363 for (i = 0; i != count; i++) {
364 char c;
365 if (get_user(c, buf + i))
366 return -EFAULT;
367 if (c == 'V')
368 expect_close = 42;
369 }
370 }
371 wdtpci_ping();
372 }
373 return count;
374 }
375
376
377
378
379
380
381
382
383
384
385
386
387 static long wdtpci_ioctl(struct file *file, unsigned int cmd,
388 unsigned long arg)
389 {
390 void __user *argp = (void __user *)arg;
391 int __user *p = argp;
392 int new_heartbeat;
393 int status;
394
395 struct watchdog_info ident = {
396 .options = WDIOF_SETTIMEOUT|
397 WDIOF_MAGICCLOSE|
398 WDIOF_KEEPALIVEPING,
399 .firmware_version = 1,
400 .identity = "PCI-WDT500/501",
401 };
402
403
404 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
405 if (type == 501) {
406 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
407 WDIOF_POWEROVER);
408 if (tachometer)
409 ident.options |= WDIOF_FANFAULT;
410 }
411
412 switch (cmd) {
413 case WDIOC_GETSUPPORT:
414 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
415 case WDIOC_GETSTATUS:
416 wdtpci_get_status(&status);
417 return put_user(status, p);
418 case WDIOC_GETBOOTSTATUS:
419 return put_user(0, p);
420 case WDIOC_KEEPALIVE:
421 wdtpci_ping();
422 return 0;
423 case WDIOC_SETTIMEOUT:
424 if (get_user(new_heartbeat, p))
425 return -EFAULT;
426 if (wdtpci_set_heartbeat(new_heartbeat))
427 return -EINVAL;
428 wdtpci_ping();
429
430 case WDIOC_GETTIMEOUT:
431 return put_user(heartbeat, p);
432 default:
433 return -ENOTTY;
434 }
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449 static int wdtpci_open(struct inode *inode, struct file *file)
450 {
451 if (test_and_set_bit(0, &open_lock))
452 return -EBUSY;
453
454 if (nowayout)
455 __module_get(THIS_MODULE);
456
457
458
459 wdtpci_start();
460 return stream_open(inode, file);
461 }
462
463
464
465
466
467
468
469
470
471
472
473
474
475 static int wdtpci_release(struct inode *inode, struct file *file)
476 {
477 if (expect_close == 42) {
478 wdtpci_stop();
479 } else {
480 pr_crit("Unexpected close, not stopping timer!\n");
481 wdtpci_ping();
482 }
483 expect_close = 0;
484 clear_bit(0, &open_lock);
485 return 0;
486 }
487
488
489
490
491
492
493
494
495
496
497
498
499 static ssize_t wdtpci_temp_read(struct file *file, char __user *buf,
500 size_t count, loff_t *ptr)
501 {
502 int temperature;
503
504 if (wdtpci_get_temperature(&temperature))
505 return -EFAULT;
506
507 if (copy_to_user(buf, &temperature, 1))
508 return -EFAULT;
509
510 return 1;
511 }
512
513
514
515
516
517
518
519
520
521 static int wdtpci_temp_open(struct inode *inode, struct file *file)
522 {
523 return stream_open(inode, file);
524 }
525
526
527
528
529
530
531
532
533
534 static int wdtpci_temp_release(struct inode *inode, struct file *file)
535 {
536 return 0;
537 }
538
539
540
541
542
543
544
545
546
547
548
549
550
551 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
552 void *unused)
553 {
554 if (code == SYS_DOWN || code == SYS_HALT)
555 wdtpci_stop();
556 return NOTIFY_DONE;
557 }
558
559
560
561
562
563
564 static const struct file_operations wdtpci_fops = {
565 .owner = THIS_MODULE,
566 .llseek = no_llseek,
567 .write = wdtpci_write,
568 .unlocked_ioctl = wdtpci_ioctl,
569 .open = wdtpci_open,
570 .release = wdtpci_release,
571 };
572
573 static struct miscdevice wdtpci_miscdev = {
574 .minor = WATCHDOG_MINOR,
575 .name = "watchdog",
576 .fops = &wdtpci_fops,
577 };
578
579 static const struct file_operations wdtpci_temp_fops = {
580 .owner = THIS_MODULE,
581 .llseek = no_llseek,
582 .read = wdtpci_temp_read,
583 .open = wdtpci_temp_open,
584 .release = wdtpci_temp_release,
585 };
586
587 static struct miscdevice temp_miscdev = {
588 .minor = TEMP_MINOR,
589 .name = "temperature",
590 .fops = &wdtpci_temp_fops,
591 };
592
593
594
595
596
597
598 static struct notifier_block wdtpci_notifier = {
599 .notifier_call = wdtpci_notify_sys,
600 };
601
602
603 static int wdtpci_init_one(struct pci_dev *dev,
604 const struct pci_device_id *ent)
605 {
606 int ret = -EIO;
607
608 dev_count++;
609 if (dev_count > 1) {
610 pr_err("This driver only supports one device\n");
611 return -ENODEV;
612 }
613
614 if (type != 500 && type != 501) {
615 pr_err("unknown card type '%d'\n", type);
616 return -ENODEV;
617 }
618
619 if (pci_enable_device(dev)) {
620 pr_err("Not possible to enable PCI Device\n");
621 return -ENODEV;
622 }
623
624 if (pci_resource_start(dev, 2) == 0x0000) {
625 pr_err("No I/O-Address for card detected\n");
626 ret = -ENODEV;
627 goto out_pci;
628 }
629
630 if (pci_request_region(dev, 2, "wdt_pci")) {
631 pr_err("I/O address 0x%llx already in use\n",
632 (unsigned long long)pci_resource_start(dev, 2));
633 goto out_pci;
634 }
635
636 irq = dev->irq;
637 io = pci_resource_start(dev, 2);
638
639 if (request_irq(irq, wdtpci_interrupt, IRQF_SHARED,
640 "wdt_pci", &wdtpci_miscdev)) {
641 pr_err("IRQ %d is not free\n", irq);
642 goto out_reg;
643 }
644
645 pr_info("PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%llx (Interrupt %d)\n",
646 (unsigned long long)io, irq);
647
648
649
650 if (wdtpci_set_heartbeat(heartbeat)) {
651 wdtpci_set_heartbeat(WD_TIMO);
652 pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n",
653 WD_TIMO);
654 }
655
656 ret = register_reboot_notifier(&wdtpci_notifier);
657 if (ret) {
658 pr_err("cannot register reboot notifier (err=%d)\n", ret);
659 goto out_irq;
660 }
661
662 if (type == 501) {
663 ret = misc_register(&temp_miscdev);
664 if (ret) {
665 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
666 TEMP_MINOR, ret);
667 goto out_rbt;
668 }
669 }
670
671 ret = misc_register(&wdtpci_miscdev);
672 if (ret) {
673 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
674 WATCHDOG_MINOR, ret);
675 goto out_misc;
676 }
677
678 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
679 heartbeat, nowayout);
680 if (type == 501)
681 pr_info("Fan Tachometer is %s\n",
682 tachometer ? "Enabled" : "Disabled");
683
684 ret = 0;
685 out:
686 return ret;
687
688 out_misc:
689 if (type == 501)
690 misc_deregister(&temp_miscdev);
691 out_rbt:
692 unregister_reboot_notifier(&wdtpci_notifier);
693 out_irq:
694 free_irq(irq, &wdtpci_miscdev);
695 out_reg:
696 pci_release_region(dev, 2);
697 out_pci:
698 pci_disable_device(dev);
699 goto out;
700 }
701
702
703 static void wdtpci_remove_one(struct pci_dev *pdev)
704 {
705
706
707 misc_deregister(&wdtpci_miscdev);
708 if (type == 501)
709 misc_deregister(&temp_miscdev);
710 unregister_reboot_notifier(&wdtpci_notifier);
711 free_irq(irq, &wdtpci_miscdev);
712 pci_release_region(pdev, 2);
713 pci_disable_device(pdev);
714 dev_count--;
715 }
716
717
718 static const struct pci_device_id wdtpci_pci_tbl[] = {
719 {
720 .vendor = PCI_VENDOR_ID_ACCESSIO,
721 .device = PCI_DEVICE_ID_ACCESSIO_WDG_CSM,
722 .subvendor = PCI_ANY_ID,
723 .subdevice = PCI_ANY_ID,
724 },
725 { 0, },
726 };
727 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
728
729
730 static struct pci_driver wdtpci_driver = {
731 .name = "wdt_pci",
732 .id_table = wdtpci_pci_tbl,
733 .probe = wdtpci_init_one,
734 .remove = wdtpci_remove_one,
735 };
736
737 module_pci_driver(wdtpci_driver);
738
739 MODULE_AUTHOR("JP Nollmann, Alan Cox");
740 MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards");
741 MODULE_LICENSE("GPL");