Home | History | Annotate | Line # | Download | only in pckbport
synaptics.c revision 1.27
      1 /*	$NetBSD: synaptics.c,v 1.27 2011/09/09 14:29:47 jakllsch 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.27 2011/09/09 14:29:47 jakllsch 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 	char	sp_left;	/* Left mouse button status */
     84 	char	sp_right;	/* Right mouse button status */
     85 	char	sp_middle;	/* Middle button status (possibly emulated) */
     86 	char	sp_up;		/* Up button status */
     87 	char	sp_down;	/* Down button status */
     88 };
     89 
     90 static void pms_synaptics_input(void *, int);
     91 static void pms_synaptics_process_packet(struct pms_softc *,
     92 		struct synaptics_packet *);
     93 static void pms_sysctl_synaptics(struct sysctllog **);
     94 static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
     95 
     96 /* Controled by sysctl. */
     97 static int synaptics_up_down_emul = 2;
     98 static int synaptics_up_down_motion_delta = 1;
     99 static int synaptics_gesture_move = 200;
    100 static int synaptics_gesture_length = 20;
    101 static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
    102 static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
    103 static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
    104 static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
    105 static int synaptics_edge_motion_delta = 32;
    106 static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
    107 static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
    108 static int synaptics_two_fingers_emul = 0;
    109 static int synaptics_scale_x = 16;
    110 static int synaptics_scale_y = 16;
    111 static int synaptics_max_speed_x = 32;
    112 static int synaptics_max_speed_y = 32;
    113 static int synaptics_movement_threshold = 4;
    114 
    115 /* Sysctl nodes. */
    116 static int synaptics_up_down_emul_nodenum;
    117 static int synaptics_up_down_motion_delta_nodenum;
    118 static int synaptics_gesture_move_nodenum;
    119 static int synaptics_gesture_length_nodenum;
    120 static int synaptics_edge_left_nodenum;
    121 static int synaptics_edge_right_nodenum;
    122 static int synaptics_edge_top_nodenum;
    123 static int synaptics_edge_bottom_nodenum;
    124 static int synaptics_edge_motion_delta_nodenum;
    125 static int synaptics_finger_high_nodenum;
    126 static int synaptics_finger_low_nodenum;
    127 static int synaptics_two_fingers_emul_nodenum;
    128 static int synaptics_scale_x_nodenum;
    129 static int synaptics_scale_y_nodenum;
    130 static int synaptics_max_speed_x_nodenum;
    131 static int synaptics_max_speed_y_nodenum;
    132 static int synaptics_movement_threshold_nodenum;
    133 
    134 int
    135 pms_synaptics_probe_init(void *vsc)
    136 {
    137 	struct pms_softc *psc = vsc;
    138 	struct synaptics_softc *sc = &psc->u.synaptics;
    139 	u_char cmd[2], resp[3];
    140 	int res, ver_minor, ver_major;
    141 	struct sysctllog *clog = NULL;
    142 
    143 	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
    144 	    SYNAPTICS_IDENTIFY_TOUCHPAD);
    145 	cmd[0] = PMS_SEND_DEV_STATUS;
    146 	res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
    147 	    resp, 0);
    148 	if (res) {
    149 		aprint_debug_dev(psc->sc_dev,
    150 		    "synaptics_probe: Identify Touchpad error.\n");
    151 		/*
    152 		 * Reset device in case the probe confused it.
    153 		 */
    154  doreset:
    155 		cmd[0] = PMS_RESET;
    156 		(void) pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
    157 		    1, 2, resp, 1);
    158 		return (res);
    159 	}
    160 
    161 	if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
    162 		aprint_debug_dev(psc->sc_dev,
    163 		    "synaptics_probe: Not synaptics.\n");
    164 		res = 1;
    165 		goto doreset;
    166 	}
    167 
    168 	sc->flags = 0;
    169 
    170 	/* Check for minimum version and print a nice message. */
    171 	ver_major = resp[2] & 0x0f;
    172 	ver_minor = resp[0];
    173 	aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
    174 	    ver_major, ver_minor);
    175 	if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
    176 		/* No capability query support. */
    177 		sc->caps = 0;
    178 		goto done;
    179 	}
    180 
    181 	/* Query the hardware capabilities. */
    182 	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
    183 	    SYNAPTICS_READ_CAPABILITIES);
    184 	cmd[0] = PMS_SEND_DEV_STATUS;
    185 	res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
    186 	    resp, 0);
    187 	if (res) {
    188 		/* Hmm, failed to get capabilites. */
    189 		aprint_error_dev(psc->sc_dev,
    190 		    "synaptics_probe: Failed to query capabilities.\n");
    191 		goto doreset;
    192 	}
    193 
    194 	sc->caps = (resp[0] << 8) | resp[2];
    195 
    196 	if (sc->caps & SYNAPTICS_CAP_MBUTTON)
    197 		sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
    198 
    199 	if (sc->caps & SYNAPTICS_CAP_4BUTTON)
    200 		sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
    201 
    202 	if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
    203 		aprint_debug_dev(psc->sc_dev,
    204 		    "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
    205 		if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
    206 			sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
    207 
    208 		if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
    209 			sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
    210 
    211 		if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
    212 			sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
    213 
    214 		/* Ask about extra buttons to detect up/down. */
    215 		if (sc->caps & SYNAPTICS_CAP_EXTNUM) {
    216 			res = pms_sliced_command(psc->sc_kbctag,
    217 			    psc->sc_kbcslot, SYNAPTICS_EXTENDED_QUERY);
    218 			cmd[0] = PMS_SEND_DEV_STATUS;
    219 			res |= pckbport_poll_cmd(psc->sc_kbctag,
    220 			    psc->sc_kbcslot, cmd, 1, 3, resp, 0);
    221 			if (res == 0)
    222 				aprint_debug_dev(psc->sc_dev,
    223 				    "synaptics_probe: Extended "
    224 				    "Capabilities 0x%02x.\n", resp[1]);
    225 			if (!res && (resp[1] >> 4) >= 2) {
    226 				/* Yes. */
    227 				sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
    228 			}
    229 		}
    230 	}
    231 
    232 	if (sc->flags) {
    233 		const char comma[] = ", ";
    234 		const char *sep = "";
    235 		aprint_normal_dev(psc->sc_dev, "");
    236 		if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
    237 			aprint_normal("%sPassthrough", sep);
    238 			sep = comma;
    239 		}
    240 		if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
    241 			aprint_normal("%sMiddle button", sep);
    242 			sep = comma;
    243 		}
    244 		if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
    245 			aprint_normal("%sButtons 4/5", sep);
    246 			sep = comma;
    247 		}
    248 		if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS) {
    249 			aprint_normal("%sUp/down buttons", sep);
    250 			sep = comma;
    251 		}
    252 		if (sc->flags & SYN_FLAG_HAS_PALM_DETECT) {
    253 			aprint_normal("%sPalm detect", sep);
    254 			sep = comma;
    255 		}
    256 		if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER)
    257 			aprint_normal("%sMulti-finger", sep);
    258 
    259 		aprint_normal("\n");
    260 	}
    261 
    262 done:
    263 	pms_sysctl_synaptics(&clog);
    264 	pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
    265 	    pms_synaptics_input, psc, device_xname(psc->sc_dev));
    266 
    267 	return (0);
    268 }
    269 
    270 void
    271 pms_synaptics_enable(void *vsc)
    272 {
    273 	struct pms_softc *psc = vsc;
    274 	struct synaptics_softc *sc = &psc->u.synaptics;
    275 	u_char cmd[2], resp[2];
    276 	int res;
    277 
    278 	if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
    279 		/*
    280 		 * Extended capability probes can confuse the passthrough device;
    281 		 * reset the touchpad now to cure that.
    282 		 */
    283 		cmd[0] = PMS_RESET;
    284 		res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd,
    285 		    1, 2, resp, 1);
    286 	}
    287 
    288 	/*
    289 	 * Enable Absolute mode with W (width) reporting, and set
    290 	 * the packet rate to maximum (80 packets per second).
    291 	 */
    292 	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
    293 	    SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE);
    294 	cmd[0] = PMS_SET_SAMPLE;
    295 	cmd[1] = SYNAPTICS_CMD_SET_MODE2;
    296 	res |= pckbport_enqueue_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 2, 0,
    297 	    1, NULL);
    298 	sc->up_down = 0;
    299 	sc->prev_fingers = 0;
    300 	sc->gesture_start_x = sc->gesture_start_y = 0;
    301 	sc->gesture_start_packet = 0;
    302 	sc->gesture_tap_packet = 0;
    303 	sc->gesture_type = 0;
    304 	sc->gesture_buttons = 0;
    305 	sc->rem_x = sc->rem_y = 0;
    306 	sc->movement_history = 0;
    307 	if (res) {
    308 		aprint_error_dev(psc->sc_dev,
    309 		    "synaptics_enable: Error enabling device.\n");
    310 	}
    311 }
    312 
    313 void
    314 pms_synaptics_resume(void *vsc)
    315 {
    316 	struct pms_softc *psc = vsc;
    317 	unsigned char cmd[1],resp[2] = { 0,0 };
    318 	int res;
    319 
    320 	cmd[0] = PMS_RESET;
    321 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
    322 	    resp, 1);
    323 	aprint_debug_dev(psc->sc_dev,
    324 	    "pms_synaptics_resume: reset on resume %d 0x%02x 0x%02x\n",
    325 	    res, resp[0], resp[1]);
    326 }
    327 
    328 static void
    329 pms_sysctl_synaptics(struct sysctllog **clog)
    330 {
    331 	int rc, root_num;
    332 	const struct sysctlnode *node;
    333 
    334 	if ((rc = sysctl_createv(clog, 0, NULL, NULL,
    335 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
    336 	    NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0)
    337 		goto err;
    338 
    339 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    340 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
    341 	    SYSCTL_DESCR("Synaptics touchpad controls"),
    342 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
    343 	    goto err;
    344 
    345 	root_num = node->sysctl_num;
    346 
    347 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    348 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    349 	    CTLTYPE_INT, "up_down_emulation",
    350 	    SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
    351 	    pms_sysctl_synaptics_verify, 0,
    352 	    &synaptics_up_down_emul,
    353 	    0, CTL_HW, root_num, CTL_CREATE,
    354 	    CTL_EOL)) != 0)
    355 		goto err;
    356 
    357 	synaptics_up_down_emul_nodenum = node->sysctl_num;
    358 
    359 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    360 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    361 	    CTLTYPE_INT, "up_down_motion_delta",
    362 	    SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
    363 	    pms_sysctl_synaptics_verify, 0,
    364 	    &synaptics_up_down_motion_delta,
    365 	    0, CTL_HW, root_num, CTL_CREATE,
    366 	    CTL_EOL)) != 0)
    367 		goto err;
    368 
    369 	synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
    370 
    371 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    372 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    373 	    CTLTYPE_INT, "gesture_move",
    374 	    SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
    375 	    pms_sysctl_synaptics_verify, 0,
    376 	    &synaptics_gesture_move,
    377 	    0, CTL_HW, root_num, CTL_CREATE,
    378 	    CTL_EOL)) != 0)
    379 		goto err;
    380 
    381 	synaptics_gesture_move_nodenum = node->sysctl_num;
    382 
    383 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    384 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    385 	    CTLTYPE_INT, "gesture_length",
    386 	    SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
    387 	    pms_sysctl_synaptics_verify, 0,
    388 	    &synaptics_gesture_length,
    389 	    0, CTL_HW, root_num, CTL_CREATE,
    390 	    CTL_EOL)) != 0)
    391 		goto err;
    392 
    393 	synaptics_gesture_length_nodenum = node->sysctl_num;
    394 
    395 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    396 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    397 	    CTLTYPE_INT, "edge_left",
    398 	    SYSCTL_DESCR("Define left edge of touchpad"),
    399 	    pms_sysctl_synaptics_verify, 0,
    400 	    &synaptics_edge_left,
    401 	    0, CTL_HW, root_num, CTL_CREATE,
    402 	    CTL_EOL)) != 0)
    403 		goto err;
    404 
    405 	synaptics_edge_left_nodenum = node->sysctl_num;
    406 
    407 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    408 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    409 	    CTLTYPE_INT, "edge_right",
    410 	    SYSCTL_DESCR("Define right edge of touchpad"),
    411 	    pms_sysctl_synaptics_verify, 0,
    412 	    &synaptics_edge_right,
    413 	    0, CTL_HW, root_num, CTL_CREATE,
    414 	    CTL_EOL)) != 0)
    415 		goto err;
    416 
    417 	synaptics_edge_right_nodenum = node->sysctl_num;
    418 
    419 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    420 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    421 	    CTLTYPE_INT, "edge_top",
    422 	    SYSCTL_DESCR("Define top edge of touchpad"),
    423 	    pms_sysctl_synaptics_verify, 0,
    424 	    &synaptics_edge_top,
    425 	    0, CTL_HW, root_num, CTL_CREATE,
    426 	    CTL_EOL)) != 0)
    427 		goto err;
    428 
    429 	synaptics_edge_top_nodenum = node->sysctl_num;
    430 
    431 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    432 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    433 	    CTLTYPE_INT, "edge_bottom",
    434 	    SYSCTL_DESCR("Define bottom edge of touchpad"),
    435 	    pms_sysctl_synaptics_verify, 0,
    436 	    &synaptics_edge_bottom,
    437 	    0, CTL_HW, root_num, CTL_CREATE,
    438 	    CTL_EOL)) != 0)
    439 		goto err;
    440 
    441 	synaptics_edge_bottom_nodenum = node->sysctl_num;
    442 
    443 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    444 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    445 	    CTLTYPE_INT, "edge_motion_delta",
    446 	    SYSCTL_DESCR("Define edge motion rate"),
    447 	    pms_sysctl_synaptics_verify, 0,
    448 	    &synaptics_edge_motion_delta,
    449 	    0, CTL_HW, root_num, CTL_CREATE,
    450 	    CTL_EOL)) != 0)
    451 		goto err;
    452 
    453 	synaptics_edge_motion_delta_nodenum = node->sysctl_num;
    454 
    455 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    456 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    457 	    CTLTYPE_INT, "finger_high",
    458 	    SYSCTL_DESCR("Define finger applied pressure threshold"),
    459 	    pms_sysctl_synaptics_verify, 0,
    460 	    &synaptics_finger_high,
    461 	    0, CTL_HW, root_num, CTL_CREATE,
    462 	    CTL_EOL)) != 0)
    463 		goto err;
    464 
    465 	synaptics_finger_high_nodenum = node->sysctl_num;
    466 
    467 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    468 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    469 	    CTLTYPE_INT, "finger_low",
    470 	    SYSCTL_DESCR("Define finger removed pressure threshold"),
    471 	    pms_sysctl_synaptics_verify, 0,
    472 	    &synaptics_finger_low,
    473 	    0, CTL_HW, root_num, CTL_CREATE,
    474 	    CTL_EOL)) != 0)
    475 		goto err;
    476 
    477 	synaptics_finger_low_nodenum = node->sysctl_num;
    478 
    479 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    480 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    481 	    CTLTYPE_INT, "two_fingers_emulation",
    482 	    SYSCTL_DESCR("Map two fingers to middle button"),
    483 	    pms_sysctl_synaptics_verify, 0,
    484 	    &synaptics_two_fingers_emul,
    485 	    0, CTL_HW, root_num, CTL_CREATE,
    486 	    CTL_EOL)) != 0)
    487 		goto err;
    488 
    489 	synaptics_two_fingers_emul_nodenum = node->sysctl_num;
    490 
    491 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    492 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    493 	    CTLTYPE_INT, "scale_x",
    494 	    SYSCTL_DESCR("Horizontal movement scale factor"),
    495 	    pms_sysctl_synaptics_verify, 0,
    496 	    &synaptics_scale_x,
    497 	    0, CTL_HW, root_num, CTL_CREATE,
    498 	    CTL_EOL)) != 0)
    499 		goto err;
    500 
    501 	synaptics_scale_x_nodenum = node->sysctl_num;
    502 
    503 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    504 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    505 	    CTLTYPE_INT, "scale_y",
    506 	    SYSCTL_DESCR("Vertical movement scale factor"),
    507 	    pms_sysctl_synaptics_verify, 0,
    508 	    &synaptics_scale_y,
    509 	    0, CTL_HW, root_num, CTL_CREATE,
    510 	    CTL_EOL)) != 0)
    511 		goto err;
    512 
    513 	synaptics_scale_y_nodenum = node->sysctl_num;
    514 
    515 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    516 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    517 	    CTLTYPE_INT, "max_speed_x",
    518 	    SYSCTL_DESCR("Horizontal movement maximum speed"),
    519 	    pms_sysctl_synaptics_verify, 0,
    520 	    &synaptics_max_speed_x,
    521 	    0, CTL_HW, root_num, CTL_CREATE,
    522 	    CTL_EOL)) != 0)
    523 		goto err;
    524 
    525 	synaptics_max_speed_x_nodenum = node->sysctl_num;
    526 
    527 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    528 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    529 	    CTLTYPE_INT, "max_speed_y",
    530 	    SYSCTL_DESCR("Vertical movement maximum speed"),
    531 	    pms_sysctl_synaptics_verify, 0,
    532 	    &synaptics_max_speed_y,
    533 	    0, CTL_HW, root_num, CTL_CREATE,
    534 	    CTL_EOL)) != 0)
    535 		goto err;
    536 
    537 	synaptics_max_speed_y_nodenum = node->sysctl_num;
    538 
    539 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    540 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    541 	    CTLTYPE_INT, "movement_threshold",
    542 	    SYSCTL_DESCR("Minimum reported movement threshold"),
    543 	    pms_sysctl_synaptics_verify, 0,
    544 	    &synaptics_movement_threshold,
    545 	    0, CTL_HW, root_num, CTL_CREATE,
    546 	    CTL_EOL)) != 0)
    547 		goto err;
    548 
    549 	synaptics_movement_threshold_nodenum = node->sysctl_num;
    550 	return;
    551 
    552 err:
    553 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    554 }
    555 
    556 static int
    557 pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
    558 {
    559 	int error, t;
    560 	struct sysctlnode node;
    561 
    562 	node = *rnode;
    563 	t = *(int *)rnode->sysctl_data;
    564 	node.sysctl_data = &t;
    565 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    566 	if (error || newp == NULL)
    567 		return error;
    568 
    569 	/* Sanity check the params. */
    570 	if (node.sysctl_num == synaptics_up_down_emul_nodenum ||
    571 	    node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
    572 		if (t < 0 || t > 2)
    573 			return (EINVAL);
    574 	} else
    575 	if (node.sysctl_num == synaptics_gesture_length_nodenum ||
    576 	    node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
    577 	    node.sysctl_num == synaptics_up_down_motion_delta_nodenum) {
    578 		if (t < 0)
    579 			return (EINVAL);
    580 	} else
    581 	if (node.sysctl_num == synaptics_edge_left_nodenum ||
    582 	    node.sysctl_num == synaptics_edge_bottom_nodenum) {
    583 		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
    584 			return (EINVAL);
    585 	} else
    586 	if (node.sysctl_num == synaptics_edge_right_nodenum ||
    587 	    node.sysctl_num == synaptics_edge_top_nodenum) {
    588 		if (t < (SYNAPTICS_EDGE_MAX / 2))
    589 			return (EINVAL);
    590 	} else
    591 	if (node.sysctl_num == synaptics_scale_x_nodenum ||
    592 	    node.sysctl_num == synaptics_scale_y_nodenum) {
    593 		if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
    594 			return (EINVAL);
    595 	} else
    596 	if (node.sysctl_num == synaptics_finger_high_nodenum) {
    597 		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
    598 		    t < synaptics_finger_low)
    599 			return (EINVAL);
    600 	} else
    601 	if (node.sysctl_num == synaptics_finger_low_nodenum) {
    602 		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
    603 		    t > synaptics_finger_high)
    604 			return (EINVAL);
    605 	} else
    606 	if (node.sysctl_num == synaptics_gesture_move_nodenum ||
    607 	    node.sysctl_num == synaptics_movement_threshold_nodenum) {
    608 		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
    609 			return (EINVAL);
    610 	} else
    611 		return (EINVAL);
    612 
    613 	*(int *)rnode->sysctl_data = t;
    614 
    615 	return (0);
    616 }
    617 
    618 /* Masks for the first byte of a packet */
    619 #define PMS_LBUTMASK 0x01
    620 #define PMS_RBUTMASK 0x02
    621 #define PMS_MBUTMASK 0x04
    622 
    623 static void
    624 pms_synaptics_parse(struct pms_softc *psc)
    625 {
    626 	struct synaptics_softc *sc = &psc->u.synaptics;
    627 	struct synaptics_packet sp;
    628 
    629 	/* Absolute X/Y coordinates of finger */
    630 	sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
    631 	   ((psc->packet[3] & 0x10) << 8);
    632 	sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
    633 	   ((psc->packet[3] & 0x20) << 7);
    634 
    635 	/* Pressure */
    636 	sp.sp_z = psc->packet[2];
    637 
    638 	/* Width of finger */
    639 	sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
    640 	   ((psc->packet[0] & 0x04) >> 1) +
    641 	   ((psc->packet[3] & 0x04) >> 2);
    642 
    643 	/* Left/Right button handling. */
    644 	sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
    645 	sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
    646 
    647 	/* Up/Down buttons. */
    648 	if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
    649 		/* Old up/down buttons. */
    650 		sp.sp_up = sp.sp_left ^
    651 		    (psc->packet[3] & PMS_LBUTMASK);
    652 		sp.sp_down = sp.sp_right ^
    653 		    (psc->packet[3] & PMS_RBUTMASK);
    654 	} else
    655 	if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
    656 	   ((psc->packet[0] & PMS_RBUTMASK) ^
    657 	   (psc->packet[3] & PMS_RBUTMASK))) {
    658 		/* New up/down button. */
    659 		sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
    660 		sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
    661 	} else {
    662 		sp.sp_up = 0;
    663 		sp.sp_down = 0;
    664 	}
    665 
    666 	/* Middle button. */
    667 	if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
    668 		/* Old style Middle Button. */
    669 		sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
    670 		    (psc->packet[3] & PMS_LBUTMASK);
    671 	} else
    672 	if (synaptics_up_down_emul == 1) {
    673 		/* Do middle button emulation using up/down buttons */
    674 		sp.sp_middle = sp.sp_up | sp.sp_down;
    675 		sp.sp_up = sp.sp_down = 0;
    676 	} else
    677 		sp.sp_middle = 0;
    678 
    679 	pms_synaptics_process_packet(psc, &sp);
    680 }
    681 
    682 static void
    683 pms_synaptics_passthrough(struct pms_softc *psc)
    684 {
    685 	int dx, dy, dz;
    686 	int buttons, changed;
    687 	int s;
    688 
    689 	buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
    690 		((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
    691 		((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);
    692 
    693 	dx = psc->packet[4];
    694 	if (dx >= 128)
    695 		dx -= 256;
    696 	if (dx == -128)
    697 		dx = -127;
    698 
    699 	dy = psc->packet[5];
    700 	if (dy >= 128)
    701 		dy -= 256;
    702 	if (dy == -128)
    703 		dy = -127;
    704 
    705 	dz = 0;
    706 
    707 	changed = buttons ^ (psc->buttons & 0xe0);
    708 	psc->buttons ^= changed;
    709 
    710 	if (dx || dy || dz || changed) {
    711 		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
    712 		s = spltty();
    713 		wsmouse_input(psc->sc_wsmousedev,
    714 			buttons, dx, dy, dz, 0,
    715 			WSMOUSE_INPUT_DELTA);
    716 		splx(s);
    717 	}
    718 }
    719 
    720 static void
    721 pms_synaptics_input(void *vsc, int data)
    722 {
    723 	struct pms_softc *psc = vsc;
    724 	struct timeval diff;
    725 
    726 	if (!psc->sc_enabled) {
    727 		/* Interrupts are not expected.	 Discard the byte. */
    728 		return;
    729 	}
    730 
    731 	getmicrouptime(&psc->current);
    732 
    733 	if (psc->inputstate > 0) {
    734 		timersub(&psc->current, &psc->last, &diff);
    735 		if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
    736 			aprint_debug_dev(psc->sc_dev,
    737 			    "pms_input: unusual delay (%ld.%06ld s), "
    738 			    "scheduling reset\n",
    739 			    (long)diff.tv_sec, (long)diff.tv_usec);
    740 			psc->inputstate = 0;
    741 			psc->sc_enabled = 0;
    742 			wakeup(&psc->sc_enabled);
    743 			return;
    744 		}
    745 	}
    746 	psc->last = psc->current;
    747 
    748 	switch (psc->inputstate) {
    749 	case 0:
    750 		if ((data & 0xc8) != 0x80) {
    751 			aprint_debug_dev(psc->sc_dev,
    752 			    "pms_input: 0x%02x out of sync\n", data);
    753 			return;	/* not in sync yet, discard input */
    754 		}
    755 		/*FALLTHROUGH*/
    756 
    757 	case 3:
    758 		if ((data & 8) == 8) {
    759 			aprint_debug_dev(psc->sc_dev,
    760 			    "pms_input: dropped in relative mode, reset\n");
    761 			psc->inputstate = 0;
    762 			psc->sc_enabled = 0;
    763 			wakeup(&psc->sc_enabled);
    764 			return;
    765 		}
    766 	}
    767 
    768 	psc->packet[psc->inputstate++] = data & 0xff;
    769 	if (psc->inputstate == 6) {
    770 		/*
    771 		 * We have a complete packet.
    772 		 * Extract the pertinent details.
    773 		 */
    774 		psc->inputstate = 0;
    775 
    776 		if ((psc->packet[0] & 0xfc) == 0x84 &&
    777 		    (psc->packet[3] & 0xcc) == 0xc4) {
    778 			/* PS/2 passthrough */
    779 			pms_synaptics_passthrough(psc);
    780 		} else {
    781 			pms_synaptics_parse(psc);
    782 		}
    783 	}
    784 }
    785 
    786 static inline int
    787 synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
    788     int *palmp)
    789 {
    790 	int fingers;
    791 
    792 	/* Assume no palm */
    793 	*palmp = 0;
    794 
    795 	/*
    796 	 * Apply some hysteresis when checking for a finger.
    797 	 * When the finger is first applied, we ignore it until the
    798 	 * pressure exceeds the 'high' threshold. The finger is considered
    799 	 * removed only when pressure falls beneath the 'low' threshold.
    800 	 */
    801 	if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
    802 	    (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
    803 		fingers = 1;
    804 	else
    805 		fingers = 0;
    806 
    807 	/*
    808 	 * If the pad can't do palm detection, skip the rest.
    809 	 */
    810 	if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
    811 		return (fingers);
    812 
    813 	/*
    814 	 * Palm detection
    815 	 */
    816 	if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
    817 	    sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
    818 		*palmp = 1;
    819 
    820 	if (sc->prev_fingers == 0 &&
    821 	    (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
    822 	     sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
    823 		/*
    824 		 * Contact area or pressure is too great to be a finger.
    825 		 * Just ignore it for now.
    826 		 */
    827 		return (0);
    828 	}
    829 
    830 	/*
    831 	 * Detect 2 and 3 fingers if supported, but only if multiple
    832 	 * fingers appear within the tap gesture time period.
    833 	 */
    834 	if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
    835 	    SYN_TIME(sc, sc->gesture_start_packet) < synaptics_gesture_length) {
    836 		switch (sp->sp_w) {
    837 		case SYNAPTICS_WIDTH_TWO_FINGERS:
    838 			fingers = 2;
    839 			break;
    840 
    841 		case SYNAPTICS_WIDTH_THREE_OR_MORE:
    842 			fingers = 3;
    843 			break;
    844 
    845 		case SYNAPTICS_WIDTH_PEN:
    846 			fingers = 1;
    847 			break;
    848 
    849 		default:
    850 			/*
    851 			 * The width value can report spurious single-finger
    852 			 * events after a multi-finger event.
    853 			 */
    854 			if (sc->prev_fingers > 1)
    855 				fingers = sc->prev_fingers;
    856 			else
    857 				fingers = 1;
    858 			break;
    859 		}
    860 	}
    861 
    862 	return (fingers);
    863 }
    864 
    865 static inline void
    866 synaptics_gesture_detect(struct synaptics_softc *sc,
    867     struct synaptics_packet *sp, int fingers)
    868 {
    869 	int gesture_len, gesture_move_x, gesture_move_y, gesture_buttons;
    870 	int set_buttons;
    871 
    872 	gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
    873 	gesture_buttons = sc->gesture_buttons;
    874 
    875 	if (fingers && sc->prev_fingers == 0) {
    876 		/*
    877 		 * Finger was just applied.
    878 		 * If the previous gesture was a single-click, set things
    879 		 * up to deal with a possible drag or double-click gesture.
    880 		 * Basically, if the finger is removed again within
    881 		 * 'synaptics_gesture_length' packets, this is treated
    882 		 * as a double-click. Otherwise we will emulate holding
    883 		 * the left button down whilst dragging the mouse.
    884 		 */
    885 		if (SYN_IS_SINGLE_TAP(sc->gesture_type))
    886 			sc->gesture_type |= SYN_GESTURE_DRAG;
    887 
    888 		sc->gesture_start_x = sp->sp_x;
    889 		sc->gesture_start_y = sp->sp_y;
    890 		sc->gesture_start_packet = sc->total_packets;
    891 	} else
    892 	if (fingers == 0 && sc->prev_fingers != 0) {
    893 		/*
    894 		 * Finger was just removed.
    895 		 * Check if the contact time and finger movement were
    896 		 * small enough to qualify as a gesture.
    897 		 * Ignore finger movement if multiple fingers were
    898 		 * detected (the pad may report coordinates for any
    899 		 * of the fingers).
    900 		 */
    901 		gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
    902 		gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
    903 
    904 		if (gesture_len < synaptics_gesture_length &&
    905 		    (sc->prev_fingers > 1 ||
    906 		    (gesture_move_x < synaptics_gesture_move &&
    907 		     gesture_move_y < synaptics_gesture_move))) {
    908 			/*
    909 			 * Looking good so far.
    910 			 */
    911 			if (SYN_IS_DRAG(sc->gesture_type)) {
    912 				/*
    913 				 * Promote this gesture to double-click.
    914 				 */
    915 				sc->gesture_type |= SYN_GESTURE_DOUBLE;
    916 				sc->gesture_type &= ~SYN_GESTURE_SINGLE;
    917 			} else {
    918 				/*
    919 				 * Single tap gesture. Set the tap length timer
    920 				 * and flag a single-click.
    921 				 */
    922 				sc->gesture_tap_packet = sc->total_packets;
    923 				sc->gesture_type |= SYN_GESTURE_SINGLE;
    924 
    925 				/*
    926 				 * The gesture can be modified depending on
    927 				 * the number of fingers detected.
    928 				 *
    929 				 * 1: Normal left button emulation.
    930 				 * 2: Either middle button or right button
    931 				 *    depending on the value of the two_fingers
    932 				 *    sysctl variable.
    933 				 * 3: Right button.
    934 				 */
    935 				switch (sc->prev_fingers) {
    936 				case 2:
    937 					if (synaptics_two_fingers_emul == 1)
    938 						gesture_buttons |= PMS_RBUTMASK;
    939 					else
    940 					if (synaptics_two_fingers_emul == 2)
    941 						gesture_buttons |= PMS_MBUTMASK;
    942 					break;
    943 				case 3:
    944 					gesture_buttons |= PMS_RBUTMASK;
    945 					break;
    946 				default:
    947 					gesture_buttons |= PMS_LBUTMASK;
    948 					break;
    949 				}
    950 			}
    951 		}
    952 
    953 		/*
    954 		 * Always clear drag state when the finger is removed.
    955 		 */
    956 		sc->gesture_type &= ~SYN_GESTURE_DRAG;
    957 	}
    958 
    959 	if (sc->gesture_type == 0) {
    960 		/*
    961 		 * There is no gesture in progress.
    962 		 * Clear emulated button state.
    963 		 */
    964 		sc->gesture_buttons = 0;
    965 		return;
    966 	}
    967 
    968 	/*
    969 	 * A gesture is in progress.
    970 	 */
    971 	set_buttons = 0;
    972 
    973 	if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
    974 		/*
    975 		 * Single-click.
    976 		 * Activate the relevant button(s) until the
    977 		 * gesture tap timer has expired.
    978 		 */
    979 		if (SYN_TIME(sc, sc->gesture_tap_packet) <
    980 		    synaptics_gesture_length)
    981 			set_buttons = 1;
    982 		else
    983 			sc->gesture_type &= ~SYN_GESTURE_SINGLE;
    984 	} else
    985 	if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
    986 		/*
    987 		 * Double-click.
    988 		 * Activate the relevant button(s) once.
    989 		 */
    990 		set_buttons = 1;
    991 		sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
    992 	}
    993 
    994 	if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
    995 		/*
    996 		 * Single-click and drag.
    997 		 * Maintain button state until the finger is removed.
    998 		 */
    999 		sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
   1000 		sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
   1001 		sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
   1002 	}
   1003 
   1004 	sc->gesture_buttons = gesture_buttons;
   1005 }
   1006 
   1007 static inline int
   1008 synaptics_filter_policy(struct synaptics_softc *sc, int *history, int value)
   1009 {
   1010 	int a, b, rv, count;
   1011 
   1012 	count = sc->total_packets;
   1013 
   1014 	/*
   1015 	 * Once we've accumulated at least SYN_HIST_SIZE values, combine
   1016 	 * each new value with the previous two and return the average.
   1017 	 *
   1018 	 * This is necessary when the touchpad is operating in 80 packets
   1019 	 * per second mode, as it performs little internal filtering on
   1020 	 * reported values.
   1021 	 *
   1022 	 * Using a rolling average helps to filter out jitter caused by
   1023 	 * tiny finger movements.
   1024 	 */
   1025 	if (sc->movement_history >= SYN_HIST_SIZE) {
   1026 		a = (history[(count + 0) % SYN_HIST_SIZE] +
   1027 		    history[(count + 1) % SYN_HIST_SIZE]) / 2;
   1028 
   1029 		b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
   1030 
   1031 		rv = b - a;
   1032 
   1033 		/*
   1034 		 * Don't report the movement if it's below a certain
   1035 		 * threshold.
   1036 		 */
   1037 		if (abs(rv) < synaptics_movement_threshold)
   1038 			rv = 0;
   1039 	} else
   1040 		rv = 0;
   1041 
   1042 	/*
   1043 	 * Add the new value to the history buffer.
   1044 	 */
   1045 	history[(count + 1) % SYN_HIST_SIZE] = value;
   1046 
   1047 	return (rv);
   1048 }
   1049 
   1050 /* Edge detection */
   1051 #define	SYN_EDGE_TOP		1
   1052 #define	SYN_EDGE_BOTTOM		2
   1053 #define	SYN_EDGE_LEFT		4
   1054 #define	SYN_EDGE_RIGHT		8
   1055 
   1056 static inline int
   1057 synaptics_check_edge(int x, int y)
   1058 {
   1059 	int rv = 0;
   1060 
   1061 	if (x < synaptics_edge_left)
   1062 		rv |= SYN_EDGE_LEFT;
   1063 	else
   1064 	if (x > synaptics_edge_right)
   1065 		rv |= SYN_EDGE_RIGHT;
   1066 
   1067 	if (y < synaptics_edge_bottom)
   1068 		rv |= SYN_EDGE_BOTTOM;
   1069 	else
   1070 	if (y > synaptics_edge_top)
   1071 		rv |= SYN_EDGE_TOP;
   1072 
   1073 	return (rv);
   1074 }
   1075 
   1076 static inline int
   1077 synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
   1078 {
   1079 
   1080 	/*
   1081 	 * When edge motion is enabled, synaptics_edge_motion_delta is
   1082 	 * combined with the current delta, together with the direction
   1083 	 * in which to simulate the motion. The result is added to
   1084 	 * the delta derived from finger movement. This provides a smooth
   1085 	 * transition from finger movement to edge motion.
   1086 	 */
   1087 	delta = synaptics_edge_motion_delta + (dir * delta);
   1088 	if (delta < 0)
   1089 		return (0);
   1090 	if (delta > synaptics_edge_motion_delta)
   1091 		return (synaptics_edge_motion_delta);
   1092 	return (delta);
   1093 }
   1094 
   1095 static inline int
   1096 synaptics_scale(int delta, int scale, int *remp)
   1097 {
   1098 	int rv;
   1099 
   1100 	/*
   1101 	 * Scale the raw delta in Synaptics coordinates (0-6143) into
   1102 	 * something more reasonable by dividing the raw delta by a
   1103 	 * scale factor. Any remainder from the previous scale result
   1104 	 * is added to the current delta before scaling.
   1105 	 * This prevents loss of resolution for very small/slow
   1106 	 * movements of the finger.
   1107 	 */
   1108 	delta += *remp;
   1109 	rv = delta / scale;
   1110 	*remp = delta % scale;
   1111 
   1112 	return (rv);
   1113 }
   1114 
   1115 static inline void
   1116 synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
   1117     int *dxp, int *dyp)
   1118 {
   1119 	int dx, dy, edge;
   1120 
   1121 	/*
   1122 	 * Compute the next values of dx and dy
   1123 	 */
   1124 	dx = synaptics_filter_policy(sc, sc->history_x, sp->sp_x);
   1125 	dy = synaptics_filter_policy(sc, sc->history_y, sp->sp_y);
   1126 
   1127 	/*
   1128 	 * If we're dealing with a drag gesture, and the finger moves to
   1129 	 * the edge of the touchpad, apply edge motion emulation if it
   1130 	 * is enabled.
   1131 	 */
   1132 	if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
   1133 		edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
   1134 
   1135 		if (edge & SYN_EDGE_LEFT)
   1136 			dx -= synaptics_edge_motion(sc, dx, 1);
   1137 		if (edge & SYN_EDGE_RIGHT)
   1138 			dx += synaptics_edge_motion(sc, dx, -1);
   1139 		if (edge & SYN_EDGE_BOTTOM)
   1140 			dy -= synaptics_edge_motion(sc, dy, 1);
   1141 		if (edge & SYN_EDGE_TOP)
   1142 			dy += synaptics_edge_motion(sc, dy, -1);
   1143 	}
   1144 
   1145 	/*
   1146 	 * Apply scaling to both deltas
   1147 	 */
   1148 	dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x);
   1149 	dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y);
   1150 
   1151 	/*
   1152 	 * Clamp deltas to specified maximums.
   1153 	 */
   1154 	if (dx > synaptics_max_speed_x)
   1155 		dx = synaptics_max_speed_x;
   1156 	if (dy > synaptics_max_speed_y)
   1157 		dy = synaptics_max_speed_y;
   1158 
   1159 	*dxp = dx;
   1160 	*dyp = dy;
   1161 
   1162 	sc->movement_history++;
   1163 }
   1164 
   1165 static void
   1166 pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
   1167 {
   1168 	struct synaptics_softc *sc = &psc->u.synaptics;
   1169 	int dx, dy, dz;
   1170 	int fingers, palm, buttons, changed;
   1171 	int s;
   1172 
   1173 	/*
   1174 	 * Do Z-axis emulation using up/down buttons if required.
   1175 	 * Note that the pad will send a one second burst of packets
   1176 	 * when an up/down button is pressed and held. At the moment
   1177 	 * we don't deal with auto-repeat, so convert the burst into
   1178 	 * a one-shot.
   1179 	 */
   1180 	dz = 0;
   1181 	if (synaptics_up_down_emul == 2) {
   1182 		if (sc->up_down == 0) {
   1183 			if (sp->sp_up && sp->sp_down) {
   1184 				/*
   1185 				 * Most up/down buttons will be actuated using
   1186 				 * a rocker switch, so we should never see
   1187 				 * them both simultaneously. But just in case,
   1188 				 * treat this situation as a middle button
   1189 				 * event.
   1190 				 */
   1191 				sp->sp_middle = 1;
   1192 			} else
   1193 			if (sp->sp_up)
   1194 				dz = -synaptics_up_down_motion_delta;
   1195 			else
   1196 			if (sp->sp_down)
   1197 				dz = synaptics_up_down_motion_delta;
   1198 		}
   1199 
   1200 		sc->up_down = sp->sp_up | sp->sp_down;
   1201 		sp->sp_up = sp->sp_down = 0;
   1202 	}
   1203 
   1204 	/*
   1205 	 * Determine whether or not a finger is on the pad.
   1206 	 * On some pads, this will return the number of fingers
   1207 	 * detected.
   1208 	 */
   1209 	fingers = synaptics_finger_detect(sc, sp, &palm);
   1210 
   1211 	/*
   1212 	 * Do gesture processing only if we didn't detect a palm.
   1213 	 */
   1214 	if (palm == 0)
   1215 		synaptics_gesture_detect(sc, sp, fingers);
   1216 	else
   1217 		sc->gesture_type = sc->gesture_buttons = 0;
   1218 
   1219 	/*
   1220 	 * Determine what buttons to report
   1221 	 */
   1222 	buttons = (sp->sp_left ? 0x1 : 0) |
   1223 	    (sp->sp_middle ? 0x2 : 0) |
   1224 	    (sp->sp_right ? 0x4 : 0) |
   1225 	    (sp->sp_up ? 0x8 : 0) |
   1226 	    (sp->sp_down ? 0x10 : 0);
   1227 	changed = buttons ^ (psc->buttons & 0x1f);
   1228 	psc->buttons ^= changed;
   1229 
   1230 	sc->prev_fingers = fingers;
   1231 	sc->total_packets++;
   1232 
   1233 	/*
   1234 	 * Do movement processing IFF we have a single finger and no palm.
   1235 	 */
   1236 	if (fingers == 1 && palm == 0)
   1237 		synaptics_movement(sc, sp, &dx, &dy);
   1238 	else {
   1239 		/*
   1240 		 * No valid finger. Therefore no movement.
   1241 		 */
   1242 		sc->movement_history = 0;
   1243 		sc->rem_x = sc->rem_y = 0;
   1244 		dx = dy = 0;
   1245 	}
   1246 
   1247 	/*
   1248 	 * Pass the final results up to wsmouse_input() if necessary.
   1249 	 */
   1250 	if (dx || dy || dz || changed) {
   1251 		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
   1252 		s = spltty();
   1253 		wsmouse_input(psc->sc_wsmousedev,
   1254 				buttons,
   1255 				dx, dy, dz, 0,
   1256 		    		WSMOUSE_INPUT_DELTA);
   1257 		splx(s);
   1258 	}
   1259 }
   1260