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