📄 pcwd_pci.c
字号:
/* * Berkshire PCI-PC Watchdog Card Driver * * (c) Copyright 2003 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@redhat.com>, * Matt Domsch <Matt_Domsch@dell.com>, * Rob Radez <rob@osinvestor.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * 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. *//* * A bells and whistles driver is available from http://www.pcwd.de/ * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/ *//* * Includes, defines, variables, module parameters, ... */#include <linux/config.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/types.h>#include <linux/delay.h>#include <linux/miscdevice.h>#include <linux/watchdog.h>#include <linux/notifier.h>#include <linux/reboot.h>#include <linux/init.h>#include <linux/fs.h>#include <linux/pci.h>#include <linux/ioport.h>#include <linux/spinlock.h>#include <asm/uaccess.h>#include <asm/io.h>/* Module and version information */#define WATCHDOG_VERSION "1.00"#define WATCHDOG_DATE "12 Jun 2004"#define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"#define WATCHDOG_NAME "pcwd_pci"#define PFX WATCHDOG_NAME ": "#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"/* Stuff for the PCI ID's */#ifndef PCI_VENDOR_ID_QUICKLOGIC#define PCI_VENDOR_ID_QUICKLOGIC 0x11e3#endif#ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD#define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030#endif/* * These are the defines that describe the control status bits for the * PCI-PC Watchdog card. */#define WD_PCI_WTRP 0x01 /* Watchdog Trip status */#define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */#define WD_PCI_TTRP 0x04 /* Temperature Trip status *//* 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/* Watchdog's internal commands */#define CMD_GET_STATUS 0x04#define CMD_GET_FIRMWARE_VERSION 0x08#define CMD_READ_WATCHDOG_TIMEOUT 0x18#define CMD_WRITE_WATCHDOG_TIMEOUT 0x19/* We can only use 1 card due to the /dev/watchdog restriction */static int cards_found;/* internal variables */static int temp_panic;static unsigned long is_active;static char expect_release;static struct { int supports_temp; /* Wether or not the card has a temperature device */ int boot_status; /* The card's boot status */ unsigned long io_addr; /* The cards I/O address */ spinlock_t io_lock; struct pci_dev *pdev;} pcipcwd_private;/* module parameters */#define WATCHDOG_HEARTBEAT 2 /* 2 sec default heartbeat */static int heartbeat = WATCHDOG_HEARTBEAT;module_param(heartbeat, int, 0);MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");#ifdef CONFIG_WATCHDOG_NOWAYOUTstatic int nowayout = 1;#elsestatic int nowayout = 0;#endifmodule_param(nowayout, int, 0);MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");/* * Internal functions */static int send_command(int cmd, int *msb, int *lsb){ int got_response, count; 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) & 0x40; for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) { mdelay(1); got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40; } 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); } spin_unlock(&pcipcwd_private.io_lock); return got_response;}static int pcipcwd_start(void){ int stat_reg; spin_lock(&pcipcwd_private.io_lock); outb_p(0x00, pcipcwd_private.io_addr + 3); udelay(1000); stat_reg = inb_p(pcipcwd_private.io_addr + 2); spin_unlock(&pcipcwd_private.io_lock); if (stat_reg & 0x10) { printk(KERN_ERR PFX "Card timer not enabled\n"); return -1; } return 0;}static int pcipcwd_stop(void){ int stat_reg; spin_lock(&pcipcwd_private.io_lock); outb_p(0xA5, pcipcwd_private.io_addr + 3); udelay(1000); outb_p(0xA5, pcipcwd_private.io_addr + 3); udelay(1000); stat_reg = inb_p(pcipcwd_private.io_addr + 2); spin_unlock(&pcipcwd_private.io_lock); if (!(stat_reg & 0x10)) { printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n"); return -1; } return 0;}static int pcipcwd_keepalive(void){ /* Re-trigger watchdog by writing to port 0 */ outb_p(0x42, pcipcwd_private.io_addr); return 0;}static int 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); heartbeat = t; return 0;}static int pcipcwd_get_status(int *status){ int new_status; *status=0; new_status = inb_p(pcipcwd_private.io_addr + 1); if (new_status & WD_PCI_WTRP) *status |= WDIOF_CARDRESET; if (new_status & WD_PCI_TTRP) { *status |= WDIOF_OVERHEAT; if (temp_panic) panic(PFX "Temperature overheat trip!\n"); } return 0;}static int pcipcwd_clear_status(void){ outb_p(0x01, pcipcwd_private.io_addr + 1); return 0;}static int pcipcwd_get_temperature(int *temperature){ *temperature = 0; if (!pcipcwd_private.supports_temp) return -ENODEV; /* * Convert celsius to fahrenheit, since this was * the decided 'standard' for this return value. */ *temperature = ((inb_p(pcipcwd_private.io_addr)) * 9 / 5) + 32; return 0;}/* * /dev/watchdog handling */static ssize_t pcipcwd_write(struct file *file, const char __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;}static int pcipcwd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ void __user *argp = (void __user *)arg; int __user *p = argp; static struct watchdog_info ident = { .options = WDIOF_OVERHEAT | WDIOF_CARDRESET | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, .firmware_version = 1, .identity = WATCHDOG_DRIVER_NAME, }; switch (cmd) { case WDIOC_GETSUPPORT: return copy_to_user(argp, &ident, sizeof (ident)) ? -EFAULT : 0; case WDIOC_GETSTATUS: { int status; pcipcwd_get_status(&status); return put_user(status, p); } case WDIOC_GETBOOTSTATUS: return put_user(pcipcwd_private.boot_status, p); case WDIOC_GETTEMP: { int temperature; if (pcipcwd_get_temperature(&temperature)) return -EFAULT; return put_user(temperature, p); } case WDIOC_KEEPALIVE: pcipcwd_keepalive(); return 0; case WDIOC_SETOPTIONS: { int new_options, retval = -EINVAL; if (get_user (new_options, p))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -