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 #ifdef USB_GLOBAL_INCLUDE_FILE 30 #include USB_GLOBAL_INCLUDE_FILE 31 #else 32 #include <sys/stdint.h> 33 #include <sys/stddef.h> 34 #include <sys/param.h> 35 #include <sys/queue.h> 36 #include <sys/types.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/module.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/condvar.h> 44 #include <sys/sysctl.h> 45 #include <sys/sx.h> 46 #include <sys/unistd.h> 47 #include <sys/callout.h> 48 #include <sys/malloc.h> 49 #include <sys/priv.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbdi.h> 53 #include <dev/usb/usbdi_util.h> 54 #include <dev/usb/usb_ioctl.h> 55 #include <dev/usb/usbhid.h> 56 57 #define USB_DEBUG_VAR usb_debug 58 59 #include <dev/usb/usb_core.h> 60 #include <dev/usb/usb_busdma.h> 61 #include <dev/usb/usb_request.h> 62 #include <dev/usb/usb_process.h> 63 #include <dev/usb/usb_transfer.h> 64 #include <dev/usb/usb_debug.h> 65 #include <dev/usb/usb_device.h> 66 #include <dev/usb/usb_util.h> 67 #include <dev/usb/usb_dynamic.h> 68 69 #include <dev/usb/usb_controller.h> 70 #include <dev/usb/usb_bus.h> 71 #include <sys/ctype.h> 72 #endif /* USB_GLOBAL_INCLUDE_FILE */ 73 74 static int usb_no_cs_fail; 75 76 SYSCTL_INT(_hw_usb, OID_AUTO, no_cs_fail, CTLFLAG_RW, 77 &usb_no_cs_fail, 0, "USB clear stall failures are ignored, if set"); 78 79 #ifdef USB_DEBUG 80 #ifdef USB_REQ_DEBUG 81 /* The following structures are used in connection to fault injection. */ 82 struct usb_ctrl_debug { 83 int bus_index; /* target bus */ 84 int dev_index; /* target address */ 85 int ds_fail; /* fail data stage */ 86 int ss_fail; /* fail status stage */ 87 int ds_delay; /* data stage delay in ms */ 88 int ss_delay; /* status stage delay in ms */ 89 int bmRequestType_value; 90 int bRequest_value; 91 }; 92 93 struct usb_ctrl_debug_bits { 94 uint16_t ds_delay; 95 uint16_t ss_delay; 96 uint8_t ds_fail:1; 97 uint8_t ss_fail:1; 98 uint8_t enabled:1; 99 }; 100 101 /* The default is to disable fault injection. */ 102 103 static struct usb_ctrl_debug usb_ctrl_debug = { 104 .bus_index = -1, 105 .dev_index = -1, 106 .bmRequestType_value = -1, 107 .bRequest_value = -1, 108 }; 109 110 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW, 111 &usb_ctrl_debug.bus_index, 0, "USB controller index to fail"); 112 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW, 113 &usb_ctrl_debug.dev_index, 0, "USB device address to fail"); 114 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW, 115 &usb_ctrl_debug.ds_fail, 0, "USB fail data stage"); 116 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW, 117 &usb_ctrl_debug.ss_fail, 0, "USB fail status stage"); 118 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW, 119 &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms"); 120 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW, 121 &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms"); 122 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW, 123 &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail"); 124 SYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW, 125 &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail"); 126 127 /*------------------------------------------------------------------------* 128 * usbd_get_debug_bits 129 * 130 * This function is only useful in USB host mode. 131 *------------------------------------------------------------------------*/ 132 static void 133 usbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req, 134 struct usb_ctrl_debug_bits *dbg) 135 { 136 int temp; 137 138 memset(dbg, 0, sizeof(*dbg)); 139 140 /* Compute data stage delay */ 141 142 temp = usb_ctrl_debug.ds_delay; 143 if (temp < 0) 144 temp = 0; 145 else if (temp > (16*1024)) 146 temp = (16*1024); 147 148 dbg->ds_delay = temp; 149 150 /* Compute status stage delay */ 151 152 temp = usb_ctrl_debug.ss_delay; 153 if (temp < 0) 154 temp = 0; 155 else if (temp > (16*1024)) 156 temp = (16*1024); 157 158 dbg->ss_delay = temp; 159 160 /* Check if this control request should be failed */ 161 162 if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index) 163 return; 164 165 if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index) 166 return; 167 168 temp = usb_ctrl_debug.bmRequestType_value; 169 170 if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255)) 171 return; 172 173 temp = usb_ctrl_debug.bRequest_value; 174 175 if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255)) 176 return; 177 178 temp = usb_ctrl_debug.ds_fail; 179 if (temp) 180 dbg->ds_fail = 1; 181 182 temp = usb_ctrl_debug.ss_fail; 183 if (temp) 184 dbg->ss_fail = 1; 185 186 dbg->enabled = 1; 187 } 188 #endif /* USB_REQ_DEBUG */ 189 #endif /* USB_DEBUG */ 190 191 /*------------------------------------------------------------------------* 192 * usbd_do_request_callback 193 * 194 * This function is the USB callback for generic USB Host control 195 * transfers. 196 *------------------------------------------------------------------------*/ 197 void 198 usbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error) 199 { 200 ; /* workaround for a bug in "indent" */ 201 202 DPRINTF("st=%u\n", USB_GET_STATE(xfer)); 203 204 switch (USB_GET_STATE(xfer)) { 205 case USB_ST_SETUP: 206 usbd_transfer_submit(xfer); 207 break; 208 default: 209 cv_signal(&xfer->xroot->udev->ctrlreq_cv); 210 break; 211 } 212 } 213 214 /*------------------------------------------------------------------------* 215 * usb_do_clear_stall_callback 216 * 217 * This function is the USB callback for generic clear stall requests. 218 *------------------------------------------------------------------------*/ 219 void 220 usb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 221 { 222 struct usb_device_request req; 223 struct usb_device *udev; 224 struct usb_endpoint *ep; 225 struct usb_endpoint *ep_end; 226 struct usb_endpoint *ep_first; 227 usb_stream_t x; 228 uint8_t to; 229 230 udev = xfer->xroot->udev; 231 232 USB_BUS_LOCK(udev->bus); 233 234 /* round robin endpoint clear stall */ 235 236 ep = udev->ep_curr; 237 ep_end = udev->endpoints + udev->endpoints_max; 238 ep_first = udev->endpoints; 239 to = udev->endpoints_max; 240 241 switch (USB_GET_STATE(xfer)) { 242 case USB_ST_TRANSFERRED: 243 tr_transferred: 244 /* reset error counter */ 245 udev->clear_stall_errors = 0; 246 247 if (ep == NULL) 248 goto tr_setup; /* device was unconfigured */ 249 if (ep->edesc && 250 ep->is_stalled) { 251 ep->toggle_next = 0; 252 ep->is_stalled = 0; 253 /* some hardware needs a callback to clear the data toggle */ 254 usbd_clear_stall_locked(udev, ep); 255 for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 256 /* start the current or next transfer, if any */ 257 usb_command_wrapper(&ep->endpoint_q[x], 258 ep->endpoint_q[x].curr); 259 } 260 } 261 ep++; 262 263 case USB_ST_SETUP: 264 tr_setup: 265 if (to == 0) 266 break; /* no endpoints - nothing to do */ 267 if ((ep < ep_first) || (ep >= ep_end)) 268 ep = ep_first; /* endpoint wrapped around */ 269 if (ep->edesc && 270 ep->is_stalled) { 271 272 /* setup a clear-stall packet */ 273 274 req.bmRequestType = UT_WRITE_ENDPOINT; 275 req.bRequest = UR_CLEAR_FEATURE; 276 USETW(req.wValue, UF_ENDPOINT_HALT); 277 req.wIndex[0] = ep->edesc->bEndpointAddress; 278 req.wIndex[1] = 0; 279 USETW(req.wLength, 0); 280 281 /* copy in the transfer */ 282 283 usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req)); 284 285 /* set length */ 286 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 287 xfer->nframes = 1; 288 USB_BUS_UNLOCK(udev->bus); 289 290 usbd_transfer_submit(xfer); 291 292 USB_BUS_LOCK(udev->bus); 293 break; 294 } 295 ep++; 296 to--; 297 goto tr_setup; 298 299 default: 300 if (error == USB_ERR_CANCELLED) 301 break; 302 303 DPRINTF("Clear stall failed.\n"); 304 305 /* 306 * Some VMs like VirtualBox always return failure on 307 * clear-stall which we sometimes should just ignore. 308 */ 309 if (usb_no_cs_fail) 310 goto tr_transferred; 311 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) 312 goto tr_setup; 313 314 if (error == USB_ERR_TIMEOUT) { 315 udev->clear_stall_errors = USB_CS_RESET_LIMIT; 316 DPRINTF("Trying to re-enumerate.\n"); 317 usbd_start_re_enumerate(udev); 318 } else { 319 udev->clear_stall_errors++; 320 if (udev->clear_stall_errors == USB_CS_RESET_LIMIT) { 321 DPRINTF("Trying to re-enumerate.\n"); 322 usbd_start_re_enumerate(udev); 323 } 324 } 325 goto tr_setup; 326 } 327 328 /* store current endpoint */ 329 udev->ep_curr = ep; 330 USB_BUS_UNLOCK(udev->bus); 331 } 332 333 static usb_handle_req_t * 334 usbd_get_hr_func(struct usb_device *udev) 335 { 336 /* figure out if there is a Handle Request function */ 337 if (udev->flags.usb_mode == USB_MODE_DEVICE) 338 return (usb_temp_get_desc_p); 339 else if (udev->parent_hub == NULL) 340 return (udev->bus->methods->roothub_exec); 341 else 342 return (NULL); 343 } 344 345 /*------------------------------------------------------------------------* 346 * usbd_do_request_flags and usbd_do_request 347 * 348 * Description of arguments passed to these functions: 349 * 350 * "udev" - this is the "usb_device" structure pointer on which the 351 * request should be performed. It is possible to call this function 352 * in both Host Side mode and Device Side mode. 353 * 354 * "mtx" - if this argument is non-NULL the mutex pointed to by it 355 * will get dropped and picked up during the execution of this 356 * function, hence this function sometimes needs to sleep. If this 357 * argument is NULL it has no effect. 358 * 359 * "req" - this argument must always be non-NULL and points to an 360 * 8-byte structure holding the USB request to be done. The USB 361 * request structure has a bit telling the direction of the USB 362 * request, if it is a read or a write. 363 * 364 * "data" - if the "wLength" part of the structure pointed to by "req" 365 * is non-zero this argument must point to a valid kernel buffer which 366 * can hold at least "wLength" bytes. If "wLength" is zero "data" can 367 * be NULL. 368 * 369 * "flags" - here is a list of valid flags: 370 * 371 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than 372 * specified 373 * 374 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed 375 * at a later point in time. This is tunable by the "hw.usb.ss_delay" 376 * sysctl. This flag is mostly useful for debugging. 377 * 378 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland 379 * pointer. 380 * 381 * "actlen" - if non-NULL the actual transfer length will be stored in 382 * the 16-bit unsigned integer pointed to by "actlen". This 383 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is 384 * used. 385 * 386 * "timeout" - gives the timeout for the control transfer in 387 * milliseconds. A "timeout" value less than 50 milliseconds is 388 * treated like a 50 millisecond timeout. A "timeout" value greater 389 * than 30 seconds is treated like a 30 second timeout. This USB stack 390 * does not allow control requests without a timeout. 391 * 392 * NOTE: This function is thread safe. All calls to 393 * "usbd_do_request_flags" will be serialised by the use of an 394 * internal "sx_lock". 395 * 396 * Returns: 397 * 0: Success 398 * Else: Failure 399 *------------------------------------------------------------------------*/ 400 usb_error_t 401 usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx, 402 struct usb_device_request *req, void *data, uint16_t flags, 403 uint16_t *actlen, usb_timeout_t timeout) 404 { 405 #ifdef USB_REQ_DEBUG 406 struct usb_ctrl_debug_bits dbg; 407 #endif 408 usb_handle_req_t *hr_func; 409 struct usb_xfer *xfer; 410 const void *desc; 411 int err = 0; 412 usb_ticks_t start_ticks; 413 usb_ticks_t delta_ticks; 414 usb_ticks_t max_ticks; 415 uint16_t length; 416 uint16_t temp; 417 uint16_t acttemp; 418 uint8_t enum_locked; 419 420 if (timeout < 50) { 421 /* timeout is too small */ 422 timeout = 50; 423 } 424 if (timeout > 30000) { 425 /* timeout is too big */ 426 timeout = 30000; 427 } 428 length = UGETW(req->wLength); 429 430 enum_locked = usbd_enum_is_locked(udev); 431 432 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x " 433 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n", 434 udev, req->bmRequestType, req->bRequest, 435 req->wValue[1], req->wValue[0], 436 req->wIndex[1], req->wIndex[0], 437 req->wLength[1], req->wLength[0]); 438 439 /* Check if the device is still alive */ 440 if (udev->state < USB_STATE_POWERED) { 441 DPRINTF("usb device has gone\n"); 442 return (USB_ERR_NOT_CONFIGURED); 443 } 444 445 /* 446 * Set "actlen" to a known value in case the caller does not 447 * check the return value: 448 */ 449 if (actlen) 450 *actlen = 0; 451 452 #if (USB_HAVE_USER_IO == 0) 453 if (flags & USB_USER_DATA_PTR) 454 return (USB_ERR_INVAL); 455 #endif 456 if ((mtx != NULL) && (mtx != &Giant)) { 457 mtx_unlock(mtx); 458 mtx_assert(mtx, MA_NOTOWNED); 459 } 460 461 /* 462 * We need to allow suspend and resume at this point, else the 463 * control transfer will timeout if the device is suspended! 464 */ 465 if (enum_locked) 466 usbd_sr_unlock(udev); 467 468 /* 469 * Grab the default sx-lock so that serialisation 470 * is achieved when multiple threads are involved: 471 */ 472 sx_xlock(&udev->ctrl_sx); 473 474 hr_func = usbd_get_hr_func(udev); 475 476 if (hr_func != NULL) { 477 DPRINTF("Handle Request function is set\n"); 478 479 desc = NULL; 480 temp = 0; 481 482 if (!(req->bmRequestType & UT_READ)) { 483 if (length != 0) { 484 DPRINTFN(1, "The handle request function " 485 "does not support writing data!\n"); 486 err = USB_ERR_INVAL; 487 goto done; 488 } 489 } 490 491 /* The root HUB code needs the BUS lock locked */ 492 493 USB_BUS_LOCK(udev->bus); 494 err = (hr_func) (udev, req, &desc, &temp); 495 USB_BUS_UNLOCK(udev->bus); 496 497 if (err) 498 goto done; 499 500 if (length > temp) { 501 if (!(flags & USB_SHORT_XFER_OK)) { 502 err = USB_ERR_SHORT_XFER; 503 goto done; 504 } 505 length = temp; 506 } 507 if (actlen) 508 *actlen = length; 509 510 if (length > 0) { 511 #if USB_HAVE_USER_IO 512 if (flags & USB_USER_DATA_PTR) { 513 if (copyout(desc, data, length)) { 514 err = USB_ERR_INVAL; 515 goto done; 516 } 517 } else 518 #endif 519 memcpy(data, desc, length); 520 } 521 goto done; /* success */ 522 } 523 524 /* 525 * Setup a new USB transfer or use the existing one, if any: 526 */ 527 usbd_ctrl_transfer_setup(udev); 528 529 xfer = udev->ctrl_xfer[0]; 530 if (xfer == NULL) { 531 /* most likely out of memory */ 532 err = USB_ERR_NOMEM; 533 goto done; 534 } 535 536 #ifdef USB_REQ_DEBUG 537 /* Get debug bits */ 538 usbd_get_debug_bits(udev, req, &dbg); 539 540 /* Check for fault injection */ 541 if (dbg.enabled) 542 flags |= USB_DELAY_STATUS_STAGE; 543 #endif 544 USB_XFER_LOCK(xfer); 545 546 if (flags & USB_DELAY_STATUS_STAGE) 547 xfer->flags.manual_status = 1; 548 else 549 xfer->flags.manual_status = 0; 550 551 if (flags & USB_SHORT_XFER_OK) 552 xfer->flags.short_xfer_ok = 1; 553 else 554 xfer->flags.short_xfer_ok = 0; 555 556 xfer->timeout = timeout; 557 558 start_ticks = ticks; 559 560 max_ticks = USB_MS_TO_TICKS(timeout); 561 562 usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req)); 563 564 usbd_xfer_set_frame_len(xfer, 0, sizeof(*req)); 565 566 while (1) { 567 temp = length; 568 if (temp > usbd_xfer_max_len(xfer)) { 569 temp = usbd_xfer_max_len(xfer); 570 } 571 #ifdef USB_REQ_DEBUG 572 if (xfer->flags.manual_status) { 573 if (usbd_xfer_frame_len(xfer, 0) != 0) { 574 /* Execute data stage separately */ 575 temp = 0; 576 } else if (temp > 0) { 577 if (dbg.ds_fail) { 578 err = USB_ERR_INVAL; 579 break; 580 } 581 if (dbg.ds_delay > 0) { 582 usb_pause_mtx( 583 xfer->xroot->xfer_mtx, 584 USB_MS_TO_TICKS(dbg.ds_delay)); 585 /* make sure we don't time out */ 586 start_ticks = ticks; 587 } 588 } 589 } 590 #endif 591 usbd_xfer_set_frame_len(xfer, 1, temp); 592 593 if (temp > 0) { 594 if (!(req->bmRequestType & UT_READ)) { 595 #if USB_HAVE_USER_IO 596 if (flags & USB_USER_DATA_PTR) { 597 USB_XFER_UNLOCK(xfer); 598 err = usbd_copy_in_user(xfer->frbuffers + 1, 599 0, data, temp); 600 USB_XFER_LOCK(xfer); 601 if (err) { 602 err = USB_ERR_INVAL; 603 break; 604 } 605 } else 606 #endif 607 usbd_copy_in(xfer->frbuffers + 1, 608 0, data, temp); 609 } 610 usbd_xfer_set_frames(xfer, 2); 611 } else { 612 if (usbd_xfer_frame_len(xfer, 0) == 0) { 613 if (xfer->flags.manual_status) { 614 #ifdef USB_REQ_DEBUG 615 if (dbg.ss_fail) { 616 err = USB_ERR_INVAL; 617 break; 618 } 619 if (dbg.ss_delay > 0) { 620 usb_pause_mtx( 621 xfer->xroot->xfer_mtx, 622 USB_MS_TO_TICKS(dbg.ss_delay)); 623 /* make sure we don't time out */ 624 start_ticks = ticks; 625 } 626 #endif 627 xfer->flags.manual_status = 0; 628 } else { 629 break; 630 } 631 } 632 usbd_xfer_set_frames(xfer, 1); 633 } 634 635 usbd_transfer_start(xfer); 636 637 while (usbd_transfer_pending(xfer)) { 638 cv_wait(&udev->ctrlreq_cv, 639 xfer->xroot->xfer_mtx); 640 } 641 642 err = xfer->error; 643 644 if (err) { 645 break; 646 } 647 648 /* get actual length of DATA stage */ 649 650 if (xfer->aframes < 2) { 651 acttemp = 0; 652 } else { 653 acttemp = usbd_xfer_frame_len(xfer, 1); 654 } 655 656 /* check for short packet */ 657 658 if (temp > acttemp) { 659 temp = acttemp; 660 length = temp; 661 } 662 if (temp > 0) { 663 if (req->bmRequestType & UT_READ) { 664 #if USB_HAVE_USER_IO 665 if (flags & USB_USER_DATA_PTR) { 666 USB_XFER_UNLOCK(xfer); 667 err = usbd_copy_out_user(xfer->frbuffers + 1, 668 0, data, temp); 669 USB_XFER_LOCK(xfer); 670 if (err) { 671 err = USB_ERR_INVAL; 672 break; 673 } 674 } else 675 #endif 676 usbd_copy_out(xfer->frbuffers + 1, 677 0, data, temp); 678 } 679 } 680 /* 681 * Clear "frlengths[0]" so that we don't send the setup 682 * packet again: 683 */ 684 usbd_xfer_set_frame_len(xfer, 0, 0); 685 686 /* update length and data pointer */ 687 length -= temp; 688 data = USB_ADD_BYTES(data, temp); 689 690 if (actlen) { 691 (*actlen) += temp; 692 } 693 /* check for timeout */ 694 695 delta_ticks = ticks - start_ticks; 696 if (delta_ticks > max_ticks) { 697 if (!err) { 698 err = USB_ERR_TIMEOUT; 699 } 700 } 701 if (err) { 702 break; 703 } 704 } 705 706 if (err) { 707 /* 708 * Make sure that the control endpoint is no longer 709 * blocked in case of a non-transfer related error: 710 */ 711 usbd_transfer_stop(xfer); 712 } 713 USB_XFER_UNLOCK(xfer); 714 715 done: 716 sx_xunlock(&udev->ctrl_sx); 717 718 if (enum_locked) 719 usbd_sr_lock(udev); 720 721 if ((mtx != NULL) && (mtx != &Giant)) 722 mtx_lock(mtx); 723 724 return ((usb_error_t)err); 725 } 726 727 /*------------------------------------------------------------------------* 728 * usbd_do_request_proc - factored out code 729 * 730 * This function is factored out code. It does basically the same like 731 * usbd_do_request_flags, except it will check the status of the 732 * passed process argument before doing the USB request. If the 733 * process is draining the USB_ERR_IOERROR code will be returned. It 734 * is assumed that the mutex associated with the process is locked 735 * when calling this function. 736 *------------------------------------------------------------------------*/ 737 usb_error_t 738 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc, 739 struct usb_device_request *req, void *data, uint16_t flags, 740 uint16_t *actlen, usb_timeout_t timeout) 741 { 742 usb_error_t err; 743 uint16_t len; 744 745 /* get request data length */ 746 len = UGETW(req->wLength); 747 748 /* check if the device is being detached */ 749 if (usb_proc_is_gone(pproc)) { 750 err = USB_ERR_IOERROR; 751 goto done; 752 } 753 754 /* forward the USB request */ 755 err = usbd_do_request_flags(udev, pproc->up_mtx, 756 req, data, flags, actlen, timeout); 757 758 done: 759 /* on failure we zero the data */ 760 /* on short packet we zero the unused data */ 761 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) { 762 if (err) 763 memset(data, 0, len); 764 else if (actlen && *actlen != len) 765 memset(((uint8_t *)data) + *actlen, 0, len - *actlen); 766 } 767 return (err); 768 } 769 770 /*------------------------------------------------------------------------* 771 * usbd_req_reset_port 772 * 773 * This function will instruct a USB HUB to perform a reset sequence 774 * on the specified port number. 775 * 776 * Returns: 777 * 0: Success. The USB device should now be at address zero. 778 * Else: Failure. No USB device is present and the USB port should be 779 * disabled. 780 *------------------------------------------------------------------------*/ 781 usb_error_t 782 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port) 783 { 784 struct usb_port_status ps; 785 usb_error_t err; 786 uint16_t n; 787 uint16_t status; 788 uint16_t change; 789 790 DPRINTF("\n"); 791 792 /* clear any leftover port reset changes first */ 793 usbd_req_clear_port_feature( 794 udev, mtx, port, UHF_C_PORT_RESET); 795 796 /* assert port reset on the given port */ 797 err = usbd_req_set_port_feature( 798 udev, mtx, port, UHF_PORT_RESET); 799 800 /* check for errors */ 801 if (err) 802 goto done; 803 n = 0; 804 while (1) { 805 /* wait for the device to recover from reset */ 806 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay)); 807 n += usb_port_reset_delay; 808 err = usbd_req_get_port_status(udev, mtx, &ps, port); 809 if (err) 810 goto done; 811 812 status = UGETW(ps.wPortStatus); 813 change = UGETW(ps.wPortChange); 814 815 /* if the device disappeared, just give up */ 816 if (!(status & UPS_CURRENT_CONNECT_STATUS)) 817 goto done; 818 819 /* check if reset is complete */ 820 if (change & UPS_C_PORT_RESET) 821 break; 822 823 /* 824 * Some Virtual Machines like VirtualBox 4.x fail to 825 * generate a port reset change event. Check if reset 826 * is no longer asserted. 827 */ 828 if (!(status & UPS_RESET)) 829 break; 830 831 /* check for timeout */ 832 if (n > 1000) { 833 n = 0; 834 break; 835 } 836 } 837 838 /* clear port reset first */ 839 err = usbd_req_clear_port_feature( 840 udev, mtx, port, UHF_C_PORT_RESET); 841 if (err) 842 goto done; 843 844 /* check for timeout */ 845 if (n == 0) { 846 err = USB_ERR_TIMEOUT; 847 goto done; 848 } 849 /* wait for the device to recover from reset */ 850 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery)); 851 852 done: 853 DPRINTFN(2, "port %d reset returning error=%s\n", 854 port, usbd_errstr(err)); 855 return (err); 856 } 857 858 /*------------------------------------------------------------------------* 859 * usbd_req_warm_reset_port 860 * 861 * This function will instruct an USB HUB to perform a warm reset 862 * sequence on the specified port number. This kind of reset is not 863 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted 864 * for SUPER-speed USB HUBs. 865 * 866 * Returns: 867 * 0: Success. The USB device should now be available again. 868 * Else: Failure. No USB device is present and the USB port should be 869 * disabled. 870 *------------------------------------------------------------------------*/ 871 usb_error_t 872 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx, 873 uint8_t port) 874 { 875 struct usb_port_status ps; 876 usb_error_t err; 877 uint16_t n; 878 uint16_t status; 879 uint16_t change; 880 881 DPRINTF("\n"); 882 883 err = usbd_req_get_port_status(udev, mtx, &ps, port); 884 if (err) 885 goto done; 886 887 status = UGETW(ps.wPortStatus); 888 889 switch (UPS_PORT_LINK_STATE_GET(status)) { 890 case UPS_PORT_LS_U3: 891 case UPS_PORT_LS_COMP_MODE: 892 case UPS_PORT_LS_LOOPBACK: 893 case UPS_PORT_LS_SS_INA: 894 break; 895 default: 896 DPRINTF("Wrong state for warm reset\n"); 897 return (0); 898 } 899 900 /* clear any leftover warm port reset changes first */ 901 usbd_req_clear_port_feature(udev, mtx, 902 port, UHF_C_BH_PORT_RESET); 903 904 /* set warm port reset */ 905 err = usbd_req_set_port_feature(udev, mtx, 906 port, UHF_BH_PORT_RESET); 907 if (err) 908 goto done; 909 910 n = 0; 911 while (1) { 912 /* wait for the device to recover from reset */ 913 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay)); 914 n += usb_port_reset_delay; 915 err = usbd_req_get_port_status(udev, mtx, &ps, port); 916 if (err) 917 goto done; 918 919 status = UGETW(ps.wPortStatus); 920 change = UGETW(ps.wPortChange); 921 922 /* if the device disappeared, just give up */ 923 if (!(status & UPS_CURRENT_CONNECT_STATUS)) 924 goto done; 925 926 /* check if reset is complete */ 927 if (change & UPS_C_BH_PORT_RESET) 928 break; 929 930 /* check for timeout */ 931 if (n > 1000) { 932 n = 0; 933 break; 934 } 935 } 936 937 /* clear port reset first */ 938 err = usbd_req_clear_port_feature( 939 udev, mtx, port, UHF_C_BH_PORT_RESET); 940 if (err) 941 goto done; 942 943 /* check for timeout */ 944 if (n == 0) { 945 err = USB_ERR_TIMEOUT; 946 goto done; 947 } 948 /* wait for the device to recover from reset */ 949 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery)); 950 951 done: 952 DPRINTFN(2, "port %d warm reset returning error=%s\n", 953 port, usbd_errstr(err)); 954 return (err); 955 } 956 957 /*------------------------------------------------------------------------* 958 * usbd_req_get_desc 959 * 960 * This function can be used to retrieve USB descriptors. It contains 961 * some additional logic like zeroing of missing descriptor bytes and 962 * retrying an USB descriptor in case of failure. The "min_len" 963 * argument specifies the minimum descriptor length. The "max_len" 964 * argument specifies the maximum descriptor length. If the real 965 * descriptor length is less than the minimum length the missing 966 * byte(s) will be zeroed. The type field, the second byte of the USB 967 * descriptor, will get forced to the correct type. If the "actlen" 968 * pointer is non-NULL, the actual length of the transfer will get 969 * stored in the 16-bit unsigned integer which it is pointing to. The 970 * first byte of the descriptor will not get updated. If the "actlen" 971 * pointer is NULL the first byte of the descriptor will get updated 972 * to reflect the actual length instead. If "min_len" is not equal to 973 * "max_len" then this function will try to retrive the beginning of 974 * the descriptor and base the maximum length on the first byte of the 975 * descriptor. 976 * 977 * Returns: 978 * 0: Success 979 * Else: Failure 980 *------------------------------------------------------------------------*/ 981 usb_error_t 982 usbd_req_get_desc(struct usb_device *udev, 983 struct mtx *mtx, uint16_t *actlen, void *desc, 984 uint16_t min_len, uint16_t max_len, 985 uint16_t id, uint8_t type, uint8_t index, 986 uint8_t retries) 987 { 988 struct usb_device_request req; 989 uint8_t *buf; 990 usb_error_t err; 991 992 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n", 993 id, type, index, max_len); 994 995 req.bmRequestType = UT_READ_DEVICE; 996 req.bRequest = UR_GET_DESCRIPTOR; 997 USETW2(req.wValue, type, index); 998 USETW(req.wIndex, id); 999 1000 while (1) { 1001 1002 if ((min_len < 2) || (max_len < 2)) { 1003 err = USB_ERR_INVAL; 1004 goto done; 1005 } 1006 USETW(req.wLength, min_len); 1007 1008 err = usbd_do_request_flags(udev, mtx, &req, 1009 desc, 0, NULL, 1000); 1010 1011 if (err) { 1012 if (!retries) { 1013 goto done; 1014 } 1015 retries--; 1016 1017 usb_pause_mtx(mtx, hz / 5); 1018 1019 continue; 1020 } 1021 buf = desc; 1022 1023 if (min_len == max_len) { 1024 1025 /* enforce correct length */ 1026 if ((buf[0] > min_len) && (actlen == NULL)) 1027 buf[0] = min_len; 1028 1029 /* enforce correct type */ 1030 buf[1] = type; 1031 1032 goto done; 1033 } 1034 /* range check */ 1035 1036 if (max_len > buf[0]) { 1037 max_len = buf[0]; 1038 } 1039 /* zero minimum data */ 1040 1041 while (min_len > max_len) { 1042 min_len--; 1043 buf[min_len] = 0; 1044 } 1045 1046 /* set new minimum length */ 1047 1048 min_len = max_len; 1049 } 1050 done: 1051 if (actlen != NULL) { 1052 if (err) 1053 *actlen = 0; 1054 else 1055 *actlen = min_len; 1056 } 1057 return (err); 1058 } 1059 1060 /*------------------------------------------------------------------------* 1061 * usbd_req_get_string_any 1062 * 1063 * This function will return the string given by "string_index" 1064 * using the first language ID. The maximum length "len" includes 1065 * the terminating zero. The "len" argument should be twice as 1066 * big pluss 2 bytes, compared with the actual maximum string length ! 1067 * 1068 * Returns: 1069 * 0: Success 1070 * Else: Failure 1071 *------------------------------------------------------------------------*/ 1072 usb_error_t 1073 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf, 1074 uint16_t len, uint8_t string_index) 1075 { 1076 char *s; 1077 uint8_t *temp; 1078 uint16_t i; 1079 uint16_t n; 1080 uint16_t c; 1081 uint8_t swap; 1082 usb_error_t err; 1083 1084 if (len == 0) { 1085 /* should not happen */ 1086 return (USB_ERR_NORMAL_COMPLETION); 1087 } 1088 if (string_index == 0) { 1089 /* this is the language table */ 1090 buf[0] = 0; 1091 return (USB_ERR_INVAL); 1092 } 1093 if (udev->flags.no_strings) { 1094 buf[0] = 0; 1095 return (USB_ERR_STALLED); 1096 } 1097 err = usbd_req_get_string_desc 1098 (udev, mtx, buf, len, udev->langid, string_index); 1099 if (err) { 1100 buf[0] = 0; 1101 return (err); 1102 } 1103 temp = (uint8_t *)buf; 1104 1105 if (temp[0] < 2) { 1106 /* string length is too short */ 1107 buf[0] = 0; 1108 return (USB_ERR_INVAL); 1109 } 1110 /* reserve one byte for terminating zero */ 1111 len--; 1112 1113 /* find maximum length */ 1114 s = buf; 1115 n = (temp[0] / 2) - 1; 1116 if (n > len) { 1117 n = len; 1118 } 1119 /* skip descriptor header */ 1120 temp += 2; 1121 1122 /* reset swap state */ 1123 swap = 3; 1124 1125 /* convert and filter */ 1126 for (i = 0; (i != n); i++) { 1127 c = UGETW(temp + (2 * i)); 1128 1129 /* convert from Unicode, handle buggy strings */ 1130 if (((c & 0xff00) == 0) && (swap & 1)) { 1131 /* Little Endian, default */ 1132 *s = c; 1133 swap = 1; 1134 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 1135 /* Big Endian */ 1136 *s = c >> 8; 1137 swap = 2; 1138 } else { 1139 /* silently skip bad character */ 1140 continue; 1141 } 1142 1143 /* 1144 * Filter by default - We only allow alphanumerical 1145 * and a few more to avoid any problems with scripts 1146 * and daemons. 1147 */ 1148 if (isalpha(*s) || 1149 isdigit(*s) || 1150 *s == '-' || 1151 *s == '+' || 1152 *s == ' ' || 1153 *s == '.' || 1154 *s == ',') { 1155 /* allowed */ 1156 s++; 1157 } 1158 /* silently skip bad character */ 1159 } 1160 *s = 0; /* zero terminate resulting string */ 1161 return (USB_ERR_NORMAL_COMPLETION); 1162 } 1163 1164 /*------------------------------------------------------------------------* 1165 * usbd_req_get_string_desc 1166 * 1167 * If you don't know the language ID, consider using 1168 * "usbd_req_get_string_any()". 1169 * 1170 * Returns: 1171 * 0: Success 1172 * Else: Failure 1173 *------------------------------------------------------------------------*/ 1174 usb_error_t 1175 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc, 1176 uint16_t max_len, uint16_t lang_id, 1177 uint8_t string_index) 1178 { 1179 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id, 1180 UDESC_STRING, string_index, 0)); 1181 } 1182 1183 /*------------------------------------------------------------------------* 1184 * usbd_req_get_config_desc_ptr 1185 * 1186 * This function is used in device side mode to retrieve the pointer 1187 * to the generated config descriptor. This saves allocating space for 1188 * an additional config descriptor when setting the configuration. 1189 * 1190 * Returns: 1191 * 0: Success 1192 * Else: Failure 1193 *------------------------------------------------------------------------*/ 1194 usb_error_t 1195 usbd_req_get_descriptor_ptr(struct usb_device *udev, 1196 struct usb_config_descriptor **ppcd, uint16_t wValue) 1197 { 1198 struct usb_device_request req; 1199 usb_handle_req_t *hr_func; 1200 const void *ptr; 1201 uint16_t len; 1202 usb_error_t err; 1203 1204 req.bmRequestType = UT_READ_DEVICE; 1205 req.bRequest = UR_GET_DESCRIPTOR; 1206 USETW(req.wValue, wValue); 1207 USETW(req.wIndex, 0); 1208 USETW(req.wLength, 0); 1209 1210 ptr = NULL; 1211 len = 0; 1212 1213 hr_func = usbd_get_hr_func(udev); 1214 1215 if (hr_func == NULL) 1216 err = USB_ERR_INVAL; 1217 else { 1218 USB_BUS_LOCK(udev->bus); 1219 err = (hr_func) (udev, &req, &ptr, &len); 1220 USB_BUS_UNLOCK(udev->bus); 1221 } 1222 1223 if (err) 1224 ptr = NULL; 1225 else if (ptr == NULL) 1226 err = USB_ERR_INVAL; 1227 1228 *ppcd = __DECONST(struct usb_config_descriptor *, ptr); 1229 1230 return (err); 1231 } 1232 1233 /*------------------------------------------------------------------------* 1234 * usbd_req_get_config_desc 1235 * 1236 * Returns: 1237 * 0: Success 1238 * Else: Failure 1239 *------------------------------------------------------------------------*/ 1240 usb_error_t 1241 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx, 1242 struct usb_config_descriptor *d, uint8_t conf_index) 1243 { 1244 usb_error_t err; 1245 1246 DPRINTFN(4, "confidx=%d\n", conf_index); 1247 1248 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 1249 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0); 1250 if (err) { 1251 goto done; 1252 } 1253 /* Extra sanity checking */ 1254 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) { 1255 err = USB_ERR_INVAL; 1256 } 1257 done: 1258 return (err); 1259 } 1260 1261 /*------------------------------------------------------------------------* 1262 * usbd_req_get_config_desc_full 1263 * 1264 * This function gets the complete USB configuration descriptor and 1265 * ensures that "wTotalLength" is correct. 1266 * 1267 * Returns: 1268 * 0: Success 1269 * Else: Failure 1270 *------------------------------------------------------------------------*/ 1271 usb_error_t 1272 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx, 1273 struct usb_config_descriptor **ppcd, struct malloc_type *mtype, 1274 uint8_t index) 1275 { 1276 struct usb_config_descriptor cd; 1277 struct usb_config_descriptor *cdesc; 1278 uint16_t len; 1279 usb_error_t err; 1280 1281 DPRINTFN(4, "index=%d\n", index); 1282 1283 *ppcd = NULL; 1284 1285 err = usbd_req_get_config_desc(udev, mtx, &cd, index); 1286 if (err) { 1287 return (err); 1288 } 1289 /* get full descriptor */ 1290 len = UGETW(cd.wTotalLength); 1291 if (len < sizeof(*cdesc)) { 1292 /* corrupt descriptor */ 1293 return (USB_ERR_INVAL); 1294 } 1295 cdesc = malloc(len, mtype, M_WAITOK); 1296 if (cdesc == NULL) { 1297 return (USB_ERR_NOMEM); 1298 } 1299 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0, 1300 UDESC_CONFIG, index, 3); 1301 if (err) { 1302 free(cdesc, mtype); 1303 return (err); 1304 } 1305 /* make sure that the device is not fooling us: */ 1306 USETW(cdesc->wTotalLength, len); 1307 1308 *ppcd = cdesc; 1309 1310 return (0); /* success */ 1311 } 1312 1313 /*------------------------------------------------------------------------* 1314 * usbd_req_get_device_desc 1315 * 1316 * Returns: 1317 * 0: Success 1318 * Else: Failure 1319 *------------------------------------------------------------------------*/ 1320 usb_error_t 1321 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx, 1322 struct usb_device_descriptor *d) 1323 { 1324 DPRINTFN(4, "\n"); 1325 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 1326 sizeof(*d), 0, UDESC_DEVICE, 0, 3)); 1327 } 1328 1329 /*------------------------------------------------------------------------* 1330 * usbd_req_get_alt_interface_no 1331 * 1332 * Returns: 1333 * 0: Success 1334 * Else: Failure 1335 *------------------------------------------------------------------------*/ 1336 usb_error_t 1337 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx, 1338 uint8_t *alt_iface_no, uint8_t iface_index) 1339 { 1340 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1341 struct usb_device_request req; 1342 1343 if ((iface == NULL) || (iface->idesc == NULL)) 1344 return (USB_ERR_INVAL); 1345 1346 req.bmRequestType = UT_READ_INTERFACE; 1347 req.bRequest = UR_GET_INTERFACE; 1348 USETW(req.wValue, 0); 1349 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1350 req.wIndex[1] = 0; 1351 USETW(req.wLength, 1); 1352 return (usbd_do_request(udev, mtx, &req, alt_iface_no)); 1353 } 1354 1355 /*------------------------------------------------------------------------* 1356 * usbd_req_set_alt_interface_no 1357 * 1358 * Returns: 1359 * 0: Success 1360 * Else: Failure 1361 *------------------------------------------------------------------------*/ 1362 usb_error_t 1363 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx, 1364 uint8_t iface_index, uint8_t alt_no) 1365 { 1366 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1367 struct usb_device_request req; 1368 1369 if ((iface == NULL) || (iface->idesc == NULL)) 1370 return (USB_ERR_INVAL); 1371 1372 req.bmRequestType = UT_WRITE_INTERFACE; 1373 req.bRequest = UR_SET_INTERFACE; 1374 req.wValue[0] = alt_no; 1375 req.wValue[1] = 0; 1376 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1377 req.wIndex[1] = 0; 1378 USETW(req.wLength, 0); 1379 return (usbd_do_request(udev, mtx, &req, 0)); 1380 } 1381 1382 /*------------------------------------------------------------------------* 1383 * usbd_req_get_device_status 1384 * 1385 * Returns: 1386 * 0: Success 1387 * Else: Failure 1388 *------------------------------------------------------------------------*/ 1389 usb_error_t 1390 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx, 1391 struct usb_status *st) 1392 { 1393 struct usb_device_request req; 1394 1395 req.bmRequestType = UT_READ_DEVICE; 1396 req.bRequest = UR_GET_STATUS; 1397 USETW(req.wValue, 0); 1398 USETW(req.wIndex, 0); 1399 USETW(req.wLength, sizeof(*st)); 1400 return (usbd_do_request(udev, mtx, &req, st)); 1401 } 1402 1403 /*------------------------------------------------------------------------* 1404 * usbd_req_get_hub_descriptor 1405 * 1406 * Returns: 1407 * 0: Success 1408 * Else: Failure 1409 *------------------------------------------------------------------------*/ 1410 usb_error_t 1411 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx, 1412 struct usb_hub_descriptor *hd, uint8_t nports) 1413 { 1414 struct usb_device_request req; 1415 uint16_t len = (nports + 7 + (8 * 8)) / 8; 1416 1417 req.bmRequestType = UT_READ_CLASS_DEVICE; 1418 req.bRequest = UR_GET_DESCRIPTOR; 1419 USETW2(req.wValue, UDESC_HUB, 0); 1420 USETW(req.wIndex, 0); 1421 USETW(req.wLength, len); 1422 return (usbd_do_request(udev, mtx, &req, hd)); 1423 } 1424 1425 /*------------------------------------------------------------------------* 1426 * usbd_req_get_ss_hub_descriptor 1427 * 1428 * Returns: 1429 * 0: Success 1430 * Else: Failure 1431 *------------------------------------------------------------------------*/ 1432 usb_error_t 1433 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx, 1434 struct usb_hub_ss_descriptor *hd, uint8_t nports) 1435 { 1436 struct usb_device_request req; 1437 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8); 1438 1439 req.bmRequestType = UT_READ_CLASS_DEVICE; 1440 req.bRequest = UR_GET_DESCRIPTOR; 1441 USETW2(req.wValue, UDESC_SS_HUB, 0); 1442 USETW(req.wIndex, 0); 1443 USETW(req.wLength, len); 1444 return (usbd_do_request(udev, mtx, &req, hd)); 1445 } 1446 1447 /*------------------------------------------------------------------------* 1448 * usbd_req_get_hub_status 1449 * 1450 * Returns: 1451 * 0: Success 1452 * Else: Failure 1453 *------------------------------------------------------------------------*/ 1454 usb_error_t 1455 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx, 1456 struct usb_hub_status *st) 1457 { 1458 struct usb_device_request req; 1459 1460 req.bmRequestType = UT_READ_CLASS_DEVICE; 1461 req.bRequest = UR_GET_STATUS; 1462 USETW(req.wValue, 0); 1463 USETW(req.wIndex, 0); 1464 USETW(req.wLength, sizeof(struct usb_hub_status)); 1465 return (usbd_do_request(udev, mtx, &req, st)); 1466 } 1467 1468 /*------------------------------------------------------------------------* 1469 * usbd_req_set_address 1470 * 1471 * This function is used to set the address for an USB device. After 1472 * port reset the USB device will respond at address zero. 1473 * 1474 * Returns: 1475 * 0: Success 1476 * Else: Failure 1477 *------------------------------------------------------------------------*/ 1478 usb_error_t 1479 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr) 1480 { 1481 struct usb_device_request req; 1482 usb_error_t err; 1483 1484 DPRINTFN(6, "setting device address=%d\n", addr); 1485 1486 req.bmRequestType = UT_WRITE_DEVICE; 1487 req.bRequest = UR_SET_ADDRESS; 1488 USETW(req.wValue, addr); 1489 USETW(req.wIndex, 0); 1490 USETW(req.wLength, 0); 1491 1492 err = USB_ERR_INVAL; 1493 1494 /* check if USB controller handles set address */ 1495 if (udev->bus->methods->set_address != NULL) 1496 err = (udev->bus->methods->set_address) (udev, mtx, addr); 1497 1498 if (err != USB_ERR_INVAL) 1499 goto done; 1500 1501 /* Setting the address should not take more than 1 second ! */ 1502 err = usbd_do_request_flags(udev, mtx, &req, NULL, 1503 USB_DELAY_STATUS_STAGE, NULL, 1000); 1504 1505 done: 1506 /* allow device time to set new address */ 1507 usb_pause_mtx(mtx, 1508 USB_MS_TO_TICKS(usb_set_address_settle)); 1509 1510 return (err); 1511 } 1512 1513 /*------------------------------------------------------------------------* 1514 * usbd_req_get_port_status 1515 * 1516 * Returns: 1517 * 0: Success 1518 * Else: Failure 1519 *------------------------------------------------------------------------*/ 1520 usb_error_t 1521 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx, 1522 struct usb_port_status *ps, uint8_t port) 1523 { 1524 struct usb_device_request req; 1525 1526 req.bmRequestType = UT_READ_CLASS_OTHER; 1527 req.bRequest = UR_GET_STATUS; 1528 USETW(req.wValue, 0); 1529 req.wIndex[0] = port; 1530 req.wIndex[1] = 0; 1531 USETW(req.wLength, sizeof *ps); 1532 return (usbd_do_request(udev, mtx, &req, ps)); 1533 } 1534 1535 /*------------------------------------------------------------------------* 1536 * usbd_req_clear_hub_feature 1537 * 1538 * Returns: 1539 * 0: Success 1540 * Else: Failure 1541 *------------------------------------------------------------------------*/ 1542 usb_error_t 1543 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx, 1544 uint16_t sel) 1545 { 1546 struct usb_device_request req; 1547 1548 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1549 req.bRequest = UR_CLEAR_FEATURE; 1550 USETW(req.wValue, sel); 1551 USETW(req.wIndex, 0); 1552 USETW(req.wLength, 0); 1553 return (usbd_do_request(udev, mtx, &req, 0)); 1554 } 1555 1556 /*------------------------------------------------------------------------* 1557 * usbd_req_set_hub_feature 1558 * 1559 * Returns: 1560 * 0: Success 1561 * Else: Failure 1562 *------------------------------------------------------------------------*/ 1563 usb_error_t 1564 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx, 1565 uint16_t sel) 1566 { 1567 struct usb_device_request req; 1568 1569 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1570 req.bRequest = UR_SET_FEATURE; 1571 USETW(req.wValue, sel); 1572 USETW(req.wIndex, 0); 1573 USETW(req.wLength, 0); 1574 return (usbd_do_request(udev, mtx, &req, 0)); 1575 } 1576 1577 /*------------------------------------------------------------------------* 1578 * usbd_req_set_hub_u1_timeout 1579 * 1580 * Returns: 1581 * 0: Success 1582 * Else: Failure 1583 *------------------------------------------------------------------------*/ 1584 usb_error_t 1585 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx, 1586 uint8_t port, uint8_t timeout) 1587 { 1588 struct usb_device_request req; 1589 1590 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1591 req.bRequest = UR_SET_FEATURE; 1592 USETW(req.wValue, UHF_PORT_U1_TIMEOUT); 1593 req.wIndex[0] = port; 1594 req.wIndex[1] = timeout; 1595 USETW(req.wLength, 0); 1596 return (usbd_do_request(udev, mtx, &req, 0)); 1597 } 1598 1599 /*------------------------------------------------------------------------* 1600 * usbd_req_set_hub_u2_timeout 1601 * 1602 * Returns: 1603 * 0: Success 1604 * Else: Failure 1605 *------------------------------------------------------------------------*/ 1606 usb_error_t 1607 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx, 1608 uint8_t port, uint8_t timeout) 1609 { 1610 struct usb_device_request req; 1611 1612 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1613 req.bRequest = UR_SET_FEATURE; 1614 USETW(req.wValue, UHF_PORT_U2_TIMEOUT); 1615 req.wIndex[0] = port; 1616 req.wIndex[1] = timeout; 1617 USETW(req.wLength, 0); 1618 return (usbd_do_request(udev, mtx, &req, 0)); 1619 } 1620 1621 /*------------------------------------------------------------------------* 1622 * usbd_req_set_hub_depth 1623 * 1624 * Returns: 1625 * 0: Success 1626 * Else: Failure 1627 *------------------------------------------------------------------------*/ 1628 usb_error_t 1629 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx, 1630 uint16_t depth) 1631 { 1632 struct usb_device_request req; 1633 1634 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1635 req.bRequest = UR_SET_HUB_DEPTH; 1636 USETW(req.wValue, depth); 1637 USETW(req.wIndex, 0); 1638 USETW(req.wLength, 0); 1639 return (usbd_do_request(udev, mtx, &req, 0)); 1640 } 1641 1642 /*------------------------------------------------------------------------* 1643 * usbd_req_clear_port_feature 1644 * 1645 * Returns: 1646 * 0: Success 1647 * Else: Failure 1648 *------------------------------------------------------------------------*/ 1649 usb_error_t 1650 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx, 1651 uint8_t port, uint16_t sel) 1652 { 1653 struct usb_device_request req; 1654 1655 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1656 req.bRequest = UR_CLEAR_FEATURE; 1657 USETW(req.wValue, sel); 1658 req.wIndex[0] = port; 1659 req.wIndex[1] = 0; 1660 USETW(req.wLength, 0); 1661 return (usbd_do_request(udev, mtx, &req, 0)); 1662 } 1663 1664 /*------------------------------------------------------------------------* 1665 * usbd_req_set_port_feature 1666 * 1667 * Returns: 1668 * 0: Success 1669 * Else: Failure 1670 *------------------------------------------------------------------------*/ 1671 usb_error_t 1672 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx, 1673 uint8_t port, uint16_t sel) 1674 { 1675 struct usb_device_request req; 1676 1677 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1678 req.bRequest = UR_SET_FEATURE; 1679 USETW(req.wValue, sel); 1680 req.wIndex[0] = port; 1681 req.wIndex[1] = 0; 1682 USETW(req.wLength, 0); 1683 return (usbd_do_request(udev, mtx, &req, 0)); 1684 } 1685 1686 /*------------------------------------------------------------------------* 1687 * usbd_req_set_protocol 1688 * 1689 * Returns: 1690 * 0: Success 1691 * Else: Failure 1692 *------------------------------------------------------------------------*/ 1693 usb_error_t 1694 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx, 1695 uint8_t iface_index, uint16_t report) 1696 { 1697 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1698 struct usb_device_request req; 1699 1700 if ((iface == NULL) || (iface->idesc == NULL)) { 1701 return (USB_ERR_INVAL); 1702 } 1703 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n", 1704 iface, report, iface->idesc->bInterfaceNumber); 1705 1706 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1707 req.bRequest = UR_SET_PROTOCOL; 1708 USETW(req.wValue, report); 1709 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1710 req.wIndex[1] = 0; 1711 USETW(req.wLength, 0); 1712 return (usbd_do_request(udev, mtx, &req, 0)); 1713 } 1714 1715 /*------------------------------------------------------------------------* 1716 * usbd_req_set_report 1717 * 1718 * Returns: 1719 * 0: Success 1720 * Else: Failure 1721 *------------------------------------------------------------------------*/ 1722 usb_error_t 1723 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len, 1724 uint8_t iface_index, uint8_t type, uint8_t id) 1725 { 1726 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1727 struct usb_device_request req; 1728 1729 if ((iface == NULL) || (iface->idesc == NULL)) { 1730 return (USB_ERR_INVAL); 1731 } 1732 DPRINTFN(5, "len=%d\n", len); 1733 1734 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1735 req.bRequest = UR_SET_REPORT; 1736 USETW2(req.wValue, type, id); 1737 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1738 req.wIndex[1] = 0; 1739 USETW(req.wLength, len); 1740 return (usbd_do_request(udev, mtx, &req, data)); 1741 } 1742 1743 /*------------------------------------------------------------------------* 1744 * usbd_req_get_report 1745 * 1746 * Returns: 1747 * 0: Success 1748 * Else: Failure 1749 *------------------------------------------------------------------------*/ 1750 usb_error_t 1751 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data, 1752 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id) 1753 { 1754 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1755 struct usb_device_request req; 1756 1757 if ((iface == NULL) || (iface->idesc == NULL)) { 1758 return (USB_ERR_INVAL); 1759 } 1760 DPRINTFN(5, "len=%d\n", len); 1761 1762 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1763 req.bRequest = UR_GET_REPORT; 1764 USETW2(req.wValue, type, id); 1765 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1766 req.wIndex[1] = 0; 1767 USETW(req.wLength, len); 1768 return (usbd_do_request(udev, mtx, &req, data)); 1769 } 1770 1771 /*------------------------------------------------------------------------* 1772 * usbd_req_set_idle 1773 * 1774 * Returns: 1775 * 0: Success 1776 * Else: Failure 1777 *------------------------------------------------------------------------*/ 1778 usb_error_t 1779 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx, 1780 uint8_t iface_index, uint8_t duration, uint8_t id) 1781 { 1782 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1783 struct usb_device_request req; 1784 1785 if ((iface == NULL) || (iface->idesc == NULL)) { 1786 return (USB_ERR_INVAL); 1787 } 1788 DPRINTFN(5, "%d %d\n", duration, id); 1789 1790 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1791 req.bRequest = UR_SET_IDLE; 1792 USETW2(req.wValue, duration, id); 1793 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1794 req.wIndex[1] = 0; 1795 USETW(req.wLength, 0); 1796 return (usbd_do_request(udev, mtx, &req, 0)); 1797 } 1798 1799 /*------------------------------------------------------------------------* 1800 * usbd_req_get_report_descriptor 1801 * 1802 * Returns: 1803 * 0: Success 1804 * Else: Failure 1805 *------------------------------------------------------------------------*/ 1806 usb_error_t 1807 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx, 1808 void *d, uint16_t size, uint8_t iface_index) 1809 { 1810 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1811 struct usb_device_request req; 1812 1813 if ((iface == NULL) || (iface->idesc == NULL)) { 1814 return (USB_ERR_INVAL); 1815 } 1816 req.bmRequestType = UT_READ_INTERFACE; 1817 req.bRequest = UR_GET_DESCRIPTOR; 1818 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 1819 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1820 req.wIndex[1] = 0; 1821 USETW(req.wLength, size); 1822 return (usbd_do_request(udev, mtx, &req, d)); 1823 } 1824 1825 /*------------------------------------------------------------------------* 1826 * usbd_req_set_config 1827 * 1828 * This function is used to select the current configuration number in 1829 * both USB device side mode and USB host side mode. When setting the 1830 * configuration the function of the interfaces can change. 1831 * 1832 * Returns: 1833 * 0: Success 1834 * Else: Failure 1835 *------------------------------------------------------------------------*/ 1836 usb_error_t 1837 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf) 1838 { 1839 struct usb_device_request req; 1840 1841 DPRINTF("setting config %d\n", conf); 1842 1843 /* do "set configuration" request */ 1844 1845 req.bmRequestType = UT_WRITE_DEVICE; 1846 req.bRequest = UR_SET_CONFIG; 1847 req.wValue[0] = conf; 1848 req.wValue[1] = 0; 1849 USETW(req.wIndex, 0); 1850 USETW(req.wLength, 0); 1851 return (usbd_do_request(udev, mtx, &req, 0)); 1852 } 1853 1854 /*------------------------------------------------------------------------* 1855 * usbd_req_get_config 1856 * 1857 * Returns: 1858 * 0: Success 1859 * Else: Failure 1860 *------------------------------------------------------------------------*/ 1861 usb_error_t 1862 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf) 1863 { 1864 struct usb_device_request req; 1865 1866 req.bmRequestType = UT_READ_DEVICE; 1867 req.bRequest = UR_GET_CONFIG; 1868 USETW(req.wValue, 0); 1869 USETW(req.wIndex, 0); 1870 USETW(req.wLength, 1); 1871 return (usbd_do_request(udev, mtx, &req, pconf)); 1872 } 1873 1874 /*------------------------------------------------------------------------* 1875 * usbd_setup_device_desc 1876 *------------------------------------------------------------------------*/ 1877 usb_error_t 1878 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx) 1879 { 1880 usb_error_t err; 1881 1882 /* 1883 * Get the first 8 bytes of the device descriptor ! 1884 * 1885 * NOTE: "usbd_do_request()" will check the device descriptor 1886 * next time we do a request to see if the maximum packet size 1887 * changed! The 8 first bytes of the device descriptor 1888 * contains the maximum packet size to use on control endpoint 1889 * 0. If this value is different from "USB_MAX_IPACKET" a new 1890 * USB control request will be setup! 1891 */ 1892 switch (udev->speed) { 1893 case USB_SPEED_FULL: 1894 case USB_SPEED_LOW: 1895 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc, 1896 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1897 if (err != 0) { 1898 DPRINTFN(0, "getting device descriptor " 1899 "at addr %d failed, %s\n", udev->address, 1900 usbd_errstr(err)); 1901 return (err); 1902 } 1903 break; 1904 default: 1905 DPRINTF("Minimum MaxPacketSize is large enough " 1906 "to hold the complete device descriptor\n"); 1907 break; 1908 } 1909 1910 /* get the full device descriptor */ 1911 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1912 1913 /* try one more time, if error */ 1914 if (err) 1915 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1916 1917 if (err) { 1918 DPRINTF("addr=%d, getting full desc failed\n", 1919 udev->address); 1920 return (err); 1921 } 1922 1923 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1924 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1925 udev->address, UGETW(udev->ddesc.bcdUSB), 1926 udev->ddesc.bDeviceClass, 1927 udev->ddesc.bDeviceSubClass, 1928 udev->ddesc.bDeviceProtocol, 1929 udev->ddesc.bMaxPacketSize, 1930 udev->ddesc.bLength, 1931 udev->speed); 1932 1933 return (err); 1934 } 1935 1936 /*------------------------------------------------------------------------* 1937 * usbd_req_re_enumerate 1938 * 1939 * NOTE: After this function returns the hardware is in the 1940 * unconfigured state! The application is responsible for setting a 1941 * new configuration. 1942 * 1943 * Returns: 1944 * 0: Success 1945 * Else: Failure 1946 *------------------------------------------------------------------------*/ 1947 usb_error_t 1948 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx) 1949 { 1950 struct usb_device *parent_hub; 1951 usb_error_t err; 1952 uint8_t old_addr; 1953 uint8_t do_retry = 1; 1954 1955 if (udev->flags.usb_mode != USB_MODE_HOST) { 1956 return (USB_ERR_INVAL); 1957 } 1958 old_addr = udev->address; 1959 parent_hub = udev->parent_hub; 1960 if (parent_hub == NULL) { 1961 return (USB_ERR_INVAL); 1962 } 1963 retry: 1964 /* 1965 * Try to reset the High Speed parent HUB of a LOW- or FULL- 1966 * speed device, if any. 1967 */ 1968 if (udev->parent_hs_hub != NULL && 1969 udev->speed != USB_SPEED_HIGH) { 1970 DPRINTF("Trying to reset parent High Speed TT.\n"); 1971 err = usbd_req_reset_tt(udev->parent_hs_hub, NULL, 1972 udev->hs_port_no); 1973 if (err) { 1974 DPRINTF("Resetting parent High " 1975 "Speed TT failed (%s).\n", 1976 usbd_errstr(err)); 1977 } 1978 } 1979 1980 /* Try to warm reset first */ 1981 if (parent_hub->speed == USB_SPEED_SUPER) 1982 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no); 1983 1984 /* Try to reset the parent HUB port. */ 1985 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no); 1986 if (err) { 1987 DPRINTFN(0, "addr=%d, port reset failed, %s\n", 1988 old_addr, usbd_errstr(err)); 1989 goto done; 1990 } 1991 1992 /* 1993 * After that the port has been reset our device should be at 1994 * address zero: 1995 */ 1996 udev->address = USB_START_ADDR; 1997 1998 /* reset "bMaxPacketSize" */ 1999 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 2000 2001 /* reset USB state */ 2002 usb_set_device_state(udev, USB_STATE_POWERED); 2003 2004 /* 2005 * Restore device address: 2006 */ 2007 err = usbd_req_set_address(udev, mtx, old_addr); 2008 if (err) { 2009 /* XXX ignore any errors! */ 2010 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n", 2011 old_addr, usbd_errstr(err)); 2012 } 2013 /* 2014 * Restore device address, if the controller driver did not 2015 * set a new one: 2016 */ 2017 if (udev->address == USB_START_ADDR) 2018 udev->address = old_addr; 2019 2020 /* setup the device descriptor and the initial "wMaxPacketSize" */ 2021 err = usbd_setup_device_desc(udev, mtx); 2022 2023 done: 2024 if (err && do_retry) { 2025 /* give the USB firmware some time to load */ 2026 usb_pause_mtx(mtx, hz / 2); 2027 /* no more retries after this retry */ 2028 do_retry = 0; 2029 /* try again */ 2030 goto retry; 2031 } 2032 /* restore address */ 2033 if (udev->address == USB_START_ADDR) 2034 udev->address = old_addr; 2035 /* update state, if successful */ 2036 if (err == 0) 2037 usb_set_device_state(udev, USB_STATE_ADDRESSED); 2038 return (err); 2039 } 2040 2041 /*------------------------------------------------------------------------* 2042 * usbd_req_clear_device_feature 2043 * 2044 * Returns: 2045 * 0: Success 2046 * Else: Failure 2047 *------------------------------------------------------------------------*/ 2048 usb_error_t 2049 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx, 2050 uint16_t sel) 2051 { 2052 struct usb_device_request req; 2053 2054 req.bmRequestType = UT_WRITE_DEVICE; 2055 req.bRequest = UR_CLEAR_FEATURE; 2056 USETW(req.wValue, sel); 2057 USETW(req.wIndex, 0); 2058 USETW(req.wLength, 0); 2059 return (usbd_do_request(udev, mtx, &req, 0)); 2060 } 2061 2062 /*------------------------------------------------------------------------* 2063 * usbd_req_set_device_feature 2064 * 2065 * Returns: 2066 * 0: Success 2067 * Else: Failure 2068 *------------------------------------------------------------------------*/ 2069 usb_error_t 2070 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx, 2071 uint16_t sel) 2072 { 2073 struct usb_device_request req; 2074 2075 req.bmRequestType = UT_WRITE_DEVICE; 2076 req.bRequest = UR_SET_FEATURE; 2077 USETW(req.wValue, sel); 2078 USETW(req.wIndex, 0); 2079 USETW(req.wLength, 0); 2080 return (usbd_do_request(udev, mtx, &req, 0)); 2081 } 2082 2083 /*------------------------------------------------------------------------* 2084 * usbd_req_reset_tt 2085 * 2086 * Returns: 2087 * 0: Success 2088 * Else: Failure 2089 *------------------------------------------------------------------------*/ 2090 usb_error_t 2091 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx, 2092 uint8_t port) 2093 { 2094 struct usb_device_request req; 2095 2096 /* For single TT HUBs the port should be 1 */ 2097 2098 if (udev->ddesc.bDeviceClass == UDCLASS_HUB && 2099 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT) 2100 port = 1; 2101 2102 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2103 req.bRequest = UR_RESET_TT; 2104 USETW(req.wValue, 0); 2105 req.wIndex[0] = port; 2106 req.wIndex[1] = 0; 2107 USETW(req.wLength, 0); 2108 return (usbd_do_request(udev, mtx, &req, 0)); 2109 } 2110 2111 /*------------------------------------------------------------------------* 2112 * usbd_req_clear_tt_buffer 2113 * 2114 * For single TT HUBs the port should be 1. 2115 * 2116 * Returns: 2117 * 0: Success 2118 * Else: Failure 2119 *------------------------------------------------------------------------*/ 2120 usb_error_t 2121 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx, 2122 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint) 2123 { 2124 struct usb_device_request req; 2125 uint16_t wValue; 2126 2127 /* For single TT HUBs the port should be 1 */ 2128 2129 if (udev->ddesc.bDeviceClass == UDCLASS_HUB && 2130 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT) 2131 port = 1; 2132 2133 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) | 2134 ((endpoint & 0x80) << 8) | ((type & 3) << 12); 2135 2136 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2137 req.bRequest = UR_CLEAR_TT_BUFFER; 2138 USETW(req.wValue, wValue); 2139 req.wIndex[0] = port; 2140 req.wIndex[1] = 0; 2141 USETW(req.wLength, 0); 2142 return (usbd_do_request(udev, mtx, &req, 0)); 2143 } 2144 2145 /*------------------------------------------------------------------------* 2146 * usbd_req_set_port_link_state 2147 * 2148 * USB 3.0 specific request 2149 * 2150 * Returns: 2151 * 0: Success 2152 * Else: Failure 2153 *------------------------------------------------------------------------*/ 2154 usb_error_t 2155 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx, 2156 uint8_t port, uint8_t link_state) 2157 { 2158 struct usb_device_request req; 2159 2160 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2161 req.bRequest = UR_SET_FEATURE; 2162 USETW(req.wValue, UHF_PORT_LINK_STATE); 2163 req.wIndex[0] = port; 2164 req.wIndex[1] = link_state; 2165 USETW(req.wLength, 0); 2166 return (usbd_do_request(udev, mtx, &req, 0)); 2167 } 2168 2169 /*------------------------------------------------------------------------* 2170 * usbd_req_set_lpm_info 2171 * 2172 * USB 2.0 specific request for Link Power Management. 2173 * 2174 * Returns: 2175 * 0: Success 2176 * USB_ERR_PENDING_REQUESTS: NYET 2177 * USB_ERR_TIMEOUT: TIMEOUT 2178 * USB_ERR_STALL: STALL 2179 * Else: Failure 2180 *------------------------------------------------------------------------*/ 2181 usb_error_t 2182 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx, 2183 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe) 2184 { 2185 struct usb_device_request req; 2186 usb_error_t err; 2187 uint8_t buf[1]; 2188 2189 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2190 req.bRequest = UR_SET_AND_TEST; 2191 USETW(req.wValue, UHF_PORT_L1); 2192 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4); 2193 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00); 2194 USETW(req.wLength, sizeof(buf)); 2195 2196 /* set default value in case of short transfer */ 2197 buf[0] = 0x00; 2198 2199 err = usbd_do_request(udev, mtx, &req, buf); 2200 if (err) 2201 return (err); 2202 2203 switch (buf[0]) { 2204 case 0x00: /* SUCCESS */ 2205 break; 2206 case 0x10: /* NYET */ 2207 err = USB_ERR_PENDING_REQUESTS; 2208 break; 2209 case 0x11: /* TIMEOUT */ 2210 err = USB_ERR_TIMEOUT; 2211 break; 2212 case 0x30: /* STALL */ 2213 err = USB_ERR_STALLED; 2214 break; 2215 default: /* reserved */ 2216 err = USB_ERR_IOERROR; 2217 break; 2218 } 2219 return (err); 2220 } 2221 2222