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