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