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