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 * Slider jesture - Tap right hand side and slide up or down.
71 * (Three finger jestures)
72 * left button jesture - Two fingers down on the left, tap/hold right
73 * middle button jesture- Two fingers down left & right, tap/hold middle
74 * right button jesture - Two fingers down on the right, tap/hold left
76 * track-pad button - Tap/push physical button, left, middle, or right
77 * side of the trackpad will issue a LEFT, MIDDLE, or
80 * track-pad button - Any tap/slide of more than 32 pixels and pushing
81 * harder to articulate the trackpad physical button
82 * always issues a LEFT button event.
84 * first-finger tracking- The X/Y coordinates always track the first finger
85 * down. If you have multiple fingers down and lift
86 * up the designated first finger, a new designated
87 * first finger will be selected without causing the
88 * mouse to jump (delta's are reset).
90 #include <sys/kernel.h>
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/device.h>
94 #include <sys/module.h>
98 #include <sys/fcntl.h>
99 /*#include <sys/input.h>*/
100 #include <sys/vnode.h>
101 #include <sys/sysctl.h>
102 #include <sys/event.h>
103 #include <sys/devfs.h>
105 #include <bus/smbus/smbconf.h>
106 #include <bus/smbus/smbus.h>
109 #include "smbus_if.h"
111 #include "device_if.h"
113 #define CYAPA_BUFSIZE 128 /* power of 2 */
114 #define CYAPA_BUFMASK (CYAPA_BUFSIZE - 1)
121 char buf[CYAPA_BUFSIZE];
126 int count; /* >0 if device opened */
130 struct kqinfo kqinfo;
142 struct inputev iev; /* subr_input.c */
146 * PS/2 mouse emulation
148 short track_x; /* current tracking */
152 char track_id1; /* first finger id */
153 char track_id2; /* second finger id */
154 short delta_x; /* accumulation -> report */
160 short touch_x; /* touch down coordinates */
163 uint16_t reported_but;
165 struct cyapa_fifo rfifo; /* device->host */
166 struct cyapa_fifo wfifo; /* host->device */
167 uint8_t ps2_cmd; /* active p2_cmd waiting for data */
171 int reporting_mode; /* 0=disabled 1=enabled */
172 int scaling_mode; /* 0=1:1 1=2:1 */
173 int remote_mode; /* 0 for streaming mode */
174 int resolution; /* count/mm */
175 int sample_rate; /* samples/sec */
176 int zenabled; /* z-axis enabled (mode 1 or 2) */
179 #define CYPOLL_SHUTDOWN 0x0001
181 #define SIMULATE_BUT4 0x0100
182 #define SIMULATE_BUT5 0x0200
183 #define SIMULATE_LOCK 0x8000
185 static void cyapa_poll_thread(void *arg);
186 static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs);
188 static int fifo_empty(struct cyapa_fifo *fifo);
189 static size_t fifo_ready(struct cyapa_fifo *fifo);
191 static size_t fifo_total_ready(struct cyapa_fifo *fifo);
193 static char *fifo_read(struct cyapa_fifo *fifo, size_t n);
194 static char *fifo_write(struct cyapa_fifo *fifo, size_t n);
195 static uint8_t fifo_read_char(struct cyapa_fifo *fifo);
196 static void fifo_write_char(struct cyapa_fifo *fifo, uint8_t c);
197 static size_t fifo_space(struct cyapa_fifo *fifo);
198 static void fifo_reset(struct cyapa_fifo *fifo);
200 static short cyapa_fuzz(short delta, short *fuzz);
202 static int cyapa_idle_freq = 1;
203 SYSCTL_INT(_debug, OID_AUTO, cyapa_idle_freq, CTLFLAG_RW,
204 &cyapa_idle_freq, 0, "");
205 static int cyapa_slow_freq = 20;
206 SYSCTL_INT(_debug, OID_AUTO, cyapa_slow_freq, CTLFLAG_RW,
207 &cyapa_slow_freq, 0, "");
208 static int cyapa_norm_freq = 100;
209 SYSCTL_INT(_debug, OID_AUTO, cyapa_norm_freq, CTLFLAG_RW,
210 &cyapa_norm_freq, 0, "");
212 static int cyapa_debug = 0;
213 SYSCTL_INT(_debug, OID_AUTO, cyapa_debug, CTLFLAG_RW,
214 &cyapa_debug, 0, "");
218 cyapa_lock(struct cyapa_softc *sc)
220 lockmgr(&sc->lk, LK_EXCLUSIVE);
225 cyapa_unlock(struct cyapa_softc *sc)
227 lockmgr(&sc->lk, LK_RELEASE);
231 * Notify if possible receive data ready. Must be called
232 * without the lock held to avoid deadlocking in kqueue.
236 cyapa_notify(struct cyapa_softc *sc)
238 if (sc->data_signal || !fifo_empty(&sc->rfifo)) {
239 KNOTE(&sc->kqinfo.ki_note, 0);
243 wakeup(&sc->blocked);
250 * Initialize the device
254 init_device(device_t dev, struct cyapa_cap *cap, int addr, int probe)
256 static char bl_exit[] = {
257 0x00, 0xff, 0xa5, 0x00, 0x01,
258 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
259 static char bl_deactivate[] = {
260 0x00, 0xff, 0x3b, 0x00, 0x01,
261 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
263 struct cyapa_boot_regs boot;
268 bus = device_get_parent(dev); /* smbus */
273 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
274 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
275 NULL, 0, (void *)&boot, sizeof(boot), NULL);
280 * Bootstrap the device if necessary. It can take up to 2 seconds
281 * for the device to fully initialize.
284 while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) {
285 if (boot.boot & CYAPA_BOOT_BUSY) {
289 } else if (boot.error & CYAPA_ERROR_BOOTLOADER) {
293 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
294 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
296 sizeof(bl_deactivate),
304 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
305 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
312 tsleep(&error, 0, "cyapabt", hz / 10);
314 error = smbus_trans(bus, addr, CMD_BOOT_STATUS,
315 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
316 NULL, 0, (void *)&boot, sizeof(boot), NULL);
322 device_printf(dev, "Unable to bring device out of bootstrap\n");
330 error = smbus_trans(bus, addr, CMD_QUERY_CAPABILITIES,
331 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
332 NULL, 0, (void *)cap, sizeof(*cap), NULL);
334 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) {
335 device_printf(dev, "Product ID \"%5.5s\" mismatch\n",
342 device_printf(dev, "Unable to initialize\n");
347 * Device infrastructure
349 #define CYAPA_SOFTC(unit) \
350 ((struct cyapa_softc *)devclass_get_softc(cyapa_devclass, (unit)))
352 static void cyapa_identify(driver_t *driver, device_t parent);
353 static int cyapa_probe(device_t);
354 static int cyapa_attach(device_t);
355 static int cyapa_detach(device_t);
357 static devclass_t cyapa_devclass;
359 static device_method_t cyapa_methods[] = {
360 /* device interface */
361 DEVMETHOD(device_identify, cyapa_identify),
362 DEVMETHOD(device_probe, cyapa_probe),
363 DEVMETHOD(device_attach, cyapa_attach),
364 DEVMETHOD(device_detach, cyapa_detach),
367 /* smbus interface */
368 DEVMETHOD(smbus_intr, smbus_generic_intr),
374 static driver_t cyapa_driver = {
377 sizeof(struct cyapa_softc),
380 static d_open_t cyapaopen;
381 static d_close_t cyapaclose;
382 static d_ioctl_t cyapaioctl;
383 static d_read_t cyaparead;
384 static d_write_t cyapawrite;
385 static d_kqfilter_t cyapakqfilter;
387 static struct dev_ops cyapa_ops = {
390 .d_close = cyapaclose,
391 .d_ioctl = cyapaioctl,
393 .d_write = cyapawrite,
394 .d_kqfilter = cyapakqfilter,
398 cyapa_identify(driver_t *driver, device_t parent)
400 if (device_find_child(parent, "cyapa", -1) == NULL)
401 BUS_ADD_CHILD(parent, parent, 0, "cyapa", -1);
405 cyapa_probe(device_t dev)
407 struct cyapa_cap cap;
412 unit = device_get_unit(dev);
415 * Only match against specific addresses to avoid blowing up
416 * other I2C devices (?). At least for now.
418 * 0x400 (from smbus) - means specific device address probe,
419 * rather than generic.
421 * 0x67 - cypress trackpad on the acer c720.
423 if ((unit & 0x04FF) != (0x0400 | 0x067))
426 error = init_device(dev, &cap, addr, 1);
430 device_set_desc(dev, "Cypress APA I2C Trackpad");
432 return (BUS_PROBE_VENDOR);
436 cyapa_attach(device_t dev)
438 struct cyapa_softc *sc = (struct cyapa_softc *)device_get_softc(dev);
439 struct cyapa_cap cap;
446 bzero(sc, sizeof(struct cyapa_softc *));
448 lockinit(&sc->lk, "cyapa", 0, 0);
449 sc->reporting_mode = 1;
451 unit = device_get_unit(dev);
452 if ((unit & 0x04FF) != (0x0400 | 0x067))
455 if (init_device(dev, &cap, addr, 0))
463 sc->devnode = make_dev(&cyapa_ops, unit,
464 UID_ROOT, GID_WHEEL, 0600,
465 "cyapa%d-%02x", unit >> 11, unit & 1023);
467 sc->devnode = make_dev(&cyapa_ops, unit,
468 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit);
471 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) |
473 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) |
475 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) |
477 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) |
479 sc->cap_buttons = cap.buttons;
481 device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n",
482 cap.prod_ida, cap.prod_idb, cap.prod_idc,
483 ((cap.buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'),
484 ((cap.buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
485 ((cap.buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
490 * Setup input event tracking
493 inputev_init(&sc->iev, "Cypress APA Trackpad (cyapa)");
494 inputev_set_evbit(&sc->iev, EV_ABS);
495 inputev_set_abs_params(&sc->iev, ABS_MT_POSITION_X,
496 0, sc->cap_resx, 0, 0);
497 inputev_set_abs_params(&sc->iev, ABS_MT_POSITION_Y,
498 0, sc->cap_resy, 0, 0);
499 inputev_set_abs_params(&sc->iev, ABS_MT_PRESSURE,
502 inputev_set_res(&sc->iev, ABS_MT_POSITION_X,
503 sc->cap_resx / sc->cap_phyx);
505 inputev_set_res(&sc->iev, ABS_MT_POSITION_Y,
506 sc->cap_resy / sc->cap_phyy);
507 if (cap.buttons & CYAPA_FNGR_LEFT) {
508 inputev_set_keybit(&sc->iev, BTN_LEFT);
509 inputev_set_propbit(&sc->iev, INPUT_PROP_BUTTONPAD);
511 if (cap.buttons & CYAPA_FNGR_MIDDLE)
512 inputev_set_keybit(&sc->iev, BTN_MIDDLE);
513 if (cap.buttons & CYAPA_FNGR_RIGHT)
514 inputev_set_keybit(&sc->iev, BTN_RIGHT);
516 inputev_register(&sc->iev);
520 * Start the polling thread.
522 lwkt_create(cyapa_poll_thread, sc,
523 &sc->poll_td, NULL, 0, -1, "cyapa-poll");
529 cyapa_detach(device_t dev)
531 struct cyapa_softc *sc = (struct cyapa_softc *)device_get_softc(dev);
535 * Cleanup input event tracking
537 inputev_deregister(&sc->iev);
541 * Cleanup our poller thread
543 atomic_set_int(&sc->poll_flags, CYPOLL_SHUTDOWN);
544 while (sc->poll_td) {
545 wakeup(&sc->poll_flags);
546 tsleep(&sc->poll_td, 0, "cyapadet", hz);
550 dev_ops_remove_minor(&cyapa_ops, device_get_unit(dev));
552 devfs_assume_knotes(sc->devnode, &sc->kqinfo);
558 * USER DEVICE I/O FUNCTIONS
561 cyapaopen (struct dev_open_args *ap)
563 cdev_t dev = ap->a_head.a_dev;
564 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
578 cyapaclose(struct dev_close_args *ap)
580 cdev_t dev = ap->a_head.a_dev;
581 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
587 /* This is not supposed to happen. */
596 cyaparead(struct dev_read_args *ap)
598 cdev_t dev = ap->a_head.a_dev;
599 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
601 struct uio *uio = ap->a_uio;
602 int ioflag = ap->a_ioflag;
607 * If buffer is empty, load a new event if it is ready
611 if (fifo_empty(&sc->rfifo) &&
612 (sc->data_signal || sc->delta_x || sc->delta_y ||
613 sc->track_but != sc->reported_but)) {
621 * Accumulate delta_x, delta_y.
624 delta_x = sc->delta_x;
625 delta_y = sc->delta_y;
626 delta_z = sc->delta_z;
631 if (delta_x < -256) {
639 if (delta_y < -256) {
647 if (delta_z < -256) {
654 * Adjust baseline for next calculation
656 sc->delta_x -= delta_x;
657 sc->delta_y -= delta_y;
658 sc->delta_z -= delta_z;
659 sc->reported_but = but;
662 * Fuzz reduces movement jitter by introducing some
663 * hysteresis. It operates without cumulative error so
664 * if you swish around quickly and return your finger to
665 * where it started, so to will the mouse.
667 delta_x = cyapa_fuzz(delta_x, &sc->fuzz_x);
668 delta_y = cyapa_fuzz(delta_y, &sc->fuzz_y);
669 delta_z = cyapa_fuzz(delta_z, &sc->fuzz_z);
680 if (but & CYAPA_FNGR_LEFT)
682 if (but & CYAPA_FNGR_MIDDLE)
684 if (but & CYAPA_FNGR_RIGHT)
687 fifo_write_char(&sc->rfifo, c0);
688 fifo_write_char(&sc->rfifo, (uint8_t)delta_x);
689 fifo_write_char(&sc->rfifo, (uint8_t)delta_y);
690 switch(sc->zenabled) {
695 fifo_write_char(&sc->rfifo, (uint8_t)delta_z);
699 * Z axis low 4 bits + 4th button and 5th button
700 * (high 2 bits must be left 0). Auto-scale
701 * delta_z to fit to avoid a wrong-direction
702 * overflow (don't try to retain the remainder).
704 while (delta_z > 7 || delta_z < -8)
706 c0 = (uint8_t)delta_z & 0x0F;
707 if (but & SIMULATE_BUT4)
709 if (but & SIMULATE_BUT5)
711 fifo_write_char(&sc->rfifo, c0);
723 * Blocking / Non-blocking
726 didread = (uio->uio_resid == 0);
728 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(&sc->rfifo)) {
732 error = lksleep(&sc->blocked, &sc->lk, PCATCH, "cyablk", 0);
738 * Return any buffered data
740 while (error == 0 && uio->uio_resid &&
741 (n = fifo_ready(&sc->rfifo)) > 0) {
742 if (n > uio->uio_resid)
746 uint8_t *ptr = fifo_read(&sc->rfifo, 0);
749 for (v = 0; v < n; ++v)
750 kprintf(" %02x", ptr[v]);
754 error = uiomove(fifo_read(&sc->rfifo, 0), n, uio);
757 fifo_read(&sc->rfifo, n);
762 if (error == 0 && didread == 0)
769 cyapawrite(struct dev_write_args *ap)
771 cdev_t dev = ap->a_head.a_dev;
772 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
773 struct uio *uio = ap->a_uio;
781 * Copy data from userland. This will also cross-over the end
782 * of the fifo and keep filling.
785 while ((n = fifo_space(&sc->wfifo)) > 0 && uio->uio_resid) {
786 if (n > uio->uio_resid)
788 error = uiomove(fifo_write(&sc->wfifo, 0), n, uio);
791 fifo_write(&sc->wfifo, n);
797 cmd_completed = (fifo_ready(&sc->wfifo) != 0);
798 while (fifo_ready(&sc->wfifo) && cmd_completed && error == 0) {
799 if (sc->ps2_cmd == 0)
800 sc->ps2_cmd = fifo_read_char(&sc->wfifo);
801 switch(sc->ps2_cmd) {
806 sc->scaling_mode = 0;
807 fifo_write_char(&sc->rfifo, 0xFA);
813 sc->scaling_mode = 1;
814 fifo_write_char(&sc->rfifo, 0xFA);
818 * SET RESOLUTION +1 byte
820 if (sc->ps2_acked == 0) {
822 fifo_write_char(&sc->rfifo, 0xFA);
824 if (fifo_ready(&sc->wfifo) == 0) {
828 sc->resolution = fifo_read_char(&sc->wfifo);
829 fifo_write_char(&sc->rfifo, 0xFA);
837 * bit 6 Mode (1=remote mode, 0=stream mode)
838 * bit 5 Enable (data reporting enabled)
839 * bit 4 Scaling (0=1:1 1=2:1)
841 * bit 2 LEFT BUTTON (1 if pressed)
842 * bit 1 MIDDLE BUTTON (1 if pressed)
843 * bit 0 RIGHT BUTTON (1 if pressed)
845 * byte2: resolution counts/mm
851 if (sc->reporting_mode)
853 if (sc->scaling_mode)
855 if (sc->track_but & CYAPA_FNGR_LEFT)
857 if (sc->track_but & CYAPA_FNGR_MIDDLE)
859 if (sc->track_but & CYAPA_FNGR_RIGHT)
861 fifo_write_char(&sc->rfifo, 0xFA);
862 fifo_write_char(&sc->rfifo, c0);
863 fifo_write_char(&sc->rfifo, 0x00);
864 fifo_write_char(&sc->rfifo, 100);
868 * Set stream mode and reset movement counters
871 fifo_write_char(&sc->rfifo, 0xFA);
878 * Read Data (if in remote mode). If not in remote
879 * mode force an event.
881 fifo_write_char(&sc->rfifo, 0xFA);
886 * Reset Wrap Mode (ignored)
888 fifo_write_char(&sc->rfifo, 0xFA);
892 * Set Wrap Mode (ignored)
894 fifo_write_char(&sc->rfifo, 0xFA);
901 fifo_write_char(&sc->rfifo, 0xFA);
910 * If we send 0x00 - normal PS/2 mouse, no Z-axis
912 * If we send 0x03 - Intellimouse, data packet has
913 * an additional Z movement byte (8 bits signed).
914 * (also reset movement counters)
916 * If we send 0x04 - Now includes z-axis and the
917 * 4th and 5th mouse buttons.
919 fifo_write_char(&sc->rfifo, 0xFA);
920 switch(sc->zenabled) {
922 fifo_write_char(&sc->rfifo, 0x03);
925 fifo_write_char(&sc->rfifo, 0x04);
928 fifo_write_char(&sc->rfifo, 0x00);
939 * byte1: the sample rate
941 if (sc->ps2_acked == 0) {
943 fifo_write_char(&sc->rfifo, 0xFA);
945 if (fifo_ready(&sc->wfifo) == 0) {
949 sc->sample_rate = fifo_read_char(&sc->wfifo);
950 fifo_write_char(&sc->rfifo, 0xFA);
953 * zenabling sequence: 200,100,80 (device id 0x03)
954 * 200,200,80 (device id 0x04)
956 * We support id 0x03 (no 4th or 5th button).
957 * We support id 0x04 (w/ 4th and 5th button).
959 if (sc->zenabled == 0 && sc->sample_rate == 200)
961 else if (sc->zenabled == -1 && sc->sample_rate == 100)
963 else if (sc->zenabled == -1 && sc->sample_rate == 200)
965 else if (sc->zenabled == -2 && sc->sample_rate == 80)
966 sc->zenabled = 1; /* z-axis mode */
967 else if (sc->zenabled == -3 && sc->sample_rate == 80)
968 sc->zenabled = 2; /* z-axis+but4/5 */
972 * Enable data reporting. Only effects stream mode.
974 fifo_write_char(&sc->rfifo, 0xFA);
975 sc->reporting_mode = 1;
979 * Disable data reporting. Only effects stream mode.
981 fifo_write_char(&sc->rfifo, 0xFA);
982 sc->reporting_mode = 1;
988 * (reset sampling rate, resolution, scaling and
991 fifo_write_char(&sc->rfifo, 0xFA);
992 sc->sample_rate = 100;
994 sc->scaling_mode = 0;
995 sc->reporting_mode = 0;
1006 * Force a resend by guaranteeing that reported_but
1007 * differs from track_but.
1009 fifo_write_char(&sc->rfifo, 0xFA);
1010 sc->data_signal = 1;
1016 fifo_reset(&sc->rfifo); /* should we do this? */
1017 fifo_reset(&sc->wfifo); /* should we do this? */
1018 fifo_write_char(&sc->rfifo, 0xFA);
1025 kprintf("unknown command %02x\n", sc->ps2_cmd);
1028 if (cmd_completed) {
1037 if (error == 0 && (cmd_completed || uio->uio_resid))
1042 static void cyapa_filt_detach(struct knote *);
1043 static int cyapa_filt(struct knote *, long);
1045 static struct filterops cyapa_filtops =
1046 { FILTEROP_ISFD, NULL, cyapa_filt_detach, cyapa_filt };
1049 cyapakqfilter(struct dev_kqfilter_args *ap)
1051 cdev_t dev = ap->a_head.a_dev;
1052 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
1053 struct knote *kn = ap->a_kn;
1054 struct klist *klist;
1056 switch(kn->kn_filter) {
1058 kn->kn_fop = &cyapa_filtops;
1059 kn->kn_hook = (void *)sc;
1063 ap->a_result = EOPNOTSUPP;
1066 klist = &sc->kqinfo.ki_note;
1067 knote_insert(klist, kn);
1073 cyapa_filt_detach(struct knote *kn)
1075 struct cyapa_softc *sc = (struct cyapa_softc *)kn->kn_hook;
1076 struct klist *klist;
1078 klist = &sc->kqinfo.ki_note;
1079 knote_remove(klist, kn);
1083 cyapa_filt(struct knote *kn, long hint)
1085 struct cyapa_softc *sc = (struct cyapa_softc *)kn->kn_hook;
1089 if (fifo_ready(&sc->rfifo) || sc->data_signal)
1099 cyapaioctl(struct dev_ioctl_args *ap)
1101 cdev_t dev = ap->a_head.a_dev;
1102 device_t bus; /* smbbus */
1103 /*struct cyapacmd *s = (struct cyapacmd *)ap->a_data;*/
1105 struct cyapa_softc *sc = CYAPA_SOFTC(minor(dev));
1114 * NOTE: smbus_*() functions automatically recurse the parent to
1115 * get to the actual device driver.
1117 bus = device_get_parent(sc->dev); /* smbus */
1119 /* Allocate the bus. */
1120 if ((error = smbus_request_bus(bus, sc->dev,
1121 (ap->a_fflag & O_NONBLOCK) ?
1122 SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
1125 switch (ap->a_cmd) {
1128 error = inputev_ioctl(&sc->iev, ap->a_cmd, ap->a_data);
1134 smbus_release_bus(bus, sc->dev);
1140 * MAJOR SUPPORT FUNCTIONS
1144 cyapa_poll_thread(void *arg)
1146 struct cyapa_softc *sc = arg;
1147 struct cyapa_regs regs;
1148 device_t bus; /* smbbus */
1150 int freq = cyapa_norm_freq;
1153 bus = device_get_parent(sc->dev);
1155 while ((sc->poll_flags & CYPOLL_SHUTDOWN) == 0) {
1156 error = smbus_request_bus(bus, sc->dev, SMB_WAIT);
1158 error = smbus_trans(bus, sc->addr, CMD_DEV_STATUS,
1159 SMB_TRANS_NOCNT | SMB_TRANS_7BIT,
1161 (void *)®s, sizeof(regs), NULL);
1163 isidle = cyapa_raw_input(sc, ®s);
1165 smbus_release_bus(bus, sc->dev);
1167 tsleep(&sc->poll_flags, 0, "cyapw", (hz + freq - 1) / freq);
1169 freq = cyapa_slow_freq;
1171 freq = cyapa_idle_freq;
1173 freq = cyapa_norm_freq;
1176 wakeup(&sc->poll_td);
1181 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs)
1192 uint16_t but; /* high bits used for simulated but4/but5 */
1194 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1197 kprintf("stat %02x buttons %c%c%c nfngrs=%d ",
1199 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1200 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'L' : '-'),
1201 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'L' : '-'),
1205 for (i = 0; i < nfingers; ++i) {
1207 kprintf(" [x=%04d y=%04d p=%d]",
1208 CYAPA_TOUCH_X(regs, i),
1209 CYAPA_TOUCH_Y(regs, i),
1210 CYAPA_TOUCH_P(regs, i));
1213 inputev_mt_slot(&sc->iev, regs->touch[i].id - 1);
1214 inputev_mt_report_slot_state(&sc->iev, MT_TOOL_FINGER, 1);
1215 inputev_report_abs(&sc->iev, ABS_MT_POSITION_X,
1216 CYAPA_TOUCH_X(regs, i));
1217 inputev_report_abs(&sc->iev, ABS_MT_POSITION_Y,
1218 CYAPA_TOUCH_Y(regs, i));
1219 inputev_report_abs(&sc->iev, ABS_MT_PRESSURE,
1220 CYAPA_TOUCH_P(regs, i));
1224 inputev_mt_sync_frame(&sc->iev);
1226 if (sc->cap_buttons & CYAPA_FNGR_LEFT)
1227 inputev_report_key(&sc->iev, BTN_LEFT,
1228 regs->fngr & CYAPA_FNGR_LEFT);
1229 if (sc->cap_buttons & CYAPA_FNGR_MIDDLE)
1230 inputev_report_key(&sc->iev, BTN_LEFT,
1231 regs->fngr & CYAPA_FNGR_MIDDLE);
1232 if (sc->cap_buttons & CYAPA_FNGR_RIGHT)
1233 inputev_report_key(&sc->iev, BTN_LEFT,
1234 regs->fngr & CYAPA_FNGR_RIGHT);
1237 * Tracking for local solutions
1240 if (nfingers == 0) {
1258 * The id assigned on touch can move around in the array,
1259 * find it. If that finger is lifted up, assign some other
1260 * finger for mouse tracking and reset track_x and track_y
1261 * to avoid a mouse jump.
1263 * If >= 2 fingers are down be sure not to assign i and
1264 * j to the same index.
1266 for (i = 0; i < nfingers; ++i) {
1267 if (sc->track_id1 == regs->touch[i].id)
1270 if (i == nfingers) {
1275 sc->track_id1 = regs->touch[i].id;
1276 if (sc->track_id2 == sc->track_id1)
1283 for (j = 0; j < nfingers; ++j) {
1284 if (sc->track_id2 == regs->touch[j].id)
1287 if (j == nfingers) {
1288 if (nfingers >= 2) {
1293 sc->track_id2 = regs->touch[j].id;
1301 * The third finger is used to tap or tap-hold to simulate
1302 * a button, we don't have to record it persistently.
1304 if (nfingers >= 3) {
1306 if (i == 0 || j == 0)
1308 if (i == 1 || j == 1)
1313 kprintf("ID %3d,%3d,%3d\t%3d,%3d,%3d\n",
1316 ((nfingers >= 3) ? regs->touch[k].id : -1),
1317 CYAPA_TOUCH_X(regs, i),
1318 CYAPA_TOUCH_X(regs, j),
1319 CYAPA_TOUCH_X(regs, k)
1324 * On initial touch determine if we are in the slider area. Setting
1325 * track_z conditionalizes the delta calculations later on.
1327 if (nfingers && sc->zenabled > 0 &&
1328 sc->track_x == -1 && sc->track_z == -1) {
1329 x = CYAPA_TOUCH_X(regs, i);
1330 z = CYAPA_TOUCH_Y(regs, i);
1331 if (x > sc->cap_resx * 9 / 10)
1335 if (nfingers && sc->track_z != -1) {
1337 * Slider emulation (right side of trackpad). Z is tracked
1338 * based on the Y position. X and Y tracking are disabled.
1340 * Because we are emulating a mouse-wheel, we do not want
1341 * to shove events out at the maximum resolution.
1343 z = CYAPA_TOUCH_Y(regs, i);
1344 sc->delta_z += z / ZSCALE - sc->track_z;
1345 if (sc->touch_z == -1)
1346 sc->touch_z = z; /* not used atm */
1347 sc->track_z = z / ZSCALE;
1348 } else if (nfingers) {
1350 * Normal pad position reporting (track_z is left -1)
1352 x = CYAPA_TOUCH_X(regs, i);
1353 y = CYAPA_TOUCH_Y(regs, i);
1354 if (sc->track_x != -1) {
1355 sc->delta_x += x - sc->track_x;
1356 sc->delta_y -= y - sc->track_y;
1357 if (sc->delta_x > sc->cap_resx)
1358 sc->delta_x = sc->cap_resx;
1359 if (sc->delta_x < -sc->cap_resx)
1360 sc->delta_x = -sc->cap_resx;
1361 if (sc->delta_y > sc->cap_resx)
1362 sc->delta_y = sc->cap_resy;
1363 if (sc->delta_y < -sc->cap_resy)
1364 sc->delta_y = -sc->cap_resy;
1366 if (sc->touch_x == -1) {
1373 if (nfingers >= 5 && sc->zenabled > 1 && sc->track_z < 0) {
1375 * Simulate the 5th button (when not in slider mode)
1377 but = SIMULATE_BUT5;
1378 } else if (nfingers >= 4 && sc->zenabled > 1 && sc->track_z < 0) {
1380 * Simulate the 4th button (when not in slider mode)
1382 but = SIMULATE_BUT4;
1383 } else if (nfingers >= 3 && sc->track_z < 0) {
1385 * Simulate the left, middle, or right button with 3
1386 * fingers when not in slider mode.
1388 * This makes it ultra easy to hit GUI buttons and move
1389 * windows with a light touch, without having to apply the
1390 * pressure required to articulate the button.
1392 * However, if we are coming down from 4 or 5 fingers,
1393 * do NOT simulate the left button and instead just release
1394 * button 4 or button 5. Leave SIMULATE_LOCK set to
1395 * placemark the condition. We must go down to 2 fingers
1396 * to release the lock.
1398 * LEFT BUTTON: Fingers arranged left-to-right 1 2 3,
1399 * move mouse with fingers 2 and 3 and tap
1400 * or hold with finger 1 (to the left of fingers
1403 * RIGHT BUTTON: Move mouse with fingers 1 and 2 and tap
1404 * or hold with finger 3.
1406 * MIDDLE BUTTON: Move mouse with fingers 1 and 3 and tap
1407 * or hold with finger 2.
1409 x1 = CYAPA_TOUCH_X(regs, i); /* 1st finger down */
1410 x2 = CYAPA_TOUCH_X(regs, j); /* 2nd finger down */
1411 x = CYAPA_TOUCH_X(regs, k); /* 3rd finger (button) down */
1412 if (sc->track_but & (SIMULATE_BUT4 |
1415 but = SIMULATE_LOCK;
1416 } else if (sc->track_but & ~SIMULATE_LOCK) {
1417 but = sc->track_but;
1418 } else if (x < x1 && x < x2) {
1419 but = CYAPA_FNGR_LEFT;
1420 } else if (x > x1 && x < x2) {
1421 but = CYAPA_FNGR_MIDDLE;
1422 } else if (x > x2 && x < x1) {
1423 but = CYAPA_FNGR_MIDDLE;
1425 but = CYAPA_FNGR_RIGHT;
1427 } else if (nfingers == 2 || (nfingers >= 2 && sc->track_z >= 0)) {
1429 * If 2 fingers are held down or 2 or more fingers are held
1430 * down and we are in slider mode, any key press is
1431 * interpreted as a left mouse button press.
1433 * If a keypress is already active we retain the active
1436 * The high-button state is unconditionally cleared with <= 2
1439 if (regs->fngr & CYAPA_FNGR_LEFT) {
1440 but = sc->track_but & ~SIMULATE_LOCK;
1442 but = CYAPA_FNGR_LEFT;
1446 } else if (nfingers == 1 &&
1447 (abs(sc->touch_x - sc->track_x) > 32 ||
1448 abs(sc->touch_y - sc->track_y) > 32)) {
1450 * When using one finger, any significant mouse movement
1451 * will lock you to the left mouse button if you push the
1452 * button, regardless of where you are on the pad.
1454 * If a keypress is already active we retain the active
1457 * The high-button state is unconditionally cleared with <= 2
1460 if (regs->fngr & CYAPA_FNGR_LEFT) {
1461 but = sc->track_but & ~SIMULATE_LOCK;
1463 but = CYAPA_FNGR_LEFT;
1467 } else if (nfingers == 1 && (regs->fngr & CYAPA_FNGR_LEFT)) {
1469 * If you are swiping while holding a button down, the
1470 * button registration does not change. Otherwise the
1471 * registered button depends on where you are on the pad.
1473 * Since no significant movement occurred we allow the
1474 * button to be pressed while within the slider area
1475 * and still be properly registered as the right button.
1477 * The high-button state is unconditionally cleared with <= 2
1480 if (sc->track_but & ~SIMULATE_LOCK)
1481 but = sc->track_but & ~SIMULATE_LOCK;
1482 else if (sc->track_x < sc->cap_resx * 1 / 3)
1483 but = CYAPA_FNGR_LEFT;
1484 else if (sc->track_x < sc->cap_resx * 2 / 3)
1485 but = CYAPA_FNGR_MIDDLE;
1487 but = CYAPA_FNGR_RIGHT;
1488 } else if (nfingers == 1) {
1490 * Clear all finger state if 1 finger is down and nothing
1496 * Clear all finger state if no fingers are down.
1500 sc->track_but = but;
1501 if (sc->delta_x || sc->delta_y || sc->delta_z ||
1502 sc->track_but != sc->reported_but) {
1503 if (sc->remote_mode == 0 && sc->reporting_mode)
1504 sc->data_signal = 1;
1519 * Returns non-zero if the fifo is empty
1523 fifo_empty(struct cyapa_fifo *fifo)
1525 return(fifo->rindex == fifo->windex);
1529 * Returns the number of characters available for reading from
1530 * the fifo without wrapping the fifo buffer.
1534 fifo_ready(struct cyapa_fifo *fifo)
1538 n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK);
1539 if (n > (size_t)(fifo->windex - fifo->rindex))
1540 n = (size_t)(fifo->windex - fifo->rindex);
1546 * Returns the number of characters available for reading from
1547 * the fifo including wrapping the fifo buffer.
1551 fifo_total_ready(struct cyapa_fifo *fifo)
1553 return ((size_t)(fifo->windex - fifo->rindex));
1558 * Returns a read pointer into the fifo and then bumps
1559 * rindex. The FIFO must have at least 'n' characters in
1560 * it. The value (n) can cause the index to wrap but users
1561 * of the buffer should never supply a value for (n) that wraps
1566 fifo_read(struct cyapa_fifo *fifo, size_t n)
1570 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1571 kprintf("fifo_read: overflow\n");
1574 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1582 fifo_read_char(struct cyapa_fifo *fifo)
1586 if (fifo->rindex == fifo->windex) {
1587 kprintf("fifo_read_char: overflow\n");
1590 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1598 * Write a character to the FIFO. The character will be discarded
1599 * if the FIFO is full.
1603 fifo_write_char(struct cyapa_fifo *fifo, uint8_t c)
1605 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1606 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1612 * Return the amount of space available for writing without wrapping
1617 fifo_space(struct cyapa_fifo *fifo)
1621 n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK);
1622 if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)))
1623 n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex));
1629 fifo_write(struct cyapa_fifo *fifo, size_t n)
1633 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1641 fifo_reset(struct cyapa_fifo *fifo)
1652 cyapa_fuzz(short delta, short *fuzzp)
1657 if (fuzz >= 0 && delta < 0) {
1660 } else if (fuzz <= 0 && delta > 0) {
1669 DRIVER_MODULE(cyapa, smbus, cyapa_driver, cyapa_devclass, NULL, NULL);
1670 MODULE_DEPEND(cyapa, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
1671 MODULE_VERSION(cyapa, 1);