drm/etnaviv: Implement mmap as GEM object function
[linux-2.6-microblaze.git] / drivers / char / lp.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic parallel printer driver
4  *
5  * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
6  * Copyright (C) 1992,1993 by Michael K. Johnson
7  * - Thanks much to Gunter Windau for pointing out to me where the error
8  *   checking ought to be.
9  * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
10  * Copyright (C) 1994 by Alan Cox (Modularised it)
11  * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
12  * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
13  * "lp=" command line parameters added by Grant Guenther, grant@torque.net
14  * lp_read (Status readback) support added by Carsten Gross,
15  *                                             carsten@sol.wohnheim.uni-ulm.de
16  * Support for parport by Philip Blundell <philb@gnu.org>
17  * Parport sharing hacking by Andrea Arcangeli
18  * Fixed kernel_(to/from)_user memory copy to check for errors
19  *                              by Riccardo Facchetti <fizban@tin.it>
20  * 22-JAN-1998  Added support for devfs  Richard Gooch <rgooch@atnf.csiro.au>
21  * Redesigned interrupt handling for handle printers with buggy handshake
22  *                              by Andrea Arcangeli, 11 May 1998
23  * Full efficient handling of printer with buggy irq handshake (now I have
24  * understood the meaning of the strange handshake). This is done sending new
25  * characters if the interrupt is just happened, even if the printer say to
26  * be still BUSY. This is needed at least with Epson Stylus Color. To enable
27  * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below...
28  * Fixed the irq on the rising edge of the strobe case.
29  * Obsoleted the CAREFUL flag since a printer that doesn' t work with
30  * CAREFUL will block a bit after in lp_check_status().
31  *                              Andrea Arcangeli, 15 Oct 1998
32  * Obsoleted and removed all the lowlevel stuff implemented in the last
33  * month to use the IEEE1284 functions (that handle the _new_ compatibilty
34  * mode fine).
35  */
36
37 /* This driver should, in theory, work with any parallel port that has an
38  * appropriate low-level driver; all I/O is done through the parport
39  * abstraction layer.
40  *
41  * If this driver is built into the kernel, you can configure it using the
42  * kernel command-line.  For example:
43  *
44  *      lp=parport1,none,parport2       (bind lp0 to parport1, disable lp1 and
45  *                                       bind lp2 to parport2)
46  *
47  *      lp=auto                         (assign lp devices to all ports that
48  *                                       have printers attached, as determined
49  *                                       by the IEEE-1284 autoprobe)
50  *
51  *      lp=reset                        (reset the printer during
52  *                                       initialisation)
53  *
54  *      lp=off                          (disable the printer driver entirely)
55  *
56  * If the driver is loaded as a module, similar functionality is available
57  * using module parameters.  The equivalent of the above commands would be:
58  *
59  *      # insmod lp.o parport=1,none,2
60  *
61  *      # insmod lp.o parport=auto
62  *
63  *      # insmod lp.o reset=1
64  */
65
66 /* COMPATIBILITY WITH OLD KERNELS
67  *
68  * Under Linux 2.0 and previous versions, lp devices were bound to ports at
69  * particular I/O addresses, as follows:
70  *
71  *      lp0             0x3bc
72  *      lp1             0x378
73  *      lp2             0x278
74  *
75  * The new driver, by default, binds lp devices to parport devices as it
76  * finds them.  This means that if you only have one port, it will be bound
77  * to lp0 regardless of its I/O address.  If you need the old behaviour, you
78  * can force it using the parameters described above.
79  */
80
81 /*
82  * The new interrupt handling code take care of the buggy handshake
83  * of some HP and Epson printer:
84  * ___
85  * ACK    _______________    ___________
86  *                       |__|
87  * ____
88  * BUSY   _________              _______
89  *                 |____________|
90  *
91  * I discovered this using the printer scanner that you can find at:
92  *
93  *      ftp://e-mind.com/pub/linux/pscan/
94  *
95  *                                      11 May 98, Andrea Arcangeli
96  *
97  * My printer scanner run on an Epson Stylus Color show that such printer
98  * generates the irq on the _rising_ edge of the STROBE. Now lp handle
99  * this case fine too.
100  *
101  *                                      15 Oct 1998, Andrea Arcangeli
102  *
103  * The so called `buggy' handshake is really the well documented
104  * compatibility mode IEEE1284 handshake. They changed the well known
105  * Centronics handshake acking in the middle of busy expecting to not
106  * break drivers or legacy application, while they broken linux lp
107  * until I fixed it reverse engineering the protocol by hand some
108  * month ago...
109  *
110  *                                     14 Dec 1998, Andrea Arcangeli
111  *
112  * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl)
113  */
114
115 #include <linux/module.h>
116 #include <linux/init.h>
117
118 #include <linux/errno.h>
119 #include <linux/kernel.h>
120 #include <linux/major.h>
121 #include <linux/sched/signal.h>
122 #include <linux/slab.h>
123 #include <linux/fcntl.h>
124 #include <linux/delay.h>
125 #include <linux/poll.h>
126 #include <linux/console.h>
127 #include <linux/device.h>
128 #include <linux/wait.h>
129 #include <linux/jiffies.h>
130 #include <linux/mutex.h>
131 #include <linux/compat.h>
132
133 #include <linux/parport.h>
134 #undef LP_STATS
135 #include <linux/lp.h>
136
137 #include <asm/irq.h>
138 #include <linux/uaccess.h>
139
140 /* if you have more than 8 printers, remember to increase LP_NO */
141 #define LP_NO 8
142
143 static DEFINE_MUTEX(lp_mutex);
144 static struct lp_struct lp_table[LP_NO];
145 static int port_num[LP_NO];
146
147 static unsigned int lp_count = 0;
148 static struct class *lp_class;
149
150 #ifdef CONFIG_LP_CONSOLE
151 static struct parport *console_registered;
152 #endif /* CONFIG_LP_CONSOLE */
153
154 #undef LP_DEBUG
155
156 /* Bits used to manage claiming the parport device */
157 #define LP_PREEMPT_REQUEST 1
158 #define LP_PARPORT_CLAIMED 2
159
160 /* --- low-level port access ----------------------------------- */
161
162 #define r_dtr(x)        (parport_read_data(lp_table[(x)].dev->port))
163 #define r_str(x)        (parport_read_status(lp_table[(x)].dev->port))
164 #define w_ctr(x,y)      do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
165 #define w_dtr(x,y)      do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
166
167 /* Claim the parport or block trying unless we've already claimed it */
168 static void lp_claim_parport_or_block(struct lp_struct *this_lp)
169 {
170         if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
171                 parport_claim_or_block(this_lp->dev);
172         }
173 }
174
175 /* Claim the parport or block trying unless we've already claimed it */
176 static void lp_release_parport(struct lp_struct *this_lp)
177 {
178         if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
179                 parport_release(this_lp->dev);
180         }
181 }
182
183
184
185 static int lp_preempt(void *handle)
186 {
187         struct lp_struct *this_lp = (struct lp_struct *)handle;
188         set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
189         return 1;
190 }
191
192
193 /*
194  * Try to negotiate to a new mode; if unsuccessful negotiate to
195  * compatibility mode.  Return the mode we ended up in.
196  */
197 static int lp_negotiate(struct parport *port, int mode)
198 {
199         if (parport_negotiate(port, mode) != 0) {
200                 mode = IEEE1284_MODE_COMPAT;
201                 parport_negotiate(port, mode);
202         }
203
204         return mode;
205 }
206
207 static int lp_reset(int minor)
208 {
209         int retval;
210         lp_claim_parport_or_block(&lp_table[minor]);
211         w_ctr(minor, LP_PSELECP);
212         udelay(LP_DELAY);
213         w_ctr(minor, LP_PSELECP | LP_PINITP);
214         retval = r_str(minor);
215         lp_release_parport(&lp_table[minor]);
216         return retval;
217 }
218
219 static void lp_error(int minor)
220 {
221         DEFINE_WAIT(wait);
222         int polling;
223
224         if (LP_F(minor) & LP_ABORT)
225                 return;
226
227         polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
228         if (polling)
229                 lp_release_parport(&lp_table[minor]);
230         prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
231         schedule_timeout(LP_TIMEOUT_POLLED);
232         finish_wait(&lp_table[minor].waitq, &wait);
233         if (polling)
234                 lp_claim_parport_or_block(&lp_table[minor]);
235         else
236                 parport_yield_blocking(lp_table[minor].dev);
237 }
238
239 static int lp_check_status(int minor)
240 {
241         int error = 0;
242         unsigned int last = lp_table[minor].last_error;
243         unsigned char status = r_str(minor);
244         if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
245                 /* No error. */
246                 last = 0;
247         else if ((status & LP_POUTPA)) {
248                 if (last != LP_POUTPA) {
249                         last = LP_POUTPA;
250                         printk(KERN_INFO "lp%d out of paper\n", minor);
251                 }
252                 error = -ENOSPC;
253         } else if (!(status & LP_PSELECD)) {
254                 if (last != LP_PSELECD) {
255                         last = LP_PSELECD;
256                         printk(KERN_INFO "lp%d off-line\n", minor);
257                 }
258                 error = -EIO;
259         } else if (!(status & LP_PERRORP)) {
260                 if (last != LP_PERRORP) {
261                         last = LP_PERRORP;
262                         printk(KERN_INFO "lp%d on fire\n", minor);
263                 }
264                 error = -EIO;
265         } else {
266                 last = 0; /* Come here if LP_CAREFUL is set and no
267                              errors are reported. */
268         }
269
270         lp_table[minor].last_error = last;
271
272         if (last != 0)
273                 lp_error(minor);
274
275         return error;
276 }
277
278 static int lp_wait_ready(int minor, int nonblock)
279 {
280         int error = 0;
281
282         /* If we're not in compatibility mode, we're ready now! */
283         if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
284                 return 0;
285         }
286
287         do {
288                 error = lp_check_status(minor);
289                 if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
290                         break;
291                 if (signal_pending(current)) {
292                         error = -EINTR;
293                         break;
294                 }
295         } while (error);
296         return error;
297 }
298
299 static ssize_t lp_write(struct file *file, const char __user *buf,
300                         size_t count, loff_t *ppos)
301 {
302         unsigned int minor = iminor(file_inode(file));
303         struct parport *port = lp_table[minor].dev->port;
304         char *kbuf = lp_table[minor].lp_buffer;
305         ssize_t retv = 0;
306         ssize_t written;
307         size_t copy_size = count;
308         int nonblock = ((file->f_flags & O_NONBLOCK) ||
309                         (LP_F(minor) & LP_ABORT));
310
311 #ifdef LP_STATS
312         if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
313                 lp_table[minor].runchars = 0;
314
315         lp_table[minor].lastcall = jiffies;
316 #endif
317
318         /* Need to copy the data from user-space. */
319         if (copy_size > LP_BUFFER_SIZE)
320                 copy_size = LP_BUFFER_SIZE;
321
322         if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
323                 return -EINTR;
324
325         if (copy_from_user(kbuf, buf, copy_size)) {
326                 retv = -EFAULT;
327                 goto out_unlock;
328         }
329
330         /* Claim Parport or sleep until it becomes available
331          */
332         lp_claim_parport_or_block(&lp_table[minor]);
333         /* Go to the proper mode. */
334         lp_table[minor].current_mode = lp_negotiate(port,
335                                                     lp_table[minor].best_mode);
336
337         parport_set_timeout(lp_table[minor].dev,
338                             (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
339                              : lp_table[minor].timeout));
340
341         if ((retv = lp_wait_ready(minor, nonblock)) == 0)
342         do {
343                 /* Write the data. */
344                 written = parport_write(port, kbuf, copy_size);
345                 if (written > 0) {
346                         copy_size -= written;
347                         count -= written;
348                         buf  += written;
349                         retv += written;
350                 }
351
352                 if (signal_pending(current)) {
353                         if (retv == 0)
354                                 retv = -EINTR;
355
356                         break;
357                 }
358
359                 if (copy_size > 0) {
360                         /* incomplete write -> check error ! */
361                         int error;
362
363                         parport_negotiate(lp_table[minor].dev->port,
364                                           IEEE1284_MODE_COMPAT);
365                         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
366
367                         error = lp_wait_ready(minor, nonblock);
368
369                         if (error) {
370                                 if (retv == 0)
371                                         retv = error;
372                                 break;
373                         } else if (nonblock) {
374                                 if (retv == 0)
375                                         retv = -EAGAIN;
376                                 break;
377                         }
378
379                         parport_yield_blocking(lp_table[minor].dev);
380                         lp_table[minor].current_mode
381                           = lp_negotiate(port,
382                                          lp_table[minor].best_mode);
383
384                 } else if (need_resched())
385                         schedule();
386
387                 if (count) {
388                         copy_size = count;
389                         if (copy_size > LP_BUFFER_SIZE)
390                                 copy_size = LP_BUFFER_SIZE;
391
392                         if (copy_from_user(kbuf, buf, copy_size)) {
393                                 if (retv == 0)
394                                         retv = -EFAULT;
395                                 break;
396                         }
397                 }
398         } while (count > 0);
399
400         if (test_and_clear_bit(LP_PREEMPT_REQUEST,
401                                &lp_table[minor].bits)) {
402                 printk(KERN_INFO "lp%d releasing parport\n", minor);
403                 parport_negotiate(lp_table[minor].dev->port,
404                                   IEEE1284_MODE_COMPAT);
405                 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
406                 lp_release_parport(&lp_table[minor]);
407         }
408 out_unlock:
409         mutex_unlock(&lp_table[minor].port_mutex);
410
411         return retv;
412 }
413
414 #ifdef CONFIG_PARPORT_1284
415
416 /* Status readback conforming to ieee1284 */
417 static ssize_t lp_read(struct file *file, char __user *buf,
418                        size_t count, loff_t *ppos)
419 {
420         DEFINE_WAIT(wait);
421         unsigned int minor=iminor(file_inode(file));
422         struct parport *port = lp_table[minor].dev->port;
423         ssize_t retval = 0;
424         char *kbuf = lp_table[minor].lp_buffer;
425         int nonblock = ((file->f_flags & O_NONBLOCK) ||
426                         (LP_F(minor) & LP_ABORT));
427
428         if (count > LP_BUFFER_SIZE)
429                 count = LP_BUFFER_SIZE;
430
431         if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
432                 return -EINTR;
433
434         lp_claim_parport_or_block(&lp_table[minor]);
435
436         parport_set_timeout(lp_table[minor].dev,
437                             (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
438                              : lp_table[minor].timeout));
439
440         parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
441         if (parport_negotiate(lp_table[minor].dev->port,
442                               IEEE1284_MODE_NIBBLE)) {
443                 retval = -EIO;
444                 goto out;
445         }
446
447         while (retval == 0) {
448                 retval = parport_read(port, kbuf, count);
449
450                 if (retval > 0)
451                         break;
452
453                 if (nonblock) {
454                         retval = -EAGAIN;
455                         break;
456                 }
457
458                 /* Wait for data. */
459
460                 if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
461                         parport_negotiate(lp_table[minor].dev->port,
462                                           IEEE1284_MODE_COMPAT);
463                         lp_error(minor);
464                         if (parport_negotiate(lp_table[minor].dev->port,
465                                               IEEE1284_MODE_NIBBLE)) {
466                                 retval = -EIO;
467                                 goto out;
468                         }
469                 } else {
470                         prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
471                         schedule_timeout(LP_TIMEOUT_POLLED);
472                         finish_wait(&lp_table[minor].waitq, &wait);
473                 }
474
475                 if (signal_pending(current)) {
476                         retval = -ERESTARTSYS;
477                         break;
478                 }
479
480                 cond_resched();
481         }
482         parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
483  out:
484         lp_release_parport(&lp_table[minor]);
485
486         if (retval > 0 && copy_to_user(buf, kbuf, retval))
487                 retval = -EFAULT;
488
489         mutex_unlock(&lp_table[minor].port_mutex);
490
491         return retval;
492 }
493
494 #endif /* IEEE 1284 support */
495
496 static int lp_open(struct inode *inode, struct file *file)
497 {
498         unsigned int minor = iminor(inode);
499         int ret = 0;
500
501         mutex_lock(&lp_mutex);
502         if (minor >= LP_NO) {
503                 ret = -ENXIO;
504                 goto out;
505         }
506         if ((LP_F(minor) & LP_EXIST) == 0) {
507                 ret = -ENXIO;
508                 goto out;
509         }
510         if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
511                 ret = -EBUSY;
512                 goto out;
513         }
514         /* If ABORTOPEN is set and the printer is offline or out of paper,
515            we may still want to open it to perform ioctl()s.  Therefore we
516            have commandeered O_NONBLOCK, even though it is being used in
517            a non-standard manner.  This is strictly a Linux hack, and
518            should most likely only ever be used by the tunelp application. */
519         if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
520                 int status;
521                 lp_claim_parport_or_block(&lp_table[minor]);
522                 status = r_str(minor);
523                 lp_release_parport(&lp_table[minor]);
524                 if (status & LP_POUTPA) {
525                         printk(KERN_INFO "lp%d out of paper\n", minor);
526                         LP_F(minor) &= ~LP_BUSY;
527                         ret = -ENOSPC;
528                         goto out;
529                 } else if (!(status & LP_PSELECD)) {
530                         printk(KERN_INFO "lp%d off-line\n", minor);
531                         LP_F(minor) &= ~LP_BUSY;
532                         ret = -EIO;
533                         goto out;
534                 } else if (!(status & LP_PERRORP)) {
535                         printk(KERN_ERR "lp%d printer error\n", minor);
536                         LP_F(minor) &= ~LP_BUSY;
537                         ret = -EIO;
538                         goto out;
539                 }
540         }
541         lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
542         if (!lp_table[minor].lp_buffer) {
543                 LP_F(minor) &= ~LP_BUSY;
544                 ret = -ENOMEM;
545                 goto out;
546         }
547         /* Determine if the peripheral supports ECP mode */
548         lp_claim_parport_or_block(&lp_table[minor]);
549         if ((lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
550              !parport_negotiate(lp_table[minor].dev->port,
551                                  IEEE1284_MODE_ECP)) {
552                 printk(KERN_INFO "lp%d: ECP mode\n", minor);
553                 lp_table[minor].best_mode = IEEE1284_MODE_ECP;
554         } else {
555                 lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
556         }
557         /* Leave peripheral in compatibility mode */
558         parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
559         lp_release_parport(&lp_table[minor]);
560         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
561 out:
562         mutex_unlock(&lp_mutex);
563         return ret;
564 }
565
566 static int lp_release(struct inode *inode, struct file *file)
567 {
568         unsigned int minor = iminor(inode);
569
570         lp_claim_parport_or_block(&lp_table[minor]);
571         parport_negotiate(lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
572         lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
573         lp_release_parport(&lp_table[minor]);
574         kfree(lp_table[minor].lp_buffer);
575         lp_table[minor].lp_buffer = NULL;
576         LP_F(minor) &= ~LP_BUSY;
577         return 0;
578 }
579
580 static int lp_do_ioctl(unsigned int minor, unsigned int cmd,
581         unsigned long arg, void __user *argp)
582 {
583         int status;
584         int retval = 0;
585
586 #ifdef LP_DEBUG
587         printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
588 #endif
589         if (minor >= LP_NO)
590                 return -ENODEV;
591         if ((LP_F(minor) & LP_EXIST) == 0)
592                 return -ENODEV;
593         switch (cmd) {
594                 case LPTIME:
595                         if (arg > UINT_MAX / HZ)
596                                 return -EINVAL;
597                         LP_TIME(minor) = arg * HZ/100;
598                         break;
599                 case LPCHAR:
600                         LP_CHAR(minor) = arg;
601                         break;
602                 case LPABORT:
603                         if (arg)
604                                 LP_F(minor) |= LP_ABORT;
605                         else
606                                 LP_F(minor) &= ~LP_ABORT;
607                         break;
608                 case LPABORTOPEN:
609                         if (arg)
610                                 LP_F(minor) |= LP_ABORTOPEN;
611                         else
612                                 LP_F(minor) &= ~LP_ABORTOPEN;
613                         break;
614                 case LPCAREFUL:
615                         if (arg)
616                                 LP_F(minor) |= LP_CAREFUL;
617                         else
618                                 LP_F(minor) &= ~LP_CAREFUL;
619                         break;
620                 case LPWAIT:
621                         LP_WAIT(minor) = arg;
622                         break;
623                 case LPSETIRQ:
624                         return -EINVAL;
625                 case LPGETIRQ:
626                         if (copy_to_user(argp, &LP_IRQ(minor),
627                                         sizeof(int)))
628                                 return -EFAULT;
629                         break;
630                 case LPGETSTATUS:
631                         if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
632                                 return -EINTR;
633                         lp_claim_parport_or_block(&lp_table[minor]);
634                         status = r_str(minor);
635                         lp_release_parport(&lp_table[minor]);
636                         mutex_unlock(&lp_table[minor].port_mutex);
637
638                         if (copy_to_user(argp, &status, sizeof(int)))
639                                 return -EFAULT;
640                         break;
641                 case LPRESET:
642                         lp_reset(minor);
643                         break;
644 #ifdef LP_STATS
645                 case LPGETSTATS:
646                         if (copy_to_user(argp, &LP_STAT(minor),
647                                         sizeof(struct lp_stats)))
648                                 return -EFAULT;
649                         if (capable(CAP_SYS_ADMIN))
650                                 memset(&LP_STAT(minor), 0,
651                                                 sizeof(struct lp_stats));
652                         break;
653 #endif
654                 case LPGETFLAGS:
655                         status = LP_F(minor);
656                         if (copy_to_user(argp, &status, sizeof(int)))
657                                 return -EFAULT;
658                         break;
659
660                 default:
661                         retval = -EINVAL;
662         }
663         return retval;
664 }
665
666 static int lp_set_timeout(unsigned int minor, s64 tv_sec, long tv_usec)
667 {
668         long to_jiffies;
669
670         /* Convert to jiffies, place in lp_table */
671         if (tv_sec < 0 || tv_usec < 0)
672                 return -EINVAL;
673
674         /*
675          * we used to not check, so let's not make this fatal,
676          * but deal with user space passing a 32-bit tv_nsec in
677          * a 64-bit field, capping the timeout to 1 second
678          * worth of microseconds, and capping the total at
679          * MAX_JIFFY_OFFSET.
680          */
681         if (tv_usec > 999999)
682                 tv_usec = 999999;
683
684         if (tv_sec >= MAX_SEC_IN_JIFFIES - 1) {
685                 to_jiffies = MAX_JIFFY_OFFSET;
686         } else {
687                 to_jiffies = DIV_ROUND_UP(tv_usec, 1000000/HZ);
688                 to_jiffies += tv_sec * (long) HZ;
689         }
690
691         if (to_jiffies <= 0) {
692                 return -EINVAL;
693         }
694         lp_table[minor].timeout = to_jiffies;
695         return 0;
696 }
697
698 static int lp_set_timeout32(unsigned int minor, void __user *arg)
699 {
700         s32 karg[2];
701
702         if (copy_from_user(karg, arg, sizeof(karg)))
703                 return -EFAULT;
704
705         return lp_set_timeout(minor, karg[0], karg[1]);
706 }
707
708 static int lp_set_timeout64(unsigned int minor, void __user *arg)
709 {
710         s64 karg[2];
711
712         if (copy_from_user(karg, arg, sizeof(karg)))
713                 return -EFAULT;
714
715         /* sparc64 suseconds_t is 32-bit only */
716         if (IS_ENABLED(CONFIG_SPARC64) && !in_compat_syscall())
717                 karg[1] >>= 32;
718
719         return lp_set_timeout(minor, karg[0], karg[1]);
720 }
721
722 static long lp_ioctl(struct file *file, unsigned int cmd,
723                         unsigned long arg)
724 {
725         unsigned int minor;
726         int ret;
727
728         minor = iminor(file_inode(file));
729         mutex_lock(&lp_mutex);
730         switch (cmd) {
731         case LPSETTIMEOUT_OLD:
732                 if (BITS_PER_LONG == 32) {
733                         ret = lp_set_timeout32(minor, (void __user *)arg);
734                         break;
735                 }
736                 fallthrough;    /* for 64-bit */
737         case LPSETTIMEOUT_NEW:
738                 ret = lp_set_timeout64(minor, (void __user *)arg);
739                 break;
740         default:
741                 ret = lp_do_ioctl(minor, cmd, arg, (void __user *)arg);
742                 break;
743         }
744         mutex_unlock(&lp_mutex);
745
746         return ret;
747 }
748
749 #ifdef CONFIG_COMPAT
750 static long lp_compat_ioctl(struct file *file, unsigned int cmd,
751                         unsigned long arg)
752 {
753         unsigned int minor;
754         int ret;
755
756         minor = iminor(file_inode(file));
757         mutex_lock(&lp_mutex);
758         switch (cmd) {
759         case LPSETTIMEOUT_OLD:
760                 if (!COMPAT_USE_64BIT_TIME) {
761                         ret = lp_set_timeout32(minor, (void __user *)arg);
762                         break;
763                 }
764                 fallthrough;    /* for x32 mode */
765         case LPSETTIMEOUT_NEW:
766                 ret = lp_set_timeout64(minor, (void __user *)arg);
767                 break;
768 #ifdef LP_STATS
769         case LPGETSTATS:
770                 /* FIXME: add an implementation if you set LP_STATS */
771                 ret = -EINVAL;
772                 break;
773 #endif
774         default:
775                 ret = lp_do_ioctl(minor, cmd, arg, compat_ptr(arg));
776                 break;
777         }
778         mutex_unlock(&lp_mutex);
779
780         return ret;
781 }
782 #endif
783
784 static const struct file_operations lp_fops = {
785         .owner          = THIS_MODULE,
786         .write          = lp_write,
787         .unlocked_ioctl = lp_ioctl,
788 #ifdef CONFIG_COMPAT
789         .compat_ioctl   = lp_compat_ioctl,
790 #endif
791         .open           = lp_open,
792         .release        = lp_release,
793 #ifdef CONFIG_PARPORT_1284
794         .read           = lp_read,
795 #endif
796         .llseek         = noop_llseek,
797 };
798
799 /* --- support for console on the line printer ----------------- */
800
801 #ifdef CONFIG_LP_CONSOLE
802
803 #define CONSOLE_LP 0
804
805 /* If the printer is out of paper, we can either lose the messages or
806  * stall until the printer is happy again.  Define CONSOLE_LP_STRICT
807  * non-zero to get the latter behaviour. */
808 #define CONSOLE_LP_STRICT 1
809
810 /* The console must be locked when we get here. */
811
812 static void lp_console_write(struct console *co, const char *s,
813                              unsigned count)
814 {
815         struct pardevice *dev = lp_table[CONSOLE_LP].dev;
816         struct parport *port = dev->port;
817         ssize_t written;
818
819         if (parport_claim(dev))
820                 /* Nothing we can do. */
821                 return;
822
823         parport_set_timeout(dev, 0);
824
825         /* Go to compatibility mode. */
826         parport_negotiate(port, IEEE1284_MODE_COMPAT);
827
828         do {
829                 /* Write the data, converting LF->CRLF as we go. */
830                 ssize_t canwrite = count;
831                 char *lf = memchr(s, '\n', count);
832                 if (lf)
833                         canwrite = lf - s;
834
835                 if (canwrite > 0) {
836                         written = parport_write(port, s, canwrite);
837
838                         if (written <= 0)
839                                 continue;
840
841                         s += written;
842                         count -= written;
843                         canwrite -= written;
844                 }
845
846                 if (lf && canwrite <= 0) {
847                         const char *crlf = "\r\n";
848                         int i = 2;
849
850                         /* Dodge the original '\n', and put '\r\n' instead. */
851                         s++;
852                         count--;
853                         do {
854                                 written = parport_write(port, crlf, i);
855                                 if (written > 0) {
856                                         i -= written;
857                                         crlf += written;
858                                 }
859                         } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
860                 }
861         } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
862
863         parport_release(dev);
864 }
865
866 static struct console lpcons = {
867         .name           = "lp",
868         .write          = lp_console_write,
869         .flags          = CON_PRINTBUFFER,
870 };
871
872 #endif /* console on line printer */
873
874 /* --- initialisation code ------------------------------------- */
875
876 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
877 static char *parport[LP_NO];
878 static bool reset;
879
880 module_param_array(parport, charp, NULL, 0);
881 module_param(reset, bool, 0);
882
883 #ifndef MODULE
884 static int __init lp_setup(char *str)
885 {
886         static int parport_ptr;
887         int x;
888
889         if (get_option(&str, &x)) {
890                 if (x == 0) {
891                         /* disable driver on "lp=" or "lp=0" */
892                         parport_nr[0] = LP_PARPORT_OFF;
893                 } else {
894                         printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
895                         return 0;
896                 }
897         } else if (!strncmp(str, "parport", 7)) {
898                 int n = simple_strtoul(str+7, NULL, 10);
899                 if (parport_ptr < LP_NO)
900                         parport_nr[parport_ptr++] = n;
901                 else
902                         printk(KERN_INFO "lp: too many ports, %s ignored.\n",
903                                str);
904         } else if (!strcmp(str, "auto")) {
905                 parport_nr[0] = LP_PARPORT_AUTO;
906         } else if (!strcmp(str, "none")) {
907                 if (parport_ptr < LP_NO)
908                         parport_nr[parport_ptr++] = LP_PARPORT_NONE;
909                 else
910                         printk(KERN_INFO "lp: too many ports, %s ignored.\n",
911                                str);
912         } else if (!strcmp(str, "reset")) {
913                 reset = true;
914         }
915         return 1;
916 }
917 #endif
918
919 static int lp_register(int nr, struct parport *port)
920 {
921         struct pardev_cb ppdev_cb;
922
923         memset(&ppdev_cb, 0, sizeof(ppdev_cb));
924         ppdev_cb.preempt = lp_preempt;
925         ppdev_cb.private = &lp_table[nr];
926         lp_table[nr].dev = parport_register_dev_model(port, "lp",
927                                                       &ppdev_cb, nr);
928         if (lp_table[nr].dev == NULL)
929                 return 1;
930         lp_table[nr].flags |= LP_EXIST;
931
932         if (reset)
933                 lp_reset(nr);
934
935         device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
936                       "lp%d", nr);
937
938         printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
939                (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
940
941 #ifdef CONFIG_LP_CONSOLE
942         if (!nr) {
943                 if (port->modes & PARPORT_MODE_SAFEININT) {
944                         register_console(&lpcons);
945                         console_registered = port;
946                         printk(KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
947                 } else
948                         printk(KERN_ERR "lp%d: cannot run console on %s\n",
949                                CONSOLE_LP, port->name);
950         }
951 #endif
952         port_num[nr] = port->number;
953
954         return 0;
955 }
956
957 static void lp_attach(struct parport *port)
958 {
959         unsigned int i;
960
961         switch (parport_nr[0]) {
962         case LP_PARPORT_UNSPEC:
963         case LP_PARPORT_AUTO:
964                 if (parport_nr[0] == LP_PARPORT_AUTO &&
965                     port->probe_info[0].class != PARPORT_CLASS_PRINTER)
966                         return;
967                 if (lp_count == LP_NO) {
968                         printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
969                         return;
970                 }
971                 for (i = 0; i < LP_NO; i++)
972                         if (port_num[i] == -1)
973                                 break;
974
975                 if (!lp_register(i, port))
976                         lp_count++;
977                 break;
978
979         default:
980                 for (i = 0; i < LP_NO; i++) {
981                         if (port->number == parport_nr[i]) {
982                                 if (!lp_register(i, port))
983                                         lp_count++;
984                                 break;
985                         }
986                 }
987                 break;
988         }
989 }
990
991 static void lp_detach(struct parport *port)
992 {
993         int n;
994
995         /* Write this some day. */
996 #ifdef CONFIG_LP_CONSOLE
997         if (console_registered == port) {
998                 unregister_console(&lpcons);
999                 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 = 0;
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");