Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Berkshire PCI-PC Watchdog Card Driver
4 *
5 * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
6 *
7 * Based on source code of the following authors:
8 * Ken Hollis <kenji@bitgate.com>,
9 * Lindsay Harris <lindsay@bluegum.com>,
10 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * Matt Domsch <Matt_Domsch@dell.com>,
12 * Rob Radez <rob@osinvestor.com>
13 *
14 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
15 * provide warranty for any of this software. This material is
16 * provided "AS-IS" and at no charge.
17 */
18
19/*
20 * A bells and whistles driver is available from:
21 * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
22 *
23 * More info available at
24 * http://www.berkprod.com/ or http://www.pcwatchdog.com/
25 */
26
27/*
28 * Includes, defines, variables, module parameters, ...
29 */
30
31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33#include <linux/module.h> /* For module specific items */
34#include <linux/moduleparam.h> /* For new moduleparam's */
35#include <linux/types.h> /* For standard types (like size_t) */
36#include <linux/errno.h> /* For the -ENODEV/... values */
37#include <linux/kernel.h> /* For printk/panic/... */
38#include <linux/delay.h> /* For mdelay function */
39#include <linux/miscdevice.h> /* For struct miscdevice */
40#include <linux/watchdog.h> /* For the watchdog specific items */
41#include <linux/notifier.h> /* For notifier support */
42#include <linux/reboot.h> /* For reboot_notifier stuff */
43#include <linux/init.h> /* For __init/__exit/... */
44#include <linux/fs.h> /* For file operations */
45#include <linux/pci.h> /* For pci functions */
46#include <linux/ioport.h> /* For io-port access */
47#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
48#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
49#include <linux/io.h> /* For inb/outb/... */
50
51/* Module and version information */
52#define WATCHDOG_VERSION "1.03"
53#define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
54#define WATCHDOG_NAME "pcwd_pci"
55#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
56
57/* Stuff for the PCI ID's */
58#ifndef PCI_VENDOR_ID_QUICKLOGIC
59#define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
60#endif
61
62#ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
63#define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
64#endif
65
66/*
67 * These are the defines that describe the control status bits for the
68 * PCI-PC Watchdog card.
69 */
70/* Port 1 : Control Status #1 */
71#define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
72#define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
73#define WD_PCI_TTRP 0x04 /* Temperature Trip status */
74#define WD_PCI_RL2A 0x08 /* Relay 2 Active */
75#define WD_PCI_RL1A 0x10 /* Relay 1 Active */
76#define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
77 reset */
78#define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
79/* Port 2 : Control Status #2 */
80#define WD_PCI_WDIS 0x10 /* Watchdog Disable */
81#define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
82#define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
83#define WD_PCI_PCMD 0x80 /* PC has sent command */
84
85/* according to documentation max. time to process a command for the pci
86 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
87#define PCI_COMMAND_TIMEOUT 150
88
89/* Watchdog's internal commands */
90#define CMD_GET_STATUS 0x04
91#define CMD_GET_FIRMWARE_VERSION 0x08
92#define CMD_READ_WATCHDOG_TIMEOUT 0x18
93#define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
94#define CMD_GET_CLEAR_RESET_COUNT 0x84
95
96/* Watchdog's Dip Switch heartbeat values */
97static const int heartbeat_tbl[] = {
98 5, /* OFF-OFF-OFF = 5 Sec */
99 10, /* OFF-OFF-ON = 10 Sec */
100 30, /* OFF-ON-OFF = 30 Sec */
101 60, /* OFF-ON-ON = 1 Min */
102 300, /* ON-OFF-OFF = 5 Min */
103 600, /* ON-OFF-ON = 10 Min */
104 1800, /* ON-ON-OFF = 30 Min */
105 3600, /* ON-ON-ON = 1 hour */
106};
107
108/* We can only use 1 card due to the /dev/watchdog restriction */
109static int cards_found;
110
111/* internal variables */
112static int temp_panic;
113static unsigned long is_active;
114static char expect_release;
115/* this is private data for each PCI-PC watchdog card */
116static struct {
117 /* Wether or not the card has a temperature device */
118 int supports_temp;
119 /* The card's boot status */
120 int boot_status;
121 /* The cards I/O address */
122 unsigned long io_addr;
123 /* the lock for io operations */
124 spinlock_t io_lock;
125 /* the PCI-device */
126 struct pci_dev *pdev;
127} pcipcwd_private;
128
129/* module parameters */
130#define QUIET 0 /* Default */
131#define VERBOSE 1 /* Verbose */
132#define DEBUG 2 /* print fancy stuff too */
133static int debug = QUIET;
134module_param(debug, int, 0);
135MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
136
137#define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
138 delay-time from dip-switches */
139static int heartbeat = WATCHDOG_HEARTBEAT;
140module_param(heartbeat, int, 0);
141MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
142 "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
143 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
144
145static bool nowayout = WATCHDOG_NOWAYOUT;
146module_param(nowayout, bool, 0);
147MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
148 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
149
150/*
151 * Internal functions
152 */
153
154static int send_command(int cmd, int *msb, int *lsb)
155{
156 int got_response, count;
157
158 if (debug >= DEBUG)
159 pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
160 cmd, *msb, *lsb);
161
162 spin_lock(&pcipcwd_private.io_lock);
163 /* If a command requires data it should be written first.
164 * Data for commands with 8 bits of data should be written to port 4.
165 * Commands with 16 bits of data, should be written as LSB to port 4
166 * and MSB to port 5.
167 * After the required data has been written then write the command to
168 * port 6. */
169 outb_p(*lsb, pcipcwd_private.io_addr + 4);
170 outb_p(*msb, pcipcwd_private.io_addr + 5);
171 outb_p(cmd, pcipcwd_private.io_addr + 6);
172
173 /* wait till the pci card processed the command, signaled by
174 * the WRSP bit in port 2 and give it a max. timeout of
175 * PCI_COMMAND_TIMEOUT to process */
176 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
177 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
178 count++) {
179 mdelay(1);
180 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
181 }
182
183 if (debug >= DEBUG) {
184 if (got_response) {
185 pr_debug("time to process command was: %d ms\n",
186 count);
187 } else {
188 pr_debug("card did not respond on command!\n");
189 }
190 }
191
192 if (got_response) {
193 /* read back response */
194 *lsb = inb_p(pcipcwd_private.io_addr + 4);
195 *msb = inb_p(pcipcwd_private.io_addr + 5);
196
197 /* clear WRSP bit */
198 inb_p(pcipcwd_private.io_addr + 6);
199
200 if (debug >= DEBUG)
201 pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
202 cmd, *msb, *lsb);
203 }
204
205 spin_unlock(&pcipcwd_private.io_lock);
206
207 return got_response;
208}
209
210static inline void pcipcwd_check_temperature_support(void)
211{
212 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
213 pcipcwd_private.supports_temp = 1;
214}
215
216static int pcipcwd_get_option_switches(void)
217{
218 int option_switches;
219
220 option_switches = inb_p(pcipcwd_private.io_addr + 3);
221 return option_switches;
222}
223
224static void pcipcwd_show_card_info(void)
225{
226 int got_fw_rev, fw_rev_major, fw_rev_minor;
227 char fw_ver_str[20]; /* The cards firmware version */
228 int option_switches;
229
230 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
231 &fw_rev_minor);
232 if (got_fw_rev)
233 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
234 else
235 sprintf(fw_ver_str, "<card no answer>");
236
237 /* Get switch settings */
238 option_switches = pcipcwd_get_option_switches();
239
240 pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
241 (int) pcipcwd_private.io_addr, fw_ver_str,
242 (pcipcwd_private.supports_temp ? "with" : "without"));
243
244 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
245 option_switches,
246 ((option_switches & 0x10) ? "ON" : "OFF"),
247 ((option_switches & 0x08) ? "ON" : "OFF"));
248
249 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
250 pr_info("Previous reset was caused by the Watchdog card\n");
251
252 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
253 pr_info("Card sensed a CPU Overheat\n");
254
255 if (pcipcwd_private.boot_status == 0)
256 pr_info("No previous trip detected - Cold boot or reset\n");
257}
258
259static int pcipcwd_start(void)
260{
261 int stat_reg;
262
263 spin_lock(&pcipcwd_private.io_lock);
264 outb_p(0x00, pcipcwd_private.io_addr + 3);
265 udelay(1000);
266
267 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
268 spin_unlock(&pcipcwd_private.io_lock);
269
270 if (stat_reg & WD_PCI_WDIS) {
271 pr_err("Card timer not enabled\n");
272 return -1;
273 }
274
275 if (debug >= VERBOSE)
276 pr_debug("Watchdog started\n");
277
278 return 0;
279}
280
281static int pcipcwd_stop(void)
282{
283 int stat_reg;
284
285 spin_lock(&pcipcwd_private.io_lock);
286 outb_p(0xA5, pcipcwd_private.io_addr + 3);
287 udelay(1000);
288
289 outb_p(0xA5, pcipcwd_private.io_addr + 3);
290 udelay(1000);
291
292 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
293 spin_unlock(&pcipcwd_private.io_lock);
294
295 if (!(stat_reg & WD_PCI_WDIS)) {
296 pr_err("Card did not acknowledge disable attempt\n");
297 return -1;
298 }
299
300 if (debug >= VERBOSE)
301 pr_debug("Watchdog stopped\n");
302
303 return 0;
304}
305
306static int pcipcwd_keepalive(void)
307{
308 /* Re-trigger watchdog by writing to port 0 */
309 spin_lock(&pcipcwd_private.io_lock);
310 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
311 spin_unlock(&pcipcwd_private.io_lock);
312
313 if (debug >= DEBUG)
314 pr_debug("Watchdog keepalive signal send\n");
315
316 return 0;
317}
318
319static int pcipcwd_set_heartbeat(int t)
320{
321 int t_msb = t / 256;
322 int t_lsb = t % 256;
323
324 if ((t < 0x0001) || (t > 0xFFFF))
325 return -EINVAL;
326
327 /* Write new heartbeat to watchdog */
328 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
329
330 heartbeat = t;
331 if (debug >= VERBOSE)
332 pr_debug("New heartbeat: %d\n", heartbeat);
333
334 return 0;
335}
336
337static int pcipcwd_get_status(int *status)
338{
339 int control_status;
340
341 *status = 0;
342 control_status = inb_p(pcipcwd_private.io_addr + 1);
343 if (control_status & WD_PCI_WTRP)
344 *status |= WDIOF_CARDRESET;
345 if (control_status & WD_PCI_TTRP) {
346 *status |= WDIOF_OVERHEAT;
347 if (temp_panic)
348 panic(KBUILD_MODNAME ": Temperature overheat trip!\n");
349 }
350
351 if (debug >= DEBUG)
352 pr_debug("Control Status #1: 0x%02x\n", control_status);
353
354 return 0;
355}
356
357static int pcipcwd_clear_status(void)
358{
359 int control_status;
360 int msb;
361 int reset_counter;
362
363 if (debug >= VERBOSE)
364 pr_info("clearing watchdog trip status & LED\n");
365
366 control_status = inb_p(pcipcwd_private.io_addr + 1);
367
368 if (debug >= DEBUG) {
369 pr_debug("status was: 0x%02x\n", control_status);
370 pr_debug("sending: 0x%02x\n",
371 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
372 }
373
374 /* clear trip status & LED and keep mode of relay 2 */
375 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
376 pcipcwd_private.io_addr + 1);
377
378 /* clear reset counter */
379 msb = 0;
380 reset_counter = 0xff;
381 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
382
383 if (debug >= DEBUG) {
384 pr_debug("reset count was: 0x%02x\n", reset_counter);
385 }
386
387 return 0;
388}
389
390static int pcipcwd_get_temperature(int *temperature)
391{
392 *temperature = 0;
393 if (!pcipcwd_private.supports_temp)
394 return -ENODEV;
395
396 spin_lock(&pcipcwd_private.io_lock);
397 *temperature = inb_p(pcipcwd_private.io_addr);
398 spin_unlock(&pcipcwd_private.io_lock);
399
400 /*
401 * Convert celsius to fahrenheit, since this was
402 * the decided 'standard' for this return value.
403 */
404 *temperature = (*temperature * 9 / 5) + 32;
405
406 if (debug >= DEBUG) {
407 pr_debug("temperature is: %d F\n", *temperature);
408 }
409
410 return 0;
411}
412
413static int pcipcwd_get_timeleft(int *time_left)
414{
415 int msb;
416 int lsb;
417
418 /* Read the time that's left before rebooting */
419 /* Note: if the board is not yet armed then we will read 0xFFFF */
420 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
421
422 *time_left = (msb << 8) + lsb;
423
424 if (debug >= VERBOSE)
425 pr_debug("Time left before next reboot: %d\n", *time_left);
426
427 return 0;
428}
429
430/*
431 * /dev/watchdog handling
432 */
433
434static ssize_t pcipcwd_write(struct file *file, const char __user *data,
435 size_t len, loff_t *ppos)
436{
437 /* See if we got the magic character 'V' and reload the timer */
438 if (len) {
439 if (!nowayout) {
440 size_t i;
441
442 /* note: just in case someone wrote the magic character
443 * five months ago... */
444 expect_release = 0;
445
446 /* scan to see whether or not we got the
447 * magic character */
448 for (i = 0; i != len; i++) {
449 char c;
450 if (get_user(c, data + i))
451 return -EFAULT;
452 if (c == 'V')
453 expect_release = 42;
454 }
455 }
456
457 /* someone wrote to us, we should reload the timer */
458 pcipcwd_keepalive();
459 }
460 return len;
461}
462
463static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
464 unsigned long arg)
465{
466 void __user *argp = (void __user *)arg;
467 int __user *p = argp;
468 static const struct watchdog_info ident = {
469 .options = WDIOF_OVERHEAT |
470 WDIOF_CARDRESET |
471 WDIOF_KEEPALIVEPING |
472 WDIOF_SETTIMEOUT |
473 WDIOF_MAGICCLOSE,
474 .firmware_version = 1,
475 .identity = WATCHDOG_DRIVER_NAME,
476 };
477
478 switch (cmd) {
479 case WDIOC_GETSUPPORT:
480 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
481
482 case WDIOC_GETSTATUS:
483 {
484 int status;
485 pcipcwd_get_status(&status);
486 return put_user(status, p);
487 }
488
489 case WDIOC_GETBOOTSTATUS:
490 return put_user(pcipcwd_private.boot_status, p);
491
492 case WDIOC_GETTEMP:
493 {
494 int temperature;
495
496 if (pcipcwd_get_temperature(&temperature))
497 return -EFAULT;
498
499 return put_user(temperature, p);
500 }
501
502 case WDIOC_SETOPTIONS:
503 {
504 int new_options, retval = -EINVAL;
505
506 if (get_user(new_options, p))
507 return -EFAULT;
508
509 if (new_options & WDIOS_DISABLECARD) {
510 if (pcipcwd_stop())
511 return -EIO;
512 retval = 0;
513 }
514
515 if (new_options & WDIOS_ENABLECARD) {
516 if (pcipcwd_start())
517 return -EIO;
518 retval = 0;
519 }
520
521 if (new_options & WDIOS_TEMPPANIC) {
522 temp_panic = 1;
523 retval = 0;
524 }
525
526 return retval;
527 }
528
529 case WDIOC_KEEPALIVE:
530 pcipcwd_keepalive();
531 return 0;
532
533 case WDIOC_SETTIMEOUT:
534 {
535 int new_heartbeat;
536
537 if (get_user(new_heartbeat, p))
538 return -EFAULT;
539
540 if (pcipcwd_set_heartbeat(new_heartbeat))
541 return -EINVAL;
542
543 pcipcwd_keepalive();
544 }
545 fallthrough;
546
547 case WDIOC_GETTIMEOUT:
548 return put_user(heartbeat, p);
549
550 case WDIOC_GETTIMELEFT:
551 {
552 int time_left;
553
554 if (pcipcwd_get_timeleft(&time_left))
555 return -EFAULT;
556
557 return put_user(time_left, p);
558 }
559
560 default:
561 return -ENOTTY;
562 }
563}
564
565static int pcipcwd_open(struct inode *inode, struct file *file)
566{
567 /* /dev/watchdog can only be opened once */
568 if (test_and_set_bit(0, &is_active)) {
569 if (debug >= VERBOSE)
570 pr_err("Attempt to open already opened device\n");
571 return -EBUSY;
572 }
573
574 /* Activate */
575 pcipcwd_start();
576 pcipcwd_keepalive();
577 return stream_open(inode, file);
578}
579
580static int pcipcwd_release(struct inode *inode, struct file *file)
581{
582 /*
583 * Shut off the timer.
584 */
585 if (expect_release == 42) {
586 pcipcwd_stop();
587 } else {
588 pr_crit("Unexpected close, not stopping watchdog!\n");
589 pcipcwd_keepalive();
590 }
591 expect_release = 0;
592 clear_bit(0, &is_active);
593 return 0;
594}
595
596/*
597 * /dev/temperature handling
598 */
599
600static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
601 size_t len, loff_t *ppos)
602{
603 int temperature;
604
605 if (pcipcwd_get_temperature(&temperature))
606 return -EFAULT;
607
608 if (copy_to_user(data, &temperature, 1))
609 return -EFAULT;
610
611 return 1;
612}
613
614static int pcipcwd_temp_open(struct inode *inode, struct file *file)
615{
616 if (!pcipcwd_private.supports_temp)
617 return -ENODEV;
618
619 return stream_open(inode, file);
620}
621
622static int pcipcwd_temp_release(struct inode *inode, struct file *file)
623{
624 return 0;
625}
626
627/*
628 * Notify system
629 */
630
631static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
632 void *unused)
633{
634 if (code == SYS_DOWN || code == SYS_HALT)
635 pcipcwd_stop(); /* Turn the WDT off */
636
637 return NOTIFY_DONE;
638}
639
640/*
641 * Kernel Interfaces
642 */
643
644static const struct file_operations pcipcwd_fops = {
645 .owner = THIS_MODULE,
646 .write = pcipcwd_write,
647 .unlocked_ioctl = pcipcwd_ioctl,
648 .compat_ioctl = compat_ptr_ioctl,
649 .open = pcipcwd_open,
650 .release = pcipcwd_release,
651};
652
653static struct miscdevice pcipcwd_miscdev = {
654 .minor = WATCHDOG_MINOR,
655 .name = "watchdog",
656 .fops = &pcipcwd_fops,
657};
658
659static const struct file_operations pcipcwd_temp_fops = {
660 .owner = THIS_MODULE,
661 .read = pcipcwd_temp_read,
662 .open = pcipcwd_temp_open,
663 .release = pcipcwd_temp_release,
664};
665
666static struct miscdevice pcipcwd_temp_miscdev = {
667 .minor = TEMP_MINOR,
668 .name = "temperature",
669 .fops = &pcipcwd_temp_fops,
670};
671
672static struct notifier_block pcipcwd_notifier = {
673 .notifier_call = pcipcwd_notify_sys,
674};
675
676/*
677 * Init & exit routines
678 */
679
680static int pcipcwd_card_init(struct pci_dev *pdev,
681 const struct pci_device_id *ent)
682{
683 int ret = -EIO;
684
685 cards_found++;
686 if (cards_found == 1)
687 pr_info("%s\n", DRIVER_VERSION);
688
689 if (cards_found > 1) {
690 pr_err("This driver only supports 1 device\n");
691 return -ENODEV;
692 }
693
694 if (pci_enable_device(pdev)) {
695 pr_err("Not possible to enable PCI Device\n");
696 return -ENODEV;
697 }
698
699 if (pci_resource_start(pdev, 0) == 0x0000) {
700 pr_err("No I/O-Address for card detected\n");
701 ret = -ENODEV;
702 goto err_out_disable_device;
703 }
704
705 spin_lock_init(&pcipcwd_private.io_lock);
706 pcipcwd_private.pdev = pdev;
707 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
708
709 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
710 pr_err("I/O address 0x%04x already in use\n",
711 (int) pcipcwd_private.io_addr);
712 ret = -EIO;
713 goto err_out_disable_device;
714 }
715
716 /* get the boot_status */
717 pcipcwd_get_status(&pcipcwd_private.boot_status);
718
719 /* clear the "card caused reboot" flag */
720 pcipcwd_clear_status();
721
722 /* disable card */
723 pcipcwd_stop();
724
725 /* Check whether or not the card supports the temperature device */
726 pcipcwd_check_temperature_support();
727
728 /* Show info about the card itself */
729 pcipcwd_show_card_info();
730
731 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
732 if (heartbeat == 0)
733 heartbeat =
734 heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
735
736 /* Check that the heartbeat value is within it's range ;
737 * if not reset to the default */
738 if (pcipcwd_set_heartbeat(heartbeat)) {
739 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
740 pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
741 WATCHDOG_HEARTBEAT);
742 }
743
744 ret = register_reboot_notifier(&pcipcwd_notifier);
745 if (ret != 0) {
746 pr_err("cannot register reboot notifier (err=%d)\n", ret);
747 goto err_out_release_region;
748 }
749
750 if (pcipcwd_private.supports_temp) {
751 ret = misc_register(&pcipcwd_temp_miscdev);
752 if (ret != 0) {
753 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
754 TEMP_MINOR, ret);
755 goto err_out_unregister_reboot;
756 }
757 }
758
759 ret = misc_register(&pcipcwd_miscdev);
760 if (ret != 0) {
761 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
762 WATCHDOG_MINOR, ret);
763 goto err_out_misc_deregister;
764 }
765
766 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
767 heartbeat, nowayout);
768
769 return 0;
770
771err_out_misc_deregister:
772 if (pcipcwd_private.supports_temp)
773 misc_deregister(&pcipcwd_temp_miscdev);
774err_out_unregister_reboot:
775 unregister_reboot_notifier(&pcipcwd_notifier);
776err_out_release_region:
777 pci_release_regions(pdev);
778err_out_disable_device:
779 pci_disable_device(pdev);
780 return ret;
781}
782
783static void pcipcwd_card_exit(struct pci_dev *pdev)
784{
785 /* Stop the timer before we leave */
786 if (!nowayout)
787 pcipcwd_stop();
788
789 /* Deregister */
790 misc_deregister(&pcipcwd_miscdev);
791 if (pcipcwd_private.supports_temp)
792 misc_deregister(&pcipcwd_temp_miscdev);
793 unregister_reboot_notifier(&pcipcwd_notifier);
794 pci_release_regions(pdev);
795 pci_disable_device(pdev);
796 cards_found--;
797}
798
799static const struct pci_device_id pcipcwd_pci_tbl[] = {
800 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
801 PCI_ANY_ID, PCI_ANY_ID, },
802 { 0 }, /* End of list */
803};
804MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
805
806static struct pci_driver pcipcwd_driver = {
807 .name = WATCHDOG_NAME,
808 .id_table = pcipcwd_pci_tbl,
809 .probe = pcipcwd_card_init,
810 .remove = pcipcwd_card_exit,
811};
812
813module_pci_driver(pcipcwd_driver);
814
815MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
816MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
817MODULE_LICENSE("GPL");
1/*
2 * Berkshire PCI-PC Watchdog Card Driver
3 *
4 * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
5 *
6 * Based on source code of the following authors:
7 * Ken Hollis <kenji@bitgate.com>,
8 * Lindsay Harris <lindsay@bluegum.com>,
9 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
10 * Matt Domsch <Matt_Domsch@dell.com>,
11 * Rob Radez <rob@osinvestor.com>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
17 *
18 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
19 * provide warranty for any of this software. This material is
20 * provided "AS-IS" and at no charge.
21 */
22
23/*
24 * A bells and whistles driver is available from:
25 * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
26 *
27 * More info available at
28 * http://www.berkprod.com/ or http://www.pcwatchdog.com/
29 */
30
31/*
32 * Includes, defines, variables, module parameters, ...
33 */
34
35#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36
37#include <linux/module.h> /* For module specific items */
38#include <linux/moduleparam.h> /* For new moduleparam's */
39#include <linux/types.h> /* For standard types (like size_t) */
40#include <linux/errno.h> /* For the -ENODEV/... values */
41#include <linux/kernel.h> /* For printk/panic/... */
42#include <linux/delay.h> /* For mdelay function */
43#include <linux/miscdevice.h> /* For struct miscdevice */
44#include <linux/watchdog.h> /* For the watchdog specific items */
45#include <linux/notifier.h> /* For notifier support */
46#include <linux/reboot.h> /* For reboot_notifier stuff */
47#include <linux/init.h> /* For __init/__exit/... */
48#include <linux/fs.h> /* For file operations */
49#include <linux/pci.h> /* For pci functions */
50#include <linux/ioport.h> /* For io-port access */
51#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
52#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
53#include <linux/io.h> /* For inb/outb/... */
54
55/* Module and version information */
56#define WATCHDOG_VERSION "1.03"
57#define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
58#define WATCHDOG_NAME "pcwd_pci"
59#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
60
61/* Stuff for the PCI ID's */
62#ifndef PCI_VENDOR_ID_QUICKLOGIC
63#define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
64#endif
65
66#ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
67#define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
68#endif
69
70/*
71 * These are the defines that describe the control status bits for the
72 * PCI-PC Watchdog card.
73 */
74/* Port 1 : Control Status #1 */
75#define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
76#define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
77#define WD_PCI_TTRP 0x04 /* Temperature Trip status */
78#define WD_PCI_RL2A 0x08 /* Relay 2 Active */
79#define WD_PCI_RL1A 0x10 /* Relay 1 Active */
80#define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
81 reset */
82#define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
83/* Port 2 : Control Status #2 */
84#define WD_PCI_WDIS 0x10 /* Watchdog Disable */
85#define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
86#define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
87#define WD_PCI_PCMD 0x80 /* PC has sent command */
88
89/* according to documentation max. time to process a command for the pci
90 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
91#define PCI_COMMAND_TIMEOUT 150
92
93/* Watchdog's internal commands */
94#define CMD_GET_STATUS 0x04
95#define CMD_GET_FIRMWARE_VERSION 0x08
96#define CMD_READ_WATCHDOG_TIMEOUT 0x18
97#define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
98#define CMD_GET_CLEAR_RESET_COUNT 0x84
99
100/* Watchdog's Dip Switch heartbeat values */
101static const int heartbeat_tbl[] = {
102 5, /* OFF-OFF-OFF = 5 Sec */
103 10, /* OFF-OFF-ON = 10 Sec */
104 30, /* OFF-ON-OFF = 30 Sec */
105 60, /* OFF-ON-ON = 1 Min */
106 300, /* ON-OFF-OFF = 5 Min */
107 600, /* ON-OFF-ON = 10 Min */
108 1800, /* ON-ON-OFF = 30 Min */
109 3600, /* ON-ON-ON = 1 hour */
110};
111
112/* We can only use 1 card due to the /dev/watchdog restriction */
113static int cards_found;
114
115/* internal variables */
116static int temp_panic;
117static unsigned long is_active;
118static char expect_release;
119/* this is private data for each PCI-PC watchdog card */
120static struct {
121 /* Wether or not the card has a temperature device */
122 int supports_temp;
123 /* The card's boot status */
124 int boot_status;
125 /* The cards I/O address */
126 unsigned long io_addr;
127 /* the lock for io operations */
128 spinlock_t io_lock;
129 /* the PCI-device */
130 struct pci_dev *pdev;
131} pcipcwd_private;
132
133/* module parameters */
134#define QUIET 0 /* Default */
135#define VERBOSE 1 /* Verbose */
136#define DEBUG 2 /* print fancy stuff too */
137static int debug = QUIET;
138module_param(debug, int, 0);
139MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
140
141#define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
142 delay-time from dip-switches */
143static int heartbeat = WATCHDOG_HEARTBEAT;
144module_param(heartbeat, int, 0);
145MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
146 "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
147 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
148
149static bool nowayout = WATCHDOG_NOWAYOUT;
150module_param(nowayout, bool, 0);
151MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
152 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
153
154/*
155 * Internal functions
156 */
157
158static int send_command(int cmd, int *msb, int *lsb)
159{
160 int got_response, count;
161
162 if (debug >= DEBUG)
163 pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
164 cmd, *msb, *lsb);
165
166 spin_lock(&pcipcwd_private.io_lock);
167 /* If a command requires data it should be written first.
168 * Data for commands with 8 bits of data should be written to port 4.
169 * Commands with 16 bits of data, should be written as LSB to port 4
170 * and MSB to port 5.
171 * After the required data has been written then write the command to
172 * port 6. */
173 outb_p(*lsb, pcipcwd_private.io_addr + 4);
174 outb_p(*msb, pcipcwd_private.io_addr + 5);
175 outb_p(cmd, pcipcwd_private.io_addr + 6);
176
177 /* wait till the pci card processed the command, signaled by
178 * the WRSP bit in port 2 and give it a max. timeout of
179 * PCI_COMMAND_TIMEOUT to process */
180 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
181 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
182 count++) {
183 mdelay(1);
184 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
185 }
186
187 if (debug >= DEBUG) {
188 if (got_response) {
189 pr_debug("time to process command was: %d ms\n",
190 count);
191 } else {
192 pr_debug("card did not respond on command!\n");
193 }
194 }
195
196 if (got_response) {
197 /* read back response */
198 *lsb = inb_p(pcipcwd_private.io_addr + 4);
199 *msb = inb_p(pcipcwd_private.io_addr + 5);
200
201 /* clear WRSP bit */
202 inb_p(pcipcwd_private.io_addr + 6);
203
204 if (debug >= DEBUG)
205 pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
206 cmd, *msb, *lsb);
207 }
208
209 spin_unlock(&pcipcwd_private.io_lock);
210
211 return got_response;
212}
213
214static inline void pcipcwd_check_temperature_support(void)
215{
216 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
217 pcipcwd_private.supports_temp = 1;
218}
219
220static int pcipcwd_get_option_switches(void)
221{
222 int option_switches;
223
224 option_switches = inb_p(pcipcwd_private.io_addr + 3);
225 return option_switches;
226}
227
228static void pcipcwd_show_card_info(void)
229{
230 int got_fw_rev, fw_rev_major, fw_rev_minor;
231 char fw_ver_str[20]; /* The cards firmware version */
232 int option_switches;
233
234 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
235 &fw_rev_minor);
236 if (got_fw_rev)
237 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
238 else
239 sprintf(fw_ver_str, "<card no answer>");
240
241 /* Get switch settings */
242 option_switches = pcipcwd_get_option_switches();
243
244 pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
245 (int) pcipcwd_private.io_addr, fw_ver_str,
246 (pcipcwd_private.supports_temp ? "with" : "without"));
247
248 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
249 option_switches,
250 ((option_switches & 0x10) ? "ON" : "OFF"),
251 ((option_switches & 0x08) ? "ON" : "OFF"));
252
253 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
254 pr_info("Previous reset was caused by the Watchdog card\n");
255
256 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
257 pr_info("Card sensed a CPU Overheat\n");
258
259 if (pcipcwd_private.boot_status == 0)
260 pr_info("No previous trip detected - Cold boot or reset\n");
261}
262
263static int pcipcwd_start(void)
264{
265 int stat_reg;
266
267 spin_lock(&pcipcwd_private.io_lock);
268 outb_p(0x00, pcipcwd_private.io_addr + 3);
269 udelay(1000);
270
271 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
272 spin_unlock(&pcipcwd_private.io_lock);
273
274 if (stat_reg & WD_PCI_WDIS) {
275 pr_err("Card timer not enabled\n");
276 return -1;
277 }
278
279 if (debug >= VERBOSE)
280 pr_debug("Watchdog started\n");
281
282 return 0;
283}
284
285static int pcipcwd_stop(void)
286{
287 int stat_reg;
288
289 spin_lock(&pcipcwd_private.io_lock);
290 outb_p(0xA5, pcipcwd_private.io_addr + 3);
291 udelay(1000);
292
293 outb_p(0xA5, pcipcwd_private.io_addr + 3);
294 udelay(1000);
295
296 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
297 spin_unlock(&pcipcwd_private.io_lock);
298
299 if (!(stat_reg & WD_PCI_WDIS)) {
300 pr_err("Card did not acknowledge disable attempt\n");
301 return -1;
302 }
303
304 if (debug >= VERBOSE)
305 pr_debug("Watchdog stopped\n");
306
307 return 0;
308}
309
310static int pcipcwd_keepalive(void)
311{
312 /* Re-trigger watchdog by writing to port 0 */
313 spin_lock(&pcipcwd_private.io_lock);
314 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
315 spin_unlock(&pcipcwd_private.io_lock);
316
317 if (debug >= DEBUG)
318 pr_debug("Watchdog keepalive signal send\n");
319
320 return 0;
321}
322
323static int pcipcwd_set_heartbeat(int t)
324{
325 int t_msb = t / 256;
326 int t_lsb = t % 256;
327
328 if ((t < 0x0001) || (t > 0xFFFF))
329 return -EINVAL;
330
331 /* Write new heartbeat to watchdog */
332 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
333
334 heartbeat = t;
335 if (debug >= VERBOSE)
336 pr_debug("New heartbeat: %d\n", heartbeat);
337
338 return 0;
339}
340
341static int pcipcwd_get_status(int *status)
342{
343 int control_status;
344
345 *status = 0;
346 control_status = inb_p(pcipcwd_private.io_addr + 1);
347 if (control_status & WD_PCI_WTRP)
348 *status |= WDIOF_CARDRESET;
349 if (control_status & WD_PCI_TTRP) {
350 *status |= WDIOF_OVERHEAT;
351 if (temp_panic)
352 panic(KBUILD_MODNAME ": Temperature overheat trip!\n");
353 }
354
355 if (debug >= DEBUG)
356 pr_debug("Control Status #1: 0x%02x\n", control_status);
357
358 return 0;
359}
360
361static int pcipcwd_clear_status(void)
362{
363 int control_status;
364 int msb;
365 int reset_counter;
366
367 if (debug >= VERBOSE)
368 pr_info("clearing watchdog trip status & LED\n");
369
370 control_status = inb_p(pcipcwd_private.io_addr + 1);
371
372 if (debug >= DEBUG) {
373 pr_debug("status was: 0x%02x\n", control_status);
374 pr_debug("sending: 0x%02x\n",
375 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
376 }
377
378 /* clear trip status & LED and keep mode of relay 2 */
379 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
380 pcipcwd_private.io_addr + 1);
381
382 /* clear reset counter */
383 msb = 0;
384 reset_counter = 0xff;
385 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
386
387 if (debug >= DEBUG) {
388 pr_debug("reset count was: 0x%02x\n", reset_counter);
389 }
390
391 return 0;
392}
393
394static int pcipcwd_get_temperature(int *temperature)
395{
396 *temperature = 0;
397 if (!pcipcwd_private.supports_temp)
398 return -ENODEV;
399
400 spin_lock(&pcipcwd_private.io_lock);
401 *temperature = inb_p(pcipcwd_private.io_addr);
402 spin_unlock(&pcipcwd_private.io_lock);
403
404 /*
405 * Convert celsius to fahrenheit, since this was
406 * the decided 'standard' for this return value.
407 */
408 *temperature = (*temperature * 9 / 5) + 32;
409
410 if (debug >= DEBUG) {
411 pr_debug("temperature is: %d F\n", *temperature);
412 }
413
414 return 0;
415}
416
417static int pcipcwd_get_timeleft(int *time_left)
418{
419 int msb;
420 int lsb;
421
422 /* Read the time that's left before rebooting */
423 /* Note: if the board is not yet armed then we will read 0xFFFF */
424 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
425
426 *time_left = (msb << 8) + lsb;
427
428 if (debug >= VERBOSE)
429 pr_debug("Time left before next reboot: %d\n", *time_left);
430
431 return 0;
432}
433
434/*
435 * /dev/watchdog handling
436 */
437
438static ssize_t pcipcwd_write(struct file *file, const char __user *data,
439 size_t len, loff_t *ppos)
440{
441 /* See if we got the magic character 'V' and reload the timer */
442 if (len) {
443 if (!nowayout) {
444 size_t i;
445
446 /* note: just in case someone wrote the magic character
447 * five months ago... */
448 expect_release = 0;
449
450 /* scan to see whether or not we got the
451 * magic character */
452 for (i = 0; i != len; i++) {
453 char c;
454 if (get_user(c, data + i))
455 return -EFAULT;
456 if (c == 'V')
457 expect_release = 42;
458 }
459 }
460
461 /* someone wrote to us, we should reload the timer */
462 pcipcwd_keepalive();
463 }
464 return len;
465}
466
467static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
468 unsigned long arg)
469{
470 void __user *argp = (void __user *)arg;
471 int __user *p = argp;
472 static const struct watchdog_info ident = {
473 .options = WDIOF_OVERHEAT |
474 WDIOF_CARDRESET |
475 WDIOF_KEEPALIVEPING |
476 WDIOF_SETTIMEOUT |
477 WDIOF_MAGICCLOSE,
478 .firmware_version = 1,
479 .identity = WATCHDOG_DRIVER_NAME,
480 };
481
482 switch (cmd) {
483 case WDIOC_GETSUPPORT:
484 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
485
486 case WDIOC_GETSTATUS:
487 {
488 int status;
489 pcipcwd_get_status(&status);
490 return put_user(status, p);
491 }
492
493 case WDIOC_GETBOOTSTATUS:
494 return put_user(pcipcwd_private.boot_status, p);
495
496 case WDIOC_GETTEMP:
497 {
498 int temperature;
499
500 if (pcipcwd_get_temperature(&temperature))
501 return -EFAULT;
502
503 return put_user(temperature, p);
504 }
505
506 case WDIOC_SETOPTIONS:
507 {
508 int new_options, retval = -EINVAL;
509
510 if (get_user(new_options, p))
511 return -EFAULT;
512
513 if (new_options & WDIOS_DISABLECARD) {
514 if (pcipcwd_stop())
515 return -EIO;
516 retval = 0;
517 }
518
519 if (new_options & WDIOS_ENABLECARD) {
520 if (pcipcwd_start())
521 return -EIO;
522 retval = 0;
523 }
524
525 if (new_options & WDIOS_TEMPPANIC) {
526 temp_panic = 1;
527 retval = 0;
528 }
529
530 return retval;
531 }
532
533 case WDIOC_KEEPALIVE:
534 pcipcwd_keepalive();
535 return 0;
536
537 case WDIOC_SETTIMEOUT:
538 {
539 int new_heartbeat;
540
541 if (get_user(new_heartbeat, p))
542 return -EFAULT;
543
544 if (pcipcwd_set_heartbeat(new_heartbeat))
545 return -EINVAL;
546
547 pcipcwd_keepalive();
548 }
549 /* fall through */
550
551 case WDIOC_GETTIMEOUT:
552 return put_user(heartbeat, p);
553
554 case WDIOC_GETTIMELEFT:
555 {
556 int time_left;
557
558 if (pcipcwd_get_timeleft(&time_left))
559 return -EFAULT;
560
561 return put_user(time_left, p);
562 }
563
564 default:
565 return -ENOTTY;
566 }
567}
568
569static int pcipcwd_open(struct inode *inode, struct file *file)
570{
571 /* /dev/watchdog can only be opened once */
572 if (test_and_set_bit(0, &is_active)) {
573 if (debug >= VERBOSE)
574 pr_err("Attempt to open already opened device\n");
575 return -EBUSY;
576 }
577
578 /* Activate */
579 pcipcwd_start();
580 pcipcwd_keepalive();
581 return nonseekable_open(inode, file);
582}
583
584static int pcipcwd_release(struct inode *inode, struct file *file)
585{
586 /*
587 * Shut off the timer.
588 */
589 if (expect_release == 42) {
590 pcipcwd_stop();
591 } else {
592 pr_crit("Unexpected close, not stopping watchdog!\n");
593 pcipcwd_keepalive();
594 }
595 expect_release = 0;
596 clear_bit(0, &is_active);
597 return 0;
598}
599
600/*
601 * /dev/temperature handling
602 */
603
604static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
605 size_t len, loff_t *ppos)
606{
607 int temperature;
608
609 if (pcipcwd_get_temperature(&temperature))
610 return -EFAULT;
611
612 if (copy_to_user(data, &temperature, 1))
613 return -EFAULT;
614
615 return 1;
616}
617
618static int pcipcwd_temp_open(struct inode *inode, struct file *file)
619{
620 if (!pcipcwd_private.supports_temp)
621 return -ENODEV;
622
623 return nonseekable_open(inode, file);
624}
625
626static int pcipcwd_temp_release(struct inode *inode, struct file *file)
627{
628 return 0;
629}
630
631/*
632 * Notify system
633 */
634
635static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
636 void *unused)
637{
638 if (code == SYS_DOWN || code == SYS_HALT)
639 pcipcwd_stop(); /* Turn the WDT off */
640
641 return NOTIFY_DONE;
642}
643
644/*
645 * Kernel Interfaces
646 */
647
648static const struct file_operations pcipcwd_fops = {
649 .owner = THIS_MODULE,
650 .llseek = no_llseek,
651 .write = pcipcwd_write,
652 .unlocked_ioctl = pcipcwd_ioctl,
653 .open = pcipcwd_open,
654 .release = pcipcwd_release,
655};
656
657static struct miscdevice pcipcwd_miscdev = {
658 .minor = WATCHDOG_MINOR,
659 .name = "watchdog",
660 .fops = &pcipcwd_fops,
661};
662
663static const struct file_operations pcipcwd_temp_fops = {
664 .owner = THIS_MODULE,
665 .llseek = no_llseek,
666 .read = pcipcwd_temp_read,
667 .open = pcipcwd_temp_open,
668 .release = pcipcwd_temp_release,
669};
670
671static struct miscdevice pcipcwd_temp_miscdev = {
672 .minor = TEMP_MINOR,
673 .name = "temperature",
674 .fops = &pcipcwd_temp_fops,
675};
676
677static struct notifier_block pcipcwd_notifier = {
678 .notifier_call = pcipcwd_notify_sys,
679};
680
681/*
682 * Init & exit routines
683 */
684
685static int pcipcwd_card_init(struct pci_dev *pdev,
686 const struct pci_device_id *ent)
687{
688 int ret = -EIO;
689
690 cards_found++;
691 if (cards_found == 1)
692 pr_info("%s\n", DRIVER_VERSION);
693
694 if (cards_found > 1) {
695 pr_err("This driver only supports 1 device\n");
696 return -ENODEV;
697 }
698
699 if (pci_enable_device(pdev)) {
700 pr_err("Not possible to enable PCI Device\n");
701 return -ENODEV;
702 }
703
704 if (pci_resource_start(pdev, 0) == 0x0000) {
705 pr_err("No I/O-Address for card detected\n");
706 ret = -ENODEV;
707 goto err_out_disable_device;
708 }
709
710 spin_lock_init(&pcipcwd_private.io_lock);
711 pcipcwd_private.pdev = pdev;
712 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
713
714 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
715 pr_err("I/O address 0x%04x already in use\n",
716 (int) pcipcwd_private.io_addr);
717 ret = -EIO;
718 goto err_out_disable_device;
719 }
720
721 /* get the boot_status */
722 pcipcwd_get_status(&pcipcwd_private.boot_status);
723
724 /* clear the "card caused reboot" flag */
725 pcipcwd_clear_status();
726
727 /* disable card */
728 pcipcwd_stop();
729
730 /* Check whether or not the card supports the temperature device */
731 pcipcwd_check_temperature_support();
732
733 /* Show info about the card itself */
734 pcipcwd_show_card_info();
735
736 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
737 if (heartbeat == 0)
738 heartbeat =
739 heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
740
741 /* Check that the heartbeat value is within it's range ;
742 * if not reset to the default */
743 if (pcipcwd_set_heartbeat(heartbeat)) {
744 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
745 pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
746 WATCHDOG_HEARTBEAT);
747 }
748
749 ret = register_reboot_notifier(&pcipcwd_notifier);
750 if (ret != 0) {
751 pr_err("cannot register reboot notifier (err=%d)\n", ret);
752 goto err_out_release_region;
753 }
754
755 if (pcipcwd_private.supports_temp) {
756 ret = misc_register(&pcipcwd_temp_miscdev);
757 if (ret != 0) {
758 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
759 TEMP_MINOR, ret);
760 goto err_out_unregister_reboot;
761 }
762 }
763
764 ret = misc_register(&pcipcwd_miscdev);
765 if (ret != 0) {
766 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
767 WATCHDOG_MINOR, ret);
768 goto err_out_misc_deregister;
769 }
770
771 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
772 heartbeat, nowayout);
773
774 return 0;
775
776err_out_misc_deregister:
777 if (pcipcwd_private.supports_temp)
778 misc_deregister(&pcipcwd_temp_miscdev);
779err_out_unregister_reboot:
780 unregister_reboot_notifier(&pcipcwd_notifier);
781err_out_release_region:
782 pci_release_regions(pdev);
783err_out_disable_device:
784 pci_disable_device(pdev);
785 return ret;
786}
787
788static void pcipcwd_card_exit(struct pci_dev *pdev)
789{
790 /* Stop the timer before we leave */
791 if (!nowayout)
792 pcipcwd_stop();
793
794 /* Deregister */
795 misc_deregister(&pcipcwd_miscdev);
796 if (pcipcwd_private.supports_temp)
797 misc_deregister(&pcipcwd_temp_miscdev);
798 unregister_reboot_notifier(&pcipcwd_notifier);
799 pci_release_regions(pdev);
800 pci_disable_device(pdev);
801 cards_found--;
802}
803
804static const struct pci_device_id pcipcwd_pci_tbl[] = {
805 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
806 PCI_ANY_ID, PCI_ANY_ID, },
807 { 0 }, /* End of list */
808};
809MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
810
811static struct pci_driver pcipcwd_driver = {
812 .name = WATCHDOG_NAME,
813 .id_table = pcipcwd_pci_tbl,
814 .probe = pcipcwd_card_init,
815 .remove = pcipcwd_card_exit,
816};
817
818module_pci_driver(pcipcwd_driver);
819
820MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
821MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
822MODULE_LICENSE("GPL");