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