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