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