xref: /netbsd-src/sys/dev/pckbport/synaptics.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: synaptics.c,v 1.10 2006/06/07 22:33:37 kardel 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  *	- Support pass-through mode (whatever that is; my docs are too old).
47  */
48 
49 #include "opt_pms.h"
50 
51 #include <sys/cdefs.h>
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 <machine/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) /*XXX: Not supported*/
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_input(void *vsc, int data)
648 {
649 	struct pms_softc *psc = vsc;
650 	struct synaptics_softc *sc = &psc->u.synaptics;
651 	struct timeval diff;
652 	struct synaptics_packet sp;
653 
654 	if (!psc->sc_enabled) {
655 		/* Interrupts are not expected.	 Discard the byte. */
656 		return;
657 	}
658 
659 	getmicrouptime(&psc->current);
660 
661 	if (psc->inputstate > 0) {
662 		timersub(&psc->current, &psc->last, &diff);
663 		if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
664 			printf("%s: pms_input: unusual delay (%ld.%06ld s), "
665 			    "scheduling reset\n", psc->sc_dev.dv_xname,
666 			    (long)diff.tv_sec, (long)diff.tv_usec);
667 			psc->inputstate = 0;
668 			psc->sc_enabled = 0;
669 			wakeup(&psc->sc_enabled);
670 			return;
671 		}
672 	}
673 	psc->last = psc->current;
674 
675 	switch (psc->inputstate) {
676 	case 0:
677 		if ((data & 0xc8) != 0x80) {
678 #ifdef SYNAPTICSDEBUG
679 			printf("%s: pms_input: 0x%02x out of sync\n",
680 			    psc->sc_dev.dv_xname, data);
681 #endif
682 			return;	/* not in sync yet, discard input */
683 		}
684 		/*FALLTHROUGH*/
685 
686 	case 3:
687 		if ((data & 8) == 8) {
688 #ifdef SYNAPTICSDEBUG
689 			printf("%s: pms_input: dropped in relative mode, "
690 			    "reset\n", psc->sc_dev.dv_xname);
691 #endif
692 			psc->inputstate = 0;
693 			psc->sc_enabled = 0;
694 			wakeup(&psc->sc_enabled);
695 			return;
696 		}
697 	}
698 
699 	psc->packet[psc->inputstate++] = data & 0xff;
700 	if (psc->inputstate == 6) {
701 		/*
702 		 * We have a complete packet.
703 		 * Extract the pertinent details.
704 		 */
705 		psc->inputstate = 0;
706 
707 		/* Absolute X/Y coordinates of finger */
708 		sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
709 		    ((psc->packet[3] & 0x10) << 8);
710 		sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
711 		    ((psc->packet[3] & 0x20) << 7);
712 
713 		/* Pressure */
714 		sp.sp_z = psc->packet[2];
715 
716 		/* Width of finger */
717 		sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
718 		    ((psc->packet[0] & 0x04) >> 1) +
719 		    ((psc->packet[3] & 0x04) >> 2);
720 
721 		/* Left/Right button handling. */
722 		sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
723 		sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
724 
725 		/* Up/Down buttons. */
726 		if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
727 			/* Old up/down buttons. */
728 			sp.sp_up = sp.sp_left ^
729 			    (psc->packet[3] & PMS_LBUTMASK);
730 			sp.sp_down = sp.sp_right ^
731 			    (psc->packet[3] & PMS_RBUTMASK);
732 		} else
733 		if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
734 		    ((psc->packet[0] & PMS_RBUTMASK) ^
735 		    (psc->packet[3] & PMS_RBUTMASK))) {
736 			/* New up/down button. */
737 			sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
738 			sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
739 		} else {
740 			sp.sp_up = 0;
741 			sp.sp_down = 0;
742 		}
743 
744 		/* Middle button. */
745 		if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
746 			/* Old style Middle Button. */
747 			sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
748 			    (psc->packet[3] & PMS_LBUTMASK);
749 		} else
750 		if (synaptics_up_down_emul == 1) {
751 			/* Do middle button emulation using up/down buttons */
752 			sp.sp_middle = sp.sp_up | sp.sp_down;
753 			sp.sp_up = sp.sp_down = 0;
754 		} else
755 			sp.sp_middle = 0;
756 
757 		/*
758 		 * Go process the new packet
759 		 */
760 		pms_synaptics_process_packet(psc, &sp);
761 	}
762 }
763 
764 static inline int
765 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
766     int *palmp)
767 {
768 	int fingers;
769 
770 	/* Assume no palm */
771 	*palmp = 0;
772 
773 	/*
774 	 * Apply some hysteresis when checking for a finger.
775 	 * When the finger is first applied, we ignore it until the
776 	 * pressure exceeds the 'high' threshold. The finger is considered
777 	 * removed only when pressure falls beneath the 'low' threshold.
778 	 */
779 	if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
780 	    (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
781 		fingers = 1;
782 	else
783 		fingers = 0;
784 
785 	/*
786 	 * If the pad can't do palm detection, skip the rest.
787 	 */
788 	if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
789 		return (fingers);
790 
791 	/*
792 	 * Palm detection
793 	 */
794 	if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
795 	    sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
796 		*palmp = 1;
797 
798 	if (sc->prev_fingers == 0 &&
799 	    (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
800 	     sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
801 		/*
802 		 * Contact area or pressure is too great to be a finger.
803 		 * Just ignore it for now.
804 		 */
805 		return (0);
806 	}
807 
808 	/*
809 	 * Detect 2 and 3 fingers if supported, but only if multiple
810 	 * fingers appear within the tap gesture time period.
811 	 */
812 	if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
813 	    SYN_TIME(sc, sc->gesture_start_packet) < synaptics_gesture_length) {
814 		switch (sp->sp_w) {
815 		case SYNAPTICS_WIDTH_TWO_FINGERS:
816 			fingers = 2;
817 			break;
818 
819 		case SYNAPTICS_WIDTH_THREE_OR_MORE:
820 			fingers = 3;
821 			break;
822 
823 		case SYNAPTICS_WIDTH_PEN:
824 			fingers = 1;
825 			break;
826 
827 		default:
828 			/*
829 			 * The width value can report spurious single-finger
830 			 * events after a multi-finger event.
831 			 */
832 			if (sc->prev_fingers > 1)
833 				fingers = sc->prev_fingers;
834 			else
835 				fingers = 1;
836 			break;
837 		}
838 	}
839 
840 	return (fingers);
841 }
842 
843 static inline void
844 synaptics_gesture_detect(struct synaptics_softc *sc,
845     struct synaptics_packet *sp, int fingers)
846 {
847 	int gesture_len, gesture_move_x, gesture_move_y, gesture_buttons;
848 	int set_buttons;
849 
850 	gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
851 	gesture_buttons = sc->gesture_buttons;
852 
853 	if (fingers && sc->prev_fingers == 0) {
854 		/*
855 		 * Finger was just applied.
856 		 * If the previous gesture was a single-click, set things
857 		 * up to deal with a possible drag or double-click gesture.
858 		 * Basically, if the finger is removed again within
859 		 * 'synaptics_gesture_length' packets, this is treated
860 		 * as a double-click. Otherwise we will emulate holding
861 		 * the left button down whilst dragging the mouse.
862 		 */
863 		if (SYN_IS_SINGLE_TAP(sc->gesture_type))
864 			sc->gesture_type |= SYN_GESTURE_DRAG;
865 
866 		sc->gesture_start_x = sp->sp_x;
867 		sc->gesture_start_y = sp->sp_y;
868 		sc->gesture_start_packet = sc->total_packets;
869 	} else
870 	if (fingers == 0 && sc->prev_fingers != 0) {
871 		/*
872 		 * Finger was just removed.
873 		 * Check if the contact time and finger movement were
874 		 * small enough to qualify as a gesture.
875 		 * Ignore finger movement if multiple fingers were
876 		 * detected (the pad may report coordinates for any
877 		 * of the fingers).
878 		 */
879 		gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
880 		gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
881 
882 		if (gesture_len < synaptics_gesture_length &&
883 		    (sc->prev_fingers > 1 ||
884 		    (gesture_move_x < synaptics_gesture_move &&
885 		     gesture_move_y < synaptics_gesture_move))) {
886 			/*
887 			 * Looking good so far.
888 			 */
889 			if (SYN_IS_DRAG(sc->gesture_type)) {
890 				/*
891 				 * Promote this gesture to double-click.
892 				 */
893 				sc->gesture_type |= SYN_GESTURE_DOUBLE;
894 				sc->gesture_type &= ~SYN_GESTURE_SINGLE;
895 			} else {
896 				/*
897 				 * Single tap gesture. Set the tap length timer
898 				 * and flag a single-click.
899 				 */
900 				sc->gesture_tap_packet = sc->total_packets;
901 				sc->gesture_type |= SYN_GESTURE_SINGLE;
902 
903 				/*
904 				 * The gesture can be modified depending on
905 				 * the number of fingers detected.
906 				 *
907 				 * 1: Normal left button emulation.
908 				 * 2: Either middle button or right button
909 				 *    depending on the value of the two_fingers
910 				 *    sysctl variable.
911 				 * 3: Right button.
912 				 */
913 				switch (sc->prev_fingers) {
914 				case 2:
915 					if (synaptics_two_fingers_emul == 1)
916 						gesture_buttons |= PMS_RBUTMASK;
917 					else
918 					if (synaptics_two_fingers_emul == 2)
919 						gesture_buttons |= PMS_MBUTMASK;
920 					break;
921 				case 3:
922 					gesture_buttons |= PMS_RBUTMASK;
923 					break;
924 				default:
925 					gesture_buttons |= PMS_LBUTMASK;
926 					break;
927 				}
928 			}
929 		}
930 
931 		/*
932 		 * Always clear drag state when the finger is removed.
933 		 */
934 		sc->gesture_type &= ~SYN_GESTURE_DRAG;
935 	}
936 
937 	if (sc->gesture_type == 0) {
938 		/*
939 		 * There is no gesture in progress.
940 		 * Clear emulated button state.
941 		 */
942 		sc->gesture_buttons = 0;
943 		return;
944 	}
945 
946 	/*
947 	 * A gesture is in progress.
948 	 */
949 	set_buttons = 0;
950 
951 	if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
952 		/*
953 		 * Single-click.
954 		 * Activate the relevant button(s) until the
955 		 * gesture tap timer has expired.
956 		 */
957 		if (SYN_TIME(sc, sc->gesture_tap_packet) <
958 		    synaptics_gesture_length)
959 			set_buttons = 1;
960 		else
961 			sc->gesture_type &= ~SYN_GESTURE_SINGLE;
962 	} else
963 	if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
964 		/*
965 		 * Double-click.
966 		 * Activate the relevant button(s) once.
967 		 */
968 		set_buttons = 1;
969 		sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
970 	}
971 
972 	if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
973 		/*
974 		 * Single-click and drag.
975 		 * Maintain button state until the finger is removed.
976 		 */
977 		sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
978 		sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
979 		sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
980 	}
981 
982 	sc->gesture_buttons = gesture_buttons;
983 }
984 
985 static inline int
986 synaptics_filter_policy(struct synaptics_softc *sc, int *history, int value)
987 {
988 	int a, b, rv, count;
989 
990 	count = sc->total_packets;
991 
992 	/*
993 	 * Once we've accumulated at least SYN_HIST_SIZE values, combine
994 	 * each new value with the previous two and return the average.
995 	 *
996 	 * This is necessary when the touchpad is operating in 80 packets
997 	 * per second mode, as it performs little internal filtering on
998 	 * reported values.
999 	 *
1000 	 * Using a rolling average helps to filter out jitter caused by
1001 	 * tiny finger movements.
1002 	 */
1003 	if (sc->movement_history >= SYN_HIST_SIZE) {
1004 		a = (history[(count + 0) % SYN_HIST_SIZE] +
1005 		    history[(count + 1) % SYN_HIST_SIZE]) / 2;
1006 
1007 		b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
1008 
1009 		rv = b - a;
1010 
1011 		/*
1012 		 * Don't report the movement if it's below a certain
1013 		 * threshold.
1014 		 */
1015 		if (abs(rv) < synaptics_movement_threshold)
1016 			rv = 0;
1017 	} else
1018 		rv = 0;
1019 
1020 	/*
1021 	 * Add the new value to the history buffer.
1022 	 */
1023 	history[(count + 1) % SYN_HIST_SIZE] = value;
1024 
1025 	return (rv);
1026 }
1027 
1028 /* Edge detection */
1029 #define	SYN_EDGE_TOP		1
1030 #define	SYN_EDGE_BOTTOM		2
1031 #define	SYN_EDGE_LEFT		4
1032 #define	SYN_EDGE_RIGHT		8
1033 
1034 static inline int
1035 synaptics_check_edge(int x, int y)
1036 {
1037 	int rv = 0;
1038 
1039 	if (x < synaptics_edge_left)
1040 		rv |= SYN_EDGE_LEFT;
1041 	else
1042 	if (x > synaptics_edge_right)
1043 		rv |= SYN_EDGE_RIGHT;
1044 
1045 	if (y < synaptics_edge_bottom)
1046 		rv |= SYN_EDGE_BOTTOM;
1047 	else
1048 	if (y > synaptics_edge_top)
1049 		rv |= SYN_EDGE_TOP;
1050 
1051 	return (rv);
1052 }
1053 
1054 static inline int
1055 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
1056 {
1057 
1058 	/*
1059 	 * When edge motion is enabled, synaptics_edge_motion_delta is
1060 	 * combined with the current delta, together with the direction
1061 	 * in which to simulate the motion. The result is added to
1062 	 * the delta derived from finger movement. This provides a smooth
1063 	 * transition from finger movement to edge motion.
1064 	 */
1065 	delta = synaptics_edge_motion_delta + (dir * delta);
1066 	if (delta < 0)
1067 		return (0);
1068 	if (delta > synaptics_edge_motion_delta)
1069 		return (synaptics_edge_motion_delta);
1070 	return (delta);
1071 }
1072 
1073 static inline int
1074 synaptics_scale(int delta, int scale, int *remp)
1075 {
1076 	int rv;
1077 
1078 	/*
1079 	 * Scale the raw delta in Synaptics coordinates (0-6143) into
1080 	 * something more reasonable by dividing the raw delta by a
1081 	 * scale factor. Any remainder from the previous scale result
1082 	 * is added to the current delta before scaling.
1083 	 * This prevents loss of resolution for very small/slow
1084 	 * movements of the finger.
1085 	 */
1086 	delta += *remp;
1087 	rv = delta / scale;
1088 	*remp = delta % scale;
1089 
1090 	return (rv);
1091 }
1092 
1093 static inline void
1094 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
1095     int *dxp, int *dyp)
1096 {
1097 	int dx, dy, edge;
1098 
1099 	/*
1100 	 * Compute the next values of dx and dy
1101 	 */
1102 	dx = synaptics_filter_policy(sc, sc->history_x, sp->sp_x);
1103 	dy = synaptics_filter_policy(sc, sc->history_y, sp->sp_y);
1104 
1105 	/*
1106 	 * If we're dealing with a drag gesture, and the finger moves to
1107 	 * the edge of the touchpad, apply edge motion emulation if it
1108 	 * is enabled.
1109 	 */
1110 	if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
1111 		edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
1112 
1113 		if (edge & SYN_EDGE_LEFT)
1114 			dx -= synaptics_edge_motion(sc, dx, 1);
1115 		if (edge & SYN_EDGE_RIGHT)
1116 			dx += synaptics_edge_motion(sc, dx, -1);
1117 		if (edge & SYN_EDGE_BOTTOM)
1118 			dy -= synaptics_edge_motion(sc, dy, 1);
1119 		if (edge & SYN_EDGE_TOP)
1120 			dy += synaptics_edge_motion(sc, dy, -1);
1121 	}
1122 
1123 	/*
1124 	 * Apply scaling to both deltas
1125 	 */
1126 	dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x);
1127 	dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y);
1128 
1129 	/*
1130 	 * Clamp deltas to specified maximums.
1131 	 */
1132 	if (dx > synaptics_max_speed_x)
1133 		dx = synaptics_max_speed_x;
1134 	if (dy > synaptics_max_speed_y)
1135 		dy = synaptics_max_speed_y;
1136 
1137 	*dxp = dx;
1138 	*dyp = dy;
1139 
1140 	sc->movement_history++;
1141 }
1142 
1143 static void
1144 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
1145 {
1146 	struct synaptics_softc *sc = &psc->u.synaptics;
1147 	int dx, dy, dz;
1148 	int fingers, palm, buttons, changed;
1149 	int s;
1150 
1151 	/*
1152 	 * Do Z-axis emulation using up/down buttons if required.
1153 	 * Note that the pad will send a one second burst of packets
1154 	 * when an up/down button is pressed and held. At the moment
1155 	 * we don't deal with auto-repeat, so convert the burst into
1156 	 * a one-shot.
1157 	 */
1158 	dz = 0;
1159 	if (synaptics_up_down_emul == 2) {
1160 		if (sc->up_down == 0) {
1161 			if (sp->sp_up && sp->sp_down) {
1162 				/*
1163 				 * Most up/down buttons will be actuated using
1164 				 * a rocker switch, so we should never see
1165 				 * them both simultaneously. But just in case,
1166 				 * treat this situation as a middle button
1167 				 * event.
1168 				 */
1169 				sp->sp_middle = 1;
1170 			} else
1171 			if (sp->sp_up)
1172 				dz = -synaptics_up_down_motion_delta;
1173 			else
1174 			if (sp->sp_down)
1175 				dz = synaptics_up_down_motion_delta;
1176 		}
1177 
1178 		sc->up_down = sp->sp_up | sp->sp_down;
1179 		sp->sp_up = sp->sp_down = 0;
1180 	}
1181 
1182 	/*
1183 	 * Determine whether or not a finger is on the pad.
1184 	 * On some pads, this will return the number of fingers
1185 	 * detected.
1186 	 */
1187 	fingers = synaptics_finger_detect(sc, sp, &palm);
1188 
1189 	/*
1190 	 * Do gesture processing only if we didn't detect a palm.
1191 	 */
1192 	if (palm == 0)
1193 		synaptics_gesture_detect(sc, sp, fingers);
1194 	else
1195 		sc->gesture_type = sc->gesture_buttons = 0;
1196 
1197 	/*
1198 	 * Determine what buttons to report
1199 	 */
1200 	buttons = (sp->sp_left ? 0x1 : 0) |
1201 	    (sp->sp_middle ? 0x2 : 0) |
1202 	    (sp->sp_right ? 0x4 : 0) |
1203 	    (sp->sp_up ? 0x8 : 0) |
1204 	    (sp->sp_down ? 0x10 : 0);
1205 	changed = buttons ^ psc->buttons;
1206 	psc->buttons = buttons;
1207 
1208 	sc->prev_fingers = fingers;
1209 	sc->total_packets++;
1210 
1211 	/*
1212 	 * Do movement processing IFF we have a single finger and no palm.
1213 	 */
1214 	if (fingers == 1 && palm == 0)
1215 		synaptics_movement(sc, sp, &dx, &dy);
1216 	else {
1217 		/*
1218 		 * No valid finger. Therefore no movement.
1219 		 */
1220 		sc->movement_history = 0;
1221 		sc->rem_x = sc->rem_y = 0;
1222 		dx = dy = 0;
1223 	}
1224 
1225 	/*
1226 	 * Pass the final results up to wsmouse_input() if necessary.
1227 	 */
1228 	if (dx || dy || dz || changed) {
1229 		s = spltty();
1230 		wsmouse_input(psc->sc_wsmousedev, buttons, dx, dy, dz,
1231 		    WSMOUSE_INPUT_DELTA);
1232 		splx(s);
1233 	}
1234 }
1235