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