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