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