xref: /netbsd/sys/arch/mac68k/dev/adb_direct.c (revision c4a72b64)
1 /*	$NetBSD: adb_direct.c,v 1.48 2002/11/03 11:04:35 shiba 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 void	adb_read_II __P((u_char *));
307 void	adb_hw_setup __P((void));
308 void	adb_hw_setup_IIsi __P((u_char *));
309 void	adb_comp_exec __P((void));
310 int	adb_cmd_result __P((u_char *));
311 int	adb_cmd_extra __P((u_char *));
312 int	adb_guess_next_device __P((void));
313 int	adb_prog_switch_enable __P((void));
314 int	adb_prog_switch_disable __P((void));
315 /* we should create this and it will be the public version */
316 int	send_adb __P((u_char *, void *, void *));
317 void	adb_iop_recv __P((IOP *, struct iop_msg *));
318 int	send_adb_iop __P((int, u_char *, void *, void *));
319 
320 #ifdef ADB_DEBUG
321 /*
322  * print_single
323  * Diagnostic display routine. Displays the hex values of the
324  * specified elements of the u_char. The length of the "string"
325  * is in [0].
326  */
327 void
328 print_single(str)
329 	u_char *str;
330 {
331 	int x;
332 
333 	if (str == 0) {
334 		printf_intr("no data - null pointer\n");
335 		return;
336 	}
337 	if (*str == 0) {
338 		printf_intr("nothing returned\n");
339 		return;
340 	}
341 	if (*str > 20) {
342 		printf_intr("ADB: ACK > 20 no way!\n");
343 		*str = (u_char)20;
344 	}
345 	printf_intr("(length=0x%x):", (u_int)*str);
346 	for (x = 1; x <= *str; x++)
347 		printf_intr("  0x%02x", (u_int)*(str + x));
348 	printf_intr("\n");
349 }
350 #endif
351 
352 void
353 adb_cuda_tickle(void)
354 {
355 	volatile int s;
356 
357 	if (adbActionState == ADB_ACTION_IN) {
358 		if (tickle_serial == adb_cuda_serial) {
359 			if (++tickle_count > 0) {
360 				s = splhigh();
361 				adbActionState = ADB_ACTION_IDLE;
362 				adbInputBuffer[0] = 0;
363 				ADB_SET_STATE_IDLE_CUDA();
364 				splx(s);
365 			}
366 		} else {
367 			tickle_serial = adb_cuda_serial;
368 			tickle_count = 0;
369 		}
370 	} else {
371 		tickle_serial = adb_cuda_serial;
372 		tickle_count = 0;
373 	}
374 
375 	callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
376 	    (void *)adb_cuda_tickle, NULL);
377 }
378 
379 /*
380  * called when when an adb interrupt happens
381  *
382  * Cuda version of adb_intr
383  * TO DO: do we want to add some calls to intr_dispatch() here to
384  * grab serial interrupts?
385  */
386 void
387 adb_intr_cuda(void *arg)
388 {
389 	volatile int i, ending;
390 	volatile unsigned int s;
391 	struct adbCommand packet;
392 
393 	s = splhigh();		/* can't be too careful - might be called */
394 	/* from a routine, NOT an interrupt */
395 
396 	ADB_VIA_CLR_INTR();	/* clear interrupt */
397 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
398 
399 switch_start:
400 	switch (adbActionState) {
401 	case ADB_ACTION_IDLE:
402 		/*
403 		 * This is an unexpected packet, so grab the first (dummy)
404 		 * byte, set up the proper vars, and tell the chip we are
405 		 * starting to receive the packet by setting the TIP bit.
406 		 */
407 		adbInputBuffer[1] = ADB_SR();
408 		adb_cuda_serial++;
409 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
410 			break;
411 
412 		ADB_SET_SR_INPUT();
413 		ADB_SET_STATE_TIP();
414 
415 		adbInputBuffer[0] = 1;
416 		adbActionState = ADB_ACTION_IN;
417 #ifdef ADB_DEBUG
418 		if (adb_debug)
419 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
420 #endif
421 		break;
422 
423 	case ADB_ACTION_IN:
424 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
425 		/* intr off means this is the last byte (end of frame) */
426 		if (ADB_INTR_IS_OFF)
427 			ending = 1;
428 		else
429 			ending = 0;
430 
431 		if (1 == ending) {	/* end of message? */
432 #ifdef ADB_DEBUG
433 			if (adb_debug) {
434 				printf_intr("in end 0x%02x ",
435 				    adbInputBuffer[adbInputBuffer[0]]);
436 				print_single(adbInputBuffer);
437 			}
438 #endif
439 
440 			/*
441 			 * Are we waiting AND does this packet match what we
442 			 * are waiting for AND is it coming from either the
443 			 * ADB or RTC/PRAM sub-device? This section _should_
444 			 * recognize all ADB and RTC/PRAM type commands, but
445 			 * there may be more... NOTE: commands are always at
446 			 * [4], even for RTC/PRAM commands.
447 			 */
448 			/* set up data for adb_pass_up */
449 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
450 
451 			if ((adbWaiting == 1) &&
452 			    (adbInputBuffer[4] == adbWaitingCmd) &&
453 			    ((adbInputBuffer[2] == 0x00) ||
454 			    (adbInputBuffer[2] == 0x01))) {
455 				packet.saveBuf = adbBuffer;
456 				packet.compRout = adbCompRout;
457 				packet.compData = adbCompData;
458 				packet.unsol = 0;
459 				packet.ack_only = 0;
460 				adb_pass_up(&packet);
461 
462 				adbWaitingCmd = 0;	/* reset "waiting" vars */
463 				adbWaiting = 0;
464 				adbBuffer = (long)0;
465 				adbCompRout = (long)0;
466 				adbCompData = (long)0;
467 			} else {
468 				packet.unsol = 1;
469 				packet.ack_only = 0;
470 				adb_pass_up(&packet);
471 			}
472 
473 
474 			/* reset vars and signal the end of this frame */
475 			adbActionState = ADB_ACTION_IDLE;
476 			adbInputBuffer[0] = 0;
477 			ADB_SET_STATE_IDLE_CUDA();
478 			/*ADB_SET_SR_INPUT();*/
479 
480 			/*
481 			 * If there is something waiting to be sent out,
482 			 * the set everything up and send the first byte.
483 			 */
484 			if (adbWriteDelay == 1) {
485 				delay(ADB_DELAY);	/* required */
486 				adbSentChars = 0;
487 				adbActionState = ADB_ACTION_OUT;
488 				/*
489 				 * If the interrupt is on, we were too slow
490 				 * and the chip has already started to send
491 				 * something to us, so back out of the write
492 				 * and start a read cycle.
493 				 */
494 				if (ADB_INTR_IS_ON) {
495 					ADB_SET_SR_INPUT();
496 					ADB_SET_STATE_IDLE_CUDA();
497 					adbSentChars = 0;
498 					adbActionState = ADB_ACTION_IDLE;
499 					adbInputBuffer[0] = 0;
500 					break;
501 				}
502 				/*
503 				 * If we got here, it's ok to start sending
504 				 * so load the first byte and tell the chip
505 				 * we want to send.
506 				 */
507 				ADB_SET_STATE_TIP();
508 				ADB_SET_SR_OUTPUT();
509 				ADB_SR() = adbOutputBuffer[adbSentChars + 1];
510 			}
511 		} else {
512 			ADB_TOGGLE_STATE_ACK_CUDA();
513 #ifdef ADB_DEBUG
514 			if (adb_debug)
515 				printf_intr("in 0x%02x ",
516 				    adbInputBuffer[adbInputBuffer[0]]);
517 #endif
518 		}
519 		break;
520 
521 	case ADB_ACTION_OUT:
522 		i = ADB_SR();	/* reset SR-intr in IFR */
523 #ifdef ADB_DEBUG
524 		if (adb_debug)
525 			printf_intr("intr out 0x%02x ", i);
526 #endif
527 
528 		adbSentChars++;
529 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
530 #ifdef ADB_DEBUG
531 			if (adb_debug)
532 				printf_intr("intr was on ");
533 #endif
534 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
535 			ADB_SET_STATE_IDLE_CUDA();
536 			adbSentChars = 0;	/* must start all over */
537 			adbActionState = ADB_ACTION_IDLE;	/* new state */
538 			adbInputBuffer[0] = 0;
539 			adbWriteDelay = 1;	/* must retry when done with
540 						 * read */
541 			delay(ADB_DELAY);
542 			goto switch_start;	/* process next state right
543 						 * now */
544 			break;
545 		}
546 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
547 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
548 									 * back? */
549 				adbWaiting = 1;	/* signal waiting for return */
550 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
551 			} else {	/* no talk, so done */
552 				/* set up stuff for adb_pass_up */
553 				memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
554 				packet.saveBuf = adbBuffer;
555 				packet.compRout = adbCompRout;
556 				packet.compData = adbCompData;
557 				packet.cmd = adbWaitingCmd;
558 				packet.unsol = 0;
559 				packet.ack_only = 1;
560 				adb_pass_up(&packet);
561 
562 				/* reset "waiting" vars, just in case */
563 				adbWaitingCmd = 0;
564 				adbBuffer = (long)0;
565 				adbCompRout = (long)0;
566 				adbCompData = (long)0;
567 			}
568 
569 			adbWriteDelay = 0;	/* done writing */
570 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
571 			ADB_SET_SR_INPUT();
572 			ADB_SET_STATE_IDLE_CUDA();
573 #ifdef ADB_DEBUG
574 			if (adb_debug)
575 				printf_intr("write done ");
576 #endif
577 		} else {
578 			ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* send next byte */
579 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
580 							 * shift */
581 #ifdef ADB_DEBUG
582 			if (adb_debug)
583 				printf_intr("toggle ");
584 #endif
585 		}
586 		break;
587 
588 	case ADB_ACTION_NOTREADY:
589 #ifdef ADB_DEBUG
590 		if (adb_debug)
591 			printf_intr("adb: not yet initialized\n");
592 #endif
593 		break;
594 
595 	default:
596 #ifdef ADB_DEBUG
597 		if (adb_debug)
598 			printf_intr("intr: unknown ADB state\n");
599 #endif
600 		break;
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 		break;
1045 	}
1046 
1047 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
1048 
1049 	splx(s);		/* restore */
1050 
1051 	return;
1052 
1053 }
1054 
1055 
1056 /*
1057  * send_adb version for II series machines
1058  */
1059 int
1060 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
1061 {
1062 	int s, len;
1063 
1064 	if (adbActionState == ADB_ACTION_NOTREADY)	/* return if ADB not
1065 							 * available */
1066 		return 1;
1067 
1068 	/* Don't interrupt while we are messing with the ADB */
1069 	s = splhigh();
1070 
1071 	if (0 != adbOutQueueHasData) {	/* right now, "has data" means "full" */
1072 		splx(s);	/* sorry, try again later */
1073 		return 1;
1074 	}
1075 	if ((long)in == (long)0) {	/* need to convert? */
1076 		/*
1077 		 * Don't need to use adb_cmd_extra here because this section
1078 		 * will be called ONLY when it is an ADB command (no RTC or
1079 		 * PRAM), especially on II series!
1080 		 */
1081 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
1082 						 * doing a listen! */
1083 			len = buffer[0];	/* length of additional data */
1084 		else
1085 			len = 0;/* no additional data */
1086 
1087 		adbOutQueue.outBuf[0] = 1 + len;	/* command + addl. data */
1088 		adbOutQueue.outBuf[1] = (u_char)command;	/* load command */
1089 
1090 		/* copy additional output data, if any */
1091 		memcpy(adbOutQueue.outBuf + 2, buffer + 1, len);
1092 	} else
1093 		/* if data ready, just copy over */
1094 		memcpy(adbOutQueue.outBuf, in, in[0] + 2);
1095 
1096 	adbOutQueue.saveBuf = buffer;	/* save buffer to know where to save
1097 					 * result */
1098 	adbOutQueue.compRout = compRout;	/* save completion routine
1099 						 * pointer */
1100 	adbOutQueue.data = data;/* save completion routine data pointer */
1101 
1102 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* is ADB available? */
1103 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupts? */
1104 		/* then start command now */
1105 		memcpy(adbOutputBuffer, adbOutQueue.outBuf,
1106 		    adbOutQueue.outBuf[0] + 2);		/* copy over data */
1107 
1108 		adbBuffer = adbOutQueue.saveBuf;	/* pointer to user data
1109 							 * area */
1110 		adbCompRout = adbOutQueue.compRout;	/* pointer to the
1111 							 * completion routine */
1112 		adbCompData = adbOutQueue.data;	/* pointer to the completion
1113 						 * routine data */
1114 
1115 		adbSentChars = 0;	/* nothing sent yet */
1116 		adbActionState = ADB_ACTION_OUT;	/* set next state */
1117 		adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
1118 
1119 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1120 
1121 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
1122 		ADB_SET_STATE_CMD();	/* tell ADB that we want to send */
1123 		adbOutQueueHasData = 0;	/* currently processing "queue" entry */
1124 	} else
1125 		adbOutQueueHasData = 1;	/* something in the write "queue" */
1126 
1127 	splx(s);
1128 
1129 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked? */
1130 		/* poll until message done */
1131 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1132 		    || (adbWaiting == 1))
1133 			if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
1134 				adb_intr_II(NULL); /* go process it */
1135 				if (adb_polling)
1136 					adb_soft_intr();
1137 			}
1138 
1139 	return 0;
1140 }
1141 
1142 
1143 /*
1144  * This routine is called from the II series interrupt routine
1145  * to determine what the "next" device is that should be polled.
1146  */
1147 int
1148 adb_guess_next_device(void)
1149 {
1150 	int last, i, dummy;
1151 
1152 	if (adbStarting) {
1153 		/*
1154 		 * Start polling EVERY device, since we can't be sure there is
1155 		 * anything in the device table yet
1156 		 */
1157 		if (adbLastDevice < 1 || adbLastDevice > 15)
1158 			adbLastDevice = 1;
1159 		if (++adbLastDevice > 15)	/* point to next one */
1160 			adbLastDevice = 1;
1161 	} else {
1162 		/* find the next device using the device table */
1163 		if (adbLastDevice < 1 || adbLastDevice > 15)	/* let's be parinoid */
1164 			adbLastDevice = 2;
1165 		last = 1;	/* default index location */
1166 
1167 		for (i = 1; i < 16; i++)	/* find index entry */
1168 			if (ADBDevTable[i].currentAddr == adbLastDevice) {	/* look for device */
1169 				last = i;	/* found it */
1170 				break;
1171 			}
1172 		dummy = last;	/* index to start at */
1173 		for (;;) {	/* find next device in index */
1174 			if (++dummy > 15)	/* wrap around if needed */
1175 				dummy = 1;
1176 			if (dummy == last) {	/* didn't find any other
1177 						 * device! This can happen if
1178 						 * there are no devices on the
1179 						 * bus */
1180 				dummy = 1;
1181 				break;
1182 			}
1183 			/* found the next device */
1184 			if (ADBDevTable[dummy].devType != 0)
1185 				break;
1186 		}
1187 		adbLastDevice = ADBDevTable[dummy].currentAddr;
1188 	}
1189 	return adbLastDevice;
1190 }
1191 
1192 
1193 /*
1194  * Called when when an adb interrupt happens.
1195  * This routine simply transfers control over to the appropriate
1196  * code for the machine we are running on.
1197  */
1198 void
1199 adb_intr(void *arg)
1200 {
1201 	switch (adbHardware) {
1202 	case ADB_HW_II:
1203 		adb_intr_II(arg);
1204 		break;
1205 
1206 	case ADB_HW_IISI:
1207 		adb_intr_IIsi(arg);
1208 		break;
1209 
1210 	case ADB_HW_PB:		/* Should not come through here. */
1211 		break;
1212 
1213 	case ADB_HW_CUDA:
1214 		adb_intr_cuda(arg);
1215 		break;
1216 
1217 	case ADB_HW_IOP:	/* Should not come through here. */
1218 		break;
1219 
1220 	case ADB_HW_UNKNOWN:
1221 		break;
1222 	}
1223 }
1224 
1225 
1226 /*
1227  * called when when an adb interrupt happens
1228  *
1229  * IIsi version of adb_intr
1230  *
1231  */
1232 void
1233 adb_intr_IIsi(void *arg)
1234 {
1235 	struct adbCommand packet;
1236 	int i, ending;
1237 	unsigned int s;
1238 
1239 	s = splhigh();		/* can't be too careful - might be called */
1240 	/* from a routine, NOT an interrupt */
1241 
1242 	ADB_VIA_CLR_INTR();	/* clear interrupt */
1243 
1244 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
1245 
1246 switch_start:
1247 	switch (adbActionState) {
1248 	case ADB_ACTION_IDLE:
1249 		delay(ADB_DELAY);	/* short delay is required before the
1250 					 * first byte */
1251 
1252 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1253 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
1254 		adbInputBuffer[1] = ADB_SR();	/* get byte */
1255 		adbInputBuffer[0] = 1;
1256 		adbActionState = ADB_ACTION_IN;	/* set next state */
1257 
1258 		ADB_SET_STATE_ACKON();	/* start ACK to ADB chip */
1259 		delay(ADB_DELAY);	/* delay */
1260 		ADB_SET_STATE_ACKOFF();	/* end ACK to ADB chip */
1261 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
1262 		break;
1263 
1264 	case ADB_ACTION_IN:
1265 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1266 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();	/* get byte */
1267 		if (ADB_INTR_IS_OFF)	/* check for end of frame */
1268 			ending = 1;
1269 		else
1270 			ending = 0;
1271 
1272 		ADB_SET_STATE_ACKON();	/* start ACK to ADB chip */
1273 		delay(ADB_DELAY);	/* delay */
1274 		ADB_SET_STATE_ACKOFF();	/* end ACK to ADB chip */
1275 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
1276 
1277 		if (1 == ending) {	/* end of message? */
1278 			ADB_SET_STATE_INACTIVE();	/* signal end of frame */
1279 			/*
1280 			 * This section _should_ handle all ADB and RTC/PRAM
1281 			 * type commands, but there may be more...  Note:
1282 			 * commands are always at [4], even for rtc/pram
1283 			 * commands
1284 			 */
1285 			/* set up data for adb_pass_up */
1286 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
1287 
1288 			if ((adbWaiting == 1) &&	/* are we waiting AND */
1289 			    (adbInputBuffer[4] == adbWaitingCmd) &&	/* the cmd we sent AND */
1290 			    ((adbInputBuffer[2] == 0x00) ||	/* it's from the ADB
1291 								 * device OR */
1292 				(adbInputBuffer[2] == 0x01))) {	/* it's from the
1293 								 * PRAM/RTC device */
1294 
1295 				packet.saveBuf = adbBuffer;
1296 				packet.compRout = adbCompRout;
1297 				packet.compData = adbCompData;
1298 				packet.unsol = 0;
1299 				packet.ack_only = 0;
1300 				adb_pass_up(&packet);
1301 
1302 				adbWaitingCmd = 0;	/* reset "waiting" vars */
1303 				adbWaiting = 0;
1304 				adbBuffer = (long)0;
1305 				adbCompRout = (long)0;
1306 				adbCompData = (long)0;
1307 			} else {
1308 				packet.unsol = 1;
1309 				packet.ack_only = 0;
1310 				adb_pass_up(&packet);
1311 			}
1312 
1313 			adbActionState = ADB_ACTION_IDLE;
1314 			adbInputBuffer[0] = 0;	/* reset length */
1315 
1316 			if (adbWriteDelay == 1) {	/* were we waiting to
1317 							 * write? */
1318 				adbSentChars = 0;	/* nothing sent yet */
1319 				adbActionState = ADB_ACTION_OUT;	/* set next state */
1320 
1321 				delay(ADB_DELAY);	/* delay */
1322 				(void)intr_dispatch(0x70); /* grab any serial interrupts */
1323 
1324 				if (ADB_INTR_IS_ON) {	/* ADB intr low during
1325 							 * write */
1326 					ADB_SET_STATE_IDLE_IISI();	/* reset */
1327 					ADB_SET_SR_INPUT();	/* make sure SR is set
1328 								 * to IN */
1329 					adbSentChars = 0;	/* must start all over */
1330 					adbActionState = ADB_ACTION_IDLE;	/* new state */
1331 					adbInputBuffer[0] = 0;
1332 					/* may be able to take this out later */
1333 					delay(ADB_DELAY);	/* delay */
1334 					break;
1335 				}
1336 				ADB_SET_STATE_ACTIVE();	/* tell ADB that we want
1337 							 * to send */
1338 				ADB_SET_STATE_ACKOFF();	/* make sure */
1339 				ADB_SET_SR_OUTPUT();	/* set shift register
1340 							 * for OUT */
1341 				ADB_SR() = adbOutputBuffer[adbSentChars + 1];
1342 				ADB_SET_STATE_ACKON();	/* tell ADB byte ready
1343 							 * to shift */
1344 			}
1345 		}
1346 		break;
1347 
1348 	case ADB_ACTION_OUT:
1349 		i = ADB_SR();	/* reset SR-intr in IFR */
1350 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1351 
1352 		ADB_SET_STATE_ACKOFF();	/* finish ACK */
1353 		adbSentChars++;
1354 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
1355 			ADB_SET_STATE_IDLE_IISI();	/* reset */
1356 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1357 			adbSentChars = 0;	/* must start all over */
1358 			adbActionState = ADB_ACTION_IDLE;	/* new state */
1359 			adbInputBuffer[0] = 0;
1360 			adbWriteDelay = 1;	/* must retry when done with
1361 						 * read */
1362 			delay(ADB_DELAY);	/* delay */
1363 			(void)intr_dispatch(0x70); /* grab any serial interrupts */
1364 			goto switch_start;	/* process next state right
1365 						 * now */
1366 			break;
1367 		}
1368 		delay(ADB_DELAY);	/* required delay */
1369 		(void)intr_dispatch(0x70); /* grab any serial interrupts */
1370 
1371 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
1372 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
1373 									 * back? */
1374 				adbWaiting = 1;	/* signal waiting for return */
1375 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
1376 			} else {/* no talk, so done */
1377 				/* set up stuff for adb_pass_up */
1378 				memcpy(packet.data, adbInputBuffer,
1379 				    adbInputBuffer[0] + 1);
1380 				packet.saveBuf = adbBuffer;
1381 				packet.compRout = adbCompRout;
1382 				packet.compData = adbCompData;
1383 				packet.cmd = adbWaitingCmd;
1384 				packet.unsol = 0;
1385 				packet.ack_only = 1;
1386 				adb_pass_up(&packet);
1387 
1388 				/* reset "waiting" vars, just in case */
1389 				adbWaitingCmd = 0;
1390 				adbBuffer = (long)0;
1391 				adbCompRout = (long)0;
1392 				adbCompData = (long)0;
1393 			}
1394 
1395 			adbWriteDelay = 0;	/* done writing */
1396 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
1397 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
1398 			ADB_SET_STATE_INACTIVE();	/* end of frame */
1399 		} else {
1400 			ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* send next byte */
1401 			ADB_SET_STATE_ACKON();	/* signal byte ready to shift */
1402 		}
1403 		break;
1404 
1405 	case ADB_ACTION_NOTREADY:
1406 #ifdef ADB_DEBUG
1407 		if (adb_debug)
1408 			printf_intr("adb: not yet initialized\n");
1409 #endif
1410 		break;
1411 
1412 	default:
1413 #ifdef ADB_DEBUG
1414 		if (adb_debug)
1415 			printf_intr("intr: unknown ADB state\n");
1416 #endif
1417 		break;
1418 	}
1419 
1420 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
1421 
1422 	splx(s);		/* restore */
1423 
1424 	return;
1425 }				/* end adb_intr_IIsi */
1426 
1427 
1428 /*****************************************************************************
1429  * if the device is currently busy, and there is no data waiting to go out, then
1430  * the data is "queued" in the outgoing buffer. If we are already waiting, then
1431  * we return.
1432  * in: if (in == 0) then the command string is built from command and buffer
1433  *     if (in != 0) then in is used as the command string
1434  * buffer: additional data to be sent (used only if in == 0)
1435  *         this is also where return data is stored
1436  * compRout: the completion routine that is called when then return value
1437  *	     is received (if a return value is expected)
1438  * data: a data pointer that can be used by the completion routine
1439  * command: an ADB command to be sent (used only if in == 0)
1440  *
1441  */
1442 int
1443 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
1444 	command)
1445 {
1446 	int s, len;
1447 
1448 	if (adbActionState == ADB_ACTION_NOTREADY)
1449 		return 1;
1450 
1451 	/* Don't interrupt while we are messing with the ADB */
1452 	s = splhigh();
1453 
1454 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
1455 	    (ADB_INTR_IS_OFF)) {/* and no incoming interrupt? */
1456 
1457 	} else
1458 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
1459 			adbWriteDelay = 1;	/* if no, then we'll "queue"
1460 						 * it up */
1461 		else {
1462 			splx(s);
1463 			return 1;	/* really busy! */
1464 		}
1465 
1466 	if ((long)in == (long)0) {	/* need to convert? */
1467 		/*
1468 		 * Don't need to use adb_cmd_extra here because this section
1469 		 * will be called ONLY when it is an ADB command (no RTC or
1470 		 * PRAM)
1471 		 */
1472 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
1473 						 * doing a listen! */
1474 			len = buffer[0];	/* length of additional data */
1475 		else
1476 			len = 0;/* no additional data */
1477 
1478 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
1479 						 * data */
1480 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
1481 		adbOutputBuffer[2] = (u_char)command;	/* load command */
1482 
1483 		/* copy additional output data, if any */
1484 		memcpy(adbOutputBuffer + 3, buffer + 1, len);
1485 	} else
1486 		/* if data ready, just copy over */
1487 		memcpy(adbOutputBuffer, in, in[0] + 2);
1488 
1489 	adbSentChars = 0;	/* nothing sent yet */
1490 	adbBuffer = buffer;	/* save buffer to know where to save result */
1491 	adbCompRout = compRout;	/* save completion routine pointer */
1492 	adbCompData = data;	/* save completion routine data pointer */
1493 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
1494 
1495 	if (adbWriteDelay != 1) {	/* start command now? */
1496 		adbActionState = ADB_ACTION_OUT;	/* set next state */
1497 
1498 		ADB_SET_STATE_ACTIVE();	/* tell ADB that we want to send */
1499 		ADB_SET_STATE_ACKOFF();	/* make sure */
1500 
1501 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
1502 
1503 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
1504 
1505 		ADB_SET_STATE_ACKON();	/* tell ADB byte ready to shift */
1506 	}
1507 	adbWriteDelay = 1;	/* something in the write "queue" */
1508 
1509 	splx(s);
1510 
1511 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked? */
1512 		/* poll until byte done */
1513 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1514 		    || (adbWaiting == 1))
1515 			if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
1516 				adb_intr_IIsi(NULL); /* go process it */
1517 				if (adb_polling)
1518 					adb_soft_intr();
1519 			}
1520 
1521 	 return 0;
1522 }				/* send_adb_IIsi */
1523 
1524 void
1525 adb_iop_recv(IOP *iop, struct iop_msg *msg)
1526 {
1527 	struct adbCommand	pkt;
1528 	unsigned		flags;
1529 
1530 	if (adbActionState != ADB_ACTION_RUNNING)
1531 		return;
1532 
1533 	switch (msg->status) {
1534 	case IOP_MSGSTAT_SENT:
1535 		if (0 == adb_cmd_result(msg->msg + 1)) {
1536 			adbWaiting = 1;
1537 			adbWaitingCmd = msg->msg[2];
1538 		}
1539 		break;
1540 	case IOP_MSGSTAT_RECEIVED:
1541 	case IOP_MSGSTAT_UNEXPECTED:
1542 		flags = msg->msg[0];
1543 		if (flags != 0) {
1544 			printf("ADB FLAGS 0x%x", flags);
1545 			break;
1546 		}
1547 		if (adbWaiting &&
1548 		    (msg->msg[2] == adbWaitingCmd)) {
1549 			pkt.saveBuf = msg->msg + 1;
1550 			pkt.compRout = adbCompRout;
1551 			pkt.compData = adbCompData;
1552 			pkt.unsol = 0;
1553 			pkt.ack_only = 0;
1554 			adb_pass_up(&pkt);
1555 
1556 			adbWaitingCmd = 0;
1557 			adbWaiting = 0;
1558 		} else {
1559 			pkt.unsol = 1;
1560 			pkt.ack_only = 0;
1561 			adb_pass_up(&pkt);
1562 		}
1563 		break;
1564 	default:
1565 		return;
1566 	}
1567 }
1568 
1569 int
1570 send_adb_iop(int cmd, u_char * buffer, void *compRout, void *data)
1571 {
1572 	u_char	buff[32];
1573 	int	cnt;
1574 
1575 	if (adbActionState != ADB_ACTION_RUNNING)
1576 		return -1;
1577 
1578 	buff[0] = IOP_ADB_FL_EXPLICIT;
1579 	buff[1] = buffer[0];
1580 	buff[2] = cmd;
1581 	cnt = (int) buff[1];
1582 	memcpy(buff + 3, buffer + 1, cnt);
1583 	return iop_send_msg(ISM_IOP, IOP_CHAN_ADB, buff, cnt+3,
1584 			    adb_iop_recv, NULL);
1585 }
1586 
1587 /*
1588  * adb_pass_up is called by the interrupt-time routines.
1589  * It takes the raw packet data that was received from the
1590  * device and puts it into the queue that the upper half
1591  * processes. It then signals for a soft ADB interrupt which
1592  * will eventually call the upper half routine (adb_soft_intr).
1593  *
1594  * If in->unsol is 0, then this is either the notification
1595  * that the packet was sent (on a LISTEN, for example), or the
1596  * response from the device (on a TALK). The completion routine
1597  * is called only if the user specified one.
1598  *
1599  * If in->unsol is 1, then this packet was unsolicited and
1600  * so we look up the device in the ADB device table to determine
1601  * what it's default service routine is.
1602  *
1603  * If in->ack_only is 1, then we really only need to call
1604  * the completion routine, so don't do any other stuff.
1605  *
1606  * Note that in->data contains the packet header AND data,
1607  * while adbInbound[]->data contains ONLY data.
1608  *
1609  * Note: Called only at interrupt time. Assumes this.
1610  */
1611 void
1612 adb_pass_up(struct adbCommand *in)
1613 {
1614 	int start = 0, len = 0, cmd = 0;
1615 	ADBDataBlock block;
1616 
1617 	/* temp for testing */
1618 	/*u_char *buffer = 0;*/
1619 	/*u_char *compdata = 0;*/
1620 	/*u_char *comprout = 0;*/
1621 
1622 	if (adbInCount >= ADB_QUEUE) {
1623 #ifdef ADB_DEBUG
1624 		if (adb_debug)
1625 			printf_intr("adb: ring buffer overflow\n");
1626 #endif
1627 		return;
1628 	}
1629 
1630 	if (in->ack_only) {
1631 		len = in->data[0];
1632 		cmd = in->cmd;
1633 		start = 0;
1634 	} else {
1635 		switch (adbHardware) {
1636 		case ADB_HW_IOP:
1637 		case ADB_HW_II:
1638 			cmd = in->data[1];
1639 			if (in->data[0] < 2)
1640 				len = 0;
1641 			else
1642 				len = in->data[0]-1;
1643 			start = 1;
1644 			break;
1645 
1646 		case ADB_HW_IISI:
1647 		case ADB_HW_CUDA:
1648 			/* If it's unsolicited, accept only ADB data for now */
1649 			if (in->unsol)
1650 				if (0 != in->data[2])
1651 					return;
1652 			cmd = in->data[4];
1653 			if (in->data[0] < 5)
1654 				len = 0;
1655 			else
1656 				len = in->data[0]-4;
1657 			start = 4;
1658 			break;
1659 
1660 		case ADB_HW_PB:
1661 			cmd = in->data[1];
1662 			if (in->data[0] < 2)
1663 				len = 0;
1664 			else
1665 				len = in->data[0]-1;
1666 			start = 1;
1667 			break;
1668 
1669 		case ADB_HW_UNKNOWN:
1670 			return;
1671 		}
1672 
1673 		/* Make sure there is a valid device entry for this device */
1674 		if (in->unsol) {
1675 			/* ignore unsolicited data during adbreinit */
1676 			if (adbStarting)
1677 				return;
1678 			/* get device's comp. routine and data area */
1679 			if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
1680 				return;
1681 		}
1682 	}
1683 
1684 	/*
1685  	 * If this is an unsolicited packet, we need to fill in
1686  	 * some info so adb_soft_intr can process this packet
1687  	 * properly. If it's not unsolicited, then use what
1688  	 * the caller sent us.
1689  	 */
1690 	if (in->unsol) {
1691 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
1692 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
1693 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
1694 	} else {
1695 		adbInbound[adbInTail].compRout = (void *)in->compRout;
1696 		adbInbound[adbInTail].compData = (void *)in->compData;
1697 		adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
1698 	}
1699 
1700 #ifdef ADB_DEBUG
1701 	if (adb_debug && in->data[1] == 2)
1702 		printf_intr("adb: caught error\n");
1703 #endif
1704 
1705 	/* copy the packet data over */
1706 	/*
1707 	 * TO DO: If the *_intr routines fed their incoming data
1708 	 * directly into an adbCommand struct, which is passed to
1709 	 * this routine, then we could eliminate this copy.
1710 	 */
1711 	memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
1712 	adbInbound[adbInTail].data[0] = len;
1713 	adbInbound[adbInTail].cmd = cmd;
1714 
1715 	adbInCount++;
1716 	if (++adbInTail >= ADB_QUEUE)
1717 		adbInTail = 0;
1718 
1719 	/*
1720 	 * If the debugger is running, call upper half manually.
1721 	 * Otherwise, trigger a soft interrupt to handle the rest later.
1722 	 */
1723 	if (adb_polling)
1724 		adb_soft_intr();
1725 	else
1726 		setsoftadb();
1727 
1728 	return;
1729 }
1730 
1731 
1732 /*
1733  * Called to process the packets after they have been
1734  * placed in the incoming queue.
1735  *
1736  */
1737 void
1738 adb_soft_intr(void)
1739 {
1740 	int s;
1741 	int cmd = 0;
1742 	u_char *buffer = 0;
1743 	u_char *comprout = 0;
1744 	u_char *compdata = 0;
1745 
1746 #if 0
1747 	s = splhigh();
1748 	printf_intr("sr: %x\n", (s & 0x0700));
1749 	splx(s);
1750 #endif
1751 
1752 /*delay(2*ADB_DELAY);*/
1753 
1754 	while (adbInCount) {
1755 #ifdef ADB_DEBUG
1756 		if (adb_debug & 0x80)
1757 			printf_intr("%x %x %x ",
1758 			    adbInCount, adbInHead, adbInTail);
1759 #endif
1760 		/* get the data we need from the queue */
1761 		buffer = adbInbound[adbInHead].saveBuf;
1762 		comprout = adbInbound[adbInHead].compRout;
1763 		compdata = adbInbound[adbInHead].compData;
1764 		cmd = adbInbound[adbInHead].cmd;
1765 
1766 		/* copy over data to data area if it's valid */
1767 		/*
1768 		 * Note that for unsol packets we don't want to copy the
1769 	 	 * data anywhere, so buffer was already set to 0.
1770 	 	 * For ack_only buffer was set to 0, so don't copy.
1771 		 */
1772 		if (buffer)
1773 			memcpy(buffer, adbInbound[adbInHead].data,
1774 			    adbInbound[adbInHead].data[0] + 1);
1775 
1776 #ifdef ADB_DEBUG
1777 			if (adb_debug & 0x80) {
1778 				printf_intr("%p %p %p %x ",
1779 				    buffer, comprout, compdata, (short)cmd);
1780 				printf_intr("buf: ");
1781 				print_single(adbInbound[adbInHead].data);
1782 			}
1783 #endif
1784 
1785 		/* call default completion routine if it's valid */
1786 		if (comprout) {
1787 #ifdef __NetBSD__
1788 			__asm __volatile (
1789 			"	movml #0xffff,%%sp@- \n" /* save all regs */
1790 			"	movl %0,%%a2	\n" 	/* compdata */
1791 			"	movl %1,%%a1	\n" 	/* comprout */
1792 			"	movl %2,%%a0 	\n"	/* buffer */
1793 			"	movl %3,%%d0 	\n"	/* cmd */
1794 			"	jbsr %%a1@ 	\n"	/* go call routine */
1795 			"	movml %%sp@+,#0xffff"	/* restore all regs */
1796 			    :
1797 			    : "g"(compdata), "g"(comprout),
1798 				"g"(buffer), "g"(cmd)
1799 			    : "d0", "a0", "a1", "a2");
1800 #else					/* for macos based testing */
1801 			asm
1802 			{
1803 				movem.l a0/a1/a2/d0, -(a7)
1804 				move.l compdata, a2
1805 				move.l comprout, a1
1806 				move.l buffer, a0
1807 				move.w cmd, d0
1808 				jsr(a1)
1809 				movem.l(a7)+, d0/a2/a1/a0
1810 			}
1811 #endif
1812 		}
1813 
1814 		s = splhigh();
1815 		adbInCount--;
1816 		if (++adbInHead >= ADB_QUEUE)
1817 			adbInHead = 0;
1818 		splx(s);
1819 
1820 	}
1821 	return;
1822 }
1823 
1824 
1825 /*
1826  * This is my version of the ADBOp routine. It mainly just calls the
1827  * hardware-specific routine.
1828  *
1829  *   data 	: pointer to data area to be used by compRout
1830  *   compRout	: completion routine
1831  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
1832  *		  byte 0 = # of bytes
1833  *		: for TALK: points to place to save return data
1834  *   command	: the adb command to send
1835  *   result	: 0 = success
1836  *		: -1 = could not complete
1837  */
1838 int
1839 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1840 {
1841 	int result;
1842 
1843 	switch (adbHardware) {
1844 	case ADB_HW_II:
1845 		result = send_adb_II((u_char *)0, (u_char *)buffer,
1846 		    (void *)compRout, (void *)data, (int)command);
1847 		if (result == 0)
1848 			return 0;
1849 		else
1850 			return -1;
1851 		break;
1852 
1853 	case ADB_HW_IOP:
1854 #ifdef __notyet__
1855 		result = send_adb_iop((int)command, (u_char *)buffer,
1856 		    (void *)compRout, (void *)data);
1857 		if (result == 0)
1858 			return 0;
1859 		else
1860 #endif
1861 			return -1;
1862 		break;
1863 
1864 	case ADB_HW_IISI:
1865 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1866 		    (void *)compRout, (void *)data, (int)command);
1867 		/*
1868 		 * I wish I knew why this delay is needed. It usually needs to
1869 		 * be here when several commands are sent in close succession,
1870 		 * especially early in device probes when doing collision
1871 		 * detection. It must be some race condition. Sigh. - jpw
1872 		 */
1873 		delay(100);
1874 		if (result == 0)
1875 			return 0;
1876 		else
1877 			return -1;
1878 		break;
1879 
1880 	case ADB_HW_PB:
1881 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
1882 		    (void *)data, (int)command);
1883 
1884 		if (result == 0)
1885 			return 0;
1886 		else
1887 			return -1;
1888 		break;
1889 
1890 	case ADB_HW_CUDA:
1891 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1892 		    (void *)compRout, (void *)data, (int)command);
1893 		if (result == 0)
1894 			return 0;
1895 		else
1896 			return -1;
1897 		break;
1898 
1899 	case ADB_HW_UNKNOWN:
1900 	default:
1901 		return -1;
1902 	}
1903 }
1904 
1905 
1906 /*
1907  * adb_hw_setup
1908  * This routine sets up the possible machine specific hardware
1909  * config (mainly VIA settings) for the various models.
1910  */
1911 void
1912 adb_hw_setup(void)
1913 {
1914 	volatile int i;
1915 	u_char send_string[ADB_MAX_MSG_LENGTH];
1916 
1917 	switch (adbHardware) {
1918 	case ADB_HW_II:
1919 		via1_register_irq(2, adb_intr_II, NULL);
1920 
1921 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1922 						 * outputs */
1923 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1924 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1925 							 * to IN (II, IIsi) */
1926 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1927 							 * hardware (II, IIsi) */
1928 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1929 						 * code only */
1930 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1931 						 * are on (II, IIsi) */
1932 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
1933 
1934 		ADB_VIA_CLR_INTR();	/* clear interrupt */
1935 		break;
1936 
1937 	case ADB_HW_IOP:
1938 		via_reg(VIA1, vIER) = 0x84;
1939 		via_reg(VIA1, vIFR) = 0x04;
1940 #ifdef __notyet__
1941 		adbActionState = ADB_ACTION_RUNNING;
1942 #endif
1943 		break;
1944 
1945 	case ADB_HW_IISI:
1946 		via1_register_irq(2, adb_intr_IIsi, NULL);
1947 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1948 						 * outputs */
1949 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1950 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1951 							 * to IN (II, IIsi) */
1952 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1953 							 * hardware (II, IIsi) */
1954 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1955 						 * code only */
1956 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1957 						 * are on (II, IIsi) */
1958 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
1959 
1960 		/* get those pesky clock ticks we missed while booting */
1961 		for (i = 0; i < 30; i++) {
1962 			delay(ADB_DELAY);
1963 			adb_hw_setup_IIsi(send_string);
1964 #ifdef ADB_DEBUG
1965 			if (adb_debug) {
1966 				printf_intr("adb: cleanup: ");
1967 				print_single(send_string);
1968 			}
1969 #endif
1970 			delay(ADB_DELAY);
1971 			if (ADB_INTR_IS_OFF)
1972 				break;
1973 		}
1974 		break;
1975 
1976 	case ADB_HW_PB:
1977 		/*
1978 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
1979 		 * pm_direct.h?
1980 		 */
1981 		pm_hw_setup();
1982 		break;
1983 
1984 	case ADB_HW_CUDA:
1985 		via1_register_irq(2, adb_intr_cuda, NULL);
1986 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1987 						 * outputs */
1988 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1989 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1990 							 * to IN */
1991 		via_reg(VIA1, vACR) = (via_reg(VIA1, vACR) | 0x0c) & ~0x10;
1992 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1993 							 * hardware */
1994 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1995 						 * code only */
1996 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1997 						 * are on */
1998 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
1999 
2000 		/* sort of a device reset */
2001 		i = ADB_SR();	/* clear interrupt */
2002 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
2003 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
2004 		delay(ADB_DELAY);
2005 		ADB_SET_STATE_TIP();	/* signal start of frame */
2006 		delay(ADB_DELAY);
2007 		ADB_TOGGLE_STATE_ACK_CUDA();
2008 		delay(ADB_DELAY);
2009 		ADB_CLR_STATE_TIP();
2010 		delay(ADB_DELAY);
2011 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
2012 		i = ADB_SR();	/* clear interrupt */
2013 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
2014 		break;
2015 
2016 	case ADB_HW_UNKNOWN:
2017 	default:
2018 		via_reg(VIA1, vIER) = 0x04;	/* turn interrupts off - TO
2019 						 * DO: turn PB ints off? */
2020 		return;
2021 		break;
2022 	}
2023 }
2024 
2025 
2026 /*
2027  * adb_hw_setup_IIsi
2028  * This is sort of a "read" routine that forces the adb hardware through a read cycle
2029  * if there is something waiting. This helps "clean up" any commands that may have gotten
2030  * stuck or stopped during the boot process.
2031  *
2032  */
2033 void
2034 adb_hw_setup_IIsi(u_char * buffer)
2035 {
2036 	int i;
2037 	int dummy;
2038 	int s;
2039 	long my_time;
2040 	int endofframe;
2041 
2042 	delay(ADB_DELAY);
2043 
2044 	i = 1;			/* skip over [0] */
2045 	s = splhigh();		/* block ALL interrupts while we are working */
2046 	ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
2047 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
2048 	/* this is required, especially on faster machines */
2049 	delay(ADB_DELAY);
2050 
2051 	if (ADB_INTR_IS_ON) {
2052 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
2053 
2054 		endofframe = 0;
2055 		while (0 == endofframe) {
2056 			/*
2057 			 * Poll for ADB interrupt and watch for timeout.
2058 			 * If time out, keep going in hopes of not hanging
2059 			 * the ADB chip - I think
2060 			 */
2061 			my_time = ADB_DELAY * 5;
2062 			while ((ADB_SR_INTR_IS_OFF) && (my_time-- > 0))
2063 				dummy = via_reg(VIA1, vBufB);
2064 
2065 			buffer[i++] = ADB_SR();	/* reset interrupt flag by
2066 						 * reading vSR */
2067 			/*
2068 			 * Perhaps put in a check here that ignores all data
2069 			 * after the first ADB_MAX_MSG_LENGTH bytes ???
2070 			 */
2071 			if (ADB_INTR_IS_OFF)	/* check for end of frame */
2072 				endofframe = 1;
2073 
2074 			ADB_SET_STATE_ACKON();	/* send ACK to ADB chip */
2075 			delay(ADB_DELAY);	/* delay */
2076 			ADB_SET_STATE_ACKOFF();	/* send ACK to ADB chip */
2077 		}
2078 		ADB_SET_STATE_INACTIVE();	/* signal end of frame and
2079 						 * delay */
2080 
2081 		/* probably don't need to delay this long */
2082 		delay(ADB_DELAY);
2083 	}
2084 	buffer[0] = --i;	/* [0] is length of message */
2085 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
2086 	splx(s);		/* restore interrupts */
2087 
2088 	return;
2089 }				/* adb_hw_setup_IIsi */
2090 
2091 
2092 
2093 /*
2094  * adb_reinit sets up the adb stuff
2095  *
2096  */
2097 void
2098 adb_reinit(void)
2099 {
2100 	u_char send_string[ADB_MAX_MSG_LENGTH];
2101 	ADBDataBlock data;	/* temp. holder for getting device info */
2102 	volatile int i, x;
2103 	int s;
2104 	int command;
2105 	int result;
2106 	int saveptr;		/* point to next free relocation address */
2107 	int device;
2108 	int nonewtimes;		/* times thru loop w/o any new devices */
2109 
2110 	adb_setup_hw_type();	/* setup hardware type */
2111 
2112 	/* Make sure we are not interrupted while building the table. */
2113 	/* ints must be on for PB & IOP (at least, for now) */
2114 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2115 		s = splhigh();
2116 	else
2117 		s = 0;		/* XXX shut the compiler up*/
2118 
2119 	ADBNumDevices = 0;	/* no devices yet */
2120 
2121 	/* Let intr routines know we are running reinit */
2122 	adbStarting = 1;
2123 
2124 	/*
2125 	 * Initialize the ADB table.  For now, we'll always use the same table
2126 	 * that is defined at the beginning of this file - no mallocs.
2127 	 */
2128 	for (i = 0; i < 16; i++) {
2129 		ADBDevTable[i].devType = 0;
2130 		ADBDevTable[i].origAddr = ADBDevTable[i].currentAddr = 0;
2131 	}
2132 
2133 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
2134 
2135 	delay(1000);
2136 
2137 	/* send an ADB reset first */
2138 	(void)adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
2139 	delay(3000);
2140 
2141 	/*
2142 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
2143 	 * which device addresses are in use and which are free. For each
2144 	 * address that is in use, move the device at that address to a higher
2145 	 * free address. Continue doing this at that address until no device
2146 	 * responds at that address. Then move the last device that was moved
2147 	 * back to the original address. Do this for the remaining addresses
2148 	 * that we determined were in use.
2149 	 *
2150 	 * When finished, do this entire process over again with the updated
2151 	 * list of in use addresses. Do this until no new devices have been
2152 	 * found in 20 passes though the in use address list. (This probably
2153 	 * seems long and complicated, but it's the best way to detect multiple
2154 	 * devices at the same address - sometimes it takes a couple of tries
2155 	 * before the collision is detected.)
2156 	 */
2157 
2158 	/* initial scan through the devices */
2159 	for (i = 1; i < 16; i++) {
2160 		command = ADBTALK(i, 3);
2161 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
2162 		    (Ptr)0, (short)command);
2163 
2164 		if (result == 0 && send_string[0] != 0) {
2165 			/* found a device */
2166 			++ADBNumDevices;
2167 			KASSERT(ADBNumDevices < 16);
2168 			ADBDevTable[ADBNumDevices].devType =
2169 				(int)(send_string[2]);
2170 			ADBDevTable[ADBNumDevices].origAddr = i;
2171 			ADBDevTable[ADBNumDevices].currentAddr = i;
2172 			ADBDevTable[ADBNumDevices].DataAreaAddr =
2173 			    (long)0;
2174 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
2175 			pm_check_adb_devices(i);	/* tell pm driver device
2176 							 * is here */
2177 		}
2178 	}
2179 
2180 	/* find highest unused address */
2181 	for (saveptr = 15; saveptr > 0; saveptr--)
2182 		if (-1 == get_adb_info(&data, saveptr))
2183 			break;
2184 
2185 #ifdef ADB_DEBUG
2186 	if (adb_debug & 0x80) {
2187 		printf_intr("first free is: 0x%02x\n", saveptr);
2188 		printf_intr("devices: %i\n", ADBNumDevices);
2189 	}
2190 #endif
2191 
2192 	nonewtimes = 0;		/* no loops w/o new devices */
2193 	while (saveptr > 0 && nonewtimes++ < 11) {
2194 		for (i = 1;saveptr > 0 && i <= ADBNumDevices; i++) {
2195 			device = ADBDevTable[i].currentAddr;
2196 #ifdef ADB_DEBUG
2197 			if (adb_debug & 0x80)
2198 				printf_intr("moving device 0x%02x to 0x%02x "
2199 				    "(index 0x%02x)  ", device, saveptr, i);
2200 #endif
2201 
2202 			/* send TALK R3 to address */
2203 			command = ADBTALK(device, 3);
2204 			(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2205 			    (Ptr)0, (short)command);
2206 
2207 			/* move device to higher address */
2208 			command = ADBLISTEN(device, 3);
2209 			send_string[0] = 2;
2210 			send_string[1] = (u_char)(saveptr | 0x60);
2211 			send_string[2] = 0xfe;
2212 			(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2213 			    (Ptr)0, (short)command);
2214 			delay(1000);
2215 
2216 			/* send TALK R3 - anthing at new address? */
2217 			command = ADBTALK(saveptr, 3);
2218 			send_string[0] = 0;
2219 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2220 			    (Ptr)0, (short)command);
2221 			delay(1000);
2222 
2223 			if (result != 0 || send_string[0] == 0) {
2224 				/*
2225 				 * maybe there's a communication breakdown;
2226 				 * just in case, move it back from whence it
2227 				 * came, and we'll try again later
2228 				 */
2229 				command = ADBLISTEN(saveptr, 3);
2230 				send_string[0] = 2;
2231 				send_string[1] = (u_char)(device | 0x60);
2232 				send_string[2] = 0x00;
2233 				(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2234 				    (Ptr)0, (short)command);
2235 #ifdef ADB_DEBUG
2236 				if (adb_debug & 0x80)
2237 					printf_intr("failed, continuing\n");
2238 #endif
2239 				delay(1000);
2240 				continue;
2241 			}
2242 
2243 			/* send TALK R3 - anything at old address? */
2244 			command = ADBTALK(device, 3);
2245 			send_string[0] = 0;
2246 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2247 			    (Ptr)0, (short)command);
2248 			if (result == 0 && send_string[0] != 0) {
2249 				/* new device found */
2250 				/* update data for previously moved device */
2251 				ADBDevTable[i].currentAddr = saveptr;
2252 #ifdef ADB_DEBUG
2253 				if (adb_debug & 0x80)
2254 					printf_intr("old device at index %i\n",i);
2255 #endif
2256 				/* add new device in table */
2257 #ifdef ADB_DEBUG
2258 				if (adb_debug & 0x80)
2259 					printf_intr("new device found\n");
2260 #endif
2261 				if (saveptr > ADBNumDevices) {
2262 					++ADBNumDevices;
2263 					KASSERT(ADBNumDevices < 16);
2264 				}
2265 				ADBDevTable[ADBNumDevices].devType =
2266 					(int)(send_string[2]);
2267 				ADBDevTable[ADBNumDevices].origAddr = device;
2268 				ADBDevTable[ADBNumDevices].currentAddr = device;
2269 				/* These will be set correctly in adbsys.c */
2270 				/* Until then, unsol. data will be ignored. */
2271 				ADBDevTable[ADBNumDevices].DataAreaAddr =
2272 				    (long)0;
2273 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
2274 				    (void *)0;
2275 				/* find next unused address */
2276 				for (x = saveptr; x > 0; x--) {
2277 					if (-1 == get_adb_info(&data, x)) {
2278 						saveptr = x;
2279 						break;
2280 					}
2281 				}
2282 				if (x == 0)
2283 					saveptr = 0;
2284 #ifdef ADB_DEBUG
2285 				if (adb_debug & 0x80)
2286 					printf_intr("new free is 0x%02x\n",
2287 					    saveptr);
2288 #endif
2289 				nonewtimes = 0;
2290 				/* tell pm driver device is here */
2291 				pm_check_adb_devices(device);
2292 			} else {
2293 #ifdef ADB_DEBUG
2294 				if (adb_debug & 0x80)
2295 					printf_intr("moving back...\n");
2296 #endif
2297 				/* move old device back */
2298 				command = ADBLISTEN(saveptr, 3);
2299 				send_string[0] = 2;
2300 				send_string[1] = (u_char)(device | 0x60);
2301 				send_string[2] = 0xfe;
2302 				(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2303 				    (Ptr)0, (short)command);
2304 				delay(1000);
2305 			}
2306 		}
2307 	}
2308 
2309 #ifdef ADB_DEBUG
2310 	if (adb_debug) {
2311 		for (i = 1; i <= ADBNumDevices; i++) {
2312 			x = get_ind_adb_info(&data, i);
2313 			if (x != -1)
2314 				printf_intr("index 0x%x, addr 0x%x, type 0x%hx\n",
2315 				    i, x, data.devType);
2316 		}
2317 	}
2318 #endif
2319 
2320 #ifndef MRG_ADB
2321 	/* enable the programmer's switch, if we have one */
2322 	adb_prog_switch_enable();
2323 #endif
2324 
2325 #ifdef ADB_DEBUG
2326 	if (adb_debug) {
2327 		if (0 == ADBNumDevices)	/* tell user if no devices found */
2328 			printf_intr("adb: no devices found\n");
2329 	}
2330 #endif
2331 
2332 	adbStarting = 0;	/* not starting anymore */
2333 #ifdef ADB_DEBUG
2334 	if (adb_debug)
2335 		printf_intr("adb: ADBReInit complete\n");
2336 #endif
2337 
2338 	if (adbHardware == ADB_HW_CUDA)
2339 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
2340 		    (void *)adb_cuda_tickle, NULL);
2341 
2342 	/* ints must be on for PB & IOP (at least, for now) */
2343 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2344 		splx(s);
2345 
2346 	return;
2347 }
2348 
2349 
2350 /*
2351  * adb_comp_exec
2352  * This is a general routine that calls the completion routine if there is one.
2353  * NOTE: This routine is now only used by pm_direct.c
2354  *       All the code in this file (adb_direct.c) uses
2355  *       the adb_pass_up routine now.
2356  */
2357 void
2358 adb_comp_exec(void)
2359 {
2360 	if ((long)0 != adbCompRout) /* don't call if empty return location */
2361 #ifdef __NetBSD__
2362 		__asm __volatile(
2363 		"	movml #0xffff,%%sp@- \n" /* save all registers */
2364 		"	movl %0,%%a2 \n"	/* adbCompData */
2365 		"	movl %1,%%a1 \n"	/* adbCompRout */
2366 		"	movl %2,%%a0 \n"	/* adbBuffer */
2367 		"	movl %3,%%d0 \n"	/* adbWaitingCmd */
2368 		"	jbsr %%a1@ \n"		/* go call the routine */
2369 		"	movml %%sp@+,#0xffff"	/* restore all registers */
2370 		    :
2371 		    : "g"(adbCompData), "g"(adbCompRout),
2372 			"g"(adbBuffer), "g"(adbWaitingCmd)
2373 		    : "d0", "a0", "a1", "a2");
2374 #else /* for Mac OS-based testing */
2375 		asm {
2376 			movem.l a0/a1/a2/d0, -(a7)
2377 			move.l adbCompData, a2
2378 			move.l adbCompRout, a1
2379 			move.l adbBuffer, a0
2380 			move.w adbWaitingCmd, d0
2381 			jsr(a1)
2382 			movem.l(a7) +, d0/a2/a1/a0
2383 		}
2384 #endif
2385 }
2386 
2387 
2388 /*
2389  * adb_cmd_result
2390  *
2391  * This routine lets the caller know whether the specified adb command string
2392  * should expect a returned result, such as a TALK command.
2393  *
2394  * returns: 0 if a result should be expected
2395  *          1 if a result should NOT be expected
2396  */
2397 int
2398 adb_cmd_result(u_char *in)
2399 {
2400 	switch (adbHardware) {
2401 	case ADB_HW_IOP:
2402 	case ADB_HW_II:
2403 		/* was it an ADB talk command? */
2404 		if ((in[1] & 0x0c) == 0x0c)
2405 			return 0;
2406 		return 1;
2407 
2408 	case ADB_HW_IISI:
2409 	case ADB_HW_CUDA:
2410 		/* was it an ADB talk command? */
2411 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
2412 			return 0;
2413 		/* was it an RTC/PRAM read date/time? */
2414 		if ((in[1] == 0x01) && (in[2] == 0x03))
2415 			return 0;
2416 		return 1;
2417 
2418 	case ADB_HW_PB:
2419 		return 1;
2420 
2421 	case ADB_HW_UNKNOWN:
2422 	default:
2423 		return 1;
2424 	}
2425 }
2426 
2427 
2428 /*
2429  * adb_cmd_extra
2430  *
2431  * This routine lets the caller know whether the specified adb command string
2432  * may have extra data appended to the end of it, such as a LISTEN command.
2433  *
2434  * returns: 0 if extra data is allowed
2435  *          1 if extra data is NOT allowed
2436  */
2437 int
2438 adb_cmd_extra(u_char *in)
2439 {
2440 	switch (adbHardware) {
2441 	case ADB_HW_II:
2442 	case ADB_HW_IOP:
2443 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
2444 			return 0;
2445 		return 1;
2446 
2447 	case ADB_HW_IISI:
2448 	case ADB_HW_CUDA:
2449 		/*
2450 		 * TO DO: support needs to be added to recognize RTC and PRAM
2451 		 * commands
2452 		 */
2453 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
2454 			return 0;
2455 		/* add others later */
2456 		return 1;
2457 
2458 	case ADB_HW_PB:
2459 		return 1;
2460 
2461 	case ADB_HW_UNKNOWN:
2462 	default:
2463 		return 1;
2464 	}
2465 }
2466 
2467 
2468 void
2469 adb_setup_hw_type(void)
2470 {
2471 	long response;
2472 
2473 	response = mac68k_machine.machineid;
2474 
2475 	/*
2476 	 * Determine what type of ADB hardware we are running on.
2477 	 */
2478 	switch (response) {
2479 	case MACH_MACC610:		/* Centris 610 */
2480 	case MACH_MACC650:		/* Centris 650 */
2481 	case MACH_MACII:		/* II */
2482 	case MACH_MACIICI:		/* IIci */
2483 	case MACH_MACIICX:		/* IIcx */
2484 	case MACH_MACIIX:		/* IIx */
2485 	case MACH_MACQ610:		/* Quadra 610 */
2486 	case MACH_MACQ650:		/* Quadra 650 */
2487 	case MACH_MACQ700:		/* Quadra 700 */
2488 	case MACH_MACQ800:		/* Quadra 800 */
2489 	case MACH_MACSE30:		/* SE/30 */
2490 		adbHardware = ADB_HW_II;
2491 #ifdef ADB_DEBUG
2492 		if (adb_debug)
2493 			printf_intr("adb: using II series hardware support\n");
2494 #endif
2495 		break;
2496 
2497 	case MACH_MACCLASSICII:		/* Classic II */
2498 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
2499 	case MACH_MACLCIII:		/* LC III, Performa 450 */
2500 	case MACH_MACIISI:		/* IIsi */
2501 	case MACH_MACIIVI:		/* IIvi */
2502 	case MACH_MACIIVX:		/* IIvx */
2503 	case MACH_MACP460:		/* Performa 460/465/467 */
2504 	case MACH_MACP600:		/* Performa 600 */
2505 		adbHardware = ADB_HW_IISI;
2506 #ifdef ADB_DEBUG
2507 		if (adb_debug)
2508 			printf_intr("adb: using IIsi series hardware support\n");
2509 #endif
2510 		break;
2511 
2512 	case MACH_MACPB140:		/* PowerBook 140 */
2513 	case MACH_MACPB145:		/* PowerBook 145 */
2514 	case MACH_MACPB160:		/* PowerBook 160 */
2515 	case MACH_MACPB165:		/* PowerBook 165 */
2516 	case MACH_MACPB165C:		/* PowerBook 165c */
2517 	case MACH_MACPB170:		/* PowerBook 170 */
2518 	case MACH_MACPB180:		/* PowerBook 180 */
2519 	case MACH_MACPB180C:		/* PowerBook 180c */
2520 		adbHardware = ADB_HW_PB;
2521 		pm_setup_adb();
2522 #ifdef ADB_DEBUG
2523 		if (adb_debug)
2524 			printf_intr("adb: using PowerBook 100-series hardware support\n");
2525 #endif
2526 		break;
2527 
2528 	case MACH_MACPB150:		/* PowerBook 150 */
2529 	case MACH_MACPB210:		/* PowerBook Duo 210 */
2530 	case MACH_MACPB230:		/* PowerBook Duo 230 */
2531 	case MACH_MACPB250:		/* PowerBook Duo 250 */
2532 	case MACH_MACPB270:		/* PowerBook Duo 270 */
2533 	case MACH_MACPB280:		/* PowerBook Duo 280 */
2534 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
2535 	case MACH_MACPB500:		/* PowerBook 500 series */
2536 	case MACH_MACPB190:		/* PowerBook 190 */
2537 	case MACH_MACPB190CS:		/* PowerBook 190cs */
2538 		adbHardware = ADB_HW_PB;
2539 		pm_setup_adb();
2540 #ifdef ADB_DEBUG
2541 		if (adb_debug)
2542 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2543 #endif
2544 		break;
2545 
2546 	case MACH_MACC660AV:		/* Centris 660AV */
2547 	case MACH_MACCCLASSIC:		/* Color Classic */
2548 	case MACH_MACCCLASSICII:	/* Color Classic II */
2549 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
2550 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
2551 	case MACH_MACLC520:		/* LC 520 */
2552 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2553 	case MACH_MACP550:		/* LC 550, Performa 550 */
2554 	case MACH_MACTV:		/* Macintosh TV */
2555 	case MACH_MACP580:		/* Performa 580/588 */
2556 	case MACH_MACQ605:		/* Quadra 605 */
2557 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
2558 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2559 	case MACH_MACQ840AV:		/* Quadra 840AV */
2560 		adbHardware = ADB_HW_CUDA;
2561 #ifdef ADB_DEBUG
2562 		if (adb_debug)
2563 			printf_intr("adb: using Cuda series hardware support\n");
2564 #endif
2565 		break;
2566 
2567 	case MACH_MACQ900:		/* Quadra 900 */
2568 	case MACH_MACQ950:		/* Quadra 950 */
2569 	case MACH_MACIIFX:		/* Mac IIfx   */
2570 		adbHardware = ADB_HW_IOP;
2571 		iop_register_listener(ISM_IOP, IOP_CHAN_ADB, adb_iop_recv, NULL);
2572 #ifdef ADB_DEBUG
2573 		if (adb_debug)
2574 			printf_intr("adb: using IOP-based ADB\n");
2575 #endif
2576 		break;
2577 
2578 	default:
2579 		adbHardware = ADB_HW_UNKNOWN;
2580 #ifdef ADB_DEBUG
2581 		if (adb_debug) {
2582 			printf_intr("adb: hardware type unknown for this machine\n");
2583 			printf_intr("adb: ADB support is disabled\n");
2584 		}
2585 #endif
2586 		break;
2587 	}
2588 
2589 	/*
2590 	 * Determine whether this machine has ADB based soft power.
2591 	 */
2592 	switch (response) {
2593 	case MACH_MACCCLASSIC:		/* Color Classic */
2594 	case MACH_MACCCLASSICII:	/* Color Classic II */
2595 	case MACH_MACIISI:		/* IIsi */
2596 	case MACH_MACIIVI:		/* IIvi */
2597 	case MACH_MACIIVX:		/* IIvx */
2598 	case MACH_MACLC520:		/* LC 520 */
2599 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2600 	case MACH_MACP550:		/* LC 550, Performa 550 */
2601 	case MACH_MACTV:		/* Macintosh TV */
2602 	case MACH_MACP580:		/* Performa 580/588 */
2603 	case MACH_MACP600:		/* Performa 600 */
2604 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2605 	case MACH_MACQ840AV:		/* Quadra 840AV */
2606 		adbSoftPower = 1;
2607 		break;
2608 	}
2609 }
2610 
2611 int
2612 count_adbs(void)
2613 {
2614 	int i;
2615 	int found;
2616 
2617 	found = 0;
2618 
2619 	for (i = 1; i < 16; i++)
2620 		if (0 != ADBDevTable[i].currentAddr)
2621 			found++;
2622 
2623 	return found;
2624 }
2625 
2626 int
2627 get_ind_adb_info(ADBDataBlock * info, int index)
2628 {
2629 	if ((index < 1) || (index > 15))	/* check range 1-15 */
2630 		return (-1);
2631 
2632 #ifdef ADB_DEBUG
2633 	if (adb_debug & 0x80)
2634 		printf_intr("index 0x%x devType is: 0x%x\n", index,
2635 		    ADBDevTable[index].devType);
2636 #endif
2637 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
2638 		return (-1);
2639 
2640 	info->devType = (unsigned char)(ADBDevTable[index].devType);
2641 	info->origADBAddr = (unsigned char)(ADBDevTable[index].origAddr);
2642 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
2643 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
2644 
2645 	return (ADBDevTable[index].currentAddr);
2646 }
2647 
2648 int
2649 get_adb_info(ADBDataBlock * info, int adbAddr)
2650 {
2651 	int i;
2652 
2653 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2654 		return (-1);
2655 
2656 	for (i = 1; i < 15; i++)
2657 		if (ADBDevTable[i].currentAddr == adbAddr) {
2658 			info->devType = (unsigned char)(ADBDevTable[i].devType);
2659 			info->origADBAddr = (unsigned char)(ADBDevTable[i].origAddr);
2660 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2661 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2662 			return 0;	/* found */
2663 		}
2664 
2665 	return (-1);		/* not found */
2666 }
2667 
2668 int
2669 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
2670 {
2671 	int i;
2672 
2673 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2674 		return (-1);
2675 
2676 	for (i = 1; i < 15; i++)
2677 		if (ADBDevTable[i].currentAddr == adbAddr) {
2678 			ADBDevTable[i].ServiceRtPtr =
2679 			    (void *)(info->siServiceRtPtr);
2680 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2681 			return 0;	/* found */
2682 		}
2683 
2684 	return (-1);		/* not found */
2685 
2686 }
2687 
2688 #ifndef MRG_ADB
2689 long
2690 mrg_adbintr(void)
2691 {
2692 	adb_intr(NULL);
2693 	return 1;	/* mimic mrg_adbintr in macrom.h just in case */
2694 }
2695 
2696 long
2697 mrg_pmintr(void)
2698 {
2699 	pm_intr(NULL);
2700 	return 1;	/* mimic mrg_pmintr in macrom.h just in case */
2701 }
2702 
2703 /* caller should really use machine-independant version: getPramTime */
2704 /* this version does pseudo-adb access only */
2705 int
2706 adb_read_date_time(unsigned long *time)
2707 {
2708 	u_char output[ADB_MAX_MSG_LENGTH];
2709 	int result;
2710 	volatile int flag = 0;
2711 
2712 	switch (adbHardware) {
2713 	case ADB_HW_II:
2714 		return -1;
2715 
2716 	case ADB_HW_IOP:
2717 		return -1;
2718 
2719 	case ADB_HW_IISI:
2720 		output[0] = 0x02;	/* 2 byte message */
2721 		output[1] = 0x01;	/* to pram/rtc device */
2722 		output[2] = 0x03;	/* read date/time */
2723 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
2724 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
2725 		if (result != 0)	/* exit if not sent */
2726 			return -1;
2727 
2728 		while (0 == flag)	/* wait for result */
2729 			;
2730 
2731 		*time = (long)(*(long *)(output + 1));
2732 		return 0;
2733 
2734 	case ADB_HW_PB:
2735 		return -1;
2736 
2737 	case ADB_HW_CUDA:
2738 		output[0] = 0x02;	/* 2 byte message */
2739 		output[1] = 0x01;	/* to pram/rtc device */
2740 		output[2] = 0x03;	/* read date/time */
2741 		result = send_adb_cuda((u_char *)output, (u_char *)output,
2742 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2743 		if (result != 0)	/* exit if not sent */
2744 			return -1;
2745 
2746 		while (0 == flag)	/* wait for result */
2747 			;
2748 
2749 		*time = (long)(*(long *)(output + 1));
2750 		return 0;
2751 
2752 	case ADB_HW_UNKNOWN:
2753 	default:
2754 		return -1;
2755 	}
2756 }
2757 
2758 /* caller should really use machine-independant version: setPramTime */
2759 /* this version does pseudo-adb access only */
2760 int
2761 adb_set_date_time(unsigned long time)
2762 {
2763 	u_char output[ADB_MAX_MSG_LENGTH];
2764 	int result;
2765 	volatile int flag = 0;
2766 
2767 	switch (adbHardware) {
2768 	case ADB_HW_II:
2769 		return -1;
2770 
2771 	case ADB_HW_IOP:
2772 		return -1;
2773 
2774 	case ADB_HW_IISI:
2775 		output[0] = 0x06;	/* 6 byte message */
2776 		output[1] = 0x01;	/* to pram/rtc device */
2777 		output[2] = 0x09;	/* set date/time */
2778 		output[3] = (u_char)(time >> 24);
2779 		output[4] = (u_char)(time >> 16);
2780 		output[5] = (u_char)(time >> 8);
2781 		output[6] = (u_char)(time);
2782 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2783 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2784 		if (result != 0)	/* exit if not sent */
2785 			return -1;
2786 
2787 		while (0 == flag)	/* wait for send to finish */
2788 			;
2789 
2790 		return 0;
2791 
2792 	case ADB_HW_PB:
2793 		return -1;
2794 
2795 	case ADB_HW_CUDA:
2796 		output[0] = 0x06;	/* 6 byte message */
2797 		output[1] = 0x01;	/* to pram/rtc device */
2798 		output[2] = 0x09;	/* set date/time */
2799 		output[3] = (u_char)(time >> 24);
2800 		output[4] = (u_char)(time >> 16);
2801 		output[5] = (u_char)(time >> 8);
2802 		output[6] = (u_char)(time);
2803 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2804 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2805 		if (result != 0)	/* exit if not sent */
2806 			return -1;
2807 
2808 		while (0 == flag)	/* wait for send to finish */
2809 			;
2810 
2811 		return 0;
2812 
2813 	case ADB_HW_UNKNOWN:
2814 	default:
2815 		return -1;
2816 	}
2817 }
2818 
2819 
2820 int
2821 adb_poweroff(void)
2822 {
2823 	u_char output[ADB_MAX_MSG_LENGTH];
2824 	int result;
2825 
2826 	if (!adbSoftPower)
2827 		return -1;
2828 
2829 	adb_polling = 1;
2830 
2831 	switch (adbHardware) {
2832 	case ADB_HW_IISI:
2833 		output[0] = 0x02;	/* 2 byte message */
2834 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2835 		output[2] = 0x0a;	/* set date/time */
2836 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2837 		    (void *)0, (void *)0, (int)0);
2838 		if (result != 0)	/* exit if not sent */
2839 			return -1;
2840 
2841 		for (;;);		/* wait for power off */
2842 
2843 		return 0;
2844 
2845 	case ADB_HW_PB:
2846 		return -1;
2847 
2848 	case ADB_HW_CUDA:
2849 		output[0] = 0x02;	/* 2 byte message */
2850 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2851 		output[2] = 0x0a;	/* set date/time */
2852 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2853 		    (void *)0, (void *)0, (int)0);
2854 		if (result != 0)	/* exit if not sent */
2855 			return -1;
2856 
2857 		for (;;);		/* wait for power off */
2858 
2859 		return 0;
2860 
2861 	case ADB_HW_II:			/* II models don't do ADB soft power */
2862 	case ADB_HW_IOP:		/* IOP models don't do ADB soft power */
2863 	case ADB_HW_UNKNOWN:
2864 	default:
2865 		return -1;
2866 	}
2867 }
2868 
2869 int
2870 adb_prog_switch_enable(void)
2871 {
2872 	u_char output[ADB_MAX_MSG_LENGTH];
2873 	int result;
2874 	volatile int flag = 0;
2875 
2876 	switch (adbHardware) {
2877 	case ADB_HW_IISI:
2878 		output[0] = 0x03;	/* 3 byte message */
2879 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2880 		output[2] = 0x1c;	/* prog. switch control */
2881 		output[3] = 0x01;	/* enable */
2882 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2883 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2884 		if (result != 0)	/* exit if not sent */
2885 			return -1;
2886 
2887 		while (0 == flag)	/* wait for send to finish */
2888 			;
2889 
2890 		return 0;
2891 
2892 	case ADB_HW_PB:
2893 		return -1;
2894 
2895 	case ADB_HW_II:		/* II models don't do prog. switch */
2896 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2897 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
2898 	case ADB_HW_UNKNOWN:
2899 	default:
2900 		return -1;
2901 	}
2902 }
2903 
2904 int
2905 adb_prog_switch_disable(void)
2906 {
2907 	u_char output[ADB_MAX_MSG_LENGTH];
2908 	int result;
2909 	volatile int flag = 0;
2910 
2911 	switch (adbHardware) {
2912 	case ADB_HW_IISI:
2913 		output[0] = 0x03;	/* 3 byte message */
2914 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2915 		output[2] = 0x1c;	/* prog. switch control */
2916 		output[3] = 0x01;	/* disable */
2917 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2918 			(void *)adb_op_comprout, (void *)&flag, (int)0);
2919 		if (result != 0)	/* exit if not sent */
2920 			return -1;
2921 
2922 		while (0 == flag)	/* wait for send to finish */
2923 			;
2924 
2925 		return 0;
2926 
2927 	case ADB_HW_PB:
2928 		return -1;
2929 
2930 	case ADB_HW_II:		/* II models don't do prog. switch */
2931 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2932 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
2933 	case ADB_HW_UNKNOWN:
2934 	default:
2935 		return -1;
2936 	}
2937 }
2938 
2939 int
2940 CountADBs(void)
2941 {
2942 	return (count_adbs());
2943 }
2944 
2945 void
2946 ADBReInit(void)
2947 {
2948 	adb_reinit();
2949 }
2950 
2951 int
2952 GetIndADB(ADBDataBlock * info, int index)
2953 {
2954 	return (get_ind_adb_info(info, index));
2955 }
2956 
2957 int
2958 GetADBInfo(ADBDataBlock * info, int adbAddr)
2959 {
2960 	return (get_adb_info(info, adbAddr));
2961 }
2962 
2963 int
2964 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2965 {
2966 	return (set_adb_info(info, adbAddr));
2967 }
2968 
2969 int
2970 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2971 {
2972 	return (adb_op(buffer, compRout, data, commandNum));
2973 }
2974 
2975 #endif
2976