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