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