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