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