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