1 /*- 2 * Copyright (c) 1992, 1993 Erik Forsberg. 3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED 13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 */ 23 /* 24 * Ported to 386bsd Oct 17, 1992 25 * Sandi Donno, Computer Science, University of Cape Town, South Africa 26 * Please send bug reports to sandi@cs.uct.ac.za 27 * 28 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca - 29 * although I was only partially successful in getting the alpha release 30 * of his "driver for the Logitech and ATI Inport Bus mice for use with 31 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless 32 * found his code to be an invaluable reference when porting this driver 33 * to 386bsd. 34 * 35 * Further modifications for latest 386BSD+patchkit and port to NetBSD, 36 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993 37 * 38 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by 39 * Andrew Herbert - 12 June 1993 40 * 41 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu> 42 * - 13 June 1993 43 * 44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp> 45 * - 24 October 1993 46 * 47 * Hardware access routines and probe logic rewritten by 48 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp> 49 * - 3, 14, 22 October 1996. 50 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'... 51 * - 14, 30 November 1996. Uses `kbdio.c'. 52 * - 13 December 1996. Uses queuing version of `kbdio.c'. 53 * - January/February 1997. Tweaked probe logic for 54 * HiNote UltraII/Latitude/Armada laptops. 55 * - 30 July 1997. Added APM support. 56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX). 57 * Improved sync check logic. 58 * Vendor specific support routines. 59 * 60 * $FreeBSD: src/sys/dev/atkbdc/psm.c,v 1.107 2010/09/09 07:52:15 ed Exp $ 61 * $FreeBSD: stable/11/sys/dev/atkbdc/psm.c 307576 2016-10-18 20:17:57Z gonzo $ 62 */ 63 64 #include "opt_psm.h" 65 66 #include <sys/param.h> 67 #include <sys/systm.h> 68 #include <sys/kernel.h> 69 #include <sys/module.h> 70 #include <sys/bus.h> 71 #include <sys/conf.h> 72 #include <sys/device.h> 73 #include <sys/event.h> 74 #include <sys/syslog.h> 75 #include <sys/malloc.h> 76 #include <sys/rman.h> 77 #include <sys/sysctl.h> 78 #include <sys/thread2.h> 79 #include <sys/time.h> 80 #include <sys/uio.h> 81 #include <sys/machintr.h> 82 83 #include <machine/clock.h> 84 #include <machine/limits.h> 85 #include <sys/mouse.h> 86 87 #include <bus/isa/isavar.h> 88 #include <dev/misc/kbd/atkbdcreg.h> 89 90 #include <sys/poll.h> 91 #include <sys/signalvar.h> 92 #include <sys/filio.h> 93 94 95 /* 96 * Driver specific options: the following options may be set by 97 * `options' statements in the kernel configuration file. 98 */ 99 100 /* debugging */ 101 #ifndef PSM_DEBUG 102 #define PSM_DEBUG 0 /* 103 * logging: 0: none, 1: brief, 2: verbose 104 * 3: sync errors, 4: all packets 105 */ 106 #endif 107 #define VLOG(level, args) do { \ 108 if (verbose >= level) \ 109 log args; \ 110 } while (0) 111 112 #ifndef PSM_INPUT_TIMEOUT 113 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */ 114 #endif 115 116 #ifndef PSM_TAP_TIMEOUT 117 #define PSM_TAP_TIMEOUT 125000 118 #endif 119 120 #ifndef PSM_TAP_THRESHOLD 121 #define PSM_TAP_THRESHOLD 25 122 #endif 123 124 /* end of driver specific options */ 125 126 #define PSM_DRIVER_NAME "psm" 127 #define PSMCPNP_DRIVER_NAME "psmcpnp" 128 129 /* input queue */ 130 #define PSM_BUFSIZE 960 131 #define PSM_SMALLBUFSIZE 240 132 133 /* operation levels */ 134 #define PSM_LEVEL_BASE 0 135 #define PSM_LEVEL_STANDARD 1 136 #define PSM_LEVEL_NATIVE 2 137 #define PSM_LEVEL_MIN PSM_LEVEL_BASE 138 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE 139 140 /* Logitech PS2++ protocol */ 141 #define MOUSE_PS2PLUS_CHECKBITS(b) \ 142 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f)) 143 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \ 144 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4)) 145 146 /* some macros */ 147 #define PSM_UNIT(dev) (minor(dev) >> 1) 148 #define PSM_NBLOCKIO(dev) (minor(dev) & 1) 149 #define PSM_MKMINOR(unit,block) ((((unit) & 0xff) << 1) | ((block) ? 0:1)) 150 151 /* ring buffer */ 152 typedef struct ringbuf { 153 int count; /* # of valid elements in the buffer */ 154 int head; /* head pointer */ 155 int tail; /* tail poiner */ 156 u_char buf[PSM_BUFSIZE]; 157 } ringbuf_t; 158 159 /* data buffer */ 160 typedef struct packetbuf { 161 u_char ipacket[16]; /* interim input buffer */ 162 int inputbytes; /* # of bytes in the input buffer */ 163 } packetbuf_t; 164 165 #ifndef PSM_PACKETQUEUE 166 #define PSM_PACKETQUEUE 128 167 #endif 168 169 170 typedef struct synapticsinfo { 171 struct sysctl_oid *sysctl_tree; 172 struct sysctl_ctx_list sysctl_ctx; 173 int directional_scrolls; 174 int two_finger_scroll; 175 int min_pressure; 176 int max_pressure; 177 int max_width; 178 int margin_top; 179 int margin_right; 180 int margin_bottom; 181 int margin_left; 182 int na_top; 183 int na_right; 184 int na_bottom; 185 int na_left; 186 int window_min; 187 int window_max; 188 int multiplicator; 189 int weight_current; 190 int weight_previous; 191 int weight_previous_na; 192 int weight_len_squared; 193 int div_min; 194 int div_max; 195 int div_max_na; 196 int div_len; 197 int tap_max_delta; 198 int tap_min_queue; 199 int taphold_timeout; 200 int vscroll_ver_area; 201 int vscroll_hor_area; 202 int vscroll_min_delta; 203 int vscroll_div_min; 204 int vscroll_div_max; 205 int touchpad_off; 206 int softbuttons_y; 207 int softbutton2_x; 208 int softbutton3_x; 209 int max_x; 210 int max_y; 211 } synapticsinfo_t; 212 213 typedef struct synapticspacket { 214 int x; 215 int y; 216 } synapticspacket_t; 217 218 #define SYNAPTICS_PACKETQUEUE 10 219 #define SYNAPTICS_QUEUE_CURSOR(x) \ 220 (x + SYNAPTICS_PACKETQUEUE) % SYNAPTICS_PACKETQUEUE 221 222 #define SYNAPTICS_VERSION_GE(synhw, major, minor) \ 223 ((synhw).infoMajor > (major) || \ 224 ((synhw).infoMajor == (major) && (synhw).infoMinor >= (minor))) 225 226 227 228 typedef struct smoother { 229 synapticspacket_t queue[SYNAPTICS_PACKETQUEUE]; 230 int queue_len; 231 int queue_cursor; 232 int start_x; 233 int start_y; 234 int avg_dx; 235 int avg_dy; 236 int squelch_x; 237 int squelch_y; 238 int is_fuzzy; 239 int active; 240 } smoother_t; 241 242 typedef struct gesture { 243 int window_min; 244 int fingers_nb; 245 int tap_button; 246 int in_taphold; 247 int in_vscroll; 248 int zmax; /* maximum pressure value */ 249 struct timeval taptimeout; /* tap timeout for touchpads */ 250 } gesture_t; 251 252 enum { 253 TRACKPOINT_SYSCTL_SENSITIVITY, 254 TRACKPOINT_SYSCTL_NEGATIVE_INERTIA, 255 TRACKPOINT_SYSCTL_UPPER_PLATEAU, 256 TRACKPOINT_SYSCTL_BACKUP_RANGE, 257 TRACKPOINT_SYSCTL_DRAG_HYSTERESIS, 258 TRACKPOINT_SYSCTL_MINIMUM_DRAG, 259 TRACKPOINT_SYSCTL_UP_THRESHOLD, 260 TRACKPOINT_SYSCTL_THRESHOLD, 261 TRACKPOINT_SYSCTL_JENKS_CURVATURE, 262 TRACKPOINT_SYSCTL_Z_TIME, 263 TRACKPOINT_SYSCTL_PRESS_TO_SELECT, 264 TRACKPOINT_SYSCTL_SKIP_BACKUPS 265 }; 266 267 typedef struct trackpointinfo { 268 struct sysctl_ctx_list sysctl_ctx; 269 struct sysctl_oid *sysctl_tree; 270 int sensitivity; 271 int inertia; 272 int uplateau; 273 int reach; 274 int draghys; 275 int mindrag; 276 int upthresh; 277 int threshold; 278 int jenks; 279 int ztime; 280 int pts; 281 int skipback; 282 } trackpointinfo_t; 283 284 typedef struct finger { 285 int x; 286 int y; 287 int p; 288 int w; 289 int flags; 290 } finger_t; 291 #define PSM_FINGERS 2 /* # of processed fingers */ 292 #define PSM_FINGER_IS_PEN (1<<0) 293 #define PSM_FINGER_FUZZY (1<<1) 294 #define PSM_FINGER_DEFAULT_P tap_threshold 295 #define PSM_FINGER_DEFAULT_W 1 296 #define PSM_FINGER_IS_SET(f) ((f).x != -1 && (f).y != -1 && (f).p != 0) 297 #define PSM_FINGER_RESET(f) do { \ 298 (f) = (finger_t) { .x = -1, .y = -1, .p = 0, .w = 0, .flags = 0 }; \ 299 } while (0) 300 301 typedef struct elantechhw { 302 int hwversion; 303 int fwversion; 304 int sizex; 305 int sizey; 306 int dpmmx; 307 int dpmmy; 308 int ntracesx; 309 int ntracesy; 310 int issemimt; 311 int isclickpad; 312 int hascrc; 313 int hastrackpoint; 314 int haspressure; 315 } elantechhw_t; 316 317 /* minimum versions supported by this driver */ 318 #define ELANTECH_HW_IS_V1(fwver) ((fwver) < 0x020030 || (fwver) == 0x020600) 319 320 #define ELANTECH_MAGIC(magic) \ 321 ((magic)[0] == 0x3c && (magic)[1] == 0x03 && \ 322 ((magic)[2] == 0xc8 || (magic)[2] == 0x00)) 323 324 #define ELANTECH_FW_ID 0x00 325 #define ELANTECH_FW_VERSION 0x01 326 #define ELANTECH_CAPABILITIES 0x02 327 #define ELANTECH_SAMPLE 0x03 328 #define ELANTECH_RESOLUTION 0x04 329 #define ELANTECH_REG_READ 0x10 330 #define ELANTECH_REG_WRITE 0x11 331 #define ELANTECH_REG_RDWR 0x00 332 #define ELANTECH_CUSTOM_CMD 0xf8 333 334 #define ELANTECH_MAX_FINGERS PSM_FINGERS 335 336 #define ELANTECH_FINGER_SET_XYP(pb) (finger_t) { \ 337 .x = (((pb)->ipacket[1] & 0x0f) << 8) | (pb)->ipacket[2], \ 338 .y = (((pb)->ipacket[4] & 0x0f) << 8) | (pb)->ipacket[5], \ 339 .p = ((pb)->ipacket[1] & 0xf0) | (((pb)->ipacket[4] >> 4) & 0x0f), \ 340 .w = PSM_FINGER_DEFAULT_W, \ 341 .flags = 0 \ 342 } 343 344 enum { 345 ELANTECH_PKT_NOP, 346 ELANTECH_PKT_TRACKPOINT, 347 ELANTECH_PKT_V2_COMMON, 348 ELANTECH_PKT_V2_2FINGER, 349 ELANTECH_PKT_V3, 350 ELANTECH_PKT_V4_STATUS, 351 ELANTECH_PKT_V4_HEAD, 352 ELANTECH_PKT_V4_MOTION 353 }; 354 355 #define ELANTECH_PKT_IS_TRACKPOINT(pb) (((pb)->ipacket[3] & 0x0f) == 0x06) 356 #define ELANTECH_PKT_IS_DEBOUNCE(pb, hwversion) ((hwversion) == 4 ? 0 : \ 357 (pb)->ipacket[0] == ((hwversion) == 2 ? 0x84 : 0xc4) && \ 358 (pb)->ipacket[1] == 0xff && (pb)->ipacket[2] == 0xff && \ 359 (pb)->ipacket[3] == 0x02 && (pb)->ipacket[4] == 0xff && \ 360 (pb)->ipacket[5] == 0xff) 361 #define ELANTECH_PKT_IS_V2(pb) \ 362 (((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x0f) == 0x02) 363 #define ELANTECH_PKT_IS_V3_HEAD(pb, hascrc) ((hascrc) ? \ 364 ((pb)->ipacket[3] & 0x09) == 0x08 : \ 365 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0xcf) == 0x02) 366 #define ELANTECH_PKT_IS_V3_TAIL(pb, hascrc) ((hascrc) ? \ 367 ((pb)->ipacket[3] & 0x09) == 0x09 : \ 368 ((pb)->ipacket[0] & 0x0c) == 0x0c && ((pb)->ipacket[3] & 0xce) == 0x0c) 369 #define ELANTECH_PKT_IS_V4(pb, hascrc) ((hascrc) ? \ 370 ((pb)->ipacket[3] & 0x08) == 0x00 : \ 371 ((pb)->ipacket[0] & 0x0c) == 0x04 && ((pb)->ipacket[3] & 0x1c) == 0x10) 372 373 typedef struct elantechaction { 374 finger_t fingers[ELANTECH_MAX_FINGERS]; 375 int mask; 376 } elantechaction_t; 377 378 /* driver control block */ 379 struct psm_softc { /* Driver status information */ 380 int unit; 381 struct kqinfo rkq; /* Processes with registered kevents */ 382 u_char state; /* Mouse driver state */ 383 int config; /* driver configuration flags */ 384 int flags; /* other flags */ 385 KBDC kbdc; /* handle to access kbd controller */ 386 struct resource *intr; /* IRQ resource */ 387 void *ih; /* interrupt handle */ 388 mousehw_t hw; /* hardware information */ 389 synapticshw_t synhw; /* Synaptics hardware information */ 390 synapticsinfo_t syninfo; /* Synaptics configuration */ 391 smoother_t smoother[PSM_FINGERS]; /* Motion smoothing */ 392 gesture_t gesture; /* Gesture context */ 393 elantechhw_t elanhw; /* Elantech hardware information */ 394 elantechaction_t elanaction; /* Elantech action context */ 395 int tphw; /* TrackPoint hardware information */ 396 trackpointinfo_t tpinfo; /* TrackPoint configuration */ 397 mousemode_t mode; /* operation mode */ 398 mousemode_t dflt_mode; /* default operation mode */ 399 mousestatus_t status; /* accumulated mouse movement */ 400 ringbuf_t queue; /* mouse status queue */ 401 packetbuf_t pqueue[PSM_PACKETQUEUE]; /* mouse data queue */ 402 int pqueue_start; /* start of data in queue */ 403 int pqueue_end; /* end of data in queue */ 404 int button; /* the latest button state */ 405 int xold; /* previous absolute X position */ 406 int yold; /* previous absolute Y position */ 407 int xaverage; /* average X position */ 408 int yaverage; /* average Y position */ 409 int squelch; /* level to filter movement at low speed */ 410 int syncerrors; /* # of bytes discarded to synchronize */ 411 int pkterrors; /* # of packets failed during quaranteen. */ 412 struct timeval inputtimeout; 413 struct timeval lastsoftintr; /* time of last soft interrupt */ 414 struct timeval lastinputerr; /* time last sync error happened */ 415 struct timeval idletimeout; 416 packetbuf_t idlepacket; /* packet to send after idle timeout */ 417 int watchdog; /* watchdog timer flag */ 418 struct callout callout; /* watchdog timer call out */ 419 struct callout softcallout; /* buffer timer call out */ 420 struct cdev *dev; 421 struct cdev *bdev; 422 int lasterr; 423 int cmdcount; 424 int extended_buttons; 425 }; 426 static devclass_t psm_devclass; 427 428 #define SYN_OFFSET(field) offsetof(struct psm_softc, syninfo.field) 429 enum { 430 SYNAPTICS_SYSCTL_MIN_PRESSURE = SYN_OFFSET(min_pressure), 431 SYNAPTICS_SYSCTL_MAX_PRESSURE = SYN_OFFSET(max_pressure), 432 SYNAPTICS_SYSCTL_MAX_WIDTH = SYN_OFFSET(max_width), 433 SYNAPTICS_SYSCTL_MARGIN_TOP = SYN_OFFSET(margin_top), 434 SYNAPTICS_SYSCTL_MARGIN_RIGHT = SYN_OFFSET(margin_right), 435 SYNAPTICS_SYSCTL_MARGIN_BOTTOM = SYN_OFFSET(margin_bottom), 436 SYNAPTICS_SYSCTL_MARGIN_LEFT = SYN_OFFSET(margin_left), 437 SYNAPTICS_SYSCTL_NA_TOP = SYN_OFFSET(na_top), 438 SYNAPTICS_SYSCTL_NA_RIGHT = SYN_OFFSET(na_right), 439 SYNAPTICS_SYSCTL_NA_BOTTOM = SYN_OFFSET(na_bottom), 440 SYNAPTICS_SYSCTL_NA_LEFT = SYN_OFFSET(na_left), 441 SYNAPTICS_SYSCTL_WINDOW_MIN = SYN_OFFSET(window_min), 442 SYNAPTICS_SYSCTL_WINDOW_MAX = SYN_OFFSET(window_max), 443 SYNAPTICS_SYSCTL_MULTIPLICATOR = SYN_OFFSET(multiplicator), 444 SYNAPTICS_SYSCTL_WEIGHT_CURRENT = SYN_OFFSET(weight_current), 445 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS = SYN_OFFSET(weight_previous), 446 SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA = SYN_OFFSET(weight_previous_na), 447 SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED = SYN_OFFSET(weight_len_squared), 448 SYNAPTICS_SYSCTL_DIV_MIN = SYN_OFFSET(div_min), 449 SYNAPTICS_SYSCTL_DIV_MAX = SYN_OFFSET(div_max), 450 SYNAPTICS_SYSCTL_DIV_MAX_NA = SYN_OFFSET(div_max_na), 451 SYNAPTICS_SYSCTL_DIV_LEN = SYN_OFFSET(div_len), 452 SYNAPTICS_SYSCTL_TAP_MAX_DELTA = SYN_OFFSET(tap_max_delta), 453 SYNAPTICS_SYSCTL_TAP_MIN_QUEUE = SYN_OFFSET(tap_min_queue), 454 SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT = SYN_OFFSET(taphold_timeout), 455 SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA = SYN_OFFSET(vscroll_hor_area), 456 SYNAPTICS_SYSCTL_VSCROLL_VER_AREA = SYN_OFFSET(vscroll_ver_area), 457 SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA = SYN_OFFSET(vscroll_min_delta), 458 SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN = SYN_OFFSET(vscroll_div_min), 459 SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX = SYN_OFFSET(vscroll_div_max), 460 SYNAPTICS_SYSCTL_TOUCHPAD_OFF = SYN_OFFSET(touchpad_off), 461 SYNAPTICS_SYSCTL_SOFTBUTTONS_Y = SYN_OFFSET(softbuttons_y), 462 SYNAPTICS_SYSCTL_SOFTBUTTON2_X = SYN_OFFSET(softbutton2_x), 463 SYNAPTICS_SYSCTL_SOFTBUTTON3_X = SYN_OFFSET(softbutton3_x), 464 }; 465 466 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit)) 467 468 /* driver state flags (state) */ 469 #define PSM_VALID 0x80 470 #define PSM_OPEN 1 /* Device is open */ 471 #define PSM_ASLP 2 /* Waiting for mouse data */ 472 #define PSM_SOFTARMED 4 /* Software interrupt armed */ 473 #define PSM_NEED_SYNCBITS 8 /* Set syncbits using next data pkt */ 474 475 /* driver configuration flags (config) */ 476 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */ 477 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */ 478 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */ 479 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */ 480 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */ 481 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */ 482 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */ 483 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */ 484 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */ 485 486 #define PSM_CONFIG_FLAGS \ 487 (PSM_CONFIG_RESOLUTION | \ 488 PSM_CONFIG_ACCEL | \ 489 PSM_CONFIG_NOCHECKSYNC | \ 490 PSM_CONFIG_NOIDPROBE | \ 491 PSM_CONFIG_NORESET | \ 492 PSM_CONFIG_FORCETAP | \ 493 PSM_CONFIG_IGNPORTERROR | \ 494 PSM_CONFIG_HOOKRESUME | \ 495 PSM_CONFIG_INITAFTERSUSPEND) 496 497 /* other flags (flags) */ 498 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */ 499 500 #define kbdcp(p) ((atkbdc_softc_t *)(p)) 501 502 /* Tunables */ 503 static int tap_enabled = -1; 504 TUNABLE_INT("hw.psm.tap_enabled", &tap_enabled); 505 506 static int synaptics_support = 0; 507 TUNABLE_INT("hw.psm.synaptics_support", &synaptics_support); 508 509 static int verbose = PSM_DEBUG; 510 TUNABLE_INT("debug.psm.loglevel", &verbose); 511 512 static int trackpoint_support = 0; 513 TUNABLE_INT("hw.psm.trackpoint_support",&trackpoint_support); 514 515 static int elantech_support = 0; 516 TUNABLE_INT("hw.psm.elantech_support",&elantech_support); 517 518 519 /* for backward compatibility */ 520 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t) 521 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t) 522 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t) 523 524 typedef struct old_mousehw { 525 int buttons; 526 int iftype; 527 int type; 528 int hwid; 529 } old_mousehw_t; 530 531 typedef struct old_mousemode { 532 int protocol; 533 int rate; 534 int resolution; 535 int accelfactor; 536 } old_mousemode_t; 537 /* packet formatting function */ 538 typedef int packetfunc_t(struct psm_softc *, u_char *, int *, int, 539 mousestatus_t *); 540 541 /* function prototypes */ 542 static void psmidentify(driver_t *, device_t); 543 static int psmprobe(device_t); 544 static int psmattach(device_t); 545 static int psmdetach(device_t); 546 static int psmresume(device_t); 547 548 static d_open_t psmopen; 549 static d_close_t psmclose; 550 static d_read_t psmread; 551 static d_write_t psmwrite; 552 static d_ioctl_t psmioctl; 553 static d_kqfilter_t psmkqfilter; 554 555 static int enable_aux_dev(KBDC); 556 static int disable_aux_dev(KBDC); 557 static int get_mouse_status(KBDC, int *, int, int); 558 static int get_aux_id(KBDC); 559 static int set_mouse_sampling_rate(KBDC, int); 560 static int set_mouse_scaling(KBDC, int); 561 static int set_mouse_resolution(KBDC, int); 562 static int set_mouse_mode(KBDC); 563 static int get_mouse_buttons(KBDC); 564 static int is_a_mouse(int); 565 static void recover_from_error(KBDC); 566 static int restore_controller(KBDC, int); 567 static int doinitialize(struct psm_softc *, mousemode_t *); 568 static int doopen(struct psm_softc *, int); 569 static int reinitialize(struct psm_softc *, int); 570 static char *model_name(int); 571 static void psmsoftintr(void *); 572 static void psmsoftintridle(void *); 573 static void psmintr(void *); 574 static void psmtimeout(void *); 575 static void psmfilter_detach(struct knote *); 576 static int psmfilter(struct knote *, long); 577 static int timeelapsed(const struct timeval *, int, int, 578 const struct timeval *); 579 static void dropqueue(struct psm_softc *); 580 static void flushpackets(struct psm_softc *); 581 static void proc_mmanplus(struct psm_softc *, packetbuf_t *, 582 mousestatus_t *, int *, int *, int *); 583 static int proc_synaptics(struct psm_softc *, packetbuf_t *, 584 mousestatus_t *, int *, int *, int *); 585 static void proc_versapad(struct psm_softc *, packetbuf_t *, 586 mousestatus_t *, int *, int *, int *); 587 static int proc_elantech(struct psm_softc *, packetbuf_t *, 588 mousestatus_t *, int *, int *, int *); 589 static int psmpalmdetect(struct psm_softc *, finger_t *, int); 590 static void psmgestures(struct psm_softc *, finger_t *, int, 591 mousestatus_t *); 592 static void psmsmoother(struct psm_softc *, finger_t *, int, 593 mousestatus_t *, int *, int *); 594 static int tame_mouse(struct psm_softc *, packetbuf_t *, mousestatus_t *, 595 u_char *); 596 597 /* vendor specific features */ 598 enum probearg { PROBE, REINIT }; 599 typedef int probefunc_t(struct psm_softc *, enum probearg); 600 601 static int mouse_id_proc1(KBDC, int, int, int *); 602 static int mouse_ext_command(KBDC, int); 603 604 static int enable_groller(struct psm_softc *sc, enum probearg arg); 605 static int enable_gmouse(struct psm_softc *sc, enum probearg arg); 606 static int enable_aglide(struct psm_softc *sc, enum probearg arg); 607 static int enable_kmouse(struct psm_softc *sc, enum probearg arg); 608 static int enable_msexplorer(struct psm_softc *sc, enum probearg arg); 609 static int enable_msintelli(struct psm_softc *sc, enum probearg arg); 610 static int enable_4dmouse(struct psm_softc *sc, enum probearg arg); 611 static int enable_4dplus(struct psm_softc *sc, enum probearg arg); 612 static int enable_mmanplus(struct psm_softc *sc, enum probearg arg); 613 static int enable_synaptics(struct psm_softc *sc, enum probearg arg); 614 static int enable_trackpoint(struct psm_softc *sc, enum probearg arg); 615 static int enable_versapad(struct psm_softc *sc, enum probearg arg); 616 static int enable_elantech(struct psm_softc *sc, enum probearg arg); 617 618 static void set_trackpoint_parameters(struct psm_softc *sc); 619 static void synaptics_passthrough_on(struct psm_softc *sc); 620 static void synaptics_passthrough_off(struct psm_softc *sc); 621 static int synaptics_preferred_mode(struct psm_softc *sc); 622 static void synaptics_set_mode(struct psm_softc *sc, int mode_byte); 623 624 625 626 static struct { 627 int model; 628 u_char syncmask; 629 int packetsize; 630 probefunc_t *probefunc; 631 } vendortype[] = { 632 /* 633 * WARNING: the order of probe is very important. Don't mess it 634 * unless you know what you are doing. 635 */ 636 { MOUSE_MODEL_NET, /* Genius NetMouse */ 637 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse }, 638 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */ 639 0xc8, 6, enable_groller }, 640 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */ 641 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus }, 642 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */ 643 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer }, 644 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */ 645 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse }, 646 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */ 647 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus }, 648 { MOUSE_MODEL_SYNAPTICS, /* Synaptics Touchpad */ 649 0xc0, MOUSE_SYNAPTICS_PACKETSIZE, enable_synaptics }, 650 { MOUSE_MODEL_ELANTECH, /* Elantech Touchpad */ 651 0x04, MOUSE_ELANTECH_PACKETSIZE, enable_elantech }, 652 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */ 653 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli }, 654 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */ 655 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide }, 656 { MOUSE_MODEL_THINK, /* Kensington ThinkingMouse */ 657 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse }, 658 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */ 659 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad }, 660 { MOUSE_MODEL_TRACKPOINT, /* IBM/Lenovo TrackPoint */ 661 0xc0, MOUSE_PS2_PACKETSIZE, enable_trackpoint }, 662 { MOUSE_MODEL_GENERIC, 663 0xc0, MOUSE_PS2_PACKETSIZE, NULL }, 664 }; 665 #define GENERIC_MOUSE_ENTRY (NELEM(vendortype) - 1) 666 667 /* device driver declaration */ 668 static device_method_t psm_methods[] = { 669 /* Device interface */ 670 DEVMETHOD(device_identify, psmidentify), 671 DEVMETHOD(device_probe, psmprobe), 672 DEVMETHOD(device_attach, psmattach), 673 DEVMETHOD(device_detach, psmdetach), 674 DEVMETHOD(device_resume, psmresume), 675 676 DEVMETHOD_END 677 }; 678 679 static driver_t psm_driver = { 680 PSM_DRIVER_NAME, 681 psm_methods, 682 sizeof(struct psm_softc) 683 }; 684 685 static struct dev_ops psm_ops = { 686 { PSM_DRIVER_NAME, 0, 0 }, 687 .d_open = psmopen, 688 .d_close = psmclose, 689 .d_read = psmread, 690 .d_write = psmwrite, 691 .d_ioctl = psmioctl, 692 .d_kqfilter = psmkqfilter 693 }; 694 695 696 /* device I/O routines */ 697 static int 698 enable_aux_dev(KBDC kbdc) 699 { 700 701 int res; 702 703 res = send_aux_command(kbdc, PSMC_ENABLE_DEV); 704 VLOG(2, (LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res)); 705 706 return (res == PSM_ACK); 707 } 708 709 static int 710 disable_aux_dev(KBDC kbdc) 711 { 712 713 int res; 714 715 res = send_aux_command(kbdc, PSMC_DISABLE_DEV); 716 VLOG(2, (LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res)); 717 718 return (res == PSM_ACK); 719 } 720 721 static int 722 get_mouse_status(KBDC kbdc, int *status, int flag, int len) 723 { 724 725 int cmd; 726 int res; 727 int i; 728 729 switch (flag) { 730 case 0: 731 default: 732 cmd = PSMC_SEND_DEV_STATUS; 733 break; 734 case 1: 735 cmd = PSMC_SEND_DEV_DATA; 736 break; 737 } 738 empty_aux_buffer(kbdc, 5); 739 res = send_aux_command(kbdc, cmd); 740 VLOG(2, (LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n", 741 (flag == 1) ? "DATA" : "STATUS", res)); 742 if (res != PSM_ACK) 743 return (0); 744 745 for (i = 0; i < len; ++i) { 746 status[i] = read_aux_data(kbdc); 747 if (status[i] < 0) 748 break; 749 } 750 751 VLOG(1, (LOG_DEBUG, "psm: %s %02x %02x %02x\n", 752 (flag == 1) ? "data" : "status", status[0], status[1], status[2])); 753 754 return (i); 755 } 756 757 static int 758 get_aux_id(KBDC kbdc) 759 { 760 int id; 761 int res; 762 763 empty_aux_buffer(kbdc, 5); 764 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID); 765 VLOG(2, (LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res)); 766 if (res != PSM_ACK) 767 return (-1); 768 769 /* 10ms delay */ 770 DRIVERSLEEP(10000); 771 772 id = read_aux_data(kbdc); 773 VLOG(2, (LOG_DEBUG, "psm: device ID: %04x\n", id)); 774 775 return (id); 776 } 777 778 static int 779 set_mouse_sampling_rate(KBDC kbdc, int rate) 780 { 781 782 int res; 783 784 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate); 785 VLOG(2, (LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res)); 786 787 return ((res == PSM_ACK) ? rate : -1); 788 } 789 790 static int 791 set_mouse_scaling(KBDC kbdc, int scale) 792 { 793 794 int res; 795 796 switch (scale) { 797 case 1: 798 default: 799 scale = PSMC_SET_SCALING11; 800 break; 801 case 2: 802 scale = PSMC_SET_SCALING21; 803 break; 804 } 805 res = send_aux_command(kbdc, scale); 806 VLOG(2, (LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n", 807 (scale == PSMC_SET_SCALING21) ? "21" : "11", res)); 808 809 return (res == PSM_ACK); 810 } 811 812 /* `val' must be 0 through PSMD_MAX_RESOLUTION */ 813 static int 814 set_mouse_resolution(KBDC kbdc, int val) 815 { 816 817 int res; 818 819 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val); 820 VLOG(2, (LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res)); 821 822 return ((res == PSM_ACK) ? val : -1); 823 } 824 825 /* 826 * NOTE: once `set_mouse_mode()' is called, the mouse device must be 827 * re-enabled by calling `enable_aux_dev()' 828 */ 829 static int 830 set_mouse_mode(KBDC kbdc) 831 { 832 833 int res; 834 835 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE); 836 VLOG(2, (LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res)); 837 838 return (res == PSM_ACK); 839 } 840 841 static int 842 get_mouse_buttons(KBDC kbdc) 843 { 844 845 int c = 2; /* assume two buttons by default */ 846 int status[3]; 847 848 /* 849 * NOTE: a special sequence to obtain Logitech Mouse specific 850 * information: set resolution to 25 ppi, set scaling to 1:1, set 851 * scaling to 1:1, set scaling to 1:1. Then the second byte of the 852 * mouse status bytes is the number of available buttons. 853 * Some manufactures also support this sequence. 854 */ 855 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 856 return (c); 857 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1) && 858 set_mouse_scaling(kbdc, 1) && 859 get_mouse_status(kbdc, status, 0, 3) >= 3 && status[1] != 0) 860 return (status[1]); 861 return (c); 862 } 863 864 /* misc subroutines */ 865 /* 866 * Someday, I will get the complete list of valid pointing devices and 867 * their IDs... XXX 868 */ 869 static int 870 is_a_mouse(int id) 871 { 872 #if 0 873 static int valid_ids[] = { 874 PSM_MOUSE_ID, /* mouse */ 875 PSM_BALLPOINT_ID, /* ballpoint device */ 876 PSM_INTELLI_ID, /* Intellimouse */ 877 PSM_EXPLORER_ID, /* Intellimouse Explorer */ 878 -1 /* end of table */ 879 }; 880 int i; 881 882 for (i = 0; valid_ids[i] >= 0; ++i) 883 if (valid_ids[i] == id) 884 return (TRUE); 885 return (FALSE); 886 #else 887 return (TRUE); 888 #endif 889 } 890 891 static char * 892 model_name(int model) 893 { 894 895 static struct { 896 int model_code; 897 char *model_name; 898 } models[] = { 899 { MOUSE_MODEL_NETSCROLL, "NetScroll" }, 900 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" }, 901 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" }, 902 { MOUSE_MODEL_THINK, "ThinkingMouse" }, 903 { MOUSE_MODEL_INTELLI, "IntelliMouse" }, 904 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" }, 905 { MOUSE_MODEL_VERSAPAD, "VersaPad" }, 906 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" }, 907 { MOUSE_MODEL_4D, "4D Mouse" }, 908 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" }, 909 { MOUSE_MODEL_SYNAPTICS, "Synaptics Touchpad" }, 910 { MOUSE_MODEL_TRACKPOINT, "IBM/Lenovo TrackPoint" }, 911 { MOUSE_MODEL_ELANTECH, "Elantech Touchpad" }, 912 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" }, 913 { MOUSE_MODEL_UNKNOWN, "Unknown" }, 914 }; 915 int i; 916 917 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) 918 if (models[i].model_code == model) 919 break; 920 return (models[i].model_name); 921 } 922 923 static void 924 recover_from_error(KBDC kbdc) 925 { 926 927 /* discard anything left in the output buffer */ 928 empty_both_buffers(kbdc, 10); 929 930 #if 0 931 /* 932 * NOTE: KBDC_RESET_KBD may not restore the communication between the 933 * keyboard and the controller. 934 */ 935 reset_kbd(kbdc); 936 #else 937 /* 938 * NOTE: somehow diagnostic and keyboard port test commands bring the 939 * keyboard back. 940 */ 941 if (!test_controller(kbdc)) 942 log(LOG_ERR, "psm: keyboard controller failed.\n"); 943 /* if there isn't a keyboard in the system, the following error is OK */ 944 if (test_kbd_port(kbdc) != 0) 945 VLOG(1, (LOG_ERR, "psm: keyboard port failed.\n")); 946 #endif 947 } 948 949 static int 950 restore_controller(KBDC kbdc, int command_byte) 951 { 952 953 empty_both_buffers(kbdc, 10); 954 955 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) { 956 log(LOG_ERR, "psm: failed to restore the keyboard controller " 957 "command byte.\n"); 958 empty_both_buffers(kbdc, 10); 959 return (FALSE); 960 } else { 961 empty_both_buffers(kbdc, 10); 962 return (TRUE); 963 } 964 } 965 966 /* 967 * Re-initialize the aux port and device. The aux port must be enabled 968 * and its interrupt must be disabled before calling this routine. 969 * The aux device will be disabled before returning. 970 * The keyboard controller must be locked via `kbdc_lock()' before 971 * calling this routine. 972 */ 973 static int 974 doinitialize(struct psm_softc *sc, mousemode_t *mode) 975 { 976 977 KBDC kbdc = sc->kbdc; 978 int stat[3]; 979 int i; 980 981 switch((i = test_aux_port(kbdc))) { 982 case 1: /* ignore these errors */ 983 case 2: 984 case 3: 985 case PSM_ACK: 986 if (verbose) 987 log(LOG_DEBUG, 988 "psm%d: strange result for test aux port (%d).\n", 989 sc->unit, i); 990 /* FALLTHROUGH */ 991 case 0: /* no error */ 992 break; 993 case -1: /* time out */ 994 default: /* error */ 995 recover_from_error(kbdc); 996 if (sc->config & PSM_CONFIG_IGNPORTERROR) 997 break; 998 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n", 999 sc->unit, i); 1000 return (FALSE); 1001 } 1002 1003 if (sc->config & PSM_CONFIG_NORESET) { 1004 /* 1005 * Don't try to reset the pointing device. It may possibly 1006 * be left in the unknown state, though... 1007 */ 1008 } else { 1009 /* 1010 * NOTE: some controllers appears to hang the `keyboard' when 1011 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 1012 */ 1013 if (!reset_aux_dev(kbdc)) { 1014 recover_from_error(kbdc); 1015 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", 1016 sc->unit); 1017 return (FALSE); 1018 } 1019 } 1020 1021 /* 1022 * both the aux port and the aux device is functioning, see 1023 * if the device can be enabled. 1024 */ 1025 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) { 1026 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", 1027 sc->unit); 1028 return (FALSE); 1029 } 1030 empty_both_buffers(kbdc, 10); /* remove stray data if any */ 1031 1032 /* Re-enable the mouse. */ 1033 for (i = 0; vendortype[i].probefunc != NULL; ++i) 1034 if (vendortype[i].model == sc->hw.model) 1035 (*vendortype[i].probefunc)(sc, REINIT); 1036 1037 /* set mouse parameters */ 1038 if (mode != (mousemode_t *)NULL) { 1039 if (mode->rate > 0) 1040 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate); 1041 if (mode->resolution >= 0) 1042 mode->resolution = 1043 set_mouse_resolution(kbdc, mode->resolution); 1044 set_mouse_scaling(kbdc, 1); 1045 set_mouse_mode(kbdc); 1046 } 1047 1048 /* Record sync on the next data packet we see. */ 1049 sc->flags |= PSM_NEED_SYNCBITS; 1050 1051 /* just check the status of the mouse */ 1052 if (get_mouse_status(kbdc, stat, 0, 3) < 3) 1053 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n", 1054 sc->unit); 1055 1056 return (TRUE); 1057 } 1058 1059 static int 1060 doopen(struct psm_softc *sc, int command_byte) 1061 { 1062 1063 int stat[3]; 1064 1065 /* 1066 * FIXME: Synaptics TouchPad seems to go back to Relative Mode with 1067 * no obvious reason. Thus we check the current mode and restore the 1068 * Absolute Mode if it was cleared. 1069 * 1070 * The previous hack at the end of psmprobe() wasn't efficient when 1071 * moused(8) was restarted. 1072 * 1073 * A Reset (FF) or Set Defaults (F6) command would clear the 1074 * Absolute Mode bit. But a verbose boot or debug.psm.loglevel=5 1075 * doesn't show any evidence of such a command. 1076 */ 1077 if (sc->hw.model == MOUSE_MODEL_SYNAPTICS) { 1078 mouse_ext_command(sc->kbdc, 1); 1079 get_mouse_status(sc->kbdc, stat, 0, 3); 1080 if ((SYNAPTICS_VERSION_GE(sc->synhw, 7, 5) || 1081 stat[1] == 0x47) && 1082 stat[2] == 0x40) { 1083 synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 1084 VLOG(5, (LOG_DEBUG, "psm%d: Synaptics Absolute Mode " 1085 "hopefully restored\n", 1086 sc->unit)); 1087 } 1088 1089 } 1090 1091 /* 1092 * A user may want to disable tap and drag gestures on a Synaptics 1093 * TouchPad when it operates in Relative Mode. 1094 */ 1095 if (sc->hw.model == MOUSE_MODEL_GENERIC) { 1096 if (tap_enabled > 0) { 1097 /* 1098 * Enable tap & drag gestures. We use a Mode Byte 1099 * and clear the DisGest bit (see §2.5 of Synaptics 1100 * TouchPad Interfacing Guide). 1101 */ 1102 VLOG(2, (LOG_DEBUG, 1103 "psm%d: enable tap and drag gestures\n", 1104 sc->unit)); 1105 mouse_ext_command(sc->kbdc, 0x00); 1106 set_mouse_sampling_rate(sc->kbdc, 20); 1107 } else if (tap_enabled == 0) { 1108 /* 1109 * Disable tap & drag gestures. We use a Mode Byte 1110 * and set the DisGest bit (see §2.5 of Synaptics 1111 * TouchPad Interfacing Guide). 1112 */ 1113 VLOG(2, (LOG_DEBUG, 1114 "psm%d: disable tap and drag gestures\n", 1115 sc->unit)); 1116 mouse_ext_command(sc->kbdc, 0x04); 1117 set_mouse_sampling_rate(sc->kbdc, 20); 1118 } 1119 } 1120 1121 /* enable the mouse device */ 1122 if (!enable_aux_dev(sc->kbdc)) { 1123 /* MOUSE ERROR: failed to enable the mouse because: 1124 * 1) the mouse is faulty, 1125 * 2) the mouse has been removed(!?) 1126 * In the latter case, the keyboard may have hung, and need 1127 * recovery procedure... 1128 */ 1129 recover_from_error(sc->kbdc); 1130 #if 0 1131 /* FIXME: we could reset the mouse here and try to enable 1132 * it again. But it will take long time and it's not a good 1133 * idea to disable the keyboard that long... 1134 */ 1135 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) { 1136 recover_from_error(sc->kbdc); 1137 #else 1138 { 1139 #endif 1140 restore_controller(sc->kbdc, command_byte); 1141 /* mark this device is no longer available */ 1142 sc->state &= ~PSM_VALID; 1143 log(LOG_ERR, 1144 "psm%d: failed to enable the device (doopen).\n", 1145 sc->unit); 1146 return (EIO); 1147 } 1148 } 1149 1150 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 1151 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", 1152 sc->unit); 1153 1154 /* enable the aux port and interrupt */ 1155 if (!set_controller_command_byte(sc->kbdc, 1156 KBD_AUX_CONTROL_BITS, 1157 KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) { 1158 /* CONTROLLER ERROR */ 1159 disable_aux_dev(sc->kbdc); 1160 restore_controller(sc->kbdc, command_byte); 1161 log(LOG_ERR, 1162 "psm%d: failed to enable the aux interrupt (doopen).\n", 1163 sc->unit); 1164 return (EIO); 1165 } 1166 1167 /* start the watchdog timer */ 1168 sc->watchdog = FALSE; 1169 callout_reset(&sc->callout, hz * 2, psmtimeout, (void *)(uintptr_t)sc); 1170 1171 return (0); 1172 } 1173 1174 static int 1175 reinitialize(struct psm_softc *sc, int doinit) 1176 { 1177 1178 int err; 1179 int c; 1180 1181 /* don't let anybody mess with the aux device */ 1182 if (!kbdc_lock(sc->kbdc, TRUE)) 1183 return (EIO); 1184 1185 crit_enter(); 1186 1187 /* block our watchdog timer */ 1188 sc->watchdog = FALSE; 1189 callout_stop(&sc->callout); 1190 1191 /* save the current controller command byte */ 1192 empty_both_buffers(sc->kbdc, 10); 1193 c = get_controller_command_byte(sc->kbdc); 1194 VLOG(2, (LOG_DEBUG, 1195 "psm%d: current command byte: %04x (reinitialize).\n", 1196 sc->unit, c)); 1197 1198 /* enable the aux port but disable the aux interrupt and the keyboard */ 1199 if ((c == -1) || 1200 !set_controller_command_byte(sc->kbdc, 1201 KBD_AUX_CONTROL_BITS, 1202 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1203 /* CONTROLLER ERROR */ 1204 crit_exit(); 1205 kbdc_lock(sc->kbdc, FALSE); 1206 log(LOG_ERR, 1207 "psm%d: unable to set the command byte (reinitialize).\n", 1208 sc->unit); 1209 return (EIO); 1210 } 1211 1212 /* flush any data */ 1213 if (sc->state & PSM_VALID) { 1214 /* this may fail; but never mind... */ 1215 disable_aux_dev(sc->kbdc); 1216 empty_aux_buffer(sc->kbdc, 10); 1217 } 1218 flushpackets(sc); 1219 sc->syncerrors = 0; 1220 sc->pkterrors = 0; 1221 memset(&sc->lastinputerr, 0, sizeof(sc->lastinputerr)); 1222 1223 /* try to detect the aux device; are you still there? */ 1224 err = 0; 1225 if (doinit) { 1226 if (doinitialize(sc, &sc->mode)) { 1227 /* yes */ 1228 sc->state |= PSM_VALID; 1229 } else { 1230 /* the device has gone! */ 1231 restore_controller(sc->kbdc, c); 1232 sc->state &= ~PSM_VALID; 1233 log(LOG_ERR, 1234 "psm%d: the aux device has gone! (reinitialize).\n", 1235 sc->unit); 1236 err = ENXIO; 1237 } 1238 } 1239 crit_exit(); 1240 1241 /* restore the driver state */ 1242 if ((sc->state & PSM_OPEN) && (err == 0)) { 1243 /* enable the aux device and the port again */ 1244 err = doopen(sc, c); 1245 if (err != 0) 1246 log(LOG_ERR, "psm%d: failed to enable the device " 1247 "(reinitialize).\n", sc->unit); 1248 } else { 1249 /* restore the keyboard port and disable the aux port */ 1250 if (!set_controller_command_byte(sc->kbdc, 1251 KBD_AUX_CONTROL_BITS, 1252 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1253 /* CONTROLLER ERROR */ 1254 log(LOG_ERR, "psm%d: failed to disable the aux port " 1255 "(reinitialize).\n", sc->unit); 1256 err = EIO; 1257 } 1258 } 1259 1260 kbdc_lock(sc->kbdc, FALSE); 1261 return (err); 1262 } 1263 1264 /* psm driver entry points */ 1265 1266 static void 1267 psmidentify(driver_t *driver, device_t parent) 1268 { 1269 1270 device_t psmc; 1271 device_t psm; 1272 u_long irq; 1273 int unit; 1274 1275 unit = device_get_unit(parent); 1276 1277 /* always add at least one child */ 1278 psm = BUS_ADD_CHILD(parent, parent, KBDC_RID_AUX, driver->name, unit); 1279 if (psm == NULL) { 1280 1281 return; 1282 } 1283 1284 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX); 1285 if (irq > 0) { 1286 1287 return; 1288 } 1289 1290 /* 1291 * If the PS/2 mouse device has already been reported by ACPI or 1292 * PnP BIOS, obtain the IRQ resource from it. 1293 * (See psmcpnp_attach() below.) 1294 */ 1295 psmc = device_find_child(device_get_parent(parent), 1296 PSMCPNP_DRIVER_NAME, unit); 1297 if (psmc == NULL) 1298 return; 1299 1300 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0); 1301 if (irq <= 0) { 1302 1303 return; 1304 } 1305 1306 bus_delete_resource(psmc, SYS_RES_IRQ, 0); 1307 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1, 1308 machintr_legacy_intr_cpuid(irq)); 1309 } 1310 1311 #define endprobe(v) do { \ 1312 if (bootverbose) \ 1313 --verbose; \ 1314 set_controller_command_byte(sc->kbdc, \ 1315 KBD_AUX_CONTROL_BITS, KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT); \ 1316 kbdc_lock(sc->kbdc, FALSE); \ 1317 return (v); \ 1318 } while (0) 1319 1320 static int 1321 psmprobe(device_t dev) 1322 { 1323 1324 int unit = device_get_unit(dev); 1325 struct psm_softc *sc = device_get_softc(dev); 1326 int stat[3]; 1327 int command_byte; 1328 /* int rid; */ 1329 int i; 1330 uintptr_t irq; 1331 uintptr_t flags; 1332 1333 #if 0 1334 kbdc_debug(TRUE); 1335 #endif 1336 1337 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq); 1338 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_FLAGS, &flags); 1339 1340 sc->unit = unit; 1341 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev))); 1342 sc->config = flags & PSM_CONFIG_FLAGS; 1343 /* XXX: for backward compatibility */ 1344 #if defined(PSM_HOOKRESUME) 1345 sc->config |= 1346 #ifdef PSM_RESETAFTERSUSPEND 1347 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND; 1348 #else 1349 PSM_CONFIG_HOOKRESUME; 1350 #endif 1351 #endif /* PSM_HOOKRESUME */ 1352 sc->flags = 0; 1353 if (bootverbose) 1354 ++verbose; 1355 1356 device_set_desc(dev, "PS/2 Mouse"); 1357 1358 if (!kbdc_lock(sc->kbdc, TRUE)) { 1359 kprintf("psm%d: unable to lock the controller.\n", unit); 1360 if (bootverbose) 1361 --verbose; 1362 return (ENXIO); 1363 } 1364 1365 1366 /* 1367 * NOTE: two bits in the command byte controls the operation of the 1368 * aux port (mouse port): the aux port disable bit (bit 5) and the aux 1369 * port interrupt (IRQ 12) enable bit (bit 2). 1370 */ 1371 1372 /* discard anything left after the keyboard initialization */ 1373 empty_both_buffers(sc->kbdc, 10); 1374 1375 /* save the current command byte; it will be used later */ 1376 command_byte = get_controller_command_byte(sc->kbdc); 1377 if (verbose) 1378 kprintf("psm%d: current command byte:%04x\n", unit, 1379 command_byte); 1380 if (command_byte == -1) { 1381 /* CONTROLLER ERROR */ 1382 kprintf("psm%d: unable to get the current " 1383 "command byte value.\n", 1384 unit); 1385 endprobe(ENXIO); 1386 } 1387 1388 /* XXX: this note is of the previous code, kept intact in case any 1389 * breakage occurs. Since both comments contradict each other. 1390 * Previous condition used was: 1391 * 1392 * if (!set_controller_command_byte(sc->kbdc, 1393 * KBD_AUX_CONTROL_BITS, 1394 * KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) 1395 * 1396 * .. <slightly different code> 1397 * NOTE: We cannot mess with the keyboard port, do NOT disable it 1398 * while we are probing the aux port during this routine. 1399 * Disabling the keyboard port will break some things 1400 * (Acer c720)... probably related to BIOS emulation of the 1401 * i8042. 1402 */ 1403 /* 1404 * disable the keyboard port while probing the aux port, which must be 1405 * enabled during this routine 1406 */ 1407 if (!set_controller_command_byte(sc->kbdc, 1408 KBD_AUX_CONTROL_BITS, 1409 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1410 /* 1411 * this is CONTROLLER ERROR; I don't know how to recover 1412 * from this error... 1413 */ 1414 kprintf("psm%d: unable to set the command byte.\n", unit); 1415 endprobe(ENXIO); 1416 } 1417 1418 /* 1419 * NOTE: Linux doesn't send discrete aux port enablement commands, 1420 * it is unclear whether this is needed or helps or hinders 1421 * bios emulators. 1422 */ 1423 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT); 1424 1425 /* 1426 * NOTE: `test_aux_port()' is designed to return with zero if the aux 1427 * port exists and is functioning. However, some controllers appears 1428 * to respond with zero even when the aux port doesn't exist. (It may 1429 * be that this is only the case when the controller DOES have the aux 1430 * port but the port is not wired on the motherboard.) The keyboard 1431 * controllers without the port, such as the original AT, are 1432 * supporsed to return with an error code or simply time out. In any 1433 * case, we have to continue probing the port even when the controller 1434 * passes this test. 1435 * 1436 * XXX: some controllers erroneously return the error code 1, 2 or 3 1437 * when it has the perfectly functional aux port. We have to ignore 1438 * this error code. Even if the controller HAS error with the aux 1439 * port, it will be detected later... 1440 * XXX: another incompatible controller returns PSM_ACK (0xfa)... 1441 */ 1442 1443 switch ((i = test_aux_port(sc->kbdc))) { 1444 case 1: /* ignore these errors */ 1445 case 2: 1446 case 3: 1447 case PSM_ACK: 1448 if (verbose) 1449 kprintf("psm%d: strange result for test aux port " 1450 "(%d).\n", unit, i); 1451 /* FALLTHROUGH */ 1452 case 0: /* no error */ 1453 break; 1454 case -1: /* time out */ 1455 default: /* error */ 1456 recover_from_error(sc->kbdc); 1457 if (sc->config & PSM_CONFIG_IGNPORTERROR) 1458 break; 1459 1460 if (verbose) 1461 kprintf("psm%d: the aux port is not " 1462 "functioning (%d).\n", 1463 unit, i); 1464 endprobe(ENXIO); 1465 } 1466 1467 if (sc->config & PSM_CONFIG_NORESET) { 1468 /* 1469 * Don't try to reset the pointing device. It may possibly be 1470 * left in the unknown state, though... 1471 */ 1472 } else { 1473 /* 1474 * NOTE: some controllers appears to hang the `keyboard' when 1475 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued. 1476 * 1477 * Attempt to reset the controller twice -- this helps 1478 * pierce through some KVM switches. The second reset 1479 * is non-fatal. 1480 */ 1481 if (!reset_aux_dev(sc->kbdc)) { 1482 recover_from_error(sc->kbdc); 1483 if (verbose) 1484 kprintf("psm%d: failed to reset the aux " 1485 "device.\n", unit); 1486 endprobe(ENXIO); 1487 } else if (!reset_aux_dev(sc->kbdc)) { 1488 recover_from_error(sc->kbdc); 1489 if (verbose >= 2) 1490 kprintf("psm%d: failed to reset the aux device " 1491 "(2).\n", unit); 1492 } 1493 } 1494 1495 /* 1496 * both the aux port and the aux device are functioning, see if the 1497 * device can be enabled. NOTE: when enabled, the device will start 1498 * sending data; we shall immediately disable the device once we know 1499 * the device can be enabled. 1500 */ 1501 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) { 1502 /* MOUSE ERROR */ 1503 recover_from_error(sc->kbdc); 1504 if (verbose) 1505 kprintf("psm%d: failed to enable the aux device.\n", 1506 unit); 1507 endprobe(ENXIO); 1508 } 1509 1510 /* save the default values after reset */ 1511 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) { 1512 sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1513 sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1514 } else { 1515 sc->dflt_mode.rate = sc->mode.rate = -1; 1516 sc->dflt_mode.resolution = sc->mode.resolution = -1; 1517 } 1518 1519 /* hardware information */ 1520 sc->hw.iftype = MOUSE_IF_PS2; 1521 1522 /* verify the device is a mouse */ 1523 sc->hw.hwid = get_aux_id(sc->kbdc); 1524 if (!is_a_mouse(sc->hw.hwid)) { 1525 if (verbose) 1526 kprintf("psm%d: unknown device type (%d).\n", unit, 1527 sc->hw.hwid); 1528 endprobe(ENXIO); 1529 } 1530 switch (sc->hw.hwid) { 1531 case PSM_BALLPOINT_ID: 1532 sc->hw.type = MOUSE_TRACKBALL; 1533 break; 1534 case PSM_MOUSE_ID: 1535 case PSM_INTELLI_ID: 1536 case PSM_EXPLORER_ID: 1537 case PSM_4DMOUSE_ID: 1538 case PSM_4DPLUS_ID: 1539 sc->hw.type = MOUSE_MOUSE; 1540 break; 1541 default: 1542 sc->hw.type = MOUSE_UNKNOWN; 1543 break; 1544 } 1545 1546 if (sc->config & PSM_CONFIG_NOIDPROBE) { 1547 sc->hw.buttons = 2; 1548 i = GENERIC_MOUSE_ENTRY; 1549 } else { 1550 /* # of buttons */ 1551 sc->hw.buttons = get_mouse_buttons(sc->kbdc); 1552 1553 /* other parameters */ 1554 for (i = 0; vendortype[i].probefunc != NULL; ++i) 1555 if ((*vendortype[i].probefunc)(sc, PROBE)) { 1556 if (verbose >= 2) 1557 kprintf("psm%d: found %s\n", unit, 1558 model_name(vendortype[i].model)); 1559 break; 1560 } 1561 } 1562 1563 sc->hw.model = vendortype[i].model; 1564 1565 sc->dflt_mode.level = PSM_LEVEL_BASE; 1566 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE; 1567 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4; 1568 if (sc->config & PSM_CONFIG_NOCHECKSYNC) 1569 sc->dflt_mode.syncmask[0] = 0; 1570 else 1571 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask; 1572 if (sc->config & PSM_CONFIG_FORCETAP) 1573 sc->dflt_mode.syncmask[0] &= ~MOUSE_PS2_TAP; 1574 sc->dflt_mode.syncmask[1] = 0; /* syncbits */ 1575 sc->mode = sc->dflt_mode; 1576 sc->mode.packetsize = vendortype[i].packetsize; 1577 1578 /* set mouse parameters */ 1579 #if 0 1580 /* 1581 * A version of Logitech FirstMouse+ won't report wheel movement, 1582 * if SET_DEFAULTS is sent... Don't use this command. 1583 * This f}}}ix was found by Takashi Nishida. 1584 */ 1585 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS); 1586 if (verbose >= 2) 1587 kprintf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i); 1588 #endif 1589 if (sc->config & PSM_CONFIG_RESOLUTION) 1590 sc->mode.resolution = 1591 set_mouse_resolution(sc->kbdc, 1592 (sc->config & PSM_CONFIG_RESOLUTION) - 1); 1593 else if (sc->mode.resolution >= 0) 1594 sc->mode.resolution = 1595 set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution); 1596 if (sc->mode.rate > 0) 1597 sc->mode.rate = 1598 set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate); 1599 set_mouse_scaling(sc->kbdc, 1); 1600 1601 /* Record sync on the next data packet we see. */ 1602 sc->flags |= PSM_NEED_SYNCBITS; 1603 1604 /* just check the status of the mouse */ 1605 /* 1606 * NOTE: XXX there are some arcane controller/mouse combinations out 1607 * there, which hung the controller unless there is data transmission 1608 * after ACK from the mouse. 1609 */ 1610 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 1611 kprintf("psm%d: failed to get status.\n", unit); 1612 else { 1613 /* 1614 * When in its native mode, some mice operate with different 1615 * default parameters than in the PS/2 compatible mode. 1616 */ 1617 sc->dflt_mode.rate = sc->mode.rate = stat[2]; 1618 sc->dflt_mode.resolution = sc->mode.resolution = stat[1]; 1619 } 1620 1621 /* disable the aux port for now... */ 1622 if (!set_controller_command_byte(sc->kbdc, 1623 KBD_AUX_CONTROL_BITS, 1624 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1625 /* 1626 * this is CONTROLLER ERROR; I don't know the proper way to 1627 * recover from this error... 1628 */ 1629 kprintf("psm%d: unable to set the command byte.\n", unit); 1630 endprobe(ENXIO); 1631 } 1632 1633 /* done */ 1634 kbdc_lock(sc->kbdc, FALSE); 1635 return (0); 1636 } 1637 1638 static int 1639 psmattach(device_t dev) 1640 { 1641 1642 int unit = device_get_unit(dev); 1643 struct psm_softc *sc = device_get_softc(dev); 1644 int error; 1645 int rid; 1646 uintptr_t irq; 1647 1648 /* Setup initial state */ 1649 sc->state = PSM_VALID; 1650 callout_init(&sc->callout); 1651 callout_init(&sc->softcallout); 1652 1653 /* Setup our interrupt handler */ 1654 rid = KBDC_RID_AUX; 1655 BUS_READ_IVAR(device_get_parent(dev), dev, KBDC_IVAR_IRQ, &irq); 1656 sc->intr = bus_alloc_legacy_irq_resource(dev, &rid, irq, RF_ACTIVE); 1657 if (sc->intr == NULL) { 1658 return (ENXIO); 1659 } 1660 1661 error = BUS_SETUP_INTR(device_get_parent(dev), dev, sc->intr, 1662 INTR_NOPOLL, psmintr, sc, &sc->ih, NULL, NULL); 1663 if (error) { 1664 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1665 return (error); 1666 } 1667 1668 /* Done */ 1669 sc->dev = make_dev(&psm_ops, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "psm%d", unit); 1670 sc->dev->si_drv1 = sc; 1671 sc->bdev = make_dev(&psm_ops, PSM_MKMINOR(unit, FALSE), 0, 0, 0666, "bpsm%d", unit); 1672 sc->bdev->si_drv1 = sc; 1673 1674 /* Some touchpad devices need full reinitialization after suspend. */ 1675 switch (sc->hw.model) { 1676 case MOUSE_MODEL_SYNAPTICS: 1677 case MOUSE_MODEL_GLIDEPOINT: 1678 case MOUSE_MODEL_VERSAPAD: 1679 case MOUSE_MODEL_ELANTECH: 1680 sc->config |= PSM_CONFIG_INITAFTERSUSPEND; 1681 break; 1682 default: 1683 if (sc->synhw.infoMajor >= 4 || sc->tphw > 0) 1684 sc->config |= PSM_CONFIG_INITAFTERSUSPEND; 1685 break; 1686 } 1687 1688 /* Elantech trackpad`s sync bit differs from touchpad`s one */ 1689 if (sc->hw.model == MOUSE_MODEL_ELANTECH && 1690 (sc->elanhw.hascrc || sc->elanhw.hastrackpoint)) 1691 sc->config |= PSM_CONFIG_NOCHECKSYNC; 1692 1693 1694 if (!verbose) 1695 kprintf("psm%d: model %s, device ID %d\n", 1696 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff); 1697 else { 1698 kprintf("psm%d: model %s, device ID %d-%02x, %d buttons\n", 1699 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff, 1700 sc->hw.hwid >> 8, sc->hw.buttons); 1701 kprintf("psm%d: config:%08x, flags:%08x, packet size:%d\n", 1702 unit, sc->config, sc->flags, sc->mode.packetsize); 1703 kprintf("psm%d: syncmask:%02x, syncbits:%02x\n", 1704 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]); 1705 } 1706 1707 if (bootverbose) 1708 --verbose; 1709 1710 return (0); 1711 } 1712 1713 static int 1714 psmdetach(device_t dev) 1715 { 1716 1717 struct psm_softc *sc; 1718 int rid; 1719 1720 sc = device_get_softc(dev); 1721 if (sc->state & PSM_OPEN) 1722 return (EBUSY); 1723 1724 rid = KBDC_RID_AUX; 1725 bus_teardown_intr(dev, sc->intr, sc->ih); 1726 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr); 1727 1728 destroy_dev(sc->dev); 1729 destroy_dev(sc->bdev); 1730 1731 /* XXX: callout_drain in original freebsd11 code */ 1732 callout_stop_sync(&sc->callout); 1733 callout_stop_sync(&sc->softcallout); 1734 1735 return (0); 1736 } 1737 1738 static int 1739 psmopen(struct dev_open_args *ap) 1740 { 1741 1742 cdev_t dev = ap->a_head.a_dev; 1743 struct psm_softc *sc; 1744 int command_byte; 1745 int err; 1746 1747 /* Get device data */ 1748 sc = dev->si_drv1; 1749 if ((sc == NULL) || (sc->state & PSM_VALID) == 0) { 1750 /* the device is no longer valid/functioning */ 1751 return (ENXIO); 1752 } 1753 1754 /* Disallow multiple opens */ 1755 if (sc->state & PSM_OPEN) 1756 return (EBUSY); 1757 1758 device_busy(devclass_get_device(psm_devclass, sc->unit)); 1759 1760 /* Initialize state */ 1761 sc->mode.level = sc->dflt_mode.level; 1762 sc->mode.protocol = sc->dflt_mode.protocol; 1763 sc->watchdog = FALSE; 1764 1765 /* flush the event queue */ 1766 sc->queue.count = 0; 1767 sc->queue.head = 0; 1768 sc->queue.tail = 0; 1769 sc->status.flags = 0; 1770 sc->status.button = 0; 1771 sc->status.obutton = 0; 1772 sc->status.dx = 0; 1773 sc->status.dy = 0; 1774 sc->status.dz = 0; 1775 sc->button = 0; 1776 sc->pqueue_start = 0; 1777 sc->pqueue_end = 0; 1778 1779 /* empty input buffer */ 1780 flushpackets(sc); 1781 sc->syncerrors = 0; 1782 sc->pkterrors = 0; 1783 1784 /* don't let timeout routines in the keyboard driver to poll the kbdc */ 1785 if (!kbdc_lock(sc->kbdc, TRUE)) 1786 return (EIO); 1787 1788 /* save the current controller command byte */ 1789 crit_enter(); 1790 command_byte = get_controller_command_byte(sc->kbdc); 1791 1792 /* enable the aux port and temporalily disable the keyboard */ 1793 if (command_byte == -1 || 1794 !set_controller_command_byte(sc->kbdc, 1795 KBD_AUX_CONTROL_BITS, 1796 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1797 /* CONTROLLER ERROR; do you know how to get out of this? */ 1798 kbdc_lock(sc->kbdc, FALSE); 1799 crit_exit(); 1800 log(LOG_ERR, 1801 "psm%d: unable to set the command byte (psmopen).\n", 1802 sc->unit); 1803 return (EIO); 1804 } 1805 /* 1806 * Now that the keyboard controller is told not to generate 1807 * the keyboard and mouse interrupts, allow 1808 * the other tty interrupts. The clock interrupt may also occur, 1809 * but timeout routines will be blocked by the poll flag set 1810 * via `kbdc_lock()' 1811 */ 1812 crit_exit(); 1813 1814 /* enable the mouse device */ 1815 err = doopen(sc, command_byte); 1816 1817 /* done */ 1818 if (err == 0) 1819 sc->state |= PSM_OPEN; 1820 kbdc_lock(sc->kbdc, FALSE); 1821 return (err); 1822 } 1823 1824 static int 1825 psmclose(struct dev_close_args *ap) 1826 { 1827 1828 cdev_t dev = ap->a_head.a_dev; 1829 struct psm_softc *sc = dev->si_drv1; 1830 int stat[3]; 1831 int command_byte; 1832 1833 /* don't let timeout routines in the keyboard driver to poll the kbdc */ 1834 if (!kbdc_lock(sc->kbdc, TRUE)) 1835 return (EIO); 1836 1837 /* save the current controller command byte */ 1838 crit_enter(); 1839 command_byte = get_controller_command_byte(sc->kbdc); 1840 if (command_byte == -1) { 1841 kbdc_lock(sc->kbdc, FALSE); 1842 crit_exit(); 1843 return (EIO); 1844 } 1845 1846 /* disable the aux interrupt and temporalily disable the keyboard */ 1847 if (!set_controller_command_byte(sc->kbdc, 1848 KBD_AUX_CONTROL_BITS, 1849 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1850 log(LOG_ERR, 1851 "psm%d: failed to disable the aux int (psmclose).\n", 1852 sc->unit); 1853 /* CONTROLLER ERROR; 1854 * NOTE: we shall force our way through. Because the only 1855 * ill effect we shall see is that we may not be able 1856 * to read ACK from the mouse, and it doesn't matter much 1857 * so long as the mouse will accept the DISABLE command. 1858 */ 1859 } 1860 crit_exit(); 1861 1862 /* stop the watchdog timer */ 1863 callout_stop(&sc->callout); 1864 1865 /* remove anything left in the output buffer */ 1866 empty_aux_buffer(sc->kbdc, 10); 1867 1868 /* disable the aux device, port and interrupt */ 1869 if (sc->state & PSM_VALID) { 1870 if (!disable_aux_dev(sc->kbdc)) { 1871 /* MOUSE ERROR; 1872 * NOTE: we don't return (error) and continue, 1873 * pretending we have successfully disabled the device. 1874 * It's OK because the interrupt routine will discard 1875 * any data from the mouse hereafter. 1876 */ 1877 log(LOG_ERR, 1878 "psm%d: failed to disable the device (psmclose).\n", 1879 sc->unit); 1880 } 1881 1882 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) 1883 log(LOG_DEBUG, 1884 "psm%d: failed to get status (psmclose).\n", 1885 sc->unit); 1886 } 1887 1888 if (!set_controller_command_byte(sc->kbdc, 1889 KBD_AUX_CONTROL_BITS, 1890 KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 1891 /* 1892 * CONTROLLER ERROR; 1893 * we shall ignore this error; see the above comment. 1894 */ 1895 log(LOG_ERR, 1896 "psm%d: failed to disable the aux port (psmclose).\n", 1897 sc->unit); 1898 } 1899 1900 /* remove anything left in the output buffer */ 1901 empty_aux_buffer(sc->kbdc, 10); 1902 1903 /* close is almost always successful */ 1904 sc->state &= ~PSM_OPEN; 1905 kbdc_lock(sc->kbdc, FALSE); 1906 device_unbusy(devclass_get_device(psm_devclass, sc->unit)); 1907 return (0); 1908 } 1909 1910 static int 1911 tame_mouse(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *status, 1912 u_char *buf) 1913 { 1914 1915 static u_char butmapps2[8] = { 1916 0, 1917 MOUSE_PS2_BUTTON1DOWN, 1918 MOUSE_PS2_BUTTON2DOWN, 1919 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN, 1920 MOUSE_PS2_BUTTON3DOWN, 1921 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN, 1922 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN, 1923 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | 1924 MOUSE_PS2_BUTTON3DOWN, 1925 }; 1926 static u_char butmapmsc[8] = { 1927 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | 1928 MOUSE_MSC_BUTTON3UP, 1929 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP, 1930 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP, 1931 MOUSE_MSC_BUTTON3UP, 1932 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP, 1933 MOUSE_MSC_BUTTON2UP, 1934 MOUSE_MSC_BUTTON1UP, 1935 0, 1936 }; 1937 int mapped; 1938 int i; 1939 1940 if (sc->mode.level == PSM_LEVEL_BASE) { 1941 mapped = status->button & ~MOUSE_BUTTON4DOWN; 1942 if (status->button & MOUSE_BUTTON4DOWN) 1943 mapped |= MOUSE_BUTTON1DOWN; 1944 status->button = mapped; 1945 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS]; 1946 i = imax(imin(status->dx, 255), -256); 1947 if (i < 0) 1948 buf[0] |= MOUSE_PS2_XNEG; 1949 buf[1] = i; 1950 i = imax(imin(status->dy, 255), -256); 1951 if (i < 0) 1952 buf[0] |= MOUSE_PS2_YNEG; 1953 buf[2] = i; 1954 return (MOUSE_PS2_PACKETSIZE); 1955 } else if (sc->mode.level == PSM_LEVEL_STANDARD) { 1956 buf[0] = MOUSE_MSC_SYNC | 1957 butmapmsc[status->button & MOUSE_STDBUTTONS]; 1958 i = imax(imin(status->dx, 255), -256); 1959 buf[1] = i >> 1; 1960 buf[3] = i - buf[1]; 1961 i = imax(imin(status->dy, 255), -256); 1962 buf[2] = i >> 1; 1963 buf[4] = i - buf[2]; 1964 i = imax(imin(status->dz, 127), -128); 1965 buf[5] = (i >> 1) & 0x7f; 1966 buf[6] = (i - (i >> 1)) & 0x7f; 1967 buf[7] = (~status->button >> 3) & 0x7f; 1968 return (MOUSE_SYS_PACKETSIZE); 1969 } 1970 return (pb->inputbytes); 1971 } 1972 1973 static int 1974 psmread(struct dev_read_args *ap) 1975 { 1976 1977 cdev_t dev = ap->a_head.a_dev; 1978 struct uio *uio = ap->a_uio; 1979 struct psm_softc *sc = dev->si_drv1; 1980 u_char buf[PSM_SMALLBUFSIZE]; 1981 int error = 0; 1982 int l; 1983 1984 if ((sc->state & PSM_VALID) == 0) 1985 return (EIO); 1986 1987 /* block until mouse activity occurred */ 1988 crit_enter(); 1989 while (sc->queue.count <= 0) { 1990 if (dev != sc->bdev) { 1991 crit_exit(); 1992 return (EWOULDBLOCK); 1993 } 1994 sc->state |= PSM_ASLP; 1995 error = tsleep(sc, PCATCH, "psmrea", 0); 1996 sc->state &= ~PSM_ASLP; 1997 if (error) { 1998 crit_exit(); 1999 return (error); 2000 } else if ((sc->state & PSM_VALID) == 0) { 2001 /* the device disappeared! */ 2002 crit_exit(); 2003 return (EIO); 2004 } 2005 } 2006 crit_exit(); 2007 2008 /* copy data to the user land */ 2009 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) { 2010 crit_enter(); 2011 l = imin(sc->queue.count, uio->uio_resid); 2012 if (l > sizeof(buf)) 2013 l = sizeof(buf); 2014 if (l > sizeof(sc->queue.buf) - sc->queue.head) { 2015 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], 2016 sizeof(sc->queue.buf) - sc->queue.head); 2017 bcopy(&sc->queue.buf[0], 2018 &buf[sizeof(sc->queue.buf) - sc->queue.head], 2019 l - (sizeof(sc->queue.buf) - sc->queue.head)); 2020 } else 2021 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l); 2022 sc->queue.count -= l; 2023 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf); 2024 crit_exit(); 2025 error = uiomove(buf, l, uio); 2026 if (error) 2027 break; 2028 } 2029 2030 return (error); 2031 } 2032 2033 static int 2034 block_mouse_data(struct psm_softc *sc, int *c) 2035 { 2036 2037 if (!kbdc_lock(sc->kbdc, TRUE)) 2038 return (EIO); 2039 2040 crit_enter(); 2041 *c = get_controller_command_byte(sc->kbdc); 2042 if ((*c == -1) || 2043 !set_controller_command_byte(sc->kbdc, 2044 KBD_AUX_CONTROL_BITS, 2045 KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) { 2046 /* this is CONTROLLER ERROR */ 2047 crit_exit(); 2048 kbdc_lock(sc->kbdc, FALSE); 2049 return (EIO); 2050 } 2051 2052 /* 2053 * The device may be in the middle of status data transmission. 2054 * The transmission will be interrupted, thus, incomplete status 2055 * data must be discarded. Although the aux interrupt is disabled 2056 * at the keyboard controller level, at most one aux interrupt 2057 * may have already been pending and a data byte is in the 2058 * output buffer; throw it away. Note that the second argument 2059 * to `empty_aux_buffer()' is zero, so that the call will just 2060 * flush the internal queue. 2061 * `psmintr()' will be invoked after `crit_exit()' if an interrupt is 2062 * pending; it will see no data and returns immediately. 2063 */ 2064 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */ 2065 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */ 2066 flushpackets(sc); 2067 crit_exit(); 2068 2069 return (0); 2070 } 2071 2072 static void 2073 dropqueue(struct psm_softc *sc) 2074 { 2075 2076 sc->queue.count = 0; 2077 sc->queue.head = 0; 2078 sc->queue.tail = 0; 2079 if ((sc->state & PSM_SOFTARMED) != 0) { 2080 sc->state &= ~PSM_SOFTARMED; 2081 callout_stop(&sc->softcallout); 2082 } 2083 sc->pqueue_start = sc->pqueue_end; 2084 } 2085 2086 static void 2087 flushpackets(struct psm_softc *sc) 2088 { 2089 2090 dropqueue(sc); 2091 bzero(&sc->pqueue, sizeof(sc->pqueue)); 2092 } 2093 2094 static int 2095 unblock_mouse_data(struct psm_softc *sc, int c) 2096 { 2097 2098 int error = 0; 2099 2100 /* 2101 * We may have seen a part of status data during `set_mouse_XXX()'. 2102 * they have been queued; flush it. 2103 */ 2104 empty_aux_buffer(sc->kbdc, 0); 2105 2106 /* restore ports and interrupt */ 2107 if (!set_controller_command_byte(sc->kbdc, 2108 KBD_AUX_CONTROL_BITS, c & KBD_AUX_CONTROL_BITS)) { 2109 /* 2110 * CONTROLLER ERROR; this is serious, we may have 2111 * been left with the inaccessible keyboard and 2112 * the disabled mouse interrupt. 2113 */ 2114 error = EIO; 2115 } 2116 2117 kbdc_lock(sc->kbdc, FALSE); 2118 return (error); 2119 } 2120 2121 static int 2122 psmwrite(struct dev_write_args *ap) 2123 { 2124 2125 cdev_t dev = ap->a_head.a_dev; 2126 struct uio *uio = ap->a_uio; 2127 struct psm_softc *sc = dev->si_drv1; 2128 u_char buf[PSM_SMALLBUFSIZE]; 2129 int error = 0; 2130 int i, l; 2131 2132 if ((sc->state & PSM_VALID) == 0) 2133 return (EIO); 2134 2135 if (sc->mode.level < PSM_LEVEL_NATIVE) 2136 return (ENODEV); 2137 2138 /* copy data from the user land */ 2139 while (uio->uio_resid > 0) { 2140 l = imin(PSM_SMALLBUFSIZE, uio->uio_resid); 2141 error = uiomove(buf, l, uio); 2142 if (error) 2143 break; 2144 for (i = 0; i < l; i++) { 2145 VLOG(4, (LOG_DEBUG, "psm: cmd 0x%x\n", buf[i])); 2146 if (!write_aux_command(sc->kbdc, buf[i])) { 2147 VLOG(2, (LOG_DEBUG, 2148 "psm: cmd 0x%x failed.\n", buf[i])); 2149 return (reinitialize(sc, FALSE)); 2150 } 2151 } 2152 } 2153 2154 return (error); 2155 } 2156 2157 2158 2159 static int 2160 psmioctl(struct dev_ioctl_args *ap) 2161 { 2162 2163 cdev_t dev = ap->a_head.a_dev; 2164 caddr_t addr= ap->a_data; 2165 struct psm_softc *sc = dev->si_drv1; 2166 mousemode_t mode; 2167 mousestatus_t status; 2168 #if (defined(MOUSE_GETVARS)) 2169 mousevar_t *var; 2170 #endif 2171 mousedata_t *data; 2172 int stat[3]; 2173 int command_byte; 2174 int error = 0; 2175 2176 /* Perform IOCTL command */ 2177 switch (ap->a_cmd) { 2178 2179 case OLD_MOUSE_GETHWINFO: 2180 crit_enter(); 2181 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons; 2182 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype; 2183 ((old_mousehw_t *)addr)->type = sc->hw.type; 2184 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff; 2185 crit_exit(); 2186 break; 2187 2188 case MOUSE_GETHWINFO: 2189 crit_enter(); 2190 *(mousehw_t *)addr = sc->hw; 2191 if (sc->mode.level == PSM_LEVEL_BASE) 2192 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC; 2193 crit_exit(); 2194 break; 2195 2196 case MOUSE_SYN_GETHWINFO: 2197 crit_enter(); 2198 if (sc->synhw.infoMajor >= 4) 2199 *(synapticshw_t *)addr = sc->synhw; 2200 else 2201 error = EINVAL; 2202 crit_exit(); 2203 break; 2204 2205 case OLD_MOUSE_GETMODE: 2206 crit_enter(); 2207 switch (sc->mode.level) { 2208 case PSM_LEVEL_BASE: 2209 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2210 break; 2211 case PSM_LEVEL_STANDARD: 2212 ((old_mousemode_t *)addr)->protocol = 2213 MOUSE_PROTO_SYSMOUSE; 2214 break; 2215 case PSM_LEVEL_NATIVE: 2216 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2217 break; 2218 } 2219 ((old_mousemode_t *)addr)->rate = sc->mode.rate; 2220 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution; 2221 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor; 2222 crit_exit(); 2223 break; 2224 2225 case MOUSE_GETMODE: 2226 crit_enter(); 2227 *(mousemode_t *)addr = sc->mode; 2228 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) { 2229 ((mousemode_t *)addr)->syncmask[0] = 0; 2230 ((mousemode_t *)addr)->syncmask[1] = 0; 2231 } 2232 ((mousemode_t *)addr)->resolution = 2233 MOUSE_RES_LOW - sc->mode.resolution; 2234 switch (sc->mode.level) { 2235 case PSM_LEVEL_BASE: 2236 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2237 ((mousemode_t *)addr)->packetsize = 2238 MOUSE_PS2_PACKETSIZE; 2239 break; 2240 case PSM_LEVEL_STANDARD: 2241 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE; 2242 ((mousemode_t *)addr)->packetsize = 2243 MOUSE_SYS_PACKETSIZE; 2244 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK; 2245 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC; 2246 break; 2247 case PSM_LEVEL_NATIVE: 2248 /* FIXME: this isn't quite correct... XXX */ 2249 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2; 2250 break; 2251 } 2252 crit_exit(); 2253 break; 2254 2255 case OLD_MOUSE_SETMODE: 2256 case MOUSE_SETMODE: 2257 if (ap->a_cmd == OLD_MOUSE_SETMODE) { 2258 mode.rate = ((old_mousemode_t *)addr)->rate; 2259 /* 2260 * resolution old I/F new I/F 2261 * default 0 0 2262 * low 1 -2 2263 * medium low 2 -3 2264 * medium high 3 -4 2265 * high 4 -5 2266 */ 2267 if (((old_mousemode_t *)addr)->resolution > 0) 2268 mode.resolution = 2269 -((old_mousemode_t *)addr)->resolution - 1; 2270 else 2271 mode.resolution = 0; 2272 mode.accelfactor = 2273 ((old_mousemode_t *)addr)->accelfactor; 2274 mode.level = -1; 2275 } else 2276 mode = *(mousemode_t *)addr; 2277 2278 /* adjust and validate parameters. */ 2279 if (mode.rate > UCHAR_MAX) 2280 return (EINVAL); 2281 if (mode.rate == 0) 2282 mode.rate = sc->dflt_mode.rate; 2283 else if (mode.rate == -1) 2284 /* don't change the current setting */ 2285 ; 2286 else if (mode.rate < 0) 2287 return (EINVAL); 2288 if (mode.resolution >= UCHAR_MAX) 2289 return (EINVAL); 2290 if (mode.resolution >= 200) 2291 mode.resolution = MOUSE_RES_HIGH; 2292 else if (mode.resolution >= 100) 2293 mode.resolution = MOUSE_RES_MEDIUMHIGH; 2294 else if (mode.resolution >= 50) 2295 mode.resolution = MOUSE_RES_MEDIUMLOW; 2296 else if (mode.resolution > 0) 2297 mode.resolution = MOUSE_RES_LOW; 2298 if (mode.resolution == MOUSE_RES_DEFAULT) 2299 mode.resolution = sc->dflt_mode.resolution; 2300 else if (mode.resolution == -1) 2301 /* don't change the current setting */ 2302 ; 2303 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 2304 mode.resolution = MOUSE_RES_LOW - mode.resolution; 2305 if (mode.level == -1) 2306 /* don't change the current setting */ 2307 mode.level = sc->mode.level; 2308 else if ((mode.level < PSM_LEVEL_MIN) || 2309 (mode.level > PSM_LEVEL_MAX)) 2310 return (EINVAL); 2311 if (mode.accelfactor == -1) 2312 /* don't change the current setting */ 2313 mode.accelfactor = sc->mode.accelfactor; 2314 else if (mode.accelfactor < 0) 2315 return (EINVAL); 2316 2317 /* don't allow anybody to poll the keyboard controller */ 2318 error = block_mouse_data(sc, &command_byte); 2319 if (error) 2320 return (error); 2321 2322 /* set mouse parameters */ 2323 if (mode.rate > 0) 2324 mode.rate = set_mouse_sampling_rate(sc->kbdc, 2325 mode.rate); 2326 if (mode.resolution >= 0) 2327 mode.resolution = 2328 set_mouse_resolution(sc->kbdc, mode.resolution); 2329 set_mouse_scaling(sc->kbdc, 1); 2330 get_mouse_status(sc->kbdc, stat, 0, 3); 2331 2332 crit_enter(); 2333 sc->mode.rate = mode.rate; 2334 sc->mode.resolution = mode.resolution; 2335 sc->mode.accelfactor = mode.accelfactor; 2336 sc->mode.level = mode.level; 2337 crit_exit(); 2338 2339 unblock_mouse_data(sc, command_byte); 2340 break; 2341 2342 case MOUSE_GETLEVEL: 2343 *(int *)addr = sc->mode.level; 2344 break; 2345 2346 case MOUSE_SETLEVEL: 2347 if ((*(int *)addr < PSM_LEVEL_MIN) || 2348 (*(int *)addr > PSM_LEVEL_MAX)) 2349 return (EINVAL); 2350 sc->mode.level = *(int *)addr; 2351 break; 2352 2353 case MOUSE_GETSTATUS: 2354 crit_enter(); 2355 status = sc->status; 2356 sc->status.flags = 0; 2357 sc->status.obutton = sc->status.button; 2358 sc->status.button = 0; 2359 sc->status.dx = 0; 2360 sc->status.dy = 0; 2361 sc->status.dz = 0; 2362 crit_exit(); 2363 *(mousestatus_t *)addr = status; 2364 break; 2365 2366 #if (defined(MOUSE_GETVARS)) 2367 case MOUSE_GETVARS: 2368 var = (mousevar_t *)addr; 2369 bzero(var, sizeof(*var)); 2370 crit_enter(); 2371 var->var[0] = MOUSE_VARS_PS2_SIG; 2372 var->var[1] = sc->config; 2373 var->var[2] = sc->flags; 2374 crit_exit(); 2375 break; 2376 2377 case MOUSE_SETVARS: 2378 return (ENODEV); 2379 #endif /* MOUSE_GETVARS */ 2380 2381 case MOUSE_READSTATE: 2382 case MOUSE_READDATA: 2383 data = (mousedata_t *)addr; 2384 if (data->len > sizeof(data->buf)/sizeof(data->buf[0])) 2385 return (EINVAL); 2386 2387 error = block_mouse_data(sc, &command_byte); 2388 if (error) 2389 return (error); 2390 if ((data->len = get_mouse_status(sc->kbdc, data->buf, 2391 (ap->a_cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0) 2392 error = EIO; 2393 unblock_mouse_data(sc, command_byte); 2394 break; 2395 2396 #if (defined(MOUSE_SETRESOLUTION)) 2397 case MOUSE_SETRESOLUTION: 2398 mode.resolution = *(int *)addr; 2399 if (mode.resolution >= UCHAR_MAX) 2400 return (EINVAL); 2401 else if (mode.resolution >= 200) 2402 mode.resolution = MOUSE_RES_HIGH; 2403 else if (mode.resolution >= 100) 2404 mode.resolution = MOUSE_RES_MEDIUMHIGH; 2405 else if (mode.resolution >= 50) 2406 mode.resolution = MOUSE_RES_MEDIUMLOW; 2407 else if (mode.resolution > 0) 2408 mode.resolution = MOUSE_RES_LOW; 2409 if (mode.resolution == MOUSE_RES_DEFAULT) 2410 mode.resolution = sc->dflt_mode.resolution; 2411 else if (mode.resolution == -1) 2412 mode.resolution = sc->mode.resolution; 2413 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */ 2414 mode.resolution = MOUSE_RES_LOW - mode.resolution; 2415 2416 error = block_mouse_data(sc, &command_byte); 2417 if (error) 2418 return (error); 2419 sc->mode.resolution = 2420 set_mouse_resolution(sc->kbdc, mode.resolution); 2421 if (sc->mode.resolution != mode.resolution) 2422 error = EIO; 2423 unblock_mouse_data(sc, command_byte); 2424 break; 2425 #endif /* MOUSE_SETRESOLUTION */ 2426 2427 #if (defined(MOUSE_SETRATE)) 2428 case MOUSE_SETRATE: 2429 mode.rate = *(int *)addr; 2430 if (mode.rate > UCHAR_MAX) 2431 return (EINVAL); 2432 if (mode.rate == 0) 2433 mode.rate = sc->dflt_mode.rate; 2434 else if (mode.rate < 0) 2435 mode.rate = sc->mode.rate; 2436 2437 error = block_mouse_data(sc, &command_byte); 2438 if (error) 2439 return (error); 2440 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate); 2441 if (sc->mode.rate != mode.rate) 2442 error = EIO; 2443 unblock_mouse_data(sc, command_byte); 2444 break; 2445 #endif /* MOUSE_SETRATE */ 2446 2447 #if (defined(MOUSE_SETSCALING)) 2448 case MOUSE_SETSCALING: 2449 if ((*(int *)addr <= 0) || (*(int *)addr > 2)) 2450 return (EINVAL); 2451 2452 error = block_mouse_data(sc, &command_byte); 2453 if (error) 2454 return (error); 2455 if (!set_mouse_scaling(sc->kbdc, *(int *)addr)) 2456 error = EIO; 2457 unblock_mouse_data(sc, command_byte); 2458 break; 2459 #endif /* MOUSE_SETSCALING */ 2460 2461 #if (defined(MOUSE_GETHWID)) 2462 case MOUSE_GETHWID: 2463 error = block_mouse_data(sc, &command_byte); 2464 if (error) 2465 return (error); 2466 sc->hw.hwid &= ~0x00ff; 2467 sc->hw.hwid |= get_aux_id(sc->kbdc); 2468 *(int *)addr = sc->hw.hwid & 0x00ff; 2469 unblock_mouse_data(sc, command_byte); 2470 break; 2471 #endif /* MOUSE_GETHWID */ 2472 2473 case FIONBIO: 2474 case FIOASYNC: 2475 break; 2476 case FIOSETOWN: 2477 break; 2478 case FIOGETOWN: 2479 break; 2480 default: 2481 return (ENOTTY); 2482 } 2483 2484 return (error); 2485 } 2486 2487 static void 2488 psmtimeout(void *arg) 2489 { 2490 2491 struct psm_softc *sc; 2492 2493 sc = (struct psm_softc *)arg; 2494 crit_enter(); 2495 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) { 2496 VLOG(4, (LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit)); 2497 psmintr(sc); 2498 kbdc_lock(sc->kbdc, FALSE); 2499 } 2500 sc->watchdog = TRUE; 2501 crit_exit(); 2502 callout_reset(&sc->callout, hz, psmtimeout, (void *)(uintptr_t)sc); 2503 } 2504 2505 /* Add all sysctls under the debug.psm and hw.psm nodes */ 2506 SYSCTL_NODE(_debug, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse"); 2507 SYSCTL_NODE(_hw, OID_AUTO, psm, CTLFLAG_RD, 0, "ps/2 mouse"); 2508 2509 SYSCTL_INT(_debug_psm, OID_AUTO, loglevel, CTLFLAG_RW, &verbose, 0, 2510 "Verbosity level"); 2511 2512 static int psmhz = 20; 2513 SYSCTL_INT(_debug_psm, OID_AUTO, hz, CTLFLAG_RW, &psmhz, 0, 2514 "Frequency of the softcallout (in hz)"); 2515 static int psmerrsecs = 2; 2516 SYSCTL_INT(_debug_psm, OID_AUTO, errsecs, CTLFLAG_RW, &psmerrsecs, 0, 2517 "Number of seconds during which packets will dropped after a sync error"); 2518 static int psmerrusecs = 0; 2519 SYSCTL_INT(_debug_psm, OID_AUTO, errusecs, CTLFLAG_RW, &psmerrusecs, 0, 2520 "Microseconds to add to psmerrsecs"); 2521 static int psmsecs = 0; 2522 SYSCTL_INT(_debug_psm, OID_AUTO, secs, CTLFLAG_RW, &psmsecs, 0, 2523 "Max number of seconds between soft interrupts"); 2524 static int psmusecs = 500000; 2525 SYSCTL_INT(_debug_psm, OID_AUTO, usecs, CTLFLAG_RW, &psmusecs, 0, 2526 "Microseconds to add to psmsecs"); 2527 static int pkterrthresh = 2; 2528 SYSCTL_INT(_debug_psm, OID_AUTO, pkterrthresh, CTLFLAG_RW, &pkterrthresh, 0, 2529 "Number of error packets allowed before reinitializing the mouse"); 2530 2531 SYSCTL_INT(_hw_psm, OID_AUTO, tap_enabled, CTLFLAG_RW, &tap_enabled, 0, 2532 "Enable tap and drag gestures"); 2533 static int tap_threshold = PSM_TAP_THRESHOLD; 2534 SYSCTL_INT(_hw_psm, OID_AUTO, tap_threshold, CTLFLAG_RW, &tap_threshold, 0, 2535 "Button tap threshold"); 2536 static int tap_timeout = PSM_TAP_TIMEOUT; 2537 SYSCTL_INT(_hw_psm, OID_AUTO, tap_timeout, CTLFLAG_RW, &tap_timeout, 0, 2538 "Tap timeout for touchpads"); 2539 2540 /* Tunables; defined near top of file */ 2541 SYSCTL_INT(_hw_psm, OID_AUTO, synaptics_support, CTLFLAG_RD, 2542 &synaptics_support, 0, "Enable support for Synaptics touchpads"); 2543 2544 SYSCTL_INT(_hw_psm, OID_AUTO, trackpoint_support, CTLFLAG_RD, 2545 &trackpoint_support, 0, "Enable support for IBM/Lenovo TrackPoint"); 2546 2547 SYSCTL_INT(_hw_psm, OID_AUTO, elantech_support, CTLFLAG_RD, 2548 &elantech_support, 0, "Enable support for Elantech touchpads"); 2549 2550 static void 2551 psmintr(void *arg) 2552 { 2553 struct psm_softc *sc = arg; 2554 struct timeval now; 2555 int c; 2556 packetbuf_t *pb; 2557 2558 2559 /* read until there is nothing to read */ 2560 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) { 2561 pb = &sc->pqueue[sc->pqueue_end]; 2562 2563 /* discard the byte if the device is not open */ 2564 if ((sc->state & PSM_OPEN) == 0) 2565 continue; 2566 2567 getmicrouptime(&now); 2568 if ((pb->inputbytes > 0) && 2569 timevalcmp(&now, &sc->inputtimeout, >)) { 2570 VLOG(3, (LOG_DEBUG, "psmintr: delay too long; " 2571 "resetting byte count\n")); 2572 pb->inputbytes = 0; 2573 sc->syncerrors = 0; 2574 sc->pkterrors = 0; 2575 } 2576 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT / 1000000; 2577 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT % 1000000; 2578 timevaladd(&sc->inputtimeout, &now); 2579 2580 pb->ipacket[pb->inputbytes++] = c; 2581 2582 if (sc->mode.level == PSM_LEVEL_NATIVE) { 2583 VLOG(4, (LOG_DEBUG, "psmintr: %02x\n", pb->ipacket[0])); 2584 sc->syncerrors = 0; 2585 sc->pkterrors = 0; 2586 goto next; 2587 } else { 2588 if (pb->inputbytes < sc->mode.packetsize) 2589 continue; 2590 2591 VLOG(4, (LOG_DEBUG, 2592 "psmintr: %02x %02x %02x %02x %02x %02x\n", 2593 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 2594 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5])); 2595 } 2596 2597 c = pb->ipacket[0]; 2598 2599 if ((sc->flags & PSM_NEED_SYNCBITS) != 0) { 2600 sc->mode.syncmask[1] = (c & sc->mode.syncmask[0]); 2601 sc->flags &= ~PSM_NEED_SYNCBITS; 2602 VLOG(2, (LOG_DEBUG, 2603 "psmintr: Sync bytes now %04x,%04x\n", 2604 sc->mode.syncmask[0], sc->mode.syncmask[0])); 2605 } else if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) { 2606 VLOG(3, (LOG_DEBUG, "psmintr: out of sync " 2607 "(%04x != %04x) %d cmds since last error.\n", 2608 c & sc->mode.syncmask[0], sc->mode.syncmask[1], 2609 sc->cmdcount - sc->lasterr)); 2610 sc->lasterr = sc->cmdcount; 2611 /* 2612 * The sync byte test is a weak measure of packet 2613 * validity. Conservatively discard any input yet 2614 * to be seen by userland when we detect a sync 2615 * error since there is a good chance some of 2616 * the queued packets have undetected errors. 2617 */ 2618 dropqueue(sc); 2619 if (sc->syncerrors == 0) 2620 sc->pkterrors++; 2621 ++sc->syncerrors; 2622 sc->lastinputerr = now; 2623 if (sc->syncerrors >= sc->mode.packetsize * 2 || 2624 sc->pkterrors >= pkterrthresh) { 2625 /* 2626 * If we've failed to find a single sync byte 2627 * in 2 packets worth of data, or we've seen 2628 * persistent packet errors during the 2629 * validation period, reinitialize the mouse 2630 * in hopes of returning it to the expected 2631 * mode. 2632 */ 2633 VLOG(3, (LOG_DEBUG, 2634 "psmintr: reset the mouse.\n")); 2635 reinitialize(sc, TRUE); 2636 } else if (sc->syncerrors == sc->mode.packetsize) { 2637 /* 2638 * Try a soft reset after searching for a sync 2639 * byte through a packet length of bytes. 2640 */ 2641 VLOG(3, (LOG_DEBUG, 2642 "psmintr: re-enable the mouse.\n")); 2643 pb->inputbytes = 0; 2644 disable_aux_dev(sc->kbdc); 2645 enable_aux_dev(sc->kbdc); 2646 } else { 2647 VLOG(3, (LOG_DEBUG, 2648 "psmintr: discard a byte (%d)\n", 2649 sc->syncerrors)); 2650 pb->inputbytes--; 2651 bcopy(&pb->ipacket[1], &pb->ipacket[0], 2652 pb->inputbytes); 2653 } 2654 continue; 2655 } 2656 2657 /* 2658 * We have what appears to be a valid packet. 2659 * Reset the error counters. 2660 */ 2661 sc->syncerrors = 0; 2662 2663 /* 2664 * Drop even good packets if they occur within a timeout 2665 * period of a sync error. This allows the detection of 2666 * a change in the mouse's packet mode without exposing 2667 * erratic mouse behavior to the user. Some KVMs forget 2668 * enhanced mouse modes during switch events. 2669 */ 2670 if (!timeelapsed(&sc->lastinputerr, psmerrsecs, psmerrusecs, 2671 &now)) { 2672 pb->inputbytes = 0; 2673 continue; 2674 } 2675 2676 /* 2677 * Now that we're out of the validation period, reset 2678 * the packet error count. 2679 */ 2680 sc->pkterrors = 0; 2681 2682 sc->cmdcount++; 2683 next: 2684 if (++sc->pqueue_end >= PSM_PACKETQUEUE) 2685 sc->pqueue_end = 0; 2686 /* 2687 * If we've filled the queue then call the softintr ourselves, 2688 * otherwise schedule the interrupt for later. 2689 */ 2690 if (!timeelapsed(&sc->lastsoftintr, psmsecs, psmusecs, &now) || 2691 (sc->pqueue_end == sc->pqueue_start)) { 2692 if ((sc->state & PSM_SOFTARMED) != 0) { 2693 sc->state &= ~PSM_SOFTARMED; 2694 callout_stop(&sc->softcallout); 2695 } 2696 psmsoftintr(arg); 2697 } else if ((sc->state & PSM_SOFTARMED) == 0) { 2698 sc->state |= PSM_SOFTARMED; 2699 callout_reset(&sc->softcallout, psmhz < 1 ? 1 : (hz/psmhz), 2700 psmsoftintr, arg); 2701 } 2702 } 2703 } 2704 2705 static void 2706 proc_mmanplus(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 2707 int *x, int *y, int *z) 2708 { 2709 2710 /* 2711 * PS2++ protocl packet 2712 * 2713 * b7 b6 b5 b4 b3 b2 b1 b0 2714 * byte 1: * 1 p3 p2 1 * * * 2715 * byte 2: c1 c2 p1 p0 d1 d0 1 0 2716 * 2717 * p3-p0: packet type 2718 * c1, c2: c1 & c2 == 1, if p2 == 0 2719 * c1 & c2 == 0, if p2 == 1 2720 * 2721 * packet type: 0 (device type) 2722 * See comments in enable_mmanplus() below. 2723 * 2724 * packet type: 1 (wheel data) 2725 * 2726 * b7 b6 b5 b4 b3 b2 b1 b0 2727 * byte 3: h * B5 B4 s d2 d1 d0 2728 * 2729 * h: 1, if horizontal roller data 2730 * 0, if vertical roller data 2731 * B4, B5: button 4 and 5 2732 * s: sign bit 2733 * d2-d0: roller data 2734 * 2735 * packet type: 2 (reserved) 2736 */ 2737 if (((pb->ipacket[0] & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC) && 2738 (abs(*x) > 191) && MOUSE_PS2PLUS_CHECKBITS(pb->ipacket)) { 2739 /* 2740 * the extended data packet encodes button 2741 * and wheel events 2742 */ 2743 switch (MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket)) { 2744 case 1: 2745 /* wheel data packet */ 2746 *x = *y = 0; 2747 if (pb->ipacket[2] & 0x80) { 2748 /* XXX horizontal roller count - ignore it */ 2749 ; 2750 } else { 2751 /* vertical roller count */ 2752 *z = (pb->ipacket[2] & MOUSE_PS2PLUS_ZNEG) ? 2753 (pb->ipacket[2] & 0x0f) - 16 : 2754 (pb->ipacket[2] & 0x0f); 2755 } 2756 ms->button |= (pb->ipacket[2] & 2757 MOUSE_PS2PLUS_BUTTON4DOWN) ? 2758 MOUSE_BUTTON4DOWN : 0; 2759 ms->button |= (pb->ipacket[2] & 2760 MOUSE_PS2PLUS_BUTTON5DOWN) ? 2761 MOUSE_BUTTON5DOWN : 0; 2762 break; 2763 case 2: 2764 /* 2765 * this packet type is reserved by 2766 * Logitech... 2767 */ 2768 /* 2769 * IBM ScrollPoint Mouse uses this 2770 * packet type to encode both vertical 2771 * and horizontal scroll movement. 2772 */ 2773 *x = *y = 0; 2774 /* horizontal count */ 2775 if (pb->ipacket[2] & 0x0f) 2776 *z = (pb->ipacket[2] & MOUSE_SPOINT_WNEG) ? 2777 -2 : 2; 2778 /* vertical count */ 2779 if (pb->ipacket[2] & 0xf0) 2780 *z = (pb->ipacket[2] & MOUSE_SPOINT_ZNEG) ? 2781 -1 : 1; 2782 break; 2783 case 0: 2784 /* device type packet - shouldn't happen 2785 * 2786 * XXX: if this shouldn't happen, shouldn't we at least 2787 * add a kprintf indiciating to the user something bad 2788 * happened or is it that prevalent that it'd spam? -htse 2789 * 2790 * */ 2791 /* FALLTHROUGH */ 2792 default: 2793 *x = *y = 0; 2794 ms->button = ms->obutton; 2795 VLOG(1, (LOG_DEBUG, "psmintr: unknown PS2++ packet " 2796 "type %d: 0x%02x 0x%02x 0x%02x\n", 2797 MOUSE_PS2PLUS_PACKET_TYPE(pb->ipacket), 2798 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2])); 2799 break; 2800 } 2801 } else { 2802 /* preserve button states */ 2803 ms->button |= ms->obutton & MOUSE_EXTBUTTONS; 2804 } 2805 } 2806 2807 static int 2808 proc_synaptics(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 2809 int *x, int *y, int *z) 2810 { 2811 static int touchpad_buttons; 2812 static int guest_buttons; 2813 static finger_t f[PSM_FINGERS]; 2814 int w, id, nfingers, ewcode, extended_buttons; 2815 2816 extended_buttons = 0; 2817 2818 /* TouchPad PS/2 absolute mode message format with capFourButtons: 2819 * 2820 * Bits: 7 6 5 4 3 2 1 0 (LSB) 2821 * ------------------------------------------------ 2822 * ipacket[0]: 1 0 W3 W2 0 W1 R L 2823 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8 2824 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0 2825 * ipacket[3]: 1 1 Yc Xc 0 W0 D U 2826 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 2827 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 2828 * 2829 * Legend: 2830 * L: left physical mouse button 2831 * R: right physical mouse button 2832 * D: down button 2833 * U: up button 2834 * W: "wrist" value 2835 * X: x position 2836 * Y: y position 2837 * Z: pressure 2838 * 2839 * 2840 * Without capFourButtons but with nExtendeButtons and/or capMiddle 2841 * 2842 * Bits: 7 6 5 4 3 2 1 0 (LSB) 2843 * ------------------------------------------------------ 2844 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L 2845 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1 2846 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2 2847 * 2848 * Legend: 2849 * M: Middle physical mouse button 2850 * E: Extended mouse buttons reported instead of low bits of X and Y 2851 * b1-b8: Extended mouse buttons 2852 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet 2853 * 4 and 5, for reading X and Y value they should be zeroed. 2854 * 2855 * Absolute reportable limits: 0 - 6143. 2856 * Typical bezel limits: 1472 - 5472. 2857 * Typical edge marings: 1632 - 5312. 2858 * 2859 * w = 3 Passthrough Packet 2860 * 2861 * Byte 2,5,6 == Byte 1,2,3 of "Guest" 2862 */ 2863 2864 if (!synaptics_support) 2865 return (0); 2866 2867 /* Sanity check for out of sync packets. */ 2868 if ((pb->ipacket[0] & 0xc8) != 0x80 || 2869 (pb->ipacket[3] & 0xc8) != 0xc0) 2870 return (-1); 2871 2872 *x = *y = 0; 2873 ms->button = ms->obutton; 2874 2875 /* 2876 * Pressure value. 2877 * Interpretation: 2878 * z = 0 No finger contact 2879 * z = 10 Finger hovering near the pad 2880 * z = 30 Very light finger contact 2881 * z = 80 Normal finger contact 2882 * z = 110 Very heavy finger contact 2883 * z = 200 Finger lying flat on pad surface 2884 * z = 255 Maximum reportable Z 2885 */ 2886 *z = pb->ipacket[2]; 2887 2888 /* 2889 * Finger width value 2890 * Interpretation: 2891 * w = 0 Two finger on the pad (capMultiFinger needed) 2892 * w = 1 Three or more fingers (capMultiFinger needed) 2893 * w = 2 Pen (instead of finger) (capPen needed) 2894 * w = 3 Reserved (passthrough?) 2895 * w = 4-7 Finger of normal width (capPalmDetect needed) 2896 * w = 8-14 Very wide finger or palm (capPalmDetect needed) 2897 * w = 15 Maximum reportable width (capPalmDetect needed) 2898 */ 2899 /* XXX Is checking capExtended enough? */ 2900 if (sc->synhw.capExtended) { 2901 w = ((pb->ipacket[0] & 0x30) >> 2) | 2902 ((pb->ipacket[0] & 0x04) >> 1) | 2903 ((pb->ipacket[3] & 0x04) >> 2); 2904 } 2905 else { 2906 /* Assume a finger of regular width. */ 2907 w = 4; 2908 } 2909 2910 switch (w) { 2911 case 3: 2912 /* 2913 * Handle packets from the guest device. See: 2914 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1 2915 */ 2916 if (sc->synhw.capPassthrough) { 2917 *x = ((pb->ipacket[1] & 0x10) ? 2918 pb->ipacket[4] - 256 : pb->ipacket[4]); 2919 *y = ((pb->ipacket[1] & 0x20) ? 2920 pb->ipacket[5] - 256 : pb->ipacket[5]); 2921 *z = 0; 2922 2923 guest_buttons = 0; 2924 if (pb->ipacket[1] & 0x01) 2925 guest_buttons |= MOUSE_BUTTON1DOWN; 2926 if (pb->ipacket[1] & 0x04) 2927 guest_buttons |= MOUSE_BUTTON2DOWN; 2928 if (pb->ipacket[1] & 0x02) 2929 guest_buttons |= MOUSE_BUTTON3DOWN; 2930 2931 ms->button = touchpad_buttons | guest_buttons | 2932 sc->extended_buttons; 2933 } 2934 goto SYNAPTICS_END; 2935 2936 case 2: 2937 /* Handle Extended W mode packets */ 2938 ewcode = (pb->ipacket[5] & 0xf0) >> 4; 2939 #if PSM_FINGERS > 1 2940 switch (ewcode) { 2941 case 1: 2942 /* Secondary finger */ 2943 if (sc->synhw.capAdvancedGestures) 2944 f[1] = (finger_t) { 2945 .x = (((pb->ipacket[4] & 0x0f) << 8) | 2946 pb->ipacket[1]) << 1, 2947 .y = (((pb->ipacket[4] & 0xf0) << 4) | 2948 pb->ipacket[2]) << 1, 2949 .p = ((pb->ipacket[3] & 0x30) | 2950 (pb->ipacket[5] & 0x0f)) << 1, 2951 .w = PSM_FINGER_DEFAULT_W, 2952 .flags = PSM_FINGER_FUZZY, 2953 }; 2954 else if (sc->synhw.capReportsV) 2955 f[1] = (finger_t) { 2956 .x = (((pb->ipacket[4] & 0x0f) << 8) | 2957 (pb->ipacket[1] & 0xfe)) << 1, 2958 .y = (((pb->ipacket[4] & 0xf0) << 4) | 2959 (pb->ipacket[2] & 0xfe)) << 1, 2960 .p = ((pb->ipacket[3] & 0x30) | 2961 (pb->ipacket[5] & 0x0e)) << 1, 2962 .w = (((pb->ipacket[5] & 0x01) << 2) | 2963 ((pb->ipacket[2] & 0x01) << 1) | 2964 (pb->ipacket[1] & 0x01)) + 8, 2965 .flags = PSM_FINGER_FUZZY, 2966 }; 2967 default: 2968 break; 2969 } 2970 #endif 2971 goto SYNAPTICS_END; 2972 2973 case 1: 2974 case 0: 2975 nfingers = w + 2; 2976 break; 2977 2978 default: 2979 nfingers = 1; 2980 } 2981 2982 if (sc->syninfo.touchpad_off) 2983 goto SYNAPTICS_END; 2984 2985 /* Button presses */ 2986 touchpad_buttons = 0; 2987 if (pb->ipacket[0] & 0x01) 2988 touchpad_buttons |= MOUSE_BUTTON1DOWN; 2989 if (pb->ipacket[0] & 0x02) 2990 touchpad_buttons |= MOUSE_BUTTON3DOWN; 2991 2992 if (sc->synhw.capExtended && sc->synhw.capFourButtons) { 2993 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01) 2994 touchpad_buttons |= MOUSE_BUTTON4DOWN; 2995 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02) 2996 touchpad_buttons |= MOUSE_BUTTON5DOWN; 2997 } else if (sc->synhw.capExtended && sc->synhw.capMiddle && 2998 !sc->synhw.capClickPad) { 2999 /* Middle Button */ 3000 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01) 3001 touchpad_buttons |= MOUSE_BUTTON2DOWN; 3002 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) { 3003 /* Extended Buttons */ 3004 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) { 3005 if (sc->syninfo.directional_scrolls) { 3006 if (pb->ipacket[4] & 0x01) 3007 extended_buttons |= MOUSE_BUTTON4DOWN; 3008 if (pb->ipacket[5] & 0x01) 3009 extended_buttons |= MOUSE_BUTTON5DOWN; 3010 if (pb->ipacket[4] & 0x02) 3011 extended_buttons |= MOUSE_BUTTON6DOWN; 3012 if (pb->ipacket[5] & 0x02) 3013 extended_buttons |= MOUSE_BUTTON7DOWN; 3014 } else { 3015 if (pb->ipacket[4] & 0x01) 3016 extended_buttons |= MOUSE_BUTTON1DOWN; 3017 if (pb->ipacket[5] & 0x01) 3018 extended_buttons |= MOUSE_BUTTON3DOWN; 3019 if (pb->ipacket[4] & 0x02) 3020 extended_buttons |= MOUSE_BUTTON2DOWN; 3021 sc->extended_buttons = extended_buttons; 3022 } 3023 3024 /* 3025 * Zero out bits used by extended buttons to avoid 3026 * misinterpretation of the data absolute position. 3027 * 3028 * The bits represented by 3029 * 3030 * (nExtendedButtons + 1) >> 1 3031 * 3032 * will be masked out in both bytes. 3033 * The mask for n bits is computed with the formula 3034 * 3035 * (1 << n) - 1 3036 */ 3037 int maskedbits = 0; 3038 int mask = 0; 3039 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1; 3040 mask = (1 << maskedbits) - 1; 3041 pb->ipacket[4] &= ~(mask); 3042 pb->ipacket[5] &= ~(mask); 3043 } else if (!sc->syninfo.directional_scrolls && 3044 !sc->gesture.in_vscroll) { 3045 /* 3046 * Keep reporting MOUSE DOWN until we get a new packet 3047 * indicating otherwise. 3048 */ 3049 extended_buttons |= sc->extended_buttons; 3050 } 3051 } 3052 /* Handle ClickPad */ 3053 if (sc->synhw.capClickPad && 3054 ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01)) 3055 touchpad_buttons |= MOUSE_BUTTON1DOWN; 3056 3057 if (sc->synhw.capReportsV && nfingers > 1) 3058 f[0] = (finger_t) { 3059 .x = ((pb->ipacket[3] & 0x10) << 8) | 3060 ((pb->ipacket[1] & 0x0f) << 8) | 3061 (pb->ipacket[4] & 0xfd), 3062 .y = ((pb->ipacket[3] & 0x20) << 7) | 3063 ((pb->ipacket[1] & 0xf0) << 4) | 3064 (pb->ipacket[5] & 0xfd), 3065 .p = *z & 0xfe, 3066 .w = (((pb->ipacket[2] & 0x01) << 2) | 3067 (pb->ipacket[5] & 0x02) | 3068 ((pb->ipacket[4] & 0x02) >> 1)) + 8, 3069 .flags = PSM_FINGER_FUZZY, 3070 }; 3071 else 3072 f[0] = (finger_t) { 3073 .x = ((pb->ipacket[3] & 0x10) << 8) | 3074 ((pb->ipacket[1] & 0x0f) << 8) | 3075 pb->ipacket[4], 3076 .y = ((pb->ipacket[3] & 0x20) << 7) | 3077 ((pb->ipacket[1] & 0xf0) << 4) | 3078 pb->ipacket[5], 3079 .p = *z, 3080 .w = w, 3081 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0, 3082 }; 3083 3084 /* Ignore hovering and unmeasurable touches */ 3085 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2) 3086 nfingers = 0; 3087 3088 for (id = 0; id < PSM_FINGERS; id++) 3089 if (id >= nfingers) 3090 PSM_FINGER_RESET(f[id]); 3091 3092 ms->button = touchpad_buttons; 3093 3094 /* Palm detection doesn't terminate the current action. */ 3095 if (!psmpalmdetect(sc, &f[0], nfingers)) { 3096 psmgestures(sc, &f[0], nfingers, ms); 3097 for (id = 0; id < PSM_FINGERS; id++) 3098 psmsmoother(sc, &f[id], id, ms, x, y); 3099 } else { 3100 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f[0].w)); 3101 } 3102 3103 ms->button |= extended_buttons | guest_buttons; 3104 3105 3106 3107 SYNAPTICS_END: 3108 /* 3109 * Use the extra buttons as a scrollwheel 3110 * 3111 * XXX X.Org uses the Z axis for vertical wheel only, 3112 * whereas moused(8) understands special values to differ 3113 * vertical and horizontal wheels. 3114 * 3115 * xf86-input-mouse needs therefore a small patch to 3116 * understand these special values. Without it, the 3117 * horizontal wheel acts as a vertical wheel in X.Org. 3118 * 3119 * That's why the horizontal wheel is disabled by 3120 * default for now. 3121 */ 3122 if (ms->button & MOUSE_BUTTON4DOWN) { 3123 *z = -1; 3124 } else if (ms->button & MOUSE_BUTTON5DOWN) { 3125 *z = 1; 3126 } else if (ms->button & MOUSE_BUTTON6DOWN) { 3127 *z = -2; 3128 } else if (ms->button & MOUSE_BUTTON7DOWN) { 3129 *z = 2; 3130 } else 3131 *z = 0; 3132 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN | 3133 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN); 3134 3135 return (0); 3136 } 3137 3138 static int 3139 psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers) 3140 { 3141 if (!( 3142 ((sc->synhw.capMultiFinger || 3143 sc->synhw.capAdvancedGestures) && nfingers > 1) || 3144 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) || 3145 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) || 3146 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) { 3147 /* 3148 * We consider the packet irrelevant for the current 3149 * action when: 3150 * - the width isn't comprised in: 3151 * [1; max_width] 3152 * - the pressure isn't comprised in: 3153 * [min_pressure; max_pressure] 3154 * - pen aren't supported but PSM_FINGER_IS_PEN is set 3155 */ 3156 return (1); 3157 } 3158 return (0); 3159 } 3160 3161 static void 3162 psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers, 3163 mousestatus_t *ms) 3164 { 3165 smoother_t *smoother; 3166 gesture_t *gest; 3167 finger_t *f; 3168 int y_ok, center_button, center_x, right_button, right_x, i; 3169 3170 f = &fingers[0]; 3171 smoother = &sc->smoother[0]; 3172 gest = &sc->gesture; 3173 3174 /* Find first active finger. */ 3175 if (nfingers > 0) { 3176 for (i = 0; i < PSM_FINGERS; i++) { 3177 if (PSM_FINGER_IS_SET(fingers[i])) { 3178 f = &fingers[i]; 3179 smoother = &sc->smoother[i]; 3180 break; 3181 } 3182 } 3183 } 3184 3185 /* 3186 * Check pressure to detect a real wanted action on the 3187 * touchpad. 3188 */ 3189 if (f->p >= sc->syninfo.min_pressure) { 3190 int x0, y0; 3191 int dxp, dyp; 3192 int start_x, start_y; 3193 int queue_len; 3194 int margin_top, margin_right, margin_bottom, margin_left; 3195 int window_min, window_max; 3196 int vscroll_hor_area, vscroll_ver_area; 3197 int two_finger_scroll; 3198 int max_x, max_y; 3199 3200 /* silence gcc warnings for last VLOG call */ 3201 dxp = dyp = 0; 3202 3203 /* Read sysctl. */ 3204 /* XXX Verify values? */ 3205 margin_top = sc->syninfo.margin_top; 3206 margin_right = sc->syninfo.margin_right; 3207 margin_bottom = sc->syninfo.margin_bottom; 3208 margin_left = sc->syninfo.margin_left; 3209 window_min = sc->syninfo.window_min; 3210 window_max = sc->syninfo.window_max; 3211 vscroll_hor_area = sc->syninfo.vscroll_hor_area; 3212 vscroll_ver_area = sc->syninfo.vscroll_ver_area; 3213 two_finger_scroll = sc->syninfo.two_finger_scroll; 3214 max_x = sc->syninfo.max_x; 3215 max_y = sc->syninfo.max_y; 3216 3217 /* Read current absolute position. */ 3218 x0 = f->x; 3219 y0 = f->y; 3220 3221 /* 3222 * Limit the coordinates to the specified margins because 3223 * this area isn't very reliable. 3224 */ 3225 if (x0 <= margin_left) 3226 x0 = margin_left; 3227 else if (x0 >= max_x - margin_right) 3228 x0 = max_x - margin_right; 3229 if (y0 <= margin_bottom) 3230 y0 = margin_bottom; 3231 else if (y0 >= max_y - margin_top) 3232 y0 = max_y - margin_top; 3233 3234 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n", 3235 x0, y0, f->p, f->w)); 3236 3237 /* 3238 * If the action is just beginning, init the structure and 3239 * compute tap timeout. 3240 */ 3241 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) { 3242 VLOG(3, (LOG_DEBUG, "synaptics: ----\n")); 3243 3244 /* Initialize queue. */ 3245 gest->window_min = window_min; 3246 3247 /* Reset pressure peak. */ 3248 gest->zmax = 0; 3249 3250 /* Reset fingers count. */ 3251 gest->fingers_nb = 0; 3252 3253 /* Reset virtual scrolling state. */ 3254 gest->in_vscroll = 0; 3255 3256 /* Compute tap timeout. */ 3257 gest->taptimeout.tv_sec = tap_timeout / 1000000; 3258 gest->taptimeout.tv_usec = tap_timeout % 1000000; 3259 timevaladd(&gest->taptimeout, &sc->lastsoftintr); 3260 3261 sc->flags |= PSM_FLAGS_FINGERDOWN; 3262 3263 /* Smoother has not been reset yet */ 3264 queue_len = 1; 3265 start_x = x0; 3266 start_y = y0; 3267 } else { 3268 queue_len = smoother->queue_len + 1; 3269 start_x = smoother->start_x; 3270 start_y = smoother->start_y; 3271 } 3272 3273 /* Process ClickPad softbuttons */ 3274 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) { 3275 y_ok = sc->syninfo.softbuttons_y >= 0 ? 3276 start_y < sc->syninfo.softbuttons_y : 3277 start_y > max_y - sc->syninfo.softbuttons_y; 3278 3279 center_button = MOUSE_BUTTON2DOWN; 3280 center_x = sc->syninfo.softbutton2_x; 3281 right_button = MOUSE_BUTTON3DOWN; 3282 right_x = sc->syninfo.softbutton3_x; 3283 3284 if (center_x > 0 && right_x > 0 && center_x > right_x) { 3285 center_button = MOUSE_BUTTON3DOWN; 3286 center_x = sc->syninfo.softbutton3_x; 3287 right_button = MOUSE_BUTTON2DOWN; 3288 right_x = sc->syninfo.softbutton2_x; 3289 } 3290 3291 if (right_x > 0 && start_x > right_x && y_ok) 3292 ms->button = (ms->button & 3293 ~MOUSE_BUTTON1DOWN) | right_button; 3294 else if (center_x > 0 && start_x > center_x && y_ok) 3295 ms->button = (ms->button & 3296 ~MOUSE_BUTTON1DOWN) | center_button; 3297 } 3298 3299 /* If in tap-hold, add the recorded button. */ 3300 if (gest->in_taphold) 3301 ms->button |= gest->tap_button; 3302 3303 /* 3304 * For tap, we keep the maximum number of fingers and the 3305 * pressure peak. Also with multiple fingers, we increase 3306 * the minimum window. 3307 */ 3308 if (nfingers > 1) 3309 gest->window_min = window_max; 3310 gest->fingers_nb = imax(nfingers, gest->fingers_nb); 3311 gest->zmax = imax(f->p, gest->zmax); 3312 3313 /* Do we have enough packets to consider this a gesture? */ 3314 if (queue_len < gest->window_min) 3315 return; 3316 3317 /* Is a scrolling action occurring? */ 3318 if (!gest->in_taphold && !ms->button && 3319 (!gest->in_vscroll || two_finger_scroll)) { 3320 /* 3321 * A scrolling action must not conflict with a tap 3322 * action. Here are the conditions to consider a 3323 * scrolling action: 3324 * - the action in a configurable area 3325 * - one of the following: 3326 * . the distance between the last packet and the 3327 * first should be above a configurable minimum 3328 * . tap timed out 3329 */ 3330 dxp = abs(x0 - start_x); 3331 dyp = abs(y0 - start_y); 3332 3333 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) || 3334 dxp >= sc->syninfo.vscroll_min_delta || 3335 dyp >= sc->syninfo.vscroll_min_delta) { 3336 /* 3337 * Handle two finger scrolling. 3338 * Note that we don't rely on fingers_nb 3339 * as that keeps the maximum number of fingers. 3340 */ 3341 if (two_finger_scroll) { 3342 if (nfingers == 2) { 3343 gest->in_vscroll += 3344 dyp ? 2 : 0; 3345 gest->in_vscroll += 3346 dxp ? 1 : 0; 3347 } 3348 } else { 3349 /* Check for horizontal scrolling. */ 3350 if ((vscroll_hor_area > 0 && 3351 start_y <= vscroll_hor_area) || 3352 (vscroll_hor_area < 0 && 3353 start_y >= 3354 max_y + vscroll_hor_area)) 3355 gest->in_vscroll += 2; 3356 3357 /* Check for vertical scrolling. */ 3358 if ((vscroll_ver_area > 0 && 3359 start_x <= vscroll_ver_area) || 3360 (vscroll_ver_area < 0 && 3361 start_x >= 3362 max_x + vscroll_ver_area)) 3363 gest->in_vscroll += 1; 3364 } 3365 3366 /* Avoid conflicts if area overlaps. */ 3367 if (gest->in_vscroll >= 3) 3368 gest->in_vscroll = 3369 (dxp > dyp) ? 2 : 1; 3370 } 3371 } 3372 /* 3373 * Reset two finger scrolling when the number of fingers 3374 * is different from two or any button is pressed. 3375 */ 3376 if (two_finger_scroll && gest->in_vscroll != 0 && 3377 (nfingers != 2 || ms->button)) 3378 gest->in_vscroll = 0; 3379 3380 VLOG(5, (LOG_DEBUG, 3381 "synaptics: virtual scrolling: %s " 3382 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n", 3383 gest->in_vscroll ? "YES" : "NO", 3384 gest->in_vscroll, dxp, dyp, 3385 gest->fingers_nb)); 3386 3387 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) { 3388 /* 3389 * An action is currently taking place but the pressure 3390 * dropped under the minimum, putting an end to it. 3391 */ 3392 int taphold_timeout, dx, dy, tap_max_delta; 3393 3394 dx = abs(smoother->queue[smoother->queue_cursor].x - 3395 smoother->start_x); 3396 dy = abs(smoother->queue[smoother->queue_cursor].y - 3397 smoother->start_y); 3398 3399 /* Max delta is disabled for multi-fingers tap. */ 3400 if (gest->fingers_nb > 1) 3401 tap_max_delta = imax(dx, dy); 3402 else 3403 tap_max_delta = sc->syninfo.tap_max_delta; 3404 3405 sc->flags &= ~PSM_FLAGS_FINGERDOWN; 3406 3407 /* Check for tap. */ 3408 VLOG(3, (LOG_DEBUG, 3409 "synaptics: zmax=%d, dx=%d, dy=%d, " 3410 "delta=%d, fingers=%d, queue=%d\n", 3411 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb, 3412 smoother->queue_len)); 3413 if (!gest->in_vscroll && gest->zmax >= tap_threshold && 3414 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) && 3415 dx <= tap_max_delta && dy <= tap_max_delta && 3416 smoother->queue_len >= sc->syninfo.tap_min_queue) { 3417 /* 3418 * We have a tap if: 3419 * - the maximum pressure went over tap_threshold 3420 * - the action ended before tap_timeout 3421 * 3422 * To handle tap-hold, we must delay any button push to 3423 * the next action. 3424 */ 3425 if (gest->in_taphold) { 3426 /* 3427 * This is the second and last tap of a 3428 * double tap action, not a tap-hold. 3429 */ 3430 gest->in_taphold = 0; 3431 3432 /* 3433 * For double-tap to work: 3434 * - no button press is emitted (to 3435 * simulate a button release) 3436 * - PSM_FLAGS_FINGERDOWN is set to 3437 * force the next packet to emit a 3438 * button press) 3439 */ 3440 VLOG(2, (LOG_DEBUG, 3441 "synaptics: button RELEASE: %d\n", 3442 gest->tap_button)); 3443 sc->flags |= PSM_FLAGS_FINGERDOWN; 3444 3445 /* Schedule button press on next interrupt */ 3446 sc->idletimeout.tv_sec = psmhz > 1 ? 3447 0 : 1; 3448 sc->idletimeout.tv_usec = psmhz > 1 ? 3449 1000000 / psmhz : 0; 3450 } else { 3451 /* 3452 * This is the first tap: we set the 3453 * tap-hold state and notify the button 3454 * down event. 3455 */ 3456 gest->in_taphold = 1; 3457 taphold_timeout = sc->syninfo.taphold_timeout; 3458 gest->taptimeout.tv_sec = taphold_timeout / 3459 1000000; 3460 gest->taptimeout.tv_usec = taphold_timeout % 3461 1000000; 3462 sc->idletimeout = gest->taptimeout; 3463 timevaladd(&gest->taptimeout, 3464 &sc->lastsoftintr); 3465 3466 switch (gest->fingers_nb) { 3467 case 3: 3468 gest->tap_button = 3469 MOUSE_BUTTON2DOWN; 3470 break; 3471 case 2: 3472 gest->tap_button = 3473 MOUSE_BUTTON3DOWN; 3474 break; 3475 default: 3476 gest->tap_button = 3477 MOUSE_BUTTON1DOWN; 3478 } 3479 VLOG(2, (LOG_DEBUG, 3480 "synaptics: button PRESS: %d\n", 3481 gest->tap_button)); 3482 ms->button |= gest->tap_button; 3483 } 3484 } else { 3485 /* 3486 * Not enough pressure or timeout: reset 3487 * tap-hold state. 3488 */ 3489 if (gest->in_taphold) { 3490 VLOG(2, (LOG_DEBUG, 3491 "synaptics: button RELEASE: %d\n", 3492 gest->tap_button)); 3493 gest->in_taphold = 0; 3494 } else { 3495 VLOG(2, (LOG_DEBUG, 3496 "synaptics: not a tap-hold\n")); 3497 } 3498 } 3499 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) { 3500 /* 3501 * For a tap-hold to work, the button must remain down at 3502 * least until timeout (where the in_taphold flags will be 3503 * cleared) or during the next action. 3504 */ 3505 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) { 3506 ms->button |= gest->tap_button; 3507 } else { 3508 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n", 3509 gest->tap_button)); 3510 gest->in_taphold = 0; 3511 } 3512 } 3513 3514 return; 3515 } 3516 3517 static void 3518 psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id, 3519 mousestatus_t *ms, int *x, int *y) 3520 { 3521 smoother_t *smoother = &sc->smoother[smoother_id]; 3522 gesture_t *gest = &(sc->gesture); 3523 3524 /* 3525 * Check pressure to detect a real wanted action on the 3526 * touchpad. 3527 */ 3528 if (f->p >= sc->syninfo.min_pressure) { 3529 int x0, y0; 3530 int cursor, peer, window; 3531 int dx, dy, dxp, dyp; 3532 int max_width, max_pressure; 3533 int margin_top, margin_right, margin_bottom, margin_left; 3534 int na_top, na_right, na_bottom, na_left; 3535 int window_min, window_max; 3536 int multiplicator; 3537 int weight_current, weight_previous, weight_len_squared; 3538 int div_min, div_max, div_len; 3539 int vscroll_hor_area, vscroll_ver_area; 3540 int two_finger_scroll; 3541 int max_x, max_y; 3542 int len, weight_prev_x, weight_prev_y; 3543 int div_max_x, div_max_y, div_x, div_y; 3544 int is_fuzzy; 3545 3546 /* Read sysctl. */ 3547 /* XXX Verify values? */ 3548 max_width = sc->syninfo.max_width; 3549 max_pressure = sc->syninfo.max_pressure; 3550 margin_top = sc->syninfo.margin_top; 3551 margin_right = sc->syninfo.margin_right; 3552 margin_bottom = sc->syninfo.margin_bottom; 3553 margin_left = sc->syninfo.margin_left; 3554 na_top = sc->syninfo.na_top; 3555 na_right = sc->syninfo.na_right; 3556 na_bottom = sc->syninfo.na_bottom; 3557 na_left = sc->syninfo.na_left; 3558 window_min = sc->syninfo.window_min; 3559 window_max = sc->syninfo.window_max; 3560 multiplicator = sc->syninfo.multiplicator; 3561 weight_current = sc->syninfo.weight_current; 3562 weight_previous = sc->syninfo.weight_previous; 3563 weight_len_squared = sc->syninfo.weight_len_squared; 3564 div_min = sc->syninfo.div_min; 3565 div_max = sc->syninfo.div_max; 3566 div_len = sc->syninfo.div_len; 3567 vscroll_hor_area = sc->syninfo.vscroll_hor_area; 3568 vscroll_ver_area = sc->syninfo.vscroll_ver_area; 3569 two_finger_scroll = sc->syninfo.two_finger_scroll; 3570 max_x = sc->syninfo.max_x; 3571 max_y = sc->syninfo.max_y; 3572 3573 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0; 3574 3575 /* Read current absolute position. */ 3576 x0 = f->x; 3577 y0 = f->y; 3578 3579 /* 3580 * Limit the coordinates to the specified margins because 3581 * this area isn't very reliable. 3582 */ 3583 if (x0 <= margin_left) 3584 x0 = margin_left; 3585 else if (x0 >= max_x - margin_right) 3586 x0 = max_x - margin_right; 3587 if (y0 <= margin_bottom) 3588 y0 = margin_bottom; 3589 else if (y0 >= max_y - margin_top) 3590 y0 = max_y - margin_top; 3591 3592 /* If the action is just beginning, init the structure. */ 3593 if (smoother->active == 0) { 3594 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id)); 3595 3596 /* Store the first point of this action. */ 3597 smoother->start_x = x0; 3598 smoother->start_y = y0; 3599 dx = dy = 0; 3600 3601 /* Initialize queue. */ 3602 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE; 3603 smoother->queue_len = 0; 3604 3605 /* Reset average. */ 3606 smoother->avg_dx = 0; 3607 smoother->avg_dy = 0; 3608 3609 /* Reset squelch. */ 3610 smoother->squelch_x = 0; 3611 smoother->squelch_y = 0; 3612 3613 /* Activate queue */ 3614 smoother->active = 1; 3615 } else { 3616 /* Calculate the current delta. */ 3617 cursor = smoother->queue_cursor; 3618 dx = x0 - smoother->queue[cursor].x; 3619 dy = y0 - smoother->queue[cursor].y; 3620 } 3621 3622 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n", 3623 smoother_id, x0, y0, f->p, f->w)); 3624 3625 /* Queue this new packet. */ 3626 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1); 3627 smoother->queue[cursor].x = x0; 3628 smoother->queue[cursor].y = y0; 3629 smoother->queue_cursor = cursor; 3630 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE) 3631 smoother->queue_len++; 3632 VLOG(5, (LOG_DEBUG, 3633 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n", 3634 smoother_id, cursor, x0, y0, dx, dy)); 3635 3636 /* Do we have enough packets to consider this a movement? */ 3637 if (smoother->queue_len < gest->window_min) 3638 return; 3639 3640 weight_prev_x = weight_prev_y = weight_previous; 3641 div_max_x = div_max_y = div_max; 3642 3643 if (gest->in_vscroll) { 3644 /* Dividers are different with virtual scrolling. */ 3645 div_min = sc->syninfo.vscroll_div_min; 3646 div_max_x = div_max_y = sc->syninfo.vscroll_div_max; 3647 } else { 3648 /* 3649 * There's a lot of noise in coordinates when 3650 * the finger is on the touchpad's borders. When 3651 * using this area, we apply a special weight and 3652 * div. 3653 */ 3654 if (x0 <= na_left || x0 >= max_x - na_right) { 3655 weight_prev_x = sc->syninfo.weight_previous_na; 3656 div_max_x = sc->syninfo.div_max_na; 3657 } 3658 3659 if (y0 <= na_bottom || y0 >= max_y - na_top) { 3660 weight_prev_y = sc->syninfo.weight_previous_na; 3661 div_max_y = sc->syninfo.div_max_na; 3662 } 3663 } 3664 3665 /* 3666 * Calculate weights for the average operands and 3667 * the divisor. Both depend on the distance between 3668 * the current packet and a previous one (based on the 3669 * window width). 3670 */ 3671 window = imin(smoother->queue_len, window_max); 3672 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1); 3673 dxp = abs(x0 - smoother->queue[peer].x) + 1; 3674 dyp = abs(y0 - smoother->queue[peer].y) + 1; 3675 len = (dxp * dxp) + (dyp * dyp); 3676 weight_prev_x = imin(weight_prev_x, 3677 weight_len_squared * weight_prev_x / len); 3678 weight_prev_y = imin(weight_prev_y, 3679 weight_len_squared * weight_prev_y / len); 3680 3681 len = (dxp + dyp) / 2; 3682 div_x = div_len * div_max_x / len; 3683 div_x = imin(div_max_x, div_x); 3684 div_x = imax(div_min, div_x); 3685 div_y = div_len * div_max_y / len; 3686 div_y = imin(div_max_y, div_y); 3687 div_y = imax(div_min, div_y); 3688 3689 VLOG(3, (LOG_DEBUG, 3690 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n", 3691 smoother_id, peer, len, weight_prev_x, weight_prev_y, 3692 div_x, div_y)); 3693 3694 /* Compute averages. */ 3695 smoother->avg_dx = 3696 (weight_current * dx * multiplicator + 3697 weight_prev_x * smoother->avg_dx) / 3698 (weight_current + weight_prev_x); 3699 3700 smoother->avg_dy = 3701 (weight_current * dy * multiplicator + 3702 weight_prev_y * smoother->avg_dy) / 3703 (weight_current + weight_prev_y); 3704 3705 VLOG(5, (LOG_DEBUG, 3706 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id, 3707 smoother->avg_dx / multiplicator, 3708 smoother->avg_dy / multiplicator)); 3709 3710 /* Use these averages to calculate x & y. */ 3711 smoother->squelch_x += smoother->avg_dx; 3712 dxp = smoother->squelch_x / (div_x * multiplicator); 3713 smoother->squelch_x = smoother->squelch_x % 3714 (div_x * multiplicator); 3715 3716 smoother->squelch_y += smoother->avg_dy; 3717 dyp = smoother->squelch_y / (div_y * multiplicator); 3718 smoother->squelch_y = smoother->squelch_y % 3719 (div_y * multiplicator); 3720 3721 switch(gest->in_vscroll) { 3722 case 0: /* Pointer movement. */ 3723 /* On real<->fuzzy finger switch the x/y pos jumps */ 3724 if (is_fuzzy == smoother->is_fuzzy) { 3725 *x += dxp; 3726 *y += dyp; 3727 } 3728 3729 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n", 3730 smoother_id, dx, dy, dxp, dyp)); 3731 break; 3732 case 1: /* Vertical scrolling. */ 3733 if (dyp != 0) 3734 ms->button |= (dyp > 0) ? 3735 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN; 3736 break; 3737 case 2: /* Horizontal scrolling. */ 3738 if (dxp != 0) 3739 ms->button |= (dxp > 0) ? 3740 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN; 3741 break; 3742 } 3743 3744 smoother->is_fuzzy = is_fuzzy; 3745 3746 } else { 3747 /* 3748 * Deactivate queue. Note: We can not just reset queue here 3749 * as these values are still used by gesture processor. 3750 * So postpone reset till next touch. 3751 */ 3752 smoother->active = 0; 3753 } 3754 } 3755 3756 static int 3757 proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 3758 int *x, int *y, int *z) 3759 { 3760 static int touchpad_button, trackpoint_button; 3761 finger_t fn, f[ELANTECH_MAX_FINGERS]; 3762 int pkt, id, scale, i, nfingers, mask; 3763 3764 if (!elantech_support) 3765 return (0); 3766 3767 /* Determine packet format and do a sanity check for out of sync packets. */ 3768 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion)) 3769 pkt = ELANTECH_PKT_NOP; 3770 else if (ELANTECH_PKT_IS_TRACKPOINT(pb)) 3771 pkt = ELANTECH_PKT_TRACKPOINT; 3772 else 3773 switch (sc->elanhw.hwversion) { 3774 case 2: 3775 if (!ELANTECH_PKT_IS_V2(pb)) 3776 return (-1); 3777 3778 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ? 3779 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON; 3780 break; 3781 case 3: 3782 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) && 3783 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc)) 3784 return (-1); 3785 3786 pkt = ELANTECH_PKT_V3; 3787 break; 3788 case 4: 3789 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc)) 3790 return (-1); 3791 3792 switch (pb->ipacket[3] & 0x03) { 3793 case 0x00: 3794 pkt = ELANTECH_PKT_V4_STATUS; 3795 break; 3796 case 0x01: 3797 pkt = ELANTECH_PKT_V4_HEAD; 3798 break; 3799 case 0x02: 3800 pkt = ELANTECH_PKT_V4_MOTION; 3801 break; 3802 default: 3803 return (-1); 3804 } 3805 break; 3806 default: 3807 return (-1); 3808 } 3809 3810 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt)); 3811 3812 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 3813 PSM_FINGER_RESET(f[id]); 3814 3815 *x = *y = *z = 0; 3816 ms->button = ms->obutton; 3817 3818 if (sc->syninfo.touchpad_off) 3819 return (0); 3820 3821 /* Common legend 3822 * L: Left mouse button pressed 3823 * R: Right mouse button pressed 3824 * N: number of fingers on touchpad 3825 * X: absolute x value (horizontal) 3826 * Y: absolute y value (vertical) 3827 * W; width of the finger touch 3828 * P: pressure 3829 */ 3830 switch (pkt) { 3831 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */ 3832 /* 7 6 5 4 3 2 1 0 (LSB) 3833 * ------------------------------------------- 3834 * ipacket[0]: N1 N0 W3 W2 . . R L 3835 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 3836 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 3837 * ipacket[3]: N4 VF W1 W0 . . . B2 3838 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 3839 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3840 * ------------------------------------------- 3841 * N4: set if more than 3 fingers (only in 3 fingers mode) 3842 * VF: a kind of flag? (only on EF123, 0 when finger 3843 * is over one of the buttons, 1 otherwise) 3844 * B2: (on EF113 only, 0 otherwise), one button pressed 3845 * P & W is not reported on EF113 touchpads 3846 */ 3847 nfingers = (pb->ipacket[0] & 0xc0) >> 6; 3848 if (nfingers == 3 && (pb->ipacket[3] & 0x80)) 3849 nfingers = 4; 3850 mask = (1 << nfingers) - 1; 3851 3852 fn = ELANTECH_FINGER_SET_XYP(pb); 3853 if (sc->elanhw.haspressure) { 3854 fn.w = ((pb->ipacket[0] & 0x30) >> 2) | 3855 ((pb->ipacket[3] & 0x30) >> 4); 3856 } else { 3857 fn.p = PSM_FINGER_DEFAULT_P; 3858 fn.w = PSM_FINGER_DEFAULT_W; 3859 } 3860 3861 /* 3862 * HW v2 dont report exact finger positions when 3 or more 3863 * fingers are on touchpad. Use reported value as fingers 3864 * position as it is required for tap detection 3865 */ 3866 if (nfingers > 2) 3867 fn.flags = PSM_FINGER_FUZZY; 3868 3869 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++) 3870 f[id] = fn; 3871 break; 3872 3873 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */ 3874 /* 7 6 5 4 3 2 1 0 (LSB) 3875 * ------------------------------------------- 3876 * ipacket[0]: N1 N0 AY8 AX8 . . R L 3877 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0 3878 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0 3879 * ipacket[3]: . . BY8 BX8 . . . . 3880 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0 3881 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0 3882 * ------------------------------------------- 3883 * AX: lower-left finger absolute x value 3884 * AY: lower-left finger absolute y value 3885 * BX: upper-right finger absolute x value 3886 * BY: upper-right finger absolute y value 3887 */ 3888 nfingers = 2; 3889 mask = (1 << nfingers) - 1; 3890 3891 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++) 3892 f[id] = (finger_t) { 3893 .x = (((pb->ipacket[id * 3] & 0x10) << 4) | 3894 pb->ipacket[id * 3 + 1]) << 2, 3895 .y = (((pb->ipacket[id * 3] & 0x20) << 3) | 3896 pb->ipacket[id * 3 + 2]) << 2, 3897 .p = PSM_FINGER_DEFAULT_P, 3898 .w = PSM_FINGER_DEFAULT_W, 3899 /* HW ver.2 sends bounding box */ 3900 .flags = PSM_FINGER_FUZZY 3901 }; 3902 break; 3903 3904 case ELANTECH_PKT_V3: /* HW Version 3 */ 3905 /* 7 6 5 4 3 2 1 0 (LSB) 3906 * ------------------------------------------- 3907 * ipacket[0]: N1 N0 W3 W2 0 1 R L 3908 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 3909 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 3910 * ipacket[3]: 0 0 W1 W0 0 0 1 0 3911 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 3912 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3913 * ------------------------------------------- 3914 */ 3915 nfingers = (pb->ipacket[0] & 0xc0) >> 6; 3916 mask = (1 << nfingers) - 1; 3917 id = nfingers - 1; 3918 3919 fn = ELANTECH_FINGER_SET_XYP(pb); 3920 fn.w = ((pb->ipacket[0] & 0x30) >> 2) | 3921 ((pb->ipacket[3] & 0x30) >> 4); 3922 3923 /* 3924 * HW v3 dont report exact finger positions when 3 or more 3925 * fingers are on touchpad. Use reported value as fingers 3926 * position as it is required for tap detection 3927 */ 3928 if (nfingers > 1) 3929 fn.flags = PSM_FINGER_FUZZY; 3930 3931 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++) 3932 f[id] = fn; 3933 3934 if (nfingers == 2) { 3935 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) { 3936 sc->elanaction.fingers[0] = fn; 3937 return (0); 3938 } else 3939 f[0] = sc->elanaction.fingers[0]; 3940 } 3941 break; 3942 3943 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */ 3944 /* 7 6 5 4 3 2 1 0 (LSB) 3945 * ------------------------------------------- 3946 * ipacket[0]: . . . . 0 1 R L 3947 * ipacket[1]: . . . F4 F3 F2 F1 F0 3948 * ipacket[2]: . . . . . . . . 3949 * ipacket[3]: . . . 1 0 0 0 0 3950 * ipacket[4]: PL . . . . . . . 3951 * ipacket[5]: . . . . . . . . 3952 * ------------------------------------------- 3953 * Fn: finger n is on touchpad 3954 * PL: palm 3955 * HV ver4 sends a status packet to indicate that the numbers 3956 * or identities of the fingers has been changed 3957 */ 3958 3959 mask = pb->ipacket[1] & 0x1f; 3960 nfingers = bitcount32(mask); 3961 3962 /* Skip "new finger is on touchpad" packets */ 3963 if ((sc->elanaction.mask & mask) == sc->elanaction.mask && 3964 (mask & ~sc->elanaction.mask)) { 3965 sc->elanaction.mask = mask; 3966 return (0); 3967 } 3968 3969 break; 3970 3971 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */ 3972 /* 7 6 5 4 3 2 1 0 (LSB) 3973 * ------------------------------------------- 3974 * ipacket[0]: W3 W2 W1 W0 0 1 R L 3975 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 3976 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 3977 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1 3978 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 3979 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3980 * ------------------------------------------- 3981 * ID: finger id 3982 * HW ver 4 sends head packets in two cases: 3983 * 1. One finger touch and movement. 3984 * 2. Next after status packet to tell new finger positions. 3985 */ 3986 mask = sc->elanaction.mask; 3987 nfingers = bitcount32(mask); 3988 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1; 3989 3990 if (id >= 0 && id < ELANTECH_MAX_FINGERS) { 3991 f[id] = ELANTECH_FINGER_SET_XYP(pb); 3992 f[id].w = (pb->ipacket[0] & 0xf0) >> 4; 3993 } 3994 break; 3995 3996 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */ 3997 /* 7 6 5 4 3 2 1 0 (LSB) 3998 * ------------------------------------------- 3999 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L 4000 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0 4001 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0 4002 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0 4003 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0 4004 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0 4005 * ------------------------------------------- 4006 * OF: delta overflows (> 127 or < -128), in this case 4007 * firmware sends us (delta x / 5) and (delta y / 5) 4008 * ID: finger id 4009 * DX: delta x (two's complement) 4010 * XY: delta y (two's complement) 4011 * byte 0 ~ 2 for one finger 4012 * byte 3 ~ 5 for another finger 4013 */ 4014 mask = sc->elanaction.mask; 4015 nfingers = bitcount32(mask); 4016 4017 scale = (pb->ipacket[0] & 0x10) ? 5 : 1; 4018 for (i = 0; i <= 3; i += 3) { 4019 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1; 4020 if (id < 0 || id >= ELANTECH_MAX_FINGERS) 4021 continue; 4022 4023 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) { 4024 f[id] = sc->elanaction.fingers[id]; 4025 f[id].x += imax(-f[id].x, 4026 (signed char)pb->ipacket[i+1] * scale); 4027 f[id].y += imax(-f[id].y, 4028 (signed char)pb->ipacket[i+2] * scale); 4029 } else { 4030 VLOG(3, (LOG_DEBUG, "elantech: " 4031 "HW v4 motion packet skipped\n")); 4032 } 4033 } 4034 4035 break; 4036 4037 case ELANTECH_PKT_TRACKPOINT: 4038 /* 7 6 5 4 3 2 1 0 (LSB) 4039 * ------------------------------------------- 4040 * ipacket[0]: 0 0 SX SY 0 M R L 4041 * ipacket[1]: ~SX 0 0 0 0 0 0 0 4042 * ipacket[2]: ~SY 0 0 0 0 0 0 0 4043 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0 4044 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 4045 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 4046 * ------------------------------------------- 4047 * X and Y are written in two's complement spread 4048 * over 9 bits with SX/SY the relative top bit and 4049 * X7..X0 and Y7..Y0 the lower bits. 4050 */ 4051 *x = (pb->ipacket[0] & 0x20) ? 4052 pb->ipacket[4] - 256 : pb->ipacket[4]; 4053 *y = (pb->ipacket[0] & 0x10) ? 4054 pb->ipacket[5] - 256 : pb->ipacket[5]; 4055 4056 trackpoint_button = 4057 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) | 4058 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) | 4059 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0); 4060 4061 ms->button = touchpad_button | trackpoint_button; 4062 return (0); 4063 4064 case ELANTECH_PKT_NOP: 4065 return (0); 4066 4067 default: 4068 return (-1); 4069 } 4070 4071 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 4072 if (PSM_FINGER_IS_SET(f[id])) 4073 VLOG(2, (LOG_DEBUG, "elantech: " 4074 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1, 4075 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags)); 4076 4077 /* Touchpad button presses */ 4078 if (sc->elanhw.isclickpad) { 4079 touchpad_button = 4080 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0); 4081 } else { 4082 touchpad_button = 4083 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) | 4084 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0); 4085 } 4086 4087 ms->button = touchpad_button | trackpoint_button; 4088 4089 /* Palm detection doesn't terminate the current action. */ 4090 if (!psmpalmdetect(sc, &f[0], nfingers)) { 4091 /* Send finger 1 position to gesture processor */ 4092 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) || 4093 nfingers == 0) 4094 psmgestures(sc, &f[0], imin(nfingers, 3), ms); 4095 /* Send fingers positions to movement smoothers */ 4096 for (id = 0; id < PSM_FINGERS; id++) 4097 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id))) 4098 psmsmoother(sc, &f[id], id, ms, x, y); 4099 } else { 4100 VLOG(2, (LOG_DEBUG, "elantech: palm detected! (%d)\n", 4101 f[0].w)); 4102 } 4103 4104 /* Store current finger positions in action context */ 4105 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) { 4106 if (PSM_FINGER_IS_SET(f[id])) 4107 sc->elanaction.fingers[id] = f[id]; 4108 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id))) 4109 PSM_FINGER_RESET(sc->elanaction.fingers[id]); 4110 } 4111 sc->elanaction.mask = mask; 4112 4113 /* Use the extra buttons as a scrollwheel */ 4114 if (ms->button & MOUSE_BUTTON4DOWN) 4115 *z = -1; 4116 else if (ms->button & MOUSE_BUTTON5DOWN) 4117 *z = 1; 4118 else if (ms->button & MOUSE_BUTTON6DOWN) 4119 *z = -2; 4120 else if (ms->button & MOUSE_BUTTON7DOWN) 4121 *z = 2; 4122 else 4123 *z = 0; 4124 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN | 4125 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN); 4126 4127 return (0); 4128 } 4129 4130 static void 4131 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 4132 int *x, int *y, int *z) 4133 { 4134 static int butmap_versapad[8] = { 4135 0, 4136 MOUSE_BUTTON3DOWN, 4137 0, 4138 MOUSE_BUTTON3DOWN, 4139 MOUSE_BUTTON1DOWN, 4140 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 4141 MOUSE_BUTTON1DOWN, 4142 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 4143 }; 4144 int c, x0, y0; 4145 4146 /* VersaPad PS/2 absolute mode message format 4147 * 4148 * [packet1] 7 6 5 4 3 2 1 0(LSB) 4149 * ipacket[0]: 1 1 0 A 1 L T R 4150 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 4151 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 4152 * ipacket[3]: 1 1 1 A 1 L T R 4153 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 4154 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 4155 * 4156 * [note] 4157 * R: right physical mouse button (1=on) 4158 * T: touch pad virtual button (1=tapping) 4159 * L: left physical mouse button (1=on) 4160 * A: position data is valid (1=valid) 4161 * H: horizontal data (12bit signed integer. H11 is sign bit.) 4162 * V: vertical data (12bit signed integer. V11 is sign bit.) 4163 * P: pressure data 4164 * 4165 * Tapping is mapped to MOUSE_BUTTON4. 4166 */ 4167 c = pb->ipacket[0]; 4168 *x = *y = 0; 4169 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 4170 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 4171 if (c & MOUSE_PS2VERSA_IN_USE) { 4172 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8); 4173 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4); 4174 if (x0 & 0x800) 4175 x0 -= 0x1000; 4176 if (y0 & 0x800) 4177 y0 -= 0x1000; 4178 if (sc->flags & PSM_FLAGS_FINGERDOWN) { 4179 *x = sc->xold - x0; 4180 *y = y0 - sc->yold; 4181 if (*x < 0) /* XXX */ 4182 ++*x; 4183 else if (*x) 4184 --*x; 4185 if (*y < 0) 4186 ++*y; 4187 else if (*y) 4188 --*y; 4189 } else 4190 sc->flags |= PSM_FLAGS_FINGERDOWN; 4191 sc->xold = x0; 4192 sc->yold = y0; 4193 } else 4194 sc->flags &= ~PSM_FLAGS_FINGERDOWN; 4195 } 4196 4197 static void 4198 psmsoftintridle(void *arg) 4199 { 4200 struct psm_softc *sc = arg; 4201 packetbuf_t *pb; 4202 4203 /* Invoke soft handler only when pqueue is empty. Otherwise it will be 4204 * invoked from psmintr soon with pqueue filled with real data */ 4205 if (sc->pqueue_start == sc->pqueue_end && 4206 sc->idlepacket.inputbytes > 0) { 4207 /* Grow circular queue backwards to avoid race with psmintr */ 4208 if (--sc->pqueue_start < 0) 4209 sc->pqueue_start = PSM_PACKETQUEUE - 1; 4210 4211 pb = &sc->pqueue[sc->pqueue_start]; 4212 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t)); 4213 VLOG(4, (LOG_DEBUG, 4214 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n", 4215 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 4216 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5])); 4217 4218 psmsoftintr(arg); 4219 } 4220 } 4221 4222 4223 static void 4224 psmsoftintr(void *arg) 4225 { 4226 /* 4227 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 4228 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 4229 */ 4230 static int butmap[8] = { 4231 0, 4232 MOUSE_BUTTON1DOWN, 4233 MOUSE_BUTTON3DOWN, 4234 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 4235 MOUSE_BUTTON2DOWN, 4236 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 4237 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 4238 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 4239 }; 4240 struct psm_softc *sc = arg; 4241 mousestatus_t ms; 4242 packetbuf_t *pb; 4243 int x, y, z, c, l; 4244 4245 getmicrouptime(&sc->lastsoftintr); 4246 4247 crit_enter(); 4248 4249 do { 4250 pb = &sc->pqueue[sc->pqueue_start]; 4251 4252 if (sc->mode.level == PSM_LEVEL_NATIVE) 4253 goto next_native; 4254 4255 c = pb->ipacket[0]; 4256 /* 4257 * A kludge for Kensington device! 4258 * The MSB of the horizontal count appears to be stored in 4259 * a strange place. 4260 */ 4261 if (sc->hw.model == MOUSE_MODEL_THINK) 4262 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 4263 4264 /* ignore the overflow bits... */ 4265 x = (c & MOUSE_PS2_XNEG) ? 4266 pb->ipacket[1] - 256 : pb->ipacket[1]; 4267 y = (c & MOUSE_PS2_YNEG) ? 4268 pb->ipacket[2] - 256 : pb->ipacket[2]; 4269 z = 0; 4270 ms.obutton = sc->button; /* previous button state */ 4271 ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 4272 /* `tapping' action */ 4273 if (sc->config & PSM_CONFIG_FORCETAP) 4274 ms.button |= ((c & MOUSE_PS2_TAP)) ? 4275 0 : MOUSE_BUTTON4DOWN; 4276 timevalclear(&sc->idletimeout); 4277 sc->idlepacket.inputbytes = 0; 4278 4279 switch (sc->hw.model) { 4280 4281 case MOUSE_MODEL_EXPLORER: 4282 /* 4283 * b7 b6 b5 b4 b3 b2 b1 b0 4284 * byte 1: oy ox sy sx 1 M R L 4285 * byte 2: x x x x x x x x 4286 * byte 3: y y y y y y y y 4287 * byte 4: * * S2 S1 s d2 d1 d0 4288 * 4289 * L, M, R, S1, S2: left, middle, right and side buttons 4290 * s: wheel data sign bit 4291 * d2-d0: wheel data 4292 */ 4293 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ? 4294 (pb->ipacket[3] & 0x0f) - 16 : 4295 (pb->ipacket[3] & 0x0f); 4296 ms.button |= 4297 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ? 4298 MOUSE_BUTTON4DOWN : 0; 4299 ms.button |= 4300 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ? 4301 MOUSE_BUTTON5DOWN : 0; 4302 break; 4303 4304 case MOUSE_MODEL_INTELLI: 4305 case MOUSE_MODEL_NET: 4306 /* wheel data is in the fourth byte */ 4307 z = (char)pb->ipacket[3]; 4308 /* 4309 * XXX some mice may send 7 when there is no Z movement? */ 4310 if ((z >= 7) || (z <= -7)) 4311 z = 0; 4312 /* some compatible mice have additional buttons */ 4313 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ? 4314 MOUSE_BUTTON4DOWN : 0; 4315 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ? 4316 MOUSE_BUTTON5DOWN : 0; 4317 break; 4318 4319 case MOUSE_MODEL_MOUSEMANPLUS: 4320 proc_mmanplus(sc, pb, &ms, &x, &y, &z); 4321 break; 4322 4323 case MOUSE_MODEL_GLIDEPOINT: 4324 /* `tapping' action */ 4325 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : 4326 MOUSE_BUTTON4DOWN; 4327 break; 4328 4329 case MOUSE_MODEL_NETSCROLL: 4330 /* 4331 * three addtional bytes encode buttons and 4332 * wheel events 4333 */ 4334 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ? 4335 MOUSE_BUTTON4DOWN : 0; 4336 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ? 4337 MOUSE_BUTTON5DOWN : 0; 4338 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ? 4339 pb->ipacket[4] - 256 : pb->ipacket[4]; 4340 break; 4341 4342 case MOUSE_MODEL_THINK: 4343 /* the fourth button state in the first byte */ 4344 ms.button |= (c & MOUSE_PS2_TAP) ? 4345 MOUSE_BUTTON4DOWN : 0; 4346 break; 4347 4348 case MOUSE_MODEL_VERSAPAD: 4349 proc_versapad(sc, pb, &ms, &x, &y, &z); 4350 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) | 4351 ((y < 0) ? MOUSE_PS2_YNEG : 0); 4352 break; 4353 4354 case MOUSE_MODEL_4D: 4355 /* 4356 * b7 b6 b5 b4 b3 b2 b1 b0 4357 * byte 1: s2 d2 s1 d1 1 M R L 4358 * byte 2: sx x x x x x x x 4359 * byte 3: sy y y y y y y y 4360 * 4361 * s1: wheel 1 direction 4362 * d1: wheel 1 data 4363 * s2: wheel 2 direction 4364 * d2: wheel 2 data 4365 */ 4366 x = (pb->ipacket[1] & 0x80) ? 4367 pb->ipacket[1] - 256 : pb->ipacket[1]; 4368 y = (pb->ipacket[2] & 0x80) ? 4369 pb->ipacket[2] - 256 : pb->ipacket[2]; 4370 switch (c & MOUSE_4D_WHEELBITS) { 4371 case 0x10: 4372 z = 1; 4373 break; 4374 case 0x30: 4375 z = -1; 4376 break; 4377 case 0x40: /* XXX 2nd wheel turning right */ 4378 z = 2; 4379 break; 4380 case 0xc0: /* XXX 2nd wheel turning left */ 4381 z = -2; 4382 break; 4383 } 4384 break; 4385 4386 case MOUSE_MODEL_4DPLUS: 4387 if ((x < 16 - 256) && (y < 16 - 256)) { 4388 /* 4389 * b7 b6 b5 b4 b3 b2 b1 b0 4390 * byte 1: 0 0 1 1 1 M R L 4391 * byte 2: 0 0 0 0 1 0 0 0 4392 * byte 3: 0 0 0 0 S s d1 d0 4393 * 4394 * L, M, R, S: left, middle, right, 4395 * and side buttons 4396 * s: wheel data sign bit 4397 * d1-d0: wheel data 4398 */ 4399 x = y = 0; 4400 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 4401 ms.button |= MOUSE_BUTTON4DOWN; 4402 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ? 4403 ((pb->ipacket[2] & 0x07) - 8) : 4404 (pb->ipacket[2] & 0x07) ; 4405 } else { 4406 /* preserve previous button states */ 4407 ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 4408 } 4409 break; 4410 4411 case MOUSE_MODEL_SYNAPTICS: 4412 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) 4413 goto next; 4414 break; 4415 4416 case MOUSE_MODEL_ELANTECH: 4417 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) 4418 goto next; 4419 break; 4420 4421 case MOUSE_MODEL_TRACKPOINT: 4422 case MOUSE_MODEL_GENERIC: 4423 default: 4424 break; 4425 } 4426 4427 /* scale values */ 4428 if (sc->mode.accelfactor >= 1) { 4429 if (x != 0) { 4430 x = x * x / sc->mode.accelfactor; 4431 if (x == 0) 4432 x = 1; 4433 if (c & MOUSE_PS2_XNEG) 4434 x = -x; 4435 } 4436 if (y != 0) { 4437 y = y * y / sc->mode.accelfactor; 4438 if (y == 0) 4439 y = 1; 4440 if (c & MOUSE_PS2_YNEG) 4441 y = -y; 4442 } 4443 } 4444 /* Store last packet for reinjection if it has not been set already */ 4445 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0) 4446 sc->idlepacket = *pb; 4447 4448 ms.dx = x; 4449 ms.dy = y; 4450 ms.dz = z; 4451 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) | 4452 (ms.obutton ^ ms.button); 4453 4454 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket); 4455 4456 sc->status.flags |= ms.flags; 4457 sc->status.dx += ms.dx; 4458 sc->status.dy += ms.dy; 4459 sc->status.dz += ms.dz; 4460 sc->status.button = ms.button; 4461 sc->button = ms.button; 4462 4463 next_native: 4464 sc->watchdog = FALSE; 4465 4466 /* queue data */ 4467 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) { 4468 l = imin(pb->inputbytes, 4469 sizeof(sc->queue.buf) - sc->queue.tail); 4470 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 4471 if (pb->inputbytes > l) 4472 bcopy(&pb->ipacket[l], &sc->queue.buf[0], 4473 pb->inputbytes - l); 4474 sc->queue.tail = (sc->queue.tail + pb->inputbytes) % 4475 sizeof(sc->queue.buf); 4476 sc->queue.count += pb->inputbytes; 4477 } 4478 pb->inputbytes = 0; 4479 4480 next: 4481 if (++sc->pqueue_start >= PSM_PACKETQUEUE) 4482 sc->pqueue_start = 0; 4483 } while (sc->pqueue_start != sc->pqueue_end); 4484 4485 if (sc->state & PSM_ASLP) { 4486 sc->state &= ~PSM_ASLP; 4487 wakeup(sc); 4488 } 4489 4490 KNOTE(&sc->rkq.ki_note, 0); 4491 sc->state &= ~PSM_SOFTARMED; 4492 4493 /* schedule injection of predefined packet after idletimeout 4494 * if no data packets have been received from psmintr */ 4495 if (timevalisset(&sc->idletimeout)) { 4496 sc->state |= PSM_SOFTARMED; 4497 callout_reset(&sc->softcallout, tvtohz_high(&sc->idletimeout), 4498 psmsoftintridle, sc); 4499 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n", 4500 tvtohz_high(&sc->idletimeout))); 4501 } 4502 crit_exit(); 4503 } 4504 4505 static struct filterops psmfiltops = 4506 { FILTEROP_ISFD, NULL, psmfilter_detach, psmfilter }; 4507 4508 static int 4509 psmkqfilter(struct dev_kqfilter_args *ap) 4510 { 4511 cdev_t dev = ap->a_head.a_dev; 4512 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev)); 4513 struct knote *kn = ap->a_kn; 4514 struct klist *klist; 4515 4516 ap->a_result = 0; 4517 4518 switch (kn->kn_filter) { 4519 case EVFILT_READ: 4520 kn->kn_fop = &psmfiltops; 4521 kn->kn_hook = (caddr_t)sc; 4522 break; 4523 default: 4524 ap->a_result = EOPNOTSUPP; 4525 return (0); 4526 } 4527 4528 klist = &sc->rkq.ki_note; 4529 knote_insert(klist, kn); 4530 4531 return (0); 4532 } 4533 4534 static void 4535 psmfilter_detach(struct knote *kn) 4536 { 4537 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook; 4538 struct klist *klist; 4539 4540 klist = &sc->rkq.ki_note; 4541 knote_remove(klist, kn); 4542 } 4543 4544 static int 4545 psmfilter(struct knote *kn, long hint) 4546 { 4547 struct psm_softc *sc = (struct psm_softc *)kn->kn_hook; 4548 int ready = 0; 4549 4550 crit_enter(); 4551 if (sc->queue.count > 0) 4552 ready = 1; 4553 crit_exit(); 4554 4555 return (ready); 4556 } 4557 4558 4559 #if 0 4560 static int 4561 psmpoll(struct dev_open_args *ap) 4562 { 4563 cdev_t dev = ap->a_head.a_dev; 4564 struct psm_softc *sc = dev->si_drv1; 4565 int revents = 0; 4566 4567 /* Return true if a mouse event available */ 4568 crit_enter(); 4569 if (events & (POLLIN | POLLRDNORM)) { 4570 if (sc->queue.count > 0) 4571 revents |= events & (POLLIN | POLLRDNORM); 4572 } 4573 crit_exit(); 4574 4575 return (revents); 4576 } 4577 #endif 4578 4579 /* vendor/model specific routines */ 4580 4581 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 4582 { 4583 4584 if (set_mouse_resolution(kbdc, res) != res) 4585 return (FALSE); 4586 if (set_mouse_scaling(kbdc, scale) && 4587 set_mouse_scaling(kbdc, scale) && 4588 set_mouse_scaling(kbdc, scale) && 4589 (get_mouse_status(kbdc, status, 0, 3) >= 3)) 4590 return (TRUE); 4591 return (FALSE); 4592 } 4593 4594 static int 4595 mouse_ext_command(KBDC kbdc, int command) 4596 { 4597 4598 int c; 4599 4600 c = (command >> 6) & 0x03; 4601 if (set_mouse_resolution(kbdc, c) != c) 4602 return (FALSE); 4603 c = (command >> 4) & 0x03; 4604 if (set_mouse_resolution(kbdc, c) != c) 4605 return (FALSE); 4606 c = (command >> 2) & 0x03; 4607 if (set_mouse_resolution(kbdc, c) != c) 4608 return (FALSE); 4609 c = (command >> 0) & 0x03; 4610 if (set_mouse_resolution(kbdc, c) != c) 4611 return (FALSE); 4612 return (TRUE); 4613 } 4614 4615 #ifdef notyet 4616 /* Logitech MouseMan Cordless II */ 4617 static int 4618 enable_lcordless(struct psm_softc *sc, enum probearg arg) 4619 { 4620 KBDC kbdc = sc->kbdc; 4621 int status[3]; 4622 int ch; 4623 4624 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status)) 4625 return (FALSE); 4626 if (status[1] == PSMD_RES_HIGH) 4627 return (FALSE); 4628 ch = (status[0] & 0x07) - 1; /* channel # */ 4629 if ((ch <= 0) || (ch > 4)) 4630 return (FALSE); 4631 /* 4632 * status[1]: always one? 4633 * status[2]: battery status? (0-100) 4634 */ 4635 return (TRUE); 4636 } 4637 #endif /* notyet */ 4638 4639 4640 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 4641 static int 4642 enable_groller(struct psm_softc *sc, enum probearg arg) 4643 { 4644 KBDC kbdc = sc->kbdc; 4645 int status[3]; 4646 4647 /* 4648 * The special sequence to enable the fourth button and the 4649 * roller. Immediately after this sequence check status bytes. 4650 * if the mouse is NetScroll, the second and the third bytes are 4651 * '3' and 'D'. 4652 */ 4653 4654 /* 4655 * If the mouse is an ordinary PS/2 mouse, the status bytes should 4656 * look like the following. 4657 * 4658 * byte 1 bit 7 always 0 4659 * bit 6 stream mode (0) 4660 * bit 5 disabled (0) 4661 * bit 4 1:1 scaling (0) 4662 * bit 3 always 0 4663 * bit 0-2 button status 4664 * byte 2 resolution (PSMD_RES_HIGH) 4665 * byte 3 report rate (?) 4666 */ 4667 4668 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status)) 4669 return (FALSE); 4670 if ((status[1] != '3') || (status[2] != 'D')) 4671 return (FALSE); 4672 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 4673 if (arg == PROBE) 4674 sc->hw.buttons = 4; 4675 return (TRUE); 4676 } 4677 4678 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 4679 static int 4680 enable_gmouse(struct psm_softc *sc, enum probearg arg) 4681 { 4682 KBDC kbdc = sc->kbdc; 4683 int status[3]; 4684 4685 /* 4686 * The special sequence to enable the middle, "rubber" button. 4687 * Immediately after this sequence check status bytes. 4688 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 4689 * the second and the third bytes are '3' and 'U'. 4690 * NOTE: NetMouse reports that it has three buttons although it has 4691 * two buttons and a rubber button. NetMouse Pro and MIE Mouse 4692 * say they have three buttons too and they do have a button on the 4693 * side... 4694 */ 4695 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status)) 4696 return (FALSE); 4697 if ((status[1] != '3') || (status[2] != 'U')) 4698 return (FALSE); 4699 return (TRUE); 4700 } 4701 4702 /* ALPS GlidePoint */ 4703 static int 4704 enable_aglide(struct psm_softc *sc, enum probearg arg) 4705 { 4706 KBDC kbdc = sc->kbdc; 4707 int status[3]; 4708 4709 /* 4710 * The special sequence to obtain ALPS GlidePoint specific 4711 * information. Immediately after this sequence, status bytes will 4712 * contain something interesting. 4713 * NOTE: ALPS produces several models of GlidePoint. Some of those 4714 * do not respond to this sequence, thus, cannot be detected this way. 4715 */ 4716 if (set_mouse_sampling_rate(kbdc, 100) != 100) 4717 return (FALSE); 4718 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status)) 4719 return (FALSE); 4720 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 4721 return (FALSE); 4722 return (TRUE); 4723 } 4724 4725 /* Kensington ThinkingMouse/Trackball */ 4726 static int 4727 enable_kmouse(struct psm_softc *sc, enum probearg arg) 4728 { 4729 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 4730 KBDC kbdc = sc->kbdc; 4731 int status[3]; 4732 int id1; 4733 int id2; 4734 int i; 4735 4736 id1 = get_aux_id(kbdc); 4737 if (set_mouse_sampling_rate(kbdc, 10) != 10) 4738 return (FALSE); 4739 /* 4740 * The device is now in the native mode? It returns a different 4741 * ID value... 4742 */ 4743 id2 = get_aux_id(kbdc); 4744 if ((id1 == id2) || (id2 != 2)) 4745 return (FALSE); 4746 4747 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 4748 return (FALSE); 4749 #if PSM_DEBUG >= 2 4750 /* at this point, resolution is LOW, sampling rate is 10/sec */ 4751 if (get_mouse_status(kbdc, status, 0, 3) < 3) 4752 return (FALSE); 4753 #endif 4754 4755 /* 4756 * The special sequence to enable the third and fourth buttons. 4757 * Otherwise they behave like the first and second buttons. 4758 */ 4759 for (i = 0; i < nitems(rate); ++i) 4760 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4761 return (FALSE); 4762 4763 /* 4764 * At this point, the device is using default resolution and 4765 * sampling rate for the native mode. 4766 */ 4767 if (get_mouse_status(kbdc, status, 0, 3) < 3) 4768 return (FALSE); 4769 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 4770 return (FALSE); 4771 4772 /* the device appears be enabled by this sequence, diable it for now */ 4773 disable_aux_dev(kbdc); 4774 empty_aux_buffer(kbdc, 5); 4775 4776 return (TRUE); 4777 } 4778 4779 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 4780 static int 4781 enable_mmanplus(struct psm_softc *sc, enum probearg arg) 4782 { 4783 KBDC kbdc = sc->kbdc; 4784 int data[3]; 4785 4786 /* the special sequence to enable the fourth button and the roller. */ 4787 /* 4788 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 4789 * must be called exactly three times since the last RESET command 4790 * before this sequence. XXX 4791 */ 4792 if (!set_mouse_scaling(kbdc, 1)) 4793 return (FALSE); 4794 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 4795 return (FALSE); 4796 if (get_mouse_status(kbdc, data, 1, 3) < 3) 4797 return (FALSE); 4798 4799 /* 4800 * PS2++ protocol, packet type 0 4801 * 4802 * b7 b6 b5 b4 b3 b2 b1 b0 4803 * byte 1: * 1 p3 p2 1 * * * 4804 * byte 2: 1 1 p1 p0 m1 m0 1 0 4805 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 4806 * 4807 * p3-p0: packet type: 0 4808 * m7-m0: model ID: MouseMan+:0x50, 4809 * FirstMouse+:0x51, 4810 * ScrollPoint:0x58... 4811 */ 4812 /* check constant bits */ 4813 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 4814 return (FALSE); 4815 if ((data[1] & 0xc3) != 0xc2) 4816 return (FALSE); 4817 /* check d3-d0 in byte 2 */ 4818 if (!MOUSE_PS2PLUS_CHECKBITS(data)) 4819 return (FALSE); 4820 /* check p3-p0 */ 4821 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 4822 return (FALSE); 4823 4824 if (arg == PROBE) { 4825 sc->hw.hwid &= 0x00ff; 4826 sc->hw.hwid |= data[2] << 8; /* save model ID */ 4827 } 4828 4829 /* 4830 * MouseMan+ (or FirstMouse+) is now in its native mode, in which 4831 * the wheel and the fourth button events are encoded in the 4832 * special data packet. The mouse may be put in the IntelliMouse mode 4833 * if it is initialized by the IntelliMouse's method. 4834 */ 4835 return (TRUE); 4836 } 4837 4838 /* MS IntelliMouse Explorer */ 4839 static int 4840 enable_msexplorer(struct psm_softc *sc, enum probearg arg) 4841 { 4842 KBDC kbdc = sc->kbdc; 4843 static u_char rate0[] = { 200, 100, 80, }; 4844 static u_char rate1[] = { 200, 200, 80, }; 4845 int id; 4846 int i; 4847 4848 /* 4849 * This is needed for at least A4Tech X-7xx mice - they do not go 4850 * straight to Explorer mode, but need to be set to Intelli mode 4851 * first. 4852 */ 4853 enable_msintelli(sc, arg); 4854 4855 /* the special sequence to enable the extra buttons and the roller. */ 4856 for (i = 0; i < nitems(rate1); ++i) 4857 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 4858 return (FALSE); 4859 /* the device will give the genuine ID only after the above sequence */ 4860 id = get_aux_id(kbdc); 4861 if (id != PSM_EXPLORER_ID) 4862 return (FALSE); 4863 4864 if (arg == PROBE) { 4865 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 4866 sc->hw.hwid = id; 4867 } 4868 4869 /* 4870 * XXX: this is a kludge to fool some KVM switch products 4871 * which think they are clever enough to know the 4-byte IntelliMouse 4872 * protocol, and assume any other protocols use 3-byte packets. 4873 * They don't convey 4-byte data packets from the IntelliMouse Explorer 4874 * correctly to the host computer because of this! 4875 * The following sequence is actually IntelliMouse's "wake up" 4876 * sequence; it will make the KVM think the mouse is IntelliMouse 4877 * when it is in fact IntelliMouse Explorer. 4878 */ 4879 for (i = 0; i < nitems(rate0); ++i) 4880 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 4881 break; 4882 get_aux_id(kbdc); 4883 4884 return (TRUE); 4885 } 4886 4887 /* 4888 * MS IntelliMouse 4889 * Logitech MouseMan+ and FirstMouse+ will also respond to this 4890 * probe routine and act like IntelliMouse. 4891 */ 4892 static int 4893 enable_msintelli(struct psm_softc *sc, enum probearg arg) 4894 { 4895 KBDC kbdc = sc->kbdc; 4896 static u_char rate[] = { 200, 100, 80, }; 4897 int id; 4898 int i; 4899 4900 /* the special sequence to enable the third button and the roller. */ 4901 for (i = 0; i < nitems(rate); ++i) 4902 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4903 return (FALSE); 4904 /* the device will give the genuine ID only after the above sequence */ 4905 id = get_aux_id(kbdc); 4906 if (id != PSM_INTELLI_ID) 4907 return (FALSE); 4908 4909 if (arg == PROBE) { 4910 sc->hw.buttons = 3; 4911 sc->hw.hwid = id; 4912 } 4913 4914 return (TRUE); 4915 } 4916 4917 /* 4918 * A4 Tech 4D Mouse 4919 * Newer wheel mice from A4 Tech may use the 4D+ protocol. 4920 */ 4921 static int 4922 enable_4dmouse(struct psm_softc *sc, enum probearg arg) 4923 { 4924 static u_char rate[] = { 200, 100, 80, 60, 40, 20 }; 4925 KBDC kbdc = sc->kbdc; 4926 int id; 4927 int i; 4928 4929 for (i = 0; i < nitems(rate); ++i) 4930 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4931 return (FALSE); 4932 id = get_aux_id(kbdc); 4933 /* 4934 * WinEasy 4D, 4 Way Scroll 4D: 6 4935 * Cable-Free 4D: 8 (4DPLUS) 4936 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 4937 */ 4938 if (id != PSM_4DMOUSE_ID) 4939 return (FALSE); 4940 4941 if (arg == PROBE) { 4942 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 4943 sc->hw.hwid = id; 4944 } 4945 4946 return (TRUE); 4947 } 4948 4949 /* 4950 * A4 Tech 4D+ Mouse 4951 * Newer wheel mice from A4 Tech seem to use this protocol. 4952 * Older models are recognized as either 4D Mouse or IntelliMouse. 4953 */ 4954 static int 4955 enable_4dplus(struct psm_softc *sc, enum probearg arg) 4956 { 4957 KBDC kbdc = sc->kbdc; 4958 int id; 4959 4960 /* 4961 * enable_4dmouse() already issued the following ID sequence... 4962 static u_char rate[] = { 200, 100, 80, 60, 40, 20 }; 4963 int i; 4964 4965 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) 4966 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4967 return (FALSE); 4968 */ 4969 4970 id = get_aux_id(kbdc); 4971 switch (id) { 4972 case PSM_4DPLUS_ID: 4973 break; 4974 case PSM_4DPLUS_RFSW35_ID: 4975 break; 4976 default: 4977 return (FALSE); 4978 } 4979 4980 if (arg == PROBE) { 4981 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3; 4982 sc->hw.hwid = id; 4983 } 4984 4985 return (TRUE); 4986 } 4987 4988 /* Synaptics Touchpad */ 4989 static int 4990 synaptics_sysctl(SYSCTL_HANDLER_ARGS) 4991 { 4992 struct psm_softc *sc; 4993 int error, arg; 4994 4995 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 || 4996 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X) 4997 return (EINVAL); 4998 4999 sc = oidp->oid_arg1; 5000 5001 /* Read the current value. */ 5002 arg = *(int *)((char *)sc + oidp->oid_arg2); 5003 error = sysctl_handle_int(oidp, &arg, 0, req); 5004 5005 /* Sanity check. */ 5006 if (error || !req->newptr) 5007 return (error); 5008 5009 /* 5010 * Check that the new value is in the concerned node's range 5011 * of values. 5012 */ 5013 switch (oidp->oid_arg2) { 5014 case SYNAPTICS_SYSCTL_MIN_PRESSURE: 5015 case SYNAPTICS_SYSCTL_MAX_PRESSURE: 5016 if (arg < 0 || arg > 255) 5017 return (EINVAL); 5018 break; 5019 case SYNAPTICS_SYSCTL_MAX_WIDTH: 5020 if (arg < 4 || arg > 15) 5021 return (EINVAL); 5022 break; 5023 case SYNAPTICS_SYSCTL_MARGIN_TOP: 5024 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM: 5025 case SYNAPTICS_SYSCTL_NA_TOP: 5026 case SYNAPTICS_SYSCTL_NA_BOTTOM: 5027 if (arg < 0 || arg > sc->synhw.maximumYCoord) 5028 return (EINVAL); 5029 break; 5030 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X: 5031 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X: 5032 /* Softbuttons is clickpad only feature */ 5033 if (!sc->synhw.capClickPad && arg != 0) 5034 return (EINVAL); 5035 /* FALLTHROUGH */ 5036 case SYNAPTICS_SYSCTL_MARGIN_RIGHT: 5037 case SYNAPTICS_SYSCTL_MARGIN_LEFT: 5038 case SYNAPTICS_SYSCTL_NA_RIGHT: 5039 case SYNAPTICS_SYSCTL_NA_LEFT: 5040 if (arg < 0 || arg > sc->synhw.maximumXCoord) 5041 return (EINVAL); 5042 break; 5043 case SYNAPTICS_SYSCTL_WINDOW_MIN: 5044 case SYNAPTICS_SYSCTL_WINDOW_MAX: 5045 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE: 5046 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE) 5047 return (EINVAL); 5048 break; 5049 case SYNAPTICS_SYSCTL_MULTIPLICATOR: 5050 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT: 5051 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS: 5052 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA: 5053 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED: 5054 case SYNAPTICS_SYSCTL_DIV_MIN: 5055 case SYNAPTICS_SYSCTL_DIV_MAX: 5056 case SYNAPTICS_SYSCTL_DIV_MAX_NA: 5057 case SYNAPTICS_SYSCTL_DIV_LEN: 5058 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN: 5059 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX: 5060 if (arg < 1) 5061 return (EINVAL); 5062 break; 5063 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA: 5064 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT: 5065 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA: 5066 if (arg < 0) 5067 return (EINVAL); 5068 break; 5069 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA: 5070 if (arg < -sc->synhw.maximumXCoord || 5071 arg > sc->synhw.maximumXCoord) 5072 return (EINVAL); 5073 break; 5074 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y: 5075 /* Softbuttons is clickpad only feature */ 5076 if (!sc->synhw.capClickPad && arg != 0) 5077 return (EINVAL); 5078 /* FALLTHROUGH */ 5079 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA: 5080 if (arg < -sc->synhw.maximumYCoord || 5081 arg > sc->synhw.maximumYCoord) 5082 return (EINVAL); 5083 break; 5084 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF: 5085 if (arg < 0 || arg > 1) 5086 return (EINVAL); 5087 break; 5088 default: 5089 return (EINVAL); 5090 } 5091 5092 /* Update. */ 5093 *(int *)((char *)sc + oidp->oid_arg2) = arg; 5094 5095 return (error); 5096 } 5097 5098 static void 5099 synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc) 5100 { 5101 /* 5102 * Set predefined sizes for softbuttons. 5103 * Values are taken to match HP Pavilion dv6 clickpad drawings 5104 * with thin middle softbutton placed on separator 5105 */ 5106 5107 /* hw.psm.synaptics.softbuttons_y */ 5108 sc->syninfo.softbuttons_y = 1700; 5109 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5110 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5111 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5112 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y, 5113 synaptics_sysctl, "I", 5114 "Vertical size of softbuttons area"); 5115 5116 /* hw.psm.synaptics.softbutton2_x */ 5117 sc->syninfo.softbutton2_x = 3100; 5118 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5119 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5120 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5121 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X, 5122 synaptics_sysctl, "I", 5123 "Horisontal position of 2-nd softbutton left edge (0-disable)"); 5124 5125 /* hw.psm.synaptics.softbutton3_x */ 5126 sc->syninfo.softbutton3_x = 3900; 5127 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5128 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5129 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5130 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X, 5131 synaptics_sysctl, "I", 5132 "Horisontal position of 3-rd softbutton left edge (0-disable)"); 5133 } 5134 5135 static void 5136 synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name, 5137 const char *descr) 5138 { 5139 5140 if (sc->syninfo.sysctl_tree != NULL) 5141 return; 5142 5143 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */ 5144 sysctl_ctx_init(&sc->syninfo.sysctl_ctx); 5145 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx, 5146 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD, 5147 0, descr); 5148 5149 /* hw.psm.synaptics.directional_scrolls. */ 5150 sc->syninfo.directional_scrolls = 0; 5151 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5152 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5153 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY, 5154 &sc->syninfo.directional_scrolls, 0, 5155 "Enable hardware scrolling pad (if non-zero) or register it as " 5156 "extended buttons (if 0)"); 5157 5158 /* hw.psm.synaptics.max_x. */ 5159 sc->syninfo.max_x = 6143; 5160 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5161 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5162 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY, 5163 &sc->syninfo.max_x, 0, 5164 "Horizontal reporting range"); 5165 5166 /* hw.psm.synaptics.max_y. */ 5167 sc->syninfo.max_y = 6143; 5168 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5169 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5170 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY, 5171 &sc->syninfo.max_y, 0, 5172 "Vertical reporting range"); 5173 5174 /* 5175 * Turn off two finger scroll if we have a 5176 * physical area reserved for scrolling or when 5177 * there's no multi finger support. 5178 */ 5179 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 && 5180 sc->synhw.capAdvancedGestures == 0)) 5181 sc->syninfo.two_finger_scroll = 0; 5182 else 5183 sc->syninfo.two_finger_scroll = 1; 5184 /* hw.psm.synaptics.two_finger_scroll. */ 5185 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5186 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5187 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY, 5188 &sc->syninfo.two_finger_scroll, 0, 5189 "Enable two finger scrolling"); 5190 5191 /* hw.psm.synaptics.min_pressure. */ 5192 sc->syninfo.min_pressure = 16; 5193 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5194 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5195 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5196 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE, 5197 synaptics_sysctl, "I", 5198 "Minimum pressure required to start an action"); 5199 5200 /* hw.psm.synaptics.max_pressure. */ 5201 sc->syninfo.max_pressure = 220; 5202 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5203 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5204 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5205 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE, 5206 synaptics_sysctl, "I", 5207 "Maximum pressure to detect palm"); 5208 5209 /* hw.psm.synaptics.max_width. */ 5210 sc->syninfo.max_width = 10; 5211 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5212 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5213 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5214 sc, SYNAPTICS_SYSCTL_MAX_WIDTH, 5215 synaptics_sysctl, "I", 5216 "Maximum finger width to detect palm"); 5217 5218 /* hw.psm.synaptics.top_margin. */ 5219 sc->syninfo.margin_top = 200; 5220 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5221 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5222 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5223 sc, SYNAPTICS_SYSCTL_MARGIN_TOP, 5224 synaptics_sysctl, "I", 5225 "Top margin"); 5226 5227 /* hw.psm.synaptics.right_margin. */ 5228 sc->syninfo.margin_right = 200; 5229 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5230 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5231 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5232 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT, 5233 synaptics_sysctl, "I", 5234 "Right margin"); 5235 5236 /* hw.psm.synaptics.bottom_margin. */ 5237 sc->syninfo.margin_bottom = 200; 5238 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5239 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5240 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5241 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM, 5242 synaptics_sysctl, "I", 5243 "Bottom margin"); 5244 5245 /* hw.psm.synaptics.left_margin. */ 5246 sc->syninfo.margin_left = 200; 5247 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5248 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5249 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5250 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT, 5251 synaptics_sysctl, "I", 5252 "Left margin"); 5253 5254 /* hw.psm.synaptics.na_top. */ 5255 sc->syninfo.na_top = 1783; 5256 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5257 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5258 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5259 sc, SYNAPTICS_SYSCTL_NA_TOP, 5260 synaptics_sysctl, "I", 5261 "Top noisy area, where weight_previous_na is used instead " 5262 "of weight_previous"); 5263 5264 /* hw.psm.synaptics.na_right. */ 5265 sc->syninfo.na_right = 563; 5266 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5267 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5268 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5269 sc, SYNAPTICS_SYSCTL_NA_RIGHT, 5270 synaptics_sysctl, "I", 5271 "Right noisy area, where weight_previous_na is used instead " 5272 "of weight_previous"); 5273 5274 /* hw.psm.synaptics.na_bottom. */ 5275 sc->syninfo.na_bottom = 1408; 5276 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5277 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5278 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5279 sc, SYNAPTICS_SYSCTL_NA_BOTTOM, 5280 synaptics_sysctl, "I", 5281 "Bottom noisy area, where weight_previous_na is used instead " 5282 "of weight_previous"); 5283 5284 /* hw.psm.synaptics.na_left. */ 5285 sc->syninfo.na_left = 1600; 5286 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5287 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5288 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5289 sc, SYNAPTICS_SYSCTL_NA_LEFT, 5290 synaptics_sysctl, "I", 5291 "Left noisy area, where weight_previous_na is used instead " 5292 "of weight_previous"); 5293 5294 /* hw.psm.synaptics.window_min. */ 5295 sc->syninfo.window_min = 4; 5296 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5297 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5298 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5299 sc, SYNAPTICS_SYSCTL_WINDOW_MIN, 5300 synaptics_sysctl, "I", 5301 "Minimum window size to start an action"); 5302 5303 /* hw.psm.synaptics.window_max. */ 5304 sc->syninfo.window_max = 10; 5305 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5306 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5307 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5308 sc, SYNAPTICS_SYSCTL_WINDOW_MAX, 5309 synaptics_sysctl, "I", 5310 "Maximum window size"); 5311 5312 /* hw.psm.synaptics.multiplicator. */ 5313 sc->syninfo.multiplicator = 10000; 5314 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5315 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5316 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5317 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR, 5318 synaptics_sysctl, "I", 5319 "Multiplicator to increase precision in averages and divisions"); 5320 5321 /* hw.psm.synaptics.weight_current. */ 5322 sc->syninfo.weight_current = 3; 5323 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5324 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5325 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5326 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT, 5327 synaptics_sysctl, "I", 5328 "Weight of the current movement in the new average"); 5329 5330 /* hw.psm.synaptics.weight_previous. */ 5331 sc->syninfo.weight_previous = 6; 5332 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5333 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5334 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5335 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS, 5336 synaptics_sysctl, "I", 5337 "Weight of the previous average"); 5338 5339 /* hw.psm.synaptics.weight_previous_na. */ 5340 sc->syninfo.weight_previous_na = 20; 5341 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5342 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5343 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5344 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA, 5345 synaptics_sysctl, "I", 5346 "Weight of the previous average (inside the noisy area)"); 5347 5348 /* hw.psm.synaptics.weight_len_squared. */ 5349 sc->syninfo.weight_len_squared = 2000; 5350 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5351 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5352 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5353 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED, 5354 synaptics_sysctl, "I", 5355 "Length (squared) of segments where weight_previous " 5356 "starts to decrease"); 5357 5358 /* hw.psm.synaptics.div_min. */ 5359 sc->syninfo.div_min = 9; 5360 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5361 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5362 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5363 sc, SYNAPTICS_SYSCTL_DIV_MIN, 5364 synaptics_sysctl, "I", 5365 "Divisor for fast movements"); 5366 5367 /* hw.psm.synaptics.div_max. */ 5368 sc->syninfo.div_max = 17; 5369 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5370 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5371 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5372 sc, SYNAPTICS_SYSCTL_DIV_MAX, 5373 synaptics_sysctl, "I", 5374 "Divisor for slow movements"); 5375 5376 /* hw.psm.synaptics.div_max_na. */ 5377 sc->syninfo.div_max_na = 30; 5378 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5379 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5380 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5381 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA, 5382 synaptics_sysctl, "I", 5383 "Divisor with slow movements (inside the noisy area)"); 5384 5385 /* hw.psm.synaptics.div_len. */ 5386 sc->syninfo.div_len = 100; 5387 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5388 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5389 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5390 sc, SYNAPTICS_SYSCTL_DIV_LEN, 5391 synaptics_sysctl, "I", 5392 "Length of segments where div_max starts to decrease"); 5393 5394 /* hw.psm.synaptics.tap_max_delta. */ 5395 sc->syninfo.tap_max_delta = 80; 5396 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5397 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5398 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5399 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA, 5400 synaptics_sysctl, "I", 5401 "Length of segments above which a tap is ignored"); 5402 5403 /* hw.psm.synaptics.tap_min_queue. */ 5404 sc->syninfo.tap_min_queue = 2; 5405 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5406 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5407 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5408 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE, 5409 synaptics_sysctl, "I", 5410 "Number of packets required to consider a tap"); 5411 5412 /* hw.psm.synaptics.taphold_timeout. */ 5413 sc->gesture.in_taphold = 0; 5414 sc->syninfo.taphold_timeout = tap_timeout; 5415 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5416 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5417 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5418 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT, 5419 synaptics_sysctl, "I", 5420 "Maximum elapsed time between two taps to consider a tap-hold " 5421 "action"); 5422 5423 /* hw.psm.synaptics.vscroll_hor_area. */ 5424 sc->syninfo.vscroll_hor_area = 0; /* 1300 */ 5425 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5426 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5427 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5428 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA, 5429 synaptics_sysctl, "I", 5430 "Area reserved for horizontal virtual scrolling"); 5431 5432 /* hw.psm.synaptics.vscroll_ver_area. */ 5433 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right; 5434 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5435 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5436 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5437 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA, 5438 synaptics_sysctl, "I", 5439 "Area reserved for vertical virtual scrolling"); 5440 5441 /* hw.psm.synaptics.vscroll_min_delta. */ 5442 sc->syninfo.vscroll_min_delta = 50; 5443 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5444 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5445 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5446 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA, 5447 synaptics_sysctl, "I", 5448 "Minimum movement to consider virtual scrolling"); 5449 5450 /* hw.psm.synaptics.vscroll_div_min. */ 5451 sc->syninfo.vscroll_div_min = 100; 5452 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5453 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5454 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5455 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN, 5456 synaptics_sysctl, "I", 5457 "Divisor for fast scrolling"); 5458 5459 /* hw.psm.synaptics.vscroll_div_min. */ 5460 sc->syninfo.vscroll_div_max = 150; 5461 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5462 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5463 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5464 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX, 5465 synaptics_sysctl, "I", 5466 "Divisor for slow scrolling"); 5467 5468 /* hw.psm.synaptics.touchpad_off. */ 5469 sc->syninfo.touchpad_off = 0; 5470 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5471 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5472 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5473 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF, 5474 synaptics_sysctl, "I", 5475 "Turn off touchpad"); 5476 5477 sc->syninfo.softbuttons_y = 0; 5478 sc->syninfo.softbutton2_x = 0; 5479 sc->syninfo.softbutton3_x = 0; 5480 5481 /* skip softbuttons sysctl on not clickpads */ 5482 if (sc->synhw.capClickPad) 5483 synaptics_sysctl_create_softbuttons_tree(sc); 5484 } 5485 5486 5487 static int 5488 synaptics_preferred_mode(struct psm_softc *sc) { 5489 int mode_byte; 5490 5491 mode_byte = 0xc4; 5492 5493 /* request wmode where available */ 5494 if (sc->synhw.capExtended) 5495 mode_byte |= 1; 5496 5497 return mode_byte; 5498 } 5499 5500 static void 5501 synaptics_set_mode(struct psm_softc *sc, int mode_byte) { 5502 mouse_ext_command(sc->kbdc, mode_byte); 5503 5504 /* "Commit" the Set Mode Byte command sent above. */ 5505 set_mouse_sampling_rate(sc->kbdc, 20); 5506 5507 /* 5508 * Enable advanced gestures mode if supported and we are not entering 5509 * passthrough mode. 5510 */ 5511 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) && 5512 !(mode_byte & (1 << 5))) { 5513 mouse_ext_command(sc->kbdc, 3); 5514 set_mouse_sampling_rate(sc->kbdc, 0xc8); 5515 } 5516 } 5517 5518 static int 5519 enable_synaptics(struct psm_softc *sc, enum probearg arg) 5520 { 5521 KBDC kbdc = sc->kbdc; 5522 synapticshw_t synhw; 5523 int status[3]; 5524 int buttons; 5525 5526 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n")); 5527 5528 /* 5529 * Just to be on the safe side: this avoids troubles with 5530 * following mouse_ext_command() when the previous command 5531 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on 5532 * Synaptics Touchpad behaviour. 5533 */ 5534 set_mouse_scaling(kbdc, 1); 5535 5536 /* Identify the Touchpad version. */ 5537 if (mouse_ext_command(kbdc, 0) == 0) 5538 return (FALSE); 5539 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5540 return (FALSE); 5541 if (status[1] != 0x47) 5542 return (FALSE); 5543 5544 bzero(&synhw, sizeof(synhw)); 5545 synhw.infoMinor = status[0]; 5546 synhw.infoMajor = status[2] & 0x0f; 5547 5548 if (verbose >= 2) 5549 kprintf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor, 5550 synhw.infoMinor); 5551 5552 if (synhw.infoMajor < 4) { 5553 kprintf(" Unsupported (pre-v4) Touchpad detected\n"); 5554 return (FALSE); 5555 } 5556 5557 /* Get the Touchpad model information. */ 5558 if (mouse_ext_command(kbdc, 3) == 0) 5559 return (FALSE); 5560 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5561 return (FALSE); 5562 if ((status[1] & 0x01) != 0) { 5563 kprintf(" Failed to read model information\n"); 5564 return (FALSE); 5565 } 5566 5567 synhw.infoRot180 = (status[0] & 0x80) != 0; 5568 synhw.infoPortrait = (status[0] & 0x40) != 0; 5569 synhw.infoSensor = status[0] & 0x3f; 5570 synhw.infoHardware = (status[1] & 0xfe) >> 1; 5571 synhw.infoNewAbs = (status[2] & 0x80) != 0; 5572 synhw.capPen = (status[2] & 0x40) != 0; 5573 synhw.infoSimplC = (status[2] & 0x20) != 0; 5574 synhw.infoGeometry = status[2] & 0x0f; 5575 5576 if (verbose >= 2) { 5577 kprintf(" Model information:\n"); 5578 kprintf(" infoRot180: %d\n", synhw.infoRot180); 5579 kprintf(" infoPortrait: %d\n", synhw.infoPortrait); 5580 kprintf(" infoSensor: %d\n", synhw.infoSensor); 5581 kprintf(" infoHardware: %d\n", synhw.infoHardware); 5582 kprintf(" infoNewAbs: %d\n", synhw.infoNewAbs); 5583 kprintf(" capPen: %d\n", synhw.capPen); 5584 kprintf(" infoSimplC: %d\n", synhw.infoSimplC); 5585 kprintf(" infoGeometry: %d\n", synhw.infoGeometry); 5586 } 5587 5588 /* Read the extended capability bits. */ 5589 if (mouse_ext_command(kbdc, 2) == 0) 5590 return (FALSE); 5591 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5592 return (FALSE); 5593 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) { 5594 kprintf(" Failed to read extended capability bits\n"); 5595 return (FALSE); 5596 } 5597 5598 /* Set the different capabilities when they exist. */ 5599 buttons = 0; 5600 synhw.capExtended = (status[0] & 0x80) != 0; 5601 if (synhw.capExtended) { 5602 synhw.nExtendedQueries = (status[0] & 0x70) >> 4; 5603 synhw.capMiddle = (status[0] & 0x04) != 0; 5604 synhw.capPassthrough = (status[2] & 0x80) != 0; 5605 synhw.capLowPower = (status[2] & 0x40) != 0; 5606 synhw.capMultiFingerReport = 5607 (status[2] & 0x20) != 0; 5608 synhw.capSleep = (status[2] & 0x10) != 0; 5609 synhw.capFourButtons = (status[2] & 0x08) != 0; 5610 synhw.capBallistics = (status[2] & 0x04) != 0; 5611 synhw.capMultiFinger = (status[2] & 0x02) != 0; 5612 synhw.capPalmDetect = (status[2] & 0x01) != 0; 5613 5614 if (!set_mouse_scaling(kbdc, 1)) 5615 return (FALSE); 5616 if (mouse_ext_command(kbdc, 0x08) == 0) 5617 return (FALSE); 5618 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5619 return (FALSE); 5620 5621 synhw.infoXupmm = status[0]; 5622 synhw.infoYupmm = status[2]; 5623 5624 if (verbose >= 2) { 5625 kprintf(" Extended capabilities:\n"); 5626 kprintf(" capExtended: %d\n", synhw.capExtended); 5627 kprintf(" capMiddle: %d\n", synhw.capMiddle); 5628 kprintf(" nExtendedQueries: %d\n", 5629 synhw.nExtendedQueries); 5630 kprintf(" capPassthrough: %d\n", synhw.capPassthrough); 5631 kprintf(" capLowPower: %d\n", synhw.capLowPower); 5632 kprintf(" capMultiFingerReport: %d\n", 5633 synhw.capMultiFingerReport); 5634 kprintf(" capSleep: %d\n", synhw.capSleep); 5635 kprintf(" capFourButtons: %d\n", synhw.capFourButtons); 5636 kprintf(" capBallistics: %d\n", synhw.capBallistics); 5637 kprintf(" capMultiFinger: %d\n", synhw.capMultiFinger); 5638 kprintf(" capPalmDetect: %d\n", synhw.capPalmDetect); 5639 kprintf(" infoXupmm: %d\n", synhw.infoXupmm); 5640 kprintf(" infoYupmm: %d\n", synhw.infoYupmm); 5641 } 5642 5643 /* 5644 * If nExtendedQueries is 1 or greater, then the TouchPad 5645 * supports this number of extended queries. We can load 5646 * more information about buttons using query 0x09. 5647 */ 5648 if (synhw.nExtendedQueries >= 1) { 5649 if (!set_mouse_scaling(kbdc, 1)) 5650 return (FALSE); 5651 if (mouse_ext_command(kbdc, 0x09) == 0) 5652 return (FALSE); 5653 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5654 return (FALSE); 5655 synhw.verticalScroll = (status[0] & 0x01) != 0; 5656 synhw.horizontalScroll = (status[0] & 0x02) != 0; 5657 synhw.verticalWheel = (status[0] & 0x08) != 0; 5658 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4; 5659 synhw.capEWmode = (status[0] & 0x04) != 0; 5660 if (verbose >= 2) { 5661 kprintf(" Extended model ID:\n"); 5662 kprintf(" verticalScroll: %d\n", 5663 synhw.verticalScroll); 5664 kprintf(" horizontalScroll: %d\n", 5665 synhw.horizontalScroll); 5666 kprintf(" verticalWheel: %d\n", 5667 synhw.verticalWheel); 5668 kprintf(" nExtendedButtons: %d\n", 5669 synhw.nExtendedButtons); 5670 kprintf(" capEWmode: %d\n", 5671 synhw.capEWmode); 5672 } 5673 /* 5674 * Add the number of extended buttons to the total 5675 * button support count, including the middle button 5676 * if capMiddle support bit is set. 5677 */ 5678 buttons = synhw.nExtendedButtons + synhw.capMiddle; 5679 } else 5680 /* 5681 * If the capFourButtons support bit is set, 5682 * add a fourth button to the total button count. 5683 */ 5684 buttons = synhw.capFourButtons ? 1 : 0; 5685 5686 /* Read the continued capabilities bits. */ 5687 if (synhw.nExtendedQueries >= 4) { 5688 if (!set_mouse_scaling(kbdc, 1)) 5689 return (FALSE); 5690 if (mouse_ext_command(kbdc, 0x0c) == 0) 5691 return (FALSE); 5692 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5693 return (FALSE); 5694 5695 synhw.capClickPad = (status[1] & 0x01) << 1; 5696 synhw.capClickPad |= (status[0] & 0x10) != 0; 5697 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0; 5698 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0; 5699 synhw.capReportsV = (status[1] & 0x08) != 0; 5700 synhw.capUniformClickPad = (status[1] & 0x10) != 0; 5701 synhw.capReportsMin = (status[1] & 0x20) != 0; 5702 synhw.capInterTouch = (status[1] & 0x40) != 0; 5703 synhw.capReportsMax = (status[0] & 0x02) != 0; 5704 synhw.capClearPad = (status[0] & 0x04) != 0; 5705 synhw.capAdvancedGestures = (status[0] & 0x08) != 0; 5706 synhw.capCoveredPad = (status[0] & 0x80) != 0; 5707 5708 if (synhw.capReportsMax) { 5709 if (!set_mouse_scaling(kbdc, 1)) 5710 return (FALSE); 5711 if (mouse_ext_command(kbdc, 0x0d) == 0) 5712 return (FALSE); 5713 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5714 return (FALSE); 5715 5716 synhw.maximumXCoord = (status[0] << 5) | 5717 ((status[1] & 0x0f) << 1); 5718 synhw.maximumYCoord = (status[2] << 5) | 5719 ((status[1] & 0xf0) >> 3); 5720 } else { 5721 /* 5722 * Typical bezel limits. Taken from 'Synaptics 5723 * PS/2 * TouchPad Interfacing Guide' p.3.2.3. 5724 */ 5725 synhw.maximumXCoord = 5472; 5726 synhw.maximumYCoord = 4448; 5727 } 5728 5729 if (synhw.capReportsMin) { 5730 if (!set_mouse_scaling(kbdc, 1)) 5731 return (FALSE); 5732 if (mouse_ext_command(kbdc, 0x0f) == 0) 5733 return (FALSE); 5734 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5735 return (FALSE); 5736 5737 synhw.minimumXCoord = (status[0] << 5) | 5738 ((status[1] & 0x0f) << 1); 5739 synhw.minimumYCoord = (status[2] << 5) | 5740 ((status[1] & 0xf0) >> 3); 5741 } else { 5742 /* 5743 * Typical bezel limits. Taken from 'Synaptics 5744 * PS/2 * TouchPad Interfacing Guide' p.3.2.3. 5745 */ 5746 synhw.minimumXCoord = 1472; 5747 synhw.minimumYCoord = 1408; 5748 } 5749 5750 if (verbose >= 2) { 5751 kprintf(" Continued capabilities:\n"); 5752 kprintf(" capClickPad: %d\n", 5753 synhw.capClickPad); 5754 kprintf(" capDeluxeLEDs: %d\n", 5755 synhw.capDeluxeLEDs); 5756 kprintf(" noAbsoluteFilter: %d\n", 5757 synhw.noAbsoluteFilter); 5758 kprintf(" capReportsV: %d\n", 5759 synhw.capReportsV); 5760 kprintf(" capUniformClickPad: %d\n", 5761 synhw.capUniformClickPad); 5762 kprintf(" capReportsMin: %d\n", 5763 synhw.capReportsMin); 5764 kprintf(" capInterTouch: %d\n", 5765 synhw.capInterTouch); 5766 kprintf(" capReportsMax: %d\n", 5767 synhw.capReportsMax); 5768 kprintf(" capClearPad: %d\n", 5769 synhw.capClearPad); 5770 kprintf(" capAdvancedGestures: %d\n", 5771 synhw.capAdvancedGestures); 5772 kprintf(" capCoveredPad: %d\n", 5773 synhw.capCoveredPad); 5774 if (synhw.capReportsMax) { 5775 kprintf(" maximumXCoord: %d\n", 5776 synhw.maximumXCoord); 5777 kprintf(" maximumYCoord: %d\n", 5778 synhw.maximumYCoord); 5779 } 5780 if (synhw.capReportsMin) { 5781 kprintf(" minimumXCoord: %d\n", 5782 synhw.minimumXCoord); 5783 kprintf(" minimumYCoord: %d\n", 5784 synhw.minimumYCoord); 5785 } 5786 } 5787 buttons += synhw.capClickPad; 5788 } 5789 } 5790 5791 if (verbose >= 2) { 5792 if (synhw.capExtended) 5793 kprintf(" Additional Buttons: %d\n", buttons); 5794 else 5795 kprintf(" No extended capabilities\n"); 5796 } 5797 5798 /* 5799 * Add the default number of 3 buttons to the total 5800 * count of supported buttons reported above. 5801 */ 5802 buttons += 3; 5803 5804 /* 5805 * Read the mode byte. 5806 * 5807 * XXX: Note the Synaptics documentation also defines the first 5808 * byte of the response to this query to be a constant 0x3b, this 5809 * does not appear to be true for Touchpads with guest devices. 5810 */ 5811 if (mouse_ext_command(kbdc, 1) == 0) 5812 return (FALSE); 5813 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5814 return (FALSE); 5815 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) { 5816 kprintf(" Failed to read mode byte\n"); 5817 return (FALSE); 5818 } 5819 5820 if (arg == PROBE) 5821 sc->synhw = synhw; 5822 if (!synaptics_support) 5823 return (FALSE); 5824 5825 synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 5826 5827 if (trackpoint_support && synhw.capPassthrough) { 5828 enable_trackpoint(sc, arg); 5829 } 5830 5831 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons)); 5832 5833 if (arg == PROBE) { 5834 /* Create sysctl tree. */ 5835 synaptics_sysctl_create_tree(sc, "synaptics", 5836 "Synaptics TouchPad"); 5837 sc->hw.buttons = buttons; 5838 } 5839 return (TRUE); 5840 } 5841 5842 static void 5843 synaptics_passthrough_on(struct psm_softc *sc) 5844 { 5845 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n")); 5846 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5)); 5847 } 5848 5849 static void 5850 synaptics_passthrough_off(struct psm_softc *sc) 5851 { 5852 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n")); 5853 set_mouse_scaling(sc->kbdc, 2); 5854 set_mouse_scaling(sc->kbdc, 1); 5855 synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 5856 } 5857 5858 /* IBM/Lenovo TrackPoint */ 5859 static int 5860 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val) 5861 { 5862 const int seq[] = { 0xe2, cmd, loc, val }; 5863 int i; 5864 5865 if (sc->synhw.capPassthrough) 5866 synaptics_passthrough_on(sc); 5867 5868 for (i = 0; i < nitems(seq); i++) { 5869 if (sc->synhw.capPassthrough && 5870 (seq[i] == 0xff || seq[i] == 0xe7)) 5871 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) { 5872 synaptics_passthrough_off(sc); 5873 return (EIO); 5874 } 5875 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) { 5876 if (sc->synhw.capPassthrough) 5877 synaptics_passthrough_off(sc); 5878 return (EIO); 5879 } 5880 } 5881 5882 if (sc->synhw.capPassthrough) 5883 synaptics_passthrough_off(sc); 5884 5885 return (0); 5886 } 5887 5888 #define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x) 5889 #define TPMASK 0 5890 #define TPLOC 1 5891 #define TPINFO 2 5892 5893 static int 5894 trackpoint_sysctl(SYSCTL_HANDLER_ARGS) 5895 { 5896 static const int data[][3] = { 5897 { 0x00, 0x4a, PSM_TPINFO(sensitivity) }, 5898 { 0x00, 0x4d, PSM_TPINFO(inertia) }, 5899 { 0x00, 0x60, PSM_TPINFO(uplateau) }, 5900 { 0x00, 0x57, PSM_TPINFO(reach) }, 5901 { 0x00, 0x58, PSM_TPINFO(draghys) }, 5902 { 0x00, 0x59, PSM_TPINFO(mindrag) }, 5903 { 0x00, 0x5a, PSM_TPINFO(upthresh) }, 5904 { 0x00, 0x5c, PSM_TPINFO(threshold) }, 5905 { 0x00, 0x5d, PSM_TPINFO(jenks) }, 5906 { 0x00, 0x5e, PSM_TPINFO(ztime) }, 5907 { 0x01, 0x2c, PSM_TPINFO(pts) }, 5908 { 0x08, 0x2d, PSM_TPINFO(skipback) } 5909 }; 5910 struct psm_softc *sc; 5911 int error, newval, *oldvalp; 5912 const int *tp; 5913 5914 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data)) 5915 return (EINVAL); 5916 sc = arg1; 5917 tp = data[arg2]; 5918 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]); 5919 newval = *oldvalp; 5920 error = sysctl_handle_int(oidp, &newval, 0, req); 5921 if (error != 0) 5922 return (error); 5923 if (newval == *oldvalp) 5924 return (0); 5925 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1)) 5926 return (EINVAL); 5927 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47, 5928 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]); 5929 if (error != 0) 5930 return (error); 5931 *oldvalp = newval; 5932 5933 return (0); 5934 } 5935 5936 static void 5937 trackpoint_sysctl_create_tree(struct psm_softc *sc) 5938 { 5939 5940 if (sc->tpinfo.sysctl_tree != NULL) 5941 return; 5942 5943 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */ 5944 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx); 5945 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx, 5946 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD, 5947 0, "IBM/Lenovo TrackPoint"); 5948 5949 /* hw.psm.trackpoint.sensitivity */ 5950 sc->tpinfo.sensitivity = 0x80; 5951 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5952 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5953 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5954 sc, TRACKPOINT_SYSCTL_SENSITIVITY, 5955 trackpoint_sysctl, "I", 5956 "Sensitivity"); 5957 5958 /* hw.psm.trackpoint.negative_inertia */ 5959 sc->tpinfo.inertia = 0x06; 5960 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5961 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5962 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5963 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA, 5964 trackpoint_sysctl, "I", 5965 "Negative inertia factor"); 5966 5967 /* hw.psm.trackpoint.upper_plateau */ 5968 sc->tpinfo.uplateau = 0x61; 5969 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5970 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5971 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5972 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU, 5973 trackpoint_sysctl, "I", 5974 "Transfer function upper plateau speed"); 5975 5976 /* hw.psm.trackpoint.backup_range */ 5977 sc->tpinfo.reach = 0x0a; 5978 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5979 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5980 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5981 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE, 5982 trackpoint_sysctl, "I", 5983 "Backup range"); 5984 5985 /* hw.psm.trackpoint.drag_hysteresis */ 5986 sc->tpinfo.draghys = 0xff; 5987 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5988 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5989 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5990 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS, 5991 trackpoint_sysctl, "I", 5992 "Drag hysteresis"); 5993 5994 /* hw.psm.trackpoint.minimum_drag */ 5995 sc->tpinfo.mindrag = 0x14; 5996 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5997 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5998 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5999 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG, 6000 trackpoint_sysctl, "I", 6001 "Minimum drag"); 6002 6003 /* hw.psm.trackpoint.up_threshold */ 6004 sc->tpinfo.upthresh = 0xff; 6005 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6006 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6007 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6008 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD, 6009 trackpoint_sysctl, "I", 6010 "Up threshold for release"); 6011 6012 /* hw.psm.trackpoint.threshold */ 6013 sc->tpinfo.threshold = 0x08; 6014 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6015 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6016 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6017 sc, TRACKPOINT_SYSCTL_THRESHOLD, 6018 trackpoint_sysctl, "I", 6019 "Threshold"); 6020 6021 /* hw.psm.trackpoint.jenks_curvature */ 6022 sc->tpinfo.jenks = 0x87; 6023 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6024 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6025 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6026 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE, 6027 trackpoint_sysctl, "I", 6028 "Jenks curvature"); 6029 6030 /* hw.psm.trackpoint.z_time */ 6031 sc->tpinfo.ztime = 0x26; 6032 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6033 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6034 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6035 sc, TRACKPOINT_SYSCTL_Z_TIME, 6036 trackpoint_sysctl, "I", 6037 "Z time constant"); 6038 6039 /* hw.psm.trackpoint.press_to_select */ 6040 sc->tpinfo.pts = 0x00; 6041 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6042 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6043 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6044 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT, 6045 trackpoint_sysctl, "I", 6046 "Press to Select"); 6047 6048 /* hw.psm.trackpoint.skip_backups */ 6049 sc->tpinfo.skipback = 0x00; 6050 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 6051 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 6052 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 6053 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS, 6054 trackpoint_sysctl, "I", 6055 "Skip backups from drags"); 6056 } 6057 6058 static void 6059 set_trackpoint_parameters(struct psm_softc *sc) 6060 { 6061 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity); 6062 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau); 6063 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia); 6064 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach); 6065 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys); 6066 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag); 6067 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh); 6068 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold); 6069 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks); 6070 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime); 6071 if (sc->tpinfo.pts == 0x01) 6072 trackpoint_command(sc, 0x47, 0x2c, 0x01); 6073 if (sc->tpinfo.skipback == 0x01) 6074 trackpoint_command(sc, 0x47, 0x2d, 0x08); 6075 } 6076 6077 static int 6078 enable_trackpoint(struct psm_softc *sc, enum probearg arg) 6079 { 6080 KBDC kbdc = sc->kbdc; 6081 int id; 6082 6083 /* 6084 * If called from enable_synaptics(), make sure that passthrough 6085 * mode is enabled so we can reach the trackpoint. 6086 * However, passthrough mode must be disabled before setting the 6087 * trackpoint parameters, as rackpoint_command() enables and disables 6088 * passthrough mode on its own. 6089 */ 6090 if (sc->synhw.capPassthrough) 6091 synaptics_passthrough_on(sc); 6092 6093 if (send_aux_command(kbdc, 0xe1) != PSM_ACK || 6094 read_aux_data(kbdc) != 0x01) 6095 goto no_trackpoint; 6096 id = read_aux_data(kbdc); 6097 if (id < 0x01) 6098 goto no_trackpoint; 6099 if (arg == PROBE) 6100 sc->tphw = id; 6101 if (!trackpoint_support) 6102 goto no_trackpoint; 6103 6104 if (sc->synhw.capPassthrough) 6105 synaptics_passthrough_off(sc); 6106 6107 if (arg == PROBE) { 6108 trackpoint_sysctl_create_tree(sc); 6109 /* 6110 * Don't overwrite hwid and buttons when we are 6111 * a guest device. 6112 */ 6113 if (!sc->synhw.capPassthrough) { 6114 sc->hw.hwid = id; 6115 sc->hw.buttons = 3; 6116 } 6117 } 6118 6119 set_trackpoint_parameters(sc); 6120 6121 return (TRUE); 6122 6123 no_trackpoint: 6124 if (sc->synhw.capPassthrough) 6125 synaptics_passthrough_off(sc); 6126 6127 return (FALSE); 6128 } 6129 6130 /* Interlink electronics VersaPad */ 6131 static int 6132 enable_versapad(struct psm_softc *sc, enum probearg arg) 6133 { 6134 KBDC kbdc = sc->kbdc; 6135 int data[3]; 6136 6137 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 6138 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 6139 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6140 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6141 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6142 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6143 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 6144 return (FALSE); 6145 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 6146 return (FALSE); 6147 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6148 6149 return (TRUE); /* PS/2 absolute mode */ 6150 } 6151 6152 /* Elantech Touchpad */ 6153 static int 6154 elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val) 6155 { 6156 int res, readcmd, retidx; 6157 int resp[3]; 6158 6159 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR; 6160 retidx = hwversion == 4 ? 1 : 0; 6161 6162 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6163 res |= send_aux_command(kbdc, readcmd) != PSM_ACK; 6164 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6165 res |= send_aux_command(kbdc, reg) != PSM_ACK; 6166 res |= get_mouse_status(kbdc, resp, 0, 3) != 3; 6167 6168 if (res == 0) 6169 *val = resp[retidx]; 6170 6171 return (res); 6172 } 6173 6174 static int 6175 elantech_write_1(KBDC kbdc, int hwversion, int reg, int val) 6176 { 6177 int res, writecmd; 6178 6179 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR; 6180 6181 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6182 res |= send_aux_command(kbdc, writecmd) != PSM_ACK; 6183 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6184 res |= send_aux_command(kbdc, reg) != PSM_ACK; 6185 if (hwversion == 4) { 6186 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6187 res |= send_aux_command(kbdc, writecmd) != PSM_ACK; 6188 } 6189 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6190 res |= send_aux_command(kbdc, val) != PSM_ACK; 6191 res |= set_mouse_scaling(kbdc, 1) == 0; 6192 6193 return (res); 6194 } 6195 6196 static int 6197 elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp) 6198 { 6199 int res; 6200 6201 if (hwversion == 2) { 6202 res = set_mouse_scaling(kbdc, 1) == 0; 6203 res |= mouse_ext_command(kbdc, cmd) == 0; 6204 } else { 6205 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6206 res |= send_aux_command(kbdc, cmd) != PSM_ACK; 6207 } 6208 res |= get_mouse_status(kbdc, resp, 0, 3) != 3; 6209 6210 return (res); 6211 } 6212 6213 static int 6214 elantech_init(KBDC kbdc, elantechhw_t *elanhw) 6215 { 6216 int i, val, res, hwversion, reg10; 6217 6218 /* set absolute mode */ 6219 hwversion = elanhw->hwversion; 6220 reg10 = -1; 6221 switch (hwversion) { 6222 case 2: 6223 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4; 6224 res = elantech_write_1(kbdc, hwversion, 0x10, reg10); 6225 if (res) 6226 break; 6227 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A); 6228 break; 6229 case 3: 6230 reg10 = 0x0b; 6231 res = elantech_write_1(kbdc, hwversion, 0x10, reg10); 6232 break; 6233 case 4: 6234 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01); 6235 break; 6236 default: 6237 res = 1; 6238 } 6239 6240 /* Read back reg 0x10 to ensure hardware is ready. */ 6241 if (res == 0 && reg10 >= 0) { 6242 for (i = 0; i < 5; i++) { 6243 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0) 6244 break; 6245 DELAY(2000); 6246 } 6247 if (i == 5) 6248 res = 1; 6249 } 6250 6251 if (res) 6252 kprintf("couldn't set absolute mode\n"); 6253 6254 return (res); 6255 } 6256 6257 static void 6258 elantech_init_synaptics(struct psm_softc *sc) 6259 { 6260 6261 /* Set capabilites required by movement smother */ 6262 sc->synhw.infoMajor = sc->elanhw.hwversion; 6263 sc->synhw.infoMinor = sc->elanhw.fwversion; 6264 sc->synhw.infoXupmm = sc->elanhw.dpmmx; 6265 sc->synhw.infoYupmm = sc->elanhw.dpmmy; 6266 sc->synhw.verticalScroll = 0; 6267 sc->synhw.nExtendedQueries = 4; 6268 sc->synhw.capExtended = 1; 6269 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint; 6270 sc->synhw.capClickPad = sc->elanhw.isclickpad; 6271 sc->synhw.capMultiFinger = 1; 6272 sc->synhw.capAdvancedGestures = 1; 6273 sc->synhw.capPalmDetect = 1; 6274 sc->synhw.capPen = 0; 6275 sc->synhw.capReportsMax = 1; 6276 sc->synhw.maximumXCoord = sc->elanhw.sizex; 6277 sc->synhw.maximumYCoord = sc->elanhw.sizey; 6278 sc->synhw.capReportsMin = 1; 6279 sc->synhw.minimumXCoord = 0; 6280 sc->synhw.minimumYCoord = 0; 6281 6282 if (sc->syninfo.sysctl_tree == NULL) { 6283 synaptics_sysctl_create_tree(sc, "elantech", 6284 "Elantech Touchpad"); 6285 6286 /* 6287 * Adjust synaptic smoother tunables 6288 * 1. Disable finger detection pressure threshold. Unlike 6289 * synaptics we assume the finger is acting when packet with 6290 * its X&Y arrives not when pressure exceedes some threshold 6291 * 2. Disable unrelated features like margins and noisy areas 6292 * 3. Disable virtual scroll areas as 2nd finger is preferable 6293 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized 6294 * softbuttons 6295 * 5. Scale down divisors and movement lengths by a factor of 3 6296 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio 6297 */ 6298 6299 /* Set reporting range to be equal touchpad size */ 6300 sc->syninfo.max_x = sc->elanhw.sizex; 6301 sc->syninfo.max_y = sc->elanhw.sizey; 6302 6303 /* Disable finger detection pressure threshold */ 6304 sc->syninfo.min_pressure = 1; 6305 6306 /* Use full area of touchpad */ 6307 sc->syninfo.margin_top = 0; 6308 sc->syninfo.margin_right = 0; 6309 sc->syninfo.margin_bottom = 0; 6310 sc->syninfo.margin_left = 0; 6311 6312 /* Disable noisy area */ 6313 sc->syninfo.na_top = 0; 6314 sc->syninfo.na_right = 0; 6315 sc->syninfo.na_bottom = 0; 6316 sc->syninfo.na_left = 0; 6317 6318 /* Tune divisors and movement lengths */ 6319 sc->syninfo.weight_len_squared = 200; 6320 sc->syninfo.div_min = 3; 6321 sc->syninfo.div_max = 6; 6322 sc->syninfo.div_max_na = 10; 6323 sc->syninfo.div_len = 30; 6324 sc->syninfo.tap_max_delta = 25; 6325 6326 /* Disable virtual scrolling areas and tune its divisors */ 6327 sc->syninfo.vscroll_hor_area = 0; 6328 sc->syninfo.vscroll_ver_area = 0; 6329 sc->syninfo.vscroll_min_delta = 15; 6330 sc->syninfo.vscroll_div_min = 30; 6331 sc->syninfo.vscroll_div_max = 50; 6332 6333 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */ 6334 if (sc->elanhw.isclickpad) { 6335 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4; 6336 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25; 6337 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25; 6338 } 6339 } 6340 6341 return; 6342 } 6343 6344 static int 6345 enable_elantech(struct psm_softc *sc, enum probearg arg) 6346 { 6347 static const int ic2hw[] = 6348 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 6349 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 }; 6350 elantechhw_t elanhw; 6351 int icversion, hwversion, dptracex, dptracey, id, resp[3], dpix, dpiy; 6352 KBDC kbdc = sc->kbdc; 6353 6354 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n")); 6355 6356 set_mouse_scaling(kbdc, 1); 6357 set_mouse_scaling(kbdc, 1); 6358 set_mouse_scaling(kbdc, 1); 6359 if (get_mouse_status(kbdc, resp, 0, 3) != 3) 6360 return (FALSE); 6361 6362 if (!ELANTECH_MAGIC(resp)) 6363 return (FALSE); 6364 6365 /* Identify the Touchpad version. */ 6366 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp)) 6367 return (FALSE); 6368 6369 bzero(&elanhw, sizeof(elanhw)); 6370 6371 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2]; 6372 icversion = resp[0] & 0x0f; 6373 hwversion = ic2hw[icversion]; 6374 6375 if (verbose >= 2) 6376 kprintf("Elantech touchpad hardware v.%d firmware v.0x%06x\n", 6377 hwversion, elanhw.fwversion); 6378 6379 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) { 6380 kprintf (" Unsupported touchpad hardware (v1)\n"); 6381 return (FALSE); 6382 } 6383 if (hwversion == 0) { 6384 kprintf (" Unknown touchpad hardware (firmware v.0x%06x)\n", 6385 elanhw.fwversion); 6386 return (FALSE); 6387 } 6388 6389 /* Get the Touchpad model information. */ 6390 elanhw.hwversion = hwversion; 6391 elanhw.issemimt = hwversion == 2; 6392 elanhw.isclickpad = (resp[1] & 0x10) != 0; 6393 elanhw.hascrc = (resp[1] & 0x40) != 0; 6394 elanhw.haspressure = elanhw.fwversion >= 0x020800; 6395 6396 /* Read the capability bits. */ 6397 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) { 6398 kprintf(" Failed to read capability bits\n"); 6399 return (FALSE); 6400 } 6401 6402 elanhw.ntracesx = resp[1] - 1; 6403 elanhw.ntracesy = resp[2] - 1; 6404 elanhw.hastrackpoint = (resp[0] & 0x80) != 0; 6405 6406 /* Get the touchpad resolution */ 6407 switch (hwversion) { 6408 case 4: 6409 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp) 6410 == 0) { 6411 dpix = (resp[1] & 0x0f) * 10 + 790; 6412 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790; 6413 elanhw.dpmmx = (dpix * 10 + 5) / 254; 6414 elanhw.dpmmy = (dpiy * 10 + 5) / 254; 6415 break; 6416 } 6417 /* FALLTHROUGH */ 6418 case 2: 6419 case 3: 6420 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */ 6421 break; 6422 } 6423 6424 if (!elantech_support) 6425 return (FALSE); 6426 6427 if (elantech_init(kbdc, &elanhw)) { 6428 kprintf("couldn't initialize elantech touchpad\n"); 6429 return (FALSE); 6430 } 6431 6432 /* 6433 * Get the touchpad reporting range. 6434 * On HW v.3 touchpads it should be done after switching hardware 6435 * to real resolution mode (by setting bit 3 of reg10) 6436 */ 6437 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) { 6438 kprintf(" Failed to read touchpad size\n"); 6439 elanhw.sizex = 10000; /* Arbitrary high values to */ 6440 elanhw.sizey = 10000; /* prevent clipping in smoother */ 6441 } else if (hwversion == 2) { 6442 dptracex = dptracey = 64; 6443 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) && 6444 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) { 6445 dptracex = resp[1] / 2; 6446 dptracey = resp[2] / 2; 6447 } 6448 elanhw.sizex = (elanhw.ntracesx - 1) * dptracex; 6449 elanhw.sizey = (elanhw.ntracesy - 1) * dptracey; 6450 } else { 6451 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1]; 6452 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2]; 6453 } 6454 6455 if (verbose >= 2) { 6456 kprintf(" Model information:\n"); 6457 kprintf(" MaxX: %d\n", elanhw.sizex); 6458 kprintf(" MaxY: %d\n", elanhw.sizey); 6459 kprintf(" DpmmX: %d\n", elanhw.dpmmx); 6460 kprintf(" DpmmY: %d\n", elanhw.dpmmy); 6461 kprintf(" TracesX: %d\n", elanhw.ntracesx); 6462 kprintf(" TracesY: %d\n", elanhw.ntracesy); 6463 kprintf(" SemiMT: %d\n", elanhw.issemimt); 6464 kprintf(" Clickpad: %d\n", elanhw.isclickpad); 6465 kprintf(" Trackpoint: %d\n", elanhw.hastrackpoint); 6466 kprintf(" CRC: %d\n", elanhw.hascrc); 6467 kprintf(" Pressure: %d\n", elanhw.haspressure); 6468 } 6469 6470 VLOG(3, (LOG_DEBUG, "elantech: END init\n")); 6471 6472 if (arg == PROBE) { 6473 sc->elanhw = elanhw; 6474 sc->hw.buttons = 3; 6475 6476 /* Initialize synaptics movement smoother */ 6477 elantech_init_synaptics(sc); 6478 6479 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 6480 PSM_FINGER_RESET(sc->elanaction.fingers[id]); 6481 } 6482 return (TRUE); 6483 } 6484 6485 /* 6486 * Return true if 'now' is earlier than (start + (secs.usecs)). 6487 * Now may be NULL and the function will fetch the current time from 6488 * getmicrouptime(), or a cached 'now' can be passed in. 6489 * All values should be numbers derived from getmicrouptime(). 6490 */ 6491 static int 6492 timeelapsed(const struct timeval *start, int secs, int usecs, 6493 const struct timeval *now) { 6494 struct timeval snow, tv; 6495 6496 /* if there is no 'now' passed in, the get it as a convenience. */ 6497 if (now == NULL) { 6498 getmicrouptime(&snow); 6499 now = &snow; 6500 } 6501 6502 tv.tv_sec = secs; 6503 tv.tv_usec = usecs; 6504 timevaladd(&tv, start); 6505 return (timevalcmp(&tv, now, <)); 6506 } 6507 6508 static int 6509 psmresume(device_t dev) 6510 { 6511 6512 struct psm_softc *sc = device_get_softc(dev); 6513 int unit = device_get_unit(dev); 6514 int err; 6515 6516 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit)); 6517 6518 if ((sc->config & 6519 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0) 6520 return (0); 6521 6522 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 6523 6524 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 6525 /* 6526 * Release the blocked process; it must be notified that 6527 * the device cannot be accessed anymore. 6528 */ 6529 sc->state &= ~PSM_ASLP; 6530 wakeup(sc); 6531 } 6532 6533 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit)); 6534 6535 return (err); 6536 } 6537 6538 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 6539 6540 #ifdef DEV_ISA 6541 #if 0 6542 /* 6543 * This sucks up assignments from PNPBIOS and ACPI. 6544 */ 6545 6546 /* 6547 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 6548 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 6549 * can be probed and attached only after the AT keyboard controller is 6550 * attached, we shall quietly reserve the IRQ resource for later use. 6551 * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 6552 * copy the IRQ resource to the PS/2 mouse device instance hanging 6553 * under the keyboard controller, then probe and attach it. 6554 */ 6555 6556 static devclass_t psmcpnp_devclass; 6557 6558 static device_probe_t psmcpnp_probe; 6559 static device_attach_t psmcpnp_attach; 6560 6561 static device_method_t psmcpnp_methods[] = { 6562 DEVMETHOD(device_probe, psmcpnp_probe), 6563 DEVMETHOD(device_attach, psmcpnp_attach), 6564 6565 { 0, 0 } 6566 }; 6567 6568 static driver_t psmcpnp_driver = { 6569 PSMCPNP_DRIVER_NAME, 6570 psmcpnp_methods, 6571 1, /* no softc */ 6572 }; 6573 6574 static struct isa_pnp_id psmcpnp_ids[] = { 6575 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 6576 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */ 6577 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */ 6578 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 6579 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 6580 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */ 6581 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */ 6582 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 6583 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 6584 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */ 6585 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */ 6586 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */ 6587 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */ 6588 { 0 } 6589 }; 6590 6591 static int 6592 create_a_copy(device_t atkbdc, device_t me) 6593 { 6594 6595 device_t psm; 6596 u_long irq; 6597 6598 /* find the PS/2 mouse device instance under the keyboard controller */ 6599 psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 6600 device_get_unit(atkbdc)); 6601 if (psm == NULL) 6602 return (ENXIO); 6603 if (device_get_state(psm) != DS_NOTPRESENT) 6604 return (0); 6605 6606 /* move our resource to the found device */ 6607 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 6608 bus_delete_resource(me, SYS_RES_IRQ, 0); 6609 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1, 6610 machintr_legacy_intr_cpuid(irq)); 6611 6612 /* ...then probe and attach it */ 6613 return (device_probe_and_attach(psm)); 6614 } 6615 6616 static int 6617 psmcpnp_probe(device_t dev) 6618 { 6619 struct resource *res; 6620 u_long irq; 6621 int rid; 6622 6623 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids)) 6624 return (ENXIO); 6625 6626 /* 6627 * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 6628 * to the PS/2 mouse device node. But, some buggy PnP BIOS 6629 * declares the PS/2 mouse device node without an IRQ resource! 6630 * If this happens, we shall refer to device hints. 6631 * If we still don't find it there, use a hardcoded value... XXX 6632 */ 6633 rid = 0; 6634 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 6635 if (irq <= 0) { 6636 if (resource_long_value(PSM_DRIVER_NAME, 6637 device_get_unit(dev),"irq", &irq) != 0) 6638 irq = 12; /* XXX */ 6639 device_printf(dev, "irq resource info is missing; " 6640 "assuming irq %ld\n", irq); 6641 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1, 6642 machintr_legacy_intr_cpuid(irq)); 6643 } 6644 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE); 6645 bus_release_resource(dev, SYS_RES_IRQ, rid, res); 6646 6647 /* keep quiet */ 6648 if (!bootverbose) 6649 device_quiet(dev); 6650 6651 return ((res == NULL) ? ENXIO : 0); 6652 } 6653 6654 static int 6655 psmcpnp_attach(device_t dev) 6656 { 6657 device_t atkbdc; 6658 6659 /* find the keyboard controller, which may be on acpi* or isa* bus */ 6660 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 6661 device_get_unit(dev)); 6662 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) { 6663 create_a_copy(atkbdc, dev); 6664 } else { 6665 /* 6666 * If we don't have the AT keyboard controller yet, 6667 * just reserve the IRQ for later use... 6668 * (See psmidentify() above.) 6669 */ 6670 rid = 0; 6671 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_SHAREABLE); 6672 } 6673 6674 return (0); 6675 } 6676 6677 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, NULL, NULL); 6678 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, NULL, NULL); 6679 6680 #endif 6681 #endif /* DEV_ISA */ 6682