// SPDX-License-Identifier: GPL-2.0+ /* * Berkshire PCI-PC Watchdog Card Driver * * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>. * * Based on source code of the following authors: * Ken Hollis <kenji@bitgate.com>, * Lindsay Harris <lindsay@bluegum.com>, * Alan Cox <alan@lxorguk.ukuu.org.uk>, * Matt Domsch <Matt_Domsch@dell.com>, * Rob Radez <rob@osinvestor.com> * * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor * provide warranty for any of this software. This material is * provided "AS-IS" and at no charge.
*/
#include <linux/module.h> /* For module specific items */ #include <linux/moduleparam.h> /* For new moduleparam's */ #include <linux/types.h> /* For standard types (like size_t) */ #include <linux/errno.h> /* For the -ENODEV/... values */ #include <linux/kernel.h> /* For printk/panic/... */ #include <linux/delay.h> /* For mdelay function */ #include <linux/miscdevice.h> /* For struct miscdevice */ #include <linux/watchdog.h> /* For the watchdog specific items */ #include <linux/notifier.h> /* For notifier support */ #include <linux/reboot.h> /* For reboot_notifier stuff */ #include <linux/init.h> /* For __init/__exit/... */ #include <linux/fs.h> /* For file operations */ #include <linux/pci.h> /* For pci functions */ #include <linux/ioport.h> /* For io-port access */ #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */ #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ #include <linux/io.h> /* For inb/outb/... */
/* Module and version information */ #define WATCHDOG_VERSION "1.03" #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog" #define WATCHDOG_NAME "pcwd_pci" #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
/* Stuff for the PCI ID's */ #ifndef PCI_VENDOR_ID_QUICKLOGIC #define PCI_VENDOR_ID_QUICKLOGIC 0x11e3 #endif
/* * These are the defines that describe the control status bits for the * PCI-PC Watchdog card.
*/ /* Port 1 : Control Status #1 */ #define WD_PCI_WTRP 0x01 /* Watchdog Trip status */ #define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */ #define WD_PCI_TTRP 0x04 /* Temperature Trip status */ #define WD_PCI_RL2A 0x08 /* Relay 2 Active */ #define WD_PCI_RL1A 0x10 /* Relay 1 Active */ #define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
reset */ #define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */ /* Port 2 : Control Status #2 */ #define WD_PCI_WDIS 0x10 /* Watchdog Disable */ #define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */ #define WD_PCI_WRSP 0x40 /* Watchdog wrote response */ #define WD_PCI_PCMD 0x80 /* PC has sent command */
/* according to documentation max. time to process a command for the pci
* watchdog card is 100 ms, so we give it 150 ms to do it's job */ #define PCI_COMMAND_TIMEOUT 150
/* We can only use 1 card due to the /dev/watchdog restriction */ staticint cards_found;
/* internal variables */ staticint temp_panic; staticunsignedlong is_active; staticchar expect_release; /* this is private data for each PCI-PC watchdog card */ staticstruct { /* Wether or not the card has a temperature device */ int supports_temp; /* The card's boot status */ int boot_status; /* The cards I/O address */ unsignedlong io_addr; /* the lock for io operations */
spinlock_t io_lock; /* the PCI-device */ struct pci_dev *pdev;
} pcipcwd_private;
staticbool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0);
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
/* * Internal functions
*/
staticint send_command(int cmd, int *msb, int *lsb)
{ int got_response, count;
if (debug >= DEBUG)
pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
cmd, *msb, *lsb);
spin_lock(&pcipcwd_private.io_lock); /* If a command requires data it should be written first. * Data for commands with 8 bits of data should be written to port 4. * Commands with 16 bits of data, should be written as LSB to port 4 * and MSB to port 5. * After the required data has been written then write the command to
* port 6. */
outb_p(*lsb, pcipcwd_private.io_addr + 4);
outb_p(*msb, pcipcwd_private.io_addr + 5);
outb_p(cmd, pcipcwd_private.io_addr + 6);
/* wait till the pci card processed the command, signaled by * the WRSP bit in port 2 and give it a max. timeout of
* PCI_COMMAND_TIMEOUT to process */
got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP; for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
count++) {
mdelay(1);
got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
}
if (debug >= DEBUG) { if (got_response) {
pr_debug("time to process command was: %d ms\n",
count);
} else {
pr_debug("card did not respond on command!\n");
}
}
if (got_response) { /* read back response */
*lsb = inb_p(pcipcwd_private.io_addr + 4);
*msb = inb_p(pcipcwd_private.io_addr + 5);
/* clear WRSP bit */
inb_p(pcipcwd_private.io_addr + 6);
if (debug >= DEBUG)
pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
cmd, *msb, *lsb);
}
staticvoid pcipcwd_show_card_info(void)
{ int got_fw_rev, fw_rev_major, fw_rev_minor; char fw_ver_str[20]; /* The cards firmware version */ int option_switches;
if (!(stat_reg & WD_PCI_WDIS)) {
pr_err("Card did not acknowledge disable attempt\n"); return -1;
}
if (debug >= VERBOSE)
pr_debug("Watchdog stopped\n");
return 0;
}
staticint pcipcwd_keepalive(void)
{ /* Re-trigger watchdog by writing to port 0 */
spin_lock(&pcipcwd_private.io_lock);
outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
spin_unlock(&pcipcwd_private.io_lock);
if (debug >= DEBUG)
pr_debug("Watchdog keepalive signal send\n");
return 0;
}
staticint pcipcwd_set_heartbeat(int t)
{ int t_msb = t / 256; int t_lsb = t % 256;
if ((t < 0x0001) || (t > 0xFFFF)) return -EINVAL;
/* Write new heartbeat to watchdog */
send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
/* * Convert celsius to fahrenheit, since this was * the decided 'standard' for this return value.
*/
*temperature = (*temperature * 9 / 5) + 32;
if (debug >= DEBUG) {
pr_debug("temperature is: %d F\n", *temperature);
}
return 0;
}
staticint pcipcwd_get_timeleft(int *time_left)
{ int msb; int lsb;
/* Read the time that's left before rebooting */ /* Note: if the board is not yet armed then we will read 0xFFFF */
send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
*time_left = (msb << 8) + lsb;
if (debug >= VERBOSE)
pr_debug("Time left before next reboot: %d\n", *time_left);
return 0;
}
/* * /dev/watchdog handling
*/
static ssize_t pcipcwd_write(struct file *file, constchar __user *data,
size_t len, loff_t *ppos)
{ /* See if we got the magic character 'V' and reload the timer */ if (len) { if (!nowayout) {
size_t i;
/* note: just in case someone wrote the magic character
* five months ago... */
expect_release = 0;
/* scan to see whether or not we got the
* magic character */ for (i = 0; i != len; i++) { char c; if (get_user(c, data + i)) return -EFAULT; if (c == 'V')
expect_release = 42;
}
}
/* someone wrote to us, we should reload the timer */
pcipcwd_keepalive();
} return len;
}
case WDIOC_KEEPALIVE:
pcipcwd_keepalive(); return 0;
case WDIOC_SETTIMEOUT:
{ int new_heartbeat;
if (get_user(new_heartbeat, p)) return -EFAULT;
if (pcipcwd_set_heartbeat(new_heartbeat)) return -EINVAL;
pcipcwd_keepalive();
}
fallthrough;
case WDIOC_GETTIMEOUT: return put_user(heartbeat, p);
case WDIOC_GETTIMELEFT:
{ int time_left;
if (pcipcwd_get_timeleft(&time_left)) return -EFAULT;
return put_user(time_left, p);
}
default: return -ENOTTY;
}
}
staticint pcipcwd_open(struct inode *inode, struct file *file)
{ /* /dev/watchdog can only be opened once */ if (test_and_set_bit(0, &is_active)) { if (debug >= VERBOSE)
pr_err("Attempt to open already opened device\n"); return -EBUSY;
}
if (pci_request_regions(pdev, WATCHDOG_NAME)) {
pr_err("I/O address 0x%04x already in use\n",
(int) pcipcwd_private.io_addr);
ret = -EIO; goto err_out_disable_device;
}
/* get the boot_status */
pcipcwd_get_status(&pcipcwd_private.boot_status);
/* clear the "card caused reboot" flag */
pcipcwd_clear_status();
/* disable card */
pcipcwd_stop();
/* Check whether or not the card supports the temperature device */
pcipcwd_check_temperature_support();
/* Show info about the card itself */
pcipcwd_show_card_info();
/* If heartbeat = 0 then we use the heartbeat from the dip-switches */ if (heartbeat == 0)
heartbeat =
heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
/* Check that the heartbeat value is within it's range ;
* if not reset to the default */ if (pcipcwd_set_heartbeat(heartbeat)) {
pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
pr_info("heartbeat value must be 0,
WATCHDOG_HEARTBEAT);
}
ret = register_reboot_notifier(&pcipcwd_notifier); if (ret != 0) {
pr_err("cannot register reboot notifier (err=%d)\n", ret); goto err_out_release_region;
}
if (pcipcwd_private.supports_temp) {
ret = misc_register(&pcipcwd_temp_miscdev); if (ret != 0) {
pr_err("cannot register miscdev on minor=%d (err=%d)\n",
TEMP_MINOR, ret); goto err_out_unregister_reboot;
}
}
ret = misc_register(&pcipcwd_miscdev); if (ret != 0) {
pr_err("cannot register miscdev on minor=%d (err=%d)\n",
WATCHDOG_MINOR, ret); goto err_out_misc_deregister;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.