xref: /netbsd-src/sys/arch/mac68k/dev/adb_direct.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: adb_direct.c,v 1.42 2000/03/23 06:39:55 thorpej Exp $	*/
2 
3 /* From: adb_direct.c 2.02 4/18/97 jpw */
4 
5 /*
6  * Copyright (C) 1996, 1997 John P. Wittkoski
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *  This product includes software developed by John P. Wittkoski.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * This code is rather messy, but I don't have time right now
37  * to clean it up as much as I would like.
38  * But it works, so I'm happy. :-) jpw
39  */
40 
41 /*
42  * TO DO:
43  *  - We could reduce the time spent in the adb_intr_* routines
44  *    by having them save the incoming and outgoing data directly
45  *    in the adbInbound and adbOutbound queues, as it would reduce
46  *    the number of times we need to copy the data around. It
47  *    would also make the code more readable and easier to follow.
48  *  - (Related to above) Use the header part of adbCommand to
49  *    reduce the number of copies we have to do of the data.
50  *  - (Related to above) Actually implement the adbOutbound queue.
51  *    This is fairly easy once you switch all the intr routines
52  *    over to using adbCommand structs directly.
53  *  - There is a bug in the state machine of adb_intr_cuda
54  *    code that causes hangs, especially on 030 machines, probably
55  *    because of some timing issues. Because I have been unable to
56  *    determine the exact cause of this bug, I used the timeout function
57  *    to check for and recover from this condition. If anyone finds
58  *    the actual cause of this bug, the calls to timeout and the
59  *    adb_cuda_tickle routine can be removed.
60  */
61 
62 #ifdef __NetBSD__
63 #include "opt_adb.h"
64 
65 #include <sys/param.h>
66 #include <sys/cdefs.h>
67 #include <sys/pool.h>
68 #include <sys/queue.h>
69 #include <sys/systm.h>
70 #include <sys/callout.h>
71 
72 #include <machine/viareg.h>
73 #include <machine/param.h>
74 #include <machine/cpu.h>
75 #include <machine/adbsys.h>			/* required for adbvar.h */
76 #include <machine/iopreg.h>			/* required for IOP support */
77 
78 #include <mac68k/mac68k/macrom.h>
79 #include <mac68k/dev/adbvar.h>
80 #define printf_intr printf
81 #else /* !__NetBSD__, i.e. Mac OS */
82 #include "via.h"				/* for macos based testing */
83 /* #define ADB_DEBUG */				/* more verbose for testing */
84 
85 /* Types of ADB hardware that we support */
86 #define ADB_HW_UNKNOWN		0x0	/* don't know */
87 #define ADB_HW_II		0x1	/* Mac II series */
88 #define ADB_HW_IISI		0x2	/* Mac IIsi series */
89 #define ADB_HW_PB		0x3	/* PowerBook series */
90 #define ADB_HW_CUDA		0x4	/* Machines with a Cuda chip */
91 #endif /* __NetBSD__ */
92 
93 /* some misc. leftovers */
94 #define vPB		0x0000
95 #define vPB3		0x08
96 #define vPB4		0x10
97 #define vPB5		0x20
98 #define vSR_INT		0x04
99 #define vSR_OUT		0x10
100 
101 /* the type of ADB action that we are currently preforming */
102 #define ADB_ACTION_NOTREADY	0x1	/* has not been initialized yet */
103 #define ADB_ACTION_IDLE		0x2	/* the bus is currently idle */
104 #define ADB_ACTION_OUT		0x3	/* sending out a command */
105 #define ADB_ACTION_IN		0x4	/* receiving data */
106 #define ADB_ACTION_POLLING	0x5	/* polling - II only */
107 #define ADB_ACTION_RUNNING	0x6	/* running - IOP only */
108 
109 /*
110  * These describe the state of the ADB bus itself, although they
111  * don't necessarily correspond directly to ADB states.
112  * Note: these are not really used in the IIsi code.
113  */
114 #define ADB_BUS_UNKNOWN		0x1	/* we don't know yet - all models */
115 #define ADB_BUS_IDLE		0x2	/* bus is idle - all models */
116 #define ADB_BUS_CMD		0x3	/* starting a command - II models */
117 #define ADB_BUS_ODD		0x4	/* the "odd" state - II models */
118 #define ADB_BUS_EVEN		0x5	/* the "even" state - II models */
119 #define ADB_BUS_ACTIVE		0x6	/* active state - IIsi models */
120 #define ADB_BUS_ACK		0x7	/* currently ACKing - IIsi models */
121 
122 /*
123  * Shortcuts for setting or testing the VIA bit states.
124  * Not all shortcuts are used for every type of ADB hardware.
125  */
126 #define ADB_SET_STATE_IDLE_II()		via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
127 #define ADB_SET_STATE_IDLE_IISI()	via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
128 #define ADB_SET_STATE_IDLE_CUDA()	via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
129 #define ADB_SET_STATE_CMD()		via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
130 #define ADB_SET_STATE_EVEN()		via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
131 						vBufB) | vPB4) & ~vPB5)
132 #define ADB_SET_STATE_ODD()		via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
133 						vBufB) | vPB5) & ~vPB4)
134 #define ADB_SET_STATE_ACTIVE() 		via_reg(VIA1, vBufB) |= vPB5
135 #define ADB_SET_STATE_INACTIVE()	via_reg(VIA1, vBufB) &= ~vPB5
136 #define ADB_SET_STATE_TIP()		via_reg(VIA1, vBufB) &= ~vPB5
137 #define ADB_CLR_STATE_TIP() 		via_reg(VIA1, vBufB) |= vPB5
138 #define ADB_SET_STATE_ACKON()		via_reg(VIA1, vBufB) |= vPB4
139 #define ADB_SET_STATE_ACKOFF()		via_reg(VIA1, vBufB) &= ~vPB4
140 #define ADB_TOGGLE_STATE_ACK_CUDA()	via_reg(VIA1, vBufB) ^= vPB4
141 #define ADB_SET_STATE_ACKON_CUDA()	via_reg(VIA1, vBufB) &= ~vPB4
142 #define ADB_SET_STATE_ACKOFF_CUDA()	via_reg(VIA1, vBufB) |= vPB4
143 #define ADB_SET_SR_INPUT()		via_reg(VIA1, vACR) &= ~vSR_OUT
144 #define ADB_SET_SR_OUTPUT()		via_reg(VIA1, vACR) |= vSR_OUT
145 #define ADB_SR()			via_reg(VIA1, vSR)
146 #define ADB_VIA_INTR_ENABLE()		via_reg(VIA1, vIER) = 0x84
147 #define ADB_VIA_INTR_DISABLE()		via_reg(VIA1, vIER) = 0x04
148 #define ADB_VIA_CLR_INTR()		via_reg(VIA1, vIFR) = 0x04
149 #define ADB_INTR_IS_OFF			(vPB3 == (via_reg(VIA1, vBufB) & vPB3))
150 #define ADB_INTR_IS_ON			(0 == (via_reg(VIA1, vBufB) & vPB3))
151 #define ADB_SR_INTR_IS_OFF		(0 == (via_reg(VIA1, vIFR) & vSR_INT))
152 #define ADB_SR_INTR_IS_ON		(vSR_INT == (via_reg(VIA1, \
153 						vIFR) & vSR_INT))
154 
155 /*
156  * This is the delay that is required (in uS) between certain
157  * ADB transactions. The actual timing delay for for each uS is
158  * calculated at boot time to account for differences in machine speed.
159  */
160 #define ADB_DELAY	150
161 
162 /*
163  * Maximum ADB message length; includes space for data, result, and
164  * device code - plus a little for safety.
165  */
166 #define ADB_MAX_MSG_LENGTH	16
167 #define ADB_MAX_HDR_LENGTH	8
168 
169 #define ADB_QUEUE		32
170 #define ADB_TICKLE_TICKS	4
171 
172 /*
173  * A structure for storing information about each ADB device.
174  */
175 struct ADBDevEntry {
176 	void	(*ServiceRtPtr) __P((void));
177 	void	*DataAreaAddr;
178 	int	devType;
179 	int	origAddr;
180 	int	currentAddr;
181 };
182 
183 /*
184  * Used to hold ADB commands that are waiting to be sent out.
185  */
186 struct adbCmdHoldEntry {
187 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
188 	u_char	*saveBuf;	/* buffer to know where to save result */
189 	u_char	*compRout;	/* completion routine pointer */
190 	u_char	*data;		/* completion routine data pointer */
191 };
192 
193 /*
194  * Eventually used for two separate queues, the queue between
195  * the upper and lower halves, and the outgoing packet queue.
196  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
197  */
198 struct adbCommand {
199 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
200 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
201 	u_char	*saveBuf;	/* where to save result */
202 	u_char	*compRout;	/* completion routine pointer */
203 	u_char	*compData;	/* completion routine data pointer */
204 	u_int	cmd;		/* the original command for this data */
205 	u_int	unsol;		/* 1 if packet was unsolicited */
206 	u_int	ack_only;	/* 1 for no special processing */
207 };
208 
209 /*
210  * Text representations of each hardware class
211  */
212 char	*adbHardwareDescr[MAX_ADB_HW + 1] = {
213 	"unknown",
214 	"II series",
215 	"IIsi series",
216 	"PowerBook",
217 	"Cuda",
218 	"IOP",
219 };
220 
221 /*
222  * A few variables that we need and their initial values.
223  */
224 int	adbHardware = ADB_HW_UNKNOWN;
225 int	adbActionState = ADB_ACTION_NOTREADY;
226 int	adbBusState = ADB_BUS_UNKNOWN;
227 int	adbWaiting = 0;		/* waiting for return data from the device */
228 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
229 int	adbOutQueueHasData = 0;	/* something in the queue waiting to go out */
230 int	adbNextEnd = 0;		/* the next incoming bute is the last (II) */
231 int	adbSoftPower = 0;	/* machine supports soft power */
232 
233 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
234 u_char	*adbBuffer = (long)0;	/* pointer to user data area */
235 void	*adbCompRout = (long)0;	/* pointer to the completion routine */
236 void	*adbCompData = (long)0;	/* pointer to the completion routine data */
237 long	adbFakeInts = 0;	/* keeps track of fake ADB interrupts for
238 				 * timeouts (II) */
239 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
240 int	adbSendTalk = 0;	/* the intr routine is sending the talk, not
241 				 * the user (II) */
242 int	adbPolling = 0;		/* we are polling for service request */
243 int	adbPollCmd = 0;		/* the last poll command we sent */
244 
245 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
246 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
247 struct	adbCmdHoldEntry adbOutQueue;		/* our 1 entry output queue */
248 
249 int	adbSentChars = 0;	/* how many characters we have sent */
250 int	adbLastDevice = 0;	/* last ADB dev we heard from (II ONLY) */
251 int	adbLastDevIndex = 0;	/* last ADB dev loc in dev table (II ONLY) */
252 int	adbLastCommand = 0;	/* the last ADB command we sent (II) */
253 
254 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
255 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
256 
257 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
258 volatile int	adbInCount = 0;		/* how many packets in in queue */
259 int	adbInHead = 0;			/* head of in queue */
260 int	adbInTail = 0;			/* tail of in queue */
261 struct	adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
262 int	adbOutCount = 0;		/* how many packets in out queue */
263 int	adbOutHead = 0;			/* head of out queue */
264 int	adbOutTail = 0;			/* tail of out queue */
265 
266 int	tickle_count = 0;		/* how many tickles seen for this packet? */
267 int	tickle_serial = 0;		/* the last packet tickled */
268 int	adb_cuda_serial = 0;		/* the current packet */
269 
270 struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
271 
272 extern struct mac68k_machine_S mac68k_machine;
273 
274 void	pm_setup_adb __P((void));
275 void	pm_hw_setup __P((void));
276 void	pm_check_adb_devices __P((int));
277 void	pm_intr __P((void *));
278 int	pm_adb_op __P((u_char *, void *, void *, int));
279 void	pm_init_adb_device __P((void));
280 
281 /*
282  * The following are private routines.
283  */
284 #ifdef ADB_DEBUG
285 void	print_single __P((u_char *));
286 #endif
287 void	adb_intr __P((void *));
288 void	adb_intr_II __P((void *));
289 void	adb_intr_IIsi __P((void *));
290 void	adb_intr_cuda __P((void *));
291 void	adb_soft_intr __P((void));
292 int	send_adb_II __P((u_char *, u_char *, void *, void *, int));
293 int	send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
294 int	send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
295 void	adb_intr_cuda_test __P((void));
296 void	adb_cuda_tickle __P((void));
297 void	adb_pass_up __P((struct adbCommand *));
298 void	adb_op_comprout __P((void));
299 void	adb_reinit __P((void));
300 int	count_adbs __P((void));
301 int	get_ind_adb_info __P((ADBDataBlock *, int));
302 int	get_adb_info __P((ADBDataBlock *, int));
303 int	set_adb_info __P((ADBSetInfoBlock *, int));
304 void	adb_setup_hw_type __P((void));
305 int	adb_op __P((Ptr, Ptr, Ptr, short));
306 int	adb_op_sync __P((Ptr, Ptr, Ptr, short));
307 void	adb_read_II __P((u_char *));
308 void	adb_hw_setup __P((void));
309 void	adb_hw_setup_IIsi __P((u_char *));
310 void	adb_comp_exec __P((void));
311 int	adb_cmd_result __P((u_char *));
312 int	adb_cmd_extra __P((u_char *));
313 int	adb_guess_next_device __P((void));
314 int	adb_prog_switch_enable __P((void));
315 int	adb_prog_switch_disable __P((void));
316 /* we should create this and it will be the public version */
317 int	send_adb __P((u_char *, void *, void *));
318 void	adb_iop_recv __P((IOP *, struct iop_msg *));
319 int	send_adb_iop __P((int, u_char *, void *, void *));
320 
321 #ifdef ADB_DEBUG
322 /*
323  * print_single
324  * Diagnostic display routine. Displays the hex values of the
325  * specified elements of the u_char. The length of the "string"
326  * is in [0].
327  */
328 void
329 print_single(str)
330 	u_char *str;
331 {
332 	int x;
333 
334 	if (str == 0) {
335 		printf_intr("no data - null pointer\n");
336 		return;
337 	}
338 	if (*str == 0) {
339 		printf_intr("nothing returned\n");
340 		return;
341 	}
342 	if (*str > 20) {
343 		printf_intr("ADB: ACK > 20 no way!\n");
344 		*str = (u_char)20;
345 	}
346 	printf_intr("(length=0x%x):", (u_int)*str);
347 	for (x = 1; x <= *str; x++)
348 		printf_intr("  0x%02x", (u_int)*(str + x));
349 	printf_intr("\n");
350 }
351 #endif
352 
353 void
354 adb_cuda_tickle(void)
355 {
356 	volatile int s;
357 
358 	if (adbActionState == ADB_ACTION_IN) {
359 		if (tickle_serial == adb_cuda_serial) {
360 			if (++tickle_count > 0) {
361 				s = splhigh();
362 				adbActionState = ADB_ACTION_IDLE;
363 				adbInputBuffer[0] = 0;
364 				ADB_SET_STATE_IDLE_CUDA();
365 				splx(s);
366 			}
367 		} else {
368 			tickle_serial = adb_cuda_serial;
369 			tickle_count = 0;
370 		}
371 	} else {
372 		tickle_serial = adb_cuda_serial;
373 		tickle_count = 0;
374 	}
375 
376 	callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
377 	    (void *)adb_cuda_tickle, NULL);
378 }
379 
380 /*
381  * called when when an adb interrupt happens
382  *
383  * Cuda version of adb_intr
384  * TO DO: do we want to add some calls to intr_dispatch() here to
385  * grab serial interrupts?
386  */
387 void
388 adb_intr_cuda(void *arg)
389 {
390 	volatile int i, ending;
391 	volatile unsigned int s;
392 	struct adbCommand packet;
393 
394 	s = splhigh();		/* can't be too careful - might be called */
395 	/* from a routine, NOT an interrupt */
396 
397 	ADB_VIA_CLR_INTR();	/* clear interrupt */
398 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
399 
400 switch_start:
401 	switch (adbActionState) {
402 	case ADB_ACTION_IDLE:
403 		/*
404 		 * This is an unexpected packet, so grab the first (dummy)
405 		 * byte, set up the proper vars, and tell the chip we are
406 		 * starting to receive the packet by setting the TIP bit.
407 		 */
408 		adbInputBuffer[1] = ADB_SR();
409 		adb_cuda_serial++;
410 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
411 			break;
412 
413 		ADB_SET_SR_INPUT();
414 		ADB_SET_STATE_TIP();
415 
416 		adbInputBuffer[0] = 1;
417 		adbActionState = ADB_ACTION_IN;
418 #ifdef ADB_DEBUG
419 		if (adb_debug)
420 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
421 #endif
422 		break;
423 
424 	case ADB_ACTION_IN:
425 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
426 		/* intr off means this is the last byte (end of frame) */
427 		if (ADB_INTR_IS_OFF)
428 			ending = 1;
429 		else
430 			ending = 0;
431 
432 		if (1 == ending) {	/* end of message? */
433 #ifdef ADB_DEBUG
434 			if (adb_debug) {
435 				printf_intr("in end 0x%02x ",
436 				    adbInputBuffer[adbInputBuffer[0]]);
437 				print_single(adbInputBuffer);
438 			}
439 #endif
440 
441 			/*
442 			 * Are we waiting AND does this packet match what we
443 			 * are waiting for AND is it coming from either the
444 			 * ADB or RTC/PRAM sub-device? This section _should_
445 			 * recognize all ADB and RTC/PRAM type commands, but
446 			 * there may be more... NOTE: commands are always at
447 			 * [4], even for RTC/PRAM commands.
448 			 */
449 			/* set up data for adb_pass_up */
450 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
451 
452 			if ((adbWaiting == 1) &&
453 			    (adbInputBuffer[4] == adbWaitingCmd) &&
454 			    ((adbInputBuffer[2] == 0x00) ||
455 			    (adbInputBuffer[2] == 0x01))) {
456 				packet.saveBuf = adbBuffer;
457 				packet.compRout = adbCompRout;
458 				packet.compData = adbCompData;
459 				packet.unsol = 0;
460 				packet.ack_only = 0;
461 				adb_pass_up(&packet);
462 
463 				adbWaitingCmd = 0;	/* reset "waiting" vars */
464 				adbWaiting = 0;
465 				adbBuffer = (long)0;
466 				adbCompRout = (long)0;
467 				adbCompData = (long)0;
468 			} else {
469 				packet.unsol = 1;
470 				packet.ack_only = 0;
471 				adb_pass_up(&packet);
472 			}
473 
474 
475 			/* reset vars and signal the end of this frame */
476 			adbActionState = ADB_ACTION_IDLE;
477 			adbInputBuffer[0] = 0;
478 			ADB_SET_STATE_IDLE_CUDA();
479 			/*ADB_SET_SR_INPUT();*/
480 
481 			/*
482 			 * If there is something waiting to be sent out,
483 			 * the set everything up and send the first byte.
484 			 */
485 			if (adbWriteDelay == 1) {
486 				delay(ADB_DELAY);	/* required */
487 				adbSentChars = 0;
488 				adbActionState = ADB_ACTION_OUT;
489 				/*
490 				 * If the interrupt is on, we were too slow
491 				 * and the chip has already started to send
492 				 * something to us, so back out of the write
493 				 * and start a read cycle.
494 				 */
495 				if (ADB_INTR_IS_ON) {
496 					ADB_SET_SR_INPUT();
497 					ADB_SET_STATE_IDLE_CUDA();
498 					adbSentChars = 0;
499 					adbActionState = ADB_ACTION_IDLE;
500 					adbInputBuffer[0] = 0;
501 					break;
502 				}
503 				/*
504 				 * If we got here, it's ok to start sending
505 				 * so load the first byte and tell the chip
506 				 * we want to send.
507 				 */
508 				ADB_SET_STATE_TIP();
509 				ADB_SET_SR_OUTPUT();
510 				ADB_SR() = adbOutputBuffer[adbSentChars + 1];
511 			}
512 		} else {
513 			ADB_TOGGLE_STATE_ACK_CUDA();
514 #ifdef ADB_DEBUG
515 			if (adb_debug)
516 				printf_intr("in 0x%02x ",
517 				    adbInputBuffer[adbInputBuffer[0]]);
518 #endif
519 		}
520 		break;
521 
522 	case ADB_ACTION_OUT:
523 		i = ADB_SR();	/* reset SR-intr in IFR */
524 #ifdef ADB_DEBUG
525 		if (adb_debug)
526 			printf_intr("intr out 0x%02x ", i);
527 #endif
528 
529 		adbSentChars++;
530 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
531 #ifdef ADB_DEBUG
532 			if (adb_debug)
533 				printf_intr("intr was on ");
534 #endif
535 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
536 			ADB_SET_STATE_IDLE_CUDA();
537 			adbSentChars = 0;	/* must start all over */
538 			adbActionState = ADB_ACTION_IDLE;	/* new state */
539 			adbInputBuffer[0] = 0;
540 			adbWriteDelay = 1;	/* must retry when done with
541 						 * read */
542 			delay(ADB_DELAY);
543 			goto switch_start;	/* process next state right
544 						 * now */
545 			break;
546 		}
547 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
548 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
549 									 * back? */
550 				adbWaiting = 1;	/* signal waiting for return */
551 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
552 			} else {	/* no talk, so done */
553 				/* set up stuff for adb_pass_up */
554 				memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
555 				packet.saveBuf = adbBuffer;
556 				packet.compRout = adbCompRout;
557 				packet.compData = adbCompData;
558 				packet.cmd = adbWaitingCmd;
559 				packet.unsol = 0;
560 				packet.ack_only = 1;
561 				adb_pass_up(&packet);
562 
563 				/* reset "waiting" vars, just in case */
564 				adbWaitingCmd = 0;
565 				adbBuffer = (long)0;
566 				adbCompRout = (long)0;
567 				adbCompData = (long)0;
568 			}
569 
570 			adbWriteDelay = 0;	/* done writing */
571 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
572 			ADB_SET_SR_INPUT();
573 			ADB_SET_STATE_IDLE_CUDA();
574 #ifdef ADB_DEBUG
575 			if (adb_debug)
576 				printf_intr("write done ");
577 #endif
578 		} else {
579 			ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* send next byte */
580 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
581 							 * shift */
582 #ifdef ADB_DEBUG
583 			if (adb_debug)
584 				printf_intr("toggle ");
585 #endif
586 		}
587 		break;
588 
589 	case ADB_ACTION_NOTREADY:
590 #ifdef ADB_DEBUG
591 		if (adb_debug)
592 			printf_intr("adb: not yet initialized\n");
593 #endif
594 		break;
595 
596 	default:
597 #ifdef ADB_DEBUG
598 		if (adb_debug)
599 			printf_intr("intr: unknown ADB state\n");
600 #endif
601 	}
602 
603 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
604 
605 	splx(s);		/* restore */
606 
607 	return;
608 }				/* end adb_intr_cuda */
609 
610 
611 int
612 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
613 	command)
614 {
615 	int s, len;
616 
617 #ifdef ADB_DEBUG
618 	if (adb_debug)
619 		printf_intr("SEND\n");
620 #endif
621 
622 	if (adbActionState == ADB_ACTION_NOTREADY)
623 		return 1;
624 
625 	/* Don't interrupt while we are messing with the ADB */
626 	s = splhigh();
627 
628 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
629 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupt? */
630 	} else
631 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
632 			adbWriteDelay = 1;	/* if no, then we'll "queue"
633 						 * it up */
634 		else {
635 			splx(s);
636 			return 1;	/* really busy! */
637 		}
638 
639 #ifdef ADB_DEBUG
640 	if (adb_debug)
641 		printf_intr("QUEUE\n");
642 #endif
643 	if ((long)in == (long)0) {	/* need to convert? */
644 		/*
645 		 * Don't need to use adb_cmd_extra here because this section
646 		 * will be called ONLY when it is an ADB command (no RTC or
647 		 * PRAM)
648 		 */
649 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
650 						 * doing a listen! */
651 			len = buffer[0];	/* length of additional data */
652 		else
653 			len = 0;/* no additional data */
654 
655 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
656 						 * data */
657 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
658 		adbOutputBuffer[2] = (u_char)command;	/* load command */
659 
660 		/* copy additional output data, if any */
661 		memcpy(adbOutputBuffer + 3, buffer + 1, len);
662 	} else
663 		/* if data ready, just copy over */
664 		memcpy(adbOutputBuffer, in, in[0] + 2);
665 
666 	adbSentChars = 0;	/* nothing sent yet */
667 	adbBuffer = buffer;	/* save buffer to know where to save result */
668 	adbCompRout = compRout;	/* save completion routine pointer */
669 	adbCompData = data;	/* save completion routine data pointer */
670 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
671 
672 	if (adbWriteDelay != 1) {	/* start command now? */
673 #ifdef ADB_DEBUG
674 		if (adb_debug)
675 			printf_intr("out start NOW");
676 #endif
677 		delay(ADB_DELAY);
678 		adbActionState = ADB_ACTION_OUT;	/* set next state */
679 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
680 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
681 		ADB_SET_STATE_ACKOFF_CUDA();
682 		ADB_SET_STATE_TIP();	/* tell ADB that we want to send */
683 	}
684 	adbWriteDelay = 1;	/* something in the write "queue" */
685 
686 	splx(s);
687 
688 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked? */
689 		/* poll until byte done */
690 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
691 		    || (adbWaiting == 1))
692 			if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
693 				adb_intr_cuda(NULL); /* go process it */
694 				if (adb_polling)
695 					adb_soft_intr();
696 			}
697 
698 	return 0;
699 }				/* send_adb_cuda */
700 
701 
702 void
703 adb_intr_II(void *arg)
704 {
705 	struct adbCommand packet;
706 	int i, intr_on = 0;
707 	int send = 0;
708 	unsigned int s;
709 
710 	s = splhigh();		/* can't be too careful - might be called */
711 	/* from a routine, NOT an interrupt */
712 
713 	ADB_VIA_CLR_INTR();	/* clear interrupt */
714 
715 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
716 
717 	delay(ADB_DELAY);	/* yuck (don't remove) */
718 
719 	(void)intr_dispatch(0x70); /* grab any serial interrupts */
720 
721 	if (ADB_INTR_IS_ON)
722 		intr_on = 1;	/* save for later */
723 
724 switch_start:
725 	switch (adbActionState) {
726 	case ADB_ACTION_POLLING:
727 		if (!intr_on) {
728 			if (adbOutQueueHasData) {
729 #ifdef ADB_DEBUG
730 				if (adb_debug & 0x80)
731 					printf_intr("POLL-doing-out-queue. ");
732 #endif
733 				ADB_SET_STATE_IDLE_II();
734 				delay(ADB_DELAY);
735 
736 				/* copy over data */
737 				memcpy(adbOutputBuffer, adbOutQueue.outBuf,
738 				    adbOutQueue.outBuf[0] + 2);
739 
740 				adbBuffer = adbOutQueue.saveBuf;	/* user data area */
741 				adbCompRout = adbOutQueue.compRout;	/* completion routine */
742 				adbCompData = adbOutQueue.data;	/* comp. rout. data */
743 				adbOutQueueHasData = 0;	/* currently processing
744 							 * "queue" entry */
745 				adbSentChars = 0;	/* nothing sent yet */
746 				adbActionState = ADB_ACTION_OUT;	/* set next state */
747 				ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
748 				ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
749 				adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
750 				ADB_SET_STATE_CMD();	/* tell ADB that we want to send */
751 				break;
752 			} else {
753 #ifdef ADB_DEBUG
754 				if (adb_debug)
755 					printf_intr("pIDLE ");
756 #endif
757 				adbActionState = ADB_ACTION_IDLE;
758 			}
759 		} else {
760 #ifdef ADB_DEBUG
761 			if (adb_debug & 0x80)
762 				printf_intr("pIN ");
763 #endif
764 			adbActionState = ADB_ACTION_IN;
765 		}
766 		delay(ADB_DELAY);
767 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
768 		goto switch_start;
769 		break;
770 	case ADB_ACTION_IDLE:
771 		if (!intr_on) {
772 			i = ADB_SR();
773 			adbBusState = ADB_BUS_IDLE;
774 			adbActionState = ADB_ACTION_IDLE;
775 			ADB_SET_STATE_IDLE_II();
776 			break;
777 		}
778 		adbInputBuffer[0] = 1;
779 		adbInputBuffer[1] = ADB_SR();	/* get first byte */
780 #ifdef ADB_DEBUG
781 		if (adb_debug & 0x80)
782 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
783 #endif
784 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
785 		adbActionState = ADB_ACTION_IN;	/* set next state */
786 		ADB_SET_STATE_EVEN();	/* set bus state to even */
787 		adbBusState = ADB_BUS_EVEN;
788 		break;
789 
790 	case ADB_ACTION_IN:
791 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();	/* get byte */
792 #ifdef ADB_DEBUG
793 		if (adb_debug & 0x80)
794 			printf_intr("in 0x%02x ",
795 			    adbInputBuffer[adbInputBuffer[0]]);
796 #endif
797 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
798 
799 		if (intr_on) {	/* process last byte of packet */
800 			adbInputBuffer[0]--;	/* minus one */
801 			/*
802 			 * If intr_on was true, and it's the second byte, then
803 			 * the byte we just discarded is really valid, so
804 			 * adjust the count
805 			 */
806 			if (adbInputBuffer[0] == 2) {
807 				adbInputBuffer[0]++;
808 			}
809 
810 #ifdef ADB_DEBUG
811 			if (adb_debug & 0x80) {
812 				printf_intr("done: ");
813 				print_single(adbInputBuffer);
814 			}
815 #endif
816 
817 			adbLastDevice = ADB_CMDADDR(adbInputBuffer[1]);
818 
819 			if (adbInputBuffer[0] == 1 && !adbWaiting) {	/* SRQ!!!*/
820 #ifdef ADB_DEBUG
821 				if (adb_debug & 0x80)
822 					printf_intr(" xSRQ! ");
823 #endif
824 				adb_guess_next_device();
825 #ifdef ADB_DEBUG
826 				if (adb_debug & 0x80)
827 					printf_intr("try 0x%0x ",
828 					    adbLastDevice);
829 #endif
830 				adbOutputBuffer[0] = 1;
831 				adbOutputBuffer[1] = ADBTALK(adbLastDevice, 0);
832 
833 				adbSentChars = 0;	/* nothing sent yet */
834 				adbActionState = ADB_ACTION_POLLING;	/* set next state */
835 				ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
836 				ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
837 				adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
838 				ADB_SET_STATE_CMD();	/* tell ADB that we want to */
839 				break;
840 			}
841 
842 			/* set up data for adb_pass_up */
843 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
844 
845 			if (!adbWaiting && (adbInputBuffer[0] != 0)) {
846 				packet.unsol = 1;
847 				packet.ack_only = 0;
848 				adb_pass_up(&packet);
849 			} else {
850 				packet.saveBuf = adbBuffer;
851 				packet.compRout = adbCompRout;
852 				packet.compData = adbCompData;
853 				packet.unsol = 0;
854 				packet.ack_only = 0;
855 				adb_pass_up(&packet);
856 			}
857 
858 			adbWaiting = 0;
859 			adbInputBuffer[0] = 0;
860 			adbBuffer = (long)0;
861 			adbCompRout = (long)0;
862 			adbCompData = (long)0;
863 			/*
864 			 * Since we are done, check whether there is any data
865 			 * waiting to do out. If so, start the sending the data.
866 			 */
867 			if (adbOutQueueHasData == 1) {
868 #ifdef ADB_DEBUG
869 				if (adb_debug & 0x80)
870 					printf_intr("XXX: DOING OUT QUEUE\n");
871 #endif
872 				/* copy over data */
873 				memcpy(adbOutputBuffer, adbOutQueue.outBuf,
874 				    adbOutQueue.outBuf[0] + 2);
875 				adbBuffer = adbOutQueue.saveBuf;	/* user data area */
876 				adbCompRout = adbOutQueue.compRout;	/* completion routine */
877 				adbCompData = adbOutQueue.data;	/* comp. rout. data */
878 				adbOutQueueHasData = 0;	/* currently processing
879 							 * "queue" entry */
880 				send = 1;
881 			} else {
882 #ifdef ADB_DEBUG
883 				if (adb_debug & 0x80)
884 					printf_intr("XXending ");
885 #endif
886 				adb_guess_next_device();
887 				adbOutputBuffer[0] = 1;
888 				adbOutputBuffer[1] = ((adbLastDevice & 0x0f) << 4) | 0x0c;
889 				adbSentChars = 0;	/* nothing sent yet */
890 				adbActionState = ADB_ACTION_POLLING;	/* set next state */
891 				ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
892 				ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
893 				adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
894 				ADB_SET_STATE_CMD();	/* tell ADB that we want to */
895 				break;
896 			}
897 		}
898 
899 		/*
900 		 * If send is true then something above determined that
901 		 * the message has ended and we need to start sending out
902 		 * a new message immediately. This could be because there
903 		 * is data waiting to go out or because an SRQ was seen.
904 		 */
905 		if (send) {
906 			adbSentChars = 0;	/* nothing sent yet */
907 			adbActionState = ADB_ACTION_OUT;	/* set next state */
908 			ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
909 			ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
910 			adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
911 			ADB_SET_STATE_CMD();	/* tell ADB that we want to
912 						 * send */
913 			break;
914 		}
915 		/* We only get this far if the message hasn't ended yet. */
916 		switch (adbBusState) {	/* set to next state */
917 		case ADB_BUS_EVEN:
918 			ADB_SET_STATE_ODD();	/* set state to odd */
919 			adbBusState = ADB_BUS_ODD;
920 			break;
921 
922 		case ADB_BUS_ODD:
923 			ADB_SET_STATE_EVEN();	/* set state to even */
924 			adbBusState = ADB_BUS_EVEN;
925 			break;
926 		default:
927 			printf_intr("strange state!!!\n");	/* huh? */
928 			break;
929 		}
930 		break;
931 
932 	case ADB_ACTION_OUT:
933 		i = ADB_SR();	/* clear interrupt */
934 		adbSentChars++;
935 		/*
936 		 * If the outgoing data was a TALK, we must
937 		 * switch to input mode to get the result.
938 		 */
939 		if ((adbOutputBuffer[1] & 0x0c) == 0x0c) {
940 			adbInputBuffer[0] = 1;
941 			adbInputBuffer[1] = i;
942 			adbActionState = ADB_ACTION_IN;
943 			ADB_SET_SR_INPUT();
944 			adbBusState = ADB_BUS_EVEN;
945 			ADB_SET_STATE_EVEN();
946 #ifdef ADB_DEBUG
947 			if (adb_debug & 0x80)
948 				printf_intr("talk out 0x%02x ", i);
949 #endif
950 			/* we want something back */
951 			adbWaiting = 1;
952 			break;
953 		}
954 		/*
955 		 * If it's not a TALK, check whether all data has been sent.
956 		 * If so, call the completion routine and clean up. If not,
957 		 * advance to the next state.
958 		 */
959 #ifdef ADB_DEBUG
960 		if (adb_debug & 0x80)
961 			printf_intr("non-talk out 0x%0x ", i);
962 #endif
963 		ADB_SET_SR_OUTPUT();
964 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
965 #ifdef ADB_DEBUG
966 			if (adb_debug & 0x80)
967 				printf_intr("done \n");
968 #endif
969 			/* set up stuff for adb_pass_up */
970 			memcpy(packet.data, adbOutputBuffer, adbOutputBuffer[0] + 1);
971 			packet.saveBuf = adbBuffer;
972 			packet.compRout = adbCompRout;
973 			packet.compData = adbCompData;
974 			packet.cmd = adbWaitingCmd;
975 			packet.unsol = 0;
976 			packet.ack_only = 1;
977 			adb_pass_up(&packet);
978 
979 			/* reset "waiting" vars, just in case */
980 			adbBuffer = (long)0;
981 			adbCompRout = (long)0;
982 			adbCompData = (long)0;
983 			if (adbOutQueueHasData == 1) {
984 				/* copy over data */
985 				memcpy(adbOutputBuffer, adbOutQueue.outBuf,
986 				    adbOutQueue.outBuf[0] + 2);
987 				adbBuffer = adbOutQueue.saveBuf;	/* user data area */
988 				adbCompRout = adbOutQueue.compRout;	/* completion routine */
989 				adbCompData = adbOutQueue.data;	/* comp. rout. data */
990 				adbOutQueueHasData = 0;	/* currently processing
991 							 * "queue" entry */
992 				adbSentChars = 0;	/* nothing sent yet */
993 				adbActionState = ADB_ACTION_OUT;	/* set next state */
994 				ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
995 				ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
996 				adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
997 				ADB_SET_STATE_CMD();	/* tell ADB that we want to
998 							 * send */
999 				break;
1000 			} else {
1001 				/* send talk to last device instead */
1002 				adbOutputBuffer[0] = 1;
1003 				adbOutputBuffer[1] =
1004 				    ADBTALK(ADB_CMDADDR(adbOutputBuffer[1]), 0);
1005 
1006 				adbSentChars = 0;	/* nothing sent yet */
1007 				adbActionState = ADB_ACTION_IDLE;	/* set next state */
1008 				ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1009 				ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
1010 				adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
1011 				ADB_SET_STATE_CMD();	/* tell ADB that we want to */
1012 				break;
1013 			}
1014 		}
1015 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];
1016 		switch (adbBusState) {	/* advance to next state */
1017 		case ADB_BUS_EVEN:
1018 			ADB_SET_STATE_ODD();	/* set state to odd */
1019 			adbBusState = ADB_BUS_ODD;
1020 			break;
1021 
1022 		case ADB_BUS_CMD:
1023 		case ADB_BUS_ODD:
1024 			ADB_SET_STATE_EVEN();	/* set state to even */
1025 			adbBusState = ADB_BUS_EVEN;
1026 			break;
1027 
1028 		default:
1029 #ifdef ADB_DEBUG
1030 			if (adb_debug) {
1031 				printf_intr("strange state!!! (0x%x)\n",
1032 				    adbBusState);
1033 			}
1034 #endif
1035 			break;
1036 		}
1037 		break;
1038 
1039 	default:
1040 #ifdef ADB_DEBUG
1041 		if (adb_debug)
1042 			printf_intr("adb: unknown ADB state (during intr)\n");
1043 #endif
1044 	}
1045 
1046 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
1047 
1048 	splx(s);		/* restore */
1049 
1050 	return;
1051 
1052 }
1053 
1054 
1055 /*
1056  * send_adb version for II series machines
1057  */
1058 int
1059 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
1060 {
1061 	int s, len;
1062 
1063 	if (adbActionState == ADB_ACTION_NOTREADY)	/* return if ADB not
1064 							 * available */
1065 		return 1;
1066 
1067 	/* Don't interrupt while we are messing with the ADB */
1068 	s = splhigh();
1069 
1070 	if (0 != adbOutQueueHasData) {	/* right now, "has data" means "full" */
1071 		splx(s);	/* sorry, try again later */
1072 		return 1;
1073 	}
1074 	if ((long)in == (long)0) {	/* need to convert? */
1075 		/*
1076 		 * Don't need to use adb_cmd_extra here because this section
1077 		 * will be called ONLY when it is an ADB command (no RTC or
1078 		 * PRAM), especially on II series!
1079 		 */
1080 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
1081 						 * doing a listen! */
1082 			len = buffer[0];	/* length of additional data */
1083 		else
1084 			len = 0;/* no additional data */
1085 
1086 		adbOutQueue.outBuf[0] = 1 + len;	/* command + addl. data */
1087 		adbOutQueue.outBuf[1] = (u_char)command;	/* load command */
1088 
1089 		/* copy additional output data, if any */
1090 		memcpy(adbOutQueue.outBuf + 2, buffer + 1, len);
1091 	} else
1092 		/* if data ready, just copy over */
1093 		memcpy(adbOutQueue.outBuf, in, in[0] + 2);
1094 
1095 	adbOutQueue.saveBuf = buffer;	/* save buffer to know where to save
1096 					 * result */
1097 	adbOutQueue.compRout = compRout;	/* save completion routine
1098 						 * pointer */
1099 	adbOutQueue.data = data;/* save completion routine data pointer */
1100 
1101 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* is ADB available? */
1102 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupts? */
1103 		/* then start command now */
1104 		memcpy(adbOutputBuffer, adbOutQueue.outBuf,
1105 		    adbOutQueue.outBuf[0] + 2);		/* copy over data */
1106 
1107 		adbBuffer = adbOutQueue.saveBuf;	/* pointer to user data
1108 							 * area */
1109 		adbCompRout = adbOutQueue.compRout;	/* pointer to the
1110 							 * completion routine */
1111 		adbCompData = adbOutQueue.data;	/* pointer to the completion
1112 						 * routine data */
1113 
1114 		adbSentChars = 0;	/* nothing sent yet */
1115 		adbActionState = ADB_ACTION_OUT;	/* set next state */
1116 		adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
1117 
1118 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1119 
1120 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
1121 		ADB_SET_STATE_CMD();	/* tell ADB that we want to send */
1122 		adbOutQueueHasData = 0;	/* currently processing "queue" entry */
1123 	} else
1124 		adbOutQueueHasData = 1;	/* something in the write "queue" */
1125 
1126 	splx(s);
1127 
1128 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked? */
1129 		/* poll until message done */
1130 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1131 		    || (adbWaiting == 1))
1132 			if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
1133 				adb_intr_II(NULL); /* go process it */
1134 				if (adb_polling)
1135 					adb_soft_intr();
1136 			}
1137 
1138 	return 0;
1139 }
1140 
1141 
1142 /*
1143  * This routine is called from the II series interrupt routine
1144  * to determine what the "next" device is that should be polled.
1145  */
1146 int
1147 adb_guess_next_device(void)
1148 {
1149 	int last, i, dummy;
1150 
1151 	if (adbStarting) {
1152 		/*
1153 		 * Start polling EVERY device, since we can't be sure there is
1154 		 * anything in the device table yet
1155 		 */
1156 		if (adbLastDevice < 1 || adbLastDevice > 15)
1157 			adbLastDevice = 1;
1158 		if (++adbLastDevice > 15)	/* point to next one */
1159 			adbLastDevice = 1;
1160 	} else {
1161 		/* find the next device using the device table */
1162 		if (adbLastDevice < 1 || adbLastDevice > 15)	/* let's be parinoid */
1163 			adbLastDevice = 2;
1164 		last = 1;	/* default index location */
1165 
1166 		for (i = 1; i < 16; i++)	/* find index entry */
1167 			if (ADBDevTable[i].currentAddr == adbLastDevice) {	/* look for device */
1168 				last = i;	/* found it */
1169 				break;
1170 			}
1171 		dummy = last;	/* index to start at */
1172 		for (;;) {	/* find next device in index */
1173 			if (++dummy > 15)	/* wrap around if needed */
1174 				dummy = 1;
1175 			if (dummy == last) {	/* didn't find any other
1176 						 * device! This can happen if
1177 						 * there are no devices on the
1178 						 * bus */
1179 				dummy = 1;
1180 				break;
1181 			}
1182 			/* found the next device */
1183 			if (ADBDevTable[dummy].devType != 0)
1184 				break;
1185 		}
1186 		adbLastDevice = ADBDevTable[dummy].currentAddr;
1187 	}
1188 	return adbLastDevice;
1189 }
1190 
1191 
1192 /*
1193  * Called when when an adb interrupt happens.
1194  * This routine simply transfers control over to the appropriate
1195  * code for the machine we are running on.
1196  */
1197 void
1198 adb_intr(void *arg)
1199 {
1200 	switch (adbHardware) {
1201 	case ADB_HW_II:
1202 		adb_intr_II(arg);
1203 		break;
1204 
1205 	case ADB_HW_IISI:
1206 		adb_intr_IIsi(arg);
1207 		break;
1208 
1209 	case ADB_HW_PB:		/* Should not come through here. */
1210 		break;
1211 
1212 	case ADB_HW_CUDA:
1213 		adb_intr_cuda(arg);
1214 		break;
1215 
1216 	case ADB_HW_IOP:	/* Should not come through here. */
1217 		break;
1218 
1219 	case ADB_HW_UNKNOWN:
1220 		break;
1221 	}
1222 }
1223 
1224 
1225 /*
1226  * called when when an adb interrupt happens
1227  *
1228  * IIsi version of adb_intr
1229  *
1230  */
1231 void
1232 adb_intr_IIsi(void *arg)
1233 {
1234 	struct adbCommand packet;
1235 	int i, ending;
1236 	unsigned int s;
1237 
1238 	s = splhigh();		/* can't be too careful - might be called */
1239 	/* from a routine, NOT an interrupt */
1240 
1241 	ADB_VIA_CLR_INTR();	/* clear interrupt */
1242 
1243 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
1244 
1245 switch_start:
1246 	switch (adbActionState) {
1247 	case ADB_ACTION_IDLE:
1248 		delay(ADB_DELAY);	/* short delay is required before the
1249 					 * first byte */
1250 
1251 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1252 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
1253 		adbInputBuffer[1] = ADB_SR();	/* get byte */
1254 		adbInputBuffer[0] = 1;
1255 		adbActionState = ADB_ACTION_IN;	/* set next state */
1256 
1257 		ADB_SET_STATE_ACKON();	/* start ACK to ADB chip */
1258 		delay(ADB_DELAY);	/* delay */
1259 		ADB_SET_STATE_ACKOFF();	/* end ACK to ADB chip */
1260 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
1261 		break;
1262 
1263 	case ADB_ACTION_IN:
1264 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1265 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();	/* get byte */
1266 		if (ADB_INTR_IS_OFF)	/* check for end of frame */
1267 			ending = 1;
1268 		else
1269 			ending = 0;
1270 
1271 		ADB_SET_STATE_ACKON();	/* start ACK to ADB chip */
1272 		delay(ADB_DELAY);	/* delay */
1273 		ADB_SET_STATE_ACKOFF();	/* end ACK to ADB chip */
1274 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
1275 
1276 		if (1 == ending) {	/* end of message? */
1277 			ADB_SET_STATE_INACTIVE();	/* signal end of frame */
1278 			/*
1279 			 * This section _should_ handle all ADB and RTC/PRAM
1280 			 * type commands, but there may be more...  Note:
1281 			 * commands are always at [4], even for rtc/pram
1282 			 * commands
1283 			 */
1284 			/* set up data for adb_pass_up */
1285 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
1286 
1287 			if ((adbWaiting == 1) &&	/* are we waiting AND */
1288 			    (adbInputBuffer[4] == adbWaitingCmd) &&	/* the cmd we sent AND */
1289 			    ((adbInputBuffer[2] == 0x00) ||	/* it's from the ADB
1290 								 * device OR */
1291 				(adbInputBuffer[2] == 0x01))) {	/* it's from the
1292 								 * PRAM/RTC device */
1293 
1294 				packet.saveBuf = adbBuffer;
1295 				packet.compRout = adbCompRout;
1296 				packet.compData = adbCompData;
1297 				packet.unsol = 0;
1298 				packet.ack_only = 0;
1299 				adb_pass_up(&packet);
1300 
1301 				adbWaitingCmd = 0;	/* reset "waiting" vars */
1302 				adbWaiting = 0;
1303 				adbBuffer = (long)0;
1304 				adbCompRout = (long)0;
1305 				adbCompData = (long)0;
1306 			} else {
1307 				packet.unsol = 1;
1308 				packet.ack_only = 0;
1309 				adb_pass_up(&packet);
1310 			}
1311 
1312 			adbActionState = ADB_ACTION_IDLE;
1313 			adbInputBuffer[0] = 0;	/* reset length */
1314 
1315 			if (adbWriteDelay == 1) {	/* were we waiting to
1316 							 * write? */
1317 				adbSentChars = 0;	/* nothing sent yet */
1318 				adbActionState = ADB_ACTION_OUT;	/* set next state */
1319 
1320 				delay(ADB_DELAY);	/* delay */
1321 				(void)intr_dispatch(0x70); /* grab any serial interrupts */
1322 
1323 				if (ADB_INTR_IS_ON) {	/* ADB intr low during
1324 							 * write */
1325 					ADB_SET_STATE_IDLE_IISI();	/* reset */
1326 					ADB_SET_SR_INPUT();	/* make sure SR is set
1327 								 * to IN */
1328 					adbSentChars = 0;	/* must start all over */
1329 					adbActionState = ADB_ACTION_IDLE;	/* new state */
1330 					adbInputBuffer[0] = 0;
1331 					/* may be able to take this out later */
1332 					delay(ADB_DELAY);	/* delay */
1333 					break;
1334 				}
1335 				ADB_SET_STATE_ACTIVE();	/* tell ADB that we want
1336 							 * to send */
1337 				ADB_SET_STATE_ACKOFF();	/* make sure */
1338 				ADB_SET_SR_OUTPUT();	/* set shift register
1339 							 * for OUT */
1340 				ADB_SR() = adbOutputBuffer[adbSentChars + 1];
1341 				ADB_SET_STATE_ACKON();	/* tell ADB byte ready
1342 							 * to shift */
1343 			}
1344 		}
1345 		break;
1346 
1347 	case ADB_ACTION_OUT:
1348 		i = ADB_SR();	/* reset SR-intr in IFR */
1349 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1350 
1351 		ADB_SET_STATE_ACKOFF();	/* finish ACK */
1352 		adbSentChars++;
1353 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
1354 			ADB_SET_STATE_IDLE_IISI();	/* reset */
1355 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1356 			adbSentChars = 0;	/* must start all over */
1357 			adbActionState = ADB_ACTION_IDLE;	/* new state */
1358 			adbInputBuffer[0] = 0;
1359 			adbWriteDelay = 1;	/* must retry when done with
1360 						 * read */
1361 			delay(ADB_DELAY);	/* delay */
1362 			(void)intr_dispatch(0x70); /* grab any serial interrupts */
1363 			goto switch_start;	/* process next state right
1364 						 * now */
1365 			break;
1366 		}
1367 		delay(ADB_DELAY);	/* required delay */
1368 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
1369 
1370 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
1371 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
1372 									 * back? */
1373 				adbWaiting = 1;	/* signal waiting for return */
1374 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
1375 			} else {/* no talk, so done */
1376 				/* set up stuff for adb_pass_up */
1377 				memcpy(packet.data, adbInputBuffer,
1378 				    adbInputBuffer[0] + 1);
1379 				packet.saveBuf = adbBuffer;
1380 				packet.compRout = adbCompRout;
1381 				packet.compData = adbCompData;
1382 				packet.cmd = adbWaitingCmd;
1383 				packet.unsol = 0;
1384 				packet.ack_only = 1;
1385 				adb_pass_up(&packet);
1386 
1387 				/* reset "waiting" vars, just in case */
1388 				adbWaitingCmd = 0;
1389 				adbBuffer = (long)0;
1390 				adbCompRout = (long)0;
1391 				adbCompData = (long)0;
1392 			}
1393 
1394 			adbWriteDelay = 0;	/* done writing */
1395 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
1396 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1397 			ADB_SET_STATE_INACTIVE();	/* end of frame */
1398 		} else {
1399 			ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* send next byte */
1400 			ADB_SET_STATE_ACKON();	/* signal byte ready to shift */
1401 		}
1402 		break;
1403 
1404 	case ADB_ACTION_NOTREADY:
1405 #ifdef ADB_DEBUG
1406 		if (adb_debug)
1407 			printf_intr("adb: not yet initialized\n");
1408 #endif
1409 		break;
1410 
1411 	default:
1412 #ifdef ADB_DEBUG
1413 		if (adb_debug)
1414 			printf_intr("intr: unknown ADB state\n");
1415 #endif
1416 	}
1417 
1418 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
1419 
1420 	splx(s);		/* restore */
1421 
1422 	return;
1423 }				/* end adb_intr_IIsi */
1424 
1425 
1426 /*****************************************************************************
1427  * if the device is currently busy, and there is no data waiting to go out, then
1428  * the data is "queued" in the outgoing buffer. If we are already waiting, then
1429  * we return.
1430  * in: if (in == 0) then the command string is built from command and buffer
1431  *     if (in != 0) then in is used as the command string
1432  * buffer: additional data to be sent (used only if in == 0)
1433  *         this is also where return data is stored
1434  * compRout: the completion routine that is called when then return value
1435  *	     is received (if a return value is expected)
1436  * data: a data pointer that can be used by the completion routine
1437  * command: an ADB command to be sent (used only if in == 0)
1438  *
1439  */
1440 int
1441 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
1442 	command)
1443 {
1444 	int s, len;
1445 
1446 	if (adbActionState == ADB_ACTION_NOTREADY)
1447 		return 1;
1448 
1449 	/* Don't interrupt while we are messing with the ADB */
1450 	s = splhigh();
1451 
1452 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
1453 	    (ADB_INTR_IS_OFF)) {/* and no incoming interrupt? */
1454 
1455 	} else
1456 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
1457 			adbWriteDelay = 1;	/* if no, then we'll "queue"
1458 						 * it up */
1459 		else {
1460 			splx(s);
1461 			return 1;	/* really busy! */
1462 		}
1463 
1464 	if ((long)in == (long)0) {	/* need to convert? */
1465 		/*
1466 		 * Don't need to use adb_cmd_extra here because this section
1467 		 * will be called ONLY when it is an ADB command (no RTC or
1468 		 * PRAM)
1469 		 */
1470 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
1471 						 * doing a listen! */
1472 			len = buffer[0];	/* length of additional data */
1473 		else
1474 			len = 0;/* no additional data */
1475 
1476 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
1477 						 * data */
1478 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
1479 		adbOutputBuffer[2] = (u_char)command;	/* load command */
1480 
1481 		/* copy additional output data, if any */
1482 		memcpy(adbOutputBuffer + 3, buffer + 1, len);
1483 	} else
1484 		/* if data ready, just copy over */
1485 		memcpy(adbOutputBuffer, in, in[0] + 2);
1486 
1487 	adbSentChars = 0;	/* nothing sent yet */
1488 	adbBuffer = buffer;	/* save buffer to know where to save result */
1489 	adbCompRout = compRout;	/* save completion routine pointer */
1490 	adbCompData = data;	/* save completion routine data pointer */
1491 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
1492 
1493 	if (adbWriteDelay != 1) {	/* start command now? */
1494 		adbActionState = ADB_ACTION_OUT;	/* set next state */
1495 
1496 		ADB_SET_STATE_ACTIVE();	/* tell ADB that we want to send */
1497 		ADB_SET_STATE_ACKOFF();	/* make sure */
1498 
1499 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1500 
1501 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
1502 
1503 		ADB_SET_STATE_ACKON();	/* tell ADB byte ready to shift */
1504 	}
1505 	adbWriteDelay = 1;	/* something in the write "queue" */
1506 
1507 	splx(s);
1508 
1509 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked? */
1510 		/* poll until byte done */
1511 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1512 		    || (adbWaiting == 1))
1513 			if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
1514 				adb_intr_IIsi(NULL); /* go process it */
1515 				if (adb_polling)
1516 					adb_soft_intr();
1517 			}
1518 
1519 	 return 0;
1520 }				/* send_adb_IIsi */
1521 
1522 void
1523 adb_iop_recv(IOP *iop, struct iop_msg *msg)
1524 {
1525 	struct adbCommand	pkt;
1526 	unsigned		flags;
1527 
1528 	if (adbActionState != ADB_ACTION_RUNNING)
1529 		return;
1530 
1531 	switch (msg->status) {
1532 	case IOP_MSGSTAT_SENT:
1533 		if (0 == adb_cmd_result(msg->msg + 1)) {
1534 			adbWaiting = 1;
1535 			adbWaitingCmd = msg->msg[2];
1536 		}
1537 		break;
1538 	case IOP_MSGSTAT_RECEIVED:
1539 	case IOP_MSGSTAT_UNEXPECTED:
1540 		flags = msg->msg[0];
1541 		if (flags != 0) {
1542 			printf("ADB FLAGS 0x%x", flags);
1543 			break;
1544 		}
1545 		if (adbWaiting &&
1546 		    (msg->msg[2] == adbWaitingCmd)) {
1547 			pkt.saveBuf = msg->msg + 1;
1548 			pkt.compRout = adbCompRout;
1549 			pkt.compData = adbCompData;
1550 			pkt.unsol = 0;
1551 			pkt.ack_only = 0;
1552 			adb_pass_up(&pkt);
1553 
1554 			adbWaitingCmd = 0;
1555 			adbWaiting = 0;
1556 		} else {
1557 			pkt.unsol = 1;
1558 			pkt.ack_only = 0;
1559 			adb_pass_up(&pkt);
1560 		}
1561 		break;
1562 	default:
1563 		return;
1564 	}
1565 }
1566 
1567 int
1568 send_adb_iop(int cmd, u_char * buffer, void *compRout, void *data)
1569 {
1570 	u_char	buff[32];
1571 	int	cnt;
1572 
1573 	if (adbActionState != ADB_ACTION_RUNNING)
1574 		return -1;
1575 
1576 	buff[0] = IOP_ADB_FL_EXPLICIT;
1577 	buff[1] = buffer[0];
1578 	buff[2] = cmd;
1579 	cnt = (int) buff[1];
1580 	memcpy(buff + 3, buffer + 1, cnt);
1581 	return iop_send_msg(ISM_IOP, IOP_CHAN_ADB, buff, cnt+3,
1582 			    adb_iop_recv, NULL);
1583 }
1584 
1585 /*
1586  * adb_pass_up is called by the interrupt-time routines.
1587  * It takes the raw packet data that was received from the
1588  * device and puts it into the queue that the upper half
1589  * processes. It then signals for a soft ADB interrupt which
1590  * will eventually call the upper half routine (adb_soft_intr).
1591  *
1592  * If in->unsol is 0, then this is either the notification
1593  * that the packet was sent (on a LISTEN, for example), or the
1594  * response from the device (on a TALK). The completion routine
1595  * is called only if the user specified one.
1596  *
1597  * If in->unsol is 1, then this packet was unsolicited and
1598  * so we look up the device in the ADB device table to determine
1599  * what it's default service routine is.
1600  *
1601  * If in->ack_only is 1, then we really only need to call
1602  * the completion routine, so don't do any other stuff.
1603  *
1604  * Note that in->data contains the packet header AND data,
1605  * while adbInbound[]->data contains ONLY data.
1606  *
1607  * Note: Called only at interrupt time. Assumes this.
1608  */
1609 void
1610 adb_pass_up(struct adbCommand *in)
1611 {
1612 	int start = 0, len = 0, cmd = 0;
1613 	ADBDataBlock block;
1614 
1615 	/* temp for testing */
1616 	/*u_char *buffer = 0;*/
1617 	/*u_char *compdata = 0;*/
1618 	/*u_char *comprout = 0;*/
1619 
1620 	if (adbInCount >= ADB_QUEUE) {
1621 #ifdef ADB_DEBUG
1622 		if (adb_debug)
1623 			printf_intr("adb: ring buffer overflow\n");
1624 #endif
1625 		return;
1626 	}
1627 
1628 	if (in->ack_only) {
1629 		len = in->data[0];
1630 		cmd = in->cmd;
1631 		start = 0;
1632 	} else {
1633 		switch (adbHardware) {
1634 		case ADB_HW_IOP:
1635 		case ADB_HW_II:
1636 			cmd = in->data[1];
1637 			if (in->data[0] < 2)
1638 				len = 0;
1639 			else
1640 				len = in->data[0]-1;
1641 			start = 1;
1642 			break;
1643 
1644 		case ADB_HW_IISI:
1645 		case ADB_HW_CUDA:
1646 			/* If it's unsolicited, accept only ADB data for now */
1647 			if (in->unsol)
1648 				if (0 != in->data[2])
1649 					return;
1650 			cmd = in->data[4];
1651 			if (in->data[0] < 5)
1652 				len = 0;
1653 			else
1654 				len = in->data[0]-4;
1655 			start = 4;
1656 			break;
1657 
1658 		case ADB_HW_PB:
1659 			cmd = in->data[1];
1660 			if (in->data[0] < 2)
1661 				len = 0;
1662 			else
1663 				len = in->data[0]-1;
1664 			start = 1;
1665 			break;
1666 
1667 		case ADB_HW_UNKNOWN:
1668 			return;
1669 		}
1670 
1671 		/* Make sure there is a valid device entry for this device */
1672 		if (in->unsol) {
1673 			/* ignore unsolicited data during adbreinit */
1674 			if (adbStarting)
1675 				return;
1676 			/* get device's comp. routine and data area */
1677 			if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
1678 				return;
1679 		}
1680 	}
1681 
1682 	/*
1683  	 * If this is an unsolicited packet, we need to fill in
1684  	 * some info so adb_soft_intr can process this packet
1685  	 * properly. If it's not unsolicited, then use what
1686  	 * the caller sent us.
1687  	 */
1688 	if (in->unsol) {
1689 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
1690 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
1691 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
1692 	} else {
1693 		adbInbound[adbInTail].compRout = (void *)in->compRout;
1694 		adbInbound[adbInTail].compData = (void *)in->compData;
1695 		adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
1696 	}
1697 
1698 #ifdef ADB_DEBUG
1699 	if (adb_debug && in->data[1] == 2)
1700 		printf_intr("adb: caught error\n");
1701 #endif
1702 
1703 	/* copy the packet data over */
1704 	/*
1705 	 * TO DO: If the *_intr routines fed their incoming data
1706 	 * directly into an adbCommand struct, which is passed to
1707 	 * this routine, then we could eliminate this copy.
1708 	 */
1709 	memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
1710 	adbInbound[adbInTail].data[0] = len;
1711 	adbInbound[adbInTail].cmd = cmd;
1712 
1713 	adbInCount++;
1714 	if (++adbInTail >= ADB_QUEUE)
1715 		adbInTail = 0;
1716 
1717 	/*
1718 	 * If the debugger is running, call upper half manually.
1719 	 * Otherwise, trigger a soft interrupt to handle the rest later.
1720 	 */
1721 	if (adb_polling)
1722 		adb_soft_intr();
1723 	else
1724 		setsoftadb();
1725 
1726 	return;
1727 }
1728 
1729 
1730 /*
1731  * Called to process the packets after they have been
1732  * placed in the incoming queue.
1733  *
1734  */
1735 void
1736 adb_soft_intr(void)
1737 {
1738 	int s;
1739 	int cmd = 0;
1740 	u_char *buffer = 0;
1741 	u_char *comprout = 0;
1742 	u_char *compdata = 0;
1743 
1744 #if 0
1745 	s = splhigh();
1746 	printf_intr("sr: %x\n", (s & 0x0700));
1747 	splx(s);
1748 #endif
1749 
1750 /*delay(2*ADB_DELAY);*/
1751 
1752 	while (adbInCount) {
1753 #ifdef ADB_DEBUG
1754 		if (adb_debug & 0x80)
1755 			printf_intr("%x %x %x ",
1756 			    adbInCount, adbInHead, adbInTail);
1757 #endif
1758 		/* get the data we need from the queue */
1759 		buffer = adbInbound[adbInHead].saveBuf;
1760 		comprout = adbInbound[adbInHead].compRout;
1761 		compdata = adbInbound[adbInHead].compData;
1762 		cmd = adbInbound[adbInHead].cmd;
1763 
1764 		/* copy over data to data area if it's valid */
1765 		/*
1766 		 * Note that for unsol packets we don't want to copy the
1767 	 	 * data anywhere, so buffer was already set to 0.
1768 	 	 * For ack_only buffer was set to 0, so don't copy.
1769 		 */
1770 		if (buffer)
1771 			memcpy(buffer, adbInbound[adbInHead].data,
1772 			    adbInbound[adbInHead].data[0] + 1);
1773 
1774 #ifdef ADB_DEBUG
1775 			if (adb_debug & 0x80) {
1776 				printf_intr("%p %p %p %x ",
1777 				    buffer, comprout, compdata, (short)cmd);
1778 				printf_intr("buf: ");
1779 				print_single(adbInbound[adbInHead].data);
1780 			}
1781 #endif
1782 
1783 		/* call default completion routine if it's valid */
1784 		if (comprout) {
1785 #ifdef __NetBSD__
1786 			asm("	movml #0xffff,sp@-	| save all registers
1787 				movl %0,a2 		| compdata
1788 				movl %1,a1 		| comprout
1789 				movl %2,a0 		| buffer
1790 				movl %3,d0 		| cmd
1791 				jbsr a1@ 		| go call the routine
1792 				movml sp@+,#0xffff	| restore all registers"
1793 			    :
1794 			    : "g"(compdata), "g"(comprout),
1795 				"g"(buffer), "g"(cmd)
1796 			    : "d0", "a0", "a1", "a2");
1797 #else					/* for macos based testing */
1798 			asm
1799 			{
1800 				movem.l a0/a1/a2/d0, -(a7)
1801 				move.l compdata, a2
1802 				move.l comprout, a1
1803 				move.l buffer, a0
1804 				move.w cmd, d0
1805 				jsr(a1)
1806 				movem.l(a7)+, d0/a2/a1/a0
1807 			}
1808 #endif
1809 		}
1810 
1811 		s = splhigh();
1812 		adbInCount--;
1813 		if (++adbInHead >= ADB_QUEUE)
1814 			adbInHead = 0;
1815 		splx(s);
1816 
1817 	}
1818 	return;
1819 }
1820 
1821 
1822 /*
1823  * This is my version of the ADBOp routine. It mainly just calls the
1824  * hardware-specific routine.
1825  *
1826  *   data 	: pointer to data area to be used by compRout
1827  *   compRout	: completion routine
1828  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
1829  *		  byte 0 = # of bytes
1830  *		: for TALK: points to place to save return data
1831  *   command	: the adb command to send
1832  *   result	: 0 = success
1833  *		: -1 = could not complete
1834  */
1835 int
1836 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1837 {
1838 	int result;
1839 
1840 	switch (adbHardware) {
1841 	case ADB_HW_II:
1842 		result = send_adb_II((u_char *)0, (u_char *)buffer,
1843 		    (void *)compRout, (void *)data, (int)command);
1844 		if (result == 0)
1845 			return 0;
1846 		else
1847 			return -1;
1848 		break;
1849 
1850 	case ADB_HW_IOP:
1851 #ifdef __notyet__
1852 		result = send_adb_iop((int)command, (u_char *)buffer,
1853 		    (void *)compRout, (void *)data);
1854 		if (result == 0)
1855 			return 0;
1856 		else
1857 #endif
1858 			return -1;
1859 		break;
1860 
1861 	case ADB_HW_IISI:
1862 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1863 		    (void *)compRout, (void *)data, (int)command);
1864 		/*
1865 		 * I wish I knew why this delay is needed. It usually needs to
1866 		 * be here when several commands are sent in close succession,
1867 		 * especially early in device probes when doing collision
1868 		 * detection. It must be some race condition. Sigh. - jpw
1869 		 */
1870 		delay(100);
1871 		if (result == 0)
1872 			return 0;
1873 		else
1874 			return -1;
1875 		break;
1876 
1877 	case ADB_HW_PB:
1878 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
1879 		    (void *)data, (int)command);
1880 
1881 		if (result == 0)
1882 			return 0;
1883 		else
1884 			return -1;
1885 		break;
1886 
1887 	case ADB_HW_CUDA:
1888 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1889 		    (void *)compRout, (void *)data, (int)command);
1890 		if (result == 0)
1891 			return 0;
1892 		else
1893 			return -1;
1894 		break;
1895 
1896 	case ADB_HW_UNKNOWN:
1897 	default:
1898 		return -1;
1899 	}
1900 }
1901 
1902 
1903 /*
1904  * adb_hw_setup
1905  * This routine sets up the possible machine specific hardware
1906  * config (mainly VIA settings) for the various models.
1907  */
1908 void
1909 adb_hw_setup(void)
1910 {
1911 	volatile int i;
1912 	u_char send_string[ADB_MAX_MSG_LENGTH];
1913 
1914 	switch (adbHardware) {
1915 	case ADB_HW_II:
1916 		via1_register_irq(2, adb_intr_II, NULL);
1917 
1918 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1919 						 * outputs */
1920 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1921 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1922 							 * to IN (II, IIsi) */
1923 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1924 							 * hardware (II, IIsi) */
1925 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1926 						 * code only */
1927 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1928 						 * are on (II, IIsi) */
1929 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
1930 
1931 		ADB_VIA_CLR_INTR();	/* clear interrupt */
1932 		break;
1933 
1934 	case ADB_HW_IOP:
1935 		via_reg(VIA1, vIER) = 0x84;
1936 		via_reg(VIA1, vIFR) = 0x04;
1937 #ifdef __notyet__
1938 		adbActionState = ADB_ACTION_RUNNING;
1939 #endif
1940 		break;
1941 
1942 	case ADB_HW_IISI:
1943 		via1_register_irq(2, adb_intr_IIsi, NULL);
1944 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1945 						 * outputs */
1946 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1947 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1948 							 * to IN (II, IIsi) */
1949 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1950 							 * hardware (II, IIsi) */
1951 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1952 						 * code only */
1953 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1954 						 * are on (II, IIsi) */
1955 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
1956 
1957 		/* get those pesky clock ticks we missed while booting */
1958 		for (i = 0; i < 30; i++) {
1959 			delay(ADB_DELAY);
1960 			adb_hw_setup_IIsi(send_string);
1961 #ifdef ADB_DEBUG
1962 			if (adb_debug) {
1963 				printf_intr("adb: cleanup: ");
1964 				print_single(send_string);
1965 			}
1966 #endif
1967 			delay(ADB_DELAY);
1968 			if (ADB_INTR_IS_OFF)
1969 				break;
1970 		}
1971 		break;
1972 
1973 	case ADB_HW_PB:
1974 		/*
1975 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
1976 		 * pm_direct.h?
1977 		 */
1978 		pm_hw_setup();
1979 		break;
1980 
1981 	case ADB_HW_CUDA:
1982 		via1_register_irq(2, adb_intr_cuda, NULL);
1983 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1984 						 * outputs */
1985 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1986 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1987 							 * to IN */
1988 		via_reg(VIA1, vACR) = (via_reg(VIA1, vACR) | 0x0c) & ~0x10;
1989 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1990 							 * hardware */
1991 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1992 						 * code only */
1993 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1994 						 * are on */
1995 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
1996 
1997 		/* sort of a device reset */
1998 		i = ADB_SR();	/* clear interrupt */
1999 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
2000 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
2001 		delay(ADB_DELAY);
2002 		ADB_SET_STATE_TIP();	/* signal start of frame */
2003 		delay(ADB_DELAY);
2004 		ADB_TOGGLE_STATE_ACK_CUDA();
2005 		delay(ADB_DELAY);
2006 		ADB_CLR_STATE_TIP();
2007 		delay(ADB_DELAY);
2008 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
2009 		i = ADB_SR();	/* clear interrupt */
2010 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
2011 		break;
2012 
2013 	case ADB_HW_UNKNOWN:
2014 	default:
2015 		via_reg(VIA1, vIER) = 0x04;	/* turn interrupts off - TO
2016 						 * DO: turn PB ints off? */
2017 		return;
2018 		break;
2019 	}
2020 }
2021 
2022 
2023 /*
2024  * adb_hw_setup_IIsi
2025  * This is sort of a "read" routine that forces the adb hardware through a read cycle
2026  * if there is something waiting. This helps "clean up" any commands that may have gotten
2027  * stuck or stopped during the boot process.
2028  *
2029  */
2030 void
2031 adb_hw_setup_IIsi(u_char * buffer)
2032 {
2033 	int i;
2034 	int dummy;
2035 	int s;
2036 	long my_time;
2037 	int endofframe;
2038 
2039 	delay(ADB_DELAY);
2040 
2041 	i = 1;			/* skip over [0] */
2042 	s = splhigh();		/* block ALL interrupts while we are working */
2043 	ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
2044 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
2045 	/* this is required, especially on faster machines */
2046 	delay(ADB_DELAY);
2047 
2048 	if (ADB_INTR_IS_ON) {
2049 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
2050 
2051 		endofframe = 0;
2052 		while (0 == endofframe) {
2053 			/*
2054 			 * Poll for ADB interrupt and watch for timeout.
2055 			 * If time out, keep going in hopes of not hanging
2056 			 * the ADB chip - I think
2057 			 */
2058 			my_time = ADB_DELAY * 5;
2059 			while ((ADB_SR_INTR_IS_OFF) && (my_time-- > 0))
2060 				dummy = via_reg(VIA1, vBufB);
2061 
2062 			buffer[i++] = ADB_SR();	/* reset interrupt flag by
2063 						 * reading vSR */
2064 			/*
2065 			 * Perhaps put in a check here that ignores all data
2066 			 * after the first ADB_MAX_MSG_LENGTH bytes ???
2067 			 */
2068 			if (ADB_INTR_IS_OFF)	/* check for end of frame */
2069 				endofframe = 1;
2070 
2071 			ADB_SET_STATE_ACKON();	/* send ACK to ADB chip */
2072 			delay(ADB_DELAY);	/* delay */
2073 			ADB_SET_STATE_ACKOFF();	/* send ACK to ADB chip */
2074 		}
2075 		ADB_SET_STATE_INACTIVE();	/* signal end of frame and
2076 						 * delay */
2077 
2078 		/* probably don't need to delay this long */
2079 		delay(ADB_DELAY);
2080 	}
2081 	buffer[0] = --i;	/* [0] is length of message */
2082 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
2083 	splx(s);		/* restore interrupts */
2084 
2085 	return;
2086 }				/* adb_hw_setup_IIsi */
2087 
2088 
2089 
2090 /*
2091  * adb_reinit sets up the adb stuff
2092  *
2093  */
2094 void
2095 adb_reinit(void)
2096 {
2097 	u_char send_string[ADB_MAX_MSG_LENGTH];
2098 	ADBDataBlock data;	/* temp. holder for getting device info */
2099 	volatile int i, x;
2100 	int s;
2101 	int command;
2102 	int result;
2103 	int saveptr;		/* point to next free relocation address */
2104 	int device;
2105 	int nonewtimes;		/* times thru loop w/o any new devices */
2106 
2107 	/* Make sure we are not interrupted while building the table. */
2108 	/* ints must be on for PB & IOP (at least, for now) */
2109 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2110 		s = splhigh();
2111 	else
2112 		s = 0;		/* XXX shut the compiler up*/
2113 
2114 	ADBNumDevices = 0;	/* no devices yet */
2115 
2116 	/* Let intr routines know we are running reinit */
2117 	adbStarting = 1;
2118 
2119 	/*
2120 	 * Initialize the ADB table.  For now, we'll always use the same table
2121 	 * that is defined at the beginning of this file - no mallocs.
2122 	 */
2123 	for (i = 0; i < 16; i++)
2124 		ADBDevTable[i].devType = 0;
2125 
2126 	adb_setup_hw_type();	/* setup hardware type */
2127 
2128 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
2129 
2130 	delay(1000);
2131 
2132 	/* send an ADB reset first */
2133 	adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
2134 	delay(3000);
2135 
2136 	/*
2137 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
2138 	 * which device addresses are in use and which are free. For each
2139 	 * address that is in use, move the device at that address to a higher
2140 	 * free address. Continue doing this at that address until no device
2141 	 * responds at that address. Then move the last device that was moved
2142 	 * back to the original address. Do this for the remaining addresses
2143 	 * that we determined were in use.
2144 	 *
2145 	 * When finished, do this entire process over again with the updated
2146 	 * list of in use addresses. Do this until no new devices have been
2147 	 * found in 20 passes though the in use address list. (This probably
2148 	 * seems long and complicated, but it's the best way to detect multiple
2149 	 * devices at the same address - sometimes it takes a couple of tries
2150 	 * before the collision is detected.)
2151 	 */
2152 
2153 	/* initial scan through the devices */
2154 	for (i = 1; i < 16; i++) {
2155 		command = ADBTALK(i, 3);
2156 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
2157 		    (Ptr)0, (short)command);
2158 
2159 		if (send_string[0] != 0) {
2160 			/* check for valid device handler */
2161 			switch (send_string[2]) {
2162 			case 0:
2163 			case 0xfd:
2164 			case 0xfe:
2165 			case 0xff:
2166 				continue;	/* invalid, skip */
2167 			}
2168 
2169 			/* found a device */
2170 			++ADBNumDevices;
2171 			KASSERT(ADBNumDevices < 16);
2172 			ADBDevTable[ADBNumDevices].devType =
2173 				(int)(send_string[2]);
2174 			ADBDevTable[ADBNumDevices].origAddr = i;
2175 			ADBDevTable[ADBNumDevices].currentAddr = i;
2176 			ADBDevTable[ADBNumDevices].DataAreaAddr =
2177 			    (long)0;
2178 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
2179 			pm_check_adb_devices(i);	/* tell pm driver device
2180 							 * is here */
2181 		}
2182 	}
2183 
2184 	/* find highest unused address */
2185 	for (saveptr = 15; saveptr > 0; saveptr--)
2186 		if (-1 == get_adb_info(&data, saveptr))
2187 			break;
2188 
2189 #ifdef ADB_DEBUG
2190 	if (adb_debug & 0x80) {
2191 		printf_intr("first free is: 0x%02x\n", saveptr);
2192 		printf_intr("devices: %i\n", ADBNumDevices);
2193 	}
2194 #endif
2195 
2196 	nonewtimes = 0;		/* no loops w/o new devices */
2197 	while (saveptr > 0 && nonewtimes++ < 11) {
2198 		for (i = 1; i <= ADBNumDevices; i++) {
2199 			device = ADBDevTable[i].currentAddr;
2200 #ifdef ADB_DEBUG
2201 			if (adb_debug & 0x80)
2202 				printf_intr("moving device 0x%02x to 0x%02x "
2203 				    "(index 0x%02x)  ", device, saveptr, i);
2204 #endif
2205 
2206 			/* send TALK R3 to address */
2207 			command = ADBTALK(device, 3);
2208 			adb_op_sync((Ptr)send_string, (Ptr)0,
2209 			    (Ptr)0, (short)command);
2210 
2211 			/* move device to higher address */
2212 			command = ADBLISTEN(device, 3);
2213 			send_string[0] = 2;
2214 			send_string[1] = (u_char)(saveptr | 0x60);
2215 			send_string[2] = 0xfe;
2216 			adb_op_sync((Ptr)send_string, (Ptr)0,
2217 			    (Ptr)0, (short)command);
2218 			delay(500);
2219 
2220 			/* send TALK R3 - anthing at new address? */
2221 			command = ADBTALK(saveptr, 3);
2222 			adb_op_sync((Ptr)send_string, (Ptr)0,
2223 			    (Ptr)0, (short)command);
2224 			delay(500);
2225 
2226 			if (send_string[0] == 0) {
2227 #ifdef ADB_DEBUG
2228 				if (adb_debug & 0x80)
2229 					printf_intr("failed, continuing\n");
2230 #endif
2231 				continue;
2232 			}
2233 
2234 			/* send TALK R3 - anything at old address? */
2235 			command = ADBTALK(device, 3);
2236 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2237 			    (Ptr)0, (short)command);
2238 			if (send_string[0] != 0) {
2239 				/* check for valid device handler */
2240 				switch (send_string[2]) {
2241 				case 0:
2242 				case 0xfd:
2243 				case 0xfe:
2244 				case 0xff:
2245 					continue;	/* invalid, skip */
2246 				}
2247 
2248 				/* new device found */
2249 				/* update data for previously moved device */
2250 				ADBDevTable[i].currentAddr = saveptr;
2251 #ifdef ADB_DEBUG
2252 				if (adb_debug & 0x80)
2253 					printf_intr("old device at index %i\n",i);
2254 #endif
2255 				/* add new device in table */
2256 #ifdef ADB_DEBUG
2257 				if (adb_debug & 0x80)
2258 					printf_intr("new device found\n");
2259 #endif
2260 				if (saveptr > ADBNumDevices) {
2261 					++ADBNumDevices;
2262 					KASSERT(ADBNumDevices < 16);
2263 				}
2264 				ADBDevTable[ADBNumDevices].devType =
2265 					(int)(send_string[2]);
2266 				ADBDevTable[ADBNumDevices].origAddr = device;
2267 				ADBDevTable[ADBNumDevices].currentAddr = device;
2268 				/* These will be set correctly in adbsys.c */
2269 				/* Until then, unsol. data will be ignored. */
2270 				ADBDevTable[ADBNumDevices].DataAreaAddr =
2271 				    (long)0;
2272 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
2273 				    (void *)0;
2274 				/* find next unused address */
2275 				for (x = saveptr; x > 0; x--) {
2276 					if (-1 == get_adb_info(&data, x)) {
2277 						saveptr = x;
2278 						break;
2279 					}
2280 				}
2281 				if (x == 0)
2282 					saveptr = 0;
2283 #ifdef ADB_DEBUG
2284 				if (adb_debug & 0x80)
2285 					printf_intr("new free is 0x%02x\n",
2286 					    saveptr);
2287 #endif
2288 				nonewtimes = 0;
2289 				/* tell pm driver device is here */
2290 				pm_check_adb_devices(device);
2291 			} else {
2292 #ifdef ADB_DEBUG
2293 				if (adb_debug & 0x80)
2294 					printf_intr("moving back...\n");
2295 #endif
2296 				/* move old device back */
2297 				command = ADBLISTEN(saveptr, 3);
2298 				send_string[0] = 2;
2299 				send_string[1] = (u_char)(device | 0x60);
2300 				send_string[2] = 0xfe;
2301 				adb_op_sync((Ptr)send_string, (Ptr)0,
2302 				    (Ptr)0, (short)command);
2303 				delay(1000);
2304 			}
2305 		}
2306 	}
2307 
2308 #ifdef ADB_DEBUG
2309 	if (adb_debug) {
2310 		for (i = 1; i <= ADBNumDevices; i++) {
2311 			x = get_ind_adb_info(&data, i);
2312 			if (x != -1)
2313 				printf_intr("index 0x%x, addr 0x%x, type 0x%hx\n",
2314 				    i, x, data.devType);
2315 		}
2316 	}
2317 #endif
2318 
2319 #ifndef MRG_ADB
2320 	/* enable the programmer's switch, if we have one */
2321 	adb_prog_switch_enable();
2322 #endif
2323 
2324 #ifdef ADB_DEBUG
2325 	if (adb_debug) {
2326 		if (0 == ADBNumDevices)	/* tell user if no devices found */
2327 			printf_intr("adb: no devices found\n");
2328 	}
2329 #endif
2330 
2331 	adbStarting = 0;	/* not starting anymore */
2332 #ifdef ADB_DEBUG
2333 	if (adb_debug)
2334 		printf_intr("adb: ADBReInit complete\n");
2335 #endif
2336 
2337 	if (adbHardware == ADB_HW_CUDA)
2338 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
2339 		    (void *)adb_cuda_tickle, NULL);
2340 
2341 	/* ints must be on for PB & IOP (at least, for now) */
2342 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2343 		splx(s);
2344 
2345 	return;
2346 }
2347 
2348 
2349 /*
2350  * adb_comp_exec
2351  * This is a general routine that calls the completion routine if there is one.
2352  * NOTE: This routine is now only used by pm_direct.c
2353  *       All the code in this file (adb_direct.c) uses
2354  *       the adb_pass_up routine now.
2355  */
2356 void
2357 adb_comp_exec(void)
2358 {
2359 	if ((long)0 != adbCompRout) /* don't call if empty return location */
2360 #ifdef __NetBSD__
2361 		asm("	movml #0xffff,sp@-	| save all registers
2362 			movl %0,a2		| adbCompData
2363 			movl %1,a1		| adbCompRout
2364 			movl %2,a0		| adbBuffer
2365 			movl %3,d0		| adbWaitingCmd
2366 			jbsr a1@		| go call the routine
2367 			movml sp@+,#0xffff	| restore all registers"
2368 		    :
2369 		    : "g"(adbCompData), "g"(adbCompRout),
2370 			"g"(adbBuffer), "g"(adbWaitingCmd)
2371 		    : "d0", "a0", "a1", "a2");
2372 #else /* for Mac OS-based testing */
2373 		asm {
2374 			movem.l a0/a1/a2/d0, -(a7)
2375 			move.l adbCompData, a2
2376 			move.l adbCompRout, a1
2377 			move.l adbBuffer, a0
2378 			move.w adbWaitingCmd, d0
2379 			jsr(a1)
2380 			movem.l(a7) +, d0/a2/a1/a0
2381 		}
2382 #endif
2383 }
2384 
2385 
2386 /*
2387  * adb_cmd_result
2388  *
2389  * This routine lets the caller know whether the specified adb command string
2390  * should expect a returned result, such as a TALK command.
2391  *
2392  * returns: 0 if a result should be expected
2393  *          1 if a result should NOT be expected
2394  */
2395 int
2396 adb_cmd_result(u_char *in)
2397 {
2398 	switch (adbHardware) {
2399 	case ADB_HW_IOP:
2400 	case ADB_HW_II:
2401 		/* was it an ADB talk command? */
2402 		if ((in[1] & 0x0c) == 0x0c)
2403 			return 0;
2404 		return 1;
2405 
2406 	case ADB_HW_IISI:
2407 	case ADB_HW_CUDA:
2408 		/* was it an ADB talk command? */
2409 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
2410 			return 0;
2411 		/* was it an RTC/PRAM read date/time? */
2412 		if ((in[1] == 0x01) && (in[2] == 0x03))
2413 			return 0;
2414 		return 1;
2415 
2416 	case ADB_HW_PB:
2417 		return 1;
2418 
2419 	case ADB_HW_UNKNOWN:
2420 	default:
2421 		return 1;
2422 	}
2423 }
2424 
2425 
2426 /*
2427  * adb_cmd_extra
2428  *
2429  * This routine lets the caller know whether the specified adb command string
2430  * may have extra data appended to the end of it, such as a LISTEN command.
2431  *
2432  * returns: 0 if extra data is allowed
2433  *          1 if extra data is NOT allowed
2434  */
2435 int
2436 adb_cmd_extra(u_char *in)
2437 {
2438 	switch (adbHardware) {
2439 	case ADB_HW_II:
2440 	case ADB_HW_IOP:
2441 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
2442 			return 0;
2443 		return 1;
2444 
2445 	case ADB_HW_IISI:
2446 	case ADB_HW_CUDA:
2447 		/*
2448 		 * TO DO: support needs to be added to recognize RTC and PRAM
2449 		 * commands
2450 		 */
2451 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
2452 			return 0;
2453 		/* add others later */
2454 		return 1;
2455 
2456 	case ADB_HW_PB:
2457 		return 1;
2458 
2459 	case ADB_HW_UNKNOWN:
2460 	default:
2461 		return 1;
2462 	}
2463 }
2464 
2465 
2466 /*
2467  * adb_op_sync
2468  *
2469  * This routine does exactly what the adb_op routine does, except that after
2470  * the adb_op is called, it waits until the return value is present before
2471  * returning.
2472  *
2473  * NOTE: The user specified compRout is ignored, since this routine specifies
2474  * it's own to adb_op, which is why you really called this in the first place
2475  * anyway.
2476  */
2477 int
2478 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
2479 {
2480 	int result;
2481 	volatile int flag = 0;
2482 
2483 	result = adb_op(buffer, (void *)adb_op_comprout,
2484 	    (void *)&flag, command);	/* send command */
2485 	if (result == 0)		/* send ok? */
2486 		while (0 == flag)
2487 			/* wait for compl. routine */;
2488 
2489 	return result;
2490 }
2491 
2492 
2493 /*
2494  * adb_op_comprout
2495  *
2496  * This function is used by the adb_op_sync routine so it knows when the
2497  * function is done.
2498  */
2499 void
2500 adb_op_comprout(void)
2501 {
2502 #ifdef __NetBSD__
2503 	asm("movw	#1,a2@			| update flag value");
2504 #else				/* for macos based testing */
2505 	asm {
2506 		move.w #1,(a2) }		/* update flag value */
2507 #endif
2508 }
2509 
2510 void
2511 adb_setup_hw_type(void)
2512 {
2513 	long response;
2514 
2515 	response = mac68k_machine.machineid;
2516 
2517 	/*
2518 	 * Determine what type of ADB hardware we are running on.
2519 	 */
2520 	switch (response) {
2521 	case MACH_MACC610:		/* Centris 610 */
2522 	case MACH_MACC650:		/* Centris 650 */
2523 	case MACH_MACII:		/* II */
2524 	case MACH_MACIICI:		/* IIci */
2525 	case MACH_MACIICX:		/* IIcx */
2526 	case MACH_MACIIX:		/* IIx */
2527 	case MACH_MACQ610:		/* Quadra 610 */
2528 	case MACH_MACQ650:		/* Quadra 650 */
2529 	case MACH_MACQ700:		/* Quadra 700 */
2530 	case MACH_MACQ800:		/* Quadra 800 */
2531 	case MACH_MACSE30:		/* SE/30 */
2532 		adbHardware = ADB_HW_II;
2533 #ifdef ADB_DEBUG
2534 		if (adb_debug)
2535 			printf_intr("adb: using II series hardware support\n");
2536 #endif
2537 		break;
2538 
2539 	case MACH_MACCLASSICII:		/* Classic II */
2540 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
2541 	case MACH_MACLCIII:		/* LC III, Performa 450 */
2542 	case MACH_MACIISI:		/* IIsi */
2543 	case MACH_MACIIVI:		/* IIvi */
2544 	case MACH_MACIIVX:		/* IIvx */
2545 	case MACH_MACP460:		/* Performa 460/465/467 */
2546 	case MACH_MACP600:		/* Performa 600 */
2547 		adbHardware = ADB_HW_IISI;
2548 #ifdef ADB_DEBUG
2549 		if (adb_debug)
2550 			printf_intr("adb: using IIsi series hardware support\n");
2551 #endif
2552 		break;
2553 
2554 	case MACH_MACPB140:		/* PowerBook 140 */
2555 	case MACH_MACPB145:		/* PowerBook 145 */
2556 	case MACH_MACPB150:		/* PowerBook 150 */
2557 	case MACH_MACPB160:		/* PowerBook 160 */
2558 	case MACH_MACPB165:		/* PowerBook 165 */
2559 	case MACH_MACPB165C:		/* PowerBook 165c */
2560 	case MACH_MACPB170:		/* PowerBook 170 */
2561 	case MACH_MACPB180:		/* PowerBook 180 */
2562 	case MACH_MACPB180C:		/* PowerBook 180c */
2563 		adbHardware = ADB_HW_PB;
2564 		pm_setup_adb();
2565 #ifdef ADB_DEBUG
2566 		if (adb_debug)
2567 			printf_intr("adb: using PowerBook 100-series hardware support\n");
2568 #endif
2569 		break;
2570 
2571 	case MACH_MACPB210:		/* PowerBook Duo 210 */
2572 	case MACH_MACPB230:		/* PowerBook Duo 230 */
2573 	case MACH_MACPB250:		/* PowerBook Duo 250 */
2574 	case MACH_MACPB270:		/* PowerBook Duo 270 */
2575 	case MACH_MACPB280:		/* PowerBook Duo 280 */
2576 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
2577 	case MACH_MACPB500:		/* PowerBook 500 series */
2578 		adbHardware = ADB_HW_PB;
2579 		pm_setup_adb();
2580 #ifdef ADB_DEBUG
2581 		if (adb_debug)
2582 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2583 #endif
2584 		break;
2585 
2586 	case MACH_MACC660AV:		/* Centris 660AV */
2587 	case MACH_MACCCLASSIC:		/* Color Classic */
2588 	case MACH_MACCCLASSICII:	/* Color Classic II */
2589 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
2590 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
2591 	case MACH_MACLC520:		/* LC 520 */
2592 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2593 	case MACH_MACP550:		/* LC 550, Performa 550 */
2594 	case MACH_MACTV:		/* Macintosh TV */
2595 	case MACH_MACP580:		/* Performa 580/588 */
2596 	case MACH_MACQ605:		/* Quadra 605 */
2597 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
2598 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2599 	case MACH_MACQ840AV:		/* Quadra 840AV */
2600 		adbHardware = ADB_HW_CUDA;
2601 #ifdef ADB_DEBUG
2602 		if (adb_debug)
2603 			printf_intr("adb: using Cuda series hardware support\n");
2604 #endif
2605 		break;
2606 
2607 	case MACH_MACQ900:		/* Quadra 900 */
2608 	case MACH_MACQ950:		/* Quadra 950 */
2609 	case MACH_MACIIFX:		/* Mac IIfx   */
2610 		adbHardware = ADB_HW_IOP;
2611 		iop_register_listener(ISM_IOP, IOP_CHAN_ADB, adb_iop_recv, NULL);
2612 #ifdef ADB_DEBUG
2613 		if (adb_debug)
2614 			printf_intr("adb: using IOP-based ADB\n");
2615 #endif
2616 		break;
2617 
2618 	default:
2619 		adbHardware = ADB_HW_UNKNOWN;
2620 #ifdef ADB_DEBUG
2621 		if (adb_debug) {
2622 			printf_intr("adb: hardware type unknown for this machine\n");
2623 			printf_intr("adb: ADB support is disabled\n");
2624 		}
2625 #endif
2626 		break;
2627 	}
2628 
2629 	/*
2630 	 * Determine whether this machine has ADB based soft power.
2631 	 */
2632 	switch (response) {
2633 	case MACH_MACCCLASSIC:		/* Color Classic */
2634 	case MACH_MACCCLASSICII:	/* Color Classic II */
2635 	case MACH_MACIISI:		/* IIsi */
2636 	case MACH_MACIIVI:		/* IIvi */
2637 	case MACH_MACIIVX:		/* IIvx */
2638 	case MACH_MACLC520:		/* LC 520 */
2639 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2640 	case MACH_MACP550:		/* LC 550, Performa 550 */
2641 	case MACH_MACTV:		/* Macintosh TV */
2642 	case MACH_MACP580:		/* Performa 580/588 */
2643 	case MACH_MACP600:		/* Performa 600 */
2644 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2645 	case MACH_MACQ840AV:		/* Quadra 840AV */
2646 		adbSoftPower = 1;
2647 		break;
2648 	}
2649 }
2650 
2651 int
2652 count_adbs(void)
2653 {
2654 	int i;
2655 	int found;
2656 
2657 	found = 0;
2658 
2659 	for (i = 1; i < 16; i++)
2660 		if (0 != ADBDevTable[i].devType)
2661 			found++;
2662 
2663 	return found;
2664 }
2665 
2666 int
2667 get_ind_adb_info(ADBDataBlock * info, int index)
2668 {
2669 	if ((index < 1) || (index > 15))	/* check range 1-15 */
2670 		return (-1);
2671 
2672 #ifdef ADB_DEBUG
2673 	if (adb_debug & 0x80)
2674 		printf_intr("index 0x%x devType is: 0x%x\n", index,
2675 		    ADBDevTable[index].devType);
2676 #endif
2677 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
2678 		return (-1);
2679 
2680 	info->devType = (unsigned char)(ADBDevTable[index].devType);
2681 	info->origADBAddr = (unsigned char)(ADBDevTable[index].origAddr);
2682 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
2683 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
2684 
2685 	return (ADBDevTable[index].currentAddr);
2686 }
2687 
2688 int
2689 get_adb_info(ADBDataBlock * info, int adbAddr)
2690 {
2691 	int i;
2692 
2693 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2694 		return (-1);
2695 
2696 	for (i = 1; i < 15; i++)
2697 		if (ADBDevTable[i].currentAddr == adbAddr) {
2698 			info->devType = (unsigned char)(ADBDevTable[i].devType);
2699 			info->origADBAddr = (unsigned char)(ADBDevTable[i].origAddr);
2700 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2701 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2702 			return 0;	/* found */
2703 		}
2704 
2705 	return (-1);		/* not found */
2706 }
2707 
2708 int
2709 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
2710 {
2711 	int i;
2712 
2713 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2714 		return (-1);
2715 
2716 	for (i = 1; i < 15; i++)
2717 		if (ADBDevTable[i].currentAddr == adbAddr) {
2718 			ADBDevTable[i].ServiceRtPtr =
2719 			    (void *)(info->siServiceRtPtr);
2720 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2721 			return 0;	/* found */
2722 		}
2723 
2724 	return (-1);		/* not found */
2725 
2726 }
2727 
2728 #ifndef MRG_ADB
2729 long
2730 mrg_adbintr(void)
2731 {
2732 	adb_intr(NULL);
2733 	return 1;	/* mimic mrg_adbintr in macrom.h just in case */
2734 }
2735 
2736 long
2737 mrg_pmintr(void)
2738 {
2739 	pm_intr(NULL);
2740 	return 1;	/* mimic mrg_pmintr in macrom.h just in case */
2741 }
2742 
2743 /* caller should really use machine-independant version: getPramTime */
2744 /* this version does pseudo-adb access only */
2745 int
2746 adb_read_date_time(unsigned long *time)
2747 {
2748 	u_char output[ADB_MAX_MSG_LENGTH];
2749 	int result;
2750 	volatile int flag = 0;
2751 
2752 	switch (adbHardware) {
2753 	case ADB_HW_II:
2754 		return -1;
2755 
2756 	case ADB_HW_IOP:
2757 		return -1;
2758 
2759 	case ADB_HW_IISI:
2760 		output[0] = 0x02;	/* 2 byte message */
2761 		output[1] = 0x01;	/* to pram/rtc device */
2762 		output[2] = 0x03;	/* read date/time */
2763 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
2764 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
2765 		if (result != 0)	/* exit if not sent */
2766 			return -1;
2767 
2768 		while (0 == flag)	/* wait for result */
2769 			;
2770 
2771 		*time = (long)(*(long *)(output + 1));
2772 		return 0;
2773 
2774 	case ADB_HW_PB:
2775 		return -1;
2776 
2777 	case ADB_HW_CUDA:
2778 		output[0] = 0x02;	/* 2 byte message */
2779 		output[1] = 0x01;	/* to pram/rtc device */
2780 		output[2] = 0x03;	/* read date/time */
2781 		result = send_adb_cuda((u_char *)output, (u_char *)output,
2782 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2783 		if (result != 0)	/* exit if not sent */
2784 			return -1;
2785 
2786 		while (0 == flag)	/* wait for result */
2787 			;
2788 
2789 		*time = (long)(*(long *)(output + 1));
2790 		return 0;
2791 
2792 	case ADB_HW_UNKNOWN:
2793 	default:
2794 		return -1;
2795 	}
2796 }
2797 
2798 /* caller should really use machine-independant version: setPramTime */
2799 /* this version does pseudo-adb access only */
2800 int
2801 adb_set_date_time(unsigned long time)
2802 {
2803 	u_char output[ADB_MAX_MSG_LENGTH];
2804 	int result;
2805 	volatile int flag = 0;
2806 
2807 	switch (adbHardware) {
2808 	case ADB_HW_II:
2809 		return -1;
2810 
2811 	case ADB_HW_IOP:
2812 		return -1;
2813 
2814 	case ADB_HW_IISI:
2815 		output[0] = 0x06;	/* 6 byte message */
2816 		output[1] = 0x01;	/* to pram/rtc device */
2817 		output[2] = 0x09;	/* set date/time */
2818 		output[3] = (u_char)(time >> 24);
2819 		output[4] = (u_char)(time >> 16);
2820 		output[5] = (u_char)(time >> 8);
2821 		output[6] = (u_char)(time);
2822 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2823 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2824 		if (result != 0)	/* exit if not sent */
2825 			return -1;
2826 
2827 		while (0 == flag)	/* wait for send to finish */
2828 			;
2829 
2830 		return 0;
2831 
2832 	case ADB_HW_PB:
2833 		return -1;
2834 
2835 	case ADB_HW_CUDA:
2836 		output[0] = 0x06;	/* 6 byte message */
2837 		output[1] = 0x01;	/* to pram/rtc device */
2838 		output[2] = 0x09;	/* set date/time */
2839 		output[3] = (u_char)(time >> 24);
2840 		output[4] = (u_char)(time >> 16);
2841 		output[5] = (u_char)(time >> 8);
2842 		output[6] = (u_char)(time);
2843 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2844 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2845 		if (result != 0)	/* exit if not sent */
2846 			return -1;
2847 
2848 		while (0 == flag)	/* wait for send to finish */
2849 			;
2850 
2851 		return 0;
2852 
2853 	case ADB_HW_UNKNOWN:
2854 	default:
2855 		return -1;
2856 	}
2857 }
2858 
2859 
2860 int
2861 adb_poweroff(void)
2862 {
2863 	u_char output[ADB_MAX_MSG_LENGTH];
2864 	int result;
2865 
2866 	if (!adbSoftPower)
2867 		return -1;
2868 
2869 	adb_polling = 1;
2870 
2871 	switch (adbHardware) {
2872 	case ADB_HW_IISI:
2873 		output[0] = 0x02;	/* 2 byte message */
2874 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2875 		output[2] = 0x0a;	/* set date/time */
2876 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2877 		    (void *)0, (void *)0, (int)0);
2878 		if (result != 0)	/* exit if not sent */
2879 			return -1;
2880 
2881 		for (;;);		/* wait for power off */
2882 
2883 		return 0;
2884 
2885 	case ADB_HW_PB:
2886 		return -1;
2887 
2888 	case ADB_HW_CUDA:
2889 		output[0] = 0x02;	/* 2 byte message */
2890 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2891 		output[2] = 0x0a;	/* set date/time */
2892 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2893 		    (void *)0, (void *)0, (int)0);
2894 		if (result != 0)	/* exit if not sent */
2895 			return -1;
2896 
2897 		for (;;);		/* wait for power off */
2898 
2899 		return 0;
2900 
2901 	case ADB_HW_II:			/* II models don't do ADB soft power */
2902 	case ADB_HW_IOP:		/* IOP models don't do ADB soft power */
2903 	case ADB_HW_UNKNOWN:
2904 	default:
2905 		return -1;
2906 	}
2907 }
2908 
2909 int
2910 adb_prog_switch_enable(void)
2911 {
2912 	u_char output[ADB_MAX_MSG_LENGTH];
2913 	int result;
2914 	volatile int flag = 0;
2915 
2916 	switch (adbHardware) {
2917 	case ADB_HW_IISI:
2918 		output[0] = 0x03;	/* 3 byte message */
2919 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2920 		output[2] = 0x1c;	/* prog. switch control */
2921 		output[3] = 0x01;	/* enable */
2922 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2923 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2924 		if (result != 0)	/* exit if not sent */
2925 			return -1;
2926 
2927 		while (0 == flag)	/* wait for send to finish */
2928 			;
2929 
2930 		return 0;
2931 
2932 	case ADB_HW_PB:
2933 		return -1;
2934 
2935 	case ADB_HW_II:		/* II models don't do prog. switch */
2936 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2937 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
2938 	case ADB_HW_UNKNOWN:
2939 	default:
2940 		return -1;
2941 	}
2942 }
2943 
2944 int
2945 adb_prog_switch_disable(void)
2946 {
2947 	u_char output[ADB_MAX_MSG_LENGTH];
2948 	int result;
2949 	volatile int flag = 0;
2950 
2951 	switch (adbHardware) {
2952 	case ADB_HW_IISI:
2953 		output[0] = 0x03;	/* 3 byte message */
2954 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2955 		output[2] = 0x1c;	/* prog. switch control */
2956 		output[3] = 0x01;	/* disable */
2957 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2958 			(void *)adb_op_comprout, (void *)&flag, (int)0);
2959 		if (result != 0)	/* exit if not sent */
2960 			return -1;
2961 
2962 		while (0 == flag)	/* wait for send to finish */
2963 			;
2964 
2965 		return 0;
2966 
2967 	case ADB_HW_PB:
2968 		return -1;
2969 
2970 	case ADB_HW_II:		/* II models don't do prog. switch */
2971 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2972 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
2973 	case ADB_HW_UNKNOWN:
2974 	default:
2975 		return -1;
2976 	}
2977 }
2978 
2979 int
2980 CountADBs(void)
2981 {
2982 	return (count_adbs());
2983 }
2984 
2985 void
2986 ADBReInit(void)
2987 {
2988 	adb_reinit();
2989 }
2990 
2991 int
2992 GetIndADB(ADBDataBlock * info, int index)
2993 {
2994 	return (get_ind_adb_info(info, index));
2995 }
2996 
2997 int
2998 GetADBInfo(ADBDataBlock * info, int adbAddr)
2999 {
3000 	return (get_adb_info(info, adbAddr));
3001 }
3002 
3003 int
3004 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
3005 {
3006 	return (set_adb_info(info, adbAddr));
3007 }
3008 
3009 int
3010 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
3011 {
3012 	return (adb_op(buffer, compRout, data, commandNum));
3013 }
3014 
3015 #endif
3016