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