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