usb_transfer.c revision 187180
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_transfer.c 187180 2009-01-13 19:04:58Z thompsa $ */ 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#include <dev/usb2/include/usb2_mfunc.h> 28#include <dev/usb2/include/usb2_error.h> 29#include <dev/usb2/include/usb2_standard.h> 30#include <dev/usb2/include/usb2_defs.h> 31 32#define USB_DEBUG_VAR usb2_debug 33 34#include <dev/usb2/core/usb2_core.h> 35#include <dev/usb2/core/usb2_busdma.h> 36#include <dev/usb2/core/usb2_process.h> 37#include <dev/usb2/core/usb2_transfer.h> 38#include <dev/usb2/core/usb2_device.h> 39#include <dev/usb2/core/usb2_debug.h> 40#include <dev/usb2/core/usb2_util.h> 41 42#include <dev/usb2/controller/usb2_controller.h> 43#include <dev/usb2/controller/usb2_bus.h> 44 45struct usb2_std_packet_size { 46 struct { 47 uint16_t min; /* inclusive */ 48 uint16_t max; /* inclusive */ 49 } range; 50 51 uint16_t fixed[4]; 52}; 53 54/* 55 * This table stores the all the allowed packet sizes based on 56 * endpoint type and USB speed: 57 */ 58static const struct usb2_std_packet_size 59 usb2_std_packet_size[4][USB_SPEED_MAX] = { 60 61 [UE_INTERRUPT] = { 62 [USB_SPEED_LOW] = {.range = {0, 8}}, 63 [USB_SPEED_FULL] = {.range = {0, 64}}, 64 [USB_SPEED_HIGH] = {.range = {0, 1024}}, 65 [USB_SPEED_VARIABLE] = {.range = {0, 1024}}, 66 [USB_SPEED_SUPER] = {.range = {0, 1024}}, 67 }, 68 69 [UE_CONTROL] = { 70 [USB_SPEED_LOW] = {.fixed = {8, 8, 8, 8}}, 71 [USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}}, 72 [USB_SPEED_HIGH] = {.fixed = {64, 64, 64, 64}}, 73 [USB_SPEED_VARIABLE] = {.fixed = {512, 512, 512, 512}}, 74 [USB_SPEED_SUPER] = {.fixed = {512, 512, 512, 512}}, 75 }, 76 77 [UE_BULK] = { 78 [USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}}, /* invalid */ 79 [USB_SPEED_FULL] = {.fixed = {8, 16, 32, 64}}, 80 [USB_SPEED_HIGH] = {.fixed = {512, 512, 512, 512}}, 81 [USB_SPEED_VARIABLE] = {.fixed = {512, 512, 1024, 1536}}, 82 [USB_SPEED_SUPER] = {.fixed = {1024, 1024, 1024, 1024}}, 83 }, 84 85 [UE_ISOCHRONOUS] = { 86 [USB_SPEED_LOW] = {.fixed = {0, 0, 0, 0}}, /* invalid */ 87 [USB_SPEED_FULL] = {.range = {0, 1023}}, 88 [USB_SPEED_HIGH] = {.range = {0, 1024}}, 89 [USB_SPEED_VARIABLE] = {.range = {0, 3584}}, 90 [USB_SPEED_SUPER] = {.range = {0, 1024}}, 91 }, 92}; 93 94static const struct usb2_config usb2_control_ep_cfg[USB_DEFAULT_XFER_MAX] = { 95 96 /* This transfer is used for generic control endpoint transfers */ 97 98 [0] = { 99 .type = UE_CONTROL, 100 .endpoint = 0x00, /* Control endpoint */ 101 .direction = UE_DIR_ANY, 102 .mh.bufsize = 1024, /* bytes */ 103 .mh.flags = {.proxy_buffer = 1,.short_xfer_ok = 1,}, 104 .mh.callback = &usb2_do_request_callback, 105 .md.bufsize = 1024, /* bytes */ 106 .md.flags = {.proxy_buffer = 1,.short_xfer_ok = 0,}, 107 .md.callback = &usb2_handle_request_callback, 108 }, 109 110 /* This transfer is used for generic clear stall only */ 111 112 [1] = { 113 .type = UE_CONTROL, 114 .endpoint = 0x00, /* Control pipe */ 115 .direction = UE_DIR_ANY, 116 .mh.bufsize = sizeof(struct usb2_device_request), 117 .mh.flags = {}, 118 .mh.callback = &usb2_do_clear_stall_callback, 119 .mh.timeout = 1000, /* 1 second */ 120 .mh.interval = 50, /* 50ms */ 121 }, 122}; 123 124/* function prototypes */ 125 126static void usb2_update_max_frame_size(struct usb2_xfer *); 127static void usb2_transfer_unsetup_sub(struct usb2_xfer_root *, uint8_t); 128static void usb2_control_transfer_init(struct usb2_xfer *); 129static uint8_t usb2_start_hardware_sub(struct usb2_xfer *); 130static void usb2_callback_proc(struct usb2_proc_msg *); 131static void usb2_callback_ss_done_defer(struct usb2_xfer *); 132static void usb2_callback_wrapper(struct usb2_xfer_queue *); 133static void usb2_dma_delay_done_cb(void *); 134static void usb2_transfer_start_cb(void *); 135static uint8_t usb2_callback_wrapper_sub(struct usb2_xfer *); 136 137/*------------------------------------------------------------------------* 138 * usb2_update_max_frame_size 139 * 140 * This function updates the maximum frame size, hence high speed USB 141 * can transfer multiple consecutive packets. 142 *------------------------------------------------------------------------*/ 143static void 144usb2_update_max_frame_size(struct usb2_xfer *xfer) 145{ 146 /* compute maximum frame size */ 147 148 if (xfer->max_packet_count == 2) { 149 xfer->max_frame_size = 2 * xfer->max_packet_size; 150 } else if (xfer->max_packet_count == 3) { 151 xfer->max_frame_size = 3 * xfer->max_packet_size; 152 } else { 153 xfer->max_frame_size = xfer->max_packet_size; 154 } 155} 156 157/*------------------------------------------------------------------------* 158 * usb2_get_dma_delay 159 * 160 * The following function is called when we need to 161 * synchronize with DMA hardware. 162 * 163 * Returns: 164 * 0: no DMA delay required 165 * Else: milliseconds of DMA delay 166 *------------------------------------------------------------------------*/ 167uint32_t 168usb2_get_dma_delay(struct usb2_bus *bus) 169{ 170 uint32_t temp = 0; 171 172 if (bus->methods->get_dma_delay) { 173 (bus->methods->get_dma_delay) (bus, &temp); 174 /* 175 * Round up and convert to milliseconds. Note that we use 176 * 1024 milliseconds per second. to save a division. 177 */ 178 temp += 0x3FF; 179 temp /= 0x400; 180 } 181 return (temp); 182} 183 184/*------------------------------------------------------------------------* 185 * usb2_transfer_setup_sub_malloc 186 * 187 * This function will allocate one or more DMA'able memory chunks 188 * according to "size", "align" and "count" arguments. "ppc" is 189 * pointed to a linear array of USB page caches afterwards. 190 * 191 * Returns: 192 * 0: Success 193 * Else: Failure 194 *------------------------------------------------------------------------*/ 195uint8_t 196usb2_transfer_setup_sub_malloc(struct usb2_setup_params *parm, 197 struct usb2_page_cache **ppc, uint32_t size, uint32_t align, 198 uint32_t count) 199{ 200 struct usb2_page_cache *pc; 201 struct usb2_page *pg; 202 void *buf; 203 uint32_t n_dma_pc; 204 uint32_t n_obj; 205 uint32_t x; 206 uint32_t y; 207 uint32_t r; 208 uint32_t z; 209 210 USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n", 211 align)); 212 USB_ASSERT(size > 0, ("Invalid size = 0!\n")); 213 214 if (count == 0) { 215 return (0); /* nothing to allocate */ 216 } 217 /* 218 * Make sure that the size is aligned properly. 219 */ 220 size = -((-size) & (-align)); 221 222 /* 223 * Try multi-allocation chunks to reduce the number of DMA 224 * allocations, hence DMA allocations are slow. 225 */ 226 if (size >= PAGE_SIZE) { 227 n_dma_pc = count; 228 n_obj = 1; 229 } else { 230 /* compute number of objects per page */ 231 n_obj = (PAGE_SIZE / size); 232 /* 233 * Compute number of DMA chunks, rounded up 234 * to nearest one: 235 */ 236 n_dma_pc = ((count + n_obj - 1) / n_obj); 237 } 238 239 if (parm->buf == NULL) { 240 /* for the future */ 241 parm->dma_page_ptr += n_dma_pc; 242 parm->dma_page_cache_ptr += n_dma_pc; 243 parm->dma_page_ptr += count; 244 parm->xfer_page_cache_ptr += count; 245 return (0); 246 } 247 for (x = 0; x != n_dma_pc; x++) { 248 /* need to initialize the page cache */ 249 parm->dma_page_cache_ptr[x].tag_parent = 250 &parm->curr_xfer->xroot->dma_parent_tag; 251 } 252 for (x = 0; x != count; x++) { 253 /* need to initialize the page cache */ 254 parm->xfer_page_cache_ptr[x].tag_parent = 255 &parm->curr_xfer->xroot->dma_parent_tag; 256 } 257 258 if (ppc) { 259 *ppc = parm->xfer_page_cache_ptr; 260 } 261 r = count; /* set remainder count */ 262 z = n_obj * size; /* set allocation size */ 263 pc = parm->xfer_page_cache_ptr; 264 pg = parm->dma_page_ptr; 265 266 for (x = 0; x != n_dma_pc; x++) { 267 268 if (r < n_obj) { 269 /* compute last remainder */ 270 z = r * size; 271 n_obj = r; 272 } 273 if (usb2_pc_alloc_mem(parm->dma_page_cache_ptr, 274 pg, z, align)) { 275 return (1); /* failure */ 276 } 277 /* Set beginning of current buffer */ 278 buf = parm->dma_page_cache_ptr->buffer; 279 /* Make room for one DMA page cache and one page */ 280 parm->dma_page_cache_ptr++; 281 pg++; 282 283 for (y = 0; (y != n_obj); y++, r--, pc++, pg++) { 284 285 /* Load sub-chunk into DMA */ 286 if (usb2_pc_dmamap_create(pc, size)) { 287 return (1); /* failure */ 288 } 289 pc->buffer = USB_ADD_BYTES(buf, y * size); 290 pc->page_start = pg; 291 292 mtx_lock(pc->tag_parent->mtx); 293 if (usb2_pc_load_mem(pc, size, 1 /* synchronous */ )) { 294 mtx_unlock(pc->tag_parent->mtx); 295 return (1); /* failure */ 296 } 297 mtx_unlock(pc->tag_parent->mtx); 298 } 299 } 300 301 parm->xfer_page_cache_ptr = pc; 302 parm->dma_page_ptr = pg; 303 return (0); 304} 305 306/*------------------------------------------------------------------------* 307 * usb2_transfer_setup_sub - transfer setup subroutine 308 * 309 * This function must be called from the "xfer_setup" callback of the 310 * USB Host or Device controller driver when setting up an USB 311 * transfer. This function will setup correct packet sizes, buffer 312 * sizes, flags and more, that are stored in the "usb2_xfer" 313 * structure. 314 *------------------------------------------------------------------------*/ 315void 316usb2_transfer_setup_sub(struct usb2_setup_params *parm) 317{ 318 enum { 319 REQ_SIZE = 8, 320 MIN_PKT = 8, 321 }; 322 struct usb2_xfer *xfer = parm->curr_xfer; 323 const struct usb2_config_sub *setup_sub = parm->curr_setup_sub; 324 struct usb2_endpoint_descriptor *edesc; 325 struct usb2_std_packet_size std_size; 326 uint32_t n_frlengths; 327 uint32_t n_frbuffers; 328 uint32_t x; 329 uint8_t type; 330 uint8_t zmps; 331 332 /* 333 * Sanity check. The following parameters must be initialized before 334 * calling this function. 335 */ 336 if ((parm->hc_max_packet_size == 0) || 337 (parm->hc_max_packet_count == 0) || 338 (parm->hc_max_frame_size == 0)) { 339 parm->err = USB_ERR_INVAL; 340 goto done; 341 } 342 edesc = xfer->pipe->edesc; 343 344 type = (edesc->bmAttributes & UE_XFERTYPE); 345 346 xfer->flags = setup_sub->flags; 347 xfer->nframes = setup_sub->frames; 348 xfer->timeout = setup_sub->timeout; 349 xfer->callback = setup_sub->callback; 350 xfer->interval = setup_sub->interval; 351 xfer->endpoint = edesc->bEndpointAddress; 352 xfer->max_packet_size = UGETW(edesc->wMaxPacketSize); 353 xfer->max_packet_count = 1; 354 /* make a shadow copy: */ 355 xfer->flags_int.usb2_mode = parm->udev->flags.usb2_mode; 356 357 parm->bufsize = setup_sub->bufsize; 358 359 if (parm->speed == USB_SPEED_HIGH) { 360 xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3; 361 xfer->max_packet_size &= 0x7FF; 362 } 363 /* range check "max_packet_count" */ 364 365 if (xfer->max_packet_count > parm->hc_max_packet_count) { 366 xfer->max_packet_count = parm->hc_max_packet_count; 367 } 368 /* filter "wMaxPacketSize" according to HC capabilities */ 369 370 if ((xfer->max_packet_size > parm->hc_max_packet_size) || 371 (xfer->max_packet_size == 0)) { 372 xfer->max_packet_size = parm->hc_max_packet_size; 373 } 374 /* filter "wMaxPacketSize" according to standard sizes */ 375 376 std_size = usb2_std_packet_size[type][parm->speed]; 377 378 if (std_size.range.min || std_size.range.max) { 379 380 if (xfer->max_packet_size < std_size.range.min) { 381 xfer->max_packet_size = std_size.range.min; 382 } 383 if (xfer->max_packet_size > std_size.range.max) { 384 xfer->max_packet_size = std_size.range.max; 385 } 386 } else { 387 388 if (xfer->max_packet_size >= std_size.fixed[3]) { 389 xfer->max_packet_size = std_size.fixed[3]; 390 } else if (xfer->max_packet_size >= std_size.fixed[2]) { 391 xfer->max_packet_size = std_size.fixed[2]; 392 } else if (xfer->max_packet_size >= std_size.fixed[1]) { 393 xfer->max_packet_size = std_size.fixed[1]; 394 } else { 395 /* only one possibility left */ 396 xfer->max_packet_size = std_size.fixed[0]; 397 } 398 } 399 400 /* compute "max_frame_size" */ 401 402 usb2_update_max_frame_size(xfer); 403 404 /* check interrupt interval and transfer pre-delay */ 405 406 if (type == UE_ISOCHRONOUS) { 407 408 uint32_t frame_limit; 409 410 xfer->interval = 0; /* not used, must be zero */ 411 xfer->flags_int.isochronous_xfr = 1; /* set flag */ 412 413 if (xfer->timeout == 0) { 414 /* 415 * set a default timeout in 416 * case something goes wrong! 417 */ 418 xfer->timeout = 1000 / 4; 419 } 420 switch (parm->speed) { 421 case USB_SPEED_LOW: 422 case USB_SPEED_FULL: 423 frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER; 424 break; 425 default: 426 frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER; 427 break; 428 } 429 430 if (xfer->nframes > frame_limit) { 431 /* 432 * this is not going to work 433 * cross hardware 434 */ 435 parm->err = USB_ERR_INVAL; 436 goto done; 437 } 438 if (xfer->nframes == 0) { 439 /* 440 * this is not a valid value 441 */ 442 parm->err = USB_ERR_ZERO_NFRAMES; 443 goto done; 444 } 445 } else { 446 447 /* 448 * if a value is specified use that else check the endpoint 449 * descriptor 450 */ 451 if (xfer->interval == 0) { 452 453 if (type == UE_INTERRUPT) { 454 455 xfer->interval = edesc->bInterval; 456 457 switch (parm->speed) { 458 case USB_SPEED_SUPER: 459 case USB_SPEED_VARIABLE: 460 /* 125us -> 1ms */ 461 if (xfer->interval < 4) 462 xfer->interval = 1; 463 else if (xfer->interval > 16) 464 xfer->interval = (1<<(16-4)); 465 else 466 xfer->interval = 467 (1 << (xfer->interval-4)); 468 break; 469 case USB_SPEED_HIGH: 470 /* 125us -> 1ms */ 471 xfer->interval /= 8; 472 break; 473 default: 474 break; 475 } 476 if (xfer->interval == 0) { 477 /* 478 * One millisecond is the smallest 479 * interval we support: 480 */ 481 xfer->interval = 1; 482 } 483 } 484 } 485 } 486 487 /* 488 * NOTE: we do not allow "max_packet_size" or "max_frame_size" 489 * to be equal to zero when setting up USB transfers, hence 490 * this leads to alot of extra code in the USB kernel. 491 */ 492 493 if ((xfer->max_frame_size == 0) || 494 (xfer->max_packet_size == 0)) { 495 496 zmps = 1; 497 498 if ((parm->bufsize <= MIN_PKT) && 499 (type != UE_CONTROL) && 500 (type != UE_BULK)) { 501 502 /* workaround */ 503 xfer->max_packet_size = MIN_PKT; 504 xfer->max_packet_count = 1; 505 parm->bufsize = 0; /* automatic setup length */ 506 usb2_update_max_frame_size(xfer); 507 508 } else { 509 parm->err = USB_ERR_ZERO_MAXP; 510 goto done; 511 } 512 513 } else { 514 zmps = 0; 515 } 516 517 /* 518 * check if we should setup a default 519 * length: 520 */ 521 522 if (parm->bufsize == 0) { 523 524 parm->bufsize = xfer->max_frame_size; 525 526 if (type == UE_ISOCHRONOUS) { 527 parm->bufsize *= xfer->nframes; 528 } 529 } 530 /* 531 * check if we are about to setup a proxy 532 * type of buffer: 533 */ 534 535 if (xfer->flags.proxy_buffer) { 536 537 /* round bufsize up */ 538 539 parm->bufsize += (xfer->max_frame_size - 1); 540 541 if (parm->bufsize < xfer->max_frame_size) { 542 /* length wrapped around */ 543 parm->err = USB_ERR_INVAL; 544 goto done; 545 } 546 /* subtract remainder */ 547 548 parm->bufsize -= (parm->bufsize % xfer->max_frame_size); 549 550 /* add length of USB device request structure, if any */ 551 552 if (type == UE_CONTROL) { 553 parm->bufsize += REQ_SIZE; /* SETUP message */ 554 } 555 } 556 xfer->max_data_length = parm->bufsize; 557 558 /* Setup "n_frlengths" and "n_frbuffers" */ 559 560 if (type == UE_ISOCHRONOUS) { 561 n_frlengths = xfer->nframes; 562 n_frbuffers = 1; 563 } else { 564 565 if (type == UE_CONTROL) { 566 xfer->flags_int.control_xfr = 1; 567 if (xfer->nframes == 0) { 568 if (parm->bufsize <= REQ_SIZE) { 569 /* 570 * there will never be any data 571 * stage 572 */ 573 xfer->nframes = 1; 574 } else { 575 xfer->nframes = 2; 576 } 577 } 578 } else { 579 if (xfer->nframes == 0) { 580 xfer->nframes = 1; 581 } 582 } 583 584 n_frlengths = xfer->nframes; 585 n_frbuffers = xfer->nframes; 586 } 587 588 /* 589 * check if we have room for the 590 * USB device request structure: 591 */ 592 593 if (type == UE_CONTROL) { 594 595 if (xfer->max_data_length < REQ_SIZE) { 596 /* length wrapped around or too small bufsize */ 597 parm->err = USB_ERR_INVAL; 598 goto done; 599 } 600 xfer->max_data_length -= REQ_SIZE; 601 } 602 /* setup "frlengths" */ 603 604 xfer->frlengths = parm->xfer_length_ptr; 605 606 parm->xfer_length_ptr += n_frlengths; 607 608 /* setup "frbuffers" */ 609 610 xfer->frbuffers = parm->xfer_page_cache_ptr; 611 612 parm->xfer_page_cache_ptr += n_frbuffers; 613 614 /* 615 * check if we need to setup 616 * a local buffer: 617 */ 618 619 if (!xfer->flags.ext_buffer) { 620 621 /* align data */ 622 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 623 624 if (parm->buf) { 625 626 xfer->local_buffer = 627 USB_ADD_BYTES(parm->buf, parm->size[0]); 628 629 usb2_set_frame_offset(xfer, 0, 0); 630 631 if ((type == UE_CONTROL) && (n_frbuffers > 1)) { 632 usb2_set_frame_offset(xfer, REQ_SIZE, 1); 633 } 634 } 635 parm->size[0] += parm->bufsize; 636 637 /* align data again */ 638 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 639 } 640 /* 641 * Compute maximum buffer size 642 */ 643 644 if (parm->bufsize_max < parm->bufsize) { 645 parm->bufsize_max = parm->bufsize; 646 } 647 if (xfer->flags_int.bdma_enable) { 648 /* 649 * Setup "dma_page_ptr". 650 * 651 * Proof for formula below: 652 * 653 * Assume there are three USB frames having length "a", "b" and 654 * "c". These USB frames will at maximum need "z" 655 * "usb2_page" structures. "z" is given by: 656 * 657 * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) + 658 * ((c / USB_PAGE_SIZE) + 2); 659 * 660 * Constraining "a", "b" and "c" like this: 661 * 662 * (a + b + c) <= parm->bufsize 663 * 664 * We know that: 665 * 666 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2)); 667 * 668 * Here is the general formula: 669 */ 670 xfer->dma_page_ptr = parm->dma_page_ptr; 671 parm->dma_page_ptr += (2 * n_frbuffers); 672 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE); 673 } 674 if (zmps) { 675 /* correct maximum data length */ 676 xfer->max_data_length = 0; 677 } 678 /* subtract USB frame remainder from "hc_max_frame_size" */ 679 680 xfer->max_usb2_frame_size = 681 (parm->hc_max_frame_size - 682 (parm->hc_max_frame_size % xfer->max_frame_size)); 683 684 if (xfer->max_usb2_frame_size == 0) { 685 parm->err = USB_ERR_INVAL; 686 goto done; 687 } 688 /* initialize max frame count */ 689 690 xfer->max_frame_count = xfer->nframes; 691 692 /* initialize frame buffers */ 693 694 if (parm->buf) { 695 for (x = 0; x != n_frbuffers; x++) { 696 xfer->frbuffers[x].tag_parent = 697 &xfer->xroot->dma_parent_tag; 698 699 if (xfer->flags_int.bdma_enable && 700 (parm->bufsize_max > 0)) { 701 702 if (usb2_pc_dmamap_create( 703 xfer->frbuffers + x, 704 parm->bufsize_max)) { 705 parm->err = USB_ERR_NOMEM; 706 goto done; 707 } 708 } 709 } 710 } 711done: 712 if (parm->err) { 713 /* 714 * Set some dummy values so that we avoid division by zero: 715 */ 716 xfer->max_usb2_frame_size = 1; 717 xfer->max_frame_size = 1; 718 xfer->max_packet_size = 1; 719 xfer->max_data_length = 0; 720 xfer->nframes = 0; 721 xfer->max_frame_count = 0; 722 } 723} 724 725/*------------------------------------------------------------------------* 726 * usb2_transfer_setup - setup an array of USB transfers 727 * 728 * NOTE: You must always call "usb2_transfer_unsetup" after calling 729 * "usb2_transfer_setup" if success was returned. 730 * 731 * The idea is that the USB device driver should pre-allocate all its 732 * transfers by one call to this function. 733 * 734 * Return values: 735 * 0: Success 736 * Else: Failure 737 *------------------------------------------------------------------------*/ 738usb2_error_t 739usb2_transfer_setup(struct usb2_device *udev, 740 const uint8_t *ifaces, struct usb2_xfer **ppxfer, 741 const struct usb2_config *setup_start, uint16_t n_setup, 742 void *priv_sc, struct mtx *xfer_mtx) 743{ 744 struct usb2_xfer dummy; 745 struct usb2_setup_params parm; 746 const struct usb2_config *setup_end = setup_start + n_setup; 747 const struct usb2_config *setup; 748 struct usb2_pipe *pipe; 749 struct usb2_xfer_root *info; 750 struct usb2_xfer *xfer; 751 void *buf = NULL; 752 uint16_t n; 753 uint16_t refcount; 754 755 parm.err = 0; 756 refcount = 0; 757 info = NULL; 758 759 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 760 "usb2_transfer_setup can sleep!"); 761 762 /* do some checking first */ 763 764 if (n_setup == 0) { 765 DPRINTFN(6, "setup array has zero length!\n"); 766 return (USB_ERR_INVAL); 767 } 768 if (ifaces == 0) { 769 DPRINTFN(6, "ifaces array is NULL!\n"); 770 return (USB_ERR_INVAL); 771 } 772 if (xfer_mtx == NULL) { 773 DPRINTFN(6, "using global lock\n"); 774 xfer_mtx = &Giant; 775 } 776 /* sanity checks */ 777 for (setup = setup_start, n = 0; 778 setup != setup_end; setup++, n++) { 779 if ((setup->mh.bufsize == 0xffffffff) || 780 (setup->md.bufsize == 0xffffffff)) { 781 parm.err = USB_ERR_BAD_BUFSIZE; 782 DPRINTF("invalid bufsize\n"); 783 } 784 if ((setup->mh.callback == NULL) && 785 (setup->md.callback == NULL)) { 786 parm.err = USB_ERR_NO_CALLBACK; 787 DPRINTF("no callback\n"); 788 } 789 ppxfer[n] = NULL; 790 } 791 792 if (parm.err) { 793 goto done; 794 } 795 bzero(&parm, sizeof(parm)); 796 797 parm.udev = udev; 798 parm.speed = usb2_get_speed(udev); 799 parm.hc_max_packet_count = 1; 800 801 if (parm.speed >= USB_SPEED_MAX) { 802 parm.err = USB_ERR_INVAL; 803 goto done; 804 } 805 /* setup all transfers */ 806 807 while (1) { 808 809 if (buf) { 810 /* 811 * Initialize the "usb2_xfer_root" structure, 812 * which is common for all our USB transfers. 813 */ 814 info = USB_ADD_BYTES(buf, 0); 815 816 info->memory_base = buf; 817 info->memory_size = parm.size[0]; 818 819 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]); 820 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]); 821 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]); 822 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]); 823 824 usb2_cv_init(&info->cv_drain, "WDRAIN"); 825 826 info->xfer_mtx = xfer_mtx; 827 828 usb2_dma_tag_setup(&info->dma_parent_tag, 829 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag, 830 xfer_mtx, &usb2_bdma_done_event, info, 32, parm.dma_tag_max); 831 832 info->bus = udev->bus; 833 info->udev = udev; 834 835 TAILQ_INIT(&info->done_q.head); 836 info->done_q.command = &usb2_callback_wrapper; 837 838 TAILQ_INIT(&info->dma_q.head); 839 info->dma_q.command = &usb2_bdma_work_loop; 840 841 info->done_m[0].hdr.pm_callback = &usb2_callback_proc; 842 info->done_m[0].xroot = info; 843 info->done_m[1].hdr.pm_callback = &usb2_callback_proc; 844 info->done_m[1].xroot = info; 845 846 if (xfer_mtx == &Giant) 847 info->done_p = 848 &udev->bus->giant_callback_proc; 849 else 850 info->done_p = 851 &udev->bus->non_giant_callback_proc; 852 } 853 /* reset sizes */ 854 855 parm.size[0] = 0; 856 parm.buf = buf; 857 parm.size[0] += sizeof(info[0]); 858 859 for (setup = setup_start, n = 0; 860 setup != setup_end; setup++, n++) { 861 862 /* select mode specific structure */ 863 if (udev->flags.usb2_mode == USB_MODE_HOST) { 864 parm.curr_setup_sub = &setup->mh; 865 } else { 866 parm.curr_setup_sub = &setup->md; 867 } 868 /* skip USB transfers without callbacks: */ 869 if (parm.curr_setup_sub->callback == NULL) { 870 continue; 871 } 872 /* see if there is a matching endpoint */ 873 pipe = usb2_get_pipe(udev, 874 ifaces[setup->if_index], setup); 875 876 if (!pipe) { 877 if (parm.curr_setup_sub->flags.no_pipe_ok) { 878 continue; 879 } 880 parm.err = USB_ERR_NO_PIPE; 881 goto done; 882 } 883 /* store current setup pointer */ 884 parm.curr_setup = setup; 885 886 /* align data properly */ 887 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 888 889 if (buf) { 890 891 /* 892 * Common initialization of the 893 * "usb2_xfer" structure. 894 */ 895 xfer = USB_ADD_BYTES(buf, parm.size[0]); 896 897 ppxfer[n] = xfer; 898 xfer->address = udev->address; 899 xfer->priv_sc = priv_sc; 900 xfer->xroot = info; 901 info->setup_refcount++; 902 903 usb2_callout_init_mtx(&xfer->timeout_handle, 904 &udev->bus->bus_mtx, 0); 905 } else { 906 /* 907 * Setup a dummy xfer, hence we are 908 * writing to the "usb2_xfer" 909 * structure pointed to by "xfer" 910 * before we have allocated any 911 * memory: 912 */ 913 xfer = &dummy; 914 bzero(&dummy, sizeof(dummy)); 915 refcount++; 916 } 917 918 parm.size[0] += sizeof(xfer[0]); 919 920 xfer->pipe = pipe; 921 922 if (buf) { 923 /* 924 * Increment the pipe refcount. This 925 * basically prevents setting a new 926 * configuration and alternate setting 927 * when USB transfers are in use on 928 * the given interface. Search the USB 929 * code for "pipe->refcount" if you 930 * want more information. 931 */ 932 xfer->pipe->refcount++; 933 } 934 parm.methods = xfer->pipe->methods; 935 parm.curr_xfer = xfer; 936 937 /* 938 * Call the Host or Device controller transfer setup 939 * routine: 940 */ 941 (udev->bus->methods->xfer_setup) (&parm); 942 943 if (parm.err) { 944 goto done; 945 } 946 } 947 948 if (buf || parm.err) { 949 goto done; 950 } 951 if (refcount == 0) { 952 /* no transfers - nothing to do ! */ 953 goto done; 954 } 955 /* align data properly */ 956 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 957 958 /* store offset temporarily */ 959 parm.size[1] = parm.size[0]; 960 961 /* 962 * The number of DMA tags required depends on 963 * the number of endpoints. The current estimate 964 * for maximum number of DMA tags per endpoint 965 * is two. 966 */ 967 parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX); 968 969 /* 970 * DMA tags for QH, TD, Data and more. 971 */ 972 parm.dma_tag_max += 8; 973 974 parm.dma_tag_p += parm.dma_tag_max; 975 976 parm.size[0] += ((uint8_t *)parm.dma_tag_p) - 977 ((uint8_t *)0); 978 979 /* align data properly */ 980 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 981 982 /* store offset temporarily */ 983 parm.size[3] = parm.size[0]; 984 985 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) - 986 ((uint8_t *)0); 987 988 /* align data properly */ 989 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 990 991 /* store offset temporarily */ 992 parm.size[4] = parm.size[0]; 993 994 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) - 995 ((uint8_t *)0); 996 997 /* store end offset temporarily */ 998 parm.size[5] = parm.size[0]; 999 1000 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) - 1001 ((uint8_t *)0); 1002 1003 /* store end offset temporarily */ 1004 1005 parm.size[2] = parm.size[0]; 1006 1007 /* align data properly */ 1008 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1009 1010 parm.size[6] = parm.size[0]; 1011 1012 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) - 1013 ((uint8_t *)0); 1014 1015 /* align data properly */ 1016 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1017 1018 /* allocate zeroed memory */ 1019 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO); 1020 1021 if (buf == NULL) { 1022 parm.err = USB_ERR_NOMEM; 1023 DPRINTFN(0, "cannot allocate memory block for " 1024 "configuration (%d bytes)\n", 1025 parm.size[0]); 1026 goto done; 1027 } 1028 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]); 1029 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]); 1030 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]); 1031 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]); 1032 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]); 1033 } 1034 1035done: 1036 if (buf) { 1037 if (info->setup_refcount == 0) { 1038 /* 1039 * "usb2_transfer_unsetup_sub" will unlock 1040 * the bus mutex before returning ! 1041 */ 1042 USB_BUS_LOCK(info->bus); 1043 1044 /* something went wrong */ 1045 usb2_transfer_unsetup_sub(info, 0); 1046 } 1047 } 1048 if (parm.err) { 1049 usb2_transfer_unsetup(ppxfer, n_setup); 1050 } 1051 return (parm.err); 1052} 1053 1054/*------------------------------------------------------------------------* 1055 * usb2_transfer_unsetup_sub - factored out code 1056 *------------------------------------------------------------------------*/ 1057static void 1058usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay) 1059{ 1060 struct usb2_page_cache *pc; 1061 uint32_t temp; 1062 1063 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 1064 1065 /* wait for any outstanding DMA operations */ 1066 1067 if (needs_delay) { 1068 temp = usb2_get_dma_delay(info->bus); 1069 usb2_pause_mtx(&info->bus->bus_mtx, temp); 1070 } 1071 1072 /* make sure that our done messages are not queued anywhere */ 1073 usb2_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); 1074 1075 USB_BUS_UNLOCK(info->bus); 1076 1077 /* free DMA'able memory, if any */ 1078 pc = info->dma_page_cache_start; 1079 while (pc != info->dma_page_cache_end) { 1080 usb2_pc_free_mem(pc); 1081 pc++; 1082 } 1083 1084 /* free DMA maps in all "xfer->frbuffers" */ 1085 pc = info->xfer_page_cache_start; 1086 while (pc != info->xfer_page_cache_end) { 1087 usb2_pc_dmamap_destroy(pc); 1088 pc++; 1089 } 1090 1091 /* free all DMA tags */ 1092 usb2_dma_tag_unsetup(&info->dma_parent_tag); 1093 1094 usb2_cv_destroy(&info->cv_drain); 1095 1096 /* 1097 * free the "memory_base" last, hence the "info" structure is 1098 * contained within the "memory_base"! 1099 */ 1100 free(info->memory_base, M_USB); 1101} 1102 1103/*------------------------------------------------------------------------* 1104 * usb2_transfer_unsetup - unsetup/free an array of USB transfers 1105 * 1106 * NOTE: All USB transfers in progress will get called back passing 1107 * the error code "USB_ERR_CANCELLED" before this function 1108 * returns. 1109 *------------------------------------------------------------------------*/ 1110void 1111usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup) 1112{ 1113 struct usb2_xfer *xfer; 1114 struct usb2_xfer_root *info; 1115 uint8_t needs_delay = 0; 1116 1117 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1118 "usb2_transfer_unsetup can sleep!"); 1119 1120 while (n_setup--) { 1121 xfer = pxfer[n_setup]; 1122 1123 if (xfer) { 1124 if (xfer->pipe) { 1125 USB_XFER_LOCK(xfer); 1126 USB_BUS_LOCK(xfer->xroot->bus); 1127 1128 /* 1129 * HINT: when you start/stop a transfer, it 1130 * might be a good idea to directly use the 1131 * "pxfer[]" structure: 1132 * 1133 * usb2_transfer_start(sc->pxfer[0]); 1134 * usb2_transfer_stop(sc->pxfer[0]); 1135 * 1136 * That way, if your code has many parts that 1137 * will not stop running under the same 1138 * lock, in other words "xfer_mtx", the 1139 * usb2_transfer_start and 1140 * usb2_transfer_stop functions will simply 1141 * return when they detect a NULL pointer 1142 * argument. 1143 * 1144 * To avoid any races we clear the "pxfer[]" 1145 * pointer while holding the private mutex 1146 * of the driver: 1147 */ 1148 pxfer[n_setup] = NULL; 1149 1150 USB_BUS_UNLOCK(xfer->xroot->bus); 1151 USB_XFER_UNLOCK(xfer); 1152 1153 usb2_transfer_drain(xfer); 1154 1155 if (xfer->flags_int.bdma_enable) { 1156 needs_delay = 1; 1157 } 1158 /* 1159 * NOTE: default pipe does not have an 1160 * interface, even if pipe->iface_index == 0 1161 */ 1162 xfer->pipe->refcount--; 1163 1164 } else { 1165 /* clear the transfer pointer */ 1166 pxfer[n_setup] = NULL; 1167 } 1168 1169 usb2_callout_drain(&xfer->timeout_handle); 1170 1171 if (xfer->xroot) { 1172 info = xfer->xroot; 1173 1174 USB_BUS_LOCK(info->bus); 1175 1176 USB_ASSERT(info->setup_refcount != 0, 1177 ("Invalid setup " 1178 "reference count!\n")); 1179 1180 info->setup_refcount--; 1181 1182 if (info->setup_refcount == 0) { 1183 usb2_transfer_unsetup_sub(info, 1184 needs_delay); 1185 } else { 1186 USB_BUS_UNLOCK(info->bus); 1187 } 1188 } 1189 } 1190 } 1191} 1192 1193/*------------------------------------------------------------------------* 1194 * usb2_control_transfer_init - factored out code 1195 * 1196 * In USB Device Mode we have to wait for the SETUP packet which 1197 * containst the "struct usb2_device_request" structure, before we can 1198 * transfer any data. In USB Host Mode we already have the SETUP 1199 * packet at the moment the USB transfer is started. This leads us to 1200 * having to setup the USB transfer at two different places in 1201 * time. This function just contains factored out control transfer 1202 * initialisation code, so that we don't duplicate the code. 1203 *------------------------------------------------------------------------*/ 1204static void 1205usb2_control_transfer_init(struct usb2_xfer *xfer) 1206{ 1207 struct usb2_device_request req; 1208 1209 /* copy out the USB request header */ 1210 1211 usb2_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); 1212 1213 /* setup remainder */ 1214 1215 xfer->flags_int.control_rem = UGETW(req.wLength); 1216 1217 /* copy direction to endpoint variable */ 1218 1219 xfer->endpoint &= ~(UE_DIR_IN | UE_DIR_OUT); 1220 xfer->endpoint |= 1221 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT; 1222} 1223 1224/*------------------------------------------------------------------------* 1225 * usb2_start_hardware_sub 1226 * 1227 * This function handles initialisation of control transfers. Control 1228 * transfers are special in that regard that they can both transmit 1229 * and receive data. 1230 * 1231 * Return values: 1232 * 0: Success 1233 * Else: Failure 1234 *------------------------------------------------------------------------*/ 1235static uint8_t 1236usb2_start_hardware_sub(struct usb2_xfer *xfer) 1237{ 1238 uint32_t len; 1239 1240 /* Check for control endpoint stall */ 1241 if (xfer->flags.stall_pipe) { 1242 /* no longer active */ 1243 xfer->flags_int.control_act = 0; 1244 } 1245 /* 1246 * Check if there is a control 1247 * transfer in progress: 1248 */ 1249 if (xfer->flags_int.control_act) { 1250 1251 if (xfer->flags_int.control_hdr) { 1252 1253 /* clear send header flag */ 1254 1255 xfer->flags_int.control_hdr = 0; 1256 1257 /* setup control transfer */ 1258 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1259 usb2_control_transfer_init(xfer); 1260 } 1261 } 1262 /* get data length */ 1263 1264 len = xfer->sumlen; 1265 1266 } else { 1267 1268 /* the size of the SETUP structure is hardcoded ! */ 1269 1270 if (xfer->frlengths[0] != sizeof(struct usb2_device_request)) { 1271 DPRINTFN(0, "Wrong framelength %u != %zu\n", 1272 xfer->frlengths[0], sizeof(struct 1273 usb2_device_request)); 1274 goto error; 1275 } 1276 /* check USB mode */ 1277 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1278 1279 /* check number of frames */ 1280 if (xfer->nframes != 1) { 1281 /* 1282 * We need to receive the setup 1283 * message first so that we know the 1284 * data direction! 1285 */ 1286 DPRINTF("Misconfigured transfer\n"); 1287 goto error; 1288 } 1289 /* 1290 * Set a dummy "control_rem" value. This 1291 * variable will be overwritten later by a 1292 * call to "usb2_control_transfer_init()" ! 1293 */ 1294 xfer->flags_int.control_rem = 0xFFFF; 1295 } else { 1296 1297 /* setup "endpoint" and "control_rem" */ 1298 1299 usb2_control_transfer_init(xfer); 1300 } 1301 1302 /* set transfer-header flag */ 1303 1304 xfer->flags_int.control_hdr = 1; 1305 1306 /* get data length */ 1307 1308 len = (xfer->sumlen - sizeof(struct usb2_device_request)); 1309 } 1310 1311 /* check if there is a length mismatch */ 1312 1313 if (len > xfer->flags_int.control_rem) { 1314 DPRINTFN(0, "Length greater than remaining length!\n"); 1315 goto error; 1316 } 1317 /* check if we are doing a short transfer */ 1318 1319 if (xfer->flags.force_short_xfer) { 1320 xfer->flags_int.control_rem = 0; 1321 } else { 1322 if ((len != xfer->max_data_length) && 1323 (len != xfer->flags_int.control_rem) && 1324 (xfer->nframes != 1)) { 1325 DPRINTFN(0, "Short control transfer without " 1326 "force_short_xfer set!\n"); 1327 goto error; 1328 } 1329 xfer->flags_int.control_rem -= len; 1330 } 1331 1332 /* the status part is executed when "control_act" is 0 */ 1333 1334 if ((xfer->flags_int.control_rem > 0) || 1335 (xfer->flags.manual_status)) { 1336 /* don't execute the STATUS stage yet */ 1337 xfer->flags_int.control_act = 1; 1338 1339 /* sanity check */ 1340 if ((!xfer->flags_int.control_hdr) && 1341 (xfer->nframes == 1)) { 1342 /* 1343 * This is not a valid operation! 1344 */ 1345 DPRINTFN(0, "Invalid parameter " 1346 "combination\n"); 1347 goto error; 1348 } 1349 } else { 1350 /* time to execute the STATUS stage */ 1351 xfer->flags_int.control_act = 0; 1352 } 1353 return (0); /* success */ 1354 1355error: 1356 return (1); /* failure */ 1357} 1358 1359/*------------------------------------------------------------------------* 1360 * usb2_start_hardware - start USB hardware for the given transfer 1361 * 1362 * This function should only be called from the USB callback. 1363 *------------------------------------------------------------------------*/ 1364void 1365usb2_start_hardware(struct usb2_xfer *xfer) 1366{ 1367 uint32_t x; 1368 1369 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n", 1370 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? 1371 "read" : "write"); 1372 1373#if USB_DEBUG 1374 if (USB_DEBUG_VAR > 0) { 1375 USB_BUS_LOCK(xfer->xroot->bus); 1376 1377 usb2_dump_pipe(xfer->pipe); 1378 1379 USB_BUS_UNLOCK(xfer->xroot->bus); 1380 } 1381#endif 1382 1383 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1384 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED); 1385 1386 /* Only open the USB transfer once! */ 1387 if (!xfer->flags_int.open) { 1388 xfer->flags_int.open = 1; 1389 1390 DPRINTF("open\n"); 1391 1392 USB_BUS_LOCK(xfer->xroot->bus); 1393 (xfer->pipe->methods->open) (xfer); 1394 USB_BUS_UNLOCK(xfer->xroot->bus); 1395 } 1396 /* set "transferring" flag */ 1397 xfer->flags_int.transferring = 1; 1398 1399 /* increment power reference */ 1400 usb2_transfer_power_ref(xfer, 1); 1401 1402 /* 1403 * Check if the transfer is waiting on a queue, most 1404 * frequently the "done_q": 1405 */ 1406 if (xfer->wait_queue) { 1407 USB_BUS_LOCK(xfer->xroot->bus); 1408 usb2_transfer_dequeue(xfer); 1409 USB_BUS_UNLOCK(xfer->xroot->bus); 1410 } 1411 /* clear "did_dma_delay" flag */ 1412 xfer->flags_int.did_dma_delay = 0; 1413 1414 /* clear "did_close" flag */ 1415 xfer->flags_int.did_close = 0; 1416 1417 /* clear "bdma_setup" flag */ 1418 xfer->flags_int.bdma_setup = 0; 1419 1420 /* by default we cannot cancel any USB transfer immediately */ 1421 xfer->flags_int.can_cancel_immed = 0; 1422 1423 /* clear lengths and frame counts by default */ 1424 xfer->sumlen = 0; 1425 xfer->actlen = 0; 1426 xfer->aframes = 0; 1427 1428 /* clear any previous errors */ 1429 xfer->error = 0; 1430 1431 /* sanity check */ 1432 1433 if (xfer->nframes == 0) { 1434 if (xfer->flags.stall_pipe) { 1435 /* 1436 * Special case - want to stall without transferring 1437 * any data: 1438 */ 1439 DPRINTF("xfer=%p nframes=0: stall " 1440 "or clear stall!\n", xfer); 1441 USB_BUS_LOCK(xfer->xroot->bus); 1442 xfer->flags_int.can_cancel_immed = 1; 1443 /* start the transfer */ 1444 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer); 1445 USB_BUS_UNLOCK(xfer->xroot->bus); 1446 return; 1447 } 1448 USB_BUS_LOCK(xfer->xroot->bus); 1449 usb2_transfer_done(xfer, USB_ERR_INVAL); 1450 USB_BUS_UNLOCK(xfer->xroot->bus); 1451 return; 1452 } 1453 /* compute total transfer length */ 1454 1455 for (x = 0; x != xfer->nframes; x++) { 1456 xfer->sumlen += xfer->frlengths[x]; 1457 if (xfer->sumlen < xfer->frlengths[x]) { 1458 /* length wrapped around */ 1459 USB_BUS_LOCK(xfer->xroot->bus); 1460 usb2_transfer_done(xfer, USB_ERR_INVAL); 1461 USB_BUS_UNLOCK(xfer->xroot->bus); 1462 return; 1463 } 1464 } 1465 1466 /* clear some internal flags */ 1467 1468 xfer->flags_int.short_xfer_ok = 0; 1469 xfer->flags_int.short_frames_ok = 0; 1470 1471 /* check if this is a control transfer */ 1472 1473 if (xfer->flags_int.control_xfr) { 1474 1475 if (usb2_start_hardware_sub(xfer)) { 1476 USB_BUS_LOCK(xfer->xroot->bus); 1477 usb2_transfer_done(xfer, USB_ERR_STALLED); 1478 USB_BUS_UNLOCK(xfer->xroot->bus); 1479 return; 1480 } 1481 } 1482 /* 1483 * Setup filtered version of some transfer flags, 1484 * in case of data read direction 1485 */ 1486 if (USB_GET_DATA_ISREAD(xfer)) { 1487 1488 if (xfer->flags_int.control_xfr) { 1489 1490 /* 1491 * Control transfers do not support reception 1492 * of multiple short USB frames ! 1493 */ 1494 1495 if (xfer->flags.short_xfer_ok) { 1496 xfer->flags_int.short_xfer_ok = 1; 1497 } 1498 } else { 1499 1500 if (xfer->flags.short_frames_ok) { 1501 xfer->flags_int.short_xfer_ok = 1; 1502 xfer->flags_int.short_frames_ok = 1; 1503 } else if (xfer->flags.short_xfer_ok) { 1504 xfer->flags_int.short_xfer_ok = 1; 1505 } 1506 } 1507 } 1508 /* 1509 * Check if BUS-DMA support is enabled and try to load virtual 1510 * buffers into DMA, if any: 1511 */ 1512 if (xfer->flags_int.bdma_enable) { 1513 /* insert the USB transfer last in the BUS-DMA queue */ 1514 usb2_command_wrapper(&xfer->xroot->dma_q, xfer); 1515 return; 1516 } 1517 /* 1518 * Enter the USB transfer into the Host Controller or 1519 * Device Controller schedule: 1520 */ 1521 usb2_pipe_enter(xfer); 1522} 1523 1524/*------------------------------------------------------------------------* 1525 * usb2_pipe_enter - factored out code 1526 *------------------------------------------------------------------------*/ 1527void 1528usb2_pipe_enter(struct usb2_xfer *xfer) 1529{ 1530 struct usb2_pipe *pipe; 1531 1532 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1533 1534 USB_BUS_LOCK(xfer->xroot->bus); 1535 1536 pipe = xfer->pipe; 1537 1538 DPRINTF("enter\n"); 1539 1540 /* enter the transfer */ 1541 (pipe->methods->enter) (xfer); 1542 1543 /* check cancelability */ 1544 if (pipe->methods->enter_is_cancelable) { 1545 xfer->flags_int.can_cancel_immed = 1; 1546 /* check for transfer error */ 1547 if (xfer->error) { 1548 /* some error has happened */ 1549 usb2_transfer_done(xfer, 0); 1550 USB_BUS_UNLOCK(xfer->xroot->bus); 1551 return; 1552 } 1553 } else { 1554 xfer->flags_int.can_cancel_immed = 0; 1555 } 1556 1557 /* start the transfer */ 1558 usb2_command_wrapper(&pipe->pipe_q, xfer); 1559 USB_BUS_UNLOCK(xfer->xroot->bus); 1560} 1561 1562/*------------------------------------------------------------------------* 1563 * usb2_transfer_start - start an USB transfer 1564 * 1565 * NOTE: Calling this function more than one time will only 1566 * result in a single transfer start, until the USB transfer 1567 * completes. 1568 *------------------------------------------------------------------------*/ 1569void 1570usb2_transfer_start(struct usb2_xfer *xfer) 1571{ 1572 if (xfer == NULL) { 1573 /* transfer is gone */ 1574 return; 1575 } 1576 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1577 1578 /* mark the USB transfer started */ 1579 1580 if (!xfer->flags_int.started) { 1581 xfer->flags_int.started = 1; 1582 } 1583 /* check if the USB transfer callback is already transferring */ 1584 1585 if (xfer->flags_int.transferring) { 1586 return; 1587 } 1588 USB_BUS_LOCK(xfer->xroot->bus); 1589 /* call the USB transfer callback */ 1590 usb2_callback_ss_done_defer(xfer); 1591 USB_BUS_UNLOCK(xfer->xroot->bus); 1592} 1593 1594/*------------------------------------------------------------------------* 1595 * usb2_transfer_stop - stop an USB transfer 1596 * 1597 * NOTE: Calling this function more than one time will only 1598 * result in a single transfer stop. 1599 * NOTE: When this function returns it is not safe to free nor 1600 * reuse any DMA buffers. See "usb2_transfer_drain()". 1601 *------------------------------------------------------------------------*/ 1602void 1603usb2_transfer_stop(struct usb2_xfer *xfer) 1604{ 1605 struct usb2_pipe *pipe; 1606 1607 if (xfer == NULL) { 1608 /* transfer is gone */ 1609 return; 1610 } 1611 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1612 1613 /* check if the USB transfer was ever opened */ 1614 1615 if (!xfer->flags_int.open) { 1616 /* nothing to do except clearing the "started" flag */ 1617 xfer->flags_int.started = 0; 1618 return; 1619 } 1620 /* try to stop the current USB transfer */ 1621 1622 USB_BUS_LOCK(xfer->xroot->bus); 1623 xfer->error = USB_ERR_CANCELLED;/* override any previous error */ 1624 /* 1625 * Clear "open" and "started" when both private and USB lock 1626 * is locked so that we don't get a race updating "flags_int" 1627 */ 1628 xfer->flags_int.open = 0; 1629 xfer->flags_int.started = 0; 1630 1631 /* 1632 * Check if we can cancel the USB transfer immediately. 1633 */ 1634 if (xfer->flags_int.transferring) { 1635 if (xfer->flags_int.can_cancel_immed && 1636 (!xfer->flags_int.did_close)) { 1637 DPRINTF("close\n"); 1638 /* 1639 * The following will lead to an USB_ERR_CANCELLED 1640 * error code being passed to the USB callback. 1641 */ 1642 (xfer->pipe->methods->close) (xfer); 1643 /* only close once */ 1644 xfer->flags_int.did_close = 1; 1645 } else { 1646 /* need to wait for the next done callback */ 1647 } 1648 } else { 1649 DPRINTF("close\n"); 1650 1651 /* close here and now */ 1652 (xfer->pipe->methods->close) (xfer); 1653 1654 /* 1655 * Any additional DMA delay is done by 1656 * "usb2_transfer_unsetup()". 1657 */ 1658 1659 /* 1660 * Special case. Check if we need to restart a blocked 1661 * pipe. 1662 */ 1663 pipe = xfer->pipe; 1664 1665 /* 1666 * If the current USB transfer is completing we need 1667 * to start the next one: 1668 */ 1669 if (pipe->pipe_q.curr == xfer) { 1670 usb2_command_wrapper(&pipe->pipe_q, NULL); 1671 } 1672 } 1673 1674 USB_BUS_UNLOCK(xfer->xroot->bus); 1675} 1676 1677/*------------------------------------------------------------------------* 1678 * usb2_transfer_pending 1679 * 1680 * This function will check if an USB transfer is pending which is a 1681 * little bit complicated! 1682 * Return values: 1683 * 0: Not pending 1684 * 1: Pending: The USB transfer will receive a callback in the future. 1685 *------------------------------------------------------------------------*/ 1686uint8_t 1687usb2_transfer_pending(struct usb2_xfer *xfer) 1688{ 1689 struct usb2_xfer_root *info; 1690 struct usb2_xfer_queue *pq; 1691 1692 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1693 1694 if (xfer->flags_int.transferring) { 1695 /* trivial case */ 1696 return (1); 1697 } 1698 USB_BUS_LOCK(xfer->xroot->bus); 1699 if (xfer->wait_queue) { 1700 /* we are waiting on a queue somewhere */ 1701 USB_BUS_UNLOCK(xfer->xroot->bus); 1702 return (1); 1703 } 1704 info = xfer->xroot; 1705 pq = &info->done_q; 1706 1707 if (pq->curr == xfer) { 1708 /* we are currently scheduled for callback */ 1709 USB_BUS_UNLOCK(xfer->xroot->bus); 1710 return (1); 1711 } 1712 /* we are not pending */ 1713 USB_BUS_UNLOCK(xfer->xroot->bus); 1714 return (0); 1715} 1716 1717/*------------------------------------------------------------------------* 1718 * usb2_transfer_drain 1719 * 1720 * This function will stop the USB transfer and wait for any 1721 * additional BUS-DMA and HW-DMA operations to complete. Buffers that 1722 * are loaded into DMA can safely be freed or reused after that this 1723 * function has returned. 1724 *------------------------------------------------------------------------*/ 1725void 1726usb2_transfer_drain(struct usb2_xfer *xfer) 1727{ 1728 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1729 "usb2_transfer_drain can sleep!"); 1730 1731 if (xfer == NULL) { 1732 /* transfer is gone */ 1733 return; 1734 } 1735 if (xfer->xroot->xfer_mtx != &Giant) { 1736 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED); 1737 } 1738 USB_XFER_LOCK(xfer); 1739 1740 usb2_transfer_stop(xfer); 1741 1742 while (usb2_transfer_pending(xfer)) { 1743 xfer->flags_int.draining = 1; 1744 /* 1745 * Wait until the current outstanding USB 1746 * transfer is complete ! 1747 */ 1748 usb2_cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); 1749 } 1750 USB_XFER_UNLOCK(xfer); 1751} 1752 1753/*------------------------------------------------------------------------* 1754 * usb2_set_frame_data 1755 * 1756 * This function sets the pointer of the buffer that should 1757 * loaded directly into DMA for the given USB frame. Passing "ptr" 1758 * equal to NULL while the corresponding "frlength" is greater 1759 * than zero gives undefined results! 1760 *------------------------------------------------------------------------*/ 1761void 1762usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr, uint32_t frindex) 1763{ 1764 /* set virtual address to load and length */ 1765 xfer->frbuffers[frindex].buffer = ptr; 1766} 1767 1768/*------------------------------------------------------------------------* 1769 * usb2_set_frame_offset 1770 * 1771 * This function sets the frame data buffer offset relative to the beginning 1772 * of the USB DMA buffer allocated for this USB transfer. 1773 *------------------------------------------------------------------------*/ 1774void 1775usb2_set_frame_offset(struct usb2_xfer *xfer, uint32_t offset, 1776 uint32_t frindex) 1777{ 1778 USB_ASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " 1779 "when the USB buffer is external!\n")); 1780 1781 /* set virtual address to load */ 1782 xfer->frbuffers[frindex].buffer = 1783 USB_ADD_BYTES(xfer->local_buffer, offset); 1784} 1785 1786/*------------------------------------------------------------------------* 1787 * usb2_callback_proc - factored out code 1788 * 1789 * This function performs USB callbacks. 1790 *------------------------------------------------------------------------*/ 1791static void 1792usb2_callback_proc(struct usb2_proc_msg *_pm) 1793{ 1794 struct usb2_done_msg *pm = (void *)_pm; 1795 struct usb2_xfer_root *info = pm->xroot; 1796 1797 /* Change locking order */ 1798 USB_BUS_UNLOCK(info->bus); 1799 1800 /* 1801 * We exploit the fact that the mutex is the same for all 1802 * callbacks that will be called from this thread: 1803 */ 1804 mtx_lock(info->xfer_mtx); 1805 USB_BUS_LOCK(info->bus); 1806 1807 /* Continue where we lost track */ 1808 usb2_command_wrapper(&info->done_q, 1809 info->done_q.curr); 1810 1811 mtx_unlock(info->xfer_mtx); 1812} 1813 1814/*------------------------------------------------------------------------* 1815 * usb2_callback_ss_done_defer 1816 * 1817 * This function will defer the start, stop and done callback to the 1818 * correct thread. 1819 *------------------------------------------------------------------------*/ 1820static void 1821usb2_callback_ss_done_defer(struct usb2_xfer *xfer) 1822{ 1823 struct usb2_xfer_root *info = xfer->xroot; 1824 struct usb2_xfer_queue *pq = &info->done_q; 1825 1826 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1827 1828 if (pq->curr != xfer) { 1829 usb2_transfer_enqueue(pq, xfer); 1830 } 1831 if (!pq->recurse_1) { 1832 1833 /* 1834 * We have to postpone the callback due to the fact we 1835 * will have a Lock Order Reversal, LOR, if we try to 1836 * proceed ! 1837 */ 1838 if (usb2_proc_msignal(info->done_p, 1839 &info->done_m[0], &info->done_m[1])) { 1840 /* ignore */ 1841 } 1842 } else { 1843 /* clear second recurse flag */ 1844 pq->recurse_2 = 0; 1845 } 1846 return; 1847 1848} 1849 1850/*------------------------------------------------------------------------* 1851 * usb2_callback_wrapper 1852 * 1853 * This is a wrapper for USB callbacks. This wrapper does some 1854 * auto-magic things like figuring out if we can call the callback 1855 * directly from the current context or if we need to wakeup the 1856 * interrupt process. 1857 *------------------------------------------------------------------------*/ 1858static void 1859usb2_callback_wrapper(struct usb2_xfer_queue *pq) 1860{ 1861 struct usb2_xfer *xfer = pq->curr; 1862 struct usb2_xfer_root *info = xfer->xroot; 1863 1864 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1865 if (!mtx_owned(xfer->xroot->xfer_mtx)) { 1866 /* 1867 * Cases that end up here: 1868 * 1869 * 5) HW interrupt done callback or other source. 1870 */ 1871 DPRINTFN(3, "case 5\n"); 1872 1873 /* 1874 * We have to postpone the callback due to the fact we 1875 * will have a Lock Order Reversal, LOR, if we try to 1876 * proceed ! 1877 */ 1878 if (usb2_proc_msignal(info->done_p, 1879 &info->done_m[0], &info->done_m[1])) { 1880 /* ignore */ 1881 } 1882 return; 1883 } 1884 /* 1885 * Cases that end up here: 1886 * 1887 * 1) We are starting a transfer 1888 * 2) We are prematurely calling back a transfer 1889 * 3) We are stopping a transfer 1890 * 4) We are doing an ordinary callback 1891 */ 1892 DPRINTFN(3, "case 1-4\n"); 1893 /* get next USB transfer in the queue */ 1894 info->done_q.curr = NULL; 1895 1896 USB_BUS_UNLOCK(xfer->xroot->bus); 1897 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED); 1898 1899 /* set correct USB state for callback */ 1900 if (!xfer->flags_int.transferring) { 1901 xfer->usb2_state = USB_ST_SETUP; 1902 if (!xfer->flags_int.started) { 1903 /* we got stopped before we even got started */ 1904 USB_BUS_LOCK(xfer->xroot->bus); 1905 goto done; 1906 } 1907 } else { 1908 1909 if (usb2_callback_wrapper_sub(xfer)) { 1910 /* the callback has been deferred */ 1911 USB_BUS_LOCK(xfer->xroot->bus); 1912 goto done; 1913 } 1914 /* decrement power reference */ 1915 usb2_transfer_power_ref(xfer, -1); 1916 1917 xfer->flags_int.transferring = 0; 1918 1919 if (xfer->error) { 1920 xfer->usb2_state = USB_ST_ERROR; 1921 } else { 1922 /* set transferred state */ 1923 xfer->usb2_state = USB_ST_TRANSFERRED; 1924 1925 /* sync DMA memory, if any */ 1926 if (xfer->flags_int.bdma_enable && 1927 (!xfer->flags_int.bdma_no_post_sync)) { 1928 usb2_bdma_post_sync(xfer); 1929 } 1930 } 1931 } 1932 1933 /* call processing routine */ 1934 (xfer->callback) (xfer); 1935 1936 /* pickup the USB mutex again */ 1937 USB_BUS_LOCK(xfer->xroot->bus); 1938 1939 /* 1940 * Check if we got started after that we got cancelled, but 1941 * before we managed to do the callback. 1942 */ 1943 if ((!xfer->flags_int.open) && 1944 (xfer->flags_int.started) && 1945 (xfer->usb2_state == USB_ST_ERROR)) { 1946 /* try to loop, but not recursivly */ 1947 usb2_command_wrapper(&info->done_q, xfer); 1948 return; 1949 } 1950 1951done: 1952 /* 1953 * Check if we are draining. 1954 */ 1955 if (xfer->flags_int.draining && 1956 (!xfer->flags_int.transferring)) { 1957 /* "usb2_transfer_drain()" is waiting for end of transfer */ 1958 xfer->flags_int.draining = 0; 1959 usb2_cv_broadcast(&xfer->xroot->cv_drain); 1960 } 1961 1962 /* do the next callback, if any */ 1963 usb2_command_wrapper(&info->done_q, 1964 info->done_q.curr); 1965} 1966 1967/*------------------------------------------------------------------------* 1968 * usb2_dma_delay_done_cb 1969 * 1970 * This function is called when the DMA delay has been exectuded, and 1971 * will make sure that the callback is called to complete the USB 1972 * transfer. This code path is ususally only used when there is an USB 1973 * error like USB_ERR_CANCELLED. 1974 *------------------------------------------------------------------------*/ 1975static void 1976usb2_dma_delay_done_cb(void *arg) 1977{ 1978 struct usb2_xfer *xfer = arg; 1979 1980 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1981 1982 DPRINTFN(3, "Completed %p\n", xfer); 1983 1984 /* queue callback for execution, again */ 1985 usb2_transfer_done(xfer, 0); 1986} 1987 1988/*------------------------------------------------------------------------* 1989 * usb2_transfer_dequeue 1990 * 1991 * - This function is used to remove an USB transfer from a USB 1992 * transfer queue. 1993 * 1994 * - This function can be called multiple times in a row. 1995 *------------------------------------------------------------------------*/ 1996void 1997usb2_transfer_dequeue(struct usb2_xfer *xfer) 1998{ 1999 struct usb2_xfer_queue *pq; 2000 2001 pq = xfer->wait_queue; 2002 if (pq) { 2003 TAILQ_REMOVE(&pq->head, xfer, wait_entry); 2004 xfer->wait_queue = NULL; 2005 } 2006} 2007 2008/*------------------------------------------------------------------------* 2009 * usb2_transfer_enqueue 2010 * 2011 * - This function is used to insert an USB transfer into a USB * 2012 * transfer queue. 2013 * 2014 * - This function can be called multiple times in a row. 2015 *------------------------------------------------------------------------*/ 2016void 2017usb2_transfer_enqueue(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer) 2018{ 2019 /* 2020 * Insert the USB transfer into the queue, if it is not 2021 * already on a USB transfer queue: 2022 */ 2023 if (xfer->wait_queue == NULL) { 2024 xfer->wait_queue = pq; 2025 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); 2026 } 2027} 2028 2029/*------------------------------------------------------------------------* 2030 * usb2_transfer_done 2031 * 2032 * - This function is used to remove an USB transfer from the busdma, 2033 * pipe or interrupt queue. 2034 * 2035 * - This function is used to queue the USB transfer on the done 2036 * queue. 2037 * 2038 * - This function is used to stop any USB transfer timeouts. 2039 *------------------------------------------------------------------------*/ 2040void 2041usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error) 2042{ 2043 struct usb2_xfer_queue *pq; 2044 2045 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2046 2047 DPRINTF("err=%s\n", usb2_errstr(error)); 2048 2049 /* 2050 * If we are not transferring then just return. 2051 * This can happen during transfer cancel. 2052 */ 2053 if (!xfer->flags_int.transferring) { 2054 DPRINTF("not transferring\n"); 2055 return; 2056 } 2057 /* only set transfer error if not already set */ 2058 if (!xfer->error) { 2059 xfer->error = error; 2060 } 2061 /* stop any callouts */ 2062 usb2_callout_stop(&xfer->timeout_handle); 2063 2064 /* 2065 * If we are waiting on a queue, just remove the USB transfer 2066 * from the queue, if any. We should have the required locks 2067 * locked to do the remove when this function is called. 2068 */ 2069 usb2_transfer_dequeue(xfer); 2070 2071 if (mtx_owned(xfer->xroot->xfer_mtx)) { 2072 /* 2073 * If the private USB lock is not locked, then we assume 2074 * that the BUS-DMA load stage has been passed: 2075 */ 2076 pq = &xfer->xroot->dma_q; 2077 2078 if (pq->curr == xfer) { 2079 /* start the next BUS-DMA load, if any */ 2080 usb2_command_wrapper(pq, NULL); 2081 } 2082 } 2083 /* keep some statistics */ 2084 if (xfer->error) { 2085 xfer->xroot->bus->stats_err.uds_requests 2086 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++; 2087 } else { 2088 xfer->xroot->bus->stats_ok.uds_requests 2089 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++; 2090 } 2091 2092 /* call the USB transfer callback */ 2093 usb2_callback_ss_done_defer(xfer); 2094} 2095 2096/*------------------------------------------------------------------------* 2097 * usb2_transfer_start_cb 2098 * 2099 * This function is called to start the USB transfer when 2100 * "xfer->interval" is greater than zero, and and the endpoint type is 2101 * BULK or CONTROL. 2102 *------------------------------------------------------------------------*/ 2103static void 2104usb2_transfer_start_cb(void *arg) 2105{ 2106 struct usb2_xfer *xfer = arg; 2107 struct usb2_pipe *pipe = xfer->pipe; 2108 2109 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2110 2111 DPRINTF("start\n"); 2112 2113 /* start the transfer */ 2114 (pipe->methods->start) (xfer); 2115 2116 /* check cancelability */ 2117 if (pipe->methods->start_is_cancelable) { 2118 xfer->flags_int.can_cancel_immed = 1; 2119 if (xfer->error) { 2120 /* some error has happened */ 2121 usb2_transfer_done(xfer, 0); 2122 } 2123 } else { 2124 xfer->flags_int.can_cancel_immed = 0; 2125 } 2126} 2127 2128/*------------------------------------------------------------------------* 2129 * usb2_transfer_set_stall 2130 * 2131 * This function is used to set the stall flag outside the 2132 * callback. This function is NULL safe. 2133 *------------------------------------------------------------------------*/ 2134void 2135usb2_transfer_set_stall(struct usb2_xfer *xfer) 2136{ 2137 if (xfer == NULL) { 2138 /* tearing down */ 2139 return; 2140 } 2141 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2142 2143 /* avoid any races by locking the USB mutex */ 2144 USB_BUS_LOCK(xfer->xroot->bus); 2145 2146 xfer->flags.stall_pipe = 1; 2147 2148 USB_BUS_UNLOCK(xfer->xroot->bus); 2149} 2150 2151/*------------------------------------------------------------------------* 2152 * usb2_transfer_clear_stall 2153 * 2154 * This function is used to clear the stall flag outside the 2155 * callback. This function is NULL safe. 2156 *------------------------------------------------------------------------*/ 2157void 2158usb2_transfer_clear_stall(struct usb2_xfer *xfer) 2159{ 2160 if (xfer == NULL) { 2161 /* tearing down */ 2162 return; 2163 } 2164 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2165 2166 /* avoid any races by locking the USB mutex */ 2167 USB_BUS_LOCK(xfer->xroot->bus); 2168 2169 xfer->flags.stall_pipe = 0; 2170 2171 USB_BUS_UNLOCK(xfer->xroot->bus); 2172} 2173 2174/*------------------------------------------------------------------------* 2175 * usb2_pipe_start 2176 * 2177 * This function is used to add an USB transfer to the pipe transfer list. 2178 *------------------------------------------------------------------------*/ 2179void 2180usb2_pipe_start(struct usb2_xfer_queue *pq) 2181{ 2182 struct usb2_pipe *pipe; 2183 struct usb2_xfer *xfer; 2184 uint8_t type; 2185 2186 xfer = pq->curr; 2187 pipe = xfer->pipe; 2188 2189 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2190 2191 /* 2192 * If the pipe is already stalled we do nothing ! 2193 */ 2194 if (pipe->is_stalled) { 2195 return; 2196 } 2197 /* 2198 * Check if we are supposed to stall the pipe: 2199 */ 2200 if (xfer->flags.stall_pipe) { 2201 /* clear stall command */ 2202 xfer->flags.stall_pipe = 0; 2203 2204 /* 2205 * Only stall BULK and INTERRUPT endpoints. 2206 */ 2207 type = (pipe->edesc->bmAttributes & UE_XFERTYPE); 2208 if ((type == UE_BULK) || 2209 (type == UE_INTERRUPT)) { 2210 struct usb2_device *udev; 2211 struct usb2_xfer_root *info; 2212 2213 info = xfer->xroot; 2214 udev = info->udev; 2215 pipe->is_stalled = 1; 2216 2217 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 2218 (udev->bus->methods->set_stall) ( 2219 udev, NULL, pipe); 2220 } else if (udev->default_xfer[1]) { 2221 info = udev->default_xfer[1]->xroot; 2222 if (usb2_proc_msignal( 2223 &info->bus->non_giant_callback_proc, 2224 &udev->cs_msg[0], &udev->cs_msg[1])) { 2225 /* ignore */ 2226 } 2227 } else { 2228 /* should not happen */ 2229 DPRINTFN(0, "No stall handler!\n"); 2230 } 2231 /* 2232 * We get started again when the stall is cleared! 2233 */ 2234 return; 2235 } 2236 } 2237 /* Set or clear stall complete - special case */ 2238 if (xfer->nframes == 0) { 2239 /* we are complete */ 2240 xfer->aframes = 0; 2241 usb2_transfer_done(xfer, 0); 2242 return; 2243 } 2244 /* 2245 * Handled cases: 2246 * 2247 * 1) Start the first transfer queued. 2248 * 2249 * 2) Re-start the current USB transfer. 2250 */ 2251 /* 2252 * Check if there should be any 2253 * pre transfer start delay: 2254 */ 2255 if (xfer->interval > 0) { 2256 type = (pipe->edesc->bmAttributes & UE_XFERTYPE); 2257 if ((type == UE_BULK) || 2258 (type == UE_CONTROL)) { 2259 usb2_transfer_timeout_ms(xfer, 2260 &usb2_transfer_start_cb, 2261 xfer->interval); 2262 return; 2263 } 2264 } 2265 DPRINTF("start\n"); 2266 2267 /* start USB transfer */ 2268 (pipe->methods->start) (xfer); 2269 2270 /* check cancelability */ 2271 if (pipe->methods->start_is_cancelable) { 2272 xfer->flags_int.can_cancel_immed = 1; 2273 if (xfer->error) { 2274 /* some error has happened */ 2275 usb2_transfer_done(xfer, 0); 2276 } 2277 } else { 2278 xfer->flags_int.can_cancel_immed = 0; 2279 } 2280} 2281 2282/*------------------------------------------------------------------------* 2283 * usb2_transfer_timeout_ms 2284 * 2285 * This function is used to setup a timeout on the given USB 2286 * transfer. If the timeout has been deferred the callback given by 2287 * "cb" will get called after "ms" milliseconds. 2288 *------------------------------------------------------------------------*/ 2289void 2290usb2_transfer_timeout_ms(struct usb2_xfer *xfer, 2291 void (*cb) (void *arg), uint32_t ms) 2292{ 2293 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2294 2295 /* defer delay */ 2296 usb2_callout_reset(&xfer->timeout_handle, 2297 USB_MS_TO_TICKS(ms), cb, xfer); 2298} 2299 2300/*------------------------------------------------------------------------* 2301 * usb2_callback_wrapper_sub 2302 * 2303 * - This function will update variables in an USB transfer after 2304 * that the USB transfer is complete. 2305 * 2306 * - This function is used to start the next USB transfer on the 2307 * pipe transfer queue, if any. 2308 * 2309 * NOTE: In some special cases the USB transfer will not be removed from 2310 * the pipe queue, but remain first. To enforce USB transfer removal call 2311 * this function passing the error code "USB_ERR_CANCELLED". 2312 * 2313 * Return values: 2314 * 0: Success. 2315 * Else: The callback has been deferred. 2316 *------------------------------------------------------------------------*/ 2317static uint8_t 2318usb2_callback_wrapper_sub(struct usb2_xfer *xfer) 2319{ 2320 struct usb2_pipe *pipe; 2321 uint32_t x; 2322 2323 if ((!xfer->flags_int.open) && 2324 (!xfer->flags_int.did_close)) { 2325 DPRINTF("close\n"); 2326 USB_BUS_LOCK(xfer->xroot->bus); 2327 (xfer->pipe->methods->close) (xfer); 2328 USB_BUS_UNLOCK(xfer->xroot->bus); 2329 /* only close once */ 2330 xfer->flags_int.did_close = 1; 2331 return (1); /* wait for new callback */ 2332 } 2333 /* 2334 * If we have a non-hardware induced error we 2335 * need to do the DMA delay! 2336 */ 2337 if (((xfer->error == USB_ERR_CANCELLED) || 2338 (xfer->error == USB_ERR_TIMEOUT)) && 2339 (!xfer->flags_int.did_dma_delay)) { 2340 2341 uint32_t temp; 2342 2343 /* only delay once */ 2344 xfer->flags_int.did_dma_delay = 1; 2345 2346 /* we can not cancel this delay */ 2347 xfer->flags_int.can_cancel_immed = 0; 2348 2349 temp = usb2_get_dma_delay(xfer->xroot->bus); 2350 2351 DPRINTFN(3, "DMA delay, %u ms, " 2352 "on %p\n", temp, xfer); 2353 2354 if (temp != 0) { 2355 USB_BUS_LOCK(xfer->xroot->bus); 2356 usb2_transfer_timeout_ms(xfer, 2357 &usb2_dma_delay_done_cb, temp); 2358 USB_BUS_UNLOCK(xfer->xroot->bus); 2359 return (1); /* wait for new callback */ 2360 } 2361 } 2362 /* check actual number of frames */ 2363 if (xfer->aframes > xfer->nframes) { 2364 if (xfer->error == 0) { 2365 panic("%s: actual number of frames, %d, is " 2366 "greater than initial number of frames, %d!\n", 2367 __FUNCTION__, xfer->aframes, xfer->nframes); 2368 } else { 2369 /* just set some valid value */ 2370 xfer->aframes = xfer->nframes; 2371 } 2372 } 2373 /* compute actual length */ 2374 xfer->actlen = 0; 2375 2376 for (x = 0; x != xfer->aframes; x++) { 2377 xfer->actlen += xfer->frlengths[x]; 2378 } 2379 2380 /* 2381 * Frames that were not transferred get zero actual length in 2382 * case the USB device driver does not check the actual number 2383 * of frames transferred, "xfer->aframes": 2384 */ 2385 for (; x < xfer->nframes; x++) { 2386 xfer->frlengths[x] = 0; 2387 } 2388 2389 /* check actual length */ 2390 if (xfer->actlen > xfer->sumlen) { 2391 if (xfer->error == 0) { 2392 panic("%s: actual length, %d, is greater than " 2393 "initial length, %d!\n", 2394 __FUNCTION__, xfer->actlen, xfer->sumlen); 2395 } else { 2396 /* just set some valid value */ 2397 xfer->actlen = xfer->sumlen; 2398 } 2399 } 2400 DPRINTFN(6, "xfer=%p pipe=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n", 2401 xfer, xfer->pipe, xfer->error, xfer->actlen, xfer->sumlen, 2402 xfer->aframes, xfer->nframes); 2403 2404 if (xfer->error) { 2405 /* end of control transfer, if any */ 2406 xfer->flags_int.control_act = 0; 2407 2408 /* check if we should block the execution queue */ 2409 if ((xfer->error != USB_ERR_CANCELLED) && 2410 (xfer->flags.pipe_bof)) { 2411 DPRINTFN(2, "xfer=%p: Block On Failure " 2412 "on pipe=%p\n", xfer, xfer->pipe); 2413 goto done; 2414 } 2415 } else { 2416 /* check for short transfers */ 2417 if (xfer->actlen < xfer->sumlen) { 2418 2419 /* end of control transfer, if any */ 2420 xfer->flags_int.control_act = 0; 2421 2422 if (!xfer->flags_int.short_xfer_ok) { 2423 xfer->error = USB_ERR_SHORT_XFER; 2424 if (xfer->flags.pipe_bof) { 2425 DPRINTFN(2, "xfer=%p: Block On Failure on " 2426 "Short Transfer on pipe %p.\n", 2427 xfer, xfer->pipe); 2428 goto done; 2429 } 2430 } 2431 } else { 2432 /* 2433 * Check if we are in the middle of a 2434 * control transfer: 2435 */ 2436 if (xfer->flags_int.control_act) { 2437 DPRINTFN(5, "xfer=%p: Control transfer " 2438 "active on pipe=%p\n", xfer, xfer->pipe); 2439 goto done; 2440 } 2441 } 2442 } 2443 2444 pipe = xfer->pipe; 2445 2446 /* 2447 * If the current USB transfer is completing we need to start the 2448 * next one: 2449 */ 2450 USB_BUS_LOCK(xfer->xroot->bus); 2451 if (pipe->pipe_q.curr == xfer) { 2452 usb2_command_wrapper(&pipe->pipe_q, NULL); 2453 2454 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) { 2455 /* there is another USB transfer waiting */ 2456 } else { 2457 /* this is the last USB transfer */ 2458 /* clear isochronous sync flag */ 2459 xfer->pipe->is_synced = 0; 2460 } 2461 } 2462 USB_BUS_UNLOCK(xfer->xroot->bus); 2463done: 2464 return (0); 2465} 2466 2467/*------------------------------------------------------------------------* 2468 * usb2_command_wrapper 2469 * 2470 * This function is used to execute commands non-recursivly on an USB 2471 * transfer. 2472 *------------------------------------------------------------------------*/ 2473void 2474usb2_command_wrapper(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer) 2475{ 2476 if (xfer) { 2477 /* 2478 * If the transfer is not already processing, 2479 * queue it! 2480 */ 2481 if (pq->curr != xfer) { 2482 usb2_transfer_enqueue(pq, xfer); 2483 if (pq->curr != NULL) { 2484 /* something is already processing */ 2485 DPRINTFN(6, "busy %p\n", pq->curr); 2486 return; 2487 } 2488 } 2489 } else { 2490 /* Get next element in queue */ 2491 pq->curr = NULL; 2492 } 2493 2494 if (!pq->recurse_1) { 2495 2496 do { 2497 2498 /* set both recurse flags */ 2499 pq->recurse_1 = 1; 2500 pq->recurse_2 = 1; 2501 2502 if (pq->curr == NULL) { 2503 xfer = TAILQ_FIRST(&pq->head); 2504 if (xfer) { 2505 TAILQ_REMOVE(&pq->head, xfer, 2506 wait_entry); 2507 xfer->wait_queue = NULL; 2508 pq->curr = xfer; 2509 } else { 2510 break; 2511 } 2512 } 2513 DPRINTFN(6, "cb %p (enter)\n", pq->curr); 2514 (pq->command) (pq); 2515 DPRINTFN(6, "cb %p (leave)\n", pq->curr); 2516 2517 } while (!pq->recurse_2); 2518 2519 /* clear first recurse flag */ 2520 pq->recurse_1 = 0; 2521 2522 } else { 2523 /* clear second recurse flag */ 2524 pq->recurse_2 = 0; 2525 } 2526} 2527 2528/*------------------------------------------------------------------------* 2529 * usb2_default_transfer_setup 2530 * 2531 * This function is used to setup the default USB control endpoint 2532 * transfer. 2533 *------------------------------------------------------------------------*/ 2534void 2535usb2_default_transfer_setup(struct usb2_device *udev) 2536{ 2537 struct usb2_xfer *xfer; 2538 uint8_t no_resetup; 2539 uint8_t iface_index; 2540 2541repeat: 2542 2543 xfer = udev->default_xfer[0]; 2544 if (xfer) { 2545 USB_XFER_LOCK(xfer); 2546 no_resetup = 2547 ((xfer->address == udev->address) && 2548 (udev->default_ep_desc.wMaxPacketSize[0] == 2549 udev->ddesc.bMaxPacketSize)); 2550 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 2551 if (no_resetup) { 2552 /* 2553 * NOTE: checking "xfer->address" and 2554 * starting the USB transfer must be 2555 * atomic! 2556 */ 2557 usb2_transfer_start(xfer); 2558 } 2559 } 2560 USB_XFER_UNLOCK(xfer); 2561 } else { 2562 no_resetup = 0; 2563 } 2564 2565 if (no_resetup) { 2566 /* 2567 * All parameters are exactly the same like before. 2568 * Just return. 2569 */ 2570 return; 2571 } 2572 /* 2573 * Update wMaxPacketSize for the default control endpoint: 2574 */ 2575 udev->default_ep_desc.wMaxPacketSize[0] = 2576 udev->ddesc.bMaxPacketSize; 2577 2578 /* 2579 * Unsetup any existing USB transfer: 2580 */ 2581 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 2582 2583 /* 2584 * Try to setup a new USB transfer for the 2585 * default control endpoint: 2586 */ 2587 iface_index = 0; 2588 if (usb2_transfer_setup(udev, &iface_index, 2589 udev->default_xfer, usb2_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL, 2590 udev->default_mtx)) { 2591 DPRINTFN(0, "could not setup default " 2592 "USB transfer!\n"); 2593 } else { 2594 goto repeat; 2595 } 2596} 2597 2598/*------------------------------------------------------------------------* 2599 * usb2_clear_data_toggle - factored out code 2600 * 2601 * NOTE: the intention of this function is not to reset the hardware 2602 * data toggle. 2603 *------------------------------------------------------------------------*/ 2604void 2605usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe) 2606{ 2607 DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe); 2608 2609 USB_BUS_LOCK(udev->bus); 2610 pipe->toggle_next = 0; 2611 USB_BUS_UNLOCK(udev->bus); 2612} 2613 2614/*------------------------------------------------------------------------* 2615 * usb2_clear_stall_callback - factored out clear stall callback 2616 * 2617 * Input parameters: 2618 * xfer1: Clear Stall Control Transfer 2619 * xfer2: Stalled USB Transfer 2620 * 2621 * This function is NULL safe. 2622 * 2623 * Return values: 2624 * 0: In progress 2625 * Else: Finished 2626 * 2627 * Clear stall config example: 2628 * 2629 * static const struct usb2_config my_clearstall = { 2630 * .type = UE_CONTROL, 2631 * .endpoint = 0, 2632 * .direction = UE_DIR_ANY, 2633 * .interval = 50, //50 milliseconds 2634 * .bufsize = sizeof(struct usb2_device_request), 2635 * .mh.timeout = 1000, //1.000 seconds 2636 * .mh.flags = { }, 2637 * .mh.callback = &my_clear_stall_callback, // ** 2638 * }; 2639 * 2640 * ** "my_clear_stall_callback" calls "usb2_clear_stall_callback" 2641 * passing the correct parameters. 2642 *------------------------------------------------------------------------*/ 2643uint8_t 2644usb2_clear_stall_callback(struct usb2_xfer *xfer1, 2645 struct usb2_xfer *xfer2) 2646{ 2647 struct usb2_device_request req; 2648 2649 if (xfer2 == NULL) { 2650 /* looks like we are tearing down */ 2651 DPRINTF("NULL input parameter\n"); 2652 return (0); 2653 } 2654 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED); 2655 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED); 2656 2657 switch (USB_GET_STATE(xfer1)) { 2658 case USB_ST_SETUP: 2659 2660 /* 2661 * pre-clear the data toggle to DATA0 ("umass.c" and 2662 * "ata-usb.c" depends on this) 2663 */ 2664 2665 usb2_clear_data_toggle(xfer2->xroot->udev, xfer2->pipe); 2666 2667 /* setup a clear-stall packet */ 2668 2669 req.bmRequestType = UT_WRITE_ENDPOINT; 2670 req.bRequest = UR_CLEAR_FEATURE; 2671 USETW(req.wValue, UF_ENDPOINT_HALT); 2672 req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress; 2673 req.wIndex[1] = 0; 2674 USETW(req.wLength, 0); 2675 2676 /* 2677 * "usb2_transfer_setup_sub()" will ensure that 2678 * we have sufficient room in the buffer for 2679 * the request structure! 2680 */ 2681 2682 /* copy in the transfer */ 2683 2684 usb2_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); 2685 2686 /* set length */ 2687 xfer1->frlengths[0] = sizeof(req); 2688 xfer1->nframes = 1; 2689 2690 usb2_start_hardware(xfer1); 2691 return (0); 2692 2693 case USB_ST_TRANSFERRED: 2694 break; 2695 2696 default: /* Error */ 2697 if (xfer1->error == USB_ERR_CANCELLED) { 2698 return (0); 2699 } 2700 break; 2701 } 2702 return (1); /* Clear Stall Finished */ 2703} 2704 2705#if (USB_NO_POLL == 0) 2706 2707/*------------------------------------------------------------------------* 2708 * usb2_callout_poll 2709 *------------------------------------------------------------------------*/ 2710static void 2711usb2_callout_poll(struct usb2_xfer *xfer) 2712{ 2713 struct usb2_callout *co; 2714 void (*cb) (void *); 2715 void *arg; 2716 struct mtx *mtx; 2717 uint32_t delta; 2718 2719 if (xfer == NULL) { 2720 return; 2721 } 2722 co = &xfer->timeout_handle; 2723 2724#if __FreeBSD_version >= 800000 2725 mtx = (void *)(co->co.c_lock); 2726#else 2727 mtx = co->co.c_mtx; 2728#endif 2729 mtx_lock(mtx); 2730 2731 if (usb2_callout_pending(co)) { 2732 delta = ticks - co->co.c_time; 2733 if (!(delta & 0x80000000)) { 2734 2735 cb = co->co.c_func; 2736 arg = co->co.c_arg; 2737 2738 /* timed out */ 2739 usb2_callout_stop(co); 2740 2741 (cb) (arg); 2742 } 2743 } 2744 mtx_unlock(mtx); 2745} 2746 2747 2748/*------------------------------------------------------------------------* 2749 * usb2_do_poll 2750 * 2751 * This function is called from keyboard driver when in polling 2752 * mode. 2753 *------------------------------------------------------------------------*/ 2754void 2755usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max) 2756{ 2757 struct usb2_xfer *xfer; 2758 struct usb2_xfer_root *xroot; 2759 struct usb2_device *udev; 2760 struct usb2_proc_msg *pm; 2761 uint32_t to; 2762 uint16_t n; 2763 2764 /* compute system tick delay */ 2765 to = ((uint32_t)(1000000)) / ((uint32_t)(hz)); 2766 DELAY(to); 2767 atomic_add_int((volatile int *)&ticks, 1); 2768 2769 for (n = 0; n != max; n++) { 2770 xfer = ppxfer[n]; 2771 if (xfer) { 2772 xroot = xfer->xroot; 2773 udev = xroot->udev; 2774 2775 /* 2776 * Poll hardware - signal that we are polling by 2777 * locking the private mutex: 2778 */ 2779 USB_XFER_LOCK(xfer); 2780 (udev->bus->methods->do_poll) (udev->bus); 2781 USB_XFER_UNLOCK(xfer); 2782 2783 /* poll clear stall start */ 2784 USB_BUS_LOCK(xfer->xroot->bus); 2785 pm = &udev->cs_msg[0].hdr; 2786 (pm->pm_callback) (pm); 2787 USB_BUS_UNLOCK(xfer->xroot->bus); 2788 2789 if (udev->default_xfer[1]) { 2790 2791 /* poll timeout */ 2792 usb2_callout_poll(udev->default_xfer[1]); 2793 2794 /* poll clear stall done thread */ 2795 USB_BUS_LOCK(xfer->xroot->bus); 2796 pm = &udev->default_xfer[1]-> 2797 xroot->done_m[0].hdr; 2798 (pm->pm_callback) (pm); 2799 USB_BUS_UNLOCK(xfer->xroot->bus); 2800 } 2801 /* poll timeout */ 2802 usb2_callout_poll(xfer); 2803 2804 /* poll done thread */ 2805 USB_BUS_LOCK(xfer->xroot->bus); 2806 pm = &xroot->done_m[0].hdr; 2807 (pm->pm_callback) (pm); 2808 USB_BUS_UNLOCK(xfer->xroot->bus); 2809 } 2810 } 2811} 2812 2813#else 2814 2815void 2816usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max) 2817{ 2818 /* polling not supported */ 2819} 2820 2821#endif 2822