Home | History | Annotate | Line # | Download | only in dev
adb_direct.c revision 1.31
      1 /*	$NetBSD: adb_direct.c,v 1.31 2005/06/05 18:49:49 nathanw 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/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: adb_direct.c,v 1.31 2005/06/05 18:49:49 nathanw Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/cdefs.h>
     67 #include <sys/systm.h>
     68 #include <sys/callout.h>
     69 #include <sys/device.h>
     70 
     71 #include <machine/param.h>
     72 #include <machine/cpu.h>
     73 #include <machine/adbsys.h>
     74 
     75 #include <macppc/dev/viareg.h>
     76 #include <macppc/dev/adbvar.h>
     77 #include <macppc/dev/pm_direct.h>
     78 
     79 #define printf_intr printf
     80 
     81 #ifdef DEBUG
     82 #ifndef ADB_DEBUG
     83 #define ADB_DEBUG
     84 #endif
     85 #endif
     86 
     87 /* some misc. leftovers */
     88 #define vPB		0x0000
     89 #define vPB3		0x08
     90 #define vPB4		0x10
     91 #define vPB5		0x20
     92 #define vSR_INT		0x04
     93 #define vSR_OUT		0x10
     94 
     95 /* the type of ADB action that we are currently preforming */
     96 #define ADB_ACTION_NOTREADY	0x1	/* has not been initialized yet */
     97 #define ADB_ACTION_IDLE		0x2	/* the bus is currently idle */
     98 #define ADB_ACTION_OUT		0x3	/* sending out a command */
     99 #define ADB_ACTION_IN		0x4	/* receiving data */
    100 #define ADB_ACTION_POLLING	0x5	/* polling - II only */
    101 
    102 /*
    103  * These describe the state of the ADB bus itself, although they
    104  * don't necessarily correspond directly to ADB states.
    105  * Note: these are not really used in the IIsi code.
    106  */
    107 #define ADB_BUS_UNKNOWN		0x1	/* we don't know yet - all models */
    108 #define ADB_BUS_IDLE		0x2	/* bus is idle - all models */
    109 #define ADB_BUS_CMD		0x3	/* starting a command - II models */
    110 #define ADB_BUS_ODD		0x4	/* the "odd" state - II models */
    111 #define ADB_BUS_EVEN		0x5	/* the "even" state - II models */
    112 #define ADB_BUS_ACTIVE		0x6	/* active state - IIsi models */
    113 #define ADB_BUS_ACK		0x7	/* currently ACKing - IIsi models */
    114 
    115 /*
    116  * Shortcuts for setting or testing the VIA bit states.
    117  * Not all shortcuts are used for every type of ADB hardware.
    118  */
    119 #define ADB_SET_STATE_IDLE_CUDA()   via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
    120 #define ADB_SET_STATE_TIP()	    via_reg_and(VIA1, vBufB, ~vPB5)
    121 #define ADB_CLR_STATE_TIP() 	    via_reg_or(VIA1, vBufB, vPB5)
    122 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
    123 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
    124 #define ADB_SET_SR_INPUT()	    via_reg_and(VIA1, vACR, ~vSR_OUT)
    125 #define ADB_SET_SR_OUTPUT()	    via_reg_or(VIA1, vACR, vSR_OUT)
    126 #define ADB_SR()		    read_via_reg(VIA1, vSR)
    127 #define ADB_VIA_INTR_ENABLE()	    write_via_reg(VIA1, vIER, 0x84)
    128 #define ADB_VIA_INTR_DISABLE()	    write_via_reg(VIA1, vIER, 0x04)
    129 #define ADB_INTR_IS_OFF		   (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
    130 #define ADB_INTR_IS_ON		   (0 == (read_via_reg(VIA1, vBufB) & vPB3))
    131 #define ADB_SR_INTR_IS_OFF	   (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
    132 #define ADB_SR_INTR_IS_ON	   (vSR_INT == (read_via_reg(VIA1, \
    133 						vIFR) & vSR_INT))
    134 
    135 /*
    136  * This is the delay that is required (in uS) between certain
    137  * ADB transactions. The actual timing delay for for each uS is
    138  * calculated at boot time to account for differences in machine speed.
    139  */
    140 #define ADB_DELAY	150
    141 
    142 /*
    143  * Maximum ADB message length; includes space for data, result, and
    144  * device code - plus a little for safety.
    145  */
    146 #define ADB_MAX_MSG_LENGTH	16
    147 #define ADB_MAX_HDR_LENGTH	8
    148 
    149 #define ADB_QUEUE		32
    150 #define ADB_TICKLE_TICKS	4
    151 
    152 /*
    153  * A structure for storing information about each ADB device.
    154  */
    155 struct ADBDevEntry {
    156 	void	(*ServiceRtPtr) __P((void));
    157 	void	*DataAreaAddr;
    158 	int	devType;
    159 	int	origAddr;
    160 	int	currentAddr;
    161 };
    162 
    163 /*
    164  * Used to hold ADB commands that are waiting to be sent out.
    165  */
    166 struct adbCmdHoldEntry {
    167 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
    168 	u_char	*saveBuf;	/* buffer to know where to save result */
    169 	u_char	*compRout;	/* completion routine pointer */
    170 	u_char	*data;		/* completion routine data pointer */
    171 };
    172 
    173 /*
    174  * Eventually used for two separate queues, the queue between
    175  * the upper and lower halves, and the outgoing packet queue.
    176  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
    177  */
    178 struct adbCommand {
    179 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
    180 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
    181 	u_char	*saveBuf;	/* where to save result */
    182 	u_char	*compRout;	/* completion routine pointer */
    183 	u_char	*compData;	/* completion routine data pointer */
    184 	u_int	cmd;		/* the original command for this data */
    185 	u_int	unsol;		/* 1 if packet was unsolicited */
    186 	u_int	ack_only;	/* 1 for no special processing */
    187 };
    188 
    189 /*
    190  * A few variables that we need and their initial values.
    191  */
    192 int	adbHardware = ADB_HW_UNKNOWN;
    193 int	adbActionState = ADB_ACTION_NOTREADY;
    194 int	adbWaiting = 0;		/* waiting for return data from the device */
    195 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
    196 
    197 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
    198 u_char	*adbBuffer = (long)0;	/* pointer to user data area */
    199 void	*adbCompRout = (long)0;	/* pointer to the completion routine */
    200 void	*adbCompData = (long)0;	/* pointer to the completion routine data */
    201 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
    202 
    203 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
    204 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
    205 
    206 int	adbSentChars = 0;	/* how many characters we have sent */
    207 
    208 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
    209 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
    210 
    211 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
    212 int	adbInCount = 0;			/* how many packets in in queue */
    213 int	adbInHead = 0;			/* head of in queue */
    214 int	adbInTail = 0;			/* tail of in queue */
    215 struct	adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
    216 int	adbOutCount = 0;		/* how many packets in out queue */
    217 int	adbOutHead = 0;			/* head of out queue */
    218 int	adbOutTail = 0;			/* tail of out queue */
    219 
    220 int	tickle_count = 0;		/* how many tickles seen for this packet? */
    221 int	tickle_serial = 0;		/* the last packet tickled */
    222 int	adb_cuda_serial = 0;		/* the current packet */
    223 
    224 struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
    225 struct callout adb_soft_intr_ch = CALLOUT_INITIALIZER;
    226 
    227 volatile u_char *Via1Base;
    228 extern int adb_polling;			/* Are we polling? */
    229 
    230 void	pm_setup_adb __P((void));
    231 void	pm_check_adb_devices __P((int));
    232 int	pm_adb_op __P((u_char *, void *, void *, int));
    233 void	pm_init_adb_device __P((void));
    234 
    235 /*
    236  * The following are private routines.
    237  */
    238 #ifdef ADB_DEBUG
    239 void	print_single __P((u_char *));
    240 #endif
    241 void	adb_soft_intr __P((void));
    242 int	send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
    243 void	adb_intr_cuda_test __P((void));
    244 void	adb_cuda_tickle __P((void));
    245 void	adb_pass_up __P((struct adbCommand *));
    246 void	adb_op_comprout __P((caddr_t, caddr_t, int));
    247 void	adb_reinit __P((void));
    248 int	count_adbs __P((void));
    249 int	get_ind_adb_info __P((ADBDataBlock *, int));
    250 int	get_adb_info __P((ADBDataBlock *, int));
    251 int	set_adb_info __P((ADBSetInfoBlock *, int));
    252 void	adb_setup_hw_type __P((void));
    253 int	adb_op __P((Ptr, Ptr, Ptr, short));
    254 int	adb_op_sync __P((Ptr, Ptr, Ptr, short));
    255 void	adb_hw_setup __P((void));
    256 int	adb_cmd_result __P((u_char *));
    257 int	adb_cmd_extra __P((u_char *));
    258 /* we should create this and it will be the public version */
    259 int	send_adb __P((u_char *, void *, void *));
    260 
    261 int	setsoftadb __P((void));
    262 
    263 #ifdef ADB_DEBUG
    264 /*
    265  * print_single
    266  * Diagnostic display routine. Displays the hex values of the
    267  * specified elements of the u_char. The length of the "string"
    268  * is in [0].
    269  */
    270 void
    271 print_single(str)
    272 	u_char *str;
    273 {
    274 	int x;
    275 
    276 	if (str == 0) {
    277 		printf_intr("no data - null pointer\n");
    278 		return;
    279 	}
    280 	if (*str == 0) {
    281 		printf_intr("nothing returned\n");
    282 		return;
    283 	}
    284 	if (*str > 20) {
    285 		printf_intr("ADB: ACK > 20 no way!\n");
    286 		*str = 20;
    287 	}
    288 	printf_intr("(length=0x%x):", *str);
    289 	for (x = 1; x <= *str; x++)
    290 		printf_intr("  0x%02x", str[x]);
    291 	printf_intr("\n");
    292 }
    293 #endif
    294 
    295 void
    296 adb_cuda_tickle(void)
    297 {
    298 	volatile int s;
    299 
    300 	if (adbActionState == ADB_ACTION_IN) {
    301 		if (tickle_serial == adb_cuda_serial) {
    302 			if (++tickle_count > 0) {
    303 				s = splhigh();
    304 				adbActionState = ADB_ACTION_IDLE;
    305 				adbInputBuffer[0] = 0;
    306 				ADB_SET_STATE_IDLE_CUDA();
    307 				splx(s);
    308 			}
    309 		} else {
    310 			tickle_serial = adb_cuda_serial;
    311 			tickle_count = 0;
    312 		}
    313 	} else {
    314 		tickle_serial = adb_cuda_serial;
    315 		tickle_count = 0;
    316 	}
    317 
    318 	callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
    319 	    (void *)adb_cuda_tickle, NULL);
    320 }
    321 
    322 /*
    323  * called when when an adb interrupt happens
    324  *
    325  * Cuda version of adb_intr
    326  * TO DO: do we want to add some calls to intr_dispatch() here to
    327  * grab serial interrupts?
    328  */
    329 int
    330 adb_intr_cuda(void *arg)
    331 {
    332 	volatile int i, ending;
    333 	volatile unsigned int s;
    334 	struct adbCommand packet;
    335 	uint8_t reg;
    336 
    337 	s = splhigh();		/* can't be too careful - might be called */
    338 				/* from a routine, NOT an interrupt */
    339 
    340 	reg = read_via_reg(VIA1, vIFR);		/* Read the interrupts */
    341 	if ((reg & 0x80) == 0) {
    342 		splx(s);
    343 		return 0;			/* No interrupts to process */
    344 	}
    345 
    346 	write_via_reg(VIA1, vIFR, reg & 0x7f);	/* Clear 'em */
    347 
    348 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
    349 
    350 switch_start:
    351 	switch (adbActionState) {
    352 	case ADB_ACTION_IDLE:
    353 		/*
    354 		 * This is an unexpected packet, so grab the first (dummy)
    355 		 * byte, set up the proper vars, and tell the chip we are
    356 		 * starting to receive the packet by setting the TIP bit.
    357 		 */
    358 		adbInputBuffer[1] = ADB_SR();
    359 		adb_cuda_serial++;
    360 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
    361 			break;
    362 
    363 		ADB_SET_SR_INPUT();
    364 		ADB_SET_STATE_TIP();
    365 
    366 		adbInputBuffer[0] = 1;
    367 		adbActionState = ADB_ACTION_IN;
    368 #ifdef ADB_DEBUG
    369 		if (adb_debug)
    370 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
    371 #endif
    372 		break;
    373 
    374 	case ADB_ACTION_IN:
    375 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
    376 		/* intr off means this is the last byte (end of frame) */
    377 		if (ADB_INTR_IS_OFF)
    378 			ending = 1;
    379 		else
    380 			ending = 0;
    381 
    382 		if (1 == ending) {	/* end of message? */
    383 #ifdef ADB_DEBUG
    384 			if (adb_debug) {
    385 				printf_intr("in end 0x%02x ",
    386 				    adbInputBuffer[adbInputBuffer[0]]);
    387 				print_single(adbInputBuffer);
    388 			}
    389 #endif
    390 
    391 			/*
    392 			 * Are we waiting AND does this packet match what we
    393 			 * are waiting for AND is it coming from either the
    394 			 * ADB or RTC/PRAM sub-device? This section _should_
    395 			 * recognize all ADB and RTC/PRAM type commands, but
    396 			 * there may be more... NOTE: commands are always at
    397 			 * [4], even for RTC/PRAM commands.
    398 			 */
    399 			/* set up data for adb_pass_up */
    400 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
    401 
    402 			if ((adbWaiting == 1) &&
    403 			    (adbInputBuffer[4] == adbWaitingCmd) &&
    404 			    ((adbInputBuffer[2] == 0x00) ||
    405 			    (adbInputBuffer[2] == 0x01))) {
    406 				packet.saveBuf = adbBuffer;
    407 				packet.compRout = adbCompRout;
    408 				packet.compData = adbCompData;
    409 				packet.unsol = 0;
    410 				packet.ack_only = 0;
    411 				adb_pass_up(&packet);
    412 
    413 				adbWaitingCmd = 0;	/* reset "waiting" vars */
    414 				adbWaiting = 0;
    415 				adbBuffer = (long)0;
    416 				adbCompRout = (long)0;
    417 				adbCompData = (long)0;
    418 			} else {
    419 				packet.unsol = 1;
    420 				packet.ack_only = 0;
    421 				adb_pass_up(&packet);
    422 			}
    423 
    424 
    425 			/* reset vars and signal the end of this frame */
    426 			adbActionState = ADB_ACTION_IDLE;
    427 			adbInputBuffer[0] = 0;
    428 			ADB_SET_STATE_IDLE_CUDA();
    429 			/*ADB_SET_SR_INPUT();*/
    430 
    431 			/*
    432 			 * If there is something waiting to be sent out,
    433 			 * the set everything up and send the first byte.
    434 			 */
    435 			if (adbWriteDelay == 1) {
    436 				delay(ADB_DELAY);	/* required */
    437 				adbSentChars = 0;
    438 				adbActionState = ADB_ACTION_OUT;
    439 				/*
    440 				 * If the interrupt is on, we were too slow
    441 				 * and the chip has already started to send
    442 				 * something to us, so back out of the write
    443 				 * and start a read cycle.
    444 				 */
    445 				if (ADB_INTR_IS_ON) {
    446 					ADB_SET_SR_INPUT();
    447 					ADB_SET_STATE_IDLE_CUDA();
    448 					adbSentChars = 0;
    449 					adbActionState = ADB_ACTION_IDLE;
    450 					adbInputBuffer[0] = 0;
    451 					break;
    452 				}
    453 				/*
    454 				 * If we got here, it's ok to start sending
    455 				 * so load the first byte and tell the chip
    456 				 * we want to send.
    457 				 */
    458 				ADB_SET_STATE_TIP();
    459 				ADB_SET_SR_OUTPUT();
    460 				write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
    461 			}
    462 		} else {
    463 			ADB_TOGGLE_STATE_ACK_CUDA();
    464 #ifdef ADB_DEBUG
    465 			if (adb_debug)
    466 				printf_intr("in 0x%02x ",
    467 				    adbInputBuffer[adbInputBuffer[0]]);
    468 #endif
    469 		}
    470 		break;
    471 
    472 	case ADB_ACTION_OUT:
    473 		i = ADB_SR();	/* reset SR-intr in IFR */
    474 #ifdef ADB_DEBUG
    475 		if (adb_debug)
    476 			printf_intr("intr out 0x%02x ", i);
    477 #endif
    478 
    479 		adbSentChars++;
    480 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
    481 #ifdef ADB_DEBUG
    482 			if (adb_debug)
    483 				printf_intr("intr was on ");
    484 #endif
    485 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    486 			ADB_SET_STATE_IDLE_CUDA();
    487 			adbSentChars = 0;	/* must start all over */
    488 			adbActionState = ADB_ACTION_IDLE;	/* new state */
    489 			adbInputBuffer[0] = 0;
    490 			adbWriteDelay = 1;	/* must retry when done with
    491 						 * read */
    492 			delay(ADB_DELAY);
    493 			goto switch_start;	/* process next state right
    494 						 * now */
    495 			break;
    496 		}
    497 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
    498 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
    499 									 * back? */
    500 				adbWaiting = 1;	/* signal waiting for return */
    501 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
    502 			} else {	/* no talk, so done */
    503 				/* set up stuff for adb_pass_up */
    504 				memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
    505 				packet.saveBuf = adbBuffer;
    506 				packet.compRout = adbCompRout;
    507 				packet.compData = adbCompData;
    508 				packet.cmd = adbWaitingCmd;
    509 				packet.unsol = 0;
    510 				packet.ack_only = 1;
    511 				adb_pass_up(&packet);
    512 
    513 				/* reset "waiting" vars, just in case */
    514 				adbWaitingCmd = 0;
    515 				adbBuffer = (long)0;
    516 				adbCompRout = (long)0;
    517 				adbCompData = (long)0;
    518 			}
    519 
    520 			adbWriteDelay = 0;	/* done writing */
    521 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
    522 			ADB_SET_SR_INPUT();
    523 			ADB_SET_STATE_IDLE_CUDA();
    524 #ifdef ADB_DEBUG
    525 			if (adb_debug)
    526 				printf_intr("write done ");
    527 #endif
    528 		} else {
    529 			write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* send next byte */
    530 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
    531 							 * shift */
    532 #ifdef ADB_DEBUG
    533 			if (adb_debug)
    534 				printf_intr("toggle ");
    535 #endif
    536 		}
    537 		break;
    538 
    539 	case ADB_ACTION_NOTREADY:
    540 #ifdef ADB_DEBUG
    541 		if (adb_debug)
    542 			printf_intr("adb: not yet initialized\n");
    543 #endif
    544 		break;
    545 
    546 	default:
    547 #ifdef ADB_DEBUG
    548 		if (adb_debug)
    549 			printf_intr("intr: unknown ADB state\n");
    550 #endif
    551 		break;
    552 	}
    553 
    554 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
    555 
    556 	splx(s);		/* restore */
    557 
    558 	return 1;
    559 }				/* end adb_intr_cuda */
    560 
    561 
    562 int
    563 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
    564 	command)
    565 {
    566 	int s, len;
    567 
    568 #ifdef ADB_DEBUG
    569 	if (adb_debug)
    570 		printf_intr("SEND\n");
    571 #endif
    572 
    573 	if (adbActionState == ADB_ACTION_NOTREADY)
    574 		return 1;
    575 
    576 	/* Don't interrupt while we are messing with the ADB */
    577 	s = splhigh();
    578 
    579 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
    580 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupt? */
    581 	} else
    582 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
    583 			adbWriteDelay = 1;	/* if no, then we'll "queue"
    584 						 * it up */
    585 		else {
    586 			splx(s);
    587 			return 1;	/* really busy! */
    588 		}
    589 
    590 #ifdef ADB_DEBUG
    591 	if (adb_debug)
    592 		printf_intr("QUEUE\n");
    593 #endif
    594 	if ((long)in == (long)0) {	/* need to convert? */
    595 		/*
    596 		 * Don't need to use adb_cmd_extra here because this section
    597 		 * will be called ONLY when it is an ADB command (no RTC or
    598 		 * PRAM)
    599 		 */
    600 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
    601 						 * doing a listen! */
    602 			len = buffer[0];	/* length of additional data */
    603 		else
    604 			len = 0;/* no additional data */
    605 
    606 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
    607 						 * data */
    608 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
    609 		adbOutputBuffer[2] = (u_char)command;	/* load command */
    610 
    611 		/* copy additional output data, if any */
    612 		memcpy(adbOutputBuffer + 3, buffer + 1, len);
    613 	} else
    614 		/* if data ready, just copy over */
    615 		memcpy(adbOutputBuffer, in, in[0] + 2);
    616 
    617 	adbSentChars = 0;	/* nothing sent yet */
    618 	adbBuffer = buffer;	/* save buffer to know where to save result */
    619 	adbCompRout = compRout;	/* save completion routine pointer */
    620 	adbCompData = data;	/* save completion routine data pointer */
    621 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
    622 
    623 	if (adbWriteDelay != 1) {	/* start command now? */
    624 #ifdef ADB_DEBUG
    625 		if (adb_debug)
    626 			printf_intr("out start NOW");
    627 #endif
    628 		delay(ADB_DELAY);
    629 		adbActionState = ADB_ACTION_OUT;	/* set next state */
    630 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
    631 		write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* load byte for output */
    632 		ADB_SET_STATE_ACKOFF_CUDA();
    633 		ADB_SET_STATE_TIP();	/* tell ADB that we want to send */
    634 	}
    635 	adbWriteDelay = 1;	/* something in the write "queue" */
    636 
    637 	splx(s);
    638 
    639 	if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */
    640 		/* poll until byte done */
    641 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
    642 		    || (adbWaiting == 1))
    643 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
    644 				adb_intr_cuda(NULL);	/* process it */
    645 				adb_soft_intr();
    646 			}
    647 
    648 	return 0;
    649 }				/* send_adb_cuda */
    650 
    651 int
    652 adb_intr(void *arg)
    653 {
    654 	switch (adbHardware) {
    655 	case ADB_HW_PMU:
    656 		return pm_intr(arg);
    657 		break;
    658 
    659 	case ADB_HW_CUDA:
    660 		return adb_intr_cuda(arg);
    661 		break;
    662 
    663 	case ADB_HW_UNKNOWN:
    664 		break;
    665 	}
    666 	return 0;
    667 }
    668 
    669 
    670 /*
    671  * adb_pass_up is called by the interrupt-time routines.
    672  * It takes the raw packet data that was received from the
    673  * device and puts it into the queue that the upper half
    674  * processes. It then signals for a soft ADB interrupt which
    675  * will eventually call the upper half routine (adb_soft_intr).
    676  *
    677  * If in->unsol is 0, then this is either the notification
    678  * that the packet was sent (on a LISTEN, for example), or the
    679  * response from the device (on a TALK). The completion routine
    680  * is called only if the user specified one.
    681  *
    682  * If in->unsol is 1, then this packet was unsolicited and
    683  * so we look up the device in the ADB device table to determine
    684  * what it's default service routine is.
    685  *
    686  * If in->ack_only is 1, then we really only need to call
    687  * the completion routine, so don't do any other stuff.
    688  *
    689  * Note that in->data contains the packet header AND data,
    690  * while adbInbound[]->data contains ONLY data.
    691  *
    692  * Note: Called only at interrupt time. Assumes this.
    693  */
    694 void
    695 adb_pass_up(struct adbCommand *in)
    696 {
    697 	int start = 0, len = 0, cmd = 0;
    698 	ADBDataBlock block;
    699 
    700 	/* temp for testing */
    701 	/*u_char *buffer = 0;*/
    702 	/*u_char *compdata = 0;*/
    703 	/*u_char *comprout = 0;*/
    704 
    705 	if (adbInCount >= ADB_QUEUE) {
    706 #ifdef ADB_DEBUG
    707 		if (adb_debug)
    708 			printf_intr("adb: ring buffer overflow\n");
    709 #endif
    710 		return;
    711 	}
    712 
    713 	if (in->ack_only) {
    714 		len = in->data[0];
    715 		cmd = in->cmd;
    716 		start = 0;
    717 	} else {
    718 		switch (adbHardware) {
    719 		case ADB_HW_CUDA:
    720 			/* If it's unsolicited, accept only ADB data for now */
    721 			if (in->unsol)
    722 				if (0 != in->data[2])
    723 					return;
    724 			cmd = in->data[4];
    725 			if (in->data[0] < 5)
    726 				len = 0;
    727 			else
    728 				len = in->data[0]-4;
    729 			start = 4;
    730 			break;
    731 
    732 		case ADB_HW_PMU:
    733 			cmd = in->data[1];
    734 			if (in->data[0] < 2)
    735 				len = 0;
    736 			else
    737 				len = in->data[0]-1;
    738 			start = 1;
    739 			break;
    740 
    741 		case ADB_HW_UNKNOWN:
    742 			return;
    743 		}
    744 
    745 		/* Make sure there is a valid device entry for this device */
    746 		if (in->unsol) {
    747 			/* ignore unsolicited data during adbreinit */
    748 			if (adbStarting)
    749 				return;
    750 			/* get device's comp. routine and data area */
    751 			if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
    752 				return;
    753 		}
    754 	}
    755 
    756 	/*
    757  	 * If this is an unsolicited packet, we need to fill in
    758  	 * some info so adb_soft_intr can process this packet
    759  	 * properly. If it's not unsolicited, then use what
    760  	 * the caller sent us.
    761  	 */
    762 	if (in->unsol) {
    763 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
    764 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
    765 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
    766 	} else {
    767 		adbInbound[adbInTail].compRout = (void *)in->compRout;
    768 		adbInbound[adbInTail].compData = (void *)in->compData;
    769 		adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
    770 	}
    771 
    772 #ifdef ADB_DEBUG
    773 	if (adb_debug && in->data[1] == 2)
    774 		printf_intr("adb: caught error\n");
    775 #endif
    776 
    777 	/* copy the packet data over */
    778 	/*
    779 	 * TO DO: If the *_intr routines fed their incoming data
    780 	 * directly into an adbCommand struct, which is passed to
    781 	 * this routine, then we could eliminate this copy.
    782 	 */
    783 	memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
    784 	adbInbound[adbInTail].data[0] = len;
    785 	adbInbound[adbInTail].cmd = cmd;
    786 
    787 	adbInCount++;
    788 	if (++adbInTail >= ADB_QUEUE)
    789 		adbInTail = 0;
    790 
    791 	/*
    792 	 * If the debugger is running, call upper half manually.
    793 	 * Otherwise, trigger a soft interrupt to handle the rest later.
    794 	 */
    795 	if (adb_polling)
    796 		adb_soft_intr();
    797 	else
    798 		setsoftadb();
    799 
    800 	return;
    801 }
    802 
    803 
    804 /*
    805  * Called to process the packets after they have been
    806  * placed in the incoming queue.
    807  *
    808  */
    809 void
    810 adb_soft_intr(void)
    811 {
    812 	int s;
    813 	int cmd = 0;
    814 	u_char *buffer = 0;
    815 	u_char *comprout = 0;
    816 	u_char *compdata = 0;
    817 
    818 #if 0
    819 	s = splhigh();
    820 	printf_intr("sr: %x\n", (s & 0x0700));
    821 	splx(s);
    822 #endif
    823 
    824 /*delay(2*ADB_DELAY);*/
    825 
    826 	while (adbInCount) {
    827 #ifdef ADB_DEBUG
    828 		if (adb_debug & 0x80)
    829 			printf_intr("%x %x %x ",
    830 			    adbInCount, adbInHead, adbInTail);
    831 #endif
    832 		/* get the data we need from the queue */
    833 		buffer = adbInbound[adbInHead].saveBuf;
    834 		comprout = adbInbound[adbInHead].compRout;
    835 		compdata = adbInbound[adbInHead].compData;
    836 		cmd = adbInbound[adbInHead].cmd;
    837 
    838 		/* copy over data to data area if it's valid */
    839 		/*
    840 		 * Note that for unsol packets we don't want to copy the
    841 		 * data anywhere, so buffer was already set to 0.
    842 		 * For ack_only buffer was set to 0, so don't copy.
    843 		 */
    844 		if (buffer)
    845 			memcpy(buffer, adbInbound[adbInHead].data,
    846 			    adbInbound[adbInHead].data[0] + 1);
    847 
    848 #ifdef ADB_DEBUG
    849 			if (adb_debug & 0x80) {
    850 				printf_intr("%p %p %p %x ",
    851 				    buffer, comprout, compdata, (short)cmd);
    852 				printf_intr("buf: ");
    853 				print_single(adbInbound[adbInHead].data);
    854 			}
    855 #endif
    856 		/* Remove the packet from the queue before calling
    857 		 * the completion routine, so that the completion
    858 		 * routine can reentrantly process the queue.  For
    859 		 * example, this happens when polling is turned on
    860 		 * by entering the debuger by keystroke.
    861 		 */
    862 		s = splhigh();
    863 		adbInCount--;
    864 		if (++adbInHead >= ADB_QUEUE)
    865 			adbInHead = 0;
    866 		splx(s);
    867 
    868 		/* call default completion routine if it's valid */
    869 		if (comprout) {
    870 			void (*f)(caddr_t, caddr_t, int) =
    871 			    (void (*)(caddr_t, caddr_t, int))comprout;
    872 
    873 			(*f)(buffer, compdata, cmd);
    874 		}
    875 	}
    876 	return;
    877 }
    878 
    879 
    880 /*
    881  * This is my version of the ADBOp routine. It mainly just calls the
    882  * hardware-specific routine.
    883  *
    884  *   data 	: pointer to data area to be used by compRout
    885  *   compRout	: completion routine
    886  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
    887  *		  byte 0 = # of bytes
    888  *		: for TALK: points to place to save return data
    889  *   command	: the adb command to send
    890  *   result	: 0 = success
    891  *		: -1 = could not complete
    892  */
    893 int
    894 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
    895 {
    896 	int result;
    897 
    898 	switch (adbHardware) {
    899 	case ADB_HW_PMU:
    900 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
    901 		    (void *)data, (int)command);
    902 
    903 		if (result == 0)
    904 			return 0;
    905 		else
    906 			return -1;
    907 		break;
    908 
    909 	case ADB_HW_CUDA:
    910 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
    911 		    (void *)compRout, (void *)data, (int)command);
    912 		if (result == 0)
    913 			return 0;
    914 		else
    915 			return -1;
    916 		break;
    917 
    918 	case ADB_HW_UNKNOWN:
    919 	default:
    920 		return -1;
    921 	}
    922 }
    923 
    924 
    925 /*
    926  * adb_hw_setup
    927  * This routine sets up the possible machine specific hardware
    928  * config (mainly VIA settings) for the various models.
    929  */
    930 void
    931 adb_hw_setup(void)
    932 {
    933 	volatile int i;
    934 
    935 	switch (adbHardware) {
    936 	case ADB_HW_PMU:
    937 		/*
    938 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
    939 		 * pm_direct.h?
    940 		 */
    941 		write_via_reg(VIA1, vIFR, 0x90);	/* clear interrupt */
    942 		break;
    943 
    944 	case ADB_HW_CUDA:
    945 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
    946 						 * outputs */
    947 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
    948 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
    949 							 * to IN */
    950 		write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
    951 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
    952 							 * hardware */
    953 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
    954 						 * are on */
    955 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
    956 
    957 		/* sort of a device reset */
    958 		i = ADB_SR();	/* clear interrupt */
    959 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
    960 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
    961 		delay(ADB_DELAY);
    962 		ADB_SET_STATE_TIP();	/* signal start of frame */
    963 		delay(ADB_DELAY);
    964 		ADB_TOGGLE_STATE_ACK_CUDA();
    965 		delay(ADB_DELAY);
    966 		ADB_CLR_STATE_TIP();
    967 		delay(ADB_DELAY);
    968 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
    969 		i = ADB_SR();	/* clear interrupt */
    970 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
    971 		break;
    972 
    973 	case ADB_HW_UNKNOWN:
    974 	default:
    975 		write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
    976 						 * DO: turn PB ints off? */
    977 		return;
    978 		break;
    979 	}
    980 }
    981 
    982 /*
    983  * adb_reinit sets up the adb stuff
    984  *
    985  */
    986 void
    987 adb_reinit(void)
    988 {
    989 	u_char send_string[ADB_MAX_MSG_LENGTH];
    990 	ADBDataBlock data;	/* temp. holder for getting device info */
    991 	volatile int i, x;
    992 	int s = 0;		/* XXX: gcc */
    993 	int command;
    994 	int result;
    995 	int saveptr;		/* point to next free relocation address */
    996 	int device;
    997 	int nonewtimes;		/* times thru loop w/o any new devices */
    998 
    999 	/* Make sure we are not interrupted while building the table. */
   1000 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
   1001 		s = splhigh();
   1002 
   1003 	ADBNumDevices = 0;	/* no devices yet */
   1004 
   1005 	/* Let intr routines know we are running reinit */
   1006 	adbStarting = 1;
   1007 
   1008 	/*
   1009 	 * Initialize the ADB table.  For now, we'll always use the same table
   1010 	 * that is defined at the beginning of this file - no mallocs.
   1011 	 */
   1012 	for (i = 0; i < 16; i++)
   1013 		ADBDevTable[i].devType = 0;
   1014 
   1015 	adb_setup_hw_type();	/* setup hardware type */
   1016 
   1017 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
   1018 
   1019 	delay(1000);
   1020 
   1021 	/* send an ADB reset first */
   1022 	result = adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
   1023 	delay(200000);
   1024 
   1025 #ifdef ADB_DEBUG
   1026 	if (result && adb_debug) {
   1027 		printf_intr("adb_reinit: failed to reset, result = %d\n",result);
   1028 	}
   1029 #endif
   1030 
   1031 	/*
   1032 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
   1033 	 * which device addresses are in use and which are free. For each
   1034 	 * address that is in use, move the device at that address to a higher
   1035 	 * free address. Continue doing this at that address until no device
   1036 	 * responds at that address. Then move the last device that was moved
   1037 	 * back to the original address. Do this for the remaining addresses
   1038 	 * that we determined were in use.
   1039 	 *
   1040 	 * When finished, do this entire process over again with the updated
   1041 	 * list of in use addresses. Do this until no new devices have been
   1042 	 * found in 20 passes though the in use address list. (This probably
   1043 	 * seems long and complicated, but it's the best way to detect multiple
   1044 	 * devices at the same address - sometimes it takes a couple of tries
   1045 	 * before the collision is detected.)
   1046 	 */
   1047 
   1048 	/* initial scan through the devices */
   1049 	for (i = 1; i < 16; i++) {
   1050 		send_string[0] = 0;
   1051 		command = ADBTALK(i, 3);
   1052 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
   1053 		    (Ptr)0, (short)command);
   1054 
   1055 #ifdef ADB_DEBUG
   1056 		if (result && adb_debug) {
   1057 			printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
   1058 					i,result,send_string[0]);
   1059 		}
   1060 #endif
   1061 
   1062 		if (send_string[0] != 0) {
   1063 			/* check for valid device handler */
   1064 			switch (send_string[2]) {
   1065 			case 0:
   1066 			case 0xfd:
   1067 			case 0xfe:
   1068 			case 0xff:
   1069 				continue;	/* invalid, skip */
   1070 			}
   1071 
   1072 			/* found a device */
   1073 			++ADBNumDevices;
   1074 			KASSERT(ADBNumDevices < 16);
   1075 			ADBDevTable[ADBNumDevices].devType =
   1076 				(int)send_string[2];
   1077 			ADBDevTable[ADBNumDevices].origAddr = i;
   1078 			ADBDevTable[ADBNumDevices].currentAddr = i;
   1079 			ADBDevTable[ADBNumDevices].DataAreaAddr =
   1080 			    (long)0;
   1081 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
   1082 			pm_check_adb_devices(i);	/* tell pm driver device
   1083 							 * is here */
   1084 		}
   1085 	}
   1086 
   1087 	/* find highest unused address */
   1088 	for (saveptr = 15; saveptr > 0; saveptr--)
   1089 		if (-1 == get_adb_info(&data, saveptr))
   1090 			break;
   1091 
   1092 #ifdef ADB_DEBUG
   1093 	if (adb_debug & 0x80) {
   1094 		printf_intr("first free is: 0x%02x\n", saveptr);
   1095 		printf_intr("devices: %i\n", ADBNumDevices);
   1096 	}
   1097 #endif
   1098 
   1099 	nonewtimes = 0;		/* no loops w/o new devices */
   1100 	while (saveptr > 0 && nonewtimes++ < 11) {
   1101 		for (i = 1; i <= ADBNumDevices; i++) {
   1102 			device = ADBDevTable[i].currentAddr;
   1103 #ifdef ADB_DEBUG
   1104 			if (adb_debug & 0x80)
   1105 				printf_intr("moving device 0x%02x to 0x%02x "
   1106 				    "(index 0x%02x)  ", device, saveptr, i);
   1107 #endif
   1108 
   1109 			/* send TALK R3 to address */
   1110 			command = ADBTALK(device, 3);
   1111 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1112 			    (Ptr)0, (short)command);
   1113 
   1114 			/* move device to higher address */
   1115 			command = ADBLISTEN(device, 3);
   1116 			send_string[0] = 2;
   1117 			send_string[1] = (u_char)(saveptr | 0x60);
   1118 			send_string[2] = 0xfe;
   1119 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1120 			    (Ptr)0, (short)command);
   1121 			delay(500);
   1122 
   1123 			/* send TALK R3 - anything at new address? */
   1124 			command = ADBTALK(saveptr, 3);
   1125 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1126 			    (Ptr)0, (short)command);
   1127 			delay(500);
   1128 
   1129 			if (send_string[0] == 0) {
   1130 #ifdef ADB_DEBUG
   1131 				if (adb_debug & 0x80)
   1132 					printf_intr("failed, continuing\n");
   1133 #endif
   1134 				continue;
   1135 			}
   1136 
   1137 			/* send TALK R3 - anything at old address? */
   1138 			command = ADBTALK(device, 3);
   1139 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
   1140 			    (Ptr)0, (short)command);
   1141 			if (send_string[0] != 0) {
   1142 				/* check for valid device handler */
   1143 				switch (send_string[2]) {
   1144 				case 0:
   1145 				case 0xfd:
   1146 				case 0xfe:
   1147 				case 0xff:
   1148 					continue;	/* invalid, skip */
   1149 				}
   1150 
   1151 				/* new device found */
   1152 				/* update data for previously moved device */
   1153 				ADBDevTable[i].currentAddr = saveptr;
   1154 #ifdef ADB_DEBUG
   1155 				if (adb_debug & 0x80)
   1156 					printf_intr("old device at index %i\n",i);
   1157 #endif
   1158 				/* add new device in table */
   1159 #ifdef ADB_DEBUG
   1160 				if (adb_debug & 0x80)
   1161 					printf_intr("new device found\n");
   1162 #endif
   1163 				if (saveptr > ADBNumDevices) {
   1164 					++ADBNumDevices;
   1165 					KASSERT(ADBNumDevices < 16);
   1166 				}
   1167 				ADBDevTable[ADBNumDevices].devType =
   1168 					(int)send_string[2];
   1169 				ADBDevTable[ADBNumDevices].origAddr = device;
   1170 				ADBDevTable[ADBNumDevices].currentAddr = device;
   1171 				/* These will be set correctly in adbsys.c */
   1172 				/* Until then, unsol. data will be ignored. */
   1173 				ADBDevTable[ADBNumDevices].DataAreaAddr =
   1174 				    (long)0;
   1175 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
   1176 				    (void *)0;
   1177 				/* find next unused address */
   1178 				for (x = saveptr; x > 0; x--) {
   1179 					if (-1 == get_adb_info(&data, x)) {
   1180 						saveptr = x;
   1181 						break;
   1182 					}
   1183 				}
   1184 				if (x == 0)
   1185 					saveptr = 0;
   1186 #ifdef ADB_DEBUG
   1187 				if (adb_debug & 0x80)
   1188 					printf_intr("new free is 0x%02x\n",
   1189 					    saveptr);
   1190 #endif
   1191 				nonewtimes = 0;
   1192 				/* tell pm driver device is here */
   1193 				pm_check_adb_devices(device);
   1194 			} else {
   1195 #ifdef ADB_DEBUG
   1196 				if (adb_debug & 0x80)
   1197 					printf_intr("moving back...\n");
   1198 #endif
   1199 				/* move old device back */
   1200 				command = ADBLISTEN(saveptr, 3);
   1201 				send_string[0] = 2;
   1202 				send_string[1] = (u_char)(device | 0x60);
   1203 				send_string[2] = 0xfe;
   1204 				adb_op_sync((Ptr)send_string, (Ptr)0,
   1205 				    (Ptr)0, (short)command);
   1206 				delay(1000);
   1207 			}
   1208 		}
   1209 	}
   1210 
   1211 #ifdef ADB_DEBUG
   1212 	if (adb_debug) {
   1213 		for (i = 1; i <= ADBNumDevices; i++) {
   1214 			x = get_ind_adb_info(&data, i);
   1215 			if (x != -1)
   1216 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
   1217 				    i, x, data.devType);
   1218 		}
   1219 	}
   1220 #endif
   1221 
   1222 #ifdef ADB_DEBUG
   1223 	if (adb_debug) {
   1224 		if (0 == ADBNumDevices)	/* tell user if no devices found */
   1225 			printf_intr("adb: no devices found\n");
   1226 	}
   1227 #endif
   1228 
   1229 	adbStarting = 0;	/* not starting anymore */
   1230 #ifdef ADB_DEBUG
   1231 	if (adb_debug)
   1232 		printf_intr("adb: ADBReInit complete\n");
   1233 #endif
   1234 
   1235 	if (adbHardware == ADB_HW_CUDA)
   1236 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
   1237 		    (void *)adb_cuda_tickle, NULL);
   1238 
   1239 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
   1240 		splx(s);
   1241 }
   1242 
   1243 /*
   1244  * adb_cmd_result
   1245  *
   1246  * This routine lets the caller know whether the specified adb command string
   1247  * should expect a returned result, such as a TALK command.
   1248  *
   1249  * returns: 0 if a result should be expected
   1250  *          1 if a result should NOT be expected
   1251  */
   1252 int
   1253 adb_cmd_result(u_char *in)
   1254 {
   1255 	switch (adbHardware) {
   1256 	case ADB_HW_CUDA:
   1257 		/* was it an ADB talk command? */
   1258 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
   1259 			return 0;
   1260 		/* was it an RTC/PRAM read date/time? */
   1261 		if ((in[1] == 0x01) && (in[2] == 0x03))
   1262 			return 0;
   1263 		return 1;
   1264 
   1265 	case ADB_HW_PMU:
   1266 		return 1;
   1267 
   1268 	case ADB_HW_UNKNOWN:
   1269 	default:
   1270 		return 1;
   1271 	}
   1272 }
   1273 
   1274 
   1275 /*
   1276  * adb_cmd_extra
   1277  *
   1278  * This routine lets the caller know whether the specified adb command string
   1279  * may have extra data appended to the end of it, such as a LISTEN command.
   1280  *
   1281  * returns: 0 if extra data is allowed
   1282  *          1 if extra data is NOT allowed
   1283  */
   1284 int
   1285 adb_cmd_extra(u_char *in)
   1286 {
   1287 	switch (adbHardware) {
   1288 	case ADB_HW_CUDA:
   1289 		/*
   1290 		 * TO DO: support needs to be added to recognize RTC and PRAM
   1291 		 * commands
   1292 		 */
   1293 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
   1294 			return 0;
   1295 		/* add others later */
   1296 		return 1;
   1297 
   1298 	case ADB_HW_PMU:
   1299 		return 1;
   1300 
   1301 	case ADB_HW_UNKNOWN:
   1302 	default:
   1303 		return 1;
   1304 	}
   1305 }
   1306 
   1307 /*
   1308  * adb_op_sync
   1309  *
   1310  * This routine does exactly what the adb_op routine does, except that after
   1311  * the adb_op is called, it waits until the return value is present before
   1312  * returning.
   1313  *
   1314  * NOTE: The user specified compRout is ignored, since this routine specifies
   1315  * it's own to adb_op, which is why you really called this in the first place
   1316  * anyway.
   1317  */
   1318 int
   1319 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
   1320 {
   1321 	int tmout;
   1322 	int result;
   1323 	volatile int flag = 0;
   1324 
   1325 	result = adb_op(buffer, (void *)adb_op_comprout,
   1326 	    (void *)&flag, command);	/* send command */
   1327 	if (result == 0) {		/* send ok? */
   1328 		/*
   1329 		 * Total time to wait is calculated as follows:
   1330 		 *  - Tlt (stop to start time): 260 usec
   1331 		 *  - start bit: 100 usec
   1332 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
   1333 		 *  - stop bit (with SRQ): 140 usec
   1334 		 * Total: 6900 usec
   1335 		 *
   1336 		 * This is the total time allowed by the specification.  Any
   1337 		 * device that doesn't conform to this will fail to operate
   1338 		 * properly on some Apple systems.  In spite of this we
   1339 		 * double the time to wait; some Cuda-based apparently
   1340 		 * queues some commands and allows the main CPU to continue
   1341 		 * processing (radical concept, eh?).  To be safe, allow
   1342 		 * time for two complete ADB transactions to occur.
   1343 		 */
   1344 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
   1345 			delay(10);
   1346 		if (!flag && tmout > 0)
   1347 			delay(tmout);
   1348 
   1349 		if (!flag)
   1350 			result = -2;
   1351 	}
   1352 
   1353 	return result;
   1354 }
   1355 
   1356 /*
   1357  * adb_op_comprout
   1358  *
   1359  * This function is used by the adb_op_sync routine so it knows when the
   1360  * function is done.
   1361  */
   1362 void
   1363 adb_op_comprout(buffer, compdata, cmd)
   1364 	caddr_t buffer, compdata;
   1365 	int cmd;
   1366 {
   1367 	short *p = (short *)compdata;
   1368 
   1369 	*p = 1;
   1370 }
   1371 
   1372 void
   1373 adb_setup_hw_type(void)
   1374 {
   1375 	switch (adbHardware) {
   1376 	case ADB_HW_CUDA:
   1377 		return;
   1378 
   1379 	case ADB_HW_PMU:
   1380 		pm_setup_adb();
   1381 		return;
   1382 
   1383 	default:
   1384 		panic("unknown adb hardware");
   1385 	}
   1386 }
   1387 
   1388 int
   1389 count_adbs(void)
   1390 {
   1391 	int i;
   1392 	int found;
   1393 
   1394 	found = 0;
   1395 
   1396 	for (i = 1; i < 16; i++)
   1397 		if (0 != ADBDevTable[i].devType)
   1398 			found++;
   1399 
   1400 	return found;
   1401 }
   1402 
   1403 int
   1404 get_ind_adb_info(ADBDataBlock * info, int index)
   1405 {
   1406 	if ((index < 1) || (index > 15))	/* check range 1-15 */
   1407 		return (-1);
   1408 
   1409 #ifdef ADB_DEBUG
   1410 	if (adb_debug & 0x80)
   1411 		printf_intr("index 0x%x devType is: 0x%x\n", index,
   1412 		    ADBDevTable[index].devType);
   1413 #endif
   1414 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
   1415 		return (-1);
   1416 
   1417 	info->devType = ADBDevTable[index].devType;
   1418 	info->origADBAddr = ADBDevTable[index].origAddr;
   1419 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
   1420 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
   1421 
   1422 	return (ADBDevTable[index].currentAddr);
   1423 }
   1424 
   1425 int
   1426 get_adb_info(ADBDataBlock * info, int adbAddr)
   1427 {
   1428 	int i;
   1429 
   1430 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1431 		return (-1);
   1432 
   1433 	for (i = 1; i < 15; i++)
   1434 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1435 			info->devType = ADBDevTable[i].devType;
   1436 			info->origADBAddr = ADBDevTable[i].origAddr;
   1437 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
   1438 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
   1439 			return 0;	/* found */
   1440 		}
   1441 
   1442 	return (-1);		/* not found */
   1443 }
   1444 
   1445 int
   1446 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
   1447 {
   1448 	int i;
   1449 
   1450 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1451 		return (-1);
   1452 
   1453 	for (i = 1; i < 15; i++)
   1454 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1455 			ADBDevTable[i].ServiceRtPtr =
   1456 			    (void *)(info->siServiceRtPtr);
   1457 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
   1458 			return 0;	/* found */
   1459 		}
   1460 
   1461 	return (-1);		/* not found */
   1462 
   1463 }
   1464 
   1465 #ifndef MRG_ADB
   1466 
   1467 /* caller should really use machine-independant version: getPramTime */
   1468 /* this version does pseudo-adb access only */
   1469 int
   1470 adb_read_date_time(unsigned long *time)
   1471 {
   1472 	u_char output[ADB_MAX_MSG_LENGTH];
   1473 	int result;
   1474 	volatile int flag = 0;
   1475 
   1476 	switch (adbHardware) {
   1477 	case ADB_HW_PMU:
   1478 		pm_read_date_time(time);
   1479 		return 0;
   1480 
   1481 	case ADB_HW_CUDA:
   1482 		output[0] = 0x02;	/* 2 byte message */
   1483 		output[1] = 0x01;	/* to pram/rtc device */
   1484 		output[2] = 0x03;	/* read date/time */
   1485 		result = send_adb_cuda((u_char *)output, (u_char *)output,
   1486 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1487 		if (result != 0)	/* exit if not sent */
   1488 			return -1;
   1489 
   1490 		while (0 == flag)	/* wait for result */
   1491 			;
   1492 
   1493 		memcpy(time, output + 1, 4);
   1494 		return 0;
   1495 
   1496 	case ADB_HW_UNKNOWN:
   1497 	default:
   1498 		return -1;
   1499 	}
   1500 }
   1501 
   1502 /* caller should really use machine-independant version: setPramTime */
   1503 /* this version does pseudo-adb access only */
   1504 int
   1505 adb_set_date_time(unsigned long time)
   1506 {
   1507 	u_char output[ADB_MAX_MSG_LENGTH];
   1508 	int result;
   1509 	volatile int flag = 0;
   1510 
   1511 	switch (adbHardware) {
   1512 
   1513 	case ADB_HW_CUDA:
   1514 		output[0] = 0x06;	/* 6 byte message */
   1515 		output[1] = 0x01;	/* to pram/rtc device */
   1516 		output[2] = 0x09;	/* set date/time */
   1517 		output[3] = (u_char)(time >> 24);
   1518 		output[4] = (u_char)(time >> 16);
   1519 		output[5] = (u_char)(time >> 8);
   1520 		output[6] = (u_char)(time);
   1521 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1522 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1523 		if (result != 0)	/* exit if not sent */
   1524 			return -1;
   1525 
   1526 		while (0 == flag)	/* wait for send to finish */
   1527 			;
   1528 
   1529 		return 0;
   1530 
   1531 	case ADB_HW_PMU:
   1532 		pm_set_date_time(time);
   1533 		return 0;
   1534 
   1535 	case ADB_HW_UNKNOWN:
   1536 	default:
   1537 		return -1;
   1538 	}
   1539 }
   1540 
   1541 
   1542 int
   1543 adb_poweroff(void)
   1544 {
   1545 	u_char output[ADB_MAX_MSG_LENGTH];
   1546 	int result;
   1547 
   1548 	adb_polling = 1;
   1549 
   1550 	switch (adbHardware) {
   1551 	case ADB_HW_PMU:
   1552 		pm_adb_poweroff();
   1553 
   1554 		for (;;);		/* wait for power off */
   1555 
   1556 		return 0;
   1557 
   1558 	case ADB_HW_CUDA:
   1559 		output[0] = 0x02;	/* 2 byte message */
   1560 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1561 		output[2] = 0x0a;	/* set date/time */
   1562 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1563 		    (void *)0, (void *)0, (int)0);
   1564 		if (result != 0)	/* exit if not sent */
   1565 			return -1;
   1566 
   1567 		for (;;);		/* wait for power off */
   1568 
   1569 		return 0;
   1570 
   1571 	case ADB_HW_UNKNOWN:
   1572 	default:
   1573 		return -1;
   1574 	}
   1575 }
   1576 
   1577 int
   1578 CountADBs(void)
   1579 {
   1580 	return (count_adbs());
   1581 }
   1582 
   1583 void
   1584 ADBReInit(void)
   1585 {
   1586 	adb_reinit();
   1587 }
   1588 
   1589 int
   1590 GetIndADB(ADBDataBlock * info, int index)
   1591 {
   1592 	return (get_ind_adb_info(info, index));
   1593 }
   1594 
   1595 int
   1596 GetADBInfo(ADBDataBlock * info, int adbAddr)
   1597 {
   1598 	return (get_adb_info(info, adbAddr));
   1599 }
   1600 
   1601 int
   1602 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
   1603 {
   1604 	return (set_adb_info(info, adbAddr));
   1605 }
   1606 
   1607 int
   1608 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
   1609 {
   1610 	return (adb_op(buffer, compRout, data, commandNum));
   1611 }
   1612 
   1613 #endif
   1614 
   1615 int
   1616 setsoftadb()
   1617 {
   1618 	callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
   1619 	return 0;
   1620 }
   1621 
   1622 void
   1623 adb_cuda_autopoll()
   1624 {
   1625 	volatile int flag = 0;
   1626 	int result;
   1627 	u_char output[16];
   1628 
   1629 	output[0] = 0x03;	/* 3-byte message */
   1630 	output[1] = 0x01;	/* to pram/rtc device */
   1631 	output[2] = 0x01;	/* cuda autopoll */
   1632 	output[3] = 0x01;
   1633 	result = send_adb_cuda(output, output, adb_op_comprout, (void *)&flag,
   1634 			       0);
   1635 	if (result != 0)	/* exit if not sent */
   1636 		return;
   1637 
   1638 	while (flag == 0);	/* wait for result */
   1639 }
   1640 
   1641 void
   1642 adb_restart(void)
   1643 {
   1644 	int result;
   1645 	u_char output[16];
   1646 
   1647 	adb_polling = 1;
   1648 
   1649 	switch (adbHardware) {
   1650 	case ADB_HW_CUDA:
   1651 		output[0] = 0x02;	/* 2 byte message */
   1652 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1653 		output[2] = 0x11;	/* restart */
   1654 		result = send_adb_cuda(output, NULL, NULL, NULL, 0);
   1655 		if (result != 0)	/* exit if not sent */
   1656 			return;
   1657 		while (1);		/* not return */
   1658 
   1659 	case ADB_HW_PMU:
   1660 		pm_adb_restart();
   1661 		while (1);		/* not return */
   1662 	}
   1663 }
   1664