Home | History | Annotate | Line # | Download | only in dev
ams.c revision 1.23
      1 /*	$NetBSD: ams.c,v 1.23 2007/01/14 23:59:06 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.23 2007/01/14 23:59:06 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/sysctl.h>
     45 #include <sys/sysctl.h>
     46 
     47 #include <machine/autoconf.h>
     48 
     49 #include <dev/wscons/wsconsio.h>
     50 #include <dev/wscons/wsmousevar.h>
     51 
     52 #include <macppc/dev/adbvar.h>
     53 #include <macppc/dev/aedvar.h>
     54 #include <macppc/dev/amsvar.h>
     55 
     56 #include "aed.h"
     57 
     58 /*
     59  * Function declarations.
     60  */
     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 *);
     66 
     67 /* Driver definition. */
     68 CFATTACH_DECL(ams, sizeof(struct ams_softc),
     69     amsmatch, amsattach, NULL, NULL);
     70 
     71 int ams_enable(void *);
     72 int ams_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     73 void ams_disable(void *);
     74 
     75 /*
     76  * handle tapping the trackpad
     77  * different pads report different button counts and use slightly different
     78  * protocols
     79  */
     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);
     83 
     84 const struct wsmouse_accessops ams_accessops = {
     85 	ams_enable,
     86 	ams_ioctl,
     87 	ams_disable,
     88 };
     89 
     90 static int
     91 amsmatch(struct device *parent, struct cfdata *cf, 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(struct device *parent, struct device *self, void *aux)
    103 {
    104 	ADBSetInfoBlock adbinfo;
    105 	struct ams_softc *sc = (struct ams_softc *)self;
    106 	struct adb_attach_args *aa_args = aux;
    107 	int error;
    108 	struct wsmousedev_attach_args a;
    109 
    110 	sc->origaddr = aa_args->origaddr;
    111 	sc->adbaddr = aa_args->adbaddr;
    112 	sc->handler_id = aa_args->handler_id;
    113 
    114 	sc->sc_class = MSCLASS_MOUSE;
    115 	sc->sc_buttons = 1;
    116 	sc->sc_res = 100;
    117 	sc->sc_devid[0] = 0;
    118 	sc->sc_devid[4] = 0;
    119 
    120 	adbinfo.siServiceRtPtr = (Ptr)ms_adbcomplete;
    121 	adbinfo.siDataAreaAddr = (caddr_t)sc;
    122 
    123 	ems_init(sc);
    124 
    125 	/* print out the type of mouse we have */
    126 	switch (sc->handler_id) {
    127 	case ADBMS_100DPI:
    128 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
    129 		    (int)(sc->sc_res));
    130 		break;
    131 	case ADBMS_200DPI:
    132 		sc->sc_res = 200;
    133 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
    134 		    (int)(sc->sc_res));
    135 		break;
    136 	case ADBMS_MSA3:
    137 		printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
    138 		    sc->sc_buttons, (int)(sc->sc_res));
    139 		break;
    140 	case ADBMS_USPEED:
    141 		printf("MicroSpeed mouse, default parameters\n");
    142 		break;
    143 	case ADBMS_UCONTOUR:
    144 		printf("Contour mouse, default parameters\n");
    145 		break;
    146 	case ADBMS_TURBO:
    147 		printf("Kensington Turbo Mouse\n");
    148 		break;
    149 	case ADBMS_EXTENDED:
    150 		if (sc->sc_devid[0] == '\0') {
    151 			printf("Logitech ");
    152 			switch (sc->sc_class) {
    153 			case MSCLASS_MOUSE:
    154 				printf("MouseMan (non-EMP) mouse");
    155 				break;
    156 			case MSCLASS_TRACKBALL:
    157 				printf("TrackMan (non-EMP) trackball");
    158 				break;
    159 			default:
    160 				printf("non-EMP relative positioning device");
    161 				break;
    162 			}
    163 			printf("\n");
    164 		} else {
    165 			printf("EMP ");
    166 			switch (sc->sc_class) {
    167 			case MSCLASS_TABLET:
    168 				printf("tablet");
    169 				break;
    170 			case MSCLASS_MOUSE:
    171 				printf("mouse");
    172 				break;
    173 			case MSCLASS_TRACKBALL:
    174 				printf("trackball");
    175 				break;
    176 			case MSCLASS_TRACKPAD:
    177 				printf("trackpad");
    178 				init_trackpad(sc);
    179 				break;
    180 			default:
    181 				printf("unknown device");
    182 				break;
    183 			}
    184 			printf(" <%s> %d-button, %d dpi\n", sc->sc_devid,
    185 			    sc->sc_buttons, (int)(sc->sc_res));
    186 		}
    187 		break;
    188 	default:
    189 		printf("relative positioning device (mouse?) (%d)\n",
    190 			sc->handler_id);
    191 		break;
    192 	}
    193 	error = SetADBInfo(&adbinfo, sc->adbaddr);
    194 #ifdef ADB_DEBUG
    195 	if (adb_debug)
    196 		printf("ams: returned %d from SetADBInfo\n", error);
    197 #endif
    198 
    199 	a.accessops = &ams_accessops;
    200 	a.accesscookie = sc;
    201 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    202 }
    203 
    204 
    205 /*
    206  * Initialize extended mouse support -- probes devices as described
    207  * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
    208  *
    209  * Extended Mouse Protocol is documented in TechNote HW1:
    210  * 	"ADB - The Untold Story:  Space Aliens Ate My Mouse"
    211  *
    212  * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
    213  * 	     Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
    214  */
    215 void
    216 ems_init(struct ams_softc *sc)
    217 {
    218 	int adbaddr;
    219 	short cmd;
    220 	u_char buffer[9];
    221 
    222 	adbaddr = sc->adbaddr;
    223 	if (sc->origaddr != ADBADDR_MS)
    224 		return;
    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);
    229 
    230 		/*
    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
    243 		 */
    244 		buffer[0] = 4;
    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
    249 					 *            = 0000b
    250 					 */
    251 		buffer[4] = 0x07;	/* Locking mask = 0000b,
    252 					 * enable buttons = 0111b
    253 					 */
    254 		adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    255 
    256 		sc->sc_buttons = 3;
    257 		sc->sc_res = 200;
    258 		return;
    259 	}
    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 };
    266 
    267 		buffer[0] = 0;
    268 		adb_op_sync((Ptr)buffer, NULL, (Ptr)0, ADBFLUSH(adbaddr));
    269 
    270 		adb_op_sync((Ptr)data1, NULL, (Ptr)0, ADBLISTEN(adbaddr, 2));
    271 
    272 		buffer[0] = 0;
    273 		adb_op_sync((Ptr)buffer, NULL, (Ptr)0, ADBFLUSH(adbaddr));
    274 
    275 		adb_op_sync((Ptr)data2, NULL, (Ptr)0, ADBLISTEN(adbaddr, 2));
    276 		return;
    277 	}
    278 	if ((sc->handler_id == ADBMS_100DPI) ||
    279 	    (sc->handler_id == ADBMS_200DPI)) {
    280 		/* found a mouse */
    281 		cmd = ADBTALK(adbaddr, 3);
    282 		if (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd)) {
    283 #ifdef ADB_DEBUG
    284 			if (adb_debug)
    285 				printf("adb: ems_init timed out\n");
    286 #endif
    287 			return;
    288 		}
    289 
    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)) {
    294 #ifdef ADB_DEBUG
    295 			if (adb_debug)
    296 				printf("adb: ems_init timed out\n");
    297 #endif
    298 			return;
    299 		}
    300 
    301 		/*
    302 		 * Check to see if successful, if not
    303 		 * try to initialize it as other types
    304 		 */
    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)) {
    311 #ifdef ADB_DEBUG
    312 				if (adb_debug)
    313 					printf("adb: ems_init timed out\n");
    314 #endif
    315 			} else if (buffer[0] == 8) {
    316 				/* we have a true EMP device */
    317 #ifdef ADB_PRINT_EMP
    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]);
    321 #endif
    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))) {
    328 				/*
    329 				 * Set up non-EMP Mouseman/Trackman to put
    330 				 * button bits in 3rd byte instead of sending
    331 				 * via pseudo keyboard device.
    332 				 */
    333 				cmd = ADBLISTEN(adbaddr, 1);
    334 				buffer[0]=2;
    335 				buffer[1]=0x00;
    336 				buffer[2]=0x81;
    337 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    338 
    339 				cmd = ADBLISTEN(adbaddr, 1);
    340 				buffer[0]=2;
    341 				buffer[1]=0x01;
    342 				buffer[2]=0x81;
    343 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    344 
    345 				cmd = ADBLISTEN(adbaddr, 1);
    346 				buffer[0]=2;
    347 				buffer[1]=0x02;
    348 				buffer[2]=0x81;
    349 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    350 
    351 				cmd = ADBLISTEN(adbaddr, 1);
    352 				buffer[0]=2;
    353 				buffer[1]=0x03;
    354 				buffer[2]=0x38;
    355 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    356 
    357 				sc->sc_buttons = 3;
    358 				sc->sc_res = 400;
    359 				if (buffer[2] == 0x21)
    360 					sc->sc_class = MSCLASS_TRACKBALL;
    361 				else
    362 					sc->sc_class = MSCLASS_MOUSE;
    363 			} else
    364 				/* unknown device? */;
    365 		} else {
    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)) {
    370 #ifdef ADB_DEBUG
    371 				if (adb_debug)
    372 					printf("adb: ems_init timed out\n");
    373 #endif
    374 				return;
    375 			}
    376 
    377 			/*
    378 			 * Check to see if successful, if not
    379 			 * try to initialize it as other types
    380 			 */
    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);
    387 				/* listen 2 */
    388 				buffer[0] = 3;
    389 				buffer[1] = 0x00;
    390 				/* Irrelevant, buffer has 0x77 */
    391 				buffer[2] = 0x07;
    392 				/*
    393 				 * enable 3 button mode = 0111b,
    394 				 * speed = normal
    395 				 */
    396 				adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    397 				sc->sc_buttons = 3;
    398 				sc->sc_res = 300;
    399 			} else {
    400 				/* No special support for this mouse */
    401 			}
    402 		}
    403 	}
    404 }
    405 
    406 /*
    407  * Handle putting the mouse data received from the ADB into
    408  * an ADB event record.
    409  */
    410 void
    411 ms_adbcomplete(caddr_t buffer, caddr_t data_area, int adb_command)
    412 {
    413 	adb_event_t event;
    414 	struct ams_softc *sc;
    415 	int adbaddr;
    416 #ifdef ADB_DEBUG
    417 	int i;
    418 
    419 	if (adb_debug)
    420 		printf("adb: transaction completion\n");
    421 #endif
    422 
    423 	adbaddr = ADB_CMDADDR(adb_command);
    424 	sc = (struct ams_softc *)data_area;
    425 
    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)
    429 			buffer[1] &= 0x7f;
    430 		else
    431 			buffer[1] |= 0x80;
    432 		if ((buffer[3] & 0x02) == 0x02)
    433 			buffer[2] &= 0x7f;
    434 		else
    435 			buffer[2] |= 0x80;
    436 		if ((buffer[3] & 0x01) == 0x01)
    437 			buffer[3] = 0x00;
    438 		else
    439 			buffer[3] = 0x80;
    440 	}
    441 
    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);
    447 
    448 #ifdef ADB_DEBUG
    449 	if (adb_debug) {
    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]);
    454 		printf("\n");
    455 	}
    456 #endif
    457 
    458 	microtime(&event.timestamp);
    459 
    460 	ms_processevent(&event, sc);
    461 }
    462 
    463 /*
    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.
    466  */
    467 static void
    468 ms_processevent(adb_event_t *event, struct ams_softc *sc)
    469 {
    470 	adb_event_t new_event;
    471 	int i, button_bit, max_byte, mask, buttons, dx, dy;
    472 
    473 	new_event = *event;
    474 	buttons = 0;
    475 
    476 	/*
    477 	 * This should handle both plain ol' Apple mice and mice
    478 	 * that claim to support the Extended Apple Mouse Protocol.
    479 	 */
    480 	max_byte = event->byte_count;
    481 	button_bit = 1;
    482 	switch (event->hand_id) {
    483 	case ADBMS_USPEED:
    484 	case ADBMS_UCONTOUR:
    485 		/* MicroSpeed mouse and Contour mouse */
    486 		if (max_byte == 4)
    487 			buttons = (~event->bytes[2]) & 0xff;
    488 		else
    489 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    490 		break;
    491 	case ADBMS_MSA3:
    492 		/* Mouse Systems A3 mouse */
    493 		if (max_byte == 3)
    494 			buttons = (~event->bytes[2]) & 0x07;
    495 		else
    496 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    497 		break;
    498 	default:
    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;
    504 			else
    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;
    512 			else
    513 				buttons &= ~button_bit;
    514 			mask = ((mask >> 4) & 0xf)
    515 				| ((mask & 0xf) << 4);
    516 		}
    517 		break;
    518 	}
    519 
    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);
    524 
    525 	if (sc->sc_class == MSCLASS_TRACKPAD) {
    526 		if (sc->sc_tapping == 1) {
    527 
    528 			if (sc->sc_down) {
    529 				/* finger is down - collect motion data */
    530 				sc->sc_x += dx;
    531 				sc->sc_y += dy;
    532 			}
    533 			switch (sc->sc_buttons) {
    534 				case 2:
    535 					ams_mangle_2(sc, buttons);
    536 					break;
    537 				case 4:
    538 					ams_mangle_4(sc, buttons);
    539 					break;
    540 			}
    541 		}
    542 		/* filter the pseudo-buttons out */
    543 		buttons &= 1;
    544 	}
    545 
    546 	new_event.u.m.buttons = sc->sc_mb | buttons;
    547 	new_event.u.m.dx = dx;
    548 	new_event.u.m.dy = dy;
    549 
    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);
    554 #if NAED > 0
    555 	aed_input(&new_event);
    556 #endif
    557 }
    558 
    559 static void
    560 ams_mangle_2(struct ams_softc *sc, int buttons)
    561 {
    562 
    563 	if (buttons & 4) {
    564 		/* finger down on pad */
    565 		if (sc->sc_down == 0) {
    566 			sc->sc_down = 1;
    567 			sc->sc_x = 0;
    568 			sc->sc_y = 0;
    569 		}
    570 	}
    571 	if (buttons & 8) {
    572 		/* finger up */
    573 		if (sc->sc_down) {
    574 			if (((sc->sc_x * sc->sc_x +
    575 			    sc->sc_y * sc->sc_y) < 20) &&
    576 			    (sc->sc_wsmousedev)) {
    577 				/*
    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
    582 				 */
    583 				wsmouse_input(sc->sc_wsmousedev,
    584 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    585 			}
    586 			sc->sc_down = 0;
    587 		}
    588 	}
    589 }
    590 
    591 static void
    592 ams_mangle_4(struct ams_softc *sc, int buttons)
    593 {
    594 
    595 	if (buttons & 0x20) {
    596 		/* finger down on pad */
    597 		if (sc->sc_down == 0) {
    598 			sc->sc_down = 1;
    599 			sc->sc_x = 0;
    600 			sc->sc_y = 0;
    601 		}
    602 	}
    603 	if ((buttons & 0x20) == 0) {
    604 		/* finger up */
    605 		if (sc->sc_down) {
    606 			if (((sc->sc_x * sc->sc_x +
    607 			    sc->sc_y * sc->sc_y) < 20) &&
    608 			    (sc->sc_wsmousedev)) {
    609 				/*
    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
    614 				 */
    615 				wsmouse_input(sc->sc_wsmousedev,
    616 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
    617 			}
    618 			sc->sc_down = 0;
    619 		}
    620 	}
    621 }
    622 
    623 int
    624 ams_enable(void *v)
    625 {
    626 	return 0;
    627 }
    628 
    629 int
    630 ams_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
    631 {
    632 	return EPASSTHROUGH;
    633 }
    634 
    635 void
    636 ams_disable(void *v)
    637 {
    638 }
    639 
    640 static void
    641 init_trackpad(struct ams_softc *sc)
    642 {
    643 	struct sysctlnode *me = NULL, *node = NULL;
    644 	int cmd, res, ret;
    645 	u_char buffer[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    646 	u_char b2[10];
    647 	u_char b3[9] = {8, 0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
    648 
    649 	cmd = ADBTALK(sc->adbaddr, 1);
    650 	res = adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    651 
    652 	if (buffer[0] != 8)
    653 		return;
    654 
    655 	/* now whack the pad */
    656 	cmd = ADBLISTEN(sc->adbaddr, 1);
    657 	memcpy(b2, buffer, 10);
    658 	b2[7] = 0x0d;
    659 	adb_op_sync((Ptr)b2, NULL, (Ptr)0, cmd);
    660 
    661 	cmd = ADBLISTEN(sc->adbaddr, 2);
    662 	adb_op_sync((Ptr)b3, NULL, (Ptr)0, cmd);
    663 
    664 	cmd = ADBLISTEN(sc->adbaddr, 1);
    665 	b2[7] = 0x03;
    666 	adb_op_sync((Ptr)b2, NULL, (Ptr)0, cmd);
    667 
    668 	buffer[0] = 0;
    669 	cmd = ADBFLUSH(sc->adbaddr);
    670 	adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd);
    671 
    672 	/*
    673 	 * setup a sysctl node to control wether tapping the pad should
    674 	 * trigger mouse button events
    675 	 */
    676 
    677 	sc->sc_tapping = 1;
    678 
    679 	ret = sysctl_createv(NULL, 0, NULL, (const struct sysctlnode **)&me,
    680 	    CTLFLAG_READWRITE,
    681 	    CTLTYPE_NODE, sc->sc_dev.dv_xname, NULL,
    682 	    NULL, 0, NULL, 0,
    683 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
    684 
    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);
    690 	if (node != NULL) {
    691 		node->sysctl_data = sc;
    692 	}
    693 }
    694 
    695 static int
    696 sysctl_ams_tap(SYSCTLFN_ARGS)
    697 {
    698 	struct sysctlnode node = *rnode;
    699 	struct ams_softc *sc = node.sysctl_data;
    700 
    701 	node.sysctl_idata = sc->sc_tapping;
    702 
    703 	if (newp) {
    704 
    705 		/* we're asked to write */
    706 		node.sysctl_data = &sc->sc_tapping;
    707 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    708 
    709 			sc->sc_tapping = (node.sysctl_idata == 0) ? 0 : 1;
    710 			return 0;
    711 		}
    712 		return EINVAL;
    713 	} else {
    714 
    715 		node.sysctl_size = 4;
    716 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    717 	}
    718 
    719 	return 0;
    720 }
    721 
    722 SYSCTL_SETUP(sysctl_ams_setup, "sysctl ams subtree setup")
    723 {
    724 
    725 	sysctl_createv(NULL, 0, NULL, NULL,
    726 		       CTLFLAG_PERMANENT,
    727 		       CTLTYPE_NODE, "machdep", NULL,
    728 		       NULL, 0, NULL, 0,
    729 		       CTL_MACHDEP, CTL_EOL);
    730 }
    731 
    732