psm.c revision 360617
141016Sdfr/*- 241016Sdfr * Copyright (c) 1992, 1993 Erik Forsberg. 341016Sdfr * Copyright (c) 1996, 1997 Kazutaka YOKOTA. 441016Sdfr * All rights reserved. 541016Sdfr * 641016Sdfr * Redistribution and use in source and binary forms, with or without 741016Sdfr * modification, are permitted provided that the following conditions 841016Sdfr * are met: 941016Sdfr * 1. Redistributions of source code must retain the above copyright 1041016Sdfr * notice, this list of conditions and the following disclaimer. 1141016Sdfr * 1241016Sdfr * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 1341016Sdfr * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1441016Sdfr * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 1541016Sdfr * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1641016Sdfr * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1741016Sdfr * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1841016Sdfr * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1941016Sdfr * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2041016Sdfr * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 2141016Sdfr * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2241016Sdfr */ 2341016Sdfr/* 2441016Sdfr * Ported to 386bsd Oct 17, 1992 2541016Sdfr * Sandi Donno, Computer Science, University of Cape Town, South Africa 2641016Sdfr * Please send bug reports to sandi@cs.uct.ac.za 2741016Sdfr * 2841016Sdfr * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca - 2941016Sdfr * although I was only partially successful in getting the alpha release 3041016Sdfr * of his "driver for the Logitech and ATI Inport Bus mice for use with 3141016Sdfr * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless 3241016Sdfr * found his code to be an invaluable reference when porting this driver 3341016Sdfr * to 386bsd. 3441016Sdfr * 3541016Sdfr * Further modifications for latest 386BSD+patchkit and port to NetBSD, 3641016Sdfr * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993 3741016Sdfr * 3841016Sdfr * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by 3941016Sdfr * Andrew Herbert - 12 June 1993 4041016Sdfr * 4141016Sdfr * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu> 4241016Sdfr * - 13 June 1993 4341016Sdfr * 4441016Sdfr * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp> 4541016Sdfr * - 24 October 1993 4641016Sdfr * 4741016Sdfr * Hardware access routines and probe logic rewritten by 4841016Sdfr * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp> 4941016Sdfr * - 3, 14, 22 October 1996. 5041016Sdfr * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'... 5141016Sdfr * - 14, 30 November 1996. Uses `kbdio.c'. 5241016Sdfr * - 13 December 1996. Uses queuing version of `kbdio.c'. 53178019Sjkim * - January/February 1997. Tweaked probe logic for 5441016Sdfr * HiNote UltraII/Latitude/Armada laptops. 5541016Sdfr * - 30 July 1997. Added APM support. 56178019Sjkim * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX). 5741016Sdfr * Improved sync check logic. 5841016Sdfr * Vendor specific support routines. 5941016Sdfr */ 6041016Sdfr 61116181Sobrien#include <sys/cdefs.h> 62116181Sobrien__FBSDID("$FreeBSD: stable/11/sys/dev/atkbdc/psm.c 360617 2020-05-03 23:38:21Z wulf $"); 63116181Sobrien 64147271Smarius#include "opt_isa.h" 6541016Sdfr#include "opt_psm.h" 66321060Swulf#include "opt_evdev.h" 6741016Sdfr 6841016Sdfr#include <sys/param.h> 6941016Sdfr#include <sys/systm.h> 7041016Sdfr#include <sys/kernel.h> 7141016Sdfr#include <sys/module.h> 7241016Sdfr#include <sys/bus.h> 7341016Sdfr#include <sys/conf.h> 74189870Srnoland#include <sys/filio.h> 7541016Sdfr#include <sys/poll.h> 76189870Srnoland#include <sys/sigio.h> 77189870Srnoland#include <sys/signalvar.h> 7841016Sdfr#include <sys/syslog.h> 7945720Speter#include <machine/bus.h> 8041181Sdfr#include <sys/rman.h> 8170834Swollman#include <sys/selinfo.h> 82123442Salfred#include <sys/sysctl.h> 8384880Syokota#include <sys/time.h> 8441016Sdfr#include <sys/uio.h> 8541016Sdfr 86114216Skan#include <sys/limits.h> 8766860Sphk#include <sys/mouse.h> 8841181Sdfr#include <machine/resource.h> 8941016Sdfr 90147271Smarius#ifdef DEV_ISA 9141016Sdfr#include <isa/isavar.h> 92147271Smarius#endif 9341016Sdfr 94321060Swulf#ifdef EVDEV_SUPPORT 95321060Swulf#include <dev/evdev/evdev.h> 96321060Swulf#include <dev/evdev/input.h> 97321060Swulf#endif 98321060Swulf 99147271Smarius#include <dev/atkbdc/atkbdcreg.h> 100147271Smarius#include <dev/atkbdc/psm.h> 101147271Smarius 10241016Sdfr/* 10341016Sdfr * Driver specific options: the following options may be set by 10441016Sdfr * `options' statements in the kernel configuration file. 10541016Sdfr */ 10641016Sdfr 10741016Sdfr/* debugging */ 10841016Sdfr#ifndef PSM_DEBUG 109178019Sjkim#define PSM_DEBUG 0 /* 110134405Sgibbs * logging: 0: none, 1: brief, 2: verbose 111134405Sgibbs * 3: sync errors, 4: all packets 112134405Sgibbs */ 11341016Sdfr#endif 114178019Sjkim#define VLOG(level, args) do { \ 115178019Sjkim if (verbose >= level) \ 116178019Sjkim log args; \ 117134405Sgibbs} while (0) 11841016Sdfr 11984880Syokota#ifndef PSM_INPUT_TIMEOUT 120178019Sjkim#define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */ 12184880Syokota#endif 12284880Syokota 123133297Sphilip#ifndef PSM_TAP_TIMEOUT 124178019Sjkim#define PSM_TAP_TIMEOUT 125000 125133297Sphilip#endif 126133297Sphilip 127133297Sphilip#ifndef PSM_TAP_THRESHOLD 128178019Sjkim#define PSM_TAP_THRESHOLD 25 129133297Sphilip#endif 130133297Sphilip 13141016Sdfr/* end of driver specific options */ 13241016Sdfr 133178019Sjkim#define PSMCPNP_DRIVER_NAME "psmcpnp" 13483492Syokota 135331170Seadlerstruct psmcpnp_softc { 136331170Seadler enum { 137331170Seadler PSMCPNP_GENERIC, 138331170Seadler PSMCPNP_FORCEPAD, 139344165Swulf PSMCPNP_TOPBUTTONPAD, 140331170Seadler } type; /* Based on PnP ID */ 141331170Seadler}; 142331170Seadler 14341016Sdfr/* input queue */ 144178019Sjkim#define PSM_BUFSIZE 960 145178019Sjkim#define PSM_SMALLBUFSIZE 240 14641016Sdfr 14741016Sdfr/* operation levels */ 148178019Sjkim#define PSM_LEVEL_BASE 0 149178019Sjkim#define PSM_LEVEL_STANDARD 1 150178019Sjkim#define PSM_LEVEL_NATIVE 2 151178019Sjkim#define PSM_LEVEL_MIN PSM_LEVEL_BASE 152178019Sjkim#define PSM_LEVEL_MAX PSM_LEVEL_NATIVE 15341016Sdfr 154343158Swulf/* Active PS/2 multiplexing */ 155343158Swulf#define PSM_NOMUX (-1) 156343158Swulf 15748778Syokota/* Logitech PS2++ protocol */ 158178019Sjkim#define MOUSE_PS2PLUS_CHECKBITS(b) \ 159178019Sjkim ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 160178019Sjkim#define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 161178019Sjkim (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 16248778Syokota 16341016Sdfr/* ring buffer */ 16441016Sdfrtypedef struct ringbuf { 165178019Sjkim int count; /* # of valid elements in the buffer */ 166178019Sjkim int head; /* head pointer */ 167178019Sjkim int tail; /* tail poiner */ 168178019Sjkim u_char buf[PSM_BUFSIZE]; 16941016Sdfr} ringbuf_t; 17041016Sdfr 171123442Salfred/* data buffer */ 172123442Salfredtypedef struct packetbuf { 173178019Sjkim u_char ipacket[16]; /* interim input buffer */ 174178019Sjkim int inputbytes; /* # of bytes in the input buffer */ 175123442Salfred} packetbuf_t; 176123442Salfred 177123442Salfred#ifndef PSM_PACKETQUEUE 178178019Sjkim#define PSM_PACKETQUEUE 128 179123442Salfred#endif 180123442Salfred 181349541Swulf/* 182349541Swulf * Synaptics command definitions. 183349541Swulf */ 184349541Swulf#define SYNAPTICS_READ_IDENTITY 0x00 185349541Swulf#define SYNAPTICS_READ_MODES 0x01 186349541Swulf#define SYNAPTICS_READ_CAPABILITIES 0x02 187349541Swulf#define SYNAPTICS_READ_MODEL_ID 0x03 188349541Swulf#define SYNAPTICS_READ_SERIAL_PREFIX 0x06 189349541Swulf#define SYNAPTICS_READ_SERIAL_SUFFIX 0x07 190349541Swulf#define SYNAPTICS_READ_RESOLUTIONS 0x08 191349541Swulf#define SYNAPTICS_READ_EXTENDED 0x09 192349541Swulf#define SYNAPTICS_READ_CAPABILITIES_CONT 0x0c 193349541Swulf#define SYNAPTICS_READ_MAX_COORDS 0x0d 194349541Swulf#define SYNAPTICS_READ_DELUXE_LED 0x0e 195349541Swulf#define SYNAPTICS_READ_MIN_COORDS 0x0f 196349541Swulf 197139982Sphiliptypedef struct synapticsinfo { 198183888Sdumbbell struct sysctl_ctx_list sysctl_ctx; 199178019Sjkim struct sysctl_oid *sysctl_tree; 200183888Sdumbbell int directional_scrolls; 201281440Srpaulo int two_finger_scroll; 202183888Sdumbbell int min_pressure; 203183888Sdumbbell int max_pressure; 204183888Sdumbbell int max_width; 205183888Sdumbbell int margin_top; 206183888Sdumbbell int margin_right; 207183888Sdumbbell int margin_bottom; 208183888Sdumbbell int margin_left; 209183888Sdumbbell int na_top; 210183888Sdumbbell int na_right; 211183888Sdumbbell int na_bottom; 212183888Sdumbbell int na_left; 213183888Sdumbbell int window_min; 214183888Sdumbbell int window_max; 215183888Sdumbbell int multiplicator; 216183888Sdumbbell int weight_current; 217183888Sdumbbell int weight_previous; 218183888Sdumbbell int weight_previous_na; 219183888Sdumbbell int weight_len_squared; 220183888Sdumbbell int div_min; 221183888Sdumbbell int div_max; 222183888Sdumbbell int div_max_na; 223183888Sdumbbell int div_len; 224183888Sdumbbell int tap_max_delta; 225183888Sdumbbell int tap_min_queue; 226183888Sdumbbell int taphold_timeout; 227183888Sdumbbell int vscroll_ver_area; 228183888Sdumbbell int vscroll_hor_area; 229183888Sdumbbell int vscroll_min_delta; 230183888Sdumbbell int vscroll_div_min; 231183888Sdumbbell int vscroll_div_max; 232281441Srpaulo int touchpad_off; 233307576Sgonzo int softbuttons_y; 234307576Sgonzo int softbutton2_x; 235307576Sgonzo int softbutton3_x; 236307576Sgonzo int max_x; 237307576Sgonzo int max_y; 238349541Swulf int natural_scroll; 239139982Sphilip} synapticsinfo_t; 240139982Sphilip 241183888Sdumbbelltypedef struct synapticspacket { 242183888Sdumbbell int x; 243183888Sdumbbell int y; 244183888Sdumbbell} synapticspacket_t; 245183888Sdumbbell 246183888Sdumbbell#define SYNAPTICS_PACKETQUEUE 10 247183888Sdumbbell#define SYNAPTICS_QUEUE_CURSOR(x) \ 248183888Sdumbbell (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE 249183888Sdumbbell 250244405Sdumbbell#define SYNAPTICS_VERSION_GE(synhw, major, minor) \ 251244405Sdumbbell ((synhw).infoMajor > (major) || \ 252244405Sdumbbell ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor))) 253244405Sdumbbell 254307576Sgonzotypedef struct smoother { 255183888Sdumbbell synapticspacket_t queue[SYNAPTICS_PACKETQUEUE]; 256183888Sdumbbell int queue_len; 257183888Sdumbbell int queue_cursor; 258183888Sdumbbell int start_x; 259183888Sdumbbell int start_y; 260183888Sdumbbell int avg_dx; 261183888Sdumbbell int avg_dy; 262183888Sdumbbell int squelch_x; 263183888Sdumbbell int squelch_y; 264307576Sgonzo int is_fuzzy; 265307576Sgonzo int active; 266307576Sgonzo} smoother_t; 267307576Sgonzo 268307576Sgonzotypedef struct gesture { 269307576Sgonzo int window_min; 270183888Sdumbbell int fingers_nb; 271183888Sdumbbell int tap_button; 272183888Sdumbbell int in_taphold; 273183888Sdumbbell int in_vscroll; 274307576Sgonzo int zmax; /* maximum pressure value */ 275307576Sgonzo struct timeval taptimeout; /* tap timeout for touchpads */ 276307576Sgonzo} gesture_t; 277183888Sdumbbell 278248478Sjkimenum { 279248478Sjkim TRACKPOINT_SYSCTL_SENSITIVITY, 280248478Sjkim TRACKPOINT_SYSCTL_NEGATIVE_INERTIA, 281248478Sjkim TRACKPOINT_SYSCTL_UPPER_PLATEAU, 282248478Sjkim TRACKPOINT_SYSCTL_BACKUP_RANGE, 283248478Sjkim TRACKPOINT_SYSCTL_DRAG_HYSTERESIS, 284248478Sjkim TRACKPOINT_SYSCTL_MINIMUM_DRAG, 285248478Sjkim TRACKPOINT_SYSCTL_UP_THRESHOLD, 286248478Sjkim TRACKPOINT_SYSCTL_THRESHOLD, 287248478Sjkim TRACKPOINT_SYSCTL_JENKS_CURVATURE, 288248478Sjkim TRACKPOINT_SYSCTL_Z_TIME, 289248478Sjkim TRACKPOINT_SYSCTL_PRESS_TO_SELECT, 290248478Sjkim TRACKPOINT_SYSCTL_SKIP_BACKUPS 291248478Sjkim}; 292248478Sjkim 293248478Sjkimtypedef struct trackpointinfo { 294248478Sjkim struct sysctl_ctx_list sysctl_ctx; 295248478Sjkim struct sysctl_oid *sysctl_tree; 296248478Sjkim int sensitivity; 297248478Sjkim int inertia; 298248478Sjkim int uplateau; 299248478Sjkim int reach; 300248478Sjkim int draghys; 301248478Sjkim int mindrag; 302248478Sjkim int upthresh; 303248478Sjkim int threshold; 304248478Sjkim int jenks; 305248478Sjkim int ztime; 306248478Sjkim int pts; 307248478Sjkim int skipback; 308248478Sjkim} trackpointinfo_t; 309248478Sjkim 310307576Sgonzotypedef struct finger { 311307576Sgonzo int x; 312307576Sgonzo int y; 313307576Sgonzo int p; 314307576Sgonzo int w; 315307576Sgonzo int flags; 316307576Sgonzo} finger_t; 317307576Sgonzo#define PSM_FINGERS 2 /* # of processed fingers */ 318307576Sgonzo#define PSM_FINGER_IS_PEN (1<<0) 319307576Sgonzo#define PSM_FINGER_FUZZY (1<<1) 320307576Sgonzo#define PSM_FINGER_DEFAULT_P tap_threshold 321307576Sgonzo#define PSM_FINGER_DEFAULT_W 1 322307576Sgonzo#define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0) 323307576Sgonzo#define PSM_FINGER_RESET(f) do { \ 324307576Sgonzo (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \ 325307576Sgonzo} while (0) 326307576Sgonzo 327307576Sgonzotypedef struct elantechhw { 328307576Sgonzo int hwversion; 329307576Sgonzo int fwversion; 330307576Sgonzo int sizex; 331307576Sgonzo int sizey; 332307576Sgonzo int dpmmx; 333307576Sgonzo int dpmmy; 334307576Sgonzo int ntracesx; 335307576Sgonzo int ntracesy; 336318500Swulf int dptracex; 337318500Swulf int dptracey; 338307576Sgonzo int issemimt; 339307576Sgonzo int isclickpad; 340307576Sgonzo int hascrc; 341307576Sgonzo int hastrackpoint; 342307576Sgonzo int haspressure; 343307576Sgonzo} elantechhw_t; 344307576Sgonzo 345307576Sgonzo/* minimum versions supported by this driver */ 346307576Sgonzo#define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600) 347307576Sgonzo 348307576Sgonzo#define ELANTECH_MAGIC(magic) \ 349307576Sgonzo ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \ 350307576Sgonzo ((magic)[2] == 0xc8 || (magic)[2] == 0x00)) 351307576Sgonzo 352307576Sgonzo#define ELANTECH_FW_ID 0x00 353307576Sgonzo#define ELANTECH_FW_VERSION 0x01 354307576Sgonzo#define ELANTECH_CAPABILITIES 0x02 355307576Sgonzo#define ELANTECH_SAMPLE 0x03 356307576Sgonzo#define ELANTECH_RESOLUTION 0x04 357307576Sgonzo#define ELANTECH_REG_READ 0x10 358307576Sgonzo#define ELANTECH_REG_WRITE 0x11 359307576Sgonzo#define ELANTECH_REG_RDWR 0x00 360307576Sgonzo#define ELANTECH_CUSTOM_CMD 0xf8 361307576Sgonzo 362321060Swulf#ifdef EVDEV_SUPPORT 363321060Swulf#define ELANTECH_MAX_FINGERS 5 364321060Swulf#else 365307576Sgonzo#define ELANTECH_MAX_FINGERS PSM_FINGERS 366321060Swulf#endif 367307576Sgonzo 368321060Swulf#define ELANTECH_FINGER_MAX_P 255 369321060Swulf#define ELANTECH_FINGER_MAX_W 15 370307576Sgonzo#define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \ 371307576Sgonzo .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \ 372307576Sgonzo .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \ 373307576Sgonzo .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \ 374307576Sgonzo .w = PSM_FINGER_DEFAULT_W, \ 375307576Sgonzo .flags = 0 \ 376307576Sgonzo} 377307576Sgonzo 378307576Sgonzoenum { 379307576Sgonzo ELANTECH_PKT_NOP, 380307576Sgonzo ELANTECH_PKT_TRACKPOINT, 381307576Sgonzo ELANTECH_PKT_V2_COMMON, 382307576Sgonzo ELANTECH_PKT_V2_2FINGER, 383307576Sgonzo ELANTECH_PKT_V3, 384307576Sgonzo ELANTECH_PKT_V4_STATUS, 385307576Sgonzo ELANTECH_PKT_V4_HEAD, 386307576Sgonzo ELANTECH_PKT_V4_MOTION 387307576Sgonzo}; 388307576Sgonzo 389307576Sgonzo#define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06) 390307576Sgonzo#define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \ 391307576Sgonzo (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \ 392307576Sgonzo (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \ 393307576Sgonzo (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \ 394307576Sgonzo (pb)->ipacket[5] == 0xff) 395307576Sgonzo#define ELANTECH_PKT_IS_V2(pb) \ 396307576Sgonzo (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02) 397307576Sgonzo#define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \ 398307576Sgonzo ((pb)->ipacket[3] & 0x09) == 0x08 : \ 399307576Sgonzo ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02) 400307576Sgonzo#define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \ 401307576Sgonzo ((pb)->ipacket[3] & 0x09) == 0x09 : \ 402307576Sgonzo ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c) 403307576Sgonzo#define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \ 404307576Sgonzo ((pb)->ipacket[3] & 0x08) == 0x00 : \ 405307576Sgonzo ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10) 406307576Sgonzo 407307576Sgonzotypedef struct elantechaction { 408307576Sgonzo finger_t fingers[ELANTECH_MAX_FINGERS]; 409307576Sgonzo int mask; 410318500Swulf int mask_v4wait; 411307576Sgonzo} elantechaction_t; 412307576Sgonzo 41341016Sdfr/* driver control block */ 41441016Sdfrstruct psm_softc { /* Driver status information */ 415178019Sjkim int unit; 416178019Sjkim struct selinfo rsel; /* Process selecting for Input */ 417178019Sjkim u_char state; /* Mouse driver state */ 418178019Sjkim int config; /* driver configuration flags */ 419178019Sjkim int flags; /* other flags */ 420178019Sjkim KBDC kbdc; /* handle to access kbd controller */ 421178019Sjkim struct resource *intr; /* IRQ resource */ 422178019Sjkim void *ih; /* interrupt handle */ 423178019Sjkim mousehw_t hw; /* hardware information */ 424178019Sjkim synapticshw_t synhw; /* Synaptics hardware information */ 425178019Sjkim synapticsinfo_t syninfo; /* Synaptics configuration */ 426307576Sgonzo smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */ 427307576Sgonzo gesture_t gesture; /* Gesture context */ 428307576Sgonzo elantechhw_t elanhw; /* Elantech hardware information */ 429307576Sgonzo elantechaction_t elanaction; /* Elantech action context */ 430248478Sjkim int tphw; /* TrackPoint hardware information */ 431248478Sjkim trackpointinfo_t tpinfo; /* TrackPoint configuration */ 432178019Sjkim mousemode_t mode; /* operation mode */ 433178019Sjkim mousemode_t dflt_mode; /* default operation mode */ 434178019Sjkim mousestatus_t status; /* accumulated mouse movement */ 435178019Sjkim ringbuf_t queue; /* mouse status queue */ 436178019Sjkim packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */ 437178019Sjkim int pqueue_start; /* start of data in queue */ 438178019Sjkim int pqueue_end; /* end of data in queue */ 439178019Sjkim int button; /* the latest button state */ 440178019Sjkim int xold; /* previous absolute X position */ 441178019Sjkim int yold; /* previous absolute Y position */ 442178019Sjkim int xaverage; /* average X position */ 443178019Sjkim int yaverage; /* average Y position */ 444178019Sjkim int squelch; /* level to filter movement at low speed */ 445178019Sjkim int syncerrors; /* # of bytes discarded to synchronize */ 446178019Sjkim int pkterrors; /* # of packets failed during quaranteen. */ 447331170Seadler int fpcount; /* forcePad valid packet counter */ 448178019Sjkim struct timeval inputtimeout; 449178019Sjkim struct timeval lastsoftintr; /* time of last soft interrupt */ 450178019Sjkim struct timeval lastinputerr; /* time last sync error happened */ 451307576Sgonzo struct timeval idletimeout; 452307576Sgonzo packetbuf_t idlepacket; /* packet to send after idle timeout */ 453178019Sjkim int watchdog; /* watchdog timer flag */ 454240211Sjhb struct callout callout; /* watchdog timer call out */ 455240211Sjhb struct callout softcallout; /* buffer timer call out */ 456178019Sjkim struct cdev *dev; 457178019Sjkim struct cdev *bdev; 458178019Sjkim int lasterr; 459178019Sjkim int cmdcount; 460189870Srnoland struct sigio *async; /* Processes waiting for SIGIO */ 461281440Srpaulo int extended_buttons; 462343158Swulf int muxport; /* MUX port with attached Synaptics */ 463343158Swulf u_char muxsave[3]; /* 3->6 byte proto conversion buffer */ 464343158Swulf int muxtpbuttons; /* Touchpad button state */ 465343158Swulf int muxmsbuttons; /* Mouse (trackpoint) button state */ 466343158Swulf struct timeval muxmidtimeout; /* middle button supression timeout */ 467321060Swulf#ifdef EVDEV_SUPPORT 468321060Swulf struct evdev_dev *evdev_a; /* Absolute reporting device */ 469321060Swulf struct evdev_dev *evdev_r; /* Relative reporting device */ 470321060Swulf#endif 47141016Sdfr}; 472114293Smarkmstatic devclass_t psm_devclass; 47341016Sdfr 47441016Sdfr/* driver state flags (state) */ 475178019Sjkim#define PSM_VALID 0x80 476178019Sjkim#define PSM_OPEN 1 /* Device is open */ 477178019Sjkim#define PSM_ASLP 2 /* Waiting for mouse data */ 478178019Sjkim#define PSM_SOFTARMED 4 /* Software interrupt armed */ 479178019Sjkim#define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */ 480321060Swulf#define PSM_EV_OPEN_R 0x10 /* Relative evdev device is open */ 481321060Swulf#define PSM_EV_OPEN_A 0x20 /* Absolute evdev device is open */ 48241016Sdfr 48341016Sdfr/* driver configuration flags (config) */ 484178019Sjkim#define PSM_CONFIG_RESOLUTION 0x000f /* resolution */ 485178019Sjkim#define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */ 486178019Sjkim#define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */ 487178019Sjkim#define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */ 488178019Sjkim#define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */ 489178019Sjkim#define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */ 490178019Sjkim#define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */ 491178019Sjkim#define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */ 492178019Sjkim#define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */ 49341016Sdfr 494178019Sjkim#define PSM_CONFIG_FLAGS \ 495178019Sjkim (PSM_CONFIG_RESOLUTION | \ 496178019Sjkim PSM_CONFIG_ACCEL | \ 497178019Sjkim PSM_CONFIG_NOCHECKSYNC | \ 498178019Sjkim PSM_CONFIG_NOIDPROBE | \ 499178019Sjkim PSM_CONFIG_NORESET | \ 500178019Sjkim PSM_CONFIG_FORCETAP | \ 501178019Sjkim PSM_CONFIG_IGNPORTERROR | \ 502178019Sjkim PSM_CONFIG_HOOKRESUME | \ 503178019Sjkim PSM_CONFIG_INITAFTERSUSPEND) 50441016Sdfr 50541016Sdfr/* other flags (flags) */ 506178019Sjkim#define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */ 50741016Sdfr 508278787Sgrembo#define kbdcp(p) ((atkbdc_softc_t *)(p)) 509278787Sgrembo#define ALWAYS_RESTORE_CONTROLLER(kbdc) !(kbdcp(kbdc)->quirks \ 510278787Sgrembo & KBDC_QUIRK_KEEP_ACTIVATED) 511278787Sgrembo 512135945Sphilip/* Tunables */ 513200674Sdumbbellstatic int tap_enabled = -1; 514307576Sgonzostatic int verbose = PSM_DEBUG; 515255210Seadlerstatic int synaptics_support = 0; 516255210Seadlerstatic int trackpoint_support = 0; 517307576Sgonzostatic int elantech_support = 0; 518248478Sjkim 51941016Sdfr/* for backward compatibility */ 520178019Sjkim#define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t) 521178019Sjkim#define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t) 522178019Sjkim#define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t) 52341016Sdfr 52441016Sdfrtypedef struct old_mousehw { 525178019Sjkim int buttons; 526178019Sjkim int iftype; 527178019Sjkim int type; 528178019Sjkim int hwid; 52941016Sdfr} old_mousehw_t; 53041016Sdfr 53141016Sdfrtypedef struct old_mousemode { 532178019Sjkim int protocol; 533178019Sjkim int rate; 534178019Sjkim int resolution; 535178019Sjkim int accelfactor; 53641016Sdfr} old_mousemode_t; 53741016Sdfr 538307576Sgonzo#define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field) 539307576Sgonzoenum { 540307576Sgonzo SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure), 541307576Sgonzo SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure), 542307576Sgonzo SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width), 543307576Sgonzo SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top), 544307576Sgonzo SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right), 545307576Sgonzo SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom), 546307576Sgonzo SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left), 547307576Sgonzo SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top), 548307576Sgonzo SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right), 549307576Sgonzo SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom), 550307576Sgonzo SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left), 551307576Sgonzo SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min), 552307576Sgonzo SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max), 553307576Sgonzo SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator), 554307576Sgonzo SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current), 555307576Sgonzo SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous), 556307576Sgonzo SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na), 557307576Sgonzo SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared), 558307576Sgonzo SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min), 559307576Sgonzo SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max), 560307576Sgonzo SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na), 561307576Sgonzo SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len), 562307576Sgonzo SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta), 563307576Sgonzo SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue), 564307576Sgonzo SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout), 565307576Sgonzo SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area), 566307576Sgonzo SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area), 567307576Sgonzo SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta), 568307576Sgonzo SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min), 569307576Sgonzo SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max), 570307576Sgonzo SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off), 571307576Sgonzo SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y), 572307576Sgonzo SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x), 573307576Sgonzo SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x), 574349541Swulf SYNAPTICS_SYSCTL_NATURAL_SCROLL = SYN_OFFSET(natural_scroll), 575349541Swulf#define SYNAPTICS_SYSCTL_LAST SYNAPTICS_SYSCTL_NATURAL_SCROLL 576307576Sgonzo}; 577307576Sgonzo 57841016Sdfr/* packet formatting function */ 579178019Sjkimtypedef int packetfunc_t(struct psm_softc *, u_char *, int *, int, 580178019Sjkim mousestatus_t *); 58141016Sdfr 58241016Sdfr/* function prototypes */ 583178019Sjkimstatic void psmidentify(driver_t *, device_t); 584178019Sjkimstatic int psmprobe(device_t); 585178019Sjkimstatic int psmattach(device_t); 586178019Sjkimstatic int psmdetach(device_t); 587178019Sjkimstatic int psmresume(device_t); 58841016Sdfr 589321060Swulfstatic d_open_t psm_cdev_open; 590321060Swulfstatic d_close_t psm_cdev_close; 591178019Sjkimstatic d_read_t psmread; 592178019Sjkimstatic d_write_t psmwrite; 593178019Sjkimstatic d_ioctl_t psmioctl; 594178019Sjkimstatic d_poll_t psmpoll; 59541016Sdfr 596321060Swulfstatic int psmopen(struct psm_softc *); 597321060Swulfstatic int psmclose(struct psm_softc *); 598321060Swulf 599321060Swulf#ifdef EVDEV_SUPPORT 600321060Swulfstatic evdev_open_t psm_ev_open_r; 601321060Swulfstatic evdev_close_t psm_ev_close_r; 602321060Swulfstatic evdev_open_t psm_ev_open_a; 603321060Swulfstatic evdev_close_t psm_ev_close_a; 604321060Swulf#endif 605321060Swulf 606178019Sjkimstatic int enable_aux_dev(KBDC); 607178019Sjkimstatic int disable_aux_dev(KBDC); 608178019Sjkimstatic int get_mouse_status(KBDC, int *, int, int); 609178019Sjkimstatic int get_aux_id(KBDC); 610178019Sjkimstatic int set_mouse_sampling_rate(KBDC, int); 611178019Sjkimstatic int set_mouse_scaling(KBDC, int); 612178019Sjkimstatic int set_mouse_resolution(KBDC, int); 613178019Sjkimstatic int set_mouse_mode(KBDC); 614178019Sjkimstatic int get_mouse_buttons(KBDC); 615178019Sjkimstatic int is_a_mouse(int); 616178019Sjkimstatic void recover_from_error(KBDC); 617178019Sjkimstatic int restore_controller(KBDC, int); 618178019Sjkimstatic int doinitialize(struct psm_softc *, mousemode_t *); 619178019Sjkimstatic int doopen(struct psm_softc *, int); 620178019Sjkimstatic int reinitialize(struct psm_softc *, int); 621178019Sjkimstatic char *model_name(int); 622178019Sjkimstatic void psmsoftintr(void *); 623307576Sgonzostatic void psmsoftintridle(void *); 624178019Sjkimstatic void psmintr(void *); 625178019Sjkimstatic void psmtimeout(void *); 626178019Sjkimstatic int timeelapsed(const struct timeval *, int, int, 627178019Sjkim const struct timeval *); 628178019Sjkimstatic void dropqueue(struct psm_softc *); 629178019Sjkimstatic void flushpackets(struct psm_softc *); 630178019Sjkimstatic void proc_mmanplus(struct psm_softc *, packetbuf_t *, 631178019Sjkim mousestatus_t *, int *, int *, int *); 632178019Sjkimstatic int proc_synaptics(struct psm_softc *, packetbuf_t *, 633178019Sjkim mousestatus_t *, int *, int *, int *); 634343158Swulfstatic int proc_synaptics_mux(struct psm_softc *, packetbuf_t *); 635178019Sjkimstatic void proc_versapad(struct psm_softc *, packetbuf_t *, 636178019Sjkim mousestatus_t *, int *, int *, int *); 637307576Sgonzostatic int proc_elantech(struct psm_softc *, packetbuf_t *, 638307576Sgonzo mousestatus_t *, int *, int *, int *); 639307576Sgonzostatic int psmpalmdetect(struct psm_softc *, finger_t *, int); 640307576Sgonzostatic void psmgestures(struct psm_softc *, finger_t *, int, 641307576Sgonzo mousestatus_t *); 642307576Sgonzostatic void psmsmoother(struct psm_softc *, finger_t *, int, 643307576Sgonzo mousestatus_t *, int *, int *); 644178019Sjkimstatic int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, 645178019Sjkim u_char *); 64641016Sdfr 64741016Sdfr/* vendor specific features */ 648284320Sglebiusenum probearg { PROBE, REINIT }; 649284320Sglebiustypedef int probefunc_t(struct psm_softc *, enum probearg); 65041016Sdfr 651178019Sjkimstatic int mouse_id_proc1(KBDC, int, int, int *); 652178019Sjkimstatic int mouse_ext_command(KBDC, int); 65341016Sdfr 654178019Sjkimstatic probefunc_t enable_groller; 655178019Sjkimstatic probefunc_t enable_gmouse; 656178019Sjkimstatic probefunc_t enable_aglide; 657178019Sjkimstatic probefunc_t enable_kmouse; 658178019Sjkimstatic probefunc_t enable_msexplorer; 659178019Sjkimstatic probefunc_t enable_msintelli; 660178019Sjkimstatic probefunc_t enable_4dmouse; 661178019Sjkimstatic probefunc_t enable_4dplus; 662178019Sjkimstatic probefunc_t enable_mmanplus; 663178019Sjkimstatic probefunc_t enable_synaptics; 664343158Swulfstatic probefunc_t enable_synaptics_mux; 665248478Sjkimstatic probefunc_t enable_trackpoint; 666178019Sjkimstatic probefunc_t enable_versapad; 667307576Sgonzostatic probefunc_t enable_elantech; 668178019Sjkim 669281441Srpaulostatic void set_trackpoint_parameters(struct psm_softc *sc); 670281441Srpaulostatic void synaptics_passthrough_on(struct psm_softc *sc); 671281441Srpaulostatic void synaptics_passthrough_off(struct psm_softc *sc); 672282734Srpaulostatic int synaptics_preferred_mode(struct psm_softc *sc); 673282734Srpaulostatic void synaptics_set_mode(struct psm_softc *sc, int mode_byte); 674281441Srpaulo 67541016Sdfrstatic struct { 676178019Sjkim int model; 677178019Sjkim u_char syncmask; 678178019Sjkim int packetsize; 679178019Sjkim probefunc_t *probefunc; 68041016Sdfr} vendortype[] = { 681178019Sjkim /* 682178019Sjkim * WARNING: the order of probe is very important. Don't mess it 683178019Sjkim * unless you know what you are doing. 684178019Sjkim */ 685343158Swulf { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad on Active Mux */ 686343158Swulf 0x00, MOUSE_PS2_PACKETSIZE, enable_synaptics_mux }, 687178019Sjkim { MOUSE_MODEL_NET, /* Genius NetMouse */ 688178019Sjkim 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse }, 689178019Sjkim { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */ 690178019Sjkim 0xc8, 6, enable_groller }, 691178019Sjkim { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */ 692178019Sjkim 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus }, 693178019Sjkim { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */ 694178019Sjkim 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer }, 695178019Sjkim { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */ 696178019Sjkim 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse }, 697178019Sjkim { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */ 698178019Sjkim 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus }, 699179474Sphilip { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */ 700179474Sphilip 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics }, 701307576Sgonzo { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */ 702307576Sgonzo 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech }, 703178019Sjkim { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */ 704178019Sjkim 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli }, 705178019Sjkim { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */ 706178019Sjkim 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide }, 707178019Sjkim { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */ 708178019Sjkim 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse }, 709178019Sjkim { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */ 710178019Sjkim 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad }, 711248478Sjkim { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */ 712248478Sjkim 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint }, 713178019Sjkim { MOUSE_MODEL_GENERIC, 714178019Sjkim 0xc0, MOUSE_PS2_PACKETSIZE, NULL }, 71541016Sdfr}; 716298307Spfg#define GENERIC_MOUSE_ENTRY (nitems(vendortype) - 1) 71741016Sdfr 71841016Sdfr/* device driver declarateion */ 71941016Sdfrstatic device_method_t psm_methods[] = { 72041016Sdfr /* Device interface */ 72183147Syokota DEVMETHOD(device_identify, psmidentify), 72241016Sdfr DEVMETHOD(device_probe, psmprobe), 72341016Sdfr DEVMETHOD(device_attach, psmattach), 72458230Syokota DEVMETHOD(device_detach, psmdetach), 72554629Syokota DEVMETHOD(device_resume, psmresume), 72641016Sdfr 72741016Sdfr { 0, 0 } 72841016Sdfr}; 72941016Sdfr 73041016Sdfrstatic driver_t psm_driver = { 731178019Sjkim PSM_DRIVER_NAME, 732178019Sjkim psm_methods, 733178019Sjkim sizeof(struct psm_softc), 73441016Sdfr}; 73541016Sdfr 73647625Sphkstatic struct cdevsw psm_cdevsw = { 737126080Sphk .d_version = D_VERSION, 738126080Sphk .d_flags = D_NEEDGIANT, 739321060Swulf .d_open = psm_cdev_open, 740321060Swulf .d_close = psm_cdev_close, 741111815Sphk .d_read = psmread, 742178017Sjkim .d_write = psmwrite, 743111815Sphk .d_ioctl = psmioctl, 744111815Sphk .d_poll = psmpoll, 745111815Sphk .d_name = PSM_DRIVER_NAME, 74641016Sdfr}; 74741016Sdfr 748321060Swulf#ifdef EVDEV_SUPPORT 749321060Swulfstatic const struct evdev_methods psm_ev_methods_r = { 750321060Swulf .ev_open = psm_ev_open_r, 751321060Swulf .ev_close = psm_ev_close_r, 752321060Swulf}; 753321060Swulfstatic const struct evdev_methods psm_ev_methods_a = { 754321060Swulf .ev_open = psm_ev_open_a, 755321060Swulf .ev_close = psm_ev_close_a, 756321060Swulf}; 757321060Swulf#endif 758321060Swulf 75941016Sdfr/* device I/O routines */ 76041016Sdfrstatic int 76141016Sdfrenable_aux_dev(KBDC kbdc) 76241016Sdfr{ 763178019Sjkim int res; 76441016Sdfr 765178019Sjkim res = send_aux_command(kbdc, PSMC_ENABLE_DEV); 766178019Sjkim VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res)); 76741016Sdfr 768178019Sjkim return (res == PSM_ACK); 76941016Sdfr} 77041016Sdfr 77141016Sdfrstatic int 77241016Sdfrdisable_aux_dev(KBDC kbdc) 77341016Sdfr{ 774178019Sjkim int res; 77541016Sdfr 776178019Sjkim res = send_aux_command(kbdc, PSMC_DISABLE_DEV); 777178019Sjkim VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res)); 77841016Sdfr 779178019Sjkim return (res == PSM_ACK); 78041016Sdfr} 78141016Sdfr 78241016Sdfrstatic int 78341016Sdfrget_mouse_status(KBDC kbdc, int *status, int flag, int len) 78441016Sdfr{ 785178019Sjkim int cmd; 786178019Sjkim int res; 787178019Sjkim int i; 78841016Sdfr 789178019Sjkim switch (flag) { 790178019Sjkim case 0: 791178019Sjkim default: 792178019Sjkim cmd = PSMC_SEND_DEV_STATUS; 793178019Sjkim break; 794178019Sjkim case 1: 795178019Sjkim cmd = PSMC_SEND_DEV_DATA; 796178019Sjkim break; 797178019Sjkim } 798178019Sjkim empty_aux_buffer(kbdc, 5); 799178019Sjkim res = send_aux_command(kbdc, cmd); 800178019Sjkim VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 801178019Sjkim (flag == 1) ? "DATA" : "STATUS", res)); 802178019Sjkim if (res != PSM_ACK) 803178019Sjkim return (0); 80441016Sdfr 805178019Sjkim for (i = 0; i < len; ++i) { 806178019Sjkim status[i] = read_aux_data(kbdc); 807178019Sjkim if (status[i] < 0) 808178019Sjkim break; 809178019Sjkim } 81041016Sdfr 811178019Sjkim VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n", 812178019Sjkim (flag == 1) ? "data" : "status", status[0], status[1], status[2])); 81341016Sdfr 814178019Sjkim return (i); 81541016Sdfr} 81641016Sdfr 81741016Sdfrstatic int 81841016Sdfrget_aux_id(KBDC kbdc) 81941016Sdfr{ 820178019Sjkim int res; 821178019Sjkim int id; 82241016Sdfr 823178019Sjkim empty_aux_buffer(kbdc, 5); 824178019Sjkim res = send_aux_command(kbdc, PSMC_SEND_DEV_ID); 825178019Sjkim VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res)); 826178019Sjkim if (res != PSM_ACK) 827178019Sjkim return (-1); 82841016Sdfr 829178019Sjkim /* 10ms delay */ 830178019Sjkim DELAY(10000); 83141016Sdfr 832178019Sjkim id = read_aux_data(kbdc); 833178019Sjkim VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id)); 83441016Sdfr 835178019Sjkim return (id); 83641016Sdfr} 83741016Sdfr 83841016Sdfrstatic int 83941016Sdfrset_mouse_sampling_rate(KBDC kbdc, int rate) 84041016Sdfr{ 841178019Sjkim int res; 84241016Sdfr 843178019Sjkim res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate); 844178019Sjkim VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res)); 84541016Sdfr 846178019Sjkim return ((res == PSM_ACK) ? rate : -1); 84741016Sdfr} 84841016Sdfr 84941016Sdfrstatic int 85041016Sdfrset_mouse_scaling(KBDC kbdc, int scale) 85141016Sdfr{ 852178019Sjkim int res; 85341016Sdfr 854178019Sjkim switch (scale) { 855178019Sjkim case 1: 856178019Sjkim default: 857178019Sjkim scale = PSMC_SET_SCALING11; 858178019Sjkim break; 859178019Sjkim case 2: 860178019Sjkim scale = PSMC_SET_SCALING21; 861178019Sjkim break; 862178019Sjkim } 863178019Sjkim res = send_aux_command(kbdc, scale); 864178019Sjkim VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 865178019Sjkim (scale == PSMC_SET_SCALING21) ? "21" : "11", res)); 86641016Sdfr 867178019Sjkim return (res == PSM_ACK); 86841016Sdfr} 86941016Sdfr 87041016Sdfr/* `val' must be 0 through PSMD_MAX_RESOLUTION */ 87141016Sdfrstatic int 87241016Sdfrset_mouse_resolution(KBDC kbdc, int val) 87341016Sdfr{ 874178019Sjkim int res; 87541016Sdfr 876178019Sjkim res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val); 877178019Sjkim VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res)); 87841016Sdfr 879178019Sjkim return ((res == PSM_ACK) ? val : -1); 88041016Sdfr} 88141016Sdfr 88241016Sdfr/* 88341016Sdfr * NOTE: once `set_mouse_mode()' is called, the mouse device must be 88441016Sdfr * re-enabled by calling `enable_aux_dev()' 88541016Sdfr */ 88641016Sdfrstatic int 88741016Sdfrset_mouse_mode(KBDC kbdc) 88841016Sdfr{ 889178019Sjkim int res; 89041016Sdfr 891178019Sjkim res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE); 892178019Sjkim VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res)); 89341016Sdfr 894178019Sjkim return (res == PSM_ACK); 89541016Sdfr} 89641016Sdfr 89741016Sdfrstatic int 89841016Sdfrget_mouse_buttons(KBDC kbdc) 89941016Sdfr{ 900178019Sjkim int c = 2; /* assume two buttons by default */ 901178019Sjkim int status[3]; 90241016Sdfr 903178019Sjkim /* 904178019Sjkim * NOTE: a special sequence to obtain Logitech Mouse specific 905178019Sjkim * information: set resolution to 25 ppi, set scaling to 1:1, set 906178019Sjkim * scaling to 1:1, set scaling to 1:1. Then the second byte of the 907178019Sjkim * mouse status bytes is the number of available buttons. 908178019Sjkim * Some manufactures also support this sequence. 909178019Sjkim */ 910178019Sjkim if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 911178019Sjkim return (c); 912178019Sjkim if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) && 913178019Sjkim set_mouse_scaling(kbdc, 1) && 914178019Sjkim get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0) 915178019Sjkim return (status[1]); 916178019Sjkim return (c); 91741016Sdfr} 91841016Sdfr 91941016Sdfr/* misc subroutines */ 92041016Sdfr/* 92141016Sdfr * Someday, I will get the complete list of valid pointing devices and 92241016Sdfr * their IDs... XXX 92341016Sdfr */ 92441016Sdfrstatic int 92541016Sdfris_a_mouse(int id) 92641016Sdfr{ 92741016Sdfr#if 0 928178019Sjkim static int valid_ids[] = { 929178019Sjkim PSM_MOUSE_ID, /* mouse */ 930178019Sjkim PSM_BALLPOINT_ID, /* ballpoint device */ 931178019Sjkim PSM_INTELLI_ID, /* Intellimouse */ 932178019Sjkim PSM_EXPLORER_ID, /* Intellimouse Explorer */ 933178019Sjkim -1 /* end of table */ 934178019Sjkim }; 935178019Sjkim int i; 93641016Sdfr 937178019Sjkim for (i = 0; valid_ids[i] >= 0; ++i) 938178019Sjkim if (valid_ids[i] == id) 939178019Sjkim return (TRUE); 940178019Sjkim return (FALSE); 94141016Sdfr#else 942178019Sjkim return (TRUE); 94341016Sdfr#endif 94441016Sdfr} 94541016Sdfr 94641016Sdfrstatic char * 94741016Sdfrmodel_name(int model) 94841016Sdfr{ 949178019Sjkim static struct { 950178019Sjkim int model_code; 951178019Sjkim char *model_name; 952178019Sjkim } models[] = { 953178019Sjkim { MOUSE_MODEL_NETSCROLL, "NetScroll" }, 954178019Sjkim { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" }, 955178019Sjkim { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" }, 956178019Sjkim { MOUSE_MODEL_THINK, "ThinkingMouse" }, 957178019Sjkim { MOUSE_MODEL_INTELLI, "IntelliMouse" }, 958178019Sjkim { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" }, 959178019Sjkim { MOUSE_MODEL_VERSAPAD, "VersaPad" }, 960178019Sjkim { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" }, 961178019Sjkim { MOUSE_MODEL_4D, "4D Mouse" }, 962178019Sjkim { MOUSE_MODEL_4DPLUS, "4D+ Mouse" }, 963178019Sjkim { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" }, 964248479Sjkim { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" }, 965307576Sgonzo { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" }, 966178019Sjkim { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" }, 967178019Sjkim { MOUSE_MODEL_UNKNOWN, "Unknown" }, 968178019Sjkim }; 969178019Sjkim int i; 97041016Sdfr 971178019Sjkim for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) 972178019Sjkim if (models[i].model_code == model) 973178019Sjkim break; 974178019Sjkim return (models[i].model_name); 97541016Sdfr} 97641016Sdfr 97741016Sdfrstatic void 97841016Sdfrrecover_from_error(KBDC kbdc) 97941016Sdfr{ 980178019Sjkim /* discard anything left in the output buffer */ 981178019Sjkim empty_both_buffers(kbdc, 10); 98241016Sdfr 98341016Sdfr#if 0 984178019Sjkim /* 985178019Sjkim * NOTE: KBDC_RESET_KBD may not restore the communication between the 986178019Sjkim * keyboard and the controller. 987178019Sjkim */ 988178019Sjkim reset_kbd(kbdc); 98941016Sdfr#else 990178019Sjkim /* 991178019Sjkim * NOTE: somehow diagnostic and keyboard port test commands bring the 992178019Sjkim * keyboard back. 993178019Sjkim */ 994178019Sjkim if (!test_controller(kbdc)) 995178019Sjkim log(LOG_ERR, "psm: keyboard controller failed.\n"); 996178019Sjkim /* if there isn't a keyboard in the system, the following error is OK */ 997178019Sjkim if (test_kbd_port(kbdc) != 0) 998178019Sjkim VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n")); 99941016Sdfr#endif 100041016Sdfr} 100141016Sdfr 100241016Sdfrstatic int 100341016Sdfrrestore_controller(KBDC kbdc, int command_byte) 100441016Sdfr{ 1005178019Sjkim empty_both_buffers(kbdc, 10); 100641016Sdfr 1007178019Sjkim if (!set_controller_command_byte(kbdc, 0xff, command_byte)) { 1008178019Sjkim log(LOG_ERR, "psm: failed to restore the keyboard controller " 1009178019Sjkim "command byte.\n"); 1010178019Sjkim empty_both_buffers(kbdc, 10); 1011178019Sjkim return (FALSE); 1012178019Sjkim } else { 1013178019Sjkim empty_both_buffers(kbdc, 10); 1014178019Sjkim return (TRUE); 1015178019Sjkim } 101641016Sdfr} 101741016Sdfr 1018178019Sjkim/* 101941016Sdfr * Re-initialize the aux port and device. The aux port must be enabled 1020178019Sjkim * and its interrupt must be disabled before calling this routine. 102141016Sdfr * The aux device will be disabled before returning. 102241016Sdfr * The keyboard controller must be locked via `kbdc_lock()' before 102341016Sdfr * calling this routine. 102441016Sdfr */ 102541016Sdfrstatic int 102684880Syokotadoinitialize(struct psm_softc *sc, mousemode_t *mode) 102741016Sdfr{ 1028178019Sjkim KBDC kbdc = sc->kbdc; 1029178019Sjkim int stat[3]; 1030178019Sjkim int i; 103141016Sdfr 1032178019Sjkim switch((i = test_aux_port(kbdc))) { 1033178019Sjkim case 1: /* ignore these errors */ 1034178019Sjkim case 2: 1035178019Sjkim case 3: 1036178019Sjkim case PSM_ACK: 1037178019Sjkim if (verbose) 1038178019Sjkim log(LOG_DEBUG, 1039178019Sjkim "psm%d: strange result for test aux port (%d).\n", 1040178019Sjkim sc->unit, i); 1041178019Sjkim /* FALLTHROUGH */ 1042178019Sjkim case 0: /* no error */ 1043178019Sjkim break; 1044178019Sjkim case -1: /* time out */ 1045178019Sjkim default: /* error */ 1046178019Sjkim recover_from_error(kbdc); 1047178019Sjkim if (sc->config & PSM_CONFIG_IGNPORTERROR) 1048178019Sjkim break; 1049178019Sjkim log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 1050178019Sjkim sc->unit, i); 1051178019Sjkim return (FALSE); 1052178019Sjkim } 105341016Sdfr 1054178019Sjkim if (sc->config & PSM_CONFIG_NORESET) { 1055178019Sjkim /* 1056178019Sjkim * Don't try to reset the pointing device. It may possibly 1057178019Sjkim * be left in the unknown state, though... 1058178019Sjkim */ 1059178019Sjkim } else { 1060178019Sjkim /* 1061178019Sjkim * NOTE: some controllers appears to hang the `keyboard' when 1062178019Sjkim * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 1063178019Sjkim */ 1064178019Sjkim if (!reset_aux_dev(kbdc)) { 1065178019Sjkim recover_from_error(kbdc); 1066178019Sjkim log(LOG_ERR, "psm%d: failed to reset the aux device.\n", 1067178019Sjkim sc->unit); 1068178019Sjkim return (FALSE); 1069178019Sjkim } 1070178019Sjkim } 1071178019Sjkim 1072178019Sjkim /* 1073178019Sjkim * both the aux port and the aux device is functioning, see 1074178019Sjkim * if the device can be enabled. 107545789Speter */ 1076178019Sjkim if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 1077178019Sjkim log(LOG_ERR, "psm%d: failed to enable the aux device.\n", 1078178019Sjkim sc->unit); 1079178019Sjkim return (FALSE); 108045789Speter } 1081178019Sjkim empty_both_buffers(kbdc, 10); /* remove stray data if any */ 108241016Sdfr 1083233580Sjkim /* Re-enable the mouse. */ 1084233580Sjkim for (i = 0; vendortype[i].probefunc != NULL; ++i) 1085233580Sjkim if (vendortype[i].model == sc->hw.model) 1086284320Sglebius (*vendortype[i].probefunc)(sc, REINIT); 108741016Sdfr 1088178019Sjkim /* set mouse parameters */ 1089178019Sjkim if (mode != (mousemode_t *)NULL) { 1090178019Sjkim if (mode->rate > 0) 1091178019Sjkim mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 1092178019Sjkim if (mode->resolution >= 0) 1093178019Sjkim mode->resolution = 1094178019Sjkim set_mouse_resolution(kbdc, mode->resolution); 1095178019Sjkim set_mouse_scaling(kbdc, 1); 1096178019Sjkim set_mouse_mode(kbdc); 1097178019Sjkim } 109841016Sdfr 1099178019Sjkim /* Record sync on the next data packet we see. */ 1100178019Sjkim sc->flags |= PSM_NEED_SYNCBITS; 110141016Sdfr 1102178019Sjkim /* just check the status of the mouse */ 1103178019Sjkim if (get_mouse_status(kbdc, stat, 0, 3) < 3) 1104178019Sjkim log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 1105178019Sjkim sc->unit); 110641016Sdfr 1107178019Sjkim return (TRUE); 110841016Sdfr} 110941016Sdfr 111041016Sdfrstatic int 111184880Syokotadoopen(struct psm_softc *sc, int command_byte) 111241016Sdfr{ 1113178019Sjkim int stat[3]; 1114343158Swulf int mux_enabled = FALSE; 111541016Sdfr 1116183888Sdumbbell /* 1117183888Sdumbbell * FIXME: Synaptics TouchPad seems to go back to Relative Mode with 1118183888Sdumbbell * no obvious reason. Thus we check the current mode and restore the 1119183888Sdumbbell * Absolute Mode if it was cleared. 1120183888Sdumbbell * 1121183888Sdumbbell * The previous hack at the end of psmprobe() wasn't efficient when 1122183888Sdumbbell * moused(8) was restarted. 1123183888Sdumbbell * 1124183888Sdumbbell * A Reset (FF) or Set Defaults (F6) command would clear the 1125183888Sdumbbell * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5 1126183888Sdumbbell * doesn't show any evidence of such a command. 1127183888Sdumbbell */ 1128183888Sdumbbell if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) { 1129343158Swulf if (sc->muxport != PSM_NOMUX) { 1130343158Swulf mux_enabled = enable_aux_mux(sc->kbdc) >= 0; 1131343158Swulf if (mux_enabled) 1132343158Swulf set_active_aux_mux_port(sc->kbdc, sc->muxport); 1133343158Swulf else 1134343158Swulf log(LOG_ERR, "psm%d: failed to enable " 1135343158Swulf "active multiplexing mode.\n", 1136343158Swulf sc->unit); 1137343158Swulf } 1138349541Swulf mouse_ext_command(sc->kbdc, SYNAPTICS_READ_MODES); 1139183888Sdumbbell get_mouse_status(sc->kbdc, stat, 0, 3); 1140244405Sdumbbell if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) || 1141343158Swulf stat[1] == 0x47) && 1142282734Srpaulo stat[2] == 0x40) { 1143282734Srpaulo synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 1144183888Sdumbbell VLOG(5, (LOG_DEBUG, "psm%d: Synaptis Absolute Mode " 1145183888Sdumbbell "hopefully restored\n", 1146183888Sdumbbell sc->unit)); 1147183888Sdumbbell } 1148343158Swulf if (mux_enabled) 1149343158Swulf disable_aux_mux(sc->kbdc); 1150183888Sdumbbell } 1151183888Sdumbbell 1152200674Sdumbbell /* 1153200674Sdumbbell * A user may want to disable tap and drag gestures on a Synaptics 1154200674Sdumbbell * TouchPad when it operates in Relative Mode. 1155200674Sdumbbell */ 1156200674Sdumbbell if (sc->hw.model == MOUSE_MODEL_GENERIC) { 1157200674Sdumbbell if (tap_enabled > 0) { 1158200674Sdumbbell VLOG(2, (LOG_DEBUG, 1159200674Sdumbbell "psm%d: enable tap and drag gestures\n", 1160200674Sdumbbell sc->unit)); 1161308652Sgonzo synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 1162200674Sdumbbell } else if (tap_enabled == 0) { 1163200674Sdumbbell VLOG(2, (LOG_DEBUG, 1164200674Sdumbbell "psm%d: disable tap and drag gestures\n", 1165200674Sdumbbell sc->unit)); 1166308652Sgonzo synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 1167200674Sdumbbell } 1168200674Sdumbbell } 1169200674Sdumbbell 1170178019Sjkim /* enable the mouse device */ 1171178019Sjkim if (!enable_aux_dev(sc->kbdc)) { 1172178019Sjkim /* MOUSE ERROR: failed to enable the mouse because: 1173178019Sjkim * 1) the mouse is faulty, 1174178019Sjkim * 2) the mouse has been removed(!?) 1175178019Sjkim * In the latter case, the keyboard may have hung, and need 1176178019Sjkim * recovery procedure... 1177178019Sjkim */ 1178178019Sjkim recover_from_error(sc->kbdc); 117941016Sdfr#if 0 1180178019Sjkim /* FIXME: we could reset the mouse here and try to enable 1181178019Sjkim * it again. But it will take long time and it's not a good 1182178019Sjkim * idea to disable the keyboard that long... 1183178019Sjkim */ 1184178019Sjkim if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 1185178019Sjkim recover_from_error(sc->kbdc); 118641016Sdfr#else 1187178019Sjkim { 118841016Sdfr#endif 1189178019Sjkim restore_controller(sc->kbdc, command_byte); 1190178019Sjkim /* mark this device is no longer available */ 1191178019Sjkim sc->state &= ~PSM_VALID; 1192178019Sjkim log(LOG_ERR, 1193178019Sjkim "psm%d: failed to enable the device (doopen).\n", 1194178019Sjkim sc->unit); 1195178019Sjkim return (EIO); 1196178019Sjkim } 119741016Sdfr } 119841016Sdfr 1199178019Sjkim if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 1200178019Sjkim log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", 1201178019Sjkim sc->unit); 120241016Sdfr 1203178019Sjkim /* enable the aux port and interrupt */ 1204178019Sjkim if (!set_controller_command_byte(sc->kbdc, 120541016Sdfr kbdc_get_device_mask(sc->kbdc), 1206178019Sjkim (command_byte & KBD_KBD_CONTROL_BITS) | 1207178019Sjkim KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 1208178019Sjkim /* CONTROLLER ERROR */ 1209178019Sjkim disable_aux_dev(sc->kbdc); 1210178019Sjkim restore_controller(sc->kbdc, command_byte); 1211178019Sjkim log(LOG_ERR, 1212178019Sjkim "psm%d: failed to enable the aux interrupt (doopen).\n", 1213178019Sjkim sc->unit); 1214178019Sjkim return (EIO); 1215178019Sjkim } 121641016Sdfr 1217178019Sjkim /* start the watchdog timer */ 1218178019Sjkim sc->watchdog = FALSE; 1219240211Sjhb callout_reset(&sc->callout, hz * 2, psmtimeout, sc); 122058230Syokota 1221178019Sjkim return (0); 122241016Sdfr} 122341016Sdfr 122484880Syokotastatic int 122584880Syokotareinitialize(struct psm_softc *sc, int doinit) 122684880Syokota{ 1227178019Sjkim int err; 1228178019Sjkim int c; 1229178019Sjkim int s; 123084880Syokota 1231178019Sjkim /* don't let anybody mess with the aux device */ 1232178019Sjkim if (!kbdc_lock(sc->kbdc, TRUE)) 1233178019Sjkim return (EIO); 1234178019Sjkim s = spltty(); 123584880Syokota 1236178019Sjkim /* block our watchdog timer */ 1237178019Sjkim sc->watchdog = FALSE; 1238240211Sjhb callout_stop(&sc->callout); 123984880Syokota 1240178019Sjkim /* save the current controller command byte */ 1241178019Sjkim empty_both_buffers(sc->kbdc, 10); 1242178019Sjkim c = get_controller_command_byte(sc->kbdc); 1243178019Sjkim VLOG(2, (LOG_DEBUG, 1244178019Sjkim "psm%d: current command byte: %04x (reinitialize).\n", 1245178019Sjkim sc->unit, c)); 124684880Syokota 1247178019Sjkim /* enable the aux port but disable the aux interrupt and the keyboard */ 1248178019Sjkim if ((c == -1) || !set_controller_command_byte(sc->kbdc, 124984880Syokota kbdc_get_device_mask(sc->kbdc), 1250178019Sjkim KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | 1251178019Sjkim KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1252178019Sjkim /* CONTROLLER ERROR */ 1253178019Sjkim splx(s); 1254178019Sjkim kbdc_lock(sc->kbdc, FALSE); 1255178019Sjkim log(LOG_ERR, 1256178019Sjkim "psm%d: unable to set the command byte (reinitialize).\n", 1257178019Sjkim sc->unit); 1258178019Sjkim return (EIO); 1259178019Sjkim } 126084880Syokota 1261178019Sjkim /* flush any data */ 1262178019Sjkim if (sc->state & PSM_VALID) { 1263178019Sjkim /* this may fail; but never mind... */ 1264178019Sjkim disable_aux_dev(sc->kbdc); 1265178019Sjkim empty_aux_buffer(sc->kbdc, 10); 1266178019Sjkim } 1267178019Sjkim flushpackets(sc); 1268178019Sjkim sc->syncerrors = 0; 1269178019Sjkim sc->pkterrors = 0; 1270178019Sjkim memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr)); 127184880Syokota 1272178019Sjkim /* try to detect the aux device; are you still there? */ 1273178019Sjkim err = 0; 1274178019Sjkim if (doinit) { 1275178019Sjkim if (doinitialize(sc, &sc->mode)) { 1276178019Sjkim /* yes */ 1277178019Sjkim sc->state |= PSM_VALID; 1278178019Sjkim } else { 1279178019Sjkim /* the device has gone! */ 1280178019Sjkim restore_controller(sc->kbdc, c); 1281178019Sjkim sc->state &= ~PSM_VALID; 1282178019Sjkim log(LOG_ERR, 1283178019Sjkim "psm%d: the aux device has gone! (reinitialize).\n", 1284178019Sjkim sc->unit); 1285178019Sjkim err = ENXIO; 1286178019Sjkim } 128784880Syokota } 1288178019Sjkim splx(s); 128984880Syokota 1290178019Sjkim /* restore the driver state */ 1291321060Swulf if ((sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A)) && 1292321060Swulf (err == 0)) { 1293178019Sjkim /* enable the aux device and the port again */ 1294178019Sjkim err = doopen(sc, c); 1295178019Sjkim if (err != 0) 1296178019Sjkim log(LOG_ERR, "psm%d: failed to enable the device " 1297178019Sjkim "(reinitialize).\n", sc->unit); 1298178019Sjkim } else { 1299178019Sjkim /* restore the keyboard port and disable the aux port */ 1300178019Sjkim if (!set_controller_command_byte(sc->kbdc, 1301178019Sjkim kbdc_get_device_mask(sc->kbdc), 1302178019Sjkim (c & KBD_KBD_CONTROL_BITS) | 1303178019Sjkim KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1304178019Sjkim /* CONTROLLER ERROR */ 1305178019Sjkim log(LOG_ERR, "psm%d: failed to disable the aux port " 1306178019Sjkim "(reinitialize).\n", sc->unit); 1307178019Sjkim err = EIO; 1308178019Sjkim } 130984880Syokota } 131084880Syokota 1311178019Sjkim kbdc_lock(sc->kbdc, FALSE); 1312178019Sjkim return (err); 131384880Syokota} 131484880Syokota 131541016Sdfr/* psm driver entry points */ 131641016Sdfr 131783147Syokotastatic void 131883147Syokotapsmidentify(driver_t *driver, device_t parent) 131983147Syokota{ 1320178019Sjkim device_t psmc; 1321178019Sjkim device_t psm; 1322178019Sjkim u_long irq; 1323178019Sjkim int unit; 132483147Syokota 1325178019Sjkim unit = device_get_unit(parent); 132683931Syokota 1327178019Sjkim /* always add at least one child */ 1328178019Sjkim psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit); 1329178019Sjkim if (psm == NULL) 1330178019Sjkim return; 133183931Syokota 1332178019Sjkim irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX); 1333178019Sjkim if (irq > 0) 1334178019Sjkim return; 133583931Syokota 1336178019Sjkim /* 1337178019Sjkim * If the PS/2 mouse device has already been reported by ACPI or 1338178019Sjkim * PnP BIOS, obtain the IRQ resource from it. 1339178019Sjkim * (See psmcpnp_attach() below.) 1340178019Sjkim */ 1341178019Sjkim psmc = device_find_child(device_get_parent(parent), 1342178019Sjkim PSMCPNP_DRIVER_NAME, unit); 1343178019Sjkim if (psmc == NULL) 1344178019Sjkim return; 1345178019Sjkim irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0); 1346178019Sjkim if (irq <= 0) 1347178019Sjkim return; 1348216491Sjhb bus_delete_resource(psmc, SYS_RES_IRQ, 0); 1349178019Sjkim bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 135083147Syokota} 135183147Syokota 1352178019Sjkim#define endprobe(v) do { \ 1353178019Sjkim if (bootverbose) \ 1354178019Sjkim --verbose; \ 1355178019Sjkim kbdc_set_device_mask(sc->kbdc, mask); \ 1356178019Sjkim kbdc_lock(sc->kbdc, FALSE); \ 1357178019Sjkim return (v); \ 1358178019Sjkim} while (0) 135941016Sdfr 136041016Sdfrstatic int 136141016Sdfrpsmprobe(device_t dev) 136241016Sdfr{ 1363178019Sjkim int unit = device_get_unit(dev); 1364178019Sjkim struct psm_softc *sc = device_get_softc(dev); 1365178019Sjkim int stat[3]; 1366178019Sjkim int command_byte; 1367178019Sjkim int mask; 1368178019Sjkim int rid; 1369178019Sjkim int i; 137041016Sdfr 137141016Sdfr#if 0 1372178019Sjkim kbdc_debug(TRUE); 137341016Sdfr#endif 137458230Syokota 1375178019Sjkim /* see if IRQ is available */ 1376178019Sjkim rid = KBDC_RID_AUX; 1377216491Sjhb sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 1378178019Sjkim if (sc->intr == NULL) { 1379178019Sjkim if (bootverbose) 1380178019Sjkim device_printf(dev, "unable to allocate IRQ\n"); 1381178019Sjkim return (ENXIO); 1382178019Sjkim } 1383178019Sjkim bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 138458230Syokota 1385178019Sjkim sc->unit = unit; 1386178019Sjkim sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 1387178019Sjkim sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS; 1388178019Sjkim /* XXX: for backward compatibility */ 138958230Syokota#if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM) 1390178019Sjkim sc->config |= 139158230Syokota#ifdef PSM_RESETAFTERSUSPEND 1392233580Sjkim PSM_CONFIG_INITAFTERSUSPEND; 139358230Syokota#else 139458230Syokota PSM_CONFIG_HOOKRESUME; 139558230Syokota#endif 139658230Syokota#endif /* PSM_HOOKRESUME | PSM_HOOKAPM */ 1397178019Sjkim sc->flags = 0; 1398343158Swulf sc->muxport = PSM_NOMUX; 1399178019Sjkim if (bootverbose) 1400178019Sjkim ++verbose; 140141016Sdfr 1402178019Sjkim device_set_desc(dev, "PS/2 Mouse"); 140343105Sdfr 1404178019Sjkim if (!kbdc_lock(sc->kbdc, TRUE)) { 1405178019Sjkim printf("psm%d: unable to lock the controller.\n", unit); 1406178019Sjkim if (bootverbose) 1407178019Sjkim --verbose; 1408178019Sjkim return (ENXIO); 1409178019Sjkim } 141041016Sdfr 1411178019Sjkim /* 1412178019Sjkim * NOTE: two bits in the command byte controls the operation of the 1413178019Sjkim * aux port (mouse port): the aux port disable bit (bit 5) and the aux 1414178019Sjkim * port interrupt (IRQ 12) enable bit (bit 2). 1415178019Sjkim */ 141641016Sdfr 1417178019Sjkim /* discard anything left after the keyboard initialization */ 1418178019Sjkim empty_both_buffers(sc->kbdc, 10); 141941016Sdfr 1420178019Sjkim /* save the current command byte; it will be used later */ 1421178019Sjkim mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS; 1422178019Sjkim command_byte = get_controller_command_byte(sc->kbdc); 1423178019Sjkim if (verbose) 1424178019Sjkim printf("psm%d: current command byte:%04x\n", unit, 1425178019Sjkim command_byte); 1426178019Sjkim if (command_byte == -1) { 1427178019Sjkim /* CONTROLLER ERROR */ 1428178019Sjkim printf("psm%d: unable to get the current command byte value.\n", 1429178019Sjkim unit); 1430178019Sjkim endprobe(ENXIO); 1431178019Sjkim } 143241016Sdfr 1433178019Sjkim /* 1434178019Sjkim * disable the keyboard port while probing the aux port, which must be 1435178019Sjkim * enabled during this routine 1436178019Sjkim */ 1437178019Sjkim if (!set_controller_command_byte(sc->kbdc, 143841016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 1439178019Sjkim KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | 1440178019Sjkim KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1441178019Sjkim /* 1442178019Sjkim * this is CONTROLLER ERROR; I don't know how to recover 1443178019Sjkim * from this error... 1444178019Sjkim */ 1445278787Sgrembo if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc)) 1446278787Sgrembo restore_controller(sc->kbdc, command_byte); 1447178019Sjkim printf("psm%d: unable to set the command byte.\n", unit); 1448178019Sjkim endprobe(ENXIO); 1449178019Sjkim } 1450178019Sjkim write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 145141016Sdfr 145245789Speter /* 1453178019Sjkim * NOTE: `test_aux_port()' is designed to return with zero if the aux 1454178019Sjkim * port exists and is functioning. However, some controllers appears 1455178019Sjkim * to respond with zero even when the aux port doesn't exist. (It may 1456178019Sjkim * be that this is only the case when the controller DOES have the aux 1457178019Sjkim * port but the port is not wired on the motherboard.) The keyboard 1458178019Sjkim * controllers without the port, such as the original AT, are 1459215473Sjhb * supposed to return with an error code or simply time out. In any 1460178019Sjkim * case, we have to continue probing the port even when the controller 1461178019Sjkim * passes this test. 1462117302Smikeh * 1463178019Sjkim * XXX: some controllers erroneously return the error code 1, 2 or 3 1464215473Sjhb * when it has a perfectly functional aux port. We have to ignore 1465178019Sjkim * this error code. Even if the controller HAS error with the aux 1466178019Sjkim * port, it will be detected later... 1467178019Sjkim * XXX: another incompatible controller returns PSM_ACK (0xfa)... 146845789Speter */ 1469178019Sjkim switch ((i = test_aux_port(sc->kbdc))) { 1470178019Sjkim case 1: /* ignore these errors */ 1471178019Sjkim case 2: 1472178019Sjkim case 3: 1473178019Sjkim case PSM_ACK: 1474178019Sjkim if (verbose) 1475178019Sjkim printf("psm%d: strange result for test aux port " 1476178019Sjkim "(%d).\n", unit, i); 1477178019Sjkim /* FALLTHROUGH */ 1478178019Sjkim case 0: /* no error */ 1479178019Sjkim break; 1480178019Sjkim case -1: /* time out */ 1481178019Sjkim default: /* error */ 1482178019Sjkim recover_from_error(sc->kbdc); 1483178019Sjkim if (sc->config & PSM_CONFIG_IGNPORTERROR) 1484178019Sjkim break; 1485278787Sgrembo if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc)) 1486278787Sgrembo restore_controller(sc->kbdc, command_byte); 1487178019Sjkim if (verbose) 1488178019Sjkim printf("psm%d: the aux port is not functioning (%d).\n", 1489178019Sjkim unit, i); 1490178019Sjkim endprobe(ENXIO); 149145789Speter } 149245789Speter 1493178019Sjkim if (sc->config & PSM_CONFIG_NORESET) { 1494178019Sjkim /* 1495178019Sjkim * Don't try to reset the pointing device. It may possibly be 1496215473Sjhb * left in an unknown state, though... 1497178019Sjkim */ 1498178019Sjkim } else { 1499178019Sjkim /* 1500178019Sjkim * NOTE: some controllers appears to hang the `keyboard' when 1501178019Sjkim * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 1502178019Sjkim * 1503178019Sjkim * Attempt to reset the controller twice -- this helps 1504178019Sjkim * pierce through some KVM switches. The second reset 1505178019Sjkim * is non-fatal. 1506178019Sjkim */ 1507178019Sjkim if (!reset_aux_dev(sc->kbdc)) { 1508178019Sjkim recover_from_error(sc->kbdc); 1509278787Sgrembo if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc)) 1510278787Sgrembo restore_controller(sc->kbdc, command_byte); 1511178019Sjkim if (verbose) 1512178019Sjkim printf("psm%d: failed to reset the aux " 1513178019Sjkim "device.\n", unit); 1514178019Sjkim endprobe(ENXIO); 1515178019Sjkim } else if (!reset_aux_dev(sc->kbdc)) { 1516178019Sjkim recover_from_error(sc->kbdc); 1517178019Sjkim if (verbose >= 2) 1518178019Sjkim printf("psm%d: failed to reset the aux device " 1519178019Sjkim "(2).\n", unit); 1520178019Sjkim } 1521178019Sjkim } 152241016Sdfr 1523178019Sjkim /* 1524215473Sjhb * both the aux port and the aux device are functioning, see if the 1525178019Sjkim * device can be enabled. NOTE: when enabled, the device will start 1526178019Sjkim * sending data; we shall immediately disable the device once we know 1527178019Sjkim * the device can be enabled. 1528178019Sjkim */ 1529178019Sjkim if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 1530178019Sjkim /* MOUSE ERROR */ 1531178019Sjkim recover_from_error(sc->kbdc); 1532278787Sgrembo if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc)) 1533278787Sgrembo restore_controller(sc->kbdc, command_byte); 1534178019Sjkim if (verbose) 1535178019Sjkim printf("psm%d: failed to enable the aux device.\n", 1536178019Sjkim unit); 1537178019Sjkim endprobe(ENXIO); 1538178019Sjkim } 153941016Sdfr 1540178019Sjkim /* save the default values after reset */ 1541178019Sjkim if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 1542178019Sjkim sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1543178019Sjkim sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1544178019Sjkim } else { 1545178019Sjkim sc->dflt_mode.rate = sc->mode.rate = -1; 1546178019Sjkim sc->dflt_mode.resolution = sc->mode.resolution = -1; 1547178019Sjkim } 154841016Sdfr 1549178019Sjkim /* hardware information */ 1550178019Sjkim sc->hw.iftype = MOUSE_IF_PS2; 155141016Sdfr 1552178019Sjkim /* verify the device is a mouse */ 1553178019Sjkim sc->hw.hwid = get_aux_id(sc->kbdc); 1554178019Sjkim if (!is_a_mouse(sc->hw.hwid)) { 1555278787Sgrembo if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc)) 1556278787Sgrembo restore_controller(sc->kbdc, command_byte); 1557178019Sjkim if (verbose) 1558178019Sjkim printf("psm%d: unknown device type (%d).\n", unit, 1559178019Sjkim sc->hw.hwid); 1560178019Sjkim endprobe(ENXIO); 1561178019Sjkim } 1562178019Sjkim switch (sc->hw.hwid) { 1563178019Sjkim case PSM_BALLPOINT_ID: 1564178019Sjkim sc->hw.type = MOUSE_TRACKBALL; 156545789Speter break; 1566178019Sjkim case PSM_MOUSE_ID: 1567178019Sjkim case PSM_INTELLI_ID: 1568178019Sjkim case PSM_EXPLORER_ID: 1569178019Sjkim case PSM_4DMOUSE_ID: 1570178019Sjkim case PSM_4DPLUS_ID: 1571178019Sjkim sc->hw.type = MOUSE_MOUSE; 1572178019Sjkim break; 1573178019Sjkim default: 1574178019Sjkim sc->hw.type = MOUSE_UNKNOWN; 1575178019Sjkim break; 157641016Sdfr } 157741016Sdfr 1578178019Sjkim if (sc->config & PSM_CONFIG_NOIDPROBE) { 1579178019Sjkim sc->hw.buttons = 2; 1580178019Sjkim i = GENERIC_MOUSE_ENTRY; 1581178019Sjkim } else { 1582178019Sjkim /* # of buttons */ 1583178019Sjkim sc->hw.buttons = get_mouse_buttons(sc->kbdc); 158441016Sdfr 1585178019Sjkim /* other parameters */ 1586178019Sjkim for (i = 0; vendortype[i].probefunc != NULL; ++i) 1587284320Sglebius if ((*vendortype[i].probefunc)(sc, PROBE)) { 1588178019Sjkim if (verbose >= 2) 1589178019Sjkim printf("psm%d: found %s\n", unit, 1590178019Sjkim model_name(vendortype[i].model)); 1591178019Sjkim break; 1592178019Sjkim } 1593178019Sjkim } 159441016Sdfr 1595178019Sjkim sc->hw.model = vendortype[i].model; 1596178019Sjkim 1597178019Sjkim sc->dflt_mode.level = PSM_LEVEL_BASE; 1598178019Sjkim sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 1599178019Sjkim sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 1600178019Sjkim if (sc->config & PSM_CONFIG_NOCHECKSYNC) 1601178019Sjkim sc->dflt_mode.syncmask[0] = 0; 1602178019Sjkim else 1603178019Sjkim sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 1604178019Sjkim if (sc->config & PSM_CONFIG_FORCETAP) 1605178019Sjkim sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP; 1606178019Sjkim sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 1607178019Sjkim sc->mode = sc->dflt_mode; 1608178019Sjkim sc->mode.packetsize = vendortype[i].packetsize; 1609178019Sjkim 1610178019Sjkim /* set mouse parameters */ 161148773Syokota#if 0 1612178019Sjkim /* 1613178019Sjkim * A version of Logitech FirstMouse+ won't report wheel movement, 1614178019Sjkim * if SET_DEFAULTS is sent... Don't use this command. 1615178019Sjkim * This fix was found by Takashi Nishida. 1616178019Sjkim */ 1617178019Sjkim i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 1618178019Sjkim if (verbose >= 2) 1619178019Sjkim printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 162048773Syokota#endif 1621178019Sjkim if (sc->config & PSM_CONFIG_RESOLUTION) 1622178019Sjkim sc->mode.resolution = 1623178019Sjkim set_mouse_resolution(sc->kbdc, 1624178019Sjkim (sc->config & PSM_CONFIG_RESOLUTION) - 1); 1625178019Sjkim else if (sc->mode.resolution >= 0) 1626178019Sjkim sc->mode.resolution = 1627178019Sjkim set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 1628178019Sjkim if (sc->mode.rate > 0) 1629178019Sjkim sc->mode.rate = 1630178019Sjkim set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 1631178019Sjkim set_mouse_scaling(sc->kbdc, 1); 163241016Sdfr 1633178019Sjkim /* Record sync on the next data packet we see. */ 1634178019Sjkim sc->flags |= PSM_NEED_SYNCBITS; 163541016Sdfr 1636178019Sjkim /* just check the status of the mouse */ 1637178019Sjkim /* 1638178019Sjkim * NOTE: XXX there are some arcane controller/mouse combinations out 1639178019Sjkim * there, which hung the controller unless there is data transmission 1640178019Sjkim * after ACK from the mouse. 164141016Sdfr */ 1642178019Sjkim if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 1643178019Sjkim printf("psm%d: failed to get status.\n", unit); 1644178019Sjkim else { 1645178019Sjkim /* 1646178019Sjkim * When in its native mode, some mice operate with different 1647178019Sjkim * default parameters than in the PS/2 compatible mode. 1648178019Sjkim */ 1649178019Sjkim sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1650178019Sjkim sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1651178019Sjkim } 165241016Sdfr 1653178019Sjkim /* disable the aux port for now... */ 1654178019Sjkim if (!set_controller_command_byte(sc->kbdc, 165541016Sdfr KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS, 1656178019Sjkim (command_byte & KBD_KBD_CONTROL_BITS) | 1657178019Sjkim KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1658178019Sjkim /* 1659178019Sjkim * this is CONTROLLER ERROR; I don't know the proper way to 1660178019Sjkim * recover from this error... 1661178019Sjkim */ 1662278787Sgrembo if (ALWAYS_RESTORE_CONTROLLER(sc->kbdc)) 1663278787Sgrembo restore_controller(sc->kbdc, command_byte); 1664178019Sjkim printf("psm%d: unable to set the command byte.\n", unit); 1665178019Sjkim endprobe(ENXIO); 1666178019Sjkim } 1667178019Sjkim 1668178019Sjkim /* done */ 1669178019Sjkim kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS); 1670178019Sjkim kbdc_lock(sc->kbdc, FALSE); 1671178019Sjkim return (0); 167241016Sdfr} 167341016Sdfr 1674321060Swulf#ifdef EVDEV_SUPPORT 1675321060Swulf/* Values are taken from Linux drivers for userland software compatibility */ 1676321060Swulf#define PS2_MOUSE_VENDOR 0x0002 1677321060Swulf#define PS2_MOUSE_GENERIC_PRODUCT 0x0001 1678321060Swulf#define PS2_MOUSE_SYNAPTICS_NAME "SynPS/2 Synaptics TouchPad" 1679321060Swulf#define PS2_MOUSE_SYNAPTICS_PRODUCT 0x0007 1680321060Swulf#define PS2_MOUSE_TRACKPOINT_NAME "TPPS/2 IBM TrackPoint" 1681321060Swulf#define PS2_MOUSE_TRACKPOINT_PRODUCT 0x000A 1682321060Swulf#define PS2_MOUSE_ELANTECH_NAME "ETPS/2 Elantech Touchpad" 1683321060Swulf#define PS2_MOUSE_ELANTECH_ST_NAME "ETPS/2 Elantech TrackPoint" 1684321060Swulf#define PS2_MOUSE_ELANTECH_PRODUCT 0x000E 1685321060Swulf 1686321060Swulf#define ABSINFO_END { ABS_CNT, 0, 0, 0 } 1687321060Swulf 1688321060Swulfstatic void 1689321060Swulfpsm_support_abs_bulk(struct evdev_dev *evdev, const uint16_t info[][4]) 1690321060Swulf{ 1691321060Swulf size_t i; 1692321060Swulf 1693321060Swulf for (i = 0; info[i][0] != ABS_CNT; i++) 1694321060Swulf evdev_support_abs(evdev, info[i][0], 0, info[i][1], info[i][2], 1695321060Swulf 0, 0, info[i][3]); 1696321060Swulf} 1697321060Swulf 1698321060Swulfstatic void 1699321060Swulfpsm_push_mt_finger(struct psm_softc *sc, int id, const finger_t *f) 1700321060Swulf{ 1701321060Swulf int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y; 1702321060Swulf 1703321060Swulf evdev_push_abs(sc->evdev_a, ABS_MT_SLOT, id); 1704321060Swulf evdev_push_abs(sc->evdev_a, ABS_MT_TRACKING_ID, id); 1705321060Swulf evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_X, f->x); 1706321060Swulf evdev_push_abs(sc->evdev_a, ABS_MT_POSITION_Y, y); 1707321060Swulf evdev_push_abs(sc->evdev_a, ABS_MT_PRESSURE, f->p); 1708321060Swulf} 1709321060Swulf 1710321060Swulfstatic void 1711321060Swulfpsm_push_st_finger(struct psm_softc *sc, const finger_t *f) 1712321060Swulf{ 1713321060Swulf int y = sc->synhw.minimumYCoord + sc->synhw.maximumYCoord - f->y; 1714321060Swulf 1715321060Swulf evdev_push_abs(sc->evdev_a, ABS_X, f->x); 1716321060Swulf evdev_push_abs(sc->evdev_a, ABS_Y, y); 1717321060Swulf evdev_push_abs(sc->evdev_a, ABS_PRESSURE, f->p); 1718321060Swulf if (sc->synhw.capPalmDetect) 1719321060Swulf evdev_push_abs(sc->evdev_a, ABS_TOOL_WIDTH, f->w); 1720321060Swulf} 1721321060Swulf 1722321060Swulfstatic void 1723321060Swulfpsm_release_mt_slot(struct evdev_dev *evdev, int32_t slot) 1724321060Swulf{ 1725321060Swulf 1726321060Swulf evdev_push_abs(evdev, ABS_MT_SLOT, slot); 1727321060Swulf evdev_push_abs(evdev, ABS_MT_TRACKING_ID, -1); 1728321060Swulf} 1729321060Swulf 173041016Sdfrstatic int 1731321060Swulfpsm_register(device_t dev, int model_code) 1732321060Swulf{ 1733321060Swulf struct psm_softc *sc = device_get_softc(dev); 1734321060Swulf struct evdev_dev *evdev_r; 1735321060Swulf int error, i, nbuttons, nwheels, product; 1736321060Swulf bool is_pointing_stick; 1737321060Swulf const char *name; 1738321060Swulf 1739321060Swulf name = model_name(model_code); 1740321060Swulf nbuttons = sc->hw.buttons; 1741321060Swulf product = PS2_MOUSE_GENERIC_PRODUCT; 1742321060Swulf nwheels = 0; 1743321060Swulf is_pointing_stick = false; 1744321060Swulf 1745321060Swulf switch (model_code) { 1746321060Swulf case MOUSE_MODEL_TRACKPOINT: 1747321060Swulf name = PS2_MOUSE_TRACKPOINT_NAME; 1748321060Swulf product = PS2_MOUSE_TRACKPOINT_PRODUCT; 1749321060Swulf nbuttons = 3; 1750321060Swulf is_pointing_stick = true; 1751321060Swulf break; 1752321060Swulf 1753321060Swulf case MOUSE_MODEL_ELANTECH: 1754321060Swulf name = PS2_MOUSE_ELANTECH_ST_NAME; 1755321060Swulf product = PS2_MOUSE_ELANTECH_PRODUCT; 1756321060Swulf nbuttons = 3; 1757321060Swulf is_pointing_stick = true; 1758321060Swulf break; 1759321060Swulf 1760321060Swulf case MOUSE_MODEL_MOUSEMANPLUS: 1761321060Swulf case MOUSE_MODEL_4D: 1762321060Swulf nwheels = 2; 1763321060Swulf break; 1764321060Swulf 1765321060Swulf case MOUSE_MODEL_EXPLORER: 1766321060Swulf case MOUSE_MODEL_INTELLI: 1767321060Swulf case MOUSE_MODEL_NET: 1768321060Swulf case MOUSE_MODEL_NETSCROLL: 1769321060Swulf case MOUSE_MODEL_4DPLUS: 1770321060Swulf nwheels = 1; 1771321060Swulf break; 1772321060Swulf } 1773321060Swulf 1774321060Swulf evdev_r = evdev_alloc(); 1775321060Swulf evdev_set_name(evdev_r, name); 1776321060Swulf evdev_set_phys(evdev_r, device_get_nameunit(dev)); 1777321060Swulf evdev_set_id(evdev_r, BUS_I8042, PS2_MOUSE_VENDOR, product, 0); 1778321060Swulf evdev_set_methods(evdev_r, sc, &psm_ev_methods_r); 1779321060Swulf 1780321060Swulf evdev_support_prop(evdev_r, INPUT_PROP_POINTER); 1781321060Swulf if (is_pointing_stick) 1782321060Swulf evdev_support_prop(evdev_r, INPUT_PROP_POINTING_STICK); 1783321060Swulf evdev_support_event(evdev_r, EV_SYN); 1784321060Swulf evdev_support_event(evdev_r, EV_KEY); 1785321060Swulf evdev_support_event(evdev_r, EV_REL); 1786321060Swulf evdev_support_rel(evdev_r, REL_X); 1787321060Swulf evdev_support_rel(evdev_r, REL_Y); 1788321060Swulf switch (nwheels) { 1789321060Swulf case 2: 1790321060Swulf evdev_support_rel(evdev_r, REL_HWHEEL); 1791321060Swulf /* FALLTHROUGH */ 1792321060Swulf case 1: 1793321060Swulf evdev_support_rel(evdev_r, REL_WHEEL); 1794321060Swulf } 1795321060Swulf for (i = 0; i < nbuttons; i++) 1796321060Swulf evdev_support_key(evdev_r, BTN_MOUSE + i); 1797321060Swulf 1798321060Swulf error = evdev_register_mtx(evdev_r, &Giant); 1799321060Swulf if (error) 1800321060Swulf evdev_free(evdev_r); 1801321060Swulf else 1802321060Swulf sc->evdev_r = evdev_r; 1803321060Swulf return (error); 1804321060Swulf} 1805321060Swulf 1806321060Swulfstatic int 1807321060Swulfpsm_register_synaptics(device_t dev) 1808321060Swulf{ 1809321060Swulf struct psm_softc *sc = device_get_softc(dev); 1810321060Swulf const uint16_t synaptics_absinfo_st[][4] = { 1811321060Swulf { ABS_X, sc->synhw.minimumXCoord, 1812321060Swulf sc->synhw.maximumXCoord, sc->synhw.infoXupmm }, 1813321060Swulf { ABS_Y, sc->synhw.minimumYCoord, 1814321060Swulf sc->synhw.maximumYCoord, sc->synhw.infoYupmm }, 1815321060Swulf { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 }, 1816321060Swulf ABSINFO_END, 1817321060Swulf }; 1818321060Swulf const uint16_t synaptics_absinfo_mt[][4] = { 1819321060Swulf { ABS_MT_SLOT, 0, PSM_FINGERS-1, 0}, 1820321060Swulf { ABS_MT_TRACKING_ID, -1, PSM_FINGERS-1, 0}, 1821321060Swulf { ABS_MT_POSITION_X, sc->synhw.minimumXCoord, 1822321060Swulf sc->synhw.maximumXCoord, sc->synhw.infoXupmm }, 1823321060Swulf { ABS_MT_POSITION_Y, sc->synhw.minimumYCoord, 1824321060Swulf sc->synhw.maximumYCoord, sc->synhw.infoYupmm }, 1825321060Swulf { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 }, 1826321060Swulf ABSINFO_END, 1827321060Swulf }; 1828321060Swulf struct evdev_dev *evdev_a; 1829321060Swulf int error, i, guest_model; 1830321060Swulf 1831321060Swulf evdev_a = evdev_alloc(); 1832321060Swulf evdev_set_name(evdev_a, PS2_MOUSE_SYNAPTICS_NAME); 1833321060Swulf evdev_set_phys(evdev_a, device_get_nameunit(dev)); 1834321060Swulf evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR, 1835321060Swulf PS2_MOUSE_SYNAPTICS_PRODUCT, 0); 1836321060Swulf evdev_set_methods(evdev_a, sc, &psm_ev_methods_a); 1837321060Swulf 1838321060Swulf evdev_support_event(evdev_a, EV_SYN); 1839321060Swulf evdev_support_event(evdev_a, EV_KEY); 1840321060Swulf evdev_support_event(evdev_a, EV_ABS); 1841321060Swulf evdev_support_prop(evdev_a, INPUT_PROP_POINTER); 1842321060Swulf if (sc->synhw.capAdvancedGestures) 1843321060Swulf evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT); 1844321060Swulf if (sc->synhw.capClickPad) 1845321060Swulf evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD); 1846344165Swulf if (sc->synhw.capClickPad && sc->synhw.topButtonPad) 1847344165Swulf evdev_support_prop(evdev_a, INPUT_PROP_TOPBUTTONPAD); 1848321060Swulf evdev_support_key(evdev_a, BTN_TOUCH); 1849349541Swulf evdev_support_nfingers(evdev_a, sc->synhw.capReportsV ? 5 : 3); 1850321060Swulf psm_support_abs_bulk(evdev_a, synaptics_absinfo_st); 1851321060Swulf if (sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) 1852321060Swulf psm_support_abs_bulk(evdev_a, synaptics_absinfo_mt); 1853321060Swulf if (sc->synhw.capPalmDetect) 1854321060Swulf evdev_support_abs(evdev_a, ABS_TOOL_WIDTH, 0, 0, 15, 0, 0, 0); 1855321060Swulf evdev_support_key(evdev_a, BTN_LEFT); 1856321060Swulf if (!sc->synhw.capClickPad) { 1857321060Swulf evdev_support_key(evdev_a, BTN_RIGHT); 1858321060Swulf if (sc->synhw.capExtended && sc->synhw.capMiddle) 1859321060Swulf evdev_support_key(evdev_a, BTN_MIDDLE); 1860321060Swulf } 1861321060Swulf if (sc->synhw.capExtended && sc->synhw.capFourButtons) { 1862321060Swulf evdev_support_key(evdev_a, BTN_BACK); 1863321060Swulf evdev_support_key(evdev_a, BTN_FORWARD); 1864321060Swulf } 1865321060Swulf if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) 1866321060Swulf for (i = 0; i < sc->synhw.nExtendedButtons; i++) 1867321060Swulf evdev_support_key(evdev_a, BTN_0 + i); 1868321060Swulf 1869321060Swulf error = evdev_register_mtx(evdev_a, &Giant); 1870343158Swulf if (!error && (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX)) { 1871321060Swulf guest_model = sc->tpinfo.sysctl_tree != NULL ? 1872321060Swulf MOUSE_MODEL_TRACKPOINT : MOUSE_MODEL_GENERIC; 1873321060Swulf error = psm_register(dev, guest_model); 1874321060Swulf } 1875321060Swulf if (error) 1876321060Swulf evdev_free(evdev_a); 1877321060Swulf else 1878321060Swulf sc->evdev_a = evdev_a; 1879321060Swulf return (error); 1880321060Swulf} 1881321060Swulf 1882321060Swulfstatic int 1883321060Swulfpsm_register_elantech(device_t dev) 1884321060Swulf{ 1885321060Swulf struct psm_softc *sc = device_get_softc(dev); 1886321060Swulf const uint16_t elantech_absinfo[][4] = { 1887321060Swulf { ABS_X, 0, sc->elanhw.sizex, 1888321060Swulf sc->elanhw.dpmmx }, 1889321060Swulf { ABS_Y, 0, sc->elanhw.sizey, 1890321060Swulf sc->elanhw.dpmmy }, 1891321060Swulf { ABS_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 }, 1892321060Swulf { ABS_TOOL_WIDTH, 0, ELANTECH_FINGER_MAX_W, 0 }, 1893321060Swulf { ABS_MT_SLOT, 0, ELANTECH_MAX_FINGERS - 1, 0 }, 1894321060Swulf { ABS_MT_TRACKING_ID, -1, ELANTECH_MAX_FINGERS - 1, 0 }, 1895321060Swulf { ABS_MT_POSITION_X, 0, sc->elanhw.sizex, 1896321060Swulf sc->elanhw.dpmmx }, 1897321060Swulf { ABS_MT_POSITION_Y, 0, sc->elanhw.sizey, 1898321060Swulf sc->elanhw.dpmmy }, 1899321060Swulf { ABS_MT_PRESSURE, 0, ELANTECH_FINGER_MAX_P, 0 }, 1900321060Swulf { ABS_MT_TOUCH_MAJOR, 0, ELANTECH_FINGER_MAX_W * 1901321060Swulf sc->elanhw.dptracex, 0 }, 1902321060Swulf ABSINFO_END, 1903321060Swulf }; 1904321060Swulf struct evdev_dev *evdev_a; 1905321060Swulf int error; 1906321060Swulf 1907321060Swulf evdev_a = evdev_alloc(); 1908321060Swulf evdev_set_name(evdev_a, PS2_MOUSE_ELANTECH_NAME); 1909321060Swulf evdev_set_phys(evdev_a, device_get_nameunit(dev)); 1910321060Swulf evdev_set_id(evdev_a, BUS_I8042, PS2_MOUSE_VENDOR, 1911321060Swulf PS2_MOUSE_ELANTECH_PRODUCT, 0); 1912321060Swulf evdev_set_methods(evdev_a, sc, &psm_ev_methods_a); 1913321060Swulf 1914321060Swulf evdev_support_event(evdev_a, EV_SYN); 1915321060Swulf evdev_support_event(evdev_a, EV_KEY); 1916321060Swulf evdev_support_event(evdev_a, EV_ABS); 1917321060Swulf evdev_support_prop(evdev_a, INPUT_PROP_POINTER); 1918321060Swulf if (sc->elanhw.issemimt) 1919321060Swulf evdev_support_prop(evdev_a, INPUT_PROP_SEMI_MT); 1920321060Swulf if (sc->elanhw.isclickpad) 1921321060Swulf evdev_support_prop(evdev_a, INPUT_PROP_BUTTONPAD); 1922321060Swulf evdev_support_key(evdev_a, BTN_TOUCH); 1923321060Swulf evdev_support_nfingers(evdev_a, ELANTECH_MAX_FINGERS); 1924321060Swulf evdev_support_key(evdev_a, BTN_LEFT); 1925321060Swulf if (!sc->elanhw.isclickpad) 1926321060Swulf evdev_support_key(evdev_a, BTN_RIGHT); 1927321060Swulf psm_support_abs_bulk(evdev_a, elantech_absinfo); 1928321060Swulf 1929321060Swulf error = evdev_register_mtx(evdev_a, &Giant); 1930321060Swulf if (!error && sc->elanhw.hastrackpoint) 1931321060Swulf error = psm_register(dev, MOUSE_MODEL_ELANTECH); 1932321060Swulf if (error) 1933321060Swulf evdev_free(evdev_a); 1934321060Swulf else 1935321060Swulf sc->evdev_a = evdev_a; 1936321060Swulf return (error); 1937321060Swulf} 1938321060Swulf#endif 1939321060Swulf 1940321060Swulfstatic int 194141016Sdfrpsmattach(device_t dev) 194241016Sdfr{ 1943357666Skevans struct make_dev_args mda; 1944178019Sjkim int unit = device_get_unit(dev); 1945178019Sjkim struct psm_softc *sc = device_get_softc(dev); 1946178019Sjkim int error; 1947178019Sjkim int rid; 194841016Sdfr 1949178019Sjkim /* Setup initial state */ 1950178019Sjkim sc->state = PSM_VALID; 1951240211Sjhb callout_init(&sc->callout, 0); 1952240211Sjhb callout_init(&sc->softcallout, 0); 195341016Sdfr 1954178019Sjkim /* Setup our interrupt handler */ 1955178019Sjkim rid = KBDC_RID_AUX; 1956216491Sjhb sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 1957178019Sjkim if (sc->intr == NULL) 1958178019Sjkim return (ENXIO); 1959178019Sjkim error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, NULL, psmintr, sc, 1960178019Sjkim &sc->ih); 1961357666Skevans if (error) 1962357666Skevans goto out; 196358230Syokota 1964178019Sjkim /* Done */ 1965357666Skevans make_dev_args_init(&mda); 1966357666Skevans mda.mda_devsw = &psm_cdevsw; 1967357666Skevans mda.mda_mode = 0666; 1968357666Skevans mda.mda_si_drv1 = sc; 196941016Sdfr 1970357666Skevans if ((error = make_dev_s(&mda, &sc->dev, "psm%d", unit)) != 0) 1971357666Skevans goto out; 1972357666Skevans if ((error = make_dev_s(&mda, &sc->bdev, "bpsm%d", unit)) != 0) 1973357666Skevans goto out; 1974357666Skevans 1975321060Swulf#ifdef EVDEV_SUPPORT 1976321060Swulf switch (sc->hw.model) { 1977321060Swulf case MOUSE_MODEL_SYNAPTICS: 1978321060Swulf error = psm_register_synaptics(dev); 1979321060Swulf break; 1980321060Swulf 1981321060Swulf case MOUSE_MODEL_ELANTECH: 1982321060Swulf error = psm_register_elantech(dev); 1983321060Swulf break; 1984321060Swulf 1985321060Swulf default: 1986321060Swulf error = psm_register(dev, sc->hw.model); 1987321060Swulf } 1988321060Swulf 1989321060Swulf if (error) 1990357666Skevans goto out; 1991321060Swulf#endif 1992321060Swulf 1993233580Sjkim /* Some touchpad devices need full reinitialization after suspend. */ 1994233580Sjkim switch (sc->hw.model) { 1995233580Sjkim case MOUSE_MODEL_SYNAPTICS: 1996233580Sjkim case MOUSE_MODEL_GLIDEPOINT: 1997233580Sjkim case MOUSE_MODEL_VERSAPAD: 1998307576Sgonzo case MOUSE_MODEL_ELANTECH: 1999233580Sjkim sc->config |= PSM_CONFIG_INITAFTERSUSPEND; 2000233580Sjkim break; 2001233580Sjkim default: 2002248478Sjkim if (sc->synhw.infoMajor >= 4 || sc->tphw > 0) 2003233580Sjkim sc->config |= PSM_CONFIG_INITAFTERSUSPEND; 2004233580Sjkim break; 2005233580Sjkim } 2006233580Sjkim 2007307576Sgonzo /* Elantech trackpad`s sync bit differs from touchpad`s one */ 2008307576Sgonzo if (sc->hw.model == MOUSE_MODEL_ELANTECH && 2009328481Sdumbbell (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) { 2010307576Sgonzo sc->config |= PSM_CONFIG_NOCHECKSYNC; 2011328481Sdumbbell sc->flags &= ~PSM_NEED_SYNCBITS; 2012328481Sdumbbell } 2013307576Sgonzo 2014178019Sjkim if (!verbose) 2015178019Sjkim printf("psm%d: model %s, device ID %d\n", 2016178019Sjkim unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 2017178019Sjkim else { 2018178019Sjkim printf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 2019178019Sjkim unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff, 2020178019Sjkim sc->hw.hwid >> 8, sc->hw.buttons); 2021178019Sjkim printf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 2022178019Sjkim unit, sc->config, sc->flags, sc->mode.packetsize); 2023328481Sdumbbell printf("psm%d: syncmask:%02x, syncbits:%02x%s\n", 2024328481Sdumbbell unit, sc->mode.syncmask[0], sc->mode.syncmask[1], 2025328481Sdumbbell sc->config & PSM_CONFIG_NOCHECKSYNC ? " (sync not checked)" : ""); 2026178019Sjkim } 202741016Sdfr 2028178019Sjkim if (bootverbose) 2029178019Sjkim --verbose; 203041016Sdfr 2031357666Skevansout: 2032357666Skevans if (error != 0) { 2033357666Skevans bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 2034357666Skevans if (sc->dev != NULL) 2035357666Skevans destroy_dev(sc->dev); 2036357666Skevans if (sc->bdev != NULL) 2037357666Skevans destroy_dev(sc->bdev); 2038357666Skevans } 2039357666Skevans return (error); 204041016Sdfr} 204141016Sdfr 204241016Sdfrstatic int 204358230Syokotapsmdetach(device_t dev) 204458230Syokota{ 2045178019Sjkim struct psm_softc *sc; 2046178019Sjkim int rid; 204758230Syokota 2048178019Sjkim sc = device_get_softc(dev); 2049178019Sjkim if (sc->state & PSM_OPEN) 2050178019Sjkim return (EBUSY); 205158230Syokota 2052321060Swulf#ifdef EVDEV_SUPPORT 2053321060Swulf evdev_free(sc->evdev_r); 2054321060Swulf evdev_free(sc->evdev_a); 2055321060Swulf#endif 2056321060Swulf 2057178019Sjkim rid = KBDC_RID_AUX; 2058178019Sjkim bus_teardown_intr(dev, sc->intr, sc->ih); 2059178019Sjkim bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 206058230Syokota 2061178019Sjkim destroy_dev(sc->dev); 2062178019Sjkim destroy_dev(sc->bdev); 206358230Syokota 2064240211Sjhb callout_drain(&sc->callout); 2065240211Sjhb callout_drain(&sc->softcallout); 2066240211Sjhb 2067178019Sjkim return (0); 206858230Syokota} 206958230Syokota 2070321060Swulf#ifdef EVDEV_SUPPORT 207158230Syokotastatic int 2072321060Swulfpsm_ev_open_r(struct evdev_dev *evdev, void *ev_softc) 207341016Sdfr{ 2074321060Swulf struct psm_softc *sc = (struct psm_softc *)ev_softc; 2075321060Swulf int err = 0; 2076321060Swulf 2077321060Swulf /* Get device data */ 2078321060Swulf if ((sc->state & PSM_VALID) == 0) { 2079321060Swulf /* the device is no longer valid/functioning */ 2080321060Swulf return (ENXIO); 2081321060Swulf } 2082321060Swulf 2083321060Swulf if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_A))) 2084321060Swulf err = psmopen(sc); 2085321060Swulf 2086321060Swulf if (err == 0) 2087321060Swulf sc->state |= PSM_EV_OPEN_R; 2088321060Swulf 2089321060Swulf return (err); 2090321060Swulf} 2091321060Swulf 2092321060Swulfstatic void 2093321060Swulfpsm_ev_close_r(struct evdev_dev *evdev, void *ev_softc) 2094321060Swulf{ 2095321060Swulf struct psm_softc *sc = (struct psm_softc *)ev_softc; 2096321060Swulf 2097321060Swulf sc->state &= ~PSM_EV_OPEN_R; 2098321060Swulf 2099321060Swulf if (sc->state & (PSM_OPEN | PSM_EV_OPEN_A)) 2100321060Swulf return; 2101321060Swulf 2102321060Swulf if (sc->state & PSM_VALID) 2103321060Swulf psmclose(sc); 2104321060Swulf} 2105321060Swulf 2106321060Swulfstatic int 2107321060Swulfpsm_ev_open_a(struct evdev_dev *evdev, void *ev_softc) 2108321060Swulf{ 2109321060Swulf struct psm_softc *sc = (struct psm_softc *)ev_softc; 2110321060Swulf int err = 0; 2111321060Swulf 2112321060Swulf /* Get device data */ 2113321060Swulf if ((sc->state & PSM_VALID) == 0) { 2114321060Swulf /* the device is no longer valid/functioning */ 2115321060Swulf return (ENXIO); 2116321060Swulf } 2117321060Swulf 2118321060Swulf if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R))) 2119321060Swulf err = psmopen(sc); 2120321060Swulf 2121321060Swulf if (err == 0) 2122321060Swulf sc->state |= PSM_EV_OPEN_A; 2123321060Swulf 2124321060Swulf return (err); 2125321060Swulf} 2126321060Swulf 2127321060Swulfstatic void 2128321060Swulfpsm_ev_close_a(struct evdev_dev *evdev, void *ev_softc) 2129321060Swulf{ 2130321060Swulf struct psm_softc *sc = (struct psm_softc *)ev_softc; 2131321060Swulf 2132321060Swulf sc->state &= ~PSM_EV_OPEN_A; 2133321060Swulf 2134321060Swulf if (sc->state & (PSM_OPEN | PSM_EV_OPEN_R)) 2135321060Swulf return; 2136321060Swulf 2137321060Swulf if (sc->state & PSM_VALID) 2138321060Swulf psmclose(sc); 2139321060Swulf} 2140321060Swulf#endif 2141321060Swulf 2142321060Swulfstatic int 2143321060Swulfpsm_cdev_open(struct cdev *dev, int flag, int fmt, struct thread *td) 2144321060Swulf{ 2145178019Sjkim struct psm_softc *sc; 2146321060Swulf int err = 0; 214741016Sdfr 2148178019Sjkim /* Get device data */ 2149212355Sed sc = dev->si_drv1; 2150178019Sjkim if ((sc == NULL) || (sc->state & PSM_VALID) == 0) { 2151178019Sjkim /* the device is no longer valid/functioning */ 2152178019Sjkim return (ENXIO); 2153178019Sjkim } 215441016Sdfr 2155178019Sjkim /* Disallow multiple opens */ 2156178019Sjkim if (sc->state & PSM_OPEN) 2157178019Sjkim return (EBUSY); 215841016Sdfr 2159212355Sed device_busy(devclass_get_device(psm_devclass, sc->unit)); 216041016Sdfr 2161321060Swulf#ifdef EVDEV_SUPPORT 2162321060Swulf /* Already opened by evdev */ 2163321060Swulf if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A))) 2164321060Swulf#endif 2165321060Swulf err = psmopen(sc); 2166321060Swulf 2167321060Swulf if (err == 0) 2168321060Swulf sc->state |= PSM_OPEN; 2169321060Swulf else 2170321060Swulf device_unbusy(devclass_get_device(psm_devclass, sc->unit)); 2171321060Swulf 2172321060Swulf return (err); 2173321060Swulf} 2174321060Swulf 2175321060Swulfstatic int 2176321060Swulfpsm_cdev_close(struct cdev *dev, int flag, int fmt, struct thread *td) 2177321060Swulf{ 2178321060Swulf struct psm_softc *sc; 2179321060Swulf int err = 0; 2180321060Swulf 2181321060Swulf /* Get device data */ 2182321060Swulf sc = dev->si_drv1; 2183321060Swulf if ((sc == NULL) || (sc->state & PSM_VALID) == 0) { 2184321060Swulf /* the device is no longer valid/functioning */ 2185321060Swulf return (ENXIO); 2186321060Swulf } 2187321060Swulf 2188321060Swulf#ifdef EVDEV_SUPPORT 2189321060Swulf /* Still opened by evdev */ 2190321060Swulf if (!(sc->state & (PSM_EV_OPEN_R | PSM_EV_OPEN_A))) 2191321060Swulf#endif 2192321060Swulf err = psmclose(sc); 2193321060Swulf 2194321060Swulf if (err == 0) { 2195321060Swulf sc->state &= ~PSM_OPEN; 2196321060Swulf /* clean up and sigio requests */ 2197321060Swulf if (sc->async != NULL) { 2198321060Swulf funsetown(&sc->async); 2199321060Swulf sc->async = NULL; 2200321060Swulf } 2201321060Swulf device_unbusy(devclass_get_device(psm_devclass, sc->unit)); 2202321060Swulf } 2203321060Swulf 2204321060Swulf return (err); 2205321060Swulf} 2206321060Swulf 2207321060Swulfstatic int 2208321060Swulfpsmopen(struct psm_softc *sc) 2209321060Swulf{ 2210321060Swulf int command_byte; 2211321060Swulf int err; 2212321060Swulf int s; 2213321060Swulf 2214178019Sjkim /* Initialize state */ 2215178019Sjkim sc->mode.level = sc->dflt_mode.level; 2216178019Sjkim sc->mode.protocol = sc->dflt_mode.protocol; 2217178019Sjkim sc->watchdog = FALSE; 2218189870Srnoland sc->async = NULL; 221941016Sdfr 2220178019Sjkim /* flush the event queue */ 2221178019Sjkim sc->queue.count = 0; 2222178019Sjkim sc->queue.head = 0; 2223178019Sjkim sc->queue.tail = 0; 2224178019Sjkim sc->status.flags = 0; 2225178019Sjkim sc->status.button = 0; 2226178019Sjkim sc->status.obutton = 0; 2227178019Sjkim sc->status.dx = 0; 2228178019Sjkim sc->status.dy = 0; 2229178019Sjkim sc->status.dz = 0; 2230178019Sjkim sc->button = 0; 2231178019Sjkim sc->pqueue_start = 0; 2232178019Sjkim sc->pqueue_end = 0; 223341016Sdfr 2234178019Sjkim /* empty input buffer */ 2235178019Sjkim flushpackets(sc); 2236178019Sjkim sc->syncerrors = 0; 2237178019Sjkim sc->pkterrors = 0; 223841016Sdfr 2239178019Sjkim /* don't let timeout routines in the keyboard driver to poll the kbdc */ 2240178019Sjkim if (!kbdc_lock(sc->kbdc, TRUE)) 2241178019Sjkim return (EIO); 224241016Sdfr 2243178019Sjkim /* save the current controller command byte */ 2244178019Sjkim s = spltty(); 2245178019Sjkim command_byte = get_controller_command_byte(sc->kbdc); 224641016Sdfr 2247178019Sjkim /* enable the aux port and temporalily disable the keyboard */ 2248178019Sjkim if (command_byte == -1 || !set_controller_command_byte(sc->kbdc, 224941016Sdfr kbdc_get_device_mask(sc->kbdc), 2250178019Sjkim KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | 2251178019Sjkim KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 2252178019Sjkim /* CONTROLLER ERROR; do you know how to get out of this? */ 2253178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2254178019Sjkim splx(s); 2255178019Sjkim log(LOG_ERR, 2256212355Sed "psm%d: unable to set the command byte (psmopen).\n", 2257212355Sed sc->unit); 2258178019Sjkim return (EIO); 2259178019Sjkim } 2260178019Sjkim /* 2261178019Sjkim * Now that the keyboard controller is told not to generate 2262178019Sjkim * the keyboard and mouse interrupts, call `splx()' to allow 2263178019Sjkim * the other tty interrupts. The clock interrupt may also occur, 2264178019Sjkim * but timeout routines will be blocked by the poll flag set 2265178019Sjkim * via `kbdc_lock()' 2266178019Sjkim */ 226741016Sdfr splx(s); 226841016Sdfr 2269178019Sjkim /* enable the mouse device */ 2270178019Sjkim err = doopen(sc, command_byte); 2271178019Sjkim 2272178019Sjkim /* done */ 2273178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2274178019Sjkim return (err); 227541016Sdfr} 227641016Sdfr 227741016Sdfrstatic int 2278321060Swulfpsmclose(struct psm_softc *sc) 227941016Sdfr{ 2280178019Sjkim int stat[3]; 2281178019Sjkim int command_byte; 2282178019Sjkim int s; 228341016Sdfr 2284178019Sjkim /* don't let timeout routines in the keyboard driver to poll the kbdc */ 2285178019Sjkim if (!kbdc_lock(sc->kbdc, TRUE)) 2286178019Sjkim return (EIO); 228741016Sdfr 2288178019Sjkim /* save the current controller command byte */ 2289178019Sjkim s = spltty(); 2290178019Sjkim command_byte = get_controller_command_byte(sc->kbdc); 2291178019Sjkim if (command_byte == -1) { 2292178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2293178019Sjkim splx(s); 2294178019Sjkim return (EIO); 2295178019Sjkim } 229641016Sdfr 2297178019Sjkim /* disable the aux interrupt and temporalily disable the keyboard */ 2298178019Sjkim if (!set_controller_command_byte(sc->kbdc, 229941016Sdfr kbdc_get_device_mask(sc->kbdc), 2300178019Sjkim KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | 2301178019Sjkim KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 2302178019Sjkim log(LOG_ERR, 2303212355Sed "psm%d: failed to disable the aux int (psmclose).\n", 2304212355Sed sc->unit); 2305178019Sjkim /* CONTROLLER ERROR; 2306178019Sjkim * NOTE: we shall force our way through. Because the only 2307178019Sjkim * ill effect we shall see is that we may not be able 2308178019Sjkim * to read ACK from the mouse, and it doesn't matter much 2309178019Sjkim * so long as the mouse will accept the DISABLE command. 2310178019Sjkim */ 2311178019Sjkim } 2312178019Sjkim splx(s); 231341016Sdfr 2314178019Sjkim /* stop the watchdog timer */ 2315240211Sjhb callout_stop(&sc->callout); 231658230Syokota 2317178019Sjkim /* remove anything left in the output buffer */ 2318178019Sjkim empty_aux_buffer(sc->kbdc, 10); 231941016Sdfr 2320178019Sjkim /* disable the aux device, port and interrupt */ 2321178019Sjkim if (sc->state & PSM_VALID) { 2322178019Sjkim if (!disable_aux_dev(sc->kbdc)) { 2323178019Sjkim /* MOUSE ERROR; 2324178019Sjkim * NOTE: we don't return (error) and continue, 2325178019Sjkim * pretending we have successfully disabled the device. 2326178019Sjkim * It's OK because the interrupt routine will discard 2327178019Sjkim * any data from the mouse hereafter. 2328178019Sjkim */ 2329178019Sjkim log(LOG_ERR, 2330178019Sjkim "psm%d: failed to disable the device (psmclose).\n", 2331212355Sed sc->unit); 2332178019Sjkim } 233341016Sdfr 2334178019Sjkim if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 2335178019Sjkim log(LOG_DEBUG, 2336212355Sed "psm%d: failed to get status (psmclose).\n", 2337212355Sed sc->unit); 2338178019Sjkim } 233941016Sdfr 2340178019Sjkim if (!set_controller_command_byte(sc->kbdc, 234141016Sdfr kbdc_get_device_mask(sc->kbdc), 2342178019Sjkim (command_byte & KBD_KBD_CONTROL_BITS) | 2343178019Sjkim KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 2344178019Sjkim /* 2345178019Sjkim * CONTROLLER ERROR; 2346178019Sjkim * we shall ignore this error; see the above comment. 2347178019Sjkim */ 2348178019Sjkim log(LOG_ERR, 2349178019Sjkim "psm%d: failed to disable the aux port (psmclose).\n", 2350212355Sed sc->unit); 2351178019Sjkim } 235241016Sdfr 2353178019Sjkim /* remove anything left in the output buffer */ 2354178019Sjkim empty_aux_buffer(sc->kbdc, 10); 235541016Sdfr 2356178019Sjkim /* close is almost always successful */ 2357178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2358178019Sjkim return (0); 235941016Sdfr} 236041016Sdfr 236141016Sdfrstatic int 2362178019Sjkimtame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, 2363178019Sjkim u_char *buf) 236441016Sdfr{ 2365178019Sjkim static u_char butmapps2[8] = { 2366178019Sjkim 0, 2367178019Sjkim MOUSE_PS2_BUTTON1DOWN, 2368178019Sjkim MOUSE_PS2_BUTTON2DOWN, 2369178019Sjkim MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 2370178019Sjkim MOUSE_PS2_BUTTON3DOWN, 2371178019Sjkim MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 2372178019Sjkim MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 2373178019Sjkim MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | 2374178019Sjkim MOUSE_PS2_BUTTON3DOWN, 2375178019Sjkim }; 2376178019Sjkim static u_char butmapmsc[8] = { 2377178019Sjkim MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | 2378178019Sjkim MOUSE_MSC_BUTTON3UP, 2379178019Sjkim MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 2380178019Sjkim MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 2381178019Sjkim MOUSE_MSC_BUTTON3UP, 2382178019Sjkim MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 2383178019Sjkim MOUSE_MSC_BUTTON2UP, 2384178019Sjkim MOUSE_MSC_BUTTON1UP, 2385178019Sjkim 0, 2386178019Sjkim }; 2387178019Sjkim int mapped; 2388178019Sjkim int i; 238941016Sdfr 2390178019Sjkim if (sc->mode.level == PSM_LEVEL_BASE) { 2391178019Sjkim mapped = status->button & ~MOUSE_BUTTON4DOWN; 2392178019Sjkim if (status->button & MOUSE_BUTTON4DOWN) 2393178019Sjkim mapped |= MOUSE_BUTTON1DOWN; 2394178019Sjkim status->button = mapped; 2395178019Sjkim buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 2396178019Sjkim i = imax(imin(status->dx, 255), -256); 2397178019Sjkim if (i < 0) 2398178019Sjkim buf[0] |= MOUSE_PS2_XNEG; 2399178019Sjkim buf[1] = i; 2400178019Sjkim i = imax(imin(status->dy, 255), -256); 2401178019Sjkim if (i < 0) 2402178019Sjkim buf[0] |= MOUSE_PS2_YNEG; 2403178019Sjkim buf[2] = i; 2404178019Sjkim return (MOUSE_PS2_PACKETSIZE); 2405178019Sjkim } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 2406178019Sjkim buf[0] = MOUSE_MSC_SYNC | 2407178019Sjkim butmapmsc[status->button & MOUSE_STDBUTTONS]; 2408178019Sjkim i = imax(imin(status->dx, 255), -256); 2409178019Sjkim buf[1] = i >> 1; 2410178019Sjkim buf[3] = i - buf[1]; 2411178019Sjkim i = imax(imin(status->dy, 255), -256); 2412178019Sjkim buf[2] = i >> 1; 2413178019Sjkim buf[4] = i - buf[2]; 2414178019Sjkim i = imax(imin(status->dz, 127), -128); 2415178019Sjkim buf[5] = (i >> 1) & 0x7f; 2416178019Sjkim buf[6] = (i - (i >> 1)) & 0x7f; 2417178019Sjkim buf[7] = (~status->button >> 3) & 0x7f; 2418178019Sjkim return (MOUSE_SYS_PACKETSIZE); 2419178019Sjkim } 2420178019Sjkim return (pb->inputbytes); 242141016Sdfr} 242241016Sdfr 242341016Sdfrstatic int 2424130585Sphkpsmread(struct cdev *dev, struct uio *uio, int flag) 242541016Sdfr{ 2426212355Sed struct psm_softc *sc = dev->si_drv1; 2427178019Sjkim u_char buf[PSM_SMALLBUFSIZE]; 2428178019Sjkim int error = 0; 2429178019Sjkim int s; 2430178019Sjkim int l; 243141016Sdfr 2432178019Sjkim if ((sc->state & PSM_VALID) == 0) 2433178019Sjkim return (EIO); 243441016Sdfr 2435298955Spfg /* block until mouse activity occurred */ 2436178019Sjkim s = spltty(); 2437178019Sjkim while (sc->queue.count <= 0) { 2438212355Sed if (dev != sc->bdev) { 2439178019Sjkim splx(s); 2440178019Sjkim return (EWOULDBLOCK); 2441178019Sjkim } 2442178019Sjkim sc->state |= PSM_ASLP; 2443178019Sjkim error = tsleep(sc, PZERO | PCATCH, "psmrea", 0); 2444178019Sjkim sc->state &= ~PSM_ASLP; 2445178019Sjkim if (error) { 2446178019Sjkim splx(s); 2447178019Sjkim return (error); 2448178019Sjkim } else if ((sc->state & PSM_VALID) == 0) { 2449178019Sjkim /* the device disappeared! */ 2450178019Sjkim splx(s); 2451178019Sjkim return (EIO); 2452178019Sjkim } 245341016Sdfr } 2454178019Sjkim splx(s); 245541016Sdfr 2456178019Sjkim /* copy data to the user land */ 2457178019Sjkim while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 2458178019Sjkim s = spltty(); 2459178019Sjkim l = imin(sc->queue.count, uio->uio_resid); 2460178019Sjkim if (l > sizeof(buf)) 2461178019Sjkim l = sizeof(buf); 2462178019Sjkim if (l > sizeof(sc->queue.buf) - sc->queue.head) { 2463178019Sjkim bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 2464178019Sjkim sizeof(sc->queue.buf) - sc->queue.head); 2465178019Sjkim bcopy(&sc->queue.buf[0], 2466178019Sjkim &buf[sizeof(sc->queue.buf) - sc->queue.head], 2467178019Sjkim l - (sizeof(sc->queue.buf) - sc->queue.head)); 2468178019Sjkim } else 2469178019Sjkim bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 2470178019Sjkim sc->queue.count -= l; 2471178019Sjkim sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 2472178019Sjkim splx(s); 2473178019Sjkim error = uiomove(buf, l, uio); 2474178019Sjkim if (error) 2475178019Sjkim break; 247641016Sdfr } 247741016Sdfr 2478178019Sjkim return (error); 247941016Sdfr} 248041016Sdfr 248141016Sdfrstatic int 248241016Sdfrblock_mouse_data(struct psm_softc *sc, int *c) 248341016Sdfr{ 2484178019Sjkim int s; 248541016Sdfr 2486178019Sjkim if (!kbdc_lock(sc->kbdc, TRUE)) 2487178019Sjkim return (EIO); 248841016Sdfr 2489178019Sjkim s = spltty(); 2490178019Sjkim *c = get_controller_command_byte(sc->kbdc); 2491178019Sjkim if ((*c == -1) || !set_controller_command_byte(sc->kbdc, 249241016Sdfr kbdc_get_device_mask(sc->kbdc), 2493178019Sjkim KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT | 2494178019Sjkim KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 2495178019Sjkim /* this is CONTROLLER ERROR */ 2496178019Sjkim splx(s); 2497178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2498178019Sjkim return (EIO); 2499178019Sjkim } 2500178019Sjkim 2501178019Sjkim /* 2502178019Sjkim * The device may be in the middle of status data transmission. 2503178019Sjkim * The transmission will be interrupted, thus, incomplete status 2504178019Sjkim * data must be discarded. Although the aux interrupt is disabled 2505178019Sjkim * at the keyboard controller level, at most one aux interrupt 2506178019Sjkim * may have already been pending and a data byte is in the 2507178019Sjkim * output buffer; throw it away. Note that the second argument 2508178019Sjkim * to `empty_aux_buffer()' is zero, so that the call will just 2509178019Sjkim * flush the internal queue. 2510178019Sjkim * `psmintr()' will be invoked after `splx()' if an interrupt is 2511178019Sjkim * pending; it will see no data and returns immediately. 2512178019Sjkim */ 2513178019Sjkim empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 2514178019Sjkim read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 2515178019Sjkim flushpackets(sc); 251641016Sdfr splx(s); 251741016Sdfr 2518178019Sjkim return (0); 251941016Sdfr} 252041016Sdfr 2521123442Salfredstatic void 2522123442Salfreddropqueue(struct psm_softc *sc) 2523123442Salfred{ 2524123442Salfred 2525178019Sjkim sc->queue.count = 0; 2526178019Sjkim sc->queue.head = 0; 2527178019Sjkim sc->queue.tail = 0; 2528123442Salfred if ((sc->state & PSM_SOFTARMED) != 0) { 2529123442Salfred sc->state &= ~PSM_SOFTARMED; 2530240211Sjhb callout_stop(&sc->softcallout); 2531123442Salfred } 2532123442Salfred sc->pqueue_start = sc->pqueue_end; 2533123442Salfred} 2534123442Salfred 2535123442Salfredstatic void 2536123442Salfredflushpackets(struct psm_softc *sc) 2537123442Salfred{ 2538123442Salfred 2539123442Salfred dropqueue(sc); 2540123442Salfred bzero(&sc->pqueue, sizeof(sc->pqueue)); 2541123442Salfred} 2542123442Salfred 254341016Sdfrstatic int 254441016Sdfrunblock_mouse_data(struct psm_softc *sc, int c) 254541016Sdfr{ 2546178019Sjkim int error = 0; 254741016Sdfr 2548178019Sjkim /* 2549178019Sjkim * We may have seen a part of status data during `set_mouse_XXX()'. 2550178019Sjkim * they have been queued; flush it. 2551178019Sjkim */ 2552178019Sjkim empty_aux_buffer(sc->kbdc, 0); 255341016Sdfr 2554178019Sjkim /* restore ports and interrupt */ 2555178019Sjkim if (!set_controller_command_byte(sc->kbdc, 2556178019Sjkim kbdc_get_device_mask(sc->kbdc), 255741016Sdfr c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) { 2558178019Sjkim /* 2559178019Sjkim * CONTROLLER ERROR; this is serious, we may have 2560178019Sjkim * been left with the inaccessible keyboard and 2561178019Sjkim * the disabled mouse interrupt. 2562178019Sjkim */ 2563178019Sjkim error = EIO; 2564178019Sjkim } 256541016Sdfr 2566178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2567178019Sjkim return (error); 256841016Sdfr} 256941016Sdfr 257041016Sdfrstatic int 2571178017Sjkimpsmwrite(struct cdev *dev, struct uio *uio, int flag) 2572178017Sjkim{ 2573212355Sed struct psm_softc *sc = dev->si_drv1; 2574178019Sjkim u_char buf[PSM_SMALLBUFSIZE]; 2575178019Sjkim int error = 0, i, l; 2576178017Sjkim 2577178019Sjkim if ((sc->state & PSM_VALID) == 0) 2578178019Sjkim return (EIO); 2579178017Sjkim 2580178019Sjkim if (sc->mode.level < PSM_LEVEL_NATIVE) 2581178019Sjkim return (ENODEV); 2582178017Sjkim 2583178019Sjkim /* copy data from the user land */ 2584178019Sjkim while (uio->uio_resid > 0) { 2585178019Sjkim l = imin(PSM_SMALLBUFSIZE, uio->uio_resid); 2586178019Sjkim error = uiomove(buf, l, uio); 2587178019Sjkim if (error) 2588178019Sjkim break; 2589178019Sjkim for (i = 0; i < l; i++) { 2590178019Sjkim VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i])); 2591178019Sjkim if (!write_aux_command(sc->kbdc, buf[i])) { 2592178019Sjkim VLOG(2, (LOG_DEBUG, 2593178019Sjkim "psm: cmd 0x%x failed.\n", buf[i])); 2594178019Sjkim return (reinitialize(sc, FALSE)); 2595178019Sjkim } 2596178019Sjkim } 2597178017Sjkim } 2598178017Sjkim 2599178019Sjkim return (error); 2600178017Sjkim} 2601178017Sjkim 2602178017Sjkimstatic int 2603178019Sjkimpsmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2604178019Sjkim struct thread *td) 260541016Sdfr{ 2606212355Sed struct psm_softc *sc = dev->si_drv1; 2607178019Sjkim mousemode_t mode; 2608178019Sjkim mousestatus_t status; 260941016Sdfr#if (defined(MOUSE_GETVARS)) 2610178019Sjkim mousevar_t *var; 261141016Sdfr#endif 2612178019Sjkim mousedata_t *data; 2613178019Sjkim int stat[3]; 2614178019Sjkim int command_byte; 2615178019Sjkim int error = 0; 2616178019Sjkim int s; 261741016Sdfr 2618178019Sjkim /* Perform IOCTL command */ 2619178019Sjkim switch (cmd) { 262041016Sdfr 2621178019Sjkim case OLD_MOUSE_GETHWINFO: 2622178019Sjkim s = spltty(); 2623178019Sjkim ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 2624178019Sjkim ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 2625178019Sjkim ((old_mousehw_t *)addr)->type = sc->hw.type; 2626178019Sjkim ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 2627178019Sjkim splx(s); 2628178019Sjkim break; 262941016Sdfr 2630178019Sjkim case MOUSE_GETHWINFO: 2631178019Sjkim s = spltty(); 2632178019Sjkim *(mousehw_t *)addr = sc->hw; 2633178019Sjkim if (sc->mode.level == PSM_LEVEL_BASE) 2634178019Sjkim ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 2635178019Sjkim splx(s); 2636178019Sjkim break; 263741016Sdfr 2638178019Sjkim case MOUSE_SYN_GETHWINFO: 2639178019Sjkim s = spltty(); 2640233580Sjkim if (sc->synhw.infoMajor >= 4) 2641178019Sjkim *(synapticshw_t *)addr = sc->synhw; 2642178019Sjkim else 2643178019Sjkim error = EINVAL; 2644178019Sjkim splx(s); 2645178019Sjkim break; 2646132865Snjl 2647178019Sjkim case OLD_MOUSE_GETMODE: 2648178019Sjkim s = spltty(); 2649178019Sjkim switch (sc->mode.level) { 2650178019Sjkim case PSM_LEVEL_BASE: 2651178019Sjkim ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2652178019Sjkim break; 2653178019Sjkim case PSM_LEVEL_STANDARD: 2654178019Sjkim ((old_mousemode_t *)addr)->protocol = 2655178019Sjkim MOUSE_PROTO_SYSMOUSE; 2656178019Sjkim break; 2657178019Sjkim case PSM_LEVEL_NATIVE: 2658178019Sjkim ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2659178019Sjkim break; 2660178019Sjkim } 2661178019Sjkim ((old_mousemode_t *)addr)->rate = sc->mode.rate; 2662178019Sjkim ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 2663178019Sjkim ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 2664178019Sjkim splx(s); 2665178019Sjkim break; 266641016Sdfr 2667178019Sjkim case MOUSE_GETMODE: 2668178019Sjkim s = spltty(); 2669178019Sjkim *(mousemode_t *)addr = sc->mode; 2670178019Sjkim if ((sc->flags & PSM_NEED_SYNCBITS) != 0) { 2671178019Sjkim ((mousemode_t *)addr)->syncmask[0] = 0; 2672178019Sjkim ((mousemode_t *)addr)->syncmask[1] = 0; 2673178019Sjkim } 2674178019Sjkim ((mousemode_t *)addr)->resolution = 2675178019Sjkim MOUSE_RES_LOW - sc->mode.resolution; 2676178019Sjkim switch (sc->mode.level) { 2677178019Sjkim case PSM_LEVEL_BASE: 2678178019Sjkim ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2679178019Sjkim ((mousemode_t *)addr)->packetsize = 2680178019Sjkim MOUSE_PS2_PACKETSIZE; 2681178019Sjkim break; 2682178019Sjkim case PSM_LEVEL_STANDARD: 2683178019Sjkim ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 2684178019Sjkim ((mousemode_t *)addr)->packetsize = 2685178019Sjkim MOUSE_SYS_PACKETSIZE; 2686178019Sjkim ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 2687178019Sjkim ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 2688178019Sjkim break; 2689178019Sjkim case PSM_LEVEL_NATIVE: 2690178019Sjkim /* FIXME: this isn't quite correct... XXX */ 2691178019Sjkim ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2692178019Sjkim break; 2693178019Sjkim } 2694178019Sjkim splx(s); 2695178019Sjkim break; 269641016Sdfr 2697178019Sjkim case OLD_MOUSE_SETMODE: 2698178019Sjkim case MOUSE_SETMODE: 2699178019Sjkim if (cmd == OLD_MOUSE_SETMODE) { 2700178019Sjkim mode.rate = ((old_mousemode_t *)addr)->rate; 2701178019Sjkim /* 2702178019Sjkim * resolution old I/F new I/F 2703178019Sjkim * default 0 0 2704178019Sjkim * low 1 -2 2705178019Sjkim * medium low 2 -3 2706178019Sjkim * medium high 3 -4 2707178019Sjkim * high 4 -5 2708178019Sjkim */ 2709178019Sjkim if (((old_mousemode_t *)addr)->resolution > 0) 2710178019Sjkim mode.resolution = 2711178019Sjkim -((old_mousemode_t *)addr)->resolution - 1; 2712178019Sjkim else 2713178019Sjkim mode.resolution = 0; 2714178019Sjkim mode.accelfactor = 2715178019Sjkim ((old_mousemode_t *)addr)->accelfactor; 2716178019Sjkim mode.level = -1; 2717178019Sjkim } else 2718178019Sjkim mode = *(mousemode_t *)addr; 271941016Sdfr 2720178019Sjkim /* adjust and validate parameters. */ 2721178019Sjkim if (mode.rate > UCHAR_MAX) 2722178019Sjkim return (EINVAL); 2723178019Sjkim if (mode.rate == 0) 2724178019Sjkim mode.rate = sc->dflt_mode.rate; 2725178019Sjkim else if (mode.rate == -1) 2726178019Sjkim /* don't change the current setting */ 2727178019Sjkim ; 2728178019Sjkim else if (mode.rate < 0) 2729178019Sjkim return (EINVAL); 2730178019Sjkim if (mode.resolution >= UCHAR_MAX) 2731178019Sjkim return (EINVAL); 2732178019Sjkim if (mode.resolution >= 200) 2733178019Sjkim mode.resolution = MOUSE_RES_HIGH; 2734178019Sjkim else if (mode.resolution >= 100) 2735178019Sjkim mode.resolution = MOUSE_RES_MEDIUMHIGH; 2736178019Sjkim else if (mode.resolution >= 50) 2737178019Sjkim mode.resolution = MOUSE_RES_MEDIUMLOW; 2738178019Sjkim else if (mode.resolution > 0) 2739178019Sjkim mode.resolution = MOUSE_RES_LOW; 2740178019Sjkim if (mode.resolution == MOUSE_RES_DEFAULT) 2741178019Sjkim mode.resolution = sc->dflt_mode.resolution; 2742178019Sjkim else if (mode.resolution == -1) 2743178019Sjkim /* don't change the current setting */ 2744178019Sjkim ; 2745178019Sjkim else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 2746178019Sjkim mode.resolution = MOUSE_RES_LOW - mode.resolution; 2747178019Sjkim if (mode.level == -1) 2748178019Sjkim /* don't change the current setting */ 2749178019Sjkim mode.level = sc->mode.level; 2750178019Sjkim else if ((mode.level < PSM_LEVEL_MIN) || 2751178019Sjkim (mode.level > PSM_LEVEL_MAX)) 2752178019Sjkim return (EINVAL); 2753178019Sjkim if (mode.accelfactor == -1) 2754178019Sjkim /* don't change the current setting */ 2755178019Sjkim mode.accelfactor = sc->mode.accelfactor; 2756178019Sjkim else if (mode.accelfactor < 0) 2757178019Sjkim return (EINVAL); 275841016Sdfr 2759178019Sjkim /* don't allow anybody to poll the keyboard controller */ 2760178019Sjkim error = block_mouse_data(sc, &command_byte); 2761178019Sjkim if (error) 2762178019Sjkim return (error); 276341016Sdfr 2764178019Sjkim /* set mouse parameters */ 2765178019Sjkim if (mode.rate > 0) 2766178019Sjkim mode.rate = set_mouse_sampling_rate(sc->kbdc, 2767178019Sjkim mode.rate); 2768178019Sjkim if (mode.resolution >= 0) 2769178019Sjkim mode.resolution = 2770178019Sjkim set_mouse_resolution(sc->kbdc, mode.resolution); 2771178019Sjkim set_mouse_scaling(sc->kbdc, 1); 2772178019Sjkim get_mouse_status(sc->kbdc, stat, 0, 3); 277341016Sdfr 2774178019Sjkim s = spltty(); 2775178019Sjkim sc->mode.rate = mode.rate; 2776178019Sjkim sc->mode.resolution = mode.resolution; 2777178019Sjkim sc->mode.accelfactor = mode.accelfactor; 2778178019Sjkim sc->mode.level = mode.level; 2779178019Sjkim splx(s); 278041016Sdfr 2781178019Sjkim unblock_mouse_data(sc, command_byte); 2782178019Sjkim break; 278341016Sdfr 2784178019Sjkim case MOUSE_GETLEVEL: 2785178019Sjkim *(int *)addr = sc->mode.level; 2786178019Sjkim break; 278741016Sdfr 2788178019Sjkim case MOUSE_SETLEVEL: 2789178019Sjkim if ((*(int *)addr < PSM_LEVEL_MIN) || 2790178019Sjkim (*(int *)addr > PSM_LEVEL_MAX)) 2791178019Sjkim return (EINVAL); 2792178019Sjkim sc->mode.level = *(int *)addr; 2793178019Sjkim break; 279441016Sdfr 2795178019Sjkim case MOUSE_GETSTATUS: 2796178019Sjkim s = spltty(); 2797178019Sjkim status = sc->status; 2798178019Sjkim sc->status.flags = 0; 2799178019Sjkim sc->status.obutton = sc->status.button; 2800178019Sjkim sc->status.button = 0; 2801178019Sjkim sc->status.dx = 0; 2802178019Sjkim sc->status.dy = 0; 2803178019Sjkim sc->status.dz = 0; 2804178019Sjkim splx(s); 2805178019Sjkim *(mousestatus_t *)addr = status; 2806178019Sjkim break; 280741016Sdfr 280841016Sdfr#if (defined(MOUSE_GETVARS)) 2809178019Sjkim case MOUSE_GETVARS: 2810178019Sjkim var = (mousevar_t *)addr; 2811178019Sjkim bzero(var, sizeof(*var)); 2812178019Sjkim s = spltty(); 2813178019Sjkim var->var[0] = MOUSE_VARS_PS2_SIG; 2814178019Sjkim var->var[1] = sc->config; 2815178019Sjkim var->var[2] = sc->flags; 2816178019Sjkim splx(s); 2817178019Sjkim break; 281841016Sdfr 2819178019Sjkim case MOUSE_SETVARS: 2820178019Sjkim return (ENODEV); 282141016Sdfr#endif /* MOUSE_GETVARS */ 282241016Sdfr 2823178019Sjkim case MOUSE_READSTATE: 2824178019Sjkim case MOUSE_READDATA: 2825178019Sjkim data = (mousedata_t *)addr; 2826178019Sjkim if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 2827178019Sjkim return (EINVAL); 282841016Sdfr 2829178019Sjkim error = block_mouse_data(sc, &command_byte); 2830178019Sjkim if (error) 2831178019Sjkim return (error); 2832178019Sjkim if ((data->len = get_mouse_status(sc->kbdc, data->buf, 2833178019Sjkim (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 2834178019Sjkim error = EIO; 2835178019Sjkim unblock_mouse_data(sc, command_byte); 2836178019Sjkim break; 283741016Sdfr 283841016Sdfr#if (defined(MOUSE_SETRESOLUTION)) 2839178019Sjkim case MOUSE_SETRESOLUTION: 2840178019Sjkim mode.resolution = *(int *)addr; 2841178019Sjkim if (mode.resolution >= UCHAR_MAX) 2842178019Sjkim return (EINVAL); 2843178019Sjkim else if (mode.resolution >= 200) 2844178019Sjkim mode.resolution = MOUSE_RES_HIGH; 2845178019Sjkim else if (mode.resolution >= 100) 2846178019Sjkim mode.resolution = MOUSE_RES_MEDIUMHIGH; 2847178019Sjkim else if (mode.resolution >= 50) 2848178019Sjkim mode.resolution = MOUSE_RES_MEDIUMLOW; 2849178019Sjkim else if (mode.resolution > 0) 2850178019Sjkim mode.resolution = MOUSE_RES_LOW; 2851178019Sjkim if (mode.resolution == MOUSE_RES_DEFAULT) 2852178019Sjkim mode.resolution = sc->dflt_mode.resolution; 2853178019Sjkim else if (mode.resolution == -1) 2854178019Sjkim mode.resolution = sc->mode.resolution; 2855178019Sjkim else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 2856178019Sjkim mode.resolution = MOUSE_RES_LOW - mode.resolution; 285741016Sdfr 2858178019Sjkim error = block_mouse_data(sc, &command_byte); 2859178019Sjkim if (error) 2860178019Sjkim return (error); 2861178019Sjkim sc->mode.resolution = 2862178019Sjkim set_mouse_resolution(sc->kbdc, mode.resolution); 2863178019Sjkim if (sc->mode.resolution != mode.resolution) 2864178019Sjkim error = EIO; 2865178019Sjkim unblock_mouse_data(sc, command_byte); 2866178019Sjkim break; 286741016Sdfr#endif /* MOUSE_SETRESOLUTION */ 286841016Sdfr 286941016Sdfr#if (defined(MOUSE_SETRATE)) 2870178019Sjkim case MOUSE_SETRATE: 2871178019Sjkim mode.rate = *(int *)addr; 2872178019Sjkim if (mode.rate > UCHAR_MAX) 2873178019Sjkim return (EINVAL); 2874178019Sjkim if (mode.rate == 0) 2875178019Sjkim mode.rate = sc->dflt_mode.rate; 2876178019Sjkim else if (mode.rate < 0) 2877178019Sjkim mode.rate = sc->mode.rate; 287841016Sdfr 2879178019Sjkim error = block_mouse_data(sc, &command_byte); 2880178019Sjkim if (error) 2881178019Sjkim return (error); 2882178019Sjkim sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 2883178019Sjkim if (sc->mode.rate != mode.rate) 2884178019Sjkim error = EIO; 2885178019Sjkim unblock_mouse_data(sc, command_byte); 2886178019Sjkim break; 288741016Sdfr#endif /* MOUSE_SETRATE */ 288841016Sdfr 288941016Sdfr#if (defined(MOUSE_SETSCALING)) 2890178019Sjkim case MOUSE_SETSCALING: 2891178019Sjkim if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 2892178019Sjkim return (EINVAL); 289341016Sdfr 2894178019Sjkim error = block_mouse_data(sc, &command_byte); 2895178019Sjkim if (error) 2896178019Sjkim return (error); 2897178019Sjkim if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 2898178019Sjkim error = EIO; 2899178019Sjkim unblock_mouse_data(sc, command_byte); 2900178019Sjkim break; 290141016Sdfr#endif /* MOUSE_SETSCALING */ 290241016Sdfr 290341016Sdfr#if (defined(MOUSE_GETHWID)) 2904178019Sjkim case MOUSE_GETHWID: 2905178019Sjkim error = block_mouse_data(sc, &command_byte); 2906178019Sjkim if (error) 2907178019Sjkim return (error); 2908178019Sjkim sc->hw.hwid &= ~0x00ff; 2909178019Sjkim sc->hw.hwid |= get_aux_id(sc->kbdc); 2910178019Sjkim *(int *)addr = sc->hw.hwid & 0x00ff; 2911178019Sjkim unblock_mouse_data(sc, command_byte); 2912178019Sjkim break; 291341016Sdfr#endif /* MOUSE_GETHWID */ 291441016Sdfr 2915189870Srnoland case FIONBIO: 2916189870Srnoland case FIOASYNC: 2917189870Srnoland break; 2918189870Srnoland case FIOSETOWN: 2919189870Srnoland error = fsetown(*(int *)addr, &sc->async); 2920189870Srnoland break; 2921189870Srnoland case FIOGETOWN: 2922189870Srnoland *(int *) addr = fgetown(&sc->async); 2923189870Srnoland break; 2924178019Sjkim default: 2925178019Sjkim return (ENOTTY); 2926178019Sjkim } 292741016Sdfr 2928178019Sjkim return (error); 292941016Sdfr} 293041016Sdfr 293141016Sdfrstatic void 293258230Syokotapsmtimeout(void *arg) 293358230Syokota{ 2934178019Sjkim struct psm_softc *sc; 2935178019Sjkim int s; 293658230Syokota 2937178019Sjkim sc = (struct psm_softc *)arg; 2938178019Sjkim s = spltty(); 2939178019Sjkim if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 2940331171Seadler VLOG(6, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit)); 2941178019Sjkim psmintr(sc); 2942178019Sjkim kbdc_lock(sc->kbdc, FALSE); 2943178019Sjkim } 2944178019Sjkim sc->watchdog = TRUE; 2945178019Sjkim splx(s); 2946240211Sjhb callout_reset(&sc->callout, hz, psmtimeout, sc); 294758230Syokota} 294858230Syokota 2949139628Sphilip/* Add all sysctls under the debug.psm and hw.psm nodes */ 2950227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse"); 2951227309Sedstatic SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse"); 2952123442Salfred 2953307576SgonzoSYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RWTUN, &verbose, 0, 2954180818Strhodes "Verbosity level"); 2955123442Salfred 2956139628Sphilipstatic int psmhz = 20; 2957180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0, 2958180818Strhodes "Frequency of the softcallout (in hz)"); 2959123442Salfredstatic int psmerrsecs = 2; 2960180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0, 2961180818Strhodes "Number of seconds during which packets will dropped after a sync error"); 2962123442Salfredstatic int psmerrusecs = 0; 2963180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0, 2964180818Strhodes "Microseconds to add to psmerrsecs"); 2965123442Salfredstatic int psmsecs = 0; 2966180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0, 2967180818Strhodes "Max number of seconds between soft interrupts"); 2968123442Salfredstatic int psmusecs = 500000; 2969180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0, 2970180818Strhodes "Microseconds to add to psmsecs"); 2971139628Sphilipstatic int pkterrthresh = 2; 2972180818StrhodesSYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0, 2973180818Strhodes "Number of error packets allowed before reinitializing the mouse"); 2974123442Salfred 2975307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RWTUN, &tap_enabled, 0, 2976200674Sdumbbell "Enable tap and drag gestures"); 2977139628Sphilipstatic int tap_threshold = PSM_TAP_THRESHOLD; 2978180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0, 2979180818Strhodes "Button tap threshold"); 2980139628Sphilipstatic int tap_timeout = PSM_TAP_TIMEOUT; 2981180818StrhodesSYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0, 2982180818Strhodes "Tap timeout for touchpads"); 2983134405Sgibbs 2984307576Sgonzo/* Tunables */ 2985307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RDTUN, 2986307576Sgonzo &synaptics_support, 0, "Enable support for Synaptics touchpads"); 2987307576Sgonzo 2988307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RDTUN, 2989307576Sgonzo &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint"); 2990307576Sgonzo 2991307576SgonzoSYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RDTUN, 2992307576Sgonzo &elantech_support, 0, "Enable support for Elantech touchpads"); 2993307576Sgonzo 299458230Syokotastatic void 299541016Sdfrpsmintr(void *arg) 299641016Sdfr{ 2997178019Sjkim struct psm_softc *sc = arg; 2998178019Sjkim struct timeval now; 2999178019Sjkim int c; 3000178019Sjkim packetbuf_t *pb; 300141016Sdfr 3002343158Swulf if (aux_mux_is_enabled(sc->kbdc)) 3003343158Swulf VLOG(2, (LOG_DEBUG, "psmintr: active multiplexing mode is not " 3004343158Swulf "supported!\n")); 3005123442Salfred 3006178019Sjkim /* read until there is nothing to read */ 3007178019Sjkim while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 3008178019Sjkim pb = &sc->pqueue[sc->pqueue_end]; 300984880Syokota 3010178019Sjkim /* discard the byte if the device is not open */ 3011321060Swulf if (!(sc->state & (PSM_OPEN | PSM_EV_OPEN_R | PSM_EV_OPEN_A))) 3012178019Sjkim continue; 301341016Sdfr 3014178019Sjkim getmicrouptime(&now); 3015178019Sjkim if ((pb->inputbytes > 0) && 3016178019Sjkim timevalcmp(&now, &sc->inputtimeout, >)) { 3017178019Sjkim VLOG(3, (LOG_DEBUG, "psmintr: delay too long; " 3018178019Sjkim "resetting byte count\n")); 3019178019Sjkim pb->inputbytes = 0; 3020178019Sjkim sc->syncerrors = 0; 3021178019Sjkim sc->pkterrors = 0; 3022178019Sjkim } 3023178019Sjkim sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000; 3024178019Sjkim sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000; 3025178019Sjkim timevaladd(&sc->inputtimeout, &now); 302641016Sdfr 3027178019Sjkim pb->ipacket[pb->inputbytes++] = c; 3028178017Sjkim 3029178019Sjkim if (sc->mode.level == PSM_LEVEL_NATIVE) { 3030178019Sjkim VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0])); 3031178019Sjkim sc->syncerrors = 0; 3032178019Sjkim sc->pkterrors = 0; 3033178019Sjkim goto next; 3034178019Sjkim } else { 3035178019Sjkim if (pb->inputbytes < sc->mode.packetsize) 3036178019Sjkim continue; 303741016Sdfr 3038178019Sjkim VLOG(4, (LOG_DEBUG, 3039178019Sjkim "psmintr: %02x %02x %02x %02x %02x %02x\n", 3040178019Sjkim pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 3041178019Sjkim pb->ipacket[3], pb->ipacket[4], pb->ipacket[5])); 3042178019Sjkim } 3043134405Sgibbs 3044178019Sjkim c = pb->ipacket[0]; 3045178019Sjkim 3046178019Sjkim if ((sc->flags & PSM_NEED_SYNCBITS) != 0) { 3047178019Sjkim sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]); 3048178019Sjkim sc->flags &= ~PSM_NEED_SYNCBITS; 3049178019Sjkim VLOG(2, (LOG_DEBUG, 3050178019Sjkim "psmintr: Sync bytes now %04x,%04x\n", 3051328484Sdumbbell sc->mode.syncmask[0], sc->mode.syncmask[1])); 3052328481Sdumbbell } else if ((sc->config & PSM_CONFIG_NOCHECKSYNC) == 0 && 3053328481Sdumbbell (c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 3054178019Sjkim VLOG(3, (LOG_DEBUG, "psmintr: out of sync " 3055178019Sjkim "(%04x != %04x) %d cmds since last error.\n", 3056178019Sjkim c & sc->mode.syncmask[0], sc->mode.syncmask[1], 3057178019Sjkim sc->cmdcount - sc->lasterr)); 3058178019Sjkim sc->lasterr = sc->cmdcount; 3059178019Sjkim /* 3060178019Sjkim * The sync byte test is a weak measure of packet 3061178019Sjkim * validity. Conservatively discard any input yet 3062178019Sjkim * to be seen by userland when we detect a sync 3063178019Sjkim * error since there is a good chance some of 3064178019Sjkim * the queued packets have undetected errors. 3065178019Sjkim */ 3066178019Sjkim dropqueue(sc); 3067178019Sjkim if (sc->syncerrors == 0) 3068178019Sjkim sc->pkterrors++; 3069178019Sjkim ++sc->syncerrors; 3070178019Sjkim sc->lastinputerr = now; 3071178019Sjkim if (sc->syncerrors >= sc->mode.packetsize * 2 || 3072178019Sjkim sc->pkterrors >= pkterrthresh) { 3073178019Sjkim /* 3074178019Sjkim * If we've failed to find a single sync byte 3075178019Sjkim * in 2 packets worth of data, or we've seen 3076178019Sjkim * persistent packet errors during the 3077178019Sjkim * validation period, reinitialize the mouse 3078178019Sjkim * in hopes of returning it to the expected 3079178019Sjkim * mode. 3080178019Sjkim */ 3081178019Sjkim VLOG(3, (LOG_DEBUG, 3082178019Sjkim "psmintr: reset the mouse.\n")); 3083178019Sjkim reinitialize(sc, TRUE); 3084178019Sjkim } else if (sc->syncerrors == sc->mode.packetsize) { 3085178019Sjkim /* 3086178019Sjkim * Try a soft reset after searching for a sync 3087178019Sjkim * byte through a packet length of bytes. 3088178019Sjkim */ 3089178019Sjkim VLOG(3, (LOG_DEBUG, 3090178019Sjkim "psmintr: re-enable the mouse.\n")); 3091178019Sjkim pb->inputbytes = 0; 3092178019Sjkim disable_aux_dev(sc->kbdc); 3093178019Sjkim enable_aux_dev(sc->kbdc); 3094178019Sjkim } else { 3095178019Sjkim VLOG(3, (LOG_DEBUG, 3096178019Sjkim "psmintr: discard a byte (%d)\n", 3097178019Sjkim sc->syncerrors)); 3098178019Sjkim pb->inputbytes--; 3099178019Sjkim bcopy(&pb->ipacket[1], &pb->ipacket[0], 3100178019Sjkim pb->inputbytes); 3101178019Sjkim } 3102178019Sjkim continue; 3103178019Sjkim } 3104178019Sjkim 3105134405Sgibbs /* 3106178019Sjkim * We have what appears to be a valid packet. 3107178019Sjkim * Reset the error counters. 3108134405Sgibbs */ 3109178019Sjkim sc->syncerrors = 0; 3110134405Sgibbs 3111134405Sgibbs /* 3112178019Sjkim * Drop even good packets if they occur within a timeout 3113178019Sjkim * period of a sync error. This allows the detection of 3114178019Sjkim * a change in the mouse's packet mode without exposing 3115178019Sjkim * erratic mouse behavior to the user. Some KVMs forget 3116178019Sjkim * enhanced mouse modes during switch events. 3117134405Sgibbs */ 3118178019Sjkim if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, 3119178019Sjkim &now)) { 3120178019Sjkim pb->inputbytes = 0; 3121178019Sjkim continue; 3122178019Sjkim } 3123134405Sgibbs 3124178019Sjkim /* 3125178019Sjkim * Now that we're out of the validation period, reset 3126178019Sjkim * the packet error count. 3127178019Sjkim */ 3128178019Sjkim sc->pkterrors = 0; 3129134405Sgibbs 3130178019Sjkim sc->cmdcount++; 3131178019Sjkimnext: 3132178019Sjkim if (++sc->pqueue_end >= PSM_PACKETQUEUE) 3133178019Sjkim sc->pqueue_end = 0; 3134178019Sjkim /* 3135178019Sjkim * If we've filled the queue then call the softintr ourselves, 3136178019Sjkim * otherwise schedule the interrupt for later. 3137178019Sjkim */ 3138178019Sjkim if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) || 3139178019Sjkim (sc->pqueue_end == sc->pqueue_start)) { 3140178019Sjkim if ((sc->state & PSM_SOFTARMED) != 0) { 3141178019Sjkim sc->state &= ~PSM_SOFTARMED; 3142240211Sjhb callout_stop(&sc->softcallout); 3143178019Sjkim } 3144178019Sjkim psmsoftintr(arg); 3145178019Sjkim } else if ((sc->state & PSM_SOFTARMED) == 0) { 3146178019Sjkim sc->state |= PSM_SOFTARMED; 3147240211Sjhb callout_reset(&sc->softcallout, 3148240211Sjhb psmhz < 1 ? 1 : (hz/psmhz), psmsoftintr, arg); 3149123442Salfred } 3150123442Salfred } 3151123442Salfred} 3152123442Salfred 3153123442Salfredstatic void 3154178019Sjkimproc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 3155178019Sjkim int *x, int *y, int *z) 3156123442Salfred{ 3157123442Salfred 3158178019Sjkim /* 3159240743Skevlo * PS2++ protocol packet 3160178019Sjkim * 3161178019Sjkim * b7 b6 b5 b4 b3 b2 b1 b0 3162178019Sjkim * byte 1: * 1 p3 p2 1 * * * 3163178019Sjkim * byte 2: c1 c2 p1 p0 d1 d0 1 0 3164178019Sjkim * 3165178019Sjkim * p3-p0: packet type 3166178019Sjkim * c1, c2: c1 & c2 == 1, if p2 == 0 3167178019Sjkim * c1 & c2 == 0, if p2 == 1 3168178019Sjkim * 3169178019Sjkim * packet type: 0 (device type) 3170178019Sjkim * See comments in enable_mmanplus() below. 3171178019Sjkim * 3172178019Sjkim * packet type: 1 (wheel data) 3173178019Sjkim * 3174178019Sjkim * b7 b6 b5 b4 b3 b2 b1 b0 3175178019Sjkim * byte 3: h * B5 B4 s d2 d1 d0 3176178019Sjkim * 3177178019Sjkim * h: 1, if horizontal roller data 3178178019Sjkim * 0, if vertical roller data 3179178019Sjkim * B4, B5: button 4 and 5 3180178019Sjkim * s: sign bit 3181178019Sjkim * d2-d0: roller data 3182178019Sjkim * 3183178019Sjkim * packet type: 2 (reserved) 318441016Sdfr */ 3185178019Sjkim if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) && 3186178019Sjkim (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) { 3187178019Sjkim /* 3188178019Sjkim * the extended data packet encodes button 3189178019Sjkim * and wheel events 3190178019Sjkim */ 3191123442Salfred switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) { 319248778Syokota case 1: 3193178019Sjkim /* wheel data packet */ 3194178019Sjkim *x = *y = 0; 3195178019Sjkim if (pb->ipacket[2] & 0x80) { 3196178019Sjkim /* XXX horizontal roller count - ignore it */ 3197178019Sjkim ; 3198178019Sjkim } else { 3199178019Sjkim /* vertical roller count */ 3200178019Sjkim *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ? 3201178019Sjkim (pb->ipacket[2] & 0x0f) - 16 : 3202178019Sjkim (pb->ipacket[2] & 0x0f); 3203178019Sjkim } 3204178019Sjkim ms->button |= (pb->ipacket[2] & 3205178019Sjkim MOUSE_PS2PLUS_BUTTON4DOWN) ? 3206178019Sjkim MOUSE_BUTTON4DOWN : 0; 3207178019Sjkim ms->button |= (pb->ipacket[2] & 3208178019Sjkim MOUSE_PS2PLUS_BUTTON5DOWN) ? 3209178019Sjkim MOUSE_BUTTON5DOWN : 0; 3210178019Sjkim break; 321148778Syokota case 2: 3212178019Sjkim /* 3213178019Sjkim * this packet type is reserved by 3214178019Sjkim * Logitech... 3215178019Sjkim */ 3216178019Sjkim /* 3217178019Sjkim * IBM ScrollPoint Mouse uses this 3218178019Sjkim * packet type to encode both vertical 3219178019Sjkim * and horizontal scroll movement. 3220178019Sjkim */ 3221178019Sjkim *x = *y = 0; 3222178019Sjkim /* horizontal count */ 3223178019Sjkim if (pb->ipacket[2] & 0x0f) 3224178019Sjkim *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? 3225178019Sjkim -2 : 2; 3226178019Sjkim /* vertical count */ 3227178019Sjkim if (pb->ipacket[2] & 0xf0) 3228178019Sjkim *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? 3229178019Sjkim -1 : 1; 3230178019Sjkim break; 323148778Syokota case 0: 3232178019Sjkim /* device type packet - shouldn't happen */ 3233178019Sjkim /* FALLTHROUGH */ 323448778Syokota default: 3235178019Sjkim *x = *y = 0; 3236178019Sjkim ms->button = ms->obutton; 3237178019Sjkim VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet " 3238178019Sjkim "type %d: 0x%02x 0x%02x 0x%02x\n", 3239178019Sjkim MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket), 3240178019Sjkim pb->ipacket[0], pb->ipacket[1], pb->ipacket[2])); 3241178019Sjkim break; 324248778Syokota } 3243178019Sjkim } else { 324441016Sdfr /* preserve button states */ 3245178019Sjkim ms->button |= ms->obutton & MOUSE_EXTBUTTONS; 3246178019Sjkim } 3247178019Sjkim} 324841016Sdfr 3249178019Sjkimstatic int 3250178019Sjkimproc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 3251178019Sjkim int *x, int *y, int *z) 3252178019Sjkim{ 3253178019Sjkim static int touchpad_buttons; 3254178019Sjkim static int guest_buttons; 3255349541Swulf static int ew_finger_count; 3256307576Sgonzo static finger_t f[PSM_FINGERS]; 3257349541Swulf int w, id, nfingers, palm, ewcode, extended_buttons, clickpad_pressed; 325841016Sdfr 3259307576Sgonzo extended_buttons = 0; 3260307576Sgonzo 3261255153Sdumbbell /* TouchPad PS/2 absolute mode message format with capFourButtons: 3262178019Sjkim * 3263178019Sjkim * Bits: 7 6 5 4 3 2 1 0 (LSB) 3264178019Sjkim * ------------------------------------------------ 3265178019Sjkim * ipacket[0]: 1 0 W3 W2 0 W1 R L 3266178019Sjkim * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8 3267178019Sjkim * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0 3268255153Sdumbbell * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L 3269178019Sjkim * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 3270178019Sjkim * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3271178019Sjkim * 3272178019Sjkim * Legend: 3273178019Sjkim * L: left physical mouse button 3274178019Sjkim * R: right physical mouse button 3275178019Sjkim * D: down button 3276178019Sjkim * U: up button 3277178019Sjkim * W: "wrist" value 3278178019Sjkim * X: x position 3279183888Sdumbbell * Y: y position 3280178019Sjkim * Z: pressure 3281178019Sjkim * 3282255153Sdumbbell * Without capFourButtons but with nExtendeButtons and/or capMiddle 3283255153Sdumbbell * 3284255153Sdumbbell * Bits: 7 6 5 4 3 2 1 0 (LSB) 3285255153Sdumbbell * ------------------------------------------------------ 3286255153Sdumbbell * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L 3287255153Sdumbbell * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1 3288255153Sdumbbell * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2 3289255153Sdumbbell * 3290255153Sdumbbell * Legend: 3291255153Sdumbbell * M: Middle physical mouse button 3292255153Sdumbbell * E: Extended mouse buttons reported instead of low bits of X and Y 3293255153Sdumbbell * b1-b8: Extended mouse buttons 3294255153Sdumbbell * Only ((nExtendedButtons + 1) >> 1) bits are used in packet 3295255153Sdumbbell * 4 and 5, for reading X and Y value they should be zeroed. 3296255153Sdumbbell * 3297178019Sjkim * Absolute reportable limits: 0 - 6143. 3298178019Sjkim * Typical bezel limits: 1472 - 5472. 3299178019Sjkim * Typical edge marings: 1632 - 5312. 3300178019Sjkim * 3301178019Sjkim * w = 3 Passthrough Packet 3302178019Sjkim * 3303178019Sjkim * Byte 2,5,6 == Byte 1,2,3 of "Guest" 3304178019Sjkim */ 330541016Sdfr 3306178019Sjkim if (!synaptics_support) 3307178019Sjkim return (0); 330841016Sdfr 3309178019Sjkim /* Sanity check for out of sync packets. */ 3310178019Sjkim if ((pb->ipacket[0] & 0xc8) != 0x80 || 3311178019Sjkim (pb->ipacket[3] & 0xc8) != 0xc0) 3312178019Sjkim return (-1); 331349965Syokota 3314183888Sdumbbell *x = *y = 0; 3315307576Sgonzo ms->button = ms->obutton; 331658230Syokota 3317183888Sdumbbell /* 3318183888Sdumbbell * Pressure value. 3319183888Sdumbbell * Interpretation: 3320183888Sdumbbell * z = 0 No finger contact 3321183888Sdumbbell * z = 10 Finger hovering near the pad 3322183888Sdumbbell * z = 30 Very light finger contact 3323183888Sdumbbell * z = 80 Normal finger contact 3324183888Sdumbbell * z = 110 Very heavy finger contact 3325183888Sdumbbell * z = 200 Finger lying flat on pad surface 3326183888Sdumbbell * z = 255 Maximum reportable Z 3327183888Sdumbbell */ 3328178019Sjkim *z = pb->ipacket[2]; 332958230Syokota 3330183888Sdumbbell /* 3331183888Sdumbbell * Finger width value 3332183888Sdumbbell * Interpretation: 3333183888Sdumbbell * w = 0 Two finger on the pad (capMultiFinger needed) 3334183888Sdumbbell * w = 1 Three or more fingers (capMultiFinger needed) 3335183888Sdumbbell * w = 2 Pen (instead of finger) (capPen needed) 3336183888Sdumbbell * w = 3 Reserved (passthrough?) 3337183888Sdumbbell * w = 4-7 Finger of normal width (capPalmDetect needed) 3338183888Sdumbbell * w = 8-14 Very wide finger or palm (capPalmDetect needed) 3339183888Sdumbbell * w = 15 Maximum reportable width (capPalmDetect needed) 3340183888Sdumbbell */ 3341183888Sdumbbell /* XXX Is checking capExtended enough? */ 3342178019Sjkim if (sc->synhw.capExtended) 3343133301Sphilip w = ((pb->ipacket[0] & 0x30) >> 2) | 3344133301Sphilip ((pb->ipacket[0] & 0x04) >> 1) | 3345133301Sphilip ((pb->ipacket[3] & 0x04) >> 2); 3346178019Sjkim else { 3347183888Sdumbbell /* Assume a finger of regular width. */ 3348133301Sphilip w = 4; 3349178019Sjkim } 3350133301Sphilip 3351307576Sgonzo switch (w) { 3352307576Sgonzo case 3: 3353307576Sgonzo /* 3354307576Sgonzo * Handle packets from the guest device. See: 3355307576Sgonzo * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1 3356307576Sgonzo */ 3357343158Swulf if (sc->synhw.capPassthrough || sc->muxport != PSM_NOMUX) { 3358307576Sgonzo *x = ((pb->ipacket[1] & 0x10) ? 3359307576Sgonzo pb->ipacket[4] - 256 : pb->ipacket[4]); 3360307576Sgonzo *y = ((pb->ipacket[1] & 0x20) ? 3361307576Sgonzo pb->ipacket[5] - 256 : pb->ipacket[5]); 3362307576Sgonzo *z = 0; 3363133868Sphilip 3364307576Sgonzo guest_buttons = 0; 3365307576Sgonzo if (pb->ipacket[1] & 0x01) 3366307576Sgonzo guest_buttons |= MOUSE_BUTTON1DOWN; 3367307576Sgonzo if (pb->ipacket[1] & 0x04) 3368307576Sgonzo guest_buttons |= MOUSE_BUTTON2DOWN; 3369307576Sgonzo if (pb->ipacket[1] & 0x02) 3370307576Sgonzo guest_buttons |= MOUSE_BUTTON3DOWN; 3371321060Swulf#ifdef EVDEV_SUPPORT 3372321060Swulf if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) { 3373321060Swulf evdev_push_rel(sc->evdev_r, REL_X, *x); 3374321060Swulf evdev_push_rel(sc->evdev_r, REL_Y, -*y); 3375321060Swulf evdev_push_mouse_btn(sc->evdev_r, 3376360617Swulf guest_buttons | sc->extended_buttons); 3377321060Swulf evdev_sync(sc->evdev_r); 3378321060Swulf } 3379321060Swulf#endif 3380307576Sgonzo ms->button = touchpad_buttons | guest_buttons | 3381307576Sgonzo sc->extended_buttons; 3382307576Sgonzo } 3383183888Sdumbbell goto SYNAPTICS_END; 3384133868Sphilip 3385307576Sgonzo case 2: 3386307576Sgonzo /* Handle Extended W mode packets */ 3387307576Sgonzo ewcode = (pb->ipacket[5] & 0xf0) >> 4; 3388307576Sgonzo#if PSM_FINGERS > 1 3389307576Sgonzo switch (ewcode) { 3390307576Sgonzo case 1: 3391307576Sgonzo /* Secondary finger */ 3392307576Sgonzo if (sc->synhw.capAdvancedGestures) 3393307576Sgonzo f[1] = (finger_t) { 3394307576Sgonzo .x = (((pb->ipacket[4] & 0x0f) << 8) | 3395307576Sgonzo pb->ipacket[1]) << 1, 3396307576Sgonzo .y = (((pb->ipacket[4] & 0xf0) << 4) | 3397307576Sgonzo pb->ipacket[2]) << 1, 3398307576Sgonzo .p = ((pb->ipacket[3] & 0x30) | 3399307576Sgonzo (pb->ipacket[5] & 0x0f)) << 1, 3400307576Sgonzo .w = PSM_FINGER_DEFAULT_W, 3401307576Sgonzo .flags = PSM_FINGER_FUZZY, 3402307576Sgonzo }; 3403307576Sgonzo else if (sc->synhw.capReportsV) 3404307576Sgonzo f[1] = (finger_t) { 3405307576Sgonzo .x = (((pb->ipacket[4] & 0x0f) << 8) | 3406307576Sgonzo (pb->ipacket[1] & 0xfe)) << 1, 3407307576Sgonzo .y = (((pb->ipacket[4] & 0xf0) << 4) | 3408307576Sgonzo (pb->ipacket[2] & 0xfe)) << 1, 3409307576Sgonzo .p = ((pb->ipacket[3] & 0x30) | 3410307576Sgonzo (pb->ipacket[5] & 0x0e)) << 1, 3411307576Sgonzo .w = (((pb->ipacket[5] & 0x01) << 2) | 3412307576Sgonzo ((pb->ipacket[2] & 0x01) << 1) | 3413307576Sgonzo (pb->ipacket[1] & 0x01)) + 8, 3414307576Sgonzo .flags = PSM_FINGER_FUZZY, 3415307576Sgonzo }; 3416349541Swulf break; 3417349541Swulf case 2: 3418349541Swulf ew_finger_count = pb->ipacket[1] & 0x0f; 3419307576Sgonzo default: 3420307576Sgonzo break; 3421307576Sgonzo } 3422307576Sgonzo#endif 3423281441Srpaulo goto SYNAPTICS_END; 3424307576Sgonzo 3425307576Sgonzo case 1: 3426349541Swulf if (sc->synhw.capReportsV && ew_finger_count > 3) { 3427349541Swulf nfingers = ew_finger_count; 3428349541Swulf break; 3429349541Swulf } 3430349541Swulf /* FALLTHROUGH */ 3431307576Sgonzo case 0: 3432307576Sgonzo nfingers = w + 2; 3433307576Sgonzo break; 3434307576Sgonzo 3435307576Sgonzo default: 3436307576Sgonzo nfingers = 1; 3437281441Srpaulo } 3438281441Srpaulo 3439307576Sgonzo if (sc->syninfo.touchpad_off) 3440307576Sgonzo goto SYNAPTICS_END; 3441307576Sgonzo 3442178019Sjkim /* Button presses */ 3443178019Sjkim touchpad_buttons = 0; 3444178019Sjkim if (pb->ipacket[0] & 0x01) 3445178019Sjkim touchpad_buttons |= MOUSE_BUTTON1DOWN; 3446178019Sjkim if (pb->ipacket[0] & 0x02) 3447178019Sjkim touchpad_buttons |= MOUSE_BUTTON3DOWN; 3448132865Snjl 3449178019Sjkim if (sc->synhw.capExtended && sc->synhw.capFourButtons) { 3450255153Sdumbbell if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01) 3451178019Sjkim touchpad_buttons |= MOUSE_BUTTON4DOWN; 3452255153Sdumbbell if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02) 3453178019Sjkim touchpad_buttons |= MOUSE_BUTTON5DOWN; 3454281704Srpaulo } else if (sc->synhw.capExtended && sc->synhw.capMiddle && 3455281704Srpaulo !sc->synhw.capClickPad) { 3456255153Sdumbbell /* Middle Button */ 3457255153Sdumbbell if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01) 3458255153Sdumbbell touchpad_buttons |= MOUSE_BUTTON2DOWN; 3459255153Sdumbbell } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) { 3460255153Sdumbbell /* Extended Buttons */ 3461255153Sdumbbell if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) { 3462255153Sdumbbell if (sc->syninfo.directional_scrolls) { 3463255153Sdumbbell if (pb->ipacket[4] & 0x01) 3464307576Sgonzo extended_buttons |= MOUSE_BUTTON4DOWN; 3465255153Sdumbbell if (pb->ipacket[5] & 0x01) 3466307576Sgonzo extended_buttons |= MOUSE_BUTTON5DOWN; 3467255153Sdumbbell if (pb->ipacket[4] & 0x02) 3468307576Sgonzo extended_buttons |= MOUSE_BUTTON6DOWN; 3469255153Sdumbbell if (pb->ipacket[5] & 0x02) 3470307576Sgonzo extended_buttons |= MOUSE_BUTTON7DOWN; 3471255153Sdumbbell } else { 3472281440Srpaulo if (pb->ipacket[4] & 0x01) 3473307576Sgonzo extended_buttons |= MOUSE_BUTTON1DOWN; 3474281440Srpaulo if (pb->ipacket[5] & 0x01) 3475307576Sgonzo extended_buttons |= MOUSE_BUTTON3DOWN; 3476281440Srpaulo if (pb->ipacket[4] & 0x02) 3477307576Sgonzo extended_buttons |= MOUSE_BUTTON2DOWN; 3478307576Sgonzo sc->extended_buttons = extended_buttons; 3479255153Sdumbbell } 3480132865Snjl 3481255153Sdumbbell /* 3482255153Sdumbbell * Zero out bits used by extended buttons to avoid 3483255153Sdumbbell * misinterpretation of the data absolute position. 3484255153Sdumbbell * 3485255153Sdumbbell * The bits represented by 3486255153Sdumbbell * 3487255153Sdumbbell * (nExtendedButtons + 1) >> 1 3488255153Sdumbbell * 3489255153Sdumbbell * will be masked out in both bytes. 3490255153Sdumbbell * The mask for n bits is computed with the formula 3491255153Sdumbbell * 3492255153Sdumbbell * (1 << n) - 1 3493255153Sdumbbell */ 3494255153Sdumbbell int maskedbits = 0; 3495255153Sdumbbell int mask = 0; 3496255153Sdumbbell maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1; 3497255153Sdumbbell mask = (1 << maskedbits) - 1; 3498321060Swulf#ifdef EVDEV_SUPPORT 3499321060Swulf int i; 3500321060Swulf if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) { 3501321060Swulf if (sc->synhw.capPassthrough) { 3502321060Swulf evdev_push_mouse_btn(sc->evdev_r, 3503321060Swulf extended_buttons); 3504321060Swulf evdev_sync(sc->evdev_r); 3505321060Swulf } 3506321060Swulf for (i = 0; i < maskedbits; i++) { 3507321060Swulf evdev_push_key(sc->evdev_a, 3508321060Swulf BTN_0 + i * 2, 3509321060Swulf pb->ipacket[4] & (1 << i)); 3510321060Swulf evdev_push_key(sc->evdev_a, 3511321060Swulf BTN_0 + i * 2 + 1, 3512321060Swulf pb->ipacket[5] & (1 << i)); 3513321060Swulf } 3514321060Swulf } 3515321060Swulf#endif 3516255153Sdumbbell pb->ipacket[4] &= ~(mask); 3517255153Sdumbbell pb->ipacket[5] &= ~(mask); 3518281440Srpaulo } else if (!sc->syninfo.directional_scrolls && 3519307576Sgonzo !sc->gesture.in_vscroll) { 3520281440Srpaulo /* 3521281440Srpaulo * Keep reporting MOUSE DOWN until we get a new packet 3522281440Srpaulo * indicating otherwise. 3523281440Srpaulo */ 3524307576Sgonzo extended_buttons |= sc->extended_buttons; 3525139982Sphilip } 3526178019Sjkim } 3527139982Sphilip 3528307576Sgonzo if (sc->synhw.capReportsV && nfingers > 1) 3529307576Sgonzo f[0] = (finger_t) { 3530307576Sgonzo .x = ((pb->ipacket[3] & 0x10) << 8) | 3531307576Sgonzo ((pb->ipacket[1] & 0x0f) << 8) | 3532307576Sgonzo (pb->ipacket[4] & 0xfd), 3533307576Sgonzo .y = ((pb->ipacket[3] & 0x20) << 7) | 3534307576Sgonzo ((pb->ipacket[1] & 0xf0) << 4) | 3535307576Sgonzo (pb->ipacket[5] & 0xfd), 3536307576Sgonzo .p = *z & 0xfe, 3537307576Sgonzo .w = (((pb->ipacket[2] & 0x01) << 2) | 3538307576Sgonzo (pb->ipacket[5] & 0x02) | 3539307576Sgonzo ((pb->ipacket[4] & 0x02) >> 1)) + 8, 3540307576Sgonzo .flags = PSM_FINGER_FUZZY, 3541307576Sgonzo }; 3542307576Sgonzo else 3543307576Sgonzo f[0] = (finger_t) { 3544307576Sgonzo .x = ((pb->ipacket[3] & 0x10) << 8) | 3545307576Sgonzo ((pb->ipacket[1] & 0x0f) << 8) | 3546307576Sgonzo pb->ipacket[4], 3547307576Sgonzo .y = ((pb->ipacket[3] & 0x20) << 7) | 3548307576Sgonzo ((pb->ipacket[1] & 0xf0) << 4) | 3549307576Sgonzo pb->ipacket[5], 3550307576Sgonzo .p = *z, 3551307576Sgonzo .w = w, 3552307576Sgonzo .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0, 3553307576Sgonzo }; 3554139982Sphilip 3555307576Sgonzo /* Ignore hovering and unmeasurable touches */ 3556307576Sgonzo if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2) 3557307576Sgonzo nfingers = 0; 3558307576Sgonzo 3559331170Seadler /* Handle ClickPad */ 3560331170Seadler if (sc->synhw.capClickPad) { 3561331170Seadler clickpad_pressed = (pb->ipacket[0] ^ pb->ipacket[3]) & 0x01; 3562331170Seadler if (sc->synhw.forcePad) { 3563331170Seadler /* 3564331170Seadler * Forcepads erroneously report button click if there 3565331170Seadler * are 2 or more fingers on the touchpad breaking 3566331170Seadler * multifinger gestures. To workaround this start 3567331170Seadler * reporting a click only after 4 consecutive single 3568331170Seadler * touch packets has been received. 3569331170Seadler * Skip these packets in case more contacts appear. 3570331170Seadler */ 3571331170Seadler switch (nfingers) { 3572331170Seadler case 0: 3573331170Seadler sc->fpcount = 0; 3574331170Seadler break; 3575331170Seadler case 1: 3576331170Seadler if (clickpad_pressed && sc->fpcount < INT_MAX) 3577331170Seadler ++sc->fpcount; 3578331170Seadler /* FALLTHROUGH */ 3579331170Seadler default: 3580331170Seadler if (!clickpad_pressed) 3581331170Seadler sc->fpcount = 0; 3582331170Seadler if (sc->fpcount >= sc->syninfo.window_min) 3583331170Seadler touchpad_buttons |= MOUSE_BUTTON1DOWN; 3584331170Seadler } 3585331170Seadler } else if (clickpad_pressed) 3586331170Seadler touchpad_buttons |= MOUSE_BUTTON1DOWN; 3587331170Seadler } 3588331170Seadler 3589307576Sgonzo for (id = 0; id < PSM_FINGERS; id++) 3590307576Sgonzo if (id >= nfingers) 3591307576Sgonzo PSM_FINGER_RESET(f[id]); 3592307576Sgonzo 3593321060Swulf#ifdef EVDEV_SUPPORT 3594321060Swulf if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) { 3595321060Swulf for (id = 0; id < PSM_FINGERS; id++) { 3596321060Swulf if (PSM_FINGER_IS_SET(f[id])) 3597321060Swulf psm_push_mt_finger(sc, id, &f[id]); 3598321060Swulf else 3599321060Swulf psm_release_mt_slot(sc->evdev_a, id); 3600321060Swulf } 3601321060Swulf evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0); 3602321060Swulf evdev_push_nfingers(sc->evdev_a, nfingers); 3603321060Swulf if (nfingers > 0) 3604321060Swulf psm_push_st_finger(sc, &f[0]); 3605321060Swulf else 3606321060Swulf evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0); 3607321060Swulf evdev_push_mouse_btn(sc->evdev_a, touchpad_buttons); 3608321060Swulf if (sc->synhw.capExtended && sc->synhw.capFourButtons) { 3609321060Swulf evdev_push_key(sc->evdev_a, BTN_FORWARD, 3610321060Swulf touchpad_buttons & MOUSE_BUTTON4DOWN); 3611321060Swulf evdev_push_key(sc->evdev_a, BTN_BACK, 3612321060Swulf touchpad_buttons & MOUSE_BUTTON5DOWN); 3613321060Swulf } 3614321060Swulf evdev_sync(sc->evdev_a); 3615321060Swulf } 3616321060Swulf#endif 3617321060Swulf 3618307576Sgonzo ms->button = touchpad_buttons; 3619307576Sgonzo 3620349541Swulf palm = psmpalmdetect(sc, &f[0], nfingers); 3621349541Swulf 3622349541Swulf /* Palm detection doesn't terminate the current action. */ 3623349541Swulf if (!palm) 3624349541Swulf psmgestures(sc, &f[0], nfingers, ms); 3625349541Swulf 3626318500Swulf for (id = 0; id < PSM_FINGERS; id++) 3627318500Swulf psmsmoother(sc, &f[id], id, ms, x, y); 3628318500Swulf 3629349541Swulf if (palm) { 3630318500Swulf *x = *y = *z = 0; 3631318500Swulf ms->button = ms->obutton; 3632318500Swulf return (0); 3633307576Sgonzo } 3634307576Sgonzo 3635307576Sgonzo ms->button |= extended_buttons | guest_buttons; 3636307576Sgonzo 3637307576SgonzoSYNAPTICS_END: 3638281440Srpaulo /* 3639307576Sgonzo * Use the extra buttons as a scrollwheel 3640307576Sgonzo * 3641307576Sgonzo * XXX X.Org uses the Z axis for vertical wheel only, 3642307576Sgonzo * whereas moused(8) understands special values to differ 3643307576Sgonzo * vertical and horizontal wheels. 3644307576Sgonzo * 3645307576Sgonzo * xf86-input-mouse needs therefore a small patch to 3646307576Sgonzo * understand these special values. Without it, the 3647307576Sgonzo * horizontal wheel acts as a vertical wheel in X.Org. 3648307576Sgonzo * 3649307576Sgonzo * That's why the horizontal wheel is disabled by 3650307576Sgonzo * default for now. 3651307576Sgonzo */ 3652307576Sgonzo if (ms->button & MOUSE_BUTTON4DOWN) 3653307576Sgonzo *z = -1; 3654307576Sgonzo else if (ms->button & MOUSE_BUTTON5DOWN) 3655307576Sgonzo *z = 1; 3656307576Sgonzo else if (ms->button & MOUSE_BUTTON6DOWN) 3657307576Sgonzo *z = -2; 3658307576Sgonzo else if (ms->button & MOUSE_BUTTON7DOWN) 3659307576Sgonzo *z = 2; 3660307576Sgonzo else 3661307576Sgonzo *z = 0; 3662307576Sgonzo ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN | 3663307576Sgonzo MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN); 3664307576Sgonzo 3665307576Sgonzo return (0); 3666307576Sgonzo} 3667307576Sgonzo 3668307576Sgonzostatic int 3669343158Swulfproc_synaptics_mux(struct psm_softc *sc, packetbuf_t *pb) 3670343158Swulf{ 3671343158Swulf int butt; 3672343158Swulf 3673343158Swulf /* 3674343158Swulf * Convert 3-byte interleaved mixture of Synaptics and generic mouse 3675343158Swulf * packets into plain 6-byte Synaptics packet protocol. 3676343158Swulf * While in hidden multiplexing mode KBC does some editing of the 3677343158Swulf * packet stream. It remembers the button bits from the last packet 3678343158Swulf * received from each device, and replaces the button bits of every 3679343158Swulf * packet with the logical OR of all devices��� most recent button bits. 3680343158Swulf * This button crosstalk should be filtered out as Synaptics and 3681343158Swulf * generic mouse encode middle button presses in a different way. 3682343158Swulf */ 3683343158Swulf switch (pb->ipacket[0] & 0xc0) { 3684343158Swulf case 0x80: /* First 3 bytes of Synaptics packet */ 3685343158Swulf bcopy(pb->ipacket, sc->muxsave, 3); 3686343158Swulf /* Compute middle mouse button supression timeout. */ 3687343158Swulf sc->muxmidtimeout.tv_sec = 0; 3688343158Swulf sc->muxmidtimeout.tv_usec = 50000; /* ~2-3 ints */ 3689343158Swulf timevaladd(&sc->muxmidtimeout, &sc->lastsoftintr); 3690343158Swulf return (1); 3691343158Swulf 3692343158Swulf case 0xc0: /* Second 3 bytes of Synaptics packet */ 3693343158Swulf /* Join two 3-bytes absolute packets */ 3694343158Swulf bcopy(pb->ipacket, pb->ipacket + 3, 3); 3695343158Swulf bcopy(sc->muxsave, pb->ipacket, 3); 3696343158Swulf /* Prefer trackpoint buttons over touchpad's */ 3697343158Swulf pb->ipacket[0] &= ~(0x08 | sc->muxmsbuttons); 3698343158Swulf pb->ipacket[3] &= ~(0x08 | sc->muxmsbuttons); 3699343158Swulf butt = (pb->ipacket[3] & 0x03) << 2 | (pb->ipacket[0] & 0x03); 3700343158Swulf /* Add hysteresis to remove spurious middle button events */ 3701343158Swulf if (butt != sc->muxtpbuttons && sc->fpcount < 1) { 3702343158Swulf pb->ipacket[0] &= 0xfc; 3703343158Swulf pb->ipacket[0] |= sc->muxtpbuttons & 0x03; 3704343158Swulf pb->ipacket[3] &= 0xfc; 3705343158Swulf pb->ipacket[3] |= sc->muxtpbuttons >> 2 & 0x03; 3706343158Swulf ++sc->fpcount; 3707343158Swulf } else { 3708343158Swulf sc->fpcount = 0; 3709343158Swulf sc->muxtpbuttons = butt; 3710343158Swulf } 3711343158Swulf /* Filter out impossible w induced by middle trackpoint btn */ 3712343158Swulf if (sc->synhw.capExtended && !sc->synhw.capPassthrough && 3713343158Swulf (pb->ipacket[0] & 0x34) == 0x04 && 3714343158Swulf (pb->ipacket[3] & 0x04) == 0x04) { 3715343158Swulf pb->ipacket[0] &= 0xfb; 3716343158Swulf pb->ipacket[3] &= 0xfb; 3717343158Swulf } 3718343158Swulf sc->muxsave[0] &= 0x30; 3719343158Swulf break; 3720343158Swulf 3721343158Swulf default: /* Generic mouse (Trackpoint) packet */ 3722343158Swulf /* Filter out middle button events induced by some w values */ 3723343158Swulf if (sc->muxmsbuttons & 0x03 || pb->ipacket[0] & 0x03 || 3724343158Swulf (timevalcmp(&sc->lastsoftintr, &sc->muxmidtimeout, <=) && 3725343158Swulf (sc->muxsave[0] & 0x30 || sc->muxsave[2] > 8))) 3726343158Swulf pb->ipacket[0] &= 0xfb; 3727343158Swulf sc->muxmsbuttons = pb->ipacket[0] & 0x07; 3728343158Swulf /* Convert to Synaptics pass-through protocol */ 3729343158Swulf pb->ipacket[4] = pb->ipacket[1]; 3730343158Swulf pb->ipacket[5] = pb->ipacket[2]; 3731343158Swulf pb->ipacket[1] = pb->ipacket[0]; 3732343158Swulf pb->ipacket[2] = 0; 3733343158Swulf pb->ipacket[0] = 0x84 | (sc->muxtpbuttons & 0x03); 3734343158Swulf pb->ipacket[3] = 0xc4 | (sc->muxtpbuttons >> 2 & 0x03); 3735343158Swulf } 3736343158Swulf 3737343158Swulf VLOG(4, (LOG_DEBUG, "synaptics: %02x %02x %02x %02x %02x %02x\n", 3738343158Swulf pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 3739343158Swulf pb->ipacket[3], pb->ipacket[4], pb->ipacket[5])); 3740343158Swulf 3741343158Swulf pb->inputbytes = MOUSE_SYNAPTICS_PACKETSIZE; 3742343158Swulf return (0); 3743343158Swulf} 3744343158Swulf 3745343158Swulfstatic int 3746307576Sgonzopsmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers) 3747307576Sgonzo{ 3748307576Sgonzo if (!( 3749318500Swulf ((sc->synhw.capMultiFinger || sc->synhw.capAdvancedGestures) && 3750318500Swulf !sc->synhw.capReportsV && nfingers > 1) || 3751318500Swulf (sc->synhw.capReportsV && nfingers > 2) || 3752307576Sgonzo (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) || 3753307576Sgonzo (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) || 3754307576Sgonzo (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) { 3755307576Sgonzo /* 3756307576Sgonzo * We consider the packet irrelevant for the current 3757307576Sgonzo * action when: 3758307576Sgonzo * - the width isn't comprised in: 3759307576Sgonzo * [1; max_width] 3760307576Sgonzo * - the pressure isn't comprised in: 3761307576Sgonzo * [min_pressure; max_pressure] 3762307576Sgonzo * - pen aren't supported but PSM_FINGER_IS_PEN is set 3763307576Sgonzo */ 3764318500Swulf VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f->w)); 3765307576Sgonzo return (1); 3766307576Sgonzo } 3767307576Sgonzo return (0); 3768307576Sgonzo} 3769307576Sgonzo 3770307576Sgonzostatic void 3771307576Sgonzopsmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers, 3772307576Sgonzo mousestatus_t *ms) 3773307576Sgonzo{ 3774307576Sgonzo smoother_t *smoother; 3775307576Sgonzo gesture_t *gest; 3776307576Sgonzo finger_t *f; 3777307576Sgonzo int y_ok, center_button, center_x, right_button, right_x, i; 3778307576Sgonzo 3779307576Sgonzo f = &fingers[0]; 3780307576Sgonzo smoother = &sc->smoother[0]; 3781307576Sgonzo gest = &sc->gesture; 3782307576Sgonzo 3783307576Sgonzo /* Find first active finger. */ 3784307576Sgonzo if (nfingers > 0) { 3785307576Sgonzo for (i = 0; i < PSM_FINGERS; i++) { 3786307576Sgonzo if (PSM_FINGER_IS_SET(fingers[i])) { 3787307576Sgonzo f = &fingers[i]; 3788307576Sgonzo smoother = &sc->smoother[i]; 3789307576Sgonzo break; 3790307576Sgonzo } 3791307576Sgonzo } 3792307576Sgonzo } 3793307576Sgonzo 3794307576Sgonzo /* 3795281440Srpaulo * Check pressure to detect a real wanted action on the 3796281440Srpaulo * touchpad. 3797281440Srpaulo */ 3798307576Sgonzo if (f->p >= sc->syninfo.min_pressure) { 3799307576Sgonzo int x0, y0; 3800307576Sgonzo int dxp, dyp; 3801307576Sgonzo int start_x, start_y; 3802307576Sgonzo int queue_len; 3803183888Sdumbbell int margin_top, margin_right, margin_bottom, margin_left; 3804183888Sdumbbell int window_min, window_max; 3805183888Sdumbbell int vscroll_hor_area, vscroll_ver_area; 3806281440Srpaulo int two_finger_scroll; 3807307576Sgonzo int max_x, max_y; 3808183888Sdumbbell 3809183888Sdumbbell /* Read sysctl. */ 3810183888Sdumbbell /* XXX Verify values? */ 3811183888Sdumbbell margin_top = sc->syninfo.margin_top; 3812183888Sdumbbell margin_right = sc->syninfo.margin_right; 3813183888Sdumbbell margin_bottom = sc->syninfo.margin_bottom; 3814183888Sdumbbell margin_left = sc->syninfo.margin_left; 3815183888Sdumbbell window_min = sc->syninfo.window_min; 3816183888Sdumbbell window_max = sc->syninfo.window_max; 3817183888Sdumbbell vscroll_hor_area = sc->syninfo.vscroll_hor_area; 3818183888Sdumbbell vscroll_ver_area = sc->syninfo.vscroll_ver_area; 3819281440Srpaulo two_finger_scroll = sc->syninfo.two_finger_scroll; 3820307576Sgonzo max_x = sc->syninfo.max_x; 3821307576Sgonzo max_y = sc->syninfo.max_y; 3822183888Sdumbbell 3823307576Sgonzo /* Read current absolute position. */ 3824307576Sgonzo x0 = f->x; 3825307576Sgonzo y0 = f->y; 3826292197Sandrew 3827307576Sgonzo /* 3828307576Sgonzo * Limit the coordinates to the specified margins because 3829307576Sgonzo * this area isn't very reliable. 3830307576Sgonzo */ 3831307576Sgonzo if (x0 <= margin_left) 3832307576Sgonzo x0 = margin_left; 3833307576Sgonzo else if (x0 >= max_x - margin_right) 3834307576Sgonzo x0 = max_x - margin_right; 3835307576Sgonzo if (y0 <= margin_bottom) 3836307576Sgonzo y0 = margin_bottom; 3837307576Sgonzo else if (y0 >= max_y - margin_top) 3838307576Sgonzo y0 = max_y - margin_top; 3839183888Sdumbbell 3840307576Sgonzo VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n", 3841307576Sgonzo x0, y0, f->p, f->w)); 3842132865Snjl 3843183888Sdumbbell /* 3844183888Sdumbbell * If the action is just beginning, init the structure and 3845183888Sdumbbell * compute tap timeout. 3846183888Sdumbbell */ 3847183888Sdumbbell if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) { 3848183888Sdumbbell VLOG(3, (LOG_DEBUG, "synaptics: ----\n")); 3849183888Sdumbbell 3850183888Sdumbbell /* Initialize queue. */ 3851307576Sgonzo gest->window_min = window_min; 3852183888Sdumbbell 3853183888Sdumbbell /* Reset pressure peak. */ 3854307576Sgonzo gest->zmax = 0; 3855183888Sdumbbell 3856183888Sdumbbell /* Reset fingers count. */ 3857307576Sgonzo gest->fingers_nb = 0; 3858183888Sdumbbell 3859183888Sdumbbell /* Reset virtual scrolling state. */ 3860307576Sgonzo gest->in_vscroll = 0; 3861183888Sdumbbell 3862183888Sdumbbell /* Compute tap timeout. */ 3863349541Swulf if (tap_enabled != 0) { 3864349541Swulf gest->taptimeout = (struct timeval) { 3865349541Swulf .tv_sec = tap_timeout / 1000000, 3866349541Swulf .tv_usec = tap_timeout % 1000000, 3867349541Swulf }; 3868349541Swulf timevaladd( 3869349541Swulf &gest->taptimeout, &sc->lastsoftintr); 3870349541Swulf } else 3871349541Swulf timevalclear(&gest->taptimeout); 3872183888Sdumbbell 3873183888Sdumbbell sc->flags |= PSM_FLAGS_FINGERDOWN; 3874307576Sgonzo 3875307576Sgonzo /* Smoother has not been reset yet */ 3876307576Sgonzo queue_len = 1; 3877307576Sgonzo start_x = x0; 3878307576Sgonzo start_y = y0; 3879183888Sdumbbell } else { 3880307576Sgonzo queue_len = smoother->queue_len + 1; 3881307576Sgonzo start_x = smoother->start_x; 3882307576Sgonzo start_y = smoother->start_y; 3883183888Sdumbbell } 3884183888Sdumbbell 3885307576Sgonzo /* Process ClickPad softbuttons */ 3886307576Sgonzo if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) { 3887307576Sgonzo y_ok = sc->syninfo.softbuttons_y >= 0 ? 3888307576Sgonzo start_y < sc->syninfo.softbuttons_y : 3889314361Sgonzo start_y > max_y + sc->syninfo.softbuttons_y; 3890183888Sdumbbell 3891307576Sgonzo center_button = MOUSE_BUTTON2DOWN; 3892307576Sgonzo center_x = sc->syninfo.softbutton2_x; 3893307576Sgonzo right_button = MOUSE_BUTTON3DOWN; 3894307576Sgonzo right_x = sc->syninfo.softbutton3_x; 3895183888Sdumbbell 3896307576Sgonzo if (center_x > 0 && right_x > 0 && center_x > right_x) { 3897307576Sgonzo center_button = MOUSE_BUTTON3DOWN; 3898307576Sgonzo center_x = sc->syninfo.softbutton3_x; 3899307576Sgonzo right_button = MOUSE_BUTTON2DOWN; 3900307576Sgonzo right_x = sc->syninfo.softbutton2_x; 3901307576Sgonzo } 3902183888Sdumbbell 3903307576Sgonzo if (right_x > 0 && start_x > right_x && y_ok) 3904307576Sgonzo ms->button = (ms->button & 3905307576Sgonzo ~MOUSE_BUTTON1DOWN) | right_button; 3906307576Sgonzo else if (center_x > 0 && start_x > center_x && y_ok) 3907307576Sgonzo ms->button = (ms->button & 3908307576Sgonzo ~MOUSE_BUTTON1DOWN) | center_button; 3909307576Sgonzo } 3910183888Sdumbbell 3911307576Sgonzo /* If in tap-hold, add the recorded button. */ 3912307576Sgonzo if (gest->in_taphold) 3913307576Sgonzo ms->button |= gest->tap_button; 3914183888Sdumbbell 3915183888Sdumbbell /* 3916183888Sdumbbell * For tap, we keep the maximum number of fingers and the 3917183888Sdumbbell * pressure peak. Also with multiple fingers, we increase 3918183888Sdumbbell * the minimum window. 3919183888Sdumbbell */ 3920307576Sgonzo if (nfingers > 1) 3921307576Sgonzo gest->window_min = window_max; 3922307576Sgonzo gest->fingers_nb = imax(nfingers, gest->fingers_nb); 3923307576Sgonzo gest->zmax = imax(f->p, gest->zmax); 3924183888Sdumbbell 3925307576Sgonzo /* Do we have enough packets to consider this a gesture? */ 3926307576Sgonzo if (queue_len < gest->window_min) 3927307576Sgonzo return; 3928183888Sdumbbell 3929298955Spfg /* Is a scrolling action occurring? */ 3930307576Sgonzo if (!gest->in_taphold && !ms->button && 3931307576Sgonzo (!gest->in_vscroll || two_finger_scroll)) { 3932178019Sjkim /* 3933183888Sdumbbell * A scrolling action must not conflict with a tap 3934183888Sdumbbell * action. Here are the conditions to consider a 3935183888Sdumbbell * scrolling action: 3936183888Sdumbbell * - the action in a configurable area 3937183888Sdumbbell * - one of the following: 3938183888Sdumbbell * . the distance between the last packet and the 3939183888Sdumbbell * first should be above a configurable minimum 3940183888Sdumbbell * . tap timed out 3941178019Sjkim */ 3942307576Sgonzo dxp = abs(x0 - start_x); 3943307576Sgonzo dyp = abs(y0 - start_y); 3944139982Sphilip 3945307576Sgonzo if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) || 3946183888Sdumbbell dxp >= sc->syninfo.vscroll_min_delta || 3947183888Sdumbbell dyp >= sc->syninfo.vscroll_min_delta) { 3948281440Srpaulo /* 3949281440Srpaulo * Handle two finger scrolling. 3950281440Srpaulo * Note that we don't rely on fingers_nb 3951281440Srpaulo * as that keeps the maximum number of fingers. 3952281440Srpaulo */ 3953281440Srpaulo if (two_finger_scroll) { 3954307576Sgonzo if (nfingers == 2) { 3955307576Sgonzo gest->in_vscroll += 3956281440Srpaulo dyp ? 2 : 0; 3957307576Sgonzo gest->in_vscroll += 3958281440Srpaulo dxp ? 1 : 0; 3959281440Srpaulo } 3960281440Srpaulo } else { 3961281440Srpaulo /* Check for horizontal scrolling. */ 3962281440Srpaulo if ((vscroll_hor_area > 0 && 3963307576Sgonzo start_y <= vscroll_hor_area) || 3964281440Srpaulo (vscroll_hor_area < 0 && 3965307576Sgonzo start_y >= 3966307576Sgonzo max_y + vscroll_hor_area)) 3967307576Sgonzo gest->in_vscroll += 2; 3968139982Sphilip 3969281440Srpaulo /* Check for vertical scrolling. */ 3970281440Srpaulo if ((vscroll_ver_area > 0 && 3971307576Sgonzo start_x <= vscroll_ver_area) || 3972281440Srpaulo (vscroll_ver_area < 0 && 3973307576Sgonzo start_x >= 3974307576Sgonzo max_x + vscroll_ver_area)) 3975307576Sgonzo gest->in_vscroll += 1; 3976281440Srpaulo } 3977139982Sphilip 3978183888Sdumbbell /* Avoid conflicts if area overlaps. */ 3979307576Sgonzo if (gest->in_vscroll >= 3) 3980307576Sgonzo gest->in_vscroll = 3981183888Sdumbbell (dxp > dyp) ? 2 : 1; 3982183888Sdumbbell } 3983183888Sdumbbell } 3984281440Srpaulo /* 3985281440Srpaulo * Reset two finger scrolling when the number of fingers 3986307576Sgonzo * is different from two or any button is pressed. 3987281440Srpaulo */ 3988307576Sgonzo if (two_finger_scroll && gest->in_vscroll != 0 && 3989307576Sgonzo (nfingers != 2 || ms->button)) 3990307576Sgonzo gest->in_vscroll = 0; 3991139982Sphilip 3992281440Srpaulo VLOG(5, (LOG_DEBUG, 3993281440Srpaulo "synaptics: virtual scrolling: %s " 3994281440Srpaulo "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n", 3995307576Sgonzo gest->in_vscroll ? "YES" : "NO", 3996307576Sgonzo gest->in_vscroll, dxp, dyp, 3997307576Sgonzo gest->fingers_nb)); 3998281440Srpaulo 3999183888Sdumbbell } else if (sc->flags & PSM_FLAGS_FINGERDOWN) { 4000183888Sdumbbell /* 4001183888Sdumbbell * An action is currently taking place but the pressure 4002183888Sdumbbell * dropped under the minimum, putting an end to it. 4003183888Sdumbbell */ 4004183888Sdumbbell int taphold_timeout, dx, dy, tap_max_delta; 4005183888Sdumbbell 4006307576Sgonzo dx = abs(smoother->queue[smoother->queue_cursor].x - 4007307576Sgonzo smoother->start_x); 4008307576Sgonzo dy = abs(smoother->queue[smoother->queue_cursor].y - 4009307576Sgonzo smoother->start_y); 4010183888Sdumbbell 4011183888Sdumbbell /* Max delta is disabled for multi-fingers tap. */ 4012307576Sgonzo if (gest->fingers_nb > 1) 4013183888Sdumbbell tap_max_delta = imax(dx, dy); 4014183888Sdumbbell else 4015183888Sdumbbell tap_max_delta = sc->syninfo.tap_max_delta; 4016183888Sdumbbell 4017183888Sdumbbell sc->flags &= ~PSM_FLAGS_FINGERDOWN; 4018183888Sdumbbell 4019183888Sdumbbell /* Check for tap. */ 4020183888Sdumbbell VLOG(3, (LOG_DEBUG, 4021183888Sdumbbell "synaptics: zmax=%d, dx=%d, dy=%d, " 4022183888Sdumbbell "delta=%d, fingers=%d, queue=%d\n", 4023307576Sgonzo gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb, 4024307576Sgonzo smoother->queue_len)); 4025307576Sgonzo if (!gest->in_vscroll && gest->zmax >= tap_threshold && 4026307576Sgonzo timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) && 4027183888Sdumbbell dx <= tap_max_delta && dy <= tap_max_delta && 4028307576Sgonzo smoother->queue_len >= sc->syninfo.tap_min_queue) { 4029178019Sjkim /* 4030183888Sdumbbell * We have a tap if: 4031183888Sdumbbell * - the maximum pressure went over tap_threshold 4032183888Sdumbbell * - the action ended before tap_timeout 4033183888Sdumbbell * 4034183888Sdumbbell * To handle tap-hold, we must delay any button push to 4035183888Sdumbbell * the next action. 4036178019Sjkim */ 4037307576Sgonzo if (gest->in_taphold) { 4038183888Sdumbbell /* 4039183888Sdumbbell * This is the second and last tap of a 4040183888Sdumbbell * double tap action, not a tap-hold. 4041183888Sdumbbell */ 4042307576Sgonzo gest->in_taphold = 0; 4043139982Sphilip 4044183888Sdumbbell /* 4045183888Sdumbbell * For double-tap to work: 4046183888Sdumbbell * - no button press is emitted (to 4047183888Sdumbbell * simulate a button release) 4048183888Sdumbbell * - PSM_FLAGS_FINGERDOWN is set to 4049183888Sdumbbell * force the next packet to emit a 4050183888Sdumbbell * button press) 4051183888Sdumbbell */ 4052183888Sdumbbell VLOG(2, (LOG_DEBUG, 4053183888Sdumbbell "synaptics: button RELEASE: %d\n", 4054307576Sgonzo gest->tap_button)); 4055183888Sdumbbell sc->flags |= PSM_FLAGS_FINGERDOWN; 4056307576Sgonzo 4057307576Sgonzo /* Schedule button press on next interrupt */ 4058307576Sgonzo sc->idletimeout.tv_sec = psmhz > 1 ? 4059307576Sgonzo 0 : 1; 4060307576Sgonzo sc->idletimeout.tv_usec = psmhz > 1 ? 4061307576Sgonzo 1000000 / psmhz : 0; 4062178019Sjkim } else { 4063178019Sjkim /* 4064183888Sdumbbell * This is the first tap: we set the 4065183888Sdumbbell * tap-hold state and notify the button 4066183888Sdumbbell * down event. 4067178019Sjkim */ 4068307576Sgonzo gest->in_taphold = 1; 4069183888Sdumbbell taphold_timeout = sc->syninfo.taphold_timeout; 4070307576Sgonzo gest->taptimeout.tv_sec = taphold_timeout / 4071183888Sdumbbell 1000000; 4072307576Sgonzo gest->taptimeout.tv_usec = taphold_timeout % 4073183888Sdumbbell 1000000; 4074307576Sgonzo sc->idletimeout = gest->taptimeout; 4075307576Sgonzo timevaladd(&gest->taptimeout, 4076307576Sgonzo &sc->lastsoftintr); 4077139982Sphilip 4078307576Sgonzo switch (gest->fingers_nb) { 4079183888Sdumbbell case 3: 4080307576Sgonzo gest->tap_button = 4081183888Sdumbbell MOUSE_BUTTON2DOWN; 4082183888Sdumbbell break; 4083183888Sdumbbell case 2: 4084307576Sgonzo gest->tap_button = 4085183888Sdumbbell MOUSE_BUTTON3DOWN; 4086183888Sdumbbell break; 4087183888Sdumbbell default: 4088307576Sgonzo gest->tap_button = 4089183888Sdumbbell MOUSE_BUTTON1DOWN; 4090183888Sdumbbell } 4091183888Sdumbbell VLOG(2, (LOG_DEBUG, 4092183888Sdumbbell "synaptics: button PRESS: %d\n", 4093307576Sgonzo gest->tap_button)); 4094307576Sgonzo ms->button |= gest->tap_button; 4095178019Sjkim } 4096183888Sdumbbell } else { 4097183888Sdumbbell /* 4098183888Sdumbbell * Not enough pressure or timeout: reset 4099183888Sdumbbell * tap-hold state. 4100183888Sdumbbell */ 4101307576Sgonzo if (gest->in_taphold) { 4102183888Sdumbbell VLOG(2, (LOG_DEBUG, 4103183888Sdumbbell "synaptics: button RELEASE: %d\n", 4104307576Sgonzo gest->tap_button)); 4105307576Sgonzo gest->in_taphold = 0; 4106183888Sdumbbell } else { 4107183888Sdumbbell VLOG(2, (LOG_DEBUG, 4108183888Sdumbbell "synaptics: not a tap-hold\n")); 4109183888Sdumbbell } 4110183888Sdumbbell } 4111307576Sgonzo } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) { 4112178019Sjkim /* 4113183888Sdumbbell * For a tap-hold to work, the button must remain down at 4114183888Sdumbbell * least until timeout (where the in_taphold flags will be 4115183888Sdumbbell * cleared) or during the next action. 4116139982Sphilip */ 4117307576Sgonzo if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) { 4118307576Sgonzo ms->button |= gest->tap_button; 4119183888Sdumbbell } else { 4120307576Sgonzo VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n", 4121307576Sgonzo gest->tap_button)); 4122307576Sgonzo gest->in_taphold = 0; 4123133297Sphilip } 4124178019Sjkim } 4125133296Sphilip 4126307576Sgonzo return; 4127307576Sgonzo} 4128307576Sgonzo 4129307576Sgonzostatic void 4130307576Sgonzopsmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id, 4131307576Sgonzo mousestatus_t *ms, int *x, int *y) 4132307576Sgonzo{ 4133307576Sgonzo smoother_t *smoother = &sc->smoother[smoother_id]; 4134307576Sgonzo gesture_t *gest = &(sc->gesture); 4135307576Sgonzo 4136183888Sdumbbell /* 4137307576Sgonzo * Check pressure to detect a real wanted action on the 4138307576Sgonzo * touchpad. 4139183888Sdumbbell */ 4140307576Sgonzo if (f->p >= sc->syninfo.min_pressure) { 4141307576Sgonzo int x0, y0; 4142307576Sgonzo int cursor, peer, window; 4143307576Sgonzo int dx, dy, dxp, dyp; 4144307576Sgonzo int max_width, max_pressure; 4145307576Sgonzo int margin_top, margin_right, margin_bottom, margin_left; 4146307576Sgonzo int na_top, na_right, na_bottom, na_left; 4147307576Sgonzo int window_min, window_max; 4148307576Sgonzo int multiplicator; 4149307576Sgonzo int weight_current, weight_previous, weight_len_squared; 4150307576Sgonzo int div_min, div_max, div_len; 4151307576Sgonzo int vscroll_hor_area, vscroll_ver_area; 4152307576Sgonzo int two_finger_scroll; 4153307576Sgonzo int max_x, max_y; 4154307576Sgonzo int len, weight_prev_x, weight_prev_y; 4155307576Sgonzo int div_max_x, div_max_y, div_x, div_y; 4156307576Sgonzo int is_fuzzy; 4157349541Swulf int natural_scroll; 4158281440Srpaulo 4159307576Sgonzo /* Read sysctl. */ 4160307576Sgonzo /* XXX Verify values? */ 4161307576Sgonzo max_width = sc->syninfo.max_width; 4162307576Sgonzo max_pressure = sc->syninfo.max_pressure; 4163307576Sgonzo margin_top = sc->syninfo.margin_top; 4164307576Sgonzo margin_right = sc->syninfo.margin_right; 4165307576Sgonzo margin_bottom = sc->syninfo.margin_bottom; 4166307576Sgonzo margin_left = sc->syninfo.margin_left; 4167307576Sgonzo na_top = sc->syninfo.na_top; 4168307576Sgonzo na_right = sc->syninfo.na_right; 4169307576Sgonzo na_bottom = sc->syninfo.na_bottom; 4170307576Sgonzo na_left = sc->syninfo.na_left; 4171307576Sgonzo window_min = sc->syninfo.window_min; 4172307576Sgonzo window_max = sc->syninfo.window_max; 4173307576Sgonzo multiplicator = sc->syninfo.multiplicator; 4174307576Sgonzo weight_current = sc->syninfo.weight_current; 4175307576Sgonzo weight_previous = sc->syninfo.weight_previous; 4176307576Sgonzo weight_len_squared = sc->syninfo.weight_len_squared; 4177307576Sgonzo div_min = sc->syninfo.div_min; 4178307576Sgonzo div_max = sc->syninfo.div_max; 4179307576Sgonzo div_len = sc->syninfo.div_len; 4180307576Sgonzo vscroll_hor_area = sc->syninfo.vscroll_hor_area; 4181307576Sgonzo vscroll_ver_area = sc->syninfo.vscroll_ver_area; 4182307576Sgonzo two_finger_scroll = sc->syninfo.two_finger_scroll; 4183307576Sgonzo max_x = sc->syninfo.max_x; 4184307576Sgonzo max_y = sc->syninfo.max_y; 4185349541Swulf natural_scroll = sc->syninfo.natural_scroll; 4186307576Sgonzo 4187307576Sgonzo is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0; 4188307576Sgonzo 4189307576Sgonzo /* Read current absolute position. */ 4190307576Sgonzo x0 = f->x; 4191307576Sgonzo y0 = f->y; 4192307576Sgonzo 4193307576Sgonzo /* 4194307576Sgonzo * Limit the coordinates to the specified margins because 4195307576Sgonzo * this area isn't very reliable. 4196307576Sgonzo */ 4197307576Sgonzo if (x0 <= margin_left) 4198307576Sgonzo x0 = margin_left; 4199307576Sgonzo else if (x0 >= max_x - margin_right) 4200307576Sgonzo x0 = max_x - margin_right; 4201307576Sgonzo if (y0 <= margin_bottom) 4202307576Sgonzo y0 = margin_bottom; 4203307576Sgonzo else if (y0 >= max_y - margin_top) 4204307576Sgonzo y0 = max_y - margin_top; 4205307576Sgonzo 4206307576Sgonzo /* If the action is just beginning, init the structure. */ 4207307576Sgonzo if (smoother->active == 0) { 4208307576Sgonzo VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id)); 4209307576Sgonzo 4210307576Sgonzo /* Store the first point of this action. */ 4211307576Sgonzo smoother->start_x = x0; 4212307576Sgonzo smoother->start_y = y0; 4213307576Sgonzo dx = dy = 0; 4214307576Sgonzo 4215307576Sgonzo /* Initialize queue. */ 4216307576Sgonzo smoother->queue_cursor = SYNAPTICS_PACKETQUEUE; 4217307576Sgonzo smoother->queue_len = 0; 4218307576Sgonzo 4219307576Sgonzo /* Reset average. */ 4220307576Sgonzo smoother->avg_dx = 0; 4221307576Sgonzo smoother->avg_dy = 0; 4222307576Sgonzo 4223307576Sgonzo /* Reset squelch. */ 4224307576Sgonzo smoother->squelch_x = 0; 4225307576Sgonzo smoother->squelch_y = 0; 4226307576Sgonzo 4227307576Sgonzo /* Activate queue */ 4228307576Sgonzo smoother->active = 1; 4229307576Sgonzo } else { 4230307576Sgonzo /* Calculate the current delta. */ 4231307576Sgonzo cursor = smoother->queue_cursor; 4232307576Sgonzo dx = x0 - smoother->queue[cursor].x; 4233307576Sgonzo dy = y0 - smoother->queue[cursor].y; 4234307576Sgonzo } 4235307576Sgonzo 4236307576Sgonzo VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n", 4237307576Sgonzo smoother_id, x0, y0, f->p, f->w)); 4238307576Sgonzo 4239307576Sgonzo /* Queue this new packet. */ 4240307576Sgonzo cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1); 4241307576Sgonzo smoother->queue[cursor].x = x0; 4242307576Sgonzo smoother->queue[cursor].y = y0; 4243307576Sgonzo smoother->queue_cursor = cursor; 4244307576Sgonzo if (smoother->queue_len < SYNAPTICS_PACKETQUEUE) 4245307576Sgonzo smoother->queue_len++; 4246307576Sgonzo VLOG(5, (LOG_DEBUG, 4247307576Sgonzo "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n", 4248307576Sgonzo smoother_id, cursor, x0, y0, dx, dy)); 4249307576Sgonzo 4250307576Sgonzo /* Do we have enough packets to consider this a movement? */ 4251307576Sgonzo if (smoother->queue_len < gest->window_min) 4252307576Sgonzo return; 4253307576Sgonzo 4254307576Sgonzo weight_prev_x = weight_prev_y = weight_previous; 4255307576Sgonzo div_max_x = div_max_y = div_max; 4256307576Sgonzo 4257307576Sgonzo if (gest->in_vscroll) { 4258307576Sgonzo /* Dividers are different with virtual scrolling. */ 4259307576Sgonzo div_min = sc->syninfo.vscroll_div_min; 4260307576Sgonzo div_max_x = div_max_y = sc->syninfo.vscroll_div_max; 4261307576Sgonzo } else { 4262307576Sgonzo /* 4263307576Sgonzo * There's a lot of noise in coordinates when 4264307576Sgonzo * the finger is on the touchpad's borders. When 4265307576Sgonzo * using this area, we apply a special weight and 4266307576Sgonzo * div. 4267307576Sgonzo */ 4268307576Sgonzo if (x0 <= na_left || x0 >= max_x - na_right) { 4269307576Sgonzo weight_prev_x = sc->syninfo.weight_previous_na; 4270307576Sgonzo div_max_x = sc->syninfo.div_max_na; 4271307576Sgonzo } 4272307576Sgonzo 4273307576Sgonzo if (y0 <= na_bottom || y0 >= max_y - na_top) { 4274307576Sgonzo weight_prev_y = sc->syninfo.weight_previous_na; 4275307576Sgonzo div_max_y = sc->syninfo.div_max_na; 4276307576Sgonzo } 4277307576Sgonzo } 4278307576Sgonzo 4279307576Sgonzo /* 4280307576Sgonzo * Calculate weights for the average operands and 4281307576Sgonzo * the divisor. Both depend on the distance between 4282307576Sgonzo * the current packet and a previous one (based on the 4283307576Sgonzo * window width). 4284307576Sgonzo */ 4285307576Sgonzo window = imin(smoother->queue_len, window_max); 4286307576Sgonzo peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1); 4287307576Sgonzo dxp = abs(x0 - smoother->queue[peer].x) + 1; 4288307576Sgonzo dyp = abs(y0 - smoother->queue[peer].y) + 1; 4289307576Sgonzo len = (dxp * dxp) + (dyp * dyp); 4290307576Sgonzo weight_prev_x = imin(weight_prev_x, 4291307576Sgonzo weight_len_squared * weight_prev_x / len); 4292307576Sgonzo weight_prev_y = imin(weight_prev_y, 4293307576Sgonzo weight_len_squared * weight_prev_y / len); 4294307576Sgonzo 4295307576Sgonzo len = (dxp + dyp) / 2; 4296307576Sgonzo div_x = div_len * div_max_x / len; 4297307576Sgonzo div_x = imin(div_max_x, div_x); 4298307576Sgonzo div_x = imax(div_min, div_x); 4299307576Sgonzo div_y = div_len * div_max_y / len; 4300307576Sgonzo div_y = imin(div_max_y, div_y); 4301307576Sgonzo div_y = imax(div_min, div_y); 4302307576Sgonzo 4303307576Sgonzo VLOG(3, (LOG_DEBUG, 4304307576Sgonzo "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n", 4305307576Sgonzo smoother_id, peer, len, weight_prev_x, weight_prev_y, 4306307576Sgonzo div_x, div_y)); 4307307576Sgonzo 4308307576Sgonzo /* Compute averages. */ 4309307576Sgonzo smoother->avg_dx = 4310307576Sgonzo (weight_current * dx * multiplicator + 4311307576Sgonzo weight_prev_x * smoother->avg_dx) / 4312307576Sgonzo (weight_current + weight_prev_x); 4313307576Sgonzo 4314307576Sgonzo smoother->avg_dy = 4315307576Sgonzo (weight_current * dy * multiplicator + 4316307576Sgonzo weight_prev_y * smoother->avg_dy) / 4317307576Sgonzo (weight_current + weight_prev_y); 4318307576Sgonzo 4319307576Sgonzo VLOG(5, (LOG_DEBUG, 4320307576Sgonzo "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id, 4321307576Sgonzo smoother->avg_dx / multiplicator, 4322307576Sgonzo smoother->avg_dy / multiplicator)); 4323307576Sgonzo 4324307576Sgonzo /* Use these averages to calculate x & y. */ 4325307576Sgonzo smoother->squelch_x += smoother->avg_dx; 4326307576Sgonzo dxp = smoother->squelch_x / (div_x * multiplicator); 4327307576Sgonzo smoother->squelch_x = smoother->squelch_x % 4328307576Sgonzo (div_x * multiplicator); 4329307576Sgonzo 4330307576Sgonzo smoother->squelch_y += smoother->avg_dy; 4331307576Sgonzo dyp = smoother->squelch_y / (div_y * multiplicator); 4332307576Sgonzo smoother->squelch_y = smoother->squelch_y % 4333307576Sgonzo (div_y * multiplicator); 4334307576Sgonzo 4335307576Sgonzo switch(gest->in_vscroll) { 4336307576Sgonzo case 0: /* Pointer movement. */ 4337307576Sgonzo /* On real<->fuzzy finger switch the x/y pos jumps */ 4338307576Sgonzo if (is_fuzzy == smoother->is_fuzzy) { 4339307576Sgonzo *x += dxp; 4340307576Sgonzo *y += dyp; 4341307576Sgonzo } 4342307576Sgonzo 4343307576Sgonzo VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n", 4344307576Sgonzo smoother_id, dx, dy, dxp, dyp)); 4345307576Sgonzo break; 4346307576Sgonzo case 1: /* Vertical scrolling. */ 4347349541Swulf if (dyp != 0) { 4348349541Swulf if (two_finger_scroll && natural_scroll) 4349349541Swulf ms->button |= (dyp > 0) ? 4350349541Swulf MOUSE_BUTTON5DOWN : MOUSE_BUTTON4DOWN; 4351349541Swulf else 4352349541Swulf ms->button |= (dyp > 0) ? 4353349541Swulf MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN; 4354349541Swulf } 4355307576Sgonzo break; 4356307576Sgonzo case 2: /* Horizontal scrolling. */ 4357349541Swulf if (dxp != 0) { 4358349541Swulf if (two_finger_scroll && natural_scroll) 4359349541Swulf ms->button |= (dxp > 0) ? 4360349541Swulf MOUSE_BUTTON6DOWN : MOUSE_BUTTON7DOWN; 4361349541Swulf else 4362349541Swulf ms->button |= (dxp > 0) ? 4363349541Swulf MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN; 4364349541Swulf } 4365307576Sgonzo break; 4366307576Sgonzo } 4367307576Sgonzo 4368307576Sgonzo smoother->is_fuzzy = is_fuzzy; 4369307576Sgonzo 4370307576Sgonzo } else { 4371307576Sgonzo /* 4372307576Sgonzo * Deactivate queue. Note: We can not just reset queue here 4373307576Sgonzo * as these values are still used by gesture processor. 4374307576Sgonzo * So postpone reset till next touch. 4375307576Sgonzo */ 4376307576Sgonzo smoother->active = 0; 4377307576Sgonzo } 4378307576Sgonzo} 4379307576Sgonzo 4380307576Sgonzostatic int 4381307576Sgonzoproc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 4382307576Sgonzo int *x, int *y, int *z) 4383307576Sgonzo{ 4384307576Sgonzo static int touchpad_button, trackpoint_button; 4385307576Sgonzo finger_t fn, f[ELANTECH_MAX_FINGERS]; 4386349541Swulf int pkt, id, scale, i, nfingers, mask, palm; 4387307576Sgonzo 4388307576Sgonzo if (!elantech_support) 4389307576Sgonzo return (0); 4390307576Sgonzo 4391307576Sgonzo /* Determine packet format and do a sanity check for out of sync packets. */ 4392307576Sgonzo if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion)) 4393307576Sgonzo pkt = ELANTECH_PKT_NOP; 4394328482Sdumbbell else if (sc->elanhw.hastrackpoint && ELANTECH_PKT_IS_TRACKPOINT(pb)) 4395307576Sgonzo pkt = ELANTECH_PKT_TRACKPOINT; 4396307576Sgonzo else 4397307576Sgonzo switch (sc->elanhw.hwversion) { 4398307576Sgonzo case 2: 4399307576Sgonzo if (!ELANTECH_PKT_IS_V2(pb)) 4400307576Sgonzo return (-1); 4401307576Sgonzo 4402307576Sgonzo pkt = (pb->ipacket[0] & 0xc0) == 0x80 ? 4403307576Sgonzo ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON; 4404307576Sgonzo break; 4405307576Sgonzo case 3: 4406307576Sgonzo if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) && 4407307576Sgonzo !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc)) 4408307576Sgonzo return (-1); 4409307576Sgonzo 4410307576Sgonzo pkt = ELANTECH_PKT_V3; 4411307576Sgonzo break; 4412307576Sgonzo case 4: 4413307576Sgonzo if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc)) 4414307576Sgonzo return (-1); 4415307576Sgonzo 4416307576Sgonzo switch (pb->ipacket[3] & 0x03) { 4417307576Sgonzo case 0x00: 4418307576Sgonzo pkt = ELANTECH_PKT_V4_STATUS; 4419307576Sgonzo break; 4420307576Sgonzo case 0x01: 4421307576Sgonzo pkt = ELANTECH_PKT_V4_HEAD; 4422307576Sgonzo break; 4423307576Sgonzo case 0x02: 4424307576Sgonzo pkt = ELANTECH_PKT_V4_MOTION; 4425307576Sgonzo break; 4426307576Sgonzo default: 4427307576Sgonzo return (-1); 4428307576Sgonzo } 4429307576Sgonzo break; 4430307576Sgonzo default: 4431307576Sgonzo return (-1); 4432307576Sgonzo } 4433307576Sgonzo 4434307576Sgonzo VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt)); 4435307576Sgonzo 4436307576Sgonzo for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 4437307576Sgonzo PSM_FINGER_RESET(f[id]); 4438307576Sgonzo 4439307576Sgonzo *x = *y = *z = 0; 4440307576Sgonzo ms->button = ms->obutton; 4441307576Sgonzo 4442307576Sgonzo if (sc->syninfo.touchpad_off) 4443307576Sgonzo return (0); 4444307576Sgonzo 4445307576Sgonzo /* Common legend 4446307576Sgonzo * L: Left mouse button pressed 4447307576Sgonzo * R: Right mouse button pressed 4448307576Sgonzo * N: number of fingers on touchpad 4449307576Sgonzo * X: absolute x value (horizontal) 4450307576Sgonzo * Y: absolute y value (vertical) 4451307576Sgonzo * W; width of the finger touch 4452307576Sgonzo * P: pressure 4453307576Sgonzo */ 4454307576Sgonzo switch (pkt) { 4455307576Sgonzo case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */ 4456307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4457307576Sgonzo * ------------------------------------------- 4458307576Sgonzo * ipacket[0]: N1 N0 W3 W2 . . R L 4459307576Sgonzo * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 4460307576Sgonzo * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 4461307576Sgonzo * ipacket[3]: N4 VF W1 W0 . . . B2 4462307576Sgonzo * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 4463307576Sgonzo * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 4464307576Sgonzo * ------------------------------------------- 4465307576Sgonzo * N4: set if more than 3 fingers (only in 3 fingers mode) 4466307576Sgonzo * VF: a kind of flag? (only on EF123, 0 when finger 4467307576Sgonzo * is over one of the buttons, 1 otherwise) 4468307576Sgonzo * B2: (on EF113 only, 0 otherwise), one button pressed 4469307576Sgonzo * P & W is not reported on EF113 touchpads 4470307576Sgonzo */ 4471307576Sgonzo nfingers = (pb->ipacket[0] & 0xc0) >> 6; 4472307576Sgonzo if (nfingers == 3 && (pb->ipacket[3] & 0x80)) 4473307576Sgonzo nfingers = 4; 4474307576Sgonzo 4475318500Swulf if (nfingers == 0) { 4476318500Swulf mask = (1 << nfingers) - 1; /* = 0x00 */ 4477318500Swulf break; 4478318500Swulf } 4479318500Swulf 4480318500Swulf /* Map 3-rd and 4-th fingers to first finger */ 4481318500Swulf mask = (1 << 1) - 1; /* = 0x01 */ 4482318500Swulf f[0] = ELANTECH_FINGER_SET_XYP(pb); 4483307576Sgonzo if (sc->elanhw.haspressure) { 4484318500Swulf f[0].w = ((pb->ipacket[0] & 0x30) >> 2) | 4485307576Sgonzo ((pb->ipacket[3] & 0x30) >> 4); 4486307576Sgonzo } else { 4487318500Swulf f[0].p = PSM_FINGER_DEFAULT_P; 4488318500Swulf f[0].w = PSM_FINGER_DEFAULT_W; 4489307576Sgonzo } 4490307576Sgonzo 4491307576Sgonzo /* 4492307576Sgonzo * HW v2 dont report exact finger positions when 3 or more 4493318500Swulf * fingers are on touchpad. 4494307576Sgonzo */ 4495307576Sgonzo if (nfingers > 2) 4496318500Swulf f[0].flags = PSM_FINGER_FUZZY; 4497307576Sgonzo 4498307576Sgonzo break; 4499307576Sgonzo 4500307576Sgonzo case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */ 4501307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4502307576Sgonzo * ------------------------------------------- 4503307576Sgonzo * ipacket[0]: N1 N0 AY8 AX8 . . R L 4504307576Sgonzo * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0 4505307576Sgonzo * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0 4506307576Sgonzo * ipacket[3]: . . BY8 BX8 . . . . 4507307576Sgonzo * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0 4508307576Sgonzo * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0 4509307576Sgonzo * ------------------------------------------- 4510307576Sgonzo * AX: lower-left finger absolute x value 4511307576Sgonzo * AY: lower-left finger absolute y value 4512307576Sgonzo * BX: upper-right finger absolute x value 4513307576Sgonzo * BY: upper-right finger absolute y value 4514307576Sgonzo */ 4515307576Sgonzo nfingers = 2; 4516307576Sgonzo mask = (1 << nfingers) - 1; 4517307576Sgonzo 4518307576Sgonzo for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++) 4519307576Sgonzo f[id] = (finger_t) { 4520307576Sgonzo .x = (((pb->ipacket[id * 3] & 0x10) << 4) | 4521307576Sgonzo pb->ipacket[id * 3 + 1]) << 2, 4522307576Sgonzo .y = (((pb->ipacket[id * 3] & 0x20) << 3) | 4523307576Sgonzo pb->ipacket[id * 3 + 2]) << 2, 4524307576Sgonzo .p = PSM_FINGER_DEFAULT_P, 4525307576Sgonzo .w = PSM_FINGER_DEFAULT_W, 4526307576Sgonzo /* HW ver.2 sends bounding box */ 4527307576Sgonzo .flags = PSM_FINGER_FUZZY 4528307576Sgonzo }; 4529307576Sgonzo break; 4530307576Sgonzo 4531307576Sgonzo case ELANTECH_PKT_V3: /* HW Version 3 */ 4532307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4533307576Sgonzo * ------------------------------------------- 4534307576Sgonzo * ipacket[0]: N1 N0 W3 W2 0 1 R L 4535307576Sgonzo * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 4536307576Sgonzo * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 4537307576Sgonzo * ipacket[3]: 0 0 W1 W0 0 0 1 0 4538307576Sgonzo * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 4539307576Sgonzo * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 4540307576Sgonzo * ------------------------------------------- 4541307576Sgonzo */ 4542307576Sgonzo nfingers = (pb->ipacket[0] & 0xc0) >> 6; 4543318500Swulf /* Map 3-rd finger to first finger */ 4544318500Swulf id = nfingers > 2 ? 0 : nfingers - 1; 4545318500Swulf mask = (1 << (id + 1)) - 1; 4546307576Sgonzo 4547318500Swulf if (nfingers == 0) 4548318500Swulf break; 4549318500Swulf 4550307576Sgonzo fn = ELANTECH_FINGER_SET_XYP(pb); 4551307576Sgonzo fn.w = ((pb->ipacket[0] & 0x30) >> 2) | 4552307576Sgonzo ((pb->ipacket[3] & 0x30) >> 4); 4553307576Sgonzo 4554307576Sgonzo /* 4555307576Sgonzo * HW v3 dont report exact finger positions when 3 or more 4556318500Swulf * fingers are on touchpad. 4557307576Sgonzo */ 4558307576Sgonzo if (nfingers > 1) 4559307576Sgonzo fn.flags = PSM_FINGER_FUZZY; 4560307576Sgonzo 4561307576Sgonzo if (nfingers == 2) { 4562307576Sgonzo if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) { 4563307576Sgonzo sc->elanaction.fingers[0] = fn; 4564307576Sgonzo return (0); 4565307576Sgonzo } else 4566307576Sgonzo f[0] = sc->elanaction.fingers[0]; 4567307576Sgonzo } 4568318500Swulf f[id] = fn; 4569307576Sgonzo break; 4570307576Sgonzo 4571307576Sgonzo case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */ 4572307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4573307576Sgonzo * ------------------------------------------- 4574307576Sgonzo * ipacket[0]: . . . . 0 1 R L 4575307576Sgonzo * ipacket[1]: . . . F4 F3 F2 F1 F0 4576307576Sgonzo * ipacket[2]: . . . . . . . . 4577307576Sgonzo * ipacket[3]: . . . 1 0 0 0 0 4578307576Sgonzo * ipacket[4]: PL . . . . . . . 4579307576Sgonzo * ipacket[5]: . . . . . . . . 4580307576Sgonzo * ------------------------------------------- 4581307576Sgonzo * Fn: finger n is on touchpad 4582307576Sgonzo * PL: palm 4583307576Sgonzo * HV ver4 sends a status packet to indicate that the numbers 4584307576Sgonzo * or identities of the fingers has been changed 4585307576Sgonzo */ 4586307576Sgonzo 4587307576Sgonzo mask = pb->ipacket[1] & 0x1f; 4588307576Sgonzo nfingers = bitcount(mask); 4589307576Sgonzo 4590318500Swulf if (sc->elanaction.mask_v4wait != 0) 4591318500Swulf VLOG(3, (LOG_DEBUG, "elantech: HW v4 status packet" 4592318500Swulf " when not all previous head packets received\n")); 4593318500Swulf 4594318500Swulf /* Bitmap of fingers to receive before gesture processing */ 4595318500Swulf sc->elanaction.mask_v4wait = mask & ~sc->elanaction.mask; 4596318500Swulf 4597307576Sgonzo /* Skip "new finger is on touchpad" packets */ 4598318500Swulf if (sc->elanaction.mask_v4wait) { 4599307576Sgonzo sc->elanaction.mask = mask; 4600307576Sgonzo return (0); 4601307576Sgonzo } 4602307576Sgonzo 4603307576Sgonzo break; 4604307576Sgonzo 4605307576Sgonzo case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */ 4606307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4607307576Sgonzo * ------------------------------------------- 4608307576Sgonzo * ipacket[0]: W3 W2 W1 W0 0 1 R L 4609307576Sgonzo * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 4610307576Sgonzo * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 4611307576Sgonzo * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1 4612307576Sgonzo * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 4613307576Sgonzo * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 4614307576Sgonzo * ------------------------------------------- 4615307576Sgonzo * ID: finger id 4616307576Sgonzo * HW ver 4 sends head packets in two cases: 4617307576Sgonzo * 1. One finger touch and movement. 4618307576Sgonzo * 2. Next after status packet to tell new finger positions. 4619307576Sgonzo */ 4620307576Sgonzo mask = sc->elanaction.mask; 4621307576Sgonzo nfingers = bitcount(mask); 4622307576Sgonzo id = ((pb->ipacket[3] & 0xe0) >> 5) - 1; 4623318500Swulf fn = ELANTECH_FINGER_SET_XYP(pb); 4624318500Swulf fn.w =(pb->ipacket[0] & 0xf0) >> 4; 4625307576Sgonzo 4626318500Swulf if (id < 0) 4627318500Swulf return (0); 4628318500Swulf 4629318500Swulf /* Packet is finger position update. Report it */ 4630318500Swulf if (sc->elanaction.mask_v4wait == 0) { 4631318500Swulf if (id < ELANTECH_MAX_FINGERS) 4632318500Swulf f[id] = fn; 4633318500Swulf break; 4634307576Sgonzo } 4635318500Swulf 4636318500Swulf /* Remove finger from waiting bitmap and store into context */ 4637318500Swulf sc->elanaction.mask_v4wait &= ~(1 << id); 4638318500Swulf if (id < ELANTECH_MAX_FINGERS) 4639318500Swulf sc->elanaction.fingers[id] = fn; 4640318500Swulf 4641318500Swulf /* Wait for other fingers if needed */ 4642318500Swulf if (sc->elanaction.mask_v4wait != 0) 4643318500Swulf return (0); 4644318500Swulf 4645318500Swulf /* All new fingers are received. Report them from context */ 4646318500Swulf for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 4647318500Swulf if (sc->elanaction.mask & (1 << id)) 4648318500Swulf f[id] = sc->elanaction.fingers[id]; 4649318500Swulf 4650307576Sgonzo break; 4651307576Sgonzo 4652307576Sgonzo case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */ 4653307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4654307576Sgonzo * ------------------------------------------- 4655307576Sgonzo * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L 4656307576Sgonzo * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0 4657307576Sgonzo * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0 4658307576Sgonzo * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0 4659307576Sgonzo * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0 4660307576Sgonzo * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0 4661307576Sgonzo * ------------------------------------------- 4662307576Sgonzo * OF: delta overflows (> 127 or < -128), in this case 4663307576Sgonzo * firmware sends us (delta x / 5) and (delta y / 5) 4664307576Sgonzo * ID: finger id 4665307576Sgonzo * DX: delta x (two's complement) 4666307576Sgonzo * XY: delta y (two's complement) 4667307576Sgonzo * byte 0 ~ 2 for one finger 4668307576Sgonzo * byte 3 ~ 5 for another finger 4669307576Sgonzo */ 4670307576Sgonzo mask = sc->elanaction.mask; 4671307576Sgonzo nfingers = bitcount(mask); 4672307576Sgonzo 4673307576Sgonzo scale = (pb->ipacket[0] & 0x10) ? 5 : 1; 4674307576Sgonzo for (i = 0; i <= 3; i += 3) { 4675307576Sgonzo id = ((pb->ipacket[i] & 0xe0) >> 5) - 1; 4676307576Sgonzo if (id < 0 || id >= ELANTECH_MAX_FINGERS) 4677307576Sgonzo continue; 4678307576Sgonzo 4679307576Sgonzo if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) { 4680307576Sgonzo f[id] = sc->elanaction.fingers[id]; 4681307576Sgonzo f[id].x += imax(-f[id].x, 4682307576Sgonzo (signed char)pb->ipacket[i+1] * scale); 4683307576Sgonzo f[id].y += imax(-f[id].y, 4684307576Sgonzo (signed char)pb->ipacket[i+2] * scale); 4685307576Sgonzo } else { 4686307576Sgonzo VLOG(3, (LOG_DEBUG, "elantech: " 4687307576Sgonzo "HW v4 motion packet skipped\n")); 4688307576Sgonzo } 4689307576Sgonzo } 4690307576Sgonzo 4691307576Sgonzo break; 4692307576Sgonzo 4693307576Sgonzo case ELANTECH_PKT_TRACKPOINT: 4694307576Sgonzo /* 7 6 5 4 3 2 1 0 (LSB) 4695307576Sgonzo * ------------------------------------------- 4696349541Swulf * ipacket[0]: 0 0 SY SX 0 M R L 4697307576Sgonzo * ipacket[1]: ~SX 0 0 0 0 0 0 0 4698307576Sgonzo * ipacket[2]: ~SY 0 0 0 0 0 0 0 4699307576Sgonzo * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0 4700307576Sgonzo * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 4701307576Sgonzo * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 4702307576Sgonzo * ------------------------------------------- 4703307576Sgonzo * X and Y are written in two's complement spread 4704307576Sgonzo * over 9 bits with SX/SY the relative top bit and 4705307576Sgonzo * X7..X0 and Y7..Y0 the lower bits. 4706307576Sgonzo */ 4707349541Swulf if (!(pb->ipacket[0] & 0xC8) && !(pb->ipacket[1] & 0x7F) && 4708349541Swulf !(pb->ipacket[2] & 0x7F) && !(pb->ipacket[3] & 0xC9) && 4709349541Swulf !(pb->ipacket[0] & 0x10) != !(pb->ipacket[1] & 0x80) && 4710349541Swulf !(pb->ipacket[0] & 0x10) != !(pb->ipacket[3] & 0x10) && 4711349541Swulf !(pb->ipacket[0] & 0x20) != !(pb->ipacket[2] & 0x80) && 4712349541Swulf !(pb->ipacket[0] & 0x20) != !(pb->ipacket[3] & 0x20)) { 4713307576Sgonzo 4714349541Swulf *x = (pb->ipacket[0] & MOUSE_PS2_XNEG) ? 4715349541Swulf pb->ipacket[4] - 256 : pb->ipacket[4]; 4716349541Swulf *y = (pb->ipacket[0] & MOUSE_PS2_YNEG) ? 4717349541Swulf pb->ipacket[5] - 256 : pb->ipacket[5]; 4718349541Swulf 4719349541Swulf trackpoint_button = 4720349541Swulf ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) | 4721349541Swulf ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) | 4722349541Swulf ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0); 4723321060Swulf#ifdef EVDEV_SUPPORT 4724349541Swulf evdev_push_rel(sc->evdev_r, REL_X, *x); 4725349541Swulf evdev_push_rel(sc->evdev_r, REL_Y, -*y); 4726349541Swulf evdev_push_mouse_btn(sc->evdev_r, trackpoint_button); 4727349541Swulf evdev_sync(sc->evdev_r); 4728321060Swulf#endif 4729349541Swulf ms->button = touchpad_button | trackpoint_button; 4730349541Swulf } else 4731349541Swulf VLOG(3, (LOG_DEBUG, "elantech: " 4732349541Swulf "unexpected trackpoint packet skipped\n")); 4733307576Sgonzo return (0); 4734307576Sgonzo 4735307576Sgonzo case ELANTECH_PKT_NOP: 4736307576Sgonzo return (0); 4737307576Sgonzo 4738307576Sgonzo default: 4739307576Sgonzo return (-1); 4740307576Sgonzo } 4741307576Sgonzo 4742307576Sgonzo for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 4743307576Sgonzo if (PSM_FINGER_IS_SET(f[id])) 4744307576Sgonzo VLOG(2, (LOG_DEBUG, "elantech: " 4745307576Sgonzo "finger %d: down [%d, %d], %d, %d, %d\n", id + 1, 4746307576Sgonzo f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags)); 4747307576Sgonzo 4748307576Sgonzo /* Touchpad button presses */ 4749307576Sgonzo if (sc->elanhw.isclickpad) { 4750307576Sgonzo touchpad_button = 4751307576Sgonzo ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0); 4752307576Sgonzo } else { 4753307576Sgonzo touchpad_button = 4754307576Sgonzo ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) | 4755307576Sgonzo ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0); 4756307576Sgonzo } 4757307576Sgonzo 4758321060Swulf#ifdef EVDEV_SUPPORT 4759321060Swulf if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) { 4760321060Swulf for (id = 0; id < ELANTECH_MAX_FINGERS; id++) { 4761321060Swulf if (PSM_FINGER_IS_SET(f[id])) { 4762321060Swulf psm_push_mt_finger(sc, id, &f[id]); 4763321060Swulf /* Convert touch width to surface units */ 4764321060Swulf evdev_push_abs(sc->evdev_a, ABS_MT_TOUCH_MAJOR, 4765321060Swulf f[id].w * sc->elanhw.dptracex); 4766321060Swulf } 4767321060Swulf if (sc->elanaction.mask & (1 << id) && 4768321060Swulf !(mask & (1 << id))) 4769321060Swulf psm_release_mt_slot(sc->evdev_a, id); 4770321060Swulf } 4771321060Swulf evdev_push_key(sc->evdev_a, BTN_TOUCH, nfingers > 0); 4772321060Swulf evdev_push_nfingers(sc->evdev_a, nfingers); 4773321060Swulf if (nfingers > 0) { 4774321060Swulf if (PSM_FINGER_IS_SET(f[0])) 4775321060Swulf psm_push_st_finger(sc, &f[0]); 4776321060Swulf } else 4777321060Swulf evdev_push_abs(sc->evdev_a, ABS_PRESSURE, 0); 4778321060Swulf evdev_push_mouse_btn(sc->evdev_a, touchpad_button); 4779321060Swulf evdev_sync(sc->evdev_a); 4780321060Swulf } 4781321060Swulf#endif 4782321060Swulf 4783307576Sgonzo ms->button = touchpad_button | trackpoint_button; 4784307576Sgonzo 4785349541Swulf /* Palm detection doesn't terminate the current action. */ 4786349541Swulf palm = psmpalmdetect(sc, &f[0], nfingers); 4787349541Swulf 4788318500Swulf /* Send finger 1 position to gesture processor */ 4789349541Swulf if ((PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) || 4790349541Swulf nfingers == 0) && !palm) 4791318500Swulf psmgestures(sc, &f[0], imin(nfingers, 3), ms); 4792349541Swulf 4793318500Swulf /* Send fingers positions to movement smoothers */ 4794318500Swulf for (id = 0; id < PSM_FINGERS; id++) 4795318500Swulf if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id))) 4796318500Swulf psmsmoother(sc, &f[id], id, ms, x, y); 4797307576Sgonzo 4798307576Sgonzo /* Store current finger positions in action context */ 4799307576Sgonzo for (id = 0; id < ELANTECH_MAX_FINGERS; id++) { 4800307576Sgonzo if (PSM_FINGER_IS_SET(f[id])) 4801307576Sgonzo sc->elanaction.fingers[id] = f[id]; 4802307576Sgonzo if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id))) 4803307576Sgonzo PSM_FINGER_RESET(sc->elanaction.fingers[id]); 4804307576Sgonzo } 4805307576Sgonzo sc->elanaction.mask = mask; 4806307576Sgonzo 4807349541Swulf if (palm) { 4808318500Swulf *x = *y = *z = 0; 4809318500Swulf ms->button = ms->obutton; 4810318500Swulf return (0); 4811318500Swulf } 4812318500Swulf 4813307576Sgonzo /* Use the extra buttons as a scrollwheel */ 4814307576Sgonzo if (ms->button & MOUSE_BUTTON4DOWN) 4815178019Sjkim *z = -1; 4816307576Sgonzo else if (ms->button & MOUSE_BUTTON5DOWN) 4817178019Sjkim *z = 1; 4818307576Sgonzo else if (ms->button & MOUSE_BUTTON6DOWN) 4819183888Sdumbbell *z = -2; 4820307576Sgonzo else if (ms->button & MOUSE_BUTTON7DOWN) 4821183888Sdumbbell *z = 2; 4822307576Sgonzo else 4823178019Sjkim *z = 0; 4824307576Sgonzo ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN | 4825307576Sgonzo MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN); 4826178019Sjkim 4827178019Sjkim return (0); 4828178019Sjkim} 4829178019Sjkim 4830178019Sjkimstatic void 4831178019Sjkimproc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 4832178019Sjkim int *x, int *y, int *z) 4833178019Sjkim{ 4834178019Sjkim static int butmap_versapad[8] = { 4835178019Sjkim 0, 4836178019Sjkim MOUSE_BUTTON3DOWN, 4837178019Sjkim 0, 4838178019Sjkim MOUSE_BUTTON3DOWN, 4839178019Sjkim MOUSE_BUTTON1DOWN, 4840178019Sjkim MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 4841178019Sjkim MOUSE_BUTTON1DOWN, 4842178019Sjkim MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 4843178019Sjkim }; 4844178019Sjkim int c, x0, y0; 4845178019Sjkim 4846178019Sjkim /* VersaPad PS/2 absolute mode message format 4847178019Sjkim * 4848178019Sjkim * [packet1] 7 6 5 4 3 2 1 0(LSB) 4849178019Sjkim * ipacket[0]: 1 1 0 A 1 L T R 4850178019Sjkim * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 4851178019Sjkim * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 4852178019Sjkim * ipacket[3]: 1 1 1 A 1 L T R 4853178019Sjkim * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 4854178019Sjkim * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 4855178019Sjkim * 4856178019Sjkim * [note] 4857178019Sjkim * R: right physical mouse button (1=on) 4858178019Sjkim * T: touch pad virtual button (1=tapping) 4859178019Sjkim * L: left physical mouse button (1=on) 4860178019Sjkim * A: position data is valid (1=valid) 4861178019Sjkim * H: horizontal data (12bit signed integer. H11 is sign bit.) 4862178019Sjkim * V: vertical data (12bit signed integer. V11 is sign bit.) 4863178019Sjkim * P: pressure data 4864178019Sjkim * 4865178019Sjkim * Tapping is mapped to MOUSE_BUTTON4. 4866178019Sjkim */ 4867178019Sjkim c = pb->ipacket[0]; 4868178019Sjkim *x = *y = 0; 4869178019Sjkim ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 4870178019Sjkim ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 4871178019Sjkim if (c & MOUSE_PS2VERSA_IN_USE) { 4872178019Sjkim x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8); 4873178019Sjkim y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4); 4874178019Sjkim if (x0 & 0x800) 4875178019Sjkim x0 -= 0x1000; 4876178019Sjkim if (y0 & 0x800) 4877178019Sjkim y0 -= 0x1000; 4878178019Sjkim if (sc->flags & PSM_FLAGS_FINGERDOWN) { 4879178019Sjkim *x = sc->xold - x0; 4880178019Sjkim *y = y0 - sc->yold; 4881178019Sjkim if (*x < 0) /* XXX */ 4882178019Sjkim ++*x; 4883178019Sjkim else if (*x) 4884178019Sjkim --*x; 4885178019Sjkim if (*y < 0) 4886178019Sjkim ++*y; 4887178019Sjkim else if (*y) 4888178019Sjkim --*y; 4889178019Sjkim } else 4890178019Sjkim sc->flags |= PSM_FLAGS_FINGERDOWN; 4891178019Sjkim sc->xold = x0; 4892178019Sjkim sc->yold = y0; 4893178019Sjkim } else 4894178019Sjkim sc->flags &= ~PSM_FLAGS_FINGERDOWN; 4895178019Sjkim} 4896178019Sjkim 4897178019Sjkimstatic void 4898307576Sgonzopsmsoftintridle(void *arg) 4899307576Sgonzo{ 4900307576Sgonzo struct psm_softc *sc = arg; 4901307576Sgonzo packetbuf_t *pb; 4902307576Sgonzo 4903307576Sgonzo /* Invoke soft handler only when pqueue is empty. Otherwise it will be 4904307576Sgonzo * invoked from psmintr soon with pqueue filled with real data */ 4905307576Sgonzo if (sc->pqueue_start == sc->pqueue_end && 4906307576Sgonzo sc->idlepacket.inputbytes > 0) { 4907307576Sgonzo /* Grow circular queue backwards to avoid race with psmintr */ 4908307576Sgonzo if (--sc->pqueue_start < 0) 4909307576Sgonzo sc->pqueue_start = PSM_PACKETQUEUE - 1; 4910307576Sgonzo 4911307576Sgonzo pb = &sc->pqueue[sc->pqueue_start]; 4912307576Sgonzo memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t)); 4913307576Sgonzo VLOG(4, (LOG_DEBUG, 4914307576Sgonzo "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n", 4915307576Sgonzo pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 4916307576Sgonzo pb->ipacket[3], pb->ipacket[4], pb->ipacket[5])); 4917307576Sgonzo 4918307576Sgonzo psmsoftintr(arg); 4919307576Sgonzo } 4920307576Sgonzo} 4921307576Sgonzo 4922307576Sgonzostatic void 4923178019Sjkimpsmsoftintr(void *arg) 4924178019Sjkim{ 4925178019Sjkim /* 4926178019Sjkim * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 4927178019Sjkim * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 4928178019Sjkim */ 4929178019Sjkim static int butmap[8] = { 4930178019Sjkim 0, 4931178019Sjkim MOUSE_BUTTON1DOWN, 4932178019Sjkim MOUSE_BUTTON3DOWN, 4933178019Sjkim MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 4934178019Sjkim MOUSE_BUTTON2DOWN, 4935178019Sjkim MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 4936178019Sjkim MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 4937178019Sjkim MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 4938178019Sjkim }; 4939212355Sed struct psm_softc *sc = arg; 4940178019Sjkim mousestatus_t ms; 4941178019Sjkim packetbuf_t *pb; 4942178019Sjkim int x, y, z, c, l, s; 4943178019Sjkim 4944178019Sjkim getmicrouptime(&sc->lastsoftintr); 4945178019Sjkim 4946178019Sjkim s = spltty(); 4947178019Sjkim 4948178019Sjkim do { 4949178019Sjkim pb = &sc->pqueue[sc->pqueue_start]; 4950178019Sjkim 4951178019Sjkim if (sc->mode.level == PSM_LEVEL_NATIVE) 4952178019Sjkim goto next_native; 4953178019Sjkim 4954178019Sjkim c = pb->ipacket[0]; 4955178019Sjkim /* 4956178019Sjkim * A kludge for Kensington device! 4957178019Sjkim * The MSB of the horizontal count appears to be stored in 4958178019Sjkim * a strange place. 4959178019Sjkim */ 4960178019Sjkim if (sc->hw.model == MOUSE_MODEL_THINK) 4961178019Sjkim pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 4962178019Sjkim 4963178019Sjkim /* ignore the overflow bits... */ 4964178019Sjkim x = (c & MOUSE_PS2_XNEG) ? 4965178019Sjkim pb->ipacket[1] - 256 : pb->ipacket[1]; 4966178019Sjkim y = (c & MOUSE_PS2_YNEG) ? 4967178019Sjkim pb->ipacket[2] - 256 : pb->ipacket[2]; 4968133296Sphilip z = 0; 4969178019Sjkim ms.obutton = sc->button; /* previous button state */ 4970178019Sjkim ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 4971178019Sjkim /* `tapping' action */ 4972178019Sjkim if (sc->config & PSM_CONFIG_FORCETAP) 4973178019Sjkim ms.button |= ((c & MOUSE_PS2_TAP)) ? 4974178019Sjkim 0 : MOUSE_BUTTON4DOWN; 4975307576Sgonzo timevalclear(&sc->idletimeout); 4976307576Sgonzo sc->idlepacket.inputbytes = 0; 4977133296Sphilip 4978178019Sjkim switch (sc->hw.model) { 4979132865Snjl 4980178019Sjkim case MOUSE_MODEL_EXPLORER: 4981178019Sjkim /* 4982178019Sjkim * b7 b6 b5 b4 b3 b2 b1 b0 4983178019Sjkim * byte 1: oy ox sy sx 1 M R L 4984178019Sjkim * byte 2: x x x x x x x x 4985178019Sjkim * byte 3: y y y y y y y y 4986178019Sjkim * byte 4: * * S2 S1 s d2 d1 d0 4987178019Sjkim * 4988178019Sjkim * L, M, R, S1, S2: left, middle, right and side buttons 4989178019Sjkim * s: wheel data sign bit 4990178019Sjkim * d2-d0: wheel data 4991178019Sjkim */ 4992178019Sjkim z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ? 4993178019Sjkim (pb->ipacket[3] & 0x0f) - 16 : 4994178019Sjkim (pb->ipacket[3] & 0x0f); 4995178019Sjkim ms.button |= 4996178019Sjkim (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ? 4997178019Sjkim MOUSE_BUTTON4DOWN : 0; 4998178019Sjkim ms.button |= 4999178019Sjkim (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ? 5000178019Sjkim MOUSE_BUTTON5DOWN : 0; 5001178019Sjkim break; 5002178019Sjkim 5003178019Sjkim case MOUSE_MODEL_INTELLI: 5004178019Sjkim case MOUSE_MODEL_NET: 5005178019Sjkim /* wheel data is in the fourth byte */ 5006178019Sjkim z = (char)pb->ipacket[3]; 5007178019Sjkim /* 5008178019Sjkim * XXX some mice may send 7 when there is no Z movement? */ 5009178019Sjkim if ((z >= 7) || (z <= -7)) 5010178019Sjkim z = 0; 5011178019Sjkim /* some compatible mice have additional buttons */ 5012178019Sjkim ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ? 5013178019Sjkim MOUSE_BUTTON4DOWN : 0; 5014178019Sjkim ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ? 5015178019Sjkim MOUSE_BUTTON5DOWN : 0; 5016178019Sjkim break; 5017178019Sjkim 5018178019Sjkim case MOUSE_MODEL_MOUSEMANPLUS: 5019178019Sjkim proc_mmanplus(sc, pb, &ms, &x, &y, &z); 5020178019Sjkim break; 5021178019Sjkim 5022178019Sjkim case MOUSE_MODEL_GLIDEPOINT: 5023178019Sjkim /* `tapping' action */ 5024178019Sjkim ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : 5025178019Sjkim MOUSE_BUTTON4DOWN; 5026178019Sjkim break; 5027178019Sjkim 5028178019Sjkim case MOUSE_MODEL_NETSCROLL: 5029178019Sjkim /* 5030298955Spfg * three additional bytes encode buttons and 5031178019Sjkim * wheel events 5032178019Sjkim */ 5033178019Sjkim ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ? 5034178019Sjkim MOUSE_BUTTON4DOWN : 0; 5035178019Sjkim ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ? 5036178019Sjkim MOUSE_BUTTON5DOWN : 0; 5037178019Sjkim z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ? 5038178019Sjkim pb->ipacket[4] - 256 : pb->ipacket[4]; 5039178019Sjkim break; 5040178019Sjkim 5041178019Sjkim case MOUSE_MODEL_THINK: 5042178019Sjkim /* the fourth button state in the first byte */ 5043178019Sjkim ms.button |= (c & MOUSE_PS2_TAP) ? 5044178019Sjkim MOUSE_BUTTON4DOWN : 0; 5045178019Sjkim break; 5046178019Sjkim 5047178019Sjkim case MOUSE_MODEL_VERSAPAD: 5048178019Sjkim proc_versapad(sc, pb, &ms, &x, &y, &z); 5049178019Sjkim c = ((x < 0) ? MOUSE_PS2_XNEG : 0) | 5050178019Sjkim ((y < 0) ? MOUSE_PS2_YNEG : 0); 5051178019Sjkim break; 5052281440Srpaulo 5053178019Sjkim case MOUSE_MODEL_4D: 5054178019Sjkim /* 5055178019Sjkim * b7 b6 b5 b4 b3 b2 b1 b0 5056178019Sjkim * byte 1: s2 d2 s1 d1 1 M R L 5057178019Sjkim * byte 2: sx x x x x x x x 5058178019Sjkim * byte 3: sy y y y y y y y 5059178019Sjkim * 5060178019Sjkim * s1: wheel 1 direction 5061178019Sjkim * d1: wheel 1 data 5062178019Sjkim * s2: wheel 2 direction 5063178019Sjkim * d2: wheel 2 data 5064178019Sjkim */ 5065178019Sjkim x = (pb->ipacket[1] & 0x80) ? 5066178019Sjkim pb->ipacket[1] - 256 : pb->ipacket[1]; 5067178019Sjkim y = (pb->ipacket[2] & 0x80) ? 5068178019Sjkim pb->ipacket[2] - 256 : pb->ipacket[2]; 5069178019Sjkim switch (c & MOUSE_4D_WHEELBITS) { 5070178019Sjkim case 0x10: 5071178019Sjkim z = 1; 5072178019Sjkim break; 5073178019Sjkim case 0x30: 5074178019Sjkim z = -1; 5075178019Sjkim break; 5076178019Sjkim case 0x40: /* XXX 2nd wheel turning right */ 5077178019Sjkim z = 2; 5078178019Sjkim break; 5079178019Sjkim case 0xc0: /* XXX 2nd wheel turning left */ 5080178019Sjkim z = -2; 5081178019Sjkim break; 5082178019Sjkim } 5083178019Sjkim break; 5084178019Sjkim 5085178019Sjkim case MOUSE_MODEL_4DPLUS: 5086178019Sjkim if ((x < 16 - 256) && (y < 16 - 256)) { 5087178019Sjkim /* 5088178019Sjkim * b7 b6 b5 b4 b3 b2 b1 b0 5089178019Sjkim * byte 1: 0 0 1 1 1 M R L 5090178019Sjkim * byte 2: 0 0 0 0 1 0 0 0 5091178019Sjkim * byte 3: 0 0 0 0 S s d1 d0 5092178019Sjkim * 5093178019Sjkim * L, M, R, S: left, middle, right, 5094178019Sjkim * and side buttons 5095178019Sjkim * s: wheel data sign bit 5096178019Sjkim * d1-d0: wheel data 5097178019Sjkim */ 5098178019Sjkim x = y = 0; 5099178019Sjkim if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 5100178019Sjkim ms.button |= MOUSE_BUTTON4DOWN; 5101178019Sjkim z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ? 5102178019Sjkim ((pb->ipacket[2] & 0x07) - 8) : 5103178019Sjkim (pb->ipacket[2] & 0x07) ; 5104178019Sjkim } else { 5105178019Sjkim /* preserve previous button states */ 5106178019Sjkim ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 5107178019Sjkim } 5108178019Sjkim break; 5109178019Sjkim 5110178019Sjkim case MOUSE_MODEL_SYNAPTICS: 5111343158Swulf if (pb->inputbytes == MOUSE_PS2_PACKETSIZE) 5112343158Swulf if (proc_synaptics_mux(sc, pb)) 5113343158Swulf goto next; 5114343158Swulf 5115329532Swulf if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) { 5116329532Swulf VLOG(3, (LOG_DEBUG, "synaptics: " 5117329532Swulf "packet rejected\n")); 5118178019Sjkim goto next; 5119329532Swulf } 5120178019Sjkim break; 5121178019Sjkim 5122307576Sgonzo case MOUSE_MODEL_ELANTECH: 5123329532Swulf if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) { 5124329532Swulf VLOG(3, (LOG_DEBUG, "elantech: " 5125329532Swulf "packet rejected\n")); 5126307576Sgonzo goto next; 5127329532Swulf } 5128307576Sgonzo break; 5129307576Sgonzo 5130248478Sjkim case MOUSE_MODEL_TRACKPOINT: 5131178019Sjkim case MOUSE_MODEL_GENERIC: 5132178019Sjkim default: 5133178019Sjkim break; 5134178019Sjkim } 5135178019Sjkim 5136321060Swulf#ifdef EVDEV_SUPPORT 5137321060Swulf if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE && 5138321060Swulf sc->hw.model != MOUSE_MODEL_ELANTECH && 5139321060Swulf sc->hw.model != MOUSE_MODEL_SYNAPTICS) { 5140334763Shselasky evdev_push_rel(sc->evdev_r, REL_X, x); 5141334763Shselasky evdev_push_rel(sc->evdev_r, REL_Y, -y); 5142321060Swulf 5143321060Swulf switch (sc->hw.model) { 5144321060Swulf case MOUSE_MODEL_EXPLORER: 5145321060Swulf case MOUSE_MODEL_INTELLI: 5146321060Swulf case MOUSE_MODEL_NET: 5147321060Swulf case MOUSE_MODEL_NETSCROLL: 5148321060Swulf case MOUSE_MODEL_4DPLUS: 5149321060Swulf evdev_push_rel(sc->evdev_r, REL_WHEEL, -z); 5150321060Swulf break; 5151321060Swulf case MOUSE_MODEL_MOUSEMANPLUS: 5152321060Swulf case MOUSE_MODEL_4D: 5153321060Swulf switch (z) { 5154321060Swulf case 1: 5155321060Swulf case -1: 5156321060Swulf evdev_push_rel(sc->evdev_r, REL_WHEEL, -z); 5157321060Swulf break; 5158321060Swulf case 2: 5159321060Swulf case -2: 5160321060Swulf evdev_push_rel(sc->evdev_r, REL_HWHEEL, z / 2); 5161321060Swulf break; 5162321060Swulf } 5163321060Swulf break; 5164321060Swulf } 5165321060Swulf 5166321060Swulf evdev_push_mouse_btn(sc->evdev_r, ms.button); 5167321060Swulf evdev_sync(sc->evdev_r); 5168321060Swulf } 5169321060Swulf#endif 5170321060Swulf 5171178019Sjkim /* scale values */ 5172178019Sjkim if (sc->mode.accelfactor >= 1) { 5173178019Sjkim if (x != 0) { 5174178019Sjkim x = x * x / sc->mode.accelfactor; 5175178019Sjkim if (x == 0) 5176178019Sjkim x = 1; 5177178019Sjkim if (c & MOUSE_PS2_XNEG) 5178178019Sjkim x = -x; 5179178019Sjkim } 5180178019Sjkim if (y != 0) { 5181178019Sjkim y = y * y / sc->mode.accelfactor; 5182178019Sjkim if (y == 0) 5183178019Sjkim y = 1; 5184178019Sjkim if (c & MOUSE_PS2_YNEG) 5185178019Sjkim y = -y; 5186178019Sjkim } 518741016Sdfr } 518841016Sdfr 5189307576Sgonzo /* Store last packet for reinjection if it has not been set already */ 5190307576Sgonzo if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0) 5191307576Sgonzo sc->idlepacket = *pb; 5192307576Sgonzo 5193178019Sjkim ms.dx = x; 5194178019Sjkim ms.dy = y; 5195178019Sjkim ms.dz = z; 5196178019Sjkim ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) | 5197178019Sjkim (ms.obutton ^ ms.button); 519841016Sdfr 5199178017Sjkim pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket); 520041016Sdfr 5201178019Sjkim sc->status.flags |= ms.flags; 5202178019Sjkim sc->status.dx += ms.dx; 5203178019Sjkim sc->status.dy += ms.dy; 5204178019Sjkim sc->status.dz += ms.dz; 5205178019Sjkim sc->status.button = ms.button; 5206178019Sjkim sc->button = ms.button; 520741016Sdfr 5208178019Sjkimnext_native: 520958230Syokota sc->watchdog = FALSE; 521058230Syokota 5211178019Sjkim /* queue data */ 5212178019Sjkim if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) { 5213178019Sjkim l = imin(pb->inputbytes, 5214178019Sjkim sizeof(sc->queue.buf) - sc->queue.tail); 5215178019Sjkim bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 5216178019Sjkim if (pb->inputbytes > l) 5217178019Sjkim bcopy(&pb->ipacket[l], &sc->queue.buf[0], 5218178019Sjkim pb->inputbytes - l); 5219178019Sjkim sc->queue.tail = (sc->queue.tail + pb->inputbytes) % 5220178019Sjkim sizeof(sc->queue.buf); 5221178019Sjkim sc->queue.count += pb->inputbytes; 522241016Sdfr } 522341016Sdfr 5224178019Sjkimnext: 5225329532Swulf pb->inputbytes = 0; 5226123442Salfred if (++sc->pqueue_start >= PSM_PACKETQUEUE) 5227123442Salfred sc->pqueue_start = 0; 5228178019Sjkim } while (sc->pqueue_start != sc->pqueue_end); 5229178019Sjkim 5230178019Sjkim if (sc->state & PSM_ASLP) { 5231178019Sjkim sc->state &= ~PSM_ASLP; 5232178019Sjkim wakeup(sc); 5233178019Sjkim } 5234178019Sjkim selwakeuppri(&sc->rsel, PZERO); 5235189870Srnoland if (sc->async != NULL) { 5236189870Srnoland pgsigio(&sc->async, SIGIO, 0); 5237189870Srnoland } 5238178019Sjkim sc->state &= ~PSM_SOFTARMED; 5239307576Sgonzo 5240307576Sgonzo /* schedule injection of predefined packet after idletimeout 5241307576Sgonzo * if no data packets have been received from psmintr */ 5242307576Sgonzo if (timevalisset(&sc->idletimeout)) { 5243307576Sgonzo sc->state |= PSM_SOFTARMED; 5244307576Sgonzo callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout), 5245307576Sgonzo psmsoftintridle, sc); 5246307576Sgonzo VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n", 5247307576Sgonzo tvtohz(&sc->idletimeout))); 5248307576Sgonzo } 5249178019Sjkim splx(s); 525041016Sdfr} 525141016Sdfr 525241016Sdfrstatic int 5253130585Sphkpsmpoll(struct cdev *dev, int events, struct thread *td) 525441016Sdfr{ 5255212355Sed struct psm_softc *sc = dev->si_drv1; 5256178019Sjkim int s; 5257178019Sjkim int revents = 0; 525841016Sdfr 5259178019Sjkim /* Return true if a mouse event available */ 5260178019Sjkim s = spltty(); 5261178019Sjkim if (events & (POLLIN | POLLRDNORM)) { 5262178019Sjkim if (sc->queue.count > 0) 5263178019Sjkim revents |= events & (POLLIN | POLLRDNORM); 5264178019Sjkim else 5265178019Sjkim selrecord(td, &sc->rsel); 5266178019Sjkim } 5267178019Sjkim splx(s); 526841016Sdfr 5269178019Sjkim return (revents); 527041016Sdfr} 527141016Sdfr 527241016Sdfr/* vendor/model specific routines */ 527341016Sdfr 527441016Sdfrstatic int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 527541016Sdfr{ 5276178019Sjkim if (set_mouse_resolution(kbdc, res) != res) 5277178019Sjkim return (FALSE); 5278178019Sjkim if (set_mouse_scaling(kbdc, scale) && 5279178019Sjkim set_mouse_scaling(kbdc, scale) && 5280178019Sjkim set_mouse_scaling(kbdc, scale) && 5281178019Sjkim (get_mouse_status(kbdc, status, 0, 3) >= 3)) 5282178019Sjkim return (TRUE); 5283178019Sjkim return (FALSE); 528441016Sdfr} 528541016Sdfr 5286178019Sjkimstatic int 528769438Syokotamouse_ext_command(KBDC kbdc, int command) 528869438Syokota{ 5289178019Sjkim int c; 529069438Syokota 5291178019Sjkim c = (command >> 6) & 0x03; 5292178019Sjkim if (set_mouse_resolution(kbdc, c) != c) 5293178019Sjkim return (FALSE); 5294178019Sjkim c = (command >> 4) & 0x03; 5295178019Sjkim if (set_mouse_resolution(kbdc, c) != c) 5296178019Sjkim return (FALSE); 5297178019Sjkim c = (command >> 2) & 0x03; 5298178019Sjkim if (set_mouse_resolution(kbdc, c) != c) 5299178019Sjkim return (FALSE); 5300178019Sjkim c = (command >> 0) & 0x03; 5301178019Sjkim if (set_mouse_resolution(kbdc, c) != c) 5302178019Sjkim return (FALSE); 5303178019Sjkim return (TRUE); 530469438Syokota} 530569438Syokota 5306153072Sru#ifdef notyet 530741016Sdfr/* Logitech MouseMan Cordless II */ 530841016Sdfrstatic int 5309284320Sglebiusenable_lcordless(struct psm_softc *sc, enum probearg arg) 531041016Sdfr{ 5311284320Sglebius KBDC kbdc = sc->kbdc; 5312178019Sjkim int status[3]; 5313178019Sjkim int ch; 531441016Sdfr 5315233580Sjkim if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status)) 5316178019Sjkim return (FALSE); 5317178019Sjkim if (status[1] == PSMD_RES_HIGH) 5318178019Sjkim return (FALSE); 5319178019Sjkim ch = (status[0] & 0x07) - 1; /* channel # */ 5320178019Sjkim if ((ch <= 0) || (ch > 4)) 5321178019Sjkim return (FALSE); 5322178019Sjkim /* 5323178019Sjkim * status[1]: always one? 5324178019Sjkim * status[2]: battery status? (0-100) 5325178019Sjkim */ 5326178019Sjkim return (TRUE); 532741016Sdfr} 532841016Sdfr#endif /* notyet */ 532941016Sdfr 533058230Syokota/* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 533141016Sdfrstatic int 5332284320Sglebiusenable_groller(struct psm_softc *sc, enum probearg arg) 533341016Sdfr{ 5334284320Sglebius KBDC kbdc = sc->kbdc; 5335178019Sjkim int status[3]; 533641016Sdfr 5337178019Sjkim /* 5338178019Sjkim * The special sequence to enable the fourth button and the 5339178019Sjkim * roller. Immediately after this sequence check status bytes. 5340178019Sjkim * if the mouse is NetScroll, the second and the third bytes are 5341178019Sjkim * '3' and 'D'. 5342178019Sjkim */ 534341016Sdfr 5344178019Sjkim /* 5345178019Sjkim * If the mouse is an ordinary PS/2 mouse, the status bytes should 5346178019Sjkim * look like the following. 5347178019Sjkim * 5348178019Sjkim * byte 1 bit 7 always 0 5349178019Sjkim * bit 6 stream mode (0) 5350178019Sjkim * bit 5 disabled (0) 5351178019Sjkim * bit 4 1:1 scaling (0) 5352178019Sjkim * bit 3 always 0 5353178019Sjkim * bit 0-2 button status 5354178019Sjkim * byte 2 resolution (PSMD_RES_HIGH) 5355178019Sjkim * byte 3 report rate (?) 5356178019Sjkim */ 535741016Sdfr 5358233580Sjkim if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status)) 5359178019Sjkim return (FALSE); 5360178019Sjkim if ((status[1] != '3') || (status[2] != 'D')) 5361178019Sjkim return (FALSE); 5362178019Sjkim /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 5363284320Sglebius if (arg == PROBE) 5364233580Sjkim sc->hw.buttons = 4; 5365178019Sjkim return (TRUE); 536641016Sdfr} 536741016Sdfr 536858230Syokota/* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 536941016Sdfrstatic int 5370284320Sglebiusenable_gmouse(struct psm_softc *sc, enum probearg arg) 537141016Sdfr{ 5372284320Sglebius KBDC kbdc = sc->kbdc; 5373178019Sjkim int status[3]; 537441016Sdfr 5375178019Sjkim /* 5376178019Sjkim * The special sequence to enable the middle, "rubber" button. 5377178019Sjkim * Immediately after this sequence check status bytes. 5378178019Sjkim * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 5379178019Sjkim * the second and the third bytes are '3' and 'U'. 5380178019Sjkim * NOTE: NetMouse reports that it has three buttons although it has 5381178019Sjkim * two buttons and a rubber button. NetMouse Pro and MIE Mouse 5382178019Sjkim * say they have three buttons too and they do have a button on the 5383178019Sjkim * side... 5384178019Sjkim */ 5385233580Sjkim if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status)) 5386178019Sjkim return (FALSE); 5387178019Sjkim if ((status[1] != '3') || (status[2] != 'U')) 5388178019Sjkim return (FALSE); 5389178019Sjkim return (TRUE); 539041016Sdfr} 539141016Sdfr 539241016Sdfr/* ALPS GlidePoint */ 539341016Sdfrstatic int 5394284320Sglebiusenable_aglide(struct psm_softc *sc, enum probearg arg) 539541016Sdfr{ 5396284320Sglebius KBDC kbdc = sc->kbdc; 5397178019Sjkim int status[3]; 539841016Sdfr 5399178019Sjkim /* 5400178019Sjkim * The special sequence to obtain ALPS GlidePoint specific 5401178019Sjkim * information. Immediately after this sequence, status bytes will 5402178019Sjkim * contain something interesting. 5403178019Sjkim * NOTE: ALPS produces several models of GlidePoint. Some of those 5404178019Sjkim * do not respond to this sequence, thus, cannot be detected this way. 5405178019Sjkim */ 5406233580Sjkim if (set_mouse_sampling_rate(kbdc, 100) != 100) 5407178019Sjkim return (FALSE); 5408233580Sjkim if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status)) 5409178019Sjkim return (FALSE); 5410178019Sjkim if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 5411178019Sjkim return (FALSE); 5412178019Sjkim return (TRUE); 541341016Sdfr} 541441016Sdfr 541541016Sdfr/* Kensington ThinkingMouse/Trackball */ 541641016Sdfrstatic int 5417284320Sglebiusenable_kmouse(struct psm_softc *sc, enum probearg arg) 541841016Sdfr{ 5419178019Sjkim static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 5420284320Sglebius KBDC kbdc = sc->kbdc; 5421178019Sjkim int status[3]; 5422178019Sjkim int id1; 5423178019Sjkim int id2; 5424178019Sjkim int i; 542541016Sdfr 5426178019Sjkim id1 = get_aux_id(kbdc); 5427178019Sjkim if (set_mouse_sampling_rate(kbdc, 10) != 10) 5428178019Sjkim return (FALSE); 5429178019Sjkim /* 5430178019Sjkim * The device is now in the native mode? It returns a different 5431178019Sjkim * ID value... 5432178019Sjkim */ 5433178019Sjkim id2 = get_aux_id(kbdc); 5434178019Sjkim if ((id1 == id2) || (id2 != 2)) 5435178019Sjkim return (FALSE); 543641016Sdfr 5437178019Sjkim if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 5438178019Sjkim return (FALSE); 543941016Sdfr#if PSM_DEBUG >= 2 5440178019Sjkim /* at this point, resolution is LOW, sampling rate is 10/sec */ 5441178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) < 3) 5442178019Sjkim return (FALSE); 544341016Sdfr#endif 544441016Sdfr 5445178019Sjkim /* 5446178019Sjkim * The special sequence to enable the third and fourth buttons. 5447178019Sjkim * Otherwise they behave like the first and second buttons. 5448178019Sjkim */ 5449298307Spfg for (i = 0; i < nitems(rate); ++i) 5450178019Sjkim if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 5451178019Sjkim return (FALSE); 545241016Sdfr 5453178019Sjkim /* 5454178019Sjkim * At this point, the device is using default resolution and 5455178019Sjkim * sampling rate for the native mode. 5456178019Sjkim */ 5457178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) < 3) 5458178019Sjkim return (FALSE); 5459178019Sjkim if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 5460178019Sjkim return (FALSE); 546141016Sdfr 5462178019Sjkim /* the device appears be enabled by this sequence, diable it for now */ 5463178019Sjkim disable_aux_dev(kbdc); 5464178019Sjkim empty_aux_buffer(kbdc, 5); 546541016Sdfr 5466178019Sjkim return (TRUE); 546741016Sdfr} 546841016Sdfr 546958230Syokota/* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 547041016Sdfrstatic int 5471284320Sglebiusenable_mmanplus(struct psm_softc *sc, enum probearg arg) 547241016Sdfr{ 5473284320Sglebius KBDC kbdc = sc->kbdc; 5474178019Sjkim int data[3]; 547541016Sdfr 5476178019Sjkim /* the special sequence to enable the fourth button and the roller. */ 5477178019Sjkim /* 5478178019Sjkim * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 5479178019Sjkim * must be called exactly three times since the last RESET command 5480178019Sjkim * before this sequence. XXX 5481178019Sjkim */ 5482178019Sjkim if (!set_mouse_scaling(kbdc, 1)) 5483178019Sjkim return (FALSE); 5484178019Sjkim if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 5485178019Sjkim return (FALSE); 5486178019Sjkim if (get_mouse_status(kbdc, data, 1, 3) < 3) 5487178019Sjkim return (FALSE); 548841016Sdfr 5489178019Sjkim /* 5490240743Skevlo * PS2++ protocol, packet type 0 5491178019Sjkim * 5492178019Sjkim * b7 b6 b5 b4 b3 b2 b1 b0 5493178019Sjkim * byte 1: * 1 p3 p2 1 * * * 5494178019Sjkim * byte 2: 1 1 p1 p0 m1 m0 1 0 5495178019Sjkim * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 5496178019Sjkim * 5497178019Sjkim * p3-p0: packet type: 0 5498178019Sjkim * m7-m0: model ID: MouseMan+:0x50, 5499178019Sjkim * FirstMouse+:0x51, 5500178019Sjkim * ScrollPoint:0x58... 5501178019Sjkim */ 5502178019Sjkim /* check constant bits */ 5503178019Sjkim if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 5504178019Sjkim return (FALSE); 5505178019Sjkim if ((data[1] & 0xc3) != 0xc2) 5506178019Sjkim return (FALSE); 5507178019Sjkim /* check d3-d0 in byte 2 */ 5508178019Sjkim if (!MOUSE_PS2PLUS_CHECKBITS(data)) 5509178019Sjkim return (FALSE); 5510178019Sjkim /* check p3-p0 */ 5511178019Sjkim if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 5512178019Sjkim return (FALSE); 551341016Sdfr 5514284320Sglebius if (arg == PROBE) { 5515233580Sjkim sc->hw.hwid &= 0x00ff; 5516233580Sjkim sc->hw.hwid |= data[2] << 8; /* save model ID */ 5517233580Sjkim } 551848778Syokota 5519178019Sjkim /* 5520178019Sjkim * MouseMan+ (or FirstMouse+) is now in its native mode, in which 5521178019Sjkim * the wheel and the fourth button events are encoded in the 5522178019Sjkim * special data packet. The mouse may be put in the IntelliMouse mode 5523178019Sjkim * if it is initialized by the IntelliMouse's method. 5524178019Sjkim */ 5525178019Sjkim return (TRUE); 552641016Sdfr} 552741016Sdfr 552858230Syokota/* MS IntelliMouse Explorer */ 552958230Syokotastatic int 5530284320Sglebiusenable_msexplorer(struct psm_softc *sc, enum probearg arg) 553158230Syokota{ 5532284320Sglebius KBDC kbdc = sc->kbdc; 5533178019Sjkim static u_char rate0[] = { 200, 100, 80, }; 5534178019Sjkim static u_char rate1[] = { 200, 200, 80, }; 5535178019Sjkim int id; 5536178019Sjkim int i; 553758230Syokota 5538178019Sjkim /* 5539178019Sjkim * This is needed for at least A4Tech X-7xx mice - they do not go 5540178019Sjkim * straight to Explorer mode, but need to be set to Intelli mode 5541178019Sjkim * first. 5542178019Sjkim */ 5543284320Sglebius enable_msintelli(sc, arg); 5544176554Srink 5545178019Sjkim /* the special sequence to enable the extra buttons and the roller. */ 5546298307Spfg for (i = 0; i < nitems(rate1); ++i) 5547178019Sjkim if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 5548178019Sjkim return (FALSE); 5549178019Sjkim /* the device will give the genuine ID only after the above sequence */ 5550178019Sjkim id = get_aux_id(kbdc); 5551178019Sjkim if (id != PSM_EXPLORER_ID) 5552178019Sjkim return (FALSE); 555369438Syokota 5554284320Sglebius if (arg == PROBE) { 5555233580Sjkim sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 5556233580Sjkim sc->hw.hwid = id; 5557233580Sjkim } 555869438Syokota 5559178019Sjkim /* 5560178019Sjkim * XXX: this is a kludge to fool some KVM switch products 5561178019Sjkim * which think they are clever enough to know the 4-byte IntelliMouse 5562178019Sjkim * protocol, and assume any other protocols use 3-byte packets. 5563178019Sjkim * They don't convey 4-byte data packets from the IntelliMouse Explorer 5564178019Sjkim * correctly to the host computer because of this! 5565178019Sjkim * The following sequence is actually IntelliMouse's "wake up" 5566178019Sjkim * sequence; it will make the KVM think the mouse is IntelliMouse 5567178019Sjkim * when it is in fact IntelliMouse Explorer. 5568178019Sjkim */ 5569298307Spfg for (i = 0; i < nitems(rate0); ++i) 5570178019Sjkim if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 5571178019Sjkim break; 5572233580Sjkim get_aux_id(kbdc); 557358923Syokota 5574178019Sjkim return (TRUE); 557558230Syokota} 557658230Syokota 5577284320Sglebius/* 5578284320Sglebius * MS IntelliMouse 5579284320Sglebius * Logitech MouseMan+ and FirstMouse+ will also respond to this 5580284320Sglebius * probe routine and act like IntelliMouse. 5581284320Sglebius */ 558241016Sdfrstatic int 5583284320Sglebiusenable_msintelli(struct psm_softc *sc, enum probearg arg) 558441016Sdfr{ 5585284320Sglebius KBDC kbdc = sc->kbdc; 5586178019Sjkim static u_char rate[] = { 200, 100, 80, }; 5587178019Sjkim int id; 5588178019Sjkim int i; 558941016Sdfr 5590178019Sjkim /* the special sequence to enable the third button and the roller. */ 5591298307Spfg for (i = 0; i < nitems(rate); ++i) 5592178019Sjkim if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 5593178019Sjkim return (FALSE); 5594178019Sjkim /* the device will give the genuine ID only after the above sequence */ 5595178019Sjkim id = get_aux_id(kbdc); 5596178019Sjkim if (id != PSM_INTELLI_ID) 5597178019Sjkim return (FALSE); 559841016Sdfr 5599284320Sglebius if (arg == PROBE) { 5600233580Sjkim sc->hw.buttons = 3; 5601233580Sjkim sc->hw.hwid = id; 5602233580Sjkim } 560341016Sdfr 5604178019Sjkim return (TRUE); 560541016Sdfr} 560641016Sdfr 5607284320Sglebius/* 5608284320Sglebius * A4 Tech 4D Mouse 5609284320Sglebius * Newer wheel mice from A4 Tech may use the 4D+ protocol. 5610284320Sglebius */ 561158230Syokotastatic int 5612284320Sglebiusenable_4dmouse(struct psm_softc *sc, enum probearg arg) 561358230Syokota{ 5614178019Sjkim static u_char rate[] = { 200, 100, 80, 60, 40, 20 }; 5615284320Sglebius KBDC kbdc = sc->kbdc; 5616178019Sjkim int id; 5617178019Sjkim int i; 561858230Syokota 5619298307Spfg for (i = 0; i < nitems(rate); ++i) 5620178019Sjkim if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 5621178019Sjkim return (FALSE); 5622178019Sjkim id = get_aux_id(kbdc); 5623178019Sjkim /* 5624178019Sjkim * WinEasy 4D, 4 Way Scroll 4D: 6 5625178019Sjkim * Cable-Free 4D: 8 (4DPLUS) 5626178019Sjkim * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 5627178019Sjkim */ 5628178019Sjkim if (id != PSM_4DMOUSE_ID) 5629178019Sjkim return (FALSE); 563058230Syokota 5631284320Sglebius if (arg == PROBE) { 5632233580Sjkim sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 5633233580Sjkim sc->hw.hwid = id; 5634233580Sjkim } 563558230Syokota 5636178019Sjkim return (TRUE); 563758230Syokota} 563858230Syokota 5639284320Sglebius/* 5640284320Sglebius * A4 Tech 4D+ Mouse 5641284320Sglebius * Newer wheel mice from A4 Tech seem to use this protocol. 5642284320Sglebius * Older models are recognized as either 4D Mouse or IntelliMouse. 5643284320Sglebius */ 564458230Syokotastatic int 5645284320Sglebiusenable_4dplus(struct psm_softc *sc, enum probearg arg) 564658230Syokota{ 5647284320Sglebius KBDC kbdc = sc->kbdc; 5648178019Sjkim int id; 564958230Syokota 5650178019Sjkim /* 5651178019Sjkim * enable_4dmouse() already issued the following ID sequence... 5652178019Sjkim static u_char rate[] = { 200, 100, 80, 60, 40, 20 }; 5653178019Sjkim int i; 565458230Syokota 5655178019Sjkim for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) 5656178019Sjkim if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 5657178019Sjkim return (FALSE); 5658178019Sjkim */ 565958230Syokota 5660178019Sjkim id = get_aux_id(kbdc); 5661178019Sjkim switch (id) { 5662178019Sjkim case PSM_4DPLUS_ID: 5663178019Sjkim break; 5664178019Sjkim case PSM_4DPLUS_RFSW35_ID: 5665178019Sjkim break; 5666178019Sjkim default: 5667178019Sjkim return (FALSE); 5668178019Sjkim } 566958230Syokota 5670284320Sglebius if (arg == PROBE) { 5671233580Sjkim sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3; 5672233580Sjkim sc->hw.hwid = id; 5673233580Sjkim } 567458230Syokota 5675178019Sjkim return (TRUE); 567658230Syokota} 567758230Syokota 5678132865Snjl/* Synaptics Touchpad */ 5679132865Snjlstatic int 5680183888Sdumbbellsynaptics_sysctl(SYSCTL_HANDLER_ARGS) 5681132865Snjl{ 5682307576Sgonzo struct psm_softc *sc; 5683183888Sdumbbell int error, arg; 5684132865Snjl 5685307576Sgonzo if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 || 5686349541Swulf oidp->oid_arg2 > SYNAPTICS_SYSCTL_LAST) 5687307576Sgonzo return (EINVAL); 5688307576Sgonzo 5689307576Sgonzo sc = oidp->oid_arg1; 5690307576Sgonzo 5691183888Sdumbbell /* Read the current value. */ 5692307576Sgonzo arg = *(int *)((char *)sc + oidp->oid_arg2); 5693183888Sdumbbell error = sysctl_handle_int(oidp, &arg, 0, req); 5694135945Sphilip 5695183888Sdumbbell /* Sanity check. */ 5696183888Sdumbbell if (error || !req->newptr) 5697183888Sdumbbell return (error); 5698183888Sdumbbell 5699183888Sdumbbell /* 5700183888Sdumbbell * Check that the new value is in the concerned node's range 5701183888Sdumbbell * of values. 5702183888Sdumbbell */ 5703183888Sdumbbell switch (oidp->oid_arg2) { 5704183888Sdumbbell case SYNAPTICS_SYSCTL_MIN_PRESSURE: 5705183888Sdumbbell case SYNAPTICS_SYSCTL_MAX_PRESSURE: 5706183888Sdumbbell if (arg < 0 || arg > 255) 5707183888Sdumbbell return (EINVAL); 5708183888Sdumbbell break; 5709183888Sdumbbell case SYNAPTICS_SYSCTL_MAX_WIDTH: 5710183888Sdumbbell if (arg < 4 || arg > 15) 5711183888Sdumbbell return (EINVAL); 5712183888Sdumbbell break; 5713183888Sdumbbell case SYNAPTICS_SYSCTL_MARGIN_TOP: 5714307576Sgonzo case SYNAPTICS_SYSCTL_MARGIN_BOTTOM: 5715307576Sgonzo case SYNAPTICS_SYSCTL_NA_TOP: 5716307576Sgonzo case SYNAPTICS_SYSCTL_NA_BOTTOM: 5717307576Sgonzo if (arg < 0 || arg > sc->synhw.maximumYCoord) 5718307576Sgonzo return (EINVAL); 5719307576Sgonzo break; 5720307576Sgonzo case SYNAPTICS_SYSCTL_SOFTBUTTON2_X: 5721307576Sgonzo case SYNAPTICS_SYSCTL_SOFTBUTTON3_X: 5722307576Sgonzo /* Softbuttons is clickpad only feature */ 5723307576Sgonzo if (!sc->synhw.capClickPad && arg != 0) 5724307576Sgonzo return (EINVAL); 5725307576Sgonzo /* FALLTHROUGH */ 5726183888Sdumbbell case SYNAPTICS_SYSCTL_MARGIN_RIGHT: 5727183888Sdumbbell case SYNAPTICS_SYSCTL_MARGIN_LEFT: 5728183888Sdumbbell case SYNAPTICS_SYSCTL_NA_RIGHT: 5729183888Sdumbbell case SYNAPTICS_SYSCTL_NA_LEFT: 5730307576Sgonzo if (arg < 0 || arg > sc->synhw.maximumXCoord) 5731183888Sdumbbell return (EINVAL); 5732183888Sdumbbell break; 5733183888Sdumbbell case SYNAPTICS_SYSCTL_WINDOW_MIN: 5734183888Sdumbbell case SYNAPTICS_SYSCTL_WINDOW_MAX: 5735183888Sdumbbell case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE: 5736183888Sdumbbell if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE) 5737183888Sdumbbell return (EINVAL); 5738183888Sdumbbell break; 5739183888Sdumbbell case SYNAPTICS_SYSCTL_MULTIPLICATOR: 5740183888Sdumbbell case SYNAPTICS_SYSCTL_WEIGHT_CURRENT: 5741183888Sdumbbell case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS: 5742183888Sdumbbell case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA: 5743183888Sdumbbell case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED: 5744183888Sdumbbell case SYNAPTICS_SYSCTL_DIV_MIN: 5745183888Sdumbbell case SYNAPTICS_SYSCTL_DIV_MAX: 5746183888Sdumbbell case SYNAPTICS_SYSCTL_DIV_MAX_NA: 5747183888Sdumbbell case SYNAPTICS_SYSCTL_DIV_LEN: 5748183888Sdumbbell case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN: 5749183888Sdumbbell case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX: 5750183888Sdumbbell if (arg < 1) 5751183888Sdumbbell return (EINVAL); 5752183888Sdumbbell break; 5753183888Sdumbbell case SYNAPTICS_SYSCTL_TAP_MAX_DELTA: 5754183888Sdumbbell case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT: 5755183888Sdumbbell case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA: 5756183888Sdumbbell if (arg < 0) 5757183888Sdumbbell return (EINVAL); 5758183888Sdumbbell break; 5759183888Sdumbbell case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA: 5760307576Sgonzo if (arg < -sc->synhw.maximumXCoord || 5761307576Sgonzo arg > sc->synhw.maximumXCoord) 5762307576Sgonzo return (EINVAL); 5763307576Sgonzo break; 5764307576Sgonzo case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y: 5765307576Sgonzo /* Softbuttons is clickpad only feature */ 5766307576Sgonzo if (!sc->synhw.capClickPad && arg != 0) 5767307576Sgonzo return (EINVAL); 5768307576Sgonzo /* FALLTHROUGH */ 5769183888Sdumbbell case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA: 5770307576Sgonzo if (arg < -sc->synhw.maximumYCoord || 5771307576Sgonzo arg > sc->synhw.maximumYCoord) 5772183888Sdumbbell return (EINVAL); 5773183888Sdumbbell break; 5774281441Srpaulo case SYNAPTICS_SYSCTL_TOUCHPAD_OFF: 5775349541Swulf case SYNAPTICS_SYSCTL_NATURAL_SCROLL: 5776281441Srpaulo if (arg < 0 || arg > 1) 5777281441Srpaulo return (EINVAL); 5778281441Srpaulo break; 5779183888Sdumbbell default: 5780183888Sdumbbell return (EINVAL); 5781183888Sdumbbell } 5782183888Sdumbbell 5783183888Sdumbbell /* Update. */ 5784307576Sgonzo *(int *)((char *)sc + oidp->oid_arg2) = arg; 5785183888Sdumbbell 5786183888Sdumbbell return (error); 5787183888Sdumbbell} 5788183888Sdumbbell 5789183888Sdumbbellstatic void 5790307576Sgonzosynaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc) 5791183888Sdumbbell{ 5792307576Sgonzo /* 5793307576Sgonzo * Set predefined sizes for softbuttons. 5794307576Sgonzo * Values are taken to match HP Pavilion dv6 clickpad drawings 5795307576Sgonzo * with thin middle softbutton placed on separator 5796307576Sgonzo */ 5797183888Sdumbbell 5798307576Sgonzo /* hw.psm.synaptics.softbuttons_y */ 5799344165Swulf sc->syninfo.softbuttons_y = sc->synhw.topButtonPad ? -1700 : 1700; 5800307576Sgonzo SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5801307576Sgonzo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5802307576Sgonzo "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5803307576Sgonzo sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y, 5804307576Sgonzo synaptics_sysctl, "I", 5805307576Sgonzo "Vertical size of softbuttons area"); 5806307576Sgonzo 5807307576Sgonzo /* hw.psm.synaptics.softbutton2_x */ 5808307576Sgonzo sc->syninfo.softbutton2_x = 3100; 5809307576Sgonzo SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5810307576Sgonzo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5811307576Sgonzo "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5812307576Sgonzo sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X, 5813307576Sgonzo synaptics_sysctl, "I", 5814307576Sgonzo "Horisontal position of 2-nd softbutton left edge (0-disable)"); 5815307576Sgonzo 5816307576Sgonzo /* hw.psm.synaptics.softbutton3_x */ 5817307576Sgonzo sc->syninfo.softbutton3_x = 3900; 5818307576Sgonzo SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5819307576Sgonzo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5820307576Sgonzo "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5821307576Sgonzo sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X, 5822307576Sgonzo synaptics_sysctl, "I", 5823307576Sgonzo "Horisontal position of 3-rd softbutton left edge (0-disable)"); 5824307576Sgonzo} 5825307576Sgonzo 5826307576Sgonzostatic void 5827307576Sgonzosynaptics_sysctl_create_tree(struct psm_softc *sc, const char *name, 5828307576Sgonzo const char *descr) 5829307576Sgonzo{ 5830307576Sgonzo 5831186218Sdumbbell if (sc->syninfo.sysctl_tree != NULL) 5832186218Sdumbbell return; 5833186218Sdumbbell 5834178019Sjkim /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */ 5835178019Sjkim sysctl_ctx_init(&sc->syninfo.sysctl_ctx); 5836178019Sjkim sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx, 5837307576Sgonzo SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD, 5838307576Sgonzo 0, descr); 5839139982Sphilip 5840183888Sdumbbell /* hw.psm.synaptics.directional_scrolls. */ 5841281440Srpaulo sc->syninfo.directional_scrolls = 0; 5842178019Sjkim SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5843178019Sjkim SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5844183888Sdumbbell "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY, 5845178019Sjkim &sc->syninfo.directional_scrolls, 0, 5846183888Sdumbbell "Enable hardware scrolling pad (if non-zero) or register it as " 5847281440Srpaulo "extended buttons (if 0)"); 5848139982Sphilip 5849307576Sgonzo /* hw.psm.synaptics.max_x. */ 5850307576Sgonzo sc->syninfo.max_x = 6143; 5851307576Sgonzo SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5852307576Sgonzo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5853307576Sgonzo "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY, 5854307576Sgonzo &sc->syninfo.max_x, 0, 5855307576Sgonzo "Horizontal reporting range"); 5856307576Sgonzo 5857307576Sgonzo /* hw.psm.synaptics.max_y. */ 5858307576Sgonzo sc->syninfo.max_y = 6143; 5859307576Sgonzo SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5860307576Sgonzo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5861307576Sgonzo "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY, 5862307576Sgonzo &sc->syninfo.max_y, 0, 5863307576Sgonzo "Vertical reporting range"); 5864307576Sgonzo 5865281440Srpaulo /* 5866281440Srpaulo * Turn off two finger scroll if we have a 5867281440Srpaulo * physical area reserved for scrolling or when 5868281440Srpaulo * there's no multi finger support. 5869281440Srpaulo */ 5870282734Srpaulo if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 && 5871282734Srpaulo sc->synhw.capAdvancedGestures == 0)) 5872281440Srpaulo sc->syninfo.two_finger_scroll = 0; 5873281440Srpaulo else 5874281440Srpaulo sc->syninfo.two_finger_scroll = 1; 5875281440Srpaulo /* hw.psm.synaptics.two_finger_scroll. */ 5876281440Srpaulo SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5877281440Srpaulo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5878281440Srpaulo "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY, 5879281440Srpaulo &sc->syninfo.two_finger_scroll, 0, 5880281440Srpaulo "Enable two finger scrolling"); 5881281440Srpaulo 5882183888Sdumbbell /* hw.psm.synaptics.min_pressure. */ 5883318500Swulf sc->syninfo.min_pressure = 32; 5884183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5885178019Sjkim SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5886183888Sdumbbell "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5887307576Sgonzo sc, SYNAPTICS_SYSCTL_MIN_PRESSURE, 5888183888Sdumbbell synaptics_sysctl, "I", 5889183888Sdumbbell "Minimum pressure required to start an action"); 5890139982Sphilip 5891183888Sdumbbell /* hw.psm.synaptics.max_pressure. */ 5892183888Sdumbbell sc->syninfo.max_pressure = 220; 5893183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5894178019Sjkim SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5895183888Sdumbbell "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5896307576Sgonzo sc, SYNAPTICS_SYSCTL_MAX_PRESSURE, 5897183888Sdumbbell synaptics_sysctl, "I", 5898183888Sdumbbell "Maximum pressure to detect palm"); 5899139982Sphilip 5900183888Sdumbbell /* hw.psm.synaptics.max_width. */ 5901183888Sdumbbell sc->syninfo.max_width = 10; 5902183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5903178019Sjkim SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5904183888Sdumbbell "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5905307576Sgonzo sc, SYNAPTICS_SYSCTL_MAX_WIDTH, 5906183888Sdumbbell synaptics_sysctl, "I", 5907183888Sdumbbell "Maximum finger width to detect palm"); 5908132865Snjl 5909183888Sdumbbell /* hw.psm.synaptics.top_margin. */ 5910183888Sdumbbell sc->syninfo.margin_top = 200; 5911183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5912183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5913183888Sdumbbell "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5914307576Sgonzo sc, SYNAPTICS_SYSCTL_MARGIN_TOP, 5915183888Sdumbbell synaptics_sysctl, "I", 5916183888Sdumbbell "Top margin"); 5917183888Sdumbbell 5918183888Sdumbbell /* hw.psm.synaptics.right_margin. */ 5919183888Sdumbbell sc->syninfo.margin_right = 200; 5920183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5921183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5922183888Sdumbbell "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5923307576Sgonzo sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT, 5924183888Sdumbbell synaptics_sysctl, "I", 5925183888Sdumbbell "Right margin"); 5926183888Sdumbbell 5927183888Sdumbbell /* hw.psm.synaptics.bottom_margin. */ 5928183888Sdumbbell sc->syninfo.margin_bottom = 200; 5929183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5930183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5931183888Sdumbbell "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5932307576Sgonzo sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM, 5933183888Sdumbbell synaptics_sysctl, "I", 5934183888Sdumbbell "Bottom margin"); 5935183888Sdumbbell 5936183888Sdumbbell /* hw.psm.synaptics.left_margin. */ 5937183888Sdumbbell sc->syninfo.margin_left = 200; 5938183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5939183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5940183888Sdumbbell "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5941307576Sgonzo sc, SYNAPTICS_SYSCTL_MARGIN_LEFT, 5942183888Sdumbbell synaptics_sysctl, "I", 5943183888Sdumbbell "Left margin"); 5944183888Sdumbbell 5945183888Sdumbbell /* hw.psm.synaptics.na_top. */ 5946183888Sdumbbell sc->syninfo.na_top = 1783; 5947183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5948183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5949183888Sdumbbell "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5950307576Sgonzo sc, SYNAPTICS_SYSCTL_NA_TOP, 5951183888Sdumbbell synaptics_sysctl, "I", 5952183888Sdumbbell "Top noisy area, where weight_previous_na is used instead " 5953183888Sdumbbell "of weight_previous"); 5954183888Sdumbbell 5955183888Sdumbbell /* hw.psm.synaptics.na_right. */ 5956183888Sdumbbell sc->syninfo.na_right = 563; 5957183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5958183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5959183888Sdumbbell "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5960307576Sgonzo sc, SYNAPTICS_SYSCTL_NA_RIGHT, 5961183888Sdumbbell synaptics_sysctl, "I", 5962183888Sdumbbell "Right noisy area, where weight_previous_na is used instead " 5963183888Sdumbbell "of weight_previous"); 5964183888Sdumbbell 5965183888Sdumbbell /* hw.psm.synaptics.na_bottom. */ 5966183888Sdumbbell sc->syninfo.na_bottom = 1408; 5967183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5968183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5969183888Sdumbbell "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5970307576Sgonzo sc, SYNAPTICS_SYSCTL_NA_BOTTOM, 5971183888Sdumbbell synaptics_sysctl, "I", 5972183888Sdumbbell "Bottom noisy area, where weight_previous_na is used instead " 5973183888Sdumbbell "of weight_previous"); 5974183888Sdumbbell 5975183888Sdumbbell /* hw.psm.synaptics.na_left. */ 5976183888Sdumbbell sc->syninfo.na_left = 1600; 5977183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5978183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5979183888Sdumbbell "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5980307576Sgonzo sc, SYNAPTICS_SYSCTL_NA_LEFT, 5981183888Sdumbbell synaptics_sysctl, "I", 5982183888Sdumbbell "Left noisy area, where weight_previous_na is used instead " 5983183888Sdumbbell "of weight_previous"); 5984183888Sdumbbell 5985183888Sdumbbell /* hw.psm.synaptics.window_min. */ 5986183888Sdumbbell sc->syninfo.window_min = 4; 5987183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5988183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5989183888Sdumbbell "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5990307576Sgonzo sc, SYNAPTICS_SYSCTL_WINDOW_MIN, 5991183888Sdumbbell synaptics_sysctl, "I", 5992183888Sdumbbell "Minimum window size to start an action"); 5993183888Sdumbbell 5994183888Sdumbbell /* hw.psm.synaptics.window_max. */ 5995183888Sdumbbell sc->syninfo.window_max = 10; 5996183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5997183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5998183888Sdumbbell "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5999307576Sgonzo sc, SYNAPTICS_SYSCTL_WINDOW_MAX, 6000183888Sdumbbell synaptics_sysctl, "I", 6001183888Sdumbbell "Maximum window size"); 6002183888Sdumbbell 6003183888Sdumbbell /* hw.psm.synaptics.multiplicator. */ 6004183888Sdumbbell sc->syninfo.multiplicator = 10000; 6005183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6006183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6007183888Sdumbbell "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6008307576Sgonzo sc, SYNAPTICS_SYSCTL_MULTIPLICATOR, 6009183888Sdumbbell synaptics_sysctl, "I", 6010183888Sdumbbell "Multiplicator to increase precision in averages and divisions"); 6011183888Sdumbbell 6012183888Sdumbbell /* hw.psm.synaptics.weight_current. */ 6013183888Sdumbbell sc->syninfo.weight_current = 3; 6014183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6015183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6016183888Sdumbbell "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6017307576Sgonzo sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT, 6018183888Sdumbbell synaptics_sysctl, "I", 6019183888Sdumbbell "Weight of the current movement in the new average"); 6020183888Sdumbbell 6021183888Sdumbbell /* hw.psm.synaptics.weight_previous. */ 6022183888Sdumbbell sc->syninfo.weight_previous = 6; 6023183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6024183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6025183888Sdumbbell "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6026307576Sgonzo sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS, 6027183888Sdumbbell synaptics_sysctl, "I", 6028183888Sdumbbell "Weight of the previous average"); 6029183888Sdumbbell 6030183888Sdumbbell /* hw.psm.synaptics.weight_previous_na. */ 6031183888Sdumbbell sc->syninfo.weight_previous_na = 20; 6032183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6033183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6034183888Sdumbbell "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6035307576Sgonzo sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA, 6036183888Sdumbbell synaptics_sysctl, "I", 6037183888Sdumbbell "Weight of the previous average (inside the noisy area)"); 6038183888Sdumbbell 6039183888Sdumbbell /* hw.psm.synaptics.weight_len_squared. */ 6040183888Sdumbbell sc->syninfo.weight_len_squared = 2000; 6041183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6042183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6043183888Sdumbbell "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6044307576Sgonzo sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED, 6045183888Sdumbbell synaptics_sysctl, "I", 6046183888Sdumbbell "Length (squared) of segments where weight_previous " 6047183888Sdumbbell "starts to decrease"); 6048183888Sdumbbell 6049183888Sdumbbell /* hw.psm.synaptics.div_min. */ 6050183888Sdumbbell sc->syninfo.div_min = 9; 6051183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6052183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6053183888Sdumbbell "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6054307576Sgonzo sc, SYNAPTICS_SYSCTL_DIV_MIN, 6055183888Sdumbbell synaptics_sysctl, "I", 6056183888Sdumbbell "Divisor for fast movements"); 6057183888Sdumbbell 6058183888Sdumbbell /* hw.psm.synaptics.div_max. */ 6059183888Sdumbbell sc->syninfo.div_max = 17; 6060183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6061183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6062183888Sdumbbell "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6063307576Sgonzo sc, SYNAPTICS_SYSCTL_DIV_MAX, 6064183888Sdumbbell synaptics_sysctl, "I", 6065183888Sdumbbell "Divisor for slow movements"); 6066183888Sdumbbell 6067183888Sdumbbell /* hw.psm.synaptics.div_max_na. */ 6068183888Sdumbbell sc->syninfo.div_max_na = 30; 6069183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6070183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6071183888Sdumbbell "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6072307576Sgonzo sc, SYNAPTICS_SYSCTL_DIV_MAX_NA, 6073183888Sdumbbell synaptics_sysctl, "I", 6074183888Sdumbbell "Divisor with slow movements (inside the noisy area)"); 6075183888Sdumbbell 6076183888Sdumbbell /* hw.psm.synaptics.div_len. */ 6077183888Sdumbbell sc->syninfo.div_len = 100; 6078183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6079183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6080183888Sdumbbell "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6081307576Sgonzo sc, SYNAPTICS_SYSCTL_DIV_LEN, 6082183888Sdumbbell synaptics_sysctl, "I", 6083183888Sdumbbell "Length of segments where div_max starts to decrease"); 6084183888Sdumbbell 6085183888Sdumbbell /* hw.psm.synaptics.tap_max_delta. */ 6086183888Sdumbbell sc->syninfo.tap_max_delta = 80; 6087183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6088183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6089183888Sdumbbell "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6090307576Sgonzo sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA, 6091183888Sdumbbell synaptics_sysctl, "I", 6092183888Sdumbbell "Length of segments above which a tap is ignored"); 6093183888Sdumbbell 6094183888Sdumbbell /* hw.psm.synaptics.tap_min_queue. */ 6095183888Sdumbbell sc->syninfo.tap_min_queue = 2; 6096183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6097183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6098183888Sdumbbell "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6099307576Sgonzo sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE, 6100183888Sdumbbell synaptics_sysctl, "I", 6101183888Sdumbbell "Number of packets required to consider a tap"); 6102183888Sdumbbell 6103183888Sdumbbell /* hw.psm.synaptics.taphold_timeout. */ 6104307576Sgonzo sc->gesture.in_taphold = 0; 6105183888Sdumbbell sc->syninfo.taphold_timeout = tap_timeout; 6106183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6107183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6108183888Sdumbbell "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6109307576Sgonzo sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT, 6110183888Sdumbbell synaptics_sysctl, "I", 6111183888Sdumbbell "Maximum elapsed time between two taps to consider a tap-hold " 6112183888Sdumbbell "action"); 6113183888Sdumbbell 6114183888Sdumbbell /* hw.psm.synaptics.vscroll_hor_area. */ 6115183888Sdumbbell sc->syninfo.vscroll_hor_area = 0; /* 1300 */ 6116183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6117183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6118183888Sdumbbell "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6119307576Sgonzo sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA, 6120183888Sdumbbell synaptics_sysctl, "I", 6121183888Sdumbbell "Area reserved for horizontal virtual scrolling"); 6122183888Sdumbbell 6123183888Sdumbbell /* hw.psm.synaptics.vscroll_ver_area. */ 6124307576Sgonzo sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right; 6125183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6126183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6127183888Sdumbbell "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6128307576Sgonzo sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA, 6129183888Sdumbbell synaptics_sysctl, "I", 6130183888Sdumbbell "Area reserved for vertical virtual scrolling"); 6131183888Sdumbbell 6132183888Sdumbbell /* hw.psm.synaptics.vscroll_min_delta. */ 6133183888Sdumbbell sc->syninfo.vscroll_min_delta = 50; 6134183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6135183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6136183888Sdumbbell "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6137307576Sgonzo sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA, 6138183888Sdumbbell synaptics_sysctl, "I", 6139183888Sdumbbell "Minimum movement to consider virtual scrolling"); 6140183888Sdumbbell 6141183888Sdumbbell /* hw.psm.synaptics.vscroll_div_min. */ 6142183888Sdumbbell sc->syninfo.vscroll_div_min = 100; 6143183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6144183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6145183888Sdumbbell "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6146307576Sgonzo sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN, 6147183888Sdumbbell synaptics_sysctl, "I", 6148183888Sdumbbell "Divisor for fast scrolling"); 6149183888Sdumbbell 6150183888Sdumbbell /* hw.psm.synaptics.vscroll_div_min. */ 6151183888Sdumbbell sc->syninfo.vscroll_div_max = 150; 6152183888Sdumbbell SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6153183888Sdumbbell SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6154183888Sdumbbell "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6155307576Sgonzo sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX, 6156183888Sdumbbell synaptics_sysctl, "I", 6157183888Sdumbbell "Divisor for slow scrolling"); 6158281441Srpaulo 6159281441Srpaulo /* hw.psm.synaptics.touchpad_off. */ 6160281441Srpaulo sc->syninfo.touchpad_off = 0; 6161281441Srpaulo SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6162281441Srpaulo SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6163281441Srpaulo "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6164307576Sgonzo sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF, 6165281441Srpaulo synaptics_sysctl, "I", 6166281441Srpaulo "Turn off touchpad"); 6167307576Sgonzo 6168349541Swulf /* hw.psm.synaptics.natural_scroll. */ 6169349541Swulf sc->syninfo.natural_scroll = 0; 6170349541Swulf SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 6171349541Swulf SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 6172349541Swulf "natural_scroll", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6173349541Swulf sc, SYNAPTICS_SYSCTL_NATURAL_SCROLL, 6174349541Swulf synaptics_sysctl, "I", 6175349541Swulf "Enable natural scrolling"); 6176349541Swulf 6177307576Sgonzo sc->syninfo.softbuttons_y = 0; 6178307576Sgonzo sc->syninfo.softbutton2_x = 0; 6179307576Sgonzo sc->syninfo.softbutton3_x = 0; 6180307576Sgonzo 6181307576Sgonzo /* skip softbuttons sysctl on not clickpads */ 6182307576Sgonzo if (sc->synhw.capClickPad) 6183307576Sgonzo synaptics_sysctl_create_softbuttons_tree(sc); 6184183888Sdumbbell} 6185183888Sdumbbell 6186183888Sdumbbellstatic int 6187282734Srpaulosynaptics_preferred_mode(struct psm_softc *sc) { 6188282734Srpaulo int mode_byte; 6189282734Srpaulo 6190308652Sgonzo /* Check if we are in relative mode */ 6191308652Sgonzo if (sc->hw.model != MOUSE_MODEL_SYNAPTICS) { 6192308652Sgonzo if (tap_enabled == 0) 6193308652Sgonzo /* 6194308652Sgonzo * Disable tap & drag gestures. We use a Mode Byte 6195308652Sgonzo * and set the DisGest bit (see ��2.5 of Synaptics 6196308652Sgonzo * TouchPad Interfacing Guide). 6197308652Sgonzo */ 6198308652Sgonzo return (0x04); 6199308652Sgonzo else 6200308652Sgonzo /* 6201308652Sgonzo * Enable tap & drag gestures. We use a Mode Byte 6202308652Sgonzo * and clear the DisGest bit (see ��2.5 of Synaptics 6203308652Sgonzo * TouchPad Interfacing Guide). 6204308652Sgonzo */ 6205308652Sgonzo return (0x00); 6206308652Sgonzo } 6207308652Sgonzo 6208307576Sgonzo mode_byte = 0xc4; 6209282734Srpaulo 6210282734Srpaulo /* request wmode where available */ 6211282734Srpaulo if (sc->synhw.capExtended) 6212282734Srpaulo mode_byte |= 1; 6213282734Srpaulo 6214282734Srpaulo return mode_byte; 6215282734Srpaulo} 6216282734Srpaulo 6217282734Srpaulostatic void 6218282734Srpaulosynaptics_set_mode(struct psm_softc *sc, int mode_byte) { 6219282734Srpaulo mouse_ext_command(sc->kbdc, mode_byte); 6220282734Srpaulo 6221282734Srpaulo /* "Commit" the Set Mode Byte command sent above. */ 6222282734Srpaulo set_mouse_sampling_rate(sc->kbdc, 20); 6223282734Srpaulo 6224282734Srpaulo /* 6225282734Srpaulo * Enable advanced gestures mode if supported and we are not entering 6226308652Sgonzo * passthrough or relative mode. 6227282734Srpaulo */ 6228307576Sgonzo if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) && 6229308652Sgonzo sc->hw.model == MOUSE_MODEL_SYNAPTICS && !(mode_byte & (1 << 5))) { 6230349541Swulf mouse_ext_command(sc->kbdc, SYNAPTICS_READ_MODEL_ID); 6231282734Srpaulo set_mouse_sampling_rate(sc->kbdc, 0xc8); 6232282734Srpaulo } 6233282734Srpaulo} 6234282734Srpaulo 6235343158Swulf/* 6236343158Swulf * AUX MUX detection code should be placed at very beginning of probe sequence 6237343158Swulf * at least before 4-byte protocol mouse probes e.g. MS IntelliMouse probe as 6238343158Swulf * latter can trigger switching the MUX to incompatible state. 6239343158Swulf */ 6240282734Srpaulostatic int 6241343158Swulfenable_synaptics_mux(struct psm_softc *sc, enum probearg arg) 6242343158Swulf{ 6243343158Swulf KBDC kbdc = sc->kbdc; 6244343158Swulf int port, version; 6245343158Swulf int probe = FALSE; 6246343158Swulf int active_ports_count = 0; 6247343158Swulf int active_ports_mask = 0; 6248343158Swulf 6249343158Swulf version = enable_aux_mux(kbdc); 6250343158Swulf if (version == -1) 6251343158Swulf return (FALSE); 6252343158Swulf 6253343158Swulf for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) { 6254343158Swulf VLOG(3, (LOG_DEBUG, "aux_mux: ping port %d\n", port)); 6255343158Swulf set_active_aux_mux_port(kbdc, port); 6256343158Swulf if (enable_aux_dev(kbdc) && disable_aux_dev(kbdc)) { 6257343158Swulf active_ports_count++; 6258343158Swulf active_ports_mask |= 1 << port; 6259343158Swulf } 6260343158Swulf } 6261343158Swulf 6262343158Swulf if (verbose >= 2) 6263343158Swulf printf("Active Multiplexing PS/2 controller v%d.%d with %d " 6264343158Swulf "active port(s)\n", version >> 4 & 0x0f, version & 0x0f, 6265343158Swulf active_ports_count); 6266343158Swulf 6267343158Swulf /* psm has a special support for GenMouse + SynTouchpad combination */ 6268343158Swulf if (active_ports_count >= 2) { 6269343158Swulf for (port = 0; port < KBDC_AUX_MUX_NUM_PORTS; port++) { 6270343158Swulf if ((active_ports_mask & 1 << port) == 0) 6271343158Swulf continue; 6272343158Swulf VLOG(3, (LOG_DEBUG, "aux_mux: probe port %d\n", port)); 6273343158Swulf set_active_aux_mux_port(kbdc, port); 6274343158Swulf probe = enable_synaptics(sc, arg); 6275343158Swulf if (probe) { 6276343158Swulf if (arg == PROBE) 6277343158Swulf sc->muxport = port; 6278343158Swulf break; 6279343158Swulf } 6280343158Swulf } 6281343158Swulf } 6282343158Swulf 6283343158Swulf /* IRQ handler does not support active multiplexing mode */ 6284343158Swulf disable_aux_mux(kbdc); 6285343158Swulf 6286343158Swulf return (probe); 6287343158Swulf} 6288343158Swulf 6289343158Swulfstatic int 6290284320Sglebiusenable_synaptics(struct psm_softc *sc, enum probearg arg) 6291183888Sdumbbell{ 6292331170Seadler device_t psmcpnp; 6293331170Seadler struct psmcpnp_softc *psmcpnp_sc; 6294284320Sglebius KBDC kbdc = sc->kbdc; 6295233580Sjkim synapticshw_t synhw; 6296183888Sdumbbell int status[3]; 6297343158Swulf int buttons; 6298183888Sdumbbell 6299183888Sdumbbell VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n")); 6300132865Snjl 6301186175Sdumbbell /* 6302186175Sdumbbell * Just to be on the safe side: this avoids troubles with 6303186175Sdumbbell * following mouse_ext_command() when the previous command 6304186175Sdumbbell * was PSMC_SET_RESOLUTION. Set Scaling has no effect on 6305186175Sdumbbell * Synaptics Touchpad behaviour. 6306186175Sdumbbell */ 6307178019Sjkim set_mouse_scaling(kbdc, 1); 6308133295Sphilip 6309186175Sdumbbell /* Identify the Touchpad version. */ 6310349541Swulf if (mouse_ext_command(kbdc, SYNAPTICS_READ_IDENTITY) == 0) 6311178019Sjkim return (FALSE); 6312178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) != 3) 6313178019Sjkim return (FALSE); 6314343158Swulf if (status[1] != 0x47) 6315178019Sjkim return (FALSE); 6316133295Sphilip 6317233580Sjkim bzero(&synhw, sizeof(synhw)); 6318233580Sjkim synhw.infoMinor = status[0]; 6319233580Sjkim synhw.infoMajor = status[2] & 0x0f; 6320132865Snjl 6321178019Sjkim if (verbose >= 2) 6322233580Sjkim printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor, 6323233580Sjkim synhw.infoMinor); 6324132865Snjl 6325343158Swulf if (synhw.infoMajor < 4) { 6326178019Sjkim printf(" Unsupported (pre-v4) Touchpad detected\n"); 6327178019Sjkim return (FALSE); 6328178019Sjkim } 6329133295Sphilip 6330186175Sdumbbell /* Get the Touchpad model information. */ 6331349541Swulf if (mouse_ext_command(kbdc, SYNAPTICS_READ_MODEL_ID) == 0) 6332178019Sjkim return (FALSE); 6333178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) != 3) 6334178019Sjkim return (FALSE); 6335178019Sjkim if ((status[1] & 0x01) != 0) { 6336178019Sjkim printf(" Failed to read model information\n"); 6337178019Sjkim return (FALSE); 6338178019Sjkim } 6339132865Snjl 6340233580Sjkim synhw.infoRot180 = (status[0] & 0x80) != 0; 6341233580Sjkim synhw.infoPortrait = (status[0] & 0x40) != 0; 6342233580Sjkim synhw.infoSensor = status[0] & 0x3f; 6343233580Sjkim synhw.infoHardware = (status[1] & 0xfe) >> 1; 6344233580Sjkim synhw.infoNewAbs = (status[2] & 0x80) != 0; 6345233580Sjkim synhw.capPen = (status[2] & 0x40) != 0; 6346233580Sjkim synhw.infoSimplC = (status[2] & 0x20) != 0; 6347233580Sjkim synhw.infoGeometry = status[2] & 0x0f; 6348132865Snjl 6349133295Sphilip if (verbose >= 2) { 6350178019Sjkim printf(" Model information:\n"); 6351233580Sjkim printf(" infoRot180: %d\n", synhw.infoRot180); 6352233580Sjkim printf(" infoPortrait: %d\n", synhw.infoPortrait); 6353233580Sjkim printf(" infoSensor: %d\n", synhw.infoSensor); 6354233580Sjkim printf(" infoHardware: %d\n", synhw.infoHardware); 6355233580Sjkim printf(" infoNewAbs: %d\n", synhw.infoNewAbs); 6356233580Sjkim printf(" capPen: %d\n", synhw.capPen); 6357233580Sjkim printf(" infoSimplC: %d\n", synhw.infoSimplC); 6358233580Sjkim printf(" infoGeometry: %d\n", synhw.infoGeometry); 6359133295Sphilip } 6360139982Sphilip 6361186175Sdumbbell /* Read the extended capability bits. */ 6362349541Swulf if (mouse_ext_command(kbdc, SYNAPTICS_READ_CAPABILITIES) == 0) 6363178019Sjkim return (FALSE); 6364178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) != 3) 6365178019Sjkim return (FALSE); 6366343158Swulf if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) { 6367178019Sjkim printf(" Failed to read extended capability bits\n"); 6368178019Sjkim return (FALSE); 6369178019Sjkim } 6370178019Sjkim 6371331170Seadler psmcpnp = devclass_get_device(devclass_find(PSMCPNP_DRIVER_NAME), 6372331170Seadler sc->unit); 6373331170Seadler psmcpnp_sc = (psmcpnp != NULL) ? device_get_softc(psmcpnp) : NULL; 6374331170Seadler 6375186175Sdumbbell /* Set the different capabilities when they exist. */ 6376233580Sjkim buttons = 0; 6377343158Swulf synhw.capExtended = (status[0] & 0x80) != 0; 6378343158Swulf if (synhw.capExtended) { 6379282734Srpaulo synhw.nExtendedQueries = (status[0] & 0x70) >> 4; 6380255153Sdumbbell synhw.capMiddle = (status[0] & 0x04) != 0; 6381255153Sdumbbell synhw.capPassthrough = (status[2] & 0x80) != 0; 6382281127Srpaulo synhw.capLowPower = (status[2] & 0x40) != 0; 6383281127Srpaulo synhw.capMultiFingerReport = 6384281127Srpaulo (status[2] & 0x20) != 0; 6385255153Sdumbbell synhw.capSleep = (status[2] & 0x10) != 0; 6386255153Sdumbbell synhw.capFourButtons = (status[2] & 0x08) != 0; 6387281127Srpaulo synhw.capBallistics = (status[2] & 0x04) != 0; 6388255153Sdumbbell synhw.capMultiFinger = (status[2] & 0x02) != 0; 6389255153Sdumbbell synhw.capPalmDetect = (status[2] & 0x01) != 0; 6390178019Sjkim 6391282734Srpaulo if (!set_mouse_scaling(kbdc, 1)) 6392282734Srpaulo return (FALSE); 6393349541Swulf if (mouse_ext_command(kbdc, SYNAPTICS_READ_RESOLUTIONS) == 0) 6394282734Srpaulo return (FALSE); 6395282734Srpaulo if (get_mouse_status(kbdc, status, 0, 3) != 3) 6396282734Srpaulo return (FALSE); 6397282734Srpaulo 6398325345Swulf if (status[0] != 0 && (status[1] & 0x80) && status[2] != 0) { 6399325345Swulf synhw.infoXupmm = status[0]; 6400325345Swulf synhw.infoYupmm = status[2]; 6401325345Swulf } 6402282734Srpaulo 6403178019Sjkim if (verbose >= 2) { 6404178019Sjkim printf(" Extended capabilities:\n"); 6405233580Sjkim printf(" capExtended: %d\n", synhw.capExtended); 6406255153Sdumbbell printf(" capMiddle: %d\n", synhw.capMiddle); 6407255153Sdumbbell printf(" nExtendedQueries: %d\n", 6408255153Sdumbbell synhw.nExtendedQueries); 6409233580Sjkim printf(" capPassthrough: %d\n", synhw.capPassthrough); 6410281127Srpaulo printf(" capLowPower: %d\n", synhw.capLowPower); 6411281127Srpaulo printf(" capMultiFingerReport: %d\n", 6412281127Srpaulo synhw.capMultiFingerReport); 6413233580Sjkim printf(" capSleep: %d\n", synhw.capSleep); 6414233580Sjkim printf(" capFourButtons: %d\n", synhw.capFourButtons); 6415281127Srpaulo printf(" capBallistics: %d\n", synhw.capBallistics); 6416233580Sjkim printf(" capMultiFinger: %d\n", synhw.capMultiFinger); 6417233580Sjkim printf(" capPalmDetect: %d\n", synhw.capPalmDetect); 6418282734Srpaulo printf(" infoXupmm: %d\n", synhw.infoXupmm); 6419282734Srpaulo printf(" infoYupmm: %d\n", synhw.infoYupmm); 6420178019Sjkim } 6421178019Sjkim 6422178019Sjkim /* 6423255153Sdumbbell * If nExtendedQueries is 1 or greater, then the TouchPad 6424255153Sdumbbell * supports this number of extended queries. We can load 6425186175Sdumbbell * more information about buttons using query 0x09. 6426178019Sjkim */ 6427282734Srpaulo if (synhw.nExtendedQueries >= 1) { 6428282734Srpaulo if (!set_mouse_scaling(kbdc, 1)) 6429282734Srpaulo return (FALSE); 6430349541Swulf if (mouse_ext_command(kbdc, 6431349541Swulf SYNAPTICS_READ_EXTENDED) == 0) 6432178019Sjkim return (FALSE); 6433178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) != 3) 6434178019Sjkim return (FALSE); 6435281440Srpaulo synhw.verticalScroll = (status[0] & 0x01) != 0; 6436281440Srpaulo synhw.horizontalScroll = (status[0] & 0x02) != 0; 6437281440Srpaulo synhw.verticalWheel = (status[0] & 0x08) != 0; 6438255153Sdumbbell synhw.nExtendedButtons = (status[1] & 0xf0) >> 4; 6439282734Srpaulo synhw.capEWmode = (status[0] & 0x04) != 0; 6440281440Srpaulo if (verbose >= 2) { 6441281440Srpaulo printf(" Extended model ID:\n"); 6442281440Srpaulo printf(" verticalScroll: %d\n", 6443281440Srpaulo synhw.verticalScroll); 6444281440Srpaulo printf(" horizontalScroll: %d\n", 6445281440Srpaulo synhw.horizontalScroll); 6446281440Srpaulo printf(" verticalWheel: %d\n", 6447281440Srpaulo synhw.verticalWheel); 6448281440Srpaulo printf(" nExtendedButtons: %d\n", 6449281440Srpaulo synhw.nExtendedButtons); 6450282734Srpaulo printf(" capEWmode: %d\n", 6451282734Srpaulo synhw.capEWmode); 6452281440Srpaulo } 6453255153Sdumbbell /* 6454255153Sdumbbell * Add the number of extended buttons to the total 6455255153Sdumbbell * button support count, including the middle button 6456255153Sdumbbell * if capMiddle support bit is set. 6457255153Sdumbbell */ 6458255153Sdumbbell buttons = synhw.nExtendedButtons + synhw.capMiddle; 6459233580Sjkim } else 6460255153Sdumbbell /* 6461255153Sdumbbell * If the capFourButtons support bit is set, 6462255153Sdumbbell * add a fourth button to the total button count. 6463255153Sdumbbell */ 6464233580Sjkim buttons = synhw.capFourButtons ? 1 : 0; 6465282734Srpaulo 6466282734Srpaulo /* Read the continued capabilities bits. */ 6467282734Srpaulo if (synhw.nExtendedQueries >= 4) { 6468282734Srpaulo if (!set_mouse_scaling(kbdc, 1)) 6469282734Srpaulo return (FALSE); 6470349541Swulf if (mouse_ext_command(kbdc, 6471349541Swulf SYNAPTICS_READ_CAPABILITIES_CONT) == 0) 6472282734Srpaulo return (FALSE); 6473282734Srpaulo if (get_mouse_status(kbdc, status, 0, 3) != 3) 6474282734Srpaulo return (FALSE); 6475282734Srpaulo 6476282734Srpaulo synhw.capClickPad = (status[1] & 0x01) << 1; 6477282734Srpaulo synhw.capClickPad |= (status[0] & 0x10) != 0; 6478282734Srpaulo synhw.capDeluxeLEDs = (status[1] & 0x02) != 0; 6479282734Srpaulo synhw.noAbsoluteFilter = (status[1] & 0x04) != 0; 6480282734Srpaulo synhw.capReportsV = (status[1] & 0x08) != 0; 6481282734Srpaulo synhw.capUniformClickPad = (status[1] & 0x10) != 0; 6482282734Srpaulo synhw.capReportsMin = (status[1] & 0x20) != 0; 6483282734Srpaulo synhw.capInterTouch = (status[1] & 0x40) != 0; 6484282734Srpaulo synhw.capReportsMax = (status[0] & 0x02) != 0; 6485282734Srpaulo synhw.capClearPad = (status[0] & 0x04) != 0; 6486282734Srpaulo synhw.capAdvancedGestures = (status[0] & 0x08) != 0; 6487282734Srpaulo synhw.capCoveredPad = (status[0] & 0x80) != 0; 6488282734Srpaulo 6489282734Srpaulo if (synhw.capReportsMax) { 6490282734Srpaulo if (!set_mouse_scaling(kbdc, 1)) 6491282734Srpaulo return (FALSE); 6492349541Swulf if (mouse_ext_command(kbdc, 6493349541Swulf SYNAPTICS_READ_MAX_COORDS) == 0) 6494282734Srpaulo return (FALSE); 6495282734Srpaulo if (get_mouse_status(kbdc, status, 0, 3) != 3) 6496282734Srpaulo return (FALSE); 6497282734Srpaulo 6498282734Srpaulo synhw.maximumXCoord = (status[0] << 5) | 6499282734Srpaulo ((status[1] & 0x0f) << 1); 6500282734Srpaulo synhw.maximumYCoord = (status[2] << 5) | 6501282734Srpaulo ((status[1] & 0xf0) >> 3); 6502307576Sgonzo } else { 6503343158Swulf /* 6504343158Swulf * Typical bezel limits. Taken from 'Synaptics 6505343158Swulf * PS/2 * TouchPad Interfacing Guide' p.3.2.3. 6506343158Swulf */ 6507343158Swulf synhw.maximumXCoord = 5472; 6508343158Swulf synhw.maximumYCoord = 4448; 6509282734Srpaulo } 6510307576Sgonzo 6511282734Srpaulo if (synhw.capReportsMin) { 6512282734Srpaulo if (!set_mouse_scaling(kbdc, 1)) 6513282734Srpaulo return (FALSE); 6514349541Swulf if (mouse_ext_command(kbdc, 6515349541Swulf SYNAPTICS_READ_MIN_COORDS) == 0) 6516282734Srpaulo return (FALSE); 6517282734Srpaulo if (get_mouse_status(kbdc, status, 0, 3) != 3) 6518282734Srpaulo return (FALSE); 6519282734Srpaulo 6520282734Srpaulo synhw.minimumXCoord = (status[0] << 5) | 6521282734Srpaulo ((status[1] & 0x0f) << 1); 6522282734Srpaulo synhw.minimumYCoord = (status[2] << 5) | 6523282734Srpaulo ((status[1] & 0xf0) >> 3); 6524307576Sgonzo } else { 6525343158Swulf /* 6526343158Swulf * Typical bezel limits. Taken from 'Synaptics 6527343158Swulf * PS/2 * TouchPad Interfacing Guide' p.3.2.3. 6528343158Swulf */ 6529343158Swulf synhw.minimumXCoord = 1472; 6530343158Swulf synhw.minimumYCoord = 1408; 6531282734Srpaulo } 6532282734Srpaulo 6533331170Seadler /* 6534331170Seadler * ClickPad properties are not exported through PS/2 6535331170Seadler * protocol. Detection is based on controller's PnP ID. 6536331170Seadler */ 6537331170Seadler if (synhw.capClickPad && psmcpnp_sc != NULL) { 6538331170Seadler switch (psmcpnp_sc->type) { 6539331170Seadler case PSMCPNP_FORCEPAD: 6540331170Seadler synhw.forcePad = 1; 6541331170Seadler break; 6542344165Swulf case PSMCPNP_TOPBUTTONPAD: 6543344165Swulf synhw.topButtonPad = 1; 6544344165Swulf break; 6545331170Seadler default: 6546331170Seadler break; 6547331170Seadler } 6548331170Seadler } 6549331170Seadler 6550282734Srpaulo if (verbose >= 2) { 6551282734Srpaulo printf(" Continued capabilities:\n"); 6552282734Srpaulo printf(" capClickPad: %d\n", 6553282734Srpaulo synhw.capClickPad); 6554282734Srpaulo printf(" capDeluxeLEDs: %d\n", 6555282734Srpaulo synhw.capDeluxeLEDs); 6556282734Srpaulo printf(" noAbsoluteFilter: %d\n", 6557282734Srpaulo synhw.noAbsoluteFilter); 6558282734Srpaulo printf(" capReportsV: %d\n", 6559282734Srpaulo synhw.capReportsV); 6560282734Srpaulo printf(" capUniformClickPad: %d\n", 6561282734Srpaulo synhw.capUniformClickPad); 6562282734Srpaulo printf(" capReportsMin: %d\n", 6563282734Srpaulo synhw.capReportsMin); 6564282734Srpaulo printf(" capInterTouch: %d\n", 6565282734Srpaulo synhw.capInterTouch); 6566282734Srpaulo printf(" capReportsMax: %d\n", 6567282734Srpaulo synhw.capReportsMax); 6568282734Srpaulo printf(" capClearPad: %d\n", 6569282734Srpaulo synhw.capClearPad); 6570282734Srpaulo printf(" capAdvancedGestures: %d\n", 6571282734Srpaulo synhw.capAdvancedGestures); 6572282734Srpaulo printf(" capCoveredPad: %d\n", 6573282734Srpaulo synhw.capCoveredPad); 6574282734Srpaulo if (synhw.capReportsMax) { 6575282734Srpaulo printf(" maximumXCoord: %d\n", 6576282734Srpaulo synhw.maximumXCoord); 6577282734Srpaulo printf(" maximumYCoord: %d\n", 6578282734Srpaulo synhw.maximumYCoord); 6579282734Srpaulo } 6580282734Srpaulo if (synhw.capReportsMin) { 6581282734Srpaulo printf(" minimumXCoord: %d\n", 6582282734Srpaulo synhw.minimumXCoord); 6583282734Srpaulo printf(" minimumYCoord: %d\n", 6584282734Srpaulo synhw.minimumYCoord); 6585282734Srpaulo } 6586331170Seadler if (synhw.capClickPad) { 6587344165Swulf printf(" Clickpad capabilities:\n"); 6588331170Seadler printf(" forcePad: %d\n", 6589331170Seadler synhw.forcePad); 6590344165Swulf printf(" topButtonPad: %d\n", 6591344165Swulf synhw.topButtonPad); 6592331170Seadler } 6593282734Srpaulo } 6594282734Srpaulo buttons += synhw.capClickPad; 6595282734Srpaulo } 6596233580Sjkim } 6597282734Srpaulo 6598233580Sjkim if (verbose >= 2) { 6599233580Sjkim if (synhw.capExtended) 6600233580Sjkim printf(" Additional Buttons: %d\n", buttons); 6601233580Sjkim else 6602178019Sjkim printf(" No extended capabilities\n"); 6603178019Sjkim } 6604178019Sjkim 6605139982Sphilip /* 6606255153Sdumbbell * Add the default number of 3 buttons to the total 6607255153Sdumbbell * count of supported buttons reported above. 6608255153Sdumbbell */ 6609255153Sdumbbell buttons += 3; 6610255153Sdumbbell 6611255153Sdumbbell /* 6612186175Sdumbbell * Read the mode byte. 6613178019Sjkim * 6614178019Sjkim * XXX: Note the Synaptics documentation also defines the first 6615178019Sjkim * byte of the response to this query to be a constant 0x3b, this 6616178019Sjkim * does not appear to be true for Touchpads with guest devices. 6617139982Sphilip */ 6618349541Swulf if (mouse_ext_command(kbdc, SYNAPTICS_READ_MODES) == 0) 6619139982Sphilip return (FALSE); 6620178019Sjkim if (get_mouse_status(kbdc, status, 0, 3) != 3) 6621139982Sphilip return (FALSE); 6622343158Swulf if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) { 6623178019Sjkim printf(" Failed to read mode byte\n"); 6624178019Sjkim return (FALSE); 6625139982Sphilip } 6626139982Sphilip 6627284320Sglebius if (arg == PROBE) 6628233580Sjkim sc->synhw = synhw; 6629233580Sjkim if (!synaptics_support) 6630233580Sjkim return (FALSE); 6631233580Sjkim 6632308652Sgonzo /* Set mouse type just now for synaptics_set_mode() */ 6633308652Sgonzo sc->hw.model = MOUSE_MODEL_SYNAPTICS; 6634308652Sgonzo 6635284320Sglebius synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 6636284320Sglebius 6637284320Sglebius if (trackpoint_support && synhw.capPassthrough) { 6638284320Sglebius enable_trackpoint(sc, arg); 6639284320Sglebius } 6640284320Sglebius 6641233580Sjkim VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons)); 6642132865Snjl 6643284320Sglebius if (arg == PROBE) { 6644233580Sjkim /* Create sysctl tree. */ 6645307576Sgonzo synaptics_sysctl_create_tree(sc, "synaptics", 6646307576Sgonzo "Synaptics TouchPad"); 6647233580Sjkim sc->hw.buttons = buttons; 6648233580Sjkim } 6649183888Sdumbbell 6650178019Sjkim return (TRUE); 6651132865Snjl} 6652133295Sphilip 6653281441Srpaulostatic void 6654281441Srpaulosynaptics_passthrough_on(struct psm_softc *sc) 6655281441Srpaulo{ 6656282734Srpaulo VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n")); 6657282734Srpaulo synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5)); 6658281441Srpaulo} 6659281441Srpaulo 6660281441Srpaulostatic void 6661281441Srpaulosynaptics_passthrough_off(struct psm_softc *sc) 6662281441Srpaulo{ 6663281441Srpaulo VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n")); 6664281441Srpaulo set_mouse_scaling(sc->kbdc, 2); 6665281441Srpaulo set_mouse_scaling(sc->kbdc, 1); 6666282734Srpaulo synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 6667281441Srpaulo} 6668281441Srpaulo 6669248478Sjkim/* IBM/Lenovo TrackPoint */ 6670248478Sjkimstatic int 6671281441Srpaulotrackpoint_command(struct psm_softc *sc, int cmd, int loc, int val) 6672248478Sjkim{ 6673248478Sjkim const int seq[] = { 0xe2, cmd, loc, val }; 6674248478Sjkim int i; 6675248478Sjkim 6676281441Srpaulo if (sc->synhw.capPassthrough) 6677281441Srpaulo synaptics_passthrough_on(sc); 6678281441Srpaulo 6679281441Srpaulo for (i = 0; i < nitems(seq); i++) { 6680281441Srpaulo if (sc->synhw.capPassthrough && 6681281441Srpaulo (seq[i] == 0xff || seq[i] == 0xe7)) 6682281441Srpaulo if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) { 6683281441Srpaulo synaptics_passthrough_off(sc); 6684281441Srpaulo return (EIO); 6685281441Srpaulo } 6686281441Srpaulo if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) { 6687281441Srpaulo if (sc->synhw.capPassthrough) 6688281441Srpaulo synaptics_passthrough_off(sc); 6689248478Sjkim return (EIO); 6690281441Srpaulo } 6691281441Srpaulo } 6692281441Srpaulo 6693281441Srpaulo if (sc->synhw.capPassthrough) 6694281441Srpaulo synaptics_passthrough_off(sc); 6695281441Srpaulo 6696248478Sjkim return (0); 6697248478Sjkim} 6698248478Sjkim 6699248478Sjkim#define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x) 6700248478Sjkim#define TPMASK 0 6701248478Sjkim#define TPLOC 1 6702248478Sjkim#define TPINFO 2 6703248478Sjkim 6704248478Sjkimstatic int 6705248478Sjkimtrackpoint_sysctl(SYSCTL_HANDLER_ARGS) 6706248478Sjkim{ 6707248478Sjkim static const int data[][3] = { 6708248478Sjkim { 0x00, 0x4a, PSM_TPINFO(sensitivity) }, 6709248478Sjkim { 0x00, 0x4d, PSM_TPINFO(inertia) }, 6710248478Sjkim { 0x00, 0x60, PSM_TPINFO(uplateau) }, 6711248478Sjkim { 0x00, 0x57, PSM_TPINFO(reach) }, 6712248478Sjkim { 0x00, 0x58, PSM_TPINFO(draghys) }, 6713248478Sjkim { 0x00, 0x59, PSM_TPINFO(mindrag) }, 6714248478Sjkim { 0x00, 0x5a, PSM_TPINFO(upthresh) }, 6715248478Sjkim { 0x00, 0x5c, PSM_TPINFO(threshold) }, 6716248478Sjkim { 0x00, 0x5d, PSM_TPINFO(jenks) }, 6717248478Sjkim { 0x00, 0x5e, PSM_TPINFO(ztime) }, 6718248478Sjkim { 0x01, 0x2c, PSM_TPINFO(pts) }, 6719248478Sjkim { 0x08, 0x2d, PSM_TPINFO(skipback) } 6720248478Sjkim }; 6721248478Sjkim struct psm_softc *sc; 6722248478Sjkim int error, newval, *oldvalp; 6723248478Sjkim const int *tp; 6724248478Sjkim 6725248478Sjkim if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data)) 6726248478Sjkim return (EINVAL); 6727248478Sjkim sc = arg1; 6728248478Sjkim tp = data[arg2]; 6729248478Sjkim oldvalp = (int *)((intptr_t)sc + tp[TPINFO]); 6730248478Sjkim newval = *oldvalp; 6731248478Sjkim error = sysctl_handle_int(oidp, &newval, 0, req); 6732248478Sjkim if (error != 0) 6733248478Sjkim return (error); 6734248478Sjkim if (newval == *oldvalp) 6735248478Sjkim return (0); 6736248478Sjkim if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1)) 6737248478Sjkim return (EINVAL); 6738281441Srpaulo error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47, 6739248478Sjkim tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]); 6740248478Sjkim if (error != 0) 6741248478Sjkim return (error); 6742248478Sjkim *oldvalp = newval; 6743248478Sjkim 6744248478Sjkim return (0); 6745248478Sjkim} 6746248478Sjkim 6747248478Sjkimstatic void 6748248478Sjkimtrackpoint_sysctl_create_tree(struct psm_softc *sc) 6749248478Sjkim{ 6750248478Sjkim 6751248478Sjkim if (sc->tpinfo.sysctl_tree != NULL) 6752248478Sjkim return; 6753248478Sjkim 6754248478Sjkim /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */ 6755248478Sjkim sysctl_ctx_init(&sc->tpinfo.sysctl_ctx); 6756248478Sjkim sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx, 6757248478Sjkim SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD, 6758248478Sjkim 0, "IBM/Lenovo TrackPoint"); 6759248478Sjkim 6760248478Sjkim /* hw.psm.trackpoint.sensitivity */ 6761281441Srpaulo sc->tpinfo.sensitivity = 0x80; 6762248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6763248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6764248478Sjkim "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6765248478Sjkim sc, TRACKPOINT_SYSCTL_SENSITIVITY, 6766248478Sjkim trackpoint_sysctl, "I", 6767248478Sjkim "Sensitivity"); 6768248478Sjkim 6769248478Sjkim /* hw.psm.trackpoint.negative_inertia */ 6770248478Sjkim sc->tpinfo.inertia = 0x06; 6771248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6772248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6773248478Sjkim "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6774248478Sjkim sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA, 6775248478Sjkim trackpoint_sysctl, "I", 6776248478Sjkim "Negative inertia factor"); 6777248478Sjkim 6778248478Sjkim /* hw.psm.trackpoint.upper_plateau */ 6779248478Sjkim sc->tpinfo.uplateau = 0x61; 6780248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6781248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6782248478Sjkim "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6783248478Sjkim sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU, 6784248478Sjkim trackpoint_sysctl, "I", 6785248478Sjkim "Transfer function upper plateau speed"); 6786248478Sjkim 6787248478Sjkim /* hw.psm.trackpoint.backup_range */ 6788248478Sjkim sc->tpinfo.reach = 0x0a; 6789248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6790248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6791248478Sjkim "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6792248478Sjkim sc, TRACKPOINT_SYSCTL_BACKUP_RANGE, 6793248478Sjkim trackpoint_sysctl, "I", 6794248478Sjkim "Backup range"); 6795248478Sjkim 6796248478Sjkim /* hw.psm.trackpoint.drag_hysteresis */ 6797248478Sjkim sc->tpinfo.draghys = 0xff; 6798248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6799248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6800248478Sjkim "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6801248478Sjkim sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS, 6802248478Sjkim trackpoint_sysctl, "I", 6803248478Sjkim "Drag hysteresis"); 6804248478Sjkim 6805248478Sjkim /* hw.psm.trackpoint.minimum_drag */ 6806248478Sjkim sc->tpinfo.mindrag = 0x14; 6807248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6808248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6809248478Sjkim "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6810248478Sjkim sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG, 6811248478Sjkim trackpoint_sysctl, "I", 6812248478Sjkim "Minimum drag"); 6813248478Sjkim 6814248478Sjkim /* hw.psm.trackpoint.up_threshold */ 6815248478Sjkim sc->tpinfo.upthresh = 0xff; 6816248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6817248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6818248478Sjkim "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6819248478Sjkim sc, TRACKPOINT_SYSCTL_UP_THRESHOLD, 6820248478Sjkim trackpoint_sysctl, "I", 6821248478Sjkim "Up threshold for release"); 6822248478Sjkim 6823248478Sjkim /* hw.psm.trackpoint.threshold */ 6824248478Sjkim sc->tpinfo.threshold = 0x08; 6825248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6826248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6827248478Sjkim "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6828248478Sjkim sc, TRACKPOINT_SYSCTL_THRESHOLD, 6829248478Sjkim trackpoint_sysctl, "I", 6830248478Sjkim "Threshold"); 6831248478Sjkim 6832248478Sjkim /* hw.psm.trackpoint.jenks_curvature */ 6833248478Sjkim sc->tpinfo.jenks = 0x87; 6834248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6835248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6836248478Sjkim "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6837248478Sjkim sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE, 6838248478Sjkim trackpoint_sysctl, "I", 6839248478Sjkim "Jenks curvature"); 6840248478Sjkim 6841248478Sjkim /* hw.psm.trackpoint.z_time */ 6842248478Sjkim sc->tpinfo.ztime = 0x26; 6843248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6844248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6845248478Sjkim "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6846248478Sjkim sc, TRACKPOINT_SYSCTL_Z_TIME, 6847248478Sjkim trackpoint_sysctl, "I", 6848248478Sjkim "Z time constant"); 6849248478Sjkim 6850248478Sjkim /* hw.psm.trackpoint.press_to_select */ 6851248478Sjkim sc->tpinfo.pts = 0x00; 6852248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6853248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6854248478Sjkim "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6855248478Sjkim sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT, 6856248478Sjkim trackpoint_sysctl, "I", 6857248478Sjkim "Press to Select"); 6858248478Sjkim 6859248478Sjkim /* hw.psm.trackpoint.skip_backups */ 6860248478Sjkim sc->tpinfo.skipback = 0x00; 6861248478Sjkim SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6862248478Sjkim SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6863248478Sjkim "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6864248478Sjkim sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS, 6865248478Sjkim trackpoint_sysctl, "I", 6866248478Sjkim "Skip backups from drags"); 6867248478Sjkim} 6868248478Sjkim 6869281441Srpaulostatic void 6870281441Srpauloset_trackpoint_parameters(struct psm_softc *sc) 6871281441Srpaulo{ 6872281441Srpaulo trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity); 6873281441Srpaulo trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau); 6874281441Srpaulo trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia); 6875281441Srpaulo trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach); 6876281441Srpaulo trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys); 6877281441Srpaulo trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag); 6878281441Srpaulo trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh); 6879281441Srpaulo trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold); 6880281441Srpaulo trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks); 6881281441Srpaulo trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime); 6882281441Srpaulo if (sc->tpinfo.pts == 0x01) 6883281441Srpaulo trackpoint_command(sc, 0x47, 0x2c, 0x01); 6884281441Srpaulo if (sc->tpinfo.skipback == 0x01) 6885281441Srpaulo trackpoint_command(sc, 0x47, 0x2d, 0x08); 6886281441Srpaulo} 6887281441Srpaulo 6888248478Sjkimstatic int 6889284320Sglebiusenable_trackpoint(struct psm_softc *sc, enum probearg arg) 6890248478Sjkim{ 6891284320Sglebius KBDC kbdc = sc->kbdc; 6892248478Sjkim int id; 6893248478Sjkim 6894284585Srpaulo /* 6895284585Srpaulo * If called from enable_synaptics(), make sure that passthrough 6896284585Srpaulo * mode is enabled so we can reach the trackpoint. 6897284585Srpaulo * However, passthrough mode must be disabled before setting the 6898284585Srpaulo * trackpoint parameters, as rackpoint_command() enables and disables 6899284585Srpaulo * passthrough mode on its own. 6900284585Srpaulo */ 6901284585Srpaulo if (sc->synhw.capPassthrough) 6902284585Srpaulo synaptics_passthrough_on(sc); 6903284585Srpaulo 6904248478Sjkim if (send_aux_command(kbdc, 0xe1) != PSM_ACK || 6905248478Sjkim read_aux_data(kbdc) != 0x01) 6906284585Srpaulo goto no_trackpoint; 6907248478Sjkim id = read_aux_data(kbdc); 6908248478Sjkim if (id < 0x01) 6909284585Srpaulo goto no_trackpoint; 6910284450Sglebius if (arg == PROBE) 6911248478Sjkim sc->tphw = id; 6912248478Sjkim if (!trackpoint_support) 6913284585Srpaulo goto no_trackpoint; 6914248478Sjkim 6915284585Srpaulo if (sc->synhw.capPassthrough) 6916284585Srpaulo synaptics_passthrough_off(sc); 6917284585Srpaulo 6918284320Sglebius if (arg == PROBE) { 6919248478Sjkim trackpoint_sysctl_create_tree(sc); 6920281441Srpaulo /* 6921281441Srpaulo * Don't overwrite hwid and buttons when we are 6922281441Srpaulo * a guest device. 6923281441Srpaulo */ 6924281441Srpaulo if (!sc->synhw.capPassthrough) { 6925281441Srpaulo sc->hw.hwid = id; 6926281441Srpaulo sc->hw.buttons = 3; 6927281441Srpaulo } 6928248478Sjkim } 6929248478Sjkim 6930284320Sglebius set_trackpoint_parameters(sc); 6931284320Sglebius 6932248478Sjkim return (TRUE); 6933284585Srpaulo 6934284585Srpaulono_trackpoint: 6935284585Srpaulo if (sc->synhw.capPassthrough) 6936284585Srpaulo synaptics_passthrough_off(sc); 6937284585Srpaulo 6938284585Srpaulo return (FALSE); 6939248478Sjkim} 6940248478Sjkim 694149965Syokota/* Interlink electronics VersaPad */ 694249965Syokotastatic int 6943284320Sglebiusenable_versapad(struct psm_softc *sc, enum probearg arg) 694449965Syokota{ 6945284320Sglebius KBDC kbdc = sc->kbdc; 6946178019Sjkim int data[3]; 694749965Syokota 6948178019Sjkim set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 6949178019Sjkim set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 6950178019Sjkim set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6951178019Sjkim set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6952178019Sjkim set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6953178019Sjkim set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6954178019Sjkim if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 6955178019Sjkim return (FALSE); 6956178019Sjkim if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 6957178019Sjkim return (FALSE); 6958178019Sjkim set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 695949965Syokota 6960178019Sjkim return (TRUE); /* PS/2 absolute mode */ 696149965Syokota} 696249965Syokota 6963307576Sgonzo/* Elantech Touchpad */ 6964307576Sgonzostatic int 6965307576Sgonzoelantech_read_1(KBDC kbdc, int hwversion, int reg, int *val) 6966307576Sgonzo{ 6967307576Sgonzo int res, readcmd, retidx; 6968307576Sgonzo int resp[3]; 6969307576Sgonzo 6970307576Sgonzo readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR; 6971307576Sgonzo retidx = hwversion == 4 ? 1 : 0; 6972307576Sgonzo 6973307576Sgonzo res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6974307576Sgonzo res |= send_aux_command(kbdc, readcmd) != PSM_ACK; 6975307576Sgonzo res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6976307576Sgonzo res |= send_aux_command(kbdc, reg) != PSM_ACK; 6977307576Sgonzo res |= get_mouse_status(kbdc, resp, 0, 3) != 3; 6978307576Sgonzo 6979307576Sgonzo if (res == 0) 6980307576Sgonzo *val = resp[retidx]; 6981307576Sgonzo 6982307576Sgonzo return (res); 6983307576Sgonzo} 6984307576Sgonzo 6985307576Sgonzostatic int 6986307576Sgonzoelantech_write_1(KBDC kbdc, int hwversion, int reg, int val) 6987307576Sgonzo{ 6988307576Sgonzo int res, writecmd; 6989307576Sgonzo 6990307576Sgonzo writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR; 6991307576Sgonzo 6992307576Sgonzo res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6993307576Sgonzo res |= send_aux_command(kbdc, writecmd) != PSM_ACK; 6994307576Sgonzo res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6995307576Sgonzo res |= send_aux_command(kbdc, reg) != PSM_ACK; 6996307576Sgonzo if (hwversion == 4) { 6997307576Sgonzo res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6998307576Sgonzo res |= send_aux_command(kbdc, writecmd) != PSM_ACK; 6999307576Sgonzo } 7000307576Sgonzo res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 7001307576Sgonzo res |= send_aux_command(kbdc, val) != PSM_ACK; 7002307576Sgonzo res |= set_mouse_scaling(kbdc, 1) == 0; 7003307576Sgonzo 7004307576Sgonzo return (res); 7005307576Sgonzo} 7006307576Sgonzo 7007307576Sgonzostatic int 7008307576Sgonzoelantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp) 7009307576Sgonzo{ 7010307576Sgonzo int res; 7011307576Sgonzo 7012307576Sgonzo if (hwversion == 2) { 7013307576Sgonzo res = set_mouse_scaling(kbdc, 1) == 0; 7014307576Sgonzo res |= mouse_ext_command(kbdc, cmd) == 0; 7015307576Sgonzo } else { 7016307576Sgonzo res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 7017307576Sgonzo res |= send_aux_command(kbdc, cmd) != PSM_ACK; 7018307576Sgonzo } 7019307576Sgonzo res |= get_mouse_status(kbdc, resp, 0, 3) != 3; 7020307576Sgonzo 7021307576Sgonzo return (res); 7022307576Sgonzo} 7023307576Sgonzo 7024307576Sgonzostatic int 7025307576Sgonzoelantech_init(KBDC kbdc, elantechhw_t *elanhw) 7026307576Sgonzo{ 7027307576Sgonzo int i, val, res, hwversion, reg10; 7028307576Sgonzo 7029307576Sgonzo /* set absolute mode */ 7030307576Sgonzo hwversion = elanhw->hwversion; 7031307576Sgonzo reg10 = -1; 7032307576Sgonzo switch (hwversion) { 7033307576Sgonzo case 2: 7034307576Sgonzo reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4; 7035307576Sgonzo res = elantech_write_1(kbdc, hwversion, 0x10, reg10); 7036307576Sgonzo if (res) 7037307576Sgonzo break; 7038307576Sgonzo res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A); 7039307576Sgonzo break; 7040307576Sgonzo case 3: 7041307576Sgonzo reg10 = 0x0b; 7042307576Sgonzo res = elantech_write_1(kbdc, hwversion, 0x10, reg10); 7043307576Sgonzo break; 7044307576Sgonzo case 4: 7045307576Sgonzo res = elantech_write_1(kbdc, hwversion, 0x07, 0x01); 7046307576Sgonzo break; 7047307576Sgonzo default: 7048307576Sgonzo res = 1; 7049307576Sgonzo } 7050307576Sgonzo 7051307576Sgonzo /* Read back reg 0x10 to ensure hardware is ready. */ 7052307576Sgonzo if (res == 0 && reg10 >= 0) { 7053307576Sgonzo for (i = 0; i < 5; i++) { 7054307576Sgonzo if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0) 7055307576Sgonzo break; 7056307576Sgonzo DELAY(2000); 7057307576Sgonzo } 7058307576Sgonzo if (i == 5) 7059307576Sgonzo res = 1; 7060307576Sgonzo } 7061307576Sgonzo 7062307576Sgonzo if (res) 7063307576Sgonzo printf("couldn't set absolute mode\n"); 7064307576Sgonzo 7065307576Sgonzo return (res); 7066307576Sgonzo} 7067307576Sgonzo 7068307576Sgonzostatic void 7069307576Sgonzoelantech_init_synaptics(struct psm_softc *sc) 7070307576Sgonzo{ 7071307576Sgonzo 7072307576Sgonzo /* Set capabilites required by movement smother */ 7073307576Sgonzo sc->synhw.infoMajor = sc->elanhw.hwversion; 7074307576Sgonzo sc->synhw.infoMinor = sc->elanhw.fwversion; 7075307576Sgonzo sc->synhw.infoXupmm = sc->elanhw.dpmmx; 7076307576Sgonzo sc->synhw.infoYupmm = sc->elanhw.dpmmy; 7077307576Sgonzo sc->synhw.verticalScroll = 0; 7078307576Sgonzo sc->synhw.nExtendedQueries = 4; 7079307576Sgonzo sc->synhw.capExtended = 1; 7080307576Sgonzo sc->synhw.capPassthrough = sc->elanhw.hastrackpoint; 7081307576Sgonzo sc->synhw.capClickPad = sc->elanhw.isclickpad; 7082307576Sgonzo sc->synhw.capMultiFinger = 1; 7083318500Swulf if (sc->elanhw.issemimt) 7084318500Swulf sc->synhw.capAdvancedGestures = 1; 7085318500Swulf else 7086318500Swulf sc->synhw.capReportsV = 1; 7087307576Sgonzo sc->synhw.capPalmDetect = 1; 7088307576Sgonzo sc->synhw.capPen = 0; 7089307576Sgonzo sc->synhw.capReportsMax = 1; 7090307576Sgonzo sc->synhw.maximumXCoord = sc->elanhw.sizex; 7091307576Sgonzo sc->synhw.maximumYCoord = sc->elanhw.sizey; 7092307576Sgonzo sc->synhw.capReportsMin = 1; 7093307576Sgonzo sc->synhw.minimumXCoord = 0; 7094307576Sgonzo sc->synhw.minimumYCoord = 0; 7095307576Sgonzo 7096307576Sgonzo if (sc->syninfo.sysctl_tree == NULL) { 7097307576Sgonzo synaptics_sysctl_create_tree(sc, "elantech", 7098307576Sgonzo "Elantech Touchpad"); 7099307576Sgonzo 7100307576Sgonzo /* 7101307576Sgonzo * Adjust synaptic smoother tunables 7102307576Sgonzo * 1. Disable finger detection pressure threshold. Unlike 7103307576Sgonzo * synaptics we assume the finger is acting when packet with 7104307576Sgonzo * its X&Y arrives not when pressure exceedes some threshold 7105307576Sgonzo * 2. Disable unrelated features like margins and noisy areas 7106307576Sgonzo * 3. Disable virtual scroll areas as 2nd finger is preferable 7107307576Sgonzo * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized 7108307576Sgonzo * softbuttons 7109307576Sgonzo * 5. Scale down divisors and movement lengths by a factor of 3 7110307576Sgonzo * where 3 is Synaptics to Elantech (~2200/800) dpi ratio 7111307576Sgonzo */ 7112307576Sgonzo 7113307576Sgonzo /* Set reporting range to be equal touchpad size */ 7114307576Sgonzo sc->syninfo.max_x = sc->elanhw.sizex; 7115307576Sgonzo sc->syninfo.max_y = sc->elanhw.sizey; 7116307576Sgonzo 7117307576Sgonzo /* Disable finger detection pressure threshold */ 7118307576Sgonzo sc->syninfo.min_pressure = 1; 7119307576Sgonzo 7120318500Swulf /* Adjust palm width to nearly match synaptics w=10 */ 7121318500Swulf sc->syninfo.max_width = 7; 7122318500Swulf 7123318500Swulf /* Elans often report double & triple taps as single event */ 7124318500Swulf sc->syninfo.tap_min_queue = 1; 7125318500Swulf 7126307576Sgonzo /* Use full area of touchpad */ 7127307576Sgonzo sc->syninfo.margin_top = 0; 7128307576Sgonzo sc->syninfo.margin_right = 0; 7129307576Sgonzo sc->syninfo.margin_bottom = 0; 7130307576Sgonzo sc->syninfo.margin_left = 0; 7131307576Sgonzo 7132307576Sgonzo /* Disable noisy area */ 7133307576Sgonzo sc->syninfo.na_top = 0; 7134307576Sgonzo sc->syninfo.na_right = 0; 7135307576Sgonzo sc->syninfo.na_bottom = 0; 7136307576Sgonzo sc->syninfo.na_left = 0; 7137307576Sgonzo 7138307576Sgonzo /* Tune divisors and movement lengths */ 7139307576Sgonzo sc->syninfo.weight_len_squared = 200; 7140307576Sgonzo sc->syninfo.div_min = 3; 7141307576Sgonzo sc->syninfo.div_max = 6; 7142307576Sgonzo sc->syninfo.div_max_na = 10; 7143307576Sgonzo sc->syninfo.div_len = 30; 7144307576Sgonzo sc->syninfo.tap_max_delta = 25; 7145307576Sgonzo 7146307576Sgonzo /* Disable virtual scrolling areas and tune its divisors */ 7147307576Sgonzo sc->syninfo.vscroll_hor_area = 0; 7148307576Sgonzo sc->syninfo.vscroll_ver_area = 0; 7149307576Sgonzo sc->syninfo.vscroll_min_delta = 15; 7150307576Sgonzo sc->syninfo.vscroll_div_min = 30; 7151307576Sgonzo sc->syninfo.vscroll_div_max = 50; 7152307576Sgonzo 7153307576Sgonzo /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */ 7154307576Sgonzo if (sc->elanhw.isclickpad) { 7155307576Sgonzo sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4; 7156307576Sgonzo sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25; 7157307576Sgonzo sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25; 7158307576Sgonzo } 7159307576Sgonzo } 7160307576Sgonzo 7161307576Sgonzo return; 7162307576Sgonzo} 7163307576Sgonzo 7164307576Sgonzostatic int 7165307576Sgonzoenable_elantech(struct psm_softc *sc, enum probearg arg) 7166307576Sgonzo{ 7167307576Sgonzo static const int ic2hw[] = 7168307576Sgonzo /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 7169349541Swulf { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }; 7170318500Swulf static const int fw_sizes[][3] = { 7171318500Swulf /* FW.vers MaxX MaxY */ 7172318500Swulf { 0x020030, 1152, 768 }, 7173318500Swulf { 0x020800, 1152, 768 }, 7174318500Swulf { 0x020b00, 1152, 768 }, 7175318500Swulf { 0x040215, 900, 500 }, 7176318500Swulf { 0x040216, 819, 405 }, 7177318500Swulf { 0x040219, 900, 500 }, 7178318500Swulf }; 7179307576Sgonzo elantechhw_t elanhw; 7180318500Swulf int icversion, hwversion, xtr, i, id, resp[3], dpix, dpiy; 7181307576Sgonzo KBDC kbdc = sc->kbdc; 7182307576Sgonzo 7183307576Sgonzo VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n")); 7184307576Sgonzo 7185307576Sgonzo set_mouse_scaling(kbdc, 1); 7186307576Sgonzo set_mouse_scaling(kbdc, 1); 7187307576Sgonzo set_mouse_scaling(kbdc, 1); 7188307576Sgonzo if (get_mouse_status(kbdc, resp, 0, 3) != 3) 7189307576Sgonzo return (FALSE); 7190307576Sgonzo 7191307576Sgonzo if (!ELANTECH_MAGIC(resp)) 7192307576Sgonzo return (FALSE); 7193307576Sgonzo 7194307576Sgonzo /* Identify the Touchpad version. */ 7195307576Sgonzo if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp)) 7196307576Sgonzo return (FALSE); 7197307576Sgonzo 7198307576Sgonzo bzero(&elanhw, sizeof(elanhw)); 7199307576Sgonzo 7200307576Sgonzo elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2]; 7201307576Sgonzo icversion = resp[0] & 0x0f; 7202307576Sgonzo hwversion = ic2hw[icversion]; 7203307576Sgonzo 7204307576Sgonzo if (verbose >= 2) 7205307576Sgonzo printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n", 7206307576Sgonzo hwversion, elanhw.fwversion); 7207307576Sgonzo 7208307576Sgonzo if (ELANTECH_HW_IS_V1(elanhw.fwversion)) { 7209307576Sgonzo printf (" Unsupported touchpad hardware (v1)\n"); 7210307576Sgonzo return (FALSE); 7211307576Sgonzo } 7212307576Sgonzo if (hwversion == 0) { 7213307576Sgonzo printf (" Unknown touchpad hardware (firmware v.0x%06x)\n", 7214307576Sgonzo elanhw.fwversion); 7215307576Sgonzo return (FALSE); 7216307576Sgonzo } 7217307576Sgonzo 7218307576Sgonzo /* Get the Touchpad model information. */ 7219307576Sgonzo elanhw.hwversion = hwversion; 7220307576Sgonzo elanhw.issemimt = hwversion == 2; 7221307576Sgonzo elanhw.isclickpad = (resp[1] & 0x10) != 0; 7222307576Sgonzo elanhw.hascrc = (resp[1] & 0x40) != 0; 7223307576Sgonzo elanhw.haspressure = elanhw.fwversion >= 0x020800; 7224307576Sgonzo 7225307576Sgonzo /* Read the capability bits. */ 7226307576Sgonzo if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) { 7227307576Sgonzo printf(" Failed to read capability bits\n"); 7228307576Sgonzo return (FALSE); 7229307576Sgonzo } 7230307576Sgonzo 7231318500Swulf elanhw.ntracesx = imax(resp[1], 3); 7232318500Swulf elanhw.ntracesy = imax(resp[2], 3); 7233307576Sgonzo elanhw.hastrackpoint = (resp[0] & 0x80) != 0; 7234307576Sgonzo 7235307576Sgonzo /* Get the touchpad resolution */ 7236307576Sgonzo switch (hwversion) { 7237307576Sgonzo case 4: 7238307576Sgonzo if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp) 7239307576Sgonzo == 0) { 7240307576Sgonzo dpix = (resp[1] & 0x0f) * 10 + 790; 7241307576Sgonzo dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790; 7242307576Sgonzo elanhw.dpmmx = (dpix * 10 + 5) / 254; 7243307576Sgonzo elanhw.dpmmy = (dpiy * 10 + 5) / 254; 7244307576Sgonzo break; 7245307576Sgonzo } 7246307576Sgonzo /* FALLTHROUGH */ 7247307576Sgonzo case 2: 7248307576Sgonzo case 3: 7249307576Sgonzo elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */ 7250307576Sgonzo break; 7251307576Sgonzo } 7252307576Sgonzo 7253307576Sgonzo if (!elantech_support) 7254307576Sgonzo return (FALSE); 7255307576Sgonzo 7256307576Sgonzo if (elantech_init(kbdc, &elanhw)) { 7257307576Sgonzo printf("couldn't initialize elantech touchpad\n"); 7258307576Sgonzo return (FALSE); 7259307576Sgonzo } 7260307576Sgonzo 7261307576Sgonzo /* 7262307576Sgonzo * Get the touchpad reporting range. 7263307576Sgonzo * On HW v.3 touchpads it should be done after switching hardware 7264307576Sgonzo * to real resolution mode (by setting bit 3 of reg10) 7265307576Sgonzo */ 7266318500Swulf elanhw.dptracex = elanhw.dptracey = 64; 7267318500Swulf for (i = 0; i < nitems(fw_sizes); i++) { 7268318500Swulf if (elanhw.fwversion == fw_sizes[i][0]) { 7269318500Swulf elanhw.sizex = fw_sizes[i][1]; 7270318500Swulf elanhw.sizey = fw_sizes[i][2]; 7271318500Swulf goto found; 7272318500Swulf } 7273318500Swulf } 7274307576Sgonzo if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) { 7275307576Sgonzo printf(" Failed to read touchpad size\n"); 7276307576Sgonzo elanhw.sizex = 10000; /* Arbitrary high values to */ 7277307576Sgonzo elanhw.sizey = 10000; /* prevent clipping in smoother */ 7278307576Sgonzo } else if (hwversion == 2) { 7279307576Sgonzo if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) && 7280307576Sgonzo !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) { 7281318500Swulf elanhw.dptracex = resp[1] / 2; 7282318500Swulf elanhw.dptracey = resp[2] / 2; 7283307576Sgonzo } 7284318500Swulf xtr = ((elanhw.fwversion >> 8) == 0x0208) ? 1 : 2; 7285318500Swulf elanhw.sizex = (elanhw.ntracesx - xtr) * elanhw.dptracex; 7286318500Swulf elanhw.sizey = (elanhw.ntracesy - xtr) * elanhw.dptracey; 7287307576Sgonzo } else { 7288307576Sgonzo elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1]; 7289307576Sgonzo elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2]; 7290318500Swulf xtr = (elanhw.sizex % (elanhw.ntracesx - 2) == 0) ? 2 : 1; 7291318500Swulf elanhw.dptracex = elanhw.sizex / (elanhw.ntracesx - xtr); 7292318500Swulf elanhw.dptracey = elanhw.sizey / (elanhw.ntracesy - xtr); 7293307576Sgonzo } 7294318500Swulffound: 7295307576Sgonzo if (verbose >= 2) { 7296307576Sgonzo printf(" Model information:\n"); 7297307576Sgonzo printf(" MaxX: %d\n", elanhw.sizex); 7298307576Sgonzo printf(" MaxY: %d\n", elanhw.sizey); 7299307576Sgonzo printf(" DpmmX: %d\n", elanhw.dpmmx); 7300307576Sgonzo printf(" DpmmY: %d\n", elanhw.dpmmy); 7301307576Sgonzo printf(" TracesX: %d\n", elanhw.ntracesx); 7302307576Sgonzo printf(" TracesY: %d\n", elanhw.ntracesy); 7303318500Swulf printf(" DptraceX: %d\n", elanhw.dptracex); 7304318500Swulf printf(" DptraceY: %d\n", elanhw.dptracey); 7305307576Sgonzo printf(" SemiMT: %d\n", elanhw.issemimt); 7306307576Sgonzo printf(" Clickpad: %d\n", elanhw.isclickpad); 7307307576Sgonzo printf(" Trackpoint: %d\n", elanhw.hastrackpoint); 7308307576Sgonzo printf(" CRC: %d\n", elanhw.hascrc); 7309307576Sgonzo printf(" Pressure: %d\n", elanhw.haspressure); 7310307576Sgonzo } 7311307576Sgonzo 7312307576Sgonzo VLOG(3, (LOG_DEBUG, "elantech: END init\n")); 7313307576Sgonzo 7314307576Sgonzo if (arg == PROBE) { 7315307576Sgonzo sc->elanhw = elanhw; 7316307576Sgonzo sc->hw.buttons = 3; 7317307576Sgonzo 7318307576Sgonzo /* Initialize synaptics movement smoother */ 7319307576Sgonzo elantech_init_synaptics(sc); 7320307576Sgonzo 7321307576Sgonzo for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 7322307576Sgonzo PSM_FINGER_RESET(sc->elanaction.fingers[id]); 7323307576Sgonzo } 7324307576Sgonzo 7325307576Sgonzo return (TRUE); 7326307576Sgonzo} 7327307576Sgonzo 7328147271Smarius/* 7329147271Smarius * Return true if 'now' is earlier than (start + (secs.usecs)). 7330147271Smarius * Now may be NULL and the function will fetch the current time from 7331147271Smarius * getmicrouptime(), or a cached 'now' can be passed in. 7332147271Smarius * All values should be numbers derived from getmicrouptime(). 7333147271Smarius */ 733441016Sdfrstatic int 7335147271Smariustimeelapsed(start, secs, usecs, now) 7336147271Smarius const struct timeval *start, *now; 7337147271Smarius int secs, usecs; 7338147271Smarius{ 7339147271Smarius struct timeval snow, tv; 7340147271Smarius 7341147271Smarius /* if there is no 'now' passed in, the get it as a convience. */ 7342147271Smarius if (now == NULL) { 7343147271Smarius getmicrouptime(&snow); 7344147271Smarius now = &snow; 7345147271Smarius } 7346178019Sjkim 7347147271Smarius tv.tv_sec = secs; 7348147271Smarius tv.tv_usec = usecs; 7349147271Smarius timevaladd(&tv, start); 7350147271Smarius return (timevalcmp(&tv, now, <)); 7351147271Smarius} 7352147271Smarius 7353147271Smariusstatic int 735454629Syokotapsmresume(device_t dev) 735541016Sdfr{ 7356178019Sjkim struct psm_softc *sc = device_get_softc(dev); 7357178019Sjkim int unit = device_get_unit(dev); 7358178019Sjkim int err; 735941016Sdfr 7360178019Sjkim VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit)); 736141016Sdfr 7362233580Sjkim if ((sc->config & 7363233580Sjkim (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0) 7364178019Sjkim return (0); 736558230Syokota 7366178019Sjkim err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 736741016Sdfr 7368178019Sjkim if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 7369178019Sjkim /* 7370178019Sjkim * Release the blocked process; it must be notified that 7371178019Sjkim * the device cannot be accessed anymore. 7372178019Sjkim */ 7373178019Sjkim sc->state &= ~PSM_ASLP; 7374178019Sjkim wakeup(sc); 7375178019Sjkim } 737641016Sdfr 7377178019Sjkim VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit)); 737841016Sdfr 7379178019Sjkim return (err); 738041016Sdfr} 738141016Sdfr 738252997SpeterDRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 7383321060Swulf#ifdef EVDEV_SUPPORT 7384321060SwulfMODULE_DEPEND(psm, evdev, 1, 1, 1); 7385321060Swulf#endif 738683147Syokota 7387147271Smarius#ifdef DEV_ISA 7388147271Smarius 738983147Syokota/* 739083147Syokota * This sucks up assignments from PNPBIOS and ACPI. 739183147Syokota */ 739283147Syokota 739383931Syokota/* 739483931Syokota * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 739583931Syokota * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 739683931Syokota * can be probed and attached only after the AT keyboard controller is 739783931Syokota * attached, we shall quietly reserve the IRQ resource for later use. 739883931Syokota * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 739983931Syokota * copy the IRQ resource to the PS/2 mouse device instance hanging 740083931Syokota * under the keyboard controller, then probe and attach it. 740183931Syokota */ 740283147Syokota 740383147Syokotastatic devclass_t psmcpnp_devclass; 740483147Syokota 740583147Syokotastatic device_probe_t psmcpnp_probe; 740683147Syokotastatic device_attach_t psmcpnp_attach; 740783147Syokota 740883147Syokotastatic device_method_t psmcpnp_methods[] = { 740983147Syokota DEVMETHOD(device_probe, psmcpnp_probe), 741083147Syokota DEVMETHOD(device_attach, psmcpnp_attach), 7411178019Sjkim 741283147Syokota { 0, 0 } 741383147Syokota}; 741483147Syokota 741583147Syokotastatic driver_t psmcpnp_driver = { 741683147Syokota PSMCPNP_DRIVER_NAME, 741783147Syokota psmcpnp_methods, 7418331170Seadler sizeof(struct psmcpnp_softc), 741983147Syokota}; 742083147Syokota 742183147Syokotastatic struct isa_pnp_id psmcpnp_ids[] = { 742288188Ssheldonh { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 7423156730Stakawata { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */ 7424156730Stakawata { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */ 742583147Syokota { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 742683147Syokota { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 7427117117Smikeh { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */ 7428156730Stakawata { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */ 742983492Syokota { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 743084407Stakawata { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 7431109679Shsu { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */ 7432109679Shsu { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */ 7433109710Smarcel { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */ 7434109679Shsu { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */ 743583147Syokota { 0 } 743683147Syokota}; 743783147Syokota 7438331170Seadler/* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */ 7439344165Swulfstatic struct isa_pnp_id topbtpad_ids[] = { 7440344165Swulf { 0x1700ae30, "Lenovo PS/2 clickpad port" }, /* LEN0017, ThinkPad */ 7441344165Swulf { 0x1800ae30, "Lenovo PS/2 clickpad port" }, /* LEN0018, ThinkPad */ 7442344165Swulf { 0x1900ae30, "Lenovo PS/2 clickpad port" }, /* LEN0019, ThinkPad */ 7443344165Swulf { 0x2300ae30, "Lenovo PS/2 clickpad port" }, /* LEN0023, ThinkPad */ 7444344165Swulf { 0x2a00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002a, ThinkPad */ 7445344165Swulf { 0x2b00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002b, ThinkPad */ 7446344165Swulf { 0x2c00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002c, ThinkPad */ 7447344165Swulf { 0x2d00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002d, ThinkPad */ 7448344165Swulf { 0x2e00ae30, "Lenovo PS/2 clickpad port" }, /* LEN002e, ThinkPad */ 7449344165Swulf { 0x3300ae30, "Lenovo PS/2 clickpad port" }, /* LEN0033, ThinkPad */ 7450344165Swulf { 0x3400ae30, "Lenovo PS/2 clickpad port" }, /* LEN0034, ThinkPad */ 7451344165Swulf { 0x3500ae30, "Lenovo PS/2 clickpad port" }, /* LEN0035, ThinkPad */ 7452344165Swulf { 0x3600ae30, "Lenovo PS/2 clickpad port" }, /* LEN0036, ThinkPad */ 7453344165Swulf { 0x3700ae30, "Lenovo PS/2 clickpad port" }, /* LEN0037, ThinkPad */ 7454344165Swulf { 0x3800ae30, "Lenovo PS/2 clickpad port" }, /* LEN0038, ThinkPad */ 7455344165Swulf { 0x3900ae30, "Lenovo PS/2 clickpad port" }, /* LEN0039, ThinkPad */ 7456344165Swulf { 0x4100ae30, "Lenovo PS/2 clickpad port" }, /* LEN0041, ThinkPad */ 7457344165Swulf { 0x4200ae30, "Lenovo PS/2 clickpad port" }, /* LEN0042, ThinkPad */ 7458344165Swulf { 0x4500ae30, "Lenovo PS/2 clickpad port" }, /* LEN0045, ThinkPad */ 7459344165Swulf { 0x4700ae30, "Lenovo PS/2 clickpad port" }, /* LEN0047, ThinkPad */ 7460344165Swulf { 0x4900ae30, "Lenovo PS/2 clickpad port" }, /* LEN0049, ThinkPad */ 7461344165Swulf { 0x0020ae30, "Lenovo PS/2 clickpad port" }, /* LEN2000, ThinkPad */ 7462344165Swulf { 0x0120ae30, "Lenovo PS/2 clickpad port" }, /* LEN2001, ThinkPad */ 7463344165Swulf { 0x0220ae30, "Lenovo PS/2 clickpad port" }, /* LEN2002, ThinkPad */ 7464344165Swulf { 0x0320ae30, "Lenovo PS/2 clickpad port" }, /* LEN2003, ThinkPad */ 7465344165Swulf { 0x0420ae30, "Lenovo PS/2 clickpad port" }, /* LEN2004, ThinkPad */ 7466344165Swulf { 0x0520ae30, "Lenovo PS/2 clickpad port" }, /* LEN2005, ThinkPad */ 7467344165Swulf { 0x0620ae30, "Lenovo PS/2 clickpad port" }, /* LEN2006, ThinkPad */ 7468344165Swulf { 0x0720ae30, "Lenovo PS/2 clickpad port" }, /* LEN2007, ThinkPad */ 7469344165Swulf { 0x0820ae30, "Lenovo PS/2 clickpad port" }, /* LEN2008, ThinkPad */ 7470344165Swulf { 0x0920ae30, "Lenovo PS/2 clickpad port" }, /* LEN2009, ThinkPad */ 7471344165Swulf { 0x0a20ae30, "Lenovo PS/2 clickpad port" }, /* LEN200a, ThinkPad */ 7472344165Swulf { 0x0b20ae30, "Lenovo PS/2 clickpad port" }, /* LEN200b, ThinkPad */ 7473344165Swulf { 0 } 7474344165Swulf}; 7475344165Swulf 7476344165Swulf/* _HID list for quirk detection. Any device below has _CID from psmcpnp_ids */ 7477331170Seadlerstatic struct isa_pnp_id forcepad_ids[] = { 7478331170Seadler { 0x0d302e4f, "HP PS/2 forcepad port" }, /* SYN300D, EB 1040 */ 7479331170Seadler { 0x14302e4f, "HP PS/2 forcepad port" }, /* SYN3014, EB 1040 */ 7480331170Seadler { 0 } 7481331170Seadler}; 7482331170Seadler 748383147Syokotastatic int 748483147Syokotacreate_a_copy(device_t atkbdc, device_t me) 748583147Syokota{ 748683147Syokota device_t psm; 748783147Syokota u_long irq; 748883147Syokota 748983931Syokota /* find the PS/2 mouse device instance under the keyboard controller */ 749083931Syokota psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 7491178019Sjkim device_get_unit(atkbdc)); 749283147Syokota if (psm == NULL) 7493178019Sjkim return (ENXIO); 749483931Syokota if (device_get_state(psm) != DS_NOTPRESENT) 7495178019Sjkim return (0); 749683147Syokota 749783931Syokota /* move our resource to the found device */ 749883931Syokota irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 7499216491Sjhb bus_delete_resource(me, SYS_RES_IRQ, 0); 750083147Syokota bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 750183147Syokota 750283147Syokota /* ...then probe and attach it */ 7503178019Sjkim return (device_probe_and_attach(psm)); 750483147Syokota} 750583147Syokota 750683147Syokotastatic int 750783147Syokotapsmcpnp_probe(device_t dev) 750883147Syokota{ 7509331170Seadler struct psmcpnp_softc *sc = device_get_softc(dev); 751083931Syokota struct resource *res; 751183931Syokota u_long irq; 751283931Syokota int rid; 751383147Syokota 7514331170Seadler if (ISA_PNP_PROBE(device_get_parent(dev), dev, forcepad_ids) == 0) 7515331170Seadler sc->type = PSMCPNP_FORCEPAD; 7516344165Swulf else if (ISA_PNP_PROBE(device_get_parent(dev), dev, topbtpad_ids) == 0) 7517344165Swulf sc->type = PSMCPNP_TOPBUTTONPAD; 7518331170Seadler else if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids) == 0) 7519331170Seadler sc->type = PSMCPNP_GENERIC; 7520331170Seadler else 7521178019Sjkim return (ENXIO); 752283147Syokota 752383931Syokota /* 752483931Syokota * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 752583931Syokota * to the PS/2 mouse device node. But, some buggy PnP BIOS 752683931Syokota * declares the PS/2 mouse device node without an IRQ resource! 752783931Syokota * If this happens, we shall refer to device hints. 752883931Syokota * If we still don't find it there, use a hardcoded value... XXX 752983931Syokota */ 753083931Syokota rid = 0; 753183931Syokota irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 753283931Syokota if (irq <= 0) { 753383931Syokota if (resource_long_value(PSM_DRIVER_NAME, 7534178019Sjkim device_get_unit(dev),"irq", &irq) != 0) 753583931Syokota irq = 12; /* XXX */ 753683931Syokota device_printf(dev, "irq resource info is missing; " 7537178019Sjkim "assuming irq %ld\n", irq); 753883931Syokota bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1); 753983931Syokota } 7540216491Sjhb res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0); 754183931Syokota bus_release_resource(dev, SYS_RES_IRQ, rid, res); 754283147Syokota 754383147Syokota /* keep quiet */ 754483147Syokota if (!bootverbose) 754583147Syokota device_quiet(dev); 754683492Syokota 754783931Syokota return ((res == NULL) ? ENXIO : 0); 754883147Syokota} 754983147Syokota 755083147Syokotastatic int 755183147Syokotapsmcpnp_attach(device_t dev) 755283147Syokota{ 755383492Syokota device_t atkbdc; 755483147Syokota 755583931Syokota /* find the keyboard controller, which may be on acpi* or isa* bus */ 755683931Syokota atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 7557178019Sjkim device_get_unit(dev)); 7558178019Sjkim if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) 755983492Syokota create_a_copy(atkbdc, dev); 756083492Syokota 7561178019Sjkim return (0); 756283147Syokota} 756383147Syokota 756483147SyokotaDRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0); 756583147SyokotaDRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0); 7566147271Smarius 7567147271Smarius#endif /* DEV_ISA */ 7568