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