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