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