#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uatp.c,v 1.31 2022/03/28 12:45:04 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uhidev.h>
#include <dev/usb/usbhid.h>
#include <dev/hid/hid.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#define CHECK(condition, fail) do { \
if (! (condition)) { \
aprint_error_dev(uatp_dev(sc), "%s: check failed: %s\n",\
__func__, #condition); \
fail; \
} \
} while (0)
#define UATP_DEBUG_ATTACH __BIT(0)
#define UATP_DEBUG_MISC __BIT(1)
#define UATP_DEBUG_WSMOUSE __BIT(2)
#define UATP_DEBUG_IOCTL __BIT(3)
#define UATP_DEBUG_RESET __BIT(4)
#define UATP_DEBUG_INTR __BIT(5)
#define UATP_DEBUG_PARSE __BIT(6)
#define UATP_DEBUG_TAP __BIT(7)
#define UATP_DEBUG_EMUL_BUTTON __BIT(8)
#define UATP_DEBUG_ACCUMULATE __BIT(9)
#define UATP_DEBUG_STATUS __BIT(10)
#define UATP_DEBUG_SPURINTR __BIT(11)
#define UATP_DEBUG_MOVE __BIT(12)
#define UATP_DEBUG_ACCEL __BIT(13)
#define UATP_DEBUG_TRACK_DIST __BIT(14)
#define UATP_DEBUG_PALM __BIT(15)
#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
#define UATP_MAX_INPUT_SIZE 81
#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)
#define UATP_MAX_ACC 0xff
#define UATP_MAX_X_RATIO 0x180
#define UATP_MAX_Y_RATIO 0x180
#define UATP_MAX_RATIO 0x180
#define UATP_MAX_WEIGHT 0x7f
#define UATP_MAX_POSITION (UATP_MAX_SENSORS * UATP_MAX_RATIO)
#define UATP_MAX_MOTION_MULTIPLIER 16
#define UATP_STATUS_BUTTON __BIT(0)
#define UATP_STATUS_BASE __BIT(2)
#define UATP_STATUS_POST_RESET __BIT(4)
struct uatp_softc;
struct uatp_descriptor;
struct uatp_parameters;
struct uatp_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,
};
static const struct uatp_descriptor *find_uatp_descriptor
(const struct uhidev_attach_arg *);
static device_t uatp_dev(const struct uatp_softc *);
static uint8_t *uatp_x_sample(struct uatp_softc *);
static uint8_t *uatp_y_sample(struct uatp_softc *);
static int *uatp_x_acc(struct uatp_softc *);
static int *uatp_y_acc(struct uatp_softc *);
static void uatp_clear_position(struct uatp_softc *);
static unsigned int uatp_x_sensors(const struct uatp_softc *);
static unsigned int uatp_y_sensors(const struct uatp_softc *);
static unsigned int uatp_x_ratio(const struct uatp_softc *);
static unsigned int uatp_y_ratio(const struct uatp_softc *);
static unsigned int uatp_old_raw_weight(const struct uatp_softc *);
static unsigned int uatp_old_smoothed_weight(const struct uatp_softc *);
static unsigned int uatp_new_raw_weight(const struct uatp_softc *);
static int scale_motion(const struct uatp_softc *, int, int *,
const unsigned int *, const unsigned int *);
static int uatp_scale_motion(const struct uatp_softc *, int, int *);
static int uatp_scale_fast_motion(const struct uatp_softc *, int, int *);
static int uatp_match(device_t, cfdata_t, void *);
static void uatp_attach(device_t, device_t, void *);
static void uatp_setup_sysctl(struct uatp_softc *);
static bool uatp_setup_sysctl_knob(struct uatp_softc *, int *, const char *,
const char *);
static void uatp_childdet(device_t, device_t);
static int uatp_detach(device_t, int);
static int uatp_activate(device_t, enum devact);
static int uatp_enable(void *);
static void uatp_disable(void *);
static int uatp_ioctl(void *, unsigned long, void *, int, struct lwp *);
static void geyser34_enable_raw_mode(struct uatp_softc *);
static void geyser34_initialize(struct uatp_softc *);
static void geyser34_finalize(struct uatp_softc *);
static void geyser34_deferred_reset(struct uatp_softc *);
static void geyser34_reset_task(void *);
static void uatp_intr(void *, void *, unsigned int);
static bool base_sample_softc_flag(const struct uatp_softc *, const uint8_t *);
static bool base_sample_input_flag(const struct uatp_softc *, const uint8_t *);
static void read_sample_1(uint8_t *, uint8_t *, const uint8_t *);
static void read_sample_2(uint8_t *, uint8_t *, const uint8_t *);
static void accumulate_sample_1(struct uatp_softc *);
static void accumulate_sample_2(struct uatp_softc *);
static void uatp_input(struct uatp_softc *, uint32_t, int, int, int, int);
static uint32_t uatp_tapped_buttons(struct uatp_softc *);
static bool interpret_input(struct uatp_softc *, int *, int *, int *, int *,
uint32_t *);
static unsigned int interpret_dimension(struct uatp_softc *, const int *,
unsigned int, unsigned int, unsigned int *, unsigned int *);
static void tap_initialize(struct uatp_softc *);
static void tap_finalize(struct uatp_softc *);
static void tap_enable(struct uatp_softc *);
static void tap_disable(struct uatp_softc *);
static void tap_transition(struct uatp_softc *, enum uatp_tap_state,
const struct timeval *, unsigned int, unsigned int);
static void tap_transition_initial(struct uatp_softc *);
static void tap_transition_tapping(struct uatp_softc *, const struct timeval *,
unsigned int);
static void tap_transition_double_tapping(struct uatp_softc *,
const struct timeval *, unsigned int);
static void tap_transition_dragging_down(struct uatp_softc *);
static void tap_transition_tapping_in_drag(struct uatp_softc *,
const struct timeval *, unsigned int);
static void tap_transition_tapped(struct uatp_softc *, const struct timeval *);
static void tap_transition_dragging_up(struct uatp_softc *);
static void tap_reset(struct uatp_softc *);
static void tap_reset_wait(struct uatp_softc *);
static void tap_touched(struct uatp_softc *, unsigned int);
static bool tap_released(struct uatp_softc *);
static void schedule_untap(struct uatp_softc *);
static void untap_callout(void *);
static uint32_t emulated_buttons(struct uatp_softc *, unsigned int);
static void update_position(struct uatp_softc *, unsigned int,
unsigned int, unsigned int, int *, int *, int *, int *);
static void move_mouse(struct uatp_softc *, unsigned int, unsigned int,
int *, int *);
static void scroll_wheel(struct uatp_softc *, unsigned int, unsigned int,
int *, int *);
static void move(struct uatp_softc *, const char *, unsigned int, unsigned int,
int *, int *, int *, int *, unsigned int *, unsigned int *, int *, int *);
static int smooth(struct uatp_softc *, unsigned int, unsigned int,
unsigned int);
static bool motion_below_threshold(struct uatp_softc *, unsigned int,
int, int);
static int accelerate(struct uatp_softc *, unsigned int, unsigned int,
unsigned int, unsigned int, bool, int *);
struct uatp_knobs {
unsigned int two_finger_buttons;
unsigned int three_finger_buttons;
#if 0
unsigned int top_edge;
unsigned int bottom_edge;
unsigned int left_edge;
unsigned int right_edge;
#endif
unsigned int multifinger_track;
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;
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;
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 = {
.two_finger_buttons = 1,
.three_finger_buttons = 1,
#if 0
.top_edge = 0,
.bottom_edge = 0,
.left_edge = 0,
.right_edge = 0,
#endif
.multifinger_track = 0,
.x_sensors = 0,
.x_ratio = 0,
.y_sensors = 0,
.y_ratio = 0,
.sensor_threshold = 5,
.sensor_normalizer = 5,
.palm_width = 0,
.old_raw_weight = 0,
.old_smoothed_weight = 5,
.new_raw_weight = 1,
.motion_remainder = 1,
.motion_threshold = 0,
.motion_multiplier = 1,
.motion_divisor = 1,
.fast_motion_threshold = 10,
.fast_motion_multiplier = 3,
.fast_motion_divisor = 2,
.fast_per_direction = 0,
.motion_delay = 4,
.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;
struct usbd_device *sc_udev;
struct usbd_interface *sc_iface0;
device_t sc_wsmousedev;
const struct uatp_parameters *sc_parameters;
struct uatp_knobs sc_knobs;
struct sysctllog *sc_log;
const struct sysctlnode *sc_node;
unsigned int sc_input_size;
uint8_t sc_input[UATP_MAX_INPUT_SIZE];
unsigned int sc_input_index;
int sc_acc[UATP_SENSORS];
uint8_t sc_base[UATP_SENSORS];
uint8_t sc_sample[UATP_SENSORS];
unsigned int sc_motion_timer;
int sc_x_raw;
int sc_y_raw;
int sc_z_raw;
int sc_w_raw;
int sc_x_smoothed;
int sc_y_smoothed;
int sc_z_smoothed;
int sc_w_smoothed;
int sc_x_remainder;
int sc_y_remainder;
int sc_z_remainder;
int sc_w_remainder;
unsigned int sc_track_distance;
uint32_t sc_status;
#define UATP_ENABLED __BIT(0)
#define UATP_DYING __BIT(1)
#define UATP_VALID __BIT(2)
struct usb_task sc_reset_task;
callout_t sc_untap_callout;
kmutex_t sc_tap_mutex;
enum uatp_tap_state sc_tap_state;
unsigned int sc_tapping_fingers;
unsigned int sc_tapped_fingers;
struct timeval sc_tap_timer;
uint32_t sc_buttons;
uint32_t sc_all_buttons;
#if UATP_DEBUG
uint32_t sc_debug_flags;
#endif
};
struct uatp_descriptor {
uint16_t vendor;
uint16_t product;
const char *description;
const struct uatp_parameters *parameters;
};
struct uatp_parameters {
unsigned int x_ratio;
unsigned int x_sensors;
unsigned int x_sensors_17;
unsigned int y_ratio;
unsigned int y_sensors;
unsigned int input_size;
void (*initialize)(struct uatp_softc *);
void (*finalize)(struct uatp_softc *);
bool (*base_sample)(const struct uatp_softc *, const uint8_t *);
void (*read_sample)(uint8_t *, uint8_t *, const uint8_t *);
void (*accumulate)(struct uatp_softc *);
void (*reset)(struct uatp_softc *);
};
static const struct uatp_parameters fountain_parameters = {
.x_ratio = 64, .x_sensors = 16, .x_sensors_17 = 26,
.y_ratio = 43, .y_sensors = 16,
.input_size = 81,
.initialize = NULL,
.finalize = NULL,
.base_sample = base_sample_softc_flag,
.read_sample = read_sample_1,
.accumulate = accumulate_sample_1,
.reset = NULL,
};
static const struct uatp_parameters geyser_1_parameters = {
.x_ratio = 64, .x_sensors = 16, .x_sensors_17 = 26,
.y_ratio = 43, .y_sensors = 16,
.input_size = 81,
.initialize = NULL,
.finalize = NULL,
.base_sample = base_sample_softc_flag,
.read_sample = read_sample_1,
.accumulate = accumulate_sample_1,
.reset = NULL,
};
static const struct uatp_parameters geyser_2_parameters = {
.x_ratio = 64, .x_sensors = 15, .x_sensors_17 = 20,
.y_ratio = 43, .y_sensors = 9,
.input_size = 64,
.initialize = NULL,
.finalize = NULL,
.base_sample = base_sample_softc_flag,
.read_sample = read_sample_2,
.accumulate = accumulate_sample_1,
.reset = NULL,
};
static const struct uatp_parameters geyser_3_4_parameters = {
.x_ratio = 64, .x_sensors = 20, .x_sensors_17 = 0,
.y_ratio = 64, .y_sensors = 9,
.input_size = 63,
.initialize = geyser34_initialize,
.finalize = geyser34_finalize,
.base_sample = base_sample_input_flag,
.read_sample = read_sample_2,
.accumulate = accumulate_sample_2,
.reset = geyser34_deferred_reset,
};
#define APPLE_TRACKPAD(PRODUCT, DESCRIPTION, PARAMETERS) \
{ \
.vendor = USB_VENDOR_APPLE, \
.product = (PRODUCT), \
.description = "Apple " DESCRIPTION " trackpad", \
.parameters = (& (PARAMETERS)), \
}
#define POWERBOOK_TRACKPAD(PRODUCT, PARAMETERS) \
APPLE_TRACKPAD(PRODUCT, "PowerBook/iBook", PARAMETERS)
#define MACBOOK_TRACKPAD(PRODUCT, PARAMETERS) \
APPLE_TRACKPAD(PRODUCT, "MacBook/MacBook Pro", PARAMETERS)
static const struct uatp_descriptor uatp_descriptors[] =
{
POWERBOOK_TRACKPAD(0x020e, fountain_parameters),
POWERBOOK_TRACKPAD(0x020f, fountain_parameters),
POWERBOOK_TRACKPAD(0x030a, fountain_parameters),
POWERBOOK_TRACKPAD(0x030b, geyser_1_parameters),
POWERBOOK_TRACKPAD(0x0214, geyser_2_parameters),
POWERBOOK_TRACKPAD(0x0215, geyser_2_parameters),
POWERBOOK_TRACKPAD(0x0216, geyser_2_parameters),
MACBOOK_TRACKPAD(0x0217, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x0218, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x0219, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x021a, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x021b, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x021c, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x0229, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x022a, geyser_3_4_parameters),
MACBOOK_TRACKPAD(0x022b, geyser_3_4_parameters),
};
#undef MACBOOK_TRACKPAD
#undef POWERBOOK_TRACKPAD
#undef APPLE_TRACKPAD
static const struct uatp_descriptor *
find_uatp_descriptor(const struct uhidev_attach_arg *uha)
{
unsigned int i;
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];
return NULL;
}
static device_t
uatp_dev(const struct uatp_softc *sc)
{
return sc->sc_dev;
}
static uint8_t *
uatp_x_sample(struct uatp_softc *sc)
{
return &sc->sc_sample[0];
}
static uint8_t *
uatp_y_sample(struct uatp_softc *sc)
{
return &sc->sc_sample[UATP_MAX_X_SENSORS];
}
static int *
uatp_x_acc(struct uatp_softc *sc)
{
return &sc->sc_acc[0];
}
static int *
uatp_y_acc(struct uatp_softc *sc)
{
return &sc->sc_acc[UATP_MAX_X_SENSORS];
}
static void
uatp_clear_position(struct uatp_softc *sc)
{
memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
sc->sc_motion_timer = 0;
sc->sc_x_raw = sc->sc_x_smoothed = -1;
sc->sc_y_raw = sc->sc_y_smoothed = -1;
sc->sc_z_raw = sc->sc_z_smoothed = -1;
sc->sc_w_raw = sc->sc_w_smoothed = -1;
sc->sc_x_remainder = 0;
sc->sc_y_remainder = 0;
sc->sc_z_remainder = 0;
sc->sc_w_remainder = 0;
sc->sc_track_distance = 0;
}
static unsigned int
uatp_x_sensors(const struct uatp_softc *sc)
{
if ((0 < sc->sc_knobs.x_sensors) &&
(sc->sc_knobs.x_sensors <= UATP_MAX_X_SENSORS))
return sc->sc_knobs.x_sensors;
else
return sc->sc_parameters->x_sensors;
}
static unsigned int
uatp_y_sensors(const struct uatp_softc *sc)
{
if ((0 < sc->sc_knobs.y_sensors) &&
(sc->sc_knobs.y_sensors <= UATP_MAX_Y_SENSORS))
return sc->sc_knobs.y_sensors;
else
return sc->sc_parameters->y_sensors;
}
static unsigned int
uatp_x_ratio(const struct uatp_softc *sc)
{
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)
{
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)
{
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)
{
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)
{
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;
if (((*multiplier) == 0) ||
((*multiplier) > UATP_MAX_MOTION_MULTIPLIER) ||
((*divisor) == 0))
DPRINTF(sc, UATP_DEBUG_ACCEL,
("bad knobs; %d (+ %d) --> %d, rem 0\n",
delta, *remainder, (delta + (*remainder))));
else
DPRINTF(sc, UATP_DEBUG_ACCEL,
("scale %d (+ %d) by %u/%u --> %d, rem %d\n",
delta, *remainder,
(*multiplier), (*divisor),
(((delta + (*remainder)) * ((int) (*multiplier)))
/ ((int) (*divisor))),
(((delta + (*remainder)) * ((int) (*multiplier)))
% ((int) (*divisor)))));
if (sc->sc_knobs.motion_remainder)
delta += *remainder;
*remainder = 0;
if (((*multiplier) == 0) ||
((*multiplier) > UATP_MAX_MOTION_MULTIPLIER) ||
((*divisor) == 0))
return delta;
product = (delta * ((int) (*multiplier)));
*remainder = (product % ((int) (*divisor)));
return product / ((int) (*divisor));
}
static int
uatp_scale_motion(const struct uatp_softc *sc, int delta, int *remainder)
{
return scale_motion(sc, delta, remainder,
&sc->sc_knobs.motion_multiplier,
&sc->sc_knobs.motion_divisor);
}
static int
uatp_scale_fast_motion(const struct uatp_softc *sc, int delta, int *remainder)
{
return scale_motion(sc, delta, remainder,
&sc->sc_knobs.fast_motion_multiplier,
&sc->sc_knobs.fast_motion_divisor);
}
CFATTACH_DECL2_NEW(uatp, sizeof(struct uatp_softc), uatp_match, uatp_attach,
uatp_detach, uatp_activate, NULL, uatp_childdet);
static const struct wsmouse_accessops uatp_accessops = {
.enable = uatp_enable,
.disable = uatp_disable,
.ioctl = uatp_ioctl,
};
static int
uatp_match(device_t parent, cfdata_t match, void *aux)
{
const struct uhidev_attach_arg *uha = aux;
void *report_descriptor;
int report_size, input_size;
const struct uatp_descriptor *uatp_descriptor;
aprint_debug("%s: vendor 0x%04x, product 0x%04x\n", __func__,
(unsigned int)uha->uiaa->uiaa_vendor,
(unsigned int)uha->uiaa->uiaa_product);
aprint_debug("%s: class 0x%04x, subclass 0x%04x, proto 0x%04x\n",
__func__,
(unsigned int)uha->uiaa->uiaa_class,
(unsigned int)uha->uiaa->uiaa_subclass,
(unsigned int)uha->uiaa->uiaa_proto);
uhidev_get_report_desc(uha->parent, &report_descriptor, &report_size);
input_size = hid_report_size(report_descriptor, report_size,
hid_input, uha->reportid);
aprint_debug("%s: reportid %d, input size %d\n", __func__,
(int)uha->reportid, input_size);
if (uha->uiaa->uiaa_proto != UIPROTO_MOUSE)
return UMATCH_NONE;
uatp_descriptor = find_uatp_descriptor(uha);
if (uatp_descriptor == NULL) {
aprint_debug("%s: unknown vendor/product id\n", __func__);
return UMATCH_NONE;
}
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;
}
return UMATCH_VENDOR_PRODUCT_CONF_IFACE;
}
static void
uatp_attach(device_t parent, device_t self, void *aux)
{
struct uatp_softc *sc = device_private(self);
const struct uhidev_attach_arg *uha = aux;
const struct uatp_descriptor *uatp_descriptor;
void *report_descriptor;
int report_size, input_size;
struct wsmousedev_attach_args a;
sc->sc_dev = self;
sc->sc_hdev = uha->parent;
sc->sc_udev = uha->uiaa->uiaa_device;
uatp_descriptor = find_uatp_descriptor(uha);
KASSERT(uatp_descriptor != NULL);
aprint_normal(": %s\n", uatp_descriptor->description);
aprint_naive(": %s\n", uatp_descriptor->description);
aprint_verbose_dev(self,
"vendor 0x%04x, product 0x%04x, report id %d\n",
(unsigned int)uha->uiaa->uiaa_vendor,
(unsigned int)uha->uiaa->uiaa_product,
uha->reportid);
uhidev_get_report_desc(uha->parent, &report_descriptor, &report_size);
input_size = hid_report_size(report_descriptor, report_size, hid_input,
uha->reportid);
KASSERT(0 < input_size);
sc->sc_input_size = input_size;
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);
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
sc->sc_knobs = default_knobs;
uatp_setup_sysctl(sc);
tap_initialize(sc);
a.accessops = &uatp_accessops;
a.accesscookie = sc;
sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
}
static void
uatp_setup_sysctl(struct uatp_softc *sc)
{
int error;
error = sysctl_createv(&sc->sc_log, 0, NULL, &sc->sc_node, 0,
CTLTYPE_NODE, device_xname(uatp_dev(sc)),
SYSCTL_DESCR("uatp configuration knobs"),
NULL, 0, NULL, 0,
CTL_HW, CTL_CREATE, CTL_EOL);
if (error != 0) {
aprint_error_dev(uatp_dev(sc),
"unable to set up sysctl tree hw.%s: %d\n",
device_xname(uatp_dev(sc)), error);
goto err;
}
#if UATP_DEBUG
if (!uatp_setup_sysctl_knob(sc, &sc->sc_debug_flags, "debug",
"uatp(4) debug flags"))
goto err;
#endif
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
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
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;
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;
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;
return;
err:
sysctl_teardown(&sc->sc_log);
sc->sc_node = NULL;
}
static bool
uatp_setup_sysctl_knob(struct uatp_softc *sc, int *ptr, const char *name,
const char *description)
{
int error;
error = sysctl_createv(&sc->sc_log, 0, NULL, NULL, CTLFLAG_READWRITE,
CTLTYPE_INT, name, SYSCTL_DESCR(description),
NULL, 0, ptr, 0,
CTL_HW, sc->sc_node->sysctl_num, CTL_CREATE, CTL_EOL);
if (error != 0) {
aprint_error_dev(uatp_dev(sc),
"unable to setup sysctl node hw.%s.%s: %d\n",
device_xname(uatp_dev(sc)), name, error);
return false;
}
return true;
}
static void
uatp_childdet(device_t self, device_t child)
{
struct uatp_softc *sc = device_private(self);
DPRINTF(sc, UATP_DEBUG_MISC, ("detaching child %s\n",
device_xname(child)));
if (child == sc->sc_wsmousedev)
sc->sc_wsmousedev = NULL;
}
static int
uatp_detach(device_t self, int flags)
{
struct uatp_softc *sc = device_private(self);
int error;
DPRINTF(sc, UATP_DEBUG_MISC, ("detaching with flags %d\n", flags));
error = config_detach_children(self, flags);
if (error)
return error;
KASSERT((sc->sc_status & UATP_ENABLED) == 0);
if (sc->sc_parameters->finalize)
sc->sc_parameters->finalize(sc);
pmf_device_deregister(self);
sysctl_teardown(&sc->sc_log);
sc->sc_node = NULL;
tap_finalize(sc);
return 0;
}
static int
uatp_activate(device_t self, enum devact act)
{
struct uatp_softc *sc = device_private(self);
DPRINTF(sc, UATP_DEBUG_MISC, ("act %d\n", (int)act));
if (act != DVACT_DEACTIVATE)
return EOPNOTSUPP;
sc->sc_status |= UATP_DYING;
return 0;
}
static int
uatp_enable(void *v)
{
struct uatp_softc *sc = v;
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("enabling wsmouse\n"));
if (sc->sc_status & UATP_DYING) {
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("busy dying\n"));
return EIO;
}
if (sc->sc_status & UATP_ENABLED) {
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("already enabled\n"));
return EBUSY;
}
sc->sc_status |= UATP_ENABLED;
sc->sc_status &=~ UATP_VALID;
sc->sc_input_index = 0;
tap_enable(sc);
uatp_clear_position(sc);
DPRINTF(sc, UATP_DEBUG_MISC, ("uhidev_open(%p)\n", sc->sc_hdev));
return uhidev_open(sc->sc_hdev, &uatp_intr, sc);
}
static void
uatp_disable(void *v)
{
struct uatp_softc *sc = v;
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("disabling wsmouse\n"));
if (!(sc->sc_status & UATP_ENABLED)) {
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("not enabled\n"));
return;
}
tap_disable(sc);
sc->sc_status &=~ UATP_ENABLED;
DPRINTF(sc, UATP_DEBUG_MISC, ("uhidev_close(%p)\n", sc->sc_hdev));
uhidev_close(sc->sc_hdev);
}
static int
uatp_ioctl(void *v, unsigned long cmd, void *data, int flag, struct lwp *p)
{
DPRINTF((struct uatp_softc*)v, UATP_DEBUG_IOCTL,
("cmd %lx, data %p, flag %x, lwp %p\n", cmd, data, flag, p));
return EPASSTHROUGH;
}
#define GEYSER34_RAW_MODE 0x04
#define GEYSER34_MODE_REPORT_ID 0
#define GEYSER34_MODE_INTERFACE 0
#define GEYSER34_MODE_PACKET_SIZE 8
static void
geyser34_enable_raw_mode(struct uatp_softc *sc)
{
uint8_t report[GEYSER34_MODE_PACKET_SIZE];
usbd_status status;
DPRINTF(sc, UATP_DEBUG_RESET, ("get feature report\n"));
status = usbd_get_report(sc->sc_iface0, UHID_FEATURE_REPORT,
GEYSER34_MODE_REPORT_ID, report, sizeof(report));
if (status != USBD_NORMAL_COMPLETION) {
aprint_error_dev(uatp_dev(sc),
"error reading feature report: %s\n", usbd_errstr(status));
return;
}
#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");
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
report[0] = GEYSER34_RAW_MODE;
DPRINTF(sc, UATP_DEBUG_RESET, ("set feature report\n"));
status = usbd_set_report(sc->sc_iface0, UHID_FEATURE_REPORT,
GEYSER34_MODE_REPORT_ID, report, sizeof(report));
if (status != USBD_NORMAL_COMPLETION) {
aprint_error_dev(uatp_dev(sc),
"error writing feature report: %s\n", usbd_errstr(status));
return;
}
}
static void
geyser34_initialize(struct uatp_softc *sc)
{
usbd_status err __diagused;
DPRINTF(sc, UATP_DEBUG_MISC, ("initializing\n"));
err = usbd_device2interface_handle(sc->sc_udev, 0, &sc->sc_iface0);
KASSERT(err == 0);
geyser34_enable_raw_mode(sc);
usb_init_task(&sc->sc_reset_task, &geyser34_reset_task, sc, 0);
}
static void
geyser34_finalize(struct uatp_softc *sc)
{
DPRINTF(sc, UATP_DEBUG_MISC, ("finalizing\n"));
usb_rem_task_wait(sc->sc_udev, &sc->sc_reset_task, USB_TASKQ_DRIVER,
NULL);
}
static void
geyser34_deferred_reset(struct uatp_softc *sc)
{
DPRINTF(sc, UATP_DEBUG_RESET, ("deferring reset\n"));
usb_add_task(sc->sc_udev, &sc->sc_reset_task, USB_TASKQ_DRIVER);
}
static void
geyser34_reset_task(void *arg)
{
struct uatp_softc *sc = arg;
DPRINTF(sc, UATP_DEBUG_RESET, ("resetting\n"));
geyser34_enable_raw_mode(sc);
}
static void
uatp_intr(void *cookie, void *ibuf, unsigned int len)
{
struct uatp_softc *sc = cookie;
uint8_t *input;
int dx, dy, dz, dw;
uint32_t buttons;
DPRINTF(sc, UATP_DEBUG_INTR, ("softc %p, ibuf %p, len %u\n",
sc, ibuf, len));
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) {
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) {
DPRINTF(sc, UATP_DEBUG_INTR, ("partial packet: %u bytes\n",
len));
return;
}
sc->sc_input_index = 0;
input = sc->sc_input;
buttons = ((input[sc->sc_input_size - 1] & UATP_STATUS_BUTTON)
? 1 : 0);
memset(uatp_x_sample(sc), 0, UATP_MAX_X_SENSORS);
memset(uatp_y_sample(sc), 0, UATP_MAX_Y_SENSORS);
sc->sc_parameters->read_sample(uatp_x_sample(sc), uatp_y_sample(sc),
input);
#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 (sc->sc_parameters->base_sample(sc, input)) {
DPRINTF(sc, UATP_DEBUG_PARSE,
("base sample, buttons %"PRIx32"\n", buttons));
sc->sc_status |= UATP_VALID;
uatp_clear_position(sc);
memcpy(sc->sc_base, sc->sc_sample, sizeof(sc->sc_base));
return;
}
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
dx = dy = dz = dw = 0;
if ((!interpret_input(sc, &dx, &dy, &dz, &dw, &buttons)) &&
(sc->sc_buttons == 0)) {
DPRINTF(sc, UATP_DEBUG_SPURINTR, ("spurious interrupt\n"));
if (sc->sc_parameters->reset)
sc->sc_parameters->reset(sc);
return;
}
DPRINTF(sc, UATP_DEBUG_INTR,
("buttons %"PRIx32", dx %d, dy %d, dz %d, dw %d\n",
buttons, dx, dy, dz, dw));
mutex_enter(&sc->sc_tap_mutex);
uatp_input(sc, buttons, dx, dy, dz, dw);
mutex_exit(&sc->sc_tap_mutex);
}
static bool
base_sample_softc_flag(const struct uatp_softc *sc, const uint8_t *input)
{
return !(sc->sc_status & UATP_VALID);
}
static bool
base_sample_input_flag(const struct uatp_softc *sc, const uint8_t *input)
{
return !!(input[sc->sc_input_size - 1] & UATP_STATUS_BASE);
}
static void
read_sample_1(uint8_t *x, uint8_t *y, const uint8_t *input)
{
unsigned int i;
for (i = 0; i < 8; i++) {
x[i] = input[5 * i + 2];
x[i + 8] = input[5 * i + 4];
x[i + 16] = input[5 * i + 42];
if (i < 2)
x[i + 24] = input[5 * i + 44];
y[i] = input[5 * i + 1];
y[i + 8] = input[5 * i + 3];
}
}
static void
read_sample_2(uint8_t *x, uint8_t *y, const uint8_t *input)
{
unsigned int i, j;
for (i = 0, j = 19; i < 20; i += 2, j += 3) {
x[i] = input[j];
x[i + 1] = input[j + 1];
}
for (i = 0, j = 1; i < 9; i += 2, j += 3) {
y[i] = input[j];
y[i + 1] = input[j + 1];
}
}
static void
accumulate_sample_1(struct uatp_softc *sc)
{
unsigned int i;
for (i = 0; i < UATP_SENSORS; i++) {
sc->sc_acc[i] += (int8_t)(sc->sc_sample[i] - sc->sc_base[i]);
if (sc->sc_acc[i] < 0) {
sc->sc_acc[i] = 0;
} else if (UATP_MAX_ACC < sc->sc_acc[i]) {
DPRINTF(sc, UATP_DEBUG_ACCUMULATE,
("overflow %d\n", sc->sc_acc[i]));
sc->sc_acc[i] = UATP_MAX_ACC;
}
}
memcpy(sc->sc_base, sc->sc_sample, sizeof(sc->sc_base));
}
static void
accumulate_sample_2(struct uatp_softc *sc)
{
unsigned int i;
for (i = 0; i < UATP_SENSORS; i++) {
sc->sc_acc[i] = (int8_t)(sc->sc_sample[i] - sc->sc_base[i]);
if (sc->sc_acc[i] < -0x80) {
DPRINTF(sc, UATP_DEBUG_ACCUMULATE,
("underflow %u - %u = %d\n",
(unsigned int)sc->sc_sample[i],
(unsigned int)sc->sc_base[i],
sc->sc_acc[i]));
sc->sc_acc[i] += 0x100;
}
if (0x7f < sc->sc_acc[i]) {
DPRINTF(sc, UATP_DEBUG_ACCUMULATE,
("overflow %u - %u = %d\n",
(unsigned int)sc->sc_sample[i],
(unsigned int)sc->sc_base[i],
sc->sc_acc[i]));
sc->sc_acc[i] -= 0x100;
}
if (sc->sc_acc[i] < 0)
sc->sc_acc[i] = 0;
}
}
static void
uatp_input(struct uatp_softc *sc, uint32_t buttons,
int dx, int dy, int dz, int dw)
{
uint32_t all_buttons;
KASSERT(mutex_owned(&sc->sc_tap_mutex));
all_buttons = buttons | uatp_tapped_buttons(sc);
if ((sc->sc_wsmousedev != NULL) &&
((dx != 0) || (dy != 0) || (dz != 0) || (dw != 0) ||
(all_buttons != sc->sc_all_buttons))) {
int s = spltty();
DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("wsmouse input:"
" buttons %"PRIx32", dx %d, dy %d, dz %d, dw %d\n",
all_buttons, dx, -dy, dz, -dw));
wsmouse_input(sc->sc_wsmousedev, all_buttons, dx, -dy, dz, -dw,
WSMOUSE_INPUT_DELTA);
splx(s);
}
sc->sc_buttons = buttons;
sc->sc_all_buttons = all_buttons;
}
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;
}
default:
aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
__func__, sc->sc_tap_state);
return 0;
}
}
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;
x_pressure = interpret_dimension(sc, uatp_x_acc(sc),
uatp_x_sensors(sc), uatp_x_ratio(sc), &x_raw, &x_fingers);
y_pressure = interpret_dimension(sc, uatp_y_acc(sc),
uatp_y_sensors(sc), uatp_y_ratio(sc), &y_raw, &y_fingers);
DPRINTF(sc, UATP_DEBUG_PARSE,
("x %u @ %u, %uf; y %u @ %u, %uf; buttons %"PRIx32"\n",
x_pressure, x_raw, x_fingers,
y_pressure, y_raw, y_fingers,
*buttons));
if ((x_pressure == 0) && (y_pressure == 0)) {
bool ok;
DPRINTF(sc, UATP_DEBUG_INTR,
("no position detected; clearing position\n"));
if (*buttons == 0) {
ok = tap_released(sc);
} else {
tap_reset(sc);
ok = true;
}
uatp_clear_position(sc);
return ok;
} else if ((x_pressure == 0) || (y_pressure == 0)) {
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
*buttons = emulated_buttons(sc, fingers);
update_position(sc, fingers, x_raw, y_raw, dx, dy, dz, dw);
return true;
} else {
DPRINTF(sc, UATP_DEBUG_INTR, ("palm detected; ignoring\n"));
return true;
}
}
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;
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)
palm_width = UATP_MAX_POSITION;
#define CHECK_(condition) CHECK(condition, return 0)
__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);
n_fingers = 0;
memset(weighted, 0, sizeof(weighted));
memset(total, 0, sizeof(total));
for (i = 0; i < n_sensors; i++) {
CHECK_(0 <= acc[i]);
v = acc[i];
if (v <= sensor_threshold) {
state = none;
continue;
} else if (UATP_MAX_ACC < v) {
aprint_verbose_dev(uatp_dev(sc),
"ignoring large accumulated sensor state: %u\n",
v);
continue;
}
switch (state) {
case none:
n_fingers += 1;
CHECK_(n_fingers <= n_sensors);
state = nondecreasing;
width = 1;
break;
case nondecreasing:
case decreasing:
CHECK_(0 < i);
CHECK_(0 <= acc[i - 1]);
width += 1;
if (palm_width <= (width * ratio)) {
DPRINTF(sc, UATP_DEBUG_PALM,
("palm detected\n"));
return 2;
} else if ((state == nondecreasing) &&
((unsigned int)acc[i - 1] > v)) {
state = decreasing;
} else if ((state == decreasing) &&
((unsigned int)acc[i - 1] < v)) {
n_fingers += 1;
CHECK_(n_fingers <= n_sensors);
state = nondecreasing;
width = 1;
}
break;
default:
aprint_error_dev(uatp_dev(sc),
"bad finger detection state: %d", state);
return 0;
}
v -= sensor_normalizer;
total[n_fingers - 1] += v;
weighted[n_fingers - 1] += (i * v);
CHECK_(total[n_fingers - 1] <=
(UATP_MAX_SENSORS * UATP_MAX_ACC));
CHECK_(weighted[n_fingers - 1] <=
(UATP_MAX_SENSORS * UATP_MAX_SENSORS * UATP_MAX_ACC));
}
if (n_fingers == 0)
return 0;
sum = 0;
for (i = 0; i < n_fingers; i++) {
DPRINTF(sc, UATP_DEBUG_PARSE,
("finger at %u\n", ((weighted[i] * ratio) / total[i])));
sum += ((weighted[i] * ratio) / total[i]);
CHECK_(sum <= UATP_MAX_SENSORS * UATP_MAX_POSITION);
}
*fingers = n_fingers;
*position = (sum / n_fingers);
return 1;
#undef CHECK_
}
static void
uatp_tap_limit(const struct uatp_softc *sc, struct timeval *limit)
{
unsigned int msec = sc->sc_knobs.tap_limit_msec;
limit->tv_sec = 0;
limit->tv_usec = ((msec < 1000) ? (1000 * msec) : 100000);
}
#if UATP_DEBUG
# define TAP_DEBUG_PRE(sc) tap_debug((sc), __func__, "")
# define TAP_DEBUG_POST(sc) tap_debug((sc), __func__, " ->")
static void
tap_debug(struct uatp_softc *sc, const char *caller, const char *prefix)
{
char buffer[128];
const char *state;
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;
}
DPRINTF(sc, UATP_DEBUG_TAP,
("%s:%s state %s, %u tapping, %u tapped\n",
caller, prefix, state,
sc->sc_tapping_fingers, sc->sc_tapped_fingers));
}
#else
# define TAP_DEBUG_PRE(sc) do {} while (0)
# define TAP_DEBUG_POST(sc) do {} while (0)
#endif
static void
tap_initialize(struct uatp_softc *sc)
{
callout_init(&sc->sc_untap_callout, 0);
callout_setfunc(&sc->sc_untap_callout, untap_callout, sc);
mutex_init(&sc->sc_tap_mutex, MUTEX_DEFAULT, IPL_SOFTUSB);
}
static void
tap_finalize(struct uatp_softc *sc)
{
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;
sc->sc_all_buttons = 0;
mutex_exit(&sc->sc_tap_mutex);
}
static void
tap_disable(struct uatp_softc *sc)
{
tap_reset_wait(sc);
}
static void
tap_reset(struct uatp_softc *sc)
{
callout_stop(&sc->sc_untap_callout);
mutex_enter(&sc->sc_tap_mutex);
tap_transition_initial(sc);
mutex_exit(&sc->sc_tap_mutex);
}
static void
tap_reset_wait(struct uatp_softc *sc)
{
callout_halt(&sc->sc_untap_callout, NULL);
mutex_enter(&sc->sc_tap_mutex);
tap_transition_initial(sc);
mutex_exit(&sc->sc_tap_mutex);
}
static const struct timeval zero_timeval;
static void
tap_transition(struct uatp_softc *sc, enum uatp_tap_state tap_state,
const struct timeval *start_time,
unsigned int tapping_fingers, unsigned int tapped_fingers)
{
KASSERT(mutex_owned(&sc->sc_tap_mutex));
sc->sc_tap_state = tap_state;
sc->sc_tap_timer = *start_time;
sc->sc_tapping_fingers = tapping_fingers;
sc->sc_tapped_fingers = tapped_fingers;
}
static void
tap_transition_initial(struct uatp_softc *sc)
{
tap_transition(sc, TAP_STATE_INITIAL, &zero_timeval, 0, 0);
}
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_dragging_down(struct uatp_softc *sc)
{
CHECK((0 < sc->sc_tapped_fingers),
do { tap_transition_initial(sc); return; } while (0));
tap_transition(sc, TAP_STATE_DRAGGING_DOWN, &zero_timeval, 0,
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);
}
static void
tap_transition_tapped(struct uatp_softc *sc, const struct timeval *start_time)
{
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);
}
static void
tap_transition_dragging_up(struct uatp_softc *sc)
{
CHECK((0 < sc->sc_tapped_fingers),
do { tap_transition_initial(sc); return; } while (0));
tap_transition(sc, TAP_STATE_DRAGGING_UP, &zero_timeval, 0,
sc->sc_tapped_fingers);
}
static void
tap_touched(struct uatp_softc *sc, unsigned int fingers)
{
struct timeval now, diff, limit;
CHECK((0 < fingers), return);
callout_stop(&sc->sc_untap_callout);
mutex_enter(&sc->sc_tap_mutex);
TAP_DEBUG_PRE(sc);
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:
tap_transition_tapping(sc, &sc->sc_tap_timer, fingers);
break;
case TAP_STATE_TAPPED:
getmicrouptime(&now);
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:
tap_transition_tapping_in_drag(sc, &sc->sc_tap_timer, fingers);
break;
default:
aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
__func__, sc->sc_tap_state);
tap_transition_initial(sc);
break;
}
TAP_DEBUG_POST(sc);
mutex_exit(&sc->sc_tap_mutex);
}
static bool
tap_released(struct uatp_softc *sc)
{
struct timeval now, diff, limit;
void (*non_tapped_transition)(struct uatp_softc *);
bool ok, temporary_release;
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:
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) {
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;
}
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
aprint_error_dev(uatp_dev(sc),
"double-tap delay too long: %ums\n", ms);
}
static void
untap_callout(void *arg)
{
struct uatp_softc *sc = arg;
mutex_enter(&sc->sc_tap_mutex);
TAP_DEBUG_PRE(sc);
switch (sc->sc_tap_state) {
case TAP_STATE_TAPPED:
tap_transition_initial(sc);
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:
break;
default:
aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
__func__, sc->sc_tap_state);
tap_transition_initial(sc);
uatp_input(sc, 0, 0, 0, 0, 0);
break;
}
TAP_DEBUG_POST(sc);
mutex_exit(&sc->sc_tap_mutex);
}
static uint32_t
emulated_buttons(struct uatp_softc *sc, unsigned int fingers)
{
CHECK((1 < fingers), return 0);
switch (fingers) {
case 2:
DPRINTF(sc, UATP_DEBUG_EMUL_BUTTON,
("2-finger emulated button: %"PRIx32"\n",
sc->sc_knobs.two_finger_buttons));
return sc->sc_knobs.two_finger_buttons;
case 3:
default:
DPRINTF(sc, UATP_DEBUG_EMUL_BUTTON,
("3-finger emulated button: %"PRIx32"\n",
sc->sc_knobs.three_finger_buttons));
return sc->sc_knobs.three_finger_buttons;
}
}
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);
if ((fingers == 1) || (sc->sc_knobs.multifinger_track == 1))
move_mouse(sc, x_raw, y_raw, dx, dy);
else if (sc->sc_knobs.multifinger_track == 2)
scroll_wheel(sc, x_raw, y_raw, dz, dw);
}
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;
__CTASSERT(0x12000000 == (2 * UATP_MAX_POSITION * UATP_MAX_POSITION));
CHECK_(a <= UATP_MAX_POSITION);
CHECK_(b <= UATP_MAX_POSITION);
*a_raw = a;
*b_raw = b;
if ((old_a_raw < 0) || (old_b_raw < 0)) {
DPRINTF(sc, UATP_DEBUG_MOVE,
("initialize %s position (%d, %d) -> (%d, %d)\n", ctx,
old_a_raw, old_b_raw, a, b));
return;
}
if ((old_a_smoothed < 0) || (old_b_smoothed < 0)) {
old_a_smoothed = old_a_raw;
old_b_smoothed = old_b_raw;
}
CHECK_(0 <= old_a_raw);
CHECK_(0 <= old_b_raw);
CHECK_(old_a_raw <= UATP_MAX_POSITION);
CHECK_(old_b_raw <= UATP_MAX_POSITION);
CHECK_(0 <= old_a_smoothed);
CHECK_(0 <= old_b_smoothed);
CHECK_(old_a_smoothed <= UATP_MAX_POSITION);
CHECK_(old_b_smoothed <= UATP_MAX_POSITION);
CHECK_(0 <= *a_raw);
CHECK_(0 <= *b_raw);
CHECK_(*a_raw <= UATP_MAX_POSITION);
CHECK_(*b_raw <= UATP_MAX_POSITION);
*a_smoothed = smooth(sc, old_a_raw, old_a_smoothed, *a_raw);
*b_smoothed = smooth(sc, old_b_raw, old_b_smoothed, *b_raw);
CHECK_(0 <= *a_smoothed);
CHECK_(0 <= *b_smoothed);
CHECK_(*a_smoothed <= UATP_MAX_POSITION);
CHECK_(*b_smoothed <= UATP_MAX_POSITION);
if (sc->sc_motion_timer < sc->sc_knobs.motion_delay) {
DPRINTF(sc, UATP_DEBUG_MOVE, ("delay motion %u\n",
sc->sc_motion_timer));
sc->sc_motion_timer += 1;
return;
}
if (*a_smoothed < old_a_smoothed)
a_dist = old_a_smoothed - *a_smoothed;
else
a_dist = *a_smoothed - old_a_smoothed;
if (*b_smoothed < old_b_smoothed)
b_dist = old_b_smoothed - *b_smoothed;
else
b_dist = *b_smoothed - old_b_smoothed;
dist_squared = (a_dist * a_dist) + (b_dist * b_dist);
if (dist_squared < ((2 * UATP_MAX_POSITION * UATP_MAX_POSITION)
- sc->sc_track_distance))
sc->sc_track_distance += dist_squared;
else
sc->sc_track_distance = (2 * UATP_MAX_POSITION *
UATP_MAX_POSITION);
DPRINTF(sc, UATP_DEBUG_TRACK_DIST, ("finger has tracked %u units^2\n",
sc->sc_track_distance));
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)
__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;
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)
__CTASSERT(0x30000 ==
(UATP_MAX_POSITION * UATP_MAX_MOTION_MULTIPLIER));
CHECK_(old_raw <= UATP_MAX_POSITION);
CHECK_(raw <= UATP_MAX_POSITION);
CHECK_(old_smoothed <= UATP_MAX_POSITION);
CHECK_(smoothed <= UATP_MAX_POSITION);
return (fast ? uatp_scale_fast_motion : uatp_scale_motion)
(sc, (((int) smoothed) - ((int) old_smoothed)), remainder);
#undef CHECK_
}
MODULE(MODULE_CLASS_DRIVER, uatp, NULL);
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
uatp_modcmd(modcmd_t cmd, void *aux)
{
int error = 0;
switch (cmd) {
case MODULE_CMD_INIT:
#ifdef _MODULE
error = config_init_component(cfdriver_ioconf_uatp,
cfattach_ioconf_uatp, cfdata_ioconf_uatp);
#endif
return error;
case MODULE_CMD_FINI:
#ifdef _MODULE
error = config_fini_component(cfdriver_ioconf_uatp,
cfattach_ioconf_uatp, cfdata_ioconf_uatp);
#endif
return error;
default:
return ENOTTY;
}
}