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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * CYAPA - Cypress APA trackpad with I2C Interface driver
37 * Written from scratch but referenced the linux cyapa.c driver to
38 * figure out the bootstrapping and commands.
40 * Unable to locate any datasheet for the device.
42 * Attaches under smbus but uses an I2C protocol (no count field).
43 * This driver should override the "smb" device for the specific unit
44 * we validate against (smb0-67).
48 * Section "InputDevice"
51 * Option "Protocol" "imps/2" (slider)
52 * # Option "Protocol" "ps/2" (basic mouse)
53 * # Option "Protocol" "explorerps/2" (not working well yet)
55 * Option "Device" "/dev/cyapa0-67"
58 * NOTE: In explorerps/2 mode the slider has only 4 bits of delta resolution
59 * and may not work as smoothly. Buttons are recognized as button
64 * Jitter supression - Implements 2-pixel hysteresis with memory.
66 * False-finger supression- Two-fingers-down does not emulate anything,
69 * False-emulated button handling-
70 * Buttons are emulated when three fingers are
71 * placed on the pad. If you place all three
72 * fingers down simultaniously, this condition
73 * is detected and will not emulate any button.
75 * Slider jesture - Tap right hand side and slide up or down.
77 * (Three finger jestures)
78 * left button jesture - Two fingers down on the left, tap/hold right
79 * middle button jesture- Two fingers down left & right, tap/hold middle
80 * right button jesture - Two fingers down on the right, tap/hold left
82 * track-pad button - Tap/push physical button, left, middle, or right
83 * side of the trackpad will issue a LEFT, MIDDLE, or
86 * track-pad button - Any tap/slide of more than 32 pixels and pushing
87 * harder to articulate the trackpad physical button
88 * always issues a LEFT button event.
90 * first-finger tracking- The X/Y coordinates always track the first finger
91 * down. If you have multiple fingers down and lift
92 * up the designated first finger, a new designated
93 * first finger will be selected without causing the
94 * mouse to jump (delta's are reset).
98 * These trackpads get confused when three or more fingers are down on the
99 * same horizontal axis and will start to glitch the finger detection.
100 * Removing your hand for a few seconds will allow the trackpad to
101 * recalibrate. Generally speaking, when using three or more fingers
102 * please try to place at least one finger off-axis (a little above or
103 * below) the other two.
105 * button-4/button-5 'claw' (4 and 5-finger) sequences have similar
108 #include <sys/kernel.h>
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/device.h>
112 #include <sys/module.h>
114 #include <sys/conf.h>
116 #include <sys/fcntl.h>
117 #include <sys/vnode.h>
118 #include <sys/sysctl.h>
119 #include <sys/event.h>
120 #include <sys/devfs.h>
122 #include <bus/smbus/smbconf.h>
123 #include <bus/smbus/smbus.h>
126 #include "smbus_if.h"
128 #define CYAPA_BUFSIZE 128 /* power of 2 */
129 #define CYAPA_BUFMASK (CYAPA_BUFSIZE - 1)
133 #define TIME_TO_IDLE (hz * 10)
134 #define TIME_TO_RESET (hz * 3)
139 char buf[CYAPA_BUFSIZE];
144 int count; /* >0 if device opened */
148 struct kqinfo kqinfo;
160 struct inputev iev; /* subr_input.c */
164 * PS/2 mouse emulation
166 short track_x; /* current tracking */
170 char track_id1; /* first finger id */
171 char track_id2; /* second finger id */
173 short delta_x; /* accumulation -> report */
179 short 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 */
194 int reporting_mode; /* 0=disabled 1=enabled */
195 int scaling_mode; /* 0=1:1 1=2:1 */
196 int remote_mode; /* 0 for streaming mode */
197 int resolution; /* count/mm */
198 int sample_rate; /* samples/sec */
199 int zenabled; /* z-axis enabled (mode 1 or 2) */
203 #define CYPOLL_SHUTDOWN 0x0001
205 #define SIMULATE_BUT4 0x0100
206 #define SIMULATE_BUT5 0x0200
207 #define SIMULATE_LOCK 0x8000
209 static void cyapa_poll_thread(void *arg);
210 static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs);
211 static void cyapa_set_power_mode(struct cyapa_softc *sc, int mode);
213 static int fifo_empty(struct cyapa_fifo *fifo);
214 static size_t fifo_ready(struct cyapa_fifo *fifo);
216 static size_t fifo_total_ready(struct cyapa_fifo *fifo);
218 static char *fifo_read(struct cyapa_fifo *fifo, size_t n);
219 static char *fifo_write(struct cyapa_fifo *fifo, size_t n);
220 static uint8_t fifo_read_char(struct cyapa_fifo *fifo);
221 static void fifo_write_char(struct cyapa_fifo *fifo, uint8_t c);
222 static size_t fifo_space(struct cyapa_fifo *fifo);
223 static void fifo_reset(struct cyapa_fifo *fifo);
225 static short cyapa_fuzz(short delta, short *fuzz);
227 static int cyapa_idle_freq = 1;
228 SYSCTL_INT(_debug, OID_AUTO, cyapa_idle_freq, CTLFLAG_RW,
229 &cyapa_idle_freq, 0, "");
230 static int cyapa_slow_freq = 20;
231 SYSCTL_INT(_debug, OID_AUTO, cyapa_slow_freq, CTLFLAG_RW,
232 &cyapa_slow_freq, 0, "");
233 static int cyapa_norm_freq = 100;
234 SYSCTL_INT(_debug, OID_AUTO, cyapa_norm_freq, CTLFLAG_RW,
235 &cyapa_norm_freq, 0, "");
236 static int cyapa_minpressure = 16;
237 SYSCTL_INT(_debug, OID_AUTO, cyapa_minpressure, CTLFLAG_RW,
238 &cyapa_minpressure, 0, "");
240 static int cyapa_debug = 0;
241 SYSCTL_INT(_debug, OID_AUTO, cyapa_debug, CTLFLAG_RW,
242 &cyapa_debug, 0, "");
243 static int cyapa_reset = 0;
244 SYSCTL_INT(_debug, OID_AUTO, cyapa_reset, CTLFLAG_RW,
245 &cyapa_reset, 0, "");
249 cyapa_lock(struct cyapa_softc *sc)
251 lockmgr(&sc->lk, LK_EXCLUSIVE);
256 cyapa_unlock(struct cyapa_softc *sc)
258 lockmgr(&sc->lk, LK_RELEASE);
262 * Notify if possible receive data ready. Must be called
263 * without the lock held to avoid deadlocking in kqueue.
267 cyapa_notify(struct cyapa_softc *sc)
269 if (sc->data_signal || !fifo_empty(&sc->rfifo)) {
270 KNOTE(&sc->kqinfo.ki_note, 0);
274 wakeup(&sc->blocked);
281 * Initialize the device
285 init_device(device_t dev, struct cyapa_cap *cap, int addr, int probe)
287 static char bl_exit[] = {
288 0x00, 0xff, 0xa5, 0x00, 0x01,
289 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
290 static char bl_deactivate[] = {
291 0x00, 0xff, 0x3b, 0x00, 0x01,
292 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
294 struct cyapa_boot_regs boot;
298 bus = device_get_parent(dev); /* smbus */
303 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
304 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
305 NULL, 0, (void *)&boot, sizeof(boot), NULL);
310 * Bootstrap the device if necessary. It can take up to 2 seconds
311 * for the device to fully initialize.
314 while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) {
315 if (boot.boot & CYAPA_BOOT_BUSY) {
319 } else if (boot.error & CYAPA_ERROR_BOOTLOADER) {
323 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
324 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
326 sizeof(bl_deactivate),
334 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
335 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
342 tsleep(&error, 0, "cyapab1", hz / 10);
344 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
345 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
346 NULL, 0, (void *)&boot, sizeof(boot), NULL);
352 device_printf(dev, "Unable to bring device out of bootstrap\n");
361 error = smbus_trans(bus, addr, CMD_QUERY_CAPABILITIES,
362 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
363 NULL, 0, (void *)cap, sizeof(*cap), NULL);
365 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) {
366 device_printf(dev, "Product ID \"%5.5s\" mismatch\n",
371 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
372 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
373 NULL, 0, (void *)&boot, sizeof(boot), NULL);
375 if (probe == 0) /* official init */
376 device_printf(dev, "cyapa init status %02x\n", boot.stat);
378 device_printf(dev, "cyapa reset status %02x\n", boot.stat);
382 device_printf(dev, "Unable to initialize\n");
387 * Device infrastructure
389 #define CYAPA_SOFTC(unit) \
390 ((struct cyapa_softc *)devclass_get_softc(cyapa_devclass, (unit)))
392 static void cyapa_identify(driver_t *driver, device_t parent);
393 static int cyapa_probe(device_t);
394 static int cyapa_attach(device_t);
395 static int cyapa_detach(device_t);
397 static devclass_t cyapa_devclass;
399 static device_method_t cyapa_methods[] = {
400 /* device interface */
401 DEVMETHOD(device_identify, cyapa_identify),
402 DEVMETHOD(device_probe, cyapa_probe),
403 DEVMETHOD(device_attach, cyapa_attach),
404 DEVMETHOD(device_detach, cyapa_detach),
407 /* smbus interface */
408 DEVMETHOD(smbus_intr, smbus_generic_intr),
414 static driver_t cyapa_driver = {
417 sizeof(struct cyapa_softc),
420 static d_open_t cyapaopen;
421 static d_close_t cyapaclose;
422 static d_ioctl_t cyapaioctl;
423 static d_read_t cyaparead;
424 static d_write_t cyapawrite;
425 static d_kqfilter_t cyapakqfilter;
427 static struct dev_ops cyapa_ops = {
430 .d_close = cyapaclose,
431 .d_ioctl = cyapaioctl,
433 .d_write = cyapawrite,
434 .d_kqfilter = cyapakqfilter,
438 cyapa_identify(driver_t *driver, device_t parent)
440 if (device_find_child(parent, "cyapa", -1) == NULL)
441 BUS_ADD_CHILD(parent, parent, 0, "cyapa", -1);
445 cyapa_probe(device_t dev)
447 struct cyapa_cap cap;
453 unit = device_get_unit(dev);
456 * Only match against specific addresses to avoid blowing up
457 * other I2C devices (?). At least for now.
459 * 0x400 (from smbus) - means specific device address probe,
460 * rather than generic.
462 * 0x67 - cypress trackpad on the acer c720.
464 if ((unit & 0x04FF) != (0x0400 | 0x067))
466 tsleep(&dummy, 0, "cyastab", hz);
468 error = init_device(dev, &cap, addr, 1);
472 device_set_desc(dev, "Cypress APA I2C Trackpad");
474 return (BUS_PROBE_VENDOR);
478 cyapa_attach(device_t dev)
480 struct cyapa_softc *sc = (struct cyapa_softc *)device_get_softc(dev);
481 struct cyapa_cap cap;
488 bzero(sc, sizeof(struct cyapa_softc *));
490 lockinit(&sc->lk, "cyapa", 0, 0);
491 sc->reporting_mode = 1;
493 unit = device_get_unit(dev);
494 if ((unit & 0x04FF) != (0x0400 | 0x067))
497 if (init_device(dev, &cap, addr, 0))
505 sc->devnode = make_dev(&cyapa_ops, unit,
506 UID_ROOT, GID_WHEEL, 0600,
507 "cyapa%d-%02x", unit >> 11, unit & 1023);
509 sc->devnode = make_dev(&cyapa_ops, unit,
510 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit);
513 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) |
515 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) |
517 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) |
519 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) |
521 sc->cap_buttons = cap.buttons;
523 device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n",
524 cap.prod_ida, cap.prod_idb, cap.prod_idc,
525 ((cap.buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'),
526 ((cap.buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
527 ((cap.buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
532 * Setup input event tracking
534 cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE);
537 * Start the polling thread.
539 lwkt_create(cyapa_poll_thread, sc,
540 &sc->poll_td, NULL, 0, -1, "cyapa-poll");
546 cyapa_detach(device_t dev)
548 struct cyapa_softc *sc = (struct cyapa_softc *)device_get_softc(dev);
552 * Cleanup input event tracking
554 inputev_deregister(&sc->iev);
558 * Cleanup our poller thread
560 atomic_set_int(&sc->poll_flags, CYPOLL_SHUTDOWN);
561 while (sc->poll_td) {
562 wakeup(&sc->poll_flags);
563 tsleep(&sc->poll_td, 0, "cyapadet", hz);
567 dev_ops_remove_minor(&cyapa_ops, device_get_unit(dev));
569 devfs_assume_knotes(sc->devnode, &sc->kqinfo);
575 * USER DEVICE I/O FUNCTIONS
578 cyapaopen (struct dev_open_args *ap)
580 cdev_t dev = ap->a_head.a_dev;
581 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
595 cyapaclose(struct dev_close_args *ap)
597 cdev_t dev = ap->a_head.a_dev;
598 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
604 /* This is not supposed to happen. */
613 cyaparead(struct dev_read_args *ap)
615 cdev_t dev = ap->a_head.a_dev;
616 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
618 struct uio *uio = ap->a_uio;
619 int ioflag = ap->a_ioflag;
624 * If buffer is empty, load a new event if it is ready
628 if (fifo_empty(&sc->rfifo) &&
629 (sc->data_signal || sc->delta_x || sc->delta_y ||
630 sc->track_but != sc->reported_but)) {
638 * Accumulate delta_x, delta_y.
641 delta_x = sc->delta_x;
642 delta_y = sc->delta_y;
643 delta_z = sc->delta_z;
648 if (delta_x < -256) {
656 if (delta_y < -256) {
664 if (delta_z < -256) {
671 * 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->rfifo, c0);
705 fifo_write_char(&sc->rfifo, (uint8_t)delta_x);
706 fifo_write_char(&sc->rfifo, (uint8_t)delta_y);
707 switch(sc->zenabled) {
712 fifo_write_char(&sc->rfifo, (uint8_t)delta_z);
716 * Z axis low 4 bits + 4th button and 5th button
717 * (high 2 bits must be left 0). Auto-scale
718 * delta_z to fit to avoid a wrong-direction
719 * overflow (don't try to retain the remainder).
721 while (delta_z > 7 || delta_z < -8)
723 c0 = (uint8_t)delta_z & 0x0F;
724 if (but & SIMULATE_BUT4)
726 if (but & SIMULATE_BUT5)
728 fifo_write_char(&sc->rfifo, c0);
740 * Blocking / Non-blocking
743 didread = (uio->uio_resid == 0);
745 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(&sc->rfifo)) {
749 error = lksleep(&sc->blocked, &sc->lk, PCATCH, "cyablk", 0);
755 * Return any buffered data
757 while (error == 0 && uio->uio_resid &&
758 (n = fifo_ready(&sc->rfifo)) > 0) {
759 if (n > uio->uio_resid)
763 uint8_t *ptr = fifo_read(&sc->rfifo, 0);
766 for (v = 0; v < n; ++v)
767 kprintf(" %02x", ptr[v]);
771 error = uiomove(fifo_read(&sc->rfifo, 0), n, uio);
774 fifo_read(&sc->rfifo, n);
779 if (error == 0 && didread == 0)
786 cyapawrite(struct dev_write_args *ap)
788 cdev_t dev = ap->a_head.a_dev;
789 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
790 struct uio *uio = ap->a_uio;
798 * Copy data from userland. This will also cross-over the end
799 * of the fifo and keep filling.
802 while ((n = fifo_space(&sc->wfifo)) > 0 && uio->uio_resid) {
803 if (n > uio->uio_resid)
805 error = uiomove(fifo_write(&sc->wfifo, 0), n, uio);
808 fifo_write(&sc->wfifo, n);
814 cmd_completed = (fifo_ready(&sc->wfifo) != 0);
815 while (fifo_ready(&sc->wfifo) && cmd_completed && error == 0) {
816 if (sc->ps2_cmd == 0)
817 sc->ps2_cmd = fifo_read_char(&sc->wfifo);
818 switch(sc->ps2_cmd) {
823 sc->scaling_mode = 0;
824 fifo_write_char(&sc->rfifo, 0xFA);
830 sc->scaling_mode = 1;
831 fifo_write_char(&sc->rfifo, 0xFA);
835 * SET RESOLUTION +1 byte
837 if (sc->ps2_acked == 0) {
839 fifo_write_char(&sc->rfifo, 0xFA);
841 if (fifo_ready(&sc->wfifo) == 0) {
845 sc->resolution = fifo_read_char(&sc->wfifo);
846 fifo_write_char(&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->rfifo, 0xFA);
879 fifo_write_char(&sc->rfifo, c0);
880 fifo_write_char(&sc->rfifo, 0x00);
881 fifo_write_char(&sc->rfifo, 100);
885 * Set stream mode and reset movement counters
888 fifo_write_char(&sc->rfifo, 0xFA);
895 * Read Data (if in remote mode). If not in remote
896 * mode force an event.
898 fifo_write_char(&sc->rfifo, 0xFA);
903 * Reset Wrap Mode (ignored)
905 fifo_write_char(&sc->rfifo, 0xFA);
909 * Set Wrap Mode (ignored)
911 fifo_write_char(&sc->rfifo, 0xFA);
918 fifo_write_char(&sc->rfifo, 0xFA);
927 * If we send 0x00 - normal PS/2 mouse, no Z-axis
929 * If we send 0x03 - Intellimouse, data packet has
930 * an additional Z movement byte (8 bits signed).
931 * (also reset movement counters)
933 * If we send 0x04 - Now includes z-axis and the
934 * 4th and 5th mouse buttons.
936 fifo_write_char(&sc->rfifo, 0xFA);
937 switch(sc->zenabled) {
939 fifo_write_char(&sc->rfifo, 0x03);
942 fifo_write_char(&sc->rfifo, 0x04);
945 fifo_write_char(&sc->rfifo, 0x00);
956 * byte1: the sample rate
958 if (sc->ps2_acked == 0) {
960 fifo_write_char(&sc->rfifo, 0xFA);
962 if (fifo_ready(&sc->wfifo) == 0) {
966 sc->sample_rate = fifo_read_char(&sc->wfifo);
967 fifo_write_char(&sc->rfifo, 0xFA);
970 * zenabling sequence: 200,100,80 (device id 0x03)
971 * 200,200,80 (device id 0x04)
973 * We support id 0x03 (no 4th or 5th button).
974 * We support id 0x04 (w/ 4th and 5th button).
976 if (sc->zenabled == 0 && sc->sample_rate == 200)
978 else if (sc->zenabled == -1 && sc->sample_rate == 100)
980 else if (sc->zenabled == -1 && sc->sample_rate == 200)
982 else if (sc->zenabled == -2 && sc->sample_rate == 80)
983 sc->zenabled = 1; /* z-axis mode */
984 else if (sc->zenabled == -3 && sc->sample_rate == 80)
985 sc->zenabled = 2; /* z-axis+but4/5 */
989 * Enable data reporting. Only effects stream mode.
991 fifo_write_char(&sc->rfifo, 0xFA);
992 sc->reporting_mode = 1;
996 * Disable data reporting. Only effects stream mode.
998 fifo_write_char(&sc->rfifo, 0xFA);
999 sc->reporting_mode = 1;
1005 * (reset sampling rate, resolution, scaling and
1006 * enter stream mode)
1008 fifo_write_char(&sc->rfifo, 0xFA);
1009 sc->sample_rate = 100;
1011 sc->scaling_mode = 0;
1012 sc->reporting_mode = 0;
1013 sc->remote_mode = 0;
1023 * Force a resend by guaranteeing that reported_but
1024 * differs from track_but.
1026 fifo_write_char(&sc->rfifo, 0xFA);
1027 sc->data_signal = 1;
1033 fifo_reset(&sc->rfifo); /* should we do this? */
1034 fifo_reset(&sc->wfifo); /* should we do this? */
1035 fifo_write_char(&sc->rfifo, 0xFA);
1042 kprintf("unknown command %02x\n", sc->ps2_cmd);
1045 if (cmd_completed) {
1054 if (error == 0 && (cmd_completed || uio->uio_resid))
1059 static void cyapa_filt_detach(struct knote *);
1060 static int cyapa_filt(struct knote *, long);
1062 static struct filterops cyapa_filtops =
1063 { FILTEROP_ISFD, NULL, cyapa_filt_detach, cyapa_filt };
1066 cyapakqfilter(struct dev_kqfilter_args *ap)
1068 cdev_t dev = ap->a_head.a_dev;
1069 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
1070 struct knote *kn = ap->a_kn;
1071 struct klist *klist;
1073 switch(kn->kn_filter) {
1075 kn->kn_fop = &cyapa_filtops;
1076 kn->kn_hook = (void *)sc;
1080 ap->a_result = EOPNOTSUPP;
1083 klist = &sc->kqinfo.ki_note;
1084 knote_insert(klist, kn);
1090 cyapa_filt_detach(struct knote *kn)
1092 struct cyapa_softc *sc = (struct cyapa_softc *)kn->kn_hook;
1093 struct klist *klist;
1095 klist = &sc->kqinfo.ki_note;
1096 knote_remove(klist, kn);
1100 cyapa_filt(struct knote *kn, long hint)
1102 struct cyapa_softc *sc = (struct cyapa_softc *)kn->kn_hook;
1106 if (fifo_ready(&sc->rfifo) || sc->data_signal)
1116 cyapaioctl(struct dev_ioctl_args *ap)
1118 cdev_t dev = ap->a_head.a_dev;
1119 device_t bus; /* smbbus */
1120 /*struct cyapacmd *s = (struct cyapacmd *)ap->a_data;*/
1122 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
1131 * NOTE: smbus_*() functions automatically recurse the parent to
1132 * get to the actual device driver.
1134 bus = device_get_parent(sc->dev); /* smbus */
1136 /* Allocate the bus. */
1137 if ((error = smbus_request_bus(bus, sc->dev,
1138 (ap->a_fflag & O_NONBLOCK) ?
1139 SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
1142 switch (ap->a_cmd) {
1145 error = inputev_ioctl(&sc->iev, ap->a_cmd, ap->a_data);
1151 smbus_release_bus(bus, sc->dev);
1157 * MAJOR SUPPORT FUNCTIONS
1161 cyapa_poll_thread(void *arg)
1163 struct cyapa_softc *sc = arg;
1164 struct cyapa_regs regs;
1165 device_t bus; /* smbbus */
1167 int freq = cyapa_norm_freq;
1169 int pstate = CMD_POWER_MODE_IDLE;
1171 int last_reset = ticks;
1173 bus = device_get_parent(sc->dev);
1175 while ((sc->poll_flags & CYPOLL_SHUTDOWN) == 0) {
1176 error = smbus_request_bus(bus, sc->dev, SMB_WAIT);
1178 error = smbus_trans(bus, sc->addr, CMD_DEV_STATUS,
1179 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
1181 (void *)®s, sizeof(regs), NULL);
1183 isidle = cyapa_raw_input(sc, ®s);
1187 * For some reason the device can crap-out. If it
1188 * drops back into bootstrap mode try to reinitialize
1192 ((regs.stat & CYAPA_STAT_RUNNING) == 0 &&
1193 (unsigned)(ticks - last_reset) > TIME_TO_RESET)) {
1196 init_device(sc->dev, NULL, sc->addr, 2);
1198 smbus_release_bus(bus, sc->dev);
1200 tsleep(&sc->poll_flags, 0, "cyapw", (hz + freq - 1) / freq);
1202 if (sc->count == 0) {
1203 freq = cyapa_idle_freq;
1204 npstate = CMD_POWER_MODE_IDLE;
1205 } else if (isidle) {
1206 freq = cyapa_slow_freq;
1207 npstate = CMD_POWER_MODE_IDLE;
1209 freq = cyapa_norm_freq;
1210 npstate = CMD_POWER_MODE_FULL;
1212 if (pstate != npstate) {
1214 cyapa_set_power_mode(sc, pstate);
1217 case CMD_POWER_MODE_OFF:
1218 kprintf("cyapa: power off\n");
1220 case CMD_POWER_MODE_IDLE:
1221 kprintf("cyapa: power idle\n");
1223 case CMD_POWER_MODE_FULL:
1224 kprintf("cyapa: power full\n");
1231 wakeup(&sc->poll_td);
1236 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs)
1239 int afingers; /* actual fingers after culling */
1249 uint16_t but; /* high bits used for simulated but4/but5 */
1252 * If the device is not running the rest of the status
1253 * means something else, set fingers to 0.
1255 if ((regs->stat & CYAPA_STAT_RUNNING) == 0) {
1260 * Process fingers/movement
1262 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1263 afingers = nfingers;
1266 kprintf("stat %02x buttons %c%c%c nfngrs=%d ",
1268 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1269 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'L' : '-'),
1270 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'L' : '-'),
1274 for (i = 0; i < nfingers; ++i) {
1276 kprintf(" [x=%04d y=%04d p=%d]",
1277 CYAPA_TOUCH_X(regs, i),
1278 CYAPA_TOUCH_Y(regs, i),
1279 CYAPA_TOUCH_P(regs, i));
1281 if (CYAPA_TOUCH_P(regs, i) < cyapa_minpressure)
1286 * Tracking for local solutions
1291 * Track timing for finger-downs. Used to detect false-3-finger
1298 if (sc->track_nfingers == 0)
1299 sc->finger1_ticks = sc->poll_ticks;
1302 if (sc->track_nfingers <= 0)
1303 sc->finger1_ticks = sc->poll_ticks;
1304 if (sc->track_nfingers <= 1)
1305 sc->finger2_ticks = sc->poll_ticks;
1309 if (sc->track_nfingers <= 0)
1310 sc->finger1_ticks = sc->poll_ticks;
1311 if (sc->track_nfingers <= 1)
1312 sc->finger2_ticks = sc->poll_ticks;
1313 if (sc->track_nfingers <= 2)
1314 sc->finger3_ticks = sc->poll_ticks;
1317 sc->track_nfingers = afingers;
1320 * Lookup and track finger indexes in the touch[] array.
1322 if (afingers == 0) {
1340 * The id assigned on touch can move around in the array,
1341 * find it. If that finger is lifted up, assign some other
1342 * finger for mouse tracking and reset track_x and track_y
1343 * to avoid a mouse jump.
1345 * If >= 2 fingers are down be sure not to assign i and
1346 * j to the same index.
1348 for (i = 0; i < nfingers; ++i) {
1349 if (sc->track_id1 == regs->touch[i].id)
1352 if (i == nfingers ||
1353 CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) {
1358 sc->track_id1 = regs->touch[i].id;
1359 if (sc->track_id2 == sc->track_id1)
1366 for (j = 0; j < nfingers; ++j) {
1367 if (sc->track_id2 == regs->touch[j].id)
1370 if (j == nfingers ||
1371 CYAPA_TOUCH_P(regs, j) < cyapa_minpressure) {
1372 if (afingers >= 2) {
1377 sc->track_id2 = regs->touch[j].id;
1385 * The third finger is used to tap or tap-hold to simulate
1386 * a button, we don't have to record it persistently.
1388 if (afingers >= 3) {
1390 if (i == 0 || j == 0)
1392 if (i == 1 || j == 1)
1400 * On initial touch determine if we are in the slider area. Setting
1401 * track_z conditionalizes the delta calculations later on.
1403 if (afingers && sc->zenabled > 0 &&
1404 sc->track_x == -1 && sc->track_z == -1) {
1405 x = CYAPA_TOUCH_X(regs, i);
1406 z = CYAPA_TOUCH_Y(regs, i);
1407 if (x > sc->cap_resx * 9 / 10)
1411 if (afingers && sc->track_z != -1) {
1413 * Slider emulation (right side of trackpad). Z is tracked
1414 * based on the Y position. X and Y tracking are disabled.
1416 * Because we are emulating a mouse-wheel, we do not want
1417 * to shove events out at the maximum resolution.
1419 z = CYAPA_TOUCH_Y(regs, i);
1420 sc->delta_z += z / ZSCALE - sc->track_z;
1421 if (sc->touch_z == -1)
1422 sc->touch_z = z; /* not used atm */
1423 sc->track_z = z / ZSCALE;
1424 } else if (afingers) {
1426 * Normal pad position reporting (track_z is left -1)
1428 x = CYAPA_TOUCH_X(regs, i);
1429 y = CYAPA_TOUCH_Y(regs, i);
1430 if (sc->track_x != -1) {
1431 sc->delta_x += x - sc->track_x;
1432 sc->delta_y -= y - sc->track_y;
1433 if (sc->delta_x > sc->cap_resx)
1434 sc->delta_x = sc->cap_resx;
1435 if (sc->delta_x < -sc->cap_resx)
1436 sc->delta_x = -sc->cap_resx;
1437 if (sc->delta_y > sc->cap_resx)
1438 sc->delta_y = sc->cap_resy;
1439 if (sc->delta_y < -sc->cap_resy)
1440 sc->delta_y = -sc->cap_resy;
1442 if (sc->touch_x == -1) {
1449 if (afingers >= 5 && sc->zenabled > 1 && sc->track_z < 0) {
1451 * Simulate the 5th button (when not in slider mode)
1453 but = SIMULATE_BUT5;
1454 } else if (afingers >= 4 && sc->zenabled > 1 && sc->track_z < 0) {
1456 * Simulate the 4th button (when not in slider mode)
1458 but = SIMULATE_BUT4;
1459 } else if (afingers >= 3 && sc->track_z < 0) {
1461 * Simulate the left, middle, or right button with 3
1462 * fingers when not in slider mode.
1464 * This makes it ultra easy to hit GUI buttons and move
1465 * windows with a light touch, without having to apply the
1466 * pressure required to articulate the button.
1468 * However, if we are coming down from 4 or 5 fingers,
1469 * do NOT simulate the left button and instead just release
1470 * button 4 or button 5. Leave SIMULATE_LOCK set to
1471 * placemark the condition. We must go down to 2 fingers
1472 * to release the lock.
1474 * LEFT BUTTON: Fingers arranged left-to-right 1 2 3,
1475 * move mouse with fingers 2 and 3 and tap
1476 * or hold with finger 1 (to the left of fingers
1479 * RIGHT BUTTON: Move mouse with fingers 1 and 2 and tap
1480 * or hold with finger 3.
1482 * MIDDLE BUTTON: Move mouse with fingers 1 and 3 and tap
1483 * or hold with finger 2.
1485 * Finally, detect when all three fingers were placed down
1486 * within one tick of each other.
1488 x1 = CYAPA_TOUCH_X(regs, i); /* 1st finger down */
1489 x2 = CYAPA_TOUCH_X(regs, j); /* 2nd finger down */
1490 x = CYAPA_TOUCH_X(regs, k); /* 3rd finger (button) down */
1491 if (sc->track_but & (SIMULATE_BUT4 |
1494 but = SIMULATE_LOCK;
1495 } else if (sc->track_but & ~SIMULATE_LOCK) {
1496 but = sc->track_but;
1497 } else if ((int)(sc->finger3_ticks - sc->finger1_ticks) <
1498 cyapa_norm_freq / 25 + 1) {
1500 * False 3-finger button detection (but still detect
1501 * if the actual physical button is held down).
1503 if (regs->fngr & CYAPA_FNGR_LEFT)
1504 but = CYAPA_FNGR_LEFT;
1507 } else if (x < x1 && x < x2) {
1508 but = CYAPA_FNGR_LEFT;
1509 } else if (x > x1 && x < x2) {
1510 but = CYAPA_FNGR_MIDDLE;
1511 } else if (x > x2 && x < x1) {
1512 but = CYAPA_FNGR_MIDDLE;
1514 but = CYAPA_FNGR_RIGHT;
1516 } else if (afingers == 2 || (afingers >= 2 && sc->track_z >= 0)) {
1518 * If 2 fingers are held down or 2 or more fingers are held
1519 * down and we are in slider mode, any key press is
1520 * interpreted as a left mouse button press.
1522 * If a keypress is already active we retain the active
1525 * The high-button state is unconditionally cleared with <= 2
1528 if (regs->fngr & CYAPA_FNGR_LEFT) {
1529 but = sc->track_but & ~SIMULATE_LOCK;
1531 but = CYAPA_FNGR_LEFT;
1535 } else if (afingers == 1 &&
1536 (abs(sc->touch_x - sc->track_x) > 32 ||
1537 abs(sc->touch_y - sc->track_y) > 32)) {
1539 * When using one finger, any significant mouse movement
1540 * will lock you to the left mouse button if you push the
1541 * button, regardless of where you are on the pad.
1543 * If a keypress is already active we retain the active
1546 * The high-button state is unconditionally cleared with <= 2
1549 if (regs->fngr & CYAPA_FNGR_LEFT) {
1550 but = sc->track_but & ~SIMULATE_LOCK;
1552 but = CYAPA_FNGR_LEFT;
1556 } else if (afingers == 1 && (regs->fngr & CYAPA_FNGR_LEFT)) {
1558 * If you are swiping while holding a button down, the
1559 * button registration does not change. Otherwise the
1560 * registered button depends on where you are on the pad.
1562 * Since no significant movement occurred we allow the
1563 * button to be pressed while within the slider area
1564 * and still be properly registered as the right button.
1566 * The high-button state is unconditionally cleared with <= 2
1569 if (sc->track_but & ~SIMULATE_LOCK)
1570 but = sc->track_but & ~SIMULATE_LOCK;
1571 else if (sc->track_x < sc->cap_resx * 1 / 3)
1572 but = CYAPA_FNGR_LEFT;
1573 else if (sc->track_x < sc->cap_resx * 2 / 3)
1574 but = CYAPA_FNGR_MIDDLE;
1576 but = CYAPA_FNGR_RIGHT;
1577 } else if (afingers == 1) {
1579 * Clear all finger state if 1 finger is down and nothing
1585 * Clear all finger state if no fingers are down.
1591 * Detect state change from last reported state and
1592 * determine if we have gone idle.
1594 sc->track_but = but;
1595 if (sc->delta_x || sc->delta_y || sc->delta_z ||
1596 sc->track_but != sc->reported_but) {
1597 sc->active_tick = ticks;
1598 if (sc->remote_mode == 0 && sc->reporting_mode)
1599 sc->data_signal = 1;
1601 } else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) {
1602 sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */
1617 cyapa_set_power_mode(struct cyapa_softc *sc, int mode)
1623 bus = device_get_parent(sc->dev);
1624 error = smbus_request_bus(bus, sc->dev, SMB_WAIT);
1626 error = smbus_trans(bus, sc->addr, CMD_POWER_MODE,
1627 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
1628 NULL, 0, (void *)&data, 1, NULL);
1629 data = (data & ~0xFC) | mode;
1631 error = smbus_trans(bus, sc->addr, CMD_POWER_MODE,
1632 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
1633 (void *)&data, 1, NULL, 0, NULL);
1635 smbus_release_bus(bus, sc->dev);
1644 * Returns non-zero if the fifo is empty
1648 fifo_empty(struct cyapa_fifo *fifo)
1650 return(fifo->rindex == fifo->windex);
1654 * Returns the number of characters available for reading from
1655 * the fifo without wrapping the fifo buffer.
1659 fifo_ready(struct cyapa_fifo *fifo)
1663 n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK);
1664 if (n > (size_t)(fifo->windex - fifo->rindex))
1665 n = (size_t)(fifo->windex - fifo->rindex);
1671 * Returns the number of characters available for reading from
1672 * the fifo including wrapping the fifo buffer.
1676 fifo_total_ready(struct cyapa_fifo *fifo)
1678 return ((size_t)(fifo->windex - fifo->rindex));
1683 * Returns a read pointer into the fifo and then bumps
1684 * rindex. The FIFO must have at least 'n' characters in
1685 * it. The value (n) can cause the index to wrap but users
1686 * of the buffer should never supply a value for (n) that wraps
1691 fifo_read(struct cyapa_fifo *fifo, size_t n)
1695 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1696 kprintf("fifo_read: overflow\n");
1699 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1707 fifo_read_char(struct cyapa_fifo *fifo)
1711 if (fifo->rindex == fifo->windex) {
1712 kprintf("fifo_read_char: overflow\n");
1715 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1723 * Write a character to the FIFO. The character will be discarded
1724 * if the FIFO is full.
1728 fifo_write_char(struct cyapa_fifo *fifo, uint8_t c)
1730 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1731 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1737 * Return the amount of space available for writing without wrapping
1742 fifo_space(struct cyapa_fifo *fifo)
1746 n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK);
1747 if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)))
1748 n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex));
1754 fifo_write(struct cyapa_fifo *fifo, size_t n)
1758 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1766 fifo_reset(struct cyapa_fifo *fifo)
1777 cyapa_fuzz(short delta, short *fuzzp)
1782 if (fuzz >= 0 && delta < 0) {
1785 } else if (fuzz <= 0 && delta > 0) {
1794 DRIVER_MODULE(cyapa, smbus, cyapa_driver, cyapa_devclass, NULL, NULL);
1795 MODULE_DEPEND(cyapa, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
1796 MODULE_VERSION(cyapa, 1);