Home | History | Annotate | Line # | Download | only in usb
uatp.c revision 1.10.2.1.4.3
      1  1.10.2.1.4.3     skrll /*	$NetBSD: uatp.c,v 1.10.2.1.4.3 2017/03/31 09:38:05 skrll Exp $	*/
      2           1.1  riastrad 
      3           1.1  riastrad /*-
      4           1.7  riastrad  * Copyright (c) 2011-2014 The NetBSD Foundation, Inc.
      5           1.1  riastrad  * All rights reserved.
      6           1.1  riastrad  *
      7           1.7  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8           1.7  riastrad  * by Taylor R. Campbell.
      9           1.7  riastrad  *
     10           1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11           1.1  riastrad  * modification, are permitted provided that the following conditions
     12           1.1  riastrad  * are met:
     13           1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14           1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15           1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16           1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17           1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18           1.1  riastrad  *
     19           1.7  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20           1.7  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21           1.7  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22           1.7  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23           1.7  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24           1.7  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25           1.7  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26           1.7  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27           1.7  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28           1.7  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29           1.7  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30           1.1  riastrad  */
     31           1.1  riastrad 
     32           1.1  riastrad /*
     33           1.1  riastrad  * uatp(4) - USB Apple Trackpad
     34           1.1  riastrad  *
     35           1.1  riastrad  * The uatp driver talks the protocol of the USB trackpads found in
     36           1.1  riastrad  * Apple laptops since 2005, including PowerBooks, iBooks, MacBooks,
     37           1.1  riastrad  * and MacBook Pros.  Some of these also present generic USB HID mice
     38           1.1  riastrad  * on another USB report id, which the ums(4) driver can handle, but
     39           1.1  riastrad  * Apple's protocol gives more detailed sensor data that lets us detect
     40           1.1  riastrad  * multiple fingers to emulate multi-button mice and scroll wheels.
     41           1.1  riastrad  */
     42  1.10.2.1.4.1     skrll 
     43           1.1  riastrad /*
     44           1.1  riastrad  * Protocol
     45           1.1  riastrad  *
     46           1.1  riastrad  * The device has a set of horizontal sensors, each being a column at a
     47           1.1  riastrad  * particular position on the x axis that tells you whether there is
     48           1.1  riastrad  * pressure anywhere on that column, and vertical sensors, each being a
     49           1.1  riastrad  * row at a particular position on the y axis that tells you whether
     50           1.1  riastrad  * there is pressure anywhere on that row.
     51           1.1  riastrad  *
     52           1.1  riastrad  * Whenever the device senses anything, it emits a readout of all of
     53           1.1  riastrad  * the sensors, in some model-dependent order.  (For the order, see
     54           1.1  riastrad  * read_sample_1 and read_sample_2.)  Each sensor datum is an unsigned
     55           1.1  riastrad  * eight-bit quantity representing some measure of pressure.  (Of
     56           1.1  riastrad  * course, it really measures capacitance, not pressure, but we'll call
     57           1.1  riastrad  * it `pressure' here.)
     58           1.1  riastrad  */
     59           1.1  riastrad 
     60           1.1  riastrad /*
     61           1.1  riastrad  * Interpretation
     62           1.1  riastrad  *
     63           1.1  riastrad  * To interpret the finger's position on the trackpad, the driver
     64           1.1  riastrad  * computes a weighted average over all possible positions, weighted by
     65           1.1  riastrad  * the pressure at that position.  The weighted average is computed in
     66           1.1  riastrad  * the dimensions of the screen, rather than the trackpad, in order to
     67           1.1  riastrad  * admit a finer resolution of positions than the trackpad grid.
     68           1.1  riastrad  *
     69           1.1  riastrad  * To update the finger's position smoothly on the trackpad, the driver
     70           1.1  riastrad  * computes a weighted average of the old raw position, the old
     71           1.1  riastrad  * smoothed position, and the new smoothed position.  The weights are
     72           1.1  riastrad  * given by the old_raw_weight, old_smoothed_weight, and new_raw_weight
     73           1.1  riastrad  * sysctl knobs.
     74           1.1  riastrad  *
     75           1.1  riastrad  * Finally, to move the cursor, the driver takes the difference between
     76           1.1  riastrad  * the old and new positions and accelerates it according to some
     77           1.1  riastrad  * heuristic knobs that need to be reworked.
     78           1.1  riastrad  *
     79           1.1  riastrad  * Finally, there are some bells & whistles to detect tapping and to
     80           1.1  riastrad  * emulate a three-button mouse by leaving two or three fingers on the
     81           1.1  riastrad  * trackpad while pressing the button.
     82           1.1  riastrad  */
     83           1.1  riastrad 
     84           1.1  riastrad /*
     85           1.1  riastrad  * Future work
     86           1.1  riastrad  *
     87           1.1  riastrad  * With the raw sensor data available, we could implement fancier bells
     88           1.1  riastrad  * & whistles too, such as pinch-to-zoom.  However, wsmouse supports
     89           1.1  riastrad  * only four-dimensional mice with buttons, and we already use two
     90           1.1  riastrad  * dimensions for mousing and two dimensions for scrolling, so there's
     91           1.1  riastrad  * no straightforward way to report zooming and other gestures to the
     92           1.1  riastrad  * operating system.  Probably a better way to do this would be just to
     93           1.1  riastrad  * attach uhid(4) instead of uatp(4) and to read the raw sensors data
     94           1.1  riastrad  * yourself -- but that requires hairy mode switching for recent models
     95           1.1  riastrad  * (see geyser34_enable_raw_mode).
     96           1.1  riastrad  *
     97           1.1  riastrad  * XXX Rework the acceleration knobs.
     98           1.1  riastrad  * XXX Implement edge scrolling.
     99           1.1  riastrad  * XXX Fix sysctl setup; preserve knobs across suspend/resume.
    100           1.1  riastrad  *     (uatp0 detaches and reattaches across suspend/resume, so as
    101           1.1  riastrad  *     written, the sysctl tree is torn down and rebuilt, losing any
    102           1.1  riastrad  *     state the user may have set.)
    103           1.1  riastrad  * XXX Refactor motion state so I can understand it again.
    104           1.1  riastrad  *     Should make a struct uatp_motion for all that state.
    105           1.1  riastrad  * XXX Add hooks for ignoring trackpad input while typing.
    106           1.1  riastrad  */
    107  1.10.2.1.4.1     skrll 
    108           1.1  riastrad /*
    109           1.1  riastrad  * Classifying devices
    110           1.1  riastrad  *
    111           1.1  riastrad  * I have only one MacBook to test this driver, but the driver should
    112           1.1  riastrad  * be applicable to almost every Apple laptop made since the beginning
    113           1.1  riastrad  * of 2005, so the driver reports lots of debugging output to help to
    114           1.1  riastrad  * classify devices.  Boot with `boot -v' (verbose) and check the
    115           1.1  riastrad  * output of `dmesg | grep uatp' to answer the following questions:
    116           1.1  riastrad  *
    117           1.1  riastrad  * - What devices (vendor, product, class, subclass, proto, USB HID
    118           1.1  riastrad  *   report dump) fail to attach when you think they should work?
    119           1.1  riastrad  *     (vendor not apple, class not hid, proto not mouse)
    120           1.1  riastrad  *
    121           1.1  riastrad  * - What devices have an unknown product id?
    122           1.1  riastrad  *     `unknown vendor/product id'
    123           1.1  riastrad  *
    124           1.1  riastrad  * - What devices have the wrong screen-to-trackpad ratios?
    125           1.1  riastrad  *     `... x sensors, scaled by ... for ... points on screen'
    126           1.1  riastrad  *     `... y sensors, scaled by ... for ... points on screen'
    127           1.1  riastrad  *   You can tweak hw.uatp0.x_ratio and hw.uatp0.y_ratio to adjust
    128           1.1  riastrad  *   this, up to a maximum of 384 for each value.
    129           1.1  riastrad  *
    130           1.1  riastrad  * - What devices have the wrong input size?
    131           1.1  riastrad  *     `expected input size ... but got ... for Apple trackpad'
    132           1.1  riastrad  *
    133           1.1  riastrad  * - What devices give wrong-sized packets?
    134           1.1  riastrad  *     `discarding ...-byte input'
    135           1.1  riastrad  *
    136           1.1  riastrad  * - What devices split packets in chunks?
    137           1.1  riastrad  *     `partial packet: ... bytes'
    138           1.1  riastrad  *
    139           1.1  riastrad  * - What devices develop large sensor readouts?
    140           1.1  riastrad  *     `large sensor readout: ...'
    141           1.1  riastrad  *
    142           1.1  riastrad  * - What devices have the wrong number of sensors?  Are there parts of
    143           1.1  riastrad  *   your trackpad that the system doesn't seem to notice?  You can
    144           1.1  riastrad  *   tweak hw.uatp0.x_sensors and hw.uatp0.y_sensors, up to a maximum
    145           1.1  riastrad  *   of 32 for each value.
    146           1.1  riastrad  */
    147  1.10.2.1.4.1     skrll 
    148           1.1  riastrad #include <sys/cdefs.h>
    149  1.10.2.1.4.3     skrll __KERNEL_RCSID(0, "$NetBSD: uatp.c,v 1.10.2.1.4.3 2017/03/31 09:38:05 skrll Exp $");
    150  1.10.2.1.4.2     skrll 
    151  1.10.2.1.4.2     skrll #ifdef _KERNEL_OPT
    152  1.10.2.1.4.2     skrll #include "opt_usb.h"
    153  1.10.2.1.4.2     skrll #endif
    154           1.1  riastrad 
    155           1.6  riastrad #include <sys/types.h>
    156           1.6  riastrad #include <sys/param.h>
    157           1.1  riastrad #include <sys/atomic.h>
    158           1.1  riastrad #include <sys/device.h>
    159           1.1  riastrad #include <sys/errno.h>
    160           1.1  riastrad #include <sys/ioctl.h>
    161           1.6  riastrad #include <sys/kernel.h>
    162           1.9  riastrad #include <sys/module.h>
    163           1.1  riastrad #include <sys/sysctl.h>
    164           1.1  riastrad #include <sys/systm.h>
    165           1.1  riastrad #include <sys/time.h>
    166           1.1  riastrad 
    167           1.1  riastrad /* Order is important here...sigh...  */
    168           1.1  riastrad #include <dev/usb/usb.h>
    169           1.1  riastrad #include <dev/usb/usbdi.h>
    170           1.1  riastrad #include <dev/usb/usbdi_util.h>
    171           1.1  riastrad #include <dev/usb/usbdevs.h>
    172           1.1  riastrad #include <dev/usb/uhidev.h>
    173           1.1  riastrad #include <dev/usb/hid.h>
    174           1.1  riastrad #include <dev/usb/usbhid.h>
    175           1.1  riastrad 
    176           1.1  riastrad #include <dev/wscons/wsconsio.h>
    177           1.1  riastrad #include <dev/wscons/wsmousevar.h>
    178           1.1  riastrad 
    179           1.1  riastrad #define CHECK(condition, fail) do {					\
    180           1.1  riastrad 	if (! (condition)) {						\
    181           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "%s: check failed: %s\n",\
    182           1.1  riastrad 			__func__, #condition);				\
    183           1.1  riastrad 		fail;							\
    184           1.1  riastrad 	}								\
    185           1.1  riastrad } while (0)
    186  1.10.2.1.4.1     skrll 
    187           1.1  riastrad #define UATP_DEBUG_ATTACH	(1 << 0)
    188           1.1  riastrad #define UATP_DEBUG_MISC		(1 << 1)
    189           1.1  riastrad #define UATP_DEBUG_WSMOUSE	(1 << 2)
    190           1.1  riastrad #define UATP_DEBUG_IOCTL	(1 << 3)
    191           1.1  riastrad #define UATP_DEBUG_RESET	(1 << 4)
    192           1.1  riastrad #define UATP_DEBUG_INTR		(1 << 5)
    193           1.1  riastrad #define UATP_DEBUG_PARSE	(1 << 6)
    194           1.1  riastrad #define UATP_DEBUG_TAP		(1 << 7)
    195           1.1  riastrad #define UATP_DEBUG_EMUL_BUTTON	(1 << 8)
    196           1.1  riastrad #define UATP_DEBUG_ACCUMULATE	(1 << 9)
    197           1.1  riastrad #define UATP_DEBUG_STATUS	(1 << 10)
    198           1.1  riastrad #define UATP_DEBUG_SPURINTR	(1 << 11)
    199           1.1  riastrad #define UATP_DEBUG_MOVE		(1 << 12)
    200           1.1  riastrad #define UATP_DEBUG_ACCEL	(1 << 13)
    201           1.1  riastrad #define UATP_DEBUG_TRACK_DIST	(1 << 14)
    202           1.1  riastrad #define UATP_DEBUG_PALM		(1 << 15)
    203           1.1  riastrad 
    204           1.1  riastrad #if UATP_DEBUG
    205           1.1  riastrad #  define DPRINTF(sc, flags, format) do {				\
    206           1.1  riastrad 	if ((flags) & (sc)->sc_debug_flags) {				\
    207           1.1  riastrad 		printf("%s: %s: ", device_xname(uatp_dev(sc)), __func__); \
    208           1.1  riastrad 		printf format;						\
    209           1.1  riastrad 	}								\
    210           1.1  riastrad } while (0)
    211           1.1  riastrad #else
    212           1.1  riastrad #  define DPRINTF(sc, flags, format) do {} while (0)
    213           1.1  riastrad #endif
    214           1.1  riastrad 
    215           1.1  riastrad /* Maximum number of bytes in an incoming packet of sensor data.  */
    216           1.1  riastrad #define UATP_MAX_INPUT_SIZE	81
    217           1.1  riastrad 
    218           1.1  riastrad /* Maximum number of sensors in each dimension.  */
    219           1.1  riastrad #define UATP_MAX_X_SENSORS	32
    220           1.1  riastrad #define UATP_MAX_Y_SENSORS	32
    221           1.1  riastrad #define UATP_MAX_SENSORS	32
    222           1.1  riastrad #define UATP_SENSORS		(UATP_MAX_X_SENSORS + UATP_MAX_Y_SENSORS)
    223           1.1  riastrad 
    224           1.1  riastrad /* Maximum accumulated sensor value.  */
    225           1.1  riastrad #define UATP_MAX_ACC		0xff
    226           1.1  riastrad 
    227           1.1  riastrad /* Maximum screen dimension to sensor dimension ratios.  */
    228           1.1  riastrad #define UATP_MAX_X_RATIO	0x180
    229           1.1  riastrad #define UATP_MAX_Y_RATIO	0x180
    230           1.1  riastrad #define UATP_MAX_RATIO		0x180
    231           1.1  riastrad 
    232           1.1  riastrad /* Maximum weight for positions in motion calculation.  */
    233           1.1  riastrad #define UATP_MAX_WEIGHT		0x7f
    234           1.1  riastrad 
    235           1.1  riastrad /* Maximum possible trackpad position in a single dimension.  */
    236           1.1  riastrad #define UATP_MAX_POSITION	(UATP_MAX_SENSORS * UATP_MAX_RATIO)
    237           1.1  riastrad 
    238           1.1  riastrad /* Bounds on acceleration.  */
    239           1.1  riastrad #define UATP_MAX_MOTION_MULTIPLIER	16
    240           1.1  riastrad 
    241           1.1  riastrad /* Status bits transmitted in the last byte of an input packet.  */
    242           1.1  riastrad #define UATP_STATUS_BUTTON	(1 << 0)	/* Button pressed */
    243           1.1  riastrad #define UATP_STATUS_BASE	(1 << 2)	/* Base sensor data */
    244           1.1  riastrad #define UATP_STATUS_POST_RESET	(1 << 4)	/* Post-reset */
    245  1.10.2.1.4.1     skrll 
    246           1.1  riastrad /* Forward declarations */
    247           1.1  riastrad 
    248           1.1  riastrad struct uatp_softc;		/* Device driver state.  */
    249           1.1  riastrad struct uatp_descriptor;		/* Descriptor for a particular model.  */
    250           1.1  riastrad struct uatp_parameters;		/* Parameters common to a set of models.  */
    251           1.1  riastrad struct uatp_knobs;		/* User-settable configuration knobs.  */
    252           1.1  riastrad enum uatp_tap_state {
    253           1.1  riastrad 	TAP_STATE_INITIAL,
    254           1.1  riastrad 	TAP_STATE_TAPPING,
    255           1.1  riastrad 	TAP_STATE_TAPPED,
    256           1.1  riastrad 	TAP_STATE_DOUBLE_TAPPING,
    257           1.1  riastrad 	TAP_STATE_DRAGGING_DOWN,
    258           1.1  riastrad 	TAP_STATE_DRAGGING_UP,
    259           1.1  riastrad 	TAP_STATE_TAPPING_IN_DRAG,
    260           1.1  riastrad };
    261           1.1  riastrad 
    262           1.1  riastrad static const struct uatp_descriptor *find_uatp_descriptor
    263           1.1  riastrad     (const struct uhidev_attach_arg *);
    264           1.1  riastrad static device_t uatp_dev(const struct uatp_softc *);
    265           1.1  riastrad static uint8_t *uatp_x_sample(struct uatp_softc *);
    266           1.1  riastrad static uint8_t *uatp_y_sample(struct uatp_softc *);
    267           1.1  riastrad static int *uatp_x_acc(struct uatp_softc *);
    268           1.1  riastrad static int *uatp_y_acc(struct uatp_softc *);
    269           1.1  riastrad static void uatp_clear_position(struct uatp_softc *);
    270           1.1  riastrad static unsigned int uatp_x_sensors(const struct uatp_softc *);
    271           1.1  riastrad static unsigned int uatp_y_sensors(const struct uatp_softc *);
    272           1.1  riastrad static unsigned int uatp_x_ratio(const struct uatp_softc *);
    273           1.1  riastrad static unsigned int uatp_y_ratio(const struct uatp_softc *);
    274           1.1  riastrad static unsigned int uatp_old_raw_weight(const struct uatp_softc *);
    275           1.1  riastrad static unsigned int uatp_old_smoothed_weight(const struct uatp_softc *);
    276           1.1  riastrad static unsigned int uatp_new_raw_weight(const struct uatp_softc *);
    277           1.1  riastrad static int scale_motion(const struct uatp_softc *, int, int *,
    278           1.1  riastrad     const unsigned int *, const unsigned int *);
    279           1.1  riastrad static int uatp_scale_motion(const struct uatp_softc *, int, int *);
    280           1.1  riastrad static int uatp_scale_fast_motion(const struct uatp_softc *, int, int *);
    281           1.1  riastrad static int uatp_match(device_t, cfdata_t, void *);
    282           1.1  riastrad static void uatp_attach(device_t, device_t, void *);
    283           1.1  riastrad static void uatp_setup_sysctl(struct uatp_softc *);
    284           1.1  riastrad static bool uatp_setup_sysctl_knob(struct uatp_softc *, int *, const char *,
    285           1.1  riastrad     const char *);
    286           1.1  riastrad static void uatp_childdet(device_t, device_t);
    287           1.1  riastrad static int uatp_detach(device_t, int);
    288           1.1  riastrad static int uatp_activate(device_t, enum devact);
    289           1.1  riastrad static int uatp_enable(void *);
    290           1.1  riastrad static void uatp_disable(void *);
    291           1.1  riastrad static int uatp_ioctl(void *, unsigned long, void *, int, struct lwp *);
    292           1.1  riastrad static void geyser34_enable_raw_mode(struct uatp_softc *);
    293           1.1  riastrad static void geyser34_initialize(struct uatp_softc *);
    294           1.1  riastrad static int geyser34_finalize(struct uatp_softc *);
    295           1.1  riastrad static void geyser34_deferred_reset(struct uatp_softc *);
    296           1.8  riastrad static void geyser34_reset_task(void *);
    297           1.1  riastrad static void uatp_intr(struct uhidev *, void *, unsigned int);
    298           1.1  riastrad static bool base_sample_softc_flag(const struct uatp_softc *, const uint8_t *);
    299           1.1  riastrad static bool base_sample_input_flag(const struct uatp_softc *, const uint8_t *);
    300           1.1  riastrad static void read_sample_1(uint8_t *, uint8_t *, const uint8_t *);
    301           1.1  riastrad static void read_sample_2(uint8_t *, uint8_t *, const uint8_t *);
    302           1.1  riastrad static void accumulate_sample_1(struct uatp_softc *);
    303           1.1  riastrad static void accumulate_sample_2(struct uatp_softc *);
    304           1.1  riastrad static void uatp_input(struct uatp_softc *, uint32_t, int, int, int, int);
    305           1.1  riastrad static uint32_t uatp_tapped_buttons(struct uatp_softc *);
    306           1.1  riastrad static bool interpret_input(struct uatp_softc *, int *, int *, int *, int *,
    307           1.1  riastrad     uint32_t *);
    308           1.1  riastrad static unsigned int interpret_dimension(struct uatp_softc *, const int *,
    309           1.1  riastrad     unsigned int, unsigned int, unsigned int *, unsigned int *);
    310           1.1  riastrad static void tap_initialize(struct uatp_softc *);
    311           1.1  riastrad static void tap_finalize(struct uatp_softc *);
    312           1.1  riastrad static void tap_enable(struct uatp_softc *);
    313           1.1  riastrad static void tap_disable(struct uatp_softc *);
    314           1.1  riastrad static void tap_transition(struct uatp_softc *, enum uatp_tap_state,
    315           1.1  riastrad     const struct timeval *, unsigned int, unsigned int);
    316           1.1  riastrad static void tap_transition_initial(struct uatp_softc *);
    317           1.1  riastrad static void tap_transition_tapping(struct uatp_softc *, const struct timeval *,
    318           1.1  riastrad     unsigned int);
    319           1.1  riastrad static void tap_transition_double_tapping(struct uatp_softc *,
    320           1.1  riastrad     const struct timeval *, unsigned int);
    321           1.1  riastrad static void tap_transition_dragging_down(struct uatp_softc *);
    322           1.1  riastrad static void tap_transition_tapping_in_drag(struct uatp_softc *,
    323           1.1  riastrad     const struct timeval *, unsigned int);
    324           1.1  riastrad static void tap_transition_tapped(struct uatp_softc *, const struct timeval *);
    325           1.1  riastrad static void tap_transition_dragging_up(struct uatp_softc *);
    326           1.1  riastrad static void tap_reset(struct uatp_softc *);
    327           1.1  riastrad static void tap_reset_wait(struct uatp_softc *);
    328           1.1  riastrad static void tap_touched(struct uatp_softc *, unsigned int);
    329           1.1  riastrad static bool tap_released(struct uatp_softc *);
    330           1.1  riastrad static void schedule_untap(struct uatp_softc *);
    331           1.1  riastrad static void untap_callout(void *);
    332           1.1  riastrad static uint32_t emulated_buttons(struct uatp_softc *, unsigned int);
    333           1.1  riastrad static void update_position(struct uatp_softc *, unsigned int,
    334           1.1  riastrad     unsigned int, unsigned int, int *, int *, int *, int *);
    335           1.1  riastrad static void move_mouse(struct uatp_softc *, unsigned int, unsigned int,
    336           1.1  riastrad     int *, int *);
    337           1.1  riastrad static void scroll_wheel(struct uatp_softc *, unsigned int, unsigned int,
    338           1.1  riastrad     int *, int *);
    339           1.1  riastrad static void move(struct uatp_softc *, const char *, unsigned int, unsigned int,
    340           1.1  riastrad     int *, int *, int *, int *, unsigned int *, unsigned int *, int *, int *);
    341           1.1  riastrad static int smooth(struct uatp_softc *, unsigned int, unsigned int,
    342           1.1  riastrad     unsigned int);
    343           1.1  riastrad static bool motion_below_threshold(struct uatp_softc *, unsigned int,
    344           1.1  riastrad     int, int);
    345           1.1  riastrad static int accelerate(struct uatp_softc *, unsigned int, unsigned int,
    346           1.1  riastrad     unsigned int, unsigned int, bool, int *);
    347  1.10.2.1.4.1     skrll 
    348           1.1  riastrad struct uatp_knobs {
    349           1.1  riastrad 	/*
    350           1.1  riastrad 	 * Button emulation.  What do we do when two or three fingers
    351           1.1  riastrad 	 * are on the trackpad when the user presses the button?
    352           1.1  riastrad 	 */
    353           1.1  riastrad 	unsigned int two_finger_buttons;
    354           1.1  riastrad 	unsigned int three_finger_buttons;
    355           1.1  riastrad 
    356           1.1  riastrad #if 0
    357           1.1  riastrad 	/*
    358           1.1  riastrad 	 * Edge scrolling.
    359           1.1  riastrad 	 *
    360           1.1  riastrad 	 * XXX Implement this.  What units should these be in?
    361           1.1  riastrad 	 */
    362           1.1  riastrad 	unsigned int top_edge;
    363           1.1  riastrad 	unsigned int bottom_edge;
    364           1.1  riastrad 	unsigned int left_edge;
    365           1.1  riastrad 	unsigned int right_edge;
    366           1.1  riastrad #endif
    367           1.1  riastrad 
    368           1.1  riastrad 	/*
    369           1.1  riastrad 	 * Multifinger tracking.  What do we do with multiple fingers?
    370           1.1  riastrad 	 * 0. Ignore them.
    371           1.1  riastrad 	 * 1. Try to interpret them as ordinary mousing.
    372           1.1  riastrad 	 * 2. Act like a two-dimensional scroll wheel.
    373           1.1  riastrad 	 */
    374           1.1  riastrad 	unsigned int multifinger_track;
    375           1.1  riastrad 
    376           1.1  riastrad 	/*
    377           1.1  riastrad 	 * Sensor parameters.
    378           1.1  riastrad 	 */
    379           1.1  riastrad 	unsigned int x_sensors;
    380           1.1  riastrad 	unsigned int x_ratio;
    381           1.1  riastrad 	unsigned int y_sensors;
    382           1.1  riastrad 	unsigned int y_ratio;
    383           1.1  riastrad 	unsigned int sensor_threshold;
    384           1.1  riastrad 	unsigned int sensor_normalizer;
    385           1.1  riastrad 	unsigned int palm_width;
    386           1.1  riastrad 	unsigned int old_raw_weight;
    387           1.1  riastrad 	unsigned int old_smoothed_weight;
    388           1.1  riastrad 	unsigned int new_raw_weight;
    389           1.1  riastrad 
    390           1.1  riastrad 	/*
    391           1.1  riastrad 	 * Motion parameters.
    392           1.1  riastrad 	 *
    393           1.1  riastrad 	 * XXX There should be a more principled model of acceleration.
    394           1.1  riastrad 	 */
    395           1.1  riastrad 	unsigned int motion_remainder;
    396           1.1  riastrad 	unsigned int motion_threshold;
    397           1.1  riastrad 	unsigned int motion_multiplier;
    398           1.1  riastrad 	unsigned int motion_divisor;
    399           1.1  riastrad 	unsigned int fast_motion_threshold;
    400           1.1  riastrad 	unsigned int fast_motion_multiplier;
    401           1.1  riastrad 	unsigned int fast_motion_divisor;
    402           1.1  riastrad 	unsigned int fast_per_direction;
    403           1.1  riastrad 	unsigned int motion_delay;
    404           1.1  riastrad 
    405           1.1  riastrad 	/*
    406           1.1  riastrad 	 * Tapping.
    407           1.1  riastrad 	 */
    408           1.1  riastrad 	unsigned int tap_limit_msec;
    409           1.1  riastrad 	unsigned int double_tap_limit_msec;
    410           1.1  riastrad 	unsigned int one_finger_tap_buttons;
    411           1.1  riastrad 	unsigned int two_finger_tap_buttons;
    412           1.1  riastrad 	unsigned int three_finger_tap_buttons;
    413           1.1  riastrad 	unsigned int tap_track_distance_limit;
    414           1.1  riastrad };
    415  1.10.2.1.4.1     skrll 
    416           1.1  riastrad static const struct uatp_knobs default_knobs = {
    417           1.1  riastrad 	/*
    418           1.1  riastrad 	 * Button emulation.  Fingers on the trackpad don't change it
    419           1.1  riastrad 	 * by default -- it's still the left button.
    420           1.1  riastrad 	 *
    421           1.1  riastrad 	 * XXX The left button should have a name.
    422           1.1  riastrad 	 */
    423           1.1  riastrad 	 .two_finger_buttons	= 1,
    424           1.1  riastrad 	 .three_finger_buttons	= 1,
    425           1.1  riastrad 
    426           1.1  riastrad #if 0
    427           1.1  riastrad 	/*
    428           1.1  riastrad 	 * Edge scrolling.  Off by default.
    429           1.1  riastrad 	 */
    430           1.1  riastrad 	.top_edge		= 0,
    431           1.1  riastrad 	.bottom_edge		= 0,
    432           1.1  riastrad 	.left_edge		= 0,
    433           1.1  riastrad 	.right_edge		= 0,
    434           1.1  riastrad #endif
    435           1.1  riastrad 
    436           1.1  riastrad 	/*
    437           1.1  riastrad 	 * Multifinger tracking.  Ignore by default.
    438           1.1  riastrad 	 */
    439           1.1  riastrad 	 .multifinger_track	= 0,
    440           1.1  riastrad 
    441           1.1  riastrad 	/*
    442           1.1  riastrad 	 * Sensor parameters.
    443           1.1  riastrad 	 */
    444           1.1  riastrad 	.x_sensors		= 0,	/* default for model */
    445           1.1  riastrad 	.x_ratio		= 0,	/* default for model */
    446           1.1  riastrad 	.y_sensors		= 0,	/* default for model */
    447           1.1  riastrad 	.y_ratio		= 0,	/* default for model */
    448           1.1  riastrad 	.sensor_threshold	= 5,
    449           1.1  riastrad 	.sensor_normalizer	= 5,
    450           1.1  riastrad 	.palm_width		= 0,	/* palm detection disabled */
    451           1.1  riastrad 	.old_raw_weight		= 0,
    452           1.1  riastrad 	.old_smoothed_weight	= 5,
    453           1.1  riastrad 	.new_raw_weight		= 1,
    454           1.1  riastrad 
    455           1.1  riastrad 	/*
    456           1.1  riastrad 	 * Motion parameters.
    457           1.1  riastrad 	 */
    458           1.1  riastrad 	.motion_remainder	= 1,
    459           1.1  riastrad 	.motion_threshold	= 0,
    460           1.1  riastrad 	.motion_multiplier	= 1,
    461           1.1  riastrad 	.motion_divisor		= 1,
    462           1.1  riastrad 	.fast_motion_threshold	= 10,
    463           1.1  riastrad 	.fast_motion_multiplier	= 3,
    464           1.1  riastrad 	.fast_motion_divisor	= 2,
    465           1.1  riastrad 	.fast_per_direction	= 0,
    466           1.1  riastrad 	.motion_delay		= 4,
    467           1.1  riastrad 
    468           1.1  riastrad 	/*
    469           1.1  riastrad 	 * Tapping.  Disabled by default, with a reasonable time set
    470           1.1  riastrad 	 * nevertheless so that you can just set the buttons to enable
    471           1.1  riastrad 	 * it.
    472           1.1  riastrad 	 */
    473           1.1  riastrad 	.tap_limit_msec			= 100,
    474           1.1  riastrad 	.double_tap_limit_msec		= 200,
    475           1.1  riastrad 	.one_finger_tap_buttons		= 0,
    476           1.1  riastrad 	.two_finger_tap_buttons		= 0,
    477           1.1  riastrad 	.three_finger_tap_buttons	= 0,
    478           1.1  riastrad 	.tap_track_distance_limit	= 200,
    479           1.1  riastrad };
    480  1.10.2.1.4.1     skrll 
    481           1.1  riastrad struct uatp_softc {
    482           1.1  riastrad 	struct uhidev sc_hdev;		/* USB parent.  */
    483           1.1  riastrad 	device_t sc_wsmousedev;		/* Attached wsmouse device.  */
    484           1.1  riastrad 	const struct uatp_parameters *sc_parameters;
    485           1.1  riastrad 	struct uatp_knobs sc_knobs;
    486           1.1  riastrad 	struct sysctllog *sc_log;	/* Log for sysctl knobs.  */
    487           1.1  riastrad 	const struct sysctlnode *sc_node;	/* Our sysctl node.  */
    488           1.1  riastrad 	unsigned int sc_input_size;	/* Input packet size.  */
    489           1.1  riastrad 	uint8_t sc_input[UATP_MAX_INPUT_SIZE];	/* Buffer for a packet.   */
    490           1.1  riastrad 	unsigned int sc_input_index;	/* Current index into sc_input.  */
    491           1.1  riastrad 	int sc_acc[UATP_SENSORS];	/* Accumulated sensor state.  */
    492           1.1  riastrad 	uint8_t sc_base[UATP_SENSORS];	/* Base sample.  */
    493           1.1  riastrad 	uint8_t sc_sample[UATP_SENSORS];/* Current sample.  */
    494           1.1  riastrad 	unsigned int sc_motion_timer;	/* XXX describe; motion_delay  */
    495           1.1  riastrad 	int sc_x_raw;			/* Raw horiz. mouse position.  */
    496           1.1  riastrad 	int sc_y_raw;			/* Raw vert. mouse position.  */
    497           1.1  riastrad 	int sc_z_raw;			/* Raw horiz. scroll position.  */
    498           1.1  riastrad 	int sc_w_raw;			/* Raw vert. scroll position.  */
    499           1.1  riastrad 	int sc_x_smoothed;		/* Smoothed horiz. mouse position.  */
    500           1.1  riastrad 	int sc_y_smoothed;		/* Smoothed vert. mouse position.  */
    501           1.1  riastrad 	int sc_z_smoothed;		/* Smoothed horiz. scroll position.  */
    502           1.1  riastrad 	int sc_w_smoothed;		/* Smoothed vert. scroll position.  */
    503           1.1  riastrad 	int sc_x_remainder;		/* Remainders from acceleration.  */
    504           1.1  riastrad 	int sc_y_remainder;
    505           1.1  riastrad 	int sc_z_remainder;
    506           1.1  riastrad 	int sc_w_remainder;
    507           1.1  riastrad 	unsigned int sc_track_distance;	/* Distance^2 finger has tracked,
    508           1.1  riastrad 					 * squared to avoid sqrt in kernel.  */
    509           1.1  riastrad 	uint32_t sc_status;		/* Status flags:  */
    510           1.1  riastrad #define UATP_ENABLED	(1 << 0)	/* . Is the wsmouse enabled?  */
    511           1.1  riastrad #define UATP_DYING	(1 << 1)	/* . Have we been deactivated?  */
    512           1.1  riastrad #define UATP_VALID	(1 << 2)	/* . Do we have valid sensor data?  */
    513           1.8  riastrad 	struct usb_task sc_reset_task;	/* Task for resetting device.  */
    514           1.1  riastrad 
    515           1.1  riastrad 	callout_t sc_untap_callout;	/* Releases button after tap.  */
    516           1.1  riastrad 	kmutex_t sc_tap_mutex;		/* Protects the following fields.  */
    517           1.1  riastrad 	kcondvar_t sc_tap_cv;		/* Signalled by untap callout.  */
    518           1.1  riastrad 	enum uatp_tap_state sc_tap_state;	/* Current tap state.  */
    519           1.1  riastrad 	unsigned int sc_tapping_fingers;	/* No. fingers tapping.  */
    520           1.1  riastrad 	unsigned int sc_tapped_fingers;	/* No. fingers of last tap.  */
    521           1.1  riastrad 	struct timeval sc_tap_timer;	/* Timer for tap state transitions.  */
    522           1.1  riastrad 	uint32_t sc_buttons;		/* Physical buttons pressed.  */
    523           1.1  riastrad 	uint32_t sc_all_buttons;	/* Buttons pressed or tapped.  */
    524           1.1  riastrad 
    525           1.1  riastrad #if UATP_DEBUG
    526           1.1  riastrad 	uint32_t sc_debug_flags;	/* Debugging output enabled.  */
    527           1.1  riastrad #endif
    528           1.1  riastrad };
    529  1.10.2.1.4.1     skrll 
    530           1.1  riastrad struct uatp_descriptor {
    531           1.1  riastrad 	uint16_t vendor;
    532           1.1  riastrad 	uint16_t product;
    533           1.1  riastrad 	const char *description;
    534           1.1  riastrad 	const struct uatp_parameters *parameters;
    535           1.1  riastrad };
    536           1.1  riastrad 
    537           1.1  riastrad struct uatp_parameters {
    538           1.1  riastrad 	unsigned int x_ratio;		/* Screen width / trackpad width.  */
    539           1.1  riastrad 	unsigned int x_sensors;		/* Number of horizontal sensors.  */
    540           1.1  riastrad 	unsigned int x_sensors_17;	/* XXX Same, on a 17" laptop.  */
    541           1.1  riastrad 	unsigned int y_ratio;		/* Screen height / trackpad height.  */
    542           1.1  riastrad 	unsigned int y_sensors;		/* Number of vertical sensors.  */
    543           1.1  riastrad 	unsigned int input_size;	/* Size in bytes of input packets.  */
    544           1.1  riastrad 
    545           1.1  riastrad 	/* Device-specific initialization routine.  May be null.  */
    546           1.1  riastrad 	void (*initialize)(struct uatp_softc *);
    547           1.1  riastrad 
    548           1.1  riastrad 	/* Device-specific finalization routine.  May be null.  May fail.  */
    549           1.1  riastrad 	int (*finalize)(struct uatp_softc *);
    550           1.1  riastrad 
    551           1.1  riastrad 	/* Tests whether this is a base sample.  Second argument is
    552           1.1  riastrad 	 * input_size bytes long.  */
    553           1.1  riastrad 	bool (*base_sample)(const struct uatp_softc *, const uint8_t *);
    554           1.1  riastrad 
    555           1.1  riastrad 	/* Reads a sensor sample from an input packet.  First argument
    556           1.1  riastrad 	 * is UATP_MAX_X_SENSORS bytes long; second, UATP_MAX_Y_SENSORS
    557           1.1  riastrad 	 * bytes; third, input_size bytes.  */
    558           1.1  riastrad 	void (*read_sample)(uint8_t *, uint8_t *, const uint8_t *);
    559           1.1  riastrad 
    560           1.1  riastrad 	/* Accumulates sensor state in sc->sc_acc.  */
    561           1.1  riastrad 	void (*accumulate)(struct uatp_softc *);
    562           1.1  riastrad 
    563           1.1  riastrad 	/* Called on spurious interrupts to reset.  May be null.  */
    564           1.1  riastrad 	void (*reset)(struct uatp_softc *);
    565           1.1  riastrad };
    566  1.10.2.1.4.1     skrll 
    567           1.1  riastrad /* Known device parameters */
    568           1.1  riastrad 
    569           1.1  riastrad static const struct uatp_parameters fountain_parameters = {
    570           1.1  riastrad 	.x_ratio	= 64,	.x_sensors = 16,	.x_sensors_17 = 26,
    571           1.1  riastrad 	.y_ratio	= 43,	.y_sensors = 16,
    572           1.1  riastrad 	.input_size	= 81,
    573           1.1  riastrad 	.initialize	= NULL,
    574           1.1  riastrad 	.finalize	= NULL,
    575           1.1  riastrad 	.base_sample	= base_sample_softc_flag,
    576           1.1  riastrad 	.read_sample	= read_sample_1,
    577           1.1  riastrad 	.accumulate	= accumulate_sample_1,
    578           1.1  riastrad 	.reset		= NULL,
    579           1.1  riastrad };
    580           1.1  riastrad 
    581           1.1  riastrad static const struct uatp_parameters geyser_1_parameters = {
    582           1.1  riastrad 	.x_ratio	= 64,	.x_sensors = 16,	.x_sensors_17 = 26,
    583           1.1  riastrad 	.y_ratio	= 43,	.y_sensors = 16,
    584           1.1  riastrad 	.input_size	= 81,
    585           1.1  riastrad 	.initialize	= NULL,
    586           1.1  riastrad 	.finalize	= NULL,
    587           1.1  riastrad 	.base_sample	= base_sample_softc_flag,
    588           1.1  riastrad 	.read_sample	= read_sample_1,
    589           1.1  riastrad 	.accumulate	= accumulate_sample_1,
    590           1.1  riastrad 	.reset		= NULL,
    591           1.1  riastrad };
    592           1.1  riastrad 
    593           1.1  riastrad static const struct uatp_parameters geyser_2_parameters = {
    594           1.1  riastrad 	.x_ratio	= 64,	.x_sensors = 15,	.x_sensors_17 = 20,
    595           1.1  riastrad 	.y_ratio	= 43,	.y_sensors = 9,
    596           1.1  riastrad 	.input_size	= 64,
    597           1.1  riastrad 	.initialize	= NULL,
    598           1.1  riastrad 	.finalize	= NULL,
    599           1.1  riastrad 	.base_sample	= base_sample_softc_flag,
    600           1.1  riastrad 	.read_sample	= read_sample_2,
    601           1.1  riastrad 	.accumulate	= accumulate_sample_1,
    602           1.1  riastrad 	.reset		= NULL,
    603           1.1  riastrad };
    604           1.1  riastrad 
    605           1.1  riastrad /*
    606           1.1  riastrad  * The Geyser 3 and Geyser 4 share parameters.  They also present
    607           1.1  riastrad  * generic USB HID mice on a different report id, so we have smaller
    608           1.1  riastrad  * packets by one byte (uhidev handles multiplexing report ids) and
    609           1.1  riastrad  * extra initialization work to switch the mode from generic USB HID
    610           1.1  riastrad  * mouse to Apple trackpad.
    611           1.1  riastrad  */
    612           1.1  riastrad 
    613           1.1  riastrad static const struct uatp_parameters geyser_3_4_parameters = {
    614           1.1  riastrad 	.x_ratio	= 64,	.x_sensors = 20, /* XXX */ .x_sensors_17 = 0,
    615           1.1  riastrad 	.y_ratio	= 64,	.y_sensors = 9,
    616           1.1  riastrad 	.input_size	= 63,	/* 64, minus one for the report id.  */
    617           1.1  riastrad 	.initialize	= geyser34_initialize,
    618           1.1  riastrad 	.finalize	= geyser34_finalize,
    619           1.1  riastrad 	.base_sample	= base_sample_input_flag,
    620           1.1  riastrad 	.read_sample	= read_sample_2,
    621           1.1  riastrad 	.accumulate	= accumulate_sample_2,
    622           1.1  riastrad 	.reset		= geyser34_deferred_reset,
    623           1.1  riastrad };
    624  1.10.2.1.4.1     skrll 
    625           1.1  riastrad /* Known device models */
    626           1.1  riastrad 
    627           1.1  riastrad #define APPLE_TRACKPAD(PRODUCT, DESCRIPTION, PARAMETERS)		\
    628           1.1  riastrad 	{								\
    629           1.1  riastrad 		.vendor = USB_VENDOR_APPLE,				\
    630           1.1  riastrad 		.product = (PRODUCT),					\
    631           1.1  riastrad 		.description = "Apple " DESCRIPTION " trackpad",	\
    632           1.1  riastrad 		.parameters = (& (PARAMETERS)),				\
    633           1.1  riastrad 	}
    634           1.1  riastrad 
    635           1.1  riastrad #define POWERBOOK_TRACKPAD(PRODUCT, PARAMETERS)				\
    636           1.1  riastrad 	APPLE_TRACKPAD(PRODUCT, "PowerBook/iBook", PARAMETERS)
    637           1.1  riastrad #define MACBOOK_TRACKPAD(PRODUCT, PARAMETERS)				\
    638           1.1  riastrad 	APPLE_TRACKPAD(PRODUCT, "MacBook/MacBook Pro", PARAMETERS)
    639           1.1  riastrad 
    640           1.1  riastrad static const struct uatp_descriptor uatp_descriptors[] =
    641           1.1  riastrad {
    642           1.1  riastrad 	POWERBOOK_TRACKPAD(0x020e, fountain_parameters),
    643           1.1  riastrad 	POWERBOOK_TRACKPAD(0x020f, fountain_parameters),
    644           1.1  riastrad 	POWERBOOK_TRACKPAD(0x030a, fountain_parameters),
    645           1.1  riastrad 
    646           1.1  riastrad 	POWERBOOK_TRACKPAD(0x030b, geyser_1_parameters),
    647           1.1  riastrad 
    648           1.1  riastrad 	POWERBOOK_TRACKPAD(0x0214, geyser_2_parameters),
    649           1.1  riastrad 	POWERBOOK_TRACKPAD(0x0215, geyser_2_parameters),
    650           1.1  riastrad 	POWERBOOK_TRACKPAD(0x0216, geyser_2_parameters),
    651           1.1  riastrad 
    652           1.1  riastrad 	MACBOOK_TRACKPAD(0x0217, geyser_3_4_parameters), /* 3 */
    653           1.1  riastrad 	MACBOOK_TRACKPAD(0x0218, geyser_3_4_parameters), /* 3 */
    654           1.1  riastrad 	MACBOOK_TRACKPAD(0x0219, geyser_3_4_parameters), /* 3 */
    655           1.1  riastrad 
    656           1.1  riastrad 	MACBOOK_TRACKPAD(0x021a, geyser_3_4_parameters), /* 4 */
    657           1.1  riastrad 	MACBOOK_TRACKPAD(0x021b, geyser_3_4_parameters), /* 4 */
    658           1.1  riastrad 	MACBOOK_TRACKPAD(0x021c, geyser_3_4_parameters), /* 4 */
    659           1.1  riastrad 
    660           1.1  riastrad 	MACBOOK_TRACKPAD(0x0229, geyser_3_4_parameters), /* 4 */
    661           1.1  riastrad 	MACBOOK_TRACKPAD(0x022a, geyser_3_4_parameters), /* 4 */
    662           1.1  riastrad 	MACBOOK_TRACKPAD(0x022b, geyser_3_4_parameters), /* 4 */
    663           1.1  riastrad };
    664           1.1  riastrad 
    665           1.1  riastrad #undef MACBOOK_TRACKPAD
    666           1.1  riastrad #undef POWERBOOK_TRACKPAD
    667           1.1  riastrad #undef APPLE_TRACKPAD
    668  1.10.2.1.4.1     skrll 
    669           1.1  riastrad /* Miscellaneous utilities */
    670           1.1  riastrad 
    671           1.1  riastrad static const struct uatp_descriptor *
    672           1.1  riastrad find_uatp_descriptor(const struct uhidev_attach_arg *uha)
    673           1.1  riastrad {
    674           1.1  riastrad 	unsigned int i;
    675           1.1  riastrad 
    676           1.1  riastrad 	for (i = 0; i < __arraycount(uatp_descriptors); i++)
    677  1.10.2.1.4.1     skrll 		if ((uha->uiaa->uiaa_vendor == uatp_descriptors[i].vendor) &&
    678  1.10.2.1.4.1     skrll 		    (uha->uiaa->uiaa_product == uatp_descriptors[i].product))
    679           1.1  riastrad 			return &uatp_descriptors[i];
    680           1.1  riastrad 
    681           1.1  riastrad 	return NULL;
    682           1.1  riastrad }
    683           1.1  riastrad 
    684           1.1  riastrad static device_t
    685           1.1  riastrad uatp_dev(const struct uatp_softc *sc)
    686           1.1  riastrad {
    687           1.1  riastrad 	return sc->sc_hdev.sc_dev;
    688           1.1  riastrad }
    689           1.1  riastrad 
    690           1.1  riastrad static uint8_t *
    691           1.1  riastrad uatp_x_sample(struct uatp_softc *sc)
    692           1.1  riastrad {
    693           1.1  riastrad 	return &sc->sc_sample[0];
    694           1.1  riastrad }
    695           1.1  riastrad 
    696           1.1  riastrad static uint8_t *
    697           1.1  riastrad uatp_y_sample(struct uatp_softc *sc)
    698           1.1  riastrad {
    699           1.1  riastrad 	return &sc->sc_sample[UATP_MAX_X_SENSORS];
    700           1.1  riastrad }
    701           1.1  riastrad 
    702           1.1  riastrad static int *
    703           1.1  riastrad uatp_x_acc(struct uatp_softc *sc)
    704           1.1  riastrad {
    705           1.1  riastrad 	return &sc->sc_acc[0];
    706           1.1  riastrad }
    707           1.1  riastrad 
    708           1.1  riastrad static int *
    709           1.1  riastrad uatp_y_acc(struct uatp_softc *sc)
    710           1.1  riastrad {
    711           1.1  riastrad 	return &sc->sc_acc[UATP_MAX_X_SENSORS];
    712           1.1  riastrad }
    713           1.1  riastrad 
    714           1.1  riastrad static void
    715           1.1  riastrad uatp_clear_position(struct uatp_softc *sc)
    716           1.1  riastrad {
    717           1.1  riastrad 	memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
    718           1.1  riastrad 	sc->sc_motion_timer = 0;
    719           1.1  riastrad 	sc->sc_x_raw = sc->sc_x_smoothed = -1;
    720           1.1  riastrad 	sc->sc_y_raw = sc->sc_y_smoothed = -1;
    721           1.1  riastrad 	sc->sc_z_raw = sc->sc_z_smoothed = -1;
    722           1.1  riastrad 	sc->sc_w_raw = sc->sc_w_smoothed = -1;
    723           1.1  riastrad 	sc->sc_x_remainder = 0;
    724           1.1  riastrad 	sc->sc_y_remainder = 0;
    725           1.1  riastrad 	sc->sc_z_remainder = 0;
    726           1.1  riastrad 	sc->sc_w_remainder = 0;
    727           1.1  riastrad 	sc->sc_track_distance = 0;
    728           1.1  riastrad }
    729  1.10.2.1.4.1     skrll 
    730           1.1  riastrad static unsigned int
    731           1.1  riastrad uatp_x_sensors(const struct uatp_softc *sc)
    732           1.1  riastrad {
    733           1.1  riastrad 	if ((0 < sc->sc_knobs.x_sensors) &&
    734           1.1  riastrad 	    (sc->sc_knobs.x_sensors <= UATP_MAX_X_SENSORS))
    735           1.1  riastrad 		return sc->sc_knobs.x_sensors;
    736           1.1  riastrad 	else
    737           1.1  riastrad 		return sc->sc_parameters->x_sensors;
    738           1.1  riastrad }
    739           1.1  riastrad 
    740           1.1  riastrad static unsigned int
    741           1.1  riastrad uatp_y_sensors(const struct uatp_softc *sc)
    742           1.1  riastrad {
    743           1.1  riastrad 	if ((0 < sc->sc_knobs.y_sensors) &&
    744           1.1  riastrad 	    (sc->sc_knobs.y_sensors <= UATP_MAX_Y_SENSORS))
    745           1.1  riastrad 		return sc->sc_knobs.y_sensors;
    746           1.1  riastrad 	else
    747           1.1  riastrad 		return sc->sc_parameters->y_sensors;
    748           1.1  riastrad }
    749           1.1  riastrad 
    750           1.1  riastrad static unsigned int
    751           1.1  riastrad uatp_x_ratio(const struct uatp_softc *sc)
    752           1.1  riastrad {
    753           1.1  riastrad 	/* XXX Reject bogus values in sysctl.  */
    754           1.1  riastrad 	if ((0 < sc->sc_knobs.x_ratio) &&
    755           1.1  riastrad 	    (sc->sc_knobs.x_ratio <= UATP_MAX_X_RATIO))
    756           1.1  riastrad 		return sc->sc_knobs.x_ratio;
    757           1.1  riastrad 	else
    758           1.1  riastrad 		return sc->sc_parameters->x_ratio;
    759           1.1  riastrad }
    760           1.1  riastrad 
    761           1.1  riastrad static unsigned int
    762           1.1  riastrad uatp_y_ratio(const struct uatp_softc *sc)
    763           1.1  riastrad {
    764           1.1  riastrad 	/* XXX Reject bogus values in sysctl.  */
    765           1.1  riastrad 	if ((0 < sc->sc_knobs.y_ratio) &&
    766           1.1  riastrad 	    (sc->sc_knobs.y_ratio <= UATP_MAX_Y_RATIO))
    767           1.1  riastrad 		return sc->sc_knobs.y_ratio;
    768           1.1  riastrad 	else
    769           1.1  riastrad 		return sc->sc_parameters->y_ratio;
    770           1.1  riastrad }
    771  1.10.2.1.4.1     skrll 
    772           1.1  riastrad static unsigned int
    773           1.1  riastrad uatp_old_raw_weight(const struct uatp_softc *sc)
    774           1.1  riastrad {
    775           1.1  riastrad 	/* XXX Reject bogus values in sysctl.  */
    776           1.1  riastrad 	if (sc->sc_knobs.old_raw_weight <= UATP_MAX_WEIGHT)
    777           1.1  riastrad 		return sc->sc_knobs.old_raw_weight;
    778           1.1  riastrad 	else
    779           1.1  riastrad 		return 0;
    780           1.1  riastrad }
    781           1.1  riastrad 
    782           1.1  riastrad static unsigned int
    783           1.1  riastrad uatp_old_smoothed_weight(const struct uatp_softc *sc)
    784           1.1  riastrad {
    785           1.1  riastrad 	/* XXX Reject bogus values in sysctl.  */
    786           1.1  riastrad 	if (sc->sc_knobs.old_smoothed_weight <= UATP_MAX_WEIGHT)
    787           1.1  riastrad 		return sc->sc_knobs.old_smoothed_weight;
    788           1.1  riastrad 	else
    789           1.1  riastrad 		return 0;
    790           1.1  riastrad }
    791           1.1  riastrad 
    792           1.1  riastrad static unsigned int
    793           1.1  riastrad uatp_new_raw_weight(const struct uatp_softc *sc)
    794           1.1  riastrad {
    795           1.1  riastrad 	/* XXX Reject bogus values in sysctl.  */
    796           1.1  riastrad 	if ((0 < sc->sc_knobs.new_raw_weight) &&
    797           1.1  riastrad 	    (sc->sc_knobs.new_raw_weight <= UATP_MAX_WEIGHT))
    798           1.1  riastrad 		return sc->sc_knobs.new_raw_weight;
    799           1.1  riastrad 	else
    800           1.1  riastrad 		return 1;
    801           1.1  riastrad }
    802  1.10.2.1.4.1     skrll 
    803           1.1  riastrad static int
    804           1.1  riastrad scale_motion(const struct uatp_softc *sc, int delta, int *remainder,
    805           1.1  riastrad     const unsigned int *multiplier, const unsigned int *divisor)
    806           1.1  riastrad {
    807           1.1  riastrad 	int product;
    808           1.1  riastrad 
    809           1.1  riastrad 	/* XXX Limit the divisor?  */
    810           1.1  riastrad 	if (((*multiplier) == 0) ||
    811           1.1  riastrad 	    ((*multiplier) > UATP_MAX_MOTION_MULTIPLIER) ||
    812           1.1  riastrad 	    ((*divisor) == 0))
    813           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_ACCEL,
    814           1.1  riastrad 		    ("bad knobs; %d (+ %d) --> %d, rem 0\n",
    815           1.1  riastrad 			delta, *remainder, (delta + (*remainder))));
    816           1.1  riastrad 	else
    817           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_ACCEL,
    818           1.1  riastrad 		    ("scale %d (+ %d) by %u/%u --> %d, rem %d\n",
    819           1.1  riastrad 			delta, *remainder,
    820           1.1  riastrad 			(*multiplier), (*divisor),
    821           1.1  riastrad 			(((delta + (*remainder)) * ((int) (*multiplier)))
    822           1.1  riastrad 			    / ((int) (*divisor))),
    823           1.1  riastrad 			(((delta + (*remainder)) * ((int) (*multiplier)))
    824           1.1  riastrad 			    % ((int) (*divisor)))));
    825           1.1  riastrad 
    826           1.1  riastrad 	if (sc->sc_knobs.motion_remainder)
    827           1.1  riastrad 		delta += *remainder;
    828           1.1  riastrad 	*remainder = 0;
    829           1.1  riastrad 
    830           1.1  riastrad 	if (((*multiplier) == 0) ||
    831           1.1  riastrad 	    ((*multiplier) > UATP_MAX_MOTION_MULTIPLIER) ||
    832           1.1  riastrad 	    ((*divisor) == 0))
    833           1.1  riastrad 		return delta;
    834           1.1  riastrad 
    835           1.1  riastrad 	product = (delta * ((int) (*multiplier)));
    836           1.1  riastrad 	*remainder = (product % ((int) (*divisor)));
    837  1.10.2.1.4.3     skrll 	return product / ((int) (*divisor));
    838           1.1  riastrad }
    839           1.1  riastrad 
    840           1.1  riastrad static int
    841           1.1  riastrad uatp_scale_motion(const struct uatp_softc *sc, int delta, int *remainder)
    842           1.1  riastrad {
    843           1.1  riastrad 	return scale_motion(sc, delta, remainder,
    844           1.1  riastrad 	    &sc->sc_knobs.motion_multiplier,
    845           1.1  riastrad 	    &sc->sc_knobs.motion_divisor);
    846           1.1  riastrad }
    847           1.1  riastrad 
    848           1.1  riastrad static int
    849           1.1  riastrad uatp_scale_fast_motion(const struct uatp_softc *sc, int delta, int *remainder)
    850           1.1  riastrad {
    851           1.1  riastrad 	return scale_motion(sc, delta, remainder,
    852           1.1  riastrad 	    &sc->sc_knobs.fast_motion_multiplier,
    853           1.1  riastrad 	    &sc->sc_knobs.fast_motion_divisor);
    854           1.1  riastrad }
    855  1.10.2.1.4.1     skrll 
    856           1.1  riastrad /* Driver goop */
    857           1.1  riastrad 
    858           1.1  riastrad CFATTACH_DECL2_NEW(uatp, sizeof(struct uatp_softc), uatp_match, uatp_attach,
    859           1.1  riastrad     uatp_detach, uatp_activate, NULL, uatp_childdet);
    860           1.1  riastrad 
    861           1.1  riastrad static const struct wsmouse_accessops uatp_accessops = {
    862           1.1  riastrad 	.enable = uatp_enable,
    863           1.1  riastrad 	.disable = uatp_disable,
    864           1.1  riastrad 	.ioctl = uatp_ioctl,
    865           1.1  riastrad };
    866           1.1  riastrad 
    867           1.1  riastrad static int
    868           1.1  riastrad uatp_match(device_t parent, cfdata_t match, void *aux)
    869           1.1  riastrad {
    870           1.1  riastrad 	const struct uhidev_attach_arg *uha = aux;
    871           1.1  riastrad 	void *report_descriptor;
    872           1.1  riastrad 	int report_size, input_size;
    873           1.1  riastrad 	const struct uatp_descriptor *uatp_descriptor;
    874           1.1  riastrad 
    875           1.1  riastrad 	aprint_debug("%s: vendor 0x%04x, product 0x%04x\n", __func__,
    876  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_vendor,
    877  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_product);
    878           1.1  riastrad 	aprint_debug("%s: class 0x%04x, subclass 0x%04x, proto 0x%04x\n",
    879           1.1  riastrad 	    __func__,
    880  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_class,
    881  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_subclass,
    882  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_proto);
    883           1.1  riastrad 
    884           1.1  riastrad 	uhidev_get_report_desc(uha->parent, &report_descriptor, &report_size);
    885           1.1  riastrad 	input_size = hid_report_size(report_descriptor, report_size,
    886           1.1  riastrad 	    hid_input, uha->reportid);
    887           1.1  riastrad 	aprint_debug("%s: reportid %d, input size %d\n", __func__,
    888           1.1  riastrad 	    (int)uha->reportid, input_size);
    889           1.1  riastrad 
    890           1.1  riastrad 	/*
    891           1.1  riastrad 	 * Keyboards, trackpads, and eject buttons share common vendor
    892           1.1  riastrad 	 * and product ids, but not protocols: only the trackpad
    893           1.1  riastrad 	 * reports a mouse protocol.
    894           1.1  riastrad 	 */
    895  1.10.2.1.4.1     skrll 	if (uha->uiaa->uiaa_proto != UIPROTO_MOUSE)
    896           1.1  riastrad 		return UMATCH_NONE;
    897           1.1  riastrad 
    898           1.1  riastrad 	/* Check for a known vendor/product id.  */
    899           1.1  riastrad 	uatp_descriptor = find_uatp_descriptor(uha);
    900           1.1  riastrad 	if (uatp_descriptor == NULL) {
    901           1.1  riastrad 		aprint_debug("%s: unknown vendor/product id\n", __func__);
    902           1.1  riastrad 		return UMATCH_NONE;
    903           1.1  riastrad 	}
    904           1.1  riastrad 
    905           1.1  riastrad 	/* Check for the expected input size.  */
    906           1.1  riastrad 	if ((input_size < 0) ||
    907           1.1  riastrad 	    ((unsigned int)input_size !=
    908           1.1  riastrad 		uatp_descriptor->parameters->input_size)) {
    909           1.1  riastrad 		aprint_debug("%s: expected input size %u\n", __func__,
    910           1.1  riastrad 		    uatp_descriptor->parameters->input_size);
    911           1.1  riastrad 		return UMATCH_NONE;
    912           1.1  riastrad 	}
    913           1.1  riastrad 
    914           1.1  riastrad 	return UMATCH_VENDOR_PRODUCT_CONF_IFACE;
    915           1.1  riastrad }
    916  1.10.2.1.4.1     skrll 
    917           1.1  riastrad static void
    918           1.1  riastrad uatp_attach(device_t parent, device_t self, void *aux)
    919           1.1  riastrad {
    920           1.1  riastrad 	struct uatp_softc *sc = device_private(self);
    921           1.1  riastrad 	const struct uhidev_attach_arg *uha = aux;
    922           1.1  riastrad 	const struct uatp_descriptor *uatp_descriptor;
    923           1.1  riastrad 	void *report_descriptor;
    924           1.1  riastrad 	int report_size, input_size;
    925           1.1  riastrad 	struct wsmousedev_attach_args a;
    926           1.1  riastrad 
    927           1.1  riastrad 	/* Set up uhidev state.  (Why doesn't uhidev do most of this?)  */
    928           1.1  riastrad 	sc->sc_hdev.sc_dev = self;
    929           1.1  riastrad 	sc->sc_hdev.sc_intr = uatp_intr;
    930           1.1  riastrad 	sc->sc_hdev.sc_parent = uha->parent;
    931           1.1  riastrad 	sc->sc_hdev.sc_report_id = uha->reportid;
    932           1.1  riastrad 
    933           1.1  riastrad 	/* Identify ourselves to dmesg.  */
    934           1.1  riastrad 	uatp_descriptor = find_uatp_descriptor(uha);
    935           1.1  riastrad 	KASSERT(uatp_descriptor != NULL);
    936           1.1  riastrad 	aprint_normal(": %s\n", uatp_descriptor->description);
    937           1.1  riastrad 	aprint_naive(": %s\n", uatp_descriptor->description);
    938           1.1  riastrad 	aprint_verbose_dev(self,
    939           1.1  riastrad 	    "vendor 0x%04x, product 0x%04x, report id %d\n",
    940  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_vendor,
    941  1.10.2.1.4.1     skrll 	    (unsigned int)uha->uiaa->uiaa_product,
    942           1.1  riastrad 	    (int)uha->reportid);
    943           1.1  riastrad 
    944           1.1  riastrad 	uhidev_get_report_desc(uha->parent, &report_descriptor, &report_size);
    945           1.1  riastrad 	input_size = hid_report_size(report_descriptor, report_size, hid_input,
    946           1.1  riastrad 	    uha->reportid);
    947           1.1  riastrad 	KASSERT(0 < input_size);
    948           1.1  riastrad 	sc->sc_input_size = input_size;
    949           1.1  riastrad 
    950           1.1  riastrad 	/* Initialize model-specific parameters.  */
    951           1.1  riastrad 	sc->sc_parameters = uatp_descriptor->parameters;
    952           1.4  christos 	KASSERT((int)sc->sc_parameters->input_size == input_size);
    953           1.1  riastrad 	KASSERT(sc->sc_parameters->x_sensors <= UATP_MAX_X_SENSORS);
    954           1.1  riastrad 	KASSERT(sc->sc_parameters->x_ratio <= UATP_MAX_X_RATIO);
    955           1.1  riastrad 	KASSERT(sc->sc_parameters->y_sensors <= UATP_MAX_Y_SENSORS);
    956           1.1  riastrad 	KASSERT(sc->sc_parameters->y_ratio <= UATP_MAX_Y_RATIO);
    957           1.1  riastrad 	aprint_verbose_dev(self,
    958           1.1  riastrad 	    "%u x sensors, scaled by %u for %u points on screen\n",
    959           1.1  riastrad 	    sc->sc_parameters->x_sensors, sc->sc_parameters->x_ratio,
    960           1.1  riastrad 	    sc->sc_parameters->x_sensors * sc->sc_parameters->x_ratio);
    961           1.1  riastrad 	aprint_verbose_dev(self,
    962           1.1  riastrad 	    "%u y sensors, scaled by %u for %u points on screen\n",
    963           1.1  riastrad 	    sc->sc_parameters->y_sensors, sc->sc_parameters->y_ratio,
    964           1.1  riastrad 	    sc->sc_parameters->y_sensors * sc->sc_parameters->y_ratio);
    965           1.1  riastrad 	if (sc->sc_parameters->initialize)
    966           1.1  riastrad 		sc->sc_parameters->initialize(sc);
    967           1.1  riastrad 
    968           1.1  riastrad 	/* Register with pmf.  Nothing special for suspend/resume.  */
    969           1.1  riastrad 	if (!pmf_device_register(self, NULL, NULL))
    970           1.1  riastrad 		aprint_error_dev(self, "couldn't establish power handler\n");
    971           1.1  riastrad 
    972           1.1  riastrad 	/* Initialize knobs and create sysctl subtree to tweak them.  */
    973           1.1  riastrad 	sc->sc_knobs = default_knobs;
    974           1.1  riastrad 	uatp_setup_sysctl(sc);
    975           1.1  riastrad 
    976           1.1  riastrad 	/* Initialize tapping.  */
    977           1.1  riastrad 	tap_initialize(sc);
    978           1.1  riastrad 
    979           1.1  riastrad 	/* Attach wsmouse.  */
    980           1.1  riastrad 	a.accessops = &uatp_accessops;
    981           1.1  riastrad 	a.accesscookie = sc;
    982          1.10  riastrad 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &a,
    983          1.10  riastrad 	    wsmousedevprint);
    984           1.1  riastrad }
    985  1.10.2.1.4.1     skrll 
    986           1.1  riastrad /* Sysctl setup */
    987           1.1  riastrad 
    988           1.1  riastrad static void
    989           1.1  riastrad uatp_setup_sysctl(struct uatp_softc *sc)
    990           1.1  riastrad {
    991           1.1  riastrad 	int error;
    992           1.1  riastrad 
    993           1.1  riastrad 	error = sysctl_createv(&sc->sc_log, 0, NULL, &sc->sc_node, 0,
    994           1.1  riastrad 	    CTLTYPE_NODE, device_xname(uatp_dev(sc)),
    995           1.1  riastrad 	    SYSCTL_DESCR("uatp configuration knobs"),
    996           1.1  riastrad 	    NULL, 0, NULL, 0,
    997           1.1  riastrad 	    CTL_HW, CTL_CREATE, CTL_EOL);
    998           1.1  riastrad 	if (error != 0) {
    999           1.1  riastrad 		aprint_error_dev(uatp_dev(sc),
   1000           1.1  riastrad 		    "unable to set up sysctl tree hw.%s: %d\n",
   1001           1.1  riastrad 		    device_xname(uatp_dev(sc)), error);
   1002           1.1  riastrad 		goto err;
   1003           1.1  riastrad 	}
   1004           1.1  riastrad 
   1005           1.1  riastrad #if UATP_DEBUG
   1006           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_debug_flags, "debug",
   1007           1.1  riastrad 		"uatp(4) debug flags"))
   1008           1.1  riastrad 		goto err;
   1009           1.1  riastrad #endif
   1010           1.1  riastrad 
   1011           1.1  riastrad 	/*
   1012           1.1  riastrad 	 * Button emulation.
   1013           1.1  riastrad 	 */
   1014           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.two_finger_buttons,
   1015           1.1  riastrad 		"two_finger_buttons",
   1016           1.1  riastrad 		"buttons to emulate with two fingers on trackpad"))
   1017           1.1  riastrad 		goto err;
   1018           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.three_finger_buttons,
   1019           1.1  riastrad 		"three_finger_buttons",
   1020           1.1  riastrad 		"buttons to emulate with three fingers on trackpad"))
   1021           1.1  riastrad 		goto err;
   1022           1.1  riastrad 
   1023           1.1  riastrad #if 0
   1024           1.1  riastrad 	/*
   1025           1.1  riastrad 	 * Edge scrolling.
   1026           1.1  riastrad 	 */
   1027           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.top_edge, "top_edge",
   1028           1.1  riastrad 		"width of top edge for edge scrolling"))
   1029           1.1  riastrad 		goto err;
   1030           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.bottom_edge,
   1031           1.1  riastrad 		"bottom_edge", "width of bottom edge for edge scrolling"))
   1032           1.1  riastrad 		goto err;
   1033           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.left_edge, "left_edge",
   1034           1.1  riastrad 		"width of left edge for edge scrolling"))
   1035           1.1  riastrad 		goto err;
   1036           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.right_edge, "right_edge",
   1037           1.1  riastrad 		"width of right edge for edge scrolling"))
   1038           1.1  riastrad 		goto err;
   1039           1.1  riastrad #endif
   1040  1.10.2.1.4.1     skrll 
   1041           1.1  riastrad 	/*
   1042           1.1  riastrad 	 * Multifinger tracking.
   1043           1.1  riastrad 	 */
   1044           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.multifinger_track,
   1045           1.1  riastrad 		"multifinger_track",
   1046           1.1  riastrad 		"0 to ignore multiple fingers, 1 to reset, 2 to scroll"))
   1047           1.1  riastrad 		goto err;
   1048           1.1  riastrad 
   1049           1.1  riastrad 	/*
   1050           1.1  riastrad 	 * Sensor parameters.
   1051           1.1  riastrad 	 */
   1052           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.x_sensors, "x_sensors",
   1053           1.1  riastrad 		"number of x sensors"))
   1054           1.1  riastrad 		goto err;
   1055           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.x_ratio, "x_ratio",
   1056           1.1  riastrad 		"screen width to trackpad width ratio"))
   1057           1.1  riastrad 		goto err;
   1058           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.y_sensors, "y_sensors",
   1059           1.1  riastrad 		"number of y sensors"))
   1060           1.1  riastrad 		goto err;
   1061           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.y_ratio, "y_ratio",
   1062           1.1  riastrad 		"screen height to trackpad height ratio"))
   1063           1.1  riastrad 		goto err;
   1064           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.sensor_threshold,
   1065           1.1  riastrad 		"sensor_threshold", "sensor threshold"))
   1066           1.1  riastrad 		goto err;
   1067           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.sensor_normalizer,
   1068           1.1  riastrad 		"sensor_normalizer", "sensor normalizer"))
   1069           1.1  riastrad 		goto err;
   1070           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.palm_width,
   1071           1.1  riastrad 		"palm_width", "lower bound on width/height of palm"))
   1072           1.1  riastrad 		goto err;
   1073           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.old_raw_weight,
   1074           1.1  riastrad 		"old_raw_weight", "weight of old raw position"))
   1075           1.1  riastrad 		goto err;
   1076           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.old_smoothed_weight,
   1077           1.1  riastrad 		"old_smoothed_weight", "weight of old smoothed position"))
   1078           1.1  riastrad 		goto err;
   1079           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.new_raw_weight,
   1080           1.1  riastrad 		"new_raw_weight", "weight of new raw position"))
   1081           1.1  riastrad 		goto err;
   1082           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_remainder,
   1083           1.1  riastrad 		"motion_remainder", "remember motion division remainder"))
   1084           1.1  riastrad 		goto err;
   1085           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_threshold,
   1086           1.1  riastrad 		"motion_threshold", "threshold before finger moves cursor"))
   1087           1.1  riastrad 		goto err;
   1088           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_multiplier,
   1089           1.1  riastrad 		"motion_multiplier", "numerator of motion scale"))
   1090           1.1  riastrad 		goto err;
   1091           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_divisor,
   1092           1.1  riastrad 		"motion_divisor", "divisor of motion scale"))
   1093           1.1  riastrad 		goto err;
   1094           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_motion_threshold,
   1095           1.1  riastrad 		"fast_motion_threshold", "threshold before fast motion"))
   1096           1.1  riastrad 		goto err;
   1097           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_motion_multiplier,
   1098           1.1  riastrad 		"fast_motion_multiplier", "numerator of fast motion scale"))
   1099           1.1  riastrad 		goto err;
   1100           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_motion_divisor,
   1101           1.1  riastrad 		"fast_motion_divisor", "divisor of fast motion scale"))
   1102           1.1  riastrad 		goto err;
   1103           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.fast_per_direction,
   1104           1.1  riastrad 		"fast_per_direction", "don't frobnitz the veeblefitzer!"))
   1105           1.1  riastrad 		goto err;
   1106           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.motion_delay,
   1107           1.1  riastrad 		"motion_delay", "number of packets before motion kicks in"))
   1108           1.1  riastrad 		goto err;
   1109  1.10.2.1.4.1     skrll 
   1110           1.1  riastrad 	/*
   1111           1.1  riastrad 	 * Tapping.
   1112           1.1  riastrad 	 */
   1113           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.tap_limit_msec,
   1114           1.1  riastrad 		"tap_limit_msec", "milliseconds before a touch is not a tap"))
   1115           1.1  riastrad 		goto err;
   1116           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.double_tap_limit_msec,
   1117           1.1  riastrad 		"double_tap_limit_msec",
   1118           1.1  riastrad 		"milliseconds before a second tap keeps the button down"))
   1119           1.1  riastrad 		goto err;
   1120           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.one_finger_tap_buttons,
   1121           1.1  riastrad 		"one_finger_tap_buttons", "buttons for one-finger taps"))
   1122           1.1  riastrad 		goto err;
   1123           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.two_finger_tap_buttons,
   1124           1.1  riastrad 		"two_finger_tap_buttons", "buttons for two-finger taps"))
   1125           1.1  riastrad 		goto err;
   1126           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.three_finger_tap_buttons,
   1127           1.1  riastrad 		"three_finger_tap_buttons", "buttons for three-finger taps"))
   1128           1.1  riastrad 		goto err;
   1129           1.1  riastrad 	if (!uatp_setup_sysctl_knob(sc, &sc->sc_knobs.tap_track_distance_limit,
   1130           1.1  riastrad 		"tap_track_distance_limit",
   1131           1.1  riastrad 		"maximum distance^2 of tracking during tap"))
   1132           1.1  riastrad 		goto err;
   1133           1.1  riastrad 
   1134           1.1  riastrad 	return;
   1135           1.1  riastrad 
   1136           1.1  riastrad err:
   1137           1.1  riastrad 	sysctl_teardown(&sc->sc_log);
   1138           1.1  riastrad 	sc->sc_node = NULL;
   1139           1.1  riastrad }
   1140           1.1  riastrad 
   1141           1.1  riastrad static bool
   1142           1.1  riastrad uatp_setup_sysctl_knob(struct uatp_softc *sc, int *ptr, const char *name,
   1143           1.1  riastrad     const char *description)
   1144           1.1  riastrad {
   1145           1.1  riastrad 	int error;
   1146           1.1  riastrad 
   1147           1.1  riastrad 	error = sysctl_createv(&sc->sc_log, 0, NULL, NULL, CTLFLAG_READWRITE,
   1148           1.1  riastrad 	    CTLTYPE_INT, name, SYSCTL_DESCR(description),
   1149           1.1  riastrad 	    NULL, 0, ptr, 0,
   1150           1.1  riastrad 	    CTL_HW, sc->sc_node->sysctl_num, CTL_CREATE, CTL_EOL);
   1151           1.1  riastrad 	if (error != 0) {
   1152           1.1  riastrad 		aprint_error_dev(uatp_dev(sc),
   1153           1.1  riastrad 		    "unable to setup sysctl node hw.%s.%s: %d\n",
   1154           1.1  riastrad 		    device_xname(uatp_dev(sc)), name, error);
   1155           1.1  riastrad 		return false;
   1156           1.1  riastrad 	}
   1157           1.1  riastrad 
   1158           1.1  riastrad 	return true;
   1159           1.1  riastrad }
   1160  1.10.2.1.4.1     skrll 
   1161           1.1  riastrad /* More driver goop */
   1162           1.1  riastrad 
   1163           1.1  riastrad static void
   1164           1.1  riastrad uatp_childdet(device_t self, device_t child)
   1165           1.1  riastrad {
   1166           1.1  riastrad 	struct uatp_softc *sc = device_private(self);
   1167           1.1  riastrad 
   1168           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("detaching child %s\n",
   1169           1.1  riastrad 	    device_xname(child)));
   1170           1.1  riastrad 
   1171           1.1  riastrad 	/* Our only child is the wsmouse device.  */
   1172           1.1  riastrad 	if (child == sc->sc_wsmousedev)
   1173           1.1  riastrad 		sc->sc_wsmousedev = NULL;
   1174           1.1  riastrad }
   1175           1.1  riastrad 
   1176           1.1  riastrad static int
   1177           1.1  riastrad uatp_detach(device_t self, int flags)
   1178           1.1  riastrad {
   1179           1.1  riastrad 	struct uatp_softc *sc = device_private(self);
   1180           1.1  riastrad 
   1181           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("detaching with flags %d\n", flags));
   1182           1.1  riastrad 
   1183           1.1  riastrad         if (sc->sc_status & UATP_ENABLED) {
   1184           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "can't detach while enabled\n");
   1185           1.1  riastrad 		return EBUSY;
   1186           1.1  riastrad         }
   1187           1.1  riastrad 
   1188           1.1  riastrad 	if (sc->sc_parameters->finalize) {
   1189           1.1  riastrad 		int error = sc->sc_parameters->finalize(sc);
   1190           1.1  riastrad 		if (error != 0)
   1191           1.1  riastrad 			return error;
   1192           1.1  riastrad 	}
   1193           1.1  riastrad 
   1194           1.1  riastrad 	pmf_device_deregister(self);
   1195           1.1  riastrad 
   1196           1.1  riastrad 	sysctl_teardown(&sc->sc_log);
   1197           1.1  riastrad 	sc->sc_node = NULL;
   1198           1.1  riastrad 
   1199           1.1  riastrad 	tap_finalize(sc);
   1200           1.1  riastrad 
   1201           1.1  riastrad 	return config_detach_children(self, flags);
   1202           1.1  riastrad }
   1203           1.1  riastrad 
   1204           1.1  riastrad static int
   1205           1.1  riastrad uatp_activate(device_t self, enum devact act)
   1206           1.1  riastrad {
   1207           1.1  riastrad 	struct uatp_softc *sc = device_private(self);
   1208           1.1  riastrad 
   1209           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("act %d\n", (int)act));
   1210           1.1  riastrad 
   1211           1.1  riastrad 	if (act != DVACT_DEACTIVATE)
   1212           1.1  riastrad 		return EOPNOTSUPP;
   1213           1.1  riastrad 
   1214           1.1  riastrad 	sc->sc_status |= UATP_DYING;
   1215           1.1  riastrad 
   1216           1.1  riastrad 	return 0;
   1217           1.1  riastrad }
   1218  1.10.2.1.4.1     skrll 
   1219           1.1  riastrad /* wsmouse routines */
   1220           1.1  riastrad 
   1221           1.1  riastrad static int
   1222           1.1  riastrad uatp_enable(void *v)
   1223           1.1  riastrad {
   1224           1.1  riastrad 	struct uatp_softc *sc = v;
   1225           1.1  riastrad 
   1226           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("enabling wsmouse\n"));
   1227           1.1  riastrad 
   1228           1.1  riastrad 	/* Refuse to enable if we've been deactivated.  */
   1229           1.1  riastrad 	if (sc->sc_status & UATP_DYING) {
   1230           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("busy dying\n"));
   1231           1.1  riastrad 		return EIO;
   1232           1.1  riastrad 	}
   1233           1.1  riastrad 
   1234           1.1  riastrad 	/* Refuse to enable if we already are enabled.  */
   1235           1.1  riastrad 	if (sc->sc_status & UATP_ENABLED) {
   1236           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("already enabled\n"));
   1237           1.1  riastrad 		return EBUSY;
   1238           1.1  riastrad 	}
   1239           1.1  riastrad 
   1240           1.1  riastrad 	sc->sc_status |= UATP_ENABLED;
   1241           1.1  riastrad 	sc->sc_status &=~ UATP_VALID;
   1242           1.1  riastrad 	sc->sc_input_index = 0;
   1243           1.1  riastrad 	tap_enable(sc);
   1244           1.1  riastrad 	uatp_clear_position(sc);
   1245           1.1  riastrad 
   1246           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("uhidev_open(%p)\n", &sc->sc_hdev));
   1247           1.1  riastrad 	return uhidev_open(&sc->sc_hdev);
   1248           1.1  riastrad }
   1249           1.1  riastrad 
   1250           1.1  riastrad static void
   1251           1.1  riastrad uatp_disable(void *v)
   1252           1.1  riastrad {
   1253           1.1  riastrad 	struct uatp_softc *sc = v;
   1254           1.1  riastrad 
   1255           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("disabling wsmouse\n"));
   1256           1.1  riastrad 
   1257           1.1  riastrad 	if (!(sc->sc_status & UATP_ENABLED)) {
   1258           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("not enabled\n"));
   1259           1.1  riastrad 		return;
   1260           1.1  riastrad 	}
   1261           1.1  riastrad 
   1262           1.1  riastrad 	tap_disable(sc);
   1263           1.1  riastrad 	sc->sc_status &=~ UATP_ENABLED;
   1264           1.1  riastrad 
   1265           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("uhidev_close(%p)\n", &sc->sc_hdev));
   1266           1.1  riastrad 	uhidev_close(&sc->sc_hdev);
   1267           1.1  riastrad }
   1268           1.1  riastrad 
   1269           1.1  riastrad static int
   1270           1.1  riastrad uatp_ioctl(void *v, unsigned long cmd, void *data, int flag, struct lwp *p)
   1271           1.1  riastrad {
   1272           1.1  riastrad 
   1273           1.3    martin 	DPRINTF((struct uatp_softc*)v, UATP_DEBUG_IOCTL,
   1274           1.1  riastrad 	    ("cmd %lx, data %p, flag %x, lwp %p\n", cmd, data, flag, p));
   1275           1.1  riastrad 
   1276           1.1  riastrad 	/* XXX Implement any relevant wsmouse(4) ioctls.  */
   1277           1.1  riastrad 	return EPASSTHROUGH;
   1278           1.1  riastrad }
   1279  1.10.2.1.4.1     skrll 
   1280           1.1  riastrad /*
   1281           1.1  riastrad  * The Geyser 3 and 4 models talk the generic USB HID mouse protocol by
   1282           1.1  riastrad  * default.  This mode switch makes them give raw sensor data instead
   1283           1.1  riastrad  * so that we can implement tapping, two-finger scrolling, &c.
   1284           1.1  riastrad  */
   1285           1.1  riastrad 
   1286           1.1  riastrad #define GEYSER34_RAW_MODE		0x04
   1287           1.1  riastrad #define GEYSER34_MODE_REPORT_ID		0
   1288           1.1  riastrad #define GEYSER34_MODE_INTERFACE		0
   1289           1.1  riastrad #define GEYSER34_MODE_PACKET_SIZE	8
   1290           1.1  riastrad 
   1291           1.1  riastrad static void
   1292           1.1  riastrad geyser34_enable_raw_mode(struct uatp_softc *sc)
   1293           1.1  riastrad {
   1294  1.10.2.1.4.1     skrll 	struct usbd_device *udev = sc->sc_hdev.sc_parent->sc_udev;
   1295           1.1  riastrad 	usb_device_request_t req;
   1296           1.1  riastrad 	usbd_status status;
   1297           1.1  riastrad 	uint8_t report[GEYSER34_MODE_PACKET_SIZE];
   1298           1.1  riastrad 
   1299           1.1  riastrad 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
   1300           1.1  riastrad 	req.bRequest = UR_GET_REPORT;
   1301           1.1  riastrad 	USETW2(req.wValue, UHID_FEATURE_REPORT, GEYSER34_MODE_REPORT_ID);
   1302           1.1  riastrad 	USETW(req.wIndex, GEYSER34_MODE_INTERFACE);
   1303           1.1  riastrad 	USETW(req.wLength, GEYSER34_MODE_PACKET_SIZE);
   1304           1.1  riastrad 
   1305           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_RESET, ("get feature report\n"));
   1306           1.1  riastrad 	status = usbd_do_request(udev, &req, report);
   1307           1.1  riastrad 	if (status != USBD_NORMAL_COMPLETION) {
   1308           1.1  riastrad 		aprint_error_dev(uatp_dev(sc),
   1309           1.1  riastrad 		    "error reading feature report: %s\n", usbd_errstr(status));
   1310           1.1  riastrad 		return;
   1311           1.1  riastrad 	}
   1312  1.10.2.1.4.1     skrll 
   1313           1.1  riastrad #if UATP_DEBUG
   1314           1.1  riastrad 	if (sc->sc_debug_flags & UATP_DEBUG_RESET) {
   1315           1.1  riastrad 		unsigned int i;
   1316           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_RESET, ("old feature report:"));
   1317           1.1  riastrad 		for (i = 0; i < GEYSER34_MODE_PACKET_SIZE; i++)
   1318           1.1  riastrad 			printf(" %02x", (unsigned int)report[i]);
   1319           1.1  riastrad 		printf("\n");
   1320           1.1  riastrad 		/* Doing this twice is harmless here and lets this be
   1321           1.1  riastrad 		 * one ifdef.  */
   1322           1.1  riastrad 		report[0] = GEYSER34_RAW_MODE;
   1323           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_RESET, ("new feature report:"));
   1324           1.1  riastrad 		for (i = 0; i < GEYSER34_MODE_PACKET_SIZE; i++)
   1325           1.1  riastrad 			printf(" %02x", (unsigned int)report[i]);
   1326           1.1  riastrad 		printf("\n");
   1327           1.1  riastrad 	}
   1328           1.1  riastrad #endif
   1329           1.1  riastrad 
   1330           1.1  riastrad 	report[0] = GEYSER34_RAW_MODE;
   1331           1.1  riastrad 
   1332           1.1  riastrad 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1333           1.1  riastrad 	req.bRequest = UR_SET_REPORT;
   1334           1.1  riastrad 	USETW2(req.wValue, UHID_FEATURE_REPORT, GEYSER34_MODE_REPORT_ID);
   1335           1.1  riastrad 	USETW(req.wIndex, GEYSER34_MODE_INTERFACE);
   1336           1.1  riastrad 	USETW(req.wLength, GEYSER34_MODE_PACKET_SIZE);
   1337           1.1  riastrad 
   1338           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_RESET, ("set feature report\n"));
   1339           1.1  riastrad 	status = usbd_do_request(udev, &req, report);
   1340           1.1  riastrad 	if (status != USBD_NORMAL_COMPLETION) {
   1341           1.1  riastrad 		aprint_error_dev(uatp_dev(sc),
   1342           1.1  riastrad 		    "error writing feature report: %s\n", usbd_errstr(status));
   1343           1.1  riastrad 		return;
   1344           1.1  riastrad 	}
   1345           1.1  riastrad }
   1346  1.10.2.1.4.1     skrll 
   1347           1.1  riastrad /*
   1348           1.1  riastrad  * The Geyser 3 and 4 need to be reset periodically after we detect a
   1349           1.8  riastrad  * continual flow of spurious interrupts.  We use a USB task for this.
   1350           1.1  riastrad  */
   1351           1.1  riastrad 
   1352           1.1  riastrad static void
   1353           1.1  riastrad geyser34_initialize(struct uatp_softc *sc)
   1354           1.1  riastrad {
   1355           1.8  riastrad 
   1356           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("initializing\n"));
   1357           1.1  riastrad 	geyser34_enable_raw_mode(sc);
   1358      1.10.2.1       snj 	usb_init_task(&sc->sc_reset_task, &geyser34_reset_task, sc, 0);
   1359           1.1  riastrad }
   1360           1.1  riastrad 
   1361           1.1  riastrad static int
   1362           1.1  riastrad geyser34_finalize(struct uatp_softc *sc)
   1363           1.1  riastrad {
   1364           1.8  riastrad 
   1365           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MISC, ("finalizing\n"));
   1366           1.8  riastrad 	usb_rem_task(sc->sc_hdev.sc_parent->sc_udev, &sc->sc_reset_task);
   1367           1.1  riastrad 
   1368           1.1  riastrad 	return 0;
   1369           1.1  riastrad }
   1370           1.1  riastrad 
   1371           1.1  riastrad static void
   1372           1.1  riastrad geyser34_deferred_reset(struct uatp_softc *sc)
   1373           1.1  riastrad {
   1374           1.8  riastrad 
   1375           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_RESET, ("deferring reset\n"));
   1376           1.8  riastrad 	usb_add_task(sc->sc_hdev.sc_parent->sc_udev, &sc->sc_reset_task,
   1377           1.8  riastrad 	    USB_TASKQ_DRIVER);
   1378           1.1  riastrad }
   1379           1.1  riastrad 
   1380           1.1  riastrad static void
   1381           1.8  riastrad geyser34_reset_task(void *arg)
   1382           1.1  riastrad {
   1383           1.1  riastrad 	struct uatp_softc *sc = arg;
   1384           1.1  riastrad 
   1385           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_RESET, ("resetting\n"));
   1386           1.1  riastrad 
   1387           1.1  riastrad 	/* Reset by putting it into raw mode.  Not sure why.  */
   1388           1.1  riastrad 	geyser34_enable_raw_mode(sc);
   1389           1.1  riastrad }
   1390  1.10.2.1.4.1     skrll 
   1391           1.1  riastrad /* Interrupt handler */
   1392           1.1  riastrad 
   1393           1.1  riastrad static void
   1394           1.1  riastrad uatp_intr(struct uhidev *addr, void *ibuf, unsigned int len)
   1395           1.1  riastrad {
   1396           1.1  riastrad 	struct uatp_softc *sc = (struct uatp_softc *)addr;
   1397           1.1  riastrad 	uint8_t *input;
   1398           1.1  riastrad 	int dx, dy, dz, dw;
   1399           1.1  riastrad 	uint32_t buttons;
   1400           1.1  riastrad 
   1401           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_INTR, ("softc %p, ibuf %p, len %u\n",
   1402           1.1  riastrad 	    addr, ibuf, len));
   1403           1.1  riastrad 
   1404           1.1  riastrad 	/*
   1405           1.1  riastrad 	 * Some devices break packets up into chunks, so we accumulate
   1406           1.1  riastrad 	 * input up to the expected packet length, or if it would
   1407           1.1  riastrad 	 * overflow, discard the whole packet and start over.
   1408           1.1  riastrad 	 */
   1409           1.1  riastrad 	if (sc->sc_input_size < len) {
   1410           1.1  riastrad 		aprint_error_dev(uatp_dev(sc),
   1411           1.1  riastrad 		    "discarding %u-byte input packet\n", len);
   1412           1.1  riastrad 		sc->sc_input_index = 0;
   1413           1.1  riastrad 		return;
   1414           1.1  riastrad 	} else if (sc->sc_input_size < (sc->sc_input_index + len)) {
   1415           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "discarding %u-byte input\n",
   1416           1.1  riastrad 		    (sc->sc_input_index + len));
   1417           1.1  riastrad 		sc->sc_input_index = 0;
   1418           1.1  riastrad 		return;
   1419           1.1  riastrad 	}
   1420           1.1  riastrad 
   1421           1.1  riastrad #if UATP_DEBUG
   1422           1.1  riastrad 	if (sc->sc_debug_flags & UATP_DEBUG_INTR) {
   1423           1.1  riastrad 		unsigned int i;
   1424           1.1  riastrad 		uint8_t *bytes = ibuf;
   1425           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_INTR, ("raw"));
   1426           1.1  riastrad 		for (i = 0; i < len; i++)
   1427           1.1  riastrad 			printf(" %02x", (unsigned int)bytes[i]);
   1428           1.1  riastrad 		printf("\n");
   1429           1.1  riastrad 	}
   1430           1.1  riastrad #endif
   1431           1.1  riastrad 
   1432           1.1  riastrad 	memcpy(&sc->sc_input[sc->sc_input_index], ibuf, len);
   1433           1.1  riastrad 	sc->sc_input_index += len;
   1434           1.1  riastrad 	if (sc->sc_input_index != sc->sc_input_size) {
   1435           1.1  riastrad 		/* Wait until packet is complete.  */
   1436           1.1  riastrad 		aprint_verbose_dev(uatp_dev(sc), "partial packet: %u bytes\n",
   1437           1.1  riastrad 		    len);
   1438           1.1  riastrad 		return;
   1439           1.1  riastrad 	}
   1440           1.1  riastrad 
   1441           1.1  riastrad 	/* Clear the buffer and process the now complete packet.  */
   1442           1.1  riastrad 	sc->sc_input_index = 0;
   1443           1.1  riastrad 	input = sc->sc_input;
   1444           1.1  riastrad 
   1445           1.1  riastrad 	/* The last byte's first bit is set iff the button is pressed.
   1446           1.1  riastrad 	 * XXX Left button should have a name.  */
   1447           1.1  riastrad 	buttons = ((input[sc->sc_input_size - 1] & UATP_STATUS_BUTTON)
   1448           1.1  riastrad 	    ? 1 : 0);
   1449           1.1  riastrad 
   1450           1.1  riastrad 	/* Read the sample.  */
   1451           1.1  riastrad 	memset(uatp_x_sample(sc), 0, UATP_MAX_X_SENSORS);
   1452           1.1  riastrad 	memset(uatp_y_sample(sc), 0, UATP_MAX_Y_SENSORS);
   1453           1.1  riastrad 	sc->sc_parameters->read_sample(uatp_x_sample(sc), uatp_y_sample(sc),
   1454           1.1  riastrad 	    input);
   1455  1.10.2.1.4.1     skrll 
   1456           1.1  riastrad #if UATP_DEBUG
   1457           1.1  riastrad 	if (sc->sc_debug_flags & UATP_DEBUG_INTR) {
   1458           1.1  riastrad 		unsigned int i;
   1459           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_INTR, ("x sensors"));
   1460           1.1  riastrad 		for (i = 0; i < uatp_x_sensors(sc); i++)
   1461           1.1  riastrad 			printf(" %02x", (unsigned int)uatp_x_sample(sc)[i]);
   1462           1.1  riastrad 		printf("\n");
   1463           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_INTR, ("y sensors"));
   1464           1.1  riastrad 		for (i = 0; i < uatp_y_sensors(sc); i++)
   1465           1.1  riastrad 			printf(" %02x", (unsigned int)uatp_y_sample(sc)[i]);
   1466           1.1  riastrad 		printf("\n");
   1467           1.1  riastrad 	} else if ((sc->sc_debug_flags & UATP_DEBUG_STATUS) &&
   1468           1.1  riastrad 		(input[sc->sc_input_size - 1] &~
   1469           1.1  riastrad 		    (UATP_STATUS_BUTTON | UATP_STATUS_BASE |
   1470           1.1  riastrad 			UATP_STATUS_POST_RESET)))
   1471           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_STATUS, ("status byte: %02x\n",
   1472           1.1  riastrad 		    input[sc->sc_input_size - 1]));
   1473           1.1  riastrad #endif
   1474           1.1  riastrad 
   1475           1.1  riastrad 	/*
   1476           1.1  riastrad 	 * If this is a base sample, initialize the state to interpret
   1477           1.1  riastrad 	 * subsequent samples relative to it, and stop here.
   1478           1.1  riastrad 	 */
   1479           1.1  riastrad 	if (sc->sc_parameters->base_sample(sc, input)) {
   1480           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_PARSE,
   1481           1.1  riastrad 		    ("base sample, buttons %"PRIx32"\n", buttons));
   1482           1.1  riastrad 		/* XXX Should the valid bit ever be reset?  */
   1483           1.1  riastrad 		sc->sc_status |= UATP_VALID;
   1484           1.1  riastrad 		uatp_clear_position(sc);
   1485           1.1  riastrad 		memcpy(sc->sc_base, sc->sc_sample, sizeof(sc->sc_base));
   1486           1.1  riastrad 		/* XXX Perform 17" size detection like Linux?  */
   1487           1.1  riastrad 		return;
   1488           1.1  riastrad 	}
   1489           1.1  riastrad 
   1490           1.1  riastrad 	/* If not, accumulate the change in the sensors.  */
   1491           1.1  riastrad 	sc->sc_parameters->accumulate(sc);
   1492           1.1  riastrad 
   1493           1.1  riastrad #if UATP_DEBUG
   1494           1.1  riastrad 	if (sc->sc_debug_flags & UATP_DEBUG_ACCUMULATE) {
   1495           1.1  riastrad 		unsigned int i;
   1496           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_ACCUMULATE, ("accumulated x state:"));
   1497           1.1  riastrad 		for (i = 0; i < uatp_x_sensors(sc); i++)
   1498           1.1  riastrad 			printf(" %02x", (unsigned int)uatp_x_acc(sc)[i]);
   1499           1.1  riastrad 		printf("\n");
   1500           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_ACCUMULATE, ("accumulated y state:"));
   1501           1.1  riastrad 		for (i = 0; i < uatp_y_sensors(sc); i++)
   1502           1.1  riastrad 			printf(" %02x", (unsigned int)uatp_y_acc(sc)[i]);
   1503           1.1  riastrad 		printf("\n");
   1504           1.1  riastrad 	}
   1505           1.1  riastrad #endif
   1506           1.1  riastrad 
   1507           1.1  riastrad 	/* Compute the change in coordinates and buttons.  */
   1508           1.1  riastrad 	dx = dy = dz = dw = 0;
   1509           1.1  riastrad 	if ((!interpret_input(sc, &dx, &dy, &dz, &dw, &buttons)) &&
   1510           1.1  riastrad 	    /* If there's no input because we're releasing a button,
   1511           1.1  riastrad 	     * then it's not spurious.  XXX Mutex?  */
   1512           1.1  riastrad 	    (sc->sc_buttons == 0)) {
   1513           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_SPURINTR, ("spurious interrupt\n"));
   1514           1.1  riastrad 		if (sc->sc_parameters->reset)
   1515           1.1  riastrad 			sc->sc_parameters->reset(sc);
   1516           1.1  riastrad 		return;
   1517           1.1  riastrad 	}
   1518           1.1  riastrad 
   1519           1.1  riastrad 	/* Report to wsmouse.  */
   1520           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_INTR,
   1521           1.1  riastrad 	    ("buttons %"PRIx32", dx %d, dy %d, dz %d, dw %d\n",
   1522           1.1  riastrad 		buttons, dx, dy, dz, dw));
   1523           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   1524           1.1  riastrad 	uatp_input(sc, buttons, dx, dy, dz, dw);
   1525           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   1526           1.1  riastrad }
   1527  1.10.2.1.4.1     skrll 
   1528           1.1  riastrad /*
   1529           1.1  riastrad  * Different ways to discern the base sample initializing the state.
   1530           1.1  riastrad  * `base_sample_softc_flag' uses a state flag stored in the softc;
   1531           1.1  riastrad  * `base_sample_input_flag' checks a flag at the end of the input
   1532           1.1  riastrad  * packet.
   1533           1.1  riastrad  */
   1534           1.1  riastrad 
   1535           1.1  riastrad static bool
   1536           1.1  riastrad base_sample_softc_flag(const struct uatp_softc *sc, const uint8_t *input)
   1537           1.1  riastrad {
   1538           1.1  riastrad 	return !(sc->sc_status & UATP_VALID);
   1539           1.1  riastrad }
   1540           1.1  riastrad 
   1541           1.1  riastrad static bool
   1542           1.1  riastrad base_sample_input_flag(const struct uatp_softc *sc, const uint8_t *input)
   1543           1.1  riastrad {
   1544           1.1  riastrad 	/* XXX Should we also check the valid flag?  */
   1545           1.1  riastrad 	return !!(input[sc->sc_input_size - 1] & UATP_STATUS_BASE);
   1546           1.1  riastrad }
   1547           1.1  riastrad 
   1548           1.1  riastrad /*
   1549           1.1  riastrad  * Pick apart the horizontal sensors from the vertical sensors.
   1550           1.1  riastrad  * Different models interleave them in different orders.
   1551           1.1  riastrad  */
   1552           1.1  riastrad 
   1553           1.1  riastrad static void
   1554           1.1  riastrad read_sample_1(uint8_t *x, uint8_t *y, const uint8_t *input)
   1555           1.1  riastrad {
   1556           1.1  riastrad 	unsigned int i;
   1557           1.1  riastrad 
   1558           1.1  riastrad 	for (i = 0; i < 8; i++) {
   1559           1.1  riastrad 		x[i] = input[5 * i + 2];
   1560           1.1  riastrad 		x[i + 8] = input[5 * i + 4];
   1561           1.1  riastrad 		x[i + 16] = input[5 * i + 42];
   1562           1.1  riastrad 		if (i < 2)
   1563           1.1  riastrad 			x[i + 24] = input[5 * i + 44];
   1564           1.1  riastrad 
   1565           1.1  riastrad 		y[i] = input[5 * i + 1];
   1566           1.1  riastrad 		y[i + 8] = input[5 * i + 3];
   1567           1.1  riastrad 	}
   1568           1.1  riastrad }
   1569           1.1  riastrad 
   1570           1.1  riastrad static void
   1571           1.1  riastrad read_sample_2(uint8_t *x, uint8_t *y, const uint8_t *input)
   1572           1.1  riastrad {
   1573           1.1  riastrad 	unsigned int i, j;
   1574           1.1  riastrad 
   1575           1.1  riastrad 	for (i = 0, j = 19; i < 20; i += 2, j += 3) {
   1576           1.1  riastrad 		x[i] = input[j];
   1577           1.1  riastrad 		x[i + 1] = input[j + 1];
   1578           1.1  riastrad 	}
   1579           1.1  riastrad 
   1580           1.1  riastrad 	for (i = 0, j = 1; i < 9; i += 2, j += 3) {
   1581           1.1  riastrad 		y[i] = input[j];
   1582           1.1  riastrad 		y[i + 1] = input[j + 1];
   1583           1.1  riastrad 	}
   1584           1.1  riastrad }
   1585  1.10.2.1.4.1     skrll 
   1586           1.1  riastrad static void
   1587           1.1  riastrad accumulate_sample_1(struct uatp_softc *sc)
   1588           1.1  riastrad {
   1589           1.1  riastrad 	unsigned int i;
   1590           1.1  riastrad 
   1591           1.1  riastrad 	for (i = 0; i < UATP_SENSORS; i++) {
   1592           1.1  riastrad 		sc->sc_acc[i] += (int8_t)(sc->sc_sample[i] - sc->sc_base[i]);
   1593           1.1  riastrad 		if (sc->sc_acc[i] < 0) {
   1594           1.1  riastrad 			sc->sc_acc[i] = 0;
   1595           1.1  riastrad 		} else if (UATP_MAX_ACC < sc->sc_acc[i]) {
   1596           1.1  riastrad 			DPRINTF(sc, UATP_DEBUG_ACCUMULATE,
   1597           1.1  riastrad 			    ("overflow %d\n", sc->sc_acc[i]));
   1598           1.1  riastrad 			sc->sc_acc[i] = UATP_MAX_ACC;
   1599           1.1  riastrad 		}
   1600           1.1  riastrad 	}
   1601           1.1  riastrad 
   1602           1.1  riastrad 	memcpy(sc->sc_base, sc->sc_sample, sizeof(sc->sc_base));
   1603           1.1  riastrad }
   1604           1.1  riastrad 
   1605           1.1  riastrad static void
   1606           1.1  riastrad accumulate_sample_2(struct uatp_softc *sc)
   1607           1.1  riastrad {
   1608           1.1  riastrad 	unsigned int i;
   1609           1.1  riastrad 
   1610           1.1  riastrad 	for (i = 0; i < UATP_SENSORS; i++) {
   1611           1.1  riastrad 		sc->sc_acc[i] = (int8_t)(sc->sc_sample[i] - sc->sc_base[i]);
   1612           1.1  riastrad 		if (sc->sc_acc[i] < -0x80) {
   1613           1.1  riastrad 			DPRINTF(sc, UATP_DEBUG_ACCUMULATE,
   1614           1.1  riastrad 			    ("underflow %u - %u = %d\n",
   1615           1.1  riastrad 				(unsigned int)sc->sc_sample[i],
   1616           1.1  riastrad 				(unsigned int)sc->sc_base[i],
   1617           1.1  riastrad 				sc->sc_acc[i]));
   1618           1.1  riastrad 			sc->sc_acc[i] += 0x100;
   1619           1.1  riastrad 		}
   1620           1.1  riastrad 		if (0x7f < sc->sc_acc[i]) {
   1621           1.1  riastrad 			DPRINTF(sc, UATP_DEBUG_ACCUMULATE,
   1622           1.1  riastrad 			    ("overflow %u - %u = %d\n",
   1623           1.1  riastrad 				(unsigned int)sc->sc_sample[i],
   1624           1.1  riastrad 				(unsigned int)sc->sc_base[i],
   1625           1.1  riastrad 				sc->sc_acc[i]));
   1626           1.1  riastrad 			sc->sc_acc[i] -= 0x100;
   1627           1.1  riastrad 		}
   1628           1.1  riastrad 		if (sc->sc_acc[i] < 0)
   1629           1.1  riastrad 			sc->sc_acc[i] = 0;
   1630           1.1  riastrad 	}
   1631           1.1  riastrad }
   1632  1.10.2.1.4.1     skrll 
   1633           1.1  riastrad /*
   1634           1.1  riastrad  * Report input to wsmouse, if there is anything interesting to report.
   1635           1.1  riastrad  * We must take into consideration the current tap-and-drag button
   1636           1.1  riastrad  * state.
   1637           1.1  riastrad  */
   1638           1.1  riastrad 
   1639           1.1  riastrad static void
   1640           1.1  riastrad uatp_input(struct uatp_softc *sc, uint32_t buttons,
   1641           1.1  riastrad     int dx, int dy, int dz, int dw)
   1642           1.1  riastrad {
   1643           1.1  riastrad 	uint32_t all_buttons;
   1644           1.1  riastrad 
   1645           1.1  riastrad 	KASSERT(mutex_owned(&sc->sc_tap_mutex));
   1646           1.1  riastrad 	all_buttons = buttons | uatp_tapped_buttons(sc);
   1647           1.1  riastrad 
   1648           1.1  riastrad 	if ((sc->sc_wsmousedev != NULL) &&
   1649           1.1  riastrad 	    ((dx != 0) || (dy != 0) || (dz != 0) || (dw != 0) ||
   1650           1.1  riastrad 		(all_buttons != sc->sc_all_buttons))) {
   1651           1.1  riastrad 		int s = spltty();
   1652           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_WSMOUSE, ("wsmouse input:"
   1653           1.1  riastrad 		    " buttons %"PRIx32", dx %d, dy %d, dz %d, dw %d\n",
   1654           1.1  riastrad 		    all_buttons, dx, -dy, dz, -dw));
   1655           1.1  riastrad 		wsmouse_input(sc->sc_wsmousedev, all_buttons, dx, -dy, dz, -dw,
   1656           1.1  riastrad 		    WSMOUSE_INPUT_DELTA);
   1657           1.1  riastrad 		splx(s);
   1658           1.1  riastrad 	}
   1659           1.1  riastrad 	sc->sc_buttons = buttons;
   1660           1.1  riastrad 	sc->sc_all_buttons = all_buttons;
   1661           1.1  riastrad }
   1662           1.1  riastrad 
   1663           1.1  riastrad /*
   1664           1.1  riastrad  * Interpret the current tap state to decide whether the tap buttons
   1665           1.1  riastrad  * are currently pressed.
   1666           1.1  riastrad  */
   1667           1.1  riastrad 
   1668           1.1  riastrad static uint32_t
   1669           1.1  riastrad uatp_tapped_buttons(struct uatp_softc *sc)
   1670           1.1  riastrad {
   1671           1.1  riastrad 	KASSERT(mutex_owned(&sc->sc_tap_mutex));
   1672           1.1  riastrad 	switch (sc->sc_tap_state) {
   1673           1.1  riastrad 	case TAP_STATE_INITIAL:
   1674           1.1  riastrad 	case TAP_STATE_TAPPING:
   1675           1.1  riastrad 		return 0;
   1676           1.1  riastrad 
   1677           1.1  riastrad 	case TAP_STATE_TAPPED:
   1678           1.1  riastrad 	case TAP_STATE_DOUBLE_TAPPING:
   1679           1.1  riastrad 	case TAP_STATE_DRAGGING_DOWN:
   1680           1.1  riastrad 	case TAP_STATE_DRAGGING_UP:
   1681           1.1  riastrad 	case TAP_STATE_TAPPING_IN_DRAG:
   1682           1.1  riastrad 		CHECK((0 < sc->sc_tapped_fingers), return 0);
   1683           1.1  riastrad 		switch (sc->sc_tapped_fingers) {
   1684           1.1  riastrad 		case 1: return sc->sc_knobs.one_finger_tap_buttons;
   1685           1.1  riastrad 		case 2: return sc->sc_knobs.two_finger_tap_buttons;
   1686           1.1  riastrad 		case 3:
   1687           1.1  riastrad 		default: return sc->sc_knobs.three_finger_tap_buttons;
   1688           1.1  riastrad 		}
   1689           1.1  riastrad 
   1690           1.1  riastrad 	default:
   1691           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
   1692           1.1  riastrad 		    __func__, sc->sc_tap_state);
   1693           1.1  riastrad 		return 0;
   1694           1.1  riastrad 	}
   1695           1.1  riastrad }
   1696  1.10.2.1.4.1     skrll 
   1697           1.1  riastrad /*
   1698           1.1  riastrad  * Interpret the current input state to find a difference in all the
   1699           1.1  riastrad  * relevant coordinates and buttons to pass on to wsmouse, and update
   1700           1.1  riastrad  * any internal driver state necessary to interpret subsequent input
   1701           1.1  riastrad  * relative to this one.
   1702           1.1  riastrad  */
   1703           1.1  riastrad 
   1704           1.1  riastrad static bool
   1705           1.1  riastrad interpret_input(struct uatp_softc *sc, int *dx, int *dy, int *dz, int *dw,
   1706           1.1  riastrad     uint32_t *buttons)
   1707           1.1  riastrad {
   1708           1.1  riastrad 	unsigned int x_pressure, x_raw, x_fingers;
   1709           1.1  riastrad 	unsigned int y_pressure, y_raw, y_fingers;
   1710           1.1  riastrad 	unsigned int fingers;
   1711           1.1  riastrad 
   1712           1.1  riastrad 	x_pressure = interpret_dimension(sc, uatp_x_acc(sc),
   1713           1.1  riastrad 	    uatp_x_sensors(sc), uatp_x_ratio(sc), &x_raw, &x_fingers);
   1714           1.1  riastrad 	y_pressure = interpret_dimension(sc, uatp_y_acc(sc),
   1715           1.1  riastrad 	    uatp_y_sensors(sc), uatp_y_ratio(sc), &y_raw, &y_fingers);
   1716           1.1  riastrad 
   1717           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_PARSE,
   1718           1.1  riastrad 	    ("x %u @ %u, %uf; y %u @ %u, %uf; buttons %"PRIx32"\n",
   1719           1.1  riastrad 		x_pressure, x_raw, x_fingers,
   1720           1.1  riastrad 		y_pressure, y_raw, y_fingers,
   1721           1.1  riastrad 		*buttons));
   1722           1.1  riastrad 
   1723           1.1  riastrad 	if ((x_pressure == 0) && (y_pressure == 0)) {
   1724           1.1  riastrad 		bool ok;
   1725           1.1  riastrad 		/* No fingers: clear position and maybe report a tap.  */
   1726           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_INTR,
   1727           1.1  riastrad 		    ("no position detected; clearing position\n"));
   1728           1.1  riastrad 		if (*buttons == 0) {
   1729           1.1  riastrad 			ok = tap_released(sc);
   1730           1.1  riastrad 		} else {
   1731           1.1  riastrad 			tap_reset(sc);
   1732           1.1  riastrad 			/* Button pressed: interrupt is not spurious.  */
   1733           1.1  riastrad 			ok = true;
   1734           1.1  riastrad 		}
   1735           1.1  riastrad 		/*
   1736           1.1  riastrad 		 * Don't clear the position until after tap_released,
   1737           1.1  riastrad 		 * which needs to know the track distance.
   1738           1.1  riastrad 		 */
   1739           1.1  riastrad 		uatp_clear_position(sc);
   1740           1.1  riastrad 		return ok;
   1741           1.1  riastrad 	} else if ((x_pressure == 0) || (y_pressure == 0)) {
   1742           1.1  riastrad 		/* XXX What to do here?  */
   1743           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_INTR,
   1744           1.1  riastrad 		    ("pressure in only one dimension; ignoring\n"));
   1745           1.1  riastrad 		return true;
   1746           1.1  riastrad 	} else if ((x_pressure == 1) && (y_pressure == 1)) {
   1747           1.1  riastrad 		fingers = max(x_fingers, y_fingers);
   1748           1.1  riastrad 		CHECK((0 < fingers), return false);
   1749           1.1  riastrad 		if (*buttons == 0)
   1750           1.1  riastrad 			tap_touched(sc, fingers);
   1751           1.1  riastrad 		else if (fingers == 1)
   1752           1.1  riastrad 			tap_reset(sc);
   1753           1.1  riastrad 		else		/* Multiple fingers, button pressed.  */
   1754           1.1  riastrad 			*buttons = emulated_buttons(sc, fingers);
   1755           1.1  riastrad 		update_position(sc, fingers, x_raw, y_raw, dx, dy, dz, dw);
   1756           1.1  riastrad 		return true;
   1757           1.1  riastrad 	} else {
   1758           1.1  riastrad 		/* Palm detected in either or both of the dimensions.  */
   1759           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_INTR, ("palm detected; ignoring\n"));
   1760           1.1  riastrad 		return true;
   1761           1.1  riastrad 	}
   1762           1.1  riastrad }
   1763  1.10.2.1.4.1     skrll 
   1764           1.1  riastrad /*
   1765           1.1  riastrad  * Interpret the accumulated sensor state along one dimension to find
   1766           1.1  riastrad  * the number, mean position, and pressure of fingers.  Returns 0 to
   1767           1.1  riastrad  * indicate no pressure, returns 1 and sets *position and *fingers to
   1768           1.1  riastrad  * indicate fingers, and returns 2 to indicate palm.
   1769           1.1  riastrad  *
   1770           1.1  riastrad  * XXX Give symbolic names to the return values.
   1771           1.1  riastrad  */
   1772           1.1  riastrad 
   1773           1.1  riastrad static unsigned int
   1774           1.1  riastrad interpret_dimension(struct uatp_softc *sc, const int *acc,
   1775           1.1  riastrad     unsigned int n_sensors, unsigned int ratio,
   1776           1.1  riastrad     unsigned int *position, unsigned int *fingers)
   1777           1.1  riastrad {
   1778           1.1  riastrad 	unsigned int i, v, n_fingers, sum;
   1779           1.1  riastrad 	unsigned int total[UATP_MAX_SENSORS];
   1780           1.1  riastrad 	unsigned int weighted[UATP_MAX_SENSORS];
   1781           1.1  riastrad 	unsigned int sensor_threshold = sc->sc_knobs.sensor_threshold;
   1782           1.1  riastrad 	unsigned int sensor_normalizer = sc->sc_knobs.sensor_normalizer;
   1783           1.1  riastrad 	unsigned int width = 0;	/* GCC is not smart enough.  */
   1784           1.1  riastrad 	unsigned int palm_width = sc->sc_knobs.palm_width;
   1785           1.1  riastrad 	enum { none, nondecreasing, decreasing } state = none;
   1786           1.1  riastrad 
   1787           1.1  riastrad 	if (sensor_threshold < sensor_normalizer)
   1788           1.1  riastrad 		sensor_normalizer = sensor_threshold;
   1789           1.1  riastrad 	if (palm_width == 0)	/* Effectively disable palm detection.  */
   1790           1.1  riastrad 		palm_width = UATP_MAX_POSITION;
   1791           1.1  riastrad 
   1792           1.1  riastrad #define CHECK_(condition) CHECK(condition, return 0)
   1793           1.1  riastrad 
   1794           1.1  riastrad 	/*
   1795           1.1  riastrad 	 * Arithmetic bounds:
   1796           1.1  riastrad 	 * . n_sensors is at most UATP_MAX_SENSORS,
   1797           1.1  riastrad 	 * . n_fingers is at most UATP_MAX_SENSORS,
   1798           1.1  riastrad 	 * . i is at most UATP_MAX_SENSORS,
   1799           1.1  riastrad 	 * . sc->sc_acc[i] is at most UATP_MAX_ACC,
   1800           1.1  riastrad 	 * . i * sc->sc_acc[i] is at most UATP_MAX_SENSORS * UATP_MAX_ACC,
   1801           1.1  riastrad 	 * . each total[j] is at most UATP_MAX_SENSORS * UATP_MAX_ACC,
   1802           1.1  riastrad 	 * . each weighted[j] is at most UATP_MAX_SENSORS^2 * UATP_MAX_ACC,
   1803           1.1  riastrad 	 * . ratio is at most UATP_MAX_RATIO,
   1804           1.1  riastrad 	 * . each weighted[j] * ratio is at most
   1805           1.1  riastrad 	 *     UATP_MAX_SENSORS^2 * UATP_MAX_ACC * UATP_MAX_RATIO,
   1806           1.1  riastrad 	 *   which is #x5fa0000 with the current values of the constants,
   1807           1.1  riastrad 	 *   and
   1808           1.1  riastrad 	 * . the sum of the positions is at most
   1809           1.1  riastrad 	 *     UATP_MAX_SENSORS * UATP_MAX_POSITION,
   1810           1.1  riastrad 	 *   which is #x60000 with the current values of the constants.
   1811           1.1  riastrad 	 * Hence all of the arithmetic here fits in int (and thus also
   1812           1.1  riastrad 	 * unsigned int).  If you change the constants, though, you
   1813           1.1  riastrad 	 * must update the analysis.
   1814           1.1  riastrad 	 */
   1815           1.1  riastrad 	__CTASSERT(0x5fa0000 == (UATP_MAX_SENSORS * UATP_MAX_SENSORS *
   1816           1.1  riastrad 		UATP_MAX_ACC * UATP_MAX_RATIO));
   1817           1.1  riastrad 	__CTASSERT(0x60000 == (UATP_MAX_SENSORS * UATP_MAX_POSITION));
   1818           1.1  riastrad 	CHECK_(n_sensors <= UATP_MAX_SENSORS);
   1819           1.1  riastrad 	CHECK_(ratio <= UATP_MAX_RATIO);
   1820           1.1  riastrad 
   1821           1.1  riastrad 	/*
   1822           1.1  riastrad 	 * Detect each finger by looking for a consecutive sequence of
   1823           1.1  riastrad 	 * increasing and then decreasing pressures above the sensor
   1824           1.1  riastrad 	 * threshold.  Compute the finger's position as the weighted
   1825           1.1  riastrad 	 * average of positions, weighted by the pressure at that
   1826           1.1  riastrad 	 * position.  Finally, return the average finger position.
   1827           1.1  riastrad 	 */
   1828           1.1  riastrad 
   1829           1.1  riastrad 	n_fingers = 0;
   1830  1.10.2.1.4.1     skrll 	memset(weighted, 0, sizeof(weighted));
   1831  1.10.2.1.4.1     skrll 	memset(total, 0, sizeof(total));
   1832  1.10.2.1.4.1     skrll 
   1833           1.1  riastrad 	for (i = 0; i < n_sensors; i++) {
   1834           1.1  riastrad 		CHECK_(0 <= acc[i]);
   1835           1.1  riastrad 		v = acc[i];
   1836           1.1  riastrad 
   1837           1.1  riastrad 		/* Ignore values outside a sensible interval.  */
   1838           1.1  riastrad 		if (v <= sensor_threshold) {
   1839           1.1  riastrad 			state = none;
   1840           1.1  riastrad 			continue;
   1841           1.1  riastrad 		} else if (UATP_MAX_ACC < v) {
   1842           1.1  riastrad 			aprint_verbose_dev(uatp_dev(sc),
   1843           1.1  riastrad 			    "ignoring large accumulated sensor state: %u\n",
   1844           1.1  riastrad 			    v);
   1845           1.1  riastrad 			continue;
   1846           1.1  riastrad 		}
   1847           1.1  riastrad 
   1848           1.1  riastrad 		switch (state) {
   1849           1.1  riastrad 		case none:
   1850           1.1  riastrad 			n_fingers += 1;
   1851           1.1  riastrad 			CHECK_(n_fingers <= n_sensors);
   1852           1.1  riastrad 			state = nondecreasing;
   1853           1.1  riastrad 			width = 1;
   1854           1.1  riastrad 			break;
   1855           1.1  riastrad 
   1856           1.1  riastrad 		case nondecreasing:
   1857           1.1  riastrad 		case decreasing:
   1858           1.1  riastrad 			CHECK_(0 < i);
   1859           1.1  riastrad 			CHECK_(0 <= acc[i - 1]);
   1860           1.1  riastrad 			width += 1;
   1861           1.1  riastrad 			if (palm_width <= (width * ratio)) {
   1862           1.1  riastrad 				DPRINTF(sc, UATP_DEBUG_PALM,
   1863           1.1  riastrad 				    ("palm detected\n"));
   1864           1.1  riastrad 				return 2;
   1865           1.1  riastrad 			} else if ((state == nondecreasing) &&
   1866           1.1  riastrad 			    ((unsigned int)acc[i - 1] > v)) {
   1867           1.1  riastrad 				state = decreasing;
   1868           1.1  riastrad 			} else if ((state == decreasing) &&
   1869           1.1  riastrad 			    ((unsigned int)acc[i - 1] < v)) {
   1870           1.1  riastrad 				n_fingers += 1;
   1871           1.1  riastrad 				CHECK_(n_fingers <= n_sensors);
   1872           1.1  riastrad 				state = nondecreasing;
   1873           1.1  riastrad 				width = 1;
   1874           1.1  riastrad 			}
   1875           1.1  riastrad 			break;
   1876           1.1  riastrad 
   1877           1.1  riastrad 		default:
   1878           1.1  riastrad 			aprint_error_dev(uatp_dev(sc),
   1879           1.1  riastrad 			    "bad finger detection state: %d", state);
   1880           1.1  riastrad 			return 0;
   1881           1.1  riastrad 		}
   1882           1.1  riastrad 
   1883           1.1  riastrad 		v -= sensor_normalizer;
   1884           1.1  riastrad 		total[n_fingers - 1] += v;
   1885           1.1  riastrad 		weighted[n_fingers - 1] += (i * v);
   1886           1.1  riastrad 		CHECK_(total[n_fingers - 1] <=
   1887           1.1  riastrad 		    (UATP_MAX_SENSORS * UATP_MAX_ACC));
   1888           1.1  riastrad 		CHECK_(weighted[n_fingers - 1] <=
   1889           1.1  riastrad 		    (UATP_MAX_SENSORS * UATP_MAX_SENSORS * UATP_MAX_ACC));
   1890           1.1  riastrad 	}
   1891           1.1  riastrad 
   1892           1.1  riastrad 	if (n_fingers == 0)
   1893           1.1  riastrad 		return 0;
   1894           1.1  riastrad 
   1895           1.1  riastrad 	sum = 0;
   1896           1.1  riastrad 	for (i = 0; i < n_fingers; i++) {
   1897           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_PARSE,
   1898           1.1  riastrad 		    ("finger at %u\n", ((weighted[i] * ratio) / total[i])));
   1899           1.1  riastrad 		sum += ((weighted[i] * ratio) / total[i]);
   1900           1.1  riastrad 		CHECK_(sum <= UATP_MAX_SENSORS * UATP_MAX_POSITION);
   1901           1.1  riastrad 	}
   1902           1.1  riastrad 
   1903           1.1  riastrad 	*fingers = n_fingers;
   1904           1.1  riastrad 	*position = (sum / n_fingers);
   1905           1.1  riastrad 	return 1;
   1906           1.1  riastrad 
   1907           1.1  riastrad #undef CHECK_
   1908           1.1  riastrad }
   1909  1.10.2.1.4.1     skrll 
   1910           1.1  riastrad /* Tapping */
   1911           1.1  riastrad 
   1912           1.1  riastrad /*
   1913           1.1  riastrad  * There is a very hairy state machine for detecting taps.  At every
   1914           1.1  riastrad  * touch, we record the maximum number of fingers touched, and don't
   1915           1.1  riastrad  * reset it to zero until the finger is released.
   1916           1.1  riastrad  *
   1917           1.1  riastrad  * INITIAL STATE
   1918           1.1  riastrad  * (no tapping fingers; no tapped fingers)
   1919           1.1  riastrad  * - On touch, go to TAPPING STATE.
   1920           1.1  riastrad  * - On any other input, remain in INITIAL STATE.
   1921           1.1  riastrad  *
   1922           1.1  riastrad  * TAPPING STATE: Finger touched; might be tap.
   1923           1.1  riastrad  * (tapping fingers; no tapped fingers)
   1924           1.1  riastrad  * - On release within the tap limit, go to TAPPED STATE.
   1925           1.1  riastrad  * - On release after the tap limit, go to INITIAL STATE.
   1926           1.1  riastrad  * - On any other input, remain in TAPPING STATE.
   1927           1.1  riastrad  *
   1928           1.1  riastrad  * TAPPED STATE: Finger recently tapped, and might double-tap.
   1929           1.1  riastrad  * (no tapping fingers; tapped fingers)
   1930           1.1  riastrad  * - On touch within the double-tap limit, go to DOUBLE-TAPPING STATE.
   1931           1.1  riastrad  * - On touch after the double-tap limit, go to TAPPING STATE.
   1932           1.1  riastrad  * - On no event after the double-tap limit, go to INITIAL STATE.
   1933           1.1  riastrad  * - On any other input, remain in TAPPED STATE.
   1934           1.1  riastrad  *
   1935           1.1  riastrad  * DOUBLE-TAPPING STATE: Finger touched soon after tap; might be double-tap.
   1936           1.1  riastrad  * (tapping fingers; tapped fingers)
   1937           1.1  riastrad  * - On release within the tap limit, release button and go to TAPPED STATE.
   1938           1.1  riastrad  * - On release after the tap limit, go to DRAGGING UP STATE.
   1939           1.1  riastrad  * - On touch after the tap limit, go to DRAGGING DOWN STATE.
   1940           1.1  riastrad  * - On any other input, remain in DOUBLE-TAPPING STATE.
   1941           1.1  riastrad  *
   1942           1.1  riastrad  * DRAGGING DOWN STATE: Finger has double-tapped and is dragging, not tapping.
   1943           1.1  riastrad  * (no tapping fingers; tapped fingers)
   1944           1.1  riastrad  * - On release, go to DRAGGING UP STATE.
   1945           1.1  riastrad  * - On any other input, remain in DRAGGING DOWN STATE.
   1946           1.1  riastrad  *
   1947           1.1  riastrad  * DRAGGING UP STATE: Finger has double-tapped and is up.
   1948           1.1  riastrad  * (no tapping fingers; tapped fingers)
   1949           1.1  riastrad  * - On touch, go to TAPPING IN DRAG STATE.
   1950           1.1  riastrad  * - On any other input, remain in DRAGGING UP STATE.
   1951           1.1  riastrad  *
   1952           1.1  riastrad  * TAPPING IN DRAG STATE: Tap-dancing while cross-dressed.
   1953           1.1  riastrad  * (tapping fingers; tapped fingers)
   1954           1.1  riastrad  * - On release within the tap limit, go to TAPPED STATE.
   1955           1.1  riastrad  * - On release after the tap limit, go to DRAGGING UP STATE.
   1956           1.1  riastrad  * - On any other input, remain in TAPPING IN DRAG STATE.
   1957           1.1  riastrad  *
   1958           1.1  riastrad  * Warning:  The graph of states is split into two components, those
   1959           1.1  riastrad  * with tapped fingers and those without.  The only path from any state
   1960           1.1  riastrad  * without tapped fingers to a state with tapped fingers must pass
   1961           1.1  riastrad  * through TAPPED STATE.  Also, the only transitions into TAPPED STATE
   1962           1.1  riastrad  * must be from states with tapping fingers, which become the tapped
   1963           1.1  riastrad  * fingers.  If you edit the state machine, you must either preserve
   1964           1.1  riastrad  * these properties, or globally transform the state machine to avoid
   1965           1.1  riastrad  * the bad consequences of violating these properties.
   1966           1.1  riastrad  */
   1967  1.10.2.1.4.1     skrll 
   1968           1.1  riastrad static void
   1969           1.1  riastrad uatp_tap_limit(const struct uatp_softc *sc, struct timeval *limit)
   1970           1.1  riastrad {
   1971           1.1  riastrad 	unsigned int msec = sc->sc_knobs.tap_limit_msec;
   1972           1.1  riastrad 	limit->tv_sec = 0;
   1973           1.1  riastrad 	limit->tv_usec = ((msec < 1000) ? (1000 * msec) : 100000);
   1974           1.1  riastrad }
   1975           1.1  riastrad 
   1976           1.1  riastrad #if UATP_DEBUG
   1977           1.1  riastrad 
   1978           1.1  riastrad #  define TAP_DEBUG_PRE(sc)	tap_debug((sc), __func__, "")
   1979           1.1  riastrad #  define TAP_DEBUG_POST(sc)	tap_debug((sc), __func__, " ->")
   1980           1.1  riastrad 
   1981           1.1  riastrad static void
   1982           1.1  riastrad tap_debug(struct uatp_softc *sc, const char *caller, const char *prefix)
   1983           1.1  riastrad {
   1984           1.1  riastrad 	char buffer[128];
   1985           1.1  riastrad 	const char *state;
   1986           1.1  riastrad 
   1987           1.1  riastrad 	KASSERT(mutex_owned(&sc->sc_tap_mutex));
   1988           1.1  riastrad 	switch (sc->sc_tap_state) {
   1989           1.1  riastrad 	case TAP_STATE_INITIAL:		state = "initial";		break;
   1990           1.1  riastrad 	case TAP_STATE_TAPPING:		state = "tapping";		break;
   1991           1.1  riastrad 	case TAP_STATE_TAPPED:		state = "tapped";		break;
   1992           1.1  riastrad 	case TAP_STATE_DOUBLE_TAPPING:	state = "double-tapping";	break;
   1993           1.1  riastrad 	case TAP_STATE_DRAGGING_DOWN:	state = "dragging-down";	break;
   1994           1.1  riastrad 	case TAP_STATE_DRAGGING_UP:	state = "dragging-up";		break;
   1995           1.1  riastrad 	case TAP_STATE_TAPPING_IN_DRAG:	state = "tapping-in-drag";	break;
   1996           1.1  riastrad 	default:
   1997  1.10.2.1.4.1     skrll 		snprintf(buffer, sizeof(buffer), "unknown (%d)",
   1998           1.1  riastrad 		    sc->sc_tap_state);
   1999           1.1  riastrad 		state = buffer;
   2000           1.1  riastrad 		break;
   2001           1.1  riastrad 	}
   2002           1.1  riastrad 
   2003           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_TAP,
   2004           1.1  riastrad 	    ("%s:%s state %s, %u tapping, %u tapped\n",
   2005           1.1  riastrad 		caller, prefix, state,
   2006           1.1  riastrad 		sc->sc_tapping_fingers, sc->sc_tapped_fingers));
   2007           1.1  riastrad }
   2008           1.1  riastrad 
   2009           1.1  riastrad #else	/* !UATP_DEBUG */
   2010           1.1  riastrad 
   2011           1.1  riastrad #  define TAP_DEBUG_PRE(sc)	do {} while (0)
   2012           1.1  riastrad #  define TAP_DEBUG_POST(sc)	do {} while (0)
   2013           1.1  riastrad 
   2014           1.1  riastrad #endif
   2015  1.10.2.1.4.1     skrll 
   2016           1.1  riastrad static void
   2017           1.1  riastrad tap_initialize(struct uatp_softc *sc)
   2018           1.1  riastrad {
   2019      1.10.2.1       snj 	callout_init(&sc->sc_untap_callout, 0);
   2020           1.1  riastrad 	callout_setfunc(&sc->sc_untap_callout, untap_callout, sc);
   2021  1.10.2.1.4.1     skrll 	mutex_init(&sc->sc_tap_mutex, MUTEX_DEFAULT, IPL_SOFTUSB);
   2022           1.1  riastrad 	cv_init(&sc->sc_tap_cv, "uatptap");
   2023           1.1  riastrad }
   2024           1.1  riastrad 
   2025           1.1  riastrad static void
   2026           1.1  riastrad tap_finalize(struct uatp_softc *sc)
   2027           1.1  riastrad {
   2028           1.1  riastrad 	/* XXX Can the callout still be scheduled here?  */
   2029           1.1  riastrad 	callout_destroy(&sc->sc_untap_callout);
   2030           1.1  riastrad 	mutex_destroy(&sc->sc_tap_mutex);
   2031           1.1  riastrad 	cv_destroy(&sc->sc_tap_cv);
   2032           1.1  riastrad }
   2033           1.1  riastrad 
   2034           1.1  riastrad static void
   2035           1.1  riastrad tap_enable(struct uatp_softc *sc)
   2036           1.1  riastrad {
   2037           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   2038           1.1  riastrad 	tap_transition_initial(sc);
   2039           1.1  riastrad 	sc->sc_buttons = 0;	/* XXX Not the right place?  */
   2040           1.1  riastrad 	sc->sc_all_buttons = 0;
   2041           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   2042           1.1  riastrad }
   2043           1.1  riastrad 
   2044           1.1  riastrad static void
   2045           1.1  riastrad tap_disable(struct uatp_softc *sc)
   2046           1.1  riastrad {
   2047           1.1  riastrad 	/* Reset tapping, and wait for any callouts to complete.  */
   2048           1.1  riastrad 	tap_reset_wait(sc);
   2049           1.1  riastrad }
   2050           1.1  riastrad 
   2051           1.1  riastrad /*
   2052           1.1  riastrad  * Reset tap state.  If the untap callout has just fired, it may signal
   2053           1.1  riastrad  * a harmless button release event before this returns.
   2054           1.1  riastrad  */
   2055           1.1  riastrad 
   2056           1.1  riastrad static void
   2057           1.1  riastrad tap_reset(struct uatp_softc *sc)
   2058           1.1  riastrad {
   2059           1.1  riastrad 	callout_stop(&sc->sc_untap_callout);
   2060           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   2061           1.1  riastrad 	tap_transition_initial(sc);
   2062           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   2063           1.1  riastrad }
   2064           1.1  riastrad 
   2065           1.1  riastrad /* Reset, but don't return until the callout is done running.  */
   2066           1.1  riastrad 
   2067           1.1  riastrad static void
   2068           1.1  riastrad tap_reset_wait(struct uatp_softc *sc)
   2069           1.1  riastrad {
   2070           1.1  riastrad 	bool fired = callout_stop(&sc->sc_untap_callout);
   2071           1.1  riastrad 
   2072           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   2073           1.1  riastrad 	if (fired)
   2074           1.1  riastrad 		while (sc->sc_tap_state == TAP_STATE_TAPPED)
   2075           1.1  riastrad 			if (cv_timedwait(&sc->sc_tap_cv, &sc->sc_tap_mutex,
   2076           1.1  riastrad 				mstohz(1000))) {
   2077           1.1  riastrad 				aprint_error_dev(uatp_dev(sc),
   2078           1.1  riastrad 				    "tap timeout\n");
   2079           1.1  riastrad 				break;
   2080           1.1  riastrad 			}
   2081           1.1  riastrad 	if (sc->sc_tap_state == TAP_STATE_TAPPED)
   2082           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "%s error\n", __func__);
   2083           1.1  riastrad 	tap_transition_initial(sc);
   2084           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   2085           1.1  riastrad }
   2086  1.10.2.1.4.1     skrll 
   2087           1.1  riastrad static const struct timeval zero_timeval;
   2088           1.1  riastrad 
   2089           1.1  riastrad static void
   2090           1.1  riastrad tap_transition(struct uatp_softc *sc, enum uatp_tap_state tap_state,
   2091           1.1  riastrad     const struct timeval *start_time,
   2092           1.1  riastrad     unsigned int tapping_fingers, unsigned int tapped_fingers)
   2093           1.1  riastrad {
   2094           1.1  riastrad 	KASSERT(mutex_owned(&sc->sc_tap_mutex));
   2095           1.1  riastrad 	sc->sc_tap_state = tap_state;
   2096           1.1  riastrad 	sc->sc_tap_timer = *start_time;
   2097           1.1  riastrad 	sc->sc_tapping_fingers = tapping_fingers;
   2098           1.1  riastrad 	sc->sc_tapped_fingers = tapped_fingers;
   2099           1.1  riastrad }
   2100           1.1  riastrad 
   2101           1.1  riastrad static void
   2102           1.1  riastrad tap_transition_initial(struct uatp_softc *sc)
   2103           1.1  riastrad {
   2104           1.1  riastrad 	/*
   2105           1.1  riastrad 	 * No checks.  This state is always kosher, and sometimes a
   2106           1.1  riastrad 	 * fallback in case of failure.
   2107           1.1  riastrad 	 */
   2108           1.1  riastrad 	tap_transition(sc, TAP_STATE_INITIAL, &zero_timeval, 0, 0);
   2109           1.1  riastrad }
   2110           1.1  riastrad 
   2111           1.1  riastrad /* Touch transitions */
   2112           1.1  riastrad 
   2113           1.1  riastrad static void
   2114           1.1  riastrad tap_transition_tapping(struct uatp_softc *sc, const struct timeval *start_time,
   2115           1.1  riastrad     unsigned int fingers)
   2116           1.1  riastrad {
   2117           1.1  riastrad 	CHECK((sc->sc_tapping_fingers <= fingers),
   2118           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2119           1.1  riastrad 	tap_transition(sc, TAP_STATE_TAPPING, start_time, fingers, 0);
   2120           1.1  riastrad }
   2121           1.1  riastrad 
   2122           1.1  riastrad static void
   2123           1.1  riastrad tap_transition_double_tapping(struct uatp_softc *sc,
   2124           1.1  riastrad     const struct timeval *start_time, unsigned int fingers)
   2125           1.1  riastrad {
   2126           1.1  riastrad 	CHECK((sc->sc_tapping_fingers <= fingers),
   2127           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2128           1.1  riastrad 	CHECK((0 < sc->sc_tapped_fingers),
   2129           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2130           1.1  riastrad 	tap_transition(sc, TAP_STATE_DOUBLE_TAPPING, start_time, fingers,
   2131           1.1  riastrad 	    sc->sc_tapped_fingers);
   2132           1.1  riastrad }
   2133  1.10.2.1.4.1     skrll 
   2134           1.1  riastrad static void
   2135           1.1  riastrad tap_transition_dragging_down(struct uatp_softc *sc)
   2136           1.1  riastrad {
   2137           1.1  riastrad 	CHECK((0 < sc->sc_tapped_fingers),
   2138           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2139           1.1  riastrad 	tap_transition(sc, TAP_STATE_DRAGGING_DOWN, &zero_timeval, 0,
   2140           1.1  riastrad 	    sc->sc_tapped_fingers);
   2141           1.1  riastrad }
   2142           1.1  riastrad 
   2143           1.1  riastrad static void
   2144           1.1  riastrad tap_transition_tapping_in_drag(struct uatp_softc *sc,
   2145           1.1  riastrad     const struct timeval *start_time, unsigned int fingers)
   2146           1.1  riastrad {
   2147           1.1  riastrad 	CHECK((sc->sc_tapping_fingers <= fingers),
   2148           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2149           1.1  riastrad 	CHECK((0 < sc->sc_tapped_fingers),
   2150           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2151           1.1  riastrad 	tap_transition(sc, TAP_STATE_TAPPING_IN_DRAG, start_time, fingers,
   2152           1.1  riastrad 	    sc->sc_tapped_fingers);
   2153           1.1  riastrad }
   2154           1.1  riastrad 
   2155           1.1  riastrad /* Release transitions */
   2156           1.1  riastrad 
   2157           1.1  riastrad static void
   2158           1.1  riastrad tap_transition_tapped(struct uatp_softc *sc, const struct timeval *start_time)
   2159           1.1  riastrad {
   2160           1.1  riastrad 	/*
   2161           1.1  riastrad 	 * The fingers that were tapping -- of which there must have
   2162           1.1  riastrad 	 * been at least one -- are now the fingers that have tapped,
   2163           1.1  riastrad 	 * and there are no longer fingers tapping.
   2164           1.1  riastrad 	 */
   2165           1.1  riastrad 	CHECK((0 < sc->sc_tapping_fingers),
   2166           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2167           1.1  riastrad 	tap_transition(sc, TAP_STATE_TAPPED, start_time, 0,
   2168           1.1  riastrad 	    sc->sc_tapping_fingers);
   2169           1.1  riastrad 	schedule_untap(sc);
   2170           1.1  riastrad }
   2171           1.1  riastrad 
   2172           1.1  riastrad static void
   2173           1.1  riastrad tap_transition_dragging_up(struct uatp_softc *sc)
   2174           1.1  riastrad {
   2175           1.1  riastrad 	CHECK((0 < sc->sc_tapped_fingers),
   2176           1.1  riastrad 	    do { tap_transition_initial(sc); return; } while (0));
   2177           1.1  riastrad 	tap_transition(sc, TAP_STATE_DRAGGING_UP, &zero_timeval, 0,
   2178           1.1  riastrad 	    sc->sc_tapped_fingers);
   2179           1.1  riastrad }
   2180  1.10.2.1.4.1     skrll 
   2181           1.1  riastrad static void
   2182           1.1  riastrad tap_touched(struct uatp_softc *sc, unsigned int fingers)
   2183           1.1  riastrad {
   2184           1.1  riastrad 	struct timeval now, diff, limit;
   2185           1.1  riastrad 
   2186           1.1  riastrad 	CHECK((0 < fingers), return);
   2187           1.1  riastrad 	callout_stop(&sc->sc_untap_callout);
   2188           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   2189           1.1  riastrad 	TAP_DEBUG_PRE(sc);
   2190           1.1  riastrad 	/*
   2191           1.1  riastrad 	 * Guarantee that the number of tapping fingers never decreases
   2192           1.1  riastrad 	 * except when it is reset to zero on release.
   2193           1.1  riastrad 	 */
   2194           1.1  riastrad 	if (fingers < sc->sc_tapping_fingers)
   2195           1.1  riastrad 		fingers = sc->sc_tapping_fingers;
   2196           1.1  riastrad 	switch (sc->sc_tap_state) {
   2197           1.1  riastrad 	case TAP_STATE_INITIAL:
   2198           1.1  riastrad 		getmicrouptime(&now);
   2199           1.1  riastrad 		tap_transition_tapping(sc, &now, fingers);
   2200           1.1  riastrad 		break;
   2201           1.1  riastrad 
   2202           1.1  riastrad 	case TAP_STATE_TAPPING:
   2203           1.1  riastrad 		/*
   2204           1.1  riastrad 		 * Number of fingers may have increased, so transition
   2205           1.1  riastrad 		 * even though we're already in TAPPING.
   2206           1.1  riastrad 		 */
   2207           1.1  riastrad 		tap_transition_tapping(sc, &sc->sc_tap_timer, fingers);
   2208           1.1  riastrad 		break;
   2209           1.1  riastrad 
   2210           1.1  riastrad 	case TAP_STATE_TAPPED:
   2211           1.1  riastrad 		getmicrouptime(&now);
   2212           1.1  riastrad 		/*
   2213           1.1  riastrad 		 * If the double-tap time limit has passed, it's the
   2214           1.1  riastrad 		 * callout's responsibility to handle that event, so we
   2215           1.1  riastrad 		 * assume the limit has not passed yet.
   2216           1.1  riastrad 		 */
   2217           1.1  riastrad 		tap_transition_double_tapping(sc, &now, fingers);
   2218           1.1  riastrad 		break;
   2219           1.1  riastrad 
   2220           1.1  riastrad 	case TAP_STATE_DOUBLE_TAPPING:
   2221           1.1  riastrad 		getmicrouptime(&now);
   2222           1.1  riastrad 		timersub(&now, &sc->sc_tap_timer, &diff);
   2223           1.1  riastrad 		uatp_tap_limit(sc, &limit);
   2224           1.1  riastrad 		if (timercmp(&diff, &limit, >) ||
   2225           1.1  riastrad 		    (sc->sc_track_distance >
   2226           1.1  riastrad 			sc->sc_knobs.tap_track_distance_limit))
   2227           1.1  riastrad 			tap_transition_dragging_down(sc);
   2228           1.1  riastrad 		break;
   2229           1.1  riastrad 
   2230           1.1  riastrad 	case TAP_STATE_DRAGGING_DOWN:
   2231           1.1  riastrad 		break;
   2232           1.1  riastrad 
   2233           1.1  riastrad 	case TAP_STATE_DRAGGING_UP:
   2234           1.1  riastrad 		getmicrouptime(&now);
   2235           1.1  riastrad 		tap_transition_tapping_in_drag(sc, &now, fingers);
   2236           1.1  riastrad 		break;
   2237           1.1  riastrad 
   2238           1.1  riastrad 	case TAP_STATE_TAPPING_IN_DRAG:
   2239           1.1  riastrad 		/*
   2240           1.1  riastrad 		 * Number of fingers may have increased, so transition
   2241           1.1  riastrad 		 * even though we're already in TAPPING IN DRAG.
   2242           1.1  riastrad 		 */
   2243           1.1  riastrad 		tap_transition_tapping_in_drag(sc, &sc->sc_tap_timer, fingers);
   2244           1.1  riastrad 		break;
   2245           1.1  riastrad 
   2246           1.1  riastrad 	default:
   2247           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
   2248           1.1  riastrad 		    __func__, sc->sc_tap_state);
   2249           1.1  riastrad 		tap_transition_initial(sc);
   2250           1.1  riastrad 		break;
   2251           1.1  riastrad 	}
   2252           1.1  riastrad 	TAP_DEBUG_POST(sc);
   2253           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   2254           1.1  riastrad }
   2255  1.10.2.1.4.1     skrll 
   2256           1.1  riastrad static bool
   2257           1.1  riastrad tap_released(struct uatp_softc *sc)
   2258           1.1  riastrad {
   2259           1.1  riastrad 	struct timeval now, diff, limit;
   2260           1.1  riastrad 	void (*non_tapped_transition)(struct uatp_softc *);
   2261           1.1  riastrad 	bool ok, temporary_release;
   2262           1.1  riastrad 
   2263           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   2264           1.1  riastrad 	TAP_DEBUG_PRE(sc);
   2265           1.1  riastrad 	switch (sc->sc_tap_state) {
   2266           1.1  riastrad 	case TAP_STATE_INITIAL:
   2267           1.1  riastrad 	case TAP_STATE_TAPPED:
   2268           1.1  riastrad 	case TAP_STATE_DRAGGING_UP:
   2269           1.1  riastrad 		/* Spurious interrupt: fingers are already off.  */
   2270           1.1  riastrad 		ok = false;
   2271           1.1  riastrad 		break;
   2272           1.1  riastrad 
   2273           1.1  riastrad 	case TAP_STATE_TAPPING:
   2274           1.1  riastrad 		temporary_release = false;
   2275           1.1  riastrad 		non_tapped_transition = &tap_transition_initial;
   2276           1.1  riastrad 		goto maybe_tap;
   2277           1.1  riastrad 
   2278           1.1  riastrad 	case TAP_STATE_DOUBLE_TAPPING:
   2279           1.1  riastrad 		temporary_release = true;
   2280           1.1  riastrad 		non_tapped_transition = &tap_transition_dragging_up;
   2281           1.1  riastrad 		goto maybe_tap;
   2282           1.1  riastrad 
   2283           1.1  riastrad 	case TAP_STATE_TAPPING_IN_DRAG:
   2284           1.1  riastrad 		temporary_release = false;
   2285           1.1  riastrad 		non_tapped_transition = &tap_transition_dragging_up;
   2286           1.1  riastrad 		goto maybe_tap;
   2287           1.1  riastrad 
   2288           1.1  riastrad 	maybe_tap:
   2289           1.1  riastrad 		getmicrouptime(&now);
   2290           1.1  riastrad 		timersub(&now, &sc->sc_tap_timer, &diff);
   2291           1.1  riastrad 		uatp_tap_limit(sc, &limit);
   2292           1.1  riastrad 		if (timercmp(&diff, &limit, <=) &&
   2293           1.1  riastrad 		    (sc->sc_track_distance <=
   2294           1.1  riastrad 			sc->sc_knobs.tap_track_distance_limit)) {
   2295           1.1  riastrad 			if (temporary_release) {
   2296           1.1  riastrad 				/*
   2297           1.1  riastrad 				 * XXX Kludge: Temporarily transition
   2298           1.1  riastrad 				 * to a tap state that uatp_input will
   2299           1.1  riastrad 				 * interpret as `no buttons tapped',
   2300           1.1  riastrad 				 * saving the tapping fingers.  There
   2301           1.1  riastrad 				 * should instead be a separate routine
   2302           1.1  riastrad 				 * uatp_input_untapped.
   2303           1.1  riastrad 				 */
   2304           1.1  riastrad 				unsigned int fingers = sc->sc_tapping_fingers;
   2305           1.1  riastrad 				tap_transition_initial(sc);
   2306           1.1  riastrad 				uatp_input(sc, 0, 0, 0, 0, 0);
   2307           1.1  riastrad 				sc->sc_tapping_fingers = fingers;
   2308           1.1  riastrad 			}
   2309           1.1  riastrad 			tap_transition_tapped(sc, &now);
   2310           1.1  riastrad 		} else {
   2311           1.1  riastrad 			(*non_tapped_transition)(sc);
   2312           1.1  riastrad 		}
   2313           1.1  riastrad 		ok = true;
   2314           1.1  riastrad 		break;
   2315           1.1  riastrad 
   2316           1.1  riastrad 	case TAP_STATE_DRAGGING_DOWN:
   2317           1.1  riastrad 		tap_transition_dragging_up(sc);
   2318           1.1  riastrad 		ok = true;
   2319           1.1  riastrad 		break;
   2320           1.1  riastrad 
   2321           1.1  riastrad 	default:
   2322           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
   2323           1.1  riastrad 		    __func__, sc->sc_tap_state);
   2324           1.1  riastrad 		tap_transition_initial(sc);
   2325           1.1  riastrad 		ok = false;
   2326           1.1  riastrad 		break;
   2327           1.1  riastrad 	}
   2328           1.1  riastrad 	TAP_DEBUG_POST(sc);
   2329           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   2330           1.1  riastrad 	return ok;
   2331           1.1  riastrad }
   2332  1.10.2.1.4.1     skrll 
   2333           1.1  riastrad /* Untapping: Releasing the button after a tap */
   2334           1.1  riastrad 
   2335           1.1  riastrad static void
   2336           1.1  riastrad schedule_untap(struct uatp_softc *sc)
   2337           1.1  riastrad {
   2338           1.1  riastrad 	unsigned int ms = sc->sc_knobs.double_tap_limit_msec;
   2339           1.1  riastrad 	if (ms <= 1000)
   2340           1.1  riastrad 		callout_schedule(&sc->sc_untap_callout, mstohz(ms));
   2341           1.1  riastrad 	else			/* XXX Reject bogus values in sysctl.  */
   2342           1.1  riastrad 		aprint_error_dev(uatp_dev(sc),
   2343           1.1  riastrad 		    "double-tap delay too long: %ums\n", ms);
   2344           1.1  riastrad }
   2345           1.1  riastrad 
   2346           1.1  riastrad static void
   2347           1.1  riastrad untap_callout(void *arg)
   2348           1.1  riastrad {
   2349           1.1  riastrad 	struct uatp_softc *sc = arg;
   2350           1.1  riastrad 
   2351           1.1  riastrad 	mutex_enter(&sc->sc_tap_mutex);
   2352           1.1  riastrad 	TAP_DEBUG_PRE(sc);
   2353           1.1  riastrad 	switch (sc->sc_tap_state) {
   2354           1.1  riastrad 	case TAP_STATE_TAPPED:
   2355           1.1  riastrad 		tap_transition_initial(sc);
   2356           1.1  riastrad 		/*
   2357           1.1  riastrad 		 * XXX Kludge: Call uatp_input after the state transition
   2358           1.1  riastrad 		 * to make sure that it will actually release the button.
   2359           1.1  riastrad 		 */
   2360           1.1  riastrad 		uatp_input(sc, 0, 0, 0, 0, 0);
   2361           1.1  riastrad 
   2362           1.1  riastrad 	case TAP_STATE_INITIAL:
   2363           1.1  riastrad 	case TAP_STATE_TAPPING:
   2364           1.1  riastrad 	case TAP_STATE_DOUBLE_TAPPING:
   2365           1.1  riastrad 	case TAP_STATE_DRAGGING_UP:
   2366           1.1  riastrad 	case TAP_STATE_DRAGGING_DOWN:
   2367           1.1  riastrad 	case TAP_STATE_TAPPING_IN_DRAG:
   2368           1.1  riastrad 		/*
   2369           1.1  riastrad 		 * Somebody else got in and changed the state before we
   2370           1.1  riastrad 		 * untapped.  Let them take over; do nothing here.
   2371           1.1  riastrad 		 */
   2372           1.1  riastrad 		break;
   2373           1.1  riastrad 
   2374           1.1  riastrad 	default:
   2375           1.1  riastrad 		aprint_error_dev(uatp_dev(sc), "%s: invalid tap state: %d\n",
   2376           1.1  riastrad 		    __func__, sc->sc_tap_state);
   2377           1.1  riastrad 		tap_transition_initial(sc);
   2378           1.1  riastrad 		/* XXX Just in case...?  */
   2379           1.1  riastrad 		uatp_input(sc, 0, 0, 0, 0, 0);
   2380           1.1  riastrad 		break;
   2381           1.1  riastrad 	}
   2382           1.1  riastrad 	TAP_DEBUG_POST(sc);
   2383           1.1  riastrad 	/* XXX Broadcast only if state was TAPPED?  */
   2384           1.1  riastrad 	cv_broadcast(&sc->sc_tap_cv);
   2385           1.1  riastrad 	mutex_exit(&sc->sc_tap_mutex);
   2386           1.1  riastrad }
   2387  1.10.2.1.4.1     skrll 
   2388           1.1  riastrad /*
   2389           1.1  riastrad  * Emulate different buttons if the user holds down n fingers while
   2390           1.1  riastrad  * pressing the physical button.  (This is unrelated to tapping.)
   2391           1.1  riastrad  */
   2392           1.1  riastrad 
   2393           1.1  riastrad static uint32_t
   2394           1.1  riastrad emulated_buttons(struct uatp_softc *sc, unsigned int fingers)
   2395           1.1  riastrad {
   2396           1.1  riastrad 	CHECK((1 < fingers), return 0);
   2397           1.1  riastrad 
   2398           1.1  riastrad 	switch (fingers) {
   2399           1.1  riastrad 	case 2:
   2400           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_EMUL_BUTTON,
   2401           1.1  riastrad 		    ("2-finger emulated button: %"PRIx32"\n",
   2402           1.1  riastrad 			sc->sc_knobs.two_finger_buttons));
   2403           1.1  riastrad 		return sc->sc_knobs.two_finger_buttons;
   2404           1.1  riastrad 
   2405           1.1  riastrad 	case 3:
   2406           1.1  riastrad 	default:
   2407           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_EMUL_BUTTON,
   2408           1.1  riastrad 		    ("3-finger emulated button: %"PRIx32"\n",
   2409           1.1  riastrad 			sc->sc_knobs.three_finger_buttons));
   2410           1.1  riastrad 		return sc->sc_knobs.three_finger_buttons;
   2411           1.1  riastrad 	}
   2412           1.1  riastrad }
   2413  1.10.2.1.4.1     skrll 
   2414           1.1  riastrad /*
   2415           1.1  riastrad  * Update the position known to the driver based on the position and
   2416           1.1  riastrad  * number of fingers.  dx, dy, dz, and dw are expected to hold zero;
   2417           1.1  riastrad  * update_position may store nonzero changes in position in them.
   2418           1.1  riastrad  */
   2419           1.1  riastrad 
   2420           1.1  riastrad static void
   2421           1.1  riastrad update_position(struct uatp_softc *sc, unsigned int fingers,
   2422           1.1  riastrad     unsigned int x_raw, unsigned int y_raw,
   2423           1.1  riastrad     int *dx, int *dy, int *dz, int *dw)
   2424           1.1  riastrad {
   2425           1.1  riastrad 	CHECK((0 < fingers), return);
   2426           1.1  riastrad 
   2427           1.1  riastrad 	if ((fingers == 1) || (sc->sc_knobs.multifinger_track == 1))
   2428           1.1  riastrad 		move_mouse(sc, x_raw, y_raw, dx, dy);
   2429           1.1  riastrad 	else if (sc->sc_knobs.multifinger_track == 2)
   2430           1.1  riastrad 		scroll_wheel(sc, x_raw, y_raw, dz, dw);
   2431           1.1  riastrad }
   2432           1.1  riastrad 
   2433           1.1  riastrad /*
   2434           1.1  riastrad  * XXX Scrolling needs to use a totally different motion model.
   2435           1.1  riastrad  */
   2436           1.1  riastrad 
   2437           1.1  riastrad static void
   2438           1.1  riastrad move_mouse(struct uatp_softc *sc, unsigned int x_raw, unsigned int y_raw,
   2439           1.1  riastrad     int *dx, int *dy)
   2440           1.1  riastrad {
   2441           1.1  riastrad 	move(sc, "mouse", x_raw, y_raw, &sc->sc_x_raw, &sc->sc_y_raw,
   2442           1.1  riastrad 	    &sc->sc_x_smoothed, &sc->sc_y_smoothed,
   2443           1.1  riastrad 	    &sc->sc_x_remainder, &sc->sc_y_remainder,
   2444           1.1  riastrad 	    dx, dy);
   2445           1.1  riastrad }
   2446           1.1  riastrad 
   2447           1.1  riastrad static void
   2448           1.1  riastrad scroll_wheel(struct uatp_softc *sc, unsigned int x_raw, unsigned int y_raw,
   2449           1.1  riastrad     int *dz, int *dw)
   2450           1.1  riastrad {
   2451           1.1  riastrad 	move(sc, "scroll", x_raw, y_raw, &sc->sc_z_raw, &sc->sc_w_raw,
   2452           1.1  riastrad 	    &sc->sc_z_smoothed, &sc->sc_w_smoothed,
   2453           1.1  riastrad 	    &sc->sc_z_remainder, &sc->sc_w_remainder,
   2454           1.1  riastrad 	    dz, dw);
   2455           1.1  riastrad }
   2456  1.10.2.1.4.1     skrll 
   2457           1.1  riastrad static void
   2458           1.1  riastrad move(struct uatp_softc *sc, const char *ctx, unsigned int a, unsigned int b,
   2459           1.1  riastrad     int *a_raw, int *b_raw,
   2460           1.1  riastrad     int *a_smoothed, int *b_smoothed,
   2461           1.1  riastrad     unsigned int *a_remainder, unsigned int *b_remainder,
   2462           1.1  riastrad     int *da, int *db)
   2463           1.1  riastrad {
   2464           1.1  riastrad #define CHECK_(condition) CHECK(condition, return)
   2465           1.1  riastrad 
   2466           1.1  riastrad 	int old_a_raw = *a_raw, old_a_smoothed = *a_smoothed;
   2467           1.1  riastrad 	int old_b_raw = *b_raw, old_b_smoothed = *b_smoothed;
   2468           1.1  riastrad 	unsigned int a_dist, b_dist, dist_squared;
   2469           1.1  riastrad 	bool a_fast, b_fast;
   2470           1.1  riastrad 
   2471           1.1  riastrad 	/*
   2472           1.1  riastrad 	 * Make sure the quadratics in motion_below_threshold and
   2473           1.1  riastrad 	 * tracking distance don't overflow int arithmetic.
   2474           1.1  riastrad 	 */
   2475           1.1  riastrad 	__CTASSERT(0x12000000 == (2 * UATP_MAX_POSITION * UATP_MAX_POSITION));
   2476           1.1  riastrad 
   2477           1.1  riastrad 	CHECK_(a <= UATP_MAX_POSITION);
   2478           1.1  riastrad 	CHECK_(b <= UATP_MAX_POSITION);
   2479           1.1  riastrad 	*a_raw = a;
   2480           1.1  riastrad 	*b_raw = b;
   2481           1.1  riastrad 	if ((old_a_raw < 0) || (old_b_raw < 0)) {
   2482           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_MOVE,
   2483           1.1  riastrad 		    ("initialize %s position (%d, %d) -> (%d, %d)\n", ctx,
   2484           1.1  riastrad 			old_a_raw, old_b_raw, a, b));
   2485           1.1  riastrad 		return;
   2486           1.1  riastrad 	}
   2487           1.1  riastrad 
   2488           1.1  riastrad 	if ((old_a_smoothed < 0) || (old_b_smoothed < 0)) {
   2489           1.1  riastrad 		/* XXX Does this make sense?  */
   2490           1.1  riastrad 		old_a_smoothed = old_a_raw;
   2491           1.1  riastrad 		old_b_smoothed = old_b_raw;
   2492           1.1  riastrad 	}
   2493           1.1  riastrad 
   2494           1.1  riastrad 	CHECK_(0 <= old_a_raw);
   2495           1.1  riastrad 	CHECK_(0 <= old_b_raw);
   2496           1.1  riastrad 	CHECK_(old_a_raw <= UATP_MAX_POSITION);
   2497           1.1  riastrad 	CHECK_(old_b_raw <= UATP_MAX_POSITION);
   2498           1.1  riastrad 	CHECK_(0 <= old_a_smoothed);
   2499           1.1  riastrad 	CHECK_(0 <= old_b_smoothed);
   2500           1.1  riastrad 	CHECK_(old_a_smoothed <= UATP_MAX_POSITION);
   2501           1.1  riastrad 	CHECK_(old_b_smoothed <= UATP_MAX_POSITION);
   2502           1.1  riastrad 	CHECK_(0 <= *a_raw);
   2503           1.1  riastrad 	CHECK_(0 <= *b_raw);
   2504           1.1  riastrad 	CHECK_(*a_raw <= UATP_MAX_POSITION);
   2505           1.1  riastrad 	CHECK_(*b_raw <= UATP_MAX_POSITION);
   2506           1.1  riastrad 	*a_smoothed = smooth(sc, old_a_raw, old_a_smoothed, *a_raw);
   2507           1.1  riastrad 	*b_smoothed = smooth(sc, old_b_raw, old_b_smoothed, *b_raw);
   2508           1.1  riastrad 	CHECK_(0 <= *a_smoothed);
   2509           1.1  riastrad 	CHECK_(0 <= *b_smoothed);
   2510           1.1  riastrad 	CHECK_(*a_smoothed <= UATP_MAX_POSITION);
   2511           1.1  riastrad 	CHECK_(*b_smoothed <= UATP_MAX_POSITION);
   2512  1.10.2.1.4.1     skrll 
   2513           1.1  riastrad 	if (sc->sc_motion_timer < sc->sc_knobs.motion_delay) {
   2514           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_MOVE, ("delay motion %u\n",
   2515           1.1  riastrad 			sc->sc_motion_timer));
   2516           1.1  riastrad 		sc->sc_motion_timer += 1;
   2517           1.1  riastrad 		return;
   2518           1.1  riastrad 	}
   2519           1.1  riastrad 
   2520           1.1  riastrad 	/* XXX Use raw distances or smoothed distances?  Acceleration?  */
   2521           1.1  riastrad 	if (*a_smoothed < old_a_smoothed)
   2522           1.1  riastrad 		a_dist = old_a_smoothed - *a_smoothed;
   2523           1.1  riastrad 	else
   2524           1.1  riastrad 		a_dist = *a_smoothed - old_a_smoothed;
   2525           1.1  riastrad 
   2526           1.1  riastrad 	if (*b_smoothed < old_b_smoothed)
   2527           1.1  riastrad 		b_dist = old_b_smoothed - *b_smoothed;
   2528           1.1  riastrad 	else
   2529           1.1  riastrad 		b_dist = *b_smoothed - old_b_smoothed;
   2530           1.1  riastrad 
   2531           1.1  riastrad 	dist_squared = (a_dist * a_dist) + (b_dist * b_dist);
   2532           1.1  riastrad 	if (dist_squared < ((2 * UATP_MAX_POSITION * UATP_MAX_POSITION)
   2533           1.1  riastrad 		- sc->sc_track_distance))
   2534           1.1  riastrad 		sc->sc_track_distance += dist_squared;
   2535           1.1  riastrad 	else
   2536           1.1  riastrad 		sc->sc_track_distance = (2 * UATP_MAX_POSITION *
   2537           1.1  riastrad 		    UATP_MAX_POSITION);
   2538           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_TRACK_DIST, ("finger has tracked %u units^2\n",
   2539           1.1  riastrad 		sc->sc_track_distance));
   2540           1.1  riastrad 
   2541           1.1  riastrad 	/*
   2542           1.1  riastrad 	 * The checks above guarantee that the differences here are at
   2543           1.1  riastrad 	 * most UATP_MAX_POSITION in magnitude, since both minuend and
   2544           1.1  riastrad 	 * subtrahend are nonnegative and at most UATP_MAX_POSITION.
   2545           1.1  riastrad 	 */
   2546           1.1  riastrad 	if (motion_below_threshold(sc, sc->sc_knobs.motion_threshold,
   2547           1.1  riastrad 		(int)(*a_smoothed - old_a_smoothed),
   2548           1.1  riastrad 		(int)(*b_smoothed - old_b_smoothed))) {
   2549           1.1  riastrad 		DPRINTF(sc, UATP_DEBUG_MOVE,
   2550           1.1  riastrad 		    ("%s motion too small: (%d, %d) -> (%d, %d)\n", ctx,
   2551           1.1  riastrad 			old_a_smoothed, old_b_smoothed,
   2552           1.1  riastrad 			*a_smoothed, *b_smoothed));
   2553           1.1  riastrad 		return;
   2554           1.1  riastrad 	}
   2555           1.1  riastrad 	if (sc->sc_knobs.fast_per_direction == 0) {
   2556           1.1  riastrad 		a_fast = b_fast = !motion_below_threshold(sc,
   2557           1.1  riastrad 		    sc->sc_knobs.fast_motion_threshold,
   2558           1.1  riastrad 		    (int)(*a_smoothed - old_a_smoothed),
   2559           1.1  riastrad 		    (int)(*b_smoothed - old_b_smoothed));
   2560           1.1  riastrad 	} else {
   2561           1.1  riastrad 		a_fast = !motion_below_threshold(sc,
   2562           1.1  riastrad 		    sc->sc_knobs.fast_motion_threshold,
   2563           1.1  riastrad 		    (int)(*a_smoothed - old_a_smoothed),
   2564           1.1  riastrad 		    0);
   2565           1.1  riastrad 		b_fast = !motion_below_threshold(sc,
   2566           1.1  riastrad 		    sc->sc_knobs.fast_motion_threshold,
   2567           1.1  riastrad 		    0,
   2568           1.1  riastrad 		    (int)(*b_smoothed - old_b_smoothed));
   2569           1.1  riastrad 	}
   2570           1.1  riastrad 	*da = accelerate(sc, old_a_raw, *a_raw, old_a_smoothed, *a_smoothed,
   2571           1.1  riastrad 	    a_fast, a_remainder);
   2572           1.1  riastrad 	*db = accelerate(sc, old_b_raw, *b_raw, old_b_smoothed, *b_smoothed,
   2573           1.1  riastrad 	    b_fast, b_remainder);
   2574           1.1  riastrad 	DPRINTF(sc, UATP_DEBUG_MOVE,
   2575           1.1  riastrad 	    ("update %s position (%d, %d) -> (%d, %d), move by (%d, %d)\n",
   2576           1.1  riastrad 		ctx, old_a_smoothed, old_b_smoothed, *a_smoothed, *b_smoothed,
   2577           1.1  riastrad 		*da, *db));
   2578           1.1  riastrad 
   2579           1.1  riastrad #undef CHECK_
   2580           1.1  riastrad }
   2581  1.10.2.1.4.1     skrll 
   2582           1.1  riastrad static int
   2583           1.1  riastrad smooth(struct uatp_softc *sc, unsigned int old_raw, unsigned int old_smoothed,
   2584           1.1  riastrad     unsigned int raw)
   2585           1.1  riastrad {
   2586           1.1  riastrad #define CHECK_(condition) CHECK(condition, return old_raw)
   2587           1.1  riastrad 
   2588           1.1  riastrad 	/*
   2589           1.1  riastrad 	 * Arithmetic bounds:
   2590           1.1  riastrad 	 * . the weights are at most UATP_MAX_WEIGHT;
   2591           1.1  riastrad 	 * . the positions are at most UATP_MAX_POSITION; and so
   2592           1.1  riastrad 	 * . the numerator of the average is at most
   2593           1.1  riastrad 	 *     3 * UATP_MAX_WEIGHT * UATP_MAX_POSITION,
   2594           1.1  riastrad 	 *   which is #x477000, fitting comfortably in an int.
   2595           1.1  riastrad 	 */
   2596           1.1  riastrad 	__CTASSERT(0x477000 == (3 * UATP_MAX_WEIGHT * UATP_MAX_POSITION));
   2597           1.1  riastrad 	unsigned int old_raw_weight = uatp_old_raw_weight(sc);
   2598           1.1  riastrad 	unsigned int old_smoothed_weight = uatp_old_smoothed_weight(sc);
   2599           1.1  riastrad 	unsigned int new_raw_weight = uatp_new_raw_weight(sc);
   2600           1.1  riastrad 	CHECK_(old_raw_weight <= UATP_MAX_WEIGHT);
   2601           1.1  riastrad 	CHECK_(old_smoothed_weight <= UATP_MAX_WEIGHT);
   2602           1.1  riastrad 	CHECK_(new_raw_weight <= UATP_MAX_WEIGHT);
   2603           1.1  riastrad 	CHECK_(old_raw <= UATP_MAX_POSITION);
   2604           1.1  riastrad 	CHECK_(old_smoothed <= UATP_MAX_POSITION);
   2605           1.1  riastrad 	CHECK_(raw <= UATP_MAX_POSITION);
   2606           1.1  riastrad 	return (((old_raw_weight * old_raw) +
   2607           1.1  riastrad 		(old_smoothed_weight * old_smoothed) +
   2608           1.1  riastrad 		(new_raw_weight * raw))
   2609           1.1  riastrad 	    / (old_raw_weight + old_smoothed_weight + new_raw_weight));
   2610           1.1  riastrad 
   2611           1.1  riastrad #undef CHECK_
   2612           1.1  riastrad }
   2613           1.1  riastrad 
   2614           1.1  riastrad static bool
   2615           1.1  riastrad motion_below_threshold(struct uatp_softc *sc, unsigned int threshold,
   2616           1.1  riastrad     int x, int y)
   2617           1.1  riastrad {
   2618           1.1  riastrad 	unsigned int x_squared, y_squared;
   2619           1.1  riastrad 
   2620           1.1  riastrad 	/* Caller guarantees the multiplication will not overflow.  */
   2621           1.1  riastrad 	KASSERT(-UATP_MAX_POSITION <= x);
   2622           1.1  riastrad 	KASSERT(-UATP_MAX_POSITION <= y);
   2623           1.1  riastrad 	KASSERT(x <= UATP_MAX_POSITION);
   2624           1.1  riastrad 	KASSERT(y <= UATP_MAX_POSITION);
   2625           1.1  riastrad 	__CTASSERT(0x12000000 == (2 * UATP_MAX_POSITION * UATP_MAX_POSITION));
   2626           1.1  riastrad 
   2627           1.1  riastrad 	x_squared = (x * x);
   2628           1.1  riastrad 	y_squared = (y * y);
   2629           1.1  riastrad 
   2630  1.10.2.1.4.3     skrll 	return (x_squared + y_squared) < threshold;
   2631           1.1  riastrad }
   2632           1.1  riastrad 
   2633           1.1  riastrad static int
   2634           1.1  riastrad accelerate(struct uatp_softc *sc, unsigned int old_raw, unsigned int raw,
   2635           1.1  riastrad     unsigned int old_smoothed, unsigned int smoothed, bool fast,
   2636           1.1  riastrad     int *remainder)
   2637           1.1  riastrad {
   2638           1.1  riastrad #define CHECK_(condition) CHECK(condition, return 0)
   2639           1.1  riastrad 
   2640           1.1  riastrad 	/* Guarantee that the scaling won't overflow.  */
   2641           1.1  riastrad 	__CTASSERT(0x30000 ==
   2642           1.1  riastrad 	    (UATP_MAX_POSITION * UATP_MAX_MOTION_MULTIPLIER));
   2643           1.1  riastrad 
   2644           1.1  riastrad 	CHECK_(old_raw <= UATP_MAX_POSITION);
   2645           1.1  riastrad 	CHECK_(raw <= UATP_MAX_POSITION);
   2646           1.1  riastrad 	CHECK_(old_smoothed <= UATP_MAX_POSITION);
   2647           1.1  riastrad 	CHECK_(smoothed <= UATP_MAX_POSITION);
   2648           1.1  riastrad 
   2649           1.1  riastrad 	return (fast ? uatp_scale_fast_motion : uatp_scale_motion)
   2650           1.1  riastrad 	    (sc, (((int) smoothed) - ((int) old_smoothed)), remainder);
   2651           1.1  riastrad 
   2652           1.1  riastrad #undef CHECK_
   2653           1.1  riastrad }
   2654  1.10.2.1.4.1     skrll 
   2655           1.9  riastrad MODULE(MODULE_CLASS_DRIVER, uatp, NULL);
   2656           1.9  riastrad 
   2657           1.9  riastrad #ifdef _MODULE
   2658           1.9  riastrad #include "ioconf.c"
   2659           1.9  riastrad #endif
   2660           1.9  riastrad 
   2661           1.9  riastrad static int
   2662           1.9  riastrad uatp_modcmd(modcmd_t cmd, void *aux)
   2663           1.9  riastrad {
   2664           1.9  riastrad 	int error = 0;
   2665           1.9  riastrad 
   2666           1.9  riastrad 	switch (cmd) {
   2667           1.9  riastrad 	case MODULE_CMD_INIT:
   2668           1.9  riastrad #ifdef _MODULE
   2669           1.9  riastrad 		error = config_init_component(cfdriver_ioconf_uatp,
   2670           1.9  riastrad 		    cfattach_ioconf_uatp, cfdata_ioconf_uatp);
   2671           1.9  riastrad #endif
   2672           1.9  riastrad 		return error;
   2673           1.9  riastrad 	case MODULE_CMD_FINI:
   2674           1.9  riastrad #ifdef _MODULE
   2675           1.9  riastrad 		error = config_fini_component(cfdriver_ioconf_uatp,
   2676           1.9  riastrad 		    cfattach_ioconf_uatp, cfdata_ioconf_uatp);
   2677           1.9  riastrad #endif
   2678           1.9  riastrad 		return error;
   2679           1.9  riastrad 	default:
   2680           1.9  riastrad 		return ENOTTY;
   2681           1.9  riastrad 	}
   2682           1.9  riastrad }
   2683