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