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>
47 #include <dev/ppbus/ppbconf.h>
48 #include <dev/ppbus/ppb_1284.h>
52 #include <dev/ppbus/ppbio.h>
54 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
59 * Wait for the peripherial up to 40ms
62 do_1284_wait(device_t bus, uint8_t mask, uint8_t status)
64 return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
68 do_peripheral_wait(device_t bus, uint8_t mask, uint8_t status)
70 return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
73 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
76 * ppb_1284_reset_error()
78 * Unconditionaly reset the error field
81 ppb_1284_reset_error(device_t bus, int state)
83 struct ppb_data *ppb = DEVTOSOFTC(bus);
85 ppb->error = PPB_NO_ERROR;
92 * ppb_1284_get_state()
97 ppb_1284_get_state(device_t bus)
99 struct ppb_data *ppb = DEVTOSOFTC(bus);
101 mtx_assert(ppb->ppc_lock, MA_OWNED);
106 * ppb_1284_set_state()
108 * Change IEEE1284 state if no error occurred
111 ppb_1284_set_state(device_t bus, int state)
113 struct ppb_data *ppb = DEVTOSOFTC(bus);
115 /* call ppb_1284_reset_error() if you absolutely want to change
116 * the state from PPB_ERROR to another */
117 mtx_assert(ppb->ppc_lock, MA_OWNED);
118 if ((ppb->state != PPB_ERROR) &&
119 (ppb->error == PPB_NO_ERROR)) {
121 ppb->error = PPB_NO_ERROR;
128 ppb_1284_set_error(device_t bus, int error, int event)
130 struct ppb_data *ppb = DEVTOSOFTC(bus);
132 /* do not accumulate errors */
133 if ((ppb->error == PPB_NO_ERROR) &&
134 (ppb->state != PPB_ERROR)) {
136 ppb->state = PPB_ERROR;
140 printf("ppb1284: error=%d status=0x%x event=%d\n", error,
141 ppb_rstr(bus) & 0xff, event);
150 * Converts mode+options into ext. value
153 ppb_request_mode(int mode, int options)
155 int request_mode = 0;
157 if (options & PPB_EXTENSIBILITY_LINK) {
158 request_mode = EXT_LINK_1284_NORMAL;
163 request_mode = (options & PPB_REQUEST_ID) ?
164 NIBBLE_1284_REQUEST_ID :
168 request_mode = (options & PPB_REQUEST_ID) ?
169 BYTE_1284_REQUEST_ID :
173 if (options & PPB_USE_RLE)
174 request_mode = (options & PPB_REQUEST_ID) ?
175 ECP_1284_RLE_REQUEST_ID :
178 request_mode = (options & PPB_REQUEST_ID) ?
179 ECP_1284_REQUEST_ID :
183 request_mode = EPP_1284_NORMAL;
186 panic("%s: unsupported mode %d\n", __func__, mode);
190 return (request_mode);
194 * ppb_peripheral_negociate()
196 * Negotiate the peripheral side
199 ppb_peripheral_negociate(device_t bus, int mode, int options)
201 int spin, request_mode, error = 0;
204 ppb_set_mode(bus, PPB_COMPATIBLE);
205 ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
207 /* compute ext. value */
208 request_mode = ppb_request_mode(mode, options);
212 while (spin-- && (ppb_rstr(bus) & nBUSY))
215 /* check termination */
216 if (!(ppb_rstr(bus) & SELECT) || !spin) {
221 /* Event 4 - read ext. value */
224 /* nibble mode is not supported */
225 if ((r == (char)request_mode) ||
226 (r == NIBBLE_1284_NORMAL)) {
228 /* Event 5 - restore direction bit, no data avail */
229 ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN));
233 ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE));
235 if (r == NIBBLE_1284_NORMAL) {
239 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
243 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
245 case BYTE_1284_NORMAL:
246 ppb_set_mode(bus, PPB_BYTE);
254 /* negotiation succeeds */
257 /* Event 5 - mode not supported */
258 ppb_wctr(bus, SELECTIN);
262 ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT));
263 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
275 ppb_peripheral_terminate(bus, PPB_WAIT);
280 * ppb_peripheral_terminate()
282 * Terminate peripheral transfer side
284 * Always return 0 in compatible mode
287 ppb_peripheral_terminate(device_t bus, int how)
295 ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION);
297 /* Event 22 - wait up to host response time (1s) */
298 if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
299 ppb_1284_set_error(bus, PPB_TIMEOUT, 22);
304 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
306 /* Event 25 - wait up to host response time (1s) */
307 if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
308 ppb_1284_set_error(bus, PPB_TIMEOUT, 25);
313 ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
316 ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
318 /* Event 28 - wait up to host response time (1s) */
319 if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
320 ppb_1284_set_error(bus, PPB_TIMEOUT, 28);
325 ppb_set_mode(bus, PPB_COMPATIBLE);
326 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
332 * byte_peripheral_outbyte()
334 * Write 1 byte in BYTE mode
337 byte_peripheral_outbyte(device_t bus, char *buffer, int last)
342 if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
343 ppb_1284_set_error(bus, PPB_TIMEOUT, 7);
347 /* check termination */
348 if (!(ppb_rstr(bus) & SELECT)) {
349 ppb_peripheral_terminate(bus, PPB_WAIT);
353 /* Event 15 - put byte on data lines */
357 ppb_wdtr(bus, *buffer);
360 ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
362 /* Event 10 - wait data read */
363 if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
364 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
370 ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
372 ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
376 /* Event 16 - wait strobe */
377 if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
378 ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
383 /* check termination */
384 if (!(ppb_rstr(bus) & SELECT)) {
385 ppb_peripheral_terminate(bus, PPB_WAIT);
394 * byte_peripheral_write()
396 * Write n bytes in BYTE mode
399 byte_peripheral_write(device_t bus, char *buffer, int len, int *sent)
404 ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER);
406 /* wait forever, the remote host is master and should initiate
409 for (i=0; i<len; i++) {
410 /* force remote nFAULT low to release the remote waiting
414 ppb_wctr(bus, r & ~nINIT);
420 error = ppb_poll_bus(bus, PPB_FOREVER, nBUSY, nBUSY,
423 if (error && error != EWOULDBLOCK)
429 if ((error = byte_peripheral_outbyte(bus, buffer+i, (i == len-1))))
434 ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
443 * Read 1 byte in BYTE mode
446 byte_1284_inbyte(device_t bus, char *buffer)
450 /* Event 7 - ready to take data (nAUTO low) */
451 ppb_wctr(bus, (PCD | nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
453 /* Event 9 - peripheral set nAck low */
454 if ((error = do_1284_wait(bus, nACK, 0))) {
455 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
460 *buffer = ppb_rdtr(bus);
462 /* Event 10 - data received, can't accept more */
463 ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
465 /* Event 11 - peripheral ack */
466 if ((error = do_1284_wait(bus, nACK, nACK))) {
467 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
471 /* Event 16 - strobe */
472 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
474 ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
481 * nibble_1284_inbyte()
483 * Read 1 byte in NIBBLE mode
486 nibble_1284_inbyte(device_t bus, char *buffer)
491 for (i = 0; i < 2; i++) {
493 /* Event 7 - ready to take data (nAUTO low) */
494 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
496 /* Event 8 - peripheral writes the first nibble */
498 /* Event 9 - peripheral set nAck low */
499 if ((error = do_1284_wait(bus, nACK, 0))) {
500 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
505 nibble[i] = ppb_rstr(bus);
507 /* Event 10 - ack, nibble received */
508 ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
510 /* Event 11 - wait ack from peripherial */
511 if ((error = do_1284_wait(bus, nACK, nACK))) {
512 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
517 *buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
518 (nibble2char(nibble[0]) & 0x0f);
527 * Read in IEEE1284 NIBBLE/BYTE mode
530 spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
532 int error = 0, len = 0;
533 int terminate_after_transfer = 1;
538 state = ppb_1284_get_state(bus);
541 case PPB_FORWARD_IDLE:
542 if ((error = ppb_1284_negociate(bus, mode, 0)))
546 case PPB_REVERSE_IDLE:
547 terminate_after_transfer = 0;
551 ppb_1284_terminate(bus);
552 if ((error = ppb_1284_negociate(bus, mode, 0)))
557 while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
559 ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
567 /* read a byte, error means no more data */
568 if (nibble_1284_inbyte(bus, buffer+len))
572 if (byte_1284_inbyte(bus, buffer+len))
584 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
588 if (terminate_after_transfer || error)
589 ppb_1284_terminate(bus);
599 ppb_1284_read_id(device_t bus, int mode, char *buffer,
604 /* fill the buffer with 0s */
610 if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
612 error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
615 if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
617 error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
620 panic("%s: unsupported mode %d\n", __func__, mode);
623 ppb_1284_terminate(bus);
633 ppb_1284_read(device_t bus, int mode, char *buffer,
641 error = spp_1284_read(bus, mode, buffer, max, read);
651 * ppb_1284_negociate()
653 * IEEE1284 negotiation phase
655 * Normal nibble mode or request device id mode (see ppb_1284.h)
657 * After negotiation, nFAULT is low if data is available
660 ppb_1284_negociate(device_t bus, int mode, int options)
669 if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
670 ppb_peripheral_terminate(bus, PPB_WAIT);
672 if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
673 ppb_1284_terminate(bus);
679 /* ensure the host is in compatible mode */
680 ppb_set_mode(bus, PPB_COMPATIBLE);
682 /* reset error to catch the actual negotiation error */
683 ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
685 /* calculate ext. value */
686 request_mode = ppb_request_mode(mode, options);
689 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
692 /* enter negotiation phase */
693 ppb_1284_set_state(bus, PPB_NEGOCIATION);
695 /* Event 0 - put the exten. value on the data lines */
696 ppb_wdtr(bus, request_mode);
699 /* request remote host attention */
700 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
702 ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
706 #endif /* !PERIPH_1284 */
708 /* Event 1 - enter IEEE1284 mode */
709 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
712 /* ignore the PError line, wait a bit more, remote host's
713 * interrupts don't respond fast enough */
714 if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT,
715 SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) {
716 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
721 /* Event 2 - trying IEEE1284 dialog */
722 if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
723 PERROR | SELECT | nFAULT)) {
724 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
728 #endif /* !PERIPH_1284 */
730 /* Event 3 - latch the ext. value to the peripheral */
731 ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
734 /* Event 4 - IEEE1284 device recognized */
735 ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
737 /* Event 6 - waiting for status lines */
738 if (do_1284_wait(bus, nACK, nACK)) {
739 ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
744 /* Event 7 - quering result consider nACK not to misunderstand
745 * a remote computer terminate sequence */
746 if (options & PPB_EXTENSIBILITY_LINK) {
748 /* XXX not fully supported yet */
749 ppb_1284_terminate(bus);
753 if (request_mode == NIBBLE_1284_NORMAL) {
754 if (do_1284_wait(bus, nACK | SELECT, nACK)) {
755 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
760 if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
761 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
770 /* enter reverse idle phase */
771 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
774 /* negotiation ok, now setup the communication */
775 ppb_1284_set_state(bus, PPB_SETUP);
776 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
779 /* ignore PError line */
780 if (do_1284_wait(bus, nACK | SELECT | nBUSY,
781 nACK | SELECT | nBUSY)) {
782 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
787 if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
788 nACK | SELECT | PERROR | nBUSY)) {
789 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
793 #endif /* !PERIPH_1284 */
795 /* ok, the host enters the ForwardIdle state */
796 ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
799 ppb_1284_set_state(bus, PPB_EPP_IDLE);
803 panic("%s: unknown mode (%d)!", __func__, mode);
805 ppb_set_mode(bus, mode);
810 ppb_1284_terminate(bus);
816 * ppb_1284_terminate()
818 * IEEE1284 termination phase, return code should ignored since the host
819 * is _always_ in compatible mode after ppb_1284_terminate()
822 ppb_1284_terminate(device_t bus)
829 /* do not reset error here to keep the error that
830 * may occurred before the ppb_1284_terminate() call */
831 ppb_1284_set_state(bus, PPB_TERMINATION);
834 /* request remote host attention */
835 ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
837 #endif /* PERIPH_1284 */
839 /* Event 22 - set nSelectin low and nAutoFeed high */
840 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
842 /* Event 24 - waiting for peripheral, Xflag ignored */
843 if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
844 ppb_1284_set_error(bus, PPB_TIMEOUT, 24);
848 /* Event 25 - set nAutoFd low */
849 ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
851 /* Event 26 - compatible mode status is set */
853 /* Event 27 - peripheral set nAck high */
854 if (do_1284_wait(bus, nACK, nACK)) {
855 ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
858 /* Event 28 - end termination, return to idle phase */
859 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
862 /* return to compatible mode */
863 ppb_set_mode(bus, PPB_COMPATIBLE);
864 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);