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

📄 lp.c

📁 linux和2410结合开发 用他可以生成2410所需的zImage文件
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Generic parallel printer driver * * Copyright (C) 1992 by Jim Weigand and Linus Torvalds * Copyright (C) 1992,1993 by Michael K. Johnson * - Thanks much to Gunter Windau for pointing out to me where the error *   checking ought to be. * Copyright (C) 1993 by Nigel Gamble (added interrupt code) * Copyright (C) 1994 by Alan Cox (Modularised it) * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl * "lp=" command line parameters added by Grant Guenther, grant@torque.net * lp_read (Status readback) support added by Carsten Gross, *                                             carsten@sol.wohnheim.uni-ulm.de * Support for parport by Philip Blundell <Philip.Blundell@pobox.com> * Parport sharing hacking by Andrea Arcangeli * Fixed kernel_(to/from)_user memory copy to check for errors * 				by Riccardo Facchetti <fizban@tin.it> * 22-JAN-1998  Added support for devfs  Richard Gooch <rgooch@atnf.csiro.au> * Redesigned interrupt handling for handle printers with buggy handshake *				by Andrea Arcangeli, 11 May 1998 * Full efficient handling of printer with buggy irq handshake (now I have * understood the meaning of the strange handshake). This is done sending new * characters if the interrupt is just happened, even if the printer say to * be still BUSY. This is needed at least with Epson Stylus Color. To enable * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below... * Fixed the irq on the rising edge of the strobe case. * Obsoleted the CAREFUL flag since a printer that doesn' t work with * CAREFUL will block a bit after in lp_check_status(). *				Andrea Arcangeli, 15 Oct 1998 * Obsoleted and removed all the lowlevel stuff implemented in the last * month to use the IEEE1284 functions (that handle the _new_ compatibilty * mode fine). *//* This driver should, in theory, work with any parallel port that has an * appropriate low-level driver; all I/O is done through the parport * abstraction layer. * * If this driver is built into the kernel, you can configure it using the * kernel command-line.  For example: * *	lp=parport1,none,parport2	(bind lp0 to parport1, disable lp1 and *					 bind lp2 to parport2) * *	lp=auto				(assign lp devices to all ports that *				         have printers attached, as determined *					 by the IEEE-1284 autoprobe) *  *	lp=reset			(reset the printer during  *					 initialisation) * *	lp=off				(disable the printer driver entirely) * * If the driver is loaded as a module, similar functionality is available * using module parameters.  The equivalent of the above commands would be: * *	# insmod lp.o parport=1,none,2 * *	# insmod lp.o parport=auto * *	# insmod lp.o reset=1 *//* COMPATIBILITY WITH OLD KERNELS * * Under Linux 2.0 and previous versions, lp devices were bound to ports at * particular I/O addresses, as follows: * *	lp0		0x3bc *	lp1		0x378 *	lp2		0x278 * * The new driver, by default, binds lp devices to parport devices as it * finds them.  This means that if you only have one port, it will be bound * to lp0 regardless of its I/O address.  If you need the old behaviour, you * can force it using the parameters described above. *//* * The new interrupt handling code take care of the buggy handshake * of some HP and Epson printer: * ___ * ACK    _______________    ___________ *                       |__| * ____ * BUSY   _________              _______ *                 |____________| * * I discovered this using the printer scanner that you can find at: * *	ftp://e-mind.com/pub/linux/pscan/ * *					11 May 98, Andrea Arcangeli * * My printer scanner run on an Epson Stylus Color show that such printer * generates the irq on the _rising_ edge of the STROBE. Now lp handle * this case fine too. * *					15 Oct 1998, Andrea Arcangeli * * The so called `buggy' handshake is really the well documented * compatibility mode IEEE1284 handshake. They changed the well known * Centronics handshake acking in the middle of busy expecting to not * break drivers or legacy application, while they broken linux lp * until I fixed it reverse engineering the protocol by hand some * month ago... * *                                     14 Dec 1998, Andrea Arcangeli * * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl) */#include <linux/module.h>#include <linux/init.h>#include <linux/config.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/major.h>#include <linux/sched.h>#include <linux/smp_lock.h>#include <linux/devfs_fs_kernel.h>#include <linux/slab.h>#include <linux/fcntl.h>#include <linux/delay.h>#include <linux/poll.h>#include <linux/console.h>#include <linux/parport.h>#undef LP_STATS#include <linux/lp.h>#include <asm/irq.h>#include <asm/uaccess.h>#include <asm/system.h>/* if you have more than 8 printers, remember to increase LP_NO */#define LP_NO 8/* ROUND_UP macro from fs/select.c */#define ROUND_UP(x,y) (((x)+(y)-1)/(y))static devfs_handle_t devfs_handle = NULL;struct lp_struct lp_table[LP_NO];static unsigned int lp_count = 0;#ifdef CONFIG_LP_CONSOLEstatic struct parport *console_registered; // initially NULL#endif /* CONFIG_LP_CONSOLE */#undef LP_DEBUG/* Bits used to manage claiming the parport device */#define LP_PREEMPT_REQUEST 1#define LP_PARPORT_CLAIMED 2/* --- low-level port access ----------------------------------- */#define r_dtr(x)	(parport_read_data(lp_table[(x)].dev->port))#define r_str(x)	(parport_read_status(lp_table[(x)].dev->port))#define w_ctr(x,y)	do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)#define w_dtr(x,y)	do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)/* Claim the parport or block trying unless we've already claimed it */static void lp_claim_parport_or_block(struct lp_struct *this_lp){	if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {		parport_claim_or_block (this_lp->dev);	}}/* Claim the parport or block trying unless we've already claimed it */static void lp_release_parport(struct lp_struct *this_lp){	if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {		parport_release (this_lp->dev);	}}static int lp_preempt(void *handle){	struct lp_struct *this_lp = (struct lp_struct *)handle;	set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);	return (1);}/*  * Try to negotiate to a new mode; if unsuccessful negotiate to * compatibility mode.  Return the mode we ended up in. */static int lp_negotiate(struct parport * port, int mode){	if (parport_negotiate (port, mode) != 0) {		mode = IEEE1284_MODE_COMPAT;		parport_negotiate (port, mode);	}	return (mode);}static int lp_reset(int minor){	int retval;	lp_claim_parport_or_block (&lp_table[minor]);	w_ctr(minor, LP_PSELECP);	udelay (LP_DELAY);	w_ctr(minor, LP_PSELECP | LP_PINITP);	retval = r_str(minor);	lp_release_parport (&lp_table[minor]);	return retval;}static void lp_error (int minor){	int polling;	if (LP_F(minor) & LP_ABORT)		return;	polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;	if (polling) lp_release_parport (&lp_table[minor]);	interruptible_sleep_on_timeout (&lp_table[minor].waitq,					LP_TIMEOUT_POLLED);	if (polling) lp_claim_parport_or_block (&lp_table[minor]);	else parport_yield_blocking (lp_table[minor].dev);}static int lp_check_status(int minor){	int error = 0;	unsigned int last = lp_table[minor].last_error;	unsigned char status = r_str(minor);	if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))		/* No error. */		last = 0;	else if ((status & LP_POUTPA)) {		if (last != LP_POUTPA) {			last = LP_POUTPA;			printk(KERN_INFO "lp%d out of paper\n", minor);		}		error = -ENOSPC;	} else if (!(status & LP_PSELECD)) {		if (last != LP_PSELECD) {			last = LP_PSELECD;			printk(KERN_INFO "lp%d off-line\n", minor);		}		error = -EIO;	} else if (!(status & LP_PERRORP)) {		if (last != LP_PERRORP) {			last = LP_PERRORP;			printk(KERN_INFO "lp%d on fire\n", minor);		}		error = -EIO;	} else {		last = 0; /* Come here if LP_CAREFUL is set and no                             errors are reported. */	}	lp_table[minor].last_error = last;	if (last != 0)		lp_error(minor);	return error;}static int lp_wait_ready(int minor){	int error = 0;	/* If we're not in compatibility mode, we're ready now! */	if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {	  return (0);	}	do {		error = lp_check_status (minor);		if (error && (LP_F(minor) & LP_ABORT))			break;		if (signal_pending (current)) {			error = -EINTR;			break;		}	} while (error);	return error;}static ssize_t lp_write(struct file * file, const char * buf,		        size_t count, loff_t *ppos){	unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);	struct parport *port = lp_table[minor].dev->port;	char *kbuf = lp_table[minor].lp_buffer;	ssize_t retv = 0;	ssize_t written;	size_t copy_size = count;#ifdef LP_STATS	if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))		lp_table[minor].runchars = 0;	lp_table[minor].lastcall = jiffies;#endif	/* Need to copy the data from user-space. */	if (copy_size > LP_BUFFER_SIZE)		copy_size = LP_BUFFER_SIZE;	if (copy_from_user (kbuf, buf, copy_size))		return -EFAULT;	if (down_interruptible (&lp_table[minor].port_mutex))		return -EINTR; 	/* Claim Parport or sleep until it becomes available 	 */	lp_claim_parport_or_block (&lp_table[minor]);	/* Go to the proper mode. */	lp_table[minor].current_mode = lp_negotiate (port, 						     lp_table[minor].best_mode);	parport_set_timeout (lp_table[minor].dev,			     lp_table[minor].timeout);	if ((retv = lp_wait_ready (minor)) == 0)	do {		/* Write the data. */		written = parport_write (port, kbuf, copy_size);		if (written >= 0) {			copy_size -= written;			count -= written;			buf  += written;			retv += written;		}		if (signal_pending (current)) {			if (retv == 0)				retv = -EINTR;			break;		}		if (copy_size > 0) {			/* incomplete write -> check error ! */			int error;			parport_negotiate (lp_table[minor].dev->port, 					   IEEE1284_MODE_COMPAT);			lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;			error = lp_wait_ready (minor);			if (error) {				if (retv == 0)					retv = error;				break;			}			parport_yield_blocking (lp_table[minor].dev);			lp_table[minor].current_mode 			  = lp_negotiate (port, 					  lp_table[minor].best_mode);		} else if (current->need_resched)			schedule ();		if (count) {			copy_size = count;			if (copy_size > LP_BUFFER_SIZE)				copy_size = LP_BUFFER_SIZE;			if (copy_from_user(kbuf, buf, copy_size)) {				if (retv == 0)					retv = -EFAULT;				break;			}		}		} while (count > 0);	if (test_and_clear_bit(LP_PREEMPT_REQUEST, 			       &lp_table[minor].bits)) {		printk(KERN_INFO "lp%d releasing parport\n", minor);		parport_negotiate (lp_table[minor].dev->port, 				   IEEE1284_MODE_COMPAT);		lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;		lp_release_parport (&lp_table[minor]);	}	up (&lp_table[minor].port_mutex); 	return retv;}#ifdef CONFIG_PARPORT_1284/* Status readback conforming to ieee1284 */static ssize_t lp_read(struct file * file, char * buf,		       size_t count, loff_t *ppos){	unsigned int minor=MINOR(file->f_dentry->d_inode->i_rdev);	struct parport *port = lp_table[minor].dev->port;	ssize_t retval = 0;	char *kbuf = lp_table[minor].lp_buffer;	if (count > LP_BUFFER_SIZE)		count = LP_BUFFER_SIZE;	if (down_interruptible (&lp_table[minor].port_mutex))		return -EINTR;	lp_claim_parport_or_block (&lp_table[minor]);	retval = parport_read (port, kbuf, count);	lp_release_parport (&lp_table[minor]);	if (retval > 0 && copy_to_user (buf, kbuf, retval))		retval = -EFAULT;	up (&lp_table[minor].port_mutex);	return retval;}#endif /* IEEE 1284 support */static int lp_open(struct inode * inode, struct file * file){	unsigned int minor = MINOR(inode->i_rdev);	if (minor >= LP_NO)		return -ENXIO;	if ((LP_F(minor) & LP_EXIST) == 0)		return -ENXIO;	if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))		return -EBUSY;	/* If ABORTOPEN is set and the printer is offline or out of paper,	   we may still want to open it to perform ioctl()s.  Therefore we	   have commandeered O_NONBLOCK, even though it is being used in	   a non-standard manner.  This is strictly a Linux hack, and	   should most likely only ever be used by the tunelp application. */	if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {		int status;		lp_claim_parport_or_block (&lp_table[minor]);		status = r_str(minor);		lp_release_parport (&lp_table[minor]);		if (status & LP_POUTPA) {			printk(KERN_INFO "lp%d out of paper\n", minor);			LP_F(minor) &= ~LP_BUSY;			return -ENOSPC;		} else if (!(status & LP_PSELECD)) {			printk(KERN_INFO "lp%d off-line\n", minor);			LP_F(minor) &= ~LP_BUSY;			return -EIO;		} else if (!(status & LP_PERRORP)) {			printk(KERN_ERR "lp%d printer error\n", minor);			LP_F(minor) &= ~LP_BUSY;			return -EIO;		}	}

⌨️ 快捷键说明

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