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