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