2019-05-19 20:08:20 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* 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 <philb@gnu.org>
|
|
|
|
* 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)
|
2018-11-26 03:47:32 +08:00
|
|
|
*
|
|
|
|
* lp=reset (reset the printer during
|
2005-04-17 06:20:36 +08:00
|
|
|
* 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/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/major.h>
|
2017-02-03 02:15:33 +08:00
|
|
|
#include <linux/sched/signal.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/console.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/wait.h>
|
2005-09-10 15:26:57 +08:00
|
|
|
#include <linux/jiffies.h>
|
2010-06-02 20:28:52 +08:00
|
|
|
#include <linux/mutex.h>
|
2009-11-14 08:33:13 +08:00
|
|
|
#include <linux/compat.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include <linux/parport.h>
|
|
|
|
#undef LP_STATS
|
|
|
|
#include <linux/lp.h>
|
|
|
|
|
|
|
|
#include <asm/irq.h>
|
2016-12-25 03:46:01 +08:00
|
|
|
#include <linux/uaccess.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* if you have more than 8 printers, remember to increase LP_NO */
|
|
|
|
#define LP_NO 8
|
|
|
|
|
2010-06-02 20:28:52 +08:00
|
|
|
static DEFINE_MUTEX(lp_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct lp_struct lp_table[LP_NO];
|
2018-12-07 22:27:30 +08:00
|
|
|
static int port_num[LP_NO];
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static unsigned int lp_count = 0;
|
2005-03-24 01:53:09 +08:00
|
|
|
static struct class *lp_class;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_LP_CONSOLE
|
2007-10-17 14:30:29 +08:00
|
|
|
static struct parport *console_registered;
|
2005-04-17 06:20:36 +08:00
|
|
|
#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)) {
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_claim_or_block(this_lp->dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)) {
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_release(this_lp->dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int lp_preempt(void *handle)
|
|
|
|
{
|
|
|
|
struct lp_struct *this_lp = (struct lp_struct *)handle;
|
|
|
|
set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
|
2018-11-26 03:47:34 +08:00
|
|
|
return 1;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-26 03:47:32 +08:00
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Try to negotiate to a new mode; if unsuccessful negotiate to
|
|
|
|
* compatibility mode. Return the mode we ended up in.
|
|
|
|
*/
|
2018-11-26 03:47:36 +08:00
|
|
|
static int lp_negotiate(struct parport *port, int mode)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-11-26 03:47:37 +08:00
|
|
|
if (parport_negotiate(port, mode) != 0) {
|
2005-04-17 06:20:36 +08:00
|
|
|
mode = IEEE1284_MODE_COMPAT;
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(port, mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:34 +08:00
|
|
|
return mode;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lp_reset(int minor)
|
|
|
|
{
|
|
|
|
int retval;
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
w_ctr(minor, LP_PSELECP);
|
2018-11-26 03:47:37 +08:00
|
|
|
udelay(LP_DELAY);
|
2005-04-17 06:20:36 +08:00
|
|
|
w_ctr(minor, LP_PSELECP | LP_PINITP);
|
|
|
|
retval = r_str(minor);
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static void lp_error(int minor)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
DEFINE_WAIT(wait);
|
|
|
|
int polling;
|
|
|
|
|
|
|
|
if (LP_F(minor) & LP_ABORT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
|
2018-11-26 03:47:33 +08:00
|
|
|
if (polling)
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
|
|
|
|
schedule_timeout(LP_TIMEOUT_POLLED);
|
|
|
|
finish_wait(&lp_table[minor].waitq, &wait);
|
2018-11-26 03:47:33 +08:00
|
|
|
if (polling)
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2018-11-26 03:47:33 +08:00
|
|
|
else
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_yield_blocking(lp_table[minor].dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-11-26 03:47:35 +08:00
|
|
|
errors are reported. */
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
lp_table[minor].last_error = last;
|
|
|
|
|
|
|
|
if (last != 0)
|
|
|
|
lp_error(minor);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lp_wait_ready(int minor, int nonblock)
|
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/* If we're not in compatibility mode, we're ready now! */
|
|
|
|
if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
|
2018-11-26 03:47:34 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2018-11-26 03:47:37 +08:00
|
|
|
error = lp_check_status(minor);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
|
|
|
|
break;
|
2018-11-26 03:47:37 +08:00
|
|
|
if (signal_pending(current)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
error = -EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:36 +08:00
|
|
|
static ssize_t lp_write(struct file *file, const char __user *buf,
|
2018-11-26 03:47:35 +08:00
|
|
|
size_t count, loff_t *ppos)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-01-24 06:07:38 +08:00
|
|
|
unsigned int minor = iminor(file_inode(file));
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
|
|
|
int nonblock = ((file->f_flags & O_NONBLOCK) ||
|
|
|
|
(LP_F(minor) & LP_ABORT));
|
|
|
|
|
|
|
|
#ifdef LP_STATS
|
2005-09-10 15:26:57 +08:00
|
|
|
if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
|
2005-04-17 06:20:36 +08:00
|
|
|
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;
|
|
|
|
|
2008-02-06 17:36:25 +08:00
|
|
|
if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINTR;
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (copy_from_user(kbuf, buf, copy_size)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
retv = -EFAULT;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:35 +08:00
|
|
|
/* Claim Parport or sleep until it becomes available
|
|
|
|
*/
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Go to the proper mode. */
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_table[minor].current_mode = lp_negotiate(port,
|
|
|
|
lp_table[minor].best_mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_set_timeout(lp_table[minor].dev,
|
|
|
|
(nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
|
|
|
|
: lp_table[minor].timeout));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if ((retv = lp_wait_ready(minor, nonblock)) == 0)
|
2005-04-17 06:20:36 +08:00
|
|
|
do {
|
|
|
|
/* Write the data. */
|
2018-11-26 03:47:37 +08:00
|
|
|
written = parport_write(port, kbuf, copy_size);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (written > 0) {
|
|
|
|
copy_size -= written;
|
|
|
|
count -= written;
|
|
|
|
buf += written;
|
|
|
|
retv += written;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (signal_pending(current)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (retv == 0)
|
|
|
|
retv = -EINTR;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_size > 0) {
|
|
|
|
/* incomplete write -> check error ! */
|
|
|
|
int error;
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(lp_table[minor].dev->port,
|
|
|
|
IEEE1284_MODE_COMPAT);
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
error = lp_wait_ready(minor, nonblock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (retv == 0)
|
|
|
|
retv = error;
|
|
|
|
break;
|
|
|
|
} else if (nonblock) {
|
|
|
|
if (retv == 0)
|
|
|
|
retv = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_yield_blocking(lp_table[minor].dev);
|
2018-11-26 03:47:32 +08:00
|
|
|
lp_table[minor].current_mode
|
2018-11-26 03:47:37 +08:00
|
|
|
= lp_negotiate(port,
|
|
|
|
lp_table[minor].best_mode);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
} else if (need_resched())
|
2018-11-26 03:47:37 +08:00
|
|
|
schedule();
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-11-26 03:47:32 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
} while (count > 0);
|
|
|
|
|
2018-11-26 03:47:32 +08:00
|
|
|
if (test_and_clear_bit(LP_PREEMPT_REQUEST,
|
2005-04-17 06:20:36 +08:00
|
|
|
&lp_table[minor].bits)) {
|
|
|
|
printk(KERN_INFO "lp%d releasing parport\n", minor);
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(lp_table[minor].dev->port,
|
|
|
|
IEEE1284_MODE_COMPAT);
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
out_unlock:
|
2008-02-06 17:36:25 +08:00
|
|
|
mutex_unlock(&lp_table[minor].port_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:35 +08:00
|
|
|
return retv;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PARPORT_1284
|
|
|
|
|
|
|
|
/* Status readback conforming to ieee1284 */
|
2018-11-26 03:47:36 +08:00
|
|
|
static ssize_t lp_read(struct file *file, char __user *buf,
|
2005-04-17 06:20:36 +08:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
DEFINE_WAIT(wait);
|
2013-01-24 06:07:38 +08:00
|
|
|
unsigned int minor=iminor(file_inode(file));
|
2005-04-17 06:20:36 +08:00
|
|
|
struct parport *port = lp_table[minor].dev->port;
|
|
|
|
ssize_t retval = 0;
|
|
|
|
char *kbuf = lp_table[minor].lp_buffer;
|
|
|
|
int nonblock = ((file->f_flags & O_NONBLOCK) ||
|
|
|
|
(LP_F(minor) & LP_ABORT));
|
|
|
|
|
|
|
|
if (count > LP_BUFFER_SIZE)
|
|
|
|
count = LP_BUFFER_SIZE;
|
|
|
|
|
2008-02-06 17:36:25 +08:00
|
|
|
if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINTR;
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_set_timeout(lp_table[minor].dev,
|
|
|
|
(nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
|
|
|
|
: lp_table[minor].timeout));
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
|
|
|
|
if (parport_negotiate(lp_table[minor].dev->port,
|
|
|
|
IEEE1284_MODE_NIBBLE)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (retval == 0) {
|
2018-11-26 03:47:37 +08:00
|
|
|
retval = parport_read(port, kbuf, count);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (retval > 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (nonblock) {
|
|
|
|
retval = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for data. */
|
|
|
|
|
|
|
|
if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(lp_table[minor].dev->port,
|
|
|
|
IEEE1284_MODE_COMPAT);
|
|
|
|
lp_error(minor);
|
|
|
|
if (parport_negotiate(lp_table[minor].dev->port,
|
|
|
|
IEEE1284_MODE_NIBBLE)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
|
|
|
|
schedule_timeout(LP_TIMEOUT_POLLED);
|
|
|
|
finish_wait(&lp_table[minor].waitq, &wait);
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (signal_pending(current)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
cond_resched();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
|
2005-04-17 06:20:36 +08:00
|
|
|
out:
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (retval > 0 && copy_to_user(buf, kbuf, retval))
|
2005-04-17 06:20:36 +08:00
|
|
|
retval = -EFAULT;
|
|
|
|
|
2008-02-06 17:36:25 +08:00
|
|
|
mutex_unlock(&lp_table[minor].port_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* IEEE 1284 support */
|
|
|
|
|
2018-11-26 03:47:36 +08:00
|
|
|
static int lp_open(struct inode *inode, struct file *file)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned int minor = iminor(inode);
|
2008-05-16 01:29:38 +08:00
|
|
|
int ret = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_lock(&lp_mutex);
|
2008-05-16 01:29:38 +08:00
|
|
|
if (minor >= LP_NO) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((LP_F(minor) & LP_EXIST) == 0) {
|
|
|
|
ret = -ENXIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
/* 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;
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
status = r_str(minor);
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (status & LP_POUTPA) {
|
|
|
|
printk(KERN_INFO "lp%d out of paper\n", minor);
|
|
|
|
LP_F(minor) &= ~LP_BUSY;
|
2008-05-16 01:29:38 +08:00
|
|
|
ret = -ENOSPC;
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else if (!(status & LP_PSELECD)) {
|
|
|
|
printk(KERN_INFO "lp%d off-line\n", minor);
|
|
|
|
LP_F(minor) &= ~LP_BUSY;
|
2008-05-16 01:29:38 +08:00
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else if (!(status & LP_PERRORP)) {
|
|
|
|
printk(KERN_ERR "lp%d printer error\n", minor);
|
|
|
|
LP_F(minor) &= ~LP_BUSY;
|
2008-05-16 01:29:38 +08:00
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
2006-12-13 16:35:56 +08:00
|
|
|
lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!lp_table[minor].lp_buffer) {
|
|
|
|
LP_F(minor) &= ~LP_BUSY;
|
2008-05-16 01:29:38 +08:00
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
/* Determine if the peripheral supports ECP mode */
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2021-03-09 15:20:59 +08:00
|
|
|
if ((lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
|
2018-11-26 03:47:37 +08:00
|
|
|
!parport_negotiate(lp_table[minor].dev->port,
|
2018-11-26 03:47:35 +08:00
|
|
|
IEEE1284_MODE_ECP)) {
|
2018-11-26 03:47:37 +08:00
|
|
|
printk(KERN_INFO "lp%d: ECP mode\n", minor);
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_table[minor].best_mode = IEEE1284_MODE_ECP;
|
|
|
|
} else {
|
|
|
|
lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
|
|
|
|
}
|
|
|
|
/* Leave peripheral in compatibility mode */
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
|
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
|
2008-05-16 01:29:38 +08:00
|
|
|
out:
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&lp_mutex);
|
2008-05-16 01:29:38 +08:00
|
|
|
return ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:36 +08:00
|
|
|
static int lp_release(struct inode *inode, struct file *file)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned int minor = iminor(inode);
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
|
|
|
parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(lp_table[minor].lp_buffer);
|
|
|
|
lp_table[minor].lp_buffer = NULL;
|
|
|
|
LP_F(minor) &= ~LP_BUSY;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:33:13 +08:00
|
|
|
static int lp_do_ioctl(unsigned int minor, unsigned int cmd,
|
|
|
|
unsigned long arg, void __user *argp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
#ifdef LP_DEBUG
|
|
|
|
printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
|
|
|
|
#endif
|
|
|
|
if (minor >= LP_NO)
|
|
|
|
return -ENODEV;
|
|
|
|
if ((LP_F(minor) & LP_EXIST) == 0)
|
|
|
|
return -ENODEV;
|
2021-03-09 15:20:59 +08:00
|
|
|
switch (cmd) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case LPTIME:
|
2013-12-18 15:45:12 +08:00
|
|
|
if (arg > UINT_MAX / HZ)
|
|
|
|
return -EINVAL;
|
2005-04-17 06:20:36 +08:00
|
|
|
LP_TIME(minor) = arg * HZ/100;
|
|
|
|
break;
|
|
|
|
case LPCHAR:
|
|
|
|
LP_CHAR(minor) = arg;
|
|
|
|
break;
|
|
|
|
case LPABORT:
|
|
|
|
if (arg)
|
|
|
|
LP_F(minor) |= LP_ABORT;
|
|
|
|
else
|
|
|
|
LP_F(minor) &= ~LP_ABORT;
|
|
|
|
break;
|
|
|
|
case LPABORTOPEN:
|
|
|
|
if (arg)
|
|
|
|
LP_F(minor) |= LP_ABORTOPEN;
|
|
|
|
else
|
|
|
|
LP_F(minor) &= ~LP_ABORTOPEN;
|
|
|
|
break;
|
|
|
|
case LPCAREFUL:
|
|
|
|
if (arg)
|
|
|
|
LP_F(minor) |= LP_CAREFUL;
|
|
|
|
else
|
|
|
|
LP_F(minor) &= ~LP_CAREFUL;
|
|
|
|
break;
|
|
|
|
case LPWAIT:
|
|
|
|
LP_WAIT(minor) = arg;
|
|
|
|
break;
|
2018-11-26 03:47:32 +08:00
|
|
|
case LPSETIRQ:
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
case LPGETIRQ:
|
|
|
|
if (copy_to_user(argp, &LP_IRQ(minor),
|
|
|
|
sizeof(int)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
case LPGETSTATUS:
|
2013-05-07 22:18:09 +08:00
|
|
|
if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
|
|
|
|
return -EINTR;
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_claim_parport_or_block(&lp_table[minor]);
|
2005-04-17 06:20:36 +08:00
|
|
|
status = r_str(minor);
|
2018-11-26 03:47:37 +08:00
|
|
|
lp_release_parport(&lp_table[minor]);
|
2013-05-07 22:18:09 +08:00
|
|
|
mutex_unlock(&lp_table[minor].port_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (copy_to_user(argp, &status, sizeof(int)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
case LPRESET:
|
|
|
|
lp_reset(minor);
|
|
|
|
break;
|
|
|
|
#ifdef LP_STATS
|
|
|
|
case LPGETSTATS:
|
|
|
|
if (copy_to_user(argp, &LP_STAT(minor),
|
|
|
|
sizeof(struct lp_stats)))
|
|
|
|
return -EFAULT;
|
|
|
|
if (capable(CAP_SYS_ADMIN))
|
|
|
|
memset(&LP_STAT(minor), 0,
|
|
|
|
sizeof(struct lp_stats));
|
|
|
|
break;
|
|
|
|
#endif
|
2018-11-26 03:47:35 +08:00
|
|
|
case LPGETFLAGS:
|
|
|
|
status = LP_F(minor);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (copy_to_user(argp, &status, sizeof(int)))
|
|
|
|
return -EFAULT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
retval = -EINVAL;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
static int lp_set_timeout(unsigned int minor, s64 tv_sec, long tv_usec)
|
2009-11-14 08:33:13 +08:00
|
|
|
{
|
|
|
|
long to_jiffies;
|
|
|
|
|
|
|
|
/* Convert to jiffies, place in lp_table */
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
if (tv_sec < 0 || tv_usec < 0)
|
2009-11-14 08:33:13 +08:00
|
|
|
return -EINVAL;
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* we used to not check, so let's not make this fatal,
|
|
|
|
* but deal with user space passing a 32-bit tv_nsec in
|
|
|
|
* a 64-bit field, capping the timeout to 1 second
|
|
|
|
* worth of microseconds, and capping the total at
|
|
|
|
* MAX_JIFFY_OFFSET.
|
|
|
|
*/
|
|
|
|
if (tv_usec > 999999)
|
|
|
|
tv_usec = 999999;
|
|
|
|
|
|
|
|
if (tv_sec >= MAX_SEC_IN_JIFFIES - 1) {
|
|
|
|
to_jiffies = MAX_JIFFY_OFFSET;
|
|
|
|
} else {
|
|
|
|
to_jiffies = DIV_ROUND_UP(tv_usec, 1000000/HZ);
|
|
|
|
to_jiffies += tv_sec * (long) HZ;
|
2009-11-14 08:33:13 +08:00
|
|
|
}
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
|
2009-11-14 08:33:13 +08:00
|
|
|
if (to_jiffies <= 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
lp_table[minor].timeout = to_jiffies;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
static int lp_set_timeout32(unsigned int minor, void __user *arg)
|
|
|
|
{
|
|
|
|
s32 karg[2];
|
|
|
|
|
|
|
|
if (copy_from_user(karg, arg, sizeof(karg)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return lp_set_timeout(minor, karg[0], karg[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lp_set_timeout64(unsigned int minor, void __user *arg)
|
|
|
|
{
|
|
|
|
s64 karg[2];
|
|
|
|
|
|
|
|
if (copy_from_user(karg, arg, sizeof(karg)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2019-11-09 04:34:29 +08:00
|
|
|
/* sparc64 suseconds_t is 32-bit only */
|
|
|
|
if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
|
|
|
|
karg[1] >>= 32;
|
|
|
|
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
return lp_set_timeout(minor, karg[0], karg[1]);
|
|
|
|
}
|
|
|
|
|
2009-11-14 08:33:13 +08:00
|
|
|
static long lp_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
unsigned int minor;
|
|
|
|
int ret;
|
|
|
|
|
2013-01-24 06:07:38 +08:00
|
|
|
minor = iminor(file_inode(file));
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_lock(&lp_mutex);
|
2009-11-14 08:33:13 +08:00
|
|
|
switch (cmd) {
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
case LPSETTIMEOUT_OLD:
|
|
|
|
if (BITS_PER_LONG == 32) {
|
|
|
|
ret = lp_set_timeout32(minor, (void __user *)arg);
|
2009-11-14 08:33:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-08-24 06:36:59 +08:00
|
|
|
fallthrough; /* for 64-bit */
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
case LPSETTIMEOUT_NEW:
|
|
|
|
ret = lp_set_timeout64(minor, (void __user *)arg);
|
2009-11-14 08:33:13 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
|
|
|
|
break;
|
|
|
|
}
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&lp_mutex);
|
2009-11-14 08:33:13 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
static long lp_compat_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
unsigned int minor;
|
|
|
|
int ret;
|
|
|
|
|
2013-01-24 06:07:38 +08:00
|
|
|
minor = iminor(file_inode(file));
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_lock(&lp_mutex);
|
2009-11-14 08:33:13 +08:00
|
|
|
switch (cmd) {
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
case LPSETTIMEOUT_OLD:
|
|
|
|
if (!COMPAT_USE_64BIT_TIME) {
|
|
|
|
ret = lp_set_timeout32(minor, (void __user *)arg);
|
2009-11-14 08:33:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2020-08-24 06:36:59 +08:00
|
|
|
fallthrough; /* for x32 mode */
|
lp: support 64-bit time_t user space
Once we get a glibc with 64-bit time_t, the LPSETTIMEOUT ioctl stops
working, since the command number and data structure no longer match.
To work around that, this introduces a new command number LPSETTIMEOUT_NEW
that is used whenever the modified user space evaluates the LPSETTIMEOUT
macro.
The trick we use is a bit convoluted but necessary: we cannot check for
any macros set by the C library in linux/lp.h, because this particular
header can be included before including sys/time.h. However, we can assume
that by the time that LPSETTIMEOUT is seen in the code, the definition
for 'timeval' and 'time_t' has been seen as well, so we can use the
sizeof() operator to determine whether we should use the old or the
new definition. We use the old one not only for traditional 32-bit user
space with 32-bit time_t, but also for all 64-bit architectures and x32,
which always use a 64-bit time_t, the new definition will be used only for
32-bit user space with 64-bit time_t, which also requires a newer kernel.
The compat_ioctl() handler now implements both commands, but has to
use a special case for existing x32 binaries. The native ioctl handler
now implements both command numbers on both 32-bit and 64-bit, though
the latter version use the same interpretation for both.
This is based on an earlier patch from Bamvor.
Cc: Bamvor Jian Zhang <bamv2005@gmail.com>
Link: http://www.spinics.net/lists/y2038/msg01162.html
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-27 19:29:50 +08:00
|
|
|
case LPSETTIMEOUT_NEW:
|
|
|
|
ret = lp_set_timeout64(minor, (void __user *)arg);
|
2009-11-14 08:33:13 +08:00
|
|
|
break;
|
|
|
|
#ifdef LP_STATS
|
|
|
|
case LPGETSTATS:
|
|
|
|
/* FIXME: add an implementation if you set LP_STATS */
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
|
|
|
|
break;
|
|
|
|
}
|
2010-06-02 20:28:52 +08:00
|
|
|
mutex_unlock(&lp_mutex);
|
2009-11-14 08:33:13 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-07-03 15:24:21 +08:00
|
|
|
static const struct file_operations lp_fops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.write = lp_write,
|
2009-11-14 08:33:13 +08:00
|
|
|
.unlocked_ioctl = lp_ioctl,
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = lp_compat_ioctl,
|
|
|
|
#endif
|
2005-04-17 06:20:36 +08:00
|
|
|
.open = lp_open,
|
|
|
|
.release = lp_release,
|
|
|
|
#ifdef CONFIG_PARPORT_1284
|
|
|
|
.read = lp_read,
|
|
|
|
#endif
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-16 00:52:59 +08:00
|
|
|
.llseek = noop_llseek,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* --- support for console on the line printer ----------------- */
|
|
|
|
|
|
|
|
#ifdef CONFIG_LP_CONSOLE
|
|
|
|
|
|
|
|
#define CONSOLE_LP 0
|
|
|
|
|
|
|
|
/* If the printer is out of paper, we can either lose the messages or
|
|
|
|
* stall until the printer is happy again. Define CONSOLE_LP_STRICT
|
|
|
|
* non-zero to get the latter behaviour. */
|
|
|
|
#define CONSOLE_LP_STRICT 1
|
|
|
|
|
|
|
|
/* The console must be locked when we get here. */
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static void lp_console_write(struct console *co, const char *s,
|
|
|
|
unsigned count)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct pardevice *dev = lp_table[CONSOLE_LP].dev;
|
|
|
|
struct parport *port = dev->port;
|
|
|
|
ssize_t written;
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (parport_claim(dev))
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Nothing we can do. */
|
|
|
|
return;
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_set_timeout(dev, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Go to compatibility mode. */
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_negotiate(port, IEEE1284_MODE_COMPAT);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* Write the data, converting LF->CRLF as we go. */
|
|
|
|
ssize_t canwrite = count;
|
2018-11-26 03:47:37 +08:00
|
|
|
char *lf = memchr(s, '\n', count);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (lf)
|
|
|
|
canwrite = lf - s;
|
|
|
|
|
|
|
|
if (canwrite > 0) {
|
2018-11-26 03:47:37 +08:00
|
|
|
written = parport_write(port, s, canwrite);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (written <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s += written;
|
|
|
|
count -= written;
|
|
|
|
canwrite -= written;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lf && canwrite <= 0) {
|
|
|
|
const char *crlf = "\r\n";
|
|
|
|
int i = 2;
|
|
|
|
|
|
|
|
/* Dodge the original '\n', and put '\r\n' instead. */
|
|
|
|
s++;
|
|
|
|
count--;
|
|
|
|
do {
|
2018-11-26 03:47:37 +08:00
|
|
|
written = parport_write(port, crlf, i);
|
2020-08-25 12:56:04 +08:00
|
|
|
if (written > 0) {
|
|
|
|
i -= written;
|
|
|
|
crlf += written;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
} while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
|
|
|
|
}
|
|
|
|
} while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_release(dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct console lpcons = {
|
|
|
|
.name = "lp",
|
|
|
|
.write = lp_console_write,
|
|
|
|
.flags = CON_PRINTBUFFER,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* console on line printer */
|
|
|
|
|
|
|
|
/* --- initialisation code ------------------------------------- */
|
|
|
|
|
|
|
|
static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
|
2007-10-17 14:30:29 +08:00
|
|
|
static char *parport[LP_NO];
|
2012-01-13 07:02:20 +08:00
|
|
|
static bool reset;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
module_param_array(parport, charp, NULL, 0);
|
|
|
|
module_param(reset, bool, 0);
|
|
|
|
|
|
|
|
#ifndef MODULE
|
2018-11-26 03:47:37 +08:00
|
|
|
static int __init lp_setup(char *str)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-10-17 14:30:29 +08:00
|
|
|
static int parport_ptr;
|
2005-04-17 06:20:36 +08:00
|
|
|
int x;
|
|
|
|
|
2007-10-17 14:30:29 +08:00
|
|
|
if (get_option(&str, &x)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (x == 0) {
|
|
|
|
/* disable driver on "lp=" or "lp=0" */
|
|
|
|
parport_nr[0] = LP_PARPORT_OFF;
|
|
|
|
} else {
|
|
|
|
printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (!strncmp(str, "parport", 7)) {
|
|
|
|
int n = simple_strtoul(str+7, NULL, 10);
|
|
|
|
if (parport_ptr < LP_NO)
|
|
|
|
parport_nr[parport_ptr++] = n;
|
|
|
|
else
|
|
|
|
printk(KERN_INFO "lp: too many ports, %s ignored.\n",
|
|
|
|
str);
|
|
|
|
} else if (!strcmp(str, "auto")) {
|
|
|
|
parport_nr[0] = LP_PARPORT_AUTO;
|
|
|
|
} else if (!strcmp(str, "none")) {
|
2017-05-17 01:18:55 +08:00
|
|
|
if (parport_ptr < LP_NO)
|
|
|
|
parport_nr[parport_ptr++] = LP_PARPORT_NONE;
|
|
|
|
else
|
|
|
|
printk(KERN_INFO "lp: too many ports, %s ignored.\n",
|
|
|
|
str);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else if (!strcmp(str, "reset")) {
|
2018-01-23 23:35:29 +08:00
|
|
|
reset = true;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int lp_register(int nr, struct parport *port)
|
|
|
|
{
|
2018-12-07 22:27:34 +08:00
|
|
|
struct pardev_cb ppdev_cb;
|
|
|
|
|
|
|
|
memset(&ppdev_cb, 0, sizeof(ppdev_cb));
|
|
|
|
ppdev_cb.preempt = lp_preempt;
|
|
|
|
ppdev_cb.private = &lp_table[nr];
|
|
|
|
lp_table[nr].dev = parport_register_dev_model(port, "lp",
|
|
|
|
&ppdev_cb, nr);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (lp_table[nr].dev == NULL)
|
|
|
|
return 1;
|
|
|
|
lp_table[nr].flags |= LP_EXIST;
|
|
|
|
|
|
|
|
if (reset)
|
|
|
|
lp_reset(nr);
|
|
|
|
|
2008-07-22 11:03:34 +08:00
|
|
|
device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
|
|
|
|
"lp%d", nr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-26 03:47:32 +08:00
|
|
|
printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
|
2005-04-17 06:20:36 +08:00
|
|
|
(port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
|
|
|
|
|
|
|
|
#ifdef CONFIG_LP_CONSOLE
|
|
|
|
if (!nr) {
|
|
|
|
if (port->modes & PARPORT_MODE_SAFEININT) {
|
2007-10-17 14:30:29 +08:00
|
|
|
register_console(&lpcons);
|
2005-04-17 06:20:36 +08:00
|
|
|
console_registered = port;
|
2018-11-26 03:47:37 +08:00
|
|
|
printk(KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else
|
2018-11-26 03:47:37 +08:00
|
|
|
printk(KERN_ERR "lp%d: cannot run console on %s\n",
|
|
|
|
CONSOLE_LP, port->name);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
#endif
|
2018-12-07 22:27:30 +08:00
|
|
|
port_num[nr] = port->number;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static void lp_attach(struct parport *port)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2007-10-17 14:30:29 +08:00
|
|
|
switch (parport_nr[0]) {
|
2005-04-17 06:20:36 +08:00
|
|
|
case LP_PARPORT_UNSPEC:
|
|
|
|
case LP_PARPORT_AUTO:
|
|
|
|
if (parport_nr[0] == LP_PARPORT_AUTO &&
|
|
|
|
port->probe_info[0].class != PARPORT_CLASS_PRINTER)
|
|
|
|
return;
|
|
|
|
if (lp_count == LP_NO) {
|
|
|
|
printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-07 22:27:32 +08:00
|
|
|
for (i = 0; i < LP_NO; i++)
|
|
|
|
if (port_num[i] == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!lp_register(i, port))
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_count++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
for (i = 0; i < LP_NO; i++) {
|
|
|
|
if (port->number == parport_nr[i]) {
|
|
|
|
if (!lp_register(i, port))
|
|
|
|
lp_count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static void lp_detach(struct parport *port)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-12-07 22:27:31 +08:00
|
|
|
int n;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Write this some day. */
|
|
|
|
#ifdef CONFIG_LP_CONSOLE
|
|
|
|
if (console_registered == port) {
|
2007-10-17 14:30:29 +08:00
|
|
|
unregister_console(&lpcons);
|
2005-04-17 06:20:36 +08:00
|
|
|
console_registered = NULL;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_LP_CONSOLE */
|
2018-12-07 22:27:31 +08:00
|
|
|
|
|
|
|
for (n = 0; n < LP_NO; n++) {
|
|
|
|
if (port_num[n] == port->number) {
|
|
|
|
port_num[n] = -1;
|
2018-12-07 22:27:33 +08:00
|
|
|
lp_count--;
|
2018-12-07 22:27:31 +08:00
|
|
|
device_destroy(lp_class, MKDEV(LP_MAJOR, n));
|
|
|
|
parport_unregister_device(lp_table[n].dev);
|
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct parport_driver lp_driver = {
|
|
|
|
.name = "lp",
|
2018-12-07 22:27:34 +08:00
|
|
|
.match_port = lp_attach,
|
2005-04-17 06:20:36 +08:00
|
|
|
.detach = lp_detach,
|
2018-12-07 22:27:34 +08:00
|
|
|
.devmodel = true,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static int __init lp_init(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
if (parport_nr[0] == LP_PARPORT_OFF)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < LP_NO; i++) {
|
|
|
|
lp_table[i].dev = NULL;
|
|
|
|
lp_table[i].flags = 0;
|
|
|
|
lp_table[i].chars = LP_INIT_CHAR;
|
|
|
|
lp_table[i].time = LP_INIT_TIME;
|
|
|
|
lp_table[i].wait = LP_INIT_WAIT;
|
|
|
|
lp_table[i].lp_buffer = NULL;
|
|
|
|
#ifdef LP_STATS
|
|
|
|
lp_table[i].lastcall = 0;
|
|
|
|
lp_table[i].runchars = 0;
|
2018-11-26 03:47:37 +08:00
|
|
|
memset(&lp_table[i].stats, 0, sizeof(struct lp_stats));
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
lp_table[i].last_error = 0;
|
2018-11-26 03:47:37 +08:00
|
|
|
init_waitqueue_head(&lp_table[i].waitq);
|
|
|
|
init_waitqueue_head(&lp_table[i].dataq);
|
2008-02-06 17:36:25 +08:00
|
|
|
mutex_init(&lp_table[i].port_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
lp_table[i].timeout = 10 * HZ;
|
2018-12-07 22:27:30 +08:00
|
|
|
port_num[i] = -1;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (register_chrdev(LP_MAJOR, "lp", &lp_fops)) {
|
|
|
|
printk(KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2005-03-24 01:53:09 +08:00
|
|
|
lp_class = class_create(THIS_MODULE, "printer");
|
2005-04-17 06:20:36 +08:00
|
|
|
if (IS_ERR(lp_class)) {
|
|
|
|
err = PTR_ERR(lp_class);
|
2006-09-29 16:59:47 +08:00
|
|
|
goto out_reg;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
if (parport_register_driver(&lp_driver)) {
|
|
|
|
printk(KERN_ERR "lp: unable to register with parport\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -EIO;
|
|
|
|
goto out_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lp_count) {
|
2018-11-26 03:47:37 +08:00
|
|
|
printk(KERN_INFO "lp: driver loaded but no devices found\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
#ifndef CONFIG_PARPORT_1284
|
|
|
|
if (parport_nr[0] == LP_PARPORT_AUTO)
|
2018-11-26 03:47:37 +08:00
|
|
|
printk(KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_class:
|
2005-03-24 01:53:09 +08:00
|
|
|
class_destroy(lp_class);
|
2006-09-29 16:59:47 +08:00
|
|
|
out_reg:
|
2005-04-17 06:20:36 +08:00
|
|
|
unregister_chrdev(LP_MAJOR, "lp");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static int __init lp_init_module(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
if (parport[0]) {
|
|
|
|
/* The user gave some parameters. Let's see what they were. */
|
|
|
|
if (!strncmp(parport[0], "auto", 4))
|
|
|
|
parport_nr[0] = LP_PARPORT_AUTO;
|
|
|
|
else {
|
|
|
|
int n;
|
|
|
|
for (n = 0; n < LP_NO && parport[n]; n++) {
|
|
|
|
if (!strncmp(parport[n], "none", 4))
|
|
|
|
parport_nr[n] = LP_PARPORT_NONE;
|
|
|
|
else {
|
|
|
|
char *ep;
|
|
|
|
unsigned long r = simple_strtoul(parport[n], &ep, 0);
|
2018-11-26 03:47:32 +08:00
|
|
|
if (ep != parport[n])
|
2005-04-17 06:20:36 +08:00
|
|
|
parport_nr[n] = r;
|
|
|
|
else {
|
|
|
|
printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lp_init();
|
|
|
|
}
|
|
|
|
|
2018-11-26 03:47:37 +08:00
|
|
|
static void lp_cleanup_module(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-11-26 03:47:37 +08:00
|
|
|
parport_unregister_driver(&lp_driver);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_LP_CONSOLE
|
2018-11-26 03:47:37 +08:00
|
|
|
unregister_console(&lpcons);
|
2005-04-17 06:20:36 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
unregister_chrdev(LP_MAJOR, "lp");
|
2005-03-24 01:53:09 +08:00
|
|
|
class_destroy(lp_class);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
__setup("lp=", lp_setup);
|
|
|
|
module_init(lp_init_module);
|
|
|
|
module_exit(lp_cleanup_module);
|
|
|
|
|
|
|
|
MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
|
|
|
|
MODULE_LICENSE("GPL");
|