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