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