2 * Copyright (c) 1997 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * General purpose routines for the IEEE1284-1994 Standard
36 #include "opt_ppb_1284.h"
38 #include <sys/param.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
45 #include <dev/ppbus/ppbconf.h>
46 #include <dev/ppbus/ppb_1284.h>
50 #include <dev/ppbus/ppbio.h>
52 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
57 * Wait for the peripherial up to 40ms
60 do_1284_wait(device_t bus, char mask, char status)
62 return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
66 do_peripheral_wait(device_t bus, char mask, char status)
68 return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
71 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
74 * ppb_1284_reset_error()
76 * Unconditionaly reset the error field
79 ppb_1284_reset_error(device_t bus, int state)
81 struct ppb_data *ppb = DEVTOSOFTC(bus);
83 ppb->error = PPB_NO_ERROR;
90 * ppb_1284_get_state()
95 ppb_1284_get_state(device_t bus)
97 struct ppb_data *ppb = DEVTOSOFTC(bus);
99 mtx_assert(ppb->ppc_lock, MA_OWNED);
104 * ppb_1284_set_state()
106 * Change IEEE1284 state if no error occured
109 ppb_1284_set_state(device_t bus, int state)
111 struct ppb_data *ppb = DEVTOSOFTC(bus);
113 /* call ppb_1284_reset_error() if you absolutly want to change
114 * the state from PPB_ERROR to another */
115 mtx_assert(ppb->ppc_lock, MA_OWNED);
116 if ((ppb->state != PPB_ERROR) &&
117 (ppb->error == PPB_NO_ERROR)) {
119 ppb->error = PPB_NO_ERROR;
126 ppb_1284_set_error(device_t bus, int error, int event)
128 struct ppb_data *ppb = DEVTOSOFTC(bus);
130 /* do not accumulate errors */
131 if ((ppb->error == PPB_NO_ERROR) &&
132 (ppb->state != PPB_ERROR)) {
134 ppb->state = PPB_ERROR;
138 printf("ppb1284: error=%d status=0x%x event=%d\n", error,
139 ppb_rstr(bus) & 0xff, event);
148 * Converts mode+options into ext. value
151 ppb_request_mode(int mode, int options)
153 int request_mode = 0;
155 if (options & PPB_EXTENSIBILITY_LINK) {
156 request_mode = EXT_LINK_1284_NORMAL;
161 request_mode = (options & PPB_REQUEST_ID) ?
162 NIBBLE_1284_REQUEST_ID :
166 request_mode = (options & PPB_REQUEST_ID) ?
167 BYTE_1284_REQUEST_ID :
171 if (options & PPB_USE_RLE)
172 request_mode = (options & PPB_REQUEST_ID) ?
173 ECP_1284_RLE_REQUEST_ID :
176 request_mode = (options & PPB_REQUEST_ID) ?
177 ECP_1284_REQUEST_ID :
181 request_mode = EPP_1284_NORMAL;
184 panic("%s: unsupported mode %d\n", __func__, mode);
188 return (request_mode);
192 * ppb_peripheral_negociate()
194 * Negociate the peripheral side
197 ppb_peripheral_negociate(device_t bus, int mode, int options)
199 int spin, request_mode, error = 0;
202 ppb_set_mode(bus, PPB_COMPATIBLE);
203 ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
205 /* compute ext. value */
206 request_mode = ppb_request_mode(mode, options);
210 while (spin-- && (ppb_rstr(bus) & nBUSY))
213 /* check termination */
214 if (!(ppb_rstr(bus) & SELECT) || !spin) {
219 /* Event 4 - read ext. value */
222 /* nibble mode is not supported */
223 if ((r == (char)request_mode) ||
224 (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 /* negociation 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++) {
491 /* Event 7 - ready to take data (nAUTO low) */
492 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
494 /* Event 8 - peripheral writes the first nibble */
496 /* Event 9 - peripheral set nAck low */
497 if ((error = do_1284_wait(bus, nACK, 0))) {
498 ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
503 nibble[i] = ppb_rstr(bus);
505 /* Event 10 - ack, nibble received */
506 ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
508 /* Event 11 - wait ack from peripherial */
509 if ((error = do_1284_wait(bus, nACK, nACK))) {
510 ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
515 *buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
516 (nibble2char(nibble[0]) & 0x0f);
525 * Read in IEEE1284 NIBBLE/BYTE mode
528 spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
530 int error = 0, len = 0;
531 int terminate_after_transfer = 1;
536 state = ppb_1284_get_state(bus);
539 case PPB_FORWARD_IDLE:
540 if ((error = ppb_1284_negociate(bus, mode, 0)))
544 case PPB_REVERSE_IDLE:
545 terminate_after_transfer = 0;
549 ppb_1284_terminate(bus);
550 if ((error = ppb_1284_negociate(bus, mode, 0)))
555 while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
557 ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
565 /* read a byte, error means no more data */
566 if (nibble_1284_inbyte(bus, buffer+len))
570 if (byte_1284_inbyte(bus, buffer+len))
582 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
586 if (terminate_after_transfer || error)
587 ppb_1284_terminate(bus);
597 ppb_1284_read_id(device_t bus, int mode, char *buffer,
602 /* fill the buffer with 0s */
608 if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
610 error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
613 if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
615 error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
618 panic("%s: unsupported mode %d\n", __func__, mode);
621 ppb_1284_terminate(bus);
631 ppb_1284_read(device_t bus, int mode, char *buffer,
639 error = spp_1284_read(bus, mode, buffer, max, read);
649 * ppb_1284_negociate()
651 * IEEE1284 negociation phase
653 * Normal nibble mode or request device id mode (see ppb_1284.h)
655 * After negociation, nFAULT is low if data is available
658 ppb_1284_negociate(device_t bus, int mode, int options)
667 if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
668 ppb_peripheral_terminate(bus, PPB_WAIT);
670 if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
671 ppb_1284_terminate(bus);
677 /* ensure the host is in compatible mode */
678 ppb_set_mode(bus, PPB_COMPATIBLE);
680 /* reset error to catch the actual negociation error */
681 ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
683 /* calculate ext. value */
684 request_mode = ppb_request_mode(mode, options);
687 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
690 /* enter negociation phase */
691 ppb_1284_set_state(bus, PPB_NEGOCIATION);
693 /* Event 0 - put the exten. value on the data lines */
694 ppb_wdtr(bus, request_mode);
697 /* request remote host attention */
698 ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
700 ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
704 #endif /* !PERIPH_1284 */
706 /* Event 1 - enter IEEE1284 mode */
707 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
710 /* ignore the PError line, wait a bit more, remote host's
711 * interrupts don't respond fast enough */
712 if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT,
713 SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) {
714 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
719 /* Event 2 - trying IEEE1284 dialog */
720 if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
721 PERROR | SELECT | nFAULT)) {
722 ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
726 #endif /* !PERIPH_1284 */
728 /* Event 3 - latch the ext. value to the peripheral */
729 ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
732 /* Event 4 - IEEE1284 device recognized */
733 ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
735 /* Event 6 - waiting for status lines */
736 if (do_1284_wait(bus, nACK, nACK)) {
737 ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
742 /* Event 7 - quering result consider nACK not to misunderstand
743 * a remote computer terminate sequence */
744 if (options & PPB_EXTENSIBILITY_LINK) {
746 /* XXX not fully supported yet */
747 ppb_1284_terminate(bus);
751 if (request_mode == NIBBLE_1284_NORMAL) {
752 if (do_1284_wait(bus, nACK | SELECT, nACK)) {
753 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
758 if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
759 ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
768 /* enter reverse idle phase */
769 ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
772 /* negociation ok, now setup the communication */
773 ppb_1284_set_state(bus, PPB_SETUP);
774 ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
777 /* ignore PError line */
778 if (do_1284_wait(bus, nACK | SELECT | nBUSY,
779 nACK | SELECT | nBUSY)) {
780 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
785 if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
786 nACK | SELECT | PERROR | nBUSY)) {
787 ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
791 #endif /* !PERIPH_1284 */
793 /* ok, the host enters the ForwardIdle state */
794 ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
797 ppb_1284_set_state(bus, PPB_EPP_IDLE);
801 panic("%s: unknown mode (%d)!", __func__, mode);
803 ppb_set_mode(bus, mode);
808 ppb_1284_terminate(bus);
814 * ppb_1284_terminate()
816 * IEEE1284 termination phase, return code should ignored since the host
817 * is _always_ in compatible mode after ppb_1284_terminate()
820 ppb_1284_terminate(device_t bus)
827 /* do not reset error here to keep the error that
828 * may occured before the ppb_1284_terminate() call */
829 ppb_1284_set_state(bus, PPB_TERMINATION);
832 /* request remote host attention */
833 ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
835 #endif /* PERIPH_1284 */
837 /* Event 22 - set nSelectin low and nAutoFeed high */
838 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
840 /* Event 24 - waiting for peripheral, Xflag ignored */
841 if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
842 ppb_1284_set_error(bus, PPB_TIMEOUT, 24);
846 /* Event 25 - set nAutoFd low */
847 ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
849 /* Event 26 - compatible mode status is set */
851 /* Event 27 - peripheral set nAck high */
852 if (do_1284_wait(bus, nACK, nACK)) {
853 ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
856 /* Event 28 - end termination, return to idle phase */
857 ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
860 /* return to compatible mode */
861 ppb_set_mode(bus, PPB_COMPATIBLE);
862 ppb_1284_set_state(bus, PPB_FORWARD_IDLE);