Linux 2.1.127
[davej-history.git] / drivers / char / lp.c
blobbaec5e4cb11df6db24e7467b08ffc1d0d4e424da
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 * Interrupt handling workaround for printers with buggy handshake
20 * by Andrea Arcangeli, 11 May 98
23 /* This driver should, in theory, work with any parallel port that has an
24 * appropriate low-level driver; all I/O is done through the parport
25 * abstraction layer.
27 * If this driver is built into the kernel, you can configure it using the
28 * kernel command-line. For example:
30 * lp=parport1,none,parport2 (bind lp0 to parport1, disable lp1 and
31 * bind lp2 to parport2)
33 * lp=auto (assign lp devices to all ports that
34 * have printers attached, as determined
35 * by the IEEE-1284 autoprobe)
37 * lp=reset (reset the printer during
38 * initialisation)
40 * lp=off (disable the printer driver entirely)
42 * If the driver is loaded as a module, similar functionality is available
43 * using module parameters. The equivalent of the above commands would be:
45 * # insmod lp.o parport=1,none,2
47 * # insmod lp.o parport=auto
49 * # insmod lp.o reset=1
52 /* COMPATIBILITY WITH OLD KERNELS
54 * Under Linux 2.0 and previous versions, lp devices were bound to ports at
55 * particular I/O addresses, as follows:
57 * lp0 0x3bc
58 * lp1 0x378
59 * lp2 0x278
61 * The new driver, by default, binds lp devices to parport devices as it
62 * finds them. This means that if you only have one port, it will be bound
63 * to lp0 regardless of its I/O address. If you need the old behaviour, you
64 * can force it using the parameters described above.
68 * The new interrupt handling code take care of the buggy handshake
69 * of some HP and Epson printer:
70 * ___
71 * ACK _______________ ___________
72 * |__|
73 * ____
74 * BUSY _________ _______
75 * |____________|
77 * I discovered this using the printer scanner that you can find at:
79 * ftp://e-mind.com/pub/linux/pscan/
81 * 11 May 98, Andrea Arcangeli
84 #include <linux/module.h>
85 #include <linux/init.h>
87 #include <linux/config.h>
88 #include <linux/errno.h>
89 #include <linux/kernel.h>
90 #include <linux/major.h>
91 #include <linux/sched.h>
92 #include <linux/malloc.h>
93 #include <linux/fcntl.h>
94 #include <linux/delay.h>
96 #include <linux/parport.h>
97 #undef LP_STATS
98 #undef LP_NEED_CAREFUL
99 #include <linux/lp.h>
101 #include <asm/irq.h>
102 #include <asm/uaccess.h>
103 #include <asm/system.h>
105 /* if you have more than 3 printers, remember to increase LP_NO */
106 #define LP_NO 3
108 struct lp_struct lp_table[LP_NO] =
110 [0 ... LP_NO-1] = {NULL, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT,
111 NULL,
112 #ifdef LP_STATS
113 0, 0, {0},
114 #endif
115 NULL, 0, 0, 0}
118 /* Test if printer is ready (and optionally has no error conditions) */
119 #ifdef LP_NEED_CAREFUL
120 #define LP_READY(minor, status) \
121 ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : ((status) & LP_PBUSY))
122 #define _LP_CAREFUL_READY(status) \
123 ((status) & (LP_PBUSY|LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
124 (LP_PBUSY|LP_PSELECD|LP_PERRORP)
125 #else
126 #define LP_READY(minor, status) ((status) & LP_PBUSY)
127 #endif
129 #undef LP_DEBUG
130 #undef LP_READ_DEBUG
132 /* --- parport support ----------------------------------------- */
134 static int lp_preempt(void *handle)
136 struct lp_struct *lps = (struct lp_struct *)handle;
138 if (waitqueue_active (&lps->wait_q))
139 wake_up_interruptible(&lps->wait_q);
141 /* Don't actually release the port now */
142 return 1;
145 #define lp_parport_release(x) do { parport_release(lp_table[(x)].dev); } while (0);
146 #define lp_parport_claim(x) do { parport_claim_or_block(lp_table[(x)].dev); } while (0);
148 /* --- low-level port access ----------------------------------- */
150 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
151 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
152 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
153 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
155 static __inline__ void lp_yield (int minor)
157 if (!parport_yield_blocking (lp_table[minor].dev))
159 if (current->need_resched)
160 schedule ();
161 } else
162 lp_table[minor].irq_missed = 1;
165 static __inline__ void lp_schedule(int minor, long timeout)
167 struct pardevice *dev = lp_table[minor].dev;
168 register unsigned long int timeslip = (jiffies - dev->time);
169 if ((timeslip > dev->timeslice) && (dev->port->waithead != NULL)) {
170 lp_parport_release(minor);
171 lp_table[minor].irq_missed = 1;
172 schedule_timeout(timeout);
173 lp_parport_claim(minor);
174 } else
175 schedule_timeout(timeout);
178 static int lp_reset(int minor)
180 int retval;
181 lp_parport_claim (minor);
182 w_ctr(minor, LP_PSELECP);
183 udelay (LP_DELAY);
184 w_ctr(minor, LP_PSELECP | LP_PINITP);
185 retval = r_str(minor);
186 lp_parport_release (minor);
187 return retval;
190 static inline int lp_char(char lpchar, int minor)
192 unsigned int wait = 0;
193 unsigned long count = 0;
194 #ifdef LP_STATS
195 struct lp_stats *stats;
196 #endif
198 for (;;)
200 lp_yield(minor);
201 if (LP_READY(minor, r_str(minor)))
202 break;
203 if (++count == LP_CHAR(minor) || signal_pending(current))
204 return 0;
207 w_dtr(minor, lpchar);
208 #ifdef LP_STATS
209 stats = &LP_STAT(minor);
210 stats->chars++;
211 #endif
212 /* must wait before taking strobe high, and after taking strobe
213 low, according spec. Some printers need it, others don't. */
214 #ifndef __sparc__
215 while (wait != LP_WAIT(minor)) /* FIXME: should be a udelay() */
216 wait++;
217 #else
218 udelay(1);
219 #endif
220 /* control port takes strobe high */
221 w_ctr(minor, LP_PSELECP | LP_PINITP | LP_PSTROBE);
222 #ifndef __sparc__
223 while (wait) /* FIXME: should be a udelay() */
224 wait--;
225 #else
226 udelay(1);
227 #endif
228 /* take strobe low */
229 if (LP_POLLED(minor))
230 /* take strobe low */
231 w_ctr(minor, LP_PSELECP | LP_PINITP);
232 else
234 lp_table[minor].irq_detected = 0;
235 lp_table[minor].irq_missed = 0;
236 w_ctr(minor, LP_PSELECP | LP_PINITP | LP_PINTEN);
239 #ifdef LP_STATS
240 /* update waittime statistics */
241 if (count > stats->maxwait) {
242 #ifdef LP_DEBUG
243 printk(KERN_DEBUG "lp%d success after %d counts.\n", minor, count);
244 #endif
245 stats->maxwait = count;
247 count *= 256;
248 wait = (count > stats->meanwait) ? count - stats->meanwait :
249 stats->meanwait - count;
250 stats->meanwait = (255 * stats->meanwait + count + 128) / 256;
251 stats->mdev = ((127 * stats->mdev) + wait + 64) / 128;
252 #endif
254 return 1;
257 static void lp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
259 struct lp_struct *lp_dev = (struct lp_struct *) dev_id;
261 if (waitqueue_active (&lp_dev->wait_q))
262 wake_up_interruptible(&lp_dev->wait_q);
264 lp_dev->irq_detected = 1;
265 lp_dev->irq_missed = 0;
268 static void lp_error(int minor)
270 if (LP_POLLED(minor) || LP_PREEMPTED(minor)) {
271 current->state = TASK_INTERRUPTIBLE;
272 lp_parport_release(minor);
273 schedule_timeout(LP_TIMEOUT_POLLED);
274 lp_parport_claim(minor);
275 lp_table[minor].irq_missed = 1;
279 static int lp_check_status(int minor)
281 unsigned int last = lp_table[minor].last_error;
282 unsigned char status = r_str(minor);
283 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 } else if (!(status & LP_PSELECD)) {
289 if (last != LP_PSELECD) {
290 last = LP_PSELECD;
291 printk(KERN_INFO "lp%d off-line\n", minor);
293 } else if (!(status & LP_PERRORP)) {
294 if (last != LP_PERRORP) {
295 last = LP_PERRORP;
296 printk(KERN_ERR "lp%d on fire!\n", minor);
299 else last = 0;
301 lp_table[minor].last_error = last;
303 if (last != 0) {
304 if (LP_F(minor) & LP_ABORT)
305 return 1;
306 lp_error(minor);
309 return 0;
312 static int lp_write_buf(unsigned int minor, const char *buf, int count)
314 unsigned long copy_size;
315 unsigned long total_bytes_written = 0;
316 unsigned long bytes_written;
317 struct lp_struct *lp = &lp_table[minor];
319 if (minor >= LP_NO)
320 return -ENXIO;
321 if (lp->dev == NULL)
322 return -ENXIO;
324 lp_table[minor].last_error = 0;
325 lp_table[minor].irq_detected = 0;
326 lp_table[minor].irq_missed = 1;
328 w_ctr(minor, LP_PSELECP | LP_PINITP);
330 do {
331 bytes_written = 0;
332 copy_size = (count <= LP_BUFFER_SIZE ? count : LP_BUFFER_SIZE);
334 if (copy_from_user(lp->lp_buffer, buf, copy_size))
336 w_ctr(minor, LP_PSELECP | LP_PINITP);
337 return -EFAULT;
340 while (copy_size) {
341 if (lp_char(lp->lp_buffer[bytes_written], minor)) {
342 --copy_size;
343 ++bytes_written;
344 #ifdef LP_STATS
345 lp->runchars++;
346 #endif
347 } else {
348 int rc = total_bytes_written + bytes_written;
350 #ifdef LP_STATS
351 if (lp->runchars > LP_STAT(minor).maxrun)
352 LP_STAT(minor).maxrun = lp->runchars;
353 LP_STAT(minor).sleeps++;
354 #endif
356 if (signal_pending(current))
358 w_ctr(minor, LP_PSELECP | LP_PINITP);
359 if (total_bytes_written + bytes_written)
360 return total_bytes_written + bytes_written;
361 else
362 return -EINTR;
365 #ifdef LP_STATS
366 lp->runchars = 0;
367 #endif
369 if (lp_check_status(minor))
371 w_ctr(minor, LP_PSELECP | LP_PINITP);
372 return rc ? rc : -EIO;
375 if (LP_POLLED(minor) ||
376 lp_table[minor].irq_missed)
378 lp_polling:
379 #if defined(LP_DEBUG) && defined(LP_STATS)
380 printk(KERN_DEBUG "lp%d sleeping at %d characters for %d jiffies\n", minor, lp->runchars, LP_TIME(minor));
381 #endif
382 current->state = TASK_INTERRUPTIBLE;
383 lp_schedule(minor, LP_TIME(minor));
384 } else {
385 cli();
386 if (LP_PREEMPTED(minor))
389 * We can' t sleep on the interrupt
390 * since another pardevice need the port.
391 * We must check this in a cli() protected
392 * envinroment to avoid parport sharing
393 * starvation.
395 sti();
396 goto lp_polling;
398 if (!lp_table[minor].irq_detected)
400 interruptible_sleep_on_timeout(&lp->wait_q, LP_TIMEOUT_INTERRUPT);
402 sti();
407 total_bytes_written += bytes_written;
408 buf += bytes_written;
409 count -= bytes_written;
411 } while (count > 0);
413 w_ctr(minor, LP_PSELECP | LP_PINITP);
414 return total_bytes_written;
417 static ssize_t lp_write(struct file * file, const char * buf,
418 size_t count, loff_t *ppos)
420 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
421 ssize_t retv;
423 #ifdef LP_STATS
424 if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
425 lp_table[minor].runchars = 0;
427 lp_table[minor].lastcall = jiffies;
428 #endif
430 /* Claim Parport or sleep until it becomes available
432 lp_parport_claim (minor);
434 retv = lp_write_buf(minor, buf, count);
436 lp_parport_release (minor);
437 return retv;
440 static long long lp_lseek(struct file * file, long long offset, int origin)
442 return -ESPIPE;
445 #ifdef CONFIG_PRINTER_READBACK
447 static int lp_read_nibble(int minor)
449 unsigned char i;
450 i = r_str(minor)>>3;
451 i &= ~8;
452 if ((i & 0x10) == 0) i |= 8;
453 return (i & 0x0f);
456 static inline void lp_select_in_high(int minor)
458 parport_frob_control(lp_table[minor].dev->port, 8, 8);
461 /* Status readback confirming to ieee1284 */
462 static ssize_t lp_read(struct file * file, char * buf,
463 size_t count, loff_t *ppos)
465 unsigned char z=0, Byte=0, status;
466 char *temp;
467 ssize_t retval;
468 unsigned int counter=0;
469 unsigned int i;
470 unsigned int minor=MINOR(file->f_dentry->d_inode->i_rdev);
472 /* Claim Parport or sleep until it becomes available
474 lp_parport_claim (minor);
476 temp=buf;
477 #ifdef LP_READ_DEBUG
478 printk(KERN_INFO "lp%d: read mode\n", minor);
479 #endif
481 retval = verify_area(VERIFY_WRITE, buf, count);
482 if (retval)
483 return retval;
484 if (parport_ieee1284_nibble_mode_ok(lp_table[minor].dev->port, 0)==0) {
485 #ifdef LP_READ_DEBUG
486 printk(KERN_INFO "lp%d: rejected IEEE1284 negotiation.\n",
487 minor);
488 #endif
489 lp_select_in_high(minor);
490 parport_release(lp_table[minor].dev);
491 return temp-buf; /* End of file */
493 for (i=0; i<=(count*2); i++) {
494 parport_frob_control(lp_table[minor].dev->port, 2, 2); /* AutoFeed high */
495 do {
496 status = (r_str(minor) & 0x40);
497 udelay(50);
498 counter++;
499 if (current->need_resched)
500 schedule ();
501 } while ((status == 0x40) && (counter < 20));
502 if (counter == 20) {
503 /* Timeout */
504 #ifdef LP_READ_DEBUG
505 printk(KERN_DEBUG "lp_read: (Autofeed high) timeout\n");
506 #endif
507 parport_frob_control(lp_table[minor].dev->port, 2, 0);
508 lp_select_in_high(minor);
509 parport_release(lp_table[minor].dev);
510 return temp-buf; /* end the read at timeout */
512 counter=0;
513 z = lp_read_nibble(minor);
514 parport_frob_control(lp_table[minor].dev->port, 2, 0); /* AutoFeed low */
515 do {
516 status=(r_str(minor) & 0x40);
517 udelay(20);
518 counter++;
519 if (current->need_resched)
520 schedule ();
521 } while ( (status == 0) && (counter < 20) );
522 if (counter == 20) { /* Timeout */
523 #ifdef LP_READ_DEBUG
524 printk(KERN_DEBUG "lp_read: (Autofeed low) timeout\n");
525 #endif
526 if (signal_pending(current)) {
527 lp_select_in_high(minor);
528 parport_release(lp_table[minor].dev);
529 if (temp !=buf)
530 return temp-buf;
531 else
532 return -EINTR;
534 current->state=TASK_INTERRUPTIBLE;
535 schedule_timeout(LP_TIME(minor));
538 counter=0;
540 if (( i & 1) != 0) {
541 Byte= (Byte | z<<4);
542 if (__put_user(Byte, (char *)temp))
543 return -EFAULT;
544 temp++;
545 } else Byte=z;
548 lp_select_in_high(minor);
549 lp_parport_release(minor);
550 return temp-buf;
553 #endif
555 static int lp_open(struct inode * inode, struct file * file)
557 unsigned int minor = MINOR(inode->i_rdev);
559 if (minor >= LP_NO)
560 return -ENXIO;
561 if ((LP_F(minor) & LP_EXIST) == 0)
562 return -ENXIO;
563 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
564 return -EBUSY;
566 MOD_INC_USE_COUNT;
568 /* If ABORTOPEN is set and the printer is offline or out of paper,
569 we may still want to open it to perform ioctl()s. Therefore we
570 have commandeered O_NONBLOCK, even though it is being used in
571 a non-standard manner. This is strictly a Linux hack, and
572 should most likely only ever be used by the tunelp application. */
573 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
574 int status;
575 lp_parport_claim (minor);
576 status = r_str(minor);
577 lp_parport_release (minor);
578 if (status & LP_POUTPA) {
579 printk(KERN_INFO "lp%d out of paper\n", minor);
580 MOD_DEC_USE_COUNT;
581 LP_F(minor) &= ~LP_BUSY;
582 return -ENOSPC;
583 } else if (!(status & LP_PSELECD)) {
584 printk(KERN_INFO "lp%d off-line\n", minor);
585 MOD_DEC_USE_COUNT;
586 LP_F(minor) &= ~LP_BUSY;
587 return -EIO;
588 } else if (!(status & LP_PERRORP)) {
589 printk(KERN_ERR "lp%d printer error\n", minor);
590 MOD_DEC_USE_COUNT;
591 LP_F(minor) &= ~LP_BUSY;
592 return -EIO;
595 lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
596 if (!lp_table[minor].lp_buffer) {
597 MOD_DEC_USE_COUNT;
598 LP_F(minor) &= ~LP_BUSY;
599 return -ENOMEM;
601 return 0;
604 static int lp_release(struct inode * inode, struct file * file)
606 unsigned int minor = MINOR(inode->i_rdev);
608 kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
609 lp_table[minor].lp_buffer = NULL;
610 MOD_DEC_USE_COUNT;
611 LP_F(minor) &= ~LP_BUSY;
612 return 0;
615 static int lp_ioctl(struct inode *inode, struct file *file,
616 unsigned int cmd, unsigned long arg)
618 unsigned int minor = MINOR(inode->i_rdev);
619 int status;
620 int retval = 0;
622 #ifdef LP_DEBUG
623 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor, cmd, arg);
624 #endif
625 if (minor >= LP_NO)
626 return -ENODEV;
627 if ((LP_F(minor) & LP_EXIST) == 0)
628 return -ENODEV;
629 switch ( cmd ) {
630 case LPTIME:
631 LP_TIME(minor) = arg * HZ/100;
632 break;
633 case LPCHAR:
634 LP_CHAR(minor) = arg;
635 break;
636 case LPABORT:
637 if (arg)
638 LP_F(minor) |= LP_ABORT;
639 else
640 LP_F(minor) &= ~LP_ABORT;
641 break;
642 case LPABORTOPEN:
643 if (arg)
644 LP_F(minor) |= LP_ABORTOPEN;
645 else
646 LP_F(minor) &= ~LP_ABORTOPEN;
647 break;
648 #ifdef LP_NEED_CAREFUL
649 case LPCAREFUL:
650 if (arg)
651 LP_F(minor) |= LP_CAREFUL;
652 else
653 LP_F(minor) &= ~LP_CAREFUL;
654 break;
655 #endif
656 case LPWAIT:
657 LP_WAIT(minor) = arg;
658 break;
659 case LPSETIRQ:
660 return -EINVAL;
661 break;
662 case LPGETIRQ:
663 if (copy_to_user((int *) arg, &LP_IRQ(minor),
664 sizeof(int)))
665 return -EFAULT;
666 break;
667 case LPGETSTATUS:
668 lp_parport_claim(minor);
669 status = r_str(minor);
670 lp_parport_release(minor);
672 if (copy_to_user((int *) arg, &status, sizeof(int)))
673 return -EFAULT;
674 break;
675 case LPRESET:
676 lp_reset(minor);
677 break;
678 #ifdef LP_STATS
679 case LPGETSTATS:
680 if (copy_to_user((int *) arg, &LP_STAT(minor),
681 sizeof(struct lp_stats)))
682 return -EFAULT;
683 if (suser())
684 memset(&LP_STAT(minor), 0,
685 sizeof(struct lp_stats));
686 break;
687 #endif
688 case LPGETFLAGS:
689 status = LP_F(minor);
690 if (copy_to_user((int *) arg, &status, sizeof(int)))
691 return -EFAULT;
692 break;
693 default:
694 retval = -EINVAL;
696 return retval;
700 static struct file_operations lp_fops = {
701 lp_lseek,
702 #ifdef CONFIG_PRINTER_READBACK
703 lp_read,
704 #else
705 NULL,
706 #endif
707 lp_write,
708 NULL, /* lp_readdir */
709 NULL, /* lp_poll */
710 lp_ioctl,
711 NULL, /* lp_mmap */
712 lp_open,
713 NULL, /* flush */
714 lp_release
717 /* --- initialisation code ------------------------------------- */
719 #ifdef MODULE
721 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
722 static char *parport[LP_NO] = { NULL, };
723 static int reset = 0;
725 MODULE_PARM(parport, "1-" __MODULE_STRING(LP_NO) "s");
726 MODULE_PARM(reset, "i");
728 #else
730 static int parport_nr[LP_NO] __initdata = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
731 static int reset __initdata = 0;
733 static int parport_ptr = 0;
735 __initfunc(void lp_setup(char *str, int *ints))
737 if (!str) {
738 if (ints[0] == 0 || ints[1] == 0) {
739 /* disable driver on "lp=" or "lp=0" */
740 parport_nr[0] = LP_PARPORT_OFF;
741 } else {
742 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", ints[1]);
744 } else if (!strncmp(str, "parport", 7)) {
745 int n = simple_strtoul(str+7, NULL, 10);
746 if (parport_ptr < LP_NO)
747 parport_nr[parport_ptr++] = n;
748 else
749 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
750 str);
751 } else if (!strcmp(str, "auto")) {
752 parport_nr[0] = LP_PARPORT_AUTO;
753 } else if (!strcmp(str, "none")) {
754 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
755 } else if (!strcmp(str, "reset")) {
756 reset = 1;
760 #endif
762 int lp_register(int nr, struct parport *port)
764 lp_table[nr].dev = parport_register_device(port, "lp",
765 lp_preempt, NULL,
766 lp_interrupt,
768 (void *) &lp_table[nr]);
769 if (lp_table[nr].dev == NULL)
770 return 1;
771 lp_table[nr].flags |= LP_EXIST;
773 if (reset)
774 lp_reset(nr);
776 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
777 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
779 return 0;
782 int lp_init(void)
784 unsigned int count = 0;
785 unsigned int i;
786 struct parport *port;
788 switch (parport_nr[0])
790 case LP_PARPORT_OFF:
791 return 0;
793 case LP_PARPORT_UNSPEC:
794 case LP_PARPORT_AUTO:
795 for (port = parport_enumerate(); port; port = port->next) {
797 if (parport_nr[0] == LP_PARPORT_AUTO &&
798 port->probe_info.class != PARPORT_CLASS_PRINTER)
799 continue;
801 if (!lp_register(count, port))
802 if (++count == LP_NO)
803 break;
805 break;
807 default:
808 for (i = 0; i < LP_NO; i++) {
809 if (parport_nr[i] >= 0) {
810 char buffer[16];
811 sprintf(buffer, "parport%d", parport_nr[i]);
812 for (port = parport_enumerate(); port;
813 port = port->next) {
814 if (!strcmp(port->name, buffer)) {
815 (void) lp_register(i, port);
816 count++;
817 break;
822 break;
825 if (count) {
826 if (register_chrdev(LP_MAJOR, "lp", &lp_fops)) {
827 printk("lp: unable to get major %d\n", LP_MAJOR);
828 return -EIO;
830 } else {
831 printk(KERN_INFO "lp: driver loaded but no devices found\n");
834 return 0;
837 #ifdef MODULE
838 int init_module(void)
840 if (parport[0]) {
841 /* The user gave some parameters. Let's see what they were. */
842 if (!strncmp(parport[0], "auto", 4))
843 parport_nr[0] = LP_PARPORT_AUTO;
844 else {
845 int n;
846 for (n = 0; n < LP_NO && parport[n]; n++) {
847 if (!strncmp(parport[n], "none", 4))
848 parport_nr[n] = LP_PARPORT_NONE;
849 else {
850 char *ep;
851 unsigned long r = simple_strtoul(parport[n], &ep, 0);
852 if (ep != parport[n])
853 parport_nr[n] = r;
854 else {
855 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
856 return -ENODEV;
863 return lp_init();
866 void cleanup_module(void)
868 unsigned int offset;
870 unregister_chrdev(LP_MAJOR, "lp");
871 for (offset = 0; offset < LP_NO; offset++) {
872 if (lp_table[offset].dev == NULL)
873 continue;
874 parport_unregister_device(lp_table[offset].dev);
877 #endif