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