1/*- 2 * Copyright (c) 2012 Huang Wen Hui 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/10/sys/dev/usb/input/wsp.c 331997 2018-04-04 08:45:41Z hselasky $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/kernel.h> 33#include <sys/malloc.h> 34#include <sys/module.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/bus.h> 38#include <sys/conf.h> 39#include <sys/fcntl.h> 40#include <sys/file.h> 41#include <sys/selinfo.h> 42#include <sys/poll.h> 43#include <sys/sysctl.h> 44 45#include <dev/usb/usb.h> 46#include <dev/usb/usbdi.h> 47#include <dev/usb/usbdi_util.h> 48#include <dev/usb/usbhid.h> 49 50#include "usbdevs.h" 51 52#define USB_DEBUG_VAR wsp_debug 53#include <dev/usb/usb_debug.h> 54 55#include <sys/mouse.h> 56 57#define WSP_DRIVER_NAME "wsp" 58#define WSP_BUFFER_MAX 1024 59 60#define WSP_CLAMP(x,low,high) do { \ 61 if ((x) < (low)) \ 62 (x) = (low); \ 63 else if ((x) > (high)) \ 64 (x) = (high); \ 65} while (0) 66 67/* Tunables */ 68static SYSCTL_NODE(_hw_usb, OID_AUTO, wsp, CTLFLAG_RW, 0, "USB wsp"); 69 70#ifdef USB_DEBUG 71enum wsp_log_level { 72 WSP_LLEVEL_DISABLED = 0, 73 WSP_LLEVEL_ERROR, 74 WSP_LLEVEL_DEBUG, /* for troubleshooting */ 75 WSP_LLEVEL_INFO, /* for diagnostics */ 76}; 77static int wsp_debug = WSP_LLEVEL_ERROR;/* the default is to only log errors */ 78 79SYSCTL_INT(_hw_usb_wsp, OID_AUTO, debug, CTLFLAG_RW, 80 &wsp_debug, WSP_LLEVEL_ERROR, "WSP debug level"); 81#endif /* USB_DEBUG */ 82 83static struct wsp_tuning { 84 int scale_factor; 85 int z_factor; 86 int pressure_touch_threshold; 87 int pressure_untouch_threshold; 88 int pressure_tap_threshold; 89 int scr_hor_threshold; 90} 91 wsp_tuning = 92{ 93 .scale_factor = 12, 94 .z_factor = 5, 95 .pressure_touch_threshold = 50, 96 .pressure_untouch_threshold = 10, 97 .pressure_tap_threshold = 120, 98 .scr_hor_threshold = 20, 99}; 100 101static void 102wsp_runing_rangecheck(struct wsp_tuning *ptun) 103{ 104 WSP_CLAMP(ptun->scale_factor, 1, 63); 105 WSP_CLAMP(ptun->z_factor, 1, 63); 106 WSP_CLAMP(ptun->pressure_touch_threshold, 1, 255); 107 WSP_CLAMP(ptun->pressure_untouch_threshold, 1, 255); 108 WSP_CLAMP(ptun->pressure_tap_threshold, 1, 255); 109 WSP_CLAMP(ptun->scr_hor_threshold, 1, 255); 110} 111 112SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scale_factor, CTLFLAG_RW, 113 &wsp_tuning.scale_factor, 0, "movement scale factor"); 114SYSCTL_INT(_hw_usb_wsp, OID_AUTO, z_factor, CTLFLAG_RW, 115 &wsp_tuning.z_factor, 0, "Z-axis scale factor"); 116SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_touch_threshold, CTLFLAG_RW, 117 &wsp_tuning.pressure_touch_threshold, 0, "touch pressure threshold"); 118SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_untouch_threshold, CTLFLAG_RW, 119 &wsp_tuning.pressure_untouch_threshold, 0, "untouch pressure threshold"); 120SYSCTL_INT(_hw_usb_wsp, OID_AUTO, pressure_tap_threshold, CTLFLAG_RW, 121 &wsp_tuning.pressure_tap_threshold, 0, "tap pressure threshold"); 122SYSCTL_INT(_hw_usb_wsp, OID_AUTO, scr_hor_threshold, CTLFLAG_RW, 123 &wsp_tuning.scr_hor_threshold, 0, "horizontal scrolling threshold"); 124 125/* 126 * Some tables, structures, definitions and constant values for the 127 * touchpad protocol has been copied from Linux's 128 * "drivers/input/mouse/bcm5974.c" which has the following copyright 129 * holders under GPLv2. All device specific code in this driver has 130 * been written from scratch. The decoding algorithm is based on 131 * output from FreeBSD's usbdump. 132 * 133 * Copyright (C) 2008 Henrik Rydberg (rydberg@euromail.se) 134 * Copyright (C) 2008 Scott Shawcroft (scott.shawcroft@gmail.com) 135 * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com) 136 * Copyright (C) 2005 Johannes Berg (johannes@sipsolutions.net) 137 * Copyright (C) 2005 Stelian Pop (stelian@popies.net) 138 * Copyright (C) 2005 Frank Arnold (frank@scirocco-5v-turbo.de) 139 * Copyright (C) 2005 Peter Osterlund (petero2@telia.com) 140 * Copyright (C) 2005 Michael Hanselmann (linux-kernel@hansmi.ch) 141 * Copyright (C) 2006 Nicolas Boichat (nicolas@boichat.ch) 142 */ 143 144/* button data structure */ 145struct bt_data { 146 uint8_t unknown1; /* constant */ 147 uint8_t button; /* left button */ 148 uint8_t rel_x; /* relative x coordinate */ 149 uint8_t rel_y; /* relative y coordinate */ 150} __packed; 151 152/* trackpad header types */ 153enum tp_type { 154 TYPE1, /* plain trackpad */ 155 TYPE2, /* button integrated in trackpad */ 156 TYPE3, /* additional header fields since June 2013 */ 157 TYPE4 /* additional header field for pressure data */ 158}; 159 160/* trackpad finger data offsets, le16-aligned */ 161#define FINGER_TYPE1 (13 * 2) 162#define FINGER_TYPE2 (15 * 2) 163#define FINGER_TYPE3 (19 * 2) 164#define FINGER_TYPE4 (23 * 2) 165 166/* trackpad button data offsets */ 167#define BUTTON_TYPE2 15 168#define BUTTON_TYPE3 23 169#define BUTTON_TYPE4 31 170 171/* list of device capability bits */ 172#define HAS_INTEGRATED_BUTTON 1 173 174/* trackpad finger data block size */ 175#define FSIZE_TYPE1 (14 * 2) 176#define FSIZE_TYPE2 (14 * 2) 177#define FSIZE_TYPE3 (14 * 2) 178#define FSIZE_TYPE4 (15 * 2) 179 180/* trackpad finger header - little endian */ 181struct tp_header { 182 uint8_t flag; 183 uint8_t sn0; 184 uint16_t wFixed0; 185 uint32_t dwSn1; 186 uint32_t dwFixed1; 187 uint16_t wLength; 188 uint8_t nfinger; 189 uint8_t ibt; 190 int16_t wUnknown[6]; 191 uint8_t q1; 192 uint8_t q2; 193} __packed; 194 195/* trackpad finger structure - little endian */ 196struct tp_finger { 197 int16_t origin; /* zero when switching track finger */ 198 int16_t abs_x; /* absolute x coodinate */ 199 int16_t abs_y; /* absolute y coodinate */ 200 int16_t rel_x; /* relative x coodinate */ 201 int16_t rel_y; /* relative y coodinate */ 202 int16_t tool_major; /* tool area, major axis */ 203 int16_t tool_minor; /* tool area, minor axis */ 204 int16_t orientation; /* 16384 when point, else 15 bit angle */ 205 int16_t touch_major; /* touch area, major axis */ 206 int16_t touch_minor; /* touch area, minor axis */ 207 int16_t unused[2]; /* zeros */ 208 int16_t pressure; /* pressure on forcetouch touchpad */ 209 int16_t multi; /* one finger: varies, more fingers: 210 * constant */ 211} __packed; 212 213/* trackpad finger data size, empirically at least ten fingers */ 214#define MAX_FINGERS 16 215#define SIZEOF_FINGER sizeof(struct tp_finger) 216#define SIZEOF_ALL_FINGERS (MAX_FINGERS * SIZEOF_FINGER) 217 218#if (WSP_BUFFER_MAX < ((MAX_FINGERS * FSIZE_TYPE4) + FINGER_TYPE4)) 219#error "WSP_BUFFER_MAX is too small" 220#endif 221 222enum { 223 WSP_FLAG_WELLSPRING1, 224 WSP_FLAG_WELLSPRING2, 225 WSP_FLAG_WELLSPRING3, 226 WSP_FLAG_WELLSPRING4, 227 WSP_FLAG_WELLSPRING4A, 228 WSP_FLAG_WELLSPRING5, 229 WSP_FLAG_WELLSPRING6A, 230 WSP_FLAG_WELLSPRING6, 231 WSP_FLAG_WELLSPRING5A, 232 WSP_FLAG_WELLSPRING7, 233 WSP_FLAG_WELLSPRING7A, 234 WSP_FLAG_WELLSPRING8, 235 WSP_FLAG_WELLSPRING9, 236 WSP_FLAG_MAX, 237}; 238 239/* device-specific configuration */ 240struct wsp_dev_params { 241 uint8_t caps; /* device capability bitmask */ 242 uint8_t tp_type; /* type of trackpad interface */ 243 uint8_t tp_button; /* offset to button data */ 244 uint8_t tp_offset; /* offset to trackpad finger data */ 245 uint8_t tp_fsize; /* bytes in single finger block */ 246 uint8_t tp_delta; /* offset from header to finger struct */ 247 uint8_t iface_index; 248 uint8_t um_size; /* usb control message length */ 249 uint8_t um_req_val; /* usb control message value */ 250 uint8_t um_req_idx; /* usb control message index */ 251 uint8_t um_switch_idx; /* usb control message mode switch index */ 252 uint8_t um_switch_on; /* usb control message mode switch on */ 253 uint8_t um_switch_off; /* usb control message mode switch off */ 254}; 255 256static const struct wsp_dev_params wsp_dev_params[WSP_FLAG_MAX] = { 257 [WSP_FLAG_WELLSPRING1] = { 258 .caps = 0, 259 .tp_type = TYPE1, 260 .tp_button = 0, 261 .tp_offset = FINGER_TYPE1, 262 .tp_fsize = FSIZE_TYPE1, 263 .tp_delta = 0, 264 .iface_index = 0, 265 .um_size = 8, 266 .um_req_val = 0x03, 267 .um_req_idx = 0x00, 268 .um_switch_idx = 0, 269 .um_switch_on = 0x01, 270 .um_switch_off = 0x08, 271 }, 272 [WSP_FLAG_WELLSPRING2] = { 273 .caps = 0, 274 .tp_type = TYPE1, 275 .tp_button = 0, 276 .tp_offset = FINGER_TYPE1, 277 .tp_fsize = FSIZE_TYPE1, 278 .tp_delta = 0, 279 .iface_index = 0, 280 .um_size = 8, 281 .um_req_val = 0x03, 282 .um_req_idx = 0x00, 283 .um_switch_idx = 0, 284 .um_switch_on = 0x01, 285 .um_switch_off = 0x08, 286 }, 287 [WSP_FLAG_WELLSPRING3] = { 288 .caps = HAS_INTEGRATED_BUTTON, 289 .tp_type = TYPE2, 290 .tp_button = BUTTON_TYPE2, 291 .tp_offset = FINGER_TYPE2, 292 .tp_fsize = FSIZE_TYPE2, 293 .tp_delta = 0, 294 .iface_index = 0, 295 .um_size = 8, 296 .um_req_val = 0x03, 297 .um_req_idx = 0x00, 298 .um_switch_idx = 0, 299 .um_switch_on = 0x01, 300 .um_switch_off = 0x08, 301 }, 302 [WSP_FLAG_WELLSPRING4] = { 303 .caps = HAS_INTEGRATED_BUTTON, 304 .tp_type = TYPE2, 305 .tp_button = BUTTON_TYPE2, 306 .tp_offset = FINGER_TYPE2, 307 .tp_fsize = FSIZE_TYPE2, 308 .tp_delta = 0, 309 .iface_index = 0, 310 .um_size = 8, 311 .um_req_val = 0x03, 312 .um_req_idx = 0x00, 313 .um_switch_idx = 0, 314 .um_switch_on = 0x01, 315 .um_switch_off = 0x08, 316 }, 317 [WSP_FLAG_WELLSPRING4A] = { 318 .caps = HAS_INTEGRATED_BUTTON, 319 .tp_type = TYPE2, 320 .tp_button = BUTTON_TYPE2, 321 .tp_offset = FINGER_TYPE2, 322 .tp_fsize = FSIZE_TYPE2, 323 .tp_delta = 0, 324 .iface_index = 0, 325 .um_size = 8, 326 .um_req_val = 0x03, 327 .um_req_idx = 0x00, 328 .um_switch_idx = 0, 329 .um_switch_on = 0x01, 330 .um_switch_off = 0x08, 331 }, 332 [WSP_FLAG_WELLSPRING5] = { 333 .caps = HAS_INTEGRATED_BUTTON, 334 .tp_type = TYPE2, 335 .tp_button = BUTTON_TYPE2, 336 .tp_offset = FINGER_TYPE2, 337 .tp_fsize = FSIZE_TYPE2, 338 .tp_delta = 0, 339 .iface_index = 0, 340 .um_size = 8, 341 .um_req_val = 0x03, 342 .um_req_idx = 0x00, 343 .um_switch_idx = 0, 344 .um_switch_on = 0x01, 345 .um_switch_off = 0x08, 346 }, 347 [WSP_FLAG_WELLSPRING6] = { 348 .caps = HAS_INTEGRATED_BUTTON, 349 .tp_type = TYPE2, 350 .tp_button = BUTTON_TYPE2, 351 .tp_offset = FINGER_TYPE2, 352 .tp_fsize = FSIZE_TYPE2, 353 .tp_delta = 0, 354 .iface_index = 0, 355 .um_size = 8, 356 .um_req_val = 0x03, 357 .um_req_idx = 0x00, 358 .um_switch_idx = 0, 359 .um_switch_on = 0x01, 360 .um_switch_off = 0x08, 361 }, 362 [WSP_FLAG_WELLSPRING5A] = { 363 .caps = HAS_INTEGRATED_BUTTON, 364 .tp_type = TYPE2, 365 .tp_button = BUTTON_TYPE2, 366 .tp_offset = FINGER_TYPE2, 367 .tp_fsize = FSIZE_TYPE2, 368 .tp_delta = 0, 369 .iface_index = 0, 370 .um_size = 8, 371 .um_req_val = 0x03, 372 .um_req_idx = 0x00, 373 .um_switch_idx = 0, 374 .um_switch_on = 0x01, 375 .um_switch_off = 0x08, 376 }, 377 [WSP_FLAG_WELLSPRING6A] = { 378 .caps = HAS_INTEGRATED_BUTTON, 379 .tp_type = TYPE2, 380 .tp_button = BUTTON_TYPE2, 381 .tp_offset = FINGER_TYPE2, 382 .tp_fsize = FSIZE_TYPE2, 383 .tp_delta = 0, 384 .um_size = 8, 385 .um_req_val = 0x03, 386 .um_req_idx = 0x00, 387 .um_switch_idx = 0, 388 .um_switch_on = 0x01, 389 .um_switch_off = 0x08, 390 }, 391 [WSP_FLAG_WELLSPRING7] = { 392 .caps = HAS_INTEGRATED_BUTTON, 393 .tp_type = TYPE2, 394 .tp_button = BUTTON_TYPE2, 395 .tp_offset = FINGER_TYPE2, 396 .tp_fsize = FSIZE_TYPE2, 397 .tp_delta = 0, 398 .iface_index = 0, 399 .um_size = 8, 400 .um_req_val = 0x03, 401 .um_req_idx = 0x00, 402 .um_switch_idx = 0, 403 .um_switch_on = 0x01, 404 .um_switch_off = 0x08, 405 }, 406 [WSP_FLAG_WELLSPRING7A] = { 407 .caps = HAS_INTEGRATED_BUTTON, 408 .tp_type = TYPE2, 409 .tp_button = BUTTON_TYPE2, 410 .tp_offset = FINGER_TYPE2, 411 .tp_fsize = FSIZE_TYPE2, 412 .tp_delta = 0, 413 .iface_index = 0, 414 .um_size = 8, 415 .um_req_val = 0x03, 416 .um_req_idx = 0x00, 417 .um_switch_idx = 0, 418 .um_switch_on = 0x01, 419 .um_switch_off = 0x08, 420 }, 421 [WSP_FLAG_WELLSPRING8] = { 422 .caps = HAS_INTEGRATED_BUTTON, 423 .tp_type = TYPE3, 424 .tp_button = BUTTON_TYPE3, 425 .tp_offset = FINGER_TYPE3, 426 .tp_fsize = FSIZE_TYPE3, 427 .tp_delta = 0, 428 .iface_index = 0, 429 .um_size = 8, 430 .um_req_val = 0x03, 431 .um_req_idx = 0x00, 432 .um_switch_idx = 0, 433 .um_switch_on = 0x01, 434 .um_switch_off = 0x08, 435 }, 436 [WSP_FLAG_WELLSPRING9] = { 437 .caps = HAS_INTEGRATED_BUTTON, 438 .tp_type = TYPE4, 439 .tp_button = BUTTON_TYPE4, 440 .tp_offset = FINGER_TYPE4, 441 .tp_fsize = FSIZE_TYPE4, 442 .tp_delta = 2, 443 .iface_index = 2, 444 .um_size = 2, 445 .um_req_val = 0x03, 446 .um_req_idx = 0x02, 447 .um_switch_idx = 1, 448 .um_switch_on = 0x01, 449 .um_switch_off = 0x00, 450 }, 451}; 452 453#define WSP_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) } 454 455static const STRUCT_USB_HOST_ID wsp_devs[] = { 456 /* MacbookAir1.1 */ 457 WSP_DEV(APPLE, WELLSPRING_ANSI, WSP_FLAG_WELLSPRING1), 458 WSP_DEV(APPLE, WELLSPRING_ISO, WSP_FLAG_WELLSPRING1), 459 WSP_DEV(APPLE, WELLSPRING_JIS, WSP_FLAG_WELLSPRING1), 460 461 /* MacbookProPenryn, aka wellspring2 */ 462 WSP_DEV(APPLE, WELLSPRING2_ANSI, WSP_FLAG_WELLSPRING2), 463 WSP_DEV(APPLE, WELLSPRING2_ISO, WSP_FLAG_WELLSPRING2), 464 WSP_DEV(APPLE, WELLSPRING2_JIS, WSP_FLAG_WELLSPRING2), 465 466 /* Macbook5,1 (unibody), aka wellspring3 */ 467 WSP_DEV(APPLE, WELLSPRING3_ANSI, WSP_FLAG_WELLSPRING3), 468 WSP_DEV(APPLE, WELLSPRING3_ISO, WSP_FLAG_WELLSPRING3), 469 WSP_DEV(APPLE, WELLSPRING3_JIS, WSP_FLAG_WELLSPRING3), 470 471 /* MacbookAir3,2 (unibody), aka wellspring4 */ 472 WSP_DEV(APPLE, WELLSPRING4_ANSI, WSP_FLAG_WELLSPRING4), 473 WSP_DEV(APPLE, WELLSPRING4_ISO, WSP_FLAG_WELLSPRING4), 474 WSP_DEV(APPLE, WELLSPRING4_JIS, WSP_FLAG_WELLSPRING4), 475 476 /* MacbookAir3,1 (unibody), aka wellspring4 */ 477 WSP_DEV(APPLE, WELLSPRING4A_ANSI, WSP_FLAG_WELLSPRING4A), 478 WSP_DEV(APPLE, WELLSPRING4A_ISO, WSP_FLAG_WELLSPRING4A), 479 WSP_DEV(APPLE, WELLSPRING4A_JIS, WSP_FLAG_WELLSPRING4A), 480 481 /* Macbook8 (unibody, March 2011) */ 482 WSP_DEV(APPLE, WELLSPRING5_ANSI, WSP_FLAG_WELLSPRING5), 483 WSP_DEV(APPLE, WELLSPRING5_ISO, WSP_FLAG_WELLSPRING5), 484 WSP_DEV(APPLE, WELLSPRING5_JIS, WSP_FLAG_WELLSPRING5), 485 486 /* MacbookAir4,1 (unibody, July 2011) */ 487 WSP_DEV(APPLE, WELLSPRING6A_ANSI, WSP_FLAG_WELLSPRING6A), 488 WSP_DEV(APPLE, WELLSPRING6A_ISO, WSP_FLAG_WELLSPRING6A), 489 WSP_DEV(APPLE, WELLSPRING6A_JIS, WSP_FLAG_WELLSPRING6A), 490 491 /* MacbookAir4,2 (unibody, July 2011) */ 492 WSP_DEV(APPLE, WELLSPRING6_ANSI, WSP_FLAG_WELLSPRING6), 493 WSP_DEV(APPLE, WELLSPRING6_ISO, WSP_FLAG_WELLSPRING6), 494 WSP_DEV(APPLE, WELLSPRING6_JIS, WSP_FLAG_WELLSPRING6), 495 496 /* Macbook8,2 (unibody) */ 497 WSP_DEV(APPLE, WELLSPRING5A_ANSI, WSP_FLAG_WELLSPRING5A), 498 WSP_DEV(APPLE, WELLSPRING5A_ISO, WSP_FLAG_WELLSPRING5A), 499 WSP_DEV(APPLE, WELLSPRING5A_JIS, WSP_FLAG_WELLSPRING5A), 500 501 /* MacbookPro10,1 (unibody, June 2012) */ 502 /* MacbookPro11,1-3 (unibody, June 2013) */ 503 WSP_DEV(APPLE, WELLSPRING7_ANSI, WSP_FLAG_WELLSPRING7), 504 WSP_DEV(APPLE, WELLSPRING7_ISO, WSP_FLAG_WELLSPRING7), 505 WSP_DEV(APPLE, WELLSPRING7_JIS, WSP_FLAG_WELLSPRING7), 506 507 /* MacbookPro10,2 (unibody, October 2012) */ 508 WSP_DEV(APPLE, WELLSPRING7A_ANSI, WSP_FLAG_WELLSPRING7A), 509 WSP_DEV(APPLE, WELLSPRING7A_ISO, WSP_FLAG_WELLSPRING7A), 510 WSP_DEV(APPLE, WELLSPRING7A_JIS, WSP_FLAG_WELLSPRING7A), 511 512 /* MacbookAir6,2 (unibody, June 2013) */ 513 WSP_DEV(APPLE, WELLSPRING8_ANSI, WSP_FLAG_WELLSPRING8), 514 WSP_DEV(APPLE, WELLSPRING8_ISO, WSP_FLAG_WELLSPRING8), 515 WSP_DEV(APPLE, WELLSPRING8_JIS, WSP_FLAG_WELLSPRING8), 516 517 /* MacbookPro12,1 MacbookPro11,4 */ 518 WSP_DEV(APPLE, WELLSPRING9_ANSI, WSP_FLAG_WELLSPRING9), 519 WSP_DEV(APPLE, WELLSPRING9_ISO, WSP_FLAG_WELLSPRING9), 520 WSP_DEV(APPLE, WELLSPRING9_JIS, WSP_FLAG_WELLSPRING9), 521}; 522 523#define WSP_FIFO_BUF_SIZE 8 /* bytes */ 524#define WSP_FIFO_QUEUE_MAXLEN 50 /* units */ 525 526enum { 527 WSP_INTR_DT, 528 WSP_N_TRANSFER, 529}; 530 531struct wsp_softc { 532 struct usb_device *sc_usb_device; 533 struct mtx sc_mutex; /* for synchronization */ 534 struct usb_xfer *sc_xfer[WSP_N_TRANSFER]; 535 struct usb_fifo_sc sc_fifo; 536 537 const struct wsp_dev_params *sc_params; /* device configuration */ 538 539 mousehw_t sc_hw; 540 mousemode_t sc_mode; 541 u_int sc_pollrate; 542 mousestatus_t sc_status; 543 u_int sc_state; 544#define WSP_ENABLED 0x01 545 546 struct tp_finger *index[MAX_FINGERS]; /* finger index data */ 547 int16_t pos_x[MAX_FINGERS]; /* position array */ 548 int16_t pos_y[MAX_FINGERS]; /* position array */ 549 u_int sc_touch; /* touch status */ 550#define WSP_UNTOUCH 0x00 551#define WSP_FIRST_TOUCH 0x01 552#define WSP_SECOND_TOUCH 0x02 553#define WSP_TOUCHING 0x04 554 int16_t pre_pos_x; /* previous position array */ 555 int16_t pre_pos_y; /* previous position array */ 556 int dx_sum; /* x axis cumulative movement */ 557 int dy_sum; /* y axis cumulative movement */ 558 int dz_sum; /* z axis cumulative movement */ 559 int dz_count; 560#define WSP_DZ_MAX_COUNT 32 561 int dt_sum; /* T-axis cumulative movement */ 562 int rdx; /* x axis remainder of divide by scale_factor */ 563 int rdy; /* y axis remainder of divide by scale_factor */ 564 int rdz; /* z axis remainder of divide by scale_factor */ 565 int tp_datalen; 566 uint8_t o_ntouch; /* old touch finger status */ 567 uint8_t finger; /* 0 or 1 *, check which finger moving */ 568 uint16_t intr_count; 569#define WSP_TAP_THRESHOLD 3 570#define WSP_TAP_MAX_COUNT 20 571 int distance; /* the distance of 2 fingers */ 572#define MAX_DISTANCE 2500 /* the max allowed distance */ 573 uint8_t ibtn; /* button status in tapping */ 574 uint8_t ntaps; /* finger status in tapping */ 575 uint8_t scr_mode; /* scroll status in movement */ 576#define WSP_SCR_NONE 0 577#define WSP_SCR_VER 1 578#define WSP_SCR_HOR 2 579 uint8_t tp_data[WSP_BUFFER_MAX] __aligned(4); /* trackpad transferred data */ 580}; 581 582/* 583 * function prototypes 584 */ 585static usb_fifo_cmd_t wsp_start_read; 586static usb_fifo_cmd_t wsp_stop_read; 587static usb_fifo_open_t wsp_open; 588static usb_fifo_close_t wsp_close; 589static usb_fifo_ioctl_t wsp_ioctl; 590 591static struct usb_fifo_methods wsp_fifo_methods = { 592 .f_open = &wsp_open, 593 .f_close = &wsp_close, 594 .f_ioctl = &wsp_ioctl, 595 .f_start_read = &wsp_start_read, 596 .f_stop_read = &wsp_stop_read, 597 .basename[0] = WSP_DRIVER_NAME, 598}; 599 600/* device initialization and shutdown */ 601static int wsp_enable(struct wsp_softc *sc); 602static void wsp_disable(struct wsp_softc *sc); 603 604/* updating fifo */ 605static void wsp_reset_buf(struct wsp_softc *sc); 606static void wsp_add_to_queue(struct wsp_softc *, int, int, int, uint32_t); 607 608/* Device methods. */ 609static device_probe_t wsp_probe; 610static device_attach_t wsp_attach; 611static device_detach_t wsp_detach; 612static usb_callback_t wsp_intr_callback; 613 614static const struct usb_config wsp_config[WSP_N_TRANSFER] = { 615 [WSP_INTR_DT] = { 616 .type = UE_INTERRUPT, 617 .endpoint = UE_ADDR_ANY, 618 .direction = UE_DIR_IN, 619 .flags = { 620 .pipe_bof = 0, 621 .short_xfer_ok = 1, 622 }, 623 .bufsize = WSP_BUFFER_MAX, 624 .callback = &wsp_intr_callback, 625 }, 626}; 627 628static usb_error_t 629wsp_set_device_mode(struct wsp_softc *sc, uint8_t on) 630{ 631 const struct wsp_dev_params *params = sc->sc_params; 632 uint8_t mode_bytes[8]; 633 usb_error_t err; 634 635 /* Type 3 does not require a mode switch */ 636 if (params->tp_type == TYPE3) 637 return 0; 638 639 err = usbd_req_get_report(sc->sc_usb_device, NULL, 640 mode_bytes, params->um_size, params->iface_index, 641 params->um_req_val, params->um_req_idx); 642 643 if (err != USB_ERR_NORMAL_COMPLETION) { 644 DPRINTF("Failed to read device mode (%d)\n", err); 645 return (err); 646 } 647 648 /* 649 * XXX Need to wait at least 250ms for hardware to get 650 * ready. The device mode handling appears to be handled 651 * asynchronously and we should not issue these commands too 652 * quickly. 653 */ 654 pause("WHW", hz / 4); 655 656 mode_bytes[params->um_switch_idx] = 657 on ? params->um_switch_on : params->um_switch_off; 658 659 return (usbd_req_set_report(sc->sc_usb_device, NULL, 660 mode_bytes, params->um_size, params->iface_index, 661 params->um_req_val, params->um_req_idx)); 662} 663 664static int 665wsp_enable(struct wsp_softc *sc) 666{ 667 /* reset status */ 668 memset(&sc->sc_status, 0, sizeof(sc->sc_status)); 669 sc->sc_state |= WSP_ENABLED; 670 671 DPRINTFN(WSP_LLEVEL_INFO, "enabled wsp\n"); 672 return (0); 673} 674 675static void 676wsp_disable(struct wsp_softc *sc) 677{ 678 sc->sc_state &= ~WSP_ENABLED; 679 DPRINTFN(WSP_LLEVEL_INFO, "disabled wsp\n"); 680} 681 682static int 683wsp_probe(device_t self) 684{ 685 struct usb_attach_arg *uaa = device_get_ivars(self); 686 struct usb_interface_descriptor *id; 687 struct usb_interface *iface; 688 uint8_t i; 689 690 if (uaa->usb_mode != USB_MODE_HOST) 691 return (ENXIO); 692 693 /* figure out first interface matching */ 694 for (i = 1;; i++) { 695 iface = usbd_get_iface(uaa->device, i); 696 if (iface == NULL || i == 3) 697 return (ENXIO); 698 id = iface->idesc; 699 if ((id == NULL) || 700 (id->bInterfaceClass != UICLASS_HID) || 701 (id->bInterfaceProtocol != 0 && 702 id->bInterfaceProtocol != UIPROTO_MOUSE)) 703 continue; 704 break; 705 } 706 /* check if we are attaching to the first match */ 707 if (uaa->info.bIfaceIndex != i) 708 return (ENXIO); 709 return (usbd_lookup_id_by_uaa(wsp_devs, sizeof(wsp_devs), uaa)); 710} 711 712static int 713wsp_attach(device_t dev) 714{ 715 struct wsp_softc *sc = device_get_softc(dev); 716 struct usb_attach_arg *uaa = device_get_ivars(dev); 717 usb_error_t err; 718 void *d_ptr = NULL; 719 uint16_t d_len; 720 721 DPRINTFN(WSP_LLEVEL_INFO, "sc=%p\n", sc); 722 723 /* Get HID descriptor */ 724 err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr, 725 &d_len, M_TEMP, uaa->info.bIfaceIndex); 726 727 if (err == USB_ERR_NORMAL_COMPLETION) { 728 /* Get HID report descriptor length */ 729 sc->tp_datalen = hid_report_size(d_ptr, d_len, hid_input, NULL); 730 free(d_ptr, M_TEMP); 731 732 if (sc->tp_datalen <= 0 || sc->tp_datalen > WSP_BUFFER_MAX) { 733 DPRINTF("Invalid datalength or too big " 734 "datalength: %d\n", sc->tp_datalen); 735 return (ENXIO); 736 } 737 } else { 738 return (ENXIO); 739 } 740 741 sc->sc_usb_device = uaa->device; 742 743 /* get device specific configuration */ 744 sc->sc_params = wsp_dev_params + USB_GET_DRIVER_INFO(uaa); 745 746 /* 747 * By default the touchpad behaves like a HID device, sending 748 * packets with reportID = 8. Such reports contain only 749 * limited information. They encode movement deltas and button 750 * events, but do not include data from the pressure 751 * sensors. The device input mode can be switched from HID 752 * reports to raw sensor data using vendor-specific USB 753 * control commands: 754 */ 755 756 /* 757 * During re-enumeration of the device we need to force the 758 * device back into HID mode before switching it to RAW 759 * mode. Else the device does not work like expected. 760 */ 761 err = wsp_set_device_mode(sc, 0); 762 if (err != USB_ERR_NORMAL_COMPLETION) { 763 DPRINTF("Failed to set mode to HID MODE (%d)\n", err); 764 return (ENXIO); 765 } 766 767 err = wsp_set_device_mode(sc, 1); 768 if (err != USB_ERR_NORMAL_COMPLETION) { 769 DPRINTF("failed to set mode to RAW MODE (%d)\n", err); 770 return (ENXIO); 771 } 772 773 mtx_init(&sc->sc_mutex, "wspmtx", NULL, MTX_DEF | MTX_RECURSE); 774 775 err = usbd_transfer_setup(uaa->device, 776 &uaa->info.bIfaceIndex, sc->sc_xfer, wsp_config, 777 WSP_N_TRANSFER, sc, &sc->sc_mutex); 778 if (err) { 779 DPRINTF("error=%s\n", usbd_errstr(err)); 780 goto detach; 781 } 782 if (usb_fifo_attach(sc->sc_usb_device, sc, &sc->sc_mutex, 783 &wsp_fifo_methods, &sc->sc_fifo, 784 device_get_unit(dev), -1, uaa->info.bIfaceIndex, 785 UID_ROOT, GID_OPERATOR, 0644)) { 786 goto detach; 787 } 788 device_set_usb_desc(dev); 789 790 sc->sc_hw.buttons = 3; 791 sc->sc_hw.iftype = MOUSE_IF_USB; 792 sc->sc_hw.type = MOUSE_PAD; 793 sc->sc_hw.model = MOUSE_MODEL_GENERIC; 794 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 795 sc->sc_mode.rate = -1; 796 sc->sc_mode.resolution = MOUSE_RES_UNKNOWN; 797 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 798 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 799 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 800 801 sc->sc_touch = WSP_UNTOUCH; 802 sc->scr_mode = WSP_SCR_NONE; 803 804 return (0); 805 806detach: 807 wsp_detach(dev); 808 return (ENOMEM); 809} 810 811static int 812wsp_detach(device_t dev) 813{ 814 struct wsp_softc *sc = device_get_softc(dev); 815 816 (void) wsp_set_device_mode(sc, 0); 817 818 mtx_lock(&sc->sc_mutex); 819 if (sc->sc_state & WSP_ENABLED) 820 wsp_disable(sc); 821 mtx_unlock(&sc->sc_mutex); 822 823 usb_fifo_detach(&sc->sc_fifo); 824 825 usbd_transfer_unsetup(sc->sc_xfer, WSP_N_TRANSFER); 826 827 mtx_destroy(&sc->sc_mutex); 828 829 return (0); 830} 831 832static void 833wsp_intr_callback(struct usb_xfer *xfer, usb_error_t error) 834{ 835 struct wsp_softc *sc = usbd_xfer_softc(xfer); 836 const struct wsp_dev_params *params = sc->sc_params; 837 struct usb_page_cache *pc; 838 struct tp_finger *f; 839 struct tp_header *h; 840 struct wsp_tuning tun = wsp_tuning; 841 int ntouch = 0; /* the finger number in touch */ 842 int ibt = 0; /* button status */ 843 int dx = 0; 844 int dy = 0; 845 int dz = 0; 846 int rdx = 0; 847 int rdy = 0; 848 int rdz = 0; 849 int len; 850 int i; 851 852 wsp_runing_rangecheck(&tun); 853 854 if (sc->dz_count == 0) 855 sc->dz_count = WSP_DZ_MAX_COUNT; 856 857 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 858 859 switch (USB_GET_STATE(xfer)) { 860 case USB_ST_TRANSFERRED: 861 862 /* copy out received data */ 863 pc = usbd_xfer_get_frame(xfer, 0); 864 usbd_copy_out(pc, 0, sc->tp_data, len); 865 866 if ((len < params->tp_offset + params->tp_fsize) || 867 ((len - params->tp_offset) % params->tp_fsize) != 0) { 868 DPRINTFN(WSP_LLEVEL_INFO, "Invalid length: %d, %x, %x\n", 869 len, sc->tp_data[0], sc->tp_data[1]); 870 goto tr_setup; 871 } 872 873 if (len < sc->tp_datalen) { 874 /* make sure we don't process old data */ 875 memset(sc->tp_data + len, 0, sc->tp_datalen - len); 876 } 877 878 h = (struct tp_header *)(sc->tp_data); 879 880 if (params->tp_type >= TYPE2) { 881 ibt = sc->tp_data[params->tp_button]; 882 ntouch = sc->tp_data[params->tp_button - 1]; 883 } 884 /* range check */ 885 if (ntouch < 0) 886 ntouch = 0; 887 else if (ntouch > MAX_FINGERS) 888 ntouch = MAX_FINGERS; 889 890 for (i = 0; i != ntouch; i++) { 891 f = (struct tp_finger *)(sc->tp_data + params->tp_offset + params->tp_delta + i * params->tp_fsize); 892 /* swap endianness, if any */ 893 if (le16toh(0x1234) != 0x1234) { 894 f->origin = le16toh((uint16_t)f->origin); 895 f->abs_x = le16toh((uint16_t)f->abs_x); 896 f->abs_y = le16toh((uint16_t)f->abs_y); 897 f->rel_x = le16toh((uint16_t)f->rel_x); 898 f->rel_y = le16toh((uint16_t)f->rel_y); 899 f->tool_major = le16toh((uint16_t)f->tool_major); 900 f->tool_minor = le16toh((uint16_t)f->tool_minor); 901 f->orientation = le16toh((uint16_t)f->orientation); 902 f->touch_major = le16toh((uint16_t)f->touch_major); 903 f->touch_minor = le16toh((uint16_t)f->touch_minor); 904 f->pressure = le16toh((uint16_t)f->pressure); 905 f->multi = le16toh((uint16_t)f->multi); 906 } 907 DPRINTFN(WSP_LLEVEL_INFO, 908 "[%d]ibt=%d, taps=%d, o=%4d, ax=%5d, ay=%5d, " 909 "rx=%5d, ry=%5d, tlmaj=%4d, tlmin=%4d, ot=%4x, " 910 "tchmaj=%4d, tchmin=%4d, presure=%4d, m=%4x\n", 911 i, ibt, ntouch, f->origin, f->abs_x, f->abs_y, 912 f->rel_x, f->rel_y, f->tool_major, f->tool_minor, f->orientation, 913 f->touch_major, f->touch_minor, f->pressure, f->multi); 914 sc->pos_x[i] = f->abs_x; 915 sc->pos_y[i] = -f->abs_y; 916 sc->index[i] = f; 917 } 918 919 sc->sc_status.flags &= ~MOUSE_POSCHANGED; 920 sc->sc_status.flags &= ~MOUSE_STDBUTTONSCHANGED; 921 sc->sc_status.obutton = sc->sc_status.button; 922 sc->sc_status.button = 0; 923 924 if (ibt != 0) { 925 if ((params->caps & HAS_INTEGRATED_BUTTON) && ntouch == 2) 926 sc->sc_status.button |= MOUSE_BUTTON3DOWN; 927 else if ((params->caps & HAS_INTEGRATED_BUTTON) && ntouch == 3) 928 sc->sc_status.button |= MOUSE_BUTTON2DOWN; 929 else 930 sc->sc_status.button |= MOUSE_BUTTON1DOWN; 931 sc->ibtn = 1; 932 } 933 sc->intr_count++; 934 935 if (sc->ntaps < ntouch) { 936 switch (ntouch) { 937 case 1: 938 if (sc->index[0]->touch_major > tun.pressure_tap_threshold && 939 sc->index[0]->tool_major <= 1200) 940 sc->ntaps = 1; 941 break; 942 case 2: 943 if (sc->index[0]->touch_major > tun.pressure_tap_threshold-30 && 944 sc->index[1]->touch_major > tun.pressure_tap_threshold-30) 945 sc->ntaps = 2; 946 break; 947 case 3: 948 if (sc->index[0]->touch_major > tun.pressure_tap_threshold-40 && 949 sc->index[1]->touch_major > tun.pressure_tap_threshold-40 && 950 sc->index[2]->touch_major > tun.pressure_tap_threshold-40) 951 sc->ntaps = 3; 952 break; 953 default: 954 break; 955 } 956 } 957 if (ntouch == 2) { 958 sc->distance = max(sc->distance, max( 959 abs(sc->pos_x[0] - sc->pos_x[1]), 960 abs(sc->pos_y[0] - sc->pos_y[1]))); 961 } 962 if (sc->index[0]->touch_major < tun.pressure_untouch_threshold && 963 sc->sc_status.button == 0) { 964 sc->sc_touch = WSP_UNTOUCH; 965 if (sc->intr_count < WSP_TAP_MAX_COUNT && 966 sc->intr_count > WSP_TAP_THRESHOLD && 967 sc->ntaps && sc->ibtn == 0) { 968 /* 969 * Add a pair of events (button-down and 970 * button-up). 971 */ 972 switch (sc->ntaps) { 973 case 1: 974 if (!(params->caps & HAS_INTEGRATED_BUTTON)) { 975 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON1DOWN); 976 DPRINTFN(WSP_LLEVEL_INFO, "LEFT CLICK!\n"); 977 } 978 break; 979 case 2: 980 DPRINTFN(WSP_LLEVEL_INFO, "sum_x=%5d, sum_y=%5d\n", 981 sc->dx_sum, sc->dy_sum); 982 if (sc->distance < MAX_DISTANCE && abs(sc->dx_sum) < 5 && 983 abs(sc->dy_sum) < 5) { 984 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON3DOWN); 985 DPRINTFN(WSP_LLEVEL_INFO, "RIGHT CLICK!\n"); 986 } 987 break; 988 case 3: 989 wsp_add_to_queue(sc, 0, 0, 0, MOUSE_BUTTON2DOWN); 990 break; 991 default: 992 /* we don't handle taps of more than three fingers */ 993 break; 994 } 995 wsp_add_to_queue(sc, 0, 0, 0, 0); /* button release */ 996 } 997 if ((sc->dt_sum / tun.scr_hor_threshold) != 0 && 998 sc->ntaps == 2 && sc->scr_mode == WSP_SCR_HOR) { 999 1000 /* 1001 * translate T-axis into button presses 1002 * until further 1003 */ 1004 if (sc->dt_sum > 0) 1005 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 3); 1006 else if (sc->dt_sum < 0) 1007 wsp_add_to_queue(sc, 0, 0, 0, 1UL << 4); 1008 } 1009 sc->dz_count = WSP_DZ_MAX_COUNT; 1010 sc->dz_sum = 0; 1011 sc->intr_count = 0; 1012 sc->ibtn = 0; 1013 sc->ntaps = 0; 1014 sc->finger = 0; 1015 sc->distance = 0; 1016 sc->dt_sum = 0; 1017 sc->dx_sum = 0; 1018 sc->dy_sum = 0; 1019 sc->rdx = 0; 1020 sc->rdy = 0; 1021 sc->rdz = 0; 1022 sc->scr_mode = WSP_SCR_NONE; 1023 } else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold && 1024 sc->sc_touch == WSP_UNTOUCH) { /* ignore first touch */ 1025 sc->sc_touch = WSP_FIRST_TOUCH; 1026 } else if (sc->index[0]->touch_major >= tun.pressure_touch_threshold && 1027 sc->sc_touch == WSP_FIRST_TOUCH) { /* ignore second touch */ 1028 sc->sc_touch = WSP_SECOND_TOUCH; 1029 DPRINTFN(WSP_LLEVEL_INFO, "Fist pre_x=%5d, pre_y=%5d\n", 1030 sc->pre_pos_x, sc->pre_pos_y); 1031 } else { 1032 if (sc->sc_touch == WSP_SECOND_TOUCH) 1033 sc->sc_touch = WSP_TOUCHING; 1034 1035 if (ntouch != 0 && 1036 sc->index[0]->touch_major >= tun.pressure_touch_threshold) { 1037 dx = sc->pos_x[0] - sc->pre_pos_x; 1038 dy = sc->pos_y[0] - sc->pre_pos_y; 1039 1040 /* Ignore movement during button is releasing */ 1041 if (sc->ibtn != 0 && sc->sc_status.button == 0) 1042 dx = dy = 0; 1043 1044 /* Ignore movement if ntouch changed */ 1045 if (sc->o_ntouch != ntouch) 1046 dx = dy = 0; 1047 1048 /* Ignore unexpeted movment when typing */ 1049 if (ntouch == 1 && sc->index[0]->tool_major > 1200) 1050 dx = dy = 0; 1051 1052 if (sc->ibtn != 0 && ntouch == 1 && 1053 sc->intr_count < WSP_TAP_MAX_COUNT && 1054 abs(sc->dx_sum) < 1 && abs(sc->dy_sum) < 1 ) 1055 dx = dy = 0; 1056 1057 if (ntouch == 2 && sc->sc_status.button != 0) { 1058 dx = sc->pos_x[sc->finger] - sc->pre_pos_x; 1059 dy = sc->pos_y[sc->finger] - sc->pre_pos_y; 1060 1061 /* 1062 * Ignore movement of switch finger or 1063 * movement from ibt=0 to ibt=1 1064 */ 1065 if (sc->index[0]->origin == 0 || sc->index[1]->origin == 0 || 1066 sc->sc_status.obutton != sc->sc_status.button) { 1067 dx = dy = 0; 1068 sc->finger = 0; 1069 } 1070 if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) < 1071 (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) && 1072 sc->finger == 0) { 1073 sc->sc_touch = WSP_SECOND_TOUCH; 1074 dx = dy = 0; 1075 sc->finger = 1; 1076 } 1077 if ((abs(sc->index[0]->rel_x) + abs(sc->index[0]->rel_y)) >= 1078 (abs(sc->index[1]->rel_x) + abs(sc->index[1]->rel_y)) && 1079 sc->finger == 1) { 1080 sc->sc_touch = WSP_SECOND_TOUCH; 1081 dx = dy = 0; 1082 sc->finger = 0; 1083 } 1084 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, mov=%5d\n", 1085 dx, dy, sc->finger); 1086 } 1087 if (sc->dz_count--) { 1088 rdz = (dy + sc->rdz) % tun.scale_factor; 1089 sc->dz_sum -= (dy + sc->rdz) / tun.scale_factor; 1090 sc->rdz = rdz; 1091 } 1092 if ((sc->dz_sum / tun.z_factor) != 0) 1093 sc->dz_count = 0; 1094 } 1095 rdx = (dx + sc->rdx) % tun.scale_factor; 1096 dx = (dx + sc->rdx) / tun.scale_factor; 1097 sc->rdx = rdx; 1098 1099 rdy = (dy + sc->rdy) % tun.scale_factor; 1100 dy = (dy + sc->rdy) / tun.scale_factor; 1101 sc->rdy = rdy; 1102 1103 sc->dx_sum += dx; 1104 sc->dy_sum += dy; 1105 1106 if (ntouch == 2 && sc->sc_status.button == 0) { 1107 if (sc->scr_mode == WSP_SCR_NONE && 1108 abs(sc->dx_sum) + abs(sc->dy_sum) > tun.scr_hor_threshold) 1109 sc->scr_mode = abs(sc->dx_sum) > 1110 abs(sc->dy_sum) * 2 ? WSP_SCR_HOR : WSP_SCR_VER; 1111 DPRINTFN(WSP_LLEVEL_INFO, "scr_mode=%5d, count=%d, dx_sum=%d, dy_sum=%d\n", 1112 sc->scr_mode, sc->intr_count, sc->dx_sum, sc->dy_sum); 1113 if (sc->scr_mode == WSP_SCR_HOR) 1114 sc->dt_sum += dx; 1115 else 1116 sc->dt_sum = 0; 1117 1118 dx = dy = 0; 1119 if (sc->dz_count == 0) 1120 dz = sc->dz_sum / tun.z_factor; 1121 if (sc->scr_mode == WSP_SCR_HOR || 1122 abs(sc->pos_x[0] - sc->pos_x[1]) > MAX_DISTANCE || 1123 abs(sc->pos_y[0] - sc->pos_y[1]) > MAX_DISTANCE) 1124 dz = 0; 1125 } 1126 if (ntouch == 3) 1127 dx = dy = dz = 0; 1128 if (sc->intr_count < WSP_TAP_MAX_COUNT && 1129 abs(dx) < 3 && abs(dy) < 3 && abs(dz) < 3) 1130 dx = dy = dz = 0; 1131 else 1132 sc->intr_count = WSP_TAP_MAX_COUNT; 1133 if (dx || dy || dz) 1134 sc->sc_status.flags |= MOUSE_POSCHANGED; 1135 DPRINTFN(WSP_LLEVEL_INFO, "dx=%5d, dy=%5d, dz=%5d, sc_touch=%x, btn=%x\n", 1136 dx, dy, dz, sc->sc_touch, sc->sc_status.button); 1137 sc->sc_status.dx += dx; 1138 sc->sc_status.dy += dy; 1139 sc->sc_status.dz += dz; 1140 1141 wsp_add_to_queue(sc, dx, -dy, dz, sc->sc_status.button); 1142 if (sc->dz_count == 0) { 1143 sc->dz_sum = 0; 1144 sc->rdz = 0; 1145 } 1146 1147 } 1148 sc->pre_pos_x = sc->pos_x[0]; 1149 sc->pre_pos_y = sc->pos_y[0]; 1150 1151 if (ntouch == 2 && sc->sc_status.button != 0) { 1152 sc->pre_pos_x = sc->pos_x[sc->finger]; 1153 sc->pre_pos_y = sc->pos_y[sc->finger]; 1154 } 1155 sc->o_ntouch = ntouch; 1156 1157 case USB_ST_SETUP: 1158tr_setup: 1159 /* check if we can put more data into the FIFO */ 1160 if (usb_fifo_put_bytes_max( 1161 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 1162 usbd_xfer_set_frame_len(xfer, 0, 1163 sc->tp_datalen); 1164 usbd_transfer_submit(xfer); 1165 } 1166 break; 1167 1168 default: /* Error */ 1169 if (error != USB_ERR_CANCELLED) { 1170 /* try clear stall first */ 1171 usbd_xfer_set_stall(xfer); 1172 goto tr_setup; 1173 } 1174 break; 1175 } 1176} 1177 1178static void 1179wsp_add_to_queue(struct wsp_softc *sc, int dx, int dy, int dz, 1180 uint32_t buttons_in) 1181{ 1182 uint32_t buttons_out; 1183 uint8_t buf[8]; 1184 1185 dx = imin(dx, 254); 1186 dx = imax(dx, -256); 1187 dy = imin(dy, 254); 1188 dy = imax(dy, -256); 1189 dz = imin(dz, 126); 1190 dz = imax(dz, -128); 1191 1192 buttons_out = MOUSE_MSC_BUTTONS; 1193 if (buttons_in & MOUSE_BUTTON1DOWN) 1194 buttons_out &= ~MOUSE_MSC_BUTTON1UP; 1195 else if (buttons_in & MOUSE_BUTTON2DOWN) 1196 buttons_out &= ~MOUSE_MSC_BUTTON2UP; 1197 else if (buttons_in & MOUSE_BUTTON3DOWN) 1198 buttons_out &= ~MOUSE_MSC_BUTTON3UP; 1199 1200 /* Encode the mouse data in standard format; refer to mouse(4) */ 1201 buf[0] = sc->sc_mode.syncmask[1]; 1202 buf[0] |= buttons_out; 1203 buf[1] = dx >> 1; 1204 buf[2] = dy >> 1; 1205 buf[3] = dx - (dx >> 1); 1206 buf[4] = dy - (dy >> 1); 1207 /* Encode extra bytes for level 1 */ 1208 if (sc->sc_mode.level == 1) { 1209 buf[5] = dz >> 1; /* dz / 2 */ 1210 buf[6] = dz - (dz >> 1);/* dz - (dz / 2) */ 1211 buf[7] = (((~buttons_in) >> 3) & MOUSE_SYS_EXTBUTTONS); 1212 } 1213 usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf, 1214 sc->sc_mode.packetsize, 1); 1215} 1216 1217static void 1218wsp_reset_buf(struct wsp_softc *sc) 1219{ 1220 /* reset read queue */ 1221 usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]); 1222} 1223 1224static void 1225wsp_start_read(struct usb_fifo *fifo) 1226{ 1227 struct wsp_softc *sc = usb_fifo_softc(fifo); 1228 int rate; 1229 1230 /* Check if we should override the default polling interval */ 1231 rate = sc->sc_pollrate; 1232 /* Range check rate */ 1233 if (rate > 1000) 1234 rate = 1000; 1235 /* Check for set rate */ 1236 if ((rate > 0) && (sc->sc_xfer[WSP_INTR_DT] != NULL)) { 1237 /* Stop current transfer, if any */ 1238 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]); 1239 /* Set new interval */ 1240 usbd_xfer_set_interval(sc->sc_xfer[WSP_INTR_DT], 1000 / rate); 1241 /* Only set pollrate once */ 1242 sc->sc_pollrate = 0; 1243 } 1244 usbd_transfer_start(sc->sc_xfer[WSP_INTR_DT]); 1245} 1246 1247static void 1248wsp_stop_read(struct usb_fifo *fifo) 1249{ 1250 struct wsp_softc *sc = usb_fifo_softc(fifo); 1251 1252 usbd_transfer_stop(sc->sc_xfer[WSP_INTR_DT]); 1253} 1254 1255 1256static int 1257wsp_open(struct usb_fifo *fifo, int fflags) 1258{ 1259 DPRINTFN(WSP_LLEVEL_INFO, "\n"); 1260 1261 if (fflags & FREAD) { 1262 struct wsp_softc *sc = usb_fifo_softc(fifo); 1263 int rc; 1264 1265 if (sc->sc_state & WSP_ENABLED) 1266 return (EBUSY); 1267 1268 if (usb_fifo_alloc_buffer(fifo, 1269 WSP_FIFO_BUF_SIZE, WSP_FIFO_QUEUE_MAXLEN)) { 1270 return (ENOMEM); 1271 } 1272 rc = wsp_enable(sc); 1273 if (rc != 0) { 1274 usb_fifo_free_buffer(fifo); 1275 return (rc); 1276 } 1277 } 1278 return (0); 1279} 1280 1281static void 1282wsp_close(struct usb_fifo *fifo, int fflags) 1283{ 1284 if (fflags & FREAD) { 1285 struct wsp_softc *sc = usb_fifo_softc(fifo); 1286 1287 wsp_disable(sc); 1288 usb_fifo_free_buffer(fifo); 1289 } 1290} 1291 1292int 1293wsp_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags) 1294{ 1295 struct wsp_softc *sc = usb_fifo_softc(fifo); 1296 mousemode_t mode; 1297 int error = 0; 1298 1299 mtx_lock(&sc->sc_mutex); 1300 1301 switch (cmd) { 1302 case MOUSE_GETHWINFO: 1303 *(mousehw_t *)addr = sc->sc_hw; 1304 break; 1305 case MOUSE_GETMODE: 1306 *(mousemode_t *)addr = sc->sc_mode; 1307 break; 1308 case MOUSE_SETMODE: 1309 mode = *(mousemode_t *)addr; 1310 1311 if (mode.level == -1) 1312 /* Don't change the current setting */ 1313 ; 1314 else if ((mode.level < 0) || (mode.level > 1)) { 1315 error = EINVAL; 1316 goto done; 1317 } 1318 sc->sc_mode.level = mode.level; 1319 sc->sc_pollrate = mode.rate; 1320 sc->sc_hw.buttons = 3; 1321 1322 if (sc->sc_mode.level == 0) { 1323 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 1324 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 1325 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 1326 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 1327 } else if (sc->sc_mode.level == 1) { 1328 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 1329 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 1330 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 1331 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 1332 } 1333 wsp_reset_buf(sc); 1334 break; 1335 case MOUSE_GETLEVEL: 1336 *(int *)addr = sc->sc_mode.level; 1337 break; 1338 case MOUSE_SETLEVEL: 1339 if (*(int *)addr < 0 || *(int *)addr > 1) { 1340 error = EINVAL; 1341 goto done; 1342 } 1343 sc->sc_mode.level = *(int *)addr; 1344 sc->sc_hw.buttons = 3; 1345 1346 if (sc->sc_mode.level == 0) { 1347 sc->sc_mode.protocol = MOUSE_PROTO_MSC; 1348 sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE; 1349 sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK; 1350 sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC; 1351 } else if (sc->sc_mode.level == 1) { 1352 sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE; 1353 sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE; 1354 sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK; 1355 sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC; 1356 } 1357 wsp_reset_buf(sc); 1358 break; 1359 case MOUSE_GETSTATUS:{ 1360 mousestatus_t *status = (mousestatus_t *)addr; 1361 1362 *status = sc->sc_status; 1363 sc->sc_status.obutton = sc->sc_status.button; 1364 sc->sc_status.button = 0; 1365 sc->sc_status.dx = 0; 1366 sc->sc_status.dy = 0; 1367 sc->sc_status.dz = 0; 1368 1369 if (status->dx || status->dy || status->dz) 1370 status->flags |= MOUSE_POSCHANGED; 1371 if (status->button != status->obutton) 1372 status->flags |= MOUSE_BUTTONSCHANGED; 1373 break; 1374 } 1375 default: 1376 error = ENOTTY; 1377 } 1378 1379done: 1380 mtx_unlock(&sc->sc_mutex); 1381 return (error); 1382} 1383 1384static device_method_t wsp_methods[] = { 1385 /* Device interface */ 1386 DEVMETHOD(device_probe, wsp_probe), 1387 DEVMETHOD(device_attach, wsp_attach), 1388 DEVMETHOD(device_detach, wsp_detach), 1389 DEVMETHOD_END 1390}; 1391 1392static driver_t wsp_driver = { 1393 .name = WSP_DRIVER_NAME, 1394 .methods = wsp_methods, 1395 .size = sizeof(struct wsp_softc) 1396}; 1397 1398static devclass_t wsp_devclass; 1399 1400DRIVER_MODULE(wsp, uhub, wsp_driver, wsp_devclass, NULL, 0); 1401MODULE_DEPEND(wsp, usb, 1, 1, 1); 1402MODULE_VERSION(wsp, 1); 1403