FreeBSD/Linux Kernel Cross Reference
sys/dev/adb/ams.c
1 /* $OpenBSD: ams.c,v 1.8 2022/04/06 18:59:27 naddy Exp $ */
2 /* $NetBSD: ams.c,v 1.11 2000/12/19 03:13:40 tsubai Exp $ */
3
4 /*
5 * Copyright (C) 1998 Colin Wood
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Colin Wood.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/device.h>
36 #include <sys/systm.h>
37
38 #include <machine/autoconf.h>
39
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wscons/wsmousevar.h>
42
43 #include <dev/adb/adb.h>
44 #include <dev/adb/amsvar.h>
45
46 /*
47 * Function declarations.
48 */
49 int amsmatch(struct device *, void *, void *);
50 void amsattach(struct device *, struct device *, void *);
51
52 /* Driver definition. */
53 const struct cfattach ams_ca = {
54 sizeof(struct ams_softc), amsmatch, amsattach
55 };
56 /* Driver definition. */
57 struct cfdriver ams_cd = {
58 NULL, "ams", DV_DULL
59 };
60
61 int ams_enable(void *);
62 int ams_ioctl(void *, u_long, caddr_t, int, struct proc *);
63 void ams_disable(void *);
64
65 const struct wsmouse_accessops ams_accessops = {
66 ams_enable,
67 ams_ioctl,
68 ams_disable,
69 };
70
71 void ems_init(struct ams_softc *);
72 void ms_adbcomplete(caddr_t buffer, caddr_t data_area, int adb_command);
73 void ms_handoff(adb_event_t *event, struct ams_softc *);
74 void ms_processevent(adb_event_t *event, struct ams_softc *);
75
76 int
77 amsmatch(struct device *parent, void *cf, void *aux)
78 {
79 struct adb_attach_args *aa_args = aux;
80
81 if (strcmp(aa_args->name, adb_device_name) != 0)
82 return (0);
83
84 if (aa_args->origaddr == ADBADDR_MS)
85 return 1;
86 else
87 return 0;
88 }
89
90 void
91 amsattach(struct device *parent, struct device *self, void *aux)
92 {
93 ADBSetInfoBlock adbinfo;
94 struct ams_softc *sc = (struct ams_softc *)self;
95 struct adb_attach_args *aa_args = aux;
96 int error;
97 struct wsmousedev_attach_args a;
98
99 sc->origaddr = aa_args->origaddr;
100 sc->adbaddr = aa_args->adbaddr;
101 sc->handler_id = aa_args->handler_id;
102
103 sc->sc_class = MSCLASS_MOUSE;
104 sc->sc_buttons = 1;
105 sc->sc_res = 100;
106 sc->sc_devid[0] = 0;
107 sc->sc_devid[4] = 0;
108
109 adbinfo.siServiceRtPtr = (Ptr)ms_adbcomplete;
110 adbinfo.siDataAreaAddr = (caddr_t)sc;
111
112 ems_init(sc);
113
114 /* print out the type of mouse we have */
115 printf(": ");
116 switch (sc->handler_id) {
117 case ADBMS_200DPI:
118 sc->sc_res = 200;
119 /* FALLTHROUGH */
120 case ADBMS_100DPI:
121 printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
122 (int)(sc->sc_res));
123 break;
124 case ADBMS_MSA3:
125 printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
126 sc->sc_buttons, (int)(sc->sc_res));
127 break;
128 case ADBMS_USPEED:
129 printf("MicroSpeed mouse, default parameters\n");
130 break;
131 case ADBMS_UCONTOUR:
132 printf("Contour mouse, default parameters\n");
133 break;
134 case ADBMS_TURBO:
135 printf("Kensington Turbo Mouse\n");
136 break;
137 case ADBMS_EXTENDED:
138 if (sc->sc_devid[0] == '\0') {
139 printf("Logitech ");
140 switch (sc->sc_class) {
141 case MSCLASS_MOUSE:
142 printf("MouseMan (non-EMP) mouse");
143 break;
144 case MSCLASS_TRACKBALL:
145 printf("TrackMan (non-EMP) trackball");
146 break;
147 default:
148 printf("non-EMP relative positioning device");
149 break;
150 }
151 printf("\n");
152 } else {
153 printf("EMP ");
154 switch (sc->sc_class) {
155 case MSCLASS_TABLET:
156 printf("tablet");
157 break;
158 case MSCLASS_MOUSE:
159 printf("mouse");
160 break;
161 case MSCLASS_TRACKBALL:
162 printf("trackball");
163 break;
164 case MSCLASS_TRACKPAD:
165 printf("trackpad");
166 break;
167 default:
168 printf("unknown device");
169 break;
170 }
171 printf(" <%s> %d-button, %d dpi\n", sc->sc_devid,
172 sc->sc_buttons, (int)(sc->sc_res));
173 }
174 break;
175 default:
176 printf("relative positioning device (mouse?) (%d)\n",
177 sc->handler_id);
178 break;
179 }
180 error = set_adb_info(&adbinfo, sc->adbaddr);
181 #ifdef ADB_DEBUG
182 if (adb_debug)
183 printf("ams: returned %d from set_adb_info\n", error);
184 #endif
185
186 a.accessops = &ams_accessops;
187 a.accesscookie = sc;
188 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
189 }
190
191
192 /*
193 * Initialize extended mouse support -- probes devices as described
194 * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
195 *
196 * Extended Mouse Protocol is documented in TechNote HW1:
197 * "ADB - The Untold Story: Space Aliens Ate My Mouse"
198 *
199 * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
200 * Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
201 */
202 void
203 ems_init(struct ams_softc *sc)
204 {
205 int adbaddr;
206 short cmd;
207 u_char buffer[9];
208
209 adbaddr = sc->adbaddr;
210 if (sc->origaddr != ADBADDR_MS)
211 return;
212 if (sc->handler_id == ADBMS_USPEED ||
213 sc->handler_id == ADBMS_UCONTOUR) {
214 /* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
215 cmd = ADBLISTEN(adbaddr, 1);
216
217 /*
218 * To setup the MicroSpeed or the Contour, it appears
219 * that we can send the following command to the mouse
220 * and then expect data back in the form:
221 * buffer[0] = 4 (bytes)
222 * buffer[1], buffer[2] as std. mouse
223 * buffer[3] = buffer[4] = 0xff when no buttons
224 * are down. When button N down, bit N is clear.
225 * buffer[4]'s locking mask enables a
226 * click to toggle the button down state--sort of
227 * like the "Easy Access" shift/control/etc. keys.
228 * buffer[3]'s alternative speed mask enables using
229 * different speed when the corr. button is down
230 */
231 buffer[0] = 4;
232 buffer[1] = 0x00; /* Alternative speed */
233 buffer[2] = 0x00; /* speed = maximum */
234 buffer[3] = 0x10; /* enable extended protocol,
235 * lower bits = alt. speed mask
236 * = 0000b
237 */
238 buffer[4] = 0x07; /* Locking mask = 0000b,
239 * enable buttons = 0111b
240 */
241 adb_op_sync((Ptr)buffer, cmd);
242
243 sc->sc_buttons = 3;
244 sc->sc_res = 200;
245 return;
246 }
247 if (sc->handler_id == ADBMS_TURBO) {
248 /* Found Kensington Turbo Mouse */
249 static u_char data1[] =
250 { 8, 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
251 static u_char data2[] =
252 { 8, 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
253
254 buffer[0] = 0;
255 adb_op_sync((Ptr)buffer, ADBFLUSH(adbaddr));
256
257 adb_op_sync((Ptr)data1, ADBLISTEN(adbaddr, 2));
258
259 buffer[0] = 0;
260 adb_op_sync((Ptr)buffer, ADBFLUSH(adbaddr));
261
262 adb_op_sync((Ptr)data2, ADBLISTEN(adbaddr, 2));
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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 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(caddr_t buffer, caddr_t data_area, int adb_command)
394 {
395 adb_event_t event;
396 struct ams_softc *sc;
397 int adbaddr;
398 #ifdef ADB_DEBUG
399 int i;
400
401 if (adb_debug)
402 printf("adb: transaction completion\n");
403 #endif
404
405 adbaddr = ADB_CMDADDR(adb_command);
406 sc = (struct ams_softc *)data_area;
407
408 if ((sc->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) {
409 /* massage the data to look like EMP data */
410 if ((buffer[3] & 0x04) == 0x04)
411 buffer[1] &= 0x7f;
412 else
413 buffer[1] |= 0x80;
414 if ((buffer[3] & 0x02) == 0x02)
415 buffer[2] &= 0x7f;
416 else
417 buffer[2] |= 0x80;
418 if ((buffer[3] & 0x01) == 0x01)
419 buffer[3] = 0x00;
420 else
421 buffer[3] = 0x80;
422 }
423
424 event.byte_count = buffer[0];
425 memcpy(event.bytes, buffer + 1, event.byte_count);
426
427 #ifdef ADB_DEBUG
428 if (adb_debug) {
429 printf("ams: from %d at %d (org %d) %d:", adbaddr,
430 sc->handler_id, sc->origaddr, buffer[0]);
431 for (i = 1; i <= buffer[0]; i++)
432 printf(" %x", buffer[i]);
433 printf("\n");
434 }
435 #endif
436
437 ms_processevent(&event, sc);
438 }
439
440 /*
441 * Given a mouse ADB event, record the button settings, calculate the
442 * x- and y-axis motion, and handoff the event to the appropriate subsystem.
443 */
444 void
445 ms_processevent(adb_event_t *event, struct ams_softc *sc)
446 {
447 int i, button_bit, max_byte, mask;
448 int dx, dy, buttons;
449
450 buttons = 0;
451
452 /*
453 * This should handle both plain ol' Apple mice and mice
454 * that claim to support the Extended Apple Mouse Protocol.
455 */
456 max_byte = event->byte_count;
457 button_bit = 1;
458 switch (sc->handler_id) {
459 case ADBMS_USPEED:
460 case ADBMS_UCONTOUR:
461 /* MicroSpeed mouse and Contour mouse */
462 if (max_byte == 4)
463 buttons = (~event->bytes[2]) & 0xff;
464 else
465 buttons = (event->bytes[0] & 0x80) ? 0 : 1;
466 break;
467 case ADBMS_MSA3:
468 /* Mouse Systems A3 mouse */
469 if (max_byte == 3)
470 buttons = (~event->bytes[2]) & 0x07;
471 else
472 buttons = (event->bytes[0] & 0x80) ? 0 : 1;
473 break;
474 default:
475 /* Classic Mouse Protocol (up to 2 buttons) */
476 for (i = 0; i < 2; i++, button_bit <<= 1)
477 /* 0 when button down */
478 if (!(event->bytes[i] & 0x80))
479 buttons |= button_bit;
480 else
481 buttons &= ~button_bit;
482 /* Extended Protocol (up to 6 more buttons) */
483 if (sc->sc_class == MSCLASS_MOUSE)
484 for (mask = 0x80; i < max_byte;
485 i += (mask == 0x80), button_bit <<= 1) {
486 /* 0 when button down */
487 if (!(event->bytes[i] & mask))
488 buttons |= button_bit;
489 else
490 buttons &= ~button_bit;
491 mask = ((mask >> 4) & 0xf)
492 | ((mask & 0xf) << 4);
493 }
494 break;
495 }
496
497 buttons |= sc->sc_mb;
498 dx = ((signed int) (event->bytes[1] & 0x3f)) -
499 ((event->bytes[1] & 0x40) ? 64 : 0);
500 dy = ((signed int) (event->bytes[0] & 0x3f)) -
501 ((event->bytes[0] & 0x40) ? 64 : 0);
502
503 if (sc->sc_wsmousedev)
504 WSMOUSE_INPUT(sc->sc_wsmousedev, buttons, dx, -dy, 0, 0);
505 }
506
507 int
508 ams_enable(void *v)
509 {
510 return 0;
511 }
512
513 int
514 ams_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
515 {
516 switch (cmd) {
517 case WSMOUSEIO_GTYPE:
518 *(u_int *)data = WSMOUSE_TYPE_ADB;
519 return (0);
520 }
521
522 return -1;
523 }
524
525 void
526 ams_disable(void *v)
527 {
528 }
Cache object: f80cfc50798b5a84a5325bf8969c3777
|