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/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/major.h>
20 #include <linux/sched.h>
21 #include <linux/malloc.h>
22 #include <linux/ioport.h>
23 #include <linux/fcntl.h>
24 #include <linux/delay.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
29 #include <linux/parport.h>
32 /* the BIOS manuals say there can be up to 4 lpt devices
33 * but I have not seen a board where the 4th address is listed
34 * if you have different hardware change the table below
35 * please let me know if you have different equipment
36 * if you have more than 3 printers, remember to increase LP_NO
38 struct lp_struct lp_table
[] =
40 {NULL
, 0, LP_INIT_CHAR
, LP_INIT_TIME
, LP_INIT_WAIT
, NULL
, NULL
, 0, 0, 0,
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,
50 static char *dev_name
= "lp";
52 /* Test if printer is ready (and optionally has no error conditions) */
53 #define LP_READY(minor, status) \
54 ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : (status & LP_PBUSY))
55 #define LP_CAREFUL_READY(minor, status) \
56 ((LP_F(minor) & LP_CAREFUL) ? _LP_CAREFUL_READY(status) : 1)
57 #define _LP_CAREFUL_READY(status) \
58 (status & (LP_PBUSY|LP_POUTPA|LP_PSELECD|LP_PERRORP)) == \
59 (LP_PBUSY|LP_PSELECD|LP_PERRORP)
64 static int lp_reset(int minor
)
66 w_ctr(minor
, LP_PSELECP
);
68 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
72 static inline int lp_char_polled(char lpchar
, int minor
)
75 unsigned long count
= 0;
76 struct lp_stats
*stats
;
79 status
= r_str(minor
);
83 } while (!LP_READY(minor
, status
) && count
< LP_CHAR(minor
));
85 if (count
== LP_CHAR(minor
)) {
87 /* we timed out, and the character was /not/ printed */
90 stats
= &LP_STAT(minor
);
92 /* must wait before taking strobe high, and after taking strobe
93 low, according spec. Some printers need it, others don't. */
94 while (wait
!= LP_WAIT(minor
))
96 /* control port takes strobe high */
97 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
);
100 /* take strobe low */
101 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
102 /* update waittime statistics */
103 if (count
> stats
->maxwait
) {
105 printk(KERN_DEBUG
"lp%d success after %d counts.\n", minor
, count
);
107 stats
->maxwait
= count
;
110 wait
= (count
> stats
->meanwait
) ? count
- stats
->meanwait
:
111 stats
->meanwait
- count
;
112 stats
->meanwait
= (255 * stats
->meanwait
+ count
+ 128) / 256;
113 stats
->mdev
= ((127 * stats
->mdev
) + wait
+ 64) / 128;
118 static inline int lp_char_interrupt(char lpchar
, int minor
)
121 unsigned long count
= 0;
122 unsigned char status
;
123 struct lp_stats
*stats
;
128 if ((status
= r_str(minor
)) & LP_PBUSY
) {
129 if (!LP_CAREFUL_READY(minor
, status
))
131 w_dtr(minor
, lpchar
);
132 stats
= &LP_STAT(minor
);
134 /* must wait before taking strobe high, and after taking strobe
135 low, according spec. Some printers need it, others don't. */
137 while (wait
!= LP_WAIT(minor
))
139 /* control port takes strobe high */
140 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PSTROBE
);
143 /* take strobe low */
144 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
145 /* update waittime statistics */
147 if (count
> stats
->maxwait
)
148 stats
->maxwait
= count
;
150 wait
= (count
> stats
->meanwait
) ? count
- stats
->meanwait
:
151 stats
->meanwait
- count
;
152 stats
->meanwait
= (255 * stats
->meanwait
+ count
+ 128) / 256;
153 stats
->mdev
= ((127 * stats
->mdev
) + wait
+ 64) / 128;
157 } while (count
++ < LP_CHAR(minor
));
162 static void lp_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
164 struct parport
*pb
= (struct parport
*) dev_id
;
165 struct ppd
*pd
= pb
->cad
;
166 struct lp_struct
*lp_dev
= (struct lp_struct
*) pd
->private;
168 if (lp_dev
->lp_wait_q
)
169 wake_up(&lp_dev
->lp_wait_q
);
172 static inline int lp_write_interrupt(unsigned int minor
, const char *buf
, int count
)
174 unsigned long copy_size
;
175 unsigned long total_bytes_written
= 0;
176 unsigned long bytes_written
;
177 struct lp_struct
*lp
= &lp_table
[minor
];
178 unsigned char status
;
182 if (lp_table
[minor
].dev
== NULL
)
187 copy_size
= (count
<= LP_BUFFER_SIZE
? count
: LP_BUFFER_SIZE
);
188 copy_from_user(lp
->lp_buffer
, buf
, copy_size
);
191 if (lp_char_interrupt(lp
->lp_buffer
[bytes_written
], minor
)) {
194 lp_table
[minor
].runchars
++;
196 int rc
= total_bytes_written
+ bytes_written
;
197 if (lp_table
[minor
].runchars
> LP_STAT(minor
).maxrun
)
198 LP_STAT(minor
).maxrun
= lp_table
[minor
].runchars
;
199 status
= r_str(minor
);
200 if ((status
& LP_POUTPA
)) {
201 printk(KERN_INFO
"lp%d out of paper\n", minor
);
202 if (LP_F(minor
) & LP_ABORT
)
203 return rc
? rc
: -ENOSPC
;
204 } else if (!(status
& LP_PSELECD
)) {
205 printk(KERN_INFO
"lp%d off-line\n", minor
);
206 if (LP_F(minor
) & LP_ABORT
)
207 return rc
? rc
: -EIO
;
208 } else if (!(status
& LP_PERRORP
)) {
209 printk(KERN_ERR
"lp%d printer error\n", minor
);
210 if (LP_F(minor
) & LP_ABORT
)
211 return rc
? rc
: -EIO
;
213 LP_STAT(minor
).sleeps
++;
215 w_ctr(minor
, LP_PSELECP
| LP_PINITP
| LP_PINTEN
);
216 status
= r_str(minor
);
217 if ((!(status
& LP_PACK
) || (status
& LP_PBUSY
))
218 && LP_CAREFUL_READY(minor
, status
)) {
219 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
223 lp_table
[minor
].runchars
= 0;
224 current
->timeout
= jiffies
+ LP_TIMEOUT_INTERRUPT
;
225 interruptible_sleep_on(&lp
->lp_wait_q
);
226 w_ctr(minor
, LP_PSELECP
| LP_PINITP
);
228 if (current
->signal
& ~current
->blocked
) {
229 if (total_bytes_written
+ bytes_written
)
230 return total_bytes_written
+ bytes_written
;
237 total_bytes_written
+= bytes_written
;
238 buf
+= bytes_written
;
239 count
-= bytes_written
;
243 return total_bytes_written
;
246 static inline int lp_write_polled(unsigned int minor
, const char *buf
, int count
)
255 retval
= lp_char_polled(c
, minor
);
256 /* only update counting vars if character was printed */
259 lp_table
[minor
].runchars
++;
260 } else { /* if printer timed out */
261 if (lp_table
[minor
].runchars
> LP_STAT(minor
).maxrun
)
262 LP_STAT(minor
).maxrun
= lp_table
[minor
].runchars
;
263 status
= r_str(minor
);
265 if (status
& LP_POUTPA
) {
266 printk(KERN_INFO
"lp%d out of paper\n", minor
);
267 if(LP_F(minor
) & LP_ABORT
)
268 return temp
-buf
?temp
-buf
:-ENOSPC
;
269 current
->state
= TASK_INTERRUPTIBLE
;
270 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
273 if (!(status
& LP_PSELECD
)) {
274 printk(KERN_INFO
"lp%d off-line\n", minor
);
275 if(LP_F(minor
) & LP_ABORT
)
276 return temp
-buf
?temp
-buf
:-EIO
;
277 current
->state
= TASK_INTERRUPTIBLE
;
278 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
281 /* not offline or out of paper. on fire? */
282 if (!(status
& LP_PERRORP
)) {
283 printk(KERN_ERR
"lp%d on fire\n", minor
);
284 if(LP_F(minor
) & LP_ABORT
)
285 return temp
-buf
?temp
-buf
:-EIO
;
286 current
->state
= TASK_INTERRUPTIBLE
;
287 current
->timeout
= jiffies
+ LP_TIMEOUT_POLLED
;
291 /* check for signals before going to sleep */
292 if (current
->signal
& ~current
->blocked
) {
298 LP_STAT(minor
).sleeps
++;
300 printk(KERN_DEBUG
"lp%d sleeping at %d characters for %d jiffies\n",
301 minor
,lp_table
[minor
].runchars
, LP_TIME(minor
));
303 lp_table
[minor
].runchars
=0;
304 current
->state
= TASK_INTERRUPTIBLE
;
305 current
->timeout
= jiffies
+ LP_TIME(minor
);
312 static long lp_write(struct inode
* inode
, struct file
* file
,
313 const char * buf
, unsigned long count
)
315 unsigned int minor
= MINOR(inode
->i_rdev
);
318 if (jiffies
-lp_table
[minor
].lastcall
> LP_TIME(minor
))
319 lp_table
[minor
].runchars
= 0;
321 lp_table
[minor
].lastcall
= jiffies
;
323 /* Claim Parport or sleep until it becomes available
324 * (see lp_wakeup() for details)
326 if (parport_claim(lp_table
[minor
].dev
)) {
327 sleep_on(&lp_table
[minor
].lp_wait_q
);
328 lp_table
[minor
].lp_wait_q
= NULL
;
330 if (LP_IRQ(minor
) > 0)
331 retv
= lp_write_interrupt(minor
, buf
, count
);
333 retv
= lp_write_polled(minor
, buf
, count
);
335 parport_release(lp_table
[minor
].dev
);
339 static long long lp_lseek(struct inode
* inode
, struct file
* file
,
340 long long offset
, int origin
)
345 #ifdef CONFIG_PRINTER_READBACK
347 static int lp_read_nibble(int minor
)
352 if ( ( i
& 0x10) == 0) i
|=8;
356 static void lp_select_in_high(int minor
) {
357 w_ctr(minor
, (r_ctr(minor
) | 8));
360 /* Status readback confirming to ieee1284 */
361 static long lp_read(struct inode
* inode
, struct file
* file
,
362 char * buf
, unsigned long count
)
364 unsigned char z
=0, Byte
=0, status
;
367 unsigned int counter
=0;
369 unsigned int minor
=MINOR(inode
->i_rdev
);
371 /* Claim Parport or sleep until it becomes available
372 * (see lp_wakeup() for details)
374 if (parport_claim(lp_table
[minor
].dev
)) {
375 sleep_on(&lp_table
[minor
].lp_wait_q
);
376 lp_table
[minor
].lp_wait_q
= NULL
;
381 printk(KERN_INFO
"lp%d: read mode\n", minor
);
384 retval
= verify_area(VERIFY_WRITE
, buf
, count
);
387 if (parport_ieee1284_nibble_mode_ok(lp_table
[minor
].dev
->port
, 0)==0) {
389 printk(KERN_INFO
"lp%d: rejected IEEE1284 negotiation.\n",
392 lp_select_in_high(minor
);
393 parport_release(lp_table
[minor
].dev
);
394 return temp
-buf
; /* End of file */
396 for (i
=0; i
<=(count
*2); i
++) {
397 w_ctr(minor
, r_ctr(minor
) | 2); /* AutoFeed high */
399 status
=(r_str(minor
) & 0x40);
404 } while ( (status
== 0x40) && (counter
< 20) );
405 if ( counter
== 20 ) { /* Timeout */
407 printk(KERN_DEBUG
"lp_read: (Autofeed high) timeout\n");
409 w_ctr(minor
, r_ctr(minor
) & ~2);
410 lp_select_in_high(minor
);
411 parport_release(lp_table
[minor
].dev
);
412 return temp
-buf
; /* end the read at timeout */
415 z
=lp_read_nibble(minor
);
416 w_ctr(minor
, r_ctr(minor
) & ~2); /* AutoFeed low */
418 status
=(r_str(minor
) & 0x40);
423 } while ( (status
== 0) && (counter
< 20) );
424 if (counter
== 20) { /* Timeout */
426 printk(KERN_DEBUG
"lp_read: (Autofeed low) timeout\n");
428 if (current
->signal
& ~current
->blocked
) {
429 lp_select_in_high(minor
);
430 parport_release(lp_table
[minor
].dev
);
436 current
->state
=TASK_INTERRUPTIBLE
;
437 current
->timeout
=jiffies
+ LP_TIME(minor
);
443 put_user(Byte
, temp
);
447 lp_select_in_high(minor
);
448 parport_release(lp_table
[minor
].dev
);
454 static int lp_open(struct inode
* inode
, struct file
* file
)
456 unsigned int minor
= MINOR(inode
->i_rdev
);
460 if ((LP_F(minor
) & LP_EXIST
) == 0)
462 if (LP_F(minor
) & LP_BUSY
)
467 /* If ABORTOPEN is set and the printer is offline or out of paper,
468 we may still want to open it to perform ioctl()s. Therefore we
469 have commandeered O_NONBLOCK, even though it is being used in
470 a non-standard manner. This is strictly a Linux hack, and
471 should most likely only ever be used by the tunelp application. */
472 if ((LP_F(minor
) & LP_ABORTOPEN
) && !(file
->f_flags
& O_NONBLOCK
)) {
473 int status
= r_str(minor
);
474 if (status
& LP_POUTPA
) {
475 printk(KERN_INFO
"lp%d out of paper\n", minor
);
478 } else if (!(status
& LP_PSELECD
)) {
479 printk(KERN_INFO
"lp%d off-line\n", minor
);
482 } else if (!(status
& LP_PERRORP
)) {
483 printk(KERN_ERR
"lp%d printer error\n", minor
);
488 if (LP_IRQ(minor
) > 0) {
489 lp_table
[minor
].lp_buffer
= (char *) kmalloc(LP_BUFFER_SIZE
, GFP_KERNEL
);
490 if (!lp_table
[minor
].lp_buffer
) {
495 LP_F(minor
) |= LP_BUSY
;
499 static int lp_release(struct inode
* inode
, struct file
* file
)
501 unsigned int minor
= MINOR(inode
->i_rdev
);
504 if ((irq
= LP_IRQ(minor
))) {
505 kfree_s(lp_table
[minor
].lp_buffer
, LP_BUFFER_SIZE
);
506 lp_table
[minor
].lp_buffer
= NULL
;
508 LP_F(minor
) &= ~LP_BUSY
;
514 static int lp_ioctl(struct inode
*inode
, struct file
*file
,
515 unsigned int cmd
, unsigned long arg
)
517 unsigned int minor
= MINOR(inode
->i_rdev
);
521 printk(KERN_DEBUG
"lp%d ioctl, cmd: 0x%x, arg: 0x%x\n", minor
, cmd
, arg
);
525 if ((LP_F(minor
) & LP_EXIST
) == 0)
529 LP_TIME(minor
) = arg
* HZ
/100;
532 LP_CHAR(minor
) = arg
;
536 LP_F(minor
) |= LP_ABORT
;
538 LP_F(minor
) &= ~LP_ABORT
;
542 LP_F(minor
) |= LP_ABORTOPEN
;
544 LP_F(minor
) &= ~LP_ABORTOPEN
;
548 LP_F(minor
) |= LP_CAREFUL
;
550 LP_F(minor
) &= ~LP_CAREFUL
;
553 LP_WAIT(minor
) = arg
;
559 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
563 copy_to_user((int *) arg
, &LP_IRQ(minor
), sizeof(int));
566 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
571 int status
= r_str(minor
);
572 copy_to_user((int *) arg
, &status
, sizeof(int));
579 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
580 sizeof(struct lp_stats
));
584 copy_to_user((int *) arg
, &LP_STAT(minor
), sizeof(struct lp_stats
));
586 memset(&LP_STAT(minor
), 0, sizeof(struct lp_stats
));
590 retval
= verify_area(VERIFY_WRITE
, (void *) arg
,
595 int status
= LP_F(minor
);
596 copy_to_user((int *) arg
, &status
, sizeof(int));
606 static struct file_operations lp_fops
= {
608 #ifdef CONFIG_PRINTER_READBACK
614 NULL
, /* lp_readdir */
622 static int parport
[LP_NO
] = { -1, };
625 #define lp_init init_module
626 MODULE_PARM(parport
, "1-" __MODULE_STRING(LP_NO
) "i");
630 static int parport_ptr
= 0;
632 void lp_setup(char *str
, int *ints
)
635 if (!strncmp(str
, "parport", 7)) {
636 int n
= simple_strtoul(str
+7, NULL
, 10);
637 if (parport_ptr
< LP_NO
)
638 parport
[parport_ptr
++] = n
;
640 printk(KERN_INFO
"lp: too many ports, %s ignored.\n",
642 } else if (!strcmp(str
, "auto")) {
645 if (ints
[0] == 0 || ints
[1] == 0) {
646 /* disable driver on "parport=" or "parport=0" */
649 printk(KERN_WARNING
"warning: 'lp=0x%x' is deprecated, ignored\n", ints
[1]);
656 int lp_wakeup(void *ref
)
658 struct lp_struct
*lp_dev
= (struct lp_struct
*) ref
;
660 if (!lp_dev
->lp_wait_q
)
661 return 1; /* Wake up whom? */
663 /* Claim the Parport */
664 if (parport_claim(lp_dev
->dev
))
665 return 1; /* Shouldn't happen */
667 wake_up(&lp_dev
->lp_wait_q
);
671 static int inline lp_searchfor(int list
[], int a
)
674 for (i
= 0; i
< LP_NO
&& list
[i
] != -1; i
++) {
675 if (list
[i
] == a
) return 1;
685 if (register_chrdev(LP_MAJOR
, "lp", &lp_fops
)) {
686 printk("lp: unable to get major %d\n", LP_MAJOR
);
690 if (parport
[0] == -2) return 0;
692 pb
= parport_enumerate();
695 if (parport
[0] == -1 || lp_searchfor(parport
, count
) ||
697 pb
->probe_info
.class == PARPORT_CLASS_PRINTER
)) {
698 lp_table
[count
].dev
=
699 parport_register_device(pb
, dev_name
, NULL
,
701 lp_interrupt
, PARPORT_DEV_TRAN
,
702 (void *) &lp_table
[count
]);
703 lp_table
[count
].flags
|= LP_EXIST
;
704 printk(KERN_INFO
"lp%d: using %s at 0x%x, ", count
,
707 printk("polling.\n");
709 printk("irq %d.\n", pb
->irq
);
711 if (++count
== LP_NO
)
716 /* Successful specified devices increase count
717 * Unsuccessful specified devices increase failed
722 printk(KERN_INFO
"lp: driver loaded but no devices found\n");
727 void cleanup_module(void)
731 unregister_chrdev(LP_MAJOR
, "lp");
732 for (offset
= 0; offset
< LP_NO
; offset
++) {
733 if (lp_table
[offset
].dev
== NULL
)
735 parport_unregister_device(lp_table
[offset
].dev
);