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