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