Home | History | Annotate | Line # | Download | only in dev
adb_direct.c revision 1.11
      1 /*	$NetBSD: adb_direct.c,v 1.11 1998/02/21 00:37:07 scottr 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 /* This code is rather messy, but I don't have time right now
     36  * to clean it up as much as I would like.
     37  * But it works, so I'm happy. :-) jpw */
     38 
     39 /* TO DO:
     40  *  - We could reduce the time spent in the adb_intr_* routines
     41  *    by having them save the incoming and outgoing data directly
     42  *    in the adbInbound and adbOutbound queues, as it would reduce
     43  *    the number of times we need to copy the data around. It
     44  *    would also make the code more readable and easier to follow.
     45  *  - (Related to above) Use the header part of adbCommand to
     46  *    reduce the number of copies we have to do of the data.
     47  *  - (Related to above) Actually implement the adbOutbound queue.
     48  *    This is fairly easy once you switch all the intr routines
     49  *    over to using adbCommand structs directly.
     50  *  - There is a bug in the state machine of adb_intr_cuda
     51  *    code that causes hangs, especially on 030 machines, probably
     52  *    because of some timing issues. Because I have been unable to
     53  *    determine the exact cause of this bug, I used the timeout function
     54  *    to check for and recover from this condition. If anyone finds
     55  *    the actual cause of this bug, the calls to timeout and the
     56  *    adb_cuda_tickle routine can be removed.
     57  */
     58 
     59 #ifdef __NetBSD__
     60 #include "opt_adb.h"
     61 
     62 #include <sys/param.h>
     63 #include <sys/cdefs.h>
     64 #include <sys/systm.h>
     65 
     66 #include <machine/viareg.h>
     67 #include <machine/param.h>
     68 #include <machine/cpu.h>
     69 #include <machine/adbsys.h>			/* required for adbvar.h */
     70 
     71 #include <mac68k/mac68k/macrom.h>
     72 #include <mac68k/dev/adb_direct.h>
     73 #include <mac68k/dev/adbvar.h>
     74 #define printf_intr printf
     75 #else
     76 #include "via.h"				/* for macos based testing */
     77 /* #define DEBUG */				/* more verbose for testing */
     78 #endif
     79 
     80 #ifdef DEBUG
     81 #ifndef ADB_DEBUG
     82 #define ADB_DEBUG
     83 #endif
     84 #endif
     85 
     86 /* some misc. leftovers */
     87 #define vPB		0x0000
     88 #define vPB3		0x08
     89 #define vPB4		0x10
     90 #define vPB5		0x20
     91 #define vSR_INT		0x04
     92 #define vSR_OUT		0x10
     93 
     94 /* types of adb hardware that we (will eventually) support */
     95 #define ADB_HW_UNKNOWN		0x01	/* don't know */
     96 #define ADB_HW_II		0x02	/* Mac II series */
     97 #define ADB_HW_IISI		0x03	/* Mac IIsi series */
     98 #define ADB_HW_PB		0x04	/* PowerBook series */
     99 #define ADB_HW_CUDA		0x05	/* Machines with a Cuda chip */
    100 
    101 /* the type of ADB action that we are currently preforming */
    102 #define ADB_ACTION_NOTREADY	0x01	/* has not been initialized yet */
    103 #define ADB_ACTION_IDLE		0x02	/* the bus is currently idle */
    104 #define ADB_ACTION_OUT		0x03	/* sending out a command */
    105 #define ADB_ACTION_IN		0x04	/* receiving data */
    106 
    107 /*
    108  * These describe the state of the ADB bus itself, although they
    109  * don't necessarily correspond directly to ADB states.
    110  * Note: these are not really used in the IIsi code.
    111  */
    112 #define ADB_BUS_UNKNOWN		0x01	/* we don't know yet - all models */
    113 #define ADB_BUS_IDLE		0x02	/* bus is idle - all models */
    114 #define ADB_BUS_CMD		0x03	/* starting a command - II models */
    115 #define ADB_BUS_ODD		0x04	/* the "odd" state - II models */
    116 #define ADB_BUS_EVEN		0x05	/* the "even" state - II models */
    117 #define ADB_BUS_ACTIVE		0x06	/* active state - IIsi models */
    118 #define ADB_BUS_ACK		0x07	/* currently ACKing - IIsi models */
    119 
    120 /*
    121  * Shortcuts for setting or testing the VIA bit states.
    122  * Not all shortcuts are used for every type of ADB hardware.
    123  */
    124 #define ADB_SET_STATE_IDLE_II()		via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
    125 #define ADB_SET_STATE_IDLE_IISI()	via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
    126 #define ADB_SET_STATE_IDLE_CUDA()	via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
    127 #define ADB_SET_STATE_CMD()		via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
    128 #define ADB_SET_STATE_EVEN()		via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
    129 						vBufB) | vPB4) & ~vPB5)
    130 #define ADB_SET_STATE_ODD()		via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
    131 						vBufB) | vPB5) & ~vPB4 )
    132 #define ADB_SET_STATE_ACTIVE() 		via_reg(VIA1, vBufB) |= vPB5
    133 #define ADB_SET_STATE_INACTIVE()	via_reg(VIA1, vBufB) &= ~vPB5
    134 #define ADB_SET_STATE_TIP()		via_reg(VIA1, vBufB) &= ~vPB5
    135 #define ADB_CLR_STATE_TIP() 		via_reg(VIA1, vBufB) |= vPB5
    136 #define ADB_SET_STATE_ACKON()		via_reg(VIA1, vBufB) |= vPB4
    137 #define ADB_SET_STATE_ACKOFF()		via_reg(VIA1, vBufB) &= ~vPB4
    138 #define ADB_TOGGLE_STATE_ACK_CUDA()	via_reg(VIA1, vBufB) ^= vPB4
    139 #define ADB_SET_STATE_ACKON_CUDA()	via_reg(VIA1, vBufB) &= ~vPB4
    140 #define ADB_SET_STATE_ACKOFF_CUDA()	via_reg(VIA1, vBufB) |= vPB4
    141 #define ADB_SET_SR_INPUT()		via_reg(VIA1, vACR) &= ~vSR_OUT
    142 #define ADB_SET_SR_OUTPUT()		via_reg(VIA1, vACR) |= vSR_OUT
    143 #define ADB_SR()			via_reg(VIA1, vSR)
    144 #define ADB_VIA_INTR_ENABLE()		via_reg(VIA1, vIER) = 0x84
    145 #define ADB_VIA_INTR_DISABLE()		via_reg(VIA1, vIER) = 0x04
    146 #define ADB_VIA_CLR_INTR()		via_reg(VIA1, vIFR) = 0x04
    147 #define ADB_INTR_IS_OFF			(vPB3 == (via_reg(VIA1, vBufB) & vPB3))
    148 #define ADB_INTR_IS_ON			(0 == (via_reg(VIA1, vBufB) & vPB3))
    149 #define ADB_SR_INTR_IS_OFF		(0 == (via_reg(VIA1, vIFR) & vSR_INT))
    150 #define ADB_SR_INTR_IS_ON		(vSR_INT == (via_reg(VIA1, \
    151 						vIFR) & vSR_INT))
    152 
    153 /*
    154  * This is the delay that is required (in uS) between certain
    155  * ADB transactions. The actual timing delay for for each uS is
    156  * calculated at boot time to account for differences in machine speed.
    157  */
    158 #define ADB_DELAY	150
    159 
    160 /*
    161  * Maximum ADB message length; includes space for data, result, and
    162  * device code - plus a little for safety.
    163  */
    164 #define ADB_MAX_MSG_LENGTH	16
    165 #define ADB_MAX_HDR_LENGTH	8
    166 
    167 #define ADB_QUEUE		32
    168 #define ADB_TICKLE_TICKS	4
    169 
    170 /*
    171  * A structure for storing information about each ADB device.
    172  */
    173 struct ADBDevEntry {
    174         void    (*ServiceRtPtr) __P((void));
    175         void    *DataAreaAddr;
    176         char    devType;
    177         char    origAddr;
    178         char    currentAddr;
    179 };
    180 
    181 /*
    182  * Used to hold ADB commands that are waiting to be sent out.
    183  */
    184 struct adbCmdHoldEntry {
    185 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
    186 	u_char	*saveBuf;	/* buffer to know where to save result */
    187 	u_char	*compRout;	/* completion routine pointer */
    188 	u_char	*data;		/* completion routine data pointer */
    189 };
    190 
    191 /*
    192  * Eventually used for two separate queues, the queue between
    193  * the upper and lower halves, and the outgoing packet queue.
    194  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
    195  */
    196 struct adbCommand {
    197 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
    198 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
    199 	u_char	*saveBuf;	/* where to save result */
    200 	u_char	*compRout;	/* completion routine pointer */
    201 	u_char	*compData;	/* completion routine data pointer */
    202 	u_int	cmd;		/* the original command for this data */
    203 	u_int	unsol;		/* 1 if packet was unsolicited */
    204 	u_int	ack_only;	/* 1 for no special processing */
    205 };
    206 
    207 /*
    208  * A few variables that we need and their initial values.
    209  */
    210 int	adbHardware = ADB_HW_UNKNOWN;
    211 int	adbActionState = ADB_ACTION_NOTREADY;
    212 int	adbBusState = ADB_BUS_UNKNOWN;
    213 int	adbWaiting = 0;		/* waiting for return data from the device */
    214 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
    215 int	adbOutQueueHasData = 0;	/* something in the queue waiting to go out */
    216 int	adbNextEnd = 0;		/* the next incoming bute is the last (II) */
    217 int	adbSoftPower = 0;	/* machine supports soft power */
    218 
    219 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
    220 u_char	*adbBuffer = (long) 0;	/* pointer to user data area */
    221 void	*adbCompRout = (long) 0;	/* pointer to the completion routine */
    222 void	*adbCompData = (long) 0;	/* pointer to the completion routine data */
    223 long	adbFakeInts = 0;	/* keeps track of fake ADB interrupts for
    224 				 * timeouts (II) */
    225 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
    226 int	adbSendTalk = 0;	/* the intr routine is sending the talk, not
    227 				 * the user (II) */
    228 int	adbPolling = 0;		/* we are polling for service request */
    229 int	adbPollCmd = 0;		/* the last poll command we sent */
    230 
    231 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
    232 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
    233 struct	adbCmdHoldEntry adbOutQueue;		/* our 1 entry output queue */
    234 
    235 int	adbSentChars = 0;	/* how many characters we have sent */
    236 int	adbLastDevice = 0;	/* last ADB dev we heard from (II ONLY) */
    237 int	adbLastDevIndex = 0;	/* last ADB dev loc in dev table (II ONLY) */
    238 int	adbLastCommand = 0;	/* the last ADB command we sent (II) */
    239 
    240 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
    241 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
    242 
    243 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
    244 int	adbInCount=0;			/* how many packets in in queue */
    245 int	adbInHead=0;			/* head of in queue */
    246 int	adbInTail=0;			/* tail of in queue */
    247 struct	adbCommand adbOutbound[ADB_QUEUE];	/* outgoing queue - not used yet */
    248 int	adbOutCount=0;			/* how many packets in out queue */
    249 int	adbOutHead=0;			/* head of out queue */
    250 int	adbOutTail=0;			/* tail of out queue */
    251 
    252 int	tickle_count=0;			/* how many tickles seen for this packet? */
    253 int	tickle_serial=0;		/* the last packet tickled */
    254 int	adb_cuda_serial=0;		/* the current packet */
    255 
    256 extern struct mac68k_machine_S mac68k_machine;
    257 extern int adb_polling;
    258 
    259 int	zshard __P((int));
    260 
    261 void	pm_setup_adb __P((void));
    262 void	pm_check_adb_devices __P((int));
    263 void	pm_intr __P((void));
    264 int	pm_adb_op __P((u_char *, void *, void *, int));
    265 void	pm_init_adb_device __P((void));
    266 
    267 /*
    268  * The following are private routines.
    269  */
    270 void	print_single __P((u_char *));
    271 void	adb_intr __P((void));
    272 void	adb_intr_II __P((void));
    273 void	adb_intr_IIsi __P((void));
    274 void	adb_intr_cuda __P((void));
    275 void	adb_soft_intr __P((void));
    276 int	send_adb_II __P((u_char *, u_char *, void *, void *, int));
    277 int	send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
    278 int	send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
    279 void	adb_intr_cuda_test __P((void));
    280 void	adb_cuda_tickle __P((void));
    281 void	adb_pass_up __P((struct adbCommand *));
    282 void	adb_op_comprout __P((void));
    283 void	adb_reinit __P((void));
    284 int	count_adbs __P((void));
    285 int	get_ind_adb_info __P((ADBDataBlock *, int));
    286 int	get_adb_info __P((ADBDataBlock *, int));
    287 int	set_adb_info __P((ADBSetInfoBlock *, int));
    288 void	adb_setup_hw_type __P((void));
    289 int	adb_op __P((Ptr, Ptr, Ptr, short));
    290 int	adb_op_sync __P((Ptr, Ptr, Ptr, short));
    291 void	adb_read_II __P((u_char *));
    292 void	adb_hw_setup __P((void));
    293 void	adb_hw_setup_IIsi __P((u_char *));
    294 void    adb_comp_exec __P((void));
    295 int	adb_cmd_result __P((u_char *));
    296 int	adb_cmd_extra __P((u_char *));
    297 int	adb_guess_next_device __P((void));
    298 int	adb_prog_switch_enable __P((void));
    299 int	adb_prog_switch_disable __P((void));
    300 /* we should create this and it will be the public version */
    301 int	send_adb __P((u_char *, void *, void *));
    302 
    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(thestring)
    311 	u_char *thestring;
    312 {
    313 	int x;
    314 
    315 	if ((int) (thestring[0]) == 0) {
    316 		printf_intr("nothing returned\n");
    317 		return;
    318 	}
    319 	if (thestring == 0) {
    320 		printf_intr("no data - null pointer\n");
    321 		return;
    322 	}
    323 	if (thestring[0] > 20) {
    324 		printf_intr("ADB: ACK > 20 no way!\n");
    325 		thestring[0] = 20;
    326 	}
    327 	printf_intr("(length=0x%x):", thestring[0]);
    328 	for (x = 0; x < thestring[0]; x++)
    329 		printf_intr("  0x%02x", thestring[x + 1]);
    330 	printf_intr("\n");
    331 }
    332 
    333 void
    334 adb_cuda_tickle(void)
    335 {
    336 	volatile int s;
    337 
    338 	if (adbActionState==ADB_ACTION_IN) {
    339 		if (tickle_serial==adb_cuda_serial) {
    340 			if (++tickle_count>0) {
    341 				s=splhigh();
    342 				adbActionState = ADB_ACTION_IDLE;
    343 				adbInputBuffer[0] = 0;
    344 				ADB_SET_STATE_IDLE_CUDA();
    345 				splx(s);
    346 			}
    347 		} else {
    348 			tickle_serial=adb_cuda_serial;
    349 			tickle_count=0;
    350 		}
    351 	} else {
    352 		tickle_serial=adb_cuda_serial;
    353 		tickle_count=0;
    354 	}
    355 
    356 	timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
    357 }
    358 
    359 /*
    360  * called when when an adb interrupt happens
    361  *
    362  * Cuda version of adb_intr
    363  * TO DO: do we want to add some zshard calls in here?
    364  */
    365 void
    366 adb_intr_cuda(void)
    367 {
    368 	volatile int i, ending;
    369 	volatile unsigned int s;
    370 	struct adbCommand packet;
    371 
    372 	s = splhigh();		/* can't be too careful - might be called */
    373 	/* from a routine, NOT an interrupt */
    374 
    375 	ADB_VIA_CLR_INTR();	/* clear interrupt */
    376 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
    377 
    378 switch_start:
    379 	switch (adbActionState) {
    380 	case ADB_ACTION_IDLE:
    381 		/* This is an unexpected packet, so grab the first (dummy)
    382 		 * byte, set up the proper vars, and tell the chip we are
    383 		 * starting to receive the packet by setting the TIP bit. */
    384 		adbInputBuffer[1] = ADB_SR();
    385 		adb_cuda_serial++;
    386 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
    387 			break;
    388 
    389 		ADB_SET_SR_INPUT();
    390 		ADB_SET_STATE_TIP();
    391 
    392 		adbInputBuffer[0] = 1;
    393 		adbActionState = ADB_ACTION_IN;
    394 #ifdef ADB_DEBUG
    395 		if (adb_debug)
    396 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
    397 #endif
    398 		break;
    399 
    400 	case ADB_ACTION_IN:
    401 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
    402 		/* intr off means this is the last byte (end of frame) */
    403 		if (ADB_INTR_IS_OFF)
    404 			ending = 1;
    405 		else
    406 			ending = 0;
    407 
    408 		if (1 == ending) {	/* end of message? */
    409 #ifdef ADB_DEBUG
    410 			if (adb_debug) {
    411 				printf_intr("in end 0x%02x ",
    412 				    adbInputBuffer[adbInputBuffer[0]]);
    413 				print_single(adbInputBuffer);
    414 			}
    415 #endif
    416 
    417 			/* Are we waiting AND does this packet match what we
    418 			 * are waiting for AND is it coming from either the
    419 			 * ADB or RTC/PRAM sub-device? This section _should_
    420 			 * recognize all ADB and RTC/PRAM type commands, but
    421 			 * there may be more... NOTE: commands are always at
    422 			 * [4], even for RTC/PRAM commands. */
    423 			/* set up data for adb_pass_up */
    424 			for (i=0; i<=adbInputBuffer[0]; i++)
    425 				packet.data[i]=adbInputBuffer[i];
    426 
    427 			if ((adbWaiting == 1) &&
    428 			    (adbInputBuffer[4] == adbWaitingCmd) &&
    429 			    ((adbInputBuffer[2] == 0x00) ||
    430 			    (adbInputBuffer[2] == 0x01))) {
    431 
    432 				packet.saveBuf=adbBuffer;
    433 				packet.compRout=adbCompRout;
    434 				packet.compData=adbCompData;
    435 				packet.unsol=0;
    436 				packet.ack_only=0;
    437 				adb_pass_up(&packet);
    438 
    439 				adbWaitingCmd = 0;	/* reset "waiting" vars */
    440 				adbWaiting = 0;
    441 				adbBuffer = (long) 0;
    442 				adbCompRout = (long) 0;
    443 				adbCompData = (long) 0;
    444 			} else {
    445 				packet.unsol=1;
    446 				packet.ack_only=0;
    447 				adb_pass_up(&packet);
    448 			}
    449 
    450 
    451 			/* reset vars and signal the end of this frame */
    452 			adbActionState = ADB_ACTION_IDLE;
    453 			adbInputBuffer[0] = 0;
    454 			ADB_SET_STATE_IDLE_CUDA();
    455 			/*ADB_SET_SR_INPUT();*/
    456 
    457 			/*
    458 			 * If there is something waiting to be sent out,
    459 			 * the set everything up and send the first byte.
    460 			 */
    461 			if (adbWriteDelay == 1) {
    462 				delay(ADB_DELAY);	/* required */
    463 				adbSentChars = 0;
    464 				adbActionState = ADB_ACTION_OUT;
    465 				/*
    466 				 * If the interrupt is on, we were too slow
    467 				 * and the chip has already started to send
    468 				 * something to us, so back out of the write
    469 				 * and start a read cycle.
    470 				 */
    471 				if (ADB_INTR_IS_ON) {
    472 					ADB_SET_SR_INPUT();
    473 					ADB_SET_STATE_IDLE_CUDA();
    474 					adbSentChars = 0;
    475 					adbActionState = ADB_ACTION_IDLE;
    476 					adbInputBuffer[0] = 0;
    477 					break;
    478 				}
    479 				/*
    480 				 * If we got here, it's ok to start sending
    481 				 * so load the first byte and tell the chip
    482 				 * we want to send.
    483 				 */
    484 				ADB_SET_STATE_TIP();
    485 				ADB_SET_SR_OUTPUT();
    486 				ADB_SR() = adbOutputBuffer[adbSentChars + 1];
    487 			}
    488 		} else {
    489 			ADB_TOGGLE_STATE_ACK_CUDA();
    490 #ifdef ADB_DEBUG
    491 			if (adb_debug)
    492 				printf_intr("in 0x%02x ",
    493 				    adbInputBuffer[adbInputBuffer[0]]);
    494 #endif
    495 		}
    496 		break;
    497 
    498 	case ADB_ACTION_OUT:
    499 		i = ADB_SR();	/* reset SR-intr in IFR */
    500 #ifdef ADB_DEBUG
    501 		if (adb_debug)
    502 			printf_intr("intr out 0x%02x ", i);
    503 #endif
    504 
    505 		adbSentChars++;
    506 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
    507 #ifdef ADB_DEBUG
    508 			if (adb_debug)
    509 				printf_intr("intr was on ");
    510 #endif
    511 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    512 			ADB_SET_STATE_IDLE_CUDA();
    513 			adbSentChars = 0;	/* must start all over */
    514 			adbActionState = ADB_ACTION_IDLE;	/* new state */
    515 			adbInputBuffer[0] = 0;
    516 			adbWriteDelay = 1;	/* must retry when done with
    517 						 * read */
    518 			delay(ADB_DELAY);
    519 			goto switch_start;	/* process next state right
    520 						 * now */
    521 			break;
    522 		}
    523 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
    524 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
    525 									 * back? */
    526 				adbWaiting = 1;	/* signal waiting for return */
    527 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
    528 			} else {/* no talk, so done */
    529                                 /* set up stuff for adb_pass_up */
    530                                 for (i=0; i<=adbInputBuffer[0]; i++)
    531                                         packet.data[i]=adbInputBuffer[i];
    532                                 packet.saveBuf=adbBuffer;
    533                                 packet.compRout=adbCompRout;
    534                                 packet.compData=adbCompData;
    535                                 packet.cmd=adbWaitingCmd;
    536                                 packet.unsol=0;
    537                                 packet.ack_only=1;
    538                                 adb_pass_up(&packet);
    539 
    540                                 /* reset "waiting" vars, just in case */
    541                                 adbWaitingCmd = 0;
    542                                 adbBuffer = (long) 0;
    543                                 adbCompRout = (long) 0;
    544                                 adbCompData = (long) 0;
    545 			}
    546 
    547 			adbWriteDelay = 0;	/* done writing */
    548 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
    549 			ADB_SET_SR_INPUT();
    550 			ADB_SET_STATE_IDLE_CUDA();
    551 #ifdef ADB_DEBUG
    552 			if (adb_debug)
    553 				printf_intr("write done ");
    554 #endif
    555 		} else {
    556 			ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* send next byte */
    557 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
    558 							 * shift */
    559 #ifdef ADB_DEBUG
    560 			if (adb_debug)
    561 				printf_intr("toggle ");
    562 #endif
    563 		}
    564 		break;
    565 
    566 	case ADB_ACTION_NOTREADY:
    567 		printf_intr("adb: not yet initialized\n");
    568 		break;
    569 
    570 	default:
    571 		printf_intr("intr: unknown ADB state\n");
    572 	}
    573 
    574 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
    575 
    576 	splx(s);		/* restore */
    577 
    578 	return;
    579 }				/* end adb_intr_cuda */
    580 
    581 
    582 int
    583 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
    584 	command)
    585 {
    586 	int i, s, len;
    587 
    588 #ifdef ADB_DEBUG
    589 	if (adb_debug)
    590 		printf_intr("SEND\n");
    591 #endif
    592 
    593 	if (adbActionState == ADB_ACTION_NOTREADY)
    594 		return 1;
    595 
    596 	s = splhigh();		/* don't interrupt while we are messing with
    597 				 * the ADB */
    598 
    599 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
    600 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupt? */
    601 	} else
    602 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
    603 			adbWriteDelay = 1;	/* if no, then we'll "queue"
    604 						 * it up */
    605 		else {
    606 			splx(s);
    607 			return 1;	/* really busy! */
    608 		}
    609 
    610 #ifdef ADB_DEBUG
    611 	if (adb_debug)
    612 		printf_intr("QUEUE\n");
    613 #endif
    614 	if ((long) in == (long) 0) {	/* need to convert? */
    615 		/* don't need to use adb_cmd_extra here because this section
    616 		 * will be called */
    617 		/* ONLY when it is an ADB command (no RTC or PRAM) */
    618 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
    619 						 * doing a listen! */
    620 			len = buffer[0];	/* length of additional data */
    621 		else
    622 			len = 0;/* no additional data */
    623 
    624 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
    625 						 * data */
    626 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
    627 		adbOutputBuffer[2] = (u_char) command;	/* load command */
    628 
    629 		for (i = 1; i <= len; i++)	/* copy additional output
    630 						 * data, if any */
    631 			adbOutputBuffer[2 + i] = buffer[i];
    632 	} else
    633 		for (i = 0; i <= (adbOutputBuffer[0] + 1); i++)
    634 			adbOutputBuffer[i] = in[i];
    635 
    636 	adbSentChars = 0;	/* nothing sent yet */
    637 	adbBuffer = buffer;	/* save buffer to know where to save result */
    638 	adbCompRout = compRout;	/* save completion routine pointer */
    639 	adbCompData = data;	/* save completion routine data pointer */
    640 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
    641 
    642 	if (adbWriteDelay != 1) {	/* start command now? */
    643 #ifdef ADB_DEBUG
    644 		if (adb_debug)
    645 			printf_intr("out start NOW");
    646 #endif
    647 		delay(ADB_DELAY);
    648 		adbActionState = ADB_ACTION_OUT;	/* set next state */
    649 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
    650 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
    651 		ADB_SET_STATE_ACKOFF_CUDA();
    652 		ADB_SET_STATE_TIP();	/* tell ADB that we want to send */
    653 	}
    654 	adbWriteDelay = 1;	/* something in the write "queue" */
    655 
    656 	splx(s);
    657 
    658 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked ? */
    659 		/* poll until byte done */
    660 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
    661 		    || (adbWaiting == 1))
    662 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
    663 				adb_intr_cuda();	/* go process
    664 							 * "interrupt" */
    665 				adb_soft_intr();
    666 				}
    667 
    668 	return 0;
    669 }				/* send_adb_cuda */
    670 
    671 
    672 /* TO DO: add one or two zshard calls in here */
    673 void
    674 adb_intr_II(void)
    675 {
    676 	struct adbCommand packet;
    677 	int i, intr_on = 0;
    678 	int send = 0, do_srq = 0;
    679 	unsigned int s;
    680 
    681 	s = splhigh();		/* can't be too careful - might be called */
    682 	/* from a routine, NOT an interrupt */
    683 
    684 	ADB_VIA_CLR_INTR();	/* clear interrupt */
    685 
    686 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
    687 
    688 /*if (ADB_INTR_IS_ON)*/
    689 /*	printf_intr("INTR ON ");*/
    690 	if (ADB_INTR_IS_ON)
    691 		intr_on = 1;	/* save for later */
    692 
    693 	switch (adbActionState) {
    694 	case ADB_ACTION_IDLE:
    695 		if (!intr_on) {
    696 			/* printf_intr("FAKE DROPPED \n"); */
    697 			/* printf_intr(" XX "); */
    698 			i = ADB_SR();
    699 			break;
    700 		}
    701 		adbNextEnd = 0;
    702 		/* printf_intr("idle "); */
    703 		adbInputBuffer[0] = 1;
    704 		adbInputBuffer[1] = ADB_SR();	/* get first byte */
    705 		/* printf_intr("0x%02x ", adbInputBuffer[1]); */
    706 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    707 		adbActionState = ADB_ACTION_IN;	/* set next state */
    708 		ADB_SET_STATE_EVEN();	/* set bus state to even */
    709 		adbBusState = ADB_BUS_EVEN;
    710 		break;
    711 
    712 	case ADB_ACTION_IN:
    713 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();	/* get byte */
    714 		/* printf_intr("in 0x%02x ",
    715 		 * adbInputBuffer[adbInputBuffer[0]]); */
    716 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    717 
    718 		/*
    719 		 * Check for an unsolicited Service Request (SRQ).
    720 		 * An empty SRQ packet NEVER ends, so we must manually
    721 		 * check for the following condition.
    722 		 */
    723 		if (adbInputBuffer[0] == 4 && adbInputBuffer[2] == 0xff &&
    724 		    adbInputBuffer[3] == 0xff && adbInputBuffer[4] == 0xff &&
    725 		    intr_on && !adbNextEnd)
    726 			do_srq = 1;
    727 
    728 		if (adbNextEnd == 1) {	/* process last byte of packet */
    729 			adbNextEnd = 0;
    730 			/* printf_intr("done: "); */
    731 
    732 			/* If the following conditions are true (4 byte
    733 			 * message, last 3 bytes are 0xff) then we basically
    734 			 * got a "no response" from the ADB chip, so change
    735 			 * the message to an empty one. We also clear intr_on
    736 			 * to stop the SRQ send later on because these packets
    737 			 * normally have the SRQ bit set even when there is
    738 			 * NOT a pending SRQ. */
    739 			if (adbInputBuffer[0] == 4 && adbInputBuffer[2] == 0xff &&
    740 			    adbInputBuffer[3] == 0xff && adbInputBuffer[4] == 0xff) {
    741 				/* printf_intr("NO RESP "); */
    742 				intr_on = 0;
    743 				adbInputBuffer[0] = 0;
    744 			}
    745 			adbLastDevice = (adbInputBuffer[1] & 0xf0) >> 4;
    746 
    747                         /* set up data for adb_pass_up */
    748                         for (i=0; i<=adbInputBuffer[0]; i++)
    749                                 packet.data[i]=adbInputBuffer[i];
    750 
    751 			if ((!adbWaiting || adbPolling)
    752 			    && (adbInputBuffer[0] != 0)) {
    753 				packet.unsol=1;
    754 				packet.ack_only=0;
    755 				adb_pass_up(&packet);
    756 			} else
    757 				if (!adbPolling) {	/* someone asked for it */
    758                                 	packet.saveBuf=adbBuffer;
    759                                 	packet.compRout=adbCompRout;
    760                                 	packet.compData=adbCompData;
    761                                 	packet.unsol=0;
    762                                 	packet.ack_only=0;
    763                                 	adb_pass_up(&packet);
    764 				}
    765 			adbWaiting = 0;
    766 			adbPolling = 0;
    767 			adbInputBuffer[0] = 0;
    768 			adbBuffer = (long) 0;
    769 			adbCompRout = (long) 0;
    770 			adbCompData = (long) 0;
    771 			/*
    772 			 * Since we are done, check whether there is any data
    773 			 * waiting to do out. If so, start the sending the data.
    774 			 */
    775 			if (adbOutQueueHasData == 1) {
    776 				/* printf_intr("XXX: DOING OUT QUEUE\n"); */
    777 				/* copy over data */
    778 				for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
    779 					adbOutputBuffer[i] = adbOutQueue.outBuf[i];
    780 				adbBuffer = adbOutQueue.saveBuf;	/* user data area */
    781 				adbCompRout = adbOutQueue.compRout;	/* completion routine */
    782 				adbCompData = adbOutQueue.data;	/* comp. rout. data */
    783 				adbOutQueueHasData = 0;	/* currently processing
    784 							 * "queue" entry */
    785 				adbPolling = 0;
    786 				send = 1;
    787 				/* if intr_on is true, then it's a SRQ so poll
    788 				 * other devices. */
    789 			} else
    790 				if (intr_on) {
    791 					/* printf_intr("starting POLL "); */
    792 					do_srq = 1;
    793 					adbPolling = 1;
    794 				} else
    795 					if ((adbInputBuffer[1] & 0x0f) != 0x0c) {
    796 						/* printf_intr("xC HACK "); */
    797 						adbPolling = 1;
    798 						send = 1;
    799 						adbOutputBuffer[0] = 1;
    800 						adbOutputBuffer[1] = (adbInputBuffer[1] & 0xf0) | 0x0c;
    801 					} else {
    802 						/* printf_intr("ending "); */
    803 						adbBusState = ADB_BUS_IDLE;
    804 						adbActionState = ADB_ACTION_IDLE;
    805 						ADB_SET_STATE_IDLE_II();
    806 						break;
    807 					}
    808 		}
    809 		/*
    810 		 * If do_srq is true then something above determined that
    811 		 * the message has ended and some device is sending a
    812 		 * service request. So we need to determine the next device
    813 		 * and send a poll to it. (If the device we send to isn't the
    814 		 * one that sent the SRQ, that ok as it will be caught
    815 		 * the next time though.)
    816 		 */
    817 		if (do_srq) {
    818 			/* printf_intr("SRQ! "); */
    819 			adbPolling = 1;
    820 			adb_guess_next_device();
    821 			adbOutputBuffer[0] = 1;
    822 			adbOutputBuffer[1] = ((adbLastDevice & 0x0f) << 4) | 0x0c;
    823 			send = 1;
    824 		}
    825 		/*
    826 		 * If send is true then something above determined that
    827 		 * the message has ended and we need to start sending out
    828 		 * a new message immediately. This could be because there
    829 		 * is data waiting to go out or because an SRQ was seen.
    830 		 */
    831 		if (send) {
    832 			adbNextEnd = 0;
    833 			adbSentChars = 0;	/* nothing sent yet */
    834 			adbActionState = ADB_ACTION_OUT;	/* set next state */
    835 			ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
    836 			ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
    837 			adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
    838 			ADB_SET_STATE_CMD();	/* tell ADB that we want to
    839 						 * send */
    840 			break;
    841 		}
    842 		/* We only get this far if the message hasn't ended yet. */
    843 		if (!intr_on)	/* if adb intr. on then the */
    844 			adbNextEnd = 1;	/* NEXT byte is the last */
    845 
    846 		switch (adbBusState) {	/* set to next state */
    847 		case ADB_BUS_EVEN:
    848 			ADB_SET_STATE_ODD();	/* set state to odd */
    849 			adbBusState = ADB_BUS_ODD;
    850 			break;
    851 
    852 		case ADB_BUS_ODD:
    853 			ADB_SET_STATE_EVEN();	/* set state to even */
    854 			adbBusState = ADB_BUS_EVEN;
    855 			break;
    856 		default:
    857 			printf_intr("strange state!!!\n");	/* huh? */
    858 			break;
    859 		}
    860 		break;
    861 
    862 	case ADB_ACTION_OUT:
    863 		adbNextEnd = 0;
    864 		if (!adbPolling)
    865 			adbWaiting = 1;	/* not unsolicited */
    866 		i = ADB_SR();	/* clear interrupt */
    867 		adbSentChars++;
    868 		/*
    869 		 * If the outgoing data was a TALK, we must
    870 		 * switch to input mode to get the result.
    871 		 */
    872 		if ((adbOutputBuffer[1] & 0x0c) == 0x0c) {
    873 			adbInputBuffer[0] = 1;
    874 			adbInputBuffer[1] = i;
    875 			adbActionState = ADB_ACTION_IN;
    876 			ADB_SET_SR_INPUT();
    877 			adbBusState = ADB_BUS_EVEN;
    878 			ADB_SET_STATE_EVEN();
    879 			/* printf_intr("talk out 0x%02x ", i); */
    880 			break;
    881 		}
    882 		/* If it's not a TALK, check whether all data has been sent.
    883 		 * If so, call the completion routine and clean up. If not,
    884 		 * advance to the next state. */
    885 		/* printf_intr("non-talk out 0x%0x ", i); */
    886 		ADB_SET_SR_OUTPUT();
    887 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
    888 			/* printf_intr("done \n"); */
    889                         /* set up stuff for adb_pass_up */
    890                         for (i=0; i<=adbInputBuffer[0]; i++)
    891                                 packet.data[i]=adbInputBuffer[i];
    892                         packet.saveBuf=adbBuffer;
    893                         packet.compRout=adbCompRout;
    894                         packet.compData=adbCompData;
    895                         packet.cmd=adbWaitingCmd;
    896                         packet.unsol=0;
    897                         packet.ack_only=1;
    898                         adb_pass_up(&packet);
    899 
    900                         /* reset "waiting" vars, just in case */
    901                         adbWaitingCmd = 0;
    902                         adbBuffer = (long) 0;
    903                         adbCompRout = (long) 0;
    904                         adbCompData = (long) 0;
    905 
    906 			if (adbOutQueueHasData == 1) {
    907 				/* copy over data */
    908 				for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
    909 					adbOutputBuffer[i] = adbOutQueue.outBuf[i];
    910 				adbBuffer = adbOutQueue.saveBuf;	/* user data area */
    911 				adbCompRout = adbOutQueue.compRout;	/* completion routine */
    912 				adbCompData = adbOutQueue.data;	/* comp. rout. data */
    913 				adbOutQueueHasData = 0;	/* currently processing
    914 							 * "queue" entry */
    915 				adbPolling = 0;
    916 			} else {
    917 				adbOutputBuffer[0] = 1;
    918 				adbOutputBuffer[1] = (adbOutputBuffer[1] & 0xf0) | 0x0c;
    919 				adbPolling = 1;	/* non-user poll */
    920 			}
    921 			adbNextEnd = 0;
    922 			adbSentChars = 0;	/* nothing sent yet */
    923 			adbActionState = ADB_ACTION_OUT;	/* set next state */
    924 			ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
    925 			ADB_SR() = adbOutputBuffer[1];	/* load byte for output */
    926 			adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
    927 			ADB_SET_STATE_CMD();	/* tell ADB that we want to
    928 						 * send */
    929 			break;
    930 		}
    931 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];
    932 		switch (adbBusState) {	/* advance to next state */
    933 		case ADB_BUS_EVEN:
    934 			ADB_SET_STATE_ODD();	/* set state to odd */
    935 			adbBusState = ADB_BUS_ODD;
    936 			break;
    937 
    938 		case ADB_BUS_CMD:
    939 		case ADB_BUS_ODD:
    940 			ADB_SET_STATE_EVEN();	/* set state to even */
    941 			adbBusState = ADB_BUS_EVEN;
    942 			break;
    943 
    944 		default:
    945 			printf_intr("strange state!!! (0x%x)\n", adbBusState);
    946 			break;
    947 		}
    948 		break;
    949 
    950 	default:
    951 		printf_intr("adb: unknown ADB state (during intr)\n");
    952 	}
    953 
    954 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
    955 
    956 	splx(s);		/* restore */
    957 
    958 	return;
    959 
    960 }
    961 
    962 
    963 /*
    964  * send_adb version for II series machines
    965  */
    966 int
    967 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
    968 {
    969 	int i, s, len;
    970 
    971 	if (adbActionState == ADB_ACTION_NOTREADY)	/* return if ADB not
    972 							 * available */
    973 		return 1;
    974 
    975 	s = splhigh();		/* don't interrupt while we are messing with
    976 				 * the ADB */
    977 
    978 	if (0 != adbOutQueueHasData) {	/* right now, "has data" means "full" */
    979 		splx(s);	/* sorry, try again later */
    980 		return 1;
    981 	}
    982 	if ((long) in == (long) 0) {	/* need to convert? */
    983 		/*
    984 		 * Don't need to use adb_cmd_extra here because this section
    985 		 * will be called ONLY when it is an ADB command (no RTC or
    986 		 * PRAM), especially on II series!
    987 		 */
    988 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
    989 						 * doing a listen! */
    990 			len = buffer[0];	/* length of additional data */
    991 		else
    992 			len = 0;/* no additional data */
    993 
    994 		adbOutQueue.outBuf[0] = 1 + len;	/* command + addl. data */
    995 		adbOutQueue.outBuf[1] = (u_char) command;	/* load command */
    996 
    997 		for (i = 1; i <= len; i++)	/* copy additional output
    998 						 * data, if any */
    999 			adbOutQueue.outBuf[1 + i] = buffer[i];
   1000 	} else
   1001 		/* if data ready, just copy over */
   1002 		for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
   1003 			adbOutQueue.outBuf[i] = in[i];
   1004 
   1005 	adbOutQueue.saveBuf = buffer;	/* save buffer to know where to save
   1006 					 * result */
   1007 	adbOutQueue.compRout = compRout;	/* save completion routine
   1008 						 * pointer */
   1009 	adbOutQueue.data = data;/* save completion routine data pointer */
   1010 
   1011 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* is ADB available? */
   1012 	    (ADB_INTR_IS_OFF) &&/* and no incoming interrupts? */
   1013 	    (adbPolling == 0)) {/* and we are not currently polling */
   1014 		/* then start command now */
   1015 		for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)	/* copy over data */
   1016 			adbOutputBuffer[i] = adbOutQueue.outBuf[i];
   1017 
   1018 		adbBuffer = adbOutQueue.saveBuf;	/* pointer to user data
   1019 							 * area */
   1020 		adbCompRout = adbOutQueue.compRout;	/* pointer to the
   1021 							 * completion routine */
   1022 		adbCompData = adbOutQueue.data;	/* pointer to the completion
   1023 						 * routine data */
   1024 
   1025 		adbSentChars = 0;	/* nothing sent yet */
   1026 		adbActionState = ADB_ACTION_OUT;	/* set next state */
   1027 		adbBusState = ADB_BUS_CMD;	/* set bus to cmd state */
   1028 
   1029 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
   1030 
   1031 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
   1032 		ADB_SET_STATE_CMD();	/* tell ADB that we want to send */
   1033 		adbOutQueueHasData = 0;	/* currently processing "queue" entry */
   1034 	} else
   1035 		adbOutQueueHasData = 1;	/* something in the write "queue" */
   1036 
   1037 	splx(s);
   1038 
   1039 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked ? */
   1040 		/* poll until message done */
   1041 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
   1042 		    || (adbWaiting == 1) || (adbPolling == 1))
   1043 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
   1044 				adb_intr_II();	/* go process "interrupt" */
   1045 				adb_soft_intr();
   1046 			}
   1047 
   1048 	return 0;
   1049 }
   1050 
   1051 
   1052 /*
   1053  * This routine is called from the II series interrupt routine
   1054  * to determine what the "next" device is that should be polled.
   1055  */
   1056 int
   1057 adb_guess_next_device(void)
   1058 {
   1059 	int last, i, dummy;
   1060 
   1061 	if (adbStarting) {
   1062 		/* start polling EVERY device, since we can't be sure there is
   1063 		 * anything in the device table yet */
   1064 		if (adbLastDevice < 1 || adbLastDevice > 15)
   1065 			adbLastDevice = 1;
   1066 		if (++adbLastDevice > 15)	/* point to next one */
   1067 			adbLastDevice = 1;
   1068 	} else {
   1069 		/* find the next device using the device table */
   1070 		if (adbLastDevice < 1 || adbLastDevice > 15)	/* let's be parinoid */
   1071 			adbLastDevice = 2;
   1072 		last = 1;	/* default index location */
   1073 
   1074 		for (i = 1; i < 16; i++)	/* find index entry */
   1075 			if (ADBDevTable[i].currentAddr == adbLastDevice) {	/* look for device */
   1076 				last = i;	/* found it */
   1077 				break;
   1078 			}
   1079 		dummy = last;	/* index to start at */
   1080 		for (;;) {	/* find next device in index */
   1081 			if (++dummy > 15)	/* wrap around if needed */
   1082 				dummy = 1;
   1083 			if (dummy == last) {	/* didn't find any other
   1084 						 * device! This can happen if
   1085 						 * there are no devices on the
   1086 						 * bus */
   1087 				dummy = 2;
   1088 				break;
   1089 			}
   1090 			/* found the next device */
   1091 			if (ADBDevTable[dummy].devType != 0)
   1092 				break;
   1093 		}
   1094 		adbLastDevice = ADBDevTable[dummy].currentAddr;
   1095 	}
   1096 	return adbLastDevice;
   1097 }
   1098 
   1099 
   1100 /*
   1101  * Called when when an adb interrupt happens.
   1102  * This routine simply transfers control over to the appropriate
   1103  * code for the machine we are running on.
   1104  */
   1105 void
   1106 adb_intr(void)
   1107 {
   1108 	switch (adbHardware) {
   1109 	case ADB_HW_II:
   1110 		adb_intr_II();
   1111 		break;
   1112 
   1113 	case ADB_HW_IISI:
   1114 		adb_intr_IIsi();
   1115 		break;
   1116 
   1117 	case ADB_HW_PB:
   1118 		break;
   1119 
   1120 	case ADB_HW_CUDA:
   1121 		adb_intr_cuda();
   1122 		break;
   1123 
   1124 	case ADB_HW_UNKNOWN:
   1125 		break;
   1126 	}
   1127 }
   1128 
   1129 
   1130 /*
   1131  * called when when an adb interrupt happens
   1132  *
   1133  * IIsi version of adb_intr
   1134  *
   1135  */
   1136 void
   1137 adb_intr_IIsi(void)
   1138 {
   1139 	struct adbCommand packet;
   1140 	int i, ending;
   1141 	unsigned int s;
   1142 
   1143 	s = splhigh();		/* can't be too careful - might be called */
   1144 	/* from a routine, NOT an interrupt */
   1145 
   1146 	ADB_VIA_CLR_INTR();	/* clear interrupt */
   1147 
   1148 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
   1149 
   1150 switch_start:
   1151 	switch (adbActionState) {
   1152 	case ADB_ACTION_IDLE:
   1153 		delay(ADB_DELAY);	/* short delay is required before the
   1154 					 * first byte */
   1155 
   1156 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
   1157 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
   1158 		adbInputBuffer[1] = ADB_SR();	/* get byte */
   1159 		adbInputBuffer[0] = 1;
   1160 		adbActionState = ADB_ACTION_IN;	/* set next state */
   1161 
   1162 		ADB_SET_STATE_ACKON();	/* start ACK to ADB chip */
   1163 		delay(ADB_DELAY);	/* delay */
   1164 		ADB_SET_STATE_ACKOFF();	/* end ACK to ADB chip */
   1165 		zshard(0);	/* grab any serial interrupts */
   1166 		break;
   1167 
   1168 	case ADB_ACTION_IN:
   1169 		ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
   1170 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();	/* get byte */
   1171 		if (ADB_INTR_IS_OFF)	/* check for end of frame */
   1172 			ending = 1;
   1173 		else
   1174 			ending = 0;
   1175 
   1176 		ADB_SET_STATE_ACKON();	/* start ACK to ADB chip */
   1177 		delay(ADB_DELAY);	/* delay */
   1178 		ADB_SET_STATE_ACKOFF();	/* end ACK to ADB chip */
   1179 		zshard(0);	/* grab any serial interrupts */
   1180 
   1181 		if (1 == ending) {	/* end of message? */
   1182 			ADB_SET_STATE_INACTIVE();	/* signal end of frame */
   1183 			/* this section _should_ handle all ADB and RTC/PRAM
   1184 			 * type commands, */
   1185 			/* but there may be more... */
   1186 			/* note: commands are always at [4], even for rtc/pram
   1187 			 * commands */
   1188 			/* set up data for adb_pass_up */
   1189 			for (i=0; i<=adbInputBuffer[0]; i++)
   1190 				packet.data[i]=adbInputBuffer[i];
   1191 
   1192 			if ((adbWaiting == 1) &&	/* are we waiting AND */
   1193 			    (adbInputBuffer[4] == adbWaitingCmd) &&	/* the cmd we sent AND */
   1194 			    ((adbInputBuffer[2] == 0x00) ||	/* it's from the ADB
   1195 								 * device OR */
   1196 				(adbInputBuffer[2] == 0x01))) {	/* it's from the
   1197 								 * PRAM/RTC device */
   1198 
   1199 				packet.saveBuf=adbBuffer;
   1200 				packet.compRout=adbCompRout;
   1201 				packet.compData=adbCompData;
   1202 				packet.unsol=0;
   1203 				packet.ack_only=0;
   1204 				adb_pass_up(&packet);
   1205 
   1206 				adbWaitingCmd = 0;	/* reset "waiting" vars */
   1207 				adbWaiting = 0;
   1208 				adbBuffer = (long) 0;
   1209 				adbCompRout = (long) 0;
   1210 				adbCompData = (long) 0;
   1211 			} else {
   1212 				packet.unsol=1;
   1213 				packet.ack_only=0;
   1214 				adb_pass_up(&packet);
   1215 			}
   1216 
   1217 			adbActionState = ADB_ACTION_IDLE;
   1218 			adbInputBuffer[0] = 0;	/* reset length */
   1219 
   1220 			if (adbWriteDelay == 1) {	/* were we waiting to
   1221 							 * write? */
   1222 				adbSentChars = 0;	/* nothing sent yet */
   1223 				adbActionState = ADB_ACTION_OUT;	/* set next state */
   1224 
   1225 				delay(ADB_DELAY);	/* delay */
   1226 				zshard(0);	/* grab any serial interrupts */
   1227 
   1228 				if (ADB_INTR_IS_ON) {	/* ADB intr low during
   1229 							 * write */
   1230 					ADB_SET_STATE_IDLE_IISI();	/* reset */
   1231 					ADB_SET_SR_INPUT();	/* make sure SR is set
   1232 								 * to IN */
   1233 					adbSentChars = 0;	/* must start all over */
   1234 					adbActionState = ADB_ACTION_IDLE;	/* new state */
   1235 					adbInputBuffer[0] = 0;
   1236 					/* may be able to take this out later */
   1237 					delay(ADB_DELAY);	/* delay */
   1238 					break;
   1239 				}
   1240 				ADB_SET_STATE_ACTIVE();	/* tell ADB that we want
   1241 							 * to send */
   1242 				ADB_SET_STATE_ACKOFF();	/* make sure */
   1243 				ADB_SET_SR_OUTPUT();	/* set shift register
   1244 							 * for OUT */
   1245 				ADB_SR() = adbOutputBuffer[adbSentChars + 1];
   1246 				ADB_SET_STATE_ACKON();	/* tell ADB byte ready
   1247 							 * to shift */
   1248 			}
   1249 		}
   1250 		break;
   1251 
   1252 	case ADB_ACTION_OUT:
   1253 		i = ADB_SR();	/* reset SR-intr in IFR */
   1254 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
   1255 
   1256 		ADB_SET_STATE_ACKOFF();	/* finish ACK */
   1257 		adbSentChars++;
   1258 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
   1259 			ADB_SET_STATE_IDLE_IISI();	/* reset */
   1260 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
   1261 			adbSentChars = 0;	/* must start all over */
   1262 			adbActionState = ADB_ACTION_IDLE;	/* new state */
   1263 			adbInputBuffer[0] = 0;
   1264 			adbWriteDelay = 1;	/* must retry when done with
   1265 						 * read */
   1266 			delay(ADB_DELAY);	/* delay */
   1267 			zshard(0);		/* grab any serial interrupts */
   1268 			goto switch_start;	/* process next state right
   1269 						 * now */
   1270 			break;
   1271 		}
   1272 		delay(ADB_DELAY);	/* required delay */
   1273 		zshard(0);	/* grab any serial interrupts */
   1274 
   1275 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
   1276 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
   1277 									 * back? */
   1278 				adbWaiting = 1;	/* signal waiting for return */
   1279 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
   1280 			} else {/* no talk, so done */
   1281 				/* set up stuff for adb_pass_up */
   1282 				for (i=0; i<=adbInputBuffer[0]; i++)
   1283 					packet.data[i]=adbInputBuffer[i];
   1284 				packet.saveBuf=adbBuffer;
   1285 				packet.compRout=adbCompRout;
   1286 				packet.compData=adbCompData;
   1287 				packet.cmd=adbWaitingCmd;
   1288 				packet.unsol=0;
   1289 				packet.ack_only=1;
   1290 				adb_pass_up(&packet);
   1291 
   1292 				/* reset "waiting" vars, just in case */
   1293 				adbWaitingCmd = 0;
   1294 				adbBuffer = (long) 0;
   1295 				adbCompRout = (long) 0;
   1296 				adbCompData = (long) 0;
   1297 			}
   1298 
   1299 			adbWriteDelay = 0;	/* done writing */
   1300 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
   1301 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
   1302 			ADB_SET_STATE_INACTIVE();	/* end of frame */
   1303 		} else {
   1304 			ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* send next byte */
   1305 			ADB_SET_STATE_ACKON();	/* signal byte ready to shift */
   1306 		}
   1307 		break;
   1308 
   1309 	case ADB_ACTION_NOTREADY:
   1310 		printf_intr("adb: not yet initialized\n");
   1311 		break;
   1312 
   1313 	default:
   1314 		printf_intr("intr: unknown ADB state\n");
   1315 	}
   1316 
   1317 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
   1318 
   1319 	splx(s);		/* restore */
   1320 
   1321 	return;
   1322 }				/* end adb_intr_IIsi */
   1323 
   1324 
   1325 /*****************************************************************************
   1326  * if the device is currently busy, and there is no data waiting to go out, then
   1327  * the data is "queued" in the outgoing buffer. If we are already waiting, then
   1328  * we return.
   1329  * in: if (in==0) then the command string is built from command and buffer
   1330  *     if (in!=0) then in is used as the command string
   1331  * buffer: additional data to be sent (used only if in==0)
   1332  *         this is also where return data is stored
   1333  * compRout: the completion routine that is called when then return value
   1334  *	     is received (if a return value is expected)
   1335  * data: a data pointer that can be used by the completion routine
   1336  * command: an ADB command to be sent (used only if in==0)
   1337  *
   1338  */
   1339 int
   1340 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
   1341 	command)
   1342 {
   1343 	int i, s, len;
   1344 
   1345 	if (adbActionState == ADB_ACTION_NOTREADY)
   1346 		return 1;
   1347 
   1348 	s = splhigh();		/* don't interrupt while we are messing with
   1349 				 * the ADB */
   1350 
   1351 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
   1352 	    (ADB_INTR_IS_OFF)) {/* and no incoming interrupt? */
   1353 
   1354 	} else
   1355 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
   1356 			adbWriteDelay = 1;	/* if no, then we'll "queue"
   1357 						 * it up */
   1358 		else {
   1359 			splx(s);
   1360 			return 1;	/* really busy! */
   1361 		}
   1362 
   1363 	if ((long) in == (long) 0) {	/* need to convert? */
   1364 		/* don't need to use adb_cmd_extra here because this section
   1365 		 * will be called */
   1366 		/* ONLY when it is an ADB command (no RTC or PRAM) */
   1367 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
   1368 						 * doing a listen! */
   1369 			len = buffer[0];	/* length of additional data */
   1370 		else
   1371 			len = 0;/* no additional data */
   1372 
   1373 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
   1374 						 * data */
   1375 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
   1376 		adbOutputBuffer[2] = (u_char) command;	/* load command */
   1377 
   1378 		for (i = 1; i <= len; i++)	/* copy additional output
   1379 						 * data, if any */
   1380 			adbOutputBuffer[2 + i] = buffer[i];
   1381 	} else
   1382 		for (i = 0; i <= (adbOutputBuffer[0] + 1); i++)
   1383 			adbOutputBuffer[i] = in[i];
   1384 
   1385 	adbSentChars = 0;	/* nothing sent yet */
   1386 	adbBuffer = buffer;	/* save buffer to know where to save result */
   1387 	adbCompRout = compRout;	/* save completion routine pointer */
   1388 	adbCompData = data;	/* save completion routine data pointer */
   1389 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
   1390 
   1391 	if (adbWriteDelay != 1) {	/* start command now? */
   1392 		adbActionState = ADB_ACTION_OUT;	/* set next state */
   1393 
   1394 		ADB_SET_STATE_ACTIVE();	/* tell ADB that we want to send */
   1395 		ADB_SET_STATE_ACKOFF();	/* make sure */
   1396 
   1397 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
   1398 
   1399 		ADB_SR() = adbOutputBuffer[adbSentChars + 1];	/* load byte for output */
   1400 
   1401 		ADB_SET_STATE_ACKON();	/* tell ADB byte ready to shift */
   1402 	}
   1403 	adbWriteDelay = 1;	/* something in the write "queue" */
   1404 
   1405 	splx(s);
   1406 
   1407 	if (0x0100 <= (s & 0x0700))	/* were VIA1 interrupts blocked ? */
   1408 		/* poll until byte done */
   1409 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
   1410 		    || (adbWaiting == 1))
   1411 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
   1412 				adb_intr_IIsi();	/* go process
   1413 							 * "interrupt" */
   1414 				adb_soft_intr();
   1415 			}
   1416 
   1417 	 return 0;
   1418 }				/* send_adb_IIsi */
   1419 
   1420 
   1421 /*
   1422  * adb_pass_up is called by the interrupt-time routines.
   1423  * It takes the raw packet data that was received from the
   1424  * device and puts it into the queue that the upper half
   1425  * processes. It then signals for a soft ADB interrupt which
   1426  * will eventually call the upper half routine (adb_soft_intr).
   1427  *
   1428  * If in->unsol is 0, then this is either the notification
   1429  * that the packet was sent (on a LISTEN, for example), or the
   1430  * response from the device (on a TALK). The completion routine
   1431  * is called only if the user specified one.
   1432  *
   1433  * If in->unsol is 1, then this packet was unsolicited and
   1434  * so we look up the device in the ADB device table to determine
   1435  * what it's default service routine is.
   1436  *
   1437  * If in->ack_only is 1, then we really only need to call
   1438  * the completion routine, so don't do any other stuff.
   1439  *
   1440  * Note that in->data contains the packet header AND data,
   1441  * while adbInbound[]->data contains ONLY data.
   1442  *
   1443  * Note: Called only at interrupt time. Assumes this.
   1444  *
   1445  */
   1446 void
   1447 adb_pass_up(struct adbCommand *in)
   1448 {
   1449 	int i, start=0, len=0, cmd=0;
   1450 	ADBDataBlock block;
   1451 
   1452 	/* temp for testing */
   1453 	/*u_char *buffer = 0;*/
   1454 	/*u_char *compdata = 0;*/
   1455 	/*u_char *comprout = 0;*/
   1456 
   1457 	if (adbInCount>=ADB_QUEUE) {
   1458 		printf_intr("adb: ring buffer overflow\n");
   1459 		return;
   1460 	}
   1461 
   1462 	if (in->ack_only) {
   1463 		len=in->data[0];
   1464 		cmd=in->cmd;
   1465 		start=0;
   1466 	} else {
   1467 		switch (adbHardware) {
   1468 		case ADB_HW_II:
   1469 			cmd = in->data[1];
   1470 			if (in->data[0] < 2)
   1471 				len=0;
   1472 			else
   1473 				len=in->data[0]-1;
   1474 			start=1;
   1475 			break;
   1476 
   1477 		case ADB_HW_IISI:
   1478 		case ADB_HW_CUDA:
   1479 			/* If it's unsolicited, accept only ADB data for now */
   1480 			if (in->unsol)
   1481 				if (0 != in->data[2])
   1482 					return;
   1483 			cmd = in->data[4];
   1484 			if (in->data[0] < 5)
   1485 				len=0;
   1486 			else
   1487 				len=in->data[0]-4;
   1488 			start=4;
   1489 			break;
   1490 
   1491 		case ADB_HW_PB:
   1492 			return;		/* how does PM handle "unsolicited" messages? */
   1493 
   1494 		case ADB_HW_UNKNOWN:
   1495 			return;
   1496 		}
   1497 
   1498 		/* Make sure there is a valid device entry for this device */
   1499 		if (in->unsol) {
   1500 			/* ignore unsolicited data during adbreinit */
   1501 			if (adbStarting)
   1502 				return;
   1503 			/* get device's comp. routine and data area */
   1504 			if (-1 == get_adb_info(&block, ((cmd & 0xf0) >> 4)))
   1505 				return;
   1506 		}
   1507 	}
   1508 
   1509 	/*
   1510  	 * If this is an unsolicited packet, we need to fill in
   1511  	 * some info so adb_soft_intr can process this packet
   1512  	 * properly. If it's not unsolicited, then use what
   1513  	 * the caller sent us.
   1514  	 */
   1515 	if (in->unsol) {
   1516 		adbInbound[adbInTail].compRout=(void *)block.dbServiceRtPtr;
   1517 		adbInbound[adbInTail].compData=(void *)block.dbDataAreaAddr;
   1518 		adbInbound[adbInTail].saveBuf=(void *)adbInbound[adbInTail].data;
   1519 	} else {
   1520 		adbInbound[adbInTail].compRout=(void *)in->compRout;
   1521 		adbInbound[adbInTail].compData=(void *)in->compData;
   1522 		adbInbound[adbInTail].saveBuf=(void *)in->saveBuf;
   1523 	}
   1524 
   1525 #ifdef ADB_DEBUG
   1526 	if (adb_debug && in->data[1] == 2)
   1527 		printf_intr("adb: caught error\n");
   1528 #endif
   1529 
   1530 	/* copy the packet data over */
   1531 	/* TO DO: If the *_intr routines fed their incoming data
   1532 	 * directly into an adbCommand struct, which is passed to
   1533 	 * this routine, then we could eliminate this copy.
   1534 	 */
   1535 	for (i = 1; i <= len; i++)
   1536 		adbInbound[adbInTail].data[i]=in->data[start+i];
   1537 
   1538 	adbInbound[adbInTail].data[0]=len;
   1539 	adbInbound[adbInTail].cmd=cmd;
   1540 
   1541 	adbInCount++;
   1542 	if (++adbInTail >= ADB_QUEUE)
   1543 		adbInTail=0;
   1544 
   1545 	/*
   1546 	 * If the debugger is running, call upper half manually.
   1547 	 * Otherwise, trigger a soft interrupt to handle the rest later.
   1548 	 */
   1549 	if (adb_polling)
   1550 		adb_soft_intr();
   1551 	else
   1552 		setsoftadb();
   1553 
   1554 	return;
   1555 }
   1556 
   1557 
   1558 /*
   1559  * Called to process the packets after they have been
   1560  * placed in the incoming queue.
   1561  *
   1562  */
   1563 void
   1564 adb_soft_intr(void)
   1565 {
   1566 	int s, i;
   1567 	int cmd=0;
   1568 	u_char *buffer=0;
   1569 	u_char *comprout=0;
   1570 	u_char *compdata=0;
   1571 
   1572 #if 0
   1573 	s=splhigh();
   1574 	printf_intr("sr: %x\n", (s & 0x0700));
   1575 	splx(s);
   1576 #endif
   1577 
   1578 /*delay(2*ADB_DELAY);*/
   1579 
   1580 	while (adbInCount) {
   1581 /*printf_intr("%x %x %x ", adbInCount, adbInHead, adbInTail);*/
   1582 		/* get the data we need from the queue */
   1583 		buffer=adbInbound[adbInHead].saveBuf;
   1584 		comprout=adbInbound[adbInHead].compRout;
   1585 		compdata=adbInbound[adbInHead].compData;
   1586 		cmd=adbInbound[adbInHead].cmd;
   1587 
   1588 		/* copy over data to data area if it's valid */
   1589 		/* note that for unsol packets we don't want to copy the
   1590 	 	* data anywhere, so buffer was already set to 0.
   1591 	 	* For ack_only buffer was set to 0, so don't copy. */
   1592 		if (buffer)
   1593 			for (i = 0; i <= adbInbound[adbInHead].data[0]; i++)
   1594 				*(buffer+i)=adbInbound[adbInHead].data[i];
   1595 
   1596 /*printf_intr("%lx %lx %lx %x ", buffer, comprout, compdata, cmd);*/
   1597 /*printf_intr("buf: ");*/
   1598 /*print_single(adbInbound[adbInHead].data);*/
   1599 
   1600 		/* call default completion routine if it's valid */
   1601 		if (comprout) {
   1602 #ifdef __NetBSD__
   1603 			asm("
   1604 		    	movml #0xffff, sp@-		| save all registers
   1605 		    	movl %0, a2 		| compdata
   1606 		    	movl %1, a1 		| comprout
   1607 		    	movl %2, a0 		| buffer
   1608 		    	movl %3, d0 		| cmd
   1609 		    	jbsr a1@ 			| go call the routine
   1610 		    	movml sp@+, #0xffff		| restore all registers"
   1611 		    	:
   1612 		    	: "g"(compdata),
   1613 		      	"g"(comprout),
   1614 		      	"g"(buffer),
   1615 		      	"g"(cmd)
   1616 		    	: "d0", "a0", "a1", "a2");
   1617 #else					/* for macos based testing */
   1618 			asm
   1619 			{
   1620 				movem.l a0/a1/a2/d0, -(a7)
   1621 				move.l compdata, a2
   1622 				move.l comprout, a1
   1623 				move.l buffer, a0
   1624 				move.w cmd, d0
   1625 				jsr(a1)
   1626 				movem.l(a7)+, d0/a2/a1/a0
   1627 			}
   1628 #endif
   1629 		}
   1630 
   1631 		s=splhigh();
   1632         	adbInCount--;
   1633         	if (++adbInHead >= ADB_QUEUE)
   1634                 	adbInHead=0;
   1635 		splx(s);
   1636 
   1637 	}
   1638 	return;
   1639 }
   1640 
   1641 
   1642 /*
   1643  * This is my version of the ADBOp routine. It mainly just calls the hardware-specific
   1644  * routine.
   1645  *
   1646  *   data 	: pointer to data area to be used by compRout
   1647  *   compRout	: completion routine
   1648  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
   1649  *		  byte 0 = # of bytes
   1650  *		: for TALK: points to place to save return data
   1651  *   command	: the adb command to send
   1652  *   result     : 0 = success
   1653  *              : -1 = could not complete
   1654  */
   1655 int
   1656 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
   1657 {
   1658 	int result;
   1659 
   1660 	switch (adbHardware) {
   1661 	case ADB_HW_II:
   1662 		result = send_adb_II((u_char *) 0,
   1663 		    (u_char *) buffer, (void *) compRout,
   1664 		    (void *) data, (int) command);
   1665 		if (result == 0)
   1666 			return 0;
   1667 		else
   1668 			return -1;
   1669 		break;
   1670 
   1671 	case ADB_HW_IISI:
   1672 		result = send_adb_IIsi((u_char *) 0,
   1673 		    (u_char *) buffer, (void *) compRout,
   1674 		    (void *) data, (int) command);
   1675 		/*
   1676 		 * I wish I knew why this delay is needed. It usually needs to
   1677 		 * be here when several commands are sent in close succession,
   1678 		 * especially early in device probes when doing collision
   1679 		 * detection. It must be some race condition. Sigh. - jpw
   1680 		 */
   1681 		delay(100);
   1682 		if (result == 0)
   1683 			return 0;
   1684 		else
   1685 			return -1;
   1686 		break;
   1687 
   1688 	case ADB_HW_PB:
   1689 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
   1690 		    (void *)data, (int)command);
   1691 
   1692 		if (result == 0)
   1693 			return 0;
   1694 		else
   1695 			return -1;
   1696 		break;
   1697 
   1698 	case ADB_HW_CUDA:
   1699 		result = send_adb_cuda((u_char *) 0,
   1700 		    (u_char *) buffer, (void *) compRout,
   1701 		    (void *) data, (int) command);
   1702 		if (result == 0)
   1703 			return 0;
   1704 		else
   1705 			return -1;
   1706 		break;
   1707 
   1708 	case ADB_HW_UNKNOWN:
   1709 	default:
   1710 		return -1;
   1711 	}
   1712 }
   1713 
   1714 
   1715 /*
   1716  * adb_hw_setup
   1717  * This routine sets up the possible machine specific hardware
   1718  * config (mainly VIA settings) for the various models.
   1719  */
   1720 void
   1721 adb_hw_setup(void)
   1722 {
   1723 	volatile int i;
   1724 	u_char send_string[ADB_MAX_MSG_LENGTH];
   1725 
   1726 	switch (adbHardware) {
   1727 	case ADB_HW_II:
   1728 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
   1729 						 * outputs */
   1730 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
   1731 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
   1732 							 * to IN (II, IIsi) */
   1733 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1734 							 * hardware (II, IIsi) */
   1735 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1736 						 * code only */
   1737 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
   1738 						 * are on (II, IIsi) */
   1739 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
   1740 
   1741 		ADB_VIA_CLR_INTR();	/* clear interrupt */
   1742 		break;
   1743 
   1744 	case ADB_HW_IISI:
   1745 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
   1746 						 * outputs */
   1747 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
   1748 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
   1749 							 * to IN (II, IIsi) */
   1750 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1751 							 * hardware (II, IIsi) */
   1752 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1753 						 * code only */
   1754 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
   1755 						 * are on (II, IIsi) */
   1756 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
   1757 
   1758 		/* get those pesky clock ticks we missed while booting */
   1759 		for (i = 0; i < 30; i++) {
   1760 			delay(ADB_DELAY);
   1761 			adb_hw_setup_IIsi(send_string);
   1762 			printf_intr("adb: cleanup: ");
   1763 			print_single(send_string);
   1764 			delay(ADB_DELAY);
   1765 			if (ADB_INTR_IS_OFF)
   1766 				break;
   1767 		}
   1768 		break;
   1769 
   1770 	case ADB_HW_PB:
   1771 		/*
   1772 		 * XXX -  really PM_VIA_CLR_INTR - should we put it in
   1773 		 * pm_direct.h?
   1774 		 */
   1775 		via_reg(VIA1, vIFR) = 0x90;	/* clear interrupt */
   1776 		break;
   1777 
   1778 	case ADB_HW_CUDA:
   1779 		via_reg(VIA1, vDirB) |= 0x30;	/* register B bits 4 and 5:
   1780 						 * outputs */
   1781 		via_reg(VIA1, vDirB) &= 0xf7;	/* register B bit 3: input */
   1782 		via_reg(VIA1, vACR) &= ~vSR_OUT;	/* make sure SR is set
   1783 							 * to IN */
   1784 		via_reg(VIA1, vACR) = (via_reg(VIA1, vACR) | 0x0c) & ~0x10;
   1785 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
   1786 							 * hardware */
   1787 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
   1788 						 * code only */
   1789 		via_reg(VIA1, vIER) = 0x84;	/* make sure VIA interrupts
   1790 						 * are on */
   1791 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
   1792 
   1793 		/* sort of a device reset */
   1794 		i = ADB_SR();	/* clear interrupt */
   1795 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
   1796 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
   1797 		delay(ADB_DELAY);
   1798 		ADB_SET_STATE_TIP();	/* signal start of frame */
   1799 		delay(ADB_DELAY);
   1800 		ADB_TOGGLE_STATE_ACK_CUDA();
   1801 		delay(ADB_DELAY);
   1802 		ADB_CLR_STATE_TIP();
   1803 		delay(ADB_DELAY);
   1804 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
   1805 		i = ADB_SR();	/* clear interrupt */
   1806 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
   1807 		break;
   1808 
   1809 	case ADB_HW_UNKNOWN:
   1810 	default:
   1811 		via_reg(VIA1, vIER) = 0x04;	/* turn interrupts off - TO
   1812 						 * DO: turn PB ints off? */
   1813 		return;
   1814 		break;
   1815 	}
   1816 }
   1817 
   1818 
   1819 /*
   1820  * adb_hw_setup_IIsi
   1821  * This is sort of a "read" routine that forces the adb hardware through a read cycle
   1822  * if there is something waiting. This helps "clean up" any commands that may have gotten
   1823  * stuck or stopped during the boot process.
   1824  *
   1825  */
   1826 void
   1827 adb_hw_setup_IIsi(u_char * buffer)
   1828 {
   1829 	int i;
   1830 	int dummy;
   1831 	int s;
   1832 	long my_time;
   1833 	int endofframe;
   1834 
   1835 	delay(ADB_DELAY);
   1836 
   1837 	i = 1;			/* skip over [0] */
   1838 	s = splhigh();		/* block ALL interrupts while we are working */
   1839 	ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
   1840 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
   1841 	/* this is required, especially on faster machines */
   1842 	delay(ADB_DELAY);
   1843 
   1844 	if (ADB_INTR_IS_ON) {
   1845 		ADB_SET_STATE_ACTIVE();	/* signal start of data frame */
   1846 
   1847 		endofframe = 0;
   1848 		while (0 == endofframe) {
   1849 			/* poll for ADB interrupt and watch for timeout */
   1850 			/* if time out, keep going in hopes of not hanging the
   1851 			 * ADB chip - I think */
   1852 			my_time = ADB_DELAY * 5;
   1853 			while ((ADB_SR_INTR_IS_OFF) && (my_time-- > 0))
   1854 				dummy = via_reg(VIA1, vBufB);
   1855 
   1856 			buffer[i++] = ADB_SR();	/* reset interrupt flag by
   1857 						 * reading vSR */
   1858 			/* perhaps put in a check here that ignores all data
   1859 			 * after the first ADB_MAX_MSG_LENGTH bytes ??? */
   1860 			if (ADB_INTR_IS_OFF)	/* check for end of frame */
   1861 				endofframe = 1;
   1862 
   1863 			ADB_SET_STATE_ACKON();	/* send ACK to ADB chip */
   1864 			delay(ADB_DELAY);	/* delay */
   1865 			ADB_SET_STATE_ACKOFF();	/* send ACK to ADB chip */
   1866 		}
   1867 		ADB_SET_STATE_INACTIVE();	/* signal end of frame and
   1868 						 * delay */
   1869 
   1870 		/* probably don't need to delay this long */
   1871 		delay(ADB_DELAY);
   1872 	}
   1873 	buffer[0] = --i;	/* [0] is length of message */
   1874 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
   1875 	splx(s);		/* restore interrupts */
   1876 
   1877 	return;
   1878 }				/* adb_hw_setup_IIsi */
   1879 
   1880 
   1881 
   1882 /*
   1883  * adb_reinit sets up the adb stuff
   1884  *
   1885  */
   1886 void
   1887 adb_reinit(void)
   1888 {
   1889 	u_char send_string[ADB_MAX_MSG_LENGTH];
   1890 	int s = 0;
   1891 	volatile int i, x;
   1892 	int command;
   1893 	int result;
   1894 	int saveptr;		/* point to next free relocation address */
   1895 	int device;
   1896 	int nonewtimes;		/* times thru loop w/o any new devices */
   1897 	ADBDataBlock data;	/* temp. holder for getting device info */
   1898 
   1899 	(void)(&s);		/* work around lame GCC bug */
   1900 
   1901 	/* Make sure we are not interrupted while building the table. */
   1902 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
   1903 		s = splhigh();
   1904 
   1905 	ADBNumDevices = 0;	/* no devices yet */
   1906 
   1907 	/* Let intr routines know we are running reinit */
   1908 	adbStarting = 1;
   1909 
   1910 	/* Initialize the ADB table.  For now, we'll always use the same table
   1911 	 * that is defined at the beginning of this file - no mallocs. */
   1912 	for (i = 0; i < 16; i++)
   1913 		ADBDevTable[i].devType = 0;
   1914 
   1915 	adb_setup_hw_type();	/* setup hardware type */
   1916 
   1917 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
   1918 
   1919 	/* send an ADB reset first */
   1920 	adb_op_sync((Ptr) 0, (Ptr) 0, (Ptr) 0, (short) 0x00);
   1921 
   1922 	/* Probe for ADB devices. Probe devices 1-15 quickly to determine
   1923 	 * which device addresses are in use and which are free. For each
   1924 	 * address that is in use, move the device at that address to a higher
   1925 	 * free address. Continue doing this at that address until no device
   1926 	 * responds at that address. Then move the last device that was moved
   1927 	 * back to the original address. Do this for the remaining addresses
   1928 	 * that we determined were in use.
   1929 	 *
   1930 	 * When finished, do this entire process over again with the updated list
   1931 	 * of in use addresses. Do this until no new devices have been found
   1932 	 * in 20 passes though the in use address list. (This probably seems
   1933 	 * long and complicated, but it's the best way to detect multiple
   1934 	 * devices at the same address - sometimes it takes a couple of tries
   1935 	 * before the collision is detected.) */
   1936 
   1937 	/* initial scan through the devices */
   1938 	for (i = 1; i < 16; i++) {
   1939 		command = (int) (0x0f | ((int) (i & 0x000f) << 4));	/* talk R3 */
   1940 		result = adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
   1941 		if (0x00 != send_string[0]) {	/* anything come back ?? */
   1942 			ADBDevTable[++ADBNumDevices].devType = (u_char) send_string[2];
   1943 			ADBDevTable[ADBNumDevices].origAddr = i;
   1944 			ADBDevTable[ADBNumDevices].currentAddr = i;
   1945 			ADBDevTable[ADBNumDevices].DataAreaAddr = (long) 0;
   1946 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *) 0;
   1947 			pm_check_adb_devices(i);	/* tell pm driver device
   1948 							 * is here */
   1949 		}
   1950 	}
   1951 
   1952 	/* find highest unused address */
   1953 	for (saveptr = 15; saveptr > 0; saveptr--)
   1954 		if (-1 == get_adb_info(&data, saveptr))
   1955 			break;
   1956 
   1957 	if (saveptr == 0)	/* no free addresses??? */
   1958 		saveptr = 15;
   1959 
   1960 	/* printf_intr("first free is: 0x%02x\n", saveptr); */
   1961 	/* printf_intr("devices: %i\n", ADBNumDevices); */
   1962 
   1963 	nonewtimes = 0;		/* no loops w/o new devices */
   1964 	while (nonewtimes++ < 11) {
   1965 		for (i = 1; i <= ADBNumDevices; i++) {
   1966 			device = ADBDevTable[i].currentAddr;
   1967 			/* printf_intr("moving device 0x%02x to 0x%02x (index
   1968 			 * 0x%02x)  ", device, saveptr, i); */
   1969 
   1970 			/* send TALK R3 to address */
   1971 			command = (int) (0x0f | ((int) (device & 0x000f) << 4));
   1972 			adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
   1973 
   1974 			/* move device to higher address */
   1975 			command = (int) (0x0b | ((int) (device & 0x000f) << 4));
   1976 			send_string[0] = 2;
   1977 			send_string[1] = (u_char) (saveptr | 0x60);
   1978 			send_string[2] = 0xfe;
   1979 			adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
   1980 
   1981 			/* send TALK R3 - anything at old address? */
   1982 			command = (int) (0x0f | ((int) (device & 0x000f) << 4));
   1983 			result = adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
   1984 			if (send_string[0] != 0) {
   1985 				/* new device found */
   1986 				/* update data for previously moved device */
   1987 				ADBDevTable[i].currentAddr = saveptr;
   1988 				/* printf_intr("old device at index %i\n",i); */
   1989 				/* add new device in table */
   1990 				/* printf_intr("new device found\n"); */
   1991 				ADBDevTable[++ADBNumDevices].devType = (u_char) send_string[2];
   1992 				ADBDevTable[ADBNumDevices].origAddr = device;
   1993 				ADBDevTable[ADBNumDevices].currentAddr = device;
   1994 				/* These will be set correctly in adbsys.c */
   1995 				/* Until then, unsol. data will be ignored. */
   1996 				ADBDevTable[ADBNumDevices].DataAreaAddr = (long) 0;
   1997 				ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *) 0;
   1998 				/* find next unused address */
   1999 				for (x = saveptr; x > 0; x--)
   2000 					if (-1 == get_adb_info(&data, x)) {
   2001 						saveptr = x;
   2002 						break;
   2003 					}
   2004 				/* printf_intr("new free is 0x%02x\n",
   2005 				 * saveptr); */
   2006 				nonewtimes = 0;
   2007 				/* tell pm driver device is here */
   2008 				pm_check_adb_devices(device);
   2009 			} else {
   2010 				/* printf_intr("moving back...\n"); */
   2011 				/* move old device back */
   2012 				command = (int) (0x0b | ((int) (saveptr & 0x000f) << 4));
   2013 				send_string[0] = 2;
   2014 				send_string[1] = (u_char) (device | 0x60);
   2015 				send_string[2] = 0xfe;
   2016 				adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
   2017 			}
   2018 		}
   2019 	}
   2020 
   2021 #ifdef ADB_DEBUG
   2022 	if (adb_debug) {
   2023 		for (i = 1; i <= ADBNumDevices; i++) {
   2024 			x = get_ind_adb_info(&data, i);
   2025 			if (x != -1)
   2026 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
   2027 				    i, x, data.devType);
   2028 		}
   2029 	}
   2030 #endif
   2031 
   2032 	adb_prog_switch_enable();	/* enable the programmer's switch, if
   2033 					 * we have one */
   2034 
   2035 	if (0 == ADBNumDevices)	/* tell user if no devices found */
   2036 		printf_intr("adb: no devices found\n");
   2037 
   2038 	adbStarting = 0;	/* not starting anymore */
   2039 	printf_intr("adb: ADBReInit complete\n");
   2040 
   2041 	if (adbHardware==ADB_HW_CUDA)
   2042 		timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
   2043 
   2044 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
   2045 		splx(s);
   2046 	return;
   2047 }
   2048 
   2049 
   2050 /*
   2051  * adb_comp_exec
   2052  * This is a general routine that calls the completion routine if there is one.
   2053  * NOTE: This routine is now only used by pm_direct.c
   2054  *       All the code in this file (adb_direct.c) uses
   2055  *       the adb_pass_up routine now.
   2056  */
   2057 void
   2058 adb_comp_exec(void)
   2059 {
   2060         if ((long) 0 != adbCompRout)    /* don't call if empty return location */
   2061 #ifdef __NetBSD__
   2062                 asm("
   2063                     movml #0xffff, sp@-         | save all registers
   2064                     movl %0, a2                 | adbCompData
   2065                     movl %1, a1                 | adbCompRout
   2066                     movl %2, a0                 | adbBuffer
   2067                     movl %3, d0                 | adbWaitingCmd
   2068                     jbsr a1@                    | go call the routine
   2069                     movml sp@+, #0xffff         | restore all registers"
   2070                     :
   2071                     :"g"(adbCompData), "g"(adbCompRout),
   2072                      "g"(adbBuffer), "g"(adbWaitingCmd)
   2073                     :"d0", "a0", "a1", "a2");
   2074 #else                                   /* for macos based testing */
   2075                 asm {
   2076                         movem.l a0/a1/a2/d0, -(a7)
   2077                         move.l adbCompData, a2
   2078                         move.l adbCompRout, a1
   2079                         move.l adbBuffer, a0
   2080                         move.w adbWaitingCmd, d0
   2081                         jsr(a1)
   2082                         movem.l(a7) +, d0/a2/a1/a0
   2083                 }
   2084 #endif
   2085 }
   2086 
   2087 
   2088 /* adb_cmd_result
   2089  * This routine lets the caller know whether the specified adb command string should
   2090  * expect a returned result, such as a TALK command.
   2091  * returns: 0 if a result should be expected
   2092  *          1 if a result should NOT be expected
   2093  */
   2094 int
   2095 adb_cmd_result(u_char * in)
   2096 {
   2097 	switch (adbHardware) {
   2098 		case ADB_HW_II:
   2099 		/* was it an ADB talk command? */
   2100 		if ((in[1] & 0x0c) == 0x0c)
   2101 			return 0;
   2102 		else
   2103 			return 1;
   2104 		break;
   2105 
   2106 	case ADB_HW_IISI:
   2107 	case ADB_HW_CUDA:
   2108 		/* was is an ADB talk command? */
   2109 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
   2110 			return 0;
   2111 		/* was is an RTC/PRAM read date/time? */
   2112 		else
   2113 			if ((in[1] == 0x01) && (in[2] == 0x03))
   2114 				return 0;
   2115 			else
   2116 				return 1;
   2117 		break;
   2118 
   2119 	case ADB_HW_PB:
   2120 		return 1;
   2121 		break;
   2122 
   2123 	case ADB_HW_UNKNOWN:
   2124 	default:
   2125 		return 1;
   2126 	}
   2127 }
   2128 
   2129 
   2130 /* adb_cmd_extra
   2131  * This routine lets the caller know whether the specified adb command string may have
   2132  * extra data appended to the end of it, such as a LISTEN command.
   2133  * returns: 0 if extra data is allowed
   2134  *          1 if extra data is NOT allowed
   2135  */
   2136 int
   2137 adb_cmd_extra(u_char * in)
   2138 {
   2139 	switch (adbHardware) {
   2140 		case ADB_HW_II:
   2141 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
   2142 			return 0;
   2143 		else
   2144 			return 1;
   2145 		break;
   2146 
   2147 	case ADB_HW_IISI:
   2148 	case ADB_HW_CUDA:
   2149 		/* TO DO: support needs to be added to recognize RTC and PRAM
   2150 		 * commands */
   2151 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
   2152 			return 0;
   2153 		else		/* add others later */
   2154 			return 1;
   2155 		break;
   2156 
   2157 	case ADB_HW_PB:
   2158 		return 1;
   2159 		break;
   2160 
   2161 	case ADB_HW_UNKNOWN:
   2162 	default:
   2163 		return 1;
   2164 	}
   2165 }
   2166 
   2167 
   2168 /* adb_op_sync
   2169  * This routine does exactly what the adb_op routine does, except that after the
   2170  * adb_op is called, it waits until the return value is present before returning.
   2171  * NOTE: The user specified compRout is ignored, since this routine specifies
   2172  * it's own to adb_op, which is why you really called this in the first place
   2173  * anyway.
   2174  */
   2175 int
   2176 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
   2177 {
   2178 	int result;
   2179 	volatile int flag = 0;
   2180 
   2181 	result = adb_op(buffer, (void *) adb_op_comprout,
   2182 	    (void *) &flag, command);	/* send command */
   2183 	if (result == 0) {	/* send ok? */
   2184 		while (0 == flag);	/* wait for compl. routine */
   2185 		return 0;
   2186 	} else
   2187 		return result;
   2188 }
   2189 
   2190 
   2191 /* adb_op_comprout
   2192  * This function is used by the adb_op_sync routine so it knows when the function is
   2193  * done.
   2194  */
   2195 void
   2196 adb_op_comprout(void)
   2197 {
   2198 #ifdef __NetBSD__
   2199 	asm("movw	#1,a2@			| update flag value");
   2200 #else				/* for macos based testing */
   2201 	asm {
   2202 		move.w #1,(a2) }		/* update flag value */
   2203 #endif
   2204 }
   2205 
   2206 void
   2207 adb_setup_hw_type(void)
   2208 {
   2209 	long response;
   2210 
   2211 	response = mac68k_machine.machineid;
   2212 
   2213 	/*
   2214 	 * Determine what type of ADB hardware we are running on.
   2215 	 */
   2216 	switch (response) {
   2217 	case 6:		/* II */
   2218 	case 7:		/* IIx */
   2219 	case 8:		/* IIcx */
   2220 	case 9:		/* SE/30 */
   2221 	case 11:	/* IIci */
   2222 	case 22:	/* Quadra 700 */
   2223 	case 30:	/* Centris 650 */
   2224 	case 35:	/* Quadra 800 */
   2225 	case 36:	/* Quadra 650 */
   2226 	case 52:	/* Centris 610 */
   2227 	case 53:	/* Quadra 610 */
   2228 		adbHardware = ADB_HW_II;
   2229 		printf_intr("adb: using II series hardware support\n");
   2230 		break;
   2231 	case 18:	/* IIsi */
   2232 	case 20:	/* Quadra 900 - not sure if IIsi or not */
   2233 	case 23:	/* Classic II */
   2234 	case 26:	/* Quadra 950 - not sure if IIsi or not */
   2235 	case 27:	/* LC III, Performa 450 */
   2236 	case 37:	/* LC II, Performa 400/405/430 */
   2237 	case 44:	/* IIvi */
   2238 	case 45:	/* Performa 600 */
   2239 	case 48:	/* IIvx */
   2240 	case 62:	/* Performa 460/465/467 */
   2241 		adbHardware = ADB_HW_IISI;
   2242 		printf_intr("adb: using IIsi series hardware support\n");
   2243 		break;
   2244 	case 21:	/* PowerBook 170 */
   2245 	case 25:	/* PowerBook 140 */
   2246 	case 54:	/* PowerBook 145 */
   2247 	case 34:	/* PowerBook 160 */
   2248 	case 84:	/* PowerBook 165 */
   2249 	case 50:	/* PowerBook 165c */
   2250 	case 33:	/* PowerBook 180 */
   2251 	case 71:	/* PowerBook 180c */
   2252 	case 115:	/* PowerBook 150 */
   2253 		adbHardware = ADB_HW_PB;
   2254 		pm_setup_adb();
   2255 		printf_intr("adb: using PowerBook 100-series hardware support\n");
   2256 		break;
   2257 	case 29:	/* PowerBook Duo 210 */
   2258 	case 32:	/* PowerBook Duo 230 */
   2259 	case 38:	/* PowerBook Duo 250 */
   2260 	case 72:	/* PowerBook 500 series */
   2261 	case 77:	/* PowerBook Duo 270 */
   2262 	case 102:	/* PowerBook Duo 280 */
   2263 	case 103:	/* PowerBook Duo 280c */
   2264 		adbHardware = ADB_HW_PB;
   2265 		pm_setup_adb();
   2266 		printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
   2267 		break;
   2268 	case 49:	/* Color Classic */
   2269 	case 56:	/* LC 520 */
   2270 	case 60:	/* Centris 660AV */
   2271 	case 78:	/* Quadra 840AV */
   2272 	case 80:	/* LC 550, Performa 550 */
   2273 	case 83:	/* Color Classic II */
   2274 	case 89:	/* LC 475, Performa 475/476 */
   2275 	case 92:	/* LC 575, Performa 575/577/578 */
   2276 	case 94:	/* Quadra 605 */
   2277 	case 98:	/* LC 630, Performa 630, Quadra 630 */
   2278 		adbHardware = ADB_HW_CUDA;
   2279 		printf_intr("adb: using Cuda series hardware support\n");
   2280 		break;
   2281 	default:
   2282 		adbHardware = ADB_HW_UNKNOWN;
   2283 		printf_intr("adb: hardware type unknown for this machine\n");
   2284 		printf_intr("adb: ADB support is disabled\n");
   2285 		break;
   2286 	}
   2287 
   2288 	/*
   2289 	 * Determine whether this machine has ADB based soft power.
   2290 	 */
   2291 	switch (response) {
   2292 	case 18:	/* IIsi */
   2293 	case 20:	/* Quadra 900 - not sure if IIsi or not */
   2294 	case 26:	/* Quadra 950 - not sure if IIsi or not */
   2295 	case 44:	/* IIvi */
   2296 	case 45:	/* Performa 600 */
   2297 	case 48:	/* IIvx */
   2298 	case 49:	/* Color Classic */
   2299 	case 83:	/* Color Classic II */
   2300 	case 56:	/* LC 520 */
   2301 	case 78:	/* Quadra 840AV */
   2302 	case 80:	/* LC 550, Performa 550 */
   2303 	case 92:	/* LC 575, Performa 575/577/578 */
   2304 	case 98:	/* LC 630, Performa 630, Quadra 630 */
   2305 		adbSoftPower=1;
   2306 		break;
   2307 	}
   2308 }
   2309 
   2310 int
   2311 count_adbs(void)
   2312 {
   2313 	int i;
   2314 	int found;
   2315 
   2316 	found = 0;
   2317 
   2318 	for (i = 1; i < 16; i++)
   2319 		if (0 != ADBDevTable[i].devType)
   2320 			found++;
   2321 
   2322 	return found;
   2323 }
   2324 
   2325 int
   2326 get_ind_adb_info(ADBDataBlock * info, int index)
   2327 {
   2328 	if ((index < 1) || (index > 15))	/* check range 1-15 */
   2329 		return (-1);
   2330 
   2331 	/* printf_intr("index 0x%x devType is: 0x%x\n", index,
   2332 	    ADBDevTable[index].devType); */
   2333 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
   2334 		return (-1);
   2335 
   2336 	info->devType = ADBDevTable[index].devType;
   2337 	info->origADBAddr = ADBDevTable[index].origAddr;
   2338 	info->dbServiceRtPtr = (Ptr) ADBDevTable[index].ServiceRtPtr;
   2339 	info->dbDataAreaAddr = (Ptr) ADBDevTable[index].DataAreaAddr;
   2340 
   2341 	return (ADBDevTable[index].currentAddr);
   2342 }
   2343 
   2344 int
   2345 get_adb_info(ADBDataBlock * info, int adbAddr)
   2346 {
   2347 	int i;
   2348 
   2349 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   2350 		return (-1);
   2351 
   2352 	for (i = 1; i < 15; i++)
   2353 		if (ADBDevTable[i].currentAddr == adbAddr) {
   2354 			info->devType = ADBDevTable[i].devType;
   2355 			info->origADBAddr = ADBDevTable[i].origAddr;
   2356 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
   2357 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
   2358 			return 0;	/* found */
   2359 		}
   2360 
   2361 	return (-1);		/* not found */
   2362 }
   2363 
   2364 int
   2365 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
   2366 {
   2367 	int i;
   2368 
   2369 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   2370 		return (-1);
   2371 
   2372 	for (i = 1; i < 15; i++)
   2373 		if (ADBDevTable[i].currentAddr == adbAddr) {
   2374 			ADBDevTable[i].ServiceRtPtr =
   2375 			    (void *)(info->siServiceRtPtr);
   2376 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
   2377 			return 0;	/* found */
   2378 		}
   2379 
   2380 	return (-1);		/* not found */
   2381 
   2382 }
   2383 
   2384 #ifndef MRG_ADB
   2385 long
   2386 mrg_adbintr(void)
   2387 {
   2388 	adb_intr();
   2389 	return 1;	/* mimic mrg_adbintr in macrom.h just in case */
   2390 }
   2391 
   2392 long
   2393 mrg_pmintr(void)
   2394 {
   2395 	pm_intr();
   2396 	return 1;	/* mimic mrg_pmintr in macrom.h just in case */
   2397 }
   2398 #endif
   2399 
   2400 /* caller should really use machine-independant version: getPramTime */
   2401 /* this version does pseudo-adb access only */
   2402 int
   2403 adb_read_date_time(unsigned long *time)
   2404 {
   2405 	u_char output[ADB_MAX_MSG_LENGTH];
   2406 	int result;
   2407 	volatile int flag = 0;
   2408 
   2409 	switch (adbHardware) {
   2410 	case ADB_HW_II:
   2411 		return -1;
   2412 
   2413 	case ADB_HW_IISI:
   2414 		output[0] = 0x02;	/* 2 byte message */
   2415 		output[1] = 0x01;	/* to pram/rtc device */
   2416 		output[2] = 0x03;	/* read date/time */
   2417 		result = send_adb_IIsi((u_char *) output,
   2418 		    (u_char *) output, (void *) adb_op_comprout,
   2419 		    (int *) &flag, (int) 0);
   2420 		if (result != 0)	/* exit if not sent */
   2421 			return -1;
   2422 
   2423 		while (0 == flag)	/* wait for result */
   2424 			;
   2425 
   2426 		*time = (long) (*(long *) (output + 1));
   2427 		return 0;
   2428 
   2429 	case ADB_HW_PB:
   2430 		return -1;
   2431 
   2432 	case ADB_HW_CUDA:
   2433 		output[0] = 0x02;	/* 2 byte message */
   2434 		output[1] = 0x01;	/* to pram/rtc device */
   2435 		output[2] = 0x03;	/* read date/time */
   2436 		result = send_adb_cuda((u_char *) output,
   2437 		    (u_char *) output, (void *) adb_op_comprout,
   2438 		    (void *) &flag, (int) 0);
   2439 		if (result != 0)	/* exit if not sent */
   2440 			return -1;
   2441 
   2442 		while (0 == flag)	/* wait for result */
   2443 			;
   2444 
   2445 		*time = (long) (*(long *) (output + 1));
   2446 		return 0;
   2447 
   2448 	case ADB_HW_UNKNOWN:
   2449 	default:
   2450 		return -1;
   2451 	}
   2452 }
   2453 
   2454 /* caller should really use machine-independant version: setPramTime */
   2455 /* this version does pseudo-adb access only */
   2456 int
   2457 adb_set_date_time(unsigned long time)
   2458 {
   2459 	u_char output[ADB_MAX_MSG_LENGTH];
   2460 	int result;
   2461 	volatile int flag = 0;
   2462 
   2463 	switch (adbHardware) {
   2464 	case ADB_HW_II:
   2465 		return -1;
   2466 
   2467 	case ADB_HW_IISI:
   2468 		output[0] = 0x06;	/* 6 byte message */
   2469 		output[1] = 0x01;	/* to pram/rtc device */
   2470 		output[2] = 0x09;	/* set date/time */
   2471 		output[3] = (u_char) (time >> 24);
   2472 		output[4] = (u_char) (time >> 16);
   2473 		output[5] = (u_char) (time >> 8);
   2474 		output[6] = (u_char) (time);
   2475 		result = send_adb_IIsi((u_char *) output,
   2476 		    (u_char *) 0, (void *) adb_op_comprout,
   2477 		    (void *) &flag, (int) 0);
   2478 		if (result != 0)	/* exit if not sent */
   2479 			return -1;
   2480 
   2481 		while (0 == flag)	/* wait for send to finish */
   2482 			;
   2483 
   2484 		return 0;
   2485 
   2486 	case ADB_HW_PB:
   2487 		return -1;
   2488 
   2489 	case ADB_HW_CUDA:
   2490 		output[0] = 0x06;	/* 6 byte message */
   2491 		output[1] = 0x01;	/* to pram/rtc device */
   2492 		output[2] = 0x09;	/* set date/time */
   2493 		output[3] = (u_char) (time >> 24);
   2494 		output[4] = (u_char) (time >> 16);
   2495 		output[5] = (u_char) (time >> 8);
   2496 		output[6] = (u_char) (time);
   2497 		result = send_adb_cuda((u_char *) output,
   2498 		    (u_char *) 0, (void *) adb_op_comprout,
   2499 		    (void *) &flag, (int) 0);
   2500 		if (result != 0)	/* exit if not sent */
   2501 			return -1;
   2502 
   2503 		while (0 == flag)	/* wait for send to finish */
   2504 			;
   2505 
   2506 		return 0;
   2507 
   2508 	case ADB_HW_UNKNOWN:
   2509 	default:
   2510 		return -1;
   2511 	}
   2512 }
   2513 
   2514 
   2515 int
   2516 adb_poweroff(void)
   2517 {
   2518 	u_char output[ADB_MAX_MSG_LENGTH];
   2519 	int result;
   2520 
   2521 	if (!adbSoftPower)
   2522 		return -1;
   2523 
   2524 	switch (adbHardware) {
   2525 	case ADB_HW_IISI:
   2526 		output[0] = 0x02;	/* 2 byte message */
   2527 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2528 		output[2] = 0x0a;	/* set date/time */
   2529 		result = send_adb_IIsi((u_char *) output,
   2530 		    (u_char *) 0, (void *) 0, (void *) 0, (int) 0);
   2531 		if (result != 0)	/* exit if not sent */
   2532 			return -1;
   2533 
   2534 		for (;;);		/* wait for power off */
   2535 
   2536 		return 0;
   2537 
   2538 	case ADB_HW_PB:
   2539 		return -1;
   2540 
   2541 	case ADB_HW_CUDA:
   2542 		output[0] = 0x02;	/* 2 byte message */
   2543 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2544 		output[2] = 0x0a;	/* set date/time */
   2545 		result = send_adb_cuda((u_char *) output,
   2546 		    (u_char *) 0, (void *) 0, (void *) 0, (int) 0);
   2547 		if (result != 0)	/* exit if not sent */
   2548 			return -1;
   2549 
   2550 		for (;;);		/* wait for power off */
   2551 
   2552 		return 0;
   2553 
   2554 	case ADB_HW_II:			/* II models don't do ADB soft power */
   2555 	case ADB_HW_UNKNOWN:
   2556 	default:
   2557 		return -1;
   2558 	}
   2559 }
   2560 
   2561 int
   2562 adb_prog_switch_enable(void)
   2563 {
   2564 	u_char output[ADB_MAX_MSG_LENGTH];
   2565 	int result;
   2566 	volatile int flag = 0;
   2567 
   2568 	switch (adbHardware) {
   2569 	case ADB_HW_IISI:
   2570 		output[0] = 0x03;	/* 3 byte message */
   2571 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2572 		output[2] = 0x1c;	/* prog. switch control */
   2573 		output[3] = 0x01;	/* enable */
   2574 		result = send_adb_IIsi((u_char *) output,
   2575 		    (u_char *) 0, (void *) adb_op_comprout,
   2576 		    (void *) &flag, (int) 0);
   2577 		if (result != 0)	/* exit if not sent */
   2578 			return -1;
   2579 
   2580 		while (0 == flag)	/* wait for send to finish */
   2581 			;
   2582 
   2583 		return 0;
   2584 
   2585 	case ADB_HW_PB:
   2586 		return -1;
   2587 
   2588 	case ADB_HW_II:		/* II models don't do prog. switch */
   2589 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
   2590 	case ADB_HW_UNKNOWN:
   2591 	default:
   2592 		return -1;
   2593 	}
   2594 }
   2595 
   2596 int
   2597 adb_prog_switch_disable(void)
   2598 {
   2599 	u_char output[ADB_MAX_MSG_LENGTH];
   2600 	int result;
   2601 	volatile int flag = 0;
   2602 
   2603 	switch (adbHardware) {
   2604 	case ADB_HW_IISI:
   2605 		output[0] = 0x03;	/* 3 byte message */
   2606 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   2607 		output[2] = 0x1c;	/* prog. switch control */
   2608 		output[3] = 0x01;	/* disable */
   2609 		result = send_adb_IIsi((u_char *) output,
   2610 		    (u_char *) 0, (void *) adb_op_comprout,
   2611 		    (void *) &flag, (int) 0);
   2612 		if (result != 0)	/* exit if not sent */
   2613 			return -1;
   2614 
   2615 		while (0 == flag)	/* wait for send to finish */
   2616 			;
   2617 
   2618 		return 0;
   2619 
   2620 	case ADB_HW_PB:
   2621 		return -1;
   2622 
   2623 	case ADB_HW_II:		/* II models don't do prog. switch */
   2624 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
   2625 	case ADB_HW_UNKNOWN:
   2626 	default:
   2627 		return -1;
   2628 	}
   2629 }
   2630 
   2631 #ifndef MRG_ADB
   2632 
   2633 int
   2634 CountADBs(void)
   2635 {
   2636 	return (count_adbs());
   2637 }
   2638 
   2639 void
   2640 ADBReInit(void)
   2641 {
   2642 	adb_reinit();
   2643 }
   2644 
   2645 int
   2646 GetIndADB(ADBDataBlock * info, int index)
   2647 {
   2648 	return (get_ind_adb_info(info, index));
   2649 }
   2650 
   2651 int
   2652 GetADBInfo(ADBDataBlock * info, int adbAddr)
   2653 {
   2654 	return (get_adb_info(info, adbAddr));
   2655 }
   2656 
   2657 int
   2658 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
   2659 {
   2660 	return (set_adb_info(info, adbAddr));
   2661 }
   2662 
   2663 int
   2664 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
   2665 {
   2666 	return (adb_op(buffer, compRout, data, commandNum));
   2667 }
   2668 
   2669 #endif
   2670 
   2671