Import 2.3.10pre1
[davej-history.git] / drivers / char / lp.c
blob7aa14a88614315adcd01ac0474410114a7866877
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
111 #include <linux/module.h>
112 #include <linux/init.h>
114 #include <linux/config.h>
115 #include <linux/errno.h>
116 #include <linux/kernel.h>
117 #include <linux/major.h>
118 #include <linux/sched.h>
119 #include <linux/malloc.h>
120 #include <linux/fcntl.h>
121 #include <linux/delay.h>
122 #include <linux/poll.h>
123 #include <linux/console.h>
125 #include <linux/parport.h>
126 #undef LP_STATS
127 #include <linux/lp.h>
129 #include <asm/irq.h>
130 #include <asm/uaccess.h>
131 #include <asm/system.h>
133 /* if you have more than 3 printers, remember to increase LP_NO */
134 #define LP_NO 3
136 struct lp_struct lp_table[LP_NO];
138 static unsigned int lp_count = 0;
140 /* Test if printer is ready */
141 #define LP_READY(status) ((status) & LP_PBUSY)
142 /* Test if the printer is not acking the strobe */
143 #define LP_NO_ACKING(status) ((status) & LP_PACK)
144 /* Test if the printer has error conditions */
145 #define LP_NO_ERROR(status) ((status) & LP_PERRORP)
147 #undef LP_DEBUG
149 /* If you want to see if you can get lp_poll working, define this. */
150 #undef SUPPORT_POLL
152 /* --- parport support ----------------------------------------- */
154 static int lp_preempt(void *handle)
156 struct lp_struct *lps = (struct lp_struct *)handle;
158 if (!(lps->flags & LP_PORT_BUSY)) {
159 /* Let the port go. */
160 clear_bit (LP_HAVE_PORT_BIT, &lps->flags);
161 return 0;
164 if (!(lps->flags & LP_PORT_BUSY)) {
165 /* Let the port go. */
166 clear_bit (LP_HAVE_PORT_BIT, &lps->flags);
167 return 0;
170 /* Don't actually release the port now */
171 return 1;
174 static void lp_check_data (struct lp_struct *lp)
176 #if !defined(CONFIG_PARPORT_1284) || !defined (SUPPORT_POLL)
177 return;
178 #else
179 struct pardevice *dev = lp->dev;
180 if (!(lp->flags & LP_NO_REVERSE)) {
181 int err = parport_negotiate (dev->port, IEEE1284_MODE_NIBBLE);
182 if (err)
183 lp->flags |= LP_NO_REVERSE;
184 else {
185 unsigned char s = parport_read_status (dev->port);
186 if (s & PARPORT_STATUS_ERROR)
187 lp->flags &= ~LP_DATA_AVAIL;
188 else {
189 lp->flags |= LP_DATA_AVAIL;
190 if (waitqueue_active (&lp->dataq))
191 wake_up_interruptible (&lp->dataq);
195 #endif /* IEEE 1284 support */
198 static void lp_parport_release (int minor)
200 lp_check_data (&lp_table[minor]);
201 if (test_and_clear_bit (LP_HAVE_PORT_BIT, &lp_table[minor].flags))
202 parport_release (lp_table[minor].dev);
204 lp_table[minor].flags &= ~LP_PORT_BUSY;
207 static void lp_parport_claim (int minor)
209 if (!test_and_set_bit (LP_HAVE_PORT_BIT, &lp_table[minor].flags))
210 parport_claim_or_block (lp_table[minor].dev);
212 lp_table[minor].flags |= LP_PORT_BUSY;
215 /* --- low-level port access ----------------------------------- */
217 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
218 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
219 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
220 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
222 static int lp_reset(int minor)
224 int retval;
225 lp_parport_claim (minor);
226 w_ctr(minor, LP_PSELECP);
227 udelay (LP_DELAY);
228 w_ctr(minor, LP_PSELECP | LP_PINITP);
229 retval = r_str(minor);
230 lp_parport_release (minor);
231 return retval;
234 static void lp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
236 struct lp_struct *lp_dev = (struct lp_struct *) dev_id;
237 if (!(lp_dev->flags & LP_PORT_BUSY))
238 /* We must have the port since we got an interrupt. */
239 lp_check_data (lp_dev);
240 if (waitqueue_active (&lp_dev->waitq))
241 wake_up_interruptible (&lp_dev->waitq);
244 static void lp_wakeup (void *handle)
246 struct lp_struct *lp_dev = handle;
248 if (lp_dev->flags & LP_PORT_BUSY)
249 return;
251 /* Grab the port if it can help (i.e. reverse mode is possible). */
252 if (!(lp_dev->flags & LP_NO_REVERSE)) {
253 parport_claim (lp_dev->dev);
254 set_bit (LP_HAVE_PORT_BIT, &lp_dev->flags);
255 lp_check_data (lp_dev);
256 if (waitqueue_active (&lp_dev->waitq))
257 wake_up_interruptible (&lp_dev->waitq);
261 static void lp_error (int minor)
263 int polling;
265 if (LP_F(minor) & LP_ABORT)
266 return;
268 polling = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
269 if (polling) lp_parport_release (minor);
270 interruptible_sleep_on_timeout (&lp_table[minor].waitq,
271 LP_TIMEOUT_POLLED);
272 if (polling) lp_parport_claim (minor);
273 else parport_yield_blocking (lp_table[minor].dev);
276 static int lp_check_status(int minor)
278 int error = 0;
279 unsigned int last = lp_table[minor].last_error;
280 unsigned char status = r_str(minor);
281 if (status & LP_PERRORP)
282 /* No error. */
283 last = 0;
284 else if ((status & LP_POUTPA)) {
285 if (last != LP_POUTPA) {
286 last = LP_POUTPA;
287 printk(KERN_INFO "lp%d out of paper\n", minor);
289 error = -ENOSPC;
290 } else if (!(status & LP_PSELECD)) {
291 if (last != LP_PSELECD) {
292 last = LP_PSELECD;
293 printk(KERN_INFO "lp%d off-line\n", minor);
295 error = -EIO;
296 } else {
297 if (last != LP_PERRORP) {
298 last = LP_PERRORP;
299 printk(KERN_INFO "lp%d on fire\n", minor);
301 error = -EIO;
304 lp_table[minor].last_error = last;
306 if (last != 0)
307 lp_error(minor);
309 return error;
312 static ssize_t lp_write(struct file * file, const char * buf,
313 size_t count, loff_t *ppos)
315 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
316 struct parport *port = lp_table[minor].dev->port;
317 char *kbuf = lp_table[minor].lp_buffer;
318 ssize_t retv = 0;
319 ssize_t written;
320 size_t copy_size = count;
322 #ifdef LP_STATS
323 if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
324 lp_table[minor].runchars = 0;
326 lp_table[minor].lastcall = jiffies;
327 #endif
329 /* Need to copy the data from user-space. */
330 if (copy_size > LP_BUFFER_SIZE)
331 copy_size = LP_BUFFER_SIZE;
333 if (copy_from_user (kbuf, buf, copy_size))
334 return -EFAULT;
336 /* Claim Parport or sleep until it becomes available
338 lp_parport_claim (minor);
340 /* Go to compatibility mode. */
341 parport_negotiate (port, IEEE1284_MODE_COMPAT);
343 do {
344 /* Wait until lp_read has finished. */
345 if (down_interruptible (&lp_table[minor].port_mutex))
346 break;
348 /* Write the data. */
349 written = parport_write (port, kbuf, copy_size);
350 if (written >= 0) {
351 copy_size -= written;
352 count -= written;
353 buf += written;
354 retv += written;
357 up (&lp_table[minor].port_mutex);
359 if (signal_pending (current)) {
360 if (retv == 0)
361 retv = -EINTR;
363 break;
366 if (copy_size > 0) {
367 /* incomplete write -> check error ! */
368 int error = lp_check_status (minor);
370 if (LP_F(minor) & LP_ABORT) {
371 if (retv == 0)
372 retv = error;
373 break;
376 parport_yield_blocking (lp_table[minor].dev);
377 } else if (current->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 lp_parport_release (minor);
395 return retv;
398 static long long lp_lseek(struct file * file, long long offset, int origin)
400 return -ESPIPE;
403 #ifdef CONFIG_PARPORT_1284
405 /* Status readback conforming to ieee1284 */
406 static ssize_t lp_read(struct file * file, char * buf,
407 size_t count, loff_t *ppos)
409 unsigned int minor=MINOR(file->f_dentry->d_inode->i_rdev);
410 struct parport *port = lp_table[minor].dev->port;
411 ssize_t retval = 0;
412 char *kbuf = lp_table[minor].lp_buffer;
414 if (count > LP_BUFFER_SIZE)
415 count = LP_BUFFER_SIZE;
417 lp_parport_claim (minor);
419 if (!down_interruptible (&lp_table[minor].port_mutex)) {
420 for (;;) {
421 retval = parport_read (port, kbuf, count);
423 if (retval)
424 break;
426 if (file->f_flags & O_NONBLOCK)
427 break;
429 /* Wait for an interrupt. */
430 interruptible_sleep_on_timeout (&lp_table[minor].waitq,
431 LP_TIMEOUT_POLLED);
433 if (signal_pending (current)) {
434 retval = -EINTR;
435 break;
439 up (&lp_table[minor].port_mutex);
442 lp_parport_release (minor);
444 if (retval > 0 && copy_to_user (buf, kbuf, retval))
445 retval = -EFAULT;
447 return retval;
450 #endif /* IEEE 1284 support */
452 static int lp_open(struct inode * inode, struct file * file)
454 unsigned int minor = MINOR(inode->i_rdev);
456 if (minor >= LP_NO)
457 return -ENXIO;
458 if ((LP_F(minor) & LP_EXIST) == 0)
459 return -ENXIO;
460 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
461 return -EBUSY;
463 MOD_INC_USE_COUNT;
465 /* If ABORTOPEN is set and the printer is offline or out of paper,
466 we may still want to open it to perform ioctl()s. Therefore we
467 have commandeered O_NONBLOCK, even though it is being used in
468 a non-standard manner. This is strictly a Linux hack, and
469 should most likely only ever be used by the tunelp application. */
470 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
471 int status;
472 lp_parport_claim (minor);
473 status = r_str(minor);
474 lp_parport_release (minor);
475 if (status & LP_POUTPA) {
476 printk(KERN_INFO "lp%d out of paper\n", minor);
477 MOD_DEC_USE_COUNT;
478 LP_F(minor) &= ~LP_BUSY;
479 return -ENOSPC;
480 } else if (!(status & LP_PSELECD)) {
481 printk(KERN_INFO "lp%d off-line\n", minor);
482 MOD_DEC_USE_COUNT;
483 LP_F(minor) &= ~LP_BUSY;
484 return -EIO;
485 } else if (!(status & LP_PERRORP)) {
486 printk(KERN_ERR "lp%d printer error\n", minor);
487 MOD_DEC_USE_COUNT;
488 LP_F(minor) &= ~LP_BUSY;
489 return -EIO;
492 lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
493 if (!lp_table[minor].lp_buffer) {
494 MOD_DEC_USE_COUNT;
495 LP_F(minor) &= ~LP_BUSY;
496 return -ENOMEM;
498 return 0;
501 static int lp_release(struct inode * inode, struct file * file)
503 unsigned int minor = MINOR(inode->i_rdev);
505 kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
506 lp_table[minor].lp_buffer = NULL;
507 MOD_DEC_USE_COUNT;
508 LP_F(minor) &= ~LP_BUSY;
509 return 0;
512 static int lp_ioctl(struct inode *inode, struct file *file,
513 unsigned int cmd, unsigned long arg)
515 unsigned int minor = MINOR(inode->i_rdev);
516 int status;
517 int retval = 0;
519 #ifdef LP_DEBUG
520 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%lx\n", minor, cmd, arg);
521 #endif
522 if (minor >= LP_NO)
523 return -ENODEV;
524 if ((LP_F(minor) & LP_EXIST) == 0)
525 return -ENODEV;
526 switch ( cmd ) {
527 case LPTIME:
528 LP_TIME(minor) = arg * HZ/100;
529 break;
530 case LPCHAR:
531 LP_CHAR(minor) = arg;
532 break;
533 case LPABORT:
534 if (arg)
535 LP_F(minor) |= LP_ABORT;
536 else
537 LP_F(minor) &= ~LP_ABORT;
538 break;
539 case LPABORTOPEN:
540 if (arg)
541 LP_F(minor) |= LP_ABORTOPEN;
542 else
543 LP_F(minor) &= ~LP_ABORTOPEN;
544 break;
545 #ifdef OBSOLETED
546 case LPCAREFUL:
547 if (arg)
548 LP_F(minor) |= LP_CAREFUL;
549 else
550 LP_F(minor) &= ~LP_CAREFUL;
551 break;
552 #endif
553 case LPWAIT:
554 LP_WAIT(minor) = arg;
555 break;
556 case LPSETIRQ:
557 return -EINVAL;
558 break;
559 case LPGETIRQ:
560 if (copy_to_user((int *) arg, &LP_IRQ(minor),
561 sizeof(int)))
562 return -EFAULT;
563 break;
564 case LPGETSTATUS:
565 lp_parport_claim(minor);
566 status = r_str(minor);
567 lp_parport_release(minor);
569 if (copy_to_user((int *) arg, &status, sizeof(int)))
570 return -EFAULT;
571 break;
572 case LPRESET:
573 lp_reset(minor);
574 break;
575 #ifdef LP_STATS
576 case LPGETSTATS:
577 if (copy_to_user((int *) arg, &LP_STAT(minor),
578 sizeof(struct lp_stats)))
579 return -EFAULT;
580 if (suser())
581 memset(&LP_STAT(minor), 0,
582 sizeof(struct lp_stats));
583 break;
584 #endif
585 case LPGETFLAGS:
586 status = LP_F(minor);
587 if (copy_to_user((int *) arg, &status, sizeof(int)))
588 return -EFAULT;
589 break;
590 default:
591 retval = -EINVAL;
593 return retval;
596 #ifdef CONFIG_PARPORT_1284
597 static unsigned int lp_poll (struct file *filp, struct poll_table_struct *wait)
599 unsigned int minor = MINOR (filp->f_dentry->d_inode->i_rdev);
600 unsigned int mask = POLLOUT | POLLWRNORM; /* always writable */
602 poll_wait (filp, &lp_table[minor].dataq, wait);
604 if (lp_table[minor].flags & LP_DATA_AVAIL)
605 mask |= POLLIN | POLLRDNORM;
607 return mask;
609 #endif /* IEEE 1284 support */
611 static struct file_operations lp_fops = {
612 lp_lseek,
613 #ifdef CONFIG_PARPORT_1284
614 lp_read,
615 #else
616 NULL,
617 #endif
618 lp_write,
619 NULL, /* lp_readdir */
620 #ifdef CONFIG_PARPORT_1284
621 lp_poll,
622 #else
623 NULL,
624 #endif
625 lp_ioctl,
626 NULL, /* lp_mmap */
627 lp_open,
628 NULL, /* flush */
629 lp_release
632 /* --- support for console on the line printer ----------------- */
634 #ifdef CONFIG_LP_CONSOLE
636 #define CONSOLE_LP 0
638 /* If the printer is out of paper, we can either lose the messages or
639 * stall until the printer is happy again. Define CONSOLE_LP_STRICT
640 * non-zero to get the latter behaviour. */
641 #define CONSOLE_LP_STRICT 1
643 static void lp_console_write (struct console *co, const char *s,
644 unsigned count)
646 struct pardevice *dev = lp_table[CONSOLE_LP].dev;
647 struct parport *port = dev->port;
648 ssize_t written;
649 signed long old_to;
651 if (!(lp_table[CONSOLE_LP].flags & (1<<LP_HAVE_PORT_BIT))) {
652 if (parport_claim (dev))
653 /* Nothing we can do. */
654 return;
655 set_bit (LP_HAVE_PORT_BIT, &lp_table[CONSOLE_LP].flags);
658 old_to = parport_set_timeout (dev, 0);
660 /* Go to compatibility mode. */
661 parport_negotiate (port, IEEE1284_MODE_COMPAT);
663 do {
664 /* Write the data. */
665 written = parport_write (port, s, count);
666 if (written > 0) {
667 s += written;
668 count -= written;
670 } while (count > 0 && (CONSOLE_LP_STRICT || written > 0));
672 parport_set_timeout (dev, old_to);
675 static kdev_t lp_console_device (struct console *c)
677 return MKDEV(LP_MAJOR, CONSOLE_LP);
680 static struct console lpcons = {
681 "lp",
682 lp_console_write,
683 NULL,
684 lp_console_device,
685 NULL,
686 NULL,
687 NULL,
688 CON_PRINTBUFFER,
691 NULL
694 #endif /* console on line printer */
696 /* --- initialisation code ------------------------------------- */
698 #ifdef MODULE
700 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
701 static char *parport[LP_NO] = { NULL, };
702 static int reset = 0;
704 MODULE_PARM(parport, "1-" __MODULE_STRING(LP_NO) "s");
705 MODULE_PARM(reset, "i");
707 #else
709 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
710 static int reset = 0;
712 static int parport_ptr = 0;
714 __initfunc(void lp_setup(char *str, int *ints))
716 if (!str) {
717 if (ints[0] == 0 || ints[1] == 0) {
718 /* disable driver on "lp=" or "lp=0" */
719 parport_nr[0] = LP_PARPORT_OFF;
720 } else {
721 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", ints[1]);
723 } else if (!strncmp(str, "parport", 7)) {
724 int n = simple_strtoul(str+7, NULL, 10);
725 if (parport_ptr < LP_NO)
726 parport_nr[parport_ptr++] = n;
727 else
728 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
729 str);
730 } else if (!strcmp(str, "auto")) {
731 parport_nr[0] = LP_PARPORT_AUTO;
732 } else if (!strcmp(str, "none")) {
733 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
734 } else if (!strcmp(str, "reset")) {
735 reset = 1;
739 #endif
741 static int lp_register(int nr, struct parport *port)
743 lp_table[nr].dev = parport_register_device(port, "lp",
744 lp_preempt, lp_wakeup,
745 lp_interrupt,
747 (void *) &lp_table[nr]);
748 if (lp_table[nr].dev == NULL)
749 return 1;
750 lp_table[nr].flags |= LP_EXIST;
752 if (reset)
753 lp_reset(nr);
755 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
756 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
758 return 0;
761 static void lp_attach (struct parport *port)
763 unsigned int i;
765 switch (parport_nr[0])
767 case LP_PARPORT_UNSPEC:
768 case LP_PARPORT_AUTO:
769 if (parport_nr[0] == LP_PARPORT_AUTO &&
770 port->probe_info[0].class != PARPORT_CLASS_PRINTER)
771 return;
773 if (!lp_register(lp_count, port))
774 if (++lp_count == LP_NO)
775 break;
777 break;
779 default:
780 for (i = 0; i < LP_NO; i++) {
781 if (port->number == parport_nr[i]) {
782 if (!lp_register(i, port))
783 lp_count++;
784 break;
787 break;
791 static void lp_detach (struct parport *port)
793 /* Write this some day. */
796 static struct parport_driver lp_driver = {
797 "lp",
798 lp_attach,
799 lp_detach,
800 NULL
803 int __init lp_init (void)
805 int i;
807 if (parport_nr[0] == LP_PARPORT_OFF)
808 return 0;
810 for (i = 0; i < LP_NO; i++) {
811 lp_table[i].dev = NULL;
812 lp_table[i].flags = 0;
813 lp_table[i].chars = LP_INIT_CHAR;
814 lp_table[i].time = LP_INIT_TIME;
815 lp_table[i].wait = LP_INIT_WAIT;
816 lp_table[i].lp_buffer = NULL;
817 #ifdef LP_STATS
818 lp_table[i].lastcall = 0;
819 lp_table[i].runchars = 0;
820 memset (&lp_table[i].stats, 0, sizeof (struct lp_stats));
821 #endif
822 lp_table[i].last_error = 0;
823 init_waitqueue_head (&lp_table[i].waitq);
824 init_waitqueue_head (&lp_table[i].dataq);
825 init_MUTEX (&lp_table[i].port_mutex);
828 if (register_chrdev (LP_MAJOR, "lp", &lp_fops)) {
829 printk ("lp: unable to get major %d\n", LP_MAJOR);
830 return -EIO;
833 if (parport_register_driver (&lp_driver)) {
834 printk ("lp: unable to register with parport\n");
835 return -EIO;
838 if (!lp_count) {
839 printk (KERN_INFO "lp: driver loaded but no devices found\n");
840 #ifndef CONFIG_PARPORT_12843
841 if (parport_nr[0] == LP_PARPORT_AUTO)
842 printk (KERN_INFO "lp: (is IEEE 1284.3 support enabled?)\n");
843 #endif
845 #ifdef CONFIG_LP_CONSOLE
846 else {
847 register_console (&lpcons);
848 printk (KERN_INFO "lp%d: console ready\n", CONSOLE_LP);
850 #endif
852 return 0;
855 #ifdef MODULE
856 int init_module(void)
858 if (parport[0]) {
859 /* The user gave some parameters. Let's see what they were. */
860 if (!strncmp(parport[0], "auto", 4))
861 parport_nr[0] = LP_PARPORT_AUTO;
862 else {
863 int n;
864 for (n = 0; n < LP_NO && parport[n]; n++) {
865 if (!strncmp(parport[n], "none", 4))
866 parport_nr[n] = LP_PARPORT_NONE;
867 else {
868 char *ep;
869 unsigned long r = simple_strtoul(parport[n], &ep, 0);
870 if (ep != parport[n])
871 parport_nr[n] = r;
872 else {
873 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
874 return -ENODEV;
881 return lp_init();
884 void cleanup_module(void)
886 unsigned int offset;
888 parport_unregister_driver (&lp_driver);
890 #ifdef CONFIG_LP_CONSOLE
891 unregister_console (&lpcons);
892 #endif
894 unregister_chrdev(LP_MAJOR, "lp");
895 for (offset = 0; offset < LP_NO; offset++) {
896 if (lp_table[offset].dev == NULL)
897 continue;
898 if (lp_table[offset].flags & (1<<LP_HAVE_PORT_BIT))
899 parport_release (lp_table[offset].dev);
900 parport_unregister_device(lp_table[offset].dev);
903 #endif