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