xref: /netbsd-src/sys/arch/mac68k/dev/adb_direct.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: adb_direct.c,v 1.49 2003/07/15 02:43:15 lukem 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.49 2003/07/15 02:43:15 lukem Exp $");
66 
67 #include "opt_adb.h"
68 
69 #include <sys/param.h>
70 #include <sys/cdefs.h>
71 #include <sys/pool.h>
72 #include <sys/queue.h>
73 #include <sys/systm.h>
74 #include <sys/callout.h>
75 
76 #include <machine/viareg.h>
77 #include <machine/param.h>
78 #include <machine/cpu.h>
79 #include <machine/adbsys.h>			/* required for adbvar.h */
80 #include <machine/iopreg.h>			/* required for IOP support */
81 
82 #include <mac68k/mac68k/macrom.h>
83 #include <mac68k/dev/adbvar.h>
84 #define printf_intr printf
85 #else /* !__NetBSD__, i.e. Mac OS */
86 #include "via.h"				/* for macos based testing */
87 /* #define ADB_DEBUG */				/* more verbose for testing */
88 
89 /* Types of ADB hardware that we support */
90 #define ADB_HW_UNKNOWN		0x0	/* don't know */
91 #define ADB_HW_II		0x1	/* Mac II series */
92 #define ADB_HW_IISI		0x2	/* Mac IIsi series */
93 #define ADB_HW_PB		0x3	/* PowerBook series */
94 #define ADB_HW_CUDA		0x4	/* Machines with a Cuda chip */
95 #endif /* __NetBSD__ */
96 
97 /* some misc. leftovers */
98 #define vPB		0x0000
99 #define vPB3		0x08
100 #define vPB4		0x10
101 #define vPB5		0x20
102 #define vSR_INT		0x04
103 #define vSR_OUT		0x10
104 
105 /* the type of ADB action that we are currently preforming */
106 #define ADB_ACTION_NOTREADY	0x1	/* has not been initialized yet */
107 #define ADB_ACTION_IDLE		0x2	/* the bus is currently idle */
108 #define ADB_ACTION_OUT		0x3	/* sending out a command */
109 #define ADB_ACTION_IN		0x4	/* receiving data */
110 #define ADB_ACTION_POLLING	0x5	/* polling - II only */
111 #define ADB_ACTION_RUNNING	0x6	/* running - IOP only */
112 
113 /*
114  * These describe the state of the ADB bus itself, although they
115  * don't necessarily correspond directly to ADB states.
116  * Note: these are not really used in the IIsi code.
117  */
118 #define ADB_BUS_UNKNOWN		0x1	/* we don't know yet - all models */
119 #define ADB_BUS_IDLE		0x2	/* bus is idle - all models */
120 #define ADB_BUS_CMD		0x3	/* starting a command - II models */
121 #define ADB_BUS_ODD		0x4	/* the "odd" state - II models */
122 #define ADB_BUS_EVEN		0x5	/* the "even" state - II models */
123 #define ADB_BUS_ACTIVE		0x6	/* active state - IIsi models */
124 #define ADB_BUS_ACK		0x7	/* currently ACKing - IIsi models */
125 
126 /*
127  * Shortcuts for setting or testing the VIA bit states.
128  * Not all shortcuts are used for every type of ADB hardware.
129  */
130 #define ADB_SET_STATE_IDLE_II()		via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
131 #define ADB_SET_STATE_IDLE_IISI()	via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
132 #define ADB_SET_STATE_IDLE_CUDA()	via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
133 #define ADB_SET_STATE_CMD()		via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
134 #define ADB_SET_STATE_EVEN()		via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
135 						vBufB) | vPB4) & ~vPB5)
136 #define ADB_SET_STATE_ODD()		via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
137 						vBufB) | vPB5) & ~vPB4)
138 #define ADB_SET_STATE_ACTIVE() 		via_reg(VIA1, vBufB) |= vPB5
139 #define ADB_SET_STATE_INACTIVE()	via_reg(VIA1, vBufB) &= ~vPB5
140 #define ADB_SET_STATE_TIP()		via_reg(VIA1, vBufB) &= ~vPB5
141 #define ADB_CLR_STATE_TIP() 		via_reg(VIA1, vBufB) |= vPB5
142 #define ADB_SET_STATE_ACKON()		via_reg(VIA1, vBufB) |= vPB4
143 #define ADB_SET_STATE_ACKOFF()		via_reg(VIA1, vBufB) &= ~vPB4
144 #define ADB_TOGGLE_STATE_ACK_CUDA()	via_reg(VIA1, vBufB) ^= vPB4
145 #define ADB_SET_STATE_ACKON_CUDA()	via_reg(VIA1, vBufB) &= ~vPB4
146 #define ADB_SET_STATE_ACKOFF_CUDA()	via_reg(VIA1, vBufB) |= vPB4
147 #define ADB_SET_SR_INPUT()		via_reg(VIA1, vACR) &= ~vSR_OUT
148 #define ADB_SET_SR_OUTPUT()		via_reg(VIA1, vACR) |= vSR_OUT
149 #define ADB_SR()			via_reg(VIA1, vSR)
150 #define ADB_VIA_INTR_ENABLE()		via_reg(VIA1, vIER) = 0x84
151 #define ADB_VIA_INTR_DISABLE()		via_reg(VIA1, vIER) = 0x04
152 #define ADB_VIA_CLR_INTR()		via_reg(VIA1, vIFR) = 0x04
153 #define ADB_INTR_IS_OFF			(vPB3 == (via_reg(VIA1, vBufB) & vPB3))
154 #define ADB_INTR_IS_ON			(0 == (via_reg(VIA1, vBufB) & vPB3))
155 #define ADB_SR_INTR_IS_OFF		(0 == (via_reg(VIA1, vIFR) & vSR_INT))
156 #define ADB_SR_INTR_IS_ON		(vSR_INT == (via_reg(VIA1, \
157 						vIFR) & vSR_INT))
158 
159 /*
160  * This is the delay that is required (in uS) between certain
161  * ADB transactions. The actual timing delay for for each uS is
162  * calculated at boot time to account for differences in machine speed.
163  */
164 #define ADB_DELAY	150
165 
166 /*
167  * Maximum ADB message length; includes space for data, result, and
168  * device code - plus a little for safety.
169  */
170 #define ADB_MAX_MSG_LENGTH	16
171 #define ADB_MAX_HDR_LENGTH	8
172 
173 #define ADB_QUEUE		32
174 #define ADB_TICKLE_TICKS	4
175 
176 /*
177  * A structure for storing information about each ADB device.
178  */
179 struct ADBDevEntry {
180 	void	(*ServiceRtPtr) __P((void));
181 	void	*DataAreaAddr;
182 	int	devType;
183 	int	origAddr;
184 	int	currentAddr;
185 };
186 
187 /*
188  * Used to hold ADB commands that are waiting to be sent out.
189  */
190 struct adbCmdHoldEntry {
191 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
192 	u_char	*saveBuf;	/* buffer to know where to save result */
193 	u_char	*compRout;	/* completion routine pointer */
194 	u_char	*data;		/* completion routine data pointer */
195 };
196 
197 /*
198  * Eventually used for two separate queues, the queue between
199  * the upper and lower halves, and the outgoing packet queue.
200  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
201  */
202 struct adbCommand {
203 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
204 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
205 	u_char	*saveBuf;	/* where to save result */
206 	u_char	*compRout;	/* completion routine pointer */
207 	u_char	*compData;	/* completion routine data pointer */
208 	u_int	cmd;		/* the original command for this data */
209 	u_int	unsol;		/* 1 if packet was unsolicited */
210 	u_int	ack_only;	/* 1 for no special processing */
211 };
212 
213 /*
214  * Text representations of each hardware class
215  */
216 char	*adbHardwareDescr[MAX_ADB_HW + 1] = {
217 	"unknown",
218 	"II series",
219 	"IIsi series",
220 	"PowerBook",
221 	"Cuda",
222 	"IOP",
223 };
224 
225 /*
226  * A few variables that we need and their initial values.
227  */
228 int	adbHardware = ADB_HW_UNKNOWN;
229 int	adbActionState = ADB_ACTION_NOTREADY;
230 int	adbBusState = ADB_BUS_UNKNOWN;
231 int	adbWaiting = 0;		/* waiting for return data from the device */
232 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
233 int	adbOutQueueHasData = 0;	/* something in the queue waiting to go out */
234 int	adbNextEnd = 0;		/* the next incoming bute is the last (II) */
235 int	adbSoftPower = 0;	/* machine supports soft power */
236 
237 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
238 u_char	*adbBuffer = (long)0;	/* pointer to user data area */
239 void	*adbCompRout = (long)0;	/* pointer to the completion routine */
240 void	*adbCompData = (long)0;	/* pointer to the completion routine data */
241 long	adbFakeInts = 0;	/* keeps track of fake ADB interrupts for
242 				 * timeouts (II) */
243 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
244 int	adbSendTalk = 0;	/* the intr routine is sending the talk, not
245 				 * the user (II) */
246 int	adbPolling = 0;		/* we are polling for service request */
247 int	adbPollCmd = 0;		/* the last poll command we sent */
248 
249 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
250 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
251 struct	adbCmdHoldEntry adbOutQueue;		/* our 1 entry output queue */
252 
253 int	adbSentChars = 0;	/* how many characters we have sent */
254 int	adbLastDevice = 0;	/* last ADB dev we heard from (II ONLY) */
255 int	adbLastDevIndex = 0;	/* last ADB dev loc in dev table (II ONLY) */
256 int	adbLastCommand = 0;	/* the last ADB command we sent (II) */
257 
258 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
259 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
260 
261 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
262 volatile int	adbInCount = 0;		/* how many packets in in queue */
263 int	adbInHead = 0;			/* head of in queue */
264 int	adbInTail = 0;			/* tail of in queue */
265 struct	adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
266 int	adbOutCount = 0;		/* how many packets in out queue */
267 int	adbOutHead = 0;			/* head of out queue */
268 int	adbOutTail = 0;			/* tail of out queue */
269 
270 int	tickle_count = 0;		/* how many tickles seen for this packet? */
271 int	tickle_serial = 0;		/* the last packet tickled */
272 int	adb_cuda_serial = 0;		/* the current packet */
273 
274 struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
275 
276 extern struct mac68k_machine_S mac68k_machine;
277 
278 void	pm_setup_adb __P((void));
279 void	pm_hw_setup __P((void));
280 void	pm_check_adb_devices __P((int));
281 void	pm_intr __P((void *));
282 int	pm_adb_op __P((u_char *, void *, void *, int));
283 void	pm_init_adb_device __P((void));
284 
285 /*
286  * The following are private routines.
287  */
288 #ifdef ADB_DEBUG
289 void	print_single __P((u_char *));
290 #endif
291 void	adb_intr __P((void *));
292 void	adb_intr_II __P((void *));
293 void	adb_intr_IIsi __P((void *));
294 void	adb_intr_cuda __P((void *));
295 void	adb_soft_intr __P((void));
296 int	send_adb_II __P((u_char *, u_char *, void *, void *, int));
297 int	send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
298 int	send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
299 void	adb_intr_cuda_test __P((void));
300 void	adb_cuda_tickle __P((void));
301 void	adb_pass_up __P((struct adbCommand *));
302 void	adb_op_comprout __P((void));
303 void	adb_reinit __P((void));
304 int	count_adbs __P((void));
305 int	get_ind_adb_info __P((ADBDataBlock *, int));
306 int	get_adb_info __P((ADBDataBlock *, int));
307 int	set_adb_info __P((ADBSetInfoBlock *, int));
308 void	adb_setup_hw_type __P((void));
309 int	adb_op __P((Ptr, Ptr, Ptr, short));
310 void	adb_read_II __P((u_char *));
311 void	adb_hw_setup __P((void));
312 void	adb_hw_setup_IIsi __P((u_char *));
313 void	adb_comp_exec __P((void));
314 int	adb_cmd_result __P((u_char *));
315 int	adb_cmd_extra __P((u_char *));
316 int	adb_guess_next_device __P((void));
317 int	adb_prog_switch_enable __P((void));
318 int	adb_prog_switch_disable __P((void));
319 /* we should create this and it will be the public version */
320 int	send_adb __P((u_char *, void *, void *));
321 void	adb_iop_recv __P((IOP *, struct iop_msg *));
322 int	send_adb_iop __P((int, u_char *, void *, void *));
323 
324 #ifdef ADB_DEBUG
325 /*
326  * print_single
327  * Diagnostic display routine. Displays the hex values of the
328  * specified elements of the u_char. The length of the "string"
329  * is in [0].
330  */
331 void
332 print_single(str)
333 	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 		setsoftadb();
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 
2114 	adb_setup_hw_type();	/* setup hardware type */
2115 
2116 	/* Make sure we are not interrupted while building the table. */
2117 	/* ints must be on for PB & IOP (at least, for now) */
2118 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2119 		s = splhigh();
2120 	else
2121 		s = 0;		/* XXX shut the compiler up*/
2122 
2123 	ADBNumDevices = 0;	/* no devices yet */
2124 
2125 	/* Let intr routines know we are running reinit */
2126 	adbStarting = 1;
2127 
2128 	/*
2129 	 * Initialize the ADB table.  For now, we'll always use the same table
2130 	 * that is defined at the beginning of this file - no mallocs.
2131 	 */
2132 	for (i = 0; i < 16; i++) {
2133 		ADBDevTable[i].devType = 0;
2134 		ADBDevTable[i].origAddr = ADBDevTable[i].currentAddr = 0;
2135 	}
2136 
2137 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
2138 
2139 	delay(1000);
2140 
2141 	/* send an ADB reset first */
2142 	(void)adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
2143 	delay(3000);
2144 
2145 	/*
2146 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
2147 	 * which device addresses are in use and which are free. For each
2148 	 * address that is in use, move the device at that address to a higher
2149 	 * free address. Continue doing this at that address until no device
2150 	 * responds at that address. Then move the last device that was moved
2151 	 * back to the original address. Do this for the remaining addresses
2152 	 * that we determined were in use.
2153 	 *
2154 	 * When finished, do this entire process over again with the updated
2155 	 * list of in use addresses. Do this until no new devices have been
2156 	 * found in 20 passes though the in use address list. (This probably
2157 	 * seems long and complicated, but it's the best way to detect multiple
2158 	 * devices at the same address - sometimes it takes a couple of tries
2159 	 * before the collision is detected.)
2160 	 */
2161 
2162 	/* initial scan through the devices */
2163 	for (i = 1; i < 16; i++) {
2164 		command = ADBTALK(i, 3);
2165 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
2166 		    (Ptr)0, (short)command);
2167 
2168 		if (result == 0 && send_string[0] != 0) {
2169 			/* found a device */
2170 			++ADBNumDevices;
2171 			KASSERT(ADBNumDevices < 16);
2172 			ADBDevTable[ADBNumDevices].devType =
2173 				(int)(send_string[2]);
2174 			ADBDevTable[ADBNumDevices].origAddr = i;
2175 			ADBDevTable[ADBNumDevices].currentAddr = i;
2176 			ADBDevTable[ADBNumDevices].DataAreaAddr =
2177 			    (long)0;
2178 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
2179 			pm_check_adb_devices(i);	/* tell pm driver device
2180 							 * is here */
2181 		}
2182 	}
2183 
2184 	/* find highest unused address */
2185 	for (saveptr = 15; saveptr > 0; saveptr--)
2186 		if (-1 == get_adb_info(&data, saveptr))
2187 			break;
2188 
2189 #ifdef ADB_DEBUG
2190 	if (adb_debug & 0x80) {
2191 		printf_intr("first free is: 0x%02x\n", saveptr);
2192 		printf_intr("devices: %i\n", ADBNumDevices);
2193 	}
2194 #endif
2195 
2196 	nonewtimes = 0;		/* no loops w/o new devices */
2197 	while (saveptr > 0 && nonewtimes++ < 11) {
2198 		for (i = 1;saveptr > 0 && i <= ADBNumDevices; i++) {
2199 			device = ADBDevTable[i].currentAddr;
2200 #ifdef ADB_DEBUG
2201 			if (adb_debug & 0x80)
2202 				printf_intr("moving device 0x%02x to 0x%02x "
2203 				    "(index 0x%02x)  ", device, saveptr, i);
2204 #endif
2205 
2206 			/* send TALK R3 to address */
2207 			command = ADBTALK(device, 3);
2208 			(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2209 			    (Ptr)0, (short)command);
2210 
2211 			/* move device to higher address */
2212 			command = ADBLISTEN(device, 3);
2213 			send_string[0] = 2;
2214 			send_string[1] = (u_char)(saveptr | 0x60);
2215 			send_string[2] = 0xfe;
2216 			(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2217 			    (Ptr)0, (short)command);
2218 			delay(1000);
2219 
2220 			/* send TALK R3 - anthing at new address? */
2221 			command = ADBTALK(saveptr, 3);
2222 			send_string[0] = 0;
2223 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2224 			    (Ptr)0, (short)command);
2225 			delay(1000);
2226 
2227 			if (result != 0 || send_string[0] == 0) {
2228 				/*
2229 				 * maybe there's a communication breakdown;
2230 				 * just in case, move it back from whence it
2231 				 * came, and we'll try again later
2232 				 */
2233 				command = ADBLISTEN(saveptr, 3);
2234 				send_string[0] = 2;
2235 				send_string[1] = (u_char)(device | 0x60);
2236 				send_string[2] = 0x00;
2237 				(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2238 				    (Ptr)0, (short)command);
2239 #ifdef ADB_DEBUG
2240 				if (adb_debug & 0x80)
2241 					printf_intr("failed, continuing\n");
2242 #endif
2243 				delay(1000);
2244 				continue;
2245 			}
2246 
2247 			/* send TALK R3 - anything at old address? */
2248 			command = ADBTALK(device, 3);
2249 			send_string[0] = 0;
2250 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
2251 			    (Ptr)0, (short)command);
2252 			if (result == 0 && send_string[0] != 0) {
2253 				/* new device found */
2254 				/* update data for previously moved device */
2255 				ADBDevTable[i].currentAddr = saveptr;
2256 #ifdef ADB_DEBUG
2257 				if (adb_debug & 0x80)
2258 					printf_intr("old device at index %i\n",i);
2259 #endif
2260 				/* add new device in table */
2261 #ifdef ADB_DEBUG
2262 				if (adb_debug & 0x80)
2263 					printf_intr("new device found\n");
2264 #endif
2265 				if (saveptr > ADBNumDevices) {
2266 					++ADBNumDevices;
2267 					KASSERT(ADBNumDevices < 16);
2268 				}
2269 				ADBDevTable[ADBNumDevices].devType =
2270 					(int)(send_string[2]);
2271 				ADBDevTable[ADBNumDevices].origAddr = device;
2272 				ADBDevTable[ADBNumDevices].currentAddr = device;
2273 				/* These will be set correctly in adbsys.c */
2274 				/* Until then, unsol. data will be ignored. */
2275 				ADBDevTable[ADBNumDevices].DataAreaAddr =
2276 				    (long)0;
2277 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
2278 				    (void *)0;
2279 				/* find next unused address */
2280 				for (x = saveptr; x > 0; x--) {
2281 					if (-1 == get_adb_info(&data, x)) {
2282 						saveptr = x;
2283 						break;
2284 					}
2285 				}
2286 				if (x == 0)
2287 					saveptr = 0;
2288 #ifdef ADB_DEBUG
2289 				if (adb_debug & 0x80)
2290 					printf_intr("new free is 0x%02x\n",
2291 					    saveptr);
2292 #endif
2293 				nonewtimes = 0;
2294 				/* tell pm driver device is here */
2295 				pm_check_adb_devices(device);
2296 			} else {
2297 #ifdef ADB_DEBUG
2298 				if (adb_debug & 0x80)
2299 					printf_intr("moving back...\n");
2300 #endif
2301 				/* move old device back */
2302 				command = ADBLISTEN(saveptr, 3);
2303 				send_string[0] = 2;
2304 				send_string[1] = (u_char)(device | 0x60);
2305 				send_string[2] = 0xfe;
2306 				(void)adb_op_sync((Ptr)send_string, (Ptr)0,
2307 				    (Ptr)0, (short)command);
2308 				delay(1000);
2309 			}
2310 		}
2311 	}
2312 
2313 #ifdef ADB_DEBUG
2314 	if (adb_debug) {
2315 		for (i = 1; i <= ADBNumDevices; i++) {
2316 			x = get_ind_adb_info(&data, i);
2317 			if (x != -1)
2318 				printf_intr("index 0x%x, addr 0x%x, type 0x%hx\n",
2319 				    i, x, data.devType);
2320 		}
2321 	}
2322 #endif
2323 
2324 #ifndef MRG_ADB
2325 	/* enable the programmer's switch, if we have one */
2326 	adb_prog_switch_enable();
2327 #endif
2328 
2329 #ifdef ADB_DEBUG
2330 	if (adb_debug) {
2331 		if (0 == ADBNumDevices)	/* tell user if no devices found */
2332 			printf_intr("adb: no devices found\n");
2333 	}
2334 #endif
2335 
2336 	adbStarting = 0;	/* not starting anymore */
2337 #ifdef ADB_DEBUG
2338 	if (adb_debug)
2339 		printf_intr("adb: ADBReInit complete\n");
2340 #endif
2341 
2342 	if (adbHardware == ADB_HW_CUDA)
2343 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
2344 		    (void *)adb_cuda_tickle, NULL);
2345 
2346 	/* ints must be on for PB & IOP (at least, for now) */
2347 	if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2348 		splx(s);
2349 
2350 	return;
2351 }
2352 
2353 
2354 /*
2355  * adb_comp_exec
2356  * This is a general routine that calls the completion routine if there is one.
2357  * NOTE: This routine is now only used by pm_direct.c
2358  *       All the code in this file (adb_direct.c) uses
2359  *       the adb_pass_up routine now.
2360  */
2361 void
2362 adb_comp_exec(void)
2363 {
2364 	if ((long)0 != adbCompRout) /* don't call if empty return location */
2365 #ifdef __NetBSD__
2366 		__asm __volatile(
2367 		"	movml #0xffff,%%sp@- \n" /* save all registers */
2368 		"	movl %0,%%a2 \n"	/* adbCompData */
2369 		"	movl %1,%%a1 \n"	/* adbCompRout */
2370 		"	movl %2,%%a0 \n"	/* adbBuffer */
2371 		"	movl %3,%%d0 \n"	/* adbWaitingCmd */
2372 		"	jbsr %%a1@ \n"		/* go call the routine */
2373 		"	movml %%sp@+,#0xffff"	/* restore all registers */
2374 		    :
2375 		    : "g"(adbCompData), "g"(adbCompRout),
2376 			"g"(adbBuffer), "g"(adbWaitingCmd)
2377 		    : "d0", "a0", "a1", "a2");
2378 #else /* for Mac OS-based testing */
2379 		asm {
2380 			movem.l a0/a1/a2/d0, -(a7)
2381 			move.l adbCompData, a2
2382 			move.l adbCompRout, a1
2383 			move.l adbBuffer, a0
2384 			move.w adbWaitingCmd, d0
2385 			jsr(a1)
2386 			movem.l(a7) +, d0/a2/a1/a0
2387 		}
2388 #endif
2389 }
2390 
2391 
2392 /*
2393  * adb_cmd_result
2394  *
2395  * This routine lets the caller know whether the specified adb command string
2396  * should expect a returned result, such as a TALK command.
2397  *
2398  * returns: 0 if a result should be expected
2399  *          1 if a result should NOT be expected
2400  */
2401 int
2402 adb_cmd_result(u_char *in)
2403 {
2404 	switch (adbHardware) {
2405 	case ADB_HW_IOP:
2406 	case ADB_HW_II:
2407 		/* was it an ADB talk command? */
2408 		if ((in[1] & 0x0c) == 0x0c)
2409 			return 0;
2410 		return 1;
2411 
2412 	case ADB_HW_IISI:
2413 	case ADB_HW_CUDA:
2414 		/* was it an ADB talk command? */
2415 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
2416 			return 0;
2417 		/* was it an RTC/PRAM read date/time? */
2418 		if ((in[1] == 0x01) && (in[2] == 0x03))
2419 			return 0;
2420 		return 1;
2421 
2422 	case ADB_HW_PB:
2423 		return 1;
2424 
2425 	case ADB_HW_UNKNOWN:
2426 	default:
2427 		return 1;
2428 	}
2429 }
2430 
2431 
2432 /*
2433  * adb_cmd_extra
2434  *
2435  * This routine lets the caller know whether the specified adb command string
2436  * may have extra data appended to the end of it, such as a LISTEN command.
2437  *
2438  * returns: 0 if extra data is allowed
2439  *          1 if extra data is NOT allowed
2440  */
2441 int
2442 adb_cmd_extra(u_char *in)
2443 {
2444 	switch (adbHardware) {
2445 	case ADB_HW_II:
2446 	case ADB_HW_IOP:
2447 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
2448 			return 0;
2449 		return 1;
2450 
2451 	case ADB_HW_IISI:
2452 	case ADB_HW_CUDA:
2453 		/*
2454 		 * TO DO: support needs to be added to recognize RTC and PRAM
2455 		 * commands
2456 		 */
2457 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
2458 			return 0;
2459 		/* add others later */
2460 		return 1;
2461 
2462 	case ADB_HW_PB:
2463 		return 1;
2464 
2465 	case ADB_HW_UNKNOWN:
2466 	default:
2467 		return 1;
2468 	}
2469 }
2470 
2471 
2472 void
2473 adb_setup_hw_type(void)
2474 {
2475 	long response;
2476 
2477 	response = mac68k_machine.machineid;
2478 
2479 	/*
2480 	 * Determine what type of ADB hardware we are running on.
2481 	 */
2482 	switch (response) {
2483 	case MACH_MACC610:		/* Centris 610 */
2484 	case MACH_MACC650:		/* Centris 650 */
2485 	case MACH_MACII:		/* II */
2486 	case MACH_MACIICI:		/* IIci */
2487 	case MACH_MACIICX:		/* IIcx */
2488 	case MACH_MACIIX:		/* IIx */
2489 	case MACH_MACQ610:		/* Quadra 610 */
2490 	case MACH_MACQ650:		/* Quadra 650 */
2491 	case MACH_MACQ700:		/* Quadra 700 */
2492 	case MACH_MACQ800:		/* Quadra 800 */
2493 	case MACH_MACSE30:		/* SE/30 */
2494 		adbHardware = ADB_HW_II;
2495 #ifdef ADB_DEBUG
2496 		if (adb_debug)
2497 			printf_intr("adb: using II series hardware support\n");
2498 #endif
2499 		break;
2500 
2501 	case MACH_MACCLASSICII:		/* Classic II */
2502 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
2503 	case MACH_MACLCIII:		/* LC III, Performa 450 */
2504 	case MACH_MACIISI:		/* IIsi */
2505 	case MACH_MACIIVI:		/* IIvi */
2506 	case MACH_MACIIVX:		/* IIvx */
2507 	case MACH_MACP460:		/* Performa 460/465/467 */
2508 	case MACH_MACP600:		/* Performa 600 */
2509 		adbHardware = ADB_HW_IISI;
2510 #ifdef ADB_DEBUG
2511 		if (adb_debug)
2512 			printf_intr("adb: using IIsi series hardware support\n");
2513 #endif
2514 		break;
2515 
2516 	case MACH_MACPB140:		/* PowerBook 140 */
2517 	case MACH_MACPB145:		/* PowerBook 145 */
2518 	case MACH_MACPB160:		/* PowerBook 160 */
2519 	case MACH_MACPB165:		/* PowerBook 165 */
2520 	case MACH_MACPB165C:		/* PowerBook 165c */
2521 	case MACH_MACPB170:		/* PowerBook 170 */
2522 	case MACH_MACPB180:		/* PowerBook 180 */
2523 	case MACH_MACPB180C:		/* PowerBook 180c */
2524 		adbHardware = ADB_HW_PB;
2525 		pm_setup_adb();
2526 #ifdef ADB_DEBUG
2527 		if (adb_debug)
2528 			printf_intr("adb: using PowerBook 100-series hardware support\n");
2529 #endif
2530 		break;
2531 
2532 	case MACH_MACPB150:		/* PowerBook 150 */
2533 	case MACH_MACPB210:		/* PowerBook Duo 210 */
2534 	case MACH_MACPB230:		/* PowerBook Duo 230 */
2535 	case MACH_MACPB250:		/* PowerBook Duo 250 */
2536 	case MACH_MACPB270:		/* PowerBook Duo 270 */
2537 	case MACH_MACPB280:		/* PowerBook Duo 280 */
2538 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
2539 	case MACH_MACPB500:		/* PowerBook 500 series */
2540 	case MACH_MACPB190:		/* PowerBook 190 */
2541 	case MACH_MACPB190CS:		/* PowerBook 190cs */
2542 		adbHardware = ADB_HW_PB;
2543 		pm_setup_adb();
2544 #ifdef ADB_DEBUG
2545 		if (adb_debug)
2546 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2547 #endif
2548 		break;
2549 
2550 	case MACH_MACC660AV:		/* Centris 660AV */
2551 	case MACH_MACCCLASSIC:		/* Color Classic */
2552 	case MACH_MACCCLASSICII:	/* Color Classic II */
2553 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
2554 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
2555 	case MACH_MACLC520:		/* LC 520 */
2556 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2557 	case MACH_MACP550:		/* LC 550, Performa 550 */
2558 	case MACH_MACTV:		/* Macintosh TV */
2559 	case MACH_MACP580:		/* Performa 580/588 */
2560 	case MACH_MACQ605:		/* Quadra 605 */
2561 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
2562 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2563 	case MACH_MACQ840AV:		/* Quadra 840AV */
2564 		adbHardware = ADB_HW_CUDA;
2565 #ifdef ADB_DEBUG
2566 		if (adb_debug)
2567 			printf_intr("adb: using Cuda series hardware support\n");
2568 #endif
2569 		break;
2570 
2571 	case MACH_MACQ900:		/* Quadra 900 */
2572 	case MACH_MACQ950:		/* Quadra 950 */
2573 	case MACH_MACIIFX:		/* Mac IIfx   */
2574 		adbHardware = ADB_HW_IOP;
2575 		iop_register_listener(ISM_IOP, IOP_CHAN_ADB, adb_iop_recv, NULL);
2576 #ifdef ADB_DEBUG
2577 		if (adb_debug)
2578 			printf_intr("adb: using IOP-based ADB\n");
2579 #endif
2580 		break;
2581 
2582 	default:
2583 		adbHardware = ADB_HW_UNKNOWN;
2584 #ifdef ADB_DEBUG
2585 		if (adb_debug) {
2586 			printf_intr("adb: hardware type unknown for this machine\n");
2587 			printf_intr("adb: ADB support is disabled\n");
2588 		}
2589 #endif
2590 		break;
2591 	}
2592 
2593 	/*
2594 	 * Determine whether this machine has ADB based soft power.
2595 	 */
2596 	switch (response) {
2597 	case MACH_MACCCLASSIC:		/* Color Classic */
2598 	case MACH_MACCCLASSICII:	/* Color Classic II */
2599 	case MACH_MACIISI:		/* IIsi */
2600 	case MACH_MACIIVI:		/* IIvi */
2601 	case MACH_MACIIVX:		/* IIvx */
2602 	case MACH_MACLC520:		/* LC 520 */
2603 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
2604 	case MACH_MACP550:		/* LC 550, Performa 550 */
2605 	case MACH_MACTV:		/* Macintosh TV */
2606 	case MACH_MACP580:		/* Performa 580/588 */
2607 	case MACH_MACP600:		/* Performa 600 */
2608 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
2609 	case MACH_MACQ840AV:		/* Quadra 840AV */
2610 		adbSoftPower = 1;
2611 		break;
2612 	}
2613 }
2614 
2615 int
2616 count_adbs(void)
2617 {
2618 	int i;
2619 	int found;
2620 
2621 	found = 0;
2622 
2623 	for (i = 1; i < 16; i++)
2624 		if (0 != ADBDevTable[i].currentAddr)
2625 			found++;
2626 
2627 	return found;
2628 }
2629 
2630 int
2631 get_ind_adb_info(ADBDataBlock * info, int index)
2632 {
2633 	if ((index < 1) || (index > 15))	/* check range 1-15 */
2634 		return (-1);
2635 
2636 #ifdef ADB_DEBUG
2637 	if (adb_debug & 0x80)
2638 		printf_intr("index 0x%x devType is: 0x%x\n", index,
2639 		    ADBDevTable[index].devType);
2640 #endif
2641 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
2642 		return (-1);
2643 
2644 	info->devType = (unsigned char)(ADBDevTable[index].devType);
2645 	info->origADBAddr = (unsigned char)(ADBDevTable[index].origAddr);
2646 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
2647 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
2648 
2649 	return (ADBDevTable[index].currentAddr);
2650 }
2651 
2652 int
2653 get_adb_info(ADBDataBlock * info, int adbAddr)
2654 {
2655 	int i;
2656 
2657 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2658 		return (-1);
2659 
2660 	for (i = 1; i < 15; i++)
2661 		if (ADBDevTable[i].currentAddr == adbAddr) {
2662 			info->devType = (unsigned char)(ADBDevTable[i].devType);
2663 			info->origADBAddr = (unsigned char)(ADBDevTable[i].origAddr);
2664 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2665 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2666 			return 0;	/* found */
2667 		}
2668 
2669 	return (-1);		/* not found */
2670 }
2671 
2672 int
2673 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
2674 {
2675 	int i;
2676 
2677 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
2678 		return (-1);
2679 
2680 	for (i = 1; i < 15; i++)
2681 		if (ADBDevTable[i].currentAddr == adbAddr) {
2682 			ADBDevTable[i].ServiceRtPtr =
2683 			    (void *)(info->siServiceRtPtr);
2684 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2685 			return 0;	/* found */
2686 		}
2687 
2688 	return (-1);		/* not found */
2689 
2690 }
2691 
2692 #ifndef MRG_ADB
2693 long
2694 mrg_adbintr(void)
2695 {
2696 	adb_intr(NULL);
2697 	return 1;	/* mimic mrg_adbintr in macrom.h just in case */
2698 }
2699 
2700 long
2701 mrg_pmintr(void)
2702 {
2703 	pm_intr(NULL);
2704 	return 1;	/* mimic mrg_pmintr in macrom.h just in case */
2705 }
2706 
2707 /* caller should really use machine-independant version: getPramTime */
2708 /* this version does pseudo-adb access only */
2709 int
2710 adb_read_date_time(unsigned long *time)
2711 {
2712 	u_char output[ADB_MAX_MSG_LENGTH];
2713 	int result;
2714 	volatile int flag = 0;
2715 
2716 	switch (adbHardware) {
2717 	case ADB_HW_II:
2718 		return -1;
2719 
2720 	case ADB_HW_IOP:
2721 		return -1;
2722 
2723 	case ADB_HW_IISI:
2724 		output[0] = 0x02;	/* 2 byte message */
2725 		output[1] = 0x01;	/* to pram/rtc device */
2726 		output[2] = 0x03;	/* read date/time */
2727 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
2728 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
2729 		if (result != 0)	/* exit if not sent */
2730 			return -1;
2731 
2732 		while (0 == flag)	/* wait for result */
2733 			;
2734 
2735 		*time = (long)(*(long *)(output + 1));
2736 		return 0;
2737 
2738 	case ADB_HW_PB:
2739 		return -1;
2740 
2741 	case ADB_HW_CUDA:
2742 		output[0] = 0x02;	/* 2 byte message */
2743 		output[1] = 0x01;	/* to pram/rtc device */
2744 		output[2] = 0x03;	/* read date/time */
2745 		result = send_adb_cuda((u_char *)output, (u_char *)output,
2746 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2747 		if (result != 0)	/* exit if not sent */
2748 			return -1;
2749 
2750 		while (0 == flag)	/* wait for result */
2751 			;
2752 
2753 		*time = (long)(*(long *)(output + 1));
2754 		return 0;
2755 
2756 	case ADB_HW_UNKNOWN:
2757 	default:
2758 		return -1;
2759 	}
2760 }
2761 
2762 /* caller should really use machine-independant version: setPramTime */
2763 /* this version does pseudo-adb access only */
2764 int
2765 adb_set_date_time(unsigned long time)
2766 {
2767 	u_char output[ADB_MAX_MSG_LENGTH];
2768 	int result;
2769 	volatile int flag = 0;
2770 
2771 	switch (adbHardware) {
2772 	case ADB_HW_II:
2773 		return -1;
2774 
2775 	case ADB_HW_IOP:
2776 		return -1;
2777 
2778 	case ADB_HW_IISI:
2779 		output[0] = 0x06;	/* 6 byte message */
2780 		output[1] = 0x01;	/* to pram/rtc device */
2781 		output[2] = 0x09;	/* set date/time */
2782 		output[3] = (u_char)(time >> 24);
2783 		output[4] = (u_char)(time >> 16);
2784 		output[5] = (u_char)(time >> 8);
2785 		output[6] = (u_char)(time);
2786 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2787 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2788 		if (result != 0)	/* exit if not sent */
2789 			return -1;
2790 
2791 		while (0 == flag)	/* wait for send to finish */
2792 			;
2793 
2794 		return 0;
2795 
2796 	case ADB_HW_PB:
2797 		return -1;
2798 
2799 	case ADB_HW_CUDA:
2800 		output[0] = 0x06;	/* 6 byte message */
2801 		output[1] = 0x01;	/* to pram/rtc device */
2802 		output[2] = 0x09;	/* set date/time */
2803 		output[3] = (u_char)(time >> 24);
2804 		output[4] = (u_char)(time >> 16);
2805 		output[5] = (u_char)(time >> 8);
2806 		output[6] = (u_char)(time);
2807 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2808 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2809 		if (result != 0)	/* exit if not sent */
2810 			return -1;
2811 
2812 		while (0 == flag)	/* wait for send to finish */
2813 			;
2814 
2815 		return 0;
2816 
2817 	case ADB_HW_UNKNOWN:
2818 	default:
2819 		return -1;
2820 	}
2821 }
2822 
2823 
2824 int
2825 adb_poweroff(void)
2826 {
2827 	u_char output[ADB_MAX_MSG_LENGTH];
2828 	int result;
2829 
2830 	if (!adbSoftPower)
2831 		return -1;
2832 
2833 	adb_polling = 1;
2834 
2835 	switch (adbHardware) {
2836 	case ADB_HW_IISI:
2837 		output[0] = 0x02;	/* 2 byte message */
2838 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2839 		output[2] = 0x0a;	/* set date/time */
2840 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2841 		    (void *)0, (void *)0, (int)0);
2842 		if (result != 0)	/* exit if not sent */
2843 			return -1;
2844 
2845 		for (;;);		/* wait for power off */
2846 
2847 		return 0;
2848 
2849 	case ADB_HW_PB:
2850 		return -1;
2851 
2852 	case ADB_HW_CUDA:
2853 		output[0] = 0x02;	/* 2 byte message */
2854 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2855 		output[2] = 0x0a;	/* set date/time */
2856 		result = send_adb_cuda((u_char *)output, (u_char *)0,
2857 		    (void *)0, (void *)0, (int)0);
2858 		if (result != 0)	/* exit if not sent */
2859 			return -1;
2860 
2861 		for (;;);		/* wait for power off */
2862 
2863 		return 0;
2864 
2865 	case ADB_HW_II:			/* II models don't do ADB soft power */
2866 	case ADB_HW_IOP:		/* IOP models don't do ADB soft power */
2867 	case ADB_HW_UNKNOWN:
2868 	default:
2869 		return -1;
2870 	}
2871 }
2872 
2873 int
2874 adb_prog_switch_enable(void)
2875 {
2876 	u_char output[ADB_MAX_MSG_LENGTH];
2877 	int result;
2878 	volatile int flag = 0;
2879 
2880 	switch (adbHardware) {
2881 	case ADB_HW_IISI:
2882 		output[0] = 0x03;	/* 3 byte message */
2883 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2884 		output[2] = 0x1c;	/* prog. switch control */
2885 		output[3] = 0x01;	/* enable */
2886 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2887 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2888 		if (result != 0)	/* exit if not sent */
2889 			return -1;
2890 
2891 		while (0 == flag)	/* wait for send to finish */
2892 			;
2893 
2894 		return 0;
2895 
2896 	case ADB_HW_PB:
2897 		return -1;
2898 
2899 	case ADB_HW_II:		/* II models don't do prog. switch */
2900 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2901 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
2902 	case ADB_HW_UNKNOWN:
2903 	default:
2904 		return -1;
2905 	}
2906 }
2907 
2908 int
2909 adb_prog_switch_disable(void)
2910 {
2911 	u_char output[ADB_MAX_MSG_LENGTH];
2912 	int result;
2913 	volatile int flag = 0;
2914 
2915 	switch (adbHardware) {
2916 	case ADB_HW_IISI:
2917 		output[0] = 0x03;	/* 3 byte message */
2918 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2919 		output[2] = 0x1c;	/* prog. switch control */
2920 		output[3] = 0x01;	/* disable */
2921 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2922 			(void *)adb_op_comprout, (void *)&flag, (int)0);
2923 		if (result != 0)	/* exit if not sent */
2924 			return -1;
2925 
2926 		while (0 == flag)	/* wait for send to finish */
2927 			;
2928 
2929 		return 0;
2930 
2931 	case ADB_HW_PB:
2932 		return -1;
2933 
2934 	case ADB_HW_II:		/* II models don't do prog. switch */
2935 	case ADB_HW_IOP:	/* IOP models don't do prog. switch */
2936 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
2937 	case ADB_HW_UNKNOWN:
2938 	default:
2939 		return -1;
2940 	}
2941 }
2942 
2943 int
2944 CountADBs(void)
2945 {
2946 	return (count_adbs());
2947 }
2948 
2949 void
2950 ADBReInit(void)
2951 {
2952 	adb_reinit();
2953 }
2954 
2955 int
2956 GetIndADB(ADBDataBlock * info, int index)
2957 {
2958 	return (get_ind_adb_info(info, index));
2959 }
2960 
2961 int
2962 GetADBInfo(ADBDataBlock * info, int adbAddr)
2963 {
2964 	return (get_adb_info(info, adbAddr));
2965 }
2966 
2967 int
2968 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2969 {
2970 	return (set_adb_info(info, adbAddr));
2971 }
2972 
2973 int
2974 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2975 {
2976 	return (adb_op(buffer, compRout, data, commandNum));
2977 }
2978 
2979 #endif
2980