synaptics.c revision 1.30.2.2 1 /* $NetBSD: synaptics.c,v 1.30.2.2 2017/12/03 11:37:30 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 2005, Steve C. Woodford
5 * Copyright (c) 2004, Ales Krenek
6 * Copyright (c) 2004, Kentaro A. Kurahone
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of the authors nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 /*
39 * TODO:
40 * - Make the sysctl values per-instance instead of global.
41 * - Consider setting initial scaling factors at runtime according
42 * to the values returned by the 'Read Resolutions' command.
43 * - Support the serial protocol (we only support PS/2 for now)
44 * - Support auto-repeat for up/down button Z-axis emulation.
45 * - Maybe add some more gestures (can we use Palm support somehow?)
46 */
47
48 #include "opt_pms.h"
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: synaptics.c,v 1.30.2.2 2017/12/03 11:37:30 jdolecek Exp $");
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/device.h>
56 #include <sys/ioctl.h>
57 #include <sys/sysctl.h>
58 #include <sys/kernel.h>
59 #include <sys/proc.h>
60
61 #include <sys/bus.h>
62
63 #include <dev/pckbport/pckbportvar.h>
64
65 #include <dev/pckbport/synapticsreg.h>
66 #include <dev/pckbport/synapticsvar.h>
67
68 #include <dev/pckbport/pmsreg.h>
69 #include <dev/pckbport/pmsvar.h>
70
71 #include <dev/wscons/wsconsio.h>
72 #include <dev/wscons/wsmousevar.h>
73
74 /*
75 * Absolute-mode packets are decoded and passed around using
76 * the following structure.
77 */
78 struct synaptics_packet {
79 signed short sp_x; /* Unscaled absolute X/Y coordinates */
80 signed short sp_y;
81 u_char sp_z; /* Z (pressure) */
82 u_char sp_w; /* W (contact patch width) */
83 signed short sp_sx; /* Secondary finger unscaled absolute */
84 /* X/Y coordinates */
85 signed short sp_xy;
86 u_char sp_finger; /* 0 for primary, 1 for secondary */
87 char sp_left; /* Left mouse button status */
88 char sp_right; /* Right mouse button status */
89 char sp_middle; /* Middle button status (possibly emulated) */
90 char sp_up; /* Up button status */
91 char sp_down; /* Down button status */
92 };
93
94 static void pms_synaptics_input(void *, int);
95 static void pms_synaptics_process_packet(struct pms_softc *,
96 struct synaptics_packet *);
97 static void pms_sysctl_synaptics(struct sysctllog **);
98 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
99
100 /* Controlled by sysctl. */
101 static int synaptics_up_down_emul = 2;
102 static int synaptics_up_down_motion_delta = 1;
103 static int synaptics_gesture_move = 200;
104 static int synaptics_gesture_length = 20;
105 static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
106 static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
107 static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
108 static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
109 static int synaptics_edge_motion_delta = 32;
110 static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
111 static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
112 static int synaptics_button_boundary = SYNAPTICS_EDGE_BOTTOM + 720;
113 static int synaptics_button2 = SYNAPTICS_EDGE_LEFT + (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
114 static int synaptics_button3 = SYNAPTICS_EDGE_LEFT + 2 * (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
115 static int synaptics_two_fingers_emul = 0;
116 static int synaptics_scale_x = 16;
117 static int synaptics_scale_y = 16;
118 static int synaptics_max_speed_x = 32;
119 static int synaptics_max_speed_y = 32;
120 static int synaptics_movement_threshold = 4;
121
122 /* Sysctl nodes. */
123 static int synaptics_button_boundary_nodenum;
124 static int synaptics_button2_nodenum;
125 static int synaptics_button3_nodenum;
126 static int synaptics_up_down_emul_nodenum;
127 static int synaptics_up_down_motion_delta_nodenum;
128 static int synaptics_gesture_move_nodenum;
129 static int synaptics_gesture_length_nodenum;
130 static int synaptics_edge_left_nodenum;
131 static int synaptics_edge_right_nodenum;
132 static int synaptics_edge_top_nodenum;
133 static int synaptics_edge_bottom_nodenum;
134 static int synaptics_edge_motion_delta_nodenum;
135 static int synaptics_finger_high_nodenum;
136 static int synaptics_finger_low_nodenum;
137 static int synaptics_two_fingers_emul_nodenum;
138 static int synaptics_scale_x_nodenum;
139 static int synaptics_scale_y_nodenum;
140 static int synaptics_max_speed_x_nodenum;
141 static int synaptics_max_speed_y_nodenum;
142 static int synaptics_movement_threshold_nodenum;
143
144 static int
145 synaptics_poll_cmd(struct pms_softc *psc, ...)
146 {
147 u_char cmd[4];
148 size_t i;
149 va_list ap;
150
151 va_start(ap, psc);
152
153 for (i = 0; i < __arraycount(cmd); i++)
154 if ((cmd[i] = (u_char)va_arg(ap, int)) == 0)
155 break;
156 va_end(ap);
157
158 int res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, i, 0,
159 NULL, 0);
160 if (res)
161 aprint_error_dev(psc->sc_dev, "command error %#x\n", cmd[0]);
162 return res;
163 }
164
165 static int
166 synaptics_poll_reset(struct pms_softc *psc)
167 {
168 u_char resp[2];
169 int res;
170
171 u_char cmd[1] = { PMS_RESET };
172 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
173 resp, 1);
174 aprint_debug_dev(psc->sc_dev, "reset %d 0x%02x 0x%02x\n",
175 res, resp[0], resp[1]);
176 return res;
177 }
178
179 static int
180 synaptics_poll_status(struct pms_softc *psc, u_char slice, u_char resp[3])
181 {
182 u_char cmd[1] = { PMS_SEND_DEV_STATUS };
183 int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, slice);
184
185 return res | pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
186 cmd, 1, 3, resp, 0);
187 }
188
189 static void
190 pms_synaptics_probe_extended(struct pms_softc *psc)
191 {
192 struct synaptics_softc *sc = &psc->u.synaptics;
193 u_char resp[3];
194 int res;
195
196 aprint_debug_dev(psc->sc_dev,
197 "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
198 if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
199 sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
200
201 if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
202 sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
203
204 if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
205 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
206
207 if (sc->caps & SYNAPTICS_CAP_MULTIFINGERREPORT)
208 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER_REPORT;
209
210 /* Ask about extra buttons to detect up/down. */
211 if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08)
212 >= SYNAPTICS_EXTENDED_QUERY)
213 {
214 res = synaptics_poll_status(psc, SYNAPTICS_EXTENDED_QUERY, resp);
215 if (res == 0) {
216 int buttons = (resp[1] >> 4);
217 aprint_debug_dev(psc->sc_dev,
218 "%s: Extended Buttons: %d.\n", __func__, buttons);
219
220 aprint_debug_dev(psc->sc_dev, "%s: Extended "
221 "Capabilities: 0x%02x 0x%02x 0x%02x.\n", __func__,
222 resp[0], resp[1], resp[2]);
223 if (buttons >= 2) {
224 /* Yes. */
225 sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
226 }
227 if (resp[0] & 0x1) {
228 /* Vertical scroll area */
229 sc->flags |= SYN_FLAG_HAS_VERTICAL_SCROLL;
230 }
231 if (resp[0] & 0x2) {
232 /* Horizontal scroll area */
233 sc->flags |= SYN_FLAG_HAS_HORIZONTAL_SCROLL;
234 }
235 if (resp[0] & 0x4) {
236 /* Extended W-Mode */
237 sc->flags |= SYN_FLAG_HAS_EXTENDED_WMODE;
238 }
239 }
240 }
241
242 /* Ask about click pad */
243 if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) >=
244 SYNAPTICS_CONTINUED_CAPABILITIES)
245 {
246 res = synaptics_poll_status(psc,
247 SYNAPTICS_CONTINUED_CAPABILITIES, resp);
248
249 /*
250 * The following describes response for the
251 * SYNAPTICS_CONTINUED_CAPABILITIES query.
252 *
253 * byte mask name meaning
254 * ---- ---- ------- ------------
255 * 0 0x01 adjustable threshold capacitive button sensitivity
256 * can be adjusted
257 * 0 0x02 report max query 0x0d gives max coord reported
258 * 0 0x04 clearpad sensor is ClearPad product
259 * 0 0x08 advanced gesture not particularly meaningful
260 * 0 0x10 clickpad bit 0 1-button ClickPad
261 * 0 0x60 multifinger mode identifies firmware finger counting
262 * (not reporting!) algorithm.
263 * Not particularly meaningful
264 * 0 0x80 covered pad W clipped to 14, 15 == pad mostly covered
265 * 1 0x01 clickpad bit 1 2-button ClickPad
266 * 1 0x02 deluxe LED controls touchpad support LED commands
267 * ala multimedia control bar
268 * 1 0x04 reduced filtering firmware does less filtering on
269 * position data, driver should watch
270 * for noise.
271 * 1 0x08 image sensor image sensor tracks 5 fingers, but only
272 * reports 2.
273 * 1 0x01 uniform clickpad whole clickpad moves instead of being
274 * hinged at the top.
275 * 1 0x20 report min query 0x0f gives min coord reported
276 */
277 if (res == 0) {
278 u_char clickpad_type = (resp[0] & 0x10);
279 clickpad_type |= (resp[1] & 0x01);
280
281 aprint_debug_dev(psc->sc_dev, "%s: Continued "
282 "Capabilities 0x%02x 0x%02x 0x%02x.\n", __func__,
283 resp[0], resp[1], resp[2]);
284 switch (clickpad_type) {
285 case 0x10:
286 sc->flags |= SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD;
287 break;
288 case 0x01:
289 sc->flags |= SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD;
290 break;
291 default:
292 break;
293 }
294 }
295 }
296 }
297
298 int
299 pms_synaptics_probe_init(void *vsc)
300 {
301 struct pms_softc *psc = vsc;
302 struct synaptics_softc *sc = &psc->u.synaptics;
303 u_char cmd[1], resp[3];
304 int res, ver_minor, ver_major;
305 struct sysctllog *clog = NULL;
306
307 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
308 SYNAPTICS_IDENTIFY_TOUCHPAD);
309 cmd[0] = PMS_SEND_DEV_STATUS;
310 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
311 resp, 0);
312 if (res) {
313 aprint_debug_dev(psc->sc_dev,
314 "synaptics_probe: Identify Touchpad error.\n");
315 /*
316 * Reset device in case the probe confused it.
317 */
318 doreset:
319 (void)synaptics_poll_reset(psc);
320 return res;
321 }
322
323 if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
324 aprint_debug_dev(psc->sc_dev,
325 "synaptics_probe: Not synaptics.\n");
326 res = 1;
327 goto doreset;
328 }
329
330 sc->flags = 0;
331
332 /* Check for minimum version and print a nice message. */
333 ver_major = resp[2] & 0x0f;
334 ver_minor = resp[0];
335 aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
336 ver_major, ver_minor);
337 if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
338 /* No capability query support. */
339 sc->caps = 0;
340 goto done;
341 }
342
343
344 /* Query the hardware capabilities. */
345 res = synaptics_poll_status(psc, SYNAPTICS_READ_CAPABILITIES, resp);
346 if (res) {
347 /* Hmm, failed to get capabilites. */
348 aprint_error_dev(psc->sc_dev,
349 "synaptics_probe: Failed to query capabilities.\n");
350 goto doreset;
351 }
352
353 sc->caps = (resp[0] << 8) | resp[2];
354
355 if (sc->caps & SYNAPTICS_CAP_MBUTTON)
356 sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
357
358 if (sc->caps & SYNAPTICS_CAP_4BUTTON)
359 sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
360
361 if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
362 pms_synaptics_probe_extended(psc);
363 }
364
365 if (sc->flags) {
366 const char comma[] = ", ";
367 const char *sep = "";
368 aprint_normal_dev(psc->sc_dev, "");
369 if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) {
370 aprint_normal("%sExtended W mode", sep);
371 sep = comma;
372 }
373 if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
374 aprint_normal("%sPassthrough", sep);
375 sep = comma;
376 }
377 if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
378 aprint_normal("%sMiddle button", sep);
379 sep = comma;
380 }
381 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
382 aprint_normal("%sButtons 4/5", sep);
383 sep = comma;
384 }
385 if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS) {
386 aprint_normal("%sUp/down buttons", sep);
387 sep = comma;
388 }
389 if (sc->flags & SYN_FLAG_HAS_PALM_DETECT) {
390 aprint_normal("%sPalm detect", sep);
391 sep = comma;
392 }
393 if (sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
394 aprint_normal("%sOne button click pad", sep);
395 sep = comma;
396 }
397 if (sc->flags & SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD) {
398 aprint_normal("%sTwo button click pad", sep);
399 sep = comma;
400 }
401 if (sc->flags & SYN_FLAG_HAS_VERTICAL_SCROLL) {
402 aprint_normal("%sVertical scroll", sep);
403 sep = comma;
404 }
405 if (sc->flags & SYN_FLAG_HAS_HORIZONTAL_SCROLL) {
406 aprint_normal("%sHorizontal scroll", sep);
407 sep = comma;
408 }
409 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER_REPORT) {
410 aprint_normal("%sMulti-finger Report", sep);
411 sep = comma;
412 }
413 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER)
414 aprint_normal("%sMulti-finger", sep);
415
416 aprint_normal("\n");
417 }
418
419 done:
420 pms_sysctl_synaptics(&clog);
421 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
422 pms_synaptics_input, psc, device_xname(psc->sc_dev));
423
424 return (0);
425 }
426
427 void
428 pms_synaptics_enable(void *vsc)
429 {
430 struct pms_softc *psc = vsc;
431 struct synaptics_softc *sc = &psc->u.synaptics;
432 u_char enable_modes;
433 int res;
434
435 if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
436 /*
437 * Extended capability probes can confuse the passthrough device;
438 * reset the touchpad now to cure that.
439 */
440 res = synaptics_poll_reset(psc);
441 }
442
443 /*
444 * Enable Absolute mode with W (width) reporting, and set
445 * the packet rate to maximum (80 packets per second). Enable
446 * extended W mode if supported so we can report second finger
447 * position.
448 */
449 enable_modes =
450 SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE;
451
452 if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
453 enable_modes |= SYNAPTICS_MODE_EXTENDED_W;
454
455 /*
456 * Synaptics documentation says to disable device before
457 * setting mode.
458 */
459 synaptics_poll_cmd(psc, PMS_DEV_DISABLE, 0);
460 /* a couple of set scales to clear out pending commands */
461 for (int i = 0; i < 2; i++)
462 synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
463
464 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
465 enable_modes);
466 if (res)
467 aprint_error("synaptics: set mode error\n");
468
469 synaptics_poll_cmd(psc, PMS_SET_SAMPLE, SYNAPTICS_CMD_SET_MODE2, 0);
470
471 /* a couple of set scales to clear out pending commands */
472 for (int i = 0; i < 2; i++)
473 synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
474
475 synaptics_poll_cmd(psc, PMS_DEV_ENABLE, 0);
476
477 sc->up_down = 0;
478 sc->prev_fingers = 0;
479 sc->gesture_start_x = sc->gesture_start_y = 0;
480 sc->gesture_start_packet = 0;
481 sc->gesture_tap_packet = 0;
482 sc->gesture_type = 0;
483 sc->gesture_buttons = 0;
484 sc->rem_x[0] = sc->rem_y[0] = 0;
485 sc->rem_x[1] = sc->rem_y[1] = 0;
486 sc->movement_history[0] = 0;
487 sc->movement_history[1] = 0;
488 sc->button_history = 0;
489 }
490
491 void
492 pms_synaptics_resume(void *vsc)
493 {
494 (void)synaptics_poll_reset(vsc);
495 }
496
497 static void
498 pms_sysctl_synaptics(struct sysctllog **clog)
499 {
500 int rc, root_num;
501 const struct sysctlnode *node;
502
503 if ((rc = sysctl_createv(clog, 0, NULL, &node,
504 CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
505 SYSCTL_DESCR("Synaptics touchpad controls"),
506 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
507 goto err;
508
509 root_num = node->sysctl_num;
510
511 if ((rc = sysctl_createv(clog, 0, NULL, &node,
512 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
513 CTLTYPE_INT, "up_down_emulation",
514 SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
515 pms_sysctl_synaptics_verify, 0,
516 &synaptics_up_down_emul,
517 0, CTL_HW, root_num, CTL_CREATE,
518 CTL_EOL)) != 0)
519 goto err;
520
521 synaptics_up_down_emul_nodenum = node->sysctl_num;
522
523 if ((rc = sysctl_createv(clog, 0, NULL, &node,
524 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
525 CTLTYPE_INT, "up_down_motion_delta",
526 SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
527 pms_sysctl_synaptics_verify, 0,
528 &synaptics_up_down_motion_delta,
529 0, CTL_HW, root_num, CTL_CREATE,
530 CTL_EOL)) != 0)
531 goto err;
532
533 synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
534
535 if ((rc = sysctl_createv(clog, 0, NULL, &node,
536 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
537 CTLTYPE_INT, "gesture_move",
538 SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
539 pms_sysctl_synaptics_verify, 0,
540 &synaptics_gesture_move,
541 0, CTL_HW, root_num, CTL_CREATE,
542 CTL_EOL)) != 0)
543 goto err;
544
545 synaptics_gesture_move_nodenum = node->sysctl_num;
546
547 if ((rc = sysctl_createv(clog, 0, NULL, &node,
548 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
549 CTLTYPE_INT, "gesture_length",
550 SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
551 pms_sysctl_synaptics_verify, 0,
552 &synaptics_gesture_length,
553 0, CTL_HW, root_num, CTL_CREATE,
554 CTL_EOL)) != 0)
555 goto err;
556
557 synaptics_gesture_length_nodenum = node->sysctl_num;
558
559 if ((rc = sysctl_createv(clog, 0, NULL, &node,
560 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
561 CTLTYPE_INT, "edge_left",
562 SYSCTL_DESCR("Define left edge of touchpad"),
563 pms_sysctl_synaptics_verify, 0,
564 &synaptics_edge_left,
565 0, CTL_HW, root_num, CTL_CREATE,
566 CTL_EOL)) != 0)
567 goto err;
568
569 synaptics_edge_left_nodenum = node->sysctl_num;
570
571 if ((rc = sysctl_createv(clog, 0, NULL, &node,
572 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
573 CTLTYPE_INT, "edge_right",
574 SYSCTL_DESCR("Define right edge of touchpad"),
575 pms_sysctl_synaptics_verify, 0,
576 &synaptics_edge_right,
577 0, CTL_HW, root_num, CTL_CREATE,
578 CTL_EOL)) != 0)
579 goto err;
580
581 synaptics_edge_right_nodenum = node->sysctl_num;
582
583 if ((rc = sysctl_createv(clog, 0, NULL, &node,
584 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
585 CTLTYPE_INT, "edge_top",
586 SYSCTL_DESCR("Define top edge of touchpad"),
587 pms_sysctl_synaptics_verify, 0,
588 &synaptics_edge_top,
589 0, CTL_HW, root_num, CTL_CREATE,
590 CTL_EOL)) != 0)
591 goto err;
592
593 synaptics_edge_top_nodenum = node->sysctl_num;
594
595 if ((rc = sysctl_createv(clog, 0, NULL, &node,
596 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
597 CTLTYPE_INT, "edge_bottom",
598 SYSCTL_DESCR("Define bottom edge of touchpad"),
599 pms_sysctl_synaptics_verify, 0,
600 &synaptics_edge_bottom,
601 0, CTL_HW, root_num, CTL_CREATE,
602 CTL_EOL)) != 0)
603 goto err;
604
605 synaptics_edge_bottom_nodenum = node->sysctl_num;
606
607 if ((rc = sysctl_createv(clog, 0, NULL, &node,
608 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
609 CTLTYPE_INT, "edge_motion_delta",
610 SYSCTL_DESCR("Define edge motion rate"),
611 pms_sysctl_synaptics_verify, 0,
612 &synaptics_edge_motion_delta,
613 0, CTL_HW, root_num, CTL_CREATE,
614 CTL_EOL)) != 0)
615 goto err;
616
617 synaptics_edge_motion_delta_nodenum = node->sysctl_num;
618
619 if ((rc = sysctl_createv(clog, 0, NULL, &node,
620 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
621 CTLTYPE_INT, "finger_high",
622 SYSCTL_DESCR("Define finger applied pressure threshold"),
623 pms_sysctl_synaptics_verify, 0,
624 &synaptics_finger_high,
625 0, CTL_HW, root_num, CTL_CREATE,
626 CTL_EOL)) != 0)
627 goto err;
628
629 synaptics_finger_high_nodenum = node->sysctl_num;
630
631 if ((rc = sysctl_createv(clog, 0, NULL, &node,
632 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
633 CTLTYPE_INT, "finger_low",
634 SYSCTL_DESCR("Define finger removed pressure threshold"),
635 pms_sysctl_synaptics_verify, 0,
636 &synaptics_finger_low,
637 0, CTL_HW, root_num, CTL_CREATE,
638 CTL_EOL)) != 0)
639 goto err;
640
641 synaptics_finger_low_nodenum = node->sysctl_num;
642
643 if ((rc = sysctl_createv(clog, 0, NULL, &node,
644 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
645 CTLTYPE_INT, "two_fingers_emulation",
646 SYSCTL_DESCR("Map two fingers to middle button"),
647 pms_sysctl_synaptics_verify, 0,
648 &synaptics_two_fingers_emul,
649 0, CTL_HW, root_num, CTL_CREATE,
650 CTL_EOL)) != 0)
651 goto err;
652
653 synaptics_two_fingers_emul_nodenum = node->sysctl_num;
654
655 if ((rc = sysctl_createv(clog, 0, NULL, &node,
656 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
657 CTLTYPE_INT, "scale_x",
658 SYSCTL_DESCR("Horizontal movement scale factor"),
659 pms_sysctl_synaptics_verify, 0,
660 &synaptics_scale_x,
661 0, CTL_HW, root_num, CTL_CREATE,
662 CTL_EOL)) != 0)
663 goto err;
664
665 synaptics_scale_x_nodenum = node->sysctl_num;
666
667 if ((rc = sysctl_createv(clog, 0, NULL, &node,
668 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
669 CTLTYPE_INT, "scale_y",
670 SYSCTL_DESCR("Vertical movement scale factor"),
671 pms_sysctl_synaptics_verify, 0,
672 &synaptics_scale_y,
673 0, CTL_HW, root_num, CTL_CREATE,
674 CTL_EOL)) != 0)
675 goto err;
676
677 synaptics_scale_y_nodenum = node->sysctl_num;
678
679 if ((rc = sysctl_createv(clog, 0, NULL, &node,
680 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
681 CTLTYPE_INT, "max_speed_x",
682 SYSCTL_DESCR("Horizontal movement maximum speed"),
683 pms_sysctl_synaptics_verify, 0,
684 &synaptics_max_speed_x,
685 0, CTL_HW, root_num, CTL_CREATE,
686 CTL_EOL)) != 0)
687 goto err;
688
689 synaptics_max_speed_x_nodenum = node->sysctl_num;
690
691 if ((rc = sysctl_createv(clog, 0, NULL, &node,
692 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
693 CTLTYPE_INT, "max_speed_y",
694 SYSCTL_DESCR("Vertical movement maximum speed"),
695 pms_sysctl_synaptics_verify, 0,
696 &synaptics_max_speed_y,
697 0, CTL_HW, root_num, CTL_CREATE,
698 CTL_EOL)) != 0)
699 goto err;
700
701 synaptics_max_speed_y_nodenum = node->sysctl_num;
702
703 if ((rc = sysctl_createv(clog, 0, NULL, &node,
704 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
705 CTLTYPE_INT, "movement_threshold",
706 SYSCTL_DESCR("Minimum reported movement threshold"),
707 pms_sysctl_synaptics_verify, 0,
708 &synaptics_movement_threshold,
709 0, CTL_HW, root_num, CTL_CREATE,
710 CTL_EOL)) != 0)
711 goto err;
712
713 synaptics_movement_threshold_nodenum = node->sysctl_num;
714
715 if ((rc = sysctl_createv(clog, 0, NULL, &node,
716 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
717 CTLTYPE_INT, "button_boundary",
718 SYSCTL_DESCR("Top edge of button area"),
719 pms_sysctl_synaptics_verify, 0,
720 &synaptics_button_boundary,
721 0, CTL_HW, root_num, CTL_CREATE,
722 CTL_EOL)) != 0)
723 goto err;
724
725 synaptics_button_boundary_nodenum = node->sysctl_num;
726
727 if ((rc = sysctl_createv(clog, 0, NULL, &node,
728 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
729 CTLTYPE_INT, "button2_edge",
730 SYSCTL_DESCR("Left edge of button 2 region"),
731 pms_sysctl_synaptics_verify, 0,
732 &synaptics_button2,
733 0, CTL_HW, root_num, CTL_CREATE,
734 CTL_EOL)) != 0)
735 goto err;
736
737 synaptics_button2_nodenum = node->sysctl_num;
738
739 if ((rc = sysctl_createv(clog, 0, NULL, &node,
740 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
741 CTLTYPE_INT, "button3_edge",
742 SYSCTL_DESCR("Left edge of button 3 region"),
743 pms_sysctl_synaptics_verify, 0,
744 &synaptics_button3,
745 0, CTL_HW, root_num, CTL_CREATE,
746 CTL_EOL)) != 0)
747 goto err;
748
749 synaptics_button3_nodenum = node->sysctl_num;
750 return;
751
752 err:
753 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
754 }
755
756 static int
757 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
758 {
759 int error, t;
760 struct sysctlnode node;
761
762 node = *rnode;
763 t = *(int *)rnode->sysctl_data;
764 node.sysctl_data = &t;
765 error = sysctl_lookup(SYSCTLFN_CALL(&node));
766 if (error || newp == NULL)
767 return error;
768
769 /* Sanity check the params. */
770 if (node.sysctl_num == synaptics_up_down_emul_nodenum ||
771 node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
772 if (t < 0 || t > 2)
773 return (EINVAL);
774 } else
775 if (node.sysctl_num == synaptics_gesture_length_nodenum ||
776 node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
777 node.sysctl_num == synaptics_up_down_motion_delta_nodenum) {
778 if (t < 0)
779 return (EINVAL);
780 } else
781 if (node.sysctl_num == synaptics_edge_left_nodenum ||
782 node.sysctl_num == synaptics_edge_bottom_nodenum) {
783 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
784 return (EINVAL);
785 } else
786 if (node.sysctl_num == synaptics_edge_right_nodenum ||
787 node.sysctl_num == synaptics_edge_top_nodenum) {
788 if (t < (SYNAPTICS_EDGE_MAX / 2))
789 return (EINVAL);
790 } else
791 if (node.sysctl_num == synaptics_scale_x_nodenum ||
792 node.sysctl_num == synaptics_scale_y_nodenum) {
793 if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
794 return (EINVAL);
795 } else
796 if (node.sysctl_num == synaptics_finger_high_nodenum) {
797 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
798 t < synaptics_finger_low)
799 return (EINVAL);
800 } else
801 if (node.sysctl_num == synaptics_finger_low_nodenum) {
802 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
803 t > synaptics_finger_high)
804 return (EINVAL);
805 } else
806 if (node.sysctl_num == synaptics_gesture_move_nodenum ||
807 node.sysctl_num == synaptics_movement_threshold_nodenum) {
808 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
809 return (EINVAL);
810 } else
811 if (node.sysctl_num == synaptics_button_boundary) {
812 if (t < 0 || t < SYNAPTICS_EDGE_BOTTOM ||
813 t > SYNAPTICS_EDGE_TOP)
814 return (EINVAL);
815 } else
816 if (node.sysctl_num == synaptics_button2 ||
817 node.sysctl_num == synaptics_button3) {
818 if (t < SYNAPTICS_EDGE_LEFT || t > SYNAPTICS_EDGE_RIGHT)
819 return (EINVAL);
820 } else
821 return (EINVAL);
822
823 *(int *)rnode->sysctl_data = t;
824
825 return (0);
826 }
827
828 /* Masks for the first byte of a packet */
829 #define PMS_LBUTMASK 0x01
830 #define PMS_RBUTMASK 0x02
831 #define PMS_MBUTMASK 0x04
832
833 static void
834 pms_synaptics_parse(struct pms_softc *psc)
835 {
836 struct synaptics_softc *sc = &psc->u.synaptics;
837 struct synaptics_packet sp;
838 char new_buttons, ew_mode;
839
840 memset(&sp, 0, sizeof(sp));
841
842 /* Width of finger */
843 sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
844 ((psc->packet[0] & 0x04) >> 1) +
845 ((psc->packet[3] & 0x04) >> 2);
846 sp.sp_finger = 0;
847 if (sp.sp_w == SYNAPTICS_WIDTH_EXTENDED_W) {
848 ew_mode = psc->packet[5] >> 4;
849 switch (ew_mode)
850 {
851 case SYNAPTICS_EW_WHEEL:
852 /* scroll wheel report, ignore for now */
853 aprint_debug_dev(psc->sc_dev, "mouse wheel packet\n");
854 return;
855
856 case SYNAPTICS_EW_SECONDARY_FINGER:
857 /* parse the second finger report */
858
859 sp.sp_finger = 1; /* just one other finger for now */
860 sp.sp_x = psc->packet[1]
861 + ((psc->packet[4] & 0x0f) << 8);
862 sp.sp_y = psc->packet[2]
863 + ((psc->packet[4] & 0xf0) << 4);
864 sp.sp_z = (psc->packet[3] & 0x30)
865 + (psc->packet[5] & 0x0f);
866
867 /* keep same buttons down as primary */
868 sp.sp_left = sc->button_history & PMS_LBUTMASK;
869 sp.sp_middle = sc->button_history & PMS_MBUTMASK;
870 sp.sp_right = sc->button_history & PMS_RBUTMASK;
871 break;
872
873 case SYNAPTICS_EW_FINGER_STATUS:
874 /* reports which finger is primary/secondary
875 * ignore for now.
876 */
877 return;
878
879 default:
880 aprint_error_dev(psc->sc_dev,
881 "invalid extended w mode %d\n",
882 ew_mode);
883 return;
884 }
885 } else {
886
887 /* Absolute X/Y coordinates of finger */
888 sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
889 ((psc->packet[3] & 0x10) << 8);
890 sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
891 ((psc->packet[3] & 0x20) << 7);
892
893 /* Pressure */
894 sp.sp_z = psc->packet[2];
895
896 /* Left/Right button handling. */
897 sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
898 sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
899
900 /* Up/Down buttons. */
901 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
902 /* Old up/down buttons. */
903 sp.sp_up = sp.sp_left ^
904 (psc->packet[3] & PMS_LBUTMASK);
905 sp.sp_down = sp.sp_right ^
906 (psc->packet[3] & PMS_RBUTMASK);
907 } else if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
908 ((psc->packet[0] & PMS_RBUTMASK) ^
909 (psc->packet[3] & PMS_RBUTMASK))) {
910 /* New up/down button. */
911 sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
912 sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
913 } else {
914 sp.sp_up = 0;
915 sp.sp_down = 0;
916 }
917
918 new_buttons = 0;
919 if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
920 /* This is not correctly specified. Read this button press
921 * from L/U bit. Emulate 3 buttons by checking the
922 * coordinates of the click and returning the appropriate
923 * button code. Outside the button region default to a
924 * left click.
925 */
926 u_char bstate = (psc->packet[0] ^ psc->packet[3])
927 & 0x01;
928 if (sp.sp_y < synaptics_button_boundary) {
929 if (sp.sp_x > synaptics_button3) {
930 sp.sp_right =
931 bstate ? PMS_RBUTMASK : 0;
932 } else if (sp.sp_x > synaptics_button2) {
933 sp.sp_middle =
934 bstate ? PMS_MBUTMASK : 0;
935 } else {
936 sp.sp_left = bstate ? PMS_LBUTMASK : 0;
937 }
938 } else
939 sp.sp_left = bstate ? 1 : 0;
940 new_buttons = sp.sp_left | sp.sp_middle | sp.sp_right;
941 if (new_buttons != sc->button_history) {
942 if (sc->button_history == 0)
943 sc->button_history = new_buttons;
944 else if (new_buttons == 0) {
945 sc->button_history = 0;
946 /* ensure all buttons are cleared just in
947 * case finger comes off in a different
948 * region.
949 */
950 sp.sp_left = 0;
951 sp.sp_middle = 0;
952 sp.sp_right = 0;
953 } else {
954 /* make sure we keep the same button even
955 * if the finger moves to a different
956 * region. This precludes chording
957 * but, oh well.
958 */
959 sp.sp_left = sc->button_history & PMS_LBUTMASK;
960 sp.sp_middle = sc->button_history
961 & PMS_MBUTMASK;
962 sp.sp_right = sc->button_history & PMS_RBUTMASK;
963 }
964 }
965 } else if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
966 /* Old style Middle Button. */
967 sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
968 (psc->packet[3] & PMS_LBUTMASK);
969 } else if (synaptics_up_down_emul == 1) {
970 /* Do middle button emulation using up/down buttons */
971 sp.sp_middle = sp.sp_up | sp.sp_down;
972 sp.sp_up = sp.sp_down = 0;
973 } else
974 sp.sp_middle = 0;
975
976 }
977
978 pms_synaptics_process_packet(psc, &sp);
979 }
980
981 static void
982 pms_synaptics_passthrough(struct pms_softc *psc)
983 {
984 int dx, dy, dz;
985 int buttons, changed;
986 int s;
987
988 buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
989 ((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
990 ((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);
991
992 dx = psc->packet[4];
993 if (dx >= 128)
994 dx -= 256;
995 if (dx == -128)
996 dx = -127;
997
998 dy = psc->packet[5];
999 if (dy >= 128)
1000 dy -= 256;
1001 if (dy == -128)
1002 dy = -127;
1003
1004 dz = 0;
1005
1006 changed = buttons ^ (psc->buttons & 0xe0);
1007 psc->buttons ^= changed;
1008
1009 if (dx || dy || dz || changed) {
1010 buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
1011 s = spltty();
1012 wsmouse_input(psc->sc_wsmousedev,
1013 buttons, dx, dy, dz, 0,
1014 WSMOUSE_INPUT_DELTA);
1015 splx(s);
1016 }
1017 }
1018
1019 static void
1020 pms_synaptics_input(void *vsc, int data)
1021 {
1022 struct pms_softc *psc = vsc;
1023 struct timeval diff;
1024
1025 if (!psc->sc_enabled) {
1026 /* Interrupts are not expected. Discard the byte. */
1027 return;
1028 }
1029
1030 getmicrouptime(&psc->current);
1031
1032 if (psc->inputstate > 0) {
1033 timersub(&psc->current, &psc->last, &diff);
1034 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
1035 aprint_debug_dev(psc->sc_dev,
1036 "pms_input: unusual delay (%ld.%06ld s), "
1037 "scheduling reset\n",
1038 (long)diff.tv_sec, (long)diff.tv_usec);
1039 printf("pms_input: unusual delay (%ld.%06ld s), "
1040 "scheduling reset\n",
1041 (long)diff.tv_sec, (long)diff.tv_usec);
1042 psc->inputstate = 0;
1043 psc->sc_enabled = 0;
1044 wakeup(&psc->sc_enabled);
1045 return;
1046 }
1047 }
1048 psc->last = psc->current;
1049
1050 switch (psc->inputstate) {
1051 case 0:
1052 if ((data & 0xc8) != 0x80) {
1053 aprint_debug_dev(psc->sc_dev,
1054 "pms_input: 0x%02x out of sync\n", data);
1055 return; /* not in sync yet, discard input */
1056 }
1057 /*FALLTHROUGH*/
1058
1059 case 3:
1060 if ((data & 8) == 8) {
1061 aprint_debug_dev(psc->sc_dev,
1062 "pms_input: dropped in relative mode, reset\n");
1063 psc->inputstate = 0;
1064 psc->sc_enabled = 0;
1065 wakeup(&psc->sc_enabled);
1066 return;
1067 }
1068 }
1069
1070 psc->packet[psc->inputstate++] = data & 0xff;
1071 if (psc->inputstate == 6) {
1072 /*
1073 * We have a complete packet.
1074 * Extract the pertinent details.
1075 */
1076 psc->inputstate = 0;
1077 if ((psc->packet[0] & 0xfc) == 0x84 &&
1078 (psc->packet[3] & 0xcc) == 0xc4) {
1079 /* W = SYNAPTICS_WIDTH_PASSTHROUGH, PS/2 passthrough */
1080 pms_synaptics_passthrough(psc);
1081 } else {
1082 pms_synaptics_parse(psc);
1083 }
1084 }
1085 }
1086
1087 static inline int
1088 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
1089 int *palmp)
1090 {
1091 int fingers;
1092
1093 /* Assume no palm */
1094 *palmp = 0;
1095
1096 /*
1097 * Apply some hysteresis when checking for a finger.
1098 * When the finger is first applied, we ignore it until the
1099 * pressure exceeds the 'high' threshold. The finger is considered
1100 * removed only when pressure falls beneath the 'low' threshold.
1101 */
1102 if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
1103 (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
1104 fingers = 1;
1105 else
1106 fingers = 0;
1107
1108 /*
1109 * If the pad can't do palm detection, skip the rest.
1110 */
1111 if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
1112 return (fingers);
1113
1114 /*
1115 * Palm detection
1116 */
1117 if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
1118 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
1119 *palmp = 1;
1120
1121 if (sc->prev_fingers == 0 &&
1122 (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
1123 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
1124 /*
1125 * Contact area or pressure is too great to be a finger.
1126 * Just ignore it for now.
1127 */
1128 return (0);
1129 }
1130
1131 /*
1132 * Detect 2 and 3 fingers if supported, but only if multiple
1133 * fingers appear within the tap gesture time period.
1134 */
1135 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
1136 SYN_TIME(sc, sc->gesture_start_packet,
1137 sp->sp_finger) < synaptics_gesture_length) {
1138 switch (sp->sp_w) {
1139 case SYNAPTICS_WIDTH_TWO_FINGERS:
1140 fingers = 2;
1141 break;
1142
1143 case SYNAPTICS_WIDTH_THREE_OR_MORE:
1144 fingers = 3;
1145 break;
1146
1147 case SYNAPTICS_WIDTH_PEN:
1148 fingers = 1;
1149 break;
1150
1151 default:
1152 /*
1153 * The width value can report spurious single-finger
1154 * events after a multi-finger event.
1155 */
1156 if (sc->prev_fingers > 1)
1157 fingers = sc->prev_fingers;
1158 else
1159 fingers = 1;
1160 break;
1161 }
1162 }
1163
1164 return (fingers);
1165 }
1166
1167 static inline void
1168 synaptics_gesture_detect(struct synaptics_softc *sc,
1169 struct synaptics_packet *sp, int fingers)
1170 {
1171 int gesture_len, gesture_buttons;
1172 int set_buttons;
1173
1174 gesture_len = SYN_TIME(sc, sc->gesture_start_packet, sp->sp_finger);
1175 gesture_buttons = sc->gesture_buttons;
1176
1177 if (fingers > 0 && (fingers == sc->prev_fingers)) {
1178 /* Finger is still present */
1179 sc->gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
1180 sc->gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
1181 } else
1182 if (fingers && sc->prev_fingers == 0) {
1183 /*
1184 * Finger was just applied.
1185 * If the previous gesture was a single-click, set things
1186 * up to deal with a possible drag or double-click gesture.
1187 * Basically, if the finger is removed again within
1188 * 'synaptics_gesture_length' packets, this is treated
1189 * as a double-click. Otherwise we will emulate holding
1190 * the left button down whilst dragging the mouse.
1191 */
1192 if (SYN_IS_SINGLE_TAP(sc->gesture_type))
1193 sc->gesture_type |= SYN_GESTURE_DRAG;
1194
1195 sc->gesture_start_x = abs(sp->sp_x);
1196 sc->gesture_start_y = abs(sp->sp_y);
1197 sc->gesture_move_x = 0;
1198 sc->gesture_move_y = 0;
1199 sc->gesture_start_packet = sc->total_packets[0];
1200
1201 #ifdef DIAGNOSTIC
1202 aprint_debug("Finger applied: gesture_start_x: %d gesture_start_y: %d\n",
1203 sc->gesture_start_x, sc->gesture_start_y);
1204 #endif
1205 } else
1206 if (fingers == 0 && sc->prev_fingers != 0) {
1207 /*
1208 * Finger was just removed.
1209 * Check if the contact time and finger movement were
1210 * small enough to qualify as a gesture.
1211 * Ignore finger movement if multiple fingers were
1212 * detected (the pad may report coordinates for any
1213 * of the fingers).
1214 */
1215
1216 #ifdef DIAGNOSTIC
1217 aprint_debug("Finger removed: gesture_len: %d (%d)\n",
1218 gesture_len, synaptics_gesture_length);
1219 aprint_debug("gesture_move_x: %d (%d) sp_x: %d\n",
1220 sc->gesture_move_x, synaptics_gesture_move, abs(sp->sp_x));
1221 aprint_debug("gesture_move_y: %d (%d) sp_y: %d\n",
1222 sc->gesture_move_y, synaptics_gesture_move, abs(sp->sp_y));
1223 #endif
1224
1225 if (gesture_len < synaptics_gesture_length &&
1226 ((sc->gesture_move_x < synaptics_gesture_move &&
1227 sc->gesture_move_y < synaptics_gesture_move))) {
1228 /*
1229 * Looking good so far.
1230 */
1231 if (SYN_IS_DRAG(sc->gesture_type)) {
1232 /*
1233 * Promote this gesture to double-click.
1234 */
1235 sc->gesture_type |= SYN_GESTURE_DOUBLE;
1236 sc->gesture_type &= ~SYN_GESTURE_SINGLE;
1237 } else {
1238 /*
1239 * Single tap gesture. Set the tap length timer
1240 * and flag a single-click.
1241 */
1242 sc->gesture_tap_packet = sc->total_packets[0];
1243 sc->gesture_type |= SYN_GESTURE_SINGLE;
1244
1245 /*
1246 * The gesture can be modified depending on
1247 * the number of fingers detected.
1248 *
1249 * 1: Normal left button emulation.
1250 * 2: Either middle button or right button
1251 * depending on the value of the two_fingers
1252 * sysctl variable.
1253 * 3: Right button.
1254 */
1255 switch (sc->prev_fingers) {
1256 case 2:
1257 if (synaptics_two_fingers_emul == 1)
1258 gesture_buttons |= PMS_RBUTMASK;
1259 else
1260 if (synaptics_two_fingers_emul == 2)
1261 gesture_buttons |= PMS_MBUTMASK;
1262 break;
1263 case 3:
1264 gesture_buttons |= PMS_RBUTMASK;
1265 break;
1266 default:
1267 gesture_buttons |= PMS_LBUTMASK;
1268 break;
1269 }
1270 }
1271 }
1272
1273 /*
1274 * Always clear drag state when the finger is removed.
1275 */
1276 sc->gesture_type &= ~SYN_GESTURE_DRAG;
1277 }
1278
1279 if (sc->gesture_type == 0) {
1280 /*
1281 * There is no gesture in progress.
1282 * Clear emulated button state.
1283 */
1284 sc->gesture_buttons = 0;
1285 return;
1286 }
1287
1288 /*
1289 * A gesture is in progress.
1290 */
1291 set_buttons = 0;
1292
1293 if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
1294 /*
1295 * Single-click.
1296 * Activate the relevant button(s) until the
1297 * gesture tap timer has expired.
1298 */
1299 if (SYN_TIME(sc, sc->gesture_tap_packet, sp->sp_finger) <
1300 synaptics_gesture_length)
1301 set_buttons = 1;
1302 else
1303 sc->gesture_type &= ~SYN_GESTURE_SINGLE;
1304 } else
1305 if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
1306 /*
1307 * Double-click.
1308 * Activate the relevant button(s) once.
1309 */
1310 set_buttons = 1;
1311 sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
1312 }
1313
1314 if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
1315 /*
1316 * Single-click and drag.
1317 * Maintain button state until the finger is removed.
1318 */
1319 sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
1320 sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
1321 sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
1322 }
1323
1324 sc->gesture_buttons = gesture_buttons;
1325 }
1326
1327 static inline int
1328 synaptics_filter_policy(struct synaptics_softc *sc, int finger, int *history,
1329 int value)
1330 {
1331 int a, b, rv, count;
1332
1333 count = sc->total_packets[finger];
1334
1335 /*
1336 * Once we've accumulated at least SYN_HIST_SIZE values, combine
1337 * each new value with the previous two and return the average.
1338 *
1339 * This is necessary when the touchpad is operating in 80 packets
1340 * per second mode, as it performs little internal filtering on
1341 * reported values.
1342 *
1343 * Using a rolling average helps to filter out jitter caused by
1344 * tiny finger movements.
1345 */
1346 if (sc->movement_history[finger] >= SYN_HIST_SIZE) {
1347 a = (history[(count + 0) % SYN_HIST_SIZE] +
1348 history[(count + 1) % SYN_HIST_SIZE]) / 2;
1349
1350 b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
1351
1352 rv = b - a;
1353
1354 /*
1355 * Don't report the movement if it's below a certain
1356 * threshold.
1357 */
1358 if (abs(rv) < synaptics_movement_threshold)
1359 rv = 0;
1360 } else
1361 rv = 0;
1362
1363 /*
1364 * Add the new value to the history buffer.
1365 */
1366 history[(count + 1) % SYN_HIST_SIZE] = value;
1367
1368 return (rv);
1369 }
1370
1371 /* Edge detection */
1372 #define SYN_EDGE_TOP 1
1373 #define SYN_EDGE_BOTTOM 2
1374 #define SYN_EDGE_LEFT 4
1375 #define SYN_EDGE_RIGHT 8
1376
1377 static inline int
1378 synaptics_check_edge(int x, int y)
1379 {
1380 int rv = 0;
1381
1382 if (x < synaptics_edge_left)
1383 rv |= SYN_EDGE_LEFT;
1384 else
1385 if (x > synaptics_edge_right)
1386 rv |= SYN_EDGE_RIGHT;
1387
1388 if (y < synaptics_edge_bottom)
1389 rv |= SYN_EDGE_BOTTOM;
1390 else
1391 if (y > synaptics_edge_top)
1392 rv |= SYN_EDGE_TOP;
1393
1394 return (rv);
1395 }
1396
1397 static inline int
1398 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
1399 {
1400
1401 /*
1402 * When edge motion is enabled, synaptics_edge_motion_delta is
1403 * combined with the current delta, together with the direction
1404 * in which to simulate the motion. The result is added to
1405 * the delta derived from finger movement. This provides a smooth
1406 * transition from finger movement to edge motion.
1407 */
1408 delta = synaptics_edge_motion_delta + (dir * delta);
1409 if (delta < 0)
1410 return (0);
1411 if (delta > synaptics_edge_motion_delta)
1412 return (synaptics_edge_motion_delta);
1413 return (delta);
1414 }
1415
1416 static inline int
1417 synaptics_scale(int delta, int scale, int *remp)
1418 {
1419 int rv;
1420
1421 /*
1422 * Scale the raw delta in Synaptics coordinates (0-6143) into
1423 * something more reasonable by dividing the raw delta by a
1424 * scale factor. Any remainder from the previous scale result
1425 * is added to the current delta before scaling.
1426 * This prevents loss of resolution for very small/slow
1427 * movements of the finger.
1428 */
1429 delta += *remp;
1430 rv = delta / scale;
1431 *remp = delta % scale;
1432
1433 return (rv);
1434 }
1435
1436 static inline void
1437 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
1438 int finger, int *dxp, int *dyp)
1439 {
1440 int dx, dy, edge;
1441
1442 /*
1443 * Compute the next values of dx and dy
1444 */
1445 dx = synaptics_filter_policy(sc, finger, sc->history_x[finger],
1446 sp->sp_x);
1447 dy = synaptics_filter_policy(sc, finger, sc->history_y[finger],
1448 sp->sp_y);
1449
1450 /*
1451 * If we're dealing with a drag gesture, and the finger moves to
1452 * the edge of the touchpad, apply edge motion emulation if it
1453 * is enabled.
1454 */
1455 if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
1456 edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
1457
1458 if (edge & SYN_EDGE_LEFT)
1459 dx -= synaptics_edge_motion(sc, dx, 1);
1460 if (edge & SYN_EDGE_RIGHT)
1461 dx += synaptics_edge_motion(sc, dx, -1);
1462 if (edge & SYN_EDGE_BOTTOM)
1463 dy -= synaptics_edge_motion(sc, dy, 1);
1464 if (edge & SYN_EDGE_TOP)
1465 dy += synaptics_edge_motion(sc, dy, -1);
1466 }
1467
1468 /*
1469 * Apply scaling to both deltas
1470 */
1471 dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x[finger]);
1472 dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y[finger]);
1473
1474 /*
1475 * Clamp deltas to specified maximums.
1476 */
1477 if (dx > synaptics_max_speed_x)
1478 dx = synaptics_max_speed_x;
1479 if (dy > synaptics_max_speed_y)
1480 dy = synaptics_max_speed_y;
1481
1482 *dxp = dx;
1483 *dyp = dy;
1484
1485 sc->movement_history[finger]++;
1486 }
1487
1488 static void
1489 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
1490 {
1491 struct synaptics_softc *sc = &psc->u.synaptics;
1492 int dx, dy, dz;
1493 int fingers, palm, buttons, changed;
1494 int s;
1495
1496 /*
1497 * Do Z-axis emulation using up/down buttons if required.
1498 * Note that the pad will send a one second burst of packets
1499 * when an up/down button is pressed and held. At the moment
1500 * we don't deal with auto-repeat, so convert the burst into
1501 * a one-shot.
1502 */
1503 dz = 0;
1504 if (synaptics_up_down_emul == 2) {
1505 if (sc->up_down == 0) {
1506 if (sp->sp_up && sp->sp_down) {
1507 /*
1508 * Most up/down buttons will be actuated using
1509 * a rocker switch, so we should never see
1510 * them both simultaneously. But just in case,
1511 * treat this situation as a middle button
1512 * event.
1513 */
1514 sp->sp_middle = 1;
1515 } else
1516 if (sp->sp_up)
1517 dz = -synaptics_up_down_motion_delta;
1518 else
1519 if (sp->sp_down)
1520 dz = synaptics_up_down_motion_delta;
1521 }
1522
1523 sc->up_down = sp->sp_up | sp->sp_down;
1524 sp->sp_up = sp->sp_down = 0;
1525 }
1526
1527 /*
1528 * Determine whether or not a finger is on the pad.
1529 * On some pads, this will return the number of fingers
1530 * detected.
1531 */
1532 fingers = synaptics_finger_detect(sc, sp, &palm);
1533
1534 /*
1535 * Do gesture processing only if we didn't detect a palm and
1536 * it is not the seondary finger.
1537 */
1538 if ((sp->sp_finger == 0) && (palm == 0))
1539 synaptics_gesture_detect(sc, sp, fingers);
1540 else
1541 sc->gesture_type = sc->gesture_buttons = 0;
1542
1543 /*
1544 * Determine what buttons to report
1545 */
1546 buttons = (sp->sp_left ? 0x1 : 0) |
1547 (sp->sp_middle ? 0x2 : 0) |
1548 (sp->sp_right ? 0x4 : 0) |
1549 (sp->sp_up ? 0x8 : 0) |
1550 (sp->sp_down ? 0x10 : 0);
1551 changed = buttons ^ (psc->buttons & 0x1f);
1552 psc->buttons ^= changed;
1553
1554 sc->prev_fingers = fingers;
1555 sc->total_packets[sp->sp_finger]++;
1556
1557 /*
1558 * Do movement processing IFF we have a single finger and no palm or
1559 * a secondary finger and no palm.
1560 */
1561 if (palm == 0) {
1562 if (fingers == 1) {
1563 synaptics_movement(sc, sp, sp->sp_finger, &dx, &dy);
1564 } else {
1565 /*
1566 * No valid finger. Therefore no movement.
1567 */
1568 sc->movement_history[sp->sp_finger] = 0;
1569 sc->rem_x[sp->sp_finger] = sc->rem_y[sp->sp_finger] = 0;
1570 dx = dy = 0;
1571 }
1572 } else {
1573 /*
1574 * No valid finger. Therefore no movement.
1575 */
1576 sc->movement_history[0] = 0;
1577 sc->rem_x[0] = sc->rem_y[0] = 0;
1578 dx = dy = 0;
1579 }
1580
1581 /*
1582 * Pass the final results up to wsmouse_input() if necessary.
1583 */
1584 if (dx || dy || dz || changed) {
1585 buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
1586 s = spltty();
1587 wsmouse_input(psc->sc_wsmousedev,
1588 buttons,
1589 dx, dy, dz, 0,
1590 WSMOUSE_INPUT_DELTA);
1591 splx(s);
1592 }
1593 }
1594