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