1 /* IEEE-1284 operations for parport.
3 * This file is for generic IEEE 1284 operations. The idea is that
4 * they are used by the low-level drivers. If they have a special way
5 * of doing something, they can provide their own routines (and put
6 * the function pointers in port->ops); if not, they can just use these
9 * Note: Make no assumptions about hardware or architecture in this file!
11 * Author: Tim Waugh <tim@cyberelk.demon.co.uk>
12 * Fixed AUTOFD polarity in ecp_forward_to_reverse(). Fred Barnes, 1999
16 #include <linux/config.h>
17 #include <linux/parport.h>
18 #include <linux/delay.h>
19 #include <asm/uaccess.h>
21 #define DEBUG /* undef me for production */
23 #ifdef CONFIG_LP_CONSOLE
24 #undef DEBUG /* Don't want a garbled console */
28 #define DPRINTK(stuff...) printk (stuff)
30 #define DPRINTK(stuff...)
34 * One-way data transfer functions. *
37 /* Compatibility mode. */
38 size_t parport_ieee1284_write_compat (struct parport
*port
,
39 const void *buffer
, size_t len
,
44 const unsigned char *addr
= buffer
;
46 struct pardevice
*dev
= port
->physport
->cad
;
47 unsigned char ctl
= (PARPORT_CONTROL_SELECT
48 | PARPORT_CONTROL_INIT
);
50 if (port
->irq
!= PARPORT_IRQ_NONE
) {
51 parport_enable_irq (port
);
54 /* Clear out previous irqs. */
55 while (!down_trylock (&port
->physport
->ieee1284
.irq
));
58 port
->physport
->ieee1284
.phase
= IEEE1284_PH_FWD_DATA
;
59 parport_write_control (port
, ctl
);
60 parport_data_forward (port
);
62 long expire
= jiffies
+ dev
->timeout
;
63 long wait
= (HZ
+ 99) / 100;
64 unsigned char mask
= (PARPORT_STATUS_ERROR
65 | PARPORT_STATUS_BUSY
);
66 unsigned char val
= (PARPORT_STATUS_ERROR
67 | PARPORT_STATUS_BUSY
);
69 /* Wait until the peripheral's ready */
71 /* Is the peripheral ready yet? */
72 if (!parport_wait_peripheral (port
, mask
, val
))
76 /* Is the peripheral upset? */
77 if ((parport_read_status (port
) &
78 (PARPORT_STATUS_PAPEROUT
|
79 PARPORT_STATUS_SELECT
|
80 PARPORT_STATUS_ERROR
))
81 != (PARPORT_STATUS_SELECT
|
82 PARPORT_STATUS_ERROR
))
83 /* If nFault is asserted (i.e. no
84 * error) and PAPEROUT and SELECT are
85 * just red herrings, give the driver
86 * a chance to check it's happy with
87 * that before continuing. */
90 /* Have we run out of time? */
91 if (!time_before (jiffies
, expire
))
94 /* Yield the port for a while. If this is the
95 first time around the loop, don't let go of
96 the port. This way, we find out if we have
97 our interrupt handler called. */
98 if (count
&& no_irq
) {
99 parport_release (dev
);
100 __set_current_state (TASK_INTERRUPTIBLE
);
101 schedule_timeout (wait
);
102 parport_claim_or_block (dev
);
105 /* We must have the device claimed here */
106 parport_wait_event (port
, wait
);
108 /* Is there a signal pending? */
109 if (signal_pending (current
))
112 /* Wait longer next time. */
114 } while (time_before (jiffies
, expire
));
116 if (signal_pending (current
))
119 DPRINTK (KERN_DEBUG
"%s: Timed out\n", port
->name
);
123 /* Write the character to the data lines. */
125 parport_write_data (port
, byte
);
129 parport_write_control (port
, ctl
| PARPORT_CONTROL_STROBE
);
130 udelay (1); /* strobe */
132 parport_write_control (port
, ctl
);
133 udelay (1); /* hold */
135 /* Assume the peripheral received it. */
138 /* Let another process run if it needs to. */
139 if (time_before (jiffies
, expire
))
140 if (!parport_yield_blocking (dev
)
141 && current
->need_resched
)
145 port
->physport
->ieee1284
.phase
= IEEE1284_PH_FWD_IDLE
;
151 size_t parport_ieee1284_read_nibble (struct parport
*port
,
152 void *buffer
, size_t len
,
155 #ifndef CONFIG_PARPORT_1284
158 unsigned char *buf
= buffer
;
160 unsigned char byte
= 0;
162 len
*= 2; /* in nibbles */
163 for (i
=0; i
< len
; i
++) {
164 unsigned char nibble
;
166 /* Does the error line indicate end of data? */
167 if (((i
& 1) == 0) &&
168 (parport_read_status(port
) & PARPORT_STATUS_ERROR
)) {
169 port
->physport
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DNA
;
171 "%s: No more nibble data (%d bytes)\n",
174 /* Go to reverse idle phase. */
175 parport_frob_control (port
,
176 PARPORT_CONTROL_AUTOFD
,
177 PARPORT_CONTROL_AUTOFD
);
178 port
->physport
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
182 /* Event 7: Set nAutoFd low. */
183 parport_frob_control (port
,
184 PARPORT_CONTROL_AUTOFD
,
185 PARPORT_CONTROL_AUTOFD
);
187 /* Event 9: nAck goes low. */
188 port
->ieee1284
.phase
= IEEE1284_PH_REV_DATA
;
189 if (parport_wait_peripheral (port
,
190 PARPORT_STATUS_ACK
, 0)) {
191 /* Timeout -- no more data? */
193 "%s: Nibble timeout at event 9 (%d bytes)\n",
200 nibble
= parport_read_status (port
) >> 3;
202 if ((nibble
& 0x10) == 0)
206 /* Event 10: Set nAutoFd high. */
207 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
209 /* Event 11: nAck goes high. */
210 if (parport_wait_peripheral (port
,
212 PARPORT_STATUS_ACK
)) {
213 /* Timeout -- no more data? */
215 "%s: Nibble timeout at event 11\n",
228 i
/= 2; /* i is now in bytes */
231 /* Read the last nibble without checking data avail. */
232 port
= port
->physport
;
233 if (parport_read_status (port
) & PARPORT_STATUS_ERROR
)
234 port
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DNA
;
236 port
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DAVAIL
;
240 #endif /* IEEE1284 support */
244 size_t parport_ieee1284_read_byte (struct parport
*port
,
245 void *buffer
, size_t len
,
248 #ifndef CONFIG_PARPORT_1284
251 unsigned char *buf
= buffer
;
254 for (count
= 0; count
< len
; count
++) {
257 /* Data available? */
258 if (parport_read_status (port
) & PARPORT_STATUS_ERROR
) {
259 port
->physport
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DNA
;
261 "%s: No more byte data (%Zd bytes)\n",
264 /* Go to reverse idle phase. */
265 parport_frob_control (port
,
266 PARPORT_CONTROL_AUTOFD
,
267 PARPORT_CONTROL_AUTOFD
);
268 port
->physport
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
272 /* Event 14: Place data bus in high impedance state. */
273 parport_data_reverse (port
);
275 /* Event 7: Set nAutoFd low. */
276 parport_frob_control (port
,
277 PARPORT_CONTROL_AUTOFD
,
278 PARPORT_CONTROL_AUTOFD
);
280 /* Event 9: nAck goes low. */
281 port
->physport
->ieee1284
.phase
= IEEE1284_PH_REV_DATA
;
282 if (parport_wait_peripheral (port
,
285 /* Timeout -- no more data? */
286 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
,
288 DPRINTK (KERN_DEBUG
"%s: Byte timeout at event 9\n",
293 byte
= parport_read_data (port
);
296 /* Event 10: Set nAutoFd high */
297 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
299 /* Event 11: nAck goes high. */
300 if (parport_wait_peripheral (port
,
302 PARPORT_STATUS_ACK
)) {
303 /* Timeout -- no more data? */
304 DPRINTK (KERN_DEBUG
"%s: Byte timeout at event 11\n",
309 /* Event 16: Set nStrobe low. */
310 parport_frob_control (port
,
311 PARPORT_CONTROL_STROBE
,
312 PARPORT_CONTROL_STROBE
);
315 /* Event 17: Set nStrobe high. */
316 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
320 /* Read the last byte without checking data avail. */
321 port
= port
->physport
;
322 if (parport_read_status (port
) & PARPORT_STATUS_ERROR
)
323 port
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DNA
;
325 port
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DAVAIL
;
329 #endif /* IEEE1284 support */
336 #ifdef CONFIG_PARPORT_1284
339 int ecp_forward_to_reverse (struct parport
*port
)
343 /* Event 38: Set nAutoFd low */
344 parport_frob_control (port
,
345 PARPORT_CONTROL_AUTOFD
,
346 PARPORT_CONTROL_AUTOFD
);
347 parport_data_reverse (port
);
350 /* Event 39: Set nInit low to initiate bus reversal */
351 parport_frob_control (port
,
352 PARPORT_CONTROL_INIT
,
355 /* Event 40: PError goes low */
356 retval
= parport_wait_peripheral (port
,
357 PARPORT_STATUS_PAPEROUT
, 0);
360 DPRINTK (KERN_DEBUG
"%s: ECP direction: reverse\n",
362 port
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
369 int ecp_reverse_to_forward (struct parport
*port
)
373 /* Event 47: Set nInit high */
374 parport_frob_control (port
,
376 | PARPORT_CONTROL_AUTOFD
,
378 | PARPORT_CONTROL_AUTOFD
);
380 /* Event 49: PError goes high */
381 retval
= parport_wait_peripheral (port
,
382 PARPORT_STATUS_PAPEROUT
,
383 PARPORT_STATUS_PAPEROUT
);
386 parport_data_forward (port
);
387 DPRINTK (KERN_DEBUG
"%s: ECP direction: forward\n",
389 port
->ieee1284
.phase
= IEEE1284_PH_FWD_IDLE
;
395 #endif /* IEEE1284 support */
397 /* ECP mode, forward channel, data. */
398 size_t parport_ieee1284_ecp_write_data (struct parport
*port
,
399 const void *buffer
, size_t len
,
402 #ifndef CONFIG_PARPORT_1284
405 const unsigned char *buf
= buffer
;
409 port
= port
->physport
;
411 if (port
->ieee1284
.phase
!= IEEE1284_PH_FWD_IDLE
)
412 if (ecp_reverse_to_forward (port
))
415 port
->ieee1284
.phase
= IEEE1284_PH_FWD_DATA
;
417 /* HostAck high (data, not command) */
418 parport_frob_control (port
,
419 PARPORT_CONTROL_AUTOFD
420 | PARPORT_CONTROL_STROBE
421 | PARPORT_CONTROL_INIT
,
422 PARPORT_CONTROL_INIT
);
423 for (written
= 0; written
< len
; written
++, buf
++) {
424 long expire
= jiffies
+ port
->cad
->timeout
;
429 parport_write_data (port
, byte
);
430 parport_frob_control (port
, PARPORT_CONTROL_STROBE
,
431 PARPORT_CONTROL_STROBE
);
433 for (retry
= 0; retry
< 100; retry
++) {
434 if (!parport_wait_peripheral (port
,
435 PARPORT_STATUS_BUSY
, 0))
438 if (signal_pending (current
)) {
439 parport_frob_control (port
,
440 PARPORT_CONTROL_STROBE
,
446 /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
447 DPRINTK (KERN_DEBUG
"%s: ECP transfer stalled!\n", port
->name
);
449 parport_frob_control (port
, PARPORT_CONTROL_INIT
,
450 PARPORT_CONTROL_INIT
);
452 if (parport_read_status (port
) & PARPORT_STATUS_PAPEROUT
) {
454 parport_frob_control (port
, PARPORT_CONTROL_INIT
, 0);
458 parport_frob_control (port
, PARPORT_CONTROL_INIT
, 0);
460 if (!(parport_read_status (port
) & PARPORT_STATUS_PAPEROUT
))
463 DPRINTK (KERN_DEBUG
"%s: Host transfer recovered\n",
466 if (time_after_eq (jiffies
, expire
)) break;
469 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
471 if (parport_wait_peripheral (port
,
473 PARPORT_STATUS_BUSY
))
474 /* Peripheral hasn't accepted the data. */
478 port
->ieee1284
.phase
= IEEE1284_PH_FWD_IDLE
;
481 #endif /* IEEE1284 support */
484 /* ECP mode, reverse channel, data. */
485 size_t parport_ieee1284_ecp_read_data (struct parport
*port
,
486 void *buffer
, size_t len
, int flags
)
488 #ifndef CONFIG_PARPORT_1284
491 struct pardevice
*dev
= port
->cad
;
492 unsigned char *buf
= buffer
;
493 int rle_count
= 0; /* shut gcc up */
497 port
= port
->physport
;
499 if (port
->ieee1284
.phase
!= IEEE1284_PH_REV_IDLE
)
500 if (ecp_forward_to_reverse (port
))
503 port
->ieee1284
.phase
= IEEE1284_PH_REV_DATA
;
505 /* Set HostAck low to start accepting data. */
506 parport_frob_control (port
,
507 PARPORT_CONTROL_AUTOFD
508 | PARPORT_CONTROL_STROBE
509 | PARPORT_CONTROL_INIT
,
510 PARPORT_CONTROL_AUTOFD
);
511 while (count
< len
) {
512 long expire
= jiffies
+ dev
->timeout
;
516 /* Event 43: Peripheral sets nAck low. It can take as
518 while (parport_wait_peripheral (port
, PARPORT_STATUS_ACK
, 0)) {
519 /* The peripheral hasn't given us data in
520 35ms. If we have data to give back to the
521 caller, do it now. */
525 /* If we've used up all the time we were allowed,
526 give up altogether. */
527 if (!time_before (jiffies
, expire
))
530 /* Yield the port for a while. */
531 if (count
&& dev
->port
->irq
!= PARPORT_IRQ_NONE
) {
532 parport_release (dev
);
533 __set_current_state (TASK_INTERRUPTIBLE
);
534 schedule_timeout ((HZ
+ 24) / 25);
535 parport_claim_or_block (dev
);
538 /* We must have the device claimed here. */
539 parport_wait_event (port
, (HZ
+ 24) / 25);
541 /* Is there a signal pending? */
542 if (signal_pending (current
))
546 /* Is this a command? */
548 /* The last byte was a run-length count, so
549 this can't be as well. */
552 command
= (parport_read_status (port
) &
553 PARPORT_STATUS_BUSY
) ? 1 : 0;
556 byte
= parport_read_data (port
);
558 /* If this is a channel command, rather than an RLE
559 command or a normal data byte, don't accept it. */
562 DPRINTK (KERN_DEBUG
"%s: stopping short at "
563 "channel command (%02x)\n",
567 else if (port
->ieee1284
.mode
!= IEEE1284_MODE_ECPRLE
)
568 DPRINTK (KERN_DEBUG
"%s: device illegally "
569 "using RLE; accepting anyway\n",
572 rle_count
= byte
+ 1;
574 /* Are we allowed to read that many bytes? */
575 if (rle_count
> (len
- count
)) {
576 DPRINTK (KERN_DEBUG
"%s: leaving %d RLE bytes "
577 "for next time\n", port
->name
,
585 /* Event 44: Set HostAck high, acknowledging handshake. */
586 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
588 /* Event 45: The peripheral has 35ms to set nAck high. */
589 if (parport_wait_peripheral (port
, PARPORT_STATUS_ACK
,
590 PARPORT_STATUS_ACK
)) {
591 /* It's gone wrong. Return what data we have
593 DPRINTK (KERN_DEBUG
"ECP read timed out at 45\n");
597 "%s: command ignored (%02x)\n",
603 /* Event 46: Set HostAck low and accept the data. */
604 parport_frob_control (port
,
605 PARPORT_CONTROL_AUTOFD
,
606 PARPORT_CONTROL_AUTOFD
);
608 /* If we just read a run-length count, fetch the data. */
612 /* If this is the byte after a run-length count, decompress. */
615 memset (buf
, byte
, rle_count
);
618 DPRINTK (KERN_DEBUG
"%s: decompressed to %d bytes\n",
619 port
->name
, rle_count
);
621 /* Normal data byte. */
628 port
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
630 #endif /* IEEE1284 support */
633 /* ECP mode, forward channel, commands. */
634 size_t parport_ieee1284_ecp_write_addr (struct parport
*port
,
635 const void *buffer
, size_t len
,
638 #ifndef CONFIG_PARPORT_1284
641 const unsigned char *buf
= buffer
;
645 port
= port
->physport
;
647 if (port
->ieee1284
.phase
!= IEEE1284_PH_FWD_IDLE
)
648 if (ecp_reverse_to_forward (port
))
651 port
->ieee1284
.phase
= IEEE1284_PH_FWD_DATA
;
653 /* HostAck low (command, not data) */
654 parport_frob_control (port
,
655 PARPORT_CONTROL_AUTOFD
656 | PARPORT_CONTROL_STROBE
657 | PARPORT_CONTROL_INIT
,
658 PARPORT_CONTROL_AUTOFD
659 | PARPORT_CONTROL_INIT
);
660 for (written
= 0; written
< len
; written
++, buf
++) {
661 long expire
= jiffies
+ port
->cad
->timeout
;
666 parport_write_data (port
, byte
);
667 parport_frob_control (port
, PARPORT_CONTROL_STROBE
,
668 PARPORT_CONTROL_STROBE
);
670 for (retry
= 0; retry
< 100; retry
++) {
671 if (!parport_wait_peripheral (port
,
672 PARPORT_STATUS_BUSY
, 0))
675 if (signal_pending (current
)) {
676 parport_frob_control (port
,
677 PARPORT_CONTROL_STROBE
,
683 /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
684 DPRINTK (KERN_DEBUG
"%s: ECP transfer stalled!\n", port
->name
);
686 parport_frob_control (port
, PARPORT_CONTROL_INIT
,
687 PARPORT_CONTROL_INIT
);
689 if (parport_read_status (port
) & PARPORT_STATUS_PAPEROUT
) {
691 parport_frob_control (port
, PARPORT_CONTROL_INIT
, 0);
695 parport_frob_control (port
, PARPORT_CONTROL_INIT
, 0);
697 if (!(parport_read_status (port
) & PARPORT_STATUS_PAPEROUT
))
700 DPRINTK (KERN_DEBUG
"%s: Host transfer recovered\n",
703 if (time_after_eq (jiffies
, expire
)) break;
706 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
708 if (parport_wait_peripheral (port
,
710 PARPORT_STATUS_BUSY
))
711 /* Peripheral hasn't accepted the data. */
715 port
->ieee1284
.phase
= IEEE1284_PH_FWD_IDLE
;
718 #endif /* IEEE1284 support */
725 /* EPP mode, forward channel, data. */
726 size_t parport_ieee1284_epp_write_data (struct parport
*port
,
727 const void *buffer
, size_t len
,
730 /* This is untested */
731 unsigned char *bp
= (unsigned char *) buffer
;
734 parport_frob_control (port
,
735 PARPORT_CONTROL_STROBE
|
736 PARPORT_CONTROL_AUTOFD
|
737 PARPORT_CONTROL_SELECT
,
738 PARPORT_CONTROL_STROBE
|
739 PARPORT_CONTROL_SELECT
);
740 port
->ops
->data_forward (port
);
741 for (; len
> 0; len
--, bp
++) {
742 /* Event 62: Write data and strobe data */
743 parport_write_data (port
, *bp
);
744 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
,
745 PARPORT_CONTROL_AUTOFD
);
748 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
, 0, 10))
752 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
755 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
,
756 PARPORT_STATUS_BUSY
, 5))
763 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
768 /* EPP mode, reverse channel, data. */
769 size_t parport_ieee1284_epp_read_data (struct parport
*port
,
770 void *buffer
, size_t len
,
773 /* This is untested. */
774 unsigned char *bp
= (unsigned char *) buffer
;
777 parport_frob_control (port
,
778 PARPORT_CONTROL_STROBE
|
779 PARPORT_CONTROL_SELECT
, 0);
780 port
->ops
->data_reverse (port
);
781 for (; len
> 0; len
--, bp
++) {
782 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
785 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
,
786 PARPORT_STATUS_BUSY
, 10))
789 *bp
= parport_read_data (port
);
791 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
,
792 PARPORT_CONTROL_AUTOFD
);
794 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
, 0, 5))
799 port
->ops
->data_forward (port
);
804 /* EPP mode, forward channel, addresses. */
805 size_t parport_ieee1284_epp_write_addr (struct parport
*port
,
806 const void *buffer
, size_t len
,
809 /* This is untested */
810 unsigned char *bp
= (unsigned char *) buffer
;
813 parport_frob_control (port
,
814 PARPORT_CONTROL_STROBE
|
815 PARPORT_CONTROL_SELECT
|
816 PARPORT_CONTROL_AUTOFD
,
817 PARPORT_CONTROL_STROBE
|
818 PARPORT_CONTROL_SELECT
);
819 port
->ops
->data_forward (port
);
820 for (; len
> 0; len
--, bp
++) {
821 /* Write data and assert nAStrb. */
822 parport_write_data (port
, *bp
);
823 parport_frob_control (port
, PARPORT_CONTROL_SELECT
,
824 PARPORT_CONTROL_SELECT
);
826 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
,
827 PARPORT_STATUS_BUSY
, 10))
830 parport_frob_control (port
, PARPORT_CONTROL_SELECT
, 0);
832 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
, 0, 5))
838 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
843 /* EPP mode, reverse channel, addresses. */
844 size_t parport_ieee1284_epp_read_addr (struct parport
*port
,
845 void *buffer
, size_t len
,
848 /* This is untested. */
849 unsigned char *bp
= (unsigned char *) buffer
;
852 parport_frob_control (port
,
853 PARPORT_CONTROL_STROBE
|
854 PARPORT_CONTROL_AUTOFD
, 0);
855 port
->ops
->data_reverse (port
);
856 for (; len
> 0; len
--, bp
++) {
857 parport_frob_control (port
, PARPORT_CONTROL_SELECT
, 0);
860 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
,
861 PARPORT_STATUS_BUSY
, 10))
864 *bp
= parport_read_data (port
);
866 parport_frob_control (port
, PARPORT_CONTROL_SELECT
,
867 PARPORT_CONTROL_SELECT
);
869 if (parport_poll_peripheral (port
, PARPORT_STATUS_BUSY
, 0, 5))
874 port
->ops
->data_forward (port
);