2 * Copyright (C) 1992 by Jim Weigand and Linus Torvalds
3 * Copyright (C) 1992,1993 by Michael K. Johnson
4 * - Thanks much to Gunter Windau for pointing out to me where the error
5 * checking ought to be.
6 * Copyright (C) 1993 by Nigel Gamble (added interrupt code)
7 * Copyright (C) 1994 by Alan Cox (Modularised it)
8 * LPCAREFUL, LPABORT, LPGETSTATUS added by Chris Metcalf, metcalf@lcs.mit.edu
9 * Statistics and support for slow printers by Rob Janssen, rob@knoware.nl
10 * "lp=" command line parameters added by Grant Guenther, grant@torque.net
11 * lp_read (Status readback) support added by Carsten Gross,
12 * carsten@sol.wohnheim.uni-ulm.de
15 #include <linux/module.h>
17 #include <linux/config.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/sched.h>
22 #include <linux/malloc.h>
23 #include <linux/ioport.h>
24 #include <linux/fcntl.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
29 #include <asm/uaccess.h>
30 #include <asm/system.h>
31 #include <linux/parport.h>
34 /* the BIOS manuals say there can be up to 4 lpt devices
35 * but I have not seen a board where the 4th address is listed
36 * if you have different hardware change the table below
37 * please let me know if you have different equipment
38 * if you have more than 3 printers, remember to increase LP_NO
40 struct lp_struct lp_table
[] =
42 {NULL
, 0, LP_INIT_CHAR
, LP_INIT_TIME
, LP_INIT_WAIT
, NULL
, NULL
, 0, 0, 0,
44 {NULL
, 0, LP_INIT_CHAR
, LP_INIT_TIME
, LP_INIT_WAIT
, NULL
, NULL
, 0, 0, 0,
46 {NULL
, 0, LP_INIT_CHAR
, LP_INIT_TIME
, LP_INIT_WAIT
, NULL
, NULL
, 0, 0, 0,
52 static char *dev_name
= "lp";
54 /* Test if printer is ready (and optionally has no error conditions) */
55 #define LP_READY(minor, status) \
56 ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : (status & LP_PBUSY))
57 #define LP_CAREFUL_READY(minor, status) \
58 ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : 1)
59 #define _LP_CAREFUL_READY(status) \
60 (status & (LP_PBUSY|LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
61 (LP_PBUSY|LP_PSELECD|LP_PERRORP)
66 static int lp_reset(int minor
)
68 w_ctr(minor
, LP_PSELECP
);
70 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
74 static inline int lp_char_polled(char lpchar
, int minor
)
77 unsigned long count
= 0;
78 struct lp_stats
*stats
;
81 status
= r_str(minor
);
85 } while (!LP_READY(minor
, status
) && count
< LP_CHAR(minor
));
87 if (count
== LP_CHAR(minor
)) {
89 /* we timed out, and the character was /not/ printed */
92 stats
= &LP_STAT(minor
);
94 /* must wait before taking strobe high, and after taking strobe
95 low, according spec. Some printers need it, others don't. */
96 while (wait
!= LP_WAIT(minor
))
98 /* control port takes strobe high */
99 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
);
102 /* take strobe low */
103 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
104 /* update waittime statistics */
105 if (count
> stats
->maxwait
) {
107 printk(KERN_DEBUG
"lp%d success after %d counts.\n", minor
, count
);
109 stats
->maxwait
= count
;
112 wait
= (count
> stats
->meanwait
) ? count
- stats
->meanwait
:
113 stats
->meanwait
- count
;
114 stats
->meanwait
= (255 * stats
->meanwait
+ count
+ 128) / 256;
115 stats
->mdev
= ((127 * stats
->mdev
) + wait
+ 64) / 128;
120 static inline int lp_char_interrupt(char lpchar
, int minor
)
123 unsigned long count
= 0;
124 unsigned char status
;
125 struct lp_stats
*stats
;
130 if ((status
= r_str(minor
)) & LP_PBUSY
) {
131 if (!LP_CAREFUL_READY(minor
, status
))
133 w_dtr(minor
, lpchar
);
134 stats
= &LP_STAT(minor
);
136 /* must wait before taking strobe high, and after taking strobe
137 low, according spec. Some printers need it, others don't. */
139 while (wait
!= LP_WAIT(minor
))
141 /* control port takes strobe high */
142 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
);
145 /* take strobe low */
146 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
147 /* update waittime statistics */
149 if (count
> stats
->maxwait
)
150 stats
->maxwait
= count
;
152 wait
= (count
> stats
->meanwait
) ? count
- stats
->meanwait
:
153 stats
->meanwait
- count
;
154 stats
->meanwait
= (255 * stats
->meanwait
+ count
+ 128) / 256;
155 stats
->mdev
= ((127 * stats
->mdev
) + wait
+ 64) / 128;
159 } while (count
++ < LP_CHAR(minor
));
164 static void lp_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
166 struct parport
*pb
= (struct parport
*) dev_id
;
167 struct ppd
*pd
= pb
->cad
;
168 struct lp_struct
*lp_dev
= (struct lp_struct
*) pd
->private;
170 if (lp_dev
->lp_wait_q
)
171 wake_up(&lp_dev
->lp_wait_q
);
174 static inline int lp_write_interrupt(unsigned int minor
, const char *buf
, int count
)
176 unsigned long copy_size
;
177 unsigned long total_bytes_written
= 0;
178 unsigned long bytes_written
;
179 struct lp_struct
*lp
= &lp_table
[minor
];
180 unsigned char status
;
184 if (lp_table
[minor
].dev
== NULL
)
189 copy_size
= (count
<= LP_BUFFER_SIZE
? count
: LP_BUFFER_SIZE
);
190 copy_from_user(lp
->lp_buffer
, buf
, copy_size
);
193 if (lp_char_interrupt(lp
->lp_buffer
[bytes_written
], minor
)) {
196 lp_table
[minor
].runchars
++;
198 int rc
= total_bytes_written
+ bytes_written
;
199 if (lp_table
[minor
].runchars
> LP_STAT(minor
).maxrun
)
200 LP_STAT(minor
).maxrun
= lp_table
[minor
].runchars
;
201 status
= r_str(minor
);
202 if ((status
& LP_POUTPA
)) {
203 printk(KERN_INFO
"lp%d out of paper\n", minor
);
204 if (LP_F(minor
) & LP_ABORT
)
205 return rc
? rc
: -ENOSPC
;
206 } else if (!(status
& LP_PSELECD
)) {
207 printk(KERN_INFO
"lp%d off-line\n", minor
);
208 if (LP_F(minor
) & LP_ABORT
)
209 return rc
? rc
: -EIO
;
210 } else if (!(status
& LP_PERRORP
)) {
211 printk(KERN_ERR
"lp%d printer error\n", minor
);
212 if (LP_F(minor
) & LP_ABORT
)
213 return rc
? rc
: -EIO
;
215 LP_STAT(minor
).sleeps
++;
217 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PINTEN
);
218 status
= r_str(minor
);
219 if ((!(status
& LP_PACK
) || (status
& LP_PBUSY
))
220 && LP_CAREFUL_READY(minor
, status
)) {
221 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
225 lp_table
[minor
].runchars
= 0;
226 current
->timeout
= jiffies
+ LP_TIMEOUT_INTERRUPT
;
227 interruptible_sleep_on(&lp
->lp_wait_q
);
228 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
230 if (current
->signal
& ~current
->blocked
) {
231 if (total_bytes_written
+ bytes_written
)
232 return total_bytes_written
+ bytes_written
;
239 total_bytes_written
+= bytes_written
;
240 buf
+= bytes_written
;
241 count
-= bytes_written
;
245 return total_bytes_written
;
248 static inline int lp_write_polled(unsigned int minor
, const char *buf
, int count
)
257 retval
= lp_char_polled(c
, minor
);
258 /* only update counting vars if character was printed */
261 lp_table
[minor
].runchars
++;
262 } else { /* if printer timed out */
263 if (lp_table
[minor
].runchars
> LP_STAT(minor
).maxrun
)
264 LP_STAT(minor
).maxrun
= lp_table
[minor
].runchars
;
265 status
= r_str(minor
);
267 if (status
& LP_POUTPA
) {
268 printk(KERN_INFO
"lp%d out of paper\n", minor
);
269 if(LP_F(minor
) & LP_ABORT
)
270 return temp
-buf
?temp
-buf
:-ENOSPC
;
271 current
->state
= TASK_INTERRUPTIBLE
;
272 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
275 if (!(status
& LP_PSELECD
)) {
276 printk(KERN_INFO
"lp%d off-line\n", minor
);
277 if(LP_F(minor
) & LP_ABORT
)
278 return temp
-buf
?temp
-buf
:-EIO
;
279 current
->state
= TASK_INTERRUPTIBLE
;
280 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
283 /* not offline or out of paper. on fire? */
284 if (!(status
& LP_PERRORP
)) {
285 printk(KERN_ERR
"lp%d on fire\n", minor
);
286 if(LP_F(minor
) & LP_ABORT
)
287 return temp
-buf
?temp
-buf
:-EIO
;
288 current
->state
= TASK_INTERRUPTIBLE
;
289 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
293 /* check for signals before going to sleep */
294 if (current
->signal
& ~current
->blocked
) {
300 LP_STAT(minor
).sleeps
++;
302 printk(KERN_DEBUG
"lp%d sleeping at %d characters for %d jiffies\n",
303 minor
,lp_table
[minor
].runchars
, LP_TIME(minor
));
305 lp_table
[minor
].runchars
=0;
306 current
->state
= TASK_INTERRUPTIBLE
;
307 current
->timeout
= jiffies
+ LP_TIME(minor
);
314 static long lp_write(struct inode
* inode
, struct file
* file
,
315 const char * buf
, unsigned long count
)
317 unsigned int minor
= MINOR(inode
->i_rdev
);
320 if (jiffies
-lp_table
[minor
].lastcall
> LP_TIME(minor
))
321 lp_table
[minor
].runchars
= 0;
323 lp_table
[minor
].lastcall
= jiffies
;
325 /* Claim Parport or sleep until it becomes available
326 * (see lp_wakeup() for details)
328 if (parport_claim(lp_table
[minor
].dev
)) {
329 sleep_on(&lp_table
[minor
].lp_wait_q
);
330 lp_table
[minor
].lp_wait_q
= NULL
;
332 if (LP_IRQ(minor
) > 0)
333 retv
= lp_write_interrupt(minor
, buf
, count
);
335 retv
= lp_write_polled(minor
, buf
, count
);
337 parport_release(lp_table
[minor
].dev
);
341 static long long lp_lseek(struct inode
* inode
, struct file
* file
,
342 long long offset
, int origin
)
347 #ifdef CONFIG_PRINTER_READBACK
349 static int lp_read_nibble(int minor
)
354 if ( ( i
& 0x10) == 0) i
|=8;
358 static void lp_select_in_high(int minor
) {
359 w_ctr(minor
, (r_ctr(minor
) | 8));
362 /* Status readback confirming to ieee1284 */
363 static long lp_read(struct inode
* inode
, struct file
* file
,
364 char * buf
, unsigned long count
)
366 unsigned char z
=0, Byte
=0, status
;
369 unsigned int counter
=0;
371 unsigned int minor
=MINOR(inode
->i_rdev
);
373 /* Claim Parport or sleep until it becomes available
374 * (see lp_wakeup() for details)
376 if (parport_claim(lp_table
[minor
].dev
)) {
377 sleep_on(&lp_table
[minor
].lp_wait_q
);
378 lp_table
[minor
].lp_wait_q
= NULL
;
383 printk(KERN_INFO
"lp%d: read mode\n", minor
);
386 retval
= verify_area(VERIFY_WRITE
, buf
, count
);
389 if (parport_ieee1284_nibble_mode_ok(lp_table
[minor
].dev
->port
, 0)==0) {
391 printk(KERN_INFO
"lp%d: rejected IEEE1284 negotiation.\n",
394 lp_select_in_high(minor
);
395 parport_release(lp_table
[minor
].dev
);
396 return temp
-buf
; /* End of file */
398 for (i
=0; i
<=(count
*2); i
++) {
399 w_ctr(minor
, r_ctr(minor
) | 2); /* AutoFeed high */
401 status
=(r_str(minor
) & 0x40);
406 } while ( (status
== 0x40) && (counter
< 20) );
407 if ( counter
== 20 ) { /* Timeout */
409 printk(KERN_DEBUG
"lp_read: (Autofeed high) timeout\n");
411 w_ctr(minor
, r_ctr(minor
) & ~2);
412 lp_select_in_high(minor
);
413 parport_release(lp_table
[minor
].dev
);
414 return temp
-buf
; /* end the read at timeout */
417 z
=lp_read_nibble(minor
);
418 w_ctr(minor
, r_ctr(minor
) & ~2); /* AutoFeed low */
420 status
=(r_str(minor
) & 0x40);
425 } while ( (status
== 0) && (counter
< 20) );
426 if (counter
== 20) { /* Timeout */
428 printk(KERN_DEBUG
"lp_read: (Autofeed low) timeout\n");
430 if (current
->signal
& ~current
->blocked
) {
431 lp_select_in_high(minor
);
432 parport_release(lp_table
[minor
].dev
);
438 current
->state
=TASK_INTERRUPTIBLE
;
439 current
->timeout
=jiffies
+ LP_TIME(minor
);
445 put_user(Byte
, temp
);
449 lp_select_in_high(minor
);
450 parport_release(lp_table
[minor
].dev
);
456 static int lp_open(struct inode
* inode
, struct file
* file
)
458 unsigned int minor
= MINOR(inode
->i_rdev
);
462 if ((LP_F(minor
) & LP_EXIST
) == 0)
464 if (LP_F(minor
) & LP_BUSY
)
469 /* If ABORTOPEN is set and the printer is offline or out of paper,
470 we may still want to open it to perform ioctl()s. Therefore we
471 have commandeered O_NONBLOCK, even though it is being used in
472 a non-standard manner. This is strictly a Linux hack, and
473 should most likely only ever be used by the tunelp application. */
474 if ((LP_F(minor
) & LP_ABORTOPEN
) && !(file
->f_flags
& O_NONBLOCK
)) {
475 int status
= r_str(minor
);
476 if (status
& LP_POUTPA
) {
477 printk(KERN_INFO
"lp%d out of paper\n", minor
);
480 } else if (!(status
& LP_PSELECD
)) {
481 printk(KERN_INFO
"lp%d off-line\n", minor
);
484 } else if (!(status
& LP_PERRORP
)) {
485 printk(KERN_ERR
"lp%d printer error\n", minor
);
490 if (LP_IRQ(minor
) > 0) {
491 lp_table
[minor
].lp_buffer
= (char *) kmalloc(LP_BUFFER_SIZE
, GFP_KERNEL
);
492 if (!lp_table
[minor
].lp_buffer
) {
497 LP_F(minor
) |= LP_BUSY
;
501 static int lp_release(struct inode
* inode
, struct file
* file
)
503 unsigned int minor
= MINOR(inode
->i_rdev
);
506 if ((irq
= LP_IRQ(minor
))) {
507 kfree_s(lp_table
[minor
].lp_buffer
, LP_BUFFER_SIZE
);
508 lp_table
[minor
].lp_buffer
= NULL
;
510 LP_F(minor
) &= ~LP_BUSY
;
516 static int lp_ioctl(struct inode
*inode
, struct file
*file
,
517 unsigned int cmd
, unsigned long arg
)
519 unsigned int minor
= MINOR(inode
->i_rdev
);
523 printk(KERN_DEBUG
"lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor
, cmd
, arg
);
527 if ((LP_F(minor
) & LP_EXIST
) == 0)
531 LP_TIME(minor
) = arg
* HZ
/100;
534 LP_CHAR(minor
) = arg
;
538 LP_F(minor
) |= LP_ABORT
;
540 LP_F(minor
) &= ~LP_ABORT
;
544 LP_F(minor
) |= LP_ABORTOPEN
;
546 LP_F(minor
) &= ~LP_ABORTOPEN
;
550 LP_F(minor
) |= LP_CAREFUL
;
552 LP_F(minor
) &= ~LP_CAREFUL
;
555 LP_WAIT(minor
) = arg
;
561 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
565 copy_to_user((int *) arg
, &LP_IRQ(minor
), sizeof(int));
568 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
573 int status
= r_str(minor
);
574 copy_to_user((int *) arg
, &status
, sizeof(int));
581 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
582 sizeof(struct lp_stats
));
586 copy_to_user((int *) arg
, &LP_STAT(minor
), sizeof(struct lp_stats
));
588 memset(&LP_STAT(minor
), 0, sizeof(struct lp_stats
));
592 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
597 int status
= LP_F(minor
);
598 copy_to_user((int *) arg
, &status
, sizeof(int));
608 static struct file_operations lp_fops
= {
610 #ifdef CONFIG_PRINTER_READBACK
616 NULL
, /* lp_readdir */
624 static int parport
[LP_NO
] = { -1, };
627 #define lp_init init_module
628 MODULE_PARM(parport
, "1-" __MODULE_STRING(LP_NO
) "i");
632 static int parport_ptr
= 0;
634 void lp_setup(char *str
, int *ints
)
637 if (!strncmp(str
, "parport", 7)) {
638 int n
= simple_strtoul(str
+7, NULL
, 10);
639 if (parport_ptr
< LP_NO
)
640 parport
[parport_ptr
++] = n
;
642 printk(KERN_INFO
"lp: too many ports, %s ignored.\n",
644 } else if (!strcmp(str
, "auto")) {
647 if (ints
[0] == 0 || ints
[1] == 0) {
648 /* disable driver on "parport=" or "parport=0" */
651 printk(KERN_WARNING
"warning: 'lp=0x%x' is deprecated, ignored\n", ints
[1]);
658 int lp_wakeup(void *ref
)
660 struct lp_struct
*lp_dev
= (struct lp_struct
*) ref
;
662 if (!lp_dev
->lp_wait_q
)
663 return 1; /* Wake up whom? */
665 /* Claim the Parport */
666 if (parport_claim(lp_dev
->dev
))
667 return 1; /* Shouldn't happen */
669 wake_up(&lp_dev
->lp_wait_q
);
673 static int inline lp_searchfor(int list
[], int a
)
676 for (i
= 0; i
< LP_NO
&& list
[i
] != -1; i
++) {
677 if (list
[i
] == a
) return 1;
682 __initfunc(int lp_init(void))
687 if (register_chrdev(LP_MAJOR
, "lp", &lp_fops
)) {
688 printk("lp: unable to get major %d\n", LP_MAJOR
);
692 if (parport
[0] == -2) return 0;
694 pb
= parport_enumerate();
697 /* We only understand PC-style ports. */
698 if (pb
->modes
& PARPORT_MODE_SPP
) {
699 if (parport
[0] == -1 || lp_searchfor(parport
, count
) ||
701 pb
->probe_info
.class == PARPORT_CLASS_PRINTER
)) {
702 lp_table
[count
].dev
=
703 parport_register_device(pb
, dev_name
, NULL
,
705 lp_interrupt
, PARPORT_DEV_TRAN
,
706 (void *) &lp_table
[count
]);
707 lp_table
[count
].flags
|= LP_EXIST
;
708 printk(KERN_INFO
"lp%d: using %s at 0x%x, ",
709 count
, pb
->name
, pb
->base
);
711 printk("polling.\n");
713 printk("irq %d.\n", pb
->irq
);
715 if (++count
== LP_NO
)
721 /* Successful specified devices increase count
722 * Unsuccessful specified devices increase failed
727 printk(KERN_INFO
"lp: driver loaded but no devices found\n");
732 void cleanup_module(void)
736 unregister_chrdev(LP_MAJOR
, "lp");
737 for (offset
= 0; offset
< LP_NO
; offset
++) {
738 if (lp_table
[offset
].dev
== NULL
)
740 parport_unregister_device(lp_table
[offset
].dev
);