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