1 /* $NetBSD: adb.c,v 1.52 2008/04/03 05:03:23 scottr Exp $ */
4 * Copyright (C) 1994 Bradley A. Grantham
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: adb.c,v 1.52 2008/04/03 05:03:23 scottr Exp $");
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/fcntl.h>
37 #include <sys/select.h>
39 #include <sys/signalvar.h>
40 #include <sys/systm.h>
44 #include <machine/autoconf.h>
46 #include <mac68k/mac68k/macrom.h>
47 #include <mac68k/dev/adbvar.h>
48 #include <mac68k/dev/akbdvar.h>
50 #include "aed.h" /* ADB Event Device for compatibility */
53 * Function declarations.
55 static int adbmatch(struct device
*, struct cfdata
*, void *);
56 static void adbattach(struct device
*, struct device
*, void *);
57 static int adbprint(void *, const char *);
58 void adb_config_interrupts(struct device
*);
60 extern void adb_jadbproc(void);
65 int adb_polling
= 0; /* Are we polling? (Debugger mode) */
67 int adb_debug
= 0; /* Output debugging messages */
68 #endif /* ADB_DEBUG */
70 extern struct mac68k_machine_S mac68k_machine
;
71 extern int adbHardware
;
72 extern char *adbHardwareDescr
[];
77 CFATTACH_DECL(adb
, sizeof(struct device
),
78 adbmatch
, adbattach
, NULL
, NULL
);
81 adbmatch(struct device
*parent
, struct cfdata
*cf
, void *aux
)
83 static int adb_matched
= 0;
85 /* Allow only one instance. */
94 adbattach(struct device
*parent
, struct device
*self
, void *aux
)
97 adb_softintr_cookie
= softint_establish(SOFTINT_SERIAL
,
98 (void (*)(void *))adb_soft_intr
, NULL
);
102 * Defer configuration until interrupts are enabled.
104 config_interrupts(self
, adb_config_interrupts
);
108 adb_config_interrupts(struct device
*self
)
110 ADBDataBlock adbdata
;
111 struct adb_attach_args aa_args
;
113 int adbindex
, adbaddr
;
115 printf("%s", self
->dv_xname
);
119 if (!mrg_romready()) {
120 printf(": no ROM ADB driver in this kernel for this machine\n");
126 printf("adb: call mrg_initadbintr\n");
129 mrg_initadbintr(); /* Mac ROM Glue okay to do ROM intr */
132 printf("adb: returned from mrg_initadbintr\n");
135 /* ADBReInit pre/post-processing */
136 JADBProc
= adb_jadbproc
;
141 printf("adb: calling ADBAlternateInit.\n");
148 printf(" (direct, %s)", adbHardwareDescr
[adbHardware
]);
153 printf("adb: done with ADBReInit\n");
156 totaladbs
= CountADBs();
158 printf(": %d target%s\n", totaladbs
, (totaladbs
== 1) ? "" : "s");
161 /* ADB event device for compatibility */
162 aa_args
.origaddr
= 0;
164 aa_args
.handler_id
= 0;
165 (void)config_found(self
, &aa_args
, adbprint
);
168 /* for each ADB device */
169 for (adbindex
= 1; adbindex
<= totaladbs
; adbindex
++) {
170 /* Get the ADB information */
171 adbaddr
= GetIndADB(&adbdata
, adbindex
);
173 aa_args
.origaddr
= (int)(adbdata
.origADBAddr
);
174 aa_args
.adbaddr
= adbaddr
;
175 aa_args
.handler_id
= (int)(adbdata
.devType
);
177 (void)config_found(self
, &aa_args
, adbprint
);
184 adbprint(void *args
, const char *name
)
186 struct adb_attach_args
*aa_args
= (struct adb_attach_args
*)args
;
189 if (name
) { /* no configured device matched */
190 rv
= UNSUPP
; /* most ADB device types are unsupported */
192 /* print out what kind of ADB device we have found */
193 aprint_normal("%s addr %d: ", name
, aa_args
->adbaddr
);
194 switch(aa_args
->origaddr
) {
197 aprint_normal("ADB event device");
201 aprint_normal("security dongle (%d)",
202 aa_args
->handler_id
);
206 aprint_normal("mapped device (%d)",
207 aa_args
->handler_id
);
211 aprint_normal("relative positioning device (%d)",
212 aa_args
->handler_id
);
217 switch (aa_args
->handler_id
) {
219 aprint_normal("WACOM ArtPad II");
222 aprint_normal("absolute positioning device (%d)",
223 aa_args
->handler_id
);
228 aprint_normal("data transfer device (modem?) (%d)",
229 aa_args
->handler_id
);
232 switch (aa_args
->handler_id
) {
234 aprint_normal("Sophisticated Circuits PowerKey");
237 aprint_normal("misc. device (remote control?) (%d)",
238 aa_args
->handler_id
);
243 aprint_normal("unknown type device, (handler %d)",
244 aa_args
->handler_id
);
246 #endif /* DIAGNOSTIC */
248 } else /* a device matched and was configured */
249 aprint_normal(" addr %d: ", aa_args
->adbaddr
);
258 * This routine does exactly what the adb_op routine does, except that after
259 * the adb_op is called, it waits until the return value is present before
262 * NOTE: The user specified compRout is ignored, since this routine specifies
263 * it's own to adb_op, which is why you really called this in the first place
267 adb_op_sync(Ptr buffer
, Ptr compRout
, Ptr data
, short command
)
270 volatile int flag
= 0;
272 result
= ADBOp(buffer
, (void *)adb_op_comprout
, __UNVOLATILE(&flag
),
273 command
); /* send command */
274 if (result
== 0) { /* send ok? */
286 * Implements a spin-wait with timeout to be used for synchronous
287 * operations on the ADB bus.
289 * Total time to wait is calculated as follows:
290 * - Tlt (stop to start time): 260 usec
291 * - start bit: 100 usec
292 * - up to 8 data bytes: 64 * 100 usec = 6400 usec
293 * - stop bit (with SRQ): 140 usec
296 * This is the total time allowed by the specification. Any device that
297 * doesn't conform to this will fail to operate properly on some Apple
298 * systems. In spite of this we double the time to wait; Cuda-based
299 * systems apparently queue commands and allow the main CPU to continue
300 * processing (how radical!). To be safe, allow time for two complete
301 * ADB transactions to occur.
304 adb_spin(volatile int *fp
)
308 for (tmout
= 13800; *fp
== 0 && tmout
>= 10; tmout
-= 10)
310 if (*fp
== 0 && tmout
> 0)
318 * This function is used by the adb_op_sync routine so it knows when the
322 adb_op_comprout(void)
324 __asm("movw #1,%a2@ | update flag value");