Linux Audio

Check our new training course

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