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