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