1 /* $NetBSD: ams.c,v 1.23 2020/06/09 11:44:48 tsutsui 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.23 2020/06/09 11:44:48 tsutsui 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 /* print out the type of mouse we have */ 130 switch (sc->handler_id) { 131 case ADBMS_100DPI: 132 printf("%d-button, %d dpi mouse\n", sc->sc_buttons, 133 (int)(sc->sc_res)); 134 break; 135 case ADBMS_200DPI: 136 sc->sc_res = 200; 137 printf("%d-button, %d dpi mouse\n", sc->sc_buttons, 138 (int)(sc->sc_res)); 139 break; 140 case ADBMS_MSA3: 141 printf("Mouse Systems A3 mouse, %d-button, %d dpi\n", 142 sc->sc_buttons, (int)(sc->sc_res)); 143 break; 144 case ADBMS_USPEED: 145 printf("MicroSpeed mouse, default parameters\n"); 146 break; 147 case ADBMS_UCONTOUR: 148 printf("Contour mouse, default parameters\n"); 149 break; 150 case ADBMS_EXTENDED: 151 if (sc->sc_devid[0] == '\0') { 152 printf("Logitech "); 153 switch (sc->sc_class) { 154 case MSCLASS_MOUSE: 155 printf("MouseMan (non-EMP) mouse"); 156 break; 157 case MSCLASS_TRACKBALL: 158 printf("TrackMan (non-EMP) trackball"); 159 break; 160 default: 161 printf("non-EMP relative positioning device"); 162 break; 163 } 164 printf("\n"); 165 } else { 166 printf("EMP "); 167 switch (sc->sc_class) { 168 case MSCLASS_TABLET: 169 printf("tablet"); 170 break; 171 case MSCLASS_MOUSE: 172 printf("mouse"); 173 break; 174 case MSCLASS_TRACKBALL: 175 printf("trackball"); 176 break; 177 default: 178 printf("unknown device"); 179 break; 180 } 181 printf(" <%s> %d-button, %d dpi\n", sc->sc_devid, 182 sc->sc_buttons, (int)(sc->sc_res)); 183 } 184 break; 185 default: 186 printf("relative positioning device (mouse?) (%d)\n", 187 sc->handler_id); 188 break; 189 } 190 error = SetADBInfo(&adbinfo, sc->adbaddr); 191 #ifdef ADB_DEBUG 192 if (adb_debug) 193 printf("ams: returned %d from SetADBInfo\n", error); 194 #endif 195 196 #if NWSMOUSE > 0 197 a.accessops = &ams_accessops; 198 a.accesscookie = sc; 199 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 200 #endif 201 } 202 203 204 /* 205 * Initialize extended mouse support -- probes devices as described 206 * in Inside Macintosh: Devices, Chapter 5 "ADB Manager". 207 * 208 * Extended Mouse Protocol is documented in TechNote HW1: 209 * "ADB - The Untold Story: Space Aliens Ate My Mouse" 210 * 211 * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe, 212 * Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan 213 */ 214 void 215 ems_init(struct ams_softc *sc) 216 { 217 int adbaddr; 218 short cmd; 219 u_char buffer[9]; 220 221 adbaddr = sc->adbaddr; 222 if (sc->origaddr != ADBADDR_MS) 223 return; 224 if (sc->handler_id == ADBMS_USPEED || 225 sc->handler_id == ADBMS_UCONTOUR) { 226 /* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */ 227 cmd = ADBLISTEN(adbaddr, 1); 228 229 /* 230 * To setup the MicroSpeed or the Contour, it appears 231 * that we can send the following command to the mouse 232 * and then expect data back in the form: 233 * buffer[0] = 4 (bytes) 234 * buffer[1], buffer[2] as std. mouse 235 * buffer[3] = buffer[4] = 0xff when no buttons 236 * are down. When button N down, bit N is clear. 237 * buffer[4]'s locking mask enables a 238 * click to toggle the button down state--sort of 239 * like the "Easy Access" shift/control/etc. keys. 240 * buffer[3]'s alternative speed mask enables using 241 * different speed when the corr. button is down 242 */ 243 buffer[0] = 4; 244 buffer[1] = 0x00; /* Alternative speed */ 245 buffer[2] = 0x00; /* speed = maximum */ 246 buffer[3] = 0x10; /* enable extended protocol, 247 * lower bits = alt. speed mask 248 * = 0000b 249 */ 250 buffer[4] = 0x07; /* Locking mask = 0000b, 251 * enable buttons = 0111b 252 */ 253 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 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 = ADBTALK(adbaddr, 3); 263 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) { 264 #ifdef ADB_DEBUG 265 if (adb_debug) 266 printf("adb: ems_init timed out\n"); 267 #endif 268 return; 269 } 270 271 /* Attempt to initialize Extended Mouse Protocol */ 272 buffer[2] = 4; /* make handler ID 4 */ 273 cmd = ADBLISTEN(adbaddr, 3); 274 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) { 275 #ifdef ADB_DEBUG 276 if (adb_debug) 277 printf("adb: ems_init timed out\n"); 278 #endif 279 return; 280 } 281 282 /* 283 * Check to see if successful, if not 284 * try to initialize it as other types 285 */ 286 cmd = ADBTALK(adbaddr, 3); 287 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 && 288 buffer[2] == ADBMS_EXTENDED) { 289 sc->handler_id = ADBMS_EXTENDED; 290 cmd = ADBTALK(adbaddr, 1); 291 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) { 292 #ifdef ADB_DEBUG 293 if (adb_debug) 294 printf("adb: ems_init timed out\n"); 295 #endif 296 } else if (buffer[0] == 8) { 297 /* we have a true EMP device */ 298 sc->sc_class = buffer[7]; 299 sc->sc_buttons = buffer[8]; 300 sc->sc_res = (int)*(short *)&buffer[5]; 301 memcpy(sc->sc_devid, &(buffer[1]), 4); 302 } else if (buffer[1] == 0x9a && 303 ((buffer[2] == 0x20) || (buffer[2] == 0x21))) { 304 /* 305 * Set up non-EMP Mouseman/Trackman to put 306 * button bits in 3rd byte instead of sending 307 * via pseudo keyboard device. 308 */ 309 cmd = ADBLISTEN(adbaddr, 1); 310 buffer[0]=2; 311 buffer[1]=0x00; 312 buffer[2]=0x81; 313 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 314 315 cmd = ADBLISTEN(adbaddr, 1); 316 buffer[0]=2; 317 buffer[1]=0x01; 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]=0x02; 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]=0x03; 330 buffer[2]=0x38; 331 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 332 333 sc->sc_buttons = 3; 334 sc->sc_res = 400; 335 if (buffer[2] == 0x21) 336 sc->sc_class = MSCLASS_TRACKBALL; 337 else 338 sc->sc_class = MSCLASS_MOUSE; 339 } else 340 /* unknown device? */; 341 } else { 342 /* Attempt to initialize as an A3 mouse */ 343 buffer[2] = 0x03; /* make handler ID 3 */ 344 cmd = ADBLISTEN(adbaddr, 3); 345 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd)) { 346 #ifdef ADB_DEBUG 347 if (adb_debug) 348 printf("adb: ems_init timed out\n"); 349 #endif 350 return; 351 } 352 353 /* 354 * Check to see if successful, if not 355 * try to initialize it as other types 356 */ 357 cmd = ADBTALK(adbaddr, 3); 358 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 359 && buffer[2] == ADBMS_MSA3) { 360 sc->handler_id = ADBMS_MSA3; 361 /* Initialize as above */ 362 cmd = ADBLISTEN(adbaddr, 2); 363 /* listen 2 */ 364 buffer[0] = 3; 365 buffer[1] = 0x00; 366 /* Irrelevant, buffer has 0x77 */ 367 buffer[2] = 0x07; 368 /* 369 * enable 3 button mode = 0111b, 370 * speed = normal 371 */ 372 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd); 373 sc->sc_buttons = 3; 374 sc->sc_res = 300; 375 } else { 376 /* No special support for this mouse */ 377 } 378 } 379 } 380 } 381 382 /* 383 * Handle putting the mouse data received from the ADB into 384 * an ADB event record. 385 */ 386 void 387 ms_adbcomplete(void *buffer, void *data_area, int adb_command) 388 { 389 adb_event_t event; 390 struct ams_softc *amsc; 391 uint8_t *buf = (uint8_t*)buffer; 392 int adbaddr; 393 #ifdef ADB_DEBUG 394 int i; 395 396 if (adb_debug) 397 printf("adb: transaction completion\n"); 398 #endif 399 400 adbaddr = ADB_CMDADDR(adb_command); 401 amsc = (struct ams_softc *)data_area; 402 403 if ((amsc->handler_id == ADBMS_EXTENDED) && (amsc->sc_devid[0] == 0)) { 404 /* massage the data to look like EMP data */ 405 if ((buf[3] & 0x04) == 0x04) 406 buf[1] &= 0x7f; 407 else 408 buf[1] |= 0x80; 409 if ((buf[3] & 0x02) == 0x02) 410 buf[2] &= 0x7f; 411 else 412 buf[2] |= 0x80; 413 if ((buf[3] & 0x01) == 0x01) 414 buf[3] = 0x00; 415 else 416 buf[3] = 0x80; 417 } 418 419 event.addr = adbaddr; 420 event.hand_id = amsc->handler_id; 421 event.def_addr = amsc->origaddr; 422 event.byte_count = buf[0]; 423 memcpy(event.bytes, buf + 1, event.byte_count); 424 425 #ifdef ADB_DEBUG 426 if (adb_debug) { 427 printf("ams: from %d at %d (org %d) %u:", event.addr, 428 event.hand_id, event.def_addr, buf[0]); 429 for (i = 1; i <= buf[0]; i++) 430 printf(" %x", buf[i]); 431 printf("\n"); 432 } 433 #endif 434 435 microtime(&event.timestamp); 436 437 ms_processevent(&event, amsc); 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 static void 445 ms_processevent(adb_event_t *event, struct ams_softc *amsc) 446 { 447 adb_event_t new_event; 448 int i, button_bit, max_byte, mask, buttons; 449 450 new_event = *event; 451 buttons = 0; 452 453 /* 454 * This should handle both plain ol' Apple mice and mice 455 * that claim to support the Extended Apple Mouse Protocol. 456 */ 457 max_byte = event->byte_count; 458 button_bit = 1; 459 switch (event->hand_id) { 460 case ADBMS_USPEED: 461 case ADBMS_UCONTOUR: 462 /* MicroSpeed mouse and Contour mouse */ 463 if (max_byte == 4) 464 buttons = (~event->bytes[2]) & 0xff; 465 else 466 buttons = (event->bytes[0] & 0x80) ? 0 : 1; 467 break; 468 case ADBMS_MSA3: 469 /* Mouse Systems A3 mouse */ 470 if (max_byte == 3) 471 buttons = (~event->bytes[2]) & 0x07; 472 else 473 buttons = (event->bytes[0] & 0x80) ? 0 : 1; 474 break; 475 default: 476 /* Classic Mouse Protocol (up to 2 buttons) */ 477 for (i = 0; i < 2; i++, button_bit <<= 1) 478 /* 0 when button down */ 479 if (!(event->bytes[i] & 0x80)) 480 buttons |= button_bit; 481 else 482 buttons &= ~button_bit; 483 /* Extended Protocol (up to 6 more buttons) */ 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 new_event.u.m.buttons = amsc->sc_mb | buttons; 497 new_event.u.m.dx = ((signed int) (event->bytes[1] & 0x3f)) - 498 ((event->bytes[1] & 0x40) ? 64 : 0); 499 new_event.u.m.dy = ((signed int) (event->bytes[0] & 0x3f)) - 500 ((event->bytes[0] & 0x40) ? 64 : 0); 501 502 #if NAED > 0 503 if (!aed_input(&new_event)) 504 #endif 505 #if NWSMOUSE > 0 506 if (amsc->sc_wsmousedev != NULL) /* wsmouse is attached? */ 507 wsmouse_input(amsc->sc_wsmousedev, 508 new_event.u.m.buttons, 509 new_event.u.m.dx, -new_event.u.m.dy, 0, 0, 510 WSMOUSE_INPUT_DELTA); 511 #else 512 /* do nothing */ ; 513 #endif 514 } 515 516 int 517 ams_enable(void *v) 518 { 519 return 0; 520 } 521 522 int 523 ams_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 524 { 525 526 switch (cmd) { 527 case WSMOUSEIO_GTYPE: 528 *(u_int *)data = WSMOUSE_TYPE_ADB; 529 break; 530 } 531 return EPASSTHROUGH; 532 } 533 534 void 535 ams_disable(void *v) 536 { 537 } 538