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