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