adb_direct.c revision 1.16 1 /* $NetBSD: adb_direct.c,v 1.16 2001/04/01 10:40:45 tsubai 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 #include <sys/param.h>
63 #include <sys/cdefs.h>
64 #include <sys/systm.h>
65 #include <sys/callout.h>
66 #include <sys/device.h>
67
68 #include <machine/param.h>
69 #include <machine/cpu.h>
70 #include <machine/adbsys.h>
71
72 #include <macppc/dev/viareg.h>
73 #include <macppc/dev/adbvar.h>
74
75 #define printf_intr printf
76
77 #ifdef DEBUG
78 #ifndef ADB_DEBUG
79 #define ADB_DEBUG
80 #endif
81 #endif
82
83 /* some misc. leftovers */
84 #define vPB 0x0000
85 #define vPB3 0x08
86 #define vPB4 0x10
87 #define vPB5 0x20
88 #define vSR_INT 0x04
89 #define vSR_OUT 0x10
90
91 /* the type of ADB action that we are currently preforming */
92 #define ADB_ACTION_NOTREADY 0x1 /* has not been initialized yet */
93 #define ADB_ACTION_IDLE 0x2 /* the bus is currently idle */
94 #define ADB_ACTION_OUT 0x3 /* sending out a command */
95 #define ADB_ACTION_IN 0x4 /* receiving data */
96 #define ADB_ACTION_POLLING 0x5 /* polling - II only */
97
98 /*
99 * These describe the state of the ADB bus itself, although they
100 * don't necessarily correspond directly to ADB states.
101 * Note: these are not really used in the IIsi code.
102 */
103 #define ADB_BUS_UNKNOWN 0x1 /* we don't know yet - all models */
104 #define ADB_BUS_IDLE 0x2 /* bus is idle - all models */
105 #define ADB_BUS_CMD 0x3 /* starting a command - II models */
106 #define ADB_BUS_ODD 0x4 /* the "odd" state - II models */
107 #define ADB_BUS_EVEN 0x5 /* the "even" state - II models */
108 #define ADB_BUS_ACTIVE 0x6 /* active state - IIsi models */
109 #define ADB_BUS_ACK 0x7 /* currently ACKing - IIsi models */
110
111 /*
112 * Shortcuts for setting or testing the VIA bit states.
113 * Not all shortcuts are used for every type of ADB hardware.
114 */
115 #define ADB_SET_STATE_IDLE_II() via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
116 #define ADB_SET_STATE_IDLE_IISI() via_reg_and(VIA1, vBufB, ~(vPB4 | vPB5))
117 #define ADB_SET_STATE_IDLE_CUDA() via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
118 #define ADB_SET_STATE_CMD() via_reg_and(VIA1, vBufB, ~(vPB4 | vPB5))
119 #define ADB_SET_STATE_EVEN() write_via_reg(VIA1, vBufB, \
120 (read_via_reg(VIA1, vBufB) | vPB4) & ~vPB5)
121 #define ADB_SET_STATE_ODD() write_via_reg(VIA1, vBufB, \
122 (read_via_reg(VIA1, vBufB) | vPB5) & ~vPB4 )
123 #define ADB_SET_STATE_ACTIVE() via_reg_or(VIA1, vBufB, vPB5)
124 #define ADB_SET_STATE_INACTIVE() via_reg_and(VIA1, vBufB, ~vPB5)
125 #define ADB_SET_STATE_TIP() via_reg_and(VIA1, vBufB, ~vPB5)
126 #define ADB_CLR_STATE_TIP() via_reg_or(VIA1, vBufB, vPB5)
127 #define ADB_SET_STATE_ACKON() via_reg_or(VIA1, vBufB, vPB4)
128 #define ADB_SET_STATE_ACKOFF() via_reg_and(VIA1, vBufB, ~vPB4)
129 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
130 #define ADB_SET_STATE_ACKON_CUDA() via_reg_and(VIA1, vBufB, ~vPB4)
131 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
132 #define ADB_SET_SR_INPUT() via_reg_and(VIA1, vACR, ~vSR_OUT)
133 #define ADB_SET_SR_OUTPUT() via_reg_or(VIA1, vACR, vSR_OUT)
134 #define ADB_SR() read_via_reg(VIA1, vSR)
135 #define ADB_VIA_INTR_ENABLE() write_via_reg(VIA1, vIER, 0x84)
136 #define ADB_VIA_INTR_DISABLE() write_via_reg(VIA1, vIER, 0x04)
137 #define ADB_VIA_CLR_INTR() write_via_reg(VIA1, vIFR, 0x04)
138 #define ADB_INTR_IS_OFF (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
139 #define ADB_INTR_IS_ON (0 == (read_via_reg(VIA1, vBufB) & vPB3))
140 #define ADB_SR_INTR_IS_OFF (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
141 #define ADB_SR_INTR_IS_ON (vSR_INT == (read_via_reg(VIA1, \
142 vIFR) & vSR_INT))
143
144 /*
145 * This is the delay that is required (in uS) between certain
146 * ADB transactions. The actual timing delay for for each uS is
147 * calculated at boot time to account for differences in machine speed.
148 */
149 #define ADB_DELAY 150
150
151 /*
152 * Maximum ADB message length; includes space for data, result, and
153 * device code - plus a little for safety.
154 */
155 #define ADB_MAX_MSG_LENGTH 16
156 #define ADB_MAX_HDR_LENGTH 8
157
158 #define ADB_QUEUE 32
159 #define ADB_TICKLE_TICKS 4
160
161 /*
162 * A structure for storing information about each ADB device.
163 */
164 struct ADBDevEntry {
165 void (*ServiceRtPtr) __P((void));
166 void *DataAreaAddr;
167 int devType;
168 int origAddr;
169 int currentAddr;
170 };
171
172 /*
173 * Used to hold ADB commands that are waiting to be sent out.
174 */
175 struct adbCmdHoldEntry {
176 u_char outBuf[ADB_MAX_MSG_LENGTH]; /* our message */
177 u_char *saveBuf; /* buffer to know where to save result */
178 u_char *compRout; /* completion routine pointer */
179 u_char *data; /* completion routine data pointer */
180 };
181
182 /*
183 * Eventually used for two separate queues, the queue between
184 * the upper and lower halves, and the outgoing packet queue.
185 * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
186 */
187 struct adbCommand {
188 u_char header[ADB_MAX_HDR_LENGTH]; /* not used yet */
189 u_char data[ADB_MAX_MSG_LENGTH]; /* packet data only */
190 u_char *saveBuf; /* where to save result */
191 u_char *compRout; /* completion routine pointer */
192 u_char *compData; /* completion routine data pointer */
193 u_int cmd; /* the original command for this data */
194 u_int unsol; /* 1 if packet was unsolicited */
195 u_int ack_only; /* 1 for no special processing */
196 };
197
198 /*
199 * A few variables that we need and their initial values.
200 */
201 int adbHardware = ADB_HW_UNKNOWN;
202 int adbActionState = ADB_ACTION_NOTREADY;
203 int adbBusState = ADB_BUS_UNKNOWN;
204 int adbWaiting = 0; /* waiting for return data from the device */
205 int adbWriteDelay = 0; /* working on (or waiting to do) a write */
206 int adbOutQueueHasData = 0; /* something in the queue waiting to go out */
207 int adbNextEnd = 0; /* the next incoming bute is the last (II) */
208 int adbSoftPower = 0; /* machine supports soft power */
209
210 int adbWaitingCmd = 0; /* ADB command we are waiting for */
211 u_char *adbBuffer = (long)0; /* pointer to user data area */
212 void *adbCompRout = (long)0; /* pointer to the completion routine */
213 void *adbCompData = (long)0; /* pointer to the completion routine data */
214 long adbFakeInts = 0; /* keeps track of fake ADB interrupts for
215 * timeouts (II) */
216 int adbStarting = 1; /* doing ADBReInit so do polling differently */
217 int adbSendTalk = 0; /* the intr routine is sending the talk, not
218 * the user (II) */
219 int adbPolling = 0; /* we are polling for service request */
220 int adbPollCmd = 0; /* the last poll command we sent */
221
222 u_char adbInputBuffer[ADB_MAX_MSG_LENGTH]; /* data input buffer */
223 u_char adbOutputBuffer[ADB_MAX_MSG_LENGTH]; /* data output buffer */
224 struct adbCmdHoldEntry adbOutQueue; /* our 1 entry output queue */
225
226 int adbSentChars = 0; /* how many characters we have sent */
227 int adbLastDevice = 0; /* last ADB dev we heard from (II ONLY) */
228 int adbLastDevIndex = 0; /* last ADB dev loc in dev table (II ONLY) */
229 int adbLastCommand = 0; /* the last ADB command we sent (II) */
230
231 struct ADBDevEntry ADBDevTable[16]; /* our ADB device table */
232 int ADBNumDevices; /* num. of ADB devices found with ADBReInit */
233
234 struct adbCommand adbInbound[ADB_QUEUE]; /* incoming queue */
235 int adbInCount = 0; /* how many packets in in queue */
236 int adbInHead = 0; /* head of in queue */
237 int adbInTail = 0; /* tail of in queue */
238 struct adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
239 int adbOutCount = 0; /* how many packets in out queue */
240 int adbOutHead = 0; /* head of out queue */
241 int adbOutTail = 0; /* tail of out queue */
242
243 int tickle_count = 0; /* how many tickles seen for this packet? */
244 int tickle_serial = 0; /* the last packet tickled */
245 int adb_cuda_serial = 0; /* the current packet */
246
247 struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
248 struct callout adb_soft_intr_ch = CALLOUT_INITIALIZER;
249
250 volatile u_char *Via1Base;
251 extern int adb_polling; /* Are we polling? */
252
253 void pm_setup_adb __P((void));
254 void pm_check_adb_devices __P((int));
255 void pm_intr __P((void));
256 int pm_adb_op __P((u_char *, void *, void *, int));
257 void pm_init_adb_device __P((void));
258
259 /*
260 * The following are private routines.
261 */
262 #ifdef ADB_DEBUG
263 void print_single __P((u_char *));
264 #endif
265 void adb_intr __P((void));
266 void adb_intr_II __P((void));
267 void adb_intr_IIsi __P((void));
268 void adb_intr_cuda __P((void));
269 void adb_soft_intr __P((void));
270 int send_adb_II __P((u_char *, u_char *, void *, void *, int));
271 int send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
272 int send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
273 void adb_intr_cuda_test __P((void));
274 void adb_cuda_tickle __P((void));
275 void adb_pass_up __P((struct adbCommand *));
276 void adb_op_comprout __P((caddr_t, caddr_t, int));
277 void adb_reinit __P((void));
278 int count_adbs __P((void));
279 int get_ind_adb_info __P((ADBDataBlock *, int));
280 int get_adb_info __P((ADBDataBlock *, int));
281 int set_adb_info __P((ADBSetInfoBlock *, int));
282 void adb_setup_hw_type __P((void));
283 int adb_op __P((Ptr, Ptr, Ptr, short));
284 int adb_op_sync __P((Ptr, Ptr, Ptr, short));
285 void adb_read_II __P((u_char *));
286 void adb_hw_setup __P((void));
287 void adb_hw_setup_IIsi __P((u_char *));
288 void adb_comp_exec __P((void));
289 int adb_cmd_result __P((u_char *));
290 int adb_cmd_extra __P((u_char *));
291 int adb_guess_next_device __P((void));
292 int adb_prog_switch_enable __P((void));
293 int adb_prog_switch_disable __P((void));
294 /* we should create this and it will be the public version */
295 int send_adb __P((u_char *, void *, void *));
296
297 #ifdef ADB_DEBUG
298 /*
299 * print_single
300 * Diagnostic display routine. Displays the hex values of the
301 * specified elements of the u_char. The length of the "string"
302 * is in [0].
303 */
304 void
305 print_single(str)
306 u_char *str;
307 {
308 int x;
309
310 if (str == 0) {
311 printf_intr("no data - null pointer\n");
312 return;
313 }
314 if (*str == 0) {
315 printf_intr("nothing returned\n");
316 return;
317 }
318 if (*str > 20) {
319 printf_intr("ADB: ACK > 20 no way!\n");
320 *str = 20;
321 }
322 printf_intr("(length=0x%x):", *str);
323 for (x = 1; x <= *str; x++)
324 printf_intr(" 0x%02x", str[x]);
325 printf_intr("\n");
326 }
327 #endif
328
329 void
330 adb_cuda_tickle(void)
331 {
332 volatile int s;
333
334 if (adbActionState == ADB_ACTION_IN) {
335 if (tickle_serial == adb_cuda_serial) {
336 if (++tickle_count > 0) {
337 s = splhigh();
338 adbActionState = ADB_ACTION_IDLE;
339 adbInputBuffer[0] = 0;
340 ADB_SET_STATE_IDLE_CUDA();
341 splx(s);
342 }
343 } else {
344 tickle_serial = adb_cuda_serial;
345 tickle_count = 0;
346 }
347 } else {
348 tickle_serial = adb_cuda_serial;
349 tickle_count = 0;
350 }
351
352 callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
353 (void *)adb_cuda_tickle, NULL);
354 }
355
356 /*
357 * called when when an adb interrupt happens
358 *
359 * Cuda version of adb_intr
360 * TO DO: do we want to add some calls to intr_dispatch() here to
361 * grab serial interrupts?
362 */
363 void
364 adb_intr_cuda(void)
365 {
366 volatile int i, ending;
367 volatile unsigned int s;
368 struct adbCommand packet;
369
370 s = splhigh(); /* can't be too careful - might be called */
371 /* from a routine, NOT an interrupt */
372
373 ADB_VIA_CLR_INTR(); /* clear interrupt */
374 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
375
376 switch_start:
377 switch (adbActionState) {
378 case ADB_ACTION_IDLE:
379 /*
380 * This is an unexpected packet, so grab the first (dummy)
381 * byte, set up the proper vars, and tell the chip we are
382 * starting to receive the packet by setting the TIP bit.
383 */
384 adbInputBuffer[1] = ADB_SR();
385 adb_cuda_serial++;
386 if (ADB_INTR_IS_OFF) /* must have been a fake start */
387 break;
388
389 ADB_SET_SR_INPUT();
390 ADB_SET_STATE_TIP();
391
392 adbInputBuffer[0] = 1;
393 adbActionState = ADB_ACTION_IN;
394 #ifdef ADB_DEBUG
395 if (adb_debug)
396 printf_intr("idle 0x%02x ", adbInputBuffer[1]);
397 #endif
398 break;
399
400 case ADB_ACTION_IN:
401 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
402 /* intr off means this is the last byte (end of frame) */
403 if (ADB_INTR_IS_OFF)
404 ending = 1;
405 else
406 ending = 0;
407
408 if (1 == ending) { /* end of message? */
409 #ifdef ADB_DEBUG
410 if (adb_debug) {
411 printf_intr("in end 0x%02x ",
412 adbInputBuffer[adbInputBuffer[0]]);
413 print_single(adbInputBuffer);
414 }
415 #endif
416
417 /*
418 * Are we waiting AND does this packet match what we
419 * are waiting for AND is it coming from either the
420 * ADB or RTC/PRAM sub-device? This section _should_
421 * recognize all ADB and RTC/PRAM type commands, but
422 * there may be more... NOTE: commands are always at
423 * [4], even for RTC/PRAM commands.
424 */
425 /* set up data for adb_pass_up */
426 memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
427
428 if ((adbWaiting == 1) &&
429 (adbInputBuffer[4] == adbWaitingCmd) &&
430 ((adbInputBuffer[2] == 0x00) ||
431 (adbInputBuffer[2] == 0x01))) {
432 packet.saveBuf = adbBuffer;
433 packet.compRout = adbCompRout;
434 packet.compData = adbCompData;
435 packet.unsol = 0;
436 packet.ack_only = 0;
437 adb_pass_up(&packet);
438
439 adbWaitingCmd = 0; /* reset "waiting" vars */
440 adbWaiting = 0;
441 adbBuffer = (long)0;
442 adbCompRout = (long)0;
443 adbCompData = (long)0;
444 } else {
445 packet.unsol = 1;
446 packet.ack_only = 0;
447 adb_pass_up(&packet);
448 }
449
450
451 /* reset vars and signal the end of this frame */
452 adbActionState = ADB_ACTION_IDLE;
453 adbInputBuffer[0] = 0;
454 ADB_SET_STATE_IDLE_CUDA();
455 /*ADB_SET_SR_INPUT();*/
456
457 /*
458 * If there is something waiting to be sent out,
459 * the set everything up and send the first byte.
460 */
461 if (adbWriteDelay == 1) {
462 delay(ADB_DELAY); /* required */
463 adbSentChars = 0;
464 adbActionState = ADB_ACTION_OUT;
465 /*
466 * If the interrupt is on, we were too slow
467 * and the chip has already started to send
468 * something to us, so back out of the write
469 * and start a read cycle.
470 */
471 if (ADB_INTR_IS_ON) {
472 ADB_SET_SR_INPUT();
473 ADB_SET_STATE_IDLE_CUDA();
474 adbSentChars = 0;
475 adbActionState = ADB_ACTION_IDLE;
476 adbInputBuffer[0] = 0;
477 break;
478 }
479 /*
480 * If we got here, it's ok to start sending
481 * so load the first byte and tell the chip
482 * we want to send.
483 */
484 ADB_SET_STATE_TIP();
485 ADB_SET_SR_OUTPUT();
486 write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
487 }
488 } else {
489 ADB_TOGGLE_STATE_ACK_CUDA();
490 #ifdef ADB_DEBUG
491 if (adb_debug)
492 printf_intr("in 0x%02x ",
493 adbInputBuffer[adbInputBuffer[0]]);
494 #endif
495 }
496 break;
497
498 case ADB_ACTION_OUT:
499 i = ADB_SR(); /* reset SR-intr in IFR */
500 #ifdef ADB_DEBUG
501 if (adb_debug)
502 printf_intr("intr out 0x%02x ", i);
503 #endif
504
505 adbSentChars++;
506 if (ADB_INTR_IS_ON) { /* ADB intr low during write */
507 #ifdef ADB_DEBUG
508 if (adb_debug)
509 printf_intr("intr was on ");
510 #endif
511 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
512 ADB_SET_STATE_IDLE_CUDA();
513 adbSentChars = 0; /* must start all over */
514 adbActionState = ADB_ACTION_IDLE; /* new state */
515 adbInputBuffer[0] = 0;
516 adbWriteDelay = 1; /* must retry when done with
517 * read */
518 delay(ADB_DELAY);
519 goto switch_start; /* process next state right
520 * now */
521 break;
522 }
523 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
524 if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data
525 * back? */
526 adbWaiting = 1; /* signal waiting for return */
527 adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */
528 } else { /* no talk, so done */
529 /* set up stuff for adb_pass_up */
530 memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
531 packet.saveBuf = adbBuffer;
532 packet.compRout = adbCompRout;
533 packet.compData = adbCompData;
534 packet.cmd = adbWaitingCmd;
535 packet.unsol = 0;
536 packet.ack_only = 1;
537 adb_pass_up(&packet);
538
539 /* reset "waiting" vars, just in case */
540 adbWaitingCmd = 0;
541 adbBuffer = (long)0;
542 adbCompRout = (long)0;
543 adbCompData = (long)0;
544 }
545
546 adbWriteDelay = 0; /* done writing */
547 adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */
548 ADB_SET_SR_INPUT();
549 ADB_SET_STATE_IDLE_CUDA();
550 #ifdef ADB_DEBUG
551 if (adb_debug)
552 printf_intr("write done ");
553 #endif
554 } else {
555 write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); /* send next byte */
556 ADB_TOGGLE_STATE_ACK_CUDA(); /* signal byte ready to
557 * shift */
558 #ifdef ADB_DEBUG
559 if (adb_debug)
560 printf_intr("toggle ");
561 #endif
562 }
563 break;
564
565 case ADB_ACTION_NOTREADY:
566 #ifdef ADB_DEBUG
567 if (adb_debug)
568 printf_intr("adb: not yet initialized\n");
569 #endif
570 break;
571
572 default:
573 #ifdef ADB_DEBUG
574 if (adb_debug)
575 printf_intr("intr: unknown ADB state\n");
576 #endif
577 }
578
579 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
580
581 splx(s); /* restore */
582
583 return;
584 } /* end adb_intr_cuda */
585
586
587 int
588 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
589 command)
590 {
591 int s, len;
592
593 #ifdef ADB_DEBUG
594 if (adb_debug)
595 printf_intr("SEND\n");
596 #endif
597
598 if (adbActionState == ADB_ACTION_NOTREADY)
599 return 1;
600
601 /* Don't interrupt while we are messing with the ADB */
602 s = splhigh();
603
604 if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */
605 (ADB_INTR_IS_OFF)) { /* and no incoming interrupt? */
606 } else
607 if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */
608 adbWriteDelay = 1; /* if no, then we'll "queue"
609 * it up */
610 else {
611 splx(s);
612 return 1; /* really busy! */
613 }
614
615 #ifdef ADB_DEBUG
616 if (adb_debug)
617 printf_intr("QUEUE\n");
618 #endif
619 if ((long)in == (long)0) { /* need to convert? */
620 /*
621 * Don't need to use adb_cmd_extra here because this section
622 * will be called ONLY when it is an ADB command (no RTC or
623 * PRAM)
624 */
625 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
626 * doing a listen! */
627 len = buffer[0]; /* length of additional data */
628 else
629 len = 0;/* no additional data */
630
631 adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl.
632 * data */
633 adbOutputBuffer[1] = 0x00; /* mark as an ADB command */
634 adbOutputBuffer[2] = (u_char)command; /* load command */
635
636 /* copy additional output data, if any */
637 memcpy(adbOutputBuffer + 3, buffer + 1, len);
638 } else
639 /* if data ready, just copy over */
640 memcpy(adbOutputBuffer, in, in[0] + 2);
641
642 adbSentChars = 0; /* nothing sent yet */
643 adbBuffer = buffer; /* save buffer to know where to save result */
644 adbCompRout = compRout; /* save completion routine pointer */
645 adbCompData = data; /* save completion routine data pointer */
646 adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */
647
648 if (adbWriteDelay != 1) { /* start command now? */
649 #ifdef ADB_DEBUG
650 if (adb_debug)
651 printf_intr("out start NOW");
652 #endif
653 delay(ADB_DELAY);
654 adbActionState = ADB_ACTION_OUT; /* set next state */
655 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
656 write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); /* load byte for output */
657 ADB_SET_STATE_ACKOFF_CUDA();
658 ADB_SET_STATE_TIP(); /* tell ADB that we want to send */
659 }
660 adbWriteDelay = 1; /* something in the write "queue" */
661
662 splx(s);
663
664 if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */
665 /* poll until byte done */
666 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
667 || (adbWaiting == 1))
668 if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
669 adb_intr_cuda(); /* process it */
670 adb_soft_intr();
671 }
672
673 return 0;
674 } /* send_adb_cuda */
675
676
677 void
678 adb_intr_II(void)
679 {
680 panic("adb_intr_II");
681 }
682
683
684 /*
685 * send_adb version for II series machines
686 */
687 int
688 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
689 {
690 panic("send_adb_II");
691 }
692
693
694 /*
695 * This routine is called from the II series interrupt routine
696 * to determine what the "next" device is that should be polled.
697 */
698 int
699 adb_guess_next_device(void)
700 {
701 int last, i, dummy;
702
703 if (adbStarting) {
704 /*
705 * Start polling EVERY device, since we can't be sure there is
706 * anything in the device table yet
707 */
708 if (adbLastDevice < 1 || adbLastDevice > 15)
709 adbLastDevice = 1;
710 if (++adbLastDevice > 15) /* point to next one */
711 adbLastDevice = 1;
712 } else {
713 /* find the next device using the device table */
714 if (adbLastDevice < 1 || adbLastDevice > 15) /* let's be parinoid */
715 adbLastDevice = 2;
716 last = 1; /* default index location */
717
718 for (i = 1; i < 16; i++) /* find index entry */
719 if (ADBDevTable[i].currentAddr == adbLastDevice) { /* look for device */
720 last = i; /* found it */
721 break;
722 }
723 dummy = last; /* index to start at */
724 for (;;) { /* find next device in index */
725 if (++dummy > 15) /* wrap around if needed */
726 dummy = 1;
727 if (dummy == last) { /* didn't find any other
728 * device! This can happen if
729 * there are no devices on the
730 * bus */
731 dummy = 1;
732 break;
733 }
734 /* found the next device */
735 if (ADBDevTable[dummy].devType != 0)
736 break;
737 }
738 adbLastDevice = ADBDevTable[dummy].currentAddr;
739 }
740 return adbLastDevice;
741 }
742
743
744 /*
745 * Called when when an adb interrupt happens.
746 * This routine simply transfers control over to the appropriate
747 * code for the machine we are running on.
748 */
749 void
750 adb_intr(void)
751 {
752 switch (adbHardware) {
753 case ADB_HW_II:
754 adb_intr_II();
755 break;
756
757 case ADB_HW_IISI:
758 adb_intr_IIsi();
759 break;
760
761 case ADB_HW_PB:
762 pm_intr();
763 break;
764
765 case ADB_HW_CUDA:
766 adb_intr_cuda();
767 break;
768
769 case ADB_HW_UNKNOWN:
770 break;
771 }
772 }
773
774
775 /*
776 * called when when an adb interrupt happens
777 *
778 * IIsi version of adb_intr
779 *
780 */
781 void
782 adb_intr_IIsi(void)
783 {
784 panic("adb_intr_IIsi");
785 }
786
787
788 /*****************************************************************************
789 * if the device is currently busy, and there is no data waiting to go out, then
790 * the data is "queued" in the outgoing buffer. If we are already waiting, then
791 * we return.
792 * in: if (in == 0) then the command string is built from command and buffer
793 * if (in != 0) then in is used as the command string
794 * buffer: additional data to be sent (used only if in == 0)
795 * this is also where return data is stored
796 * compRout: the completion routine that is called when then return value
797 * is received (if a return value is expected)
798 * data: a data pointer that can be used by the completion routine
799 * command: an ADB command to be sent (used only if in == 0)
800 *
801 */
802 int
803 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
804 command)
805 {
806 panic("send_adb_IIsi");
807 }
808
809
810 /*
811 * adb_pass_up is called by the interrupt-time routines.
812 * It takes the raw packet data that was received from the
813 * device and puts it into the queue that the upper half
814 * processes. It then signals for a soft ADB interrupt which
815 * will eventually call the upper half routine (adb_soft_intr).
816 *
817 * If in->unsol is 0, then this is either the notification
818 * that the packet was sent (on a LISTEN, for example), or the
819 * response from the device (on a TALK). The completion routine
820 * is called only if the user specified one.
821 *
822 * If in->unsol is 1, then this packet was unsolicited and
823 * so we look up the device in the ADB device table to determine
824 * what it's default service routine is.
825 *
826 * If in->ack_only is 1, then we really only need to call
827 * the completion routine, so don't do any other stuff.
828 *
829 * Note that in->data contains the packet header AND data,
830 * while adbInbound[]->data contains ONLY data.
831 *
832 * Note: Called only at interrupt time. Assumes this.
833 */
834 void
835 adb_pass_up(struct adbCommand *in)
836 {
837 int start = 0, len = 0, cmd = 0;
838 ADBDataBlock block;
839
840 /* temp for testing */
841 /*u_char *buffer = 0;*/
842 /*u_char *compdata = 0;*/
843 /*u_char *comprout = 0;*/
844
845 if (adbInCount >= ADB_QUEUE) {
846 #ifdef ADB_DEBUG
847 if (adb_debug)
848 printf_intr("adb: ring buffer overflow\n");
849 #endif
850 return;
851 }
852
853 if (in->ack_only) {
854 len = in->data[0];
855 cmd = in->cmd;
856 start = 0;
857 } else {
858 switch (adbHardware) {
859 case ADB_HW_II:
860 cmd = in->data[1];
861 if (in->data[0] < 2)
862 len = 0;
863 else
864 len = in->data[0]-1;
865 start = 1;
866 break;
867
868 case ADB_HW_IISI:
869 case ADB_HW_CUDA:
870 /* If it's unsolicited, accept only ADB data for now */
871 if (in->unsol)
872 if (0 != in->data[2])
873 return;
874 cmd = in->data[4];
875 if (in->data[0] < 5)
876 len = 0;
877 else
878 len = in->data[0]-4;
879 start = 4;
880 break;
881
882 case ADB_HW_PB:
883 cmd = in->data[1];
884 if (in->data[0] < 2)
885 len = 0;
886 else
887 len = in->data[0]-1;
888 start = 1;
889 break;
890
891 case ADB_HW_UNKNOWN:
892 return;
893 }
894
895 /* Make sure there is a valid device entry for this device */
896 if (in->unsol) {
897 /* ignore unsolicited data during adbreinit */
898 if (adbStarting)
899 return;
900 /* get device's comp. routine and data area */
901 if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
902 return;
903 }
904 }
905
906 /*
907 * If this is an unsolicited packet, we need to fill in
908 * some info so adb_soft_intr can process this packet
909 * properly. If it's not unsolicited, then use what
910 * the caller sent us.
911 */
912 if (in->unsol) {
913 adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
914 adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
915 adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
916 } else {
917 adbInbound[adbInTail].compRout = (void *)in->compRout;
918 adbInbound[adbInTail].compData = (void *)in->compData;
919 adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
920 }
921
922 #ifdef ADB_DEBUG
923 if (adb_debug && in->data[1] == 2)
924 printf_intr("adb: caught error\n");
925 #endif
926
927 /* copy the packet data over */
928 /*
929 * TO DO: If the *_intr routines fed their incoming data
930 * directly into an adbCommand struct, which is passed to
931 * this routine, then we could eliminate this copy.
932 */
933 memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
934 adbInbound[adbInTail].data[0] = len;
935 adbInbound[adbInTail].cmd = cmd;
936
937 adbInCount++;
938 if (++adbInTail >= ADB_QUEUE)
939 adbInTail = 0;
940
941 /*
942 * If the debugger is running, call upper half manually.
943 * Otherwise, trigger a soft interrupt to handle the rest later.
944 */
945 if (adb_polling)
946 adb_soft_intr();
947 else
948 setsoftadb();
949
950 return;
951 }
952
953
954 /*
955 * Called to process the packets after they have been
956 * placed in the incoming queue.
957 *
958 */
959 void
960 adb_soft_intr(void)
961 {
962 int s;
963 int cmd = 0;
964 u_char *buffer = 0;
965 u_char *comprout = 0;
966 u_char *compdata = 0;
967
968 #if 0
969 s = splhigh();
970 printf_intr("sr: %x\n", (s & 0x0700));
971 splx(s);
972 #endif
973
974 /*delay(2*ADB_DELAY);*/
975
976 while (adbInCount) {
977 #ifdef ADB_DEBUG
978 if (adb_debug & 0x80)
979 printf_intr("%x %x %x ",
980 adbInCount, adbInHead, adbInTail);
981 #endif
982 /* get the data we need from the queue */
983 buffer = adbInbound[adbInHead].saveBuf;
984 comprout = adbInbound[adbInHead].compRout;
985 compdata = adbInbound[adbInHead].compData;
986 cmd = adbInbound[adbInHead].cmd;
987
988 /* copy over data to data area if it's valid */
989 /*
990 * Note that for unsol packets we don't want to copy the
991 * data anywhere, so buffer was already set to 0.
992 * For ack_only buffer was set to 0, so don't copy.
993 */
994 if (buffer)
995 memcpy(buffer, adbInbound[adbInHead].data,
996 adbInbound[adbInHead].data[0] + 1);
997
998 #ifdef ADB_DEBUG
999 if (adb_debug & 0x80) {
1000 printf_intr("%p %p %p %x ",
1001 buffer, comprout, compdata, (short)cmd);
1002 printf_intr("buf: ");
1003 print_single(adbInbound[adbInHead].data);
1004 }
1005 #endif
1006
1007 /* call default completion routine if it's valid */
1008 if (comprout) {
1009 int (*f)() = (void *)comprout;
1010
1011 (*f)(buffer, compdata, cmd);
1012 #if 0
1013 #ifdef __NetBSD__
1014 asm(" movml #0xffff,sp@- | save all registers
1015 movl %0,a2 | compdata
1016 movl %1,a1 | comprout
1017 movl %2,a0 | buffer
1018 movl %3,d0 | cmd
1019 jbsr a1@ | go call the routine
1020 movml sp@+,#0xffff | restore all registers"
1021 :
1022 : "g"(compdata), "g"(comprout),
1023 "g"(buffer), "g"(cmd)
1024 : "d0", "a0", "a1", "a2");
1025 #else /* for macos based testing */
1026 asm
1027 {
1028 movem.l a0/a1/a2/d0, -(a7)
1029 move.l compdata, a2
1030 move.l comprout, a1
1031 move.l buffer, a0
1032 move.w cmd, d0
1033 jsr(a1)
1034 movem.l(a7)+, d0/a2/a1/a0
1035 }
1036 #endif
1037 #endif
1038 }
1039
1040 s = splhigh();
1041 adbInCount--;
1042 if (++adbInHead >= ADB_QUEUE)
1043 adbInHead = 0;
1044 splx(s);
1045
1046 }
1047 return;
1048 }
1049
1050
1051 /*
1052 * This is my version of the ADBOp routine. It mainly just calls the
1053 * hardware-specific routine.
1054 *
1055 * data : pointer to data area to be used by compRout
1056 * compRout : completion routine
1057 * buffer : for LISTEN: points to data to send - MAX 8 data bytes,
1058 * byte 0 = # of bytes
1059 * : for TALK: points to place to save return data
1060 * command : the adb command to send
1061 * result : 0 = success
1062 * : -1 = could not complete
1063 */
1064 int
1065 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1066 {
1067 int result;
1068
1069 switch (adbHardware) {
1070 case ADB_HW_II:
1071 result = send_adb_II((u_char *)0, (u_char *)buffer,
1072 (void *)compRout, (void *)data, (int)command);
1073 if (result == 0)
1074 return 0;
1075 else
1076 return -1;
1077 break;
1078
1079 case ADB_HW_IISI:
1080 result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1081 (void *)compRout, (void *)data, (int)command);
1082 /*
1083 * I wish I knew why this delay is needed. It usually needs to
1084 * be here when several commands are sent in close succession,
1085 * especially early in device probes when doing collision
1086 * detection. It must be some race condition. Sigh. - jpw
1087 */
1088 delay(100);
1089 if (result == 0)
1090 return 0;
1091 else
1092 return -1;
1093 break;
1094
1095 case ADB_HW_PB:
1096 result = pm_adb_op((u_char *)buffer, (void *)compRout,
1097 (void *)data, (int)command);
1098
1099 if (result == 0)
1100 return 0;
1101 else
1102 return -1;
1103 break;
1104
1105 case ADB_HW_CUDA:
1106 result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1107 (void *)compRout, (void *)data, (int)command);
1108 if (result == 0)
1109 return 0;
1110 else
1111 return -1;
1112 break;
1113
1114 case ADB_HW_UNKNOWN:
1115 default:
1116 return -1;
1117 }
1118 }
1119
1120
1121 /*
1122 * adb_hw_setup
1123 * This routine sets up the possible machine specific hardware
1124 * config (mainly VIA settings) for the various models.
1125 */
1126 void
1127 adb_hw_setup(void)
1128 {
1129 volatile int i;
1130 u_char send_string[ADB_MAX_MSG_LENGTH];
1131
1132 switch (adbHardware) {
1133 case ADB_HW_II:
1134 via_reg_or(VIA1, vDirB, 0x30); /* register B bits 4 and 5:
1135 * outputs */
1136 via_reg_and(VIA1, vDirB, 0xf7); /* register B bit 3: input */
1137 via_reg_and(VIA1, vACR, ~vSR_OUT); /* make sure SR is set
1138 * to IN (II, IIsi) */
1139 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1140 * hardware (II, IIsi) */
1141 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1142 * code only */
1143 write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1144 * are on (II, IIsi) */
1145 ADB_SET_STATE_IDLE_II(); /* set ADB bus state to idle */
1146
1147 ADB_VIA_CLR_INTR(); /* clear interrupt */
1148 break;
1149
1150 case ADB_HW_IISI:
1151 via_reg_or(VIA1, vDirB, 0x30); /* register B bits 4 and 5:
1152 * outputs */
1153 via_reg_and(VIA1, vDirB, 0xf7); /* register B bit 3: input */
1154 via_reg_and(VIA1, vACR, ~vSR_OUT); /* make sure SR is set
1155 * to IN (II, IIsi) */
1156 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1157 * hardware (II, IIsi) */
1158 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1159 * code only */
1160 write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1161 * are on (II, IIsi) */
1162 ADB_SET_STATE_IDLE_IISI(); /* set ADB bus state to idle */
1163
1164 /* get those pesky clock ticks we missed while booting */
1165 for (i = 0; i < 30; i++) {
1166 delay(ADB_DELAY);
1167 adb_hw_setup_IIsi(send_string);
1168 #ifdef ADB_DEBUG
1169 if (adb_debug) {
1170 printf_intr("adb: cleanup: ");
1171 print_single(send_string);
1172 }
1173 #endif
1174 delay(ADB_DELAY);
1175 if (ADB_INTR_IS_OFF)
1176 break;
1177 }
1178 break;
1179
1180 case ADB_HW_PB:
1181 /*
1182 * XXX - really PM_VIA_CLR_INTR - should we put it in
1183 * pm_direct.h?
1184 */
1185 write_via_reg(VIA1, vIFR, 0x90); /* clear interrupt */
1186 break;
1187
1188 case ADB_HW_CUDA:
1189 via_reg_or(VIA1, vDirB, 0x30); /* register B bits 4 and 5:
1190 * outputs */
1191 via_reg_and(VIA1, vDirB, 0xf7); /* register B bit 3: input */
1192 via_reg_and(VIA1, vACR, ~vSR_OUT); /* make sure SR is set
1193 * to IN */
1194 write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
1195 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1196 * hardware */
1197 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1198 * code only */
1199 write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1200 * are on */
1201 ADB_SET_STATE_IDLE_CUDA(); /* set ADB bus state to idle */
1202
1203 /* sort of a device reset */
1204 i = ADB_SR(); /* clear interrupt */
1205 ADB_VIA_INTR_DISABLE(); /* no interrupts while clearing */
1206 ADB_SET_STATE_IDLE_CUDA(); /* reset state to idle */
1207 delay(ADB_DELAY);
1208 ADB_SET_STATE_TIP(); /* signal start of frame */
1209 delay(ADB_DELAY);
1210 ADB_TOGGLE_STATE_ACK_CUDA();
1211 delay(ADB_DELAY);
1212 ADB_CLR_STATE_TIP();
1213 delay(ADB_DELAY);
1214 ADB_SET_STATE_IDLE_CUDA(); /* back to idle state */
1215 i = ADB_SR(); /* clear interrupt */
1216 ADB_VIA_INTR_ENABLE(); /* ints ok now */
1217 break;
1218
1219 case ADB_HW_UNKNOWN:
1220 default:
1221 write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
1222 * DO: turn PB ints off? */
1223 return;
1224 break;
1225 }
1226 }
1227
1228
1229 /*
1230 * adb_hw_setup_IIsi
1231 * This is sort of a "read" routine that forces the adb hardware through a read cycle
1232 * if there is something waiting. This helps "clean up" any commands that may have gotten
1233 * stuck or stopped during the boot process.
1234 *
1235 */
1236 void
1237 adb_hw_setup_IIsi(u_char * buffer)
1238 {
1239 panic("adb_hw_setup_IIsi");
1240 }
1241
1242
1243 /*
1244 * adb_reinit sets up the adb stuff
1245 *
1246 */
1247 void
1248 adb_reinit(void)
1249 {
1250 u_char send_string[ADB_MAX_MSG_LENGTH];
1251 ADBDataBlock data; /* temp. holder for getting device info */
1252 volatile int i, x;
1253 int s;
1254 int command;
1255 int result;
1256 int saveptr; /* point to next free relocation address */
1257 int device;
1258 int nonewtimes; /* times thru loop w/o any new devices */
1259
1260 /* Make sure we are not interrupted while building the table. */
1261 if (adbHardware != ADB_HW_PB) /* ints must be on for PB? */
1262 s = splhigh();
1263
1264 ADBNumDevices = 0; /* no devices yet */
1265
1266 /* Let intr routines know we are running reinit */
1267 adbStarting = 1;
1268
1269 /*
1270 * Initialize the ADB table. For now, we'll always use the same table
1271 * that is defined at the beginning of this file - no mallocs.
1272 */
1273 for (i = 0; i < 16; i++)
1274 ADBDevTable[i].devType = 0;
1275
1276 adb_setup_hw_type(); /* setup hardware type */
1277
1278 adb_hw_setup(); /* init the VIA bits and hard reset ADB */
1279
1280 delay(1000);
1281
1282 /* send an ADB reset first */
1283 adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
1284 delay(200000);
1285
1286 /*
1287 * Probe for ADB devices. Probe devices 1-15 quickly to determine
1288 * which device addresses are in use and which are free. For each
1289 * address that is in use, move the device at that address to a higher
1290 * free address. Continue doing this at that address until no device
1291 * responds at that address. Then move the last device that was moved
1292 * back to the original address. Do this for the remaining addresses
1293 * that we determined were in use.
1294 *
1295 * When finished, do this entire process over again with the updated
1296 * list of in use addresses. Do this until no new devices have been
1297 * found in 20 passes though the in use address list. (This probably
1298 * seems long and complicated, but it's the best way to detect multiple
1299 * devices at the same address - sometimes it takes a couple of tries
1300 * before the collision is detected.)
1301 */
1302
1303 /* initial scan through the devices */
1304 for (i = 1; i < 16; i++) {
1305 send_string[0] = 0;
1306 command = ADBTALK(i, 3);
1307 result = adb_op_sync((Ptr)send_string, (Ptr)0,
1308 (Ptr)0, (short)command);
1309
1310 if (send_string[0] != 0) {
1311 /* check for valid device handler */
1312 switch (send_string[2]) {
1313 case 0:
1314 case 0xfd:
1315 case 0xfe:
1316 case 0xff:
1317 continue; /* invalid, skip */
1318 }
1319
1320 /* found a device */
1321 ++ADBNumDevices;
1322 KASSERT(ADBNumDevices < 16);
1323 ADBDevTable[ADBNumDevices].devType =
1324 (int)send_string[2];
1325 ADBDevTable[ADBNumDevices].origAddr = i;
1326 ADBDevTable[ADBNumDevices].currentAddr = i;
1327 ADBDevTable[ADBNumDevices].DataAreaAddr =
1328 (long)0;
1329 ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
1330 pm_check_adb_devices(i); /* tell pm driver device
1331 * is here */
1332 }
1333 }
1334
1335 /* find highest unused address */
1336 for (saveptr = 15; saveptr > 0; saveptr--)
1337 if (-1 == get_adb_info(&data, saveptr))
1338 break;
1339
1340 #ifdef ADB_DEBUG
1341 if (adb_debug & 0x80) {
1342 printf_intr("first free is: 0x%02x\n", saveptr);
1343 printf_intr("devices: %i\n", ADBNumDevices);
1344 }
1345 #endif
1346
1347 nonewtimes = 0; /* no loops w/o new devices */
1348 while (saveptr > 0 && nonewtimes++ < 11) {
1349 for (i = 1; i <= ADBNumDevices; i++) {
1350 device = ADBDevTable[i].currentAddr;
1351 #ifdef ADB_DEBUG
1352 if (adb_debug & 0x80)
1353 printf_intr("moving device 0x%02x to 0x%02x "
1354 "(index 0x%02x) ", device, saveptr, i);
1355 #endif
1356
1357 /* send TALK R3 to address */
1358 command = ADBTALK(device, 3);
1359 adb_op_sync((Ptr)send_string, (Ptr)0,
1360 (Ptr)0, (short)command);
1361
1362 /* move device to higher address */
1363 command = ADBLISTEN(device, 3);
1364 send_string[0] = 2;
1365 send_string[1] = (u_char)(saveptr | 0x60);
1366 send_string[2] = 0xfe;
1367 adb_op_sync((Ptr)send_string, (Ptr)0,
1368 (Ptr)0, (short)command);
1369 delay(500);
1370
1371 /* send TALK R3 - anything at new address? */
1372 command = ADBTALK(saveptr, 3);
1373 adb_op_sync((Ptr)send_string, (Ptr)0,
1374 (Ptr)0, (short)command);
1375 delay(500);
1376
1377 if (send_string[0] == 0) {
1378 #ifdef ADB_DEBUG
1379 if (adb_debug & 0x80)
1380 printf_intr("failed, continuing\n");
1381 #endif
1382 continue;
1383 }
1384
1385 /* send TALK R3 - anything at old address? */
1386 command = ADBTALK(device, 3);
1387 result = adb_op_sync((Ptr)send_string, (Ptr)0,
1388 (Ptr)0, (short)command);
1389 if (send_string[0] != 0) {
1390 /* check for valid device handler */
1391 switch (send_string[2]) {
1392 case 0:
1393 case 0xfd:
1394 case 0xfe:
1395 case 0xff:
1396 continue; /* invalid, skip */
1397 }
1398
1399 /* new device found */
1400 /* update data for previously moved device */
1401 ADBDevTable[i].currentAddr = saveptr;
1402 #ifdef ADB_DEBUG
1403 if (adb_debug & 0x80)
1404 printf_intr("old device at index %i\n",i);
1405 #endif
1406 /* add new device in table */
1407 #ifdef ADB_DEBUG
1408 if (adb_debug & 0x80)
1409 printf_intr("new device found\n");
1410 #endif
1411 if (saveptr > ADBNumDevices) {
1412 ++ADBNumDevices;
1413 KASSERT(ADBNumDevices < 16);
1414 }
1415 ADBDevTable[ADBNumDevices].devType =
1416 (int)send_string[2];
1417 ADBDevTable[ADBNumDevices].origAddr = device;
1418 ADBDevTable[ADBNumDevices].currentAddr = device;
1419 /* These will be set correctly in adbsys.c */
1420 /* Until then, unsol. data will be ignored. */
1421 ADBDevTable[ADBNumDevices].DataAreaAddr =
1422 (long)0;
1423 ADBDevTable[ADBNumDevices].ServiceRtPtr =
1424 (void *)0;
1425 /* find next unused address */
1426 for (x = saveptr; x > 0; x--) {
1427 if (-1 == get_adb_info(&data, x)) {
1428 saveptr = x;
1429 break;
1430 }
1431 }
1432 if (x == 0)
1433 saveptr = 0;
1434 #ifdef ADB_DEBUG
1435 if (adb_debug & 0x80)
1436 printf_intr("new free is 0x%02x\n",
1437 saveptr);
1438 #endif
1439 nonewtimes = 0;
1440 /* tell pm driver device is here */
1441 pm_check_adb_devices(device);
1442 } else {
1443 #ifdef ADB_DEBUG
1444 if (adb_debug & 0x80)
1445 printf_intr("moving back...\n");
1446 #endif
1447 /* move old device back */
1448 command = ADBLISTEN(saveptr, 3);
1449 send_string[0] = 2;
1450 send_string[1] = (u_char)(device | 0x60);
1451 send_string[2] = 0xfe;
1452 adb_op_sync((Ptr)send_string, (Ptr)0,
1453 (Ptr)0, (short)command);
1454 delay(1000);
1455 }
1456 }
1457 }
1458
1459 #ifdef ADB_DEBUG
1460 if (adb_debug) {
1461 for (i = 1; i <= ADBNumDevices; i++) {
1462 x = get_ind_adb_info(&data, i);
1463 if (x != -1)
1464 printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
1465 i, x, data.devType);
1466 }
1467 }
1468 #endif
1469
1470 #ifndef MRG_ADB
1471 /* enable the programmer's switch, if we have one */
1472 adb_prog_switch_enable();
1473 #endif
1474
1475 #ifdef ADB_DEBUG
1476 if (adb_debug) {
1477 if (0 == ADBNumDevices) /* tell user if no devices found */
1478 printf_intr("adb: no devices found\n");
1479 }
1480 #endif
1481
1482 adbStarting = 0; /* not starting anymore */
1483 #ifdef ADB_DEBUG
1484 if (adb_debug)
1485 printf_intr("adb: ADBReInit complete\n");
1486 #endif
1487
1488 if (adbHardware == ADB_HW_CUDA)
1489 callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
1490 (void *)adb_cuda_tickle, NULL);
1491
1492 if (adbHardware != ADB_HW_PB) /* ints must be on for PB? */
1493 splx(s);
1494 }
1495
1496
1497 #if 0
1498 /*
1499 * adb_comp_exec
1500 * This is a general routine that calls the completion routine if there is one.
1501 * NOTE: This routine is now only used by pm_direct.c
1502 * All the code in this file (adb_direct.c) uses
1503 * the adb_pass_up routine now.
1504 */
1505 void
1506 adb_comp_exec(void)
1507 {
1508 if ((long)0 != adbCompRout) /* don't call if empty return location */
1509 #ifdef __NetBSD__
1510 asm(" movml #0xffff,sp@- | save all registers
1511 movl %0,a2 | adbCompData
1512 movl %1,a1 | adbCompRout
1513 movl %2,a0 | adbBuffer
1514 movl %3,d0 | adbWaitingCmd
1515 jbsr a1@ | go call the routine
1516 movml sp@+,#0xffff | restore all registers"
1517 :
1518 : "g"(adbCompData), "g"(adbCompRout),
1519 "g"(adbBuffer), "g"(adbWaitingCmd)
1520 : "d0", "a0", "a1", "a2");
1521 #else /* for Mac OS-based testing */
1522 asm {
1523 movem.l a0/a1/a2/d0, -(a7)
1524 move.l adbCompData, a2
1525 move.l adbCompRout, a1
1526 move.l adbBuffer, a0
1527 move.w adbWaitingCmd, d0
1528 jsr(a1)
1529 movem.l(a7) +, d0/a2/a1/a0
1530 }
1531 #endif
1532 }
1533 #endif
1534
1535
1536 /*
1537 * adb_cmd_result
1538 *
1539 * This routine lets the caller know whether the specified adb command string
1540 * should expect a returned result, such as a TALK command.
1541 *
1542 * returns: 0 if a result should be expected
1543 * 1 if a result should NOT be expected
1544 */
1545 int
1546 adb_cmd_result(u_char *in)
1547 {
1548 switch (adbHardware) {
1549 case ADB_HW_II:
1550 /* was it an ADB talk command? */
1551 if ((in[1] & 0x0c) == 0x0c)
1552 return 0;
1553 return 1;
1554
1555 case ADB_HW_IISI:
1556 case ADB_HW_CUDA:
1557 /* was it an ADB talk command? */
1558 if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
1559 return 0;
1560 /* was it an RTC/PRAM read date/time? */
1561 if ((in[1] == 0x01) && (in[2] == 0x03))
1562 return 0;
1563 return 1;
1564
1565 case ADB_HW_PB:
1566 return 1;
1567
1568 case ADB_HW_UNKNOWN:
1569 default:
1570 return 1;
1571 }
1572 }
1573
1574
1575 /*
1576 * adb_cmd_extra
1577 *
1578 * This routine lets the caller know whether the specified adb command string
1579 * may have extra data appended to the end of it, such as a LISTEN command.
1580 *
1581 * returns: 0 if extra data is allowed
1582 * 1 if extra data is NOT allowed
1583 */
1584 int
1585 adb_cmd_extra(u_char *in)
1586 {
1587 switch (adbHardware) {
1588 case ADB_HW_II:
1589 if ((in[1] & 0x0c) == 0x08) /* was it a listen command? */
1590 return 0;
1591 return 1;
1592
1593 case ADB_HW_IISI:
1594 case ADB_HW_CUDA:
1595 /*
1596 * TO DO: support needs to be added to recognize RTC and PRAM
1597 * commands
1598 */
1599 if ((in[2] & 0x0c) == 0x08) /* was it a listen command? */
1600 return 0;
1601 /* add others later */
1602 return 1;
1603
1604 case ADB_HW_PB:
1605 return 1;
1606
1607 case ADB_HW_UNKNOWN:
1608 default:
1609 return 1;
1610 }
1611 }
1612
1613 /*
1614 * adb_op_sync
1615 *
1616 * This routine does exactly what the adb_op routine does, except that after
1617 * the adb_op is called, it waits until the return value is present before
1618 * returning.
1619 *
1620 * NOTE: The user specified compRout is ignored, since this routine specifies
1621 * it's own to adb_op, which is why you really called this in the first place
1622 * anyway.
1623 */
1624 int
1625 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
1626 {
1627 int tmout;
1628 int result;
1629 volatile int flag = 0;
1630
1631 result = adb_op(buffer, (void *)adb_op_comprout,
1632 (void *)&flag, command); /* send command */
1633 if (result == 0) { /* send ok? */
1634 /*
1635 * Total time to wait is calculated as follows:
1636 * - Tlt (stop to start time): 260 usec
1637 * - start bit: 100 usec
1638 * - up to 8 data bytes: 64 * 100 usec = 6400 usec
1639 * - stop bit (with SRQ): 140 usec
1640 * Total: 6900 usec
1641 *
1642 * This is the total time allowed by the specification. Any
1643 * device that doesn't conform to this will fail to operate
1644 * properly on some Apple systems. In spite of this we
1645 * double the time to wait; some Cuda-based apparently
1646 * queues some commands and allows the main CPU to continue
1647 * processing (radical concept, eh?). To be safe, allow
1648 * time for two complete ADB transactions to occur.
1649 */
1650 for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
1651 delay(10);
1652 if (!flag && tmout > 0)
1653 delay(tmout);
1654
1655 if (!flag)
1656 result = -2;
1657 }
1658
1659 return result;
1660 }
1661
1662 /*
1663 * adb_op_comprout
1664 *
1665 * This function is used by the adb_op_sync routine so it knows when the
1666 * function is done.
1667 */
1668 void
1669 adb_op_comprout(buffer, compdata, cmd)
1670 caddr_t buffer, compdata;
1671 int cmd;
1672 {
1673 short *p = (short *)compdata;
1674
1675 *p = 1;
1676 }
1677
1678 void
1679 adb_setup_hw_type(void)
1680 {
1681 switch (adbHardware) {
1682 case ADB_HW_CUDA:
1683 adbSoftPower = 1;
1684 return;
1685
1686 case ADB_HW_PB:
1687 adbSoftPower = 1;
1688 pm_setup_adb();
1689 return;
1690
1691 default:
1692 panic("unknown adb hardware");
1693 }
1694 #if 0
1695 response = 0; /*mac68k_machine.machineid;*/
1696
1697 /*
1698 * Determine what type of ADB hardware we are running on.
1699 */
1700 switch (response) {
1701 case MACH_MACC610: /* Centris 610 */
1702 case MACH_MACC650: /* Centris 650 */
1703 case MACH_MACII: /* II */
1704 case MACH_MACIICI: /* IIci */
1705 case MACH_MACIICX: /* IIcx */
1706 case MACH_MACIIX: /* IIx */
1707 case MACH_MACQ610: /* Quadra 610 */
1708 case MACH_MACQ650: /* Quadra 650 */
1709 case MACH_MACQ700: /* Quadra 700 */
1710 case MACH_MACQ800: /* Quadra 800 */
1711 case MACH_MACSE30: /* SE/30 */
1712 adbHardware = ADB_HW_II;
1713 #ifdef ADB_DEBUG
1714 if (adb_debug)
1715 printf_intr("adb: using II series hardware support\n");
1716 #endif
1717 break;
1718
1719 case MACH_MACCLASSICII: /* Classic II */
1720 case MACH_MACLCII: /* LC II, Performa 400/405/430 */
1721 case MACH_MACLCIII: /* LC III, Performa 450 */
1722 case MACH_MACIISI: /* IIsi */
1723 case MACH_MACIIVI: /* IIvi */
1724 case MACH_MACIIVX: /* IIvx */
1725 case MACH_MACP460: /* Performa 460/465/467 */
1726 case MACH_MACP600: /* Performa 600 */
1727 adbHardware = ADB_HW_IISI;
1728 #ifdef ADB_DEBUG
1729 if (adb_debug)
1730 printf_intr("adb: using IIsi series hardware support\n");
1731 #endif
1732 break;
1733
1734 case MACH_MACPB140: /* PowerBook 140 */
1735 case MACH_MACPB145: /* PowerBook 145 */
1736 case MACH_MACPB150: /* PowerBook 150 */
1737 case MACH_MACPB160: /* PowerBook 160 */
1738 case MACH_MACPB165: /* PowerBook 165 */
1739 case MACH_MACPB165C: /* PowerBook 165c */
1740 case MACH_MACPB170: /* PowerBook 170 */
1741 case MACH_MACPB180: /* PowerBook 180 */
1742 case MACH_MACPB180C: /* PowerBook 180c */
1743 adbHardware = ADB_HW_PB;
1744 pm_setup_adb();
1745 #ifdef ADB_DEBUG
1746 if (adb_debug)
1747 printf_intr("adb: using PowerBook 100-series hardware support\n");
1748 #endif
1749 break;
1750
1751 case MACH_MACPB210: /* PowerBook Duo 210 */
1752 case MACH_MACPB230: /* PowerBook Duo 230 */
1753 case MACH_MACPB250: /* PowerBook Duo 250 */
1754 case MACH_MACPB270: /* PowerBook Duo 270 */
1755 case MACH_MACPB280: /* PowerBook Duo 280 */
1756 case MACH_MACPB280C: /* PowerBook Duo 280c */
1757 case MACH_MACPB500: /* PowerBook 500 series */
1758 adbHardware = ADB_HW_PB;
1759 pm_setup_adb();
1760 #ifdef ADB_DEBUG
1761 if (adb_debug)
1762 printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
1763 #endif
1764 break;
1765
1766 case MACH_MACC660AV: /* Centris 660AV */
1767 case MACH_MACCCLASSIC: /* Color Classic */
1768 case MACH_MACCCLASSICII: /* Color Classic II */
1769 case MACH_MACLC475: /* LC 475, Performa 475/476 */
1770 case MACH_MACLC475_33: /* Clock-chipped 47x */
1771 case MACH_MACLC520: /* LC 520 */
1772 case MACH_MACLC575: /* LC 575, Performa 575/577/578 */
1773 case MACH_MACP550: /* LC 550, Performa 550 */
1774 case MACH_MACP580: /* Performa 580/588 */
1775 case MACH_MACQ605: /* Quadra 605 */
1776 case MACH_MACQ605_33: /* Clock-chipped Quadra 605 */
1777 case MACH_MACQ630: /* LC 630, Performa 630, Quadra 630 */
1778 case MACH_MACQ840AV: /* Quadra 840AV */
1779 adbHardware = ADB_HW_CUDA;
1780 #ifdef ADB_DEBUG
1781 if (adb_debug)
1782 printf_intr("adb: using Cuda series hardware support\n");
1783 #endif
1784 break;
1785 default:
1786 adbHardware = ADB_HW_UNKNOWN;
1787 #ifdef ADB_DEBUG
1788 if (adb_debug) {
1789 printf_intr("adb: hardware type unknown for this machine\n");
1790 printf_intr("adb: ADB support is disabled\n");
1791 }
1792 #endif
1793 break;
1794 }
1795
1796 /*
1797 * Determine whether this machine has ADB based soft power.
1798 */
1799 switch (response) {
1800 case MACH_MACCCLASSIC: /* Color Classic */
1801 case MACH_MACCCLASSICII: /* Color Classic II */
1802 case MACH_MACIISI: /* IIsi */
1803 case MACH_MACIIVI: /* IIvi */
1804 case MACH_MACIIVX: /* IIvx */
1805 case MACH_MACLC520: /* LC 520 */
1806 case MACH_MACLC575: /* LC 575, Performa 575/577/578 */
1807 case MACH_MACP550: /* LC 550, Performa 550 */
1808 case MACH_MACP600: /* Performa 600 */
1809 case MACH_MACQ630: /* LC 630, Performa 630, Quadra 630 */
1810 case MACH_MACQ840AV: /* Quadra 840AV */
1811 adbSoftPower = 1;
1812 break;
1813 }
1814 #endif
1815 }
1816
1817 int
1818 count_adbs(void)
1819 {
1820 int i;
1821 int found;
1822
1823 found = 0;
1824
1825 for (i = 1; i < 16; i++)
1826 if (0 != ADBDevTable[i].devType)
1827 found++;
1828
1829 return found;
1830 }
1831
1832 int
1833 get_ind_adb_info(ADBDataBlock * info, int index)
1834 {
1835 if ((index < 1) || (index > 15)) /* check range 1-15 */
1836 return (-1);
1837
1838 #ifdef ADB_DEBUG
1839 if (adb_debug & 0x80)
1840 printf_intr("index 0x%x devType is: 0x%x\n", index,
1841 ADBDevTable[index].devType);
1842 #endif
1843 if (0 == ADBDevTable[index].devType) /* make sure it's a valid entry */
1844 return (-1);
1845
1846 info->devType = ADBDevTable[index].devType;
1847 info->origADBAddr = ADBDevTable[index].origAddr;
1848 info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
1849 info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
1850
1851 return (ADBDevTable[index].currentAddr);
1852 }
1853
1854 int
1855 get_adb_info(ADBDataBlock * info, int adbAddr)
1856 {
1857 int i;
1858
1859 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
1860 return (-1);
1861
1862 for (i = 1; i < 15; i++)
1863 if (ADBDevTable[i].currentAddr == adbAddr) {
1864 info->devType = ADBDevTable[i].devType;
1865 info->origADBAddr = ADBDevTable[i].origAddr;
1866 info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
1867 info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
1868 return 0; /* found */
1869 }
1870
1871 return (-1); /* not found */
1872 }
1873
1874 int
1875 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
1876 {
1877 int i;
1878
1879 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
1880 return (-1);
1881
1882 for (i = 1; i < 15; i++)
1883 if (ADBDevTable[i].currentAddr == adbAddr) {
1884 ADBDevTable[i].ServiceRtPtr =
1885 (void *)(info->siServiceRtPtr);
1886 ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
1887 return 0; /* found */
1888 }
1889
1890 return (-1); /* not found */
1891
1892 }
1893
1894 #ifndef MRG_ADB
1895
1896 /* caller should really use machine-independant version: getPramTime */
1897 /* this version does pseudo-adb access only */
1898 int
1899 adb_read_date_time(unsigned long *time)
1900 {
1901 u_char output[ADB_MAX_MSG_LENGTH];
1902 int result;
1903 volatile int flag = 0;
1904
1905 switch (adbHardware) {
1906 case ADB_HW_II:
1907 return -1;
1908
1909 case ADB_HW_IISI:
1910 output[0] = 0x02; /* 2 byte message */
1911 output[1] = 0x01; /* to pram/rtc device */
1912 output[2] = 0x03; /* read date/time */
1913 result = send_adb_IIsi((u_char *)output, (u_char *)output,
1914 (void *)adb_op_comprout, (int *)&flag, (int)0);
1915 if (result != 0) /* exit if not sent */
1916 return -1;
1917
1918 while (0 == flag) /* wait for result */
1919 ;
1920
1921 *time = (long)(*(long *)(output + 1));
1922 return 0;
1923
1924 case ADB_HW_PB:
1925 pm_read_date_time(time);
1926 return 0;
1927
1928 case ADB_HW_CUDA:
1929 output[0] = 0x02; /* 2 byte message */
1930 output[1] = 0x01; /* to pram/rtc device */
1931 output[2] = 0x03; /* read date/time */
1932 result = send_adb_cuda((u_char *)output, (u_char *)output,
1933 (void *)adb_op_comprout, (void *)&flag, (int)0);
1934 if (result != 0) /* exit if not sent */
1935 return -1;
1936
1937 while (0 == flag) /* wait for result */
1938 ;
1939
1940 memcpy(time, output + 1, 4);
1941 return 0;
1942
1943 case ADB_HW_UNKNOWN:
1944 default:
1945 return -1;
1946 }
1947 }
1948
1949 /* caller should really use machine-independant version: setPramTime */
1950 /* this version does pseudo-adb access only */
1951 int
1952 adb_set_date_time(unsigned long time)
1953 {
1954 u_char output[ADB_MAX_MSG_LENGTH];
1955 int result;
1956 volatile int flag = 0;
1957
1958 switch (adbHardware) {
1959
1960 case ADB_HW_CUDA:
1961 output[0] = 0x06; /* 6 byte message */
1962 output[1] = 0x01; /* to pram/rtc device */
1963 output[2] = 0x09; /* set date/time */
1964 output[3] = (u_char)(time >> 24);
1965 output[4] = (u_char)(time >> 16);
1966 output[5] = (u_char)(time >> 8);
1967 output[6] = (u_char)(time);
1968 result = send_adb_cuda((u_char *)output, (u_char *)0,
1969 (void *)adb_op_comprout, (void *)&flag, (int)0);
1970 if (result != 0) /* exit if not sent */
1971 return -1;
1972
1973 while (0 == flag) /* wait for send to finish */
1974 ;
1975
1976 return 0;
1977
1978 case ADB_HW_PB:
1979 pm_set_date_time(time);
1980 return 0;
1981
1982 case ADB_HW_II:
1983 case ADB_HW_IISI:
1984 case ADB_HW_UNKNOWN:
1985 default:
1986 return -1;
1987 }
1988 }
1989
1990
1991 int
1992 adb_poweroff(void)
1993 {
1994 u_char output[ADB_MAX_MSG_LENGTH];
1995 int result;
1996
1997 if (!adbSoftPower)
1998 return -1;
1999
2000 adb_polling = 1;
2001
2002 switch (adbHardware) {
2003 case ADB_HW_IISI:
2004 output[0] = 0x02; /* 2 byte message */
2005 output[1] = 0x01; /* to pram/rtc/soft-power device */
2006 output[2] = 0x0a; /* set date/time */
2007 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2008 (void *)0, (void *)0, (int)0);
2009 if (result != 0) /* exit if not sent */
2010 return -1;
2011
2012 for (;;); /* wait for power off */
2013
2014 return 0;
2015
2016 case ADB_HW_PB:
2017 pm_adb_poweroff();
2018
2019 for (;;); /* wait for power off */
2020
2021 return 0;
2022
2023 case ADB_HW_CUDA:
2024 output[0] = 0x02; /* 2 byte message */
2025 output[1] = 0x01; /* to pram/rtc/soft-power device */
2026 output[2] = 0x0a; /* set date/time */
2027 result = send_adb_cuda((u_char *)output, (u_char *)0,
2028 (void *)0, (void *)0, (int)0);
2029 if (result != 0) /* exit if not sent */
2030 return -1;
2031
2032 for (;;); /* wait for power off */
2033
2034 return 0;
2035
2036 case ADB_HW_II: /* II models don't do ADB soft power */
2037 case ADB_HW_UNKNOWN:
2038 default:
2039 return -1;
2040 }
2041 }
2042
2043 int
2044 adb_prog_switch_enable(void)
2045 {
2046 u_char output[ADB_MAX_MSG_LENGTH];
2047 int result;
2048 volatile int flag = 0;
2049
2050 switch (adbHardware) {
2051 case ADB_HW_IISI:
2052 output[0] = 0x03; /* 3 byte message */
2053 output[1] = 0x01; /* to pram/rtc/soft-power device */
2054 output[2] = 0x1c; /* prog. switch control */
2055 output[3] = 0x01; /* enable */
2056 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2057 (void *)adb_op_comprout, (void *)&flag, (int)0);
2058 if (result != 0) /* exit if not sent */
2059 return -1;
2060
2061 while (0 == flag) /* wait for send to finish */
2062 ;
2063
2064 return 0;
2065
2066 case ADB_HW_PB:
2067 return -1;
2068
2069 case ADB_HW_II: /* II models don't do prog. switch */
2070 case ADB_HW_CUDA: /* cuda doesn't do prog. switch TO DO: verify this */
2071 case ADB_HW_UNKNOWN:
2072 default:
2073 return -1;
2074 }
2075 }
2076
2077 int
2078 adb_prog_switch_disable(void)
2079 {
2080 u_char output[ADB_MAX_MSG_LENGTH];
2081 int result;
2082 volatile int flag = 0;
2083
2084 switch (adbHardware) {
2085 case ADB_HW_IISI:
2086 output[0] = 0x03; /* 3 byte message */
2087 output[1] = 0x01; /* to pram/rtc/soft-power device */
2088 output[2] = 0x1c; /* prog. switch control */
2089 output[3] = 0x01; /* disable */
2090 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2091 (void *)adb_op_comprout, (void *)&flag, (int)0);
2092 if (result != 0) /* exit if not sent */
2093 return -1;
2094
2095 while (0 == flag) /* wait for send to finish */
2096 ;
2097
2098 return 0;
2099
2100 case ADB_HW_PB:
2101 return -1;
2102
2103 case ADB_HW_II: /* II models don't do prog. switch */
2104 case ADB_HW_CUDA: /* cuda doesn't do prog. switch */
2105 case ADB_HW_UNKNOWN:
2106 default:
2107 return -1;
2108 }
2109 }
2110
2111 int
2112 CountADBs(void)
2113 {
2114 return (count_adbs());
2115 }
2116
2117 void
2118 ADBReInit(void)
2119 {
2120 adb_reinit();
2121 }
2122
2123 int
2124 GetIndADB(ADBDataBlock * info, int index)
2125 {
2126 return (get_ind_adb_info(info, index));
2127 }
2128
2129 int
2130 GetADBInfo(ADBDataBlock * info, int adbAddr)
2131 {
2132 return (get_adb_info(info, adbAddr));
2133 }
2134
2135 int
2136 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2137 {
2138 return (set_adb_info(info, adbAddr));
2139 }
2140
2141 int
2142 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2143 {
2144 return (adb_op(buffer, compRout, data, commandNum));
2145 }
2146
2147 #endif
2148
2149 int
2150 setsoftadb()
2151 {
2152 callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
2153 return 0;
2154 }
2155
2156 void
2157 adb_cuda_autopoll()
2158 {
2159 volatile int flag = 0;
2160 int result;
2161 u_char output[16];
2162
2163 output[0] = 0x03; /* 3-byte message */
2164 output[1] = 0x01; /* to pram/rtc device */
2165 output[2] = 0x01; /* cuda autopoll */
2166 output[3] = 0x01;
2167 result = send_adb_cuda(output, output, adb_op_comprout, (void *)&flag,
2168 0);
2169 if (result != 0) /* exit if not sent */
2170 return;
2171
2172 while (flag == 0); /* wait for result */
2173 }
2174
2175 void
2176 adb_restart()
2177 {
2178 int result;
2179 u_char output[16];
2180
2181 adb_polling = 1;
2182
2183 switch (adbHardware) {
2184 case ADB_HW_CUDA:
2185 output[0] = 0x02; /* 2 byte message */
2186 output[1] = 0x01; /* to pram/rtc/soft-power device */
2187 output[2] = 0x11; /* restart */
2188 result = send_adb_cuda(output, NULL, NULL, NULL, 0);
2189 if (result != 0) /* exit if not sent */
2190 return;
2191 while (1); /* not return */
2192
2193 case ADB_HW_PB:
2194 pm_adb_restart();
2195 while (1); /* not return */
2196 }
2197 }
2198