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