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