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