Home | History | Annotate | Line # | Download | only in dev
pbms.c revision 1.3.2.2
      1  1.3.2.2  yamt /* $Id: pbms.c,v 1.3.2.2 2006/02/18 15:38:41 yamt Exp $ */
      2  1.3.2.2  yamt 
      3  1.3.2.2  yamt /*
      4  1.3.2.2  yamt  * Copyright (c) 2005, Johan Walln
      5  1.3.2.2  yamt  * All rights reserved.
      6  1.3.2.2  yamt  *
      7  1.3.2.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.3.2.2  yamt  * modification, are permitted provided that the following conditions are
      9  1.3.2.2  yamt  * met:
     10  1.3.2.2  yamt  *
     11  1.3.2.2  yamt  *   1. Redistributions of source code must retain the above copyright
     12  1.3.2.2  yamt  *      notice, this list of conditions and the following disclaimer.
     13  1.3.2.2  yamt  *
     14  1.3.2.2  yamt  *   2. Redistributions in binary form must reproduce the above
     15  1.3.2.2  yamt  *      copyright notice, this list of conditions and the following
     16  1.3.2.2  yamt  *      disclaimer in the documentation and/or other materials provided
     17  1.3.2.2  yamt  *      with the distribution.
     18  1.3.2.2  yamt  *
     19  1.3.2.2  yamt  *   3. The name of the copyright holder may not be used to endorse or
     20  1.3.2.2  yamt  *      promote products derived from this software without specific
     21  1.3.2.2  yamt  *      prior written permission.
     22  1.3.2.2  yamt  *
     23  1.3.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
     24  1.3.2.2  yamt  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.3.2.2  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.3.2.2  yamt  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE
     27  1.3.2.2  yamt  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.3.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.3.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     30  1.3.2.2  yamt  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31  1.3.2.2  yamt  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32  1.3.2.2  yamt  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33  1.3.2.2  yamt  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.3.2.2  yamt  */
     35  1.3.2.2  yamt 
     36  1.3.2.2  yamt /*
     37  1.3.2.2  yamt  * The pbms driver provides support for the trackpad on new (post
     38  1.3.2.2  yamt  * February 2005) Apple PowerBooks (and iBooks?) that are not standard
     39  1.3.2.2  yamt  * USB HID mice.
     40  1.3.2.2  yamt  */
     41  1.3.2.2  yamt 
     42  1.3.2.2  yamt /*
     43  1.3.2.2  yamt  * The protocol (that is, the interpretation of the data generated by
     44  1.3.2.2  yamt  * the trackpad) is taken from the Linux appletouch driver version
     45  1.3.2.2  yamt  * 0.08 by Johannes Berg, Stelian Pop and Frank Arnold.  The method
     46  1.3.2.2  yamt  * used to detect fingers on the trackpad is also taken from that
     47  1.3.2.2  yamt  * driver.
     48  1.3.2.2  yamt  */
     49  1.3.2.2  yamt 
     50  1.3.2.2  yamt /*
     51  1.3.2.2  yamt  * To add support for other devices using the same protocol, add an
     52  1.3.2.2  yamt  * entry to the pbms_devices table below.  See the comments for
     53  1.3.2.2  yamt  * pbms_devices and struct pbms_devs.
     54  1.3.2.2  yamt  */
     55  1.3.2.2  yamt 
     56  1.3.2.2  yamt /*
     57  1.3.2.2  yamt  * PROTOCOL:
     58  1.3.2.2  yamt  *
     59  1.3.2.2  yamt  * The driver transfers continuously 81 byte events.  The last byte is
     60  1.3.2.2  yamt  * 1 if the button is pressed, and is 0 otherwise. Of the remaining
     61  1.3.2.2  yamt  * bytes, 26 + 16 = 42 are sensors detecting pressure in the X or
     62  1.3.2.2  yamt  * horizontal, and Y or vertical directions, respectively.  On 12 and
     63  1.3.2.2  yamt  * 15 inch PowerBooks, only the 16 first sensors in the X-direction
     64  1.3.2.2  yamt  * are used. In the X-direction, the sensors correspond to byte
     65  1.3.2.2  yamt  * positions
     66  1.3.2.2  yamt  *
     67  1.3.2.2  yamt  *   2, 7, 12, 17, 22, 27, 32, 37, 4, 9, 14, 19, 24, 29, 34, 39, 42,
     68  1.3.2.2  yamt  *   47, 52, 57, 62, 67, 72, 77, 44 and 49;
     69  1.3.2.2  yamt  *
     70  1.3.2.2  yamt  * in the Y direction, the sensors correspond to byte positions
     71  1.3.2.2  yamt  *
     72  1.3.2.2  yamt  *   1, 6, 11, 16, 21, 26, 31, 36, 3, 8, 13, 18, 23, 28, 33 and 38.
     73  1.3.2.2  yamt  *
     74  1.3.2.2  yamt  * The change in the sensor values over time is more interesting than
     75  1.3.2.2  yamt  * their absolute values: if the pressure increases, we know that the
     76  1.3.2.2  yamt  * finger has just moved there.
     77  1.3.2.2  yamt  *
     78  1.3.2.2  yamt  * We keep track of the previous sample (of sensor values in the X and
     79  1.3.2.2  yamt  * Y directions) and the accumulated change for each sensor.  When we
     80  1.3.2.2  yamt  * receive a new sample, we add the difference of the new sensor value
     81  1.3.2.2  yamt  * and the old value to the accumulated change.  If the accumulator
     82  1.3.2.2  yamt  * becomes negative, we set it to zero.  The effect is that the
     83  1.3.2.2  yamt  * accumulator is large for sensors whose pressure has recently
     84  1.3.2.2  yamt  * increased.  If there is little change in pressure (or if the
     85  1.3.2.2  yamt  * pressure decreases), the accumulator drifts back to zero.
     86  1.3.2.2  yamt  *
     87  1.3.2.2  yamt  * Since there is some fluctuations, we ignore accumulator values
     88  1.3.2.2  yamt  * below a threshold.  The raw finger position is computed as a
     89  1.3.2.2  yamt  * weighted average of the other sensors (the weights are the
     90  1.3.2.2  yamt  * accumulated changes).
     91  1.3.2.2  yamt  *
     92  1.3.2.2  yamt  * For smoothing, we keep track of the previous raw finger position,
     93  1.3.2.2  yamt  * and the virtual position reported to wsmouse.  The new raw position
     94  1.3.2.2  yamt  * is computed as a weighted average of the old raw position and the
     95  1.3.2.2  yamt  * computed raw position.  Since this still generates some noise, we
     96  1.3.2.2  yamt  * compute a new virtual position as a weighted average of the previous
     97  1.3.2.2  yamt  * virtual position and the new raw position.  The weights are
     98  1.3.2.2  yamt  * controlled by the raw change and a noise parameter.  The position
     99  1.3.2.2  yamt  * is reported as a relative position.
    100  1.3.2.2  yamt  */
    101  1.3.2.2  yamt 
    102  1.3.2.2  yamt /*
    103  1.3.2.2  yamt  * TODO:
    104  1.3.2.2  yamt  *
    105  1.3.2.2  yamt  * Add support for other drivers of the same type.
    106  1.3.2.2  yamt  *
    107  1.3.2.2  yamt  * Add support for tapping and two-finger scrolling?  The
    108  1.3.2.2  yamt  * implementation already detects two fingers, so this should be
    109  1.3.2.2  yamt  * relatively easy.
    110  1.3.2.2  yamt  *
    111  1.3.2.2  yamt  * Implement some of the mouse ioctls?
    112  1.3.2.2  yamt  *
    113  1.3.2.2  yamt  * Take care of the XXXs.
    114  1.3.2.2  yamt  *
    115  1.3.2.2  yamt  */
    116  1.3.2.2  yamt 
    117  1.3.2.2  yamt #include <sys/cdefs.h>
    118  1.3.2.2  yamt 
    119  1.3.2.2  yamt #include <sys/param.h>
    120  1.3.2.2  yamt #include <sys/device.h>
    121  1.3.2.2  yamt #include <sys/errno.h>
    122  1.3.2.2  yamt 
    123  1.3.2.2  yamt #include <sys/ioctl.h>
    124  1.3.2.2  yamt #include <sys/systm.h>
    125  1.3.2.2  yamt #include <sys/tty.h>
    126  1.3.2.2  yamt 
    127  1.3.2.2  yamt #include <dev/usb/usb.h>
    128  1.3.2.2  yamt #include <dev/usb/usbdi.h>
    129  1.3.2.2  yamt #include <dev/usb/usbdevs.h>
    130  1.3.2.2  yamt #include <dev/usb/uhidev.h>
    131  1.3.2.2  yamt 
    132  1.3.2.2  yamt #include <dev/wscons/wsconsio.h>
    133  1.3.2.2  yamt #include <dev/wscons/wsmousevar.h>
    134  1.3.2.2  yamt 
    135  1.3.2.2  yamt 
    136  1.3.2.2  yamt /*
    137  1.3.2.2  yamt  * Debugging output.
    138  1.3.2.2  yamt  */
    139  1.3.2.2  yamt 
    140  1.3.2.2  yamt 
    141  1.3.2.2  yamt /* XXX Should be redone, and its use should be added back. */
    142  1.3.2.2  yamt 
    143  1.3.2.2  yamt #ifdef PBMS_DEBUG
    144  1.3.2.2  yamt 
    145  1.3.2.2  yamt /*
    146  1.3.2.2  yamt  * Print the error message (preceded by the driver and function)
    147  1.3.2.2  yamt  * specified by the string literal fmt (followed by newline) if
    148  1.3.2.2  yamt  * pbmsdebug is greater than n. The macro may only be used in the
    149  1.3.2.2  yamt  * scope of sc, which must be castable to struct device *. There must
    150  1.3.2.2  yamt  * be at least one vararg. Do not define PBMS_DEBUG on non-C99
    151  1.3.2.2  yamt  * compilers.
    152  1.3.2.2  yamt  */
    153  1.3.2.2  yamt 
    154  1.3.2.2  yamt #define DPRINTFN(n, fmt, ...)						      \
    155  1.3.2.2  yamt do {									      \
    156  1.3.2.2  yamt 	if (pbmsdebug > (n))						      \
    157  1.3.2.2  yamt 		logprintf("%s: %s: " fmt "\n",				      \
    158  1.3.2.2  yamt 			  ((struct device *) sc)->dv_xname,		      \
    159  1.3.2.2  yamt 			  __func__, __VA_ARGS__);			      \
    160  1.3.2.2  yamt } while ( /* CONSTCOND */ 0)
    161  1.3.2.2  yamt 
    162  1.3.2.2  yamt int pbmsdebug = 0;
    163  1.3.2.2  yamt 
    164  1.3.2.2  yamt #endif /* PBMS_DEBUG */
    165  1.3.2.2  yamt 
    166  1.3.2.2  yamt 
    167  1.3.2.2  yamt /*
    168  1.3.2.2  yamt  * Magic numbers.
    169  1.3.2.2  yamt  */
    170  1.3.2.2  yamt 
    171  1.3.2.2  yamt 
    172  1.3.2.2  yamt /* The amount of data transfered by the USB device. */
    173  1.3.2.2  yamt #define PBMS_DATA_LEN 81
    174  1.3.2.2  yamt 
    175  1.3.2.2  yamt /* The maximum number of sensors. */
    176  1.3.2.2  yamt #define PBMS_X_SENSORS 26
    177  1.3.2.2  yamt #define PBMS_Y_SENSORS 16
    178  1.3.2.2  yamt #define PBMS_SENSORS (PBMS_X_SENSORS + PBMS_Y_SENSORS)
    179  1.3.2.2  yamt 
    180  1.3.2.2  yamt /*
    181  1.3.2.2  yamt  * Parameters for supported devices.  For generality, these parameters
    182  1.3.2.2  yamt  * can be different for each device.  The meanings of the parameters
    183  1.3.2.2  yamt  * are as follows.
    184  1.3.2.2  yamt  *
    185  1.3.2.2  yamt  * desc:      A printable description used for dmesg output.
    186  1.3.2.2  yamt  *
    187  1.3.2.2  yamt  * noise:     Amount of noise in the computed position. This controls
    188  1.3.2.2  yamt  *            how large a change must be to get reported, and how
    189  1.3.2.2  yamt  *            large enough changes are smoothed.  A good value can
    190  1.3.2.2  yamt  *            probably only be found experimentally, but something around
    191  1.3.2.2  yamt  *            16 seems suitable.
    192  1.3.2.2  yamt  *
    193  1.3.2.2  yamt  * product:   The product ID of the trackpad.
    194  1.3.2.2  yamt  *
    195  1.3.2.2  yamt  *
    196  1.3.2.2  yamt  * threshold: Accumulated changes less than this are ignored.  A good
    197  1.3.2.2  yamt  *            value could be determined experimentally, but 5 is a
    198  1.3.2.2  yamt  *            reasonable guess.
    199  1.3.2.2  yamt  *
    200  1.3.2.2  yamt  * vendor:    The vendor ID.  Currently USB_VENDOR_APPLE for all devices.
    201  1.3.2.2  yamt  *
    202  1.3.2.2  yamt  * x_factor:  Factor used in computations with X-coordinates.  If the
    203  1.3.2.2  yamt  *            x-resolution of the display is x, this should be
    204  1.3.2.2  yamt  *            (x + 1) / (x_sensors - 1).  Other values work fine, but
    205  1.3.2.2  yamt  *            then the aspect ratio is not necessarily kept.
    206  1.3.2.2  yamt  *
    207  1.3.2.2  yamt  * x_sensors: The number of sensors in the X-direction.
    208  1.3.2.2  yamt  *
    209  1.3.2.2  yamt  * y_factor:  As x_factors, but for Y-coordinates.
    210  1.3.2.2  yamt  *
    211  1.3.2.2  yamt  * y_sensors: The number of sensors in the Y-direction.
    212  1.3.2.2  yamt  */
    213  1.3.2.2  yamt 
    214  1.3.2.2  yamt struct pbms_dev {
    215  1.3.2.2  yamt 	const char *descr; /* Description of the driver (for dmesg). */
    216  1.3.2.2  yamt 	int noise;	   /* Amount of noise in the computed position. */
    217  1.3.2.2  yamt 	int threshold;	   /* Changes less than this are ignored. */
    218  1.3.2.2  yamt 	int x_factor;	   /* Factor used in computation with X-coordinates. */
    219  1.3.2.2  yamt 	int x_sensors;	   /* The number of X-sensors. */
    220  1.3.2.2  yamt 	int y_factor;	   /* Factor used in computation with Y-coordinates. */
    221  1.3.2.2  yamt 	int y_sensors;	   /* The number of Y-sensors. */
    222  1.3.2.2  yamt 	uint16_t product;  /* Product ID. */
    223  1.3.2.2  yamt 	uint16_t vendor;   /* The vendor ID. */
    224  1.3.2.2  yamt };
    225  1.3.2.2  yamt 
    226  1.3.2.2  yamt /* Devices supported by this driver. */
    227  1.3.2.2  yamt static struct pbms_dev pbms_devices[] =
    228  1.3.2.2  yamt {
    229  1.3.2.2  yamt #define POWERBOOK_TOUCHPAD(inches, prod, x_fact, x_sens, y_fact)	      \
    230  1.3.2.2  yamt        {								      \
    231  1.3.2.2  yamt 		.descr = #inches " inch PowerBook Trackpad",		      \
    232  1.3.2.2  yamt 		.vendor = USB_VENDOR_APPLE,				      \
    233  1.3.2.2  yamt 		.product = (prod),					      \
    234  1.3.2.2  yamt 		.noise = 16,						      \
    235  1.3.2.2  yamt 		.threshold = 5,						      \
    236  1.3.2.2  yamt 		.x_factor = (x_fact),					      \
    237  1.3.2.2  yamt 		.x_sensors = (x_sens),					      \
    238  1.3.2.2  yamt 		.y_factor = (y_fact),					      \
    239  1.3.2.2  yamt 		.y_sensors = 16						      \
    240  1.3.2.2  yamt        }
    241  1.3.2.2  yamt        /* 12 inch PowerBooks */
    242  1.3.2.2  yamt        POWERBOOK_TOUCHPAD(12, 0x030a, 69, 16, 52), /* XXX Not tested. */
    243  1.3.2.2  yamt        /* 15 inch PowerBooks */
    244  1.3.2.2  yamt        POWERBOOK_TOUCHPAD(15, 0x020e, 85, 16, 57), /* XXX Not tested. */
    245  1.3.2.2  yamt        POWERBOOK_TOUCHPAD(15, 0x020f, 85, 16, 57),
    246  1.3.2.2  yamt        /* 17 inch PowerBooks */
    247  1.3.2.2  yamt        POWERBOOK_TOUCHPAD(17, 0x020d, 71, 26, 68)  /* XXX Not tested. */
    248  1.3.2.2  yamt #undef POWERBOOK_TOUCHPAD
    249  1.3.2.2  yamt };
    250  1.3.2.2  yamt 
    251  1.3.2.2  yamt /* The number of supported devices. */
    252  1.3.2.2  yamt #define PBMS_NUM_DEVICES (sizeof(pbms_devices) / sizeof(pbms_devices[0]))
    253  1.3.2.2  yamt 
    254  1.3.2.2  yamt 
    255  1.3.2.2  yamt /*
    256  1.3.2.2  yamt  * Types and prototypes.
    257  1.3.2.2  yamt  */
    258  1.3.2.2  yamt 
    259  1.3.2.2  yamt 
    260  1.3.2.2  yamt /* Device data. */
    261  1.3.2.2  yamt struct pbms_softc {
    262  1.3.2.2  yamt 	struct uhidev sc_hdev;	      /* USB parent (got the struct device). */
    263  1.3.2.2  yamt 	int sc_acc[PBMS_SENSORS];     /* Accumulated sensor values. */
    264  1.3.2.2  yamt 	signed char sc_prev[PBMS_SENSORS];   /* Previous sample. */
    265  1.3.2.2  yamt 	signed char sc_sample[PBMS_SENSORS]; /* Current sample. */
    266  1.3.2.2  yamt 	struct device *sc_wsmousedev; /* WSMouse device. */
    267  1.3.2.2  yamt 	int sc_noise;		      /* Amount of noise. */
    268  1.3.2.2  yamt 	int sc_theshold;	      /* Threshold value. */
    269  1.3.2.2  yamt 	int sc_x;		      /* Virtual position in horizontal
    270  1.3.2.2  yamt 				       * direction (wsmouse position). */
    271  1.3.2.2  yamt 	int sc_x_factor;	      /* X-coordinate factor. */
    272  1.3.2.2  yamt 	int sc_x_raw;		      /* X-position of finger on trackpad. */
    273  1.3.2.2  yamt 	int sc_x_sensors;	      /* Number of X-sensors. */
    274  1.3.2.2  yamt 	int sc_y;		      /* Virtual position in vertical direction
    275  1.3.2.2  yamt 				       * (wsmouse position). */
    276  1.3.2.2  yamt 	int sc_y_factor;	      /* Y-coordinate factor. */
    277  1.3.2.2  yamt 	int sc_y_raw;		      /* Y-position of finger on trackpad. */
    278  1.3.2.2  yamt 	int sc_y_sensors;	      /* Number of Y-sensors. */
    279  1.3.2.2  yamt 	uint32_t sc_buttons;	      /* Button state. */
    280  1.3.2.2  yamt 	uint32_t sc_status;	      /* Status flags. */
    281  1.3.2.2  yamt #define PBMS_ENABLED 1		      /* Is the device enabled? */
    282  1.3.2.2  yamt #define PBMS_DYING 2		      /* Is the device dying? */
    283  1.3.2.2  yamt #define PBMS_VALID 4		      /* Is the previous sample valid? */
    284  1.3.2.2  yamt };
    285  1.3.2.2  yamt 
    286  1.3.2.2  yamt 
    287  1.3.2.2  yamt /* Static function prototypes. */
    288  1.3.2.2  yamt static void pbms_intr(struct uhidev *, void *, unsigned int);
    289  1.3.2.2  yamt static int pbms_enable(void *);
    290  1.3.2.2  yamt static void pbms_disable(void *);
    291  1.3.2.2  yamt static int pbms_ioctl(void *, unsigned long, caddr_t, int, struct lwp *);
    292  1.3.2.2  yamt static void reorder_sample(signed char *, signed char *);
    293  1.3.2.2  yamt static int compute_delta(struct pbms_softc *, int *, int *, int *, uint32_t *);
    294  1.3.2.2  yamt static int detect_pos(int *, int, int, int, int *, int *);
    295  1.3.2.2  yamt static int smooth_pos(int, int, int);
    296  1.3.2.2  yamt 
    297  1.3.2.2  yamt /* Access methods for wsmouse. */
    298  1.3.2.2  yamt const struct wsmouse_accessops pbms_accessops = {
    299  1.3.2.2  yamt 	pbms_enable,
    300  1.3.2.2  yamt 	pbms_ioctl,
    301  1.3.2.2  yamt 	pbms_disable,
    302  1.3.2.2  yamt };
    303  1.3.2.2  yamt 
    304  1.3.2.2  yamt /* This take cares also of the basic device registration. */
    305  1.3.2.2  yamt USB_DECLARE_DRIVER(pbms);
    306  1.3.2.2  yamt 
    307  1.3.2.2  yamt 
    308  1.3.2.2  yamt /*
    309  1.3.2.2  yamt  * Basic driver.
    310  1.3.2.2  yamt  */
    311  1.3.2.2  yamt 
    312  1.3.2.2  yamt 
    313  1.3.2.2  yamt /* Try to match the device at some uhidev. */
    314  1.3.2.2  yamt 
    315  1.3.2.2  yamt int
    316  1.3.2.2  yamt pbms_match(struct device *parent, struct cfdata *match, void *aux)
    317  1.3.2.2  yamt {
    318  1.3.2.2  yamt 	struct uhidev_attach_arg *uha = aux;
    319  1.3.2.2  yamt 	usb_device_descriptor_t *udd;
    320  1.3.2.2  yamt 	int i;
    321  1.3.2.2  yamt 	uint16_t vendor, product;
    322  1.3.2.2  yamt 
    323  1.3.2.2  yamt 	/*
    324  1.3.2.2  yamt 	 * We just check if the vendor and product IDs have the magic numbers
    325  1.3.2.2  yamt 	 * we expect.
    326  1.3.2.2  yamt 	 */
    327  1.3.2.2  yamt 	if ((udd = usbd_get_device_descriptor(uha->parent->sc_udev)) != NULL) {
    328  1.3.2.2  yamt 		vendor = UGETW(udd->idVendor);
    329  1.3.2.2  yamt 		product = UGETW(udd->idProduct);
    330  1.3.2.2  yamt 		for (i = 0; i < PBMS_NUM_DEVICES; i++) {
    331  1.3.2.2  yamt 			if (vendor == pbms_devices[i].vendor &&
    332  1.3.2.2  yamt 			    product == pbms_devices[i].product)
    333  1.3.2.2  yamt 				return UMATCH_IFACECLASS;
    334  1.3.2.2  yamt 		}
    335  1.3.2.2  yamt 	}
    336  1.3.2.2  yamt 	return UMATCH_NONE;
    337  1.3.2.2  yamt }
    338  1.3.2.2  yamt 
    339  1.3.2.2  yamt 
    340  1.3.2.2  yamt /* Attach the device. */
    341  1.3.2.2  yamt 
    342  1.3.2.2  yamt void
    343  1.3.2.2  yamt pbms_attach(struct device *parent, struct device *self, void *aux)
    344  1.3.2.2  yamt {
    345  1.3.2.2  yamt 	struct wsmousedev_attach_args a;
    346  1.3.2.2  yamt 	struct uhidev_attach_arg *uha = aux;
    347  1.3.2.2  yamt 	struct pbms_dev *pd;
    348  1.3.2.2  yamt 	struct pbms_softc *sc = (struct pbms_softc *)self;
    349  1.3.2.2  yamt 	usb_device_descriptor_t *udd;
    350  1.3.2.2  yamt 	int i;
    351  1.3.2.2  yamt 	uint16_t vendor, product;
    352  1.3.2.2  yamt 
    353  1.3.2.2  yamt 	sc->sc_hdev.sc_intr = pbms_intr;
    354  1.3.2.2  yamt 	sc->sc_hdev.sc_parent = uha->parent;
    355  1.3.2.2  yamt 	sc->sc_hdev.sc_report_id = uha->reportid;
    356  1.3.2.2  yamt 
    357  1.3.2.2  yamt 	/* Fill in device-specific parameters. */
    358  1.3.2.2  yamt 	if ((udd = usbd_get_device_descriptor(uha->parent->sc_udev)) != NULL) {
    359  1.3.2.2  yamt 		product = UGETW(udd->idProduct);
    360  1.3.2.2  yamt 		vendor = UGETW(udd->idVendor);
    361  1.3.2.2  yamt 		for (i = 0; i < PBMS_NUM_DEVICES; i++) {
    362  1.3.2.2  yamt 			pd = &pbms_devices[i];
    363  1.3.2.2  yamt 			if (product == pd->product && vendor == pd->vendor) {
    364  1.3.2.2  yamt 				printf(": %s\n", pd->descr);
    365  1.3.2.2  yamt 				sc->sc_noise = pd->noise;
    366  1.3.2.2  yamt 				sc->sc_theshold = pd->threshold;
    367  1.3.2.2  yamt 				sc->sc_x_factor = pd->x_factor;
    368  1.3.2.2  yamt 				sc->sc_x_sensors = pd->x_sensors;
    369  1.3.2.2  yamt 				sc->sc_y_factor = pd->y_factor;
    370  1.3.2.2  yamt 				sc->sc_y_sensors = pd->y_sensors;
    371  1.3.2.2  yamt 				break;
    372  1.3.2.2  yamt 			}
    373  1.3.2.2  yamt 		}
    374  1.3.2.2  yamt 	}
    375  1.3.2.2  yamt 	KASSERT(0 <= sc->sc_x_sensors && sc->sc_x_sensors <= PBMS_X_SENSORS);
    376  1.3.2.2  yamt 	KASSERT(0 <= sc->sc_y_sensors && sc->sc_y_sensors <= PBMS_Y_SENSORS);
    377  1.3.2.2  yamt 
    378  1.3.2.2  yamt 	sc->sc_status = 0;
    379  1.3.2.2  yamt 
    380  1.3.2.2  yamt 	a.accessops = &pbms_accessops;
    381  1.3.2.2  yamt 	a.accesscookie = sc;
    382  1.3.2.2  yamt 
    383  1.3.2.2  yamt 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
    384  1.3.2.2  yamt 
    385  1.3.2.2  yamt 	USB_ATTACH_SUCCESS_RETURN;
    386  1.3.2.2  yamt }
    387  1.3.2.2  yamt 
    388  1.3.2.2  yamt 
    389  1.3.2.2  yamt /* Detach the device. */
    390  1.3.2.2  yamt 
    391  1.3.2.2  yamt int
    392  1.3.2.2  yamt pbms_detach(struct device *self, int flags)
    393  1.3.2.2  yamt {
    394  1.3.2.2  yamt 	struct pbms_softc *sc = (struct pbms_softc *)self;
    395  1.3.2.2  yamt 	int ret;
    396  1.3.2.2  yamt 
    397  1.3.2.2  yamt 	/* The wsmouse driver does all the work. */
    398  1.3.2.2  yamt 	ret = 0;
    399  1.3.2.2  yamt 	if (sc->sc_wsmousedev != NULL)
    400  1.3.2.2  yamt 		ret = config_detach(sc->sc_wsmousedev, flags);
    401  1.3.2.2  yamt 
    402  1.3.2.2  yamt 	return ret;
    403  1.3.2.2  yamt }
    404  1.3.2.2  yamt 
    405  1.3.2.2  yamt 
    406  1.3.2.2  yamt /* Activate the device. */
    407  1.3.2.2  yamt 
    408  1.3.2.2  yamt int
    409  1.3.2.2  yamt pbms_activate(device_ptr_t self, enum devact act)
    410  1.3.2.2  yamt {
    411  1.3.2.2  yamt 	struct pbms_softc *sc = (struct pbms_softc *)self;
    412  1.3.2.2  yamt 	int ret;
    413  1.3.2.2  yamt 
    414  1.3.2.2  yamt 	if (act == DVACT_DEACTIVATE) {
    415  1.3.2.2  yamt 		ret = 0;
    416  1.3.2.2  yamt 		if (sc->sc_wsmousedev != NULL)
    417  1.3.2.2  yamt 			ret = config_deactivate(sc->sc_wsmousedev);
    418  1.3.2.2  yamt 		sc->sc_status |= PBMS_DYING;
    419  1.3.2.2  yamt 		return ret;
    420  1.3.2.2  yamt 	}
    421  1.3.2.2  yamt 	return EOPNOTSUPP;
    422  1.3.2.2  yamt }
    423  1.3.2.2  yamt 
    424  1.3.2.2  yamt 
    425  1.3.2.2  yamt /* Enable the device. */
    426  1.3.2.2  yamt 
    427  1.3.2.2  yamt static int
    428  1.3.2.2  yamt pbms_enable(void *v)
    429  1.3.2.2  yamt {
    430  1.3.2.2  yamt 	struct pbms_softc *sc = v;
    431  1.3.2.2  yamt 
    432  1.3.2.2  yamt 	/* Check that we are not detaching or already enabled. */
    433  1.3.2.2  yamt 	if (sc->sc_status & PBMS_DYING)
    434  1.3.2.2  yamt 		return EIO;
    435  1.3.2.2  yamt 	if (sc->sc_status & PBMS_ENABLED)
    436  1.3.2.2  yamt 		return EBUSY;
    437  1.3.2.2  yamt 
    438  1.3.2.2  yamt 	sc->sc_status |= PBMS_ENABLED;
    439  1.3.2.2  yamt 	sc->sc_status &= ~PBMS_VALID;
    440  1.3.2.2  yamt 	sc->sc_buttons = 0;
    441  1.3.2.2  yamt 	memset(sc->sc_sample, 0, sizeof(sc->sc_sample));
    442  1.3.2.2  yamt 
    443  1.3.2.2  yamt 	return uhidev_open(&sc->sc_hdev);
    444  1.3.2.2  yamt }
    445  1.3.2.2  yamt 
    446  1.3.2.2  yamt 
    447  1.3.2.2  yamt /* Disable the device. */
    448  1.3.2.2  yamt 
    449  1.3.2.2  yamt static void
    450  1.3.2.2  yamt pbms_disable(void *v)
    451  1.3.2.2  yamt {
    452  1.3.2.2  yamt 	struct pbms_softc *sc = v;
    453  1.3.2.2  yamt 
    454  1.3.2.2  yamt 	if (!(sc->sc_status & PBMS_ENABLED))
    455  1.3.2.2  yamt 		return;
    456  1.3.2.2  yamt 
    457  1.3.2.2  yamt 	sc->sc_status &= ~PBMS_ENABLED;
    458  1.3.2.2  yamt 	uhidev_close(&sc->sc_hdev);
    459  1.3.2.2  yamt }
    460  1.3.2.2  yamt 
    461  1.3.2.2  yamt 
    462  1.3.2.2  yamt /* XXX ioctl not implemented. */
    463  1.3.2.2  yamt 
    464  1.3.2.2  yamt static int
    465  1.3.2.2  yamt pbms_ioctl(void *v, unsigned long cmd, caddr_t data, int flag, struct lwp *p)
    466  1.3.2.2  yamt {
    467  1.3.2.2  yamt 	return EPASSTHROUGH;
    468  1.3.2.2  yamt }
    469  1.3.2.2  yamt 
    470  1.3.2.2  yamt 
    471  1.3.2.2  yamt /*
    472  1.3.2.2  yamt  * Interrupts & pointer movement.
    473  1.3.2.2  yamt  */
    474  1.3.2.2  yamt 
    475  1.3.2.2  yamt 
    476  1.3.2.2  yamt /* Handle interrupts. */
    477  1.3.2.2  yamt 
    478  1.3.2.2  yamt void
    479  1.3.2.2  yamt pbms_intr(struct uhidev *addr, void *ibuf, unsigned int len)
    480  1.3.2.2  yamt {
    481  1.3.2.2  yamt 	struct pbms_softc *sc = (struct pbms_softc *)addr;
    482  1.3.2.2  yamt 	signed char *data;
    483  1.3.2.2  yamt 	int dx, dy, dz, i, s;
    484  1.3.2.2  yamt 	uint32_t buttons;
    485  1.3.2.2  yamt 
    486  1.3.2.2  yamt 	/* Ignore incomplete data packets. */
    487  1.3.2.2  yamt 	if (len != PBMS_DATA_LEN)
    488  1.3.2.2  yamt 		return;
    489  1.3.2.2  yamt 	data = ibuf;
    490  1.3.2.2  yamt 
    491  1.3.2.2  yamt 	/* The last byte is 1 if the button is pressed and 0 otherwise. */
    492  1.3.2.2  yamt 	buttons = !!data[PBMS_DATA_LEN - 1];
    493  1.3.2.2  yamt 
    494  1.3.2.2  yamt 	/* Everything below assumes that the sample is reordered. */
    495  1.3.2.2  yamt 	reorder_sample(sc->sc_sample, data);
    496  1.3.2.2  yamt 
    497  1.3.2.2  yamt 	/* Is this the first sample? */
    498  1.3.2.2  yamt 	if (!(sc->sc_status & PBMS_VALID)) {
    499  1.3.2.2  yamt 		sc->sc_status |= PBMS_VALID;
    500  1.3.2.2  yamt 		sc->sc_x = sc->sc_y = -1;
    501  1.3.2.2  yamt 		sc->sc_x_raw = sc->sc_y_raw = -1;
    502  1.3.2.2  yamt 		memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
    503  1.3.2.2  yamt 		memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
    504  1.3.2.2  yamt 		return;
    505  1.3.2.2  yamt 	}
    506  1.3.2.2  yamt 	/* Accumulate the sensor change while keeping it nonnegative. */
    507  1.3.2.2  yamt 	for (i = 0; i < PBMS_SENSORS; i++) {
    508  1.3.2.2  yamt 		sc->sc_acc[i] += sc->sc_sample[i] - sc->sc_prev[i];
    509  1.3.2.2  yamt 		if (sc->sc_acc[i] < 0)
    510  1.3.2.2  yamt 			sc->sc_acc[i] = 0;
    511  1.3.2.2  yamt 	}
    512  1.3.2.2  yamt 	memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
    513  1.3.2.2  yamt 
    514  1.3.2.2  yamt 	/* Compute change. */
    515  1.3.2.2  yamt 	dx = dy = dz = 0;
    516  1.3.2.2  yamt 	if (!compute_delta(sc, &dx, &dy, &dz, &buttons))
    517  1.3.2.2  yamt 		return;
    518  1.3.2.2  yamt 
    519  1.3.2.2  yamt 	/* Report to wsmouse. */
    520  1.3.2.2  yamt 	if ((dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) &&
    521  1.3.2.2  yamt 	    sc->sc_wsmousedev != NULL) {
    522  1.3.2.2  yamt 		s = spltty();
    523  1.3.2.2  yamt 		wsmouse_input(sc->sc_wsmousedev, buttons, dx, -dy, dz,
    524  1.3.2.2  yamt 		    WSMOUSE_INPUT_DELTA);
    525  1.3.2.2  yamt 		splx(s);
    526  1.3.2.2  yamt 	}
    527  1.3.2.2  yamt 	sc->sc_buttons = buttons;
    528  1.3.2.2  yamt }
    529  1.3.2.2  yamt 
    530  1.3.2.2  yamt 
    531  1.3.2.2  yamt /*
    532  1.3.2.2  yamt  * Reorder the sensor values so that all the X-sensors are before the
    533  1.3.2.2  yamt  * Y-sensors in the natural order. Note that this might have to be
    534  1.3.2.2  yamt  * rewritten if PBMS_X_SENSORS or PBMS_Y_SENSORS change.
    535  1.3.2.2  yamt  */
    536  1.3.2.2  yamt 
    537  1.3.2.2  yamt static void
    538  1.3.2.2  yamt reorder_sample(signed char *to, signed char *from)
    539  1.3.2.2  yamt {
    540  1.3.2.2  yamt 	int i;
    541  1.3.2.2  yamt 
    542  1.3.2.2  yamt 	for (i = 0; i < 8; i++) {
    543  1.3.2.2  yamt 		/* X-sensors. */
    544  1.3.2.2  yamt 		to[i] = from[5 * i + 2];
    545  1.3.2.2  yamt 		to[i + 8] = from[5 * i + 4];
    546  1.3.2.2  yamt 		to[i + 16] = from[5 * i + 42];
    547  1.3.2.2  yamt #if 0
    548  1.3.2.2  yamt 		/*
    549  1.3.2.2  yamt 		 * XXX This seems to introduce random ventical jumps, so
    550  1.3.2.2  yamt 		 * we ignore these sensors until we figure out their meaning.
    551  1.3.2.2  yamt 		 */
    552  1.3.2.2  yamt 		if (i < 2)
    553  1.3.2.2  yamt 			to[i + 24] = from[5 * i + 44];
    554  1.3.2.2  yamt #endif /* 0 */
    555  1.3.2.2  yamt 		/* Y-sensors. */
    556  1.3.2.2  yamt 		to[i + 26] = from[5 * i + 1];
    557  1.3.2.2  yamt 		to[i + 34] = from[5 * i + 3];
    558  1.3.2.2  yamt 	}
    559  1.3.2.2  yamt }
    560  1.3.2.2  yamt 
    561  1.3.2.2  yamt 
    562  1.3.2.2  yamt /*
    563  1.3.2.2  yamt  * Compute the change in x, y and z direction, update the button state
    564  1.3.2.2  yamt  * (to simulate more than one button, scrolling etc.), and update the
    565  1.3.2.2  yamt  * history. Note that dx, dy, dz and buttons are modified only if
    566  1.3.2.2  yamt  * corresponding pressure is detected and should thus be initialised
    567  1.3.2.2  yamt  * before the call.  Return 0 on error.
    568  1.3.2.2  yamt  */
    569  1.3.2.2  yamt 
    570  1.3.2.2  yamt /* XXX Could we report something useful in dz? */
    571  1.3.2.2  yamt 
    572  1.3.2.2  yamt static int
    573  1.3.2.2  yamt compute_delta(struct pbms_softc *sc, int *dx, int *dy, int *dz,
    574  1.3.2.2  yamt 	      uint32_t * buttons)
    575  1.3.2.2  yamt {
    576  1.3.2.2  yamt 	int x_det, y_det, x_raw, y_raw, x_fingers, y_fingers, fingers, x, y;
    577  1.3.2.2  yamt 
    578  1.3.2.2  yamt 	x_det = detect_pos(sc->sc_acc, sc->sc_x_sensors, sc->sc_theshold,
    579  1.3.2.2  yamt 			   sc->sc_x_factor, &x_raw, &x_fingers);
    580  1.3.2.2  yamt 	y_det = detect_pos(sc->sc_acc + PBMS_X_SENSORS, sc->sc_y_sensors,
    581  1.3.2.2  yamt 			   sc->sc_theshold, sc->sc_y_factor,
    582  1.3.2.2  yamt 			   &y_raw, &y_fingers);
    583  1.3.2.2  yamt 	fingers = max(x_fingers, y_fingers);
    584  1.3.2.2  yamt 
    585  1.3.2.2  yamt 	/* Check the number of fingers and if we have detected a position. */
    586  1.3.2.2  yamt 	if (fingers > 1) {
    587  1.3.2.2  yamt 		/* More than one finger detected, resetting. */
    588  1.3.2.2  yamt 		memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
    589  1.3.2.2  yamt 		sc->sc_x_raw = sc->sc_y_raw = sc->sc_x = sc->sc_y = -1;
    590  1.3.2.2  yamt 		return 0;
    591  1.3.2.2  yamt 	} else if (x_det == 0 && y_det == 0) {
    592  1.3.2.2  yamt 		/* No position detected, resetting. */
    593  1.3.2.2  yamt 		memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
    594  1.3.2.2  yamt 		sc->sc_x_raw = sc->sc_y_raw = sc->sc_x = sc->sc_y = -1;
    595  1.3.2.2  yamt 	} else if (x_det > 0 && y_det > 0) {
    596  1.3.2.2  yamt 		/* Smooth position. */
    597  1.3.2.2  yamt 		if (sc->sc_x_raw >= 0) {
    598  1.3.2.2  yamt 			sc->sc_x_raw = (3 * sc->sc_x_raw + x_raw) / 4;
    599  1.3.2.2  yamt 			sc->sc_y_raw = (3 * sc->sc_y_raw + y_raw) / 4;
    600  1.3.2.2  yamt 			/*
    601  1.3.2.2  yamt 			 * Compute virtual position and change if we already
    602  1.3.2.2  yamt 			 * have a decent position.
    603  1.3.2.2  yamt 			 */
    604  1.3.2.2  yamt 			if (sc->sc_x >= 0) {
    605  1.3.2.2  yamt 				x = smooth_pos(sc->sc_x, sc->sc_x_raw,
    606  1.3.2.2  yamt 					       sc->sc_noise);
    607  1.3.2.2  yamt 				y = smooth_pos(sc->sc_y, sc->sc_y_raw,
    608  1.3.2.2  yamt 					       sc->sc_noise);
    609  1.3.2.2  yamt 				*dx = x - sc->sc_x;
    610  1.3.2.2  yamt 				*dy = y - sc->sc_y;
    611  1.3.2.2  yamt 				sc->sc_x = x;
    612  1.3.2.2  yamt 				sc->sc_y = y;
    613  1.3.2.2  yamt 			} else {
    614  1.3.2.2  yamt 				/* Initialise virtual position. */
    615  1.3.2.2  yamt 				sc->sc_x = sc->sc_x_raw;
    616  1.3.2.2  yamt 				sc->sc_y = sc->sc_y_raw;
    617  1.3.2.2  yamt 			}
    618  1.3.2.2  yamt 		} else {
    619  1.3.2.2  yamt 			/* Initialise raw position. */
    620  1.3.2.2  yamt 			sc->sc_x_raw = x_raw;
    621  1.3.2.2  yamt 			sc->sc_y_raw = y_raw;
    622  1.3.2.2  yamt 		}
    623  1.3.2.2  yamt 	}
    624  1.3.2.2  yamt 	return 1;
    625  1.3.2.2  yamt }
    626  1.3.2.2  yamt 
    627  1.3.2.2  yamt 
    628  1.3.2.2  yamt /*
    629  1.3.2.2  yamt  * Compute the new smoothed position from the previous smoothed position
    630  1.3.2.2  yamt  * and the raw position.
    631  1.3.2.2  yamt  */
    632  1.3.2.2  yamt 
    633  1.3.2.2  yamt static int
    634  1.3.2.2  yamt smooth_pos(int pos_old, int pos_raw, int noise)
    635  1.3.2.2  yamt {
    636  1.3.2.2  yamt 	int ad, delta;
    637  1.3.2.2  yamt 
    638  1.3.2.2  yamt 	delta = pos_raw - pos_old;
    639  1.3.2.2  yamt 	ad = abs(delta);
    640  1.3.2.2  yamt 
    641  1.3.2.2  yamt 	/* Too small changes are ignored. */
    642  1.3.2.2  yamt 	if (ad < noise / 2)
    643  1.3.2.2  yamt 		delta = 0;
    644  1.3.2.2  yamt 	/* A bit larger changes are smoothed. */
    645  1.3.2.2  yamt 	else if (ad < noise)
    646  1.3.2.2  yamt 		delta /= 4;
    647  1.3.2.2  yamt 	else if (ad < 2 * noise)
    648  1.3.2.2  yamt 		delta /= 2;
    649  1.3.2.2  yamt 
    650  1.3.2.2  yamt 	return pos_old + delta;
    651  1.3.2.2  yamt }
    652  1.3.2.2  yamt 
    653  1.3.2.2  yamt 
    654  1.3.2.2  yamt /*
    655  1.3.2.2  yamt  * Detect the position of the finger.  Returns the total pressure.
    656  1.3.2.2  yamt  * The position is returned in pos_ret and the number of fingers
    657  1.3.2.2  yamt  * is returned in fingers_ret.  The position returned in pos_ret
    658  1.3.2.2  yamt  * is in [0, (n_sensors - 1) * factor - 1].
    659  1.3.2.2  yamt  */
    660  1.3.2.2  yamt 
    661  1.3.2.2  yamt static int
    662  1.3.2.2  yamt detect_pos(int *sensors, int n_sensors, int threshold, int fact,
    663  1.3.2.2  yamt 	   int *pos_ret, int *fingers_ret)
    664  1.3.2.2  yamt {
    665  1.3.2.2  yamt 	int i, w, s;
    666  1.3.2.2  yamt 
    667  1.3.2.2  yamt 	/*
    668  1.3.2.2  yamt 	 * Compute the number of fingers, total pressure, and weighted
    669  1.3.2.2  yamt 	 * position of the fingers.
    670  1.3.2.2  yamt 	 */
    671  1.3.2.2  yamt 	*fingers_ret = 0;
    672  1.3.2.2  yamt 	w = s = 0;
    673  1.3.2.2  yamt 	for (i = 0; i < n_sensors; i++) {
    674  1.3.2.2  yamt 		if (sensors[i] >= threshold) {
    675  1.3.2.2  yamt 			if (i == 0 || sensors[i - 1] < threshold)
    676  1.3.2.2  yamt 				*fingers_ret += 1;
    677  1.3.2.2  yamt 			s += sensors[i];
    678  1.3.2.2  yamt 			w += sensors[i] * i;
    679  1.3.2.2  yamt 		}
    680  1.3.2.2  yamt 	}
    681  1.3.2.2  yamt 
    682  1.3.2.2  yamt 	if (s > 0)
    683  1.3.2.2  yamt 		*pos_ret = w * fact / s;
    684  1.3.2.2  yamt 
    685  1.3.2.2  yamt 	return s;
    686  1.3.2.2  yamt }
    687