Home | History | Annotate | Line # | Download | only in dev
ams.c revision 1.19
      1 /*	$NetBSD: ams.c,v 1.19 2006/10/15 21:15:21 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: ams.c,v 1.19 2006/10/15 21:15:21 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 
     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 __P((struct device *, struct cfdata *, void *));
     60 static void	amsattach __P((struct device *, struct device *, void *));
     61 static void	ems_init __P((struct ams_softc *));
     62 static void	ms_processevent __P((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 __P((void *));
     70 int ams_ioctl __P((void *, u_long, caddr_t, int, struct lwp *));
     71 void ams_disable __P((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(parent, cf, aux)
     89 	struct device *parent;
     90 	struct cfdata *cf;
     91 	void *aux;
     92 {
     93 	struct adb_attach_args *aa_args = aux;
     94 
     95 	if (aa_args->origaddr == ADBADDR_MS)
     96 		return 1;
     97 	else
     98 		return 0;
     99 }
    100 
    101 static void
    102 amsattach(parent, self, aux)
    103 	struct device *parent, *self;
    104 	void   *aux;
    105 {
    106 	ADBSetInfoBlock adbinfo;
    107 	struct ams_softc *sc = (struct ams_softc *)self;
    108 	struct adb_attach_args *aa_args = aux;
    109 	int error;
    110 	struct wsmousedev_attach_args a;
    111 
    112 	sc->origaddr = aa_args->origaddr;
    113 	sc->adbaddr = aa_args->adbaddr;
    114 	sc->handler_id = aa_args->handler_id;
    115 
    116 	sc->sc_class = MSCLASS_MOUSE;
    117 	sc->sc_buttons = 1;
    118 	sc->sc_res = 100;
    119 	sc->sc_devid[0] = 0;
    120 	sc->sc_devid[4] = 0;
    121 
    122 	adbinfo.siServiceRtPtr = (Ptr)ms_adbcomplete;
    123 	adbinfo.siDataAreaAddr = (caddr_t)sc;
    124 
    125 	ems_init(sc);
    126 
    127 	/* print out the type of mouse we have */
    128 	switch (sc->handler_id) {
    129 	case ADBMS_100DPI:
    130 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
    131 		    (int)(sc->sc_res));
    132 		break;
    133 	case ADBMS_200DPI:
    134 		sc->sc_res = 200;
    135 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
    136 		    (int)(sc->sc_res));
    137 		break;
    138 	case ADBMS_MSA3:
    139 		printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
    140 		    sc->sc_buttons, (int)(sc->sc_res));
    141 		break;
    142 	case ADBMS_USPEED:
    143 		printf("MicroSpeed mouse, default parameters\n");
    144 		break;
    145 	case ADBMS_UCONTOUR:
    146 		printf("Contour mouse, default parameters\n");
    147 		break;
    148 	case ADBMS_TURBO:
    149 		printf("Kensington Turbo Mouse\n");
    150 		break;
    151 	case ADBMS_EXTENDED:
    152 		if (sc->sc_devid[0] == '\0') {
    153 			printf("Logitech ");
    154 			switch (sc->sc_class) {
    155 			case MSCLASS_MOUSE:
    156 				printf("MouseMan (non-EMP) mouse");
    157 				break;
    158 			case MSCLASS_TRACKBALL:
    159 				printf("TrackMan (non-EMP) trackball");
    160 				break;
    161 			default:
    162 				printf("non-EMP relative positioning device");
    163 				break;
    164 			}
    165 			printf("\n");
    166 		} else {
    167 			printf("EMP ");
    168 			switch (sc->sc_class) {
    169 			case MSCLASS_TABLET:
    170 				printf("tablet");
    171 				break;
    172 			case MSCLASS_MOUSE:
    173 				printf("mouse");
    174 				break;
    175 			case MSCLASS_TRACKBALL:
    176 				printf("trackball");
    177 				break;
    178 			case MSCLASS_TRACKPAD:
    179 				printf("trackpad");
    180 				init_trackpad(sc);
    181 				break;
    182 			default:
    183 				printf("unknown device");
    184 				break;
    185 			}
    186 			printf(" <%s> %d-button, %d dpi\n", sc->sc_devid,
    187 			    sc->sc_buttons, (int)(sc->sc_res));
    188 		}
    189 		break;
    190 	default:
    191 		printf("relative positioning device (mouse?) (%d)\n",
    192 			sc->handler_id);
    193 		break;
    194 	}
    195 	error = SetADBInfo(&adbinfo, sc->adbaddr);
    196 #ifdef ADB_DEBUG
    197 	if (adb_debug)
    198 		printf("ams: returned %d from SetADBInfo\n", error);
    199 #endif
    200 
    201 	a.accessops = &ams_accessops;
    202 	a.accesscookie = sc;
    203 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    204 }
    205 
    206 
    207 /*
    208  * Initialize extended mouse support -- probes devices as described
    209  * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
    210  *
    211  * Extended Mouse Protocol is documented in TechNote HW1:
    212  * 	"ADB - The Untold Story:  Space Aliens Ate My Mouse"
    213  *
    214  * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
    215  * 	     Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
    216  */
    217 void
    218 ems_init(sc)
    219 	struct ams_softc *sc;
    220 {
    221 	int adbaddr;
    222 	short cmd;
    223 	u_char buffer[9];
    224 
    225 	adbaddr = sc->adbaddr;
    226 	if (sc->origaddr != ADBADDR_MS)
    227 		return;
    228 	if (sc->handler_id == ADBMS_USPEED ||
    229 	    sc->handler_id == ADBMS_UCONTOUR) {
    230 		/* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
    231 		cmd = ADBLISTEN(adbaddr, 1);
    232 
    233 		/*
    234 		 * To setup the MicroSpeed or the Contour, it appears
    235 		 * that we can send the following command to the mouse
    236 		 * and then expect data back in the form:
    237 		 *  buffer[0] = 4 (bytes)
    238 		 *  buffer[1], buffer[2] as std. mouse
    239 		 *  buffer[3] = buffer[4] = 0xff when no buttons
    240 		 *   are down.  When button N down, bit N is clear.
    241 		 * buffer[4]'s locking mask enables a
    242 		 * click to toggle the button down state--sort of
    243 		 * like the "Easy Access" shift/control/etc. keys.
    244 		 * buffer[3]'s alternative speed mask enables using
    245 		 * different speed when the corr. button is down
    246 		 */
    247 		buffer[0] = 4;
    248 		buffer[1] = 0x00;	/* Alternative speed */
    249 		buffer[2] = 0x00;	/* speed = maximum */
    250 		buffer[3] = 0x10;	/* enable extended protocol,
    251 					 * lower bits = alt. speed mask
    252 					 *            = 0000b
    253 					 */
    254 		buffer[4] = 0x07;	/* Locking mask = 0000b,
    255 					 * enable buttons = 0111b
    256 					 */
    257 		adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    258 
    259 		sc->sc_buttons = 3;
    260 		sc->sc_res = 200;
    261 		return;
    262 	}
    263 	if (sc->handler_id == ADBMS_TURBO) {
    264 		/* Found Kensington Turbo Mouse */
    265 		static u_char data1[] =
    266 			{ 8, 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
    267 		static u_char data2[] =
    268 			{ 8, 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
    269 
    270 		buffer[0] = 0;
    271 		adb_op_sync((Ptr)buffer, NULL, (Ptr)0, ADBFLUSH(adbaddr));
    272 
    273 		adb_op_sync((Ptr)data1, NULL, (Ptr)0, ADBLISTEN(adbaddr, 2));
    274 
    275 		buffer[0] = 0;
    276 		adb_op_sync((Ptr)buffer, NULL, (Ptr)0, ADBFLUSH(adbaddr));
    277 
    278 		adb_op_sync((Ptr)data2, NULL, (Ptr)0, ADBLISTEN(adbaddr, 2));
    279 		return;
    280 	}
    281 	if ((sc->handler_id == ADBMS_100DPI) ||
    282 	    (sc->handler_id == ADBMS_200DPI)) {
    283 		/* found a mouse */
    284 		cmd = ADBTALK(adbaddr, 3);
    285 		if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
    286 #ifdef ADB_DEBUG
    287 			if (adb_debug)
    288 				printf("adb: ems_init timed out\n");
    289 #endif
    290 			return;
    291 		}
    292 
    293 		/* Attempt to initialize Extended Mouse Protocol */
    294 		buffer[2] = 4; /* make handler ID 4 */
    295 		cmd = ADBLISTEN(adbaddr, 3);
    296 		if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
    297 #ifdef ADB_DEBUG
    298 			if (adb_debug)
    299 				printf("adb: ems_init timed out\n");
    300 #endif
    301 			return;
    302 		}
    303 
    304 		/*
    305 		 * Check to see if successful, if not
    306 		 * try to initialize it as other types
    307 		 */
    308 		cmd = ADBTALK(adbaddr, 3);
    309 		if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0 &&
    310 		    buffer[2] == ADBMS_EXTENDED) {
    311 			sc->handler_id = ADBMS_EXTENDED;
    312 			cmd = ADBTALK(adbaddr, 1);
    313 			if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
    314 #ifdef ADB_DEBUG
    315 				if (adb_debug)
    316 					printf("adb: ems_init timed out\n");
    317 #endif
    318 			} else if (buffer[0] == 8) {
    319 				/* we have a true EMP device */
    320 #ifdef ADB_PRINT_EMP
    321 				printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n",
    322 				    buffer[1], buffer[2], buffer[3], buffer[4],
    323 				    buffer[5], buffer[6], buffer[7], buffer[8]);
    324 #endif
    325 				sc->sc_class = buffer[7];
    326 				sc->sc_buttons = buffer[8];
    327 				sc->sc_res = (int)*(short *)&buffer[5];
    328 				memcpy(sc->sc_devid, &(buffer[1]), 4);
    329 			} else if (buffer[1] == 0x9a &&
    330 			    ((buffer[2] == 0x20) || (buffer[2] == 0x21))) {
    331 				/*
    332 				 * Set up non-EMP Mouseman/Trackman to put
    333 				 * button bits in 3rd byte instead of sending
    334 				 * via pseudo keyboard device.
    335 				 */
    336 				cmd = ADBLISTEN(adbaddr, 1);
    337 				buffer[0]=2;
    338 				buffer[1]=0x00;
    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]=0x01;
    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]=0x02;
    351 				buffer[2]=0x81;
    352 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    353 
    354 				cmd = ADBLISTEN(adbaddr, 1);
    355 				buffer[0]=2;
    356 				buffer[1]=0x03;
    357 				buffer[2]=0x38;
    358 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    359 
    360 				sc->sc_buttons = 3;
    361 				sc->sc_res = 400;
    362 				if (buffer[2] == 0x21)
    363 					sc->sc_class = MSCLASS_TRACKBALL;
    364 				else
    365 					sc->sc_class = MSCLASS_MOUSE;
    366 			} else
    367 				/* unknown device? */;
    368 		} else {
    369 			/* Attempt to initialize as an A3 mouse */
    370 			buffer[2] = 0x03; /* make handler ID 3 */
    371 			cmd = ADBLISTEN(adbaddr, 3);
    372 			if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
    373 #ifdef ADB_DEBUG
    374 				if (adb_debug)
    375 					printf("adb: ems_init timed out\n");
    376 #endif
    377 				return;
    378 			}
    379 
    380 			/*
    381 			 * Check to see if successful, if not
    382 			 * try to initialize it as other types
    383 			 */
    384 			cmd = ADBTALK(adbaddr, 3);
    385 			if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0
    386 			    && buffer[2] == ADBMS_MSA3) {
    387 				sc->handler_id = ADBMS_MSA3;
    388 				/* Initialize as above */
    389 				cmd = ADBLISTEN(adbaddr, 2);
    390 				/* listen 2 */
    391 				buffer[0] = 3;
    392 				buffer[1] = 0x00;
    393 				/* Irrelevant, buffer has 0x77 */
    394 				buffer[2] = 0x07;
    395 				/*
    396 				 * enable 3 button mode = 0111b,
    397 				 * speed = normal
    398 				 */
    399 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    400 				sc->sc_buttons = 3;
    401 				sc->sc_res = 300;
    402 			} else {
    403 				/* No special support for this mouse */
    404 			}
    405 		}
    406 	}
    407 }
    408 
    409 /*
    410  * Handle putting the mouse data received from the ADB into
    411  * an ADB event record.
    412  */
    413 void
    414 ms_adbcomplete(buffer, data_area, adb_command)
    415 	caddr_t buffer;
    416 	caddr_t data_area;
    417 	int adb_command;
    418 {
    419 	adb_event_t event;
    420 	struct ams_softc *sc;
    421 	int adbaddr;
    422 #ifdef ADB_DEBUG
    423 	int i;
    424 
    425 	if (adb_debug)
    426 		printf("adb: transaction completion\n");
    427 #endif
    428 
    429 	adbaddr = ADB_CMDADDR(adb_command);
    430 	sc = (struct ams_softc *)data_area;
    431 
    432 	if ((sc->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) {
    433 		/* massage the data to look like EMP data */
    434 		if ((buffer[3] & 0x04) == 0x04)
    435 			buffer[1] &= 0x7f;
    436 		else
    437 			buffer[1] |= 0x80;
    438 		if ((buffer[3] & 0x02) == 0x02)
    439 			buffer[2] &= 0x7f;
    440 		else
    441 			buffer[2] |= 0x80;
    442 		if ((buffer[3] & 0x01) == 0x01)
    443 			buffer[3] = 0x00;
    444 		else
    445 			buffer[3] = 0x80;
    446 	}
    447 
    448 	event.addr = adbaddr;
    449 	event.hand_id = sc->handler_id;
    450 	event.def_addr = sc->origaddr;
    451 	event.byte_count = buffer[0];
    452 	memcpy(event.bytes, buffer + 1, event.byte_count);
    453 
    454 #ifdef ADB_DEBUG
    455 	if (adb_debug) {
    456 		printf("ams: from %d at %d (org %d) %d:", event.addr,
    457 		    event.hand_id, event.def_addr, buffer[0]);
    458 		for (i = 1; i <= buffer[0]; i++)
    459 			printf(" %x", buffer[i]);
    460 		printf("\n");
    461 	}
    462 #endif
    463 
    464 	microtime(&event.timestamp);
    465 
    466 	ms_processevent(&event, sc);
    467 }
    468 
    469 /*
    470  * Given a mouse ADB event, record the button settings, calculate the
    471  * x- and y-axis motion, and handoff the event to the appropriate subsystem.
    472  */
    473 static void
    474 ms_processevent(event, sc)
    475 	adb_event_t *event;
    476 	struct ams_softc *sc;
    477 {
    478 	adb_event_t new_event;
    479 	int i, button_bit, max_byte, mask, buttons, dx, dy;
    480 
    481 	new_event = *event;
    482 	buttons = 0;
    483 
    484 	/*
    485 	 * This should handle both plain ol' Apple mice and mice
    486 	 * that claim to support the Extended Apple Mouse Protocol.
    487 	 */
    488 	max_byte = event->byte_count;
    489 	button_bit = 1;
    490 	switch (event->hand_id) {
    491 	case ADBMS_USPEED:
    492 	case ADBMS_UCONTOUR:
    493 		/* MicroSpeed mouse and Contour mouse */
    494 		if (max_byte == 4)
    495 			buttons = (~event->bytes[2]) & 0xff;
    496 		else
    497 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    498 		break;
    499 	case ADBMS_MSA3:
    500 		/* Mouse Systems A3 mouse */
    501 		if (max_byte == 3)
    502 			buttons = (~event->bytes[2]) & 0x07;
    503 		else
    504 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    505 		break;
    506 	default:
    507 		/* Classic Mouse Protocol (up to 2 buttons) */
    508 		for (i = 0; i < 2; i++, button_bit <<= 1)
    509 			/* 0 when button down */
    510 			if (!(event->bytes[i] & 0x80))
    511 				buttons |= button_bit;
    512 			else
    513 				buttons &= ~button_bit;
    514 		/* Extended Protocol (up to 6 more buttons) */
    515 		for (mask = 0x80; i < max_byte;
    516 		     i += (mask == 0x80), button_bit <<= 1) {
    517 			/* 0 when button down */
    518 			if (!(event->bytes[i] & mask))
    519 				buttons |= button_bit;
    520 			else
    521 				buttons &= ~button_bit;
    522 			mask = ((mask >> 4) & 0xf)
    523 				| ((mask & 0xf) << 4);
    524 		}
    525 		break;
    526 	}
    527 
    528 	dx = ((int)(event->bytes[1] & 0x3f)) -
    529 				((event->bytes[1] & 0x40) ? 64 : 0);
    530 	dy = ((int) (event->bytes[0] & 0x3f)) -
    531 				((event->bytes[0] & 0x40) ? 64 : 0);
    532 
    533 	if (sc->sc_class == MSCLASS_TRACKPAD) {
    534 
    535 		if (sc->sc_down) {
    536 			/* finger is down - collect motion data */
    537 			sc->sc_x += dx;
    538 			sc->sc_y += dy;
    539 		}
    540 		switch (sc->sc_buttons) {
    541 			case 2:
    542 				ams_mangle_2(sc, buttons);
    543 				break;
    544 			case 4:
    545 				ams_mangle_4(sc, buttons);
    546 				break;
    547 		}
    548 		/* filter the pseudo-buttons out */
    549 		buttons &= 1;
    550 	}
    551 
    552 	new_event.u.m.buttons = sc->sc_mb | buttons;
    553 	new_event.u.m.dx = dx;
    554 	new_event.u.m.dy = dy;
    555 
    556 	if (sc->sc_wsmousedev)
    557 		wsmouse_input(sc->sc_wsmousedev, new_event.u.m.buttons,
    558 			      new_event.u.m.dx, -new_event.u.m.dy, 0,
    559 			      WSMOUSE_INPUT_DELTA);
    560 #if NAED > 0
    561 	aed_input(&new_event);
    562 #endif
    563 }
    564 
    565 static void
    566 ams_mangle_2(struct ams_softc *sc, int buttons)
    567 {
    568 
    569 	if (buttons & 4) {
    570 		/* finger down on pad */
    571 		if (sc->sc_down == 0) {
    572 			sc->sc_down = 1;
    573 			sc->sc_x = 0;
    574 			sc->sc_y = 0;
    575 		}
    576 	}
    577 	if (buttons & 8) {
    578 		/* finger up */
    579 		if (sc->sc_down) {
    580 			if (((sc->sc_x * sc->sc_x +
    581 			    sc->sc_y * sc->sc_y) < 20) &&
    582 			    (sc->sc_wsmousedev)) {
    583 				/*
    584 				 * if there wasn't much movement between
    585 				 * finger down and up again we assume
    586 				 * someone tapped the pad and we just
    587 				 * send a mouse button event
    588 				 */
    589 				wsmouse_input(sc->sc_wsmousedev,
    590 				    1, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    591 			}
    592 			sc->sc_down = 0;
    593 		}
    594 	}
    595 }
    596 
    597 static void
    598 ams_mangle_4(struct ams_softc *sc, int buttons)
    599 {
    600 
    601 	if (buttons & 0x20) {
    602 		/* finger down on pad */
    603 		if (sc->sc_down == 0) {
    604 			sc->sc_down = 1;
    605 			sc->sc_x = 0;
    606 			sc->sc_y = 0;
    607 		}
    608 	}
    609 	if ((buttons & 0x20) == 0) {
    610 		/* finger up */
    611 		if (sc->sc_down) {
    612 			if (((sc->sc_x * sc->sc_x +
    613 			    sc->sc_y * sc->sc_y) < 20) &&
    614 			    (sc->sc_wsmousedev)) {
    615 				/*
    616 				 * if there wasn't much movement between
    617 				 * finger down and up again we assume
    618 				 * someone tapped the pad and we just
    619 				 * send a mouse button event
    620 				 */
    621 				wsmouse_input(sc->sc_wsmousedev,
    622 				    1, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    623 			}
    624 			sc->sc_down = 0;
    625 		}
    626 	}
    627 }
    628 
    629 int
    630 ams_enable(v)
    631 	void *v;
    632 {
    633 	return 0;
    634 }
    635 
    636 int
    637 ams_ioctl(v, cmd, data, flag, l)
    638 	void *v;
    639 	u_long cmd;
    640 	caddr_t data;
    641 	int flag;
    642 	struct lwp *l;
    643 {
    644 	return EPASSTHROUGH;
    645 }
    646 
    647 void
    648 ams_disable(v)
    649 	void *v;
    650 {
    651 }
    652 
    653 static void
    654 init_trackpad(struct ams_softc *sc)
    655 {
    656 	int cmd, res;
    657 	u_char buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    658 	u_char b2[10];
    659 	u_char b3[9] = {8, 0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
    660 
    661 	cmd = ADBTALK(sc->adbaddr, 1);
    662 	res = adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    663 
    664 	if (buffer[0] != 8)
    665 		return;
    666 
    667 	/* now whack the pad */
    668 	cmd = ADBLISTEN(sc->adbaddr, 1);
    669 	memcpy(b2, buffer, 10);
    670 	b2[7] = 0x0d;
    671 	adb_op_sync((Ptr)b2, NULL, (Ptr)0, cmd);
    672 
    673 	cmd = ADBLISTEN(sc->adbaddr, 2);
    674 	adb_op_sync((Ptr)b3, NULL, (Ptr)0, cmd);
    675 
    676 	cmd = ADBLISTEN(sc->adbaddr, 1);
    677 	b2[7] = 0x03;
    678 	adb_op_sync((Ptr)b2, NULL, (Ptr)0, cmd);
    679 
    680 	buffer[0] = 0;
    681 	cmd = ADBFLUSH(sc->adbaddr);
    682 	adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    683 }
    684 
    685