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