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