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