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