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