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
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
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:
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:
77 * ACK _______________ ___________
80 * BUSY _________ _______
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
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>
111 #include <linux/lp.h>
114 #include <asm/uaccess.h>
115 #include <asm/system.h>
117 /* if you have more than 3 printers, remember to increase LP_NO */
120 struct lp_struct lp_table
[LP_NO
] =
122 [0 ... LP_NO
-1] = {NULL
, 0, LP_INIT_CHAR
, LP_INIT_TIME
, LP_INIT_WAIT
,
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)
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 */
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
)
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;
190 lp_parport_claim(minor
);
195 static int lp_reset(int minor
)
198 lp_parport_claim (minor
);
199 w_ctr(minor
, LP_PSELECP
);
201 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
202 retval
= r_str(minor
);
203 lp_parport_release (minor
);
207 static inline void lp_wait(int minor
)
209 unsigned int wait
= 0;
211 /* FIXME: should be function(time) */
212 while (wait
++ != LP_WAIT(minor
));
219 static inline int lp_char(char lpchar
, int minor
)
221 unsigned long count
= 0;
223 struct lp_stats
*stats
;
226 if (signal_pending(current
))
231 unsigned char status
;
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
)) ||
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
))
256 w_dtr(minor
, lpchar
);
259 stats
= &LP_STAT(minor
);
264 * Epson Stylus Color generate the IRQ on the rising edge of
265 * strobe so clean the irq's information before playing with
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
276 /* must wait before taking strobe high, and after taking strobe
277 low, according spec. Some printers need it, others don't. */
280 /* control port takes strobe high */
281 if (LP_POLLED(minor
))
283 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
);
285 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
287 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
| LP_PINTEN
);
289 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PINTEN
);
293 /* update waittime statistics */
294 if (count
> stats
->maxwait
) {
296 printk(KERN_DEBUG
"lp%d success after %d counts.\n",
299 stats
->maxwait
= count
;
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;
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
);
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
) {
341 printk(KERN_INFO
"lp%d out of paper\n", minor
);
343 } else if (!(status
& LP_PSELECD
)) {
344 if (last
!= LP_PSELECD
) {
346 printk(KERN_INFO
"lp%d off-line\n", minor
);
348 } else if (!(status
& LP_PERRORP
)) {
349 if (last
!= LP_PERRORP
) {
351 printk(KERN_ERR
"lp%d on fire!\n", minor
);
356 lp_table
[minor
].last_error
= last
;
359 if (LP_F(minor
) & LP_ABORT
)
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
];
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
);
387 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PINTEN
);
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
);
400 if (lp_char(lp
->lp_buffer
[bytes_written
], minor
)) {
407 int rc
= total_bytes_written
+ bytes_written
;
410 if (lp
->runchars
> LP_STAT(minor
).maxrun
)
411 LP_STAT(minor
).maxrun
= lp
->runchars
;
412 LP_STAT(minor
).sleeps
++;
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
;
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
)
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
));
441 current
->state
= TASK_INTERRUPTIBLE
;
442 current
->timeout
= jiffies
+ LP_TIME(minor
);
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
458 if (!lp_table
[minor
].irq_detected
)
460 current
->timeout
= jiffies
+ LP_TIMEOUT_INTERRUPT
;
461 interruptible_sleep_on(&lp
->wait_q
);
468 total_bytes_written
+= bytes_written
;
469 buf
+= bytes_written
;
470 count
-= bytes_written
;
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
);
485 if (jiffies
-lp_table
[minor
].lastcall
> LP_TIME(minor
))
486 lp_table
[minor
].runchars
= 0;
488 lp_table
[minor
].lastcall
= jiffies
;
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
);
501 static long long lp_lseek(struct file
* file
, long long offset
, int origin
)
506 #ifdef CONFIG_PRINTER_READBACK
508 static int lp_read_nibble(int minor
)
513 if ((i
& 0x10) == 0) i
|= 8;
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
;
529 unsigned int counter
=0;
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
);
539 printk(KERN_INFO
"lp%d: read mode\n", minor
);
542 retval
= verify_area(VERIFY_WRITE
, buf
, count
);
545 if (parport_ieee1284_nibble_mode_ok(lp_table
[minor
].dev
->port
, 0)==0) {
547 printk(KERN_INFO
"lp%d: rejected IEEE1284 negotiation.\n",
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 */
557 status
= (r_str(minor
) & 0x40);
560 if (current
->need_resched
)
562 } while ((status
== 0x40) && (counter
< 20));
566 printk(KERN_DEBUG
"lp_read: (Autofeed high) timeout\n");
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 */
574 z
= lp_read_nibble(minor
);
575 parport_frob_control(lp_table
[minor
].dev
->port
, 2, 0); /* AutoFeed low */
577 status
=(r_str(minor
) & 0x40);
580 if (current
->need_resched
)
582 } while ( (status
== 0) && (counter
< 20) );
583 if (counter
== 20) { /* Timeout */
585 printk(KERN_DEBUG
"lp_read: (Autofeed low) timeout\n");
587 if (signal_pending(current
)) {
588 lp_select_in_high(minor
);
589 parport_release(lp_table
[minor
].dev
);
595 current
->state
=TASK_INTERRUPTIBLE
;
596 current
->timeout
=jiffies
+ LP_TIME(minor
);
604 if (__put_user(Byte
, (char *)temp
))
610 lp_select_in_high(minor
);
611 lp_parport_release(minor
);
617 static int lp_open(struct inode
* inode
, struct file
* file
)
619 unsigned int minor
= MINOR(inode
->i_rdev
);
623 if ((LP_F(minor
) & LP_EXIST
) == 0)
625 if (test_and_set_bit(LP_BUSY_BIT_POS
, &LP_F(minor
)))
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
)) {
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
);
643 LP_F(minor
) &= ~LP_BUSY
;
645 } else if (!(status
& LP_PSELECD
)) {
646 printk(KERN_INFO
"lp%d off-line\n", minor
);
648 LP_F(minor
) &= ~LP_BUSY
;
650 } else if (!(status
& LP_PERRORP
)) {
651 printk(KERN_ERR
"lp%d printer error\n", minor
);
653 LP_F(minor
) &= ~LP_BUSY
;
657 lp_table
[minor
].lp_buffer
= (char *) kmalloc(LP_BUFFER_SIZE
, GFP_KERNEL
);
658 if (!lp_table
[minor
].lp_buffer
) {
660 LP_F(minor
) &= ~LP_BUSY
;
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
;
673 LP_F(minor
) &= ~LP_BUSY
;
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
);
685 printk(KERN_DEBUG
"lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor
, cmd
, arg
);
689 if ((LP_F(minor
) & LP_EXIST
) == 0)
693 LP_TIME(minor
) = arg
* HZ
/100;
696 LP_CHAR(minor
) = arg
;
700 LP_F(minor
) |= LP_ABORT
;
702 LP_F(minor
) &= ~LP_ABORT
;
706 LP_F(minor
) |= LP_ABORTOPEN
;
708 LP_F(minor
) &= ~LP_ABORTOPEN
;
711 LP_WAIT(minor
) = arg
;
717 if (copy_to_user((int *) arg
, &LP_IRQ(minor
),
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)))
734 if (copy_to_user((int *) arg
, &LP_STAT(minor
),
735 sizeof(struct lp_stats
)))
738 memset(&LP_STAT(minor
), 0,
739 sizeof(struct lp_stats
));
743 status
= LP_F(minor
);
744 if (copy_to_user((int *) arg
, &status
, sizeof(int)))
754 static struct file_operations lp_fops
= {
756 #ifdef CONFIG_PRINTER_READBACK
762 NULL
, /* lp_readdir */
771 /* --- initialisation code ------------------------------------- */
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");
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
))
792 if (ints
[0] == 0 || ints
[1] == 0) {
793 /* disable driver on "lp=" or "lp=0" */
794 parport_nr
[0] = LP_PARPORT_OFF
;
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
;
803 printk(KERN_INFO
"lp: too many ports, %s ignored.\n",
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")) {
816 int lp_register(int nr
, struct parport
*port
)
818 lp_table
[nr
].dev
= parport_register_device(port
, "lp",
822 (void *) &lp_table
[nr
]);
823 if (lp_table
[nr
].dev
== NULL
)
825 lp_table
[nr
].flags
|= LP_EXIST
;
830 printk(KERN_INFO
"lp%d: using %s (%s).\n", nr
, port
->name
,
831 (port
->irq
== PARPORT_IRQ_NONE
)?"polling":"interrupt-driven");
838 unsigned int count
= 0;
840 struct parport
*port
;
842 switch (parport_nr
[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
)
855 if (!lp_register(count
, port
))
856 if (++count
== LP_NO
)
862 for (i
= 0; i
< LP_NO
; i
++) {
863 if (parport_nr
[i
] >= 0) {
865 sprintf(buffer
, "parport%d", parport_nr
[i
]);
866 for (port
= parport_enumerate(); port
;
868 if (!strcmp(port
->name
, buffer
)) {
869 (void) lp_register(i
, port
);
880 if (register_chrdev(LP_MAJOR
, "lp", &lp_fops
)) {
881 printk("lp: unable to get major %d\n", LP_MAJOR
);
885 printk(KERN_INFO
"lp: driver loaded but no devices found\n");
892 int init_module(void)
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
;
900 for (n
= 0; n
< LP_NO
&& parport
[n
]; n
++) {
901 if (!strncmp(parport
[n
], "none", 4))
902 parport_nr
[n
] = LP_PARPORT_NONE
;
905 unsigned long r
= simple_strtoul(parport
[n
], &ep
, 0);
906 if (ep
!= parport
[n
])
909 printk(KERN_ERR
"lp: bad port specifier `%s'\n", parport
[n
]);
920 void cleanup_module(void)
924 unregister_chrdev(LP_MAJOR
, "lp");
925 for (offset
= 0; offset
< LP_NO
; offset
++) {
926 if (lp_table
[offset
].dev
== NULL
)
928 parport_unregister_device(lp_table
[offset
].dev
);