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