adb_bus.c revision 1.1.2.2 1 1.1.2.2 ad /* $NetBSD: adb_bus.c,v 1.1.2.2 2007/02/06 17:10:30 ad Exp $ */
2 1.1.2.2 ad
3 1.1.2.2 ad /*-
4 1.1.2.2 ad * Copyright (c) 2006 Michael Lorenz
5 1.1.2.2 ad * All rights reserved.
6 1.1.2.2 ad *
7 1.1.2.2 ad * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 ad * modification, are permitted provided that the following conditions
9 1.1.2.2 ad * are met:
10 1.1.2.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 ad * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 ad * documentation and/or other materials provided with the distribution.
15 1.1.2.2 ad * 3. Neither the name of The NetBSD Foundation nor the names of its
16 1.1.2.2 ad * contributors may be used to endorse or promote products derived
17 1.1.2.2 ad * from this software without specific prior written permission.
18 1.1.2.2 ad *
19 1.1.2.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 ad */
31 1.1.2.2 ad
32 1.1.2.2 ad #include <sys/cdefs.h>
33 1.1.2.2 ad __KERNEL_RCSID(0, "$NetBSD: adb_bus.c,v 1.1.2.2 2007/02/06 17:10:30 ad Exp $");
34 1.1.2.2 ad
35 1.1.2.2 ad #include <sys/param.h>
36 1.1.2.2 ad #include <sys/systm.h>
37 1.1.2.2 ad #include <sys/kernel.h>
38 1.1.2.2 ad #include <sys/device.h>
39 1.1.2.2 ad #include <sys/proc.h>
40 1.1.2.2 ad
41 1.1.2.2 ad #include <machine/bus.h>
42 1.1.2.2 ad #include <machine/autoconf.h>
43 1.1.2.2 ad #include <dev/adb/adbvar.h>
44 1.1.2.2 ad
45 1.1.2.2 ad #include "adbdebug.h"
46 1.1.2.2 ad
47 1.1.2.2 ad #ifdef ADB_DEBUG
48 1.1.2.2 ad #define DPRINTF printf
49 1.1.2.2 ad #else
50 1.1.2.2 ad #define DPRINTF while (0) printf
51 1.1.2.2 ad #endif
52 1.1.2.2 ad
53 1.1.2.2 ad static int nadb_match(struct device *, struct cfdata *, void *);
54 1.1.2.2 ad static void nadb_attach(struct device *, struct device *, void *);
55 1.1.2.2 ad
56 1.1.2.2 ad struct nadb_softc {
57 1.1.2.2 ad struct device sc_dev;
58 1.1.2.2 ad struct adb_bus_accessops *sc_ops;
59 1.1.2.2 ad uint32_t sc_msg;
60 1.1.2.2 ad uint32_t sc_event;
61 1.1.2.2 ad struct adb_device sc_devtable[16];
62 1.1.2.2 ad int sc_free; /* highest free address */
63 1.1.2.2 ad int sc_len; /* length of received message */
64 1.1.2.2 ad uint8_t sc_data[16];
65 1.1.2.2 ad };
66 1.1.2.2 ad
67 1.1.2.2 ad CFATTACH_DECL(nadb, sizeof(struct nadb_softc),
68 1.1.2.2 ad nadb_match, nadb_attach, NULL, NULL);
69 1.1.2.2 ad
70 1.1.2.2 ad static void nadb_init(struct device *);
71 1.1.2.2 ad static void nadb_handler(void *, int, uint8_t *);
72 1.1.2.2 ad static void nadb_send_sync(void *, int, int, uint8_t *);
73 1.1.2.2 ad static int nadb_register(struct nadb_softc *, int, int, int);
74 1.1.2.2 ad static void nadb_remove(struct nadb_softc *, int);
75 1.1.2.2 ad static int nadb_devprint(void *, const char *);
76 1.1.2.2 ad
77 1.1.2.2 ad static int
78 1.1.2.2 ad nadb_match(struct device *parent, struct cfdata *cf, void *aux)
79 1.1.2.2 ad {
80 1.1.2.2 ad
81 1.1.2.2 ad return 1;
82 1.1.2.2 ad }
83 1.1.2.2 ad
84 1.1.2.2 ad static void
85 1.1.2.2 ad nadb_attach(struct device *parent, struct device *us, void *aux)
86 1.1.2.2 ad {
87 1.1.2.2 ad struct nadb_softc *sc = (struct nadb_softc *)us;
88 1.1.2.2 ad struct adb_bus_accessops *ops = aux;
89 1.1.2.2 ad
90 1.1.2.2 ad sc->sc_ops = ops;
91 1.1.2.2 ad sc->sc_ops->set_handler(sc->sc_ops->cookie, nadb_handler, sc);
92 1.1.2.2 ad
93 1.1.2.2 ad config_interrupts(us, nadb_init);
94 1.1.2.2 ad }
95 1.1.2.2 ad
96 1.1.2.2 ad static void
97 1.1.2.2 ad nadb_init(struct device *dev)
98 1.1.2.2 ad {
99 1.1.2.2 ad struct nadb_softc *sc = (struct nadb_softc *)dev;
100 1.1.2.2 ad struct adb_attach_args aaa;
101 1.1.2.2 ad int i, last_moved_up, devmask = 0;
102 1.1.2.2 ad uint8_t cmd[2];
103 1.1.2.2 ad
104 1.1.2.2 ad sc->sc_free = 15;
105 1.1.2.2 ad for (i = 0; i < 16; i++) {
106 1.1.2.2 ad sc->sc_devtable[i].original_addr = 0;
107 1.1.2.2 ad sc->sc_devtable[i].current_addr = 0;
108 1.1.2.2 ad sc->sc_devtable[i].handler_id = 0;
109 1.1.2.2 ad sc->sc_devtable[i].cookie = NULL;
110 1.1.2.2 ad sc->sc_devtable[i].handler = NULL;
111 1.1.2.2 ad }
112 1.1.2.2 ad
113 1.1.2.2 ad /* bus reset (?) */
114 1.1.2.2 ad //nadb_send_sync(sc, 0, 0, NULL);
115 1.1.2.2 ad //delay(200000);
116 1.1.2.2 ad
117 1.1.2.2 ad /*
118 1.1.2.2 ad * scan only addresses 1 - 7
119 1.1.2.2 ad * if something responds move it to >7 and see if something else is
120 1.1.2.2 ad * there. If not move the previous one back.
121 1.1.2.2 ad * XXX we don't check for collisions if we use up all addresses >7
122 1.1.2.2 ad */
123 1.1.2.2 ad for (i = 1; i < 8; i++) {
124 1.1.2.2 ad DPRINTF("\n%d: ", i);
125 1.1.2.2 ad last_moved_up = 0;
126 1.1.2.2 ad nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
127 1.1.2.2 ad /* found something? */
128 1.1.2.2 ad while (sc->sc_len > 2) {
129 1.1.2.2 ad /* something answered, so move it up */
130 1.1.2.2 ad
131 1.1.2.2 ad DPRINTF("Found a device on address %d\n", i);
132 1.1.2.2 ad cmd[0] = sc->sc_free | 0x60;
133 1.1.2.2 ad cmd[1] = 0xfe;
134 1.1.2.2 ad nadb_send_sync(sc, ADBLISTEN(i, 3), 2, cmd);
135 1.1.2.2 ad
136 1.1.2.2 ad /* see if it really moved */
137 1.1.2.2 ad nadb_send_sync(sc, ADBTALK(sc->sc_free, 3), 0, NULL);
138 1.1.2.2 ad if (sc->sc_len > 2) {
139 1.1.2.2 ad /* ok */
140 1.1.2.2 ad DPRINTF("moved it to %d\n", sc->sc_free);
141 1.1.2.2 ad nadb_register(sc, sc->sc_free, i, sc->sc_data[3]);
142 1.1.2.2 ad last_moved_up = sc->sc_free;
143 1.1.2.2 ad sc->sc_free--;
144 1.1.2.2 ad }
145 1.1.2.2 ad /* see if something else is there */
146 1.1.2.2 ad nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
147 1.1.2.2 ad }
148 1.1.2.2 ad if (last_moved_up != 0) {
149 1.1.2.2 ad /* move last one back to original address */
150 1.1.2.2 ad cmd[0] = i | 0x60;
151 1.1.2.2 ad cmd[1] = 0xfe;
152 1.1.2.2 ad nadb_send_sync(sc, ADBLISTEN(last_moved_up, 3), 2, cmd);
153 1.1.2.2 ad
154 1.1.2.2 ad nadb_send_sync(sc, ADBTALK(i, 3), 0, NULL);
155 1.1.2.2 ad if (sc->sc_len > 2) {
156 1.1.2.2 ad DPRINTF("moved %d back to %d\n", last_moved_up, i);
157 1.1.2.2 ad nadb_remove(sc, last_moved_up);
158 1.1.2.2 ad nadb_register(sc, i, i, sc->sc_data[3]);
159 1.1.2.2 ad sc->sc_free = last_moved_up;
160 1.1.2.2 ad }
161 1.1.2.2 ad }
162 1.1.2.2 ad }
163 1.1.2.2 ad
164 1.1.2.2 ad /* now attach the buggers we've found */
165 1.1.2.2 ad aaa.ops = sc->sc_ops;
166 1.1.2.2 ad for (i = 0; i < 16; i++) {
167 1.1.2.2 ad if (sc->sc_devtable[i].current_addr != 0) {
168 1.1.2.2 ad DPRINTF("dev: %d %d %02x\n",
169 1.1.2.2 ad sc->sc_devtable[i].current_addr,
170 1.1.2.2 ad sc->sc_devtable[i].original_addr,
171 1.1.2.2 ad sc->sc_devtable[i].handler_id);
172 1.1.2.2 ad aaa.dev = &sc->sc_devtable[i];
173 1.1.2.2 ad if (config_found(&sc->sc_dev, &aaa, nadb_devprint)) {
174 1.1.2.2 ad devmask |= (1 << i);
175 1.1.2.2 ad } else {
176 1.1.2.2 ad printf(" not configured\n");
177 1.1.2.2 ad }
178 1.1.2.2 ad }
179 1.1.2.2 ad }
180 1.1.2.2 ad /* now enable autopolling */
181 1.1.2.2 ad DPRINTF("devmask: %04x\n", devmask);
182 1.1.2.2 ad sc->sc_ops->autopoll(sc->sc_ops->cookie, devmask);
183 1.1.2.2 ad }
184 1.1.2.2 ad
185 1.1.2.2 ad int
186 1.1.2.2 ad nadb_print(void *aux, const char *what)
187 1.1.2.2 ad {
188 1.1.2.2 ad
189 1.1.2.2 ad printf(": Apple Desktop Bus\n");
190 1.1.2.2 ad return 0;
191 1.1.2.2 ad }
192 1.1.2.2 ad
193 1.1.2.2 ad static int
194 1.1.2.2 ad nadb_devprint(void *aux, const char *what)
195 1.1.2.2 ad {
196 1.1.2.2 ad struct adb_attach_args *aaa = aux;
197 1.1.2.2 ad
198 1.1.2.2 ad switch(aaa->dev->original_addr) {
199 1.1.2.2 ad case 2:
200 1.1.2.2 ad printf(": ADB Keyboard");
201 1.1.2.2 ad break;
202 1.1.2.2 ad case 3:
203 1.1.2.2 ad printf(": ADB relative pointing device");
204 1.1.2.2 ad break;
205 1.1.2.2 ad default:
206 1.1.2.2 ad printf(": something from address %d:%02x",
207 1.1.2.2 ad aaa->dev->original_addr,
208 1.1.2.2 ad aaa->dev->handler_id);
209 1.1.2.2 ad break;
210 1.1.2.2 ad }
211 1.1.2.2 ad return 0;
212 1.1.2.2 ad }
213 1.1.2.2 ad
214 1.1.2.2 ad static void
215 1.1.2.2 ad nadb_handler(void *cookie, int len, uint8_t *data)
216 1.1.2.2 ad {
217 1.1.2.2 ad struct nadb_softc *sc = cookie;
218 1.1.2.2 ad struct adb_device *dev;
219 1.1.2.2 ad int addr;
220 1.1.2.2 ad
221 1.1.2.2 ad #ifdef ADB_DEBUG
222 1.1.2.2 ad int i;
223 1.1.2.2 ad printf("adb:");
224 1.1.2.2 ad for (i = 0; i < len; i++) {
225 1.1.2.2 ad printf(" %02x", data[i]);
226 1.1.2.2 ad }
227 1.1.2.2 ad printf("\n");
228 1.1.2.2 ad #endif
229 1.1.2.2 ad
230 1.1.2.2 ad addr = data[1] >> 4;
231 1.1.2.2 ad dev = &sc->sc_devtable[addr];
232 1.1.2.2 ad if ((dev->current_addr != 0) && (dev->handler != NULL)) {
233 1.1.2.2 ad
234 1.1.2.2 ad dev->handler(dev->cookie, len, data);
235 1.1.2.2 ad } else {
236 1.1.2.2 ad sc->sc_msg = 1;
237 1.1.2.2 ad sc->sc_len = len;
238 1.1.2.2 ad memcpy(sc->sc_data, data, len);
239 1.1.2.2 ad wakeup(&sc->sc_event);
240 1.1.2.2 ad }
241 1.1.2.2 ad }
242 1.1.2.2 ad
243 1.1.2.2 ad static void
244 1.1.2.2 ad nadb_send_sync(void *cookie, int command, int len, uint8_t *data)
245 1.1.2.2 ad {
246 1.1.2.2 ad struct nadb_softc *sc = cookie;
247 1.1.2.2 ad
248 1.1.2.2 ad sc->sc_msg = 0;
249 1.1.2.2 ad sc->sc_ops->send(sc->sc_ops->cookie, 0, command, len, data);
250 1.1.2.2 ad while (sc->sc_msg == 0) {
251 1.1.2.2 ad tsleep(&sc->sc_event, 0, "adb_send", 100);
252 1.1.2.2 ad }
253 1.1.2.2 ad }
254 1.1.2.2 ad
255 1.1.2.2 ad static int
256 1.1.2.2 ad nadb_register(struct nadb_softc *sc, int current, int orig, int handler)
257 1.1.2.2 ad {
258 1.1.2.2 ad struct adb_device *dev;
259 1.1.2.2 ad
260 1.1.2.2 ad if ((current > 0) && (current < 16)) {
261 1.1.2.2 ad dev = &sc->sc_devtable[current];
262 1.1.2.2 ad if (dev->current_addr != 0)
263 1.1.2.2 ad /* in use! */
264 1.1.2.2 ad return -1;
265 1.1.2.2 ad dev->current_addr = current;
266 1.1.2.2 ad dev->original_addr = orig;
267 1.1.2.2 ad dev->handler_id = handler;
268 1.1.2.2 ad return 0;
269 1.1.2.2 ad }
270 1.1.2.2 ad return -1;
271 1.1.2.2 ad }
272 1.1.2.2 ad
273 1.1.2.2 ad static void
274 1.1.2.2 ad nadb_remove(struct nadb_softc *sc, int addr)
275 1.1.2.2 ad {
276 1.1.2.2 ad
277 1.1.2.2 ad if ((addr > 0) && (addr < 16)) {
278 1.1.2.2 ad sc->sc_devtable[addr].current_addr = 0;
279 1.1.2.2 ad sc->sc_devtable[addr].original_addr = 0;
280 1.1.2.2 ad sc->sc_devtable[addr].handler_id = 0;
281 1.1.2.2 ad }
282 1.1.2.2 ad }
283