xref: /freebsd/sys/dev/ppbus/ppb_1284.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1e51b0386SMike Smith /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4e51b0386SMike Smith  * Copyright (c) 1997 Nicolas Souchu
5e51b0386SMike Smith  * All rights reserved.
6e51b0386SMike Smith  *
7e51b0386SMike Smith  * Redistribution and use in source and binary forms, with or without
8e51b0386SMike Smith  * modification, are permitted provided that the following conditions
9e51b0386SMike Smith  * are met:
10e51b0386SMike Smith  * 1. Redistributions of source code must retain the above copyright
11e51b0386SMike Smith  *    notice, this list of conditions and the following disclaimer.
12e51b0386SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13e51b0386SMike Smith  *    notice, this list of conditions and the following disclaimer in the
14e51b0386SMike Smith  *    documentation and/or other materials provided with the distribution.
15e51b0386SMike Smith  *
16e51b0386SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17e51b0386SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18e51b0386SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19e51b0386SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20e51b0386SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21e51b0386SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22e51b0386SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23e51b0386SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24e51b0386SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25e51b0386SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26e51b0386SMike Smith  * SUCH DAMAGE.
27e51b0386SMike Smith  *
28e51b0386SMike Smith  *
29e51b0386SMike Smith  */
303f2076daSEivind Eklund 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32bc35c174SNicolas Souchu /*
33bc35c174SNicolas Souchu  * General purpose routines for the IEEE1284-1994 Standard
34bc35c174SNicolas Souchu  */
35bc35c174SNicolas Souchu 
36bc35c174SNicolas Souchu #include "opt_ppb_1284.h"
373f2076daSEivind Eklund 
38e51b0386SMike Smith #include <sys/param.h>
392067d312SJohn Baldwin #include <sys/lock.h>
402067d312SJohn Baldwin #include <sys/mutex.h>
41e51b0386SMike Smith #include <sys/systm.h>
420f210c92SNicolas Souchu #include <sys/bus.h>
43e51b0386SMike Smith 
44e51b0386SMike Smith #include <dev/ppbus/ppbconf.h>
45e51b0386SMike Smith #include <dev/ppbus/ppb_1284.h>
46e51b0386SMike Smith 
470f210c92SNicolas Souchu #include "ppbus_if.h"
480f210c92SNicolas Souchu 
490f210c92SNicolas Souchu #include <dev/ppbus/ppbio.h>
500f210c92SNicolas Souchu 
510f210c92SNicolas Souchu #define DEVTOSOFTC(dev) ((struct ppb_data *)device_get_softc(dev))
520f210c92SNicolas Souchu 
53e51b0386SMike Smith /*
5446f3ff79SMike Smith  * do_1284_wait()
55e51b0386SMike Smith  *
56e51b0386SMike Smith  * Wait for the peripherial up to 40ms
57e51b0386SMike Smith  */
58bc35c174SNicolas Souchu static int
do_1284_wait(device_t bus,uint8_t mask,uint8_t status)593128fa9aSDimitry Andric do_1284_wait(device_t bus, uint8_t mask, uint8_t status)
60e51b0386SMike Smith {
610f210c92SNicolas Souchu 	return (ppb_poll_bus(bus, 4, mask, status, PPB_NOINTR | PPB_POLL));
6246f3ff79SMike Smith }
63e51b0386SMike Smith 
64bc35c174SNicolas Souchu static int
do_peripheral_wait(device_t bus,uint8_t mask,uint8_t status)653128fa9aSDimitry Andric do_peripheral_wait(device_t bus, uint8_t mask, uint8_t status)
66bc35c174SNicolas Souchu {
670f210c92SNicolas Souchu 	return (ppb_poll_bus(bus, 100, mask, status, PPB_NOINTR | PPB_POLL));
68e51b0386SMike Smith }
69e51b0386SMike Smith 
70e51b0386SMike Smith #define nibble2char(s) (((s & ~nACK) >> 3) | (~s & nBUSY) >> 4)
71e51b0386SMike Smith 
72e51b0386SMike Smith /*
73bc35c174SNicolas Souchu  * ppb_1284_reset_error()
74bc35c174SNicolas Souchu  *
75bc35c174SNicolas Souchu  * Unconditionaly reset the error field
76bc35c174SNicolas Souchu  */
77bc35c174SNicolas Souchu static int
ppb_1284_reset_error(device_t bus,int state)780f210c92SNicolas Souchu ppb_1284_reset_error(device_t bus, int state)
79bc35c174SNicolas Souchu {
800f210c92SNicolas Souchu 	struct ppb_data *ppb = DEVTOSOFTC(bus);
810f210c92SNicolas Souchu 
820f210c92SNicolas Souchu 	ppb->error = PPB_NO_ERROR;
830f210c92SNicolas Souchu 	ppb->state = state;
84bc35c174SNicolas Souchu 
85bc35c174SNicolas Souchu 	return (0);
86bc35c174SNicolas Souchu }
87bc35c174SNicolas Souchu 
88bc35c174SNicolas Souchu /*
89bc35c174SNicolas Souchu  * ppb_1284_get_state()
90bc35c174SNicolas Souchu  *
91bc35c174SNicolas Souchu  * Get IEEE1284 state
92bc35c174SNicolas Souchu  */
93bdcee5cfSNicolas Souchu int
ppb_1284_get_state(device_t bus)940f210c92SNicolas Souchu ppb_1284_get_state(device_t bus)
95bc35c174SNicolas Souchu {
962067d312SJohn Baldwin 	struct ppb_data *ppb = DEVTOSOFTC(bus);
97284c87f6SJohn Baldwin 
982067d312SJohn Baldwin 	mtx_assert(ppb->ppc_lock, MA_OWNED);
992067d312SJohn Baldwin 	return (ppb->state);
100bc35c174SNicolas Souchu }
101bc35c174SNicolas Souchu 
102bc35c174SNicolas Souchu /*
103bc35c174SNicolas Souchu  * ppb_1284_set_state()
104bc35c174SNicolas Souchu  *
105453130d9SPedro F. Giffuni  * Change IEEE1284 state if no error occurred
106bc35c174SNicolas Souchu  */
107bdcee5cfSNicolas Souchu int
ppb_1284_set_state(device_t bus,int state)1080f210c92SNicolas Souchu ppb_1284_set_state(device_t bus, int state)
109bc35c174SNicolas Souchu {
1100f210c92SNicolas Souchu 	struct ppb_data *ppb = DEVTOSOFTC(bus);
1110f210c92SNicolas Souchu 
112453130d9SPedro F. Giffuni 	/* call ppb_1284_reset_error() if you absolutely want to change
113bc35c174SNicolas Souchu 	 * the state from PPB_ERROR to another */
1142067d312SJohn Baldwin 	mtx_assert(ppb->ppc_lock, MA_OWNED);
1150f210c92SNicolas Souchu 	if ((ppb->state != PPB_ERROR) &&
1160f210c92SNicolas Souchu 			(ppb->error == PPB_NO_ERROR)) {
1170f210c92SNicolas Souchu 		ppb->state = state;
1180f210c92SNicolas Souchu 		ppb->error = PPB_NO_ERROR;
119bc35c174SNicolas Souchu 	}
120bc35c174SNicolas Souchu 
121bc35c174SNicolas Souchu 	return (0);
122bc35c174SNicolas Souchu }
123bc35c174SNicolas Souchu 
124bc35c174SNicolas Souchu static int
ppb_1284_set_error(device_t bus,int error,int event)1250f210c92SNicolas Souchu ppb_1284_set_error(device_t bus, int error, int event)
126bc35c174SNicolas Souchu {
1270f210c92SNicolas Souchu 	struct ppb_data *ppb = DEVTOSOFTC(bus);
1280f210c92SNicolas Souchu 
129bc35c174SNicolas Souchu 	/* do not accumulate errors */
1300f210c92SNicolas Souchu 	if ((ppb->error == PPB_NO_ERROR) &&
1310f210c92SNicolas Souchu 			(ppb->state != PPB_ERROR)) {
1320f210c92SNicolas Souchu 		ppb->error = error;
1330f210c92SNicolas Souchu 		ppb->state = PPB_ERROR;
134bc35c174SNicolas Souchu 	}
135bc35c174SNicolas Souchu 
136bc35c174SNicolas Souchu #ifdef DEBUG_1284
137bc35c174SNicolas Souchu 	printf("ppb1284: error=%d status=0x%x event=%d\n", error,
1380f210c92SNicolas Souchu 		ppb_rstr(bus) & 0xff, event);
139bc35c174SNicolas Souchu #endif
140bc35c174SNicolas Souchu 
141bc35c174SNicolas Souchu 	return (0);
142bc35c174SNicolas Souchu }
143bc35c174SNicolas Souchu 
144bc35c174SNicolas Souchu /*
145bc35c174SNicolas Souchu  * ppb_request_mode()
146bc35c174SNicolas Souchu  *
147bc35c174SNicolas Souchu  * Converts mode+options into ext. value
148bc35c174SNicolas Souchu  */
149bc35c174SNicolas Souchu static int
ppb_request_mode(int mode,int options)150bc35c174SNicolas Souchu ppb_request_mode(int mode, int options)
151bc35c174SNicolas Souchu {
152bc35c174SNicolas Souchu 	int request_mode = 0;
153bc35c174SNicolas Souchu 
154bc35c174SNicolas Souchu 	if (options & PPB_EXTENSIBILITY_LINK) {
155bc35c174SNicolas Souchu 		request_mode = EXT_LINK_1284_NORMAL;
156bc35c174SNicolas Souchu 
157bc35c174SNicolas Souchu 	} else {
158bc35c174SNicolas Souchu 		switch (mode) {
159bc35c174SNicolas Souchu 		case PPB_NIBBLE:
160bc35c174SNicolas Souchu 			request_mode = (options & PPB_REQUEST_ID) ?
161bc35c174SNicolas Souchu 					NIBBLE_1284_REQUEST_ID :
162bc35c174SNicolas Souchu 					NIBBLE_1284_NORMAL;
163bc35c174SNicolas Souchu 			break;
164bc35c174SNicolas Souchu 		case PPB_PS2:
165bc35c174SNicolas Souchu 			request_mode = (options & PPB_REQUEST_ID) ?
166bc35c174SNicolas Souchu 					BYTE_1284_REQUEST_ID :
167bc35c174SNicolas Souchu 					BYTE_1284_NORMAL;
168bc35c174SNicolas Souchu 			break;
169bc35c174SNicolas Souchu 		case PPB_ECP:
170bc35c174SNicolas Souchu 			if (options & PPB_USE_RLE)
171bc35c174SNicolas Souchu 				request_mode = (options & PPB_REQUEST_ID) ?
172bc35c174SNicolas Souchu 					ECP_1284_RLE_REQUEST_ID :
173bc35c174SNicolas Souchu 					ECP_1284_RLE;
174bc35c174SNicolas Souchu 			else
175bc35c174SNicolas Souchu 				request_mode = (options & PPB_REQUEST_ID) ?
176bc35c174SNicolas Souchu 					ECP_1284_REQUEST_ID :
177bc35c174SNicolas Souchu 					ECP_1284_NORMAL;
178bc35c174SNicolas Souchu 			break;
179bc35c174SNicolas Souchu 		case PPB_EPP:
180bc35c174SNicolas Souchu 			request_mode = EPP_1284_NORMAL;
181bc35c174SNicolas Souchu 			break;
182bc35c174SNicolas Souchu 		default:
1836e551fb6SDavid E. O'Brien 			panic("%s: unsupported mode %d\n", __func__, mode);
184bc35c174SNicolas Souchu 		}
185bc35c174SNicolas Souchu 	}
186bc35c174SNicolas Souchu 
187bc35c174SNicolas Souchu 	return (request_mode);
188bc35c174SNicolas Souchu }
189bc35c174SNicolas Souchu 
190bc35c174SNicolas Souchu /*
191bc35c174SNicolas Souchu  * ppb_peripheral_negociate()
192bc35c174SNicolas Souchu  *
193453130d9SPedro F. Giffuni  * Negotiate the peripheral side
194bc35c174SNicolas Souchu  */
195bc35c174SNicolas Souchu int
ppb_peripheral_negociate(device_t bus,int mode,int options)1960f210c92SNicolas Souchu ppb_peripheral_negociate(device_t bus, int mode, int options)
197bc35c174SNicolas Souchu {
198bc35c174SNicolas Souchu 	int spin, request_mode, error = 0;
199bc35c174SNicolas Souchu 	char r;
200bc35c174SNicolas Souchu 
2010f210c92SNicolas Souchu 	ppb_set_mode(bus, PPB_COMPATIBLE);
2020f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_PERIPHERAL_NEGOCIATION);
203bc35c174SNicolas Souchu 
204bc35c174SNicolas Souchu 	/* compute ext. value */
205bc35c174SNicolas Souchu 	request_mode = ppb_request_mode(mode, options);
206bc35c174SNicolas Souchu 
207bc35c174SNicolas Souchu 	/* wait host */
208bc35c174SNicolas Souchu 	spin = 10;
2090f210c92SNicolas Souchu 	while (spin-- && (ppb_rstr(bus) & nBUSY))
210bc35c174SNicolas Souchu 		DELAY(1);
211bc35c174SNicolas Souchu 
212bc35c174SNicolas Souchu 	/* check termination */
2130f210c92SNicolas Souchu 	if (!(ppb_rstr(bus) & SELECT) || !spin) {
214bc35c174SNicolas Souchu 		error = ENODEV;
215bc35c174SNicolas Souchu 		goto error;
216bc35c174SNicolas Souchu 	}
217bc35c174SNicolas Souchu 
218bc35c174SNicolas Souchu 	/* Event 4 - read ext. value */
2190f210c92SNicolas Souchu 	r = ppb_rdtr(bus);
220bc35c174SNicolas Souchu 
221bc35c174SNicolas Souchu 	/* nibble mode is not supported */
222bc35c174SNicolas Souchu 	if ((r == (char)request_mode) ||
223bc35c174SNicolas Souchu 			(r == NIBBLE_1284_NORMAL)) {
224bc35c174SNicolas Souchu 		/* Event 5 - restore direction bit, no data avail */
2250f210c92SNicolas Souchu 		ppb_wctr(bus, (STROBE | nINIT) & ~(SELECTIN));
226bc35c174SNicolas Souchu 		DELAY(1);
227bc35c174SNicolas Souchu 
228bc35c174SNicolas Souchu 		/* Event 6 */
2290f210c92SNicolas Souchu 		ppb_wctr(bus, (nINIT) & ~(SELECTIN | STROBE));
230bc35c174SNicolas Souchu 
231bc35c174SNicolas Souchu 		if (r == NIBBLE_1284_NORMAL) {
232bc35c174SNicolas Souchu #ifdef DEBUG_1284
233bc35c174SNicolas Souchu 			printf("R");
234bc35c174SNicolas Souchu #endif
2350f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
236bc35c174SNicolas Souchu 			error = EINVAL;
237bc35c174SNicolas Souchu 			goto error;
238bc35c174SNicolas Souchu 		} else {
2390f210c92SNicolas Souchu 			ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
240bc35c174SNicolas Souchu 			switch (r) {
241bc35c174SNicolas Souchu 			case BYTE_1284_NORMAL:
2420f210c92SNicolas Souchu 				ppb_set_mode(bus, PPB_BYTE);
243bc35c174SNicolas Souchu 				break;
244bc35c174SNicolas Souchu 			default:
245bc35c174SNicolas Souchu 				break;
246bc35c174SNicolas Souchu 			}
247bc35c174SNicolas Souchu #ifdef DEBUG_1284
248bc35c174SNicolas Souchu 			printf("A");
249bc35c174SNicolas Souchu #endif
250453130d9SPedro F. Giffuni 			/* negotiation succeeds */
251bc35c174SNicolas Souchu 		}
252bc35c174SNicolas Souchu 	} else {
253bc35c174SNicolas Souchu 		/* Event 5 - mode not supported */
2540f210c92SNicolas Souchu 		ppb_wctr(bus, SELECTIN);
255bc35c174SNicolas Souchu 		DELAY(1);
256bc35c174SNicolas Souchu 
257bc35c174SNicolas Souchu 		/* Event 6 */
2580f210c92SNicolas Souchu 		ppb_wctr(bus, (SELECTIN) & ~(STROBE | nINIT));
2590f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 4);
260bc35c174SNicolas Souchu 
261bc35c174SNicolas Souchu #ifdef DEBUG_1284
262bc35c174SNicolas Souchu 		printf("r");
263bc35c174SNicolas Souchu #endif
264bc35c174SNicolas Souchu 		error = EINVAL;
265bc35c174SNicolas Souchu 		goto error;
266bc35c174SNicolas Souchu 	}
267bc35c174SNicolas Souchu 
268bc35c174SNicolas Souchu 	return (0);
269bc35c174SNicolas Souchu 
270bc35c174SNicolas Souchu error:
2710f210c92SNicolas Souchu 	ppb_peripheral_terminate(bus, PPB_WAIT);
272bc35c174SNicolas Souchu 	return (error);
273bc35c174SNicolas Souchu }
274bc35c174SNicolas Souchu 
275bc35c174SNicolas Souchu /*
276bc35c174SNicolas Souchu  * ppb_peripheral_terminate()
277bc35c174SNicolas Souchu  *
278bc35c174SNicolas Souchu  * Terminate peripheral transfer side
279bc35c174SNicolas Souchu  *
280bc35c174SNicolas Souchu  * Always return 0 in compatible mode
281bc35c174SNicolas Souchu  */
282bc35c174SNicolas Souchu int
ppb_peripheral_terminate(device_t bus,int how)2830f210c92SNicolas Souchu ppb_peripheral_terminate(device_t bus, int how)
284bc35c174SNicolas Souchu {
285bc35c174SNicolas Souchu 	int error = 0;
286bc35c174SNicolas Souchu 
287bc35c174SNicolas Souchu #ifdef DEBUG_1284
288bc35c174SNicolas Souchu 	printf("t");
289bc35c174SNicolas Souchu #endif
290bc35c174SNicolas Souchu 
2910f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_PERIPHERAL_TERMINATION);
292bc35c174SNicolas Souchu 
293bc35c174SNicolas Souchu 	/* Event 22 - wait up to host response time (1s) */
2940f210c92SNicolas Souchu 	if ((error = do_peripheral_wait(bus, SELECT | nBUSY, 0))) {
2950f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 22);
296bc35c174SNicolas Souchu 		goto error;
297bc35c174SNicolas Souchu 	}
298bc35c174SNicolas Souchu 
299bc35c174SNicolas Souchu 	/* Event 24 */
3000f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
301bc35c174SNicolas Souchu 
302bc35c174SNicolas Souchu 	/* Event 25 - wait up to host response time (1s) */
3030f210c92SNicolas Souchu 	if ((error = do_peripheral_wait(bus, nBUSY, nBUSY))) {
3040f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 25);
305bc35c174SNicolas Souchu 		goto error;
306bc35c174SNicolas Souchu 	}
307bc35c174SNicolas Souchu 
308bc35c174SNicolas Souchu 	/* Event 26 */
3090f210c92SNicolas Souchu 	ppb_wctr(bus, (SELECTIN | nINIT | STROBE) & ~(AUTOFEED));
310bc35c174SNicolas Souchu 	DELAY(1);
311bc35c174SNicolas Souchu 	/* Event 27 */
3120f210c92SNicolas Souchu 	ppb_wctr(bus, (SELECTIN | nINIT) & ~(STROBE | AUTOFEED));
313bc35c174SNicolas Souchu 
314bc35c174SNicolas Souchu 	/* Event 28 - wait up to host response time (1s) */
3150f210c92SNicolas Souchu 	if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
3160f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 28);
317bc35c174SNicolas Souchu 		goto error;
318bc35c174SNicolas Souchu 	}
319bc35c174SNicolas Souchu 
320bc35c174SNicolas Souchu error:
3210f210c92SNicolas Souchu 	ppb_set_mode(bus, PPB_COMPATIBLE);
3220f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
323bc35c174SNicolas Souchu 
324bc35c174SNicolas Souchu 	return (0);
325bc35c174SNicolas Souchu }
326bc35c174SNicolas Souchu 
327bc35c174SNicolas Souchu /*
328bc35c174SNicolas Souchu  * byte_peripheral_outbyte()
329bc35c174SNicolas Souchu  *
330bc35c174SNicolas Souchu  * Write 1 byte in BYTE mode
331bc35c174SNicolas Souchu  */
332bc35c174SNicolas Souchu static int
byte_peripheral_outbyte(device_t bus,char * buffer,int last)3330f210c92SNicolas Souchu byte_peripheral_outbyte(device_t bus, char *buffer, int last)
334bc35c174SNicolas Souchu {
335bc35c174SNicolas Souchu 	int error = 0;
336bc35c174SNicolas Souchu 
337bc35c174SNicolas Souchu 	/* Event 7 */
3380f210c92SNicolas Souchu 	if ((error = do_1284_wait(bus, nBUSY, nBUSY))) {
3390f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 7);
340bc35c174SNicolas Souchu 		goto error;
341bc35c174SNicolas Souchu 	}
342bc35c174SNicolas Souchu 
343bc35c174SNicolas Souchu 	/* check termination */
3440f210c92SNicolas Souchu 	if (!(ppb_rstr(bus) & SELECT)) {
3450f210c92SNicolas Souchu 		ppb_peripheral_terminate(bus, PPB_WAIT);
346bc35c174SNicolas Souchu 		goto error;
347bc35c174SNicolas Souchu 	}
348bc35c174SNicolas Souchu 
349bc35c174SNicolas Souchu 	/* Event 15 - put byte on data lines */
350bc35c174SNicolas Souchu #ifdef DEBUG_1284
351bc35c174SNicolas Souchu 	printf("B");
352bc35c174SNicolas Souchu #endif
3530f210c92SNicolas Souchu 	ppb_wdtr(bus, *buffer);
354bc35c174SNicolas Souchu 
355bc35c174SNicolas Souchu 	/* Event 9 */
3560f210c92SNicolas Souchu 	ppb_wctr(bus, (AUTOFEED | STROBE) & ~(nINIT | SELECTIN));
357bc35c174SNicolas Souchu 
358bc35c174SNicolas Souchu 	/* Event 10 - wait data read */
3590f210c92SNicolas Souchu 	if ((error = do_peripheral_wait(bus, nBUSY, 0))) {
3600f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
361bc35c174SNicolas Souchu 		goto error;
362bc35c174SNicolas Souchu 	}
363bc35c174SNicolas Souchu 
364bc35c174SNicolas Souchu 	/* Event 11 */
365bc35c174SNicolas Souchu 	if (!last) {
3660f210c92SNicolas Souchu 		ppb_wctr(bus, (AUTOFEED) & ~(nINIT | STROBE | SELECTIN));
367bc35c174SNicolas Souchu 	} else {
3680f210c92SNicolas Souchu 		ppb_wctr(bus, (nINIT) & ~(STROBE | SELECTIN | AUTOFEED));
369bc35c174SNicolas Souchu 	}
370bc35c174SNicolas Souchu 
371bc35c174SNicolas Souchu #if 0
372bc35c174SNicolas Souchu 	/* Event 16 - wait strobe */
3730f210c92SNicolas Souchu 	if ((error = do_peripheral_wait(bus, nACK | nBUSY, 0))) {
3740f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 16);
375bc35c174SNicolas Souchu 		goto error;
376bc35c174SNicolas Souchu 	}
377bc35c174SNicolas Souchu #endif
378bc35c174SNicolas Souchu 
379bc35c174SNicolas Souchu 	/* check termination */
3800f210c92SNicolas Souchu 	if (!(ppb_rstr(bus) & SELECT)) {
3810f210c92SNicolas Souchu 		ppb_peripheral_terminate(bus, PPB_WAIT);
382bc35c174SNicolas Souchu 		goto error;
383bc35c174SNicolas Souchu 	}
384bc35c174SNicolas Souchu 
385bc35c174SNicolas Souchu error:
386bc35c174SNicolas Souchu 	return (error);
387bc35c174SNicolas Souchu }
388bc35c174SNicolas Souchu 
389bc35c174SNicolas Souchu /*
390bc35c174SNicolas Souchu  * byte_peripheral_write()
391bc35c174SNicolas Souchu  *
392bc35c174SNicolas Souchu  * Write n bytes in BYTE mode
393bc35c174SNicolas Souchu  */
394bc35c174SNicolas Souchu int
byte_peripheral_write(device_t bus,char * buffer,int len,int * sent)3950f210c92SNicolas Souchu byte_peripheral_write(device_t bus, char *buffer, int len, int *sent)
396bc35c174SNicolas Souchu {
397bc35c174SNicolas Souchu 	int error = 0, i;
398bc35c174SNicolas Souchu 	char r;
399bc35c174SNicolas Souchu 
4000f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_PERIPHERAL_TRANSFER);
401bc35c174SNicolas Souchu 
402bc35c174SNicolas Souchu 	/* wait forever, the remote host is master and should initiate
403bc35c174SNicolas Souchu 	 * termination
404bc35c174SNicolas Souchu 	 */
405bc35c174SNicolas Souchu 	for (i=0; i<len; i++) {
406bc35c174SNicolas Souchu 		/* force remote nFAULT low to release the remote waiting
407bc35c174SNicolas Souchu 		 * process, if any
408bc35c174SNicolas Souchu 		 */
4090f210c92SNicolas Souchu 		r = ppb_rctr(bus);
4100f210c92SNicolas Souchu 		ppb_wctr(bus, r & ~nINIT);
411bc35c174SNicolas Souchu 
412bc35c174SNicolas Souchu #ifdef DEBUG_1284
413bc35c174SNicolas Souchu 		printf("y");
414bc35c174SNicolas Souchu #endif
415bc35c174SNicolas Souchu 		/* Event 7 */
4160f210c92SNicolas Souchu 		error = ppb_poll_bus(bus, PPB_FOREVER, nBUSY, nBUSY,
417bc35c174SNicolas Souchu 					PPB_INTR);
418bc35c174SNicolas Souchu 
419bc35c174SNicolas Souchu 		if (error && error != EWOULDBLOCK)
420bc35c174SNicolas Souchu 			goto error;
421bc35c174SNicolas Souchu 
422bc35c174SNicolas Souchu #ifdef DEBUG_1284
423bc35c174SNicolas Souchu 		printf("b");
424bc35c174SNicolas Souchu #endif
4250f210c92SNicolas Souchu 		if ((error = byte_peripheral_outbyte(bus, buffer+i, (i == len-1))))
426bc35c174SNicolas Souchu 			goto error;
427bc35c174SNicolas Souchu 	}
428bc35c174SNicolas Souchu error:
429bc35c174SNicolas Souchu 	if (!error)
4300f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_PERIPHERAL_IDLE);
431bc35c174SNicolas Souchu 
432bc35c174SNicolas Souchu 	*sent = i;
433bc35c174SNicolas Souchu 	return (error);
434bc35c174SNicolas Souchu }
435bc35c174SNicolas Souchu 
436bc35c174SNicolas Souchu /*
437bc35c174SNicolas Souchu  * byte_1284_inbyte()
438bc35c174SNicolas Souchu  *
439bc35c174SNicolas Souchu  * Read 1 byte in BYTE mode
440bc35c174SNicolas Souchu  */
441bc35c174SNicolas Souchu int
byte_1284_inbyte(device_t bus,char * buffer)4420f210c92SNicolas Souchu byte_1284_inbyte(device_t bus, char *buffer)
443bc35c174SNicolas Souchu {
444bc35c174SNicolas Souchu 	int error = 0;
445bc35c174SNicolas Souchu 
446bc35c174SNicolas Souchu 	/* Event 7 - ready to take data (nAUTO low) */
4470f210c92SNicolas Souchu 	ppb_wctr(bus, (PCD | nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
448bc35c174SNicolas Souchu 
449bc35c174SNicolas Souchu 	/* Event 9 - peripheral set nAck low */
4500f210c92SNicolas Souchu 	if ((error = do_1284_wait(bus, nACK, 0))) {
4510f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
452bc35c174SNicolas Souchu 		goto error;
453bc35c174SNicolas Souchu 	}
454bc35c174SNicolas Souchu 
455bc35c174SNicolas Souchu 	/* read the byte */
4560f210c92SNicolas Souchu 	*buffer = ppb_rdtr(bus);
457bc35c174SNicolas Souchu 
458bc35c174SNicolas Souchu 	/* Event 10 - data received, can't accept more */
4590f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
460bc35c174SNicolas Souchu 
461bc35c174SNicolas Souchu 	/* Event 11 - peripheral ack */
4620f210c92SNicolas Souchu 	if ((error = do_1284_wait(bus, nACK, nACK))) {
4630f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
464bc35c174SNicolas Souchu 		goto error;
465bc35c174SNicolas Souchu 	}
466bc35c174SNicolas Souchu 
467bc35c174SNicolas Souchu 	/* Event 16 - strobe */
4680f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
469bc35c174SNicolas Souchu 	DELAY(3);
4700f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT) & ~(AUTOFEED | STROBE | SELECTIN));
471bc35c174SNicolas Souchu 
472bc35c174SNicolas Souchu error:
473bc35c174SNicolas Souchu 	return (error);
474bc35c174SNicolas Souchu }
475bc35c174SNicolas Souchu 
476bc35c174SNicolas Souchu /*
477e51b0386SMike Smith  * nibble_1284_inbyte()
478e51b0386SMike Smith  *
47946f3ff79SMike Smith  * Read 1 byte in NIBBLE mode
480e51b0386SMike Smith  */
481e51b0386SMike Smith int
nibble_1284_inbyte(device_t bus,char * buffer)4820f210c92SNicolas Souchu nibble_1284_inbyte(device_t bus, char *buffer)
483e51b0386SMike Smith {
48446f3ff79SMike Smith 	char nibble[2];
485e51b0386SMike Smith 	int i, error;
486e51b0386SMike Smith 
487e51b0386SMike Smith 	for (i = 0; i < 2; i++) {
488bc35c174SNicolas Souchu 		/* Event 7 - ready to take data (nAUTO low) */
4890f210c92SNicolas Souchu 		ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
490bc35c174SNicolas Souchu 
491bc35c174SNicolas Souchu 		/* Event 8 - peripheral writes the first nibble */
492bc35c174SNicolas Souchu 
493bc35c174SNicolas Souchu 		/* Event 9 - peripheral set nAck low */
4940f210c92SNicolas Souchu 		if ((error = do_1284_wait(bus, nACK, 0))) {
4950f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_TIMEOUT, 9);
496bc35c174SNicolas Souchu 			goto error;
497bc35c174SNicolas Souchu 		}
498e51b0386SMike Smith 
499e51b0386SMike Smith 		/* read nibble */
5000f210c92SNicolas Souchu 		nibble[i] = ppb_rstr(bus);
501e51b0386SMike Smith 
502bc35c174SNicolas Souchu 		/* Event 10 - ack, nibble received */
5030f210c92SNicolas Souchu 		ppb_wctr(bus, nINIT & ~(AUTOFEED | STROBE | SELECTIN));
504e51b0386SMike Smith 
505bc35c174SNicolas Souchu 		/* Event 11 - wait ack from peripherial */
5060f210c92SNicolas Souchu 		if ((error = do_1284_wait(bus, nACK, nACK))) {
5070f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_TIMEOUT, 11);
508bc35c174SNicolas Souchu 			goto error;
509bc35c174SNicolas Souchu 		}
510e51b0386SMike Smith 	}
511e51b0386SMike Smith 
512e51b0386SMike Smith 	*buffer = ((nibble2char(nibble[1]) << 4) & 0xf0) |
513e51b0386SMike Smith 				(nibble2char(nibble[0]) & 0x0f);
514e51b0386SMike Smith 
515bc35c174SNicolas Souchu error:
516bc35c174SNicolas Souchu 	return (error);
517e51b0386SMike Smith }
518e51b0386SMike Smith 
519e51b0386SMike Smith /*
520bc35c174SNicolas Souchu  * spp_1284_read()
521bc35c174SNicolas Souchu  *
522bc35c174SNicolas Souchu  * Read in IEEE1284 NIBBLE/BYTE mode
523e51b0386SMike Smith  */
524bc35c174SNicolas Souchu int
spp_1284_read(device_t bus,int mode,char * buffer,int max,int * read)5250f210c92SNicolas Souchu spp_1284_read(device_t bus, int mode, char *buffer, int max, int *read)
526e51b0386SMike Smith {
527bc35c174SNicolas Souchu 	int error = 0, len = 0;
528bc35c174SNicolas Souchu 	int terminate_after_transfer = 1;
529bc35c174SNicolas Souchu 	int state;
530e51b0386SMike Smith 
531bc35c174SNicolas Souchu 	*read = len = 0;
532e51b0386SMike Smith 
5330f210c92SNicolas Souchu 	state = ppb_1284_get_state(bus);
534e51b0386SMike Smith 
535bc35c174SNicolas Souchu 	switch (state) {
536bc35c174SNicolas Souchu 	case PPB_FORWARD_IDLE:
5370f210c92SNicolas Souchu 		if ((error = ppb_1284_negociate(bus, mode, 0)))
538bc35c174SNicolas Souchu 			return (error);
539bc35c174SNicolas Souchu 		break;
540e51b0386SMike Smith 
541bc35c174SNicolas Souchu 	case PPB_REVERSE_IDLE:
542bc35c174SNicolas Souchu 		terminate_after_transfer = 0;
543bc35c174SNicolas Souchu 		break;
544e51b0386SMike Smith 
545bc35c174SNicolas Souchu 	default:
5460f210c92SNicolas Souchu 		ppb_1284_terminate(bus);
5470f210c92SNicolas Souchu 		if ((error = ppb_1284_negociate(bus, mode, 0)))
548bc35c174SNicolas Souchu 			return (error);
549bc35c174SNicolas Souchu 		break;
550bc35c174SNicolas Souchu 	}
551bc35c174SNicolas Souchu 
5520f210c92SNicolas Souchu 	while ((len < max) && !(ppb_rstr(bus) & (nFAULT))) {
5530f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_REVERSE_TRANSFER);
554bc35c174SNicolas Souchu 
555bc35c174SNicolas Souchu #ifdef DEBUG_1284
556bc35c174SNicolas Souchu 		printf("B");
557bc35c174SNicolas Souchu #endif
558bc35c174SNicolas Souchu 
559bc35c174SNicolas Souchu 		switch (mode) {
560bc35c174SNicolas Souchu 		case PPB_NIBBLE:
561bc35c174SNicolas Souchu 			/* read a byte, error means no more data */
5620f210c92SNicolas Souchu 			if (nibble_1284_inbyte(bus, buffer+len))
563bc35c174SNicolas Souchu 				goto end_while;
564bc35c174SNicolas Souchu 			break;
565bc35c174SNicolas Souchu 		case PPB_BYTE:
5660f210c92SNicolas Souchu 			if (byte_1284_inbyte(bus, buffer+len))
567bc35c174SNicolas Souchu 				goto end_while;
568bc35c174SNicolas Souchu 			break;
569bc35c174SNicolas Souchu 		default:
570bc35c174SNicolas Souchu 			error = EINVAL;
571bc35c174SNicolas Souchu 			goto end_while;
572bc35c174SNicolas Souchu 		}
573bc35c174SNicolas Souchu 		len ++;
574bc35c174SNicolas Souchu 	}
575bc35c174SNicolas Souchu end_while:
576bc35c174SNicolas Souchu 
577bc35c174SNicolas Souchu 	if (!error)
5780f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
579bc35c174SNicolas Souchu 
580bc35c174SNicolas Souchu 	*read = len;
581bc35c174SNicolas Souchu 
582bc35c174SNicolas Souchu 	if (terminate_after_transfer || error)
5830f210c92SNicolas Souchu 		ppb_1284_terminate(bus);
584bc35c174SNicolas Souchu 
585bc35c174SNicolas Souchu 	return (error);
586bc35c174SNicolas Souchu }
587bc35c174SNicolas Souchu 
588bc35c174SNicolas Souchu /*
589bc35c174SNicolas Souchu  * ppb_1284_read_id()
590bc35c174SNicolas Souchu  *
591bc35c174SNicolas Souchu  */
592bc35c174SNicolas Souchu int
ppb_1284_read_id(device_t bus,int mode,char * buffer,int max,int * read)5930f210c92SNicolas Souchu ppb_1284_read_id(device_t bus, int mode, char *buffer,
594bc35c174SNicolas Souchu 		int max, int *read)
595bc35c174SNicolas Souchu {
596bc35c174SNicolas Souchu 	int error = 0;
597bc35c174SNicolas Souchu 
598bc35c174SNicolas Souchu 	/* fill the buffer with 0s */
599bc35c174SNicolas Souchu 	bzero(buffer, max);
600bc35c174SNicolas Souchu 
601bc35c174SNicolas Souchu 	switch (mode) {
602bc35c174SNicolas Souchu 	case PPB_NIBBLE:
603bc35c174SNicolas Souchu 	case PPB_ECP:
6040f210c92SNicolas Souchu 		if ((error = ppb_1284_negociate(bus, PPB_NIBBLE, PPB_REQUEST_ID)))
605bc35c174SNicolas Souchu 			return (error);
6060f210c92SNicolas Souchu 		error = spp_1284_read(bus, PPB_NIBBLE, buffer, max, read);
607bc35c174SNicolas Souchu 		break;
608bc35c174SNicolas Souchu 	case PPB_BYTE:
6090f210c92SNicolas Souchu 		if ((error = ppb_1284_negociate(bus, PPB_BYTE, PPB_REQUEST_ID)))
610bc35c174SNicolas Souchu 			return (error);
6110f210c92SNicolas Souchu 		error = spp_1284_read(bus, PPB_BYTE, buffer, max, read);
612bc35c174SNicolas Souchu 		break;
613bc35c174SNicolas Souchu 	default:
6146e551fb6SDavid E. O'Brien 		panic("%s: unsupported mode %d\n", __func__, mode);
615bc35c174SNicolas Souchu 	}
616bc35c174SNicolas Souchu 
6170f210c92SNicolas Souchu 	ppb_1284_terminate(bus);
618bc35c174SNicolas Souchu 	return (error);
619bc35c174SNicolas Souchu }
620bc35c174SNicolas Souchu 
621bc35c174SNicolas Souchu /*
622bc35c174SNicolas Souchu  * ppb_1284_read()
623bc35c174SNicolas Souchu  *
624bc35c174SNicolas Souchu  * IEEE1284 read
625bc35c174SNicolas Souchu  */
626bc35c174SNicolas Souchu int
ppb_1284_read(device_t bus,int mode,char * buffer,int max,int * read)6270f210c92SNicolas Souchu ppb_1284_read(device_t bus, int mode, char *buffer,
628bc35c174SNicolas Souchu 		int max, int *read)
629bc35c174SNicolas Souchu {
630bc35c174SNicolas Souchu 	int error = 0;
631bc35c174SNicolas Souchu 
632bc35c174SNicolas Souchu 	switch (mode) {
633bc35c174SNicolas Souchu 	case PPB_NIBBLE:
634bc35c174SNicolas Souchu 	case PPB_BYTE:
6350f210c92SNicolas Souchu 		error = spp_1284_read(bus, mode, buffer, max, read);
636bc35c174SNicolas Souchu 		break;
637bc35c174SNicolas Souchu 	default:
638bc35c174SNicolas Souchu 		return (EINVAL);
639bc35c174SNicolas Souchu 	}
640bc35c174SNicolas Souchu 
641bc35c174SNicolas Souchu 	return (error);
642e51b0386SMike Smith }
643e51b0386SMike Smith 
644e51b0386SMike Smith /*
64524ef1eb1SNicolas Souchu  * ppb_1284_negociate()
646e51b0386SMike Smith  *
647453130d9SPedro F. Giffuni  * IEEE1284 negotiation phase
648bc35c174SNicolas Souchu  *
649e51b0386SMike Smith  * Normal nibble mode or request device id mode (see ppb_1284.h)
650bc35c174SNicolas Souchu  *
651453130d9SPedro F. Giffuni  * After negotiation, nFAULT is low if data is available
652e51b0386SMike Smith  */
653e51b0386SMike Smith int
ppb_1284_negociate(device_t bus,int mode,int options)6540f210c92SNicolas Souchu ppb_1284_negociate(device_t bus, int mode, int options)
655e51b0386SMike Smith {
656e51b0386SMike Smith 	int error;
657bc35c174SNicolas Souchu 	int request_mode;
658e51b0386SMike Smith 
659bc35c174SNicolas Souchu #ifdef DEBUG_1284
660bc35c174SNicolas Souchu 	printf("n");
661bc35c174SNicolas Souchu #endif
662bc35c174SNicolas Souchu 
6630f210c92SNicolas Souchu 	if (ppb_1284_get_state(bus) >= PPB_PERIPHERAL_NEGOCIATION)
6640f210c92SNicolas Souchu 		ppb_peripheral_terminate(bus, PPB_WAIT);
665bc35c174SNicolas Souchu 
6660f210c92SNicolas Souchu 	if (ppb_1284_get_state(bus) != PPB_FORWARD_IDLE)
6670f210c92SNicolas Souchu 		ppb_1284_terminate(bus);
668bc35c174SNicolas Souchu 
669bc35c174SNicolas Souchu #ifdef DEBUG_1284
670bc35c174SNicolas Souchu 	printf("%d", mode);
671bc35c174SNicolas Souchu #endif
672bc35c174SNicolas Souchu 
673bc35c174SNicolas Souchu 	/* ensure the host is in compatible mode */
6740f210c92SNicolas Souchu 	ppb_set_mode(bus, PPB_COMPATIBLE);
675bc35c174SNicolas Souchu 
676453130d9SPedro F. Giffuni 	/* reset error to catch the actual negotiation error */
6770f210c92SNicolas Souchu 	ppb_1284_reset_error(bus, PPB_FORWARD_IDLE);
678bc35c174SNicolas Souchu 
679bc35c174SNicolas Souchu 	/* calculate ext. value */
680bc35c174SNicolas Souchu 	request_mode = ppb_request_mode(mode, options);
681bc35c174SNicolas Souchu 
682bc35c174SNicolas Souchu 	/* default state */
6830f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
68424ef1eb1SNicolas Souchu 	DELAY(1);
685e51b0386SMike Smith 
686453130d9SPedro F. Giffuni 	/* enter negotiation phase */
6870f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_NEGOCIATION);
688bc35c174SNicolas Souchu 
689bc35c174SNicolas Souchu 	/* Event 0 - put the exten. value on the data lines */
6900f210c92SNicolas Souchu 	ppb_wdtr(bus, request_mode);
691bc35c174SNicolas Souchu 
692bc35c174SNicolas Souchu #ifdef PERIPH_1284
693bc35c174SNicolas Souchu 	/* request remote host attention */
6940f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | STROBE) & ~(AUTOFEED | SELECTIN));
695bc35c174SNicolas Souchu 	DELAY(1);
6960f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT) & ~(STROBE | AUTOFEED | SELECTIN));
697bc35c174SNicolas Souchu #else
69824ef1eb1SNicolas Souchu 	DELAY(1);
69924ef1eb1SNicolas Souchu 
700bc35c174SNicolas Souchu #endif /* !PERIPH_1284 */
701bc35c174SNicolas Souchu 
702bc35c174SNicolas Souchu 	/* Event 1 - enter IEEE1284 mode */
7030f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | AUTOFEED) & ~(STROBE | SELECTIN));
70424ef1eb1SNicolas Souchu 
705bc35c174SNicolas Souchu #ifdef PERIPH_1284
706bc35c174SNicolas Souchu 	/* ignore the PError line, wait a bit more, remote host's
707bc35c174SNicolas Souchu 	 * interrupts don't respond fast enough */
7080f210c92SNicolas Souchu 	if (ppb_poll_bus(bus, 40, nACK | SELECT | nFAULT,
709bc35c174SNicolas Souchu 				SELECT | nFAULT, PPB_NOINTR | PPB_POLL)) {
7100f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
711bc35c174SNicolas Souchu 		error = ENODEV;
71224ef1eb1SNicolas Souchu 		goto error;
713bc35c174SNicolas Souchu 	}
714bc35c174SNicolas Souchu #else
715bc35c174SNicolas Souchu 	/* Event 2 - trying IEEE1284 dialog */
7160f210c92SNicolas Souchu 	if (do_1284_wait(bus, nACK | PERROR | SELECT | nFAULT,
717bc35c174SNicolas Souchu 			PERROR  | SELECT | nFAULT)) {
7180f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_NOT_IEEE1284, 2);
719bc35c174SNicolas Souchu 		error = ENODEV;
720bc35c174SNicolas Souchu 		goto error;
721bc35c174SNicolas Souchu 	}
722bc35c174SNicolas Souchu #endif /* !PERIPH_1284 */
72324ef1eb1SNicolas Souchu 
724bc35c174SNicolas Souchu 	/* Event 3 - latch the ext. value to the peripheral */
7250f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | STROBE | AUTOFEED) & ~SELECTIN);
726bc35c174SNicolas Souchu 	DELAY(1);
727e51b0386SMike Smith 
728bc35c174SNicolas Souchu 	/* Event 4 - IEEE1284 device recognized */
7290f210c92SNicolas Souchu 	ppb_wctr(bus, nINIT & ~(SELECTIN | AUTOFEED | STROBE));
73024ef1eb1SNicolas Souchu 
731bc35c174SNicolas Souchu 	/* Event 6 - waiting for status lines */
7320f210c92SNicolas Souchu 	if (do_1284_wait(bus, nACK, nACK)) {
7330f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 6);
734bc35c174SNicolas Souchu 		error = EBUSY;
73524ef1eb1SNicolas Souchu 		goto error;
736bc35c174SNicolas Souchu 	}
73724ef1eb1SNicolas Souchu 
738975e2e3fSGordon Bergling 	/* Event 7 - querying result consider nACK not to misunderstand
739bc35c174SNicolas Souchu 	 * a remote computer terminate sequence */
74078534d41SNicolas Souchu 	if (options & PPB_EXTENSIBILITY_LINK) {
74178534d41SNicolas Souchu 		/* XXX not fully supported yet */
7420f210c92SNicolas Souchu 		ppb_1284_terminate(bus);
74378534d41SNicolas Souchu 		return (0);
74478534d41SNicolas Souchu 	}
745bc35c174SNicolas Souchu 	if (request_mode == NIBBLE_1284_NORMAL) {
7460f210c92SNicolas Souchu 		if (do_1284_wait(bus, nACK | SELECT, nACK)) {
7470f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
748bc35c174SNicolas Souchu 			error = ENODEV;
74924ef1eb1SNicolas Souchu 			goto error;
750bc35c174SNicolas Souchu 		}
751bc35c174SNicolas Souchu 	} else {
7520f210c92SNicolas Souchu 		if (do_1284_wait(bus, nACK | SELECT, SELECT | nACK)) {
7530f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_MODE_UNSUPPORTED, 7);
754bc35c174SNicolas Souchu 			error = ENODEV;
75524ef1eb1SNicolas Souchu 			goto error;
756bc35c174SNicolas Souchu 		}
757bc35c174SNicolas Souchu 	}
758bc35c174SNicolas Souchu 
759bc35c174SNicolas Souchu 	switch (mode) {
760bc35c174SNicolas Souchu 	case PPB_NIBBLE:
761bc35c174SNicolas Souchu 	case PPB_PS2:
762bc35c174SNicolas Souchu 		/* enter reverse idle phase */
7630f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_REVERSE_IDLE);
764bc35c174SNicolas Souchu 		break;
765bc35c174SNicolas Souchu 	case PPB_ECP:
766453130d9SPedro F. Giffuni 		/* negotiation ok, now setup the communication */
7670f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_SETUP);
7680f210c92SNicolas Souchu 		ppb_wctr(bus, (nINIT | AUTOFEED) & ~(SELECTIN | STROBE));
769bc35c174SNicolas Souchu 
770bc35c174SNicolas Souchu #ifdef PERIPH_1284
771bc35c174SNicolas Souchu 		/* ignore PError line */
7720f210c92SNicolas Souchu 		if (do_1284_wait(bus, nACK | SELECT | nBUSY,
773bc35c174SNicolas Souchu 					nACK | SELECT | nBUSY)) {
7740f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
775bc35c174SNicolas Souchu 			error = ENODEV;
776bc35c174SNicolas Souchu 			goto error;
777bc35c174SNicolas Souchu 		}
778bc35c174SNicolas Souchu #else
7790f210c92SNicolas Souchu 		if (do_1284_wait(bus, nACK | SELECT | PERROR | nBUSY,
780bc35c174SNicolas Souchu 					nACK | SELECT | PERROR | nBUSY)) {
7810f210c92SNicolas Souchu 			ppb_1284_set_error(bus, PPB_TIMEOUT, 30);
782bc35c174SNicolas Souchu 			error = ENODEV;
783bc35c174SNicolas Souchu 			goto error;
784bc35c174SNicolas Souchu 		}
785bc35c174SNicolas Souchu #endif /* !PERIPH_1284 */
786bc35c174SNicolas Souchu 
787bc35c174SNicolas Souchu 		/* ok, the host enters the ForwardIdle state */
7880f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_ECP_FORWARD_IDLE);
789bc35c174SNicolas Souchu 		break;
790bc35c174SNicolas Souchu 	case PPB_EPP:
7910f210c92SNicolas Souchu 		ppb_1284_set_state(bus, PPB_EPP_IDLE);
792bc35c174SNicolas Souchu 		break;
793bc35c174SNicolas Souchu 
794bc35c174SNicolas Souchu 	default:
7956e551fb6SDavid E. O'Brien 		panic("%s: unknown mode (%d)!", __func__, mode);
796bc35c174SNicolas Souchu 	}
7970f210c92SNicolas Souchu 	ppb_set_mode(bus, mode);
79824ef1eb1SNicolas Souchu 
79924ef1eb1SNicolas Souchu 	return (0);
80024ef1eb1SNicolas Souchu 
80124ef1eb1SNicolas Souchu error:
8020f210c92SNicolas Souchu 	ppb_1284_terminate(bus);
80324ef1eb1SNicolas Souchu 
804e51b0386SMike Smith 	return (error);
805e51b0386SMike Smith }
806e51b0386SMike Smith 
807bc35c174SNicolas Souchu /*
808bc35c174SNicolas Souchu  * ppb_1284_terminate()
809bc35c174SNicolas Souchu  *
810bc35c174SNicolas Souchu  * IEEE1284 termination phase, return code should ignored since the host
811bc35c174SNicolas Souchu  * is _always_ in compatible mode after ppb_1284_terminate()
812bc35c174SNicolas Souchu  */
81324ef1eb1SNicolas Souchu int
ppb_1284_terminate(device_t bus)8140f210c92SNicolas Souchu ppb_1284_terminate(device_t bus)
81524ef1eb1SNicolas Souchu {
816e51b0386SMike Smith 
817bc35c174SNicolas Souchu #ifdef DEBUG_1284
818bc35c174SNicolas Souchu 	printf("T");
819bc35c174SNicolas Souchu #endif
820e51b0386SMike Smith 
821bc35c174SNicolas Souchu 	/* do not reset error here to keep the error that
822453130d9SPedro F. Giffuni 	 * may occurred before the ppb_1284_terminate() call */
8230f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_TERMINATION);
82424ef1eb1SNicolas Souchu 
825bc35c174SNicolas Souchu #ifdef PERIPH_1284
826bc35c174SNicolas Souchu 	/* request remote host attention */
8270f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | STROBE | SELECTIN) & ~(AUTOFEED));
828bc35c174SNicolas Souchu 	DELAY(1);
829bc35c174SNicolas Souchu #endif /* PERIPH_1284 */
83024ef1eb1SNicolas Souchu 
831bc35c174SNicolas Souchu 	/* Event 22 - set nSelectin low and nAutoFeed high */
8320f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
83324ef1eb1SNicolas Souchu 
834bc35c174SNicolas Souchu 	/* Event 24 - waiting for peripheral, Xflag ignored */
8350f210c92SNicolas Souchu 	if (do_1284_wait(bus, nACK | nBUSY | nFAULT, nFAULT)) {
8360f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 24);
837bc35c174SNicolas Souchu 		goto error;
83824ef1eb1SNicolas Souchu 	}
839e51b0386SMike Smith 
840bc35c174SNicolas Souchu 	/* Event 25 - set nAutoFd low */
8410f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | SELECTIN | AUTOFEED) & ~STROBE);
842bc35c174SNicolas Souchu 
843bc35c174SNicolas Souchu 	/* Event 26 - compatible mode status is set */
844bc35c174SNicolas Souchu 
845bc35c174SNicolas Souchu 	/* Event 27 - peripheral set nAck high */
8460f210c92SNicolas Souchu 	if (do_1284_wait(bus, nACK, nACK)) {
8470f210c92SNicolas Souchu 		ppb_1284_set_error(bus, PPB_TIMEOUT, 27);
848bc35c174SNicolas Souchu 	}
849bc35c174SNicolas Souchu 
850bc35c174SNicolas Souchu 	/* Event 28 - end termination, return to idle phase */
8510f210c92SNicolas Souchu 	ppb_wctr(bus, (nINIT | SELECTIN) & ~(STROBE | AUTOFEED));
852bc35c174SNicolas Souchu 
853bc35c174SNicolas Souchu error:
854bc35c174SNicolas Souchu 	/* return to compatible mode */
8550f210c92SNicolas Souchu 	ppb_set_mode(bus, PPB_COMPATIBLE);
8560f210c92SNicolas Souchu 	ppb_1284_set_state(bus, PPB_FORWARD_IDLE);
857bc35c174SNicolas Souchu 
858e51b0386SMike Smith 	return (0);
859e51b0386SMike Smith }
860