Home | History | Annotate | Line # | Download | only in adb
adb_ms.c revision 1.1
      1 /*	$NetBSD: adb_ms.c,v 1.1 2007/01/17 23:20:16 macallan 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: adb_ms.c,v 1.1 2007/01/17 23:20:16 macallan 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 #include <sys/kernel.h>
     45 
     46 #include <machine/autoconf.h>
     47 
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/wsmousevar.h>
     50 
     51 #include <machine/adbsys.h>
     52 #include <dev/adb/adbvar.h>
     53 
     54 #include "adbdebug.h"
     55 
     56 #ifdef ADBMS_DEBUG
     57 #define DPRINTF printf
     58 #else
     59 #define DPRINTF while (0) printf
     60 #endif
     61 
     62 /*
     63  * State info, per mouse instance.
     64  */
     65 struct adbms_softc {
     66 	struct	device	sc_dev;
     67 	struct adb_device *sc_adbdev;
     68 	struct adb_bus_accessops *sc_ops;
     69 
     70 	/* Extended Mouse Protocol info, faked for non-EMP mice */
     71 	u_int8_t	sc_class;	/* mouse class (mouse, trackball) */
     72 	u_int8_t	sc_buttons;	/* number of buttons */
     73 	u_int32_t	sc_res;		/* mouse resolution (dpi) */
     74 	char		sc_devid[5];	/* device indentifier */
     75 	uint8_t		sc_us;		/* cmd to watch for */
     76 	int		sc_mb;		/* current button state */
     77 	struct device	*sc_wsmousedev;
     78 	/* helpers for trackpads */
     79 	int		sc_down;
     80 	/*
     81 	 * trackpad protocol variant. Known so far:
     82 	 * 2 buttons - PowerBook 3400, single events on button 3 and 4 indicate
     83 	 *             finger down and up
     84 	 * 4 buttons - iBook G4, button 6 indicates finger down, button 4 is
     85 	 *             always down
     86 	 */
     87 	int		sc_x, sc_y;
     88 	/* buffers */
     89 	int		sc_poll;
     90 	int		sc_msg_len;
     91 	int		sc_event;
     92 	uint8_t		sc_buffer[16];
     93 };
     94 
     95 /* EMP device classes */
     96 #define MSCLASS_TABLET		0
     97 #define MSCLASS_MOUSE		1
     98 #define MSCLASS_TRACKBALL	2
     99 #define MSCLASS_TRACKPAD	3
    100 
    101 /*
    102  * Function declarations.
    103  */
    104 static int	adbms_match(struct device *, struct cfdata *, void *);
    105 static void	adbms_attach(struct device *, struct device *, void *);
    106 static void	ems_init(struct adbms_softc *);
    107 //static void	ms_processevent(adb_event_t *event, struct adbms_softc *);
    108 static void	init_trackpad(struct adbms_softc *);
    109 static void	adbms_init_mouse(struct adbms_softc *);
    110 static void	adbms_init_turbo(struct adbms_softc *);
    111 static void	adbms_init_uspeed(struct adbms_softc *);
    112 static void	adbms_process_event(struct adbms_softc *, int, uint8_t *);
    113 static int	adbms_send_sync(struct adbms_softc *, uint8_t, int, uint8_t *);
    114 
    115 /* Driver definition. */
    116 CFATTACH_DECL(adbms, sizeof(struct adbms_softc),
    117     adbms_match, adbms_attach, NULL, NULL);
    118 
    119 static int adbms_enable(void *);
    120 static int adbms_ioctl(void *, u_long, caddr_t, int, struct lwp *);
    121 static void adbms_disable(void *);
    122 
    123 /*
    124  * handle tapping the trackpad
    125  * different pads report different button counts and use slightly different
    126  * protocols
    127  */
    128 static void adbms_mangle_2(struct adbms_softc *, int);
    129 static void adbms_mangle_4(struct adbms_softc *, int);
    130 static void adbms_handler(void *, int, uint8_t *);
    131 static int  adbms_wait(struct adbms_softc *, int);
    132 
    133 const struct wsmouse_accessops adbms_accessops = {
    134 	adbms_enable,
    135 	adbms_ioctl,
    136 	adbms_disable,
    137 };
    138 
    139 static int
    140 adbms_match(struct device *parent, struct cfdata *cf, void *aux)
    141 {
    142 	struct adb_attach_args *aaa = aux;
    143 
    144 	if (aaa->dev->original_addr == ADBADDR_MS)
    145 		return 1;
    146 	else
    147 		return 0;
    148 }
    149 
    150 static void
    151 adbms_attach(struct device *parent, struct device *self, void *aux)
    152 {
    153 	struct adbms_softc *sc = (struct adbms_softc *)self;
    154 	struct adb_attach_args *aaa = aux;
    155 	struct wsmousedev_attach_args a;
    156 
    157 	sc->sc_ops = aaa->ops;
    158 	sc->sc_adbdev = aaa->dev;
    159 	sc->sc_adbdev->cookie = sc;
    160 	sc->sc_adbdev->handler = adbms_handler;
    161 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
    162 	printf(" addr %d ", sc->sc_adbdev->current_addr);
    163 
    164 	sc->sc_class = MSCLASS_MOUSE;
    165 	sc->sc_buttons = 1;
    166 	sc->sc_res = 100;
    167 	sc->sc_devid[0] = 0;
    168 	sc->sc_devid[4] = 0;
    169 	sc->sc_poll = 0;
    170 	sc->sc_msg_len = 0;
    171 
    172 	ems_init(sc);
    173 
    174 	/* print out the type of mouse we have */
    175 	switch (sc->sc_adbdev->handler_id) {
    176 	case ADBMS_100DPI:
    177 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
    178 		    (int)(sc->sc_res));
    179 		break;
    180 	case ADBMS_200DPI:
    181 		sc->sc_res = 200;
    182 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
    183 		    (int)(sc->sc_res));
    184 		break;
    185 	case ADBMS_MSA3:
    186 		printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
    187 		    sc->sc_buttons, (int)(sc->sc_res));
    188 		break;
    189 	case ADBMS_USPEED:
    190 		printf("MicroSpeed mouse, default parameters\n");
    191 		break;
    192 	case ADBMS_UCONTOUR:
    193 		printf("Contour mouse, default parameters\n");
    194 		break;
    195 	case ADBMS_TURBO:
    196 		printf("Kensington Turbo Mouse\n");
    197 		break;
    198 	case ADBMS_EXTENDED:
    199 		if (sc->sc_devid[0] == '\0') {
    200 			printf("Logitech ");
    201 			switch (sc->sc_class) {
    202 			case MSCLASS_MOUSE:
    203 				printf("MouseMan (non-EMP) mouse");
    204 				break;
    205 			case MSCLASS_TRACKBALL:
    206 				printf("TrackMan (non-EMP) trackball");
    207 				break;
    208 			default:
    209 				printf("non-EMP relative positioning device");
    210 				break;
    211 			}
    212 			printf("\n");
    213 		} else {
    214 			printf("EMP ");
    215 			switch (sc->sc_class) {
    216 			case MSCLASS_TABLET:
    217 				printf("tablet");
    218 				break;
    219 			case MSCLASS_MOUSE:
    220 				printf("mouse");
    221 				break;
    222 			case MSCLASS_TRACKBALL:
    223 				printf("trackball");
    224 				break;
    225 			case MSCLASS_TRACKPAD:
    226 				printf("trackpad");
    227 				init_trackpad(sc);
    228 				break;
    229 			default:
    230 				printf("unknown device");
    231 				break;
    232 			}
    233 			printf(" <%s> %d-button, %d dpi\n", sc->sc_devid,
    234 			    sc->sc_buttons, (int)(sc->sc_res));
    235 		}
    236 		break;
    237 	default:
    238 		printf("relative positioning device (mouse?) (%d)\n",
    239 			sc->sc_adbdev->handler_id);
    240 		break;
    241 	}
    242 
    243 	a.accessops = &adbms_accessops;
    244 	a.accesscookie = sc;
    245 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    246 }
    247 
    248 
    249 /*
    250  * Initialize extended mouse support -- probes devices as described
    251  * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
    252  *
    253  * Extended Mouse Protocol is documented in TechNote HW1:
    254  * 	"ADB - The Untold Story:  Space Aliens Ate My Mouse"
    255  *
    256  * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
    257  * 	     Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
    258  */
    259 void
    260 ems_init(struct adbms_softc *sc)
    261 {
    262 
    263 	DPRINTF("ems_init %d\n", sc->sc_adbdev->handler_id);
    264 
    265 	switch (sc->sc_adbdev->handler_id) {
    266 		case ADBMS_USPEED:
    267 		case ADBMS_UCONTOUR:
    268 			adbms_init_uspeed(sc);
    269 			return;
    270 		case ADBMS_TURBO:
    271 			adbms_init_turbo(sc);
    272 			return;
    273 		case ADBMS_100DPI:
    274 		case ADBMS_200DPI:
    275 			adbms_init_mouse(sc);
    276 	}
    277 }
    278 
    279 static void
    280 adbms_init_uspeed(struct adbms_softc *sc)
    281 {
    282 	uint8_t cmd, addr, buffer[4];
    283 
    284 	addr = sc->sc_adbdev->current_addr;
    285 
    286 	/* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
    287 	cmd = ADBLISTEN(addr, 1);
    288 
    289 	/*
    290 	 * To setup the MicroSpeed or the Contour, it appears
    291 	 * that we can send the following command to the mouse
    292 	 * and then expect data back in the form:
    293 	 *  buffer[0] = 4 (bytes)
    294 	 *  buffer[1], buffer[2] as std. mouse
    295 	 *  buffer[3] = buffer[4] = 0xff when no buttons
    296 	 *   are down.  When button N down, bit N is clear.
    297 	 * buffer[4]'s locking mask enables a
    298 	 * click to toggle the button down state--sort of
    299 	 * like the "Easy Access" shift/control/etc. keys.
    300 	 * buffer[3]'s alternative speed mask enables using
    301 	 * different speed when the corr. button is down
    302 	 */
    303 	buffer[0] = 0x00;	/* Alternative speed */
    304 	buffer[1] = 0x00;	/* speed = maximum */
    305 	buffer[2] = 0x10;	/* enable extended protocol,
    306 				 * lower bits = alt. speed mask
    307 				 *            = 0000b
    308 				 */
    309 	buffer[3] = 0x07;	/* Locking mask = 0000b,
    310 				 * enable buttons = 0111b
    311 				 */
    312 	adbms_send_sync(sc, cmd, 4, buffer);
    313 
    314 	sc->sc_buttons = 3;
    315 	sc->sc_res = 200;
    316 }
    317 
    318 static void
    319 adbms_init_turbo(struct adbms_softc *sc)
    320 {
    321 	uint8_t addr;
    322 
    323 	/* Found Kensington Turbo Mouse */
    324 	static u_char data1[] =
    325 		{ 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
    326 	static u_char data2[] =
    327 		{ 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
    328 
    329 	addr = sc->sc_adbdev->current_addr;
    330 
    331 	adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL);
    332 	adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data1);
    333 	adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL);
    334 	adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data2);
    335 }
    336 
    337 static void
    338 adbms_init_mouse(struct adbms_softc *sc)
    339 {
    340 	int len;
    341 	uint8_t cmd, addr, buffer[16];
    342 
    343 	addr = sc->sc_adbdev->current_addr;
    344 	/* found a mouse */
    345 	cmd = ADBTALK(addr, 3);
    346 	if (!adbms_send_sync(sc, cmd, 0, NULL)) {
    347 #ifdef ADBMS_DEBUG
    348 		printf("adb: ems_init timed out\n");
    349 #endif
    350 		return;
    351 	}
    352 
    353 	/* Attempt to initialize Extended Mouse Protocol */
    354 	len = sc->sc_msg_len;
    355 	memcpy(buffer, sc->sc_buffer, len);
    356 	DPRINTF("buffer: %02x %02x\n", buffer[0], buffer[1]);
    357 	buffer[1] = 4; /* make handler ID 4 */
    358 	cmd = ADBLISTEN(addr, 3);
    359 	if (!adbms_send_sync(sc, cmd, len, buffer)) {
    360 #ifdef ADBMS_DEBUG
    361 		printf("adb: ems_init timed out\n");
    362 #endif
    363 		return;
    364 	}
    365 
    366 	/*
    367 	 * Check to see if successful, if not
    368 	 * try to initialize it as other types
    369 	 */
    370 	cmd = ADBTALK(addr, 3);
    371 	if (!adbms_send_sync(sc, cmd, 0, NULL)) {
    372 		DPRINTF("timeout checking for EMP switch\n");
    373 		return;
    374 	}
    375 	DPRINTF("new handler ID: %02x\n", sc->sc_buffer[1]);
    376 	if (sc->sc_buffer[1] == ADBMS_EXTENDED) {
    377 		sc->sc_adbdev->handler_id = ADBMS_EXTENDED;
    378 		cmd = ADBTALK(addr, 1);
    379 		if(!adbms_send_sync(sc, cmd, 0, NULL)) {
    380 			DPRINTF("adb: ems_init timed out\n");
    381 			return;
    382 		}
    383 
    384 		len = sc->sc_msg_len;
    385 		memcpy(buffer, sc->sc_buffer, len);
    386 
    387 		if (sc->sc_msg_len == 8) {
    388 			/* we have a true EMP device */
    389 #ifdef ADB_PRINT_EMP
    390 
    391 			printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n",
    392 			    buffer[0], buffer[1], buffer[2], buffer[3],
    393 			    buffer[4], buffer[5], buffer[6], buffer[7]);
    394 #endif
    395 			sc->sc_class = buffer[6];
    396 			sc->sc_buttons = buffer[7];
    397 			sc->sc_res = (int)*(short *)&buffer[4];
    398 				memcpy(sc->sc_devid, &(buffer[0]), 4);
    399 		} else if (buffer[0] == 0x9a &&
    400 		    ((buffer[1] == 0x20) || (buffer[1] == 0x21))) {
    401 			/*
    402 			 * Set up non-EMP Mouseman/Trackman to put
    403 			 * button bits in 3rd byte instead of sending
    404 			 * via pseudo keyboard device.
    405 			 */
    406 			if (buffer[1] == 0x21)
    407 				sc->sc_class = MSCLASS_TRACKBALL;
    408 			else
    409 				sc->sc_class = MSCLASS_MOUSE;
    410 
    411 			cmd = ADBLISTEN(addr, 1);
    412 			buffer[0]=0x00;
    413 			buffer[1]=0x81;
    414 			adbms_send_sync(sc, cmd, 2, buffer);
    415 
    416 			cmd = ADBLISTEN(addr, 1);
    417 			buffer[0]=0x01;
    418 			buffer[1]=0x81;
    419 			adbms_send_sync(sc, cmd, 2, buffer);
    420 
    421 			cmd = ADBLISTEN(addr, 1);
    422 			buffer[0]=0x02;
    423 			buffer[1]=0x81;
    424 			adbms_send_sync(sc, cmd, 2, buffer);
    425 
    426 			cmd = ADBLISTEN(addr, 1);
    427 			buffer[0]=0x03;
    428 			buffer[1]=0x38;
    429 			adbms_send_sync(sc, cmd, 2, buffer);
    430 
    431 			sc->sc_buttons = 3;
    432 			sc->sc_res = 400;
    433 		}
    434 	} else {
    435 		/* Attempt to initialize as an A3 mouse */
    436 		buffer[1] = 0x03; /* make handler ID 3 */
    437 		cmd = ADBLISTEN(addr, 3);
    438 		if (!adbms_send_sync(sc, cmd, len, buffer)) {
    439 #ifdef ADBMS_DEBUG
    440 			printf("adb: ems_init timed out\n");
    441 #endif
    442 			return;
    443 		}
    444 
    445 		/*
    446 		 * Check to see if successful, if not
    447 		 * try to initialize it as other types
    448 		 */
    449 		cmd = ADBTALK(addr, 3);
    450 		if(adbms_send_sync(sc, cmd, 0, NULL)) {
    451 			len = sc->sc_msg_len;
    452 			memcpy(buffer, sc->sc_buffer, len);
    453 			if (buffer[1] == ADBMS_MSA3) {
    454 				sc->sc_adbdev->handler_id = ADBMS_MSA3;
    455 				/* Initialize as above */
    456 				cmd = ADBLISTEN(addr, 2);
    457 				/* listen 2 */
    458 				buffer[0] = 0x00;
    459 				/* Irrelevant, buffer has 0x77 */
    460 				buffer[2] = 0x07;
    461 				/*
    462 				 * enable 3 button mode = 0111b,
    463 				 * speed = normal
    464 				 */
    465 				adbms_send_sync(sc, cmd, 3, buffer);
    466 				sc->sc_buttons = 3;
    467 				sc->sc_res = 300;
    468 			}
    469 		}
    470 	}
    471 }
    472 
    473 static void
    474 adbms_handler(void *cookie, int len, uint8_t *data)
    475 {
    476 	struct adbms_softc *sc = cookie;
    477 
    478 #ifdef ADBMS_DEBUG
    479 	int i;
    480 	printf("%s: %02x - ", sc->sc_dev.dv_xname, sc->sc_us);
    481 	for (i = 0; i < len; i++) {
    482 		printf(" %02x", data[i]);
    483 	}
    484 	printf("\n");
    485 #endif
    486 	if (len >= 2) {
    487 		memcpy(sc->sc_buffer, &data[2], len - 2);
    488 		sc->sc_msg_len = len - 2;
    489 		if (data[1] == sc->sc_us) {
    490 			/* make sense of the mouse message */
    491 			adbms_process_event(sc, sc->sc_msg_len, sc->sc_buffer);
    492 			return;
    493 		}
    494 		wakeup(&sc->sc_event);
    495 	} else {
    496 		DPRINTF("bogus message\n");
    497 	}
    498 }
    499 
    500 static void
    501 adbms_process_event(struct adbms_softc *sc, int len, uint8_t *buffer)
    502 {
    503 	int buttons = 0, mask, dx, dy, i;
    504 	int button_bit = 1;
    505 
    506 	if ((sc->sc_adbdev->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) {
    507 		/* massage the data to look like EMP data */
    508 		if ((buffer[2] & 0x04) == 0x04)
    509 			buffer[0] &= 0x7f;
    510 		else
    511 			buffer[0] |= 0x80;
    512 		if ((buffer[2] & 0x02) == 0x02)
    513 			buffer[1] &= 0x7f;
    514 		else
    515 			buffer[1] |= 0x80;
    516 		if ((buffer[2] & 0x01) == 0x01)
    517 			buffer[2] = 0x00;
    518 		else
    519 			buffer[2] = 0x80;
    520 	}
    521 
    522 	switch (sc->sc_adbdev->handler_id) {
    523 		case ADBMS_USPEED:
    524 		case ADBMS_UCONTOUR:
    525 			/* MicroSpeed mouse and Contour mouse */
    526 			if (len == 4)
    527 				buttons = (~buffer[3]) & 0xff;
    528 			else
    529 				buttons = (buffer[1] & 0x80) ? 0 : 1;
    530 			break;
    531 		case ADBMS_MSA3:
    532 			/* Mouse Systems A3 mouse */
    533 			if (len == 3)
    534 				buttons = (~buffer[2]) & 0x07;
    535 			else
    536 				buttons = (buffer[0] & 0x80) ? 0 : 1;
    537 			break;
    538 		default:
    539 			/* Classic Mouse Protocol (up to 2 buttons) */
    540 			for (i = 0; i < 2; i++, button_bit <<= 1)
    541 				/* 0 when button down */
    542 				if (!(buffer[i] & 0x80))
    543 					buttons |= button_bit;
    544 				else
    545 					buttons &= ~button_bit;
    546 			/* Extended Protocol (up to 6 more buttons) */
    547 			for (mask = 0x80; i < len;
    548 			     i += (mask == 0x80), button_bit <<= 1) {
    549 				/* 0 when button down */
    550 				if (!(buffer[i] & mask))
    551 					buttons |= button_bit;
    552 				else
    553 					buttons &= ~button_bit;
    554 				mask = ((mask >> 4) & 0xf)
    555 					| ((mask & 0xf) << 4);
    556 			}
    557 			break;
    558 	}
    559 
    560 	dx = ((int)(buffer[1] & 0x3f)) - ((buffer[1] & 0x40) ? 64 : 0);
    561 	dy = ((int)(buffer[0] & 0x3f)) - ((buffer[0] & 0x40) ? 64 : 0);
    562 
    563 	if (sc->sc_class == MSCLASS_TRACKPAD) {
    564 
    565 		if (sc->sc_down) {
    566 			/* finger is down - collect motion data */
    567 			sc->sc_x += dx;
    568 			sc->sc_y += dy;
    569 		}
    570 		DPRINTF("buttons: %02x\n", buttons);
    571 		switch (sc->sc_buttons) {
    572 			case 2:
    573 				buttons |= ((buttons & 2) >> 1);
    574 				adbms_mangle_2(sc, buttons);
    575 				break;
    576 			case 4:
    577 				adbms_mangle_4(sc, buttons);
    578 				break;
    579 		}
    580 		/* filter the pseudo-buttons out */
    581 		buttons &= 1;
    582 	}
    583 
    584 	if (sc->sc_wsmousedev)
    585 		wsmouse_input(sc->sc_wsmousedev, sc->sc_mb | buttons,
    586 			      dx, -dy, 0, 0,
    587 			      WSMOUSE_INPUT_DELTA);
    588 #if NAED > 0
    589 	aed_input(&new_event);
    590 #endif
    591 }
    592 
    593 static void
    594 adbms_mangle_2(struct adbms_softc *sc, int buttons)
    595 {
    596 
    597 	if (buttons & 4) {
    598 		/* finger down on pad */
    599 		if (sc->sc_down == 0) {
    600 			sc->sc_down = 1;
    601 			sc->sc_x = 0;
    602 			sc->sc_y = 0;
    603 		}
    604 	}
    605 	if (buttons & 8) {
    606 		/* finger up */
    607 		if (sc->sc_down) {
    608 			if (((sc->sc_x * sc->sc_x +
    609 			    sc->sc_y * sc->sc_y) < 20) &&
    610 			    (sc->sc_wsmousedev)) {
    611 				/*
    612 				 * if there wasn't much movement between
    613 				 * finger down and up again we assume
    614 				 * someone tapped the pad and we just
    615 				 * send a mouse button event
    616 				 */
    617 				wsmouse_input(sc->sc_wsmousedev,
    618 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    619 			}
    620 			sc->sc_down = 0;
    621 		}
    622 	}
    623 }
    624 
    625 static void
    626 adbms_mangle_4(struct adbms_softc *sc, int buttons)
    627 {
    628 
    629 	if (buttons & 0x20) {
    630 		/* finger down on pad */
    631 		if (sc->sc_down == 0) {
    632 			sc->sc_down = 1;
    633 			sc->sc_x = 0;
    634 			sc->sc_y = 0;
    635 		}
    636 	}
    637 	if ((buttons & 0x20) == 0) {
    638 		/* finger up */
    639 		if (sc->sc_down) {
    640 			if (((sc->sc_x * sc->sc_x +
    641 			    sc->sc_y * sc->sc_y) < 20) &&
    642 			    (sc->sc_wsmousedev)) {
    643 				/*
    644 				 * if there wasn't much movement between
    645 				 * finger down and up again we assume
    646 				 * someone tapped the pad and we just
    647 				 * send a mouse button event
    648 				 */
    649 				wsmouse_input(sc->sc_wsmousedev,
    650 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    651 			}
    652 			sc->sc_down = 0;
    653 		}
    654 	}
    655 }
    656 
    657 static int
    658 adbms_enable(void *v)
    659 {
    660 	return 0;
    661 }
    662 
    663 static int
    664 adbms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
    665 {
    666 
    667 	switch (cmd) {
    668 	case WSMOUSEIO_GTYPE:
    669 		*(u_int *)data = WSMOUSE_TYPE_ADB;
    670 		break;
    671 
    672 	default:
    673 		return (EPASSTHROUGH);
    674 	}
    675 	return (0);
    676 }
    677 
    678 static void
    679 adbms_disable(void *v)
    680 {
    681 }
    682 
    683 static void
    684 init_trackpad(struct adbms_softc *sc)
    685 {
    686 	int cmd, addr;
    687 	uint8_t buffer[16];
    688 	uint8_t b2[] = {0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
    689 
    690 	addr = sc->sc_adbdev->current_addr;
    691 	cmd = ADBTALK(addr, 1);
    692 	if (!adbms_send_sync(sc, cmd, 0, NULL))
    693 		return;
    694 
    695 	if (sc->sc_msg_len != 8)
    696 		return;
    697 
    698 	memcpy(buffer, sc->sc_buffer, 8);
    699 
    700 	/* now whack the pad */
    701 	cmd = ADBLISTEN(addr, 1);
    702 	buffer[6] = 0x0d;
    703 	adbms_send_sync(sc, cmd, 8, buffer);
    704 
    705 	delay(1000);
    706 	cmd = ADBLISTEN(addr, 2);
    707 	adbms_send_sync(sc, cmd, 8, b2);
    708 
    709 	delay(1000);
    710 	cmd = ADBLISTEN(addr, 1);
    711 	buffer[6] = 0x03;
    712 	adbms_send_sync(sc, cmd, 8, buffer);
    713 
    714 	cmd = ADBFLUSH(addr);
    715 	adbms_send_sync(sc, cmd, 0, NULL);
    716 	delay(1000);
    717 }
    718 
    719 static int
    720 adbms_wait(struct adbms_softc *sc, int timeout)
    721 {
    722 	int cnt = 0;
    723 
    724 	if (sc->sc_poll) {
    725 		while (sc->sc_msg_len == -1) {
    726 			sc->sc_ops->poll(sc->sc_ops->cookie);
    727 		}
    728 	} else {
    729 		while ((sc->sc_msg_len == -1) && (cnt < timeout)) {
    730 			tsleep(&sc->sc_event, 0, "adbkbdio", hz);
    731 			cnt++;
    732 		}
    733 	}
    734 	return (sc->sc_msg_len > 0);
    735 }
    736 
    737 static int
    738 adbms_send_sync(struct adbms_softc *sc, uint8_t cmd, int len, uint8_t *msg)
    739 {
    740 	int i;
    741 
    742 	sc->sc_msg_len = -1;
    743 	DPRINTF("send: %02x", cmd);
    744 	for (i = 0; i < len; i++)
    745 		DPRINTF(" %02x", msg[i]);
    746 	DPRINTF("\n");
    747 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, len, msg);
    748 	adbms_wait(sc, 1000);
    749 	return (sc->sc_msg_len != -1);
    750 }
    751