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