cleanup
[linux-2.4.21-pre4.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/parport.h>
19 #include <linux/delay.h>
20 #include <asm/uaccess.h>
21
22 #undef DEBUG /* undef me for production */
23
24 #ifdef CONFIG_LP_CONSOLE
25 #undef DEBUG /* Don't want a garbled console */
26 #endif
27
28 #ifdef DEBUG
29 #define DPRINTK(stuff...) printk (stuff)
30 #else
31 #define DPRINTK(stuff...)
32 #endif
33
34 /***                                *
35  * One-way data transfer functions. *
36  *                                ***/
37
38 /* Compatibility mode. */
39 size_t parport_ieee1284_write_compat (struct parport *port,
40                                       const void *buffer, size_t len,
41                                       int flags)
42 {
43         int no_irq = 1;
44         ssize_t count = 0;
45         const unsigned char *addr = buffer;
46         unsigned char byte;
47         struct pardevice *dev = port->physport->cad;
48         unsigned char ctl = (PARPORT_CONTROL_SELECT
49                              | PARPORT_CONTROL_INIT);
50
51         if (port->irq != PARPORT_IRQ_NONE) {
52                 parport_enable_irq (port);
53                 no_irq = 0;
54         }
55
56         port->physport->ieee1284.phase = IEEE1284_PH_FWD_DATA;
57         parport_write_control (port, ctl);
58         parport_data_forward (port);
59         while (count < len) {
60                 long expire = jiffies + dev->timeout;
61                 long wait = (HZ + 99) / 100;
62                 unsigned char mask = (PARPORT_STATUS_ERROR
63                                       | PARPORT_STATUS_BUSY);
64                 unsigned char val = (PARPORT_STATUS_ERROR
65                                      | PARPORT_STATUS_BUSY);
66
67                 /* Wait until the peripheral's ready */
68                 do {
69                         /* Is the peripheral ready yet? */
70                         if (!parport_wait_peripheral (port, mask, val))
71                                 /* Skip the loop */
72                                 goto ready;
73
74                         /* Is the peripheral upset? */
75                         if ((parport_read_status (port) &
76                              (PARPORT_STATUS_PAPEROUT |
77                               PARPORT_STATUS_SELECT |
78                               PARPORT_STATUS_ERROR))
79                             != (PARPORT_STATUS_SELECT |
80                                 PARPORT_STATUS_ERROR))
81                                 /* If nFault is asserted (i.e. no
82                                  * error) and PAPEROUT and SELECT are
83                                  * just red herrings, give the driver
84                                  * a chance to check it's happy with
85                                  * that before continuing. */
86                                 goto stop;
87
88                         /* Have we run out of time? */
89                         if (!time_before (jiffies, expire))
90                                 break;
91
92                         /* Yield the port for a while.  If this is the
93                            first time around the loop, don't let go of
94                            the port.  This way, we find out if we have
95                            our interrupt handler called. */
96                         if (count && no_irq) {
97                                 parport_release (dev);
98                                 __set_current_state (TASK_INTERRUPTIBLE);
99                                 schedule_timeout (wait);
100                                 parport_claim_or_block (dev);
101                         }
102                         else
103                                 /* We must have the device claimed here */
104                                 parport_wait_event (port, wait);
105
106                         /* Is there a signal pending? */
107                         if (signal_pending (current))
108                                 break;
109
110                         /* Wait longer next time. */
111                         wait *= 2;
112                 } while (time_before (jiffies, expire));
113
114                 if (signal_pending (current))
115                         break;
116
117                 DPRINTK (KERN_DEBUG "%s: Timed out\n", port->name);
118                 break;
119
120         ready:
121                 /* Write the character to the data lines. */
122                 byte = *addr++;
123                 parport_write_data (port, byte);
124                 udelay (1);
125
126                 /* Pulse strobe. */
127                 parport_write_control (port, ctl | PARPORT_CONTROL_STROBE);
128                 udelay (1); /* strobe */
129
130                 parport_write_control (port, ctl);
131                 udelay (1); /* hold */
132
133                 /* Assume the peripheral received it. */
134                 count++;
135
136                 /* Let another process run if it needs to. */
137                 if (time_before (jiffies, expire))
138                         if (!parport_yield_blocking (dev)
139                             && current->need_resched)
140                                 schedule ();
141         }
142  stop:
143         port->physport->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
144
145         return count;
146 }
147
148 /* Nibble mode. */
149 size_t parport_ieee1284_read_nibble (struct parport *port, 
150                                      void *buffer, size_t len,
151                                      int flags)
152 {
153 #ifndef CONFIG_PARPORT_1284
154         return 0;
155 #else
156         unsigned char *buf = buffer;
157         int i;
158         unsigned char byte = 0;
159
160         len *= 2; /* in nibbles */
161         for (i=0; i < len; i++) {
162                 unsigned char nibble;
163
164                 /* Does the error line indicate end of data? */
165                 if (((i & 1) == 0) &&
166                     (parport_read_status(port) & PARPORT_STATUS_ERROR)) {
167                         port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
168                         DPRINTK (KERN_DEBUG
169                                 "%s: No more nibble data (%d bytes)\n",
170                                 port->name, i/2);
171
172                         /* Go to reverse idle phase. */
173                         parport_frob_control (port,
174                                               PARPORT_CONTROL_AUTOFD,
175                                               PARPORT_CONTROL_AUTOFD);
176                         port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
177                         break;
178                 }
179
180                 /* Event 7: Set nAutoFd low. */
181                 parport_frob_control (port,
182                                       PARPORT_CONTROL_AUTOFD,
183                                       PARPORT_CONTROL_AUTOFD);
184
185                 /* Event 9: nAck goes low. */
186                 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
187                 if (parport_wait_peripheral (port,
188                                              PARPORT_STATUS_ACK, 0)) {
189                         /* Timeout -- no more data? */
190                         DPRINTK (KERN_DEBUG
191                                  "%s: Nibble timeout at event 9 (%d bytes)\n",
192                                  port->name, i/2);
193                         parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
194                         break;
195                 }
196
197
198                 /* Read a nibble. */
199                 nibble = parport_read_status (port) >> 3;
200                 nibble &= ~8;
201                 if ((nibble & 0x10) == 0)
202                         nibble |= 8;
203                 nibble &= 0xf;
204
205                 /* Event 10: Set nAutoFd high. */
206                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
207
208                 /* Event 11: nAck goes high. */
209                 if (parport_wait_peripheral (port,
210                                              PARPORT_STATUS_ACK,
211                                              PARPORT_STATUS_ACK)) {
212                         /* Timeout -- no more data? */
213                         DPRINTK (KERN_DEBUG
214                                  "%s: Nibble timeout at event 11\n",
215                                  port->name);
216                         break;
217                 }
218
219                 if (i & 1) {
220                         /* Second nibble */
221                         byte |= nibble << 4;
222                         *buf++ = byte;
223                 } else 
224                         byte = nibble;
225         }
226
227         i /= 2; /* i is now in bytes */
228
229         if (i == len) {
230                 /* Read the last nibble without checking data avail. */
231                 port = port->physport;
232                 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
233                         port->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
234                 else
235                         port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
236         }
237
238         return i;
239 #endif /* IEEE1284 support */
240 }
241
242 /* Byte mode. */
243 size_t parport_ieee1284_read_byte (struct parport *port,
244                                    void *buffer, size_t len,
245                                    int flags)
246 {
247 #ifndef CONFIG_PARPORT_1284
248         return 0;
249 #else
250         unsigned char *buf = buffer;
251         ssize_t count = 0;
252
253         for (count = 0; count < len; count++) {
254                 unsigned char byte;
255
256                 /* Data available? */
257                 if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
258                         port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
259                         DPRINTK (KERN_DEBUG
260                                  "%s: No more byte data (%Zd bytes)\n",
261                                  port->name, count);
262
263                         /* Go to reverse idle phase. */
264                         parport_frob_control (port,
265                                               PARPORT_CONTROL_AUTOFD,
266                                               PARPORT_CONTROL_AUTOFD);
267                         port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
268                         break;
269                 }
270
271                 /* Event 14: Place data bus in high impedance state. */
272                 parport_data_reverse (port);
273
274                 /* Event 7: Set nAutoFd low. */
275                 parport_frob_control (port,
276                                       PARPORT_CONTROL_AUTOFD,
277                                       PARPORT_CONTROL_AUTOFD);
278
279                 /* Event 9: nAck goes low. */
280                 port->physport->ieee1284.phase = IEEE1284_PH_REV_DATA;
281                 if (parport_wait_peripheral (port,
282                                              PARPORT_STATUS_ACK,
283                                              0)) {
284                         /* Timeout -- no more data? */
285                         parport_frob_control (port, PARPORT_CONTROL_AUTOFD,
286                                                  0);
287                         DPRINTK (KERN_DEBUG "%s: Byte timeout at event 9\n",
288                                  port->name);
289                         break;
290                 }
291
292                 byte = parport_read_data (port);
293                 *buf++ = byte;
294
295                 /* Event 10: Set nAutoFd high */
296                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
297
298                 /* Event 11: nAck goes high. */
299                 if (parport_wait_peripheral (port,
300                                              PARPORT_STATUS_ACK,
301                                              PARPORT_STATUS_ACK)) {
302                         /* Timeout -- no more data? */
303                         DPRINTK (KERN_DEBUG "%s: Byte timeout at event 11\n",
304                                  port->name);
305                         break;
306                 }
307
308                 /* Event 16: Set nStrobe low. */
309                 parport_frob_control (port,
310                                       PARPORT_CONTROL_STROBE,
311                                       PARPORT_CONTROL_STROBE);
312                 udelay (5);
313
314                 /* Event 17: Set nStrobe high. */
315                 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
316         }
317
318         if (count == len) {
319                 /* Read the last byte without checking data avail. */
320                 port = port->physport;
321                 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
322                         port->ieee1284.phase = IEEE1284_PH_HBUSY_DNA;
323                 else
324                         port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
325         }
326
327         return count;
328 #endif /* IEEE1284 support */
329 }
330
331 /***              *
332  * ECP Functions. *
333  *              ***/
334
335 #ifdef CONFIG_PARPORT_1284
336
337 static inline
338 int ecp_forward_to_reverse (struct parport *port)
339 {
340         int retval;
341
342         /* Event 38: Set nAutoFd low */
343         parport_frob_control (port,
344                               PARPORT_CONTROL_AUTOFD,
345                               PARPORT_CONTROL_AUTOFD);
346         parport_data_reverse (port);
347         udelay (5);
348
349         /* Event 39: Set nInit low to initiate bus reversal */
350         parport_frob_control (port,
351                               PARPORT_CONTROL_INIT,
352                               0);
353
354         /* Event 40: PError goes low */
355         retval = parport_wait_peripheral (port,
356                                           PARPORT_STATUS_PAPEROUT, 0);
357
358         if (!retval) {
359                 DPRINTK (KERN_DEBUG "%s: ECP direction: reverse\n",
360                          port->name);
361                 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
362         } else {
363                 DPRINTK (KERN_DEBUG "%s: ECP direction: failed to reverse\n",
364                          port->name);
365                 port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
366         }
367
368         return retval;
369 }
370
371 static inline
372 int ecp_reverse_to_forward (struct parport *port)
373 {
374         int retval;
375
376         /* Event 47: Set nInit high */
377         parport_frob_control (port,
378                               PARPORT_CONTROL_INIT
379                               | PARPORT_CONTROL_AUTOFD,
380                               PARPORT_CONTROL_INIT
381                               | PARPORT_CONTROL_AUTOFD);
382
383         /* Event 49: PError goes high */
384         retval = parport_wait_peripheral (port,
385                                           PARPORT_STATUS_PAPEROUT,
386                                           PARPORT_STATUS_PAPEROUT);
387
388         if (!retval) {
389                 parport_data_forward (port);
390                 DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
391                          port->name);
392                 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
393         } else {
394                 DPRINTK (KERN_DEBUG
395                          "%s: ECP direction: failed to switch forward\n",
396                          port->name);
397                 port->ieee1284.phase = IEEE1284_PH_ECP_DIR_UNKNOWN;
398         }
399
400
401         return retval;
402 }
403
404 #endif /* IEEE1284 support */
405
406 /* ECP mode, forward channel, data. */
407 size_t parport_ieee1284_ecp_write_data (struct parport *port,
408                                         const void *buffer, size_t len,
409                                         int flags)
410 {
411 #ifndef CONFIG_PARPORT_1284
412         return 0;
413 #else
414         const unsigned char *buf = buffer;
415         size_t written;
416         int retry;
417
418         port = port->physport;
419
420         if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE)
421                 if (ecp_reverse_to_forward (port))
422                         return 0;
423
424         port->ieee1284.phase = IEEE1284_PH_FWD_DATA;
425
426         /* HostAck high (data, not command) */
427         parport_frob_control (port,
428                               PARPORT_CONTROL_AUTOFD
429                               | PARPORT_CONTROL_STROBE
430                               | PARPORT_CONTROL_INIT,
431                               PARPORT_CONTROL_INIT);
432         for (written = 0; written < len; written++, buf++) {
433                 long expire = jiffies + port->cad->timeout;
434                 unsigned char byte;
435
436                 byte = *buf;
437         try_again:
438                 parport_write_data (port, byte);
439                 parport_frob_control (port, PARPORT_CONTROL_STROBE,
440                                       PARPORT_CONTROL_STROBE);
441                 udelay (5);
442                 for (retry = 0; retry < 100; retry++) {
443                         if (!parport_wait_peripheral (port,
444                                                       PARPORT_STATUS_BUSY, 0))
445                                 goto success;
446
447                         if (signal_pending (current)) {
448                                 parport_frob_control (port,
449                                                       PARPORT_CONTROL_STROBE,
450                                                       0);
451                                 break;
452                         }
453                 }
454
455                 /* Time for Host Transfer Recovery (page 41 of IEEE1284) */
456                 DPRINTK (KERN_DEBUG "%s: ECP transfer stalled!\n", port->name);
457
458                 parport_frob_control (port, PARPORT_CONTROL_INIT,
459                                       PARPORT_CONTROL_INIT);
460                 udelay (50);
461                 if (parport_read_status (port) & PARPORT_STATUS_PAPEROUT) {
462                         /* It's buggered. */
463                         parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
464                         break;
465                 }
466
467                 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
468                 udelay (50);
469                 if (!(parport_read_status (port) & PARPORT_STATUS_PAPEROUT))
470                         break;
471
472                 DPRINTK (KERN_DEBUG "%s: Host transfer recovered\n",
473                          port->name);
474
475                 if (time_after_eq (jiffies, expire)) break;
476                 goto try_again;
477         success:
478                 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
479                 udelay (5);
480                 if (parport_wait_peripheral (port,
481                                              PARPORT_STATUS_BUSY,
482                                              PARPORT_STATUS_BUSY))
483                         /* Peripheral hasn't accepted the data. */
484                         break;
485         }
486
487         port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
488
489         return written;
490 #endif /* IEEE1284 support */
491 }
492
493 /* ECP mode, reverse channel, data. */
494 size_t parport_ieee1284_ecp_read_data (struct parport *port,
495                                        void *buffer, size_t len, int flags)
496 {
497 #ifndef CONFIG_PARPORT_1284
498         return 0;
499 #else
500         struct pardevice *dev = port->cad;
501         unsigned char *buf = buffer;
502         int rle_count = 0; /* shut gcc up */
503         unsigned char ctl;
504         int rle = 0;
505         ssize_t count = 0;
506
507         port = port->physport;
508
509         if (port->ieee1284.phase != IEEE1284_PH_REV_IDLE)
510                 if (ecp_forward_to_reverse (port))
511                         return 0;
512
513         port->ieee1284.phase = IEEE1284_PH_REV_DATA;
514
515         /* Set HostAck low to start accepting data. */
516         ctl = parport_read_control (port);
517         ctl &= ~(PARPORT_CONTROL_STROBE | PARPORT_CONTROL_INIT |
518                  PARPORT_CONTROL_AUTOFD);
519         parport_write_control (port,
520                                ctl | PARPORT_CONTROL_AUTOFD);
521         while (count < len) {
522                 long expire = jiffies + dev->timeout;
523                 unsigned char byte;
524                 int command;
525
526                 /* Event 43: Peripheral sets nAck low. It can take as
527                    long as it wants. */
528                 while (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
529                         /* The peripheral hasn't given us data in
530                            35ms.  If we have data to give back to the
531                            caller, do it now. */
532                         if (count)
533                                 goto out;
534
535                         /* If we've used up all the time we were allowed,
536                            give up altogether. */
537                         if (!time_before (jiffies, expire))
538                                 goto out;
539
540                         /* Yield the port for a while. */
541                         if (count && dev->port->irq != PARPORT_IRQ_NONE) {
542                                 parport_release (dev);
543                                 __set_current_state (TASK_INTERRUPTIBLE);
544                                 schedule_timeout ((HZ + 24) / 25);
545                                 parport_claim_or_block (dev);
546                         }
547                         else
548                                 /* We must have the device claimed here. */
549                                 parport_wait_event (port, (HZ + 24) / 25);
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                 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