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