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; 2754 2755 /* TouchPad PS/2 absolute mode message format with capFourButtons: 2756 * 2757 * Bits: 7 6 5 4 3 2 1 0 (LSB) 2758 * ------------------------------------------------ 2759 * ipacket[0]: 1 0 W3 W2 0 W1 R L 2760 * ipacket[1]: Yb Ya Y9 Y8 Xb Xa X9 X8 2761 * ipacket[2]: Z7 Z6 Z5 Z4 Z3 Z2 Z1 Z0 2762 * ipacket[3]: 1 1 Yc Xc 0 W0 D^R U^L 2763 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 2764 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 2765 * 2766 * Legend: 2767 * L: left physical mouse button 2768 * R: right physical mouse button 2769 * D: down button 2770 * U: up button 2771 * W: "wrist" value 2772 * X: x position 2773 * Y: y position 2774 * Z: pressure 2775 * 2776 * Without capFourButtons but with nExtendeButtons and/or capMiddle 2777 * 2778 * Bits: 7 6 5 4 3 2 1 0 (LSB) 2779 * ------------------------------------------------------ 2780 * ipacket[3]: 1 1 Yc Xc 0 W0 E^R M^L 2781 * ipacket[4]: X7 X6 X5 X4 X3|b7 X2|b5 X1|b3 X0|b1 2782 * ipacket[5]: Y7 Y6 Y5 Y4 Y3|b8 Y2|b6 Y1|b4 Y0|b2 2783 * 2784 * Legend: 2785 * M: Middle physical mouse button 2786 * E: Extended mouse buttons reported instead of low bits of X and Y 2787 * b1-b8: Extended mouse buttons 2788 * Only ((nExtendedButtons + 1) >> 1) bits are used in packet 2789 * 4 and 5, for reading X and Y value they should be zeroed. 2790 * 2791 * Absolute reportable limits: 0 - 6143. 2792 * Typical bezel limits: 1472 - 5472. 2793 * Typical edge marings: 1632 - 5312. 2794 * 2795 * w = 3 Passthrough Packet 2796 * 2797 * Byte 2,5,6 == Byte 1,2,3 of "Guest" 2798 */ 2799 2800 if (!synaptics_support) 2801 return (0); 2802 2803 /* Sanity check for out of sync packets. */ 2804 if ((pb->ipacket[0] & 0xc8) != 0x80 || 2805 (pb->ipacket[3] & 0xc8) != 0xc0) 2806 return (-1); 2807 2808 *x = *y = 0; 2809 ms->button = ms->obutton; 2810 2811 /* 2812 * Pressure value. 2813 * Interpretation: 2814 * z = 0 No finger contact 2815 * z = 10 Finger hovering near the pad 2816 * z = 30 Very light finger contact 2817 * z = 80 Normal finger contact 2818 * z = 110 Very heavy finger contact 2819 * z = 200 Finger lying flat on pad surface 2820 * z = 255 Maximum reportable Z 2821 */ 2822 *z = pb->ipacket[2]; 2823 2824 /* 2825 * Finger width value 2826 * Interpretation: 2827 * w = 0 Two finger on the pad (capMultiFinger needed) 2828 * w = 1 Three or more fingers (capMultiFinger needed) 2829 * w = 2 Pen (instead of finger) (capPen needed) 2830 * w = 3 Reserved (passthrough?) 2831 * w = 4-7 Finger of normal width (capPalmDetect needed) 2832 * w = 8-14 Very wide finger or palm (capPalmDetect needed) 2833 * w = 15 Maximum reportable width (capPalmDetect needed) 2834 */ 2835 /* XXX Is checking capExtended enough? */ 2836 if (sc->synhw.capExtended) 2837 w = ((pb->ipacket[0] & 0x30) >> 2) | 2838 ((pb->ipacket[0] & 0x04) >> 1) | 2839 ((pb->ipacket[3] & 0x04) >> 2); 2840 else { 2841 /* Assume a finger of regular width. */ 2842 w = 4; 2843 } 2844 2845 switch (w) { 2846 case 3: 2847 /* 2848 * Handle packets from the guest device. See: 2849 * Synaptics PS/2 TouchPad Interfacing Guide, Section 5.1 2850 */ 2851 if (sc->synhw.capPassthrough) { 2852 *x = ((pb->ipacket[1] & 0x10) ? 2853 pb->ipacket[4] - 256 : pb->ipacket[4]); 2854 *y = ((pb->ipacket[1] & 0x20) ? 2855 pb->ipacket[5] - 256 : pb->ipacket[5]); 2856 *z = 0; 2857 2858 guest_buttons = 0; 2859 if (pb->ipacket[1] & 0x01) 2860 guest_buttons |= MOUSE_BUTTON1DOWN; 2861 if (pb->ipacket[1] & 0x04) 2862 guest_buttons |= MOUSE_BUTTON2DOWN; 2863 if (pb->ipacket[1] & 0x02) 2864 guest_buttons |= MOUSE_BUTTON3DOWN; 2865 2866 ms->button = touchpad_buttons | guest_buttons; 2867 } 2868 goto SYNAPTICS_END; 2869 2870 case 2: 2871 /* Handle Extended W mode packets */ 2872 ewcode = (pb->ipacket[5] & 0xf0) >> 4; 2873 #if PSM_FINGERS > 1 2874 switch (ewcode) { 2875 case 1: 2876 /* Secondary finger */ 2877 if (sc->synhw.capAdvancedGestures) 2878 f[1] = (finger_t) { 2879 .x = (((pb->ipacket[4] & 0x0f) << 8) | 2880 pb->ipacket[1]) << 1, 2881 .y = (((pb->ipacket[4] & 0xf0) << 4) | 2882 pb->ipacket[2]) << 1, 2883 .p = ((pb->ipacket[3] & 0x30) | 2884 (pb->ipacket[5] & 0x0f)) << 1, 2885 .w = PSM_FINGER_DEFAULT_W, 2886 .flags = PSM_FINGER_FUZZY, 2887 }; 2888 else if (sc->synhw.capReportsV) 2889 f[1] = (finger_t) { 2890 .x = (((pb->ipacket[4] & 0x0f) << 8) | 2891 (pb->ipacket[1] & 0xfe)) << 1, 2892 .y = (((pb->ipacket[4] & 0xf0) << 4) | 2893 (pb->ipacket[2] & 0xfe)) << 1, 2894 .p = ((pb->ipacket[3] & 0x30) | 2895 (pb->ipacket[5] & 0x0e)) << 1, 2896 .w = (((pb->ipacket[5] & 0x01) << 2) | 2897 ((pb->ipacket[2] & 0x01) << 1) | 2898 (pb->ipacket[1] & 0x01)) + 8, 2899 .flags = PSM_FINGER_FUZZY, 2900 }; 2901 default: 2902 break; 2903 } 2904 #endif 2905 goto SYNAPTICS_END; 2906 2907 case 1: 2908 case 0: 2909 nfingers = w + 2; 2910 break; 2911 2912 default: 2913 nfingers = 1; 2914 } 2915 2916 if (sc->syninfo.touchpad_off) 2917 goto SYNAPTICS_END; 2918 2919 /* Button presses */ 2920 touchpad_buttons = 0; 2921 if (pb->ipacket[0] & 0x01) 2922 touchpad_buttons |= MOUSE_BUTTON1DOWN; 2923 if (pb->ipacket[0] & 0x02) 2924 touchpad_buttons |= MOUSE_BUTTON3DOWN; 2925 2926 if (sc->synhw.capExtended && sc->synhw.capFourButtons) { 2927 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x01) 2928 touchpad_buttons |= MOUSE_BUTTON4DOWN; 2929 if ((pb->ipacket[3] ^ pb->ipacket[0]) & 0x02) 2930 touchpad_buttons |= MOUSE_BUTTON5DOWN; 2931 } else if (sc->synhw.capExtended && sc->synhw.capMiddle && 2932 !sc->synhw.capClickPad) { 2933 /* Middle Button */ 2934 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01) 2935 touchpad_buttons |= MOUSE_BUTTON2DOWN; 2936 } else if (sc->synhw.capExtended && sc->synhw.capClickPad) { 2937 /* ClickPad Button */ 2938 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x01) 2939 touchpad_buttons = MOUSE_BUTTON1DOWN; 2940 } else if (sc->synhw.capExtended && (sc->synhw.nExtendedButtons > 0)) { 2941 /* Extended Buttons */ 2942 if ((pb->ipacket[0] ^ pb->ipacket[3]) & 0x02) { 2943 if (sc->syninfo.directional_scrolls) { 2944 if (pb->ipacket[4] & 0x01) 2945 touchpad_buttons |= MOUSE_BUTTON4DOWN; 2946 if (pb->ipacket[5] & 0x01) 2947 touchpad_buttons |= MOUSE_BUTTON5DOWN; 2948 if (pb->ipacket[4] & 0x02) 2949 touchpad_buttons |= MOUSE_BUTTON6DOWN; 2950 if (pb->ipacket[5] & 0x02) 2951 touchpad_buttons |= MOUSE_BUTTON7DOWN; 2952 } else { 2953 if (pb->ipacket[4] & 0x01) 2954 touchpad_buttons |= MOUSE_BUTTON1DOWN; 2955 if (pb->ipacket[5] & 0x01) 2956 touchpad_buttons |= MOUSE_BUTTON3DOWN; 2957 if (pb->ipacket[4] & 0x02) 2958 touchpad_buttons |= MOUSE_BUTTON2DOWN; 2959 sc->extended_buttons = touchpad_buttons; 2960 } 2961 2962 /* 2963 * Zero out bits used by extended buttons to avoid 2964 * misinterpretation of the data absolute position. 2965 * 2966 * The bits represented by 2967 * 2968 * (nExtendedButtons + 1) >> 1 2969 * 2970 * will be masked out in both bytes. 2971 * The mask for n bits is computed with the formula 2972 * 2973 * (1 << n) - 1 2974 */ 2975 int maskedbits = 0; 2976 int mask = 0; 2977 maskedbits = (sc->synhw.nExtendedButtons + 1) >> 1; 2978 mask = (1 << maskedbits) - 1; 2979 pb->ipacket[4] &= ~(mask); 2980 pb->ipacket[5] &= ~(mask); 2981 } else if (!sc->syninfo.directional_scrolls && 2982 !sc->gesture.in_vscroll) { 2983 /* 2984 * Keep reporting MOUSE DOWN until we get a new packet 2985 * indicating otherwise. 2986 */ 2987 touchpad_buttons |= sc->extended_buttons; 2988 } 2989 } 2990 2991 if (sc->synhw.capReportsV && nfingers > 1) 2992 f[0] = (finger_t) { 2993 .x = ((pb->ipacket[3] & 0x10) << 8) | 2994 ((pb->ipacket[1] & 0x0f) << 8) | 2995 (pb->ipacket[4] & 0xfd), 2996 .y = ((pb->ipacket[3] & 0x20) << 7) | 2997 ((pb->ipacket[1] & 0xf0) << 4) | 2998 (pb->ipacket[5] & 0xfd), 2999 .p = *z & 0xfe, 3000 .w = (((pb->ipacket[2] & 0x01) << 2) | 3001 (pb->ipacket[5] & 0x02) | 3002 ((pb->ipacket[4] & 0x02) >> 1)) + 8, 3003 .flags = PSM_FINGER_FUZZY, 3004 }; 3005 else 3006 f[0] = (finger_t) { 3007 .x = ((pb->ipacket[3] & 0x10) << 8) | 3008 ((pb->ipacket[1] & 0x0f) << 8) | 3009 pb->ipacket[4], 3010 .y = ((pb->ipacket[3] & 0x20) << 7) | 3011 ((pb->ipacket[1] & 0xf0) << 4) | 3012 pb->ipacket[5], 3013 .p = *z, 3014 .w = w, 3015 .flags = nfingers > 1 ? PSM_FINGER_FUZZY : 0, 3016 }; 3017 3018 /* Ignore hovering and unmeasurable touches */ 3019 if (f[0].p < sc->syninfo.min_pressure || f[0].x < 2) 3020 nfingers = 0; 3021 3022 for (id = 0; id < PSM_FINGERS; id++) 3023 if (id >= nfingers) 3024 PSM_FINGER_RESET(f[id]); 3025 3026 ms->button = touchpad_buttons | guest_buttons; 3027 3028 /* Palm detection doesn't terminate the current action. */ 3029 if (!psmpalmdetect(sc, &f[0], nfingers)) { 3030 psmgestures(sc, &f[0], nfingers, ms); 3031 for (id = 0; id < PSM_FINGERS; id++) 3032 psmsmoother(sc, &f[id], id, ms, x, y); 3033 } else { 3034 VLOG(2, (LOG_DEBUG, "synaptics: palm detected! (%d)\n", f[0].w)); 3035 } 3036 3037 SYNAPTICS_END: 3038 /* 3039 * Use the extra buttons as a scrollwheel 3040 * 3041 * XXX X.Org uses the Z axis for vertical wheel only, 3042 * whereas moused(8) understands special values to differ 3043 * vertical and horizontal wheels. 3044 * 3045 * xf86-input-mouse needs therefore a small patch to 3046 * understand these special values. Without it, the 3047 * horizontal wheel acts as a vertical wheel in X.Org. 3048 * 3049 * That's why the horizontal wheel is disabled by 3050 * default for now. 3051 */ 3052 if (ms->button & MOUSE_BUTTON4DOWN) 3053 *z = -1; 3054 else if (ms->button & MOUSE_BUTTON5DOWN) 3055 *z = 1; 3056 else if (ms->button & MOUSE_BUTTON6DOWN) 3057 *z = -2; 3058 else if (ms->button & MOUSE_BUTTON7DOWN) 3059 *z = 2; 3060 else 3061 *z = 0; 3062 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN | 3063 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN); 3064 3065 return (0); 3066 } 3067 3068 static int 3069 psmpalmdetect(struct psm_softc *sc, finger_t *f, int nfingers) 3070 { 3071 if (!( 3072 ((sc->synhw.capMultiFinger || 3073 sc->synhw.capAdvancedGestures) && nfingers > 1) || 3074 (sc->synhw.capPalmDetect && f->w <= sc->syninfo.max_width) || 3075 (!sc->synhw.capPalmDetect && f->p <= sc->syninfo.max_pressure) || 3076 (sc->synhw.capPen && f->flags & PSM_FINGER_IS_PEN))) { 3077 /* 3078 * We consider the packet irrelevant for the current 3079 * action when: 3080 * - the width isn't comprised in: 3081 * [1; max_width] 3082 * - the pressure isn't comprised in: 3083 * [min_pressure; max_pressure] 3084 * - pen aren't supported but PSM_FINGER_IS_PEN is set 3085 */ 3086 return (1); 3087 } 3088 return (0); 3089 } 3090 3091 static void 3092 psmgestures(struct psm_softc *sc, finger_t *fingers, int nfingers, 3093 mousestatus_t *ms) 3094 { 3095 smoother_t *smoother; 3096 gesture_t *gest; 3097 finger_t *f; 3098 int y_ok, center_button, center_x, right_button, right_x, i; 3099 3100 f = &fingers[0]; 3101 smoother = &sc->smoother[0]; 3102 gest = &sc->gesture; 3103 3104 /* Find first active finger. */ 3105 if (nfingers > 0) { 3106 for (i = 0; i < PSM_FINGERS; i++) { 3107 if (PSM_FINGER_IS_SET(fingers[i])) { 3108 f = &fingers[i]; 3109 smoother = &sc->smoother[i]; 3110 break; 3111 } 3112 } 3113 } 3114 3115 /* 3116 * Check pressure to detect a real wanted action on the 3117 * touchpad. 3118 */ 3119 if (f->p >= sc->syninfo.min_pressure) { 3120 int x0, y0; 3121 int dxp, dyp; 3122 int start_x, start_y; 3123 int queue_len; 3124 int margin_top, margin_right, margin_bottom, margin_left; 3125 int window_min, window_max; 3126 int vscroll_hor_area, vscroll_ver_area; 3127 int two_finger_scroll; 3128 int max_x, max_y; 3129 3130 /* Read sysctl. */ 3131 /* XXX Verify values? */ 3132 margin_top = sc->syninfo.margin_top; 3133 margin_right = sc->syninfo.margin_right; 3134 margin_bottom = sc->syninfo.margin_bottom; 3135 margin_left = sc->syninfo.margin_left; 3136 window_min = sc->syninfo.window_min; 3137 window_max = sc->syninfo.window_max; 3138 vscroll_hor_area = sc->syninfo.vscroll_hor_area; 3139 vscroll_ver_area = sc->syninfo.vscroll_ver_area; 3140 two_finger_scroll = sc->syninfo.two_finger_scroll; 3141 max_x = sc->syninfo.max_x; 3142 max_y = sc->syninfo.max_y; 3143 3144 /* Read current absolute position. */ 3145 x0 = f->x; 3146 y0 = f->y; 3147 3148 /* 3149 * Limit the coordinates to the specified margins because 3150 * this area isn't very reliable. 3151 */ 3152 if (x0 <= margin_left) 3153 x0 = margin_left; 3154 else if (x0 >= max_x - margin_right) 3155 x0 = max_x - margin_right; 3156 if (y0 <= margin_bottom) 3157 y0 = margin_bottom; 3158 else if (y0 >= max_y - margin_top) 3159 y0 = max_y - margin_top; 3160 3161 VLOG(3, (LOG_DEBUG, "synaptics: ipacket: [%d, %d], %d, %d\n", 3162 x0, y0, f->p, f->w)); 3163 3164 /* 3165 * If the action is just beginning, init the structure and 3166 * compute tap timeout. 3167 */ 3168 if (!(sc->flags & PSM_FLAGS_FINGERDOWN)) { 3169 VLOG(3, (LOG_DEBUG, "synaptics: ----\n")); 3170 3171 /* Initialize queue. */ 3172 gest->window_min = window_min; 3173 3174 /* Reset pressure peak. */ 3175 gest->zmax = 0; 3176 3177 /* Reset fingers count. */ 3178 gest->fingers_nb = 0; 3179 3180 /* Reset virtual scrolling state. */ 3181 gest->in_vscroll = 0; 3182 3183 /* Compute tap timeout. */ 3184 gest->taptimeout.tv_sec = tap_timeout / 1000000; 3185 gest->taptimeout.tv_usec = tap_timeout % 1000000; 3186 timevaladd(&gest->taptimeout, &sc->lastsoftintr); 3187 3188 sc->flags |= PSM_FLAGS_FINGERDOWN; 3189 3190 /* Smoother has not been reset yet */ 3191 queue_len = 1; 3192 start_x = x0; 3193 start_y = y0; 3194 } else { 3195 queue_len = smoother->queue_len + 1; 3196 start_x = smoother->start_x; 3197 start_y = smoother->start_y; 3198 } 3199 3200 /* Process ClickPad softbuttons */ 3201 if (sc->synhw.capClickPad && ms->button & MOUSE_BUTTON1DOWN) { 3202 y_ok = sc->syninfo.softbuttons_y >= 0 ? 3203 start_y < sc->syninfo.softbuttons_y : 3204 start_y > max_y - sc->syninfo.softbuttons_y; 3205 3206 center_button = MOUSE_BUTTON2DOWN; 3207 center_x = sc->syninfo.softbutton2_x; 3208 right_button = MOUSE_BUTTON3DOWN; 3209 right_x = sc->syninfo.softbutton3_x; 3210 3211 if (center_x > 0 && right_x > 0 && center_x > right_x) { 3212 center_button = MOUSE_BUTTON3DOWN; 3213 center_x = sc->syninfo.softbutton3_x; 3214 right_button = MOUSE_BUTTON2DOWN; 3215 right_x = sc->syninfo.softbutton2_x; 3216 } 3217 3218 if (right_x > 0 && start_x > right_x && y_ok) 3219 ms->button = (ms->button & 3220 ~MOUSE_BUTTON1DOWN) | right_button; 3221 else if (center_x > 0 && start_x > center_x && y_ok) 3222 ms->button = (ms->button & 3223 ~MOUSE_BUTTON1DOWN) | center_button; 3224 } 3225 3226 /* If in tap-hold, add the recorded button. */ 3227 if (gest->in_taphold) 3228 ms->button |= gest->tap_button; 3229 3230 /* 3231 * For tap, we keep the maximum number of fingers and the 3232 * pressure peak. Also with multiple fingers, we increase 3233 * the minimum window. 3234 */ 3235 if (nfingers > 1) 3236 gest->window_min = window_max; 3237 gest->fingers_nb = imax(nfingers, gest->fingers_nb); 3238 gest->zmax = imax(f->p, gest->zmax); 3239 3240 /* Do we have enough packets to consider this a gesture? */ 3241 if (queue_len < gest->window_min) 3242 return; 3243 3244 /* Is a scrolling action occurring? */ 3245 if (!gest->in_taphold && !ms->button && 3246 (!gest->in_vscroll || two_finger_scroll)) { 3247 /* 3248 * A scrolling action must not conflict with a tap 3249 * action. Here are the conditions to consider a 3250 * scrolling action: 3251 * - the action in a configurable area 3252 * - one of the following: 3253 * . the distance between the last packet and the 3254 * first should be above a configurable minimum 3255 * . tap timed out 3256 */ 3257 dxp = abs(x0 - start_x); 3258 dyp = abs(y0 - start_y); 3259 3260 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, >) || 3261 dxp >= sc->syninfo.vscroll_min_delta || 3262 dyp >= sc->syninfo.vscroll_min_delta) { 3263 /* 3264 * Handle two finger scrolling. 3265 * Note that we don't rely on fingers_nb 3266 * as that keeps the maximum number of fingers. 3267 */ 3268 if (two_finger_scroll) { 3269 if (nfingers == 2) { 3270 gest->in_vscroll += 3271 dyp ? 2 : 0; 3272 gest->in_vscroll += 3273 dxp ? 1 : 0; 3274 } 3275 } else { 3276 /* Check for horizontal scrolling. */ 3277 if ((vscroll_hor_area > 0 && 3278 start_y <= vscroll_hor_area) || 3279 (vscroll_hor_area < 0 && 3280 start_y >= 3281 max_y + vscroll_hor_area)) 3282 gest->in_vscroll += 2; 3283 3284 /* Check for vertical scrolling. */ 3285 if ((vscroll_ver_area > 0 && 3286 start_x <= vscroll_ver_area) || 3287 (vscroll_ver_area < 0 && 3288 start_x >= 3289 max_x + vscroll_ver_area)) 3290 gest->in_vscroll += 1; 3291 } 3292 3293 /* Avoid conflicts if area overlaps. */ 3294 if (gest->in_vscroll >= 3) 3295 gest->in_vscroll = 3296 (dxp > dyp) ? 2 : 1; 3297 } 3298 } 3299 /* 3300 * Reset two finger scrolling when the number of fingers 3301 * is different from two or any button is pressed. 3302 */ 3303 if (two_finger_scroll && gest->in_vscroll != 0 && 3304 (nfingers != 2 || ms->button)) 3305 gest->in_vscroll = 0; 3306 3307 VLOG(5, (LOG_DEBUG, 3308 "synaptics: virtual scrolling: %s " 3309 "(direction=%d, dxp=%d, dyp=%d, fingers=%d)\n", 3310 gest->in_vscroll ? "YES" : "NO", 3311 gest->in_vscroll, dxp, dyp, 3312 gest->fingers_nb)); 3313 3314 } else if (sc->flags & PSM_FLAGS_FINGERDOWN) { 3315 /* 3316 * An action is currently taking place but the pressure 3317 * dropped under the minimum, putting an end to it. 3318 */ 3319 int taphold_timeout, dx, dy, tap_max_delta; 3320 3321 dx = abs(smoother->queue[smoother->queue_cursor].x - 3322 smoother->start_x); 3323 dy = abs(smoother->queue[smoother->queue_cursor].y - 3324 smoother->start_y); 3325 3326 /* Max delta is disabled for multi-fingers tap. */ 3327 if (gest->fingers_nb > 1) 3328 tap_max_delta = imax(dx, dy); 3329 else 3330 tap_max_delta = sc->syninfo.tap_max_delta; 3331 3332 sc->flags &= ~PSM_FLAGS_FINGERDOWN; 3333 3334 /* Check for tap. */ 3335 VLOG(3, (LOG_DEBUG, 3336 "synaptics: zmax=%d, dx=%d, dy=%d, " 3337 "delta=%d, fingers=%d, queue=%d\n", 3338 gest->zmax, dx, dy, tap_max_delta, gest->fingers_nb, 3339 smoother->queue_len)); 3340 if (!gest->in_vscroll && gest->zmax >= tap_threshold && 3341 timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=) && 3342 dx <= tap_max_delta && dy <= tap_max_delta && 3343 smoother->queue_len >= sc->syninfo.tap_min_queue) { 3344 /* 3345 * We have a tap if: 3346 * - the maximum pressure went over tap_threshold 3347 * - the action ended before tap_timeout 3348 * 3349 * To handle tap-hold, we must delay any button push to 3350 * the next action. 3351 */ 3352 if (gest->in_taphold) { 3353 /* 3354 * This is the second and last tap of a 3355 * double tap action, not a tap-hold. 3356 */ 3357 gest->in_taphold = 0; 3358 3359 /* 3360 * For double-tap to work: 3361 * - no button press is emitted (to 3362 * simulate a button release) 3363 * - PSM_FLAGS_FINGERDOWN is set to 3364 * force the next packet to emit a 3365 * button press) 3366 */ 3367 VLOG(2, (LOG_DEBUG, 3368 "synaptics: button RELEASE: %d\n", 3369 gest->tap_button)); 3370 sc->flags |= PSM_FLAGS_FINGERDOWN; 3371 3372 /* Schedule button press on next interrupt */ 3373 sc->idletimeout.tv_sec = psmhz > 1 ? 3374 0 : 1; 3375 sc->idletimeout.tv_usec = psmhz > 1 ? 3376 1000000 / psmhz : 0; 3377 } else { 3378 /* 3379 * This is the first tap: we set the 3380 * tap-hold state and notify the button 3381 * down event. 3382 */ 3383 gest->in_taphold = 1; 3384 taphold_timeout = sc->syninfo.taphold_timeout; 3385 gest->taptimeout.tv_sec = taphold_timeout / 3386 1000000; 3387 gest->taptimeout.tv_usec = taphold_timeout % 3388 1000000; 3389 sc->idletimeout = gest->taptimeout; 3390 timevaladd(&gest->taptimeout, 3391 &sc->lastsoftintr); 3392 3393 switch (gest->fingers_nb) { 3394 case 3: 3395 gest->tap_button = 3396 MOUSE_BUTTON2DOWN; 3397 break; 3398 case 2: 3399 gest->tap_button = 3400 MOUSE_BUTTON3DOWN; 3401 break; 3402 default: 3403 gest->tap_button = 3404 MOUSE_BUTTON1DOWN; 3405 } 3406 VLOG(2, (LOG_DEBUG, 3407 "synaptics: button PRESS: %d\n", 3408 gest->tap_button)); 3409 ms->button |= gest->tap_button; 3410 } 3411 } else { 3412 /* 3413 * Not enough pressure or timeout: reset 3414 * tap-hold state. 3415 */ 3416 if (gest->in_taphold) { 3417 VLOG(2, (LOG_DEBUG, 3418 "synaptics: button RELEASE: %d\n", 3419 gest->tap_button)); 3420 gest->in_taphold = 0; 3421 } else { 3422 VLOG(2, (LOG_DEBUG, 3423 "synaptics: not a tap-hold\n")); 3424 } 3425 } 3426 } else if (!(sc->flags & PSM_FLAGS_FINGERDOWN) && gest->in_taphold) { 3427 /* 3428 * For a tap-hold to work, the button must remain down at 3429 * least until timeout (where the in_taphold flags will be 3430 * cleared) or during the next action. 3431 */ 3432 if (timevalcmp(&sc->lastsoftintr, &gest->taptimeout, <=)) { 3433 ms->button |= gest->tap_button; 3434 } else { 3435 VLOG(2, (LOG_DEBUG, "synaptics: button RELEASE: %d\n", 3436 gest->tap_button)); 3437 gest->in_taphold = 0; 3438 } 3439 } 3440 3441 return; 3442 } 3443 3444 static void 3445 psmsmoother(struct psm_softc *sc, finger_t *f, int smoother_id, 3446 mousestatus_t *ms, int *x, int *y) 3447 { 3448 smoother_t *smoother = &sc->smoother[smoother_id]; 3449 gesture_t *gest = &(sc->gesture); 3450 3451 /* 3452 * Check pressure to detect a real wanted action on the 3453 * touchpad. 3454 */ 3455 if (f->p >= sc->syninfo.min_pressure) { 3456 int x0, y0; 3457 int cursor, peer, window; 3458 int dx, dy, dxp, dyp; 3459 int max_width, max_pressure; 3460 int margin_top, margin_right, margin_bottom, margin_left; 3461 int na_top, na_right, na_bottom, na_left; 3462 int window_min, window_max; 3463 int multiplicator; 3464 int weight_current, weight_previous, weight_len_squared; 3465 int div_min, div_max, div_len; 3466 int vscroll_hor_area, vscroll_ver_area; 3467 int two_finger_scroll; 3468 int max_x, max_y; 3469 int len, weight_prev_x, weight_prev_y; 3470 int div_max_x, div_max_y, div_x, div_y; 3471 int is_fuzzy; 3472 3473 /* Read sysctl. */ 3474 /* XXX Verify values? */ 3475 max_width = sc->syninfo.max_width; 3476 max_pressure = sc->syninfo.max_pressure; 3477 margin_top = sc->syninfo.margin_top; 3478 margin_right = sc->syninfo.margin_right; 3479 margin_bottom = sc->syninfo.margin_bottom; 3480 margin_left = sc->syninfo.margin_left; 3481 na_top = sc->syninfo.na_top; 3482 na_right = sc->syninfo.na_right; 3483 na_bottom = sc->syninfo.na_bottom; 3484 na_left = sc->syninfo.na_left; 3485 window_min = sc->syninfo.window_min; 3486 window_max = sc->syninfo.window_max; 3487 multiplicator = sc->syninfo.multiplicator; 3488 weight_current = sc->syninfo.weight_current; 3489 weight_previous = sc->syninfo.weight_previous; 3490 weight_len_squared = sc->syninfo.weight_len_squared; 3491 div_min = sc->syninfo.div_min; 3492 div_max = sc->syninfo.div_max; 3493 div_len = sc->syninfo.div_len; 3494 vscroll_hor_area = sc->syninfo.vscroll_hor_area; 3495 vscroll_ver_area = sc->syninfo.vscroll_ver_area; 3496 two_finger_scroll = sc->syninfo.two_finger_scroll; 3497 max_x = sc->syninfo.max_x; 3498 max_y = sc->syninfo.max_y; 3499 3500 is_fuzzy = (f->flags & PSM_FINGER_FUZZY) != 0; 3501 3502 /* Read current absolute position. */ 3503 x0 = f->x; 3504 y0 = f->y; 3505 3506 /* 3507 * Limit the coordinates to the specified margins because 3508 * this area isn't very reliable. 3509 */ 3510 if (x0 <= margin_left) 3511 x0 = margin_left; 3512 else if (x0 >= max_x - margin_right) 3513 x0 = max_x - margin_right; 3514 if (y0 <= margin_bottom) 3515 y0 = margin_bottom; 3516 else if (y0 >= max_y - margin_top) 3517 y0 = max_y - margin_top; 3518 3519 /* If the action is just beginning, init the structure. */ 3520 if (smoother->active == 0) { 3521 VLOG(3, (LOG_DEBUG, "smoother%d: ---\n", smoother_id)); 3522 3523 /* Store the first point of this action. */ 3524 smoother->start_x = x0; 3525 smoother->start_y = y0; 3526 dx = dy = 0; 3527 3528 /* Initialize queue. */ 3529 smoother->queue_cursor = SYNAPTICS_PACKETQUEUE; 3530 smoother->queue_len = 0; 3531 3532 /* Reset average. */ 3533 smoother->avg_dx = 0; 3534 smoother->avg_dy = 0; 3535 3536 /* Reset squelch. */ 3537 smoother->squelch_x = 0; 3538 smoother->squelch_y = 0; 3539 3540 /* Activate queue */ 3541 smoother->active = 1; 3542 } else { 3543 /* Calculate the current delta. */ 3544 cursor = smoother->queue_cursor; 3545 dx = x0 - smoother->queue[cursor].x; 3546 dy = y0 - smoother->queue[cursor].y; 3547 } 3548 3549 VLOG(3, (LOG_DEBUG, "smoother%d: ipacket: [%d, %d], %d, %d\n", 3550 smoother_id, x0, y0, f->p, f->w)); 3551 3552 /* Queue this new packet. */ 3553 cursor = SYNAPTICS_QUEUE_CURSOR(smoother->queue_cursor - 1); 3554 smoother->queue[cursor].x = x0; 3555 smoother->queue[cursor].y = y0; 3556 smoother->queue_cursor = cursor; 3557 if (smoother->queue_len < SYNAPTICS_PACKETQUEUE) 3558 smoother->queue_len++; 3559 VLOG(5, (LOG_DEBUG, 3560 "smoother%d: cursor[%d]: x=%d, y=%d, dx=%d, dy=%d\n", 3561 smoother_id, cursor, x0, y0, dx, dy)); 3562 3563 /* Do we have enough packets to consider this a movement? */ 3564 if (smoother->queue_len < gest->window_min) 3565 return; 3566 3567 weight_prev_x = weight_prev_y = weight_previous; 3568 div_max_x = div_max_y = div_max; 3569 3570 if (gest->in_vscroll) { 3571 /* Dividers are different with virtual scrolling. */ 3572 div_min = sc->syninfo.vscroll_div_min; 3573 div_max_x = div_max_y = sc->syninfo.vscroll_div_max; 3574 } else { 3575 /* 3576 * There's a lot of noise in coordinates when 3577 * the finger is on the touchpad's borders. When 3578 * using this area, we apply a special weight and 3579 * div. 3580 */ 3581 if (x0 <= na_left || x0 >= max_x - na_right) { 3582 weight_prev_x = sc->syninfo.weight_previous_na; 3583 div_max_x = sc->syninfo.div_max_na; 3584 } 3585 3586 if (y0 <= na_bottom || y0 >= max_y - na_top) { 3587 weight_prev_y = sc->syninfo.weight_previous_na; 3588 div_max_y = sc->syninfo.div_max_na; 3589 } 3590 } 3591 3592 /* 3593 * Calculate weights for the average operands and 3594 * the divisor. Both depend on the distance between 3595 * the current packet and a previous one (based on the 3596 * window width). 3597 */ 3598 window = imin(smoother->queue_len, window_max); 3599 peer = SYNAPTICS_QUEUE_CURSOR(cursor + window - 1); 3600 dxp = abs(x0 - smoother->queue[peer].x) + 1; 3601 dyp = abs(y0 - smoother->queue[peer].y) + 1; 3602 len = (dxp * dxp) + (dyp * dyp); 3603 weight_prev_x = imin(weight_prev_x, 3604 weight_len_squared * weight_prev_x / len); 3605 weight_prev_y = imin(weight_prev_y, 3606 weight_len_squared * weight_prev_y / len); 3607 3608 len = (dxp + dyp) / 2; 3609 div_x = div_len * div_max_x / len; 3610 div_x = imin(div_max_x, div_x); 3611 div_x = imax(div_min, div_x); 3612 div_y = div_len * div_max_y / len; 3613 div_y = imin(div_max_y, div_y); 3614 div_y = imax(div_min, div_y); 3615 3616 VLOG(3, (LOG_DEBUG, 3617 "smoother%d: peer=%d, len=%d, weight=%d/%d, div=%d/%d\n", 3618 smoother_id, peer, len, weight_prev_x, weight_prev_y, 3619 div_x, div_y)); 3620 3621 /* Compute averages. */ 3622 smoother->avg_dx = 3623 (weight_current * dx * multiplicator + 3624 weight_prev_x * smoother->avg_dx) / 3625 (weight_current + weight_prev_x); 3626 3627 smoother->avg_dy = 3628 (weight_current * dy * multiplicator + 3629 weight_prev_y * smoother->avg_dy) / 3630 (weight_current + weight_prev_y); 3631 3632 VLOG(5, (LOG_DEBUG, 3633 "smoother%d: avg_dx~=%d, avg_dy~=%d\n", smoother_id, 3634 smoother->avg_dx / multiplicator, 3635 smoother->avg_dy / multiplicator)); 3636 3637 /* Use these averages to calculate x & y. */ 3638 smoother->squelch_x += smoother->avg_dx; 3639 dxp = smoother->squelch_x / (div_x * multiplicator); 3640 smoother->squelch_x = smoother->squelch_x % 3641 (div_x * multiplicator); 3642 3643 smoother->squelch_y += smoother->avg_dy; 3644 dyp = smoother->squelch_y / (div_y * multiplicator); 3645 smoother->squelch_y = smoother->squelch_y % 3646 (div_y * multiplicator); 3647 3648 switch(gest->in_vscroll) { 3649 case 0: /* Pointer movement. */ 3650 /* On real<->fuzzy finger switch the x/y pos jumps */ 3651 if (is_fuzzy == smoother->is_fuzzy) { 3652 *x += dxp; 3653 *y += dyp; 3654 } 3655 3656 VLOG(3, (LOG_DEBUG, "smoother%d: [%d, %d] -> [%d, %d]\n", 3657 smoother_id, dx, dy, dxp, dyp)); 3658 break; 3659 case 1: /* Vertical scrolling. */ 3660 if (dyp != 0) 3661 ms->button |= (dyp > 0) ? 3662 MOUSE_BUTTON4DOWN : MOUSE_BUTTON5DOWN; 3663 break; 3664 case 2: /* Horizontal scrolling. */ 3665 if (dxp != 0) 3666 ms->button |= (dxp > 0) ? 3667 MOUSE_BUTTON7DOWN : MOUSE_BUTTON6DOWN; 3668 break; 3669 } 3670 3671 smoother->is_fuzzy = is_fuzzy; 3672 3673 } else { 3674 /* 3675 * Deactivate queue. Note: We can not just reset queue here 3676 * as these values are still used by gesture processor. 3677 * So postpone reset till next touch. 3678 */ 3679 smoother->active = 0; 3680 } 3681 } 3682 3683 static int 3684 proc_elantech(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 3685 int *x, int *y, int *z) 3686 { 3687 static int touchpad_button, trackpoint_button; 3688 finger_t fn, f[ELANTECH_MAX_FINGERS]; 3689 int pkt, id, scale, i, nfingers, mask; 3690 3691 if (!elantech_support) 3692 return (0); 3693 3694 /* Determine packet format and do a sanity check for out of sync packets. */ 3695 if (ELANTECH_PKT_IS_DEBOUNCE(pb, sc->elanhw.hwversion)) 3696 pkt = ELANTECH_PKT_NOP; 3697 else if (ELANTECH_PKT_IS_TRACKPOINT(pb)) 3698 pkt = ELANTECH_PKT_TRACKPOINT; 3699 else 3700 switch (sc->elanhw.hwversion) { 3701 case 2: 3702 if (!ELANTECH_PKT_IS_V2(pb)) 3703 return (-1); 3704 3705 pkt = (pb->ipacket[0] & 0xc0) == 0x80 ? 3706 ELANTECH_PKT_V2_2FINGER : ELANTECH_PKT_V2_COMMON; 3707 break; 3708 case 3: 3709 if (!ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc) && 3710 !ELANTECH_PKT_IS_V3_TAIL(pb, sc->elanhw.hascrc)) 3711 return (-1); 3712 3713 pkt = ELANTECH_PKT_V3; 3714 break; 3715 case 4: 3716 if (!ELANTECH_PKT_IS_V4(pb, sc->elanhw.hascrc)) 3717 return (-1); 3718 3719 switch (pb->ipacket[3] & 0x03) { 3720 case 0x00: 3721 pkt = ELANTECH_PKT_V4_STATUS; 3722 break; 3723 case 0x01: 3724 pkt = ELANTECH_PKT_V4_HEAD; 3725 break; 3726 case 0x02: 3727 pkt = ELANTECH_PKT_V4_MOTION; 3728 break; 3729 default: 3730 return (-1); 3731 } 3732 break; 3733 default: 3734 return (-1); 3735 } 3736 3737 VLOG(5, (LOG_DEBUG, "elantech: ipacket format: %d\n", pkt)); 3738 3739 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 3740 PSM_FINGER_RESET(f[id]); 3741 3742 *x = *y = *z = 0; 3743 ms->button = ms->obutton; 3744 3745 if (sc->syninfo.touchpad_off) 3746 return (0); 3747 3748 /* Common legend 3749 * L: Left mouse button pressed 3750 * R: Right mouse button pressed 3751 * N: number of fingers on touchpad 3752 * X: absolute x value (horizontal) 3753 * Y: absolute y value (vertical) 3754 * W; width of the finger touch 3755 * P: pressure 3756 */ 3757 switch (pkt) { 3758 case ELANTECH_PKT_V2_COMMON: /* HW V2. One/Three finger touch */ 3759 /* 7 6 5 4 3 2 1 0 (LSB) 3760 * ------------------------------------------- 3761 * ipacket[0]: N1 N0 W3 W2 . . R L 3762 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 3763 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 3764 * ipacket[3]: N4 VF W1 W0 . . . B2 3765 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 3766 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3767 * ------------------------------------------- 3768 * N4: set if more than 3 fingers (only in 3 fingers mode) 3769 * VF: a kind of flag? (only on EF123, 0 when finger 3770 * is over one of the buttons, 1 otherwise) 3771 * B2: (on EF113 only, 0 otherwise), one button pressed 3772 * P & W is not reported on EF113 touchpads 3773 */ 3774 nfingers = (pb->ipacket[0] & 0xc0) >> 6; 3775 if (nfingers == 3 && (pb->ipacket[3] & 0x80)) 3776 nfingers = 4; 3777 mask = (1 << nfingers) - 1; 3778 3779 fn = ELANTECH_FINGER_SET_XYP(pb); 3780 if (sc->elanhw.haspressure) { 3781 fn.w = ((pb->ipacket[0] & 0x30) >> 2) | 3782 ((pb->ipacket[3] & 0x30) >> 4); 3783 } else { 3784 fn.p = PSM_FINGER_DEFAULT_P; 3785 fn.w = PSM_FINGER_DEFAULT_W; 3786 } 3787 3788 /* 3789 * HW v2 dont report exact finger positions when 3 or more 3790 * fingers are on touchpad. Use reported value as fingers 3791 * position as it is required for tap detection 3792 */ 3793 if (nfingers > 2) 3794 fn.flags = PSM_FINGER_FUZZY; 3795 3796 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++) 3797 f[id] = fn; 3798 break; 3799 3800 case ELANTECH_PKT_V2_2FINGER: /*HW V2. Two finger touch */ 3801 /* 7 6 5 4 3 2 1 0 (LSB) 3802 * ------------------------------------------- 3803 * ipacket[0]: N1 N0 AY8 AX8 . . R L 3804 * ipacket[1]: AX7 AX6 AX5 AX4 AX3 AX2 AX1 AX0 3805 * ipacket[2]: AY7 AY6 AY5 AY4 AY3 AY2 AY1 AY0 3806 * ipacket[3]: . . BY8 BX8 . . . . 3807 * ipacket[4]: BX7 BX6 BX5 BX4 BX3 BX2 BX1 BX0 3808 * ipacket[5]: BY7 BY6 BY5 BY4 BY3 BY2 BY1 BY0 3809 * ------------------------------------------- 3810 * AX: lower-left finger absolute x value 3811 * AY: lower-left finger absolute y value 3812 * BX: upper-right finger absolute x value 3813 * BY: upper-right finger absolute y value 3814 */ 3815 nfingers = 2; 3816 mask = (1 << nfingers) - 1; 3817 3818 for (id = 0; id < imin(2, ELANTECH_MAX_FINGERS); id ++) 3819 f[id] = (finger_t) { 3820 .x = (((pb->ipacket[id * 3] & 0x10) << 4) | 3821 pb->ipacket[id * 3 + 1]) << 2, 3822 .y = (((pb->ipacket[id * 3] & 0x20) << 3) | 3823 pb->ipacket[id * 3 + 2]) << 2, 3824 .p = PSM_FINGER_DEFAULT_P, 3825 .w = PSM_FINGER_DEFAULT_W, 3826 /* HW ver.2 sends bounding box */ 3827 .flags = PSM_FINGER_FUZZY 3828 }; 3829 break; 3830 3831 case ELANTECH_PKT_V3: /* HW Version 3 */ 3832 /* 7 6 5 4 3 2 1 0 (LSB) 3833 * ------------------------------------------- 3834 * ipacket[0]: N1 N0 W3 W2 0 1 R L 3835 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 3836 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 3837 * ipacket[3]: 0 0 W1 W0 0 0 1 0 3838 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 3839 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3840 * ------------------------------------------- 3841 */ 3842 nfingers = (pb->ipacket[0] & 0xc0) >> 6; 3843 mask = (1 << nfingers) - 1; 3844 id = nfingers - 1; 3845 3846 fn = ELANTECH_FINGER_SET_XYP(pb); 3847 fn.w = ((pb->ipacket[0] & 0x30) >> 2) | 3848 ((pb->ipacket[3] & 0x30) >> 4); 3849 3850 /* 3851 * HW v3 dont report exact finger positions when 3 or more 3852 * fingers are on touchpad. Use reported value as fingers 3853 * position as it is required for tap detection 3854 */ 3855 if (nfingers > 1) 3856 fn.flags = PSM_FINGER_FUZZY; 3857 3858 for (id = 0; id < imin(nfingers, ELANTECH_MAX_FINGERS); id++) 3859 f[id] = fn; 3860 3861 if (nfingers == 2) { 3862 if (ELANTECH_PKT_IS_V3_HEAD(pb, sc->elanhw.hascrc)) { 3863 sc->elanaction.fingers[0] = fn; 3864 return (0); 3865 } else 3866 f[0] = sc->elanaction.fingers[0]; 3867 } 3868 break; 3869 3870 case ELANTECH_PKT_V4_STATUS: /* HW Version 4. Status packet */ 3871 /* 7 6 5 4 3 2 1 0 (LSB) 3872 * ------------------------------------------- 3873 * ipacket[0]: . . . . 0 1 R L 3874 * ipacket[1]: . . . F4 F3 F2 F1 F0 3875 * ipacket[2]: . . . . . . . . 3876 * ipacket[3]: . . . 1 0 0 0 0 3877 * ipacket[4]: PL . . . . . . . 3878 * ipacket[5]: . . . . . . . . 3879 * ------------------------------------------- 3880 * Fn: finger n is on touchpad 3881 * PL: palm 3882 * HV ver4 sends a status packet to indicate that the numbers 3883 * or identities of the fingers has been changed 3884 */ 3885 3886 mask = pb->ipacket[1] & 0x1f; 3887 nfingers = bitcount(mask); 3888 3889 /* Skip "new finger is on touchpad" packets */ 3890 if ((sc->elanaction.mask & mask) == sc->elanaction.mask && 3891 (mask & ~sc->elanaction.mask)) { 3892 sc->elanaction.mask = mask; 3893 return (0); 3894 } 3895 3896 break; 3897 3898 case ELANTECH_PKT_V4_HEAD: /* HW Version 4. Head packet */ 3899 /* 7 6 5 4 3 2 1 0 (LSB) 3900 * ------------------------------------------- 3901 * ipacket[0]: W3 W2 W1 W0 0 1 R L 3902 * ipacket[1]: P7 P6 P5 P4 X11 X10 X9 X8 3903 * ipacket[2]: X7 X6 X5 X4 X3 X2 X1 X0 3904 * ipacket[3]: ID2 ID1 ID0 1 0 0 0 1 3905 * ipacket[4]: P3 P1 P2 P0 Y11 Y10 Y9 Y8 3906 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3907 * ------------------------------------------- 3908 * ID: finger id 3909 * HW ver 4 sends head packets in two cases: 3910 * 1. One finger touch and movement. 3911 * 2. Next after status packet to tell new finger positions. 3912 */ 3913 mask = sc->elanaction.mask; 3914 nfingers = bitcount(mask); 3915 id = ((pb->ipacket[3] & 0xe0) >> 5) - 1; 3916 3917 if (id >= 0 && id < ELANTECH_MAX_FINGERS) { 3918 f[id] = ELANTECH_FINGER_SET_XYP(pb); 3919 f[id].w = (pb->ipacket[0] & 0xf0) >> 4; 3920 } 3921 break; 3922 3923 case ELANTECH_PKT_V4_MOTION: /* HW Version 4. Motion packet */ 3924 /* 7 6 5 4 3 2 1 0 (LSB) 3925 * ------------------------------------------- 3926 * ipacket[0]: ID2 ID1 ID0 OF 0 1 R L 3927 * ipacket[1]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0 3928 * ipacket[2]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0 3929 * ipacket[3]: ID2 ID1 ID0 1 0 0 1 0 3930 * ipacket[4]: DX7 DX6 DX5 DX4 DX3 DX2 DX1 DX0 3931 * ipacket[5]: DY7 DY6 DY5 DY4 DY3 DY2 DY1 DY0 3932 * ------------------------------------------- 3933 * OF: delta overflows (> 127 or < -128), in this case 3934 * firmware sends us (delta x / 5) and (delta y / 5) 3935 * ID: finger id 3936 * DX: delta x (two's complement) 3937 * XY: delta y (two's complement) 3938 * byte 0 ~ 2 for one finger 3939 * byte 3 ~ 5 for another finger 3940 */ 3941 mask = sc->elanaction.mask; 3942 nfingers = bitcount(mask); 3943 3944 scale = (pb->ipacket[0] & 0x10) ? 5 : 1; 3945 for (i = 0; i <= 3; i += 3) { 3946 id = ((pb->ipacket[i] & 0xe0) >> 5) - 1; 3947 if (id < 0 || id >= ELANTECH_MAX_FINGERS) 3948 continue; 3949 3950 if (PSM_FINGER_IS_SET(sc->elanaction.fingers[id])) { 3951 f[id] = sc->elanaction.fingers[id]; 3952 f[id].x += imax(-f[id].x, 3953 (signed char)pb->ipacket[i+1] * scale); 3954 f[id].y += imax(-f[id].y, 3955 (signed char)pb->ipacket[i+2] * scale); 3956 } else { 3957 VLOG(3, (LOG_DEBUG, "elantech: " 3958 "HW v4 motion packet skipped\n")); 3959 } 3960 } 3961 3962 break; 3963 3964 case ELANTECH_PKT_TRACKPOINT: 3965 /* 7 6 5 4 3 2 1 0 (LSB) 3966 * ------------------------------------------- 3967 * ipacket[0]: 0 0 SX SY 0 M R L 3968 * ipacket[1]: ~SX 0 0 0 0 0 0 0 3969 * ipacket[2]: ~SY 0 0 0 0 0 0 0 3970 * ipacket[3]: 0 0 ~SY ~SX 0 1 1 0 3971 * ipacket[4]: X7 X6 X5 X4 X3 X2 X1 X0 3972 * ipacket[5]: Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 3973 * ------------------------------------------- 3974 * X and Y are written in two's complement spread 3975 * over 9 bits with SX/SY the relative top bit and 3976 * X7..X0 and Y7..Y0 the lower bits. 3977 */ 3978 *x = (pb->ipacket[0] & 0x20) ? 3979 pb->ipacket[4] - 256 : pb->ipacket[4]; 3980 *y = (pb->ipacket[0] & 0x10) ? 3981 pb->ipacket[5] - 256 : pb->ipacket[5]; 3982 3983 trackpoint_button = 3984 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) | 3985 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0) | 3986 ((pb->ipacket[0] & 0x04) ? MOUSE_BUTTON2DOWN : 0); 3987 3988 ms->button = touchpad_button | trackpoint_button; 3989 return (0); 3990 3991 case ELANTECH_PKT_NOP: 3992 return (0); 3993 3994 default: 3995 return (-1); 3996 } 3997 3998 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 3999 if (PSM_FINGER_IS_SET(f[id])) 4000 VLOG(2, (LOG_DEBUG, "elantech: " 4001 "finger %d: down [%d, %d], %d, %d, %d\n", id + 1, 4002 f[id].x, f[id].y, f[id].p, f[id].w, f[id].flags)); 4003 4004 /* Touchpad button presses */ 4005 if (sc->elanhw.isclickpad) { 4006 touchpad_button = 4007 ((pb->ipacket[0] & 0x03) ? MOUSE_BUTTON1DOWN : 0); 4008 } else { 4009 touchpad_button = 4010 ((pb->ipacket[0] & 0x01) ? MOUSE_BUTTON1DOWN : 0) | 4011 ((pb->ipacket[0] & 0x02) ? MOUSE_BUTTON3DOWN : 0); 4012 } 4013 4014 ms->button = touchpad_button | trackpoint_button; 4015 4016 /* Palm detection doesn't terminate the current action. */ 4017 if (!psmpalmdetect(sc, &f[0], nfingers)) { 4018 /* Send finger 1 position to gesture processor */ 4019 if (PSM_FINGER_IS_SET(f[0]) || PSM_FINGER_IS_SET(f[1]) || 4020 nfingers == 0) 4021 psmgestures(sc, &f[0], imin(nfingers, 3), ms); 4022 /* Send fingers positions to movement smoothers */ 4023 for (id = 0; id < PSM_FINGERS; id++) 4024 if (PSM_FINGER_IS_SET(f[id]) || !(mask & (1 << id))) 4025 psmsmoother(sc, &f[id], id, ms, x, y); 4026 } else { 4027 VLOG(2, (LOG_DEBUG, "elantech: palm detected! (%d)\n", 4028 f[0].w)); 4029 } 4030 4031 /* Store current finger positions in action context */ 4032 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) { 4033 if (PSM_FINGER_IS_SET(f[id])) 4034 sc->elanaction.fingers[id] = f[id]; 4035 if ((sc->elanaction.mask & (1 << id)) && !(mask & (1 << id))) 4036 PSM_FINGER_RESET(sc->elanaction.fingers[id]); 4037 } 4038 sc->elanaction.mask = mask; 4039 4040 /* Use the extra buttons as a scrollwheel */ 4041 if (ms->button & MOUSE_BUTTON4DOWN) 4042 *z = -1; 4043 else if (ms->button & MOUSE_BUTTON5DOWN) 4044 *z = 1; 4045 else if (ms->button & MOUSE_BUTTON6DOWN) 4046 *z = -2; 4047 else if (ms->button & MOUSE_BUTTON7DOWN) 4048 *z = 2; 4049 else 4050 *z = 0; 4051 ms->button &= ~(MOUSE_BUTTON4DOWN | MOUSE_BUTTON5DOWN | 4052 MOUSE_BUTTON6DOWN | MOUSE_BUTTON7DOWN); 4053 4054 return (0); 4055 } 4056 4057 static void 4058 proc_versapad(struct psm_softc *sc, packetbuf_t *pb, mousestatus_t *ms, 4059 int *x, int *y, int *z) 4060 { 4061 static int butmap_versapad[8] = { 4062 0, 4063 MOUSE_BUTTON3DOWN, 4064 0, 4065 MOUSE_BUTTON3DOWN, 4066 MOUSE_BUTTON1DOWN, 4067 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 4068 MOUSE_BUTTON1DOWN, 4069 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN 4070 }; 4071 int c, x0, y0; 4072 4073 /* VersaPad PS/2 absolute mode message format 4074 * 4075 * [packet1] 7 6 5 4 3 2 1 0(LSB) 4076 * ipacket[0]: 1 1 0 A 1 L T R 4077 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0 4078 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0 4079 * ipacket[3]: 1 1 1 A 1 L T R 4080 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8 4081 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0 4082 * 4083 * [note] 4084 * R: right physical mouse button (1=on) 4085 * T: touch pad virtual button (1=tapping) 4086 * L: left physical mouse button (1=on) 4087 * A: position data is valid (1=valid) 4088 * H: horizontal data (12bit signed integer. H11 is sign bit.) 4089 * V: vertical data (12bit signed integer. V11 is sign bit.) 4090 * P: pressure data 4091 * 4092 * Tapping is mapped to MOUSE_BUTTON4. 4093 */ 4094 c = pb->ipacket[0]; 4095 *x = *y = 0; 4096 ms->button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS]; 4097 ms->button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0; 4098 if (c & MOUSE_PS2VERSA_IN_USE) { 4099 x0 = pb->ipacket[1] | (((pb->ipacket[4]) & 0x0f) << 8); 4100 y0 = pb->ipacket[2] | (((pb->ipacket[4]) & 0xf0) << 4); 4101 if (x0 & 0x800) 4102 x0 -= 0x1000; 4103 if (y0 & 0x800) 4104 y0 -= 0x1000; 4105 if (sc->flags & PSM_FLAGS_FINGERDOWN) { 4106 *x = sc->xold - x0; 4107 *y = y0 - sc->yold; 4108 if (*x < 0) /* XXX */ 4109 ++*x; 4110 else if (*x) 4111 --*x; 4112 if (*y < 0) 4113 ++*y; 4114 else if (*y) 4115 --*y; 4116 } else 4117 sc->flags |= PSM_FLAGS_FINGERDOWN; 4118 sc->xold = x0; 4119 sc->yold = y0; 4120 } else 4121 sc->flags &= ~PSM_FLAGS_FINGERDOWN; 4122 } 4123 4124 static void 4125 psmsoftintridle(void *arg) 4126 { 4127 struct psm_softc *sc = arg; 4128 packetbuf_t *pb; 4129 4130 /* Invoke soft handler only when pqueue is empty. Otherwise it will be 4131 * invoked from psmintr soon with pqueue filled with real data */ 4132 if (sc->pqueue_start == sc->pqueue_end && 4133 sc->idlepacket.inputbytes > 0) { 4134 /* Grow circular queue backwards to avoid race with psmintr */ 4135 if (--sc->pqueue_start < 0) 4136 sc->pqueue_start = PSM_PACKETQUEUE - 1; 4137 4138 pb = &sc->pqueue[sc->pqueue_start]; 4139 memcpy(pb, &sc->idlepacket, sizeof(packetbuf_t)); 4140 VLOG(4, (LOG_DEBUG, 4141 "psmsoftintridle: %02x %02x %02x %02x %02x %02x\n", 4142 pb->ipacket[0], pb->ipacket[1], pb->ipacket[2], 4143 pb->ipacket[3], pb->ipacket[4], pb->ipacket[5])); 4144 4145 psmsoftintr(arg); 4146 } 4147 } 4148 4149 static void 4150 psmsoftintr(void *arg) 4151 { 4152 /* 4153 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN) 4154 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN). 4155 */ 4156 static int butmap[8] = { 4157 0, 4158 MOUSE_BUTTON1DOWN, 4159 MOUSE_BUTTON3DOWN, 4160 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 4161 MOUSE_BUTTON2DOWN, 4162 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN, 4163 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 4164 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN 4165 }; 4166 struct psm_softc *sc = arg; 4167 mousestatus_t ms; 4168 packetbuf_t *pb; 4169 int x, y, z, c, l, s; 4170 4171 getmicrouptime(&sc->lastsoftintr); 4172 4173 s = spltty(); 4174 4175 do { 4176 pb = &sc->pqueue[sc->pqueue_start]; 4177 4178 if (sc->mode.level == PSM_LEVEL_NATIVE) 4179 goto next_native; 4180 4181 c = pb->ipacket[0]; 4182 /* 4183 * A kludge for Kensington device! 4184 * The MSB of the horizontal count appears to be stored in 4185 * a strange place. 4186 */ 4187 if (sc->hw.model == MOUSE_MODEL_THINK) 4188 pb->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0; 4189 4190 /* ignore the overflow bits... */ 4191 x = (c & MOUSE_PS2_XNEG) ? 4192 pb->ipacket[1] - 256 : pb->ipacket[1]; 4193 y = (c & MOUSE_PS2_YNEG) ? 4194 pb->ipacket[2] - 256 : pb->ipacket[2]; 4195 z = 0; 4196 ms.obutton = sc->button; /* previous button state */ 4197 ms.button = butmap[c & MOUSE_PS2_BUTTONS]; 4198 /* `tapping' action */ 4199 if (sc->config & PSM_CONFIG_FORCETAP) 4200 ms.button |= ((c & MOUSE_PS2_TAP)) ? 4201 0 : MOUSE_BUTTON4DOWN; 4202 timevalclear(&sc->idletimeout); 4203 sc->idlepacket.inputbytes = 0; 4204 4205 switch (sc->hw.model) { 4206 4207 case MOUSE_MODEL_EXPLORER: 4208 /* 4209 * b7 b6 b5 b4 b3 b2 b1 b0 4210 * byte 1: oy ox sy sx 1 M R L 4211 * byte 2: x x x x x x x x 4212 * byte 3: y y y y y y y y 4213 * byte 4: * * S2 S1 s d2 d1 d0 4214 * 4215 * L, M, R, S1, S2: left, middle, right and side buttons 4216 * s: wheel data sign bit 4217 * d2-d0: wheel data 4218 */ 4219 z = (pb->ipacket[3] & MOUSE_EXPLORER_ZNEG) ? 4220 (pb->ipacket[3] & 0x0f) - 16 : 4221 (pb->ipacket[3] & 0x0f); 4222 ms.button |= 4223 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN) ? 4224 MOUSE_BUTTON4DOWN : 0; 4225 ms.button |= 4226 (pb->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN) ? 4227 MOUSE_BUTTON5DOWN : 0; 4228 break; 4229 4230 case MOUSE_MODEL_INTELLI: 4231 case MOUSE_MODEL_NET: 4232 /* wheel data is in the fourth byte */ 4233 z = (char)pb->ipacket[3]; 4234 /* 4235 * XXX some mice may send 7 when there is no Z movement? */ 4236 if ((z >= 7) || (z <= -7)) 4237 z = 0; 4238 /* some compatible mice have additional buttons */ 4239 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN) ? 4240 MOUSE_BUTTON4DOWN : 0; 4241 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN) ? 4242 MOUSE_BUTTON5DOWN : 0; 4243 break; 4244 4245 case MOUSE_MODEL_MOUSEMANPLUS: 4246 proc_mmanplus(sc, pb, &ms, &x, &y, &z); 4247 break; 4248 4249 case MOUSE_MODEL_GLIDEPOINT: 4250 /* `tapping' action */ 4251 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : 4252 MOUSE_BUTTON4DOWN; 4253 break; 4254 4255 case MOUSE_MODEL_NETSCROLL: 4256 /* 4257 * three additional bytes encode buttons and 4258 * wheel events 4259 */ 4260 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON3DOWN) ? 4261 MOUSE_BUTTON4DOWN : 0; 4262 ms.button |= (pb->ipacket[3] & MOUSE_PS2_BUTTON1DOWN) ? 4263 MOUSE_BUTTON5DOWN : 0; 4264 z = (pb->ipacket[3] & MOUSE_PS2_XNEG) ? 4265 pb->ipacket[4] - 256 : pb->ipacket[4]; 4266 break; 4267 4268 case MOUSE_MODEL_THINK: 4269 /* the fourth button state in the first byte */ 4270 ms.button |= (c & MOUSE_PS2_TAP) ? 4271 MOUSE_BUTTON4DOWN : 0; 4272 break; 4273 4274 case MOUSE_MODEL_VERSAPAD: 4275 proc_versapad(sc, pb, &ms, &x, &y, &z); 4276 c = ((x < 0) ? MOUSE_PS2_XNEG : 0) | 4277 ((y < 0) ? MOUSE_PS2_YNEG : 0); 4278 break; 4279 4280 case MOUSE_MODEL_4D: 4281 /* 4282 * b7 b6 b5 b4 b3 b2 b1 b0 4283 * byte 1: s2 d2 s1 d1 1 M R L 4284 * byte 2: sx x x x x x x x 4285 * byte 3: sy y y y y y y y 4286 * 4287 * s1: wheel 1 direction 4288 * d1: wheel 1 data 4289 * s2: wheel 2 direction 4290 * d2: wheel 2 data 4291 */ 4292 x = (pb->ipacket[1] & 0x80) ? 4293 pb->ipacket[1] - 256 : pb->ipacket[1]; 4294 y = (pb->ipacket[2] & 0x80) ? 4295 pb->ipacket[2] - 256 : pb->ipacket[2]; 4296 switch (c & MOUSE_4D_WHEELBITS) { 4297 case 0x10: 4298 z = 1; 4299 break; 4300 case 0x30: 4301 z = -1; 4302 break; 4303 case 0x40: /* XXX 2nd wheel turning right */ 4304 z = 2; 4305 break; 4306 case 0xc0: /* XXX 2nd wheel turning left */ 4307 z = -2; 4308 break; 4309 } 4310 break; 4311 4312 case MOUSE_MODEL_4DPLUS: 4313 if ((x < 16 - 256) && (y < 16 - 256)) { 4314 /* 4315 * b7 b6 b5 b4 b3 b2 b1 b0 4316 * byte 1: 0 0 1 1 1 M R L 4317 * byte 2: 0 0 0 0 1 0 0 0 4318 * byte 3: 0 0 0 0 S s d1 d0 4319 * 4320 * L, M, R, S: left, middle, right, 4321 * and side buttons 4322 * s: wheel data sign bit 4323 * d1-d0: wheel data 4324 */ 4325 x = y = 0; 4326 if (pb->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN) 4327 ms.button |= MOUSE_BUTTON4DOWN; 4328 z = (pb->ipacket[2] & MOUSE_4DPLUS_ZNEG) ? 4329 ((pb->ipacket[2] & 0x07) - 8) : 4330 (pb->ipacket[2] & 0x07) ; 4331 } else { 4332 /* preserve previous button states */ 4333 ms.button |= ms.obutton & MOUSE_EXTBUTTONS; 4334 } 4335 break; 4336 4337 case MOUSE_MODEL_SYNAPTICS: 4338 if (proc_synaptics(sc, pb, &ms, &x, &y, &z) != 0) 4339 goto next; 4340 break; 4341 4342 case MOUSE_MODEL_ELANTECH: 4343 if (proc_elantech(sc, pb, &ms, &x, &y, &z) != 0) 4344 goto next; 4345 break; 4346 4347 case MOUSE_MODEL_TRACKPOINT: 4348 case MOUSE_MODEL_GENERIC: 4349 default: 4350 break; 4351 } 4352 4353 /* scale values */ 4354 if (sc->mode.accelfactor >= 1) { 4355 if (x != 0) { 4356 x = x * x / sc->mode.accelfactor; 4357 if (x == 0) 4358 x = 1; 4359 if (c & MOUSE_PS2_XNEG) 4360 x = -x; 4361 } 4362 if (y != 0) { 4363 y = y * y / sc->mode.accelfactor; 4364 if (y == 0) 4365 y = 1; 4366 if (c & MOUSE_PS2_YNEG) 4367 y = -y; 4368 } 4369 } 4370 4371 /* Store last packet for reinjection if it has not been set already */ 4372 if (timevalisset(&sc->idletimeout) && sc->idlepacket.inputbytes == 0) 4373 sc->idlepacket = *pb; 4374 4375 ms.dx = x; 4376 ms.dy = y; 4377 ms.dz = z; 4378 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0) | 4379 (ms.obutton ^ ms.button); 4380 4381 pb->inputbytes = tame_mouse(sc, pb, &ms, pb->ipacket); 4382 4383 sc->status.flags |= ms.flags; 4384 sc->status.dx += ms.dx; 4385 sc->status.dy += ms.dy; 4386 sc->status.dz += ms.dz; 4387 sc->status.button = ms.button; 4388 sc->button = ms.button; 4389 4390 next_native: 4391 sc->watchdog = FALSE; 4392 4393 /* queue data */ 4394 if (sc->queue.count + pb->inputbytes < sizeof(sc->queue.buf)) { 4395 l = imin(pb->inputbytes, 4396 sizeof(sc->queue.buf) - sc->queue.tail); 4397 bcopy(&pb->ipacket[0], &sc->queue.buf[sc->queue.tail], l); 4398 if (pb->inputbytes > l) 4399 bcopy(&pb->ipacket[l], &sc->queue.buf[0], 4400 pb->inputbytes - l); 4401 sc->queue.tail = (sc->queue.tail + pb->inputbytes) % 4402 sizeof(sc->queue.buf); 4403 sc->queue.count += pb->inputbytes; 4404 } 4405 pb->inputbytes = 0; 4406 4407 next: 4408 if (++sc->pqueue_start >= PSM_PACKETQUEUE) 4409 sc->pqueue_start = 0; 4410 } while (sc->pqueue_start != sc->pqueue_end); 4411 4412 if (sc->state & PSM_ASLP) { 4413 sc->state &= ~PSM_ASLP; 4414 wakeup(sc); 4415 } 4416 selwakeuppri(&sc->rsel, PZERO); 4417 if (sc->async != NULL) { 4418 pgsigio(&sc->async, SIGIO, 0); 4419 } 4420 sc->state &= ~PSM_SOFTARMED; 4421 4422 /* schedule injection of predefined packet after idletimeout 4423 * if no data packets have been received from psmintr */ 4424 if (timevalisset(&sc->idletimeout)) { 4425 sc->state |= PSM_SOFTARMED; 4426 callout_reset(&sc->softcallout, tvtohz(&sc->idletimeout), 4427 psmsoftintridle, sc); 4428 VLOG(2, (LOG_DEBUG, "softintr: callout set: %d ticks\n", 4429 tvtohz(&sc->idletimeout))); 4430 } 4431 splx(s); 4432 } 4433 4434 static int 4435 psmpoll(struct cdev *dev, int events, struct thread *td) 4436 { 4437 struct psm_softc *sc = dev->si_drv1; 4438 int s; 4439 int revents = 0; 4440 4441 /* Return true if a mouse event available */ 4442 s = spltty(); 4443 if (events & (POLLIN | POLLRDNORM)) { 4444 if (sc->queue.count > 0) 4445 revents |= events & (POLLIN | POLLRDNORM); 4446 else 4447 selrecord(td, &sc->rsel); 4448 } 4449 splx(s); 4450 4451 return (revents); 4452 } 4453 4454 /* vendor/model specific routines */ 4455 4456 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status) 4457 { 4458 if (set_mouse_resolution(kbdc, res) != res) 4459 return (FALSE); 4460 if (set_mouse_scaling(kbdc, scale) && 4461 set_mouse_scaling(kbdc, scale) && 4462 set_mouse_scaling(kbdc, scale) && 4463 (get_mouse_status(kbdc, status, 0, 3) >= 3)) 4464 return (TRUE); 4465 return (FALSE); 4466 } 4467 4468 static int 4469 mouse_ext_command(KBDC kbdc, int command) 4470 { 4471 int c; 4472 4473 c = (command >> 6) & 0x03; 4474 if (set_mouse_resolution(kbdc, c) != c) 4475 return (FALSE); 4476 c = (command >> 4) & 0x03; 4477 if (set_mouse_resolution(kbdc, c) != c) 4478 return (FALSE); 4479 c = (command >> 2) & 0x03; 4480 if (set_mouse_resolution(kbdc, c) != c) 4481 return (FALSE); 4482 c = (command >> 0) & 0x03; 4483 if (set_mouse_resolution(kbdc, c) != c) 4484 return (FALSE); 4485 return (TRUE); 4486 } 4487 4488 #ifdef notyet 4489 /* Logitech MouseMan Cordless II */ 4490 static int 4491 enable_lcordless(struct psm_softc *sc, enum probearg arg) 4492 { 4493 KBDC kbdc = sc->kbdc; 4494 int status[3]; 4495 int ch; 4496 4497 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 2, status)) 4498 return (FALSE); 4499 if (status[1] == PSMD_RES_HIGH) 4500 return (FALSE); 4501 ch = (status[0] & 0x07) - 1; /* channel # */ 4502 if ((ch <= 0) || (ch > 4)) 4503 return (FALSE); 4504 /* 4505 * status[1]: always one? 4506 * status[2]: battery status? (0-100) 4507 */ 4508 return (TRUE); 4509 } 4510 #endif /* notyet */ 4511 4512 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */ 4513 static int 4514 enable_groller(struct psm_softc *sc, enum probearg arg) 4515 { 4516 KBDC kbdc = sc->kbdc; 4517 int status[3]; 4518 4519 /* 4520 * The special sequence to enable the fourth button and the 4521 * roller. Immediately after this sequence check status bytes. 4522 * if the mouse is NetScroll, the second and the third bytes are 4523 * '3' and 'D'. 4524 */ 4525 4526 /* 4527 * If the mouse is an ordinary PS/2 mouse, the status bytes should 4528 * look like the following. 4529 * 4530 * byte 1 bit 7 always 0 4531 * bit 6 stream mode (0) 4532 * bit 5 disabled (0) 4533 * bit 4 1:1 scaling (0) 4534 * bit 3 always 0 4535 * bit 0-2 button status 4536 * byte 2 resolution (PSMD_RES_HIGH) 4537 * byte 3 report rate (?) 4538 */ 4539 4540 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status)) 4541 return (FALSE); 4542 if ((status[1] != '3') || (status[2] != 'D')) 4543 return (FALSE); 4544 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */ 4545 if (arg == PROBE) 4546 sc->hw.buttons = 4; 4547 return (TRUE); 4548 } 4549 4550 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */ 4551 static int 4552 enable_gmouse(struct psm_softc *sc, enum probearg arg) 4553 { 4554 KBDC kbdc = sc->kbdc; 4555 int status[3]; 4556 4557 /* 4558 * The special sequence to enable the middle, "rubber" button. 4559 * Immediately after this sequence check status bytes. 4560 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse, 4561 * the second and the third bytes are '3' and 'U'. 4562 * NOTE: NetMouse reports that it has three buttons although it has 4563 * two buttons and a rubber button. NetMouse Pro and MIE Mouse 4564 * say they have three buttons too and they do have a button on the 4565 * side... 4566 */ 4567 if (!mouse_id_proc1(kbdc, PSMD_RES_HIGH, 1, status)) 4568 return (FALSE); 4569 if ((status[1] != '3') || (status[2] != 'U')) 4570 return (FALSE); 4571 return (TRUE); 4572 } 4573 4574 /* ALPS GlidePoint */ 4575 static int 4576 enable_aglide(struct psm_softc *sc, enum probearg arg) 4577 { 4578 KBDC kbdc = sc->kbdc; 4579 int status[3]; 4580 4581 /* 4582 * The special sequence to obtain ALPS GlidePoint specific 4583 * information. Immediately after this sequence, status bytes will 4584 * contain something interesting. 4585 * NOTE: ALPS produces several models of GlidePoint. Some of those 4586 * do not respond to this sequence, thus, cannot be detected this way. 4587 */ 4588 if (set_mouse_sampling_rate(kbdc, 100) != 100) 4589 return (FALSE); 4590 if (!mouse_id_proc1(kbdc, PSMD_RES_LOW, 2, status)) 4591 return (FALSE); 4592 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100)) 4593 return (FALSE); 4594 return (TRUE); 4595 } 4596 4597 /* Kensington ThinkingMouse/Trackball */ 4598 static int 4599 enable_kmouse(struct psm_softc *sc, enum probearg arg) 4600 { 4601 static u_char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 }; 4602 KBDC kbdc = sc->kbdc; 4603 int status[3]; 4604 int id1; 4605 int id2; 4606 int i; 4607 4608 id1 = get_aux_id(kbdc); 4609 if (set_mouse_sampling_rate(kbdc, 10) != 10) 4610 return (FALSE); 4611 /* 4612 * The device is now in the native mode? It returns a different 4613 * ID value... 4614 */ 4615 id2 = get_aux_id(kbdc); 4616 if ((id1 == id2) || (id2 != 2)) 4617 return (FALSE); 4618 4619 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW) 4620 return (FALSE); 4621 #if PSM_DEBUG >= 2 4622 /* at this point, resolution is LOW, sampling rate is 10/sec */ 4623 if (get_mouse_status(kbdc, status, 0, 3) < 3) 4624 return (FALSE); 4625 #endif 4626 4627 /* 4628 * The special sequence to enable the third and fourth buttons. 4629 * Otherwise they behave like the first and second buttons. 4630 */ 4631 for (i = 0; i < nitems(rate); ++i) 4632 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4633 return (FALSE); 4634 4635 /* 4636 * At this point, the device is using default resolution and 4637 * sampling rate for the native mode. 4638 */ 4639 if (get_mouse_status(kbdc, status, 0, 3) < 3) 4640 return (FALSE); 4641 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1])) 4642 return (FALSE); 4643 4644 /* the device appears be enabled by this sequence, diable it for now */ 4645 disable_aux_dev(kbdc); 4646 empty_aux_buffer(kbdc, 5); 4647 4648 return (TRUE); 4649 } 4650 4651 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */ 4652 static int 4653 enable_mmanplus(struct psm_softc *sc, enum probearg arg) 4654 { 4655 KBDC kbdc = sc->kbdc; 4656 int data[3]; 4657 4658 /* the special sequence to enable the fourth button and the roller. */ 4659 /* 4660 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION 4661 * must be called exactly three times since the last RESET command 4662 * before this sequence. XXX 4663 */ 4664 if (!set_mouse_scaling(kbdc, 1)) 4665 return (FALSE); 4666 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb)) 4667 return (FALSE); 4668 if (get_mouse_status(kbdc, data, 1, 3) < 3) 4669 return (FALSE); 4670 4671 /* 4672 * PS2++ protocol, packet type 0 4673 * 4674 * b7 b6 b5 b4 b3 b2 b1 b0 4675 * byte 1: * 1 p3 p2 1 * * * 4676 * byte 2: 1 1 p1 p0 m1 m0 1 0 4677 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0 4678 * 4679 * p3-p0: packet type: 0 4680 * m7-m0: model ID: MouseMan+:0x50, 4681 * FirstMouse+:0x51, 4682 * ScrollPoint:0x58... 4683 */ 4684 /* check constant bits */ 4685 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC) 4686 return (FALSE); 4687 if ((data[1] & 0xc3) != 0xc2) 4688 return (FALSE); 4689 /* check d3-d0 in byte 2 */ 4690 if (!MOUSE_PS2PLUS_CHECKBITS(data)) 4691 return (FALSE); 4692 /* check p3-p0 */ 4693 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0) 4694 return (FALSE); 4695 4696 if (arg == PROBE) { 4697 sc->hw.hwid &= 0x00ff; 4698 sc->hw.hwid |= data[2] << 8; /* save model ID */ 4699 } 4700 4701 /* 4702 * MouseMan+ (or FirstMouse+) is now in its native mode, in which 4703 * the wheel and the fourth button events are encoded in the 4704 * special data packet. The mouse may be put in the IntelliMouse mode 4705 * if it is initialized by the IntelliMouse's method. 4706 */ 4707 return (TRUE); 4708 } 4709 4710 /* MS IntelliMouse Explorer */ 4711 static int 4712 enable_msexplorer(struct psm_softc *sc, enum probearg arg) 4713 { 4714 KBDC kbdc = sc->kbdc; 4715 static u_char rate0[] = { 200, 100, 80, }; 4716 static u_char rate1[] = { 200, 200, 80, }; 4717 int id; 4718 int i; 4719 4720 /* 4721 * This is needed for at least A4Tech X-7xx mice - they do not go 4722 * straight to Explorer mode, but need to be set to Intelli mode 4723 * first. 4724 */ 4725 enable_msintelli(sc, arg); 4726 4727 /* the special sequence to enable the extra buttons and the roller. */ 4728 for (i = 0; i < nitems(rate1); ++i) 4729 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i]) 4730 return (FALSE); 4731 /* the device will give the genuine ID only after the above sequence */ 4732 id = get_aux_id(kbdc); 4733 if (id != PSM_EXPLORER_ID) 4734 return (FALSE); 4735 4736 if (arg == PROBE) { 4737 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */ 4738 sc->hw.hwid = id; 4739 } 4740 4741 /* 4742 * XXX: this is a kludge to fool some KVM switch products 4743 * which think they are clever enough to know the 4-byte IntelliMouse 4744 * protocol, and assume any other protocols use 3-byte packets. 4745 * They don't convey 4-byte data packets from the IntelliMouse Explorer 4746 * correctly to the host computer because of this! 4747 * The following sequence is actually IntelliMouse's "wake up" 4748 * sequence; it will make the KVM think the mouse is IntelliMouse 4749 * when it is in fact IntelliMouse Explorer. 4750 */ 4751 for (i = 0; i < nitems(rate0); ++i) 4752 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i]) 4753 break; 4754 get_aux_id(kbdc); 4755 4756 return (TRUE); 4757 } 4758 4759 /* 4760 * MS IntelliMouse 4761 * Logitech MouseMan+ and FirstMouse+ will also respond to this 4762 * probe routine and act like IntelliMouse. 4763 */ 4764 static int 4765 enable_msintelli(struct psm_softc *sc, enum probearg arg) 4766 { 4767 KBDC kbdc = sc->kbdc; 4768 static u_char rate[] = { 200, 100, 80, }; 4769 int id; 4770 int i; 4771 4772 /* the special sequence to enable the third button and the roller. */ 4773 for (i = 0; i < nitems(rate); ++i) 4774 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4775 return (FALSE); 4776 /* the device will give the genuine ID only after the above sequence */ 4777 id = get_aux_id(kbdc); 4778 if (id != PSM_INTELLI_ID) 4779 return (FALSE); 4780 4781 if (arg == PROBE) { 4782 sc->hw.buttons = 3; 4783 sc->hw.hwid = id; 4784 } 4785 4786 return (TRUE); 4787 } 4788 4789 /* 4790 * A4 Tech 4D Mouse 4791 * Newer wheel mice from A4 Tech may use the 4D+ protocol. 4792 */ 4793 static int 4794 enable_4dmouse(struct psm_softc *sc, enum probearg arg) 4795 { 4796 static u_char rate[] = { 200, 100, 80, 60, 40, 20 }; 4797 KBDC kbdc = sc->kbdc; 4798 int id; 4799 int i; 4800 4801 for (i = 0; i < nitems(rate); ++i) 4802 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4803 return (FALSE); 4804 id = get_aux_id(kbdc); 4805 /* 4806 * WinEasy 4D, 4 Way Scroll 4D: 6 4807 * Cable-Free 4D: 8 (4DPLUS) 4808 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS) 4809 */ 4810 if (id != PSM_4DMOUSE_ID) 4811 return (FALSE); 4812 4813 if (arg == PROBE) { 4814 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */ 4815 sc->hw.hwid = id; 4816 } 4817 4818 return (TRUE); 4819 } 4820 4821 /* 4822 * A4 Tech 4D+ Mouse 4823 * Newer wheel mice from A4 Tech seem to use this protocol. 4824 * Older models are recognized as either 4D Mouse or IntelliMouse. 4825 */ 4826 static int 4827 enable_4dplus(struct psm_softc *sc, enum probearg arg) 4828 { 4829 KBDC kbdc = sc->kbdc; 4830 int id; 4831 4832 /* 4833 * enable_4dmouse() already issued the following ID sequence... 4834 static u_char rate[] = { 200, 100, 80, 60, 40, 20 }; 4835 int i; 4836 4837 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) 4838 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i]) 4839 return (FALSE); 4840 */ 4841 4842 id = get_aux_id(kbdc); 4843 switch (id) { 4844 case PSM_4DPLUS_ID: 4845 break; 4846 case PSM_4DPLUS_RFSW35_ID: 4847 break; 4848 default: 4849 return (FALSE); 4850 } 4851 4852 if (arg == PROBE) { 4853 sc->hw.buttons = (id == PSM_4DPLUS_ID) ? 4 : 3; 4854 sc->hw.hwid = id; 4855 } 4856 4857 return (TRUE); 4858 } 4859 4860 /* Synaptics Touchpad */ 4861 static int 4862 synaptics_sysctl(SYSCTL_HANDLER_ARGS) 4863 { 4864 struct psm_softc *sc; 4865 int error, arg; 4866 4867 if (oidp->oid_arg1 == NULL || oidp->oid_arg2 < 0 || 4868 oidp->oid_arg2 > SYNAPTICS_SYSCTL_SOFTBUTTON3_X) 4869 return (EINVAL); 4870 4871 sc = oidp->oid_arg1; 4872 4873 /* Read the current value. */ 4874 arg = *(int *)((char *)sc + oidp->oid_arg2); 4875 error = sysctl_handle_int(oidp, &arg, 0, req); 4876 4877 /* Sanity check. */ 4878 if (error || !req->newptr) 4879 return (error); 4880 4881 /* 4882 * Check that the new value is in the concerned node's range 4883 * of values. 4884 */ 4885 switch (oidp->oid_arg2) { 4886 case SYNAPTICS_SYSCTL_MIN_PRESSURE: 4887 case SYNAPTICS_SYSCTL_MAX_PRESSURE: 4888 if (arg < 0 || arg > 255) 4889 return (EINVAL); 4890 break; 4891 case SYNAPTICS_SYSCTL_MAX_WIDTH: 4892 if (arg < 4 || arg > 15) 4893 return (EINVAL); 4894 break; 4895 case SYNAPTICS_SYSCTL_MARGIN_TOP: 4896 case SYNAPTICS_SYSCTL_MARGIN_BOTTOM: 4897 case SYNAPTICS_SYSCTL_NA_TOP: 4898 case SYNAPTICS_SYSCTL_NA_BOTTOM: 4899 if (arg < 0 || arg > sc->synhw.maximumYCoord) 4900 return (EINVAL); 4901 break; 4902 case SYNAPTICS_SYSCTL_SOFTBUTTON2_X: 4903 case SYNAPTICS_SYSCTL_SOFTBUTTON3_X: 4904 /* Softbuttons is clickpad only feature */ 4905 if (!sc->synhw.capClickPad && arg != 0) 4906 return (EINVAL); 4907 /* FALLTHROUGH */ 4908 case SYNAPTICS_SYSCTL_MARGIN_RIGHT: 4909 case SYNAPTICS_SYSCTL_MARGIN_LEFT: 4910 case SYNAPTICS_SYSCTL_NA_RIGHT: 4911 case SYNAPTICS_SYSCTL_NA_LEFT: 4912 if (arg < 0 || arg > sc->synhw.maximumXCoord) 4913 return (EINVAL); 4914 break; 4915 case SYNAPTICS_SYSCTL_WINDOW_MIN: 4916 case SYNAPTICS_SYSCTL_WINDOW_MAX: 4917 case SYNAPTICS_SYSCTL_TAP_MIN_QUEUE: 4918 if (arg < 1 || arg > SYNAPTICS_PACKETQUEUE) 4919 return (EINVAL); 4920 break; 4921 case SYNAPTICS_SYSCTL_MULTIPLICATOR: 4922 case SYNAPTICS_SYSCTL_WEIGHT_CURRENT: 4923 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS: 4924 case SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA: 4925 case SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED: 4926 case SYNAPTICS_SYSCTL_DIV_MIN: 4927 case SYNAPTICS_SYSCTL_DIV_MAX: 4928 case SYNAPTICS_SYSCTL_DIV_MAX_NA: 4929 case SYNAPTICS_SYSCTL_DIV_LEN: 4930 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN: 4931 case SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX: 4932 if (arg < 1) 4933 return (EINVAL); 4934 break; 4935 case SYNAPTICS_SYSCTL_TAP_MAX_DELTA: 4936 case SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT: 4937 case SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA: 4938 if (arg < 0) 4939 return (EINVAL); 4940 break; 4941 case SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA: 4942 if (arg < -sc->synhw.maximumXCoord || 4943 arg > sc->synhw.maximumXCoord) 4944 return (EINVAL); 4945 break; 4946 case SYNAPTICS_SYSCTL_SOFTBUTTONS_Y: 4947 /* Softbuttons is clickpad only feature */ 4948 if (!sc->synhw.capClickPad && arg != 0) 4949 return (EINVAL); 4950 /* FALLTHROUGH */ 4951 case SYNAPTICS_SYSCTL_VSCROLL_VER_AREA: 4952 if (arg < -sc->synhw.maximumYCoord || 4953 arg > sc->synhw.maximumYCoord) 4954 return (EINVAL); 4955 break; 4956 case SYNAPTICS_SYSCTL_TOUCHPAD_OFF: 4957 if (arg < 0 || arg > 1) 4958 return (EINVAL); 4959 break; 4960 default: 4961 return (EINVAL); 4962 } 4963 4964 /* Update. */ 4965 *(int *)((char *)sc + oidp->oid_arg2) = arg; 4966 4967 return (error); 4968 } 4969 4970 static void 4971 synaptics_sysctl_create_softbuttons_tree(struct psm_softc *sc) 4972 { 4973 /* 4974 * Set predefined sizes for softbuttons. 4975 * Values are taken to match HP Pavilion dv6 clickpad drawings 4976 * with thin middle softbutton placed on separator 4977 */ 4978 4979 /* hw.psm.synaptics.softbuttons_y */ 4980 sc->syninfo.softbuttons_y = 1700; 4981 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 4982 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 4983 "softbuttons_y", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 4984 sc, SYNAPTICS_SYSCTL_SOFTBUTTONS_Y, 4985 synaptics_sysctl, "I", 4986 "Vertical size of softbuttons area"); 4987 4988 /* hw.psm.synaptics.softbutton2_x */ 4989 sc->syninfo.softbutton2_x = 3100; 4990 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 4991 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 4992 "softbutton2_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 4993 sc, SYNAPTICS_SYSCTL_SOFTBUTTON2_X, 4994 synaptics_sysctl, "I", 4995 "Horisontal position of 2-nd softbutton left edge (0-disable)"); 4996 4997 /* hw.psm.synaptics.softbutton3_x */ 4998 sc->syninfo.softbutton3_x = 3900; 4999 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5000 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5001 "softbutton3_x", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5002 sc, SYNAPTICS_SYSCTL_SOFTBUTTON3_X, 5003 synaptics_sysctl, "I", 5004 "Horisontal position of 3-rd softbutton left edge (0-disable)"); 5005 } 5006 5007 static void 5008 synaptics_sysctl_create_tree(struct psm_softc *sc, const char *name, 5009 const char *descr) 5010 { 5011 5012 if (sc->syninfo.sysctl_tree != NULL) 5013 return; 5014 5015 /* Attach extra synaptics sysctl nodes under hw.psm.synaptics */ 5016 sysctl_ctx_init(&sc->syninfo.sysctl_ctx); 5017 sc->syninfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->syninfo.sysctl_ctx, 5018 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, name, CTLFLAG_RD, 5019 0, descr); 5020 5021 /* hw.psm.synaptics.directional_scrolls. */ 5022 sc->syninfo.directional_scrolls = 0; 5023 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5024 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5025 "directional_scrolls", CTLFLAG_RW|CTLFLAG_ANYBODY, 5026 &sc->syninfo.directional_scrolls, 0, 5027 "Enable hardware scrolling pad (if non-zero) or register it as " 5028 "extended buttons (if 0)"); 5029 5030 /* hw.psm.synaptics.max_x. */ 5031 sc->syninfo.max_x = 6143; 5032 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5033 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5034 "max_x", CTLFLAG_RD|CTLFLAG_ANYBODY, 5035 &sc->syninfo.max_x, 0, 5036 "Horizontal reporting range"); 5037 5038 /* hw.psm.synaptics.max_y. */ 5039 sc->syninfo.max_y = 6143; 5040 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5041 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5042 "max_y", CTLFLAG_RD|CTLFLAG_ANYBODY, 5043 &sc->syninfo.max_y, 0, 5044 "Vertical reporting range"); 5045 5046 /* 5047 * Turn off two finger scroll if we have a 5048 * physical area reserved for scrolling or when 5049 * there's no multi finger support. 5050 */ 5051 if (sc->synhw.verticalScroll || (sc->synhw.capMultiFinger == 0 && 5052 sc->synhw.capAdvancedGestures == 0)) 5053 sc->syninfo.two_finger_scroll = 0; 5054 else 5055 sc->syninfo.two_finger_scroll = 1; 5056 /* hw.psm.synaptics.two_finger_scroll. */ 5057 SYSCTL_ADD_INT(&sc->syninfo.sysctl_ctx, 5058 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5059 "two_finger_scroll", CTLFLAG_RW|CTLFLAG_ANYBODY, 5060 &sc->syninfo.two_finger_scroll, 0, 5061 "Enable two finger scrolling"); 5062 5063 /* hw.psm.synaptics.min_pressure. */ 5064 sc->syninfo.min_pressure = 16; 5065 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5066 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5067 "min_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5068 sc, SYNAPTICS_SYSCTL_MIN_PRESSURE, 5069 synaptics_sysctl, "I", 5070 "Minimum pressure required to start an action"); 5071 5072 /* hw.psm.synaptics.max_pressure. */ 5073 sc->syninfo.max_pressure = 220; 5074 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5075 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5076 "max_pressure", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5077 sc, SYNAPTICS_SYSCTL_MAX_PRESSURE, 5078 synaptics_sysctl, "I", 5079 "Maximum pressure to detect palm"); 5080 5081 /* hw.psm.synaptics.max_width. */ 5082 sc->syninfo.max_width = 10; 5083 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5084 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5085 "max_width", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5086 sc, SYNAPTICS_SYSCTL_MAX_WIDTH, 5087 synaptics_sysctl, "I", 5088 "Maximum finger width to detect palm"); 5089 5090 /* hw.psm.synaptics.top_margin. */ 5091 sc->syninfo.margin_top = 200; 5092 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5093 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5094 "margin_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5095 sc, SYNAPTICS_SYSCTL_MARGIN_TOP, 5096 synaptics_sysctl, "I", 5097 "Top margin"); 5098 5099 /* hw.psm.synaptics.right_margin. */ 5100 sc->syninfo.margin_right = 200; 5101 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5102 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5103 "margin_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5104 sc, SYNAPTICS_SYSCTL_MARGIN_RIGHT, 5105 synaptics_sysctl, "I", 5106 "Right margin"); 5107 5108 /* hw.psm.synaptics.bottom_margin. */ 5109 sc->syninfo.margin_bottom = 200; 5110 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5111 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5112 "margin_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5113 sc, SYNAPTICS_SYSCTL_MARGIN_BOTTOM, 5114 synaptics_sysctl, "I", 5115 "Bottom margin"); 5116 5117 /* hw.psm.synaptics.left_margin. */ 5118 sc->syninfo.margin_left = 200; 5119 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5120 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5121 "margin_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5122 sc, SYNAPTICS_SYSCTL_MARGIN_LEFT, 5123 synaptics_sysctl, "I", 5124 "Left margin"); 5125 5126 /* hw.psm.synaptics.na_top. */ 5127 sc->syninfo.na_top = 1783; 5128 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5129 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5130 "na_top", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5131 sc, SYNAPTICS_SYSCTL_NA_TOP, 5132 synaptics_sysctl, "I", 5133 "Top noisy area, where weight_previous_na is used instead " 5134 "of weight_previous"); 5135 5136 /* hw.psm.synaptics.na_right. */ 5137 sc->syninfo.na_right = 563; 5138 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5139 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5140 "na_right", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5141 sc, SYNAPTICS_SYSCTL_NA_RIGHT, 5142 synaptics_sysctl, "I", 5143 "Right noisy area, where weight_previous_na is used instead " 5144 "of weight_previous"); 5145 5146 /* hw.psm.synaptics.na_bottom. */ 5147 sc->syninfo.na_bottom = 1408; 5148 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5149 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5150 "na_bottom", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5151 sc, SYNAPTICS_SYSCTL_NA_BOTTOM, 5152 synaptics_sysctl, "I", 5153 "Bottom noisy area, where weight_previous_na is used instead " 5154 "of weight_previous"); 5155 5156 /* hw.psm.synaptics.na_left. */ 5157 sc->syninfo.na_left = 1600; 5158 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5159 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5160 "na_left", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5161 sc, SYNAPTICS_SYSCTL_NA_LEFT, 5162 synaptics_sysctl, "I", 5163 "Left noisy area, where weight_previous_na is used instead " 5164 "of weight_previous"); 5165 5166 /* hw.psm.synaptics.window_min. */ 5167 sc->syninfo.window_min = 4; 5168 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5169 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5170 "window_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5171 sc, SYNAPTICS_SYSCTL_WINDOW_MIN, 5172 synaptics_sysctl, "I", 5173 "Minimum window size to start an action"); 5174 5175 /* hw.psm.synaptics.window_max. */ 5176 sc->syninfo.window_max = 10; 5177 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5178 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5179 "window_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5180 sc, SYNAPTICS_SYSCTL_WINDOW_MAX, 5181 synaptics_sysctl, "I", 5182 "Maximum window size"); 5183 5184 /* hw.psm.synaptics.multiplicator. */ 5185 sc->syninfo.multiplicator = 10000; 5186 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5187 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5188 "multiplicator", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5189 sc, SYNAPTICS_SYSCTL_MULTIPLICATOR, 5190 synaptics_sysctl, "I", 5191 "Multiplicator to increase precision in averages and divisions"); 5192 5193 /* hw.psm.synaptics.weight_current. */ 5194 sc->syninfo.weight_current = 3; 5195 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5196 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5197 "weight_current", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5198 sc, SYNAPTICS_SYSCTL_WEIGHT_CURRENT, 5199 synaptics_sysctl, "I", 5200 "Weight of the current movement in the new average"); 5201 5202 /* hw.psm.synaptics.weight_previous. */ 5203 sc->syninfo.weight_previous = 6; 5204 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5205 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5206 "weight_previous", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5207 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS, 5208 synaptics_sysctl, "I", 5209 "Weight of the previous average"); 5210 5211 /* hw.psm.synaptics.weight_previous_na. */ 5212 sc->syninfo.weight_previous_na = 20; 5213 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5214 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5215 "weight_previous_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5216 sc, SYNAPTICS_SYSCTL_WEIGHT_PREVIOUS_NA, 5217 synaptics_sysctl, "I", 5218 "Weight of the previous average (inside the noisy area)"); 5219 5220 /* hw.psm.synaptics.weight_len_squared. */ 5221 sc->syninfo.weight_len_squared = 2000; 5222 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5223 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5224 "weight_len_squared", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5225 sc, SYNAPTICS_SYSCTL_WEIGHT_LEN_SQUARED, 5226 synaptics_sysctl, "I", 5227 "Length (squared) of segments where weight_previous " 5228 "starts to decrease"); 5229 5230 /* hw.psm.synaptics.div_min. */ 5231 sc->syninfo.div_min = 9; 5232 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5233 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5234 "div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5235 sc, SYNAPTICS_SYSCTL_DIV_MIN, 5236 synaptics_sysctl, "I", 5237 "Divisor for fast movements"); 5238 5239 /* hw.psm.synaptics.div_max. */ 5240 sc->syninfo.div_max = 17; 5241 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5242 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5243 "div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5244 sc, SYNAPTICS_SYSCTL_DIV_MAX, 5245 synaptics_sysctl, "I", 5246 "Divisor for slow movements"); 5247 5248 /* hw.psm.synaptics.div_max_na. */ 5249 sc->syninfo.div_max_na = 30; 5250 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5251 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5252 "div_max_na", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5253 sc, SYNAPTICS_SYSCTL_DIV_MAX_NA, 5254 synaptics_sysctl, "I", 5255 "Divisor with slow movements (inside the noisy area)"); 5256 5257 /* hw.psm.synaptics.div_len. */ 5258 sc->syninfo.div_len = 100; 5259 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5260 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5261 "div_len", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5262 sc, SYNAPTICS_SYSCTL_DIV_LEN, 5263 synaptics_sysctl, "I", 5264 "Length of segments where div_max starts to decrease"); 5265 5266 /* hw.psm.synaptics.tap_max_delta. */ 5267 sc->syninfo.tap_max_delta = 80; 5268 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5269 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5270 "tap_max_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5271 sc, SYNAPTICS_SYSCTL_TAP_MAX_DELTA, 5272 synaptics_sysctl, "I", 5273 "Length of segments above which a tap is ignored"); 5274 5275 /* hw.psm.synaptics.tap_min_queue. */ 5276 sc->syninfo.tap_min_queue = 2; 5277 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5278 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5279 "tap_min_queue", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5280 sc, SYNAPTICS_SYSCTL_TAP_MIN_QUEUE, 5281 synaptics_sysctl, "I", 5282 "Number of packets required to consider a tap"); 5283 5284 /* hw.psm.synaptics.taphold_timeout. */ 5285 sc->gesture.in_taphold = 0; 5286 sc->syninfo.taphold_timeout = tap_timeout; 5287 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5288 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5289 "taphold_timeout", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5290 sc, SYNAPTICS_SYSCTL_TAPHOLD_TIMEOUT, 5291 synaptics_sysctl, "I", 5292 "Maximum elapsed time between two taps to consider a tap-hold " 5293 "action"); 5294 5295 /* hw.psm.synaptics.vscroll_hor_area. */ 5296 sc->syninfo.vscroll_hor_area = 0; /* 1300 */ 5297 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5298 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5299 "vscroll_hor_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5300 sc, SYNAPTICS_SYSCTL_VSCROLL_HOR_AREA, 5301 synaptics_sysctl, "I", 5302 "Area reserved for horizontal virtual scrolling"); 5303 5304 /* hw.psm.synaptics.vscroll_ver_area. */ 5305 sc->syninfo.vscroll_ver_area = -400 - sc->syninfo.margin_right; 5306 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5307 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5308 "vscroll_ver_area", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5309 sc, SYNAPTICS_SYSCTL_VSCROLL_VER_AREA, 5310 synaptics_sysctl, "I", 5311 "Area reserved for vertical virtual scrolling"); 5312 5313 /* hw.psm.synaptics.vscroll_min_delta. */ 5314 sc->syninfo.vscroll_min_delta = 50; 5315 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5316 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5317 "vscroll_min_delta", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5318 sc, SYNAPTICS_SYSCTL_VSCROLL_MIN_DELTA, 5319 synaptics_sysctl, "I", 5320 "Minimum movement to consider virtual scrolling"); 5321 5322 /* hw.psm.synaptics.vscroll_div_min. */ 5323 sc->syninfo.vscroll_div_min = 100; 5324 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5325 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5326 "vscroll_div_min", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5327 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MIN, 5328 synaptics_sysctl, "I", 5329 "Divisor for fast scrolling"); 5330 5331 /* hw.psm.synaptics.vscroll_div_min. */ 5332 sc->syninfo.vscroll_div_max = 150; 5333 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5334 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5335 "vscroll_div_max", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5336 sc, SYNAPTICS_SYSCTL_VSCROLL_DIV_MAX, 5337 synaptics_sysctl, "I", 5338 "Divisor for slow scrolling"); 5339 5340 /* hw.psm.synaptics.touchpad_off. */ 5341 sc->syninfo.touchpad_off = 0; 5342 SYSCTL_ADD_PROC(&sc->syninfo.sysctl_ctx, 5343 SYSCTL_CHILDREN(sc->syninfo.sysctl_tree), OID_AUTO, 5344 "touchpad_off", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5345 sc, SYNAPTICS_SYSCTL_TOUCHPAD_OFF, 5346 synaptics_sysctl, "I", 5347 "Turn off touchpad"); 5348 5349 sc->syninfo.softbuttons_y = 0; 5350 sc->syninfo.softbutton2_x = 0; 5351 sc->syninfo.softbutton3_x = 0; 5352 5353 /* skip softbuttons sysctl on not clickpads */ 5354 if (sc->synhw.capClickPad) 5355 synaptics_sysctl_create_softbuttons_tree(sc); 5356 } 5357 5358 static int 5359 synaptics_preferred_mode(struct psm_softc *sc) { 5360 int mode_byte; 5361 5362 mode_byte = 0xc4; 5363 5364 /* request wmode where available */ 5365 if (sc->synhw.capExtended) 5366 mode_byte |= 1; 5367 5368 return mode_byte; 5369 } 5370 5371 static void 5372 synaptics_set_mode(struct psm_softc *sc, int mode_byte) { 5373 mouse_ext_command(sc->kbdc, mode_byte); 5374 5375 /* "Commit" the Set Mode Byte command sent above. */ 5376 set_mouse_sampling_rate(sc->kbdc, 20); 5377 5378 /* 5379 * Enable advanced gestures mode if supported and we are not entering 5380 * passthrough mode. 5381 */ 5382 if ((sc->synhw.capAdvancedGestures || sc->synhw.capReportsV) && 5383 !(mode_byte & (1 << 5))) { 5384 mouse_ext_command(sc->kbdc, 3); 5385 set_mouse_sampling_rate(sc->kbdc, 0xc8); 5386 } 5387 } 5388 5389 static int 5390 enable_synaptics(struct psm_softc *sc, enum probearg arg) 5391 { 5392 KBDC kbdc = sc->kbdc; 5393 synapticshw_t synhw; 5394 int status[3]; 5395 int buttons; 5396 5397 VLOG(3, (LOG_DEBUG, "synaptics: BEGIN init\n")); 5398 5399 /* 5400 * Just to be on the safe side: this avoids troubles with 5401 * following mouse_ext_command() when the previous command 5402 * was PSMC_SET_RESOLUTION. Set Scaling has no effect on 5403 * Synaptics Touchpad behaviour. 5404 */ 5405 set_mouse_scaling(kbdc, 1); 5406 5407 /* Identify the Touchpad version. */ 5408 if (mouse_ext_command(kbdc, 0) == 0) 5409 return (FALSE); 5410 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5411 return (FALSE); 5412 if (status[1] != 0x47) 5413 return (FALSE); 5414 5415 bzero(&synhw, sizeof(synhw)); 5416 synhw.infoMinor = status[0]; 5417 synhw.infoMajor = status[2] & 0x0f; 5418 5419 if (verbose >= 2) 5420 printf("Synaptics Touchpad v%d.%d\n", synhw.infoMajor, 5421 synhw.infoMinor); 5422 5423 if (synhw.infoMajor < 4) { 5424 printf(" Unsupported (pre-v4) Touchpad detected\n"); 5425 return (FALSE); 5426 } 5427 5428 /* Get the Touchpad model information. */ 5429 if (mouse_ext_command(kbdc, 3) == 0) 5430 return (FALSE); 5431 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5432 return (FALSE); 5433 if ((status[1] & 0x01) != 0) { 5434 printf(" Failed to read model information\n"); 5435 return (FALSE); 5436 } 5437 5438 synhw.infoRot180 = (status[0] & 0x80) != 0; 5439 synhw.infoPortrait = (status[0] & 0x40) != 0; 5440 synhw.infoSensor = status[0] & 0x3f; 5441 synhw.infoHardware = (status[1] & 0xfe) >> 1; 5442 synhw.infoNewAbs = (status[2] & 0x80) != 0; 5443 synhw.capPen = (status[2] & 0x40) != 0; 5444 synhw.infoSimplC = (status[2] & 0x20) != 0; 5445 synhw.infoGeometry = status[2] & 0x0f; 5446 5447 if (verbose >= 2) { 5448 printf(" Model information:\n"); 5449 printf(" infoRot180: %d\n", synhw.infoRot180); 5450 printf(" infoPortrait: %d\n", synhw.infoPortrait); 5451 printf(" infoSensor: %d\n", synhw.infoSensor); 5452 printf(" infoHardware: %d\n", synhw.infoHardware); 5453 printf(" infoNewAbs: %d\n", synhw.infoNewAbs); 5454 printf(" capPen: %d\n", synhw.capPen); 5455 printf(" infoSimplC: %d\n", synhw.infoSimplC); 5456 printf(" infoGeometry: %d\n", synhw.infoGeometry); 5457 } 5458 5459 /* Read the extended capability bits. */ 5460 if (mouse_ext_command(kbdc, 2) == 0) 5461 return (FALSE); 5462 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5463 return (FALSE); 5464 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) { 5465 printf(" Failed to read extended capability bits\n"); 5466 return (FALSE); 5467 } 5468 5469 /* Set the different capabilities when they exist. */ 5470 buttons = 0; 5471 synhw.capExtended = (status[0] & 0x80) != 0; 5472 if (synhw.capExtended) { 5473 synhw.nExtendedQueries = (status[0] & 0x70) >> 4; 5474 synhw.capMiddle = (status[0] & 0x04) != 0; 5475 synhw.capPassthrough = (status[2] & 0x80) != 0; 5476 synhw.capLowPower = (status[2] & 0x40) != 0; 5477 synhw.capMultiFingerReport = 5478 (status[2] & 0x20) != 0; 5479 synhw.capSleep = (status[2] & 0x10) != 0; 5480 synhw.capFourButtons = (status[2] & 0x08) != 0; 5481 synhw.capBallistics = (status[2] & 0x04) != 0; 5482 synhw.capMultiFinger = (status[2] & 0x02) != 0; 5483 synhw.capPalmDetect = (status[2] & 0x01) != 0; 5484 5485 if (!set_mouse_scaling(kbdc, 1)) 5486 return (FALSE); 5487 if (mouse_ext_command(kbdc, 0x08) == 0) 5488 return (FALSE); 5489 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5490 return (FALSE); 5491 5492 synhw.infoXupmm = status[0]; 5493 synhw.infoYupmm = status[2]; 5494 5495 if (verbose >= 2) { 5496 printf(" Extended capabilities:\n"); 5497 printf(" capExtended: %d\n", synhw.capExtended); 5498 printf(" capMiddle: %d\n", synhw.capMiddle); 5499 printf(" nExtendedQueries: %d\n", 5500 synhw.nExtendedQueries); 5501 printf(" capPassthrough: %d\n", synhw.capPassthrough); 5502 printf(" capLowPower: %d\n", synhw.capLowPower); 5503 printf(" capMultiFingerReport: %d\n", 5504 synhw.capMultiFingerReport); 5505 printf(" capSleep: %d\n", synhw.capSleep); 5506 printf(" capFourButtons: %d\n", synhw.capFourButtons); 5507 printf(" capBallistics: %d\n", synhw.capBallistics); 5508 printf(" capMultiFinger: %d\n", synhw.capMultiFinger); 5509 printf(" capPalmDetect: %d\n", synhw.capPalmDetect); 5510 printf(" infoXupmm: %d\n", synhw.infoXupmm); 5511 printf(" infoYupmm: %d\n", synhw.infoYupmm); 5512 } 5513 5514 /* 5515 * If nExtendedQueries is 1 or greater, then the TouchPad 5516 * supports this number of extended queries. We can load 5517 * more information about buttons using query 0x09. 5518 */ 5519 if (synhw.nExtendedQueries >= 1) { 5520 if (!set_mouse_scaling(kbdc, 1)) 5521 return (FALSE); 5522 if (mouse_ext_command(kbdc, 0x09) == 0) 5523 return (FALSE); 5524 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5525 return (FALSE); 5526 synhw.verticalScroll = (status[0] & 0x01) != 0; 5527 synhw.horizontalScroll = (status[0] & 0x02) != 0; 5528 synhw.verticalWheel = (status[0] & 0x08) != 0; 5529 synhw.nExtendedButtons = (status[1] & 0xf0) >> 4; 5530 synhw.capEWmode = (status[0] & 0x04) != 0; 5531 if (verbose >= 2) { 5532 printf(" Extended model ID:\n"); 5533 printf(" verticalScroll: %d\n", 5534 synhw.verticalScroll); 5535 printf(" horizontalScroll: %d\n", 5536 synhw.horizontalScroll); 5537 printf(" verticalWheel: %d\n", 5538 synhw.verticalWheel); 5539 printf(" nExtendedButtons: %d\n", 5540 synhw.nExtendedButtons); 5541 printf(" capEWmode: %d\n", 5542 synhw.capEWmode); 5543 } 5544 /* 5545 * Add the number of extended buttons to the total 5546 * button support count, including the middle button 5547 * if capMiddle support bit is set. 5548 */ 5549 buttons = synhw.nExtendedButtons + synhw.capMiddle; 5550 } else 5551 /* 5552 * If the capFourButtons support bit is set, 5553 * add a fourth button to the total button count. 5554 */ 5555 buttons = synhw.capFourButtons ? 1 : 0; 5556 5557 /* Read the continued capabilities bits. */ 5558 if (synhw.nExtendedQueries >= 4) { 5559 if (!set_mouse_scaling(kbdc, 1)) 5560 return (FALSE); 5561 if (mouse_ext_command(kbdc, 0x0c) == 0) 5562 return (FALSE); 5563 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5564 return (FALSE); 5565 5566 synhw.capClickPad = (status[1] & 0x01) << 1; 5567 synhw.capClickPad |= (status[0] & 0x10) != 0; 5568 synhw.capDeluxeLEDs = (status[1] & 0x02) != 0; 5569 synhw.noAbsoluteFilter = (status[1] & 0x04) != 0; 5570 synhw.capReportsV = (status[1] & 0x08) != 0; 5571 synhw.capUniformClickPad = (status[1] & 0x10) != 0; 5572 synhw.capReportsMin = (status[1] & 0x20) != 0; 5573 synhw.capInterTouch = (status[1] & 0x40) != 0; 5574 synhw.capReportsMax = (status[0] & 0x02) != 0; 5575 synhw.capClearPad = (status[0] & 0x04) != 0; 5576 synhw.capAdvancedGestures = (status[0] & 0x08) != 0; 5577 synhw.capCoveredPad = (status[0] & 0x80) != 0; 5578 5579 if (synhw.capReportsMax) { 5580 if (!set_mouse_scaling(kbdc, 1)) 5581 return (FALSE); 5582 if (mouse_ext_command(kbdc, 0x0d) == 0) 5583 return (FALSE); 5584 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5585 return (FALSE); 5586 5587 synhw.maximumXCoord = (status[0] << 5) | 5588 ((status[1] & 0x0f) << 1); 5589 synhw.maximumYCoord = (status[2] << 5) | 5590 ((status[1] & 0xf0) >> 3); 5591 } else { 5592 /* 5593 * Typical bezel limits. Taken from 'Synaptics 5594 * PS/2 * TouchPad Interfacing Guide' p.3.2.3. 5595 */ 5596 synhw.maximumXCoord = 5472; 5597 synhw.maximumYCoord = 4448; 5598 } 5599 5600 if (synhw.capReportsMin) { 5601 if (!set_mouse_scaling(kbdc, 1)) 5602 return (FALSE); 5603 if (mouse_ext_command(kbdc, 0x0f) == 0) 5604 return (FALSE); 5605 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5606 return (FALSE); 5607 5608 synhw.minimumXCoord = (status[0] << 5) | 5609 ((status[1] & 0x0f) << 1); 5610 synhw.minimumYCoord = (status[2] << 5) | 5611 ((status[1] & 0xf0) >> 3); 5612 } else { 5613 /* 5614 * Typical bezel limits. Taken from 'Synaptics 5615 * PS/2 * TouchPad Interfacing Guide' p.3.2.3. 5616 */ 5617 synhw.minimumXCoord = 1472; 5618 synhw.minimumYCoord = 1408; 5619 } 5620 5621 if (verbose >= 2) { 5622 printf(" Continued capabilities:\n"); 5623 printf(" capClickPad: %d\n", 5624 synhw.capClickPad); 5625 printf(" capDeluxeLEDs: %d\n", 5626 synhw.capDeluxeLEDs); 5627 printf(" noAbsoluteFilter: %d\n", 5628 synhw.noAbsoluteFilter); 5629 printf(" capReportsV: %d\n", 5630 synhw.capReportsV); 5631 printf(" capUniformClickPad: %d\n", 5632 synhw.capUniformClickPad); 5633 printf(" capReportsMin: %d\n", 5634 synhw.capReportsMin); 5635 printf(" capInterTouch: %d\n", 5636 synhw.capInterTouch); 5637 printf(" capReportsMax: %d\n", 5638 synhw.capReportsMax); 5639 printf(" capClearPad: %d\n", 5640 synhw.capClearPad); 5641 printf(" capAdvancedGestures: %d\n", 5642 synhw.capAdvancedGestures); 5643 printf(" capCoveredPad: %d\n", 5644 synhw.capCoveredPad); 5645 if (synhw.capReportsMax) { 5646 printf(" maximumXCoord: %d\n", 5647 synhw.maximumXCoord); 5648 printf(" maximumYCoord: %d\n", 5649 synhw.maximumYCoord); 5650 } 5651 if (synhw.capReportsMin) { 5652 printf(" minimumXCoord: %d\n", 5653 synhw.minimumXCoord); 5654 printf(" minimumYCoord: %d\n", 5655 synhw.minimumYCoord); 5656 } 5657 } 5658 buttons += synhw.capClickPad; 5659 } 5660 } 5661 5662 if (verbose >= 2) { 5663 if (synhw.capExtended) 5664 printf(" Additional Buttons: %d\n", buttons); 5665 else 5666 printf(" No extended capabilities\n"); 5667 } 5668 5669 /* 5670 * Add the default number of 3 buttons to the total 5671 * count of supported buttons reported above. 5672 */ 5673 buttons += 3; 5674 5675 /* 5676 * Read the mode byte. 5677 * 5678 * XXX: Note the Synaptics documentation also defines the first 5679 * byte of the response to this query to be a constant 0x3b, this 5680 * does not appear to be true for Touchpads with guest devices. 5681 */ 5682 if (mouse_ext_command(kbdc, 1) == 0) 5683 return (FALSE); 5684 if (get_mouse_status(kbdc, status, 0, 3) != 3) 5685 return (FALSE); 5686 if (!SYNAPTICS_VERSION_GE(synhw, 7, 5) && status[1] != 0x47) { 5687 printf(" Failed to read mode byte\n"); 5688 return (FALSE); 5689 } 5690 5691 if (arg == PROBE) 5692 sc->synhw = synhw; 5693 if (!synaptics_support) 5694 return (FALSE); 5695 5696 synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 5697 5698 if (trackpoint_support && synhw.capPassthrough) { 5699 enable_trackpoint(sc, arg); 5700 } 5701 5702 VLOG(3, (LOG_DEBUG, "synaptics: END init (%d buttons)\n", buttons)); 5703 5704 if (arg == PROBE) { 5705 /* Create sysctl tree. */ 5706 synaptics_sysctl_create_tree(sc, "synaptics", 5707 "Synaptics TouchPad"); 5708 sc->hw.buttons = buttons; 5709 } 5710 5711 return (TRUE); 5712 } 5713 5714 static void 5715 synaptics_passthrough_on(struct psm_softc *sc) 5716 { 5717 VLOG(2, (LOG_NOTICE, "psm: setting pass-through mode.\n")); 5718 synaptics_set_mode(sc, synaptics_preferred_mode(sc) | (1 << 5)); 5719 } 5720 5721 static void 5722 synaptics_passthrough_off(struct psm_softc *sc) 5723 { 5724 VLOG(2, (LOG_NOTICE, "psm: turning pass-through mode off.\n")); 5725 set_mouse_scaling(sc->kbdc, 2); 5726 set_mouse_scaling(sc->kbdc, 1); 5727 synaptics_set_mode(sc, synaptics_preferred_mode(sc)); 5728 } 5729 5730 /* IBM/Lenovo TrackPoint */ 5731 static int 5732 trackpoint_command(struct psm_softc *sc, int cmd, int loc, int val) 5733 { 5734 const int seq[] = { 0xe2, cmd, loc, val }; 5735 int i; 5736 5737 if (sc->synhw.capPassthrough) 5738 synaptics_passthrough_on(sc); 5739 5740 for (i = 0; i < nitems(seq); i++) { 5741 if (sc->synhw.capPassthrough && 5742 (seq[i] == 0xff || seq[i] == 0xe7)) 5743 if (send_aux_command(sc->kbdc, 0xe7) != PSM_ACK) { 5744 synaptics_passthrough_off(sc); 5745 return (EIO); 5746 } 5747 if (send_aux_command(sc->kbdc, seq[i]) != PSM_ACK) { 5748 if (sc->synhw.capPassthrough) 5749 synaptics_passthrough_off(sc); 5750 return (EIO); 5751 } 5752 } 5753 5754 if (sc->synhw.capPassthrough) 5755 synaptics_passthrough_off(sc); 5756 5757 return (0); 5758 } 5759 5760 #define PSM_TPINFO(x) offsetof(struct psm_softc, tpinfo.x) 5761 #define TPMASK 0 5762 #define TPLOC 1 5763 #define TPINFO 2 5764 5765 static int 5766 trackpoint_sysctl(SYSCTL_HANDLER_ARGS) 5767 { 5768 static const int data[][3] = { 5769 { 0x00, 0x4a, PSM_TPINFO(sensitivity) }, 5770 { 0x00, 0x4d, PSM_TPINFO(inertia) }, 5771 { 0x00, 0x60, PSM_TPINFO(uplateau) }, 5772 { 0x00, 0x57, PSM_TPINFO(reach) }, 5773 { 0x00, 0x58, PSM_TPINFO(draghys) }, 5774 { 0x00, 0x59, PSM_TPINFO(mindrag) }, 5775 { 0x00, 0x5a, PSM_TPINFO(upthresh) }, 5776 { 0x00, 0x5c, PSM_TPINFO(threshold) }, 5777 { 0x00, 0x5d, PSM_TPINFO(jenks) }, 5778 { 0x00, 0x5e, PSM_TPINFO(ztime) }, 5779 { 0x01, 0x2c, PSM_TPINFO(pts) }, 5780 { 0x08, 0x2d, PSM_TPINFO(skipback) } 5781 }; 5782 struct psm_softc *sc; 5783 int error, newval, *oldvalp; 5784 const int *tp; 5785 5786 if (arg1 == NULL || arg2 < 0 || arg2 >= nitems(data)) 5787 return (EINVAL); 5788 sc = arg1; 5789 tp = data[arg2]; 5790 oldvalp = (int *)((intptr_t)sc + tp[TPINFO]); 5791 newval = *oldvalp; 5792 error = sysctl_handle_int(oidp, &newval, 0, req); 5793 if (error != 0) 5794 return (error); 5795 if (newval == *oldvalp) 5796 return (0); 5797 if (newval < 0 || newval > (tp[TPMASK] == 0 ? 255 : 1)) 5798 return (EINVAL); 5799 error = trackpoint_command(sc, tp[TPMASK] == 0 ? 0x81 : 0x47, 5800 tp[TPLOC], tp[TPMASK] == 0 ? newval : tp[TPMASK]); 5801 if (error != 0) 5802 return (error); 5803 *oldvalp = newval; 5804 5805 return (0); 5806 } 5807 5808 static void 5809 trackpoint_sysctl_create_tree(struct psm_softc *sc) 5810 { 5811 5812 if (sc->tpinfo.sysctl_tree != NULL) 5813 return; 5814 5815 /* Attach extra trackpoint sysctl nodes under hw.psm.trackpoint */ 5816 sysctl_ctx_init(&sc->tpinfo.sysctl_ctx); 5817 sc->tpinfo.sysctl_tree = SYSCTL_ADD_NODE(&sc->tpinfo.sysctl_ctx, 5818 SYSCTL_STATIC_CHILDREN(_hw_psm), OID_AUTO, "trackpoint", CTLFLAG_RD, 5819 0, "IBM/Lenovo TrackPoint"); 5820 5821 /* hw.psm.trackpoint.sensitivity */ 5822 sc->tpinfo.sensitivity = 0x80; 5823 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5824 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5825 "sensitivity", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5826 sc, TRACKPOINT_SYSCTL_SENSITIVITY, 5827 trackpoint_sysctl, "I", 5828 "Sensitivity"); 5829 5830 /* hw.psm.trackpoint.negative_inertia */ 5831 sc->tpinfo.inertia = 0x06; 5832 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5833 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5834 "negative_inertia", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5835 sc, TRACKPOINT_SYSCTL_NEGATIVE_INERTIA, 5836 trackpoint_sysctl, "I", 5837 "Negative inertia factor"); 5838 5839 /* hw.psm.trackpoint.upper_plateau */ 5840 sc->tpinfo.uplateau = 0x61; 5841 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5842 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5843 "upper_plateau", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5844 sc, TRACKPOINT_SYSCTL_UPPER_PLATEAU, 5845 trackpoint_sysctl, "I", 5846 "Transfer function upper plateau speed"); 5847 5848 /* hw.psm.trackpoint.backup_range */ 5849 sc->tpinfo.reach = 0x0a; 5850 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5851 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5852 "backup_range", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5853 sc, TRACKPOINT_SYSCTL_BACKUP_RANGE, 5854 trackpoint_sysctl, "I", 5855 "Backup range"); 5856 5857 /* hw.psm.trackpoint.drag_hysteresis */ 5858 sc->tpinfo.draghys = 0xff; 5859 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5860 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5861 "drag_hysteresis", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5862 sc, TRACKPOINT_SYSCTL_DRAG_HYSTERESIS, 5863 trackpoint_sysctl, "I", 5864 "Drag hysteresis"); 5865 5866 /* hw.psm.trackpoint.minimum_drag */ 5867 sc->tpinfo.mindrag = 0x14; 5868 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5869 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5870 "minimum_drag", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5871 sc, TRACKPOINT_SYSCTL_MINIMUM_DRAG, 5872 trackpoint_sysctl, "I", 5873 "Minimum drag"); 5874 5875 /* hw.psm.trackpoint.up_threshold */ 5876 sc->tpinfo.upthresh = 0xff; 5877 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5878 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5879 "up_threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5880 sc, TRACKPOINT_SYSCTL_UP_THRESHOLD, 5881 trackpoint_sysctl, "I", 5882 "Up threshold for release"); 5883 5884 /* hw.psm.trackpoint.threshold */ 5885 sc->tpinfo.threshold = 0x08; 5886 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5887 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5888 "threshold", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5889 sc, TRACKPOINT_SYSCTL_THRESHOLD, 5890 trackpoint_sysctl, "I", 5891 "Threshold"); 5892 5893 /* hw.psm.trackpoint.jenks_curvature */ 5894 sc->tpinfo.jenks = 0x87; 5895 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5896 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5897 "jenks_curvature", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5898 sc, TRACKPOINT_SYSCTL_JENKS_CURVATURE, 5899 trackpoint_sysctl, "I", 5900 "Jenks curvature"); 5901 5902 /* hw.psm.trackpoint.z_time */ 5903 sc->tpinfo.ztime = 0x26; 5904 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5905 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5906 "z_time", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5907 sc, TRACKPOINT_SYSCTL_Z_TIME, 5908 trackpoint_sysctl, "I", 5909 "Z time constant"); 5910 5911 /* hw.psm.trackpoint.press_to_select */ 5912 sc->tpinfo.pts = 0x00; 5913 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5914 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5915 "press_to_select", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5916 sc, TRACKPOINT_SYSCTL_PRESS_TO_SELECT, 5917 trackpoint_sysctl, "I", 5918 "Press to Select"); 5919 5920 /* hw.psm.trackpoint.skip_backups */ 5921 sc->tpinfo.skipback = 0x00; 5922 SYSCTL_ADD_PROC(&sc->tpinfo.sysctl_ctx, 5923 SYSCTL_CHILDREN(sc->tpinfo.sysctl_tree), OID_AUTO, 5924 "skip_backups", CTLTYPE_INT|CTLFLAG_RW|CTLFLAG_ANYBODY, 5925 sc, TRACKPOINT_SYSCTL_SKIP_BACKUPS, 5926 trackpoint_sysctl, "I", 5927 "Skip backups from drags"); 5928 } 5929 5930 static void 5931 set_trackpoint_parameters(struct psm_softc *sc) 5932 { 5933 trackpoint_command(sc, 0x81, 0x4a, sc->tpinfo.sensitivity); 5934 trackpoint_command(sc, 0x81, 0x60, sc->tpinfo.uplateau); 5935 trackpoint_command(sc, 0x81, 0x4d, sc->tpinfo.inertia); 5936 trackpoint_command(sc, 0x81, 0x57, sc->tpinfo.reach); 5937 trackpoint_command(sc, 0x81, 0x58, sc->tpinfo.draghys); 5938 trackpoint_command(sc, 0x81, 0x59, sc->tpinfo.mindrag); 5939 trackpoint_command(sc, 0x81, 0x5a, sc->tpinfo.upthresh); 5940 trackpoint_command(sc, 0x81, 0x5c, sc->tpinfo.threshold); 5941 trackpoint_command(sc, 0x81, 0x5d, sc->tpinfo.jenks); 5942 trackpoint_command(sc, 0x81, 0x5e, sc->tpinfo.ztime); 5943 if (sc->tpinfo.pts == 0x01) 5944 trackpoint_command(sc, 0x47, 0x2c, 0x01); 5945 if (sc->tpinfo.skipback == 0x01) 5946 trackpoint_command(sc, 0x47, 0x2d, 0x08); 5947 } 5948 5949 static int 5950 enable_trackpoint(struct psm_softc *sc, enum probearg arg) 5951 { 5952 KBDC kbdc = sc->kbdc; 5953 int id; 5954 5955 /* 5956 * If called from enable_synaptics(), make sure that passthrough 5957 * mode is enabled so we can reach the trackpoint. 5958 * However, passthrough mode must be disabled before setting the 5959 * trackpoint parameters, as rackpoint_command() enables and disables 5960 * passthrough mode on its own. 5961 */ 5962 if (sc->synhw.capPassthrough) 5963 synaptics_passthrough_on(sc); 5964 5965 if (send_aux_command(kbdc, 0xe1) != PSM_ACK || 5966 read_aux_data(kbdc) != 0x01) 5967 goto no_trackpoint; 5968 id = read_aux_data(kbdc); 5969 if (id < 0x01) 5970 goto no_trackpoint; 5971 if (arg == PROBE) 5972 sc->tphw = id; 5973 if (!trackpoint_support) 5974 goto no_trackpoint; 5975 5976 if (sc->synhw.capPassthrough) 5977 synaptics_passthrough_off(sc); 5978 5979 if (arg == PROBE) { 5980 trackpoint_sysctl_create_tree(sc); 5981 /* 5982 * Don't overwrite hwid and buttons when we are 5983 * a guest device. 5984 */ 5985 if (!sc->synhw.capPassthrough) { 5986 sc->hw.hwid = id; 5987 sc->hw.buttons = 3; 5988 } 5989 } 5990 5991 set_trackpoint_parameters(sc); 5992 5993 return (TRUE); 5994 5995 no_trackpoint: 5996 if (sc->synhw.capPassthrough) 5997 synaptics_passthrough_off(sc); 5998 5999 return (FALSE); 6000 } 6001 6002 /* Interlink electronics VersaPad */ 6003 static int 6004 enable_versapad(struct psm_softc *sc, enum probearg arg) 6005 { 6006 KBDC kbdc = sc->kbdc; 6007 int data[3]; 6008 6009 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */ 6010 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */ 6011 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6012 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6013 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6014 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6015 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */ 6016 return (FALSE); 6017 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */ 6018 return (FALSE); 6019 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */ 6020 6021 return (TRUE); /* PS/2 absolute mode */ 6022 } 6023 6024 /* Elantech Touchpad */ 6025 static int 6026 elantech_read_1(KBDC kbdc, int hwversion, int reg, int *val) 6027 { 6028 int res, readcmd, retidx; 6029 int resp[3]; 6030 6031 readcmd = hwversion == 2 ? ELANTECH_REG_READ : ELANTECH_REG_RDWR; 6032 retidx = hwversion == 4 ? 1 : 0; 6033 6034 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6035 res |= send_aux_command(kbdc, readcmd) != PSM_ACK; 6036 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6037 res |= send_aux_command(kbdc, reg) != PSM_ACK; 6038 res |= get_mouse_status(kbdc, resp, 0, 3) != 3; 6039 6040 if (res == 0) 6041 *val = resp[retidx]; 6042 6043 return (res); 6044 } 6045 6046 static int 6047 elantech_write_1(KBDC kbdc, int hwversion, int reg, int val) 6048 { 6049 int res, writecmd; 6050 6051 writecmd = hwversion == 2 ? ELANTECH_REG_WRITE : ELANTECH_REG_RDWR; 6052 6053 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6054 res |= send_aux_command(kbdc, writecmd) != PSM_ACK; 6055 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6056 res |= send_aux_command(kbdc, reg) != PSM_ACK; 6057 if (hwversion == 4) { 6058 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6059 res |= send_aux_command(kbdc, writecmd) != PSM_ACK; 6060 } 6061 res |= send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6062 res |= send_aux_command(kbdc, val) != PSM_ACK; 6063 res |= set_mouse_scaling(kbdc, 1) == 0; 6064 6065 return (res); 6066 } 6067 6068 static int 6069 elantech_cmd(KBDC kbdc, int hwversion, int cmd, int *resp) 6070 { 6071 int res; 6072 6073 if (hwversion == 2) { 6074 res = set_mouse_scaling(kbdc, 1) == 0; 6075 res |= mouse_ext_command(kbdc, cmd) == 0; 6076 } else { 6077 res = send_aux_command(kbdc, ELANTECH_CUSTOM_CMD) != PSM_ACK; 6078 res |= send_aux_command(kbdc, cmd) != PSM_ACK; 6079 } 6080 res |= get_mouse_status(kbdc, resp, 0, 3) != 3; 6081 6082 return (res); 6083 } 6084 6085 static int 6086 elantech_init(KBDC kbdc, elantechhw_t *elanhw) 6087 { 6088 int i, val, res, hwversion, reg10; 6089 6090 /* set absolute mode */ 6091 hwversion = elanhw->hwversion; 6092 reg10 = -1; 6093 switch (hwversion) { 6094 case 2: 6095 reg10 = elanhw->fwversion == 0x020030 ? 0x54 : 0xc4; 6096 res = elantech_write_1(kbdc, hwversion, 0x10, reg10); 6097 if (res) 6098 break; 6099 res = elantech_write_1(kbdc, hwversion, 0x11, 0x8A); 6100 break; 6101 case 3: 6102 reg10 = 0x0b; 6103 res = elantech_write_1(kbdc, hwversion, 0x10, reg10); 6104 break; 6105 case 4: 6106 res = elantech_write_1(kbdc, hwversion, 0x07, 0x01); 6107 break; 6108 default: 6109 res = 1; 6110 } 6111 6112 /* Read back reg 0x10 to ensure hardware is ready. */ 6113 if (res == 0 && reg10 >= 0) { 6114 for (i = 0; i < 5; i++) { 6115 if (elantech_read_1(kbdc, hwversion, 0x10, &val) == 0) 6116 break; 6117 DELAY(2000); 6118 } 6119 if (i == 5) 6120 res = 1; 6121 } 6122 6123 if (res) 6124 printf("couldn't set absolute mode\n"); 6125 6126 return (res); 6127 } 6128 6129 static void 6130 elantech_init_synaptics(struct psm_softc *sc) 6131 { 6132 6133 /* Set capabilites required by movement smother */ 6134 sc->synhw.infoMajor = sc->elanhw.hwversion; 6135 sc->synhw.infoMinor = sc->elanhw.fwversion; 6136 sc->synhw.infoXupmm = sc->elanhw.dpmmx; 6137 sc->synhw.infoYupmm = sc->elanhw.dpmmy; 6138 sc->synhw.verticalScroll = 0; 6139 sc->synhw.nExtendedQueries = 4; 6140 sc->synhw.capExtended = 1; 6141 sc->synhw.capPassthrough = sc->elanhw.hastrackpoint; 6142 sc->synhw.capClickPad = sc->elanhw.isclickpad; 6143 sc->synhw.capMultiFinger = 1; 6144 sc->synhw.capAdvancedGestures = 1; 6145 sc->synhw.capPalmDetect = 1; 6146 sc->synhw.capPen = 0; 6147 sc->synhw.capReportsMax = 1; 6148 sc->synhw.maximumXCoord = sc->elanhw.sizex; 6149 sc->synhw.maximumYCoord = sc->elanhw.sizey; 6150 sc->synhw.capReportsMin = 1; 6151 sc->synhw.minimumXCoord = 0; 6152 sc->synhw.minimumYCoord = 0; 6153 6154 if (sc->syninfo.sysctl_tree == NULL) { 6155 synaptics_sysctl_create_tree(sc, "elantech", 6156 "Elantech Touchpad"); 6157 6158 /* 6159 * Adjust synaptic smoother tunables 6160 * 1. Disable finger detection pressure threshold. Unlike 6161 * synaptics we assume the finger is acting when packet with 6162 * its X&Y arrives not when pressure exceedes some threshold 6163 * 2. Disable unrelated features like margins and noisy areas 6164 * 3. Disable virtual scroll areas as 2nd finger is preferable 6165 * 4. For clickpads set bottom quarter as 42% - 16% - 42% sized 6166 * softbuttons 6167 * 5. Scale down divisors and movement lengths by a factor of 3 6168 * where 3 is Synaptics to Elantech (~2200/800) dpi ratio 6169 */ 6170 6171 /* Set reporting range to be equal touchpad size */ 6172 sc->syninfo.max_x = sc->elanhw.sizex; 6173 sc->syninfo.max_y = sc->elanhw.sizey; 6174 6175 /* Disable finger detection pressure threshold */ 6176 sc->syninfo.min_pressure = 1; 6177 6178 /* Use full area of touchpad */ 6179 sc->syninfo.margin_top = 0; 6180 sc->syninfo.margin_right = 0; 6181 sc->syninfo.margin_bottom = 0; 6182 sc->syninfo.margin_left = 0; 6183 6184 /* Disable noisy area */ 6185 sc->syninfo.na_top = 0; 6186 sc->syninfo.na_right = 0; 6187 sc->syninfo.na_bottom = 0; 6188 sc->syninfo.na_left = 0; 6189 6190 /* Tune divisors and movement lengths */ 6191 sc->syninfo.weight_len_squared = 200; 6192 sc->syninfo.div_min = 3; 6193 sc->syninfo.div_max = 6; 6194 sc->syninfo.div_max_na = 10; 6195 sc->syninfo.div_len = 30; 6196 sc->syninfo.tap_max_delta = 25; 6197 6198 /* Disable virtual scrolling areas and tune its divisors */ 6199 sc->syninfo.vscroll_hor_area = 0; 6200 sc->syninfo.vscroll_ver_area = 0; 6201 sc->syninfo.vscroll_min_delta = 15; 6202 sc->syninfo.vscroll_div_min = 30; 6203 sc->syninfo.vscroll_div_max = 50; 6204 6205 /* Set bottom quarter as 42% - 16% - 42% sized softbuttons */ 6206 if (sc->elanhw.isclickpad) { 6207 sc->syninfo.softbuttons_y = sc->elanhw.sizey / 4; 6208 sc->syninfo.softbutton2_x = sc->elanhw.sizex * 11 / 25; 6209 sc->syninfo.softbutton3_x = sc->elanhw.sizex * 14 / 25; 6210 } 6211 } 6212 6213 return; 6214 } 6215 6216 static int 6217 enable_elantech(struct psm_softc *sc, enum probearg arg) 6218 { 6219 static const int ic2hw[] = 6220 /*IC: 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 6221 { 0, 0, 2, 0, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0 }; 6222 elantechhw_t elanhw; 6223 int icversion, hwversion, dptracex, dptracey, id, resp[3], dpix, dpiy; 6224 KBDC kbdc = sc->kbdc; 6225 6226 VLOG(3, (LOG_DEBUG, "elantech: BEGIN init\n")); 6227 6228 set_mouse_scaling(kbdc, 1); 6229 set_mouse_scaling(kbdc, 1); 6230 set_mouse_scaling(kbdc, 1); 6231 if (get_mouse_status(kbdc, resp, 0, 3) != 3) 6232 return (FALSE); 6233 6234 if (!ELANTECH_MAGIC(resp)) 6235 return (FALSE); 6236 6237 /* Identify the Touchpad version. */ 6238 if (elantech_cmd(kbdc, 2, ELANTECH_FW_VERSION, resp)) 6239 return (FALSE); 6240 6241 bzero(&elanhw, sizeof(elanhw)); 6242 6243 elanhw.fwversion = (resp[0] << 16) | (resp[1] << 8) | resp[2]; 6244 icversion = resp[0] & 0x0f; 6245 hwversion = ic2hw[icversion]; 6246 6247 if (verbose >= 2) 6248 printf("Elantech touchpad hardware v.%d firmware v.0x%06x\n", 6249 hwversion, elanhw.fwversion); 6250 6251 if (ELANTECH_HW_IS_V1(elanhw.fwversion)) { 6252 printf (" Unsupported touchpad hardware (v1)\n"); 6253 return (FALSE); 6254 } 6255 if (hwversion == 0) { 6256 printf (" Unknown touchpad hardware (firmware v.0x%06x)\n", 6257 elanhw.fwversion); 6258 return (FALSE); 6259 } 6260 6261 /* Get the Touchpad model information. */ 6262 elanhw.hwversion = hwversion; 6263 elanhw.issemimt = hwversion == 2; 6264 elanhw.isclickpad = (resp[1] & 0x10) != 0; 6265 elanhw.hascrc = (resp[1] & 0x40) != 0; 6266 elanhw.haspressure = elanhw.fwversion >= 0x020800; 6267 6268 /* Read the capability bits. */ 6269 if (elantech_cmd(kbdc, hwversion, ELANTECH_CAPABILITIES, resp) != 0) { 6270 printf(" Failed to read capability bits\n"); 6271 return (FALSE); 6272 } 6273 6274 elanhw.ntracesx = resp[1] - 1; 6275 elanhw.ntracesy = resp[2] - 1; 6276 elanhw.hastrackpoint = (resp[0] & 0x80) != 0; 6277 6278 /* Get the touchpad resolution */ 6279 switch (hwversion) { 6280 case 4: 6281 if (elantech_cmd(kbdc, hwversion, ELANTECH_RESOLUTION, resp) 6282 == 0) { 6283 dpix = (resp[1] & 0x0f) * 10 + 790; 6284 dpiy = ((resp[1] & 0xf0) >> 4) * 10 + 790; 6285 elanhw.dpmmx = (dpix * 10 + 5) / 254; 6286 elanhw.dpmmy = (dpiy * 10 + 5) / 254; 6287 break; 6288 } 6289 /* FALLTHROUGH */ 6290 case 2: 6291 case 3: 6292 elanhw.dpmmx = elanhw.dpmmy = 32; /* 800 dpi */ 6293 break; 6294 } 6295 6296 if (!elantech_support) 6297 return (FALSE); 6298 6299 if (elantech_init(kbdc, &elanhw)) { 6300 printf("couldn't initialize elantech touchpad\n"); 6301 return (FALSE); 6302 } 6303 6304 /* 6305 * Get the touchpad reporting range. 6306 * On HW v.3 touchpads it should be done after switching hardware 6307 * to real resolution mode (by setting bit 3 of reg10) 6308 */ 6309 if (elantech_cmd(kbdc, hwversion, ELANTECH_FW_ID, resp) != 0) { 6310 printf(" Failed to read touchpad size\n"); 6311 elanhw.sizex = 10000; /* Arbitrary high values to */ 6312 elanhw.sizey = 10000; /* prevent clipping in smoother */ 6313 } else if (hwversion == 2) { 6314 dptracex = dptracey = 64; 6315 if ((elanhw.fwversion >> 16) == 0x14 && (resp[1] & 0x10) && 6316 !elantech_cmd(kbdc, hwversion, ELANTECH_SAMPLE, resp)) { 6317 dptracex = resp[1] / 2; 6318 dptracey = resp[2] / 2; 6319 } 6320 elanhw.sizex = (elanhw.ntracesx - 1) * dptracex; 6321 elanhw.sizey = (elanhw.ntracesy - 1) * dptracey; 6322 } else { 6323 elanhw.sizex = (resp[0] & 0x0f) << 8 | resp[1]; 6324 elanhw.sizey = (resp[0] & 0xf0) << 4 | resp[2]; 6325 } 6326 6327 if (verbose >= 2) { 6328 printf(" Model information:\n"); 6329 printf(" MaxX: %d\n", elanhw.sizex); 6330 printf(" MaxY: %d\n", elanhw.sizey); 6331 printf(" DpmmX: %d\n", elanhw.dpmmx); 6332 printf(" DpmmY: %d\n", elanhw.dpmmy); 6333 printf(" TracesX: %d\n", elanhw.ntracesx); 6334 printf(" TracesY: %d\n", elanhw.ntracesy); 6335 printf(" SemiMT: %d\n", elanhw.issemimt); 6336 printf(" Clickpad: %d\n", elanhw.isclickpad); 6337 printf(" Trackpoint: %d\n", elanhw.hastrackpoint); 6338 printf(" CRC: %d\n", elanhw.hascrc); 6339 printf(" Pressure: %d\n", elanhw.haspressure); 6340 } 6341 6342 VLOG(3, (LOG_DEBUG, "elantech: END init\n")); 6343 6344 if (arg == PROBE) { 6345 sc->elanhw = elanhw; 6346 sc->hw.buttons = 3; 6347 6348 /* Initialize synaptics movement smoother */ 6349 elantech_init_synaptics(sc); 6350 6351 for (id = 0; id < ELANTECH_MAX_FINGERS; id++) 6352 PSM_FINGER_RESET(sc->elanaction.fingers[id]); 6353 } 6354 6355 return (TRUE); 6356 } 6357 6358 /* 6359 * Return true if 'now' is earlier than (start + (secs.usecs)). 6360 * Now may be NULL and the function will fetch the current time from 6361 * getmicrouptime(), or a cached 'now' can be passed in. 6362 * All values should be numbers derived from getmicrouptime(). 6363 */ 6364 static int 6365 timeelapsed(start, secs, usecs, now) 6366 const struct timeval *start, *now; 6367 int secs, usecs; 6368 { 6369 struct timeval snow, tv; 6370 6371 /* if there is no 'now' passed in, the get it as a convience. */ 6372 if (now == NULL) { 6373 getmicrouptime(&snow); 6374 now = &snow; 6375 } 6376 6377 tv.tv_sec = secs; 6378 tv.tv_usec = usecs; 6379 timevaladd(&tv, start); 6380 return (timevalcmp(&tv, now, <)); 6381 } 6382 6383 static int 6384 psmresume(device_t dev) 6385 { 6386 struct psm_softc *sc = device_get_softc(dev); 6387 int unit = device_get_unit(dev); 6388 int err; 6389 6390 VLOG(2, (LOG_NOTICE, "psm%d: system resume hook called.\n", unit)); 6391 6392 if ((sc->config & 6393 (PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND)) == 0) 6394 return (0); 6395 6396 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND); 6397 6398 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) { 6399 /* 6400 * Release the blocked process; it must be notified that 6401 * the device cannot be accessed anymore. 6402 */ 6403 sc->state &= ~PSM_ASLP; 6404 wakeup(sc); 6405 } 6406 6407 VLOG(2, (LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit)); 6408 6409 return (err); 6410 } 6411 6412 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0); 6413 6414 #ifdef DEV_ISA 6415 6416 /* 6417 * This sucks up assignments from PNPBIOS and ACPI. 6418 */ 6419 6420 /* 6421 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may 6422 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device 6423 * can be probed and attached only after the AT keyboard controller is 6424 * attached, we shall quietly reserve the IRQ resource for later use. 6425 * If the PS/2 mouse device is reported to us AFTER the keyboard controller, 6426 * copy the IRQ resource to the PS/2 mouse device instance hanging 6427 * under the keyboard controller, then probe and attach it. 6428 */ 6429 6430 static devclass_t psmcpnp_devclass; 6431 6432 static device_probe_t psmcpnp_probe; 6433 static device_attach_t psmcpnp_attach; 6434 6435 static device_method_t psmcpnp_methods[] = { 6436 DEVMETHOD(device_probe, psmcpnp_probe), 6437 DEVMETHOD(device_attach, psmcpnp_attach), 6438 6439 { 0, 0 } 6440 }; 6441 6442 static driver_t psmcpnp_driver = { 6443 PSMCPNP_DRIVER_NAME, 6444 psmcpnp_methods, 6445 1, /* no softc */ 6446 }; 6447 6448 static struct isa_pnp_id psmcpnp_ids[] = { 6449 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */ 6450 { 0x0e0fd041, "PS/2 mouse port" }, /* PNP0F0E */ 6451 { 0x120fd041, "PS/2 mouse port" }, /* PNP0F12 */ 6452 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */ 6453 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */ 6454 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */ 6455 { 0x0002a906, "ALPS Glide Point" }, /* ALPS Glide Point */ 6456 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */ 6457 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */ 6458 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */ 6459 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */ 6460 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */ 6461 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */ 6462 { 0 } 6463 }; 6464 6465 static int 6466 create_a_copy(device_t atkbdc, device_t me) 6467 { 6468 device_t psm; 6469 u_long irq; 6470 6471 /* find the PS/2 mouse device instance under the keyboard controller */ 6472 psm = device_find_child(atkbdc, PSM_DRIVER_NAME, 6473 device_get_unit(atkbdc)); 6474 if (psm == NULL) 6475 return (ENXIO); 6476 if (device_get_state(psm) != DS_NOTPRESENT) 6477 return (0); 6478 6479 /* move our resource to the found device */ 6480 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0); 6481 bus_delete_resource(me, SYS_RES_IRQ, 0); 6482 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1); 6483 6484 /* ...then probe and attach it */ 6485 return (device_probe_and_attach(psm)); 6486 } 6487 6488 static int 6489 psmcpnp_probe(device_t dev) 6490 { 6491 struct resource *res; 6492 u_long irq; 6493 int rid; 6494 6495 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids)) 6496 return (ENXIO); 6497 6498 /* 6499 * The PnP BIOS and ACPI are supposed to assign an IRQ (12) 6500 * to the PS/2 mouse device node. But, some buggy PnP BIOS 6501 * declares the PS/2 mouse device node without an IRQ resource! 6502 * If this happens, we shall refer to device hints. 6503 * If we still don't find it there, use a hardcoded value... XXX 6504 */ 6505 rid = 0; 6506 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid); 6507 if (irq <= 0) { 6508 if (resource_long_value(PSM_DRIVER_NAME, 6509 device_get_unit(dev),"irq", &irq) != 0) 6510 irq = 12; /* XXX */ 6511 device_printf(dev, "irq resource info is missing; " 6512 "assuming irq %ld\n", irq); 6513 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1); 6514 } 6515 res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 0); 6516 bus_release_resource(dev, SYS_RES_IRQ, rid, res); 6517 6518 /* keep quiet */ 6519 if (!bootverbose) 6520 device_quiet(dev); 6521 6522 return ((res == NULL) ? ENXIO : 0); 6523 } 6524 6525 static int 6526 psmcpnp_attach(device_t dev) 6527 { 6528 device_t atkbdc; 6529 6530 /* find the keyboard controller, which may be on acpi* or isa* bus */ 6531 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME), 6532 device_get_unit(dev)); 6533 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) 6534 create_a_copy(atkbdc, dev); 6535 6536 return (0); 6537 } 6538 6539 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0); 6540 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0); 6541 6542 #endif /* DEV_ISA */ 6543