synaptics.c revision 1.75 1 /* $NetBSD: synaptics.c,v 1.75 2021/12/04 14:53:56 nia 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.75 2021/12/04 14:53:56 nia 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 signed short sp_sx; /* Unscaled absolute X/Y coordinates */
83 signed short sp_sy; /* for secondary finger */
84 u_char sp_sz; /* Z (pressure) */
85 u_char sp_w; /* W (contact patch width) */
86 u_char sp_primary; /* seen primary finger packet */
87 u_char sp_secondary; /* seen secondary finger packet */
88 u_char sp_finger_status; /* seen extended finger packet */
89 u_char sp_finger_count; /* number of fingers seen */
90 char sp_left; /* Left mouse button status */
91 char sp_right; /* Right mouse button status */
92 char sp_middle; /* Middle button status (possibly emulated) */
93 char sp_up; /* Up button status */
94 char sp_down; /* Down button status */
95 };
96
97 static void pms_synaptics_input(void *, int);
98 static void pms_synaptics_process_packet(struct pms_softc *,
99 struct synaptics_packet *);
100 static void pms_sysctl_synaptics(struct sysctllog **);
101 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
102
103 /* Controlled by sysctl. */
104 static int synaptics_up_down_emul = 3;
105 static int synaptics_up_down_motion_delta = 1;
106 static int synaptics_gesture_move = 200;
107 static int synaptics_gesture_length = 20;
108 static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
109 static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
110 static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
111 static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
112 static int synaptics_edge_motion_delta = 32;
113 static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
114 static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
115 static int synaptics_button_boundary = SYNAPTICS_EDGE_BOTTOM + 720;
116 static int synaptics_button2 = SYNAPTICS_EDGE_LEFT + (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
117 static int synaptics_button3 = SYNAPTICS_EDGE_LEFT + 2 * (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
118 static int synaptics_two_fingers_emul = 0;
119 static int synaptics_scale_x = 16;
120 static int synaptics_scale_y = 16;
121 static int synaptics_scale_z = 32;
122 static int synaptics_max_speed_x = 32;
123 static int synaptics_max_speed_y = 32;
124 static int synaptics_max_speed_z = 2;
125 static int synaptics_movement_threshold = 4;
126 static int synaptics_movement_enable = 1;
127 static int synaptics_button_region_movement = 1;
128 static bool synaptics_aux_mid_button_scroll = TRUE;
129 static int synaptics_debug = 0;
130
131 #define DPRINTF(LEVEL, SC, FMT, ARGS...) do \
132 { \
133 if (synaptics_debug >= LEVEL) { \
134 struct pms_softc *_dprintf_psc = \
135 container_of((SC), struct pms_softc, u.synaptics); \
136 device_printf(_dprintf_psc->sc_dev, FMT, ##ARGS); \
137 } \
138 } while (0)
139
140 /* Sysctl nodes. */
141 static int synaptics_button_boundary_nodenum;
142 static int synaptics_button2_nodenum;
143 static int synaptics_button3_nodenum;
144 static int synaptics_up_down_emul_nodenum;
145 static int synaptics_up_down_motion_delta_nodenum;
146 static int synaptics_gesture_move_nodenum;
147 static int synaptics_gesture_length_nodenum;
148 static int synaptics_edge_left_nodenum;
149 static int synaptics_edge_right_nodenum;
150 static int synaptics_edge_top_nodenum;
151 static int synaptics_edge_bottom_nodenum;
152 static int synaptics_edge_motion_delta_nodenum;
153 static int synaptics_finger_high_nodenum;
154 static int synaptics_finger_low_nodenum;
155 static int synaptics_two_fingers_emul_nodenum;
156 static int synaptics_scale_x_nodenum;
157 static int synaptics_scale_y_nodenum;
158 static int synaptics_scale_z_nodenum;
159 static int synaptics_max_speed_x_nodenum;
160 static int synaptics_max_speed_y_nodenum;
161 static int synaptics_max_speed_z_nodenum;
162 static int synaptics_movement_threshold_nodenum;
163 static int synaptics_movement_enable_nodenum;
164 static int synaptics_button_region_movement_nodenum;
165 static int synaptics_aux_mid_button_scroll_nodenum;
166
167 /*
168 * This holds the processed packet data, it is global because multiple
169 * packets from the trackpad may be processed when handling multiple
170 * fingers on the trackpad to gather all the data.
171 */
172 static struct synaptics_packet packet;
173
174 static int
175 synaptics_poll_cmd(struct pms_softc *psc, ...)
176 {
177 u_char cmd[4];
178 size_t i;
179 va_list ap;
180
181 va_start(ap, psc);
182
183 for (i = 0; i < __arraycount(cmd); i++)
184 if ((cmd[i] = (u_char)va_arg(ap, int)) == 0)
185 break;
186 va_end(ap);
187
188 int res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, i, 0,
189 NULL, 0);
190 if (res)
191 aprint_error_dev(psc->sc_dev, "command error %#x\n", cmd[0]);
192 return res;
193 }
194
195 static int
196 synaptics_poll_reset(struct pms_softc *psc)
197 {
198 u_char resp[2];
199 int res;
200
201 u_char cmd[1] = { PMS_RESET };
202 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
203 resp, 1);
204 aprint_debug_dev(psc->sc_dev, "reset %d 0x%02x 0x%02x\n",
205 res, resp[0], resp[1]);
206 return res;
207 }
208
209 static int
210 synaptics_special_read(struct pms_softc *psc, u_char slice, u_char resp[3])
211 {
212 u_char cmd[1] = { PMS_SEND_DEV_STATUS };
213 int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, slice);
214
215 return res | pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
216 cmd, 1, 3, resp, 0);
217 }
218
219 static int
220 synaptics_special_write(struct pms_softc *psc, u_char command, u_char arg)
221 {
222 int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, arg);
223 if (res)
224 return res;
225
226 u_char cmd[2];
227 cmd[0] = PMS_SET_SAMPLE;
228 cmd[1] = command;
229 res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
230 cmd, 2, 0, NULL, 0);
231 return res;
232 }
233
234 static void
235 pms_synaptics_probe_extended(struct pms_softc *psc)
236 {
237 struct synaptics_softc *sc = &psc->u.synaptics;
238 u_char resp[3];
239 int res;
240
241 aprint_debug_dev(psc->sc_dev,
242 "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
243 if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
244 sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
245
246 if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
247 sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
248
249 if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
250 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
251
252 if (sc->caps & SYNAPTICS_CAP_MULTIFINGERREPORT)
253 sc->flags |= SYN_FLAG_HAS_MULTI_FINGER_REPORT;
254
255 /* Ask about extra buttons to detect up/down. */
256 if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08)
257 >= SYNAPTICS_EXTENDED_QUERY)
258 {
259 res = synaptics_special_read(psc, SYNAPTICS_EXTENDED_QUERY, resp);
260 if (res == 0) {
261 int buttons = (resp[1] >> 4);
262 aprint_debug_dev(psc->sc_dev,
263 "%s: Extended Buttons: %d.\n", __func__, buttons);
264
265 aprint_debug_dev(psc->sc_dev, "%s: Extended "
266 "Capabilities: 0x%02x 0x%02x 0x%02x.\n", __func__,
267 resp[0], resp[1], resp[2]);
268 if (buttons >= 2) {
269 /* Yes. */
270 sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
271 }
272 if (resp[0] & 0x1) {
273 /* Vertical scroll area */
274 sc->flags |= SYN_FLAG_HAS_VERTICAL_SCROLL;
275 }
276 if (resp[0] & 0x2) {
277 /* Horizontal scroll area */
278 sc->flags |= SYN_FLAG_HAS_HORIZONTAL_SCROLL;
279 }
280 if (resp[0] & 0x4) {
281 /* Extended W-Mode */
282 sc->flags |= SYN_FLAG_HAS_EXTENDED_WMODE;
283 }
284 }
285 }
286
287 /* Ask about click pad */
288 if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) >=
289 SYNAPTICS_CONTINUED_CAPABILITIES)
290 {
291 res = synaptics_special_read(psc,
292 SYNAPTICS_CONTINUED_CAPABILITIES, resp);
293
294 /*
295 * The following describes response for the
296 * SYNAPTICS_CONTINUED_CAPABILITIES query.
297 *
298 * byte mask name meaning
299 * ---- ---- ------- ------------
300 * 0 0x01 adjustable threshold capacitive button sensitivity
301 * can be adjusted
302 * 0 0x02 report max query 0x0d gives max coord reported
303 * 0 0x04 clearpad sensor is ClearPad product
304 * 0 0x08 advanced gesture not particularly meaningful
305 * 0 0x10 clickpad bit 0 1-button ClickPad
306 * 0 0x60 multifinger mode identifies firmware finger counting
307 * (not reporting!) algorithm.
308 * Not particularly meaningful
309 * 0 0x80 covered pad W clipped to 14, 15 == pad mostly covered
310 * 1 0x01 clickpad bit 1 2-button ClickPad
311 * 1 0x02 deluxe LED controls touchpad support LED commands
312 * ala multimedia control bar
313 * 1 0x04 reduced filtering firmware does less filtering on
314 * position data, driver should watch
315 * for noise.
316 * 1 0x08 image sensor image sensor tracks 5 fingers, but only
317 * reports 2.
318 * 1 0x10 uniform clickpad whole clickpad moves instead of being
319 * hinged at the top.
320 * 1 0x20 report min query 0x0f gives min coord reported
321 */
322 if (res == 0) {
323 uint val = SYN_CCAP_VALUE(resp);
324
325 aprint_debug_dev(psc->sc_dev, "%s: Continued "
326 "Capabilities 0x%02x 0x%02x 0x%02x.\n", __func__,
327 resp[0], resp[1], resp[2]);
328 switch (SYN_CCAP_CLICKPAD_TYPE(val)) {
329 case 0: /* not a clickpad */
330 break;
331 case 1:
332 sc->flags |= SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD;
333 break;
334 case 2:
335 sc->flags |= SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD;
336 break;
337 case 3: /* reserved */
338 default:
339 /* unreached */
340 break;
341 }
342
343 if ((val & SYN_CCAP_HAS_ADV_GESTURE_MODE))
344 sc->flags |= SYN_FLAG_HAS_ADV_GESTURE_MODE;
345 }
346 }
347 }
348
349 static const struct {
350 int bit;
351 const char *desc;
352 } syn_flags[] = {
353 { SYN_FLAG_HAS_EXTENDED_WMODE, "Extended W mode", },
354 { SYN_FLAG_HAS_PASSTHROUGH, "Passthrough", },
355 { SYN_FLAG_HAS_MIDDLE_BUTTON, "Middle button", },
356 { SYN_FLAG_HAS_BUTTONS_4_5, "Buttons 4/5", },
357 { SYN_FLAG_HAS_UP_DOWN_BUTTONS, "Up/down buttons", },
358 { SYN_FLAG_HAS_PALM_DETECT, "Palm detect", },
359 { SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD, "One button click pad", },
360 { SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD, "Two button click pad", },
361 { SYN_FLAG_HAS_VERTICAL_SCROLL, "Vertical scroll", },
362 { SYN_FLAG_HAS_HORIZONTAL_SCROLL, "Horizontal scroll", },
363 { SYN_FLAG_HAS_MULTI_FINGER_REPORT, "Multi-finger Report", },
364 { SYN_FLAG_HAS_MULTI_FINGER, "Multi-finger", },
365 };
366
367 int
368 pms_synaptics_probe_init(void *vsc)
369 {
370 struct pms_softc *psc = vsc;
371 struct synaptics_softc *sc = &psc->u.synaptics;
372 u_char cmd[1], resp[3];
373 int res, ver_minor, ver_major;
374 struct sysctllog *clog = NULL;
375
376 res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
377 SYNAPTICS_IDENTIFY_TOUCHPAD);
378 cmd[0] = PMS_SEND_DEV_STATUS;
379 res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
380 resp, 0);
381 if (res) {
382 aprint_debug_dev(psc->sc_dev,
383 "synaptics_probe: Identify Touchpad error.\n");
384 /*
385 * Reset device in case the probe confused it.
386 */
387 doreset:
388 (void)synaptics_poll_reset(psc);
389 return res;
390 }
391
392 if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
393 aprint_debug_dev(psc->sc_dev,
394 "synaptics_probe: Not synaptics.\n");
395 res = 1;
396 goto doreset;
397 }
398
399 sc->flags = 0;
400
401 /* Check for minimum version and print a nice message. */
402 ver_major = resp[2] & 0x0f;
403 ver_minor = resp[0];
404 aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
405 ver_major, ver_minor);
406 if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
407 /* No capability query support. */
408 sc->caps = 0;
409 goto done;
410 }
411
412
413 /* Query the hardware capabilities. */
414 res = synaptics_special_read(psc, SYNAPTICS_READ_CAPABILITIES, resp);
415 if (res) {
416 /* Hmm, failed to get capabilities. */
417 aprint_error_dev(psc->sc_dev,
418 "synaptics_probe: Failed to query capabilities.\n");
419 goto doreset;
420 }
421
422 sc->caps = SYNAPTICS_CAP_VALUE(resp);
423
424 if (sc->caps & SYNAPTICS_CAP_MBUTTON)
425 sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
426
427 if (sc->caps & SYNAPTICS_CAP_4BUTTON)
428 sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
429
430 if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
431 pms_synaptics_probe_extended(psc);
432 }
433
434 if (sc->flags) {
435 const char comma[] = ", ";
436 const char *sep = "";
437 aprint_normal_dev(psc->sc_dev, "");
438 for (size_t f = 0; f < __arraycount(syn_flags); f++) {
439 if (sc->flags & syn_flags[f].bit) {
440 aprint_normal("%s%s", sep, syn_flags[f].desc);
441 sep = comma;
442 }
443 }
444 aprint_normal("\n");
445 }
446
447 done:
448 pms_sysctl_synaptics(&clog);
449 pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
450 pms_synaptics_input, psc, device_xname(psc->sc_dev));
451
452 return (0);
453 }
454
455 void
456 pms_synaptics_enable(void *vsc)
457 {
458 struct pms_softc *psc = vsc;
459 struct synaptics_softc *sc = &psc->u.synaptics;
460 u_char enable_modes;
461 int res, i;
462
463 if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
464 /*
465 * Extended capability probes can confuse the passthrough
466 * device; reset the touchpad now to cure that.
467 */
468 res = synaptics_poll_reset(psc);
469 }
470
471 /*
472 * Enable Absolute mode with W (width) reporting, and set
473 * the packet rate to maximum (80 packets per second). Enable
474 * extended W mode if supported so we can report second finger
475 * position.
476 */
477 enable_modes =
478 SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE;
479
480 if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
481 enable_modes |= SYNAPTICS_MODE_EXTENDED_W;
482
483 /*
484 * Synaptics documentation says to disable device before
485 * setting mode.
486 */
487 synaptics_poll_cmd(psc, PMS_DEV_DISABLE, 0);
488 /* a couple of set scales to clear out pending commands */
489 for (i = 0; i < 2; i++)
490 synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
491
492 res = synaptics_special_write(psc, SYNAPTICS_CMD_SET_MODE2, enable_modes);
493 if (res)
494 aprint_error("synaptics: set mode error\n");
495
496 /* a couple of set scales to clear out pending commands */
497 for (i = 0; i < 2; i++)
498 synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
499
500 /* Set advanced gesture mode */
501 if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) ||
502 (sc->flags & SYN_FLAG_HAS_ADV_GESTURE_MODE))
503 synaptics_special_write(psc, SYNAPTICS_WRITE_DELUXE_3, 0x3);
504
505 /* Disable motion in the button region for clickpads */
506 if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD)
507 synaptics_button_region_movement = 0;
508
509 sc->up_down = 0;
510 sc->prev_fingers = 0;
511 sc->gesture_start_x = sc->gesture_start_y = 0;
512 sc->gesture_start_packet = 0;
513 sc->gesture_tap_packet = 0;
514 sc->gesture_type = 0;
515 sc->gesture_buttons = 0;
516 sc->total_packets = 0;
517 for (i = 0; i < SYN_MAX_FINGERS; i++) {
518 sc->rem_x[i] = sc->rem_y[i] = sc->rem_z[i] = 0;
519 }
520 sc->button_history = 0;
521
522 /* clear the packet decode structure */
523 memset(&packet, 0, sizeof(packet));
524 }
525
526 void
527 pms_synaptics_resume(void *vsc)
528 {
529 (void)synaptics_poll_reset(vsc);
530 }
531
532 static void
533 pms_sysctl_synaptics(struct sysctllog **clog)
534 {
535 int rc, root_num;
536 const struct sysctlnode *node;
537
538 if ((rc = sysctl_createv(clog, 0, NULL, &node,
539 CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
540 SYSCTL_DESCR("Synaptics touchpad controls"),
541 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
542 goto err;
543
544 root_num = node->sysctl_num;
545
546 if ((rc = sysctl_createv(clog, 0, NULL, &node,
547 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
548 CTLTYPE_INT, "up_down_emulation",
549 SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
550 pms_sysctl_synaptics_verify, 0,
551 &synaptics_up_down_emul,
552 0, CTL_HW, root_num, CTL_CREATE,
553 CTL_EOL)) != 0)
554 goto err;
555
556 synaptics_up_down_emul_nodenum = node->sysctl_num;
557
558 if ((rc = sysctl_createv(clog, 0, NULL, &node,
559 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
560 CTLTYPE_INT, "up_down_motion_delta",
561 SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
562 pms_sysctl_synaptics_verify, 0,
563 &synaptics_up_down_motion_delta,
564 0, CTL_HW, root_num, CTL_CREATE,
565 CTL_EOL)) != 0)
566 goto err;
567
568 synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
569
570 if ((rc = sysctl_createv(clog, 0, NULL, &node,
571 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
572 CTLTYPE_INT, "gesture_move",
573 SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
574 pms_sysctl_synaptics_verify, 0,
575 &synaptics_gesture_move,
576 0, CTL_HW, root_num, CTL_CREATE,
577 CTL_EOL)) != 0)
578 goto err;
579
580 synaptics_gesture_move_nodenum = node->sysctl_num;
581
582 if ((rc = sysctl_createv(clog, 0, NULL, &node,
583 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
584 CTLTYPE_INT, "gesture_length",
585 SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
586 pms_sysctl_synaptics_verify, 0,
587 &synaptics_gesture_length,
588 0, CTL_HW, root_num, CTL_CREATE,
589 CTL_EOL)) != 0)
590 goto err;
591
592 synaptics_gesture_length_nodenum = node->sysctl_num;
593
594 if ((rc = sysctl_createv(clog, 0, NULL, &node,
595 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
596 CTLTYPE_INT, "edge_left",
597 SYSCTL_DESCR("Define left edge of touchpad"),
598 pms_sysctl_synaptics_verify, 0,
599 &synaptics_edge_left,
600 0, CTL_HW, root_num, CTL_CREATE,
601 CTL_EOL)) != 0)
602 goto err;
603
604 synaptics_edge_left_nodenum = node->sysctl_num;
605
606 if ((rc = sysctl_createv(clog, 0, NULL, &node,
607 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
608 CTLTYPE_INT, "edge_right",
609 SYSCTL_DESCR("Define right edge of touchpad"),
610 pms_sysctl_synaptics_verify, 0,
611 &synaptics_edge_right,
612 0, CTL_HW, root_num, CTL_CREATE,
613 CTL_EOL)) != 0)
614 goto err;
615
616 synaptics_edge_right_nodenum = node->sysctl_num;
617
618 if ((rc = sysctl_createv(clog, 0, NULL, &node,
619 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
620 CTLTYPE_INT, "edge_top",
621 SYSCTL_DESCR("Define top edge of touchpad"),
622 pms_sysctl_synaptics_verify, 0,
623 &synaptics_edge_top,
624 0, CTL_HW, root_num, CTL_CREATE,
625 CTL_EOL)) != 0)
626 goto err;
627
628 synaptics_edge_top_nodenum = node->sysctl_num;
629
630 if ((rc = sysctl_createv(clog, 0, NULL, &node,
631 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
632 CTLTYPE_INT, "edge_bottom",
633 SYSCTL_DESCR("Define bottom edge of touchpad"),
634 pms_sysctl_synaptics_verify, 0,
635 &synaptics_edge_bottom,
636 0, CTL_HW, root_num, CTL_CREATE,
637 CTL_EOL)) != 0)
638 goto err;
639
640 synaptics_edge_bottom_nodenum = node->sysctl_num;
641
642 if ((rc = sysctl_createv(clog, 0, NULL, &node,
643 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
644 CTLTYPE_INT, "edge_motion_delta",
645 SYSCTL_DESCR("Define edge motion rate"),
646 pms_sysctl_synaptics_verify, 0,
647 &synaptics_edge_motion_delta,
648 0, CTL_HW, root_num, CTL_CREATE,
649 CTL_EOL)) != 0)
650 goto err;
651
652 synaptics_edge_motion_delta_nodenum = node->sysctl_num;
653
654 if ((rc = sysctl_createv(clog, 0, NULL, &node,
655 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
656 CTLTYPE_INT, "finger_high",
657 SYSCTL_DESCR("Define finger applied pressure threshold"),
658 pms_sysctl_synaptics_verify, 0,
659 &synaptics_finger_high,
660 0, CTL_HW, root_num, CTL_CREATE,
661 CTL_EOL)) != 0)
662 goto err;
663
664 synaptics_finger_high_nodenum = node->sysctl_num;
665
666 if ((rc = sysctl_createv(clog, 0, NULL, &node,
667 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
668 CTLTYPE_INT, "finger_low",
669 SYSCTL_DESCR("Define finger removed pressure threshold"),
670 pms_sysctl_synaptics_verify, 0,
671 &synaptics_finger_low,
672 0, CTL_HW, root_num, CTL_CREATE,
673 CTL_EOL)) != 0)
674 goto err;
675
676 synaptics_finger_low_nodenum = node->sysctl_num;
677
678 if ((rc = sysctl_createv(clog, 0, NULL, &node,
679 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
680 CTLTYPE_INT, "two_fingers_emulation",
681 SYSCTL_DESCR("Map two fingers to middle button"),
682 pms_sysctl_synaptics_verify, 0,
683 &synaptics_two_fingers_emul,
684 0, CTL_HW, root_num, CTL_CREATE,
685 CTL_EOL)) != 0)
686 goto err;
687
688 synaptics_two_fingers_emul_nodenum = node->sysctl_num;
689
690 if ((rc = sysctl_createv(clog, 0, NULL, &node,
691 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
692 CTLTYPE_INT, "scale_x",
693 SYSCTL_DESCR("Horizontal movement scale factor"),
694 pms_sysctl_synaptics_verify, 0,
695 &synaptics_scale_x,
696 0, CTL_HW, root_num, CTL_CREATE,
697 CTL_EOL)) != 0)
698 goto err;
699
700 synaptics_scale_x_nodenum = node->sysctl_num;
701
702 if ((rc = sysctl_createv(clog, 0, NULL, &node,
703 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
704 CTLTYPE_INT, "scale_y",
705 SYSCTL_DESCR("Vertical movement scale factor"),
706 pms_sysctl_synaptics_verify, 0,
707 &synaptics_scale_y,
708 0, CTL_HW, root_num, CTL_CREATE,
709 CTL_EOL)) != 0)
710 goto err;
711
712 synaptics_scale_y_nodenum = node->sysctl_num;
713
714 if ((rc = sysctl_createv(clog, 0, NULL, &node,
715 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
716 CTLTYPE_INT, "scale_z",
717 SYSCTL_DESCR("Sroll wheel emulation scale factor"),
718 pms_sysctl_synaptics_verify, 0,
719 &synaptics_scale_z,
720 0, CTL_HW, root_num, CTL_CREATE,
721 CTL_EOL)) != 0)
722 goto err;
723
724 synaptics_scale_z_nodenum = node->sysctl_num;
725
726 if ((rc = sysctl_createv(clog, 0, NULL, &node,
727 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
728 CTLTYPE_INT, "max_speed_x",
729 SYSCTL_DESCR("Horizontal movement maximum speed"),
730 pms_sysctl_synaptics_verify, 0,
731 &synaptics_max_speed_x,
732 0, CTL_HW, root_num, CTL_CREATE,
733 CTL_EOL)) != 0)
734 goto err;
735
736 synaptics_max_speed_x_nodenum = node->sysctl_num;
737
738 if ((rc = sysctl_createv(clog, 0, NULL, &node,
739 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
740 CTLTYPE_INT, "max_speed_y",
741 SYSCTL_DESCR("Vertical movement maximum speed"),
742 pms_sysctl_synaptics_verify, 0,
743 &synaptics_max_speed_y,
744 0, CTL_HW, root_num, CTL_CREATE,
745 CTL_EOL)) != 0)
746 goto err;
747
748 synaptics_max_speed_y_nodenum = node->sysctl_num;
749
750 if ((rc = sysctl_createv(clog, 0, NULL, &node,
751 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
752 CTLTYPE_INT, "max_speed_z",
753 SYSCTL_DESCR("Scroll wheel emulation maximum speed"),
754 pms_sysctl_synaptics_verify, 0,
755 &synaptics_max_speed_z,
756 0, CTL_HW, root_num, CTL_CREATE,
757 CTL_EOL)) != 0)
758 goto err;
759
760 synaptics_max_speed_z_nodenum = node->sysctl_num;
761
762 if ((rc = sysctl_createv(clog, 0, NULL, &node,
763 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
764 CTLTYPE_INT, "movement_threshold",
765 SYSCTL_DESCR("Minimum reported movement threshold"),
766 pms_sysctl_synaptics_verify, 0,
767 &synaptics_movement_threshold,
768 0, CTL_HW, root_num, CTL_CREATE,
769 CTL_EOL)) != 0)
770 goto err;
771
772 synaptics_movement_threshold_nodenum = node->sysctl_num;
773
774 if ((rc = sysctl_createv(clog, 0, NULL, &node,
775 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
776 CTLTYPE_INT, "movement_enable",
777 SYSCTL_DESCR("Enable movement reporting"),
778 pms_sysctl_synaptics_verify, 0,
779 &synaptics_movement_enable,
780 0, CTL_HW, root_num, CTL_CREATE,
781 CTL_EOL)) != 0)
782 goto err;
783
784 synaptics_movement_enable_nodenum = node->sysctl_num;
785
786 if ((rc = sysctl_createv(clog, 0, NULL, &node,
787 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
788 CTLTYPE_INT, "button_region_movement_enable",
789 SYSCTL_DESCR("Enable movement within clickpad button region"),
790 pms_sysctl_synaptics_verify, 0,
791 &synaptics_button_region_movement,
792 0, CTL_HW, root_num, CTL_CREATE,
793 CTL_EOL)) != 0)
794 goto err;
795
796 synaptics_button_region_movement_nodenum = node->sysctl_num;
797
798 if ((rc = sysctl_createv(clog, 0, NULL, &node,
799 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
800 CTLTYPE_INT, "button_boundary",
801 SYSCTL_DESCR("Top edge of button area"),
802 pms_sysctl_synaptics_verify, 0,
803 &synaptics_button_boundary,
804 0, CTL_HW, root_num, CTL_CREATE,
805 CTL_EOL)) != 0)
806 goto err;
807
808 synaptics_button_boundary_nodenum = node->sysctl_num;
809
810 if ((rc = sysctl_createv(clog, 0, NULL, &node,
811 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
812 CTLTYPE_INT, "button2_edge",
813 SYSCTL_DESCR("Left edge of button 2 region"),
814 pms_sysctl_synaptics_verify, 0,
815 &synaptics_button2,
816 0, CTL_HW, root_num, CTL_CREATE,
817 CTL_EOL)) != 0)
818 goto err;
819
820 synaptics_button2_nodenum = node->sysctl_num;
821
822 if ((rc = sysctl_createv(clog, 0, NULL, &node,
823 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
824 CTLTYPE_INT, "button3_edge",
825 SYSCTL_DESCR("Left edge of button 3 region"),
826 pms_sysctl_synaptics_verify, 0,
827 &synaptics_button3,
828 0, CTL_HW, root_num, CTL_CREATE,
829 CTL_EOL)) != 0)
830 goto err;
831
832 synaptics_button3_nodenum = node->sysctl_num;
833
834 if ((rc = sysctl_createv(clog, 0, NULL, &node,
835 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
836 CTLTYPE_BOOL, "aux_mid_button_scroll",
837 SYSCTL_DESCR("Interpet Y-Axis movement with the middle button held as scrolling on the passthrough device (e.g. TrackPoint)"),
838 pms_sysctl_synaptics_verify, 0,
839 &synaptics_aux_mid_button_scroll,
840 0, CTL_HW, root_num, CTL_CREATE,
841 CTL_EOL)) != 0)
842 goto err;
843
844 synaptics_aux_mid_button_scroll_nodenum = node->sysctl_num;
845
846 if ((rc = sysctl_createv(clog, 0, NULL, &node,
847 CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
848 CTLTYPE_INT, "debug",
849 SYSCTL_DESCR("Enable debug output"),
850 NULL, 0,
851 &synaptics_debug,
852 0, CTL_HW, root_num, CTL_CREATE,
853 CTL_EOL)) != 0)
854 goto err;
855
856 return;
857
858 err:
859 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
860 }
861
862 static int
863 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
864 {
865 int error, t;
866 struct sysctlnode node;
867
868 node = *rnode;
869 t = *(int *)rnode->sysctl_data;
870 node.sysctl_data = &t;
871 error = sysctl_lookup(SYSCTLFN_CALL(&node));
872 if (error || newp == NULL)
873 return error;
874
875 /* Sanity check the params. */
876 if (node.sysctl_num == synaptics_up_down_emul_nodenum) {
877 if (t < 0 || t > 3)
878 return (EINVAL);
879 } else
880 if (node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
881 if (t < 0 || t > 2)
882 return (EINVAL);
883 } else
884 if (node.sysctl_num == synaptics_gesture_length_nodenum ||
885 node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
886 node.sysctl_num == synaptics_up_down_motion_delta_nodenum ||
887 node.sysctl_num == synaptics_max_speed_x_nodenum ||
888 node.sysctl_num == synaptics_max_speed_y_nodenum ||
889 node.sysctl_num == synaptics_max_speed_z_nodenum) {
890 if (t < 0)
891 return (EINVAL);
892 } else
893 if (node.sysctl_num == synaptics_edge_left_nodenum ||
894 node.sysctl_num == synaptics_edge_bottom_nodenum) {
895 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
896 return (EINVAL);
897 } else
898 if (node.sysctl_num == synaptics_edge_right_nodenum ||
899 node.sysctl_num == synaptics_edge_top_nodenum) {
900 if (t < (SYNAPTICS_EDGE_MAX / 2))
901 return (EINVAL);
902 } else
903 if (node.sysctl_num == synaptics_scale_x_nodenum ||
904 node.sysctl_num == synaptics_scale_y_nodenum ||
905 node.sysctl_num == synaptics_scale_z_nodenum) {
906 if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
907 return (EINVAL);
908 } else
909 if (node.sysctl_num == synaptics_finger_high_nodenum) {
910 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
911 t < synaptics_finger_low)
912 return (EINVAL);
913 } else
914 if (node.sysctl_num == synaptics_finger_low_nodenum) {
915 if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
916 t > synaptics_finger_high)
917 return (EINVAL);
918 } else
919 if (node.sysctl_num == synaptics_gesture_move_nodenum ||
920 node.sysctl_num == synaptics_movement_threshold_nodenum) {
921 if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
922 return (EINVAL);
923 } else
924 if (node.sysctl_num == synaptics_button_boundary_nodenum) {
925 if (t < 0 || t < SYNAPTICS_EDGE_BOTTOM ||
926 t > SYNAPTICS_EDGE_TOP)
927 return (EINVAL);
928 } else
929 if (node.sysctl_num == synaptics_button2_nodenum ||
930 node.sysctl_num == synaptics_button3_nodenum) {
931 if (t < SYNAPTICS_EDGE_LEFT || t > SYNAPTICS_EDGE_RIGHT)
932 return (EINVAL);
933 } else
934 if (node.sysctl_num == synaptics_movement_enable_nodenum) {
935 if (t < 0 || t > 1)
936 return (EINVAL);
937 } else
938 if (node.sysctl_num == synaptics_button_region_movement_nodenum) {
939 if (t < 0 || t > 1)
940 return (EINVAL);
941 } else
942 if (node.sysctl_num == synaptics_aux_mid_button_scroll_nodenum) {
943 if (t < 0 || t > 1)
944 return (EINVAL);
945 } else
946 return (EINVAL);
947
948 *(int *)rnode->sysctl_data = t;
949
950 return (0);
951 }
952
953 /*
954 * Extract the number of fingers from the current packet and return
955 * it to the caller.
956 */
957 static unsigned
958 pms_synaptics_get_fingers(struct pms_softc *psc, u_char w, short z)
959 {
960 struct synaptics_softc *sc = &psc->u.synaptics;
961 unsigned short ew_mode;
962 unsigned fingers;
963
964 fingers = 0;
965
966
967 /*
968 * If w is zero and z says no fingers then return
969 * no fingers, w == can also mean 2 fingers... confusing.
970 */
971 if (w == 0 && z == SYNAPTICS_FINGER_NONE)
972 return 0;
973
974 if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) &&
975 (w == SYNAPTICS_WIDTH_EXTENDED_W)) {
976 ew_mode = psc->packet[5] >> 4;
977 switch (ew_mode)
978 {
979 case SYNAPTICS_EW_WHEEL:
980 break;
981
982 case SYNAPTICS_EW_SECONDARY_FINGER:
983 /* to get here we must have 2 fingers at least */
984 fingers = 2;
985 break;
986
987 case SYNAPTICS_EW_FINGER_STATUS:
988 fingers = psc->packet[1] & 0x0f;
989 break;
990
991 default:
992 aprint_error_dev(psc->sc_dev,
993 "invalid extended w mode %d\n",
994 ew_mode);
995 return 0; /* pretend there are no fingers */
996 }
997 } else {
998
999 fingers = 1;
1000
1001 /*
1002 * If SYN_FLAG_HAS_MULTI_FINGER is set then check
1003 * sp_w is below SYNAPTICS_WIDTH_FINGER_MIN, if it is
1004 * then this will be the finger count.
1005 *
1006 * There are a couple of "special" values otherwise
1007 * just punt with one finger, if this really is a palm
1008 * then it will be caught later.
1009 */
1010 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER) {
1011 if (w == SYNAPTICS_WIDTH_TWO_FINGERS)
1012 fingers = 2;
1013 else if (w == SYNAPTICS_WIDTH_THREE_OR_MORE)
1014 fingers = 3;
1015 }
1016
1017 }
1018
1019 return fingers;
1020 }
1021
1022 /* Masks for the first byte of a packet */
1023 #define PMS_LBUTMASK 0x01
1024 #define PMS_RBUTMASK 0x02
1025 #define PMS_MBUTMASK 0x04
1026
1027 static void
1028 pms_synaptics_parse(struct pms_softc *psc)
1029 {
1030 struct synaptics_softc *sc = &psc->u.synaptics;
1031 struct synaptics_packet nsp;
1032 char new_buttons, ew_mode;
1033 unsigned v, primary_finger, secondary_finger;
1034 int ext_left = -1, ext_right = -1, ext_middle = -1,
1035 ext_up = -1, ext_down = -1;
1036
1037 sc->total_packets++;
1038
1039 memcpy(&nsp, &packet, sizeof(packet));
1040
1041 /* Width of finger */
1042 nsp.sp_w = ((psc->packet[0] & 0x30) >> 2)
1043 + ((psc->packet[0] & 0x04) >> 1)
1044 + ((psc->packet[3] & 0x04) >> 2);
1045
1046 v = 0;
1047 primary_finger = 0;
1048 secondary_finger = 0;
1049 if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) &&
1050 (nsp.sp_w == SYNAPTICS_WIDTH_EXTENDED_W)) {
1051 ew_mode = psc->packet[5] >> 4;
1052 switch (ew_mode)
1053 {
1054 case SYNAPTICS_EW_WHEEL:
1055 /* scroll wheel report, ignore for now */
1056 aprint_debug_dev(psc->sc_dev, "mouse wheel packet\n");
1057 return;
1058
1059 case SYNAPTICS_EW_SECONDARY_FINGER:
1060 /* parse the second finger report */
1061
1062 nsp.sp_secondary = 1;
1063
1064 nsp.sp_sx = ((psc->packet[1] & 0xfe) << 1)
1065 + ((psc->packet[4] & 0x0f) << 9);
1066 nsp.sp_sy = ((psc->packet[2] & 0xfe) << 1)
1067 + ((psc->packet[4] & 0xf0) << 5);
1068 nsp.sp_sz = (psc->packet[3] & 0x30)
1069 + ((psc->packet[5] & 0x0e) << 1);
1070
1071 /* work out the virtual finger width */
1072 v = 8 + (psc->packet[1] & 0x01) +
1073 ((psc->packet[2] & 0x01) << 1) +
1074 ((psc->packet[5] & 0x01) << 2);
1075
1076 /* keep same buttons down as primary */
1077 nsp.sp_left = sc->button_history & PMS_LBUTMASK;
1078 nsp.sp_middle = sc->button_history & PMS_MBUTMASK;
1079 nsp.sp_right = sc->button_history & PMS_RBUTMASK;
1080 break;
1081
1082 case SYNAPTICS_EW_FINGER_STATUS:
1083 /* This works but what is it good for?
1084 * it gives us an index of the primary/secondary
1085 * fingers but no other packets pass this
1086 * index.
1087 *
1088 * XXX Park this, possibly handle a finger
1089 * XXX change if indexes change.
1090 */
1091 primary_finger = psc->packet[2];
1092 secondary_finger = psc->packet[4];
1093 nsp.sp_finger_status = 1;
1094 nsp.sp_finger_count = pms_synaptics_get_fingers(psc,
1095 nsp.sp_w, nsp.sp_z);
1096 goto skip_position;
1097
1098 default:
1099 aprint_error_dev(psc->sc_dev,
1100 "invalid extended w mode %d\n",
1101 ew_mode);
1102 return;
1103 }
1104 } else {
1105 nsp.sp_primary = 1;
1106
1107 /*
1108 * If SYN_FLAG_HAS_MULTI_FINGER is set then check
1109 * sp_w is below SYNAPTICS_WIDTH_FINGER_MIN, if it is
1110 * then this will be the finger count.
1111 *
1112 * There are a couple of "special" values otherwise
1113 * just punt with one finger, if this really is a palm
1114 * then it will be caught later.
1115 */
1116 if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER) {
1117 /*
1118 * To make life interesting if there are
1119 * two or more fingers on the touchpad then
1120 * the coordinate reporting changes and an extra
1121 * "virtual" finger width is reported.
1122 */
1123 nsp.sp_x = (psc->packet[4] & 0xfc) +
1124 ((psc->packet[4] & 0x02) << 1) +
1125 ((psc->packet[1] & 0x0f) << 8) +
1126 ((psc->packet[3] & 0x10) << 8);
1127
1128 nsp.sp_y = (psc->packet[5] & 0xfc) +
1129 ((psc->packet[5] & 0x02) << 1) +
1130 ((psc->packet[1] & 0xf0) << 4) +
1131 ((psc->packet[3] & 0x20) << 7);
1132
1133 /* Pressure */
1134 nsp.sp_z = psc->packet[2] & 0xfe;
1135
1136 /* derive the virtual finger width */
1137 v = 8 + ((psc->packet[4] & 0x02) >> 1) +
1138 (psc->packet[5] & 0x02) +
1139 ((psc->packet[2] & 0x01) << 2);
1140
1141 } else {
1142 /* Absolute X/Y coordinates of finger */
1143 nsp.sp_x = psc->packet[4] +
1144 ((psc->packet[1] & 0x0f) << 8) +
1145 ((psc->packet[3] & 0x10) << 8);
1146
1147 nsp.sp_y = psc->packet[5] +
1148 ((psc->packet[1] & 0xf0) << 4) +
1149 ((psc->packet[3] & 0x20) << 7);
1150
1151 /* Pressure */
1152 nsp.sp_z = psc->packet[2];
1153 }
1154
1155 nsp.sp_finger_count = pms_synaptics_get_fingers(psc,
1156 nsp.sp_w, nsp.sp_z);
1157
1158 /*
1159 * We don't have extended W so we only know if there
1160 * are multiple fingers on the touchpad, only the primary
1161 * location is reported so just pretend we have an
1162 * unmoving second finger.
1163 */
1164 if (((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
1165 != SYN_FLAG_HAS_EXTENDED_WMODE) &&
1166 (nsp.sp_finger_count > 1)) {
1167 nsp.sp_secondary = 1;
1168 nsp.sp_sx = 0;
1169 nsp.sp_sy = 0;
1170 nsp.sp_sz = 0;
1171 }
1172
1173 if ((psc->packet[0] ^ psc->packet[3]) & 0x02) {
1174 /* extended buttons */
1175
1176 aprint_debug_dev(psc->sc_dev,
1177 "synaptics_parse: %02x %02x %02x %02x %02x %02x\n",
1178 psc->packet[0], psc->packet[1], psc->packet[2],
1179 psc->packet[3], psc->packet[4], psc->packet[5]);
1180
1181 if ((psc->packet[4] & SYN_1BUTMASK) != 0)
1182 ext_left = PMS_LBUTMASK;
1183 else
1184 ext_left = 0;
1185
1186 if ((psc->packet[4] & SYN_3BUTMASK) != 0)
1187 ext_middle = PMS_MBUTMASK;
1188 else
1189 ext_middle = 0;
1190
1191 if ((psc->packet[5] & SYN_2BUTMASK) != 0)
1192 ext_right = PMS_RBUTMASK;
1193 else
1194 ext_right = 0;
1195
1196 if ((psc->packet[5] & SYN_4BUTMASK) != 0)
1197 ext_up = 1;
1198 else
1199 ext_up = 0;
1200
1201 if ((psc->packet[4] & SYN_5BUTMASK) != 0)
1202 ext_down = 1;
1203 else
1204 ext_down = 0;
1205 } else {
1206 /* Left/Right button handling. */
1207 nsp.sp_left = psc->packet[0] & PMS_LBUTMASK;
1208 nsp.sp_right = psc->packet[0] & PMS_RBUTMASK;
1209 }
1210
1211 /* Up/Down buttons. */
1212 if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
1213 /* Old up/down buttons. */
1214 nsp.sp_up = nsp.sp_left ^
1215 (psc->packet[3] & PMS_LBUTMASK);
1216 nsp.sp_down = nsp.sp_right ^
1217 (psc->packet[3] & PMS_RBUTMASK);
1218 } else if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
1219 ((psc->packet[0] & PMS_RBUTMASK) ^
1220 (psc->packet[3] & PMS_RBUTMASK))) {
1221 /* New up/down button. */
1222 nsp.sp_up = psc->packet[4] & SYN_1BUTMASK;
1223 nsp.sp_down = psc->packet[5] & SYN_2BUTMASK;
1224 } else {
1225 nsp.sp_up = 0;
1226 nsp.sp_down = 0;
1227 }
1228
1229 new_buttons = 0;
1230 if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
1231 /* This is not correctly specified. Read this button press
1232 * from L/U bit. Emulate 3 buttons by checking the
1233 * coordinates of the click and returning the appropriate
1234 * button code. Outside the button region default to a
1235 * left click.
1236 */
1237 u_char bstate = (psc->packet[0] ^ psc->packet[3])
1238 & 0x01;
1239 if (nsp.sp_y < synaptics_button_boundary) {
1240 if (nsp.sp_x > synaptics_button3) {
1241 nsp.sp_right =
1242 bstate ? PMS_RBUTMASK : 0;
1243 } else if (nsp.sp_x > synaptics_button2) {
1244 nsp.sp_middle =
1245 bstate ? PMS_MBUTMASK : 0;
1246 } else {
1247 nsp.sp_left = bstate ? PMS_LBUTMASK : 0;
1248 }
1249 } else
1250 nsp.sp_left = bstate ? 1 : 0;
1251 new_buttons = nsp.sp_left | nsp.sp_middle | nsp.sp_right;
1252 if (new_buttons != sc->button_history) {
1253 if (sc->button_history == 0)
1254 sc->button_history = new_buttons;
1255 else if (new_buttons == 0) {
1256 sc->button_history = 0;
1257 /* ensure all buttons are cleared just in
1258 * case finger comes off in a different
1259 * region.
1260 */
1261 nsp.sp_left = 0;
1262 nsp.sp_middle = 0;
1263 nsp.sp_right = 0;
1264 } else {
1265 /* make sure we keep the same button even
1266 * if the finger moves to a different
1267 * region. This precludes chording
1268 * but, oh well.
1269 */
1270 nsp.sp_left = sc->button_history & PMS_LBUTMASK;
1271 nsp.sp_middle = sc->button_history
1272 & PMS_MBUTMASK;
1273 nsp.sp_right = sc->button_history & PMS_RBUTMASK;
1274 }
1275 }
1276 } else if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
1277 /* Old style Middle Button. */
1278 nsp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
1279 (psc->packet[3] & PMS_LBUTMASK);
1280 } else {
1281 nsp.sp_middle = 0;
1282 }
1283
1284 /*
1285 * Overlay extended button state if anything changed,
1286 * preserve the state if a button is being held.
1287 */
1288 if (ext_left != -1)
1289 nsp.sp_left = sc->ext_left = ext_left;
1290 else if (sc->ext_left != 0)
1291 nsp.sp_left = sc->ext_left;
1292
1293 if (ext_right != -1)
1294 nsp.sp_right = sc->ext_right = ext_right;
1295 else if (sc->ext_right != 0)
1296 nsp.sp_right = sc->ext_right;
1297
1298 if (ext_middle != -1)
1299 nsp.sp_middle = sc->ext_middle = ext_middle;
1300 else if (sc->ext_middle != 0)
1301 nsp.sp_middle = sc->ext_middle;
1302
1303 if (ext_up != -1)
1304 nsp.sp_up = sc->ext_up = ext_up;
1305 else if (sc->ext_up != 0)
1306 nsp.sp_up = sc->ext_up;
1307
1308 if (ext_down != -1)
1309 nsp.sp_down = sc->ext_down = ext_down;
1310 else if (sc->ext_down != 0)
1311 nsp.sp_down = sc->ext_down;
1312
1313 switch (synaptics_up_down_emul) {
1314 case 1:
1315 /* Do middle button emulation using up/down buttons */
1316 nsp.sp_middle = nsp.sp_up | nsp.sp_down;
1317 nsp.sp_up = nsp.sp_down = 0;
1318 break;
1319 case 3:
1320 /* Do left/right button emulation using up/down buttons */
1321 nsp.sp_left = nsp.sp_left | nsp.sp_up;
1322 nsp.sp_right = nsp.sp_right | nsp.sp_down;
1323 nsp.sp_up = nsp.sp_down = 0;
1324 break;
1325 default:
1326 /*
1327 * Don't do any remapping...
1328 * Z-axis emulation is handled in pms_synaptics_process_packet
1329 */
1330 break;
1331 }
1332 }
1333
1334 /* set the finger count only if we haven't seen an extended-w
1335 * finger count status
1336 */
1337 if (nsp.sp_finger_status == 0)
1338 nsp.sp_finger_count = pms_synaptics_get_fingers(psc, nsp.sp_w,
1339 nsp.sp_z);
1340
1341 skip_position:
1342 DPRINTF(20, sc,
1343 "synaptics_parse: sp_x %d sp_y %d sp_z %d, sp_sx %d, sp_sy %d, "
1344 "sp_sz %d, sp_w %d sp_finger_count %d, sp_primary %d, "
1345 "sp_secondary %d, v %d, primary_finger %d, secondary_finger %d\n",
1346 nsp.sp_x, nsp.sp_y, nsp.sp_z, nsp.sp_sx,
1347 nsp.sp_sy, nsp.sp_sz, nsp.sp_w, nsp.sp_finger_count,
1348 nsp.sp_primary, nsp.sp_secondary, v, primary_finger,
1349 secondary_finger);
1350
1351
1352 /* If no fingers and we at least saw the primary finger
1353 * or the buttons changed then process the last packet.
1354 */
1355 if (pms_synaptics_get_fingers(psc, nsp.sp_w, nsp.sp_z) == 0 ||
1356 nsp.sp_left != packet.sp_left ||
1357 nsp.sp_right != packet.sp_right ||
1358 nsp.sp_middle != packet.sp_middle ||
1359 nsp.sp_up != packet.sp_up ||
1360 nsp.sp_down != packet.sp_down) {
1361 if (nsp.sp_primary == 1) {
1362 pms_synaptics_process_packet(psc, &nsp);
1363 sc->packet_count[SYN_PRIMARY_FINGER] = 0;
1364 sc->packet_count[SYN_SECONDARY_FINGER] = 0;
1365 }
1366
1367 /* clear the fingers seen since we have processed */
1368 nsp.sp_primary = 0;
1369 nsp.sp_secondary = 0;
1370 nsp.sp_finger_status = 0;
1371 } else if (nsp.sp_finger_count != packet.sp_finger_count) {
1372 /*
1373 * If the number of fingers changes then send the current packet
1374 * for processing and restart the process.
1375 */
1376 if (packet.sp_primary == 1) {
1377 pms_synaptics_process_packet(psc, &packet);
1378 sc->packet_count[SYN_PRIMARY_FINGER]++;
1379 }
1380
1381 sc->packet_count[SYN_PRIMARY_FINGER] = 0;
1382 sc->packet_count[SYN_SECONDARY_FINGER] = 0;
1383 }
1384
1385 /* Only one finger, process the new packet */
1386 if (nsp.sp_finger_count == 1) {
1387 if (nsp.sp_finger_count != packet.sp_finger_count) {
1388 sc->packet_count[SYN_PRIMARY_FINGER] = 0;
1389 sc->packet_count[SYN_SECONDARY_FINGER] = 0;
1390 }
1391 pms_synaptics_process_packet(psc, &nsp);
1392
1393 /* clear the fingers seen since we have processed */
1394 nsp.sp_primary = 0;
1395 nsp.sp_secondary = 0;
1396 nsp.sp_finger_status = 0;
1397
1398 sc->packet_count[SYN_PRIMARY_FINGER]++;
1399 }
1400
1401 /*
1402 * More than one finger and we have seen the primary and secondary
1403 * fingers then process the packet.
1404 */
1405 if ((nsp.sp_finger_count > 1) && (nsp.sp_primary == 1)
1406 && (nsp.sp_secondary == 1)) {
1407 if (nsp.sp_finger_count != packet.sp_finger_count) {
1408 sc->packet_count[SYN_PRIMARY_FINGER] = 0;
1409 sc->packet_count[SYN_SECONDARY_FINGER] = 0;
1410 }
1411 pms_synaptics_process_packet(psc, &nsp);
1412
1413 /* clear the fingers seen since we have processed */
1414 nsp.sp_primary = 0;
1415 nsp.sp_secondary = 0;
1416 nsp.sp_finger_status = 0;
1417
1418 sc->packet_count[SYN_PRIMARY_FINGER]++;
1419 sc->packet_count[SYN_SECONDARY_FINGER]++;
1420 }
1421
1422 memcpy(&packet, &nsp, sizeof(packet));
1423 }
1424
1425 /*
1426 * Passthrough is used for e.g. TrackPoints and additional pointing
1427 * devices connected to a Synaptics touchpad.
1428 */
1429 static void
1430 pms_synaptics_passthrough(struct pms_softc *psc)
1431 {
1432 int dx, dy, dz;
1433 int buttons, changed;
1434 int s;
1435
1436 buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
1437 ((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
1438 ((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);
1439
1440 dx = psc->packet[4];
1441 if (dx >= 128)
1442 dx -= 256;
1443 if (dx == -128)
1444 dx = -127;
1445
1446 dy = psc->packet[5];
1447 if (dy >= 128)
1448 dy -= 256;
1449 if (dy == -128)
1450 dy = -127;
1451
1452 dz = 0;
1453
1454 changed = buttons ^ (psc->buttons & 0xe0);
1455 psc->buttons ^= changed;
1456
1457 if (dx || dy || dz || changed) {
1458 s = spltty();
1459 /*
1460 * If the middle button is held, interpret movement as
1461 * scrolling.
1462 */
1463 if (synaptics_aux_mid_button_scroll &&
1464 dy && (psc->buttons & 0x2)) {
1465 wsmouse_precision_scroll(psc->sc_wsmousedev, dx, dy);
1466 } else {
1467 buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
1468 wsmouse_input(psc->sc_wsmousedev,
1469 buttons, dx, dy, dz, 0,
1470 WSMOUSE_INPUT_DELTA);
1471 }
1472 splx(s);
1473 }
1474 }
1475
1476 static void
1477 pms_synaptics_input(void *vsc, int data)
1478 {
1479 struct pms_softc *psc = vsc;
1480 struct timeval diff;
1481
1482 if (!psc->sc_enabled) {
1483 /* Interrupts are not expected. Discard the byte. */
1484 return;
1485 }
1486
1487 getmicrouptime(&psc->current);
1488
1489 if (psc->inputstate > 0) {
1490 timersub(&psc->current, &psc->last, &diff);
1491 if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
1492 aprint_debug_dev(psc->sc_dev,
1493 "pms_synaptics_input: unusual delay (%ld.%06ld s), "
1494 "scheduling reset\n",
1495 (long)diff.tv_sec, (long)diff.tv_usec);
1496 printf("pms_synaptics_input: unusual delay (%ld.%06ld s), "
1497 "scheduling reset\n",
1498 (long)diff.tv_sec, (long)diff.tv_usec);
1499 psc->inputstate = 0;
1500 psc->sc_enabled = 0;
1501 wakeup(&psc->sc_enabled);
1502 return;
1503 }
1504 }
1505 psc->last = psc->current;
1506
1507 switch (psc->inputstate) {
1508 case -5:
1509 case -4:
1510 case -3:
1511 case -2:
1512 case -1:
1513 case 0:
1514 if ((data & 0xc8) != 0x80) {
1515 aprint_debug_dev(psc->sc_dev,
1516 "pms_synaptics_input: 0x%02x out of sync\n", data);
1517 /* use negative counts to limit resync phase */
1518 psc->inputstate--;
1519 return; /* not in sync yet, discard input */
1520 }
1521 psc->inputstate = 0;
1522 /*FALLTHROUGH*/
1523
1524 case -6:
1525 case 3:
1526 if ((data & 8) == 8) {
1527 aprint_debug_dev(psc->sc_dev,
1528 "pms_synaptics_input: dropped in relative mode, reset\n");
1529 psc->inputstate = 0;
1530 psc->sc_enabled = 0;
1531 wakeup(&psc->sc_enabled);
1532 return;
1533 }
1534 }
1535
1536 psc->packet[psc->inputstate++] = data & 0xff;
1537 if (psc->inputstate == 6) {
1538 /*
1539 * We have a complete packet.
1540 * Extract the pertinent details.
1541 */
1542 psc->inputstate = 0;
1543 if ((psc->packet[0] & 0xfc) == 0x84 &&
1544 (psc->packet[3] & 0xcc) == 0xc4) {
1545 /* W = SYNAPTICS_WIDTH_PASSTHROUGH, PS/2 passthrough */
1546 pms_synaptics_passthrough(psc);
1547 } else {
1548 pms_synaptics_parse(psc);
1549 }
1550 }
1551 }
1552
1553 static inline int
1554 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
1555 int *palmp)
1556 {
1557 int fingers;
1558
1559 /* Assume no palm */
1560 *palmp = 0;
1561
1562 /*
1563 * Apply some hysteresis when checking for a finger.
1564 * When the finger is first applied, we ignore it until the
1565 * pressure exceeds the 'high' threshold. The finger is considered
1566 * removed only when pressure falls beneath the 'low' threshold.
1567 */
1568 if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
1569 (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
1570 fingers = 1;
1571 else
1572 fingers = 0;
1573
1574 /*
1575 * If the pad can't do palm detection, skip the rest.
1576 */
1577 if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
1578 return (fingers);
1579
1580 /*
1581 * Palm detection
1582 */
1583 if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
1584 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
1585 *palmp = 1;
1586
1587 if (sc->prev_fingers == 0 &&
1588 (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
1589 sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
1590 /*
1591 * Contact area or pressure is too great to be a finger.
1592 * Just ignore it for now.
1593 */
1594 return (0);
1595 }
1596
1597 /*
1598 * Detect 2 and 3 fingers if supported, but only if multiple
1599 * fingers appear within the tap gesture time period.
1600 */
1601 if ((sc->flags & SYN_FLAG_HAS_MULTI_FINGER) &&
1602 ((SYN_TIME(sc, sc->gesture_start_packet)
1603 < synaptics_gesture_length) ||
1604 SYN_TIME(sc, sc->gesture_start_packet)
1605 < synaptics_gesture_length)) {
1606 switch (sp->sp_w) {
1607 case SYNAPTICS_WIDTH_TWO_FINGERS:
1608 fingers = 2;
1609 break;
1610
1611 case SYNAPTICS_WIDTH_THREE_OR_MORE:
1612 fingers = 3;
1613 break;
1614
1615 default:
1616 /*
1617 * The width value can report spurious single-finger
1618 * events after a multi-finger event.
1619 */
1620 fingers = sc->prev_fingers <= 1 ? 1 : sc->prev_fingers;
1621 break;
1622 }
1623 }
1624
1625 return (fingers);
1626 }
1627
1628 static inline void
1629 synaptics_gesture_detect(struct synaptics_softc *sc,
1630 struct synaptics_packet *sp, int fingers)
1631 {
1632 int gesture_len, gesture_buttons;
1633 int set_buttons;
1634
1635 gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
1636 gesture_buttons = sc->gesture_buttons;
1637
1638 if (fingers > 0 && (fingers == sc->prev_fingers)) {
1639 /* Finger is still present */
1640 sc->gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
1641 sc->gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
1642 } else
1643 if (fingers && sc->prev_fingers == 0) {
1644 /*
1645 * Finger was just applied.
1646 * If the previous gesture was a single-click, set things
1647 * up to deal with a possible drag or double-click gesture.
1648 * Basically, if the finger is removed again within
1649 * 'synaptics_gesture_length' packets, this is treated
1650 * as a double-click. Otherwise we will emulate holding
1651 * the left button down whilst dragging the mouse.
1652 */
1653 if (SYN_IS_SINGLE_TAP(sc->gesture_type))
1654 sc->gesture_type |= SYN_GESTURE_DRAG;
1655
1656 sc->gesture_start_x = abs(sp->sp_x);
1657 sc->gesture_start_y = abs(sp->sp_y);
1658 sc->gesture_move_x = 0;
1659 sc->gesture_move_y = 0;
1660 sc->gesture_start_packet = sc->total_packets;
1661
1662 DPRINTF(10, sc, "Finger applied:"
1663 " gesture_start_x: %d"
1664 " gesture_start_y: %d\n",
1665 sc->gesture_start_x, sc->gesture_start_y);
1666 } else
1667 if (fingers == 0 && sc->prev_fingers != 0) {
1668 /*
1669 * Finger was just removed.
1670 * Check if the contact time and finger movement were
1671 * small enough to qualify as a gesture.
1672 * Ignore finger movement if multiple fingers were
1673 * detected (the pad may report coordinates for any
1674 * of the fingers).
1675 */
1676
1677 DPRINTF(10, sc, "Finger removed: gesture_len: %d (%d)\n",
1678 gesture_len, synaptics_gesture_length);
1679 DPRINTF(10, sc, "gesture_move_x: %d (%d) sp_x: %d\n",
1680 sc->gesture_move_x, synaptics_gesture_move, abs(sp->sp_x));
1681 DPRINTF(10, sc, "gesture_move_y: %d (%d) sp_y: %d\n",
1682 sc->gesture_move_y, synaptics_gesture_move, abs(sp->sp_y));
1683
1684 if (gesture_len < synaptics_gesture_length &&
1685 ((sc->gesture_move_x < synaptics_gesture_move &&
1686 sc->gesture_move_y < synaptics_gesture_move))) {
1687 /*
1688 * Looking good so far.
1689 */
1690 if (SYN_IS_DRAG(sc->gesture_type)) {
1691 /*
1692 * Promote this gesture to double-click.
1693 */
1694 sc->gesture_type |= SYN_GESTURE_DOUBLE;
1695 sc->gesture_type &= ~SYN_GESTURE_SINGLE;
1696 } else {
1697 /*
1698 * Single tap gesture. Set the tap length timer
1699 * and flag a single-click.
1700 */
1701 sc->gesture_tap_packet = sc->total_packets;
1702 sc->gesture_type |= SYN_GESTURE_SINGLE;
1703
1704 /*
1705 * The gesture can be modified depending on
1706 * the number of fingers detected.
1707 *
1708 * 1: Normal left button emulation.
1709 * 2: Either middle button or right button
1710 * depending on the value of the two_fingers
1711 * sysctl variable.
1712 * 3: Right button.
1713 */
1714 switch (sc->prev_fingers) {
1715 case 2:
1716 if (synaptics_two_fingers_emul == 1)
1717 gesture_buttons |= PMS_RBUTMASK;
1718 else
1719 if (synaptics_two_fingers_emul == 2)
1720 gesture_buttons |= PMS_MBUTMASK;
1721 break;
1722 case 3:
1723 gesture_buttons |= PMS_RBUTMASK;
1724 break;
1725 default:
1726 gesture_buttons |= PMS_LBUTMASK;
1727 break;
1728 }
1729 }
1730 }
1731
1732 /*
1733 * Always clear drag state when the finger is removed.
1734 */
1735 sc->gesture_type &= ~SYN_GESTURE_DRAG;
1736 }
1737
1738 if (sc->gesture_type == 0) {
1739 /*
1740 * There is no gesture in progress.
1741 * Clear emulated button state.
1742 */
1743 sc->gesture_buttons = 0;
1744 return;
1745 }
1746
1747 /*
1748 * A gesture is in progress.
1749 */
1750 set_buttons = 0;
1751
1752 if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
1753 /*
1754 * Single-click.
1755 * Activate the relevant button(s) until the
1756 * gesture tap timer has expired.
1757 */
1758 if (SYN_TIME(sc, sc->gesture_tap_packet) <
1759 synaptics_gesture_length)
1760 set_buttons = 1;
1761 else
1762 sc->gesture_type &= ~SYN_GESTURE_SINGLE;
1763 DPRINTF(10, sc, "synaptics_gesture: single tap, buttons %d\n",
1764 set_buttons);
1765 DPRINTF(10, sc, "synaptics_gesture: single tap, tap at %d, current %d\n",
1766 sc->gesture_tap_packet, sc->total_packets);
1767 DPRINTF(10, sc, "synaptics_gesture: single tap, tap_time %d, gesture len %d\n",
1768 SYN_TIME(sc, sc->gesture_tap_packet), synaptics_gesture_length);
1769 } else
1770 if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
1771 /*
1772 * Double-click.
1773 * Activate the relevant button(s) once.
1774 */
1775 set_buttons = 1;
1776 sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
1777 }
1778
1779 if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
1780 /*
1781 * Single-click and drag.
1782 * Maintain button state until the finger is removed.
1783 */
1784 sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
1785 sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
1786 sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
1787 }
1788
1789 sc->gesture_buttons = gesture_buttons;
1790 }
1791
1792 static inline int
1793 synaptics_filter_policy(struct synaptics_softc *sc, int finger, int *history,
1794 int value, u_int count)
1795 {
1796 int a, b, rv;
1797
1798 /*
1799 * Once we've accumulated at least SYN_HIST_SIZE values, combine
1800 * each new value with the previous two and return the average.
1801 *
1802 * This is necessary when the touchpad is operating in 80 packets
1803 * per second mode, as it performs little internal filtering on
1804 * reported values.
1805 *
1806 * Using a rolling average helps to filter out jitter caused by
1807 * tiny finger movements.
1808 */
1809 if (count >= SYN_HIST_SIZE) {
1810 a = (history[(count + 0) % SYN_HIST_SIZE] +
1811 history[(count + 1) % SYN_HIST_SIZE]) / 2;
1812
1813 b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
1814
1815 rv = b - a;
1816
1817 /*
1818 * Don't report the movement if it's below a certain
1819 * threshold.
1820 */
1821 if (abs(rv) < synaptics_movement_threshold)
1822 rv = 0;
1823 } else
1824 rv = 0;
1825
1826 /*
1827 * Add the new value to the history buffer.
1828 */
1829 history[(count + 1) % SYN_HIST_SIZE] = value;
1830
1831 return (rv);
1832 }
1833
1834 /* Edge detection */
1835 #define SYN_EDGE_TOP 1
1836 #define SYN_EDGE_BOTTOM 2
1837 #define SYN_EDGE_LEFT 4
1838 #define SYN_EDGE_RIGHT 8
1839
1840 static inline int
1841 synaptics_check_edge(int x, int y)
1842 {
1843 int rv = 0;
1844
1845 if (x < synaptics_edge_left)
1846 rv |= SYN_EDGE_LEFT;
1847 else
1848 if (x > synaptics_edge_right)
1849 rv |= SYN_EDGE_RIGHT;
1850
1851 if (y < synaptics_edge_bottom)
1852 rv |= SYN_EDGE_BOTTOM;
1853 else
1854 if (y > synaptics_edge_top)
1855 rv |= SYN_EDGE_TOP;
1856
1857 return (rv);
1858 }
1859
1860 static inline int
1861 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
1862 {
1863
1864 /*
1865 * When edge motion is enabled, synaptics_edge_motion_delta is
1866 * combined with the current delta, together with the direction
1867 * in which to simulate the motion. The result is added to
1868 * the delta derived from finger movement. This provides a smooth
1869 * transition from finger movement to edge motion.
1870 */
1871 delta = synaptics_edge_motion_delta + (dir * delta);
1872 if (delta < 0)
1873 return (0);
1874 if (delta > synaptics_edge_motion_delta)
1875 return (synaptics_edge_motion_delta);
1876 return (delta);
1877 }
1878
1879 static inline int
1880 synaptics_scale(int delta, int scale, int *remp)
1881 {
1882 int rv;
1883
1884 /*
1885 * Scale the raw delta in Synaptics coordinates (0-6143) into
1886 * something more reasonable by dividing the raw delta by a
1887 * scale factor. Any remainder from the previous scale result
1888 * is added to the current delta before scaling.
1889 * This prevents loss of resolution for very small/slow
1890 * movements of the finger.
1891 */
1892 delta += *remp;
1893 rv = delta / scale;
1894 *remp = delta % scale;
1895
1896 return (rv);
1897 }
1898
1899 static inline void
1900 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
1901 int *dxp, int *dyp, int *dzp, int *sdxp, int *sdyp, int *sdzp)
1902 {
1903 int dx, dy, dz, sdx, sdy, sdz, edge;
1904
1905 dx = dy = dz = sdx = sdy = sdz = 0;
1906
1907 /*
1908 * Compute the next values of dx, dy, dz, sdx, sdy, sdz.
1909 */
1910 dx = synaptics_filter_policy(sc, 0,
1911 sc->history_x[SYN_PRIMARY_FINGER], sp->sp_x,
1912 sc->packet_count[SYN_PRIMARY_FINGER]);
1913 dy = synaptics_filter_policy(sc, 0,
1914 sc->history_y[SYN_PRIMARY_FINGER], sp->sp_y,
1915 sc->packet_count[SYN_PRIMARY_FINGER]);
1916
1917 if (sp->sp_finger_count > 1) {
1918 sdx = synaptics_filter_policy(sc, 1,
1919 sc->history_x[SYN_SECONDARY_FINGER], sp->sp_sx,
1920 sc->packet_count[SYN_SECONDARY_FINGER]);
1921 sdy = synaptics_filter_policy(sc, 1,
1922 sc->history_y[SYN_SECONDARY_FINGER], sp->sp_sy,
1923 sc->packet_count[SYN_SECONDARY_FINGER]);
1924 DPRINTF(10, sc, "synaptics_movement: dx %d dy %d sdx %d sdy %d\n",
1925 dx, dy, sdx, sdy);
1926 }
1927
1928 /*
1929 * If we're dealing with a drag gesture, and the finger moves to
1930 * the edge of the touchpad, apply edge motion emulation if it
1931 * is enabled.
1932 */
1933 if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
1934 edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
1935
1936 if (edge & SYN_EDGE_LEFT)
1937 dx -= synaptics_edge_motion(sc, dx, 1);
1938 if (edge & SYN_EDGE_RIGHT)
1939 dx += synaptics_edge_motion(sc, dx, -1);
1940 if (edge & SYN_EDGE_BOTTOM)
1941 dy -= synaptics_edge_motion(sc, dy, 1);
1942 if (edge & SYN_EDGE_TOP)
1943 dy += synaptics_edge_motion(sc, dy, -1);
1944
1945 if (sp->sp_finger_count > 1) {
1946 edge = synaptics_check_edge(sp->sp_sx, sp->sp_sy);
1947
1948 if (edge & SYN_EDGE_LEFT)
1949 sdx -= synaptics_edge_motion(sc, sdx, 1);
1950 if (edge & SYN_EDGE_RIGHT)
1951 sdx += synaptics_edge_motion(sc, sdx, -1);
1952 if (edge & SYN_EDGE_BOTTOM)
1953 sdy -= synaptics_edge_motion(sc, sdy, 1);
1954 if (edge & SYN_EDGE_TOP)
1955 sdy += synaptics_edge_motion(sc, sdy, -1);
1956 }
1957 }
1958
1959 /*
1960 * Apply scaling to the deltas
1961 */
1962 dx = synaptics_scale(dx, synaptics_scale_x,
1963 &sc->rem_x[SYN_PRIMARY_FINGER]);
1964 dy = synaptics_scale(dy, synaptics_scale_y,
1965 &sc->rem_y[SYN_PRIMARY_FINGER]);
1966 dz = synaptics_scale(dz, synaptics_scale_z,
1967 &sc->rem_z[SYN_PRIMARY_FINGER]);
1968
1969 if (sp->sp_finger_count > 1) {
1970 sdx = synaptics_scale(sdx, synaptics_scale_x,
1971 &sc->rem_x[SYN_SECONDARY_FINGER]);
1972 sdy = synaptics_scale(sdy, synaptics_scale_y,
1973 &sc->rem_y[SYN_SECONDARY_FINGER]);
1974 sdz = synaptics_scale(sdz, synaptics_scale_z,
1975 &sc->rem_z[SYN_SECONDARY_FINGER]);
1976
1977 DPRINTF(10, sc,
1978 "synaptics_movement 2: dx %d dy %d sdx %d sdy %d\n",
1979 dx, dy, sdx, sdy);
1980 }
1981
1982 /*
1983 * Clamp deltas to specified maximums.
1984 */
1985 if (abs(dx) > synaptics_max_speed_x)
1986 dx = ((dx >= 0)? 1 : -1) * synaptics_max_speed_x;
1987 if (abs(dy) > synaptics_max_speed_y)
1988 dy = ((dy >= 0)? 1 : -1) * synaptics_max_speed_y;
1989 if (abs(dz) > synaptics_max_speed_z)
1990 dz = ((dz >= 0)? 1 : -1) * synaptics_max_speed_z;
1991
1992 if (sp->sp_finger_count > 1) {
1993 if (abs(sdx) > synaptics_max_speed_x)
1994 sdx = ((sdx >= 0)? 1 : -1) * synaptics_max_speed_x;
1995 if (abs(dy) > synaptics_max_speed_y)
1996 sdy = ((sdy >= 0)? 1 : -1) * synaptics_max_speed_y;
1997 if (abs(sdz) > synaptics_max_speed_z)
1998 sdz = ((sdz >= 0)? 1 : -1) * synaptics_max_speed_z;
1999 }
2000
2001 *dxp = dx;
2002 *dyp = dy;
2003 *dzp = dz;
2004 *sdxp = sdx;
2005 *sdyp = sdy;
2006 *sdzp = sdz;
2007
2008 }
2009
2010 static void
2011 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
2012 {
2013 struct synaptics_softc *sc = &psc->u.synaptics;
2014 int dx, dy, dz, sdx, sdy, sdz;
2015 int fingers, palm, buttons, changed;
2016 int s;
2017
2018 sdx = sdy = sdz = 0;
2019
2020 /*
2021 * Do Z-axis emulation using up/down buttons if required.
2022 * Note that the pad will send a one second burst of packets
2023 * when an up/down button is pressed and held. At the moment
2024 * we don't deal with auto-repeat, so convert the burst into
2025 * a one-shot.
2026 */
2027 dz = 0;
2028 if (synaptics_up_down_emul == 2) {
2029 if (sc->up_down == 0) {
2030 if (sp->sp_up && sp->sp_down) {
2031 sp->sp_middle = 1;
2032 } else if (sp->sp_up) {
2033 dz = -synaptics_up_down_motion_delta;
2034 } else if (sp->sp_down) {
2035 dz = synaptics_up_down_motion_delta;
2036 }
2037 }
2038
2039 sc->up_down = sp->sp_up | sp->sp_down;
2040 sp->sp_up = sp->sp_down = 0;
2041 }
2042
2043 /*
2044 * Determine whether or not a finger is on the pad.
2045 * On some pads, this will return the number of fingers
2046 * detected.
2047 */
2048 fingers = synaptics_finger_detect(sc, sp, &palm);
2049
2050 /*
2051 * Do gesture processing only if we didn't detect a palm.
2052 */
2053 if (palm == 0)
2054 synaptics_gesture_detect(sc, sp, fingers);
2055 else
2056 sc->gesture_type = sc->gesture_buttons = 0;
2057
2058 /*
2059 * Determine what buttons to report
2060 */
2061 buttons = (sp->sp_left ? 0x1 : 0) |
2062 (sp->sp_middle ? 0x2 : 0) |
2063 (sp->sp_right ? 0x4 : 0) |
2064 (sp->sp_up ? 0x8 : 0) |
2065 (sp->sp_down ? 0x10 : 0);
2066 changed = buttons ^ (psc->buttons & 0x1f);
2067 psc->buttons ^= changed;
2068
2069 sc->prev_fingers = fingers;
2070
2071 /*
2072 * Do movement processing IFF we have a single finger and no palm or
2073 * a secondary finger and no palm.
2074 */
2075 if (palm == 0 && synaptics_movement_enable) {
2076 if (fingers > 0) {
2077 synaptics_movement(sc, sp, &dx, &dy, &dz, &sdx, &sdy,
2078 &sdz);
2079
2080 /*
2081 * Check if there are two fingers, if there are then
2082 * check if we have a clickpad, if we do then we
2083 * don't scroll iff one of the fingers is in the
2084 * button region. Otherwise interpret as a scroll
2085 */
2086 if (sp->sp_finger_count >= 2 && sc->gesture_type == 0 ) {
2087 if (!(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) ||
2088 ((sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) &&
2089 (sp->sp_y > synaptics_button_boundary) &&
2090 (sp->sp_sy > synaptics_button_boundary))) {
2091 s = spltty();
2092 wsmouse_precision_scroll(
2093 psc->sc_wsmousedev, dx, dy);
2094 splx(s);
2095 return;
2096 }
2097 }
2098
2099 /*
2100 * Allow user to turn off movements in the button
2101 * region of a click pad.
2102 */
2103 if (sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
2104 if ((sp->sp_y < synaptics_button_boundary) &&
2105 (!synaptics_button_region_movement)) {
2106 dx = dy = dz = 0;
2107 }
2108
2109 if ((sp->sp_sy < synaptics_button_boundary) &&
2110 (!synaptics_button_region_movement)) {
2111 sdx = sdy = sdz = 0;
2112 }
2113 }
2114
2115 /* Now work out which finger to report, just
2116 * go with the one that has moved the most.
2117 */
2118 if ((abs(dx) + abs(dy) + abs(dz)) <
2119 (abs(sdx) + abs(sdy) + abs(sdz))) {
2120 /* secondary finger wins */
2121 dx = sdx;
2122 dy = sdy;
2123 dz = sdz;
2124 }
2125 } else {
2126 /*
2127 * No valid finger. Therefore no movement.
2128 */
2129 sc->rem_x[SYN_PRIMARY_FINGER] = 0;
2130 sc->rem_y[SYN_PRIMARY_FINGER] = 0;
2131 sc->rem_x[SYN_SECONDARY_FINGER] = 0;
2132 sc->rem_y[SYN_SECONDARY_FINGER] = 0;
2133 dx = dy = 0;
2134 }
2135 } else {
2136 /*
2137 * No valid finger. Therefore no movement.
2138 */
2139 sc->rem_x[SYN_PRIMARY_FINGER] = 0;
2140 sc->rem_y[SYN_PRIMARY_FINGER] = 0;
2141 sc->rem_z[SYN_PRIMARY_FINGER] = 0;
2142 dx = dy = dz = 0;
2143 }
2144
2145 DPRINTF(10, sc,
2146 "pms_synaptics_process_packet: dx %d dy %d dz %d sdx %d sdy %d sdz %d\n",
2147 dx, dy, dz, sdx, sdy, sdz);
2148
2149 /*
2150 * Pass the final results up to wsmouse_input() if necessary.
2151 */
2152 if (dx || dy || dz || changed) {
2153 buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
2154 s = spltty();
2155 wsmouse_input(psc->sc_wsmousedev,
2156 buttons,
2157 dx, dy, dz, 0,
2158 WSMOUSE_INPUT_DELTA);
2159 splx(s);
2160 }
2161 }
2162