pcwd.c

来自「LINUX 2.6.17.4的源码」· C语言 代码 · 共 1,003 行 · 第 1/2 页

C
1,003
字号
	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 -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(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;}/* *	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 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;}static int __devinit pcwatchdog_init(int base_addr){	int ret;	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;	}	pcwd_private.io_addr = base_addr;	/* 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);		pcwd_private.io_addr = 0x0000;		return -EIO;	}	/* 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();	init_timer(&pcwd_private.timer);	pcwd_private.timer.function = pcwd_timer_ping;	pcwd_private.timer.data = 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();	/* 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(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);		pcwd_private.io_addr = 0x0000;		return ret;	}	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);			unregister_reboot_notifier(&pcwd_notifier);			release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);			pcwd_private.io_addr = 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 (pcwd_private.supports_temp)			misc_deregister(&temp_miscdev);		unregister_reboot_notifier(&pcwd_notifier);		release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);		pcwd_private.io_addr = 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 (pcwd_private.supports_temp)		misc_deregister(&temp_miscdev);	unregister_reboot_notifier(&pcwd_notifier);	release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);	pcwd_private.io_addr = 0x0000;	cards_found--;}/* *  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(&pcwd_private.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 (pcwd_private.io_addr)		pcwatchdog_exit();	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>");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 + =
减小字号Ctrl + -
显示快捷键?