ams.c revision 1.14 1 /* $NetBSD: ams.c,v 1.14 2003/07/15 02:43:16 lukem 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/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ams.c,v 1.14 2003/07/15 02:43:16 lukem Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/fcntl.h>
39 #include <sys/poll.h>
40 #include <sys/select.h>
41 #include <sys/proc.h>
42 #include <sys/signalvar.h>
43 #include <sys/systm.h>
44
45 #include "aed.h"
46 #include "wsmouse.h"
47
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wsmousevar.h>
50
51 #include <machine/autoconf.h>
52 #include <machine/keyboard.h>
53
54 #include <mac68k/mac68k/macrom.h>
55 #include <mac68k/dev/adbvar.h>
56 #include <mac68k/dev/aedvar.h>
57 #include <mac68k/dev/amsvar.h>
58
59 /*
60 * Function declarations.
61 */
62 static int amsmatch __P((struct device *, struct cfdata *, void *));
63 static void amsattach __P((struct device *, struct device *, void *));
64 static void ems_init __P((struct ams_softc *));
65 static void ms_processevent __P((adb_event_t *event, struct ams_softc *));
66
67 /*
68 * Global variables.
69 */
70 extern int kbd_polling; /* Are we polling (Debugger mode)? from kbd.c */
71
72 /*
73 * Local variables.
74 */
75
76 /* Driver definition. */
77 CFATTACH_DECL(ams, sizeof(struct ams_softc),
78 amsmatch, amsattach, NULL, NULL);
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;
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 = ADBLISTEN(adbaddr, 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 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
260
261 sc->sc_buttons = 3;
262 sc->sc_res = 200;
263 return;
264 }
265 if ((sc->handler_id == ADBMS_100DPI) ||
266 (sc->handler_id == ADBMS_200DPI)) {
267 /* found a mouse */
268 cmd = ADBTALK(adbaddr, 3);
269 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
270 #ifdef ADB_DEBUG
271 if (adb_debug)
272 printf("adb: ems_init timed out\n");
273 #endif
274 return;
275 }
276
277 /* Attempt to initialize Extended Mouse Protocol */
278 buffer[2] = 4; /* make handler ID 4 */
279 cmd = ADBLISTEN(adbaddr, 3);
280 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
281 #ifdef ADB_DEBUG
282 if (adb_debug)
283 printf("adb: ems_init timed out\n");
284 #endif
285 return;
286 }
287
288 /*
289 * Check to see if successful, if not
290 * try to initialize it as other types
291 */
292 cmd = ADBTALK(adbaddr, 3);
293 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
294 buffer[2] == ADBMS_EXTENDED) {
295 sc->handler_id = ADBMS_EXTENDED;
296 cmd = ADBTALK(adbaddr, 1);
297 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
298 #ifdef ADB_DEBUG
299 if (adb_debug)
300 printf("adb: ems_init timed out\n");
301 #endif
302 } else if (buffer[0] == 8) {
303 /* we have a true EMP device */
304 sc->sc_class = buffer[7];
305 sc->sc_buttons = buffer[8];
306 sc->sc_res = (int)*(short *)&buffer[5];
307 bcopy(&(buffer[1]), sc->sc_devid, 4);
308 } else if (buffer[1] == 0x9a &&
309 ((buffer[2] == 0x20) || (buffer[2] == 0x21))) {
310 /*
311 * Set up non-EMP Mouseman/Trackman to put
312 * button bits in 3rd byte instead of sending
313 * via pseudo keyboard device.
314 */
315 cmd = ADBLISTEN(adbaddr, 1);
316 buffer[0]=2;
317 buffer[1]=0x00;
318 buffer[2]=0x81;
319 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
320
321 cmd = ADBLISTEN(adbaddr, 1);
322 buffer[0]=2;
323 buffer[1]=0x01;
324 buffer[2]=0x81;
325 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
326
327 cmd = ADBLISTEN(adbaddr, 1);
328 buffer[0]=2;
329 buffer[1]=0x02;
330 buffer[2]=0x81;
331 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
332
333 cmd = ADBLISTEN(adbaddr, 1);
334 buffer[0]=2;
335 buffer[1]=0x03;
336 buffer[2]=0x38;
337 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
338
339 sc->sc_buttons = 3;
340 sc->sc_res = 400;
341 if (buffer[2] == 0x21)
342 sc->sc_class = MSCLASS_TRACKBALL;
343 else
344 sc->sc_class = MSCLASS_MOUSE;
345 } else
346 /* unknown device? */;
347 } else {
348 /* Attempt to initialize as an A3 mouse */
349 buffer[2] = 0x03; /* make handler ID 3 */
350 cmd = ADBLISTEN(adbaddr, 3);
351 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
352 #ifdef ADB_DEBUG
353 if (adb_debug)
354 printf("adb: ems_init timed out\n");
355 #endif
356 return;
357 }
358
359 /*
360 * Check to see if successful, if not
361 * try to initialize it as other types
362 */
363 cmd = ADBTALK(adbaddr, 3);
364 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0
365 && 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 (EPASSTHROUGH);
541 }
542
543 void
544 ams_disable(v)
545 void *v;
546 {
547 }
548