async_tx: avoid the async xor_zero_sum path when src_cnt > device->max_xor
[wrt350n-kernel.git] / drivers / char / lp.c
blob60ac642752bea4441604515513efa160d6e8c73f
1 /*
2 * Generic parallel printer driver
4 * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
5 * Copyright (C) 1992,1993 by Michael K. Johnson
6 * - Thanks much to Gunter Windau for pointing out to me where the error
7 * checking ought to be.
8 * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
9 * Copyright (C) 1994 by Alan Cox (Modularised it)
10 * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
11 * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
12 * "lp=" command line parameters added by Grant Guenther, grant@torque.net
13 * lp_read (Status readback) support added by Carsten Gross,
14 * carsten@sol.wohnheim.uni-ulm.de
15 * Support for parport by Philip Blundell <philb@gnu.org>
16 * Parport sharing hacking by Andrea Arcangeli
17 * Fixed kernel_(to/from)_user memory copy to check for errors
18 * by Riccardo Facchetti <fizban@tin.it>
19 * 22-JAN-1998 Added support for devfs Richard Gooch <rgooch@atnf.csiro.au>
20 * Redesigned interrupt handling for handle printers with buggy handshake
21 * by Andrea Arcangeli, 11 May 1998
22 * Full efficient handling of printer with buggy irq handshake (now I have
23 * understood the meaning of the strange handshake). This is done sending new
24 * characters if the interrupt is just happened, even if the printer say to
25 * be still BUSY. This is needed at least with Epson Stylus Color. To enable
26 * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below...
27 * Fixed the irq on the rising edge of the strobe case.
28 * Obsoleted the CAREFUL flag since a printer that doesn' t work with
29 * CAREFUL will block a bit after in lp_check_status().
30 * Andrea Arcangeli, 15 Oct 1998
31 * Obsoleted and removed all the lowlevel stuff implemented in the last
32 * month to use the IEEE1284 functions (that handle the _new_ compatibilty
33 * mode fine).
36 /* This driver should, in theory, work with any parallel port that has an
37 * appropriate low-level driver; all I/O is done through the parport
38 * abstraction layer.
40 * If this driver is built into the kernel, you can configure it using the
41 * kernel command-line. For example:
43 * lp=parport1,none,parport2 (bind lp0 to parport1, disable lp1 and
44 * bind lp2 to parport2)
46 * lp=auto (assign lp devices to all ports that
47 * have printers attached, as determined
48 * by the IEEE-1284 autoprobe)
50 * lp=reset (reset the printer during
51 * initialisation)
53 * lp=off (disable the printer driver entirely)
55 * If the driver is loaded as a module, similar functionality is available
56 * using module parameters. The equivalent of the above commands would be:
58 * # insmod lp.o parport=1,none,2
60 * # insmod lp.o parport=auto
62 * # insmod lp.o reset=1
65 /* COMPATIBILITY WITH OLD KERNELS
67 * Under Linux 2.0 and previous versions, lp devices were bound to ports at
68 * particular I/O addresses, as follows:
70 * lp0 0x3bc
71 * lp1 0x378
72 * lp2 0x278
74 * The new driver, by default, binds lp devices to parport devices as it
75 * finds them. This means that if you only have one port, it will be bound
76 * to lp0 regardless of its I/O address. If you need the old behaviour, you
77 * can force it using the parameters described above.
81 * The new interrupt handling code take care of the buggy handshake
82 * of some HP and Epson printer:
83 * ___
84 * ACK _______________ ___________
85 * |__|
86 * ____
87 * BUSY _________ _______
88 * |____________|
90 * I discovered this using the printer scanner that you can find at:
92 * ftp://e-mind.com/pub/linux/pscan/
94 * 11 May 98, Andrea Arcangeli
96 * My printer scanner run on an Epson Stylus Color show that such printer
97 * generates the irq on the _rising_ edge of the STROBE. Now lp handle
98 * this case fine too.
100 * 15 Oct 1998, Andrea Arcangeli
102 * The so called `buggy' handshake is really the well documented
103 * compatibility mode IEEE1284 handshake. They changed the well known
104 * Centronics handshake acking in the middle of busy expecting to not
105 * break drivers or legacy application, while they broken linux lp
106 * until I fixed it reverse engineering the protocol by hand some
107 * month ago...
109 * 14 Dec 1998, Andrea Arcangeli
111 * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl)
114 #include <linux/module.h>
115 #include <linux/init.h>
117 #include <linux/errno.h>
118 #include <linux/kernel.h>
119 #include <linux/major.h>
120 #include <linux/sched.h>
121 #include <linux/slab.h>
122 #include <linux/fcntl.h>
123 #include <linux/delay.h>
124 #include <linux/poll.h>
125 #include <linux/console.h>
126 #include <linux/device.h>
127 #include <linux/wait.h>
128 #include <linux/jiffies.h>
130 #include <linux/parport.h>
131 #undef LP_STATS
132 #include <linux/lp.h>
134 #include <asm/irq.h>
135 #include <asm/uaccess.h>
136 #include <asm/system.h>
138 /* if you have more than 8 printers, remember to increase LP_NO */
139 #define LP_NO 8
141 static struct lp_struct lp_table[LP_NO];
143 static unsigned int lp_count = 0;
144 static struct class *lp_class;
146 #ifdef CONFIG_LP_CONSOLE
147 static struct parport *console_registered;
148 #endif /* CONFIG_LP_CONSOLE */
150 #undef LP_DEBUG
152 /* Bits used to manage claiming the parport device */
153 #define LP_PREEMPT_REQUEST 1
154 #define LP_PARPORT_CLAIMED 2
156 /* --- low-level port access ----------------------------------- */
158 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
159 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
160 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
161 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
163 /* Claim the parport or block trying unless we've already claimed it */
164 static void lp_claim_parport_or_block(struct lp_struct *this_lp)
166 if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
167 parport_claim_or_block (this_lp->dev);
171 /* Claim the parport or block trying unless we've already claimed it */
172 static void lp_release_parport(struct lp_struct *this_lp)
174 if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
175 parport_release (this_lp->dev);
181 static int lp_preempt(void *handle)
183 struct lp_struct *this_lp = (struct lp_struct *)handle;
184 set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
185 return (1);
190 * Try to negotiate to a new mode; if unsuccessful negotiate to
191 * compatibility mode. Return the mode we ended up in.
193 static int lp_negotiate(struct parport * port, int mode)
195 if (parport_negotiate (port, mode) != 0) {
196 mode = IEEE1284_MODE_COMPAT;
197 parport_negotiate (port, mode);
200 return (mode);
203 static int lp_reset(int minor)
205 int retval;
206 lp_claim_parport_or_block (&lp_table[minor]);
207 w_ctr(minor, LP_PSELECP);
208 udelay (LP_DELAY);
209 w_ctr(minor, LP_PSELECP | LP_PINITP);
210 retval = r_str(minor);
211 lp_release_parport (&lp_table[minor]);
212 return retval;
215 static void lp_error (int minor)
217 DEFINE_WAIT(wait);
218 int polling;
220 if (LP_F(minor) & LP_ABORT)
221 return;
223 polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
224 if (polling) lp_release_parport (&lp_table[minor]);
225 prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
226 schedule_timeout(LP_TIMEOUT_POLLED);
227 finish_wait(&lp_table[minor].waitq, &wait);
228 if (polling) lp_claim_parport_or_block (&lp_table[minor]);
229 else parport_yield_blocking (lp_table[minor].dev);
232 static int lp_check_status(int minor)
234 int error = 0;
235 unsigned int last = lp_table[minor].last_error;
236 unsigned char status = r_str(minor);
237 if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
238 /* No error. */
239 last = 0;
240 else if ((status & LP_POUTPA)) {
241 if (last != LP_POUTPA) {
242 last = LP_POUTPA;
243 printk(KERN_INFO "lp%d out of paper\n", minor);
245 error = -ENOSPC;
246 } else if (!(status & LP_PSELECD)) {
247 if (last != LP_PSELECD) {
248 last = LP_PSELECD;
249 printk(KERN_INFO "lp%d off-line\n", minor);
251 error = -EIO;
252 } else if (!(status & LP_PERRORP)) {
253 if (last != LP_PERRORP) {
254 last = LP_PERRORP;
255 printk(KERN_INFO "lp%d on fire\n", minor);
257 error = -EIO;
258 } else {
259 last = 0; /* Come here if LP_CAREFUL is set and no
260 errors are reported. */
263 lp_table[minor].last_error = last;
265 if (last != 0)
266 lp_error(minor);
268 return error;
271 static int lp_wait_ready(int minor, int nonblock)
273 int error = 0;
275 /* If we're not in compatibility mode, we're ready now! */
276 if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
277 return (0);
280 do {
281 error = lp_check_status (minor);
282 if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
283 break;
284 if (signal_pending (current)) {
285 error = -EINTR;
286 break;
288 } while (error);
289 return error;
292 static ssize_t lp_write(struct file * file, const char __user * buf,
293 size_t count, loff_t *ppos)
295 unsigned int minor = iminor(file->f_path.dentry->d_inode);
296 struct parport *port = lp_table[minor].dev->port;
297 char *kbuf = lp_table[minor].lp_buffer;
298 ssize_t retv = 0;
299 ssize_t written;
300 size_t copy_size = count;
301 int nonblock = ((file->f_flags & O_NONBLOCK) ||
302 (LP_F(minor) & LP_ABORT));
304 #ifdef LP_STATS
305 if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
306 lp_table[minor].runchars = 0;
308 lp_table[minor].lastcall = jiffies;
309 #endif
311 /* Need to copy the data from user-space. */
312 if (copy_size > LP_BUFFER_SIZE)
313 copy_size = LP_BUFFER_SIZE;
315 if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
316 return -EINTR;
318 if (copy_from_user (kbuf, buf, copy_size)) {
319 retv = -EFAULT;
320 goto out_unlock;
323 /* Claim Parport or sleep until it becomes available
325 lp_claim_parport_or_block (&lp_table[minor]);
326 /* Go to the proper mode. */
327 lp_table[minor].current_mode = lp_negotiate (port,
328 lp_table[minor].best_mode);
330 parport_set_timeout (lp_table[minor].dev,
331 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
332 : lp_table[minor].timeout));
334 if ((retv = lp_wait_ready (minor, nonblock)) == 0)
335 do {
336 /* Write the data. */
337 written = parport_write (port, kbuf, copy_size);
338 if (written > 0) {
339 copy_size -= written;
340 count -= written;
341 buf += written;
342 retv += written;
345 if (signal_pending (current)) {
346 if (retv == 0)
347 retv = -EINTR;
349 break;
352 if (copy_size > 0) {
353 /* incomplete write -> check error ! */
354 int error;
356 parport_negotiate (lp_table[minor].dev->port,
357 IEEE1284_MODE_COMPAT);
358 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
360 error = lp_wait_ready (minor, nonblock);
362 if (error) {
363 if (retv == 0)
364 retv = error;
365 break;
366 } else if (nonblock) {
367 if (retv == 0)
368 retv = -EAGAIN;
369 break;
372 parport_yield_blocking (lp_table[minor].dev);
373 lp_table[minor].current_mode
374 = lp_negotiate (port,
375 lp_table[minor].best_mode);
377 } else if (need_resched())
378 schedule ();
380 if (count) {
381 copy_size = count;
382 if (copy_size > LP_BUFFER_SIZE)
383 copy_size = LP_BUFFER_SIZE;
385 if (copy_from_user(kbuf, buf, copy_size)) {
386 if (retv == 0)
387 retv = -EFAULT;
388 break;
391 } while (count > 0);
393 if (test_and_clear_bit(LP_PREEMPT_REQUEST,
394 &lp_table[minor].bits)) {
395 printk(KERN_INFO "lp%d releasing parport\n", minor);
396 parport_negotiate (lp_table[minor].dev->port,
397 IEEE1284_MODE_COMPAT);
398 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
399 lp_release_parport (&lp_table[minor]);
401 out_unlock:
402 mutex_unlock(&lp_table[minor].port_mutex);
404 return retv;
407 #ifdef CONFIG_PARPORT_1284
409 /* Status readback conforming to ieee1284 */
410 static ssize_t lp_read(struct file * file, char __user * buf,
411 size_t count, loff_t *ppos)
413 DEFINE_WAIT(wait);
414 unsigned int minor=iminor(file->f_path.dentry->d_inode);
415 struct parport *port = lp_table[minor].dev->port;
416 ssize_t retval = 0;
417 char *kbuf = lp_table[minor].lp_buffer;
418 int nonblock = ((file->f_flags & O_NONBLOCK) ||
419 (LP_F(minor) & LP_ABORT));
421 if (count > LP_BUFFER_SIZE)
422 count = LP_BUFFER_SIZE;
424 if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
425 return -EINTR;
427 lp_claim_parport_or_block (&lp_table[minor]);
429 parport_set_timeout (lp_table[minor].dev,
430 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
431 : lp_table[minor].timeout));
433 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
434 if (parport_negotiate (lp_table[minor].dev->port,
435 IEEE1284_MODE_NIBBLE)) {
436 retval = -EIO;
437 goto out;
440 while (retval == 0) {
441 retval = parport_read (port, kbuf, count);
443 if (retval > 0)
444 break;
446 if (nonblock) {
447 retval = -EAGAIN;
448 break;
451 /* Wait for data. */
453 if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
454 parport_negotiate (lp_table[minor].dev->port,
455 IEEE1284_MODE_COMPAT);
456 lp_error (minor);
457 if (parport_negotiate (lp_table[minor].dev->port,
458 IEEE1284_MODE_NIBBLE)) {
459 retval = -EIO;
460 goto out;
462 } else {
463 prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
464 schedule_timeout(LP_TIMEOUT_POLLED);
465 finish_wait(&lp_table[minor].waitq, &wait);
468 if (signal_pending (current)) {
469 retval = -ERESTARTSYS;
470 break;
473 cond_resched ();
475 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
476 out:
477 lp_release_parport (&lp_table[minor]);
479 if (retval > 0 && copy_to_user (buf, kbuf, retval))
480 retval = -EFAULT;
482 mutex_unlock(&lp_table[minor].port_mutex);
484 return retval;
487 #endif /* IEEE 1284 support */
489 static int lp_open(struct inode * inode, struct file * file)
491 unsigned int minor = iminor(inode);
493 if (minor >= LP_NO)
494 return -ENXIO;
495 if ((LP_F(minor) & LP_EXIST) == 0)
496 return -ENXIO;
497 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
498 return -EBUSY;
500 /* If ABORTOPEN is set and the printer is offline or out of paper,
501 we may still want to open it to perform ioctl()s. Therefore we
502 have commandeered O_NONBLOCK, even though it is being used in
503 a non-standard manner. This is strictly a Linux hack, and
504 should most likely only ever be used by the tunelp application. */
505 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
506 int status;
507 lp_claim_parport_or_block (&lp_table[minor]);
508 status = r_str(minor);
509 lp_release_parport (&lp_table[minor]);
510 if (status & LP_POUTPA) {
511 printk(KERN_INFO "lp%d out of paper\n", minor);
512 LP_F(minor) &= ~LP_BUSY;
513 return -ENOSPC;
514 } else if (!(status & LP_PSELECD)) {
515 printk(KERN_INFO "lp%d off-line\n", minor);
516 LP_F(minor) &= ~LP_BUSY;
517 return -EIO;
518 } else if (!(status & LP_PERRORP)) {
519 printk(KERN_ERR "lp%d printer error\n", minor);
520 LP_F(minor) &= ~LP_BUSY;
521 return -EIO;
524 lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
525 if (!lp_table[minor].lp_buffer) {
526 LP_F(minor) &= ~LP_BUSY;
527 return -ENOMEM;
529 /* Determine if the peripheral supports ECP mode */
530 lp_claim_parport_or_block (&lp_table[minor]);
531 if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
532 !parport_negotiate (lp_table[minor].dev->port,
533 IEEE1284_MODE_ECP)) {
534 printk (KERN_INFO "lp%d: ECP mode\n", minor);
535 lp_table[minor].best_mode = IEEE1284_MODE_ECP;
536 } else {
537 lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
539 /* Leave peripheral in compatibility mode */
540 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
541 lp_release_parport (&lp_table[minor]);
542 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
543 return 0;
546 static int lp_release(struct inode * inode, struct file * file)
548 unsigned int minor = iminor(inode);
550 lp_claim_parport_or_block (&lp_table[minor]);
551 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
552 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
553 lp_release_parport (&lp_table[minor]);
554 kfree(lp_table[minor].lp_buffer);
555 lp_table[minor].lp_buffer = NULL;
556 LP_F(minor) &= ~LP_BUSY;
557 return 0;
560 static int lp_ioctl(struct inode *inode, struct file *file,
561 unsigned int cmd, unsigned long arg)
563 unsigned int minor = iminor(inode);
564 int status;
565 int retval = 0;
566 void __user *argp = (void __user *)arg;
568 #ifdef LP_DEBUG
569 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
570 #endif
571 if (minor >= LP_NO)
572 return -ENODEV;
573 if ((LP_F(minor) & LP_EXIST) == 0)
574 return -ENODEV;
575 switch ( cmd ) {
576 struct timeval par_timeout;
577 long to_jiffies;
579 case LPTIME:
580 LP_TIME(minor) = arg * HZ/100;
581 break;
582 case LPCHAR:
583 LP_CHAR(minor) = arg;
584 break;
585 case LPABORT:
586 if (arg)
587 LP_F(minor) |= LP_ABORT;
588 else
589 LP_F(minor) &= ~LP_ABORT;
590 break;
591 case LPABORTOPEN:
592 if (arg)
593 LP_F(minor) |= LP_ABORTOPEN;
594 else
595 LP_F(minor) &= ~LP_ABORTOPEN;
596 break;
597 case LPCAREFUL:
598 if (arg)
599 LP_F(minor) |= LP_CAREFUL;
600 else
601 LP_F(minor) &= ~LP_CAREFUL;
602 break;
603 case LPWAIT:
604 LP_WAIT(minor) = arg;
605 break;
606 case LPSETIRQ:
607 return -EINVAL;
608 break;
609 case LPGETIRQ:
610 if (copy_to_user(argp, &LP_IRQ(minor),
611 sizeof(int)))
612 return -EFAULT;
613 break;
614 case LPGETSTATUS:
615 lp_claim_parport_or_block (&lp_table[minor]);
616 status = r_str(minor);
617 lp_release_parport (&lp_table[minor]);
619 if (copy_to_user(argp, &status, sizeof(int)))
620 return -EFAULT;
621 break;
622 case LPRESET:
623 lp_reset(minor);
624 break;
625 #ifdef LP_STATS
626 case LPGETSTATS:
627 if (copy_to_user(argp, &LP_STAT(minor),
628 sizeof(struct lp_stats)))
629 return -EFAULT;
630 if (capable(CAP_SYS_ADMIN))
631 memset(&LP_STAT(minor), 0,
632 sizeof(struct lp_stats));
633 break;
634 #endif
635 case LPGETFLAGS:
636 status = LP_F(minor);
637 if (copy_to_user(argp, &status, sizeof(int)))
638 return -EFAULT;
639 break;
641 case LPSETTIMEOUT:
642 if (copy_from_user (&par_timeout, argp,
643 sizeof (struct timeval))) {
644 return -EFAULT;
646 /* Convert to jiffies, place in lp_table */
647 if ((par_timeout.tv_sec < 0) ||
648 (par_timeout.tv_usec < 0)) {
649 return -EINVAL;
651 to_jiffies = DIV_ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
652 to_jiffies += par_timeout.tv_sec * (long) HZ;
653 if (to_jiffies <= 0) {
654 return -EINVAL;
656 lp_table[minor].timeout = to_jiffies;
657 break;
659 default:
660 retval = -EINVAL;
662 return retval;
665 static const struct file_operations lp_fops = {
666 .owner = THIS_MODULE,
667 .write = lp_write,
668 .ioctl = lp_ioctl,
669 .open = lp_open,
670 .release = lp_release,
671 #ifdef CONFIG_PARPORT_1284
672 .read = lp_read,
673 #endif
676 /* --- support for console on the line printer ----------------- */
678 #ifdef CONFIG_LP_CONSOLE
680 #define CONSOLE_LP 0
682 /* If the printer is out of paper, we can either lose the messages or
683 * stall until the printer is happy again. Define CONSOLE_LP_STRICT
684 * non-zero to get the latter behaviour. */
685 #define CONSOLE_LP_STRICT 1
687 /* The console must be locked when we get here. */
689 static void lp_console_write (struct console *co, const char *s,
690 unsigned count)
692 struct pardevice *dev = lp_table[CONSOLE_LP].dev;
693 struct parport *port = dev->port;
694 ssize_t written;
696 if (parport_claim (dev))
697 /* Nothing we can do. */
698 return;
700 parport_set_timeout (dev, 0);
702 /* Go to compatibility mode. */
703 parport_negotiate (port, IEEE1284_MODE_COMPAT);
705 do {
706 /* Write the data, converting LF->CRLF as we go. */
707 ssize_t canwrite = count;
708 char *lf = memchr (s, '\n', count);
709 if (lf)
710 canwrite = lf - s;
712 if (canwrite > 0) {
713 written = parport_write (port, s, canwrite);
715 if (written <= 0)
716 continue;
718 s += written;
719 count -= written;
720 canwrite -= written;
723 if (lf && canwrite <= 0) {
724 const char *crlf = "\r\n";
725 int i = 2;
727 /* Dodge the original '\n', and put '\r\n' instead. */
728 s++;
729 count--;
730 do {
731 written = parport_write (port, crlf, i);
732 if (written > 0)
733 i -= written, crlf += written;
734 } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
736 } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
738 parport_release (dev);
741 static struct console lpcons = {
742 .name = "lp",
743 .write = lp_console_write,
744 .flags = CON_PRINTBUFFER,
747 #endif /* console on line printer */
749 /* --- initialisation code ------------------------------------- */
751 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
752 static char *parport[LP_NO];
753 static int reset;
755 module_param_array(parport, charp, NULL, 0);
756 module_param(reset, bool, 0);
758 #ifndef MODULE
759 static int __init lp_setup (char *str)
761 static int parport_ptr;
762 int x;
764 if (get_option(&str, &x)) {
765 if (x == 0) {
766 /* disable driver on "lp=" or "lp=0" */
767 parport_nr[0] = LP_PARPORT_OFF;
768 } else {
769 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
770 return 0;
772 } else if (!strncmp(str, "parport", 7)) {
773 int n = simple_strtoul(str+7, NULL, 10);
774 if (parport_ptr < LP_NO)
775 parport_nr[parport_ptr++] = n;
776 else
777 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
778 str);
779 } else if (!strcmp(str, "auto")) {
780 parport_nr[0] = LP_PARPORT_AUTO;
781 } else if (!strcmp(str, "none")) {
782 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
783 } else if (!strcmp(str, "reset")) {
784 reset = 1;
786 return 1;
788 #endif
790 static int lp_register(int nr, struct parport *port)
792 lp_table[nr].dev = parport_register_device(port, "lp",
793 lp_preempt, NULL, NULL, 0,
794 (void *) &lp_table[nr]);
795 if (lp_table[nr].dev == NULL)
796 return 1;
797 lp_table[nr].flags |= LP_EXIST;
799 if (reset)
800 lp_reset(nr);
802 device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), "lp%d", nr);
804 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
805 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
807 #ifdef CONFIG_LP_CONSOLE
808 if (!nr) {
809 if (port->modes & PARPORT_MODE_SAFEININT) {
810 register_console(&lpcons);
811 console_registered = port;
812 printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
813 } else
814 printk (KERN_ERR "lp%d: cannot run console on %s\n",
815 CONSOLE_LP, port->name);
817 #endif
819 return 0;
822 static void lp_attach (struct parport *port)
824 unsigned int i;
826 switch (parport_nr[0]) {
827 case LP_PARPORT_UNSPEC:
828 case LP_PARPORT_AUTO:
829 if (parport_nr[0] == LP_PARPORT_AUTO &&
830 port->probe_info[0].class != PARPORT_CLASS_PRINTER)
831 return;
832 if (lp_count == LP_NO) {
833 printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
834 return;
836 if (!lp_register(lp_count, port))
837 lp_count++;
838 break;
840 default:
841 for (i = 0; i < LP_NO; i++) {
842 if (port->number == parport_nr[i]) {
843 if (!lp_register(i, port))
844 lp_count++;
845 break;
848 break;
852 static void lp_detach (struct parport *port)
854 /* Write this some day. */
855 #ifdef CONFIG_LP_CONSOLE
856 if (console_registered == port) {
857 unregister_console(&lpcons);
858 console_registered = NULL;
860 #endif /* CONFIG_LP_CONSOLE */
863 static struct parport_driver lp_driver = {
864 .name = "lp",
865 .attach = lp_attach,
866 .detach = lp_detach,
869 static int __init lp_init (void)
871 int i, err = 0;
873 if (parport_nr[0] == LP_PARPORT_OFF)
874 return 0;
876 for (i = 0; i < LP_NO; i++) {
877 lp_table[i].dev = NULL;
878 lp_table[i].flags = 0;
879 lp_table[i].chars = LP_INIT_CHAR;
880 lp_table[i].time = LP_INIT_TIME;
881 lp_table[i].wait = LP_INIT_WAIT;
882 lp_table[i].lp_buffer = NULL;
883 #ifdef LP_STATS
884 lp_table[i].lastcall = 0;
885 lp_table[i].runchars = 0;
886 memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
887 #endif
888 lp_table[i].last_error = 0;
889 init_waitqueue_head (&lp_table[i].waitq);
890 init_waitqueue_head (&lp_table[i].dataq);
891 mutex_init(&lp_table[i].port_mutex);
892 lp_table[i].timeout = 10 * HZ;
895 if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
896 printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
897 return -EIO;
900 lp_class = class_create(THIS_MODULE, "printer");
901 if (IS_ERR(lp_class)) {
902 err = PTR_ERR(lp_class);
903 goto out_reg;
906 if (parport_register_driver (&lp_driver)) {
907 printk (KERN_ERR "lp: unable to register with parport\n");
908 err = -EIO;
909 goto out_class;
912 if (!lp_count) {
913 printk (KERN_INFO "lp: driver loaded but no devices found\n");
914 #ifndef CONFIG_PARPORT_1284
915 if (parport_nr[0] == LP_PARPORT_AUTO)
916 printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
917 #endif
920 return 0;
922 out_class:
923 class_destroy(lp_class);
924 out_reg:
925 unregister_chrdev(LP_MAJOR, "lp");
926 return err;
929 static int __init lp_init_module (void)
931 if (parport[0]) {
932 /* The user gave some parameters. Let's see what they were. */
933 if (!strncmp(parport[0], "auto", 4))
934 parport_nr[0] = LP_PARPORT_AUTO;
935 else {
936 int n;
937 for (n = 0; n < LP_NO && parport[n]; n++) {
938 if (!strncmp(parport[n], "none", 4))
939 parport_nr[n] = LP_PARPORT_NONE;
940 else {
941 char *ep;
942 unsigned long r = simple_strtoul(parport[n], &ep, 0);
943 if (ep != parport[n])
944 parport_nr[n] = r;
945 else {
946 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
947 return -ENODEV;
954 return lp_init();
957 static void lp_cleanup_module (void)
959 unsigned int offset;
961 parport_unregister_driver (&lp_driver);
963 #ifdef CONFIG_LP_CONSOLE
964 unregister_console (&lpcons);
965 #endif
967 unregister_chrdev(LP_MAJOR, "lp");
968 for (offset = 0; offset < LP_NO; offset++) {
969 if (lp_table[offset].dev == NULL)
970 continue;
971 parport_unregister_device(lp_table[offset].dev);
972 device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
974 class_destroy(lp_class);
977 __setup("lp=", lp_setup);
978 module_init(lp_init_module);
979 module_exit(lp_cleanup_module);
981 MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
982 MODULE_LICENSE("GPL");