ams.c revision 1.19 1 1.19 macallan /* $NetBSD: ams.c,v 1.19 2006/10/15 21:15:21 macallan Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (C) 1998 Colin Wood
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Redistribution and use in source and binary forms, with or without
8 1.1 tsubai * modification, are permitted provided that the following conditions
9 1.1 tsubai * are met:
10 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.1 tsubai * notice, this list of conditions and the following disclaimer.
12 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.1 tsubai * documentation and/or other materials provided with the distribution.
15 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.1 tsubai * must display the following acknowledgement:
17 1.1 tsubai * This product includes software developed by Colin Wood.
18 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
19 1.1 tsubai * derived from this software without specific prior written permission.
20 1.1 tsubai *
21 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 tsubai */
32 1.16 lukem
33 1.16 lukem #include <sys/cdefs.h>
34 1.19 macallan __KERNEL_RCSID(0, "$NetBSD: ams.c,v 1.19 2006/10/15 21:15:21 macallan Exp $");
35 1.1 tsubai
36 1.1 tsubai #include <sys/param.h>
37 1.1 tsubai #include <sys/device.h>
38 1.1 tsubai #include <sys/fcntl.h>
39 1.1 tsubai #include <sys/poll.h>
40 1.1 tsubai #include <sys/select.h>
41 1.1 tsubai #include <sys/proc.h>
42 1.1 tsubai #include <sys/signalvar.h>
43 1.1 tsubai #include <sys/systm.h>
44 1.1 tsubai
45 1.9 tsubai #include <machine/autoconf.h>
46 1.9 tsubai
47 1.2 tsubai #include <dev/wscons/wsconsio.h>
48 1.2 tsubai #include <dev/wscons/wsmousevar.h>
49 1.2 tsubai
50 1.1 tsubai #include <macppc/dev/adbvar.h>
51 1.1 tsubai #include <macppc/dev/aedvar.h>
52 1.1 tsubai #include <macppc/dev/amsvar.h>
53 1.1 tsubai
54 1.2 tsubai #include "aed.h"
55 1.2 tsubai
56 1.1 tsubai /*
57 1.1 tsubai * Function declarations.
58 1.1 tsubai */
59 1.2 tsubai static int amsmatch __P((struct device *, struct cfdata *, void *));
60 1.2 tsubai static void amsattach __P((struct device *, struct device *, void *));
61 1.2 tsubai static void ems_init __P((struct ams_softc *));
62 1.2 tsubai static void ms_processevent __P((adb_event_t *event, struct ams_softc *));
63 1.19 macallan static void init_trackpad(struct ams_softc *);
64 1.1 tsubai
65 1.1 tsubai /* Driver definition. */
66 1.15 thorpej CFATTACH_DECL(ams, sizeof(struct ams_softc),
67 1.15 thorpej amsmatch, amsattach, NULL, NULL);
68 1.1 tsubai
69 1.2 tsubai int ams_enable __P((void *));
70 1.18 christos int ams_ioctl __P((void *, u_long, caddr_t, int, struct lwp *));
71 1.2 tsubai void ams_disable __P((void *));
72 1.2 tsubai
73 1.19 macallan /*
74 1.19 macallan * handle tapping the trackpad
75 1.19 macallan * different pads report different button counts and use slightly different
76 1.19 macallan * protocols
77 1.19 macallan */
78 1.19 macallan static void ams_mangle_2(struct ams_softc *, int);
79 1.19 macallan static void ams_mangle_4(struct ams_softc *, int);
80 1.19 macallan
81 1.2 tsubai const struct wsmouse_accessops ams_accessops = {
82 1.2 tsubai ams_enable,
83 1.2 tsubai ams_ioctl,
84 1.2 tsubai ams_disable,
85 1.2 tsubai };
86 1.2 tsubai
87 1.1 tsubai static int
88 1.2 tsubai amsmatch(parent, cf, aux)
89 1.1 tsubai struct device *parent;
90 1.1 tsubai struct cfdata *cf;
91 1.1 tsubai void *aux;
92 1.1 tsubai {
93 1.11 tsubai struct adb_attach_args *aa_args = aux;
94 1.1 tsubai
95 1.1 tsubai if (aa_args->origaddr == ADBADDR_MS)
96 1.1 tsubai return 1;
97 1.1 tsubai else
98 1.1 tsubai return 0;
99 1.1 tsubai }
100 1.1 tsubai
101 1.1 tsubai static void
102 1.2 tsubai amsattach(parent, self, aux)
103 1.1 tsubai struct device *parent, *self;
104 1.1 tsubai void *aux;
105 1.1 tsubai {
106 1.1 tsubai ADBSetInfoBlock adbinfo;
107 1.2 tsubai struct ams_softc *sc = (struct ams_softc *)self;
108 1.11 tsubai struct adb_attach_args *aa_args = aux;
109 1.1 tsubai int error;
110 1.2 tsubai struct wsmousedev_attach_args a;
111 1.1 tsubai
112 1.1 tsubai sc->origaddr = aa_args->origaddr;
113 1.1 tsubai sc->adbaddr = aa_args->adbaddr;
114 1.1 tsubai sc->handler_id = aa_args->handler_id;
115 1.1 tsubai
116 1.1 tsubai sc->sc_class = MSCLASS_MOUSE;
117 1.1 tsubai sc->sc_buttons = 1;
118 1.1 tsubai sc->sc_res = 100;
119 1.1 tsubai sc->sc_devid[0] = 0;
120 1.1 tsubai sc->sc_devid[4] = 0;
121 1.1 tsubai
122 1.1 tsubai adbinfo.siServiceRtPtr = (Ptr)ms_adbcomplete;
123 1.1 tsubai adbinfo.siDataAreaAddr = (caddr_t)sc;
124 1.1 tsubai
125 1.1 tsubai ems_init(sc);
126 1.1 tsubai
127 1.1 tsubai /* print out the type of mouse we have */
128 1.1 tsubai switch (sc->handler_id) {
129 1.1 tsubai case ADBMS_100DPI:
130 1.1 tsubai printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
131 1.5 tsubai (int)(sc->sc_res));
132 1.1 tsubai break;
133 1.1 tsubai case ADBMS_200DPI:
134 1.1 tsubai sc->sc_res = 200;
135 1.1 tsubai printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
136 1.5 tsubai (int)(sc->sc_res));
137 1.1 tsubai break;
138 1.1 tsubai case ADBMS_MSA3:
139 1.1 tsubai printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
140 1.5 tsubai sc->sc_buttons, (int)(sc->sc_res));
141 1.1 tsubai break;
142 1.1 tsubai case ADBMS_USPEED:
143 1.1 tsubai printf("MicroSpeed mouse, default parameters\n");
144 1.1 tsubai break;
145 1.5 tsubai case ADBMS_UCONTOUR:
146 1.5 tsubai printf("Contour mouse, default parameters\n");
147 1.5 tsubai break;
148 1.10 tsubai case ADBMS_TURBO:
149 1.10 tsubai printf("Kensington Turbo Mouse\n");
150 1.10 tsubai break;
151 1.1 tsubai case ADBMS_EXTENDED:
152 1.1 tsubai if (sc->sc_devid[0] == '\0') {
153 1.1 tsubai printf("Logitech ");
154 1.1 tsubai switch (sc->sc_class) {
155 1.1 tsubai case MSCLASS_MOUSE:
156 1.1 tsubai printf("MouseMan (non-EMP) mouse");
157 1.1 tsubai break;
158 1.1 tsubai case MSCLASS_TRACKBALL:
159 1.1 tsubai printf("TrackMan (non-EMP) trackball");
160 1.1 tsubai break;
161 1.1 tsubai default:
162 1.1 tsubai printf("non-EMP relative positioning device");
163 1.1 tsubai break;
164 1.1 tsubai }
165 1.1 tsubai printf("\n");
166 1.1 tsubai } else {
167 1.1 tsubai printf("EMP ");
168 1.1 tsubai switch (sc->sc_class) {
169 1.1 tsubai case MSCLASS_TABLET:
170 1.1 tsubai printf("tablet");
171 1.1 tsubai break;
172 1.1 tsubai case MSCLASS_MOUSE:
173 1.1 tsubai printf("mouse");
174 1.1 tsubai break;
175 1.1 tsubai case MSCLASS_TRACKBALL:
176 1.1 tsubai printf("trackball");
177 1.1 tsubai break;
178 1.6 tsubai case MSCLASS_TRACKPAD:
179 1.6 tsubai printf("trackpad");
180 1.19 macallan init_trackpad(sc);
181 1.6 tsubai break;
182 1.1 tsubai default:
183 1.1 tsubai printf("unknown device");
184 1.1 tsubai break;
185 1.1 tsubai }
186 1.1 tsubai printf(" <%s> %d-button, %d dpi\n", sc->sc_devid,
187 1.5 tsubai sc->sc_buttons, (int)(sc->sc_res));
188 1.1 tsubai }
189 1.1 tsubai break;
190 1.1 tsubai default:
191 1.1 tsubai printf("relative positioning device (mouse?) (%d)\n",
192 1.1 tsubai sc->handler_id);
193 1.1 tsubai break;
194 1.1 tsubai }
195 1.1 tsubai error = SetADBInfo(&adbinfo, sc->adbaddr);
196 1.1 tsubai #ifdef ADB_DEBUG
197 1.1 tsubai if (adb_debug)
198 1.9 tsubai printf("ams: returned %d from SetADBInfo\n", error);
199 1.1 tsubai #endif
200 1.1 tsubai
201 1.2 tsubai a.accessops = &ams_accessops;
202 1.2 tsubai a.accesscookie = sc;
203 1.2 tsubai sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
204 1.1 tsubai }
205 1.1 tsubai
206 1.1 tsubai
207 1.1 tsubai /*
208 1.1 tsubai * Initialize extended mouse support -- probes devices as described
209 1.1 tsubai * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
210 1.1 tsubai *
211 1.1 tsubai * Extended Mouse Protocol is documented in TechNote HW1:
212 1.1 tsubai * "ADB - The Untold Story: Space Aliens Ate My Mouse"
213 1.1 tsubai *
214 1.1 tsubai * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
215 1.1 tsubai * Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
216 1.1 tsubai */
217 1.1 tsubai void
218 1.1 tsubai ems_init(sc)
219 1.5 tsubai struct ams_softc *sc;
220 1.1 tsubai {
221 1.9 tsubai int adbaddr;
222 1.1 tsubai short cmd;
223 1.1 tsubai u_char buffer[9];
224 1.1 tsubai
225 1.1 tsubai adbaddr = sc->adbaddr;
226 1.1 tsubai if (sc->origaddr != ADBADDR_MS)
227 1.1 tsubai return;
228 1.5 tsubai if (sc->handler_id == ADBMS_USPEED ||
229 1.5 tsubai sc->handler_id == ADBMS_UCONTOUR) {
230 1.5 tsubai /* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
231 1.9 tsubai cmd = ADBLISTEN(adbaddr, 1);
232 1.1 tsubai
233 1.1 tsubai /*
234 1.5 tsubai * To setup the MicroSpeed or the Contour, it appears
235 1.5 tsubai * that we can send the following command to the mouse
236 1.5 tsubai * and then expect data back in the form:
237 1.1 tsubai * buffer[0] = 4 (bytes)
238 1.1 tsubai * buffer[1], buffer[2] as std. mouse
239 1.1 tsubai * buffer[3] = buffer[4] = 0xff when no buttons
240 1.1 tsubai * are down. When button N down, bit N is clear.
241 1.1 tsubai * buffer[4]'s locking mask enables a
242 1.1 tsubai * click to toggle the button down state--sort of
243 1.1 tsubai * like the "Easy Access" shift/control/etc. keys.
244 1.6 tsubai * buffer[3]'s alternative speed mask enables using
245 1.1 tsubai * different speed when the corr. button is down
246 1.1 tsubai */
247 1.1 tsubai buffer[0] = 4;
248 1.1 tsubai buffer[1] = 0x00; /* Alternative speed */
249 1.1 tsubai buffer[2] = 0x00; /* speed = maximum */
250 1.1 tsubai buffer[3] = 0x10; /* enable extended protocol,
251 1.1 tsubai * lower bits = alt. speed mask
252 1.1 tsubai * = 0000b
253 1.1 tsubai */
254 1.1 tsubai buffer[4] = 0x07; /* Locking mask = 0000b,
255 1.1 tsubai * enable buttons = 0111b
256 1.1 tsubai */
257 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
258 1.1 tsubai
259 1.1 tsubai sc->sc_buttons = 3;
260 1.1 tsubai sc->sc_res = 200;
261 1.1 tsubai return;
262 1.10 tsubai }
263 1.10 tsubai if (sc->handler_id == ADBMS_TURBO) {
264 1.10 tsubai /* Found Kensington Turbo Mouse */
265 1.10 tsubai static u_char data1[] =
266 1.10 tsubai { 8, 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
267 1.10 tsubai static u_char data2[] =
268 1.10 tsubai { 8, 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
269 1.10 tsubai
270 1.10 tsubai buffer[0] = 0;
271 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, ADBFLUSH(adbaddr));
272 1.10 tsubai
273 1.17 nathanw adb_op_sync((Ptr)data1, NULL, (Ptr)0, ADBLISTEN(adbaddr, 2));
274 1.10 tsubai
275 1.10 tsubai buffer[0] = 0;
276 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, ADBFLUSH(adbaddr));
277 1.10 tsubai
278 1.17 nathanw adb_op_sync((Ptr)data2, NULL, (Ptr)0, ADBLISTEN(adbaddr, 2));
279 1.11 tsubai return;
280 1.1 tsubai }
281 1.6 tsubai if ((sc->handler_id == ADBMS_100DPI) ||
282 1.1 tsubai (sc->handler_id == ADBMS_200DPI)) {
283 1.1 tsubai /* found a mouse */
284 1.9 tsubai cmd = ADBTALK(adbaddr, 3);
285 1.17 nathanw if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
286 1.1 tsubai #ifdef ADB_DEBUG
287 1.1 tsubai if (adb_debug)
288 1.9 tsubai printf("adb: ems_init timed out\n");
289 1.1 tsubai #endif
290 1.1 tsubai return;
291 1.1 tsubai }
292 1.1 tsubai
293 1.1 tsubai /* Attempt to initialize Extended Mouse Protocol */
294 1.9 tsubai buffer[2] = 4; /* make handler ID 4 */
295 1.9 tsubai cmd = ADBLISTEN(adbaddr, 3);
296 1.17 nathanw if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
297 1.9 tsubai #ifdef ADB_DEBUG
298 1.9 tsubai if (adb_debug)
299 1.9 tsubai printf("adb: ems_init timed out\n");
300 1.9 tsubai #endif
301 1.9 tsubai return;
302 1.9 tsubai }
303 1.1 tsubai
304 1.6 tsubai /*
305 1.1 tsubai * Check to see if successful, if not
306 1.1 tsubai * try to initialize it as other types
307 1.1 tsubai */
308 1.9 tsubai cmd = ADBTALK(adbaddr, 3);
309 1.17 nathanw if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0 &&
310 1.9 tsubai buffer[2] == ADBMS_EXTENDED) {
311 1.1 tsubai sc->handler_id = ADBMS_EXTENDED;
312 1.9 tsubai cmd = ADBTALK(adbaddr, 1);
313 1.17 nathanw if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
314 1.9 tsubai #ifdef ADB_DEBUG
315 1.9 tsubai if (adb_debug)
316 1.9 tsubai printf("adb: ems_init timed out\n");
317 1.9 tsubai #endif
318 1.9 tsubai } else if (buffer[0] == 8) {
319 1.1 tsubai /* we have a true EMP device */
320 1.19 macallan #ifdef ADB_PRINT_EMP
321 1.19 macallan printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n",
322 1.19 macallan buffer[1], buffer[2], buffer[3], buffer[4],
323 1.19 macallan buffer[5], buffer[6], buffer[7], buffer[8]);
324 1.19 macallan #endif
325 1.1 tsubai sc->sc_class = buffer[7];
326 1.1 tsubai sc->sc_buttons = buffer[8];
327 1.1 tsubai sc->sc_res = (int)*(short *)&buffer[5];
328 1.12 wiz memcpy(sc->sc_devid, &(buffer[1]), 4);
329 1.6 tsubai } else if (buffer[1] == 0x9a &&
330 1.1 tsubai ((buffer[2] == 0x20) || (buffer[2] == 0x21))) {
331 1.6 tsubai /*
332 1.1 tsubai * Set up non-EMP Mouseman/Trackman to put
333 1.1 tsubai * button bits in 3rd byte instead of sending
334 1.1 tsubai * via pseudo keyboard device.
335 1.1 tsubai */
336 1.9 tsubai cmd = ADBLISTEN(adbaddr, 1);
337 1.1 tsubai buffer[0]=2;
338 1.1 tsubai buffer[1]=0x00;
339 1.1 tsubai buffer[2]=0x81;
340 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
341 1.9 tsubai
342 1.9 tsubai cmd = ADBLISTEN(adbaddr, 1);
343 1.1 tsubai buffer[0]=2;
344 1.1 tsubai buffer[1]=0x01;
345 1.1 tsubai buffer[2]=0x81;
346 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
347 1.9 tsubai
348 1.9 tsubai cmd = ADBLISTEN(adbaddr, 1);
349 1.1 tsubai buffer[0]=2;
350 1.1 tsubai buffer[1]=0x02;
351 1.1 tsubai buffer[2]=0x81;
352 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
353 1.9 tsubai
354 1.9 tsubai cmd = ADBLISTEN(adbaddr, 1);
355 1.1 tsubai buffer[0]=2;
356 1.1 tsubai buffer[1]=0x03;
357 1.1 tsubai buffer[2]=0x38;
358 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
359 1.9 tsubai
360 1.1 tsubai sc->sc_buttons = 3;
361 1.1 tsubai sc->sc_res = 400;
362 1.1 tsubai if (buffer[2] == 0x21)
363 1.1 tsubai sc->sc_class = MSCLASS_TRACKBALL;
364 1.1 tsubai else
365 1.1 tsubai sc->sc_class = MSCLASS_MOUSE;
366 1.6 tsubai } else
367 1.1 tsubai /* unknown device? */;
368 1.1 tsubai } else {
369 1.1 tsubai /* Attempt to initialize as an A3 mouse */
370 1.1 tsubai buffer[2] = 0x03; /* make handler ID 3 */
371 1.9 tsubai cmd = ADBLISTEN(adbaddr, 3);
372 1.17 nathanw if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
373 1.9 tsubai #ifdef ADB_DEBUG
374 1.9 tsubai if (adb_debug)
375 1.9 tsubai printf("adb: ems_init timed out\n");
376 1.9 tsubai #endif
377 1.9 tsubai return;
378 1.9 tsubai }
379 1.6 tsubai
380 1.6 tsubai /*
381 1.1 tsubai * Check to see if successful, if not
382 1.1 tsubai * try to initialize it as other types
383 1.1 tsubai */
384 1.9 tsubai cmd = ADBTALK(adbaddr, 3);
385 1.17 nathanw if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0
386 1.9 tsubai && buffer[2] == ADBMS_MSA3) {
387 1.1 tsubai sc->handler_id = ADBMS_MSA3;
388 1.1 tsubai /* Initialize as above */
389 1.9 tsubai cmd = ADBLISTEN(adbaddr, 2);
390 1.1 tsubai /* listen 2 */
391 1.1 tsubai buffer[0] = 3;
392 1.1 tsubai buffer[1] = 0x00;
393 1.1 tsubai /* Irrelevant, buffer has 0x77 */
394 1.1 tsubai buffer[2] = 0x07;
395 1.6 tsubai /*
396 1.1 tsubai * enable 3 button mode = 0111b,
397 1.1 tsubai * speed = normal
398 1.1 tsubai */
399 1.17 nathanw adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
400 1.1 tsubai sc->sc_buttons = 3;
401 1.1 tsubai sc->sc_res = 300;
402 1.1 tsubai } else {
403 1.1 tsubai /* No special support for this mouse */
404 1.1 tsubai }
405 1.6 tsubai }
406 1.1 tsubai }
407 1.1 tsubai }
408 1.1 tsubai
409 1.1 tsubai /*
410 1.1 tsubai * Handle putting the mouse data received from the ADB into
411 1.1 tsubai * an ADB event record.
412 1.1 tsubai */
413 1.6 tsubai void
414 1.1 tsubai ms_adbcomplete(buffer, data_area, adb_command)
415 1.1 tsubai caddr_t buffer;
416 1.1 tsubai caddr_t data_area;
417 1.1 tsubai int adb_command;
418 1.1 tsubai {
419 1.1 tsubai adb_event_t event;
420 1.9 tsubai struct ams_softc *sc;
421 1.1 tsubai int adbaddr;
422 1.1 tsubai #ifdef ADB_DEBUG
423 1.1 tsubai int i;
424 1.1 tsubai
425 1.1 tsubai if (adb_debug)
426 1.1 tsubai printf("adb: transaction completion\n");
427 1.1 tsubai #endif
428 1.1 tsubai
429 1.9 tsubai adbaddr = ADB_CMDADDR(adb_command);
430 1.9 tsubai sc = (struct ams_softc *)data_area;
431 1.1 tsubai
432 1.9 tsubai if ((sc->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) {
433 1.1 tsubai /* massage the data to look like EMP data */
434 1.1 tsubai if ((buffer[3] & 0x04) == 0x04)
435 1.1 tsubai buffer[1] &= 0x7f;
436 1.1 tsubai else
437 1.1 tsubai buffer[1] |= 0x80;
438 1.1 tsubai if ((buffer[3] & 0x02) == 0x02)
439 1.1 tsubai buffer[2] &= 0x7f;
440 1.1 tsubai else
441 1.1 tsubai buffer[2] |= 0x80;
442 1.1 tsubai if ((buffer[3] & 0x01) == 0x01)
443 1.1 tsubai buffer[3] = 0x00;
444 1.1 tsubai else
445 1.1 tsubai buffer[3] = 0x80;
446 1.1 tsubai }
447 1.1 tsubai
448 1.1 tsubai event.addr = adbaddr;
449 1.9 tsubai event.hand_id = sc->handler_id;
450 1.9 tsubai event.def_addr = sc->origaddr;
451 1.1 tsubai event.byte_count = buffer[0];
452 1.1 tsubai memcpy(event.bytes, buffer + 1, event.byte_count);
453 1.1 tsubai
454 1.1 tsubai #ifdef ADB_DEBUG
455 1.1 tsubai if (adb_debug) {
456 1.9 tsubai printf("ams: from %d at %d (org %d) %d:", event.addr,
457 1.1 tsubai event.hand_id, event.def_addr, buffer[0]);
458 1.1 tsubai for (i = 1; i <= buffer[0]; i++)
459 1.1 tsubai printf(" %x", buffer[i]);
460 1.1 tsubai printf("\n");
461 1.1 tsubai }
462 1.1 tsubai #endif
463 1.1 tsubai
464 1.1 tsubai microtime(&event.timestamp);
465 1.1 tsubai
466 1.9 tsubai ms_processevent(&event, sc);
467 1.1 tsubai }
468 1.1 tsubai
469 1.1 tsubai /*
470 1.1 tsubai * Given a mouse ADB event, record the button settings, calculate the
471 1.1 tsubai * x- and y-axis motion, and handoff the event to the appropriate subsystem.
472 1.1 tsubai */
473 1.1 tsubai static void
474 1.9 tsubai ms_processevent(event, sc)
475 1.1 tsubai adb_event_t *event;
476 1.9 tsubai struct ams_softc *sc;
477 1.1 tsubai {
478 1.1 tsubai adb_event_t new_event;
479 1.19 macallan int i, button_bit, max_byte, mask, buttons, dx, dy;
480 1.1 tsubai
481 1.1 tsubai new_event = *event;
482 1.1 tsubai buttons = 0;
483 1.1 tsubai
484 1.1 tsubai /*
485 1.1 tsubai * This should handle both plain ol' Apple mice and mice
486 1.1 tsubai * that claim to support the Extended Apple Mouse Protocol.
487 1.1 tsubai */
488 1.1 tsubai max_byte = event->byte_count;
489 1.1 tsubai button_bit = 1;
490 1.1 tsubai switch (event->hand_id) {
491 1.1 tsubai case ADBMS_USPEED:
492 1.5 tsubai case ADBMS_UCONTOUR:
493 1.5 tsubai /* MicroSpeed mouse and Contour mouse */
494 1.1 tsubai if (max_byte == 4)
495 1.1 tsubai buttons = (~event->bytes[2]) & 0xff;
496 1.1 tsubai else
497 1.1 tsubai buttons = (event->bytes[0] & 0x80) ? 0 : 1;
498 1.1 tsubai break;
499 1.1 tsubai case ADBMS_MSA3:
500 1.1 tsubai /* Mouse Systems A3 mouse */
501 1.1 tsubai if (max_byte == 3)
502 1.1 tsubai buttons = (~event->bytes[2]) & 0x07;
503 1.1 tsubai else
504 1.1 tsubai buttons = (event->bytes[0] & 0x80) ? 0 : 1;
505 1.1 tsubai break;
506 1.19 macallan default:
507 1.1 tsubai /* Classic Mouse Protocol (up to 2 buttons) */
508 1.1 tsubai for (i = 0; i < 2; i++, button_bit <<= 1)
509 1.1 tsubai /* 0 when button down */
510 1.1 tsubai if (!(event->bytes[i] & 0x80))
511 1.1 tsubai buttons |= button_bit;
512 1.1 tsubai else
513 1.1 tsubai buttons &= ~button_bit;
514 1.1 tsubai /* Extended Protocol (up to 6 more buttons) */
515 1.1 tsubai for (mask = 0x80; i < max_byte;
516 1.1 tsubai i += (mask == 0x80), button_bit <<= 1) {
517 1.1 tsubai /* 0 when button down */
518 1.1 tsubai if (!(event->bytes[i] & mask))
519 1.1 tsubai buttons |= button_bit;
520 1.1 tsubai else
521 1.1 tsubai buttons &= ~button_bit;
522 1.1 tsubai mask = ((mask >> 4) & 0xf)
523 1.1 tsubai | ((mask & 0xf) << 4);
524 1.19 macallan }
525 1.1 tsubai break;
526 1.1 tsubai }
527 1.19 macallan
528 1.19 macallan dx = ((int)(event->bytes[1] & 0x3f)) -
529 1.1 tsubai ((event->bytes[1] & 0x40) ? 64 : 0);
530 1.19 macallan dy = ((int) (event->bytes[0] & 0x3f)) -
531 1.1 tsubai ((event->bytes[0] & 0x40) ? 64 : 0);
532 1.1 tsubai
533 1.19 macallan if (sc->sc_class == MSCLASS_TRACKPAD) {
534 1.19 macallan
535 1.19 macallan if (sc->sc_down) {
536 1.19 macallan /* finger is down - collect motion data */
537 1.19 macallan sc->sc_x += dx;
538 1.19 macallan sc->sc_y += dy;
539 1.19 macallan }
540 1.19 macallan switch (sc->sc_buttons) {
541 1.19 macallan case 2:
542 1.19 macallan ams_mangle_2(sc, buttons);
543 1.19 macallan break;
544 1.19 macallan case 4:
545 1.19 macallan ams_mangle_4(sc, buttons);
546 1.19 macallan break;
547 1.19 macallan }
548 1.19 macallan /* filter the pseudo-buttons out */
549 1.19 macallan buttons &= 1;
550 1.19 macallan }
551 1.19 macallan
552 1.19 macallan new_event.u.m.buttons = sc->sc_mb | buttons;
553 1.19 macallan new_event.u.m.dx = dx;
554 1.19 macallan new_event.u.m.dy = dy;
555 1.19 macallan
556 1.9 tsubai if (sc->sc_wsmousedev)
557 1.9 tsubai wsmouse_input(sc->sc_wsmousedev, new_event.u.m.buttons,
558 1.8 takemura new_event.u.m.dx, -new_event.u.m.dy, 0,
559 1.8 takemura WSMOUSE_INPUT_DELTA);
560 1.2 tsubai #if NAED > 0
561 1.1 tsubai aed_input(&new_event);
562 1.2 tsubai #endif
563 1.2 tsubai }
564 1.2 tsubai
565 1.19 macallan static void
566 1.19 macallan ams_mangle_2(struct ams_softc *sc, int buttons)
567 1.19 macallan {
568 1.19 macallan
569 1.19 macallan if (buttons & 4) {
570 1.19 macallan /* finger down on pad */
571 1.19 macallan if (sc->sc_down == 0) {
572 1.19 macallan sc->sc_down = 1;
573 1.19 macallan sc->sc_x = 0;
574 1.19 macallan sc->sc_y = 0;
575 1.19 macallan }
576 1.19 macallan }
577 1.19 macallan if (buttons & 8) {
578 1.19 macallan /* finger up */
579 1.19 macallan if (sc->sc_down) {
580 1.19 macallan if (((sc->sc_x * sc->sc_x +
581 1.19 macallan sc->sc_y * sc->sc_y) < 20) &&
582 1.19 macallan (sc->sc_wsmousedev)) {
583 1.19 macallan /*
584 1.19 macallan * if there wasn't much movement between
585 1.19 macallan * finger down and up again we assume
586 1.19 macallan * someone tapped the pad and we just
587 1.19 macallan * send a mouse button event
588 1.19 macallan */
589 1.19 macallan wsmouse_input(sc->sc_wsmousedev,
590 1.19 macallan 1, 0, 0, 0, WSMOUSE_INPUT_DELTA);
591 1.19 macallan }
592 1.19 macallan sc->sc_down = 0;
593 1.19 macallan }
594 1.19 macallan }
595 1.19 macallan }
596 1.19 macallan
597 1.19 macallan static void
598 1.19 macallan ams_mangle_4(struct ams_softc *sc, int buttons)
599 1.19 macallan {
600 1.19 macallan
601 1.19 macallan if (buttons & 0x20) {
602 1.19 macallan /* finger down on pad */
603 1.19 macallan if (sc->sc_down == 0) {
604 1.19 macallan sc->sc_down = 1;
605 1.19 macallan sc->sc_x = 0;
606 1.19 macallan sc->sc_y = 0;
607 1.19 macallan }
608 1.19 macallan }
609 1.19 macallan if ((buttons & 0x20) == 0) {
610 1.19 macallan /* finger up */
611 1.19 macallan if (sc->sc_down) {
612 1.19 macallan if (((sc->sc_x * sc->sc_x +
613 1.19 macallan sc->sc_y * sc->sc_y) < 20) &&
614 1.19 macallan (sc->sc_wsmousedev)) {
615 1.19 macallan /*
616 1.19 macallan * if there wasn't much movement between
617 1.19 macallan * finger down and up again we assume
618 1.19 macallan * someone tapped the pad and we just
619 1.19 macallan * send a mouse button event
620 1.19 macallan */
621 1.19 macallan wsmouse_input(sc->sc_wsmousedev,
622 1.19 macallan 1, 0, 0, 0, WSMOUSE_INPUT_DELTA);
623 1.19 macallan }
624 1.19 macallan sc->sc_down = 0;
625 1.19 macallan }
626 1.19 macallan }
627 1.19 macallan }
628 1.19 macallan
629 1.2 tsubai int
630 1.2 tsubai ams_enable(v)
631 1.2 tsubai void *v;
632 1.2 tsubai {
633 1.2 tsubai return 0;
634 1.2 tsubai }
635 1.2 tsubai
636 1.2 tsubai int
637 1.18 christos ams_ioctl(v, cmd, data, flag, l)
638 1.2 tsubai void *v;
639 1.2 tsubai u_long cmd;
640 1.2 tsubai caddr_t data;
641 1.2 tsubai int flag;
642 1.18 christos struct lwp *l;
643 1.2 tsubai {
644 1.13 atatat return EPASSTHROUGH;
645 1.2 tsubai }
646 1.2 tsubai
647 1.2 tsubai void
648 1.2 tsubai ams_disable(v)
649 1.2 tsubai void *v;
650 1.2 tsubai {
651 1.1 tsubai }
652 1.19 macallan
653 1.19 macallan static void
654 1.19 macallan init_trackpad(struct ams_softc *sc)
655 1.19 macallan {
656 1.19 macallan int cmd, res;
657 1.19 macallan u_char buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
658 1.19 macallan u_char b2[10];
659 1.19 macallan u_char b3[9] = {8, 0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
660 1.19 macallan
661 1.19 macallan cmd = ADBTALK(sc->adbaddr, 1);
662 1.19 macallan res = adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
663 1.19 macallan
664 1.19 macallan if (buffer[0] != 8)
665 1.19 macallan return;
666 1.19 macallan
667 1.19 macallan /* now whack the pad */
668 1.19 macallan cmd = ADBLISTEN(sc->adbaddr, 1);
669 1.19 macallan memcpy(b2, buffer, 10);
670 1.19 macallan b2[7] = 0x0d;
671 1.19 macallan adb_op_sync((Ptr)b2, NULL, (Ptr)0, cmd);
672 1.19 macallan
673 1.19 macallan cmd = ADBLISTEN(sc->adbaddr, 2);
674 1.19 macallan adb_op_sync((Ptr)b3, NULL, (Ptr)0, cmd);
675 1.19 macallan
676 1.19 macallan cmd = ADBLISTEN(sc->adbaddr, 1);
677 1.19 macallan b2[7] = 0x03;
678 1.19 macallan adb_op_sync((Ptr)b2, NULL, (Ptr)0, cmd);
679 1.19 macallan
680 1.19 macallan buffer[0] = 0;
681 1.19 macallan cmd = ADBFLUSH(sc->adbaddr);
682 1.19 macallan adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
683 1.19 macallan }
684 1.19 macallan
685