xref: /freebsd/sys/dev/ppbus/ppb_1284.c (revision fed1ca4b719c56c930f2259d80663cd34be812bb)
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  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * General purpose routines for the IEEE1284-1994 Standard
34  */
35 
36 #include "opt_ppb_1284.h"
37 
38 #include <sys/param.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 
44 
45 #include <dev/ppbus/ppbconf.h>
46 #include <dev/ppbus/ppb_1284.h>
47 
48 #include "ppbus_if.h"
49 
50 #include <dev/ppbus/ppbio.h>
51 
52 #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
53 
54 /*
55  * do_1284_wait()
56  *
57  * Wait for the peripherial up to 40ms
58  */
59 static int
60 do_1284_wait(device_t bus, char mask, char status)
61 {
62 	return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
63 }
64 
65 static int
66 do_peripheral_wait(device_t bus, char mask, char status)
67 {
68 	return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
69 }
70 
71 #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
72 
73 /*
74  * ppb_1284_reset_error()
75  *
76  * Unconditionaly reset the error field
77  */
78 static int
79 ppb_1284_reset_error(device_t bus, int state)
80 {
81 	struct ppb_data *ppb = DEVTOSOFTC(bus);
82 
83 	ppb->error = PPB_NO_ERROR;
84 	ppb->state = state;
85 
86 	return (0);
87 }
88 
89 /*
90  * ppb_1284_get_state()
91  *
92  * Get IEEE1284 state
93  */
94 int
95 ppb_1284_get_state(device_t bus)
96 {
97 	struct ppb_data *ppb = DEVTOSOFTC(bus);
98 
99 	mtx_assert(ppb->ppc_lock, MA_OWNED);
100 	return (ppb->state);
101 }
102 
103 /*
104  * ppb_1284_set_state()
105  *
106  * Change IEEE1284 state if no error occurred
107  */
108 int
109 ppb_1284_set_state(device_t bus, int state)
110 {
111 	struct ppb_data *ppb = DEVTOSOFTC(bus);
112 
113 	/* call ppb_1284_reset_error() if you absolutely 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)) {
118 		ppb->state = state;
119 		ppb->error = PPB_NO_ERROR;
120 	}
121 
122 	return (0);
123 }
124 
125 static int
126 ppb_1284_set_error(device_t bus, int error, int event)
127 {
128 	struct ppb_data *ppb = DEVTOSOFTC(bus);
129 
130 	/* do not accumulate errors */
131 	if ((ppb->error == PPB_NO_ERROR) &&
132 			(ppb->state != PPB_ERROR)) {
133 		ppb->error = error;
134 		ppb->state = PPB_ERROR;
135 	}
136 
137 #ifdef DEBUG_1284
138 	printf("ppb1284: error=%d status=0x%x event=%d\n", error,
139 		ppb_rstr(bus) & 0xff, event);
140 #endif
141 
142 	return (0);
143 }
144 
145 /*
146  * ppb_request_mode()
147  *
148  * Converts mode+options into ext. value
149  */
150 static int
151 ppb_request_mode(int mode, int options)
152 {
153 	int request_mode = 0;
154 
155 	if (options & PPB_EXTENSIBILITY_LINK) {
156 		request_mode = EXT_LINK_1284_NORMAL;
157 
158 	} else {
159 		switch (mode) {
160 		case PPB_NIBBLE:
161 			request_mode = (options & PPB_REQUEST_ID) ?
162 					NIBBLE_1284_REQUEST_ID :
163 					NIBBLE_1284_NORMAL;
164 			break;
165 		case PPB_PS2:
166 			request_mode = (options & PPB_REQUEST_ID) ?
167 					BYTE_1284_REQUEST_ID :
168 					BYTE_1284_NORMAL;
169 			break;
170 		case PPB_ECP:
171 			if (options & PPB_USE_RLE)
172 				request_mode = (options & PPB_REQUEST_ID) ?
173 					ECP_1284_RLE_REQUEST_ID :
174 					ECP_1284_RLE;
175 			else
176 				request_mode = (options & PPB_REQUEST_ID) ?
177 					ECP_1284_REQUEST_ID :
178 					ECP_1284_NORMAL;
179 			break;
180 		case PPB_EPP:
181 			request_mode = EPP_1284_NORMAL;
182 			break;
183 		default:
184 			panic("%s: unsupported mode %d\n", __func__, mode);
185 		}
186 	}
187 
188 	return (request_mode);
189 }
190 
191 /*
192  * ppb_peripheral_negociate()
193  *
194  * Negotiate the peripheral side
195  */
196 int
197 ppb_peripheral_negociate(device_t bus, int mode, int options)
198 {
199 	int spin, request_mode, error = 0;
200 	char r;
201 
202 	ppb_set_mode(bus, PPB_COMPATIBLE);
203 	ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
204 
205 	/* compute ext. value */
206 	request_mode = ppb_request_mode(mode, options);
207 
208 	/* wait host */
209 	spin = 10;
210 	while (spin-- && (ppb_rstr(bus) & nBUSY))
211 		DELAY(1);
212 
213 	/* check termination */
214 	if (!(ppb_rstr(bus) & SELECT) || !spin) {
215 		error = ENODEV;
216 		goto error;
217 	}
218 
219 	/* Event 4 - read ext. value */
220 	r = ppb_rdtr(bus);
221 
222 	/* nibble mode is not supported */
223 	if ((r == (char)request_mode) ||
224 			(r == NIBBLE_1284_NORMAL)) {
225 
226 		/* Event 5 - restore direction bit, no data avail */
227 		ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN));
228 		DELAY(1);
229 
230 		/* Event 6 */
231 		ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE));
232 
233 		if (r == NIBBLE_1284_NORMAL) {
234 #ifdef DEBUG_1284
235 			printf("R");
236 #endif
237 			ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
238 			error = EINVAL;
239 			goto error;
240 		} else {
241 			ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
242 			switch (r) {
243 			case BYTE_1284_NORMAL:
244 				ppb_set_mode(bus, PPB_BYTE);
245 				break;
246 			default:
247 				break;
248 			}
249 #ifdef DEBUG_1284
250 			printf("A");
251 #endif
252 			/* negotiation succeeds */
253 		}
254 	} else {
255 		/* Event 5 - mode not supported */
256 		ppb_wctr(bus, SELECTIN);
257 		DELAY(1);
258 
259 		/* Event 6 */
260 		ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT));
261 		ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
262 
263 #ifdef DEBUG_1284
264 		printf("r");
265 #endif
266 		error = EINVAL;
267 		goto error;
268 	}
269 
270 	return (0);
271 
272 error:
273 	ppb_peripheral_terminate(bus, PPB_WAIT);
274 	return (error);
275 }
276 
277 /*
278  * ppb_peripheral_terminate()
279  *
280  * Terminate peripheral transfer side
281  *
282  * Always return 0 in compatible mode
283  */
284 int
285 ppb_peripheral_terminate(device_t bus, int how)
286 {
287 	int error = 0;
288 
289 #ifdef DEBUG_1284
290 	printf("t");
291 #endif
292 
293 	ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION);
294 
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);
298 		goto error;
299 	}
300 
301 	/* Event 24 */
302 	ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
303 
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);
307 		goto error;
308 	}
309 
310 	/* Event 26 */
311 	ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
312 	DELAY(1);
313 	/* Event 27 */
314 	ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
315 
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);
319 		goto error;
320 	}
321 
322 error:
323 	ppb_set_mode(bus, PPB_COMPATIBLE);
324 	ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
325 
326 	return (0);
327 }
328 
329 /*
330  * byte_peripheral_outbyte()
331  *
332  * Write 1 byte in BYTE mode
333  */
334 static int
335 byte_peripheral_outbyte(device_t bus, char *buffer, int last)
336 {
337 	int error = 0;
338 
339 	/* Event 7 */
340 	if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
341 		ppb_1284_set_error(bus, PPB_TIMEOUT, 7);
342 		goto error;
343 	}
344 
345 	/* check termination */
346 	if (!(ppb_rstr(bus) & SELECT)) {
347 		ppb_peripheral_terminate(bus, PPB_WAIT);
348 		goto error;
349 	}
350 
351 	/* Event 15 - put byte on data lines */
352 #ifdef DEBUG_1284
353 	printf("B");
354 #endif
355 	ppb_wdtr(bus, *buffer);
356 
357 	/* Event 9 */
358 	ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
359 
360 	/* Event 10 - wait data read */
361 	if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
362 		ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
363 		goto error;
364 	}
365 
366 	/* Event 11 */
367 	if (!last) {
368 		ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
369 	} else {
370 		ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
371 	}
372 
373 #if 0
374 	/* Event 16 - wait strobe */
375 	if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
376 		ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
377 		goto error;
378 	}
379 #endif
380 
381 	/* check termination */
382 	if (!(ppb_rstr(bus) & SELECT)) {
383 		ppb_peripheral_terminate(bus, PPB_WAIT);
384 		goto error;
385 	}
386 
387 error:
388 	return (error);
389 }
390 
391 /*
392  * byte_peripheral_write()
393  *
394  * Write n bytes in BYTE mode
395  */
396 int
397 byte_peripheral_write(device_t bus, char *buffer, int len, int *sent)
398 {
399 	int error = 0, i;
400 	char r;
401 
402 	ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER);
403 
404 	/* wait forever, the remote host is master and should initiate
405 	 * termination
406 	 */
407 	for (i=0; i<len; i++) {
408 		/* force remote nFAULT low to release the remote waiting
409 		 * process, if any
410 		 */
411 		r = ppb_rctr(bus);
412 		ppb_wctr(bus, r & ~nINIT);
413 
414 #ifdef DEBUG_1284
415 		printf("y");
416 #endif
417 		/* Event 7 */
418 		error = ppb_poll_bus(bus, PPB_FOREVER, nBUSY, nBUSY,
419 					PPB_INTR);
420 
421 		if (error && error != EWOULDBLOCK)
422 			goto error;
423 
424 #ifdef DEBUG_1284
425 		printf("b");
426 #endif
427 		if ((error = byte_peripheral_outbyte(bus, buffer+i, (i == len-1))))
428 			goto error;
429 	}
430 error:
431 	if (!error)
432 		ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
433 
434 	*sent = i;
435 	return (error);
436 }
437 
438 /*
439  * byte_1284_inbyte()
440  *
441  * Read 1 byte in BYTE mode
442  */
443 int
444 byte_1284_inbyte(device_t bus, char *buffer)
445 {
446 	int error = 0;
447 
448 	/* Event 7 - ready to take data (nAUTO low) */
449 	ppb_wctr(bus, (PCD | nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
450 
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);
454 		goto error;
455 	}
456 
457 	/* read the byte */
458 	*buffer = ppb_rdtr(bus);
459 
460 	/* Event 10 - data received, can't accept more */
461 	ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
462 
463 	/* Event 11 - peripheral ack */
464 	if ((error = do_1284_wait(bus, nACK, nACK))) {
465 		ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
466 		goto error;
467 	}
468 
469 	/* Event 16 - strobe */
470 	ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
471 	DELAY(3);
472 	ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
473 
474 error:
475 	return (error);
476 }
477 
478 /*
479  * nibble_1284_inbyte()
480  *
481  * Read 1 byte in NIBBLE mode
482  */
483 int
484 nibble_1284_inbyte(device_t bus, char *buffer)
485 {
486 	char nibble[2];
487 	int i, error;
488 
489 	for (i = 0; i < 2; i++) {
490 
491 		/* Event 7 - ready to take data (nAUTO low) */
492 		ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
493 
494 		/* Event 8 - peripheral writes the first nibble */
495 
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);
499 			goto error;
500 		}
501 
502 		/* read nibble */
503 		nibble[i] = ppb_rstr(bus);
504 
505 		/* Event 10 - ack, nibble received */
506 		ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
507 
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);
511 			goto error;
512 		}
513 	}
514 
515 	*buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
516 				(nibble2char(nibble[0]) & 0x0f);
517 
518 error:
519 	return (error);
520 }
521 
522 /*
523  * spp_1284_read()
524  *
525  * Read in IEEE1284 NIBBLE/BYTE mode
526  */
527 int
528 spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
529 {
530 	int error = 0, len = 0;
531 	int terminate_after_transfer = 1;
532 	int state;
533 
534 	*read = len = 0;
535 
536 	state = ppb_1284_get_state(bus);
537 
538 	switch (state) {
539 	case PPB_FORWARD_IDLE:
540 		if ((error = ppb_1284_negociate(bus, mode, 0)))
541 			return (error);
542 		break;
543 
544 	case PPB_REVERSE_IDLE:
545 		terminate_after_transfer = 0;
546 		break;
547 
548 	default:
549 		ppb_1284_terminate(bus);
550 		if ((error = ppb_1284_negociate(bus, mode, 0)))
551 			return (error);
552 		break;
553 	}
554 
555 	while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
556 
557 		ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
558 
559 #ifdef DEBUG_1284
560 		printf("B");
561 #endif
562 
563 		switch (mode) {
564 		case PPB_NIBBLE:
565 			/* read a byte, error means no more data */
566 			if (nibble_1284_inbyte(bus, buffer+len))
567 				goto end_while;
568 			break;
569 		case PPB_BYTE:
570 			if (byte_1284_inbyte(bus, buffer+len))
571 				goto end_while;
572 			break;
573 		default:
574 			error = EINVAL;
575 			goto end_while;
576 		}
577 		len ++;
578 	}
579 end_while:
580 
581 	if (!error)
582 		ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
583 
584 	*read = len;
585 
586 	if (terminate_after_transfer || error)
587 		ppb_1284_terminate(bus);
588 
589 	return (error);
590 }
591 
592 /*
593  * ppb_1284_read_id()
594  *
595  */
596 int
597 ppb_1284_read_id(device_t bus, int mode, char *buffer,
598 		int max, int *read)
599 {
600 	int error = 0;
601 
602 	/* fill the buffer with 0s */
603 	bzero(buffer, max);
604 
605 	switch (mode) {
606 	case PPB_NIBBLE:
607 	case PPB_ECP:
608 		if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
609 			return (error);
610 		error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
611 		break;
612 	case PPB_BYTE:
613 		if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
614 			return (error);
615 		error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
616 		break;
617 	default:
618 		panic("%s: unsupported mode %d\n", __func__, mode);
619 	}
620 
621 	ppb_1284_terminate(bus);
622 	return (error);
623 }
624 
625 /*
626  * ppb_1284_read()
627  *
628  * IEEE1284 read
629  */
630 int
631 ppb_1284_read(device_t bus, int mode, char *buffer,
632 		int max, int *read)
633 {
634 	int error = 0;
635 
636 	switch (mode) {
637 	case PPB_NIBBLE:
638 	case PPB_BYTE:
639 		error = spp_1284_read(bus, mode, buffer, max, read);
640 		break;
641 	default:
642 		return (EINVAL);
643 	}
644 
645 	return (error);
646 }
647 
648 /*
649  * ppb_1284_negociate()
650  *
651  * IEEE1284 negotiation phase
652  *
653  * Normal nibble mode or request device id mode (see ppb_1284.h)
654  *
655  * After negotiation, nFAULT is low if data is available
656  */
657 int
658 ppb_1284_negociate(device_t bus, int mode, int options)
659 {
660 	int error;
661 	int request_mode;
662 
663 #ifdef DEBUG_1284
664 	printf("n");
665 #endif
666 
667 	if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
668 		ppb_peripheral_terminate(bus, PPB_WAIT);
669 
670 	if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
671 		ppb_1284_terminate(bus);
672 
673 #ifdef DEBUG_1284
674 	printf("%d", mode);
675 #endif
676 
677 	/* ensure the host is in compatible mode */
678 	ppb_set_mode(bus, PPB_COMPATIBLE);
679 
680 	/* reset error to catch the actual negotiation error */
681 	ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
682 
683 	/* calculate ext. value */
684 	request_mode = ppb_request_mode(mode, options);
685 
686 	/* default state */
687 	ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
688 	DELAY(1);
689 
690 	/* enter negotiation phase */
691 	ppb_1284_set_state(bus, PPB_NEGOCIATION);
692 
693 	/* Event 0 - put the exten. value on the data lines */
694 	ppb_wdtr(bus, request_mode);
695 
696 #ifdef PERIPH_1284
697 	/* request remote host attention */
698 	ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
699 	DELAY(1);
700 	ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
701 #else
702 	DELAY(1);
703 
704 #endif /* !PERIPH_1284 */
705 
706 	/* Event 1 - enter IEEE1284 mode */
707 	ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
708 
709 #ifdef PERIPH_1284
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);
715 		error = ENODEV;
716 		goto error;
717 	}
718 #else
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);
723 		error = ENODEV;
724 		goto error;
725 	}
726 #endif /* !PERIPH_1284 */
727 
728 	/* Event 3 - latch the ext. value to the peripheral */
729 	ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
730 	DELAY(1);
731 
732 	/* Event 4 - IEEE1284 device recognized */
733 	ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
734 
735 	/* Event 6 - waiting for status lines */
736 	if (do_1284_wait(bus, nACK, nACK)) {
737 		ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
738 		error = EBUSY;
739 		goto error;
740 	}
741 
742 	/* Event 7 - quering result consider nACK not to misunderstand
743 	 * a remote computer terminate sequence */
744 	if (options & PPB_EXTENSIBILITY_LINK) {
745 
746 		/* XXX not fully supported yet */
747 		ppb_1284_terminate(bus);
748 		return (0);
749 
750 	}
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);
754 			error = ENODEV;
755 			goto error;
756 		}
757 	} else {
758 		if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
759 			ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
760 			error = ENODEV;
761 			goto error;
762 		}
763 	}
764 
765 	switch (mode) {
766 	case PPB_NIBBLE:
767 	case PPB_PS2:
768 		/* enter reverse idle phase */
769 		ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
770 		break;
771 	case PPB_ECP:
772 		/* negotiation ok, now setup the communication */
773 		ppb_1284_set_state(bus, PPB_SETUP);
774 		ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
775 
776 #ifdef PERIPH_1284
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);
781 			error = ENODEV;
782 			goto error;
783 		}
784 #else
785 		if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
786 					nACK | SELECT | PERROR | nBUSY)) {
787 			ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
788 			error = ENODEV;
789 			goto error;
790 		}
791 #endif /* !PERIPH_1284 */
792 
793 		/* ok, the host enters the ForwardIdle state */
794 		ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
795 		break;
796 	case PPB_EPP:
797 		ppb_1284_set_state(bus, PPB_EPP_IDLE);
798 		break;
799 
800 	default:
801 		panic("%s: unknown mode (%d)!", __func__, mode);
802 	}
803 	ppb_set_mode(bus, mode);
804 
805 	return (0);
806 
807 error:
808 	ppb_1284_terminate(bus);
809 
810 	return (error);
811 }
812 
813 /*
814  * ppb_1284_terminate()
815  *
816  * IEEE1284 termination phase, return code should ignored since the host
817  * is _always_ in compatible mode after ppb_1284_terminate()
818  */
819 int
820 ppb_1284_terminate(device_t bus)
821 {
822 
823 #ifdef DEBUG_1284
824 	printf("T");
825 #endif
826 
827 	/* do not reset error here to keep the error that
828 	 * may occurred before the ppb_1284_terminate() call */
829 	ppb_1284_set_state(bus, PPB_TERMINATION);
830 
831 #ifdef PERIPH_1284
832 	/* request remote host attention */
833 	ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
834 	DELAY(1);
835 #endif /* PERIPH_1284 */
836 
837 	/* Event 22 - set nSelectin low and nAutoFeed high */
838 	ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
839 
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);
843 		goto error;
844 	}
845 
846 	/* Event 25 - set nAutoFd low */
847 	ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
848 
849 	/* Event 26 - compatible mode status is set */
850 
851 	/* Event 27 - peripheral set nAck high */
852 	if (do_1284_wait(bus, nACK, nACK)) {
853 		ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
854 	}
855 
856 	/* Event 28 - end termination, return to idle phase */
857 	ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
858 
859 error:
860 	/* return to compatible mode */
861 	ppb_set_mode(bus, PPB_COMPATIBLE);
862 	ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
863 
864 	return (0);
865 }
866