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