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