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