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