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