📄 pcwd.c
字号:
/* * PC Watchdog Driver * by Ken Hollis (khollis@bitgate.com) * * Permission granted from Simon Machell (73244.1270@compuserve.com) * Written for the Linux Kernel, and GPLed by Ken Hollis * * 960107 Added request_region routines, modulized the whole thing. * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added * WD_TIMEOUT define. * 960216 Added eof marker on the file, and changed verbose messages. * 960716 Made functional and cosmetic changes to the source for * inclusion in Linux 2.0.x kernels, thanks to Alan Cox. * 960717 Removed read/seek routines, replaced with ioctl. Also, added * check_region command due to Alan's suggestion. * 960821 Made changes to compile in newer 2.0.x kernels. Added * "cold reboot sense" entry. * 960825 Made a few changes to code, deleted some defines and made * typedefs to replace them. Made heartbeat reset only available * via ioctl, and removed the write routine. * 960828 Added new items for PC Watchdog Rev.C card. * 960829 Changed around all of the IOCTLs, added new features, * added watchdog disable/re-enable routines. Added firmware * version reporting. Added read routine for temperature. * Removed some extra defines, added an autodetect Revision * routine. * 961006 Revised some documentation, fixed some cosmetic bugs. Made * drivers to panic the system if it's overheating at bootup. * 961118 Changed some verbiage on some of the output, tidied up * code bits, and added compatibility to 2.1.x. * 970912 Enabled board on open and disable on close. * 971107 Took account of recent VFS changes (broke read). * 971210 Disable board on initialisation in case board already ticking. * 971222 Changed open/close for temperature handling * Michael Meskes <meskes@debian.org>. * 980112 Used minor numbers from include/linux/miscdevice.h * 990403 Clear reset status after reading control status register in * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>] * 990605 Made changes to code to support Firmware 1.22a, added * fairly useless proc entry. * 990610 removed said useless proc code for the merge <alan> * 000403 Removed last traces of proc code. <davej> * 011214 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com> * Added timeout module option to override default *//* * 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/ */#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/types.h>#include <linux/timer.h>#include <linux/jiffies.h>#include <linux/config.h>#include <linux/wait.h>#include <linux/slab.h>#include <linux/ioport.h>#include <linux/delay.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/watchdog.h>#include <linux/notifier.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/reboot.h>#include <linux/sched.h> /* TASK_INTERRUPTIBLE, set_current_state() and friends */#include <asm/uaccess.h>#include <asm/io.h>#define WD_VER "1.16 (06/12/2004)"#define PFX "pcwd: "/* * It should be noted that PCWD_REVISION_B was removed because A and B * are essentially the same types of card, with the exception that B * has temperature reporting. Since I didn't receive a Rev.B card, * the Rev.B card is not supported. (It's a good thing too, as they * are no longer in production.) */#define PCWD_REVISION_A 1#define PCWD_REVISION_C 2/* * These are the defines that describe the control status bits for the * PC Watchdog card, revision A. */#define WD_WDRST 0x01 /* Previously reset state */#define WD_T110 0x02 /* Temperature overheat sense */#define WD_HRTBT 0x04 /* Heartbeat sense */#define WD_RLY2 0x08 /* External relay triggered */#define WD_SRLY2 0x80 /* Software external relay triggered *//* * These are the defines that describe the control status bits for the * PC Watchdog card, revision C. */#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */#define WD_REVC_TTRP 0x04 /* Temperature Trip status *//* max. time we give an ISA watchdog card to process a command *//* 500ms for each 4 bit response (according to spec.) */#define ISA_COMMAND_TIMEOUT 1000/* Watchdog's internal commands */#define CMD_ISA_IDLE 0x00#define CMD_ISA_VERSION_INTEGER 0x01#define CMD_ISA_VERSION_TENTH 0x02#define CMD_ISA_VERSION_HUNDRETH 0x03#define CMD_ISA_VERSION_MINOR 0x04#define CMD_ISA_SWITCH_SETTINGS 0x05#define CMD_ISA_DELAY_TIME_2SECS 0x0A#define CMD_ISA_DELAY_TIME_4SECS 0x0B#define CMD_ISA_DELAY_TIME_8SECS 0x0C/* * We are using an kernel timer to do the pinging of the watchdog * every ~500ms. We try to set the internal heartbeat of the * watchdog to 2 ms. */#define WDT_INTERVAL (HZ/2+1)/* We can only use 1 card due to the /dev/watchdog restriction */static int cards_found;/* internal variables */static atomic_t open_allowed = ATOMIC_INIT(1);static char expect_close;static struct timer_list timer;static unsigned long next_heartbeat;static int temp_panic;static int revision; /* The card's revision */static int supports_temp; /* Wether or not the card has a temperature device */static int command_mode; /* Wether or not the card is in command mode */static int initial_status; /* The card's boot status */static int current_readport; /* The cards I/O address */static spinlock_t io_lock;/* module parameters */#define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */static int heartbeat = WATCHDOG_HEARTBEAT;module_param(heartbeat, int, 0);MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");static int nowayout = WATCHDOG_NOWAYOUT;module_param(nowayout, int, 0);MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");/* * Internal functions */static int send_isa_command(int cmd){ int i; int control_status; int port0, last_port0; /* Double read for stabilising */ /* The WCMD bit must be 1 and the command is only 4 bits in size */ control_status = (cmd & 0x0F) | 0x80; outb_p(control_status, current_readport + 2); udelay(ISA_COMMAND_TIMEOUT); port0 = inb_p(current_readport); for (i = 0; i < 25; ++i) { last_port0 = port0; port0 = inb_p(current_readport); if (port0 == last_port0) break; /* Data is stable */ udelay (250); } return port0;}static int set_command_mode(void){ int i, found=0, count=0; /* Set the card into command mode */ spin_lock(&io_lock); while ((!found) && (count < 3)) { i = send_isa_command(CMD_ISA_IDLE); if (i == 0x00) found = 1; else if (i == 0xF3) { /* Card does not like what we've done to it */ outb_p(0x00, current_readport + 2); udelay(1200); /* Spec says wait 1ms */ outb_p(0x00, current_readport + 2); udelay(ISA_COMMAND_TIMEOUT); } count++; } spin_unlock(&io_lock); command_mode = found; return(found);}static void unset_command_mode(void){ /* Set the card into normal mode */ spin_lock(&io_lock); outb_p(0x00, current_readport + 2); udelay(ISA_COMMAND_TIMEOUT); spin_unlock(&io_lock); command_mode = 0;}static void pcwd_timer_ping(unsigned long data){ int wdrst_stat; /* If we got a heartbeat pulse within the WDT_INTERVAL * we agree to ping the WDT */ if(time_before(jiffies, next_heartbeat)) { /* Ping the watchdog */ spin_lock(&io_lock); if (revision == PCWD_REVISION_A) { /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */ wdrst_stat = inb_p(current_readport); wdrst_stat &= 0x0F; wdrst_stat |= WD_WDRST; outb_p(wdrst_stat, current_readport + 1); } else { /* Re-trigger watchdog by writing to port 0 */ outb_p(0x00, current_readport); } /* Re-set the timer interval */ mod_timer(&timer, jiffies + WDT_INTERVAL); spin_unlock(&io_lock); } else { printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n"); }}static int pcwd_start(void){ int stat_reg; next_heartbeat = jiffies + (heartbeat * HZ); /* Start the timer */ mod_timer(&timer, jiffies + WDT_INTERVAL); /* Enable the port */ if (revision == PCWD_REVISION_C) { spin_lock(&io_lock); outb_p(0x00, current_readport + 3); udelay(ISA_COMMAND_TIMEOUT); stat_reg = inb_p(current_readport + 2); spin_unlock(&io_lock); if (stat_reg & 0x10) { printk(KERN_INFO PFX "Could not start watchdog\n"); return -EIO; } } return 0;}static int pcwd_stop(void){ int stat_reg; /* Stop the timer */ del_timer(&timer); /* Disable the board */ if (revision == PCWD_REVISION_C) { spin_lock(&io_lock); outb_p(0xA5, current_readport + 3); udelay(ISA_COMMAND_TIMEOUT); outb_p(0xA5, current_readport + 3); udelay(ISA_COMMAND_TIMEOUT); stat_reg = inb_p(current_readport + 2); spin_unlock(&io_lock); if ((stat_reg & 0x10) == 0) { printk(KERN_INFO PFX "Could not stop watchdog\n"); return -EIO; } } return 0;}static int pcwd_keepalive(void){ /* user land ping */ next_heartbeat = jiffies + (heartbeat * HZ); return 0;}static int pcwd_set_heartbeat(int t){ if ((t < 2) || (t > 7200)) /* arbitrary upper limit */ return -EINVAL; heartbeat = t; return 0;}static int pcwd_get_status(int *status){ int card_status; *status=0; spin_lock(&io_lock); if (revision == PCWD_REVISION_A) /* Rev A cards return status information from * the base register, which is used for the * temperature in other cards. */ card_status = inb(current_readport); else { /* Rev C cards return card status in the base * address + 1 register. And use different bits * to indicate a card initiated reset, and an * over-temperature condition. And the reboot * status can be reset. */ card_status = inb(current_readport + 1); } spin_unlock(&io_lock); if (revision == PCWD_REVISION_A) { if (card_status & WD_WDRST) *status |= WDIOF_CARDRESET; if (card_status & WD_T110) { *status |= WDIOF_OVERHEAT; if (temp_panic) { printk (KERN_INFO PFX "Temperature overheat trip!\n"); kernel_power_off(); } } } else { if (card_status & WD_REVC_WTRP) *status |= WDIOF_CARDRESET; if (card_status & WD_REVC_TTRP) { *status |= WDIOF_OVERHEAT; if (temp_panic) { printk (KERN_INFO PFX "Temperature overheat trip!\n"); kernel_power_off(); } } } return 0;}static int pcwd_clear_status(void){ if (revision == PCWD_REVISION_C) { spin_lock(&io_lock); outb_p(0x00, current_readport + 1); /* clear reset status */ spin_unlock(&io_lock); } return 0;}static int pcwd_get_temperature(int *temperature){ /* check that port 0 gives temperature info and no command results */ if (command_mode) return -1; *temperature = 0; if (!supports_temp) return -ENODEV; /* * Convert celsius to fahrenheit, since this was * the decided 'standard' for this return value. */ spin_lock(&io_lock); *temperature = ((inb(current_readport)) * 9 / 5) + 32; spin_unlock(&io_lock); return 0;}/* * /dev/watchdog handling */static int pcwd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ int rv; int status; int temperature; int new_heartbeat; int __user *argp = (int __user *)arg; static struct watchdog_info ident = { .options = WDIOF_OVERHEAT | WDIOF_CARDRESET | WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, .firmware_version = 1, .identity = "PCWD", }; switch(cmd) { default: return -ENOIOCTLCMD; case WDIOC_GETSUPPORT: if(copy_to_user(argp, &ident, sizeof(ident))) return -EFAULT; return 0; case WDIOC_GETSTATUS: pcwd_get_status(&status); return put_user(status, argp); case WDIOC_GETBOOTSTATUS: return put_user(initial_status, argp); case WDIOC_GETTEMP: if (pcwd_get_temperature(&temperature)) return -EFAULT; return put_user(temperature, argp); case WDIOC_SETOPTIONS: if (revision == PCWD_REVISION_C) { if(copy_from_user(&rv, argp, sizeof(int))) return -EFAULT; if (rv & WDIOS_DISABLECARD) { return pcwd_stop(); } if (rv & WDIOS_ENABLECARD) { return pcwd_start(); } if (rv & WDIOS_TEMPPANIC) { temp_panic = 1; } } return -EINVAL; case WDIOC_KEEPALIVE: pcwd_keepalive(); return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -