From owner-svn-src-head@freebsd.org Sat Jul 25 18:14:37 2015 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 495609AB932; Sat, 25 Jul 2015 18:14:37 +0000 (UTC) (envelope-from grembo@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 37098C35; Sat, 25 Jul 2015 18:14:37 +0000 (UTC) (envelope-from grembo@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.14.9/8.14.9) with ESMTP id t6PIEb2n067094; Sat, 25 Jul 2015 18:14:37 GMT (envelope-from grembo@FreeBSD.org) Received: (from grembo@localhost) by repo.freebsd.org (8.14.9/8.14.9/Submit) id t6PIEaiX067090; Sat, 25 Jul 2015 18:14:36 GMT (envelope-from grembo@FreeBSD.org) Message-Id: <201507251814.t6PIEaiX067090@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: grembo set sender to grembo@FreeBSD.org using -f From: Michael Gmelin Date: Sat, 25 Jul 2015 18:14:36 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r285876 - in head: share/man/man4 sys/dev/cyapa sys/modules/i2c sys/modules/i2c/cyapa X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Jul 2015 18:14:37 -0000 Author: grembo (ports committer) Date: Sat Jul 25 18:14:35 2015 New Revision: 285876 URL: https://svnweb.freebsd.org/changeset/base/285876 Log: cyapa(4), driver for the Cypress APA I2C trackpad Differential Revision: https://reviews.freebsd.org/D3068 Reviewed by: kib, wblock Approved by: kib Relnotes: yes Added: head/share/man/man4/cyapa.4 (contents, props changed) head/sys/dev/cyapa/ head/sys/dev/cyapa/cyapa.c (contents, props changed) head/sys/dev/cyapa/cyapa.h (contents, props changed) head/sys/modules/i2c/cyapa/ head/sys/modules/i2c/cyapa/Makefile (contents, props changed) Modified: head/share/man/man4/Makefile head/sys/modules/i2c/Makefile Modified: head/share/man/man4/Makefile ============================================================================== --- head/share/man/man4/Makefile Sat Jul 25 16:20:04 2015 (r285875) +++ head/share/man/man4/Makefile Sat Jul 25 18:14:35 2015 (r285876) @@ -107,6 +107,7 @@ MAN= aac.4 \ cxgb.4 \ cxgbe.4 \ cy.4 \ + cyapa.4 \ da.4 \ dc.4 \ dcons.4 \ Added: head/share/man/man4/cyapa.4 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/share/man/man4/cyapa.4 Sat Jul 25 18:14:35 2015 (r285876) @@ -0,0 +1,200 @@ +.\" Copyright (c) 2015 Michael Gmelin +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd July 25, 2015 +.Dt CYAPA 4 +.Os +.Sh NAME +.Nm cyapa +.Nd Cypress APA trackpad with I2C interface driver +.Sh SYNOPSIS +To compile this driver into the kernel, place the following lines into +the kernel configuration file: +.Bd -ragged -offset indent +.Cd "device cyapa" +.Cd "device ig4" +.Cd "device smbus" +.Ed +.Pp +Alternatively, to load the driver as a module at boot time, place the following line in +.Xr loader.conf 5 : +.Bd -literal -offset indent +cyapa_load="YES" +ig4_load="YES" +.Ed +.Sh DESCRIPTION +The +.Nm +driver provides support for the Cypress APA trackpad. +It emulates the IntelliMouse PS/2 protocol. +It supports basic mouse ioctls, so that +.Xr moused 8 +is supported properly. +.Ss Trackpad layout +.Bd -literal + 2/3 1/3 + +--------------------+------------+ + | | Middle | + | | Button | + | Left | | + | Button +------------+ + | | Right | + | | Button | + +--------------------+............| + | Thumb/Button Area | 15% + +---------------------------------+ +.Ed +.Ss Trackpad features +.Bl -tag -width 8n +.It Va Two finger scrolling +Use two fingers for Z axis scrolling. +.It Va Button down/second finger +While one finger clicks and holds down the touchpad, the second finger can be +used to move the mouse cursor. +This can be useful for drawing or selecting text. +.It Va Thumb/Button area +The lower 15% of the trackpad will not affect the mouse cursor position. +This allows for high precision clicking, by controlling the cursor with the +index finger and pushing/holding the pad down with the thumb. +.It Va Trackpad button +Push physical button. +The left two thirds of the pad issues a LEFT button event. +The upper right corner issues a MIDDLE button event. +The lower right corner issues a RIGHT button. +Optionally, tap to click can be enabled (see below). +.El +.Sh SYSCTL VARIABLES +These +.Xr sysctl 8 +variables are available: +.Bl -tag -width 8n +.It Va debug.cyapa_idle_freq +Scan frequency in idle mode, the default is 1. +.It Va debug.cyapa_slow_freq +Scan frequency in slow mode, the default is 20. +.It Va debug.cyapa_norm_freq +Scan frequency in normal mode, the default is 100. +.It Va debug.cyapa_minpressure +Minimum pressure to detect a finger, the default is 12. +.It Va debug.cyapa_enable_tapclick +Controls tap to click. +Possible values: +.Bl -tag -width 8n +.It 0 +Tap to click is disabled. +This is the default value. +.It 1 +Tap to click always generates a left mouse button event. +.It 2 +Tap to click generates left mouse button event if the left 2/3rds of the pad +are tapped and a right mouse button event otherwise. +.It 3 +Tap to click generates mouse button events as if the physical button was +pressed (see +.Sx DESCRIPTION +above). +.El +.It Va debug.cyapa_tapclick_min_ticks +Minimum tap duration in ticks to create a click, the default is 1. +.It Va debug.cyapa_tapclick_max_ticks +Maximum tap duration in ticks to create a click, the default is 8. +.It Va debug.cyapa_move_min_ticks +Minimum ticks before cursor movement occurs, the default is 4. +.It Va debug.cyapa_scroll_wait_ticks +Ticks to wait before starting to scroll, the default is 0. +.It Va debug.cyapa_scroll_stick_ticks +Ticks while preventing cursor movement on single finger after scroll, +the default is 15. +.It Va debug.cyapa_thumbarea_percent +Size of bottom thumb area in percent, the default is 15. +.It Va debug.cyapa_debug +Setting this to a non-zero value enables debug output to console and syslog, +the default is 0. +.It Va debug.cyapa_reset +Setting this to a non-zero value reinitializes the device. +The sysctl resets to zero immediately. +.El +.Sh FILES +.Nm +creates +.Pa /dev/cyapa0 , +which presents the mouse as an +.Ar IntelliMouse PS/2 +device. +It supports +.Xr moused 8 +levels 0 through 2, level 1 is used by default. +.Sh EXAMPLES +To use +.Nm +with +.Xr moused 8 , +add the following lines to the +.Xr rc.conf 5 +file: +.Pp +.Dl moused_enable="YES" +.Dl moused_port="/dev/cyapa0" +.Pp +If vertical scrolling is not desired, add +.Pp +.Dl moused_flags="-l0" +.Pp +to +.Xr rc.conf 5 . +.Pp +Enable tap to click for the left and the right mouse button and +disable the thumb area by adding these lines to the +.Xr sysctl.conf 5 +file: +.Pp +.Dl debug.cyapa_thumbarea_percent=0 +.Dl debug.cyapa_enable_tapclick=2 +.Sh SEE ALSO +.Xr ig4 4 , +.Xr moused 4 , +.Xr smbus 4 , +.Xr sysmouse 4 +.Sh AUTHORS +.An -nosplit +The original +.Nm +driver was written for DragonFly BSD by +.An Matthew Dillon . +.Pp +It has been ported, modified, and enhanced for +.Fx +by +.An Michael Gmelin Aq Mt freebsd@grem.de . +.Pp +This manual page was written by +.An Michael Gmelin Aq Mt freebsd@grem.de . +.Sh BUGS +The +.Nm +driver detects the device based on its I2C address (0x67). +This might have unforeseen consequences if the initialization sequence +is sent to an unknown device at that address. Added: head/sys/dev/cyapa/cyapa.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/cyapa/cyapa.c Sat Jul 25 18:14:35 2015 (r285876) @@ -0,0 +1,1707 @@ +/* + * Copyright (c) 2014 The DragonFly Project. All rights reserved. + * + * This code is derived from software contributed to The DragonFly Project + * by Matthew Dillon and was subsequently ported, + * modified and enhanced for FreeBSD by Michael Gmelin . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name of The DragonFly Project nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific, prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +/* + * CYAPA - Cypress APA trackpad with I2C Interface driver + * + * Based on DragonFlyBSD's cyapa driver, which referenced the linux + * cyapa.c driver to figure out the bootstrapping and commands. + * + * Unable to locate any datasheet for the device. + * + * + * Trackpad layout: + * + * 2/3 1/3 + * +--------------------+------------+ + * | | Middle | + * | | Button | + * | Left | | + * | Button +------------+ + * | | Right | + * | | Button | + * +--------------------+............| + * | Thumb/Button Area | 15% + * +---------------------------------+ + * + * + * FEATURES + * + * IMPS/2 emulation - Emulates the IntelliMouse protocol. + * + * Jitter supression - Implements 2-pixel hysteresis with memory. + * + * Jump detecion - Detect jumps caused by touchpad. + * + * Two finger scrolling - Use two fingers for Z axis scrolling. + * + * Button down/2nd finger - While one finger clicks and holds down the + * touchpad, the second one can be used to move + * the mouse cursor. Useful for drawing or + * selecting text. + * + * Thumb/Button Area - The lower 15%* of the trackpad will not affect + * the mouse cursor position. This allows for high + * precision clicking, by controlling the cursor + * with the index finger and pushing/holding the + * pad down with the thumb. + * * can be changed using sysctl + * + * Track-pad button - Push physical button. Left 2/3rds of the pad + * will issue a LEFT button event, upper right + * corner will issue a MIDDLE button event, + * lower right corner will issue a RIGHT button + * event. Optional tap support can be enabled + * and configured using sysctl. + * + * WARNINGS + * + * These trackpads get confused when three or more fingers are down on the + * same horizontal axis and will start to glitch the finger detection. + * Removing your hand for a few seconds will allow the trackpad to + * recalibrate. Generally speaking, when using three or more fingers + * please try to place at least one finger off-axis (a little above or + * below) the other two. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "smbus_if.h" +#include "bus_if.h" +#include "device_if.h" + +#define CYAPA_BUFSIZE 128 /* power of 2 */ +#define CYAPA_BUFMASK (CYAPA_BUFSIZE - 1) + +#define ZSCALE 15 + +#define TIME_TO_IDLE (hz * 10) +#define TIME_TO_RESET (hz * 3) + +static MALLOC_DEFINE(M_CYAPA, "cyapa", "CYAPA device data"); + +struct cyapa_fifo { + int rindex; + int windex; + char buf[CYAPA_BUFSIZE]; +}; + +struct cyapa_softc { + device_t dev; + int count; /* >0 if device opened */ + int unit; + int addr; + struct cdev *devnode; + struct selinfo selinfo; + struct mtx mutex; + + int cap_resx; + int cap_resy; + int cap_phyx; + int cap_phyy; + uint8_t cap_buttons; + + int detaching; /* driver is detaching */ + int poll_thread_running; /* poll thread is running */ + + /* PS/2 mouse emulation */ + int track_x; /* current tracking */ + int track_y; + int track_z; + int track_z_ticks; + uint16_t track_but; + char track_id; /* first finger id */ + int track_nfingers; + int delta_x; /* accumulation -> report */ + int delta_y; + int delta_z; + int fuzz_x; + int fuzz_y; + int fuzz_z; + int touch_x; /* touch down coordinates */ + int touch_y; + int touch_z; + int finger1_ticks; + int finger2_ticks; + int finger3_ticks; + uint16_t reported_but; + + struct cyapa_fifo rfifo; /* device->host */ + struct cyapa_fifo wfifo; /* host->device */ + uint8_t ps2_cmd; /* active p2_cmd waiting for data */ + uint8_t ps2_acked; + int active_tick; + int data_signal; + int blocked; + int isselect; + int reporting_mode; /* 0=disabled 1=enabled */ + int scaling_mode; /* 0=1:1 1=2:1 */ + int remote_mode; /* 0 for streaming mode */ + int zenabled; /* z-axis enabled (mode 1 or 2) */ + mousehw_t hw; /* hardware information */ + mousemode_t mode; /* mode */ + int poll_ticks; +}; + +struct cyapa_cdevpriv { + struct cyapa_softc *sc; +}; + +#define CYPOLL_SHUTDOWN 0x0001 + +static void cyapa_poll_thread(void *arg); +static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, + int freq); +static void cyapa_set_power_mode(struct cyapa_softc *sc, int mode); + +static int fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo); +static size_t fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo); +static char *fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, + size_t n); +static char *fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, + size_t n); +static uint8_t fifo_read_char(struct cyapa_softc *sc, + struct cyapa_fifo *fifo); +static void fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, + uint8_t c); +static size_t fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo); +static void fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo); + +static int cyapa_fuzz(int delta, int *fuzz); + +static int cyapa_idle_freq = 1; +SYSCTL_INT(_debug, OID_AUTO, cyapa_idle_freq, CTLFLAG_RW, + &cyapa_idle_freq, 0, "Scan frequency in idle mode"); +static int cyapa_slow_freq = 20; +SYSCTL_INT(_debug, OID_AUTO, cyapa_slow_freq, CTLFLAG_RW, + &cyapa_slow_freq, 0, "Scan frequency in slow mode "); +static int cyapa_norm_freq = 100; +SYSCTL_INT(_debug, OID_AUTO, cyapa_norm_freq, CTLFLAG_RW, + &cyapa_norm_freq, 0, "Normal scan frequency"); +static int cyapa_minpressure = 12; +SYSCTL_INT(_debug, OID_AUTO, cyapa_minpressure, CTLFLAG_RW, + &cyapa_minpressure, 0, "Minimum pressure to detect finger"); +static int cyapa_enable_tapclick = 0; +SYSCTL_INT(_debug, OID_AUTO, cyapa_enable_tapclick, CTLFLAG_RW, + &cyapa_enable_tapclick, 0, "Enable tap to click"); +static int cyapa_tapclick_min_ticks = 1; +SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_min_ticks, CTLFLAG_RW, + &cyapa_tapclick_min_ticks, 0, "Minimum tap duration for click"); +static int cyapa_tapclick_max_ticks = 8; +SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_max_ticks, CTLFLAG_RW, + &cyapa_tapclick_max_ticks, 0, "Maximum tap duration for click"); +static int cyapa_move_min_ticks = 4; +SYSCTL_INT(_debug, OID_AUTO, cyapa_move_min_ticks, CTLFLAG_RW, + &cyapa_move_min_ticks, 0, + "Minimum ticks before cursor position is changed"); +static int cyapa_scroll_wait_ticks = 0; +SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_wait_ticks, CTLFLAG_RW, + &cyapa_scroll_wait_ticks, 0, + "Wait N ticks before starting to scroll"); +static int cyapa_scroll_stick_ticks = 15; +SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_stick_ticks, CTLFLAG_RW, + &cyapa_scroll_stick_ticks, 0, + "Prevent cursor move on single finger for N ticks after scroll"); +static int cyapa_thumbarea_percent = 15; +SYSCTL_INT(_debug, OID_AUTO, cyapa_thumbarea_percent, CTLFLAG_RW, + &cyapa_thumbarea_percent, 0, + "Size of bottom thumb area in percent"); + +static int cyapa_debug = 0; +SYSCTL_INT(_debug, OID_AUTO, cyapa_debug, CTLFLAG_RW, + &cyapa_debug, 0, "Enable debugging"); +static int cyapa_reset = 0; +SYSCTL_INT(_debug, OID_AUTO, cyapa_reset, CTLFLAG_RW, + &cyapa_reset, 0, "Reset track pad"); + +static void +cyapa_lock(struct cyapa_softc *sc) +{ + + mtx_lock(&sc->mutex); +} + +static void +cyapa_unlock(struct cyapa_softc *sc) +{ + + mtx_unlock(&sc->mutex); +} + +#define CYAPA_LOCK_ASSERT(sc) mtx_assert(&(sc)->mutex, MA_OWNED); + +/* + * Notify if possible receive data ready. Must be called + * with sc->mutex held (cyapa_lock(sc)). + */ +static void +cyapa_notify(struct cyapa_softc *sc) +{ + + CYAPA_LOCK_ASSERT(sc); + + if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) { + KNOTE_LOCKED(&sc->selinfo.si_note, 0); + if (sc->blocked || sc->isselect) { + if (sc->blocked) { + sc->blocked = 0; + wakeup(&sc->blocked); + } + if (sc->isselect) { + sc->isselect = 0; + selwakeup(&sc->selinfo); + } + } + } +} + +/* + * Initialize the device + */ +static int +init_device(device_t dev, struct cyapa_cap *cap, int addr, int probe) +{ + static char bl_exit[] = { + 0x00, 0xff, 0xa5, 0x00, 0x01, + 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; + static char bl_deactivate[] = { + 0x00, 0xff, 0x3b, 0x00, 0x01, + 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; + device_t bus; + struct cyapa_boot_regs boot; + int error; + int retries; + + bus = device_get_parent(dev); /* smbus */ + + /* Get status */ + error = smbus_trans(bus, addr, CMD_BOOT_STATUS, + SMB_TRANS_NOCNT | SMB_TRANS_7BIT, + NULL, 0, (void *)&boot, sizeof(boot), NULL); + if (error) + goto done; + + /* + * Bootstrap the device if necessary. It can take up to 2 seconds + * for the device to fully initialize. + */ + retries = 20; + while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) { + if (boot.boot & CYAPA_BOOT_BUSY) { + /* Busy, wait loop. */ + } else if (boot.error & CYAPA_ERROR_BOOTLOADER) { + /* Magic */ + error = smbus_trans(bus, addr, CMD_BOOT_STATUS, + SMB_TRANS_NOCNT | SMB_TRANS_7BIT, + bl_deactivate, sizeof(bl_deactivate), + NULL, 0, NULL); + if (error) + goto done; + } else { + /* Magic */ + error = smbus_trans(bus, addr, CMD_BOOT_STATUS, + SMB_TRANS_NOCNT | SMB_TRANS_7BIT, + bl_exit, sizeof(bl_exit), NULL, 0, NULL); + if (error) + goto done; + } + pause("cyapab1", (hz * 2) / 10); + --retries; + error = smbus_trans(bus, addr, CMD_BOOT_STATUS, + SMB_TRANS_NOCNT | SMB_TRANS_7BIT, + NULL, 0, (void *)&boot, sizeof(boot), NULL); + if (error) + goto done; + } + + if (retries == 0) { + device_printf(dev, "Unable to bring device out of bootstrap\n"); + error = ENXIO; + goto done; + } + + /* Check identity */ + if (cap) { + error = smbus_trans(bus, addr, CMD_QUERY_CAPABILITIES, + SMB_TRANS_NOCNT | SMB_TRANS_7BIT, + NULL, 0, (void *)cap, sizeof(*cap), NULL); + + if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) { + device_printf(dev, "Product ID \"%5.5s\" mismatch\n", + cap->prod_ida); + error = ENXIO; + } + } + error = smbus_trans(bus, addr, CMD_BOOT_STATUS, + SMB_TRANS_NOCNT | SMB_TRANS_7BIT, + NULL, 0, (void *)&boot, sizeof(boot), NULL); + + if (probe == 0) /* official init */ + device_printf(dev, "cyapa init status %02x\n", boot.stat); + else if (probe == 2) + device_printf(dev, "cyapa reset status %02x\n", boot.stat); + +done: + if (error) + device_printf(dev, "Unable to initialize\n"); + return (error); +} + +static int cyapa_probe(device_t); +static int cyapa_attach(device_t); +static int cyapa_detach(device_t); +static void cyapa_cdevpriv_dtor(void*); + +static devclass_t cyapa_devclass; + +static device_method_t cyapa_methods[] = { + /* device interface */ + DEVMETHOD(device_probe, cyapa_probe), + DEVMETHOD(device_attach, cyapa_attach), + DEVMETHOD(device_detach, cyapa_detach), + + DEVMETHOD_END +}; + +static driver_t cyapa_driver = { + "cyapa", + cyapa_methods, + sizeof(struct cyapa_softc), +}; + +static d_open_t cyapaopen; +static d_ioctl_t cyapaioctl; +static d_read_t cyaparead; +static d_write_t cyapawrite; +static d_kqfilter_t cyapakqfilter; +static d_poll_t cyapapoll; + +static struct cdevsw cyapa_cdevsw = { + .d_version = D_VERSION, + .d_open = cyapaopen, + .d_ioctl = cyapaioctl, + .d_read = cyaparead, + .d_write = cyapawrite, + .d_kqfilter = cyapakqfilter, + .d_poll = cyapapoll, +}; + +static int +cyapa_probe(device_t dev) +{ + struct cyapa_cap cap; + int unit; + int addr; + int error; + + addr = smbus_get_addr(dev); + + /* + * 0x67 - cypress trackpad on the acer c720 + * (other devices might use other ids). + */ + if (addr != 0x67) + return (ENXIO); + + unit = device_get_unit(dev); + + error = init_device(dev, &cap, addr, 1); + if (error != 0) + return (ENXIO); + + device_set_desc(dev, "Cypress APA I2C Trackpad"); + + return (BUS_PROBE_VENDOR); +} + +static int +cyapa_attach(device_t dev) +{ + struct cyapa_softc *sc; + struct cyapa_cap cap; + int unit; + int addr; + + sc = device_get_softc(dev); + sc->reporting_mode = 1; + + unit = device_get_unit(dev); + addr = *((unsigned char*) device_get_ivars(dev)); + + if (init_device(dev, &cap, addr, 0)) + return (ENXIO); + + mtx_init(&sc->mutex, "cyapa", NULL, MTX_DEF); + + sc->dev = dev; + sc->unit = unit; + sc->addr = addr; + + knlist_init_mtx(&sc->selinfo.si_note, &sc->mutex); + + sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) | + cap.max_abs_x_low; + sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) | + cap.max_abs_y_low; + sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) | + cap.phy_siz_x_low; + sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) | + cap.phy_siz_y_low; + sc->cap_buttons = cap.buttons; + + device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n", + cap.prod_ida, cap.prod_idb, cap.prod_idc, + ((cap.buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'), + ((cap.buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'), + ((cap.buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'), + sc->cap_resx, sc->cap_resy); + + sc->hw.buttons = 5; + sc->hw.iftype = MOUSE_IF_PS2; + sc->hw.type = MOUSE_MOUSE; + sc->hw.model = MOUSE_MODEL_INTELLI; + sc->hw.hwid = addr; + + sc->mode.protocol = MOUSE_PROTO_PS2; + sc->mode.rate = 100; + sc->mode.resolution = 4; + sc->mode.accelfactor = 1; + sc->mode.level = 0; + sc->mode.packetsize = MOUSE_PS2_PACKETSIZE; + + /* Setup input event tracking */ + cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE); + + /* Start the polling thread */ + kthread_add(cyapa_poll_thread, sc, NULL, NULL, + 0, 0, "cyapa-poll"); + + sc->devnode = make_dev(&cyapa_cdevsw, unit, + UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit); + + sc->devnode->si_drv1 = sc; + + return (0); +} + +static int +cyapa_detach(device_t dev) +{ + struct cyapa_softc *sc; + + sc = device_get_softc(dev); + + /* Cleanup poller thread */ + cyapa_lock(sc); + while (sc->poll_thread_running) { + sc->detaching = 1; + mtx_sleep(&sc->detaching, &sc->mutex, PCATCH, "cyapadet", hz); + } + cyapa_unlock(sc); + + destroy_dev(sc->devnode); + + knlist_clear(&sc->selinfo.si_note, 0); + seldrain(&sc->selinfo); + knlist_destroy(&sc->selinfo.si_note); + + mtx_destroy(&sc->mutex); + + return (0); +} + +/* + * USER DEVICE I/O FUNCTIONS + */ +static int +cyapaopen(struct cdev *dev, int oflags, int devtype, struct thread *td) +{ + struct cyapa_cdevpriv *priv; + int error; + + priv = malloc(sizeof(*priv), M_CYAPA, M_WAITOK | M_ZERO); + priv->sc = dev->si_drv1; + + error = devfs_set_cdevpriv(priv, cyapa_cdevpriv_dtor); + if (error == 0) { + cyapa_lock(priv->sc); + priv->sc->count++; + cyapa_unlock(priv->sc); + } + else + free(priv, M_CYAPA); + + return (error); +} + +static void +cyapa_cdevpriv_dtor(void *data) +{ + struct cyapa_cdevpriv *priv; + + priv = data; + KASSERT(priv != NULL, ("cyapa cdevpriv should not be NULL!")); + + cyapa_lock(priv->sc); + priv->sc->count--; + cyapa_unlock(priv->sc); + + free(priv, M_CYAPA); +} + +static int +cyaparead(struct cdev *dev, struct uio *uio, int ioflag) +{ + struct cyapa_softc *sc; + int error; + int didread; + size_t n; + char* ptr; + + sc = dev->si_drv1; + /* If buffer is empty, load a new event if it is ready */ + cyapa_lock(sc); +again: + if (fifo_empty(sc, &sc->rfifo) && + (sc->data_signal || sc->delta_x || sc->delta_y || + sc->track_but != sc->reported_but)) { + uint8_t c0; + uint16_t but; + int delta_x; + int delta_y; + int delta_z; + + /* Accumulate delta_x, delta_y */ + sc->data_signal = 0; + delta_x = sc->delta_x; + delta_y = sc->delta_y; + delta_z = sc->delta_z; + if (delta_x > 255) { + delta_x = 255; + sc->data_signal = 1; + } + if (delta_x < -256) { + delta_x = -256; + sc->data_signal = 1; + } + if (delta_y > 255) { + delta_y = 255; + sc->data_signal = 1; + } + if (delta_y < -256) { + delta_y = -256; + sc->data_signal = 1; + } + if (delta_z > 255) { + delta_z = 255; + sc->data_signal = 1; + } + if (delta_z < -256) { + delta_z = -256; + sc->data_signal = 1; + } + but = sc->track_but; + + /* Adjust baseline for next calculation */ + sc->delta_x -= delta_x; + sc->delta_y -= delta_y; + sc->delta_z -= delta_z; + sc->reported_but = but; + + /* + * Fuzz reduces movement jitter by introducing some + * hysteresis. It operates without cumulative error so + * if you swish around quickly and return your finger to + * where it started, so to will the mouse. + */ + delta_x = cyapa_fuzz(delta_x, &sc->fuzz_x); + delta_y = cyapa_fuzz(delta_y, &sc->fuzz_y); + delta_z = cyapa_fuzz(delta_z, &sc->fuzz_z); + + /* + * Generate report + */ + c0 = 0; + if (delta_x < 0) + c0 |= 0x10; + if (delta_y < 0) + c0 |= 0x20; + c0 |= 0x08; + if (but & CYAPA_FNGR_LEFT) + c0 |= 0x01; + if (but & CYAPA_FNGR_MIDDLE) + c0 |= 0x04; + if (but & CYAPA_FNGR_RIGHT) + c0 |= 0x02; + + fifo_write_char(sc, &sc->rfifo, c0); + fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_x); + fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_y); + switch(sc->zenabled) { + case 1: + /* Z axis all 8 bits */ + fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_z); + break; + case 2: + /* + * Z axis low 4 bits + 4th button and 5th button + * (high 2 bits must be left 0). Auto-scale + * delta_z to fit to avoid a wrong-direction + * overflow (don't try to retain the remainder). + */ + while (delta_z > 7 || delta_z < -8) + delta_z >>= 1; + c0 = (uint8_t)delta_z & 0x0F; + fifo_write_char(sc, &sc->rfifo, c0); + break; + default: + /* basic PS/2 */ + break; + } + cyapa_notify(sc); + } + + /* Blocking / Non-blocking */ + error = 0; + didread = (uio->uio_resid == 0); + + while ((ioflag & IO_NDELAY) == 0 && fifo_empty(sc, &sc->rfifo)) { + if (sc->data_signal) + goto again; + sc->blocked = 1; + error = mtx_sleep(&sc->blocked, &sc->mutex, PCATCH, "cyablk", 0); + if (error) + break; + } + + /* Return any buffered data */ + while (error == 0 && uio->uio_resid && + (n = fifo_ready(sc, &sc->rfifo)) > 0) { + if (n > uio->uio_resid) + n = uio->uio_resid; + ptr = fifo_read(sc, &sc->rfifo, 0); + cyapa_unlock(sc); + error = uiomove(ptr, n, uio); + cyapa_lock(sc); + if (error) + break; + fifo_read(sc, &sc->rfifo, n); + didread = 1; + } + cyapa_unlock(sc); + + if (error == 0 && didread == 0) { + error = EWOULDBLOCK; + } + return (didread ? 0 : error); +} + +static int +cyapawrite(struct cdev *dev, struct uio *uio, int ioflag) +{ + struct cyapa_softc *sc; + int error; + int cmd_completed; + size_t n; + uint8_t c0; + char* ptr; + + sc = dev->si_drv1; +again: + /* + * Copy data from userland. This will also cross-over the end + * of the fifo and keep filling. + */ + cyapa_lock(sc); + while ((n = fifo_space(sc, &sc->wfifo)) > 0 && uio->uio_resid) { + if (n > uio->uio_resid) + n = uio->uio_resid; + ptr = fifo_write(sc, &sc->wfifo, 0); + cyapa_unlock(sc); + error = uiomove(ptr, n, uio); + cyapa_lock(sc); + if (error) + break; *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***