1 /* $NetBSD: synaptics.c,v 1.32 2014/05/23 01:11:29 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2005, Steve C. Woodford 5 * Copyright (c) 2004, Ales Krenek 6 * Copyright (c) 2004, Kentaro A. Kurahone 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * * Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * * Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * * Neither the name of the authors nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 * 36 */ 37 38 /* 39 * TODO: 40 * - Make the sysctl values per-instance instead of global. 41 * - Consider setting initial scaling factors at runtime according 42 * to the values returned by the 'Read Resolutions' command. 43 * - Support the serial protocol (we only support PS/2 for now) 44 * - Support auto-repeat for up/down button Z-axis emulation. 45 * - Maybe add some more gestures (can we use Palm support somehow?) 46 */ 47 48 #include "opt_pms.h" 49 50 #include <sys/cdefs.h> 51 __KERNEL_RCSID(0, "$NetBSD: synaptics.c,v 1.32 2014/05/23 01:11:29 christos Exp $"); 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/device.h> 56 #include <sys/ioctl.h> 57 #include <sys/sysctl.h> 58 #include <sys/kernel.h> 59 #include <sys/proc.h> 60 61 #include <sys/bus.h> 62 63 #include <dev/pckbport/pckbportvar.h> 64 65 #include <dev/pckbport/synapticsreg.h> 66 #include <dev/pckbport/synapticsvar.h> 67 68 #include <dev/pckbport/pmsreg.h> 69 #include <dev/pckbport/pmsvar.h> 70 71 #include <dev/wscons/wsconsio.h> 72 #include <dev/wscons/wsmousevar.h> 73 74 /* 75 * Absolute-mode packets are decoded and passed around using 76 * the following structure. 77 */ 78 struct synaptics_packet { 79 signed short sp_x; /* Unscaled absolute X/Y coordinates */ 80 signed short sp_y; 81 u_char sp_z; /* Z (pressure) */ 82 u_char sp_w; /* W (contact patch width) */ 83 char sp_left; /* Left mouse button status */ 84 char sp_right; /* Right mouse button status */ 85 char sp_middle; /* Middle button status (possibly emulated) */ 86 char sp_up; /* Up button status */ 87 char sp_down; /* Down button status */ 88 }; 89 90 static void pms_synaptics_input(void *, int); 91 static void pms_synaptics_process_packet(struct pms_softc *, 92 struct synaptics_packet *); 93 static void pms_sysctl_synaptics(struct sysctllog **); 94 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS); 95 96 /* Controlled by sysctl. */ 97 static int synaptics_up_down_emul = 2; 98 static int synaptics_up_down_motion_delta = 1; 99 static int synaptics_gesture_move = 200; 100 static int synaptics_gesture_length = 20; 101 static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT; 102 static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT; 103 static int synaptics_edge_top = SYNAPTICS_EDGE_TOP; 104 static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM; 105 static int synaptics_edge_motion_delta = 32; 106 static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5; 107 static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10; 108 static int synaptics_two_fingers_emul = 0; 109 static int synaptics_scale_x = 16; 110 static int synaptics_scale_y = 16; 111 static int synaptics_max_speed_x = 32; 112 static int synaptics_max_speed_y = 32; 113 static int synaptics_movement_threshold = 4; 114 115 /* Sysctl nodes. */ 116 static int synaptics_up_down_emul_nodenum; 117 static int synaptics_up_down_motion_delta_nodenum; 118 static int synaptics_gesture_move_nodenum; 119 static int synaptics_gesture_length_nodenum; 120 static int synaptics_edge_left_nodenum; 121 static int synaptics_edge_right_nodenum; 122 static int synaptics_edge_top_nodenum; 123 static int synaptics_edge_bottom_nodenum; 124 static int synaptics_edge_motion_delta_nodenum; 125 static int synaptics_finger_high_nodenum; 126 static int synaptics_finger_low_nodenum; 127 static int synaptics_two_fingers_emul_nodenum; 128 static int synaptics_scale_x_nodenum; 129 static int synaptics_scale_y_nodenum; 130 static int synaptics_max_speed_x_nodenum; 131 static int synaptics_max_speed_y_nodenum; 132 static int synaptics_movement_threshold_nodenum; 133 134 static void 135 pms_synaptics_probe_extended(struct pms_softc *psc) 136 { 137 struct synaptics_softc *sc = &psc->u.synaptics; 138 u_char cmd[1], resp[3]; 139 int res; 140 141 aprint_debug_dev(psc->sc_dev, 142 "synaptics_probe: Capabilities 0x%04x.\n", sc->caps); 143 if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH) 144 sc->flags |= SYN_FLAG_HAS_PASSTHROUGH; 145 146 if (sc->caps & SYNAPTICS_CAP_PALMDETECT) 147 sc->flags |= SYN_FLAG_HAS_PALM_DETECT; 148 149 if (sc->caps & SYNAPTICS_CAP_MULTIDETECT) 150 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER; 151 152 if (sc->caps & SYNAPTICS_CAP_MULTIFINGERREPORT) 153 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER_REPORT; 154 155 /* Ask about extra buttons to detect up/down. */ 156 if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) 157 >= SYNAPTICS_EXTENDED_QUERY) 158 { 159 res = pms_sliced_command(psc->sc_kbctag, 160 psc->sc_kbcslot, SYNAPTICS_EXTENDED_QUERY); 161 cmd[0] = PMS_SEND_DEV_STATUS; 162 res |= pckbport_poll_cmd(psc->sc_kbctag, 163 psc->sc_kbcslot, cmd, 1, 3, resp, 0); 164 if (res == 0) { 165 int buttons = (resp[1] >> 4); 166 aprint_debug_dev(psc->sc_dev, 167 "%s: Extended Buttons: %d.\n", __func__, buttons); 168 169 aprint_debug_dev(psc->sc_dev, "%s: Extended " 170 "Capabilities: 0x%02x 0x%02x 0x%02x.\n", __func__, 171 resp[0], resp[1], resp[2]); 172 if (buttons >= 2) { 173 /* Yes. */ 174 sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS; 175 } 176 if (resp[0] & 0x1) { 177 /* Vertical scroll area */ 178 sc->flags |= SYN_FLAG_HAS_VERTICAL_SCROLL; 179 } 180 if (resp[0] & 0x2) { 181 /* Horizontal scroll area */ 182 sc->flags |= SYN_FLAG_HAS_HORIZONTAL_SCROLL; 183 } 184 if (resp[0] & 0x4) { 185 /* Extended W-Mode */ 186 sc->flags |= SYN_FLAG_HAS_EXTENDED_WMODE; 187 } 188 } 189 } 190 191 /* Ask about click pad */ 192 if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) >= 193 SYNAPTICS_CONTINUED_CAPABILITIES) 194 { 195 res = pms_sliced_command(psc->sc_kbctag, 196 psc->sc_kbcslot, SYNAPTICS_CONTINUED_CAPABILITIES); 197 cmd[0] = PMS_SEND_DEV_STATUS; 198 res |= pckbport_poll_cmd(psc->sc_kbctag, 199 psc->sc_kbcslot, cmd, 1, 3, resp, 0); 200 if (res == 0) { 201 u_char clickpad_type = (resp[1] & 0x1); 202 clickpad_type |= ((resp[0] >> 4) & 0x1); 203 204 aprint_debug_dev(psc->sc_dev, "%s: Continued " 205 "Capabilities 0x%02x 0x%02x 0x%02x.\n", __func__, 206 resp[0], resp[1], resp[2]); 207 switch (clickpad_type) { 208 case 1: 209 sc->flags |= SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD; 210 break; 211 case 2: 212 sc->flags |= SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD; 213 break; 214 default: 215 break; 216 } 217 } 218 } 219 } 220 221 int 222 pms_synaptics_probe_init(void *vsc) 223 { 224 struct pms_softc *psc = vsc; 225 struct synaptics_softc *sc = &psc->u.synaptics; 226 u_char cmd[1], resp[3]; 227 int res, ver_minor, ver_major; 228 struct sysctllog *clog = NULL; 229 230 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, 231 SYNAPTICS_IDENTIFY_TOUCHPAD); 232 cmd[0] = PMS_SEND_DEV_STATUS; 233 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3, 234 resp, 0); 235 if (res) { 236 aprint_debug_dev(psc->sc_dev, 237 "synaptics_probe: Identify Touchpad error.\n"); 238 /* 239 * Reset device in case the probe confused it. 240 */ 241 doreset: 242 cmd[0] = PMS_RESET; 243 (void) pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 244 1, 2, resp, 1); 245 return (res); 246 } 247 248 if (resp[1] != SYNAPTICS_MAGIC_BYTE) { 249 aprint_debug_dev(psc->sc_dev, 250 "synaptics_probe: Not synaptics.\n"); 251 res = 1; 252 goto doreset; 253 } 254 255 sc->flags = 0; 256 257 /* Check for minimum version and print a nice message. */ 258 ver_major = resp[2] & 0x0f; 259 ver_minor = resp[0]; 260 aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n", 261 ver_major, ver_minor); 262 if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) { 263 /* No capability query support. */ 264 sc->caps = 0; 265 goto done; 266 } 267 268 /* Query the hardware capabilities. */ 269 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, 270 SYNAPTICS_READ_CAPABILITIES); 271 cmd[0] = PMS_SEND_DEV_STATUS; 272 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3, 273 resp, 0); 274 if (res) { 275 /* Hmm, failed to get capabilites. */ 276 aprint_error_dev(psc->sc_dev, 277 "synaptics_probe: Failed to query capabilities.\n"); 278 goto doreset; 279 } 280 281 sc->caps = (resp[0] << 8) | resp[2]; 282 283 if (sc->caps & SYNAPTICS_CAP_MBUTTON) 284 sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON; 285 286 if (sc->caps & SYNAPTICS_CAP_4BUTTON) 287 sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5; 288 289 if (sc->caps & SYNAPTICS_CAP_EXTENDED) { 290 pms_synaptics_probe_extended(psc); 291 } 292 293 if (sc->flags) { 294 const char comma[] = ", "; 295 const char *sep = ""; 296 aprint_normal_dev(psc->sc_dev, ""); 297 if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) { 298 aprint_normal("%sExtended W mode", sep); 299 sep = comma; 300 } 301 if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) { 302 aprint_normal("%sPassthrough", sep); 303 sep = comma; 304 } 305 if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) { 306 aprint_normal("%sMiddle button", sep); 307 sep = comma; 308 } 309 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) { 310 aprint_normal("%sButtons 4/5", sep); 311 sep = comma; 312 } 313 if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS) { 314 aprint_normal("%sUp/down buttons", sep); 315 sep = comma; 316 } 317 if (sc->flags & SYN_FLAG_HAS_PALM_DETECT) { 318 aprint_normal("%sPalm detect", sep); 319 sep = comma; 320 } 321 if (sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) { 322 aprint_normal("%sOne button click pad", sep); 323 sep = comma; 324 } 325 if (sc->flags & SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD) { 326 aprint_normal("%sTwo button click pad", sep); 327 sep = comma; 328 } 329 if (sc->flags & SYN_FLAG_HAS_VERTICAL_SCROLL) { 330 aprint_normal("%sVertical scroll", sep); 331 sep = comma; 332 } 333 if (sc->flags & SYN_FLAG_HAS_HORIZONTAL_SCROLL) { 334 aprint_normal("%sHorizontal scroll", sep); 335 sep = comma; 336 } 337 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER_REPORT) { 338 aprint_normal("%sMulti-finger Report", sep); 339 sep = comma; 340 } 341 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER) 342 aprint_normal("%sMulti-finger", sep); 343 344 aprint_normal("\n"); 345 } 346 347 done: 348 pms_sysctl_synaptics(&clog); 349 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot, 350 pms_synaptics_input, psc, device_xname(psc->sc_dev)); 351 352 return (0); 353 } 354 355 void 356 pms_synaptics_enable(void *vsc) 357 { 358 struct pms_softc *psc = vsc; 359 struct synaptics_softc *sc = &psc->u.synaptics; 360 u_char cmd[2], resp[2]; 361 int res; 362 363 if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) { 364 /* 365 * Extended capability probes can confuse the passthrough device; 366 * reset the touchpad now to cure that. 367 */ 368 cmd[0] = PMS_RESET; 369 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 370 1, 2, resp, 1); 371 } 372 373 /* 374 * Enable Absolute mode with W (width) reporting, and set 375 * the packet rate to maximum (80 packets per second). 376 */ 377 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, 378 SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE); 379 cmd[0] = PMS_SET_SAMPLE; 380 cmd[1] = SYNAPTICS_CMD_SET_MODE2; 381 res |= pckbport_enqueue_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 2, 0, 382 1, NULL); 383 sc->up_down = 0; 384 sc->prev_fingers = 0; 385 sc->gesture_start_x = sc->gesture_start_y = 0; 386 sc->gesture_start_packet = 0; 387 sc->gesture_tap_packet = 0; 388 sc->gesture_type = 0; 389 sc->gesture_buttons = 0; 390 sc->rem_x = sc->rem_y = 0; 391 sc->movement_history = 0; 392 if (res) { 393 aprint_error_dev(psc->sc_dev, 394 "synaptics_enable: Error enabling device.\n"); 395 } 396 } 397 398 void 399 pms_synaptics_resume(void *vsc) 400 { 401 struct pms_softc *psc = vsc; 402 unsigned char cmd[1],resp[2] = { 0,0 }; 403 int res; 404 405 cmd[0] = PMS_RESET; 406 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2, 407 resp, 1); 408 aprint_debug_dev(psc->sc_dev, 409 "pms_synaptics_resume: reset on resume %d 0x%02x 0x%02x\n", 410 res, resp[0], resp[1]); 411 } 412 413 static void 414 pms_sysctl_synaptics(struct sysctllog **clog) 415 { 416 int rc, root_num; 417 const struct sysctlnode *node; 418 419 if ((rc = sysctl_createv(clog, 0, NULL, &node, 420 CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics", 421 SYSCTL_DESCR("Synaptics touchpad controls"), 422 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) 423 goto err; 424 425 root_num = node->sysctl_num; 426 427 if ((rc = sysctl_createv(clog, 0, NULL, &node, 428 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 429 CTLTYPE_INT, "up_down_emulation", 430 SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"), 431 pms_sysctl_synaptics_verify, 0, 432 &synaptics_up_down_emul, 433 0, CTL_HW, root_num, CTL_CREATE, 434 CTL_EOL)) != 0) 435 goto err; 436 437 synaptics_up_down_emul_nodenum = node->sysctl_num; 438 439 if ((rc = sysctl_createv(clog, 0, NULL, &node, 440 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 441 CTLTYPE_INT, "up_down_motion_delta", 442 SYSCTL_DESCR("Up/down button Z-axis emulation rate"), 443 pms_sysctl_synaptics_verify, 0, 444 &synaptics_up_down_motion_delta, 445 0, CTL_HW, root_num, CTL_CREATE, 446 CTL_EOL)) != 0) 447 goto err; 448 449 synaptics_up_down_motion_delta_nodenum = node->sysctl_num; 450 451 if ((rc = sysctl_createv(clog, 0, NULL, &node, 452 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 453 CTLTYPE_INT, "gesture_move", 454 SYSCTL_DESCR("Movement greater than this between taps cancels gesture"), 455 pms_sysctl_synaptics_verify, 0, 456 &synaptics_gesture_move, 457 0, CTL_HW, root_num, CTL_CREATE, 458 CTL_EOL)) != 0) 459 goto err; 460 461 synaptics_gesture_move_nodenum = node->sysctl_num; 462 463 if ((rc = sysctl_createv(clog, 0, NULL, &node, 464 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 465 CTLTYPE_INT, "gesture_length", 466 SYSCTL_DESCR("Time period in which tap is recognised as a gesture"), 467 pms_sysctl_synaptics_verify, 0, 468 &synaptics_gesture_length, 469 0, CTL_HW, root_num, CTL_CREATE, 470 CTL_EOL)) != 0) 471 goto err; 472 473 synaptics_gesture_length_nodenum = node->sysctl_num; 474 475 if ((rc = sysctl_createv(clog, 0, NULL, &node, 476 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 477 CTLTYPE_INT, "edge_left", 478 SYSCTL_DESCR("Define left edge of touchpad"), 479 pms_sysctl_synaptics_verify, 0, 480 &synaptics_edge_left, 481 0, CTL_HW, root_num, CTL_CREATE, 482 CTL_EOL)) != 0) 483 goto err; 484 485 synaptics_edge_left_nodenum = node->sysctl_num; 486 487 if ((rc = sysctl_createv(clog, 0, NULL, &node, 488 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 489 CTLTYPE_INT, "edge_right", 490 SYSCTL_DESCR("Define right edge of touchpad"), 491 pms_sysctl_synaptics_verify, 0, 492 &synaptics_edge_right, 493 0, CTL_HW, root_num, CTL_CREATE, 494 CTL_EOL)) != 0) 495 goto err; 496 497 synaptics_edge_right_nodenum = node->sysctl_num; 498 499 if ((rc = sysctl_createv(clog, 0, NULL, &node, 500 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 501 CTLTYPE_INT, "edge_top", 502 SYSCTL_DESCR("Define top edge of touchpad"), 503 pms_sysctl_synaptics_verify, 0, 504 &synaptics_edge_top, 505 0, CTL_HW, root_num, CTL_CREATE, 506 CTL_EOL)) != 0) 507 goto err; 508 509 synaptics_edge_top_nodenum = node->sysctl_num; 510 511 if ((rc = sysctl_createv(clog, 0, NULL, &node, 512 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 513 CTLTYPE_INT, "edge_bottom", 514 SYSCTL_DESCR("Define bottom edge of touchpad"), 515 pms_sysctl_synaptics_verify, 0, 516 &synaptics_edge_bottom, 517 0, CTL_HW, root_num, CTL_CREATE, 518 CTL_EOL)) != 0) 519 goto err; 520 521 synaptics_edge_bottom_nodenum = node->sysctl_num; 522 523 if ((rc = sysctl_createv(clog, 0, NULL, &node, 524 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 525 CTLTYPE_INT, "edge_motion_delta", 526 SYSCTL_DESCR("Define edge motion rate"), 527 pms_sysctl_synaptics_verify, 0, 528 &synaptics_edge_motion_delta, 529 0, CTL_HW, root_num, CTL_CREATE, 530 CTL_EOL)) != 0) 531 goto err; 532 533 synaptics_edge_motion_delta_nodenum = node->sysctl_num; 534 535 if ((rc = sysctl_createv(clog, 0, NULL, &node, 536 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 537 CTLTYPE_INT, "finger_high", 538 SYSCTL_DESCR("Define finger applied pressure threshold"), 539 pms_sysctl_synaptics_verify, 0, 540 &synaptics_finger_high, 541 0, CTL_HW, root_num, CTL_CREATE, 542 CTL_EOL)) != 0) 543 goto err; 544 545 synaptics_finger_high_nodenum = node->sysctl_num; 546 547 if ((rc = sysctl_createv(clog, 0, NULL, &node, 548 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 549 CTLTYPE_INT, "finger_low", 550 SYSCTL_DESCR("Define finger removed pressure threshold"), 551 pms_sysctl_synaptics_verify, 0, 552 &synaptics_finger_low, 553 0, CTL_HW, root_num, CTL_CREATE, 554 CTL_EOL)) != 0) 555 goto err; 556 557 synaptics_finger_low_nodenum = node->sysctl_num; 558 559 if ((rc = sysctl_createv(clog, 0, NULL, &node, 560 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 561 CTLTYPE_INT, "two_fingers_emulation", 562 SYSCTL_DESCR("Map two fingers to middle button"), 563 pms_sysctl_synaptics_verify, 0, 564 &synaptics_two_fingers_emul, 565 0, CTL_HW, root_num, CTL_CREATE, 566 CTL_EOL)) != 0) 567 goto err; 568 569 synaptics_two_fingers_emul_nodenum = node->sysctl_num; 570 571 if ((rc = sysctl_createv(clog, 0, NULL, &node, 572 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 573 CTLTYPE_INT, "scale_x", 574 SYSCTL_DESCR("Horizontal movement scale factor"), 575 pms_sysctl_synaptics_verify, 0, 576 &synaptics_scale_x, 577 0, CTL_HW, root_num, CTL_CREATE, 578 CTL_EOL)) != 0) 579 goto err; 580 581 synaptics_scale_x_nodenum = node->sysctl_num; 582 583 if ((rc = sysctl_createv(clog, 0, NULL, &node, 584 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 585 CTLTYPE_INT, "scale_y", 586 SYSCTL_DESCR("Vertical movement scale factor"), 587 pms_sysctl_synaptics_verify, 0, 588 &synaptics_scale_y, 589 0, CTL_HW, root_num, CTL_CREATE, 590 CTL_EOL)) != 0) 591 goto err; 592 593 synaptics_scale_y_nodenum = node->sysctl_num; 594 595 if ((rc = sysctl_createv(clog, 0, NULL, &node, 596 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 597 CTLTYPE_INT, "max_speed_x", 598 SYSCTL_DESCR("Horizontal movement maximum speed"), 599 pms_sysctl_synaptics_verify, 0, 600 &synaptics_max_speed_x, 601 0, CTL_HW, root_num, CTL_CREATE, 602 CTL_EOL)) != 0) 603 goto err; 604 605 synaptics_max_speed_x_nodenum = node->sysctl_num; 606 607 if ((rc = sysctl_createv(clog, 0, NULL, &node, 608 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 609 CTLTYPE_INT, "max_speed_y", 610 SYSCTL_DESCR("Vertical movement maximum speed"), 611 pms_sysctl_synaptics_verify, 0, 612 &synaptics_max_speed_y, 613 0, CTL_HW, root_num, CTL_CREATE, 614 CTL_EOL)) != 0) 615 goto err; 616 617 synaptics_max_speed_y_nodenum = node->sysctl_num; 618 619 if ((rc = sysctl_createv(clog, 0, NULL, &node, 620 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 621 CTLTYPE_INT, "movement_threshold", 622 SYSCTL_DESCR("Minimum reported movement threshold"), 623 pms_sysctl_synaptics_verify, 0, 624 &synaptics_movement_threshold, 625 0, CTL_HW, root_num, CTL_CREATE, 626 CTL_EOL)) != 0) 627 goto err; 628 629 synaptics_movement_threshold_nodenum = node->sysctl_num; 630 return; 631 632 err: 633 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 634 } 635 636 static int 637 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS) 638 { 639 int error, t; 640 struct sysctlnode node; 641 642 node = *rnode; 643 t = *(int *)rnode->sysctl_data; 644 node.sysctl_data = &t; 645 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 646 if (error || newp == NULL) 647 return error; 648 649 /* Sanity check the params. */ 650 if (node.sysctl_num == synaptics_up_down_emul_nodenum || 651 node.sysctl_num == synaptics_two_fingers_emul_nodenum) { 652 if (t < 0 || t > 2) 653 return (EINVAL); 654 } else 655 if (node.sysctl_num == synaptics_gesture_length_nodenum || 656 node.sysctl_num == synaptics_edge_motion_delta_nodenum || 657 node.sysctl_num == synaptics_up_down_motion_delta_nodenum) { 658 if (t < 0) 659 return (EINVAL); 660 } else 661 if (node.sysctl_num == synaptics_edge_left_nodenum || 662 node.sysctl_num == synaptics_edge_bottom_nodenum) { 663 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2)) 664 return (EINVAL); 665 } else 666 if (node.sysctl_num == synaptics_edge_right_nodenum || 667 node.sysctl_num == synaptics_edge_top_nodenum) { 668 if (t < (SYNAPTICS_EDGE_MAX / 2)) 669 return (EINVAL); 670 } else 671 if (node.sysctl_num == synaptics_scale_x_nodenum || 672 node.sysctl_num == synaptics_scale_y_nodenum) { 673 if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4)) 674 return (EINVAL); 675 } else 676 if (node.sysctl_num == synaptics_finger_high_nodenum) { 677 if (t < 0 || t > SYNAPTICS_FINGER_PALM || 678 t < synaptics_finger_low) 679 return (EINVAL); 680 } else 681 if (node.sysctl_num == synaptics_finger_low_nodenum) { 682 if (t < 0 || t > SYNAPTICS_FINGER_PALM || 683 t > synaptics_finger_high) 684 return (EINVAL); 685 } else 686 if (node.sysctl_num == synaptics_gesture_move_nodenum || 687 node.sysctl_num == synaptics_movement_threshold_nodenum) { 688 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4)) 689 return (EINVAL); 690 } else 691 return (EINVAL); 692 693 *(int *)rnode->sysctl_data = t; 694 695 return (0); 696 } 697 698 /* Masks for the first byte of a packet */ 699 #define PMS_LBUTMASK 0x01 700 #define PMS_RBUTMASK 0x02 701 #define PMS_MBUTMASK 0x04 702 703 static void 704 pms_synaptics_parse(struct pms_softc *psc) 705 { 706 struct synaptics_softc *sc = &psc->u.synaptics; 707 struct synaptics_packet sp; 708 709 memset(&sp, 0, sizeof(sp)); 710 711 /* Absolute X/Y coordinates of finger */ 712 sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) + 713 ((psc->packet[3] & 0x10) << 8); 714 sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) + 715 ((psc->packet[3] & 0x20) << 7); 716 717 /* Pressure */ 718 sp.sp_z = psc->packet[2]; 719 720 /* Width of finger */ 721 sp.sp_w = ((psc->packet[0] & 0x30) >> 2) + 722 ((psc->packet[0] & 0x04) >> 1) + 723 ((psc->packet[3] & 0x04) >> 2); 724 725 /* Left/Right button handling. */ 726 sp.sp_left = psc->packet[0] & PMS_LBUTMASK; 727 sp.sp_right = psc->packet[0] & PMS_RBUTMASK; 728 729 /* Up/Down buttons. */ 730 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) { 731 /* Old up/down buttons. */ 732 sp.sp_up = sp.sp_left ^ 733 (psc->packet[3] & PMS_LBUTMASK); 734 sp.sp_down = sp.sp_right ^ 735 (psc->packet[3] & PMS_RBUTMASK); 736 } else 737 if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS && 738 ((psc->packet[0] & PMS_RBUTMASK) ^ 739 (psc->packet[3] & PMS_RBUTMASK))) { 740 /* New up/down button. */ 741 sp.sp_up = psc->packet[4] & SYN_1BUTMASK; 742 sp.sp_down = psc->packet[5] & SYN_2BUTMASK; 743 } else { 744 sp.sp_up = 0; 745 sp.sp_down = 0; 746 } 747 748 if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) { 749 /* This is not correctly specified. Read this button press 750 * from L/U bit. 751 */ 752 sp.sp_left = ((psc->packet[0] ^ psc->packet[3]) & 0x01) ? 1 : 0; 753 } else 754 /* Middle button. */ 755 if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) { 756 /* Old style Middle Button. */ 757 sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^ 758 (psc->packet[3] & PMS_LBUTMASK); 759 } else 760 if (synaptics_up_down_emul == 1) { 761 /* Do middle button emulation using up/down buttons */ 762 sp.sp_middle = sp.sp_up | sp.sp_down; 763 sp.sp_up = sp.sp_down = 0; 764 } else 765 sp.sp_middle = 0; 766 767 pms_synaptics_process_packet(psc, &sp); 768 } 769 770 static void 771 pms_synaptics_passthrough(struct pms_softc *psc) 772 { 773 int dx, dy, dz; 774 int buttons, changed; 775 int s; 776 777 buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) | 778 ((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) | 779 ((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0); 780 781 dx = psc->packet[4]; 782 if (dx >= 128) 783 dx -= 256; 784 if (dx == -128) 785 dx = -127; 786 787 dy = psc->packet[5]; 788 if (dy >= 128) 789 dy -= 256; 790 if (dy == -128) 791 dy = -127; 792 793 dz = 0; 794 795 changed = buttons ^ (psc->buttons & 0xe0); 796 psc->buttons ^= changed; 797 798 if (dx || dy || dz || changed) { 799 buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7); 800 s = spltty(); 801 wsmouse_input(psc->sc_wsmousedev, 802 buttons, dx, dy, dz, 0, 803 WSMOUSE_INPUT_DELTA); 804 splx(s); 805 } 806 } 807 808 static void 809 pms_synaptics_input(void *vsc, int data) 810 { 811 struct pms_softc *psc = vsc; 812 struct timeval diff; 813 814 if (!psc->sc_enabled) { 815 /* Interrupts are not expected. Discard the byte. */ 816 return; 817 } 818 819 getmicrouptime(&psc->current); 820 821 if (psc->inputstate > 0) { 822 timersub(&psc->current, &psc->last, &diff); 823 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) { 824 aprint_debug_dev(psc->sc_dev, 825 "pms_input: unusual delay (%ld.%06ld s), " 826 "scheduling reset\n", 827 (long)diff.tv_sec, (long)diff.tv_usec); 828 psc->inputstate = 0; 829 psc->sc_enabled = 0; 830 wakeup(&psc->sc_enabled); 831 return; 832 } 833 } 834 psc->last = psc->current; 835 836 switch (psc->inputstate) { 837 case 0: 838 if ((data & 0xc8) != 0x80) { 839 aprint_debug_dev(psc->sc_dev, 840 "pms_input: 0x%02x out of sync\n", data); 841 return; /* not in sync yet, discard input */ 842 } 843 /*FALLTHROUGH*/ 844 845 case 3: 846 if ((data & 8) == 8) { 847 aprint_debug_dev(psc->sc_dev, 848 "pms_input: dropped in relative mode, reset\n"); 849 psc->inputstate = 0; 850 psc->sc_enabled = 0; 851 wakeup(&psc->sc_enabled); 852 return; 853 } 854 } 855 856 psc->packet[psc->inputstate++] = data & 0xff; 857 if (psc->inputstate == 6) { 858 /* 859 * We have a complete packet. 860 * Extract the pertinent details. 861 */ 862 psc->inputstate = 0; 863 864 if ((psc->packet[0] & 0xfc) == 0x84 && 865 (psc->packet[3] & 0xcc) == 0xc4) { 866 /* W = SYNAPTICS_WIDTH_PASSTHROUGH, PS/2 passthrough */ 867 pms_synaptics_passthrough(psc); 868 } else { 869 pms_synaptics_parse(psc); 870 } 871 } 872 } 873 874 static inline int 875 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp, 876 int *palmp) 877 { 878 int fingers; 879 880 /* Assume no palm */ 881 *palmp = 0; 882 883 /* 884 * Apply some hysteresis when checking for a finger. 885 * When the finger is first applied, we ignore it until the 886 * pressure exceeds the 'high' threshold. The finger is considered 887 * removed only when pressure falls beneath the 'low' threshold. 888 */ 889 if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) || 890 (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low)) 891 fingers = 1; 892 else 893 fingers = 0; 894 895 /* 896 * If the pad can't do palm detection, skip the rest. 897 */ 898 if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0) 899 return (fingers); 900 901 /* 902 * Palm detection 903 */ 904 if (sp->sp_z > SYNAPTICS_FINGER_FLAT && 905 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN) 906 *palmp = 1; 907 908 if (sc->prev_fingers == 0 && 909 (sp->sp_z > SYNAPTICS_FINGER_FLAT || 910 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) { 911 /* 912 * Contact area or pressure is too great to be a finger. 913 * Just ignore it for now. 914 */ 915 return (0); 916 } 917 918 /* 919 * Detect 2 and 3 fingers if supported, but only if multiple 920 * fingers appear within the tap gesture time period. 921 */ 922 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER && 923 SYN_TIME(sc, sc->gesture_start_packet) < synaptics_gesture_length) { 924 switch (sp->sp_w) { 925 case SYNAPTICS_WIDTH_TWO_FINGERS: 926 fingers = 2; 927 break; 928 929 case SYNAPTICS_WIDTH_THREE_OR_MORE: 930 fingers = 3; 931 break; 932 933 case SYNAPTICS_WIDTH_PEN: 934 fingers = 1; 935 break; 936 937 default: 938 /* 939 * The width value can report spurious single-finger 940 * events after a multi-finger event. 941 */ 942 if (sc->prev_fingers > 1) 943 fingers = sc->prev_fingers; 944 else 945 fingers = 1; 946 break; 947 } 948 } 949 950 return (fingers); 951 } 952 953 static inline void 954 synaptics_gesture_detect(struct synaptics_softc *sc, 955 struct synaptics_packet *sp, int fingers) 956 { 957 int gesture_len, gesture_buttons; 958 int set_buttons; 959 960 gesture_len = SYN_TIME(sc, sc->gesture_start_packet); 961 gesture_buttons = sc->gesture_buttons; 962 963 if (fingers > 0 && (fingers == sc->prev_fingers)) { 964 /* Finger is still present */ 965 sc->gesture_move_x = abs(sc->gesture_start_x - sp->sp_x); 966 sc->gesture_move_y = abs(sc->gesture_start_y - sp->sp_y); 967 } else 968 if (fingers && sc->prev_fingers == 0) { 969 /* 970 * Finger was just applied. 971 * If the previous gesture was a single-click, set things 972 * up to deal with a possible drag or double-click gesture. 973 * Basically, if the finger is removed again within 974 * 'synaptics_gesture_length' packets, this is treated 975 * as a double-click. Otherwise we will emulate holding 976 * the left button down whilst dragging the mouse. 977 */ 978 if (SYN_IS_SINGLE_TAP(sc->gesture_type)) 979 sc->gesture_type |= SYN_GESTURE_DRAG; 980 981 sc->gesture_start_x = abs(sp->sp_x); 982 sc->gesture_start_y = abs(sp->sp_y); 983 sc->gesture_move_x = 0; 984 sc->gesture_move_y = 0; 985 sc->gesture_start_packet = sc->total_packets; 986 987 #ifdef DIAGNOSTIC 988 aprint_debug("Finger applied: gesture_start_x: %d gesture_start_y: %d\n", 989 sc->gesture_start_x, sc->gesture_start_y); 990 #endif 991 } else 992 if (fingers == 0 && sc->prev_fingers != 0) { 993 /* 994 * Finger was just removed. 995 * Check if the contact time and finger movement were 996 * small enough to qualify as a gesture. 997 * Ignore finger movement if multiple fingers were 998 * detected (the pad may report coordinates for any 999 * of the fingers). 1000 */ 1001 1002 #ifdef DIAGNOSTIC 1003 aprint_debug("Finger removed: gesture_len: %d (%d)\n", 1004 gesture_len, synaptics_gesture_length); 1005 aprint_debug("gesture_move_x: %d (%d) sp_x: %d\n", 1006 sc->gesture_move_x, synaptics_gesture_move, abs(sp->sp_x)); 1007 aprint_debug("gesture_move_y: %d (%d) sp_y: %d\n", 1008 sc->gesture_move_y, synaptics_gesture_move, abs(sp->sp_y)); 1009 #endif 1010 1011 if (gesture_len < synaptics_gesture_length && 1012 ((sc->gesture_move_x < synaptics_gesture_move && 1013 sc->gesture_move_y < synaptics_gesture_move))) { 1014 /* 1015 * Looking good so far. 1016 */ 1017 if (SYN_IS_DRAG(sc->gesture_type)) { 1018 /* 1019 * Promote this gesture to double-click. 1020 */ 1021 sc->gesture_type |= SYN_GESTURE_DOUBLE; 1022 sc->gesture_type &= ~SYN_GESTURE_SINGLE; 1023 } else { 1024 /* 1025 * Single tap gesture. Set the tap length timer 1026 * and flag a single-click. 1027 */ 1028 sc->gesture_tap_packet = sc->total_packets; 1029 sc->gesture_type |= SYN_GESTURE_SINGLE; 1030 1031 /* 1032 * The gesture can be modified depending on 1033 * the number of fingers detected. 1034 * 1035 * 1: Normal left button emulation. 1036 * 2: Either middle button or right button 1037 * depending on the value of the two_fingers 1038 * sysctl variable. 1039 * 3: Right button. 1040 */ 1041 switch (sc->prev_fingers) { 1042 case 2: 1043 if (synaptics_two_fingers_emul == 1) 1044 gesture_buttons |= PMS_RBUTMASK; 1045 else 1046 if (synaptics_two_fingers_emul == 2) 1047 gesture_buttons |= PMS_MBUTMASK; 1048 break; 1049 case 3: 1050 gesture_buttons |= PMS_RBUTMASK; 1051 break; 1052 default: 1053 gesture_buttons |= PMS_LBUTMASK; 1054 break; 1055 } 1056 } 1057 } 1058 1059 /* 1060 * Always clear drag state when the finger is removed. 1061 */ 1062 sc->gesture_type &= ~SYN_GESTURE_DRAG; 1063 } 1064 1065 if (sc->gesture_type == 0) { 1066 /* 1067 * There is no gesture in progress. 1068 * Clear emulated button state. 1069 */ 1070 sc->gesture_buttons = 0; 1071 return; 1072 } 1073 1074 /* 1075 * A gesture is in progress. 1076 */ 1077 set_buttons = 0; 1078 1079 if (SYN_IS_SINGLE_TAP(sc->gesture_type)) { 1080 /* 1081 * Single-click. 1082 * Activate the relevant button(s) until the 1083 * gesture tap timer has expired. 1084 */ 1085 if (SYN_TIME(sc, sc->gesture_tap_packet) < 1086 synaptics_gesture_length) 1087 set_buttons = 1; 1088 else 1089 sc->gesture_type &= ~SYN_GESTURE_SINGLE; 1090 } else 1091 if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) { 1092 /* 1093 * Double-click. 1094 * Activate the relevant button(s) once. 1095 */ 1096 set_buttons = 1; 1097 sc->gesture_type &= ~SYN_GESTURE_DOUBLE; 1098 } 1099 1100 if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) { 1101 /* 1102 * Single-click and drag. 1103 * Maintain button state until the finger is removed. 1104 */ 1105 sp->sp_left |= gesture_buttons & PMS_LBUTMASK; 1106 sp->sp_right |= gesture_buttons & PMS_RBUTMASK; 1107 sp->sp_middle |= gesture_buttons & PMS_MBUTMASK; 1108 } 1109 1110 sc->gesture_buttons = gesture_buttons; 1111 } 1112 1113 static inline int 1114 synaptics_filter_policy(struct synaptics_softc *sc, int *history, int value) 1115 { 1116 int a, b, rv, count; 1117 1118 count = sc->total_packets; 1119 1120 /* 1121 * Once we've accumulated at least SYN_HIST_SIZE values, combine 1122 * each new value with the previous two and return the average. 1123 * 1124 * This is necessary when the touchpad is operating in 80 packets 1125 * per second mode, as it performs little internal filtering on 1126 * reported values. 1127 * 1128 * Using a rolling average helps to filter out jitter caused by 1129 * tiny finger movements. 1130 */ 1131 if (sc->movement_history >= SYN_HIST_SIZE) { 1132 a = (history[(count + 0) % SYN_HIST_SIZE] + 1133 history[(count + 1) % SYN_HIST_SIZE]) / 2; 1134 1135 b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2; 1136 1137 rv = b - a; 1138 1139 /* 1140 * Don't report the movement if it's below a certain 1141 * threshold. 1142 */ 1143 if (abs(rv) < synaptics_movement_threshold) 1144 rv = 0; 1145 } else 1146 rv = 0; 1147 1148 /* 1149 * Add the new value to the history buffer. 1150 */ 1151 history[(count + 1) % SYN_HIST_SIZE] = value; 1152 1153 return (rv); 1154 } 1155 1156 /* Edge detection */ 1157 #define SYN_EDGE_TOP 1 1158 #define SYN_EDGE_BOTTOM 2 1159 #define SYN_EDGE_LEFT 4 1160 #define SYN_EDGE_RIGHT 8 1161 1162 static inline int 1163 synaptics_check_edge(int x, int y) 1164 { 1165 int rv = 0; 1166 1167 if (x < synaptics_edge_left) 1168 rv |= SYN_EDGE_LEFT; 1169 else 1170 if (x > synaptics_edge_right) 1171 rv |= SYN_EDGE_RIGHT; 1172 1173 if (y < synaptics_edge_bottom) 1174 rv |= SYN_EDGE_BOTTOM; 1175 else 1176 if (y > synaptics_edge_top) 1177 rv |= SYN_EDGE_TOP; 1178 1179 return (rv); 1180 } 1181 1182 static inline int 1183 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir) 1184 { 1185 1186 /* 1187 * When edge motion is enabled, synaptics_edge_motion_delta is 1188 * combined with the current delta, together with the direction 1189 * in which to simulate the motion. The result is added to 1190 * the delta derived from finger movement. This provides a smooth 1191 * transition from finger movement to edge motion. 1192 */ 1193 delta = synaptics_edge_motion_delta + (dir * delta); 1194 if (delta < 0) 1195 return (0); 1196 if (delta > synaptics_edge_motion_delta) 1197 return (synaptics_edge_motion_delta); 1198 return (delta); 1199 } 1200 1201 static inline int 1202 synaptics_scale(int delta, int scale, int *remp) 1203 { 1204 int rv; 1205 1206 /* 1207 * Scale the raw delta in Synaptics coordinates (0-6143) into 1208 * something more reasonable by dividing the raw delta by a 1209 * scale factor. Any remainder from the previous scale result 1210 * is added to the current delta before scaling. 1211 * This prevents loss of resolution for very small/slow 1212 * movements of the finger. 1213 */ 1214 delta += *remp; 1215 rv = delta / scale; 1216 *remp = delta % scale; 1217 1218 return (rv); 1219 } 1220 1221 static inline void 1222 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp, 1223 int *dxp, int *dyp) 1224 { 1225 int dx, dy, edge; 1226 1227 /* 1228 * Compute the next values of dx and dy 1229 */ 1230 dx = synaptics_filter_policy(sc, sc->history_x, sp->sp_x); 1231 dy = synaptics_filter_policy(sc, sc->history_y, sp->sp_y); 1232 1233 /* 1234 * If we're dealing with a drag gesture, and the finger moves to 1235 * the edge of the touchpad, apply edge motion emulation if it 1236 * is enabled. 1237 */ 1238 if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) { 1239 edge = synaptics_check_edge(sp->sp_x, sp->sp_y); 1240 1241 if (edge & SYN_EDGE_LEFT) 1242 dx -= synaptics_edge_motion(sc, dx, 1); 1243 if (edge & SYN_EDGE_RIGHT) 1244 dx += synaptics_edge_motion(sc, dx, -1); 1245 if (edge & SYN_EDGE_BOTTOM) 1246 dy -= synaptics_edge_motion(sc, dy, 1); 1247 if (edge & SYN_EDGE_TOP) 1248 dy += synaptics_edge_motion(sc, dy, -1); 1249 } 1250 1251 /* 1252 * Apply scaling to both deltas 1253 */ 1254 dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x); 1255 dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y); 1256 1257 /* 1258 * Clamp deltas to specified maximums. 1259 */ 1260 if (dx > synaptics_max_speed_x) 1261 dx = synaptics_max_speed_x; 1262 if (dy > synaptics_max_speed_y) 1263 dy = synaptics_max_speed_y; 1264 1265 *dxp = dx; 1266 *dyp = dy; 1267 1268 sc->movement_history++; 1269 } 1270 1271 static void 1272 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp) 1273 { 1274 struct synaptics_softc *sc = &psc->u.synaptics; 1275 int dx, dy, dz; 1276 int fingers, palm, buttons, changed; 1277 int s; 1278 1279 /* 1280 * Do Z-axis emulation using up/down buttons if required. 1281 * Note that the pad will send a one second burst of packets 1282 * when an up/down button is pressed and held. At the moment 1283 * we don't deal with auto-repeat, so convert the burst into 1284 * a one-shot. 1285 */ 1286 dz = 0; 1287 if (synaptics_up_down_emul == 2) { 1288 if (sc->up_down == 0) { 1289 if (sp->sp_up && sp->sp_down) { 1290 /* 1291 * Most up/down buttons will be actuated using 1292 * a rocker switch, so we should never see 1293 * them both simultaneously. But just in case, 1294 * treat this situation as a middle button 1295 * event. 1296 */ 1297 sp->sp_middle = 1; 1298 } else 1299 if (sp->sp_up) 1300 dz = -synaptics_up_down_motion_delta; 1301 else 1302 if (sp->sp_down) 1303 dz = synaptics_up_down_motion_delta; 1304 } 1305 1306 sc->up_down = sp->sp_up | sp->sp_down; 1307 sp->sp_up = sp->sp_down = 0; 1308 } 1309 1310 /* 1311 * Determine whether or not a finger is on the pad. 1312 * On some pads, this will return the number of fingers 1313 * detected. 1314 */ 1315 fingers = synaptics_finger_detect(sc, sp, &palm); 1316 1317 /* 1318 * Do gesture processing only if we didn't detect a palm. 1319 */ 1320 if (palm == 0) 1321 synaptics_gesture_detect(sc, sp, fingers); 1322 else 1323 sc->gesture_type = sc->gesture_buttons = 0; 1324 1325 /* 1326 * Determine what buttons to report 1327 */ 1328 buttons = (sp->sp_left ? 0x1 : 0) | 1329 (sp->sp_middle ? 0x2 : 0) | 1330 (sp->sp_right ? 0x4 : 0) | 1331 (sp->sp_up ? 0x8 : 0) | 1332 (sp->sp_down ? 0x10 : 0); 1333 changed = buttons ^ (psc->buttons & 0x1f); 1334 psc->buttons ^= changed; 1335 1336 sc->prev_fingers = fingers; 1337 sc->total_packets++; 1338 1339 /* 1340 * Do movement processing IFF we have a single finger and no palm. 1341 */ 1342 if (fingers == 1 && palm == 0) 1343 synaptics_movement(sc, sp, &dx, &dy); 1344 else { 1345 /* 1346 * No valid finger. Therefore no movement. 1347 */ 1348 sc->movement_history = 0; 1349 sc->rem_x = sc->rem_y = 0; 1350 dx = dy = 0; 1351 } 1352 1353 /* 1354 * Pass the final results up to wsmouse_input() if necessary. 1355 */ 1356 if (dx || dy || dz || changed) { 1357 buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7); 1358 s = spltty(); 1359 wsmouse_input(psc->sc_wsmousedev, 1360 buttons, 1361 dx, dy, dz, 0, 1362 WSMOUSE_INPUT_DELTA); 1363 splx(s); 1364 } 1365 } 1366