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