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