/*- * Copyright (c) 1997 Nicolas Souchu * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/dev/ppbus/ppb_1284.c,v 1.11 2000/01/14 08:03:14 nsouch Exp $ * $DragonFly: src/sys/bus/ppbus/ppb_1284.c,v 1.3 2003/08/07 21:16:47 dillon Exp $ * */ /* * General purpose routines for the IEEE1284-1994 Standard */ #include "opt_ppb_1284.h" #include #include #include #include #include "ppbconf.h" #include "ppb_1284.h" #include "ppbus_if.h" #include "ppbio.h" #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev)) /* * do_1284_wait() * * Wait for the peripherial up to 40ms */ static int do_1284_wait(device_t bus, char mask, char status) { return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL)); } static int do_peripheral_wait(device_t bus, char mask, char status) { return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL)); } #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4) /* * ppb_1284_reset_error() * * Unconditionaly reset the error field */ static int ppb_1284_reset_error(device_t bus, int state) { struct ppb_data *ppb = DEVTOSOFTC(bus); ppb->error = PPB_NO_ERROR; ppb->state = state; return (0); } /* * ppb_1284_get_state() * * Get IEEE1284 state */ int ppb_1284_get_state(device_t bus) { return (DEVTOSOFTC(bus)->state); } /* * ppb_1284_set_state() * * Change IEEE1284 state if no error occured */ int ppb_1284_set_state(device_t bus, int state) { struct ppb_data *ppb = DEVTOSOFTC(bus); /* call ppb_1284_reset_error() if you absolutly want to change * the state from PPB_ERROR to another */ if ((ppb->state != PPB_ERROR) && (ppb->error == PPB_NO_ERROR)) { ppb->state = state; ppb->error = PPB_NO_ERROR; } return (0); } static int ppb_1284_set_error(device_t bus, int error, int event) { struct ppb_data *ppb = DEVTOSOFTC(bus); /* do not accumulate errors */ if ((ppb->error == PPB_NO_ERROR) && (ppb->state != PPB_ERROR)) { ppb->error = error; ppb->state = PPB_ERROR; } #ifdef DEBUG_1284 printf("ppb1284: error=%d status=0x%x event=%d\n", error, ppb_rstr(bus) & 0xff, event); #endif return (0); } /* * ppb_request_mode() * * Converts mode+options into ext. value */ static int ppb_request_mode(int mode, int options) { int request_mode = 0; if (options & PPB_EXTENSIBILITY_LINK) { request_mode = EXT_LINK_1284_NORMAL; } else { switch (mode) { case PPB_NIBBLE: request_mode = (options & PPB_REQUEST_ID) ? NIBBLE_1284_REQUEST_ID : NIBBLE_1284_NORMAL; break; case PPB_PS2: request_mode = (options & PPB_REQUEST_ID) ? BYTE_1284_REQUEST_ID : BYTE_1284_NORMAL; break; case PPB_ECP: if (options & PPB_USE_RLE) request_mode = (options & PPB_REQUEST_ID) ? ECP_1284_RLE_REQUEST_ID : ECP_1284_RLE; else request_mode = (options & PPB_REQUEST_ID) ? ECP_1284_REQUEST_ID : ECP_1284_NORMAL; break; case PPB_EPP: request_mode = EPP_1284_NORMAL; break; default: panic("%s: unsupported mode %d\n", __FUNCTION__, mode); } } return (request_mode); } /* * ppb_peripheral_negociate() * * Negociate the peripheral side */ int ppb_peripheral_negociate(device_t bus, int mode, int options) { int spin, request_mode, error = 0; char r; ppb_set_mode(bus, PPB_COMPATIBLE); ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION); /* compute ext. value */ request_mode = ppb_request_mode(mode, options); /* wait host */ spin = 10; while (spin-- && (ppb_rstr(bus) & nBUSY)) DELAY(1); /* check termination */ if (!(ppb_rstr(bus) & SELECT) || !spin) { error = ENODEV; goto error; } /* Event 4 - read ext. value */ r = ppb_rdtr(bus); /* nibble mode is not supported */ if ((r == (char)request_mode) || (r == NIBBLE_1284_NORMAL)) { /* Event 5 - restore direction bit, no data avail */ ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN)); DELAY(1); /* Event 6 */ ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE)); if (r == NIBBLE_1284_NORMAL) { #ifdef DEBUG_1284 printf("R"); #endif ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4); error = EINVAL; goto error; } else { ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE); switch (r) { case BYTE_1284_NORMAL: ppb_set_mode(bus, PPB_BYTE); break; default: break; } #ifdef DEBUG_1284 printf("A"); #endif /* negociation succeeds */ } } else { /* Event 5 - mode not supported */ ppb_wctr(bus, SELECTIN); DELAY(1); /* Event 6 */ ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT)); ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4); #ifdef DEBUG_1284 printf("r"); #endif error = EINVAL; goto error; } return (0); error: ppb_peripheral_terminate(bus, PPB_WAIT); return (error); } /* * ppb_peripheral_terminate() * * Terminate peripheral transfer side * * Always return 0 in compatible mode */ int ppb_peripheral_terminate(device_t bus, int how) { int error = 0; #ifdef DEBUG_1284 printf("t"); #endif ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION); /* Event 22 - wait up to host response time (1s) */ if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) { ppb_1284_set_error(bus, PPB_TIMEOUT, 22); goto error; } /* Event 24 */ ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN)); /* Event 25 - wait up to host response time (1s) */ if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) { ppb_1284_set_error(bus, PPB_TIMEOUT, 25); goto error; } /* Event 26 */ ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED)); DELAY(1); /* Event 27 */ ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED)); /* Event 28 - wait up to host response time (1s) */ if ((error = do_peripheral_wait(bus, nBUSY, 0))) { ppb_1284_set_error(bus, PPB_TIMEOUT, 28); goto error; } error: ppb_set_mode(bus, PPB_COMPATIBLE); ppb_1284_set_state(bus, PPB_FORWARD_IDLE); return (0); } /* * byte_peripheral_outbyte() * * Write 1 byte in BYTE mode */ static int byte_peripheral_outbyte(device_t bus, char *buffer, int last) { int error = 0; /* Event 7 */ if ((error = do_1284_wait(bus, nBUSY, nBUSY))) { ppb_1284_set_error(bus, PPB_TIMEOUT, 7); goto error; } /* check termination */ if (!(ppb_rstr(bus) & SELECT)) { ppb_peripheral_terminate(bus, PPB_WAIT); goto error; } /* Event 15 - put byte on data lines */ #ifdef DEBUG_1284 printf("B"); #endif ppb_wdtr(bus, *buffer); /* Event 9 */ ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN)); /* Event 10 - wait data read */ if ((error = do_peripheral_wait(bus, nBUSY, 0))) { ppb_1284_set_error(bus, PPB_TIMEOUT, 16); goto error; } /* Event 11 */ if (!last) { ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN)); } else { ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED)); } #if 0 /* Event 16 - wait strobe */ if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) { ppb_1284_set_error(bus, PPB_TIMEOUT, 16); goto error; } #endif /* check termination */ if (!(ppb_rstr(bus) & SELECT)) { ppb_peripheral_terminate(bus, PPB_WAIT); goto error; } error: return (error); } /* * byte_peripheral_write() * * Write n bytes in BYTE mode */ int byte_peripheral_write(device_t bus, char *buffer, int len, int *sent) { int error = 0, i; char r; ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER); /* wait forever, the remote host is master and should initiate * termination */ for (i=0; i= PPB_PERIPHERAL_NEGOCIATION) ppb_peripheral_terminate(bus, PPB_WAIT); if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE) ppb_1284_terminate(bus); #ifdef DEBUG_1284 printf("%d", mode); #endif /* ensure the host is in compatible mode */ ppb_set_mode(bus, PPB_COMPATIBLE); /* reset error to catch the actual negociation error */ ppb_1284_reset_error(bus, PPB_FORWARD_IDLE); /* calculate ext. value */ request_mode = ppb_request_mode(mode, options); /* default state */ ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED)); DELAY(1); /* enter negociation phase */ ppb_1284_set_state(bus, PPB_NEGOCIATION); /* Event 0 - put the exten. value on the data lines */ ppb_wdtr(bus, request_mode); #ifdef PERIPH_1284 /* request remote host attention */ ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN)); DELAY(1); ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN)); #else DELAY(1); #endif /* !PERIPH_1284 */ /* Event 1 - enter IEEE1284 mode */ ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN)); #ifdef PERIPH_1284 /* ignore the PError line, wait a bit more, remote host's * interrupts don't respond fast enough */ if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT, SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) { ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2); error = ENODEV; goto error; } #else /* Event 2 - trying IEEE1284 dialog */ if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT, PERROR | SELECT | nFAULT)) { ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2); error = ENODEV; goto error; } #endif /* !PERIPH_1284 */ /* Event 3 - latch the ext. value to the peripheral */ ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN); DELAY(1); /* Event 4 - IEEE1284 device recognized */ ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE)); /* Event 6 - waiting for status lines */ if (do_1284_wait(bus, nACK, nACK)) { ppb_1284_set_error(bus, PPB_TIMEOUT, 6); error = EBUSY; goto error; } /* Event 7 - quering result consider nACK not to misunderstand * a remote computer terminate sequence */ if (options & PPB_EXTENSIBILITY_LINK) { /* XXX not fully supported yet */ ppb_1284_terminate(bus); return (0); } if (request_mode == NIBBLE_1284_NORMAL) { if (do_1284_wait(bus, nACK | SELECT, nACK)) { ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7); error = ENODEV; goto error; } } else { if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) { ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7); error = ENODEV; goto error; } } switch (mode) { case PPB_NIBBLE: case PPB_PS2: /* enter reverse idle phase */ ppb_1284_set_state(bus, PPB_REVERSE_IDLE); break; case PPB_ECP: /* negociation ok, now setup the communication */ ppb_1284_set_state(bus, PPB_SETUP); ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE)); #ifdef PERIPH_1284 /* ignore PError line */ if (do_1284_wait(bus, nACK | SELECT | nBUSY, nACK | SELECT | nBUSY)) { ppb_1284_set_error(bus, PPB_TIMEOUT, 30); error = ENODEV; goto error; } #else if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY, nACK | SELECT | PERROR | nBUSY)) { ppb_1284_set_error(bus, PPB_TIMEOUT, 30); error = ENODEV; goto error; } #endif /* !PERIPH_1284 */ /* ok, the host enters the ForwardIdle state */ ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE); break; case PPB_EPP: ppb_1284_set_state(bus, PPB_EPP_IDLE); break; default: panic("%s: unknown mode (%d)!", __FUNCTION__, mode); } ppb_set_mode(bus, mode); return (0); error: ppb_1284_terminate(bus); return (error); } /* * ppb_1284_terminate() * * IEEE1284 termination phase, return code should ignored since the host * is _always_ in compatible mode after ppb_1284_terminate() */ int ppb_1284_terminate(device_t bus) { #ifdef DEBUG_1284 printf("T"); #endif /* do not reset error here to keep the error that * may occured before the ppb_1284_terminate() call */ ppb_1284_set_state(bus, PPB_TERMINATION); #ifdef PERIPH_1284 /* request remote host attention */ ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED)); DELAY(1); #endif /* PERIPH_1284 */ /* Event 22 - set nSelectin low and nAutoFeed high */ ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED)); /* Event 24 - waiting for peripheral, Xflag ignored */ if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) { ppb_1284_set_error(bus, PPB_TIMEOUT, 24); goto error; } /* Event 25 - set nAutoFd low */ ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE); /* Event 26 - compatible mode status is set */ /* Event 27 - peripheral set nAck high */ if (do_1284_wait(bus, nACK, nACK)) { ppb_1284_set_error(bus, PPB_TIMEOUT, 27); } /* Event 28 - end termination, return to idle phase */ ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED)); error: /* return to compatible mode */ ppb_set_mode(bus, PPB_COMPATIBLE); ppb_1284_set_state(bus, PPB_FORWARD_IDLE); return (0); }