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