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