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