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