ams.c revision 1.28 1 /* $NetBSD: ams.c,v 1.28 2024/12/16 03:45:52 nat 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.28 2024/12/16 03:45:52 nat 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(device_t, cfdata_t, void *);
63 static void amsattach(device_t, device_t, void *);
64 static void ems_init(struct ams_softc *);
65 static void ms_processevent(adb_event_t *, 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_NEW(ams, sizeof(struct ams_softc),
78 amsmatch, amsattach, NULL, NULL);
79
80 extern struct cfdriver ams_cd;
81
82 int ams_enable(void *);
83 int ams_ioctl(void *, u_long, void *, int, struct lwp *);
84 void ams_disable(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(device_t parent, cfdata_t cf, void *aux)
94 {
95 struct adb_attach_args * aa_args = (struct adb_attach_args *)aux;
96
97 if (aa_args->origaddr == ADBADDR_MS)
98 return 1;
99 else
100 return 0;
101 }
102
103 static void
104 amsattach(device_t parent, device_t self, void *aux)
105 {
106 ADBSetInfoBlock adbinfo;
107 struct ams_softc *sc = device_private(self);
108 struct adb_attach_args * aa_args = (struct adb_attach_args *)aux;
109 int error __unused;
110 #if NWSMOUSE > 0
111 struct wsmousedev_attach_args a;
112 #endif
113
114 sc->origaddr = aa_args->origaddr;
115 sc->adbaddr = aa_args->adbaddr;
116 sc->handler_id = aa_args->handler_id;
117
118 sc->sc_class = MSCLASS_MOUSE;
119 sc->sc_buttons = 1;
120 sc->sc_res = 100;
121 sc->sc_devid[0] = 0;
122 sc->sc_devid[4] = 0;
123
124 adbinfo.siServiceRtPtr = (Ptr)adb_ms_asmcomplete;
125 adbinfo.siDataAreaAddr = (void *)sc;
126
127 ems_init(sc);
128
129 if (mac68k_machine.machineid == MACH_MACPB500 &&
130 sc->sc_class == MSCLASS_MOUSE && sc->sc_devid[0] == '\0')
131 sc->handler_id = ADBMS_200DPI;
132
133 /* print out the type of mouse we have */
134 switch (sc->handler_id) {
135 case ADBMS_100DPI:
136 printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
137 (int)(sc->sc_res));
138 break;
139 case ADBMS_200DPI:
140 sc->sc_res = 200;
141 printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
142 (int)(sc->sc_res));
143 break;
144 case ADBMS_MSA3:
145 printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
146 sc->sc_buttons, (int)(sc->sc_res));
147 break;
148 case ADBMS_USPEED:
149 printf("MicroSpeed mouse, default parameters\n");
150 break;
151 case ADBMS_UCONTOUR:
152 printf("Contour mouse, default parameters\n");
153 break;
154 case ADBMS_EXTENDED:
155 if (sc->sc_devid[0] == '\0') {
156 printf("Logitech ");
157 switch (sc->sc_class) {
158 case MSCLASS_MOUSE:
159 printf("MouseMan (non-EMP) mouse");
160 break;
161 case MSCLASS_TRACKBALL:
162 printf("TrackMan (non-EMP) trackball");
163 break;
164 default:
165 printf("non-EMP relative positioning device");
166 break;
167 }
168 printf("\n");
169 } else {
170 printf("EMP ");
171 switch (sc->sc_class) {
172 case MSCLASS_TABLET:
173 printf("tablet");
174 break;
175 case MSCLASS_MOUSE:
176 printf("mouse");
177 break;
178 case MSCLASS_TRACKBALL:
179 printf("trackball");
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("ams: returned %d from SetADBInfo\n", error);
198 #endif
199
200 #if NWSMOUSE > 0
201 a.accessops = &ams_accessops;
202 a.accesscookie = sc;
203 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
204 #endif
205 }
206
207
208 /*
209 * Initialize extended mouse support -- probes devices as described
210 * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
211 *
212 * Extended Mouse Protocol is documented in TechNote HW1:
213 * "ADB - The Untold Story: Space Aliens Ate My Mouse"
214 *
215 * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
216 * Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
217 */
218 void
219 ems_init(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 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
292 buffer[2] == ADBMS_EXTENDED) {
293 sc->handler_id = ADBMS_EXTENDED;
294 cmd = ADBTALK(adbaddr, 1);
295 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
296 #ifdef ADB_DEBUG
297 if (adb_debug)
298 printf("adb: ems_init timed out\n");
299 #endif
300 } else if (buffer[0] == 8) {
301 /* we have a true EMP device */
302 sc->sc_class = buffer[7];
303 sc->sc_buttons = buffer[8];
304 sc->sc_res = (int)*(short *)&buffer[5];
305 memcpy(sc->sc_devid, &(buffer[1]), 4);
306 } else if (buffer[1] == 0x9a &&
307 ((buffer[2] == 0x20) || (buffer[2] == 0x21))) {
308 /*
309 * Set up non-EMP Mouseman/Trackman to put
310 * button bits in 3rd byte instead of sending
311 * via pseudo keyboard device.
312 */
313 cmd = ADBLISTEN(adbaddr, 1);
314 buffer[0]=2;
315 buffer[1]=0x00;
316 buffer[2]=0x81;
317 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
318
319 cmd = ADBLISTEN(adbaddr, 1);
320 buffer[0]=2;
321 buffer[1]=0x01;
322 buffer[2]=0x81;
323 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
324
325 cmd = ADBLISTEN(adbaddr, 1);
326 buffer[0]=2;
327 buffer[1]=0x02;
328 buffer[2]=0x81;
329 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
330
331 cmd = ADBLISTEN(adbaddr, 1);
332 buffer[0]=2;
333 buffer[1]=0x03;
334 buffer[2]=0x38;
335 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
336
337 sc->sc_buttons = 3;
338 sc->sc_res = 400;
339 if (buffer[2] == 0x21)
340 sc->sc_class = MSCLASS_TRACKBALL;
341 else
342 sc->sc_class = MSCLASS_MOUSE;
343 } else
344 /* unknown device? */;
345 } else {
346 /* Attempt to initialize as an A3 mouse */
347 buffer[2] = 0x03; /* make handler ID 3 */
348 cmd = ADBLISTEN(adbaddr, 3);
349 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) {
350 #ifdef ADB_DEBUG
351 if (adb_debug)
352 printf("adb: ems_init timed out\n");
353 #endif
354 return;
355 }
356
357 /*
358 * Check to see if successful, if not
359 * try to initialize it as other types
360 */
361 cmd = ADBTALK(adbaddr, 3);
362 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0
363 && buffer[2] == ADBMS_MSA3) {
364 sc->handler_id = ADBMS_MSA3;
365 /* Initialize as above */
366 cmd = ADBLISTEN(adbaddr, 2);
367 /* listen 2 */
368 buffer[0] = 3;
369 buffer[1] = 0x00;
370 /* Irrelevant, buffer has 0x77 */
371 buffer[2] = 0x07;
372 /*
373 * enable 3 button mode = 0111b,
374 * speed = normal
375 */
376 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
377 sc->sc_buttons = 3;
378 sc->sc_res = 300;
379 } else {
380 /* No special support for this mouse */
381 }
382 }
383 }
384 }
385
386 /*
387 * Handle putting the mouse data received from the ADB into
388 * an ADB event record.
389 */
390 void
391 ms_adbcomplete(void *buffer, void *data_area, int adb_command)
392 {
393 adb_event_t event;
394 struct ams_softc *amsc;
395 uint8_t *buf = (uint8_t*)buffer;
396 int adbaddr;
397 #ifdef ADB_DEBUG
398 int i;
399
400 if (adb_debug)
401 printf("adb: transaction completion\n");
402 #endif
403
404 adbaddr = ADB_CMDADDR(adb_command);
405 amsc = (struct ams_softc *)data_area;
406
407 if ((amsc->handler_id == ADBMS_EXTENDED) && (amsc->sc_devid[0] == 0)) {
408 /* massage the data to look like EMP data */
409 if ((buf[3] & 0x04) == 0x04)
410 buf[1] &= 0x7f;
411 else
412 buf[1] |= 0x80;
413 if ((buf[3] & 0x02) == 0x02)
414 buf[2] &= 0x7f;
415 else
416 buf[2] |= 0x80;
417 if ((buf[3] & 0x01) == 0x01)
418 buf[3] = 0x00;
419 else
420 buf[3] = 0x80;
421 }
422
423 event.addr = adbaddr;
424 event.hand_id = amsc->handler_id;
425 event.def_addr = amsc->origaddr;
426 event.byte_count = buf[0];
427 memcpy(event.bytes, buf + 1, event.byte_count);
428
429 #ifdef ADB_DEBUG
430 if (adb_debug) {
431 printf("ams: from %d at %d (org %d) %u:", event.addr,
432 event.hand_id, event.def_addr, buf[0]);
433 for (i = 1; i <= buf[0]; i++)
434 printf(" %x", buf[i]);
435 printf("\n");
436 }
437 #endif
438
439 microtime(&event.timestamp);
440
441 ms_processevent(&event, amsc);
442 }
443
444 /*
445 * Given a mouse ADB event, record the button settings, calculate the
446 * x- and y-axis motion, and handoff the event to the appropriate subsystem.
447 */
448 static void
449 ms_processevent(adb_event_t *event, struct ams_softc *amsc)
450 {
451 adb_event_t new_event;
452 int i, button_bit, max_byte, mask, buttons;
453
454 new_event = *event;
455 buttons = 0;
456
457 /*
458 * This should handle both plain ol' Apple mice and mice
459 * that claim to support the Extended Apple Mouse Protocol.
460 */
461 max_byte = event->byte_count;
462 button_bit = 1;
463 switch (event->hand_id) {
464 case ADBMS_USPEED:
465 case ADBMS_UCONTOUR:
466 /* MicroSpeed mouse and Contour mouse */
467 if (max_byte == 4)
468 buttons = (~event->bytes[2]) & 0xff;
469 else
470 buttons = (event->bytes[0] & 0x80) ? 0 : 1;
471 break;
472 case ADBMS_MSA3:
473 /* Mouse Systems A3 mouse */
474 if (max_byte == 3)
475 buttons = (~event->bytes[2]) & 0x07;
476 else
477 buttons = (event->bytes[0] & 0x80) ? 0 : 1;
478 break;
479 default:
480 /* Classic Mouse Protocol (up to 2 buttons) */
481 for (i = 0; i < 2; i++, button_bit <<= 1)
482 /* 0 when button down */
483 if (!(event->bytes[i] & 0x80))
484 buttons |= button_bit;
485 else
486 buttons &= ~button_bit;
487 /* Extended Protocol (up to 6 more buttons) */
488 for (mask = 0x80; i < max_byte;
489 i += (mask == 0x80), button_bit <<= 1) {
490 /* 0 when button down */
491 if (!(event->bytes[i] & mask))
492 buttons |= button_bit;
493 else
494 buttons &= ~button_bit;
495 mask = ((mask >> 4) & 0xf)
496 | ((mask & 0xf) << 4);
497 }
498 break;
499 }
500 new_event.u.m.buttons = amsc->sc_mb | buttons;
501 new_event.u.m.dx = ((signed int) (event->bytes[1] & 0x3f)) -
502 ((event->bytes[1] & 0x40) ? 64 : 0);
503 new_event.u.m.dy = ((signed int) (event->bytes[0] & 0x3f)) -
504 ((event->bytes[0] & 0x40) ? 64 : 0);
505
506 int phys_buttons = new_event.u.m.buttons;
507 #if NAED > 0
508 if (!aed_input(&new_event))
509 #endif
510 #if NWSMOUSE > 0
511 if (amsc->sc_wsmousedev != NULL) { /* wsmouse is attached? */
512 if ((phys_buttons != new_event.u.m.buttons) &&
513 (phys_buttons & 1))
514 amsc->sc_oldbuttons =
515 new_event.u.m.buttons & ~1;
516 else if ((phys_buttons == new_event.u.m.buttons) &&
517 (amsc->sc_oldbuttons == 0))
518 amsc->sc_oldbuttons = phys_buttons;
519
520 if (phys_buttons == 0)
521 amsc->sc_oldbuttons = 0;
522 wsmouse_input(amsc->sc_wsmousedev,
523 amsc->sc_oldbuttons,
524 new_event.u.m.dx, -new_event.u.m.dy, 0, 0,
525 WSMOUSE_INPUT_DELTA);
526 }
527 #else
528 /* do nothing */ ;
529 #endif
530 }
531
532 int
533 ams_enable(void *v)
534 {
535 return 0;
536 }
537
538 int
539 ams_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
540 {
541
542 switch (cmd) {
543 case WSMOUSEIO_GTYPE:
544 *(u_int *)data = WSMOUSE_TYPE_ADB;
545 return 0;
546 }
547 return EPASSTHROUGH;
548 }
549
550 void
551 ams_disable(void *v)
552 {
553 }
554