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/smbus/smbconf.h>
126 #include <dev/smbus/smbus.h>
127 #include <dev/cyapa/cyapa.h>
129 #include "smbus_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 */
153 struct cdev *devnode;
154 struct selinfo selinfo;
163 int detaching; /* driver is detaching */
164 int poll_thread_running; /* poll thread is running */
166 /* PS/2 mouse emulation */
167 int track_x; /* current tracking */
172 char track_id; /* first finger id */
174 int delta_x; /* accumulation -> report */
180 int touch_x; /* touch down coordinates */
186 uint16_t reported_but;
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 */
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 */
205 struct cyapa_cdevpriv {
206 struct cyapa_softc *sc;
209 #define CYPOLL_SHUTDOWN 0x0001
211 static void cyapa_poll_thread(void *arg);
212 static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs,
214 static void cyapa_set_power_mode(struct cyapa_softc *sc, int mode);
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,
220 static char *fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
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,
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);
229 static int cyapa_fuzz(int delta, int *fuzz);
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");
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");
277 cyapa_lock(struct cyapa_softc *sc)
280 mtx_lock(&sc->mutex);
284 cyapa_unlock(struct cyapa_softc *sc)
287 mtx_unlock(&sc->mutex);
290 #define CYAPA_LOCK_ASSERT(sc) mtx_assert(&(sc)->mutex, MA_OWNED);
293 * Notify if possible receive data ready. Must be called
294 * with sc->mutex held (cyapa_lock(sc)).
297 cyapa_notify(struct cyapa_softc *sc)
300 CYAPA_LOCK_ASSERT(sc);
302 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) {
303 KNOTE_LOCKED(&sc->selinfo.si_note, 0);
304 if (sc->blocked || sc->isselect) {
307 wakeup(&sc->blocked);
311 selwakeup(&sc->selinfo);
318 * Initialize the device
321 init_device(device_t dev, struct cyapa_cap *cap, int addr, int probe)
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 };
330 struct cyapa_boot_regs boot;
334 bus = device_get_parent(dev); /* smbus */
337 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
338 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
339 NULL, 0, (void *)&boot, sizeof(boot), NULL);
344 * Bootstrap the device if necessary. It can take up to 2 seconds
345 * for the device to fully initialize.
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) {
353 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
354 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
355 bl_deactivate, sizeof(bl_deactivate),
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);
367 pause("cyapab1", (hz * 2) / 10);
369 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
370 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
371 NULL, 0, (void *)&boot, sizeof(boot), NULL);
377 device_printf(dev, "Unable to bring device out of bootstrap\n");
384 error = smbus_trans(bus, addr, CMD_QUERY_CAPABILITIES,
385 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
386 NULL, 0, (void *)cap, sizeof(*cap), NULL);
388 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) {
389 device_printf(dev, "Product ID \"%5.5s\" mismatch\n",
394 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
395 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
396 NULL, 0, (void *)&boot, sizeof(boot), NULL);
398 if (probe == 0) /* official init */
399 device_printf(dev, "cyapa init status %02x\n", boot.stat);
401 device_printf(dev, "cyapa reset status %02x\n", boot.stat);
405 device_printf(dev, "Unable to initialize\n");
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*);
414 static devclass_t cyapa_devclass;
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),
425 static driver_t cyapa_driver = {
428 sizeof(struct cyapa_softc),
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;
438 static struct cdevsw cyapa_cdevsw = {
439 .d_version = D_VERSION,
441 .d_ioctl = cyapaioctl,
443 .d_write = cyapawrite,
444 .d_kqfilter = cyapakqfilter,
449 cyapa_probe(device_t dev)
451 struct cyapa_cap cap;
455 addr = smbus_get_addr(dev);
458 * 0x67 - cypress trackpad on the acer c720
459 * (other devices might use other ids).
464 error = init_device(dev, &cap, addr, 1);
468 device_set_desc(dev, "Cypress APA I2C Trackpad");
470 return (BUS_PROBE_VENDOR);
474 cyapa_attach(device_t dev)
476 struct cyapa_softc *sc;
477 struct cyapa_cap cap;
481 sc = device_get_softc(dev);
482 sc->reporting_mode = 1;
484 unit = device_get_unit(dev);
485 addr = smbus_get_addr(dev);
487 if (init_device(dev, &cap, addr, 0))
490 mtx_init(&sc->mutex, "cyapa", NULL, MTX_DEF);
495 knlist_init_mtx(&sc->selinfo.si_note, &sc->mutex);
497 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) |
499 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) |
501 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) |
503 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) |
505 sc->cap_buttons = cap.buttons;
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);
515 sc->hw.iftype = MOUSE_IF_PS2;
516 sc->hw.type = MOUSE_MOUSE;
517 sc->hw.model = MOUSE_MODEL_INTELLI;
520 sc->mode.protocol = MOUSE_PROTO_PS2;
522 sc->mode.resolution = 4;
523 sc->mode.accelfactor = 1;
525 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
527 /* Setup input event tracking */
528 cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE);
530 /* Start the polling thread */
531 kthread_add(cyapa_poll_thread, sc, NULL, NULL,
534 sc->devnode = make_dev(&cyapa_cdevsw, unit,
535 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit);
537 sc->devnode->si_drv1 = sc;
543 cyapa_detach(device_t dev)
545 struct cyapa_softc *sc;
547 sc = device_get_softc(dev);
549 /* Cleanup poller thread */
551 while (sc->poll_thread_running) {
553 mtx_sleep(&sc->detaching, &sc->mutex, PCATCH, "cyapadet", hz);
557 destroy_dev(sc->devnode);
559 knlist_clear(&sc->selinfo.si_note, 0);
560 seldrain(&sc->selinfo);
561 knlist_destroy(&sc->selinfo.si_note);
563 mtx_destroy(&sc->mutex);
569 * USER DEVICE I/O FUNCTIONS
572 cyapaopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
574 struct cyapa_cdevpriv *priv;
577 priv = malloc(sizeof(*priv), M_CYAPA, M_WAITOK | M_ZERO);
578 priv->sc = dev->si_drv1;
580 error = devfs_set_cdevpriv(priv, cyapa_cdevpriv_dtor);
582 cyapa_lock(priv->sc);
584 cyapa_unlock(priv->sc);
593 cyapa_cdevpriv_dtor(void *data)
595 struct cyapa_cdevpriv *priv;
598 KASSERT(priv != NULL, ("cyapa cdevpriv should not be NULL!"));
600 cyapa_lock(priv->sc);
602 cyapa_unlock(priv->sc);
608 cyaparead(struct cdev *dev, struct uio *uio, int ioflag)
610 struct cyapa_softc *sc;
617 /* If buffer is empty, load a new event if it is ready */
620 if (fifo_empty(sc, &sc->rfifo) &&
621 (sc->data_signal || sc->delta_x || sc->delta_y ||
622 sc->track_but != sc->reported_but)) {
629 /* Accumulate delta_x, delta_y */
631 delta_x = sc->delta_x;
632 delta_y = sc->delta_y;
633 delta_z = sc->delta_z;
638 if (delta_x < -256) {
646 if (delta_y < -256) {
654 if (delta_z < -256) {
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;
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.
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);
685 if (but & CYAPA_FNGR_LEFT)
687 if (but & CYAPA_FNGR_MIDDLE)
689 if (but & CYAPA_FNGR_RIGHT)
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) {
697 /* Z axis all 8 bits */
698 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_z);
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).
707 while (delta_z > 7 || delta_z < -8)
709 c0 = (uint8_t)delta_z & 0x0F;
710 fifo_write_char(sc, &sc->rfifo, c0);
719 /* Blocking / Non-blocking */
721 didread = (uio->uio_resid == 0);
723 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(sc, &sc->rfifo)) {
727 error = mtx_sleep(&sc->blocked, &sc->mutex, PCATCH, "cyablk", 0);
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)
737 ptr = fifo_read(sc, &sc->rfifo, 0);
739 error = uiomove(ptr, n, uio);
743 fifo_read(sc, &sc->rfifo, n);
748 if (error == 0 && didread == 0) {
751 return (didread ? 0 : error);
755 cyapawrite(struct cdev *dev, struct uio *uio, int ioflag)
757 struct cyapa_softc *sc;
767 * Copy data from userland. This will also cross-over the end
768 * of the fifo and keep filling.
771 while ((n = fifo_space(sc, &sc->wfifo)) > 0 && uio->uio_resid) {
772 if (n > uio->uio_resid)
774 ptr = fifo_write(sc, &sc->wfifo, 0);
776 error = uiomove(ptr, n, uio);
780 fifo_write(sc, &sc->wfifo, n);
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) {
790 /* SET SCALING 1:1 */
791 sc->scaling_mode = 0;
792 fifo_write_char(sc, &sc->rfifo, 0xFA);
795 /* SET SCALING 2:1 */
796 sc->scaling_mode = 1;
797 fifo_write_char(sc, &sc->rfifo, 0xFA);
800 /* SET RESOLUTION +1 byte */
801 if (sc->ps2_acked == 0) {
803 fifo_write_char(sc, &sc->rfifo, 0xFA);
805 if (fifo_ready(sc, &sc->wfifo) == 0) {
809 sc->mode.resolution = fifo_read_char(sc, &sc->wfifo);
810 fifo_write_char(sc, &sc->rfifo, 0xFA);
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)
822 * bit 2 LEFT BUTTON (1 if pressed)
823 * bit 1 MIDDLE BUTTON (1 if pressed)
824 * bit 0 RIGHT BUTTON (1 if pressed)
826 * byte2: resolution counts/mm
832 if (sc->reporting_mode)
834 if (sc->scaling_mode)
836 if (sc->track_but & CYAPA_FNGR_LEFT)
838 if (sc->track_but & CYAPA_FNGR_MIDDLE)
840 if (sc->track_but & CYAPA_FNGR_RIGHT)
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);
848 /* Set stream mode and reset movement counters */
850 fifo_write_char(sc, &sc->rfifo, 0xFA);
857 * Read Data (if in remote mode). If not in remote
858 * mode force an event.
860 fifo_write_char(sc, &sc->rfifo, 0xFA);
864 /* Reset Wrap Mode (ignored) */
865 fifo_write_char(sc, &sc->rfifo, 0xFA);
868 /* Set Wrap Mode (ignored) */
869 fifo_write_char(sc, &sc->rfifo, 0xFA);
872 /* Set Remote Mode */
874 fifo_write_char(sc, &sc->rfifo, 0xFA);
883 * If we send 0x00 - normal PS/2 mouse, no Z-axis
885 * If we send 0x03 - Intellimouse, data packet has
886 * an additional Z movement byte (8 bits signed).
887 * (also reset movement counters)
889 * If we send 0x04 - Now includes z-axis and the
890 * 4th and 5th mouse buttons.
892 fifo_write_char(sc, &sc->rfifo, 0xFA);
893 switch(sc->zenabled) {
895 fifo_write_char(sc, &sc->rfifo, 0x03);
898 fifo_write_char(sc, &sc->rfifo, 0x04);
901 fifo_write_char(sc, &sc->rfifo, 0x00);
912 * byte1: the sample rate
914 if (sc->ps2_acked == 0) {
916 fifo_write_char(sc, &sc->rfifo, 0xFA);
918 if (fifo_ready(sc, &sc->wfifo) == 0) {
922 sc->mode.rate = fifo_read_char(sc, &sc->wfifo);
923 fifo_write_char(sc, &sc->rfifo, 0xFA);
926 * zenabling sequence: 200,100,80 (device id 0x03)
927 * 200,200,80 (device id 0x04)
929 * We support id 0x03 (no 4th or 5th button).
930 * We support id 0x04 (w/ 4th and 5th button).
932 if (sc->zenabled == 0 && sc->mode.rate == 200)
934 else if (sc->zenabled == -1 && sc->mode.rate == 100)
936 else if (sc->zenabled == -1 && sc->mode.rate == 200)
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 */
946 /* Enable data reporting. Only effects stream mode. */
947 fifo_write_char(sc, &sc->rfifo, 0xFA);
948 sc->reporting_mode = 1;
952 * Disable data reporting. Only effects stream mode
953 * and is ignored right now.
955 fifo_write_char(sc, &sc->rfifo, 0xFA);
956 sc->reporting_mode = 1;
962 * (reset sampling rate, resolution, scaling and
965 fifo_write_char(sc, &sc->rfifo, 0xFA);
967 sc->mode.resolution = 4;
968 sc->scaling_mode = 0;
969 sc->reporting_mode = 1;
980 * Force a resend by guaranteeing that reported_but
981 * differs from track_but.
983 fifo_write_char(sc, &sc->rfifo, 0xFA);
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);
1000 printf("unknown command %02x\n", sc->ps2_cmd);
1003 if (cmd_completed) {
1010 if (error == 0 && (cmd_completed || uio->uio_resid))
1015 static void cyapafiltdetach(struct knote *);
1016 static int cyapafilt(struct knote *, long);
1018 static struct filterops cyapa_filtops = {
1020 .f_detach = cyapafiltdetach,
1021 .f_event = cyapafilt
1025 cyapakqfilter(struct cdev *dev, struct knote *kn)
1027 struct cyapa_softc *sc;
1028 struct knlist *knlist;
1032 switch(kn->kn_filter) {
1034 kn->kn_fop = &cyapa_filtops;
1035 kn->kn_hook = (void *)sc;
1038 return (EOPNOTSUPP);
1040 knlist = &sc->selinfo.si_note;
1041 knlist_add(knlist, kn, 0);
1047 cyapapoll(struct cdev *dev, int events, struct thread *td)
1049 struct cyapa_softc *sc;
1056 if (events & (POLLIN | POLLRDNORM)) {
1057 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo))
1058 revents = events & (POLLIN | POLLRDNORM);
1061 selrecord(td, &sc->selinfo);
1070 cyapafiltdetach(struct knote *kn)
1072 struct cyapa_softc *sc;
1073 struct knlist *knlist;
1075 sc = (struct cyapa_softc *)kn->kn_hook;
1077 knlist = &sc->selinfo.si_note;
1078 knlist_remove(knlist, kn, 0);
1082 cyapafilt(struct knote *kn, long hint)
1084 struct cyapa_softc *sc;
1087 sc = (struct cyapa_softc *)kn->kn_hook;
1090 ready = fifo_ready(sc, &sc->rfifo) || sc->data_signal;
1097 cyapaioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1099 struct cyapa_softc *sc;
1107 case MOUSE_GETHWINFO:
1108 *(mousehw_t *)data = sc->hw;
1109 if (sc->mode.level == 0)
1110 ((mousehw_t *)data)->model = MOUSE_MODEL_GENERIC;
1114 *(mousemode_t *)data = sc->mode;
1115 ((mousemode_t *)data)->resolution =
1116 MOUSE_RES_LOW - sc->mode.resolution;
1117 switch (sc->mode.level) {
1119 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1120 ((mousemode_t *)data)->packetsize =
1121 MOUSE_PS2_PACKETSIZE;
1124 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1125 ((mousemode_t *)data)->packetsize =
1126 MOUSE_PS2_PACKETSIZE + 1;
1131 case MOUSE_GETLEVEL:
1132 *(int *)data = sc->mode.level;
1135 case MOUSE_SETLEVEL:
1136 if ((*(int *)data < 0) &&
1137 (*(int *)data > 2)) {
1141 sc->mode.level = *(int *)data ? 2 : 0;
1142 sc->zenabled = sc->mode.level ? 1 : 0;
1155 * MAJOR SUPPORT FUNCTIONS
1158 cyapa_poll_thread(void *arg)
1160 struct cyapa_softc *sc;
1161 struct cyapa_regs regs;
1162 device_t bus; /* smbus */
1171 freq = cyapa_norm_freq;
1173 pstate = CMD_POWER_MODE_IDLE;
1176 bus = device_get_parent(sc->dev);
1179 sc->poll_thread_running = 1;
1181 while (!sc->detaching) {
1183 error = smbus_request_bus(bus, sc->dev, SMB_WAIT);
1185 error = smbus_trans(bus, sc->addr, CMD_DEV_STATUS,
1186 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
1188 (void *)®s, sizeof(regs), NULL);
1190 isidle = cyapa_raw_input(sc, ®s, freq);
1194 * For some reason the device can crap-out. If it
1195 * drops back into bootstrap mode try to reinitialize
1199 ((regs.stat & CYAPA_STAT_RUNNING) == 0 &&
1200 (unsigned)(ticks - last_reset) > TIME_TO_RESET)) {
1203 init_device(sc->dev, NULL, sc->addr, 2);
1205 smbus_release_bus(bus, sc->dev);
1207 pause("cyapw", hz / freq);
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;
1217 freq = cyapa_norm_freq;
1218 npstate = CMD_POWER_MODE_FULL;
1220 if (pstate != npstate) {
1222 cyapa_set_power_mode(sc, pstate);
1225 case CMD_POWER_MODE_OFF:
1226 printf("cyapa: power off\n");
1228 case CMD_POWER_MODE_IDLE:
1229 printf("cyapa: power idle\n");
1231 case CMD_POWER_MODE_FULL:
1232 printf("cyapa: power full\n");
1240 sc->poll_thread_running = 0;
1246 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, int freq)
1249 int afingers; /* actual fingers after culling */
1254 int thumbarea_begin;
1263 uint16_t but; /* high bits used for simulated but4/but5 */
1265 thumbarea_begin = sc->cap_resy -
1266 ((sc->cap_resy * cyapa_thumbarea_percent) / 100);
1267 click_x = click_y = 0;
1270 * If the device is not running the rest of the status
1271 * means something else, set fingers to 0.
1273 if ((regs->stat & CYAPA_STAT_RUNNING) == 0) {
1277 /* Process fingers/movement */
1278 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1279 afingers = nfingers;
1282 printf("stat %02x buttons %c%c%c nfngrs=%d ",
1284 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1285 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
1286 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
1291 for (i = 0; i < afingers; ) {
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),
1299 if ((CYAPA_TOUCH_Y(regs, i) > thumbarea_begin && seen_thumb) ||
1300 CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) {
1303 regs->touch[i] = regs->touch[i+1];
1307 if (CYAPA_TOUCH_Y(regs, i) > thumbarea_begin)
1312 nfingers = afingers;
1314 /* Tracking for local solutions */
1318 * Track timing for finger-downs. Used to detect false-3-finger
1325 if (sc->track_nfingers == 0)
1326 sc->finger1_ticks = sc->poll_ticks;
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;
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;
1344 newfinger = sc->track_nfingers < afingers;
1345 lessfingers = sc->track_nfingers > afingers;
1346 sc->track_nfingers = afingers;
1349 * Lookup and track finger indexes in the touch[] array.
1351 if (afingers == 0) {
1352 click_x = sc->track_x;
1353 click_y = sc->track_y;
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.
1375 * If >= 2 fingers are down be sure not to assign i and
1376 * j to the same index.
1378 for (i = 0; i < nfingers; ++i) {
1379 if (sc->track_id == regs->touch[i].id)
1382 if (i == nfingers) {
1387 while (CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin &&
1389 if (i == nfingers) {
1392 sc->track_id = regs->touch[i].id;
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) {
1403 sc->track_id = regs->touch[i].id;
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) {
1412 sc->track_z_ticks = 0;
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) {
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;
1433 } else if (afingers) {
1434 /* Normal pad position reporting */
1435 x = CYAPA_TOUCH_X(regs, i);
1436 y = CYAPA_TOUCH_Y(regs, i);
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;
1453 if (abs(sc->delta_y) > sc->cap_resy / 2 ||
1454 abs(sc->delta_x) > sc->cap_resx / 2) {
1456 printf("Detected jump by %i %i\n",
1457 sc->delta_x, sc->delta_y);
1458 sc->delta_x = sc->delta_y = 0;
1461 if (sc->touch_x == -1) {
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);
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;
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;
1493 but = CYAPA_FNGR_RIGHT;
1495 but = CYAPA_FNGR_LEFT;
1502 * Detect state change from last reported state and
1503 * determine if we have gone idle.
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;
1512 } else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) {
1513 sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */
1522 printf("%i >> %i << %i\n", isidle, sc->track_id, sc->delta_y);
1527 cyapa_set_power_mode(struct cyapa_softc *sc, int mode)
1533 bus = device_get_parent(sc->dev);
1534 error = smbus_request_bus(bus, sc->dev, SMB_WAIT);
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;
1541 error = smbus_trans(bus, sc->addr, CMD_POWER_MODE,
1542 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
1543 (void *)&data, 1, NULL, 0, NULL);
1545 smbus_release_bus(bus, sc->dev);
1554 * Returns non-zero if the fifo is empty
1557 fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1560 CYAPA_LOCK_ASSERT(sc);
1562 return (fifo->rindex == fifo->windex);
1566 * Returns the number of characters available for reading from
1567 * the fifo without wrapping the fifo buffer.
1570 fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1574 CYAPA_LOCK_ASSERT(sc);
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);
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
1590 fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1594 CYAPA_LOCK_ASSERT(sc);
1595 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1596 printf("fifo_read: overflow\n");
1599 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1606 fifo_read_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1610 CYAPA_LOCK_ASSERT(sc);
1612 if (fifo->rindex == fifo->windex) {
1613 printf("fifo_read_char: overflow\n");
1616 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1624 * Write a character to the FIFO. The character will be discarded
1625 * if the FIFO is full.
1628 fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, uint8_t c)
1631 CYAPA_LOCK_ASSERT(sc);
1633 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1634 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1640 * Return the amount of space available for writing without wrapping
1644 fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1648 CYAPA_LOCK_ASSERT(sc);
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));
1657 fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1661 CYAPA_LOCK_ASSERT(sc);
1663 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1670 fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1673 CYAPA_LOCK_ASSERT(sc);
1683 cyapa_fuzz(int delta, int *fuzzp)
1688 if (fuzz >= 0 && delta < 0) {
1691 } else if (fuzz <= 0 && delta > 0) {
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);