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/caps.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 lockmgr(&sc->sc_lock, LK_EXCLUSIVE); 516 sc->sc_flags &= ~UHID_FLAG_IMMED; 517 lockmgr(&sc->sc_lock, LK_RELEASE); 518 519 if (usb_fifo_alloc_buffer(fifo, 520 sc->sc_isize + 1, UHID_FRAME_NUM)) { 521 return (ENOMEM); 522 } 523 } 524 if (fflags & FWRITE) { 525 if (usb_fifo_alloc_buffer(fifo, 526 sc->sc_osize + 1, UHID_FRAME_NUM)) { 527 return (ENOMEM); 528 } 529 } 530 return (0); 531 } 532 533 static void 534 uhid_close(struct usb_fifo *fifo, int fflags) 535 { 536 if (fflags & (FREAD | FWRITE)) { 537 usb_fifo_free_buffer(fifo); 538 } 539 } 540 541 static int 542 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, 543 int fflags) 544 { 545 struct uhid_softc *sc = usb_fifo_softc(fifo); 546 struct usb_gen_descriptor *ugd; 547 uint32_t size; 548 int error = 0; 549 uint8_t id; 550 551 switch (cmd) { 552 case USB_GET_REPORT_DESC: 553 ugd = addr; 554 if (sc->sc_repdesc_size > ugd->ugd_maxlen) { 555 size = ugd->ugd_maxlen; 556 } else { 557 size = sc->sc_repdesc_size; 558 } 559 ugd->ugd_actlen = size; 560 if (ugd->ugd_data == NULL) 561 break; /* descriptor length only */ 562 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size); 563 break; 564 565 case USB_SET_IMMED: 566 if (!(fflags & FREAD)) { 567 error = EPERM; 568 break; 569 } 570 if (*(int *)addr) { 571 572 /* do a test read */ 573 574 error = uhid_get_report(sc, UHID_INPUT_REPORT, 575 sc->sc_iid, NULL, NULL, sc->sc_isize); 576 if (error) { 577 break; 578 } 579 lockmgr(&sc->sc_lock, LK_EXCLUSIVE); 580 sc->sc_flags |= UHID_FLAG_IMMED; 581 lockmgr(&sc->sc_lock, LK_RELEASE); 582 } else { 583 lockmgr(&sc->sc_lock, LK_EXCLUSIVE); 584 sc->sc_flags &= ~UHID_FLAG_IMMED; 585 lockmgr(&sc->sc_lock, LK_RELEASE); 586 } 587 break; 588 589 case USB_GET_REPORT: 590 if (!(fflags & FREAD)) { 591 error = EPERM; 592 break; 593 } 594 ugd = addr; 595 switch (ugd->ugd_report_type) { 596 case UHID_INPUT_REPORT: 597 size = sc->sc_isize; 598 id = sc->sc_iid; 599 break; 600 case UHID_OUTPUT_REPORT: 601 size = sc->sc_osize; 602 id = sc->sc_oid; 603 break; 604 case UHID_FEATURE_REPORT: 605 size = sc->sc_fsize; 606 id = sc->sc_fid; 607 break; 608 default: 609 return (EINVAL); 610 } 611 if (id != 0) 612 copyin(ugd->ugd_data, &id, 1); 613 error = uhid_get_report(sc, ugd->ugd_report_type, id, 614 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); 615 break; 616 617 case USB_SET_REPORT: 618 if (!(fflags & FWRITE)) { 619 error = EPERM; 620 break; 621 } 622 ugd = addr; 623 switch (ugd->ugd_report_type) { 624 case UHID_INPUT_REPORT: 625 size = sc->sc_isize; 626 id = sc->sc_iid; 627 break; 628 case UHID_OUTPUT_REPORT: 629 size = sc->sc_osize; 630 id = sc->sc_oid; 631 break; 632 case UHID_FEATURE_REPORT: 633 size = sc->sc_fsize; 634 id = sc->sc_fid; 635 break; 636 default: 637 return (EINVAL); 638 } 639 if (id != 0) 640 copyin(ugd->ugd_data, &id, 1); 641 error = uhid_set_report(sc, ugd->ugd_report_type, id, 642 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); 643 break; 644 645 case USB_GET_REPORT_ID: 646 *(int *)addr = 0; /* XXX: we only support reportid 0? */ 647 break; 648 649 default: 650 error = EINVAL; 651 break; 652 } 653 return (error); 654 } 655 656 static const STRUCT_USB_HOST_ID uhid_devs[] = { 657 /* generic HID class */ 658 {USB_IFACE_CLASS(UICLASS_HID),}, 659 /* the Xbox 360 gamepad doesn't use the HID class */ 660 {USB_IFACE_CLASS(UICLASS_VENDOR), 661 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER), 662 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),}, 663 }; 664 665 static int 666 uhid_probe(device_t dev) 667 { 668 struct usb_attach_arg *uaa = device_get_ivars(dev); 669 int error; 670 671 DPRINTFN(11, "\n"); 672 673 if (uaa->usb_mode != USB_MODE_HOST) 674 return (ENXIO); 675 676 error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa); 677 if (error) 678 return (error); 679 680 if (usb_test_quirk(uaa, UQ_HID_IGNORE)) 681 return (ENXIO); 682 683 /* 684 * Don't attach to mouse and keyboard devices, hence then no 685 * "nomatch" event is generated and then ums and ukbd won't 686 * attach properly when loaded. 687 */ 688 if ((uaa->info.bInterfaceClass == UICLASS_HID) && 689 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 690 (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) && 691 !usb_test_quirk(uaa, UQ_KBD_IGNORE)) || 692 ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) && 693 !usb_test_quirk(uaa, UQ_UMS_IGNORE)))) 694 return (ENXIO); 695 696 return (BUS_PROBE_GENERIC); 697 } 698 699 static int 700 uhid_attach(device_t dev) 701 { 702 struct usb_attach_arg *uaa = device_get_ivars(dev); 703 struct uhid_softc *sc = device_get_softc(dev); 704 int unit = device_get_unit(dev); 705 int error = 0; 706 707 DPRINTFN(10, "sc=%p\n", sc); 708 709 device_set_usb_desc(dev); 710 711 lockinit(&sc->sc_lock, "uhid lock", 0, LK_CANRECURSE); 712 713 sc->sc_udev = uaa->device; 714 715 sc->sc_iface_no = uaa->info.bIfaceNum; 716 sc->sc_iface_index = uaa->info.bIfaceIndex; 717 718 error = usbd_transfer_setup(uaa->device, 719 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config, 720 UHID_N_TRANSFER, sc, &sc->sc_lock); 721 722 if (error) { 723 DPRINTF("error=%s\n", usbd_errstr(error)); 724 goto detach; 725 } 726 if (uaa->info.idVendor == USB_VENDOR_WACOM) { 727 728 /* the report descriptor for the Wacom Graphire is broken */ 729 730 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) { 731 732 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr); 733 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr); 734 sc->sc_flags |= UHID_FLAG_STATIC_DESC; 735 736 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) { 737 738 static uint8_t reportbuf[] = {2, 2, 2}; 739 740 /* 741 * The Graphire3 needs 0x0202 to be written to 742 * feature report ID 2 before it'll start 743 * returning digitizer data. 744 */ 745 error = usbd_req_set_report(uaa->device, NULL, 746 reportbuf, sizeof(reportbuf), 747 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2); 748 749 if (error) { 750 DPRINTF("set report failed, error=%s (ignored)\n", 751 usbd_errstr(error)); 752 } 753 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr); 754 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr); 755 sc->sc_flags |= UHID_FLAG_STATIC_DESC; 756 } 757 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) && 758 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) && 759 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { 760 761 /* the Xbox 360 gamepad has no report descriptor */ 762 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr); 763 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr); 764 sc->sc_flags |= UHID_FLAG_STATIC_DESC; 765 } 766 if (sc->sc_repdesc_ptr == NULL) { 767 768 error = usbd_req_get_hid_desc(uaa->device, NULL, 769 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size, 770 M_USBDEV, uaa->info.bIfaceIndex); 771 772 if (error) { 773 device_printf(dev, "no report descriptor\n"); 774 goto detach; 775 } 776 } 777 error = usbd_req_set_idle(uaa->device, NULL, 778 uaa->info.bIfaceIndex, 0, 0); 779 780 if (error) { 781 DPRINTF("set idle failed, error=%s (ignored)\n", 782 usbd_errstr(error)); 783 } 784 sc->sc_isize = hid_report_size 785 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid); 786 787 sc->sc_osize = hid_report_size 788 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid); 789 790 sc->sc_fsize = hid_report_size 791 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid); 792 793 if (sc->sc_isize > UHID_BSIZE) { 794 DPRINTF("input size is too large, " 795 "%d bytes (truncating)\n", 796 sc->sc_isize); 797 sc->sc_isize = UHID_BSIZE; 798 } 799 if (sc->sc_osize > UHID_BSIZE) { 800 DPRINTF("output size is too large, " 801 "%d bytes (truncating)\n", 802 sc->sc_osize); 803 sc->sc_osize = UHID_BSIZE; 804 } 805 if (sc->sc_fsize > UHID_BSIZE) { 806 DPRINTF("feature size is too large, " 807 "%d bytes (truncating)\n", 808 sc->sc_fsize); 809 sc->sc_fsize = UHID_BSIZE; 810 } 811 812 error = usb_fifo_attach(uaa->device, sc, &sc->sc_lock, 813 &uhid_fifo_methods, &sc->sc_fifo, 814 unit, -1, uaa->info.bIfaceIndex, 815 UID_ROOT, GID_OPERATOR, 0644); 816 if (error) { 817 goto detach; 818 } 819 return (0); /* success */ 820 821 detach: 822 uhid_detach(dev); 823 return (ENOMEM); 824 } 825 826 static int 827 uhid_detach(device_t dev) 828 { 829 struct uhid_softc *sc = device_get_softc(dev); 830 831 usb_fifo_detach(&sc->sc_fifo); 832 833 usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER); 834 835 if (sc->sc_repdesc_ptr) { 836 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) { 837 kfree(sc->sc_repdesc_ptr, M_USBDEV); 838 } 839 } 840 lockuninit(&sc->sc_lock); 841 842 return (0); 843 } 844 845 static devclass_t uhid_devclass; 846 847 static device_method_t uhid_methods[] = { 848 DEVMETHOD(device_probe, uhid_probe), 849 DEVMETHOD(device_attach, uhid_attach), 850 DEVMETHOD(device_detach, uhid_detach), 851 852 DEVMETHOD_END 853 }; 854 855 static driver_t uhid_driver = { 856 .name = "uhid", 857 .methods = uhid_methods, 858 .size = sizeof(struct uhid_softc), 859 }; 860 861 DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, NULL); 862 MODULE_DEPEND(uhid, usb, 1, 1, 1); 863 MODULE_VERSION(uhid, 1); 864