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