1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <dev/usb/usb_mfunc.h> 30 #include <dev/usb/usb_error.h> 31 #include <dev/usb/usb.h> 32 #include <dev/usb/usb_ioctl.h> 33 #include <dev/usb/usbhid.h> 34 35 #define USB_DEBUG_VAR usb2_debug 36 37 #include <dev/usb/usb_core.h> 38 #include <dev/usb/usb_busdma.h> 39 #include <dev/usb/usb_request.h> 40 #include <dev/usb/usb_process.h> 41 #include <dev/usb/usb_transfer.h> 42 #include <dev/usb/usb_debug.h> 43 #include <dev/usb/usb_device.h> 44 #include <dev/usb/usb_util.h> 45 #include <dev/usb/usb_dynamic.h> 46 47 #include <dev/usb/usb_controller.h> 48 #include <dev/usb/usb_bus.h> 49 #include <sys/ctype.h> 50 51 #if USB_DEBUG 52 static int usb2_pr_poll_delay = USB_PORT_RESET_DELAY; 53 static int usb2_pr_recovery_delay = USB_PORT_RESET_RECOVERY; 54 static int usb2_ss_delay = 0; 55 56 SYSCTL_INT(_hw_usb2, OID_AUTO, pr_poll_delay, CTLFLAG_RW, 57 &usb2_pr_poll_delay, 0, "USB port reset poll delay in ms"); 58 SYSCTL_INT(_hw_usb2, OID_AUTO, pr_recovery_delay, CTLFLAG_RW, 59 &usb2_pr_recovery_delay, 0, "USB port reset recovery delay in ms"); 60 SYSCTL_INT(_hw_usb2, OID_AUTO, ss_delay, CTLFLAG_RW, 61 &usb2_ss_delay, 0, "USB status stage delay in ms"); 62 #endif 63 64 /*------------------------------------------------------------------------* 65 * usb2_do_request_callback 66 * 67 * This function is the USB callback for generic USB Host control 68 * transfers. 69 *------------------------------------------------------------------------*/ 70 void 71 usb2_do_request_callback(struct usb2_xfer *xfer) 72 { 73 ; /* workaround for a bug in "indent" */ 74 75 DPRINTF("st=%u\n", USB_GET_STATE(xfer)); 76 77 switch (USB_GET_STATE(xfer)) { 78 case USB_ST_SETUP: 79 usb2_start_hardware(xfer); 80 break; 81 default: 82 usb2_cv_signal(xfer->xroot->udev->default_cv); 83 break; 84 } 85 } 86 87 /*------------------------------------------------------------------------* 88 * usb2_do_clear_stall_callback 89 * 90 * This function is the USB callback for generic clear stall requests. 91 *------------------------------------------------------------------------*/ 92 void 93 usb2_do_clear_stall_callback(struct usb2_xfer *xfer) 94 { 95 struct usb2_device_request req; 96 struct usb2_device *udev; 97 struct usb2_pipe *pipe; 98 struct usb2_pipe *pipe_end; 99 struct usb2_pipe *pipe_first; 100 uint8_t to = USB_EP_MAX; 101 102 udev = xfer->xroot->udev; 103 104 USB_BUS_LOCK(udev->bus); 105 106 /* round robin pipe clear stall */ 107 108 pipe = udev->pipe_curr; 109 pipe_end = udev->pipes + USB_EP_MAX; 110 pipe_first = udev->pipes; 111 if (pipe == NULL) { 112 pipe = pipe_first; 113 } 114 switch (USB_GET_STATE(xfer)) { 115 case USB_ST_TRANSFERRED: 116 if (pipe->edesc && 117 pipe->is_stalled) { 118 pipe->toggle_next = 0; 119 pipe->is_stalled = 0; 120 /* start up the current or next transfer, if any */ 121 usb2_command_wrapper(&pipe->pipe_q, 122 pipe->pipe_q.curr); 123 } 124 pipe++; 125 126 case USB_ST_SETUP: 127 tr_setup: 128 if (pipe == pipe_end) { 129 pipe = pipe_first; 130 } 131 if (pipe->edesc && 132 pipe->is_stalled) { 133 134 /* setup a clear-stall packet */ 135 136 req.bmRequestType = UT_WRITE_ENDPOINT; 137 req.bRequest = UR_CLEAR_FEATURE; 138 USETW(req.wValue, UF_ENDPOINT_HALT); 139 req.wIndex[0] = pipe->edesc->bEndpointAddress; 140 req.wIndex[1] = 0; 141 USETW(req.wLength, 0); 142 143 /* copy in the transfer */ 144 145 usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req)); 146 147 /* set length */ 148 xfer->frlengths[0] = sizeof(req); 149 xfer->nframes = 1; 150 USB_BUS_UNLOCK(udev->bus); 151 152 usb2_start_hardware(xfer); 153 154 USB_BUS_LOCK(udev->bus); 155 break; 156 } 157 pipe++; 158 if (--to) 159 goto tr_setup; 160 break; 161 162 default: 163 if (xfer->error == USB_ERR_CANCELLED) { 164 break; 165 } 166 goto tr_setup; 167 } 168 169 /* store current pipe */ 170 udev->pipe_curr = pipe; 171 USB_BUS_UNLOCK(udev->bus); 172 } 173 174 /*------------------------------------------------------------------------* 175 * usb2_do_request_flags and usb2_do_request 176 * 177 * Description of arguments passed to these functions: 178 * 179 * "udev" - this is the "usb2_device" structure pointer on which the 180 * request should be performed. It is possible to call this function 181 * in both Host Side mode and Device Side mode. 182 * 183 * "mtx" - if this argument is non-NULL the mutex pointed to by it 184 * will get dropped and picked up during the execution of this 185 * function, hence this function sometimes needs to sleep. If this 186 * argument is NULL it has no effect. 187 * 188 * "req" - this argument must always be non-NULL and points to an 189 * 8-byte structure holding the USB request to be done. The USB 190 * request structure has a bit telling the direction of the USB 191 * request, if it is a read or a write. 192 * 193 * "data" - if the "wLength" part of the structure pointed to by "req" 194 * is non-zero this argument must point to a valid kernel buffer which 195 * can hold at least "wLength" bytes. If "wLength" is zero "data" can 196 * be NULL. 197 * 198 * "flags" - here is a list of valid flags: 199 * 200 * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than 201 * specified 202 * 203 * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed 204 * at a later point in time. This is tunable by the "hw.usb.ss_delay" 205 * sysctl. This flag is mostly useful for debugging. 206 * 207 * o USB_USER_DATA_PTR: treat the "data" pointer like a userland 208 * pointer. 209 * 210 * "actlen" - if non-NULL the actual transfer length will be stored in 211 * the 16-bit unsigned integer pointed to by "actlen". This 212 * information is mostly useful when the "USB_SHORT_XFER_OK" flag is 213 * used. 214 * 215 * "timeout" - gives the timeout for the control transfer in 216 * milliseconds. A "timeout" value less than 50 milliseconds is 217 * treated like a 50 millisecond timeout. A "timeout" value greater 218 * than 30 seconds is treated like a 30 second timeout. This USB stack 219 * does not allow control requests without a timeout. 220 * 221 * NOTE: This function is thread safe. All calls to 222 * "usb2_do_request_flags" will be serialised by the use of an 223 * internal "sx_lock". 224 * 225 * Returns: 226 * 0: Success 227 * Else: Failure 228 *------------------------------------------------------------------------*/ 229 usb2_error_t 230 usb2_do_request_flags(struct usb2_device *udev, struct mtx *mtx, 231 struct usb2_device_request *req, void *data, uint32_t flags, 232 uint16_t *actlen, uint32_t timeout) 233 { 234 struct usb2_xfer *xfer; 235 const void *desc; 236 int err = 0; 237 uint32_t start_ticks; 238 uint32_t delta_ticks; 239 uint32_t max_ticks; 240 uint16_t length; 241 uint16_t temp; 242 243 if (timeout < 50) { 244 /* timeout is too small */ 245 timeout = 50; 246 } 247 if (timeout > 30000) { 248 /* timeout is too big */ 249 timeout = 30000; 250 } 251 length = UGETW(req->wLength); 252 253 DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x " 254 "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n", 255 udev, req->bmRequestType, req->bRequest, 256 req->wValue[1], req->wValue[0], 257 req->wIndex[1], req->wIndex[0], 258 req->wLength[1], req->wLength[0]); 259 260 /* 261 * Set "actlen" to a known value in case the caller does not 262 * check the return value: 263 */ 264 if (actlen) { 265 *actlen = 0; 266 } 267 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 268 DPRINTF("USB device mode\n"); 269 (usb2_temp_get_desc_p) (udev, req, &desc, &temp); 270 if (length > temp) { 271 if (!(flags & USB_SHORT_XFER_OK)) { 272 return (USB_ERR_SHORT_XFER); 273 } 274 length = temp; 275 } 276 if (actlen) { 277 *actlen = length; 278 } 279 if (length > 0) { 280 if (flags & USB_USER_DATA_PTR) { 281 if (copyout(desc, data, length)) { 282 return (USB_ERR_INVAL); 283 } 284 } else { 285 bcopy(desc, data, length); 286 } 287 } 288 return (0); /* success */ 289 } 290 if (mtx) { 291 mtx_unlock(mtx); 292 if (mtx != &Giant) { 293 mtx_assert(mtx, MA_NOTOWNED); 294 } 295 } 296 /* 297 * Grab the default sx-lock so that serialisation 298 * is achieved when multiple threads are involved: 299 */ 300 301 sx_xlock(udev->default_sx); 302 303 /* 304 * Setup a new USB transfer or use the existing one, if any: 305 */ 306 usb2_default_transfer_setup(udev); 307 308 xfer = udev->default_xfer[0]; 309 if (xfer == NULL) { 310 /* most likely out of memory */ 311 err = USB_ERR_NOMEM; 312 goto done; 313 } 314 USB_XFER_LOCK(xfer); 315 316 if (flags & USB_DELAY_STATUS_STAGE) { 317 xfer->flags.manual_status = 1; 318 } else { 319 xfer->flags.manual_status = 0; 320 } 321 322 xfer->timeout = timeout; 323 324 start_ticks = ticks; 325 326 max_ticks = USB_MS_TO_TICKS(timeout); 327 328 usb2_copy_in(xfer->frbuffers, 0, req, sizeof(*req)); 329 330 xfer->frlengths[0] = sizeof(*req); 331 xfer->nframes = 2; 332 333 while (1) { 334 temp = length; 335 if (temp > xfer->max_data_length) { 336 temp = xfer->max_data_length; 337 } 338 xfer->frlengths[1] = temp; 339 340 if (temp > 0) { 341 if (!(req->bmRequestType & UT_READ)) { 342 if (flags & USB_USER_DATA_PTR) { 343 USB_XFER_UNLOCK(xfer); 344 err = usb2_copy_in_user(xfer->frbuffers + 1, 345 0, data, temp); 346 USB_XFER_LOCK(xfer); 347 if (err) { 348 err = USB_ERR_INVAL; 349 break; 350 } 351 } else { 352 usb2_copy_in(xfer->frbuffers + 1, 0, data, temp); 353 } 354 } 355 xfer->nframes = 2; 356 } else { 357 if (xfer->frlengths[0] == 0) { 358 if (xfer->flags.manual_status) { 359 #if USB_DEBUG 360 int temp; 361 362 temp = usb2_ss_delay; 363 if (temp > 5000) { 364 temp = 5000; 365 } 366 if (temp > 0) { 367 usb2_pause_mtx( 368 xfer->xroot->xfer_mtx, 369 USB_MS_TO_TICKS(temp)); 370 } 371 #endif 372 xfer->flags.manual_status = 0; 373 } else { 374 break; 375 } 376 } 377 xfer->nframes = 1; 378 } 379 380 usb2_transfer_start(xfer); 381 382 while (usb2_transfer_pending(xfer)) { 383 usb2_cv_wait(udev->default_cv, 384 xfer->xroot->xfer_mtx); 385 } 386 387 err = xfer->error; 388 389 if (err) { 390 break; 391 } 392 /* subtract length of SETUP packet, if any */ 393 394 if (xfer->aframes > 0) { 395 xfer->actlen -= xfer->frlengths[0]; 396 } else { 397 xfer->actlen = 0; 398 } 399 400 /* check for short packet */ 401 402 if (temp > xfer->actlen) { 403 temp = xfer->actlen; 404 if (!(flags & USB_SHORT_XFER_OK)) { 405 err = USB_ERR_SHORT_XFER; 406 } 407 length = temp; 408 } 409 if (temp > 0) { 410 if (req->bmRequestType & UT_READ) { 411 if (flags & USB_USER_DATA_PTR) { 412 USB_XFER_UNLOCK(xfer); 413 err = usb2_copy_out_user(xfer->frbuffers + 1, 414 0, data, temp); 415 USB_XFER_LOCK(xfer); 416 if (err) { 417 err = USB_ERR_INVAL; 418 break; 419 } 420 } else { 421 usb2_copy_out(xfer->frbuffers + 1, 422 0, data, temp); 423 } 424 } 425 } 426 /* 427 * Clear "frlengths[0]" so that we don't send the setup 428 * packet again: 429 */ 430 xfer->frlengths[0] = 0; 431 432 /* update length and data pointer */ 433 length -= temp; 434 data = USB_ADD_BYTES(data, temp); 435 436 if (actlen) { 437 (*actlen) += temp; 438 } 439 /* check for timeout */ 440 441 delta_ticks = ticks - start_ticks; 442 if (delta_ticks > max_ticks) { 443 if (!err) { 444 err = USB_ERR_TIMEOUT; 445 } 446 } 447 if (err) { 448 break; 449 } 450 } 451 452 if (err) { 453 /* 454 * Make sure that the control endpoint is no longer 455 * blocked in case of a non-transfer related error: 456 */ 457 usb2_transfer_stop(xfer); 458 } 459 USB_XFER_UNLOCK(xfer); 460 461 done: 462 sx_xunlock(udev->default_sx); 463 464 if (mtx) { 465 mtx_lock(mtx); 466 } 467 return ((usb2_error_t)err); 468 } 469 470 /*------------------------------------------------------------------------* 471 * usb2_do_request_proc - factored out code 472 * 473 * This function is factored out code. It does basically the same like 474 * usb2_do_request_flags, except it will check the status of the 475 * passed process argument before doing the USB request. If the 476 * process is draining the USB_ERR_IOERROR code will be returned. It 477 * is assumed that the mutex associated with the process is locked 478 * when calling this function. 479 *------------------------------------------------------------------------*/ 480 usb2_error_t 481 usb2_do_request_proc(struct usb2_device *udev, struct usb2_process *pproc, 482 struct usb2_device_request *req, void *data, uint32_t flags, 483 uint16_t *actlen, uint32_t timeout) 484 { 485 usb2_error_t err; 486 uint16_t len; 487 488 /* get request data length */ 489 len = UGETW(req->wLength); 490 491 /* check if the device is being detached */ 492 if (usb2_proc_is_gone(pproc)) { 493 err = USB_ERR_IOERROR; 494 goto done; 495 } 496 497 /* forward the USB request */ 498 err = usb2_do_request_flags(udev, pproc->up_mtx, 499 req, data, flags, actlen, timeout); 500 501 done: 502 /* on failure we zero the data */ 503 /* on short packet we zero the unused data */ 504 if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) { 505 if (err) 506 memset(data, 0, len); 507 else if (actlen && *actlen != len) 508 memset(((uint8_t *)data) + *actlen, 0, len - *actlen); 509 } 510 return (err); 511 } 512 513 /*------------------------------------------------------------------------* 514 * usb2_req_reset_port 515 * 516 * This function will instruct an USB HUB to perform a reset sequence 517 * on the specified port number. 518 * 519 * Returns: 520 * 0: Success. The USB device should now be at address zero. 521 * Else: Failure. No USB device is present and the USB port should be 522 * disabled. 523 *------------------------------------------------------------------------*/ 524 usb2_error_t 525 usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port) 526 { 527 struct usb2_port_status ps; 528 usb2_error_t err; 529 uint16_t n; 530 531 #if USB_DEBUG 532 uint16_t pr_poll_delay; 533 uint16_t pr_recovery_delay; 534 535 #endif 536 err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET); 537 if (err) { 538 goto done; 539 } 540 #if USB_DEBUG 541 /* range check input parameters */ 542 pr_poll_delay = usb2_pr_poll_delay; 543 if (pr_poll_delay < 1) { 544 pr_poll_delay = 1; 545 } else if (pr_poll_delay > 1000) { 546 pr_poll_delay = 1000; 547 } 548 pr_recovery_delay = usb2_pr_recovery_delay; 549 if (pr_recovery_delay > 1000) { 550 pr_recovery_delay = 1000; 551 } 552 #endif 553 n = 0; 554 while (1) { 555 #if USB_DEBUG 556 /* wait for the device to recover from reset */ 557 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay)); 558 n += pr_poll_delay; 559 #else 560 /* wait for the device to recover from reset */ 561 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY)); 562 n += USB_PORT_RESET_DELAY; 563 #endif 564 err = usb2_req_get_port_status(udev, mtx, &ps, port); 565 if (err) { 566 goto done; 567 } 568 /* if the device disappeared, just give up */ 569 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) { 570 goto done; 571 } 572 /* check if reset is complete */ 573 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) { 574 break; 575 } 576 /* check for timeout */ 577 if (n > 1000) { 578 n = 0; 579 break; 580 } 581 } 582 583 /* clear port reset first */ 584 err = usb2_req_clear_port_feature( 585 udev, mtx, port, UHF_C_PORT_RESET); 586 if (err) { 587 goto done; 588 } 589 /* check for timeout */ 590 if (n == 0) { 591 err = USB_ERR_TIMEOUT; 592 goto done; 593 } 594 #if USB_DEBUG 595 /* wait for the device to recover from reset */ 596 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay)); 597 #else 598 /* wait for the device to recover from reset */ 599 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY)); 600 #endif 601 602 done: 603 DPRINTFN(2, "port %d reset returning error=%s\n", 604 port, usb2_errstr(err)); 605 return (err); 606 } 607 608 /*------------------------------------------------------------------------* 609 * usb2_req_get_desc 610 * 611 * This function can be used to retrieve USB descriptors. It contains 612 * some additional logic like zeroing of missing descriptor bytes and 613 * retrying an USB descriptor in case of failure. The "min_len" 614 * argument specifies the minimum descriptor length. The "max_len" 615 * argument specifies the maximum descriptor length. If the real 616 * descriptor length is less than the minimum length the missing 617 * byte(s) will be zeroed. The type field, the second byte of the USB 618 * descriptor, will get forced to the correct type. If the "actlen" 619 * pointer is non-NULL, the actual length of the transfer will get 620 * stored in the 16-bit unsigned integer which it is pointing to. The 621 * first byte of the descriptor will not get updated. If the "actlen" 622 * pointer is NULL the first byte of the descriptor will get updated 623 * to reflect the actual length instead. If "min_len" is not equal to 624 * "max_len" then this function will try to retrive the beginning of 625 * the descriptor and base the maximum length on the first byte of the 626 * descriptor. 627 * 628 * Returns: 629 * 0: Success 630 * Else: Failure 631 *------------------------------------------------------------------------*/ 632 usb2_error_t 633 usb2_req_get_desc(struct usb2_device *udev, 634 struct mtx *mtx, uint16_t *actlen, void *desc, 635 uint16_t min_len, uint16_t max_len, 636 uint16_t id, uint8_t type, uint8_t index, 637 uint8_t retries) 638 { 639 struct usb2_device_request req; 640 uint8_t *buf; 641 usb2_error_t err; 642 643 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n", 644 id, type, index, max_len); 645 646 req.bmRequestType = UT_READ_DEVICE; 647 req.bRequest = UR_GET_DESCRIPTOR; 648 USETW2(req.wValue, type, index); 649 USETW(req.wIndex, id); 650 651 while (1) { 652 653 if ((min_len < 2) || (max_len < 2)) { 654 err = USB_ERR_INVAL; 655 goto done; 656 } 657 USETW(req.wLength, min_len); 658 659 err = usb2_do_request_flags(udev, mtx, &req, 660 desc, 0, NULL, 1000); 661 662 if (err) { 663 if (!retries) { 664 goto done; 665 } 666 retries--; 667 668 usb2_pause_mtx(mtx, hz / 5); 669 670 continue; 671 } 672 buf = desc; 673 674 if (min_len == max_len) { 675 676 /* enforce correct length */ 677 if ((buf[0] > min_len) && (actlen == NULL)) 678 buf[0] = min_len; 679 680 /* enforce correct type */ 681 buf[1] = type; 682 683 goto done; 684 } 685 /* range check */ 686 687 if (max_len > buf[0]) { 688 max_len = buf[0]; 689 } 690 /* zero minimum data */ 691 692 while (min_len > max_len) { 693 min_len--; 694 buf[min_len] = 0; 695 } 696 697 /* set new minimum length */ 698 699 min_len = max_len; 700 } 701 done: 702 if (actlen != NULL) { 703 if (err) 704 *actlen = 0; 705 else 706 *actlen = min_len; 707 } 708 return (err); 709 } 710 711 /*------------------------------------------------------------------------* 712 * usb2_req_get_string_any 713 * 714 * This function will return the string given by "string_index" 715 * using the first language ID. The maximum length "len" includes 716 * the terminating zero. The "len" argument should be twice as 717 * big pluss 2 bytes, compared with the actual maximum string length ! 718 * 719 * Returns: 720 * 0: Success 721 * Else: Failure 722 *------------------------------------------------------------------------*/ 723 usb2_error_t 724 usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf, 725 uint16_t len, uint8_t string_index) 726 { 727 char *s; 728 uint8_t *temp; 729 uint16_t i; 730 uint16_t n; 731 uint16_t c; 732 uint8_t swap; 733 usb2_error_t err; 734 735 if (len == 0) { 736 /* should not happen */ 737 return (USB_ERR_NORMAL_COMPLETION); 738 } 739 if (string_index == 0) { 740 /* this is the language table */ 741 buf[0] = 0; 742 return (USB_ERR_INVAL); 743 } 744 if (udev->flags.no_strings) { 745 buf[0] = 0; 746 return (USB_ERR_STALLED); 747 } 748 err = usb2_req_get_string_desc 749 (udev, mtx, buf, len, udev->langid, string_index); 750 if (err) { 751 buf[0] = 0; 752 return (err); 753 } 754 temp = (uint8_t *)buf; 755 756 if (temp[0] < 2) { 757 /* string length is too short */ 758 buf[0] = 0; 759 return (USB_ERR_INVAL); 760 } 761 /* reserve one byte for terminating zero */ 762 len--; 763 764 /* find maximum length */ 765 s = buf; 766 n = (temp[0] / 2) - 1; 767 if (n > len) { 768 n = len; 769 } 770 /* skip descriptor header */ 771 temp += 2; 772 773 /* reset swap state */ 774 swap = 3; 775 776 /* convert and filter */ 777 for (i = 0; (i != n); i++) { 778 c = UGETW(temp + (2 * i)); 779 780 /* convert from Unicode, handle buggy strings */ 781 if (((c & 0xff00) == 0) && (swap & 1)) { 782 /* Little Endian, default */ 783 *s = c; 784 swap = 1; 785 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 786 /* Big Endian */ 787 *s = c >> 8; 788 swap = 2; 789 } else { 790 /* silently skip bad character */ 791 continue; 792 } 793 794 /* 795 * Filter by default - we don't allow greater and less than 796 * signs because they might confuse the dmesg printouts! 797 */ 798 if ((*s == '<') || (*s == '>') || (!isprint(*s))) { 799 /* silently skip bad character */ 800 continue; 801 } 802 s++; 803 } 804 *s = 0; /* zero terminate resulting string */ 805 return (USB_ERR_NORMAL_COMPLETION); 806 } 807 808 /*------------------------------------------------------------------------* 809 * usb2_req_get_string_desc 810 * 811 * If you don't know the language ID, consider using 812 * "usb2_req_get_string_any()". 813 * 814 * Returns: 815 * 0: Success 816 * Else: Failure 817 *------------------------------------------------------------------------*/ 818 usb2_error_t 819 usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc, 820 uint16_t max_len, uint16_t lang_id, 821 uint8_t string_index) 822 { 823 return (usb2_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id, 824 UDESC_STRING, string_index, 0)); 825 } 826 827 /*------------------------------------------------------------------------* 828 * usb2_req_get_config_desc 829 * 830 * Returns: 831 * 0: Success 832 * Else: Failure 833 *------------------------------------------------------------------------*/ 834 usb2_error_t 835 usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx, 836 struct usb2_config_descriptor *d, uint8_t conf_index) 837 { 838 usb2_error_t err; 839 840 DPRINTFN(4, "confidx=%d\n", conf_index); 841 842 err = usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 843 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0); 844 if (err) { 845 goto done; 846 } 847 /* Extra sanity checking */ 848 if (UGETW(d->wTotalLength) < sizeof(*d)) { 849 err = USB_ERR_INVAL; 850 } 851 done: 852 return (err); 853 } 854 855 /*------------------------------------------------------------------------* 856 * usb2_req_get_config_desc_full 857 * 858 * This function gets the complete USB configuration descriptor and 859 * ensures that "wTotalLength" is correct. 860 * 861 * Returns: 862 * 0: Success 863 * Else: Failure 864 *------------------------------------------------------------------------*/ 865 usb2_error_t 866 usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx, 867 struct usb2_config_descriptor **ppcd, struct malloc_type *mtype, 868 uint8_t index) 869 { 870 struct usb2_config_descriptor cd; 871 struct usb2_config_descriptor *cdesc; 872 uint16_t len; 873 usb2_error_t err; 874 875 DPRINTFN(4, "index=%d\n", index); 876 877 *ppcd = NULL; 878 879 err = usb2_req_get_config_desc(udev, mtx, &cd, index); 880 if (err) { 881 return (err); 882 } 883 /* get full descriptor */ 884 len = UGETW(cd.wTotalLength); 885 if (len < sizeof(*cdesc)) { 886 /* corrupt descriptor */ 887 return (USB_ERR_INVAL); 888 } 889 cdesc = malloc(len, mtype, M_WAITOK); 890 if (cdesc == NULL) { 891 return (USB_ERR_NOMEM); 892 } 893 err = usb2_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0, 894 UDESC_CONFIG, index, 3); 895 if (err) { 896 free(cdesc, mtype); 897 return (err); 898 } 899 /* make sure that the device is not fooling us: */ 900 USETW(cdesc->wTotalLength, len); 901 902 *ppcd = cdesc; 903 904 return (0); /* success */ 905 } 906 907 /*------------------------------------------------------------------------* 908 * usb2_req_get_device_desc 909 * 910 * Returns: 911 * 0: Success 912 * Else: Failure 913 *------------------------------------------------------------------------*/ 914 usb2_error_t 915 usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx, 916 struct usb2_device_descriptor *d) 917 { 918 DPRINTFN(4, "\n"); 919 return (usb2_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 920 sizeof(*d), 0, UDESC_DEVICE, 0, 3)); 921 } 922 923 /*------------------------------------------------------------------------* 924 * usb2_req_get_alt_interface_no 925 * 926 * Returns: 927 * 0: Success 928 * Else: Failure 929 *------------------------------------------------------------------------*/ 930 usb2_error_t 931 usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx, 932 uint8_t *alt_iface_no, uint8_t iface_index) 933 { 934 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 935 struct usb2_device_request req; 936 937 if ((iface == NULL) || (iface->idesc == NULL)) { 938 return (USB_ERR_INVAL); 939 } 940 req.bmRequestType = UT_READ_INTERFACE; 941 req.bRequest = UR_GET_INTERFACE; 942 USETW(req.wValue, 0); 943 req.wIndex[0] = iface->idesc->bInterfaceNumber; 944 req.wIndex[1] = 0; 945 USETW(req.wLength, 1); 946 return (usb2_do_request(udev, mtx, &req, alt_iface_no)); 947 } 948 949 /*------------------------------------------------------------------------* 950 * usb2_req_set_alt_interface_no 951 * 952 * Returns: 953 * 0: Success 954 * Else: Failure 955 *------------------------------------------------------------------------*/ 956 usb2_error_t 957 usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx, 958 uint8_t iface_index, uint8_t alt_no) 959 { 960 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 961 struct usb2_device_request req; 962 963 if ((iface == NULL) || (iface->idesc == NULL)) { 964 return (USB_ERR_INVAL); 965 } 966 req.bmRequestType = UT_WRITE_INTERFACE; 967 req.bRequest = UR_SET_INTERFACE; 968 req.wValue[0] = alt_no; 969 req.wValue[1] = 0; 970 req.wIndex[0] = iface->idesc->bInterfaceNumber; 971 req.wIndex[1] = 0; 972 USETW(req.wLength, 0); 973 return (usb2_do_request(udev, mtx, &req, 0)); 974 } 975 976 /*------------------------------------------------------------------------* 977 * usb2_req_get_device_status 978 * 979 * Returns: 980 * 0: Success 981 * Else: Failure 982 *------------------------------------------------------------------------*/ 983 usb2_error_t 984 usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx, 985 struct usb2_status *st) 986 { 987 struct usb2_device_request req; 988 989 req.bmRequestType = UT_READ_DEVICE; 990 req.bRequest = UR_GET_STATUS; 991 USETW(req.wValue, 0); 992 USETW(req.wIndex, 0); 993 USETW(req.wLength, sizeof(*st)); 994 return (usb2_do_request(udev, mtx, &req, st)); 995 } 996 997 /*------------------------------------------------------------------------* 998 * usb2_req_get_hub_descriptor 999 * 1000 * Returns: 1001 * 0: Success 1002 * Else: Failure 1003 *------------------------------------------------------------------------*/ 1004 usb2_error_t 1005 usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx, 1006 struct usb2_hub_descriptor *hd, uint8_t nports) 1007 { 1008 struct usb2_device_request req; 1009 uint16_t len = (nports + 7 + (8 * 8)) / 8; 1010 1011 req.bmRequestType = UT_READ_CLASS_DEVICE; 1012 req.bRequest = UR_GET_DESCRIPTOR; 1013 USETW2(req.wValue, UDESC_HUB, 0); 1014 USETW(req.wIndex, 0); 1015 USETW(req.wLength, len); 1016 return (usb2_do_request(udev, mtx, &req, hd)); 1017 } 1018 1019 /*------------------------------------------------------------------------* 1020 * usb2_req_get_hub_status 1021 * 1022 * Returns: 1023 * 0: Success 1024 * Else: Failure 1025 *------------------------------------------------------------------------*/ 1026 usb2_error_t 1027 usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx, 1028 struct usb2_hub_status *st) 1029 { 1030 struct usb2_device_request req; 1031 1032 req.bmRequestType = UT_READ_CLASS_DEVICE; 1033 req.bRequest = UR_GET_STATUS; 1034 USETW(req.wValue, 0); 1035 USETW(req.wIndex, 0); 1036 USETW(req.wLength, sizeof(struct usb2_hub_status)); 1037 return (usb2_do_request(udev, mtx, &req, st)); 1038 } 1039 1040 /*------------------------------------------------------------------------* 1041 * usb2_req_set_address 1042 * 1043 * This function is used to set the address for an USB device. After 1044 * port reset the USB device will respond at address zero. 1045 * 1046 * Returns: 1047 * 0: Success 1048 * Else: Failure 1049 *------------------------------------------------------------------------*/ 1050 usb2_error_t 1051 usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr) 1052 { 1053 struct usb2_device_request req; 1054 1055 DPRINTFN(6, "setting device address=%d\n", addr); 1056 1057 req.bmRequestType = UT_WRITE_DEVICE; 1058 req.bRequest = UR_SET_ADDRESS; 1059 USETW(req.wValue, addr); 1060 USETW(req.wIndex, 0); 1061 USETW(req.wLength, 0); 1062 1063 /* Setting the address should not take more than 1 second ! */ 1064 return (usb2_do_request_flags(udev, mtx, &req, NULL, 1065 USB_DELAY_STATUS_STAGE, NULL, 1000)); 1066 } 1067 1068 /*------------------------------------------------------------------------* 1069 * usb2_req_get_port_status 1070 * 1071 * Returns: 1072 * 0: Success 1073 * Else: Failure 1074 *------------------------------------------------------------------------*/ 1075 usb2_error_t 1076 usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx, 1077 struct usb2_port_status *ps, uint8_t port) 1078 { 1079 struct usb2_device_request req; 1080 1081 req.bmRequestType = UT_READ_CLASS_OTHER; 1082 req.bRequest = UR_GET_STATUS; 1083 USETW(req.wValue, 0); 1084 req.wIndex[0] = port; 1085 req.wIndex[1] = 0; 1086 USETW(req.wLength, sizeof *ps); 1087 return (usb2_do_request(udev, mtx, &req, ps)); 1088 } 1089 1090 /*------------------------------------------------------------------------* 1091 * usb2_req_clear_hub_feature 1092 * 1093 * Returns: 1094 * 0: Success 1095 * Else: Failure 1096 *------------------------------------------------------------------------*/ 1097 usb2_error_t 1098 usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx, 1099 uint16_t sel) 1100 { 1101 struct usb2_device_request req; 1102 1103 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1104 req.bRequest = UR_CLEAR_FEATURE; 1105 USETW(req.wValue, sel); 1106 USETW(req.wIndex, 0); 1107 USETW(req.wLength, 0); 1108 return (usb2_do_request(udev, mtx, &req, 0)); 1109 } 1110 1111 /*------------------------------------------------------------------------* 1112 * usb2_req_set_hub_feature 1113 * 1114 * Returns: 1115 * 0: Success 1116 * Else: Failure 1117 *------------------------------------------------------------------------*/ 1118 usb2_error_t 1119 usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx, 1120 uint16_t sel) 1121 { 1122 struct usb2_device_request req; 1123 1124 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1125 req.bRequest = UR_SET_FEATURE; 1126 USETW(req.wValue, sel); 1127 USETW(req.wIndex, 0); 1128 USETW(req.wLength, 0); 1129 return (usb2_do_request(udev, mtx, &req, 0)); 1130 } 1131 1132 /*------------------------------------------------------------------------* 1133 * usb2_req_clear_port_feature 1134 * 1135 * Returns: 1136 * 0: Success 1137 * Else: Failure 1138 *------------------------------------------------------------------------*/ 1139 usb2_error_t 1140 usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx, 1141 uint8_t port, uint16_t sel) 1142 { 1143 struct usb2_device_request req; 1144 1145 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1146 req.bRequest = UR_CLEAR_FEATURE; 1147 USETW(req.wValue, sel); 1148 req.wIndex[0] = port; 1149 req.wIndex[1] = 0; 1150 USETW(req.wLength, 0); 1151 return (usb2_do_request(udev, mtx, &req, 0)); 1152 } 1153 1154 /*------------------------------------------------------------------------* 1155 * usb2_req_set_port_feature 1156 * 1157 * Returns: 1158 * 0: Success 1159 * Else: Failure 1160 *------------------------------------------------------------------------*/ 1161 usb2_error_t 1162 usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx, 1163 uint8_t port, uint16_t sel) 1164 { 1165 struct usb2_device_request req; 1166 1167 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1168 req.bRequest = UR_SET_FEATURE; 1169 USETW(req.wValue, sel); 1170 req.wIndex[0] = port; 1171 req.wIndex[1] = 0; 1172 USETW(req.wLength, 0); 1173 return (usb2_do_request(udev, mtx, &req, 0)); 1174 } 1175 1176 /*------------------------------------------------------------------------* 1177 * usb2_req_set_protocol 1178 * 1179 * Returns: 1180 * 0: Success 1181 * Else: Failure 1182 *------------------------------------------------------------------------*/ 1183 usb2_error_t 1184 usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx, 1185 uint8_t iface_index, uint16_t report) 1186 { 1187 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1188 struct usb2_device_request req; 1189 1190 if ((iface == NULL) || (iface->idesc == NULL)) { 1191 return (USB_ERR_INVAL); 1192 } 1193 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n", 1194 iface, report, iface->idesc->bInterfaceNumber); 1195 1196 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1197 req.bRequest = UR_SET_PROTOCOL; 1198 USETW(req.wValue, report); 1199 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1200 req.wIndex[1] = 0; 1201 USETW(req.wLength, 0); 1202 return (usb2_do_request(udev, mtx, &req, 0)); 1203 } 1204 1205 /*------------------------------------------------------------------------* 1206 * usb2_req_set_report 1207 * 1208 * Returns: 1209 * 0: Success 1210 * Else: Failure 1211 *------------------------------------------------------------------------*/ 1212 usb2_error_t 1213 usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len, 1214 uint8_t iface_index, uint8_t type, uint8_t id) 1215 { 1216 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1217 struct usb2_device_request req; 1218 1219 if ((iface == NULL) || (iface->idesc == NULL)) { 1220 return (USB_ERR_INVAL); 1221 } 1222 DPRINTFN(5, "len=%d\n", len); 1223 1224 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1225 req.bRequest = UR_SET_REPORT; 1226 USETW2(req.wValue, type, id); 1227 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1228 req.wIndex[1] = 0; 1229 USETW(req.wLength, len); 1230 return (usb2_do_request(udev, mtx, &req, data)); 1231 } 1232 1233 /*------------------------------------------------------------------------* 1234 * usb2_req_get_report 1235 * 1236 * Returns: 1237 * 0: Success 1238 * Else: Failure 1239 *------------------------------------------------------------------------*/ 1240 usb2_error_t 1241 usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data, 1242 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id) 1243 { 1244 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1245 struct usb2_device_request req; 1246 1247 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) { 1248 return (USB_ERR_INVAL); 1249 } 1250 DPRINTFN(5, "len=%d\n", len); 1251 1252 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1253 req.bRequest = UR_GET_REPORT; 1254 USETW2(req.wValue, type, id); 1255 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1256 req.wIndex[1] = 0; 1257 USETW(req.wLength, len); 1258 return (usb2_do_request(udev, mtx, &req, data)); 1259 } 1260 1261 /*------------------------------------------------------------------------* 1262 * usb2_req_set_idle 1263 * 1264 * Returns: 1265 * 0: Success 1266 * Else: Failure 1267 *------------------------------------------------------------------------*/ 1268 usb2_error_t 1269 usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx, 1270 uint8_t iface_index, uint8_t duration, uint8_t id) 1271 { 1272 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1273 struct usb2_device_request req; 1274 1275 if ((iface == NULL) || (iface->idesc == NULL)) { 1276 return (USB_ERR_INVAL); 1277 } 1278 DPRINTFN(5, "%d %d\n", duration, id); 1279 1280 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1281 req.bRequest = UR_SET_IDLE; 1282 USETW2(req.wValue, duration, id); 1283 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1284 req.wIndex[1] = 0; 1285 USETW(req.wLength, 0); 1286 return (usb2_do_request(udev, mtx, &req, 0)); 1287 } 1288 1289 /*------------------------------------------------------------------------* 1290 * usb2_req_get_report_descriptor 1291 * 1292 * Returns: 1293 * 0: Success 1294 * Else: Failure 1295 *------------------------------------------------------------------------*/ 1296 usb2_error_t 1297 usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx, 1298 void *d, uint16_t size, uint8_t iface_index) 1299 { 1300 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1301 struct usb2_device_request req; 1302 1303 if ((iface == NULL) || (iface->idesc == NULL)) { 1304 return (USB_ERR_INVAL); 1305 } 1306 req.bmRequestType = UT_READ_INTERFACE; 1307 req.bRequest = UR_GET_DESCRIPTOR; 1308 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 1309 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1310 req.wIndex[1] = 0; 1311 USETW(req.wLength, size); 1312 return (usb2_do_request(udev, mtx, &req, d)); 1313 } 1314 1315 /*------------------------------------------------------------------------* 1316 * usb2_req_set_config 1317 * 1318 * This function is used to select the current configuration number in 1319 * both USB device side mode and USB host side mode. When setting the 1320 * configuration the function of the interfaces can change. 1321 * 1322 * Returns: 1323 * 0: Success 1324 * Else: Failure 1325 *------------------------------------------------------------------------*/ 1326 usb2_error_t 1327 usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf) 1328 { 1329 struct usb2_device_request req; 1330 1331 DPRINTF("setting config %d\n", conf); 1332 1333 /* do "set configuration" request */ 1334 1335 req.bmRequestType = UT_WRITE_DEVICE; 1336 req.bRequest = UR_SET_CONFIG; 1337 req.wValue[0] = conf; 1338 req.wValue[1] = 0; 1339 USETW(req.wIndex, 0); 1340 USETW(req.wLength, 0); 1341 return (usb2_do_request(udev, mtx, &req, 0)); 1342 } 1343 1344 /*------------------------------------------------------------------------* 1345 * usb2_req_get_config 1346 * 1347 * Returns: 1348 * 0: Success 1349 * Else: Failure 1350 *------------------------------------------------------------------------*/ 1351 usb2_error_t 1352 usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf) 1353 { 1354 struct usb2_device_request req; 1355 1356 req.bmRequestType = UT_READ_DEVICE; 1357 req.bRequest = UR_GET_CONFIG; 1358 USETW(req.wValue, 0); 1359 USETW(req.wIndex, 0); 1360 USETW(req.wLength, 1); 1361 return (usb2_do_request(udev, mtx, &req, pconf)); 1362 } 1363 1364 /*------------------------------------------------------------------------* 1365 * usb2_req_re_enumerate 1366 * 1367 * NOTE: After this function returns the hardware is in the 1368 * unconfigured state! The application is responsible for setting a 1369 * new configuration. 1370 * 1371 * Returns: 1372 * 0: Success 1373 * Else: Failure 1374 *------------------------------------------------------------------------*/ 1375 usb2_error_t 1376 usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx) 1377 { 1378 struct usb2_device *parent_hub; 1379 usb2_error_t err; 1380 uint8_t old_addr; 1381 uint8_t do_retry = 1; 1382 1383 if (udev->flags.usb2_mode != USB_MODE_HOST) { 1384 return (USB_ERR_INVAL); 1385 } 1386 old_addr = udev->address; 1387 parent_hub = udev->parent_hub; 1388 if (parent_hub == NULL) { 1389 return (USB_ERR_INVAL); 1390 } 1391 retry: 1392 err = usb2_req_reset_port(parent_hub, mtx, udev->port_no); 1393 if (err) { 1394 DPRINTFN(0, "addr=%d, port reset failed\n", old_addr); 1395 goto done; 1396 } 1397 /* 1398 * After that the port has been reset our device should be at 1399 * address zero: 1400 */ 1401 udev->address = USB_START_ADDR; 1402 1403 /* reset "bMaxPacketSize" */ 1404 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1405 1406 /* 1407 * Restore device address: 1408 */ 1409 err = usb2_req_set_address(udev, mtx, old_addr); 1410 if (err) { 1411 /* XXX ignore any errors! */ 1412 DPRINTFN(0, "addr=%d, set address failed! (ignored)\n", 1413 old_addr); 1414 } 1415 /* restore device address */ 1416 udev->address = old_addr; 1417 1418 /* allow device time to set new address */ 1419 usb2_pause_mtx(mtx, USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1420 1421 /* get the device descriptor */ 1422 err = usb2_req_get_desc(udev, mtx, NULL, &udev->ddesc, 1423 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1424 if (err) { 1425 DPRINTFN(0, "getting device descriptor " 1426 "at addr %d failed!\n", udev->address); 1427 goto done; 1428 } 1429 /* get the full device descriptor */ 1430 err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc); 1431 if (err) { 1432 DPRINTFN(0, "addr=%d, getting device " 1433 "descriptor failed!\n", old_addr); 1434 goto done; 1435 } 1436 done: 1437 if (err && do_retry) { 1438 /* give the USB firmware some time to load */ 1439 usb2_pause_mtx(mtx, hz / 2); 1440 /* no more retries after this retry */ 1441 do_retry = 0; 1442 /* try again */ 1443 goto retry; 1444 } 1445 /* restore address */ 1446 udev->address = old_addr; 1447 return (err); 1448 } 1449 1450 /*------------------------------------------------------------------------* 1451 * usb2_req_clear_device_feature 1452 * 1453 * Returns: 1454 * 0: Success 1455 * Else: Failure 1456 *------------------------------------------------------------------------*/ 1457 usb2_error_t 1458 usb2_req_clear_device_feature(struct usb2_device *udev, struct mtx *mtx, 1459 uint16_t sel) 1460 { 1461 struct usb2_device_request req; 1462 1463 req.bmRequestType = UT_WRITE_DEVICE; 1464 req.bRequest = UR_CLEAR_FEATURE; 1465 USETW(req.wValue, sel); 1466 USETW(req.wIndex, 0); 1467 USETW(req.wLength, 0); 1468 return (usb2_do_request(udev, mtx, &req, 0)); 1469 } 1470 1471 /*------------------------------------------------------------------------* 1472 * usb2_req_set_device_feature 1473 * 1474 * Returns: 1475 * 0: Success 1476 * Else: Failure 1477 *------------------------------------------------------------------------*/ 1478 usb2_error_t 1479 usb2_req_set_device_feature(struct usb2_device *udev, struct mtx *mtx, 1480 uint16_t sel) 1481 { 1482 struct usb2_device_request req; 1483 1484 req.bmRequestType = UT_WRITE_DEVICE; 1485 req.bRequest = UR_SET_FEATURE; 1486 USETW(req.wValue, sel); 1487 USETW(req.wIndex, 0); 1488 USETW(req.wLength, 0); 1489 return (usb2_do_request(udev, mtx, &req, 0)); 1490 } 1491