Home | History | Annotate | Line # | Download | only in adb
adb_ms.c revision 1.13
      1 /*	$NetBSD: adb_ms.c,v 1.13 2012/06/02 21:36:43 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1998	Colin Wood
      5  * Copyright (C) 2006, 2007 Michael Lorenz
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Colin Wood.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: adb_ms.c,v 1.13 2012/06/02 21:36:43 dsl Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/device.h>
     39 #include <sys/fcntl.h>
     40 #include <sys/poll.h>
     41 #include <sys/select.h>
     42 #include <sys/proc.h>
     43 #include <sys/signalvar.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/sysctl.h>
     47 
     48 #include <machine/autoconf.h>
     49 
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wscons/wsmousevar.h>
     52 
     53 #include <machine/adbsys.h>
     54 #include <dev/adb/adbvar.h>
     55 
     56 #include "adbdebug.h"
     57 
     58 #ifdef ADBMS_DEBUG
     59 #define DPRINTF printf
     60 #else
     61 #define DPRINTF while (0) printf
     62 #endif
     63 
     64 /*
     65  * State info, per mouse instance.
     66  */
     67 struct adbms_softc {
     68 	device_t	sc_dev;
     69 	struct adb_device *sc_adbdev;
     70 	struct adb_bus_accessops *sc_ops;
     71 
     72 	/* Extended Mouse Protocol info, faked for non-EMP mice */
     73 	u_int8_t	sc_class;	/* mouse class (mouse, trackball) */
     74 	u_int8_t	sc_buttons;	/* number of buttons */
     75 	u_int32_t	sc_res;		/* mouse resolution (dpi) */
     76 	char		sc_devid[5];	/* device indentifier */
     77 	uint8_t		sc_us;		/* cmd to watch for */
     78 	int		sc_mb;		/* current button state */
     79 	struct device	*sc_wsmousedev;
     80 	/* helpers for trackpads */
     81 	int		sc_down;
     82 	/*
     83 	 * trackpad protocol variant. Known so far:
     84 	 * 2 buttons - PowerBook 3400, single events on button 3 and 4 indicate
     85 	 *             finger down and up
     86 	 * 4 buttons - iBook G4, button 6 indicates finger down, button 4 is
     87 	 *             always down
     88 	 */
     89 	int		sc_x, sc_y;
     90 	int		sc_tapping;
     91 	/* buffers */
     92 	int		sc_poll;
     93 	int		sc_msg_len;
     94 	int		sc_event;
     95 	uint8_t		sc_buffer[16];
     96 };
     97 
     98 /* EMP device classes */
     99 #define MSCLASS_TABLET		0
    100 #define MSCLASS_MOUSE		1
    101 #define MSCLASS_TRACKBALL	2
    102 #define MSCLASS_TRACKPAD	3
    103 
    104 /*
    105  * Function declarations.
    106  */
    107 static int	adbms_match(device_t, cfdata_t, void *);
    108 static void	adbms_attach(device_t, device_t, void *);
    109 static void	ems_init(struct adbms_softc *);
    110 static void	init_trackpad(struct adbms_softc *);
    111 static void	adbms_init_mouse(struct adbms_softc *);
    112 static void	adbms_init_turbo(struct adbms_softc *);
    113 static void	adbms_init_uspeed(struct adbms_softc *);
    114 static void	adbms_process_event(struct adbms_softc *, int, uint8_t *);
    115 static int	adbms_send_sync(struct adbms_softc *, uint8_t, int, uint8_t *);
    116 
    117 /* Driver definition. */
    118 CFATTACH_DECL_NEW(adbms, sizeof(struct adbms_softc),
    119     adbms_match, adbms_attach, NULL, NULL);
    120 
    121 static int adbms_enable(void *);
    122 static int adbms_ioctl(void *, u_long, void *, int, struct lwp *);
    123 static void adbms_disable(void *);
    124 
    125 /*
    126  * handle tapping the trackpad
    127  * different pads report different button counts and use slightly different
    128  * protocols
    129  */
    130 static void adbms_mangle_2(struct adbms_softc *, int);
    131 static void adbms_mangle_4(struct adbms_softc *, int);
    132 static void adbms_handler(void *, int, uint8_t *);
    133 static int  adbms_wait(struct adbms_softc *, int);
    134 static int  sysctl_adbms_tap(SYSCTLFN_ARGS);
    135 
    136 const struct wsmouse_accessops adbms_accessops = {
    137 	adbms_enable,
    138 	adbms_ioctl,
    139 	adbms_disable,
    140 };
    141 
    142 static int
    143 adbms_match(device_t parent, cfdata_t cf, void *aux)
    144 {
    145 	struct adb_attach_args *aaa = aux;
    146 
    147 	if (aaa->dev->original_addr == ADBADDR_MS)
    148 		return 1;
    149 	else
    150 		return 0;
    151 }
    152 
    153 static void
    154 adbms_attach(device_t parent, device_t self, void *aux)
    155 {
    156 	struct adbms_softc *sc = device_private(self);
    157 	struct adb_attach_args *aaa = aux;
    158 	struct wsmousedev_attach_args a;
    159 
    160 	sc->sc_dev = self;
    161 	sc->sc_ops = aaa->ops;
    162 	sc->sc_adbdev = aaa->dev;
    163 	sc->sc_adbdev->cookie = sc;
    164 	sc->sc_adbdev->handler = adbms_handler;
    165 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
    166 	printf(" addr %d: ", sc->sc_adbdev->current_addr);
    167 
    168 	sc->sc_class = MSCLASS_MOUSE;
    169 	sc->sc_buttons = 1;
    170 	sc->sc_res = 100;
    171 	sc->sc_devid[0] = 0;
    172 	sc->sc_devid[4] = 0;
    173 	sc->sc_poll = 0;
    174 	sc->sc_msg_len = 0;
    175 	sc->sc_tapping = 1;
    176 
    177 	ems_init(sc);
    178 
    179 	/* print out the type of mouse we have */
    180 	switch (sc->sc_adbdev->handler_id) {
    181 	case ADBMS_100DPI:
    182 		printf("%d-button, %u dpi mouse\n", sc->sc_buttons,
    183 		    sc->sc_res);
    184 		break;
    185 	case ADBMS_200DPI:
    186 		sc->sc_res = 200;
    187 		printf("%d-button, %u dpi mouse\n", sc->sc_buttons,
    188 		    sc->sc_res);
    189 		break;
    190 	case ADBMS_MSA3:
    191 		printf("Mouse Systems A3 mouse, %d-button, %u dpi\n",
    192 		    sc->sc_buttons, sc->sc_res);
    193 		break;
    194 	case ADBMS_USPEED:
    195 		printf("MicroSpeed mouse, default parameters\n");
    196 		break;
    197 	case ADBMS_UCONTOUR:
    198 		printf("Contour mouse, default parameters\n");
    199 		break;
    200 	case ADBMS_TURBO:
    201 		printf("Kensington Turbo Mouse\n");
    202 		break;
    203 	case ADBMS_EXTENDED:
    204 		if (sc->sc_devid[0] == '\0') {
    205 			printf("Logitech ");
    206 			switch (sc->sc_class) {
    207 			case MSCLASS_MOUSE:
    208 				printf("MouseMan (non-EMP) mouse");
    209 				break;
    210 			case MSCLASS_TRACKBALL:
    211 				printf("TrackMan (non-EMP) trackball");
    212 				break;
    213 			default:
    214 				printf("non-EMP relative positioning device");
    215 				break;
    216 			}
    217 			printf("\n");
    218 		} else {
    219 			printf("EMP ");
    220 			switch (sc->sc_class) {
    221 			case MSCLASS_TABLET:
    222 				printf("tablet");
    223 				break;
    224 			case MSCLASS_MOUSE:
    225 				printf("mouse");
    226 				break;
    227 			case MSCLASS_TRACKBALL:
    228 				printf("trackball");
    229 				break;
    230 			case MSCLASS_TRACKPAD:
    231 				printf("trackpad");
    232 				init_trackpad(sc);
    233 				break;
    234 			default:
    235 				printf("unknown device");
    236 				break;
    237 			}
    238 			printf(" <%s> %d-button, %u dpi\n", sc->sc_devid,
    239 			    sc->sc_buttons, sc->sc_res);
    240 		}
    241 		break;
    242 	default:
    243 		printf("relative positioning device (mouse?) (%d)\n",
    244 			sc->sc_adbdev->handler_id);
    245 		break;
    246 	}
    247 
    248 	a.accessops = &adbms_accessops;
    249 	a.accesscookie = sc;
    250 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    251 }
    252 
    253 
    254 /*
    255  * Initialize extended mouse support -- probes devices as described
    256  * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
    257  *
    258  * Extended Mouse Protocol is documented in TechNote HW1:
    259  * 	"ADB - The Untold Story:  Space Aliens Ate My Mouse"
    260  *
    261  * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
    262  * 	     Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
    263  */
    264 void
    265 ems_init(struct adbms_softc *sc)
    266 {
    267 
    268 	DPRINTF("ems_init %d\n", sc->sc_adbdev->handler_id);
    269 
    270 	switch (sc->sc_adbdev->handler_id) {
    271 		case ADBMS_USPEED:
    272 		case ADBMS_UCONTOUR:
    273 			adbms_init_uspeed(sc);
    274 			return;
    275 		case ADBMS_TURBO:
    276 			adbms_init_turbo(sc);
    277 			return;
    278 		case ADBMS_100DPI:
    279 		case ADBMS_200DPI:
    280 			adbms_init_mouse(sc);
    281 	}
    282 }
    283 
    284 static void
    285 adbms_init_uspeed(struct adbms_softc *sc)
    286 {
    287 	uint8_t cmd, addr, buffer[4];
    288 
    289 	addr = sc->sc_adbdev->current_addr;
    290 
    291 	/* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
    292 	cmd = ADBLISTEN(addr, 1);
    293 
    294 	/*
    295 	 * To setup the MicroSpeed or the Contour, it appears
    296 	 * that we can send the following command to the mouse
    297 	 * and then expect data back in the form:
    298 	 *  buffer[0] = 4 (bytes)
    299 	 *  buffer[1], buffer[2] as std. mouse
    300 	 *  buffer[3] = buffer[4] = 0xff when no buttons
    301 	 *   are down.  When button N down, bit N is clear.
    302 	 * buffer[4]'s locking mask enables a
    303 	 * click to toggle the button down state--sort of
    304 	 * like the "Easy Access" shift/control/etc. keys.
    305 	 * buffer[3]'s alternative speed mask enables using
    306 	 * different speed when the corr. button is down
    307 	 */
    308 	buffer[0] = 0x00;	/* Alternative speed */
    309 	buffer[1] = 0x00;	/* speed = maximum */
    310 	buffer[2] = 0x10;	/* enable extended protocol,
    311 				 * lower bits = alt. speed mask
    312 				 *            = 0000b
    313 				 */
    314 	buffer[3] = 0x07;	/* Locking mask = 0000b,
    315 				 * enable buttons = 0111b
    316 				 */
    317 	adbms_send_sync(sc, cmd, 4, buffer);
    318 
    319 	sc->sc_buttons = 3;
    320 	sc->sc_res = 200;
    321 }
    322 
    323 static void
    324 adbms_init_turbo(struct adbms_softc *sc)
    325 {
    326 	uint8_t addr;
    327 
    328 	/* Found Kensington Turbo Mouse */
    329 	static u_char data1[] =
    330 		{ 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
    331 	static u_char data2[] =
    332 		{ 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
    333 
    334 	addr = sc->sc_adbdev->current_addr;
    335 
    336 	adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL);
    337 	adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data1);
    338 	adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL);
    339 	adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data2);
    340 }
    341 
    342 static void
    343 adbms_init_mouse(struct adbms_softc *sc)
    344 {
    345 	int len;
    346 	uint8_t cmd, addr, buffer[16];
    347 
    348 	addr = sc->sc_adbdev->current_addr;
    349 	/* found a mouse */
    350 	cmd = ADBTALK(addr, 3);
    351 	if (!adbms_send_sync(sc, cmd, 0, NULL)) {
    352 #ifdef ADBMS_DEBUG
    353 		printf("adb: ems_init timed out\n");
    354 #endif
    355 		return;
    356 	}
    357 
    358 	/* Attempt to initialize Extended Mouse Protocol */
    359 	len = sc->sc_msg_len;
    360 	memcpy(buffer, sc->sc_buffer, len);
    361 	DPRINTF("buffer: %02x %02x\n", buffer[0], buffer[1]);
    362 	buffer[1] = 4; /* make handler ID 4 */
    363 	cmd = ADBLISTEN(addr, 3);
    364 	if (!adbms_send_sync(sc, cmd, len, buffer)) {
    365 #ifdef ADBMS_DEBUG
    366 		printf("adb: ems_init timed out\n");
    367 #endif
    368 		return;
    369 	}
    370 
    371 	/*
    372 	 * Check to see if successful, if not
    373 	 * try to initialize it as other types
    374 	 */
    375 	cmd = ADBTALK(addr, 3);
    376 	if (!adbms_send_sync(sc, cmd, 0, NULL)) {
    377 		DPRINTF("timeout checking for EMP switch\n");
    378 		return;
    379 	}
    380 	DPRINTF("new handler ID: %02x\n", sc->sc_buffer[1]);
    381 	if (sc->sc_buffer[1] == ADBMS_EXTENDED) {
    382 		sc->sc_adbdev->handler_id = ADBMS_EXTENDED;
    383 		cmd = ADBTALK(addr, 1);
    384 		if(!adbms_send_sync(sc, cmd, 0, NULL)) {
    385 			DPRINTF("adb: ems_init timed out\n");
    386 			return;
    387 		}
    388 
    389 		len = sc->sc_msg_len;
    390 		memcpy(buffer, sc->sc_buffer, len);
    391 
    392 		if (sc->sc_msg_len == 8) {
    393 			uint16_t res;
    394 			/* we have a true EMP device */
    395 #ifdef ADB_PRINT_EMP
    396 
    397 			printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n",
    398 			    buffer[0], buffer[1], buffer[2], buffer[3],
    399 			    buffer[4], buffer[5], buffer[6], buffer[7]);
    400 #endif
    401 			memcpy(sc->sc_devid, &buffer[0], 4);
    402 			memcpy(&res, &buffer[4], sizeof(res));
    403 			sc->sc_res = res;
    404 			sc->sc_class = buffer[6];
    405 			sc->sc_buttons = buffer[7];
    406 		} else if (buffer[0] == 0x9a &&
    407 		    ((buffer[1] == 0x20) || (buffer[1] == 0x21))) {
    408 			/*
    409 			 * Set up non-EMP Mouseman/Trackman to put
    410 			 * button bits in 3rd byte instead of sending
    411 			 * via pseudo keyboard device.
    412 			 */
    413 			if (buffer[1] == 0x21)
    414 				sc->sc_class = MSCLASS_TRACKBALL;
    415 			else
    416 				sc->sc_class = MSCLASS_MOUSE;
    417 
    418 			cmd = ADBLISTEN(addr, 1);
    419 			buffer[0]=0x00;
    420 			buffer[1]=0x81;
    421 			adbms_send_sync(sc, cmd, 2, buffer);
    422 
    423 			cmd = ADBLISTEN(addr, 1);
    424 			buffer[0]=0x01;
    425 			buffer[1]=0x81;
    426 			adbms_send_sync(sc, cmd, 2, buffer);
    427 
    428 			cmd = ADBLISTEN(addr, 1);
    429 			buffer[0]=0x02;
    430 			buffer[1]=0x81;
    431 			adbms_send_sync(sc, cmd, 2, buffer);
    432 
    433 			cmd = ADBLISTEN(addr, 1);
    434 			buffer[0]=0x03;
    435 			buffer[1]=0x38;
    436 			adbms_send_sync(sc, cmd, 2, buffer);
    437 
    438 			sc->sc_buttons = 3;
    439 			sc->sc_res = 400;
    440 		}
    441 	} else {
    442 		/* Attempt to initialize as an A3 mouse */
    443 		buffer[1] = 0x03; /* make handler ID 3 */
    444 		cmd = ADBLISTEN(addr, 3);
    445 		if (!adbms_send_sync(sc, cmd, len, buffer)) {
    446 #ifdef ADBMS_DEBUG
    447 			printf("adb: ems_init timed out\n");
    448 #endif
    449 			return;
    450 		}
    451 
    452 		/*
    453 		 * Check to see if successful, if not
    454 		 * try to initialize it as other types
    455 		 */
    456 		cmd = ADBTALK(addr, 3);
    457 		if(adbms_send_sync(sc, cmd, 0, NULL)) {
    458 			len = sc->sc_msg_len;
    459 			memcpy(buffer, sc->sc_buffer, len);
    460 			if (buffer[1] == ADBMS_MSA3) {
    461 				sc->sc_adbdev->handler_id = ADBMS_MSA3;
    462 				/* Initialize as above */
    463 				cmd = ADBLISTEN(addr, 2);
    464 				/* listen 2 */
    465 				buffer[0] = 0x00;
    466 				/* Irrelevant, buffer has 0x77 */
    467 				buffer[2] = 0x07;
    468 				/*
    469 				 * enable 3 button mode = 0111b,
    470 				 * speed = normal
    471 				 */
    472 				adbms_send_sync(sc, cmd, 3, buffer);
    473 				sc->sc_buttons = 3;
    474 				sc->sc_res = 300;
    475 			}
    476 		}
    477 	}
    478 }
    479 
    480 static void
    481 adbms_handler(void *cookie, int len, uint8_t *data)
    482 {
    483 	struct adbms_softc *sc = cookie;
    484 
    485 #ifdef ADBMS_DEBUG
    486 	int i;
    487 	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
    488 	for (i = 0; i < len; i++) {
    489 		printf(" %02x", data[i]);
    490 	}
    491 	printf("\n");
    492 #endif
    493 	if (len >= 2) {
    494 		memcpy(sc->sc_buffer, &data[2], len - 2);
    495 		sc->sc_msg_len = len - 2;
    496 		if (data[1] == sc->sc_us) {
    497 			/* make sense of the mouse message */
    498 			adbms_process_event(sc, sc->sc_msg_len, sc->sc_buffer);
    499 			return;
    500 		}
    501 		wakeup(&sc->sc_event);
    502 	} else {
    503 		DPRINTF("bogus message\n");
    504 	}
    505 }
    506 
    507 static void
    508 adbms_process_event(struct adbms_softc *sc, int len, uint8_t *buffer)
    509 {
    510 	int buttons = 0, mask, dx, dy, i;
    511 	int button_bit = 1;
    512 
    513 	if ((sc->sc_adbdev->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) {
    514 		/* massage the data to look like EMP data */
    515 		if ((buffer[2] & 0x04) == 0x04)
    516 			buffer[0] &= 0x7f;
    517 		else
    518 			buffer[0] |= 0x80;
    519 		if ((buffer[2] & 0x02) == 0x02)
    520 			buffer[1] &= 0x7f;
    521 		else
    522 			buffer[1] |= 0x80;
    523 		if ((buffer[2] & 0x01) == 0x01)
    524 			buffer[2] = 0x00;
    525 		else
    526 			buffer[2] = 0x80;
    527 	}
    528 
    529 	switch (sc->sc_adbdev->handler_id) {
    530 		case ADBMS_USPEED:
    531 		case ADBMS_UCONTOUR:
    532 			/* MicroSpeed mouse and Contour mouse */
    533 			if (len == 4)
    534 				buttons = (~buffer[3]) & 0xff;
    535 			else
    536 				buttons = (buffer[1] & 0x80) ? 0 : 1;
    537 			break;
    538 		case ADBMS_MSA3:
    539 			/* Mouse Systems A3 mouse */
    540 			if (len == 3)
    541 				buttons = (~buffer[2]) & 0x07;
    542 			else
    543 				buttons = (buffer[0] & 0x80) ? 0 : 1;
    544 			break;
    545 		default:
    546 			/* Classic Mouse Protocol (up to 2 buttons) */
    547 			for (i = 0; i < 2; i++, button_bit <<= 1)
    548 				/* 0 when button down */
    549 				if (!(buffer[i] & 0x80))
    550 					buttons |= button_bit;
    551 				else
    552 					buttons &= ~button_bit;
    553 			/* Extended Protocol (up to 6 more buttons) */
    554 			for (mask = 0x80; i < len;
    555 			     i += (mask == 0x80), button_bit <<= 1) {
    556 				/* 0 when button down */
    557 				if (!(buffer[i] & mask))
    558 					buttons |= button_bit;
    559 				else
    560 					buttons &= ~button_bit;
    561 				mask = ((mask >> 4) & 0xf)
    562 					| ((mask & 0xf) << 4);
    563 			}
    564 			break;
    565 	}
    566 
    567 	dx = ((int)(buffer[1] & 0x3f)) - ((buffer[1] & 0x40) ? 64 : 0);
    568 	dy = ((int)(buffer[0] & 0x3f)) - ((buffer[0] & 0x40) ? 64 : 0);
    569 
    570 	if (sc->sc_class == MSCLASS_TRACKPAD) {
    571 
    572 		if (sc->sc_tapping == 1) {
    573 			if (sc->sc_down) {
    574 				/* finger is down - collect motion data */
    575 				sc->sc_x += dx;
    576 				sc->sc_y += dy;
    577 			}
    578 			DPRINTF("buttons: %02x\n", buttons);
    579 			switch (sc->sc_buttons) {
    580 				case 2:
    581 					buttons |= ((buttons & 2) >> 1);
    582 					adbms_mangle_2(sc, buttons);
    583 					break;
    584 				case 4:
    585 					adbms_mangle_4(sc, buttons);
    586 					break;
    587 			}
    588 		}
    589 		/* filter the pseudo-buttons out */
    590 		buttons &= 1;
    591 	}
    592 
    593 	if (sc->sc_wsmousedev)
    594 		wsmouse_input(sc->sc_wsmousedev, sc->sc_mb | buttons,
    595 			      dx, -dy, 0, 0,
    596 			      WSMOUSE_INPUT_DELTA);
    597 #if NAED > 0
    598 	aed_input(&new_event);
    599 #endif
    600 }
    601 
    602 static void
    603 adbms_mangle_2(struct adbms_softc *sc, int buttons)
    604 {
    605 
    606 	if (buttons & 4) {
    607 		/* finger down on pad */
    608 		if (sc->sc_down == 0) {
    609 			sc->sc_down = 1;
    610 			sc->sc_x = 0;
    611 			sc->sc_y = 0;
    612 		}
    613 	}
    614 	if (buttons & 8) {
    615 		/* finger up */
    616 		if (sc->sc_down) {
    617 			if (((sc->sc_x * sc->sc_x +
    618 			    sc->sc_y * sc->sc_y) < 3) &&
    619 			    (sc->sc_wsmousedev)) {
    620 				/*
    621 				 * if there wasn't much movement between
    622 				 * finger down and up again we assume
    623 				 * someone tapped the pad and we just
    624 				 * send a mouse button event
    625 				 */
    626 				wsmouse_input(sc->sc_wsmousedev,
    627 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    628 			}
    629 			sc->sc_down = 0;
    630 		}
    631 	}
    632 }
    633 
    634 static void
    635 adbms_mangle_4(struct adbms_softc *sc, int buttons)
    636 {
    637 
    638 	if (buttons & 0x20) {
    639 		/* finger down on pad */
    640 		if (sc->sc_down == 0) {
    641 			sc->sc_down = 1;
    642 			sc->sc_x = 0;
    643 			sc->sc_y = 0;
    644 		}
    645 	}
    646 	if ((buttons & 0x20) == 0) {
    647 		/* finger up */
    648 		if (sc->sc_down) {
    649 			if (((sc->sc_x * sc->sc_x +
    650 			    sc->sc_y * sc->sc_y) < 3) &&
    651 			    (sc->sc_wsmousedev)) {
    652 				/*
    653 				 * if there wasn't much movement between
    654 				 * finger down and up again we assume
    655 				 * someone tapped the pad and we just
    656 				 * send a mouse button event
    657 				 */
    658 				wsmouse_input(sc->sc_wsmousedev,
    659 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    660 			}
    661 			sc->sc_down = 0;
    662 		}
    663 	}
    664 }
    665 
    666 static int
    667 adbms_enable(void *v)
    668 {
    669 	return 0;
    670 }
    671 
    672 static int
    673 adbms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    674 {
    675 
    676 	switch (cmd) {
    677 	case WSMOUSEIO_GTYPE:
    678 		*(u_int *)data = WSMOUSE_TYPE_ADB;
    679 		break;
    680 
    681 	default:
    682 		return (EPASSTHROUGH);
    683 	}
    684 	return (0);
    685 }
    686 
    687 static void
    688 adbms_disable(void *v)
    689 {
    690 }
    691 
    692 static void
    693 init_trackpad(struct adbms_softc *sc)
    694 {
    695 	const struct sysctlnode *me = NULL, *node = NULL;
    696 	int cmd, addr, ret;
    697 	uint8_t buffer[16];
    698 	uint8_t b2[] = {0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
    699 
    700 	addr = sc->sc_adbdev->current_addr;
    701 	cmd = ADBTALK(addr, 1);
    702 	if (!adbms_send_sync(sc, cmd, 0, NULL))
    703 		return;
    704 
    705 	if (sc->sc_msg_len != 8)
    706 		return;
    707 
    708 	memcpy(buffer, sc->sc_buffer, 8);
    709 
    710 	/* now whack the pad */
    711 	cmd = ADBLISTEN(addr, 1);
    712 	buffer[6] = 0x0d;
    713 	adbms_send_sync(sc, cmd, 8, buffer);
    714 
    715 	delay(1000);
    716 	cmd = ADBLISTEN(addr, 2);
    717 	adbms_send_sync(sc, cmd, 8, b2);
    718 
    719 	delay(1000);
    720 	cmd = ADBLISTEN(addr, 1);
    721 	buffer[6] = 0x03;
    722 	adbms_send_sync(sc, cmd, 8, buffer);
    723 
    724 	cmd = ADBFLUSH(addr);
    725 	adbms_send_sync(sc, cmd, 0, NULL);
    726 	delay(1000);
    727 
    728 	/*
    729 	 * setup a sysctl node to control whether tapping the pad should
    730 	 * trigger mouse button events
    731 	 */
    732 
    733 	sc->sc_tapping = 1;
    734 
    735 	ret = sysctl_createv(NULL, 0, NULL, &me,
    736 	    CTLFLAG_READWRITE,
    737 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
    738 	    NULL, 0, NULL, 0,
    739 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    740 
    741 	ret = sysctl_createv(NULL, 0, NULL, &node,
    742 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    743 	    CTLTYPE_INT, "tapping", "tapping the pad causes button events",
    744 	    sysctl_adbms_tap, 1, (void *)sc, 0,
    745 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
    746 }
    747 
    748 static int
    749 adbms_wait(struct adbms_softc *sc, int timeout)
    750 {
    751 	int cnt = 0;
    752 
    753 	if (sc->sc_poll) {
    754 		while (sc->sc_msg_len == -1) {
    755 			sc->sc_ops->poll(sc->sc_ops->cookie);
    756 		}
    757 	} else {
    758 		while ((sc->sc_msg_len == -1) && (cnt < timeout)) {
    759 			tsleep(&sc->sc_event, 0, "adbmsio", hz);
    760 			cnt++;
    761 		}
    762 	}
    763 	return (sc->sc_msg_len > 0);
    764 }
    765 
    766 static int
    767 adbms_send_sync(struct adbms_softc *sc, uint8_t cmd, int len, uint8_t *msg)
    768 {
    769 	int i;
    770 
    771 	sc->sc_msg_len = -1;
    772 	DPRINTF("send: %02x", cmd);
    773 	for (i = 0; i < len; i++)
    774 		DPRINTF(" %02x", msg[i]);
    775 	DPRINTF("\n");
    776 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, len, msg);
    777 	adbms_wait(sc, 1000);
    778 	return (sc->sc_msg_len != -1);
    779 }
    780 
    781 static int
    782 sysctl_adbms_tap(SYSCTLFN_ARGS)
    783 {
    784 	struct sysctlnode node = *rnode;
    785 	struct adbms_softc *sc = node.sysctl_data;
    786 
    787 	node.sysctl_idata = sc->sc_tapping;
    788 
    789 	if (newp) {
    790 
    791 		/* we're asked to write */
    792 		node.sysctl_data = &sc->sc_tapping;
    793 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    794 
    795 			sc->sc_tapping = (*(int *)node.sysctl_data == 0) ? 0 : 1;
    796 			return 0;
    797 		}
    798 		return EINVAL;
    799 	} else {
    800 
    801 		node.sysctl_data = &sc->sc_tapping;
    802 		node.sysctl_size = 4;
    803 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    804 	}
    805 
    806 	return 0;
    807 }
    808 
    809 SYSCTL_SETUP(sysctl_ams_setup, "sysctl ams subtree setup")
    810 {
    811 
    812 	sysctl_createv(NULL, 0, NULL, NULL,
    813 		       CTLFLAG_PERMANENT,
    814 		       CTLTYPE_NODE, "machdep", NULL,
    815 		       NULL, 0, NULL, 0,
    816 		       CTL_MACHDEP, CTL_EOL);
    817 }
    818