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