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
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
, 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
);
175 schedule_timeout(timeout
);
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 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
) {
286 printk(KERN_INFO
"lp%d out of paper\n", minor
);
288 } else if (!(status
& LP_PSELECD
)) {
289 if (last
!= LP_PSELECD
) {
291 printk(KERN_INFO
"lp%d off-line\n", minor
);
293 } else if (!(status
& LP_PERRORP
)) {
294 if (last
!= LP_PERRORP
) {
296 printk(KERN_ERR
"lp%d on fire!\n", minor
);
301 lp_table
[minor
].last_error
= last
;
304 if (LP_F(minor
) & LP_ABORT
)
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
];
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
);
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
);
341 if (lp_char(lp
->lp_buffer
[bytes_written
], minor
)) {
348 int rc
= total_bytes_written
+ bytes_written
;
351 if (lp
->runchars
> LP_STAT(minor
).maxrun
)
352 LP_STAT(minor
).maxrun
= lp
->runchars
;
353 LP_STAT(minor
).sleeps
++;
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
;
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
)
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
));
382 current
->state
= TASK_INTERRUPTIBLE
;
383 lp_schedule(minor
, LP_TIME(minor
));
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
398 if (!lp_table
[minor
].irq_detected
)
400 interruptible_sleep_on_timeout(&lp
->wait_q
, LP_TIMEOUT_INTERRUPT
);
407 total_bytes_written
+= bytes_written
;
408 buf
+= bytes_written
;
409 count
-= bytes_written
;
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
);
424 if (jiffies
-lp_table
[minor
].lastcall
> LP_TIME(minor
))
425 lp_table
[minor
].runchars
= 0;
427 lp_table
[minor
].lastcall
= jiffies
;
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
);
440 static long long lp_lseek(struct file
* file
, long long offset
, int origin
)
445 #ifdef CONFIG_PRINTER_READBACK
447 static int lp_read_nibble(int minor
)
452 if ((i
& 0x10) == 0) i
|= 8;
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
;
468 unsigned int counter
=0;
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
);
478 printk(KERN_INFO
"lp%d: read mode\n", minor
);
481 retval
= verify_area(VERIFY_WRITE
, buf
, count
);
484 if (parport_ieee1284_nibble_mode_ok(lp_table
[minor
].dev
->port
, 0)==0) {
486 printk(KERN_INFO
"lp%d: rejected IEEE1284 negotiation.\n",
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 */
496 status
= (r_str(minor
) & 0x40);
499 if (current
->need_resched
)
501 } while ((status
== 0x40) && (counter
< 20));
505 printk(KERN_DEBUG
"lp_read: (Autofeed high) timeout\n");
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 */
513 z
= lp_read_nibble(minor
);
514 parport_frob_control(lp_table
[minor
].dev
->port
, 2, 0); /* AutoFeed low */
516 status
=(r_str(minor
) & 0x40);
519 if (current
->need_resched
)
521 } while ( (status
== 0) && (counter
< 20) );
522 if (counter
== 20) { /* Timeout */
524 printk(KERN_DEBUG
"lp_read: (Autofeed low) timeout\n");
526 if (signal_pending(current
)) {
527 lp_select_in_high(minor
);
528 parport_release(lp_table
[minor
].dev
);
534 current
->state
=TASK_INTERRUPTIBLE
;
535 schedule_timeout(LP_TIME(minor
));
542 if (__put_user(Byte
, (char *)temp
))
548 lp_select_in_high(minor
);
549 lp_parport_release(minor
);
555 static int lp_open(struct inode
* inode
, struct file
* file
)
557 unsigned int minor
= MINOR(inode
->i_rdev
);
561 if ((LP_F(minor
) & LP_EXIST
) == 0)
563 if (test_and_set_bit(LP_BUSY_BIT_POS
, &LP_F(minor
)))
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
)) {
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
);
581 LP_F(minor
) &= ~LP_BUSY
;
583 } else if (!(status
& LP_PSELECD
)) {
584 printk(KERN_INFO
"lp%d off-line\n", minor
);
586 LP_F(minor
) &= ~LP_BUSY
;
588 } else if (!(status
& LP_PERRORP
)) {
589 printk(KERN_ERR
"lp%d printer error\n", minor
);
591 LP_F(minor
) &= ~LP_BUSY
;
595 lp_table
[minor
].lp_buffer
= (char *) kmalloc(LP_BUFFER_SIZE
, GFP_KERNEL
);
596 if (!lp_table
[minor
].lp_buffer
) {
598 LP_F(minor
) &= ~LP_BUSY
;
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
;
611 LP_F(minor
) &= ~LP_BUSY
;
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
);
623 printk(KERN_DEBUG
"lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor
, cmd
, arg
);
627 if ((LP_F(minor
) & LP_EXIST
) == 0)
631 LP_TIME(minor
) = arg
* HZ
/100;
634 LP_CHAR(minor
) = arg
;
638 LP_F(minor
) |= LP_ABORT
;
640 LP_F(minor
) &= ~LP_ABORT
;
644 LP_F(minor
) |= LP_ABORTOPEN
;
646 LP_F(minor
) &= ~LP_ABORTOPEN
;
648 #ifdef LP_NEED_CAREFUL
651 LP_F(minor
) |= LP_CAREFUL
;
653 LP_F(minor
) &= ~LP_CAREFUL
;
657 LP_WAIT(minor
) = arg
;
663 if (copy_to_user((int *) arg
, &LP_IRQ(minor
),
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)))
680 if (copy_to_user((int *) arg
, &LP_STAT(minor
),
681 sizeof(struct lp_stats
)))
684 memset(&LP_STAT(minor
), 0,
685 sizeof(struct lp_stats
));
689 status
= LP_F(minor
);
690 if (copy_to_user((int *) arg
, &status
, sizeof(int)))
700 static struct file_operations lp_fops
= {
702 #ifdef CONFIG_PRINTER_READBACK
708 NULL
, /* lp_readdir */
717 /* --- initialisation code ------------------------------------- */
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");
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
))
738 if (ints
[0] == 0 || ints
[1] == 0) {
739 /* disable driver on "lp=" or "lp=0" */
740 parport_nr
[0] = LP_PARPORT_OFF
;
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
;
749 printk(KERN_INFO
"lp: too many ports, %s ignored.\n",
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")) {
762 int lp_register(int nr
, struct parport
*port
)
764 lp_table
[nr
].dev
= parport_register_device(port
, "lp",
768 (void *) &lp_table
[nr
]);
769 if (lp_table
[nr
].dev
== NULL
)
771 lp_table
[nr
].flags
|= LP_EXIST
;
776 printk(KERN_INFO
"lp%d: using %s (%s).\n", nr
, port
->name
,
777 (port
->irq
== PARPORT_IRQ_NONE
)?"polling":"interrupt-driven");
784 unsigned int count
= 0;
786 struct parport
*port
;
788 switch (parport_nr
[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
)
801 if (!lp_register(count
, port
))
802 if (++count
== LP_NO
)
808 for (i
= 0; i
< LP_NO
; i
++) {
809 if (parport_nr
[i
] >= 0) {
811 sprintf(buffer
, "parport%d", parport_nr
[i
]);
812 for (port
= parport_enumerate(); port
;
814 if (!strcmp(port
->name
, buffer
)) {
815 (void) lp_register(i
, port
);
826 if (register_chrdev(LP_MAJOR
, "lp", &lp_fops
)) {
827 printk("lp: unable to get major %d\n", LP_MAJOR
);
831 printk(KERN_INFO
"lp: driver loaded but no devices found\n");
838 int init_module(void)
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
;
846 for (n
= 0; n
< LP_NO
&& parport
[n
]; n
++) {
847 if (!strncmp(parport
[n
], "none", 4))
848 parport_nr
[n
] = LP_PARPORT_NONE
;
851 unsigned long r
= simple_strtoul(parport
[n
], &ep
, 0);
852 if (ep
!= parport
[n
])
855 printk(KERN_ERR
"lp: bad port specifier `%s'\n", parport
[n
]);
866 void cleanup_module(void)
870 unregister_chrdev(LP_MAJOR
, "lp");
871 for (offset
= 0; offset
< LP_NO
; offset
++) {
872 if (lp_table
[offset
].dev
== NULL
)
874 parport_unregister_device(lp_table
[offset
].dev
);