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