/*-
* Copyright (c) 2011-2014 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* uatp(4) - USB Apple Trackpad
*
* The uatp driver talks the protocol of the USB trackpads found in
* Apple laptops since 2005, including PowerBooks, iBooks, MacBooks,
* and MacBook Pros. Some of these also present generic USB HID mice
* on another USB report id, which the ums(4) driver can handle, but
* Apple's protocol gives more detailed sensor data that lets us detect
* multiple fingers to emulate multi-button mice and scroll wheels.
*/
/*
* Protocol
*
* The device has a set of horizontal sensors, each being a column at a
* particular position on the x axis that tells you whether there is
* pressure anywhere on that column, and vertical sensors, each being a
* row at a particular position on the y axis that tells you whether
* there is pressure anywhere on that row.
*
* Whenever the device senses anything, it emits a readout of all of
* the sensors, in some model-dependent order. (For the order, see
* read_sample_1 and read_sample_2.) Each sensor datum is an unsigned
* eight-bit quantity representing some measure of pressure. (Of
* course, it really measures capacitance, not pressure, but we'll call
* it `pressure' here.)
*/
/*
* Interpretation
*
* To interpret the finger's position on the trackpad, the driver
* computes a weighted average over all possible positions, weighted by
* the pressure at that position. The weighted average is computed in
* the dimensions of the screen, rather than the trackpad, in order to
* admit a finer resolution of positions than the trackpad grid.
*
* To update the finger's position smoothly on the trackpad, the driver
* computes a weighted average of the old raw position, the old
* smoothed position, and the new smoothed position. The weights are
* given by the old_raw_weight, old_smoothed_weight, and new_raw_weight
* sysctl knobs.
*
* Finally, to move the cursor, the driver takes the difference between
* the old and new positions and accelerates it according to some
* heuristic knobs that need to be reworked.
*
* Finally, there are some bells & whistles to detect tapping and to
* emulate a three-button mouse by leaving two or three fingers on the
* trackpad while pressing the button.
*/
/*
* Future work
*
* With the raw sensor data available, we could implement fancier bells
* & whistles too, such as pinch-to-zoom. However, wsmouse supports
* only four-dimensional mice with buttons, and we already use two
* dimensions for mousing and two dimensions for scrolling, so there's
* no straightforward way to report zooming and other gestures to the
* operating system. Probably a better way to do this would be just to
* attach uhid(4) instead of uatp(4) and to read the raw sensors data
* yourself -- but that requires hairy mode switching for recent models
* (see geyser34_enable_raw_mode).
*
* XXX Rework the acceleration knobs.
* XXX Implement edge scrolling.
* XXX Fix sysctl setup; preserve knobs across suspend/resume.
* (uatp0 detaches and reattaches across suspend/resume, so as
* written, the sysctl tree is torn down and rebuilt, losing any
* state the user may have set.)
* XXX Refactor motion state so I can understand it again.
* Should make a struct uatp_motion for all that state.
* XXX Add hooks for ignoring trackpad input while typing.
*/
/*
* Classifying devices
*
* I have only one MacBook to test this driver, but the driver should
* be applicable to almost every Apple laptop made since the beginning
* of 2005, so the driver reports lots of debugging output to help to
* classify devices. Boot with `boot -v' (verbose) and check the
* output of `dmesg | grep uatp' to answer the following questions:
*
* - What devices (vendor, product, class, subclass, proto, USB HID
* report dump) fail to attach when you think they should work?
* (vendor not apple, class not hid, proto not mouse)
*
* - What devices have an unknown product id?
* `unknown vendor/product id'
*
* - What devices have the wrong screen-to-trackpad ratios?
* `... x sensors, scaled by ... for ... points on screen'
* `... y sensors, scaled by ... for ... points on screen'
* You can tweak hw.uatp0.x_ratio and hw.uatp0.y_ratio to adjust
* this, up to a maximum of 384 for each value.
*
* - What devices have the wrong input size?
* `expected input size ... but got ... for Apple trackpad'
*
* - What devices give wrong-sized packets?
* `discarding ...-byte input'
*
* - What devices split packets in chunks?
* `partial packet: ... bytes'
*
* - What devices develop large sensor readouts?
* `large sensor readout: ...'
*
* - What devices have the wrong number of sensors? Are there parts of
* your trackpad that the system doesn't seem to notice? You can
* tweak hw.uatp0.x_sensors and hw.uatp0.y_sensors, up to a maximum
* of 32 for each value.
*/
/*
* Unconditionally enable the debug output so you don't have to
* recompile the kernel to diagnose it. This is not a high-throughput
* NIC driver or anything that will be hurt by a few conditionals.
*/
#define UATP_DEBUG 1
#if UATP_DEBUG
# define DPRINTF(sc, flags, format) do { \
if ((flags) & (sc)->sc_debug_flags) { \
printf("%s: %s: ", device_xname(uatp_dev(sc)), __func__); \
printf format; \
} \
} while (0)
#else
# define DPRINTF(sc, flags, format) do {} while (0)
#endif
/* Maximum number of bytes in an incoming packet of sensor data. */
#define UATP_MAX_INPUT_SIZE 81
/* Maximum number of sensors in each dimension. */
#define UATP_MAX_X_SENSORS 32
#define UATP_MAX_Y_SENSORS 32
#define UATP_MAX_SENSORS 32
#define UATP_SENSORS (UATP_MAX_X_SENSORS + UATP_MAX_Y_SENSORS)
/* Maximum accumulated sensor value. */
#define UATP_MAX_ACC 0xff
/* Maximum screen dimension to sensor dimension ratios. */
#define UATP_MAX_X_RATIO 0x180
#define UATP_MAX_Y_RATIO 0x180
#define UATP_MAX_RATIO 0x180
/* Maximum weight for positions in motion calculation. */
#define UATP_MAX_WEIGHT 0x7f
/* Maximum possible trackpad position in a single dimension. */
#define UATP_MAX_POSITION (UATP_MAX_SENSORS * UATP_MAX_RATIO)
/* Bounds on acceleration. */
#define UATP_MAX_MOTION_MULTIPLIER 16
/* Status bits transmitted in the last byte of an input packet. */
#define UATP_STATUS_BUTTON __BIT(0) /* Button pressed */
#define UATP_STATUS_BASE __BIT(2) /* Base sensor data */
#define UATP_STATUS_POST_RESET __BIT(4) /* Post-reset */
/* Forward declarations */
struct uatp_softc; /* Device driver state. */
struct uatp_descriptor; /* Descriptor for a particular model. */
struct uatp_parameters; /* Parameters common to a set of models. */
struct uatp_knobs; /* User-settable configuration knobs. */
enum uatp_tap_state {
TAP_STATE_INITIAL,
TAP_STATE_TAPPING,
TAP_STATE_TAPPED,
TAP_STATE_DOUBLE_TAPPING,
TAP_STATE_DRAGGING_DOWN,
TAP_STATE_DRAGGING_UP,
TAP_STATE_TAPPING_IN_DRAG,
};
struct uatp_knobs {
/*
* Button emulation. What do we do when two or three fingers
* are on the trackpad when the user presses the button?
*/
unsigned int two_finger_buttons;
unsigned int three_finger_buttons;
#if 0
/*
* Edge scrolling.
*
* XXX Implement this. What units should these be in?
*/
unsigned int top_edge;
unsigned int bottom_edge;
unsigned int left_edge;
unsigned int right_edge;
#endif
/*
* Multifinger tracking. What do we do with multiple fingers?
* 0. Ignore them.
* 1. Try to interpret them as ordinary mousing.
* 2. Act like a two-dimensional scroll wheel.
*/
unsigned int multifinger_track;
/*
* Sensor parameters.
*/
unsigned int x_sensors;
unsigned int x_ratio;
unsigned int y_sensors;
unsigned int y_ratio;
unsigned int sensor_threshold;
unsigned int sensor_normalizer;
unsigned int palm_width;
unsigned int old_raw_weight;
unsigned int old_smoothed_weight;
unsigned int new_raw_weight;
/*
* Motion parameters.
*
* XXX There should be a more principled model of acceleration.
*/
unsigned int motion_remainder;
unsigned int motion_threshold;
unsigned int motion_multiplier;
unsigned int motion_divisor;
unsigned int fast_motion_threshold;
unsigned int fast_motion_multiplier;
unsigned int fast_motion_divisor;
unsigned int fast_per_direction;
unsigned int motion_delay;
/*
* Tapping.
*/
unsigned int tap_limit_msec;
unsigned int double_tap_limit_msec;
unsigned int one_finger_tap_buttons;
unsigned int two_finger_tap_buttons;
unsigned int three_finger_tap_buttons;
unsigned int tap_track_distance_limit;
};
static const struct uatp_knobs default_knobs = {
/*
* Button emulation. Fingers on the trackpad don't change it
* by default -- it's still the left button.
*
* XXX The left button should have a name.
*/
.two_finger_buttons = 1,
.three_finger_buttons = 1,
/*
* Tapping. Disabled by default, with a reasonable time set
* nevertheless so that you can just set the buttons to enable
* it.
*/
.tap_limit_msec = 100,
.double_tap_limit_msec = 200,
.one_finger_tap_buttons = 0,
.two_finger_tap_buttons = 0,
.three_finger_tap_buttons = 0,
.tap_track_distance_limit = 200,
};
struct uatp_softc {
device_t sc_dev;
struct uhidev *sc_hdev; /* uhidev(9) parent. */
struct usbd_device *sc_udev; /* USB device. */
struct usbd_interface *sc_iface0; /* Geyser 3/4 reset interface. */
device_t sc_wsmousedev; /* Attached wsmouse device. */
const struct uatp_parameters *sc_parameters;
struct uatp_knobs sc_knobs;
struct sysctllog *sc_log; /* Log for sysctl knobs. */
const struct sysctlnode *sc_node; /* Our sysctl node. */
unsigned int sc_input_size; /* Input packet size. */
uint8_t sc_input[UATP_MAX_INPUT_SIZE]; /* Buffer for a packet. */
unsigned int sc_input_index; /* Current index into sc_input. */
int sc_acc[UATP_SENSORS]; /* Accumulated sensor state. */
uint8_t sc_base[UATP_SENSORS]; /* Base sample. */
uint8_t sc_sample[UATP_SENSORS];/* Current sample. */
unsigned int sc_motion_timer; /* XXX describe; motion_delay */
int sc_x_raw; /* Raw horiz. mouse position. */
int sc_y_raw; /* Raw vert. mouse position. */
int sc_z_raw; /* Raw horiz. scroll position. */
int sc_w_raw; /* Raw vert. scroll position. */
int sc_x_smoothed; /* Smoothed horiz. mouse position. */
int sc_y_smoothed; /* Smoothed vert. mouse position. */
int sc_z_smoothed; /* Smoothed horiz. scroll position. */
int sc_w_smoothed; /* Smoothed vert. scroll position. */
int sc_x_remainder; /* Remainders from acceleration. */
int sc_y_remainder;
int sc_z_remainder;
int sc_w_remainder;
unsigned int sc_track_distance; /* Distance^2 finger has tracked,
* squared to avoid sqrt in kernel. */
uint32_t sc_status; /* Status flags: */
#define UATP_ENABLED __BIT(0) /* . Is the wsmouse enabled? */
#define UATP_DYING __BIT(1) /* . Have we been deactivated? */
#define UATP_VALID __BIT(2) /* . Do we have valid sensor data? */
struct usb_task sc_reset_task; /* Task for resetting device. */
callout_t sc_untap_callout; /* Releases button after tap. */
kmutex_t sc_tap_mutex; /* Protects the following fields. */
enum uatp_tap_state sc_tap_state; /* Current tap state. */
unsigned int sc_tapping_fingers; /* No. fingers tapping. */
unsigned int sc_tapped_fingers; /* No. fingers of last tap. */
struct timeval sc_tap_timer; /* Timer for tap state transitions. */
uint32_t sc_buttons; /* Physical buttons pressed. */
uint32_t sc_all_buttons; /* Buttons pressed or tapped. */
struct uatp_parameters {
unsigned int x_ratio; /* Screen width / trackpad width. */
unsigned int x_sensors; /* Number of horizontal sensors. */
unsigned int x_sensors_17; /* XXX Same, on a 17" laptop. */
unsigned int y_ratio; /* Screen height / trackpad height. */
unsigned int y_sensors; /* Number of vertical sensors. */
unsigned int input_size; /* Size in bytes of input packets. */
/* Device-specific initialization routine. May be null. */
void (*initialize)(struct uatp_softc *);
/* Device-specific finalization routine. May be null. */
void (*finalize)(struct uatp_softc *);
/* Tests whether this is a base sample. Second argument is
* input_size bytes long. */
bool (*base_sample)(const struct uatp_softc *, const uint8_t *);
/* Reads a sensor sample from an input packet. First argument
* is UATP_MAX_X_SENSORS bytes long; second, UATP_MAX_Y_SENSORS
* bytes; third, input_size bytes. */
void (*read_sample)(uint8_t *, uint8_t *, const uint8_t *);
/* Accumulates sensor state in sc->sc_acc. */
void (*accumulate)(struct uatp_softc *);
/* Called on spurious interrupts to reset. May be null. */
void (*reset)(struct uatp_softc *);
};
/*
* The Geyser 3 and Geyser 4 share parameters. They also present
* generic USB HID mice on a different report id, so we have smaller
* packets by one byte (uhidev handles multiplexing report ids) and
* extra initialization work to switch the mode from generic USB HID
* mouse to Apple trackpad.
*/
for (i = 0; i < __arraycount(uatp_descriptors); i++)
if ((uha->uiaa->uiaa_vendor == uatp_descriptors[i].vendor) &&
(uha->uiaa->uiaa_product == uatp_descriptors[i].product))
return &uatp_descriptors[i];
static unsigned int
uatp_x_ratio(const struct uatp_softc *sc)
{
/* XXX Reject bogus values in sysctl. */
if ((0 < sc->sc_knobs.x_ratio) &&
(sc->sc_knobs.x_ratio <= UATP_MAX_X_RATIO))
return sc->sc_knobs.x_ratio;
else
return sc->sc_parameters->x_ratio;
}
static unsigned int
uatp_y_ratio(const struct uatp_softc *sc)
{
/* XXX Reject bogus values in sysctl. */
if ((0 < sc->sc_knobs.y_ratio) &&
(sc->sc_knobs.y_ratio <= UATP_MAX_Y_RATIO))
return sc->sc_knobs.y_ratio;
else
return sc->sc_parameters->y_ratio;
}
static unsigned int
uatp_old_raw_weight(const struct uatp_softc *sc)
{
/* XXX Reject bogus values in sysctl. */
if (sc->sc_knobs.old_raw_weight <= UATP_MAX_WEIGHT)
return sc->sc_knobs.old_raw_weight;
else
return 0;
}
static unsigned int
uatp_old_smoothed_weight(const struct uatp_softc *sc)
{
/* XXX Reject bogus values in sysctl. */
if (sc->sc_knobs.old_smoothed_weight <= UATP_MAX_WEIGHT)
return sc->sc_knobs.old_smoothed_weight;
else
return 0;
}
static unsigned int
uatp_new_raw_weight(const struct uatp_softc *sc)
{
/* XXX Reject bogus values in sysctl. */
if ((0 < sc->sc_knobs.new_raw_weight) &&
(sc->sc_knobs.new_raw_weight <= UATP_MAX_WEIGHT))
return sc->sc_knobs.new_raw_weight;
else
return 1;
}
static int
scale_motion(const struct uatp_softc *sc, int delta, int *remainder,
const unsigned int *multiplier, const unsigned int *divisor)
{
int product;
/*
* Keyboards, trackpads, and eject buttons share common vendor
* and product ids, but not protocols: only the trackpad
* reports a mouse protocol.
*/
if (uha->uiaa->uiaa_proto != UIPROTO_MOUSE)
return UMATCH_NONE;
/* Check for a known vendor/product id. */
uatp_descriptor = find_uatp_descriptor(uha);
if (uatp_descriptor == NULL) {
aprint_debug("%s: unknown vendor/product id\n", __func__);
return UMATCH_NONE;
}
/* Check for the expected input size. */
if ((input_size < 0) ||
((unsigned int)input_size !=
uatp_descriptor->parameters->input_size)) {
aprint_debug("%s: expected input size %u\n", __func__,
uatp_descriptor->parameters->input_size);
return UMATCH_NONE;
}
/* Initialize model-specific parameters. */
sc->sc_parameters = uatp_descriptor->parameters;
KASSERT((int)sc->sc_parameters->input_size == input_size);
KASSERT(sc->sc_parameters->x_sensors <= UATP_MAX_X_SENSORS);
KASSERT(sc->sc_parameters->x_ratio <= UATP_MAX_X_RATIO);
KASSERT(sc->sc_parameters->y_sensors <= UATP_MAX_Y_SENSORS);
KASSERT(sc->sc_parameters->y_ratio <= UATP_MAX_Y_RATIO);
aprint_verbose_dev(self,
"%u x sensors, scaled by %u for %u points on screen\n",
sc->sc_parameters->x_sensors, sc->sc_parameters->x_ratio,
sc->sc_parameters->x_sensors * sc->sc_parameters->x_ratio);
aprint_verbose_dev(self,
"%u y sensors, scaled by %u for %u points on screen\n",
sc->sc_parameters->y_sensors, sc->sc_parameters->y_ratio,
sc->sc_parameters->y_sensors * sc->sc_parameters->y_ratio);
if (sc->sc_parameters->initialize)
sc->sc_parameters->initialize(sc);
/* Register with pmf. Nothing special for suspend/resume. */
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
/* Initialize knobs and create sysctl subtree to tweak them. */
sc->sc_knobs = default_knobs;
uatp_setup_sysctl(sc);
/*
* Button emulation.
*/
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.two_finger_buttons,
"two_finger_buttons",
"buttons to emulate with two fingers on trackpad"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.three_finger_buttons,
"three_finger_buttons",
"buttons to emulate with three fingers on trackpad"))
goto err;
#if 0
/*
* Edge scrolling.
*/
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.top_edge, "top_edge",
"width of top edge for edge scrolling"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.bottom_edge,
"bottom_edge", "width of bottom edge for edge scrolling"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.left_edge, "left_edge",
"width of left edge for edge scrolling"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.right_edge, "right_edge",
"width of right edge for edge scrolling"))
goto err;
#endif
/*
* Multifinger tracking.
*/
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.multifinger_track,
"multifinger_track",
"0 to ignore multiple fingers, 1 to reset, 2 to scroll"))
goto err;
/*
* Sensor parameters.
*/
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.x_sensors, "x_sensors",
"number of x sensors"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.x_ratio, "x_ratio",
"screen width to trackpad width ratio"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.y_sensors, "y_sensors",
"number of y sensors"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.y_ratio, "y_ratio",
"screen height to trackpad height ratio"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.sensor_threshold,
"sensor_threshold", "sensor threshold"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.sensor_normalizer,
"sensor_normalizer", "sensor normalizer"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.palm_width,
"palm_width", "lower bound on width/height of palm"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.old_raw_weight,
"old_raw_weight", "weight of old raw position"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.old_smoothed_weight,
"old_smoothed_weight", "weight of old smoothed position"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.new_raw_weight,
"new_raw_weight", "weight of new raw position"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_remainder,
"motion_remainder", "remember motion division remainder"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_threshold,
"motion_threshold", "threshold before finger moves cursor"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_multiplier,
"motion_multiplier", "numerator of motion scale"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_divisor,
"motion_divisor", "divisor of motion scale"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_motion_threshold,
"fast_motion_threshold", "threshold before fast motion"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_motion_multiplier,
"fast_motion_multiplier", "numerator of fast motion scale"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_motion_divisor,
"fast_motion_divisor", "divisor of fast motion scale"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_per_direction,
"fast_per_direction", "don't frobnitz the veeblefitzer!"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_delay,
"motion_delay", "number of packets before motion kicks in"))
goto err;
/*
* Tapping.
*/
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.tap_limit_msec,
"tap_limit_msec", "milliseconds before a touch is not a tap"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.double_tap_limit_msec,
"double_tap_limit_msec",
"milliseconds before a second tap keeps the button down"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.one_finger_tap_buttons,
"one_finger_tap_buttons", "buttons for one-finger taps"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.two_finger_tap_buttons,
"two_finger_tap_buttons", "buttons for two-finger taps"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.three_finger_tap_buttons,
"three_finger_tap_buttons", "buttons for three-finger taps"))
goto err;
if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.tap_track_distance_limit,
"tap_track_distance_limit",
"maximum distance^2 of tracking during tap"))
goto err;
/* Refuse to enable if we've been deactivated. */
if (sc->sc_status & UATP_DYING) {
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("busy dying\n"));
return EIO;
}
/* Refuse to enable if we already are enabled. */
if (sc->sc_status & UATP_ENABLED) {
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("already enabled\n"));
return EBUSY;
}
/*
* The Geyser 3 and 4 models talk the generic USB HID mouse protocol by
* default. This mode switch makes them give raw sensor data instead
* so that we can implement tapping, two-finger scrolling, &c.
*/
#if UATP_DEBUG
if (sc->sc_debug_flags & UATP_DEBUG_RESET) {
unsigned int i;
DPRINTF(sc, UATP_DEBUG_RESET, ("old feature report:"));
for (i = 0; i < GEYSER34_MODE_PACKET_SIZE; i++)
printf(" %02x", (unsigned int)report[i]);
printf("\n");
/* Doing this twice is harmless here and lets this be
* one ifdef. */
report[0] = GEYSER34_RAW_MODE;
DPRINTF(sc, UATP_DEBUG_RESET, ("new feature report:"));
for (i = 0; i < GEYSER34_MODE_PACKET_SIZE; i++)
printf(" %02x", (unsigned int)report[i]);
printf("\n");
}
#endif
/*
* Some devices break packets up into chunks, so we accumulate
* input up to the expected packet length, or if it would
* overflow, discard the whole packet and start over.
*/
if (sc->sc_input_size < len) {
aprint_error_dev(uatp_dev(sc),
"discarding %u-byte input packet\n", len);
sc->sc_input_index = 0;
return;
} else if (sc->sc_input_size < (sc->sc_input_index + len)) {
aprint_error_dev(uatp_dev(sc), "discarding %u-byte input\n",
(sc->sc_input_index + len));
sc->sc_input_index = 0;
return;
} else if (sc->sc_input_size == 81 && len == 17 &&
sc->sc_input_index != 64) {
/*
* Quirk of Fountain and Geyser 1 devices: a 17-byte
* packet seems to mean the last one, but sometimes we
* get desynchronized, so drop this one and start over
* if we see a 17-byte packet that's not at the end.
*/
aprint_error_dev(uatp_dev(sc),
"discarding 17-byte nonterminal input at %u\n",
sc->sc_input_index);
sc->sc_input_index = 0;
return;
}
#if UATP_DEBUG
if (sc->sc_debug_flags & UATP_DEBUG_INTR) {
unsigned int i;
uint8_t *bytes = ibuf;
DPRINTF(sc, UATP_DEBUG_INTR, ("raw"));
for (i = 0; i < len; i++)
printf(" %02x", (unsigned int)bytes[i]);
printf("\n");
}
#endif
memcpy(&sc->sc_input[sc->sc_input_index], ibuf, len);
sc->sc_input_index += len;
if (sc->sc_input_index != sc->sc_input_size) {
/* Wait until packet is complete. */
DPRINTF(sc, UATP_DEBUG_INTR, ("partial packet: %u bytes\n",
len));
return;
}
/* Clear the buffer and process the now complete packet. */
sc->sc_input_index = 0;
input = sc->sc_input;
/* The last byte's first bit is set iff the button is pressed.
* XXX Left button should have a name. */
buttons = ((input[sc->sc_input_size - 1] & UATP_STATUS_BUTTON)
? 1 : 0);
#if UATP_DEBUG
if (sc->sc_debug_flags & UATP_DEBUG_INTR) {
unsigned int i;
DPRINTF(sc, UATP_DEBUG_INTR, ("x sensors"));
for (i = 0; i < uatp_x_sensors(sc); i++)
printf(" %02x", (unsigned int)uatp_x_sample(sc)[i]);
printf("\n");
DPRINTF(sc, UATP_DEBUG_INTR, ("y sensors"));
for (i = 0; i < uatp_y_sensors(sc); i++)
printf(" %02x", (unsigned int)uatp_y_sample(sc)[i]);
printf("\n");
} else if ((sc->sc_debug_flags & UATP_DEBUG_STATUS) &&
(input[sc->sc_input_size - 1] &~
(UATP_STATUS_BUTTON | UATP_STATUS_BASE |
UATP_STATUS_POST_RESET)))
DPRINTF(sc, UATP_DEBUG_STATUS, ("status byte: %02x\n",
input[sc->sc_input_size - 1]));
#endif
/*
* If this is a base sample, initialize the state to interpret
* subsequent samples relative to it, and stop here.
*/
if (sc->sc_parameters->base_sample(sc, input)) {
DPRINTF(sc, UATP_DEBUG_PARSE,
("base sample, buttons %"PRIx32"\n", buttons));
/* XXX Should the valid bit ever be reset? */
sc->sc_status |= UATP_VALID;
uatp_clear_position(sc);
memcpy(sc->sc_base, sc->sc_sample, sizeof(sc->sc_base));
/* XXX Perform 17" size detection like Linux? */
return;
}
/* If not, accumulate the change in the sensors. */
sc->sc_parameters->accumulate(sc);
#if UATP_DEBUG
if (sc->sc_debug_flags & UATP_DEBUG_ACCUMULATE) {
unsigned int i;
DPRINTF(sc, UATP_DEBUG_ACCUMULATE, ("accumulated x state:"));
for (i = 0; i < uatp_x_sensors(sc); i++)
printf(" %02x", (unsigned int)uatp_x_acc(sc)[i]);
printf("\n");
DPRINTF(sc, UATP_DEBUG_ACCUMULATE, ("accumulated y state:"));
for (i = 0; i < uatp_y_sensors(sc); i++)
printf(" %02x", (unsigned int)uatp_y_acc(sc)[i]);
printf("\n");
}
#endif
/* Compute the change in coordinates and buttons. */
dx = dy = dz = dw = 0;
if ((!interpret_input(sc, &dx, &dy, &dz, &dw, &buttons)) &&
/* If there's no input because we're releasing a button,
* then it's not spurious. XXX Mutex? */
(sc->sc_buttons == 0)) {
DPRINTF(sc, UATP_DEBUG_SPURINTR, ("spurious interrupt\n"));
if (sc->sc_parameters->reset)
sc->sc_parameters->reset(sc);
return;
}
/*
* Different ways to discern the base sample initializing the state.
* `base_sample_softc_flag' uses a state flag stored in the softc;
* `base_sample_input_flag' checks a flag at the end of the input
* packet.
*/
/*
* Report input to wsmouse, if there is anything interesting to report.
* We must take into consideration the current tap-and-drag button
* state.
*/
static void
uatp_input(struct uatp_softc *sc, uint32_t buttons,
int dx, int dy, int dz, int dw)
{
uint32_t all_buttons;
/*
* Interpret the current tap state to decide whether the tap buttons
* are currently pressed.
*/
static uint32_t
uatp_tapped_buttons(struct uatp_softc *sc)
{
KASSERT(mutex_owned(&sc->sc_tap_mutex));
switch (sc->sc_tap_state) {
case TAP_STATE_INITIAL:
case TAP_STATE_TAPPING:
return 0;
case TAP_STATE_TAPPED:
case TAP_STATE_DOUBLE_TAPPING:
case TAP_STATE_DRAGGING_DOWN:
case TAP_STATE_DRAGGING_UP:
case TAP_STATE_TAPPING_IN_DRAG:
CHECK((0 < sc->sc_tapped_fingers), return 0);
switch (sc->sc_tapped_fingers) {
case 1: return sc->sc_knobs.one_finger_tap_buttons;
case 2: return sc->sc_knobs.two_finger_tap_buttons;
case 3:
default: return sc->sc_knobs.three_finger_tap_buttons;
}
/*
* Interpret the current input state to find a difference in all the
* relevant coordinates and buttons to pass on to wsmouse, and update
* any internal driver state necessary to interpret subsequent input
* relative to this one.
*/
static bool
interpret_input(struct uatp_softc *sc, int *dx, int *dy, int *dz, int *dw,
uint32_t *buttons)
{
unsigned int x_pressure, x_raw, x_fingers;
unsigned int y_pressure, y_raw, y_fingers;
unsigned int fingers;
if ((x_pressure == 0) && (y_pressure == 0)) {
bool ok;
/* No fingers: clear position and maybe report a tap. */
DPRINTF(sc, UATP_DEBUG_INTR,
("no position detected; clearing position\n"));
if (*buttons == 0) {
ok = tap_released(sc);
} else {
tap_reset(sc);
/* Button pressed: interrupt is not spurious. */
ok = true;
}
/*
* Don't clear the position until after tap_released,
* which needs to know the track distance.
*/
uatp_clear_position(sc);
return ok;
} else if ((x_pressure == 0) || (y_pressure == 0)) {
/* XXX What to do here? */
DPRINTF(sc, UATP_DEBUG_INTR,
("pressure in only one dimension; ignoring\n"));
return true;
} else if ((x_pressure == 1) && (y_pressure == 1)) {
fingers = uimax(x_fingers, y_fingers);
CHECK((0 < fingers), return false);
if (*buttons == 0)
tap_touched(sc, fingers);
else if (fingers == 1)
tap_reset(sc);
else /* Multiple fingers, button pressed. */
*buttons = emulated_buttons(sc, fingers);
update_position(sc, fingers, x_raw, y_raw, dx, dy, dz, dw);
return true;
} else {
/* Palm detected in either or both of the dimensions. */
DPRINTF(sc, UATP_DEBUG_INTR, ("palm detected; ignoring\n"));
return true;
}
}
/*
* Interpret the accumulated sensor state along one dimension to find
* the number, mean position, and pressure of fingers. Returns 0 to
* indicate no pressure, returns 1 and sets *position and *fingers to
* indicate fingers, and returns 2 to indicate palm.
*
* XXX Give symbolic names to the return values.
*/
static unsigned int
interpret_dimension(struct uatp_softc *sc, const int *acc,
unsigned int n_sensors, unsigned int ratio,
unsigned int *position, unsigned int *fingers)
{
unsigned int i, v, n_fingers, sum;
unsigned int total[UATP_MAX_SENSORS];
unsigned int weighted[UATP_MAX_SENSORS];
unsigned int sensor_threshold = sc->sc_knobs.sensor_threshold;
unsigned int sensor_normalizer = sc->sc_knobs.sensor_normalizer;
unsigned int width = 0; /* GCC is not smart enough. */
unsigned int palm_width = sc->sc_knobs.palm_width;
enum { none, nondecreasing, decreasing } state = none;
if (sensor_threshold < sensor_normalizer)
sensor_normalizer = sensor_threshold;
if (palm_width == 0) /* Effectively disable palm detection. */
palm_width = UATP_MAX_POSITION;
/*
* Arithmetic bounds:
* . n_sensors is at most UATP_MAX_SENSORS,
* . n_fingers is at most UATP_MAX_SENSORS,
* . i is at most UATP_MAX_SENSORS,
* . sc->sc_acc[i] is at most UATP_MAX_ACC,
* . i * sc->sc_acc[i] is at most UATP_MAX_SENSORS * UATP_MAX_ACC,
* . each total[j] is at most UATP_MAX_SENSORS * UATP_MAX_ACC,
* . each weighted[j] is at most UATP_MAX_SENSORS^2 * UATP_MAX_ACC,
* . ratio is at most UATP_MAX_RATIO,
* . each weighted[j] * ratio is at most
* UATP_MAX_SENSORS^2 * UATP_MAX_ACC * UATP_MAX_RATIO,
* which is #x5fa0000 with the current values of the constants,
* and
* . the sum of the positions is at most
* UATP_MAX_SENSORS * UATP_MAX_POSITION,
* which is #x60000 with the current values of the constants.
* Hence all of the arithmetic here fits in int (and thus also
* unsigned int). If you change the constants, though, you
* must update the analysis.
*/
__CTASSERT(0x5fa0000 == (UATP_MAX_SENSORS * UATP_MAX_SENSORS *
UATP_MAX_ACC * UATP_MAX_RATIO));
__CTASSERT(0x60000 == (UATP_MAX_SENSORS * UATP_MAX_POSITION));
CHECK_(n_sensors <= UATP_MAX_SENSORS);
CHECK_(ratio <= UATP_MAX_RATIO);
/*
* Detect each finger by looking for a consecutive sequence of
* increasing and then decreasing pressures above the sensor
* threshold. Compute the finger's position as the weighted
* average of positions, weighted by the pressure at that
* position. Finally, return the average finger position.
*/
/*
* There is a very hairy state machine for detecting taps. At every
* touch, we record the maximum number of fingers touched, and don't
* reset it to zero until the finger is released.
*
* INITIAL STATE
* (no tapping fingers; no tapped fingers)
* - On touch, go to TAPPING STATE.
* - On any other input, remain in INITIAL STATE.
*
* TAPPING STATE: Finger touched; might be tap.
* (tapping fingers; no tapped fingers)
* - On release within the tap limit, go to TAPPED STATE.
* - On release after the tap limit, go to INITIAL STATE.
* - On any other input, remain in TAPPING STATE.
*
* TAPPED STATE: Finger recently tapped, and might double-tap.
* (no tapping fingers; tapped fingers)
* - On touch within the double-tap limit, go to DOUBLE-TAPPING STATE.
* - On touch after the double-tap limit, go to TAPPING STATE.
* - On no event after the double-tap limit, go to INITIAL STATE.
* - On any other input, remain in TAPPED STATE.
*
* DOUBLE-TAPPING STATE: Finger touched soon after tap; might be double-tap.
* (tapping fingers; tapped fingers)
* - On release within the tap limit, release button and go to TAPPED STATE.
* - On release after the tap limit, go to DRAGGING UP STATE.
* - On touch after the tap limit, go to DRAGGING DOWN STATE.
* - On any other input, remain in DOUBLE-TAPPING STATE.
*
* DRAGGING DOWN STATE: Finger has double-tapped and is dragging, not tapping.
* (no tapping fingers; tapped fingers)
* - On release, go to DRAGGING UP STATE.
* - On any other input, remain in DRAGGING DOWN STATE.
*
* DRAGGING UP STATE: Finger has double-tapped and is up.
* (no tapping fingers; tapped fingers)
* - On touch, go to TAPPING IN DRAG STATE.
* - On any other input, remain in DRAGGING UP STATE.
*
* TAPPING IN DRAG STATE: Tap-dancing while cross-dressed.
* (tapping fingers; tapped fingers)
* - On release within the tap limit, go to TAPPED STATE.
* - On release after the tap limit, go to DRAGGING UP STATE.
* - On any other input, remain in TAPPING IN DRAG STATE.
*
* Warning: The graph of states is split into two components, those
* with tapped fingers and those without. The only path from any state
* without tapped fingers to a state with tapped fingers must pass
* through TAPPED STATE. Also, the only transitions into TAPPED STATE
* must be from states with tapping fingers, which become the tapped
* fingers. If you edit the state machine, you must either preserve
* these properties, or globally transform the state machine to avoid
* the bad consequences of violating these properties.
*/
KASSERT(mutex_owned(&sc->sc_tap_mutex));
switch (sc->sc_tap_state) {
case TAP_STATE_INITIAL: state = "initial"; break;
case TAP_STATE_TAPPING: state = "tapping"; break;
case TAP_STATE_TAPPED: state = "tapped"; break;
case TAP_STATE_DOUBLE_TAPPING: state = "double-tapping"; break;
case TAP_STATE_DRAGGING_DOWN: state = "dragging-down"; break;
case TAP_STATE_DRAGGING_UP: state = "dragging-up"; break;
case TAP_STATE_TAPPING_IN_DRAG: state = "tapping-in-drag"; break;
default:
snprintf(buffer, sizeof(buffer), "unknown (%d)",
sc->sc_tap_state);
state = buffer;
break;
}
static void
tap_finalize(struct uatp_softc *sc)
{
/* XXX Can the callout still be scheduled here? */
callout_destroy(&sc->sc_untap_callout);
mutex_destroy(&sc->sc_tap_mutex);
}
static void
tap_enable(struct uatp_softc *sc)
{
mutex_enter(&sc->sc_tap_mutex);
tap_transition_initial(sc);
sc->sc_buttons = 0; /* XXX Not the right place? */
sc->sc_all_buttons = 0;
mutex_exit(&sc->sc_tap_mutex);
}
static void
tap_disable(struct uatp_softc *sc)
{
/* Reset tapping, and wait for any callouts to complete. */
tap_reset_wait(sc);
}
/*
* Reset tap state. If the untap callout has just fired, it may signal
* a harmless button release event before this returns.
*/
static void
tap_transition_initial(struct uatp_softc *sc)
{
/*
* No checks. This state is always kosher, and sometimes a
* fallback in case of failure.
*/
tap_transition(sc, TAP_STATE_INITIAL, &zero_timeval, 0, 0);
}
/* Touch transitions */
static void
tap_transition_tapping(struct uatp_softc *sc, const struct timeval *start_time,
unsigned int fingers)
{
CHECK((sc->sc_tapping_fingers <= fingers),
do { tap_transition_initial(sc); return; } while (0));
tap_transition(sc, TAP_STATE_TAPPING, start_time, fingers, 0);
}
static void
tap_transition_double_tapping(struct uatp_softc *sc,
const struct timeval *start_time, unsigned int fingers)
{
CHECK((sc->sc_tapping_fingers <= fingers),
do { tap_transition_initial(sc); return; } while (0));
CHECK((0 < sc->sc_tapped_fingers),
do { tap_transition_initial(sc); return; } while (0));
tap_transition(sc, TAP_STATE_DOUBLE_TAPPING, start_time, fingers,
sc->sc_tapped_fingers);
}
static void
tap_transition_tapping_in_drag(struct uatp_softc *sc,
const struct timeval *start_time, unsigned int fingers)
{
CHECK((sc->sc_tapping_fingers <= fingers),
do { tap_transition_initial(sc); return; } while (0));
CHECK((0 < sc->sc_tapped_fingers),
do { tap_transition_initial(sc); return; } while (0));
tap_transition(sc, TAP_STATE_TAPPING_IN_DRAG, start_time, fingers,
sc->sc_tapped_fingers);
}
/* Release transitions */
static void
tap_transition_tapped(struct uatp_softc *sc, const struct timeval *start_time)
{
/*
* The fingers that were tapping -- of which there must have
* been at least one -- are now the fingers that have tapped,
* and there are no longer fingers tapping.
*/
CHECK((0 < sc->sc_tapping_fingers),
do { tap_transition_initial(sc); return; } while (0));
tap_transition(sc, TAP_STATE_TAPPED, start_time, 0,
sc->sc_tapping_fingers);
schedule_untap(sc);
}
CHECK((0 < fingers), return);
callout_stop(&sc->sc_untap_callout);
mutex_enter(&sc->sc_tap_mutex);
TAP_DEBUG_PRE(sc);
/*
* Guarantee that the number of tapping fingers never decreases
* except when it is reset to zero on release.
*/
if (fingers < sc->sc_tapping_fingers)
fingers = sc->sc_tapping_fingers;
switch (sc->sc_tap_state) {
case TAP_STATE_INITIAL:
getmicrouptime(&now);
tap_transition_tapping(sc, &now, fingers);
break;
case TAP_STATE_TAPPING:
/*
* Number of fingers may have increased, so transition
* even though we're already in TAPPING.
*/
tap_transition_tapping(sc, &sc->sc_tap_timer, fingers);
break;
case TAP_STATE_TAPPED:
getmicrouptime(&now);
/*
* If the double-tap time limit has passed, it's the
* callout's responsibility to handle that event, so we
* assume the limit has not passed yet.
*/
tap_transition_double_tapping(sc, &now, fingers);
break;
case TAP_STATE_DOUBLE_TAPPING:
getmicrouptime(&now);
timersub(&now, &sc->sc_tap_timer, &diff);
uatp_tap_limit(sc, &limit);
if (timercmp(&diff, &limit, >) ||
(sc->sc_track_distance >
sc->sc_knobs.tap_track_distance_limit))
tap_transition_dragging_down(sc);
break;
case TAP_STATE_DRAGGING_DOWN:
break;
case TAP_STATE_DRAGGING_UP:
getmicrouptime(&now);
tap_transition_tapping_in_drag(sc, &now, fingers);
break;
case TAP_STATE_TAPPING_IN_DRAG:
/*
* Number of fingers may have increased, so transition
* even though we're already in TAPPING IN DRAG.
*/
tap_transition_tapping_in_drag(sc, &sc->sc_tap_timer, fingers);
break;
mutex_enter(&sc->sc_tap_mutex);
TAP_DEBUG_PRE(sc);
switch (sc->sc_tap_state) {
case TAP_STATE_INITIAL:
case TAP_STATE_TAPPED:
case TAP_STATE_DRAGGING_UP:
/* Spurious interrupt: fingers are already off. */
ok = false;
break;
case TAP_STATE_TAPPING:
temporary_release = false;
non_tapped_transition = &tap_transition_initial;
goto maybe_tap;
case TAP_STATE_DOUBLE_TAPPING:
temporary_release = true;
non_tapped_transition = &tap_transition_dragging_up;
goto maybe_tap;
case TAP_STATE_TAPPING_IN_DRAG:
temporary_release = false;
non_tapped_transition = &tap_transition_dragging_up;
goto maybe_tap;
maybe_tap:
getmicrouptime(&now);
timersub(&now, &sc->sc_tap_timer, &diff);
uatp_tap_limit(sc, &limit);
if (timercmp(&diff, &limit, <=) &&
(sc->sc_track_distance <=
sc->sc_knobs.tap_track_distance_limit)) {
if (temporary_release) {
/*
* XXX Kludge: Temporarily transition
* to a tap state that uatp_input will
* interpret as `no buttons tapped',
* saving the tapping fingers. There
* should instead be a separate routine
* uatp_input_untapped.
*/
unsigned int fingers = sc->sc_tapping_fingers;
tap_transition_initial(sc);
uatp_input(sc, 0, 0, 0, 0, 0);
sc->sc_tapping_fingers = fingers;
}
tap_transition_tapped(sc, &now);
} else {
(*non_tapped_transition)(sc);
}
ok = true;
break;
case TAP_STATE_DRAGGING_DOWN:
tap_transition_dragging_up(sc);
ok = true;
break;
default:
aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
__func__, sc->sc_tap_state);
tap_transition_initial(sc);
ok = false;
break;
}
TAP_DEBUG_POST(sc);
mutex_exit(&sc->sc_tap_mutex);
return ok;
}
/* Untapping: Releasing the button after a tap */
static void
schedule_untap(struct uatp_softc *sc)
{
unsigned int ms = sc->sc_knobs.double_tap_limit_msec;
if (ms <= 1000)
callout_schedule(&sc->sc_untap_callout, mstohz(ms));
else /* XXX Reject bogus values in sysctl. */
aprint_error_dev(uatp_dev(sc),
"double-tap delay too long: %ums\n", ms);
}
mutex_enter(&sc->sc_tap_mutex);
TAP_DEBUG_PRE(sc);
switch (sc->sc_tap_state) {
case TAP_STATE_TAPPED:
tap_transition_initial(sc);
/*
* XXX Kludge: Call uatp_input after the state transition
* to make sure that it will actually release the button.
*/
uatp_input(sc, 0, 0, 0, 0, 0);
case TAP_STATE_INITIAL:
case TAP_STATE_TAPPING:
case TAP_STATE_DOUBLE_TAPPING:
case TAP_STATE_DRAGGING_UP:
case TAP_STATE_DRAGGING_DOWN:
case TAP_STATE_TAPPING_IN_DRAG:
/*
* Somebody else got in and changed the state before we
* untapped. Let them take over; do nothing here.
*/
break;
default:
aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
__func__, sc->sc_tap_state);
tap_transition_initial(sc);
/* XXX Just in case...? */
uatp_input(sc, 0, 0, 0, 0, 0);
break;
}
TAP_DEBUG_POST(sc);
mutex_exit(&sc->sc_tap_mutex);
}
/*
* Emulate different buttons if the user holds down n fingers while
* pressing the physical button. (This is unrelated to tapping.)
*/
/*
* Update the position known to the driver based on the position and
* number of fingers. dx, dy, dz, and dw are expected to hold zero;
* update_position may store nonzero changes in position in them.
*/
static void
update_position(struct uatp_softc *sc, unsigned int fingers,
unsigned int x_raw, unsigned int y_raw,
int *dx, int *dy, int *dz, int *dw)
{
CHECK((0 < fingers), return);
/*
* XXX Scrolling needs to use a totally different motion model.
*/
static void
move_mouse(struct uatp_softc *sc, unsigned int x_raw, unsigned int y_raw,
int *dx, int *dy)
{
move(sc, "mouse", x_raw, y_raw, &sc->sc_x_raw, &sc->sc_y_raw,
&sc->sc_x_smoothed, &sc->sc_y_smoothed,
&sc->sc_x_remainder, &sc->sc_y_remainder,
dx, dy);
}
static void
scroll_wheel(struct uatp_softc *sc, unsigned int x_raw, unsigned int y_raw,
int *dz, int *dw)
{
move(sc, "scroll", x_raw, y_raw, &sc->sc_z_raw, &sc->sc_w_raw,
&sc->sc_z_smoothed, &sc->sc_w_smoothed,
&sc->sc_z_remainder, &sc->sc_w_remainder,
dz, dw);
}
static void
move(struct uatp_softc *sc, const char *ctx, unsigned int a, unsigned int b,
int *a_raw, int *b_raw,
int *a_smoothed, int *b_smoothed,
unsigned int *a_remainder, unsigned int *b_remainder,
int *da, int *db)
{
#define CHECK_(condition) CHECK(condition, return)
int old_a_raw = *a_raw, old_a_smoothed = *a_smoothed;
int old_b_raw = *b_raw, old_b_smoothed = *b_smoothed;
unsigned int a_dist, b_dist, dist_squared;
bool a_fast, b_fast;
/*
* Make sure the quadratics in motion_below_threshold and
* tracking distance don't overflow int arithmetic.
*/
__CTASSERT(0x12000000 == (2 * UATP_MAX_POSITION * UATP_MAX_POSITION));
/*
* The checks above guarantee that the differences here are at
* most UATP_MAX_POSITION in magnitude, since both minuend and
* subtrahend are nonnegative and at most UATP_MAX_POSITION.
*/
if (motion_below_threshold(sc, sc->sc_knobs.motion_threshold,
(int)(*a_smoothed - old_a_smoothed),
(int)(*b_smoothed - old_b_smoothed))) {
DPRINTF(sc, UATP_DEBUG_MOVE,
("%s motion too small: (%d, %d) -> (%d, %d)\n", ctx,
old_a_smoothed, old_b_smoothed,
*a_smoothed, *b_smoothed));
return;
}
if (sc->sc_knobs.fast_per_direction == 0) {
a_fast = b_fast = !motion_below_threshold(sc,
sc->sc_knobs.fast_motion_threshold,
(int)(*a_smoothed - old_a_smoothed),
(int)(*b_smoothed - old_b_smoothed));
} else {
a_fast = !motion_below_threshold(sc,
sc->sc_knobs.fast_motion_threshold,
(int)(*a_smoothed - old_a_smoothed),
0);
b_fast = !motion_below_threshold(sc,
sc->sc_knobs.fast_motion_threshold,
0,
(int)(*b_smoothed - old_b_smoothed));
}
*da = accelerate(sc, old_a_raw, *a_raw, old_a_smoothed, *a_smoothed,
a_fast, a_remainder);
*db = accelerate(sc, old_b_raw, *b_raw, old_b_smoothed, *b_smoothed,
b_fast, b_remainder);
DPRINTF(sc, UATP_DEBUG_MOVE,
("update %s position (%d, %d) -> (%d, %d), move by (%d, %d)\n",
ctx, old_a_smoothed, old_b_smoothed, *a_smoothed, *b_smoothed,
*da, *db));
#undef CHECK_
}
static int
smooth(struct uatp_softc *sc, unsigned int old_raw, unsigned int old_smoothed,
unsigned int raw)
{
#define CHECK_(condition) CHECK(condition, return old_raw)
/*
* Arithmetic bounds:
* . the weights are at most UATP_MAX_WEIGHT;
* . the positions are at most UATP_MAX_POSITION; and so
* . the numerator of the average is at most
* 3 * UATP_MAX_WEIGHT * UATP_MAX_POSITION,
* which is #x477000, fitting comfortably in an int.
*/
__CTASSERT(0x477000 == (3 * UATP_MAX_WEIGHT * UATP_MAX_POSITION));
unsigned int old_raw_weight = uatp_old_raw_weight(sc);
unsigned int old_smoothed_weight = uatp_old_smoothed_weight(sc);
unsigned int new_raw_weight = uatp_new_raw_weight(sc);
CHECK_(old_raw_weight <= UATP_MAX_WEIGHT);
CHECK_(old_smoothed_weight <= UATP_MAX_WEIGHT);
CHECK_(new_raw_weight <= UATP_MAX_WEIGHT);
CHECK_(old_raw <= UATP_MAX_POSITION);
CHECK_(old_smoothed <= UATP_MAX_POSITION);
CHECK_(raw <= UATP_MAX_POSITION);
return (((old_raw_weight * old_raw) +
(old_smoothed_weight * old_smoothed) +
(new_raw_weight * raw))
/ (old_raw_weight + old_smoothed_weight + new_raw_weight));
#undef CHECK_
}
static bool
motion_below_threshold(struct uatp_softc *sc, unsigned int threshold,
int x, int y)
{
unsigned int x_squared, y_squared;
/* Caller guarantees the multiplication will not overflow. */
KASSERT(-UATP_MAX_POSITION <= x);
KASSERT(-UATP_MAX_POSITION <= y);
KASSERT(x <= UATP_MAX_POSITION);
KASSERT(y <= UATP_MAX_POSITION);
__CTASSERT(0x12000000 == (2 * UATP_MAX_POSITION * UATP_MAX_POSITION));
x_squared = (x * x);
y_squared = (y * y);
return (x_squared + y_squared) < threshold;
}
static int
accelerate(struct uatp_softc *sc, unsigned int old_raw, unsigned int raw,
unsigned int old_smoothed, unsigned int smoothed, bool fast,
int *remainder)
{
#define CHECK_(condition) CHECK(condition, return 0)
/* Guarantee that the scaling won't overflow. */
__CTASSERT(0x30000 ==
(UATP_MAX_POSITION * UATP_MAX_MOTION_MULTIPLIER));