Merge ../torvalds-2.6/
[pandora-kernel.git] / drivers / parport / ieee1284_ops.c
1 /* IEEE-1284 operations for parport.
2  *
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
7  * as a fallback.
8  *
9  * Note: Make no assumptions about hardware or architecture in this file!
10  *
11  * Author: Tim Waugh <tim@cyberelk.demon.co.uk>
12  * Fixed AUTOFD polarity in ecp_forward_to_reverse().  Fred Barnes, 1999
13  * Software emulated EPP fixes, Fred Barnes, 04/2001.
14  */
15
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/parport.h>
20 #include <linux/delay.h>
21 #include <linux/sched.h>
22 #include <asm/uaccess.h>
23
24 #undef DEBUG /* undef me for production */
25
26 #ifdef CONFIG_LP_CONSOLE
27 #undef DEBUG /* Don't want a garbled console */
28 #endif
29
30 #ifdef DEBUG
31 #define DPRINTK(stuff...) printk (stuff)
32 #else
33 #define DPRINTK(stuff...)
34 #endif
35
36 /***                                *
37  * One-way data transfer functions. *
38  *                                ***/
39
40 /* Compatibility mode. */
41 size_t parport_ieee1284_write_compat (struct parport *port,
42                                       const void *buffer, size_t len,
43                                       int flags)
44 {
45         int no_irq = 1;
46         ssize_t count = 0;
47         const unsigned char *addr = buffer;
48         unsigned char byte;
49         struct pardevice *dev = port->physport->cad;
50         unsigned char ctl = (PARPORT_CONTROL_SELECT
51                              | PARPORT_CONTROL_INIT);
52
53         if (port->irq != PARPORT_IRQ_NONE) {
54                 parport_enable_irq (port);
55                 no_irq = 0;
56         }
57
58         port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
59         parport_write_control (port, ctl);
60         parport_data_forward (port);
61         while (count < len) {
62                 unsigned long expire = jiffies + dev->timeout;
63                 long wait = msecs_to_jiffies(10);
64                 unsigned char mask = (PARPORT_STATUS_ERROR
65                                       | PARPORT_STATUS_BUSY);
66                 unsigned char val = (PARPORT_STATUS_ERROR
67                                      | PARPORT_STATUS_BUSY);
68
69                 /* Wait until the peripheral's ready */
70                 do {
71                         /* Is the peripheral ready yet? */
72                         if (!parport_wait_peripheral (port, mask, val))
73                                 /* Skip the loop */
74                                 goto ready;
75
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. */
88                                 goto stop;
89
90                         /* Have we run out of time? */
91                         if (!time_before (jiffies, expire))
92                                 break;
93
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                                 schedule_timeout_interruptible(wait);
101                                 parport_claim_or_block (dev);
102                         }
103                         else
104                                 /* We must have the device claimed here */
105                                 parport_wait_event (port, wait);
106
107                         /* Is there a signal pending? */
108                         if (signal_pending (current))
109                                 break;
110
111                         /* Wait longer next time. */
112                         wait *= 2;
113                 } while (time_before (jiffies, expire));
114
115                 if (signal_pending (current))
116                         break;
117
118                 DPRINTK (KERN_DEBUG "%s: Timed out\n", port->name);
119                 break;
120
121         ready:
122                 /* Write the character to the data lines. */
123                 byte = *addr++;
124                 parport_write_data (port, byte);
125                 udelay (1);
126
127                 /* Pulse strobe. */
128                 parport_write_control (port, ctl | PARPORT_CONTROL_STROBE);
129                 udelay (1); /* strobe */
130
131                 parport_write_control (port, ctl);
132                 udelay (1); /* hold */
133
134                 /* Assume the peripheral received it. */
135                 count++;
136
137                 /* Let another process run if it needs to. */
138                 if (time_before (jiffies, expire))
139                         if (!parport_yield_blocking (dev)
140                             && need_resched())
141                                 schedule ();
142         }
143  stop:
144         port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
145
146         return count;
147 }
148
149 /* Nibble mode. */
150 size_t parport_ieee1284_read_nibble (struct parport *port, 
151                                      void *buffer, size_t len,
152                                      int flags)
153 {
154 #ifndef CONFIG_PARPORT_1284
155         return 0;
156 #else
157         unsigned char *buf = buffer;
158         int i;
159         unsigned char byte = 0;
160
161         len *= 2; /* in nibbles */
162         for (i=0; i < len; i++) {
163                 unsigned char nibble;
164
165                 /* Does the error line indicate end of data? */
166                 if (((i & 1) == 0) &&
167                     (parport_read_status(port) & PARPORT_STATUS_ERROR)) {
168                         port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
169                         DPRINTK (KERN_DEBUG
170                                 "%s: No more nibble data (%d bytes)\n",
171                                 port->name, i/2);
172
173                         /* Go to reverse idle phase. */
174                         parport_frob_control (port,
175                                               PARPORT_CONTROL_AUTOFD,
176                                               PARPORT_CONTROL_AUTOFD);
177                         port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
178                         break;
179                 }
180
181                 /* Event 7: Set nAutoFd low. */
182                 parport_frob_control (port,
183                                       PARPORT_CONTROL_AUTOFD,
184                                       PARPORT_CONTROL_AUTOFD);
185
186                 /* Event 9: nAck goes low. */
187                 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
188                 if (parport_wait_peripheral (port,
189                                              PARPORT_STATUS_ACK, 0)) {
190                         /* Timeout -- no more data? */
191                         DPRINTK (KERN_DEBUG
192                                  "%s: Nibble timeout at event 9 (%d bytes)\n",
193                                  port->name, i/2);
194                         parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
195                         break;
196                 }
197
198
199                 /* Read a nibble. */
200                 nibble = parport_read_status (port) >> 3;
201                 nibble &= ~8;
202                 if ((nibble & 0x10) == 0)
203                         nibble |= 8;
204                 nibble &= 0xf;
205
206                 /* Event 10: Set nAutoFd high. */
207                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
208
209                 /* Event 11: nAck goes high. */
210                 if (parport_wait_peripheral (port,
211                                              PARPORT_STATUS_ACK,
212                                              PARPORT_STATUS_ACK)) {
213                         /* Timeout -- no more data? */
214                         DPRINTK (KERN_DEBUG
215                                  "%s: Nibble timeout at event 11\n",
216                                  port->name);
217                         break;
218                 }
219
220                 if (i & 1) {
221                         /* Second nibble */
222                         byte |= nibble << 4;
223                         *buf++ = byte;
224                 } else 
225                         byte = nibble;
226         }
227
228         i /= 2; /* i is now in bytes */
229
230         if (i == len) {
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;
235                 else
236                         port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
237         }
238
239         return i;
240 #endif /* IEEE1284 support */
241 }
242
243 /* Byte mode. */
244 size_t parport_ieee1284_read_byte (struct parport *port,
245                                    void *buffer, size_t len,
246                                    int flags)
247 {
248 #ifndef CONFIG_PARPORT_1284
249         return 0;
250 #else
251         unsigned char *buf = buffer;
252         ssize_t count = 0;
253
254         for (count = 0; count < len; count++) {
255                 unsigned char byte;
256
257                 /* Data available? */
258                 if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
259                         port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
260                         DPRINTK (KERN_DEBUG
261                                  "%s: No more byte data (%Zd bytes)\n",
262                                  port->name, count);
263
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;
269                         break;
270                 }
271
272                 /* Event 14: Place data bus in high impedance state. */
273                 parport_data_reverse (port);
274
275                 /* Event 7: Set nAutoFd low. */
276                 parport_frob_control (port,
277                                       PARPORT_CONTROL_AUTOFD,
278                                       PARPORT_CONTROL_AUTOFD);
279
280                 /* Event 9: nAck goes low. */
281                 port->physport->ieee1284.phase = IEEE1284_PH_REV_DATA;
282                 if (parport_wait_peripheral (port,
283                                              PARPORT_STATUS_ACK,
284                                              0)) {
285                         /* Timeout -- no more data? */
286                         parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
287                                                  0);
288                         DPRINTK (KERN_DEBUG "%s: Byte timeout at event 9\n",
289                                  port->name);
290                         break;
291                 }
292
293                 byte = parport_read_data (port);
294                 *buf++ = byte;
295
296                 /* Event 10: Set nAutoFd high */
297                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
298
299                 /* Event 11: nAck goes high. */
300                 if (parport_wait_peripheral (port,
301                                              PARPORT_STATUS_ACK,
302                                              PARPORT_STATUS_ACK)) {
303                         /* Timeout -- no more data? */
304                         DPRINTK (KERN_DEBUG "%s: Byte timeout at event 11\n",
305                                  port->name);
306                         break;
307                 }
308
309                 /* Event 16: Set nStrobe low. */
310                 parport_frob_control (port,
311                                       PARPORT_CONTROL_STROBE,
312                                       PARPORT_CONTROL_STROBE);
313                 udelay (5);
314
315                 /* Event 17: Set nStrobe high. */
316                 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
317         }
318
319         if (count == len) {
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;
324                 else
325                         port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
326         }
327
328         return count;
329 #endif /* IEEE1284 support */
330 }
331
332 /***              *
333  * ECP Functions. *
334  *              ***/
335
336 #ifdef CONFIG_PARPORT_1284
337
338 static inline
339 int ecp_forward_to_reverse (struct parport *port)
340 {
341         int retval;
342
343         /* Event 38: Set nAutoFd low */
344         parport_frob_control (port,
345                               PARPORT_CONTROL_AUTOFD,
346                               PARPORT_CONTROL_AUTOFD);
347         parport_data_reverse (port);
348         udelay (5);
349
350         /* Event 39: Set nInit low to initiate bus reversal */
351         parport_frob_control (port,
352                               PARPORT_CONTROL_INIT,
353                               0);
354
355         /* Event 40: PError goes low */
356         retval = parport_wait_peripheral (port,
357                                           PARPORT_STATUS_PAPEROUT, 0);
358
359         if (!retval) {
360                 DPRINTK (KERN_DEBUG "%s: ECP direction: reverse\n",
361                          port->name);
362                 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
363         } else {
364                 DPRINTK (KERN_DEBUG "%s: ECP direction: failed to reverse\n",
365                          port->name);
366                 port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
367         }
368
369         return retval;
370 }
371
372 static inline
373 int ecp_reverse_to_forward (struct parport *port)
374 {
375         int retval;
376
377         /* Event 47: Set nInit high */
378         parport_frob_control (port,
379                               PARPORT_CONTROL_INIT
380                               | PARPORT_CONTROL_AUTOFD,
381                               PARPORT_CONTROL_INIT
382                               | PARPORT_CONTROL_AUTOFD);
383
384         /* Event 49: PError goes high */
385         retval = parport_wait_peripheral (port,
386                                           PARPORT_STATUS_PAPEROUT,
387                                           PARPORT_STATUS_PAPEROUT);
388
389         if (!retval) {
390                 parport_data_forward (port);
391                 DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
392                          port->name);
393                 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
394         } else {
395                 DPRINTK (KERN_DEBUG
396                          "%s: ECP direction: failed to switch forward\n",
397                          port->name);
398                 port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
399         }
400
401
402         return retval;
403 }
404
405 #endif /* IEEE1284 support */
406
407 /* ECP mode, forward channel, data. */
408 size_t parport_ieee1284_ecp_write_data (struct parport *port,
409                                         const void *buffer, size_t len,
410                                         int flags)
411 {
412 #ifndef CONFIG_PARPORT_1284
413         return 0;
414 #else
415         const unsigned char *buf = buffer;
416         size_t written;
417         int retry;
418
419         port = port->physport;
420
421         if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
422                 if (ecp_reverse_to_forward (port))
423                         return 0;
424
425         port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
426
427         /* HostAck high (data, not command) */
428         parport_frob_control (port,
429                               PARPORT_CONTROL_AUTOFD
430                               | PARPORT_CONTROL_STROBE
431                               | PARPORT_CONTROL_INIT,
432                               PARPORT_CONTROL_INIT);
433         for (written = 0; written < len; written++, buf++) {
434                 unsigned long expire = jiffies + port->cad->timeout;
435                 unsigned char byte;
436
437                 byte = *buf;
438         try_again:
439                 parport_write_data (port, byte);
440                 parport_frob_control (port, PARPORT_CONTROL_STROBE,
441                                       PARPORT_CONTROL_STROBE);
442                 udelay (5);
443                 for (retry = 0; retry < 100; retry++) {
444                         if (!parport_wait_peripheral (port,
445                                                       PARPORT_STATUS_BUSY, 0))
446                                 goto success;
447
448                         if (signal_pending (current)) {
449                                 parport_frob_control (port,
450                                                       PARPORT_CONTROL_STROBE,
451                                                       0);
452                                 break;
453                         }
454                 }
455
456                 /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
457                 DPRINTK (KERN_DEBUG "%s: ECP transfer stalled!\n", port->name);
458
459                 parport_frob_control (port, PARPORT_CONTROL_INIT,
460                                       PARPORT_CONTROL_INIT);
461                 udelay (50);
462                 if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
463                         /* It's buggered. */
464                         parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
465                         break;
466                 }
467
468                 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
469                 udelay (50);
470                 if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
471                         break;
472
473                 DPRINTK (KERN_DEBUG "%s: Host transfer recovered\n",
474                          port->name);
475
476                 if (time_after_eq (jiffies, expire)) break;
477                 goto try_again;
478         success:
479                 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
480                 udelay (5);
481                 if (parport_wait_peripheral (port,
482                                              PARPORT_STATUS_BUSY,
483                                              PARPORT_STATUS_BUSY))
484                         /* Peripheral hasn't accepted the data. */
485                         break;
486         }
487
488         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
489
490         return written;
491 #endif /* IEEE1284 support */
492 }
493
494 /* ECP mode, reverse channel, data. */
495 size_t parport_ieee1284_ecp_read_data (struct parport *port,
496                                        void *buffer, size_t len, int flags)
497 {
498 #ifndef CONFIG_PARPORT_1284
499         return 0;
500 #else
501         struct pardevice *dev = port->cad;
502         unsigned char *buf = buffer;
503         int rle_count = 0; /* shut gcc up */
504         unsigned char ctl;
505         int rle = 0;
506         ssize_t count = 0;
507
508         port = port->physport;
509
510         if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE)
511                 if (ecp_forward_to_reverse (port))
512                         return 0;
513
514         port->ieee1284.phase = IEEE1284_PH_REV_DATA;
515
516         /* Set HostAck low to start accepting data. */
517         ctl = parport_read_control (port);
518         ctl &= ~(PARPORT_CONTROL_STROBE | PARPORT_CONTROL_INIT |
519                  PARPORT_CONTROL_AUTOFD);
520         parport_write_control (port,
521                                ctl | PARPORT_CONTROL_AUTOFD);
522         while (count < len) {
523                 unsigned long expire = jiffies + dev->timeout;
524                 unsigned char byte;
525                 int command;
526
527                 /* Event 43: Peripheral sets nAck low. It can take as
528                    long as it wants. */
529                 while (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
530                         /* The peripheral hasn't given us data in
531                            35ms.  If we have data to give back to the
532                            caller, do it now. */
533                         if (count)
534                                 goto out;
535
536                         /* If we've used up all the time we were allowed,
537                            give up altogether. */
538                         if (!time_before (jiffies, expire))
539                                 goto out;
540
541                         /* Yield the port for a while. */
542                         if (count && dev->port->irq != PARPORT_IRQ_NONE) {
543                                 parport_release (dev);
544                                 schedule_timeout_interruptible(msecs_to_jiffies(40));
545                                 parport_claim_or_block (dev);
546                         }
547                         else
548                                 /* We must have the device claimed here. */
549                                 parport_wait_event (port, msecs_to_jiffies(40));
550
551                         /* Is there a signal pending? */
552                         if (signal_pending (current))
553                                 goto out;
554                 }
555
556                 /* Is this a command? */
557                 if (rle)
558                         /* The last byte was a run-length count, so
559                            this can't be as well. */
560                         command = 0;
561                 else
562                         command = (parport_read_status (port) &
563                                    PARPORT_STATUS_BUSY) ? 1 : 0;
564
565                 /* Read the data. */
566                 byte = parport_read_data (port);
567
568                 /* If this is a channel command, rather than an RLE
569                    command or a normal data byte, don't accept it. */
570                 if (command) {
571                         if (byte & 0x80) {
572                                 DPRINTK (KERN_DEBUG "%s: stopping short at "
573                                          "channel command (%02x)\n",
574                                          port->name, byte);
575                                 goto out;
576                         }
577                         else if (port->ieee1284.mode != IEEE1284_MODE_ECPRLE)
578                                 DPRINTK (KERN_DEBUG "%s: device illegally "
579                                          "using RLE; accepting anyway\n",
580                                          port->name);
581
582                         rle_count = byte + 1;
583
584                         /* Are we allowed to read that many bytes? */
585                         if (rle_count > (len - count)) {
586                                 DPRINTK (KERN_DEBUG "%s: leaving %d RLE bytes "
587                                          "for next time\n", port->name,
588                                          rle_count);
589                                 break;
590                         }
591
592                         rle = 1;
593                 }
594
595                 /* Event 44: Set HostAck high, acknowledging handshake. */
596                 parport_write_control (port, ctl);
597
598                 /* Event 45: The peripheral has 35ms to set nAck high. */
599                 if (parport_wait_peripheral (port, PARPORT_STATUS_ACK,
600                                              PARPORT_STATUS_ACK)) {
601                         /* It's gone wrong.  Return what data we have
602                            to the caller. */
603                         DPRINTK (KERN_DEBUG "ECP read timed out at 45\n");
604
605                         if (command)
606                                 printk (KERN_WARNING
607                                         "%s: command ignored (%02x)\n",
608                                         port->name, byte);
609
610                         break;
611                 }
612
613                 /* Event 46: Set HostAck low and accept the data. */
614                 parport_write_control (port,
615                                        ctl | PARPORT_CONTROL_AUTOFD);
616
617                 /* If we just read a run-length count, fetch the data. */
618                 if (command)
619                         continue;
620
621                 /* If this is the byte after a run-length count, decompress. */
622                 if (rle) {
623                         rle = 0;
624                         memset (buf, byte, rle_count);
625                         buf += rle_count;
626                         count += rle_count;
627                         DPRINTK (KERN_DEBUG "%s: decompressed to %d bytes\n",
628                                  port->name, rle_count);
629                 } else {
630                         /* Normal data byte. */
631                         *buf = byte;
632                         buf++, count++;
633                 }
634         }
635
636  out:
637         port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
638         return count;
639 #endif /* IEEE1284 support */
640 }
641
642 /* ECP mode, forward channel, commands. */
643 size_t parport_ieee1284_ecp_write_addr (struct parport *port,
644                                         const void *buffer, size_t len,
645                                         int flags)
646 {
647 #ifndef CONFIG_PARPORT_1284
648         return 0;
649 #else
650         const unsigned char *buf = buffer;
651         size_t written;
652         int retry;
653
654         port = port->physport;
655
656         if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
657                 if (ecp_reverse_to_forward (port))
658                         return 0;
659
660         port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
661
662         /* HostAck low (command, not data) */
663         parport_frob_control (port,
664                               PARPORT_CONTROL_AUTOFD
665                               | PARPORT_CONTROL_STROBE
666                               | PARPORT_CONTROL_INIT,
667                               PARPORT_CONTROL_AUTOFD
668                               | PARPORT_CONTROL_INIT);
669         for (written = 0; written < len; written++, buf++) {
670                 unsigned long expire = jiffies + port->cad->timeout;
671                 unsigned char byte;
672
673                 byte = *buf;
674         try_again:
675                 parport_write_data (port, byte);
676                 parport_frob_control (port, PARPORT_CONTROL_STROBE,
677                                       PARPORT_CONTROL_STROBE);
678                 udelay (5);
679                 for (retry = 0; retry < 100; retry++) {
680                         if (!parport_wait_peripheral (port,
681                                                       PARPORT_STATUS_BUSY, 0))
682                                 goto success;
683
684                         if (signal_pending (current)) {
685                                 parport_frob_control (port,
686                                                       PARPORT_CONTROL_STROBE,
687                                                       0);
688                                 break;
689                         }
690                 }
691
692                 /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
693                 DPRINTK (KERN_DEBUG "%s: ECP transfer stalled!\n", port->name);
694
695                 parport_frob_control (port, PARPORT_CONTROL_INIT,
696                                       PARPORT_CONTROL_INIT);
697                 udelay (50);
698                 if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
699                         /* It's buggered. */
700                         parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
701                         break;
702                 }
703
704                 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
705                 udelay (50);
706                 if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
707                         break;
708
709                 DPRINTK (KERN_DEBUG "%s: Host transfer recovered\n",
710                          port->name);
711
712                 if (time_after_eq (jiffies, expire)) break;
713                 goto try_again;
714         success:
715                 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
716                 udelay (5);
717                 if (parport_wait_peripheral (port,
718                                              PARPORT_STATUS_BUSY,
719                                              PARPORT_STATUS_BUSY))
720                         /* Peripheral hasn't accepted the data. */
721                         break;
722         }
723
724         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
725
726         return written;
727 #endif /* IEEE1284 support */
728 }
729
730 /***              *
731  * EPP functions. *
732  *              ***/
733
734 /* EPP mode, forward channel, data. */
735 size_t parport_ieee1284_epp_write_data (struct parport *port,
736                                         const void *buffer, size_t len,
737                                         int flags)
738 {
739         unsigned char *bp = (unsigned char *) buffer;
740         size_t ret = 0;
741
742         /* set EPP idle state (just to make sure) with strobe low */
743         parport_frob_control (port,
744                               PARPORT_CONTROL_STROBE |
745                               PARPORT_CONTROL_AUTOFD |
746                               PARPORT_CONTROL_SELECT |
747                               PARPORT_CONTROL_INIT,
748                               PARPORT_CONTROL_STROBE |
749                               PARPORT_CONTROL_INIT);
750         port->ops->data_forward (port);
751         for (; len > 0; len--, bp++) {
752                 /* Event 62: Write data and set autofd low */
753                 parport_write_data (port, *bp);
754                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
755                                       PARPORT_CONTROL_AUTOFD);
756
757                 /* Event 58: wait for busy (nWait) to go high */
758                 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 0, 10))
759                         break;
760
761                 /* Event 63: set nAutoFd (nDStrb) high */
762                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
763
764                 /* Event 60: wait for busy (nWait) to go low */
765                 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
766                                              PARPORT_STATUS_BUSY, 5))
767                         break;
768
769                 ret++;
770         }
771
772         /* Event 61: set strobe (nWrite) high */
773         parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
774
775         return ret;
776 }
777
778 /* EPP mode, reverse channel, data. */
779 size_t parport_ieee1284_epp_read_data (struct parport *port,
780                                        void *buffer, size_t len,
781                                        int flags)
782 {
783         unsigned char *bp = (unsigned char *) buffer;
784         unsigned ret = 0;
785
786         /* set EPP idle state (just to make sure) with strobe high */
787         parport_frob_control (port,
788                               PARPORT_CONTROL_STROBE |
789                               PARPORT_CONTROL_AUTOFD |
790                               PARPORT_CONTROL_SELECT |
791                               PARPORT_CONTROL_INIT,
792                               PARPORT_CONTROL_INIT);
793         port->ops->data_reverse (port);
794         for (; len > 0; len--, bp++) {
795                 /* Event 67: set nAutoFd (nDStrb) low */
796                 parport_frob_control (port,
797                                       PARPORT_CONTROL_AUTOFD,
798                                       PARPORT_CONTROL_AUTOFD);
799                 /* Event 58: wait for Busy to go high */
800                 if (parport_wait_peripheral (port, PARPORT_STATUS_BUSY, 0)) {
801                         break;
802                 }
803
804                 *bp = parport_read_data (port);
805
806                 /* Event 63: set nAutoFd (nDStrb) high */
807                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
808
809                 /* Event 60: wait for Busy to go low */
810                 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
811                                              PARPORT_STATUS_BUSY, 5)) {
812                         break;
813                 }
814
815                 ret++;
816         }
817         port->ops->data_forward (port);
818
819         return ret;
820 }
821
822 /* EPP mode, forward channel, addresses. */
823 size_t parport_ieee1284_epp_write_addr (struct parport *port,
824                                         const void *buffer, size_t len,
825                                         int flags)
826 {
827         unsigned char *bp = (unsigned char *) buffer;
828         size_t ret = 0;
829
830         /* set EPP idle state (just to make sure) with strobe low */
831         parport_frob_control (port,
832                               PARPORT_CONTROL_STROBE |
833                               PARPORT_CONTROL_AUTOFD |
834                               PARPORT_CONTROL_SELECT |
835                               PARPORT_CONTROL_INIT,
836                               PARPORT_CONTROL_STROBE |
837                               PARPORT_CONTROL_INIT);
838         port->ops->data_forward (port);
839         for (; len > 0; len--, bp++) {
840                 /* Event 56: Write data and set nAStrb low. */
841                 parport_write_data (port, *bp);
842                 parport_frob_control (port, PARPORT_CONTROL_SELECT,
843                                       PARPORT_CONTROL_SELECT);
844
845                 /* Event 58: wait for busy (nWait) to go high */
846                 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 0, 10))
847                         break;
848
849                 /* Event 59: set nAStrb high */
850                 parport_frob_control (port, PARPORT_CONTROL_SELECT, 0);
851
852                 /* Event 60: wait for busy (nWait) to go low */
853                 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY,
854                                              PARPORT_STATUS_BUSY, 5))
855                         break;
856
857                 ret++;
858         }
859
860         /* Event 61: set strobe (nWrite) high */
861         parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
862
863         return ret;
864 }
865
866 /* EPP mode, reverse channel, addresses. */
867 size_t parport_ieee1284_epp_read_addr (struct parport *port,
868                                        void *buffer, size_t len,
869                                        int flags)
870 {
871         unsigned char *bp = (unsigned char *) buffer;
872         unsigned ret = 0;
873
874         /* Set EPP idle state (just to make sure) with strobe high */
875         parport_frob_control (port,
876                               PARPORT_CONTROL_STROBE |
877                               PARPORT_CONTROL_AUTOFD |
878                               PARPORT_CONTROL_SELECT |
879                               PARPORT_CONTROL_INIT,
880                               PARPORT_CONTROL_INIT);
881         port->ops->data_reverse (port);
882         for (; len > 0; len--, bp++) {
883                 /* Event 64: set nSelectIn (nAStrb) low */
884                 parport_frob_control (port, PARPORT_CONTROL_SELECT,
885                                       PARPORT_CONTROL_SELECT);
886
887                 /* Event 58: wait for Busy to go high */
888                 if (parport_wait_peripheral (port, PARPORT_STATUS_BUSY, 0)) {
889                         break;
890                 }
891
892                 *bp = parport_read_data (port);
893
894                 /* Event 59: set nSelectIn (nAStrb) high */
895                 parport_frob_control (port, PARPORT_CONTROL_SELECT,
896                                       PARPORT_CONTROL_SELECT);
897
898                 /* Event 60: wait for Busy to go low */
899                 if (parport_poll_peripheral (port, PARPORT_STATUS_BUSY, 
900                                              PARPORT_STATUS_BUSY, 5))
901                         break;
902
903                 ret++;
904         }
905         port->ops->data_forward (port);
906
907         return ret;
908 }
909
910 EXPORT_SYMBOL(parport_ieee1284_ecp_write_data);
911 EXPORT_SYMBOL(parport_ieee1284_ecp_read_data);
912 EXPORT_SYMBOL(parport_ieee1284_ecp_write_addr);
913 EXPORT_SYMBOL(parport_ieee1284_write_compat);
914 EXPORT_SYMBOL(parport_ieee1284_read_nibble);
915 EXPORT_SYMBOL(parport_ieee1284_read_byte);
916 EXPORT_SYMBOL(parport_ieee1284_epp_write_data);
917 EXPORT_SYMBOL(parport_ieee1284_epp_read_data);
918 EXPORT_SYMBOL(parport_ieee1284_epp_write_addr);
919 EXPORT_SYMBOL(parport_ieee1284_epp_read_addr);