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