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