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