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;
155 struct intr_config_hook intr_hook;
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_read_bytes(device_t dev, uint8_t reg, uint8_t *val, int cnt)
279 uint16_t addr = iicbus_get_addr(dev);
280 struct iic_msg msgs[] = {
281 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
282 { addr, IIC_M_RD, cnt, val },
285 return (iicbus_transfer(dev, msgs, nitems(msgs)));
289 cyapa_write_bytes(device_t dev, uint8_t reg, const uint8_t *val, int cnt)
291 uint16_t addr = iicbus_get_addr(dev);
292 struct iic_msg msgs[] = {
293 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
294 { addr, IIC_M_WR | IIC_M_NOSTART, cnt, __DECONST(uint8_t *, val) },
297 return (iicbus_transfer(dev, msgs, nitems(msgs)));
301 cyapa_lock(struct cyapa_softc *sc)
304 mtx_lock(&sc->mutex);
308 cyapa_unlock(struct cyapa_softc *sc)
311 mtx_unlock(&sc->mutex);
314 #define CYAPA_LOCK_ASSERT(sc) mtx_assert(&(sc)->mutex, MA_OWNED);
317 * Notify if possible receive data ready. Must be called
318 * with sc->mutex held (cyapa_lock(sc)).
321 cyapa_notify(struct cyapa_softc *sc)
324 CYAPA_LOCK_ASSERT(sc);
326 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) {
327 KNOTE_LOCKED(&sc->selinfo.si_note, 0);
328 if (sc->blocked || sc->isselect) {
331 wakeup(&sc->blocked);
335 selwakeup(&sc->selinfo);
342 * Initialize the device
345 init_device(device_t dev, struct cyapa_cap *cap, int probe)
347 static char bl_exit[] = {
348 0x00, 0xff, 0xa5, 0x00, 0x01,
349 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
350 static char bl_deactivate[] = {
351 0x00, 0xff, 0x3b, 0x00, 0x01,
352 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
353 struct cyapa_boot_regs boot;
358 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
359 (void *)&boot, sizeof(boot));
364 * Bootstrap the device if necessary. It can take up to 2 seconds
365 * for the device to fully initialize.
368 while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) {
369 if (boot.boot & CYAPA_BOOT_BUSY) {
370 /* Busy, wait loop. */
371 } else if (boot.error & CYAPA_ERROR_BOOTLOADER) {
373 error = cyapa_write_bytes(dev, CMD_BOOT_STATUS,
374 bl_deactivate, sizeof(bl_deactivate));
379 error = cyapa_write_bytes(dev, CMD_BOOT_STATUS,
380 bl_exit, sizeof(bl_exit));
384 pause("cyapab1", (hz * 2) / 10);
386 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
387 (void *)&boot, sizeof(boot));
393 device_printf(dev, "Unable to bring device out of bootstrap\n");
400 error = cyapa_read_bytes(dev, CMD_QUERY_CAPABILITIES,
401 (void *)cap, sizeof(*cap));
403 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) {
404 device_printf(dev, "Product ID \"%5.5s\" mismatch\n",
409 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
410 (void *)&boot, sizeof(boot));
412 if (probe == 0) /* official init */
413 device_printf(dev, "cyapa init status %02x\n", boot.stat);
415 device_printf(dev, "cyapa reset status %02x\n", boot.stat);
419 device_printf(dev, "Unable to initialize\n");
424 * Start the polling thread
427 cyapa_start(void *xdev)
429 struct cyapa_softc *sc;
432 sc = device_get_softc(dev);
434 config_intrhook_disestablish(&sc->intr_hook);
436 /* Setup input event tracking */
437 cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE);
439 /* Start the polling thread */
440 kthread_add(cyapa_poll_thread, sc, NULL, NULL,
444 static int cyapa_probe(device_t);
445 static int cyapa_attach(device_t);
446 static int cyapa_detach(device_t);
447 static void cyapa_cdevpriv_dtor(void*);
449 static devclass_t cyapa_devclass;
451 static device_method_t cyapa_methods[] = {
452 /* device interface */
453 DEVMETHOD(device_probe, cyapa_probe),
454 DEVMETHOD(device_attach, cyapa_attach),
455 DEVMETHOD(device_detach, cyapa_detach),
460 static driver_t cyapa_driver = {
463 sizeof(struct cyapa_softc),
466 static d_open_t cyapaopen;
467 static d_ioctl_t cyapaioctl;
468 static d_read_t cyaparead;
469 static d_write_t cyapawrite;
470 static d_kqfilter_t cyapakqfilter;
471 static d_poll_t cyapapoll;
473 static struct cdevsw cyapa_cdevsw = {
474 .d_version = D_VERSION,
476 .d_ioctl = cyapaioctl,
478 .d_write = cyapawrite,
479 .d_kqfilter = cyapakqfilter,
484 cyapa_probe(device_t dev)
486 struct cyapa_cap cap;
490 addr = iicbus_get_addr(dev);
493 * 0x67 - cypress trackpad on the acer c720
494 * (other devices might use other ids).
499 error = init_device(dev, &cap, 1);
503 device_set_desc(dev, "Cypress APA I2C Trackpad");
505 return (BUS_PROBE_VENDOR);
509 cyapa_attach(device_t dev)
511 struct cyapa_softc *sc;
512 struct cyapa_cap cap;
516 sc = device_get_softc(dev);
517 sc->reporting_mode = 1;
519 unit = device_get_unit(dev);
520 addr = iicbus_get_addr(dev);
522 if (init_device(dev, &cap, 0))
525 mtx_init(&sc->mutex, "cyapa", NULL, MTX_DEF);
529 knlist_init_mtx(&sc->selinfo.si_note, &sc->mutex);
531 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) |
533 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) |
535 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) |
537 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) |
539 sc->cap_buttons = cap.buttons;
541 device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n",
542 cap.prod_ida, cap.prod_idb, cap.prod_idc,
543 ((cap.buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'),
544 ((cap.buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
545 ((cap.buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
546 sc->cap_resx, sc->cap_resy);
549 sc->hw.iftype = MOUSE_IF_PS2;
550 sc->hw.type = MOUSE_MOUSE;
551 sc->hw.model = MOUSE_MODEL_INTELLI;
554 sc->mode.protocol = MOUSE_PROTO_PS2;
556 sc->mode.resolution = 4;
557 sc->mode.accelfactor = 1;
559 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
561 sc->intr_hook.ich_func = cyapa_start;
562 sc->intr_hook.ich_arg = sc->dev;
564 /* Postpone start of the polling thread until sleep is available */
565 if (config_intrhook_establish(&sc->intr_hook) != 0) {
566 mtx_destroy(&sc->mutex);
570 sc->devnode = make_dev(&cyapa_cdevsw, unit,
571 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit);
573 sc->devnode->si_drv1 = sc;
579 cyapa_detach(device_t dev)
581 struct cyapa_softc *sc;
583 sc = device_get_softc(dev);
585 /* Cleanup poller thread */
587 while (sc->poll_thread_running) {
589 mtx_sleep(&sc->detaching, &sc->mutex, PCATCH, "cyapadet", hz);
593 destroy_dev(sc->devnode);
595 knlist_clear(&sc->selinfo.si_note, 0);
596 seldrain(&sc->selinfo);
597 knlist_destroy(&sc->selinfo.si_note);
599 mtx_destroy(&sc->mutex);
605 * USER DEVICE I/O FUNCTIONS
608 cyapaopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
610 struct cyapa_cdevpriv *priv;
613 priv = malloc(sizeof(*priv), M_CYAPA, M_WAITOK | M_ZERO);
614 priv->sc = dev->si_drv1;
616 error = devfs_set_cdevpriv(priv, cyapa_cdevpriv_dtor);
618 cyapa_lock(priv->sc);
620 cyapa_unlock(priv->sc);
629 cyapa_cdevpriv_dtor(void *data)
631 struct cyapa_cdevpriv *priv;
634 KASSERT(priv != NULL, ("cyapa cdevpriv should not be NULL!"));
636 cyapa_lock(priv->sc);
638 cyapa_unlock(priv->sc);
644 cyaparead(struct cdev *dev, struct uio *uio, int ioflag)
646 struct cyapa_softc *sc;
653 /* If buffer is empty, load a new event if it is ready */
656 if (fifo_empty(sc, &sc->rfifo) &&
657 (sc->data_signal || sc->delta_x || sc->delta_y ||
658 sc->track_but != sc->reported_but)) {
665 /* Accumulate delta_x, delta_y */
667 delta_x = sc->delta_x;
668 delta_y = sc->delta_y;
669 delta_z = sc->delta_z;
674 if (delta_x < -256) {
682 if (delta_y < -256) {
690 if (delta_z < -256) {
696 /* Adjust baseline for next calculation */
697 sc->delta_x -= delta_x;
698 sc->delta_y -= delta_y;
699 sc->delta_z -= delta_z;
700 sc->reported_but = but;
703 * Fuzz reduces movement jitter by introducing some
704 * hysteresis. It operates without cumulative error so
705 * if you swish around quickly and return your finger to
706 * where it started, so to will the mouse.
708 delta_x = cyapa_fuzz(delta_x, &sc->fuzz_x);
709 delta_y = cyapa_fuzz(delta_y, &sc->fuzz_y);
710 delta_z = cyapa_fuzz(delta_z, &sc->fuzz_z);
721 if (but & CYAPA_FNGR_LEFT)
723 if (but & CYAPA_FNGR_MIDDLE)
725 if (but & CYAPA_FNGR_RIGHT)
728 fifo_write_char(sc, &sc->rfifo, c0);
729 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_x);
730 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_y);
731 switch(sc->zenabled) {
733 /* Z axis all 8 bits */
734 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_z);
738 * Z axis low 4 bits + 4th button and 5th button
739 * (high 2 bits must be left 0). Auto-scale
740 * delta_z to fit to avoid a wrong-direction
741 * overflow (don't try to retain the remainder).
743 while (delta_z > 7 || delta_z < -8)
745 c0 = (uint8_t)delta_z & 0x0F;
746 fifo_write_char(sc, &sc->rfifo, c0);
755 /* Blocking / Non-blocking */
757 didread = (uio->uio_resid == 0);
759 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(sc, &sc->rfifo)) {
763 error = mtx_sleep(&sc->blocked, &sc->mutex, PCATCH, "cyablk", 0);
768 /* Return any buffered data */
769 while (error == 0 && uio->uio_resid &&
770 (n = fifo_ready(sc, &sc->rfifo)) > 0) {
771 if (n > uio->uio_resid)
773 ptr = fifo_read(sc, &sc->rfifo, 0);
775 error = uiomove(ptr, n, uio);
779 fifo_read(sc, &sc->rfifo, n);
784 if (error == 0 && didread == 0) {
787 return (didread ? 0 : error);
791 cyapawrite(struct cdev *dev, struct uio *uio, int ioflag)
793 struct cyapa_softc *sc;
803 * Copy data from userland. This will also cross-over the end
804 * of the fifo and keep filling.
807 while ((n = fifo_space(sc, &sc->wfifo)) > 0 && uio->uio_resid) {
808 if (n > uio->uio_resid)
810 ptr = fifo_write(sc, &sc->wfifo, 0);
812 error = uiomove(ptr, n, uio);
816 fifo_write(sc, &sc->wfifo, n);
819 /* Handle commands */
820 cmd_completed = (fifo_ready(sc, &sc->wfifo) != 0);
821 while (fifo_ready(sc, &sc->wfifo) && cmd_completed && error == 0) {
822 if (sc->ps2_cmd == 0)
823 sc->ps2_cmd = fifo_read_char(sc, &sc->wfifo);
824 switch(sc->ps2_cmd) {
826 /* SET SCALING 1:1 */
827 sc->scaling_mode = 0;
828 fifo_write_char(sc, &sc->rfifo, 0xFA);
831 /* SET SCALING 2:1 */
832 sc->scaling_mode = 1;
833 fifo_write_char(sc, &sc->rfifo, 0xFA);
836 /* SET RESOLUTION +1 byte */
837 if (sc->ps2_acked == 0) {
839 fifo_write_char(sc, &sc->rfifo, 0xFA);
841 if (fifo_ready(sc, &sc->wfifo) == 0) {
845 sc->mode.resolution = fifo_read_char(sc, &sc->wfifo);
846 fifo_write_char(sc, &sc->rfifo, 0xFA);
854 * bit 6 Mode (1=remote mode, 0=stream mode)
855 * bit 5 Enable (data reporting enabled)
856 * bit 4 Scaling (0=1:1 1=2:1)
858 * bit 2 LEFT BUTTON (1 if pressed)
859 * bit 1 MIDDLE BUTTON (1 if pressed)
860 * bit 0 RIGHT BUTTON (1 if pressed)
862 * byte2: resolution counts/mm
868 if (sc->reporting_mode)
870 if (sc->scaling_mode)
872 if (sc->track_but & CYAPA_FNGR_LEFT)
874 if (sc->track_but & CYAPA_FNGR_MIDDLE)
876 if (sc->track_but & CYAPA_FNGR_RIGHT)
878 fifo_write_char(sc, &sc->rfifo, 0xFA);
879 fifo_write_char(sc, &sc->rfifo, c0);
880 fifo_write_char(sc, &sc->rfifo, 0x00);
881 fifo_write_char(sc, &sc->rfifo, 100);
884 /* Set stream mode and reset movement counters */
886 fifo_write_char(sc, &sc->rfifo, 0xFA);
893 * Read Data (if in remote mode). If not in remote
894 * mode force an event.
896 fifo_write_char(sc, &sc->rfifo, 0xFA);
900 /* Reset Wrap Mode (ignored) */
901 fifo_write_char(sc, &sc->rfifo, 0xFA);
904 /* Set Wrap Mode (ignored) */
905 fifo_write_char(sc, &sc->rfifo, 0xFA);
908 /* Set Remote Mode */
910 fifo_write_char(sc, &sc->rfifo, 0xFA);
919 * If we send 0x00 - normal PS/2 mouse, no Z-axis
921 * If we send 0x03 - Intellimouse, data packet has
922 * an additional Z movement byte (8 bits signed).
923 * (also reset movement counters)
925 * If we send 0x04 - Now includes z-axis and the
926 * 4th and 5th mouse buttons.
928 fifo_write_char(sc, &sc->rfifo, 0xFA);
929 switch(sc->zenabled) {
931 fifo_write_char(sc, &sc->rfifo, 0x03);
934 fifo_write_char(sc, &sc->rfifo, 0x04);
937 fifo_write_char(sc, &sc->rfifo, 0x00);
948 * byte1: the sample rate
950 if (sc->ps2_acked == 0) {
952 fifo_write_char(sc, &sc->rfifo, 0xFA);
954 if (fifo_ready(sc, &sc->wfifo) == 0) {
958 sc->mode.rate = fifo_read_char(sc, &sc->wfifo);
959 fifo_write_char(sc, &sc->rfifo, 0xFA);
962 * zenabling sequence: 200,100,80 (device id 0x03)
963 * 200,200,80 (device id 0x04)
965 * We support id 0x03 (no 4th or 5th button).
966 * We support id 0x04 (w/ 4th and 5th button).
968 if (sc->zenabled == 0 && sc->mode.rate == 200)
970 else if (sc->zenabled == -1 && sc->mode.rate == 100)
972 else if (sc->zenabled == -1 && sc->mode.rate == 200)
974 else if (sc->zenabled == -2 && sc->mode.rate == 80)
975 sc->zenabled = 1; /* z-axis mode */
976 else if (sc->zenabled == -3 && sc->mode.rate == 80)
977 sc->zenabled = 2; /* z-axis+but4/5 */
982 /* Enable data reporting. Only effects stream mode. */
983 fifo_write_char(sc, &sc->rfifo, 0xFA);
984 sc->reporting_mode = 1;
988 * Disable data reporting. Only effects stream mode
989 * and is ignored right now.
991 fifo_write_char(sc, &sc->rfifo, 0xFA);
992 sc->reporting_mode = 1;
998 * (reset sampling rate, resolution, scaling and
1001 fifo_write_char(sc, &sc->rfifo, 0xFA);
1002 sc->mode.rate = 100;
1003 sc->mode.resolution = 4;
1004 sc->scaling_mode = 0;
1005 sc->reporting_mode = 1;
1006 sc->remote_mode = 0;
1016 * Force a resend by guaranteeing that reported_but
1017 * differs from track_but.
1019 fifo_write_char(sc, &sc->rfifo, 0xFA);
1020 sc->data_signal = 1;
1026 fifo_reset(sc, &sc->rfifo); /* should we do this? */
1027 fifo_reset(sc, &sc->wfifo); /* should we do this? */
1028 fifo_write_char(sc, &sc->rfifo, 0xFA);
1036 printf("unknown command %02x\n", sc->ps2_cmd);
1039 if (cmd_completed) {
1046 if (error == 0 && (cmd_completed || uio->uio_resid))
1051 static void cyapafiltdetach(struct knote *);
1052 static int cyapafilt(struct knote *, long);
1054 static struct filterops cyapa_filtops = {
1056 .f_detach = cyapafiltdetach,
1057 .f_event = cyapafilt
1061 cyapakqfilter(struct cdev *dev, struct knote *kn)
1063 struct cyapa_softc *sc;
1064 struct knlist *knlist;
1068 switch(kn->kn_filter) {
1070 kn->kn_fop = &cyapa_filtops;
1071 kn->kn_hook = (void *)sc;
1074 return (EOPNOTSUPP);
1076 knlist = &sc->selinfo.si_note;
1077 knlist_add(knlist, kn, 0);
1083 cyapapoll(struct cdev *dev, int events, struct thread *td)
1085 struct cyapa_softc *sc;
1092 if (events & (POLLIN | POLLRDNORM)) {
1093 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo))
1094 revents = events & (POLLIN | POLLRDNORM);
1097 selrecord(td, &sc->selinfo);
1106 cyapafiltdetach(struct knote *kn)
1108 struct cyapa_softc *sc;
1109 struct knlist *knlist;
1111 sc = (struct cyapa_softc *)kn->kn_hook;
1113 knlist = &sc->selinfo.si_note;
1114 knlist_remove(knlist, kn, 0);
1118 cyapafilt(struct knote *kn, long hint)
1120 struct cyapa_softc *sc;
1123 sc = (struct cyapa_softc *)kn->kn_hook;
1126 ready = fifo_ready(sc, &sc->rfifo) || sc->data_signal;
1133 cyapaioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1135 struct cyapa_softc *sc;
1143 case MOUSE_GETHWINFO:
1144 *(mousehw_t *)data = sc->hw;
1145 if (sc->mode.level == 0)
1146 ((mousehw_t *)data)->model = MOUSE_MODEL_GENERIC;
1150 *(mousemode_t *)data = sc->mode;
1151 ((mousemode_t *)data)->resolution =
1152 MOUSE_RES_LOW - sc->mode.resolution;
1153 switch (sc->mode.level) {
1155 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1156 ((mousemode_t *)data)->packetsize =
1157 MOUSE_PS2_PACKETSIZE;
1160 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1161 ((mousemode_t *)data)->packetsize =
1162 MOUSE_PS2_PACKETSIZE + 1;
1167 case MOUSE_GETLEVEL:
1168 *(int *)data = sc->mode.level;
1171 case MOUSE_SETLEVEL:
1172 if ((*(int *)data < 0) &&
1173 (*(int *)data > 2)) {
1177 sc->mode.level = *(int *)data ? 2 : 0;
1178 sc->zenabled = sc->mode.level ? 1 : 0;
1191 * MAJOR SUPPORT FUNCTIONS
1194 cyapa_poll_thread(void *arg)
1196 struct cyapa_softc *sc;
1197 struct cyapa_regs regs;
1198 device_t bus; /* iicbus */
1207 freq = cyapa_norm_freq;
1209 pstate = CMD_POWER_MODE_IDLE;
1212 bus = device_get_parent(sc->dev);
1215 sc->poll_thread_running = 1;
1217 while (!sc->detaching) {
1219 error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1221 error = cyapa_read_bytes(sc->dev, CMD_DEV_STATUS,
1222 (void *)®s, sizeof(regs));
1224 isidle = cyapa_raw_input(sc, ®s, freq);
1228 * For some reason the device can crap-out. If it
1229 * drops back into bootstrap mode try to reinitialize
1233 ((regs.stat & CYAPA_STAT_RUNNING) == 0 &&
1234 (unsigned)(ticks - last_reset) > TIME_TO_RESET)) {
1237 init_device(sc->dev, NULL, 2);
1239 iicbus_release_bus(bus, sc->dev);
1241 pause("cyapw", hz / freq);
1244 if (sc->count == 0) {
1245 freq = cyapa_idle_freq;
1246 npstate = CMD_POWER_MODE_IDLE;
1247 } else if (isidle) {
1248 freq = cyapa_slow_freq;
1249 npstate = CMD_POWER_MODE_IDLE;
1251 freq = cyapa_norm_freq;
1252 npstate = CMD_POWER_MODE_FULL;
1254 if (pstate != npstate) {
1256 cyapa_set_power_mode(sc, pstate);
1259 case CMD_POWER_MODE_OFF:
1260 printf("cyapa: power off\n");
1262 case CMD_POWER_MODE_IDLE:
1263 printf("cyapa: power idle\n");
1265 case CMD_POWER_MODE_FULL:
1266 printf("cyapa: power full\n");
1274 sc->poll_thread_running = 0;
1280 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, int freq)
1283 int afingers; /* actual fingers after culling */
1288 int thumbarea_begin;
1297 uint16_t but; /* high bits used for simulated but4/but5 */
1299 thumbarea_begin = sc->cap_resy -
1300 ((sc->cap_resy * cyapa_thumbarea_percent) / 100);
1301 click_x = click_y = 0;
1304 * If the device is not running the rest of the status
1305 * means something else, set fingers to 0.
1307 if ((regs->stat & CYAPA_STAT_RUNNING) == 0) {
1311 /* Process fingers/movement */
1312 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1313 afingers = nfingers;
1316 printf("stat %02x buttons %c%c%c nfngrs=%d ",
1318 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1319 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
1320 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
1325 for (i = 0; i < afingers; ) {
1327 printf(" [x=%04d y=%04d p=%d i=%d]",
1328 CYAPA_TOUCH_X(regs, i),
1329 CYAPA_TOUCH_Y(regs, i),
1330 CYAPA_TOUCH_P(regs, i),
1333 if ((CYAPA_TOUCH_Y(regs, i) > thumbarea_begin && seen_thumb) ||
1334 CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) {
1337 regs->touch[i] = regs->touch[i+1];
1341 if (CYAPA_TOUCH_Y(regs, i) > thumbarea_begin)
1346 nfingers = afingers;
1348 /* Tracking for local solutions */
1352 * Track timing for finger-downs. Used to detect false-3-finger
1359 if (sc->track_nfingers == 0)
1360 sc->finger1_ticks = sc->poll_ticks;
1363 if (sc->track_nfingers <= 0)
1364 sc->finger1_ticks = sc->poll_ticks;
1365 if (sc->track_nfingers <= 1)
1366 sc->finger2_ticks = sc->poll_ticks;
1370 if (sc->track_nfingers <= 0)
1371 sc->finger1_ticks = sc->poll_ticks;
1372 if (sc->track_nfingers <= 1)
1373 sc->finger2_ticks = sc->poll_ticks;
1374 if (sc->track_nfingers <= 2)
1375 sc->finger3_ticks = sc->poll_ticks;
1378 newfinger = sc->track_nfingers < afingers;
1379 lessfingers = sc->track_nfingers > afingers;
1380 sc->track_nfingers = afingers;
1383 * Lookup and track finger indexes in the touch[] array.
1385 if (afingers == 0) {
1386 click_x = sc->track_x;
1387 click_y = sc->track_y;
1404 * The id assigned on touch can move around in the array,
1405 * find it. If that finger is lifted up, assign some other
1406 * finger for mouse tracking and reset track_x and track_y
1407 * to avoid a mouse jump.
1409 * If >= 2 fingers are down be sure not to assign i and
1410 * j to the same index.
1412 for (i = 0; i < nfingers; ++i) {
1413 if (sc->track_id == regs->touch[i].id)
1416 if (i == nfingers) {
1421 while (CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin &&
1423 if (i == nfingers) {
1426 sc->track_id = regs->touch[i].id;
1428 else if ((sc->track_but ||
1429 CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin) &&
1430 newfinger && afingers == 2) {
1431 j = regs->touch[0].id == sc->track_id ? 1 : 0;
1432 if (CYAPA_TOUCH_Y(regs, j) < thumbarea_begin) {
1437 sc->track_id = regs->touch[i].id;
1442 /* Two finger scrolling - reset after timeout */
1443 if (sc->track_z != -1 && afingers != 2 &&
1444 (sc->poll_ticks - sc->track_z_ticks) > cyapa_scroll_stick_ticks) {
1446 sc->track_z_ticks = 0;
1449 /* Initiate two finger scrolling */
1450 if (!(regs->fngr & CYAPA_FNGR_LEFT) &&
1451 ((afingers && sc->track_z != -1) ||
1452 (afingers == 2 && CYAPA_TOUCH_Y(regs, 0) < thumbarea_begin &&
1453 CYAPA_TOUCH_Y(regs, 1) < thumbarea_begin))) {
1454 if (afingers == 2 && (sc->poll_ticks - sc->finger2_ticks)
1455 > cyapa_scroll_wait_ticks) {
1456 z = (CYAPA_TOUCH_Y(regs, 0) +
1457 CYAPA_TOUCH_Y(regs, 1)) >> 1;
1458 sc->delta_z += z / ZSCALE - sc->track_z;
1459 if (sc->track_z == -1) {
1462 if (sc->touch_z == -1)
1463 sc->touch_z = z; /* not used atm */
1464 sc->track_z = z / ZSCALE;
1465 sc->track_z_ticks = sc->poll_ticks;
1467 } else if (afingers) {
1468 /* Normal pad position reporting */
1469 x = CYAPA_TOUCH_X(regs, i);
1470 y = CYAPA_TOUCH_Y(regs, i);
1473 if (sc->track_x != -1 && sc->track_y < thumbarea_begin &&
1474 (afingers > 1 || (sc->poll_ticks - sc->finger1_ticks)
1475 >= cyapa_move_min_ticks || freq < cyapa_norm_freq)) {
1476 sc->delta_x += x - sc->track_x;
1477 sc->delta_y -= y - sc->track_y;
1478 if (sc->delta_x > sc->cap_resx)
1479 sc->delta_x = sc->cap_resx;
1480 if (sc->delta_x < -sc->cap_resx)
1481 sc->delta_x = -sc->cap_resx;
1482 if (sc->delta_y > sc->cap_resy)
1483 sc->delta_y = sc->cap_resy;
1484 if (sc->delta_y < -sc->cap_resy)
1485 sc->delta_y = -sc->cap_resy;
1487 if (abs(sc->delta_y) > sc->cap_resy / 2 ||
1488 abs(sc->delta_x) > sc->cap_resx / 2) {
1490 printf("Detected jump by %i %i\n",
1491 sc->delta_x, sc->delta_y);
1492 sc->delta_x = sc->delta_y = 0;
1495 if (sc->touch_x == -1) {
1503 /* Select finger (L = 2/3x, M = 1/3u, R = 1/3d) */
1504 int is_tapclick = (cyapa_enable_tapclick && lessfingers &&
1505 afingers == 0 && sc->poll_ticks - sc->finger1_ticks
1506 >= cyapa_tapclick_min_ticks &&
1507 sc->poll_ticks - sc->finger1_ticks < cyapa_tapclick_max_ticks);
1509 if (regs->fngr & CYAPA_FNGR_LEFT || is_tapclick) {
1510 if (sc->track_but) {
1511 but = sc->track_but;
1512 } else if (afingers == 1) {
1513 if (click_x < sc->cap_resx * 2 / 3)
1514 but = CYAPA_FNGR_LEFT;
1515 else if (click_y < sc->cap_resy / 2)
1516 but = CYAPA_FNGR_MIDDLE;
1518 but = CYAPA_FNGR_RIGHT;
1519 } else if (is_tapclick) {
1520 if (click_x < sc->cap_resx * 2 / 3 ||
1521 cyapa_enable_tapclick < 2)
1522 but = CYAPA_FNGR_LEFT;
1523 else if (click_y < sc->cap_resy / 2 &&
1524 cyapa_enable_tapclick > 2)
1525 but = CYAPA_FNGR_MIDDLE;
1527 but = CYAPA_FNGR_RIGHT;
1529 but = CYAPA_FNGR_LEFT;
1536 * Detect state change from last reported state and
1537 * determine if we have gone idle.
1539 sc->track_but = but;
1540 if (sc->delta_x || sc->delta_y || sc->delta_z ||
1541 sc->track_but != sc->reported_but) {
1542 sc->active_tick = ticks;
1543 if (sc->remote_mode == 0 && sc->reporting_mode)
1544 sc->data_signal = 1;
1546 } else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) {
1547 sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */
1556 printf("%i >> %i << %i\n", isidle, sc->track_id, sc->delta_y);
1561 cyapa_set_power_mode(struct cyapa_softc *sc, int mode)
1567 bus = device_get_parent(sc->dev);
1568 error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1570 error = cyapa_read_bytes(sc->dev, CMD_POWER_MODE,
1572 data = (data & ~0xFC) | mode;
1574 error = cyapa_write_bytes(sc->dev, CMD_POWER_MODE,
1577 iicbus_release_bus(bus, sc->dev);
1586 * Returns non-zero if the fifo is empty
1589 fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1592 CYAPA_LOCK_ASSERT(sc);
1594 return (fifo->rindex == fifo->windex);
1598 * Returns the number of characters available for reading from
1599 * the fifo without wrapping the fifo buffer.
1602 fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1606 CYAPA_LOCK_ASSERT(sc);
1608 n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK);
1609 if (n > (size_t)(fifo->windex - fifo->rindex))
1610 n = (size_t)(fifo->windex - fifo->rindex);
1615 * Returns a read pointer into the fifo and then bumps
1616 * rindex. The FIFO must have at least 'n' characters in
1617 * it. The value (n) can cause the index to wrap but users
1618 * of the buffer should never supply a value for (n) that wraps
1622 fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1626 CYAPA_LOCK_ASSERT(sc);
1627 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1628 printf("fifo_read: overflow\n");
1631 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1638 fifo_read_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1642 CYAPA_LOCK_ASSERT(sc);
1644 if (fifo->rindex == fifo->windex) {
1645 printf("fifo_read_char: overflow\n");
1648 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1656 * Write a character to the FIFO. The character will be discarded
1657 * if the FIFO is full.
1660 fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, uint8_t c)
1663 CYAPA_LOCK_ASSERT(sc);
1665 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1666 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1672 * Return the amount of space available for writing without wrapping
1676 fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1680 CYAPA_LOCK_ASSERT(sc);
1682 n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK);
1683 if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)))
1684 n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex));
1689 fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1693 CYAPA_LOCK_ASSERT(sc);
1695 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1702 fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1705 CYAPA_LOCK_ASSERT(sc);
1715 cyapa_fuzz(int delta, int *fuzzp)
1720 if (fuzz >= 0 && delta < 0) {
1723 } else if (fuzz <= 0 && delta > 0) {
1732 DRIVER_MODULE(cyapa, iicbus, cyapa_driver, cyapa_devclass, NULL, NULL);
1733 MODULE_DEPEND(cyapa, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1734 MODULE_VERSION(cyapa, 1);