1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <dev/usb/usb_mfunc.h> 30 #include <dev/usb/usb_error.h> 31 #include <dev/usb/usb.h> 32 #include <dev/usb/usb_ioctl.h> 33 #include <dev/usb/usbhid.h> 34 35 #define USB_DEBUG_VAR usb2_debug 36 37 #include <dev/usb/usb_core.h> 38 #include <dev/usb/usb_busdma.h> 39 #include <dev/usb/usb_request.h> 40 #include <dev/usb/usb_process.h> 41 #include <dev/usb/usb_transfer.h> 42 #include <dev/usb/usb_debug.h> 43 #include <dev/usb/usb_device.h> 44 #include <dev/usb/usb_util.h> 45 #include <dev/usb/usb_dynamic.h> 46 47 #include <dev/usb/usb_controller.h> 48 #include <dev/usb/usb_bus.h> 49 #include <sys/ctype.h> 50 51 #if USB_DEBUG 52 static int usb2_pr_poll_delay = USB_PORT_RESET_DELAY; 53 static int usb2_pr_recovery_delay = USB_PORT_RESET_RECOVERY; 54 static int usb2_ss_delay = 0; 55 56 SYSCTL_INT(_hw_usb2, OID_AUTO, pr_poll_delay, CTLFLAG_RW, 57 &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms"); 58 SYSCTL_INT(_hw_usb2, OID_AUTO, pr_recovery_delay, CTLFLAG_RW, 59 &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms"); 60 SYSCTL_INT(_hw_usb2, OID_AUTO, ss_delay, CTLFLAG_RW, 61 &usb2_ss_delay, 0, "USB status stage delay in ms"); 62 #endif 63 64 /*------------------------------------------------------------------------* 65 * usb2_do_request_callback 66 * 67 * This function is the USB callback for generic USB Host control 68 * transfers. 69 *------------------------------------------------------------------------*/ 70 void 71 usb2_do_request_callback(struct usb2_xfer *xfer) 72 { 73 ; /* workaround for a bug in "indent" */ 74 75 DPRINTF("st=%u\n", USB_GET_STATE(xfer)); 76 77 switch (USB_GET_STATE(xfer)) { 78 case USB_ST_SETUP: 79 usb2_start_hardware(xfer); 80 break; 81 default: 82 usb2_cv_signal(xfer->xroot->udev->default_cv); 83 break; 84 } 85 } 86 87 /*------------------------------------------------------------------------* 88 * usb2_do_clear_stall_callback 89 * 90 * This function is the USB callback for generic clear stall requests. 91 *------------------------------------------------------------------------*/ 92 void 93 usb2_do_clear_stall_callback(struct usb2_xfer *xfer) 94 { 95 struct usb2_device_request req; 96 struct usb2_device *udev; 97 struct usb2_pipe *pipe; 98 struct usb2_pipe *pipe_end; 99 struct usb2_pipe *pipe_first; 100 uint8_t to = USB_EP_MAX; 101 102 udev = xfer->xroot->udev; 103 104 USB_BUS_LOCK(udev->bus); 105 106 /* round robin pipe clear stall */ 107 108 pipe = udev->pipe_curr; 109 pipe_end = udev->pipes + USB_EP_MAX; 110 pipe_first = udev->pipes; 111 if (pipe == NULL) { 112 pipe = pipe_first; 113 } 114 switch (USB_GET_STATE(xfer)) { 115 case USB_ST_TRANSFERRED: 116 if (pipe->edesc && 117 pipe->is_stalled) { 118 pipe->toggle_next = 0; 119 pipe->is_stalled = 0; 120 /* start up the current or next transfer, if any */ 121 usb2_command_wrapper(&pipe->pipe_q, 122 pipe->pipe_q.curr); 123 } 124 pipe++; 125 126 case USB_ST_SETUP: 127 tr_setup: 128 if (pipe == pipe_end) { 129 pipe = pipe_first; 130 } 131 if (pipe->edesc && 132 pipe->is_stalled) { 133 134 /* setup a clear-stall packet */ 135 136 req.bmRequestType = UT_WRITE_ENDPOINT; 137 req.bRequest = UR_CLEAR_FEATURE; 138 USETW(req.wValue, UF_ENDPOINT_HALT); 139 req.wIndex[0] = pipe->edesc->bEndpointAddress; 140 req.wIndex[1] = 0; 141 USETW(req.wLength, 0); 142 143 /* copy in the transfer */ 144 145 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req)); 146 147 /* set length */ 148 xfer->frlengths[0] = sizeof(req); 149 xfer->nframes = 1; 150 USB_BUS_UNLOCK(udev->bus); 151 152 usb2_start_hardware(xfer); 153 154 USB_BUS_LOCK(udev->bus); 155 break; 156 } 157 pipe++; 158 if (--to) 159 goto tr_setup; 160 break; 161 162 default: 163 if (xfer->error == USB_ERR_CANCELLED) { 164 break; 165 } 166 goto tr_setup; 167 } 168 169 /* store current pipe */ 170 udev->pipe_curr = pipe; 171 USB_BUS_UNLOCK(udev->bus); 172 } 173 174 /*------------------------------------------------------------------------* 175 * usb2_do_request_flags and usb2_do_request 176 * 177 * Description of arguments passed to these functions: 178 * 179 * "udev" - this is the "usb2_device" structure pointer on which the 180 * request should be performed. It is possible to call this function 181 * in both Host Side mode and Device Side mode. 182 * 183 * "mtx" - if this argument is non-NULL the mutex pointed to by it 184 * will get dropped and picked up during the execution of this 185 * function, hence this function sometimes needs to sleep. If this 186 * argument is NULL it has no effect. 187 * 188 * "req" - this argument must always be non-NULL and points to an 189 * 8-byte structure holding the USB request to be done. The USB 190 * request structure has a bit telling the direction of the USB 191 * request, if it is a read or a write. 192 * 193 * "data" - if the "wLength" part of the structure pointed to by "req" 194 * is non-zero this argument must point to a valid kernel buffer which 195 * can hold at least "wLength" bytes. If "wLength" is zero "data" can 196 * be NULL. 197 * 198 * "flags" - here is a list of valid flags: 199 * 200 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than 201 * specified 202 * 203 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed 204 * at a later point in time. This is tunable by the "hw.usb.ss_delay" 205 * sysctl. This flag is mostly useful for debugging. 206 * 207 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland 208 * pointer. 209 * 210 * "actlen" - if non-NULL the actual transfer length will be stored in 211 * the 16-bit unsigned integer pointed to by "actlen". This 212 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is 213 * used. 214 * 215 * "timeout" - gives the timeout for the control transfer in 216 * milliseconds. A "timeout" value less than 50 milliseconds is 217 * treated like a 50 millisecond timeout. A "timeout" value greater 218 * than 30 seconds is treated like a 30 second timeout. This USB stack 219 * does not allow control requests without a timeout. 220 * 221 * NOTE: This function is thread safe. All calls to 222 * "usb2_do_request_flags" will be serialised by the use of an 223 * internal "sx_lock". 224 * 225 * Returns: 226 * 0: Success 227 * Else: Failure 228 *------------------------------------------------------------------------*/ 229 usb2_error_t 230 usb2_do_request_flags(struct usb2_device *udev, struct mtx *mtx, 231 struct usb2_device_request *req, void *data, uint16_t flags, 232 uint16_t *actlen, usb2_timeout_t timeout) 233 { 234 struct usb2_xfer *xfer; 235 const void *desc; 236 int err = 0; 237 usb2_ticks_t start_ticks; 238 usb2_ticks_t delta_ticks; 239 usb2_ticks_t max_ticks; 240 uint16_t length; 241 uint16_t temp; 242 243 if (timeout < 50) { 244 /* timeout is too small */ 245 timeout = 50; 246 } 247 if (timeout > 30000) { 248 /* timeout is too big */ 249 timeout = 30000; 250 } 251 length = UGETW(req->wLength); 252 253 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x " 254 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n", 255 udev, req->bmRequestType, req->bRequest, 256 req->wValue[1], req->wValue[0], 257 req->wIndex[1], req->wIndex[0], 258 req->wLength[1], req->wLength[0]); 259 260 /* 261 * Set "actlen" to a known value in case the caller does not 262 * check the return value: 263 */ 264 if (actlen) { 265 *actlen = 0; 266 } 267 #if (USB_HAVE_USER_IO == 0) 268 if (flags & USB_USER_DATA_PTR) 269 return (USB_ERR_INVAL); 270 #endif 271 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 272 DPRINTF("USB device mode\n"); 273 (usb2_temp_get_desc_p) (udev, req, &desc, &temp); 274 if (length > temp) { 275 if (!(flags & USB_SHORT_XFER_OK)) { 276 return (USB_ERR_SHORT_XFER); 277 } 278 length = temp; 279 } 280 if (actlen) { 281 *actlen = length; 282 } 283 if (length > 0) { 284 #if USB_HAVE_USER_IO 285 if (flags & USB_USER_DATA_PTR) { 286 if (copyout(desc, data, length)) { 287 return (USB_ERR_INVAL); 288 } 289 } else 290 #endif 291 bcopy(desc, data, length); 292 } 293 return (0); /* success */ 294 } 295 if (mtx) { 296 mtx_unlock(mtx); 297 if (mtx != &Giant) { 298 mtx_assert(mtx, MA_NOTOWNED); 299 } 300 } 301 /* 302 * Grab the default sx-lock so that serialisation 303 * is achieved when multiple threads are involved: 304 */ 305 306 sx_xlock(udev->default_sx); 307 308 /* 309 * Setup a new USB transfer or use the existing one, if any: 310 */ 311 usb2_default_transfer_setup(udev); 312 313 xfer = udev->default_xfer[0]; 314 if (xfer == NULL) { 315 /* most likely out of memory */ 316 err = USB_ERR_NOMEM; 317 goto done; 318 } 319 USB_XFER_LOCK(xfer); 320 321 if (flags & USB_DELAY_STATUS_STAGE) { 322 xfer->flags.manual_status = 1; 323 } else { 324 xfer->flags.manual_status = 0; 325 } 326 327 xfer->timeout = timeout; 328 329 start_ticks = ticks; 330 331 max_ticks = USB_MS_TO_TICKS(timeout); 332 333 usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req)); 334 335 xfer->frlengths[0] = sizeof(*req); 336 xfer->nframes = 2; 337 338 while (1) { 339 temp = length; 340 if (temp > xfer->max_data_length) { 341 temp = xfer->max_data_length; 342 } 343 xfer->frlengths[1] = temp; 344 345 if (temp > 0) { 346 if (!(req->bmRequestType & UT_READ)) { 347 #if USB_HAVE_USER_IO 348 if (flags & USB_USER_DATA_PTR) { 349 USB_XFER_UNLOCK(xfer); 350 err = usb2_copy_in_user(xfer->frbuffers + 1, 351 0, data, temp); 352 USB_XFER_LOCK(xfer); 353 if (err) { 354 err = USB_ERR_INVAL; 355 break; 356 } 357 } else 358 #endif 359 usb2_copy_in(xfer->frbuffers + 1, 360 0, data, temp); 361 } 362 xfer->nframes = 2; 363 } else { 364 if (xfer->frlengths[0] == 0) { 365 if (xfer->flags.manual_status) { 366 #if USB_DEBUG 367 int temp; 368 369 temp = usb2_ss_delay; 370 if (temp > 5000) { 371 temp = 5000; 372 } 373 if (temp > 0) { 374 usb2_pause_mtx( 375 xfer->xroot->xfer_mtx, 376 USB_MS_TO_TICKS(temp)); 377 } 378 #endif 379 xfer->flags.manual_status = 0; 380 } else { 381 break; 382 } 383 } 384 xfer->nframes = 1; 385 } 386 387 usb2_transfer_start(xfer); 388 389 while (usb2_transfer_pending(xfer)) { 390 usb2_cv_wait(udev->default_cv, 391 xfer->xroot->xfer_mtx); 392 } 393 394 err = xfer->error; 395 396 if (err) { 397 break; 398 } 399 /* subtract length of SETUP packet, if any */ 400 401 if (xfer->aframes > 0) { 402 xfer->actlen -= xfer->frlengths[0]; 403 } else { 404 xfer->actlen = 0; 405 } 406 407 /* check for short packet */ 408 409 if (temp > xfer->actlen) { 410 temp = xfer->actlen; 411 if (!(flags & USB_SHORT_XFER_OK)) { 412 err = USB_ERR_SHORT_XFER; 413 } 414 length = temp; 415 } 416 if (temp > 0) { 417 if (req->bmRequestType & UT_READ) { 418 #if USB_HAVE_USER_IO 419 if (flags & USB_USER_DATA_PTR) { 420 USB_XFER_UNLOCK(xfer); 421 err = usb2_copy_out_user(xfer->frbuffers + 1, 422 0, data, temp); 423 USB_XFER_LOCK(xfer); 424 if (err) { 425 err = USB_ERR_INVAL; 426 break; 427 } 428 } else 429 #endif 430 usb2_copy_out(xfer->frbuffers + 1, 431 0, data, temp); 432 } 433 } 434 /* 435 * Clear "frlengths[0]" so that we don't send the setup 436 * packet again: 437 */ 438 xfer->frlengths[0] = 0; 439 440 /* update length and data pointer */ 441 length -= temp; 442 data = USB_ADD_BYTES(data, temp); 443 444 if (actlen) { 445 (*actlen) += temp; 446 } 447 /* check for timeout */ 448 449 delta_ticks = ticks - start_ticks; 450 if (delta_ticks > max_ticks) { 451 if (!err) { 452 err = USB_ERR_TIMEOUT; 453 } 454 } 455 if (err) { 456 break; 457 } 458 } 459 460 if (err) { 461 /* 462 * Make sure that the control endpoint is no longer 463 * blocked in case of a non-transfer related error: 464 */ 465 usb2_transfer_stop(xfer); 466 } 467 USB_XFER_UNLOCK(xfer); 468 469 done: 470 sx_xunlock(udev->default_sx); 471 472 if (mtx) { 473 mtx_lock(mtx); 474 } 475 return ((usb2_error_t)err); 476 } 477 478 /*------------------------------------------------------------------------* 479 * usb2_do_request_proc - factored out code 480 * 481 * This function is factored out code. It does basically the same like 482 * usb2_do_request_flags, except it will check the status of the 483 * passed process argument before doing the USB request. If the 484 * process is draining the USB_ERR_IOERROR code will be returned. It 485 * is assumed that the mutex associated with the process is locked 486 * when calling this function. 487 *------------------------------------------------------------------------*/ 488 usb2_error_t 489 usb2_do_request_proc(struct usb2_device *udev, struct usb2_process *pproc, 490 struct usb2_device_request *req, void *data, uint16_t flags, 491 uint16_t *actlen, usb2_timeout_t timeout) 492 { 493 usb2_error_t err; 494 uint16_t len; 495 496 /* get request data length */ 497 len = UGETW(req->wLength); 498 499 /* check if the device is being detached */ 500 if (usb2_proc_is_gone(pproc)) { 501 err = USB_ERR_IOERROR; 502 goto done; 503 } 504 505 /* forward the USB request */ 506 err = usb2_do_request_flags(udev, pproc->up_mtx, 507 req, data, flags, actlen, timeout); 508 509 done: 510 /* on failure we zero the data */ 511 /* on short packet we zero the unused data */ 512 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) { 513 if (err) 514 memset(data, 0, len); 515 else if (actlen && *actlen != len) 516 memset(((uint8_t *)data) + *actlen, 0, len - *actlen); 517 } 518 return (err); 519 } 520 521 /*------------------------------------------------------------------------* 522 * usb2_req_reset_port 523 * 524 * This function will instruct an USB HUB to perform a reset sequence 525 * on the specified port number. 526 * 527 * Returns: 528 * 0: Success. The USB device should now be at address zero. 529 * Else: Failure. No USB device is present and the USB port should be 530 * disabled. 531 *------------------------------------------------------------------------*/ 532 usb2_error_t 533 usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port) 534 { 535 struct usb2_port_status ps; 536 usb2_error_t err; 537 uint16_t n; 538 539 #if USB_DEBUG 540 uint16_t pr_poll_delay; 541 uint16_t pr_recovery_delay; 542 543 #endif 544 err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET); 545 if (err) { 546 goto done; 547 } 548 #if USB_DEBUG 549 /* range check input parameters */ 550 pr_poll_delay = usb2_pr_poll_delay; 551 if (pr_poll_delay < 1) { 552 pr_poll_delay = 1; 553 } else if (pr_poll_delay > 1000) { 554 pr_poll_delay = 1000; 555 } 556 pr_recovery_delay = usb2_pr_recovery_delay; 557 if (pr_recovery_delay > 1000) { 558 pr_recovery_delay = 1000; 559 } 560 #endif 561 n = 0; 562 while (1) { 563 #if USB_DEBUG 564 /* wait for the device to recover from reset */ 565 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay)); 566 n += pr_poll_delay; 567 #else 568 /* wait for the device to recover from reset */ 569 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY)); 570 n += USB_PORT_RESET_DELAY; 571 #endif 572 err = usb2_req_get_port_status(udev, mtx, &ps, port); 573 if (err) { 574 goto done; 575 } 576 /* if the device disappeared, just give up */ 577 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) { 578 goto done; 579 } 580 /* check if reset is complete */ 581 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) { 582 break; 583 } 584 /* check for timeout */ 585 if (n > 1000) { 586 n = 0; 587 break; 588 } 589 } 590 591 /* clear port reset first */ 592 err = usb2_req_clear_port_feature( 593 udev, mtx, port, UHF_C_PORT_RESET); 594 if (err) { 595 goto done; 596 } 597 /* check for timeout */ 598 if (n == 0) { 599 err = USB_ERR_TIMEOUT; 600 goto done; 601 } 602 #if USB_DEBUG 603 /* wait for the device to recover from reset */ 604 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay)); 605 #else 606 /* wait for the device to recover from reset */ 607 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY)); 608 #endif 609 610 done: 611 DPRINTFN(2, "port %d reset returning error=%s\n", 612 port, usb2_errstr(err)); 613 return (err); 614 } 615 616 /*------------------------------------------------------------------------* 617 * usb2_req_get_desc 618 * 619 * This function can be used to retrieve USB descriptors. It contains 620 * some additional logic like zeroing of missing descriptor bytes and 621 * retrying an USB descriptor in case of failure. The "min_len" 622 * argument specifies the minimum descriptor length. The "max_len" 623 * argument specifies the maximum descriptor length. If the real 624 * descriptor length is less than the minimum length the missing 625 * byte(s) will be zeroed. The type field, the second byte of the USB 626 * descriptor, will get forced to the correct type. If the "actlen" 627 * pointer is non-NULL, the actual length of the transfer will get 628 * stored in the 16-bit unsigned integer which it is pointing to. The 629 * first byte of the descriptor will not get updated. If the "actlen" 630 * pointer is NULL the first byte of the descriptor will get updated 631 * to reflect the actual length instead. If "min_len" is not equal to 632 * "max_len" then this function will try to retrive the beginning of 633 * the descriptor and base the maximum length on the first byte of the 634 * descriptor. 635 * 636 * Returns: 637 * 0: Success 638 * Else: Failure 639 *------------------------------------------------------------------------*/ 640 usb2_error_t 641 usb2_req_get_desc(struct usb2_device *udev, 642 struct mtx *mtx, uint16_t *actlen, void *desc, 643 uint16_t min_len, uint16_t max_len, 644 uint16_t id, uint8_t type, uint8_t index, 645 uint8_t retries) 646 { 647 struct usb2_device_request req; 648 uint8_t *buf; 649 usb2_error_t err; 650 651 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n", 652 id, type, index, max_len); 653 654 req.bmRequestType = UT_READ_DEVICE; 655 req.bRequest = UR_GET_DESCRIPTOR; 656 USETW2(req.wValue, type, index); 657 USETW(req.wIndex, id); 658 659 while (1) { 660 661 if ((min_len < 2) || (max_len < 2)) { 662 err = USB_ERR_INVAL; 663 goto done; 664 } 665 USETW(req.wLength, min_len); 666 667 err = usb2_do_request_flags(udev, mtx, &req, 668 desc, 0, NULL, 1000); 669 670 if (err) { 671 if (!retries) { 672 goto done; 673 } 674 retries--; 675 676 usb2_pause_mtx(mtx, hz / 5); 677 678 continue; 679 } 680 buf = desc; 681 682 if (min_len == max_len) { 683 684 /* enforce correct length */ 685 if ((buf[0] > min_len) && (actlen == NULL)) 686 buf[0] = min_len; 687 688 /* enforce correct type */ 689 buf[1] = type; 690 691 goto done; 692 } 693 /* range check */ 694 695 if (max_len > buf[0]) { 696 max_len = buf[0]; 697 } 698 /* zero minimum data */ 699 700 while (min_len > max_len) { 701 min_len--; 702 buf[min_len] = 0; 703 } 704 705 /* set new minimum length */ 706 707 min_len = max_len; 708 } 709 done: 710 if (actlen != NULL) { 711 if (err) 712 *actlen = 0; 713 else 714 *actlen = min_len; 715 } 716 return (err); 717 } 718 719 /*------------------------------------------------------------------------* 720 * usb2_req_get_string_any 721 * 722 * This function will return the string given by "string_index" 723 * using the first language ID. The maximum length "len" includes 724 * the terminating zero. The "len" argument should be twice as 725 * big pluss 2 bytes, compared with the actual maximum string length ! 726 * 727 * Returns: 728 * 0: Success 729 * Else: Failure 730 *------------------------------------------------------------------------*/ 731 usb2_error_t 732 usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf, 733 uint16_t len, uint8_t string_index) 734 { 735 char *s; 736 uint8_t *temp; 737 uint16_t i; 738 uint16_t n; 739 uint16_t c; 740 uint8_t swap; 741 usb2_error_t err; 742 743 if (len == 0) { 744 /* should not happen */ 745 return (USB_ERR_NORMAL_COMPLETION); 746 } 747 if (string_index == 0) { 748 /* this is the language table */ 749 buf[0] = 0; 750 return (USB_ERR_INVAL); 751 } 752 if (udev->flags.no_strings) { 753 buf[0] = 0; 754 return (USB_ERR_STALLED); 755 } 756 err = usb2_req_get_string_desc 757 (udev, mtx, buf, len, udev->langid, string_index); 758 if (err) { 759 buf[0] = 0; 760 return (err); 761 } 762 temp = (uint8_t *)buf; 763 764 if (temp[0] < 2) { 765 /* string length is too short */ 766 buf[0] = 0; 767 return (USB_ERR_INVAL); 768 } 769 /* reserve one byte for terminating zero */ 770 len--; 771 772 /* find maximum length */ 773 s = buf; 774 n = (temp[0] / 2) - 1; 775 if (n > len) { 776 n = len; 777 } 778 /* skip descriptor header */ 779 temp += 2; 780 781 /* reset swap state */ 782 swap = 3; 783 784 /* convert and filter */ 785 for (i = 0; (i != n); i++) { 786 c = UGETW(temp + (2 * i)); 787 788 /* convert from Unicode, handle buggy strings */ 789 if (((c & 0xff00) == 0) && (swap & 1)) { 790 /* Little Endian, default */ 791 *s = c; 792 swap = 1; 793 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 794 /* Big Endian */ 795 *s = c >> 8; 796 swap = 2; 797 } else { 798 /* silently skip bad character */ 799 continue; 800 } 801 802 /* 803 * Filter by default - we don't allow greater and less than 804 * signs because they might confuse the dmesg printouts! 805 */ 806 if ((*s == '<') || (*s == '>') || (!isprint(*s))) { 807 /* silently skip bad character */ 808 continue; 809 } 810 s++; 811 } 812 *s = 0; /* zero terminate resulting string */ 813 return (USB_ERR_NORMAL_COMPLETION); 814 } 815 816 /*------------------------------------------------------------------------* 817 * usb2_req_get_string_desc 818 * 819 * If you don't know the language ID, consider using 820 * "usb2_req_get_string_any()". 821 * 822 * Returns: 823 * 0: Success 824 * Else: Failure 825 *------------------------------------------------------------------------*/ 826 usb2_error_t 827 usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc, 828 uint16_t max_len, uint16_t lang_id, 829 uint8_t string_index) 830 { 831 return (usb2_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id, 832 UDESC_STRING, string_index, 0)); 833 } 834 835 /*------------------------------------------------------------------------* 836 * usb2_req_get_config_desc_ptr 837 * 838 * This function is used in device side mode to retrieve the pointer 839 * to the generated config descriptor. This saves allocating space for 840 * an additional config descriptor when setting the configuration. 841 * 842 * Returns: 843 * 0: Success 844 * Else: Failure 845 *------------------------------------------------------------------------*/ 846 usb2_error_t 847 usb2_req_get_config_desc_ptr(struct usb2_device *udev, 848 struct usb2_config_descriptor **ppcd, uint8_t config_index) 849 { 850 uint16_t len; 851 852 struct usb2_device_request req; 853 854 if (udev->flags.usb2_mode != USB_MODE_DEVICE) 855 return (USB_ERR_INVAL); 856 857 req.bmRequestType = UT_READ_CLASS_DEVICE; 858 req.bRequest = UR_GET_DESCRIPTOR; 859 USETW2(req.wValue, UDESC_CONFIG, config_index); 860 USETW(req.wIndex, 0); 861 USETW(req.wLength, 0); 862 863 (usb2_temp_get_desc_p) (udev, &req, 864 __DECONST(const void **, ppcd), &len); 865 866 return (*ppcd ? USB_ERR_NORMAL_COMPLETION : USB_ERR_INVAL); 867 } 868 869 /*------------------------------------------------------------------------* 870 * usb2_req_get_config_desc 871 * 872 * Returns: 873 * 0: Success 874 * Else: Failure 875 *------------------------------------------------------------------------*/ 876 usb2_error_t 877 usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx, 878 struct usb2_config_descriptor *d, uint8_t conf_index) 879 { 880 usb2_error_t err; 881 882 DPRINTFN(4, "confidx=%d\n", conf_index); 883 884 err = usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 885 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0); 886 if (err) { 887 goto done; 888 } 889 /* Extra sanity checking */ 890 if (UGETW(d->wTotalLength) < sizeof(*d)) { 891 err = USB_ERR_INVAL; 892 } 893 done: 894 return (err); 895 } 896 897 /*------------------------------------------------------------------------* 898 * usb2_req_get_config_desc_full 899 * 900 * This function gets the complete USB configuration descriptor and 901 * ensures that "wTotalLength" is correct. 902 * 903 * Returns: 904 * 0: Success 905 * Else: Failure 906 *------------------------------------------------------------------------*/ 907 usb2_error_t 908 usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx, 909 struct usb2_config_descriptor **ppcd, struct malloc_type *mtype, 910 uint8_t index) 911 { 912 struct usb2_config_descriptor cd; 913 struct usb2_config_descriptor *cdesc; 914 uint16_t len; 915 usb2_error_t err; 916 917 DPRINTFN(4, "index=%d\n", index); 918 919 *ppcd = NULL; 920 921 err = usb2_req_get_config_desc(udev, mtx, &cd, index); 922 if (err) { 923 return (err); 924 } 925 /* get full descriptor */ 926 len = UGETW(cd.wTotalLength); 927 if (len < sizeof(*cdesc)) { 928 /* corrupt descriptor */ 929 return (USB_ERR_INVAL); 930 } 931 cdesc = malloc(len, mtype, M_WAITOK); 932 if (cdesc == NULL) { 933 return (USB_ERR_NOMEM); 934 } 935 err = usb2_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0, 936 UDESC_CONFIG, index, 3); 937 if (err) { 938 free(cdesc, mtype); 939 return (err); 940 } 941 /* make sure that the device is not fooling us: */ 942 USETW(cdesc->wTotalLength, len); 943 944 *ppcd = cdesc; 945 946 return (0); /* success */ 947 } 948 949 /*------------------------------------------------------------------------* 950 * usb2_req_get_device_desc 951 * 952 * Returns: 953 * 0: Success 954 * Else: Failure 955 *------------------------------------------------------------------------*/ 956 usb2_error_t 957 usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx, 958 struct usb2_device_descriptor *d) 959 { 960 DPRINTFN(4, "\n"); 961 return (usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 962 sizeof(*d), 0, UDESC_DEVICE, 0, 3)); 963 } 964 965 /*------------------------------------------------------------------------* 966 * usb2_req_get_alt_interface_no 967 * 968 * Returns: 969 * 0: Success 970 * Else: Failure 971 *------------------------------------------------------------------------*/ 972 usb2_error_t 973 usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx, 974 uint8_t *alt_iface_no, uint8_t iface_index) 975 { 976 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 977 struct usb2_device_request req; 978 979 if ((iface == NULL) || (iface->idesc == NULL)) { 980 return (USB_ERR_INVAL); 981 } 982 req.bmRequestType = UT_READ_INTERFACE; 983 req.bRequest = UR_GET_INTERFACE; 984 USETW(req.wValue, 0); 985 req.wIndex[0] = iface->idesc->bInterfaceNumber; 986 req.wIndex[1] = 0; 987 USETW(req.wLength, 1); 988 return (usb2_do_request(udev, mtx, &req, alt_iface_no)); 989 } 990 991 /*------------------------------------------------------------------------* 992 * usb2_req_set_alt_interface_no 993 * 994 * Returns: 995 * 0: Success 996 * Else: Failure 997 *------------------------------------------------------------------------*/ 998 usb2_error_t 999 usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx, 1000 uint8_t iface_index, uint8_t alt_no) 1001 { 1002 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1003 struct usb2_device_request req; 1004 1005 if ((iface == NULL) || (iface->idesc == NULL)) { 1006 return (USB_ERR_INVAL); 1007 } 1008 req.bmRequestType = UT_WRITE_INTERFACE; 1009 req.bRequest = UR_SET_INTERFACE; 1010 req.wValue[0] = alt_no; 1011 req.wValue[1] = 0; 1012 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1013 req.wIndex[1] = 0; 1014 USETW(req.wLength, 0); 1015 return (usb2_do_request(udev, mtx, &req, 0)); 1016 } 1017 1018 /*------------------------------------------------------------------------* 1019 * usb2_req_get_device_status 1020 * 1021 * Returns: 1022 * 0: Success 1023 * Else: Failure 1024 *------------------------------------------------------------------------*/ 1025 usb2_error_t 1026 usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx, 1027 struct usb2_status *st) 1028 { 1029 struct usb2_device_request req; 1030 1031 req.bmRequestType = UT_READ_DEVICE; 1032 req.bRequest = UR_GET_STATUS; 1033 USETW(req.wValue, 0); 1034 USETW(req.wIndex, 0); 1035 USETW(req.wLength, sizeof(*st)); 1036 return (usb2_do_request(udev, mtx, &req, st)); 1037 } 1038 1039 /*------------------------------------------------------------------------* 1040 * usb2_req_get_hub_descriptor 1041 * 1042 * Returns: 1043 * 0: Success 1044 * Else: Failure 1045 *------------------------------------------------------------------------*/ 1046 usb2_error_t 1047 usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx, 1048 struct usb2_hub_descriptor *hd, uint8_t nports) 1049 { 1050 struct usb2_device_request req; 1051 uint16_t len = (nports + 7 + (8 * 8)) / 8; 1052 1053 req.bmRequestType = UT_READ_CLASS_DEVICE; 1054 req.bRequest = UR_GET_DESCRIPTOR; 1055 USETW2(req.wValue, UDESC_HUB, 0); 1056 USETW(req.wIndex, 0); 1057 USETW(req.wLength, len); 1058 return (usb2_do_request(udev, mtx, &req, hd)); 1059 } 1060 1061 /*------------------------------------------------------------------------* 1062 * usb2_req_get_hub_status 1063 * 1064 * Returns: 1065 * 0: Success 1066 * Else: Failure 1067 *------------------------------------------------------------------------*/ 1068 usb2_error_t 1069 usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx, 1070 struct usb2_hub_status *st) 1071 { 1072 struct usb2_device_request req; 1073 1074 req.bmRequestType = UT_READ_CLASS_DEVICE; 1075 req.bRequest = UR_GET_STATUS; 1076 USETW(req.wValue, 0); 1077 USETW(req.wIndex, 0); 1078 USETW(req.wLength, sizeof(struct usb2_hub_status)); 1079 return (usb2_do_request(udev, mtx, &req, st)); 1080 } 1081 1082 /*------------------------------------------------------------------------* 1083 * usb2_req_set_address 1084 * 1085 * This function is used to set the address for an USB device. After 1086 * port reset the USB device will respond at address zero. 1087 * 1088 * Returns: 1089 * 0: Success 1090 * Else: Failure 1091 *------------------------------------------------------------------------*/ 1092 usb2_error_t 1093 usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr) 1094 { 1095 struct usb2_device_request req; 1096 1097 DPRINTFN(6, "setting device address=%d\n", addr); 1098 1099 req.bmRequestType = UT_WRITE_DEVICE; 1100 req.bRequest = UR_SET_ADDRESS; 1101 USETW(req.wValue, addr); 1102 USETW(req.wIndex, 0); 1103 USETW(req.wLength, 0); 1104 1105 /* Setting the address should not take more than 1 second ! */ 1106 return (usb2_do_request_flags(udev, mtx, &req, NULL, 1107 USB_DELAY_STATUS_STAGE, NULL, 1000)); 1108 } 1109 1110 /*------------------------------------------------------------------------* 1111 * usb2_req_get_port_status 1112 * 1113 * Returns: 1114 * 0: Success 1115 * Else: Failure 1116 *------------------------------------------------------------------------*/ 1117 usb2_error_t 1118 usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx, 1119 struct usb2_port_status *ps, uint8_t port) 1120 { 1121 struct usb2_device_request req; 1122 1123 req.bmRequestType = UT_READ_CLASS_OTHER; 1124 req.bRequest = UR_GET_STATUS; 1125 USETW(req.wValue, 0); 1126 req.wIndex[0] = port; 1127 req.wIndex[1] = 0; 1128 USETW(req.wLength, sizeof *ps); 1129 return (usb2_do_request(udev, mtx, &req, ps)); 1130 } 1131 1132 /*------------------------------------------------------------------------* 1133 * usb2_req_clear_hub_feature 1134 * 1135 * Returns: 1136 * 0: Success 1137 * Else: Failure 1138 *------------------------------------------------------------------------*/ 1139 usb2_error_t 1140 usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx, 1141 uint16_t sel) 1142 { 1143 struct usb2_device_request req; 1144 1145 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1146 req.bRequest = UR_CLEAR_FEATURE; 1147 USETW(req.wValue, sel); 1148 USETW(req.wIndex, 0); 1149 USETW(req.wLength, 0); 1150 return (usb2_do_request(udev, mtx, &req, 0)); 1151 } 1152 1153 /*------------------------------------------------------------------------* 1154 * usb2_req_set_hub_feature 1155 * 1156 * Returns: 1157 * 0: Success 1158 * Else: Failure 1159 *------------------------------------------------------------------------*/ 1160 usb2_error_t 1161 usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx, 1162 uint16_t sel) 1163 { 1164 struct usb2_device_request req; 1165 1166 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1167 req.bRequest = UR_SET_FEATURE; 1168 USETW(req.wValue, sel); 1169 USETW(req.wIndex, 0); 1170 USETW(req.wLength, 0); 1171 return (usb2_do_request(udev, mtx, &req, 0)); 1172 } 1173 1174 /*------------------------------------------------------------------------* 1175 * usb2_req_clear_port_feature 1176 * 1177 * Returns: 1178 * 0: Success 1179 * Else: Failure 1180 *------------------------------------------------------------------------*/ 1181 usb2_error_t 1182 usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx, 1183 uint8_t port, uint16_t sel) 1184 { 1185 struct usb2_device_request req; 1186 1187 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1188 req.bRequest = UR_CLEAR_FEATURE; 1189 USETW(req.wValue, sel); 1190 req.wIndex[0] = port; 1191 req.wIndex[1] = 0; 1192 USETW(req.wLength, 0); 1193 return (usb2_do_request(udev, mtx, &req, 0)); 1194 } 1195 1196 /*------------------------------------------------------------------------* 1197 * usb2_req_set_port_feature 1198 * 1199 * Returns: 1200 * 0: Success 1201 * Else: Failure 1202 *------------------------------------------------------------------------*/ 1203 usb2_error_t 1204 usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx, 1205 uint8_t port, uint16_t sel) 1206 { 1207 struct usb2_device_request req; 1208 1209 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1210 req.bRequest = UR_SET_FEATURE; 1211 USETW(req.wValue, sel); 1212 req.wIndex[0] = port; 1213 req.wIndex[1] = 0; 1214 USETW(req.wLength, 0); 1215 return (usb2_do_request(udev, mtx, &req, 0)); 1216 } 1217 1218 /*------------------------------------------------------------------------* 1219 * usb2_req_set_protocol 1220 * 1221 * Returns: 1222 * 0: Success 1223 * Else: Failure 1224 *------------------------------------------------------------------------*/ 1225 usb2_error_t 1226 usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx, 1227 uint8_t iface_index, uint16_t report) 1228 { 1229 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1230 struct usb2_device_request req; 1231 1232 if ((iface == NULL) || (iface->idesc == NULL)) { 1233 return (USB_ERR_INVAL); 1234 } 1235 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n", 1236 iface, report, iface->idesc->bInterfaceNumber); 1237 1238 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1239 req.bRequest = UR_SET_PROTOCOL; 1240 USETW(req.wValue, report); 1241 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1242 req.wIndex[1] = 0; 1243 USETW(req.wLength, 0); 1244 return (usb2_do_request(udev, mtx, &req, 0)); 1245 } 1246 1247 /*------------------------------------------------------------------------* 1248 * usb2_req_set_report 1249 * 1250 * Returns: 1251 * 0: Success 1252 * Else: Failure 1253 *------------------------------------------------------------------------*/ 1254 usb2_error_t 1255 usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len, 1256 uint8_t iface_index, uint8_t type, uint8_t id) 1257 { 1258 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1259 struct usb2_device_request req; 1260 1261 if ((iface == NULL) || (iface->idesc == NULL)) { 1262 return (USB_ERR_INVAL); 1263 } 1264 DPRINTFN(5, "len=%d\n", len); 1265 1266 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1267 req.bRequest = UR_SET_REPORT; 1268 USETW2(req.wValue, type, id); 1269 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1270 req.wIndex[1] = 0; 1271 USETW(req.wLength, len); 1272 return (usb2_do_request(udev, mtx, &req, data)); 1273 } 1274 1275 /*------------------------------------------------------------------------* 1276 * usb2_req_get_report 1277 * 1278 * Returns: 1279 * 0: Success 1280 * Else: Failure 1281 *------------------------------------------------------------------------*/ 1282 usb2_error_t 1283 usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data, 1284 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id) 1285 { 1286 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1287 struct usb2_device_request req; 1288 1289 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) { 1290 return (USB_ERR_INVAL); 1291 } 1292 DPRINTFN(5, "len=%d\n", len); 1293 1294 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1295 req.bRequest = UR_GET_REPORT; 1296 USETW2(req.wValue, type, id); 1297 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1298 req.wIndex[1] = 0; 1299 USETW(req.wLength, len); 1300 return (usb2_do_request(udev, mtx, &req, data)); 1301 } 1302 1303 /*------------------------------------------------------------------------* 1304 * usb2_req_set_idle 1305 * 1306 * Returns: 1307 * 0: Success 1308 * Else: Failure 1309 *------------------------------------------------------------------------*/ 1310 usb2_error_t 1311 usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx, 1312 uint8_t iface_index, uint8_t duration, uint8_t id) 1313 { 1314 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1315 struct usb2_device_request req; 1316 1317 if ((iface == NULL) || (iface->idesc == NULL)) { 1318 return (USB_ERR_INVAL); 1319 } 1320 DPRINTFN(5, "%d %d\n", duration, id); 1321 1322 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1323 req.bRequest = UR_SET_IDLE; 1324 USETW2(req.wValue, duration, id); 1325 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1326 req.wIndex[1] = 0; 1327 USETW(req.wLength, 0); 1328 return (usb2_do_request(udev, mtx, &req, 0)); 1329 } 1330 1331 /*------------------------------------------------------------------------* 1332 * usb2_req_get_report_descriptor 1333 * 1334 * Returns: 1335 * 0: Success 1336 * Else: Failure 1337 *------------------------------------------------------------------------*/ 1338 usb2_error_t 1339 usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx, 1340 void *d, uint16_t size, uint8_t iface_index) 1341 { 1342 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1343 struct usb2_device_request req; 1344 1345 if ((iface == NULL) || (iface->idesc == NULL)) { 1346 return (USB_ERR_INVAL); 1347 } 1348 req.bmRequestType = UT_READ_INTERFACE; 1349 req.bRequest = UR_GET_DESCRIPTOR; 1350 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 1351 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1352 req.wIndex[1] = 0; 1353 USETW(req.wLength, size); 1354 return (usb2_do_request(udev, mtx, &req, d)); 1355 } 1356 1357 /*------------------------------------------------------------------------* 1358 * usb2_req_set_config 1359 * 1360 * This function is used to select the current configuration number in 1361 * both USB device side mode and USB host side mode. When setting the 1362 * configuration the function of the interfaces can change. 1363 * 1364 * Returns: 1365 * 0: Success 1366 * Else: Failure 1367 *------------------------------------------------------------------------*/ 1368 usb2_error_t 1369 usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf) 1370 { 1371 struct usb2_device_request req; 1372 1373 DPRINTF("setting config %d\n", conf); 1374 1375 /* do "set configuration" request */ 1376 1377 req.bmRequestType = UT_WRITE_DEVICE; 1378 req.bRequest = UR_SET_CONFIG; 1379 req.wValue[0] = conf; 1380 req.wValue[1] = 0; 1381 USETW(req.wIndex, 0); 1382 USETW(req.wLength, 0); 1383 return (usb2_do_request(udev, mtx, &req, 0)); 1384 } 1385 1386 /*------------------------------------------------------------------------* 1387 * usb2_req_get_config 1388 * 1389 * Returns: 1390 * 0: Success 1391 * Else: Failure 1392 *------------------------------------------------------------------------*/ 1393 usb2_error_t 1394 usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf) 1395 { 1396 struct usb2_device_request req; 1397 1398 req.bmRequestType = UT_READ_DEVICE; 1399 req.bRequest = UR_GET_CONFIG; 1400 USETW(req.wValue, 0); 1401 USETW(req.wIndex, 0); 1402 USETW(req.wLength, 1); 1403 return (usb2_do_request(udev, mtx, &req, pconf)); 1404 } 1405 1406 /*------------------------------------------------------------------------* 1407 * usb2_req_re_enumerate 1408 * 1409 * NOTE: After this function returns the hardware is in the 1410 * unconfigured state! The application is responsible for setting a 1411 * new configuration. 1412 * 1413 * Returns: 1414 * 0: Success 1415 * Else: Failure 1416 *------------------------------------------------------------------------*/ 1417 usb2_error_t 1418 usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx) 1419 { 1420 struct usb2_device *parent_hub; 1421 usb2_error_t err; 1422 uint8_t old_addr; 1423 uint8_t do_retry = 1; 1424 1425 if (udev->flags.usb2_mode != USB_MODE_HOST) { 1426 return (USB_ERR_INVAL); 1427 } 1428 old_addr = udev->address; 1429 parent_hub = udev->parent_hub; 1430 if (parent_hub == NULL) { 1431 return (USB_ERR_INVAL); 1432 } 1433 retry: 1434 err = usb2_req_reset_port(parent_hub, mtx, udev->port_no); 1435 if (err) { 1436 DPRINTFN(0, "addr=%d, port reset failed\n", old_addr); 1437 goto done; 1438 } 1439 /* 1440 * After that the port has been reset our device should be at 1441 * address zero: 1442 */ 1443 udev->address = USB_START_ADDR; 1444 1445 /* reset "bMaxPacketSize" */ 1446 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1447 1448 /* 1449 * Restore device address: 1450 */ 1451 err = usb2_req_set_address(udev, mtx, old_addr); 1452 if (err) { 1453 /* XXX ignore any errors! */ 1454 DPRINTFN(0, "addr=%d, set address failed! (ignored)\n", 1455 old_addr); 1456 } 1457 /* restore device address */ 1458 udev->address = old_addr; 1459 1460 /* allow device time to set new address */ 1461 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1462 1463 /* get the device descriptor */ 1464 err = usb2_req_get_desc(udev, mtx, NULL, &udev->ddesc, 1465 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1466 if (err) { 1467 DPRINTFN(0, "getting device descriptor " 1468 "at addr %d failed!\n", udev->address); 1469 goto done; 1470 } 1471 /* get the full device descriptor */ 1472 err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc); 1473 if (err) { 1474 DPRINTFN(0, "addr=%d, getting device " 1475 "descriptor failed!\n", old_addr); 1476 goto done; 1477 } 1478 done: 1479 if (err && do_retry) { 1480 /* give the USB firmware some time to load */ 1481 usb2_pause_mtx(mtx, hz / 2); 1482 /* no more retries after this retry */ 1483 do_retry = 0; 1484 /* try again */ 1485 goto retry; 1486 } 1487 /* restore address */ 1488 udev->address = old_addr; 1489 return (err); 1490 } 1491 1492 /*------------------------------------------------------------------------* 1493 * usb2_req_clear_device_feature 1494 * 1495 * Returns: 1496 * 0: Success 1497 * Else: Failure 1498 *------------------------------------------------------------------------*/ 1499 usb2_error_t 1500 usb2_req_clear_device_feature(struct usb2_device *udev, struct mtx *mtx, 1501 uint16_t sel) 1502 { 1503 struct usb2_device_request req; 1504 1505 req.bmRequestType = UT_WRITE_DEVICE; 1506 req.bRequest = UR_CLEAR_FEATURE; 1507 USETW(req.wValue, sel); 1508 USETW(req.wIndex, 0); 1509 USETW(req.wLength, 0); 1510 return (usb2_do_request(udev, mtx, &req, 0)); 1511 } 1512 1513 /*------------------------------------------------------------------------* 1514 * usb2_req_set_device_feature 1515 * 1516 * Returns: 1517 * 0: Success 1518 * Else: Failure 1519 *------------------------------------------------------------------------*/ 1520 usb2_error_t 1521 usb2_req_set_device_feature(struct usb2_device *udev, struct mtx *mtx, 1522 uint16_t sel) 1523 { 1524 struct usb2_device_request req; 1525 1526 req.bmRequestType = UT_WRITE_DEVICE; 1527 req.bRequest = UR_SET_FEATURE; 1528 USETW(req.wValue, sel); 1529 USETW(req.wIndex, 0); 1530 USETW(req.wLength, 0); 1531 return (usb2_do_request(udev, mtx, &req, 0)); 1532 } 1533