Home | History | Annotate | Line # | Download | only in dev
adb_direct.c revision 1.19
      1 /*	$NetBSD: adb_direct.c,v 1.19 2001/08/03 23:09:42 tsubai Exp $	*/
      2 
      3 /* From: adb_direct.c 2.02 4/18/97 jpw */
      4 
      5 /*
      6  * Copyright (C) 1996, 1997 John P. Wittkoski
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *  This product includes software developed by John P. Wittkoski.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * This code is rather messy, but I don't have time right now
     37  * to clean it up as much as I would like.
     38  * But it works, so I'm happy. :-) jpw
     39  */
     40 
     41 /*
     42  * TO DO:
     43  *  - We could reduce the time spent in the adb_intr_* routines
     44  *    by having them save the incoming and outgoing data directly
     45  *    in the adbInbound and adbOutbound queues, as it would reduce
     46  *    the number of times we need to copy the data around. It
     47  *    would also make the code more readable and easier to follow.
     48  *  - (Related to above) Use the header part of adbCommand to
     49  *    reduce the number of copies we have to do of the data.
     50  *  - (Related to above) Actually implement the adbOutbound queue.
     51  *    This is fairly easy once you switch all the intr routines
     52  *    over to using adbCommand structs directly.
     53  *  - There is a bug in the state machine of adb_intr_cuda
     54  *    code that causes hangs, especially on 030 machines, probably
     55  *    because of some timing issues. Because I have been unable to
     56  *    determine the exact cause of this bug, I used the timeout function
     57  *    to check for and recover from this condition. If anyone finds
     58  *    the actual cause of this bug, the calls to timeout and the
     59  *    adb_cuda_tickle routine can be removed.
     60  */
     61 
     62 #include <sys/param.h>
     63 #include <sys/cdefs.h>
     64 #include <sys/systm.h>
     65 #include <sys/callout.h>
     66 #include <sys/device.h>
     67 
     68 #include <machine/param.h>
     69 #include <machine/cpu.h>
     70 #include <machine/adbsys.h>
     71 
     72 #include <macppc/dev/viareg.h>
     73 #include <macppc/dev/adbvar.h>
     74 #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 int	adb_cmd_result __P((u_char *));
    290 int	adb_cmd_extra __P((u_char *));
    291 int	adb_guess_next_device __P((void));
    292 int	adb_prog_switch_enable __P((void));
    293 int	adb_prog_switch_disable __P((void));
    294 /* we should create this and it will be the public version */
    295 int	send_adb __P((u_char *, void *, void *));
    296 
    297 int	setsoftadb __P((void));
    298 
    299 #ifdef ADB_DEBUG
    300 /*
    301  * print_single
    302  * Diagnostic display routine. Displays the hex values of the
    303  * specified elements of the u_char. The length of the "string"
    304  * is in [0].
    305  */
    306 void
    307 print_single(str)
    308 	u_char *str;
    309 {
    310 	int x;
    311 
    312 	if (str == 0) {
    313 		printf_intr("no data - null pointer\n");
    314 		return;
    315 	}
    316 	if (*str == 0) {
    317 		printf_intr("nothing returned\n");
    318 		return;
    319 	}
    320 	if (*str > 20) {
    321 		printf_intr("ADB: ACK > 20 no way!\n");
    322 		*str = 20;
    323 	}
    324 	printf_intr("(length=0x%x):", *str);
    325 	for (x = 1; x <= *str; x++)
    326 		printf_intr("  0x%02x", str[x]);
    327 	printf_intr("\n");
    328 }
    329 #endif
    330 
    331 void
    332 adb_cuda_tickle(void)
    333 {
    334 	volatile int s;
    335 
    336 	if (adbActionState == ADB_ACTION_IN) {
    337 		if (tickle_serial == adb_cuda_serial) {
    338 			if (++tickle_count > 0) {
    339 				s = splhigh();
    340 				adbActionState = ADB_ACTION_IDLE;
    341 				adbInputBuffer[0] = 0;
    342 				ADB_SET_STATE_IDLE_CUDA();
    343 				splx(s);
    344 			}
    345 		} else {
    346 			tickle_serial = adb_cuda_serial;
    347 			tickle_count = 0;
    348 		}
    349 	} else {
    350 		tickle_serial = adb_cuda_serial;
    351 		tickle_count = 0;
    352 	}
    353 
    354 	callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
    355 	    (void *)adb_cuda_tickle, NULL);
    356 }
    357 
    358 /*
    359  * called when when an adb interrupt happens
    360  *
    361  * Cuda version of adb_intr
    362  * TO DO: do we want to add some calls to intr_dispatch() here to
    363  * grab serial interrupts?
    364  */
    365 void
    366 adb_intr_cuda(void)
    367 {
    368 	volatile int i, ending;
    369 	volatile unsigned int s;
    370 	struct adbCommand packet;
    371 
    372 	s = splhigh();		/* can't be too careful - might be called */
    373 	/* from a routine, NOT an interrupt */
    374 
    375 	ADB_VIA_CLR_INTR();	/* clear interrupt */
    376 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
    377 
    378 switch_start:
    379 	switch (adbActionState) {
    380 	case ADB_ACTION_IDLE:
    381 		/*
    382 		 * This is an unexpected packet, so grab the first (dummy)
    383 		 * byte, set up the proper vars, and tell the chip we are
    384 		 * starting to receive the packet by setting the TIP bit.
    385 		 */
    386 		adbInputBuffer[1] = ADB_SR();
    387 		adb_cuda_serial++;
    388 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
    389 			break;
    390 
    391 		ADB_SET_SR_INPUT();
    392 		ADB_SET_STATE_TIP();
    393 
    394 		adbInputBuffer[0] = 1;
    395 		adbActionState = ADB_ACTION_IN;
    396 #ifdef ADB_DEBUG
    397 		if (adb_debug)
    398 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
    399 #endif
    400 		break;
    401 
    402 	case ADB_ACTION_IN:
    403 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
    404 		/* intr off means this is the last byte (end of frame) */
    405 		if (ADB_INTR_IS_OFF)
    406 			ending = 1;
    407 		else
    408 			ending = 0;
    409 
    410 		if (1 == ending) {	/* end of message? */
    411 #ifdef ADB_DEBUG
    412 			if (adb_debug) {
    413 				printf_intr("in end 0x%02x ",
    414 				    adbInputBuffer[adbInputBuffer[0]]);
    415 				print_single(adbInputBuffer);
    416 			}
    417 #endif
    418 
    419 			/*
    420 			 * Are we waiting AND does this packet match what we
    421 			 * are waiting for AND is it coming from either the
    422 			 * ADB or RTC/PRAM sub-device? This section _should_
    423 			 * recognize all ADB and RTC/PRAM type commands, but
    424 			 * there may be more... NOTE: commands are always at
    425 			 * [4], even for RTC/PRAM commands.
    426 			 */
    427 			/* set up data for adb_pass_up */
    428 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
    429 
    430 			if ((adbWaiting == 1) &&
    431 			    (adbInputBuffer[4] == adbWaitingCmd) &&
    432 			    ((adbInputBuffer[2] == 0x00) ||
    433 			    (adbInputBuffer[2] == 0x01))) {
    434 				packet.saveBuf = adbBuffer;
    435 				packet.compRout = adbCompRout;
    436 				packet.compData = adbCompData;
    437 				packet.unsol = 0;
    438 				packet.ack_only = 0;
    439 				adb_pass_up(&packet);
    440 
    441 				adbWaitingCmd = 0;	/* reset "waiting" vars */
    442 				adbWaiting = 0;
    443 				adbBuffer = (long)0;
    444 				adbCompRout = (long)0;
    445 				adbCompData = (long)0;
    446 			} else {
    447 				packet.unsol = 1;
    448 				packet.ack_only = 0;
    449 				adb_pass_up(&packet);
    450 			}
    451 
    452 
    453 			/* reset vars and signal the end of this frame */
    454 			adbActionState = ADB_ACTION_IDLE;
    455 			adbInputBuffer[0] = 0;
    456 			ADB_SET_STATE_IDLE_CUDA();
    457 			/*ADB_SET_SR_INPUT();*/
    458 
    459 			/*
    460 			 * If there is something waiting to be sent out,
    461 			 * the set everything up and send the first byte.
    462 			 */
    463 			if (adbWriteDelay == 1) {
    464 				delay(ADB_DELAY);	/* required */
    465 				adbSentChars = 0;
    466 				adbActionState = ADB_ACTION_OUT;
    467 				/*
    468 				 * If the interrupt is on, we were too slow
    469 				 * and the chip has already started to send
    470 				 * something to us, so back out of the write
    471 				 * and start a read cycle.
    472 				 */
    473 				if (ADB_INTR_IS_ON) {
    474 					ADB_SET_SR_INPUT();
    475 					ADB_SET_STATE_IDLE_CUDA();
    476 					adbSentChars = 0;
    477 					adbActionState = ADB_ACTION_IDLE;
    478 					adbInputBuffer[0] = 0;
    479 					break;
    480 				}
    481 				/*
    482 				 * If we got here, it's ok to start sending
    483 				 * so load the first byte and tell the chip
    484 				 * we want to send.
    485 				 */
    486 				ADB_SET_STATE_TIP();
    487 				ADB_SET_SR_OUTPUT();
    488 				write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
    489 			}
    490 		} else {
    491 			ADB_TOGGLE_STATE_ACK_CUDA();
    492 #ifdef ADB_DEBUG
    493 			if (adb_debug)
    494 				printf_intr("in 0x%02x ",
    495 				    adbInputBuffer[adbInputBuffer[0]]);
    496 #endif
    497 		}
    498 		break;
    499 
    500 	case ADB_ACTION_OUT:
    501 		i = ADB_SR();	/* reset SR-intr in IFR */
    502 #ifdef ADB_DEBUG
    503 		if (adb_debug)
    504 			printf_intr("intr out 0x%02x ", i);
    505 #endif
    506 
    507 		adbSentChars++;
    508 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
    509 #ifdef ADB_DEBUG
    510 			if (adb_debug)
    511 				printf_intr("intr was on ");
    512 #endif
    513 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    514 			ADB_SET_STATE_IDLE_CUDA();
    515 			adbSentChars = 0;	/* must start all over */
    516 			adbActionState = ADB_ACTION_IDLE;	/* new state */
    517 			adbInputBuffer[0] = 0;
    518 			adbWriteDelay = 1;	/* must retry when done with
    519 						 * read */
    520 			delay(ADB_DELAY);
    521 			goto switch_start;	/* process next state right
    522 						 * now */
    523 			break;
    524 		}
    525 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
    526 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
    527 									 * back? */
    528 				adbWaiting = 1;	/* signal waiting for return */
    529 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
    530 			} else {	/* no talk, so done */
    531 				/* set up stuff for adb_pass_up */
    532 				memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
    533 				packet.saveBuf = adbBuffer;
    534 				packet.compRout = adbCompRout;
    535 				packet.compData = adbCompData;
    536 				packet.cmd = adbWaitingCmd;
    537 				packet.unsol = 0;
    538 				packet.ack_only = 1;
    539 				adb_pass_up(&packet);
    540 
    541 				/* reset "waiting" vars, just in case */
    542 				adbWaitingCmd = 0;
    543 				adbBuffer = (long)0;
    544 				adbCompRout = (long)0;
    545 				adbCompData = (long)0;
    546 			}
    547 
    548 			adbWriteDelay = 0;	/* done writing */
    549 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
    550 			ADB_SET_SR_INPUT();
    551 			ADB_SET_STATE_IDLE_CUDA();
    552 #ifdef ADB_DEBUG
    553 			if (adb_debug)
    554 				printf_intr("write done ");
    555 #endif
    556 		} else {
    557 			write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* send next byte */
    558 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
    559 							 * shift */
    560 #ifdef ADB_DEBUG
    561 			if (adb_debug)
    562 				printf_intr("toggle ");
    563 #endif
    564 		}
    565 		break;
    566 
    567 	case ADB_ACTION_NOTREADY:
    568 #ifdef ADB_DEBUG
    569 		if (adb_debug)
    570 			printf_intr("adb: not yet initialized\n");
    571 #endif
    572 		break;
    573 
    574 	default:
    575 #ifdef ADB_DEBUG
    576 		if (adb_debug)
    577 			printf_intr("intr: unknown ADB state\n");
    578 #endif
    579 		break;
    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 		}
   1017 
   1018 		s = splhigh();
   1019 		adbInCount--;
   1020 		if (++adbInHead >= ADB_QUEUE)
   1021 			adbInHead = 0;
   1022 		splx(s);
   1023 
   1024 	}
   1025 	return;
   1026 }
   1027 
   1028 
   1029 /*
   1030  * This is my version of the ADBOp routine. It mainly just calls the
   1031  * hardware-specific routine.
   1032  *
   1033  *   data 	: pointer to data area to be used by compRout
   1034  *   compRout	: completion routine
   1035  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
   1036  *		  byte 0 = # of bytes
   1037  *		: for TALK: points to place to save return data
   1038  *   command	: the adb command to send
   1039  *   result	: 0 = success
   1040  *		: -1 = could not complete
   1041  */
   1042 int
   1043 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
   1044 {
   1045 	int result;
   1046 
   1047 	switch (adbHardware) {
   1048 	case ADB_HW_II:
   1049 		result = send_adb_II((u_char *)0, (u_char *)buffer,
   1050 		    (void *)compRout, (void *)data, (int)command);
   1051 		if (result == 0)
   1052 			return 0;
   1053 		else
   1054 			return -1;
   1055 		break;
   1056 
   1057 	case ADB_HW_IISI:
   1058 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
   1059 		    (void *)compRout, (void *)data, (int)command);
   1060 		/*
   1061 		 * I wish I knew why this delay is needed. It usually needs to
   1062 		 * be here when several commands are sent in close succession,
   1063 		 * especially early in device probes when doing collision
   1064 		 * detection. It must be some race condition. Sigh. - jpw
   1065 		 */
   1066 		delay(100);
   1067 		if (result == 0)
   1068 			return 0;
   1069 		else
   1070 			return -1;
   1071 		break;
   1072 
   1073 	case ADB_HW_PB:
   1074 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
   1075 		    (void *)data, (int)command);
   1076 
   1077 		if (result == 0)
   1078 			return 0;
   1079 		else
   1080 			return -1;
   1081 		break;
   1082 
   1083 	case ADB_HW_CUDA:
   1084 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
   1085 		    (void *)compRout, (void *)data, (int)command);
   1086 		if (result == 0)
   1087 			return 0;
   1088 		else
   1089 			return -1;
   1090 		break;
   1091 
   1092 	case ADB_HW_UNKNOWN:
   1093 	default:
   1094 		return -1;
   1095 	}
   1096 }
   1097 
   1098 
   1099 /*
   1100  * adb_hw_setup
   1101  * This routine sets up the possible machine specific hardware
   1102  * config (mainly VIA settings) for the various models.
   1103  */
   1104 void
   1105 adb_hw_setup(void)
   1106 {
   1107 	volatile int i;
   1108 	u_char send_string[ADB_MAX_MSG_LENGTH];
   1109 
   1110 	switch (adbHardware) {
   1111 	case ADB_HW_II:
   1112 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
   1113 						 * outputs */
   1114 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
   1115 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
   1116 							 * to IN (II, IIsi) */
   1117 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1118 							 * hardware (II, IIsi) */
   1119 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1120 						 * code only */
   1121 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
   1122 						 * are on (II, IIsi) */
   1123 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
   1124 
   1125 		ADB_VIA_CLR_INTR();	/* clear interrupt */
   1126 		break;
   1127 
   1128 	case ADB_HW_IISI:
   1129 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
   1130 						 * outputs */
   1131 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
   1132 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
   1133 							 * to IN (II, IIsi) */
   1134 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1135 							 * hardware (II, IIsi) */
   1136 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1137 						 * code only */
   1138 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
   1139 						 * are on (II, IIsi) */
   1140 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
   1141 
   1142 		/* get those pesky clock ticks we missed while booting */
   1143 		for (i = 0; i < 30; i++) {
   1144 			delay(ADB_DELAY);
   1145 			adb_hw_setup_IIsi(send_string);
   1146 #ifdef ADB_DEBUG
   1147 			if (adb_debug) {
   1148 				printf_intr("adb: cleanup: ");
   1149 				print_single(send_string);
   1150 			}
   1151 #endif
   1152 			delay(ADB_DELAY);
   1153 			if (ADB_INTR_IS_OFF)
   1154 				break;
   1155 		}
   1156 		break;
   1157 
   1158 	case ADB_HW_PB:
   1159 		/*
   1160 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
   1161 		 * pm_direct.h?
   1162 		 */
   1163 		write_via_reg(VIA1, vIFR, 0x90);	/* clear interrupt */
   1164 		break;
   1165 
   1166 	case ADB_HW_CUDA:
   1167 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
   1168 						 * outputs */
   1169 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
   1170 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
   1171 							 * to IN */
   1172 		write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
   1173 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1174 							 * hardware */
   1175 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1176 						 * code only */
   1177 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
   1178 						 * are on */
   1179 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
   1180 
   1181 		/* sort of a device reset */
   1182 		i = ADB_SR();	/* clear interrupt */
   1183 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
   1184 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
   1185 		delay(ADB_DELAY);
   1186 		ADB_SET_STATE_TIP();	/* signal start of frame */
   1187 		delay(ADB_DELAY);
   1188 		ADB_TOGGLE_STATE_ACK_CUDA();
   1189 		delay(ADB_DELAY);
   1190 		ADB_CLR_STATE_TIP();
   1191 		delay(ADB_DELAY);
   1192 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
   1193 		i = ADB_SR();	/* clear interrupt */
   1194 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
   1195 		break;
   1196 
   1197 	case ADB_HW_UNKNOWN:
   1198 	default:
   1199 		write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
   1200 						 * DO: turn PB ints off? */
   1201 		return;
   1202 		break;
   1203 	}
   1204 }
   1205 
   1206 
   1207 /*
   1208  * adb_hw_setup_IIsi
   1209  * This is sort of a "read" routine that forces the adb hardware through a read cycle
   1210  * if there is something waiting. This helps "clean up" any commands that may have gotten
   1211  * stuck or stopped during the boot process.
   1212  *
   1213  */
   1214 void
   1215 adb_hw_setup_IIsi(u_char * buffer)
   1216 {
   1217 	panic("adb_hw_setup_IIsi");
   1218 }
   1219 
   1220 
   1221 /*
   1222  * adb_reinit sets up the adb stuff
   1223  *
   1224  */
   1225 void
   1226 adb_reinit(void)
   1227 {
   1228 	u_char send_string[ADB_MAX_MSG_LENGTH];
   1229 	ADBDataBlock data;	/* temp. holder for getting device info */
   1230 	volatile int i, x;
   1231 	int s;
   1232 	int command;
   1233 	int result;
   1234 	int saveptr;		/* point to next free relocation address */
   1235 	int device;
   1236 	int nonewtimes;		/* times thru loop w/o any new devices */
   1237 
   1238 	/* Make sure we are not interrupted while building the table. */
   1239 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
   1240 		s = splhigh();
   1241 
   1242 	ADBNumDevices = 0;	/* no devices yet */
   1243 
   1244 	/* Let intr routines know we are running reinit */
   1245 	adbStarting = 1;
   1246 
   1247 	/*
   1248 	 * Initialize the ADB table.  For now, we'll always use the same table
   1249 	 * that is defined at the beginning of this file - no mallocs.
   1250 	 */
   1251 	for (i = 0; i < 16; i++)
   1252 		ADBDevTable[i].devType = 0;
   1253 
   1254 	adb_setup_hw_type();	/* setup hardware type */
   1255 
   1256 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
   1257 
   1258 	delay(1000);
   1259 
   1260 	/* send an ADB reset first */
   1261 	adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
   1262 	delay(200000);
   1263 
   1264 	/*
   1265 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
   1266 	 * which device addresses are in use and which are free. For each
   1267 	 * address that is in use, move the device at that address to a higher
   1268 	 * free address. Continue doing this at that address until no device
   1269 	 * responds at that address. Then move the last device that was moved
   1270 	 * back to the original address. Do this for the remaining addresses
   1271 	 * that we determined were in use.
   1272 	 *
   1273 	 * When finished, do this entire process over again with the updated
   1274 	 * list of in use addresses. Do this until no new devices have been
   1275 	 * found in 20 passes though the in use address list. (This probably
   1276 	 * seems long and complicated, but it's the best way to detect multiple
   1277 	 * devices at the same address - sometimes it takes a couple of tries
   1278 	 * before the collision is detected.)
   1279 	 */
   1280 
   1281 	/* initial scan through the devices */
   1282 	for (i = 1; i < 16; i++) {
   1283 		send_string[0] = 0;
   1284 		command = ADBTALK(i, 3);
   1285 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
   1286 		    (Ptr)0, (short)command);
   1287 
   1288 		if (send_string[0] != 0) {
   1289 			/* check for valid device handler */
   1290 			switch (send_string[2]) {
   1291 			case 0:
   1292 			case 0xfd:
   1293 			case 0xfe:
   1294 			case 0xff:
   1295 				continue;	/* invalid, skip */
   1296 			}
   1297 
   1298 			/* found a device */
   1299 			++ADBNumDevices;
   1300 			KASSERT(ADBNumDevices < 16);
   1301 			ADBDevTable[ADBNumDevices].devType =
   1302 				(int)send_string[2];
   1303 			ADBDevTable[ADBNumDevices].origAddr = i;
   1304 			ADBDevTable[ADBNumDevices].currentAddr = i;
   1305 			ADBDevTable[ADBNumDevices].DataAreaAddr =
   1306 			    (long)0;
   1307 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
   1308 			pm_check_adb_devices(i);	/* tell pm driver device
   1309 							 * is here */
   1310 		}
   1311 	}
   1312 
   1313 	/* find highest unused address */
   1314 	for (saveptr = 15; saveptr > 0; saveptr--)
   1315 		if (-1 == get_adb_info(&data, saveptr))
   1316 			break;
   1317 
   1318 #ifdef ADB_DEBUG
   1319 	if (adb_debug & 0x80) {
   1320 		printf_intr("first free is: 0x%02x\n", saveptr);
   1321 		printf_intr("devices: %i\n", ADBNumDevices);
   1322 	}
   1323 #endif
   1324 
   1325 	nonewtimes = 0;		/* no loops w/o new devices */
   1326 	while (saveptr > 0 && nonewtimes++ < 11) {
   1327 		for (i = 1; i <= ADBNumDevices; i++) {
   1328 			device = ADBDevTable[i].currentAddr;
   1329 #ifdef ADB_DEBUG
   1330 			if (adb_debug & 0x80)
   1331 				printf_intr("moving device 0x%02x to 0x%02x "
   1332 				    "(index 0x%02x)  ", device, saveptr, i);
   1333 #endif
   1334 
   1335 			/* send TALK R3 to address */
   1336 			command = ADBTALK(device, 3);
   1337 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1338 			    (Ptr)0, (short)command);
   1339 
   1340 			/* move device to higher address */
   1341 			command = ADBLISTEN(device, 3);
   1342 			send_string[0] = 2;
   1343 			send_string[1] = (u_char)(saveptr | 0x60);
   1344 			send_string[2] = 0xfe;
   1345 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1346 			    (Ptr)0, (short)command);
   1347 			delay(500);
   1348 
   1349 			/* send TALK R3 - anything at new address? */
   1350 			command = ADBTALK(saveptr, 3);
   1351 			adb_op_sync((Ptr)send_string, (Ptr)0,
   1352 			    (Ptr)0, (short)command);
   1353 			delay(500);
   1354 
   1355 			if (send_string[0] == 0) {
   1356 #ifdef ADB_DEBUG
   1357 				if (adb_debug & 0x80)
   1358 					printf_intr("failed, continuing\n");
   1359 #endif
   1360 				continue;
   1361 			}
   1362 
   1363 			/* send TALK R3 - anything at old address? */
   1364 			command = ADBTALK(device, 3);
   1365 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
   1366 			    (Ptr)0, (short)command);
   1367 			if (send_string[0] != 0) {
   1368 				/* check for valid device handler */
   1369 				switch (send_string[2]) {
   1370 				case 0:
   1371 				case 0xfd:
   1372 				case 0xfe:
   1373 				case 0xff:
   1374 					continue;	/* invalid, skip */
   1375 				}
   1376 
   1377 				/* new device found */
   1378 				/* update data for previously moved device */
   1379 				ADBDevTable[i].currentAddr = saveptr;
   1380 #ifdef ADB_DEBUG
   1381 				if (adb_debug & 0x80)
   1382 					printf_intr("old device at index %i\n",i);
   1383 #endif
   1384 				/* add new device in table */
   1385 #ifdef ADB_DEBUG
   1386 				if (adb_debug & 0x80)
   1387 					printf_intr("new device found\n");
   1388 #endif
   1389 				if (saveptr > ADBNumDevices) {
   1390 					++ADBNumDevices;
   1391 					KASSERT(ADBNumDevices < 16);
   1392 				}
   1393 				ADBDevTable[ADBNumDevices].devType =
   1394 					(int)send_string[2];
   1395 				ADBDevTable[ADBNumDevices].origAddr = device;
   1396 				ADBDevTable[ADBNumDevices].currentAddr = device;
   1397 				/* These will be set correctly in adbsys.c */
   1398 				/* Until then, unsol. data will be ignored. */
   1399 				ADBDevTable[ADBNumDevices].DataAreaAddr =
   1400 				    (long)0;
   1401 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
   1402 				    (void *)0;
   1403 				/* find next unused address */
   1404 				for (x = saveptr; x > 0; x--) {
   1405 					if (-1 == get_adb_info(&data, x)) {
   1406 						saveptr = x;
   1407 						break;
   1408 					}
   1409 				}
   1410 				if (x == 0)
   1411 					saveptr = 0;
   1412 #ifdef ADB_DEBUG
   1413 				if (adb_debug & 0x80)
   1414 					printf_intr("new free is 0x%02x\n",
   1415 					    saveptr);
   1416 #endif
   1417 				nonewtimes = 0;
   1418 				/* tell pm driver device is here */
   1419 				pm_check_adb_devices(device);
   1420 			} else {
   1421 #ifdef ADB_DEBUG
   1422 				if (adb_debug & 0x80)
   1423 					printf_intr("moving back...\n");
   1424 #endif
   1425 				/* move old device back */
   1426 				command = ADBLISTEN(saveptr, 3);
   1427 				send_string[0] = 2;
   1428 				send_string[1] = (u_char)(device | 0x60);
   1429 				send_string[2] = 0xfe;
   1430 				adb_op_sync((Ptr)send_string, (Ptr)0,
   1431 				    (Ptr)0, (short)command);
   1432 				delay(1000);
   1433 			}
   1434 		}
   1435 	}
   1436 
   1437 #ifdef ADB_DEBUG
   1438 	if (adb_debug) {
   1439 		for (i = 1; i <= ADBNumDevices; i++) {
   1440 			x = get_ind_adb_info(&data, i);
   1441 			if (x != -1)
   1442 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
   1443 				    i, x, data.devType);
   1444 		}
   1445 	}
   1446 #endif
   1447 
   1448 #ifndef MRG_ADB
   1449 	/* enable the programmer's switch, if we have one */
   1450 	adb_prog_switch_enable();
   1451 #endif
   1452 
   1453 #ifdef ADB_DEBUG
   1454 	if (adb_debug) {
   1455 		if (0 == ADBNumDevices)	/* tell user if no devices found */
   1456 			printf_intr("adb: no devices found\n");
   1457 	}
   1458 #endif
   1459 
   1460 	adbStarting = 0;	/* not starting anymore */
   1461 #ifdef ADB_DEBUG
   1462 	if (adb_debug)
   1463 		printf_intr("adb: ADBReInit complete\n");
   1464 #endif
   1465 
   1466 	if (adbHardware == ADB_HW_CUDA)
   1467 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
   1468 		    (void *)adb_cuda_tickle, NULL);
   1469 
   1470 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
   1471 		splx(s);
   1472 }
   1473 
   1474 /*
   1475  * adb_cmd_result
   1476  *
   1477  * This routine lets the caller know whether the specified adb command string
   1478  * should expect a returned result, such as a TALK command.
   1479  *
   1480  * returns: 0 if a result should be expected
   1481  *          1 if a result should NOT be expected
   1482  */
   1483 int
   1484 adb_cmd_result(u_char *in)
   1485 {
   1486 	switch (adbHardware) {
   1487 	case ADB_HW_II:
   1488 		/* was it an ADB talk command? */
   1489 		if ((in[1] & 0x0c) == 0x0c)
   1490 			return 0;
   1491 		return 1;
   1492 
   1493 	case ADB_HW_IISI:
   1494 	case ADB_HW_CUDA:
   1495 		/* was it an ADB talk command? */
   1496 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
   1497 			return 0;
   1498 		/* was it an RTC/PRAM read date/time? */
   1499 		if ((in[1] == 0x01) && (in[2] == 0x03))
   1500 			return 0;
   1501 		return 1;
   1502 
   1503 	case ADB_HW_PB:
   1504 		return 1;
   1505 
   1506 	case ADB_HW_UNKNOWN:
   1507 	default:
   1508 		return 1;
   1509 	}
   1510 }
   1511 
   1512 
   1513 /*
   1514  * adb_cmd_extra
   1515  *
   1516  * This routine lets the caller know whether the specified adb command string
   1517  * may have extra data appended to the end of it, such as a LISTEN command.
   1518  *
   1519  * returns: 0 if extra data is allowed
   1520  *          1 if extra data is NOT allowed
   1521  */
   1522 int
   1523 adb_cmd_extra(u_char *in)
   1524 {
   1525 	switch (adbHardware) {
   1526 		case ADB_HW_II:
   1527 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
   1528 			return 0;
   1529 		return 1;
   1530 
   1531 	case ADB_HW_IISI:
   1532 	case ADB_HW_CUDA:
   1533 		/*
   1534 		 * TO DO: support needs to be added to recognize RTC and PRAM
   1535 		 * commands
   1536 		 */
   1537 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
   1538 			return 0;
   1539 		/* add others later */
   1540 		return 1;
   1541 
   1542 	case ADB_HW_PB:
   1543 		return 1;
   1544 
   1545 	case ADB_HW_UNKNOWN:
   1546 	default:
   1547 		return 1;
   1548 	}
   1549 }
   1550 
   1551 /*
   1552  * adb_op_sync
   1553  *
   1554  * This routine does exactly what the adb_op routine does, except that after
   1555  * the adb_op is called, it waits until the return value is present before
   1556  * returning.
   1557  *
   1558  * NOTE: The user specified compRout is ignored, since this routine specifies
   1559  * it's own to adb_op, which is why you really called this in the first place
   1560  * anyway.
   1561  */
   1562 int
   1563 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
   1564 {
   1565 	int tmout;
   1566 	int result;
   1567 	volatile int flag = 0;
   1568 
   1569 	result = adb_op(buffer, (void *)adb_op_comprout,
   1570 	    (void *)&flag, command);	/* send command */
   1571 	if (result == 0) {		/* send ok? */
   1572 		/*
   1573 		 * Total time to wait is calculated as follows:
   1574 		 *  - Tlt (stop to start time): 260 usec
   1575 		 *  - start bit: 100 usec
   1576 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
   1577 		 *  - stop bit (with SRQ): 140 usec
   1578 		 * Total: 6900 usec
   1579 		 *
   1580 		 * This is the total time allowed by the specification.  Any
   1581 		 * device that doesn't conform to this will fail to operate
   1582 		 * properly on some Apple systems.  In spite of this we
   1583 		 * double the time to wait; some Cuda-based apparently
   1584 		 * queues some commands and allows the main CPU to continue
   1585 		 * processing (radical concept, eh?).  To be safe, allow
   1586 		 * time for two complete ADB transactions to occur.
   1587 		 */
   1588 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
   1589 			delay(10);
   1590 		if (!flag && tmout > 0)
   1591 			delay(tmout);
   1592 
   1593 		if (!flag)
   1594 			result = -2;
   1595 	}
   1596 
   1597 	return result;
   1598 }
   1599 
   1600 /*
   1601  * adb_op_comprout
   1602  *
   1603  * This function is used by the adb_op_sync routine so it knows when the
   1604  * function is done.
   1605  */
   1606 void
   1607 adb_op_comprout(buffer, compdata, cmd)
   1608 	caddr_t buffer, compdata;
   1609 	int cmd;
   1610 {
   1611 	short *p = (short *)compdata;
   1612 
   1613 	*p = 1;
   1614 }
   1615 
   1616 void
   1617 adb_setup_hw_type(void)
   1618 {
   1619 	switch (adbHardware) {
   1620 	case ADB_HW_CUDA:
   1621 		adbSoftPower = 1;
   1622 		return;
   1623 
   1624 	case ADB_HW_PB:
   1625 		adbSoftPower = 1;
   1626 		pm_setup_adb();
   1627 		return;
   1628 
   1629 	default:
   1630 		panic("unknown adb hardware");
   1631 	}
   1632 #if 0
   1633 	response = 0; /*mac68k_machine.machineid;*/
   1634 
   1635 	/*
   1636 	 * Determine what type of ADB hardware we are running on.
   1637 	 */
   1638 	switch (response) {
   1639 	case MACH_MACC610:		/* Centris 610 */
   1640 	case MACH_MACC650:		/* Centris 650 */
   1641 	case MACH_MACII:		/* II */
   1642 	case MACH_MACIICI:		/* IIci */
   1643 	case MACH_MACIICX:		/* IIcx */
   1644 	case MACH_MACIIX:		/* IIx */
   1645 	case MACH_MACQ610:		/* Quadra 610 */
   1646 	case MACH_MACQ650:		/* Quadra 650 */
   1647 	case MACH_MACQ700:		/* Quadra 700 */
   1648 	case MACH_MACQ800:		/* Quadra 800 */
   1649 	case MACH_MACSE30:		/* SE/30 */
   1650 		adbHardware = ADB_HW_II;
   1651 #ifdef ADB_DEBUG
   1652 		if (adb_debug)
   1653 			printf_intr("adb: using II series hardware support\n");
   1654 #endif
   1655 		break;
   1656 
   1657 	case MACH_MACCLASSICII:		/* Classic II */
   1658 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
   1659 	case MACH_MACLCIII:		/* LC III, Performa 450 */
   1660 	case MACH_MACIISI:		/* IIsi */
   1661 	case MACH_MACIIVI:		/* IIvi */
   1662 	case MACH_MACIIVX:		/* IIvx */
   1663 	case MACH_MACP460:		/* Performa 460/465/467 */
   1664 	case MACH_MACP600:		/* Performa 600 */
   1665 		adbHardware = ADB_HW_IISI;
   1666 #ifdef ADB_DEBUG
   1667 		if (adb_debug)
   1668 			printf_intr("adb: using IIsi series hardware support\n");
   1669 #endif
   1670 		break;
   1671 
   1672 	case MACH_MACPB140:		/* PowerBook 140 */
   1673 	case MACH_MACPB145:		/* PowerBook 145 */
   1674 	case MACH_MACPB150:		/* PowerBook 150 */
   1675 	case MACH_MACPB160:		/* PowerBook 160 */
   1676 	case MACH_MACPB165:		/* PowerBook 165 */
   1677 	case MACH_MACPB165C:		/* PowerBook 165c */
   1678 	case MACH_MACPB170:		/* PowerBook 170 */
   1679 	case MACH_MACPB180:		/* PowerBook 180 */
   1680 	case MACH_MACPB180C:		/* PowerBook 180c */
   1681 		adbHardware = ADB_HW_PB;
   1682 		pm_setup_adb();
   1683 #ifdef ADB_DEBUG
   1684 		if (adb_debug)
   1685 			printf_intr("adb: using PowerBook 100-series hardware support\n");
   1686 #endif
   1687 		break;
   1688 
   1689 	case MACH_MACPB210:		/* PowerBook Duo 210 */
   1690 	case MACH_MACPB230:		/* PowerBook Duo 230 */
   1691 	case MACH_MACPB250:		/* PowerBook Duo 250 */
   1692 	case MACH_MACPB270:		/* PowerBook Duo 270 */
   1693 	case MACH_MACPB280:		/* PowerBook Duo 280 */
   1694 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
   1695 	case MACH_MACPB500:		/* PowerBook 500 series */
   1696 		adbHardware = ADB_HW_PB;
   1697 		pm_setup_adb();
   1698 #ifdef ADB_DEBUG
   1699 		if (adb_debug)
   1700 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
   1701 #endif
   1702 		break;
   1703 
   1704 	case MACH_MACC660AV:		/* Centris 660AV */
   1705 	case MACH_MACCCLASSIC:		/* Color Classic */
   1706 	case MACH_MACCCLASSICII:	/* Color Classic II */
   1707 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
   1708 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
   1709 	case MACH_MACLC520:		/* LC 520 */
   1710 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
   1711 	case MACH_MACP550:		/* LC 550, Performa 550 */
   1712 	case MACH_MACP580:		/* Performa 580/588 */
   1713 	case MACH_MACQ605:		/* Quadra 605 */
   1714 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
   1715 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
   1716 	case MACH_MACQ840AV:		/* Quadra 840AV */
   1717 		adbHardware = ADB_HW_CUDA;
   1718 #ifdef ADB_DEBUG
   1719 		if (adb_debug)
   1720 			printf_intr("adb: using Cuda series hardware support\n");
   1721 #endif
   1722 		break;
   1723 	default:
   1724 		adbHardware = ADB_HW_UNKNOWN;
   1725 #ifdef ADB_DEBUG
   1726 		if (adb_debug) {
   1727 			printf_intr("adb: hardware type unknown for this machine\n");
   1728 			printf_intr("adb: ADB support is disabled\n");
   1729 		}
   1730 #endif
   1731 		break;
   1732 	}
   1733 
   1734 	/*
   1735 	 * Determine whether this machine has ADB based soft power.
   1736 	 */
   1737 	switch (response) {
   1738 	case MACH_MACCCLASSIC:		/* Color Classic */
   1739 	case MACH_MACCCLASSICII:	/* Color Classic II */
   1740 	case MACH_MACIISI:		/* IIsi */
   1741 	case MACH_MACIIVI:		/* IIvi */
   1742 	case MACH_MACIIVX:		/* IIvx */
   1743 	case MACH_MACLC520:		/* LC 520 */
   1744 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
   1745 	case MACH_MACP550:		/* LC 550, Performa 550 */
   1746 	case MACH_MACP600:		/* Performa 600 */
   1747 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
   1748 	case MACH_MACQ840AV:		/* Quadra 840AV */
   1749 		adbSoftPower = 1;
   1750 		break;
   1751 	}
   1752 #endif
   1753 }
   1754 
   1755 int
   1756 count_adbs(void)
   1757 {
   1758 	int i;
   1759 	int found;
   1760 
   1761 	found = 0;
   1762 
   1763 	for (i = 1; i < 16; i++)
   1764 		if (0 != ADBDevTable[i].devType)
   1765 			found++;
   1766 
   1767 	return found;
   1768 }
   1769 
   1770 int
   1771 get_ind_adb_info(ADBDataBlock * info, int index)
   1772 {
   1773 	if ((index < 1) || (index > 15))	/* check range 1-15 */
   1774 		return (-1);
   1775 
   1776 #ifdef ADB_DEBUG
   1777 	if (adb_debug & 0x80)
   1778 		printf_intr("index 0x%x devType is: 0x%x\n", index,
   1779 		    ADBDevTable[index].devType);
   1780 #endif
   1781 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
   1782 		return (-1);
   1783 
   1784 	info->devType = ADBDevTable[index].devType;
   1785 	info->origADBAddr = ADBDevTable[index].origAddr;
   1786 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
   1787 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
   1788 
   1789 	return (ADBDevTable[index].currentAddr);
   1790 }
   1791 
   1792 int
   1793 get_adb_info(ADBDataBlock * info, int adbAddr)
   1794 {
   1795 	int i;
   1796 
   1797 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1798 		return (-1);
   1799 
   1800 	for (i = 1; i < 15; i++)
   1801 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1802 			info->devType = ADBDevTable[i].devType;
   1803 			info->origADBAddr = ADBDevTable[i].origAddr;
   1804 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
   1805 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
   1806 			return 0;	/* found */
   1807 		}
   1808 
   1809 	return (-1);		/* not found */
   1810 }
   1811 
   1812 int
   1813 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
   1814 {
   1815 	int i;
   1816 
   1817 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1818 		return (-1);
   1819 
   1820 	for (i = 1; i < 15; i++)
   1821 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1822 			ADBDevTable[i].ServiceRtPtr =
   1823 			    (void *)(info->siServiceRtPtr);
   1824 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
   1825 			return 0;	/* found */
   1826 		}
   1827 
   1828 	return (-1);		/* not found */
   1829 
   1830 }
   1831 
   1832 #ifndef MRG_ADB
   1833 
   1834 /* caller should really use machine-independant version: getPramTime */
   1835 /* this version does pseudo-adb access only */
   1836 int
   1837 adb_read_date_time(unsigned long *time)
   1838 {
   1839 	u_char output[ADB_MAX_MSG_LENGTH];
   1840 	int result;
   1841 	volatile int flag = 0;
   1842 
   1843 	switch (adbHardware) {
   1844 	case ADB_HW_II:
   1845 		return -1;
   1846 
   1847 	case ADB_HW_IISI:
   1848 		output[0] = 0x02;	/* 2 byte message */
   1849 		output[1] = 0x01;	/* to pram/rtc device */
   1850 		output[2] = 0x03;	/* read date/time */
   1851 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
   1852 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
   1853 		if (result != 0)	/* exit if not sent */
   1854 			return -1;
   1855 
   1856 		while (0 == flag)	/* wait for result */
   1857 			;
   1858 
   1859 		*time = (long)(*(long *)(output + 1));
   1860 		return 0;
   1861 
   1862 	case ADB_HW_PB:
   1863 		pm_read_date_time(time);
   1864 		return 0;
   1865 
   1866 	case ADB_HW_CUDA:
   1867 		output[0] = 0x02;	/* 2 byte message */
   1868 		output[1] = 0x01;	/* to pram/rtc device */
   1869 		output[2] = 0x03;	/* read date/time */
   1870 		result = send_adb_cuda((u_char *)output, (u_char *)output,
   1871 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1872 		if (result != 0)	/* exit if not sent */
   1873 			return -1;
   1874 
   1875 		while (0 == flag)	/* wait for result */
   1876 			;
   1877 
   1878 		memcpy(time, output + 1, 4);
   1879 		return 0;
   1880 
   1881 	case ADB_HW_UNKNOWN:
   1882 	default:
   1883 		return -1;
   1884 	}
   1885 }
   1886 
   1887 /* caller should really use machine-independant version: setPramTime */
   1888 /* this version does pseudo-adb access only */
   1889 int
   1890 adb_set_date_time(unsigned long time)
   1891 {
   1892 	u_char output[ADB_MAX_MSG_LENGTH];
   1893 	int result;
   1894 	volatile int flag = 0;
   1895 
   1896 	switch (adbHardware) {
   1897 
   1898 	case ADB_HW_CUDA:
   1899 		output[0] = 0x06;	/* 6 byte message */
   1900 		output[1] = 0x01;	/* to pram/rtc device */
   1901 		output[2] = 0x09;	/* set date/time */
   1902 		output[3] = (u_char)(time >> 24);
   1903 		output[4] = (u_char)(time >> 16);
   1904 		output[5] = (u_char)(time >> 8);
   1905 		output[6] = (u_char)(time);
   1906 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1907 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1908 		if (result != 0)	/* exit if not sent */
   1909 			return -1;
   1910 
   1911 		while (0 == flag)	/* wait for send to finish */
   1912 			;
   1913 
   1914 		return 0;
   1915 
   1916 	case ADB_HW_PB:
   1917 		pm_set_date_time(time);
   1918 		return 0;
   1919 
   1920 	case ADB_HW_II:
   1921 	case ADB_HW_IISI:
   1922 	case ADB_HW_UNKNOWN:
   1923 	default:
   1924 		return -1;
   1925 	}
   1926 }
   1927 
   1928 
   1929 int
   1930 adb_poweroff(void)
   1931 {
   1932 	u_char output[ADB_MAX_MSG_LENGTH];
   1933 	int result;
   1934 
   1935 	if (!adbSoftPower)
   1936 		return -1;
   1937 
   1938 	adb_polling = 1;
   1939 
   1940 	switch (adbHardware) {
   1941 	case ADB_HW_IISI:
   1942 		output[0] = 0x02;	/* 2 byte message */
   1943 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1944 		output[2] = 0x0a;	/* set date/time */
   1945 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
   1946 		    (void *)0, (void *)0, (int)0);
   1947 		if (result != 0)	/* exit if not sent */
   1948 			return -1;
   1949 
   1950 		for (;;);		/* wait for power off */
   1951 
   1952 		return 0;
   1953 
   1954 	case ADB_HW_PB:
   1955 		pm_adb_poweroff();
   1956 
   1957 		for (;;);		/* wait for power off */
   1958 
   1959 		return 0;
   1960 
   1961 	case ADB_HW_CUDA:
   1962 		output[0] = 0x02;	/* 2 byte message */
   1963 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1964 		output[2] = 0x0a;	/* set date/time */
   1965 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1966 		    (void *)0, (void *)0, (int)0);
   1967 		if (result != 0)	/* exit if not sent */
   1968 			return -1;
   1969 
   1970 		for (;;);		/* wait for power off */
   1971 
   1972 		return 0;
   1973 
   1974 	case ADB_HW_II:			/* II models don't do ADB soft power */
   1975 	case ADB_HW_UNKNOWN:
   1976 	default:
   1977 		return -1;
   1978 	}
   1979 }
   1980 
   1981 int
   1982 adb_prog_switch_enable(void)
   1983 {
   1984 	u_char output[ADB_MAX_MSG_LENGTH];
   1985 	int result;
   1986 	volatile int flag = 0;
   1987 
   1988 	switch (adbHardware) {
   1989 	case ADB_HW_IISI:
   1990 		output[0] = 0x03;	/* 3 byte message */
   1991 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1992 		output[2] = 0x1c;	/* prog. switch control */
   1993 		output[3] = 0x01;	/* enable */
   1994 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
   1995 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
   1996 		if (result != 0)	/* exit if not sent */
   1997 			return -1;
   1998 
   1999 		while (0 == flag)	/* wait for send to finish */
   2000 			;
   2001 
   2002 		return 0;
   2003 
   2004 	case ADB_HW_PB:
   2005 		return -1;
   2006 
   2007 	case ADB_HW_II:		/* II models don't do prog. switch */
   2008 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
   2009 	case ADB_HW_UNKNOWN:
   2010 	default:
   2011 		return -1;
   2012 	}
   2013 }
   2014 
   2015 int
   2016 adb_prog_switch_disable(void)
   2017 {
   2018 	u_char output[ADB_MAX_MSG_LENGTH];
   2019 	int result;
   2020 	volatile int flag = 0;
   2021 
   2022 	switch (adbHardware) {
   2023 	case ADB_HW_IISI:
   2024 		output[0] = 0x03;	/* 3 byte message */
   2025 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2026 		output[2] = 0x1c;	/* prog. switch control */
   2027 		output[3] = 0x01;	/* disable */
   2028 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
   2029 			(void *)adb_op_comprout, (void *)&flag, (int)0);
   2030 		if (result != 0)	/* exit if not sent */
   2031 			return -1;
   2032 
   2033 		while (0 == flag)	/* wait for send to finish */
   2034 			;
   2035 
   2036 		return 0;
   2037 
   2038 	case ADB_HW_PB:
   2039 		return -1;
   2040 
   2041 	case ADB_HW_II:		/* II models don't do prog. switch */
   2042 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
   2043 	case ADB_HW_UNKNOWN:
   2044 	default:
   2045 		return -1;
   2046 	}
   2047 }
   2048 
   2049 int
   2050 CountADBs(void)
   2051 {
   2052 	return (count_adbs());
   2053 }
   2054 
   2055 void
   2056 ADBReInit(void)
   2057 {
   2058 	adb_reinit();
   2059 }
   2060 
   2061 int
   2062 GetIndADB(ADBDataBlock * info, int index)
   2063 {
   2064 	return (get_ind_adb_info(info, index));
   2065 }
   2066 
   2067 int
   2068 GetADBInfo(ADBDataBlock * info, int adbAddr)
   2069 {
   2070 	return (get_adb_info(info, adbAddr));
   2071 }
   2072 
   2073 int
   2074 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
   2075 {
   2076 	return (set_adb_info(info, adbAddr));
   2077 }
   2078 
   2079 int
   2080 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
   2081 {
   2082 	return (adb_op(buffer, compRout, data, commandNum));
   2083 }
   2084 
   2085 #endif
   2086 
   2087 int
   2088 setsoftadb()
   2089 {
   2090 	callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
   2091 	return 0;
   2092 }
   2093 
   2094 void
   2095 adb_cuda_autopoll()
   2096 {
   2097 	volatile int flag = 0;
   2098 	int result;
   2099 	u_char output[16];
   2100 
   2101 	output[0] = 0x03;	/* 3-byte message */
   2102 	output[1] = 0x01;	/* to pram/rtc device */
   2103 	output[2] = 0x01;	/* cuda autopoll */
   2104 	output[3] = 0x01;
   2105 	result = send_adb_cuda(output, output, adb_op_comprout, (void *)&flag,
   2106 			       0);
   2107 	if (result != 0)	/* exit if not sent */
   2108 		return;
   2109 
   2110 	while (flag == 0);	/* wait for result */
   2111 }
   2112 
   2113 void
   2114 adb_restart(void)
   2115 {
   2116 	int result;
   2117 	u_char output[16];
   2118 
   2119 	adb_polling = 1;
   2120 
   2121 	switch (adbHardware) {
   2122 	case ADB_HW_CUDA:
   2123 		output[0] = 0x02;	/* 2 byte message */
   2124 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2125 		output[2] = 0x11;	/* restart */
   2126 		result = send_adb_cuda(output, NULL, NULL, NULL, 0);
   2127 		if (result != 0)	/* exit if not sent */
   2128 			return;
   2129 		while (1);		/* not return */
   2130 
   2131 	case ADB_HW_PB:
   2132 		pm_adb_restart();
   2133 		while (1);		/* not return */
   2134 	}
   2135 }
   2136