usb_transfer.c revision 246122
1/* $FreeBSD: head/sys/dev/usb/usb_transfer.c 246122 2013-01-30 15:26:04Z hselasky $ */ 2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#ifdef USB_GLOBAL_INCLUDE_FILE 28#include USB_GLOBAL_INCLUDE_FILE 29#else 30#include <sys/stdint.h> 31#include <sys/stddef.h> 32#include <sys/param.h> 33#include <sys/queue.h> 34#include <sys/types.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/bus.h> 38#include <sys/module.h> 39#include <sys/lock.h> 40#include <sys/mutex.h> 41#include <sys/condvar.h> 42#include <sys/sysctl.h> 43#include <sys/sx.h> 44#include <sys/unistd.h> 45#include <sys/callout.h> 46#include <sys/malloc.h> 47#include <sys/priv.h> 48#include <sys/proc.h> 49 50#include <dev/usb/usb.h> 51#include <dev/usb/usbdi.h> 52#include <dev/usb/usbdi_util.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_process.h> 59#include <dev/usb/usb_transfer.h> 60#include <dev/usb/usb_device.h> 61#include <dev/usb/usb_debug.h> 62#include <dev/usb/usb_util.h> 63 64#include <dev/usb/usb_controller.h> 65#include <dev/usb/usb_bus.h> 66#include <dev/usb/usb_pf.h> 67#endif /* USB_GLOBAL_INCLUDE_FILE */ 68 69struct usb_std_packet_size { 70 struct { 71 uint16_t min; /* inclusive */ 72 uint16_t max; /* inclusive */ 73 } range; 74 75 uint16_t fixed[4]; 76}; 77 78static usb_callback_t usb_request_callback; 79 80static const struct usb_config usb_control_ep_cfg[USB_CTRL_XFER_MAX] = { 81 82 /* This transfer is used for generic control endpoint transfers */ 83 84 [0] = { 85 .type = UE_CONTROL, 86 .endpoint = 0x00, /* Control endpoint */ 87 .direction = UE_DIR_ANY, 88 .bufsize = USB_EP0_BUFSIZE, /* bytes */ 89 .flags = {.proxy_buffer = 1,}, 90 .callback = &usb_request_callback, 91 .usb_mode = USB_MODE_DUAL, /* both modes */ 92 }, 93 94 /* This transfer is used for generic clear stall only */ 95 96 [1] = { 97 .type = UE_CONTROL, 98 .endpoint = 0x00, /* Control pipe */ 99 .direction = UE_DIR_ANY, 100 .bufsize = sizeof(struct usb_device_request), 101 .callback = &usb_do_clear_stall_callback, 102 .timeout = 1000, /* 1 second */ 103 .interval = 50, /* 50ms */ 104 .usb_mode = USB_MODE_HOST, 105 }, 106}; 107 108/* function prototypes */ 109 110static void usbd_update_max_frame_size(struct usb_xfer *); 111static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t); 112static void usbd_control_transfer_init(struct usb_xfer *); 113static int usbd_setup_ctrl_transfer(struct usb_xfer *); 114static void usb_callback_proc(struct usb_proc_msg *); 115static void usbd_callback_ss_done_defer(struct usb_xfer *); 116static void usbd_callback_wrapper(struct usb_xfer_queue *); 117static void usbd_transfer_start_cb(void *); 118static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *); 119static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr, 120 uint8_t type, enum usb_dev_speed speed); 121 122/*------------------------------------------------------------------------* 123 * usb_request_callback 124 *------------------------------------------------------------------------*/ 125static void 126usb_request_callback(struct usb_xfer *xfer, usb_error_t error) 127{ 128 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) 129 usb_handle_request_callback(xfer, error); 130 else 131 usbd_do_request_callback(xfer, error); 132} 133 134/*------------------------------------------------------------------------* 135 * usbd_update_max_frame_size 136 * 137 * This function updates the maximum frame size, hence high speed USB 138 * can transfer multiple consecutive packets. 139 *------------------------------------------------------------------------*/ 140static void 141usbd_update_max_frame_size(struct usb_xfer *xfer) 142{ 143 /* compute maximum frame size */ 144 /* this computation should not overflow 16-bit */ 145 /* max = 15 * 1024 */ 146 147 xfer->max_frame_size = xfer->max_packet_size * xfer->max_packet_count; 148} 149 150/*------------------------------------------------------------------------* 151 * usbd_get_dma_delay 152 * 153 * The following function is called when we need to 154 * synchronize with DMA hardware. 155 * 156 * Returns: 157 * 0: no DMA delay required 158 * Else: milliseconds of DMA delay 159 *------------------------------------------------------------------------*/ 160usb_timeout_t 161usbd_get_dma_delay(struct usb_device *udev) 162{ 163 struct usb_bus_methods *mtod; 164 uint32_t temp; 165 166 mtod = udev->bus->methods; 167 temp = 0; 168 169 if (mtod->get_dma_delay) { 170 (mtod->get_dma_delay) (udev, &temp); 171 /* 172 * Round up and convert to milliseconds. Note that we use 173 * 1024 milliseconds per second. to save a division. 174 */ 175 temp += 0x3FF; 176 temp /= 0x400; 177 } 178 return (temp); 179} 180 181/*------------------------------------------------------------------------* 182 * usbd_transfer_setup_sub_malloc 183 * 184 * This function will allocate one or more DMA'able memory chunks 185 * according to "size", "align" and "count" arguments. "ppc" is 186 * pointed to a linear array of USB page caches afterwards. 187 * 188 * If the "align" argument is equal to "1" a non-contiguous allocation 189 * can happen. Else if the "align" argument is greater than "1", the 190 * allocation will always be contiguous in memory. 191 * 192 * Returns: 193 * 0: Success 194 * Else: Failure 195 *------------------------------------------------------------------------*/ 196#if USB_HAVE_BUSDMA 197uint8_t 198usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm, 199 struct usb_page_cache **ppc, usb_size_t size, usb_size_t align, 200 usb_size_t count) 201{ 202 struct usb_page_cache *pc; 203 struct usb_page *pg; 204 void *buf; 205 usb_size_t n_dma_pc; 206 usb_size_t n_dma_pg; 207 usb_size_t n_obj; 208 usb_size_t x; 209 usb_size_t y; 210 usb_size_t r; 211 usb_size_t z; 212 213 USB_ASSERT(align > 0, ("Invalid alignment, 0x%08x\n", 214 align)); 215 USB_ASSERT(size > 0, ("Invalid size = 0\n")); 216 217 if (count == 0) { 218 return (0); /* nothing to allocate */ 219 } 220 /* 221 * Make sure that the size is aligned properly. 222 */ 223 size = -((-size) & (-align)); 224 225 /* 226 * Try multi-allocation chunks to reduce the number of DMA 227 * allocations, hence DMA allocations are slow. 228 */ 229 if (align == 1) { 230 /* special case - non-cached multi page DMA memory */ 231 n_dma_pc = count; 232 n_dma_pg = (2 + (size / USB_PAGE_SIZE)); 233 n_obj = 1; 234 } else if (size >= USB_PAGE_SIZE) { 235 n_dma_pc = count; 236 n_dma_pg = 1; 237 n_obj = 1; 238 } else { 239 /* compute number of objects per page */ 240 n_obj = (USB_PAGE_SIZE / size); 241 /* 242 * Compute number of DMA chunks, rounded up 243 * to nearest one: 244 */ 245 n_dma_pc = ((count + n_obj - 1) / n_obj); 246 n_dma_pg = 1; 247 } 248 249 /* 250 * DMA memory is allocated once, but mapped twice. That's why 251 * there is one list for auto-free and another list for 252 * non-auto-free which only holds the mapping and not the 253 * allocation. 254 */ 255 if (parm->buf == NULL) { 256 /* reserve memory (auto-free) */ 257 parm->dma_page_ptr += n_dma_pc * n_dma_pg; 258 parm->dma_page_cache_ptr += n_dma_pc; 259 260 /* reserve memory (no-auto-free) */ 261 parm->dma_page_ptr += count * n_dma_pg; 262 parm->xfer_page_cache_ptr += count; 263 return (0); 264 } 265 for (x = 0; x != n_dma_pc; x++) { 266 /* need to initialize the page cache */ 267 parm->dma_page_cache_ptr[x].tag_parent = 268 &parm->curr_xfer->xroot->dma_parent_tag; 269 } 270 for (x = 0; x != count; x++) { 271 /* need to initialize the page cache */ 272 parm->xfer_page_cache_ptr[x].tag_parent = 273 &parm->curr_xfer->xroot->dma_parent_tag; 274 } 275 276 if (ppc) { 277 *ppc = parm->xfer_page_cache_ptr; 278 } 279 r = count; /* set remainder count */ 280 z = n_obj * size; /* set allocation size */ 281 pc = parm->xfer_page_cache_ptr; 282 pg = parm->dma_page_ptr; 283 284 for (x = 0; x != n_dma_pc; x++) { 285 286 if (r < n_obj) { 287 /* compute last remainder */ 288 z = r * size; 289 n_obj = r; 290 } 291 if (usb_pc_alloc_mem(parm->dma_page_cache_ptr, 292 pg, z, align)) { 293 return (1); /* failure */ 294 } 295 /* Set beginning of current buffer */ 296 buf = parm->dma_page_cache_ptr->buffer; 297 /* Make room for one DMA page cache and one page */ 298 parm->dma_page_cache_ptr++; 299 pg += n_dma_pg; 300 301 for (y = 0; (y != n_obj); y++, r--, pc++, pg += n_dma_pg) { 302 303 /* Load sub-chunk into DMA */ 304 if (usb_pc_dmamap_create(pc, size)) { 305 return (1); /* failure */ 306 } 307 pc->buffer = USB_ADD_BYTES(buf, y * size); 308 pc->page_start = pg; 309 310 mtx_lock(pc->tag_parent->mtx); 311 if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) { 312 mtx_unlock(pc->tag_parent->mtx); 313 return (1); /* failure */ 314 } 315 mtx_unlock(pc->tag_parent->mtx); 316 } 317 } 318 319 parm->xfer_page_cache_ptr = pc; 320 parm->dma_page_ptr = pg; 321 return (0); 322} 323#endif 324 325/*------------------------------------------------------------------------* 326 * usbd_transfer_setup_sub - transfer setup subroutine 327 * 328 * This function must be called from the "xfer_setup" callback of the 329 * USB Host or Device controller driver when setting up an USB 330 * transfer. This function will setup correct packet sizes, buffer 331 * sizes, flags and more, that are stored in the "usb_xfer" 332 * structure. 333 *------------------------------------------------------------------------*/ 334void 335usbd_transfer_setup_sub(struct usb_setup_params *parm) 336{ 337 enum { 338 REQ_SIZE = 8, 339 MIN_PKT = 8, 340 }; 341 struct usb_xfer *xfer = parm->curr_xfer; 342 const struct usb_config *setup = parm->curr_setup; 343 struct usb_endpoint_ss_comp_descriptor *ecomp; 344 struct usb_endpoint_descriptor *edesc; 345 struct usb_std_packet_size std_size; 346 usb_frcount_t n_frlengths; 347 usb_frcount_t n_frbuffers; 348 usb_frcount_t x; 349 uint8_t type; 350 uint8_t zmps; 351 352 /* 353 * Sanity check. The following parameters must be initialized before 354 * calling this function. 355 */ 356 if ((parm->hc_max_packet_size == 0) || 357 (parm->hc_max_packet_count == 0) || 358 (parm->hc_max_frame_size == 0)) { 359 parm->err = USB_ERR_INVAL; 360 goto done; 361 } 362 edesc = xfer->endpoint->edesc; 363 ecomp = xfer->endpoint->ecomp; 364 365 type = (edesc->bmAttributes & UE_XFERTYPE); 366 367 xfer->flags = setup->flags; 368 xfer->nframes = setup->frames; 369 xfer->timeout = setup->timeout; 370 xfer->callback = setup->callback; 371 xfer->interval = setup->interval; 372 xfer->endpointno = edesc->bEndpointAddress; 373 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize); 374 xfer->max_packet_count = 1; 375 /* make a shadow copy: */ 376 xfer->flags_int.usb_mode = parm->udev->flags.usb_mode; 377 378 parm->bufsize = setup->bufsize; 379 380 switch (parm->speed) { 381 case USB_SPEED_HIGH: 382 switch (type) { 383 case UE_ISOCHRONOUS: 384 case UE_INTERRUPT: 385 xfer->max_packet_count += 386 (xfer->max_packet_size >> 11) & 3; 387 388 /* check for invalid max packet count */ 389 if (xfer->max_packet_count > 3) 390 xfer->max_packet_count = 3; 391 break; 392 default: 393 break; 394 } 395 xfer->max_packet_size &= 0x7FF; 396 break; 397 case USB_SPEED_SUPER: 398 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3; 399 400 if (ecomp != NULL) 401 xfer->max_packet_count += ecomp->bMaxBurst; 402 403 if ((xfer->max_packet_count == 0) || 404 (xfer->max_packet_count > 16)) 405 xfer->max_packet_count = 16; 406 407 switch (type) { 408 case UE_CONTROL: 409 xfer->max_packet_count = 1; 410 break; 411 case UE_ISOCHRONOUS: 412 if (ecomp != NULL) { 413 uint8_t mult; 414 415 mult = UE_GET_SS_ISO_MULT( 416 ecomp->bmAttributes) + 1; 417 if (mult > 3) 418 mult = 3; 419 420 xfer->max_packet_count *= mult; 421 } 422 break; 423 default: 424 break; 425 } 426 xfer->max_packet_size &= 0x7FF; 427 break; 428 default: 429 break; 430 } 431 /* range check "max_packet_count" */ 432 433 if (xfer->max_packet_count > parm->hc_max_packet_count) { 434 xfer->max_packet_count = parm->hc_max_packet_count; 435 } 436 /* filter "wMaxPacketSize" according to HC capabilities */ 437 438 if ((xfer->max_packet_size > parm->hc_max_packet_size) || 439 (xfer->max_packet_size == 0)) { 440 xfer->max_packet_size = parm->hc_max_packet_size; 441 } 442 /* filter "wMaxPacketSize" according to standard sizes */ 443 444 usbd_get_std_packet_size(&std_size, type, parm->speed); 445 446 if (std_size.range.min || std_size.range.max) { 447 448 if (xfer->max_packet_size < std_size.range.min) { 449 xfer->max_packet_size = std_size.range.min; 450 } 451 if (xfer->max_packet_size > std_size.range.max) { 452 xfer->max_packet_size = std_size.range.max; 453 } 454 } else { 455 456 if (xfer->max_packet_size >= std_size.fixed[3]) { 457 xfer->max_packet_size = std_size.fixed[3]; 458 } else if (xfer->max_packet_size >= std_size.fixed[2]) { 459 xfer->max_packet_size = std_size.fixed[2]; 460 } else if (xfer->max_packet_size >= std_size.fixed[1]) { 461 xfer->max_packet_size = std_size.fixed[1]; 462 } else { 463 /* only one possibility left */ 464 xfer->max_packet_size = std_size.fixed[0]; 465 } 466 } 467 468 /* compute "max_frame_size" */ 469 470 usbd_update_max_frame_size(xfer); 471 472 /* check interrupt interval and transfer pre-delay */ 473 474 if (type == UE_ISOCHRONOUS) { 475 476 uint16_t frame_limit; 477 478 xfer->interval = 0; /* not used, must be zero */ 479 xfer->flags_int.isochronous_xfr = 1; /* set flag */ 480 481 if (xfer->timeout == 0) { 482 /* 483 * set a default timeout in 484 * case something goes wrong! 485 */ 486 xfer->timeout = 1000 / 4; 487 } 488 switch (parm->speed) { 489 case USB_SPEED_LOW: 490 case USB_SPEED_FULL: 491 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER; 492 xfer->fps_shift = 0; 493 break; 494 default: 495 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER; 496 xfer->fps_shift = edesc->bInterval; 497 if (xfer->fps_shift > 0) 498 xfer->fps_shift--; 499 if (xfer->fps_shift > 3) 500 xfer->fps_shift = 3; 501 if (xfer->flags.pre_scale_frames != 0) 502 xfer->nframes <<= (3 - xfer->fps_shift); 503 break; 504 } 505 506 if (xfer->nframes > frame_limit) { 507 /* 508 * this is not going to work 509 * cross hardware 510 */ 511 parm->err = USB_ERR_INVAL; 512 goto done; 513 } 514 if (xfer->nframes == 0) { 515 /* 516 * this is not a valid value 517 */ 518 parm->err = USB_ERR_ZERO_NFRAMES; 519 goto done; 520 } 521 } else { 522 523 /* 524 * If a value is specified use that else check the 525 * endpoint descriptor! 526 */ 527 if (type == UE_INTERRUPT) { 528 529 uint32_t temp; 530 531 if (xfer->interval == 0) { 532 533 xfer->interval = edesc->bInterval; 534 535 switch (parm->speed) { 536 case USB_SPEED_LOW: 537 case USB_SPEED_FULL: 538 break; 539 default: 540 /* 125us -> 1ms */ 541 if (xfer->interval < 4) 542 xfer->interval = 1; 543 else if (xfer->interval > 16) 544 xfer->interval = (1 << (16 - 4)); 545 else 546 xfer->interval = 547 (1 << (xfer->interval - 4)); 548 break; 549 } 550 } 551 552 if (xfer->interval == 0) { 553 /* 554 * One millisecond is the smallest 555 * interval we support: 556 */ 557 xfer->interval = 1; 558 } 559 560 xfer->fps_shift = 0; 561 temp = 1; 562 563 while ((temp != 0) && (temp < xfer->interval)) { 564 xfer->fps_shift++; 565 temp *= 2; 566 } 567 568 switch (parm->speed) { 569 case USB_SPEED_LOW: 570 case USB_SPEED_FULL: 571 break; 572 default: 573 xfer->fps_shift += 3; 574 break; 575 } 576 } 577 } 578 579 /* 580 * NOTE: we do not allow "max_packet_size" or "max_frame_size" 581 * to be equal to zero when setting up USB transfers, hence 582 * this leads to alot of extra code in the USB kernel. 583 */ 584 585 if ((xfer->max_frame_size == 0) || 586 (xfer->max_packet_size == 0)) { 587 588 zmps = 1; 589 590 if ((parm->bufsize <= MIN_PKT) && 591 (type != UE_CONTROL) && 592 (type != UE_BULK)) { 593 594 /* workaround */ 595 xfer->max_packet_size = MIN_PKT; 596 xfer->max_packet_count = 1; 597 parm->bufsize = 0; /* automatic setup length */ 598 usbd_update_max_frame_size(xfer); 599 600 } else { 601 parm->err = USB_ERR_ZERO_MAXP; 602 goto done; 603 } 604 605 } else { 606 zmps = 0; 607 } 608 609 /* 610 * check if we should setup a default 611 * length: 612 */ 613 614 if (parm->bufsize == 0) { 615 616 parm->bufsize = xfer->max_frame_size; 617 618 if (type == UE_ISOCHRONOUS) { 619 parm->bufsize *= xfer->nframes; 620 } 621 } 622 /* 623 * check if we are about to setup a proxy 624 * type of buffer: 625 */ 626 627 if (xfer->flags.proxy_buffer) { 628 629 /* round bufsize up */ 630 631 parm->bufsize += (xfer->max_frame_size - 1); 632 633 if (parm->bufsize < xfer->max_frame_size) { 634 /* length wrapped around */ 635 parm->err = USB_ERR_INVAL; 636 goto done; 637 } 638 /* subtract remainder */ 639 640 parm->bufsize -= (parm->bufsize % xfer->max_frame_size); 641 642 /* add length of USB device request structure, if any */ 643 644 if (type == UE_CONTROL) { 645 parm->bufsize += REQ_SIZE; /* SETUP message */ 646 } 647 } 648 xfer->max_data_length = parm->bufsize; 649 650 /* Setup "n_frlengths" and "n_frbuffers" */ 651 652 if (type == UE_ISOCHRONOUS) { 653 n_frlengths = xfer->nframes; 654 n_frbuffers = 1; 655 } else { 656 657 if (type == UE_CONTROL) { 658 xfer->flags_int.control_xfr = 1; 659 if (xfer->nframes == 0) { 660 if (parm->bufsize <= REQ_SIZE) { 661 /* 662 * there will never be any data 663 * stage 664 */ 665 xfer->nframes = 1; 666 } else { 667 xfer->nframes = 2; 668 } 669 } 670 } else { 671 if (xfer->nframes == 0) { 672 xfer->nframes = 1; 673 } 674 } 675 676 n_frlengths = xfer->nframes; 677 n_frbuffers = xfer->nframes; 678 } 679 680 /* 681 * check if we have room for the 682 * USB device request structure: 683 */ 684 685 if (type == UE_CONTROL) { 686 687 if (xfer->max_data_length < REQ_SIZE) { 688 /* length wrapped around or too small bufsize */ 689 parm->err = USB_ERR_INVAL; 690 goto done; 691 } 692 xfer->max_data_length -= REQ_SIZE; 693 } 694 /* 695 * Setup "frlengths" and shadow "frlengths" for keeping the 696 * initial frame lengths when a USB transfer is complete. This 697 * information is useful when computing isochronous offsets. 698 */ 699 xfer->frlengths = parm->xfer_length_ptr; 700 parm->xfer_length_ptr += 2 * n_frlengths; 701 702 /* setup "frbuffers" */ 703 xfer->frbuffers = parm->xfer_page_cache_ptr; 704 parm->xfer_page_cache_ptr += n_frbuffers; 705 706 /* initialize max frame count */ 707 xfer->max_frame_count = xfer->nframes; 708 709 /* 710 * check if we need to setup 711 * a local buffer: 712 */ 713 714 if (!xfer->flags.ext_buffer) { 715#if USB_HAVE_BUSDMA 716 struct usb_page_search page_info; 717 struct usb_page_cache *pc; 718 719 if (usbd_transfer_setup_sub_malloc(parm, 720 &pc, parm->bufsize, 1, 1)) { 721 parm->err = USB_ERR_NOMEM; 722 } else if (parm->buf != NULL) { 723 724 usbd_get_page(pc, 0, &page_info); 725 726 xfer->local_buffer = page_info.buffer; 727 728 usbd_xfer_set_frame_offset(xfer, 0, 0); 729 730 if ((type == UE_CONTROL) && (n_frbuffers > 1)) { 731 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1); 732 } 733 } 734#else 735 /* align data */ 736 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 737 738 if (parm->buf != NULL) { 739 xfer->local_buffer = 740 USB_ADD_BYTES(parm->buf, parm->size[0]); 741 742 usbd_xfer_set_frame_offset(xfer, 0, 0); 743 744 if ((type == UE_CONTROL) && (n_frbuffers > 1)) { 745 usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1); 746 } 747 } 748 parm->size[0] += parm->bufsize; 749 750 /* align data again */ 751 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 752#endif 753 } 754 /* 755 * Compute maximum buffer size 756 */ 757 758 if (parm->bufsize_max < parm->bufsize) { 759 parm->bufsize_max = parm->bufsize; 760 } 761#if USB_HAVE_BUSDMA 762 if (xfer->flags_int.bdma_enable) { 763 /* 764 * Setup "dma_page_ptr". 765 * 766 * Proof for formula below: 767 * 768 * Assume there are three USB frames having length "a", "b" and 769 * "c". These USB frames will at maximum need "z" 770 * "usb_page" structures. "z" is given by: 771 * 772 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) + 773 * ((c / USB_PAGE_SIZE) + 2); 774 * 775 * Constraining "a", "b" and "c" like this: 776 * 777 * (a + b + c) <= parm->bufsize 778 * 779 * We know that: 780 * 781 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2)); 782 * 783 * Here is the general formula: 784 */ 785 xfer->dma_page_ptr = parm->dma_page_ptr; 786 parm->dma_page_ptr += (2 * n_frbuffers); 787 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE); 788 } 789#endif 790 if (zmps) { 791 /* correct maximum data length */ 792 xfer->max_data_length = 0; 793 } 794 /* subtract USB frame remainder from "hc_max_frame_size" */ 795 796 xfer->max_hc_frame_size = 797 (parm->hc_max_frame_size - 798 (parm->hc_max_frame_size % xfer->max_frame_size)); 799 800 if (xfer->max_hc_frame_size == 0) { 801 parm->err = USB_ERR_INVAL; 802 goto done; 803 } 804 805 /* initialize frame buffers */ 806 807 if (parm->buf) { 808 for (x = 0; x != n_frbuffers; x++) { 809 xfer->frbuffers[x].tag_parent = 810 &xfer->xroot->dma_parent_tag; 811#if USB_HAVE_BUSDMA 812 if (xfer->flags_int.bdma_enable && 813 (parm->bufsize_max > 0)) { 814 815 if (usb_pc_dmamap_create( 816 xfer->frbuffers + x, 817 parm->bufsize_max)) { 818 parm->err = USB_ERR_NOMEM; 819 goto done; 820 } 821 } 822#endif 823 } 824 } 825done: 826 if (parm->err) { 827 /* 828 * Set some dummy values so that we avoid division by zero: 829 */ 830 xfer->max_hc_frame_size = 1; 831 xfer->max_frame_size = 1; 832 xfer->max_packet_size = 1; 833 xfer->max_data_length = 0; 834 xfer->nframes = 0; 835 xfer->max_frame_count = 0; 836 } 837} 838 839/*------------------------------------------------------------------------* 840 * usbd_transfer_setup - setup an array of USB transfers 841 * 842 * NOTE: You must always call "usbd_transfer_unsetup" after calling 843 * "usbd_transfer_setup" if success was returned. 844 * 845 * The idea is that the USB device driver should pre-allocate all its 846 * transfers by one call to this function. 847 * 848 * Return values: 849 * 0: Success 850 * Else: Failure 851 *------------------------------------------------------------------------*/ 852usb_error_t 853usbd_transfer_setup(struct usb_device *udev, 854 const uint8_t *ifaces, struct usb_xfer **ppxfer, 855 const struct usb_config *setup_start, uint16_t n_setup, 856 void *priv_sc, struct mtx *xfer_mtx) 857{ 858 struct usb_xfer dummy; 859 struct usb_setup_params parm; 860 const struct usb_config *setup_end = setup_start + n_setup; 861 const struct usb_config *setup; 862 struct usb_endpoint *ep; 863 struct usb_xfer_root *info; 864 struct usb_xfer *xfer; 865 void *buf = NULL; 866 uint16_t n; 867 uint16_t refcount; 868 869 parm.err = 0; 870 refcount = 0; 871 info = NULL; 872 873 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 874 "usbd_transfer_setup can sleep!"); 875 876 /* do some checking first */ 877 878 if (n_setup == 0) { 879 DPRINTFN(6, "setup array has zero length!\n"); 880 return (USB_ERR_INVAL); 881 } 882 if (ifaces == 0) { 883 DPRINTFN(6, "ifaces array is NULL!\n"); 884 return (USB_ERR_INVAL); 885 } 886 if (xfer_mtx == NULL) { 887 DPRINTFN(6, "using global lock\n"); 888 xfer_mtx = &Giant; 889 } 890 /* sanity checks */ 891 for (setup = setup_start, n = 0; 892 setup != setup_end; setup++, n++) { 893 if (setup->bufsize == (usb_frlength_t)-1) { 894 parm.err = USB_ERR_BAD_BUFSIZE; 895 DPRINTF("invalid bufsize\n"); 896 } 897 if (setup->callback == NULL) { 898 parm.err = USB_ERR_NO_CALLBACK; 899 DPRINTF("no callback\n"); 900 } 901 ppxfer[n] = NULL; 902 } 903 904 if (parm.err) { 905 goto done; 906 } 907 memset(&parm, 0, sizeof(parm)); 908 909 parm.udev = udev; 910 parm.speed = usbd_get_speed(udev); 911 parm.hc_max_packet_count = 1; 912 913 if (parm.speed >= USB_SPEED_MAX) { 914 parm.err = USB_ERR_INVAL; 915 goto done; 916 } 917 /* setup all transfers */ 918 919 while (1) { 920 921 if (buf) { 922 /* 923 * Initialize the "usb_xfer_root" structure, 924 * which is common for all our USB transfers. 925 */ 926 info = USB_ADD_BYTES(buf, 0); 927 928 info->memory_base = buf; 929 info->memory_size = parm.size[0]; 930 931#if USB_HAVE_BUSDMA 932 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]); 933 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]); 934#endif 935 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]); 936 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]); 937 938 cv_init(&info->cv_drain, "WDRAIN"); 939 940 info->xfer_mtx = xfer_mtx; 941#if USB_HAVE_BUSDMA 942 usb_dma_tag_setup(&info->dma_parent_tag, 943 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag, 944 xfer_mtx, &usb_bdma_done_event, 32, parm.dma_tag_max); 945#endif 946 947 info->bus = udev->bus; 948 info->udev = udev; 949 950 TAILQ_INIT(&info->done_q.head); 951 info->done_q.command = &usbd_callback_wrapper; 952#if USB_HAVE_BUSDMA 953 TAILQ_INIT(&info->dma_q.head); 954 info->dma_q.command = &usb_bdma_work_loop; 955#endif 956 info->done_m[0].hdr.pm_callback = &usb_callback_proc; 957 info->done_m[0].xroot = info; 958 info->done_m[1].hdr.pm_callback = &usb_callback_proc; 959 info->done_m[1].xroot = info; 960 961 /* 962 * In device side mode control endpoint 963 * requests need to run from a separate 964 * context, else there is a chance of 965 * deadlock! 966 */ 967 if (setup_start == usb_control_ep_cfg) 968 info->done_p = 969 &udev->bus->control_xfer_proc; 970 else if (xfer_mtx == &Giant) 971 info->done_p = 972 &udev->bus->giant_callback_proc; 973 else 974 info->done_p = 975 &udev->bus->non_giant_callback_proc; 976 } 977 /* reset sizes */ 978 979 parm.size[0] = 0; 980 parm.buf = buf; 981 parm.size[0] += sizeof(info[0]); 982 983 for (setup = setup_start, n = 0; 984 setup != setup_end; setup++, n++) { 985 986 /* skip USB transfers without callbacks: */ 987 if (setup->callback == NULL) { 988 continue; 989 } 990 /* see if there is a matching endpoint */ 991 ep = usbd_get_endpoint(udev, 992 ifaces[setup->if_index], setup); 993 994 /* 995 * Check that the USB PIPE is valid and that 996 * the endpoint mode is proper. 997 * 998 * Make sure we don't allocate a streams 999 * transfer when such a combination is not 1000 * valid. 1001 */ 1002 if ((ep == NULL) || (ep->methods == NULL) || 1003 ((ep->ep_mode != USB_EP_MODE_STREAMS) && 1004 (ep->ep_mode != USB_EP_MODE_DEFAULT)) || 1005 (setup->stream_id != 0 && 1006 (setup->stream_id >= USB_MAX_EP_STREAMS || 1007 (ep->ep_mode != USB_EP_MODE_STREAMS)))) { 1008 if (setup->flags.no_pipe_ok) 1009 continue; 1010 if ((setup->usb_mode != USB_MODE_DUAL) && 1011 (setup->usb_mode != udev->flags.usb_mode)) 1012 continue; 1013 parm.err = USB_ERR_NO_PIPE; 1014 goto done; 1015 } 1016 1017 /* align data properly */ 1018 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1019 1020 /* store current setup pointer */ 1021 parm.curr_setup = setup; 1022 1023 if (buf) { 1024 /* 1025 * Common initialization of the 1026 * "usb_xfer" structure. 1027 */ 1028 xfer = USB_ADD_BYTES(buf, parm.size[0]); 1029 xfer->address = udev->address; 1030 xfer->priv_sc = priv_sc; 1031 xfer->xroot = info; 1032 1033 usb_callout_init_mtx(&xfer->timeout_handle, 1034 &udev->bus->bus_mtx, 0); 1035 } else { 1036 /* 1037 * Setup a dummy xfer, hence we are 1038 * writing to the "usb_xfer" 1039 * structure pointed to by "xfer" 1040 * before we have allocated any 1041 * memory: 1042 */ 1043 xfer = &dummy; 1044 memset(&dummy, 0, sizeof(dummy)); 1045 refcount++; 1046 } 1047 1048 /* set transfer endpoint pointer */ 1049 xfer->endpoint = ep; 1050 1051 /* set transfer stream ID */ 1052 xfer->stream_id = setup->stream_id; 1053 1054 parm.size[0] += sizeof(xfer[0]); 1055 parm.methods = xfer->endpoint->methods; 1056 parm.curr_xfer = xfer; 1057 1058 /* 1059 * Call the Host or Device controller transfer 1060 * setup routine: 1061 */ 1062 (udev->bus->methods->xfer_setup) (&parm); 1063 1064 /* check for error */ 1065 if (parm.err) 1066 goto done; 1067 1068 if (buf) { 1069 /* 1070 * Increment the endpoint refcount. This 1071 * basically prevents setting a new 1072 * configuration and alternate setting 1073 * when USB transfers are in use on 1074 * the given interface. Search the USB 1075 * code for "endpoint->refcount_alloc" if you 1076 * want more information. 1077 */ 1078 USB_BUS_LOCK(info->bus); 1079 if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX) 1080 parm.err = USB_ERR_INVAL; 1081 1082 xfer->endpoint->refcount_alloc++; 1083 1084 if (xfer->endpoint->refcount_alloc == 0) 1085 panic("usbd_transfer_setup(): Refcount wrapped to zero\n"); 1086 USB_BUS_UNLOCK(info->bus); 1087 1088 /* 1089 * Whenever we set ppxfer[] then we 1090 * also need to increment the 1091 * "setup_refcount": 1092 */ 1093 info->setup_refcount++; 1094 1095 /* 1096 * Transfer is successfully setup and 1097 * can be used: 1098 */ 1099 ppxfer[n] = xfer; 1100 } 1101 1102 /* check for error */ 1103 if (parm.err) 1104 goto done; 1105 } 1106 1107 if (buf || parm.err) { 1108 goto done; 1109 } 1110 if (refcount == 0) { 1111 /* no transfers - nothing to do ! */ 1112 goto done; 1113 } 1114 /* align data properly */ 1115 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1116 1117 /* store offset temporarily */ 1118 parm.size[1] = parm.size[0]; 1119 1120 /* 1121 * The number of DMA tags required depends on 1122 * the number of endpoints. The current estimate 1123 * for maximum number of DMA tags per endpoint 1124 * is three: 1125 * 1) for loading memory 1126 * 2) for allocating memory 1127 * 3) for fixing memory [UHCI] 1128 */ 1129 parm.dma_tag_max += 3 * MIN(n_setup, USB_EP_MAX); 1130 1131 /* 1132 * DMA tags for QH, TD, Data and more. 1133 */ 1134 parm.dma_tag_max += 8; 1135 1136 parm.dma_tag_p += parm.dma_tag_max; 1137 1138 parm.size[0] += ((uint8_t *)parm.dma_tag_p) - 1139 ((uint8_t *)0); 1140 1141 /* align data properly */ 1142 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1143 1144 /* store offset temporarily */ 1145 parm.size[3] = parm.size[0]; 1146 1147 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) - 1148 ((uint8_t *)0); 1149 1150 /* align data properly */ 1151 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1152 1153 /* store offset temporarily */ 1154 parm.size[4] = parm.size[0]; 1155 1156 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) - 1157 ((uint8_t *)0); 1158 1159 /* store end offset temporarily */ 1160 parm.size[5] = parm.size[0]; 1161 1162 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) - 1163 ((uint8_t *)0); 1164 1165 /* store end offset temporarily */ 1166 1167 parm.size[2] = parm.size[0]; 1168 1169 /* align data properly */ 1170 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1171 1172 parm.size[6] = parm.size[0]; 1173 1174 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) - 1175 ((uint8_t *)0); 1176 1177 /* align data properly */ 1178 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1179 1180 /* allocate zeroed memory */ 1181 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO); 1182 1183 if (buf == NULL) { 1184 parm.err = USB_ERR_NOMEM; 1185 DPRINTFN(0, "cannot allocate memory block for " 1186 "configuration (%d bytes)\n", 1187 parm.size[0]); 1188 goto done; 1189 } 1190 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]); 1191 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]); 1192 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]); 1193 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]); 1194 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]); 1195 } 1196 1197done: 1198 if (buf) { 1199 if (info->setup_refcount == 0) { 1200 /* 1201 * "usbd_transfer_unsetup_sub" will unlock 1202 * the bus mutex before returning ! 1203 */ 1204 USB_BUS_LOCK(info->bus); 1205 1206 /* something went wrong */ 1207 usbd_transfer_unsetup_sub(info, 0); 1208 } 1209 } 1210 if (parm.err) { 1211 usbd_transfer_unsetup(ppxfer, n_setup); 1212 } 1213 return (parm.err); 1214} 1215 1216/*------------------------------------------------------------------------* 1217 * usbd_transfer_unsetup_sub - factored out code 1218 *------------------------------------------------------------------------*/ 1219static void 1220usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay) 1221{ 1222#if USB_HAVE_BUSDMA 1223 struct usb_page_cache *pc; 1224#endif 1225 1226 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 1227 1228 /* wait for any outstanding DMA operations */ 1229 1230 if (needs_delay) { 1231 usb_timeout_t temp; 1232 temp = usbd_get_dma_delay(info->udev); 1233 if (temp != 0) { 1234 usb_pause_mtx(&info->bus->bus_mtx, 1235 USB_MS_TO_TICKS(temp)); 1236 } 1237 } 1238 1239 /* make sure that our done messages are not queued anywhere */ 1240 usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); 1241 1242 USB_BUS_UNLOCK(info->bus); 1243 1244#if USB_HAVE_BUSDMA 1245 /* free DMA'able memory, if any */ 1246 pc = info->dma_page_cache_start; 1247 while (pc != info->dma_page_cache_end) { 1248 usb_pc_free_mem(pc); 1249 pc++; 1250 } 1251 1252 /* free DMA maps in all "xfer->frbuffers" */ 1253 pc = info->xfer_page_cache_start; 1254 while (pc != info->xfer_page_cache_end) { 1255 usb_pc_dmamap_destroy(pc); 1256 pc++; 1257 } 1258 1259 /* free all DMA tags */ 1260 usb_dma_tag_unsetup(&info->dma_parent_tag); 1261#endif 1262 1263 cv_destroy(&info->cv_drain); 1264 1265 /* 1266 * free the "memory_base" last, hence the "info" structure is 1267 * contained within the "memory_base"! 1268 */ 1269 free(info->memory_base, M_USB); 1270} 1271 1272/*------------------------------------------------------------------------* 1273 * usbd_transfer_unsetup - unsetup/free an array of USB transfers 1274 * 1275 * NOTE: All USB transfers in progress will get called back passing 1276 * the error code "USB_ERR_CANCELLED" before this function 1277 * returns. 1278 *------------------------------------------------------------------------*/ 1279void 1280usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup) 1281{ 1282 struct usb_xfer *xfer; 1283 struct usb_xfer_root *info; 1284 uint8_t needs_delay = 0; 1285 1286 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1287 "usbd_transfer_unsetup can sleep!"); 1288 1289 while (n_setup--) { 1290 xfer = pxfer[n_setup]; 1291 1292 if (xfer == NULL) 1293 continue; 1294 1295 info = xfer->xroot; 1296 1297 USB_XFER_LOCK(xfer); 1298 USB_BUS_LOCK(info->bus); 1299 1300 /* 1301 * HINT: when you start/stop a transfer, it might be a 1302 * good idea to directly use the "pxfer[]" structure: 1303 * 1304 * usbd_transfer_start(sc->pxfer[0]); 1305 * usbd_transfer_stop(sc->pxfer[0]); 1306 * 1307 * That way, if your code has many parts that will not 1308 * stop running under the same lock, in other words 1309 * "xfer_mtx", the usbd_transfer_start and 1310 * usbd_transfer_stop functions will simply return 1311 * when they detect a NULL pointer argument. 1312 * 1313 * To avoid any races we clear the "pxfer[]" pointer 1314 * while holding the private mutex of the driver: 1315 */ 1316 pxfer[n_setup] = NULL; 1317 1318 USB_BUS_UNLOCK(info->bus); 1319 USB_XFER_UNLOCK(xfer); 1320 1321 usbd_transfer_drain(xfer); 1322 1323#if USB_HAVE_BUSDMA 1324 if (xfer->flags_int.bdma_enable) 1325 needs_delay = 1; 1326#endif 1327 /* 1328 * NOTE: default endpoint does not have an 1329 * interface, even if endpoint->iface_index == 0 1330 */ 1331 USB_BUS_LOCK(info->bus); 1332 xfer->endpoint->refcount_alloc--; 1333 USB_BUS_UNLOCK(info->bus); 1334 1335 usb_callout_drain(&xfer->timeout_handle); 1336 1337 USB_BUS_LOCK(info->bus); 1338 1339 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup " 1340 "reference count\n")); 1341 1342 info->setup_refcount--; 1343 1344 if (info->setup_refcount == 0) { 1345 usbd_transfer_unsetup_sub(info, 1346 needs_delay); 1347 } else { 1348 USB_BUS_UNLOCK(info->bus); 1349 } 1350 } 1351} 1352 1353/*------------------------------------------------------------------------* 1354 * usbd_control_transfer_init - factored out code 1355 * 1356 * In USB Device Mode we have to wait for the SETUP packet which 1357 * containst the "struct usb_device_request" structure, before we can 1358 * transfer any data. In USB Host Mode we already have the SETUP 1359 * packet at the moment the USB transfer is started. This leads us to 1360 * having to setup the USB transfer at two different places in 1361 * time. This function just contains factored out control transfer 1362 * initialisation code, so that we don't duplicate the code. 1363 *------------------------------------------------------------------------*/ 1364static void 1365usbd_control_transfer_init(struct usb_xfer *xfer) 1366{ 1367 struct usb_device_request req; 1368 1369 /* copy out the USB request header */ 1370 1371 usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); 1372 1373 /* setup remainder */ 1374 1375 xfer->flags_int.control_rem = UGETW(req.wLength); 1376 1377 /* copy direction to endpoint variable */ 1378 1379 xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT); 1380 xfer->endpointno |= 1381 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT; 1382} 1383 1384/*------------------------------------------------------------------------* 1385 * usbd_setup_ctrl_transfer 1386 * 1387 * This function handles initialisation of control transfers. Control 1388 * transfers are special in that regard that they can both transmit 1389 * and receive data. 1390 * 1391 * Return values: 1392 * 0: Success 1393 * Else: Failure 1394 *------------------------------------------------------------------------*/ 1395static int 1396usbd_setup_ctrl_transfer(struct usb_xfer *xfer) 1397{ 1398 usb_frlength_t len; 1399 1400 /* Check for control endpoint stall */ 1401 if (xfer->flags.stall_pipe && xfer->flags_int.control_act) { 1402 /* the control transfer is no longer active */ 1403 xfer->flags_int.control_stall = 1; 1404 xfer->flags_int.control_act = 0; 1405 } else { 1406 /* don't stall control transfer by default */ 1407 xfer->flags_int.control_stall = 0; 1408 } 1409 1410 /* Check for invalid number of frames */ 1411 if (xfer->nframes > 2) { 1412 /* 1413 * If you need to split a control transfer, you 1414 * have to do one part at a time. Only with 1415 * non-control transfers you can do multiple 1416 * parts a time. 1417 */ 1418 DPRINTFN(0, "Too many frames: %u\n", 1419 (unsigned int)xfer->nframes); 1420 goto error; 1421 } 1422 1423 /* 1424 * Check if there is a control 1425 * transfer in progress: 1426 */ 1427 if (xfer->flags_int.control_act) { 1428 1429 if (xfer->flags_int.control_hdr) { 1430 1431 /* clear send header flag */ 1432 1433 xfer->flags_int.control_hdr = 0; 1434 1435 /* setup control transfer */ 1436 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1437 usbd_control_transfer_init(xfer); 1438 } 1439 } 1440 /* get data length */ 1441 1442 len = xfer->sumlen; 1443 1444 } else { 1445 1446 /* the size of the SETUP structure is hardcoded ! */ 1447 1448 if (xfer->frlengths[0] != sizeof(struct usb_device_request)) { 1449 DPRINTFN(0, "Wrong framelength %u != %zu\n", 1450 xfer->frlengths[0], sizeof(struct 1451 usb_device_request)); 1452 goto error; 1453 } 1454 /* check USB mode */ 1455 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1456 1457 /* check number of frames */ 1458 if (xfer->nframes != 1) { 1459 /* 1460 * We need to receive the setup 1461 * message first so that we know the 1462 * data direction! 1463 */ 1464 DPRINTF("Misconfigured transfer\n"); 1465 goto error; 1466 } 1467 /* 1468 * Set a dummy "control_rem" value. This 1469 * variable will be overwritten later by a 1470 * call to "usbd_control_transfer_init()" ! 1471 */ 1472 xfer->flags_int.control_rem = 0xFFFF; 1473 } else { 1474 1475 /* setup "endpoint" and "control_rem" */ 1476 1477 usbd_control_transfer_init(xfer); 1478 } 1479 1480 /* set transfer-header flag */ 1481 1482 xfer->flags_int.control_hdr = 1; 1483 1484 /* get data length */ 1485 1486 len = (xfer->sumlen - sizeof(struct usb_device_request)); 1487 } 1488 1489 /* check if there is a length mismatch */ 1490 1491 if (len > xfer->flags_int.control_rem) { 1492 DPRINTFN(0, "Length (%d) greater than " 1493 "remaining length (%d)\n", len, 1494 xfer->flags_int.control_rem); 1495 goto error; 1496 } 1497 /* check if we are doing a short transfer */ 1498 1499 if (xfer->flags.force_short_xfer) { 1500 xfer->flags_int.control_rem = 0; 1501 } else { 1502 if ((len != xfer->max_data_length) && 1503 (len != xfer->flags_int.control_rem) && 1504 (xfer->nframes != 1)) { 1505 DPRINTFN(0, "Short control transfer without " 1506 "force_short_xfer set\n"); 1507 goto error; 1508 } 1509 xfer->flags_int.control_rem -= len; 1510 } 1511 1512 /* the status part is executed when "control_act" is 0 */ 1513 1514 if ((xfer->flags_int.control_rem > 0) || 1515 (xfer->flags.manual_status)) { 1516 /* don't execute the STATUS stage yet */ 1517 xfer->flags_int.control_act = 1; 1518 1519 /* sanity check */ 1520 if ((!xfer->flags_int.control_hdr) && 1521 (xfer->nframes == 1)) { 1522 /* 1523 * This is not a valid operation! 1524 */ 1525 DPRINTFN(0, "Invalid parameter " 1526 "combination\n"); 1527 goto error; 1528 } 1529 } else { 1530 /* time to execute the STATUS stage */ 1531 xfer->flags_int.control_act = 0; 1532 } 1533 return (0); /* success */ 1534 1535error: 1536 return (1); /* failure */ 1537} 1538 1539/*------------------------------------------------------------------------* 1540 * usbd_transfer_submit - start USB hardware for the given transfer 1541 * 1542 * This function should only be called from the USB callback. 1543 *------------------------------------------------------------------------*/ 1544void 1545usbd_transfer_submit(struct usb_xfer *xfer) 1546{ 1547 struct usb_xfer_root *info; 1548 struct usb_bus *bus; 1549 usb_frcount_t x; 1550 1551 info = xfer->xroot; 1552 bus = info->bus; 1553 1554 DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n", 1555 xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? 1556 "read" : "write"); 1557 1558#ifdef USB_DEBUG 1559 if (USB_DEBUG_VAR > 0) { 1560 USB_BUS_LOCK(bus); 1561 1562 usb_dump_endpoint(xfer->endpoint); 1563 1564 USB_BUS_UNLOCK(bus); 1565 } 1566#endif 1567 1568 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1569 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED); 1570 1571 /* Only open the USB transfer once! */ 1572 if (!xfer->flags_int.open) { 1573 xfer->flags_int.open = 1; 1574 1575 DPRINTF("open\n"); 1576 1577 USB_BUS_LOCK(bus); 1578 (xfer->endpoint->methods->open) (xfer); 1579 USB_BUS_UNLOCK(bus); 1580 } 1581 /* set "transferring" flag */ 1582 xfer->flags_int.transferring = 1; 1583 1584#if USB_HAVE_POWERD 1585 /* increment power reference */ 1586 usbd_transfer_power_ref(xfer, 1); 1587#endif 1588 /* 1589 * Check if the transfer is waiting on a queue, most 1590 * frequently the "done_q": 1591 */ 1592 if (xfer->wait_queue) { 1593 USB_BUS_LOCK(bus); 1594 usbd_transfer_dequeue(xfer); 1595 USB_BUS_UNLOCK(bus); 1596 } 1597 /* clear "did_dma_delay" flag */ 1598 xfer->flags_int.did_dma_delay = 0; 1599 1600 /* clear "did_close" flag */ 1601 xfer->flags_int.did_close = 0; 1602 1603#if USB_HAVE_BUSDMA 1604 /* clear "bdma_setup" flag */ 1605 xfer->flags_int.bdma_setup = 0; 1606#endif 1607 /* by default we cannot cancel any USB transfer immediately */ 1608 xfer->flags_int.can_cancel_immed = 0; 1609 1610 /* clear lengths and frame counts by default */ 1611 xfer->sumlen = 0; 1612 xfer->actlen = 0; 1613 xfer->aframes = 0; 1614 1615 /* clear any previous errors */ 1616 xfer->error = 0; 1617 1618 /* Check if the device is still alive */ 1619 if (info->udev->state < USB_STATE_POWERED) { 1620 USB_BUS_LOCK(bus); 1621 /* 1622 * Must return cancelled error code else 1623 * device drivers can hang. 1624 */ 1625 usbd_transfer_done(xfer, USB_ERR_CANCELLED); 1626 USB_BUS_UNLOCK(bus); 1627 return; 1628 } 1629 1630 /* sanity check */ 1631 if (xfer->nframes == 0) { 1632 if (xfer->flags.stall_pipe) { 1633 /* 1634 * Special case - want to stall without transferring 1635 * any data: 1636 */ 1637 DPRINTF("xfer=%p nframes=0: stall " 1638 "or clear stall!\n", xfer); 1639 USB_BUS_LOCK(bus); 1640 xfer->flags_int.can_cancel_immed = 1; 1641 /* start the transfer */ 1642 usb_command_wrapper(&xfer->endpoint-> 1643 endpoint_q[xfer->stream_id], xfer); 1644 USB_BUS_UNLOCK(bus); 1645 return; 1646 } 1647 USB_BUS_LOCK(bus); 1648 usbd_transfer_done(xfer, USB_ERR_INVAL); 1649 USB_BUS_UNLOCK(bus); 1650 return; 1651 } 1652 /* compute some variables */ 1653 1654 for (x = 0; x != xfer->nframes; x++) { 1655 /* make a copy of the frlenghts[] */ 1656 xfer->frlengths[x + xfer->max_frame_count] = xfer->frlengths[x]; 1657 /* compute total transfer length */ 1658 xfer->sumlen += xfer->frlengths[x]; 1659 if (xfer->sumlen < xfer->frlengths[x]) { 1660 /* length wrapped around */ 1661 USB_BUS_LOCK(bus); 1662 usbd_transfer_done(xfer, USB_ERR_INVAL); 1663 USB_BUS_UNLOCK(bus); 1664 return; 1665 } 1666 } 1667 1668 /* clear some internal flags */ 1669 1670 xfer->flags_int.short_xfer_ok = 0; 1671 xfer->flags_int.short_frames_ok = 0; 1672 1673 /* check if this is a control transfer */ 1674 1675 if (xfer->flags_int.control_xfr) { 1676 1677 if (usbd_setup_ctrl_transfer(xfer)) { 1678 USB_BUS_LOCK(bus); 1679 usbd_transfer_done(xfer, USB_ERR_STALLED); 1680 USB_BUS_UNLOCK(bus); 1681 return; 1682 } 1683 } 1684 /* 1685 * Setup filtered version of some transfer flags, 1686 * in case of data read direction 1687 */ 1688 if (USB_GET_DATA_ISREAD(xfer)) { 1689 1690 if (xfer->flags.short_frames_ok) { 1691 xfer->flags_int.short_xfer_ok = 1; 1692 xfer->flags_int.short_frames_ok = 1; 1693 } else if (xfer->flags.short_xfer_ok) { 1694 xfer->flags_int.short_xfer_ok = 1; 1695 1696 /* check for control transfer */ 1697 if (xfer->flags_int.control_xfr) { 1698 /* 1699 * 1) Control transfers do not support 1700 * reception of multiple short USB 1701 * frames in host mode and device side 1702 * mode, with exception of: 1703 * 1704 * 2) Due to sometimes buggy device 1705 * side firmware we need to do a 1706 * STATUS stage in case of short 1707 * control transfers in USB host mode. 1708 * The STATUS stage then becomes the 1709 * "alt_next" to the DATA stage. 1710 */ 1711 xfer->flags_int.short_frames_ok = 1; 1712 } 1713 } 1714 } 1715 /* 1716 * Check if BUS-DMA support is enabled and try to load virtual 1717 * buffers into DMA, if any: 1718 */ 1719#if USB_HAVE_BUSDMA 1720 if (xfer->flags_int.bdma_enable) { 1721 /* insert the USB transfer last in the BUS-DMA queue */ 1722 usb_command_wrapper(&xfer->xroot->dma_q, xfer); 1723 return; 1724 } 1725#endif 1726 /* 1727 * Enter the USB transfer into the Host Controller or 1728 * Device Controller schedule: 1729 */ 1730 usbd_pipe_enter(xfer); 1731} 1732 1733/*------------------------------------------------------------------------* 1734 * usbd_pipe_enter - factored out code 1735 *------------------------------------------------------------------------*/ 1736void 1737usbd_pipe_enter(struct usb_xfer *xfer) 1738{ 1739 struct usb_endpoint *ep; 1740 1741 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1742 1743 USB_BUS_LOCK(xfer->xroot->bus); 1744 1745 ep = xfer->endpoint; 1746 1747 DPRINTF("enter\n"); 1748 1749 /* the transfer can now be cancelled */ 1750 xfer->flags_int.can_cancel_immed = 1; 1751 1752 /* enter the transfer */ 1753 (ep->methods->enter) (xfer); 1754 1755 /* check for transfer error */ 1756 if (xfer->error) { 1757 /* some error has happened */ 1758 usbd_transfer_done(xfer, 0); 1759 USB_BUS_UNLOCK(xfer->xroot->bus); 1760 return; 1761 } 1762 1763 /* start the transfer */ 1764 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], xfer); 1765 USB_BUS_UNLOCK(xfer->xroot->bus); 1766} 1767 1768/*------------------------------------------------------------------------* 1769 * usbd_transfer_start - start an USB transfer 1770 * 1771 * NOTE: Calling this function more than one time will only 1772 * result in a single transfer start, until the USB transfer 1773 * completes. 1774 *------------------------------------------------------------------------*/ 1775void 1776usbd_transfer_start(struct usb_xfer *xfer) 1777{ 1778 if (xfer == NULL) { 1779 /* transfer is gone */ 1780 return; 1781 } 1782 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1783 1784 /* mark the USB transfer started */ 1785 1786 if (!xfer->flags_int.started) { 1787 /* lock the BUS lock to avoid races updating flags_int */ 1788 USB_BUS_LOCK(xfer->xroot->bus); 1789 xfer->flags_int.started = 1; 1790 USB_BUS_UNLOCK(xfer->xroot->bus); 1791 } 1792 /* check if the USB transfer callback is already transferring */ 1793 1794 if (xfer->flags_int.transferring) { 1795 return; 1796 } 1797 USB_BUS_LOCK(xfer->xroot->bus); 1798 /* call the USB transfer callback */ 1799 usbd_callback_ss_done_defer(xfer); 1800 USB_BUS_UNLOCK(xfer->xroot->bus); 1801} 1802 1803/*------------------------------------------------------------------------* 1804 * usbd_transfer_stop - stop an USB transfer 1805 * 1806 * NOTE: Calling this function more than one time will only 1807 * result in a single transfer stop. 1808 * NOTE: When this function returns it is not safe to free nor 1809 * reuse any DMA buffers. See "usbd_transfer_drain()". 1810 *------------------------------------------------------------------------*/ 1811void 1812usbd_transfer_stop(struct usb_xfer *xfer) 1813{ 1814 struct usb_endpoint *ep; 1815 1816 if (xfer == NULL) { 1817 /* transfer is gone */ 1818 return; 1819 } 1820 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1821 1822 /* check if the USB transfer was ever opened */ 1823 1824 if (!xfer->flags_int.open) { 1825 if (xfer->flags_int.started) { 1826 /* nothing to do except clearing the "started" flag */ 1827 /* lock the BUS lock to avoid races updating flags_int */ 1828 USB_BUS_LOCK(xfer->xroot->bus); 1829 xfer->flags_int.started = 0; 1830 USB_BUS_UNLOCK(xfer->xroot->bus); 1831 } 1832 return; 1833 } 1834 /* try to stop the current USB transfer */ 1835 1836 USB_BUS_LOCK(xfer->xroot->bus); 1837 /* override any previous error */ 1838 xfer->error = USB_ERR_CANCELLED; 1839 1840 /* 1841 * Clear "open" and "started" when both private and USB lock 1842 * is locked so that we don't get a race updating "flags_int" 1843 */ 1844 xfer->flags_int.open = 0; 1845 xfer->flags_int.started = 0; 1846 1847 /* 1848 * Check if we can cancel the USB transfer immediately. 1849 */ 1850 if (xfer->flags_int.transferring) { 1851 if (xfer->flags_int.can_cancel_immed && 1852 (!xfer->flags_int.did_close)) { 1853 DPRINTF("close\n"); 1854 /* 1855 * The following will lead to an USB_ERR_CANCELLED 1856 * error code being passed to the USB callback. 1857 */ 1858 (xfer->endpoint->methods->close) (xfer); 1859 /* only close once */ 1860 xfer->flags_int.did_close = 1; 1861 } else { 1862 /* need to wait for the next done callback */ 1863 } 1864 } else { 1865 DPRINTF("close\n"); 1866 1867 /* close here and now */ 1868 (xfer->endpoint->methods->close) (xfer); 1869 1870 /* 1871 * Any additional DMA delay is done by 1872 * "usbd_transfer_unsetup()". 1873 */ 1874 1875 /* 1876 * Special case. Check if we need to restart a blocked 1877 * endpoint. 1878 */ 1879 ep = xfer->endpoint; 1880 1881 /* 1882 * If the current USB transfer is completing we need 1883 * to start the next one: 1884 */ 1885 if (ep->endpoint_q[xfer->stream_id].curr == xfer) { 1886 usb_command_wrapper( 1887 &ep->endpoint_q[xfer->stream_id], NULL); 1888 } 1889 } 1890 1891 USB_BUS_UNLOCK(xfer->xroot->bus); 1892} 1893 1894/*------------------------------------------------------------------------* 1895 * usbd_transfer_pending 1896 * 1897 * This function will check if an USB transfer is pending which is a 1898 * little bit complicated! 1899 * Return values: 1900 * 0: Not pending 1901 * 1: Pending: The USB transfer will receive a callback in the future. 1902 *------------------------------------------------------------------------*/ 1903uint8_t 1904usbd_transfer_pending(struct usb_xfer *xfer) 1905{ 1906 struct usb_xfer_root *info; 1907 struct usb_xfer_queue *pq; 1908 1909 if (xfer == NULL) { 1910 /* transfer is gone */ 1911 return (0); 1912 } 1913 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1914 1915 if (xfer->flags_int.transferring) { 1916 /* trivial case */ 1917 return (1); 1918 } 1919 USB_BUS_LOCK(xfer->xroot->bus); 1920 if (xfer->wait_queue) { 1921 /* we are waiting on a queue somewhere */ 1922 USB_BUS_UNLOCK(xfer->xroot->bus); 1923 return (1); 1924 } 1925 info = xfer->xroot; 1926 pq = &info->done_q; 1927 1928 if (pq->curr == xfer) { 1929 /* we are currently scheduled for callback */ 1930 USB_BUS_UNLOCK(xfer->xroot->bus); 1931 return (1); 1932 } 1933 /* we are not pending */ 1934 USB_BUS_UNLOCK(xfer->xroot->bus); 1935 return (0); 1936} 1937 1938/*------------------------------------------------------------------------* 1939 * usbd_transfer_drain 1940 * 1941 * This function will stop the USB transfer and wait for any 1942 * additional BUS-DMA and HW-DMA operations to complete. Buffers that 1943 * are loaded into DMA can safely be freed or reused after that this 1944 * function has returned. 1945 *------------------------------------------------------------------------*/ 1946void 1947usbd_transfer_drain(struct usb_xfer *xfer) 1948{ 1949 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1950 "usbd_transfer_drain can sleep!"); 1951 1952 if (xfer == NULL) { 1953 /* transfer is gone */ 1954 return; 1955 } 1956 if (xfer->xroot->xfer_mtx != &Giant) { 1957 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED); 1958 } 1959 USB_XFER_LOCK(xfer); 1960 1961 usbd_transfer_stop(xfer); 1962 1963 while (usbd_transfer_pending(xfer) || 1964 xfer->flags_int.doing_callback) { 1965 1966 /* 1967 * It is allowed that the callback can drop its 1968 * transfer mutex. In that case checking only 1969 * "usbd_transfer_pending()" is not enough to tell if 1970 * the USB transfer is fully drained. We also need to 1971 * check the internal "doing_callback" flag. 1972 */ 1973 xfer->flags_int.draining = 1; 1974 1975 /* 1976 * Wait until the current outstanding USB 1977 * transfer is complete ! 1978 */ 1979 cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); 1980 } 1981 USB_XFER_UNLOCK(xfer); 1982} 1983 1984struct usb_page_cache * 1985usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex) 1986{ 1987 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1988 1989 return (&xfer->frbuffers[frindex]); 1990} 1991 1992void * 1993usbd_xfer_get_frame_buffer(struct usb_xfer *xfer, usb_frcount_t frindex) 1994{ 1995 struct usb_page_search page_info; 1996 1997 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1998 1999 usbd_get_page(&xfer->frbuffers[frindex], 0, &page_info); 2000 return (page_info.buffer); 2001} 2002 2003/*------------------------------------------------------------------------* 2004 * usbd_xfer_get_fps_shift 2005 * 2006 * The following function is only useful for isochronous transfers. It 2007 * returns how many times the frame execution rate has been shifted 2008 * down. 2009 * 2010 * Return value: 2011 * Success: 0..3 2012 * Failure: 0 2013 *------------------------------------------------------------------------*/ 2014uint8_t 2015usbd_xfer_get_fps_shift(struct usb_xfer *xfer) 2016{ 2017 return (xfer->fps_shift); 2018} 2019 2020usb_frlength_t 2021usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex) 2022{ 2023 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 2024 2025 return (xfer->frlengths[frindex]); 2026} 2027 2028/*------------------------------------------------------------------------* 2029 * usbd_xfer_set_frame_data 2030 * 2031 * This function sets the pointer of the buffer that should 2032 * loaded directly into DMA for the given USB frame. Passing "ptr" 2033 * equal to NULL while the corresponding "frlength" is greater 2034 * than zero gives undefined results! 2035 *------------------------------------------------------------------------*/ 2036void 2037usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 2038 void *ptr, usb_frlength_t len) 2039{ 2040 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 2041 2042 /* set virtual address to load and length */ 2043 xfer->frbuffers[frindex].buffer = ptr; 2044 usbd_xfer_set_frame_len(xfer, frindex, len); 2045} 2046 2047void 2048usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 2049 void **ptr, int *len) 2050{ 2051 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 2052 2053 if (ptr != NULL) 2054 *ptr = xfer->frbuffers[frindex].buffer; 2055 if (len != NULL) 2056 *len = xfer->frlengths[frindex]; 2057} 2058 2059/*------------------------------------------------------------------------* 2060 * usbd_xfer_old_frame_length 2061 * 2062 * This function returns the framelength of the given frame at the 2063 * time the transfer was submitted. This function can be used to 2064 * compute the starting data pointer of the next isochronous frame 2065 * when an isochronous transfer has completed. 2066 *------------------------------------------------------------------------*/ 2067usb_frlength_t 2068usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex) 2069{ 2070 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 2071 2072 return (xfer->frlengths[frindex + xfer->max_frame_count]); 2073} 2074 2075void 2076usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes, 2077 int *nframes) 2078{ 2079 if (actlen != NULL) 2080 *actlen = xfer->actlen; 2081 if (sumlen != NULL) 2082 *sumlen = xfer->sumlen; 2083 if (aframes != NULL) 2084 *aframes = xfer->aframes; 2085 if (nframes != NULL) 2086 *nframes = xfer->nframes; 2087} 2088 2089/*------------------------------------------------------------------------* 2090 * usbd_xfer_set_frame_offset 2091 * 2092 * This function sets the frame data buffer offset relative to the beginning 2093 * of the USB DMA buffer allocated for this USB transfer. 2094 *------------------------------------------------------------------------*/ 2095void 2096usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset, 2097 usb_frcount_t frindex) 2098{ 2099 KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " 2100 "when the USB buffer is external\n")); 2101 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 2102 2103 /* set virtual address to load */ 2104 xfer->frbuffers[frindex].buffer = 2105 USB_ADD_BYTES(xfer->local_buffer, offset); 2106} 2107 2108void 2109usbd_xfer_set_interval(struct usb_xfer *xfer, int i) 2110{ 2111 xfer->interval = i; 2112} 2113 2114void 2115usbd_xfer_set_timeout(struct usb_xfer *xfer, int t) 2116{ 2117 xfer->timeout = t; 2118} 2119 2120void 2121usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n) 2122{ 2123 xfer->nframes = n; 2124} 2125 2126usb_frcount_t 2127usbd_xfer_max_frames(struct usb_xfer *xfer) 2128{ 2129 return (xfer->max_frame_count); 2130} 2131 2132usb_frlength_t 2133usbd_xfer_max_len(struct usb_xfer *xfer) 2134{ 2135 return (xfer->max_data_length); 2136} 2137 2138usb_frlength_t 2139usbd_xfer_max_framelen(struct usb_xfer *xfer) 2140{ 2141 return (xfer->max_frame_size); 2142} 2143 2144void 2145usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex, 2146 usb_frlength_t len) 2147{ 2148 KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 2149 2150 xfer->frlengths[frindex] = len; 2151} 2152 2153/*------------------------------------------------------------------------* 2154 * usb_callback_proc - factored out code 2155 * 2156 * This function performs USB callbacks. 2157 *------------------------------------------------------------------------*/ 2158static void 2159usb_callback_proc(struct usb_proc_msg *_pm) 2160{ 2161 struct usb_done_msg *pm = (void *)_pm; 2162 struct usb_xfer_root *info = pm->xroot; 2163 2164 /* Change locking order */ 2165 USB_BUS_UNLOCK(info->bus); 2166 2167 /* 2168 * We exploit the fact that the mutex is the same for all 2169 * callbacks that will be called from this thread: 2170 */ 2171 mtx_lock(info->xfer_mtx); 2172 USB_BUS_LOCK(info->bus); 2173 2174 /* Continue where we lost track */ 2175 usb_command_wrapper(&info->done_q, 2176 info->done_q.curr); 2177 2178 mtx_unlock(info->xfer_mtx); 2179} 2180 2181/*------------------------------------------------------------------------* 2182 * usbd_callback_ss_done_defer 2183 * 2184 * This function will defer the start, stop and done callback to the 2185 * correct thread. 2186 *------------------------------------------------------------------------*/ 2187static void 2188usbd_callback_ss_done_defer(struct usb_xfer *xfer) 2189{ 2190 struct usb_xfer_root *info = xfer->xroot; 2191 struct usb_xfer_queue *pq = &info->done_q; 2192 2193 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2194 2195 if (pq->curr != xfer) { 2196 usbd_transfer_enqueue(pq, xfer); 2197 } 2198 if (!pq->recurse_1) { 2199 2200 /* 2201 * We have to postpone the callback due to the fact we 2202 * will have a Lock Order Reversal, LOR, if we try to 2203 * proceed ! 2204 */ 2205 if (usb_proc_msignal(info->done_p, 2206 &info->done_m[0], &info->done_m[1])) { 2207 /* ignore */ 2208 } 2209 } else { 2210 /* clear second recurse flag */ 2211 pq->recurse_2 = 0; 2212 } 2213 return; 2214 2215} 2216 2217/*------------------------------------------------------------------------* 2218 * usbd_callback_wrapper 2219 * 2220 * This is a wrapper for USB callbacks. This wrapper does some 2221 * auto-magic things like figuring out if we can call the callback 2222 * directly from the current context or if we need to wakeup the 2223 * interrupt process. 2224 *------------------------------------------------------------------------*/ 2225static void 2226usbd_callback_wrapper(struct usb_xfer_queue *pq) 2227{ 2228 struct usb_xfer *xfer = pq->curr; 2229 struct usb_xfer_root *info = xfer->xroot; 2230 2231 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 2232 if (!mtx_owned(info->xfer_mtx) && !SCHEDULER_STOPPED()) { 2233 /* 2234 * Cases that end up here: 2235 * 2236 * 5) HW interrupt done callback or other source. 2237 */ 2238 DPRINTFN(3, "case 5\n"); 2239 2240 /* 2241 * We have to postpone the callback due to the fact we 2242 * will have a Lock Order Reversal, LOR, if we try to 2243 * proceed ! 2244 */ 2245 if (usb_proc_msignal(info->done_p, 2246 &info->done_m[0], &info->done_m[1])) { 2247 /* ignore */ 2248 } 2249 return; 2250 } 2251 /* 2252 * Cases that end up here: 2253 * 2254 * 1) We are starting a transfer 2255 * 2) We are prematurely calling back a transfer 2256 * 3) We are stopping a transfer 2257 * 4) We are doing an ordinary callback 2258 */ 2259 DPRINTFN(3, "case 1-4\n"); 2260 /* get next USB transfer in the queue */ 2261 info->done_q.curr = NULL; 2262 2263 /* set flag in case of drain */ 2264 xfer->flags_int.doing_callback = 1; 2265 2266 USB_BUS_UNLOCK(info->bus); 2267 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED); 2268 2269 /* set correct USB state for callback */ 2270 if (!xfer->flags_int.transferring) { 2271 xfer->usb_state = USB_ST_SETUP; 2272 if (!xfer->flags_int.started) { 2273 /* we got stopped before we even got started */ 2274 USB_BUS_LOCK(info->bus); 2275 goto done; 2276 } 2277 } else { 2278 2279 if (usbd_callback_wrapper_sub(xfer)) { 2280 /* the callback has been deferred */ 2281 USB_BUS_LOCK(info->bus); 2282 goto done; 2283 } 2284#if USB_HAVE_POWERD 2285 /* decrement power reference */ 2286 usbd_transfer_power_ref(xfer, -1); 2287#endif 2288 xfer->flags_int.transferring = 0; 2289 2290 if (xfer->error) { 2291 xfer->usb_state = USB_ST_ERROR; 2292 } else { 2293 /* set transferred state */ 2294 xfer->usb_state = USB_ST_TRANSFERRED; 2295#if USB_HAVE_BUSDMA 2296 /* sync DMA memory, if any */ 2297 if (xfer->flags_int.bdma_enable && 2298 (!xfer->flags_int.bdma_no_post_sync)) { 2299 usb_bdma_post_sync(xfer); 2300 } 2301#endif 2302 } 2303 } 2304 2305#if USB_HAVE_PF 2306 if (xfer->usb_state != USB_ST_SETUP) 2307 usbpf_xfertap(xfer, USBPF_XFERTAP_DONE); 2308#endif 2309 /* call processing routine */ 2310 (xfer->callback) (xfer, xfer->error); 2311 2312 /* pickup the USB mutex again */ 2313 USB_BUS_LOCK(info->bus); 2314 2315 /* 2316 * Check if we got started after that we got cancelled, but 2317 * before we managed to do the callback. 2318 */ 2319 if ((!xfer->flags_int.open) && 2320 (xfer->flags_int.started) && 2321 (xfer->usb_state == USB_ST_ERROR)) { 2322 /* clear flag in case of drain */ 2323 xfer->flags_int.doing_callback = 0; 2324 /* try to loop, but not recursivly */ 2325 usb_command_wrapper(&info->done_q, xfer); 2326 return; 2327 } 2328 2329done: 2330 /* clear flag in case of drain */ 2331 xfer->flags_int.doing_callback = 0; 2332 2333 /* 2334 * Check if we are draining. 2335 */ 2336 if (xfer->flags_int.draining && 2337 (!xfer->flags_int.transferring)) { 2338 /* "usbd_transfer_drain()" is waiting for end of transfer */ 2339 xfer->flags_int.draining = 0; 2340 cv_broadcast(&info->cv_drain); 2341 } 2342 2343 /* do the next callback, if any */ 2344 usb_command_wrapper(&info->done_q, 2345 info->done_q.curr); 2346} 2347 2348/*------------------------------------------------------------------------* 2349 * usb_dma_delay_done_cb 2350 * 2351 * This function is called when the DMA delay has been exectuded, and 2352 * will make sure that the callback is called to complete the USB 2353 * transfer. This code path is ususally only used when there is an USB 2354 * error like USB_ERR_CANCELLED. 2355 *------------------------------------------------------------------------*/ 2356void 2357usb_dma_delay_done_cb(struct usb_xfer *xfer) 2358{ 2359 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2360 2361 DPRINTFN(3, "Completed %p\n", xfer); 2362 2363 /* queue callback for execution, again */ 2364 usbd_transfer_done(xfer, 0); 2365} 2366 2367/*------------------------------------------------------------------------* 2368 * usbd_transfer_dequeue 2369 * 2370 * - This function is used to remove an USB transfer from a USB 2371 * transfer queue. 2372 * 2373 * - This function can be called multiple times in a row. 2374 *------------------------------------------------------------------------*/ 2375void 2376usbd_transfer_dequeue(struct usb_xfer *xfer) 2377{ 2378 struct usb_xfer_queue *pq; 2379 2380 pq = xfer->wait_queue; 2381 if (pq) { 2382 TAILQ_REMOVE(&pq->head, xfer, wait_entry); 2383 xfer->wait_queue = NULL; 2384 } 2385} 2386 2387/*------------------------------------------------------------------------* 2388 * usbd_transfer_enqueue 2389 * 2390 * - This function is used to insert an USB transfer into a USB * 2391 * transfer queue. 2392 * 2393 * - This function can be called multiple times in a row. 2394 *------------------------------------------------------------------------*/ 2395void 2396usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer) 2397{ 2398 /* 2399 * Insert the USB transfer into the queue, if it is not 2400 * already on a USB transfer queue: 2401 */ 2402 if (xfer->wait_queue == NULL) { 2403 xfer->wait_queue = pq; 2404 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); 2405 } 2406} 2407 2408/*------------------------------------------------------------------------* 2409 * usbd_transfer_done 2410 * 2411 * - This function is used to remove an USB transfer from the busdma, 2412 * pipe or interrupt queue. 2413 * 2414 * - This function is used to queue the USB transfer on the done 2415 * queue. 2416 * 2417 * - This function is used to stop any USB transfer timeouts. 2418 *------------------------------------------------------------------------*/ 2419void 2420usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error) 2421{ 2422 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2423 2424 DPRINTF("err=%s\n", usbd_errstr(error)); 2425 2426 /* 2427 * If we are not transferring then just return. 2428 * This can happen during transfer cancel. 2429 */ 2430 if (!xfer->flags_int.transferring) { 2431 DPRINTF("not transferring\n"); 2432 /* end of control transfer, if any */ 2433 xfer->flags_int.control_act = 0; 2434 return; 2435 } 2436 /* only set transfer error if not already set */ 2437 if (!xfer->error) { 2438 xfer->error = error; 2439 } 2440 /* stop any callouts */ 2441 usb_callout_stop(&xfer->timeout_handle); 2442 2443 /* 2444 * If we are waiting on a queue, just remove the USB transfer 2445 * from the queue, if any. We should have the required locks 2446 * locked to do the remove when this function is called. 2447 */ 2448 usbd_transfer_dequeue(xfer); 2449 2450#if USB_HAVE_BUSDMA 2451 if (mtx_owned(xfer->xroot->xfer_mtx)) { 2452 struct usb_xfer_queue *pq; 2453 2454 /* 2455 * If the private USB lock is not locked, then we assume 2456 * that the BUS-DMA load stage has been passed: 2457 */ 2458 pq = &xfer->xroot->dma_q; 2459 2460 if (pq->curr == xfer) { 2461 /* start the next BUS-DMA load, if any */ 2462 usb_command_wrapper(pq, NULL); 2463 } 2464 } 2465#endif 2466 /* keep some statistics */ 2467 if (xfer->error) { 2468 xfer->xroot->bus->stats_err.uds_requests 2469 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; 2470 } else { 2471 xfer->xroot->bus->stats_ok.uds_requests 2472 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; 2473 } 2474 2475 /* call the USB transfer callback */ 2476 usbd_callback_ss_done_defer(xfer); 2477} 2478 2479/*------------------------------------------------------------------------* 2480 * usbd_transfer_start_cb 2481 * 2482 * This function is called to start the USB transfer when 2483 * "xfer->interval" is greater than zero, and and the endpoint type is 2484 * BULK or CONTROL. 2485 *------------------------------------------------------------------------*/ 2486static void 2487usbd_transfer_start_cb(void *arg) 2488{ 2489 struct usb_xfer *xfer = arg; 2490 struct usb_endpoint *ep = xfer->endpoint; 2491 2492 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2493 2494 DPRINTF("start\n"); 2495 2496#if USB_HAVE_PF 2497 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT); 2498#endif 2499 2500 /* the transfer can now be cancelled */ 2501 xfer->flags_int.can_cancel_immed = 1; 2502 2503 /* start USB transfer, if no error */ 2504 if (xfer->error == 0) 2505 (ep->methods->start) (xfer); 2506 2507 /* check for transfer error */ 2508 if (xfer->error) { 2509 /* some error has happened */ 2510 usbd_transfer_done(xfer, 0); 2511 } 2512} 2513 2514/*------------------------------------------------------------------------* 2515 * usbd_xfer_set_stall 2516 * 2517 * This function is used to set the stall flag outside the 2518 * callback. This function is NULL safe. 2519 *------------------------------------------------------------------------*/ 2520void 2521usbd_xfer_set_stall(struct usb_xfer *xfer) 2522{ 2523 if (xfer == NULL) { 2524 /* tearing down */ 2525 return; 2526 } 2527 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2528 2529 /* avoid any races by locking the USB mutex */ 2530 USB_BUS_LOCK(xfer->xroot->bus); 2531 xfer->flags.stall_pipe = 1; 2532 USB_BUS_UNLOCK(xfer->xroot->bus); 2533} 2534 2535int 2536usbd_xfer_is_stalled(struct usb_xfer *xfer) 2537{ 2538 return (xfer->endpoint->is_stalled); 2539} 2540 2541/*------------------------------------------------------------------------* 2542 * usbd_transfer_clear_stall 2543 * 2544 * This function is used to clear the stall flag outside the 2545 * callback. This function is NULL safe. 2546 *------------------------------------------------------------------------*/ 2547void 2548usbd_transfer_clear_stall(struct usb_xfer *xfer) 2549{ 2550 if (xfer == NULL) { 2551 /* tearing down */ 2552 return; 2553 } 2554 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2555 2556 /* avoid any races by locking the USB mutex */ 2557 USB_BUS_LOCK(xfer->xroot->bus); 2558 2559 xfer->flags.stall_pipe = 0; 2560 2561 USB_BUS_UNLOCK(xfer->xroot->bus); 2562} 2563 2564/*------------------------------------------------------------------------* 2565 * usbd_pipe_start 2566 * 2567 * This function is used to add an USB transfer to the pipe transfer list. 2568 *------------------------------------------------------------------------*/ 2569void 2570usbd_pipe_start(struct usb_xfer_queue *pq) 2571{ 2572 struct usb_endpoint *ep; 2573 struct usb_xfer *xfer; 2574 uint8_t type; 2575 2576 xfer = pq->curr; 2577 ep = xfer->endpoint; 2578 2579 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2580 2581 /* 2582 * If the endpoint is already stalled we do nothing ! 2583 */ 2584 if (ep->is_stalled) { 2585 return; 2586 } 2587 /* 2588 * Check if we are supposed to stall the endpoint: 2589 */ 2590 if (xfer->flags.stall_pipe) { 2591 struct usb_device *udev; 2592 struct usb_xfer_root *info; 2593 2594 /* clear stall command */ 2595 xfer->flags.stall_pipe = 0; 2596 2597 /* get pointer to USB device */ 2598 info = xfer->xroot; 2599 udev = info->udev; 2600 2601 /* 2602 * Only stall BULK and INTERRUPT endpoints. 2603 */ 2604 type = (ep->edesc->bmAttributes & UE_XFERTYPE); 2605 if ((type == UE_BULK) || 2606 (type == UE_INTERRUPT)) { 2607 uint8_t did_stall; 2608 2609 did_stall = 1; 2610 2611 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2612 (udev->bus->methods->set_stall) ( 2613 udev, ep, &did_stall); 2614 } else if (udev->ctrl_xfer[1]) { 2615 info = udev->ctrl_xfer[1]->xroot; 2616 usb_proc_msignal( 2617 &info->bus->non_giant_callback_proc, 2618 &udev->cs_msg[0], &udev->cs_msg[1]); 2619 } else { 2620 /* should not happen */ 2621 DPRINTFN(0, "No stall handler\n"); 2622 } 2623 /* 2624 * Check if we should stall. Some USB hardware 2625 * handles set- and clear-stall in hardware. 2626 */ 2627 if (did_stall) { 2628 /* 2629 * The transfer will be continued when 2630 * the clear-stall control endpoint 2631 * message is received. 2632 */ 2633 ep->is_stalled = 1; 2634 return; 2635 } 2636 } else if (type == UE_ISOCHRONOUS) { 2637 2638 /* 2639 * Make sure any FIFO overflow or other FIFO 2640 * error conditions go away by resetting the 2641 * endpoint FIFO through the clear stall 2642 * method. 2643 */ 2644 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2645 (udev->bus->methods->clear_stall) (udev, ep); 2646 } 2647 } 2648 } 2649 /* Set or clear stall complete - special case */ 2650 if (xfer->nframes == 0) { 2651 /* we are complete */ 2652 xfer->aframes = 0; 2653 usbd_transfer_done(xfer, 0); 2654 return; 2655 } 2656 /* 2657 * Handled cases: 2658 * 2659 * 1) Start the first transfer queued. 2660 * 2661 * 2) Re-start the current USB transfer. 2662 */ 2663 /* 2664 * Check if there should be any 2665 * pre transfer start delay: 2666 */ 2667 if (xfer->interval > 0) { 2668 type = (ep->edesc->bmAttributes & UE_XFERTYPE); 2669 if ((type == UE_BULK) || 2670 (type == UE_CONTROL)) { 2671 usbd_transfer_timeout_ms(xfer, 2672 &usbd_transfer_start_cb, 2673 xfer->interval); 2674 return; 2675 } 2676 } 2677 DPRINTF("start\n"); 2678 2679#if USB_HAVE_PF 2680 usbpf_xfertap(xfer, USBPF_XFERTAP_SUBMIT); 2681#endif 2682 /* the transfer can now be cancelled */ 2683 xfer->flags_int.can_cancel_immed = 1; 2684 2685 /* start USB transfer, if no error */ 2686 if (xfer->error == 0) 2687 (ep->methods->start) (xfer); 2688 2689 /* check for transfer error */ 2690 if (xfer->error) { 2691 /* some error has happened */ 2692 usbd_transfer_done(xfer, 0); 2693 } 2694} 2695 2696/*------------------------------------------------------------------------* 2697 * usbd_transfer_timeout_ms 2698 * 2699 * This function is used to setup a timeout on the given USB 2700 * transfer. If the timeout has been deferred the callback given by 2701 * "cb" will get called after "ms" milliseconds. 2702 *------------------------------------------------------------------------*/ 2703void 2704usbd_transfer_timeout_ms(struct usb_xfer *xfer, 2705 void (*cb) (void *arg), usb_timeout_t ms) 2706{ 2707 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2708 2709 /* defer delay */ 2710 usb_callout_reset(&xfer->timeout_handle, 2711 USB_MS_TO_TICKS(ms), cb, xfer); 2712} 2713 2714/*------------------------------------------------------------------------* 2715 * usbd_callback_wrapper_sub 2716 * 2717 * - This function will update variables in an USB transfer after 2718 * that the USB transfer is complete. 2719 * 2720 * - This function is used to start the next USB transfer on the 2721 * ep transfer queue, if any. 2722 * 2723 * NOTE: In some special cases the USB transfer will not be removed from 2724 * the pipe queue, but remain first. To enforce USB transfer removal call 2725 * this function passing the error code "USB_ERR_CANCELLED". 2726 * 2727 * Return values: 2728 * 0: Success. 2729 * Else: The callback has been deferred. 2730 *------------------------------------------------------------------------*/ 2731static uint8_t 2732usbd_callback_wrapper_sub(struct usb_xfer *xfer) 2733{ 2734 struct usb_endpoint *ep; 2735 struct usb_bus *bus; 2736 usb_frcount_t x; 2737 2738 bus = xfer->xroot->bus; 2739 2740 if ((!xfer->flags_int.open) && 2741 (!xfer->flags_int.did_close)) { 2742 DPRINTF("close\n"); 2743 USB_BUS_LOCK(bus); 2744 (xfer->endpoint->methods->close) (xfer); 2745 USB_BUS_UNLOCK(bus); 2746 /* only close once */ 2747 xfer->flags_int.did_close = 1; 2748 return (1); /* wait for new callback */ 2749 } 2750 /* 2751 * If we have a non-hardware induced error we 2752 * need to do the DMA delay! 2753 */ 2754 if (xfer->error != 0 && !xfer->flags_int.did_dma_delay && 2755 (xfer->error == USB_ERR_CANCELLED || 2756 xfer->error == USB_ERR_TIMEOUT || 2757 bus->methods->start_dma_delay != NULL)) { 2758 2759 usb_timeout_t temp; 2760 2761 /* only delay once */ 2762 xfer->flags_int.did_dma_delay = 1; 2763 2764 /* we can not cancel this delay */ 2765 xfer->flags_int.can_cancel_immed = 0; 2766 2767 temp = usbd_get_dma_delay(xfer->xroot->udev); 2768 2769 DPRINTFN(3, "DMA delay, %u ms, " 2770 "on %p\n", temp, xfer); 2771 2772 if (temp != 0) { 2773 USB_BUS_LOCK(bus); 2774 /* 2775 * Some hardware solutions have dedicated 2776 * events when it is safe to free DMA'ed 2777 * memory. For the other hardware platforms we 2778 * use a static delay. 2779 */ 2780 if (bus->methods->start_dma_delay != NULL) { 2781 (bus->methods->start_dma_delay) (xfer); 2782 } else { 2783 usbd_transfer_timeout_ms(xfer, 2784 (void (*)(void *))&usb_dma_delay_done_cb, 2785 temp); 2786 } 2787 USB_BUS_UNLOCK(bus); 2788 return (1); /* wait for new callback */ 2789 } 2790 } 2791 /* check actual number of frames */ 2792 if (xfer->aframes > xfer->nframes) { 2793 if (xfer->error == 0) { 2794 panic("%s: actual number of frames, %d, is " 2795 "greater than initial number of frames, %d\n", 2796 __FUNCTION__, xfer->aframes, xfer->nframes); 2797 } else { 2798 /* just set some valid value */ 2799 xfer->aframes = xfer->nframes; 2800 } 2801 } 2802 /* compute actual length */ 2803 xfer->actlen = 0; 2804 2805 for (x = 0; x != xfer->aframes; x++) { 2806 xfer->actlen += xfer->frlengths[x]; 2807 } 2808 2809 /* 2810 * Frames that were not transferred get zero actual length in 2811 * case the USB device driver does not check the actual number 2812 * of frames transferred, "xfer->aframes": 2813 */ 2814 for (; x < xfer->nframes; x++) { 2815 usbd_xfer_set_frame_len(xfer, x, 0); 2816 } 2817 2818 /* check actual length */ 2819 if (xfer->actlen > xfer->sumlen) { 2820 if (xfer->error == 0) { 2821 panic("%s: actual length, %d, is greater than " 2822 "initial length, %d\n", 2823 __FUNCTION__, xfer->actlen, xfer->sumlen); 2824 } else { 2825 /* just set some valid value */ 2826 xfer->actlen = xfer->sumlen; 2827 } 2828 } 2829 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n", 2830 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen, 2831 xfer->aframes, xfer->nframes); 2832 2833 if (xfer->error) { 2834 /* end of control transfer, if any */ 2835 xfer->flags_int.control_act = 0; 2836 2837 /* check if we should block the execution queue */ 2838 if ((xfer->error != USB_ERR_CANCELLED) && 2839 (xfer->flags.pipe_bof)) { 2840 DPRINTFN(2, "xfer=%p: Block On Failure " 2841 "on endpoint=%p\n", xfer, xfer->endpoint); 2842 goto done; 2843 } 2844 } else { 2845 /* check for short transfers */ 2846 if (xfer->actlen < xfer->sumlen) { 2847 2848 /* end of control transfer, if any */ 2849 xfer->flags_int.control_act = 0; 2850 2851 if (!xfer->flags_int.short_xfer_ok) { 2852 xfer->error = USB_ERR_SHORT_XFER; 2853 if (xfer->flags.pipe_bof) { 2854 DPRINTFN(2, "xfer=%p: Block On Failure on " 2855 "Short Transfer on endpoint %p.\n", 2856 xfer, xfer->endpoint); 2857 goto done; 2858 } 2859 } 2860 } else { 2861 /* 2862 * Check if we are in the middle of a 2863 * control transfer: 2864 */ 2865 if (xfer->flags_int.control_act) { 2866 DPRINTFN(5, "xfer=%p: Control transfer " 2867 "active on endpoint=%p\n", xfer, xfer->endpoint); 2868 goto done; 2869 } 2870 } 2871 } 2872 2873 ep = xfer->endpoint; 2874 2875 /* 2876 * If the current USB transfer is completing we need to start the 2877 * next one: 2878 */ 2879 USB_BUS_LOCK(bus); 2880 if (ep->endpoint_q[xfer->stream_id].curr == xfer) { 2881 usb_command_wrapper(&ep->endpoint_q[xfer->stream_id], NULL); 2882 2883 if (ep->endpoint_q[xfer->stream_id].curr != NULL || 2884 TAILQ_FIRST(&ep->endpoint_q[xfer->stream_id].head) != NULL) { 2885 /* there is another USB transfer waiting */ 2886 } else { 2887 /* this is the last USB transfer */ 2888 /* clear isochronous sync flag */ 2889 xfer->endpoint->is_synced = 0; 2890 } 2891 } 2892 USB_BUS_UNLOCK(bus); 2893done: 2894 return (0); 2895} 2896 2897/*------------------------------------------------------------------------* 2898 * usb_command_wrapper 2899 * 2900 * This function is used to execute commands non-recursivly on an USB 2901 * transfer. 2902 *------------------------------------------------------------------------*/ 2903void 2904usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer) 2905{ 2906 if (xfer) { 2907 /* 2908 * If the transfer is not already processing, 2909 * queue it! 2910 */ 2911 if (pq->curr != xfer) { 2912 usbd_transfer_enqueue(pq, xfer); 2913 if (pq->curr != NULL) { 2914 /* something is already processing */ 2915 DPRINTFN(6, "busy %p\n", pq->curr); 2916 return; 2917 } 2918 } 2919 } else { 2920 /* Get next element in queue */ 2921 pq->curr = NULL; 2922 } 2923 2924 if (!pq->recurse_1) { 2925 2926 do { 2927 2928 /* set both recurse flags */ 2929 pq->recurse_1 = 1; 2930 pq->recurse_2 = 1; 2931 2932 if (pq->curr == NULL) { 2933 xfer = TAILQ_FIRST(&pq->head); 2934 if (xfer) { 2935 TAILQ_REMOVE(&pq->head, xfer, 2936 wait_entry); 2937 xfer->wait_queue = NULL; 2938 pq->curr = xfer; 2939 } else { 2940 break; 2941 } 2942 } 2943 DPRINTFN(6, "cb %p (enter)\n", pq->curr); 2944 (pq->command) (pq); 2945 DPRINTFN(6, "cb %p (leave)\n", pq->curr); 2946 2947 } while (!pq->recurse_2); 2948 2949 /* clear first recurse flag */ 2950 pq->recurse_1 = 0; 2951 2952 } else { 2953 /* clear second recurse flag */ 2954 pq->recurse_2 = 0; 2955 } 2956} 2957 2958/*------------------------------------------------------------------------* 2959 * usbd_ctrl_transfer_setup 2960 * 2961 * This function is used to setup the default USB control endpoint 2962 * transfer. 2963 *------------------------------------------------------------------------*/ 2964void 2965usbd_ctrl_transfer_setup(struct usb_device *udev) 2966{ 2967 struct usb_xfer *xfer; 2968 uint8_t no_resetup; 2969 uint8_t iface_index; 2970 2971 /* check for root HUB */ 2972 if (udev->parent_hub == NULL) 2973 return; 2974repeat: 2975 2976 xfer = udev->ctrl_xfer[0]; 2977 if (xfer) { 2978 USB_XFER_LOCK(xfer); 2979 no_resetup = 2980 ((xfer->address == udev->address) && 2981 (udev->ctrl_ep_desc.wMaxPacketSize[0] == 2982 udev->ddesc.bMaxPacketSize)); 2983 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2984 if (no_resetup) { 2985 /* 2986 * NOTE: checking "xfer->address" and 2987 * starting the USB transfer must be 2988 * atomic! 2989 */ 2990 usbd_transfer_start(xfer); 2991 } 2992 } 2993 USB_XFER_UNLOCK(xfer); 2994 } else { 2995 no_resetup = 0; 2996 } 2997 2998 if (no_resetup) { 2999 /* 3000 * All parameters are exactly the same like before. 3001 * Just return. 3002 */ 3003 return; 3004 } 3005 /* 3006 * Update wMaxPacketSize for the default control endpoint: 3007 */ 3008 udev->ctrl_ep_desc.wMaxPacketSize[0] = 3009 udev->ddesc.bMaxPacketSize; 3010 3011 /* 3012 * Unsetup any existing USB transfer: 3013 */ 3014 usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 3015 3016 /* 3017 * Reset clear stall error counter. 3018 */ 3019 udev->clear_stall_errors = 0; 3020 3021 /* 3022 * Try to setup a new USB transfer for the 3023 * default control endpoint: 3024 */ 3025 iface_index = 0; 3026 if (usbd_transfer_setup(udev, &iface_index, 3027 udev->ctrl_xfer, usb_control_ep_cfg, USB_CTRL_XFER_MAX, NULL, 3028 &udev->device_mtx)) { 3029 DPRINTFN(0, "could not setup default " 3030 "USB transfer\n"); 3031 } else { 3032 goto repeat; 3033 } 3034} 3035 3036/*------------------------------------------------------------------------* 3037 * usbd_clear_data_toggle - factored out code 3038 * 3039 * NOTE: the intention of this function is not to reset the hardware 3040 * data toggle. 3041 *------------------------------------------------------------------------*/ 3042void 3043usbd_clear_stall_locked(struct usb_device *udev, struct usb_endpoint *ep) 3044{ 3045 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 3046 3047 /* check that we have a valid case */ 3048 if (udev->flags.usb_mode == USB_MODE_HOST && 3049 udev->parent_hub != NULL && 3050 udev->bus->methods->clear_stall != NULL && 3051 ep->methods != NULL) { 3052 (udev->bus->methods->clear_stall) (udev, ep); 3053 } 3054} 3055 3056/*------------------------------------------------------------------------* 3057 * usbd_clear_data_toggle - factored out code 3058 * 3059 * NOTE: the intention of this function is not to reset the hardware 3060 * data toggle on the USB device side. 3061 *------------------------------------------------------------------------*/ 3062void 3063usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep) 3064{ 3065 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep); 3066 3067 USB_BUS_LOCK(udev->bus); 3068 ep->toggle_next = 0; 3069 /* some hardware needs a callback to clear the data toggle */ 3070 usbd_clear_stall_locked(udev, ep); 3071 USB_BUS_UNLOCK(udev->bus); 3072} 3073 3074/*------------------------------------------------------------------------* 3075 * usbd_clear_stall_callback - factored out clear stall callback 3076 * 3077 * Input parameters: 3078 * xfer1: Clear Stall Control Transfer 3079 * xfer2: Stalled USB Transfer 3080 * 3081 * This function is NULL safe. 3082 * 3083 * Return values: 3084 * 0: In progress 3085 * Else: Finished 3086 * 3087 * Clear stall config example: 3088 * 3089 * static const struct usb_config my_clearstall = { 3090 * .type = UE_CONTROL, 3091 * .endpoint = 0, 3092 * .direction = UE_DIR_ANY, 3093 * .interval = 50, //50 milliseconds 3094 * .bufsize = sizeof(struct usb_device_request), 3095 * .timeout = 1000, //1.000 seconds 3096 * .callback = &my_clear_stall_callback, // ** 3097 * .usb_mode = USB_MODE_HOST, 3098 * }; 3099 * 3100 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback" 3101 * passing the correct parameters. 3102 *------------------------------------------------------------------------*/ 3103uint8_t 3104usbd_clear_stall_callback(struct usb_xfer *xfer1, 3105 struct usb_xfer *xfer2) 3106{ 3107 struct usb_device_request req; 3108 3109 if (xfer2 == NULL) { 3110 /* looks like we are tearing down */ 3111 DPRINTF("NULL input parameter\n"); 3112 return (0); 3113 } 3114 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED); 3115 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED); 3116 3117 switch (USB_GET_STATE(xfer1)) { 3118 case USB_ST_SETUP: 3119 3120 /* 3121 * pre-clear the data toggle to DATA0 ("umass.c" and 3122 * "ata-usb.c" depends on this) 3123 */ 3124 3125 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint); 3126 3127 /* setup a clear-stall packet */ 3128 3129 req.bmRequestType = UT_WRITE_ENDPOINT; 3130 req.bRequest = UR_CLEAR_FEATURE; 3131 USETW(req.wValue, UF_ENDPOINT_HALT); 3132 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress; 3133 req.wIndex[1] = 0; 3134 USETW(req.wLength, 0); 3135 3136 /* 3137 * "usbd_transfer_setup_sub()" will ensure that 3138 * we have sufficient room in the buffer for 3139 * the request structure! 3140 */ 3141 3142 /* copy in the transfer */ 3143 3144 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); 3145 3146 /* set length */ 3147 xfer1->frlengths[0] = sizeof(req); 3148 xfer1->nframes = 1; 3149 3150 usbd_transfer_submit(xfer1); 3151 return (0); 3152 3153 case USB_ST_TRANSFERRED: 3154 break; 3155 3156 default: /* Error */ 3157 if (xfer1->error == USB_ERR_CANCELLED) { 3158 return (0); 3159 } 3160 break; 3161 } 3162 return (1); /* Clear Stall Finished */ 3163} 3164 3165/*------------------------------------------------------------------------* 3166 * usbd_transfer_poll 3167 * 3168 * The following function gets called from the USB keyboard driver and 3169 * UMASS when the system has paniced. 3170 * 3171 * NOTE: It is currently not possible to resume normal operation on 3172 * the USB controller which has been polled, due to clearing of the 3173 * "up_dsleep" and "up_msleep" flags. 3174 *------------------------------------------------------------------------*/ 3175void 3176usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max) 3177{ 3178 struct usb_xfer *xfer; 3179 struct usb_xfer_root *xroot; 3180 struct usb_device *udev; 3181 struct usb_proc_msg *pm; 3182 uint16_t n; 3183 uint16_t drop_bus; 3184 uint16_t drop_xfer; 3185 3186 for (n = 0; n != max; n++) { 3187 /* Extra checks to avoid panic */ 3188 xfer = ppxfer[n]; 3189 if (xfer == NULL) 3190 continue; /* no USB transfer */ 3191 xroot = xfer->xroot; 3192 if (xroot == NULL) 3193 continue; /* no USB root */ 3194 udev = xroot->udev; 3195 if (udev == NULL) 3196 continue; /* no USB device */ 3197 if (udev->bus == NULL) 3198 continue; /* no BUS structure */ 3199 if (udev->bus->methods == NULL) 3200 continue; /* no BUS methods */ 3201 if (udev->bus->methods->xfer_poll == NULL) 3202 continue; /* no poll method */ 3203 3204 /* make sure that the BUS mutex is not locked */ 3205 drop_bus = 0; 3206 while (mtx_owned(&xroot->udev->bus->bus_mtx) && !SCHEDULER_STOPPED()) { 3207 mtx_unlock(&xroot->udev->bus->bus_mtx); 3208 drop_bus++; 3209 } 3210 3211 /* make sure that the transfer mutex is not locked */ 3212 drop_xfer = 0; 3213 while (mtx_owned(xroot->xfer_mtx) && !SCHEDULER_STOPPED()) { 3214 mtx_unlock(xroot->xfer_mtx); 3215 drop_xfer++; 3216 } 3217 3218 /* Make sure cv_signal() and cv_broadcast() is not called */ 3219 udev->bus->control_xfer_proc.up_msleep = 0; 3220 udev->bus->explore_proc.up_msleep = 0; 3221 udev->bus->giant_callback_proc.up_msleep = 0; 3222 udev->bus->non_giant_callback_proc.up_msleep = 0; 3223 3224 /* poll USB hardware */ 3225 (udev->bus->methods->xfer_poll) (udev->bus); 3226 3227 USB_BUS_LOCK(xroot->bus); 3228 3229 /* check for clear stall */ 3230 if (udev->ctrl_xfer[1] != NULL) { 3231 3232 /* poll clear stall start */ 3233 pm = &udev->cs_msg[0].hdr; 3234 (pm->pm_callback) (pm); 3235 /* poll clear stall done thread */ 3236 pm = &udev->ctrl_xfer[1]-> 3237 xroot->done_m[0].hdr; 3238 (pm->pm_callback) (pm); 3239 } 3240 3241 /* poll done thread */ 3242 pm = &xroot->done_m[0].hdr; 3243 (pm->pm_callback) (pm); 3244 3245 USB_BUS_UNLOCK(xroot->bus); 3246 3247 /* restore transfer mutex */ 3248 while (drop_xfer--) 3249 mtx_lock(xroot->xfer_mtx); 3250 3251 /* restore BUS mutex */ 3252 while (drop_bus--) 3253 mtx_lock(&xroot->udev->bus->bus_mtx); 3254 } 3255} 3256 3257static void 3258usbd_get_std_packet_size(struct usb_std_packet_size *ptr, 3259 uint8_t type, enum usb_dev_speed speed) 3260{ 3261 static const uint16_t intr_range_max[USB_SPEED_MAX] = { 3262 [USB_SPEED_LOW] = 8, 3263 [USB_SPEED_FULL] = 64, 3264 [USB_SPEED_HIGH] = 1024, 3265 [USB_SPEED_VARIABLE] = 1024, 3266 [USB_SPEED_SUPER] = 1024, 3267 }; 3268 3269 static const uint16_t isoc_range_max[USB_SPEED_MAX] = { 3270 [USB_SPEED_LOW] = 0, /* invalid */ 3271 [USB_SPEED_FULL] = 1023, 3272 [USB_SPEED_HIGH] = 1024, 3273 [USB_SPEED_VARIABLE] = 3584, 3274 [USB_SPEED_SUPER] = 1024, 3275 }; 3276 3277 static const uint16_t control_min[USB_SPEED_MAX] = { 3278 [USB_SPEED_LOW] = 8, 3279 [USB_SPEED_FULL] = 8, 3280 [USB_SPEED_HIGH] = 64, 3281 [USB_SPEED_VARIABLE] = 512, 3282 [USB_SPEED_SUPER] = 512, 3283 }; 3284 3285 static const uint16_t bulk_min[USB_SPEED_MAX] = { 3286 [USB_SPEED_LOW] = 8, 3287 [USB_SPEED_FULL] = 8, 3288 [USB_SPEED_HIGH] = 512, 3289 [USB_SPEED_VARIABLE] = 512, 3290 [USB_SPEED_SUPER] = 1024, 3291 }; 3292 3293 uint16_t temp; 3294 3295 memset(ptr, 0, sizeof(*ptr)); 3296 3297 switch (type) { 3298 case UE_INTERRUPT: 3299 ptr->range.max = intr_range_max[speed]; 3300 break; 3301 case UE_ISOCHRONOUS: 3302 ptr->range.max = isoc_range_max[speed]; 3303 break; 3304 default: 3305 if (type == UE_BULK) 3306 temp = bulk_min[speed]; 3307 else /* UE_CONTROL */ 3308 temp = control_min[speed]; 3309 3310 /* default is fixed */ 3311 ptr->fixed[0] = temp; 3312 ptr->fixed[1] = temp; 3313 ptr->fixed[2] = temp; 3314 ptr->fixed[3] = temp; 3315 3316 if (speed == USB_SPEED_FULL) { 3317 /* multiple sizes */ 3318 ptr->fixed[1] = 16; 3319 ptr->fixed[2] = 32; 3320 ptr->fixed[3] = 64; 3321 } 3322 if ((speed == USB_SPEED_VARIABLE) && 3323 (type == UE_BULK)) { 3324 /* multiple sizes */ 3325 ptr->fixed[2] = 1024; 3326 ptr->fixed[3] = 1536; 3327 } 3328 break; 3329 } 3330} 3331 3332void * 3333usbd_xfer_softc(struct usb_xfer *xfer) 3334{ 3335 return (xfer->priv_sc); 3336} 3337 3338void * 3339usbd_xfer_get_priv(struct usb_xfer *xfer) 3340{ 3341 return (xfer->priv_fifo); 3342} 3343 3344void 3345usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr) 3346{ 3347 xfer->priv_fifo = ptr; 3348} 3349 3350uint8_t 3351usbd_xfer_state(struct usb_xfer *xfer) 3352{ 3353 return (xfer->usb_state); 3354} 3355 3356void 3357usbd_xfer_set_flag(struct usb_xfer *xfer, int flag) 3358{ 3359 switch (flag) { 3360 case USB_FORCE_SHORT_XFER: 3361 xfer->flags.force_short_xfer = 1; 3362 break; 3363 case USB_SHORT_XFER_OK: 3364 xfer->flags.short_xfer_ok = 1; 3365 break; 3366 case USB_MULTI_SHORT_OK: 3367 xfer->flags.short_frames_ok = 1; 3368 break; 3369 case USB_MANUAL_STATUS: 3370 xfer->flags.manual_status = 1; 3371 break; 3372 } 3373} 3374 3375void 3376usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag) 3377{ 3378 switch (flag) { 3379 case USB_FORCE_SHORT_XFER: 3380 xfer->flags.force_short_xfer = 0; 3381 break; 3382 case USB_SHORT_XFER_OK: 3383 xfer->flags.short_xfer_ok = 0; 3384 break; 3385 case USB_MULTI_SHORT_OK: 3386 xfer->flags.short_frames_ok = 0; 3387 break; 3388 case USB_MANUAL_STATUS: 3389 xfer->flags.manual_status = 0; 3390 break; 3391 } 3392} 3393 3394/* 3395 * The following function returns in milliseconds when the isochronous 3396 * transfer was completed by the hardware. The returned value wraps 3397 * around 65536 milliseconds. 3398 */ 3399uint16_t 3400usbd_xfer_get_timestamp(struct usb_xfer *xfer) 3401{ 3402 return (xfer->isoc_time_complete); 3403} 3404