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