1 /*- 2 * Copyright (c) 2014-2017 Vladimir Kondratyev <wulf@FreeBSD.org> 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 /* 31 * MS Windows 7/8/10 compatible USB HID Multi-touch Device driver. 32 * https://msdn.microsoft.com/en-us/library/windows/hardware/jj151569(v=vs.85).aspx 33 * https://www.kernel.org/doc/Documentation/input/multi-touch-protocol.txt 34 */ 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/conf.h> 39 #include <sys/kernel.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/mutex.h> 44 #include <sys/stddef.h> 45 #include <sys/sysctl.h> 46 #include <sys/systm.h> 47 48 #include "usbdevs.h" 49 #include <dev/usb/usb.h> 50 #include <dev/usb/usbdi.h> 51 #include <dev/usb/usbdi_util.h> 52 #include <dev/usb/usbhid.h> 53 54 #include <dev/usb/quirk/usb_quirk.h> 55 56 #include <dev/evdev/evdev.h> 57 #include <dev/evdev/input.h> 58 59 #define USB_DEBUG_VAR wmt_debug 60 #include <dev/usb/usb_debug.h> 61 62 #ifdef USB_DEBUG 63 static int wmt_debug = 0; 64 65 static SYSCTL_NODE(_hw_usb, OID_AUTO, wmt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 66 "USB MSWindows 7/8/10 compatible Multi-touch Device"); 67 SYSCTL_INT(_hw_usb_wmt, OID_AUTO, debug, CTLFLAG_RWTUN, 68 &wmt_debug, 1, "Debug level"); 69 #endif 70 71 #define WMT_BSIZE 1024 /* bytes, buffer size */ 72 73 enum { 74 WMT_INTR_DT, 75 WMT_N_TRANSFER, 76 }; 77 78 enum { 79 WMT_TIP_SWITCH, 80 #define WMT_SLOT WMT_TIP_SWITCH 81 WMT_WIDTH, 82 #define WMT_MAJOR WMT_WIDTH 83 WMT_HEIGHT, 84 #define WMT_MINOR WMT_HEIGHT 85 WMT_ORIENTATION, 86 WMT_X, 87 WMT_Y, 88 WMT_CONTACTID, 89 WMT_PRESSURE, 90 WMT_IN_RANGE, 91 WMT_CONFIDENCE, 92 WMT_TOOL_X, 93 WMT_TOOL_Y, 94 WMT_N_USAGES, 95 }; 96 97 #define WMT_NO_CODE (ABS_MAX + 10) 98 #define WMT_NO_USAGE -1 99 100 struct wmt_hid_map_item { 101 char name[5]; 102 int32_t usage; /* HID usage */ 103 uint32_t code; /* Evdev event code */ 104 bool required; /* Required for MT Digitizers */ 105 }; 106 107 static const struct wmt_hid_map_item wmt_hid_map[WMT_N_USAGES] = { 108 109 [WMT_TIP_SWITCH] = { /* WMT_SLOT */ 110 .name = "TIP", 111 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH), 112 .code = ABS_MT_SLOT, 113 .required = true, 114 }, 115 [WMT_WIDTH] = { /* WMT_MAJOR */ 116 .name = "WDTH", 117 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_WIDTH), 118 .code = ABS_MT_TOUCH_MAJOR, 119 .required = false, 120 }, 121 [WMT_HEIGHT] = { /* WMT_MINOR */ 122 .name = "HGHT", 123 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_HEIGHT), 124 .code = ABS_MT_TOUCH_MINOR, 125 .required = false, 126 }, 127 [WMT_ORIENTATION] = { 128 .name = "ORIE", 129 .usage = WMT_NO_USAGE, 130 .code = ABS_MT_ORIENTATION, 131 .required = false, 132 }, 133 [WMT_X] = { 134 .name = "X", 135 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 136 .code = ABS_MT_POSITION_X, 137 .required = true, 138 }, 139 [WMT_Y] = { 140 .name = "Y", 141 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 142 .code = ABS_MT_POSITION_Y, 143 .required = true, 144 }, 145 [WMT_CONTACTID] = { 146 .name = "C_ID", 147 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID), 148 .code = ABS_MT_TRACKING_ID, 149 .required = true, 150 }, 151 [WMT_PRESSURE] = { 152 .name = "PRES", 153 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_PRESSURE), 154 .code = ABS_MT_PRESSURE, 155 .required = false, 156 }, 157 [WMT_IN_RANGE] = { 158 .name = "RANG", 159 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE), 160 .code = ABS_MT_DISTANCE, 161 .required = false, 162 }, 163 [WMT_CONFIDENCE] = { 164 .name = "CONF", 165 .usage = HID_USAGE2(HUP_DIGITIZERS, HUD_CONFIDENCE), 166 .code = WMT_NO_CODE, 167 .required = false, 168 }, 169 [WMT_TOOL_X] = { /* Shares HID usage with WMT_X */ 170 .name = "TL_X", 171 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X), 172 .code = ABS_MT_TOOL_X, 173 .required = false, 174 }, 175 [WMT_TOOL_Y] = { /* Shares HID usage with WMT_Y */ 176 .name = "TL_Y", 177 .usage = HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y), 178 .code = ABS_MT_TOOL_Y, 179 .required = false, 180 }, 181 }; 182 183 struct wmt_absinfo { 184 int32_t min; 185 int32_t max; 186 int32_t res; 187 }; 188 189 struct wmt_softc 190 { 191 device_t dev; 192 struct mtx mtx; 193 struct wmt_absinfo ai[WMT_N_USAGES]; 194 struct hid_location locs[MAX_MT_SLOTS][WMT_N_USAGES]; 195 struct hid_location nconts_loc; 196 197 struct usb_xfer *xfer[WMT_N_TRANSFER]; 198 struct evdev_dev *evdev; 199 200 uint32_t slot_data[WMT_N_USAGES]; 201 uint32_t caps; 202 uint32_t isize; 203 uint32_t nconts_max; 204 uint32_t report_len; 205 uint8_t report_id; 206 207 struct hid_location cont_max_loc; 208 uint32_t cont_max_rlen; 209 uint8_t cont_max_rid; 210 uint32_t thqa_cert_rlen; 211 uint8_t thqa_cert_rid; 212 213 uint8_t buf[WMT_BSIZE] __aligned(4); 214 }; 215 216 #define USAGE_SUPPORTED(caps, usage) ((caps) & (1 << (usage))) 217 #define WMT_FOREACH_USAGE(caps, usage) \ 218 for ((usage) = 0; (usage) < WMT_N_USAGES; ++(usage)) \ 219 if (USAGE_SUPPORTED((caps), (usage))) 220 221 static bool wmt_hid_parse(struct wmt_softc *, const void *, uint16_t); 222 static void wmt_cont_max_parse(struct wmt_softc *, const void *, uint16_t); 223 224 static usb_callback_t wmt_intr_callback; 225 226 static device_probe_t wmt_probe; 227 static device_attach_t wmt_attach; 228 static device_detach_t wmt_detach; 229 230 #if __FreeBSD_version >= 1200077 231 static evdev_open_t wmt_ev_open; 232 static evdev_close_t wmt_ev_close; 233 #else 234 static evdev_open_t wmt_ev_open_11; 235 static evdev_close_t wmt_ev_close_11; 236 #endif 237 238 static const struct evdev_methods wmt_evdev_methods = { 239 #if __FreeBSD_version >= 1200077 240 .ev_open = &wmt_ev_open, 241 .ev_close = &wmt_ev_close, 242 #else 243 .ev_open = &wmt_ev_open_11, 244 .ev_close = &wmt_ev_close_11, 245 #endif 246 }; 247 248 static const struct usb_config wmt_config[WMT_N_TRANSFER] = { 249 250 [WMT_INTR_DT] = { 251 .type = UE_INTERRUPT, 252 .endpoint = UE_ADDR_ANY, 253 .direction = UE_DIR_IN, 254 .flags = { .pipe_bof = 1, .short_xfer_ok = 1 }, 255 .bufsize = WMT_BSIZE, 256 .callback = &wmt_intr_callback, 257 }, 258 }; 259 260 static int 261 wmt_probe(device_t dev) 262 { 263 struct usb_attach_arg *uaa = device_get_ivars(dev); 264 void *d_ptr; 265 uint16_t d_len; 266 int err; 267 268 if (uaa->usb_mode != USB_MODE_HOST) 269 return (ENXIO); 270 271 if (uaa->info.bInterfaceClass != UICLASS_HID) 272 return (ENXIO); 273 274 if (usb_test_quirk(uaa, UQ_WMT_IGNORE)) 275 return (ENXIO); 276 277 err = usbd_req_get_hid_desc(uaa->device, NULL, 278 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 279 if (err) 280 return (ENXIO); 281 282 if (wmt_hid_parse(NULL, d_ptr, d_len)) 283 err = BUS_PROBE_DEFAULT; 284 else 285 err = ENXIO; 286 287 free(d_ptr, M_TEMP); 288 return (err); 289 } 290 291 static int 292 wmt_attach(device_t dev) 293 { 294 struct usb_attach_arg *uaa = device_get_ivars(dev); 295 struct wmt_softc *sc = device_get_softc(dev); 296 void *d_ptr; 297 uint16_t d_len; 298 size_t i; 299 int err; 300 bool hid_ok; 301 302 device_set_usb_desc(dev); 303 sc->dev = dev; 304 305 /* Get HID descriptor */ 306 err = usbd_req_get_hid_desc(uaa->device, NULL, 307 &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex); 308 if (err) { 309 DPRINTF("usbd_req_get_hid_desc error=%s\n", usbd_errstr(err)); 310 return (ENXIO); 311 } 312 313 hid_ok = wmt_hid_parse(sc, d_ptr, d_len); 314 free(d_ptr, M_TEMP); 315 if (!hid_ok) { 316 DPRINTF("multi-touch HID descriptor not found\n"); 317 return (ENXIO); 318 } 319 320 /* Check HID report length */ 321 if (sc->isize <= 0 || sc->isize > WMT_BSIZE) { 322 DPRINTF("Input size invalid or too large: %d\n", sc->isize); 323 return (ENXIO); 324 } 325 326 /* Fetch and parse "Contact count maximum" feature report */ 327 if (sc->cont_max_rlen > 0 && sc->cont_max_rlen <= WMT_BSIZE) { 328 err = usbd_req_get_report(uaa->device, NULL, sc->buf, 329 sc->cont_max_rlen, uaa->info.bIfaceIndex, 330 UHID_FEATURE_REPORT, sc->cont_max_rid); 331 if (err == USB_ERR_NORMAL_COMPLETION) 332 wmt_cont_max_parse(sc, sc->buf, sc->cont_max_rlen); 333 else 334 DPRINTF("usbd_req_get_report error=(%s)\n", 335 usbd_errstr(err)); 336 } else 337 DPRINTF("Feature report %hhu size invalid or too large: %u\n", 338 sc->cont_max_rid, sc->cont_max_rlen); 339 340 /* Fetch THQA certificate to enable some devices like WaveShare */ 341 if (sc->thqa_cert_rlen > 0 && sc->thqa_cert_rlen <= WMT_BSIZE && 342 sc->thqa_cert_rid != sc->cont_max_rid) 343 (void)usbd_req_get_report(uaa->device, NULL, sc->buf, 344 sc->thqa_cert_rlen, uaa->info.bIfaceIndex, 345 UHID_FEATURE_REPORT, sc->thqa_cert_rid); 346 347 mtx_init(&sc->mtx, "wmt lock", NULL, MTX_DEF); 348 349 err = usbd_transfer_setup(uaa->device, &uaa->info.bIfaceIndex, 350 sc->xfer, wmt_config, WMT_N_TRANSFER, sc, &sc->mtx); 351 if (err != USB_ERR_NORMAL_COMPLETION) { 352 DPRINTF("usbd_transfer_setup error=%s\n", usbd_errstr(err)); 353 goto detach; 354 } 355 356 sc->evdev = evdev_alloc(); 357 evdev_set_name(sc->evdev, device_get_desc(dev)); 358 evdev_set_phys(sc->evdev, device_get_nameunit(dev)); 359 evdev_set_id(sc->evdev, BUS_USB, uaa->info.idVendor, 360 uaa->info.idProduct, 0); 361 evdev_set_serial(sc->evdev, usb_get_serial(uaa->device)); 362 evdev_set_methods(sc->evdev, sc, &wmt_evdev_methods); 363 evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT); 364 evdev_support_prop(sc->evdev, INPUT_PROP_DIRECT); 365 evdev_support_event(sc->evdev, EV_SYN); 366 evdev_support_event(sc->evdev, EV_ABS); 367 WMT_FOREACH_USAGE(sc->caps, i) { 368 if (wmt_hid_map[i].code != WMT_NO_CODE) 369 evdev_support_abs(sc->evdev, wmt_hid_map[i].code, 0, 370 sc->ai[i].min, sc->ai[i].max, 0, 0, sc->ai[i].res); 371 } 372 373 err = evdev_register_mtx(sc->evdev, &sc->mtx); 374 if (err) 375 goto detach; 376 377 return (0); 378 379 detach: 380 wmt_detach(dev); 381 return (ENXIO); 382 } 383 384 static int 385 wmt_detach(device_t dev) 386 { 387 struct wmt_softc *sc = device_get_softc(dev); 388 389 evdev_free(sc->evdev); 390 usbd_transfer_unsetup(sc->xfer, WMT_N_TRANSFER); 391 mtx_destroy(&sc->mtx); 392 return (0); 393 } 394 395 static void 396 wmt_process_report(struct wmt_softc *sc, uint8_t *buf, int len) 397 { 398 size_t usage; 399 uint32_t *slot_data = sc->slot_data; 400 uint32_t cont; 401 uint32_t nconts; 402 uint32_t width; 403 uint32_t height; 404 int32_t slot; 405 406 nconts = hid_get_data_unsigned(buf, len, &sc->nconts_loc); 407 408 #ifdef USB_DEBUG 409 DPRINTFN(6, "nconts = %u ", (unsigned)nconts); 410 if (wmt_debug >= 6) { 411 WMT_FOREACH_USAGE(sc->caps, usage) { 412 if (wmt_hid_map[usage].usage != WMT_NO_USAGE) 413 printf(" %-4s", wmt_hid_map[usage].name); 414 } 415 printf("\n"); 416 } 417 #endif 418 419 if (nconts > sc->nconts_max) { 420 DPRINTF("Contact count overflow %u\n", (unsigned)nconts); 421 nconts = sc->nconts_max; 422 } 423 424 /* Use protocol Type B for reporting events */ 425 for (cont = 0; cont < nconts; cont++) { 426 427 bzero(slot_data, sizeof(sc->slot_data)); 428 WMT_FOREACH_USAGE(sc->caps, usage) { 429 if (sc->locs[cont][usage].size > 0) 430 slot_data[usage] = hid_get_data_unsigned( 431 buf, len, &sc->locs[cont][usage]); 432 } 433 434 slot = evdev_get_mt_slot_by_tracking_id(sc->evdev, 435 slot_data[WMT_CONTACTID]); 436 437 #ifdef USB_DEBUG 438 DPRINTFN(6, "cont%01x: data = ", cont); 439 if (wmt_debug >= 6) { 440 WMT_FOREACH_USAGE(sc->caps, usage) { 441 if (wmt_hid_map[usage].usage != WMT_NO_USAGE) 442 printf("%04x ", slot_data[usage]); 443 } 444 printf("slot = %d\n", (int)slot); 445 } 446 #endif 447 448 if (slot == -1) { 449 DPRINTF("Slot overflow for contact_id %u\n", 450 (unsigned)slot_data[WMT_CONTACTID]); 451 continue; 452 } 453 454 if (slot_data[WMT_TIP_SWITCH] != 0 && 455 !(USAGE_SUPPORTED(sc->caps, WMT_CONFIDENCE) && 456 slot_data[WMT_CONFIDENCE] == 0)) { 457 /* This finger is in proximity of the sensor */ 458 slot_data[WMT_SLOT] = slot; 459 slot_data[WMT_IN_RANGE] = !slot_data[WMT_IN_RANGE]; 460 /* Divided by two to match visual scale of touch */ 461 width = slot_data[WMT_WIDTH] >> 1; 462 height = slot_data[WMT_HEIGHT] >> 1; 463 slot_data[WMT_ORIENTATION] = width > height; 464 slot_data[WMT_MAJOR] = MAX(width, height); 465 slot_data[WMT_MINOR] = MIN(width, height); 466 467 WMT_FOREACH_USAGE(sc->caps, usage) 468 if (wmt_hid_map[usage].code != WMT_NO_CODE) 469 evdev_push_abs(sc->evdev, 470 wmt_hid_map[usage].code, 471 slot_data[usage]); 472 } else { 473 evdev_push_abs(sc->evdev, ABS_MT_SLOT, slot); 474 evdev_push_abs(sc->evdev, ABS_MT_TRACKING_ID, -1); 475 } 476 } 477 evdev_sync(sc->evdev); 478 } 479 480 static void 481 wmt_intr_callback(struct usb_xfer *xfer, usb_error_t error) 482 { 483 struct wmt_softc *sc = usbd_xfer_softc(xfer); 484 struct usb_page_cache *pc; 485 uint8_t *buf = sc->buf; 486 int len; 487 488 usbd_xfer_status(xfer, &len, NULL, NULL, NULL); 489 490 switch (USB_GET_STATE(xfer)) { 491 case USB_ST_TRANSFERRED: 492 pc = usbd_xfer_get_frame(xfer, 0); 493 494 DPRINTFN(6, "sc=%p actlen=%d\n", sc, len); 495 496 if (len >= (int)sc->report_len || 497 (len > 0 && sc->report_id != 0)) { 498 /* Limit report length to the maximum */ 499 if (len > (int)sc->report_len) 500 len = sc->report_len; 501 502 usbd_copy_out(pc, 0, buf, len); 503 504 /* Ignore irrelevant reports */ 505 if (sc->report_id && *buf != sc->report_id) 506 goto tr_ignore; 507 508 /* Make sure we don't process old data */ 509 if (len < sc->report_len) 510 bzero(buf + len, sc->report_len - len); 511 512 /* Strip leading "report ID" byte */ 513 if (sc->report_id) { 514 len--; 515 buf++; 516 } 517 518 wmt_process_report(sc, buf, len); 519 } else { 520 tr_ignore: 521 DPRINTF("Ignored transfer, %d bytes\n", len); 522 } 523 524 case USB_ST_SETUP: 525 tr_setup: 526 usbd_xfer_set_frame_len(xfer, 0, sc->isize); 527 usbd_transfer_submit(xfer); 528 break; 529 default: 530 if (error != USB_ERR_CANCELLED) { 531 /* Try clear stall first */ 532 usbd_xfer_set_stall(xfer); 533 goto tr_setup; 534 } 535 break; 536 } 537 } 538 539 static void 540 wmt_ev_close_11(struct evdev_dev *evdev, void *ev_softc) 541 { 542 struct wmt_softc *sc = ev_softc; 543 544 mtx_assert(&sc->mtx, MA_OWNED); 545 usbd_transfer_stop(sc->xfer[WMT_INTR_DT]); 546 } 547 548 static int 549 wmt_ev_open_11(struct evdev_dev *evdev, void *ev_softc) 550 { 551 struct wmt_softc *sc = ev_softc; 552 553 mtx_assert(&sc->mtx, MA_OWNED); 554 usbd_transfer_start(sc->xfer[WMT_INTR_DT]); 555 556 return (0); 557 } 558 559 #if __FreeBSD_version >= 1200077 560 static int 561 wmt_ev_close(struct evdev_dev *evdev) 562 { 563 struct wmt_softc *sc = evdev_get_softc(evdev); 564 565 wmt_ev_close_11(evdev, sc); 566 567 return (0); 568 } 569 570 static int 571 wmt_ev_open(struct evdev_dev *evdev) 572 { 573 struct wmt_softc *sc = evdev_get_softc(evdev); 574 575 return (wmt_ev_open_11(evdev, sc)); 576 577 } 578 #endif 579 580 /* port of userland hid_report_size() from usbhid(3) to kernel */ 581 static int 582 wmt_hid_report_size(const void *buf, uint16_t len, enum hid_kind k, uint8_t id) 583 { 584 struct hid_data *d; 585 struct hid_item h; 586 uint32_t temp; 587 uint32_t hpos; 588 uint32_t lpos; 589 int report_id = 0; 590 591 hpos = 0; 592 lpos = 0xFFFFFFFF; 593 594 for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) { 595 if (h.kind == k && h.report_ID == id) { 596 /* compute minimum */ 597 if (lpos > h.loc.pos) 598 lpos = h.loc.pos; 599 /* compute end position */ 600 temp = h.loc.pos + (h.loc.size * h.loc.count); 601 /* compute maximum */ 602 if (hpos < temp) 603 hpos = temp; 604 if (h.report_ID != 0) 605 report_id = 1; 606 } 607 } 608 hid_end_parse(d); 609 610 /* safety check - can happen in case of currupt descriptors */ 611 if (lpos > hpos) 612 temp = 0; 613 else 614 temp = hpos - lpos; 615 616 /* return length in bytes rounded up */ 617 return ((temp + 7) / 8 + report_id); 618 } 619 620 static bool 621 wmt_hid_parse(struct wmt_softc *sc, const void *d_ptr, uint16_t d_len) 622 { 623 struct hid_item hi; 624 struct hid_data *hd; 625 size_t i; 626 size_t cont = 0; 627 uint32_t caps = 0; 628 int32_t cont_count_max = 0; 629 uint8_t report_id = 0; 630 uint8_t cont_max_rid = 0; 631 uint8_t thqa_cert_rid = 0; 632 bool touch_coll = false; 633 bool finger_coll = false; 634 bool cont_count_found = false; 635 bool scan_time_found = false; 636 637 #define WMT_HI_ABSOLUTE(hi) \ 638 (((hi).flags & (HIO_CONST|HIO_VARIABLE|HIO_RELATIVE)) == HIO_VARIABLE) 639 #define HUMS_THQA_CERT 0xC5 640 641 /* Parse features for maximum contact count */ 642 hd = hid_start_parse(d_ptr, d_len, 1 << hid_feature); 643 while (hid_get_item(hd, &hi)) { 644 switch (hi.kind) { 645 case hid_collection: 646 if (hi.collevel == 1 && hi.usage == 647 HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN)) 648 touch_coll = true; 649 break; 650 case hid_endcollection: 651 if (hi.collevel == 0 && touch_coll) 652 touch_coll = false; 653 break; 654 case hid_feature: 655 if (hi.collevel == 1 && touch_coll && hi.usage == 656 HID_USAGE2(HUP_MICROSOFT, HUMS_THQA_CERT)) { 657 thqa_cert_rid = hi.report_ID; 658 break; 659 } 660 if (hi.collevel == 1 && touch_coll && hi.usage == 661 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX)) { 662 cont_count_max = hi.logical_maximum; 663 cont_max_rid = hi.report_ID; 664 if (sc != NULL) 665 sc->cont_max_loc = hi.loc; 666 } 667 break; 668 default: 669 break; 670 } 671 } 672 hid_end_parse(hd); 673 674 /* Maximum contact count is required usage */ 675 if (cont_max_rid == 0) 676 return (false); 677 678 touch_coll = false; 679 680 /* Parse input for other parameters */ 681 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input); 682 while (hid_get_item(hd, &hi)) { 683 switch (hi.kind) { 684 case hid_collection: 685 if (hi.collevel == 1 && hi.usage == 686 HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN)) 687 touch_coll = true; 688 else if (touch_coll && hi.collevel == 2 && 689 (report_id == 0 || report_id == hi.report_ID) && 690 hi.usage == HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER)) 691 finger_coll = true; 692 break; 693 case hid_endcollection: 694 if (hi.collevel == 1 && finger_coll) { 695 finger_coll = false; 696 cont++; 697 } else if (hi.collevel == 0 && touch_coll) 698 touch_coll = false; 699 break; 700 case hid_input: 701 /* 702 * Ensure that all usages are located within the same 703 * report and proper collection. 704 */ 705 if (WMT_HI_ABSOLUTE(hi) && touch_coll && 706 (report_id == 0 || report_id == hi.report_ID)) 707 report_id = hi.report_ID; 708 else 709 break; 710 711 if (hi.collevel == 1 && hi.usage == 712 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTCOUNT)) { 713 cont_count_found = true; 714 if (sc != NULL) 715 sc->nconts_loc = hi.loc; 716 break; 717 } 718 /* Scan time is required but clobbered by evdev */ 719 if (hi.collevel == 1 && hi.usage == 720 HID_USAGE2(HUP_DIGITIZERS, HUD_SCAN_TIME)) { 721 scan_time_found = true; 722 break; 723 } 724 725 if (!finger_coll || hi.collevel != 2) 726 break; 727 if (sc == NULL && cont > 0) 728 break; 729 if (cont >= MAX_MT_SLOTS) { 730 DPRINTF("Finger %zu ignored\n", cont); 731 break; 732 } 733 734 for (i = 0; i < WMT_N_USAGES; i++) { 735 if (hi.usage == wmt_hid_map[i].usage) { 736 if (sc == NULL) { 737 if (USAGE_SUPPORTED(caps, i)) 738 continue; 739 caps |= 1 << i; 740 break; 741 } 742 /* 743 * HUG_X usage is an array mapped to 744 * both ABS_MT_POSITION and ABS_MT_TOOL 745 * events. So don`t stop search if we 746 * already have HUG_X mapping done. 747 */ 748 if (sc->locs[cont][i].size) 749 continue; 750 sc->locs[cont][i] = hi.loc; 751 /* 752 * Hid parser returns valid logical and 753 * physical sizes for first finger only 754 * at least on ElanTS 0x04f3:0x0012. 755 */ 756 if (cont > 0) 757 break; 758 caps |= 1 << i; 759 sc->ai[i] = (struct wmt_absinfo) { 760 .max = hi.logical_maximum, 761 .min = hi.logical_minimum, 762 .res = hid_item_resolution(&hi), 763 }; 764 break; 765 } 766 } 767 break; 768 default: 769 break; 770 } 771 } 772 hid_end_parse(hd); 773 774 /* Check for required HID Usages */ 775 if (!cont_count_found || !scan_time_found || cont == 0) 776 return (false); 777 for (i = 0; i < WMT_N_USAGES; i++) { 778 if (wmt_hid_map[i].required && !USAGE_SUPPORTED(caps, i)) 779 return (false); 780 } 781 782 /* Stop probing here */ 783 if (sc == NULL) 784 return (true); 785 786 /* 787 * According to specifications 'Contact Count Maximum' should be read 788 * from Feature Report rather than from HID descriptor. Set sane 789 * default value now to handle the case of 'Get Report' request failure 790 */ 791 if (cont_count_max < 1) 792 cont_count_max = cont; 793 794 /* Cap contact count maximum to MAX_MT_SLOTS */ 795 if (cont_count_max > MAX_MT_SLOTS) 796 cont_count_max = MAX_MT_SLOTS; 797 798 /* Set number of MT protocol type B slots */ 799 sc->ai[WMT_SLOT] = (struct wmt_absinfo) { 800 .min = 0, 801 .max = cont_count_max - 1, 802 .res = 0, 803 }; 804 805 /* Report touch orientation if both width and height are supported */ 806 if (USAGE_SUPPORTED(caps, WMT_WIDTH) && 807 USAGE_SUPPORTED(caps, WMT_HEIGHT)) { 808 caps |= (1 << WMT_ORIENTATION); 809 sc->ai[WMT_ORIENTATION].max = 1; 810 } 811 812 sc->isize = hid_report_size(d_ptr, d_len, hid_input, NULL); 813 sc->report_len = wmt_hid_report_size(d_ptr, d_len, hid_input, 814 report_id); 815 sc->cont_max_rlen = wmt_hid_report_size(d_ptr, d_len, hid_feature, 816 cont_max_rid); 817 if (thqa_cert_rid > 0) 818 sc->thqa_cert_rlen = wmt_hid_report_size(d_ptr, d_len, 819 hid_feature, thqa_cert_rid); 820 821 sc->report_id = report_id; 822 sc->caps = caps; 823 sc->nconts_max = cont; 824 sc->cont_max_rid = cont_max_rid; 825 sc->thqa_cert_rid = thqa_cert_rid; 826 827 /* Announce information about the touch device */ 828 device_printf(sc->dev, 829 "%d contacts and [%s%s%s%s%s]. Report range [%d:%d] - [%d:%d]\n", 830 (int)cont_count_max, 831 USAGE_SUPPORTED(sc->caps, WMT_IN_RANGE) ? "R" : "", 832 USAGE_SUPPORTED(sc->caps, WMT_CONFIDENCE) ? "C" : "", 833 USAGE_SUPPORTED(sc->caps, WMT_WIDTH) ? "W" : "", 834 USAGE_SUPPORTED(sc->caps, WMT_HEIGHT) ? "H" : "", 835 USAGE_SUPPORTED(sc->caps, WMT_PRESSURE) ? "P" : "", 836 (int)sc->ai[WMT_X].min, (int)sc->ai[WMT_Y].min, 837 (int)sc->ai[WMT_X].max, (int)sc->ai[WMT_Y].max); 838 return (true); 839 } 840 841 static void 842 wmt_cont_max_parse(struct wmt_softc *sc, const void *r_ptr, uint16_t r_len) 843 { 844 uint32_t cont_count_max; 845 846 cont_count_max = hid_get_data_unsigned((const uint8_t *)r_ptr + 1, 847 r_len - 1, &sc->cont_max_loc); 848 if (cont_count_max > MAX_MT_SLOTS) { 849 DPRINTF("Hardware reported %d contacts while only %d is " 850 "supported\n", (int)cont_count_max, MAX_MT_SLOTS); 851 cont_count_max = MAX_MT_SLOTS; 852 } 853 /* Feature report is a primary source of 'Contact Count Maximum' */ 854 if (cont_count_max > 0 && 855 cont_count_max != sc->ai[WMT_SLOT].max + 1) { 856 sc->ai[WMT_SLOT].max = cont_count_max - 1; 857 device_printf(sc->dev, "%d feature report contacts", 858 cont_count_max); 859 } 860 } 861 862 static const STRUCT_USB_HOST_ID wmt_devs[] = { 863 /* generic HID class w/o boot interface */ 864 {USB_IFACE_CLASS(UICLASS_HID), 865 USB_IFACE_SUBCLASS(0),}, 866 }; 867 868 static devclass_t wmt_devclass; 869 870 static device_method_t wmt_methods[] = { 871 DEVMETHOD(device_probe, wmt_probe), 872 DEVMETHOD(device_attach, wmt_attach), 873 DEVMETHOD(device_detach, wmt_detach), 874 875 DEVMETHOD_END 876 }; 877 878 static driver_t wmt_driver = { 879 .name = "wmt", 880 .methods = wmt_methods, 881 .size = sizeof(struct wmt_softc), 882 }; 883 884 DRIVER_MODULE(wmt, uhub, wmt_driver, wmt_devclass, NULL, 0); 885 MODULE_DEPEND(wmt, usb, 1, 1, 1); 886 MODULE_DEPEND(wmt, evdev, 1, 1, 1); 887 MODULE_VERSION(wmt, 1); 888 USB_PNP_HOST_INFO(wmt_devs); 889