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