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