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