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