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