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