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