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