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