Home | History | Annotate | Line # | Download | only in dev
ams.c revision 1.6
      1 /*	$NetBSD: ams.c,v 1.6 2000/02/14 07:01:46 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 = ((adbaddr<<4)&0xF0)|0x9;	/* listen 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 = ((adbaddr << 4) & 0xf0) | 0x3;
    273 
    274 		extdms_done = 0;
    275 		cmd = (cmd & 0xf3) | 0x0c; /* talk command */
    276 		ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    277 		    (Ptr)&extdms_done, cmd);
    278 
    279 		/* Wait until done, but no more than 2 secs */
    280 		count = 40000;
    281 		while (!extdms_done && count-- > 0)
    282 			delay(50);
    283 
    284 		if (!extdms_done) {
    285 #ifdef ADB_DEBUG
    286 			if (adb_debug)
    287 				printf("adb: extdms_init timed out\n");
    288 #endif
    289 			return;
    290 		}
    291 
    292 		/* Attempt to initialize Extended Mouse Protocol */
    293 		buffer[2] = '\004'; /* make handler ID 4 */
    294 		extdms_done = 0;
    295 		cmd = (cmd & 0xf3) | 0x08; /* listen command */
    296 		ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    297 		    (Ptr)&extdms_done, cmd);
    298 		while (!extdms_done)
    299 			/* busy wait until done */;
    300 
    301 		/*
    302 		 * Check to see if successful, if not
    303 		 * try to initialize it as other types
    304 		 */
    305 		cmd = ((adbaddr << 4) & 0xf0) | 0x3;
    306 		extdms_done = 0;
    307 		cmd = (cmd & 0xf3) | 0x0c; /* talk command */
    308 		ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    309 		    (Ptr)&extdms_done, cmd);
    310 		while (!extdms_done)
    311 			/* busy wait until done */;
    312 
    313 		if (buffer[2] == ADBMS_EXTENDED) {
    314 			sc->handler_id = ADBMS_EXTENDED;
    315 			extdms_done = 0;
    316 			/* talk register 1 */
    317 			ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    318 			    (Ptr)&extdms_done, (adbaddr << 4) | 0xd);
    319 			while (!extdms_done)
    320 				/* busy-wait until done */;
    321 			if (buffer[0] == 8) {
    322 				/* we have a true EMP device */
    323 				sc->sc_class = buffer[7];
    324 				sc->sc_buttons = buffer[8];
    325 				sc->sc_res = (int)*(short *)&buffer[5];
    326 				bcopy(&(buffer[1]), sc->sc_devid, 4);
    327 			} else if (buffer[1] == 0x9a &&
    328 			    ((buffer[2] == 0x20) || (buffer[2] == 0x21))) {
    329 				/*
    330 				 * Set up non-EMP Mouseman/Trackman to put
    331 				 * button bits in 3rd byte instead of sending
    332 				 * via pseudo keyboard device.
    333 				 */
    334 				extdms_done = 0;
    335 				/* listen register 1 */
    336 				buffer[0]=2;
    337 				buffer[1]=0x00;
    338 				buffer[2]=0x81;
    339 				ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    340 				    (Ptr)&extdms_done, (adbaddr << 4) | 0x9);
    341 				while (!extdms_done)
    342 					/* busy-wait until done */;
    343 				extdms_done = 0;
    344 				/* listen register 1 */
    345 				buffer[0]=2;
    346 				buffer[1]=0x01;
    347 				buffer[2]=0x81;
    348 				ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    349 				    (Ptr)&extdms_done, (adbaddr << 4) | 0x9);
    350 				while (!extdms_done)
    351 					/* busy-wait until done */;
    352 				extdms_done = 0;
    353 				/* listen register 1 */
    354 				buffer[0]=2;
    355 				buffer[1]=0x02;
    356 				buffer[2]=0x81;
    357 				ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    358 				    (Ptr)&extdms_done, (adbaddr << 4) | 0x9);
    359 				while (!extdms_done)
    360 					/* busy-wait until done */;
    361 				extdms_done = 0;
    362 				/* listen register 1 */
    363 				buffer[0]=2;
    364 				buffer[1]=0x03;
    365 				buffer[2]=0x38;
    366 				ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    367 				      (Ptr)&extdms_done, (adbaddr << 4) | 0x9);
    368 				while (!extdms_done)
    369 					/* busy-wait until done */;
    370 				sc->sc_buttons = 3;
    371 				sc->sc_res = 400;
    372 				if (buffer[2] == 0x21)
    373 					sc->sc_class = MSCLASS_TRACKBALL;
    374 				else
    375 					sc->sc_class = MSCLASS_MOUSE;
    376 			} else
    377 				/* unknown device? */;
    378 		} else {
    379 			/* Attempt to initialize as an A3 mouse */
    380 			buffer[2] = 0x03; /* make handler ID 3 */
    381 			extdms_done = 0;
    382 			cmd = (cmd & 0xf3) | 0x08; /* listen command */
    383 			ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    384 			    (Ptr)&extdms_done, cmd);
    385 			while (!extdms_done)
    386 				/* busy wait until done */;
    387 
    388 			/*
    389 			 * Check to see if successful, if not
    390 			 * try to initialize it as other types
    391 			 */
    392 			cmd = ((adbaddr << 4) & 0xf0) | 0x3;
    393 			extdms_done = 0;
    394 			cmd = (cmd & 0xf3) | 0x0c; /* talk command */
    395 			ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    396 			    (Ptr)&extdms_done, cmd);
    397 			while (!extdms_done)
    398 				/* busy wait until done */;
    399 
    400 			if (buffer[2] == ADBMS_MSA3) {
    401 				sc->handler_id = ADBMS_MSA3;
    402 				/* Initialize as above */
    403 				cmd = ((adbaddr << 4) & 0xF0) | 0xA;
    404 				/* listen 2 */
    405 				buffer[0] = 3;
    406 				buffer[1] = 0x00;
    407 				/* Irrelevant, buffer has 0x77 */
    408 				buffer[2] = 0x07;
    409 				/*
    410 				 * enable 3 button mode = 0111b,
    411 				 * speed = normal
    412 				 */
    413 				extdms_done = 0;
    414 				ADBOp((Ptr)buffer, (Ptr)extdms_complete,
    415 				    (Ptr)&extdms_done, cmd);
    416 				while (!extdms_done)
    417 					/* busy wait until done */;
    418 				sc->sc_buttons = 3;
    419 				sc->sc_res = 300;
    420 			} else {
    421 				/* No special support for this mouse */
    422 			}
    423 		}
    424 	}
    425 }
    426 
    427 /*
    428  * Handle putting the mouse data received from the ADB into
    429  * an ADB event record.
    430  */
    431 void
    432 ms_adbcomplete(buffer, data_area, adb_command)
    433 	caddr_t buffer;
    434 	caddr_t data_area;
    435 	int adb_command;
    436 {
    437 	adb_event_t event;
    438 	struct ams_softc *amsc;
    439 	int adbaddr;
    440 #ifdef ADB_DEBUG
    441 	int i;
    442 
    443 	if (adb_debug)
    444 		printf("adb: transaction completion\n");
    445 #endif
    446 
    447 	adbaddr = (adb_command & 0xf0) >> 4;
    448 	amsc = (struct ams_softc *)data_area;
    449 
    450 	if ((amsc->handler_id == ADBMS_EXTENDED) && (amsc->sc_devid[0] == 0)) {
    451 		/* massage the data to look like EMP data */
    452 		if ((buffer[3] & 0x04) == 0x04)
    453 			buffer[1] &= 0x7f;
    454 		else
    455 			buffer[1] |= 0x80;
    456 		if ((buffer[3] & 0x02) == 0x02)
    457 			buffer[2] &= 0x7f;
    458 		else
    459 			buffer[2] |= 0x80;
    460 		if ((buffer[3] & 0x01) == 0x01)
    461 			buffer[3] = 0x00;
    462 		else
    463 			buffer[3] = 0x80;
    464 	}
    465 
    466 	event.addr = adbaddr;
    467 	event.hand_id = amsc->handler_id;
    468 	event.def_addr = amsc->origaddr;
    469 	event.byte_count = buffer[0];
    470 	memcpy(event.bytes, buffer + 1, event.byte_count);
    471 
    472 #ifdef ADB_DEBUG
    473 	if (adb_debug) {
    474 		printf("ams: from %d at %d (org %d) %d:", event.addr,
    475 		    event.hand_id, event.def_addr, buffer[0]);
    476 		for (i = 1; i <= buffer[0]; i++)
    477 			printf(" %x", buffer[i]);
    478 		printf("\n");
    479 	}
    480 #endif
    481 
    482 	microtime(&event.timestamp);
    483 
    484 	ms_processevent(&event, amsc);
    485 }
    486 
    487 /*
    488  * Given a mouse ADB event, record the button settings, calculate the
    489  * x- and y-axis motion, and handoff the event to the appropriate subsystem.
    490  */
    491 static void
    492 ms_processevent(event, amsc)
    493 	adb_event_t *event;
    494 	struct ams_softc *amsc;
    495 {
    496 	adb_event_t new_event;
    497 	int i, button_bit, max_byte, mask, buttons;
    498 
    499 	new_event = *event;
    500 	buttons = 0;
    501 
    502 	/*
    503 	 * This should handle both plain ol' Apple mice and mice
    504 	 * that claim to support the Extended Apple Mouse Protocol.
    505 	 */
    506 	max_byte = event->byte_count;
    507 	button_bit = 1;
    508 	switch (event->hand_id) {
    509 	case ADBMS_USPEED:
    510 	case ADBMS_UCONTOUR:
    511 		/* MicroSpeed mouse and Contour mouse */
    512 		if (max_byte == 4)
    513 			buttons = (~event->bytes[2]) & 0xff;
    514 		else
    515 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    516 		break;
    517 	case ADBMS_MSA3:
    518 		/* Mouse Systems A3 mouse */
    519 		if (max_byte == 3)
    520 			buttons = (~event->bytes[2]) & 0x07;
    521 		else
    522 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
    523 		break;
    524 	default:
    525 		/* Classic Mouse Protocol (up to 2 buttons) */
    526 		for (i = 0; i < 2; i++, button_bit <<= 1)
    527 			/* 0 when button down */
    528 			if (!(event->bytes[i] & 0x80))
    529 				buttons |= button_bit;
    530 			else
    531 				buttons &= ~button_bit;
    532 		/* Extended Protocol (up to 6 more buttons) */
    533 		for (mask = 0x80; i < max_byte;
    534 		     i += (mask == 0x80), button_bit <<= 1) {
    535 			/* 0 when button down */
    536 			if (!(event->bytes[i] & mask))
    537 				buttons |= button_bit;
    538 			else
    539 				buttons &= ~button_bit;
    540 			mask = ((mask >> 4) & 0xf)
    541 				| ((mask & 0xf) << 4);
    542 		}
    543 		break;
    544 	}
    545 	new_event.u.m.buttons = amsc->sc_mb | buttons;
    546 	new_event.u.m.dx = ((signed int) (event->bytes[1] & 0x3f)) -
    547 				((event->bytes[1] & 0x40) ? 64 : 0);
    548 	new_event.u.m.dy = ((signed int) (event->bytes[0] & 0x3f)) -
    549 				((event->bytes[0] & 0x40) ? 64 : 0);
    550 
    551 #if NAED > 0
    552 	if (!aed_input(&new_event))
    553 #endif
    554 #if NWSMOUSE > 0
    555 		wsmouse_input(amsc->sc_wsmousedev, new_event.u.m.buttons,
    556 		    new_event.u.m.dx, new_event.u.m.dy, 0, 0);
    557 #else
    558 		/* do nothing */ ;
    559 #endif
    560 }
    561 
    562 int
    563 ams_enable(v)
    564 	void *v;
    565 {
    566 	return 0;
    567 }
    568 
    569 int
    570 ams_ioctl(v, cmd, data, flag, p)
    571 	void *v;
    572 	u_long cmd;
    573 	caddr_t data;
    574 	int flag;
    575 	struct proc *p;
    576 {
    577 	return (ENOTTY);
    578 }
    579 
    580 void
    581 ams_disable(v)
    582 	void *v;
    583 {
    584 }
    585