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