Import 2.1.127pre3
[davej-history.git] / drivers / char / lp.c
blobdc26e44c3b29a5fded379f723c46cfb514fa3099
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.
25 * I also fixed the irq on the rising edge of the strobe problem.
26 * Andrea Arcangeli, 15 Oct 1998
29 /* This driver should, in theory, work with any parallel port that has an
30 * appropriate low-level driver; all I/O is done through the parport
31 * abstraction layer.
33 * If this driver is built into the kernel, you can configure it using the
34 * kernel command-line. For example:
36 * lp=parport1,none,parport2 (bind lp0 to parport1, disable lp1 and
37 * bind lp2 to parport2)
39 * lp=auto (assign lp devices to all ports that
40 * have printers attached, as determined
41 * by the IEEE-1284 autoprobe)
43 * lp=reset (reset the printer during
44 * initialisation)
46 * lp=off (disable the printer driver entirely)
48 * If the driver is loaded as a module, similar functionality is available
49 * using module parameters. The equivalent of the above commands would be:
51 * # insmod lp.o parport=1,none,2
53 * # insmod lp.o parport=auto
55 * # insmod lp.o reset=1
58 /* COMPATIBILITY WITH OLD KERNELS
60 * Under Linux 2.0 and previous versions, lp devices were bound to ports at
61 * particular I/O addresses, as follows:
63 * lp0 0x3bc
64 * lp1 0x378
65 * lp2 0x278
67 * The new driver, by default, binds lp devices to parport devices as it
68 * finds them. This means that if you only have one port, it will be bound
69 * to lp0 regardless of its I/O address. If you need the old behaviour, you
70 * can force it using the parameters described above.
74 * The new interrupt handling code take care of the buggy handshake
75 * of some HP and Epson printer:
76 * ___
77 * ACK _______________ ___________
78 * |__|
79 * ____
80 * BUSY _________ _______
81 * |____________|
83 * I discovered this using the printer scanner that you can find at:
85 * ftp://e-mind.com/pub/linux/pscan/
87 * My printer scanner run on an Epson Stylus Color show that such printer
88 * generates the irq on the _rising_ edge of the STROBE. Now lp handle
89 * this case fine too.
91 * I also understood that on such printer we are just allowed to send
92 * new characters after the interrupt even if the BUSY line is still active.
94 * 15 Oct 1998, Andrea Arcangeli
97 #include <linux/module.h>
98 #include <linux/init.h>
100 #include <linux/config.h>
101 #include <linux/errno.h>
102 #include <linux/kernel.h>
103 #include <linux/major.h>
104 #include <linux/sched.h>
105 #include <linux/malloc.h>
106 #include <linux/fcntl.h>
107 #include <linux/delay.h>
109 #include <linux/parport.h>
110 #undef LP_STATS
111 #include <linux/lp.h>
113 #include <asm/irq.h>
114 #include <asm/uaccess.h>
115 #include <asm/system.h>
117 /* if you have more than 3 printers, remember to increase LP_NO */
118 #define LP_NO 3
120 struct lp_struct lp_table[LP_NO] =
122 [0 ... LP_NO-1] = {NULL, 0, LP_INIT_CHAR, LP_INIT_TIME, LP_INIT_WAIT,
123 NULL,
124 #ifdef LP_STATS
125 0, 0, {0},
126 #endif
127 NULL, 0, 0, 0}
131 * Test if printer is ready.
133 #define LP_READY(status) \
134 ((status) & (LP_PBUSY|LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
135 (LP_PBUSY|LP_PSELECD|LP_PERRORP)
138 * Test if the printer has error conditions.
140 #define LP_NO_ERROR(status) \
141 ((status) & (LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
142 (LP_PSELECD|LP_PERRORP)
144 #define LP_NO_ACKING(status) ((status) & LP_PACK)
146 #undef LP_DEBUG
147 #undef LP_READ_DEBUG
149 /* --- parport support ----------------------------------------- */
151 static int lp_preempt(void *handle)
153 struct lp_struct *lps = (struct lp_struct *)handle;
155 if (waitqueue_active (&lps->wait_q))
156 wake_up_interruptible(&lps->wait_q);
158 /* Don't actually release the port now */
159 return 1;
162 #define lp_parport_release(x) do { parport_release(lp_table[(x)].dev); } while (0);
163 #define lp_parport_claim(x) do { parport_claim_or_block(lp_table[(x)].dev); } while (0);
165 /* --- low-level port access ----------------------------------- */
167 #define r_dtr(x) (parport_read_data(lp_table[(x)].dev->port))
168 #define r_str(x) (parport_read_status(lp_table[(x)].dev->port))
169 #define w_ctr(x,y) do { parport_write_control(lp_table[(x)].dev->port, (y)); } while (0)
170 #define w_dtr(x,y) do { parport_write_data(lp_table[(x)].dev->port, (y)); } while (0)
172 static __inline__ void lp_yield (int minor)
174 if (!parport_yield_blocking (lp_table[minor].dev))
176 if (current->need_resched)
177 schedule ();
178 } else
179 lp_table[minor].irq_missed = 1;
182 static __inline__ void lp_schedule(int minor)
184 struct pardevice *dev = lp_table[minor].dev;
185 register unsigned long int timeslip = (jiffies - dev->time);
186 if ((timeslip > dev->timeslice) && (dev->port->waithead != NULL)) {
187 lp_parport_release(minor);
188 lp_table[minor].irq_missed = 1;
189 schedule ();
190 lp_parport_claim(minor);
191 } else
192 schedule();
195 static int lp_reset(int minor)
197 int retval;
198 lp_parport_claim (minor);
199 w_ctr(minor, LP_PSELECP);
200 udelay (LP_DELAY);
201 w_ctr(minor, LP_PSELECP | LP_PINITP);
202 retval = r_str(minor);
203 lp_parport_release (minor);
204 return retval;
207 static inline void lp_wait(int minor)
209 unsigned int wait = 0;
210 #ifndef __sparc__
211 /* FIXME: should be function(time) */
212 while (wait++ != LP_WAIT(minor));
213 #else
214 udelay(1);
215 #endif
219 static inline int lp_char(char lpchar, int minor)
221 unsigned long count = 0;
222 #ifdef LP_STATS
223 struct lp_stats *stats;
224 #endif
226 if (signal_pending(current))
227 return 0;
229 for (;;)
231 unsigned char status;
232 lp_yield(minor);
234 status = r_str(minor);
236 * On Epson Stylus Color we must continue even if LP_READY()
237 * is false to be efficient. This way is backwards
238 * compatible with old not-buggy printers. -arca
240 if (LP_NO_ERROR(status) &&
241 ((lp_table[minor].irq_detected && LP_NO_ACKING(status)) ||
242 LP_READY(status)))
243 break;
245 * To have a chance to sleep on the interrupt we should break
246 * the polling loop ASAP. Unfortunately there seems to be
247 * some hardware that underperform so we leave this
248 * configurable at runtime. So when printing with irqs
249 * `tunelp /dev/lp0 -c 1' is a must to take the full
250 * advantage of the irq. -arca
252 if (++count == LP_CHAR(minor))
253 return 0;
256 w_dtr(minor, lpchar);
258 #ifdef LP_STATS
259 stats = &LP_STAT(minor);
260 stats->chars++;
261 #endif
264 * Epson Stylus Color generate the IRQ on the rising edge of
265 * strobe so clean the irq's information before playing with
266 * the strobe. -arca
268 lp_table[minor].irq_detected = 0;
269 lp_table[minor].irq_missed = 0;
271 * Be sure that the CPU doesn' t reorder instruction. I am not sure
272 * if it' s needed also before an outb(). If not tell me ;-). -arca
274 mb();
276 /* must wait before taking strobe high, and after taking strobe
277 low, according spec. Some printers need it, others don't. */
278 lp_wait(minor);
280 /* control port takes strobe high */
281 if (LP_POLLED(minor))
283 w_ctr(minor, LP_PSELECP | LP_PINITP | LP_PSTROBE);
284 lp_wait(minor);
285 w_ctr(minor, LP_PSELECP | LP_PINITP);
286 } else {
287 w_ctr(minor, LP_PSELECP | LP_PINITP | LP_PSTROBE | LP_PINTEN);
288 lp_wait(minor);
289 w_ctr(minor, LP_PSELECP | LP_PINITP | LP_PINTEN);
292 #ifdef LP_STATS
293 /* update waittime statistics */
294 if (count > stats->maxwait) {
295 #ifdef LP_DEBUG
296 printk(KERN_DEBUG "lp%d success after %d counts.\n",
297 minor, count);
298 #endif
299 stats->maxwait = count;
301 count *= 256;
302 wait = (count > stats->meanwait) ? count - stats->meanwait :
303 stats->meanwait - count;
304 stats->meanwait = (255 * stats->meanwait + count + 128) / 256;
305 stats->mdev = ((127 * stats->mdev) + wait + 64) / 128;
306 #endif
308 return 1;
311 static void lp_interrupt(int irq, void *dev_id, struct pt_regs *regs)
313 struct lp_struct *lp_dev = (struct lp_struct *) dev_id;
315 if (waitqueue_active (&lp_dev->wait_q))
316 wake_up_interruptible(&lp_dev->wait_q);
318 lp_dev->irq_detected = 1;
319 lp_dev->irq_missed = 0;
322 static void lp_error(int minor)
324 if (LP_POLLED(minor) || LP_PREEMPTED(minor)) {
325 current->state = TASK_INTERRUPTIBLE;
326 current->timeout = jiffies + LP_TIMEOUT_POLLED;
327 lp_parport_release(minor);
328 schedule();
329 lp_parport_claim(minor);
330 lp_table[minor].irq_missed = 1;
334 static int lp_check_status(int minor)
336 unsigned int last = lp_table[minor].last_error;
337 unsigned char status = r_str(minor);
338 if ((status & LP_POUTPA)) {
339 if (last != LP_POUTPA) {
340 last = LP_POUTPA;
341 printk(KERN_INFO "lp%d out of paper\n", minor);
343 } else if (!(status & LP_PSELECD)) {
344 if (last != LP_PSELECD) {
345 last = LP_PSELECD;
346 printk(KERN_INFO "lp%d off-line\n", minor);
348 } else if (!(status & LP_PERRORP)) {
349 if (last != LP_PERRORP) {
350 last = LP_PERRORP;
351 printk(KERN_ERR "lp%d on fire!\n", minor);
354 else last = 0;
356 lp_table[minor].last_error = last;
358 if (last != 0) {
359 if (LP_F(minor) & LP_ABORT)
360 return 1;
361 lp_error(minor);
364 return 0;
367 static int lp_write_buf(unsigned int minor, const char *buf, int count)
369 unsigned long copy_size;
370 unsigned long total_bytes_written = 0;
371 unsigned long bytes_written;
372 struct lp_struct *lp = &lp_table[minor];
374 if (minor >= LP_NO)
375 return -ENXIO;
376 if (lp->dev == NULL)
377 return -ENXIO;
379 lp_table[minor].last_error = 0;
380 lp_table[minor].irq_detected = 0;
381 lp_table[minor].irq_missed = 1;
382 LP_POLLED(minor) = lp_table[minor].dev->port->irq == PARPORT_IRQ_NONE;
384 if (LP_POLLED(minor))
385 w_ctr(minor, LP_PSELECP | LP_PINITP);
386 else
387 w_ctr(minor, LP_PSELECP | LP_PINITP | LP_PINTEN);
389 do {
390 bytes_written = 0;
391 copy_size = (count <= LP_BUFFER_SIZE ? count : LP_BUFFER_SIZE);
393 if (copy_from_user(lp->lp_buffer, buf, copy_size))
395 w_ctr(minor, LP_PSELECP | LP_PINITP);
396 return -EFAULT;
399 while (copy_size) {
400 if (lp_char(lp->lp_buffer[bytes_written], minor)) {
401 --copy_size;
402 ++bytes_written;
403 #ifdef LP_STATS
404 lp->runchars++;
405 #endif
406 } else {
407 int rc = total_bytes_written + bytes_written;
409 #ifdef LP_STATS
410 if (lp->runchars > LP_STAT(minor).maxrun)
411 LP_STAT(minor).maxrun = lp->runchars;
412 LP_STAT(minor).sleeps++;
413 #endif
415 if (signal_pending(current))
417 w_ctr(minor, LP_PSELECP | LP_PINITP);
418 if (total_bytes_written + bytes_written)
419 return total_bytes_written + bytes_written;
420 else
421 return -EINTR;
424 #ifdef LP_STATS
425 lp->runchars = 0;
426 #endif
428 if (lp_check_status(minor))
430 w_ctr(minor, LP_PSELECP | LP_PINITP);
431 return rc ? rc : -EIO;
434 if (LP_POLLED(minor) ||
435 lp_table[minor].irq_missed)
437 lp_polling:
438 #if defined(LP_DEBUG) && defined(LP_STATS)
439 printk(KERN_DEBUG "lp%d sleeping at %d characters for %d jiffies\n", minor, lp->runchars, LP_TIME(minor));
440 #endif
441 current->state = TASK_INTERRUPTIBLE;
442 current->timeout = jiffies + LP_TIME(minor);
443 lp_schedule (minor);
444 } else {
445 cli();
446 if (LP_PREEMPTED(minor))
449 * We can' t sleep on the interrupt
450 * since another pardevice need the port.
451 * We must check this in a cli() protected
452 * envinroment to avoid parport sharing
453 * starvation.
455 sti();
456 goto lp_polling;
458 if (!lp_table[minor].irq_detected)
460 current->timeout = jiffies + LP_TIMEOUT_INTERRUPT;
461 interruptible_sleep_on(&lp->wait_q);
463 sti();
468 total_bytes_written += bytes_written;
469 buf += bytes_written;
470 count -= bytes_written;
472 } while (count > 0);
474 w_ctr(minor, LP_PSELECP | LP_PINITP);
475 return total_bytes_written;
478 static ssize_t lp_write(struct file * file, const char * buf,
479 size_t count, loff_t *ppos)
481 unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
482 ssize_t retv;
484 #ifdef LP_STATS
485 if (jiffies-lp_table[minor].lastcall > LP_TIME(minor))
486 lp_table[minor].runchars = 0;
488 lp_table[minor].lastcall = jiffies;
489 #endif
491 /* Claim Parport or sleep until it becomes available
493 lp_parport_claim (minor);
495 retv = lp_write_buf(minor, buf, count);
497 lp_parport_release (minor);
498 return retv;
501 static long long lp_lseek(struct file * file, long long offset, int origin)
503 return -ESPIPE;
506 #ifdef CONFIG_PRINTER_READBACK
508 static int lp_read_nibble(int minor)
510 unsigned char i;
511 i = r_str(minor)>>3;
512 i &= ~8;
513 if ((i & 0x10) == 0) i |= 8;
514 return (i & 0x0f);
517 static inline void lp_select_in_high(int minor)
519 parport_frob_control(lp_table[minor].dev->port, 8, 8);
522 /* Status readback confirming to ieee1284 */
523 static ssize_t lp_read(struct file * file, char * buf,
524 size_t count, loff_t *ppos)
526 unsigned char z=0, Byte=0, status;
527 char *temp;
528 ssize_t retval;
529 unsigned int counter=0;
530 unsigned int i;
531 unsigned int minor=MINOR(file->f_dentry->d_inode->i_rdev);
533 /* Claim Parport or sleep until it becomes available
535 lp_parport_claim (minor);
537 temp=buf;
538 #ifdef LP_READ_DEBUG
539 printk(KERN_INFO "lp%d: read mode\n", minor);
540 #endif
542 retval = verify_area(VERIFY_WRITE, buf, count);
543 if (retval)
544 return retval;
545 if (parport_ieee1284_nibble_mode_ok(lp_table[minor].dev->port, 0)==0) {
546 #ifdef LP_READ_DEBUG
547 printk(KERN_INFO "lp%d: rejected IEEE1284 negotiation.\n",
548 minor);
549 #endif
550 lp_select_in_high(minor);
551 parport_release(lp_table[minor].dev);
552 return temp-buf; /* End of file */
554 for (i=0; i<=(count*2); i++) {
555 parport_frob_control(lp_table[minor].dev->port, 2, 2); /* AutoFeed high */
556 do {
557 status = (r_str(minor) & 0x40);
558 udelay(50);
559 counter++;
560 if (current->need_resched)
561 schedule ();
562 } while ((status == 0x40) && (counter < 20));
563 if (counter == 20) {
564 /* Timeout */
565 #ifdef LP_READ_DEBUG
566 printk(KERN_DEBUG "lp_read: (Autofeed high) timeout\n");
567 #endif
568 parport_frob_control(lp_table[minor].dev->port, 2, 0);
569 lp_select_in_high(minor);
570 parport_release(lp_table[minor].dev);
571 return temp-buf; /* end the read at timeout */
573 counter=0;
574 z = lp_read_nibble(minor);
575 parport_frob_control(lp_table[minor].dev->port, 2, 0); /* AutoFeed low */
576 do {
577 status=(r_str(minor) & 0x40);
578 udelay(20);
579 counter++;
580 if (current->need_resched)
581 schedule ();
582 } while ( (status == 0) && (counter < 20) );
583 if (counter == 20) { /* Timeout */
584 #ifdef LP_READ_DEBUG
585 printk(KERN_DEBUG "lp_read: (Autofeed low) timeout\n");
586 #endif
587 if (signal_pending(current)) {
588 lp_select_in_high(minor);
589 parport_release(lp_table[minor].dev);
590 if (temp !=buf)
591 return temp-buf;
592 else
593 return -EINTR;
595 current->state=TASK_INTERRUPTIBLE;
596 current->timeout=jiffies + LP_TIME(minor);
597 schedule ();
600 counter=0;
602 if (( i & 1) != 0) {
603 Byte= (Byte | z<<4);
604 if (__put_user(Byte, (char *)temp))
605 return -EFAULT;
606 temp++;
607 } else Byte=z;
610 lp_select_in_high(minor);
611 lp_parport_release(minor);
612 return temp-buf;
615 #endif
617 static int lp_open(struct inode * inode, struct file * file)
619 unsigned int minor = MINOR(inode->i_rdev);
621 if (minor >= LP_NO)
622 return -ENXIO;
623 if ((LP_F(minor) & LP_EXIST) == 0)
624 return -ENXIO;
625 if (test_and_set_bit(LP_BUSY_BIT_POS, &LP_F(minor)))
626 return -EBUSY;
628 MOD_INC_USE_COUNT;
630 /* If ABORTOPEN is set and the printer is offline or out of paper,
631 we may still want to open it to perform ioctl()s. Therefore we
632 have commandeered O_NONBLOCK, even though it is being used in
633 a non-standard manner. This is strictly a Linux hack, and
634 should most likely only ever be used by the tunelp application. */
635 if ((LP_F(minor) & LP_ABORTOPEN) && !(file->f_flags & O_NONBLOCK)) {
636 int status;
637 lp_parport_claim (minor);
638 status = r_str(minor);
639 lp_parport_release (minor);
640 if (status & LP_POUTPA) {
641 printk(KERN_INFO "lp%d out of paper\n", minor);
642 MOD_DEC_USE_COUNT;
643 LP_F(minor) &= ~LP_BUSY;
644 return -ENOSPC;
645 } else if (!(status & LP_PSELECD)) {
646 printk(KERN_INFO "lp%d off-line\n", minor);
647 MOD_DEC_USE_COUNT;
648 LP_F(minor) &= ~LP_BUSY;
649 return -EIO;
650 } else if (!(status & LP_PERRORP)) {
651 printk(KERN_ERR "lp%d printer error\n", minor);
652 MOD_DEC_USE_COUNT;
653 LP_F(minor) &= ~LP_BUSY;
654 return -EIO;
657 lp_table[minor].lp_buffer = (char *) kmalloc(LP_BUFFER_SIZE, GFP_KERNEL);
658 if (!lp_table[minor].lp_buffer) {
659 MOD_DEC_USE_COUNT;
660 LP_F(minor) &= ~LP_BUSY;
661 return -ENOMEM;
663 return 0;
666 static int lp_release(struct inode * inode, struct file * file)
668 unsigned int minor = MINOR(inode->i_rdev);
670 kfree_s(lp_table[minor].lp_buffer, LP_BUFFER_SIZE);
671 lp_table[minor].lp_buffer = NULL;
672 MOD_DEC_USE_COUNT;
673 LP_F(minor) &= ~LP_BUSY;
674 return 0;
677 static int lp_ioctl(struct inode *inode, struct file *file,
678 unsigned int cmd, unsigned long arg)
680 unsigned int minor = MINOR(inode->i_rdev);
681 int status;
682 int retval = 0;
684 #ifdef LP_DEBUG
685 printk(KERN_DEBUG "lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor, cmd, arg);
686 #endif
687 if (minor >= LP_NO)
688 return -ENODEV;
689 if ((LP_F(minor) & LP_EXIST) == 0)
690 return -ENODEV;
691 switch ( cmd ) {
692 case LPTIME:
693 LP_TIME(minor) = arg * HZ/100;
694 break;
695 case LPCHAR:
696 LP_CHAR(minor) = arg;
697 break;
698 case LPABORT:
699 if (arg)
700 LP_F(minor) |= LP_ABORT;
701 else
702 LP_F(minor) &= ~LP_ABORT;
703 break;
704 case LPABORTOPEN:
705 if (arg)
706 LP_F(minor) |= LP_ABORTOPEN;
707 else
708 LP_F(minor) &= ~LP_ABORTOPEN;
709 break;
710 case LPWAIT:
711 LP_WAIT(minor) = arg;
712 break;
713 case LPSETIRQ:
714 return -EINVAL;
715 break;
716 case LPGETIRQ:
717 if (copy_to_user((int *) arg, &LP_IRQ(minor),
718 sizeof(int)))
719 return -EFAULT;
720 break;
721 case LPGETSTATUS:
722 lp_parport_claim(minor);
723 status = r_str(minor);
724 lp_parport_release(minor);
726 if (copy_to_user((int *) arg, &status, sizeof(int)))
727 return -EFAULT;
728 break;
729 case LPRESET:
730 lp_reset(minor);
731 break;
732 #ifdef LP_STATS
733 case LPGETSTATS:
734 if (copy_to_user((int *) arg, &LP_STAT(minor),
735 sizeof(struct lp_stats)))
736 return -EFAULT;
737 if (suser())
738 memset(&LP_STAT(minor), 0,
739 sizeof(struct lp_stats));
740 break;
741 #endif
742 case LPGETFLAGS:
743 status = LP_F(minor);
744 if (copy_to_user((int *) arg, &status, sizeof(int)))
745 return -EFAULT;
746 break;
747 default:
748 retval = -EINVAL;
750 return retval;
754 static struct file_operations lp_fops = {
755 lp_lseek,
756 #ifdef CONFIG_PRINTER_READBACK
757 lp_read,
758 #else
759 NULL,
760 #endif
761 lp_write,
762 NULL, /* lp_readdir */
763 NULL, /* lp_poll */
764 lp_ioctl,
765 NULL, /* lp_mmap */
766 lp_open,
767 NULL, /* flush */
768 lp_release
771 /* --- initialisation code ------------------------------------- */
773 #ifdef MODULE
775 static int parport_nr[LP_NO] = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
776 static char *parport[LP_NO] = { NULL, };
777 static int reset = 0;
779 MODULE_PARM(parport, "1-" __MODULE_STRING(LP_NO) "s");
780 MODULE_PARM(reset, "i");
782 #else
784 static int parport_nr[LP_NO] __initdata = { [0 ... LP_NO-1] = LP_PARPORT_UNSPEC };
785 static int reset __initdata = 0;
787 static int parport_ptr = 0;
789 __initfunc(void lp_setup(char *str, int *ints))
791 if (!str) {
792 if (ints[0] == 0 || ints[1] == 0) {
793 /* disable driver on "lp=" or "lp=0" */
794 parport_nr[0] = LP_PARPORT_OFF;
795 } else {
796 printk(KERN_WARNING "warning: 'lp=0x%x' is deprecated, ignored\n", ints[1]);
798 } else if (!strncmp(str, "parport", 7)) {
799 int n = simple_strtoul(str+7, NULL, 10);
800 if (parport_ptr < LP_NO)
801 parport_nr[parport_ptr++] = n;
802 else
803 printk(KERN_INFO "lp: too many ports, %s ignored.\n",
804 str);
805 } else if (!strcmp(str, "auto")) {
806 parport_nr[0] = LP_PARPORT_AUTO;
807 } else if (!strcmp(str, "none")) {
808 parport_nr[parport_ptr++] = LP_PARPORT_NONE;
809 } else if (!strcmp(str, "reset")) {
810 reset = 1;
814 #endif
816 int lp_register(int nr, struct parport *port)
818 lp_table[nr].dev = parport_register_device(port, "lp",
819 lp_preempt, NULL,
820 lp_interrupt,
822 (void *) &lp_table[nr]);
823 if (lp_table[nr].dev == NULL)
824 return 1;
825 lp_table[nr].flags |= LP_EXIST;
827 if (reset)
828 lp_reset(nr);
830 printk(KERN_INFO "lp%d: using %s (%s).\n", nr, port->name,
831 (port->irq == PARPORT_IRQ_NONE)?"polling":"interrupt-driven");
833 return 0;
836 int lp_init(void)
838 unsigned int count = 0;
839 unsigned int i;
840 struct parport *port;
842 switch (parport_nr[0])
844 case LP_PARPORT_OFF:
845 return 0;
847 case LP_PARPORT_UNSPEC:
848 case LP_PARPORT_AUTO:
849 for (port = parport_enumerate(); port; port = port->next) {
851 if (parport_nr[0] == LP_PARPORT_AUTO &&
852 port->probe_info.class != PARPORT_CLASS_PRINTER)
853 continue;
855 if (!lp_register(count, port))
856 if (++count == LP_NO)
857 break;
859 break;
861 default:
862 for (i = 0; i < LP_NO; i++) {
863 if (parport_nr[i] >= 0) {
864 char buffer[16];
865 sprintf(buffer, "parport%d", parport_nr[i]);
866 for (port = parport_enumerate(); port;
867 port = port->next) {
868 if (!strcmp(port->name, buffer)) {
869 (void) lp_register(i, port);
870 count++;
871 break;
876 break;
879 if (count) {
880 if (register_chrdev(LP_MAJOR, "lp", &lp_fops)) {
881 printk("lp: unable to get major %d\n", LP_MAJOR);
882 return -EIO;
884 } else {
885 printk(KERN_INFO "lp: driver loaded but no devices found\n");
888 return 0;
891 #ifdef MODULE
892 int init_module(void)
894 if (parport[0]) {
895 /* The user gave some parameters. Let's see what they were. */
896 if (!strncmp(parport[0], "auto", 4))
897 parport_nr[0] = LP_PARPORT_AUTO;
898 else {
899 int n;
900 for (n = 0; n < LP_NO && parport[n]; n++) {
901 if (!strncmp(parport[n], "none", 4))
902 parport_nr[n] = LP_PARPORT_NONE;
903 else {
904 char *ep;
905 unsigned long r = simple_strtoul(parport[n], &ep, 0);
906 if (ep != parport[n])
907 parport_nr[n] = r;
908 else {
909 printk(KERN_ERR "lp: bad port specifier `%s'\n", parport[n]);
910 return -ENODEV;
917 return lp_init();
920 void cleanup_module(void)
922 unsigned int offset;
924 unregister_chrdev(LP_MAJOR, "lp");
925 for (offset = 0; offset < LP_NO; offset++) {
926 if (lp_table[offset].dev == NULL)
927 continue;
928 parport_unregister_device(lp_table[offset].dev);
931 #endif