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