1 /* $NetBSD: adb_ms.c,v 1.16 2014/10/29 00:48:12 macallan Exp $ */ 2 3 /* 4 * Copyright (C) 1998 Colin Wood 5 * Copyright (C) 2006, 2007 Michael Lorenz 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/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: adb_ms.c,v 1.16 2014/10/29 00:48:12 macallan Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/device.h> 39 #include <sys/fcntl.h> 40 #include <sys/poll.h> 41 #include <sys/select.h> 42 #include <sys/proc.h> 43 #include <sys/signalvar.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/sysctl.h> 47 48 #include <machine/autoconf.h> 49 50 #include <dev/wscons/wsconsio.h> 51 #include <dev/wscons/wsmousevar.h> 52 53 #include <machine/adbsys.h> 54 #include <dev/adb/adbvar.h> 55 56 #include "adbdebug.h" 57 58 #ifdef ADBMS_DEBUG 59 #define DPRINTF printf 60 #else 61 #define DPRINTF while (0) printf 62 #endif 63 64 /* 65 * State info, per mouse instance. 66 */ 67 struct adbms_softc { 68 device_t sc_dev; 69 struct adb_device *sc_adbdev; 70 struct adb_bus_accessops *sc_ops; 71 72 /* Extended Mouse Protocol info, faked for non-EMP mice */ 73 u_int8_t sc_class; /* mouse class (mouse, trackball) */ 74 u_int8_t sc_buttons; /* number of buttons */ 75 u_int32_t sc_res; /* mouse resolution (dpi) */ 76 char sc_devid[5]; /* device indentifier */ 77 uint8_t sc_us; /* cmd to watch for */ 78 int sc_mb; /* current button state */ 79 device_t sc_wsmousedev; 80 /* helpers for trackpads */ 81 int sc_down; 82 /* 83 * trackpad protocol variant. Known so far: 84 * 2 buttons - PowerBook 3400, single events on button 3 and 4 indicate 85 * finger down and up 86 * 4 buttons - iBook G4, button 6 indicates finger down, button 4 is 87 * always down 88 */ 89 int sc_x, sc_y; 90 int sc_tapping; 91 /* buffers */ 92 int sc_poll; 93 int sc_msg_len; 94 int sc_event; 95 uint8_t sc_buffer[16]; 96 }; 97 98 /* EMP device classes */ 99 #define MSCLASS_TABLET 0 100 #define MSCLASS_MOUSE 1 101 #define MSCLASS_TRACKBALL 2 102 #define MSCLASS_TRACKPAD 3 103 104 /* 105 * Function declarations. 106 */ 107 static int adbms_match(device_t, cfdata_t, void *); 108 static void adbms_attach(device_t, device_t, void *); 109 static void ems_init(struct adbms_softc *); 110 static void init_trackpad(struct adbms_softc *); 111 static void adbms_init_mouse(struct adbms_softc *); 112 static void adbms_init_turbo(struct adbms_softc *); 113 static void adbms_init_uspeed(struct adbms_softc *); 114 static void adbms_process_event(struct adbms_softc *, int, uint8_t *); 115 static int adbms_send_sync(struct adbms_softc *, uint8_t, int, uint8_t *); 116 117 /* Driver definition. */ 118 CFATTACH_DECL_NEW(adbms, sizeof(struct adbms_softc), 119 adbms_match, adbms_attach, NULL, NULL); 120 121 static int adbms_enable(void *); 122 static int adbms_ioctl(void *, u_long, void *, int, struct lwp *); 123 static void adbms_disable(void *); 124 125 /* 126 * handle tapping the trackpad 127 * different pads report different button counts and use slightly different 128 * protocols 129 */ 130 static void adbms_mangle_2(struct adbms_softc *, int); 131 static void adbms_mangle_4(struct adbms_softc *, int); 132 static void adbms_handler(void *, int, uint8_t *); 133 static int adbms_wait(struct adbms_softc *, int); 134 static int sysctl_adbms_tap(SYSCTLFN_ARGS); 135 136 const struct wsmouse_accessops adbms_accessops = { 137 adbms_enable, 138 adbms_ioctl, 139 adbms_disable, 140 }; 141 142 static int 143 adbms_match(device_t parent, cfdata_t cf, void *aux) 144 { 145 struct adb_attach_args *aaa = aux; 146 147 if (aaa->dev->original_addr == ADBADDR_MS) 148 return 1; 149 else 150 return 0; 151 } 152 153 static void 154 adbms_attach(device_t parent, device_t self, void *aux) 155 { 156 struct adbms_softc *sc = device_private(self); 157 struct adb_attach_args *aaa = aux; 158 struct wsmousedev_attach_args a; 159 160 sc->sc_dev = self; 161 sc->sc_ops = aaa->ops; 162 sc->sc_adbdev = aaa->dev; 163 sc->sc_adbdev->cookie = sc; 164 sc->sc_adbdev->handler = adbms_handler; 165 sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0); 166 printf(" addr %d: ", sc->sc_adbdev->current_addr); 167 168 sc->sc_class = MSCLASS_MOUSE; 169 sc->sc_buttons = 1; 170 sc->sc_res = 100; 171 sc->sc_devid[0] = 0; 172 sc->sc_devid[4] = 0; 173 sc->sc_poll = 0; 174 sc->sc_msg_len = 0; 175 sc->sc_tapping = 1; 176 177 ems_init(sc); 178 179 /* print out the type of mouse we have */ 180 switch (sc->sc_adbdev->handler_id) { 181 case ADBMS_100DPI: 182 printf("%d-button, %u dpi mouse\n", sc->sc_buttons, 183 sc->sc_res); 184 break; 185 case ADBMS_200DPI: 186 sc->sc_res = 200; 187 printf("%d-button, %u dpi mouse\n", sc->sc_buttons, 188 sc->sc_res); 189 break; 190 case ADBMS_MSA3: 191 printf("Mouse Systems A3 mouse, %d-button, %u dpi\n", 192 sc->sc_buttons, sc->sc_res); 193 break; 194 case ADBMS_USPEED: 195 printf("MicroSpeed mouse, default parameters\n"); 196 break; 197 case ADBMS_UCONTOUR: 198 printf("Contour mouse, default parameters\n"); 199 break; 200 case ADBMS_TURBO: 201 printf("Kensington Turbo Mouse\n"); 202 break; 203 case ADBMS_EXTENDED: 204 if (sc->sc_devid[0] == '\0') { 205 printf("Logitech "); 206 switch (sc->sc_class) { 207 case MSCLASS_MOUSE: 208 printf("MouseMan (non-EMP) mouse"); 209 break; 210 case MSCLASS_TRACKBALL: 211 printf("TrackMan (non-EMP) trackball"); 212 break; 213 default: 214 printf("non-EMP relative positioning device"); 215 break; 216 } 217 printf("\n"); 218 } else { 219 printf("EMP "); 220 switch (sc->sc_class) { 221 case MSCLASS_TABLET: 222 printf("tablet"); 223 break; 224 case MSCLASS_MOUSE: 225 printf("mouse"); 226 break; 227 case MSCLASS_TRACKBALL: 228 printf("trackball"); 229 break; 230 case MSCLASS_TRACKPAD: 231 printf("trackpad"); 232 init_trackpad(sc); 233 break; 234 default: 235 printf("unknown device"); 236 break; 237 } 238 printf(" <%s> %d-button, %u dpi\n", sc->sc_devid, 239 sc->sc_buttons, sc->sc_res); 240 } 241 break; 242 default: 243 printf("relative positioning device (mouse?) (%d)\n", 244 sc->sc_adbdev->handler_id); 245 break; 246 } 247 248 a.accessops = &adbms_accessops; 249 a.accesscookie = sc; 250 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint); 251 } 252 253 254 /* 255 * Initialize extended mouse support -- probes devices as described 256 * in Inside Macintosh: Devices, Chapter 5 "ADB Manager". 257 * 258 * Extended Mouse Protocol is documented in TechNote HW1: 259 * "ADB - The Untold Story: Space Aliens Ate My Mouse" 260 * 261 * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe, 262 * Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan 263 */ 264 void 265 ems_init(struct adbms_softc *sc) 266 { 267 268 DPRINTF("ems_init %d\n", sc->sc_adbdev->handler_id); 269 270 switch (sc->sc_adbdev->handler_id) { 271 case ADBMS_USPEED: 272 case ADBMS_UCONTOUR: 273 adbms_init_uspeed(sc); 274 return; 275 case ADBMS_TURBO: 276 adbms_init_turbo(sc); 277 return; 278 case ADBMS_100DPI: 279 case ADBMS_200DPI: 280 adbms_init_mouse(sc); 281 } 282 } 283 284 static void 285 adbms_init_uspeed(struct adbms_softc *sc) 286 { 287 uint8_t cmd, addr, buffer[4]; 288 289 addr = sc->sc_adbdev->current_addr; 290 291 /* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */ 292 cmd = ADBLISTEN(addr, 1); 293 294 /* 295 * To setup the MicroSpeed or the Contour, it appears 296 * that we can send the following command to the mouse 297 * and then expect data back in the form: 298 * buffer[0] = 4 (bytes) 299 * buffer[1], buffer[2] as std. mouse 300 * buffer[3] = buffer[4] = 0xff when no buttons 301 * are down. When button N down, bit N is clear. 302 * buffer[4]'s locking mask enables a 303 * click to toggle the button down state--sort of 304 * like the "Easy Access" shift/control/etc. keys. 305 * buffer[3]'s alternative speed mask enables using 306 * different speed when the corr. button is down 307 */ 308 buffer[0] = 0x00; /* Alternative speed */ 309 buffer[1] = 0x00; /* speed = maximum */ 310 buffer[2] = 0x10; /* enable extended protocol, 311 * lower bits = alt. speed mask 312 * = 0000b 313 */ 314 buffer[3] = 0x07; /* Locking mask = 0000b, 315 * enable buttons = 0111b 316 */ 317 adbms_send_sync(sc, cmd, 4, buffer); 318 319 sc->sc_buttons = 3; 320 sc->sc_res = 200; 321 } 322 323 static void 324 adbms_init_turbo(struct adbms_softc *sc) 325 { 326 uint8_t addr; 327 328 /* Found Kensington Turbo Mouse */ 329 static u_char data1[] = 330 { 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 }; 331 static u_char data2[] = 332 { 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 }; 333 334 addr = sc->sc_adbdev->current_addr; 335 336 adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL); 337 adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data1); 338 adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL); 339 adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data2); 340 } 341 342 static void 343 adbms_init_mouse(struct adbms_softc *sc) 344 { 345 int len; 346 uint8_t cmd, addr, buffer[16]; 347 348 addr = sc->sc_adbdev->current_addr; 349 /* found a mouse */ 350 cmd = ADBTALK(addr, 3); 351 if (!adbms_send_sync(sc, cmd, 0, NULL)) { 352 #ifdef ADBMS_DEBUG 353 printf("adb: ems_init timed out\n"); 354 #endif 355 return; 356 } 357 358 /* Attempt to initialize Extended Mouse Protocol */ 359 len = sc->sc_msg_len; 360 memcpy(buffer, sc->sc_buffer, len); 361 DPRINTF("buffer: %02x %02x\n", buffer[0], buffer[1]); 362 buffer[1] = 4; /* make handler ID 4 */ 363 cmd = ADBLISTEN(addr, 3); 364 if (!adbms_send_sync(sc, cmd, len, buffer)) { 365 #ifdef ADBMS_DEBUG 366 printf("adb: ems_init timed out\n"); 367 #endif 368 return; 369 } 370 371 /* 372 * Check to see if successful, if not 373 * try to initialize it as other types 374 */ 375 cmd = ADBTALK(addr, 3); 376 if (!adbms_send_sync(sc, cmd, 0, NULL)) { 377 DPRINTF("timeout checking for EMP switch\n"); 378 return; 379 } 380 DPRINTF("new handler ID: %02x\n", sc->sc_buffer[1]); 381 if (sc->sc_buffer[1] == ADBMS_EXTENDED) { 382 sc->sc_adbdev->handler_id = ADBMS_EXTENDED; 383 cmd = ADBTALK(addr, 1); 384 if(!adbms_send_sync(sc, cmd, 0, NULL)) { 385 DPRINTF("adb: ems_init timed out\n"); 386 return; 387 } 388 389 len = sc->sc_msg_len; 390 memcpy(buffer, sc->sc_buffer, len); 391 392 if (sc->sc_msg_len == 8) { 393 uint16_t res; 394 /* we have a true EMP device */ 395 #ifdef ADB_PRINT_EMP 396 397 printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n", 398 buffer[0], buffer[1], buffer[2], buffer[3], 399 buffer[4], buffer[5], buffer[6], buffer[7]); 400 #endif 401 memcpy(sc->sc_devid, &buffer[0], 4); 402 memcpy(&res, &buffer[4], sizeof(res)); 403 sc->sc_res = res; 404 sc->sc_class = buffer[6]; 405 sc->sc_buttons = buffer[7]; 406 } else if (buffer[0] == 0x9a && 407 ((buffer[1] == 0x20) || (buffer[1] == 0x21))) { 408 /* 409 * Set up non-EMP Mouseman/Trackman to put 410 * button bits in 3rd byte instead of sending 411 * via pseudo keyboard device. 412 */ 413 if (buffer[1] == 0x21) 414 sc->sc_class = MSCLASS_TRACKBALL; 415 else 416 sc->sc_class = MSCLASS_MOUSE; 417 418 cmd = ADBLISTEN(addr, 1); 419 buffer[0]=0x00; 420 buffer[1]=0x81; 421 adbms_send_sync(sc, cmd, 2, buffer); 422 423 cmd = ADBLISTEN(addr, 1); 424 buffer[0]=0x01; 425 buffer[1]=0x81; 426 adbms_send_sync(sc, cmd, 2, buffer); 427 428 cmd = ADBLISTEN(addr, 1); 429 buffer[0]=0x02; 430 buffer[1]=0x81; 431 adbms_send_sync(sc, cmd, 2, buffer); 432 433 cmd = ADBLISTEN(addr, 1); 434 buffer[0]=0x03; 435 buffer[1]=0x38; 436 adbms_send_sync(sc, cmd, 2, buffer); 437 438 sc->sc_buttons = 3; 439 sc->sc_res = 400; 440 } 441 } else { 442 /* Attempt to initialize as an A3 mouse */ 443 buffer[1] = 0x03; /* make handler ID 3 */ 444 cmd = ADBLISTEN(addr, 3); 445 if (!adbms_send_sync(sc, cmd, len, buffer)) { 446 #ifdef ADBMS_DEBUG 447 printf("adb: ems_init timed out\n"); 448 #endif 449 return; 450 } 451 452 /* 453 * Check to see if successful, if not 454 * try to initialize it as other types 455 */ 456 cmd = ADBTALK(addr, 3); 457 if(adbms_send_sync(sc, cmd, 0, NULL)) { 458 len = sc->sc_msg_len; 459 memcpy(buffer, sc->sc_buffer, len); 460 if (buffer[1] == ADBMS_MSA3) { 461 sc->sc_adbdev->handler_id = ADBMS_MSA3; 462 /* Initialize as above */ 463 cmd = ADBLISTEN(addr, 2); 464 /* listen 2 */ 465 buffer[0] = 0x00; 466 /* Irrelevant, buffer has 0x77 */ 467 buffer[2] = 0x07; 468 /* 469 * enable 3 button mode = 0111b, 470 * speed = normal 471 */ 472 adbms_send_sync(sc, cmd, 3, buffer); 473 sc->sc_buttons = 3; 474 sc->sc_res = 300; 475 } 476 } 477 } 478 } 479 480 static void 481 adbms_handler(void *cookie, int len, uint8_t *data) 482 { 483 struct adbms_softc *sc = cookie; 484 485 #ifdef ADBMS_DEBUG 486 int i; 487 printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us); 488 for (i = 0; i < len; i++) { 489 printf(" %02x", data[i]); 490 } 491 printf("\n"); 492 #endif 493 if (len >= 2) { 494 memcpy(sc->sc_buffer, &data[2], len - 2); 495 sc->sc_msg_len = len - 2; 496 if (data[1] == sc->sc_us) { 497 /* make sense of the mouse message */ 498 adbms_process_event(sc, sc->sc_msg_len, sc->sc_buffer); 499 return; 500 } 501 wakeup(&sc->sc_event); 502 } else { 503 DPRINTF("bogus message\n"); 504 } 505 } 506 507 static void 508 adbms_process_event(struct adbms_softc *sc, int len, uint8_t *buffer) 509 { 510 int buttons = 0, mask, dx, dy, i; 511 int button_bit = 1; 512 513 if ((sc->sc_adbdev->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) { 514 /* massage the data to look like EMP data */ 515 if ((buffer[2] & 0x04) == 0x04) 516 buffer[0] &= 0x7f; 517 else 518 buffer[0] |= 0x80; 519 if ((buffer[2] & 0x02) == 0x02) 520 buffer[1] &= 0x7f; 521 else 522 buffer[1] |= 0x80; 523 if ((buffer[2] & 0x01) == 0x01) 524 buffer[2] = 0x00; 525 else 526 buffer[2] = 0x80; 527 } 528 529 switch (sc->sc_adbdev->handler_id) { 530 case ADBMS_USPEED: 531 case ADBMS_UCONTOUR: 532 /* MicroSpeed mouse and Contour mouse */ 533 if (len == 4) 534 buttons = (~buffer[3]) & 0xff; 535 else 536 buttons = (buffer[1] & 0x80) ? 0 : 1; 537 break; 538 case ADBMS_MSA3: 539 /* Mouse Systems A3 mouse */ 540 if (len == 3) 541 buttons = (~buffer[2]) & 0x07; 542 else 543 buttons = (buffer[0] & 0x80) ? 0 : 1; 544 break; 545 default: 546 /* Classic Mouse Protocol (up to 2 buttons) */ 547 for (i = 0; i < 2; i++, button_bit <<= 1) 548 /* 0 when button down */ 549 if (!(buffer[i] & 0x80)) 550 buttons |= button_bit; 551 else 552 buttons &= ~button_bit; 553 /* Extended Protocol (up to 6 more buttons) */ 554 for (mask = 0x80; i < len; 555 i += (mask == 0x80), button_bit <<= 1) { 556 /* 0 when button down */ 557 if (!(buffer[i] & mask)) 558 buttons |= button_bit; 559 else 560 buttons &= ~button_bit; 561 mask = ((mask >> 4) & 0xf) 562 | ((mask & 0xf) << 4); 563 } 564 break; 565 } 566 567 if (sc->sc_adbdev->handler_id != ADBMS_EXTENDED) { 568 dx = ((int)(buffer[1] & 0x3f)) - ((buffer[1] & 0x40) ? 64 : 0); 569 dy = ((int)(buffer[0] & 0x3f)) - ((buffer[0] & 0x40) ? 64 : 0); 570 } else { 571 /* EMP crap, additional motion bits */ 572 int shift = 7, ddx, ddy, sign, smask; 573 574 #ifdef ADBMS_DEBUG 575 printf("EMP packet:"); 576 for (i = 0; i < len; i++) 577 printf(" %02x", buffer[i]); 578 printf("\n"); 579 #endif 580 dx = (int)buffer[1] & 0x7f; 581 dy = (int)buffer[0] & 0x7f; 582 for (i = 2; i < len; i++) { 583 ddx = (buffer[i] & 0x07); 584 ddy = (buffer[i] & 0x70) >> 4; 585 dx |= (ddx << shift); 586 dy |= (ddy << shift); 587 shift += 3; 588 } 589 sign = 1 << (shift - 1); 590 smask = 0xffffffff << shift; 591 if (dx & sign) 592 dx |= smask; 593 if (dy & sign) 594 dy |= smask; 595 #ifdef ADBMS_DEBUG 596 printf("%d %d %08x %d\n", dx, dy, smask, shift); 597 #endif 598 } 599 600 if (sc->sc_class == MSCLASS_TRACKPAD) { 601 602 if (sc->sc_tapping == 1) { 603 if (sc->sc_down) { 604 /* finger is down - collect motion data */ 605 sc->sc_x += dx; 606 sc->sc_y += dy; 607 } 608 DPRINTF("buttons: %02x\n", buttons); 609 switch (sc->sc_buttons) { 610 case 2: 611 buttons |= ((buttons & 2) >> 1); 612 adbms_mangle_2(sc, buttons); 613 break; 614 case 4: 615 adbms_mangle_4(sc, buttons); 616 break; 617 } 618 } 619 /* filter the pseudo-buttons out */ 620 buttons &= 1; 621 } 622 623 if (sc->sc_wsmousedev) 624 wsmouse_input(sc->sc_wsmousedev, sc->sc_mb | buttons, 625 dx, -dy, 0, 0, 626 WSMOUSE_INPUT_DELTA); 627 #if NAED > 0 628 aed_input(&new_event); 629 #endif 630 } 631 632 static void 633 adbms_mangle_2(struct adbms_softc *sc, int buttons) 634 { 635 636 if (buttons & 4) { 637 /* finger down on pad */ 638 if (sc->sc_down == 0) { 639 sc->sc_down = 1; 640 sc->sc_x = 0; 641 sc->sc_y = 0; 642 } 643 } 644 if (buttons & 8) { 645 /* finger up */ 646 if (sc->sc_down) { 647 if (((sc->sc_x * sc->sc_x + 648 sc->sc_y * sc->sc_y) < 3) && 649 (sc->sc_wsmousedev)) { 650 /* 651 * if there wasn't much movement between 652 * finger down and up again we assume 653 * someone tapped the pad and we just 654 * send a mouse button event 655 */ 656 wsmouse_input(sc->sc_wsmousedev, 657 1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA); 658 } 659 sc->sc_down = 0; 660 } 661 } 662 } 663 664 static void 665 adbms_mangle_4(struct adbms_softc *sc, int buttons) 666 { 667 668 if (buttons & 0x20) { 669 /* finger down on pad */ 670 if (sc->sc_down == 0) { 671 sc->sc_down = 1; 672 sc->sc_x = 0; 673 sc->sc_y = 0; 674 } 675 } 676 if ((buttons & 0x20) == 0) { 677 /* finger up */ 678 if (sc->sc_down) { 679 if (((sc->sc_x * sc->sc_x + 680 sc->sc_y * sc->sc_y) < 3) && 681 (sc->sc_wsmousedev)) { 682 /* 683 * if there wasn't much movement between 684 * finger down and up again we assume 685 * someone tapped the pad and we just 686 * send a mouse button event 687 */ 688 wsmouse_input(sc->sc_wsmousedev, 689 1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA); 690 } 691 sc->sc_down = 0; 692 } 693 } 694 } 695 696 static int 697 adbms_enable(void *v) 698 { 699 return 0; 700 } 701 702 static int 703 adbms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 704 { 705 706 switch (cmd) { 707 case WSMOUSEIO_GTYPE: 708 *(u_int *)data = WSMOUSE_TYPE_ADB; 709 break; 710 711 default: 712 return (EPASSTHROUGH); 713 } 714 return (0); 715 } 716 717 static void 718 adbms_disable(void *v) 719 { 720 } 721 722 static void 723 init_trackpad(struct adbms_softc *sc) 724 { 725 const struct sysctlnode *me = NULL, *node = NULL; 726 int cmd, addr, ret; 727 uint8_t buffer[16]; 728 uint8_t b2[] = {0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50}; 729 730 addr = sc->sc_adbdev->current_addr; 731 cmd = ADBTALK(addr, 1); 732 if (!adbms_send_sync(sc, cmd, 0, NULL)) 733 return; 734 735 if (sc->sc_msg_len != 8) 736 return; 737 738 memcpy(buffer, sc->sc_buffer, 8); 739 740 /* now whack the pad */ 741 cmd = ADBLISTEN(addr, 1); 742 buffer[6] = 0x0d; 743 adbms_send_sync(sc, cmd, 8, buffer); 744 745 delay(1000); 746 cmd = ADBLISTEN(addr, 2); 747 adbms_send_sync(sc, cmd, 8, b2); 748 749 delay(1000); 750 cmd = ADBLISTEN(addr, 1); 751 buffer[6] = 0x03; 752 adbms_send_sync(sc, cmd, 8, buffer); 753 754 cmd = ADBFLUSH(addr); 755 adbms_send_sync(sc, cmd, 0, NULL); 756 delay(1000); 757 758 /* 759 * setup a sysctl node to control whether tapping the pad should 760 * trigger mouse button events 761 */ 762 763 sc->sc_tapping = 1; 764 765 ret = sysctl_createv(NULL, 0, NULL, &me, 766 CTLFLAG_READWRITE, 767 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, 768 NULL, 0, NULL, 0, 769 CTL_MACHDEP, CTL_CREATE, CTL_EOL); 770 771 ret = sysctl_createv(NULL, 0, NULL, &node, 772 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 773 CTLTYPE_INT, "tapping", "tapping the pad causes button events", 774 sysctl_adbms_tap, 1, (void *)sc, 0, 775 CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL); 776 777 (void)ret; 778 } 779 780 static int 781 adbms_wait(struct adbms_softc *sc, int timeout) 782 { 783 int cnt = 0; 784 785 if (sc->sc_poll) { 786 while (sc->sc_msg_len == -1) { 787 sc->sc_ops->poll(sc->sc_ops->cookie); 788 } 789 } else { 790 while ((sc->sc_msg_len == -1) && (cnt < timeout)) { 791 tsleep(&sc->sc_event, 0, "adbmsio", hz); 792 cnt++; 793 } 794 } 795 return (sc->sc_msg_len > 0); 796 } 797 798 static int 799 adbms_send_sync(struct adbms_softc *sc, uint8_t cmd, int len, uint8_t *msg) 800 { 801 int i; 802 803 sc->sc_msg_len = -1; 804 DPRINTF("send: %02x", cmd); 805 for (i = 0; i < len; i++) 806 DPRINTF(" %02x", msg[i]); 807 DPRINTF("\n"); 808 sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, len, msg); 809 adbms_wait(sc, 1000); 810 return (sc->sc_msg_len != -1); 811 } 812 813 static int 814 sysctl_adbms_tap(SYSCTLFN_ARGS) 815 { 816 struct sysctlnode node = *rnode; 817 struct adbms_softc *sc = node.sysctl_data; 818 819 node.sysctl_idata = sc->sc_tapping; 820 821 if (newp) { 822 823 /* we're asked to write */ 824 node.sysctl_data = &sc->sc_tapping; 825 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) { 826 827 sc->sc_tapping = (*(int *)node.sysctl_data == 0) ? 0 : 1; 828 return 0; 829 } 830 return EINVAL; 831 } else { 832 833 node.sysctl_data = &sc->sc_tapping; 834 node.sysctl_size = 4; 835 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 836 } 837 838 return 0; 839 } 840 841 SYSCTL_SETUP(sysctl_ams_setup, "sysctl ams subtree setup") 842 { 843 844 sysctl_createv(NULL, 0, NULL, NULL, 845 CTLFLAG_PERMANENT, 846 CTLTYPE_NODE, "machdep", NULL, 847 NULL, 0, NULL, 0, 848 CTL_MACHDEP, CTL_EOL); 849 } 850