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