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