File: /usr/src/linux/drivers/parport/ieee1284.c

1     /* $Id: parport_ieee1284.c,v 1.4 1997/10/19 21:37:21 philip Exp $
2      * IEEE-1284 implementation for parport.
3      *
4      * Authors: Phil Blundell <Philip.Blundell@pobox.com>
5      *          Carsten Gross <carsten@sol.wohnheim.uni-ulm.de>
6      *	    Jose Renau <renau@acm.org>
7      *          Tim Waugh <tim@cyberelk.demon.co.uk> (largely rewritten)
8      *
9      * This file is responsible for IEEE 1284 negotiation, and for handing
10      * read/write requests to low-level drivers.
11      *
12      * Any part of this program may be used in documents licensed under
13      * the GNU Free Documentation License, Version 1.1 or any later version
14      * published by the Free Software Foundation.
15      *
16      * Various hacks, Fred Barnes <frmb2@ukc.ac.uk>, 04/2000
17      */
18     
19     #include <linux/config.h>
20     #include <linux/threads.h>
21     #include <linux/parport.h>
22     #include <linux/delay.h>
23     #include <linux/kernel.h>
24     #include <linux/interrupt.h>
25     
26     #undef DEBUG /* undef me for production */
27     
28     #ifdef CONFIG_LP_CONSOLE
29     #undef DEBUG /* Don't want a garbled console */
30     #endif
31     
32     #ifdef DEBUG
33     #define DPRINTK(stuff...) printk (stuff)
34     #else
35     #define DPRINTK(stuff...)
36     #endif
37     
38     /* Make parport_wait_peripheral wake up.
39      * It will be useful to call this from an interrupt handler. */
40     void parport_ieee1284_wakeup (struct parport *port)
41     {
42     	up (&port->physport->ieee1284.irq);
43     }
44     
45     static struct parport *port_from_cookie[PARPORT_MAX];
46     static void timeout_waiting_on_port (unsigned long cookie)
47     {
48     	parport_ieee1284_wakeup (port_from_cookie[cookie % PARPORT_MAX]);
49     }
50     
51     /**
52      *	parport_wait_event - wait for an event on a parallel port
53      *	@port: port to wait on
54      *	@timeout: time to wait (in jiffies)
55      *
56      *	This function waits for up to @timeout jiffies for an
57      *	interrupt to occur on a parallel port.  If the port timeout is
58      *	set to zero, it returns immediately.
59      *
60      *	If an interrupt occurs before the timeout period elapses, this
61      *	function returns one immediately.  If it times out, it returns
62      *	a value greater than zero.  An error code less than zero
63      *	indicates an error (most likely a pending signal), and the
64      *	calling code should finish what it's doing as soon as it can.
65      */
66     
67     int parport_wait_event (struct parport *port, signed long timeout)
68     {
69     	int ret;
70     	struct timer_list timer;
71     
72     	if (!port->physport->cad->timeout)
73     		/* Zero timeout is special, and we can't down() the
74     		   semaphore. */
75     		return 1;
76     
77     	init_timer (&timer);
78     	timer.expires = jiffies + timeout;
79     	timer.function = timeout_waiting_on_port;
80     	port_from_cookie[port->number % PARPORT_MAX] = port;
81     	timer.data = port->number;
82     
83     	add_timer (&timer);
84     	ret = down_interruptible (&port->physport->ieee1284.irq);
85     	if (!del_timer (&timer) && !ret)
86     		/* Timed out. */
87     		ret = 1;
88     
89     	return ret;
90     }
91     
92     /**
93      *	parport_poll_peripheral - poll status lines
94      *	@port: port to watch
95      *	@mask: status lines to watch
96      *	@result: desired values of chosen status lines
97      *	@usec: timeout
98      *
99      *	This function busy-waits until the masked status lines have
100      *	the desired values, or until the timeout period elapses.  The
101      *	@mask and @result parameters are bitmasks, with the bits
102      *	defined by the constants in parport.h: %PARPORT_STATUS_BUSY,
103      *	and so on.
104      *
105      *	This function does not call schedule(); instead it busy-waits
106      *	using udelay().  It currently has a resolution of 5usec.
107      *
108      *	If the status lines take on the desired values before the
109      *	timeout period elapses, parport_poll_peripheral() returns zero
110      *	immediately.  A zero return value greater than zero indicates
111      *	a timeout.  An error code (less than zero) indicates an error,
112      *	most likely a signal that arrived, and the caller should
113      *	finish what it is doing as soon as possible.
114     */
115     
116     int parport_poll_peripheral(struct parport *port,
117     			    unsigned char mask,
118     			    unsigned char result,
119     			    int usec)
120     {
121     	/* Zero return code is success, >0 is timeout. */
122     	int counter = usec / 5;
123     	unsigned char status;
124     	for (; counter > 0; counter--) {
125     		status = parport_read_status (port);
126     		if ((status & mask) == result)
127     			return 0;
128     		if (signal_pending (current))
129     			return -EINTR;
130     		if (current->need_resched)
131     			break;
132     		udelay (5);
133     	}
134     
135     	return 1;
136     }
137     
138     /**
139      *	parport_wait_peripheral - wait for status lines to change in 35ms
140      *	@port: port to watch
141      *	@mask: status lines to watch
142      *	@result: desired values of chosen status lines
143      *
144      *	This function waits until the masked status lines have the
145      *	desired values, or until 35ms have elapsed (see IEEE 1284-1994
146      *	page 24 to 25 for why this value in particular is hardcoded).
147      *	The @mask and @result parameters are bitmasks, with the bits
148      *	defined by the constants in parport.h: %PARPORT_STATUS_BUSY,
149      *	and so on.
150      *
151      *	The port is polled quickly to start off with, in anticipation
152      *	of a fast response from the peripheral.  This fast polling
153      *	time is configurable (using /proc), and defaults to 500usec.
154      *	If the timeout for this port (see parport_set_timeout()) is
155      *	zero, the fast polling time is 35ms, and this function does
156      *	not call schedule().
157      *
158      *	If the timeout for this port is non-zero, after the fast
159      *	polling fails it uses parport_wait_event() to wait for up to
160      *	10ms, waking up if an interrupt occurs.
161      */
162     
163     int parport_wait_peripheral(struct parport *port,
164     			    unsigned char mask, 
165     			    unsigned char result)
166     {
167     	int ret;
168     	int usec;
169     	long deadline;
170     	unsigned char status;
171     
172     	usec = port->physport->spintime; /* usecs of fast polling */
173     	if (!port->physport->cad->timeout)
174     		/* A zero timeout is "special": busy wait for the
175     		   entire 35ms. */
176     		usec = 35000;
177     
178     	/* Fast polling.
179     	 *
180     	 * This should be adjustable.
181     	 * How about making a note (in the device structure) of how long
182     	 * it takes, so we know for next time?
183     	 */
184     	ret = parport_poll_peripheral (port, mask, result, usec);
185     	if (ret != 1)
186     		return ret;
187     
188     	if (!port->physport->cad->timeout)
189     		/* We may be in an interrupt handler, so we can't poll
190     		 * slowly anyway. */
191     		return 1;
192     
193     	/* 40ms of slow polling. */
194     	deadline = jiffies + (HZ + 24) / 25;
195     	while (time_before (jiffies, deadline)) {
196     		int ret;
197     
198     		if (signal_pending (current))
199     			return -EINTR;
200     
201     		/* Wait for 10ms (or until an interrupt occurs if
202     		 * the handler is set) */
203     		if ((ret = parport_wait_event (port, (HZ + 99) / 100)) < 0)
204     			return ret;
205     
206     		status = parport_read_status (port);
207     		if ((status & mask) == result)
208     			return 0;
209     
210     		if (!ret) {
211     			/* parport_wait_event didn't time out, but the
212     			 * peripheral wasn't actually ready either.
213     			 * Wait for another 10ms. */
214     			__set_current_state (TASK_INTERRUPTIBLE);
215     			schedule_timeout ((HZ+ 99) / 100);
216     		}
217     	}
218     
219     	return 1;
220     }
221     
222     #ifdef CONFIG_PARPORT_1284
223     /* Terminate a negotiated mode. */
224     static void parport_ieee1284_terminate (struct parport *port)
225     {
226     	int r;
227     	port = port->physport;
228     
229     	/* EPP terminates differently. */
230     	switch (port->ieee1284.mode) {
231     	case IEEE1284_MODE_EPP:
232     	case IEEE1284_MODE_EPPSL:
233     	case IEEE1284_MODE_EPPSWE:
234     		/* Terminate from EPP mode. */
235     
236     		/* Event 68: Set nInit low */
237     		parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
238     		udelay (50);
239     
240     		/* Event 69: Set nInit high, nSelectIn low */
241     		parport_frob_control (port,
242     				      PARPORT_CONTROL_SELECT
243     				      | PARPORT_CONTROL_INIT,
244     				      PARPORT_CONTROL_SELECT
245     				      | PARPORT_CONTROL_INIT);
246     		break;
247     
248     	case IEEE1284_MODE_ECP:
249     	case IEEE1284_MODE_ECPRLE:
250     	case IEEE1284_MODE_ECPSWE:
251     		/* In ECP we can only terminate from fwd idle phase. */
252     		if (port->ieee1284.phase != IEEE1284_PH_FWD_IDLE) {
253     			/* Event 47: Set nInit high */
254     			parport_frob_control (port,
255     					      PARPORT_CONTROL_INIT
256     					      | PARPORT_CONTROL_AUTOFD,
257     					      PARPORT_CONTROL_INIT
258     					      | PARPORT_CONTROL_AUTOFD);
259     
260     			/* Event 49: PError goes high */
261     			r = parport_wait_peripheral (port,
262     						     PARPORT_STATUS_PAPEROUT,
263     						     PARPORT_STATUS_PAPEROUT);
264     			if (r)
265     				DPRINTK (KERN_INFO "%s: Timeout at event 49\n",
266     					 port->name);
267     
268     			parport_data_forward (port);
269     			DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
270     				 port->name);
271     			port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
272     		}
273     
274     		/* fall-though.. */
275     
276     	default:
277     		/* Terminate from all other modes. */
278     
279     		/* Event 22: Set nSelectIn low, nAutoFd high */
280     		parport_frob_control (port,
281     				      PARPORT_CONTROL_SELECT
282     				      | PARPORT_CONTROL_AUTOFD,
283     				      PARPORT_CONTROL_SELECT);
284     
285     		/* Event 24: nAck goes low */
286     		r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
287     		if (r)
288     			DPRINTK (KERN_INFO "%s: Timeout at event 24\n",
289     				 port->name);
290     
291     		/* Event 25: Set nAutoFd low */
292     		parport_frob_control (port,
293     				      PARPORT_CONTROL_AUTOFD,
294     				      PARPORT_CONTROL_AUTOFD);
295     
296     		/* Event 27: nAck goes high */
297     		r = parport_wait_peripheral (port,
298     					     PARPORT_STATUS_ACK, 
299     					     PARPORT_STATUS_ACK);
300     		if (r)
301     			DPRINTK (KERN_INFO "%s: Timeout at event 27\n",
302     				 port->name);
303     
304     		/* Event 29: Set nAutoFd high */
305     		parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
306     	}
307     
308     	port->ieee1284.mode = IEEE1284_MODE_COMPAT;
309     	port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
310     
311     	DPRINTK (KERN_DEBUG "%s: In compatibility (forward idle) mode\n",
312     		 port->name);
313     }		
314     #endif /* IEEE1284 support */
315     
316     /**
317      *	parport_negotiate - negotiate an IEEE 1284 mode
318      *	@port: port to use
319      *	@mode: mode to negotiate to
320      *
321      *	Use this to negotiate to a particular IEEE 1284 transfer mode.
322      *	The @mode parameter should be one of the constants in
323      *	parport.h starting %IEEE1284_MODE_xxx.
324      *
325      *	The return value is 0 if the peripheral has accepted the
326      *	negotiation to the mode specified, -1 if the peripheral is not
327      *	IEEE 1284 compliant (or not present), or 1 if the peripheral
328      *	has rejected the negotiation.
329      */
330     
331     int parport_negotiate (struct parport *port, int mode)
332     {
333     #ifndef CONFIG_PARPORT_1284
334     	if (mode == IEEE1284_MODE_COMPAT)
335     		return 0;
336     	printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
337     	return -1;
338     #else
339     	int m = mode & ~IEEE1284_ADDR;
340     	int r;
341     	unsigned char xflag;
342     
343     	port = port->physport;
344     
345     	/* Is there anything to do? */
346     	if (port->ieee1284.mode == mode)
347     		return 0;
348     
349     	/* Is the difference just an address-or-not bit? */
350     	if ((port->ieee1284.mode & ~IEEE1284_ADDR) == (mode & ~IEEE1284_ADDR)){
351     		port->ieee1284.mode = mode;
352     		return 0;
353     	}
354     
355     	/* Go to compability forward idle mode */
356     	if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
357     		parport_ieee1284_terminate (port);
358     
359     	if (mode == IEEE1284_MODE_COMPAT)
360     		/* Compatibility mode: no negotiation. */
361     		return 0; 
362     
363     	switch (mode) {
364     	case IEEE1284_MODE_ECPSWE:
365     		m = IEEE1284_MODE_ECP;
366     		break;
367     	case IEEE1284_MODE_EPPSL:
368     	case IEEE1284_MODE_EPPSWE:
369     		m = IEEE1284_MODE_EPP;
370     		break;
371     	case IEEE1284_MODE_BECP:
372     		return -ENOSYS; /* FIXME (implement BECP) */
373     	}
374     
375     	if (mode & IEEE1284_EXT_LINK)
376     		m = 1<<7; /* request extensibility link */
377     
378     	port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
379     
380     	/* Start off with nStrobe and nAutoFd high, and nSelectIn low */
381     	parport_frob_control (port,
382     			      PARPORT_CONTROL_STROBE
383     			      | PARPORT_CONTROL_AUTOFD
384     			      | PARPORT_CONTROL_SELECT,
385     			      PARPORT_CONTROL_SELECT);
386     	udelay(1);
387     
388     	/* Event 0: Set data */
389     	parport_data_forward (port);
390     	parport_write_data (port, m);
391     	udelay (400); /* Shouldn't need to wait this long. */
392     
393     	/* Event 1: Set nSelectIn high, nAutoFd low */
394     	parport_frob_control (port,
395     			      PARPORT_CONTROL_SELECT
396     			      | PARPORT_CONTROL_AUTOFD,
397     			      PARPORT_CONTROL_AUTOFD);
398     
399     	/* Event 2: PError, Select, nFault go high, nAck goes low */
400     	if (parport_wait_peripheral (port,
401     				     PARPORT_STATUS_ERROR
402     				     | PARPORT_STATUS_SELECT
403     				     | PARPORT_STATUS_PAPEROUT
404     				     | PARPORT_STATUS_ACK,
405     				     PARPORT_STATUS_ERROR
406     				     | PARPORT_STATUS_SELECT
407     				     | PARPORT_STATUS_PAPEROUT)) {
408     		/* Timeout */
409     		parport_frob_control (port,
410     				      PARPORT_CONTROL_SELECT
411     				      | PARPORT_CONTROL_AUTOFD,
412     				      PARPORT_CONTROL_SELECT);
413     		DPRINTK (KERN_DEBUG
414     			 "%s: Peripheral not IEEE1284 compliant (0x%02X)\n",
415     			 port->name, parport_read_status (port));
416     		port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
417     		return -1; /* Not IEEE1284 compliant */
418     	}
419     
420     	/* Event 3: Set nStrobe low */
421     	parport_frob_control (port,
422     			      PARPORT_CONTROL_STROBE,
423     			      PARPORT_CONTROL_STROBE);
424     
425     	/* Event 4: Set nStrobe and nAutoFd high */
426     	udelay (5);
427     	parport_frob_control (port,
428     			      PARPORT_CONTROL_STROBE
429     			      | PARPORT_CONTROL_AUTOFD,
430     			      0);
431     
432     	/* Event 6: nAck goes high */
433     	if (parport_wait_peripheral (port,
434     				     PARPORT_STATUS_ACK,
435     				     PARPORT_STATUS_ACK)) {
436     		/* This shouldn't really happen with a compliant device. */
437     		DPRINTK (KERN_DEBUG
438     			 "%s: Mode 0x%02x not supported? (0x%02x)\n",
439     			 port->name, mode, port->ops->read_status (port));
440     		parport_ieee1284_terminate (port);
441     		return 1;
442     	}
443     
444     	xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
445     
446     	/* xflag should be high for all modes other than nibble (0). */
447     	if (mode && !xflag) {
448     		/* Mode not supported. */
449     		DPRINTK (KERN_DEBUG "%s: Mode 0x%02x rejected by peripheral\n",
450     			 port->name, mode);
451     		parport_ieee1284_terminate (port);
452     		return 1;
453     	}
454     
455     	/* More to do if we've requested extensibility link. */
456     	if (mode & IEEE1284_EXT_LINK) {
457     		m = mode & 0x7f;
458     		udelay (1);
459     		parport_write_data (port, m);
460     		udelay (1);
461     
462     		/* Event 51: Set nStrobe low */
463     		parport_frob_control (port,
464     				      PARPORT_CONTROL_STROBE,
465     				      PARPORT_CONTROL_STROBE);
466     
467     		/* Event 52: nAck goes low */
468     		if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
469     			/* This peripheral is _very_ slow. */
470     			DPRINTK (KERN_DEBUG
471     				 "%s: Event 52 didn't happen\n",
472     				 port->name);
473     			parport_ieee1284_terminate (port);
474     			return 1;
475     		}
476     
477     		/* Event 53: Set nStrobe high */
478     		parport_frob_control (port,
479     				      PARPORT_CONTROL_STROBE,
480     				      0);
481     
482     		/* Event 55: nAck goes high */
483     		if (parport_wait_peripheral (port,
484     					     PARPORT_STATUS_ACK,
485     					     PARPORT_STATUS_ACK)) {
486     			/* This shouldn't really happen with a compliant
487     			 * device. */
488     			DPRINTK (KERN_DEBUG
489     				 "%s: Mode 0x%02x not supported? (0x%02x)\n",
490     				 port->name, mode,
491     				 port->ops->read_status (port));
492     			parport_ieee1284_terminate (port);
493     			return 1;
494     		}
495     
496     		/* Event 54: Peripheral sets XFlag to reflect support */
497     		xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
498     
499     		/* xflag should be high. */
500     		if (!xflag) {
501     			/* Extended mode not supported. */
502     			DPRINTK (KERN_DEBUG "%s: Extended mode 0x%02x not "
503     				 "supported\n", port->name, mode);
504     			parport_ieee1284_terminate (port);
505     			return 1;
506     		}
507     
508     		/* Any further setup is left to the caller. */
509     	}
510     
511     	/* Mode is supported */
512     	DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
513     	port->ieee1284.mode = mode;
514     
515     	/* But ECP is special */
516     	if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
517     		port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
518     
519     		/* Event 30: Set nAutoFd low */
520     		parport_frob_control (port,
521     				      PARPORT_CONTROL_AUTOFD,
522     				      PARPORT_CONTROL_AUTOFD);
523     
524     		/* Event 31: PError goes high. */
525     		r = parport_wait_peripheral (port,
526     					     PARPORT_STATUS_PAPEROUT,
527     					     PARPORT_STATUS_PAPEROUT);
528     		if (r) {
529     			DPRINTK (KERN_INFO "%s: Timeout at event 31\n",
530     				port->name);
531     		}
532     
533     		port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
534     		DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
535     			 port->name);
536     	} else switch (mode) {
537     	case IEEE1284_MODE_NIBBLE:
538     	case IEEE1284_MODE_BYTE:
539     		port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
540     		break;
541     	default:
542     		port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
543     	}
544     
545     
546     	return 0;
547     #endif /* IEEE1284 support */
548     }
549     
550     /* Acknowledge that the peripheral has data available.
551      * Events 18-20, in order to get from Reverse Idle phase
552      * to Host Busy Data Available.
553      * This will most likely be called from an interrupt.
554      * Returns zero if data was available.
555      */
556     #ifdef CONFIG_PARPORT_1284
557     static int parport_ieee1284_ack_data_avail (struct parport *port)
558     {
559     	if (parport_read_status (port) & PARPORT_STATUS_ERROR)
560     		/* Event 18 didn't happen. */
561     		return -1;
562     
563     	/* Event 20: nAutoFd goes high. */
564     	port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
565     	port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
566     	return 0;
567     }
568     #endif /* IEEE1284 support */
569     
570     /* Handle an interrupt. */
571     void parport_ieee1284_interrupt (int which, void *handle, struct pt_regs *regs)
572     {
573     	struct parport *port = handle;
574     	parport_ieee1284_wakeup (port);
575     
576     #ifdef CONFIG_PARPORT_1284
577     	if (port->ieee1284.phase == IEEE1284_PH_REV_IDLE) {
578     		/* An interrupt in this phase means that data
579     		 * is now available. */
580     		DPRINTK (KERN_DEBUG "%s: Data available\n", port->name);
581     		parport_ieee1284_ack_data_avail (port);
582     	}
583     #endif /* IEEE1284 support */
584     }
585     
586     /**
587      *	parport_write - write a block of data to a parallel port
588      *	@port: port to write to
589      *	@buffer: data buffer (in kernel space)
590      *	@len: number of bytes of data to transfer
591      *
592      *	This will write up to @len bytes of @buffer to the port
593      *	specified, using the IEEE 1284 transfer mode most recently
594      *	negotiated to (using parport_negotiate()), as long as that
595      *	mode supports forward transfers (host to peripheral).
596      *
597      *	It is the caller's responsibility to ensure that the first
598      *	@len bytes of @buffer are valid.
599      *
600      *	This function returns the number of bytes transferred (if zero
601      *	or positive), or else an error code.
602      */
603     
604     ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
605     {
606     #ifndef CONFIG_PARPORT_1284
607     	return port->ops->compat_write_data (port, buffer, len, 0);
608     #else
609     	ssize_t retval;
610     	int mode = port->ieee1284.mode;
611     	int addr = mode & IEEE1284_ADDR;
612     	size_t (*fn) (struct parport *, const void *, size_t, int);
613     
614     	/* Ignore the device-ID-request bit and the address bit. */
615     	mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
616     
617     	/* Use the mode we're in. */
618     	switch (mode) {
619     	case IEEE1284_MODE_NIBBLE:
620     	case IEEE1284_MODE_BYTE:
621     		parport_negotiate (port, IEEE1284_MODE_COMPAT);
622     	case IEEE1284_MODE_COMPAT:
623     		DPRINTK (KERN_DEBUG "%s: Using compatibility mode\n",
624     			 port->name);
625     		fn = port->ops->compat_write_data;
626     		break;
627     
628     	case IEEE1284_MODE_EPP:
629     		DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
630     		if (addr) {
631     			fn = port->ops->epp_write_addr;
632     		} else {
633     			fn = port->ops->epp_write_data;
634     		}
635     		break;
636     	case IEEE1284_MODE_EPPSWE:
637     		DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
638     			port->name);
639     		if (addr) {
640     			fn = parport_ieee1284_epp_write_addr;
641     		} else {
642     			fn = parport_ieee1284_epp_write_data;
643     		}
644     		break;
645     	case IEEE1284_MODE_ECP:
646     	case IEEE1284_MODE_ECPRLE:
647     		DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
648     		if (addr) {
649     			fn = port->ops->ecp_write_addr;
650     		} else {
651     			fn = port->ops->ecp_write_data;
652     		}
653     		break;
654     
655     	case IEEE1284_MODE_ECPSWE:
656     		DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
657     			 port->name);
658     		/* The caller has specified that it must be emulated,
659     		 * even if we have ECP hardware! */
660     		if (addr) {
661     			fn = parport_ieee1284_ecp_write_addr;
662     		} else {
663     			fn = parport_ieee1284_ecp_write_data;
664     		}
665     		break;
666     
667     	default:
668     		DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
669     			port->ieee1284.mode);
670     		return -ENOSYS;
671     	}
672     
673     	retval = (*fn) (port, buffer, len, 0);
674     	DPRINTK (KERN_DEBUG "%s: wrote %d/%d bytes\n", port->name, retval, len);
675     	return retval;
676     #endif /* IEEE1284 support */
677     }
678     
679     /**
680      *	parport_read - read a block of data from a parallel port
681      *	@port: port to read from
682      *	@buffer: data buffer (in kernel space)
683      *	@len: number of bytes of data to transfer
684      *
685      *	This will read up to @len bytes of @buffer to the port
686      *	specified, using the IEEE 1284 transfer mode most recently
687      *	negotiated to (using parport_negotiate()), as long as that
688      *	mode supports reverse transfers (peripheral to host).
689      *
690      *	It is the caller's responsibility to ensure that the first
691      *	@len bytes of @buffer are available to write to.
692      *
693      *	This function returns the number of bytes transferred (if zero
694      *	or positive), or else an error code.
695      */
696     
697     ssize_t parport_read (struct parport *port, void *buffer, size_t len)
698     {
699     #ifndef CONFIG_PARPORT_1284
700     	printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
701     	return -ENODEV;
702     #else
703     	int mode = port->physport->ieee1284.mode;
704     	int addr = mode & IEEE1284_ADDR;
705     	size_t (*fn) (struct parport *, void *, size_t, int);
706     
707     	/* Ignore the device-ID-request bit and the address bit. */
708     	mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
709     
710     	/* Use the mode we're in. */
711     	switch (mode) {
712     	case IEEE1284_MODE_COMPAT:
713     		/* if we can tri-state use BYTE mode instead of NIBBLE mode,
714     		 * if that fails, revert to NIBBLE mode -- ought to store somewhere
715     		 * the device's ability to do BYTE mode reverse transfers, so we don't
716     		 * end up needlessly calling negotiate(BYTE) repeately..  (fb)
717     		 */
718     		if ((port->physport->modes & PARPORT_MODE_TRISTATE) &&
719     		    !parport_negotiate (port, IEEE1284_MODE_BYTE)) {
720     			/* got into BYTE mode OK */
721     			DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
722     			fn = port->ops->byte_read_data;
723     			break;
724     		}
725     		if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
726     			return -EIO;
727     		}
728     		/* fall through to NIBBLE */
729     	case IEEE1284_MODE_NIBBLE:
730     		DPRINTK (KERN_DEBUG "%s: Using nibble mode\n", port->name);
731     		fn = port->ops->nibble_read_data;
732     		break;
733     
734     	case IEEE1284_MODE_BYTE:
735     		DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
736     		fn = port->ops->byte_read_data;
737     		break;
738     
739     	case IEEE1284_MODE_EPP:
740     		DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
741     		if (addr) {
742     			fn = port->ops->epp_read_addr;
743     		} else {
744     			fn = port->ops->epp_read_data;
745     		}
746     		break;
747     	case IEEE1284_MODE_EPPSWE:
748     		DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
749     			port->name);
750     		if (addr) {
751     			fn = parport_ieee1284_epp_read_addr;
752     		} else {
753     			fn = parport_ieee1284_epp_read_data;
754     		}
755     		break;
756     	case IEEE1284_MODE_ECP:
757     	case IEEE1284_MODE_ECPRLE:
758     		DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
759     		fn = port->ops->ecp_read_data;
760     		break;
761     
762     	case IEEE1284_MODE_ECPSWE:
763     		DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
764     			 port->name);
765     		fn = parport_ieee1284_ecp_read_data;
766     		break;
767     
768     	default:
769     		DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
770     			 port->physport->ieee1284.mode);
771     		return -ENOSYS;
772     	}
773     
774     	return (*fn) (port, buffer, len, 0);
775     #endif /* IEEE1284 support */
776     }
777     
778     /**
779      *	parport_set_timeout - set the inactivity timeout for a device
780      *	@dev: device on a port
781      *	@inactivity: inactivity timeout (in jiffies)
782      *
783      *	This sets the inactivity timeout for a particular device on a
784      *	port.  This affects functions like parport_wait_peripheral().
785      *	The special value 0 means not to call schedule() while dealing
786      *	with this device.
787      *
788      *	The return value is the previous inactivity timeout.
789      *
790      *	Any callers of parport_wait_event() for this device are woken
791      *	up.
792      */
793     
794     long parport_set_timeout (struct pardevice *dev, long inactivity)
795     {
796     	long int old = dev->timeout;
797     
798     	dev->timeout = inactivity;
799     
800     	if (dev->port->physport->cad == dev)
801     		parport_ieee1284_wakeup (dev->port);
802     
803     	return old;
804     }
805