Linux Audio

Check our new training course

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