Home | History | Annotate | Line # | Download | only in ppbus
ppbus_1284.c revision 1.3
      1 /* $NetBSD: ppbus_1284.c,v 1.3 2004/01/22 01:17:03 bjh21 Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1997 Nicolas Souchu
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     15  *
     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
     26  * SUCH DAMAGE.
     27  *
     28  * $FreeBSD: src/sys/dev/ppbus/ppb_1284.c,v 1.11 2000/01/14 08:03:14 nsouch Exp $
     29  *
     30  */
     31 
     32 /* General purpose routines for the IEEE1284-1994 Standard */
     33 
     34 #include "opt_ppbus_1284.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/malloc.h>
     38 #include <sys/systm.h>
     39 
     40 #include <dev/ppbus/ppbus_conf.h>
     41 #include <dev/ppbus/ppbus_base.h>
     42 #include <dev/ppbus/ppbus_1284.h>
     43 #include <dev/ppbus/ppbus_io.h>
     44 #include <dev/ppbus/ppbus_var.h>
     45 
     46 
     47 /* Wait for the peripherial up to 40ms */
     48 static int
     49 do_1284_wait(struct ppbus_softc * bus, char mask, char status)
     50 {
     51 	return (ppbus_poll_bus(&(bus->sc_dev), 4, mask, status,
     52 		PPBUS_NOINTR | PPBUS_POLL));
     53 }
     54 
     55 /* Wait for the host up to 1 second (peripheral side) */
     56 static int
     57 do_peripheral_wait(struct ppbus_softc * bus, char mask, char status)
     58 {
     59 	return (ppbus_poll_bus(&(bus->sc_dev), 100, mask, status,
     60 		PPBUS_NOINTR | PPBUS_POLL));
     61 }
     62 
     63 
     64 /* Unconditionaly reset the error field */
     65 static int
     66 ppbus_1284_reset_error(struct ppbus_softc * bus, int state)
     67 {
     68 	bus->sc_1284_error = PPBUS_NO_ERROR;
     69 	bus->sc_1284_state = state;
     70 	return 0;
     71 }
     72 
     73 
     74 /* Get IEEE1284 state */
     75 int
     76 ppbus_1284_get_state(struct device * dev)
     77 {
     78 	return (((struct ppbus_softc *)dev)->sc_1284_state);
     79 }
     80 
     81 
     82 /* Set IEEE1284 state if no error occured */
     83 int
     84 ppbus_1284_set_state(struct device * dev, int state)
     85 {
     86 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
     87 
     88 	/* call ppbus_1284_reset_error() if you absolutly want to change
     89 	 * the state from PPBUS_ERROR to another */
     90 	if ((bus->sc_1284_state != PPBUS_ERROR) &&
     91 			(bus->sc_1284_error == PPBUS_NO_ERROR)) {
     92 		bus->sc_1284_state = state;
     93 		bus->sc_1284_error = PPBUS_NO_ERROR;
     94 	}
     95 
     96 	return 0;
     97 }
     98 
     99 
    100 /* Set the IEEE1284 error field */
    101 static int
    102 ppbus_1284_set_error(struct ppbus_softc * bus, int error, int event)
    103 {
    104 	/* do not accumulate errors */
    105 	if ((bus->sc_1284_error == PPBUS_NO_ERROR) &&
    106 			(bus->sc_1284_state != PPBUS_ERROR)) {
    107 		bus->sc_1284_error = error;
    108 		bus->sc_1284_state = PPBUS_ERROR;
    109 	}
    110 
    111 #ifdef DEBUG_1284
    112 	printf("%s<1284>: error=%d status=0x%x event=%d\n",
    113 		bus->sc_dev.dv_xname, error, ppbus_rstr((struct device *)bus),
    114 		event);
    115 
    116 #endif
    117 
    118 	return 0;
    119 }
    120 
    121 
    122 /* Converts mode+options into ext. value */
    123 static int
    124 ppbus_request_mode(int mode, int options)
    125 {
    126 	int request_mode = 0;
    127 
    128 	if (options & PPBUS_EXTENSIBILITY_LINK) {
    129 		request_mode = EXT_LINK_1284_NORMAL;
    130 
    131 	}
    132 	else {
    133 		switch (mode) {
    134 		case PPBUS_NIBBLE:
    135 			request_mode = (options & PPBUS_REQUEST_ID) ?
    136 					NIBBLE_1284_REQUEST_ID :
    137 					NIBBLE_1284_NORMAL;
    138 			break;
    139 		case PPBUS_PS2:
    140 			request_mode = (options & PPBUS_REQUEST_ID) ?
    141 					BYTE_1284_REQUEST_ID :
    142 					BYTE_1284_NORMAL;
    143 			break;
    144 		case PPBUS_ECP:
    145 			if (options & PPBUS_USE_RLE)
    146 				request_mode = (options & PPBUS_REQUEST_ID) ?
    147 					ECP_1284_RLE_REQUEST_ID :
    148 					ECP_1284_RLE;
    149 			else
    150 				request_mode = (options & PPBUS_REQUEST_ID) ?
    151 					ECP_1284_REQUEST_ID :
    152 					ECP_1284_NORMAL;
    153 			break;
    154 		case PPBUS_EPP:
    155 			request_mode = EPP_1284_NORMAL;
    156 			break;
    157 		default:
    158 			panic("%s: unsupported mode %d\n", __FUNCTION__, mode);
    159 		}
    160 	}
    161 
    162 	return (request_mode);
    163 }
    164 
    165 
    166 /* Negociate the peripheral side */
    167 int
    168 ppbus_peripheral_negociate(struct device * dev, int mode, int options)
    169 {
    170 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
    171 	int spin, request_mode, error = 0;
    172 	char r;
    173 
    174 	ppbus_1284_terminate(dev);
    175 	ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_NEGOCIATION);
    176 
    177 	/* compute ext. value */
    178 	request_mode = ppbus_request_mode(mode, options);
    179 
    180 	/* wait host */
    181 	spin = 10;
    182 	while (spin-- && (ppbus_rstr(dev) & nBUSY))
    183 		DELAY(1);
    184 
    185 	/* check termination */
    186 	if (!(ppbus_rstr(dev) & SELECT) || !spin) {
    187 		error = ENODEV;
    188 		goto error;
    189 	}
    190 
    191 	/* Event 4 - read ext. value */
    192 	r = ppbus_rdtr(dev);
    193 
    194 	/* nibble mode is not supported */
    195 	if ((r == (char)request_mode) ||
    196 			(r == NIBBLE_1284_NORMAL)) {
    197 
    198 		/* Event 5 - restore direction bit, no data avail */
    199 		ppbus_wctr(dev, (STROBE | nINIT) & ~(SELECTIN));
    200 		DELAY(1);
    201 
    202 		/* Event 6 */
    203 		ppbus_wctr(dev, (nINIT) & ~(SELECTIN | STROBE));
    204 
    205 		if (r == NIBBLE_1284_NORMAL) {
    206 #ifdef DEBUG_1284
    207 			printf("R");
    208 #endif
    209 			ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 4);
    210 			error = EINVAL;
    211 			goto error;
    212 		}
    213 		else {
    214 			ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_IDLE);
    215 #ifdef DEBUG_1284
    216 			printf("A");
    217 #endif
    218 			/* negociation succeeds */
    219 		}
    220 	}
    221 	else {
    222 		/* Event 5 - mode not supported */
    223 		ppbus_wctr(dev, SELECTIN);
    224 		DELAY(1);
    225 
    226 		/* Event 6 */
    227 		ppbus_wctr(dev, (SELECTIN) & ~(STROBE | nINIT));
    228 		ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 4);
    229 
    230 #ifdef DEBUG_1284
    231 		printf("r");
    232 #endif
    233 		error = EINVAL;
    234 		goto error;
    235 	}
    236 
    237 	return (0);
    238 
    239 error:
    240 	ppbus_peripheral_terminate(dev, PPBUS_WAIT);
    241 	return (error);
    242 }
    243 
    244 
    245 /* Terminate peripheral transfer side. Always return 0 in compatible mode */
    246 int
    247 ppbus_peripheral_terminate(struct device * dev, int how)
    248 {
    249 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
    250 	int error = 0;
    251 
    252 #ifdef DEBUG_1284
    253 	printf("t");
    254 #endif
    255 
    256 	ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_TERMINATION);
    257 
    258 	/* Event 22 - wait up to host response time (1s) */
    259 	if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
    260 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 22);
    261 		goto error;
    262 	}
    263 
    264 	/* Event 24 */
    265         ppbus_wctr(dev, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
    266 
    267 	/* Event 25 - wait up to host response time (1s) */
    268 	if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
    269 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 25);
    270 		goto error;
    271 	}
    272 
    273 	/* Event 26 */
    274         ppbus_wctr(dev, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
    275 	DELAY(1);
    276 	/* Event 27 */
    277         ppbus_wctr(dev, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
    278 
    279 	/* Event 28 - wait up to host response time (1s) */
    280 	if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
    281 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 28);
    282 		goto error;
    283 	}
    284 
    285 error:
    286 	ppbus_1284_terminate(dev);
    287 	ppbus_1284_set_state(dev, PPBUS_FORWARD_IDLE);
    288 
    289 	return (0);
    290 }
    291 
    292 
    293 /* Write 1 byte to host in BYTE mode (peripheral side) */
    294 static int
    295 byte_peripheral_outbyte(struct device * dev, char *buffer, int last)
    296 {
    297 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
    298 	int error = 0;
    299 
    300 	/* Event 7 */
    301 	if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
    302 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 7);
    303 		goto error;
    304 	}
    305 
    306 	/* check termination */
    307 	if (!(ppbus_rstr(dev) & SELECT)) {
    308 		ppbus_peripheral_terminate(dev, PPBUS_WAIT);
    309 		goto error;
    310 	}
    311 
    312 	/* Event 15 - put byte on data lines */
    313 #ifdef DEBUG_1284
    314 	printf("B");
    315 #endif
    316 	ppbus_wdtr(dev, *buffer);
    317 
    318 	/* Event 9 */
    319 	ppbus_wctr(dev, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
    320 
    321 	/* Event 10 - wait data read */
    322 	if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
    323 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 16);
    324 		goto error;
    325 	}
    326 
    327 	/* Event 11 */
    328 	if (!last) {
    329 		ppbus_wctr(dev, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
    330 	} else {
    331 		ppbus_wctr(dev, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
    332 	}
    333 
    334 #if 0
    335 	/* Event 16 - wait strobe */
    336 	if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
    337 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 16);
    338 		goto error;
    339 	}
    340 #endif
    341 
    342 	/* check termination */
    343 	if (!(ppbus_rstr(dev) & SELECT)) {
    344 		ppbus_peripheral_terminate(dev, PPBUS_WAIT);
    345 		goto error;
    346 	}
    347 
    348 error:
    349 	return (error);
    350 }
    351 
    352 
    353 /* Write n bytes to host in BYTE mode (peripheral side) */
    354 int
    355 byte_peripheral_write(struct device * dev, char *buffer, int len,
    356 	int *sent)
    357 {
    358 	int error = 0, i;
    359 	char r;
    360 
    361 	ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_TRANSFER);
    362 
    363 	/* wait forever, the remote host is master and should initiate
    364 	 * termination
    365 	 */
    366 	for(i = 0; i < len; i++) {
    367 		/* force remote nFAULT low to release the remote waiting
    368 		 * process, if any
    369 		 */
    370 		r = ppbus_rctr(dev);
    371 		ppbus_wctr(dev, r & ~nINIT);
    372 
    373 #ifdef DEBUG_1284
    374 		printf("y");
    375 #endif
    376 		/* Event 7 */
    377 		error = ppbus_poll_bus(dev, PPBUS_FOREVER, nBUSY, nBUSY,
    378 					PPBUS_INTR);
    379 
    380 		if (error && error != EWOULDBLOCK)
    381 			goto error;
    382 
    383 #ifdef DEBUG_1284
    384 		printf("b");
    385 #endif
    386 		if ((error = byte_peripheral_outbyte(dev, buffer+i, (i == len-1))))
    387 			goto error;
    388 	}
    389 error:
    390 	if (!error)
    391 		ppbus_1284_set_state(dev, PPBUS_PERIPHERAL_IDLE);
    392 
    393 	*sent = i;
    394 	return (error);
    395 }
    396 
    397 
    398 /* Read the device ID using the specified mode */
    399 int
    400 ppbus_1284_read_id(struct device * dev, int mode, char ** buffer,
    401 		size_t * size, size_t * read)
    402 {
    403 	u_int16_t msg_sz;
    404 	u_int8_t length_field;
    405 	u_int8_t old_mode;
    406 	int error;
    407 	int old_ivar;
    408 	int new_ivar = 1;
    409 
    410 	error = ppbus_read_ivar(dev, PPBUS_IVAR_IEEE, &old_ivar);
    411 	if(error) {
    412 		printf("%s(%s): error reading PPBUS_IVAR_IEEE.\n", __func__,
    413 			dev->dv_xname);
    414 		return error;
    415 	}
    416 	if(old_ivar == 0) {
    417 		error = ppbus_write_ivar(dev, PPBUS_IVAR_IEEE, &new_ivar);
    418 		if(error) {
    419 			printf("%s(%s): error enabling IEEE usage.\n", __func__,
    420 				dev->dv_xname);
    421 			return error;
    422 		}
    423 	}
    424 
    425 	old_mode = ppbus_get_mode(dev);
    426 	switch (mode) {
    427 	case PPBUS_NIBBLE:
    428 	case PPBUS_ECP:
    429 	case PPBUS_BYTE:
    430 		error = ppbus_set_mode(dev, mode, PPBUS_REQUEST_ID);
    431 		if(error) {
    432 			goto end_read_id;
    433 			printf("%s(%s): error setting bus mode.\n", __func__,
    434 				dev->dv_xname);
    435 		}
    436 		break;
    437 	default:
    438 		printf("%s(%s): mode does not support returning device ID.\n",
    439 			__func__, dev->dv_xname);
    440 		error = ENODEV;
    441 		goto end_read_id;
    442 	}
    443 
    444 	error = ppbus_read(dev, &length_field, 1, 0, read);
    445 	if(error) {
    446 		printf("%s(%s): error reading first byte.\n", __func__,
    447 			dev->dv_xname);
    448 		goto end_read_id;
    449 	}
    450 	msg_sz = length_field;
    451 	error = ppbus_read(dev, &length_field, 1, 0, read);
    452 	if(error) {
    453 		printf("%s(%s): error reading second byte.\n",
    454 			__func__, dev->dv_xname);
    455 		goto end_read_id;
    456 	}
    457 	msg_sz <<= 8;
    458 	msg_sz |= length_field;
    459 	msg_sz -= 2;
    460 	if(msg_sz <= 0) {
    461 		printf("%s(%s): device ID length <= 0.\n", __func__,
    462 			dev->dv_xname);
    463 		goto end_read_id;
    464 	}
    465 	*buffer = malloc(msg_sz, M_DEVBUF, M_WAITOK);
    466 	*size = msg_sz;
    467 	error = ppbus_read(dev, *buffer, msg_sz, 0, read);
    468 
    469 end_read_id:
    470 	ppbus_set_mode(dev, old_mode, 0);
    471 	if(old_ivar == 0) {
    472 		if(ppbus_write_ivar(dev, PPBUS_IVAR_IEEE, &old_ivar)) {
    473 			printf("%s(%s): error restoring PPBUS_IVAR_IEEE.\n",
    474 				__func__, dev->dv_xname);
    475 		}
    476 	}
    477 	return (error);
    478 }
    479 
    480 /*
    481  * IEEE1284 negociation phase: after negociation, nFAULT is low if data is
    482  * available for reverse modes.
    483  */
    484 int
    485 ppbus_1284_negociate(struct device * dev, int mode, int options)
    486 {
    487 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
    488 	int error;
    489 	int request_mode;
    490 
    491 #ifdef DEBUG_1284
    492 	printf("n");
    493 #endif
    494 
    495 	if (ppbus_1284_get_state(dev) >= PPBUS_PERIPHERAL_NEGOCIATION)
    496 		ppbus_peripheral_terminate(dev, PPBUS_WAIT);
    497 
    498 #ifdef DEBUG_1284
    499 	printf("%d", mode);
    500 #endif
    501 
    502 	/* ensure the host is in compatible mode */
    503 	ppbus_1284_terminate(dev);
    504 
    505 	/* reset error to catch the actual negociation error */
    506 	ppbus_1284_reset_error(bus, PPBUS_FORWARD_IDLE);
    507 
    508 	/* calculate ext. value */
    509 	request_mode = ppbus_request_mode(mode, options);
    510 
    511 	/* default state */
    512 	ppbus_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
    513 	DELAY(1);
    514 
    515 	/* enter negociation phase */
    516 	ppbus_1284_set_state(dev, PPBUS_NEGOCIATION);
    517 
    518 	/* Event 0 - put the exten. value on the data lines */
    519 	ppbus_wdtr(dev, request_mode);
    520 
    521 #ifdef PERIPH_1284
    522 	/* request remote host attention */
    523         ppbus_wctr(dev, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
    524         DELAY(1);
    525         ppbus_wctr(dev, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
    526 #else
    527 	DELAY(1);
    528 
    529 #endif /* !PERIPH_1284 */
    530 
    531 	/* Event 1 - enter IEEE1284 mode */
    532 	ppbus_wctr(dev, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
    533 
    534 #ifdef PERIPH_1284
    535 	/* ignore the PError line, wait a bit more, remote host's
    536 	 * interrupts don't respond fast enough */
    537 	if (ppbus_poll_bus(bus, 40, nACK | SELECT | nFAULT,
    538 				SELECT | nFAULT, PPBUS_NOINTR | PPBUS_POLL)) {
    539                 ppbus_1284_set_error(bus, PPBUS_NOT_IEEE1284, 2);
    540                 error = ENODEV;
    541                 goto error;
    542         }
    543 #else
    544 	/* Event 2 - trying IEEE1284 dialog */
    545 	if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
    546 			PERROR  | SELECT | nFAULT)) {
    547 		ppbus_1284_set_error(bus, PPBUS_NOT_IEEE1284, 2);
    548 		error = ENODEV;
    549 		goto error;
    550 	}
    551 #endif /* !PERIPH_1284 */
    552 
    553 	/* Event 3 - latch the ext. value to the peripheral */
    554 	ppbus_wctr(dev, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
    555 	DELAY(1);
    556 
    557 	/* Event 4 - IEEE1284 device recognized */
    558 	ppbus_wctr(dev, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
    559 
    560 	/* Event 6 - waiting for status lines */
    561 	if (do_1284_wait(bus, nACK, nACK)) {
    562 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 6);
    563 		error = EBUSY;
    564 		goto error;
    565 	}
    566 
    567 	/* Event 7 - quering result consider nACK not to misunderstand
    568 	 * a remote computer terminate sequence */
    569 	if (options & PPBUS_EXTENSIBILITY_LINK) {
    570 		/* XXX not fully supported yet */
    571 		ppbus_1284_terminate(dev);
    572 		error = ENODEV;
    573 		goto error;
    574 		/* return (0); */
    575 	}
    576 	if (request_mode == NIBBLE_1284_NORMAL) {
    577 		if (do_1284_wait(bus, nACK | SELECT, nACK)) {
    578 			ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 7);
    579 			error = ENODEV;
    580 			goto error;
    581 		}
    582 	} else {
    583 		if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
    584 			ppbus_1284_set_error(bus, PPBUS_MODE_UNSUPPORTED, 7);
    585 			error = ENODEV;
    586 			goto error;
    587 		}
    588 	}
    589 
    590 	switch (mode) {
    591 	case PPBUS_NIBBLE:
    592 	case PPBUS_PS2:
    593 		/* enter reverse idle phase */
    594 		ppbus_1284_set_state(dev, PPBUS_REVERSE_IDLE);
    595 		break;
    596 	case PPBUS_ECP:
    597 		/* negociation ok, now setup the communication */
    598 		ppbus_1284_set_state(dev, PPBUS_SETUP);
    599 		ppbus_wctr(dev, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
    600 
    601 #ifdef PERIPH_1284
    602 		/* ignore PError line */
    603 		if (do_1284_wait(bus, nACK | SELECT | nBUSY,
    604                                         nACK | SELECT | nBUSY)) {
    605                         ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 30);
    606                         error = ENODEV;
    607                         goto error;
    608                 }
    609 #else
    610 		if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
    611 					nACK | SELECT | PERROR | nBUSY)) {
    612 			ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 30);
    613 			error = ENODEV;
    614 			goto error;
    615 		}
    616 #endif /* !PERIPH_1284 */
    617 
    618 		/* ok, the host enters the ForwardIdle state */
    619 		ppbus_1284_set_state(dev, PPBUS_ECP_FORWARD_IDLE);
    620 		break;
    621 	case PPBUS_EPP:
    622 		ppbus_1284_set_state(dev, PPBUS_EPP_IDLE);
    623 		break;
    624 	default:
    625 		panic("%s: unknown mode (%d)!", __func__, mode);
    626 	}
    627 
    628 	return 0;
    629 
    630 error:
    631 	ppbus_1284_terminate(dev);
    632 	return error;
    633 }
    634 
    635 /*
    636  * IEEE1284 termination phase, return code should ignored since the host
    637  * is _always_ in compatible mode after ppbus_1284_terminate()
    638  */
    639 int
    640 ppbus_1284_terminate(struct device * dev)
    641 {
    642 	struct ppbus_softc * bus = (struct ppbus_softc *) dev;
    643 
    644 #ifdef DEBUG_1284
    645 	printf("T");
    646 #endif
    647 
    648 	/* do not reset error here to keep the error that
    649 	 * may occured before the ppbus_1284_terminate() call */
    650 	ppbus_1284_set_state(dev, PPBUS_TERMINATION);
    651 
    652 #ifdef PERIPH_1284
    653 	/* request remote host attention */
    654         ppbus_wctr(dev, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
    655         DELAY(1);
    656 #endif /* PERIPH_1284 */
    657 
    658 	/* Event 22 - set nSelectin low and nAutoFeed high */
    659 	ppbus_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
    660 
    661 	/* Event 24 - waiting for peripheral, Xflag ignored */
    662 	if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
    663 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 24);
    664 		goto error;
    665 	}
    666 
    667 	/* Event 25 - set nAutoFd low */
    668 	ppbus_wctr(dev, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
    669 
    670 	/* Event 26 - compatible mode status is set */
    671 
    672 	/* Event 27 - peripheral set nAck high */
    673 	if (do_1284_wait(bus, nACK, nACK)) {
    674 		ppbus_1284_set_error(bus, PPBUS_TIMEOUT, 27);
    675 	}
    676 
    677 	/* Event 28 - end termination, return to idle phase */
    678 	ppbus_wctr(dev, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
    679 
    680 error:
    681 	ppbus_1284_set_state(dev, PPBUS_FORWARD_IDLE);
    682 
    683 	return (0);
    684 }
    685