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