xref: /netbsd-src/sys/arch/mac68k/dev/ams.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: ams.c,v 1.9 2000/07/03 08:59:27 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 
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 		adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
292 
293 		if (buffer[2] == ADBMS_EXTENDED) {
294 			sc->handler_id = ADBMS_EXTENDED;
295 			cmd = ADBTALK(adbaddr, 1);
296 			if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
297 #ifdef ADB_DEBUG
298 				if (adb_debug)
299 					printf("adb: ems_init timed out\n");
300 #endif
301 			} else if (buffer[0] == 8) {
302 				/* we have a true EMP device */
303 				sc->sc_class = buffer[7];
304 				sc->sc_buttons = buffer[8];
305 				sc->sc_res = (int)*(short *)&buffer[5];
306 				bcopy(&(buffer[1]), sc->sc_devid, 4);
307 			} else if (buffer[1] == 0x9a &&
308 			    ((buffer[2] == 0x20) || (buffer[2] == 0x21))) {
309 				/*
310 				 * Set up non-EMP Mouseman/Trackman to put
311 				 * button bits in 3rd byte instead of sending
312 				 * via pseudo keyboard device.
313 				 */
314 				cmd = ADBLISTEN(adbaddr, 1);
315 				buffer[0]=2;
316 				buffer[1]=0x00;
317 				buffer[2]=0x81;
318 				adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
319 
320 				cmd = ADBLISTEN(adbaddr, 1);
321 				buffer[0]=2;
322 				buffer[1]=0x01;
323 				buffer[2]=0x81;
324 				adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
325 
326 				cmd = ADBLISTEN(adbaddr, 1);
327 				buffer[0]=2;
328 				buffer[1]=0x02;
329 				buffer[2]=0x81;
330 				adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
331 
332 				cmd = ADBLISTEN(adbaddr, 1);
333 				buffer[0]=2;
334 				buffer[1]=0x03;
335 				buffer[2]=0x38;
336 				adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
337 
338 				sc->sc_buttons = 3;
339 				sc->sc_res = 400;
340 				if (buffer[2] == 0x21)
341 					sc->sc_class = MSCLASS_TRACKBALL;
342 				else
343 					sc->sc_class = MSCLASS_MOUSE;
344 			} else
345 				/* unknown device? */;
346 		} else {
347 			/* Attempt to initialize as an A3 mouse */
348 			buffer[2] = 0x03; /* make handler ID 3 */
349 			cmd = ADBLISTEN(adbaddr, 3);
350 			if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
351 #ifdef ADB_DEBUG
352 				if (adb_debug)
353 					printf("adb: ems_init timed out\n");
354 #endif
355 				return;
356 			}
357 
358 			/*
359 			 * Check to see if successful, if not
360 			 * try to initialize it as other types
361 			 */
362 			cmd = ADBTALK(adbaddr, 3);
363 			adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
364 
365 			if (buffer[2] == ADBMS_MSA3) {
366 				sc->handler_id = ADBMS_MSA3;
367 				/* Initialize as above */
368 				cmd = ADBLISTEN(adbaddr, 2);
369 				/* listen 2 */
370 				buffer[0] = 3;
371 				buffer[1] = 0x00;
372 				/* Irrelevant, buffer has 0x77 */
373 				buffer[2] = 0x07;
374 				/*
375 				 * enable 3 button mode = 0111b,
376 				 * speed = normal
377 				 */
378 				adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
379 				sc->sc_buttons = 3;
380 				sc->sc_res = 300;
381 			} else {
382 				/* No special support for this mouse */
383 			}
384 		}
385 	}
386 }
387 
388 /*
389  * Handle putting the mouse data received from the ADB into
390  * an ADB event record.
391  */
392 void
393 ms_adbcomplete(buffer, data_area, adb_command)
394 	caddr_t buffer;
395 	caddr_t data_area;
396 	int adb_command;
397 {
398 	adb_event_t event;
399 	struct ams_softc *amsc;
400 	int adbaddr;
401 #ifdef ADB_DEBUG
402 	int i;
403 
404 	if (adb_debug)
405 		printf("adb: transaction completion\n");
406 #endif
407 
408 	adbaddr = ADB_CMDADDR(adb_command);
409 	amsc = (struct ams_softc *)data_area;
410 
411 	if ((amsc->handler_id == ADBMS_EXTENDED) && (amsc->sc_devid[0] == 0)) {
412 		/* massage the data to look like EMP data */
413 		if ((buffer[3] & 0x04) == 0x04)
414 			buffer[1] &= 0x7f;
415 		else
416 			buffer[1] |= 0x80;
417 		if ((buffer[3] & 0x02) == 0x02)
418 			buffer[2] &= 0x7f;
419 		else
420 			buffer[2] |= 0x80;
421 		if ((buffer[3] & 0x01) == 0x01)
422 			buffer[3] = 0x00;
423 		else
424 			buffer[3] = 0x80;
425 	}
426 
427 	event.addr = adbaddr;
428 	event.hand_id = amsc->handler_id;
429 	event.def_addr = amsc->origaddr;
430 	event.byte_count = buffer[0];
431 	memcpy(event.bytes, buffer + 1, event.byte_count);
432 
433 #ifdef ADB_DEBUG
434 	if (adb_debug) {
435 		printf("ams: from %d at %d (org %d) %d:", event.addr,
436 		    event.hand_id, event.def_addr, buffer[0]);
437 		for (i = 1; i <= buffer[0]; i++)
438 			printf(" %x", buffer[i]);
439 		printf("\n");
440 	}
441 #endif
442 
443 	microtime(&event.timestamp);
444 
445 	ms_processevent(&event, amsc);
446 }
447 
448 /*
449  * Given a mouse ADB event, record the button settings, calculate the
450  * x- and y-axis motion, and handoff the event to the appropriate subsystem.
451  */
452 static void
453 ms_processevent(event, amsc)
454 	adb_event_t *event;
455 	struct ams_softc *amsc;
456 {
457 	adb_event_t new_event;
458 	int i, button_bit, max_byte, mask, buttons;
459 
460 	new_event = *event;
461 	buttons = 0;
462 
463 	/*
464 	 * This should handle both plain ol' Apple mice and mice
465 	 * that claim to support the Extended Apple Mouse Protocol.
466 	 */
467 	max_byte = event->byte_count;
468 	button_bit = 1;
469 	switch (event->hand_id) {
470 	case ADBMS_USPEED:
471 	case ADBMS_UCONTOUR:
472 		/* MicroSpeed mouse and Contour mouse */
473 		if (max_byte == 4)
474 			buttons = (~event->bytes[2]) & 0xff;
475 		else
476 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
477 		break;
478 	case ADBMS_MSA3:
479 		/* Mouse Systems A3 mouse */
480 		if (max_byte == 3)
481 			buttons = (~event->bytes[2]) & 0x07;
482 		else
483 			buttons = (event->bytes[0] & 0x80) ? 0 : 1;
484 		break;
485 	default:
486 		/* Classic Mouse Protocol (up to 2 buttons) */
487 		for (i = 0; i < 2; i++, button_bit <<= 1)
488 			/* 0 when button down */
489 			if (!(event->bytes[i] & 0x80))
490 				buttons |= button_bit;
491 			else
492 				buttons &= ~button_bit;
493 		/* Extended Protocol (up to 6 more buttons) */
494 		for (mask = 0x80; i < max_byte;
495 		     i += (mask == 0x80), button_bit <<= 1) {
496 			/* 0 when button down */
497 			if (!(event->bytes[i] & mask))
498 				buttons |= button_bit;
499 			else
500 				buttons &= ~button_bit;
501 			mask = ((mask >> 4) & 0xf)
502 				| ((mask & 0xf) << 4);
503 		}
504 		break;
505 	}
506 	new_event.u.m.buttons = amsc->sc_mb | buttons;
507 	new_event.u.m.dx = ((signed int) (event->bytes[1] & 0x3f)) -
508 				((event->bytes[1] & 0x40) ? 64 : 0);
509 	new_event.u.m.dy = ((signed int) (event->bytes[0] & 0x3f)) -
510 				((event->bytes[0] & 0x40) ? 64 : 0);
511 
512 #if NAED > 0
513 	if (!aed_input(&new_event))
514 #endif
515 #if NWSMOUSE > 0
516 		if (amsc->sc_wsmousedev != NULL) /* wsmouse is attached? */
517 			wsmouse_input(amsc->sc_wsmousedev,
518 			    new_event.u.m.buttons,
519 			    new_event.u.m.dx, new_event.u.m.dy, 0, 0);
520 #else
521 		/* do nothing */ ;
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 (ENOTTY);
541 }
542 
543 void
544 ams_disable(v)
545 	void *v;
546 {
547 }
548