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("I/O error - waiting a bit for TT cleanup\n"); 725 usb_pause_mtx(mtx, hz / 16); 726 break; 727 } 728 return ((usb_error_t)err); 729 } 730 731 /*------------------------------------------------------------------------* 732 * usbd_do_request_proc - factored out code 733 * 734 * This function is factored out code. It does basically the same like 735 * usbd_do_request_flags, except it will check the status of the 736 * passed process argument before doing the USB request. If the 737 * process is draining the USB_ERR_IOERROR code will be returned. It 738 * is assumed that the mutex associated with the process is locked 739 * when calling this function. 740 *------------------------------------------------------------------------*/ 741 usb_error_t 742 usbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc, 743 struct usb_device_request *req, void *data, uint16_t flags, 744 uint16_t *actlen, usb_timeout_t timeout) 745 { 746 usb_error_t err; 747 uint16_t len; 748 749 /* get request data length */ 750 len = UGETW(req->wLength); 751 752 /* check if the device is being detached */ 753 if (usb_proc_is_gone(pproc)) { 754 err = USB_ERR_IOERROR; 755 goto done; 756 } 757 758 /* forward the USB request */ 759 err = usbd_do_request_flags(udev, pproc->up_mtx, 760 req, data, flags, actlen, timeout); 761 762 done: 763 /* on failure we zero the data */ 764 /* on short packet we zero the unused data */ 765 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) { 766 if (err) 767 memset(data, 0, len); 768 else if (actlen && *actlen != len) 769 memset(((uint8_t *)data) + *actlen, 0, len - *actlen); 770 } 771 return (err); 772 } 773 774 /*------------------------------------------------------------------------* 775 * usbd_req_reset_port 776 * 777 * This function will instruct a USB HUB to perform a reset sequence 778 * on the specified port number. 779 * 780 * Returns: 781 * 0: Success. The USB device should now be at address zero. 782 * Else: Failure. No USB device is present and the USB port should be 783 * disabled. 784 *------------------------------------------------------------------------*/ 785 usb_error_t 786 usbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port) 787 { 788 struct usb_port_status ps; 789 usb_error_t err; 790 uint16_t n; 791 uint16_t status; 792 uint16_t change; 793 794 DPRINTF("\n"); 795 796 /* clear any leftover port reset changes first */ 797 usbd_req_clear_port_feature( 798 udev, mtx, port, UHF_C_PORT_RESET); 799 800 /* assert port reset on the given port */ 801 err = usbd_req_set_port_feature( 802 udev, mtx, port, UHF_PORT_RESET); 803 804 /* check for errors */ 805 if (err) 806 goto done; 807 n = 0; 808 while (1) { 809 /* wait for the device to recover from reset */ 810 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay)); 811 n += usb_port_reset_delay; 812 err = usbd_req_get_port_status(udev, mtx, &ps, port); 813 if (err) 814 goto done; 815 816 status = UGETW(ps.wPortStatus); 817 change = UGETW(ps.wPortChange); 818 819 /* if the device disappeared, just give up */ 820 if (!(status & UPS_CURRENT_CONNECT_STATUS)) 821 goto done; 822 823 /* check if reset is complete */ 824 if (change & UPS_C_PORT_RESET) 825 break; 826 827 /* 828 * Some Virtual Machines like VirtualBox 4.x fail to 829 * generate a port reset change event. Check if reset 830 * is no longer asserted. 831 */ 832 if (!(status & UPS_RESET)) 833 break; 834 835 /* check for timeout */ 836 if (n > 1000) { 837 n = 0; 838 break; 839 } 840 } 841 842 /* clear port reset first */ 843 err = usbd_req_clear_port_feature( 844 udev, mtx, port, UHF_C_PORT_RESET); 845 if (err) 846 goto done; 847 848 /* check for timeout */ 849 if (n == 0) { 850 err = USB_ERR_TIMEOUT; 851 goto done; 852 } 853 /* wait for the device to recover from reset */ 854 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery)); 855 856 done: 857 DPRINTFN(2, "port %d reset returning error=%s\n", 858 port, usbd_errstr(err)); 859 return (err); 860 } 861 862 /*------------------------------------------------------------------------* 863 * usbd_req_warm_reset_port 864 * 865 * This function will instruct an USB HUB to perform a warm reset 866 * sequence on the specified port number. This kind of reset is not 867 * mandatory for LOW-, FULL- and HIGH-speed USB HUBs and is targeted 868 * for SUPER-speed USB HUBs. 869 * 870 * Returns: 871 * 0: Success. The USB device should now be available again. 872 * Else: Failure. No USB device is present and the USB port should be 873 * disabled. 874 *------------------------------------------------------------------------*/ 875 usb_error_t 876 usbd_req_warm_reset_port(struct usb_device *udev, struct mtx *mtx, 877 uint8_t port) 878 { 879 struct usb_port_status ps; 880 usb_error_t err; 881 uint16_t n; 882 uint16_t status; 883 uint16_t change; 884 885 DPRINTF("\n"); 886 887 err = usbd_req_get_port_status(udev, mtx, &ps, port); 888 if (err) 889 goto done; 890 891 status = UGETW(ps.wPortStatus); 892 893 switch (UPS_PORT_LINK_STATE_GET(status)) { 894 case UPS_PORT_LS_U3: 895 case UPS_PORT_LS_COMP_MODE: 896 case UPS_PORT_LS_LOOPBACK: 897 case UPS_PORT_LS_SS_INA: 898 break; 899 default: 900 DPRINTF("Wrong state for warm reset\n"); 901 return (0); 902 } 903 904 /* clear any leftover warm port reset changes first */ 905 usbd_req_clear_port_feature(udev, mtx, 906 port, UHF_C_BH_PORT_RESET); 907 908 /* set warm port reset */ 909 err = usbd_req_set_port_feature(udev, mtx, 910 port, UHF_BH_PORT_RESET); 911 if (err) 912 goto done; 913 914 n = 0; 915 while (1) { 916 /* wait for the device to recover from reset */ 917 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_delay)); 918 n += usb_port_reset_delay; 919 err = usbd_req_get_port_status(udev, mtx, &ps, port); 920 if (err) 921 goto done; 922 923 status = UGETW(ps.wPortStatus); 924 change = UGETW(ps.wPortChange); 925 926 /* if the device disappeared, just give up */ 927 if (!(status & UPS_CURRENT_CONNECT_STATUS)) 928 goto done; 929 930 /* check if reset is complete */ 931 if (change & UPS_C_BH_PORT_RESET) 932 break; 933 934 /* check for timeout */ 935 if (n > 1000) { 936 n = 0; 937 break; 938 } 939 } 940 941 /* clear port reset first */ 942 err = usbd_req_clear_port_feature( 943 udev, mtx, port, UHF_C_BH_PORT_RESET); 944 if (err) 945 goto done; 946 947 /* check for timeout */ 948 if (n == 0) { 949 err = USB_ERR_TIMEOUT; 950 goto done; 951 } 952 /* wait for the device to recover from reset */ 953 usb_pause_mtx(mtx, USB_MS_TO_TICKS(usb_port_reset_recovery)); 954 955 done: 956 DPRINTFN(2, "port %d warm reset returning error=%s\n", 957 port, usbd_errstr(err)); 958 return (err); 959 } 960 961 /*------------------------------------------------------------------------* 962 * usbd_req_get_desc 963 * 964 * This function can be used to retrieve USB descriptors. It contains 965 * some additional logic like zeroing of missing descriptor bytes and 966 * retrying an USB descriptor in case of failure. The "min_len" 967 * argument specifies the minimum descriptor length. The "max_len" 968 * argument specifies the maximum descriptor length. If the real 969 * descriptor length is less than the minimum length the missing 970 * byte(s) will be zeroed. The type field, the second byte of the USB 971 * descriptor, will get forced to the correct type. If the "actlen" 972 * pointer is non-NULL, the actual length of the transfer will get 973 * stored in the 16-bit unsigned integer which it is pointing to. The 974 * first byte of the descriptor will not get updated. If the "actlen" 975 * pointer is NULL the first byte of the descriptor will get updated 976 * to reflect the actual length instead. If "min_len" is not equal to 977 * "max_len" then this function will try to retrive the beginning of 978 * the descriptor and base the maximum length on the first byte of the 979 * descriptor. 980 * 981 * Returns: 982 * 0: Success 983 * Else: Failure 984 *------------------------------------------------------------------------*/ 985 usb_error_t 986 usbd_req_get_desc(struct usb_device *udev, 987 struct mtx *mtx, uint16_t *actlen, void *desc, 988 uint16_t min_len, uint16_t max_len, 989 uint16_t id, uint8_t type, uint8_t index, 990 uint8_t retries) 991 { 992 struct usb_device_request req; 993 uint8_t *buf; 994 usb_error_t err; 995 996 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n", 997 id, type, index, max_len); 998 999 req.bmRequestType = UT_READ_DEVICE; 1000 req.bRequest = UR_GET_DESCRIPTOR; 1001 USETW2(req.wValue, type, index); 1002 USETW(req.wIndex, id); 1003 1004 while (1) { 1005 1006 if ((min_len < 2) || (max_len < 2)) { 1007 err = USB_ERR_INVAL; 1008 goto done; 1009 } 1010 USETW(req.wLength, min_len); 1011 1012 err = usbd_do_request_flags(udev, mtx, &req, 1013 desc, 0, NULL, 500 /* ms */); 1014 1015 if (err) { 1016 if (!retries) { 1017 goto done; 1018 } 1019 retries--; 1020 1021 usb_pause_mtx(mtx, hz / 5); 1022 1023 continue; 1024 } 1025 buf = desc; 1026 1027 if (min_len == max_len) { 1028 1029 /* enforce correct length */ 1030 if ((buf[0] > min_len) && (actlen == NULL)) 1031 buf[0] = min_len; 1032 1033 /* enforce correct type */ 1034 buf[1] = type; 1035 1036 goto done; 1037 } 1038 /* range check */ 1039 1040 if (max_len > buf[0]) { 1041 max_len = buf[0]; 1042 } 1043 /* zero minimum data */ 1044 1045 while (min_len > max_len) { 1046 min_len--; 1047 buf[min_len] = 0; 1048 } 1049 1050 /* set new minimum length */ 1051 1052 min_len = max_len; 1053 } 1054 done: 1055 if (actlen != NULL) { 1056 if (err) 1057 *actlen = 0; 1058 else 1059 *actlen = min_len; 1060 } 1061 return (err); 1062 } 1063 1064 /*------------------------------------------------------------------------* 1065 * usbd_req_get_string_any 1066 * 1067 * This function will return the string given by "string_index" 1068 * using the first language ID. The maximum length "len" includes 1069 * the terminating zero. The "len" argument should be twice as 1070 * big pluss 2 bytes, compared with the actual maximum string length ! 1071 * 1072 * Returns: 1073 * 0: Success 1074 * Else: Failure 1075 *------------------------------------------------------------------------*/ 1076 usb_error_t 1077 usbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf, 1078 uint16_t len, uint8_t string_index) 1079 { 1080 char *s; 1081 uint8_t *temp; 1082 uint16_t i; 1083 uint16_t n; 1084 uint16_t c; 1085 uint8_t swap; 1086 usb_error_t err; 1087 1088 if (len == 0) { 1089 /* should not happen */ 1090 return (USB_ERR_NORMAL_COMPLETION); 1091 } 1092 if (string_index == 0) { 1093 /* this is the language table */ 1094 buf[0] = 0; 1095 return (USB_ERR_INVAL); 1096 } 1097 if (udev->flags.no_strings) { 1098 buf[0] = 0; 1099 return (USB_ERR_STALLED); 1100 } 1101 err = usbd_req_get_string_desc 1102 (udev, mtx, buf, len, udev->langid, string_index); 1103 if (err) { 1104 buf[0] = 0; 1105 return (err); 1106 } 1107 temp = (uint8_t *)buf; 1108 1109 if (temp[0] < 2) { 1110 /* string length is too short */ 1111 buf[0] = 0; 1112 return (USB_ERR_INVAL); 1113 } 1114 /* reserve one byte for terminating zero */ 1115 len--; 1116 1117 /* find maximum length */ 1118 s = buf; 1119 n = (temp[0] / 2) - 1; 1120 if (n > len) { 1121 n = len; 1122 } 1123 /* skip descriptor header */ 1124 temp += 2; 1125 1126 /* reset swap state */ 1127 swap = 3; 1128 1129 /* convert and filter */ 1130 for (i = 0; (i != n); i++) { 1131 c = UGETW(temp + (2 * i)); 1132 1133 /* convert from Unicode, handle buggy strings */ 1134 if (((c & 0xff00) == 0) && (swap & 1)) { 1135 /* Little Endian, default */ 1136 *s = c; 1137 swap = 1; 1138 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 1139 /* Big Endian */ 1140 *s = c >> 8; 1141 swap = 2; 1142 } else { 1143 /* silently skip bad character */ 1144 continue; 1145 } 1146 1147 /* 1148 * Filter by default - We only allow alphanumerical 1149 * and a few more to avoid any problems with scripts 1150 * and daemons. 1151 */ 1152 if (isalpha(*s) || 1153 isdigit(*s) || 1154 *s == '-' || 1155 *s == '+' || 1156 *s == ' ' || 1157 *s == '.' || 1158 *s == ',') { 1159 /* allowed */ 1160 s++; 1161 } 1162 /* silently skip bad character */ 1163 } 1164 *s = 0; /* zero terminate resulting string */ 1165 return (USB_ERR_NORMAL_COMPLETION); 1166 } 1167 1168 /*------------------------------------------------------------------------* 1169 * usbd_req_get_string_desc 1170 * 1171 * If you don't know the language ID, consider using 1172 * "usbd_req_get_string_any()". 1173 * 1174 * Returns: 1175 * 0: Success 1176 * Else: Failure 1177 *------------------------------------------------------------------------*/ 1178 usb_error_t 1179 usbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc, 1180 uint16_t max_len, uint16_t lang_id, 1181 uint8_t string_index) 1182 { 1183 return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id, 1184 UDESC_STRING, string_index, 0)); 1185 } 1186 1187 /*------------------------------------------------------------------------* 1188 * usbd_req_get_config_desc_ptr 1189 * 1190 * This function is used in device side mode to retrieve the pointer 1191 * to the generated config descriptor. This saves allocating space for 1192 * an additional config descriptor when setting the configuration. 1193 * 1194 * Returns: 1195 * 0: Success 1196 * Else: Failure 1197 *------------------------------------------------------------------------*/ 1198 usb_error_t 1199 usbd_req_get_descriptor_ptr(struct usb_device *udev, 1200 struct usb_config_descriptor **ppcd, uint16_t wValue) 1201 { 1202 struct usb_device_request req; 1203 usb_handle_req_t *hr_func; 1204 const void *ptr; 1205 uint16_t len; 1206 usb_error_t err; 1207 1208 req.bmRequestType = UT_READ_DEVICE; 1209 req.bRequest = UR_GET_DESCRIPTOR; 1210 USETW(req.wValue, wValue); 1211 USETW(req.wIndex, 0); 1212 USETW(req.wLength, 0); 1213 1214 ptr = NULL; 1215 len = 0; 1216 1217 hr_func = usbd_get_hr_func(udev); 1218 1219 if (hr_func == NULL) 1220 err = USB_ERR_INVAL; 1221 else { 1222 USB_BUS_LOCK(udev->bus); 1223 err = (hr_func) (udev, &req, &ptr, &len); 1224 USB_BUS_UNLOCK(udev->bus); 1225 } 1226 1227 if (err) 1228 ptr = NULL; 1229 else if (ptr == NULL) 1230 err = USB_ERR_INVAL; 1231 1232 *ppcd = __DECONST(struct usb_config_descriptor *, ptr); 1233 1234 return (err); 1235 } 1236 1237 /*------------------------------------------------------------------------* 1238 * usbd_req_get_config_desc 1239 * 1240 * Returns: 1241 * 0: Success 1242 * Else: Failure 1243 *------------------------------------------------------------------------*/ 1244 usb_error_t 1245 usbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx, 1246 struct usb_config_descriptor *d, uint8_t conf_index) 1247 { 1248 usb_error_t err; 1249 1250 DPRINTFN(4, "confidx=%d\n", conf_index); 1251 1252 err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 1253 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0); 1254 if (err) { 1255 goto done; 1256 } 1257 /* Extra sanity checking */ 1258 if (UGETW(d->wTotalLength) < (uint16_t)sizeof(*d)) { 1259 err = USB_ERR_INVAL; 1260 } 1261 done: 1262 return (err); 1263 } 1264 1265 /*------------------------------------------------------------------------* 1266 * usbd_alloc_config_desc 1267 * 1268 * This function is used to allocate a zeroed configuration 1269 * descriptor. 1270 * 1271 * Returns: 1272 * NULL: Failure 1273 * Else: Success 1274 *------------------------------------------------------------------------*/ 1275 void * 1276 usbd_alloc_config_desc(struct usb_device *udev, uint32_t size) 1277 { 1278 if (size > USB_CONFIG_MAX) { 1279 DPRINTF("Configuration descriptor too big\n"); 1280 return (NULL); 1281 } 1282 #if (USB_HAVE_FIXED_CONFIG == 0) 1283 return (malloc(size, M_USBDEV, M_ZERO | M_WAITOK)); 1284 #else 1285 memset(udev->config_data, 0, sizeof(udev->config_data)); 1286 return (udev->config_data); 1287 #endif 1288 } 1289 1290 /*------------------------------------------------------------------------* 1291 * usbd_alloc_config_desc 1292 * 1293 * This function is used to free a configuration descriptor. 1294 *------------------------------------------------------------------------*/ 1295 void 1296 usbd_free_config_desc(struct usb_device *udev, void *ptr) 1297 { 1298 #if (USB_HAVE_FIXED_CONFIG == 0) 1299 free(ptr, M_USBDEV); 1300 #endif 1301 } 1302 1303 /*------------------------------------------------------------------------* 1304 * usbd_req_get_config_desc_full 1305 * 1306 * This function gets the complete USB configuration descriptor and 1307 * ensures that "wTotalLength" is correct. The returned configuration 1308 * descriptor is freed by calling "usbd_free_config_desc()". 1309 * 1310 * Returns: 1311 * 0: Success 1312 * Else: Failure 1313 *------------------------------------------------------------------------*/ 1314 usb_error_t 1315 usbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx, 1316 struct usb_config_descriptor **ppcd, uint8_t index) 1317 { 1318 struct usb_config_descriptor cd; 1319 struct usb_config_descriptor *cdesc; 1320 uint32_t len; 1321 usb_error_t err; 1322 1323 DPRINTFN(4, "index=%d\n", index); 1324 1325 *ppcd = NULL; 1326 1327 err = usbd_req_get_config_desc(udev, mtx, &cd, index); 1328 if (err) 1329 return (err); 1330 1331 /* get full descriptor */ 1332 len = UGETW(cd.wTotalLength); 1333 if (len < (uint32_t)sizeof(*cdesc)) { 1334 /* corrupt descriptor */ 1335 return (USB_ERR_INVAL); 1336 } else if (len > USB_CONFIG_MAX) { 1337 DPRINTF("Configuration descriptor was truncated\n"); 1338 len = USB_CONFIG_MAX; 1339 } 1340 cdesc = usbd_alloc_config_desc(udev, len); 1341 if (cdesc == NULL) 1342 return (USB_ERR_NOMEM); 1343 err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0, 1344 UDESC_CONFIG, index, 3); 1345 if (err) { 1346 usbd_free_config_desc(udev, cdesc); 1347 return (err); 1348 } 1349 /* make sure that the device is not fooling us: */ 1350 USETW(cdesc->wTotalLength, len); 1351 1352 *ppcd = cdesc; 1353 1354 return (0); /* success */ 1355 } 1356 1357 /*------------------------------------------------------------------------* 1358 * usbd_req_get_device_desc 1359 * 1360 * Returns: 1361 * 0: Success 1362 * Else: Failure 1363 *------------------------------------------------------------------------*/ 1364 usb_error_t 1365 usbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx, 1366 struct usb_device_descriptor *d) 1367 { 1368 DPRINTFN(4, "\n"); 1369 return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 1370 sizeof(*d), 0, UDESC_DEVICE, 0, 3)); 1371 } 1372 1373 /*------------------------------------------------------------------------* 1374 * usbd_req_get_alt_interface_no 1375 * 1376 * Returns: 1377 * 0: Success 1378 * Else: Failure 1379 *------------------------------------------------------------------------*/ 1380 usb_error_t 1381 usbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx, 1382 uint8_t *alt_iface_no, uint8_t iface_index) 1383 { 1384 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1385 struct usb_device_request req; 1386 1387 if ((iface == NULL) || (iface->idesc == NULL)) 1388 return (USB_ERR_INVAL); 1389 1390 req.bmRequestType = UT_READ_INTERFACE; 1391 req.bRequest = UR_GET_INTERFACE; 1392 USETW(req.wValue, 0); 1393 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1394 req.wIndex[1] = 0; 1395 USETW(req.wLength, 1); 1396 return (usbd_do_request(udev, mtx, &req, alt_iface_no)); 1397 } 1398 1399 /*------------------------------------------------------------------------* 1400 * usbd_req_set_alt_interface_no 1401 * 1402 * Returns: 1403 * 0: Success 1404 * Else: Failure 1405 *------------------------------------------------------------------------*/ 1406 usb_error_t 1407 usbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx, 1408 uint8_t iface_index, uint8_t alt_no) 1409 { 1410 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1411 struct usb_device_request req; 1412 1413 if ((iface == NULL) || (iface->idesc == NULL)) 1414 return (USB_ERR_INVAL); 1415 1416 req.bmRequestType = UT_WRITE_INTERFACE; 1417 req.bRequest = UR_SET_INTERFACE; 1418 req.wValue[0] = alt_no; 1419 req.wValue[1] = 0; 1420 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1421 req.wIndex[1] = 0; 1422 USETW(req.wLength, 0); 1423 return (usbd_do_request(udev, mtx, &req, 0)); 1424 } 1425 1426 /*------------------------------------------------------------------------* 1427 * usbd_req_get_device_status 1428 * 1429 * Returns: 1430 * 0: Success 1431 * Else: Failure 1432 *------------------------------------------------------------------------*/ 1433 usb_error_t 1434 usbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx, 1435 struct usb_status *st) 1436 { 1437 struct usb_device_request req; 1438 1439 req.bmRequestType = UT_READ_DEVICE; 1440 req.bRequest = UR_GET_STATUS; 1441 USETW(req.wValue, 0); 1442 USETW(req.wIndex, 0); 1443 USETW(req.wLength, sizeof(*st)); 1444 return (usbd_do_request(udev, mtx, &req, st)); 1445 } 1446 1447 /*------------------------------------------------------------------------* 1448 * usbd_req_get_hub_descriptor 1449 * 1450 * Returns: 1451 * 0: Success 1452 * Else: Failure 1453 *------------------------------------------------------------------------*/ 1454 usb_error_t 1455 usbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx, 1456 struct usb_hub_descriptor *hd, uint8_t nports) 1457 { 1458 struct usb_device_request req; 1459 uint16_t len = (nports + 7 + (8 * 8)) / 8; 1460 1461 req.bmRequestType = UT_READ_CLASS_DEVICE; 1462 req.bRequest = UR_GET_DESCRIPTOR; 1463 USETW2(req.wValue, UDESC_HUB, 0); 1464 USETW(req.wIndex, 0); 1465 USETW(req.wLength, len); 1466 return (usbd_do_request(udev, mtx, &req, hd)); 1467 } 1468 1469 /*------------------------------------------------------------------------* 1470 * usbd_req_get_ss_hub_descriptor 1471 * 1472 * Returns: 1473 * 0: Success 1474 * Else: Failure 1475 *------------------------------------------------------------------------*/ 1476 usb_error_t 1477 usbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx, 1478 struct usb_hub_ss_descriptor *hd, uint8_t nports) 1479 { 1480 struct usb_device_request req; 1481 uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8); 1482 1483 req.bmRequestType = UT_READ_CLASS_DEVICE; 1484 req.bRequest = UR_GET_DESCRIPTOR; 1485 USETW2(req.wValue, UDESC_SS_HUB, 0); 1486 USETW(req.wIndex, 0); 1487 USETW(req.wLength, len); 1488 return (usbd_do_request(udev, mtx, &req, hd)); 1489 } 1490 1491 /*------------------------------------------------------------------------* 1492 * usbd_req_get_hub_status 1493 * 1494 * Returns: 1495 * 0: Success 1496 * Else: Failure 1497 *------------------------------------------------------------------------*/ 1498 usb_error_t 1499 usbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx, 1500 struct usb_hub_status *st) 1501 { 1502 struct usb_device_request req; 1503 1504 req.bmRequestType = UT_READ_CLASS_DEVICE; 1505 req.bRequest = UR_GET_STATUS; 1506 USETW(req.wValue, 0); 1507 USETW(req.wIndex, 0); 1508 USETW(req.wLength, sizeof(struct usb_hub_status)); 1509 return (usbd_do_request(udev, mtx, &req, st)); 1510 } 1511 1512 /*------------------------------------------------------------------------* 1513 * usbd_req_set_address 1514 * 1515 * This function is used to set the address for an USB device. After 1516 * port reset the USB device will respond at address zero. 1517 * 1518 * Returns: 1519 * 0: Success 1520 * Else: Failure 1521 *------------------------------------------------------------------------*/ 1522 usb_error_t 1523 usbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr) 1524 { 1525 struct usb_device_request req; 1526 usb_error_t err; 1527 1528 DPRINTFN(6, "setting device address=%d\n", addr); 1529 1530 req.bmRequestType = UT_WRITE_DEVICE; 1531 req.bRequest = UR_SET_ADDRESS; 1532 USETW(req.wValue, addr); 1533 USETW(req.wIndex, 0); 1534 USETW(req.wLength, 0); 1535 1536 err = USB_ERR_INVAL; 1537 1538 /* check if USB controller handles set address */ 1539 if (udev->bus->methods->set_address != NULL) 1540 err = (udev->bus->methods->set_address) (udev, mtx, addr); 1541 1542 if (err != USB_ERR_INVAL) 1543 goto done; 1544 1545 /* Setting the address should not take more than 1 second ! */ 1546 err = usbd_do_request_flags(udev, mtx, &req, NULL, 1547 USB_DELAY_STATUS_STAGE, NULL, 1000); 1548 1549 done: 1550 /* allow device time to set new address */ 1551 usb_pause_mtx(mtx, 1552 USB_MS_TO_TICKS(usb_set_address_settle)); 1553 1554 return (err); 1555 } 1556 1557 /*------------------------------------------------------------------------* 1558 * usbd_req_get_port_status 1559 * 1560 * Returns: 1561 * 0: Success 1562 * Else: Failure 1563 *------------------------------------------------------------------------*/ 1564 usb_error_t 1565 usbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx, 1566 struct usb_port_status *ps, uint8_t port) 1567 { 1568 struct usb_device_request req; 1569 1570 req.bmRequestType = UT_READ_CLASS_OTHER; 1571 req.bRequest = UR_GET_STATUS; 1572 USETW(req.wValue, 0); 1573 req.wIndex[0] = port; 1574 req.wIndex[1] = 0; 1575 USETW(req.wLength, sizeof *ps); 1576 return (usbd_do_request(udev, mtx, &req, ps)); 1577 } 1578 1579 /*------------------------------------------------------------------------* 1580 * usbd_req_clear_hub_feature 1581 * 1582 * Returns: 1583 * 0: Success 1584 * Else: Failure 1585 *------------------------------------------------------------------------*/ 1586 usb_error_t 1587 usbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx, 1588 uint16_t sel) 1589 { 1590 struct usb_device_request req; 1591 1592 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1593 req.bRequest = UR_CLEAR_FEATURE; 1594 USETW(req.wValue, sel); 1595 USETW(req.wIndex, 0); 1596 USETW(req.wLength, 0); 1597 return (usbd_do_request(udev, mtx, &req, 0)); 1598 } 1599 1600 /*------------------------------------------------------------------------* 1601 * usbd_req_set_hub_feature 1602 * 1603 * Returns: 1604 * 0: Success 1605 * Else: Failure 1606 *------------------------------------------------------------------------*/ 1607 usb_error_t 1608 usbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx, 1609 uint16_t sel) 1610 { 1611 struct usb_device_request req; 1612 1613 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1614 req.bRequest = UR_SET_FEATURE; 1615 USETW(req.wValue, sel); 1616 USETW(req.wIndex, 0); 1617 USETW(req.wLength, 0); 1618 return (usbd_do_request(udev, mtx, &req, 0)); 1619 } 1620 1621 /*------------------------------------------------------------------------* 1622 * usbd_req_set_hub_u1_timeout 1623 * 1624 * Returns: 1625 * 0: Success 1626 * Else: Failure 1627 *------------------------------------------------------------------------*/ 1628 usb_error_t 1629 usbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx, 1630 uint8_t port, uint8_t timeout) 1631 { 1632 struct usb_device_request req; 1633 1634 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1635 req.bRequest = UR_SET_FEATURE; 1636 USETW(req.wValue, UHF_PORT_U1_TIMEOUT); 1637 req.wIndex[0] = port; 1638 req.wIndex[1] = timeout; 1639 USETW(req.wLength, 0); 1640 return (usbd_do_request(udev, mtx, &req, 0)); 1641 } 1642 1643 /*------------------------------------------------------------------------* 1644 * usbd_req_set_hub_u2_timeout 1645 * 1646 * Returns: 1647 * 0: Success 1648 * Else: Failure 1649 *------------------------------------------------------------------------*/ 1650 usb_error_t 1651 usbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx, 1652 uint8_t port, uint8_t timeout) 1653 { 1654 struct usb_device_request req; 1655 1656 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1657 req.bRequest = UR_SET_FEATURE; 1658 USETW(req.wValue, UHF_PORT_U2_TIMEOUT); 1659 req.wIndex[0] = port; 1660 req.wIndex[1] = timeout; 1661 USETW(req.wLength, 0); 1662 return (usbd_do_request(udev, mtx, &req, 0)); 1663 } 1664 1665 /*------------------------------------------------------------------------* 1666 * usbd_req_set_hub_depth 1667 * 1668 * Returns: 1669 * 0: Success 1670 * Else: Failure 1671 *------------------------------------------------------------------------*/ 1672 usb_error_t 1673 usbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx, 1674 uint16_t depth) 1675 { 1676 struct usb_device_request req; 1677 1678 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1679 req.bRequest = UR_SET_HUB_DEPTH; 1680 USETW(req.wValue, depth); 1681 USETW(req.wIndex, 0); 1682 USETW(req.wLength, 0); 1683 return (usbd_do_request(udev, mtx, &req, 0)); 1684 } 1685 1686 /*------------------------------------------------------------------------* 1687 * usbd_req_clear_port_feature 1688 * 1689 * Returns: 1690 * 0: Success 1691 * Else: Failure 1692 *------------------------------------------------------------------------*/ 1693 usb_error_t 1694 usbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx, 1695 uint8_t port, uint16_t sel) 1696 { 1697 struct usb_device_request req; 1698 1699 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1700 req.bRequest = UR_CLEAR_FEATURE; 1701 USETW(req.wValue, sel); 1702 req.wIndex[0] = port; 1703 req.wIndex[1] = 0; 1704 USETW(req.wLength, 0); 1705 return (usbd_do_request(udev, mtx, &req, 0)); 1706 } 1707 1708 /*------------------------------------------------------------------------* 1709 * usbd_req_set_port_feature 1710 * 1711 * Returns: 1712 * 0: Success 1713 * Else: Failure 1714 *------------------------------------------------------------------------*/ 1715 usb_error_t 1716 usbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx, 1717 uint8_t port, uint16_t sel) 1718 { 1719 struct usb_device_request req; 1720 1721 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1722 req.bRequest = UR_SET_FEATURE; 1723 USETW(req.wValue, sel); 1724 req.wIndex[0] = port; 1725 req.wIndex[1] = 0; 1726 USETW(req.wLength, 0); 1727 return (usbd_do_request(udev, mtx, &req, 0)); 1728 } 1729 1730 /*------------------------------------------------------------------------* 1731 * usbd_req_set_protocol 1732 * 1733 * Returns: 1734 * 0: Success 1735 * Else: Failure 1736 *------------------------------------------------------------------------*/ 1737 usb_error_t 1738 usbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx, 1739 uint8_t iface_index, uint16_t report) 1740 { 1741 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1742 struct usb_device_request req; 1743 1744 if ((iface == NULL) || (iface->idesc == NULL)) { 1745 return (USB_ERR_INVAL); 1746 } 1747 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n", 1748 iface, report, iface->idesc->bInterfaceNumber); 1749 1750 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1751 req.bRequest = UR_SET_PROTOCOL; 1752 USETW(req.wValue, report); 1753 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1754 req.wIndex[1] = 0; 1755 USETW(req.wLength, 0); 1756 return (usbd_do_request(udev, mtx, &req, 0)); 1757 } 1758 1759 /*------------------------------------------------------------------------* 1760 * usbd_req_set_report 1761 * 1762 * Returns: 1763 * 0: Success 1764 * Else: Failure 1765 *------------------------------------------------------------------------*/ 1766 usb_error_t 1767 usbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len, 1768 uint8_t iface_index, uint8_t type, uint8_t id) 1769 { 1770 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1771 struct usb_device_request req; 1772 1773 if ((iface == NULL) || (iface->idesc == NULL)) { 1774 return (USB_ERR_INVAL); 1775 } 1776 DPRINTFN(5, "len=%d\n", len); 1777 1778 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1779 req.bRequest = UR_SET_REPORT; 1780 USETW2(req.wValue, type, id); 1781 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1782 req.wIndex[1] = 0; 1783 USETW(req.wLength, len); 1784 return (usbd_do_request(udev, mtx, &req, data)); 1785 } 1786 1787 /*------------------------------------------------------------------------* 1788 * usbd_req_get_report 1789 * 1790 * Returns: 1791 * 0: Success 1792 * Else: Failure 1793 *------------------------------------------------------------------------*/ 1794 usb_error_t 1795 usbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data, 1796 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id) 1797 { 1798 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1799 struct usb_device_request req; 1800 1801 if ((iface == NULL) || (iface->idesc == NULL)) { 1802 return (USB_ERR_INVAL); 1803 } 1804 DPRINTFN(5, "len=%d\n", len); 1805 1806 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1807 req.bRequest = UR_GET_REPORT; 1808 USETW2(req.wValue, type, id); 1809 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1810 req.wIndex[1] = 0; 1811 USETW(req.wLength, len); 1812 return (usbd_do_request(udev, mtx, &req, data)); 1813 } 1814 1815 /*------------------------------------------------------------------------* 1816 * usbd_req_set_idle 1817 * 1818 * Returns: 1819 * 0: Success 1820 * Else: Failure 1821 *------------------------------------------------------------------------*/ 1822 usb_error_t 1823 usbd_req_set_idle(struct usb_device *udev, struct mtx *mtx, 1824 uint8_t iface_index, uint8_t duration, uint8_t id) 1825 { 1826 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1827 struct usb_device_request req; 1828 1829 if ((iface == NULL) || (iface->idesc == NULL)) { 1830 return (USB_ERR_INVAL); 1831 } 1832 DPRINTFN(5, "%d %d\n", duration, id); 1833 1834 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1835 req.bRequest = UR_SET_IDLE; 1836 USETW2(req.wValue, duration, id); 1837 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1838 req.wIndex[1] = 0; 1839 USETW(req.wLength, 0); 1840 return (usbd_do_request(udev, mtx, &req, 0)); 1841 } 1842 1843 /*------------------------------------------------------------------------* 1844 * usbd_req_get_report_descriptor 1845 * 1846 * Returns: 1847 * 0: Success 1848 * Else: Failure 1849 *------------------------------------------------------------------------*/ 1850 usb_error_t 1851 usbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx, 1852 void *d, uint16_t size, uint8_t iface_index) 1853 { 1854 struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1855 struct usb_device_request req; 1856 1857 if ((iface == NULL) || (iface->idesc == NULL)) { 1858 return (USB_ERR_INVAL); 1859 } 1860 req.bmRequestType = UT_READ_INTERFACE; 1861 req.bRequest = UR_GET_DESCRIPTOR; 1862 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 1863 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1864 req.wIndex[1] = 0; 1865 USETW(req.wLength, size); 1866 return (usbd_do_request(udev, mtx, &req, d)); 1867 } 1868 1869 /*------------------------------------------------------------------------* 1870 * usbd_req_set_config 1871 * 1872 * This function is used to select the current configuration number in 1873 * both USB device side mode and USB host side mode. When setting the 1874 * configuration the function of the interfaces can change. 1875 * 1876 * Returns: 1877 * 0: Success 1878 * Else: Failure 1879 *------------------------------------------------------------------------*/ 1880 usb_error_t 1881 usbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf) 1882 { 1883 struct usb_device_request req; 1884 1885 DPRINTF("setting config %d\n", conf); 1886 1887 /* do "set configuration" request */ 1888 1889 req.bmRequestType = UT_WRITE_DEVICE; 1890 req.bRequest = UR_SET_CONFIG; 1891 req.wValue[0] = conf; 1892 req.wValue[1] = 0; 1893 USETW(req.wIndex, 0); 1894 USETW(req.wLength, 0); 1895 return (usbd_do_request(udev, mtx, &req, 0)); 1896 } 1897 1898 /*------------------------------------------------------------------------* 1899 * usbd_req_get_config 1900 * 1901 * Returns: 1902 * 0: Success 1903 * Else: Failure 1904 *------------------------------------------------------------------------*/ 1905 usb_error_t 1906 usbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf) 1907 { 1908 struct usb_device_request req; 1909 1910 req.bmRequestType = UT_READ_DEVICE; 1911 req.bRequest = UR_GET_CONFIG; 1912 USETW(req.wValue, 0); 1913 USETW(req.wIndex, 0); 1914 USETW(req.wLength, 1); 1915 return (usbd_do_request(udev, mtx, &req, pconf)); 1916 } 1917 1918 /*------------------------------------------------------------------------* 1919 * usbd_setup_device_desc 1920 *------------------------------------------------------------------------*/ 1921 usb_error_t 1922 usbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx) 1923 { 1924 usb_error_t err; 1925 1926 /* 1927 * Get the first 8 bytes of the device descriptor ! 1928 * 1929 * NOTE: "usbd_do_request()" will check the device descriptor 1930 * next time we do a request to see if the maximum packet size 1931 * changed! The 8 first bytes of the device descriptor 1932 * contains the maximum packet size to use on control endpoint 1933 * 0. If this value is different from "USB_MAX_IPACKET" a new 1934 * USB control request will be setup! 1935 */ 1936 switch (udev->speed) { 1937 case USB_SPEED_FULL: 1938 if (usb_full_ddesc != 0) { 1939 /* get full device descriptor */ 1940 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1941 if (err == 0) 1942 break; 1943 } 1944 1945 /* get partial device descriptor, some devices crash on this */ 1946 err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc, 1947 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1948 if (err != 0) 1949 break; 1950 1951 /* get the full device descriptor */ 1952 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1953 break; 1954 1955 default: 1956 DPRINTF("Minimum bMaxPacketSize is large enough " 1957 "to hold the complete device descriptor or " 1958 "only one bMaxPacketSize choice\n"); 1959 1960 /* get the full device descriptor */ 1961 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1962 1963 /* try one more time, if error */ 1964 if (err != 0) 1965 err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1966 break; 1967 } 1968 1969 if (err != 0) { 1970 DPRINTFN(0, "getting device descriptor " 1971 "at addr %d failed, %s\n", udev->address, 1972 usbd_errstr(err)); 1973 return (err); 1974 } 1975 1976 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1977 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1978 udev->address, UGETW(udev->ddesc.bcdUSB), 1979 udev->ddesc.bDeviceClass, 1980 udev->ddesc.bDeviceSubClass, 1981 udev->ddesc.bDeviceProtocol, 1982 udev->ddesc.bMaxPacketSize, 1983 udev->ddesc.bLength, 1984 udev->speed); 1985 1986 return (err); 1987 } 1988 1989 /*------------------------------------------------------------------------* 1990 * usbd_req_re_enumerate 1991 * 1992 * NOTE: After this function returns the hardware is in the 1993 * unconfigured state! The application is responsible for setting a 1994 * new configuration. 1995 * 1996 * Returns: 1997 * 0: Success 1998 * Else: Failure 1999 *------------------------------------------------------------------------*/ 2000 usb_error_t 2001 usbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx) 2002 { 2003 struct usb_device *parent_hub; 2004 usb_error_t err; 2005 uint8_t old_addr; 2006 uint8_t do_retry = 1; 2007 2008 if (udev->flags.usb_mode != USB_MODE_HOST) { 2009 return (USB_ERR_INVAL); 2010 } 2011 old_addr = udev->address; 2012 parent_hub = udev->parent_hub; 2013 if (parent_hub == NULL) { 2014 return (USB_ERR_INVAL); 2015 } 2016 retry: 2017 #if USB_HAVE_TT_SUPPORT 2018 /* 2019 * Try to reset the High Speed parent HUB of a LOW- or FULL- 2020 * speed device, if any. 2021 */ 2022 if (udev->parent_hs_hub != NULL && 2023 udev->speed != USB_SPEED_HIGH) { 2024 DPRINTF("Trying to reset parent High Speed TT.\n"); 2025 if (udev->parent_hs_hub == parent_hub && 2026 (uhub_count_active_host_ports(parent_hub, USB_SPEED_LOW) + 2027 uhub_count_active_host_ports(parent_hub, USB_SPEED_FULL)) == 1) { 2028 /* we can reset the whole TT */ 2029 err = usbd_req_reset_tt(parent_hub, NULL, 2030 udev->hs_port_no); 2031 } else { 2032 /* only reset a particular device and endpoint */ 2033 err = usbd_req_clear_tt_buffer(udev->parent_hs_hub, NULL, 2034 udev->hs_port_no, old_addr, UE_CONTROL, 0); 2035 } 2036 if (err) { 2037 DPRINTF("Resetting parent High " 2038 "Speed TT failed (%s).\n", 2039 usbd_errstr(err)); 2040 } 2041 } 2042 #endif 2043 /* Try to warm reset first */ 2044 if (parent_hub->speed == USB_SPEED_SUPER) 2045 usbd_req_warm_reset_port(parent_hub, mtx, udev->port_no); 2046 2047 /* Try to reset the parent HUB port. */ 2048 err = usbd_req_reset_port(parent_hub, mtx, udev->port_no); 2049 if (err) { 2050 DPRINTFN(0, "addr=%d, port reset failed, %s\n", 2051 old_addr, usbd_errstr(err)); 2052 goto done; 2053 } 2054 2055 /* 2056 * After that the port has been reset our device should be at 2057 * address zero: 2058 */ 2059 udev->address = USB_START_ADDR; 2060 2061 /* reset "bMaxPacketSize" */ 2062 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 2063 2064 /* reset USB state */ 2065 usb_set_device_state(udev, USB_STATE_POWERED); 2066 2067 /* 2068 * Restore device address: 2069 */ 2070 err = usbd_req_set_address(udev, mtx, old_addr); 2071 if (err) { 2072 /* XXX ignore any errors! */ 2073 DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n", 2074 old_addr, usbd_errstr(err)); 2075 } 2076 /* 2077 * Restore device address, if the controller driver did not 2078 * set a new one: 2079 */ 2080 if (udev->address == USB_START_ADDR) 2081 udev->address = old_addr; 2082 2083 /* setup the device descriptor and the initial "wMaxPacketSize" */ 2084 err = usbd_setup_device_desc(udev, mtx); 2085 2086 done: 2087 if (err && do_retry) { 2088 /* give the USB firmware some time to load */ 2089 usb_pause_mtx(mtx, hz / 2); 2090 /* no more retries after this retry */ 2091 do_retry = 0; 2092 /* try again */ 2093 goto retry; 2094 } 2095 /* restore address */ 2096 if (udev->address == USB_START_ADDR) 2097 udev->address = old_addr; 2098 /* update state, if successful */ 2099 if (err == 0) 2100 usb_set_device_state(udev, USB_STATE_ADDRESSED); 2101 return (err); 2102 } 2103 2104 /*------------------------------------------------------------------------* 2105 * usbd_req_clear_device_feature 2106 * 2107 * Returns: 2108 * 0: Success 2109 * Else: Failure 2110 *------------------------------------------------------------------------*/ 2111 usb_error_t 2112 usbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx, 2113 uint16_t sel) 2114 { 2115 struct usb_device_request req; 2116 2117 req.bmRequestType = UT_WRITE_DEVICE; 2118 req.bRequest = UR_CLEAR_FEATURE; 2119 USETW(req.wValue, sel); 2120 USETW(req.wIndex, 0); 2121 USETW(req.wLength, 0); 2122 return (usbd_do_request(udev, mtx, &req, 0)); 2123 } 2124 2125 /*------------------------------------------------------------------------* 2126 * usbd_req_set_device_feature 2127 * 2128 * Returns: 2129 * 0: Success 2130 * Else: Failure 2131 *------------------------------------------------------------------------*/ 2132 usb_error_t 2133 usbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx, 2134 uint16_t sel) 2135 { 2136 struct usb_device_request req; 2137 2138 req.bmRequestType = UT_WRITE_DEVICE; 2139 req.bRequest = UR_SET_FEATURE; 2140 USETW(req.wValue, sel); 2141 USETW(req.wIndex, 0); 2142 USETW(req.wLength, 0); 2143 return (usbd_do_request(udev, mtx, &req, 0)); 2144 } 2145 2146 /*------------------------------------------------------------------------* 2147 * usbd_req_reset_tt 2148 * 2149 * Returns: 2150 * 0: Success 2151 * Else: Failure 2152 *------------------------------------------------------------------------*/ 2153 usb_error_t 2154 usbd_req_reset_tt(struct usb_device *udev, struct mtx *mtx, 2155 uint8_t port) 2156 { 2157 struct usb_device_request req; 2158 2159 /* For single TT HUBs the port should be 1 */ 2160 2161 if (udev->ddesc.bDeviceClass == UDCLASS_HUB && 2162 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT) 2163 port = 1; 2164 2165 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2166 req.bRequest = UR_RESET_TT; 2167 USETW(req.wValue, 0); 2168 req.wIndex[0] = port; 2169 req.wIndex[1] = 0; 2170 USETW(req.wLength, 0); 2171 return (usbd_do_request(udev, mtx, &req, 0)); 2172 } 2173 2174 /*------------------------------------------------------------------------* 2175 * usbd_req_clear_tt_buffer 2176 * 2177 * For single TT HUBs the port should be 1. 2178 * 2179 * Returns: 2180 * 0: Success 2181 * Else: Failure 2182 *------------------------------------------------------------------------*/ 2183 usb_error_t 2184 usbd_req_clear_tt_buffer(struct usb_device *udev, struct mtx *mtx, 2185 uint8_t port, uint8_t addr, uint8_t type, uint8_t endpoint) 2186 { 2187 struct usb_device_request req; 2188 uint16_t wValue; 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 wValue = (endpoint & 0xF) | ((addr & 0x7F) << 4) | 2197 ((endpoint & 0x80) << 8) | ((type & 3) << 12); 2198 2199 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2200 req.bRequest = UR_CLEAR_TT_BUFFER; 2201 USETW(req.wValue, wValue); 2202 req.wIndex[0] = port; 2203 req.wIndex[1] = 0; 2204 USETW(req.wLength, 0); 2205 return (usbd_do_request(udev, mtx, &req, 0)); 2206 } 2207 2208 /*------------------------------------------------------------------------* 2209 * usbd_req_set_port_link_state 2210 * 2211 * USB 3.0 specific request 2212 * 2213 * Returns: 2214 * 0: Success 2215 * Else: Failure 2216 *------------------------------------------------------------------------*/ 2217 usb_error_t 2218 usbd_req_set_port_link_state(struct usb_device *udev, struct mtx *mtx, 2219 uint8_t port, uint8_t link_state) 2220 { 2221 struct usb_device_request req; 2222 2223 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2224 req.bRequest = UR_SET_FEATURE; 2225 USETW(req.wValue, UHF_PORT_LINK_STATE); 2226 req.wIndex[0] = port; 2227 req.wIndex[1] = link_state; 2228 USETW(req.wLength, 0); 2229 return (usbd_do_request(udev, mtx, &req, 0)); 2230 } 2231 2232 /*------------------------------------------------------------------------* 2233 * usbd_req_set_lpm_info 2234 * 2235 * USB 2.0 specific request for Link Power Management. 2236 * 2237 * Returns: 2238 * 0: Success 2239 * USB_ERR_PENDING_REQUESTS: NYET 2240 * USB_ERR_TIMEOUT: TIMEOUT 2241 * USB_ERR_STALL: STALL 2242 * Else: Failure 2243 *------------------------------------------------------------------------*/ 2244 usb_error_t 2245 usbd_req_set_lpm_info(struct usb_device *udev, struct mtx *mtx, 2246 uint8_t port, uint8_t besl, uint8_t addr, uint8_t rwe) 2247 { 2248 struct usb_device_request req; 2249 usb_error_t err; 2250 uint8_t buf[1]; 2251 2252 req.bmRequestType = UT_WRITE_CLASS_OTHER; 2253 req.bRequest = UR_SET_AND_TEST; 2254 USETW(req.wValue, UHF_PORT_L1); 2255 req.wIndex[0] = (port & 0xF) | ((besl & 0xF) << 4); 2256 req.wIndex[1] = (addr & 0x7F) | (rwe ? 0x80 : 0x00); 2257 USETW(req.wLength, sizeof(buf)); 2258 2259 /* set default value in case of short transfer */ 2260 buf[0] = 0x00; 2261 2262 err = usbd_do_request(udev, mtx, &req, buf); 2263 if (err) 2264 return (err); 2265 2266 switch (buf[0]) { 2267 case 0x00: /* SUCCESS */ 2268 break; 2269 case 0x10: /* NYET */ 2270 err = USB_ERR_PENDING_REQUESTS; 2271 break; 2272 case 0x11: /* TIMEOUT */ 2273 err = USB_ERR_TIMEOUT; 2274 break; 2275 case 0x30: /* STALL */ 2276 err = USB_ERR_STALLED; 2277 break; 2278 default: /* reserved */ 2279 err = USB_ERR_IOERROR; 2280 break; 2281 } 2282 return (err); 2283 } 2284 2285