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