1 /* $NetBSD: ams.c,v 1.24 2007/03/04 06:00:10 christos Exp $ */
4 * Copyright (C) 1998 Colin Wood
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ams.c,v 1.24 2007/03/04 06:00:10 christos Exp $");
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/fcntl.h>
40 #include <sys/select.h>
42 #include <sys/signalvar.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/sysctl.h>
47 #include <machine/autoconf.h>
49 #include <dev/wscons/wsconsio.h>
50 #include <dev/wscons/wsmousevar.h>
52 #include <macppc/dev/adbvar.h>
53 #include <macppc/dev/aedvar.h>
54 #include <macppc/dev/amsvar.h>
59 * Function declarations.
61 static int amsmatch(struct device
*, struct cfdata
*, void *);
62 static void amsattach(struct device
*, struct device
*, void *);
63 static void ems_init(struct ams_softc
*);
64 static void ms_processevent(adb_event_t
*event
, struct ams_softc
*);
65 static void init_trackpad(struct ams_softc
*);
67 /* Driver definition. */
68 CFATTACH_DECL(ams
, sizeof(struct ams_softc
),
69 amsmatch
, amsattach
, NULL
, NULL
);
71 int ams_enable(void *);
72 int ams_ioctl(void *, u_long
, void *, int, struct lwp
*);
73 void ams_disable(void *);
76 * handle tapping the trackpad
77 * different pads report different button counts and use slightly different
80 static void ams_mangle_2(struct ams_softc
*, int);
81 static void ams_mangle_4(struct ams_softc
*, int);
82 static int sysctl_ams_tap(SYSCTLFN_ARGS
);
84 const struct wsmouse_accessops ams_accessops
= {
91 amsmatch(struct device
*parent
, struct cfdata
*cf
, void *aux
)
93 struct adb_attach_args
*aa_args
= aux
;
95 if (aa_args
->origaddr
== ADBADDR_MS
)
102 amsattach(struct device
*parent
, struct device
*self
, void *aux
)
104 ADBSetInfoBlock adbinfo
;
105 struct ams_softc
*sc
= (struct ams_softc
*)self
;
106 struct adb_attach_args
*aa_args
= aux
;
108 struct wsmousedev_attach_args a
;
110 sc
->origaddr
= aa_args
->origaddr
;
111 sc
->adbaddr
= aa_args
->adbaddr
;
112 sc
->handler_id
= aa_args
->handler_id
;
114 sc
->sc_class
= MSCLASS_MOUSE
;
120 adbinfo
.siServiceRtPtr
= (Ptr
)ms_adbcomplete
;
121 adbinfo
.siDataAreaAddr
= (void *)sc
;
125 /* print out the type of mouse we have */
126 switch (sc
->handler_id
) {
128 printf("%d-button, %d dpi mouse\n", sc
->sc_buttons
,
133 printf("%d-button, %d dpi mouse\n", sc
->sc_buttons
,
137 printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
138 sc
->sc_buttons
, (int)(sc
->sc_res
));
141 printf("MicroSpeed mouse, default parameters\n");
144 printf("Contour mouse, default parameters\n");
147 printf("Kensington Turbo Mouse\n");
150 if (sc
->sc_devid
[0] == '\0') {
152 switch (sc
->sc_class
) {
154 printf("MouseMan (non-EMP) mouse");
156 case MSCLASS_TRACKBALL
:
157 printf("TrackMan (non-EMP) trackball");
160 printf("non-EMP relative positioning device");
166 switch (sc
->sc_class
) {
173 case MSCLASS_TRACKBALL
:
176 case MSCLASS_TRACKPAD
:
181 printf("unknown device");
184 printf(" <%s> %d-button, %d dpi\n", sc
->sc_devid
,
185 sc
->sc_buttons
, (int)(sc
->sc_res
));
189 printf("relative positioning device (mouse?) (%d)\n",
193 error
= SetADBInfo(&adbinfo
, sc
->adbaddr
);
196 printf("ams: returned %d from SetADBInfo\n", error
);
199 a
.accessops
= &ams_accessops
;
201 sc
->sc_wsmousedev
= config_found(self
, &a
, wsmousedevprint
);
206 * Initialize extended mouse support -- probes devices as described
207 * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
209 * Extended Mouse Protocol is documented in TechNote HW1:
210 * "ADB - The Untold Story: Space Aliens Ate My Mouse"
212 * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
213 * Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
216 ems_init(struct ams_softc
*sc
)
222 adbaddr
= sc
->adbaddr
;
223 if (sc
->origaddr
!= ADBADDR_MS
)
225 if (sc
->handler_id
== ADBMS_USPEED
||
226 sc
->handler_id
== ADBMS_UCONTOUR
) {
227 /* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
228 cmd
= ADBLISTEN(adbaddr
, 1);
231 * To setup the MicroSpeed or the Contour, it appears
232 * that we can send the following command to the mouse
233 * and then expect data back in the form:
234 * buffer[0] = 4 (bytes)
235 * buffer[1], buffer[2] as std. mouse
236 * buffer[3] = buffer[4] = 0xff when no buttons
237 * are down. When button N down, bit N is clear.
238 * buffer[4]'s locking mask enables a
239 * click to toggle the button down state--sort of
240 * like the "Easy Access" shift/control/etc. keys.
241 * buffer[3]'s alternative speed mask enables using
242 * different speed when the corr. button is down
245 buffer
[1] = 0x00; /* Alternative speed */
246 buffer
[2] = 0x00; /* speed = maximum */
247 buffer
[3] = 0x10; /* enable extended protocol,
248 * lower bits = alt. speed mask
251 buffer
[4] = 0x07; /* Locking mask = 0000b,
252 * enable buttons = 0111b
254 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
260 if (sc
->handler_id
== ADBMS_TURBO
) {
261 /* Found Kensington Turbo Mouse */
262 static u_char data1
[] =
263 { 8, 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
264 static u_char data2
[] =
265 { 8, 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
268 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, ADBFLUSH(adbaddr
));
270 adb_op_sync((Ptr
)data1
, NULL
, (Ptr
)0, ADBLISTEN(adbaddr
, 2));
273 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, ADBFLUSH(adbaddr
));
275 adb_op_sync((Ptr
)data2
, NULL
, (Ptr
)0, ADBLISTEN(adbaddr
, 2));
278 if ((sc
->handler_id
== ADBMS_100DPI
) ||
279 (sc
->handler_id
== ADBMS_200DPI
)) {
281 cmd
= ADBTALK(adbaddr
, 3);
282 if (adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
)) {
285 printf("adb: ems_init timed out\n");
290 /* Attempt to initialize Extended Mouse Protocol */
291 buffer
[2] = 4; /* make handler ID 4 */
292 cmd
= ADBLISTEN(adbaddr
, 3);
293 if (adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
)) {
296 printf("adb: ems_init timed out\n");
302 * Check to see if successful, if not
303 * try to initialize it as other types
305 cmd
= ADBTALK(adbaddr
, 3);
306 if (adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
) == 0 &&
307 buffer
[2] == ADBMS_EXTENDED
) {
308 sc
->handler_id
= ADBMS_EXTENDED
;
309 cmd
= ADBTALK(adbaddr
, 1);
310 if (adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
)) {
313 printf("adb: ems_init timed out\n");
315 } else if (buffer
[0] == 8) {
316 /* we have a true EMP device */
318 printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n",
319 buffer
[1], buffer
[2], buffer
[3], buffer
[4],
320 buffer
[5], buffer
[6], buffer
[7], buffer
[8]);
322 sc
->sc_class
= buffer
[7];
323 sc
->sc_buttons
= buffer
[8];
324 sc
->sc_res
= (int)*(short *)&buffer
[5];
325 memcpy(sc
->sc_devid
, &(buffer
[1]), 4);
326 } else if (buffer
[1] == 0x9a &&
327 ((buffer
[2] == 0x20) || (buffer
[2] == 0x21))) {
329 * Set up non-EMP Mouseman/Trackman to put
330 * button bits in 3rd byte instead of sending
331 * via pseudo keyboard device.
333 cmd
= ADBLISTEN(adbaddr
, 1);
337 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
339 cmd
= ADBLISTEN(adbaddr
, 1);
343 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
345 cmd
= ADBLISTEN(adbaddr
, 1);
349 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
351 cmd
= ADBLISTEN(adbaddr
, 1);
355 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
359 if (buffer
[2] == 0x21)
360 sc
->sc_class
= MSCLASS_TRACKBALL
;
362 sc
->sc_class
= MSCLASS_MOUSE
;
364 /* unknown device? */;
366 /* Attempt to initialize as an A3 mouse */
367 buffer
[2] = 0x03; /* make handler ID 3 */
368 cmd
= ADBLISTEN(adbaddr
, 3);
369 if (adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
)) {
372 printf("adb: ems_init timed out\n");
378 * Check to see if successful, if not
379 * try to initialize it as other types
381 cmd
= ADBTALK(adbaddr
, 3);
382 if (adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
) == 0
383 && buffer
[2] == ADBMS_MSA3
) {
384 sc
->handler_id
= ADBMS_MSA3
;
385 /* Initialize as above */
386 cmd
= ADBLISTEN(adbaddr
, 2);
390 /* Irrelevant, buffer has 0x77 */
393 * enable 3 button mode = 0111b,
396 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
400 /* No special support for this mouse */
407 * Handle putting the mouse data received from the ADB into
408 * an ADB event record.
411 ms_adbcomplete(uint8_t *buffer
, uint8_t *data_area
, int adb_command
)
414 struct ams_softc
*sc
;
420 printf("adb: transaction completion\n");
423 adbaddr
= ADB_CMDADDR(adb_command
);
424 sc
= (struct ams_softc
*)data_area
;
426 if ((sc
->handler_id
== ADBMS_EXTENDED
) && (sc
->sc_devid
[0] == 0)) {
427 /* massage the data to look like EMP data */
428 if ((buffer
[3] & 0x04) == 0x04)
432 if ((buffer
[3] & 0x02) == 0x02)
436 if ((buffer
[3] & 0x01) == 0x01)
442 event
.addr
= adbaddr
;
443 event
.hand_id
= sc
->handler_id
;
444 event
.def_addr
= sc
->origaddr
;
445 event
.byte_count
= buffer
[0];
446 memcpy(event
.bytes
, buffer
+ 1, event
.byte_count
);
450 printf("ams: from %d at %d (org %d) %d:", event
.addr
,
451 event
.hand_id
, event
.def_addr
, buffer
[0]);
452 for (i
= 1; i
<= buffer
[0]; i
++)
453 printf(" %x", buffer
[i
]);
458 microtime(&event
.timestamp
);
460 ms_processevent(&event
, sc
);
464 * Given a mouse ADB event, record the button settings, calculate the
465 * x- and y-axis motion, and handoff the event to the appropriate subsystem.
468 ms_processevent(adb_event_t
*event
, struct ams_softc
*sc
)
470 adb_event_t new_event
;
471 int i
, button_bit
, max_byte
, mask
, buttons
, dx
, dy
;
477 * This should handle both plain ol' Apple mice and mice
478 * that claim to support the Extended Apple Mouse Protocol.
480 max_byte
= event
->byte_count
;
482 switch (event
->hand_id
) {
485 /* MicroSpeed mouse and Contour mouse */
487 buttons
= (~event
->bytes
[2]) & 0xff;
489 buttons
= (event
->bytes
[0] & 0x80) ? 0 : 1;
492 /* Mouse Systems A3 mouse */
494 buttons
= (~event
->bytes
[2]) & 0x07;
496 buttons
= (event
->bytes
[0] & 0x80) ? 0 : 1;
499 /* Classic Mouse Protocol (up to 2 buttons) */
500 for (i
= 0; i
< 2; i
++, button_bit
<<= 1)
501 /* 0 when button down */
502 if (!(event
->bytes
[i
] & 0x80))
503 buttons
|= button_bit
;
505 buttons
&= ~button_bit
;
506 /* Extended Protocol (up to 6 more buttons) */
507 for (mask
= 0x80; i
< max_byte
;
508 i
+= (mask
== 0x80), button_bit
<<= 1) {
509 /* 0 when button down */
510 if (!(event
->bytes
[i
] & mask
))
511 buttons
|= button_bit
;
513 buttons
&= ~button_bit
;
514 mask
= ((mask
>> 4) & 0xf)
515 | ((mask
& 0xf) << 4);
520 dx
= ((int)(event
->bytes
[1] & 0x3f)) -
521 ((event
->bytes
[1] & 0x40) ? 64 : 0);
522 dy
= ((int) (event
->bytes
[0] & 0x3f)) -
523 ((event
->bytes
[0] & 0x40) ? 64 : 0);
525 if (sc
->sc_class
== MSCLASS_TRACKPAD
) {
526 if (sc
->sc_tapping
== 1) {
529 /* finger is down - collect motion data */
533 switch (sc
->sc_buttons
) {
535 ams_mangle_2(sc
, buttons
);
538 ams_mangle_4(sc
, buttons
);
542 /* filter the pseudo-buttons out */
546 new_event
.u
.m
.buttons
= sc
->sc_mb
| buttons
;
547 new_event
.u
.m
.dx
= dx
;
548 new_event
.u
.m
.dy
= dy
;
550 if (sc
->sc_wsmousedev
)
551 wsmouse_input(sc
->sc_wsmousedev
, new_event
.u
.m
.buttons
,
552 new_event
.u
.m
.dx
, -new_event
.u
.m
.dy
, 0, 0,
553 WSMOUSE_INPUT_DELTA
);
555 aed_input(&new_event
);
560 ams_mangle_2(struct ams_softc
*sc
, int buttons
)
564 /* finger down on pad */
565 if (sc
->sc_down
== 0) {
574 if (((sc
->sc_x
* sc
->sc_x
+
575 sc
->sc_y
* sc
->sc_y
) < 20) &&
576 (sc
->sc_wsmousedev
)) {
578 * if there wasn't much movement between
579 * finger down and up again we assume
580 * someone tapped the pad and we just
581 * send a mouse button event
583 wsmouse_input(sc
->sc_wsmousedev
,
584 1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA
);
592 ams_mangle_4(struct ams_softc
*sc
, int buttons
)
595 if (buttons
& 0x20) {
596 /* finger down on pad */
597 if (sc
->sc_down
== 0) {
603 if ((buttons
& 0x20) == 0) {
606 if (((sc
->sc_x
* sc
->sc_x
+
607 sc
->sc_y
* sc
->sc_y
) < 20) &&
608 (sc
->sc_wsmousedev
)) {
610 * if there wasn't much movement between
611 * finger down and up again we assume
612 * someone tapped the pad and we just
613 * send a mouse button event
615 wsmouse_input(sc
->sc_wsmousedev
,
616 1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA
);
630 ams_ioctl(void *v
, u_long cmd
, void *data
, int flag
, struct lwp
*l
)
641 init_trackpad(struct ams_softc
*sc
)
643 struct sysctlnode
*me
= NULL
, *node
= NULL
;
645 u_char buffer
[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
647 u_char b3
[9] = {8, 0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
649 cmd
= ADBTALK(sc
->adbaddr
, 1);
650 res
= adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
655 /* now whack the pad */
656 cmd
= ADBLISTEN(sc
->adbaddr
, 1);
657 memcpy(b2
, buffer
, 10);
659 adb_op_sync((Ptr
)b2
, NULL
, (Ptr
)0, cmd
);
661 cmd
= ADBLISTEN(sc
->adbaddr
, 2);
662 adb_op_sync((Ptr
)b3
, NULL
, (Ptr
)0, cmd
);
664 cmd
= ADBLISTEN(sc
->adbaddr
, 1);
666 adb_op_sync((Ptr
)b2
, NULL
, (Ptr
)0, cmd
);
669 cmd
= ADBFLUSH(sc
->adbaddr
);
670 adb_op_sync((Ptr
)buffer
, NULL
, (Ptr
)0, cmd
);
673 * setup a sysctl node to control wether tapping the pad should
674 * trigger mouse button events
679 ret
= sysctl_createv(NULL
, 0, NULL
, (const struct sysctlnode
**)&me
,
681 CTLTYPE_NODE
, sc
->sc_dev
.dv_xname
, NULL
,
683 CTL_MACHDEP
, CTL_CREATE
, CTL_EOL
);
685 ret
=sysctl_createv(NULL
, 0, NULL
, (const struct sysctlnode
**)&node
,
686 CTLFLAG_READWRITE
| CTLFLAG_OWNDESC
| CTLFLAG_IMMEDIATE
,
687 CTLTYPE_INT
, "tapping", "tapping the pad causes button events",
688 sysctl_ams_tap
, 1, NULL
, 0,
689 CTL_MACHDEP
, me
->sysctl_num
, CTL_CREATE
, CTL_EOL
);
691 node
->sysctl_data
= sc
;
696 sysctl_ams_tap(SYSCTLFN_ARGS
)
698 struct sysctlnode node
= *rnode
;
699 struct ams_softc
*sc
= node
.sysctl_data
;
701 node
.sysctl_idata
= sc
->sc_tapping
;
705 /* we're asked to write */
706 node
.sysctl_data
= &sc
->sc_tapping
;
707 if (sysctl_lookup(SYSCTLFN_CALL(&node
)) == 0) {
709 sc
->sc_tapping
= (node
.sysctl_idata
== 0) ? 0 : 1;
715 node
.sysctl_size
= 4;
716 return (sysctl_lookup(SYSCTLFN_CALL(&node
)));
722 SYSCTL_SETUP(sysctl_ams_setup
, "sysctl ams subtree setup")
725 sysctl_createv(NULL
, 0, NULL
, NULL
,
727 CTLTYPE_NODE
, "machdep", NULL
,
729 CTL_MACHDEP
, CTL_EOL
);