Import 2.3.46pre3
[davej-history.git] / drivers / char / lp.c
blob72377382f7b1ab287df28b863a7c5763bb3315a5
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 <Philip.Blundell@pobox.com>
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 * Redesigned interrupt handling for handle printers with buggy handshake
20 * by Andrea Arcangeli, 11 May 1998
21 * Full efficient handling of printer with buggy irq handshake (now I have
22 * understood the meaning of the strange handshake). This is done sending new
23 * characters if the interrupt is just happened, even if the printer say to
24 * be still BUSY. This is needed at least with Epson Stylus Color. To enable
25 * the new TRUST_IRQ mode read the `LP OPTIMIZATION' section below...
26 * Fixed the irq on the rising edge of the strobe case.
27 * Obsoleted the CAREFUL flag since a printer that doesn' t work with
28 * CAREFUL will block a bit after in lp_check_status().
29 * Andrea Arcangeli, 15 Oct 1998
30 * Obsoleted and removed all the lowlevel stuff implemented in the last
31 * month to use the IEEE1284 functions (that handle the _new_ compatibilty
32 * mode fine).
35 /* This driver should, in theory, work with any parallel port that has an
36 * appropriate low-level driver; all I/O is done through the parport
37 * abstraction layer.
39 * If this driver is built into the kernel, you can configure it using the
40 * kernel command-line. For example:
42 * lp=parport1,none,parport2 (bind lp0 to parport1, disable lp1 and
43 * bind lp2 to parport2)
45 * lp=auto (assign lp devices to all ports that
46 * have printers attached, as determined
47 * by the IEEE-1284 autoprobe)
49 * lp=reset (reset the printer during
50 * initialisation)
52 * lp=off (disable the printer driver entirely)
54 * If the driver is loaded as a module, similar functionality is available
55 * using module parameters. The equivalent of the above commands would be:
57 * # insmod lp.o parport=1,none,2
59 * # insmod lp.o parport=auto
61 * # insmod lp.o reset=1
64 /* COMPATIBILITY WITH OLD KERNELS
66 * Under Linux 2.0 and previous versions, lp devices were bound to ports at
67 * particular I/O addresses, as follows:
69 * lp0 0x3bc
70 * lp1 0x378
71 * lp2 0x278
73 * The new driver, by default, binds lp devices to parport devices as it
74 * finds them. This means that if you only have one port, it will be bound
75 * to lp0 regardless of its I/O address. If you need the old behaviour, you
76 * can force it using the parameters described above.
80 * The new interrupt handling code take care of the buggy handshake
81 * of some HP and Epson printer:
82 * ___
83 * ACK _______________ ___________
84 * |__|
85 * ____
86 * BUSY _________ _______
87 * |____________|
89 * I discovered this using the printer scanner that you can find at:
91 * ftp://e-mind.com/pub/linux/pscan/
93 * 11 May 98, Andrea Arcangeli
95 * My printer scanner run on an Epson Stylus Color show that such printer
96 * generates the irq on the _rising_ edge of the STROBE. Now lp handle
97 * this case fine too.
99 * 15 Oct 1998, Andrea Arcangeli
101 * The so called `buggy' handshake is really the well documented
102 * compatibility mode IEEE1284 handshake. They changed the well known
103 * Centronics handshake acking in the middle of busy expecting to not
104 * break drivers or legacy application, while they broken linux lp
105 * until I fixed it reverse engineering the protocol by hand some
106 * month ago...
108 * 14 Dec 1998, Andrea Arcangeli
110 * Copyright (C) 2000 by Tim Waugh (added LPSETTIMEOUT ioctl)
113 #include <linux/module.h>
114 #include <linux/init.h>
116 #include <linux/config.h>
117 #include <linux/errno.h>
118 #include <linux/kernel.h>
119 #include <linux/major.h>
120 #include <linux/sched.h>
121 #include <linux/malloc.h>
122 #include <linux/fcntl.h>
123 #include <linux/delay.h>
124 #include <linux/poll.h>
125 #include <linux/console.h>
127 #include <linux/parport.h>
128 #undef LP_STATS
129 #include <linux/lp.h>
131 #include <asm/irq.h>
132 #include <asm/uaccess.h>
133 #include <asm/system.h>
135 /* if you have more than 3 printers, remember to increase LP_NO */
136 #define LP_NO 3
138 /* ROUND_UP macro from fs/select.c */
139 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
141 struct lp_struct lp_table[LP_NO];
143 static unsigned int lp_count = 0;
145 /* Test if printer is ready */
146 #define LP_READY(status) ((status) & LP_PBUSY)
147 /* Test if the printer is not acking the strobe */
148 #define LP_NO_ACKING(status) ((status) & LP_PACK)
149 /* Test if the printer has error conditions */
150 #define LP_NO_ERROR(status) ((status) & LP_PERRORP)
152 #undef LP_DEBUG
154 /* If you want to see if you can get lp_poll working, define this. */
155 #undef SUPPORT_POLL
157 /* --- parport support ----------------------------------------- */
159 static int lp_preempt(void *handle)
161 struct lp_struct *lps = (struct lp_struct *)handle;
163 if (!(lps->flags & LP_PORT_BUSY)) {
164 /* Let the port go. */
165 clear_bit (LP_HAVE_PORT_BIT, &lps->flags);
166 return 0;
169 /* Don't actually release the port now */
170 return 1;
173 static void lp_check_data (struct lp_struct *lp)
175 #if !defined(CONFIG_PARPORT_1284) || !defined (SUPPORT_POLL)
176 return;
177 #else
178 struct pardevice *dev = lp->dev;
179 if (!(lp->flags & LP_NO_REVERSE)) {
180 int err = parport_negotiate (dev->port, IEEE1284_MODE_NIBBLE);
181 if (err)
182 lp->flags |= LP_NO_REVERSE;
183 else {
184 unsigned char s = parport_read_status (dev->port);
185 if (s & PARPORT_STATUS_ERROR)
186 lp->flags &= ~LP_DATA_AVAIL;
187 else {
188 lp->flags |= LP_DATA_AVAIL;
189 if (waitqueue_active (&lp->dataq))
190 wake_up_interruptible (&lp->dataq);
194 #endif /* IEEE 1284 support */
197 static void lp_parport_release (int minor)
199 lp_check_data (&lp_table[minor]);
200 if (test_and_clear_bit (LP_HAVE_PORT_BIT, &lp_table[minor].flags))
201 parport_release (lp_table[minor].dev);
203 lp_table[minor].flags &= ~LP_PORT_BUSY;
206 static void lp_parport_claim (int minor)
208 if (!test_and_set_bit (LP_HAVE_PORT_BIT, &lp_table[minor].flags))
209 parport_claim_or_block (lp_table[minor].dev);
211 lp_table[minor].flags |= LP_PORT_BUSY;
214 /* --- low-level port access ----------------------------------- */
216 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
217 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
218 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
219 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
221 static int lp_reset(int minor)
223 int retval;
224 lp_parport_claim (minor);
225 w_ctr(minor, LP_PSELECP);
226 udelay (LP_DELAY);
227 w_ctr(minor, LP_PSELECP | LP_PINITP);
228 retval = r_str(minor);
229 lp_parport_release (minor);
230 return retval;
233 static void lp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
235 struct lp_struct *lp_dev = (struct lp_struct *) dev_id;
236 if (!(lp_dev->flags & LP_PORT_BUSY))
237 /* We must have the port since we got an interrupt. */
238 lp_check_data (lp_dev);
239 if (waitqueue_active (&lp_dev->waitq))
240 wake_up_interruptible (&lp_dev->waitq);
243 static void lp_wakeup (void *handle)
245 struct lp_struct *lp_dev = handle;
247 if (lp_dev->flags & LP_PORT_BUSY)
248 return;
250 /* Grab the port if it can help (i.e. reverse mode is possible). */
251 if (!(lp_dev->flags & LP_NO_REVERSE)) {
252 parport_claim (lp_dev->dev);
253 set_bit (LP_HAVE_PORT_BIT, &lp_dev->flags);
254 lp_check_data (lp_dev);
255 if (waitqueue_active (&lp_dev->waitq))
256 wake_up_interruptible (&lp_dev->waitq);
260 static void lp_error (int minor)
262 int polling;
264 if (LP_F(minor) & LP_ABORT)
265 return;
267 polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
268 if (polling) lp_parport_release (minor);
269 interruptible_sleep_on_timeout (&lp_table[minor].waitq,
270 LP_TIMEOUT_POLLED);
271 if (polling) lp_parport_claim (minor);
272 else parport_yield_blocking (lp_table[minor].dev);
275 static int lp_check_status(int minor)
277 int error = 0;
278 unsigned int last = lp_table[minor].last_error;
279 unsigned char status = r_str(minor);
280 if ((status & LP_PERRORP) && !(LP_F(minor) & LP_CAREFUL))
281 /* No error. */
282 last = 0;
283 else if ((status & LP_POUTPA)) {
284 if (last != LP_POUTPA) {
285 last = LP_POUTPA;
286 printk(KERN_INFO "lp%d out of paper\n", minor);
288 error = -ENOSPC;
289 } else if (!(status & LP_PSELECD)) {
290 if (last != LP_PSELECD) {
291 last = LP_PSELECD;
292 printk(KERN_INFO "lp%d off-line\n", minor);
294 error = -EIO;
295 } else if (!(status & LP_PERRORP)) {
296 if (last != LP_PERRORP) {
297 last = LP_PERRORP;
298 printk(KERN_INFO "lp%d on fire\n", minor);
300 error = -EIO;
301 } else {
302 last = 0; /* Come here if LP_CAREFUL is set and no
303 errors are reported. */
306 lp_table[minor].last_error = last;
308 if (last != 0)
309 lp_error(minor);
311 return error;
314 static ssize_t lp_write(struct file * file, const char * buf,
315 size_t count, loff_t *ppos)
317 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
318 struct parport *port = lp_table[minor].dev->port;
319 char *kbuf = lp_table[minor].lp_buffer;
320 ssize_t retv = 0;
321 ssize_t written;
322 size_t copy_size = count;
323 long old_to;
325 #ifdef LP_STATS
326 if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
327 lp_table[minor].runchars = 0;
329 lp_table[minor].lastcall = jiffies;
330 #endif
332 /* Need to copy the data from user-space. */
333 if (copy_size > LP_BUFFER_SIZE)
334 copy_size = LP_BUFFER_SIZE;
336 if (copy_from_user (kbuf, buf, copy_size))
337 return -EFAULT;
339 if (down_interruptible (&lp_table[minor].port_mutex))
340 return -EINTR;
342 /* Claim Parport or sleep until it becomes available
344 lp_parport_claim (minor);
346 /* Go to compatibility mode. */
347 parport_negotiate (port, IEEE1284_MODE_COMPAT);
349 old_to = parport_set_timeout (lp_table[minor].dev,
350 lp_table[minor].timeout);
352 do {
353 /* Write the data. */
354 written = parport_write (port, kbuf, copy_size);
355 if (written >= 0) {
356 copy_size -= written;
357 count -= written;
358 buf += written;
359 retv += written;
362 if (signal_pending (current)) {
363 if (retv == 0)
364 retv = -EINTR;
366 break;
369 if (copy_size > 0) {
370 /* incomplete write -> check error ! */
371 int error = lp_check_status (minor);
373 if (LP_F(minor) & LP_ABORT) {
374 if (retv == 0)
375 retv = error;
376 break;
379 parport_yield_blocking (lp_table[minor].dev);
380 } else if (current->need_resched)
381 schedule ();
383 if (count) {
384 copy_size = count;
385 if (copy_size > LP_BUFFER_SIZE)
386 copy_size = LP_BUFFER_SIZE;
388 if (copy_from_user(kbuf, buf, copy_size)) {
389 if (retv == 0)
390 retv = -EFAULT;
391 break;
394 } while (count > 0);
396 /* Not really necessary, but polite. */
397 parport_set_timeout (lp_table[minor].dev, old_to);
399 lp_parport_release (minor);
401 up (&lp_table[minor].port_mutex);
403 return retv;
406 #ifdef CONFIG_PARPORT_1284
408 /* Status readback conforming to ieee1284 */
409 static ssize_t lp_read(struct file * file, char * buf,
410 size_t count, loff_t *ppos)
412 unsigned int minor=MINOR(file->f_dentry->d_inode->i_rdev);
413 struct parport *port = lp_table[minor].dev->port;
414 ssize_t retval = 0;
415 char *kbuf = lp_table[minor].lp_buffer;
417 if (count > LP_BUFFER_SIZE)
418 count = LP_BUFFER_SIZE;
420 if (down_interruptible (&lp_table[minor].port_mutex))
421 return -EINTR;
423 lp_parport_claim (minor);
425 for (;;) {
426 retval = parport_read (port, kbuf, count);
428 if (retval)
429 break;
431 if (file->f_flags & O_NONBLOCK)
432 break;
434 /* Wait for an interrupt. */
435 interruptible_sleep_on_timeout (&lp_table[minor].waitq,
436 LP_TIMEOUT_POLLED);
438 if (signal_pending (current)) {
439 retval = -EINTR;
440 break;
444 lp_parport_release (minor);
446 if (retval > 0 && copy_to_user (buf, kbuf, retval))
447 retval = -EFAULT;
449 up (&lp_table[minor].port_mutex);
451 return retval;
454 #endif /* IEEE 1284 support */
456 static int lp_open(struct inode * inode, struct file * file)
458 unsigned int minor = MINOR(inode->i_rdev);
460 if (minor >= LP_NO)
461 return -ENXIO;
462 if ((LP_F(minor) & LP_EXIST) == 0)
463 return -ENXIO;
464 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
465 return -EBUSY;
467 MOD_INC_USE_COUNT;
469 /* If ABORTOPEN is set and the printer is offline or out of paper,
470 we may still want to open it to perform ioctl()s. Therefore we
471 have commandeered O_NONBLOCK, even though it is being used in
472 a non-standard manner. This is strictly a Linux hack, and
473 should most likely only ever be used by the tunelp application. */
474 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
475 int status;
476 lp_parport_claim (minor);
477 status = r_str(minor);
478 lp_parport_release (minor);
479 if (status & LP_POUTPA) {
480 printk(KERN_INFO "lp%d out of paper\n", minor);
481 MOD_DEC_USE_COUNT;
482 LP_F(minor) &= ~LP_BUSY;
483 return -ENOSPC;
484 } else if (!(status & LP_PSELECD)) {
485 printk(KERN_INFO "lp%d off-line\n", minor);
486 MOD_DEC_USE_COUNT;
487 LP_F(minor) &= ~LP_BUSY;
488 return -EIO;
489 } else if (!(status & LP_PERRORP)) {
490 printk(KERN_ERR "lp%d printer error\n", minor);
491 MOD_DEC_USE_COUNT;
492 LP_F(minor) &= ~LP_BUSY;
493 return -EIO;
496 lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
497 if (!lp_table[minor].lp_buffer) {
498 MOD_DEC_USE_COUNT;
499 LP_F(minor) &= ~LP_BUSY;
500 return -ENOMEM;
502 return 0;
505 static int lp_release(struct inode * inode, struct file * file)
507 unsigned int minor = MINOR(inode->i_rdev);
509 kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
510 lp_table[minor].lp_buffer = NULL;
511 MOD_DEC_USE_COUNT;
512 LP_F(minor) &= ~LP_BUSY;
513 return 0;
516 static int lp_ioctl(struct inode *inode, struct file *file,
517 unsigned int cmd, unsigned long arg)
519 unsigned int minor = MINOR(inode->i_rdev);
520 int status;
521 int retval = 0;
523 #ifdef LP_DEBUG
524 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
525 #endif
526 if (minor >= LP_NO)
527 return -ENODEV;
528 if ((LP_F(minor) & LP_EXIST) == 0)
529 return -ENODEV;
530 switch ( cmd ) {
531 struct timeval par_timeout;
532 long to_jiffies;
534 case LPTIME:
535 LP_TIME(minor) = arg * HZ/100;
536 break;
537 case LPCHAR:
538 LP_CHAR(minor) = arg;
539 break;
540 case LPABORT:
541 if (arg)
542 LP_F(minor) |= LP_ABORT;
543 else
544 LP_F(minor) &= ~LP_ABORT;
545 break;
546 case LPABORTOPEN:
547 if (arg)
548 LP_F(minor) |= LP_ABORTOPEN;
549 else
550 LP_F(minor) &= ~LP_ABORTOPEN;
551 break;
552 case LPCAREFUL:
553 if (arg)
554 LP_F(minor) |= LP_CAREFUL;
555 else
556 LP_F(minor) &= ~LP_CAREFUL;
557 break;
558 case LPWAIT:
559 LP_WAIT(minor) = arg;
560 break;
561 case LPSETIRQ:
562 return -EINVAL;
563 break;
564 case LPGETIRQ:
565 if (copy_to_user((int *) arg, &LP_IRQ(minor),
566 sizeof(int)))
567 return -EFAULT;
568 break;
569 case LPGETSTATUS:
570 lp_parport_claim(minor);
571 status = r_str(minor);
572 lp_parport_release(minor);
574 if (copy_to_user((int *) arg, &status, sizeof(int)))
575 return -EFAULT;
576 break;
577 case LPRESET:
578 lp_reset(minor);
579 break;
580 #ifdef LP_STATS
581 case LPGETSTATS:
582 if (copy_to_user((int *) arg, &LP_STAT(minor),
583 sizeof(struct lp_stats)))
584 return -EFAULT;
585 if (suser())
586 memset(&LP_STAT(minor), 0,
587 sizeof(struct lp_stats));
588 break;
589 #endif
590 case LPGETFLAGS:
591 status = LP_F(minor);
592 if (copy_to_user((int *) arg, &status, sizeof(int)))
593 return -EFAULT;
594 break;
596 case LPSETTIMEOUT:
597 if (copy_from_user (&par_timeout,
598 (struct timeval *) arg,
599 sizeof (struct timeval))) {
600 return -EFAULT;
602 /* Convert to jiffies, place in lp_table */
603 if ((par_timeout.tv_sec < 0) ||
604 (par_timeout.tv_usec < 0)) {
605 return -EINVAL;
607 to_jiffies = ROUND_UP(par_timeout.tv_usec, 1000000/HZ);
608 to_jiffies += par_timeout.tv_sec * (long) HZ;
609 if (to_jiffies <= 0) {
610 return -EINVAL;
612 lp_table[minor].timeout = to_jiffies;
613 break;
615 default:
616 retval = -EINVAL;
618 return retval;
621 #ifdef CONFIG_PARPORT_1284
622 static unsigned int lp_poll (struct file *filp, struct poll_table_struct *wait)
624 unsigned int minor = MINOR (filp->f_dentry->d_inode->i_rdev);
625 unsigned int mask = POLLOUT | POLLWRNORM; /* always writable */
627 poll_wait (filp, &lp_table[minor].dataq, wait);
629 if (lp_table[minor].flags & LP_DATA_AVAIL)
630 mask |= POLLIN | POLLRDNORM;
632 return mask;
634 #endif /* IEEE 1284 support */
636 static struct file_operations lp_fops = {
637 write: lp_write,
638 ioctl: lp_ioctl,
639 open: lp_open,
640 release: lp_release,
641 #ifdef CONFIG_PARPORT_1284
642 read: lp_read,
643 poll: lp_poll,
644 #endif
647 /* --- support for console on the line printer ----------------- */
649 #ifdef CONFIG_LP_CONSOLE
651 #define CONSOLE_LP 0
653 /* If the printer is out of paper, we can either lose the messages or
654 * stall until the printer is happy again. Define CONSOLE_LP_STRICT
655 * non-zero to get the latter behaviour. */
656 #define CONSOLE_LP_STRICT 1
658 /* The console_lock must be held when we get here. */
660 static void lp_console_write (struct console *co, const char *s,
661 unsigned count)
663 struct pardevice *dev = lp_table[CONSOLE_LP].dev;
664 struct parport *port = dev->port;
665 ssize_t written;
666 signed long old_to;
668 if (!(lp_table[CONSOLE_LP].flags & (1<<LP_HAVE_PORT_BIT))) {
669 if (parport_claim (dev))
670 /* Nothing we can do. */
671 return;
672 set_bit (LP_HAVE_PORT_BIT, &lp_table[CONSOLE_LP].flags);
675 old_to = parport_set_timeout (dev, 0);
677 /* Go to compatibility mode. */
678 parport_negotiate (port, IEEE1284_MODE_COMPAT);
680 do {
681 /* Write the data, converting LF->CRLF as we go. */
682 ssize_t canwrite = count;
683 char *lf = strchr (s, '\n');
684 if (lf)
685 canwrite = lf - s;
687 if (canwrite > 0) {
688 written = parport_write (port, s, canwrite);
690 if (written <= 0)
691 continue;
693 s += written;
694 count -= written;
695 canwrite -= written;
698 if (lf && canwrite <= 0) {
699 const char *crlf = "\r\n";
700 int i = 2;
702 /* Dodge the original '\n', and put '\r\n' instead. */
703 s++;
704 count--;
705 do {
706 written = parport_write (port, crlf, i);
707 if (written > 0)
708 i -= written, crlf += written;
709 } while (i > 0 && (CONSOLE_LP_STRICT || written > 0));
711 } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
713 parport_set_timeout (dev, old_to);
716 static kdev_t lp_console_device (struct console *c)
718 return MKDEV(LP_MAJOR, CONSOLE_LP);
721 static struct console lpcons = {
722 "lp",
723 lp_console_write,
724 NULL,
725 lp_console_device,
726 NULL,
727 NULL,
728 NULL,
729 CON_PRINTBUFFER,
732 NULL
735 #endif /* console on line printer */
737 /* --- initialisation code ------------------------------------- */
739 #ifdef MODULE
741 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
742 static char *parport[LP_NO] = { NULL, };
743 static int reset = 0;
745 MODULE_PARM(parport, "1-" __MODULE_STRING(LP_NO) "s");
746 MODULE_PARM(reset, "i");
748 #else
750 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
751 static int reset = 0;
753 static int parport_ptr = 0;
755 void __init lp_setup(char *str, int *ints)
757 if (!str) {
758 if (ints[0] == 0 || ints[1] == 0) {
759 /* disable driver on "lp=" or "lp=0" */
760 parport_nr[0] = LP_PARPORT_OFF;
761 } else {
762 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", ints[1]);
764 } else if (!strncmp(str, "parport", 7)) {
765 int n = simple_strtoul(str+7, NULL, 10);
766 if (parport_ptr < LP_NO)
767 parport_nr[parport_ptr++] = n;
768 else
769 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
770 str);
771 } else if (!strcmp(str, "auto")) {
772 parport_nr[0] = LP_PARPORT_AUTO;
773 } else if (!strcmp(str, "none")) {
774 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
775 } else if (!strcmp(str, "reset")) {
776 reset = 1;
780 #endif
782 static int lp_register(int nr, struct parport *port)
784 lp_table[nr].dev = parport_register_device(port, "lp",
785 lp_preempt, lp_wakeup,
786 lp_interrupt,
788 (void *) &lp_table[nr]);
789 if (lp_table[nr].dev == NULL)
790 return 1;
791 lp_table[nr].flags |= LP_EXIST;
793 if (reset)
794 lp_reset(nr);
796 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
797 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
799 #ifdef CONFIG_LP_CONSOLE
800 if (!nr) {
801 if (port->modes & PARPORT_MODE_SAFEININT) {
802 MOD_INC_USE_COUNT;
803 register_console (&lpcons);
804 printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
805 } else
806 printk (KERN_ERR "lp%d: cannot run console on %s\n",
807 CONSOLE_LP, port->name);
809 #endif
811 return 0;
814 static void lp_attach (struct parport *port)
816 unsigned int i;
818 switch (parport_nr[0])
820 case LP_PARPORT_UNSPEC:
821 case LP_PARPORT_AUTO:
822 if (parport_nr[0] == LP_PARPORT_AUTO &&
823 port->probe_info[0].class != PARPORT_CLASS_PRINTER)
824 return;
826 if (!lp_register(lp_count, port))
827 if (++lp_count == LP_NO)
828 break;
830 break;
832 default:
833 for (i = 0; i < LP_NO; i++) {
834 if (port->number == parport_nr[i]) {
835 if (!lp_register(i, port))
836 lp_count++;
837 break;
840 break;
844 static void lp_detach (struct parport *port)
846 /* Write this some day. */
849 static struct parport_driver lp_driver = {
850 "lp",
851 lp_attach,
852 lp_detach,
853 NULL
856 int __init lp_init (void)
858 int i;
860 if (parport_nr[0] == LP_PARPORT_OFF)
861 return 0;
863 for (i = 0; i < LP_NO; i++) {
864 lp_table[i].dev = NULL;
865 lp_table[i].flags = 0;
866 lp_table[i].chars = LP_INIT_CHAR;
867 lp_table[i].time = LP_INIT_TIME;
868 lp_table[i].wait = LP_INIT_WAIT;
869 lp_table[i].lp_buffer = NULL;
870 #ifdef LP_STATS
871 lp_table[i].lastcall = 0;
872 lp_table[i].runchars = 0;
873 memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
874 #endif
875 lp_table[i].last_error = 0;
876 init_waitqueue_head (&lp_table[i].waitq);
877 init_waitqueue_head (&lp_table[i].dataq);
878 init_MUTEX (&lp_table[i].port_mutex);
879 lp_table[i].timeout = 10 * HZ;
882 if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
883 printk ("lp: unable to get major %d\n", LP_MAJOR);
884 return -EIO;
887 if (parport_register_driver (&lp_driver)) {
888 printk ("lp: unable to register with parport\n");
889 return -EIO;
892 if (!lp_count) {
893 printk (KERN_INFO "lp: driver loaded but no devices found\n");
894 #ifndef CONFIG_PARPORT_12843
895 if (parport_nr[0] == LP_PARPORT_AUTO)
896 printk (KERN_INFO "lp: (is IEEE 1284.3 support enabled?)\n");
897 #endif
900 return 0;
903 #ifdef MODULE
904 int init_module(void)
906 if (parport[0]) {
907 /* The user gave some parameters. Let's see what they were. */
908 if (!strncmp(parport[0], "auto", 4))
909 parport_nr[0] = LP_PARPORT_AUTO;
910 else {
911 int n;
912 for (n = 0; n < LP_NO && parport[n]; n++) {
913 if (!strncmp(parport[n], "none", 4))
914 parport_nr[n] = LP_PARPORT_NONE;
915 else {
916 char *ep;
917 unsigned long r = simple_strtoul(parport[n], &ep, 0);
918 if (ep != parport[n])
919 parport_nr[n] = r;
920 else {
921 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
922 return -ENODEV;
929 return lp_init();
932 void cleanup_module(void)
934 unsigned int offset;
936 parport_unregister_driver (&lp_driver);
938 #ifdef CONFIG_LP_CONSOLE
939 unregister_console (&lpcons);
940 #endif
942 unregister_chrdev(LP_MAJOR, "lp");
943 for (offset = 0; offset < LP_NO; offset++) {
944 if (lp_table[offset].dev == NULL)
945 continue;
946 if (lp_table[offset].flags & (1<<LP_HAVE_PORT_BIT))
947 parport_release (lp_table[offset].dev);
948 parport_unregister_device(lp_table[offset].dev);
951 #endif