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