1 /* $NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $ */ 2 3 /* Also already merged from NetBSD: 4 * $NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $ 5 */ 6 7 #include <sys/cdefs.h> 8 __FBSDID("$FreeBSD$"); 9 10 /*- 11 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 12 * 13 * Copyright (c) 1998 The NetBSD Foundation, Inc. 14 * All rights reserved. 15 * 16 * This code is derived from software contributed to The NetBSD Foundation 17 * by Lennart Augustsson (lennart@augustsson.net) at 18 * Carlstedt Research & Technology. 19 * 20 * Redistribution and use in source and binary forms, with or without 21 * modification, are permitted provided that the following conditions 22 * are met: 23 * 1. Redistributions of source code must retain the above copyright 24 * notice, this list of conditions and the following disclaimer. 25 * 2. Redistributions in binary form must reproduce the above copyright 26 * notice, this list of conditions and the following disclaimer in the 27 * documentation and/or other materials provided with the distribution. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 /* 43 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 44 */ 45 46 #include <sys/stdint.h> 47 #include <sys/stddef.h> 48 #include <sys/param.h> 49 #include <sys/queue.h> 50 #include <sys/types.h> 51 #include <sys/systm.h> 52 #include <sys/kernel.h> 53 #include <sys/bus.h> 54 #include <sys/module.h> 55 #include <sys/lock.h> 56 #include <sys/mutex.h> 57 #include <sys/condvar.h> 58 #include <sys/sysctl.h> 59 #include <sys/sx.h> 60 #include <sys/unistd.h> 61 #include <sys/callout.h> 62 #include <sys/malloc.h> 63 #include <sys/priv.h> 64 #include <sys/conf.h> 65 #include <sys/fcntl.h> 66 67 #include "usbdevs.h" 68 #include <dev/usb/usb.h> 69 #include <dev/usb/usbdi.h> 70 #include <dev/usb/usbdi_util.h> 71 #include <dev/usb/usbhid.h> 72 #include <dev/usb/usb_ioctl.h> 73 #include <dev/usb/usb_generic.h> 74 75 #define USB_DEBUG_VAR uhid_debug 76 #include <dev/usb/usb_debug.h> 77 78 #include <dev/usb/input/usb_rdesc.h> 79 #include <dev/usb/quirk/usb_quirk.h> 80 81 #ifdef USB_DEBUG 82 static int uhid_debug = 0; 83 84 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 85 "USB uhid"); 86 SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RWTUN, 87 &uhid_debug, 0, "Debug level"); 88 #endif 89 90 #define UHID_BSIZE 1024 /* bytes, buffer size */ 91 #define UHID_FRAME_NUM 50 /* bytes, frame number */ 92 93 enum { 94 UHID_INTR_DT_WR, 95 UHID_INTR_DT_RD, 96 UHID_CTRL_DT_WR, 97 UHID_CTRL_DT_RD, 98 UHID_N_TRANSFER, 99 }; 100 101 struct uhid_softc { 102 struct usb_fifo_sc sc_fifo; 103 struct mtx sc_mtx; 104 105 struct usb_xfer *sc_xfer[UHID_N_TRANSFER]; 106 struct usb_device *sc_udev; 107 void *sc_repdesc_ptr; 108 109 uint32_t sc_isize; 110 uint32_t sc_osize; 111 uint32_t sc_fsize; 112 113 uint16_t sc_repdesc_size; 114 115 uint8_t sc_iface_no; 116 uint8_t sc_iface_index; 117 uint8_t sc_iid; 118 uint8_t sc_oid; 119 uint8_t sc_fid; 120 uint8_t sc_flags; 121 #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */ 122 #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are 123 * static */ 124 }; 125 126 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()}; 127 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()}; 128 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()}; 129 130 /* prototypes */ 131 132 static device_probe_t uhid_probe; 133 static device_attach_t uhid_attach; 134 static device_detach_t uhid_detach; 135 136 static usb_callback_t uhid_intr_write_callback; 137 static usb_callback_t uhid_intr_read_callback; 138 static usb_callback_t uhid_write_callback; 139 static usb_callback_t uhid_read_callback; 140 141 static usb_fifo_cmd_t uhid_start_read; 142 static usb_fifo_cmd_t uhid_stop_read; 143 static usb_fifo_cmd_t uhid_start_write; 144 static usb_fifo_cmd_t uhid_stop_write; 145 static usb_fifo_open_t uhid_open; 146 static usb_fifo_close_t uhid_close; 147 static usb_fifo_ioctl_t uhid_ioctl; 148 static usb_fifo_ioctl_t uhid_ioctl_post; 149 150 static struct usb_fifo_methods uhid_fifo_methods = { 151 .f_open = &uhid_open, 152 .f_close = &uhid_close, 153 .f_ioctl = &uhid_ioctl, 154 .f_ioctl_post = &uhid_ioctl_post, 155 .f_start_read = &uhid_start_read, 156 .f_stop_read = &uhid_stop_read, 157 .f_start_write = &uhid_start_write, 158 .f_stop_write = &uhid_stop_write, 159 .basename[0] = "uhid", 160 }; 161 162 static void 163 uhid_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 164 { 165 struct uhid_softc *sc = usbd_xfer_softc(xfer); 166 struct usb_page_cache *pc; 167 int actlen; 168 169 switch (USB_GET_STATE(xfer)) { 170 case USB_ST_TRANSFERRED: 171 case USB_ST_SETUP: 172 tr_setup: 173 pc = usbd_xfer_get_frame(xfer, 0); 174 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 175 0, usbd_xfer_max_len(xfer), &actlen, 0)) { 176 usbd_xfer_set_frame_len(xfer, 0, actlen); 177 usbd_transfer_submit(xfer); 178 } 179 return; 180 181 default: /* Error */ 182 if (error != USB_ERR_CANCELLED) { 183 /* try to clear stall first */ 184 usbd_xfer_set_stall(xfer); 185 goto tr_setup; 186 } 187 return; 188 } 189 } 190 191 static void 192 uhid_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 193 { 194 struct uhid_softc *sc = usbd_xfer_softc(xfer); 195 struct usb_page_cache *pc; 196 int actlen; 197 198 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 199 200 switch (USB_GET_STATE(xfer)) { 201 case USB_ST_TRANSFERRED: 202 DPRINTF("transferred!\n"); 203 204 pc = usbd_xfer_get_frame(xfer, 0); 205 206 /* 207 * If the ID byte is non zero we allow descriptors 208 * having multiple sizes: 209 */ 210 if ((actlen >= (int)sc->sc_isize) || 211 ((actlen > 0) && (sc->sc_iid != 0))) { 212 /* limit report length to the maximum */ 213 if (actlen > (int)sc->sc_isize) 214 actlen = sc->sc_isize; 215 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, 216 0, actlen, 1); 217 } else { 218 /* ignore it */ 219 DPRINTF("ignored transfer, %d bytes\n", actlen); 220 } 221 222 case USB_ST_SETUP: 223 re_submit: 224 if (usb_fifo_put_bytes_max( 225 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 226 usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize); 227 usbd_transfer_submit(xfer); 228 } 229 return; 230 231 default: /* Error */ 232 if (error != USB_ERR_CANCELLED) { 233 /* try to clear stall first */ 234 usbd_xfer_set_stall(xfer); 235 goto re_submit; 236 } 237 return; 238 } 239 } 240 241 static void 242 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no, 243 uint8_t type, uint8_t id, uint16_t size) 244 { 245 req->bmRequestType = UT_WRITE_CLASS_INTERFACE; 246 req->bRequest = UR_SET_REPORT; 247 USETW2(req->wValue, type, id); 248 req->wIndex[0] = iface_no; 249 req->wIndex[1] = 0; 250 USETW(req->wLength, size); 251 } 252 253 static void 254 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no, 255 uint8_t type, uint8_t id, uint16_t size) 256 { 257 req->bmRequestType = UT_READ_CLASS_INTERFACE; 258 req->bRequest = UR_GET_REPORT; 259 USETW2(req->wValue, type, id); 260 req->wIndex[0] = iface_no; 261 req->wIndex[1] = 0; 262 USETW(req->wLength, size); 263 } 264 265 static void 266 uhid_write_callback(struct usb_xfer *xfer, usb_error_t error) 267 { 268 struct uhid_softc *sc = usbd_xfer_softc(xfer); 269 struct usb_device_request req; 270 struct usb_page_cache *pc; 271 uint32_t size = sc->sc_osize; 272 uint32_t actlen; 273 uint8_t id; 274 275 switch (USB_GET_STATE(xfer)) { 276 case USB_ST_TRANSFERRED: 277 case USB_ST_SETUP: 278 /* try to extract the ID byte */ 279 if (sc->sc_oid) { 280 pc = usbd_xfer_get_frame(xfer, 0); 281 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 282 0, 1, &actlen, 0)) { 283 if (actlen != 1) { 284 goto tr_error; 285 } 286 usbd_copy_out(pc, 0, &id, 1); 287 288 } else { 289 return; 290 } 291 if (size) { 292 size--; 293 } 294 } else { 295 id = 0; 296 } 297 298 pc = usbd_xfer_get_frame(xfer, 1); 299 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 300 0, UHID_BSIZE, &actlen, 1)) { 301 if (actlen != size) { 302 goto tr_error; 303 } 304 uhid_fill_set_report 305 (&req, sc->sc_iface_no, 306 UHID_OUTPUT_REPORT, id, size); 307 308 pc = usbd_xfer_get_frame(xfer, 0); 309 usbd_copy_in(pc, 0, &req, sizeof(req)); 310 311 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 312 usbd_xfer_set_frame_len(xfer, 1, size); 313 usbd_xfer_set_frames(xfer, size ? 2 : 1); 314 usbd_transfer_submit(xfer); 315 } 316 return; 317 318 default: 319 tr_error: 320 /* bomb out */ 321 usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]); 322 return; 323 } 324 } 325 326 static void 327 uhid_read_callback(struct usb_xfer *xfer, usb_error_t error) 328 { 329 struct uhid_softc *sc = usbd_xfer_softc(xfer); 330 struct usb_device_request req; 331 struct usb_page_cache *pc; 332 333 pc = usbd_xfer_get_frame(xfer, 0); 334 335 switch (USB_GET_STATE(xfer)) { 336 case USB_ST_TRANSFERRED: 337 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req), 338 sc->sc_isize, 1); 339 return; 340 341 case USB_ST_SETUP: 342 343 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) { 344 uhid_fill_get_report 345 (&req, sc->sc_iface_no, UHID_INPUT_REPORT, 346 sc->sc_iid, sc->sc_isize); 347 348 usbd_copy_in(pc, 0, &req, sizeof(req)); 349 350 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 351 usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize); 352 usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1); 353 usbd_transfer_submit(xfer); 354 } 355 return; 356 357 default: /* Error */ 358 /* bomb out */ 359 usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]); 360 return; 361 } 362 } 363 364 static const struct usb_config uhid_config[UHID_N_TRANSFER] = { 365 [UHID_INTR_DT_WR] = { 366 .type = UE_INTERRUPT, 367 .endpoint = UE_ADDR_ANY, 368 .direction = UE_DIR_OUT, 369 .flags = {.pipe_bof = 1,.no_pipe_ok = 1, }, 370 .bufsize = UHID_BSIZE, 371 .callback = &uhid_intr_write_callback, 372 }, 373 374 [UHID_INTR_DT_RD] = { 375 .type = UE_INTERRUPT, 376 .endpoint = UE_ADDR_ANY, 377 .direction = UE_DIR_IN, 378 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 379 .bufsize = UHID_BSIZE, 380 .callback = &uhid_intr_read_callback, 381 }, 382 383 [UHID_CTRL_DT_WR] = { 384 .type = UE_CONTROL, 385 .endpoint = 0x00, /* Control pipe */ 386 .direction = UE_DIR_ANY, 387 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, 388 .callback = &uhid_write_callback, 389 .timeout = 1000, /* 1 second */ 390 }, 391 392 [UHID_CTRL_DT_RD] = { 393 .type = UE_CONTROL, 394 .endpoint = 0x00, /* Control pipe */ 395 .direction = UE_DIR_ANY, 396 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, 397 .callback = &uhid_read_callback, 398 .timeout = 1000, /* 1 second */ 399 }, 400 }; 401 402 static void 403 uhid_start_read(struct usb_fifo *fifo) 404 { 405 struct uhid_softc *sc = usb_fifo_softc(fifo); 406 407 if (sc->sc_flags & UHID_FLAG_IMMED) { 408 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]); 409 } else { 410 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]); 411 } 412 } 413 414 static void 415 uhid_stop_read(struct usb_fifo *fifo) 416 { 417 struct uhid_softc *sc = usb_fifo_softc(fifo); 418 419 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]); 420 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]); 421 } 422 423 static void 424 uhid_start_write(struct usb_fifo *fifo) 425 { 426 struct uhid_softc *sc = usb_fifo_softc(fifo); 427 428 if ((sc->sc_flags & UHID_FLAG_IMMED) || 429 sc->sc_xfer[UHID_INTR_DT_WR] == NULL) { 430 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]); 431 } else { 432 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_WR]); 433 } 434 } 435 436 static void 437 uhid_stop_write(struct usb_fifo *fifo) 438 { 439 struct uhid_softc *sc = usb_fifo_softc(fifo); 440 441 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]); 442 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_WR]); 443 } 444 445 static int 446 uhid_get_report(struct uhid_softc *sc, uint8_t type, 447 uint8_t id, void *kern_data, void *user_data, 448 uint16_t len) 449 { 450 int err; 451 uint8_t free_data = 0; 452 453 if (kern_data == NULL) { 454 kern_data = malloc(len, M_USBDEV, M_WAITOK); 455 free_data = 1; 456 } 457 err = usbd_req_get_report(sc->sc_udev, NULL, kern_data, 458 len, sc->sc_iface_index, type, id); 459 if (err) { 460 err = ENXIO; 461 goto done; 462 } 463 if (user_data) { 464 /* dummy buffer */ 465 err = copyout(kern_data, user_data, len); 466 if (err) { 467 goto done; 468 } 469 } 470 done: 471 if (free_data) { 472 free(kern_data, M_USBDEV); 473 } 474 return (err); 475 } 476 477 static int 478 uhid_set_report(struct uhid_softc *sc, uint8_t type, 479 uint8_t id, void *kern_data, void *user_data, 480 uint16_t len) 481 { 482 int err; 483 uint8_t free_data = 0; 484 485 if (kern_data == NULL) { 486 kern_data = malloc(len, M_USBDEV, M_WAITOK); 487 free_data = 1; 488 err = copyin(user_data, kern_data, len); 489 if (err) { 490 goto done; 491 } 492 } 493 err = usbd_req_set_report(sc->sc_udev, NULL, kern_data, 494 len, sc->sc_iface_index, type, id); 495 if (err) { 496 err = ENXIO; 497 goto done; 498 } 499 done: 500 if (free_data) { 501 free(kern_data, M_USBDEV); 502 } 503 return (err); 504 } 505 506 static int 507 uhid_open(struct usb_fifo *fifo, int fflags) 508 { 509 struct uhid_softc *sc = usb_fifo_softc(fifo); 510 511 /* 512 * The buffers are one byte larger than maximum so that one 513 * can detect too large read/writes and short transfers: 514 */ 515 if (fflags & FREAD) { 516 /* reset flags */ 517 mtx_lock(&sc->sc_mtx); 518 sc->sc_flags &= ~UHID_FLAG_IMMED; 519 mtx_unlock(&sc->sc_mtx); 520 521 if (usb_fifo_alloc_buffer(fifo, 522 sc->sc_isize + 1, UHID_FRAME_NUM)) { 523 return (ENOMEM); 524 } 525 } 526 if (fflags & FWRITE) { 527 if (usb_fifo_alloc_buffer(fifo, 528 sc->sc_osize + 1, UHID_FRAME_NUM)) { 529 return (ENOMEM); 530 } 531 } 532 return (0); 533 } 534 535 static void 536 uhid_close(struct usb_fifo *fifo, int fflags) 537 { 538 if (fflags & (FREAD | FWRITE)) { 539 usb_fifo_free_buffer(fifo); 540 } 541 } 542 543 static int 544 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, 545 int fflags) 546 { 547 struct uhid_softc *sc = usb_fifo_softc(fifo); 548 struct usb_gen_descriptor *ugd; 549 uint32_t size; 550 int error = 0; 551 uint8_t id; 552 553 switch (cmd) { 554 case USB_GET_REPORT_DESC: 555 ugd = addr; 556 if (sc->sc_repdesc_size > ugd->ugd_maxlen) { 557 size = ugd->ugd_maxlen; 558 } else { 559 size = sc->sc_repdesc_size; 560 } 561 ugd->ugd_actlen = size; 562 if (ugd->ugd_data == NULL) 563 break; /* descriptor length only */ 564 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size); 565 break; 566 567 case USB_SET_IMMED: 568 if (!(fflags & FREAD)) { 569 error = EPERM; 570 break; 571 } 572 if (*(int *)addr) { 573 /* do a test read */ 574 575 error = uhid_get_report(sc, UHID_INPUT_REPORT, 576 sc->sc_iid, NULL, NULL, sc->sc_isize); 577 if (error) { 578 break; 579 } 580 mtx_lock(&sc->sc_mtx); 581 sc->sc_flags |= UHID_FLAG_IMMED; 582 mtx_unlock(&sc->sc_mtx); 583 } else { 584 mtx_lock(&sc->sc_mtx); 585 sc->sc_flags &= ~UHID_FLAG_IMMED; 586 mtx_unlock(&sc->sc_mtx); 587 } 588 break; 589 590 case USB_GET_REPORT: 591 if (!(fflags & FREAD)) { 592 error = EPERM; 593 break; 594 } 595 ugd = addr; 596 switch (ugd->ugd_report_type) { 597 case UHID_INPUT_REPORT: 598 size = sc->sc_isize; 599 id = sc->sc_iid; 600 break; 601 case UHID_OUTPUT_REPORT: 602 size = sc->sc_osize; 603 id = sc->sc_oid; 604 break; 605 case UHID_FEATURE_REPORT: 606 size = sc->sc_fsize; 607 id = sc->sc_fid; 608 break; 609 default: 610 return (EINVAL); 611 } 612 if (id != 0) 613 copyin(ugd->ugd_data, &id, 1); 614 error = uhid_get_report(sc, ugd->ugd_report_type, id, 615 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); 616 break; 617 618 case USB_SET_REPORT: 619 if (!(fflags & FWRITE)) { 620 error = EPERM; 621 break; 622 } 623 ugd = addr; 624 switch (ugd->ugd_report_type) { 625 case UHID_INPUT_REPORT: 626 size = sc->sc_isize; 627 id = sc->sc_iid; 628 break; 629 case UHID_OUTPUT_REPORT: 630 size = sc->sc_osize; 631 id = sc->sc_oid; 632 break; 633 case UHID_FEATURE_REPORT: 634 size = sc->sc_fsize; 635 id = sc->sc_fid; 636 break; 637 default: 638 return (EINVAL); 639 } 640 if (id != 0) 641 copyin(ugd->ugd_data, &id, 1); 642 error = uhid_set_report(sc, ugd->ugd_report_type, id, 643 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); 644 break; 645 646 case USB_GET_REPORT_ID: 647 *(int *)addr = 0; /* XXX: we only support reportid 0? */ 648 break; 649 650 default: 651 error = ENOIOCTL; 652 break; 653 } 654 return (error); 655 } 656 657 static int 658 uhid_ioctl_post(struct usb_fifo *fifo, u_long cmd, void *addr, 659 int fflags) 660 { 661 int error; 662 663 switch (cmd) { 664 case USB_GET_DEVICEINFO: 665 error = ugen_fill_deviceinfo(fifo, addr); 666 break; 667 668 default: 669 error = EINVAL; 670 break; 671 } 672 return (error); 673 } 674 675 static const STRUCT_USB_HOST_ID uhid_devs[] = { 676 /* generic HID class */ 677 {USB_IFACE_CLASS(UICLASS_HID),}, 678 /* the Xbox 360 gamepad doesn't use the HID class */ 679 {USB_IFACE_CLASS(UICLASS_VENDOR), 680 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER), 681 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),}, 682 }; 683 684 static int 685 uhid_probe(device_t dev) 686 { 687 struct usb_attach_arg *uaa = device_get_ivars(dev); 688 int error; 689 void *buf; 690 uint16_t len; 691 692 DPRINTFN(11, "\n"); 693 694 if (uaa->usb_mode != USB_MODE_HOST) 695 return (ENXIO); 696 697 error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa); 698 if (error) 699 return (error); 700 701 if (usb_test_quirk(uaa, UQ_HID_IGNORE)) 702 return (ENXIO); 703 704 /* 705 * Don't attach to mouse and keyboard devices, hence then no 706 * "nomatch" event is generated and then ums and ukbd won't 707 * attach properly when loaded. 708 */ 709 if ((uaa->info.bInterfaceClass == UICLASS_HID) && 710 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 711 (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) && 712 !usb_test_quirk(uaa, UQ_KBD_IGNORE)) || 713 ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) && 714 !usb_test_quirk(uaa, UQ_UMS_IGNORE)))) 715 return (ENXIO); 716 717 /* Check for mandatory multitouch usages to give wmt(4) a chance */ 718 if (!usb_test_quirk(uaa, UQ_WMT_IGNORE)) { 719 error = usbd_req_get_hid_desc(uaa->device, NULL, 720 &buf, &len, M_USBDEV, uaa->info.bIfaceIndex); 721 /* Let HID decscriptor-less devices to be handled at attach */ 722 if (!error) { 723 if (hid_locate(buf, len, 724 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX), 725 hid_feature, 0, NULL, NULL, NULL) && 726 hid_locate(buf, len, 727 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID), 728 hid_input, 0, NULL, NULL, NULL)) { 729 free(buf, M_USBDEV); 730 return (ENXIO); 731 } 732 free(buf, M_USBDEV); 733 } 734 } 735 736 return (BUS_PROBE_GENERIC); 737 } 738 739 static int 740 uhid_attach(device_t dev) 741 { 742 struct usb_attach_arg *uaa = device_get_ivars(dev); 743 struct uhid_softc *sc = device_get_softc(dev); 744 int unit = device_get_unit(dev); 745 int error = 0; 746 747 DPRINTFN(10, "sc=%p\n", sc); 748 749 device_set_usb_desc(dev); 750 751 mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE); 752 753 sc->sc_udev = uaa->device; 754 755 sc->sc_iface_no = uaa->info.bIfaceNum; 756 sc->sc_iface_index = uaa->info.bIfaceIndex; 757 758 error = usbd_transfer_setup(uaa->device, 759 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config, 760 UHID_N_TRANSFER, sc, &sc->sc_mtx); 761 762 if (error) { 763 DPRINTF("error=%s\n", usbd_errstr(error)); 764 goto detach; 765 } 766 if (uaa->info.idVendor == USB_VENDOR_WACOM) { 767 /* the report descriptor for the Wacom Graphire is broken */ 768 769 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) { 770 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr); 771 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr); 772 sc->sc_flags |= UHID_FLAG_STATIC_DESC; 773 774 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) { 775 static uint8_t reportbuf[] = {2, 2, 2}; 776 777 /* 778 * The Graphire3 needs 0x0202 to be written to 779 * feature report ID 2 before it'll start 780 * returning digitizer data. 781 */ 782 error = usbd_req_set_report(uaa->device, NULL, 783 reportbuf, sizeof(reportbuf), 784 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2); 785 786 if (error) { 787 DPRINTF("set report failed, error=%s (ignored)\n", 788 usbd_errstr(error)); 789 } 790 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr); 791 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr); 792 sc->sc_flags |= UHID_FLAG_STATIC_DESC; 793 } 794 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) && 795 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) && 796 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { 797 static const uint8_t reportbuf[3] = {1, 3, 0}; 798 /* 799 * Turn off the four LEDs on the gamepad which 800 * are blinking by default: 801 */ 802 error = usbd_req_set_report(uaa->device, NULL, 803 __DECONST(void *, reportbuf), sizeof(reportbuf), 804 uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0); 805 if (error) { 806 DPRINTF("set output report failed, error=%s (ignored)\n", 807 usbd_errstr(error)); 808 } 809 /* the Xbox 360 gamepad has no report descriptor */ 810 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr); 811 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr); 812 sc->sc_flags |= UHID_FLAG_STATIC_DESC; 813 } 814 if (sc->sc_repdesc_ptr == NULL) { 815 error = usbd_req_get_hid_desc(uaa->device, NULL, 816 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size, 817 M_USBDEV, uaa->info.bIfaceIndex); 818 819 if (error) { 820 device_printf(dev, "no report descriptor\n"); 821 goto detach; 822 } 823 } 824 error = usbd_req_set_idle(uaa->device, NULL, 825 uaa->info.bIfaceIndex, 0, 0); 826 827 if (error) { 828 DPRINTF("set idle failed, error=%s (ignored)\n", 829 usbd_errstr(error)); 830 } 831 sc->sc_isize = hid_report_size 832 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid); 833 834 sc->sc_osize = hid_report_size 835 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid); 836 837 sc->sc_fsize = hid_report_size 838 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid); 839 840 if (sc->sc_isize > UHID_BSIZE) { 841 DPRINTF("input size is too large, " 842 "%d bytes (truncating)\n", 843 sc->sc_isize); 844 sc->sc_isize = UHID_BSIZE; 845 } 846 if (sc->sc_osize > UHID_BSIZE) { 847 DPRINTF("output size is too large, " 848 "%d bytes (truncating)\n", 849 sc->sc_osize); 850 sc->sc_osize = UHID_BSIZE; 851 } 852 if (sc->sc_fsize > UHID_BSIZE) { 853 DPRINTF("feature size is too large, " 854 "%d bytes (truncating)\n", 855 sc->sc_fsize); 856 sc->sc_fsize = UHID_BSIZE; 857 } 858 859 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 860 &uhid_fifo_methods, &sc->sc_fifo, 861 unit, -1, uaa->info.bIfaceIndex, 862 UID_ROOT, GID_OPERATOR, 0644); 863 if (error) { 864 goto detach; 865 } 866 return (0); /* success */ 867 868 detach: 869 uhid_detach(dev); 870 return (ENOMEM); 871 } 872 873 static int 874 uhid_detach(device_t dev) 875 { 876 struct uhid_softc *sc = device_get_softc(dev); 877 878 usb_fifo_detach(&sc->sc_fifo); 879 880 usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER); 881 882 if (sc->sc_repdesc_ptr) { 883 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) { 884 free(sc->sc_repdesc_ptr, M_USBDEV); 885 } 886 } 887 mtx_destroy(&sc->sc_mtx); 888 889 return (0); 890 } 891 892 static devclass_t uhid_devclass; 893 894 static device_method_t uhid_methods[] = { 895 DEVMETHOD(device_probe, uhid_probe), 896 DEVMETHOD(device_attach, uhid_attach), 897 DEVMETHOD(device_detach, uhid_detach), 898 899 DEVMETHOD_END 900 }; 901 902 static driver_t uhid_driver = { 903 .name = "uhid", 904 .methods = uhid_methods, 905 .size = sizeof(struct uhid_softc), 906 }; 907 908 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0); 909 MODULE_DEPEND(uhid, usb, 1, 1, 1); 910 MODULE_VERSION(uhid, 1); 911 USB_PNP_HOST_INFO(uhid_devs); 912