⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcwd.c

📁 Linux内核源代码 为压缩文件 是<<Linux内核>>一书中的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * 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> */#include <linux/module.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/timer.h>#include <linux/config.h>#include <linux/kernel.h>#include <linux/wait.h>#include <linux/string.h>#include <linux/malloc.h>#include <linux/ioport.h>#include <linux/delay.h>#include <linux/miscdevice.h>#include <linux/fs.h>#include <linux/mm.h>#include <linux/watchdog.h>#include <linux/init.h>#include <linux/proc_fs.h>#include <linux/spinlock.h>#include <linux/smp_lock.h>#include <asm/uaccess.h>#include <asm/io.h>/* * 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 3. */static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };#define WD_VER                  "1.10 (06/05/99)"/* * 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#define	WD_TIMEOUT		3	/* 1 1/2 seconds for a timeout *//* * These are the defines 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 */static int current_readport, revision, temp_panic;static int is_open, initial_status, supports_temp, mode_debug;static spinlock_t io_lock;/* * PCWD_CHECKCARD * * This routine checks the "current_readport" to see if the card lies there. * If it does, it returns accordingly. */static int __init pcwd_checkcard(void){	int card_dat, prev_card_dat, found = 0, count = 0, done = 0;	/* As suggested by Alan Cox - this is a safety measure. */	if (check_region(current_readport, 4)) {		printk("pcwd: Port 0x%x unavailable.\n", current_readport);		return 0;	}	card_dat = 0x00;	prev_card_dat = 0x00;	prev_card_dat = inb(current_readport);	if (prev_card_dat == 0xFF)		return 0;	while(count < WD_TIMEOUT) {	/* Read the raw card data from the port, and strip off the	   first 4 bits */		card_dat = inb_p(current_readport);		card_dat &= 0x000F;	/* Sleep 1/2 second (or 500000 microseconds :) */		mdelay(500);		done = 0;	/* If there's a heart beat in both instances, then this means we	   found our card.  This also means that either the card was	   previously reset, or the computer was power-cycled. */		if ((card_dat & WD_HRTBT) && (prev_card_dat & WD_HRTBT) &&			(!done)) {			found = 1;			done = 1;			break;		}	/* If the card data is exactly the same as the previous card data,	   it's safe to assume that we should check again.  The manual says	   that the heart beat will change every second (or the bit will	   toggle), and this can be used to see if the card is there.  If	   the card was powered up with a cold boot, then the card will	   not start blinking until 2.5 minutes after a reboot, so this	   bit will stay at 1. */		if ((card_dat == prev_card_dat) && (!done)) {			count++;			done = 1;		}	/* If the card data is toggling any bits, this means that the heart	   beat was detected, or something else about the card is set. */		if ((card_dat != prev_card_dat) && (!done)) {			done = 1;			found = 1;			break;		}	/* Otherwise something else strange happened. */		if (!done)			count++;	}	return((found) ? 1 : 0);}void pcwd_showprevstate(void){	int card_status = 0x0000;	if (revision == PCWD_REVISION_A)		initial_status = card_status = inb(current_readport);	else {		initial_status = card_status = inb(current_readport + 1);		outb_p(0x00, current_readport + 1); /* clear reset status */	}	if (revision == PCWD_REVISION_A) {		if (card_status & WD_WDRST)			printk("pcwd: Previous reboot was caused by the card.\n");		if (card_status & WD_T110) {			printk("pcwd: Card senses a CPU Overheat.  Panicking!\n");			panic("pcwd: CPU Overheat.\n");		}		if ((!(card_status & WD_WDRST)) &&		    (!(card_status & WD_T110)))			printk("pcwd: Cold boot sense.\n");	} else {		if (card_status & 0x01)			printk("pcwd: Previous reboot was caused by the card.\n");		if (card_status & 0x04) {			printk("pcwd: Card senses a CPU Overheat.  Panicking!\n");			panic("pcwd: CPU Overheat.\n");		}		if ((!(card_status & 0x01)) &&		    (!(card_status & 0x04)))			printk("pcwd: Cold boot sense.\n");	}}static void pcwd_send_heartbeat(void){	int wdrst_stat;	wdrst_stat = inb_p(current_readport);	wdrst_stat &= 0x0F;	wdrst_stat |= WD_WDRST;	if (revision == PCWD_REVISION_A)		outb_p(wdrst_stat, current_readport + 1);	else		outb_p(wdrst_stat, current_readport);}static int pcwd_ioctl(struct inode *inode, struct file *file,		      unsigned int cmd, unsigned long arg){	int i, cdat, rv;	static struct watchdog_info ident=	{		WDIOF_OVERHEAT|WDIOF_CARDRESET,		1,		"PCWD"	};	switch(cmd) {	default:		return -ENOIOCTLCMD;	case WDIOC_GETSUPPORT:		i = copy_to_user((void*)arg, &ident, sizeof(ident));		return i ? -EFAULT : 0;	case WDIOC_GETSTATUS:		spin_lock(&io_lock);		if (revision == PCWD_REVISION_A) 			cdat = inb(current_readport);		else			cdat = inb(current_readport + 1 );		spin_unlock(&io_lock);		rv = 0;		if (revision == PCWD_REVISION_A) 		{			if (cdat & WD_WDRST)				rv |= WDIOF_CARDRESET;			if (cdat & WD_T110) 			{				rv |= WDIOF_OVERHEAT;				if (temp_panic)					panic("pcwd: Temperature overheat trip!\n");			}		}		else 		{			if (cdat & 0x01)				rv |= WDIOF_CARDRESET;			if (cdat & 0x04) 			{				rv |= WDIOF_OVERHEAT;				if (temp_panic)					panic("pcwd: Temperature overheat trip!\n");			}		}		if(put_user(rv, (int *) arg))			return -EFAULT;		return 0;	case WDIOC_GETBOOTSTATUS:		rv = 0;		if (revision == PCWD_REVISION_A) 		{			if (initial_status & WD_WDRST)				rv |= WDIOF_CARDRESET;			if (initial_status & WD_T110)				rv |= WDIOF_OVERHEAT;		}		else		{			if (initial_status & 0x01)				rv |= WDIOF_CARDRESET;			if (initial_status & 0x04)				rv |= WDIOF_OVERHEAT;		}		if(put_user(rv, (int *) arg))			return -EFAULT;		return 0;	case WDIOC_GETTEMP:		rv = 0;		if ((supports_temp) && (mode_debug == 0)) 		{			spin_lock(&io_lock);			rv = inb(current_readport);			spin_unlock(&io_lock);			if(put_user(rv, (int*) arg))				return -EFAULT;		} else if(put_user(rv, (int*) arg))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -