pcwd.c
来自「linux 内核源代码」· C语言 代码 · 共 1,014 行 · 第 1/2 页
C
1,014 行
if (temp_panic) { printk(KERN_INFO PFX "Temperature overheat trip!\n"); kernel_power_off(); /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */ } } } return 0;}static int pcwd_clear_status(void){ int control_status; if (pcwd_private.revision == PCWD_REVISION_C) { spin_lock(&pcwd_private.io_lock); if (debug >= VERBOSE) printk(KERN_INFO PFX "clearing watchdog trip status\n"); control_status = inb_p(pcwd_private.io_addr + 1); if (debug >= DEBUG) { printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status); printk(KERN_DEBUG PFX "sending: 0x%02x\n", (control_status & WD_REVC_R2DS)); } /* clear reset status & Keep Relay 2 disable state as it is */ outb_p((control_status & WD_REVC_R2DS), pcwd_private.io_addr + 1); spin_unlock(&pcwd_private.io_lock); } return 0;}static int pcwd_get_temperature(int *temperature){ /* check that port 0 gives temperature info and no command results */ if (pcwd_private.command_mode) return -1; *temperature = 0; if (!pcwd_private.supports_temp) return -ENODEV; /* * Convert celsius to fahrenheit, since this was * the decided 'standard' for this return value. */ spin_lock(&pcwd_private.io_lock); *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32; spin_unlock(&pcwd_private.io_lock); if (debug >= DEBUG) { printk(KERN_DEBUG PFX "temperature is: %d F\n", *temperature); } 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 -ENOTTY; 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(pcwd_private.boot_status, argp); case WDIOC_GETTEMP: if (pcwd_get_temperature(&temperature)) return -EFAULT; return put_user(temperature, argp); case WDIOC_SETOPTIONS: if (pcwd_private.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; 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) ) { if (debug >= VERBOSE) printk(KERN_ERR PFX "Attempt to open already opened device.\n"); 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 (!pcwd_private.supports_temp) return -ENODEV; return nonseekable_open(inode, file);}static int pcwd_temp_close(struct inode *inode, struct file *file){ return 0;}/* * Kernel Interfaces */static const 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 const 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,};/* * Init & exit routines */static inline int get_revision(void){ int r = PCWD_REVISION_C; spin_lock(&pcwd_private.io_lock); /* REV A cards use only 2 io ports; test * presumes a floating bus reads as 0xff. */ if ((inb(pcwd_private.io_addr + 2) == 0xFF) || (inb(pcwd_private.io_addr + 3) == 0xFF)) r=PCWD_REVISION_A; spin_unlock(&pcwd_private.io_lock); return r;}/* * 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 __devinit pcwd_isa_match(struct device *dev, unsigned int id){ int base_addr=pcwd_ioports[id]; 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 (debug >= DEBUG) printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n", id); 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;}static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id){ int ret; if (debug >= DEBUG) printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n", id); 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 (pcwd_ioports[id] == 0x0000) { printk(KERN_ERR PFX "No I/O-Address for card detected\n"); return -ENODEV; } pcwd_private.io_addr = pcwd_ioports[id]; spin_lock_init(&pcwd_private.io_lock); /* Check card's revision */ pcwd_private.revision = get_revision(); if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) { printk(KERN_ERR PFX "I/O address 0x%04x already in use\n", pcwd_private.io_addr); ret=-EIO; goto error_request_region; } /* Initial variables */ pcwd_private.supports_temp = 0; temp_panic = 0; pcwd_private.boot_status = 0x0000; /* get the boot_status */ pcwd_get_status(&pcwd_private.boot_status); /* clear the "card caused reboot" flag */ pcwd_clear_status(); setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0); /* Disable the board */ pcwd_stop(); /* Check whether or not the card supports the temperature device */ pcwd_check_temperature_support(); /* Show info about the card itself */ pcwd_show_card_info(); /* If heartbeat = 0 then we use the heartbeat from the dip-switches */ if (heartbeat == 0) heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)]; /* 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); } if (pcwd_private.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); goto error_misc_register_temp; } } ret = misc_register(&pcwd_miscdev); if (ret) { printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", WATCHDOG_MINOR, ret); goto error_misc_register_watchdog; } printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", heartbeat, nowayout); return 0;error_misc_register_watchdog: if (pcwd_private.supports_temp) misc_deregister(&temp_miscdev);error_misc_register_temp: release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);error_request_region: pcwd_private.io_addr = 0x0000; cards_found--; return ret;}static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id){ if (debug >= DEBUG) printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n", id); if (!pcwd_private.io_addr) return 1; /* Disable the board */ if (!nowayout) pcwd_stop(); /* Deregister */ misc_deregister(&pcwd_miscdev); if (pcwd_private.supports_temp) misc_deregister(&temp_miscdev); release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); pcwd_private.io_addr = 0x0000; cards_found--; return 0;}static void pcwd_isa_shutdown(struct device *dev, unsigned int id){ if (debug >= DEBUG) printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n", id); pcwd_stop();}static struct isa_driver pcwd_isa_driver = { .match = pcwd_isa_match, .probe = pcwd_isa_probe, .remove = __devexit_p(pcwd_isa_remove), .shutdown = pcwd_isa_shutdown, .driver = { .owner = THIS_MODULE, .name = WATCHDOG_NAME, },};static int __init pcwd_init_module(void){ return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);}static void __exit pcwd_cleanup_module(void){ isa_unregister_driver(&pcwd_isa_driver); printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");}module_init(pcwd_init_module);module_exit(pcwd_cleanup_module);MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");MODULE_VERSION(WATCHDOG_VERSION);MODULE_LICENSE("GPL");MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);MODULE_ALIAS_MISCDEV(TEMP_MINOR);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?