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