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