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