1 /* 2 * Copyright (c) 2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> and was subsequently ported, 6 * modified and enhanced for FreeBSD by Michael Gmelin <freebsd@grem.de>. 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 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * CYAPA - Cypress APA trackpad with I2C Interface driver 41 * 42 * Based on DragonFlyBSD's cyapa driver, which referenced the linux 43 * cyapa.c driver to figure out the bootstrapping and commands. 44 * 45 * Unable to locate any datasheet for the device. 46 * 47 * 48 * Trackpad layout: 49 * 50 * 2/3 1/3 51 * +--------------------+------------+ 52 * | | Middle | 53 * | | Button | 54 * | Left | | 55 * | Button +------------+ 56 * | | Right | 57 * | | Button | 58 * +--------------------+............| 59 * | Thumb/Button Area | 15% 60 * +---------------------------------+ 61 * 62 * 63 * FEATURES 64 * 65 * IMPS/2 emulation - Emulates the IntelliMouse protocol. 66 * 67 * Jitter supression - Implements 2-pixel hysteresis with memory. 68 * 69 * Jump detecion - Detect jumps caused by touchpad. 70 * 71 * Two finger scrolling - Use two fingers for Z axis scrolling. 72 * 73 * Button down/2nd finger - While one finger clicks and holds down the 74 * touchpad, the second one can be used to move 75 * the mouse cursor. Useful for drawing or 76 * selecting text. 77 * 78 * Thumb/Button Area - The lower 15%* of the trackpad will not affect 79 * the mouse cursor position. This allows for high 80 * precision clicking, by controlling the cursor 81 * with the index finger and pushing/holding the 82 * pad down with the thumb. 83 * * can be changed using sysctl 84 * 85 * Track-pad button - Push physical button. Left 2/3rds of the pad 86 * will issue a LEFT button event, upper right 87 * corner will issue a MIDDLE button event, 88 * lower right corner will issue a RIGHT button 89 * event. Optional tap support can be enabled 90 * and configured using sysctl. 91 * 92 * WARNINGS 93 * 94 * These trackpads get confused when three or more fingers are down on the 95 * same horizontal axis and will start to glitch the finger detection. 96 * Removing your hand for a few seconds will allow the trackpad to 97 * recalibrate. Generally speaking, when using three or more fingers 98 * please try to place at least one finger off-axis (a little above or 99 * below) the other two. 100 */ 101 102 #include <sys/param.h> 103 #include <sys/bus.h> 104 #include <sys/conf.h> 105 #include <sys/event.h> 106 #include <sys/fcntl.h> 107 #include <sys/kernel.h> 108 #include <sys/kthread.h> 109 #include <sys/lock.h> 110 #include <sys/lockmgr.h> 111 #include <sys/malloc.h> 112 #include <sys/mbuf.h> 113 #include <sys/module.h> 114 #include <sys/mouse.h> 115 #include <sys/mutex.h> 116 #include <sys/poll.h> 117 #include <sys/selinfo.h> 118 #include <sys/sysctl.h> 119 #include <sys/sysctl.h> 120 #include <sys/systm.h> 121 #include <sys/systm.h> 122 #include <sys/uio.h> 123 #include <sys/vnode.h> 124 125 #include <dev/smbus/smbconf.h> 126 #include <dev/smbus/smbus.h> 127 #include <dev/cyapa/cyapa.h> 128 129 #include "smbus_if.h" 130 #include "bus_if.h" 131 #include "device_if.h" 132 133 #define CYAPA_BUFSIZE 128 /* power of 2 */ 134 #define CYAPA_BUFMASK (CYAPA_BUFSIZE - 1) 135 136 #define ZSCALE 15 137 138 #define TIME_TO_IDLE (hz * 10) 139 #define TIME_TO_RESET (hz * 3) 140 141 static MALLOC_DEFINE(M_CYAPA, "cyapa", "CYAPA device data"); 142 143 struct cyapa_fifo { 144 int rindex; 145 int windex; 146 char buf[CYAPA_BUFSIZE]; 147 }; 148 149 struct cyapa_softc { 150 device_t dev; 151 int count; /* >0 if device opened */ 152 int addr; 153 struct cdev *devnode; 154 struct selinfo selinfo; 155 struct mtx mutex; 156 157 int cap_resx; 158 int cap_resy; 159 int cap_phyx; 160 int cap_phyy; 161 uint8_t cap_buttons; 162 163 int detaching; /* driver is detaching */ 164 int poll_thread_running; /* poll thread is running */ 165 166 /* PS/2 mouse emulation */ 167 int track_x; /* current tracking */ 168 int track_y; 169 int track_z; 170 int track_z_ticks; 171 uint16_t track_but; 172 char track_id; /* first finger id */ 173 int track_nfingers; 174 int delta_x; /* accumulation -> report */ 175 int delta_y; 176 int delta_z; 177 int fuzz_x; 178 int fuzz_y; 179 int fuzz_z; 180 int touch_x; /* touch down coordinates */ 181 int touch_y; 182 int touch_z; 183 int finger1_ticks; 184 int finger2_ticks; 185 int finger3_ticks; 186 uint16_t reported_but; 187 188 struct cyapa_fifo rfifo; /* device->host */ 189 struct cyapa_fifo wfifo; /* host->device */ 190 uint8_t ps2_cmd; /* active p2_cmd waiting for data */ 191 uint8_t ps2_acked; 192 int active_tick; 193 int data_signal; 194 int blocked; 195 int isselect; 196 int reporting_mode; /* 0=disabled 1=enabled */ 197 int scaling_mode; /* 0=1:1 1=2:1 */ 198 int remote_mode; /* 0 for streaming mode */ 199 int zenabled; /* z-axis enabled (mode 1 or 2) */ 200 mousehw_t hw; /* hardware information */ 201 mousemode_t mode; /* mode */ 202 int poll_ticks; 203 }; 204 205 struct cyapa_cdevpriv { 206 struct cyapa_softc *sc; 207 }; 208 209 #define CYPOLL_SHUTDOWN 0x0001 210 211 static void cyapa_poll_thread(void *arg); 212 static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, 213 int freq); 214 static void cyapa_set_power_mode(struct cyapa_softc *sc, int mode); 215 216 static int fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo); 217 static size_t fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo); 218 static char *fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, 219 size_t n); 220 static char *fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, 221 size_t n); 222 static uint8_t fifo_read_char(struct cyapa_softc *sc, 223 struct cyapa_fifo *fifo); 224 static void fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, 225 uint8_t c); 226 static size_t fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo); 227 static void fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo); 228 229 static int cyapa_fuzz(int delta, int *fuzz); 230 231 static int cyapa_idle_freq = 1; 232 SYSCTL_INT(_debug, OID_AUTO, cyapa_idle_freq, CTLFLAG_RW, 233 &cyapa_idle_freq, 0, "Scan frequency in idle mode"); 234 static int cyapa_slow_freq = 20; 235 SYSCTL_INT(_debug, OID_AUTO, cyapa_slow_freq, CTLFLAG_RW, 236 &cyapa_slow_freq, 0, "Scan frequency in slow mode "); 237 static int cyapa_norm_freq = 100; 238 SYSCTL_INT(_debug, OID_AUTO, cyapa_norm_freq, CTLFLAG_RW, 239 &cyapa_norm_freq, 0, "Normal scan frequency"); 240 static int cyapa_minpressure = 12; 241 SYSCTL_INT(_debug, OID_AUTO, cyapa_minpressure, CTLFLAG_RW, 242 &cyapa_minpressure, 0, "Minimum pressure to detect finger"); 243 static int cyapa_enable_tapclick = 0; 244 SYSCTL_INT(_debug, OID_AUTO, cyapa_enable_tapclick, CTLFLAG_RW, 245 &cyapa_enable_tapclick, 0, "Enable tap to click"); 246 static int cyapa_tapclick_min_ticks = 1; 247 SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_min_ticks, CTLFLAG_RW, 248 &cyapa_tapclick_min_ticks, 0, "Minimum tap duration for click"); 249 static int cyapa_tapclick_max_ticks = 8; 250 SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_max_ticks, CTLFLAG_RW, 251 &cyapa_tapclick_max_ticks, 0, "Maximum tap duration for click"); 252 static int cyapa_move_min_ticks = 4; 253 SYSCTL_INT(_debug, OID_AUTO, cyapa_move_min_ticks, CTLFLAG_RW, 254 &cyapa_move_min_ticks, 0, 255 "Minimum ticks before cursor position is changed"); 256 static int cyapa_scroll_wait_ticks = 0; 257 SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_wait_ticks, CTLFLAG_RW, 258 &cyapa_scroll_wait_ticks, 0, 259 "Wait N ticks before starting to scroll"); 260 static int cyapa_scroll_stick_ticks = 15; 261 SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_stick_ticks, CTLFLAG_RW, 262 &cyapa_scroll_stick_ticks, 0, 263 "Prevent cursor move on single finger for N ticks after scroll"); 264 static int cyapa_thumbarea_percent = 15; 265 SYSCTL_INT(_debug, OID_AUTO, cyapa_thumbarea_percent, CTLFLAG_RW, 266 &cyapa_thumbarea_percent, 0, 267 "Size of bottom thumb area in percent"); 268 269 static int cyapa_debug = 0; 270 SYSCTL_INT(_debug, OID_AUTO, cyapa_debug, CTLFLAG_RW, 271 &cyapa_debug, 0, "Enable debugging"); 272 static int cyapa_reset = 0; 273 SYSCTL_INT(_debug, OID_AUTO, cyapa_reset, CTLFLAG_RW, 274 &cyapa_reset, 0, "Reset track pad"); 275 276 static void 277 cyapa_lock(struct cyapa_softc *sc) 278 { 279 280 mtx_lock(&sc->mutex); 281 } 282 283 static void 284 cyapa_unlock(struct cyapa_softc *sc) 285 { 286 287 mtx_unlock(&sc->mutex); 288 } 289 290 #define CYAPA_LOCK_ASSERT(sc) mtx_assert(&(sc)->mutex, MA_OWNED); 291 292 /* 293 * Notify if possible receive data ready. Must be called 294 * with sc->mutex held (cyapa_lock(sc)). 295 */ 296 static void 297 cyapa_notify(struct cyapa_softc *sc) 298 { 299 300 CYAPA_LOCK_ASSERT(sc); 301 302 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) { 303 KNOTE_LOCKED(&sc->selinfo.si_note, 0); 304 if (sc->blocked || sc->isselect) { 305 if (sc->blocked) { 306 sc->blocked = 0; 307 wakeup(&sc->blocked); 308 } 309 if (sc->isselect) { 310 sc->isselect = 0; 311 selwakeup(&sc->selinfo); 312 } 313 } 314 } 315 } 316 317 /* 318 * Initialize the device 319 */ 320 static int 321 init_device(device_t dev, struct cyapa_cap *cap, int addr, int probe) 322 { 323 static char bl_exit[] = { 324 0x00, 0xff, 0xa5, 0x00, 0x01, 325 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; 326 static char bl_deactivate[] = { 327 0x00, 0xff, 0x3b, 0x00, 0x01, 328 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; 329 device_t bus; 330 struct cyapa_boot_regs boot; 331 int error; 332 int retries; 333 334 bus = device_get_parent(dev); /* smbus */ 335 336 /* Get status */ 337 error = smbus_trans(bus, addr, CMD_BOOT_STATUS, 338 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 339 NULL, 0, (void *)&boot, sizeof(boot), NULL); 340 if (error) 341 goto done; 342 343 /* 344 * Bootstrap the device if necessary. It can take up to 2 seconds 345 * for the device to fully initialize. 346 */ 347 retries = 20; 348 while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) { 349 if (boot.boot & CYAPA_BOOT_BUSY) { 350 /* Busy, wait loop. */ 351 } else if (boot.error & CYAPA_ERROR_BOOTLOADER) { 352 /* Magic */ 353 error = smbus_trans(bus, addr, CMD_BOOT_STATUS, 354 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 355 bl_deactivate, sizeof(bl_deactivate), 356 NULL, 0, NULL); 357 if (error) 358 goto done; 359 } else { 360 /* Magic */ 361 error = smbus_trans(bus, addr, CMD_BOOT_STATUS, 362 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 363 bl_exit, sizeof(bl_exit), NULL, 0, NULL); 364 if (error) 365 goto done; 366 } 367 pause("cyapab1", (hz * 2) / 10); 368 --retries; 369 error = smbus_trans(bus, addr, CMD_BOOT_STATUS, 370 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 371 NULL, 0, (void *)&boot, sizeof(boot), NULL); 372 if (error) 373 goto done; 374 } 375 376 if (retries == 0) { 377 device_printf(dev, "Unable to bring device out of bootstrap\n"); 378 error = ENXIO; 379 goto done; 380 } 381 382 /* Check identity */ 383 if (cap) { 384 error = smbus_trans(bus, addr, CMD_QUERY_CAPABILITIES, 385 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 386 NULL, 0, (void *)cap, sizeof(*cap), NULL); 387 388 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) { 389 device_printf(dev, "Product ID \"%5.5s\" mismatch\n", 390 cap->prod_ida); 391 error = ENXIO; 392 } 393 } 394 error = smbus_trans(bus, addr, CMD_BOOT_STATUS, 395 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 396 NULL, 0, (void *)&boot, sizeof(boot), NULL); 397 398 if (probe == 0) /* official init */ 399 device_printf(dev, "cyapa init status %02x\n", boot.stat); 400 else if (probe == 2) 401 device_printf(dev, "cyapa reset status %02x\n", boot.stat); 402 403 done: 404 if (error) 405 device_printf(dev, "Unable to initialize\n"); 406 return (error); 407 } 408 409 static int cyapa_probe(device_t); 410 static int cyapa_attach(device_t); 411 static int cyapa_detach(device_t); 412 static void cyapa_cdevpriv_dtor(void*); 413 414 static devclass_t cyapa_devclass; 415 416 static device_method_t cyapa_methods[] = { 417 /* device interface */ 418 DEVMETHOD(device_probe, cyapa_probe), 419 DEVMETHOD(device_attach, cyapa_attach), 420 DEVMETHOD(device_detach, cyapa_detach), 421 422 DEVMETHOD_END 423 }; 424 425 static driver_t cyapa_driver = { 426 "cyapa", 427 cyapa_methods, 428 sizeof(struct cyapa_softc), 429 }; 430 431 static d_open_t cyapaopen; 432 static d_ioctl_t cyapaioctl; 433 static d_read_t cyaparead; 434 static d_write_t cyapawrite; 435 static d_kqfilter_t cyapakqfilter; 436 static d_poll_t cyapapoll; 437 438 static struct cdevsw cyapa_cdevsw = { 439 .d_version = D_VERSION, 440 .d_open = cyapaopen, 441 .d_ioctl = cyapaioctl, 442 .d_read = cyaparead, 443 .d_write = cyapawrite, 444 .d_kqfilter = cyapakqfilter, 445 .d_poll = cyapapoll, 446 }; 447 448 static int 449 cyapa_probe(device_t dev) 450 { 451 struct cyapa_cap cap; 452 int addr; 453 int error; 454 455 addr = smbus_get_addr(dev); 456 457 /* 458 * 0x67 - cypress trackpad on the acer c720 459 * (other devices might use other ids). 460 */ 461 if (addr != 0x67) 462 return (ENXIO); 463 464 error = init_device(dev, &cap, addr, 1); 465 if (error != 0) 466 return (ENXIO); 467 468 device_set_desc(dev, "Cypress APA I2C Trackpad"); 469 470 return (BUS_PROBE_VENDOR); 471 } 472 473 static int 474 cyapa_attach(device_t dev) 475 { 476 struct cyapa_softc *sc; 477 struct cyapa_cap cap; 478 int unit; 479 int addr; 480 481 sc = device_get_softc(dev); 482 sc->reporting_mode = 1; 483 484 unit = device_get_unit(dev); 485 addr = smbus_get_addr(dev); 486 487 if (init_device(dev, &cap, addr, 0)) 488 return (ENXIO); 489 490 mtx_init(&sc->mutex, "cyapa", NULL, MTX_DEF); 491 492 sc->dev = dev; 493 sc->addr = addr; 494 495 knlist_init_mtx(&sc->selinfo.si_note, &sc->mutex); 496 497 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) | 498 cap.max_abs_x_low; 499 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) | 500 cap.max_abs_y_low; 501 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) | 502 cap.phy_siz_x_low; 503 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) | 504 cap.phy_siz_y_low; 505 sc->cap_buttons = cap.buttons; 506 507 device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n", 508 cap.prod_ida, cap.prod_idb, cap.prod_idc, 509 ((cap.buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'), 510 ((cap.buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'), 511 ((cap.buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'), 512 sc->cap_resx, sc->cap_resy); 513 514 sc->hw.buttons = 5; 515 sc->hw.iftype = MOUSE_IF_PS2; 516 sc->hw.type = MOUSE_MOUSE; 517 sc->hw.model = MOUSE_MODEL_INTELLI; 518 sc->hw.hwid = addr; 519 520 sc->mode.protocol = MOUSE_PROTO_PS2; 521 sc->mode.rate = 100; 522 sc->mode.resolution = 4; 523 sc->mode.accelfactor = 1; 524 sc->mode.level = 0; 525 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE; 526 527 /* Setup input event tracking */ 528 cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE); 529 530 /* Start the polling thread */ 531 kthread_add(cyapa_poll_thread, sc, NULL, NULL, 532 0, 0, "cyapa-poll"); 533 534 sc->devnode = make_dev(&cyapa_cdevsw, unit, 535 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit); 536 537 sc->devnode->si_drv1 = sc; 538 539 return (0); 540 } 541 542 static int 543 cyapa_detach(device_t dev) 544 { 545 struct cyapa_softc *sc; 546 547 sc = device_get_softc(dev); 548 549 /* Cleanup poller thread */ 550 cyapa_lock(sc); 551 while (sc->poll_thread_running) { 552 sc->detaching = 1; 553 mtx_sleep(&sc->detaching, &sc->mutex, PCATCH, "cyapadet", hz); 554 } 555 cyapa_unlock(sc); 556 557 destroy_dev(sc->devnode); 558 559 knlist_clear(&sc->selinfo.si_note, 0); 560 seldrain(&sc->selinfo); 561 knlist_destroy(&sc->selinfo.si_note); 562 563 mtx_destroy(&sc->mutex); 564 565 return (0); 566 } 567 568 /* 569 * USER DEVICE I/O FUNCTIONS 570 */ 571 static int 572 cyapaopen(struct cdev *dev, int oflags, int devtype, struct thread *td) 573 { 574 struct cyapa_cdevpriv *priv; 575 int error; 576 577 priv = malloc(sizeof(*priv), M_CYAPA, M_WAITOK | M_ZERO); 578 priv->sc = dev->si_drv1; 579 580 error = devfs_set_cdevpriv(priv, cyapa_cdevpriv_dtor); 581 if (error == 0) { 582 cyapa_lock(priv->sc); 583 priv->sc->count++; 584 cyapa_unlock(priv->sc); 585 } 586 else 587 free(priv, M_CYAPA); 588 589 return (error); 590 } 591 592 static void 593 cyapa_cdevpriv_dtor(void *data) 594 { 595 struct cyapa_cdevpriv *priv; 596 597 priv = data; 598 KASSERT(priv != NULL, ("cyapa cdevpriv should not be NULL!")); 599 600 cyapa_lock(priv->sc); 601 priv->sc->count--; 602 cyapa_unlock(priv->sc); 603 604 free(priv, M_CYAPA); 605 } 606 607 static int 608 cyaparead(struct cdev *dev, struct uio *uio, int ioflag) 609 { 610 struct cyapa_softc *sc; 611 int error; 612 int didread; 613 size_t n; 614 char* ptr; 615 616 sc = dev->si_drv1; 617 /* If buffer is empty, load a new event if it is ready */ 618 cyapa_lock(sc); 619 again: 620 if (fifo_empty(sc, &sc->rfifo) && 621 (sc->data_signal || sc->delta_x || sc->delta_y || 622 sc->track_but != sc->reported_but)) { 623 uint8_t c0; 624 uint16_t but; 625 int delta_x; 626 int delta_y; 627 int delta_z; 628 629 /* Accumulate delta_x, delta_y */ 630 sc->data_signal = 0; 631 delta_x = sc->delta_x; 632 delta_y = sc->delta_y; 633 delta_z = sc->delta_z; 634 if (delta_x > 255) { 635 delta_x = 255; 636 sc->data_signal = 1; 637 } 638 if (delta_x < -256) { 639 delta_x = -256; 640 sc->data_signal = 1; 641 } 642 if (delta_y > 255) { 643 delta_y = 255; 644 sc->data_signal = 1; 645 } 646 if (delta_y < -256) { 647 delta_y = -256; 648 sc->data_signal = 1; 649 } 650 if (delta_z > 255) { 651 delta_z = 255; 652 sc->data_signal = 1; 653 } 654 if (delta_z < -256) { 655 delta_z = -256; 656 sc->data_signal = 1; 657 } 658 but = sc->track_but; 659 660 /* Adjust baseline for next calculation */ 661 sc->delta_x -= delta_x; 662 sc->delta_y -= delta_y; 663 sc->delta_z -= delta_z; 664 sc->reported_but = but; 665 666 /* 667 * Fuzz reduces movement jitter by introducing some 668 * hysteresis. It operates without cumulative error so 669 * if you swish around quickly and return your finger to 670 * where it started, so to will the mouse. 671 */ 672 delta_x = cyapa_fuzz(delta_x, &sc->fuzz_x); 673 delta_y = cyapa_fuzz(delta_y, &sc->fuzz_y); 674 delta_z = cyapa_fuzz(delta_z, &sc->fuzz_z); 675 676 /* 677 * Generate report 678 */ 679 c0 = 0; 680 if (delta_x < 0) 681 c0 |= 0x10; 682 if (delta_y < 0) 683 c0 |= 0x20; 684 c0 |= 0x08; 685 if (but & CYAPA_FNGR_LEFT) 686 c0 |= 0x01; 687 if (but & CYAPA_FNGR_MIDDLE) 688 c0 |= 0x04; 689 if (but & CYAPA_FNGR_RIGHT) 690 c0 |= 0x02; 691 692 fifo_write_char(sc, &sc->rfifo, c0); 693 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_x); 694 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_y); 695 switch(sc->zenabled) { 696 case 1: 697 /* Z axis all 8 bits */ 698 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_z); 699 break; 700 case 2: 701 /* 702 * Z axis low 4 bits + 4th button and 5th button 703 * (high 2 bits must be left 0). Auto-scale 704 * delta_z to fit to avoid a wrong-direction 705 * overflow (don't try to retain the remainder). 706 */ 707 while (delta_z > 7 || delta_z < -8) 708 delta_z >>= 1; 709 c0 = (uint8_t)delta_z & 0x0F; 710 fifo_write_char(sc, &sc->rfifo, c0); 711 break; 712 default: 713 /* basic PS/2 */ 714 break; 715 } 716 cyapa_notify(sc); 717 } 718 719 /* Blocking / Non-blocking */ 720 error = 0; 721 didread = (uio->uio_resid == 0); 722 723 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(sc, &sc->rfifo)) { 724 if (sc->data_signal) 725 goto again; 726 sc->blocked = 1; 727 error = mtx_sleep(&sc->blocked, &sc->mutex, PCATCH, "cyablk", 0); 728 if (error) 729 break; 730 } 731 732 /* Return any buffered data */ 733 while (error == 0 && uio->uio_resid && 734 (n = fifo_ready(sc, &sc->rfifo)) > 0) { 735 if (n > uio->uio_resid) 736 n = uio->uio_resid; 737 ptr = fifo_read(sc, &sc->rfifo, 0); 738 cyapa_unlock(sc); 739 error = uiomove(ptr, n, uio); 740 cyapa_lock(sc); 741 if (error) 742 break; 743 fifo_read(sc, &sc->rfifo, n); 744 didread = 1; 745 } 746 cyapa_unlock(sc); 747 748 if (error == 0 && didread == 0) { 749 error = EWOULDBLOCK; 750 } 751 return (didread ? 0 : error); 752 } 753 754 static int 755 cyapawrite(struct cdev *dev, struct uio *uio, int ioflag) 756 { 757 struct cyapa_softc *sc; 758 int error; 759 int cmd_completed; 760 size_t n; 761 uint8_t c0; 762 char* ptr; 763 764 sc = dev->si_drv1; 765 again: 766 /* 767 * Copy data from userland. This will also cross-over the end 768 * of the fifo and keep filling. 769 */ 770 cyapa_lock(sc); 771 while ((n = fifo_space(sc, &sc->wfifo)) > 0 && uio->uio_resid) { 772 if (n > uio->uio_resid) 773 n = uio->uio_resid; 774 ptr = fifo_write(sc, &sc->wfifo, 0); 775 cyapa_unlock(sc); 776 error = uiomove(ptr, n, uio); 777 cyapa_lock(sc); 778 if (error) 779 break; 780 fifo_write(sc, &sc->wfifo, n); 781 } 782 783 /* Handle commands */ 784 cmd_completed = (fifo_ready(sc, &sc->wfifo) != 0); 785 while (fifo_ready(sc, &sc->wfifo) && cmd_completed && error == 0) { 786 if (sc->ps2_cmd == 0) 787 sc->ps2_cmd = fifo_read_char(sc, &sc->wfifo); 788 switch(sc->ps2_cmd) { 789 case 0xE6: 790 /* SET SCALING 1:1 */ 791 sc->scaling_mode = 0; 792 fifo_write_char(sc, &sc->rfifo, 0xFA); 793 break; 794 case 0xE7: 795 /* SET SCALING 2:1 */ 796 sc->scaling_mode = 1; 797 fifo_write_char(sc, &sc->rfifo, 0xFA); 798 break; 799 case 0xE8: 800 /* SET RESOLUTION +1 byte */ 801 if (sc->ps2_acked == 0) { 802 sc->ps2_acked = 1; 803 fifo_write_char(sc, &sc->rfifo, 0xFA); 804 } 805 if (fifo_ready(sc, &sc->wfifo) == 0) { 806 cmd_completed = 0; 807 break; 808 } 809 sc->mode.resolution = fifo_read_char(sc, &sc->wfifo); 810 fifo_write_char(sc, &sc->rfifo, 0xFA); 811 break; 812 case 0xE9: 813 /* 814 * STATUS REQUEST 815 * 816 * byte1: 817 * bit 7 0 818 * bit 6 Mode (1=remote mode, 0=stream mode) 819 * bit 5 Enable (data reporting enabled) 820 * bit 4 Scaling (0=1:1 1=2:1) 821 * bit 3 0 822 * bit 2 LEFT BUTTON (1 if pressed) 823 * bit 1 MIDDLE BUTTON (1 if pressed) 824 * bit 0 RIGHT BUTTON (1 if pressed) 825 * 826 * byte2: resolution counts/mm 827 * byte3: sample rate 828 */ 829 c0 = 0; 830 if (sc->remote_mode) 831 c0 |= 0x40; 832 if (sc->reporting_mode) 833 c0 |= 0x20; 834 if (sc->scaling_mode) 835 c0 |= 0x10; 836 if (sc->track_but & CYAPA_FNGR_LEFT) 837 c0 |= 0x04; 838 if (sc->track_but & CYAPA_FNGR_MIDDLE) 839 c0 |= 0x02; 840 if (sc->track_but & CYAPA_FNGR_RIGHT) 841 c0 |= 0x01; 842 fifo_write_char(sc, &sc->rfifo, 0xFA); 843 fifo_write_char(sc, &sc->rfifo, c0); 844 fifo_write_char(sc, &sc->rfifo, 0x00); 845 fifo_write_char(sc, &sc->rfifo, 100); 846 break; 847 case 0xEA: 848 /* Set stream mode and reset movement counters */ 849 sc->remote_mode = 0; 850 fifo_write_char(sc, &sc->rfifo, 0xFA); 851 sc->delta_x = 0; 852 sc->delta_y = 0; 853 sc->delta_z = 0; 854 break; 855 case 0xEB: 856 /* 857 * Read Data (if in remote mode). If not in remote 858 * mode force an event. 859 */ 860 fifo_write_char(sc, &sc->rfifo, 0xFA); 861 sc->data_signal = 1; 862 break; 863 case 0xEC: 864 /* Reset Wrap Mode (ignored) */ 865 fifo_write_char(sc, &sc->rfifo, 0xFA); 866 break; 867 case 0xEE: 868 /* Set Wrap Mode (ignored) */ 869 fifo_write_char(sc, &sc->rfifo, 0xFA); 870 break; 871 case 0xF0: 872 /* Set Remote Mode */ 873 sc->remote_mode = 1; 874 fifo_write_char(sc, &sc->rfifo, 0xFA); 875 sc->delta_x = 0; 876 sc->delta_y = 0; 877 sc->delta_z = 0; 878 break; 879 case 0xF2: 880 /* 881 * Get Device ID 882 * 883 * If we send 0x00 - normal PS/2 mouse, no Z-axis 884 * 885 * If we send 0x03 - Intellimouse, data packet has 886 * an additional Z movement byte (8 bits signed). 887 * (also reset movement counters) 888 * 889 * If we send 0x04 - Now includes z-axis and the 890 * 4th and 5th mouse buttons. 891 */ 892 fifo_write_char(sc, &sc->rfifo, 0xFA); 893 switch(sc->zenabled) { 894 case 1: 895 fifo_write_char(sc, &sc->rfifo, 0x03); 896 break; 897 case 2: 898 fifo_write_char(sc, &sc->rfifo, 0x04); 899 break; 900 default: 901 fifo_write_char(sc, &sc->rfifo, 0x00); 902 break; 903 } 904 sc->delta_x = 0; 905 sc->delta_y = 0; 906 sc->delta_z = 0; 907 break; 908 case 0xF3: 909 /* 910 * Set Sample Rate 911 * 912 * byte1: the sample rate 913 */ 914 if (sc->ps2_acked == 0) { 915 sc->ps2_acked = 1; 916 fifo_write_char(sc, &sc->rfifo, 0xFA); 917 } 918 if (fifo_ready(sc, &sc->wfifo) == 0) { 919 cmd_completed = 0; 920 break; 921 } 922 sc->mode.rate = fifo_read_char(sc, &sc->wfifo); 923 fifo_write_char(sc, &sc->rfifo, 0xFA); 924 925 /* 926 * zenabling sequence: 200,100,80 (device id 0x03) 927 * 200,200,80 (device id 0x04) 928 * 929 * We support id 0x03 (no 4th or 5th button). 930 * We support id 0x04 (w/ 4th and 5th button). 931 */ 932 if (sc->zenabled == 0 && sc->mode.rate == 200) 933 sc->zenabled = -1; 934 else if (sc->zenabled == -1 && sc->mode.rate == 100) 935 sc->zenabled = -2; 936 else if (sc->zenabled == -1 && sc->mode.rate == 200) 937 sc->zenabled = -3; 938 else if (sc->zenabled == -2 && sc->mode.rate == 80) 939 sc->zenabled = 1; /* z-axis mode */ 940 else if (sc->zenabled == -3 && sc->mode.rate == 80) 941 sc->zenabled = 2; /* z-axis+but4/5 */ 942 if (sc->mode.level) 943 sc->zenabled = 1; 944 break; 945 case 0xF4: 946 /* Enable data reporting. Only effects stream mode. */ 947 fifo_write_char(sc, &sc->rfifo, 0xFA); 948 sc->reporting_mode = 1; 949 break; 950 case 0xF5: 951 /* 952 * Disable data reporting. Only effects stream mode 953 * and is ignored right now. 954 */ 955 fifo_write_char(sc, &sc->rfifo, 0xFA); 956 sc->reporting_mode = 1; 957 break; 958 case 0xF6: 959 /* 960 * SET DEFAULTS 961 * 962 * (reset sampling rate, resolution, scaling and 963 * enter stream mode) 964 */ 965 fifo_write_char(sc, &sc->rfifo, 0xFA); 966 sc->mode.rate = 100; 967 sc->mode.resolution = 4; 968 sc->scaling_mode = 0; 969 sc->reporting_mode = 1; 970 sc->remote_mode = 0; 971 sc->delta_x = 0; 972 sc->delta_y = 0; 973 sc->delta_z = 0; 974 /* signal */ 975 break; 976 case 0xFE: 977 /* 978 * RESEND 979 * 980 * Force a resend by guaranteeing that reported_but 981 * differs from track_but. 982 */ 983 fifo_write_char(sc, &sc->rfifo, 0xFA); 984 sc->data_signal = 1; 985 break; 986 case 0xFF: 987 /* 988 * RESET 989 */ 990 fifo_reset(sc, &sc->rfifo); /* should we do this? */ 991 fifo_reset(sc, &sc->wfifo); /* should we do this? */ 992 fifo_write_char(sc, &sc->rfifo, 0xFA); 993 sc->delta_x = 0; 994 sc->delta_y = 0; 995 sc->delta_z = 0; 996 sc->zenabled = 0; 997 sc->mode.level = 0; 998 break; 999 default: 1000 printf("unknown command %02x\n", sc->ps2_cmd); 1001 break; 1002 } 1003 if (cmd_completed) { 1004 sc->ps2_cmd = 0; 1005 sc->ps2_acked = 0; 1006 } 1007 cyapa_notify(sc); 1008 } 1009 cyapa_unlock(sc); 1010 if (error == 0 && (cmd_completed || uio->uio_resid)) 1011 goto again; 1012 return (error); 1013 } 1014 1015 static void cyapafiltdetach(struct knote *); 1016 static int cyapafilt(struct knote *, long); 1017 1018 static struct filterops cyapa_filtops = { 1019 .f_isfd = 1, 1020 .f_detach = cyapafiltdetach, 1021 .f_event = cyapafilt 1022 }; 1023 1024 static int 1025 cyapakqfilter(struct cdev *dev, struct knote *kn) 1026 { 1027 struct cyapa_softc *sc; 1028 struct knlist *knlist; 1029 1030 sc = dev->si_drv1; 1031 1032 switch(kn->kn_filter) { 1033 case EVFILT_READ: 1034 kn->kn_fop = &cyapa_filtops; 1035 kn->kn_hook = (void *)sc; 1036 break; 1037 default: 1038 return (EOPNOTSUPP); 1039 } 1040 knlist = &sc->selinfo.si_note; 1041 knlist_add(knlist, kn, 0); 1042 1043 return (0); 1044 } 1045 1046 static int 1047 cyapapoll(struct cdev *dev, int events, struct thread *td) 1048 { 1049 struct cyapa_softc *sc; 1050 int revents; 1051 1052 sc = dev->si_drv1; 1053 revents = 0; 1054 1055 cyapa_lock(sc); 1056 if (events & (POLLIN | POLLRDNORM)) { 1057 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) 1058 revents = events & (POLLIN | POLLRDNORM); 1059 else { 1060 sc->isselect = 1; 1061 selrecord(td, &sc->selinfo); 1062 } 1063 } 1064 cyapa_unlock(sc); 1065 1066 return (revents); 1067 } 1068 1069 static void 1070 cyapafiltdetach(struct knote *kn) 1071 { 1072 struct cyapa_softc *sc; 1073 struct knlist *knlist; 1074 1075 sc = (struct cyapa_softc *)kn->kn_hook; 1076 1077 knlist = &sc->selinfo.si_note; 1078 knlist_remove(knlist, kn, 0); 1079 } 1080 1081 static int 1082 cyapafilt(struct knote *kn, long hint) 1083 { 1084 struct cyapa_softc *sc; 1085 int ready; 1086 1087 sc = (struct cyapa_softc *)kn->kn_hook; 1088 1089 cyapa_lock(sc); 1090 ready = fifo_ready(sc, &sc->rfifo) || sc->data_signal; 1091 cyapa_unlock(sc); 1092 1093 return (ready); 1094 } 1095 1096 static int 1097 cyapaioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 1098 { 1099 struct cyapa_softc *sc; 1100 int error; 1101 1102 sc = dev->si_drv1; 1103 error = 0; 1104 1105 cyapa_lock(sc); 1106 switch (cmd) { 1107 case MOUSE_GETHWINFO: 1108 *(mousehw_t *)data = sc->hw; 1109 if (sc->mode.level == 0) 1110 ((mousehw_t *)data)->model = MOUSE_MODEL_GENERIC; 1111 break; 1112 1113 case MOUSE_GETMODE: 1114 *(mousemode_t *)data = sc->mode; 1115 ((mousemode_t *)data)->resolution = 1116 MOUSE_RES_LOW - sc->mode.resolution; 1117 switch (sc->mode.level) { 1118 case 0: 1119 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2; 1120 ((mousemode_t *)data)->packetsize = 1121 MOUSE_PS2_PACKETSIZE; 1122 break; 1123 case 2: 1124 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2; 1125 ((mousemode_t *)data)->packetsize = 1126 MOUSE_PS2_PACKETSIZE + 1; 1127 break; 1128 } 1129 break; 1130 1131 case MOUSE_GETLEVEL: 1132 *(int *)data = sc->mode.level; 1133 break; 1134 1135 case MOUSE_SETLEVEL: 1136 if ((*(int *)data < 0) && 1137 (*(int *)data > 2)) { 1138 error = EINVAL; 1139 break; 1140 } 1141 sc->mode.level = *(int *)data ? 2 : 0; 1142 sc->zenabled = sc->mode.level ? 1 : 0; 1143 break; 1144 1145 default: 1146 error = ENOTTY; 1147 break; 1148 } 1149 cyapa_unlock(sc); 1150 1151 return (error); 1152 } 1153 1154 /* 1155 * MAJOR SUPPORT FUNCTIONS 1156 */ 1157 static void 1158 cyapa_poll_thread(void *arg) 1159 { 1160 struct cyapa_softc *sc; 1161 struct cyapa_regs regs; 1162 device_t bus; /* smbus */ 1163 int error; 1164 int freq; 1165 int isidle; 1166 int pstate; 1167 int npstate; 1168 int last_reset; 1169 1170 sc = arg; 1171 freq = cyapa_norm_freq; 1172 isidle = 0; 1173 pstate = CMD_POWER_MODE_IDLE; 1174 last_reset = ticks; 1175 1176 bus = device_get_parent(sc->dev); 1177 1178 cyapa_lock(sc); 1179 sc->poll_thread_running = 1; 1180 1181 while (!sc->detaching) { 1182 cyapa_unlock(sc); 1183 error = smbus_request_bus(bus, sc->dev, SMB_WAIT); 1184 if (error == 0) { 1185 error = smbus_trans(bus, sc->addr, CMD_DEV_STATUS, 1186 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 1187 NULL, 0, 1188 (void *)®s, sizeof(regs), NULL); 1189 if (error == 0) { 1190 isidle = cyapa_raw_input(sc, ®s, freq); 1191 } 1192 1193 /* 1194 * For some reason the device can crap-out. If it 1195 * drops back into bootstrap mode try to reinitialize 1196 * it. 1197 */ 1198 if (cyapa_reset || 1199 ((regs.stat & CYAPA_STAT_RUNNING) == 0 && 1200 (unsigned)(ticks - last_reset) > TIME_TO_RESET)) { 1201 cyapa_reset = 0; 1202 last_reset = ticks; 1203 init_device(sc->dev, NULL, sc->addr, 2); 1204 } 1205 smbus_release_bus(bus, sc->dev); 1206 } 1207 pause("cyapw", hz / freq); 1208 ++sc->poll_ticks; 1209 1210 if (sc->count == 0) { 1211 freq = cyapa_idle_freq; 1212 npstate = CMD_POWER_MODE_IDLE; 1213 } else if (isidle) { 1214 freq = cyapa_slow_freq; 1215 npstate = CMD_POWER_MODE_IDLE; 1216 } else { 1217 freq = cyapa_norm_freq; 1218 npstate = CMD_POWER_MODE_FULL; 1219 } 1220 if (pstate != npstate) { 1221 pstate = npstate; 1222 cyapa_set_power_mode(sc, pstate); 1223 if (cyapa_debug) { 1224 switch(pstate) { 1225 case CMD_POWER_MODE_OFF: 1226 printf("cyapa: power off\n"); 1227 break; 1228 case CMD_POWER_MODE_IDLE: 1229 printf("cyapa: power idle\n"); 1230 break; 1231 case CMD_POWER_MODE_FULL: 1232 printf("cyapa: power full\n"); 1233 break; 1234 } 1235 } 1236 } 1237 1238 cyapa_lock(sc); 1239 } 1240 sc->poll_thread_running = 0; 1241 cyapa_unlock(sc); 1242 kthread_exit(); 1243 } 1244 1245 static int 1246 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, int freq) 1247 { 1248 int nfingers; 1249 int afingers; /* actual fingers after culling */ 1250 int i; 1251 int j; 1252 int k; 1253 int isidle; 1254 int thumbarea_begin; 1255 int seen_thumb; 1256 int x; 1257 int y; 1258 int z; 1259 int newfinger; 1260 int lessfingers; 1261 int click_x; 1262 int click_y; 1263 uint16_t but; /* high bits used for simulated but4/but5 */ 1264 1265 thumbarea_begin = sc->cap_resy - 1266 ((sc->cap_resy * cyapa_thumbarea_percent) / 100); 1267 click_x = click_y = 0; 1268 1269 /* 1270 * If the device is not running the rest of the status 1271 * means something else, set fingers to 0. 1272 */ 1273 if ((regs->stat & CYAPA_STAT_RUNNING) == 0) { 1274 regs->fngr = 0; 1275 } 1276 1277 /* Process fingers/movement */ 1278 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr); 1279 afingers = nfingers; 1280 1281 if (cyapa_debug) { 1282 printf("stat %02x buttons %c%c%c nfngrs=%d ", 1283 regs->stat, 1284 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'), 1285 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'M' : '-'), 1286 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'R' : '-'), 1287 nfingers); 1288 } 1289 1290 seen_thumb = 0; 1291 for (i = 0; i < afingers; ) { 1292 if (cyapa_debug) { 1293 printf(" [x=%04d y=%04d p=%d i=%d]", 1294 CYAPA_TOUCH_X(regs, i), 1295 CYAPA_TOUCH_Y(regs, i), 1296 CYAPA_TOUCH_P(regs, i), 1297 regs->touch[i].id); 1298 } 1299 if ((CYAPA_TOUCH_Y(regs, i) > thumbarea_begin && seen_thumb) || 1300 CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) { 1301 --afingers; 1302 if (i < afingers) { 1303 regs->touch[i] = regs->touch[i+1]; 1304 continue; 1305 } 1306 } else { 1307 if (CYAPA_TOUCH_Y(regs, i) > thumbarea_begin) 1308 seen_thumb = 1; 1309 } 1310 ++i; 1311 } 1312 nfingers = afingers; 1313 1314 /* Tracking for local solutions */ 1315 cyapa_lock(sc); 1316 1317 /* 1318 * Track timing for finger-downs. Used to detect false-3-finger 1319 * button-down. 1320 */ 1321 switch(afingers) { 1322 case 0: 1323 break; 1324 case 1: 1325 if (sc->track_nfingers == 0) 1326 sc->finger1_ticks = sc->poll_ticks; 1327 break; 1328 case 2: 1329 if (sc->track_nfingers <= 0) 1330 sc->finger1_ticks = sc->poll_ticks; 1331 if (sc->track_nfingers <= 1) 1332 sc->finger2_ticks = sc->poll_ticks; 1333 break; 1334 case 3: 1335 default: 1336 if (sc->track_nfingers <= 0) 1337 sc->finger1_ticks = sc->poll_ticks; 1338 if (sc->track_nfingers <= 1) 1339 sc->finger2_ticks = sc->poll_ticks; 1340 if (sc->track_nfingers <= 2) 1341 sc->finger3_ticks = sc->poll_ticks; 1342 break; 1343 } 1344 newfinger = sc->track_nfingers < afingers; 1345 lessfingers = sc->track_nfingers > afingers; 1346 sc->track_nfingers = afingers; 1347 1348 /* 1349 * Lookup and track finger indexes in the touch[] array. 1350 */ 1351 if (afingers == 0) { 1352 click_x = sc->track_x; 1353 click_y = sc->track_y; 1354 sc->track_x = -1; 1355 sc->track_y = -1; 1356 sc->track_z = -1; 1357 sc->fuzz_x = 0; 1358 sc->fuzz_y = 0; 1359 sc->fuzz_z = 0; 1360 sc->touch_x = -1; 1361 sc->touch_y = -1; 1362 sc->touch_z = -1; 1363 sc->track_id = -1; 1364 sc->track_but = 0; 1365 i = 0; 1366 j = 0; 1367 k = 0; 1368 } else { 1369 /* 1370 * The id assigned on touch can move around in the array, 1371 * find it. If that finger is lifted up, assign some other 1372 * finger for mouse tracking and reset track_x and track_y 1373 * to avoid a mouse jump. 1374 * 1375 * If >= 2 fingers are down be sure not to assign i and 1376 * j to the same index. 1377 */ 1378 for (i = 0; i < nfingers; ++i) { 1379 if (sc->track_id == regs->touch[i].id) 1380 break; 1381 } 1382 if (i == nfingers) { 1383 i = 0; 1384 sc->track_x = -1; 1385 sc->track_y = -1; 1386 sc->track_z = -1; 1387 while (CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin && 1388 i < nfingers) ++i; 1389 if (i == nfingers) { 1390 i = 0; 1391 } 1392 sc->track_id = regs->touch[i].id; 1393 } 1394 else if ((sc->track_but || 1395 CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin) && 1396 newfinger && afingers == 2) { 1397 j = regs->touch[0].id == sc->track_id ? 1 : 0; 1398 if (CYAPA_TOUCH_Y(regs, j) < thumbarea_begin) { 1399 i = j; 1400 sc->track_x = -1; 1401 sc->track_y = -1; 1402 sc->track_z = -1; 1403 sc->track_id = regs->touch[i].id; 1404 } 1405 } 1406 } 1407 1408 /* Two finger scrolling - reset after timeout */ 1409 if (sc->track_z != -1 && afingers != 2 && 1410 (sc->poll_ticks - sc->track_z_ticks) > cyapa_scroll_stick_ticks) { 1411 sc->track_z = -1; 1412 sc->track_z_ticks = 0; 1413 } 1414 1415 /* Initiate two finger scrolling */ 1416 if (!(regs->fngr & CYAPA_FNGR_LEFT) && 1417 ((afingers && sc->track_z != -1) || 1418 (afingers == 2 && CYAPA_TOUCH_Y(regs, 0) < thumbarea_begin && 1419 CYAPA_TOUCH_Y(regs, 1) < thumbarea_begin))) { 1420 if (afingers == 2 && (sc->poll_ticks - sc->finger2_ticks) 1421 > cyapa_scroll_wait_ticks) { 1422 z = (CYAPA_TOUCH_Y(regs, 0) + 1423 CYAPA_TOUCH_Y(regs, 1)) >> 1; 1424 sc->delta_z += z / ZSCALE - sc->track_z; 1425 if (sc->track_z == -1) { 1426 sc->delta_z = 0; 1427 } 1428 if (sc->touch_z == -1) 1429 sc->touch_z = z; /* not used atm */ 1430 sc->track_z = z / ZSCALE; 1431 sc->track_z_ticks = sc->poll_ticks; 1432 } 1433 } else if (afingers) { 1434 /* Normal pad position reporting */ 1435 x = CYAPA_TOUCH_X(regs, i); 1436 y = CYAPA_TOUCH_Y(regs, i); 1437 click_x = x; 1438 click_y = y; 1439 if (sc->track_x != -1 && sc->track_y < thumbarea_begin && 1440 (afingers > 1 || (sc->poll_ticks - sc->finger1_ticks) 1441 >= cyapa_move_min_ticks || freq < cyapa_norm_freq)) { 1442 sc->delta_x += x - sc->track_x; 1443 sc->delta_y -= y - sc->track_y; 1444 if (sc->delta_x > sc->cap_resx) 1445 sc->delta_x = sc->cap_resx; 1446 if (sc->delta_x < -sc->cap_resx) 1447 sc->delta_x = -sc->cap_resx; 1448 if (sc->delta_y > sc->cap_resx) 1449 sc->delta_y = sc->cap_resy; 1450 if (sc->delta_y < -sc->cap_resy) 1451 sc->delta_y = -sc->cap_resy; 1452 1453 if (abs(sc->delta_y) > sc->cap_resy / 2 || 1454 abs(sc->delta_x) > sc->cap_resx / 2) { 1455 if (cyapa_debug) 1456 printf("Detected jump by %i %i\n", 1457 sc->delta_x, sc->delta_y); 1458 sc->delta_x = sc->delta_y = 0; 1459 } 1460 } 1461 if (sc->touch_x == -1) { 1462 sc->touch_x = x; 1463 sc->touch_y = y; 1464 } 1465 sc->track_x = x; 1466 sc->track_y = y; 1467 } 1468 1469 /* Select finger (L = 2/3x, M = 1/3u, R = 1/3d) */ 1470 int is_tapclick = (cyapa_enable_tapclick && lessfingers && 1471 afingers == 0 && sc->poll_ticks - sc->finger1_ticks 1472 >= cyapa_tapclick_min_ticks && 1473 sc->poll_ticks - sc->finger1_ticks < cyapa_tapclick_max_ticks); 1474 1475 if (regs->fngr & CYAPA_FNGR_LEFT || is_tapclick) { 1476 if (sc->track_but) { 1477 but = sc->track_but; 1478 } else if (afingers == 1) { 1479 if (click_x < sc->cap_resx * 2 / 3) 1480 but = CYAPA_FNGR_LEFT; 1481 else if (click_y < sc->cap_resy / 2) 1482 but = CYAPA_FNGR_MIDDLE; 1483 else 1484 but = CYAPA_FNGR_RIGHT; 1485 } else if (is_tapclick) { 1486 if (click_x < sc->cap_resx * 2 / 3 || 1487 cyapa_enable_tapclick < 2) 1488 but = CYAPA_FNGR_LEFT; 1489 else if (click_y < sc->cap_resy / 2 && 1490 cyapa_enable_tapclick > 2) 1491 but = CYAPA_FNGR_MIDDLE; 1492 else 1493 but = CYAPA_FNGR_RIGHT; 1494 } else { 1495 but = CYAPA_FNGR_LEFT; 1496 } 1497 } else { 1498 but = 0; 1499 } 1500 1501 /* 1502 * Detect state change from last reported state and 1503 * determine if we have gone idle. 1504 */ 1505 sc->track_but = but; 1506 if (sc->delta_x || sc->delta_y || sc->delta_z || 1507 sc->track_but != sc->reported_but) { 1508 sc->active_tick = ticks; 1509 if (sc->remote_mode == 0 && sc->reporting_mode) 1510 sc->data_signal = 1; 1511 isidle = 0; 1512 } else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) { 1513 sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */ 1514 isidle = 1; 1515 } else { 1516 isidle = 0; 1517 } 1518 cyapa_notify(sc); 1519 cyapa_unlock(sc); 1520 1521 if (cyapa_debug) 1522 printf("%i >> %i << %i\n", isidle, sc->track_id, sc->delta_y); 1523 return (isidle); 1524 } 1525 1526 static void 1527 cyapa_set_power_mode(struct cyapa_softc *sc, int mode) 1528 { 1529 uint8_t data; 1530 device_t bus; 1531 int error; 1532 1533 bus = device_get_parent(sc->dev); 1534 error = smbus_request_bus(bus, sc->dev, SMB_WAIT); 1535 if (error == 0) { 1536 error = smbus_trans(bus, sc->addr, CMD_POWER_MODE, 1537 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 1538 NULL, 0, (void *)&data, 1, NULL); 1539 data = (data & ~0xFC) | mode; 1540 if (error == 0) { 1541 error = smbus_trans(bus, sc->addr, CMD_POWER_MODE, 1542 SMB_TRANS_NOCNT | SMB_TRANS_7BIT, 1543 (void *)&data, 1, NULL, 0, NULL); 1544 } 1545 smbus_release_bus(bus, sc->dev); 1546 } 1547 } 1548 1549 /* 1550 * FIFO FUNCTIONS 1551 */ 1552 1553 /* 1554 * Returns non-zero if the fifo is empty 1555 */ 1556 static int 1557 fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo) 1558 { 1559 1560 CYAPA_LOCK_ASSERT(sc); 1561 1562 return (fifo->rindex == fifo->windex); 1563 } 1564 1565 /* 1566 * Returns the number of characters available for reading from 1567 * the fifo without wrapping the fifo buffer. 1568 */ 1569 static size_t 1570 fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo) 1571 { 1572 size_t n; 1573 1574 CYAPA_LOCK_ASSERT(sc); 1575 1576 n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK); 1577 if (n > (size_t)(fifo->windex - fifo->rindex)) 1578 n = (size_t)(fifo->windex - fifo->rindex); 1579 return (n); 1580 } 1581 1582 /* 1583 * Returns a read pointer into the fifo and then bumps 1584 * rindex. The FIFO must have at least 'n' characters in 1585 * it. The value (n) can cause the index to wrap but users 1586 * of the buffer should never supply a value for (n) that wraps 1587 * the buffer. 1588 */ 1589 static char * 1590 fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n) 1591 { 1592 char *ptr; 1593 1594 CYAPA_LOCK_ASSERT(sc); 1595 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) { 1596 printf("fifo_read: overflow\n"); 1597 return (fifo->buf); 1598 } 1599 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK); 1600 fifo->rindex += n; 1601 1602 return (ptr); 1603 } 1604 1605 static uint8_t 1606 fifo_read_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo) 1607 { 1608 uint8_t c; 1609 1610 CYAPA_LOCK_ASSERT(sc); 1611 1612 if (fifo->rindex == fifo->windex) { 1613 printf("fifo_read_char: overflow\n"); 1614 c = 0; 1615 } else { 1616 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK]; 1617 ++fifo->rindex; 1618 } 1619 return (c); 1620 } 1621 1622 1623 /* 1624 * Write a character to the FIFO. The character will be discarded 1625 * if the FIFO is full. 1626 */ 1627 static void 1628 fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, uint8_t c) 1629 { 1630 1631 CYAPA_LOCK_ASSERT(sc); 1632 1633 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) { 1634 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c; 1635 ++fifo->windex; 1636 } 1637 } 1638 1639 /* 1640 * Return the amount of space available for writing without wrapping 1641 * the fifo. 1642 */ 1643 static size_t 1644 fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo) 1645 { 1646 size_t n; 1647 1648 CYAPA_LOCK_ASSERT(sc); 1649 1650 n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK); 1651 if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex))) 1652 n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)); 1653 return (n); 1654 } 1655 1656 static char * 1657 fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n) 1658 { 1659 char *ptr; 1660 1661 CYAPA_LOCK_ASSERT(sc); 1662 1663 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK); 1664 fifo->windex += n; 1665 1666 return (ptr); 1667 } 1668 1669 static void 1670 fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo) 1671 { 1672 1673 CYAPA_LOCK_ASSERT(sc); 1674 1675 fifo->rindex = 0; 1676 fifo->windex = 0; 1677 } 1678 1679 /* 1680 * Fuzz handling 1681 */ 1682 static int 1683 cyapa_fuzz(int delta, int *fuzzp) 1684 { 1685 int fuzz; 1686 1687 fuzz = *fuzzp; 1688 if (fuzz >= 0 && delta < 0) { 1689 ++delta; 1690 --fuzz; 1691 } else if (fuzz <= 0 && delta > 0) { 1692 --delta; 1693 ++fuzz; 1694 } 1695 *fuzzp = fuzz; 1696 1697 return (delta); 1698 } 1699 1700 DRIVER_MODULE(cyapa, smbus, cyapa_driver, cyapa_devclass, NULL, NULL); 1701 MODULE_DEPEND(cyapa, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER); 1702 MODULE_VERSION(cyapa, 1); 1703