2 * IEEE-1284 implementation for parport.
9 * This file is responsible for IEEE 1284 negotiation, and for handing
10 * read/write requests to low-level drivers.
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.
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>
28 #undef DEBUG /* undef me for production */
30 #ifdef CONFIG_LP_CONSOLE
31 #undef DEBUG /* Don't want a garbled console */
35 #define DPRINTK(stuff...) printk (stuff)
37 #define DPRINTK(stuff...)
40 /* Make parport_wait_peripheral wake up.
41 * It will be useful to call this from an interrupt handler. */
42 static void parport_ieee1284_wakeup (struct parport *port)
44 up (&port->physport->ieee1284.irq);
47 static void timeout_waiting_on_port (struct timer_list *t)
49 struct parport *port = from_timer(port, t, timer);
51 parport_ieee1284_wakeup (port);
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)
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.
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.
70 int parport_wait_event (struct parport *port, signed long timeout)
74 if (!port->physport->cad->timeout)
75 /* Zero timeout is special, and we can't down() the
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)
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
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,
102 * This function does not call schedule(); instead it busy-waits
103 * using udelay(). It currently has a resolution of 5usec.
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.
113 int parport_poll_peripheral(struct parport *port,
115 unsigned char result,
118 /* Zero return code is success, >0 is timeout. */
119 int count = usec / 5 + 2;
121 unsigned char status;
122 for (i = 0; i < count; i++) {
123 status = parport_read_status (port);
124 if ((status & mask) == result)
126 if (signal_pending (current))
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
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,
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().
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.
162 int parport_wait_peripheral(struct parport *port,
164 unsigned char result)
168 unsigned long deadline;
169 unsigned char status;
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
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?
183 ret = parport_poll_peripheral (port, mask, result, usec);
187 if (!port->physport->cad->timeout)
188 /* We may be in an interrupt handler, so we can't poll
192 /* 40ms of slow polling. */
193 deadline = jiffies + msecs_to_jiffies(40);
194 while (time_before (jiffies, deadline)) {
195 if (signal_pending (current))
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)
203 status = parport_read_status (port);
204 if ((status & mask) == result)
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));
218 #ifdef CONFIG_PARPORT_1284
219 /* Terminate a negotiated mode. */
220 static void parport_ieee1284_terminate (struct parport *port)
223 port = port->physport;
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. */
232 /* Event 68: Set nInit low */
233 parport_frob_control (port, PARPORT_CONTROL_INIT, 0);
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);
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,
252 | PARPORT_CONTROL_AUTOFD,
254 | PARPORT_CONTROL_AUTOFD);
256 /* Event 49: PError goes high */
257 r = parport_wait_peripheral (port,
258 PARPORT_STATUS_PAPEROUT,
259 PARPORT_STATUS_PAPEROUT);
261 DPRINTK (KERN_INFO "%s: Timeout at event 49\n",
264 parport_data_forward (port);
265 DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
267 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
273 /* Terminate from all other modes. */
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);
281 /* Event 24: nAck goes low */
282 r = parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0);
284 DPRINTK (KERN_INFO "%s: Timeout at event 24\n",
287 /* Event 25: Set nAutoFd low */
288 parport_frob_control (port,
289 PARPORT_CONTROL_AUTOFD,
290 PARPORT_CONTROL_AUTOFD);
292 /* Event 27: nAck goes high */
293 r = parport_wait_peripheral (port,
297 DPRINTK (KERN_INFO "%s: Timeout at event 27\n",
300 /* Event 29: Set nAutoFd high */
301 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
304 port->ieee1284.mode = IEEE1284_MODE_COMPAT;
305 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
307 DPRINTK (KERN_DEBUG "%s: In compatibility (forward idle) mode\n",
310 #endif /* IEEE1284 support */
313 * parport_negotiate - negotiate an IEEE 1284 mode
315 * @mode: mode to negotiate to
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.
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.
327 int parport_negotiate (struct parport *port, int mode)
329 #ifndef CONFIG_PARPORT_1284
330 if (mode == IEEE1284_MODE_COMPAT)
332 printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
335 int m = mode & ~IEEE1284_ADDR;
339 port = port->physport;
341 /* Is there anything to do? */
342 if (port->ieee1284.mode == mode)
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;
351 /* Go to compatibility forward idle mode */
352 if (port->ieee1284.mode != IEEE1284_MODE_COMPAT)
353 parport_ieee1284_terminate (port);
355 if (mode == IEEE1284_MODE_COMPAT)
356 /* Compatibility mode: no negotiation. */
360 case IEEE1284_MODE_ECPSWE:
361 m = IEEE1284_MODE_ECP;
363 case IEEE1284_MODE_EPPSL:
364 case IEEE1284_MODE_EPPSWE:
365 m = IEEE1284_MODE_EPP;
367 case IEEE1284_MODE_BECP:
368 return -ENOSYS; /* FIXME (implement BECP) */
371 if (mode & IEEE1284_EXT_LINK)
372 m = 1<<7; /* request extensibility link */
374 port->ieee1284.phase = IEEE1284_PH_NEGOTIATION;
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);
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. */
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);
395 /* Event 2: PError, Select, nFault go high, nAck goes low */
396 if (parport_wait_peripheral (port,
398 | PARPORT_STATUS_SELECT
399 | PARPORT_STATUS_PAPEROUT
400 | PARPORT_STATUS_ACK,
402 | PARPORT_STATUS_SELECT
403 | PARPORT_STATUS_PAPEROUT)) {
405 parport_frob_control (port,
406 PARPORT_CONTROL_SELECT
407 | PARPORT_CONTROL_AUTOFD,
408 PARPORT_CONTROL_SELECT);
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 */
416 /* Event 3: Set nStrobe low */
417 parport_frob_control (port,
418 PARPORT_CONTROL_STROBE,
419 PARPORT_CONTROL_STROBE);
421 /* Event 4: Set nStrobe and nAutoFd high */
423 parport_frob_control (port,
424 PARPORT_CONTROL_STROBE
425 | PARPORT_CONTROL_AUTOFD,
428 /* Event 6: nAck goes high */
429 if (parport_wait_peripheral (port,
431 PARPORT_STATUS_ACK)) {
432 /* This shouldn't really happen with a compliant device. */
434 "%s: Mode 0x%02x not supported? (0x%02x)\n",
435 port->name, mode, port->ops->read_status (port));
436 parport_ieee1284_terminate (port);
440 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
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",
447 parport_ieee1284_terminate (port);
451 /* More to do if we've requested extensibility link. */
452 if (mode & IEEE1284_EXT_LINK) {
455 parport_write_data (port, m);
458 /* Event 51: Set nStrobe low */
459 parport_frob_control (port,
460 PARPORT_CONTROL_STROBE,
461 PARPORT_CONTROL_STROBE);
463 /* Event 52: nAck goes low */
464 if (parport_wait_peripheral (port, PARPORT_STATUS_ACK, 0)) {
465 /* This peripheral is _very_ slow. */
467 "%s: Event 52 didn't happen\n",
469 parport_ieee1284_terminate (port);
473 /* Event 53: Set nStrobe high */
474 parport_frob_control (port,
475 PARPORT_CONTROL_STROBE,
478 /* Event 55: nAck goes high */
479 if (parport_wait_peripheral (port,
481 PARPORT_STATUS_ACK)) {
482 /* This shouldn't really happen with a compliant
485 "%s: Mode 0x%02x not supported? (0x%02x)\n",
487 port->ops->read_status (port));
488 parport_ieee1284_terminate (port);
492 /* Event 54: Peripheral sets XFlag to reflect support */
493 xflag = parport_read_status (port) & PARPORT_STATUS_SELECT;
495 /* xflag should be high. */
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);
504 /* Any further setup is left to the caller. */
507 /* Mode is supported */
508 DPRINTK (KERN_DEBUG "%s: In mode 0x%02x\n", port->name, mode);
509 port->ieee1284.mode = mode;
511 /* But ECP is special */
512 if (!(mode & IEEE1284_EXT_LINK) && (m & IEEE1284_MODE_ECP)) {
513 port->ieee1284.phase = IEEE1284_PH_ECP_SETUP;
515 /* Event 30: Set nAutoFd low */
516 parport_frob_control (port,
517 PARPORT_CONTROL_AUTOFD,
518 PARPORT_CONTROL_AUTOFD);
520 /* Event 31: PError goes high. */
521 r = parport_wait_peripheral (port,
522 PARPORT_STATUS_PAPEROUT,
523 PARPORT_STATUS_PAPEROUT);
525 DPRINTK (KERN_INFO "%s: Timeout at event 31\n",
529 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
530 DPRINTK (KERN_DEBUG "%s: ECP direction: forward\n",
532 } else switch (mode) {
533 case IEEE1284_MODE_NIBBLE:
534 case IEEE1284_MODE_BYTE:
535 port->ieee1284.phase = IEEE1284_PH_REV_IDLE;
538 port->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
543 #endif /* IEEE1284 support */
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.
552 #ifdef CONFIG_PARPORT_1284
553 static int parport_ieee1284_ack_data_avail (struct parport *port)
555 if (parport_read_status (port) & PARPORT_STATUS_ERROR)
556 /* Event 18 didn't happen. */
559 /* Event 20: nAutoFd goes high. */
560 port->ops->frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
561 port->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
564 #endif /* IEEE1284 support */
566 /* Handle an interrupt. */
567 void parport_ieee1284_interrupt (void *handle)
569 struct parport *port = handle;
570 parport_ieee1284_wakeup (port);
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);
579 #endif /* IEEE1284 support */
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
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).
593 * It is the caller's responsibility to ensure that the first
594 * @len bytes of @buffer are valid.
596 * This function returns the number of bytes transferred (if zero
597 * or positive), or else an error code.
600 ssize_t parport_write (struct parport *port, const void *buffer, size_t len)
602 #ifndef CONFIG_PARPORT_1284
603 return port->ops->compat_write_data (port, buffer, len, 0);
606 int mode = port->ieee1284.mode;
607 int addr = mode & IEEE1284_ADDR;
608 size_t (*fn) (struct parport *, const void *, size_t, int);
610 /* Ignore the device-ID-request bit and the address bit. */
611 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
613 /* Use the mode we're in. */
615 case IEEE1284_MODE_NIBBLE:
616 case IEEE1284_MODE_BYTE:
617 parport_negotiate (port, IEEE1284_MODE_COMPAT);
619 case IEEE1284_MODE_COMPAT:
620 DPRINTK (KERN_DEBUG "%s: Using compatibility mode\n",
622 fn = port->ops->compat_write_data;
625 case IEEE1284_MODE_EPP:
626 DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
628 fn = port->ops->epp_write_addr;
630 fn = port->ops->epp_write_data;
633 case IEEE1284_MODE_EPPSWE:
634 DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
637 fn = parport_ieee1284_epp_write_addr;
639 fn = parport_ieee1284_epp_write_data;
642 case IEEE1284_MODE_ECP:
643 case IEEE1284_MODE_ECPRLE:
644 DPRINTK (KERN_DEBUG "%s: Using ECP mode\n", port->name);
646 fn = port->ops->ecp_write_addr;
648 fn = port->ops->ecp_write_data;
652 case IEEE1284_MODE_ECPSWE:
653 DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
655 /* The caller has specified that it must be emulated,
656 * even if we have ECP hardware! */
658 fn = parport_ieee1284_ecp_write_addr;
660 fn = parport_ieee1284_ecp_write_data;
665 DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
666 port->ieee1284.mode);
670 retval = (*fn) (port, buffer, len, 0);
671 DPRINTK (KERN_DEBUG "%s: wrote %d/%d bytes\n", port->name, retval, len);
673 #endif /* IEEE1284 support */
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
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).
687 * It is the caller's responsibility to ensure that the first
688 * @len bytes of @buffer are available to write to.
690 * This function returns the number of bytes transferred (if zero
691 * or positive), or else an error code.
694 ssize_t parport_read (struct parport *port, void *buffer, size_t len)
696 #ifndef CONFIG_PARPORT_1284
697 printk (KERN_ERR "parport: IEEE1284 not supported in this kernel\n");
700 int mode = port->physport->ieee1284.mode;
701 int addr = mode & IEEE1284_ADDR;
702 size_t (*fn) (struct parport *, void *, size_t, int);
704 /* Ignore the device-ID-request bit and the address bit. */
705 mode &= ~(IEEE1284_DEVICEID | IEEE1284_ADDR);
707 /* Use the mode we're in. */
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)
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;
722 if (parport_negotiate (port, IEEE1284_MODE_NIBBLE)) {
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;
731 case IEEE1284_MODE_BYTE:
732 DPRINTK (KERN_DEBUG "%s: Using byte mode\n", port->name);
733 fn = port->ops->byte_read_data;
736 case IEEE1284_MODE_EPP:
737 DPRINTK (KERN_DEBUG "%s: Using EPP mode\n", port->name);
739 fn = port->ops->epp_read_addr;
741 fn = port->ops->epp_read_data;
744 case IEEE1284_MODE_EPPSWE:
745 DPRINTK (KERN_DEBUG "%s: Using software-emulated EPP mode\n",
748 fn = parport_ieee1284_epp_read_addr;
750 fn = parport_ieee1284_epp_read_data;
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;
759 case IEEE1284_MODE_ECPSWE:
760 DPRINTK (KERN_DEBUG "%s: Using software-emulated ECP mode\n",
762 fn = parport_ieee1284_ecp_read_data;
766 DPRINTK (KERN_DEBUG "%s: Unknown mode 0x%02x\n", port->name,
767 port->physport->ieee1284.mode);
771 return (*fn) (port, buffer, len, 0);
772 #endif /* IEEE1284 support */
776 * parport_set_timeout - set the inactivity timeout for a device
777 * @dev: device on a port
778 * @inactivity: inactivity timeout (in jiffies)
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
785 * The return value is the previous inactivity timeout.
787 * Any callers of parport_wait_event() for this device are woken
791 long parport_set_timeout (struct pardevice *dev, long inactivity)
793 long int old = dev->timeout;
795 dev->timeout = inactivity;
797 if (dev->port->physport->cad == dev)
798 parport_ieee1284_wakeup (dev->port);
803 /* Exported symbols for modules. */
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);