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