Home | History | Annotate | Line # | Download | only in dev
adb_direct.c revision 1.10
      1 /*	$NetBSD: adb_direct.c,v 1.10 1999/06/22 11:29:11 tsubai Exp $	*/
      2 
      3 /* From: adb_direct.c 2.02 4/18/97 jpw */
      4 
      5 /*
      6  * Copyright (C) 1996, 1997 John P. Wittkoski
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *  This product includes software developed by John P. Wittkoski.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * This code is rather messy, but I don't have time right now
     37  * to clean it up as much as I would like.
     38  * But it works, so I'm happy. :-) jpw
     39  */
     40 
     41 /*
     42  * TO DO:
     43  *  - We could reduce the time spent in the adb_intr_* routines
     44  *    by having them save the incoming and outgoing data directly
     45  *    in the adbInbound and adbOutbound queues, as it would reduce
     46  *    the number of times we need to copy the data around. It
     47  *    would also make the code more readable and easier to follow.
     48  *  - (Related to above) Use the header part of adbCommand to
     49  *    reduce the number of copies we have to do of the data.
     50  *  - (Related to above) Actually implement the adbOutbound queue.
     51  *    This is fairly easy once you switch all the intr routines
     52  *    over to using adbCommand structs directly.
     53  *  - There is a bug in the state machine of adb_intr_cuda
     54  *    code that causes hangs, especially on 030 machines, probably
     55  *    because of some timing issues. Because I have been unable to
     56  *    determine the exact cause of this bug, I used the timeout function
     57  *    to check for and recover from this condition. If anyone finds
     58  *    the actual cause of this bug, the calls to timeout and the
     59  *    adb_cuda_tickle routine can be removed.
     60  */
     61 
     62 #include <sys/param.h>
     63 #include <sys/cdefs.h>
     64 #include <sys/systm.h>
     65 #include <sys/device.h>
     66 
     67 #include <machine/param.h>
     68 #include <machine/cpu.h>
     69 #include <machine/adbsys.h>
     70 
     71 #include <macppc/dev/viareg.h>
     72 #include <macppc/dev/adbvar.h>
     73 
     74 #define printf_intr printf
     75 
     76 #ifdef DEBUG
     77 #ifndef ADB_DEBUG
     78 #define ADB_DEBUG
     79 #endif
     80 #endif
     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 /* the type of ADB action that we are currently preforming */
     91 #define ADB_ACTION_NOTREADY	0x1	/* has not been initialized yet */
     92 #define ADB_ACTION_IDLE		0x2	/* the bus is currently idle */
     93 #define ADB_ACTION_OUT		0x3	/* sending out a command */
     94 #define ADB_ACTION_IN		0x4	/* receiving data */
     95 #define ADB_ACTION_POLLING	0x5	/* polling - II only */
     96 
     97 /*
     98  * These describe the state of the ADB bus itself, although they
     99  * don't necessarily correspond directly to ADB states.
    100  * Note: these are not really used in the IIsi code.
    101  */
    102 #define ADB_BUS_UNKNOWN		0x1	/* we don't know yet - all models */
    103 #define ADB_BUS_IDLE		0x2	/* bus is idle - all models */
    104 #define ADB_BUS_CMD		0x3	/* starting a command - II models */
    105 #define ADB_BUS_ODD		0x4	/* the "odd" state - II models */
    106 #define ADB_BUS_EVEN		0x5	/* the "even" state - II models */
    107 #define ADB_BUS_ACTIVE		0x6	/* active state - IIsi models */
    108 #define ADB_BUS_ACK		0x7	/* currently ACKing - IIsi models */
    109 
    110 /*
    111  * Shortcuts for setting or testing the VIA bit states.
    112  * Not all shortcuts are used for every type of ADB hardware.
    113  */
    114 #define ADB_SET_STATE_IDLE_II()     via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
    115 #define ADB_SET_STATE_IDLE_IISI()   via_reg_and(VIA1, vBufB, ~(vPB4 | vPB5))
    116 #define ADB_SET_STATE_IDLE_CUDA()   via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
    117 #define ADB_SET_STATE_CMD()         via_reg_and(VIA1, vBufB, ~(vPB4 | vPB5))
    118 #define ADB_SET_STATE_EVEN()        write_via_reg(VIA1, vBufB, \
    119                               (read_via_reg(VIA1, vBufB) | vPB4) & ~vPB5)
    120 #define ADB_SET_STATE_ODD()         write_via_reg(VIA1, vBufB, \
    121                               (read_via_reg(VIA1, vBufB) | vPB5) & ~vPB4 )
    122 #define ADB_SET_STATE_ACTIVE() 	    via_reg_or(VIA1, vBufB, vPB5)
    123 #define ADB_SET_STATE_INACTIVE()    via_reg_and(VIA1, vBufB, ~vPB5)
    124 #define ADB_SET_STATE_TIP()	    via_reg_and(VIA1, vBufB, ~vPB5)
    125 #define ADB_CLR_STATE_TIP() 	    via_reg_or(VIA1, vBufB, vPB5)
    126 #define ADB_SET_STATE_ACKON()	    via_reg_or(VIA1, vBufB, vPB4)
    127 #define ADB_SET_STATE_ACKOFF()	    via_reg_and(VIA1, vBufB, ~vPB4)
    128 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
    129 #define ADB_SET_STATE_ACKON_CUDA()  via_reg_and(VIA1, vBufB, ~vPB4)
    130 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
    131 #define ADB_SET_SR_INPUT()	    via_reg_and(VIA1, vACR, ~vSR_OUT)
    132 #define ADB_SET_SR_OUTPUT()	    via_reg_or(VIA1, vACR, vSR_OUT)
    133 #define ADB_SR()		    read_via_reg(VIA1, vSR)
    134 #define ADB_VIA_INTR_ENABLE()	    write_via_reg(VIA1, vIER, 0x84)
    135 #define ADB_VIA_INTR_DISABLE()	    write_via_reg(VIA1, vIER, 0x04)
    136 #define ADB_VIA_CLR_INTR()	    write_via_reg(VIA1, vIFR, 0x04)
    137 #define ADB_INTR_IS_OFF		   (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
    138 #define ADB_INTR_IS_ON		   (0 == (read_via_reg(VIA1, vBufB) & vPB3))
    139 #define ADB_SR_INTR_IS_OFF	   (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
    140 #define ADB_SR_INTR_IS_ON	   (vSR_INT == (read_via_reg(VIA1, \
    141 						vIFR) & vSR_INT))
    142 
    143 /*
    144  * This is the delay that is required (in uS) between certain
    145  * ADB transactions. The actual timing delay for for each uS is
    146  * calculated at boot time to account for differences in machine speed.
    147  */
    148 #define ADB_DELAY	150
    149 
    150 /*
    151  * Maximum ADB message length; includes space for data, result, and
    152  * device code - plus a little for safety.
    153  */
    154 #define ADB_MAX_MSG_LENGTH	16
    155 #define ADB_MAX_HDR_LENGTH	8
    156 
    157 #define ADB_QUEUE		32
    158 #define ADB_TICKLE_TICKS	4
    159 
    160 /*
    161  * A structure for storing information about each ADB device.
    162  */
    163 struct ADBDevEntry {
    164 	void	(*ServiceRtPtr) __P((void));
    165 	void	*DataAreaAddr;
    166 	char	devType;
    167 	char	origAddr;
    168 	char	currentAddr;
    169 };
    170 
    171 /*
    172  * Used to hold ADB commands that are waiting to be sent out.
    173  */
    174 struct adbCmdHoldEntry {
    175 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
    176 	u_char	*saveBuf;	/* buffer to know where to save result */
    177 	u_char	*compRout;	/* completion routine pointer */
    178 	u_char	*data;		/* completion routine data pointer */
    179 };
    180 
    181 /*
    182  * Eventually used for two separate queues, the queue between
    183  * the upper and lower halves, and the outgoing packet queue.
    184  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
    185  */
    186 struct adbCommand {
    187 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
    188 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
    189 	u_char	*saveBuf;	/* where to save result */
    190 	u_char	*compRout;	/* completion routine pointer */
    191 	u_char	*compData;	/* completion routine data pointer */
    192 	u_int	cmd;		/* the original command for this data */
    193 	u_int	unsol;		/* 1 if packet was unsolicited */
    194 	u_int	ack_only;	/* 1 for no special processing */
    195 };
    196 
    197 /*
    198  * A few variables that we need and their initial values.
    199  */
    200 int	adbHardware = ADB_HW_UNKNOWN;
    201 int	adbActionState = ADB_ACTION_NOTREADY;
    202 int	adbBusState = ADB_BUS_UNKNOWN;
    203 int	adbWaiting = 0;		/* waiting for return data from the device */
    204 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
    205 int	adbOutQueueHasData = 0;	/* something in the queue waiting to go out */
    206 int	adbNextEnd = 0;		/* the next incoming bute is the last (II) */
    207 int	adbSoftPower = 0;	/* machine supports soft power */
    208 
    209 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
    210 u_char	*adbBuffer = (long)0;	/* pointer to user data area */
    211 void	*adbCompRout = (long)0;	/* pointer to the completion routine */
    212 void	*adbCompData = (long)0;	/* pointer to the completion routine data */
    213 long	adbFakeInts = 0;	/* keeps track of fake ADB interrupts for
    214 				 * timeouts (II) */
    215 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
    216 int	adbSendTalk = 0;	/* the intr routine is sending the talk, not
    217 				 * the user (II) */
    218 int	adbPolling = 0;		/* we are polling for service request */
    219 int	adbPollCmd = 0;		/* the last poll command we sent */
    220 
    221 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
    222 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
    223 struct	adbCmdHoldEntry adbOutQueue;		/* our 1 entry output queue */
    224 
    225 int	adbSentChars = 0;	/* how many characters we have sent */
    226 int	adbLastDevice = 0;	/* last ADB dev we heard from (II ONLY) */
    227 int	adbLastDevIndex = 0;	/* last ADB dev loc in dev table (II ONLY) */
    228 int	adbLastCommand = 0;	/* the last ADB command we sent (II) */
    229 
    230 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
    231 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
    232 
    233 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
    234 int	adbInCount = 0;			/* how many packets in in queue */
    235 int	adbInHead = 0;			/* head of in queue */
    236 int	adbInTail = 0;			/* tail of in queue */
    237 struct	adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
    238 int	adbOutCount = 0;		/* how many packets in out queue */
    239 int	adbOutHead = 0;			/* head of out queue */
    240 int	adbOutTail = 0;			/* tail of out queue */
    241 
    242 int	tickle_count = 0;		/* how many tickles seen for this packet? */
    243 int	tickle_serial = 0;		/* the last packet tickled */
    244 int	adb_cuda_serial = 0;		/* the current packet */
    245 
    246 volatile u_char *Via1Base;
    247 extern int adb_polling;			/* Are we polling? */
    248 
    249 void	pm_setup_adb __P((void));
    250 void	pm_check_adb_devices __P((int));
    251 void	pm_intr __P((void));
    252 int	pm_adb_op __P((u_char *, void *, void *, int));
    253 void	pm_init_adb_device __P((void));
    254 
    255 /*
    256  * The following are private routines.
    257  */
    258 #ifdef ADB_DEBUG
    259 void	print_single __P((u_char *));
    260 #endif
    261 void	adb_intr __P((void));
    262 void	adb_intr_II __P((void));
    263 void	adb_intr_IIsi __P((void));
    264 void	adb_intr_cuda __P((void));
    265 void	adb_soft_intr __P((void));
    266 int	send_adb_II __P((u_char *, u_char *, void *, void *, int));
    267 int	send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
    268 int	send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
    269 void	adb_intr_cuda_test __P((void));
    270 void	adb_cuda_tickle __P((void));
    271 void	adb_pass_up __P((struct adbCommand *));
    272 void	adb_op_comprout __P((caddr_t, caddr_t, int));
    273 void	adb_reinit __P((void));
    274 int	count_adbs __P((void));
    275 int	get_ind_adb_info __P((ADBDataBlock *, int));
    276 int	get_adb_info __P((ADBDataBlock *, int));
    277 int	set_adb_info __P((ADBSetInfoBlock *, int));
    278 void	adb_setup_hw_type __P((void));
    279 int	adb_op __P((Ptr, Ptr, Ptr, short));
    280 int	adb_op_sync __P((Ptr, Ptr, Ptr, short));
    281 void	adb_read_II __P((u_char *));
    282 void	adb_hw_setup __P((void));
    283 void	adb_hw_setup_IIsi __P((u_char *));
    284 void	adb_comp_exec __P((void));
    285 int	adb_cmd_result __P((u_char *));
    286 int	adb_cmd_extra __P((u_char *));
    287 int	adb_guess_next_device __P((void));
    288 int	adb_prog_switch_enable __P((void));
    289 int	adb_prog_switch_disable __P((void));
    290 /* we should create this and it will be the public version */
    291 int	send_adb __P((u_char *, void *, void *));
    292 
    293 #ifdef ADB_DEBUG
    294 /*
    295  * print_single
    296  * Diagnostic display routine. Displays the hex values of the
    297  * specified elements of the u_char. The length of the "string"
    298  * is in [0].
    299  */
    300 void
    301 print_single(thestring)
    302 	u_char *thestring;
    303 {
    304 	int x;
    305 
    306 	if ((int)(thestring[0]) == 0) {
    307 		printf_intr("nothing returned\n");
    308 		return;
    309 	}
    310 	if (thestring == 0) {
    311 		printf_intr("no data - null pointer\n");
    312 		return;
    313 	}
    314 	if (thestring[0] > 20) {
    315 		printf_intr("ADB: ACK > 20 no way!\n");
    316 		thestring[0] = 20;
    317 	}
    318 	printf_intr("(length=0x%x):", thestring[0]);
    319 	for (x = 0; x < thestring[0]; x++)
    320 		printf_intr("  0x%02x", thestring[x + 1]);
    321 	printf_intr("\n");
    322 }
    323 #endif
    324 
    325 void
    326 adb_cuda_tickle(void)
    327 {
    328 	volatile int s;
    329 
    330 	if (adbActionState == ADB_ACTION_IN) {
    331 		if (tickle_serial == adb_cuda_serial) {
    332 			if (++tickle_count > 0) {
    333 				s = splhigh();
    334 				adbActionState = ADB_ACTION_IDLE;
    335 				adbInputBuffer[0] = 0;
    336 				ADB_SET_STATE_IDLE_CUDA();
    337 				splx(s);
    338 			}
    339 		} else {
    340 			tickle_serial = adb_cuda_serial;
    341 			tickle_count = 0;
    342 		}
    343 	} else {
    344 		tickle_serial = adb_cuda_serial;
    345 		tickle_count = 0;
    346 	}
    347 
    348 	timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
    349 }
    350 
    351 /*
    352  * called when when an adb interrupt happens
    353  *
    354  * Cuda version of adb_intr
    355  * TO DO: do we want to add some calls to intr_dispatch() here to
    356  * grab serial interrupts?
    357  */
    358 void
    359 adb_intr_cuda(void)
    360 {
    361 	volatile int i, ending;
    362 	volatile unsigned int s;
    363 	struct adbCommand packet;
    364 
    365 	s = splhigh();		/* can't be too careful - might be called */
    366 	/* from a routine, NOT an interrupt */
    367 
    368 	ADB_VIA_CLR_INTR();	/* clear interrupt */
    369 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
    370 
    371 switch_start:
    372 	switch (adbActionState) {
    373 	case ADB_ACTION_IDLE:
    374 		/*
    375 		 * This is an unexpected packet, so grab the first (dummy)
    376 		 * byte, set up the proper vars, and tell the chip we are
    377 		 * starting to receive the packet by setting the TIP bit.
    378 		 */
    379 		adbInputBuffer[1] = ADB_SR();
    380 		adb_cuda_serial++;
    381 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
    382 			break;
    383 
    384 		ADB_SET_SR_INPUT();
    385 		ADB_SET_STATE_TIP();
    386 
    387 		adbInputBuffer[0] = 1;
    388 		adbActionState = ADB_ACTION_IN;
    389 #ifdef ADB_DEBUG
    390 		if (adb_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 ADB_DEBUG
    405 			if (adb_debug) {
    406 				printf_intr("in end 0x%02x ",
    407 				    adbInputBuffer[adbInputBuffer[0]]);
    408 				print_single(adbInputBuffer);
    409 			}
    410 #endif
    411 
    412 			/*
    413 			 * Are we waiting AND does this packet match what we
    414 			 * are waiting for AND is it coming from either the
    415 			 * ADB or RTC/PRAM sub-device? This section _should_
    416 			 * recognize all ADB and RTC/PRAM type commands, but
    417 			 * there may be more... NOTE: commands are always at
    418 			 * [4], even for RTC/PRAM commands.
    419 			 */
    420 			/* set up data for adb_pass_up */
    421 			for (i = 0; i <= adbInputBuffer[0]; i++)
    422 				packet.data[i] = adbInputBuffer[i];
    423 
    424 			if ((adbWaiting == 1) &&
    425 			    (adbInputBuffer[4] == adbWaitingCmd) &&
    426 			    ((adbInputBuffer[2] == 0x00) ||
    427 			    (adbInputBuffer[2] == 0x01))) {
    428 				packet.saveBuf = adbBuffer;
    429 				packet.compRout = adbCompRout;
    430 				packet.compData = adbCompData;
    431 				packet.unsol = 0;
    432 				packet.ack_only = 0;
    433 				adb_pass_up(&packet);
    434 
    435 				adbWaitingCmd = 0;	/* reset "waiting" vars */
    436 				adbWaiting = 0;
    437 				adbBuffer = (long)0;
    438 				adbCompRout = (long)0;
    439 				adbCompData = (long)0;
    440 			} else {
    441 				packet.unsol = 1;
    442 				packet.ack_only = 0;
    443 				adb_pass_up(&packet);
    444 			}
    445 
    446 
    447 			/* reset vars and signal the end of this frame */
    448 			adbActionState = ADB_ACTION_IDLE;
    449 			adbInputBuffer[0] = 0;
    450 			ADB_SET_STATE_IDLE_CUDA();
    451 			/*ADB_SET_SR_INPUT();*/
    452 
    453 			/*
    454 			 * If there is something waiting to be sent out,
    455 			 * the set everything up and send the first byte.
    456 			 */
    457 			if (adbWriteDelay == 1) {
    458 				delay(ADB_DELAY);	/* required */
    459 				adbSentChars = 0;
    460 				adbActionState = ADB_ACTION_OUT;
    461 				/*
    462 				 * If the interrupt is on, we were too slow
    463 				 * and the chip has already started to send
    464 				 * something to us, so back out of the write
    465 				 * and start a read cycle.
    466 				 */
    467 				if (ADB_INTR_IS_ON) {
    468 					ADB_SET_SR_INPUT();
    469 					ADB_SET_STATE_IDLE_CUDA();
    470 					adbSentChars = 0;
    471 					adbActionState = ADB_ACTION_IDLE;
    472 					adbInputBuffer[0] = 0;
    473 					break;
    474 				}
    475 				/*
    476 				 * If we got here, it's ok to start sending
    477 				 * so load the first byte and tell the chip
    478 				 * we want to send.
    479 				 */
    480 				ADB_SET_STATE_TIP();
    481 				ADB_SET_SR_OUTPUT();
    482 				write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
    483 			}
    484 		} else {
    485 			ADB_TOGGLE_STATE_ACK_CUDA();
    486 #ifdef ADB_DEBUG
    487 			if (adb_debug)
    488 				printf_intr("in 0x%02x ",
    489 				    adbInputBuffer[adbInputBuffer[0]]);
    490 #endif
    491 		}
    492 		break;
    493 
    494 	case ADB_ACTION_OUT:
    495 		i = ADB_SR();	/* reset SR-intr in IFR */
    496 #ifdef ADB_DEBUG
    497 		if (adb_debug)
    498 			printf_intr("intr out 0x%02x ", i);
    499 #endif
    500 
    501 		adbSentChars++;
    502 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
    503 #ifdef ADB_DEBUG
    504 			if (adb_debug)
    505 				printf_intr("intr was on ");
    506 #endif
    507 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    508 			ADB_SET_STATE_IDLE_CUDA();
    509 			adbSentChars = 0;	/* must start all over */
    510 			adbActionState = ADB_ACTION_IDLE;	/* new state */
    511 			adbInputBuffer[0] = 0;
    512 			adbWriteDelay = 1;	/* must retry when done with
    513 						 * read */
    514 			delay(ADB_DELAY);
    515 			goto switch_start;	/* process next state right
    516 						 * now */
    517 			break;
    518 		}
    519 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
    520 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
    521 									 * back? */
    522 				adbWaiting = 1;	/* signal waiting for return */
    523 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
    524 			} else {	/* no talk, so done */
    525 				/* set up stuff for adb_pass_up */
    526 				for (i = 0; i <= adbInputBuffer[0]; i++)
    527 					packet.data[i] = adbInputBuffer[i];
    528 				packet.saveBuf = adbBuffer;
    529 				packet.compRout = adbCompRout;
    530 				packet.compData = adbCompData;
    531 				packet.cmd = adbWaitingCmd;
    532 				packet.unsol = 0;
    533 				packet.ack_only = 1;
    534 				adb_pass_up(&packet);
    535 
    536 				/* reset "waiting" vars, just in case */
    537 				adbWaitingCmd = 0;
    538 				adbBuffer = (long)0;
    539 				adbCompRout = (long)0;
    540 				adbCompData = (long)0;
    541 			}
    542 
    543 			adbWriteDelay = 0;	/* done writing */
    544 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
    545 			ADB_SET_SR_INPUT();
    546 			ADB_SET_STATE_IDLE_CUDA();
    547 #ifdef ADB_DEBUG
    548 			if (adb_debug)
    549 				printf_intr("write done ");
    550 #endif
    551 		} else {
    552 			write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* send next byte */
    553 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
    554 							 * shift */
    555 #ifdef ADB_DEBUG
    556 			if (adb_debug)
    557 				printf_intr("toggle ");
    558 #endif
    559 		}
    560 		break;
    561 
    562 	case ADB_ACTION_NOTREADY:
    563 #ifdef ADB_DEBUG
    564 		if (adb_debug)
    565 			printf_intr("adb: not yet initialized\n");
    566 #endif
    567 		break;
    568 
    569 	default:
    570 #ifdef ADB_DEBUG
    571 		if (adb_debug)
    572 			printf_intr("intr: unknown ADB state\n");
    573 #endif
    574 	}
    575 
    576 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
    577 
    578 	splx(s);		/* restore */
    579 
    580 	return;
    581 }				/* end adb_intr_cuda */
    582 
    583 
    584 int
    585 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
    586 	command)
    587 {
    588 	int i, s, len;
    589 
    590 #ifdef ADB_DEBUG
    591 	if (adb_debug)
    592 		printf_intr("SEND\n");
    593 #endif
    594 
    595 	if (adbActionState == ADB_ACTION_NOTREADY)
    596 		return 1;
    597 
    598 	/* Don't interrupt while we are messing with the ADB */
    599 	s = splhigh();
    600 
    601 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
    602 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupt? */
    603 	} else
    604 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
    605 			adbWriteDelay = 1;	/* if no, then we'll "queue"
    606 						 * it up */
    607 		else {
    608 			splx(s);
    609 			return 1;	/* really busy! */
    610 		}
    611 
    612 #ifdef ADB_DEBUG
    613 	if (adb_debug)
    614 		printf_intr("QUEUE\n");
    615 #endif
    616 	if ((long)in == (long)0) {	/* need to convert? */
    617 		/*
    618 		 * Don't need to use adb_cmd_extra here because this section
    619 		 * will be called ONLY when it is an ADB command (no RTC or
    620 		 * PRAM)
    621 		 */
    622 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
    623 						 * doing a listen! */
    624 			len = buffer[0];	/* length of additional data */
    625 		else
    626 			len = 0;/* no additional data */
    627 
    628 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
    629 						 * data */
    630 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
    631 		adbOutputBuffer[2] = (u_char)command;	/* load command */
    632 
    633 		for (i = 1; i <= len; i++)	/* copy additional output
    634 						 * data, if any */
    635 			adbOutputBuffer[2 + i] = buffer[i];
    636 	} else
    637 		for (i = 0; i <= (in[0] + 1); i++)
    638 			adbOutputBuffer[i] = in[i];
    639 
    640 	adbSentChars = 0;	/* nothing sent yet */
    641 	adbBuffer = buffer;	/* save buffer to know where to save result */
    642 	adbCompRout = compRout;	/* save completion routine pointer */
    643 	adbCompData = data;	/* save completion routine data pointer */
    644 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
    645 
    646 	if (adbWriteDelay != 1) {	/* start command now? */
    647 #ifdef ADB_DEBUG
    648 		if (adb_debug)
    649 			printf_intr("out start NOW");
    650 #endif
    651 		delay(ADB_DELAY);
    652 		adbActionState = ADB_ACTION_OUT;	/* set next state */
    653 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
    654 		write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* load byte for output */
    655 		ADB_SET_STATE_ACKOFF_CUDA();
    656 		ADB_SET_STATE_TIP();	/* tell ADB that we want to send */
    657 	}
    658 	adbWriteDelay = 1;	/* something in the write "queue" */
    659 
    660 	splx(s);
    661 
    662 	if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */
    663 		/* poll until byte done */
    664 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
    665 		    || (adbWaiting == 1))
    666 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
    667 				adb_intr_cuda();	/* process it */
    668 				adb_soft_intr();
    669 			}
    670 
    671 	return 0;
    672 }				/* send_adb_cuda */
    673 
    674 
    675 void
    676 adb_intr_II(void)
    677 {
    678 	panic("adb_intr_II");
    679 }
    680 
    681 
    682 /*
    683  * send_adb version for II series machines
    684  */
    685 int
    686 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
    687 {
    688 	panic("send_adb_II");
    689 }
    690 
    691 
    692 /*
    693  * This routine is called from the II series interrupt routine
    694  * to determine what the "next" device is that should be polled.
    695  */
    696 int
    697 adb_guess_next_device(void)
    698 {
    699 	int last, i, dummy;
    700 
    701 	if (adbStarting) {
    702 		/*
    703 		 * Start polling EVERY device, since we can't be sure there is
    704 		 * anything in the device table yet
    705 		 */
    706 		if (adbLastDevice < 1 || adbLastDevice > 15)
    707 			adbLastDevice = 1;
    708 		if (++adbLastDevice > 15)	/* point to next one */
    709 			adbLastDevice = 1;
    710 	} else {
    711 		/* find the next device using the device table */
    712 		if (adbLastDevice < 1 || adbLastDevice > 15)	/* let's be parinoid */
    713 			adbLastDevice = 2;
    714 		last = 1;	/* default index location */
    715 
    716 		for (i = 1; i < 16; i++)	/* find index entry */
    717 			if (ADBDevTable[i].currentAddr == adbLastDevice) {	/* look for device */
    718 				last = i;	/* found it */
    719 				break;
    720 			}
    721 		dummy = last;	/* index to start at */
    722 		for (;;) {	/* find next device in index */
    723 			if (++dummy > 15)	/* wrap around if needed */
    724 				dummy = 1;
    725 			if (dummy == last) {	/* didn't find any other
    726 						 * device! This can happen if
    727 						 * there are no devices on the
    728 						 * bus */
    729 				dummy = 2;
    730 				break;
    731 			}
    732 			/* found the next device */
    733 			if (ADBDevTable[dummy].devType != 0)
    734 				break;
    735 		}
    736 		adbLastDevice = ADBDevTable[dummy].currentAddr;
    737 	}
    738 	return adbLastDevice;
    739 }
    740 
    741 
    742 /*
    743  * Called when when an adb interrupt happens.
    744  * This routine simply transfers control over to the appropriate
    745  * code for the machine we are running on.
    746  */
    747 void
    748 adb_intr(void)
    749 {
    750 	switch (adbHardware) {
    751 	case ADB_HW_II:
    752 		adb_intr_II();
    753 		break;
    754 
    755 	case ADB_HW_IISI:
    756 		adb_intr_IIsi();
    757 		break;
    758 
    759 	case ADB_HW_PB:
    760 		pm_intr();
    761 		break;
    762 
    763 	case ADB_HW_CUDA:
    764 		adb_intr_cuda();
    765 		break;
    766 
    767 	case ADB_HW_UNKNOWN:
    768 		break;
    769 	}
    770 }
    771 
    772 
    773 /*
    774  * called when when an adb interrupt happens
    775  *
    776  * IIsi version of adb_intr
    777  *
    778  */
    779 void
    780 adb_intr_IIsi(void)
    781 {
    782 	panic("adb_intr_IIsi");
    783 }
    784 
    785 
    786 /*****************************************************************************
    787  * if the device is currently busy, and there is no data waiting to go out, then
    788  * the data is "queued" in the outgoing buffer. If we are already waiting, then
    789  * we return.
    790  * in: if (in == 0) then the command string is built from command and buffer
    791  *     if (in != 0) then in is used as the command string
    792  * buffer: additional data to be sent (used only if in == 0)
    793  *         this is also where return data is stored
    794  * compRout: the completion routine that is called when then return value
    795  *	     is received (if a return value is expected)
    796  * data: a data pointer that can be used by the completion routine
    797  * command: an ADB command to be sent (used only if in == 0)
    798  *
    799  */
    800 int
    801 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
    802 	command)
    803 {
    804 	panic("send_adb_IIsi");
    805 }
    806 
    807 
    808 /*
    809  * adb_pass_up is called by the interrupt-time routines.
    810  * It takes the raw packet data that was received from the
    811  * device and puts it into the queue that the upper half
    812  * processes. It then signals for a soft ADB interrupt which
    813  * will eventually call the upper half routine (adb_soft_intr).
    814  *
    815  * If in->unsol is 0, then this is either the notification
    816  * that the packet was sent (on a LISTEN, for example), or the
    817  * response from the device (on a TALK). The completion routine
    818  * is called only if the user specified one.
    819  *
    820  * If in->unsol is 1, then this packet was unsolicited and
    821  * so we look up the device in the ADB device table to determine
    822  * what it's default service routine is.
    823  *
    824  * If in->ack_only is 1, then we really only need to call
    825  * the completion routine, so don't do any other stuff.
    826  *
    827  * Note that in->data contains the packet header AND data,
    828  * while adbInbound[]->data contains ONLY data.
    829  *
    830  * Note: Called only at interrupt time. Assumes this.
    831  */
    832 void
    833 adb_pass_up(struct adbCommand *in)
    834 {
    835 	int i, start = 0, len = 0, cmd = 0;
    836 	ADBDataBlock block;
    837 
    838 	/* temp for testing */
    839 	/*u_char *buffer = 0;*/
    840 	/*u_char *compdata = 0;*/
    841 	/*u_char *comprout = 0;*/
    842 
    843 	if (adbInCount >= ADB_QUEUE) {
    844 #ifdef ADB_DEBUG
    845 		if (adb_debug)
    846 			printf_intr("adb: ring buffer overflow\n");
    847 #endif
    848 		return;
    849 	}
    850 
    851 	if (in->ack_only) {
    852 		len = in->data[0];
    853 		cmd = in->cmd;
    854 		start = 0;
    855 	} else {
    856 		switch (adbHardware) {
    857 		case ADB_HW_II:
    858 			cmd = in->data[1];
    859 			if (in->data[0] < 2)
    860 				len = 0;
    861 			else
    862 				len = in->data[0]-1;
    863 			start = 1;
    864 			break;
    865 
    866 		case ADB_HW_IISI:
    867 		case ADB_HW_CUDA:
    868 			/* If it's unsolicited, accept only ADB data for now */
    869 			if (in->unsol)
    870 				if (0 != in->data[2])
    871 					return;
    872 			cmd = in->data[4];
    873 			if (in->data[0] < 5)
    874 				len = 0;
    875 			else
    876 				len = in->data[0]-4;
    877 			start = 4;
    878 			break;
    879 
    880 		case ADB_HW_PB:
    881 			cmd = in->data[1];
    882 			if (in->data[0] < 2)
    883 				len = 0;
    884 			else
    885 				len = in->data[0]-1;
    886 			start = 1;
    887 			break;
    888 
    889 		case ADB_HW_UNKNOWN:
    890 			return;
    891 		}
    892 
    893 		/* Make sure there is a valid device entry for this device */
    894 		if (in->unsol) {
    895 			/* ignore unsolicited data during adbreinit */
    896 			if (adbStarting)
    897 				return;
    898 			/* get device's comp. routine and data area */
    899 			if (-1 == get_adb_info(&block, ((cmd & 0xf0) >> 4)))
    900 				return;
    901 		}
    902 	}
    903 
    904 	/*
    905  	 * If this is an unsolicited packet, we need to fill in
    906  	 * some info so adb_soft_intr can process this packet
    907  	 * properly. If it's not unsolicited, then use what
    908  	 * the caller sent us.
    909  	 */
    910 	if (in->unsol) {
    911 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
    912 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
    913 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
    914 	} else {
    915 		adbInbound[adbInTail].compRout = (void *)in->compRout;
    916 		adbInbound[adbInTail].compData = (void *)in->compData;
    917 		adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
    918 	}
    919 
    920 #ifdef ADB_DEBUG
    921 	if (adb_debug && in->data[1] == 2)
    922 		printf_intr("adb: caught error\n");
    923 #endif
    924 
    925 	/* copy the packet data over */
    926 	/*
    927 	 * TO DO: If the *_intr routines fed their incoming data
    928 	 * directly into an adbCommand struct, which is passed to
    929 	 * this routine, then we could eliminate this copy.
    930 	 */
    931 	for (i = 1; i <= len; i++)
    932 		adbInbound[adbInTail].data[i] = in->data[start+i];
    933 
    934 	adbInbound[adbInTail].data[0] = len;
    935 	adbInbound[adbInTail].cmd = cmd;
    936 
    937 	adbInCount++;
    938 	if (++adbInTail >= ADB_QUEUE)
    939 		adbInTail = 0;
    940 
    941 	/*
    942 	 * If the debugger is running, call upper half manually.
    943 	 * Otherwise, trigger a soft interrupt to handle the rest later.
    944 	 */
    945 	if (adb_polling)
    946 		adb_soft_intr();
    947 	else
    948 		setsoftadb();
    949 
    950 	return;
    951 }
    952 
    953 
    954 /*
    955  * Called to process the packets after they have been
    956  * placed in the incoming queue.
    957  *
    958  */
    959 void
    960 adb_soft_intr(void)
    961 {
    962 	int s, i;
    963 	int cmd = 0;
    964 	u_char *buffer = 0;
    965 	u_char *comprout = 0;
    966 	u_char *compdata = 0;
    967 
    968 #if 0
    969 	s = splhigh();
    970 	printf_intr("sr: %x\n", (s & 0x0700));
    971 	splx(s);
    972 #endif
    973 
    974 /*delay(2*ADB_DELAY);*/
    975 
    976 	while (adbInCount) {
    977 #ifdef ADB_DEBUG
    978 		if (adb_debug & 0x80)
    979 			printf_intr("%x %x %x ",
    980 			    adbInCount, adbInHead, adbInTail);
    981 #endif
    982 		/* get the data we need from the queue */
    983 		buffer = adbInbound[adbInHead].saveBuf;
    984 		comprout = adbInbound[adbInHead].compRout;
    985 		compdata = adbInbound[adbInHead].compData;
    986 		cmd = adbInbound[adbInHead].cmd;
    987 
    988 		/* copy over data to data area if it's valid */
    989 		/*
    990 		 * Note that for unsol packets we don't want to copy the
    991 		 * data anywhere, so buffer was already set to 0.
    992 		 * For ack_only buffer was set to 0, so don't copy.
    993 		 */
    994 		if (buffer)
    995 			for (i = 0; i <= adbInbound[adbInHead].data[0]; i++)
    996 				*(buffer+i) = adbInbound[adbInHead].data[i];
    997 
    998 #ifdef ADB_DEBUG
    999 			if (adb_debug & 0x80) {
   1000 				printf_intr("%p %p %p %x ",
   1001 				    buffer, comprout, compdata, (short)cmd);
   1002 				printf_intr("buf: ");
   1003 				print_single(adbInbound[adbInHead].data);
   1004 			}
   1005 #endif
   1006 
   1007 		/* call default completion routine if it's valid */
   1008 		if (comprout) {
   1009 			int (*f)() = (void *)comprout;
   1010 
   1011 			(*f)(buffer, compdata, cmd);
   1012 #if 0
   1013 #ifdef __NetBSD__
   1014 			asm("	movml #0xffff,sp@-	| save all registers
   1015 				movl %0,a2 		| compdata
   1016 				movl %1,a1 		| comprout
   1017 				movl %2,a0 		| buffer
   1018 				movl %3,d0 		| cmd
   1019 				jbsr a1@ 		| go call the routine
   1020 				movml sp@+,#0xffff	| restore all registers"
   1021 			    :
   1022 			    : "g"(compdata), "g"(comprout),
   1023 				"g"(buffer), "g"(cmd)
   1024 			    : "d0", "a0", "a1", "a2");
   1025 #else					/* for macos based testing */
   1026 			asm
   1027 			{
   1028 				movem.l a0/a1/a2/d0, -(a7)
   1029 				move.l compdata, a2
   1030 				move.l comprout, a1
   1031 				move.l buffer, a0
   1032 				move.w cmd, d0
   1033 				jsr(a1)
   1034 				movem.l(a7)+, d0/a2/a1/a0
   1035 			}
   1036 #endif
   1037 #endif
   1038 		}
   1039 
   1040 		s = splhigh();
   1041 		adbInCount--;
   1042 		if (++adbInHead >= ADB_QUEUE)
   1043 			adbInHead = 0;
   1044 		splx(s);
   1045 
   1046 	}
   1047 	return;
   1048 }
   1049 
   1050 
   1051 /*
   1052  * This is my version of the ADBOp routine. It mainly just calls the
   1053  * hardware-specific routine.
   1054  *
   1055  *   data 	: pointer to data area to be used by compRout
   1056  *   compRout	: completion routine
   1057  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
   1058  *		  byte 0 = # of bytes
   1059  *		: for TALK: points to place to save return data
   1060  *   command	: the adb command to send
   1061  *   result	: 0 = success
   1062  *		: -1 = could not complete
   1063  */
   1064 int
   1065 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
   1066 {
   1067 	int result;
   1068 
   1069 	switch (adbHardware) {
   1070 	case ADB_HW_II:
   1071 		result = send_adb_II((u_char *)0, (u_char *)buffer,
   1072 		    (void *)compRout, (void *)data, (int)command);
   1073 		if (result == 0)
   1074 			return 0;
   1075 		else
   1076 			return -1;
   1077 		break;
   1078 
   1079 	case ADB_HW_IISI:
   1080 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
   1081 		    (void *)compRout, (void *)data, (int)command);
   1082 		/*
   1083 		 * I wish I knew why this delay is needed. It usually needs to
   1084 		 * be here when several commands are sent in close succession,
   1085 		 * especially early in device probes when doing collision
   1086 		 * detection. It must be some race condition. Sigh. - jpw
   1087 		 */
   1088 		delay(100);
   1089 		if (result == 0)
   1090 			return 0;
   1091 		else
   1092 			return -1;
   1093 		break;
   1094 
   1095 	case ADB_HW_PB:
   1096 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
   1097 		    (void *)data, (int)command);
   1098 
   1099 		if (result == 0)
   1100 			return 0;
   1101 		else
   1102 			return -1;
   1103 		break;
   1104 
   1105 	case ADB_HW_CUDA:
   1106 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
   1107 		    (void *)compRout, (void *)data, (int)command);
   1108 		if (result == 0)
   1109 			return 0;
   1110 		else
   1111 			return -1;
   1112 		break;
   1113 
   1114 	case ADB_HW_UNKNOWN:
   1115 	default:
   1116 		return -1;
   1117 	}
   1118 }
   1119 
   1120 
   1121 /*
   1122  * adb_hw_setup
   1123  * This routine sets up the possible machine specific hardware
   1124  * config (mainly VIA settings) for the various models.
   1125  */
   1126 void
   1127 adb_hw_setup(void)
   1128 {
   1129 	volatile int i;
   1130 	u_char send_string[ADB_MAX_MSG_LENGTH];
   1131 
   1132 	switch (adbHardware) {
   1133 	case ADB_HW_II:
   1134 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
   1135 						 * outputs */
   1136 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
   1137 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
   1138 							 * to IN (II, IIsi) */
   1139 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1140 							 * hardware (II, IIsi) */
   1141 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1142 						 * code only */
   1143 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
   1144 						 * are on (II, IIsi) */
   1145 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
   1146 
   1147 		ADB_VIA_CLR_INTR();	/* clear interrupt */
   1148 		break;
   1149 
   1150 	case ADB_HW_IISI:
   1151 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
   1152 						 * outputs */
   1153 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
   1154 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
   1155 							 * to IN (II, IIsi) */
   1156 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1157 							 * hardware (II, IIsi) */
   1158 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1159 						 * code only */
   1160 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
   1161 						 * are on (II, IIsi) */
   1162 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
   1163 
   1164 		/* get those pesky clock ticks we missed while booting */
   1165 		for (i = 0; i < 30; i++) {
   1166 			delay(ADB_DELAY);
   1167 			adb_hw_setup_IIsi(send_string);
   1168 #ifdef ADB_DEBUG
   1169 			if (adb_debug) {
   1170 				printf_intr("adb: cleanup: ");
   1171 				print_single(send_string);
   1172 			}
   1173 #endif
   1174 			delay(ADB_DELAY);
   1175 			if (ADB_INTR_IS_OFF)
   1176 				break;
   1177 		}
   1178 		break;
   1179 
   1180 	case ADB_HW_PB:
   1181 		/*
   1182 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
   1183 		 * pm_direct.h?
   1184 		 */
   1185 		write_via_reg(VIA1, vIFR, 0x90);	/* clear interrupt */
   1186 		break;
   1187 
   1188 	case ADB_HW_CUDA:
   1189 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
   1190 						 * outputs */
   1191 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
   1192 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
   1193 							 * to IN */
   1194 		write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
   1195 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1196 							 * hardware */
   1197 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1198 						 * code only */
   1199 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
   1200 						 * are on */
   1201 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
   1202 
   1203 		/* sort of a device reset */
   1204 		i = ADB_SR();	/* clear interrupt */
   1205 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
   1206 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
   1207 		delay(ADB_DELAY);
   1208 		ADB_SET_STATE_TIP();	/* signal start of frame */
   1209 		delay(ADB_DELAY);
   1210 		ADB_TOGGLE_STATE_ACK_CUDA();
   1211 		delay(ADB_DELAY);
   1212 		ADB_CLR_STATE_TIP();
   1213 		delay(ADB_DELAY);
   1214 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
   1215 		i = ADB_SR();	/* clear interrupt */
   1216 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
   1217 		break;
   1218 
   1219 	case ADB_HW_UNKNOWN:
   1220 	default:
   1221 		write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
   1222 						 * DO: turn PB ints off? */
   1223 		return;
   1224 		break;
   1225 	}
   1226 }
   1227 
   1228 
   1229 /*
   1230  * adb_hw_setup_IIsi
   1231  * This is sort of a "read" routine that forces the adb hardware through a read cycle
   1232  * if there is something waiting. This helps "clean up" any commands that may have gotten
   1233  * stuck or stopped during the boot process.
   1234  *
   1235  */
   1236 void
   1237 adb_hw_setup_IIsi(u_char * buffer)
   1238 {
   1239 	panic("adb_hw_setup_IIsi");
   1240 }
   1241 
   1242 
   1243 
   1244 /*
   1245  * adb_reinit sets up the adb stuff
   1246  *
   1247  */
   1248 void
   1249 adb_reinit(void)
   1250 {
   1251 	u_char send_string[ADB_MAX_MSG_LENGTH];
   1252 	int s = 0;
   1253 	volatile int i, x;
   1254 	int command;
   1255 	int result;
   1256 	int saveptr;		/* point to next free relocation address */
   1257 	int device;
   1258 	int nonewtimes;		/* times thru loop w/o any new devices */
   1259 	ADBDataBlock data;	/* temp. holder for getting device info */
   1260 
   1261 	(void)(&s);		/* work around lame GCC bug */
   1262 
   1263 	/* Make sure we are not interrupted while building the table. */
   1264 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
   1265 		s = splhigh();
   1266 
   1267 	ADBNumDevices = 0;	/* no devices yet */
   1268 
   1269 	/* Let intr routines know we are running reinit */
   1270 	adbStarting = 1;
   1271 
   1272 	/*
   1273 	 * Initialize the ADB table.  For now, we'll always use the same table
   1274 	 * that is defined at the beginning of this file - no mallocs.
   1275 	 */
   1276 	for (i = 0; i < 16; i++)
   1277 		ADBDevTable[i].devType = 0;
   1278 
   1279 	adb_setup_hw_type();	/* setup hardware type */
   1280 
   1281 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
   1282 
   1283 	delay(1000);
   1284 
   1285 	/* send an ADB reset first */
   1286 	adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
   1287 
   1288 	/*
   1289 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
   1290 	 * which device addresses are in use and which are free. For each
   1291 	 * address that is in use, move the device at that address to a higher
   1292 	 * free address. Continue doing this at that address until no device
   1293 	 * responds at that address. Then move the last device that was moved
   1294 	 * back to the original address. Do this for the remaining addresses
   1295 	 * that we determined were in use.
   1296 	 *
   1297 	 * When finished, do this entire process over again with the updated
   1298 	 * list of in use addresses. Do this until no new devices have been
   1299 	 * found in 20 passes though the in use address list. (This probably
   1300 	 * seems long and complicated, but it's the best way to detect multiple
   1301 	 * devices at the same address - sometimes it takes a couple of tries
   1302 	 * before the collision is detected.)
   1303 	 */
   1304 
   1305 	/* initial scan through the devices */
   1306 	for (i = 1; i < 16; i++) {
   1307 		command = (int)(0x0f | ((int)(i & 0x000f) << 4));	/* talk R3 */
   1308 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
   1309 		    (Ptr)0, (short)command);
   1310 		if (0x00 != send_string[0]) {	/* anything come back ?? */
   1311 			ADBDevTable[++ADBNumDevices].devType =
   1312 			    (u_char)send_string[2];
   1313 			ADBDevTable[ADBNumDevices].origAddr = i;
   1314 			ADBDevTable[ADBNumDevices].currentAddr = i;
   1315 			ADBDevTable[ADBNumDevices].DataAreaAddr =
   1316 			    (long)0;
   1317 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
   1318 			pm_check_adb_devices(i);	/* tell pm driver device
   1319 							 * is here */
   1320 		}
   1321 	}
   1322 
   1323 	/* find highest unused address */
   1324 	for (saveptr = 15; saveptr > 0; saveptr--)
   1325 		if (-1 == get_adb_info(&data, saveptr))
   1326 			break;
   1327 
   1328 	if (saveptr == 0)	/* no free addresses??? */
   1329 		saveptr = 15;
   1330 
   1331 #ifdef ADB_DEBUG
   1332 	if (adb_debug & 0x80) {
   1333 		printf_intr("first free is: 0x%02x\n", saveptr);
   1334 		printf_intr("devices: %i\n", ADBNumDevices);
   1335 	}
   1336 #endif
   1337 
   1338 	nonewtimes = 0;		/* no loops w/o new devices */
   1339 	while (nonewtimes++ < 11) {
   1340 		for (i = 1; i <= ADBNumDevices; i++) {
   1341 			device = ADBDevTable[i].currentAddr;
   1342 #ifdef ADB_DEBUG
   1343 			if (adb_debug & 0x80)
   1344 				printf_intr("moving device 0x%02x to 0x%02x "
   1345 				    "(index 0x%02x)  ", device, saveptr, i);
   1346 #endif
   1347 
   1348 			/* send TALK R3 to address */
   1349 			command = (int)(0x0f | ((int)(device & 0x000f) << 4));
   1350 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1351 			    (Ptr)0, (short)command);
   1352 
   1353 			/* move device to higher address */
   1354 			command = (int)(0x0b | ((int)(device & 0x000f) << 4));
   1355 			send_string[0] = 2;
   1356 			send_string[1] = (u_char)(saveptr | 0x60);
   1357 			send_string[2] = 0xfe;
   1358 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1359 			    (Ptr)0, (short)command);
   1360 			delay(500);
   1361 
   1362 			/* send TALK R3 - anything at old address? */
   1363 			command = (int)(0x0f | ((int)(device & 0x000f) << 4));
   1364 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
   1365 			    (Ptr)0, (short)command);
   1366 			if (send_string[0] != 0) {
   1367 				/* new device found */
   1368 				/* update data for previously moved device */
   1369 				ADBDevTable[i].currentAddr = saveptr;
   1370 #ifdef ADB_DEBUG
   1371 				if (adb_debug & 0x80)
   1372 					printf_intr("old device at index %i\n",i);
   1373 #endif
   1374 				/* add new device in table */
   1375 #ifdef ADB_DEBUG
   1376 				if (adb_debug & 0x80)
   1377 					printf_intr("new device found\n");
   1378 #endif
   1379 				ADBDevTable[++ADBNumDevices].devType =
   1380 				    (u_char)send_string[2];
   1381 				ADBDevTable[ADBNumDevices].origAddr = device;
   1382 				ADBDevTable[ADBNumDevices].currentAddr = device;
   1383 				/* These will be set correctly in adbsys.c */
   1384 				/* Until then, unsol. data will be ignored. */
   1385 				ADBDevTable[ADBNumDevices].DataAreaAddr =
   1386 				    (long)0;
   1387 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
   1388 				    (void *)0;
   1389 				/* find next unused address */
   1390 				for (x = saveptr; x > 0; x--)
   1391 					if (-1 == get_adb_info(&data, x)) {
   1392 						saveptr = x;
   1393 						break;
   1394 					}
   1395 #ifdef ADB_DEBUG
   1396 				if (adb_debug & 0x80)
   1397 					printf_intr("new free is 0x%02x\n",
   1398 					    saveptr);
   1399 #endif
   1400 				nonewtimes = 0;
   1401 				/* tell pm driver device is here */
   1402 				pm_check_adb_devices(device);
   1403 			} else {
   1404 #ifdef ADB_DEBUG
   1405 				if (adb_debug & 0x80)
   1406 					printf_intr("moving back...\n");
   1407 #endif
   1408 				/* move old device back */
   1409 				command = (int)(0x0b | ((int)(saveptr & 0x000f) << 4));
   1410 				send_string[0] = 2;
   1411 				send_string[1] = (u_char)(device | 0x60);
   1412 				send_string[2] = 0xfe;
   1413 				adb_op_sync((Ptr)send_string, (Ptr)0,
   1414 				    (Ptr)0, (short)command);
   1415 				delay(1000);
   1416 			}
   1417 		}
   1418 	}
   1419 
   1420 #ifdef ADB_DEBUG
   1421 	if (adb_debug) {
   1422 		for (i = 1; i <= ADBNumDevices; i++) {
   1423 			x = get_ind_adb_info(&data, i);
   1424 			if (x != -1)
   1425 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
   1426 				    i, x, data.devType);
   1427 		}
   1428 	}
   1429 #endif
   1430 
   1431 #ifndef MRG_ADB
   1432 	/* enable the programmer's switch, if we have one */
   1433 	adb_prog_switch_enable();
   1434 #endif
   1435 
   1436 #ifdef ADB_DEBUG
   1437 	if (adb_debug) {
   1438 		if (0 == ADBNumDevices)	/* tell user if no devices found */
   1439 			printf_intr("adb: no devices found\n");
   1440 	}
   1441 #endif
   1442 
   1443 	adbStarting = 0;	/* not starting anymore */
   1444 #ifdef ADB_DEBUG
   1445 	if (adb_debug)
   1446 		printf_intr("adb: ADBReInit complete\n");
   1447 #endif
   1448 
   1449 	if (adbHardware == ADB_HW_CUDA)
   1450 		timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
   1451 
   1452 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
   1453 		splx(s);
   1454 	return;
   1455 }
   1456 
   1457 
   1458 #if 0
   1459 /*
   1460  * adb_comp_exec
   1461  * This is a general routine that calls the completion routine if there is one.
   1462  * NOTE: This routine is now only used by pm_direct.c
   1463  *       All the code in this file (adb_direct.c) uses
   1464  *       the adb_pass_up routine now.
   1465  */
   1466 void
   1467 adb_comp_exec(void)
   1468 {
   1469 	if ((long)0 != adbCompRout) /* don't call if empty return location */
   1470 #ifdef __NetBSD__
   1471 		asm("	movml #0xffff,sp@-	| save all registers
   1472 			movl %0,a2		| adbCompData
   1473 			movl %1,a1		| adbCompRout
   1474 			movl %2,a0		| adbBuffer
   1475 			movl %3,d0		| adbWaitingCmd
   1476 			jbsr a1@		| go call the routine
   1477 			movml sp@+,#0xffff	| restore all registers"
   1478 		    :
   1479 		    : "g"(adbCompData), "g"(adbCompRout),
   1480 			"g"(adbBuffer), "g"(adbWaitingCmd)
   1481 		    : "d0", "a0", "a1", "a2");
   1482 #else /* for Mac OS-based testing */
   1483 		asm {
   1484 			movem.l a0/a1/a2/d0, -(a7)
   1485 			move.l adbCompData, a2
   1486 			move.l adbCompRout, a1
   1487 			move.l adbBuffer, a0
   1488 			move.w adbWaitingCmd, d0
   1489 			jsr(a1)
   1490 			movem.l(a7) +, d0/a2/a1/a0
   1491 		}
   1492 #endif
   1493 }
   1494 #endif
   1495 
   1496 
   1497 /*
   1498  * adb_cmd_result
   1499  *
   1500  * This routine lets the caller know whether the specified adb command string
   1501  * should expect a returned result, such as a TALK command.
   1502  *
   1503  * returns: 0 if a result should be expected
   1504  *          1 if a result should NOT be expected
   1505  */
   1506 int
   1507 adb_cmd_result(u_char *in)
   1508 {
   1509 	switch (adbHardware) {
   1510 	case ADB_HW_II:
   1511 		/* was it an ADB talk command? */
   1512 		if ((in[1] & 0x0c) == 0x0c)
   1513 			return 0;
   1514 		return 1;
   1515 
   1516 	case ADB_HW_IISI:
   1517 	case ADB_HW_CUDA:
   1518 		/* was it an ADB talk command? */
   1519 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
   1520 			return 0;
   1521 		/* was it an RTC/PRAM read date/time? */
   1522 		if ((in[1] == 0x01) && (in[2] == 0x03))
   1523 			return 0;
   1524 		return 1;
   1525 
   1526 	case ADB_HW_PB:
   1527 		return 1;
   1528 
   1529 	case ADB_HW_UNKNOWN:
   1530 	default:
   1531 		return 1;
   1532 	}
   1533 }
   1534 
   1535 
   1536 /*
   1537  * adb_cmd_extra
   1538  *
   1539  * This routine lets the caller know whether the specified adb command string
   1540  * may have extra data appended to the end of it, such as a LISTEN command.
   1541  *
   1542  * returns: 0 if extra data is allowed
   1543  *          1 if extra data is NOT allowed
   1544  */
   1545 int
   1546 adb_cmd_extra(u_char *in)
   1547 {
   1548 	switch (adbHardware) {
   1549 		case ADB_HW_II:
   1550 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
   1551 			return 0;
   1552 		return 1;
   1553 
   1554 	case ADB_HW_IISI:
   1555 	case ADB_HW_CUDA:
   1556 		/*
   1557 		 * TO DO: support needs to be added to recognize RTC and PRAM
   1558 		 * commands
   1559 		 */
   1560 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
   1561 			return 0;
   1562 		/* add others later */
   1563 		return 1;
   1564 
   1565 	case ADB_HW_PB:
   1566 		return 1;
   1567 
   1568 	case ADB_HW_UNKNOWN:
   1569 	default:
   1570 		return 1;
   1571 	}
   1572 }
   1573 
   1574 
   1575 /*
   1576  * adb_op_sync
   1577  *
   1578  * This routine does exactly what the adb_op routine does, except that after
   1579  * the adb_op is called, it waits until the return value is present before
   1580  * returning.
   1581  *
   1582  * NOTE: The user specified compRout is ignored, since this routine specifies
   1583  * it's own to adb_op, which is why you really called this in the first place
   1584  * anyway.
   1585  */
   1586 int
   1587 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
   1588 {
   1589 	int result;
   1590 	volatile int flag = 0;
   1591 
   1592 	result = adb_op(buffer, (void *)adb_op_comprout,
   1593 	    (void *)&flag, command);	/* send command */
   1594 	if (result == 0)		/* send ok? */
   1595 		while (0 == flag)
   1596 			/* wait for compl. routine */;
   1597 
   1598 	return result;
   1599 }
   1600 
   1601 
   1602 /*
   1603  * adb_op_comprout
   1604  *
   1605  * This function is used by the adb_op_sync routine so it knows when the
   1606  * function is done.
   1607  */
   1608 void
   1609 adb_op_comprout(buffer, compdata, cmd)
   1610 	caddr_t buffer, compdata;
   1611 	int cmd;
   1612 {
   1613 	short *p = (short *)compdata;
   1614 
   1615 	*p = 1;
   1616 }
   1617 
   1618 void
   1619 adb_setup_hw_type(void)
   1620 {
   1621 	switch (adbHardware) {
   1622 	case ADB_HW_CUDA:
   1623 		adbSoftPower = 1;
   1624 		return;
   1625 
   1626 	case ADB_HW_PB:
   1627 		pm_setup_adb();
   1628 		return;
   1629 
   1630 	default:
   1631 		panic("unknown adb hardware");
   1632 	}
   1633 #if 0
   1634 	response = 0; /*mac68k_machine.machineid;*/
   1635 
   1636 	/*
   1637 	 * Determine what type of ADB hardware we are running on.
   1638 	 */
   1639 	switch (response) {
   1640 	case MACH_MACC610:		/* Centris 610 */
   1641 	case MACH_MACC650:		/* Centris 650 */
   1642 	case MACH_MACII:		/* II */
   1643 	case MACH_MACIICI:		/* IIci */
   1644 	case MACH_MACIICX:		/* IIcx */
   1645 	case MACH_MACIIX:		/* IIx */
   1646 	case MACH_MACQ610:		/* Quadra 610 */
   1647 	case MACH_MACQ650:		/* Quadra 650 */
   1648 	case MACH_MACQ700:		/* Quadra 700 */
   1649 	case MACH_MACQ800:		/* Quadra 800 */
   1650 	case MACH_MACSE30:		/* SE/30 */
   1651 		adbHardware = ADB_HW_II;
   1652 #ifdef ADB_DEBUG
   1653 		if (adb_debug)
   1654 			printf_intr("adb: using II series hardware support\n");
   1655 #endif
   1656 		break;
   1657 
   1658 	case MACH_MACCLASSICII:		/* Classic II */
   1659 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
   1660 	case MACH_MACLCIII:		/* LC III, Performa 450 */
   1661 	case MACH_MACIISI:		/* IIsi */
   1662 	case MACH_MACIIVI:		/* IIvi */
   1663 	case MACH_MACIIVX:		/* IIvx */
   1664 	case MACH_MACP460:		/* Performa 460/465/467 */
   1665 	case MACH_MACP600:		/* Performa 600 */
   1666 	case MACH_MACQ900:		/* Quadra 900 -  XXX not sure */
   1667 	case MACH_MACQ950:		/* Quadra 950 -  XXX not sure */
   1668 		adbHardware = ADB_HW_IISI;
   1669 #ifdef ADB_DEBUG
   1670 		if (adb_debug)
   1671 			printf_intr("adb: using IIsi series hardware support\n");
   1672 #endif
   1673 		break;
   1674 
   1675 	case MACH_MACPB140:		/* PowerBook 140 */
   1676 	case MACH_MACPB145:		/* PowerBook 145 */
   1677 	case MACH_MACPB150:		/* PowerBook 150 */
   1678 	case MACH_MACPB160:		/* PowerBook 160 */
   1679 	case MACH_MACPB165:		/* PowerBook 165 */
   1680 	case MACH_MACPB165C:		/* PowerBook 165c */
   1681 	case MACH_MACPB170:		/* PowerBook 170 */
   1682 	case MACH_MACPB180:		/* PowerBook 180 */
   1683 	case MACH_MACPB180C:		/* PowerBook 180c */
   1684 		adbHardware = ADB_HW_PB;
   1685 		pm_setup_adb();
   1686 #ifdef ADB_DEBUG
   1687 		if (adb_debug)
   1688 			printf_intr("adb: using PowerBook 100-series hardware support\n");
   1689 #endif
   1690 		break;
   1691 
   1692 	case MACH_MACPB210:		/* PowerBook Duo 210 */
   1693 	case MACH_MACPB230:		/* PowerBook Duo 230 */
   1694 	case MACH_MACPB250:		/* PowerBook Duo 250 */
   1695 	case MACH_MACPB270:		/* PowerBook Duo 270 */
   1696 	case MACH_MACPB280:		/* PowerBook Duo 280 */
   1697 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
   1698 	case MACH_MACPB500:		/* PowerBook 500 series */
   1699 		adbHardware = ADB_HW_PB;
   1700 		pm_setup_adb();
   1701 #ifdef ADB_DEBUG
   1702 		if (adb_debug)
   1703 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
   1704 #endif
   1705 		break;
   1706 
   1707 	case MACH_MACC660AV:		/* Centris 660AV */
   1708 	case MACH_MACCCLASSIC:		/* Color Classic */
   1709 	case MACH_MACCCLASSICII:	/* Color Classic II */
   1710 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
   1711 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
   1712 	case MACH_MACLC520:		/* LC 520 */
   1713 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
   1714 	case MACH_MACP550:		/* LC 550, Performa 550 */
   1715 	case MACH_MACP580:		/* Performa 580/588 */
   1716 	case MACH_MACQ605:		/* Quadra 605 */
   1717 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
   1718 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
   1719 	case MACH_MACQ840AV:		/* Quadra 840AV */
   1720 		adbHardware = ADB_HW_CUDA;
   1721 #ifdef ADB_DEBUG
   1722 		if (adb_debug)
   1723 			printf_intr("adb: using Cuda series hardware support\n");
   1724 #endif
   1725 		break;
   1726 	default:
   1727 		adbHardware = ADB_HW_UNKNOWN;
   1728 #ifdef ADB_DEBUG
   1729 		if (adb_debug) {
   1730 			printf_intr("adb: hardware type unknown for this machine\n");
   1731 			printf_intr("adb: ADB support is disabled\n");
   1732 		}
   1733 #endif
   1734 		break;
   1735 	}
   1736 
   1737 	/*
   1738 	 * Determine whether this machine has ADB based soft power.
   1739 	 */
   1740 	switch (response) {
   1741 	case MACH_MACCCLASSIC:		/* Color Classic */
   1742 	case MACH_MACCCLASSICII:	/* Color Classic II */
   1743 	case MACH_MACIISI:		/* IIsi */
   1744 	case MACH_MACIIVI:		/* IIvi */
   1745 	case MACH_MACIIVX:		/* IIvx */
   1746 	case MACH_MACLC520:		/* LC 520 */
   1747 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
   1748 	case MACH_MACP550:		/* LC 550, Performa 550 */
   1749 	case MACH_MACP600:		/* Performa 600 */
   1750 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
   1751 	case MACH_MACQ840AV:		/* Quadra 840AV */
   1752 	case MACH_MACQ900:		/* Quadra 900 -  XXX not sure */
   1753 	case MACH_MACQ950:		/* Quadra 950 -  XXX not sure */
   1754 		adbSoftPower = 1;
   1755 		break;
   1756 	}
   1757 #endif
   1758 }
   1759 
   1760 int
   1761 count_adbs(void)
   1762 {
   1763 	int i;
   1764 	int found;
   1765 
   1766 	found = 0;
   1767 
   1768 	for (i = 1; i < 16; i++)
   1769 		if (0 != ADBDevTable[i].devType)
   1770 			found++;
   1771 
   1772 	return found;
   1773 }
   1774 
   1775 int
   1776 get_ind_adb_info(ADBDataBlock * info, int index)
   1777 {
   1778 	if ((index < 1) || (index > 15))	/* check range 1-15 */
   1779 		return (-1);
   1780 
   1781 #ifdef ADB_DEBUG
   1782 	if (adb_debug & 0x80)
   1783 		printf_intr("index 0x%x devType is: 0x%x\n", index,
   1784 		    ADBDevTable[index].devType);
   1785 #endif
   1786 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
   1787 		return (-1);
   1788 
   1789 	info->devType = ADBDevTable[index].devType;
   1790 	info->origADBAddr = ADBDevTable[index].origAddr;
   1791 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
   1792 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
   1793 
   1794 	return (ADBDevTable[index].currentAddr);
   1795 }
   1796 
   1797 int
   1798 get_adb_info(ADBDataBlock * info, int adbAddr)
   1799 {
   1800 	int i;
   1801 
   1802 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1803 		return (-1);
   1804 
   1805 	for (i = 1; i < 15; i++)
   1806 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1807 			info->devType = ADBDevTable[i].devType;
   1808 			info->origADBAddr = ADBDevTable[i].origAddr;
   1809 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
   1810 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
   1811 			return 0;	/* found */
   1812 		}
   1813 
   1814 	return (-1);		/* not found */
   1815 }
   1816 
   1817 int
   1818 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
   1819 {
   1820 	int i;
   1821 
   1822 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1823 		return (-1);
   1824 
   1825 	for (i = 1; i < 15; i++)
   1826 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1827 			ADBDevTable[i].ServiceRtPtr =
   1828 			    (void *)(info->siServiceRtPtr);
   1829 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
   1830 			return 0;	/* found */
   1831 		}
   1832 
   1833 	return (-1);		/* not found */
   1834 
   1835 }
   1836 
   1837 #ifndef MRG_ADB
   1838 
   1839 /* caller should really use machine-independant version: getPramTime */
   1840 /* this version does pseudo-adb access only */
   1841 int
   1842 adb_read_date_time(unsigned long *time)
   1843 {
   1844 	u_char output[ADB_MAX_MSG_LENGTH];
   1845 	int result;
   1846 	volatile int flag = 0;
   1847 
   1848 	switch (adbHardware) {
   1849 	case ADB_HW_II:
   1850 		return -1;
   1851 
   1852 	case ADB_HW_IISI:
   1853 		output[0] = 0x02;	/* 2 byte message */
   1854 		output[1] = 0x01;	/* to pram/rtc device */
   1855 		output[2] = 0x03;	/* read date/time */
   1856 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
   1857 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
   1858 		if (result != 0)	/* exit if not sent */
   1859 			return -1;
   1860 
   1861 		while (0 == flag)	/* wait for result */
   1862 			;
   1863 
   1864 		*time = (long)(*(long *)(output + 1));
   1865 		return 0;
   1866 
   1867 	case ADB_HW_PB:
   1868 		pm_read_date_time(time);
   1869 		return 0;
   1870 
   1871 	case ADB_HW_CUDA:
   1872 		output[0] = 0x02;	/* 2 byte message */
   1873 		output[1] = 0x01;	/* to pram/rtc device */
   1874 		output[2] = 0x03;	/* read date/time */
   1875 		result = send_adb_cuda((u_char *)output, (u_char *)output,
   1876 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1877 		if (result != 0)	/* exit if not sent */
   1878 			return -1;
   1879 
   1880 		while (0 == flag)	/* wait for result */
   1881 			;
   1882 
   1883 		memcpy(time, output + 1, 4);
   1884 		return 0;
   1885 
   1886 	case ADB_HW_UNKNOWN:
   1887 	default:
   1888 		return -1;
   1889 	}
   1890 }
   1891 
   1892 /* caller should really use machine-independant version: setPramTime */
   1893 /* this version does pseudo-adb access only */
   1894 int
   1895 adb_set_date_time(unsigned long time)
   1896 {
   1897 	u_char output[ADB_MAX_MSG_LENGTH];
   1898 	int result;
   1899 	volatile int flag = 0;
   1900 
   1901 	switch (adbHardware) {
   1902 
   1903 	case ADB_HW_CUDA:
   1904 		output[0] = 0x06;	/* 6 byte message */
   1905 		output[1] = 0x01;	/* to pram/rtc device */
   1906 		output[2] = 0x09;	/* set date/time */
   1907 		output[3] = (u_char)(time >> 24);
   1908 		output[4] = (u_char)(time >> 16);
   1909 		output[5] = (u_char)(time >> 8);
   1910 		output[6] = (u_char)(time);
   1911 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1912 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1913 		if (result != 0)	/* exit if not sent */
   1914 			return -1;
   1915 
   1916 		while (0 == flag)	/* wait for send to finish */
   1917 			;
   1918 
   1919 		return 0;
   1920 
   1921 	case ADB_HW_PB:
   1922 		pm_set_date_time(time);
   1923 		return 0;
   1924 
   1925 	case ADB_HW_II:
   1926 	case ADB_HW_IISI:
   1927 	case ADB_HW_UNKNOWN:
   1928 	default:
   1929 		return -1;
   1930 	}
   1931 }
   1932 
   1933 
   1934 int
   1935 adb_poweroff(void)
   1936 {
   1937 	u_char output[ADB_MAX_MSG_LENGTH];
   1938 	int result;
   1939 
   1940 	if (!adbSoftPower)
   1941 		return -1;
   1942 
   1943 	adb_polling = 1;
   1944 
   1945 	switch (adbHardware) {
   1946 	case ADB_HW_IISI:
   1947 		output[0] = 0x02;	/* 2 byte message */
   1948 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1949 		output[2] = 0x0a;	/* set date/time */
   1950 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
   1951 		    (void *)0, (void *)0, (int)0);
   1952 		if (result != 0)	/* exit if not sent */
   1953 			return -1;
   1954 
   1955 		for (;;);		/* wait for power off */
   1956 
   1957 		return 0;
   1958 
   1959 	case ADB_HW_PB:
   1960 		return -1;
   1961 
   1962 	case ADB_HW_CUDA:
   1963 		output[0] = 0x02;	/* 2 byte message */
   1964 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1965 		output[2] = 0x0a;	/* set date/time */
   1966 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1967 		    (void *)0, (void *)0, (int)0);
   1968 		if (result != 0)	/* exit if not sent */
   1969 			return -1;
   1970 
   1971 		for (;;);		/* wait for power off */
   1972 
   1973 		return 0;
   1974 
   1975 	case ADB_HW_II:			/* II models don't do ADB soft power */
   1976 	case ADB_HW_UNKNOWN:
   1977 	default:
   1978 		return -1;
   1979 	}
   1980 }
   1981 
   1982 int
   1983 adb_prog_switch_enable(void)
   1984 {
   1985 	u_char output[ADB_MAX_MSG_LENGTH];
   1986 	int result;
   1987 	volatile int flag = 0;
   1988 
   1989 	switch (adbHardware) {
   1990 	case ADB_HW_IISI:
   1991 		output[0] = 0x03;	/* 3 byte message */
   1992 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1993 		output[2] = 0x1c;	/* prog. switch control */
   1994 		output[3] = 0x01;	/* enable */
   1995 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
   1996 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1997 		if (result != 0)	/* exit if not sent */
   1998 			return -1;
   1999 
   2000 		while (0 == flag)	/* wait for send to finish */
   2001 			;
   2002 
   2003 		return 0;
   2004 
   2005 	case ADB_HW_PB:
   2006 		return -1;
   2007 
   2008 	case ADB_HW_II:		/* II models don't do prog. switch */
   2009 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
   2010 	case ADB_HW_UNKNOWN:
   2011 	default:
   2012 		return -1;
   2013 	}
   2014 }
   2015 
   2016 int
   2017 adb_prog_switch_disable(void)
   2018 {
   2019 	u_char output[ADB_MAX_MSG_LENGTH];
   2020 	int result;
   2021 	volatile int flag = 0;
   2022 
   2023 	switch (adbHardware) {
   2024 	case ADB_HW_IISI:
   2025 		output[0] = 0x03;	/* 3 byte message */
   2026 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2027 		output[2] = 0x1c;	/* prog. switch control */
   2028 		output[3] = 0x01;	/* disable */
   2029 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
   2030 			(void *)adb_op_comprout, (void *)&flag, (int)0);
   2031 		if (result != 0)	/* exit if not sent */
   2032 			return -1;
   2033 
   2034 		while (0 == flag)	/* wait for send to finish */
   2035 			;
   2036 
   2037 		return 0;
   2038 
   2039 	case ADB_HW_PB:
   2040 		return -1;
   2041 
   2042 	case ADB_HW_II:		/* II models don't do prog. switch */
   2043 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
   2044 	case ADB_HW_UNKNOWN:
   2045 	default:
   2046 		return -1;
   2047 	}
   2048 }
   2049 
   2050 int
   2051 CountADBs(void)
   2052 {
   2053 	return (count_adbs());
   2054 }
   2055 
   2056 void
   2057 ADBReInit(void)
   2058 {
   2059 	adb_reinit();
   2060 }
   2061 
   2062 int
   2063 GetIndADB(ADBDataBlock * info, int index)
   2064 {
   2065 	return (get_ind_adb_info(info, index));
   2066 }
   2067 
   2068 int
   2069 GetADBInfo(ADBDataBlock * info, int adbAddr)
   2070 {
   2071 	return (get_adb_info(info, adbAddr));
   2072 }
   2073 
   2074 int
   2075 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
   2076 {
   2077 	return (set_adb_info(info, adbAddr));
   2078 }
   2079 
   2080 int
   2081 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
   2082 {
   2083 	return (adb_op(buffer, compRout, data, commandNum));
   2084 }
   2085 
   2086 #endif
   2087 
   2088 int
   2089 setsoftadb()
   2090 {
   2091 	timeout((void *)adb_soft_intr, NULL, 1);
   2092 	return 0;
   2093 }
   2094 
   2095 void
   2096 adb_cuda_autopoll()
   2097 {
   2098 	volatile int flag = 0;
   2099 	int result;
   2100 	u_char output[16];
   2101 	extern void adb_op_comprout();
   2102 
   2103 	output[0] = 0x03;	/* 3-byte message */
   2104 	output[1] = 0x01;	/* to pram/rtc device */
   2105 	output[2] = 0x01;	/* cuda autopoll */
   2106 	output[3] = 0x01;
   2107 	result = send_adb_cuda(output, output, adb_op_comprout,
   2108 		(void *)&flag, 0);
   2109 	if (result != 0)	/* exit if not sent */
   2110 		return;
   2111 
   2112 	while (flag == 0);	/* wait for result */
   2113 }
   2114 
   2115 void
   2116 adb_restart()
   2117 {
   2118 	volatile int flag = 0;
   2119 	int result;
   2120 	u_char output[16];
   2121 
   2122 	adb_polling = 1;
   2123 
   2124 	switch (adbHardware) {
   2125 	case ADB_HW_CUDA:
   2126 		output[0] = 0x02;	/* 2 byte message */
   2127 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2128 		output[2] = 0x11;	/* restart */
   2129 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   2130 				       (void *)0, (void *)0, (int)0);
   2131 		if (result != 0)	/* exit if not sent */
   2132 			return;
   2133 		while (1);		/* not return */
   2134 
   2135 	case ADB_HW_PB:
   2136 		pm_adb_restart();
   2137 		while (1);		/* not return */
   2138 	}
   2139 }
   2140