xref: /netbsd-src/sys/arch/macppc/dev/adb_direct.c (revision 0dd5877adce57db949b16ae963e5a6831cccdfb6)
1 /*	$NetBSD: adb_direct.c,v 1.20 2002/01/02 20:30:45 dbj 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 
1010 		/* call default completion routine if it's valid */
1011 		if (comprout) {
1012 			void (*f)(caddr_t, caddr_t, int) =
1013 			    (void (*)(caddr_t, caddr_t, int))comprout;
1014 
1015 			(*f)(buffer, compdata, cmd);
1016 		}
1017 
1018 		s = splhigh();
1019 		adbInCount--;
1020 		if (++adbInHead >= ADB_QUEUE)
1021 			adbInHead = 0;
1022 		splx(s);
1023 
1024 	}
1025 	return;
1026 }
1027 
1028 
1029 /*
1030  * This is my version of the ADBOp routine. It mainly just calls the
1031  * hardware-specific routine.
1032  *
1033  *   data 	: pointer to data area to be used by compRout
1034  *   compRout	: completion routine
1035  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
1036  *		  byte 0 = # of bytes
1037  *		: for TALK: points to place to save return data
1038  *   command	: the adb command to send
1039  *   result	: 0 = success
1040  *		: -1 = could not complete
1041  */
1042 int
1043 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1044 {
1045 	int result;
1046 
1047 	switch (adbHardware) {
1048 	case ADB_HW_II:
1049 		result = send_adb_II((u_char *)0, (u_char *)buffer,
1050 		    (void *)compRout, (void *)data, (int)command);
1051 		if (result == 0)
1052 			return 0;
1053 		else
1054 			return -1;
1055 		break;
1056 
1057 	case ADB_HW_IISI:
1058 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1059 		    (void *)compRout, (void *)data, (int)command);
1060 		/*
1061 		 * I wish I knew why this delay is needed. It usually needs to
1062 		 * be here when several commands are sent in close succession,
1063 		 * especially early in device probes when doing collision
1064 		 * detection. It must be some race condition. Sigh. - jpw
1065 		 */
1066 		delay(100);
1067 		if (result == 0)
1068 			return 0;
1069 		else
1070 			return -1;
1071 		break;
1072 
1073 	case ADB_HW_PB:
1074 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
1075 		    (void *)data, (int)command);
1076 
1077 		if (result == 0)
1078 			return 0;
1079 		else
1080 			return -1;
1081 		break;
1082 
1083 	case ADB_HW_CUDA:
1084 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1085 		    (void *)compRout, (void *)data, (int)command);
1086 		if (result == 0)
1087 			return 0;
1088 		else
1089 			return -1;
1090 		break;
1091 
1092 	case ADB_HW_UNKNOWN:
1093 	default:
1094 		return -1;
1095 	}
1096 }
1097 
1098 
1099 /*
1100  * adb_hw_setup
1101  * This routine sets up the possible machine specific hardware
1102  * config (mainly VIA settings) for the various models.
1103  */
1104 void
1105 adb_hw_setup(void)
1106 {
1107 	volatile int i;
1108 	u_char send_string[ADB_MAX_MSG_LENGTH];
1109 
1110 	switch (adbHardware) {
1111 	case ADB_HW_II:
1112 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
1113 						 * outputs */
1114 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
1115 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
1116 							 * to IN (II, IIsi) */
1117 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1118 							 * hardware (II, IIsi) */
1119 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1120 						 * code only */
1121 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1122 						 * are on (II, IIsi) */
1123 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
1124 
1125 		ADB_VIA_CLR_INTR();	/* clear interrupt */
1126 		break;
1127 
1128 	case ADB_HW_IISI:
1129 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
1130 						 * outputs */
1131 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
1132 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
1133 							 * to IN (II, IIsi) */
1134 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1135 							 * hardware (II, IIsi) */
1136 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1137 						 * code only */
1138 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1139 						 * are on (II, IIsi) */
1140 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
1141 
1142 		/* get those pesky clock ticks we missed while booting */
1143 		for (i = 0; i < 30; i++) {
1144 			delay(ADB_DELAY);
1145 			adb_hw_setup_IIsi(send_string);
1146 #ifdef ADB_DEBUG
1147 			if (adb_debug) {
1148 				printf_intr("adb: cleanup: ");
1149 				print_single(send_string);
1150 			}
1151 #endif
1152 			delay(ADB_DELAY);
1153 			if (ADB_INTR_IS_OFF)
1154 				break;
1155 		}
1156 		break;
1157 
1158 	case ADB_HW_PB:
1159 		/*
1160 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
1161 		 * pm_direct.h?
1162 		 */
1163 		write_via_reg(VIA1, vIFR, 0x90);	/* clear interrupt */
1164 		break;
1165 
1166 	case ADB_HW_CUDA:
1167 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
1168 						 * outputs */
1169 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
1170 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
1171 							 * to IN */
1172 		write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
1173 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1174 							 * hardware */
1175 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1176 						 * code only */
1177 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1178 						 * are on */
1179 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
1180 
1181 		/* sort of a device reset */
1182 		i = ADB_SR();	/* clear interrupt */
1183 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
1184 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
1185 		delay(ADB_DELAY);
1186 		ADB_SET_STATE_TIP();	/* signal start of frame */
1187 		delay(ADB_DELAY);
1188 		ADB_TOGGLE_STATE_ACK_CUDA();
1189 		delay(ADB_DELAY);
1190 		ADB_CLR_STATE_TIP();
1191 		delay(ADB_DELAY);
1192 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
1193 		i = ADB_SR();	/* clear interrupt */
1194 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
1195 		break;
1196 
1197 	case ADB_HW_UNKNOWN:
1198 	default:
1199 		write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
1200 						 * DO: turn PB ints off? */
1201 		return;
1202 		break;
1203 	}
1204 }
1205 
1206 
1207 /*
1208  * adb_hw_setup_IIsi
1209  * This is sort of a "read" routine that forces the adb hardware through a read cycle
1210  * if there is something waiting. This helps "clean up" any commands that may have gotten
1211  * stuck or stopped during the boot process.
1212  *
1213  */
1214 void
1215 adb_hw_setup_IIsi(u_char * buffer)
1216 {
1217 	panic("adb_hw_setup_IIsi");
1218 }
1219 
1220 
1221 /*
1222  * adb_reinit sets up the adb stuff
1223  *
1224  */
1225 void
1226 adb_reinit(void)
1227 {
1228 	u_char send_string[ADB_MAX_MSG_LENGTH];
1229 	ADBDataBlock data;	/* temp. holder for getting device info */
1230 	volatile int i, x;
1231 	int s;
1232 	int command;
1233 	int result;
1234 	int saveptr;		/* point to next free relocation address */
1235 	int device;
1236 	int nonewtimes;		/* times thru loop w/o any new devices */
1237 
1238 	/* Make sure we are not interrupted while building the table. */
1239 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
1240 		s = splhigh();
1241 
1242 	ADBNumDevices = 0;	/* no devices yet */
1243 
1244 	/* Let intr routines know we are running reinit */
1245 	adbStarting = 1;
1246 
1247 	/*
1248 	 * Initialize the ADB table.  For now, we'll always use the same table
1249 	 * that is defined at the beginning of this file - no mallocs.
1250 	 */
1251 	for (i = 0; i < 16; i++)
1252 		ADBDevTable[i].devType = 0;
1253 
1254 	adb_setup_hw_type();	/* setup hardware type */
1255 
1256 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
1257 
1258 	delay(1000);
1259 
1260 	/* send an ADB reset first */
1261 	result = adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
1262 	delay(200000);
1263 
1264 #ifdef ADB_DEBUG
1265 	if (result && adb_debug) {
1266 		printf_intr("adb_reinit: failed to reset, result = %d\n",result);
1267 	}
1268 #endif
1269 
1270 	/*
1271 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
1272 	 * which device addresses are in use and which are free. For each
1273 	 * address that is in use, move the device at that address to a higher
1274 	 * free address. Continue doing this at that address until no device
1275 	 * responds at that address. Then move the last device that was moved
1276 	 * back to the original address. Do this for the remaining addresses
1277 	 * that we determined were in use.
1278 	 *
1279 	 * When finished, do this entire process over again with the updated
1280 	 * list of in use addresses. Do this until no new devices have been
1281 	 * found in 20 passes though the in use address list. (This probably
1282 	 * seems long and complicated, but it's the best way to detect multiple
1283 	 * devices at the same address - sometimes it takes a couple of tries
1284 	 * before the collision is detected.)
1285 	 */
1286 
1287 	/* initial scan through the devices */
1288 	for (i = 1; i < 16; i++) {
1289 		send_string[0] = 0;
1290 		command = ADBTALK(i, 3);
1291 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
1292 		    (Ptr)0, (short)command);
1293 
1294 #ifdef ADB_DEBUG
1295 		if (result && adb_debug) {
1296 			printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
1297 					i,result,send_string[0]);
1298 		}
1299 #endif
1300 
1301 		if (send_string[0] != 0) {
1302 			/* check for valid device handler */
1303 			switch (send_string[2]) {
1304 			case 0:
1305 			case 0xfd:
1306 			case 0xfe:
1307 			case 0xff:
1308 				continue;	/* invalid, skip */
1309 			}
1310 
1311 			/* found a device */
1312 			++ADBNumDevices;
1313 			KASSERT(ADBNumDevices < 16);
1314 			ADBDevTable[ADBNumDevices].devType =
1315 				(int)send_string[2];
1316 			ADBDevTable[ADBNumDevices].origAddr = i;
1317 			ADBDevTable[ADBNumDevices].currentAddr = i;
1318 			ADBDevTable[ADBNumDevices].DataAreaAddr =
1319 			    (long)0;
1320 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
1321 			pm_check_adb_devices(i);	/* tell pm driver device
1322 							 * is here */
1323 		}
1324 	}
1325 
1326 	/* find highest unused address */
1327 	for (saveptr = 15; saveptr > 0; saveptr--)
1328 		if (-1 == get_adb_info(&data, saveptr))
1329 			break;
1330 
1331 #ifdef ADB_DEBUG
1332 	if (adb_debug & 0x80) {
1333 		printf_intr("first free is: 0x%02x\n", saveptr);
1334 		printf_intr("devices: %i\n", ADBNumDevices);
1335 	}
1336 #endif
1337 
1338 	nonewtimes = 0;		/* no loops w/o new devices */
1339 	while (saveptr > 0 && nonewtimes++ < 11) {
1340 		for (i = 1; i <= ADBNumDevices; i++) {
1341 			device = ADBDevTable[i].currentAddr;
1342 #ifdef ADB_DEBUG
1343 			if (adb_debug & 0x80)
1344 				printf_intr("moving device 0x%02x to 0x%02x "
1345 				    "(index 0x%02x)  ", device, saveptr, i);
1346 #endif
1347 
1348 			/* send TALK R3 to address */
1349 			command = ADBTALK(device, 3);
1350 			adb_op_sync((Ptr)send_string, (Ptr)0,
1351 			    (Ptr)0, (short)command);
1352 
1353 			/* move device to higher address */
1354 			command = ADBLISTEN(device, 3);
1355 			send_string[0] = 2;
1356 			send_string[1] = (u_char)(saveptr | 0x60);
1357 			send_string[2] = 0xfe;
1358 			adb_op_sync((Ptr)send_string, (Ptr)0,
1359 			    (Ptr)0, (short)command);
1360 			delay(500);
1361 
1362 			/* send TALK R3 - anything at new address? */
1363 			command = ADBTALK(saveptr, 3);
1364 			adb_op_sync((Ptr)send_string, (Ptr)0,
1365 			    (Ptr)0, (short)command);
1366 			delay(500);
1367 
1368 			if (send_string[0] == 0) {
1369 #ifdef ADB_DEBUG
1370 				if (adb_debug & 0x80)
1371 					printf_intr("failed, continuing\n");
1372 #endif
1373 				continue;
1374 			}
1375 
1376 			/* send TALK R3 - anything at old address? */
1377 			command = ADBTALK(device, 3);
1378 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
1379 			    (Ptr)0, (short)command);
1380 			if (send_string[0] != 0) {
1381 				/* check for valid device handler */
1382 				switch (send_string[2]) {
1383 				case 0:
1384 				case 0xfd:
1385 				case 0xfe:
1386 				case 0xff:
1387 					continue;	/* invalid, skip */
1388 				}
1389 
1390 				/* new device found */
1391 				/* update data for previously moved device */
1392 				ADBDevTable[i].currentAddr = saveptr;
1393 #ifdef ADB_DEBUG
1394 				if (adb_debug & 0x80)
1395 					printf_intr("old device at index %i\n",i);
1396 #endif
1397 				/* add new device in table */
1398 #ifdef ADB_DEBUG
1399 				if (adb_debug & 0x80)
1400 					printf_intr("new device found\n");
1401 #endif
1402 				if (saveptr > ADBNumDevices) {
1403 					++ADBNumDevices;
1404 					KASSERT(ADBNumDevices < 16);
1405 				}
1406 				ADBDevTable[ADBNumDevices].devType =
1407 					(int)send_string[2];
1408 				ADBDevTable[ADBNumDevices].origAddr = device;
1409 				ADBDevTable[ADBNumDevices].currentAddr = device;
1410 				/* These will be set correctly in adbsys.c */
1411 				/* Until then, unsol. data will be ignored. */
1412 				ADBDevTable[ADBNumDevices].DataAreaAddr =
1413 				    (long)0;
1414 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
1415 				    (void *)0;
1416 				/* find next unused address */
1417 				for (x = saveptr; x > 0; x--) {
1418 					if (-1 == get_adb_info(&data, x)) {
1419 						saveptr = x;
1420 						break;
1421 					}
1422 				}
1423 				if (x == 0)
1424 					saveptr = 0;
1425 #ifdef ADB_DEBUG
1426 				if (adb_debug & 0x80)
1427 					printf_intr("new free is 0x%02x\n",
1428 					    saveptr);
1429 #endif
1430 				nonewtimes = 0;
1431 				/* tell pm driver device is here */
1432 				pm_check_adb_devices(device);
1433 			} else {
1434 #ifdef ADB_DEBUG
1435 				if (adb_debug & 0x80)
1436 					printf_intr("moving back...\n");
1437 #endif
1438 				/* move old device back */
1439 				command = ADBLISTEN(saveptr, 3);
1440 				send_string[0] = 2;
1441 				send_string[1] = (u_char)(device | 0x60);
1442 				send_string[2] = 0xfe;
1443 				adb_op_sync((Ptr)send_string, (Ptr)0,
1444 				    (Ptr)0, (short)command);
1445 				delay(1000);
1446 			}
1447 		}
1448 	}
1449 
1450 #ifdef ADB_DEBUG
1451 	if (adb_debug) {
1452 		for (i = 1; i <= ADBNumDevices; i++) {
1453 			x = get_ind_adb_info(&data, i);
1454 			if (x != -1)
1455 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
1456 				    i, x, data.devType);
1457 		}
1458 	}
1459 #endif
1460 
1461 #ifndef MRG_ADB
1462 	/* enable the programmer's switch, if we have one */
1463 	adb_prog_switch_enable();
1464 #endif
1465 
1466 #ifdef ADB_DEBUG
1467 	if (adb_debug) {
1468 		if (0 == ADBNumDevices)	/* tell user if no devices found */
1469 			printf_intr("adb: no devices found\n");
1470 	}
1471 #endif
1472 
1473 	adbStarting = 0;	/* not starting anymore */
1474 #ifdef ADB_DEBUG
1475 	if (adb_debug)
1476 		printf_intr("adb: ADBReInit complete\n");
1477 #endif
1478 
1479 	if (adbHardware == ADB_HW_CUDA)
1480 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
1481 		    (void *)adb_cuda_tickle, NULL);
1482 
1483 	if (adbHardware != ADB_HW_PB)	/* ints must be on for PB? */
1484 		splx(s);
1485 }
1486 
1487 /*
1488  * adb_cmd_result
1489  *
1490  * This routine lets the caller know whether the specified adb command string
1491  * should expect a returned result, such as a TALK command.
1492  *
1493  * returns: 0 if a result should be expected
1494  *          1 if a result should NOT be expected
1495  */
1496 int
1497 adb_cmd_result(u_char *in)
1498 {
1499 	switch (adbHardware) {
1500 	case ADB_HW_II:
1501 		/* was it an ADB talk command? */
1502 		if ((in[1] & 0x0c) == 0x0c)
1503 			return 0;
1504 		return 1;
1505 
1506 	case ADB_HW_IISI:
1507 	case ADB_HW_CUDA:
1508 		/* was it an ADB talk command? */
1509 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
1510 			return 0;
1511 		/* was it an RTC/PRAM read date/time? */
1512 		if ((in[1] == 0x01) && (in[2] == 0x03))
1513 			return 0;
1514 		return 1;
1515 
1516 	case ADB_HW_PB:
1517 		return 1;
1518 
1519 	case ADB_HW_UNKNOWN:
1520 	default:
1521 		return 1;
1522 	}
1523 }
1524 
1525 
1526 /*
1527  * adb_cmd_extra
1528  *
1529  * This routine lets the caller know whether the specified adb command string
1530  * may have extra data appended to the end of it, such as a LISTEN command.
1531  *
1532  * returns: 0 if extra data is allowed
1533  *          1 if extra data is NOT allowed
1534  */
1535 int
1536 adb_cmd_extra(u_char *in)
1537 {
1538 	switch (adbHardware) {
1539 		case ADB_HW_II:
1540 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
1541 			return 0;
1542 		return 1;
1543 
1544 	case ADB_HW_IISI:
1545 	case ADB_HW_CUDA:
1546 		/*
1547 		 * TO DO: support needs to be added to recognize RTC and PRAM
1548 		 * commands
1549 		 */
1550 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
1551 			return 0;
1552 		/* add others later */
1553 		return 1;
1554 
1555 	case ADB_HW_PB:
1556 		return 1;
1557 
1558 	case ADB_HW_UNKNOWN:
1559 	default:
1560 		return 1;
1561 	}
1562 }
1563 
1564 /*
1565  * adb_op_sync
1566  *
1567  * This routine does exactly what the adb_op routine does, except that after
1568  * the adb_op is called, it waits until the return value is present before
1569  * returning.
1570  *
1571  * NOTE: The user specified compRout is ignored, since this routine specifies
1572  * it's own to adb_op, which is why you really called this in the first place
1573  * anyway.
1574  */
1575 int
1576 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
1577 {
1578 	int tmout;
1579 	int result;
1580 	volatile int flag = 0;
1581 
1582 	result = adb_op(buffer, (void *)adb_op_comprout,
1583 	    (void *)&flag, command);	/* send command */
1584 	if (result == 0) {		/* send ok? */
1585 		/*
1586 		 * Total time to wait is calculated as follows:
1587 		 *  - Tlt (stop to start time): 260 usec
1588 		 *  - start bit: 100 usec
1589 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
1590 		 *  - stop bit (with SRQ): 140 usec
1591 		 * Total: 6900 usec
1592 		 *
1593 		 * This is the total time allowed by the specification.  Any
1594 		 * device that doesn't conform to this will fail to operate
1595 		 * properly on some Apple systems.  In spite of this we
1596 		 * double the time to wait; some Cuda-based apparently
1597 		 * queues some commands and allows the main CPU to continue
1598 		 * processing (radical concept, eh?).  To be safe, allow
1599 		 * time for two complete ADB transactions to occur.
1600 		 */
1601 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
1602 			delay(10);
1603 		if (!flag && tmout > 0)
1604 			delay(tmout);
1605 
1606 		if (!flag)
1607 			result = -2;
1608 	}
1609 
1610 	return result;
1611 }
1612 
1613 /*
1614  * adb_op_comprout
1615  *
1616  * This function is used by the adb_op_sync routine so it knows when the
1617  * function is done.
1618  */
1619 void
1620 adb_op_comprout(buffer, compdata, cmd)
1621 	caddr_t buffer, compdata;
1622 	int cmd;
1623 {
1624 	short *p = (short *)compdata;
1625 
1626 	*p = 1;
1627 }
1628 
1629 void
1630 adb_setup_hw_type(void)
1631 {
1632 	switch (adbHardware) {
1633 	case ADB_HW_CUDA:
1634 		adbSoftPower = 1;
1635 		return;
1636 
1637 	case ADB_HW_PB:
1638 		adbSoftPower = 1;
1639 		pm_setup_adb();
1640 		return;
1641 
1642 	default:
1643 		panic("unknown adb hardware");
1644 	}
1645 #if 0
1646 	response = 0; /*mac68k_machine.machineid;*/
1647 
1648 	/*
1649 	 * Determine what type of ADB hardware we are running on.
1650 	 */
1651 	switch (response) {
1652 	case MACH_MACC610:		/* Centris 610 */
1653 	case MACH_MACC650:		/* Centris 650 */
1654 	case MACH_MACII:		/* II */
1655 	case MACH_MACIICI:		/* IIci */
1656 	case MACH_MACIICX:		/* IIcx */
1657 	case MACH_MACIIX:		/* IIx */
1658 	case MACH_MACQ610:		/* Quadra 610 */
1659 	case MACH_MACQ650:		/* Quadra 650 */
1660 	case MACH_MACQ700:		/* Quadra 700 */
1661 	case MACH_MACQ800:		/* Quadra 800 */
1662 	case MACH_MACSE30:		/* SE/30 */
1663 		adbHardware = ADB_HW_II;
1664 #ifdef ADB_DEBUG
1665 		if (adb_debug)
1666 			printf_intr("adb: using II series hardware support\n");
1667 #endif
1668 		break;
1669 
1670 	case MACH_MACCLASSICII:		/* Classic II */
1671 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
1672 	case MACH_MACLCIII:		/* LC III, Performa 450 */
1673 	case MACH_MACIISI:		/* IIsi */
1674 	case MACH_MACIIVI:		/* IIvi */
1675 	case MACH_MACIIVX:		/* IIvx */
1676 	case MACH_MACP460:		/* Performa 460/465/467 */
1677 	case MACH_MACP600:		/* Performa 600 */
1678 		adbHardware = ADB_HW_IISI;
1679 #ifdef ADB_DEBUG
1680 		if (adb_debug)
1681 			printf_intr("adb: using IIsi series hardware support\n");
1682 #endif
1683 		break;
1684 
1685 	case MACH_MACPB140:		/* PowerBook 140 */
1686 	case MACH_MACPB145:		/* PowerBook 145 */
1687 	case MACH_MACPB150:		/* PowerBook 150 */
1688 	case MACH_MACPB160:		/* PowerBook 160 */
1689 	case MACH_MACPB165:		/* PowerBook 165 */
1690 	case MACH_MACPB165C:		/* PowerBook 165c */
1691 	case MACH_MACPB170:		/* PowerBook 170 */
1692 	case MACH_MACPB180:		/* PowerBook 180 */
1693 	case MACH_MACPB180C:		/* PowerBook 180c */
1694 		adbHardware = ADB_HW_PB;
1695 		pm_setup_adb();
1696 #ifdef ADB_DEBUG
1697 		if (adb_debug)
1698 			printf_intr("adb: using PowerBook 100-series hardware support\n");
1699 #endif
1700 		break;
1701 
1702 	case MACH_MACPB210:		/* PowerBook Duo 210 */
1703 	case MACH_MACPB230:		/* PowerBook Duo 230 */
1704 	case MACH_MACPB250:		/* PowerBook Duo 250 */
1705 	case MACH_MACPB270:		/* PowerBook Duo 270 */
1706 	case MACH_MACPB280:		/* PowerBook Duo 280 */
1707 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
1708 	case MACH_MACPB500:		/* PowerBook 500 series */
1709 		adbHardware = ADB_HW_PB;
1710 		pm_setup_adb();
1711 #ifdef ADB_DEBUG
1712 		if (adb_debug)
1713 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
1714 #endif
1715 		break;
1716 
1717 	case MACH_MACC660AV:		/* Centris 660AV */
1718 	case MACH_MACCCLASSIC:		/* Color Classic */
1719 	case MACH_MACCCLASSICII:	/* Color Classic II */
1720 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
1721 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
1722 	case MACH_MACLC520:		/* LC 520 */
1723 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
1724 	case MACH_MACP550:		/* LC 550, Performa 550 */
1725 	case MACH_MACP580:		/* Performa 580/588 */
1726 	case MACH_MACQ605:		/* Quadra 605 */
1727 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
1728 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
1729 	case MACH_MACQ840AV:		/* Quadra 840AV */
1730 		adbHardware = ADB_HW_CUDA;
1731 #ifdef ADB_DEBUG
1732 		if (adb_debug)
1733 			printf_intr("adb: using Cuda series hardware support\n");
1734 #endif
1735 		break;
1736 	default:
1737 		adbHardware = ADB_HW_UNKNOWN;
1738 #ifdef ADB_DEBUG
1739 		if (adb_debug) {
1740 			printf_intr("adb: hardware type unknown for this machine\n");
1741 			printf_intr("adb: ADB support is disabled\n");
1742 		}
1743 #endif
1744 		break;
1745 	}
1746 
1747 	/*
1748 	 * Determine whether this machine has ADB based soft power.
1749 	 */
1750 	switch (response) {
1751 	case MACH_MACCCLASSIC:		/* Color Classic */
1752 	case MACH_MACCCLASSICII:	/* Color Classic II */
1753 	case MACH_MACIISI:		/* IIsi */
1754 	case MACH_MACIIVI:		/* IIvi */
1755 	case MACH_MACIIVX:		/* IIvx */
1756 	case MACH_MACLC520:		/* LC 520 */
1757 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
1758 	case MACH_MACP550:		/* LC 550, Performa 550 */
1759 	case MACH_MACP600:		/* Performa 600 */
1760 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
1761 	case MACH_MACQ840AV:		/* Quadra 840AV */
1762 		adbSoftPower = 1;
1763 		break;
1764 	}
1765 #endif
1766 }
1767 
1768 int
1769 count_adbs(void)
1770 {
1771 	int i;
1772 	int found;
1773 
1774 	found = 0;
1775 
1776 	for (i = 1; i < 16; i++)
1777 		if (0 != ADBDevTable[i].devType)
1778 			found++;
1779 
1780 	return found;
1781 }
1782 
1783 int
1784 get_ind_adb_info(ADBDataBlock * info, int index)
1785 {
1786 	if ((index < 1) || (index > 15))	/* check range 1-15 */
1787 		return (-1);
1788 
1789 #ifdef ADB_DEBUG
1790 	if (adb_debug & 0x80)
1791 		printf_intr("index 0x%x devType is: 0x%x\n", index,
1792 		    ADBDevTable[index].devType);
1793 #endif
1794 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
1795 		return (-1);
1796 
1797 	info->devType = ADBDevTable[index].devType;
1798 	info->origADBAddr = ADBDevTable[index].origAddr;
1799 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
1800 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
1801 
1802 	return (ADBDevTable[index].currentAddr);
1803 }
1804 
1805 int
1806 get_adb_info(ADBDataBlock * info, int adbAddr)
1807 {
1808 	int i;
1809 
1810 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
1811 		return (-1);
1812 
1813 	for (i = 1; i < 15; i++)
1814 		if (ADBDevTable[i].currentAddr == adbAddr) {
1815 			info->devType = ADBDevTable[i].devType;
1816 			info->origADBAddr = ADBDevTable[i].origAddr;
1817 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
1818 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
1819 			return 0;	/* found */
1820 		}
1821 
1822 	return (-1);		/* not found */
1823 }
1824 
1825 int
1826 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
1827 {
1828 	int i;
1829 
1830 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
1831 		return (-1);
1832 
1833 	for (i = 1; i < 15; i++)
1834 		if (ADBDevTable[i].currentAddr == adbAddr) {
1835 			ADBDevTable[i].ServiceRtPtr =
1836 			    (void *)(info->siServiceRtPtr);
1837 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
1838 			return 0;	/* found */
1839 		}
1840 
1841 	return (-1);		/* not found */
1842 
1843 }
1844 
1845 #ifndef MRG_ADB
1846 
1847 /* caller should really use machine-independant version: getPramTime */
1848 /* this version does pseudo-adb access only */
1849 int
1850 adb_read_date_time(unsigned long *time)
1851 {
1852 	u_char output[ADB_MAX_MSG_LENGTH];
1853 	int result;
1854 	volatile int flag = 0;
1855 
1856 	switch (adbHardware) {
1857 	case ADB_HW_II:
1858 		return -1;
1859 
1860 	case ADB_HW_IISI:
1861 		output[0] = 0x02;	/* 2 byte message */
1862 		output[1] = 0x01;	/* to pram/rtc device */
1863 		output[2] = 0x03;	/* read date/time */
1864 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
1865 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
1866 		if (result != 0)	/* exit if not sent */
1867 			return -1;
1868 
1869 		while (0 == flag)	/* wait for result */
1870 			;
1871 
1872 		*time = (long)(*(long *)(output + 1));
1873 		return 0;
1874 
1875 	case ADB_HW_PB:
1876 		pm_read_date_time(time);
1877 		return 0;
1878 
1879 	case ADB_HW_CUDA:
1880 		output[0] = 0x02;	/* 2 byte message */
1881 		output[1] = 0x01;	/* to pram/rtc device */
1882 		output[2] = 0x03;	/* read date/time */
1883 		result = send_adb_cuda((u_char *)output, (u_char *)output,
1884 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
1885 		if (result != 0)	/* exit if not sent */
1886 			return -1;
1887 
1888 		while (0 == flag)	/* wait for result */
1889 			;
1890 
1891 		memcpy(time, output + 1, 4);
1892 		return 0;
1893 
1894 	case ADB_HW_UNKNOWN:
1895 	default:
1896 		return -1;
1897 	}
1898 }
1899 
1900 /* caller should really use machine-independant version: setPramTime */
1901 /* this version does pseudo-adb access only */
1902 int
1903 adb_set_date_time(unsigned long time)
1904 {
1905 	u_char output[ADB_MAX_MSG_LENGTH];
1906 	int result;
1907 	volatile int flag = 0;
1908 
1909 	switch (adbHardware) {
1910 
1911 	case ADB_HW_CUDA:
1912 		output[0] = 0x06;	/* 6 byte message */
1913 		output[1] = 0x01;	/* to pram/rtc device */
1914 		output[2] = 0x09;	/* set date/time */
1915 		output[3] = (u_char)(time >> 24);
1916 		output[4] = (u_char)(time >> 16);
1917 		output[5] = (u_char)(time >> 8);
1918 		output[6] = (u_char)(time);
1919 		result = send_adb_cuda((u_char *)output, (u_char *)0,
1920 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
1921 		if (result != 0)	/* exit if not sent */
1922 			return -1;
1923 
1924 		while (0 == flag)	/* wait for send to finish */
1925 			;
1926 
1927 		return 0;
1928 
1929 	case ADB_HW_PB:
1930 		pm_set_date_time(time);
1931 		return 0;
1932 
1933 	case ADB_HW_II:
1934 	case ADB_HW_IISI:
1935 	case ADB_HW_UNKNOWN:
1936 	default:
1937 		return -1;
1938 	}
1939 }
1940 
1941 
1942 int
1943 adb_poweroff(void)
1944 {
1945 	u_char output[ADB_MAX_MSG_LENGTH];
1946 	int result;
1947 
1948 	if (!adbSoftPower)
1949 		return -1;
1950 
1951 	adb_polling = 1;
1952 
1953 	switch (adbHardware) {
1954 	case ADB_HW_IISI:
1955 		output[0] = 0x02;	/* 2 byte message */
1956 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
1957 		output[2] = 0x0a;	/* set date/time */
1958 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
1959 		    (void *)0, (void *)0, (int)0);
1960 		if (result != 0)	/* exit if not sent */
1961 			return -1;
1962 
1963 		for (;;);		/* wait for power off */
1964 
1965 		return 0;
1966 
1967 	case ADB_HW_PB:
1968 		pm_adb_poweroff();
1969 
1970 		for (;;);		/* wait for power off */
1971 
1972 		return 0;
1973 
1974 	case ADB_HW_CUDA:
1975 		output[0] = 0x02;	/* 2 byte message */
1976 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
1977 		output[2] = 0x0a;	/* set date/time */
1978 		result = send_adb_cuda((u_char *)output, (u_char *)0,
1979 		    (void *)0, (void *)0, (int)0);
1980 		if (result != 0)	/* exit if not sent */
1981 			return -1;
1982 
1983 		for (;;);		/* wait for power off */
1984 
1985 		return 0;
1986 
1987 	case ADB_HW_II:			/* II models don't do ADB soft power */
1988 	case ADB_HW_UNKNOWN:
1989 	default:
1990 		return -1;
1991 	}
1992 }
1993 
1994 int
1995 adb_prog_switch_enable(void)
1996 {
1997 	u_char output[ADB_MAX_MSG_LENGTH];
1998 	int result;
1999 	volatile int flag = 0;
2000 
2001 	switch (adbHardware) {
2002 	case ADB_HW_IISI:
2003 		output[0] = 0x03;	/* 3 byte message */
2004 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2005 		output[2] = 0x1c;	/* prog. switch control */
2006 		output[3] = 0x01;	/* enable */
2007 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2008 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2009 		if (result != 0)	/* exit if not sent */
2010 			return -1;
2011 
2012 		while (0 == flag)	/* wait for send to finish */
2013 			;
2014 
2015 		return 0;
2016 
2017 	case ADB_HW_PB:
2018 		return -1;
2019 
2020 	case ADB_HW_II:		/* II models don't do prog. switch */
2021 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
2022 	case ADB_HW_UNKNOWN:
2023 	default:
2024 		return -1;
2025 	}
2026 }
2027 
2028 int
2029 adb_prog_switch_disable(void)
2030 {
2031 	u_char output[ADB_MAX_MSG_LENGTH];
2032 	int result;
2033 	volatile int flag = 0;
2034 
2035 	switch (adbHardware) {
2036 	case ADB_HW_IISI:
2037 		output[0] = 0x03;	/* 3 byte message */
2038 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2039 		output[2] = 0x1c;	/* prog. switch control */
2040 		output[3] = 0x01;	/* disable */
2041 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2042 			(void *)adb_op_comprout, (void *)&flag, (int)0);
2043 		if (result != 0)	/* exit if not sent */
2044 			return -1;
2045 
2046 		while (0 == flag)	/* wait for send to finish */
2047 			;
2048 
2049 		return 0;
2050 
2051 	case ADB_HW_PB:
2052 		return -1;
2053 
2054 	case ADB_HW_II:		/* II models don't do prog. switch */
2055 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
2056 	case ADB_HW_UNKNOWN:
2057 	default:
2058 		return -1;
2059 	}
2060 }
2061 
2062 int
2063 CountADBs(void)
2064 {
2065 	return (count_adbs());
2066 }
2067 
2068 void
2069 ADBReInit(void)
2070 {
2071 	adb_reinit();
2072 }
2073 
2074 int
2075 GetIndADB(ADBDataBlock * info, int index)
2076 {
2077 	return (get_ind_adb_info(info, index));
2078 }
2079 
2080 int
2081 GetADBInfo(ADBDataBlock * info, int adbAddr)
2082 {
2083 	return (get_adb_info(info, adbAddr));
2084 }
2085 
2086 int
2087 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2088 {
2089 	return (set_adb_info(info, adbAddr));
2090 }
2091 
2092 int
2093 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2094 {
2095 	return (adb_op(buffer, compRout, data, commandNum));
2096 }
2097 
2098 #endif
2099 
2100 int
2101 setsoftadb()
2102 {
2103 	callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
2104 	return 0;
2105 }
2106 
2107 void
2108 adb_cuda_autopoll()
2109 {
2110 	volatile int flag = 0;
2111 	int result;
2112 	u_char output[16];
2113 
2114 	output[0] = 0x03;	/* 3-byte message */
2115 	output[1] = 0x01;	/* to pram/rtc device */
2116 	output[2] = 0x01;	/* cuda autopoll */
2117 	output[3] = 0x01;
2118 	result = send_adb_cuda(output, output, adb_op_comprout, (void *)&flag,
2119 			       0);
2120 	if (result != 0)	/* exit if not sent */
2121 		return;
2122 
2123 	while (flag == 0);	/* wait for result */
2124 }
2125 
2126 void
2127 adb_restart(void)
2128 {
2129 	int result;
2130 	u_char output[16];
2131 
2132 	adb_polling = 1;
2133 
2134 	switch (adbHardware) {
2135 	case ADB_HW_CUDA:
2136 		output[0] = 0x02;	/* 2 byte message */
2137 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2138 		output[2] = 0x11;	/* restart */
2139 		result = send_adb_cuda(output, NULL, NULL, NULL, 0);
2140 		if (result != 0)	/* exit if not sent */
2141 			return;
2142 		while (1);		/* not return */
2143 
2144 	case ADB_HW_PB:
2145 		pm_adb_restart();
2146 		while (1);		/* not return */
2147 	}
2148 }
2149