2 * Copyright (c) 2014 The DragonFly Project. All rights reserved.
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>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * CYAPA - Cypress APA trackpad with I2C Interface driver
42 * Based on DragonFlyBSD's cyapa driver, which referenced the linux
43 * cyapa.c driver to figure out the bootstrapping and commands.
45 * Unable to locate any datasheet for the device.
51 * +--------------------+------------+
55 * | Button +------------+
58 * +--------------------+............|
59 * | Thumb/Button Area | 15%
60 * +---------------------------------+
65 * IMPS/2 emulation - Emulates the IntelliMouse protocol.
67 * Jitter supression - Implements 2-pixel hysteresis with memory.
69 * Jump detecion - Detect jumps caused by touchpad.
71 * Two finger scrolling - Use two fingers for Z axis scrolling.
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
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
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.
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.
102 #include <sys/param.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>
123 #include <sys/vnode.h>
125 #include <dev/iicbus/iiconf.h>
126 #include <dev/iicbus/iicbus.h>
127 #include <dev/cyapa/cyapa.h>
129 #include "iicbus_if.h"
131 #include "device_if.h"
133 #define CYAPA_BUFSIZE 128 /* power of 2 */
134 #define CYAPA_BUFMASK (CYAPA_BUFSIZE - 1)
138 #define TIME_TO_IDLE (hz * 10)
139 #define TIME_TO_RESET (hz * 3)
141 static MALLOC_DEFINE(M_CYAPA, "cyapa", "CYAPA device data");
146 char buf[CYAPA_BUFSIZE];
151 int count; /* >0 if device opened */
152 struct cdev *devnode;
153 struct selinfo selinfo;
162 int detaching; /* driver is detaching */
163 int poll_thread_running; /* poll thread is running */
165 /* PS/2 mouse emulation */
166 int track_x; /* current tracking */
171 char track_id; /* first finger id */
173 int delta_x; /* accumulation -> report */
179 int touch_x; /* touch down coordinates */
185 uint16_t reported_but;
187 struct cyapa_fifo rfifo; /* device->host */
188 struct cyapa_fifo wfifo; /* host->device */
189 uint8_t ps2_cmd; /* active p2_cmd waiting for data */
195 int reporting_mode; /* 0=disabled 1=enabled */
196 int scaling_mode; /* 0=1:1 1=2:1 */
197 int remote_mode; /* 0 for streaming mode */
198 int zenabled; /* z-axis enabled (mode 1 or 2) */
199 mousehw_t hw; /* hardware information */
200 mousemode_t mode; /* mode */
204 struct cyapa_cdevpriv {
205 struct cyapa_softc *sc;
208 #define CYPOLL_SHUTDOWN 0x0001
210 static void cyapa_poll_thread(void *arg);
211 static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs,
213 static void cyapa_set_power_mode(struct cyapa_softc *sc, int mode);
215 static int fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
216 static size_t fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
217 static char *fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
219 static char *fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
221 static uint8_t fifo_read_char(struct cyapa_softc *sc,
222 struct cyapa_fifo *fifo);
223 static void fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
225 static size_t fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
226 static void fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
228 static int cyapa_fuzz(int delta, int *fuzz);
230 static int cyapa_idle_freq = 1;
231 SYSCTL_INT(_debug, OID_AUTO, cyapa_idle_freq, CTLFLAG_RW,
232 &cyapa_idle_freq, 0, "Scan frequency in idle mode");
233 static int cyapa_slow_freq = 20;
234 SYSCTL_INT(_debug, OID_AUTO, cyapa_slow_freq, CTLFLAG_RW,
235 &cyapa_slow_freq, 0, "Scan frequency in slow mode ");
236 static int cyapa_norm_freq = 100;
237 SYSCTL_INT(_debug, OID_AUTO, cyapa_norm_freq, CTLFLAG_RW,
238 &cyapa_norm_freq, 0, "Normal scan frequency");
239 static int cyapa_minpressure = 12;
240 SYSCTL_INT(_debug, OID_AUTO, cyapa_minpressure, CTLFLAG_RW,
241 &cyapa_minpressure, 0, "Minimum pressure to detect finger");
242 static int cyapa_enable_tapclick = 0;
243 SYSCTL_INT(_debug, OID_AUTO, cyapa_enable_tapclick, CTLFLAG_RW,
244 &cyapa_enable_tapclick, 0, "Enable tap to click");
245 static int cyapa_tapclick_min_ticks = 1;
246 SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_min_ticks, CTLFLAG_RW,
247 &cyapa_tapclick_min_ticks, 0, "Minimum tap duration for click");
248 static int cyapa_tapclick_max_ticks = 8;
249 SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_max_ticks, CTLFLAG_RW,
250 &cyapa_tapclick_max_ticks, 0, "Maximum tap duration for click");
251 static int cyapa_move_min_ticks = 4;
252 SYSCTL_INT(_debug, OID_AUTO, cyapa_move_min_ticks, CTLFLAG_RW,
253 &cyapa_move_min_ticks, 0,
254 "Minimum ticks before cursor position is changed");
255 static int cyapa_scroll_wait_ticks = 0;
256 SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_wait_ticks, CTLFLAG_RW,
257 &cyapa_scroll_wait_ticks, 0,
258 "Wait N ticks before starting to scroll");
259 static int cyapa_scroll_stick_ticks = 15;
260 SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_stick_ticks, CTLFLAG_RW,
261 &cyapa_scroll_stick_ticks, 0,
262 "Prevent cursor move on single finger for N ticks after scroll");
263 static int cyapa_thumbarea_percent = 15;
264 SYSCTL_INT(_debug, OID_AUTO, cyapa_thumbarea_percent, CTLFLAG_RW,
265 &cyapa_thumbarea_percent, 0,
266 "Size of bottom thumb area in percent");
268 static int cyapa_debug = 0;
269 SYSCTL_INT(_debug, OID_AUTO, cyapa_debug, CTLFLAG_RW,
270 &cyapa_debug, 0, "Enable debugging");
271 static int cyapa_reset = 0;
272 SYSCTL_INT(_debug, OID_AUTO, cyapa_reset, CTLFLAG_RW,
273 &cyapa_reset, 0, "Reset track pad");
276 cyapa_read_bytes(device_t dev, uint8_t reg, uint8_t *val, int cnt)
278 uint16_t addr = iicbus_get_addr(dev);
279 struct iic_msg msgs[] = {
280 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
281 { addr, IIC_M_RD, cnt, val },
284 return (iicbus_transfer(dev, msgs, nitems(msgs)));
288 cyapa_write_bytes(device_t dev, uint8_t reg, const uint8_t *val, int cnt)
290 uint16_t addr = iicbus_get_addr(dev);
291 struct iic_msg msgs[] = {
292 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
293 { addr, IIC_M_WR | IIC_M_NOSTART, cnt, __DECONST(uint8_t *, val) },
296 return (iicbus_transfer(dev, msgs, nitems(msgs)));
300 cyapa_lock(struct cyapa_softc *sc)
303 mtx_lock(&sc->mutex);
307 cyapa_unlock(struct cyapa_softc *sc)
310 mtx_unlock(&sc->mutex);
313 #define CYAPA_LOCK_ASSERT(sc) mtx_assert(&(sc)->mutex, MA_OWNED);
316 * Notify if possible receive data ready. Must be called
317 * with sc->mutex held (cyapa_lock(sc)).
320 cyapa_notify(struct cyapa_softc *sc)
323 CYAPA_LOCK_ASSERT(sc);
325 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) {
326 KNOTE_LOCKED(&sc->selinfo.si_note, 0);
327 if (sc->blocked || sc->isselect) {
330 wakeup(&sc->blocked);
334 selwakeup(&sc->selinfo);
341 * Initialize the device
344 init_device(device_t dev, struct cyapa_cap *cap, int probe)
346 static char bl_exit[] = {
347 0x00, 0xff, 0xa5, 0x00, 0x01,
348 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
349 static char bl_deactivate[] = {
350 0x00, 0xff, 0x3b, 0x00, 0x01,
351 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
352 struct cyapa_boot_regs boot;
357 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
358 (void *)&boot, sizeof(boot));
363 * Bootstrap the device if necessary. It can take up to 2 seconds
364 * for the device to fully initialize.
367 while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) {
368 if (boot.boot & CYAPA_BOOT_BUSY) {
369 /* Busy, wait loop. */
370 } else if (boot.error & CYAPA_ERROR_BOOTLOADER) {
372 error = cyapa_write_bytes(dev, CMD_BOOT_STATUS,
373 bl_deactivate, sizeof(bl_deactivate));
378 error = cyapa_write_bytes(dev, CMD_BOOT_STATUS,
379 bl_exit, sizeof(bl_exit));
383 pause("cyapab1", (hz * 2) / 10);
385 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
386 (void *)&boot, sizeof(boot));
392 device_printf(dev, "Unable to bring device out of bootstrap\n");
399 error = cyapa_read_bytes(dev, CMD_QUERY_CAPABILITIES,
400 (void *)cap, sizeof(*cap));
402 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) {
403 device_printf(dev, "Product ID \"%5.5s\" mismatch\n",
408 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
409 (void *)&boot, sizeof(boot));
411 if (probe == 0) /* official init */
412 device_printf(dev, "cyapa init status %02x\n", boot.stat);
414 device_printf(dev, "cyapa reset status %02x\n", boot.stat);
418 device_printf(dev, "Unable to initialize\n");
422 static int cyapa_probe(device_t);
423 static int cyapa_attach(device_t);
424 static int cyapa_detach(device_t);
425 static void cyapa_cdevpriv_dtor(void*);
427 static devclass_t cyapa_devclass;
429 static device_method_t cyapa_methods[] = {
430 /* device interface */
431 DEVMETHOD(device_probe, cyapa_probe),
432 DEVMETHOD(device_attach, cyapa_attach),
433 DEVMETHOD(device_detach, cyapa_detach),
438 static driver_t cyapa_driver = {
441 sizeof(struct cyapa_softc),
444 static d_open_t cyapaopen;
445 static d_ioctl_t cyapaioctl;
446 static d_read_t cyaparead;
447 static d_write_t cyapawrite;
448 static d_kqfilter_t cyapakqfilter;
449 static d_poll_t cyapapoll;
451 static struct cdevsw cyapa_cdevsw = {
452 .d_version = D_VERSION,
454 .d_ioctl = cyapaioctl,
456 .d_write = cyapawrite,
457 .d_kqfilter = cyapakqfilter,
462 cyapa_probe(device_t dev)
464 struct cyapa_cap cap;
468 addr = iicbus_get_addr(dev);
471 * 0x67 - cypress trackpad on the acer c720
472 * (other devices might use other ids).
477 error = init_device(dev, &cap, 1);
481 device_set_desc(dev, "Cypress APA I2C Trackpad");
483 return (BUS_PROBE_VENDOR);
487 cyapa_attach(device_t dev)
489 struct cyapa_softc *sc;
490 struct cyapa_cap cap;
494 sc = device_get_softc(dev);
495 sc->reporting_mode = 1;
497 unit = device_get_unit(dev);
498 addr = iicbus_get_addr(dev);
500 if (init_device(dev, &cap, 0))
503 mtx_init(&sc->mutex, "cyapa", NULL, MTX_DEF);
507 knlist_init_mtx(&sc->selinfo.si_note, &sc->mutex);
509 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) |
511 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) |
513 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) |
515 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) |
517 sc->cap_buttons = cap.buttons;
519 device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n",
520 cap.prod_ida, cap.prod_idb, cap.prod_idc,
521 ((cap.buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'),
522 ((cap.buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
523 ((cap.buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
524 sc->cap_resx, sc->cap_resy);
527 sc->hw.iftype = MOUSE_IF_PS2;
528 sc->hw.type = MOUSE_MOUSE;
529 sc->hw.model = MOUSE_MODEL_INTELLI;
532 sc->mode.protocol = MOUSE_PROTO_PS2;
534 sc->mode.resolution = 4;
535 sc->mode.accelfactor = 1;
537 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
539 /* Setup input event tracking */
540 cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE);
542 /* Start the polling thread */
543 kthread_add(cyapa_poll_thread, sc, NULL, NULL,
546 sc->devnode = make_dev(&cyapa_cdevsw, unit,
547 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit);
549 sc->devnode->si_drv1 = sc;
555 cyapa_detach(device_t dev)
557 struct cyapa_softc *sc;
559 sc = device_get_softc(dev);
561 /* Cleanup poller thread */
563 while (sc->poll_thread_running) {
565 mtx_sleep(&sc->detaching, &sc->mutex, PCATCH, "cyapadet", hz);
569 destroy_dev(sc->devnode);
571 knlist_clear(&sc->selinfo.si_note, 0);
572 seldrain(&sc->selinfo);
573 knlist_destroy(&sc->selinfo.si_note);
575 mtx_destroy(&sc->mutex);
581 * USER DEVICE I/O FUNCTIONS
584 cyapaopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
586 struct cyapa_cdevpriv *priv;
589 priv = malloc(sizeof(*priv), M_CYAPA, M_WAITOK | M_ZERO);
590 priv->sc = dev->si_drv1;
592 error = devfs_set_cdevpriv(priv, cyapa_cdevpriv_dtor);
594 cyapa_lock(priv->sc);
596 cyapa_unlock(priv->sc);
605 cyapa_cdevpriv_dtor(void *data)
607 struct cyapa_cdevpriv *priv;
610 KASSERT(priv != NULL, ("cyapa cdevpriv should not be NULL!"));
612 cyapa_lock(priv->sc);
614 cyapa_unlock(priv->sc);
620 cyaparead(struct cdev *dev, struct uio *uio, int ioflag)
622 struct cyapa_softc *sc;
629 /* If buffer is empty, load a new event if it is ready */
632 if (fifo_empty(sc, &sc->rfifo) &&
633 (sc->data_signal || sc->delta_x || sc->delta_y ||
634 sc->track_but != sc->reported_but)) {
641 /* Accumulate delta_x, delta_y */
643 delta_x = sc->delta_x;
644 delta_y = sc->delta_y;
645 delta_z = sc->delta_z;
650 if (delta_x < -256) {
658 if (delta_y < -256) {
666 if (delta_z < -256) {
672 /* Adjust baseline for next calculation */
673 sc->delta_x -= delta_x;
674 sc->delta_y -= delta_y;
675 sc->delta_z -= delta_z;
676 sc->reported_but = but;
679 * Fuzz reduces movement jitter by introducing some
680 * hysteresis. It operates without cumulative error so
681 * if you swish around quickly and return your finger to
682 * where it started, so to will the mouse.
684 delta_x = cyapa_fuzz(delta_x, &sc->fuzz_x);
685 delta_y = cyapa_fuzz(delta_y, &sc->fuzz_y);
686 delta_z = cyapa_fuzz(delta_z, &sc->fuzz_z);
697 if (but & CYAPA_FNGR_LEFT)
699 if (but & CYAPA_FNGR_MIDDLE)
701 if (but & CYAPA_FNGR_RIGHT)
704 fifo_write_char(sc, &sc->rfifo, c0);
705 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_x);
706 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_y);
707 switch(sc->zenabled) {
709 /* Z axis all 8 bits */
710 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_z);
714 * Z axis low 4 bits + 4th button and 5th button
715 * (high 2 bits must be left 0). Auto-scale
716 * delta_z to fit to avoid a wrong-direction
717 * overflow (don't try to retain the remainder).
719 while (delta_z > 7 || delta_z < -8)
721 c0 = (uint8_t)delta_z & 0x0F;
722 fifo_write_char(sc, &sc->rfifo, c0);
731 /* Blocking / Non-blocking */
733 didread = (uio->uio_resid == 0);
735 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(sc, &sc->rfifo)) {
739 error = mtx_sleep(&sc->blocked, &sc->mutex, PCATCH, "cyablk", 0);
744 /* Return any buffered data */
745 while (error == 0 && uio->uio_resid &&
746 (n = fifo_ready(sc, &sc->rfifo)) > 0) {
747 if (n > uio->uio_resid)
749 ptr = fifo_read(sc, &sc->rfifo, 0);
751 error = uiomove(ptr, n, uio);
755 fifo_read(sc, &sc->rfifo, n);
760 if (error == 0 && didread == 0) {
763 return (didread ? 0 : error);
767 cyapawrite(struct cdev *dev, struct uio *uio, int ioflag)
769 struct cyapa_softc *sc;
779 * Copy data from userland. This will also cross-over the end
780 * of the fifo and keep filling.
783 while ((n = fifo_space(sc, &sc->wfifo)) > 0 && uio->uio_resid) {
784 if (n > uio->uio_resid)
786 ptr = fifo_write(sc, &sc->wfifo, 0);
788 error = uiomove(ptr, n, uio);
792 fifo_write(sc, &sc->wfifo, n);
795 /* Handle commands */
796 cmd_completed = (fifo_ready(sc, &sc->wfifo) != 0);
797 while (fifo_ready(sc, &sc->wfifo) && cmd_completed && error == 0) {
798 if (sc->ps2_cmd == 0)
799 sc->ps2_cmd = fifo_read_char(sc, &sc->wfifo);
800 switch(sc->ps2_cmd) {
802 /* SET SCALING 1:1 */
803 sc->scaling_mode = 0;
804 fifo_write_char(sc, &sc->rfifo, 0xFA);
807 /* SET SCALING 2:1 */
808 sc->scaling_mode = 1;
809 fifo_write_char(sc, &sc->rfifo, 0xFA);
812 /* SET RESOLUTION +1 byte */
813 if (sc->ps2_acked == 0) {
815 fifo_write_char(sc, &sc->rfifo, 0xFA);
817 if (fifo_ready(sc, &sc->wfifo) == 0) {
821 sc->mode.resolution = fifo_read_char(sc, &sc->wfifo);
822 fifo_write_char(sc, &sc->rfifo, 0xFA);
830 * bit 6 Mode (1=remote mode, 0=stream mode)
831 * bit 5 Enable (data reporting enabled)
832 * bit 4 Scaling (0=1:1 1=2:1)
834 * bit 2 LEFT BUTTON (1 if pressed)
835 * bit 1 MIDDLE BUTTON (1 if pressed)
836 * bit 0 RIGHT BUTTON (1 if pressed)
838 * byte2: resolution counts/mm
844 if (sc->reporting_mode)
846 if (sc->scaling_mode)
848 if (sc->track_but & CYAPA_FNGR_LEFT)
850 if (sc->track_but & CYAPA_FNGR_MIDDLE)
852 if (sc->track_but & CYAPA_FNGR_RIGHT)
854 fifo_write_char(sc, &sc->rfifo, 0xFA);
855 fifo_write_char(sc, &sc->rfifo, c0);
856 fifo_write_char(sc, &sc->rfifo, 0x00);
857 fifo_write_char(sc, &sc->rfifo, 100);
860 /* Set stream mode and reset movement counters */
862 fifo_write_char(sc, &sc->rfifo, 0xFA);
869 * Read Data (if in remote mode). If not in remote
870 * mode force an event.
872 fifo_write_char(sc, &sc->rfifo, 0xFA);
876 /* Reset Wrap Mode (ignored) */
877 fifo_write_char(sc, &sc->rfifo, 0xFA);
880 /* Set Wrap Mode (ignored) */
881 fifo_write_char(sc, &sc->rfifo, 0xFA);
884 /* Set Remote Mode */
886 fifo_write_char(sc, &sc->rfifo, 0xFA);
895 * If we send 0x00 - normal PS/2 mouse, no Z-axis
897 * If we send 0x03 - Intellimouse, data packet has
898 * an additional Z movement byte (8 bits signed).
899 * (also reset movement counters)
901 * If we send 0x04 - Now includes z-axis and the
902 * 4th and 5th mouse buttons.
904 fifo_write_char(sc, &sc->rfifo, 0xFA);
905 switch(sc->zenabled) {
907 fifo_write_char(sc, &sc->rfifo, 0x03);
910 fifo_write_char(sc, &sc->rfifo, 0x04);
913 fifo_write_char(sc, &sc->rfifo, 0x00);
924 * byte1: the sample rate
926 if (sc->ps2_acked == 0) {
928 fifo_write_char(sc, &sc->rfifo, 0xFA);
930 if (fifo_ready(sc, &sc->wfifo) == 0) {
934 sc->mode.rate = fifo_read_char(sc, &sc->wfifo);
935 fifo_write_char(sc, &sc->rfifo, 0xFA);
938 * zenabling sequence: 200,100,80 (device id 0x03)
939 * 200,200,80 (device id 0x04)
941 * We support id 0x03 (no 4th or 5th button).
942 * We support id 0x04 (w/ 4th and 5th button).
944 if (sc->zenabled == 0 && sc->mode.rate == 200)
946 else if (sc->zenabled == -1 && sc->mode.rate == 100)
948 else if (sc->zenabled == -1 && sc->mode.rate == 200)
950 else if (sc->zenabled == -2 && sc->mode.rate == 80)
951 sc->zenabled = 1; /* z-axis mode */
952 else if (sc->zenabled == -3 && sc->mode.rate == 80)
953 sc->zenabled = 2; /* z-axis+but4/5 */
958 /* Enable data reporting. Only effects stream mode. */
959 fifo_write_char(sc, &sc->rfifo, 0xFA);
960 sc->reporting_mode = 1;
964 * Disable data reporting. Only effects stream mode
965 * and is ignored right now.
967 fifo_write_char(sc, &sc->rfifo, 0xFA);
968 sc->reporting_mode = 1;
974 * (reset sampling rate, resolution, scaling and
977 fifo_write_char(sc, &sc->rfifo, 0xFA);
979 sc->mode.resolution = 4;
980 sc->scaling_mode = 0;
981 sc->reporting_mode = 1;
992 * Force a resend by guaranteeing that reported_but
993 * differs from track_but.
995 fifo_write_char(sc, &sc->rfifo, 0xFA);
1002 fifo_reset(sc, &sc->rfifo); /* should we do this? */
1003 fifo_reset(sc, &sc->wfifo); /* should we do this? */
1004 fifo_write_char(sc, &sc->rfifo, 0xFA);
1012 printf("unknown command %02x\n", sc->ps2_cmd);
1015 if (cmd_completed) {
1022 if (error == 0 && (cmd_completed || uio->uio_resid))
1027 static void cyapafiltdetach(struct knote *);
1028 static int cyapafilt(struct knote *, long);
1030 static struct filterops cyapa_filtops = {
1032 .f_detach = cyapafiltdetach,
1033 .f_event = cyapafilt
1037 cyapakqfilter(struct cdev *dev, struct knote *kn)
1039 struct cyapa_softc *sc;
1040 struct knlist *knlist;
1044 switch(kn->kn_filter) {
1046 kn->kn_fop = &cyapa_filtops;
1047 kn->kn_hook = (void *)sc;
1050 return (EOPNOTSUPP);
1052 knlist = &sc->selinfo.si_note;
1053 knlist_add(knlist, kn, 0);
1059 cyapapoll(struct cdev *dev, int events, struct thread *td)
1061 struct cyapa_softc *sc;
1068 if (events & (POLLIN | POLLRDNORM)) {
1069 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo))
1070 revents = events & (POLLIN | POLLRDNORM);
1073 selrecord(td, &sc->selinfo);
1082 cyapafiltdetach(struct knote *kn)
1084 struct cyapa_softc *sc;
1085 struct knlist *knlist;
1087 sc = (struct cyapa_softc *)kn->kn_hook;
1089 knlist = &sc->selinfo.si_note;
1090 knlist_remove(knlist, kn, 0);
1094 cyapafilt(struct knote *kn, long hint)
1096 struct cyapa_softc *sc;
1099 sc = (struct cyapa_softc *)kn->kn_hook;
1102 ready = fifo_ready(sc, &sc->rfifo) || sc->data_signal;
1109 cyapaioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1111 struct cyapa_softc *sc;
1119 case MOUSE_GETHWINFO:
1120 *(mousehw_t *)data = sc->hw;
1121 if (sc->mode.level == 0)
1122 ((mousehw_t *)data)->model = MOUSE_MODEL_GENERIC;
1126 *(mousemode_t *)data = sc->mode;
1127 ((mousemode_t *)data)->resolution =
1128 MOUSE_RES_LOW - sc->mode.resolution;
1129 switch (sc->mode.level) {
1131 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1132 ((mousemode_t *)data)->packetsize =
1133 MOUSE_PS2_PACKETSIZE;
1136 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1137 ((mousemode_t *)data)->packetsize =
1138 MOUSE_PS2_PACKETSIZE + 1;
1143 case MOUSE_GETLEVEL:
1144 *(int *)data = sc->mode.level;
1147 case MOUSE_SETLEVEL:
1148 if ((*(int *)data < 0) &&
1149 (*(int *)data > 2)) {
1153 sc->mode.level = *(int *)data ? 2 : 0;
1154 sc->zenabled = sc->mode.level ? 1 : 0;
1167 * MAJOR SUPPORT FUNCTIONS
1170 cyapa_poll_thread(void *arg)
1172 struct cyapa_softc *sc;
1173 struct cyapa_regs regs;
1174 device_t bus; /* iicbus */
1183 freq = cyapa_norm_freq;
1185 pstate = CMD_POWER_MODE_IDLE;
1188 bus = device_get_parent(sc->dev);
1191 sc->poll_thread_running = 1;
1193 while (!sc->detaching) {
1195 error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1197 error = cyapa_read_bytes(sc->dev, CMD_DEV_STATUS,
1198 (void *)®s, sizeof(regs));
1200 isidle = cyapa_raw_input(sc, ®s, freq);
1204 * For some reason the device can crap-out. If it
1205 * drops back into bootstrap mode try to reinitialize
1209 ((regs.stat & CYAPA_STAT_RUNNING) == 0 &&
1210 (unsigned)(ticks - last_reset) > TIME_TO_RESET)) {
1213 init_device(sc->dev, NULL, 2);
1215 iicbus_release_bus(bus, sc->dev);
1217 pause("cyapw", hz / freq);
1220 if (sc->count == 0) {
1221 freq = cyapa_idle_freq;
1222 npstate = CMD_POWER_MODE_IDLE;
1223 } else if (isidle) {
1224 freq = cyapa_slow_freq;
1225 npstate = CMD_POWER_MODE_IDLE;
1227 freq = cyapa_norm_freq;
1228 npstate = CMD_POWER_MODE_FULL;
1230 if (pstate != npstate) {
1232 cyapa_set_power_mode(sc, pstate);
1235 case CMD_POWER_MODE_OFF:
1236 printf("cyapa: power off\n");
1238 case CMD_POWER_MODE_IDLE:
1239 printf("cyapa: power idle\n");
1241 case CMD_POWER_MODE_FULL:
1242 printf("cyapa: power full\n");
1250 sc->poll_thread_running = 0;
1256 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, int freq)
1259 int afingers; /* actual fingers after culling */
1264 int thumbarea_begin;
1273 uint16_t but; /* high bits used for simulated but4/but5 */
1275 thumbarea_begin = sc->cap_resy -
1276 ((sc->cap_resy * cyapa_thumbarea_percent) / 100);
1277 click_x = click_y = 0;
1280 * If the device is not running the rest of the status
1281 * means something else, set fingers to 0.
1283 if ((regs->stat & CYAPA_STAT_RUNNING) == 0) {
1287 /* Process fingers/movement */
1288 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1289 afingers = nfingers;
1292 printf("stat %02x buttons %c%c%c nfngrs=%d ",
1294 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1295 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
1296 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
1301 for (i = 0; i < afingers; ) {
1303 printf(" [x=%04d y=%04d p=%d i=%d]",
1304 CYAPA_TOUCH_X(regs, i),
1305 CYAPA_TOUCH_Y(regs, i),
1306 CYAPA_TOUCH_P(regs, i),
1309 if ((CYAPA_TOUCH_Y(regs, i) > thumbarea_begin && seen_thumb) ||
1310 CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) {
1313 regs->touch[i] = regs->touch[i+1];
1317 if (CYAPA_TOUCH_Y(regs, i) > thumbarea_begin)
1322 nfingers = afingers;
1324 /* Tracking for local solutions */
1328 * Track timing for finger-downs. Used to detect false-3-finger
1335 if (sc->track_nfingers == 0)
1336 sc->finger1_ticks = sc->poll_ticks;
1339 if (sc->track_nfingers <= 0)
1340 sc->finger1_ticks = sc->poll_ticks;
1341 if (sc->track_nfingers <= 1)
1342 sc->finger2_ticks = sc->poll_ticks;
1346 if (sc->track_nfingers <= 0)
1347 sc->finger1_ticks = sc->poll_ticks;
1348 if (sc->track_nfingers <= 1)
1349 sc->finger2_ticks = sc->poll_ticks;
1350 if (sc->track_nfingers <= 2)
1351 sc->finger3_ticks = sc->poll_ticks;
1354 newfinger = sc->track_nfingers < afingers;
1355 lessfingers = sc->track_nfingers > afingers;
1356 sc->track_nfingers = afingers;
1359 * Lookup and track finger indexes in the touch[] array.
1361 if (afingers == 0) {
1362 click_x = sc->track_x;
1363 click_y = sc->track_y;
1380 * The id assigned on touch can move around in the array,
1381 * find it. If that finger is lifted up, assign some other
1382 * finger for mouse tracking and reset track_x and track_y
1383 * to avoid a mouse jump.
1385 * If >= 2 fingers are down be sure not to assign i and
1386 * j to the same index.
1388 for (i = 0; i < nfingers; ++i) {
1389 if (sc->track_id == regs->touch[i].id)
1392 if (i == nfingers) {
1397 while (CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin &&
1399 if (i == nfingers) {
1402 sc->track_id = regs->touch[i].id;
1404 else if ((sc->track_but ||
1405 CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin) &&
1406 newfinger && afingers == 2) {
1407 j = regs->touch[0].id == sc->track_id ? 1 : 0;
1408 if (CYAPA_TOUCH_Y(regs, j) < thumbarea_begin) {
1413 sc->track_id = regs->touch[i].id;
1418 /* Two finger scrolling - reset after timeout */
1419 if (sc->track_z != -1 && afingers != 2 &&
1420 (sc->poll_ticks - sc->track_z_ticks) > cyapa_scroll_stick_ticks) {
1422 sc->track_z_ticks = 0;
1425 /* Initiate two finger scrolling */
1426 if (!(regs->fngr & CYAPA_FNGR_LEFT) &&
1427 ((afingers && sc->track_z != -1) ||
1428 (afingers == 2 && CYAPA_TOUCH_Y(regs, 0) < thumbarea_begin &&
1429 CYAPA_TOUCH_Y(regs, 1) < thumbarea_begin))) {
1430 if (afingers == 2 && (sc->poll_ticks - sc->finger2_ticks)
1431 > cyapa_scroll_wait_ticks) {
1432 z = (CYAPA_TOUCH_Y(regs, 0) +
1433 CYAPA_TOUCH_Y(regs, 1)) >> 1;
1434 sc->delta_z += z / ZSCALE - sc->track_z;
1435 if (sc->track_z == -1) {
1438 if (sc->touch_z == -1)
1439 sc->touch_z = z; /* not used atm */
1440 sc->track_z = z / ZSCALE;
1441 sc->track_z_ticks = sc->poll_ticks;
1443 } else if (afingers) {
1444 /* Normal pad position reporting */
1445 x = CYAPA_TOUCH_X(regs, i);
1446 y = CYAPA_TOUCH_Y(regs, i);
1449 if (sc->track_x != -1 && sc->track_y < thumbarea_begin &&
1450 (afingers > 1 || (sc->poll_ticks - sc->finger1_ticks)
1451 >= cyapa_move_min_ticks || freq < cyapa_norm_freq)) {
1452 sc->delta_x += x - sc->track_x;
1453 sc->delta_y -= y - sc->track_y;
1454 if (sc->delta_x > sc->cap_resx)
1455 sc->delta_x = sc->cap_resx;
1456 if (sc->delta_x < -sc->cap_resx)
1457 sc->delta_x = -sc->cap_resx;
1458 if (sc->delta_y > sc->cap_resx)
1459 sc->delta_y = sc->cap_resy;
1460 if (sc->delta_y < -sc->cap_resy)
1461 sc->delta_y = -sc->cap_resy;
1463 if (abs(sc->delta_y) > sc->cap_resy / 2 ||
1464 abs(sc->delta_x) > sc->cap_resx / 2) {
1466 printf("Detected jump by %i %i\n",
1467 sc->delta_x, sc->delta_y);
1468 sc->delta_x = sc->delta_y = 0;
1471 if (sc->touch_x == -1) {
1479 /* Select finger (L = 2/3x, M = 1/3u, R = 1/3d) */
1480 int is_tapclick = (cyapa_enable_tapclick && lessfingers &&
1481 afingers == 0 && sc->poll_ticks - sc->finger1_ticks
1482 >= cyapa_tapclick_min_ticks &&
1483 sc->poll_ticks - sc->finger1_ticks < cyapa_tapclick_max_ticks);
1485 if (regs->fngr & CYAPA_FNGR_LEFT || is_tapclick) {
1486 if (sc->track_but) {
1487 but = sc->track_but;
1488 } else if (afingers == 1) {
1489 if (click_x < sc->cap_resx * 2 / 3)
1490 but = CYAPA_FNGR_LEFT;
1491 else if (click_y < sc->cap_resy / 2)
1492 but = CYAPA_FNGR_MIDDLE;
1494 but = CYAPA_FNGR_RIGHT;
1495 } else if (is_tapclick) {
1496 if (click_x < sc->cap_resx * 2 / 3 ||
1497 cyapa_enable_tapclick < 2)
1498 but = CYAPA_FNGR_LEFT;
1499 else if (click_y < sc->cap_resy / 2 &&
1500 cyapa_enable_tapclick > 2)
1501 but = CYAPA_FNGR_MIDDLE;
1503 but = CYAPA_FNGR_RIGHT;
1505 but = CYAPA_FNGR_LEFT;
1512 * Detect state change from last reported state and
1513 * determine if we have gone idle.
1515 sc->track_but = but;
1516 if (sc->delta_x || sc->delta_y || sc->delta_z ||
1517 sc->track_but != sc->reported_but) {
1518 sc->active_tick = ticks;
1519 if (sc->remote_mode == 0 && sc->reporting_mode)
1520 sc->data_signal = 1;
1522 } else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) {
1523 sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */
1532 printf("%i >> %i << %i\n", isidle, sc->track_id, sc->delta_y);
1537 cyapa_set_power_mode(struct cyapa_softc *sc, int mode)
1543 bus = device_get_parent(sc->dev);
1544 error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1546 error = cyapa_read_bytes(sc->dev, CMD_POWER_MODE,
1548 data = (data & ~0xFC) | mode;
1550 error = cyapa_write_bytes(sc->dev, CMD_POWER_MODE,
1553 iicbus_release_bus(bus, sc->dev);
1562 * Returns non-zero if the fifo is empty
1565 fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1568 CYAPA_LOCK_ASSERT(sc);
1570 return (fifo->rindex == fifo->windex);
1574 * Returns the number of characters available for reading from
1575 * the fifo without wrapping the fifo buffer.
1578 fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1582 CYAPA_LOCK_ASSERT(sc);
1584 n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK);
1585 if (n > (size_t)(fifo->windex - fifo->rindex))
1586 n = (size_t)(fifo->windex - fifo->rindex);
1591 * Returns a read pointer into the fifo and then bumps
1592 * rindex. The FIFO must have at least 'n' characters in
1593 * it. The value (n) can cause the index to wrap but users
1594 * of the buffer should never supply a value for (n) that wraps
1598 fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1602 CYAPA_LOCK_ASSERT(sc);
1603 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1604 printf("fifo_read: overflow\n");
1607 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1614 fifo_read_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1618 CYAPA_LOCK_ASSERT(sc);
1620 if (fifo->rindex == fifo->windex) {
1621 printf("fifo_read_char: overflow\n");
1624 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1632 * Write a character to the FIFO. The character will be discarded
1633 * if the FIFO is full.
1636 fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, uint8_t c)
1639 CYAPA_LOCK_ASSERT(sc);
1641 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1642 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1648 * Return the amount of space available for writing without wrapping
1652 fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1656 CYAPA_LOCK_ASSERT(sc);
1658 n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK);
1659 if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)))
1660 n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex));
1665 fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1669 CYAPA_LOCK_ASSERT(sc);
1671 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1678 fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1681 CYAPA_LOCK_ASSERT(sc);
1691 cyapa_fuzz(int delta, int *fuzzp)
1696 if (fuzz >= 0 && delta < 0) {
1699 } else if (fuzz <= 0 && delta > 0) {
1708 DRIVER_MODULE(cyapa, iicbus, cyapa_driver, cyapa_devclass, NULL, NULL);
1709 MODULE_DEPEND(cyapa, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1710 MODULE_VERSION(cyapa, 1);