xref: /netbsd-src/sys/arch/mac68k/dev/adb_direct.c (revision d974db0adae7638e51d0b459a9c2efece6ded7e4)
1 /*	$NetBSD: adb_direct.c,v 1.59 2007/10/17 19:55:13 garbled 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.59 2007/10/17 19:55:13 garbled 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 
75 #include <machine/viareg.h>
76 #include <machine/param.h>
77 #include <machine/cpu.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, 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 i, 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 		i = 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 it's 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 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
1696 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
1697 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
1698 	} else {
1699 		adbInbound[adbInTail].compRout = (void *)in->compRout;
1700 		adbInbound[adbInTail].compData = (void *)in->compData;
1701 		adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
1702 	}
1703 
1704 #ifdef ADB_DEBUG
1705 	if (adb_debug && in->data[1] == 2)
1706 		printf_intr("adb: caught error\n");
1707 #endif
1708 
1709 	/* copy the packet data over */
1710 	/*
1711 	 * TO DO: If the *_intr routines fed their incoming data
1712 	 * directly into an adbCommand struct, which is passed to
1713 	 * this routine, then we could eliminate this copy.
1714 	 */
1715 	memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
1716 	adbInbound[adbInTail].data[0] = len;
1717 	adbInbound[adbInTail].cmd = cmd;
1718 
1719 	adbInCount++;
1720 	if (++adbInTail >= ADB_QUEUE)
1721 		adbInTail = 0;
1722 
1723 	/*
1724 	 * If the debugger is running, call upper half manually.
1725 	 * Otherwise, trigger a soft interrupt to handle the rest later.
1726 	 */
1727 	if (adb_polling)
1728 		adb_soft_intr();
1729 	else
1730 		softintr_schedule(adb_softintr_cookie);
1731 
1732 	return;
1733 }
1734 
1735 
1736 /*
1737  * Called to process the packets after they have been
1738  * placed in the incoming queue.
1739  *
1740  */
1741 void
1742 adb_soft_intr(void)
1743 {
1744 	int s;
1745 	int cmd = 0;
1746 	u_char *buffer = 0;
1747 	u_char *comprout = 0;
1748 	u_char *compdata = 0;
1749 
1750 #if 0
1751 	s = splhigh();
1752 	printf_intr("sr: %x\n", (s & 0x0700));
1753 	splx(s);
1754 #endif
1755 
1756 /*delay(2*ADB_DELAY);*/
1757 
1758 	while (adbInCount) {
1759 #ifdef ADB_DEBUG
1760 		if (adb_debug & 0x80)
1761 			printf_intr("%x %x %x ",
1762 			    adbInCount, adbInHead, adbInTail);
1763 #endif
1764 		/* get the data we need from the queue */
1765 		buffer = adbInbound[adbInHead].saveBuf;
1766 		comprout = adbInbound[adbInHead].compRout;
1767 		compdata = adbInbound[adbInHead].compData;
1768 		cmd = adbInbound[adbInHead].cmd;
1769 
1770 		/* copy over data to data area if it's valid */
1771 		/*
1772 		 * Note that for unsol packets we don't want to copy the
1773 	 	 * data anywhere, so buffer was already set to 0.
1774 	 	 * For ack_only buffer was set to 0, so don't copy.
1775 		 */
1776 		if (buffer)
1777 			memcpy(buffer, adbInbound[adbInHead].data,
1778 			    adbInbound[adbInHead].data[0] + 1);
1779 
1780 #ifdef ADB_DEBUG
1781 			if (adb_debug & 0x80) {
1782 				printf_intr("%p %p %p %x ",
1783 				    buffer, comprout, compdata, (short)cmd);
1784 				printf_intr("buf: ");
1785 				print_single(adbInbound[adbInHead].data);
1786 			}
1787 #endif
1788 
1789 		/* call default completion routine if it's valid */
1790 		if (comprout) {
1791 #ifdef __NetBSD__
1792 			__asm volatile (
1793 			"	movml #0xffff,%%sp@- \n" /* save all regs */
1794 			"	movl %0,%%a2	\n" 	/* compdata */
1795 			"	movl %1,%%a1	\n" 	/* comprout */
1796 			"	movl %2,%%a0 	\n"	/* buffer */
1797 			"	movl %3,%%d0 	\n"	/* cmd */
1798 			"	jbsr %%a1@ 	\n"	/* go call routine */
1799 			"	movml %%sp@+,#0xffff"	/* restore all regs */
1800 			    :
1801 			    : "g"(compdata), "g"(comprout),
1802 				"g"(buffer), "g"(cmd)
1803 			    : "d0", "a0", "a1", "a2");
1804 #else					/* for macos based testing */
1805 			asm
1806 			{
1807 				movem.l a0/a1/a2/d0, -(a7)
1808 				move.l compdata, a2
1809 				move.l comprout, a1
1810 				move.l buffer, a0
1811 				move.w cmd, d0
1812 				jsr(a1)
1813 				movem.l(a7)+, d0/a2/a1/a0
1814 			}
1815 #endif
1816 		}
1817 
1818 		s = splhigh();
1819 		adbInCount--;
1820 		if (++adbInHead >= ADB_QUEUE)
1821 			adbInHead = 0;
1822 		splx(s);
1823 
1824 	}
1825 	return;
1826 }
1827 
1828 
1829 /*
1830  * This is my version of the ADBOp routine. It mainly just calls the
1831  * hardware-specific routine.
1832  *
1833  *   data 	: pointer to data area to be used by compRout
1834  *   compRout	: completion routine
1835  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
1836  *		  byte 0 = # of bytes
1837  *		: for TALK: points to place to save return data
1838  *   command	: the adb command to send
1839  *   result	: 0 = success
1840  *		: -1 = could not complete
1841  */
1842 int
1843 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1844 {
1845 	int result;
1846 
1847 	switch (adbHardware) {
1848 	case ADB_HW_II:
1849 		result = send_adb_II((u_char *)0, (u_char *)buffer,
1850 		    (void *)compRout, (void *)data, (int)command);
1851 		if (result == 0)
1852 			return 0;
1853 		else
1854 			return -1;
1855 		break;
1856 
1857 	case ADB_HW_IOP:
1858 #ifdef __notyet__
1859 		result = send_adb_iop((int)command, (u_char *)buffer,
1860 		    (void *)compRout, (void *)data);
1861 		if (result == 0)
1862 			return 0;
1863 		else
1864 #endif
1865 			return -1;
1866 		break;
1867 
1868 	case ADB_HW_IISI:
1869 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1870 		    (void *)compRout, (void *)data, (int)command);
1871 		/*
1872 		 * I wish I knew why this delay is needed. It usually needs to
1873 		 * be here when several commands are sent in close succession,
1874 		 * especially early in device probes when doing collision
1875 		 * detection. It must be some race condition. Sigh. - jpw
1876 		 */
1877 		delay(100);
1878 		if (result == 0)
1879 			return 0;
1880 		else
1881 			return -1;
1882 		break;
1883 
1884 	case ADB_HW_PB:
1885 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
1886 		    (void *)data, (int)command);
1887 
1888 		if (result == 0)
1889 			return 0;
1890 		else
1891 			return -1;
1892 		break;
1893 
1894 	case ADB_HW_CUDA:
1895 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1896 		    (void *)compRout, (void *)data, (int)command);
1897 		if (result == 0)
1898 			return 0;
1899 		else
1900 			return -1;
1901 		break;
1902 
1903 	case ADB_HW_UNKNOWN:
1904 	default:
1905 		return -1;
1906 	}
1907 }
1908 
1909 
1910 /*
1911  * adb_hw_setup
1912  * This routine sets up the possible machine specific hardware
1913  * config (mainly VIA settings) for the various models.
1914  */
1915 void
1916 adb_hw_setup(void)
1917 {
1918 	volatile int i;
1919 	u_char send_string[ADB_MAX_MSG_LENGTH];
1920 
1921 	switch (adbHardware) {
1922 	case ADB_HW_II:
1923 		via1_register_irq(2, adb_intr_II, NULL);
1924 
1925 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1926 						 * outputs */
1927 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1928 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1929 							 * to IN (II, IIsi) */
1930 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1931 							 * hardware (II, IIsi) */
1932 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1933 						 * code only */
1934 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1935 						 * are on (II, IIsi) */
1936 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
1937 
1938 		ADB_VIA_CLR_INTR();	/* clear interrupt */
1939 		break;
1940 
1941 	case ADB_HW_IOP:
1942 		via_reg(VIA1, vIER) = 0x84;
1943 		via_reg(VIA1, vIFR) = 0x04;
1944 #ifdef __notyet__
1945 		adbActionState = ADB_ACTION_RUNNING;
1946 #endif
1947 		break;
1948 
1949 	case ADB_HW_IISI:
1950 		via1_register_irq(2, adb_intr_IIsi, NULL);
1951 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1952 						 * outputs */
1953 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1954 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1955 							 * to IN (II, IIsi) */
1956 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1957 							 * hardware (II, IIsi) */
1958 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1959 						 * code only */
1960 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
1961 						 * are on (II, IIsi) */
1962 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
1963 
1964 		/* get those pesky clock ticks we missed while booting */
1965 		for (i = 0; i < 30; i++) {
1966 			delay(ADB_DELAY);
1967 			adb_hw_setup_IIsi(send_string);
1968 #ifdef ADB_DEBUG
1969 			if (adb_debug) {
1970 				printf_intr("adb: cleanup: ");
1971 				print_single(send_string);
1972 			}
1973 #endif
1974 			delay(ADB_DELAY);
1975 			if (ADB_INTR_IS_OFF)
1976 				break;
1977 		}
1978 		break;
1979 
1980 	case ADB_HW_PB:
1981 		/*
1982 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
1983 		 * pm_direct.h?
1984 		 */
1985 		pm_hw_setup();
1986 		break;
1987 
1988 	case ADB_HW_CUDA:
1989 		via1_register_irq(2, adb_intr_cuda, NULL);
1990 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
1991 						 * outputs */
1992 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
1993 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
1994 							 * to IN */
1995 		via_reg(VIA1, vACR) = (via_reg(VIA1, vACR) | 0x0c) & ~0x10;
1996 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1997 							 * hardware */
1998 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1999 						 * code only */
2000 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
2001 						 * are on */
2002 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
2003 
2004 		/* sort of a device reset */
2005 		i = ADB_SR();	/* clear interrupt */
2006 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
2007 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
2008 		delay(ADB_DELAY);
2009 		ADB_SET_STATE_TIP();	/* signal start of frame */
2010 		delay(ADB_DELAY);
2011 		ADB_TOGGLE_STATE_ACK_CUDA();
2012 		delay(ADB_DELAY);
2013 		ADB_CLR_STATE_TIP();
2014 		delay(ADB_DELAY);
2015 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
2016 		i = ADB_SR();	/* clear interrupt */
2017 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
2018 		break;
2019 
2020 	case ADB_HW_UNKNOWN:
2021 	default:
2022 		via_reg(VIA1, vIER) = 0x04;	/* turn interrupts off - TO
2023 						 * DO: turn PB ints off? */
2024 		return;
2025 		break;
2026 	}
2027 }
2028 
2029 
2030 /*
2031  * adb_hw_setup_IIsi
2032  * This is sort of a "read" routine that forces the adb hardware through a read cycle
2033  * if there is something waiting. This helps "clean up" any commands that may have gotten
2034  * stuck or stopped during the boot process.
2035  *
2036  */
2037 void
2038 adb_hw_setup_IIsi(u_char *buffer)
2039 {
2040 	int i;
2041 	int dummy;
2042 	int s;
2043 	long my_time;
2044 	int endofframe;
2045 
2046 	delay(ADB_DELAY);
2047 
2048 	i = 1;			/* skip over [0] */
2049 	s = splhigh();		/* block ALL interrupts while we are working */
2050 	ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
2051 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
2052 	/* this is required, especially on faster machines */
2053 	delay(ADB_DELAY);
2054 
2055 	if (ADB_INTR_IS_ON) {
2056 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
2057 
2058 		endofframe = 0;
2059 		while (0 == endofframe) {
2060 			/*
2061 			 * Poll for ADB interrupt and watch for timeout.
2062 			 * If time out, keep going in hopes of not hanging
2063 			 * the ADB chip - I think
2064 			 */
2065 			my_time = ADB_DELAY * 5;
2066 			while ((ADB_SR_INTR_IS_OFF) && (my_time-- > 0))
2067 				dummy = via_reg(VIA1, vBufB);
2068 
2069 			buffer[i++] = ADB_SR();	/* reset interrupt flag by
2070 						 * reading vSR */
2071 			/*
2072 			 * Perhaps put in a check here that ignores all data
2073 			 * after the first ADB_MAX_MSG_LENGTH bytes ???
2074 			 */
2075 			if (ADB_INTR_IS_OFF)	/* check for end of frame */
2076 				endofframe = 1;
2077 
2078 			ADB_SET_STATE_ACKON();	/* send ACK to ADB chip */
2079 			delay(ADB_DELAY);	/* delay */
2080 			ADB_SET_STATE_ACKOFF();	/* send ACK to ADB chip */
2081 		}
2082 		ADB_SET_STATE_INACTIVE();	/* signal end of frame and
2083 						 * delay */
2084 
2085 		/* probably don't need to delay this long */
2086 		delay(ADB_DELAY);
2087 	}
2088 	buffer[0] = --i;	/* [0] is length of message */
2089 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
2090 	splx(s);		/* restore interrupts */
2091 
2092 	return;
2093 }				/* adb_hw_setup_IIsi */
2094 
2095 
2096 
2097 /*
2098  * adb_reinit sets up the adb stuff
2099  *
2100  */
2101 void
2102 adb_reinit(void)
2103 {
2104 	u_char send_string[ADB_MAX_MSG_LENGTH];
2105 	ADBDataBlock data;	/* temp. holder for getting device info */
2106 	volatile int i, x;
2107 	int s;
2108 	int command;
2109 	int result;
2110 	int saveptr;		/* point to next free relocation address */
2111 	int device;
2112 	int nonewtimes;		/* times thru loop w/o any new devices */
2113 	static bool again;
2114 
2115 	if (!again) {
2116 		callout_init(&adb_cuda_tickle_ch, 0);
2117 		again = true;
2118 	}
2119 
2120 	adb_setup_hw_type();	/* setup hardware type */
2121 
2122 	/* Make sure we are not interrupted while building the table. */
2123 	/* ints must be on for PB & IOP (at least, for now) */
2124 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2125 		s = splhigh();
2126 	else
2127 		s = 0;		/* XXX shut the compiler up*/
2128 
2129 	ADBNumDevices = 0;	/* no devices yet */
2130 
2131 	/* Let intr routines know we are running reinit */
2132 	adbStarting = 1;
2133 
2134 	/*
2135 	 * Initialize the ADB table.  For now, we'll always use the same table
2136 	 * that is defined at the beginning of this file - no mallocs.
2137 	 */
2138 	for (i = 0; i < 16; i++) {
2139 		ADBDevTable[i].devType = 0;
2140 		ADBDevTable[i].origAddr = ADBDevTable[i].currentAddr = 0;
2141 	}
2142 
2143 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
2144 
2145 	delay(1000);
2146 
2147 	/* send an ADB reset first */
2148 	(void)adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
2149 	delay(3000);
2150 
2151 	/*
2152 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
2153 	 * which device addresses are in use and which are free. For each
2154 	 * address that is in use, move the device at that address to a higher
2155 	 * free address. Continue doing this at that address until no device
2156 	 * responds at that address. Then move the last device that was moved
2157 	 * back to the original address. Do this for the remaining addresses
2158 	 * that we determined were in use.
2159 	 *
2160 	 * When finished, do this entire process over again with the updated
2161 	 * list of in use addresses. Do this until no new devices have been
2162 	 * found in 20 passes though the in use address list. (This probably
2163 	 * seems long and complicated, but it's the best way to detect multiple
2164 	 * devices at the same address - sometimes it takes a couple of tries
2165 	 * before the collision is detected.)
2166 	 */
2167 
2168 	/* initial scan through the devices */
2169 	for (i = 1; i < 16; i++) {
2170 		command = ADBTALK(i, 3);
2171 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
2172 		    (Ptr)0, (short)command);
2173 
2174 		if (result == 0 && send_string[0] != 0) {
2175 			/* found a device */
2176 			++ADBNumDevices;
2177 			KASSERT(ADBNumDevices < 16);
2178 			ADBDevTable[ADBNumDevices].devType =
2179 				(int)(send_string[2]);
2180 			ADBDevTable[ADBNumDevices].origAddr = i;
2181 			ADBDevTable[ADBNumDevices].currentAddr = i;
2182 			ADBDevTable[ADBNumDevices].DataAreaAddr =
2183 			    (long)0;
2184 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
2185 			pm_check_adb_devices(i);	/* tell pm driver device
2186 							 * is here */
2187 		}
2188 	}
2189 
2190 	/* find highest unused address */
2191 	for (saveptr = 15; saveptr > 0; saveptr--)
2192 		if (-1 == get_adb_info(&data, saveptr))
2193 			break;
2194 
2195 #ifdef ADB_DEBUG
2196 	if (adb_debug & 0x80) {
2197 		printf_intr("first free is: 0x%02x\n", saveptr);
2198 		printf_intr("devices: %i\n", ADBNumDevices);
2199 	}
2200 #endif
2201 
2202 	nonewtimes = 0;		/* no loops w/o new devices */
2203 	while (saveptr > 0 && nonewtimes++ < 11) {
2204 		for (i = 1;saveptr > 0 && i <= ADBNumDevices; i++) {
2205 			device = ADBDevTable[i].currentAddr;
2206 #ifdef ADB_DEBUG
2207 			if (adb_debug & 0x80)
2208 				printf_intr("moving device 0x%02x to 0x%02x "
2209 				    "(index 0x%02x)  ", device, saveptr, i);
2210 #endif
2211 
2212 			/* send TALK R3 to address */
2213 			command = ADBTALK(device, 3);
2214 			(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2215 			    (Ptr)0, (short)command);
2216 
2217 			/* move device to higher address */
2218 			command = ADBLISTEN(device, 3);
2219 			send_string[0] = 2;
2220 			send_string[1] = (u_char)(saveptr | 0x60);
2221 			send_string[2] = 0xfe;
2222 			(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2223 			    (Ptr)0, (short)command);
2224 			delay(1000);
2225 
2226 			/* send TALK R3 - anthing at new address? */
2227 			command = ADBTALK(saveptr, 3);
2228 			send_string[0] = 0;
2229 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2230 			    (Ptr)0, (short)command);
2231 			delay(1000);
2232 
2233 			if (result != 0 || send_string[0] == 0) {
2234 				/*
2235 				 * maybe there's a communication breakdown;
2236 				 * just in case, move it back from whence it
2237 				 * came, and we'll try again later
2238 				 */
2239 				command = ADBLISTEN(saveptr, 3);
2240 				send_string[0] = 2;
2241 				send_string[1] = (u_char)(device | 0x60);
2242 				send_string[2] = 0x00;
2243 				(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2244 				    (Ptr)0, (short)command);
2245 #ifdef ADB_DEBUG
2246 				if (adb_debug & 0x80)
2247 					printf_intr("failed, continuing\n");
2248 #endif
2249 				delay(1000);
2250 				continue;
2251 			}
2252 
2253 			/* send TALK R3 - anything at old address? */
2254 			command = ADBTALK(device, 3);
2255 			send_string[0] = 0;
2256 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2257 			    (Ptr)0, (short)command);
2258 			if (result == 0 && send_string[0] != 0) {
2259 				/* new device found */
2260 				/* update data for previously moved device */
2261 				ADBDevTable[i].currentAddr = saveptr;
2262 #ifdef ADB_DEBUG
2263 				if (adb_debug & 0x80)
2264 					printf_intr("old device at index %i\n",i);
2265 #endif
2266 				/* add new device in table */
2267 #ifdef ADB_DEBUG
2268 				if (adb_debug & 0x80)
2269 					printf_intr("new device found\n");
2270 #endif
2271 				if (saveptr > ADBNumDevices) {
2272 					++ADBNumDevices;
2273 					KASSERT(ADBNumDevices < 16);
2274 				}
2275 				ADBDevTable[ADBNumDevices].devType =
2276 					(int)(send_string[2]);
2277 				ADBDevTable[ADBNumDevices].origAddr = device;
2278 				ADBDevTable[ADBNumDevices].currentAddr = device;
2279 				/* These will be set correctly in adbsys.c */
2280 				/* Until then, unsol. data will be ignored. */
2281 				ADBDevTable[ADBNumDevices].DataAreaAddr =
2282 				    (long)0;
2283 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
2284 				    (void *)0;
2285 				/* find next unused address */
2286 				for (x = saveptr; x > 0; x--) {
2287 					if (-1 == get_adb_info(&data, x)) {
2288 						saveptr = x;
2289 						break;
2290 					}
2291 				}
2292 				if (x == 0)
2293 					saveptr = 0;
2294 #ifdef ADB_DEBUG
2295 				if (adb_debug & 0x80)
2296 					printf_intr("new free is 0x%02x\n",
2297 					    saveptr);
2298 #endif
2299 				nonewtimes = 0;
2300 				/* tell pm driver device is here */
2301 				pm_check_adb_devices(device);
2302 			} else {
2303 #ifdef ADB_DEBUG
2304 				if (adb_debug & 0x80)
2305 					printf_intr("moving back...\n");
2306 #endif
2307 				/* move old device back */
2308 				command = ADBLISTEN(saveptr, 3);
2309 				send_string[0] = 2;
2310 				send_string[1] = (u_char)(device | 0x60);
2311 				send_string[2] = 0xfe;
2312 				(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2313 				    (Ptr)0, (short)command);
2314 				delay(1000);
2315 			}
2316 		}
2317 	}
2318 
2319 #ifdef ADB_DEBUG
2320 	if (adb_debug) {
2321 		for (i = 1; i <= ADBNumDevices; i++) {
2322 			x = get_ind_adb_info(&data, i);
2323 			if (x != -1)
2324 				printf_intr("index 0x%x, addr 0x%x, type 0x%hx\n",
2325 				    i, x, data.devType);
2326 		}
2327 	}
2328 #endif
2329 
2330 #ifndef MRG_ADB
2331 	/* enable the programmer's switch, if we have one */
2332 	adb_prog_switch_enable();
2333 #endif
2334 
2335 #ifdef ADB_DEBUG
2336 	if (adb_debug) {
2337 		if (0 == ADBNumDevices)	/* tell user if no devices found */
2338 			printf_intr("adb: no devices found\n");
2339 	}
2340 #endif
2341 
2342 	adbStarting = 0;	/* not starting anymore */
2343 #ifdef ADB_DEBUG
2344 	if (adb_debug)
2345 		printf_intr("adb: ADBReInit complete\n");
2346 #endif
2347 
2348 	if (adbHardware == ADB_HW_CUDA)
2349 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
2350 		    (void *)adb_cuda_tickle, NULL);
2351 
2352 	/* ints must be on for PB & IOP (at least, for now) */
2353 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2354 		splx(s);
2355 
2356 	return;
2357 }
2358 
2359 
2360 /*
2361  * adb_comp_exec
2362  * This is a general routine that calls the completion routine if there is one.
2363  * NOTE: This routine is now only used by pm_direct.c
2364  *       All the code in this file (adb_direct.c) uses
2365  *       the adb_pass_up routine now.
2366  */
2367 void
2368 adb_comp_exec(void)
2369 {
2370 	if ((long)0 != adbCompRout) /* don't call if empty return location */
2371 #ifdef __NetBSD__
2372 		__asm volatile(
2373 		"	movml #0xffff,%%sp@- \n" /* save all registers */
2374 		"	movl %0,%%a2 \n"	/* adbCompData */
2375 		"	movl %1,%%a1 \n"	/* adbCompRout */
2376 		"	movl %2,%%a0 \n"	/* adbBuffer */
2377 		"	movl %3,%%d0 \n"	/* adbWaitingCmd */
2378 		"	jbsr %%a1@ \n"		/* go call the routine */
2379 		"	movml %%sp@+,#0xffff"	/* restore all registers */
2380 		    :
2381 		    : "g"(adbCompData), "g"(adbCompRout),
2382 			"g"(adbBuffer), "g"(adbWaitingCmd)
2383 		    : "d0", "a0", "a1", "a2");
2384 #else /* for Mac OS-based testing */
2385 		asm {
2386 			movem.l a0/a1/a2/d0, -(a7)
2387 			move.l adbCompData, a2
2388 			move.l adbCompRout, a1
2389 			move.l adbBuffer, a0
2390 			move.w adbWaitingCmd, d0
2391 			jsr(a1)
2392 			movem.l(a7) +, d0/a2/a1/a0
2393 		}
2394 #endif
2395 }
2396 
2397 
2398 /*
2399  * adb_cmd_result
2400  *
2401  * This routine lets the caller know whether the specified adb command string
2402  * should expect a returned result, such as a TALK command.
2403  *
2404  * returns: 0 if a result should be expected
2405  *          1 if a result should NOT be expected
2406  */
2407 int
2408 adb_cmd_result(u_char *in)
2409 {
2410 	switch (adbHardware) {
2411 	case ADB_HW_IOP:
2412 	case ADB_HW_II:
2413 		/* was it an ADB talk command? */
2414 		if ((in[1] & 0x0c) == 0x0c)
2415 			return 0;
2416 		return 1;
2417 
2418 	case ADB_HW_IISI:
2419 	case ADB_HW_CUDA:
2420 		/* was it an ADB talk command? */
2421 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
2422 			return 0;
2423 		/* was it an RTC/PRAM read date/time? */
2424 		if ((in[1] == 0x01) && (in[2] == 0x03))
2425 			return 0;
2426 		return 1;
2427 
2428 	case ADB_HW_PB:
2429 		return 1;
2430 
2431 	case ADB_HW_UNKNOWN:
2432 	default:
2433 		return 1;
2434 	}
2435 }
2436 
2437 
2438 /*
2439  * adb_cmd_extra
2440  *
2441  * This routine lets the caller know whether the specified adb command string
2442  * may have extra data appended to the end of it, such as a LISTEN command.
2443  *
2444  * returns: 0 if extra data is allowed
2445  *          1 if extra data is NOT allowed
2446  */
2447 int
2448 adb_cmd_extra(u_char *in)
2449 {
2450 	switch (adbHardware) {
2451 	case ADB_HW_II:
2452 	case ADB_HW_IOP:
2453 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
2454 			return 0;
2455 		return 1;
2456 
2457 	case ADB_HW_IISI:
2458 	case ADB_HW_CUDA:
2459 		/*
2460 		 * TO DO: support needs to be added to recognize RTC and PRAM
2461 		 * commands
2462 		 */
2463 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
2464 			return 0;
2465 		/* add others later */
2466 		return 1;
2467 
2468 	case ADB_HW_PB:
2469 		return 1;
2470 
2471 	case ADB_HW_UNKNOWN:
2472 	default:
2473 		return 1;
2474 	}
2475 }
2476 
2477 
2478 void
2479 adb_setup_hw_type(void)
2480 {
2481 	long response;
2482 
2483 	response = mac68k_machine.machineid;
2484 
2485 	/*
2486 	 * Determine what type of ADB hardware we are running on.
2487 	 */
2488 	switch (response) {
2489 	case MACH_MACC610:		/* Centris 610 */
2490 	case MACH_MACC650:		/* Centris 650 */
2491 	case MACH_MACII:		/* II */
2492 	case MACH_MACIICI:		/* IIci */
2493 	case MACH_MACIICX:		/* IIcx */
2494 	case MACH_MACIIX:		/* IIx */
2495 	case MACH_MACQ610:		/* Quadra 610 */
2496 	case MACH_MACQ650:		/* Quadra 650 */
2497 	case MACH_MACQ700:		/* Quadra 700 */
2498 	case MACH_MACQ800:		/* Quadra 800 */
2499 	case MACH_MACSE30:		/* SE/30 */
2500 		adbHardware = ADB_HW_II;
2501 #ifdef ADB_DEBUG
2502 		if (adb_debug)
2503 			printf_intr("adb: using II series hardware support\n");
2504 #endif
2505 		break;
2506 
2507 	case MACH_MACCLASSICII:		/* Classic II */
2508 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
2509 	case MACH_MACLCIII:		/* LC III, Performa 450 */
2510 	case MACH_MACIISI:		/* IIsi */
2511 	case MACH_MACIIVI:		/* IIvi */
2512 	case MACH_MACIIVX:		/* IIvx */
2513 	case MACH_MACP460:		/* Performa 460/465/467 */
2514 	case MACH_MACP600:		/* Performa 600 */
2515 		adbHardware = ADB_HW_IISI;
2516 #ifdef ADB_DEBUG
2517 		if (adb_debug)
2518 			printf_intr("adb: using IIsi series hardware support\n");
2519 #endif
2520 		break;
2521 
2522 	case MACH_MACPB140:		/* PowerBook 140 */
2523 	case MACH_MACPB145:		/* PowerBook 145 */
2524 	case MACH_MACPB160:		/* PowerBook 160 */
2525 	case MACH_MACPB165:		/* PowerBook 165 */
2526 	case MACH_MACPB165C:		/* PowerBook 165c */
2527 	case MACH_MACPB170:		/* PowerBook 170 */
2528 	case MACH_MACPB180:		/* PowerBook 180 */
2529 	case MACH_MACPB180C:		/* PowerBook 180c */
2530 		adbHardware = ADB_HW_PB;
2531 		pm_setup_adb();
2532 #ifdef ADB_DEBUG
2533 		if (adb_debug)
2534 			printf_intr("adb: using PowerBook 100-series hardware support\n");
2535 #endif
2536 		break;
2537 
2538 	case MACH_MACPB150:		/* PowerBook 150 */
2539 	case MACH_MACPB210:		/* PowerBook Duo 210 */
2540 	case MACH_MACPB230:		/* PowerBook Duo 230 */
2541 	case MACH_MACPB250:		/* PowerBook Duo 250 */
2542 	case MACH_MACPB270:		/* PowerBook Duo 270 */
2543 	case MACH_MACPB280:		/* PowerBook Duo 280 */
2544 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
2545 	case MACH_MACPB500:		/* PowerBook 500 series */
2546 	case MACH_MACPB190:		/* PowerBook 190 */
2547 	case MACH_MACPB190CS:		/* PowerBook 190cs */
2548 		adbHardware = ADB_HW_PB;
2549 		pm_setup_adb();
2550 #ifdef ADB_DEBUG
2551 		if (adb_debug)
2552 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2553 #endif
2554 		break;
2555 
2556 	case MACH_MACC660AV:		/* Centris 660AV */
2557 	case MACH_MACCCLASSIC:		/* Color Classic */
2558 	case MACH_MACCCLASSICII:	/* Color Classic II */
2559 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
2560 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
2561 	case MACH_MACLC520:		/* LC 520 */
2562 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2563 	case MACH_MACP550:		/* LC 550, Performa 550 */
2564 	case MACH_MACTV:		/* Macintosh TV */
2565 	case MACH_MACP580:		/* Performa 580/588 */
2566 	case MACH_MACQ605:		/* Quadra 605 */
2567 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
2568 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2569 	case MACH_MACQ840AV:		/* Quadra 840AV */
2570 		adbHardware = ADB_HW_CUDA;
2571 #ifdef ADB_DEBUG
2572 		if (adb_debug)
2573 			printf_intr("adb: using Cuda series hardware support\n");
2574 #endif
2575 		break;
2576 
2577 	case MACH_MACQ900:		/* Quadra 900 */
2578 	case MACH_MACQ950:		/* Quadra 950 */
2579 	case MACH_MACIIFX:		/* Mac IIfx   */
2580 		adbHardware = ADB_HW_IOP;
2581 		iop_register_listener(ISM_IOP, IOP_CHAN_ADB, adb_iop_recv, NULL);
2582 #ifdef ADB_DEBUG
2583 		if (adb_debug)
2584 			printf_intr("adb: using IOP-based ADB\n");
2585 #endif
2586 		break;
2587 
2588 	default:
2589 		adbHardware = ADB_HW_UNKNOWN;
2590 #ifdef ADB_DEBUG
2591 		if (adb_debug) {
2592 			printf_intr("adb: hardware type unknown for this machine\n");
2593 			printf_intr("adb: ADB support is disabled\n");
2594 		}
2595 #endif
2596 		break;
2597 	}
2598 
2599 	/*
2600 	 * Determine whether this machine has ADB based soft power.
2601 	 */
2602 	switch (response) {
2603 	case MACH_MACCCLASSIC:		/* Color Classic */
2604 	case MACH_MACCCLASSICII:	/* Color Classic II */
2605 	case MACH_MACIISI:		/* IIsi */
2606 	case MACH_MACIIVI:		/* IIvi */
2607 	case MACH_MACIIVX:		/* IIvx */
2608 	case MACH_MACLC520:		/* LC 520 */
2609 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2610 	case MACH_MACP550:		/* LC 550, Performa 550 */
2611 	case MACH_MACTV:		/* Macintosh TV */
2612 	case MACH_MACP580:		/* Performa 580/588 */
2613 	case MACH_MACP600:		/* Performa 600 */
2614 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2615 	case MACH_MACQ840AV:		/* Quadra 840AV */
2616 		adbSoftPower = 1;
2617 		break;
2618 	}
2619 }
2620 
2621 int
2622 count_adbs(void)
2623 {
2624 	int i;
2625 	int found;
2626 
2627 	found = 0;
2628 
2629 	for (i = 1; i < 16; i++)
2630 		if (0 != ADBDevTable[i].currentAddr)
2631 			found++;
2632 
2633 	return found;
2634 }
2635 
2636 int
2637 get_ind_adb_info(ADBDataBlock *info, int index)
2638 {
2639 	if ((index < 1) || (index > 15))	/* check range 1-15 */
2640 		return (-1);
2641 
2642 #ifdef ADB_DEBUG
2643 	if (adb_debug & 0x80)
2644 		printf_intr("index 0x%x devType is: 0x%x\n", index,
2645 		    ADBDevTable[index].devType);
2646 #endif
2647 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
2648 		return (-1);
2649 
2650 	info->devType = (unsigned char)(ADBDevTable[index].devType);
2651 	info->origADBAddr = (unsigned char)(ADBDevTable[index].origAddr);
2652 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
2653 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
2654 
2655 	return (ADBDevTable[index].currentAddr);
2656 }
2657 
2658 int
2659 get_adb_info(ADBDataBlock *info, int adbAddr)
2660 {
2661 	int i;
2662 
2663 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2664 		return (-1);
2665 
2666 	for (i = 1; i < 15; i++)
2667 		if (ADBDevTable[i].currentAddr == adbAddr) {
2668 			info->devType = (unsigned char)(ADBDevTable[i].devType);
2669 			info->origADBAddr = (unsigned char)(ADBDevTable[i].origAddr);
2670 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2671 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2672 			return 0;	/* found */
2673 		}
2674 
2675 	return (-1);		/* not found */
2676 }
2677 
2678 int
2679 set_adb_info(ADBSetInfoBlock *info, int adbAddr)
2680 {
2681 	int i;
2682 
2683 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2684 		return (-1);
2685 
2686 	for (i = 1; i < 15; i++)
2687 		if (ADBDevTable[i].currentAddr == adbAddr) {
2688 			ADBDevTable[i].ServiceRtPtr =
2689 			    (void *)(info->siServiceRtPtr);
2690 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2691 			return 0;	/* found */
2692 		}
2693 
2694 	return (-1);		/* not found */
2695 
2696 }
2697 
2698 #ifndef MRG_ADB
2699 long
2700 mrg_adbintr(void)
2701 {
2702 	adb_intr(NULL);
2703 	return 1;	/* mimic mrg_adbintr in macrom.h just in case */
2704 }
2705 
2706 long
2707 mrg_pmintr(void)
2708 {
2709 	pm_intr(NULL);
2710 	return 1;	/* mimic mrg_pmintr in macrom.h just in case */
2711 }
2712 
2713 /* caller should really use machine-independent version: getPramTime */
2714 /* this version does pseudo-adb access only */
2715 int
2716 adb_read_date_time(unsigned long *curtime)
2717 {
2718 	u_char output[ADB_MAX_MSG_LENGTH];
2719 	int result;
2720 	volatile int flag = 0;
2721 
2722 	switch (adbHardware) {
2723 	case ADB_HW_II:
2724 		return -1;
2725 
2726 	case ADB_HW_IOP:
2727 		return -1;
2728 
2729 	case ADB_HW_IISI:
2730 		output[0] = 0x02;	/* 2 byte message */
2731 		output[1] = 0x01;	/* to pram/rtc device */
2732 		output[2] = 0x03;	/* read date/time */
2733 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
2734 		    (void *)adb_op_comprout, __UNVOLATILE(&flag), (int)0);
2735 		if (result != 0)	/* exit if not sent */
2736 			return -1;
2737 
2738 		while (0 == flag)	/* wait for result */
2739 			;
2740 
2741 		*curtime = (long)(*(long *)(output + 1));
2742 		return 0;
2743 
2744 	case ADB_HW_PB:
2745 		return -1;
2746 
2747 	case ADB_HW_CUDA:
2748 		output[0] = 0x02;	/* 2 byte message */
2749 		output[1] = 0x01;	/* to pram/rtc device */
2750 		output[2] = 0x03;	/* read date/time */
2751 		result = send_adb_cuda((u_char *)output, (u_char *)output,
2752 		    (void *)adb_op_comprout, __UNVOLATILE(&flag), (int)0);
2753 		if (result != 0)	/* exit if not sent */
2754 			return -1;
2755 
2756 		while (0 == flag)	/* wait for result */
2757 			;
2758 
2759 		*curtime = (long)(*(long *)(output + 1));
2760 		return 0;
2761 
2762 	case ADB_HW_UNKNOWN:
2763 	default:
2764 		return -1;
2765 	}
2766 }
2767 
2768 /* caller should really use machine-independent version: setPramTime */
2769 /* this version does pseudo-adb access only */
2770 int
2771 adb_set_date_time(unsigned long curtime)
2772 {
2773 	u_char output[ADB_MAX_MSG_LENGTH];
2774 	int result;
2775 	volatile int flag = 0;
2776 
2777 	switch (adbHardware) {
2778 	case ADB_HW_II:
2779 		return -1;
2780 
2781 	case ADB_HW_IOP:
2782 		return -1;
2783 
2784 	case ADB_HW_IISI:
2785 		output[0] = 0x06;	/* 6 byte message */
2786 		output[1] = 0x01;	/* to pram/rtc device */
2787 		output[2] = 0x09;	/* set date/time */
2788 		output[3] = (u_char)(curtime >> 24);
2789 		output[4] = (u_char)(curtime >> 16);
2790 		output[5] = (u_char)(curtime >> 8);
2791 		output[6] = (u_char)(curtime);
2792 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2793 		    (void *)adb_op_comprout, __UNVOLATILE(&flag), (int)0);
2794 		if (result != 0)	/* exit if not sent */
2795 			return -1;
2796 
2797 		while (0 == flag)	/* wait for send to finish */
2798 			;
2799 
2800 		return 0;
2801 
2802 	case ADB_HW_PB:
2803 		return -1;
2804 
2805 	case ADB_HW_CUDA:
2806 		output[0] = 0x06;	/* 6 byte message */
2807 		output[1] = 0x01;	/* to pram/rtc device */
2808 		output[2] = 0x09;	/* set date/time */
2809 		output[3] = (u_char)(curtime >> 24);
2810 		output[4] = (u_char)(curtime >> 16);
2811 		output[5] = (u_char)(curtime >> 8);
2812 		output[6] = (u_char)(curtime);
2813 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2814 		    (void *)adb_op_comprout, __UNVOLATILE(&flag), (int)0);
2815 		if (result != 0)	/* exit if not sent */
2816 			return -1;
2817 
2818 		while (0 == flag)	/* wait for send to finish */
2819 			;
2820 
2821 		return 0;
2822 
2823 	case ADB_HW_UNKNOWN:
2824 	default:
2825 		return -1;
2826 	}
2827 }
2828 
2829 
2830 int
2831 adb_poweroff(void)
2832 {
2833 	u_char output[ADB_MAX_MSG_LENGTH];
2834 	int result;
2835 
2836 	if (!adbSoftPower)
2837 		return -1;
2838 
2839 	adb_polling = 1;
2840 
2841 	switch (adbHardware) {
2842 	case ADB_HW_IISI:
2843 		output[0] = 0x02;	/* 2 byte message */
2844 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2845 		output[2] = 0x0a;	/* set date/time */
2846 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2847 		    (void *)0, (void *)0, (int)0);
2848 		if (result != 0)	/* exit if not sent */
2849 			return -1;
2850 
2851 		for (;;);		/* wait for power off */
2852 
2853 		return 0;
2854 
2855 	case ADB_HW_PB:
2856 		return -1;
2857 
2858 	case ADB_HW_CUDA:
2859 		output[0] = 0x02;	/* 2 byte message */
2860 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2861 		output[2] = 0x0a;	/* set date/time */
2862 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2863 		    (void *)0, (void *)0, (int)0);
2864 		if (result != 0)	/* exit if not sent */
2865 			return -1;
2866 
2867 		for (;;);		/* wait for power off */
2868 
2869 		return 0;
2870 
2871 	case ADB_HW_II:			/* II models don't do ADB soft power */
2872 	case ADB_HW_IOP:		/* IOP models don't do ADB soft power */
2873 	case ADB_HW_UNKNOWN:
2874 	default:
2875 		return -1;
2876 	}
2877 }
2878 
2879 int
2880 adb_prog_switch_enable(void)
2881 {
2882 	u_char output[ADB_MAX_MSG_LENGTH];
2883 	int result;
2884 	volatile int flag = 0;
2885 
2886 	switch (adbHardware) {
2887 	case ADB_HW_IISI:
2888 		output[0] = 0x03;	/* 3 byte message */
2889 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2890 		output[2] = 0x1c;	/* prog. switch control */
2891 		output[3] = 0x01;	/* enable */
2892 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2893 		    (void *)adb_op_comprout, __UNVOLATILE(&flag), (int)0);
2894 		if (result != 0)	/* exit if not sent */
2895 			return -1;
2896 
2897 		while (0 == flag)	/* wait for send to finish */
2898 			;
2899 
2900 		return 0;
2901 
2902 	case ADB_HW_PB:
2903 		return -1;
2904 
2905 	case ADB_HW_II:		/* II models don't do prog. switch */
2906 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2907 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
2908 	case ADB_HW_UNKNOWN:
2909 	default:
2910 		return -1;
2911 	}
2912 }
2913 
2914 int
2915 adb_prog_switch_disable(void)
2916 {
2917 	u_char output[ADB_MAX_MSG_LENGTH];
2918 	int result;
2919 	volatile int flag = 0;
2920 
2921 	switch (adbHardware) {
2922 	case ADB_HW_IISI:
2923 		output[0] = 0x03;	/* 3 byte message */
2924 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2925 		output[2] = 0x1c;	/* prog. switch control */
2926 		output[3] = 0x01;	/* disable */
2927 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2928 			(void *)adb_op_comprout, __UNVOLATILE(&flag), (int)0);
2929 		if (result != 0)	/* exit if not sent */
2930 			return -1;
2931 
2932 		while (0 == flag)	/* wait for send to finish */
2933 			;
2934 
2935 		return 0;
2936 
2937 	case ADB_HW_PB:
2938 		return -1;
2939 
2940 	case ADB_HW_II:		/* II models don't do prog. switch */
2941 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2942 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
2943 	case ADB_HW_UNKNOWN:
2944 	default:
2945 		return -1;
2946 	}
2947 }
2948 
2949 int
2950 CountADBs(void)
2951 {
2952 	return (count_adbs());
2953 }
2954 
2955 void
2956 ADBReInit(void)
2957 {
2958 	adb_reinit();
2959 }
2960 
2961 int
2962 GetIndADB(ADBDataBlock *info, int index)
2963 {
2964 	return (get_ind_adb_info(info, index));
2965 }
2966 
2967 int
2968 GetADBInfo(ADBDataBlock *info, int adbAddr)
2969 {
2970 	return (get_adb_info(info, adbAddr));
2971 }
2972 
2973 int
2974 SetADBInfo(ADBSetInfoBlock *info, int adbAddr)
2975 {
2976 	return (set_adb_info(info, adbAddr));
2977 }
2978 
2979 int
2980 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2981 {
2982 	return (adb_op(buffer, compRout, data, commandNum));
2983 }
2984 
2985 #endif
2986