perf stat: Fix multi-run stats
[linux-2.6/mini2440.git] / drivers / char / lp.c
blobe444c2dba160870658b8ac45910ba79b85d12419
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>
129 #include <linux/smp_lock.h>
131 #include <linux/parport.h>
132 #undef LP_STATS
133 #include <linux/lp.h>
135 #include <asm/irq.h>
136 #include <asm/uaccess.h>
137 #include <asm/system.h>
139 /* if you have more than 8 printers, remember to increase LP_NO */
140 #define LP_NO 8
142 static struct lp_struct lp_table[LP_NO];
144 static unsigned int lp_count = 0;
145 static struct class *lp_class;
147 #ifdef CONFIG_LP_CONSOLE
148 static struct parport *console_registered;
149 #endif /* CONFIG_LP_CONSOLE */
151 #undef LP_DEBUG
153 /* Bits used to manage claiming the parport device */
154 #define LP_PREEMPT_REQUEST 1
155 #define LP_PARPORT_CLAIMED 2
157 /* --- low-level port access ----------------------------------- */
159 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
160 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
161 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
162 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
164 /* Claim the parport or block trying unless we've already claimed it */
165 static void lp_claim_parport_or_block(struct lp_struct *this_lp)
167 if (!test_and_set_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
168 parport_claim_or_block (this_lp->dev);
172 /* Claim the parport or block trying unless we've already claimed it */
173 static void lp_release_parport(struct lp_struct *this_lp)
175 if (test_and_clear_bit(LP_PARPORT_CLAIMED, &this_lp->bits)) {
176 parport_release (this_lp->dev);
182 static int lp_preempt(void *handle)
184 struct lp_struct *this_lp = (struct lp_struct *)handle;
185 set_bit(LP_PREEMPT_REQUEST, &this_lp->bits);
186 return (1);
191 * Try to negotiate to a new mode; if unsuccessful negotiate to
192 * compatibility mode. Return the mode we ended up in.
194 static int lp_negotiate(struct parport * port, int mode)
196 if (parport_negotiate (port, mode) != 0) {
197 mode = IEEE1284_MODE_COMPAT;
198 parport_negotiate (port, mode);
201 return (mode);
204 static int lp_reset(int minor)
206 int retval;
207 lp_claim_parport_or_block (&lp_table[minor]);
208 w_ctr(minor, LP_PSELECP);
209 udelay (LP_DELAY);
210 w_ctr(minor, LP_PSELECP | LP_PINITP);
211 retval = r_str(minor);
212 lp_release_parport (&lp_table[minor]);
213 return retval;
216 static void lp_error (int minor)
218 DEFINE_WAIT(wait);
219 int polling;
221 if (LP_F(minor) & LP_ABORT)
222 return;
224 polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
225 if (polling) lp_release_parport (&lp_table[minor]);
226 prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
227 schedule_timeout(LP_TIMEOUT_POLLED);
228 finish_wait(&lp_table[minor].waitq, &wait);
229 if (polling) lp_claim_parport_or_block (&lp_table[minor]);
230 else parport_yield_blocking (lp_table[minor].dev);
233 static int lp_check_status(int minor)
235 int error = 0;
236 unsigned int last = lp_table[minor].last_error;
237 unsigned char status = r_str(minor);
238 if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
239 /* No error. */
240 last = 0;
241 else if ((status & LP_POUTPA)) {
242 if (last != LP_POUTPA) {
243 last = LP_POUTPA;
244 printk(KERN_INFO "lp%d out of paper\n", minor);
246 error = -ENOSPC;
247 } else if (!(status & LP_PSELECD)) {
248 if (last != LP_PSELECD) {
249 last = LP_PSELECD;
250 printk(KERN_INFO "lp%d off-line\n", minor);
252 error = -EIO;
253 } else if (!(status & LP_PERRORP)) {
254 if (last != LP_PERRORP) {
255 last = LP_PERRORP;
256 printk(KERN_INFO "lp%d on fire\n", minor);
258 error = -EIO;
259 } else {
260 last = 0; /* Come here if LP_CAREFUL is set and no
261 errors are reported. */
264 lp_table[minor].last_error = last;
266 if (last != 0)
267 lp_error(minor);
269 return error;
272 static int lp_wait_ready(int minor, int nonblock)
274 int error = 0;
276 /* If we're not in compatibility mode, we're ready now! */
277 if (lp_table[minor].current_mode != IEEE1284_MODE_COMPAT) {
278 return (0);
281 do {
282 error = lp_check_status (minor);
283 if (error && (nonblock || (LP_F(minor) & LP_ABORT)))
284 break;
285 if (signal_pending (current)) {
286 error = -EINTR;
287 break;
289 } while (error);
290 return error;
293 static ssize_t lp_write(struct file * file, const char __user * buf,
294 size_t count, loff_t *ppos)
296 unsigned int minor = iminor(file->f_path.dentry->d_inode);
297 struct parport *port = lp_table[minor].dev->port;
298 char *kbuf = lp_table[minor].lp_buffer;
299 ssize_t retv = 0;
300 ssize_t written;
301 size_t copy_size = count;
302 int nonblock = ((file->f_flags & O_NONBLOCK) ||
303 (LP_F(minor) & LP_ABORT));
305 #ifdef LP_STATS
306 if (time_after(jiffies, lp_table[minor].lastcall + LP_TIME(minor)))
307 lp_table[minor].runchars = 0;
309 lp_table[minor].lastcall = jiffies;
310 #endif
312 /* Need to copy the data from user-space. */
313 if (copy_size > LP_BUFFER_SIZE)
314 copy_size = LP_BUFFER_SIZE;
316 if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
317 return -EINTR;
319 if (copy_from_user (kbuf, buf, copy_size)) {
320 retv = -EFAULT;
321 goto out_unlock;
324 /* Claim Parport or sleep until it becomes available
326 lp_claim_parport_or_block (&lp_table[minor]);
327 /* Go to the proper mode. */
328 lp_table[minor].current_mode = lp_negotiate (port,
329 lp_table[minor].best_mode);
331 parport_set_timeout (lp_table[minor].dev,
332 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
333 : lp_table[minor].timeout));
335 if ((retv = lp_wait_ready (minor, nonblock)) == 0)
336 do {
337 /* Write the data. */
338 written = parport_write (port, kbuf, copy_size);
339 if (written > 0) {
340 copy_size -= written;
341 count -= written;
342 buf += written;
343 retv += written;
346 if (signal_pending (current)) {
347 if (retv == 0)
348 retv = -EINTR;
350 break;
353 if (copy_size > 0) {
354 /* incomplete write -> check error ! */
355 int error;
357 parport_negotiate (lp_table[minor].dev->port,
358 IEEE1284_MODE_COMPAT);
359 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
361 error = lp_wait_ready (minor, nonblock);
363 if (error) {
364 if (retv == 0)
365 retv = error;
366 break;
367 } else if (nonblock) {
368 if (retv == 0)
369 retv = -EAGAIN;
370 break;
373 parport_yield_blocking (lp_table[minor].dev);
374 lp_table[minor].current_mode
375 = lp_negotiate (port,
376 lp_table[minor].best_mode);
378 } else if (need_resched())
379 schedule ();
381 if (count) {
382 copy_size = count;
383 if (copy_size > LP_BUFFER_SIZE)
384 copy_size = LP_BUFFER_SIZE;
386 if (copy_from_user(kbuf, buf, copy_size)) {
387 if (retv == 0)
388 retv = -EFAULT;
389 break;
392 } while (count > 0);
394 if (test_and_clear_bit(LP_PREEMPT_REQUEST,
395 &lp_table[minor].bits)) {
396 printk(KERN_INFO "lp%d releasing parport\n", minor);
397 parport_negotiate (lp_table[minor].dev->port,
398 IEEE1284_MODE_COMPAT);
399 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
400 lp_release_parport (&lp_table[minor]);
402 out_unlock:
403 mutex_unlock(&lp_table[minor].port_mutex);
405 return retv;
408 #ifdef CONFIG_PARPORT_1284
410 /* Status readback conforming to ieee1284 */
411 static ssize_t lp_read(struct file * file, char __user * buf,
412 size_t count, loff_t *ppos)
414 DEFINE_WAIT(wait);
415 unsigned int minor=iminor(file->f_path.dentry->d_inode);
416 struct parport *port = lp_table[minor].dev->port;
417 ssize_t retval = 0;
418 char *kbuf = lp_table[minor].lp_buffer;
419 int nonblock = ((file->f_flags & O_NONBLOCK) ||
420 (LP_F(minor) & LP_ABORT));
422 if (count > LP_BUFFER_SIZE)
423 count = LP_BUFFER_SIZE;
425 if (mutex_lock_interruptible(&lp_table[minor].port_mutex))
426 return -EINTR;
428 lp_claim_parport_or_block (&lp_table[minor]);
430 parport_set_timeout (lp_table[minor].dev,
431 (nonblock ? PARPORT_INACTIVITY_O_NONBLOCK
432 : lp_table[minor].timeout));
434 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
435 if (parport_negotiate (lp_table[minor].dev->port,
436 IEEE1284_MODE_NIBBLE)) {
437 retval = -EIO;
438 goto out;
441 while (retval == 0) {
442 retval = parport_read (port, kbuf, count);
444 if (retval > 0)
445 break;
447 if (nonblock) {
448 retval = -EAGAIN;
449 break;
452 /* Wait for data. */
454 if (lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE) {
455 parport_negotiate (lp_table[minor].dev->port,
456 IEEE1284_MODE_COMPAT);
457 lp_error (minor);
458 if (parport_negotiate (lp_table[minor].dev->port,
459 IEEE1284_MODE_NIBBLE)) {
460 retval = -EIO;
461 goto out;
463 } else {
464 prepare_to_wait(&lp_table[minor].waitq, &wait, TASK_INTERRUPTIBLE);
465 schedule_timeout(LP_TIMEOUT_POLLED);
466 finish_wait(&lp_table[minor].waitq, &wait);
469 if (signal_pending (current)) {
470 retval = -ERESTARTSYS;
471 break;
474 cond_resched ();
476 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
477 out:
478 lp_release_parport (&lp_table[minor]);
480 if (retval > 0 && copy_to_user (buf, kbuf, retval))
481 retval = -EFAULT;
483 mutex_unlock(&lp_table[minor].port_mutex);
485 return retval;
488 #endif /* IEEE 1284 support */
490 static int lp_open(struct inode * inode, struct file * file)
492 unsigned int minor = iminor(inode);
493 int ret = 0;
495 lock_kernel();
496 if (minor >= LP_NO) {
497 ret = -ENXIO;
498 goto out;
500 if ((LP_F(minor) & LP_EXIST) == 0) {
501 ret = -ENXIO;
502 goto out;
504 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor))) {
505 ret = -EBUSY;
506 goto out;
508 /* If ABORTOPEN is set and the printer is offline or out of paper,
509 we may still want to open it to perform ioctl()s. Therefore we
510 have commandeered O_NONBLOCK, even though it is being used in
511 a non-standard manner. This is strictly a Linux hack, and
512 should most likely only ever be used by the tunelp application. */
513 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
514 int status;
515 lp_claim_parport_or_block (&lp_table[minor]);
516 status = r_str(minor);
517 lp_release_parport (&lp_table[minor]);
518 if (status & LP_POUTPA) {
519 printk(KERN_INFO "lp%d out of paper\n", minor);
520 LP_F(minor) &= ~LP_BUSY;
521 ret = -ENOSPC;
522 goto out;
523 } else if (!(status & LP_PSELECD)) {
524 printk(KERN_INFO "lp%d off-line\n", minor);
525 LP_F(minor) &= ~LP_BUSY;
526 ret = -EIO;
527 goto out;
528 } else if (!(status & LP_PERRORP)) {
529 printk(KERN_ERR "lp%d printer error\n", minor);
530 LP_F(minor) &= ~LP_BUSY;
531 ret = -EIO;
532 goto out;
535 lp_table[minor].lp_buffer = kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
536 if (!lp_table[minor].lp_buffer) {
537 LP_F(minor) &= ~LP_BUSY;
538 ret = -ENOMEM;
539 goto out;
541 /* Determine if the peripheral supports ECP mode */
542 lp_claim_parport_or_block (&lp_table[minor]);
543 if ( (lp_table[minor].dev->port->modes & PARPORT_MODE_ECP) &&
544 !parport_negotiate (lp_table[minor].dev->port,
545 IEEE1284_MODE_ECP)) {
546 printk (KERN_INFO "lp%d: ECP mode\n", minor);
547 lp_table[minor].best_mode = IEEE1284_MODE_ECP;
548 } else {
549 lp_table[minor].best_mode = IEEE1284_MODE_COMPAT;
551 /* Leave peripheral in compatibility mode */
552 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
553 lp_release_parport (&lp_table[minor]);
554 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
555 out:
556 unlock_kernel();
557 return ret;
560 static int lp_release(struct inode * inode, struct file * file)
562 unsigned int minor = iminor(inode);
564 lp_claim_parport_or_block (&lp_table[minor]);
565 parport_negotiate (lp_table[minor].dev->port, IEEE1284_MODE_COMPAT);
566 lp_table[minor].current_mode = IEEE1284_MODE_COMPAT;
567 lp_release_parport (&lp_table[minor]);
568 kfree(lp_table[minor].lp_buffer);
569 lp_table[minor].lp_buffer = NULL;
570 LP_F(minor) &= ~LP_BUSY;
571 return 0;
574 static int lp_ioctl(struct inode *inode, struct file *file,
575 unsigned int cmd, unsigned long arg)
577 unsigned int minor = iminor(inode);
578 int status;
579 int retval = 0;
580 void __user *argp = (void __user *)arg;
582 #ifdef LP_DEBUG
583 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
584 #endif
585 if (minor >= LP_NO)
586 return -ENODEV;
587 if ((LP_F(minor) & LP_EXIST) == 0)
588 return -ENODEV;
589 switch ( cmd ) {
590 struct timeval par_timeout;
591 long to_jiffies;
593 case LPTIME:
594 LP_TIME(minor) = arg * HZ/100;
595 break;
596 case LPCHAR:
597 LP_CHAR(minor) = arg;
598 break;
599 case LPABORT:
600 if (arg)
601 LP_F(minor) |= LP_ABORT;
602 else
603 LP_F(minor) &= ~LP_ABORT;
604 break;
605 case LPABORTOPEN:
606 if (arg)
607 LP_F(minor) |= LP_ABORTOPEN;
608 else
609 LP_F(minor) &= ~LP_ABORTOPEN;
610 break;
611 case LPCAREFUL:
612 if (arg)
613 LP_F(minor) |= LP_CAREFUL;
614 else
615 LP_F(minor) &= ~LP_CAREFUL;
616 break;
617 case LPWAIT:
618 LP_WAIT(minor) = arg;
619 break;
620 case LPSETIRQ:
621 return -EINVAL;
622 break;
623 case LPGETIRQ:
624 if (copy_to_user(argp, &LP_IRQ(minor),
625 sizeof(int)))
626 return -EFAULT;
627 break;
628 case LPGETSTATUS:
629 lp_claim_parport_or_block (&lp_table[minor]);
630 status = r_str(minor);
631 lp_release_parport (&lp_table[minor]);
633 if (copy_to_user(argp, &status, sizeof(int)))
634 return -EFAULT;
635 break;
636 case LPRESET:
637 lp_reset(minor);
638 break;
639 #ifdef LP_STATS
640 case LPGETSTATS:
641 if (copy_to_user(argp, &LP_STAT(minor),
642 sizeof(struct lp_stats)))
643 return -EFAULT;
644 if (capable(CAP_SYS_ADMIN))
645 memset(&LP_STAT(minor), 0,
646 sizeof(struct lp_stats));
647 break;
648 #endif
649 case LPGETFLAGS:
650 status = LP_F(minor);
651 if (copy_to_user(argp, &status, sizeof(int)))
652 return -EFAULT;
653 break;
655 case LPSETTIMEOUT:
656 if (copy_from_user (&par_timeout, argp,
657 sizeof (struct timeval))) {
658 return -EFAULT;
660 /* Convert to jiffies, place in lp_table */
661 if ((par_timeout.tv_sec < 0) ||
662 (par_timeout.tv_usec < 0)) {
663 return -EINVAL;
665 to_jiffies = DIV_ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
666 to_jiffies += par_timeout.tv_sec * (long) HZ;
667 if (to_jiffies <= 0) {
668 return -EINVAL;
670 lp_table[minor].timeout = to_jiffies;
671 break;
673 default:
674 retval = -EINVAL;
676 return retval;
679 static const struct file_operations lp_fops = {
680 .owner = THIS_MODULE,
681 .write = lp_write,
682 .ioctl = lp_ioctl,
683 .open = lp_open,
684 .release = lp_release,
685 #ifdef CONFIG_PARPORT_1284
686 .read = lp_read,
687 #endif
690 /* --- support for console on the line printer ----------------- */
692 #ifdef CONFIG_LP_CONSOLE
694 #define CONSOLE_LP 0
696 /* If the printer is out of paper, we can either lose the messages or
697 * stall until the printer is happy again. Define CONSOLE_LP_STRICT
698 * non-zero to get the latter behaviour. */
699 #define CONSOLE_LP_STRICT 1
701 /* The console must be locked when we get here. */
703 static void lp_console_write (struct console *co, const char *s,
704 unsigned count)
706 struct pardevice *dev = lp_table[CONSOLE_LP].dev;
707 struct parport *port = dev->port;
708 ssize_t written;
710 if (parport_claim (dev))
711 /* Nothing we can do. */
712 return;
714 parport_set_timeout (dev, 0);
716 /* Go to compatibility mode. */
717 parport_negotiate (port, IEEE1284_MODE_COMPAT);
719 do {
720 /* Write the data, converting LF->CRLF as we go. */
721 ssize_t canwrite = count;
722 char *lf = memchr (s, '\n', count);
723 if (lf)
724 canwrite = lf - s;
726 if (canwrite > 0) {
727 written = parport_write (port, s, canwrite);
729 if (written <= 0)
730 continue;
732 s += written;
733 count -= written;
734 canwrite -= written;
737 if (lf && canwrite <= 0) {
738 const char *crlf = "\r\n";
739 int i = 2;
741 /* Dodge the original '\n', and put '\r\n' instead. */
742 s++;
743 count--;
744 do {
745 written = parport_write (port, crlf, i);
746 if (written > 0)
747 i -= written, crlf += written;
748 } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
750 } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
752 parport_release (dev);
755 static struct console lpcons = {
756 .name = "lp",
757 .write = lp_console_write,
758 .flags = CON_PRINTBUFFER,
761 #endif /* console on line printer */
763 /* --- initialisation code ------------------------------------- */
765 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
766 static char *parport[LP_NO];
767 static int reset;
769 module_param_array(parport, charp, NULL, 0);
770 module_param(reset, bool, 0);
772 #ifndef MODULE
773 static int __init lp_setup (char *str)
775 static int parport_ptr;
776 int x;
778 if (get_option(&str, &x)) {
779 if (x == 0) {
780 /* disable driver on "lp=" or "lp=0" */
781 parport_nr[0] = LP_PARPORT_OFF;
782 } else {
783 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", x);
784 return 0;
786 } else if (!strncmp(str, "parport", 7)) {
787 int n = simple_strtoul(str+7, NULL, 10);
788 if (parport_ptr < LP_NO)
789 parport_nr[parport_ptr++] = n;
790 else
791 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
792 str);
793 } else if (!strcmp(str, "auto")) {
794 parport_nr[0] = LP_PARPORT_AUTO;
795 } else if (!strcmp(str, "none")) {
796 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
797 } else if (!strcmp(str, "reset")) {
798 reset = 1;
800 return 1;
802 #endif
804 static int lp_register(int nr, struct parport *port)
806 lp_table[nr].dev = parport_register_device(port, "lp",
807 lp_preempt, NULL, NULL, 0,
808 (void *) &lp_table[nr]);
809 if (lp_table[nr].dev == NULL)
810 return 1;
811 lp_table[nr].flags |= LP_EXIST;
813 if (reset)
814 lp_reset(nr);
816 device_create(lp_class, port->dev, MKDEV(LP_MAJOR, nr), NULL,
817 "lp%d", nr);
819 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
820 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
822 #ifdef CONFIG_LP_CONSOLE
823 if (!nr) {
824 if (port->modes & PARPORT_MODE_SAFEININT) {
825 register_console(&lpcons);
826 console_registered = port;
827 printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
828 } else
829 printk (KERN_ERR "lp%d: cannot run console on %s\n",
830 CONSOLE_LP, port->name);
832 #endif
834 return 0;
837 static void lp_attach (struct parport *port)
839 unsigned int i;
841 switch (parport_nr[0]) {
842 case LP_PARPORT_UNSPEC:
843 case LP_PARPORT_AUTO:
844 if (parport_nr[0] == LP_PARPORT_AUTO &&
845 port->probe_info[0].class != PARPORT_CLASS_PRINTER)
846 return;
847 if (lp_count == LP_NO) {
848 printk(KERN_INFO "lp: ignoring parallel port (max. %d)\n",LP_NO);
849 return;
851 if (!lp_register(lp_count, port))
852 lp_count++;
853 break;
855 default:
856 for (i = 0; i < LP_NO; i++) {
857 if (port->number == parport_nr[i]) {
858 if (!lp_register(i, port))
859 lp_count++;
860 break;
863 break;
867 static void lp_detach (struct parport *port)
869 /* Write this some day. */
870 #ifdef CONFIG_LP_CONSOLE
871 if (console_registered == port) {
872 unregister_console(&lpcons);
873 console_registered = NULL;
875 #endif /* CONFIG_LP_CONSOLE */
878 static struct parport_driver lp_driver = {
879 .name = "lp",
880 .attach = lp_attach,
881 .detach = lp_detach,
884 static int __init lp_init (void)
886 int i, err = 0;
888 if (parport_nr[0] == LP_PARPORT_OFF)
889 return 0;
891 for (i = 0; i < LP_NO; i++) {
892 lp_table[i].dev = NULL;
893 lp_table[i].flags = 0;
894 lp_table[i].chars = LP_INIT_CHAR;
895 lp_table[i].time = LP_INIT_TIME;
896 lp_table[i].wait = LP_INIT_WAIT;
897 lp_table[i].lp_buffer = NULL;
898 #ifdef LP_STATS
899 lp_table[i].lastcall = 0;
900 lp_table[i].runchars = 0;
901 memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
902 #endif
903 lp_table[i].last_error = 0;
904 init_waitqueue_head (&lp_table[i].waitq);
905 init_waitqueue_head (&lp_table[i].dataq);
906 mutex_init(&lp_table[i].port_mutex);
907 lp_table[i].timeout = 10 * HZ;
910 if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
911 printk (KERN_ERR "lp: unable to get major %d\n", LP_MAJOR);
912 return -EIO;
915 lp_class = class_create(THIS_MODULE, "printer");
916 if (IS_ERR(lp_class)) {
917 err = PTR_ERR(lp_class);
918 goto out_reg;
921 if (parport_register_driver (&lp_driver)) {
922 printk (KERN_ERR "lp: unable to register with parport\n");
923 err = -EIO;
924 goto out_class;
927 if (!lp_count) {
928 printk (KERN_INFO "lp: driver loaded but no devices found\n");
929 #ifndef CONFIG_PARPORT_1284
930 if (parport_nr[0] == LP_PARPORT_AUTO)
931 printk (KERN_INFO "lp: (is IEEE 1284 support enabled?)\n");
932 #endif
935 return 0;
937 out_class:
938 class_destroy(lp_class);
939 out_reg:
940 unregister_chrdev(LP_MAJOR, "lp");
941 return err;
944 static int __init lp_init_module (void)
946 if (parport[0]) {
947 /* The user gave some parameters. Let's see what they were. */
948 if (!strncmp(parport[0], "auto", 4))
949 parport_nr[0] = LP_PARPORT_AUTO;
950 else {
951 int n;
952 for (n = 0; n < LP_NO && parport[n]; n++) {
953 if (!strncmp(parport[n], "none", 4))
954 parport_nr[n] = LP_PARPORT_NONE;
955 else {
956 char *ep;
957 unsigned long r = simple_strtoul(parport[n], &ep, 0);
958 if (ep != parport[n])
959 parport_nr[n] = r;
960 else {
961 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
962 return -ENODEV;
969 return lp_init();
972 static void lp_cleanup_module (void)
974 unsigned int offset;
976 parport_unregister_driver (&lp_driver);
978 #ifdef CONFIG_LP_CONSOLE
979 unregister_console (&lpcons);
980 #endif
982 unregister_chrdev(LP_MAJOR, "lp");
983 for (offset = 0; offset < LP_NO; offset++) {
984 if (lp_table[offset].dev == NULL)
985 continue;
986 parport_unregister_device(lp_table[offset].dev);
987 device_destroy(lp_class, MKDEV(LP_MAJOR, offset));
989 class_destroy(lp_class);
992 __setup("lp=", lp_setup);
993 module_init(lp_init_module);
994 module_exit(lp_cleanup_module);
996 MODULE_ALIAS_CHARDEV_MAJOR(LP_MAJOR);
997 MODULE_LICENSE("GPL");