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