📄 pcwd.c
字号:
case WDIOC_SETTIMEOUT: if (get_user(new_heartbeat, argp)) return -EFAULT; if (pcwd_set_heartbeat(new_heartbeat)) return -EINVAL; pcwd_keepalive(); /* Fall */ case WDIOC_GETTIMEOUT: return put_user(heartbeat, argp); } return 0;}static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len, loff_t *ppos){ if (len) { if (!nowayout) { size_t i; /* In case it was set long ago */ expect_close = 0; for (i = 0; i != len; i++) { char c; if (get_user(c, buf + i)) return -EFAULT; if (c == 'V') expect_close = 42; } } pcwd_keepalive(); } return len;}static int pcwd_open(struct inode *inode, struct file *file){ if (!atomic_dec_and_test(&open_allowed) ) { atomic_inc( &open_allowed ); return -EBUSY; } if (nowayout) __module_get(THIS_MODULE); /* Activate */ pcwd_start(); pcwd_keepalive(); return nonseekable_open(inode, file);}static int pcwd_close(struct inode *inode, struct file *file){ if (expect_close == 42) { pcwd_stop(); } else { printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); pcwd_keepalive(); } expect_close = 0; atomic_inc( &open_allowed ); return 0;}/* * /dev/temperature handling */static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ppos){ int temperature; if (pcwd_get_temperature(&temperature)) return -EFAULT; if (copy_to_user(buf, &temperature, 1)) return -EFAULT; return 1;}static int pcwd_temp_open(struct inode *inode, struct file *file){ if (!supports_temp) return -ENODEV; return nonseekable_open(inode, file);}static int pcwd_temp_close(struct inode *inode, struct file *file){ return 0;}/* * Notify system */static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused){ if (code==SYS_DOWN || code==SYS_HALT) { /* Turn the WDT off */ pcwd_stop(); } return NOTIFY_DONE;}/* * Kernel Interfaces */static struct file_operations pcwd_fops = { .owner = THIS_MODULE, .llseek = no_llseek, .write = pcwd_write, .ioctl = pcwd_ioctl, .open = pcwd_open, .release = pcwd_close,};static struct miscdevice pcwd_miscdev = { .minor = WATCHDOG_MINOR, .name = "watchdog", .fops = &pcwd_fops,};static struct file_operations pcwd_temp_fops = { .owner = THIS_MODULE, .llseek = no_llseek, .read = pcwd_temp_read, .open = pcwd_temp_open, .release = pcwd_temp_close,};static struct miscdevice temp_miscdev = { .minor = TEMP_MINOR, .name = "temperature", .fops = &pcwd_temp_fops,};static struct notifier_block pcwd_notifier = { .notifier_call = pcwd_notify_sys,};/* * Init & exit routines */static inline void get_support(void){ if (inb(current_readport) != 0xF0) supports_temp = 1;}static inline int get_revision(void){ int r = PCWD_REVISION_C; spin_lock(&io_lock); /* REV A cards use only 2 io ports; test * presumes a floating bus reads as 0xff. */ if ((inb(current_readport + 2) == 0xFF) || (inb(current_readport + 3) == 0xFF)) r=PCWD_REVISION_A; spin_unlock(&io_lock); return r;}static inline char *get_firmware(void){ int one, ten, hund, minor; char *ret; ret = kmalloc(6, GFP_KERNEL); if(ret == NULL) return NULL; if (set_command_mode()) { one = send_isa_command(CMD_ISA_VERSION_INTEGER); ten = send_isa_command(CMD_ISA_VERSION_TENTH); hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH); minor = send_isa_command(CMD_ISA_VERSION_MINOR); sprintf(ret, "%c.%c%c%c", one, ten, hund, minor); } else sprintf(ret, "ERROR"); unset_command_mode(); return(ret);}static inline int get_option_switches(void){ int rv=0; if (set_command_mode()) { /* Get switch settings */ rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS); } unset_command_mode(); return(rv);}static int __devinit pcwatchdog_init(int base_addr){ int ret; char *firmware; int option_switches; cards_found++; if (cards_found == 1) printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER); if (cards_found > 1) { printk(KERN_ERR PFX "This driver only supports 1 device\n"); return -ENODEV; } if (base_addr == 0x0000) { printk(KERN_ERR PFX "No I/O-Address for card detected\n"); return -ENODEV; } current_readport = base_addr; /* Check card's revision */ revision = get_revision(); if (!request_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) { printk(KERN_ERR PFX "I/O address 0x%04x already in use\n", current_readport); current_readport = 0x0000; return -EIO; } /* Initial variables */ supports_temp = 0; temp_panic = 0; initial_status = 0x0000; /* get the boot_status */ pcwd_get_status(&initial_status); /* clear the "card caused reboot" flag */ pcwd_clear_status(); init_timer(&timer); timer.function = pcwd_timer_ping; timer.data = 0; /* Disable the board */ pcwd_stop(); /* Check whether or not the card supports the temperature device */ get_support(); /* Get some extra info from the hardware (in command/debug/diag mode) */ if (revision == PCWD_REVISION_A) printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", current_readport); else if (revision == PCWD_REVISION_C) { firmware = get_firmware(); printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n", current_readport, firmware); kfree(firmware); option_switches = get_option_switches(); printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n", option_switches, ((option_switches & 0x10) ? "ON" : "OFF"), ((option_switches & 0x08) ? "ON" : "OFF")); /* Reprogram internal heartbeat to 2 seconds */ if (set_command_mode()) { send_isa_command(CMD_ISA_DELAY_TIME_2SECS); unset_command_mode(); } } else { /* Should NEVER happen, unless get_revision() fails. */ printk(KERN_INFO PFX "Unable to get revision\n"); release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4); current_readport = 0x0000; return -1; } if (supports_temp) printk(KERN_INFO PFX "Temperature Option Detected\n"); if (initial_status & WDIOF_CARDRESET) printk(KERN_INFO PFX "Previous reboot was caused by the card\n"); if (initial_status & WDIOF_OVERHEAT) { printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n"); printk(KERN_EMERG PFX "CPU Overheat\n"); } if (initial_status == 0) printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n"); /* Check that the heartbeat value is within it's range ; if not reset to the default */ if (pcwd_set_heartbeat(heartbeat)) { pcwd_set_heartbeat(WATCHDOG_HEARTBEAT); printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n", WATCHDOG_HEARTBEAT); } ret = register_reboot_notifier(&pcwd_notifier); if (ret) { printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret); release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4); current_readport = 0x0000; return ret; } if (supports_temp) { ret = misc_register(&temp_miscdev); if (ret) { printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", TEMP_MINOR, ret); unregister_reboot_notifier(&pcwd_notifier); release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4); current_readport = 0x0000; return ret; } } ret = misc_register(&pcwd_miscdev); if (ret) { printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", WATCHDOG_MINOR, ret); if (supports_temp) misc_deregister(&temp_miscdev); unregister_reboot_notifier(&pcwd_notifier); release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4); current_readport = 0x0000; return ret; } printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", heartbeat, nowayout); return 0;}static void __devexit pcwatchdog_exit(void){ /* Disable the board */ if (!nowayout) pcwd_stop(); /* Deregister */ misc_deregister(&pcwd_miscdev); if (supports_temp) misc_deregister(&temp_miscdev); unregister_reboot_notifier(&pcwd_notifier); release_region(current_readport, (revision == PCWD_REVISION_A) ? 2 : 4); current_readport = 0x0000;}/* * The ISA cards have a heartbeat bit in one of the registers, which * register is card dependent. The heartbeat bit is monitored, and if * found, is considered proof that a Berkshire card has been found. * The initial rate is once per second at board start up, then twice * per second for normal operation. */static int __init pcwd_checkcard(int base_addr){ int port0, last_port0; /* Reg 0, in case it's REV A */ int port1, last_port1; /* Register 1 for REV C cards */ int i; int retval; if (!request_region (base_addr, 4, "PCWD")) { printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr); return 0; } retval = 0; port0 = inb_p(base_addr); /* For REV A boards */ port1 = inb_p(base_addr + 1); /* For REV C boards */ if (port0 != 0xff || port1 != 0xff) { /* Not an 'ff' from a floating bus, so must be a card! */ for (i = 0; i < 4; ++i) { msleep(500); last_port0 = port0; last_port1 = port1; port0 = inb_p(base_addr); port1 = inb_p(base_addr + 1); /* Has either hearbeat bit changed? */ if ((port0 ^ last_port0) & WD_HRTBT || (port1 ^ last_port1) & WD_REVC_HRBT) { retval = 1; break; } } } release_region (base_addr, 4); return retval;}/* * These are the auto-probe addresses available. * * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350. * Revision A has an address range of 2 addresses, while Revision C has 4. */static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };static int __init pcwd_init_module(void){ int i, found = 0; spin_lock_init(&io_lock); for (i = 0; pcwd_ioports[i] != 0; i++) { if (pcwd_checkcard(pcwd_ioports[i])) { if (!(pcwatchdog_init(pcwd_ioports[i]))) found++; } } if (!found) { printk (KERN_INFO PFX "No card detected, or port not available\n"); return -ENODEV; } return 0;}static void __exit pcwd_cleanup_module(void){ if (current_readport) pcwatchdog_exit(); return;}module_init(pcwd_init_module);module_exit(pcwd_cleanup_module);MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");MODULE_LICENSE("GPL");MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);MODULE_ALIAS_MISCDEV(TEMP_MINOR);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -