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