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