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