synaptics.c revision 1.27 1 1.27 jakllsch /* $NetBSD: synaptics.c,v 1.27 2011/09/09 14:29:47 jakllsch 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.27 jakllsch __KERNEL_RCSID(0, "$NetBSD: synaptics.c,v 1.27 2011/09/09 14:29:47 jakllsch Exp $");
52 1.1 christos
53 1.1 christos #include <sys/param.h>
54 1.1 christos #include <sys/systm.h>
55 1.1 christos #include <sys/device.h>
56 1.1 christos #include <sys/ioctl.h>
57 1.1 christos #include <sys/sysctl.h>
58 1.1 christos #include <sys/kernel.h>
59 1.25 uebayasi #include <sys/proc.h>
60 1.1 christos
61 1.16 ad #include <sys/bus.h>
62 1.1 christos
63 1.1 christos #include <dev/pckbport/pckbportvar.h>
64 1.1 christos
65 1.1 christos #include <dev/pckbport/synapticsreg.h>
66 1.1 christos #include <dev/pckbport/synapticsvar.h>
67 1.1 christos
68 1.1 christos #include <dev/pckbport/pmsreg.h>
69 1.1 christos #include <dev/pckbport/pmsvar.h>
70 1.1 christos
71 1.1 christos #include <dev/wscons/wsconsio.h>
72 1.1 christos #include <dev/wscons/wsmousevar.h>
73 1.1 christos
74 1.3 scw /*
75 1.3 scw * Absolute-mode packets are decoded and passed around using
76 1.3 scw * the following structure.
77 1.3 scw */
78 1.3 scw struct synaptics_packet {
79 1.3 scw signed short sp_x; /* Unscaled absolute X/Y coordinates */
80 1.3 scw signed short sp_y;
81 1.3 scw u_char sp_z; /* Z (pressure) */
82 1.3 scw u_char sp_w; /* W (contact patch width) */
83 1.3 scw char sp_left; /* Left mouse button status */
84 1.3 scw char sp_right; /* Right mouse button status */
85 1.3 scw char sp_middle; /* Middle button status (possibly emulated) */
86 1.3 scw char sp_up; /* Up button status */
87 1.3 scw char sp_down; /* Down button status */
88 1.3 scw };
89 1.3 scw
90 1.1 christos static void pms_synaptics_input(void *, int);
91 1.3 scw static void pms_synaptics_process_packet(struct pms_softc *,
92 1.3 scw struct synaptics_packet *);
93 1.2 christos static void pms_sysctl_synaptics(struct sysctllog **);
94 1.1 christos static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
95 1.1 christos
96 1.1 christos /* Controled by sysctl. */
97 1.3 scw static int synaptics_up_down_emul = 2;
98 1.3 scw static int synaptics_up_down_motion_delta = 1;
99 1.3 scw static int synaptics_gesture_move = 200;
100 1.3 scw static int synaptics_gesture_length = 20;
101 1.3 scw static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
102 1.3 scw static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
103 1.3 scw static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
104 1.3 scw static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
105 1.3 scw static int synaptics_edge_motion_delta = 32;
106 1.3 scw static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
107 1.3 scw static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
108 1.3 scw static int synaptics_two_fingers_emul = 0;
109 1.3 scw static int synaptics_scale_x = 16;
110 1.3 scw static int synaptics_scale_y = 16;
111 1.3 scw static int synaptics_max_speed_x = 32;
112 1.3 scw static int synaptics_max_speed_y = 32;
113 1.3 scw static int synaptics_movement_threshold = 4;
114 1.1 christos
115 1.1 christos /* Sysctl nodes. */
116 1.3 scw static int synaptics_up_down_emul_nodenum;
117 1.3 scw static int synaptics_up_down_motion_delta_nodenum;
118 1.3 scw static int synaptics_gesture_move_nodenum;
119 1.3 scw static int synaptics_gesture_length_nodenum;
120 1.3 scw static int synaptics_edge_left_nodenum;
121 1.3 scw static int synaptics_edge_right_nodenum;
122 1.3 scw static int synaptics_edge_top_nodenum;
123 1.3 scw static int synaptics_edge_bottom_nodenum;
124 1.3 scw static int synaptics_edge_motion_delta_nodenum;
125 1.3 scw static int synaptics_finger_high_nodenum;
126 1.3 scw static int synaptics_finger_low_nodenum;
127 1.3 scw static int synaptics_two_fingers_emul_nodenum;
128 1.3 scw static int synaptics_scale_x_nodenum;
129 1.3 scw static int synaptics_scale_y_nodenum;
130 1.3 scw static int synaptics_max_speed_x_nodenum;
131 1.3 scw static int synaptics_max_speed_y_nodenum;
132 1.3 scw static int synaptics_movement_threshold_nodenum;
133 1.1 christos
134 1.1 christos int
135 1.1 christos pms_synaptics_probe_init(void *vsc)
136 1.1 christos {
137 1.3 scw struct pms_softc *psc = vsc;
138 1.3 scw struct synaptics_softc *sc = &psc->u.synaptics;
139 1.1 christos u_char cmd[2], resp[3];
140 1.3 scw int res, ver_minor, ver_major;
141 1.2 christos struct sysctllog *clog = NULL;
142 1.1 christos
143 1.27 jakllsch res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
144 1.1 christos SYNAPTICS_IDENTIFY_TOUCHPAD);
145 1.1 christos cmd[0] = PMS_SEND_DEV_STATUS;
146 1.3 scw res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
147 1.3 scw resp, 0);
148 1.1 christos if (res) {
149 1.26 cegger aprint_debug_dev(psc->sc_dev,
150 1.20 cube "synaptics_probe: Identify Touchpad error.\n");
151 1.3 scw /*
152 1.3 scw * Reset device in case the probe confused it.
153 1.3 scw */
154 1.3 scw doreset:
155 1.3 scw cmd[0] = PMS_RESET;
156 1.3 scw (void) pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
157 1.3 scw 1, 2, resp, 1);
158 1.3 scw return (res);
159 1.1 christos }
160 1.1 christos
161 1.1 christos if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
162 1.26 cegger aprint_debug_dev(psc->sc_dev,
163 1.20 cube "synaptics_probe: Not synaptics.\n");
164 1.3 scw res = 1;
165 1.3 scw goto doreset;
166 1.1 christos }
167 1.1 christos
168 1.3 scw sc->flags = 0;
169 1.3 scw
170 1.1 christos /* Check for minimum version and print a nice message. */
171 1.1 christos ver_major = resp[2] & 0x0f;
172 1.1 christos ver_minor = resp[0];
173 1.20 cube aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
174 1.20 cube ver_major, ver_minor);
175 1.1 christos if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
176 1.1 christos /* No capability query support. */
177 1.3 scw sc->caps = 0;
178 1.1 christos goto done;
179 1.1 christos }
180 1.1 christos
181 1.1 christos /* Query the hardware capabilities. */
182 1.27 jakllsch res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
183 1.1 christos SYNAPTICS_READ_CAPABILITIES);
184 1.1 christos cmd[0] = PMS_SEND_DEV_STATUS;
185 1.3 scw res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
186 1.3 scw resp, 0);
187 1.1 christos if (res) {
188 1.1 christos /* Hmm, failed to get capabilites. */
189 1.20 cube aprint_error_dev(psc->sc_dev,
190 1.20 cube "synaptics_probe: Failed to query capabilities.\n");
191 1.3 scw goto doreset;
192 1.1 christos }
193 1.1 christos
194 1.3 scw sc->caps = (resp[0] << 8) | resp[2];
195 1.3 scw
196 1.3 scw if (sc->caps & SYNAPTICS_CAP_MBUTTON)
197 1.3 scw sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
198 1.3 scw
199 1.3 scw if (sc->caps & SYNAPTICS_CAP_4BUTTON)
200 1.3 scw sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
201 1.3 scw
202 1.3 scw if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
203 1.26 cegger aprint_debug_dev(psc->sc_dev,
204 1.20 cube "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
205 1.14 mlelstv if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
206 1.3 scw sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
207 1.3 scw
208 1.3 scw if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
209 1.3 scw sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
210 1.3 scw
211 1.3 scw if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
212 1.3 scw sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
213 1.1 christos
214 1.1 christos /* Ask about extra buttons to detect up/down. */
215 1.3 scw if (sc->caps & SYNAPTICS_CAP_EXTNUM) {
216 1.27 jakllsch res = pms_sliced_command(psc->sc_kbctag,
217 1.3 scw psc->sc_kbcslot, SYNAPTICS_EXTENDED_QUERY);
218 1.1 christos cmd[0] = PMS_SEND_DEV_STATUS;
219 1.3 scw res |= pckbport_poll_cmd(psc->sc_kbctag,
220 1.3 scw psc->sc_kbcslot, cmd, 1, 3, resp, 0);
221 1.3 scw if (res == 0)
222 1.26 cegger aprint_debug_dev(psc->sc_dev,
223 1.21 ad "synaptics_probe: Extended "
224 1.20 cube "Capabilities 0x%02x.\n", resp[1]);
225 1.3 scw if (!res && (resp[1] >> 4) >= 2) {
226 1.1 christos /* Yes. */
227 1.3 scw sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
228 1.1 christos }
229 1.3 scw }
230 1.3 scw }
231 1.3 scw
232 1.3 scw if (sc->flags) {
233 1.3 scw const char comma[] = ", ";
234 1.3 scw const char *sep = "";
235 1.20 cube aprint_normal_dev(psc->sc_dev, "");
236 1.23 plunky if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
237 1.23 plunky aprint_normal("%sPassthrough", sep);
238 1.23 plunky sep = comma;
239 1.23 plunky }
240 1.3 scw if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
241 1.3 scw aprint_normal("%sMiddle button", sep);
242 1.3 scw sep = comma;
243 1.3 scw }
244 1.3 scw if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
245 1.3 scw aprint_normal("%sButtons 4/5", sep);
246 1.3 scw sep = comma;
247 1.3 scw }
248 1.3 scw if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS) {
249 1.3 scw aprint_normal("%sUp/down buttons", sep);
250 1.3 scw sep = comma;
251 1.3 scw }
252 1.3 scw if (sc->flags & SYN_FLAG_HAS_PALM_DETECT) {
253 1.3 scw aprint_normal("%sPalm detect", sep);
254 1.3 scw sep = comma;
255 1.3 scw }
256 1.3 scw if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER)
257 1.3 scw aprint_normal("%sMulti-finger", sep);
258 1.3 scw
259 1.3 scw aprint_normal("\n");
260 1.1 christos }
261 1.1 christos
262 1.1 christos done:
263 1.2 christos pms_sysctl_synaptics(&clog);
264 1.3 scw pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
265 1.20 cube pms_synaptics_input, psc, device_xname(psc->sc_dev));
266 1.3 scw
267 1.3 scw return (0);
268 1.1 christos }
269 1.1 christos
270 1.1 christos void
271 1.1 christos pms_synaptics_enable(void *vsc)
272 1.1 christos {
273 1.3 scw struct pms_softc *psc = vsc;
274 1.3 scw struct synaptics_softc *sc = &psc->u.synaptics;
275 1.23 plunky u_char cmd[2], resp[2];
276 1.1 christos int res;
277 1.1 christos
278 1.23 plunky if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
279 1.23 plunky /*
280 1.23 plunky * Extended capability probes can confuse the passthrough device;
281 1.23 plunky * reset the touchpad now to cure that.
282 1.23 plunky */
283 1.23 plunky cmd[0] = PMS_RESET;
284 1.23 plunky res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
285 1.23 plunky 1, 2, resp, 1);
286 1.23 plunky }
287 1.23 plunky
288 1.3 scw /*
289 1.3 scw * Enable Absolute mode with W (width) reporting, and set
290 1.3 scw * the packet rate to maximum (80 packets per second).
291 1.3 scw */
292 1.27 jakllsch res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
293 1.3 scw SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE);
294 1.1 christos cmd[0] = PMS_SET_SAMPLE;
295 1.22 plunky cmd[1] = SYNAPTICS_CMD_SET_MODE2;
296 1.3 scw res |= pckbport_enqueue_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 2, 0,
297 1.3 scw 1, NULL);
298 1.3 scw sc->up_down = 0;
299 1.3 scw sc->prev_fingers = 0;
300 1.3 scw sc->gesture_start_x = sc->gesture_start_y = 0;
301 1.3 scw sc->gesture_start_packet = 0;
302 1.3 scw sc->gesture_tap_packet = 0;
303 1.3 scw sc->gesture_type = 0;
304 1.3 scw sc->gesture_buttons = 0;
305 1.3 scw sc->rem_x = sc->rem_y = 0;
306 1.3 scw sc->movement_history = 0;
307 1.3 scw if (res) {
308 1.20 cube aprint_error_dev(psc->sc_dev,
309 1.20 cube "synaptics_enable: Error enabling device.\n");
310 1.3 scw }
311 1.1 christos }
312 1.1 christos
313 1.1 christos void
314 1.1 christos pms_synaptics_resume(void *vsc)
315 1.1 christos {
316 1.3 scw struct pms_softc *psc = vsc;
317 1.3 scw unsigned char cmd[1],resp[2] = { 0,0 };
318 1.3 scw int res;
319 1.1 christos
320 1.1 christos cmd[0] = PMS_RESET;
321 1.3 scw res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
322 1.3 scw resp, 1);
323 1.20 cube aprint_debug_dev(psc->sc_dev,
324 1.20 cube "pms_synaptics_resume: reset on resume %d 0x%02x 0x%02x\n",
325 1.20 cube res, resp[0], resp[1]);
326 1.1 christos }
327 1.1 christos
328 1.3 scw static void
329 1.3 scw pms_sysctl_synaptics(struct sysctllog **clog)
330 1.1 christos {
331 1.1 christos int rc, root_num;
332 1.6 atatat const struct sysctlnode *node;
333 1.1 christos
334 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, NULL,
335 1.1 christos CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
336 1.1 christos NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
337 1.1 christos goto err;
338 1.1 christos
339 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, &node,
340 1.1 christos CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
341 1.1 christos SYSCTL_DESCR("Synaptics touchpad controls"),
342 1.1 christos NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
343 1.1 christos goto err;
344 1.1 christos
345 1.1 christos root_num = node->sysctl_num;
346 1.1 christos
347 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, &node,
348 1.1 christos CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
349 1.3 scw CTLTYPE_INT, "up_down_emulation",
350 1.3 scw SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
351 1.3 scw pms_sysctl_synaptics_verify, 0,
352 1.3 scw &synaptics_up_down_emul,
353 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
354 1.3 scw CTL_EOL)) != 0)
355 1.3 scw goto err;
356 1.3 scw
357 1.3 scw synaptics_up_down_emul_nodenum = node->sysctl_num;
358 1.3 scw
359 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
360 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
361 1.3 scw CTLTYPE_INT, "up_down_motion_delta",
362 1.3 scw SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
363 1.3 scw pms_sysctl_synaptics_verify, 0,
364 1.3 scw &synaptics_up_down_motion_delta,
365 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
366 1.3 scw CTL_EOL)) != 0)
367 1.3 scw goto err;
368 1.3 scw
369 1.3 scw synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
370 1.3 scw
371 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
372 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
373 1.3 scw CTLTYPE_INT, "gesture_move",
374 1.3 scw SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
375 1.3 scw pms_sysctl_synaptics_verify, 0,
376 1.3 scw &synaptics_gesture_move,
377 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
378 1.3 scw CTL_EOL)) != 0)
379 1.3 scw goto err;
380 1.3 scw
381 1.3 scw synaptics_gesture_move_nodenum = node->sysctl_num;
382 1.3 scw
383 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
384 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
385 1.3 scw CTLTYPE_INT, "gesture_length",
386 1.3 scw SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
387 1.3 scw pms_sysctl_synaptics_verify, 0,
388 1.3 scw &synaptics_gesture_length,
389 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
390 1.3 scw CTL_EOL)) != 0)
391 1.3 scw goto err;
392 1.3 scw
393 1.3 scw synaptics_gesture_length_nodenum = node->sysctl_num;
394 1.3 scw
395 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
396 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
397 1.3 scw CTLTYPE_INT, "edge_left",
398 1.3 scw SYSCTL_DESCR("Define left edge of touchpad"),
399 1.3 scw pms_sysctl_synaptics_verify, 0,
400 1.3 scw &synaptics_edge_left,
401 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
402 1.3 scw CTL_EOL)) != 0)
403 1.3 scw goto err;
404 1.3 scw
405 1.3 scw synaptics_edge_left_nodenum = node->sysctl_num;
406 1.3 scw
407 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
408 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
409 1.3 scw CTLTYPE_INT, "edge_right",
410 1.3 scw SYSCTL_DESCR("Define right edge of touchpad"),
411 1.1 christos pms_sysctl_synaptics_verify, 0,
412 1.3 scw &synaptics_edge_right,
413 1.1 christos 0, CTL_HW, root_num, CTL_CREATE,
414 1.1 christos CTL_EOL)) != 0)
415 1.1 christos goto err;
416 1.1 christos
417 1.3 scw synaptics_edge_right_nodenum = node->sysctl_num;
418 1.1 christos
419 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, &node,
420 1.1 christos CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
421 1.3 scw CTLTYPE_INT, "edge_top",
422 1.3 scw SYSCTL_DESCR("Define top edge of touchpad"),
423 1.1 christos pms_sysctl_synaptics_verify, 0,
424 1.3 scw &synaptics_edge_top,
425 1.1 christos 0, CTL_HW, root_num, CTL_CREATE,
426 1.1 christos CTL_EOL)) != 0)
427 1.3 scw goto err;
428 1.3 scw
429 1.3 scw synaptics_edge_top_nodenum = node->sysctl_num;
430 1.3 scw
431 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
432 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
433 1.3 scw CTLTYPE_INT, "edge_bottom",
434 1.3 scw SYSCTL_DESCR("Define bottom edge of touchpad"),
435 1.3 scw pms_sysctl_synaptics_verify, 0,
436 1.3 scw &synaptics_edge_bottom,
437 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
438 1.3 scw CTL_EOL)) != 0)
439 1.3 scw goto err;
440 1.3 scw
441 1.3 scw synaptics_edge_bottom_nodenum = node->sysctl_num;
442 1.3 scw
443 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
444 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
445 1.3 scw CTLTYPE_INT, "edge_motion_delta",
446 1.3 scw SYSCTL_DESCR("Define edge motion rate"),
447 1.3 scw pms_sysctl_synaptics_verify, 0,
448 1.3 scw &synaptics_edge_motion_delta,
449 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
450 1.3 scw CTL_EOL)) != 0)
451 1.3 scw goto err;
452 1.3 scw
453 1.3 scw synaptics_edge_motion_delta_nodenum = node->sysctl_num;
454 1.3 scw
455 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
456 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
457 1.3 scw CTLTYPE_INT, "finger_high",
458 1.3 scw SYSCTL_DESCR("Define finger applied pressure threshold"),
459 1.3 scw pms_sysctl_synaptics_verify, 0,
460 1.3 scw &synaptics_finger_high,
461 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
462 1.3 scw CTL_EOL)) != 0)
463 1.3 scw goto err;
464 1.3 scw
465 1.3 scw synaptics_finger_high_nodenum = node->sysctl_num;
466 1.3 scw
467 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
468 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
469 1.3 scw CTLTYPE_INT, "finger_low",
470 1.3 scw SYSCTL_DESCR("Define finger removed pressure threshold"),
471 1.3 scw pms_sysctl_synaptics_verify, 0,
472 1.3 scw &synaptics_finger_low,
473 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
474 1.3 scw CTL_EOL)) != 0)
475 1.3 scw goto err;
476 1.3 scw
477 1.3 scw synaptics_finger_low_nodenum = node->sysctl_num;
478 1.3 scw
479 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
480 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
481 1.3 scw CTLTYPE_INT, "two_fingers_emulation",
482 1.3 scw SYSCTL_DESCR("Map two fingers to middle button"),
483 1.3 scw pms_sysctl_synaptics_verify, 0,
484 1.3 scw &synaptics_two_fingers_emul,
485 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
486 1.3 scw CTL_EOL)) != 0)
487 1.3 scw goto err;
488 1.3 scw
489 1.3 scw synaptics_two_fingers_emul_nodenum = node->sysctl_num;
490 1.3 scw
491 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
492 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
493 1.3 scw CTLTYPE_INT, "scale_x",
494 1.3 scw SYSCTL_DESCR("Horizontal movement scale factor"),
495 1.3 scw pms_sysctl_synaptics_verify, 0,
496 1.3 scw &synaptics_scale_x,
497 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
498 1.3 scw CTL_EOL)) != 0)
499 1.3 scw goto err;
500 1.3 scw
501 1.3 scw synaptics_scale_x_nodenum = node->sysctl_num;
502 1.3 scw
503 1.3 scw if ((rc = sysctl_createv(clog, 0, NULL, &node,
504 1.3 scw CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
505 1.3 scw CTLTYPE_INT, "scale_y",
506 1.3 scw SYSCTL_DESCR("Vertical movement scale factor"),
507 1.3 scw pms_sysctl_synaptics_verify, 0,
508 1.3 scw &synaptics_scale_y,
509 1.3 scw 0, CTL_HW, root_num, CTL_CREATE,
510 1.3 scw CTL_EOL)) != 0)
511 1.3 scw goto err;
512 1.1 christos
513 1.3 scw synaptics_scale_y_nodenum = node->sysctl_num;
514 1.1 christos
515 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, &node,
516 1.1 christos CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
517 1.3 scw CTLTYPE_INT, "max_speed_x",
518 1.3 scw SYSCTL_DESCR("Horizontal movement maximum speed"),
519 1.1 christos pms_sysctl_synaptics_verify, 0,
520 1.3 scw &synaptics_max_speed_x,
521 1.1 christos 0, CTL_HW, root_num, CTL_CREATE,
522 1.1 christos CTL_EOL)) != 0)
523 1.1 christos goto err;
524 1.1 christos
525 1.3 scw synaptics_max_speed_x_nodenum = node->sysctl_num;
526 1.1 christos
527 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, &node,
528 1.1 christos CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
529 1.3 scw CTLTYPE_INT, "max_speed_y",
530 1.3 scw SYSCTL_DESCR("Vertical movement maximum speed"),
531 1.1 christos pms_sysctl_synaptics_verify, 0,
532 1.3 scw &synaptics_max_speed_y,
533 1.1 christos 0, CTL_HW, root_num, CTL_CREATE,
534 1.1 christos CTL_EOL)) != 0)
535 1.1 christos goto err;
536 1.1 christos
537 1.3 scw synaptics_max_speed_y_nodenum = node->sysctl_num;
538 1.1 christos
539 1.1 christos if ((rc = sysctl_createv(clog, 0, NULL, &node,
540 1.1 christos CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
541 1.3 scw CTLTYPE_INT, "movement_threshold",
542 1.3 scw SYSCTL_DESCR("Minimum reported movement threshold"),
543 1.1 christos pms_sysctl_synaptics_verify, 0,
544 1.3 scw &synaptics_movement_threshold,
545 1.1 christos 0, CTL_HW, root_num, CTL_CREATE,
546 1.1 christos CTL_EOL)) != 0)
547 1.1 christos goto err;
548 1.1 christos
549 1.3 scw synaptics_movement_threshold_nodenum = node->sysctl_num;
550 1.1 christos return;
551 1.1 christos
552 1.1 christos err:
553 1.3 scw aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
554 1.1 christos }
555 1.1 christos
556 1.1 christos static int
557 1.1 christos pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
558 1.1 christos {
559 1.1 christos int error, t;
560 1.1 christos struct sysctlnode node;
561 1.1 christos
562 1.1 christos node = *rnode;
563 1.1 christos t = *(int *)rnode->sysctl_data;
564 1.1 christos node.sysctl_data = &t;
565 1.1 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
566 1.1 christos if (error || newp == NULL)
567 1.1 christos return error;
568 1.1 christos
569 1.1 christos /* Sanity check the params. */
570 1.3 scw if (node.sysctl_num == synaptics_up_down_emul_nodenum ||
571 1.3 scw node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
572 1.3 scw if (t < 0 || t > 2)
573 1.3 scw return (EINVAL);
574 1.3 scw } else
575 1.3 scw if (node.sysctl_num == synaptics_gesture_length_nodenum ||
576 1.3 scw node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
577 1.3 scw node.sysctl_num == synaptics_up_down_motion_delta_nodenum) {
578 1.1 christos if (t < 0)
579 1.3 scw return (EINVAL);
580 1.3 scw } else
581 1.3 scw if (node.sysctl_num == synaptics_edge_left_nodenum ||
582 1.3 scw node.sysctl_num == synaptics_edge_bottom_nodenum) {
583 1.3 scw if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
584 1.3 scw return (EINVAL);
585 1.3 scw } else
586 1.3 scw if (node.sysctl_num == synaptics_edge_right_nodenum ||
587 1.3 scw node.sysctl_num == synaptics_edge_top_nodenum) {
588 1.3 scw if (t < (SYNAPTICS_EDGE_MAX / 2))
589 1.3 scw return (EINVAL);
590 1.1 christos } else
591 1.3 scw if (node.sysctl_num == synaptics_scale_x_nodenum ||
592 1.3 scw node.sysctl_num == synaptics_scale_y_nodenum) {
593 1.3 scw if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
594 1.3 scw return (EINVAL);
595 1.3 scw } else
596 1.3 scw if (node.sysctl_num == synaptics_finger_high_nodenum) {
597 1.3 scw if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
598 1.3 scw t < synaptics_finger_low)
599 1.3 scw return (EINVAL);
600 1.3 scw } else
601 1.3 scw if (node.sysctl_num == synaptics_finger_low_nodenum) {
602 1.3 scw if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
603 1.3 scw t > synaptics_finger_high)
604 1.3 scw return (EINVAL);
605 1.3 scw } else
606 1.3 scw if (node.sysctl_num == synaptics_gesture_move_nodenum ||
607 1.3 scw node.sysctl_num == synaptics_movement_threshold_nodenum) {
608 1.3 scw if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
609 1.3 scw return (EINVAL);
610 1.3 scw } else
611 1.3 scw return (EINVAL);
612 1.1 christos
613 1.3 scw *(int *)rnode->sysctl_data = t;
614 1.1 christos
615 1.3 scw return (0);
616 1.1 christos }
617 1.1 christos
618 1.3 scw /* Masks for the first byte of a packet */
619 1.3 scw #define PMS_LBUTMASK 0x01
620 1.3 scw #define PMS_RBUTMASK 0x02
621 1.3 scw #define PMS_MBUTMASK 0x04
622 1.1 christos
623 1.1 christos static void
624 1.14 mlelstv pms_synaptics_parse(struct pms_softc *psc)
625 1.14 mlelstv {
626 1.14 mlelstv struct synaptics_softc *sc = &psc->u.synaptics;
627 1.14 mlelstv struct synaptics_packet sp;
628 1.14 mlelstv
629 1.14 mlelstv /* Absolute X/Y coordinates of finger */
630 1.14 mlelstv sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
631 1.14 mlelstv ((psc->packet[3] & 0x10) << 8);
632 1.14 mlelstv sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
633 1.14 mlelstv ((psc->packet[3] & 0x20) << 7);
634 1.14 mlelstv
635 1.14 mlelstv /* Pressure */
636 1.14 mlelstv sp.sp_z = psc->packet[2];
637 1.14 mlelstv
638 1.14 mlelstv /* Width of finger */
639 1.14 mlelstv sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
640 1.14 mlelstv ((psc->packet[0] & 0x04) >> 1) +
641 1.14 mlelstv ((psc->packet[3] & 0x04) >> 2);
642 1.14 mlelstv
643 1.14 mlelstv /* Left/Right button handling. */
644 1.14 mlelstv sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
645 1.14 mlelstv sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
646 1.14 mlelstv
647 1.14 mlelstv /* Up/Down buttons. */
648 1.14 mlelstv if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
649 1.14 mlelstv /* Old up/down buttons. */
650 1.14 mlelstv sp.sp_up = sp.sp_left ^
651 1.14 mlelstv (psc->packet[3] & PMS_LBUTMASK);
652 1.14 mlelstv sp.sp_down = sp.sp_right ^
653 1.14 mlelstv (psc->packet[3] & PMS_RBUTMASK);
654 1.14 mlelstv } else
655 1.14 mlelstv if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
656 1.14 mlelstv ((psc->packet[0] & PMS_RBUTMASK) ^
657 1.14 mlelstv (psc->packet[3] & PMS_RBUTMASK))) {
658 1.14 mlelstv /* New up/down button. */
659 1.14 mlelstv sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
660 1.14 mlelstv sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
661 1.14 mlelstv } else {
662 1.14 mlelstv sp.sp_up = 0;
663 1.14 mlelstv sp.sp_down = 0;
664 1.14 mlelstv }
665 1.14 mlelstv
666 1.14 mlelstv /* Middle button. */
667 1.14 mlelstv if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
668 1.14 mlelstv /* Old style Middle Button. */
669 1.14 mlelstv sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
670 1.14 mlelstv (psc->packet[3] & PMS_LBUTMASK);
671 1.14 mlelstv } else
672 1.14 mlelstv if (synaptics_up_down_emul == 1) {
673 1.14 mlelstv /* Do middle button emulation using up/down buttons */
674 1.14 mlelstv sp.sp_middle = sp.sp_up | sp.sp_down;
675 1.14 mlelstv sp.sp_up = sp.sp_down = 0;
676 1.14 mlelstv } else
677 1.14 mlelstv sp.sp_middle = 0;
678 1.14 mlelstv
679 1.14 mlelstv pms_synaptics_process_packet(psc, &sp);
680 1.14 mlelstv }
681 1.14 mlelstv
682 1.14 mlelstv static void
683 1.14 mlelstv pms_synaptics_passthrough(struct pms_softc *psc)
684 1.14 mlelstv {
685 1.14 mlelstv int dx, dy, dz;
686 1.14 mlelstv int buttons, changed;
687 1.14 mlelstv int s;
688 1.14 mlelstv
689 1.14 mlelstv buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
690 1.14 mlelstv ((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
691 1.14 mlelstv ((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);
692 1.14 mlelstv
693 1.14 mlelstv dx = psc->packet[4];
694 1.14 mlelstv if (dx >= 128)
695 1.14 mlelstv dx -= 256;
696 1.14 mlelstv if (dx == -128)
697 1.14 mlelstv dx = -127;
698 1.14 mlelstv
699 1.14 mlelstv dy = psc->packet[5];
700 1.14 mlelstv if (dy >= 128)
701 1.14 mlelstv dy -= 256;
702 1.14 mlelstv if (dy == -128)
703 1.14 mlelstv dy = -127;
704 1.14 mlelstv
705 1.14 mlelstv dz = 0;
706 1.14 mlelstv
707 1.14 mlelstv changed = buttons ^ (psc->buttons & 0xe0);
708 1.14 mlelstv psc->buttons ^= changed;
709 1.14 mlelstv
710 1.14 mlelstv if (dx || dy || dz || changed) {
711 1.14 mlelstv buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
712 1.14 mlelstv s = spltty();
713 1.14 mlelstv wsmouse_input(psc->sc_wsmousedev,
714 1.15 mlelstv buttons, dx, dy, dz, 0,
715 1.14 mlelstv WSMOUSE_INPUT_DELTA);
716 1.14 mlelstv splx(s);
717 1.14 mlelstv }
718 1.14 mlelstv }
719 1.14 mlelstv
720 1.14 mlelstv static void
721 1.1 christos pms_synaptics_input(void *vsc, int data)
722 1.1 christos {
723 1.3 scw struct pms_softc *psc = vsc;
724 1.3 scw struct timeval diff;
725 1.1 christos
726 1.3 scw if (!psc->sc_enabled) {
727 1.1 christos /* Interrupts are not expected. Discard the byte. */
728 1.1 christos return;
729 1.1 christos }
730 1.1 christos
731 1.10 kardel getmicrouptime(&psc->current);
732 1.1 christos
733 1.3 scw if (psc->inputstate > 0) {
734 1.3 scw timersub(&psc->current, &psc->last, &diff);
735 1.1 christos if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
736 1.20 cube aprint_debug_dev(psc->sc_dev,
737 1.20 cube "pms_input: unusual delay (%ld.%06ld s), "
738 1.20 cube "scheduling reset\n",
739 1.1 christos (long)diff.tv_sec, (long)diff.tv_usec);
740 1.3 scw psc->inputstate = 0;
741 1.3 scw psc->sc_enabled = 0;
742 1.3 scw wakeup(&psc->sc_enabled);
743 1.1 christos return;
744 1.1 christos }
745 1.1 christos }
746 1.3 scw psc->last = psc->current;
747 1.1 christos
748 1.3 scw switch (psc->inputstate) {
749 1.3 scw case 0:
750 1.1 christos if ((data & 0xc8) != 0x80) {
751 1.26 cegger aprint_debug_dev(psc->sc_dev,
752 1.20 cube "pms_input: 0x%02x out of sync\n", data);
753 1.1 christos return; /* not in sync yet, discard input */
754 1.1 christos }
755 1.3 scw /*FALLTHROUGH*/
756 1.3 scw
757 1.3 scw case 3:
758 1.5 perry if ((data & 8) == 8) {
759 1.26 cegger aprint_debug_dev(psc->sc_dev,
760 1.20 cube "pms_input: dropped in relative mode, reset\n");
761 1.3 scw psc->inputstate = 0;
762 1.3 scw psc->sc_enabled = 0;
763 1.3 scw wakeup(&psc->sc_enabled);
764 1.1 christos return;
765 1.1 christos }
766 1.1 christos }
767 1.1 christos
768 1.3 scw psc->packet[psc->inputstate++] = data & 0xff;
769 1.3 scw if (psc->inputstate == 6) {
770 1.3 scw /*
771 1.3 scw * We have a complete packet.
772 1.3 scw * Extract the pertinent details.
773 1.3 scw */
774 1.3 scw psc->inputstate = 0;
775 1.3 scw
776 1.14 mlelstv if ((psc->packet[0] & 0xfc) == 0x84 &&
777 1.14 mlelstv (psc->packet[3] & 0xcc) == 0xc4) {
778 1.14 mlelstv /* PS/2 passthrough */
779 1.14 mlelstv pms_synaptics_passthrough(psc);
780 1.3 scw } else {
781 1.14 mlelstv pms_synaptics_parse(psc);
782 1.1 christos }
783 1.1 christos }
784 1.3 scw }
785 1.1 christos
786 1.9 perry static inline int
787 1.3 scw synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
788 1.3 scw int *palmp)
789 1.3 scw {
790 1.3 scw int fingers;
791 1.3 scw
792 1.3 scw /* Assume no palm */
793 1.3 scw *palmp = 0;
794 1.3 scw
795 1.3 scw /*
796 1.3 scw * Apply some hysteresis when checking for a finger.
797 1.3 scw * When the finger is first applied, we ignore it until the
798 1.3 scw * pressure exceeds the 'high' threshold. The finger is considered
799 1.3 scw * removed only when pressure falls beneath the 'low' threshold.
800 1.3 scw */
801 1.3 scw if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
802 1.3 scw (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
803 1.3 scw fingers = 1;
804 1.3 scw else
805 1.3 scw fingers = 0;
806 1.3 scw
807 1.3 scw /*
808 1.3 scw * If the pad can't do palm detection, skip the rest.
809 1.3 scw */
810 1.3 scw if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
811 1.3 scw return (fingers);
812 1.3 scw
813 1.3 scw /*
814 1.3 scw * Palm detection
815 1.3 scw */
816 1.3 scw if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
817 1.3 scw sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
818 1.3 scw *palmp = 1;
819 1.3 scw
820 1.3 scw if (sc->prev_fingers == 0 &&
821 1.3 scw (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
822 1.3 scw sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
823 1.3 scw /*
824 1.3 scw * Contact area or pressure is too great to be a finger.
825 1.3 scw * Just ignore it for now.
826 1.3 scw */
827 1.3 scw return (0);
828 1.3 scw }
829 1.3 scw
830 1.3 scw /*
831 1.3 scw * Detect 2 and 3 fingers if supported, but only if multiple
832 1.3 scw * fingers appear within the tap gesture time period.
833 1.3 scw */
834 1.3 scw if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
835 1.3 scw SYN_TIME(sc, sc->gesture_start_packet) < synaptics_gesture_length) {
836 1.3 scw switch (sp->sp_w) {
837 1.3 scw case SYNAPTICS_WIDTH_TWO_FINGERS:
838 1.3 scw fingers = 2;
839 1.3 scw break;
840 1.3 scw
841 1.3 scw case SYNAPTICS_WIDTH_THREE_OR_MORE:
842 1.3 scw fingers = 3;
843 1.3 scw break;
844 1.3 scw
845 1.3 scw case SYNAPTICS_WIDTH_PEN:
846 1.3 scw fingers = 1;
847 1.3 scw break;
848 1.3 scw
849 1.3 scw default:
850 1.3 scw /*
851 1.3 scw * The width value can report spurious single-finger
852 1.3 scw * events after a multi-finger event.
853 1.3 scw */
854 1.3 scw if (sc->prev_fingers > 1)
855 1.3 scw fingers = sc->prev_fingers;
856 1.3 scw else
857 1.3 scw fingers = 1;
858 1.3 scw break;
859 1.1 christos }
860 1.1 christos }
861 1.3 scw
862 1.3 scw return (fingers);
863 1.1 christos }
864 1.1 christos
865 1.9 perry static inline void
866 1.3 scw synaptics_gesture_detect(struct synaptics_softc *sc,
867 1.3 scw struct synaptics_packet *sp, int fingers)
868 1.3 scw {
869 1.3 scw int gesture_len, gesture_move_x, gesture_move_y, gesture_buttons;
870 1.3 scw int set_buttons;
871 1.3 scw
872 1.3 scw gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
873 1.3 scw gesture_buttons = sc->gesture_buttons;
874 1.1 christos
875 1.3 scw if (fingers && sc->prev_fingers == 0) {
876 1.3 scw /*
877 1.3 scw * Finger was just applied.
878 1.3 scw * If the previous gesture was a single-click, set things
879 1.3 scw * up to deal with a possible drag or double-click gesture.
880 1.3 scw * Basically, if the finger is removed again within
881 1.3 scw * 'synaptics_gesture_length' packets, this is treated
882 1.3 scw * as a double-click. Otherwise we will emulate holding
883 1.3 scw * the left button down whilst dragging the mouse.
884 1.3 scw */
885 1.3 scw if (SYN_IS_SINGLE_TAP(sc->gesture_type))
886 1.3 scw sc->gesture_type |= SYN_GESTURE_DRAG;
887 1.3 scw
888 1.3 scw sc->gesture_start_x = sp->sp_x;
889 1.3 scw sc->gesture_start_y = sp->sp_y;
890 1.3 scw sc->gesture_start_packet = sc->total_packets;
891 1.3 scw } else
892 1.3 scw if (fingers == 0 && sc->prev_fingers != 0) {
893 1.3 scw /*
894 1.3 scw * Finger was just removed.
895 1.3 scw * Check if the contact time and finger movement were
896 1.3 scw * small enough to qualify as a gesture.
897 1.3 scw * Ignore finger movement if multiple fingers were
898 1.3 scw * detected (the pad may report coordinates for any
899 1.3 scw * of the fingers).
900 1.3 scw */
901 1.3 scw gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
902 1.3 scw gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
903 1.3 scw
904 1.3 scw if (gesture_len < synaptics_gesture_length &&
905 1.3 scw (sc->prev_fingers > 1 ||
906 1.3 scw (gesture_move_x < synaptics_gesture_move &&
907 1.3 scw gesture_move_y < synaptics_gesture_move))) {
908 1.3 scw /*
909 1.3 scw * Looking good so far.
910 1.3 scw */
911 1.3 scw if (SYN_IS_DRAG(sc->gesture_type)) {
912 1.3 scw /*
913 1.3 scw * Promote this gesture to double-click.
914 1.3 scw */
915 1.3 scw sc->gesture_type |= SYN_GESTURE_DOUBLE;
916 1.3 scw sc->gesture_type &= ~SYN_GESTURE_SINGLE;
917 1.3 scw } else {
918 1.3 scw /*
919 1.3 scw * Single tap gesture. Set the tap length timer
920 1.3 scw * and flag a single-click.
921 1.3 scw */
922 1.3 scw sc->gesture_tap_packet = sc->total_packets;
923 1.3 scw sc->gesture_type |= SYN_GESTURE_SINGLE;
924 1.3 scw
925 1.3 scw /*
926 1.3 scw * The gesture can be modified depending on
927 1.3 scw * the number of fingers detected.
928 1.3 scw *
929 1.3 scw * 1: Normal left button emulation.
930 1.3 scw * 2: Either middle button or right button
931 1.3 scw * depending on the value of the two_fingers
932 1.3 scw * sysctl variable.
933 1.3 scw * 3: Right button.
934 1.3 scw */
935 1.3 scw switch (sc->prev_fingers) {
936 1.3 scw case 2:
937 1.3 scw if (synaptics_two_fingers_emul == 1)
938 1.3 scw gesture_buttons |= PMS_RBUTMASK;
939 1.3 scw else
940 1.3 scw if (synaptics_two_fingers_emul == 2)
941 1.3 scw gesture_buttons |= PMS_MBUTMASK;
942 1.3 scw break;
943 1.3 scw case 3:
944 1.3 scw gesture_buttons |= PMS_RBUTMASK;
945 1.3 scw break;
946 1.3 scw default:
947 1.3 scw gesture_buttons |= PMS_LBUTMASK;
948 1.3 scw break;
949 1.3 scw }
950 1.1 christos }
951 1.1 christos }
952 1.1 christos
953 1.3 scw /*
954 1.3 scw * Always clear drag state when the finger is removed.
955 1.3 scw */
956 1.3 scw sc->gesture_type &= ~SYN_GESTURE_DRAG;
957 1.3 scw }
958 1.3 scw
959 1.3 scw if (sc->gesture_type == 0) {
960 1.3 scw /*
961 1.3 scw * There is no gesture in progress.
962 1.3 scw * Clear emulated button state.
963 1.3 scw */
964 1.3 scw sc->gesture_buttons = 0;
965 1.3 scw return;
966 1.3 scw }
967 1.3 scw
968 1.3 scw /*
969 1.3 scw * A gesture is in progress.
970 1.3 scw */
971 1.3 scw set_buttons = 0;
972 1.3 scw
973 1.3 scw if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
974 1.3 scw /*
975 1.3 scw * Single-click.
976 1.3 scw * Activate the relevant button(s) until the
977 1.3 scw * gesture tap timer has expired.
978 1.3 scw */
979 1.3 scw if (SYN_TIME(sc, sc->gesture_tap_packet) <
980 1.3 scw synaptics_gesture_length)
981 1.3 scw set_buttons = 1;
982 1.3 scw else
983 1.3 scw sc->gesture_type &= ~SYN_GESTURE_SINGLE;
984 1.3 scw } else
985 1.3 scw if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
986 1.3 scw /*
987 1.3 scw * Double-click.
988 1.3 scw * Activate the relevant button(s) once.
989 1.3 scw */
990 1.3 scw set_buttons = 1;
991 1.3 scw sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
992 1.3 scw }
993 1.3 scw
994 1.3 scw if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
995 1.3 scw /*
996 1.3 scw * Single-click and drag.
997 1.3 scw * Maintain button state until the finger is removed.
998 1.3 scw */
999 1.3 scw sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
1000 1.3 scw sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
1001 1.3 scw sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
1002 1.3 scw }
1003 1.3 scw
1004 1.3 scw sc->gesture_buttons = gesture_buttons;
1005 1.3 scw }
1006 1.3 scw
1007 1.9 perry static inline int
1008 1.3 scw synaptics_filter_policy(struct synaptics_softc *sc, int *history, int value)
1009 1.3 scw {
1010 1.3 scw int a, b, rv, count;
1011 1.1 christos
1012 1.3 scw count = sc->total_packets;
1013 1.3 scw
1014 1.3 scw /*
1015 1.3 scw * Once we've accumulated at least SYN_HIST_SIZE values, combine
1016 1.3 scw * each new value with the previous two and return the average.
1017 1.3 scw *
1018 1.3 scw * This is necessary when the touchpad is operating in 80 packets
1019 1.3 scw * per second mode, as it performs little internal filtering on
1020 1.3 scw * reported values.
1021 1.3 scw *
1022 1.3 scw * Using a rolling average helps to filter out jitter caused by
1023 1.3 scw * tiny finger movements.
1024 1.3 scw */
1025 1.3 scw if (sc->movement_history >= SYN_HIST_SIZE) {
1026 1.3 scw a = (history[(count + 0) % SYN_HIST_SIZE] +
1027 1.3 scw history[(count + 1) % SYN_HIST_SIZE]) / 2;
1028 1.3 scw
1029 1.3 scw b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
1030 1.3 scw
1031 1.3 scw rv = b - a;
1032 1.3 scw
1033 1.3 scw /*
1034 1.3 scw * Don't report the movement if it's below a certain
1035 1.3 scw * threshold.
1036 1.3 scw */
1037 1.3 scw if (abs(rv) < synaptics_movement_threshold)
1038 1.3 scw rv = 0;
1039 1.3 scw } else
1040 1.3 scw rv = 0;
1041 1.3 scw
1042 1.3 scw /*
1043 1.3 scw * Add the new value to the history buffer.
1044 1.3 scw */
1045 1.3 scw history[(count + 1) % SYN_HIST_SIZE] = value;
1046 1.3 scw
1047 1.3 scw return (rv);
1048 1.3 scw }
1049 1.3 scw
1050 1.3 scw /* Edge detection */
1051 1.3 scw #define SYN_EDGE_TOP 1
1052 1.3 scw #define SYN_EDGE_BOTTOM 2
1053 1.3 scw #define SYN_EDGE_LEFT 4
1054 1.3 scw #define SYN_EDGE_RIGHT 8
1055 1.3 scw
1056 1.9 perry static inline int
1057 1.3 scw synaptics_check_edge(int x, int y)
1058 1.3 scw {
1059 1.3 scw int rv = 0;
1060 1.3 scw
1061 1.3 scw if (x < synaptics_edge_left)
1062 1.3 scw rv |= SYN_EDGE_LEFT;
1063 1.3 scw else
1064 1.3 scw if (x > synaptics_edge_right)
1065 1.3 scw rv |= SYN_EDGE_RIGHT;
1066 1.3 scw
1067 1.3 scw if (y < synaptics_edge_bottom)
1068 1.3 scw rv |= SYN_EDGE_BOTTOM;
1069 1.3 scw else
1070 1.3 scw if (y > synaptics_edge_top)
1071 1.3 scw rv |= SYN_EDGE_TOP;
1072 1.3 scw
1073 1.3 scw return (rv);
1074 1.3 scw }
1075 1.3 scw
1076 1.9 perry static inline int
1077 1.13 christos synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
1078 1.3 scw {
1079 1.3 scw
1080 1.3 scw /*
1081 1.3 scw * When edge motion is enabled, synaptics_edge_motion_delta is
1082 1.3 scw * combined with the current delta, together with the direction
1083 1.3 scw * in which to simulate the motion. The result is added to
1084 1.3 scw * the delta derived from finger movement. This provides a smooth
1085 1.3 scw * transition from finger movement to edge motion.
1086 1.3 scw */
1087 1.3 scw delta = synaptics_edge_motion_delta + (dir * delta);
1088 1.3 scw if (delta < 0)
1089 1.3 scw return (0);
1090 1.3 scw if (delta > synaptics_edge_motion_delta)
1091 1.3 scw return (synaptics_edge_motion_delta);
1092 1.3 scw return (delta);
1093 1.3 scw }
1094 1.3 scw
1095 1.9 perry static inline int
1096 1.3 scw synaptics_scale(int delta, int scale, int *remp)
1097 1.3 scw {
1098 1.3 scw int rv;
1099 1.3 scw
1100 1.3 scw /*
1101 1.3 scw * Scale the raw delta in Synaptics coordinates (0-6143) into
1102 1.3 scw * something more reasonable by dividing the raw delta by a
1103 1.3 scw * scale factor. Any remainder from the previous scale result
1104 1.3 scw * is added to the current delta before scaling.
1105 1.3 scw * This prevents loss of resolution for very small/slow
1106 1.3 scw * movements of the finger.
1107 1.3 scw */
1108 1.3 scw delta += *remp;
1109 1.3 scw rv = delta / scale;
1110 1.3 scw *remp = delta % scale;
1111 1.3 scw
1112 1.3 scw return (rv);
1113 1.3 scw }
1114 1.3 scw
1115 1.9 perry static inline void
1116 1.3 scw synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
1117 1.3 scw int *dxp, int *dyp)
1118 1.3 scw {
1119 1.3 scw int dx, dy, edge;
1120 1.3 scw
1121 1.3 scw /*
1122 1.3 scw * Compute the next values of dx and dy
1123 1.3 scw */
1124 1.3 scw dx = synaptics_filter_policy(sc, sc->history_x, sp->sp_x);
1125 1.3 scw dy = synaptics_filter_policy(sc, sc->history_y, sp->sp_y);
1126 1.3 scw
1127 1.3 scw /*
1128 1.3 scw * If we're dealing with a drag gesture, and the finger moves to
1129 1.3 scw * the edge of the touchpad, apply edge motion emulation if it
1130 1.3 scw * is enabled.
1131 1.3 scw */
1132 1.3 scw if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
1133 1.3 scw edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
1134 1.3 scw
1135 1.3 scw if (edge & SYN_EDGE_LEFT)
1136 1.3 scw dx -= synaptics_edge_motion(sc, dx, 1);
1137 1.3 scw if (edge & SYN_EDGE_RIGHT)
1138 1.3 scw dx += synaptics_edge_motion(sc, dx, -1);
1139 1.3 scw if (edge & SYN_EDGE_BOTTOM)
1140 1.3 scw dy -= synaptics_edge_motion(sc, dy, 1);
1141 1.3 scw if (edge & SYN_EDGE_TOP)
1142 1.3 scw dy += synaptics_edge_motion(sc, dy, -1);
1143 1.3 scw }
1144 1.3 scw
1145 1.3 scw /*
1146 1.3 scw * Apply scaling to both deltas
1147 1.3 scw */
1148 1.3 scw dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x);
1149 1.3 scw dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y);
1150 1.1 christos
1151 1.3 scw /*
1152 1.3 scw * Clamp deltas to specified maximums.
1153 1.3 scw */
1154 1.3 scw if (dx > synaptics_max_speed_x)
1155 1.3 scw dx = synaptics_max_speed_x;
1156 1.3 scw if (dy > synaptics_max_speed_y)
1157 1.3 scw dy = synaptics_max_speed_y;
1158 1.1 christos
1159 1.3 scw *dxp = dx;
1160 1.3 scw *dyp = dy;
1161 1.1 christos
1162 1.3 scw sc->movement_history++;
1163 1.3 scw }
1164 1.1 christos
1165 1.3 scw static void
1166 1.3 scw pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
1167 1.3 scw {
1168 1.3 scw struct synaptics_softc *sc = &psc->u.synaptics;
1169 1.3 scw int dx, dy, dz;
1170 1.3 scw int fingers, palm, buttons, changed;
1171 1.3 scw int s;
1172 1.1 christos
1173 1.3 scw /*
1174 1.3 scw * Do Z-axis emulation using up/down buttons if required.
1175 1.3 scw * Note that the pad will send a one second burst of packets
1176 1.3 scw * when an up/down button is pressed and held. At the moment
1177 1.3 scw * we don't deal with auto-repeat, so convert the burst into
1178 1.3 scw * a one-shot.
1179 1.3 scw */
1180 1.3 scw dz = 0;
1181 1.3 scw if (synaptics_up_down_emul == 2) {
1182 1.3 scw if (sc->up_down == 0) {
1183 1.3 scw if (sp->sp_up && sp->sp_down) {
1184 1.3 scw /*
1185 1.3 scw * Most up/down buttons will be actuated using
1186 1.3 scw * a rocker switch, so we should never see
1187 1.3 scw * them both simultaneously. But just in case,
1188 1.3 scw * treat this situation as a middle button
1189 1.3 scw * event.
1190 1.3 scw */
1191 1.3 scw sp->sp_middle = 1;
1192 1.3 scw } else
1193 1.3 scw if (sp->sp_up)
1194 1.3 scw dz = -synaptics_up_down_motion_delta;
1195 1.3 scw else
1196 1.3 scw if (sp->sp_down)
1197 1.3 scw dz = synaptics_up_down_motion_delta;
1198 1.1 christos }
1199 1.1 christos
1200 1.3 scw sc->up_down = sp->sp_up | sp->sp_down;
1201 1.3 scw sp->sp_up = sp->sp_down = 0;
1202 1.3 scw }
1203 1.3 scw
1204 1.3 scw /*
1205 1.3 scw * Determine whether or not a finger is on the pad.
1206 1.3 scw * On some pads, this will return the number of fingers
1207 1.3 scw * detected.
1208 1.3 scw */
1209 1.3 scw fingers = synaptics_finger_detect(sc, sp, &palm);
1210 1.3 scw
1211 1.3 scw /*
1212 1.3 scw * Do gesture processing only if we didn't detect a palm.
1213 1.3 scw */
1214 1.3 scw if (palm == 0)
1215 1.3 scw synaptics_gesture_detect(sc, sp, fingers);
1216 1.3 scw else
1217 1.3 scw sc->gesture_type = sc->gesture_buttons = 0;
1218 1.1 christos
1219 1.3 scw /*
1220 1.3 scw * Determine what buttons to report
1221 1.3 scw */
1222 1.3 scw buttons = (sp->sp_left ? 0x1 : 0) |
1223 1.3 scw (sp->sp_middle ? 0x2 : 0) |
1224 1.3 scw (sp->sp_right ? 0x4 : 0) |
1225 1.3 scw (sp->sp_up ? 0x8 : 0) |
1226 1.3 scw (sp->sp_down ? 0x10 : 0);
1227 1.14 mlelstv changed = buttons ^ (psc->buttons & 0x1f);
1228 1.14 mlelstv psc->buttons ^= changed;
1229 1.1 christos
1230 1.3 scw sc->prev_fingers = fingers;
1231 1.3 scw sc->total_packets++;
1232 1.1 christos
1233 1.3 scw /*
1234 1.3 scw * Do movement processing IFF we have a single finger and no palm.
1235 1.3 scw */
1236 1.3 scw if (fingers == 1 && palm == 0)
1237 1.3 scw synaptics_movement(sc, sp, &dx, &dy);
1238 1.3 scw else {
1239 1.3 scw /*
1240 1.3 scw * No valid finger. Therefore no movement.
1241 1.3 scw */
1242 1.3 scw sc->movement_history = 0;
1243 1.3 scw sc->rem_x = sc->rem_y = 0;
1244 1.3 scw dx = dy = 0;
1245 1.3 scw }
1246 1.1 christos
1247 1.3 scw /*
1248 1.3 scw * Pass the final results up to wsmouse_input() if necessary.
1249 1.3 scw */
1250 1.3 scw if (dx || dy || dz || changed) {
1251 1.14 mlelstv buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
1252 1.3 scw s = spltty();
1253 1.12 plunky wsmouse_input(psc->sc_wsmousedev,
1254 1.12 plunky buttons,
1255 1.12 plunky dx, dy, dz, 0,
1256 1.12 plunky WSMOUSE_INPUT_DELTA);
1257 1.3 scw splx(s);
1258 1.1 christos }
1259 1.1 christos }
1260