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