Home | History | Annotate | Line # | Download | only in pckbport
synaptics.c revision 1.70.8.1
      1  1.70.8.1      cjep /*	$NetBSD: synaptics.c,v 1.70.8.1 2021/05/31 22:15:18 cjep 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.70.8.1      cjep __KERNEL_RCSID(0, "$NetBSD: synaptics.c,v 1.70.8.1 2021/05/31 22:15:18 cjep Exp $");
     52       1.1  christos 
     53       1.1  christos #include <sys/param.h>
     54       1.1  christos #include <sys/systm.h>
     55       1.1  christos #include <sys/device.h>
     56       1.1  christos #include <sys/ioctl.h>
     57       1.1  christos #include <sys/sysctl.h>
     58       1.1  christos #include <sys/kernel.h>
     59      1.25  uebayasi #include <sys/proc.h>
     60       1.1  christos 
     61      1.16        ad #include <sys/bus.h>
     62       1.1  christos 
     63       1.1  christos #include <dev/pckbport/pckbportvar.h>
     64       1.1  christos 
     65       1.1  christos #include <dev/pckbport/synapticsreg.h>
     66       1.1  christos #include <dev/pckbport/synapticsvar.h>
     67       1.1  christos 
     68       1.1  christos #include <dev/pckbport/pmsreg.h>
     69       1.1  christos #include <dev/pckbport/pmsvar.h>
     70       1.1  christos 
     71       1.1  christos #include <dev/wscons/wsconsio.h>
     72       1.1  christos #include <dev/wscons/wsmousevar.h>
     73       1.1  christos 
     74       1.3       scw /*
     75       1.3       scw  * Absolute-mode packets are decoded and passed around using
     76       1.3       scw  * the following structure.
     77       1.3       scw  */
     78       1.3       scw struct synaptics_packet {
     79       1.3       scw 	signed short	sp_x;	/* Unscaled absolute X/Y coordinates */
     80       1.3       scw 	signed short	sp_y;
     81       1.3       scw 	u_char	sp_z;		/* Z (pressure) */
     82       1.3       scw 	u_char	sp_w;		/* W (contact patch width) */
     83      1.34     blymn 	signed short	sp_sx;	/* Secondary finger unscaled absolute */
     84      1.34     blymn 				/* X/Y coordinates */
     85      1.34     blymn 	signed short	sp_xy;
     86      1.34     blymn 	u_char	sp_finger;	/* 0 for primary, 1 for secondary */
     87       1.3       scw 	char	sp_left;	/* Left mouse button status */
     88       1.3       scw 	char	sp_right;	/* Right mouse button status */
     89       1.3       scw 	char	sp_middle;	/* Middle button status (possibly emulated) */
     90       1.3       scw 	char	sp_up;		/* Up button status */
     91       1.3       scw 	char	sp_down;	/* Down button status */
     92       1.3       scw };
     93       1.3       scw 
     94       1.1  christos static void pms_synaptics_input(void *, int);
     95       1.3       scw static void pms_synaptics_process_packet(struct pms_softc *,
     96       1.3       scw 		struct synaptics_packet *);
     97       1.2  christos static void pms_sysctl_synaptics(struct sysctllog **);
     98       1.1  christos static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);
     99       1.1  christos 
    100      1.28  jakllsch /* Controlled by sysctl. */
    101      1.56       nia static int synaptics_up_down_emul = 3;
    102       1.3       scw static int synaptics_up_down_motion_delta = 1;
    103       1.3       scw static int synaptics_gesture_move = 200;
    104       1.3       scw static int synaptics_gesture_length = 20;
    105       1.3       scw static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
    106       1.3       scw static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
    107       1.3       scw static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
    108       1.3       scw static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
    109       1.3       scw static int synaptics_edge_motion_delta = 32;
    110       1.3       scw static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
    111       1.3       scw static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
    112      1.34     blymn static int synaptics_button_boundary = SYNAPTICS_EDGE_BOTTOM + 720;
    113      1.34     blymn static int synaptics_button2 = SYNAPTICS_EDGE_LEFT + (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
    114      1.34     blymn static int synaptics_button3 = SYNAPTICS_EDGE_LEFT + 2 * (SYNAPTICS_EDGE_RIGHT - SYNAPTICS_EDGE_LEFT) / 3;
    115       1.3       scw static int synaptics_two_fingers_emul = 0;
    116       1.3       scw static int synaptics_scale_x = 16;
    117       1.3       scw static int synaptics_scale_y = 16;
    118      1.63       nia static int synaptics_scale_z = 32;
    119       1.3       scw static int synaptics_max_speed_x = 32;
    120       1.3       scw static int synaptics_max_speed_y = 32;
    121      1.63       nia static int synaptics_max_speed_z = 2;
    122       1.3       scw static int synaptics_movement_threshold = 4;
    123      1.45     blymn static int synaptics_fscroll_min = 13;
    124      1.45     blymn static int synaptics_fscroll_max = 14;
    125      1.44     blymn static int synaptics_dz_hold = 30;
    126      1.36  jmcneill static int synaptics_movement_enable = 1;
    127      1.70       nia static bool synaptics_aux_mid_button_scroll = TRUE;
    128  1.70.8.1      cjep static int synaptics_debug = 0;
    129  1.70.8.1      cjep 
    130  1.70.8.1      cjep #define	DPRINTF(SC, FMT, ARGS...) do					      \
    131  1.70.8.1      cjep {									      \
    132  1.70.8.1      cjep 	if (synaptics_debug) {						      \
    133  1.70.8.1      cjep 		struct pms_softc *_dprintf_psc =			      \
    134  1.70.8.1      cjep 		    container_of((SC), struct pms_softc, u.synaptics);	      \
    135  1.70.8.1      cjep 		device_printf(_dprintf_psc->sc_dev, FMT, ##ARGS);	      \
    136  1.70.8.1      cjep 	}								      \
    137  1.70.8.1      cjep } while (0)
    138       1.1  christos 
    139       1.1  christos /* Sysctl nodes. */
    140      1.34     blymn static int synaptics_button_boundary_nodenum;
    141      1.34     blymn static int synaptics_button2_nodenum;
    142      1.34     blymn static int synaptics_button3_nodenum;
    143       1.3       scw static int synaptics_up_down_emul_nodenum;
    144       1.3       scw static int synaptics_up_down_motion_delta_nodenum;
    145       1.3       scw static int synaptics_gesture_move_nodenum;
    146       1.3       scw static int synaptics_gesture_length_nodenum;
    147       1.3       scw static int synaptics_edge_left_nodenum;
    148       1.3       scw static int synaptics_edge_right_nodenum;
    149       1.3       scw static int synaptics_edge_top_nodenum;
    150       1.3       scw static int synaptics_edge_bottom_nodenum;
    151       1.3       scw static int synaptics_edge_motion_delta_nodenum;
    152       1.3       scw static int synaptics_finger_high_nodenum;
    153       1.3       scw static int synaptics_finger_low_nodenum;
    154       1.3       scw static int synaptics_two_fingers_emul_nodenum;
    155       1.3       scw static int synaptics_scale_x_nodenum;
    156       1.3       scw static int synaptics_scale_y_nodenum;
    157      1.44     blymn static int synaptics_scale_z_nodenum;
    158       1.3       scw static int synaptics_max_speed_x_nodenum;
    159       1.3       scw static int synaptics_max_speed_y_nodenum;
    160      1.44     blymn static int synaptics_max_speed_z_nodenum;
    161       1.3       scw static int synaptics_movement_threshold_nodenum;
    162      1.44     blymn static int synaptics_finger_scroll_min_nodenum;
    163      1.44     blymn static int synaptics_finger_scroll_max_nodenum;
    164      1.44     blymn static int synaptics_dz_hold_nodenum;
    165      1.36  jmcneill static int synaptics_movement_enable_nodenum;
    166      1.70       nia static int synaptics_aux_mid_button_scroll_nodenum;
    167       1.1  christos 
    168      1.34     blymn static int
    169      1.34     blymn synaptics_poll_cmd(struct pms_softc *psc, ...)
    170      1.34     blymn {
    171      1.34     blymn 	u_char cmd[4];
    172      1.34     blymn 	size_t i;
    173      1.34     blymn 	va_list ap;
    174      1.34     blymn 
    175      1.34     blymn 	va_start(ap, psc);
    176      1.34     blymn 
    177      1.34     blymn 	for (i = 0; i < __arraycount(cmd); i++)
    178      1.34     blymn 		if ((cmd[i] = (u_char)va_arg(ap, int)) == 0)
    179      1.34     blymn 			break;
    180      1.34     blymn 	va_end(ap);
    181      1.34     blymn 
    182      1.34     blymn 	int res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, i, 0,
    183      1.34     blymn     	    NULL, 0);
    184      1.34     blymn 	if (res)
    185      1.34     blymn 		aprint_error_dev(psc->sc_dev, "command error %#x\n", cmd[0]);
    186      1.34     blymn 	return res;
    187      1.34     blymn }
    188      1.34     blymn 
    189      1.34     blymn static int
    190      1.34     blymn synaptics_poll_reset(struct pms_softc *psc)
    191      1.34     blymn {
    192      1.34     blymn 	u_char resp[2];
    193      1.34     blymn 	int res;
    194      1.34     blymn 
    195      1.34     blymn 	u_char cmd[1] = { PMS_RESET };
    196      1.34     blymn 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
    197      1.34     blymn 	    resp, 1);
    198      1.34     blymn 	aprint_debug_dev(psc->sc_dev, "reset %d 0x%02x 0x%02x\n",
    199      1.34     blymn 	    res, resp[0], resp[1]);
    200      1.34     blymn 	return res;
    201      1.34     blymn }
    202      1.34     blymn 
    203      1.34     blymn static int
    204      1.42      maya synaptics_special_read(struct pms_softc *psc, u_char slice, u_char resp[3])
    205      1.34     blymn {
    206      1.34     blymn 	u_char cmd[1] = { PMS_SEND_DEV_STATUS };
    207      1.34     blymn 	int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, slice);
    208      1.34     blymn 
    209      1.34     blymn 	return res | pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    210      1.34     blymn 	    cmd, 1, 3, resp, 0);
    211      1.34     blymn }
    212      1.34     blymn 
    213      1.42      maya static int
    214      1.42      maya synaptics_special_write(struct pms_softc *psc, u_char command, u_char arg)
    215      1.42      maya {
    216      1.42      maya 	int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, arg);
    217      1.42      maya 	if (res)
    218      1.42      maya 		return res;
    219      1.42      maya 
    220      1.42      maya 	u_char cmd[2];
    221      1.42      maya 	cmd[0] = PMS_SET_SAMPLE;
    222      1.42      maya 	cmd[1] = command;
    223      1.42      maya 	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
    224      1.42      maya 	    cmd, 2, 0, NULL, 0);
    225      1.42      maya 	return res;
    226      1.42      maya }
    227      1.42      maya 
    228      1.32  christos static void
    229      1.32  christos pms_synaptics_probe_extended(struct pms_softc *psc)
    230      1.32  christos {
    231      1.32  christos 	struct synaptics_softc *sc = &psc->u.synaptics;
    232      1.34     blymn 	u_char resp[3];
    233      1.32  christos 	int res;
    234      1.32  christos 
    235      1.32  christos 	aprint_debug_dev(psc->sc_dev,
    236      1.32  christos 	    "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
    237      1.32  christos 	if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
    238      1.32  christos 		sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;
    239      1.32  christos 
    240      1.32  christos 	if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
    241      1.32  christos 		sc->flags |= SYN_FLAG_HAS_PALM_DETECT;
    242      1.32  christos 
    243      1.32  christos 	if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
    244      1.32  christos 		sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;
    245      1.32  christos 
    246      1.32  christos 	if (sc->caps & SYNAPTICS_CAP_MULTIFINGERREPORT)
    247      1.32  christos 		sc->flags |= SYN_FLAG_HAS_MULTI_FINGER_REPORT;
    248      1.32  christos 
    249      1.32  christos 	/* Ask about extra buttons to detect up/down. */
    250      1.32  christos 	if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08)
    251      1.32  christos 	    >= SYNAPTICS_EXTENDED_QUERY)
    252      1.32  christos 	{
    253      1.42      maya 		res = synaptics_special_read(psc, SYNAPTICS_EXTENDED_QUERY, resp);
    254      1.32  christos 		if (res == 0) {
    255      1.32  christos 			int buttons = (resp[1] >> 4);
    256      1.32  christos 			aprint_debug_dev(psc->sc_dev,
    257      1.32  christos 			    "%s: Extended Buttons: %d.\n", __func__, buttons);
    258      1.32  christos 
    259      1.32  christos 			aprint_debug_dev(psc->sc_dev, "%s: Extended "
    260      1.32  christos 			    "Capabilities: 0x%02x 0x%02x 0x%02x.\n", __func__,
    261      1.32  christos 			    resp[0], resp[1], resp[2]);
    262      1.32  christos 			if (buttons >= 2) {
    263      1.32  christos 				/* Yes. */
    264      1.32  christos 				sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
    265      1.32  christos 			}
    266      1.32  christos 			if (resp[0] & 0x1) {
    267      1.32  christos 				/* Vertical scroll area */
    268      1.32  christos 				sc->flags |= SYN_FLAG_HAS_VERTICAL_SCROLL;
    269      1.32  christos 			}
    270      1.32  christos 			if (resp[0] & 0x2) {
    271      1.32  christos 				/* Horizontal scroll area */
    272      1.32  christos 				sc->flags |= SYN_FLAG_HAS_HORIZONTAL_SCROLL;
    273      1.32  christos 			}
    274      1.32  christos 			if (resp[0] & 0x4) {
    275      1.32  christos 				/* Extended W-Mode */
    276      1.32  christos 				sc->flags |= SYN_FLAG_HAS_EXTENDED_WMODE;
    277      1.32  christos 			}
    278      1.32  christos 		}
    279      1.32  christos 	}
    280      1.32  christos 
    281      1.32  christos 	/* Ask about click pad */
    282      1.32  christos 	if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) >=
    283      1.32  christos 	    SYNAPTICS_CONTINUED_CAPABILITIES)
    284      1.32  christos 	{
    285      1.42      maya 		res = synaptics_special_read(psc,
    286      1.34     blymn 		    SYNAPTICS_CONTINUED_CAPABILITIES, resp);
    287      1.34     blymn 
    288      1.33  christos /*
    289      1.33  christos  * The following describes response for the
    290      1.33  christos  * SYNAPTICS_CONTINUED_CAPABILITIES query.
    291      1.33  christos  *
    292      1.33  christos  * byte	mask	name			meaning
    293      1.33  christos  * ----	----	-------			------------
    294      1.33  christos  * 0	0x01	adjustable threshold	capacitive button sensitivity
    295      1.33  christos  *					can be adjusted
    296      1.33  christos  * 0	0x02	report max		query 0x0d gives max coord reported
    297      1.33  christos  * 0	0x04	clearpad		sensor is ClearPad product
    298      1.33  christos  * 0	0x08	advanced gesture	not particularly meaningful
    299      1.33  christos  * 0	0x10	clickpad bit 0		1-button ClickPad
    300      1.33  christos  * 0	0x60	multifinger mode	identifies firmware finger counting
    301      1.33  christos  *					(not reporting!) algorithm.
    302      1.33  christos  *					Not particularly meaningful
    303      1.33  christos  * 0	0x80	covered pad		W clipped to 14, 15 == pad mostly covered
    304      1.33  christos  * 1	0x01	clickpad bit 1		2-button ClickPad
    305      1.33  christos  * 1	0x02	deluxe LED controls	touchpad support LED commands
    306      1.33  christos  *					ala multimedia control bar
    307      1.33  christos  * 1	0x04	reduced filtering	firmware does less filtering on
    308      1.33  christos  *					position data, driver should watch
    309      1.33  christos  *					for noise.
    310      1.33  christos  * 1	0x08	image sensor		image sensor tracks 5 fingers, but only
    311      1.33  christos  *					reports 2.
    312      1.47     blymn  * 1	0x10	uniform clickpad	whole clickpad moves instead of being
    313      1.33  christos  *					hinged at the top.
    314      1.33  christos  * 1	0x20	report min		query 0x0f gives min coord reported
    315      1.33  christos  */
    316      1.32  christos 		if (res == 0) {
    317      1.48     blymn 			uint val = SYN_CCAP_VALUE(resp);
    318      1.32  christos 
    319      1.32  christos 			aprint_debug_dev(psc->sc_dev, "%s: Continued "
    320      1.32  christos 			    "Capabilities 0x%02x 0x%02x 0x%02x.\n", __func__,
    321      1.32  christos 			    resp[0], resp[1], resp[2]);
    322      1.48     blymn 			switch (SYN_CCAP_CLICKPAD_TYPE(val)) {
    323      1.48     blymn 			case 0: /* not a clickpad */
    324      1.48     blymn 				break;
    325      1.48     blymn 			case 1:
    326      1.32  christos 				sc->flags |= SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD;
    327      1.32  christos 				break;
    328      1.48     blymn 			case 2:
    329      1.32  christos 				sc->flags |= SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD;
    330      1.32  christos 				break;
    331      1.48     blymn 			case 3: /* reserved */
    332      1.32  christos 			default:
    333      1.48     blymn 				/* unreached */
    334      1.32  christos 				break;
    335      1.32  christos 			}
    336      1.49     blymn 
    337      1.49     blymn 			if ((val & SYN_CCAP_HAS_ADV_GESTURE_MODE))
    338      1.49     blymn 				sc->flags |= SYN_FLAG_HAS_ADV_GESTURE_MODE;
    339      1.32  christos 		}
    340      1.32  christos 	}
    341      1.32  christos }
    342      1.32  christos 
    343      1.40  christos static const struct {
    344      1.40  christos 	int bit;
    345      1.40  christos 	const char *desc;
    346      1.40  christos } syn_flags[] = {
    347      1.40  christos 	{ SYN_FLAG_HAS_EXTENDED_WMODE, "Extended W mode", },
    348      1.40  christos 	{ SYN_FLAG_HAS_PASSTHROUGH, "Passthrough", },
    349      1.40  christos 	{ SYN_FLAG_HAS_MIDDLE_BUTTON, "Middle button", },
    350      1.40  christos 	{ SYN_FLAG_HAS_BUTTONS_4_5, "Buttons 4/5", },
    351      1.40  christos 	{ SYN_FLAG_HAS_UP_DOWN_BUTTONS, "Up/down buttons", },
    352      1.40  christos 	{ SYN_FLAG_HAS_PALM_DETECT, "Palm detect", },
    353      1.40  christos 	{ SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD, "One button click pad", },
    354      1.40  christos 	{ SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD, "Two button click pad", },
    355      1.40  christos 	{ SYN_FLAG_HAS_VERTICAL_SCROLL, "Vertical scroll", },
    356      1.40  christos 	{ SYN_FLAG_HAS_HORIZONTAL_SCROLL, "Horizontal scroll", },
    357      1.40  christos 	{ SYN_FLAG_HAS_MULTI_FINGER_REPORT, "Multi-finger Report", },
    358      1.40  christos 	{ SYN_FLAG_HAS_MULTI_FINGER, "Multi-finger", },
    359      1.40  christos };
    360      1.40  christos 
    361       1.1  christos int
    362       1.1  christos pms_synaptics_probe_init(void *vsc)
    363       1.1  christos {
    364       1.3       scw 	struct pms_softc *psc = vsc;
    365       1.3       scw 	struct synaptics_softc *sc = &psc->u.synaptics;
    366      1.28  jakllsch 	u_char cmd[1], resp[3];
    367       1.3       scw 	int res, ver_minor, ver_major;
    368       1.2  christos 	struct sysctllog *clog = NULL;
    369       1.1  christos 
    370      1.27  jakllsch 	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
    371       1.1  christos 	    SYNAPTICS_IDENTIFY_TOUCHPAD);
    372       1.1  christos 	cmd[0] = PMS_SEND_DEV_STATUS;
    373       1.3       scw 	res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
    374       1.3       scw 	    resp, 0);
    375       1.1  christos 	if (res) {
    376      1.26    cegger 		aprint_debug_dev(psc->sc_dev,
    377      1.20      cube 		    "synaptics_probe: Identify Touchpad error.\n");
    378       1.3       scw 		/*
    379       1.3       scw 		 * Reset device in case the probe confused it.
    380       1.3       scw 		 */
    381       1.3       scw  doreset:
    382      1.35     ryoon 		(void)synaptics_poll_reset(psc);
    383      1.35     ryoon 		return res;
    384       1.1  christos 	}
    385       1.1  christos 
    386       1.1  christos 	if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
    387      1.26    cegger 		aprint_debug_dev(psc->sc_dev,
    388      1.20      cube 		    "synaptics_probe: Not synaptics.\n");
    389       1.3       scw 		res = 1;
    390       1.3       scw 		goto doreset;
    391       1.1  christos 	}
    392       1.1  christos 
    393       1.3       scw 	sc->flags = 0;
    394       1.3       scw 
    395       1.1  christos 	/* Check for minimum version and print a nice message. */
    396       1.1  christos 	ver_major = resp[2] & 0x0f;
    397       1.1  christos 	ver_minor = resp[0];
    398      1.20      cube 	aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
    399      1.20      cube 	    ver_major, ver_minor);
    400       1.1  christos 	if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
    401       1.1  christos 		/* No capability query support. */
    402       1.3       scw 		sc->caps = 0;
    403       1.1  christos 		goto done;
    404       1.1  christos 	}
    405       1.1  christos 
    406      1.34     blymn 
    407       1.1  christos 	/* Query the hardware capabilities. */
    408      1.42      maya 	res = synaptics_special_read(psc, SYNAPTICS_READ_CAPABILITIES, resp);
    409       1.1  christos 	if (res) {
    410       1.1  christos 		/* Hmm, failed to get capabilites. */
    411      1.20      cube 		aprint_error_dev(psc->sc_dev,
    412      1.20      cube 		    "synaptics_probe: Failed to query capabilities.\n");
    413       1.3       scw 		goto doreset;
    414       1.1  christos 	}
    415       1.1  christos 
    416      1.48     blymn 	sc->caps = SYNAPTICS_CAP_VALUE(resp);
    417       1.3       scw 
    418       1.3       scw 	if (sc->caps & SYNAPTICS_CAP_MBUTTON)
    419       1.3       scw 		sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;
    420       1.3       scw 
    421       1.3       scw 	if (sc->caps & SYNAPTICS_CAP_4BUTTON)
    422       1.3       scw 		sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;
    423       1.3       scw 
    424       1.3       scw 	if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
    425      1.32  christos 		pms_synaptics_probe_extended(psc);
    426       1.3       scw 	}
    427       1.3       scw 
    428       1.3       scw 	if (sc->flags) {
    429       1.3       scw 		const char comma[] = ", ";
    430       1.3       scw 		const char *sep = "";
    431      1.20      cube 		aprint_normal_dev(psc->sc_dev, "");
    432      1.40  christos 		for (size_t f = 0; f < __arraycount(syn_flags); f++) {
    433      1.40  christos 			if (sc->flags & syn_flags[f].bit) {
    434      1.40  christos 				aprint_normal("%s%s", sep, syn_flags[f].desc);
    435      1.40  christos 				sep = comma;
    436      1.40  christos 			}
    437       1.3       scw 		}
    438      1.41  christos 		aprint_normal("\n");
    439       1.1  christos 	}
    440       1.1  christos 
    441       1.1  christos done:
    442       1.2  christos 	pms_sysctl_synaptics(&clog);
    443       1.3       scw 	pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
    444      1.20      cube 	    pms_synaptics_input, psc, device_xname(psc->sc_dev));
    445       1.3       scw 
    446       1.3       scw 	return (0);
    447       1.1  christos }
    448       1.1  christos 
    449       1.1  christos void
    450       1.1  christos pms_synaptics_enable(void *vsc)
    451       1.1  christos {
    452       1.3       scw 	struct pms_softc *psc = vsc;
    453       1.3       scw 	struct synaptics_softc *sc = &psc->u.synaptics;
    454      1.34     blymn 	u_char enable_modes;
    455      1.44     blymn 	int res, i;
    456       1.1  christos 
    457      1.23    plunky 	if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
    458      1.32  christos 		/*
    459      1.40  christos 		 * Extended capability probes can confuse the passthrough
    460      1.40  christos 		 * device; reset the touchpad now to cure that.
    461      1.23    plunky 		 */
    462      1.34     blymn 		res = synaptics_poll_reset(psc);
    463      1.23    plunky 	}
    464      1.23    plunky 
    465       1.3       scw 	/*
    466       1.3       scw 	 * Enable Absolute mode with W (width) reporting, and set
    467      1.34     blymn 	 * the packet rate to maximum (80 packets per second). Enable
    468      1.34     blymn 	 * extended W mode if supported so we can report second finger
    469      1.34     blymn 	 * position.
    470       1.3       scw 	 */
    471      1.34     blymn 	enable_modes =
    472      1.34     blymn 	   SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE;
    473      1.34     blymn 
    474      1.38     ryoon 	if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
    475      1.34     blymn 		enable_modes |= SYNAPTICS_MODE_EXTENDED_W;
    476      1.34     blymn 
    477      1.34     blymn 	/*
    478      1.34     blymn  	* Synaptics documentation says to disable device before
    479      1.34     blymn  	* setting mode.
    480      1.34     blymn  	*/
    481      1.34     blymn 	synaptics_poll_cmd(psc, PMS_DEV_DISABLE, 0);
    482      1.34     blymn 	/* a couple of set scales to clear out pending commands */
    483      1.44     blymn 	for (i = 0; i < 2; i++)
    484      1.34     blymn 		synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
    485      1.34     blymn 
    486      1.42      maya 	res = synaptics_special_write(psc, SYNAPTICS_CMD_SET_MODE2, enable_modes);
    487      1.34     blymn 	if (res)
    488      1.34     blymn 		aprint_error("synaptics: set mode error\n");
    489      1.34     blymn 
    490      1.34     blymn 	/* a couple of set scales to clear out pending commands */
    491      1.44     blymn 	for (i = 0; i < 2; i++)
    492      1.34     blymn 		synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);
    493      1.34     blymn 
    494      1.42      maya 	/* Set advanced gesture mode */
    495      1.49     blymn 	if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) ||
    496      1.49     blymn 	    (sc->flags & SYN_FLAG_HAS_ADV_GESTURE_MODE))
    497      1.42      maya 		synaptics_special_write(psc, SYNAPTICS_WRITE_DELUXE_3, 0x3);
    498      1.39     ryoon 
    499       1.3       scw 	sc->up_down = 0;
    500       1.3       scw 	sc->prev_fingers = 0;
    501       1.3       scw 	sc->gesture_start_x = sc->gesture_start_y = 0;
    502       1.3       scw 	sc->gesture_start_packet = 0;
    503       1.3       scw 	sc->gesture_tap_packet = 0;
    504       1.3       scw 	sc->gesture_type = 0;
    505       1.3       scw 	sc->gesture_buttons = 0;
    506      1.44     blymn 	sc->dz_hold = 0;
    507      1.44     blymn 	for (i = 0; i < SYN_MAX_FINGERS; i++) {
    508      1.44     blymn 		sc->rem_x[i] = sc->rem_y[i] = sc->rem_z[i] = 0;
    509      1.44     blymn 		sc->movement_history[i] = 0;
    510      1.44     blymn 	}
    511      1.34     blymn 	sc->button_history = 0;
    512       1.1  christos }
    513       1.1  christos 
    514       1.1  christos void
    515       1.1  christos pms_synaptics_resume(void *vsc)
    516       1.1  christos {
    517      1.34     blymn 	(void)synaptics_poll_reset(vsc);
    518       1.1  christos }
    519       1.1  christos 
    520       1.3       scw static void
    521       1.3       scw pms_sysctl_synaptics(struct sysctllog **clog)
    522       1.1  christos {
    523       1.1  christos 	int rc, root_num;
    524       1.6    atatat 	const struct sysctlnode *node;
    525       1.1  christos 
    526       1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    527       1.1  christos 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
    528       1.1  christos 	    SYSCTL_DESCR("Synaptics touchpad controls"),
    529       1.1  christos 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
    530       1.1  christos 	    goto err;
    531       1.1  christos 
    532       1.1  christos 	root_num = node->sysctl_num;
    533       1.1  christos 
    534       1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    535       1.1  christos 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    536       1.3       scw 	    CTLTYPE_INT, "up_down_emulation",
    537       1.3       scw 	    SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
    538       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    539       1.3       scw 	    &synaptics_up_down_emul,
    540       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    541       1.3       scw 	    CTL_EOL)) != 0)
    542       1.3       scw 		goto err;
    543       1.3       scw 
    544       1.3       scw 	synaptics_up_down_emul_nodenum = node->sysctl_num;
    545       1.3       scw 
    546       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    547       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    548       1.3       scw 	    CTLTYPE_INT, "up_down_motion_delta",
    549       1.3       scw 	    SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
    550       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    551       1.3       scw 	    &synaptics_up_down_motion_delta,
    552       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    553       1.3       scw 	    CTL_EOL)) != 0)
    554       1.3       scw 		goto err;
    555       1.3       scw 
    556       1.3       scw 	synaptics_up_down_motion_delta_nodenum = node->sysctl_num;
    557       1.3       scw 
    558       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    559       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    560       1.3       scw 	    CTLTYPE_INT, "gesture_move",
    561       1.3       scw 	    SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
    562       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    563       1.3       scw 	    &synaptics_gesture_move,
    564       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    565       1.3       scw 	    CTL_EOL)) != 0)
    566       1.3       scw 		goto err;
    567       1.3       scw 
    568       1.3       scw 	synaptics_gesture_move_nodenum = node->sysctl_num;
    569       1.3       scw 
    570       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    571       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    572       1.3       scw 	    CTLTYPE_INT, "gesture_length",
    573       1.3       scw 	    SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
    574       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    575       1.3       scw 	    &synaptics_gesture_length,
    576       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    577       1.3       scw 	    CTL_EOL)) != 0)
    578       1.3       scw 		goto err;
    579       1.3       scw 
    580       1.3       scw 	synaptics_gesture_length_nodenum = node->sysctl_num;
    581       1.3       scw 
    582       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    583       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    584       1.3       scw 	    CTLTYPE_INT, "edge_left",
    585       1.3       scw 	    SYSCTL_DESCR("Define left edge of touchpad"),
    586       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    587       1.3       scw 	    &synaptics_edge_left,
    588       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    589       1.3       scw 	    CTL_EOL)) != 0)
    590       1.3       scw 		goto err;
    591       1.3       scw 
    592       1.3       scw 	synaptics_edge_left_nodenum = node->sysctl_num;
    593       1.3       scw 
    594       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    595       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    596       1.3       scw 	    CTLTYPE_INT, "edge_right",
    597       1.3       scw 	    SYSCTL_DESCR("Define right edge of touchpad"),
    598       1.1  christos 	    pms_sysctl_synaptics_verify, 0,
    599       1.3       scw 	    &synaptics_edge_right,
    600       1.1  christos 	    0, CTL_HW, root_num, CTL_CREATE,
    601       1.1  christos 	    CTL_EOL)) != 0)
    602       1.1  christos 		goto err;
    603       1.1  christos 
    604       1.3       scw 	synaptics_edge_right_nodenum = node->sysctl_num;
    605       1.1  christos 
    606       1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    607       1.1  christos 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    608       1.3       scw 	    CTLTYPE_INT, "edge_top",
    609       1.3       scw 	    SYSCTL_DESCR("Define top edge of touchpad"),
    610       1.1  christos 	    pms_sysctl_synaptics_verify, 0,
    611       1.3       scw 	    &synaptics_edge_top,
    612       1.1  christos 	    0, CTL_HW, root_num, CTL_CREATE,
    613       1.1  christos 	    CTL_EOL)) != 0)
    614       1.3       scw 		goto err;
    615       1.3       scw 
    616       1.3       scw 	synaptics_edge_top_nodenum = node->sysctl_num;
    617       1.3       scw 
    618       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    619       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    620       1.3       scw 	    CTLTYPE_INT, "edge_bottom",
    621       1.3       scw 	    SYSCTL_DESCR("Define bottom edge of touchpad"),
    622       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    623       1.3       scw 	    &synaptics_edge_bottom,
    624       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    625       1.3       scw 	    CTL_EOL)) != 0)
    626       1.3       scw 		goto err;
    627       1.3       scw 
    628       1.3       scw 	synaptics_edge_bottom_nodenum = node->sysctl_num;
    629       1.3       scw 
    630       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    631       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    632       1.3       scw 	    CTLTYPE_INT, "edge_motion_delta",
    633       1.3       scw 	    SYSCTL_DESCR("Define edge motion rate"),
    634       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    635       1.3       scw 	    &synaptics_edge_motion_delta,
    636       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    637       1.3       scw 	    CTL_EOL)) != 0)
    638       1.3       scw 		goto err;
    639       1.3       scw 
    640       1.3       scw 	synaptics_edge_motion_delta_nodenum = node->sysctl_num;
    641       1.3       scw 
    642       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    643       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    644       1.3       scw 	    CTLTYPE_INT, "finger_high",
    645       1.3       scw 	    SYSCTL_DESCR("Define finger applied pressure threshold"),
    646       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    647       1.3       scw 	    &synaptics_finger_high,
    648       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    649       1.3       scw 	    CTL_EOL)) != 0)
    650       1.3       scw 		goto err;
    651       1.3       scw 
    652       1.3       scw 	synaptics_finger_high_nodenum = node->sysctl_num;
    653       1.3       scw 
    654       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    655       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    656       1.3       scw 	    CTLTYPE_INT, "finger_low",
    657       1.3       scw 	    SYSCTL_DESCR("Define finger removed pressure threshold"),
    658       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    659       1.3       scw 	    &synaptics_finger_low,
    660       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    661       1.3       scw 	    CTL_EOL)) != 0)
    662       1.3       scw 		goto err;
    663       1.3       scw 
    664       1.3       scw 	synaptics_finger_low_nodenum = node->sysctl_num;
    665       1.3       scw 
    666       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    667       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    668       1.3       scw 	    CTLTYPE_INT, "two_fingers_emulation",
    669       1.3       scw 	    SYSCTL_DESCR("Map two fingers to middle button"),
    670       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    671       1.3       scw 	    &synaptics_two_fingers_emul,
    672       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    673       1.3       scw 	    CTL_EOL)) != 0)
    674       1.3       scw 		goto err;
    675       1.3       scw 
    676       1.3       scw 	synaptics_two_fingers_emul_nodenum = node->sysctl_num;
    677       1.3       scw 
    678       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    679       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    680       1.3       scw 	    CTLTYPE_INT, "scale_x",
    681       1.3       scw 	    SYSCTL_DESCR("Horizontal movement scale factor"),
    682       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    683      1.30       dsl 	    &synaptics_scale_x,
    684       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    685       1.3       scw 	    CTL_EOL)) != 0)
    686       1.3       scw 		goto err;
    687       1.3       scw 
    688       1.3       scw 	synaptics_scale_x_nodenum = node->sysctl_num;
    689       1.3       scw 
    690       1.3       scw 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    691       1.3       scw 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    692       1.3       scw 	    CTLTYPE_INT, "scale_y",
    693       1.3       scw 	    SYSCTL_DESCR("Vertical movement scale factor"),
    694       1.3       scw 	    pms_sysctl_synaptics_verify, 0,
    695      1.30       dsl 	    &synaptics_scale_y,
    696       1.3       scw 	    0, CTL_HW, root_num, CTL_CREATE,
    697       1.3       scw 	    CTL_EOL)) != 0)
    698       1.3       scw 		goto err;
    699       1.1  christos 
    700       1.3       scw 	synaptics_scale_y_nodenum = node->sysctl_num;
    701       1.1  christos 
    702       1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    703       1.1  christos 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    704      1.44     blymn 	    CTLTYPE_INT, "scale_z",
    705      1.44     blymn 	    SYSCTL_DESCR("Sroll wheel emulation scale factor"),
    706      1.44     blymn 	    pms_sysctl_synaptics_verify, 0,
    707      1.44     blymn 	    &synaptics_scale_z,
    708      1.44     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    709      1.44     blymn 	    CTL_EOL)) != 0)
    710      1.44     blymn 		goto err;
    711      1.44     blymn 
    712      1.44     blymn 	synaptics_scale_z_nodenum = node->sysctl_num;
    713      1.44     blymn 
    714      1.44     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    715      1.44     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    716       1.3       scw 	    CTLTYPE_INT, "max_speed_x",
    717       1.3       scw 	    SYSCTL_DESCR("Horizontal movement maximum speed"),
    718       1.1  christos 	    pms_sysctl_synaptics_verify, 0,
    719       1.3       scw 	    &synaptics_max_speed_x,
    720       1.1  christos 	    0, CTL_HW, root_num, CTL_CREATE,
    721       1.1  christos 	    CTL_EOL)) != 0)
    722       1.1  christos 		goto err;
    723       1.1  christos 
    724       1.3       scw 	synaptics_max_speed_x_nodenum = node->sysctl_num;
    725       1.1  christos 
    726       1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    727       1.1  christos 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    728       1.3       scw 	    CTLTYPE_INT, "max_speed_y",
    729       1.3       scw 	    SYSCTL_DESCR("Vertical movement maximum speed"),
    730       1.1  christos 	    pms_sysctl_synaptics_verify, 0,
    731       1.3       scw 	    &synaptics_max_speed_y,
    732       1.1  christos 	    0, CTL_HW, root_num, CTL_CREATE,
    733       1.1  christos 	    CTL_EOL)) != 0)
    734       1.1  christos 		goto err;
    735       1.1  christos 
    736       1.3       scw 	synaptics_max_speed_y_nodenum = node->sysctl_num;
    737       1.1  christos 
    738       1.1  christos 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    739       1.1  christos 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    740      1.44     blymn 	    CTLTYPE_INT, "max_speed_z",
    741      1.44     blymn 	    SYSCTL_DESCR("Scroll wheel emulation maximum speed"),
    742      1.44     blymn 	    pms_sysctl_synaptics_verify, 0,
    743      1.44     blymn 	    &synaptics_max_speed_z,
    744      1.44     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    745      1.44     blymn 	    CTL_EOL)) != 0)
    746      1.44     blymn 		goto err;
    747      1.44     blymn 
    748      1.44     blymn 	synaptics_max_speed_z_nodenum = node->sysctl_num;
    749      1.44     blymn 
    750      1.44     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    751      1.44     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    752       1.3       scw 	    CTLTYPE_INT, "movement_threshold",
    753       1.3       scw 	    SYSCTL_DESCR("Minimum reported movement threshold"),
    754       1.1  christos 	    pms_sysctl_synaptics_verify, 0,
    755       1.3       scw 	    &synaptics_movement_threshold,
    756       1.1  christos 	    0, CTL_HW, root_num, CTL_CREATE,
    757       1.1  christos 	    CTL_EOL)) != 0)
    758       1.1  christos 		goto err;
    759       1.1  christos 
    760       1.3       scw 	synaptics_movement_threshold_nodenum = node->sysctl_num;
    761      1.34     blymn 
    762      1.34     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    763      1.34     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    764      1.36  jmcneill 	    CTLTYPE_INT, "movement_enable",
    765      1.36  jmcneill 	    SYSCTL_DESCR("Enable movement reporting"),
    766      1.36  jmcneill 	    pms_sysctl_synaptics_verify, 0,
    767      1.36  jmcneill 	    &synaptics_movement_enable,
    768      1.36  jmcneill 	    0, CTL_HW, root_num, CTL_CREATE,
    769      1.36  jmcneill 	    CTL_EOL)) != 0)
    770      1.36  jmcneill 		goto err;
    771      1.36  jmcneill 
    772      1.36  jmcneill 	synaptics_movement_enable_nodenum = node->sysctl_num;
    773      1.36  jmcneill 
    774      1.36  jmcneill 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    775      1.36  jmcneill 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    776      1.34     blymn 	    CTLTYPE_INT, "button_boundary",
    777      1.34     blymn 	    SYSCTL_DESCR("Top edge of button area"),
    778      1.34     blymn 	    pms_sysctl_synaptics_verify, 0,
    779      1.34     blymn 	    &synaptics_button_boundary,
    780      1.34     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    781      1.34     blymn 	    CTL_EOL)) != 0)
    782      1.34     blymn 		goto err;
    783      1.34     blymn 
    784      1.34     blymn 	synaptics_button_boundary_nodenum = node->sysctl_num;
    785      1.34     blymn 
    786      1.34     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    787      1.34     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    788      1.34     blymn 	    CTLTYPE_INT, "button2_edge",
    789      1.34     blymn 	    SYSCTL_DESCR("Left edge of button 2 region"),
    790      1.34     blymn 	    pms_sysctl_synaptics_verify, 0,
    791      1.34     blymn 	    &synaptics_button2,
    792      1.34     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    793      1.34     blymn 	    CTL_EOL)) != 0)
    794      1.34     blymn 		goto err;
    795      1.34     blymn 
    796      1.34     blymn 	synaptics_button2_nodenum = node->sysctl_num;
    797      1.34     blymn 
    798      1.34     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    799      1.34     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    800      1.34     blymn 	    CTLTYPE_INT, "button3_edge",
    801      1.34     blymn 	    SYSCTL_DESCR("Left edge of button 3 region"),
    802      1.34     blymn 	    pms_sysctl_synaptics_verify, 0,
    803      1.34     blymn 	    &synaptics_button3,
    804      1.34     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    805      1.34     blymn 	    CTL_EOL)) != 0)
    806      1.34     blymn 		goto err;
    807      1.34     blymn 
    808      1.34     blymn 	synaptics_button3_nodenum = node->sysctl_num;
    809      1.44     blymn 
    810      1.44     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    811      1.44     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    812      1.44     blymn 	    CTLTYPE_INT, "finger_scroll-min",
    813      1.45     blymn 	    SYSCTL_DESCR("Minimum width at which y cursor movements will be converted to scroll wheel events"),
    814      1.44     blymn 	    pms_sysctl_synaptics_verify, 0,
    815      1.44     blymn 	    &synaptics_fscroll_min,
    816      1.44     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    817      1.44     blymn 	    CTL_EOL)) != 0)
    818      1.44     blymn 		goto err;
    819      1.44     blymn 
    820      1.44     blymn 	synaptics_finger_scroll_min_nodenum = node->sysctl_num;
    821      1.44     blymn 
    822      1.44     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    823      1.44     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    824      1.44     blymn 	    CTLTYPE_INT, "finger_scroll-max",
    825      1.45     blymn 	    SYSCTL_DESCR("Maximum width at which y cursor movements will be converted to scroll wheel events"),
    826      1.44     blymn 	    pms_sysctl_synaptics_verify, 0,
    827      1.44     blymn 	    &synaptics_fscroll_max,
    828      1.44     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    829      1.44     blymn 	    CTL_EOL)) != 0)
    830      1.44     blymn 		goto err;
    831      1.44     blymn 
    832      1.44     blymn 	synaptics_finger_scroll_max_nodenum = node->sysctl_num;
    833      1.44     blymn 
    834      1.44     blymn 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    835      1.44     blymn 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    836      1.44     blymn 	    CTLTYPE_INT, "finger_scroll-hysteresis",
    837      1.45     blymn 	    SYSCTL_DESCR("Number of packets to keep reporting y cursor movements as scroll wheel events"),
    838      1.44     blymn 	    pms_sysctl_synaptics_verify, 0,
    839      1.44     blymn 	    &synaptics_dz_hold,
    840      1.44     blymn 	    0, CTL_HW, root_num, CTL_CREATE,
    841      1.44     blymn 	    CTL_EOL)) != 0)
    842      1.44     blymn 		goto err;
    843      1.44     blymn 
    844      1.44     blymn 	synaptics_dz_hold_nodenum = node->sysctl_num;
    845      1.70       nia 
    846      1.70       nia 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    847      1.70       nia 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    848      1.70       nia 	    CTLTYPE_BOOL, "aux_mid_button_scroll",
    849      1.70       nia 	    SYSCTL_DESCR("Interpet Y-Axis movement with the middle button held as scrolling on the passthrough device (e.g. TrackPoint)"),
    850      1.70       nia 	    pms_sysctl_synaptics_verify, 0,
    851      1.70       nia 	    &synaptics_aux_mid_button_scroll,
    852      1.70       nia 	    0, CTL_HW, root_num, CTL_CREATE,
    853      1.70       nia 	    CTL_EOL)) != 0)
    854      1.70       nia 		goto err;
    855      1.70       nia 
    856      1.70       nia 	synaptics_aux_mid_button_scroll_nodenum = node->sysctl_num;
    857  1.70.8.1      cjep 
    858  1.70.8.1      cjep 	if ((rc = sysctl_createv(clog, 0, NULL, &node,
    859  1.70.8.1      cjep 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
    860  1.70.8.1      cjep 	    CTLTYPE_INT, "debug",
    861  1.70.8.1      cjep 	    SYSCTL_DESCR("Enable debug output"),
    862  1.70.8.1      cjep 	    NULL, 0,
    863  1.70.8.1      cjep 	    &synaptics_debug,
    864  1.70.8.1      cjep 	    0, CTL_HW, root_num, CTL_CREATE,
    865  1.70.8.1      cjep 	    CTL_EOL)) != 0)
    866  1.70.8.1      cjep 		goto err;
    867  1.70.8.1      cjep 
    868       1.1  christos 	return;
    869       1.1  christos 
    870       1.1  christos err:
    871       1.3       scw 	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
    872       1.1  christos }
    873       1.1  christos 
    874       1.1  christos static int
    875       1.1  christos pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
    876       1.1  christos {
    877       1.1  christos 	int error, t;
    878       1.1  christos 	struct sysctlnode node;
    879       1.1  christos 
    880       1.1  christos 	node = *rnode;
    881       1.1  christos 	t = *(int *)rnode->sysctl_data;
    882       1.1  christos 	node.sysctl_data = &t;
    883       1.1  christos 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    884       1.1  christos 	if (error || newp == NULL)
    885       1.1  christos 		return error;
    886       1.1  christos 
    887       1.1  christos 	/* Sanity check the params. */
    888      1.55       nia 	if (node.sysctl_num == synaptics_up_down_emul_nodenum) {
    889      1.55       nia 		if (t < 0 || t > 3)
    890      1.55       nia 			return (EINVAL);
    891      1.55       nia 	} else
    892      1.55       nia 	if (node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
    893       1.3       scw 		if (t < 0 || t > 2)
    894       1.3       scw 			return (EINVAL);
    895       1.3       scw 	} else
    896       1.3       scw 	if (node.sysctl_num == synaptics_gesture_length_nodenum ||
    897       1.3       scw 	    node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
    898      1.58       nia 	    node.sysctl_num == synaptics_up_down_motion_delta_nodenum ||
    899      1.57       nia 	    node.sysctl_num == synaptics_max_speed_x_nodenum ||
    900      1.57       nia 	    node.sysctl_num == synaptics_max_speed_y_nodenum ||
    901      1.57       nia 	    node.sysctl_num == synaptics_max_speed_z_nodenum) {
    902       1.1  christos 		if (t < 0)
    903       1.3       scw 			return (EINVAL);
    904       1.3       scw 	} else
    905       1.3       scw 	if (node.sysctl_num == synaptics_edge_left_nodenum ||
    906       1.3       scw 	    node.sysctl_num == synaptics_edge_bottom_nodenum) {
    907       1.3       scw 		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
    908       1.3       scw 			return (EINVAL);
    909       1.3       scw 	} else
    910       1.3       scw 	if (node.sysctl_num == synaptics_edge_right_nodenum ||
    911       1.3       scw 	    node.sysctl_num == synaptics_edge_top_nodenum) {
    912       1.3       scw 		if (t < (SYNAPTICS_EDGE_MAX / 2))
    913       1.3       scw 			return (EINVAL);
    914       1.1  christos 	} else
    915       1.3       scw 	if (node.sysctl_num == synaptics_scale_x_nodenum ||
    916      1.46     blymn 	    node.sysctl_num == synaptics_scale_y_nodenum ||
    917      1.46     blymn 	    node.sysctl_num == synaptics_scale_z_nodenum) {
    918       1.3       scw 		if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
    919       1.3       scw 			return (EINVAL);
    920       1.3       scw 	} else
    921       1.3       scw 	if (node.sysctl_num == synaptics_finger_high_nodenum) {
    922       1.3       scw 		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
    923       1.3       scw 		    t < synaptics_finger_low)
    924       1.3       scw 			return (EINVAL);
    925       1.3       scw 	} else
    926       1.3       scw 	if (node.sysctl_num == synaptics_finger_low_nodenum) {
    927       1.3       scw 		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
    928       1.3       scw 		    t > synaptics_finger_high)
    929       1.3       scw 			return (EINVAL);
    930       1.3       scw 	} else
    931       1.3       scw 	if (node.sysctl_num == synaptics_gesture_move_nodenum ||
    932       1.3       scw 	    node.sysctl_num == synaptics_movement_threshold_nodenum) {
    933       1.3       scw 		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
    934       1.3       scw 			return (EINVAL);
    935       1.3       scw 	} else
    936      1.36  jmcneill 	if (node.sysctl_num == synaptics_button_boundary_nodenum) {
    937      1.38     ryoon 		if (t < 0 || t < SYNAPTICS_EDGE_BOTTOM ||
    938      1.34     blymn 		    t > SYNAPTICS_EDGE_TOP)
    939      1.34     blymn 			return (EINVAL);
    940      1.34     blymn 	} else
    941      1.36  jmcneill 	if (node.sysctl_num == synaptics_button2_nodenum ||
    942      1.36  jmcneill 	    node.sysctl_num == synaptics_button3_nodenum) {
    943      1.34     blymn 		if (t < SYNAPTICS_EDGE_LEFT || t > SYNAPTICS_EDGE_RIGHT)
    944      1.34     blymn 			return (EINVAL);
    945      1.34     blymn 	} else
    946      1.44     blymn 	if (node.sysctl_num == synaptics_finger_scroll_min_nodenum ||
    947      1.44     blymn 	    node.sysctl_num == synaptics_finger_scroll_max_nodenum) {
    948      1.44     blymn 		/* make sure we avoid the "magic" widths, 4 and below
    949      1.44     blymn 		   are for fingers, 15 is palm detect. */
    950      1.44     blymn 		if ((t < 5) || (t > 14))
    951      1.44     blymn 			return (EINVAL);
    952      1.44     blymn 	} else
    953      1.45     blymn 	if (node.sysctl_num == synaptics_dz_hold_nodenum) {
    954      1.45     blymn 		if (t < 0)
    955      1.45     blymn 			return (EINVAL);
    956      1.45     blymn 	} else
    957      1.36  jmcneill 	if (node.sysctl_num == synaptics_movement_enable_nodenum) {
    958      1.36  jmcneill 		if (t < 0 || t > 1)
    959      1.36  jmcneill 			return (EINVAL);
    960      1.36  jmcneill 	} else
    961      1.70       nia 	if (node.sysctl_num == synaptics_aux_mid_button_scroll_nodenum) {
    962      1.70       nia 		if (t < 0 || t > 1)
    963      1.70       nia 			return (EINVAL);
    964      1.70       nia 	} else
    965       1.3       scw 		return (EINVAL);
    966       1.1  christos 
    967       1.3       scw 	*(int *)rnode->sysctl_data = t;
    968       1.1  christos 
    969       1.3       scw 	return (0);
    970       1.1  christos }
    971       1.1  christos 
    972       1.3       scw /* Masks for the first byte of a packet */
    973       1.3       scw #define PMS_LBUTMASK 0x01
    974       1.3       scw #define PMS_RBUTMASK 0x02
    975       1.3       scw #define PMS_MBUTMASK 0x04
    976       1.1  christos 
    977       1.1  christos static void
    978      1.14   mlelstv pms_synaptics_parse(struct pms_softc *psc)
    979      1.14   mlelstv {
    980      1.14   mlelstv 	struct synaptics_softc *sc = &psc->u.synaptics;
    981      1.14   mlelstv 	struct synaptics_packet sp;
    982      1.34     blymn 	char new_buttons, ew_mode;
    983      1.14   mlelstv 
    984      1.32  christos 	memset(&sp, 0, sizeof(sp));
    985      1.32  christos 
    986      1.14   mlelstv 	/* Width of finger */
    987      1.14   mlelstv 	sp.sp_w = ((psc->packet[0] & 0x30) >> 2) +
    988      1.14   mlelstv 	   ((psc->packet[0] & 0x04) >> 1) +
    989      1.14   mlelstv 	   ((psc->packet[3] & 0x04) >> 2);
    990      1.34     blymn 	sp.sp_finger = 0;
    991      1.52     ryoon 	if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) &&
    992      1.52     ryoon 	    (sp.sp_w == SYNAPTICS_WIDTH_EXTENDED_W)) {
    993      1.34     blymn 		ew_mode = psc->packet[5] >> 4;
    994      1.34     blymn 		switch (ew_mode)
    995      1.34     blymn 		{
    996      1.34     blymn 		case SYNAPTICS_EW_WHEEL:
    997      1.34     blymn 			/* scroll wheel report, ignore for now */
    998      1.34     blymn 			aprint_debug_dev(psc->sc_dev, "mouse wheel packet\n");
    999      1.34     blymn 			return;
   1000      1.14   mlelstv 
   1001      1.34     blymn 		case SYNAPTICS_EW_SECONDARY_FINGER:
   1002      1.34     blymn 			/* parse the second finger report */
   1003      1.51     ryoon 
   1004      1.34     blymn 			sp.sp_finger = 1; /* just one other finger for now */
   1005      1.34     blymn 			sp.sp_x = psc->packet[1]
   1006      1.34     blymn 			    + ((psc->packet[4] & 0x0f) << 8);
   1007      1.34     blymn 			sp.sp_y = psc->packet[2]
   1008      1.34     blymn 			    + ((psc->packet[4] & 0xf0) << 4);
   1009      1.34     blymn 			sp.sp_z = (psc->packet[3] & 0x30)
   1010      1.34     blymn 			    + (psc->packet[5] & 0x0f);
   1011      1.34     blymn 
   1012      1.34     blymn 			/* keep same buttons down as primary */
   1013      1.34     blymn 			sp.sp_left = sc->button_history & PMS_LBUTMASK;
   1014      1.34     blymn 			sp.sp_middle = sc->button_history & PMS_MBUTMASK;
   1015      1.34     blymn 			sp.sp_right = sc->button_history & PMS_RBUTMASK;
   1016      1.34     blymn 			break;
   1017      1.34     blymn 
   1018      1.34     blymn 		case SYNAPTICS_EW_FINGER_STATUS:
   1019      1.34     blymn 			/* reports which finger is primary/secondary
   1020      1.34     blymn 			 * ignore for now.
   1021      1.34     blymn 			 */
   1022      1.34     blymn 			return;
   1023      1.34     blymn 
   1024      1.34     blymn 		default:
   1025      1.34     blymn 			aprint_error_dev(psc->sc_dev,
   1026      1.34     blymn 			    "invalid extended w mode %d\n",
   1027      1.34     blymn 			    ew_mode);
   1028      1.34     blymn 			return;
   1029      1.34     blymn 		}
   1030      1.14   mlelstv 	} else {
   1031      1.34     blymn 
   1032      1.34     blymn 		/* Absolute X/Y coordinates of finger */
   1033      1.34     blymn 		sp.sp_x = psc->packet[4] + ((psc->packet[1] & 0x0f) << 8) +
   1034      1.34     blymn 	   	((psc->packet[3] & 0x10) << 8);
   1035      1.34     blymn 		sp.sp_y = psc->packet[5] + ((psc->packet[1] & 0xf0) << 4) +
   1036      1.34     blymn 	   	((psc->packet[3] & 0x20) << 7);
   1037      1.34     blymn 
   1038      1.34     blymn 		/* Pressure */
   1039      1.34     blymn 		sp.sp_z = psc->packet[2];
   1040      1.34     blymn 
   1041      1.49     blymn 		if ((psc->packet[0] ^ psc->packet[3]) & 0x02) {
   1042      1.49     blymn 			/* extended buttons */
   1043      1.49     blymn 
   1044      1.49     blymn 			aprint_debug_dev(psc->sc_dev,
   1045      1.49     blymn 			    "synaptics_parse: %02x %02x %02x %02x %02x %02x\n",
   1046      1.49     blymn 			    psc->packet[0], psc->packet[1], psc->packet[2],
   1047      1.49     blymn 			    psc->packet[3], psc->packet[4], psc->packet[5]);
   1048      1.49     blymn 
   1049      1.49     blymn 			if ((psc->packet[4] & SYN_1BUTMASK) != 0)
   1050      1.65  jmcneill 				sc->ext_left = PMS_LBUTMASK;
   1051      1.65  jmcneill 			else
   1052      1.65  jmcneill 				sc->ext_left = 0;
   1053      1.49     blymn 
   1054      1.49     blymn 			if ((psc->packet[4] & SYN_3BUTMASK) != 0)
   1055      1.65  jmcneill 				sc->ext_middle = PMS_MBUTMASK;
   1056      1.65  jmcneill 			else
   1057      1.65  jmcneill 				sc->ext_middle = 0;
   1058      1.49     blymn 
   1059      1.49     blymn 			if ((psc->packet[5] & SYN_2BUTMASK) != 0)
   1060      1.65  jmcneill 				sc->ext_right = PMS_RBUTMASK;
   1061      1.65  jmcneill 			else
   1062      1.65  jmcneill 				sc->ext_right = 0;
   1063      1.49     blymn 
   1064      1.49     blymn 			if ((psc->packet[5] & SYN_4BUTMASK) != 0)
   1065      1.65  jmcneill 				sc->ext_up = 1;
   1066      1.65  jmcneill 			else
   1067      1.65  jmcneill 				sc->ext_up = 0;
   1068      1.49     blymn 
   1069      1.49     blymn 			if ((psc->packet[4] & SYN_5BUTMASK) != 0)
   1070      1.65  jmcneill 				sc->ext_down = 1;
   1071      1.65  jmcneill 			else
   1072      1.65  jmcneill 				sc->ext_down = 0;
   1073      1.49     blymn 		} else {
   1074      1.49     blymn 			/* Left/Right button handling. */
   1075      1.49     blymn 			sp.sp_left = psc->packet[0] & PMS_LBUTMASK;
   1076      1.49     blymn 			sp.sp_right = psc->packet[0] & PMS_RBUTMASK;
   1077      1.49     blymn 		}
   1078      1.34     blymn 
   1079      1.34     blymn 		/* Up/Down buttons. */
   1080      1.34     blymn 		if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
   1081      1.34     blymn 			/* Old up/down buttons. */
   1082      1.34     blymn 			sp.sp_up = sp.sp_left ^
   1083      1.34     blymn 		    	    (psc->packet[3] & PMS_LBUTMASK);
   1084      1.34     blymn 			sp.sp_down = sp.sp_right ^
   1085      1.34     blymn 		    	    (psc->packet[3] & PMS_RBUTMASK);
   1086      1.34     blymn 		} else if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
   1087      1.34     blymn 	   	    ((psc->packet[0] & PMS_RBUTMASK) ^
   1088      1.34     blymn 	   	    (psc->packet[3] & PMS_RBUTMASK))) {
   1089      1.34     blymn 			/* New up/down button. */
   1090      1.34     blymn 			sp.sp_up = psc->packet[4] & SYN_1BUTMASK;
   1091      1.34     blymn 			sp.sp_down = psc->packet[5] & SYN_2BUTMASK;
   1092      1.34     blymn 		} else {
   1093      1.34     blymn 			sp.sp_up = 0;
   1094      1.34     blymn 			sp.sp_down = 0;
   1095      1.34     blymn 		}
   1096      1.34     blymn 
   1097      1.34     blymn 		new_buttons = 0;
   1098      1.34     blymn 		if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
   1099      1.34     blymn 			/* This is not correctly specified. Read this button press
   1100      1.38     ryoon 		 	* from L/U bit.  Emulate 3 buttons by checking the
   1101      1.34     blymn 		 	* coordinates of the click and returning the appropriate
   1102      1.34     blymn 		 	* button code.  Outside the button region default to a
   1103      1.34     blymn 		 	* left click.
   1104      1.34     blymn 		 	*/
   1105      1.34     blymn 			u_char bstate = (psc->packet[0] ^ psc->packet[3])
   1106      1.34     blymn 					    & 0x01;
   1107      1.34     blymn 			if (sp.sp_y < synaptics_button_boundary) {
   1108      1.34     blymn 				if (sp.sp_x > synaptics_button3) {
   1109      1.34     blymn 					sp.sp_right =
   1110      1.34     blymn 			   			bstate ? PMS_RBUTMASK : 0;
   1111      1.34     blymn 				} else if (sp.sp_x > synaptics_button2) {
   1112      1.34     blymn 					sp.sp_middle =
   1113      1.34     blymn 				   		bstate ? PMS_MBUTMASK : 0;
   1114      1.34     blymn 				} else {
   1115      1.34     blymn 					sp.sp_left = bstate ? PMS_LBUTMASK : 0;
   1116      1.34     blymn 				}
   1117      1.34     blymn 			} else
   1118      1.34     blymn 				sp.sp_left = bstate ? 1 : 0;
   1119      1.34     blymn 			new_buttons = sp.sp_left | sp.sp_middle | sp.sp_right;
   1120      1.34     blymn 			if (new_buttons != sc->button_history) {
   1121      1.34     blymn 				if (sc->button_history == 0)
   1122      1.34     blymn 					sc->button_history = new_buttons;
   1123      1.34     blymn 				else if (new_buttons == 0) {
   1124      1.34     blymn 					sc->button_history = 0;
   1125      1.34     blymn 				       /* ensure all buttons are cleared just in
   1126      1.34     blymn 				 	* case finger comes off in a different
   1127      1.34     blymn 				 	* region.
   1128      1.34     blymn 				 	*/
   1129      1.34     blymn 					sp.sp_left = 0;
   1130      1.34     blymn 					sp.sp_middle = 0;
   1131      1.34     blymn 					sp.sp_right = 0;
   1132      1.34     blymn 				} else {
   1133      1.34     blymn 					/* make sure we keep the same button even
   1134      1.34     blymn 				 	* if the finger moves to a different
   1135      1.34     blymn 				 	* region.  This precludes chording
   1136      1.34     blymn 				 	* but, oh well.
   1137      1.34     blymn 				 	*/
   1138      1.34     blymn 					sp.sp_left = sc->button_history & PMS_LBUTMASK;
   1139      1.34     blymn 					sp.sp_middle = sc->button_history
   1140      1.34     blymn 				    	& PMS_MBUTMASK;
   1141      1.34     blymn 					sp.sp_right = sc->button_history & PMS_RBUTMASK;
   1142      1.34     blymn 				}
   1143      1.34     blymn 			}
   1144      1.34     blymn 		} else if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
   1145      1.34     blymn 			/* Old style Middle Button. */
   1146      1.34     blymn 			sp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
   1147      1.34     blymn 		    	    (psc->packet[3] & PMS_LBUTMASK);
   1148      1.55       nia 		} else if (synaptics_up_down_emul != 1) {
   1149      1.55       nia 			sp.sp_middle = 0;
   1150      1.55       nia 		}
   1151      1.55       nia 
   1152      1.65  jmcneill 		/* Overlay extended button state */
   1153      1.65  jmcneill 		sp.sp_left |= sc->ext_left;
   1154      1.65  jmcneill 		sp.sp_right |= sc->ext_right;
   1155      1.65  jmcneill 		sp.sp_middle |= sc->ext_middle;
   1156      1.65  jmcneill 		sp.sp_up |= sc->ext_up;
   1157      1.65  jmcneill 		sp.sp_down |= sc->ext_down;
   1158      1.65  jmcneill 
   1159      1.55       nia 		switch (synaptics_up_down_emul) {
   1160      1.55       nia 		case 1:
   1161      1.34     blymn 			/* Do middle button emulation using up/down buttons */
   1162      1.34     blymn 			sp.sp_middle = sp.sp_up | sp.sp_down;
   1163      1.34     blymn 			sp.sp_up = sp.sp_down = 0;
   1164      1.55       nia 			break;
   1165      1.55       nia 		case 3:
   1166      1.55       nia 			/* Do left/right button emulation using up/down buttons */
   1167      1.64       nia 			sp.sp_left = sp.sp_left | sp.sp_up;
   1168      1.64       nia 			sp.sp_right = sp.sp_right | sp.sp_down;
   1169      1.55       nia 			sp.sp_up = sp.sp_down = 0;
   1170      1.55       nia 			break;
   1171      1.55       nia 		default:
   1172      1.55       nia 			/*
   1173      1.55       nia 			 * Don't do any remapping...
   1174      1.55       nia 			 * Z-axis emulation is handled in pms_synaptics_process_packet
   1175      1.55       nia 			 */
   1176      1.55       nia 			break;
   1177      1.55       nia 		}
   1178      1.14   mlelstv 	}
   1179      1.14   mlelstv 
   1180      1.14   mlelstv 	pms_synaptics_process_packet(psc, &sp);
   1181      1.14   mlelstv }
   1182      1.14   mlelstv 
   1183      1.69       nia /*
   1184      1.69       nia  * Passthrough is used for e.g. TrackPoints and additional pointing
   1185      1.69       nia  * devices connected to a Synaptics touchpad.
   1186      1.69       nia  */
   1187      1.14   mlelstv static void
   1188      1.14   mlelstv pms_synaptics_passthrough(struct pms_softc *psc)
   1189      1.14   mlelstv {
   1190      1.14   mlelstv 	int dx, dy, dz;
   1191      1.14   mlelstv 	int buttons, changed;
   1192      1.14   mlelstv 	int s;
   1193      1.14   mlelstv 
   1194      1.14   mlelstv 	buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
   1195      1.14   mlelstv 		((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
   1196      1.14   mlelstv 		((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);
   1197      1.14   mlelstv 
   1198      1.14   mlelstv 	dx = psc->packet[4];
   1199      1.14   mlelstv 	if (dx >= 128)
   1200      1.14   mlelstv 		dx -= 256;
   1201      1.14   mlelstv 	if (dx == -128)
   1202      1.14   mlelstv 		dx = -127;
   1203      1.14   mlelstv 
   1204      1.14   mlelstv 	dy = psc->packet[5];
   1205      1.14   mlelstv 	if (dy >= 128)
   1206      1.14   mlelstv 		dy -= 256;
   1207      1.14   mlelstv 	if (dy == -128)
   1208      1.14   mlelstv 		dy = -127;
   1209      1.14   mlelstv 
   1210      1.14   mlelstv 	dz = 0;
   1211      1.14   mlelstv 
   1212      1.14   mlelstv 	changed = buttons ^ (psc->buttons & 0xe0);
   1213      1.14   mlelstv 	psc->buttons ^= changed;
   1214      1.14   mlelstv 
   1215      1.14   mlelstv 	if (dx || dy || dz || changed) {
   1216      1.70       nia 		/*
   1217      1.70       nia 		 * If the middle button is held, interpret Y-axis
   1218      1.70       nia 		 * movement as scrolling.
   1219      1.70       nia 		 */
   1220      1.70       nia 		if (synaptics_aux_mid_button_scroll &&
   1221      1.70       nia 		    dy && (psc->buttons & 0x2)) {
   1222      1.70       nia 			dz = -dy;
   1223      1.70       nia 			dx = dy = 0;
   1224      1.70       nia 		}
   1225      1.14   mlelstv 		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
   1226      1.14   mlelstv 		s = spltty();
   1227      1.14   mlelstv 		wsmouse_input(psc->sc_wsmousedev,
   1228      1.15   mlelstv 			buttons, dx, dy, dz, 0,
   1229      1.14   mlelstv 			WSMOUSE_INPUT_DELTA);
   1230      1.14   mlelstv 		splx(s);
   1231      1.14   mlelstv 	}
   1232      1.14   mlelstv }
   1233      1.14   mlelstv 
   1234      1.14   mlelstv static void
   1235       1.1  christos pms_synaptics_input(void *vsc, int data)
   1236       1.1  christos {
   1237       1.3       scw 	struct pms_softc *psc = vsc;
   1238       1.3       scw 	struct timeval diff;
   1239       1.1  christos 
   1240       1.3       scw 	if (!psc->sc_enabled) {
   1241      1.28  jakllsch 		/* Interrupts are not expected. Discard the byte. */
   1242       1.1  christos 		return;
   1243       1.1  christos 	}
   1244       1.1  christos 
   1245      1.10    kardel 	getmicrouptime(&psc->current);
   1246       1.1  christos 
   1247      1.67       nia 	if (psc->inputstate > 0) {
   1248       1.3       scw 		timersub(&psc->current, &psc->last, &diff);
   1249       1.1  christos 		if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
   1250      1.20      cube 			aprint_debug_dev(psc->sc_dev,
   1251      1.53     ryoon 			    "pms_synaptics_input: unusual delay (%ld.%06ld s), "
   1252      1.20      cube 			    "scheduling reset\n",
   1253       1.1  christos 			    (long)diff.tv_sec, (long)diff.tv_usec);
   1254      1.53     ryoon 			printf("pms_synaptics_input: unusual delay (%ld.%06ld s), "
   1255      1.34     blymn 			    "scheduling reset\n",
   1256      1.34     blymn 			    (long)diff.tv_sec, (long)diff.tv_usec);
   1257       1.3       scw 			psc->inputstate = 0;
   1258       1.3       scw 			psc->sc_enabled = 0;
   1259       1.3       scw 			wakeup(&psc->sc_enabled);
   1260       1.1  christos 			return;
   1261       1.1  christos 		}
   1262       1.1  christos 	}
   1263       1.3       scw 	psc->last = psc->current;
   1264       1.1  christos 
   1265       1.3       scw 	switch (psc->inputstate) {
   1266      1.50   mlelstv 	case -5:
   1267      1.50   mlelstv 	case -4:
   1268      1.50   mlelstv 	case -3:
   1269      1.50   mlelstv 	case -2:
   1270      1.50   mlelstv 	case -1:
   1271       1.3       scw 	case 0:
   1272       1.1  christos 		if ((data & 0xc8) != 0x80) {
   1273      1.26    cegger 			aprint_debug_dev(psc->sc_dev,
   1274      1.54     ryoon 			    "pms_synaptics_input: 0x%02x out of sync\n", data);
   1275      1.50   mlelstv 			/* use negative counts to limit resync phase */
   1276      1.50   mlelstv 			psc->inputstate--;
   1277       1.1  christos 			return;	/* not in sync yet, discard input */
   1278       1.1  christos 		}
   1279      1.50   mlelstv 		psc->inputstate = 0;
   1280       1.3       scw 		/*FALLTHROUGH*/
   1281       1.3       scw 
   1282      1.50   mlelstv 	case -6:
   1283       1.3       scw 	case 3:
   1284       1.5     perry 		if ((data & 8) == 8) {
   1285      1.26    cegger 			aprint_debug_dev(psc->sc_dev,
   1286      1.54     ryoon 			    "pms_synaptics_input: dropped in relative mode, reset\n");
   1287       1.3       scw 			psc->inputstate = 0;
   1288       1.3       scw 			psc->sc_enabled = 0;
   1289       1.3       scw 			wakeup(&psc->sc_enabled);
   1290       1.1  christos 			return;
   1291       1.1  christos 		}
   1292       1.1  christos 	}
   1293       1.1  christos 
   1294       1.3       scw 	psc->packet[psc->inputstate++] = data & 0xff;
   1295       1.3       scw 	if (psc->inputstate == 6) {
   1296       1.3       scw 		/*
   1297       1.3       scw 		 * We have a complete packet.
   1298       1.3       scw 		 * Extract the pertinent details.
   1299       1.3       scw 		 */
   1300       1.3       scw 		psc->inputstate = 0;
   1301      1.14   mlelstv 		if ((psc->packet[0] & 0xfc) == 0x84 &&
   1302      1.14   mlelstv 		    (psc->packet[3] & 0xcc) == 0xc4) {
   1303      1.28  jakllsch 			/* W = SYNAPTICS_WIDTH_PASSTHROUGH, PS/2 passthrough */
   1304      1.14   mlelstv 			pms_synaptics_passthrough(psc);
   1305       1.3       scw 		} else {
   1306      1.14   mlelstv 			pms_synaptics_parse(psc);
   1307       1.1  christos 		}
   1308       1.1  christos 	}
   1309       1.3       scw }
   1310       1.1  christos 
   1311       1.9     perry static inline int
   1312       1.3       scw synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
   1313       1.3       scw     int *palmp)
   1314       1.3       scw {
   1315       1.3       scw 	int fingers;
   1316       1.3       scw 
   1317       1.3       scw 	/* Assume no palm */
   1318       1.3       scw 	*palmp = 0;
   1319       1.3       scw 
   1320       1.3       scw 	/*
   1321       1.3       scw 	 * Apply some hysteresis when checking for a finger.
   1322       1.3       scw 	 * When the finger is first applied, we ignore it until the
   1323       1.3       scw 	 * pressure exceeds the 'high' threshold. The finger is considered
   1324       1.3       scw 	 * removed only when pressure falls beneath the 'low' threshold.
   1325       1.3       scw 	 */
   1326       1.3       scw 	if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
   1327       1.3       scw 	    (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
   1328       1.3       scw 		fingers = 1;
   1329       1.3       scw 	else
   1330       1.3       scw 		fingers = 0;
   1331       1.3       scw 
   1332       1.3       scw 	/*
   1333       1.3       scw 	 * If the pad can't do palm detection, skip the rest.
   1334       1.3       scw 	 */
   1335       1.3       scw 	if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
   1336       1.3       scw 		return (fingers);
   1337       1.3       scw 
   1338       1.3       scw 	/*
   1339       1.3       scw 	 * Palm detection
   1340       1.3       scw 	 */
   1341       1.3       scw 	if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
   1342       1.3       scw 	    sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
   1343       1.3       scw 		*palmp = 1;
   1344       1.3       scw 
   1345       1.3       scw 	if (sc->prev_fingers == 0 &&
   1346       1.3       scw 	    (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
   1347       1.3       scw 	     sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
   1348       1.3       scw 		/*
   1349       1.3       scw 		 * Contact area or pressure is too great to be a finger.
   1350       1.3       scw 		 * Just ignore it for now.
   1351       1.3       scw 		 */
   1352       1.3       scw 		return (0);
   1353       1.3       scw 	}
   1354       1.3       scw 
   1355      1.63       nia 	/*
   1356      1.63       nia 	 * Detect 2 and 3 fingers if supported, but only if multiple
   1357      1.63       nia 	 * fingers appear within the tap gesture time period.
   1358      1.63       nia 	 */
   1359      1.63       nia 	if (sc->flags & SYN_FLAG_HAS_MULTI_FINGER &&
   1360      1.63       nia 	    SYN_TIME(sc, sc->gesture_start_packet,
   1361      1.63       nia 	    sp->sp_finger) < synaptics_gesture_length) {
   1362       1.3       scw 		switch (sp->sp_w) {
   1363       1.3       scw 		case SYNAPTICS_WIDTH_TWO_FINGERS:
   1364       1.3       scw 			fingers = 2;
   1365       1.3       scw 			break;
   1366       1.3       scw 
   1367       1.3       scw 		case SYNAPTICS_WIDTH_THREE_OR_MORE:
   1368       1.3       scw 			fingers = 3;
   1369       1.3       scw 			break;
   1370       1.3       scw 
   1371       1.3       scw 		default:
   1372       1.3       scw 			/*
   1373       1.3       scw 			 * The width value can report spurious single-finger
   1374       1.3       scw 			 * events after a multi-finger event.
   1375       1.3       scw 			 */
   1376      1.61       nia 			fingers = sc->prev_fingers <= 1 ? 1 : sc->prev_fingers;
   1377       1.3       scw 			break;
   1378       1.1  christos 		}
   1379       1.1  christos 	}
   1380       1.3       scw 
   1381       1.3       scw 	return (fingers);
   1382       1.1  christos }
   1383       1.1  christos 
   1384       1.9     perry static inline void
   1385       1.3       scw synaptics_gesture_detect(struct synaptics_softc *sc,
   1386       1.3       scw     struct synaptics_packet *sp, int fingers)
   1387       1.3       scw {
   1388      1.32  christos 	int gesture_len, gesture_buttons;
   1389       1.3       scw 	int set_buttons;
   1390       1.3       scw 
   1391      1.34     blymn 	gesture_len = SYN_TIME(sc, sc->gesture_start_packet, sp->sp_finger);
   1392       1.3       scw 	gesture_buttons = sc->gesture_buttons;
   1393       1.1  christos 
   1394      1.32  christos 	if (fingers > 0 && (fingers == sc->prev_fingers)) {
   1395      1.32  christos 		/* Finger is still present */
   1396      1.32  christos 		sc->gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
   1397      1.32  christos 		sc->gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
   1398      1.32  christos 	} else
   1399       1.3       scw 	if (fingers && sc->prev_fingers == 0) {
   1400       1.3       scw 		/*
   1401       1.3       scw 		 * Finger was just applied.
   1402       1.3       scw 		 * If the previous gesture was a single-click, set things
   1403       1.3       scw 		 * up to deal with a possible drag or double-click gesture.
   1404       1.3       scw 		 * Basically, if the finger is removed again within
   1405       1.3       scw 		 * 'synaptics_gesture_length' packets, this is treated
   1406       1.3       scw 		 * as a double-click. Otherwise we will emulate holding
   1407       1.3       scw 		 * the left button down whilst dragging the mouse.
   1408       1.3       scw 		 */
   1409       1.3       scw 		if (SYN_IS_SINGLE_TAP(sc->gesture_type))
   1410       1.3       scw 			sc->gesture_type |= SYN_GESTURE_DRAG;
   1411       1.3       scw 
   1412      1.32  christos 		sc->gesture_start_x = abs(sp->sp_x);
   1413      1.32  christos 		sc->gesture_start_y = abs(sp->sp_y);
   1414      1.32  christos 		sc->gesture_move_x = 0;
   1415      1.32  christos 		sc->gesture_move_y = 0;
   1416      1.34     blymn 		sc->gesture_start_packet = sc->total_packets[0];
   1417      1.32  christos 
   1418  1.70.8.1      cjep 		DPRINTF(sc, "Finger applied:"
   1419  1.70.8.1      cjep 		    " gesture_start_x: %d"
   1420  1.70.8.1      cjep 		    " gesture_start_y: %d\n",
   1421  1.70.8.1      cjep 		    sc->gesture_start_x, sc->gesture_start_y);
   1422       1.3       scw 	} else
   1423       1.3       scw 	if (fingers == 0 && sc->prev_fingers != 0) {
   1424       1.3       scw 		/*
   1425       1.3       scw 		 * Finger was just removed.
   1426       1.3       scw 		 * Check if the contact time and finger movement were
   1427       1.3       scw 		 * small enough to qualify as a gesture.
   1428       1.3       scw 		 * Ignore finger movement if multiple fingers were
   1429       1.3       scw 		 * detected (the pad may report coordinates for any
   1430       1.3       scw 		 * of the fingers).
   1431       1.3       scw 		 */
   1432      1.32  christos 
   1433  1.70.8.1      cjep 		DPRINTF(sc, "Finger removed: gesture_len: %d (%d)\n",
   1434  1.70.8.1      cjep 		    gesture_len, synaptics_gesture_length);
   1435  1.70.8.1      cjep 		DPRINTF(sc, "gesture_move_x: %d (%d) sp_x: %d\n",
   1436  1.70.8.1      cjep 		    sc->gesture_move_x, synaptics_gesture_move, abs(sp->sp_x));
   1437  1.70.8.1      cjep 		DPRINTF(sc, "gesture_move_y: %d (%d) sp_y: %d\n",
   1438  1.70.8.1      cjep 		    sc->gesture_move_y, synaptics_gesture_move, abs(sp->sp_y));
   1439       1.3       scw 
   1440       1.3       scw 		if (gesture_len < synaptics_gesture_length &&
   1441      1.32  christos 		    ((sc->gesture_move_x < synaptics_gesture_move &&
   1442      1.32  christos 		     sc->gesture_move_y < synaptics_gesture_move))) {
   1443       1.3       scw 			/*
   1444       1.3       scw 			 * Looking good so far.
   1445       1.3       scw 			 */
   1446       1.3       scw 			if (SYN_IS_DRAG(sc->gesture_type)) {
   1447       1.3       scw 				/*
   1448       1.3       scw 				 * Promote this gesture to double-click.
   1449       1.3       scw 				 */
   1450       1.3       scw 				sc->gesture_type |= SYN_GESTURE_DOUBLE;
   1451       1.3       scw 				sc->gesture_type &= ~SYN_GESTURE_SINGLE;
   1452       1.3       scw 			} else {
   1453       1.3       scw 				/*
   1454       1.3       scw 				 * Single tap gesture. Set the tap length timer
   1455       1.3       scw 				 * and flag a single-click.
   1456       1.3       scw 				 */
   1457      1.34     blymn 				sc->gesture_tap_packet = sc->total_packets[0];
   1458       1.3       scw 				sc->gesture_type |= SYN_GESTURE_SINGLE;
   1459       1.3       scw 
   1460       1.3       scw 				/*
   1461       1.3       scw 				 * The gesture can be modified depending on
   1462       1.3       scw 				 * the number of fingers detected.
   1463       1.3       scw 				 *
   1464       1.3       scw 				 * 1: Normal left button emulation.
   1465       1.3       scw 				 * 2: Either middle button or right button
   1466       1.3       scw 				 *    depending on the value of the two_fingers
   1467       1.3       scw 				 *    sysctl variable.
   1468       1.3       scw 				 * 3: Right button.
   1469       1.3       scw 				 */
   1470       1.3       scw 				switch (sc->prev_fingers) {
   1471       1.3       scw 				case 2:
   1472       1.3       scw 					if (synaptics_two_fingers_emul == 1)
   1473       1.3       scw 						gesture_buttons |= PMS_RBUTMASK;
   1474       1.3       scw 					else
   1475       1.3       scw 					if (synaptics_two_fingers_emul == 2)
   1476       1.3       scw 						gesture_buttons |= PMS_MBUTMASK;
   1477       1.3       scw 					break;
   1478       1.3       scw 				case 3:
   1479       1.3       scw 					gesture_buttons |= PMS_RBUTMASK;
   1480       1.3       scw 					break;
   1481       1.3       scw 				default:
   1482       1.3       scw 					gesture_buttons |= PMS_LBUTMASK;
   1483       1.3       scw 					break;
   1484       1.3       scw 				}
   1485       1.1  christos 			}
   1486       1.1  christos 		}
   1487       1.1  christos 
   1488       1.3       scw 		/*
   1489       1.3       scw 		 * Always clear drag state when the finger is removed.
   1490       1.3       scw 		 */
   1491       1.3       scw 		sc->gesture_type &= ~SYN_GESTURE_DRAG;
   1492       1.3       scw 	}
   1493       1.3       scw 
   1494       1.3       scw 	if (sc->gesture_type == 0) {
   1495       1.3       scw 		/*
   1496       1.3       scw 		 * There is no gesture in progress.
   1497       1.3       scw 		 * Clear emulated button state.
   1498       1.3       scw 		 */
   1499       1.3       scw 		sc->gesture_buttons = 0;
   1500       1.3       scw 		return;
   1501       1.3       scw 	}
   1502       1.3       scw 
   1503       1.3       scw 	/*
   1504       1.3       scw 	 * A gesture is in progress.
   1505       1.3       scw 	 */
   1506       1.3       scw 	set_buttons = 0;
   1507       1.3       scw 
   1508       1.3       scw 	if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
   1509       1.3       scw 		/*
   1510       1.3       scw 		 * Single-click.
   1511       1.3       scw 		 * Activate the relevant button(s) until the
   1512       1.3       scw 		 * gesture tap timer has expired.
   1513       1.3       scw 		 */
   1514      1.34     blymn 		if (SYN_TIME(sc, sc->gesture_tap_packet, sp->sp_finger) <
   1515       1.3       scw 		    synaptics_gesture_length)
   1516       1.3       scw 			set_buttons = 1;
   1517       1.3       scw 		else
   1518       1.3       scw 			sc->gesture_type &= ~SYN_GESTURE_SINGLE;
   1519       1.3       scw 	} else
   1520       1.3       scw 	if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
   1521       1.3       scw 		/*
   1522       1.3       scw 		 * Double-click.
   1523       1.3       scw 		 * Activate the relevant button(s) once.
   1524       1.3       scw 		 */
   1525       1.3       scw 		set_buttons = 1;
   1526       1.3       scw 		sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
   1527       1.3       scw 	}
   1528       1.3       scw 
   1529       1.3       scw 	if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
   1530       1.3       scw 		/*
   1531       1.3       scw 		 * Single-click and drag.
   1532       1.3       scw 		 * Maintain button state until the finger is removed.
   1533       1.3       scw 		 */
   1534       1.3       scw 		sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
   1535       1.3       scw 		sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
   1536       1.3       scw 		sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
   1537       1.3       scw 	}
   1538       1.3       scw 
   1539       1.3       scw 	sc->gesture_buttons = gesture_buttons;
   1540       1.3       scw }
   1541       1.3       scw 
   1542       1.9     perry static inline int
   1543      1.34     blymn synaptics_filter_policy(struct synaptics_softc *sc, int finger, int *history,
   1544      1.34     blymn 			int value)
   1545       1.3       scw {
   1546       1.3       scw 	int a, b, rv, count;
   1547       1.1  christos 
   1548      1.34     blymn 	count = sc->total_packets[finger];
   1549       1.3       scw 
   1550       1.3       scw 	/*
   1551       1.3       scw 	 * Once we've accumulated at least SYN_HIST_SIZE values, combine
   1552       1.3       scw 	 * each new value with the previous two and return the average.
   1553       1.3       scw 	 *
   1554       1.3       scw 	 * This is necessary when the touchpad is operating in 80 packets
   1555       1.3       scw 	 * per second mode, as it performs little internal filtering on
   1556       1.3       scw 	 * reported values.
   1557       1.3       scw 	 *
   1558       1.3       scw 	 * Using a rolling average helps to filter out jitter caused by
   1559       1.3       scw 	 * tiny finger movements.
   1560       1.3       scw 	 */
   1561      1.34     blymn 	if (sc->movement_history[finger] >= SYN_HIST_SIZE) {
   1562       1.3       scw 		a = (history[(count + 0) % SYN_HIST_SIZE] +
   1563       1.3       scw 		    history[(count + 1) % SYN_HIST_SIZE]) / 2;
   1564       1.3       scw 
   1565       1.3       scw 		b = (value + history[(count + 0) % SYN_HIST_SIZE]) / 2;
   1566       1.3       scw 
   1567       1.3       scw 		rv = b - a;
   1568       1.3       scw 
   1569       1.3       scw 		/*
   1570       1.3       scw 		 * Don't report the movement if it's below a certain
   1571       1.3       scw 		 * threshold.
   1572       1.3       scw 		 */
   1573       1.3       scw 		if (abs(rv) < synaptics_movement_threshold)
   1574       1.3       scw 			rv = 0;
   1575       1.3       scw 	} else
   1576       1.3       scw 		rv = 0;
   1577       1.3       scw 
   1578       1.3       scw 	/*
   1579       1.3       scw 	 * Add the new value to the history buffer.
   1580       1.3       scw 	 */
   1581       1.3       scw 	history[(count + 1) % SYN_HIST_SIZE] = value;
   1582       1.3       scw 
   1583       1.3       scw 	return (rv);
   1584       1.3       scw }
   1585       1.3       scw 
   1586       1.3       scw /* Edge detection */
   1587       1.3       scw #define	SYN_EDGE_TOP		1
   1588       1.3       scw #define	SYN_EDGE_BOTTOM		2
   1589       1.3       scw #define	SYN_EDGE_LEFT		4
   1590       1.3       scw #define	SYN_EDGE_RIGHT		8
   1591       1.3       scw 
   1592       1.9     perry static inline int
   1593       1.3       scw synaptics_check_edge(int x, int y)
   1594       1.3       scw {
   1595       1.3       scw 	int rv = 0;
   1596       1.3       scw 
   1597       1.3       scw 	if (x < synaptics_edge_left)
   1598       1.3       scw 		rv |= SYN_EDGE_LEFT;
   1599       1.3       scw 	else
   1600       1.3       scw 	if (x > synaptics_edge_right)
   1601       1.3       scw 		rv |= SYN_EDGE_RIGHT;
   1602       1.3       scw 
   1603       1.3       scw 	if (y < synaptics_edge_bottom)
   1604       1.3       scw 		rv |= SYN_EDGE_BOTTOM;
   1605       1.3       scw 	else
   1606       1.3       scw 	if (y > synaptics_edge_top)
   1607       1.3       scw 		rv |= SYN_EDGE_TOP;
   1608       1.3       scw 
   1609       1.3       scw 	return (rv);
   1610       1.3       scw }
   1611       1.3       scw 
   1612       1.9     perry static inline int
   1613      1.13  christos synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
   1614       1.3       scw {
   1615       1.3       scw 
   1616       1.3       scw 	/*
   1617       1.3       scw 	 * When edge motion is enabled, synaptics_edge_motion_delta is
   1618       1.3       scw 	 * combined with the current delta, together with the direction
   1619       1.3       scw 	 * in which to simulate the motion. The result is added to
   1620       1.3       scw 	 * the delta derived from finger movement. This provides a smooth
   1621       1.3       scw 	 * transition from finger movement to edge motion.
   1622       1.3       scw 	 */
   1623       1.3       scw 	delta = synaptics_edge_motion_delta + (dir * delta);
   1624       1.3       scw 	if (delta < 0)
   1625       1.3       scw 		return (0);
   1626       1.3       scw 	if (delta > synaptics_edge_motion_delta)
   1627       1.3       scw 		return (synaptics_edge_motion_delta);
   1628       1.3       scw 	return (delta);
   1629       1.3       scw }
   1630       1.3       scw 
   1631       1.9     perry static inline int
   1632       1.3       scw synaptics_scale(int delta, int scale, int *remp)
   1633       1.3       scw {
   1634       1.3       scw 	int rv;
   1635       1.3       scw 
   1636       1.3       scw 	/*
   1637       1.3       scw 	 * Scale the raw delta in Synaptics coordinates (0-6143) into
   1638       1.3       scw 	 * something more reasonable by dividing the raw delta by a
   1639       1.3       scw 	 * scale factor. Any remainder from the previous scale result
   1640       1.3       scw 	 * is added to the current delta before scaling.
   1641       1.3       scw 	 * This prevents loss of resolution for very small/slow
   1642       1.3       scw 	 * movements of the finger.
   1643       1.3       scw 	 */
   1644       1.3       scw 	delta += *remp;
   1645       1.3       scw 	rv = delta / scale;
   1646       1.3       scw 	*remp = delta % scale;
   1647       1.3       scw 
   1648       1.3       scw 	return (rv);
   1649       1.3       scw }
   1650       1.3       scw 
   1651       1.9     perry static inline void
   1652       1.3       scw synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
   1653      1.44     blymn     int finger, int scroll_emul, int *dxp, int *dyp, int *dzp)
   1654       1.3       scw {
   1655      1.44     blymn 	int dx, dy, dz, edge;
   1656      1.44     blymn 
   1657      1.44     blymn 	dx = dy = dz = 0;
   1658       1.3       scw 
   1659       1.3       scw 	/*
   1660      1.44     blymn 	 * Compute the next values of dx and dy and dz.  If scroll_emul
   1661      1.44     blymn 	 * is non-zero, take the dy and used it as use it as dz so we
   1662      1.44     blymn 	 * can emulate a scroll wheel.
   1663       1.3       scw 	 */
   1664      1.44     blymn 	if (scroll_emul == 0) {
   1665      1.44     blymn 		dx = synaptics_filter_policy(sc, finger, sc->history_x[finger],
   1666      1.44     blymn 			sp->sp_x);
   1667      1.44     blymn 		dy = synaptics_filter_policy(sc, finger, sc->history_y[finger],
   1668      1.44     blymn 			sp->sp_y);
   1669      1.44     blymn 	} else {
   1670      1.44     blymn 		dz = synaptics_filter_policy(sc, finger, sc->history_z[finger],
   1671      1.44     blymn 			sp->sp_y);
   1672      1.44     blymn 		dx = dy = 0;
   1673      1.44     blymn 	}
   1674       1.3       scw 
   1675       1.3       scw 	/*
   1676       1.3       scw 	 * If we're dealing with a drag gesture, and the finger moves to
   1677       1.3       scw 	 * the edge of the touchpad, apply edge motion emulation if it
   1678       1.3       scw 	 * is enabled.
   1679       1.3       scw 	 */
   1680       1.3       scw 	if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
   1681       1.3       scw 		edge = synaptics_check_edge(sp->sp_x, sp->sp_y);
   1682       1.3       scw 
   1683       1.3       scw 		if (edge & SYN_EDGE_LEFT)
   1684       1.3       scw 			dx -= synaptics_edge_motion(sc, dx, 1);
   1685       1.3       scw 		if (edge & SYN_EDGE_RIGHT)
   1686       1.3       scw 			dx += synaptics_edge_motion(sc, dx, -1);
   1687       1.3       scw 		if (edge & SYN_EDGE_BOTTOM)
   1688       1.3       scw 			dy -= synaptics_edge_motion(sc, dy, 1);
   1689       1.3       scw 		if (edge & SYN_EDGE_TOP)
   1690       1.3       scw 			dy += synaptics_edge_motion(sc, dy, -1);
   1691       1.3       scw 	}
   1692       1.3       scw 
   1693       1.3       scw 	/*
   1694      1.44     blymn 	 * Apply scaling to the deltas
   1695       1.3       scw 	 */
   1696      1.34     blymn 	dx = synaptics_scale(dx, synaptics_scale_x, &sc->rem_x[finger]);
   1697      1.34     blymn 	dy = synaptics_scale(dy, synaptics_scale_y, &sc->rem_y[finger]);
   1698      1.44     blymn 	dz = synaptics_scale(dz, synaptics_scale_z, &sc->rem_z[finger]);
   1699       1.1  christos 
   1700       1.3       scw 	/*
   1701       1.3       scw 	 * Clamp deltas to specified maximums.
   1702       1.3       scw 	 */
   1703      1.43     blymn 	if (abs(dx) > synaptics_max_speed_x)
   1704      1.43     blymn 		dx = ((dx >= 0)? 1 : -1) * synaptics_max_speed_x;
   1705      1.43     blymn 	if (abs(dy) > synaptics_max_speed_y)
   1706      1.43     blymn 		dy = ((dy >= 0)? 1 : -1) * synaptics_max_speed_y;
   1707      1.44     blymn 	if (abs(dz) > synaptics_max_speed_z)
   1708      1.44     blymn 		dz = ((dz >= 0)? 1 : -1) * synaptics_max_speed_z;
   1709       1.1  christos 
   1710       1.3       scw 	*dxp = dx;
   1711       1.3       scw 	*dyp = dy;
   1712      1.44     blymn 	*dzp = dz;
   1713       1.1  christos 
   1714      1.34     blymn 	sc->movement_history[finger]++;
   1715       1.3       scw }
   1716       1.1  christos 
   1717       1.3       scw static void
   1718       1.3       scw pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
   1719       1.3       scw {
   1720       1.3       scw 	struct synaptics_softc *sc = &psc->u.synaptics;
   1721       1.3       scw 	int dx, dy, dz;
   1722       1.3       scw 	int fingers, palm, buttons, changed;
   1723      1.44     blymn 	int s, z_emul;
   1724       1.1  christos 
   1725       1.3       scw 	/*
   1726       1.3       scw 	 * Do Z-axis emulation using up/down buttons if required.
   1727       1.3       scw 	 * Note that the pad will send a one second burst of packets
   1728       1.3       scw 	 * when an up/down button is pressed and held. At the moment
   1729       1.3       scw 	 * we don't deal with auto-repeat, so convert the burst into
   1730       1.3       scw 	 * a one-shot.
   1731       1.3       scw 	 */
   1732       1.3       scw 	dz = 0;
   1733       1.3       scw 	if (synaptics_up_down_emul == 2) {
   1734       1.3       scw 		if (sc->up_down == 0) {
   1735       1.3       scw 			if (sp->sp_up && sp->sp_down) {
   1736       1.3       scw 				sp->sp_middle = 1;
   1737      1.68       nia 			} else if (sp->sp_up) {
   1738       1.3       scw 				dz = -synaptics_up_down_motion_delta;
   1739      1.68       nia 			} else if (sp->sp_down) {
   1740       1.3       scw 				dz = synaptics_up_down_motion_delta;
   1741      1.68       nia 			}
   1742       1.1  christos 		}
   1743       1.1  christos 
   1744       1.3       scw 		sc->up_down = sp->sp_up | sp->sp_down;
   1745       1.3       scw 		sp->sp_up = sp->sp_down = 0;
   1746       1.3       scw 	}
   1747       1.3       scw 
   1748       1.3       scw 	/*
   1749       1.3       scw 	 * Determine whether or not a finger is on the pad.
   1750       1.3       scw 	 * On some pads, this will return the number of fingers
   1751       1.3       scw 	 * detected.
   1752       1.3       scw 	 */
   1753       1.3       scw 	fingers = synaptics_finger_detect(sc, sp, &palm);
   1754       1.3       scw 
   1755       1.3       scw 	/*
   1756      1.34     blymn 	 * Do gesture processing only if we didn't detect a palm and
   1757      1.34     blymn 	 * it is not the seondary finger.
   1758       1.3       scw 	 */
   1759      1.34     blymn 	if ((sp->sp_finger == 0) && (palm == 0))
   1760       1.3       scw 		synaptics_gesture_detect(sc, sp, fingers);
   1761       1.3       scw 	else
   1762       1.3       scw 		sc->gesture_type = sc->gesture_buttons = 0;
   1763       1.1  christos 
   1764       1.3       scw 	/*
   1765       1.3       scw 	 * Determine what buttons to report
   1766       1.3       scw 	 */
   1767       1.3       scw 	buttons = (sp->sp_left ? 0x1 : 0) |
   1768       1.3       scw 	    (sp->sp_middle ? 0x2 : 0) |
   1769       1.3       scw 	    (sp->sp_right ? 0x4 : 0) |
   1770       1.3       scw 	    (sp->sp_up ? 0x8 : 0) |
   1771       1.3       scw 	    (sp->sp_down ? 0x10 : 0);
   1772      1.14   mlelstv 	changed = buttons ^ (psc->buttons & 0x1f);
   1773      1.14   mlelstv 	psc->buttons ^= changed;
   1774       1.1  christos 
   1775       1.3       scw 	sc->prev_fingers = fingers;
   1776      1.34     blymn 	sc->total_packets[sp->sp_finger]++;
   1777       1.1  christos 
   1778       1.3       scw 	/*
   1779      1.34     blymn 	 * Do movement processing IFF we have a single finger and no palm or
   1780      1.34     blymn 	 * a secondary finger and no palm.
   1781       1.3       scw 	 */
   1782      1.36  jmcneill 	if (palm == 0 && synaptics_movement_enable) {
   1783      1.34     blymn 		if (fingers == 1) {
   1784      1.44     blymn 			z_emul = 0;
   1785      1.44     blymn 
   1786      1.44     blymn 			if ((sp->sp_w >= synaptics_fscroll_min) &&
   1787      1.44     blymn 			    (sp->sp_w <= synaptics_fscroll_max)) {
   1788      1.44     blymn 				z_emul = 1;
   1789      1.44     blymn 				sc->dz_hold = synaptics_dz_hold;
   1790      1.44     blymn 			}
   1791      1.44     blymn 
   1792      1.44     blymn 			if (sc->dz_hold > 0) {
   1793      1.44     blymn 				z_emul = 1;
   1794      1.44     blymn 			}
   1795      1.44     blymn 
   1796      1.44     blymn 			synaptics_movement(sc, sp, sp->sp_finger,
   1797      1.44     blymn 				z_emul, &dx, &dy, &dz);
   1798      1.34     blymn 		} else {
   1799      1.34     blymn 			/*
   1800      1.34     blymn 			 * No valid finger. Therefore no movement.
   1801      1.34     blymn 			 */
   1802      1.34     blymn 			sc->movement_history[sp->sp_finger] = 0;
   1803      1.34     blymn 			sc->rem_x[sp->sp_finger] = sc->rem_y[sp->sp_finger] = 0;
   1804      1.34     blymn 			dx = dy = 0;
   1805      1.34     blymn 		}
   1806      1.34     blymn 	} else {
   1807       1.3       scw 		/*
   1808       1.3       scw 		 * No valid finger. Therefore no movement.
   1809       1.3       scw 		 */
   1810      1.34     blymn 		sc->movement_history[0] = 0;
   1811      1.44     blymn 		sc->rem_x[0] = sc->rem_y[0] = sc->rem_z[0] = 0;
   1812      1.44     blymn 		dx = dy = dz = 0;
   1813       1.3       scw 	}
   1814       1.1  christos 
   1815      1.44     blymn 	if (sc->dz_hold > 0)
   1816      1.44     blymn 		sc->dz_hold--;
   1817      1.44     blymn 
   1818       1.3       scw 	/*
   1819       1.3       scw 	 * Pass the final results up to wsmouse_input() if necessary.
   1820       1.3       scw 	 */
   1821       1.3       scw 	if (dx || dy || dz || changed) {
   1822      1.14   mlelstv 		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
   1823       1.3       scw 		s = spltty();
   1824      1.12    plunky 		wsmouse_input(psc->sc_wsmousedev,
   1825      1.12    plunky 				buttons,
   1826      1.12    plunky 				dx, dy, dz, 0,
   1827      1.12    plunky 		    		WSMOUSE_INPUT_DELTA);
   1828       1.3       scw 		splx(s);
   1829       1.1  christos 	}
   1830       1.1  christos }
   1831