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