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