xref: /netbsd-src/sys/dev/pckbport/synaptics.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: synaptics.c,v 1.42 2018/07/14 00:47:33 maya 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.42 2018/07/14 00:47:33 maya 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 #include <sys/proc.h>
60 
61 #include <sys/bus.h>
62 
63 #include <dev/pckbport/pckbportvar.h>
64 
65 #include <dev/pckbport/synapticsreg.h>
66 #include <dev/pckbport/synapticsvar.h>
67 
68 #include <dev/pckbport/pmsreg.h>
69 #include <dev/pckbport/pmsvar.h>
70 
71 #include <dev/wscons/wsconsio.h>
72 #include <dev/wscons/wsmousevar.h>
73 
74 /*
75  * Absolute-mode packets are decoded and passed around using
76  * the following structure.
77  */
78 struct synaptics_packet {
79 	signed short	sp_x;	/* Unscaled absolute X/Y coordinates */
80 	signed short	sp_y;
81 	u_char	sp_z;		/* Z (pressure) */
82 	u_char	sp_w;		/* W (contact patch width) */
83 	signed short	sp_sx;	/* Secondary finger unscaled absolute */
84 				/* X/Y coordinates */
85 	signed short	sp_xy;
86 	u_char	sp_finger;	/* 0 for primary, 1 for secondary */
87 	char	sp_left;	/* Left mouse button status */
88 	char	sp_right;	/* Right mouse button status */
89 	char	sp_middle;	/* Middle button status (possibly emulated) */
90 	char	sp_up;		/* Up button status */
91 	char	sp_down;	/* Down button status */
92 };
93 
94 static void pms_synaptics_input(void *, int);
95 static void pms_synaptics_process_packet(struct pms_softc *,
96 		struct synaptics_packet *);
97 static void pms_sysctl_synaptics(struct sysctllog **);
98 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
99 
100 /* Controlled by sysctl. */
101 static int synaptics_up_down_emul = 2;
102 static int synaptics_up_down_motion_delta = 1;
103 static int synaptics_gesture_move = 200;
104 static int synaptics_gesture_length = 20;
105 static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
106 static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
107 static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
108 static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
109 static int synaptics_edge_motion_delta = 32;
110 static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
111 static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
112 static int synaptics_button_boundary = SYNAPTICS_EDGE_BOTTOM + 720;
113 static int synaptics_button2 = SYNAPTICS_EDGE_LEFT + (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
114 static int synaptics_button3 = SYNAPTICS_EDGE_LEFT + 2 * (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
115 static int synaptics_two_fingers_emul = 0;
116 static int synaptics_scale_x = 16;
117 static int synaptics_scale_y = 16;
118 static int synaptics_max_speed_x = 32;
119 static int synaptics_max_speed_y = 32;
120 static int synaptics_movement_threshold = 4;
121 static int synaptics_movement_enable = 1;
122 
123 /* Sysctl nodes. */
124 static int synaptics_button_boundary_nodenum;
125 static int synaptics_button2_nodenum;
126 static int synaptics_button3_nodenum;
127 static int synaptics_up_down_emul_nodenum;
128 static int synaptics_up_down_motion_delta_nodenum;
129 static int synaptics_gesture_move_nodenum;
130 static int synaptics_gesture_length_nodenum;
131 static int synaptics_edge_left_nodenum;
132 static int synaptics_edge_right_nodenum;
133 static int synaptics_edge_top_nodenum;
134 static int synaptics_edge_bottom_nodenum;
135 static int synaptics_edge_motion_delta_nodenum;
136 static int synaptics_finger_high_nodenum;
137 static int synaptics_finger_low_nodenum;
138 static int synaptics_two_fingers_emul_nodenum;
139 static int synaptics_scale_x_nodenum;
140 static int synaptics_scale_y_nodenum;
141 static int synaptics_max_speed_x_nodenum;
142 static int synaptics_max_speed_y_nodenum;
143 static int synaptics_movement_threshold_nodenum;
144 static int synaptics_movement_enable_nodenum;
145 
146 static int
147 synaptics_poll_cmd(struct pms_softc *psc, ...)
148 {
149 	u_char cmd[4];
150 	size_t i;
151 	va_list ap;
152 
153 	va_start(ap, psc);
154 
155 	for (i = 0; i < __arraycount(cmd); i++)
156 		if ((cmd[i] = (u_char)va_arg(ap, int)) == 0)
157 			break;
158 	va_end(ap);
159 
160 	int res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, i, 0,
161     	    NULL, 0);
162 	if (res)
163 		aprint_error_dev(psc->sc_dev, "command error %#x\n", cmd[0]);
164 	return res;
165 }
166 
167 static int
168 synaptics_poll_reset(struct pms_softc *psc)
169 {
170 	u_char resp[2];
171 	int res;
172 
173 	u_char cmd[1] = { PMS_RESET };
174 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
175 	    resp, 1);
176 	aprint_debug_dev(psc->sc_dev, "reset %d 0x%02x 0x%02x\n",
177 	    res, resp[0], resp[1]);
178 	return res;
179 }
180 
181 static int
182 synaptics_special_read(struct pms_softc *psc, u_char slice, u_char resp[3])
183 {
184 	u_char cmd[1] = { PMS_SEND_DEV_STATUS };
185 	int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, slice);
186 
187 	return res | pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
188 	    cmd, 1, 3, resp, 0);
189 }
190 
191 static int
192 synaptics_special_write(struct pms_softc *psc, u_char command, u_char arg)
193 {
194 	int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, arg);
195 	if (res)
196 		return res;
197 
198 	u_char cmd[2];
199 	cmd[0] = PMS_SET_SAMPLE;
200 	cmd[1] = command;
201 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
202 	    cmd, 2, 0, NULL, 0);
203 	return res;
204 }
205 
206 static void
207 pms_synaptics_probe_extended(struct pms_softc *psc)
208 {
209 	struct synaptics_softc *sc = &psc->u.synaptics;
210 	u_char resp[3];
211 	int res;
212 
213 	aprint_debug_dev(psc->sc_dev,
214 	    "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
215 	if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
216 		sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
217 
218 	if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
219 		sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
220 
221 	if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
222 		sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
223 
224 	if (sc->caps & SYNAPTICS_CAP_MULTIFINGERREPORT)
225 		sc->flags |= SYN_FLAG_HAS_MULTI_FINGER_REPORT;
226 
227 	/* Ask about extra buttons to detect up/down. */
228 	if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08)
229 	    >= SYNAPTICS_EXTENDED_QUERY)
230 	{
231 		res = synaptics_special_read(psc, SYNAPTICS_EXTENDED_QUERY, resp);
232 		if (res == 0) {
233 			int buttons = (resp[1] >> 4);
234 			aprint_debug_dev(psc->sc_dev,
235 			    "%s: Extended Buttons: %d.\n", __func__, buttons);
236 
237 			aprint_debug_dev(psc->sc_dev, "%s: Extended "
238 			    "Capabilities: 0x%02x 0x%02x 0x%02x.\n", __func__,
239 			    resp[0], resp[1], resp[2]);
240 			if (buttons >= 2) {
241 				/* Yes. */
242 				sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
243 			}
244 			if (resp[0] & 0x1) {
245 				/* Vertical scroll area */
246 				sc->flags |= SYN_FLAG_HAS_VERTICAL_SCROLL;
247 			}
248 			if (resp[0] & 0x2) {
249 				/* Horizontal scroll area */
250 				sc->flags |= SYN_FLAG_HAS_HORIZONTAL_SCROLL;
251 			}
252 			if (resp[0] & 0x4) {
253 				/* Extended W-Mode */
254 				sc->flags |= SYN_FLAG_HAS_EXTENDED_WMODE;
255 			}
256 		}
257 	}
258 
259 	/* Ask about click pad */
260 	if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) >=
261 	    SYNAPTICS_CONTINUED_CAPABILITIES)
262 	{
263 		res = synaptics_special_read(psc,
264 		    SYNAPTICS_CONTINUED_CAPABILITIES, resp);
265 
266 /*
267  * The following describes response for the
268  * SYNAPTICS_CONTINUED_CAPABILITIES query.
269  *
270  * byte	mask	name			meaning
271  * ----	----	-------			------------
272  * 0	0x01	adjustable threshold	capacitive button sensitivity
273  *					can be adjusted
274  * 0	0x02	report max		query 0x0d gives max coord reported
275  * 0	0x04	clearpad		sensor is ClearPad product
276  * 0	0x08	advanced gesture	not particularly meaningful
277  * 0	0x10	clickpad bit 0		1-button ClickPad
278  * 0	0x60	multifinger mode	identifies firmware finger counting
279  *					(not reporting!) algorithm.
280  *					Not particularly meaningful
281  * 0	0x80	covered pad		W clipped to 14, 15 == pad mostly covered
282  * 1	0x01	clickpad bit 1		2-button ClickPad
283  * 1	0x02	deluxe LED controls	touchpad support LED commands
284  *					ala multimedia control bar
285  * 1	0x04	reduced filtering	firmware does less filtering on
286  *					position data, driver should watch
287  *					for noise.
288  * 1	0x08	image sensor		image sensor tracks 5 fingers, but only
289  *					reports 2.
290  * 1	0x01	uniform clickpad	whole clickpad moves instead of being
291  *					hinged at the top.
292  * 1	0x20	report min		query 0x0f gives min coord reported
293  */
294 		if (res == 0) {
295 			u_char clickpad_type = (resp[0] & 0x10);
296 			clickpad_type |=       (resp[1] & 0x01);
297 
298 			aprint_debug_dev(psc->sc_dev, "%s: Continued "
299 			    "Capabilities 0x%02x 0x%02x 0x%02x.\n", __func__,
300 			    resp[0], resp[1], resp[2]);
301 			switch (clickpad_type) {
302 			case 0x10:
303 				sc->flags |= SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD;
304 				break;
305 			case 0x01:
306 				sc->flags |= SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD;
307 				break;
308 			default:
309 				break;
310 			}
311 		}
312 	}
313 }
314 
315 static const struct {
316 	int bit;
317 	const char *desc;
318 } syn_flags[] = {
319 	{ SYN_FLAG_HAS_EXTENDED_WMODE, "Extended W mode", },
320 	{ SYN_FLAG_HAS_PASSTHROUGH, "Passthrough", },
321 	{ SYN_FLAG_HAS_MIDDLE_BUTTON, "Middle button", },
322 	{ SYN_FLAG_HAS_BUTTONS_4_5, "Buttons 4/5", },
323 	{ SYN_FLAG_HAS_UP_DOWN_BUTTONS, "Up/down buttons", },
324 	{ SYN_FLAG_HAS_PALM_DETECT, "Palm detect", },
325 	{ SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD, "One button click pad", },
326 	{ SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD, "Two button click pad", },
327 	{ SYN_FLAG_HAS_VERTICAL_SCROLL, "Vertical scroll", },
328 	{ SYN_FLAG_HAS_HORIZONTAL_SCROLL, "Horizontal scroll", },
329 	{ SYN_FLAG_HAS_MULTI_FINGER_REPORT, "Multi-finger Report", },
330 	{ SYN_FLAG_HAS_MULTI_FINGER, "Multi-finger", },
331 };
332 
333 int
334 pms_synaptics_probe_init(void *vsc)
335 {
336 	struct pms_softc *psc = vsc;
337 	struct synaptics_softc *sc = &psc->u.synaptics;
338 	u_char cmd[1], resp[3];
339 	int res, ver_minor, ver_major;
340 	struct sysctllog *clog = NULL;
341 
342 	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
343 	    SYNAPTICS_IDENTIFY_TOUCHPAD);
344 	cmd[0] = PMS_SEND_DEV_STATUS;
345 	res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
346 	    resp, 0);
347 	if (res) {
348 		aprint_debug_dev(psc->sc_dev,
349 		    "synaptics_probe: Identify Touchpad error.\n");
350 		/*
351 		 * Reset device in case the probe confused it.
352 		 */
353  doreset:
354 		(void)synaptics_poll_reset(psc);
355 		return res;
356 	}
357 
358 	if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
359 		aprint_debug_dev(psc->sc_dev,
360 		    "synaptics_probe: Not synaptics.\n");
361 		res = 1;
362 		goto doreset;
363 	}
364 
365 	sc->flags = 0;
366 
367 	/* Check for minimum version and print a nice message. */
368 	ver_major = resp[2] & 0x0f;
369 	ver_minor = resp[0];
370 	aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
371 	    ver_major, ver_minor);
372 	if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
373 		/* No capability query support. */
374 		sc->caps = 0;
375 		goto done;
376 	}
377 
378 
379 	/* Query the hardware capabilities. */
380 	res = synaptics_special_read(psc, SYNAPTICS_READ_CAPABILITIES, resp);
381 	if (res) {
382 		/* Hmm, failed to get capabilites. */
383 		aprint_error_dev(psc->sc_dev,
384 		    "synaptics_probe: Failed to query capabilities.\n");
385 		goto doreset;
386 	}
387 
388 	sc->caps = (resp[0] << 8) | resp[2];
389 
390 	if (sc->caps & SYNAPTICS_CAP_MBUTTON)
391 		sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
392 
393 	if (sc->caps & SYNAPTICS_CAP_4BUTTON)
394 		sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
395 
396 	if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
397 		pms_synaptics_probe_extended(psc);
398 	}
399 
400 	if (sc->flags) {
401 		const char comma[] = ", ";
402 		const char *sep = "";
403 		aprint_normal_dev(psc->sc_dev, "");
404 		for (size_t f = 0; f < __arraycount(syn_flags); f++) {
405 			if (sc->flags & syn_flags[f].bit) {
406 				aprint_normal("%s%s", sep, syn_flags[f].desc);
407 				sep = comma;
408 			}
409 		}
410 		aprint_normal("\n");
411 	}
412 
413 done:
414 	pms_sysctl_synaptics(&clog);
415 	pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
416 	    pms_synaptics_input, psc, device_xname(psc->sc_dev));
417 
418 	return (0);
419 }
420 
421 void
422 pms_synaptics_enable(void *vsc)
423 {
424 	struct pms_softc *psc = vsc;
425 	struct synaptics_softc *sc = &psc->u.synaptics;
426 	u_char enable_modes;
427 	int res;
428 
429 	if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
430 		/*
431 		 * Extended capability probes can confuse the passthrough
432 		 * device; reset the touchpad now to cure that.
433 		 */
434 		res = synaptics_poll_reset(psc);
435 	}
436 
437 	/*
438 	 * Enable Absolute mode with W (width) reporting, and set
439 	 * the packet rate to maximum (80 packets per second). Enable
440 	 * extended W mode if supported so we can report second finger
441 	 * position.
442 	 */
443 	enable_modes =
444 	   SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE;
445 
446 	if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
447 		enable_modes |= SYNAPTICS_MODE_EXTENDED_W;
448 
449 	/*
450  	* Synaptics documentation says to disable device before
451  	* setting mode.
452  	*/
453 	synaptics_poll_cmd(psc, PMS_DEV_DISABLE, 0);
454 	/* a couple of set scales to clear out pending commands */
455 	for (int i = 0; i < 2; i++)
456 		synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
457 
458 	res = synaptics_special_write(psc, SYNAPTICS_CMD_SET_MODE2, enable_modes);
459 	if (res)
460 		aprint_error("synaptics: set mode error\n");
461 
462 	/* a couple of set scales to clear out pending commands */
463 	for (int i = 0; i < 2; i++)
464 		synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
465 
466 	/* Set advanced gesture mode */
467 	if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
468 		synaptics_special_write(psc, SYNAPTICS_WRITE_DELUXE_3, 0x3);
469 
470 	synaptics_poll_cmd(psc, PMS_DEV_ENABLE, 0);
471 
472 	sc->up_down = 0;
473 	sc->prev_fingers = 0;
474 	sc->gesture_start_x = sc->gesture_start_y = 0;
475 	sc->gesture_start_packet = 0;
476 	sc->gesture_tap_packet = 0;
477 	sc->gesture_type = 0;
478 	sc->gesture_buttons = 0;
479 	sc->rem_x[0] = sc->rem_y[0] = 0;
480 	sc->rem_x[1] = sc->rem_y[1] = 0;
481 	sc->movement_history[0] = 0;
482 	sc->movement_history[1] = 0;
483 	sc->button_history = 0;
484 }
485 
486 void
487 pms_synaptics_resume(void *vsc)
488 {
489 	(void)synaptics_poll_reset(vsc);
490 }
491 
492 static void
493 pms_sysctl_synaptics(struct sysctllog **clog)
494 {
495 	int rc, root_num;
496 	const struct sysctlnode *node;
497 
498 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
499 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
500 	    SYSCTL_DESCR("Synaptics touchpad controls"),
501 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
502 	    goto err;
503 
504 	root_num = node->sysctl_num;
505 
506 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
507 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
508 	    CTLTYPE_INT, "up_down_emulation",
509 	    SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
510 	    pms_sysctl_synaptics_verify, 0,
511 	    &synaptics_up_down_emul,
512 	    0, CTL_HW, root_num, CTL_CREATE,
513 	    CTL_EOL)) != 0)
514 		goto err;
515 
516 	synaptics_up_down_emul_nodenum = node->sysctl_num;
517 
518 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
519 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
520 	    CTLTYPE_INT, "up_down_motion_delta",
521 	    SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
522 	    pms_sysctl_synaptics_verify, 0,
523 	    &synaptics_up_down_motion_delta,
524 	    0, CTL_HW, root_num, CTL_CREATE,
525 	    CTL_EOL)) != 0)
526 		goto err;
527 
528 	synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
529 
530 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
531 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
532 	    CTLTYPE_INT, "gesture_move",
533 	    SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
534 	    pms_sysctl_synaptics_verify, 0,
535 	    &synaptics_gesture_move,
536 	    0, CTL_HW, root_num, CTL_CREATE,
537 	    CTL_EOL)) != 0)
538 		goto err;
539 
540 	synaptics_gesture_move_nodenum = node->sysctl_num;
541 
542 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
543 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
544 	    CTLTYPE_INT, "gesture_length",
545 	    SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
546 	    pms_sysctl_synaptics_verify, 0,
547 	    &synaptics_gesture_length,
548 	    0, CTL_HW, root_num, CTL_CREATE,
549 	    CTL_EOL)) != 0)
550 		goto err;
551 
552 	synaptics_gesture_length_nodenum = node->sysctl_num;
553 
554 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
555 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
556 	    CTLTYPE_INT, "edge_left",
557 	    SYSCTL_DESCR("Define left edge of touchpad"),
558 	    pms_sysctl_synaptics_verify, 0,
559 	    &synaptics_edge_left,
560 	    0, CTL_HW, root_num, CTL_CREATE,
561 	    CTL_EOL)) != 0)
562 		goto err;
563 
564 	synaptics_edge_left_nodenum = node->sysctl_num;
565 
566 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
567 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
568 	    CTLTYPE_INT, "edge_right",
569 	    SYSCTL_DESCR("Define right edge of touchpad"),
570 	    pms_sysctl_synaptics_verify, 0,
571 	    &synaptics_edge_right,
572 	    0, CTL_HW, root_num, CTL_CREATE,
573 	    CTL_EOL)) != 0)
574 		goto err;
575 
576 	synaptics_edge_right_nodenum = node->sysctl_num;
577 
578 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
579 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
580 	    CTLTYPE_INT, "edge_top",
581 	    SYSCTL_DESCR("Define top edge of touchpad"),
582 	    pms_sysctl_synaptics_verify, 0,
583 	    &synaptics_edge_top,
584 	    0, CTL_HW, root_num, CTL_CREATE,
585 	    CTL_EOL)) != 0)
586 		goto err;
587 
588 	synaptics_edge_top_nodenum = node->sysctl_num;
589 
590 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
591 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
592 	    CTLTYPE_INT, "edge_bottom",
593 	    SYSCTL_DESCR("Define bottom edge of touchpad"),
594 	    pms_sysctl_synaptics_verify, 0,
595 	    &synaptics_edge_bottom,
596 	    0, CTL_HW, root_num, CTL_CREATE,
597 	    CTL_EOL)) != 0)
598 		goto err;
599 
600 	synaptics_edge_bottom_nodenum = node->sysctl_num;
601 
602 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
603 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
604 	    CTLTYPE_INT, "edge_motion_delta",
605 	    SYSCTL_DESCR("Define edge motion rate"),
606 	    pms_sysctl_synaptics_verify, 0,
607 	    &synaptics_edge_motion_delta,
608 	    0, CTL_HW, root_num, CTL_CREATE,
609 	    CTL_EOL)) != 0)
610 		goto err;
611 
612 	synaptics_edge_motion_delta_nodenum = node->sysctl_num;
613 
614 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
615 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
616 	    CTLTYPE_INT, "finger_high",
617 	    SYSCTL_DESCR("Define finger applied pressure threshold"),
618 	    pms_sysctl_synaptics_verify, 0,
619 	    &synaptics_finger_high,
620 	    0, CTL_HW, root_num, CTL_CREATE,
621 	    CTL_EOL)) != 0)
622 		goto err;
623 
624 	synaptics_finger_high_nodenum = node->sysctl_num;
625 
626 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
627 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
628 	    CTLTYPE_INT, "finger_low",
629 	    SYSCTL_DESCR("Define finger removed pressure threshold"),
630 	    pms_sysctl_synaptics_verify, 0,
631 	    &synaptics_finger_low,
632 	    0, CTL_HW, root_num, CTL_CREATE,
633 	    CTL_EOL)) != 0)
634 		goto err;
635 
636 	synaptics_finger_low_nodenum = node->sysctl_num;
637 
638 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
639 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
640 	    CTLTYPE_INT, "two_fingers_emulation",
641 	    SYSCTL_DESCR("Map two fingers to middle button"),
642 	    pms_sysctl_synaptics_verify, 0,
643 	    &synaptics_two_fingers_emul,
644 	    0, CTL_HW, root_num, CTL_CREATE,
645 	    CTL_EOL)) != 0)
646 		goto err;
647 
648 	synaptics_two_fingers_emul_nodenum = node->sysctl_num;
649 
650 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
651 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
652 	    CTLTYPE_INT, "scale_x",
653 	    SYSCTL_DESCR("Horizontal movement scale factor"),
654 	    pms_sysctl_synaptics_verify, 0,
655 	    &synaptics_scale_x,
656 	    0, CTL_HW, root_num, CTL_CREATE,
657 	    CTL_EOL)) != 0)
658 		goto err;
659 
660 	synaptics_scale_x_nodenum = node->sysctl_num;
661 
662 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
663 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
664 	    CTLTYPE_INT, "scale_y",
665 	    SYSCTL_DESCR("Vertical movement scale factor"),
666 	    pms_sysctl_synaptics_verify, 0,
667 	    &synaptics_scale_y,
668 	    0, CTL_HW, root_num, CTL_CREATE,
669 	    CTL_EOL)) != 0)
670 		goto err;
671 
672 	synaptics_scale_y_nodenum = node->sysctl_num;
673 
674 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
675 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
676 	    CTLTYPE_INT, "max_speed_x",
677 	    SYSCTL_DESCR("Horizontal movement maximum speed"),
678 	    pms_sysctl_synaptics_verify, 0,
679 	    &synaptics_max_speed_x,
680 	    0, CTL_HW, root_num, CTL_CREATE,
681 	    CTL_EOL)) != 0)
682 		goto err;
683 
684 	synaptics_max_speed_x_nodenum = node->sysctl_num;
685 
686 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
687 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
688 	    CTLTYPE_INT, "max_speed_y",
689 	    SYSCTL_DESCR("Vertical movement maximum speed"),
690 	    pms_sysctl_synaptics_verify, 0,
691 	    &synaptics_max_speed_y,
692 	    0, CTL_HW, root_num, CTL_CREATE,
693 	    CTL_EOL)) != 0)
694 		goto err;
695 
696 	synaptics_max_speed_y_nodenum = node->sysctl_num;
697 
698 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
699 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
700 	    CTLTYPE_INT, "movement_threshold",
701 	    SYSCTL_DESCR("Minimum reported movement threshold"),
702 	    pms_sysctl_synaptics_verify, 0,
703 	    &synaptics_movement_threshold,
704 	    0, CTL_HW, root_num, CTL_CREATE,
705 	    CTL_EOL)) != 0)
706 		goto err;
707 
708 	synaptics_movement_threshold_nodenum = node->sysctl_num;
709 
710 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
711 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
712 	    CTLTYPE_INT, "movement_enable",
713 	    SYSCTL_DESCR("Enable movement reporting"),
714 	    pms_sysctl_synaptics_verify, 0,
715 	    &synaptics_movement_enable,
716 	    0, CTL_HW, root_num, CTL_CREATE,
717 	    CTL_EOL)) != 0)
718 		goto err;
719 
720 	synaptics_movement_enable_nodenum = node->sysctl_num;
721 
722 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
723 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
724 	    CTLTYPE_INT, "button_boundary",
725 	    SYSCTL_DESCR("Top edge of button area"),
726 	    pms_sysctl_synaptics_verify, 0,
727 	    &synaptics_button_boundary,
728 	    0, CTL_HW, root_num, CTL_CREATE,
729 	    CTL_EOL)) != 0)
730 		goto err;
731 
732 	synaptics_button_boundary_nodenum = node->sysctl_num;
733 
734 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
735 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
736 	    CTLTYPE_INT, "button2_edge",
737 	    SYSCTL_DESCR("Left edge of button 2 region"),
738 	    pms_sysctl_synaptics_verify, 0,
739 	    &synaptics_button2,
740 	    0, CTL_HW, root_num, CTL_CREATE,
741 	    CTL_EOL)) != 0)
742 		goto err;
743 
744 	synaptics_button2_nodenum = node->sysctl_num;
745 
746 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
747 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
748 	    CTLTYPE_INT, "button3_edge",
749 	    SYSCTL_DESCR("Left edge of button 3 region"),
750 	    pms_sysctl_synaptics_verify, 0,
751 	    &synaptics_button3,
752 	    0, CTL_HW, root_num, CTL_CREATE,
753 	    CTL_EOL)) != 0)
754 		goto err;
755 
756 	synaptics_button3_nodenum = node->sysctl_num;
757 	return;
758 
759 err:
760 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
761 }
762 
763 static int
764 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
765 {
766 	int error, t;
767 	struct sysctlnode node;
768 
769 	node = *rnode;
770 	t = *(int *)rnode->sysctl_data;
771 	node.sysctl_data = &t;
772 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
773 	if (error || newp == NULL)
774 		return error;
775 
776 	/* Sanity check the params. */
777 	if (node.sysctl_num == synaptics_up_down_emul_nodenum ||
778 	    node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
779 		if (t < 0 || t > 2)
780 			return (EINVAL);
781 	} else
782 	if (node.sysctl_num == synaptics_gesture_length_nodenum ||
783 	    node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
784 	    node.sysctl_num == synaptics_up_down_motion_delta_nodenum) {
785 		if (t < 0)
786 			return (EINVAL);
787 	} else
788 	if (node.sysctl_num == synaptics_edge_left_nodenum ||
789 	    node.sysctl_num == synaptics_edge_bottom_nodenum) {
790 		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
791 			return (EINVAL);
792 	} else
793 	if (node.sysctl_num == synaptics_edge_right_nodenum ||
794 	    node.sysctl_num == synaptics_edge_top_nodenum) {
795 		if (t < (SYNAPTICS_EDGE_MAX / 2))
796 			return (EINVAL);
797 	} else
798 	if (node.sysctl_num == synaptics_scale_x_nodenum ||
799 	    node.sysctl_num == synaptics_scale_y_nodenum) {
800 		if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
801 			return (EINVAL);
802 	} else
803 	if (node.sysctl_num == synaptics_finger_high_nodenum) {
804 		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
805 		    t < synaptics_finger_low)
806 			return (EINVAL);
807 	} else
808 	if (node.sysctl_num == synaptics_finger_low_nodenum) {
809 		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
810 		    t > synaptics_finger_high)
811 			return (EINVAL);
812 	} else
813 	if (node.sysctl_num == synaptics_gesture_move_nodenum ||
814 	    node.sysctl_num == synaptics_movement_threshold_nodenum) {
815 		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
816 			return (EINVAL);
817 	} else
818 	if (node.sysctl_num == synaptics_button_boundary_nodenum) {
819 		if (t < 0 || t < SYNAPTICS_EDGE_BOTTOM ||
820 		    t > SYNAPTICS_EDGE_TOP)
821 			return (EINVAL);
822 	} else
823 	if (node.sysctl_num == synaptics_button2_nodenum ||
824 	    node.sysctl_num == synaptics_button3_nodenum) {
825 		if (t < SYNAPTICS_EDGE_LEFT || t > SYNAPTICS_EDGE_RIGHT)
826 			return (EINVAL);
827 	} else
828 	if (node.sysctl_num == synaptics_movement_enable_nodenum) {
829 		if (t < 0 || t > 1)
830 			return (EINVAL);
831 	} else
832 		return (EINVAL);
833 
834 	*(int *)rnode->sysctl_data = t;
835 
836 	return (0);
837 }
838 
839 /* Masks for the first byte of a packet */
840 #define PMS_LBUTMASK 0x01
841 #define PMS_RBUTMASK 0x02
842 #define PMS_MBUTMASK 0x04
843 
844 static void
845 pms_synaptics_parse(struct pms_softc *psc)
846 {
847 	struct synaptics_softc *sc = &psc->u.synaptics;
848 	struct synaptics_packet sp;
849 	char new_buttons, ew_mode;
850 
851 	memset(&sp, 0, sizeof(sp));
852 
853 	/* Width of finger */
854 	sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
855 	   ((psc->packet[0] & 0x04) >> 1) +
856 	   ((psc->packet[3] & 0x04) >> 2);
857 	sp.sp_finger = 0;
858 	if (sp.sp_w == SYNAPTICS_WIDTH_EXTENDED_W) {
859 		ew_mode = psc->packet[5] >> 4;
860 		switch (ew_mode)
861 		{
862 		case SYNAPTICS_EW_WHEEL:
863 			/* scroll wheel report, ignore for now */
864 			aprint_debug_dev(psc->sc_dev, "mouse wheel packet\n");
865 			return;
866 
867 		case SYNAPTICS_EW_SECONDARY_FINGER:
868 			/* parse the second finger report */
869 
870 			sp.sp_finger = 1; /* just one other finger for now */
871 			sp.sp_x = psc->packet[1]
872 			    + ((psc->packet[4] & 0x0f) << 8);
873 			sp.sp_y = psc->packet[2]
874 			    + ((psc->packet[4] & 0xf0) << 4);
875 			sp.sp_z = (psc->packet[3] & 0x30)
876 			    + (psc->packet[5] & 0x0f);
877 
878 			/* keep same buttons down as primary */
879 			sp.sp_left = sc->button_history & PMS_LBUTMASK;
880 			sp.sp_middle = sc->button_history & PMS_MBUTMASK;
881 			sp.sp_right = sc->button_history & PMS_RBUTMASK;
882 			break;
883 
884 		case SYNAPTICS_EW_FINGER_STATUS:
885 			/* reports which finger is primary/secondary
886 			 * ignore for now.
887 			 */
888 			return;
889 
890 		default:
891 			aprint_error_dev(psc->sc_dev,
892 			    "invalid extended w mode %d\n",
893 			    ew_mode);
894 			return;
895 		}
896 	} else {
897 
898 		/* Absolute X/Y coordinates of finger */
899 		sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
900 	   	((psc->packet[3] & 0x10) << 8);
901 		sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
902 	   	((psc->packet[3] & 0x20) << 7);
903 
904 		/* Pressure */
905 		sp.sp_z = psc->packet[2];
906 
907 		/* Left/Right button handling. */
908 		sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
909 		sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
910 
911 		/* Up/Down buttons. */
912 		if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
913 			/* Old up/down buttons. */
914 			sp.sp_up = sp.sp_left ^
915 		    	    (psc->packet[3] & PMS_LBUTMASK);
916 			sp.sp_down = sp.sp_right ^
917 		    	    (psc->packet[3] & PMS_RBUTMASK);
918 		} else if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
919 	   	    ((psc->packet[0] & PMS_RBUTMASK) ^
920 	   	    (psc->packet[3] & PMS_RBUTMASK))) {
921 			/* New up/down button. */
922 			sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
923 			sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
924 		} else {
925 			sp.sp_up = 0;
926 			sp.sp_down = 0;
927 		}
928 
929 		new_buttons = 0;
930 		if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
931 			/* This is not correctly specified. Read this button press
932 		 	* from L/U bit.  Emulate 3 buttons by checking the
933 		 	* coordinates of the click and returning the appropriate
934 		 	* button code.  Outside the button region default to a
935 		 	* left click.
936 		 	*/
937 			u_char bstate = (psc->packet[0] ^ psc->packet[3])
938 					    & 0x01;
939 			if (sp.sp_y < synaptics_button_boundary) {
940 				if (sp.sp_x > synaptics_button3) {
941 					sp.sp_right =
942 			   			bstate ? PMS_RBUTMASK : 0;
943 				} else if (sp.sp_x > synaptics_button2) {
944 					sp.sp_middle =
945 				   		bstate ? PMS_MBUTMASK : 0;
946 				} else {
947 					sp.sp_left = bstate ? PMS_LBUTMASK : 0;
948 				}
949 			} else
950 				sp.sp_left = bstate ? 1 : 0;
951 			new_buttons = sp.sp_left | sp.sp_middle | sp.sp_right;
952 			if (new_buttons != sc->button_history) {
953 				if (sc->button_history == 0)
954 					sc->button_history = new_buttons;
955 				else if (new_buttons == 0) {
956 					sc->button_history = 0;
957 				       /* ensure all buttons are cleared just in
958 				 	* case finger comes off in a different
959 				 	* region.
960 				 	*/
961 					sp.sp_left = 0;
962 					sp.sp_middle = 0;
963 					sp.sp_right = 0;
964 				} else {
965 					/* make sure we keep the same button even
966 				 	* if the finger moves to a different
967 				 	* region.  This precludes chording
968 				 	* but, oh well.
969 				 	*/
970 					sp.sp_left = sc->button_history & PMS_LBUTMASK;
971 					sp.sp_middle = sc->button_history
972 				    	& PMS_MBUTMASK;
973 					sp.sp_right = sc->button_history & PMS_RBUTMASK;
974 				}
975 			}
976 		} else if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
977 			/* Old style Middle Button. */
978 			sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
979 		    	    (psc->packet[3] & PMS_LBUTMASK);
980 		} else if (synaptics_up_down_emul == 1) {
981 			/* Do middle button emulation using up/down buttons */
982 			sp.sp_middle = sp.sp_up | sp.sp_down;
983 			sp.sp_up = sp.sp_down = 0;
984 		} else
985 			sp.sp_middle = 0;
986 
987 	}
988 
989 	pms_synaptics_process_packet(psc, &sp);
990 }
991 
992 static void
993 pms_synaptics_passthrough(struct pms_softc *psc)
994 {
995 	int dx, dy, dz;
996 	int buttons, changed;
997 	int s;
998 
999 	buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
1000 		((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
1001 		((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);
1002 
1003 	dx = psc->packet[4];
1004 	if (dx >= 128)
1005 		dx -= 256;
1006 	if (dx == -128)
1007 		dx = -127;
1008 
1009 	dy = psc->packet[5];
1010 	if (dy >= 128)
1011 		dy -= 256;
1012 	if (dy == -128)
1013 		dy = -127;
1014 
1015 	dz = 0;
1016 
1017 	changed = buttons ^ (psc->buttons & 0xe0);
1018 	psc->buttons ^= changed;
1019 
1020 	if (dx || dy || dz || changed) {
1021 		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
1022 		s = spltty();
1023 		wsmouse_input(psc->sc_wsmousedev,
1024 			buttons, dx, dy, dz, 0,
1025 			WSMOUSE_INPUT_DELTA);
1026 		splx(s);
1027 	}
1028 }
1029 
1030 static void
1031 pms_synaptics_input(void *vsc, int data)
1032 {
1033 	struct pms_softc *psc = vsc;
1034 	struct timeval diff;
1035 
1036 	if (!psc->sc_enabled) {
1037 		/* Interrupts are not expected. Discard the byte. */
1038 		return;
1039 	}
1040 
1041 	getmicrouptime(&psc->current);
1042 
1043 	if (psc->inputstate > 0) {
1044 		timersub(&psc->current, &psc->last, &diff);
1045 		if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
1046 			aprint_debug_dev(psc->sc_dev,
1047 			    "pms_input: unusual delay (%ld.%06ld s), "
1048 			    "scheduling reset\n",
1049 			    (long)diff.tv_sec, (long)diff.tv_usec);
1050 			printf("pms_input: unusual delay (%ld.%06ld s), "
1051 			    "scheduling reset\n",
1052 			    (long)diff.tv_sec, (long)diff.tv_usec);
1053 			psc->inputstate = 0;
1054 			psc->sc_enabled = 0;
1055 			wakeup(&psc->sc_enabled);
1056 			return;
1057 		}
1058 	}
1059 	psc->last = psc->current;
1060 
1061 	switch (psc->inputstate) {
1062 	case 0:
1063 		if ((data & 0xc8) != 0x80) {
1064 			aprint_debug_dev(psc->sc_dev,
1065 			    "pms_input: 0x%02x out of sync\n", data);
1066 			return;	/* not in sync yet, discard input */
1067 		}
1068 		/*FALLTHROUGH*/
1069 
1070 	case 3:
1071 		if ((data & 8) == 8) {
1072 			aprint_debug_dev(psc->sc_dev,
1073 			    "pms_input: dropped in relative mode, reset\n");
1074 			psc->inputstate = 0;
1075 			psc->sc_enabled = 0;
1076 			wakeup(&psc->sc_enabled);
1077 			return;
1078 		}
1079 	}
1080 
1081 	psc->packet[psc->inputstate++] = data & 0xff;
1082 	if (psc->inputstate == 6) {
1083 		/*
1084 		 * We have a complete packet.
1085 		 * Extract the pertinent details.
1086 		 */
1087 		psc->inputstate = 0;
1088 		if ((psc->packet[0] & 0xfc) == 0x84 &&
1089 		    (psc->packet[3] & 0xcc) == 0xc4) {
1090 			/* W = SYNAPTICS_WIDTH_PASSTHROUGH, PS/2 passthrough */
1091 			pms_synaptics_passthrough(psc);
1092 		} else {
1093 			pms_synaptics_parse(psc);
1094 		}
1095 	}
1096 }
1097 
1098 static inline int
1099 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
1100     int *palmp)
1101 {
1102 	int fingers;
1103 
1104 	/* Assume no palm */
1105 	*palmp = 0;
1106 
1107 	/*
1108 	 * Apply some hysteresis when checking for a finger.
1109 	 * When the finger is first applied, we ignore it until the
1110 	 * pressure exceeds the 'high' threshold. The finger is considered
1111 	 * removed only when pressure falls beneath the 'low' threshold.
1112 	 */
1113 	if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
1114 	    (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
1115 		fingers = 1;
1116 	else
1117 		fingers = 0;
1118 
1119 	/*
1120 	 * If the pad can't do palm detection, skip the rest.
1121 	 */
1122 	if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
1123 		return (fingers);
1124 
1125 	/*
1126 	 * Palm detection
1127 	 */
1128 	if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
1129 	    sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
1130 		*palmp = 1;
1131 
1132 	if (sc->prev_fingers == 0 &&
1133 	    (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
1134 	     sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
1135 		/*
1136 		 * Contact area or pressure is too great to be a finger.
1137 		 * Just ignore it for now.
1138 		 */
1139 		return (0);
1140 	}
1141 
1142 	/*
1143 	 * Detect 2 and 3 fingers if supported, but only if multiple
1144 	 * fingers appear within the tap gesture time period.
1145 	 */
1146 	if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
1147 	    SYN_TIME(sc, sc->gesture_start_packet,
1148 	    sp->sp_finger) < synaptics_gesture_length) {
1149 		switch (sp->sp_w) {
1150 		case SYNAPTICS_WIDTH_TWO_FINGERS:
1151 			fingers = 2;
1152 			break;
1153 
1154 		case SYNAPTICS_WIDTH_THREE_OR_MORE:
1155 			fingers = 3;
1156 			break;
1157 
1158 		case SYNAPTICS_WIDTH_PEN:
1159 			fingers = 1;
1160 			break;
1161 
1162 		default:
1163 			/*
1164 			 * The width value can report spurious single-finger
1165 			 * events after a multi-finger event.
1166 			 */
1167 			if (sc->prev_fingers > 1)
1168 				fingers = sc->prev_fingers;
1169 			else
1170 				fingers = 1;
1171 			break;
1172 		}
1173 	}
1174 
1175 	return (fingers);
1176 }
1177 
1178 static inline void
1179 synaptics_gesture_detect(struct synaptics_softc *sc,
1180     struct synaptics_packet *sp, int fingers)
1181 {
1182 	int gesture_len, gesture_buttons;
1183 	int set_buttons;
1184 
1185 	gesture_len = SYN_TIME(sc, sc->gesture_start_packet, sp->sp_finger);
1186 	gesture_buttons = sc->gesture_buttons;
1187 
1188 	if (fingers > 0 && (fingers == sc->prev_fingers)) {
1189 		/* Finger is still present */
1190 		sc->gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
1191 		sc->gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
1192 	} else
1193 	if (fingers && sc->prev_fingers == 0) {
1194 		/*
1195 		 * Finger was just applied.
1196 		 * If the previous gesture was a single-click, set things
1197 		 * up to deal with a possible drag or double-click gesture.
1198 		 * Basically, if the finger is removed again within
1199 		 * 'synaptics_gesture_length' packets, this is treated
1200 		 * as a double-click. Otherwise we will emulate holding
1201 		 * the left button down whilst dragging the mouse.
1202 		 */
1203 		if (SYN_IS_SINGLE_TAP(sc->gesture_type))
1204 			sc->gesture_type |= SYN_GESTURE_DRAG;
1205 
1206 		sc->gesture_start_x = abs(sp->sp_x);
1207 		sc->gesture_start_y = abs(sp->sp_y);
1208 		sc->gesture_move_x = 0;
1209 		sc->gesture_move_y = 0;
1210 		sc->gesture_start_packet = sc->total_packets[0];
1211 
1212 #ifdef DIAGNOSTIC
1213 		aprint_debug("Finger applied: gesture_start_x: %d gesture_start_y: %d\n",
1214 			sc->gesture_start_x, sc->gesture_start_y);
1215 #endif
1216 	} else
1217 	if (fingers == 0 && sc->prev_fingers != 0) {
1218 		/*
1219 		 * Finger was just removed.
1220 		 * Check if the contact time and finger movement were
1221 		 * small enough to qualify as a gesture.
1222 		 * Ignore finger movement if multiple fingers were
1223 		 * detected (the pad may report coordinates for any
1224 		 * of the fingers).
1225 		 */
1226 
1227 #ifdef DIAGNOSTIC
1228 		aprint_debug("Finger removed: gesture_len: %d (%d)\n",
1229 			gesture_len, synaptics_gesture_length);
1230 		aprint_debug("gesture_move_x: %d (%d) sp_x: %d\n",
1231 			sc->gesture_move_x, synaptics_gesture_move, abs(sp->sp_x));
1232 		aprint_debug("gesture_move_y: %d (%d) sp_y: %d\n",
1233 			sc->gesture_move_y, synaptics_gesture_move, abs(sp->sp_y));
1234 #endif
1235 
1236 		if (gesture_len < synaptics_gesture_length &&
1237 		    ((sc->gesture_move_x < synaptics_gesture_move &&
1238 		     sc->gesture_move_y < synaptics_gesture_move))) {
1239 			/*
1240 			 * Looking good so far.
1241 			 */
1242 			if (SYN_IS_DRAG(sc->gesture_type)) {
1243 				/*
1244 				 * Promote this gesture to double-click.
1245 				 */
1246 				sc->gesture_type |= SYN_GESTURE_DOUBLE;
1247 				sc->gesture_type &= ~SYN_GESTURE_SINGLE;
1248 			} else {
1249 				/*
1250 				 * Single tap gesture. Set the tap length timer
1251 				 * and flag a single-click.
1252 				 */
1253 				sc->gesture_tap_packet = sc->total_packets[0];
1254 				sc->gesture_type |= SYN_GESTURE_SINGLE;
1255 
1256 				/*
1257 				 * The gesture can be modified depending on
1258 				 * the number of fingers detected.
1259 				 *
1260 				 * 1: Normal left button emulation.
1261 				 * 2: Either middle button or right button
1262 				 *    depending on the value of the two_fingers
1263 				 *    sysctl variable.
1264 				 * 3: Right button.
1265 				 */
1266 				switch (sc->prev_fingers) {
1267 				case 2:
1268 					if (synaptics_two_fingers_emul == 1)
1269 						gesture_buttons |= PMS_RBUTMASK;
1270 					else
1271 					if (synaptics_two_fingers_emul == 2)
1272 						gesture_buttons |= PMS_MBUTMASK;
1273 					break;
1274 				case 3:
1275 					gesture_buttons |= PMS_RBUTMASK;
1276 					break;
1277 				default:
1278 					gesture_buttons |= PMS_LBUTMASK;
1279 					break;
1280 				}
1281 			}
1282 		}
1283 
1284 		/*
1285 		 * Always clear drag state when the finger is removed.
1286 		 */
1287 		sc->gesture_type &= ~SYN_GESTURE_DRAG;
1288 	}
1289 
1290 	if (sc->gesture_type == 0) {
1291 		/*
1292 		 * There is no gesture in progress.
1293 		 * Clear emulated button state.
1294 		 */
1295 		sc->gesture_buttons = 0;
1296 		return;
1297 	}
1298 
1299 	/*
1300 	 * A gesture is in progress.
1301 	 */
1302 	set_buttons = 0;
1303 
1304 	if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
1305 		/*
1306 		 * Single-click.
1307 		 * Activate the relevant button(s) until the
1308 		 * gesture tap timer has expired.
1309 		 */
1310 		if (SYN_TIME(sc, sc->gesture_tap_packet, sp->sp_finger) <
1311 		    synaptics_gesture_length)
1312 			set_buttons = 1;
1313 		else
1314 			sc->gesture_type &= ~SYN_GESTURE_SINGLE;
1315 	} else
1316 	if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
1317 		/*
1318 		 * Double-click.
1319 		 * Activate the relevant button(s) once.
1320 		 */
1321 		set_buttons = 1;
1322 		sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
1323 	}
1324 
1325 	if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
1326 		/*
1327 		 * Single-click and drag.
1328 		 * Maintain button state until the finger is removed.
1329 		 */
1330 		sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
1331 		sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
1332 		sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
1333 	}
1334 
1335 	sc->gesture_buttons = gesture_buttons;
1336 }
1337 
1338 static inline int
1339 synaptics_filter_policy(struct synaptics_softc *sc, int finger, int *history,
1340 			int value)
1341 {
1342 	int a, b, rv, count;
1343 
1344 	count = sc->total_packets[finger];
1345 
1346 	/*
1347 	 * Once we've accumulated at least SYN_HIST_SIZE values, combine
1348 	 * each new value with the previous two and return the average.
1349 	 *
1350 	 * This is necessary when the touchpad is operating in 80 packets
1351 	 * per second mode, as it performs little internal filtering on
1352 	 * reported values.
1353 	 *
1354 	 * Using a rolling average helps to filter out jitter caused by
1355 	 * tiny finger movements.
1356 	 */
1357 	if (sc->movement_history[finger] >= SYN_HIST_SIZE) {
1358 		a = (history[(count + 0) % SYN_HIST_SIZE] +
1359 		    history[(count + 1) % SYN_HIST_SIZE]) / 2;
1360 
1361 		b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
1362 
1363 		rv = b - a;
1364 
1365 		/*
1366 		 * Don't report the movement if it's below a certain
1367 		 * threshold.
1368 		 */
1369 		if (abs(rv) < synaptics_movement_threshold)
1370 			rv = 0;
1371 	} else
1372 		rv = 0;
1373 
1374 	/*
1375 	 * Add the new value to the history buffer.
1376 	 */
1377 	history[(count + 1) % SYN_HIST_SIZE] = value;
1378 
1379 	return (rv);
1380 }
1381 
1382 /* Edge detection */
1383 #define	SYN_EDGE_TOP		1
1384 #define	SYN_EDGE_BOTTOM		2
1385 #define	SYN_EDGE_LEFT		4
1386 #define	SYN_EDGE_RIGHT		8
1387 
1388 static inline int
1389 synaptics_check_edge(int x, int y)
1390 {
1391 	int rv = 0;
1392 
1393 	if (x < synaptics_edge_left)
1394 		rv |= SYN_EDGE_LEFT;
1395 	else
1396 	if (x > synaptics_edge_right)
1397 		rv |= SYN_EDGE_RIGHT;
1398 
1399 	if (y < synaptics_edge_bottom)
1400 		rv |= SYN_EDGE_BOTTOM;
1401 	else
1402 	if (y > synaptics_edge_top)
1403 		rv |= SYN_EDGE_TOP;
1404 
1405 	return (rv);
1406 }
1407 
1408 static inline int
1409 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
1410 {
1411 
1412 	/*
1413 	 * When edge motion is enabled, synaptics_edge_motion_delta is
1414 	 * combined with the current delta, together with the direction
1415 	 * in which to simulate the motion. The result is added to
1416 	 * the delta derived from finger movement. This provides a smooth
1417 	 * transition from finger movement to edge motion.
1418 	 */
1419 	delta = synaptics_edge_motion_delta + (dir * delta);
1420 	if (delta < 0)
1421 		return (0);
1422 	if (delta > synaptics_edge_motion_delta)
1423 		return (synaptics_edge_motion_delta);
1424 	return (delta);
1425 }
1426 
1427 static inline int
1428 synaptics_scale(int delta, int scale, int *remp)
1429 {
1430 	int rv;
1431 
1432 	/*
1433 	 * Scale the raw delta in Synaptics coordinates (0-6143) into
1434 	 * something more reasonable by dividing the raw delta by a
1435 	 * scale factor. Any remainder from the previous scale result
1436 	 * is added to the current delta before scaling.
1437 	 * This prevents loss of resolution for very small/slow
1438 	 * movements of the finger.
1439 	 */
1440 	delta += *remp;
1441 	rv = delta / scale;
1442 	*remp = delta % scale;
1443 
1444 	return (rv);
1445 }
1446 
1447 static inline void
1448 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
1449     int finger, int *dxp, int *dyp)
1450 {
1451 	int dx, dy, edge;
1452 
1453 	/*
1454 	 * Compute the next values of dx and dy
1455 	 */
1456 	dx = synaptics_filter_policy(sc, finger, sc->history_x[finger],
1457 		sp->sp_x);
1458 	dy = synaptics_filter_policy(sc, finger, sc->history_y[finger],
1459 		sp->sp_y);
1460 
1461 	/*
1462 	 * If we're dealing with a drag gesture, and the finger moves to
1463 	 * the edge of the touchpad, apply edge motion emulation if it
1464 	 * is enabled.
1465 	 */
1466 	if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
1467 		edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
1468 
1469 		if (edge & SYN_EDGE_LEFT)
1470 			dx -= synaptics_edge_motion(sc, dx, 1);
1471 		if (edge & SYN_EDGE_RIGHT)
1472 			dx += synaptics_edge_motion(sc, dx, -1);
1473 		if (edge & SYN_EDGE_BOTTOM)
1474 			dy -= synaptics_edge_motion(sc, dy, 1);
1475 		if (edge & SYN_EDGE_TOP)
1476 			dy += synaptics_edge_motion(sc, dy, -1);
1477 	}
1478 
1479 	/*
1480 	 * Apply scaling to both deltas
1481 	 */
1482 	dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x[finger]);
1483 	dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y[finger]);
1484 
1485 	/*
1486 	 * Clamp deltas to specified maximums.
1487 	 */
1488 	if (dx > synaptics_max_speed_x)
1489 		dx = synaptics_max_speed_x;
1490 	if (dy > synaptics_max_speed_y)
1491 		dy = synaptics_max_speed_y;
1492 
1493 	*dxp = dx;
1494 	*dyp = dy;
1495 
1496 	sc->movement_history[finger]++;
1497 }
1498 
1499 static void
1500 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
1501 {
1502 	struct synaptics_softc *sc = &psc->u.synaptics;
1503 	int dx, dy, dz;
1504 	int fingers, palm, buttons, changed;
1505 	int s;
1506 
1507 	/*
1508 	 * Do Z-axis emulation using up/down buttons if required.
1509 	 * Note that the pad will send a one second burst of packets
1510 	 * when an up/down button is pressed and held. At the moment
1511 	 * we don't deal with auto-repeat, so convert the burst into
1512 	 * a one-shot.
1513 	 */
1514 	dz = 0;
1515 	if (synaptics_up_down_emul == 2) {
1516 		if (sc->up_down == 0) {
1517 			if (sp->sp_up && sp->sp_down) {
1518 				/*
1519 				 * Most up/down buttons will be actuated using
1520 				 * a rocker switch, so we should never see
1521 				 * them both simultaneously. But just in case,
1522 				 * treat this situation as a middle button
1523 				 * event.
1524 				 */
1525 				sp->sp_middle = 1;
1526 			} else
1527 			if (sp->sp_up)
1528 				dz = -synaptics_up_down_motion_delta;
1529 			else
1530 			if (sp->sp_down)
1531 				dz = synaptics_up_down_motion_delta;
1532 		}
1533 
1534 		sc->up_down = sp->sp_up | sp->sp_down;
1535 		sp->sp_up = sp->sp_down = 0;
1536 	}
1537 
1538 	/*
1539 	 * Determine whether or not a finger is on the pad.
1540 	 * On some pads, this will return the number of fingers
1541 	 * detected.
1542 	 */
1543 	fingers = synaptics_finger_detect(sc, sp, &palm);
1544 
1545 	/*
1546 	 * Do gesture processing only if we didn't detect a palm and
1547 	 * it is not the seondary finger.
1548 	 */
1549 	if ((sp->sp_finger == 0) && (palm == 0))
1550 		synaptics_gesture_detect(sc, sp, fingers);
1551 	else
1552 		sc->gesture_type = sc->gesture_buttons = 0;
1553 
1554 	/*
1555 	 * Determine what buttons to report
1556 	 */
1557 	buttons = (sp->sp_left ? 0x1 : 0) |
1558 	    (sp->sp_middle ? 0x2 : 0) |
1559 	    (sp->sp_right ? 0x4 : 0) |
1560 	    (sp->sp_up ? 0x8 : 0) |
1561 	    (sp->sp_down ? 0x10 : 0);
1562 	changed = buttons ^ (psc->buttons & 0x1f);
1563 	psc->buttons ^= changed;
1564 
1565 	sc->prev_fingers = fingers;
1566 	sc->total_packets[sp->sp_finger]++;
1567 
1568 	/*
1569 	 * Do movement processing IFF we have a single finger and no palm or
1570 	 * a secondary finger and no palm.
1571 	 */
1572 	if (palm == 0 && synaptics_movement_enable) {
1573 		if (fingers == 1) {
1574 			synaptics_movement(sc, sp, sp->sp_finger, &dx, &dy);
1575 		} else {
1576 			/*
1577 			 * No valid finger. Therefore no movement.
1578 			 */
1579 			sc->movement_history[sp->sp_finger] = 0;
1580 			sc->rem_x[sp->sp_finger] = sc->rem_y[sp->sp_finger] = 0;
1581 			dx = dy = 0;
1582 		}
1583 	} else {
1584 		/*
1585 		 * No valid finger. Therefore no movement.
1586 		 */
1587 		sc->movement_history[0] = 0;
1588 		sc->rem_x[0] = sc->rem_y[0] = 0;
1589 		dx = dy = 0;
1590 	}
1591 
1592 	/*
1593 	 * Pass the final results up to wsmouse_input() if necessary.
1594 	 */
1595 	if (dx || dy || dz || changed) {
1596 		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
1597 		s = spltty();
1598 		wsmouse_input(psc->sc_wsmousedev,
1599 				buttons,
1600 				dx, dy, dz, 0,
1601 		    		WSMOUSE_INPUT_DELTA);
1602 		splx(s);
1603 	}
1604 }
1605