2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * General purpose routines for the IEEE1284-1994 Standard
38 #include "opt_ppb_1284.h"
40 #include <sys/param.h>
42 #include <sys/mutex.h>
43 #include <sys/systm.h>
46 #include <dev/ppbus/ppbconf.h>
47 #include <dev/ppbus/ppb_1284.h>
51 #include <dev/ppbus/ppbio.h>
53 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
58 * Wait for the peripherial up to 40ms
61 do_1284_wait(device_t bus, uint8_t mask, uint8_t status)
63 return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
67 do_peripheral_wait(device_t bus, uint8_t mask, uint8_t status)
69 return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
72 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
75 * ppb_1284_reset_error()
77 * Unconditionaly reset the error field
80 ppb_1284_reset_error(device_t bus, int state)
82 struct ppb_data *ppb = DEVTOSOFTC(bus);
84 ppb->error = PPB_NO_ERROR;
91 * ppb_1284_get_state()
96 ppb_1284_get_state(device_t bus)
98 struct ppb_data *ppb = DEVTOSOFTC(bus);
100 mtx_assert(ppb->ppc_lock, MA_OWNED);
105 * ppb_1284_set_state()
107 * Change IEEE1284 state if no error occurred
110 ppb_1284_set_state(device_t bus, int state)
112 struct ppb_data *ppb = DEVTOSOFTC(bus);
114 /* call ppb_1284_reset_error() if you absolutely want to change
115 * the state from PPB_ERROR to another */
116 mtx_assert(ppb->ppc_lock, MA_OWNED);
117 if ((ppb->state != PPB_ERROR) &&
118 (ppb->error == PPB_NO_ERROR)) {
120 ppb->error = PPB_NO_ERROR;
127 ppb_1284_set_error(device_t bus, int error, int event)
129 struct ppb_data *ppb = DEVTOSOFTC(bus);
131 /* do not accumulate errors */
132 if ((ppb->error == PPB_NO_ERROR) &&
133 (ppb->state != PPB_ERROR)) {
135 ppb->state = PPB_ERROR;
139 printf("ppb1284: error=%d status=0x%x event=%d\n", error,
140 ppb_rstr(bus) & 0xff, event);
149 * Converts mode+options into ext. value
152 ppb_request_mode(int mode, int options)
154 int request_mode = 0;
156 if (options & PPB_EXTENSIBILITY_LINK) {
157 request_mode = EXT_LINK_1284_NORMAL;
162 request_mode = (options & PPB_REQUEST_ID) ?
163 NIBBLE_1284_REQUEST_ID :
167 request_mode = (options & PPB_REQUEST_ID) ?
168 BYTE_1284_REQUEST_ID :
172 if (options & PPB_USE_RLE)
173 request_mode = (options & PPB_REQUEST_ID) ?
174 ECP_1284_RLE_REQUEST_ID :
177 request_mode = (options & PPB_REQUEST_ID) ?
178 ECP_1284_REQUEST_ID :
182 request_mode = EPP_1284_NORMAL;
185 panic("%s: unsupported mode %d\n", __func__, mode);
189 return (request_mode);
193 * ppb_peripheral_negociate()
195 * Negotiate the peripheral side
198 ppb_peripheral_negociate(device_t bus, int mode, int options)
200 int spin, request_mode, error = 0;
203 ppb_set_mode(bus, PPB_COMPATIBLE);
204 ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
206 /* compute ext. value */
207 request_mode = ppb_request_mode(mode, options);
211 while (spin-- && (ppb_rstr(bus) & nBUSY))
214 /* check termination */
215 if (!(ppb_rstr(bus) & SELECT) || !spin) {
220 /* Event 4 - read ext. value */
223 /* nibble mode is not supported */
224 if ((r == (char)request_mode) ||
225 (r == NIBBLE_1284_NORMAL)) {
226 /* Event 5 - restore direction bit, no data avail */
227 ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN));
231 ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE));
233 if (r == NIBBLE_1284_NORMAL) {
237 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
241 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
243 case BYTE_1284_NORMAL:
244 ppb_set_mode(bus, PPB_BYTE);
252 /* negotiation succeeds */
255 /* Event 5 - mode not supported */
256 ppb_wctr(bus, SELECTIN);
260 ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT));
261 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
273 ppb_peripheral_terminate(bus, PPB_WAIT);
278 * ppb_peripheral_terminate()
280 * Terminate peripheral transfer side
282 * Always return 0 in compatible mode
285 ppb_peripheral_terminate(device_t bus, int how)
293 ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION);
295 /* Event 22 - wait up to host response time (1s) */
296 if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
297 ppb_1284_set_error(bus, PPB_TIMEOUT, 22);
302 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
304 /* Event 25 - wait up to host response time (1s) */
305 if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
306 ppb_1284_set_error(bus, PPB_TIMEOUT, 25);
311 ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
314 ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
316 /* Event 28 - wait up to host response time (1s) */
317 if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
318 ppb_1284_set_error(bus, PPB_TIMEOUT, 28);
323 ppb_set_mode(bus, PPB_COMPATIBLE);
324 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
330 * byte_peripheral_outbyte()
332 * Write 1 byte in BYTE mode
335 byte_peripheral_outbyte(device_t bus, char *buffer, int last)
340 if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
341 ppb_1284_set_error(bus, PPB_TIMEOUT, 7);
345 /* check termination */
346 if (!(ppb_rstr(bus) & SELECT)) {
347 ppb_peripheral_terminate(bus, PPB_WAIT);
351 /* Event 15 - put byte on data lines */
355 ppb_wdtr(bus, *buffer);
358 ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
360 /* Event 10 - wait data read */
361 if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
362 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
368 ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
370 ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
374 /* Event 16 - wait strobe */
375 if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
376 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
381 /* check termination */
382 if (!(ppb_rstr(bus) & SELECT)) {
383 ppb_peripheral_terminate(bus, PPB_WAIT);
392 * byte_peripheral_write()
394 * Write n bytes in BYTE mode
397 byte_peripheral_write(device_t bus, char *buffer, int len, int *sent)
402 ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER);
404 /* wait forever, the remote host is master and should initiate
407 for (i=0; i<len; i++) {
408 /* force remote nFAULT low to release the remote waiting
412 ppb_wctr(bus, r & ~nINIT);
418 error = ppb_poll_bus(bus, PPB_FOREVER, nBUSY, nBUSY,
421 if (error && error != EWOULDBLOCK)
427 if ((error = byte_peripheral_outbyte(bus, buffer+i, (i == len-1))))
432 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
441 * Read 1 byte in BYTE mode
444 byte_1284_inbyte(device_t bus, char *buffer)
448 /* Event 7 - ready to take data (nAUTO low) */
449 ppb_wctr(bus, (PCD | nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
451 /* Event 9 - peripheral set nAck low */
452 if ((error = do_1284_wait(bus, nACK, 0))) {
453 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
458 *buffer = ppb_rdtr(bus);
460 /* Event 10 - data received, can't accept more */
461 ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
463 /* Event 11 - peripheral ack */
464 if ((error = do_1284_wait(bus, nACK, nACK))) {
465 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
469 /* Event 16 - strobe */
470 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
472 ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
479 * nibble_1284_inbyte()
481 * Read 1 byte in NIBBLE mode
484 nibble_1284_inbyte(device_t bus, char *buffer)
489 for (i = 0; i < 2; i++) {
490 /* Event 7 - ready to take data (nAUTO low) */
491 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
493 /* Event 8 - peripheral writes the first nibble */
495 /* Event 9 - peripheral set nAck low */
496 if ((error = do_1284_wait(bus, nACK, 0))) {
497 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
502 nibble[i] = ppb_rstr(bus);
504 /* Event 10 - ack, nibble received */
505 ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
507 /* Event 11 - wait ack from peripherial */
508 if ((error = do_1284_wait(bus, nACK, nACK))) {
509 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
514 *buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
515 (nibble2char(nibble[0]) & 0x0f);
524 * Read in IEEE1284 NIBBLE/BYTE mode
527 spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
529 int error = 0, len = 0;
530 int terminate_after_transfer = 1;
535 state = ppb_1284_get_state(bus);
538 case PPB_FORWARD_IDLE:
539 if ((error = ppb_1284_negociate(bus, mode, 0)))
543 case PPB_REVERSE_IDLE:
544 terminate_after_transfer = 0;
548 ppb_1284_terminate(bus);
549 if ((error = ppb_1284_negociate(bus, mode, 0)))
554 while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
555 ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
563 /* read a byte, error means no more data */
564 if (nibble_1284_inbyte(bus, buffer+len))
568 if (byte_1284_inbyte(bus, buffer+len))
580 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
584 if (terminate_after_transfer || error)
585 ppb_1284_terminate(bus);
595 ppb_1284_read_id(device_t bus, int mode, char *buffer,
600 /* fill the buffer with 0s */
606 if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
608 error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
611 if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
613 error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
616 panic("%s: unsupported mode %d\n", __func__, mode);
619 ppb_1284_terminate(bus);
629 ppb_1284_read(device_t bus, int mode, char *buffer,
637 error = spp_1284_read(bus, mode, buffer, max, read);
647 * ppb_1284_negociate()
649 * IEEE1284 negotiation phase
651 * Normal nibble mode or request device id mode (see ppb_1284.h)
653 * After negotiation, nFAULT is low if data is available
656 ppb_1284_negociate(device_t bus, int mode, int options)
665 if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
666 ppb_peripheral_terminate(bus, PPB_WAIT);
668 if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
669 ppb_1284_terminate(bus);
675 /* ensure the host is in compatible mode */
676 ppb_set_mode(bus, PPB_COMPATIBLE);
678 /* reset error to catch the actual negotiation error */
679 ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
681 /* calculate ext. value */
682 request_mode = ppb_request_mode(mode, options);
685 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
688 /* enter negotiation phase */
689 ppb_1284_set_state(bus, PPB_NEGOCIATION);
691 /* Event 0 - put the exten. value on the data lines */
692 ppb_wdtr(bus, request_mode);
695 /* request remote host attention */
696 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
698 ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
702 #endif /* !PERIPH_1284 */
704 /* Event 1 - enter IEEE1284 mode */
705 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
708 /* ignore the PError line, wait a bit more, remote host's
709 * interrupts don't respond fast enough */
710 if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT,
711 SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) {
712 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
717 /* Event 2 - trying IEEE1284 dialog */
718 if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
719 PERROR | SELECT | nFAULT)) {
720 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
724 #endif /* !PERIPH_1284 */
726 /* Event 3 - latch the ext. value to the peripheral */
727 ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
730 /* Event 4 - IEEE1284 device recognized */
731 ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
733 /* Event 6 - waiting for status lines */
734 if (do_1284_wait(bus, nACK, nACK)) {
735 ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
740 /* Event 7 - quering result consider nACK not to misunderstand
741 * a remote computer terminate sequence */
742 if (options & PPB_EXTENSIBILITY_LINK) {
743 /* XXX not fully supported yet */
744 ppb_1284_terminate(bus);
747 if (request_mode == NIBBLE_1284_NORMAL) {
748 if (do_1284_wait(bus, nACK | SELECT, nACK)) {
749 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
754 if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
755 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
764 /* enter reverse idle phase */
765 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
768 /* negotiation ok, now setup the communication */
769 ppb_1284_set_state(bus, PPB_SETUP);
770 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
773 /* ignore PError line */
774 if (do_1284_wait(bus, nACK | SELECT | nBUSY,
775 nACK | SELECT | nBUSY)) {
776 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
781 if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
782 nACK | SELECT | PERROR | nBUSY)) {
783 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
787 #endif /* !PERIPH_1284 */
789 /* ok, the host enters the ForwardIdle state */
790 ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
793 ppb_1284_set_state(bus, PPB_EPP_IDLE);
797 panic("%s: unknown mode (%d)!", __func__, mode);
799 ppb_set_mode(bus, mode);
804 ppb_1284_terminate(bus);
810 * ppb_1284_terminate()
812 * IEEE1284 termination phase, return code should ignored since the host
813 * is _always_ in compatible mode after ppb_1284_terminate()
816 ppb_1284_terminate(device_t bus)
823 /* do not reset error here to keep the error that
824 * may occurred before the ppb_1284_terminate() call */
825 ppb_1284_set_state(bus, PPB_TERMINATION);
828 /* request remote host attention */
829 ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
831 #endif /* PERIPH_1284 */
833 /* Event 22 - set nSelectin low and nAutoFeed high */
834 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
836 /* Event 24 - waiting for peripheral, Xflag ignored */
837 if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
838 ppb_1284_set_error(bus, PPB_TIMEOUT, 24);
842 /* Event 25 - set nAutoFd low */
843 ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
845 /* Event 26 - compatible mode status is set */
847 /* Event 27 - peripheral set nAck high */
848 if (do_1284_wait(bus, nACK, nACK)) {
849 ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
852 /* Event 28 - end termination, return to idle phase */
853 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
856 /* return to compatible mode */
857 ppb_set_mode(bus, PPB_COMPATIBLE);
858 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);