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 <arcangeli@mbox.queen.it>
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
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
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:
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:
71 * ACK _______________ ___________
74 * BUSY _________ _______
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>
98 #undef LP_NEED_CAREFUL
102 #include <asm/uaccess.h>
103 #include <asm/system.h>
105 /* if you have more than 3 printers, remember to increase LP_NO */
108 struct lp_struct lp_table
[LP_NO
] =
110 [0 ... LP_NO
-1] = {NULL
, 0, LP_INIT_CHAR
, LP_INIT_TIME
, LP_INIT_WAIT
,
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)
126 #define LP_READY(minor, status) ((status) & LP_PBUSY)
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 */
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
)
162 lp_table
[minor
].irq_missed
= 1;
165 static __inline__
void lp_schedule(int minor
)
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;
173 lp_parport_claim(minor
);
178 static int lp_reset(int minor
)
181 lp_parport_claim (minor
);
182 w_ctr(minor
, LP_PSELECP
);
184 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
185 retval
= r_str(minor
);
186 lp_parport_release (minor
);
190 static inline int lp_char(char lpchar
, int minor
)
192 unsigned int wait
= 0;
193 unsigned long count
= 0;
195 struct lp_stats
*stats
;
201 if (LP_READY(minor
, r_str(minor
)))
203 if (++count
== LP_CHAR(minor
) || signal_pending(current
))
207 w_dtr(minor
, lpchar
);
209 stats
= &LP_STAT(minor
);
212 /* must wait before taking strobe high, and after taking strobe
213 low, according spec. Some printers need it, others don't. */
215 while (wait
!= LP_WAIT(minor
)) /* FIXME: should be a udelay() */
220 /* control port takes strobe high */
221 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
);
223 while (wait
) /* FIXME: should be a udelay() */
228 /* take strobe low */
229 if (LP_POLLED(minor
))
230 /* take strobe low */
231 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
234 lp_table
[minor
].irq_detected
= 0;
235 lp_table
[minor
].irq_missed
= 0;
236 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PINTEN
);
240 /* update waittime statistics */
241 if (count
> stats
->maxwait
) {
243 printk(KERN_DEBUG
"lp%d success after %d counts.\n", minor
, count
);
245 stats
->maxwait
= count
;
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;
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 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
273 lp_parport_release(minor
);
275 lp_parport_claim(minor
);
276 lp_table
[minor
].irq_missed
= 1;
280 static int lp_check_status(int minor
)
282 unsigned int last
= lp_table
[minor
].last_error
;
283 unsigned char status
= r_str(minor
);
284 if ((status
& LP_POUTPA
)) {
285 if (last
!= LP_POUTPA
) {
287 printk(KERN_INFO
"lp%d out of paper\n", minor
);
289 } else if (!(status
& LP_PSELECD
)) {
290 if (last
!= LP_PSELECD
) {
292 printk(KERN_INFO
"lp%d off-line\n", minor
);
294 } else if (!(status
& LP_PERRORP
)) {
295 if (last
!= LP_PERRORP
) {
297 printk(KERN_ERR
"lp%d on fire!\n", minor
);
302 lp_table
[minor
].last_error
= last
;
305 if (LP_F(minor
) & LP_ABORT
)
313 static int lp_write_buf(unsigned int minor
, const char *buf
, int count
)
315 unsigned long copy_size
;
316 unsigned long total_bytes_written
= 0;
317 unsigned long bytes_written
;
318 struct lp_struct
*lp
= &lp_table
[minor
];
325 lp_table
[minor
].last_error
= 0;
326 lp_table
[minor
].irq_detected
= 0;
327 lp_table
[minor
].irq_missed
= 1;
329 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
333 copy_size
= (count
<= LP_BUFFER_SIZE
? count
: LP_BUFFER_SIZE
);
335 if (copy_from_user(lp
->lp_buffer
, buf
, copy_size
))
337 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
342 if (lp_char(lp
->lp_buffer
[bytes_written
], minor
)) {
349 int rc
= total_bytes_written
+ bytes_written
;
352 if (lp
->runchars
> LP_STAT(minor
).maxrun
)
353 LP_STAT(minor
).maxrun
= lp
->runchars
;
354 LP_STAT(minor
).sleeps
++;
357 if (signal_pending(current
))
359 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
360 if (total_bytes_written
+ bytes_written
)
361 return total_bytes_written
+ bytes_written
;
370 if (lp_check_status(minor
))
372 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
373 return rc
? rc
: -EIO
;
376 if (LP_POLLED(minor
) ||
377 lp_table
[minor
].irq_missed
)
380 #if defined(LP_DEBUG) && defined(LP_STATS)
381 printk(KERN_DEBUG
"lp%d sleeping at %d characters for %d jiffies\n", minor
, lp
->runchars
, LP_TIME(minor
));
383 current
->state
= TASK_INTERRUPTIBLE
;
384 current
->timeout
= jiffies
+ LP_TIME(minor
);
388 if (LP_PREEMPTED(minor
))
391 * We can' t sleep on the interrupt
392 * since another pardevice need the port.
393 * We must check this in a cli() protected
394 * envinroment to avoid parport sharing
400 if (!lp_table
[minor
].irq_detected
)
402 current
->timeout
= jiffies
+ LP_TIMEOUT_INTERRUPT
;
403 interruptible_sleep_on(&lp
->wait_q
);
410 total_bytes_written
+= bytes_written
;
411 buf
+= bytes_written
;
412 count
-= bytes_written
;
416 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
417 return total_bytes_written
;
420 static ssize_t
lp_write(struct file
* file
, const char * buf
,
421 size_t count
, loff_t
*ppos
)
423 unsigned int minor
= MINOR(file
->f_dentry
->d_inode
->i_rdev
);
427 if (jiffies
-lp_table
[minor
].lastcall
> LP_TIME(minor
))
428 lp_table
[minor
].runchars
= 0;
430 lp_table
[minor
].lastcall
= jiffies
;
433 /* Claim Parport or sleep until it becomes available
435 lp_parport_claim (minor
);
437 retv
= lp_write_buf(minor
, buf
, count
);
439 lp_parport_release (minor
);
443 static long long lp_lseek(struct file
* file
, long long offset
, int origin
)
448 #ifdef CONFIG_PRINTER_READBACK
450 static int lp_read_nibble(int minor
)
455 if ((i
& 0x10) == 0) i
|= 8;
459 static inline void lp_select_in_high(int minor
)
461 parport_frob_control(lp_table
[minor
].dev
->port
, 8, 8);
464 /* Status readback confirming to ieee1284 */
465 static ssize_t
lp_read(struct file
* file
, char * buf
,
466 size_t count
, loff_t
*ppos
)
468 unsigned char z
=0, Byte
=0, status
;
471 unsigned int counter
=0;
473 unsigned int minor
=MINOR(file
->f_dentry
->d_inode
->i_rdev
);
475 /* Claim Parport or sleep until it becomes available
477 lp_parport_claim (minor
);
481 printk(KERN_INFO
"lp%d: read mode\n", minor
);
484 retval
= verify_area(VERIFY_WRITE
, buf
, count
);
487 if (parport_ieee1284_nibble_mode_ok(lp_table
[minor
].dev
->port
, 0)==0) {
489 printk(KERN_INFO
"lp%d: rejected IEEE1284 negotiation.\n",
492 lp_select_in_high(minor
);
493 parport_release(lp_table
[minor
].dev
);
494 return temp
-buf
; /* End of file */
496 for (i
=0; i
<=(count
*2); i
++) {
497 parport_frob_control(lp_table
[minor
].dev
->port
, 2, 2); /* AutoFeed high */
499 status
= (r_str(minor
) & 0x40);
502 if (current
->need_resched
)
504 } while ((status
== 0x40) && (counter
< 20));
508 printk(KERN_DEBUG
"lp_read: (Autofeed high) timeout\n");
510 parport_frob_control(lp_table
[minor
].dev
->port
, 2, 0);
511 lp_select_in_high(minor
);
512 parport_release(lp_table
[minor
].dev
);
513 return temp
-buf
; /* end the read at timeout */
516 z
= lp_read_nibble(minor
);
517 parport_frob_control(lp_table
[minor
].dev
->port
, 2, 0); /* AutoFeed low */
519 status
=(r_str(minor
) & 0x40);
522 if (current
->need_resched
)
524 } while ( (status
== 0) && (counter
< 20) );
525 if (counter
== 20) { /* Timeout */
527 printk(KERN_DEBUG
"lp_read: (Autofeed low) timeout\n");
529 if (signal_pending(current
)) {
530 lp_select_in_high(minor
);
531 parport_release(lp_table
[minor
].dev
);
537 current
->state
=TASK_INTERRUPTIBLE
;
538 current
->timeout
=jiffies
+ LP_TIME(minor
);
546 if (put_user(Byte
, (char *)temp
))
552 lp_select_in_high(minor
);
553 lp_parport_release(minor
);
559 static int lp_open(struct inode
* inode
, struct file
* file
)
561 unsigned int minor
= MINOR(inode
->i_rdev
);
565 if ((LP_F(minor
) & LP_EXIST
) == 0)
567 if (test_and_set_bit(LP_BUSY_BIT_POS
, &LP_F(minor
)))
572 /* If ABORTOPEN is set and the printer is offline or out of paper,
573 we may still want to open it to perform ioctl()s. Therefore we
574 have commandeered O_NONBLOCK, even though it is being used in
575 a non-standard manner. This is strictly a Linux hack, and
576 should most likely only ever be used by the tunelp application. */
577 if ((LP_F(minor
) & LP_ABORTOPEN
) && !(file
->f_flags
& O_NONBLOCK
)) {
579 lp_parport_claim (minor
);
580 status
= r_str(minor
);
581 lp_parport_release (minor
);
582 if (status
& LP_POUTPA
) {
583 printk(KERN_INFO
"lp%d out of paper\n", minor
);
585 LP_F(minor
) &= ~LP_BUSY
;
587 } else if (!(status
& LP_PSELECD
)) {
588 printk(KERN_INFO
"lp%d off-line\n", minor
);
590 LP_F(minor
) &= ~LP_BUSY
;
592 } else if (!(status
& LP_PERRORP
)) {
593 printk(KERN_ERR
"lp%d printer error\n", minor
);
595 LP_F(minor
) &= ~LP_BUSY
;
599 lp_table
[minor
].lp_buffer
= (char *) kmalloc(LP_BUFFER_SIZE
, GFP_KERNEL
);
600 if (!lp_table
[minor
].lp_buffer
) {
602 LP_F(minor
) &= ~LP_BUSY
;
608 static int lp_release(struct inode
* inode
, struct file
* file
)
610 unsigned int minor
= MINOR(inode
->i_rdev
);
612 kfree_s(lp_table
[minor
].lp_buffer
, LP_BUFFER_SIZE
);
613 lp_table
[minor
].lp_buffer
= NULL
;
615 LP_F(minor
) &= ~LP_BUSY
;
619 static int lp_ioctl(struct inode
*inode
, struct file
*file
,
620 unsigned int cmd
, unsigned long arg
)
622 unsigned int minor
= MINOR(inode
->i_rdev
);
627 printk(KERN_DEBUG
"lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor
, cmd
, arg
);
631 if ((LP_F(minor
) & LP_EXIST
) == 0)
635 LP_TIME(minor
) = arg
* HZ
/100;
638 LP_CHAR(minor
) = arg
;
642 LP_F(minor
) |= LP_ABORT
;
644 LP_F(minor
) &= ~LP_ABORT
;
648 LP_F(minor
) |= LP_ABORTOPEN
;
650 LP_F(minor
) &= ~LP_ABORTOPEN
;
652 #ifdef LP_NEED_CAREFUL
655 LP_F(minor
) |= LP_CAREFUL
;
657 LP_F(minor
) &= ~LP_CAREFUL
;
661 LP_WAIT(minor
) = arg
;
667 if (copy_to_user((int *) arg
, &LP_IRQ(minor
),
672 lp_parport_claim(minor
);
673 status
= r_str(minor
);
674 lp_parport_release(minor
);
676 if (copy_to_user((int *) arg
, &status
, sizeof(int)))
684 if (copy_to_user((int *) arg
, &LP_STAT(minor
),
685 sizeof(struct lp_stats
)))
688 memset(&LP_STAT(minor
), 0,
689 sizeof(struct lp_stats
));
693 status
= LP_F(minor
);
694 if (copy_to_user((int *) arg
, &status
, sizeof(int)))
704 static struct file_operations lp_fops
= {
706 #ifdef CONFIG_PRINTER_READBACK
712 NULL
, /* lp_readdir */
721 /* --- initialisation code ------------------------------------- */
725 static int parport_nr
[LP_NO
] = { [0 ... LP_NO
-1] = LP_PARPORT_UNSPEC
};
726 static char *parport
[LP_NO
] = { NULL
, };
727 static int reset
= 0;
729 MODULE_PARM(parport
, "1-" __MODULE_STRING(LP_NO
) "s");
730 MODULE_PARM(reset
, "i");
734 static int parport_nr
[LP_NO
] __initdata
= { [0 ... LP_NO
-1] = LP_PARPORT_UNSPEC
};
735 static int reset __initdata
= 0;
737 static int parport_ptr
= 0;
739 __initfunc(void lp_setup(char *str
, int *ints
))
742 if (ints
[0] == 0 || ints
[1] == 0) {
743 /* disable driver on "lp=" or "lp=0" */
744 parport_nr
[0] = LP_PARPORT_OFF
;
746 printk(KERN_WARNING
"warning: 'lp=0x%x' is deprecated, ignored\n", ints
[1]);
748 } else if (!strncmp(str
, "parport", 7)) {
749 int n
= simple_strtoul(str
+7, NULL
, 10);
750 if (parport_ptr
< LP_NO
)
751 parport_nr
[parport_ptr
++] = n
;
753 printk(KERN_INFO
"lp: too many ports, %s ignored.\n",
755 } else if (!strcmp(str
, "auto")) {
756 parport_nr
[0] = LP_PARPORT_AUTO
;
757 } else if (!strcmp(str
, "none")) {
758 parport_nr
[parport_ptr
++] = LP_PARPORT_NONE
;
759 } else if (!strcmp(str
, "reset")) {
766 int lp_register(int nr
, struct parport
*port
)
768 lp_table
[nr
].dev
= parport_register_device(port
, "lp",
772 (void *) &lp_table
[nr
]);
773 if (lp_table
[nr
].dev
== NULL
)
775 lp_table
[nr
].flags
|= LP_EXIST
;
780 printk(KERN_INFO
"lp%d: using %s (%s).\n", nr
, port
->name
,
781 (port
->irq
== PARPORT_IRQ_NONE
)?"polling":"interrupt-driven");
788 unsigned int count
= 0;
790 struct parport
*port
;
792 switch (parport_nr
[0])
797 case LP_PARPORT_UNSPEC
:
798 case LP_PARPORT_AUTO
:
799 for (port
= parport_enumerate(); port
; port
= port
->next
) {
801 if (parport_nr
[0] == LP_PARPORT_AUTO
&&
802 port
->probe_info
.class != PARPORT_CLASS_PRINTER
)
805 if (!lp_register(count
, port
))
806 if (++count
== LP_NO
)
812 for (i
= 0; i
< LP_NO
; i
++) {
813 if (parport_nr
[i
] >= 0) {
815 sprintf(buffer
, "parport%d", parport_nr
[i
]);
816 for (port
= parport_enumerate(); port
;
818 if (!strcmp(port
->name
, buffer
)) {
819 (void) lp_register(i
, port
);
830 if (register_chrdev(LP_MAJOR
, "lp", &lp_fops
)) {
831 printk("lp: unable to get major %d\n", LP_MAJOR
);
835 printk(KERN_INFO
"lp: driver loaded but no devices found\n");
842 int init_module(void)
845 /* The user gave some parameters. Let's see what they were. */
846 if (!strncmp(parport
[0], "auto", 4))
847 parport_nr
[0] = LP_PARPORT_AUTO
;
850 for (n
= 0; n
< LP_NO
&& parport
[n
]; n
++) {
851 if (!strncmp(parport
[n
], "none", 4))
852 parport_nr
[n
] = LP_PARPORT_NONE
;
855 unsigned long r
= simple_strtoul(parport
[n
], &ep
, 0);
856 if (ep
!= parport
[n
])
859 printk(KERN_ERR
"lp: bad port specifier `%s'\n", parport
[n
]);
870 void cleanup_module(void)
874 unregister_chrdev(LP_MAJOR
, "lp");
875 for (offset
= 0; offset
< LP_NO
; offset
++) {
876 if (lp_table
[offset
].dev
== NULL
)
878 parport_unregister_device(lp_table
[offset
].dev
);