Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Generic parallel printer driver
0004  *
0005  * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
0006  * Copyright (C) 1992,1993 by Michael K. Johnson
0007  * - Thanks much to Gunter Windau for pointing out to me where the error
0008  *   checking ought to be.
0009  * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
0010  * Copyright (C) 1994 by Alan Cox (Modularised it)
0011  * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
0012  * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
0013  * "lp=" command line parameters added by Grant Guenther, grant@torque.net
0014  * lp_read (Status readback) support added by Carsten Gross,
0015  *                                             carsten@sol.wohnheim.uni-ulm.de
0016  * Support for parport by Philip Blundell <philb@gnu.org>
0017  * Parport sharing hacking by Andrea Arcangeli
0018  * Fixed kernel_(to/from)_user memory copy to check for errors
0019  *              by Riccardo Facchetti <fizban@tin.it>
0020  * 22-JAN-1998  Added support for devfs  Richard Gooch <rgooch@atnf.csiro.au>
0021  * Redesigned interrupt handling for handle printers with buggy handshake
0022  *              by Andrea Arcangeli, 11 May 1998
0023  * Full efficient handling of printer with buggy irq handshake (now I have
0024  * understood the meaning of the strange handshake). This is done sending new
0025  * characters if the interrupt is just happened, even if the printer say to
0026  * be still BUSY. This is needed at least with Epson Stylus Color. To enable
0027  * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below...
0028  * Fixed the irq on the rising edge of the strobe case.
0029  * Obsoleted the CAREFUL flag since a printer that doesn' t work with
0030  * CAREFUL will block a bit after in lp_check_status().
0031  *              Andrea Arcangeli, 15 Oct 1998
0032  * Obsoleted and removed all the lowlevel stuff implemented in the last
0033  * month to use the IEEE1284 functions (that handle the _new_ compatibilty
0034  * mode fine).
0035  */
0036 
0037 /* This driver should, in theory, work with any parallel port that has an
0038  * appropriate low-level driver; all I/O is done through the parport
0039  * abstraction layer.
0040  *
0041  * If this driver is built into the kernel, you can configure it using the
0042  * kernel command-line.  For example:
0043  *
0044  *  lp=parport1,none,parport2   (bind lp0 to parport1, disable lp1 and
0045  *                   bind lp2 to parport2)
0046  *
0047  *  lp=auto             (assign lp devices to all ports that
0048  *                       have printers attached, as determined
0049  *                   by the IEEE-1284 autoprobe)
0050  *
0051  *  lp=reset            (reset the printer during
0052  *                   initialisation)
0053  *
0054  *  lp=off              (disable the printer driver entirely)
0055  *
0056  * If the driver is loaded as a module, similar functionality is available
0057  * using module parameters.  The equivalent of the above commands would be:
0058  *
0059  *  # insmod lp.o parport=1,none,2
0060  *
0061  *  # insmod lp.o parport=auto
0062  *
0063  *  # insmod lp.o reset=1
0064  */
0065 
0066 /* COMPATIBILITY WITH OLD KERNELS
0067  *
0068  * Under Linux 2.0 and previous versions, lp devices were bound to ports at
0069  * particular I/O addresses, as follows:
0070  *
0071  *  lp0     0x3bc
0072  *  lp1     0x378
0073  *  lp2     0x278
0074  *
0075  * The new driver, by default, binds lp devices to parport devices as it
0076  * finds them.  This means that if you only have one port, it will be bound
0077  * to lp0 regardless of its I/O address.  If you need the old behaviour, you
0078  * can force it using the parameters described above.
0079  */
0080 
0081 /*
0082  * The new interrupt handling code take care of the buggy handshake
0083  * of some HP and Epson printer:
0084  * ___
0085  * ACK    _______________    ___________
0086  *                       |__|
0087  * ____
0088  * BUSY   _________              _______
0089  *                 |____________|
0090  *
0091  * I discovered this using the printer scanner that you can find at:
0092  *
0093  *  ftp://e-mind.com/pub/linux/pscan/
0094  *
0095  *                  11 May 98, Andrea Arcangeli
0096  *
0097  * My printer scanner run on an Epson Stylus Color show that such printer
0098  * generates the irq on the _rising_ edge of the STROBE. Now lp handle
0099  * this case fine too.
0100  *
0101  *                  15 Oct 1998, Andrea Arcangeli
0102  *
0103  * The so called `buggy' handshake is really the well documented
0104  * compatibility mode IEEE1284 handshake. They changed the well known
0105  * Centronics handshake acking in the middle of busy expecting to not
0106  * break drivers or legacy application, while they broken linux lp
0107  * until I fixed it reverse engineering the protocol by hand some
0108  * month ago...
0109  *
0110  *                                     14 Dec 1998, Andrea Arcangeli
0111  *
0112  * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl)
0113  */
0114 
0115 #include <linux/module.h>
0116 #include <linux/init.h>
0117 
0118 #include <linux/errno.h>
0119 #include <linux/kernel.h>
0120 #include <linux/major.h>
0121 #include <linux/sched/signal.h>
0122 #include <linux/slab.h>
0123 #include <linux/fcntl.h>
0124 #include <linux/delay.h>
0125 #include <linux/poll.h>
0126 #include <linux/console.h>
0127 #include <linux/device.h>
0128 #include <linux/wait.h>
0129 #include <linux/jiffies.h>
0130 #include <linux/mutex.h>
0131 #include <linux/compat.h>
0132 
0133 #include <linux/parport.h>
0134 #undef LP_STATS
0135 #include <linux/lp.h>
0136 
0137 #include <asm/irq.h>
0138 #include <linux/uaccess.h>
0139 
0140 /* if you have more than 8 printers, remember to increase LP_NO */
0141 #define LP_NO 8
0142 
0143 static DEFINE_MUTEX(lp_mutex);
0144 static struct lp_struct lp_table[LP_NO];
0145 static int port_num[LP_NO];
0146 
0147 static unsigned int lp_count = 0;
0148 static struct class *lp_class;
0149 
0150 #ifdef CONFIG_LP_CONSOLE
0151 static struct parport *console_registered;
0152 #endif /* CONFIG_LP_CONSOLE */
0153 
0154 #undef LP_DEBUG
0155 
0156 /* Bits used to manage claiming the parport device */
0157 #define LP_PREEMPT_REQUEST 1
0158 #define LP_PARPORT_CLAIMED 2
0159 
0160 /* --- low-level port access ----------------------------------- */
0161 
0162 #define r_dtr(x)    (parport_read_data(lp_table[(x)].dev->port))
0163 #define r_str(x)    (parport_read_status(lp_table[(x)].dev->port))
0164 #define w_ctr(x,y)  do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
0165 #define w_dtr(x,y)  do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
0166 
0167 /* Claim the parport or block trying unless we've already claimed it */
0168 static void lp_claim_parport_or_block(struct lp_struct *this_lp)
0169 {
0170     if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
0171         parport_claim_or_block(this_lp->dev);
0172     }
0173 }
0174 
0175 /* Claim the parport or block trying unless we've already claimed it */
0176 static void lp_release_parport(struct lp_struct *this_lp)
0177 {
0178     if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
0179         parport_release(this_lp->dev);
0180     }
0181 }
0182 
0183 
0184 
0185 static int lp_preempt(void *handle)
0186 {
0187     struct lp_struct *this_lp = (struct lp_struct *)handle;
0188     set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
0189     return 1;
0190 }
0191 
0192 
0193 /*
0194  * Try to negotiate to a new mode; if unsuccessful negotiate to
0195  * compatibility mode.  Return the mode we ended up in.
0196  */
0197 static int lp_negotiate(struct parport *port, int mode)
0198 {
0199     if (parport_negotiate(port, mode) != 0) {
0200         mode = IEEE1284_MODE_COMPAT;
0201         parport_negotiate(port, mode);
0202     }
0203 
0204     return mode;
0205 }
0206 
0207 static int lp_reset(int minor)
0208 {
0209     int retval;
0210     lp_claim_parport_or_block(&lp_table[minor]);
0211     w_ctr(minor, LP_PSELECP);
0212     udelay(LP_DELAY);
0213     w_ctr(minor, LP_PSELECP | LP_PINITP);
0214     retval = r_str(minor);
0215     lp_release_parport(&lp_table[minor]);
0216     return retval;
0217 }
0218 
0219 static void lp_error(int minor)
0220 {
0221     DEFINE_WAIT(wait);
0222     int polling;
0223 
0224     if (LP_F(minor) & LP_ABORT)
0225         return;
0226 
0227     polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
0228     if (polling)
0229         lp_release_parport(&lp_table[minor]);
0230     prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
0231     schedule_timeout(LP_TIMEOUT_POLLED);
0232     finish_wait(&lp_table[minor].waitq, &wait);
0233     if (polling)
0234         lp_claim_parport_or_block(&lp_table[minor]);
0235     else
0236         parport_yield_blocking(lp_table[minor].dev);
0237 }
0238 
0239 static int lp_check_status(int minor)
0240 {
0241     int error = 0;
0242     unsigned int last = lp_table[minor].last_error;
0243     unsigned char status = r_str(minor);
0244     if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
0245         /* No error. */
0246         last = 0;
0247     else if ((status & LP_POUTPA)) {
0248         if (last != LP_POUTPA) {
0249             last = LP_POUTPA;
0250             printk(KERN_INFO "lp%d out of paper\n", minor);
0251         }
0252         error = -ENOSPC;
0253     } else if (!(status & LP_PSELECD)) {
0254         if (last != LP_PSELECD) {
0255             last = LP_PSELECD;
0256             printk(KERN_INFO "lp%d off-line\n", minor);
0257         }
0258         error = -EIO;
0259     } else if (!(status & LP_PERRORP)) {
0260         if (last != LP_PERRORP) {
0261             last = LP_PERRORP;
0262             printk(KERN_INFO "lp%d on fire\n", minor);
0263         }
0264         error = -EIO;
0265     } else {
0266         last = 0; /* Come here if LP_CAREFUL is set and no
0267                  errors are reported. */
0268     }
0269 
0270     lp_table[minor].last_error = last;
0271 
0272     if (last != 0)
0273         lp_error(minor);
0274 
0275     return error;
0276 }
0277 
0278 static int lp_wait_ready(int minor, int nonblock)
0279 {
0280     int error = 0;
0281 
0282     /* If we're not in compatibility mode, we're ready now! */
0283     if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
0284         return 0;
0285     }
0286 
0287     do {
0288         error = lp_check_status(minor);
0289         if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
0290             break;
0291         if (signal_pending(current)) {
0292             error = -EINTR;
0293             break;
0294         }
0295     } while (error);
0296     return error;
0297 }
0298 
0299 static ssize_t lp_write(struct file *file, const char __user *buf,
0300             size_t count, loff_t *ppos)
0301 {
0302     unsigned int minor = iminor(file_inode(file));
0303     struct parport *port = lp_table[minor].dev->port;
0304     char *kbuf = lp_table[minor].lp_buffer;
0305     ssize_t retv = 0;
0306     ssize_t written;
0307     size_t copy_size = count;
0308     int nonblock = ((file->f_flags & O_NONBLOCK) ||
0309             (LP_F(minor) & LP_ABORT));
0310 
0311 #ifdef LP_STATS
0312     if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
0313         lp_table[minor].runchars = 0;
0314 
0315     lp_table[minor].lastcall = jiffies;
0316 #endif
0317 
0318     /* Need to copy the data from user-space. */
0319     if (copy_size > LP_BUFFER_SIZE)
0320         copy_size = LP_BUFFER_SIZE;
0321 
0322     if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
0323         return -EINTR;
0324 
0325     if (copy_from_user(kbuf, buf, copy_size)) {
0326         retv = -EFAULT;
0327         goto out_unlock;
0328     }
0329 
0330     /* Claim Parport or sleep until it becomes available
0331      */
0332     lp_claim_parport_or_block(&lp_table[minor]);
0333     /* Go to the proper mode. */
0334     lp_table[minor].current_mode = lp_negotiate(port,
0335                             lp_table[minor].best_mode);
0336 
0337     parport_set_timeout(lp_table[minor].dev,
0338                 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
0339                  : lp_table[minor].timeout));
0340 
0341     if ((retv = lp_wait_ready(minor, nonblock)) == 0)
0342     do {
0343         /* Write the data. */
0344         written = parport_write(port, kbuf, copy_size);
0345         if (written > 0) {
0346             copy_size -= written;
0347             count -= written;
0348             buf  += written;
0349             retv += written;
0350         }
0351 
0352         if (signal_pending(current)) {
0353             if (retv == 0)
0354                 retv = -EINTR;
0355 
0356             break;
0357         }
0358 
0359         if (copy_size > 0) {
0360             /* incomplete write -> check error ! */
0361             int error;
0362 
0363             parport_negotiate(lp_table[minor].dev->port,
0364                       IEEE1284_MODE_COMPAT);
0365             lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
0366 
0367             error = lp_wait_ready(minor, nonblock);
0368 
0369             if (error) {
0370                 if (retv == 0)
0371                     retv = error;
0372                 break;
0373             } else if (nonblock) {
0374                 if (retv == 0)
0375                     retv = -EAGAIN;
0376                 break;
0377             }
0378 
0379             parport_yield_blocking(lp_table[minor].dev);
0380             lp_table[minor].current_mode
0381               = lp_negotiate(port,
0382                      lp_table[minor].best_mode);
0383 
0384         } else if (need_resched())
0385             schedule();
0386 
0387         if (count) {
0388             copy_size = count;
0389             if (copy_size > LP_BUFFER_SIZE)
0390                 copy_size = LP_BUFFER_SIZE;
0391 
0392             if (copy_from_user(kbuf, buf, copy_size)) {
0393                 if (retv == 0)
0394                     retv = -EFAULT;
0395                 break;
0396             }
0397         }
0398     } while (count > 0);
0399 
0400     if (test_and_clear_bit(LP_PREEMPT_REQUEST,
0401                    &lp_table[minor].bits)) {
0402         printk(KERN_INFO "lp%d releasing parport\n", minor);
0403         parport_negotiate(lp_table[minor].dev->port,
0404                   IEEE1284_MODE_COMPAT);
0405         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
0406         lp_release_parport(&lp_table[minor]);
0407     }
0408 out_unlock:
0409     mutex_unlock(&lp_table[minor].port_mutex);
0410 
0411     return retv;
0412 }
0413 
0414 #ifdef CONFIG_PARPORT_1284
0415 
0416 /* Status readback conforming to ieee1284 */
0417 static ssize_t lp_read(struct file *file, char __user *buf,
0418                size_t count, loff_t *ppos)
0419 {
0420     DEFINE_WAIT(wait);
0421     unsigned int minor=iminor(file_inode(file));
0422     struct parport *port = lp_table[minor].dev->port;
0423     ssize_t retval = 0;
0424     char *kbuf = lp_table[minor].lp_buffer;
0425     int nonblock = ((file->f_flags & O_NONBLOCK) ||
0426             (LP_F(minor) & LP_ABORT));
0427 
0428     if (count > LP_BUFFER_SIZE)
0429         count = LP_BUFFER_SIZE;
0430 
0431     if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
0432         return -EINTR;
0433 
0434     lp_claim_parport_or_block(&lp_table[minor]);
0435 
0436     parport_set_timeout(lp_table[minor].dev,
0437                 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
0438                  : lp_table[minor].timeout));
0439 
0440     parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
0441     if (parport_negotiate(lp_table[minor].dev->port,
0442                   IEEE1284_MODE_NIBBLE)) {
0443         retval = -EIO;
0444         goto out;
0445     }
0446 
0447     while (retval == 0) {
0448         retval = parport_read(port, kbuf, count);
0449 
0450         if (retval > 0)
0451             break;
0452 
0453         if (nonblock) {
0454             retval = -EAGAIN;
0455             break;
0456         }
0457 
0458         /* Wait for data. */
0459 
0460         if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
0461             parport_negotiate(lp_table[minor].dev->port,
0462                       IEEE1284_MODE_COMPAT);
0463             lp_error(minor);
0464             if (parport_negotiate(lp_table[minor].dev->port,
0465                           IEEE1284_MODE_NIBBLE)) {
0466                 retval = -EIO;
0467                 goto out;
0468             }
0469         } else {
0470             prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
0471             schedule_timeout(LP_TIMEOUT_POLLED);
0472             finish_wait(&lp_table[minor].waitq, &wait);
0473         }
0474 
0475         if (signal_pending(current)) {
0476             retval = -ERESTARTSYS;
0477             break;
0478         }
0479 
0480         cond_resched();
0481     }
0482     parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
0483  out:
0484     lp_release_parport(&lp_table[minor]);
0485 
0486     if (retval > 0 && copy_to_user(buf, kbuf, retval))
0487         retval = -EFAULT;
0488 
0489     mutex_unlock(&lp_table[minor].port_mutex);
0490 
0491     return retval;
0492 }
0493 
0494 #endif /* IEEE 1284 support */
0495 
0496 static int lp_open(struct inode *inode, struct file *file)
0497 {
0498     unsigned int minor = iminor(inode);
0499     int ret = 0;
0500 
0501     mutex_lock(&lp_mutex);
0502     if (minor >= LP_NO) {
0503         ret = -ENXIO;
0504         goto out;
0505     }
0506     if ((LP_F(minor) & LP_EXIST) == 0) {
0507         ret = -ENXIO;
0508         goto out;
0509     }
0510     if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
0511         ret = -EBUSY;
0512         goto out;
0513     }
0514     /* If ABORTOPEN is set and the printer is offline or out of paper,
0515        we may still want to open it to perform ioctl()s.  Therefore we
0516        have commandeered O_NONBLOCK, even though it is being used in
0517        a non-standard manner.  This is strictly a Linux hack, and
0518        should most likely only ever be used by the tunelp application. */
0519     if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
0520         int status;
0521         lp_claim_parport_or_block(&lp_table[minor]);
0522         status = r_str(minor);
0523         lp_release_parport(&lp_table[minor]);
0524         if (status & LP_POUTPA) {
0525             printk(KERN_INFO "lp%d out of paper\n", minor);
0526             LP_F(minor) &= ~LP_BUSY;
0527             ret = -ENOSPC;
0528             goto out;
0529         } else if (!(status & LP_PSELECD)) {
0530             printk(KERN_INFO "lp%d off-line\n", minor);
0531             LP_F(minor) &= ~LP_BUSY;
0532             ret = -EIO;
0533             goto out;
0534         } else if (!(status & LP_PERRORP)) {
0535             printk(KERN_ERR "lp%d printer error\n", minor);
0536             LP_F(minor) &= ~LP_BUSY;
0537             ret = -EIO;
0538             goto out;
0539         }
0540     }
0541     lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
0542     if (!lp_table[minor].lp_buffer) {
0543         LP_F(minor) &= ~LP_BUSY;
0544         ret = -ENOMEM;
0545         goto out;
0546     }
0547     /* Determine if the peripheral supports ECP mode */
0548     lp_claim_parport_or_block(&lp_table[minor]);
0549     if ((lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
0550          !parport_negotiate(lp_table[minor].dev->port,
0551                  IEEE1284_MODE_ECP)) {
0552         printk(KERN_INFO "lp%d: ECP mode\n", minor);
0553         lp_table[minor].best_mode = IEEE1284_MODE_ECP;
0554     } else {
0555         lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
0556     }
0557     /* Leave peripheral in compatibility mode */
0558     parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
0559     lp_release_parport(&lp_table[minor]);
0560     lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
0561 out:
0562     mutex_unlock(&lp_mutex);
0563     return ret;
0564 }
0565 
0566 static int lp_release(struct inode *inode, struct file *file)
0567 {
0568     unsigned int minor = iminor(inode);
0569 
0570     lp_claim_parport_or_block(&lp_table[minor]);
0571     parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
0572     lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
0573     lp_release_parport(&lp_table[minor]);
0574     kfree(lp_table[minor].lp_buffer);
0575     lp_table[minor].lp_buffer = NULL;
0576     LP_F(minor) &= ~LP_BUSY;
0577     return 0;
0578 }
0579 
0580 static int lp_do_ioctl(unsigned int minor, unsigned int cmd,
0581     unsigned long arg, void __user *argp)
0582 {
0583     int status;
0584     int retval = 0;
0585 
0586 #ifdef LP_DEBUG
0587     printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
0588 #endif
0589     if (minor >= LP_NO)
0590         return -ENODEV;
0591     if ((LP_F(minor) & LP_EXIST) == 0)
0592         return -ENODEV;
0593     switch (cmd) {
0594         case LPTIME:
0595             if (arg > UINT_MAX / HZ)
0596                 return -EINVAL;
0597             LP_TIME(minor) = arg * HZ/100;
0598             break;
0599         case LPCHAR:
0600             LP_CHAR(minor) = arg;
0601             break;
0602         case LPABORT:
0603             if (arg)
0604                 LP_F(minor) |= LP_ABORT;
0605             else
0606                 LP_F(minor) &= ~LP_ABORT;
0607             break;
0608         case LPABORTOPEN:
0609             if (arg)
0610                 LP_F(minor) |= LP_ABORTOPEN;
0611             else
0612                 LP_F(minor) &= ~LP_ABORTOPEN;
0613             break;
0614         case LPCAREFUL:
0615             if (arg)
0616                 LP_F(minor) |= LP_CAREFUL;
0617             else
0618                 LP_F(minor) &= ~LP_CAREFUL;
0619             break;
0620         case LPWAIT:
0621             LP_WAIT(minor) = arg;
0622             break;
0623         case LPSETIRQ:
0624             return -EINVAL;
0625         case LPGETIRQ:
0626             if (copy_to_user(argp, &LP_IRQ(minor),
0627                     sizeof(int)))
0628                 return -EFAULT;
0629             break;
0630         case LPGETSTATUS:
0631             if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
0632                 return -EINTR;
0633             lp_claim_parport_or_block(&lp_table[minor]);
0634             status = r_str(minor);
0635             lp_release_parport(&lp_table[minor]);
0636             mutex_unlock(&lp_table[minor].port_mutex);
0637 
0638             if (copy_to_user(argp, &status, sizeof(int)))
0639                 return -EFAULT;
0640             break;
0641         case LPRESET:
0642             lp_reset(minor);
0643             break;
0644 #ifdef LP_STATS
0645         case LPGETSTATS:
0646             if (copy_to_user(argp, &LP_STAT(minor),
0647                     sizeof(struct lp_stats)))
0648                 return -EFAULT;
0649             if (capable(CAP_SYS_ADMIN))
0650                 memset(&LP_STAT(minor), 0,
0651                         sizeof(struct lp_stats));
0652             break;
0653 #endif
0654         case LPGETFLAGS:
0655             status = LP_F(minor);
0656             if (copy_to_user(argp, &status, sizeof(int)))
0657                 return -EFAULT;
0658             break;
0659 
0660         default:
0661             retval = -EINVAL;
0662     }
0663     return retval;
0664 }
0665 
0666 static int lp_set_timeout(unsigned int minor, s64 tv_sec, long tv_usec)
0667 {
0668     long to_jiffies;
0669 
0670     /* Convert to jiffies, place in lp_table */
0671     if (tv_sec < 0 || tv_usec < 0)
0672         return -EINVAL;
0673 
0674     /*
0675      * we used to not check, so let's not make this fatal,
0676      * but deal with user space passing a 32-bit tv_nsec in
0677      * a 64-bit field, capping the timeout to 1 second
0678      * worth of microseconds, and capping the total at
0679      * MAX_JIFFY_OFFSET.
0680      */
0681     if (tv_usec > 999999)
0682         tv_usec = 999999;
0683 
0684     if (tv_sec >= MAX_SEC_IN_JIFFIES - 1) {
0685         to_jiffies = MAX_JIFFY_OFFSET;
0686     } else {
0687         to_jiffies = DIV_ROUND_UP(tv_usec, 1000000/HZ);
0688         to_jiffies += tv_sec * (long) HZ;
0689     }
0690 
0691     if (to_jiffies <= 0) {
0692         return -EINVAL;
0693     }
0694     lp_table[minor].timeout = to_jiffies;
0695     return 0;
0696 }
0697 
0698 static int lp_set_timeout32(unsigned int minor, void __user *arg)
0699 {
0700     s32 karg[2];
0701 
0702     if (copy_from_user(karg, arg, sizeof(karg)))
0703         return -EFAULT;
0704 
0705     return lp_set_timeout(minor, karg[0], karg[1]);
0706 }
0707 
0708 static int lp_set_timeout64(unsigned int minor, void __user *arg)
0709 {
0710     s64 karg[2];
0711 
0712     if (copy_from_user(karg, arg, sizeof(karg)))
0713         return -EFAULT;
0714 
0715     /* sparc64 suseconds_t is 32-bit only */
0716     if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
0717         karg[1] >>= 32;
0718 
0719     return lp_set_timeout(minor, karg[0], karg[1]);
0720 }
0721 
0722 static long lp_ioctl(struct file *file, unsigned int cmd,
0723             unsigned long arg)
0724 {
0725     unsigned int minor;
0726     int ret;
0727 
0728     minor = iminor(file_inode(file));
0729     mutex_lock(&lp_mutex);
0730     switch (cmd) {
0731     case LPSETTIMEOUT_OLD:
0732         if (BITS_PER_LONG == 32) {
0733             ret = lp_set_timeout32(minor, (void __user *)arg);
0734             break;
0735         }
0736         fallthrough;    /* for 64-bit */
0737     case LPSETTIMEOUT_NEW:
0738         ret = lp_set_timeout64(minor, (void __user *)arg);
0739         break;
0740     default:
0741         ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
0742         break;
0743     }
0744     mutex_unlock(&lp_mutex);
0745 
0746     return ret;
0747 }
0748 
0749 #ifdef CONFIG_COMPAT
0750 static long lp_compat_ioctl(struct file *file, unsigned int cmd,
0751             unsigned long arg)
0752 {
0753     unsigned int minor;
0754     int ret;
0755 
0756     minor = iminor(file_inode(file));
0757     mutex_lock(&lp_mutex);
0758     switch (cmd) {
0759     case LPSETTIMEOUT_OLD:
0760         if (!COMPAT_USE_64BIT_TIME) {
0761             ret = lp_set_timeout32(minor, (void __user *)arg);
0762             break;
0763         }
0764         fallthrough;    /* for x32 mode */
0765     case LPSETTIMEOUT_NEW:
0766         ret = lp_set_timeout64(minor, (void __user *)arg);
0767         break;
0768 #ifdef LP_STATS
0769     case LPGETSTATS:
0770         /* FIXME: add an implementation if you set LP_STATS */
0771         ret = -EINVAL;
0772         break;
0773 #endif
0774     default:
0775         ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
0776         break;
0777     }
0778     mutex_unlock(&lp_mutex);
0779 
0780     return ret;
0781 }
0782 #endif
0783 
0784 static const struct file_operations lp_fops = {
0785     .owner      = THIS_MODULE,
0786     .write      = lp_write,
0787     .unlocked_ioctl = lp_ioctl,
0788 #ifdef CONFIG_COMPAT
0789     .compat_ioctl   = lp_compat_ioctl,
0790 #endif
0791     .open       = lp_open,
0792     .release    = lp_release,
0793 #ifdef CONFIG_PARPORT_1284
0794     .read       = lp_read,
0795 #endif
0796     .llseek     = noop_llseek,
0797 };
0798 
0799 /* --- support for console on the line printer ----------------- */
0800 
0801 #ifdef CONFIG_LP_CONSOLE
0802 
0803 #define CONSOLE_LP 0
0804 
0805 /* If the printer is out of paper, we can either lose the messages or
0806  * stall until the printer is happy again.  Define CONSOLE_LP_STRICT
0807  * non-zero to get the latter behaviour. */
0808 #define CONSOLE_LP_STRICT 1
0809 
0810 /* The console must be locked when we get here. */
0811 
0812 static void lp_console_write(struct console *co, const char *s,
0813                  unsigned count)
0814 {
0815     struct pardevice *dev = lp_table[CONSOLE_LP].dev;
0816     struct parport *port = dev->port;
0817     ssize_t written;
0818 
0819     if (parport_claim(dev))
0820         /* Nothing we can do. */
0821         return;
0822 
0823     parport_set_timeout(dev, 0);
0824 
0825     /* Go to compatibility mode. */
0826     parport_negotiate(port, IEEE1284_MODE_COMPAT);
0827 
0828     do {
0829         /* Write the data, converting LF->CRLF as we go. */
0830         ssize_t canwrite = count;
0831         char *lf = memchr(s, '\n', count);
0832         if (lf)
0833             canwrite = lf - s;
0834 
0835         if (canwrite > 0) {
0836             written = parport_write(port, s, canwrite);
0837 
0838             if (written <= 0)
0839                 continue;
0840 
0841             s += written;
0842             count -= written;
0843             canwrite -= written;
0844         }
0845 
0846         if (lf && canwrite <= 0) {
0847             const char *crlf = "\r\n";
0848             int i = 2;
0849 
0850             /* Dodge the original '\n', and put '\r\n' instead. */
0851             s++;
0852             count--;
0853             do {
0854                 written = parport_write(port, crlf, i);
0855                 if (written > 0) {
0856                     i -= written;
0857                     crlf += written;
0858                 }
0859             } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
0860         }
0861     } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
0862 
0863     parport_release(dev);
0864 }
0865 
0866 static struct console lpcons = {
0867     .name       = "lp",
0868     .write      = lp_console_write,
0869     .flags      = CON_PRINTBUFFER,
0870 };
0871 
0872 #endif /* console on line printer */
0873 
0874 /* --- initialisation code ------------------------------------- */
0875 
0876 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
0877 static char *parport[LP_NO];
0878 static bool reset;
0879 
0880 module_param_array(parport, charp, NULL, 0);
0881 module_param(reset, bool, 0);
0882 
0883 #ifndef MODULE
0884 static int __init lp_setup(char *str)
0885 {
0886     static int parport_ptr;
0887     int x;
0888 
0889     if (get_option(&str, &x)) {
0890         if (x == 0) {
0891             /* disable driver on "lp=" or "lp=0" */
0892             parport_nr[0] = LP_PARPORT_OFF;
0893         } else {
0894             printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
0895             return 0;
0896         }
0897     } else if (!strncmp(str, "parport", 7)) {
0898         int n = simple_strtoul(str+7, NULL, 10);
0899         if (parport_ptr < LP_NO)
0900             parport_nr[parport_ptr++] = n;
0901         else
0902             printk(KERN_INFO "lp: too many ports, %s ignored.\n",
0903                    str);
0904     } else if (!strcmp(str, "auto")) {
0905         parport_nr[0] = LP_PARPORT_AUTO;
0906     } else if (!strcmp(str, "none")) {
0907         if (parport_ptr < LP_NO)
0908             parport_nr[parport_ptr++] = LP_PARPORT_NONE;
0909         else
0910             printk(KERN_INFO "lp: too many ports, %s ignored.\n",
0911                    str);
0912     } else if (!strcmp(str, "reset")) {
0913         reset = true;
0914     }
0915     return 1;
0916 }
0917 #endif
0918 
0919 static int lp_register(int nr, struct parport *port)
0920 {
0921     struct pardev_cb ppdev_cb;
0922 
0923     memset(&ppdev_cb, 0, sizeof(ppdev_cb));
0924     ppdev_cb.preempt = lp_preempt;
0925     ppdev_cb.private = &lp_table[nr];
0926     lp_table[nr].dev = parport_register_dev_model(port, "lp",
0927                               &ppdev_cb, nr);
0928     if (lp_table[nr].dev == NULL)
0929         return 1;
0930     lp_table[nr].flags |= LP_EXIST;
0931 
0932     if (reset)
0933         lp_reset(nr);
0934 
0935     device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
0936               "lp%d", nr);
0937 
0938     printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
0939            (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
0940 
0941 #ifdef CONFIG_LP_CONSOLE
0942     if (!nr) {
0943         if (port->modes & PARPORT_MODE_SAFEININT) {
0944             register_console(&lpcons);
0945             console_registered = port;
0946             printk(KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
0947         } else
0948             printk(KERN_ERR "lp%d: cannot run console on %s\n",
0949                    CONSOLE_LP, port->name);
0950     }
0951 #endif
0952     port_num[nr] = port->number;
0953 
0954     return 0;
0955 }
0956 
0957 static void lp_attach(struct parport *port)
0958 {
0959     unsigned int i;
0960 
0961     switch (parport_nr[0]) {
0962     case LP_PARPORT_UNSPEC:
0963     case LP_PARPORT_AUTO:
0964         if (parport_nr[0] == LP_PARPORT_AUTO &&
0965             port->probe_info[0].class != PARPORT_CLASS_PRINTER)
0966             return;
0967         if (lp_count == LP_NO) {
0968             printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
0969             return;
0970         }
0971         for (i = 0; i < LP_NO; i++)
0972             if (port_num[i] == -1)
0973                 break;
0974 
0975         if (!lp_register(i, port))
0976             lp_count++;
0977         break;
0978 
0979     default:
0980         for (i = 0; i < LP_NO; i++) {
0981             if (port->number == parport_nr[i]) {
0982                 if (!lp_register(i, port))
0983                     lp_count++;
0984                 break;
0985             }
0986         }
0987         break;
0988     }
0989 }
0990 
0991 static void lp_detach(struct parport *port)
0992 {
0993     int n;
0994 
0995     /* Write this some day. */
0996 #ifdef CONFIG_LP_CONSOLE
0997     if (console_registered == port) {
0998         unregister_console(&lpcons);
0999         console_registered = NULL;
1000     }
1001 #endif /* CONFIG_LP_CONSOLE */
1002 
1003     for (n = 0; n < LP_NO; n++) {
1004         if (port_num[n] == port->number) {
1005             port_num[n] = -1;
1006             lp_count--;
1007             device_destroy(lp_class, MKDEV(LP_MAJOR, n));
1008             parport_unregister_device(lp_table[n].dev);
1009         }
1010     }
1011 }
1012 
1013 static struct parport_driver lp_driver = {
1014     .name = "lp",
1015     .match_port = lp_attach,
1016     .detach = lp_detach,
1017     .devmodel = true,
1018 };
1019 
1020 static int __init lp_init(void)
1021 {
1022     int i, err;
1023 
1024     if (parport_nr[0] == LP_PARPORT_OFF)
1025         return 0;
1026 
1027     for (i = 0; i < LP_NO; i++) {
1028         lp_table[i].dev = NULL;
1029         lp_table[i].flags = 0;
1030         lp_table[i].chars = LP_INIT_CHAR;
1031         lp_table[i].time = LP_INIT_TIME;
1032         lp_table[i].wait = LP_INIT_WAIT;
1033         lp_table[i].lp_buffer = NULL;
1034 #ifdef LP_STATS
1035         lp_table[i].lastcall = 0;
1036         lp_table[i].runchars = 0;
1037         memset(&lp_table[i].stats, 0, sizeof(struct lp_stats));
1038 #endif
1039         lp_table[i].last_error = 0;
1040         init_waitqueue_head(&lp_table[i].waitq);
1041         init_waitqueue_head(&lp_table[i].dataq);
1042         mutex_init(&lp_table[i].port_mutex);
1043         lp_table[i].timeout = 10 * HZ;
1044         port_num[i] = -1;
1045     }
1046 
1047     if (register_chrdev(LP_MAJOR, "lp", &lp_fops)) {
1048         printk(KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
1049         return -EIO;
1050     }
1051 
1052     lp_class = class_create(THIS_MODULE, "printer");
1053     if (IS_ERR(lp_class)) {
1054         err = PTR_ERR(lp_class);
1055         goto out_reg;
1056     }
1057 
1058     if (parport_register_driver(&lp_driver)) {
1059         printk(KERN_ERR "lp: unable to register with parport\n");
1060         err = -EIO;
1061         goto out_class;
1062     }
1063 
1064     if (!lp_count) {
1065         printk(KERN_INFO "lp: driver loaded but no devices found\n");
1066 #ifndef CONFIG_PARPORT_1284
1067         if (parport_nr[0] == LP_PARPORT_AUTO)
1068             printk(KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
1069 #endif
1070     }
1071 
1072     return 0;
1073 
1074 out_class:
1075     class_destroy(lp_class);
1076 out_reg:
1077     unregister_chrdev(LP_MAJOR, "lp");
1078     return err;
1079 }
1080 
1081 static int __init lp_init_module(void)
1082 {
1083     if (parport[0]) {
1084         /* The user gave some parameters.  Let's see what they were.  */
1085         if (!strncmp(parport[0], "auto", 4))
1086             parport_nr[0] = LP_PARPORT_AUTO;
1087         else {
1088             int n;
1089             for (n = 0; n < LP_NO && parport[n]; n++) {
1090                 if (!strncmp(parport[n], "none", 4))
1091                     parport_nr[n] = LP_PARPORT_NONE;
1092                 else {
1093                     char *ep;
1094                     unsigned long r = simple_strtoul(parport[n], &ep, 0);
1095                     if (ep != parport[n])
1096                         parport_nr[n] = r;
1097                     else {
1098                         printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
1099                         return -ENODEV;
1100                     }
1101                 }
1102             }
1103         }
1104     }
1105 
1106     return lp_init();
1107 }
1108 
1109 static void lp_cleanup_module(void)
1110 {
1111     parport_unregister_driver(&lp_driver);
1112 
1113 #ifdef CONFIG_LP_CONSOLE
1114     unregister_console(&lpcons);
1115 #endif
1116 
1117     unregister_chrdev(LP_MAJOR, "lp");
1118     class_destroy(lp_class);
1119 }
1120 
1121 __setup("lp=", lp_setup);
1122 module_init(lp_init_module);
1123 module_exit(lp_cleanup_module);
1124 
1125 MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
1126 MODULE_LICENSE("GPL");