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