xref: /netbsd-src/sys/arch/macppc/dev/pbms.c (revision f82ea25022a5fb99fe020b818aaff1f5b2a4c2b8)
1*f82ea250Sriastradh /* $Id: pbms.c,v 1.20 2022/03/28 12:43:12 riastradh Exp $ */
28d7d99b5Schristos 
38d7d99b5Schristos /*
48d7d99b5Schristos  * Copyright (c) 2005, Johan Wall�n
58d7d99b5Schristos  * All rights reserved.
68d7d99b5Schristos  *
78d7d99b5Schristos  * Redistribution and use in source and binary forms, with or without
88d7d99b5Schristos  * modification, are permitted provided that the following conditions are
98d7d99b5Schristos  * met:
108d7d99b5Schristos  *
118d7d99b5Schristos  *   1. Redistributions of source code must retain the above copyright
128d7d99b5Schristos  *      notice, this list of conditions and the following disclaimer.
138d7d99b5Schristos  *
148d7d99b5Schristos  *   2. Redistributions in binary form must reproduce the above
158d7d99b5Schristos  *      copyright notice, this list of conditions and the following
168d7d99b5Schristos  *      disclaimer in the documentation and/or other materials provided
178d7d99b5Schristos  *      with the distribution.
188d7d99b5Schristos  *
198d7d99b5Schristos  *   3. The name of the copyright holder may not be used to endorse or
208d7d99b5Schristos  *      promote products derived from this software without specific
218d7d99b5Schristos  *      prior written permission.
228d7d99b5Schristos  *
238d7d99b5Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
248d7d99b5Schristos  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
258d7d99b5Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
268d7d99b5Schristos  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE
278d7d99b5Schristos  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
288d7d99b5Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
298d7d99b5Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
308d7d99b5Schristos  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
318d7d99b5Schristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
328d7d99b5Schristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
338d7d99b5Schristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
348d7d99b5Schristos  */
358d7d99b5Schristos 
368d7d99b5Schristos /*
378d7d99b5Schristos  * The pbms driver provides support for the trackpad on new (post
388d7d99b5Schristos  * February 2005) Apple PowerBooks (and iBooks?) that are not standard
398d7d99b5Schristos  * USB HID mice.
408d7d99b5Schristos  */
418d7d99b5Schristos 
428d7d99b5Schristos /*
438d7d99b5Schristos  * The protocol (that is, the interpretation of the data generated by
448d7d99b5Schristos  * the trackpad) is taken from the Linux appletouch driver version
458d7d99b5Schristos  * 0.08 by Johannes Berg, Stelian Pop and Frank Arnold.  The method
468d7d99b5Schristos  * used to detect fingers on the trackpad is also taken from that
478d7d99b5Schristos  * driver.
488d7d99b5Schristos  */
498d7d99b5Schristos 
508d7d99b5Schristos /*
518d7d99b5Schristos  * To add support for other devices using the same protocol, add an
528d7d99b5Schristos  * entry to the pbms_devices table below.  See the comments for
538d7d99b5Schristos  * pbms_devices and struct pbms_devs.
548d7d99b5Schristos  */
558d7d99b5Schristos 
568d7d99b5Schristos /*
578d7d99b5Schristos  * PROTOCOL:
588d7d99b5Schristos  *
598d7d99b5Schristos  * The driver transfers continuously 81 byte events.  The last byte is
608d7d99b5Schristos  * 1 if the button is pressed, and is 0 otherwise. Of the remaining
618d7d99b5Schristos  * bytes, 26 + 16 = 42 are sensors detecting pressure in the X or
628d7d99b5Schristos  * horizontal, and Y or vertical directions, respectively.  On 12 and
638d7d99b5Schristos  * 15 inch PowerBooks, only the 16 first sensors in the X-direction
648d7d99b5Schristos  * are used. In the X-direction, the sensors correspond to byte
658d7d99b5Schristos  * positions
668d7d99b5Schristos  *
678d7d99b5Schristos  *   2, 7, 12, 17, 22, 27, 32, 37, 4, 9, 14, 19, 24, 29, 34, 39, 42,
688d7d99b5Schristos  *   47, 52, 57, 62, 67, 72, 77, 44 and 49;
698d7d99b5Schristos  *
70155d37a0Sphx  * In the Y direction, the sensors correspond to byte positions
718d7d99b5Schristos  *
728d7d99b5Schristos  *   1, 6, 11, 16, 21, 26, 31, 36, 3, 8, 13, 18, 23, 28, 33 and 38.
738d7d99b5Schristos  *
74155d37a0Sphx  * On 12 inch iBooks only the 9 first sensors in Y-direction are used.
758d7d99b5Schristos  * The change in the sensor values over time is more interesting than
768d7d99b5Schristos  * their absolute values: if the pressure increases, we know that the
778d7d99b5Schristos  * finger has just moved there.
788d7d99b5Schristos  *
798d7d99b5Schristos  * We keep track of the previous sample (of sensor values in the X and
808d7d99b5Schristos  * Y directions) and the accumulated change for each sensor.  When we
818d7d99b5Schristos  * receive a new sample, we add the difference of the new sensor value
828d7d99b5Schristos  * and the old value to the accumulated change.  If the accumulator
838d7d99b5Schristos  * becomes negative, we set it to zero.  The effect is that the
848d7d99b5Schristos  * accumulator is large for sensors whose pressure has recently
858d7d99b5Schristos  * increased.  If there is little change in pressure (or if the
868d7d99b5Schristos  * pressure decreases), the accumulator drifts back to zero.
878d7d99b5Schristos  *
888d7d99b5Schristos  * Since there is some fluctuations, we ignore accumulator values
898d7d99b5Schristos  * below a threshold.  The raw finger position is computed as a
908d7d99b5Schristos  * weighted average of the other sensors (the weights are the
918d7d99b5Schristos  * accumulated changes).
928d7d99b5Schristos  *
938d7d99b5Schristos  * For smoothing, we keep track of the previous raw finger position,
948d7d99b5Schristos  * and the virtual position reported to wsmouse.  The new raw position
958d7d99b5Schristos  * is computed as a weighted average of the old raw position and the
968d7d99b5Schristos  * computed raw position.  Since this still generates some noise, we
978d7d99b5Schristos  * compute a new virtual position as a weighted average of the previous
988d7d99b5Schristos  * virtual position and the new raw position.  The weights are
998d7d99b5Schristos  * controlled by the raw change and a noise parameter.  The position
1008d7d99b5Schristos  * is reported as a relative position.
1018d7d99b5Schristos  */
1028d7d99b5Schristos 
1038d7d99b5Schristos /*
1048d7d99b5Schristos  * TODO:
1058d7d99b5Schristos  *
1068d7d99b5Schristos  * Add support for other drivers of the same type.
1078d7d99b5Schristos  *
1088d7d99b5Schristos  * Add support for tapping and two-finger scrolling?  The
1098d7d99b5Schristos  * implementation already detects two fingers, so this should be
1108d7d99b5Schristos  * relatively easy.
1118d7d99b5Schristos  *
1128d7d99b5Schristos  * Implement some of the mouse ioctls?
1138d7d99b5Schristos  *
1148d7d99b5Schristos  * Take care of the XXXs.
1158d7d99b5Schristos  *
1168d7d99b5Schristos  */
1178d7d99b5Schristos 
1188d7d99b5Schristos #include <sys/cdefs.h>
1198d7d99b5Schristos 
1208d7d99b5Schristos #include <sys/param.h>
1218d7d99b5Schristos #include <sys/device.h>
1228d7d99b5Schristos #include <sys/errno.h>
1238d7d99b5Schristos 
1248d7d99b5Schristos #include <sys/ioctl.h>
1258d7d99b5Schristos #include <sys/systm.h>
1268d7d99b5Schristos #include <sys/tty.h>
1278d7d99b5Schristos 
1288d7d99b5Schristos #include <dev/usb/usb.h>
1298d7d99b5Schristos #include <dev/usb/usbdi.h>
1308d7d99b5Schristos #include <dev/usb/usbdevs.h>
1318d7d99b5Schristos #include <dev/usb/uhidev.h>
132c1d8007eSmaya #include <dev/hid/hid.h>
1338d7d99b5Schristos 
1348d7d99b5Schristos #include <dev/wscons/wsconsio.h>
1358d7d99b5Schristos #include <dev/wscons/wsmousevar.h>
1368d7d99b5Schristos 
1378d7d99b5Schristos /*
1388d7d99b5Schristos  * Magic numbers.
1398d7d99b5Schristos  */
1408d7d99b5Schristos 
1418d7d99b5Schristos 
1427991f5a7Sandvar /* The amount of data transferred by the USB device. */
1438d7d99b5Schristos #define PBMS_DATA_LEN 81
1448d7d99b5Schristos 
1458d7d99b5Schristos /* The maximum number of sensors. */
1468d7d99b5Schristos #define PBMS_X_SENSORS 26
1478d7d99b5Schristos #define PBMS_Y_SENSORS 16
1488d7d99b5Schristos #define PBMS_SENSORS (PBMS_X_SENSORS + PBMS_Y_SENSORS)
1498d7d99b5Schristos 
1508d7d99b5Schristos /*
1518d7d99b5Schristos  * Parameters for supported devices.  For generality, these parameters
1528d7d99b5Schristos  * can be different for each device.  The meanings of the parameters
1538d7d99b5Schristos  * are as follows.
1548d7d99b5Schristos  *
1558d7d99b5Schristos  * desc:      A printable description used for dmesg output.
1568d7d99b5Schristos  *
1578d7d99b5Schristos  * noise:     Amount of noise in the computed position. This controls
1588d7d99b5Schristos  *            how large a change must be to get reported, and how
1598d7d99b5Schristos  *            large enough changes are smoothed.  A good value can
1608d7d99b5Schristos  *            probably only be found experimentally, but something around
1618d7d99b5Schristos  *            16 seems suitable.
1628d7d99b5Schristos  *
1638d7d99b5Schristos  * product:   The product ID of the trackpad.
1648d7d99b5Schristos  *
1658d7d99b5Schristos  *
1668d7d99b5Schristos  * threshold: Accumulated changes less than this are ignored.  A good
1678d7d99b5Schristos  *            value could be determined experimentally, but 5 is a
1688d7d99b5Schristos  *            reasonable guess.
1698d7d99b5Schristos  *
1708d7d99b5Schristos  * vendor:    The vendor ID.  Currently USB_VENDOR_APPLE for all devices.
1718d7d99b5Schristos  *
1728d7d99b5Schristos  * x_factor:  Factor used in computations with X-coordinates.  If the
1738d7d99b5Schristos  *            x-resolution of the display is x, this should be
1748d7d99b5Schristos  *            (x + 1) / (x_sensors - 1).  Other values work fine, but
1758d7d99b5Schristos  *            then the aspect ratio is not necessarily kept.
1768d7d99b5Schristos  *
1778d7d99b5Schristos  * x_sensors: The number of sensors in the X-direction.
1788d7d99b5Schristos  *
1798d7d99b5Schristos  * y_factor:  As x_factors, but for Y-coordinates.
1808d7d99b5Schristos  *
1818d7d99b5Schristos  * y_sensors: The number of sensors in the Y-direction.
1828d7d99b5Schristos  */
1838d7d99b5Schristos 
1848d7d99b5Schristos struct pbms_dev {
1858d7d99b5Schristos 	const char *descr; /* Description of the driver (for dmesg). */
1868d7d99b5Schristos 	int noise;	   /* Amount of noise in the computed position. */
1878d7d99b5Schristos 	int threshold;	   /* Changes less than this are ignored. */
1888d7d99b5Schristos 	int x_factor;	   /* Factor used in computation with X-coordinates. */
1898d7d99b5Schristos 	int x_sensors;	   /* The number of X-sensors. */
1908d7d99b5Schristos 	int y_factor;	   /* Factor used in computation with Y-coordinates. */
1918d7d99b5Schristos 	int y_sensors;	   /* The number of Y-sensors. */
1928d7d99b5Schristos 	uint16_t product;  /* Product ID. */
1938d7d99b5Schristos 	uint16_t vendor;   /* The vendor ID. */
1948d7d99b5Schristos };
1958d7d99b5Schristos 
1968d7d99b5Schristos /* Devices supported by this driver. */
1978d7d99b5Schristos static struct pbms_dev pbms_devices[] =
1988d7d99b5Schristos {
1998d7d99b5Schristos #define POWERBOOK_TOUCHPAD(inches, prod, x_fact, x_sens, y_fact)	      \
2008d7d99b5Schristos        {								      \
2018d7d99b5Schristos 		.descr = #inches " inch PowerBook Trackpad",		      \
2028d7d99b5Schristos 		.vendor = USB_VENDOR_APPLE,				      \
2038d7d99b5Schristos 		.product = (prod),					      \
2048d7d99b5Schristos 		.noise = 16,						      \
2058d7d99b5Schristos 		.threshold = 5,						      \
2068d7d99b5Schristos 		.x_factor = (x_fact),					      \
2078d7d99b5Schristos 		.x_sensors = (x_sens),					      \
2088d7d99b5Schristos 		.y_factor = (y_fact),					      \
2098d7d99b5Schristos 		.y_sensors = 16						      \
2108d7d99b5Schristos        }
211155d37a0Sphx        /* 12 inch PowerBooks/iBooks */
2128d7d99b5Schristos        POWERBOOK_TOUCHPAD(12, 0x030a, 69, 16, 52), /* XXX Not tested. */
213155d37a0Sphx        POWERBOOK_TOUCHPAD(12, 0x030b, 73, 15, 96),
2148d7d99b5Schristos        /* 15 inch PowerBooks */
2158d7d99b5Schristos        POWERBOOK_TOUCHPAD(15, 0x020e, 85, 16, 57), /* XXX Not tested. */
2168d7d99b5Schristos        POWERBOOK_TOUCHPAD(15, 0x020f, 85, 16, 57),
217111a9327Saymeric        POWERBOOK_TOUCHPAD(15, 0x0215, 90, 15, 107),
2188d7d99b5Schristos        /* 17 inch PowerBooks */
2198d7d99b5Schristos        POWERBOOK_TOUCHPAD(17, 0x020d, 71, 26, 68)  /* XXX Not tested. */
2208d7d99b5Schristos #undef POWERBOOK_TOUCHPAD
2218d7d99b5Schristos };
2228d7d99b5Schristos 
2238d7d99b5Schristos /* The number of supported devices. */
2248d7d99b5Schristos #define PBMS_NUM_DEVICES (sizeof(pbms_devices) / sizeof(pbms_devices[0]))
2258d7d99b5Schristos 
2268d7d99b5Schristos 
2278d7d99b5Schristos /*
2288d7d99b5Schristos  * Types and prototypes.
2298d7d99b5Schristos  */
2308d7d99b5Schristos 
2318d7d99b5Schristos 
2328d7d99b5Schristos /* Device data. */
2338d7d99b5Schristos struct pbms_softc {
234cbab9cadSchs 	struct uhidev sc_hdev;	      /* USB parent */
235ccf07f81Saymeric 	int is_geyser2;
236155d37a0Sphx 	int sc_datalen;		      /* Size of a data packet */
237155d37a0Sphx 	int sc_bufusage;	      /* Number of bytes in sc_databuf */
2388d7d99b5Schristos 	int sc_acc[PBMS_SENSORS];     /* Accumulated sensor values. */
239ccf07f81Saymeric 	unsigned char sc_prev[PBMS_SENSORS];   /* Previous sample. */
240ccf07f81Saymeric 	unsigned char sc_sample[PBMS_SENSORS]; /* Current sample. */
241155d37a0Sphx 	uint8_t sc_databuf[PBMS_DATA_LEN];     /* Buffer for a data packet */
2427507fa68Sdyoung 	device_t sc_wsmousedev; /* WSMouse device. */
2438d7d99b5Schristos 	int sc_noise;		      /* Amount of noise. */
2448d7d99b5Schristos 	int sc_theshold;	      /* Threshold value. */
2458d7d99b5Schristos 	int sc_x;		      /* Virtual position in horizontal
2468d7d99b5Schristos 				       * direction (wsmouse position). */
2478d7d99b5Schristos 	int sc_x_factor;	      /* X-coordinate factor. */
2488d7d99b5Schristos 	int sc_x_raw;		      /* X-position of finger on trackpad. */
2498d7d99b5Schristos 	int sc_x_sensors;	      /* Number of X-sensors. */
2508d7d99b5Schristos 	int sc_y;		      /* Virtual position in vertical direction
2518d7d99b5Schristos 				       * (wsmouse position). */
2528d7d99b5Schristos 	int sc_y_factor;	      /* Y-coordinate factor. */
2538d7d99b5Schristos 	int sc_y_raw;		      /* Y-position of finger on trackpad. */
2548d7d99b5Schristos 	int sc_y_sensors;	      /* Number of Y-sensors. */
2558d7d99b5Schristos 	uint32_t sc_buttons;	      /* Button state. */
2568d7d99b5Schristos 	uint32_t sc_status;	      /* Status flags. */
2578d7d99b5Schristos #define PBMS_ENABLED 1		      /* Is the device enabled? */
2588d7d99b5Schristos #define PBMS_DYING 2		      /* Is the device dying? */
2598d7d99b5Schristos #define PBMS_VALID 4		      /* Is the previous sample valid? */
2608d7d99b5Schristos };
2618d7d99b5Schristos 
2628d7d99b5Schristos 
2638d7d99b5Schristos /* Static function prototypes. */
2648d7d99b5Schristos static void pbms_intr(struct uhidev *, void *, unsigned int);
2658d7d99b5Schristos static int pbms_enable(void *);
2668d7d99b5Schristos static void pbms_disable(void *);
26753524e44Schristos static int pbms_ioctl(void *, unsigned long, void *, int, struct lwp *);
268ccf07f81Saymeric static void reorder_sample(struct pbms_softc *, unsigned char *, unsigned char *);
2698d7d99b5Schristos static int compute_delta(struct pbms_softc *, int *, int *, int *, uint32_t *);
2708d7d99b5Schristos static int detect_pos(int *, int, int, int, int *, int *);
2718d7d99b5Schristos static int smooth_pos(int, int, int);
2728d7d99b5Schristos 
2738d7d99b5Schristos /* Access methods for wsmouse. */
2748d7d99b5Schristos const struct wsmouse_accessops pbms_accessops = {
2758d7d99b5Schristos 	pbms_enable,
2768d7d99b5Schristos 	pbms_ioctl,
2778d7d99b5Schristos 	pbms_disable,
2788d7d99b5Schristos };
2798d7d99b5Schristos 
2808d7d99b5Schristos /* This take cares also of the basic device registration. */
2817507fa68Sdyoung int pbms_match(device_t, cfdata_t, void *);
2827507fa68Sdyoung void pbms_attach(device_t, device_t, void *);
2837507fa68Sdyoung int pbms_detach(device_t, int);
2847507fa68Sdyoung void pbms_childdet(device_t, device_t);
2857507fa68Sdyoung int pbms_activate(device_t, enum devact);
2867507fa68Sdyoung extern struct cfdriver pbms_cd;
2877507fa68Sdyoung CFATTACH_DECL2_NEW(pbms, sizeof(struct pbms_softc), pbms_match, pbms_attach,
2887507fa68Sdyoung     pbms_detach, pbms_activate, NULL, pbms_childdet);
2898d7d99b5Schristos 
2908d7d99b5Schristos /*
2918d7d99b5Schristos  * Basic driver.
2928d7d99b5Schristos  */
2938d7d99b5Schristos 
2948d7d99b5Schristos 
2958d7d99b5Schristos /* Try to match the device at some uhidev. */
2968d7d99b5Schristos 
2978d7d99b5Schristos int
pbms_match(device_t parent,cfdata_t match,void * aux)2987507fa68Sdyoung pbms_match(device_t parent, cfdata_t match, void *aux)
2998d7d99b5Schristos {
3008d7d99b5Schristos 	struct uhidev_attach_arg *uha = aux;
3018d7d99b5Schristos 	usb_device_descriptor_t *udd;
3028d7d99b5Schristos 	int i;
3038d7d99b5Schristos 	uint16_t vendor, product;
3048d7d99b5Schristos 
3058d7d99b5Schristos 	/*
3068d7d99b5Schristos 	 * We just check if the vendor and product IDs have the magic numbers
3078d7d99b5Schristos 	 * we expect.
3088d7d99b5Schristos 	 */
3099724d4d2Sskrll 	if (uha->uiaa->uiaa_proto == UIPROTO_MOUSE &&
310*f82ea250Sriastradh 	    ((udd = usbd_get_device_descriptor(uha->uiaa->uiaa_device))
311*f82ea250Sriastradh 		!= NULL)) {
3128d7d99b5Schristos 		vendor = UGETW(udd->idVendor);
3138d7d99b5Schristos 		product = UGETW(udd->idProduct);
3148d7d99b5Schristos 		for (i = 0; i < PBMS_NUM_DEVICES; i++) {
3158d7d99b5Schristos 			if (vendor == pbms_devices[i].vendor &&
3168d7d99b5Schristos 			    product == pbms_devices[i].product)
3178d7d99b5Schristos 				return UMATCH_IFACECLASS;
3188d7d99b5Schristos 		}
3198d7d99b5Schristos 	}
3208d7d99b5Schristos 	return UMATCH_NONE;
3218d7d99b5Schristos }
3228d7d99b5Schristos 
3238d7d99b5Schristos 
3248d7d99b5Schristos /* Attach the device. */
3258d7d99b5Schristos 
3268d7d99b5Schristos void
pbms_attach(device_t parent,device_t self,void * aux)3277507fa68Sdyoung pbms_attach(device_t parent, device_t self, void *aux)
3288d7d99b5Schristos {
3298d7d99b5Schristos 	struct wsmousedev_attach_args a;
3308d7d99b5Schristos 	struct uhidev_attach_arg *uha = aux;
3318d7d99b5Schristos 	struct pbms_dev *pd;
332111a9327Saymeric 	struct pbms_softc *sc = device_private(self);
333*f82ea250Sriastradh 	struct usbd_device *udev;
3348d7d99b5Schristos 	usb_device_descriptor_t *udd;
3358d7d99b5Schristos 	int i;
3368d7d99b5Schristos 	uint16_t vendor, product;
3378d7d99b5Schristos 
3388d7d99b5Schristos 	sc->sc_hdev.sc_intr = pbms_intr;
3398d7d99b5Schristos 	sc->sc_hdev.sc_parent = uha->parent;
3408d7d99b5Schristos 	sc->sc_hdev.sc_report_id = uha->reportid;
3418d7d99b5Schristos 
342ccf07f81Saymeric 	sc->is_geyser2 = 0;
343ccf07f81Saymeric 	sc->sc_datalen = PBMS_DATA_LEN;
344ccf07f81Saymeric 
3458d7d99b5Schristos 	/* Fill in device-specific parameters. */
346*f82ea250Sriastradh 	udev = uha->uiaa->uiaa_udevice;
347*f82ea250Sriastradh 	if ((udd = usbd_get_device_descriptor(udev)) != NULL) {
3488d7d99b5Schristos 		product = UGETW(udd->idProduct);
3498d7d99b5Schristos 		vendor = UGETW(udd->idVendor);
3508d7d99b5Schristos 		for (i = 0; i < PBMS_NUM_DEVICES; i++) {
3518d7d99b5Schristos 			pd = &pbms_devices[i];
3528d7d99b5Schristos 			if (product == pd->product && vendor == pd->vendor) {
3538d7d99b5Schristos 				printf(": %s\n", pd->descr);
3548d7d99b5Schristos 				sc->sc_noise = pd->noise;
3558d7d99b5Schristos 				sc->sc_theshold = pd->threshold;
3568d7d99b5Schristos 				sc->sc_x_factor = pd->x_factor;
3578d7d99b5Schristos 				sc->sc_x_sensors = pd->x_sensors;
3588d7d99b5Schristos 				sc->sc_y_factor = pd->y_factor;
3598d7d99b5Schristos 				sc->sc_y_sensors = pd->y_sensors;
360111a9327Saymeric 				if (product == 0x0215) {
361ccf07f81Saymeric 					sc->is_geyser2 = 1;
362ccf07f81Saymeric 					sc->sc_datalen = 64;
363111a9327Saymeric 					sc->sc_y_sensors = 9;
364ccf07f81Saymeric 				}
365155d37a0Sphx 				else if (product == 0x030b)
366155d37a0Sphx 					sc->sc_y_sensors = 9;
3678d7d99b5Schristos 				break;
3688d7d99b5Schristos 			}
3698d7d99b5Schristos 		}
3708d7d99b5Schristos 	}
3718d7d99b5Schristos 	KASSERT(0 <= sc->sc_x_sensors && sc->sc_x_sensors <= PBMS_X_SENSORS);
3728d7d99b5Schristos 	KASSERT(0 <= sc->sc_y_sensors && sc->sc_y_sensors <= PBMS_Y_SENSORS);
3738d7d99b5Schristos 
3748d7d99b5Schristos 	sc->sc_status = 0;
3758d7d99b5Schristos 
3768d7d99b5Schristos 	a.accessops = &pbms_accessops;
3778d7d99b5Schristos 	a.accesscookie = sc;
3788d7d99b5Schristos 
379c7fb772bSthorpej 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
3808d7d99b5Schristos 
3816b66a6ecSpooka 	return;
3828d7d99b5Schristos }
3838d7d99b5Schristos 
3848d7d99b5Schristos 
3858d7d99b5Schristos /* Detach the device. */
3868d7d99b5Schristos 
3877507fa68Sdyoung void
pbms_childdet(device_t self,device_t child)3887507fa68Sdyoung pbms_childdet(device_t self, device_t child)
3898d7d99b5Schristos {
3907507fa68Sdyoung 	struct pbms_softc *sc = device_private(self);
3918d7d99b5Schristos 
3927507fa68Sdyoung 	if (sc->sc_wsmousedev == child)
3937507fa68Sdyoung 		sc->sc_wsmousedev = NULL;
3947507fa68Sdyoung }
3958d7d99b5Schristos 
3967507fa68Sdyoung int
pbms_detach(device_t self,int flags)3977507fa68Sdyoung pbms_detach(device_t self, int flags)
3987507fa68Sdyoung {
3997507fa68Sdyoung 	/* XXX This could not possibly be sufficient! */
4007507fa68Sdyoung 	return config_detach_children(self, flags);
4018d7d99b5Schristos }
4028d7d99b5Schristos 
4038d7d99b5Schristos 
4048d7d99b5Schristos /* Activate the device. */
4058d7d99b5Schristos 
4068d7d99b5Schristos int
pbms_activate(device_t self,enum devact act)4077507fa68Sdyoung pbms_activate(device_t self, enum devact act)
4088d7d99b5Schristos {
4097507fa68Sdyoung 	struct pbms_softc *sc = device_private(self);
4108d7d99b5Schristos 
4117507fa68Sdyoung 	if (act != DVACT_DEACTIVATE)
4128d7d99b5Schristos 		return EOPNOTSUPP;
4137507fa68Sdyoung 
4147507fa68Sdyoung 	sc->sc_status |= PBMS_DYING;
4157507fa68Sdyoung 	return 0;
4168d7d99b5Schristos }
4178d7d99b5Schristos 
4188d7d99b5Schristos 
4198d7d99b5Schristos /* Enable the device. */
4208d7d99b5Schristos 
4218d7d99b5Schristos static int
pbms_enable(void * v)4228d7d99b5Schristos pbms_enable(void *v)
4238d7d99b5Schristos {
4248d7d99b5Schristos 	struct pbms_softc *sc = v;
4258d7d99b5Schristos 
4268d7d99b5Schristos 	/* Check that we are not detaching or already enabled. */
4278d7d99b5Schristos 	if (sc->sc_status & PBMS_DYING)
4288d7d99b5Schristos 		return EIO;
4298d7d99b5Schristos 	if (sc->sc_status & PBMS_ENABLED)
4308d7d99b5Schristos 		return EBUSY;
4318d7d99b5Schristos 
4328d7d99b5Schristos 	sc->sc_status |= PBMS_ENABLED;
4338d7d99b5Schristos 	sc->sc_status &= ~PBMS_VALID;
434155d37a0Sphx 	sc->sc_bufusage = 0;
4358d7d99b5Schristos 	sc->sc_buttons = 0;
4368d7d99b5Schristos 	memset(sc->sc_sample, 0, sizeof(sc->sc_sample));
4378d7d99b5Schristos 
4388d7d99b5Schristos 	return uhidev_open(&sc->sc_hdev);
4398d7d99b5Schristos }
4408d7d99b5Schristos 
4418d7d99b5Schristos 
4428d7d99b5Schristos /* Disable the device. */
4438d7d99b5Schristos 
4448d7d99b5Schristos static void
pbms_disable(void * v)4458d7d99b5Schristos pbms_disable(void *v)
4468d7d99b5Schristos {
4478d7d99b5Schristos 	struct pbms_softc *sc = v;
4488d7d99b5Schristos 
4498d7d99b5Schristos 	if (!(sc->sc_status & PBMS_ENABLED))
4508d7d99b5Schristos 		return;
4518d7d99b5Schristos 
4528d7d99b5Schristos 	sc->sc_status &= ~PBMS_ENABLED;
4538d7d99b5Schristos 	uhidev_close(&sc->sc_hdev);
4548d7d99b5Schristos }
4558d7d99b5Schristos 
4568d7d99b5Schristos 
4578d7d99b5Schristos /* XXX ioctl not implemented. */
4588d7d99b5Schristos 
4598d7d99b5Schristos static int
pbms_ioctl(void * v,unsigned long cmd,void * data,int flag,struct lwp * p)46053524e44Schristos pbms_ioctl(void *v, unsigned long cmd, void *data, int flag, struct lwp *p)
4618d7d99b5Schristos {
4628d7d99b5Schristos 	return EPASSTHROUGH;
4638d7d99b5Schristos }
4648d7d99b5Schristos 
4658d7d99b5Schristos 
4668d7d99b5Schristos /*
4678d7d99b5Schristos  * Interrupts & pointer movement.
4688d7d99b5Schristos  */
4698d7d99b5Schristos 
4708d7d99b5Schristos 
4718d7d99b5Schristos /* Handle interrupts. */
4728d7d99b5Schristos 
4738d7d99b5Schristos void
pbms_intr(struct uhidev * addr,void * ibuf,unsigned int len)4748d7d99b5Schristos pbms_intr(struct uhidev *addr, void *ibuf, unsigned int len)
4758d7d99b5Schristos {
4768d7d99b5Schristos 	struct pbms_softc *sc = (struct pbms_softc *)addr;
477155d37a0Sphx 	uint8_t *data;
4788d7d99b5Schristos 	int dx, dy, dz, i, s;
4798d7d99b5Schristos 	uint32_t buttons;
4808d7d99b5Schristos 
481155d37a0Sphx 	/*
482155d37a0Sphx 	 * We may have to construct the full data packet over two or three
483155d37a0Sphx 	 * sequential interrupts, as the device only sends us chunks of
484155d37a0Sphx 	 * 32 or 64 bytes of data.
485155d37a0Sphx 	 * This also requires some synchronization, to make sure we place
486155d37a0Sphx 	 * the first protocol-byte at the first byte in the bufffer.
487155d37a0Sphx 	 */
488155d37a0Sphx 	if (sc->is_geyser2) {
489155d37a0Sphx 		/* XXX Need to check this. */
490155d37a0Sphx 	} else {
491155d37a0Sphx 		/* the last chunk is always 17 bytes */
492155d37a0Sphx 		if (len == 17 && sc->sc_bufusage + len != sc->sc_datalen) {
493155d37a0Sphx 			sc->sc_bufusage = 0;	/* discard bad packet */
4948d7d99b5Schristos 			return;
495155d37a0Sphx 		}
496155d37a0Sphx 	}
4978d7d99b5Schristos 
498155d37a0Sphx 	memcpy(sc->sc_databuf + sc->sc_bufusage, ibuf, len);
499155d37a0Sphx 	sc->sc_bufusage += len;
500155d37a0Sphx 	if (sc->sc_bufusage != sc->sc_datalen)
501155d37a0Sphx 		return;		/* wait until packet is complete */
502155d37a0Sphx 
503155d37a0Sphx 	/* process the now complete protocol and clear the buffer */
504155d37a0Sphx 	data = sc->sc_databuf;
505155d37a0Sphx 	sc->sc_bufusage = 0;
506ccf07f81Saymeric #if 0
507155d37a0Sphx 	for (i = 0; i < sc->sc_datalen; i++)
508155d37a0Sphx 		printf(" %02x", data[i]);
509155d37a0Sphx 	printf("\n");
510ccf07f81Saymeric #endif
511ccf07f81Saymeric 
5128d7d99b5Schristos 	/* The last byte is 1 if the button is pressed and 0 otherwise. */
513ccf07f81Saymeric 	buttons = !!data[sc->sc_datalen - 1];
5148d7d99b5Schristos 
5158d7d99b5Schristos 	/* Everything below assumes that the sample is reordered. */
516ccf07f81Saymeric 	reorder_sample(sc, sc->sc_sample, data);
5178d7d99b5Schristos 
5188d7d99b5Schristos 	/* Is this the first sample? */
5198d7d99b5Schristos 	if (!(sc->sc_status & PBMS_VALID)) {
5208d7d99b5Schristos 		sc->sc_status |= PBMS_VALID;
5218d7d99b5Schristos 		sc->sc_x = sc->sc_y = -1;
5228d7d99b5Schristos 		sc->sc_x_raw = sc->sc_y_raw = -1;
5238d7d99b5Schristos 		memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
5248d7d99b5Schristos 		memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
5258d7d99b5Schristos 		return;
5268d7d99b5Schristos 	}
5278d7d99b5Schristos 	/* Accumulate the sensor change while keeping it nonnegative. */
5288d7d99b5Schristos 	for (i = 0; i < PBMS_SENSORS; i++) {
529ccf07f81Saymeric 		sc->sc_acc[i] +=
530ccf07f81Saymeric 			(signed char) (sc->sc_sample[i] - sc->sc_prev[i]);
5318d7d99b5Schristos 		if (sc->sc_acc[i] < 0)
5328d7d99b5Schristos 			sc->sc_acc[i] = 0;
5338d7d99b5Schristos 	}
5348d7d99b5Schristos 	memcpy(sc->sc_prev, sc->sc_sample, sizeof(sc->sc_prev));
5358d7d99b5Schristos 
5368d7d99b5Schristos 	/* Compute change. */
5378d7d99b5Schristos 	dx = dy = dz = 0;
5388d7d99b5Schristos 	if (!compute_delta(sc, &dx, &dy, &dz, &buttons))
5398d7d99b5Schristos 		return;
5408d7d99b5Schristos 
5418d7d99b5Schristos 	/* Report to wsmouse. */
5428d7d99b5Schristos 	if ((dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) &&
5438d7d99b5Schristos 	    sc->sc_wsmousedev != NULL) {
5448d7d99b5Schristos 		s = spltty();
54557c0199dSplunky 		wsmouse_input(sc->sc_wsmousedev, buttons, dx, -dy, dz, 0,
5468d7d99b5Schristos 		    WSMOUSE_INPUT_DELTA);
5478d7d99b5Schristos 		splx(s);
5488d7d99b5Schristos 	}
5498d7d99b5Schristos 	sc->sc_buttons = buttons;
5508d7d99b5Schristos }
5518d7d99b5Schristos 
5528d7d99b5Schristos 
5538d7d99b5Schristos /*
5548d7d99b5Schristos  * Reorder the sensor values so that all the X-sensors are before the
5558d7d99b5Schristos  * Y-sensors in the natural order. Note that this might have to be
5568d7d99b5Schristos  * rewritten if PBMS_X_SENSORS or PBMS_Y_SENSORS change.
5578d7d99b5Schristos  */
5588d7d99b5Schristos 
5598d7d99b5Schristos static void
reorder_sample(struct pbms_softc * sc,unsigned char * to,unsigned char * from)560ccf07f81Saymeric reorder_sample(struct pbms_softc *sc, unsigned char *to, unsigned char *from)
5618d7d99b5Schristos {
5628d7d99b5Schristos 	int i;
5638d7d99b5Schristos 
564ccf07f81Saymeric 	if (sc->is_geyser2) {
565ccf07f81Saymeric 		int j;
566ccf07f81Saymeric 
567ccf07f81Saymeric 		memset(to, 0, PBMS_SENSORS);
568ccf07f81Saymeric 		for (i = 0, j = 19; i < 20; i += 2, j += 3) {
569ccf07f81Saymeric 			to[i] = from[j];
570ccf07f81Saymeric 			to[i + 1] = from[j + 1];
571ccf07f81Saymeric 		}
572ccf07f81Saymeric 		for (i = 0, j = 1; i < 9; i += 2, j += 3) {
573ccf07f81Saymeric 			to[PBMS_X_SENSORS + i] = from[j];
574ccf07f81Saymeric 			to[PBMS_X_SENSORS + i + 1] = from[j + 1];
575ccf07f81Saymeric 		}
576ccf07f81Saymeric 	} else {
5778d7d99b5Schristos 		for (i = 0; i < 8; i++) {
5788d7d99b5Schristos 			/* X-sensors. */
5798d7d99b5Schristos 			to[i] = from[5 * i + 2];
5808d7d99b5Schristos 			to[i + 8] = from[5 * i + 4];
5818d7d99b5Schristos 			to[i + 16] = from[5 * i + 42];
5828d7d99b5Schristos 	#if 0
5838d7d99b5Schristos 			/*
5848d7d99b5Schristos 			 * XXX This seems to introduce random ventical jumps, so
5858d7d99b5Schristos 			 * we ignore these sensors until we figure out their meaning.
5868d7d99b5Schristos 			 */
5878d7d99b5Schristos 			if (i < 2)
5888d7d99b5Schristos 				to[i + 24] = from[5 * i + 44];
5898d7d99b5Schristos 	#endif /* 0 */
5908d7d99b5Schristos 			/* Y-sensors. */
5918d7d99b5Schristos 			to[i + 26] = from[5 * i + 1];
5928d7d99b5Schristos 			to[i + 34] = from[5 * i + 3];
5938d7d99b5Schristos 		}
5948d7d99b5Schristos 	}
595ccf07f81Saymeric }
5968d7d99b5Schristos 
5978d7d99b5Schristos 
5988d7d99b5Schristos /*
5998d7d99b5Schristos  * Compute the change in x, y and z direction, update the button state
6008d7d99b5Schristos  * (to simulate more than one button, scrolling etc.), and update the
6018d7d99b5Schristos  * history. Note that dx, dy, dz and buttons are modified only if
6028d7d99b5Schristos  * corresponding pressure is detected and should thus be initialised
6038d7d99b5Schristos  * before the call.  Return 0 on error.
6048d7d99b5Schristos  */
6058d7d99b5Schristos 
6068d7d99b5Schristos /* XXX Could we report something useful in dz? */
6078d7d99b5Schristos 
6088d7d99b5Schristos static int
compute_delta(struct pbms_softc * sc,int * dx,int * dy,int * dz,uint32_t * buttons)6098d7d99b5Schristos compute_delta(struct pbms_softc *sc, int *dx, int *dy, int *dz,
6108d7d99b5Schristos 	      uint32_t * buttons)
6118d7d99b5Schristos {
6128d7d99b5Schristos 	int x_det, y_det, x_raw, y_raw, x_fingers, y_fingers, fingers, x, y;
6138d7d99b5Schristos 
6148d7d99b5Schristos 	x_det = detect_pos(sc->sc_acc, sc->sc_x_sensors, sc->sc_theshold,
6158d7d99b5Schristos 			   sc->sc_x_factor, &x_raw, &x_fingers);
6168d7d99b5Schristos 	y_det = detect_pos(sc->sc_acc + PBMS_X_SENSORS, sc->sc_y_sensors,
6178d7d99b5Schristos 			   sc->sc_theshold, sc->sc_y_factor,
6188d7d99b5Schristos 			   &y_raw, &y_fingers);
619d1579b2dSriastradh 	fingers = uimax(x_fingers, y_fingers);
6208d7d99b5Schristos 
6218d7d99b5Schristos 	/* Check the number of fingers and if we have detected a position. */
6228d7d99b5Schristos 	if (fingers > 1) {
6238d7d99b5Schristos 		/* More than one finger detected, resetting. */
6248d7d99b5Schristos 		memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
6258d7d99b5Schristos 		sc->sc_x_raw = sc->sc_y_raw = sc->sc_x = sc->sc_y = -1;
6268d7d99b5Schristos 		return 0;
6278d7d99b5Schristos 	} else if (x_det == 0 && y_det == 0) {
6288d7d99b5Schristos 		/* No position detected, resetting. */
6298d7d99b5Schristos 		memset(sc->sc_acc, 0, sizeof(sc->sc_acc));
6308d7d99b5Schristos 		sc->sc_x_raw = sc->sc_y_raw = sc->sc_x = sc->sc_y = -1;
6318d7d99b5Schristos 	} else if (x_det > 0 && y_det > 0) {
6328d7d99b5Schristos 		/* Smooth position. */
6338d7d99b5Schristos 		if (sc->sc_x_raw >= 0) {
6348d7d99b5Schristos 			sc->sc_x_raw = (3 * sc->sc_x_raw + x_raw) / 4;
6358d7d99b5Schristos 			sc->sc_y_raw = (3 * sc->sc_y_raw + y_raw) / 4;
6368d7d99b5Schristos 			/*
6378d7d99b5Schristos 			 * Compute virtual position and change if we already
6388d7d99b5Schristos 			 * have a decent position.
6398d7d99b5Schristos 			 */
6408d7d99b5Schristos 			if (sc->sc_x >= 0) {
6418d7d99b5Schristos 				x = smooth_pos(sc->sc_x, sc->sc_x_raw,
6428d7d99b5Schristos 					       sc->sc_noise);
6438d7d99b5Schristos 				y = smooth_pos(sc->sc_y, sc->sc_y_raw,
6448d7d99b5Schristos 					       sc->sc_noise);
6458d7d99b5Schristos 				*dx = x - sc->sc_x;
6468d7d99b5Schristos 				*dy = y - sc->sc_y;
6478d7d99b5Schristos 				sc->sc_x = x;
6488d7d99b5Schristos 				sc->sc_y = y;
6498d7d99b5Schristos 			} else {
6508d7d99b5Schristos 				/* Initialise virtual position. */
6518d7d99b5Schristos 				sc->sc_x = sc->sc_x_raw;
6528d7d99b5Schristos 				sc->sc_y = sc->sc_y_raw;
6538d7d99b5Schristos 			}
6548d7d99b5Schristos 		} else {
6558d7d99b5Schristos 			/* Initialise raw position. */
6568d7d99b5Schristos 			sc->sc_x_raw = x_raw;
6578d7d99b5Schristos 			sc->sc_y_raw = y_raw;
6588d7d99b5Schristos 		}
6598d7d99b5Schristos 	}
6608d7d99b5Schristos 	return 1;
6618d7d99b5Schristos }
6628d7d99b5Schristos 
6638d7d99b5Schristos 
6648d7d99b5Schristos /*
6658d7d99b5Schristos  * Compute the new smoothed position from the previous smoothed position
6668d7d99b5Schristos  * and the raw position.
6678d7d99b5Schristos  */
6688d7d99b5Schristos 
6698d7d99b5Schristos static int
smooth_pos(int pos_old,int pos_raw,int noise)6708d7d99b5Schristos smooth_pos(int pos_old, int pos_raw, int noise)
6718d7d99b5Schristos {
6728d7d99b5Schristos 	int ad, delta;
6738d7d99b5Schristos 
6748d7d99b5Schristos 	delta = pos_raw - pos_old;
6758d7d99b5Schristos 	ad = abs(delta);
6768d7d99b5Schristos 
6778d7d99b5Schristos 	/* Too small changes are ignored. */
6788d7d99b5Schristos 	if (ad < noise / 2)
6798d7d99b5Schristos 		delta = 0;
6808d7d99b5Schristos 	/* A bit larger changes are smoothed. */
6818d7d99b5Schristos 	else if (ad < noise)
6828d7d99b5Schristos 		delta /= 4;
6838d7d99b5Schristos 	else if (ad < 2 * noise)
6848d7d99b5Schristos 		delta /= 2;
6858d7d99b5Schristos 
6868d7d99b5Schristos 	return pos_old + delta;
6878d7d99b5Schristos }
6888d7d99b5Schristos 
6898d7d99b5Schristos 
6908d7d99b5Schristos /*
6918d7d99b5Schristos  * Detect the position of the finger.  Returns the total pressure.
6928d7d99b5Schristos  * The position is returned in pos_ret and the number of fingers
6938d7d99b5Schristos  * is returned in fingers_ret.  The position returned in pos_ret
6948d7d99b5Schristos  * is in [0, (n_sensors - 1) * factor - 1].
6958d7d99b5Schristos  */
6968d7d99b5Schristos 
6978d7d99b5Schristos static int
detect_pos(int * sensors,int n_sensors,int threshold,int fact,int * pos_ret,int * fingers_ret)6988d7d99b5Schristos detect_pos(int *sensors, int n_sensors, int threshold, int fact,
6998d7d99b5Schristos 	   int *pos_ret, int *fingers_ret)
7008d7d99b5Schristos {
7018d7d99b5Schristos 	int i, w, s;
7028d7d99b5Schristos 
7038d7d99b5Schristos 	/*
7048d7d99b5Schristos 	 * Compute the number of fingers, total pressure, and weighted
7058d7d99b5Schristos 	 * position of the fingers.
7068d7d99b5Schristos 	 */
7078d7d99b5Schristos 	*fingers_ret = 0;
7088d7d99b5Schristos 	w = s = 0;
7098d7d99b5Schristos 	for (i = 0; i < n_sensors; i++) {
7108d7d99b5Schristos 		if (sensors[i] >= threshold) {
7118d7d99b5Schristos 			if (i == 0 || sensors[i - 1] < threshold)
7128d7d99b5Schristos 				*fingers_ret += 1;
7138d7d99b5Schristos 			s += sensors[i];
7148d7d99b5Schristos 			w += sensors[i] * i;
7158d7d99b5Schristos 		}
7168d7d99b5Schristos 	}
7178d7d99b5Schristos 
7188d7d99b5Schristos 	if (s > 0)
7198d7d99b5Schristos 		*pos_ret = w * fact / s;
7208d7d99b5Schristos 
7218d7d99b5Schristos 	return s;
7228d7d99b5Schristos }
723