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