1 /* $FreeBSD$ */ 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/usb/usb_mfunc.h> 28 #include <dev/usb/usb_error.h> 29 #include <dev/usb/usb.h> 30 #include <dev/usb/usb_defs.h> 31 32 #define USB_DEBUG_VAR usb2_debug 33 34 #include <dev/usb/usb_core.h> 35 #include <dev/usb/usb_busdma.h> 36 #include <dev/usb/usb_process.h> 37 #include <dev/usb/usb_transfer.h> 38 #include <dev/usb/usb_device.h> 39 #include <dev/usb/usb_debug.h> 40 #include <dev/usb/usb_util.h> 41 42 #include <dev/usb/usb_controller.h> 43 #include <dev/usb/usb_bus.h> 44 45 struct 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 */ 58 static 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 94 static 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 126 static void usb2_update_max_frame_size(struct usb2_xfer *); 127 static void usb2_transfer_unsetup_sub(struct usb2_xfer_root *, uint8_t); 128 static void usb2_control_transfer_init(struct usb2_xfer *); 129 static uint8_t usb2_start_hardware_sub(struct usb2_xfer *); 130 static void usb2_callback_proc(struct usb2_proc_msg *); 131 static void usb2_callback_ss_done_defer(struct usb2_xfer *); 132 static void usb2_callback_wrapper(struct usb2_xfer_queue *); 133 static void usb2_dma_delay_done_cb(void *); 134 static void usb2_transfer_start_cb(void *); 135 static 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 *------------------------------------------------------------------------*/ 143 static void 144 usb2_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 *------------------------------------------------------------------------*/ 167 uint32_t 168 usb2_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 *------------------------------------------------------------------------*/ 195 uint8_t 196 usb2_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 *------------------------------------------------------------------------*/ 315 void 316 usb2_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 } 711 done: 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 *------------------------------------------------------------------------*/ 738 usb2_error_t 739 usb2_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 * Common initialization of the 892 * "usb2_xfer" structure. 893 */ 894 xfer = USB_ADD_BYTES(buf, parm.size[0]); 895 xfer->address = udev->address; 896 xfer->priv_sc = priv_sc; 897 xfer->xroot = info; 898 899 usb2_callout_init_mtx(&xfer->timeout_handle, 900 &udev->bus->bus_mtx, 0); 901 } else { 902 /* 903 * Setup a dummy xfer, hence we are 904 * writing to the "usb2_xfer" 905 * structure pointed to by "xfer" 906 * before we have allocated any 907 * memory: 908 */ 909 xfer = &dummy; 910 bzero(&dummy, sizeof(dummy)); 911 refcount++; 912 } 913 914 /* set transfer pipe pointer */ 915 xfer->pipe = pipe; 916 917 parm.size[0] += sizeof(xfer[0]); 918 parm.methods = xfer->pipe->methods; 919 parm.curr_xfer = xfer; 920 921 /* 922 * Call the Host or Device controller transfer 923 * setup routine: 924 */ 925 (udev->bus->methods->xfer_setup) (&parm); 926 927 /* check for error */ 928 if (parm.err) 929 goto done; 930 931 if (buf) { 932 /* 933 * Increment the pipe refcount. This 934 * basically prevents setting a new 935 * configuration and alternate setting 936 * when USB transfers are in use on 937 * the given interface. Search the USB 938 * code for "pipe->refcount" if you 939 * want more information. 940 */ 941 xfer->pipe->refcount++; 942 943 /* 944 * Whenever we set ppxfer[] then we 945 * also need to increment the 946 * "setup_refcount": 947 */ 948 info->setup_refcount++; 949 950 /* 951 * Transfer is successfully setup and 952 * can be used: 953 */ 954 ppxfer[n] = xfer; 955 } 956 } 957 958 if (buf || parm.err) { 959 goto done; 960 } 961 if (refcount == 0) { 962 /* no transfers - nothing to do ! */ 963 goto done; 964 } 965 /* align data properly */ 966 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 967 968 /* store offset temporarily */ 969 parm.size[1] = parm.size[0]; 970 971 /* 972 * The number of DMA tags required depends on 973 * the number of endpoints. The current estimate 974 * for maximum number of DMA tags per endpoint 975 * is two. 976 */ 977 parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX); 978 979 /* 980 * DMA tags for QH, TD, Data and more. 981 */ 982 parm.dma_tag_max += 8; 983 984 parm.dma_tag_p += parm.dma_tag_max; 985 986 parm.size[0] += ((uint8_t *)parm.dma_tag_p) - 987 ((uint8_t *)0); 988 989 /* align data properly */ 990 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 991 992 /* store offset temporarily */ 993 parm.size[3] = parm.size[0]; 994 995 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) - 996 ((uint8_t *)0); 997 998 /* align data properly */ 999 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1000 1001 /* store offset temporarily */ 1002 parm.size[4] = parm.size[0]; 1003 1004 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) - 1005 ((uint8_t *)0); 1006 1007 /* store end offset temporarily */ 1008 parm.size[5] = parm.size[0]; 1009 1010 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) - 1011 ((uint8_t *)0); 1012 1013 /* store end offset temporarily */ 1014 1015 parm.size[2] = parm.size[0]; 1016 1017 /* align data properly */ 1018 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1019 1020 parm.size[6] = parm.size[0]; 1021 1022 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) - 1023 ((uint8_t *)0); 1024 1025 /* align data properly */ 1026 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1027 1028 /* allocate zeroed memory */ 1029 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO); 1030 1031 if (buf == NULL) { 1032 parm.err = USB_ERR_NOMEM; 1033 DPRINTFN(0, "cannot allocate memory block for " 1034 "configuration (%d bytes)\n", 1035 parm.size[0]); 1036 goto done; 1037 } 1038 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]); 1039 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]); 1040 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]); 1041 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]); 1042 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]); 1043 } 1044 1045 done: 1046 if (buf) { 1047 if (info->setup_refcount == 0) { 1048 /* 1049 * "usb2_transfer_unsetup_sub" will unlock 1050 * the bus mutex before returning ! 1051 */ 1052 USB_BUS_LOCK(info->bus); 1053 1054 /* something went wrong */ 1055 usb2_transfer_unsetup_sub(info, 0); 1056 } 1057 } 1058 if (parm.err) { 1059 usb2_transfer_unsetup(ppxfer, n_setup); 1060 } 1061 return (parm.err); 1062 } 1063 1064 /*------------------------------------------------------------------------* 1065 * usb2_transfer_unsetup_sub - factored out code 1066 *------------------------------------------------------------------------*/ 1067 static void 1068 usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay) 1069 { 1070 struct usb2_page_cache *pc; 1071 uint32_t temp; 1072 1073 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 1074 1075 /* wait for any outstanding DMA operations */ 1076 1077 if (needs_delay) { 1078 temp = usb2_get_dma_delay(info->bus); 1079 usb2_pause_mtx(&info->bus->bus_mtx, 1080 USB_MS_TO_TICKS(temp)); 1081 } 1082 1083 /* make sure that our done messages are not queued anywhere */ 1084 usb2_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); 1085 1086 USB_BUS_UNLOCK(info->bus); 1087 1088 /* free DMA'able memory, if any */ 1089 pc = info->dma_page_cache_start; 1090 while (pc != info->dma_page_cache_end) { 1091 usb2_pc_free_mem(pc); 1092 pc++; 1093 } 1094 1095 /* free DMA maps in all "xfer->frbuffers" */ 1096 pc = info->xfer_page_cache_start; 1097 while (pc != info->xfer_page_cache_end) { 1098 usb2_pc_dmamap_destroy(pc); 1099 pc++; 1100 } 1101 1102 /* free all DMA tags */ 1103 usb2_dma_tag_unsetup(&info->dma_parent_tag); 1104 1105 usb2_cv_destroy(&info->cv_drain); 1106 1107 /* 1108 * free the "memory_base" last, hence the "info" structure is 1109 * contained within the "memory_base"! 1110 */ 1111 free(info->memory_base, M_USB); 1112 } 1113 1114 /*------------------------------------------------------------------------* 1115 * usb2_transfer_unsetup - unsetup/free an array of USB transfers 1116 * 1117 * NOTE: All USB transfers in progress will get called back passing 1118 * the error code "USB_ERR_CANCELLED" before this function 1119 * returns. 1120 *------------------------------------------------------------------------*/ 1121 void 1122 usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup) 1123 { 1124 struct usb2_xfer *xfer; 1125 struct usb2_xfer_root *info; 1126 uint8_t needs_delay = 0; 1127 1128 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1129 "usb2_transfer_unsetup can sleep!"); 1130 1131 while (n_setup--) { 1132 xfer = pxfer[n_setup]; 1133 1134 if (xfer == NULL) 1135 continue; 1136 1137 info = xfer->xroot; 1138 1139 USB_XFER_LOCK(xfer); 1140 USB_BUS_LOCK(info->bus); 1141 1142 /* 1143 * HINT: when you start/stop a transfer, it might be a 1144 * good idea to directly use the "pxfer[]" structure: 1145 * 1146 * usb2_transfer_start(sc->pxfer[0]); 1147 * usb2_transfer_stop(sc->pxfer[0]); 1148 * 1149 * That way, if your code has many parts that will not 1150 * stop running under the same lock, in other words 1151 * "xfer_mtx", the usb2_transfer_start and 1152 * usb2_transfer_stop functions will simply return 1153 * when they detect a NULL pointer argument. 1154 * 1155 * To avoid any races we clear the "pxfer[]" pointer 1156 * while holding the private mutex of the driver: 1157 */ 1158 pxfer[n_setup] = NULL; 1159 1160 USB_BUS_UNLOCK(info->bus); 1161 USB_XFER_UNLOCK(xfer); 1162 1163 usb2_transfer_drain(xfer); 1164 1165 if (xfer->flags_int.bdma_enable) 1166 needs_delay = 1; 1167 1168 /* 1169 * NOTE: default pipe does not have an 1170 * interface, even if pipe->iface_index == 0 1171 */ 1172 xfer->pipe->refcount--; 1173 1174 usb2_callout_drain(&xfer->timeout_handle); 1175 1176 USB_BUS_LOCK(info->bus); 1177 1178 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup " 1179 "reference count!\n")); 1180 1181 info->setup_refcount--; 1182 1183 if (info->setup_refcount == 0) { 1184 usb2_transfer_unsetup_sub(info, 1185 needs_delay); 1186 } else { 1187 USB_BUS_UNLOCK(info->bus); 1188 } 1189 } 1190 } 1191 1192 /*------------------------------------------------------------------------* 1193 * usb2_control_transfer_init - factored out code 1194 * 1195 * In USB Device Mode we have to wait for the SETUP packet which 1196 * containst the "struct usb2_device_request" structure, before we can 1197 * transfer any data. In USB Host Mode we already have the SETUP 1198 * packet at the moment the USB transfer is started. This leads us to 1199 * having to setup the USB transfer at two different places in 1200 * time. This function just contains factored out control transfer 1201 * initialisation code, so that we don't duplicate the code. 1202 *------------------------------------------------------------------------*/ 1203 static void 1204 usb2_control_transfer_init(struct usb2_xfer *xfer) 1205 { 1206 struct usb2_device_request req; 1207 1208 /* copy out the USB request header */ 1209 1210 usb2_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); 1211 1212 /* setup remainder */ 1213 1214 xfer->flags_int.control_rem = UGETW(req.wLength); 1215 1216 /* copy direction to endpoint variable */ 1217 1218 xfer->endpoint &= ~(UE_DIR_IN | UE_DIR_OUT); 1219 xfer->endpoint |= 1220 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT; 1221 } 1222 1223 /*------------------------------------------------------------------------* 1224 * usb2_start_hardware_sub 1225 * 1226 * This function handles initialisation of control transfers. Control 1227 * transfers are special in that regard that they can both transmit 1228 * and receive data. 1229 * 1230 * Return values: 1231 * 0: Success 1232 * Else: Failure 1233 *------------------------------------------------------------------------*/ 1234 static uint8_t 1235 usb2_start_hardware_sub(struct usb2_xfer *xfer) 1236 { 1237 uint32_t len; 1238 1239 /* Check for control endpoint stall */ 1240 if (xfer->flags.stall_pipe) { 1241 /* no longer active */ 1242 xfer->flags_int.control_act = 0; 1243 } 1244 /* 1245 * Check if there is a control 1246 * transfer in progress: 1247 */ 1248 if (xfer->flags_int.control_act) { 1249 1250 if (xfer->flags_int.control_hdr) { 1251 1252 /* clear send header flag */ 1253 1254 xfer->flags_int.control_hdr = 0; 1255 1256 /* setup control transfer */ 1257 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1258 usb2_control_transfer_init(xfer); 1259 } 1260 } 1261 /* get data length */ 1262 1263 len = xfer->sumlen; 1264 1265 } else { 1266 1267 /* the size of the SETUP structure is hardcoded ! */ 1268 1269 if (xfer->frlengths[0] != sizeof(struct usb2_device_request)) { 1270 DPRINTFN(0, "Wrong framelength %u != %zu\n", 1271 xfer->frlengths[0], sizeof(struct 1272 usb2_device_request)); 1273 goto error; 1274 } 1275 /* check USB mode */ 1276 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1277 1278 /* check number of frames */ 1279 if (xfer->nframes != 1) { 1280 /* 1281 * We need to receive the setup 1282 * message first so that we know the 1283 * data direction! 1284 */ 1285 DPRINTF("Misconfigured transfer\n"); 1286 goto error; 1287 } 1288 /* 1289 * Set a dummy "control_rem" value. This 1290 * variable will be overwritten later by a 1291 * call to "usb2_control_transfer_init()" ! 1292 */ 1293 xfer->flags_int.control_rem = 0xFFFF; 1294 } else { 1295 1296 /* setup "endpoint" and "control_rem" */ 1297 1298 usb2_control_transfer_init(xfer); 1299 } 1300 1301 /* set transfer-header flag */ 1302 1303 xfer->flags_int.control_hdr = 1; 1304 1305 /* get data length */ 1306 1307 len = (xfer->sumlen - sizeof(struct usb2_device_request)); 1308 } 1309 1310 /* check if there is a length mismatch */ 1311 1312 if (len > xfer->flags_int.control_rem) { 1313 DPRINTFN(0, "Length greater than remaining length!\n"); 1314 goto error; 1315 } 1316 /* check if we are doing a short transfer */ 1317 1318 if (xfer->flags.force_short_xfer) { 1319 xfer->flags_int.control_rem = 0; 1320 } else { 1321 if ((len != xfer->max_data_length) && 1322 (len != xfer->flags_int.control_rem) && 1323 (xfer->nframes != 1)) { 1324 DPRINTFN(0, "Short control transfer without " 1325 "force_short_xfer set!\n"); 1326 goto error; 1327 } 1328 xfer->flags_int.control_rem -= len; 1329 } 1330 1331 /* the status part is executed when "control_act" is 0 */ 1332 1333 if ((xfer->flags_int.control_rem > 0) || 1334 (xfer->flags.manual_status)) { 1335 /* don't execute the STATUS stage yet */ 1336 xfer->flags_int.control_act = 1; 1337 1338 /* sanity check */ 1339 if ((!xfer->flags_int.control_hdr) && 1340 (xfer->nframes == 1)) { 1341 /* 1342 * This is not a valid operation! 1343 */ 1344 DPRINTFN(0, "Invalid parameter " 1345 "combination\n"); 1346 goto error; 1347 } 1348 } else { 1349 /* time to execute the STATUS stage */ 1350 xfer->flags_int.control_act = 0; 1351 } 1352 return (0); /* success */ 1353 1354 error: 1355 return (1); /* failure */ 1356 } 1357 1358 /*------------------------------------------------------------------------* 1359 * usb2_start_hardware - start USB hardware for the given transfer 1360 * 1361 * This function should only be called from the USB callback. 1362 *------------------------------------------------------------------------*/ 1363 void 1364 usb2_start_hardware(struct usb2_xfer *xfer) 1365 { 1366 uint32_t x; 1367 1368 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n", 1369 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? 1370 "read" : "write"); 1371 1372 #if USB_DEBUG 1373 if (USB_DEBUG_VAR > 0) { 1374 USB_BUS_LOCK(xfer->xroot->bus); 1375 1376 usb2_dump_pipe(xfer->pipe); 1377 1378 USB_BUS_UNLOCK(xfer->xroot->bus); 1379 } 1380 #endif 1381 1382 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1383 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED); 1384 1385 /* Only open the USB transfer once! */ 1386 if (!xfer->flags_int.open) { 1387 xfer->flags_int.open = 1; 1388 1389 DPRINTF("open\n"); 1390 1391 USB_BUS_LOCK(xfer->xroot->bus); 1392 (xfer->pipe->methods->open) (xfer); 1393 USB_BUS_UNLOCK(xfer->xroot->bus); 1394 } 1395 /* set "transferring" flag */ 1396 xfer->flags_int.transferring = 1; 1397 1398 /* increment power reference */ 1399 usb2_transfer_power_ref(xfer, 1); 1400 1401 /* 1402 * Check if the transfer is waiting on a queue, most 1403 * frequently the "done_q": 1404 */ 1405 if (xfer->wait_queue) { 1406 USB_BUS_LOCK(xfer->xroot->bus); 1407 usb2_transfer_dequeue(xfer); 1408 USB_BUS_UNLOCK(xfer->xroot->bus); 1409 } 1410 /* clear "did_dma_delay" flag */ 1411 xfer->flags_int.did_dma_delay = 0; 1412 1413 /* clear "did_close" flag */ 1414 xfer->flags_int.did_close = 0; 1415 1416 /* clear "bdma_setup" flag */ 1417 xfer->flags_int.bdma_setup = 0; 1418 1419 /* by default we cannot cancel any USB transfer immediately */ 1420 xfer->flags_int.can_cancel_immed = 0; 1421 1422 /* clear lengths and frame counts by default */ 1423 xfer->sumlen = 0; 1424 xfer->actlen = 0; 1425 xfer->aframes = 0; 1426 1427 /* clear any previous errors */ 1428 xfer->error = 0; 1429 1430 /* sanity check */ 1431 1432 if (xfer->nframes == 0) { 1433 if (xfer->flags.stall_pipe) { 1434 /* 1435 * Special case - want to stall without transferring 1436 * any data: 1437 */ 1438 DPRINTF("xfer=%p nframes=0: stall " 1439 "or clear stall!\n", xfer); 1440 USB_BUS_LOCK(xfer->xroot->bus); 1441 xfer->flags_int.can_cancel_immed = 1; 1442 /* start the transfer */ 1443 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer); 1444 USB_BUS_UNLOCK(xfer->xroot->bus); 1445 return; 1446 } 1447 USB_BUS_LOCK(xfer->xroot->bus); 1448 usb2_transfer_done(xfer, USB_ERR_INVAL); 1449 USB_BUS_UNLOCK(xfer->xroot->bus); 1450 return; 1451 } 1452 /* compute total transfer length */ 1453 1454 for (x = 0; x != xfer->nframes; x++) { 1455 xfer->sumlen += xfer->frlengths[x]; 1456 if (xfer->sumlen < xfer->frlengths[x]) { 1457 /* length wrapped around */ 1458 USB_BUS_LOCK(xfer->xroot->bus); 1459 usb2_transfer_done(xfer, USB_ERR_INVAL); 1460 USB_BUS_UNLOCK(xfer->xroot->bus); 1461 return; 1462 } 1463 } 1464 1465 /* clear some internal flags */ 1466 1467 xfer->flags_int.short_xfer_ok = 0; 1468 xfer->flags_int.short_frames_ok = 0; 1469 1470 /* check if this is a control transfer */ 1471 1472 if (xfer->flags_int.control_xfr) { 1473 1474 if (usb2_start_hardware_sub(xfer)) { 1475 USB_BUS_LOCK(xfer->xroot->bus); 1476 usb2_transfer_done(xfer, USB_ERR_STALLED); 1477 USB_BUS_UNLOCK(xfer->xroot->bus); 1478 return; 1479 } 1480 } 1481 /* 1482 * Setup filtered version of some transfer flags, 1483 * in case of data read direction 1484 */ 1485 if (USB_GET_DATA_ISREAD(xfer)) { 1486 1487 if (xfer->flags_int.control_xfr) { 1488 1489 /* 1490 * Control transfers do not support reception 1491 * of multiple short USB frames ! 1492 */ 1493 1494 if (xfer->flags.short_xfer_ok) { 1495 xfer->flags_int.short_xfer_ok = 1; 1496 } 1497 } else { 1498 1499 if (xfer->flags.short_frames_ok) { 1500 xfer->flags_int.short_xfer_ok = 1; 1501 xfer->flags_int.short_frames_ok = 1; 1502 } else if (xfer->flags.short_xfer_ok) { 1503 xfer->flags_int.short_xfer_ok = 1; 1504 } 1505 } 1506 } 1507 /* 1508 * Check if BUS-DMA support is enabled and try to load virtual 1509 * buffers into DMA, if any: 1510 */ 1511 if (xfer->flags_int.bdma_enable) { 1512 /* insert the USB transfer last in the BUS-DMA queue */ 1513 usb2_command_wrapper(&xfer->xroot->dma_q, xfer); 1514 return; 1515 } 1516 /* 1517 * Enter the USB transfer into the Host Controller or 1518 * Device Controller schedule: 1519 */ 1520 usb2_pipe_enter(xfer); 1521 } 1522 1523 /*------------------------------------------------------------------------* 1524 * usb2_pipe_enter - factored out code 1525 *------------------------------------------------------------------------*/ 1526 void 1527 usb2_pipe_enter(struct usb2_xfer *xfer) 1528 { 1529 struct usb2_pipe *pipe; 1530 1531 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1532 1533 USB_BUS_LOCK(xfer->xroot->bus); 1534 1535 pipe = xfer->pipe; 1536 1537 DPRINTF("enter\n"); 1538 1539 /* enter the transfer */ 1540 (pipe->methods->enter) (xfer); 1541 1542 /* check cancelability */ 1543 if (pipe->methods->enter_is_cancelable) { 1544 xfer->flags_int.can_cancel_immed = 1; 1545 /* check for transfer error */ 1546 if (xfer->error) { 1547 /* some error has happened */ 1548 usb2_transfer_done(xfer, 0); 1549 USB_BUS_UNLOCK(xfer->xroot->bus); 1550 return; 1551 } 1552 } else { 1553 xfer->flags_int.can_cancel_immed = 0; 1554 } 1555 1556 /* start the transfer */ 1557 usb2_command_wrapper(&pipe->pipe_q, xfer); 1558 USB_BUS_UNLOCK(xfer->xroot->bus); 1559 } 1560 1561 /*------------------------------------------------------------------------* 1562 * usb2_transfer_start - start an USB transfer 1563 * 1564 * NOTE: Calling this function more than one time will only 1565 * result in a single transfer start, until the USB transfer 1566 * completes. 1567 *------------------------------------------------------------------------*/ 1568 void 1569 usb2_transfer_start(struct usb2_xfer *xfer) 1570 { 1571 if (xfer == NULL) { 1572 /* transfer is gone */ 1573 return; 1574 } 1575 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1576 1577 /* mark the USB transfer started */ 1578 1579 if (!xfer->flags_int.started) { 1580 xfer->flags_int.started = 1; 1581 } 1582 /* check if the USB transfer callback is already transferring */ 1583 1584 if (xfer->flags_int.transferring) { 1585 return; 1586 } 1587 USB_BUS_LOCK(xfer->xroot->bus); 1588 /* call the USB transfer callback */ 1589 usb2_callback_ss_done_defer(xfer); 1590 USB_BUS_UNLOCK(xfer->xroot->bus); 1591 } 1592 1593 /*------------------------------------------------------------------------* 1594 * usb2_transfer_stop - stop an USB transfer 1595 * 1596 * NOTE: Calling this function more than one time will only 1597 * result in a single transfer stop. 1598 * NOTE: When this function returns it is not safe to free nor 1599 * reuse any DMA buffers. See "usb2_transfer_drain()". 1600 *------------------------------------------------------------------------*/ 1601 void 1602 usb2_transfer_stop(struct usb2_xfer *xfer) 1603 { 1604 struct usb2_pipe *pipe; 1605 1606 if (xfer == NULL) { 1607 /* transfer is gone */ 1608 return; 1609 } 1610 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1611 1612 /* check if the USB transfer was ever opened */ 1613 1614 if (!xfer->flags_int.open) { 1615 /* nothing to do except clearing the "started" flag */ 1616 xfer->flags_int.started = 0; 1617 return; 1618 } 1619 /* try to stop the current USB transfer */ 1620 1621 USB_BUS_LOCK(xfer->xroot->bus); 1622 xfer->error = USB_ERR_CANCELLED;/* override any previous error */ 1623 /* 1624 * Clear "open" and "started" when both private and USB lock 1625 * is locked so that we don't get a race updating "flags_int" 1626 */ 1627 xfer->flags_int.open = 0; 1628 xfer->flags_int.started = 0; 1629 1630 /* 1631 * Check if we can cancel the USB transfer immediately. 1632 */ 1633 if (xfer->flags_int.transferring) { 1634 if (xfer->flags_int.can_cancel_immed && 1635 (!xfer->flags_int.did_close)) { 1636 DPRINTF("close\n"); 1637 /* 1638 * The following will lead to an USB_ERR_CANCELLED 1639 * error code being passed to the USB callback. 1640 */ 1641 (xfer->pipe->methods->close) (xfer); 1642 /* only close once */ 1643 xfer->flags_int.did_close = 1; 1644 } else { 1645 /* need to wait for the next done callback */ 1646 } 1647 } else { 1648 DPRINTF("close\n"); 1649 1650 /* close here and now */ 1651 (xfer->pipe->methods->close) (xfer); 1652 1653 /* 1654 * Any additional DMA delay is done by 1655 * "usb2_transfer_unsetup()". 1656 */ 1657 1658 /* 1659 * Special case. Check if we need to restart a blocked 1660 * pipe. 1661 */ 1662 pipe = xfer->pipe; 1663 1664 /* 1665 * If the current USB transfer is completing we need 1666 * to start the next one: 1667 */ 1668 if (pipe->pipe_q.curr == xfer) { 1669 usb2_command_wrapper(&pipe->pipe_q, NULL); 1670 } 1671 } 1672 1673 USB_BUS_UNLOCK(xfer->xroot->bus); 1674 } 1675 1676 /*------------------------------------------------------------------------* 1677 * usb2_transfer_pending 1678 * 1679 * This function will check if an USB transfer is pending which is a 1680 * little bit complicated! 1681 * Return values: 1682 * 0: Not pending 1683 * 1: Pending: The USB transfer will receive a callback in the future. 1684 *------------------------------------------------------------------------*/ 1685 uint8_t 1686 usb2_transfer_pending(struct usb2_xfer *xfer) 1687 { 1688 struct usb2_xfer_root *info; 1689 struct usb2_xfer_queue *pq; 1690 1691 if (xfer == NULL) { 1692 /* transfer is gone */ 1693 return (0); 1694 } 1695 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1696 1697 if (xfer->flags_int.transferring) { 1698 /* trivial case */ 1699 return (1); 1700 } 1701 USB_BUS_LOCK(xfer->xroot->bus); 1702 if (xfer->wait_queue) { 1703 /* we are waiting on a queue somewhere */ 1704 USB_BUS_UNLOCK(xfer->xroot->bus); 1705 return (1); 1706 } 1707 info = xfer->xroot; 1708 pq = &info->done_q; 1709 1710 if (pq->curr == xfer) { 1711 /* we are currently scheduled for callback */ 1712 USB_BUS_UNLOCK(xfer->xroot->bus); 1713 return (1); 1714 } 1715 /* we are not pending */ 1716 USB_BUS_UNLOCK(xfer->xroot->bus); 1717 return (0); 1718 } 1719 1720 /*------------------------------------------------------------------------* 1721 * usb2_transfer_drain 1722 * 1723 * This function will stop the USB transfer and wait for any 1724 * additional BUS-DMA and HW-DMA operations to complete. Buffers that 1725 * are loaded into DMA can safely be freed or reused after that this 1726 * function has returned. 1727 *------------------------------------------------------------------------*/ 1728 void 1729 usb2_transfer_drain(struct usb2_xfer *xfer) 1730 { 1731 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1732 "usb2_transfer_drain can sleep!"); 1733 1734 if (xfer == NULL) { 1735 /* transfer is gone */ 1736 return; 1737 } 1738 if (xfer->xroot->xfer_mtx != &Giant) { 1739 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED); 1740 } 1741 USB_XFER_LOCK(xfer); 1742 1743 usb2_transfer_stop(xfer); 1744 1745 while (usb2_transfer_pending(xfer)) { 1746 xfer->flags_int.draining = 1; 1747 /* 1748 * Wait until the current outstanding USB 1749 * transfer is complete ! 1750 */ 1751 usb2_cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); 1752 } 1753 USB_XFER_UNLOCK(xfer); 1754 } 1755 1756 /*------------------------------------------------------------------------* 1757 * usb2_set_frame_data 1758 * 1759 * This function sets the pointer of the buffer that should 1760 * loaded directly into DMA for the given USB frame. Passing "ptr" 1761 * equal to NULL while the corresponding "frlength" is greater 1762 * than zero gives undefined results! 1763 *------------------------------------------------------------------------*/ 1764 void 1765 usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr, uint32_t frindex) 1766 { 1767 /* set virtual address to load and length */ 1768 xfer->frbuffers[frindex].buffer = ptr; 1769 } 1770 1771 /*------------------------------------------------------------------------* 1772 * usb2_set_frame_offset 1773 * 1774 * This function sets the frame data buffer offset relative to the beginning 1775 * of the USB DMA buffer allocated for this USB transfer. 1776 *------------------------------------------------------------------------*/ 1777 void 1778 usb2_set_frame_offset(struct usb2_xfer *xfer, uint32_t offset, 1779 uint32_t frindex) 1780 { 1781 USB_ASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " 1782 "when the USB buffer is external!\n")); 1783 1784 /* set virtual address to load */ 1785 xfer->frbuffers[frindex].buffer = 1786 USB_ADD_BYTES(xfer->local_buffer, offset); 1787 } 1788 1789 /*------------------------------------------------------------------------* 1790 * usb2_callback_proc - factored out code 1791 * 1792 * This function performs USB callbacks. 1793 *------------------------------------------------------------------------*/ 1794 static void 1795 usb2_callback_proc(struct usb2_proc_msg *_pm) 1796 { 1797 struct usb2_done_msg *pm = (void *)_pm; 1798 struct usb2_xfer_root *info = pm->xroot; 1799 1800 /* Change locking order */ 1801 USB_BUS_UNLOCK(info->bus); 1802 1803 /* 1804 * We exploit the fact that the mutex is the same for all 1805 * callbacks that will be called from this thread: 1806 */ 1807 mtx_lock(info->xfer_mtx); 1808 USB_BUS_LOCK(info->bus); 1809 1810 /* Continue where we lost track */ 1811 usb2_command_wrapper(&info->done_q, 1812 info->done_q.curr); 1813 1814 mtx_unlock(info->xfer_mtx); 1815 } 1816 1817 /*------------------------------------------------------------------------* 1818 * usb2_callback_ss_done_defer 1819 * 1820 * This function will defer the start, stop and done callback to the 1821 * correct thread. 1822 *------------------------------------------------------------------------*/ 1823 static void 1824 usb2_callback_ss_done_defer(struct usb2_xfer *xfer) 1825 { 1826 struct usb2_xfer_root *info = xfer->xroot; 1827 struct usb2_xfer_queue *pq = &info->done_q; 1828 1829 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1830 1831 if (pq->curr != xfer) { 1832 usb2_transfer_enqueue(pq, xfer); 1833 } 1834 if (!pq->recurse_1) { 1835 1836 /* 1837 * We have to postpone the callback due to the fact we 1838 * will have a Lock Order Reversal, LOR, if we try to 1839 * proceed ! 1840 */ 1841 if (usb2_proc_msignal(info->done_p, 1842 &info->done_m[0], &info->done_m[1])) { 1843 /* ignore */ 1844 } 1845 } else { 1846 /* clear second recurse flag */ 1847 pq->recurse_2 = 0; 1848 } 1849 return; 1850 1851 } 1852 1853 /*------------------------------------------------------------------------* 1854 * usb2_callback_wrapper 1855 * 1856 * This is a wrapper for USB callbacks. This wrapper does some 1857 * auto-magic things like figuring out if we can call the callback 1858 * directly from the current context or if we need to wakeup the 1859 * interrupt process. 1860 *------------------------------------------------------------------------*/ 1861 static void 1862 usb2_callback_wrapper(struct usb2_xfer_queue *pq) 1863 { 1864 struct usb2_xfer *xfer = pq->curr; 1865 struct usb2_xfer_root *info = xfer->xroot; 1866 1867 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1868 if (!mtx_owned(xfer->xroot->xfer_mtx)) { 1869 /* 1870 * Cases that end up here: 1871 * 1872 * 5) HW interrupt done callback or other source. 1873 */ 1874 DPRINTFN(3, "case 5\n"); 1875 1876 /* 1877 * We have to postpone the callback due to the fact we 1878 * will have a Lock Order Reversal, LOR, if we try to 1879 * proceed ! 1880 */ 1881 if (usb2_proc_msignal(info->done_p, 1882 &info->done_m[0], &info->done_m[1])) { 1883 /* ignore */ 1884 } 1885 return; 1886 } 1887 /* 1888 * Cases that end up here: 1889 * 1890 * 1) We are starting a transfer 1891 * 2) We are prematurely calling back a transfer 1892 * 3) We are stopping a transfer 1893 * 4) We are doing an ordinary callback 1894 */ 1895 DPRINTFN(3, "case 1-4\n"); 1896 /* get next USB transfer in the queue */ 1897 info->done_q.curr = NULL; 1898 1899 USB_BUS_UNLOCK(xfer->xroot->bus); 1900 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_NOTOWNED); 1901 1902 /* set correct USB state for callback */ 1903 if (!xfer->flags_int.transferring) { 1904 xfer->usb2_state = USB_ST_SETUP; 1905 if (!xfer->flags_int.started) { 1906 /* we got stopped before we even got started */ 1907 USB_BUS_LOCK(xfer->xroot->bus); 1908 goto done; 1909 } 1910 } else { 1911 1912 if (usb2_callback_wrapper_sub(xfer)) { 1913 /* the callback has been deferred */ 1914 USB_BUS_LOCK(xfer->xroot->bus); 1915 goto done; 1916 } 1917 /* decrement power reference */ 1918 usb2_transfer_power_ref(xfer, -1); 1919 1920 xfer->flags_int.transferring = 0; 1921 1922 if (xfer->error) { 1923 xfer->usb2_state = USB_ST_ERROR; 1924 } else { 1925 /* set transferred state */ 1926 xfer->usb2_state = USB_ST_TRANSFERRED; 1927 1928 /* sync DMA memory, if any */ 1929 if (xfer->flags_int.bdma_enable && 1930 (!xfer->flags_int.bdma_no_post_sync)) { 1931 usb2_bdma_post_sync(xfer); 1932 } 1933 } 1934 } 1935 1936 /* call processing routine */ 1937 (xfer->callback) (xfer); 1938 1939 /* pickup the USB mutex again */ 1940 USB_BUS_LOCK(xfer->xroot->bus); 1941 1942 /* 1943 * Check if we got started after that we got cancelled, but 1944 * before we managed to do the callback. 1945 */ 1946 if ((!xfer->flags_int.open) && 1947 (xfer->flags_int.started) && 1948 (xfer->usb2_state == USB_ST_ERROR)) { 1949 /* try to loop, but not recursivly */ 1950 usb2_command_wrapper(&info->done_q, xfer); 1951 return; 1952 } 1953 1954 done: 1955 /* 1956 * Check if we are draining. 1957 */ 1958 if (xfer->flags_int.draining && 1959 (!xfer->flags_int.transferring)) { 1960 /* "usb2_transfer_drain()" is waiting for end of transfer */ 1961 xfer->flags_int.draining = 0; 1962 usb2_cv_broadcast(&xfer->xroot->cv_drain); 1963 } 1964 1965 /* do the next callback, if any */ 1966 usb2_command_wrapper(&info->done_q, 1967 info->done_q.curr); 1968 } 1969 1970 /*------------------------------------------------------------------------* 1971 * usb2_dma_delay_done_cb 1972 * 1973 * This function is called when the DMA delay has been exectuded, and 1974 * will make sure that the callback is called to complete the USB 1975 * transfer. This code path is ususally only used when there is an USB 1976 * error like USB_ERR_CANCELLED. 1977 *------------------------------------------------------------------------*/ 1978 static void 1979 usb2_dma_delay_done_cb(void *arg) 1980 { 1981 struct usb2_xfer *xfer = arg; 1982 1983 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1984 1985 DPRINTFN(3, "Completed %p\n", xfer); 1986 1987 /* queue callback for execution, again */ 1988 usb2_transfer_done(xfer, 0); 1989 } 1990 1991 /*------------------------------------------------------------------------* 1992 * usb2_transfer_dequeue 1993 * 1994 * - This function is used to remove an USB transfer from a USB 1995 * transfer queue. 1996 * 1997 * - This function can be called multiple times in a row. 1998 *------------------------------------------------------------------------*/ 1999 void 2000 usb2_transfer_dequeue(struct usb2_xfer *xfer) 2001 { 2002 struct usb2_xfer_queue *pq; 2003 2004 pq = xfer->wait_queue; 2005 if (pq) { 2006 TAILQ_REMOVE(&pq->head, xfer, wait_entry); 2007 xfer->wait_queue = NULL; 2008 } 2009 } 2010 2011 /*------------------------------------------------------------------------* 2012 * usb2_transfer_enqueue 2013 * 2014 * - This function is used to insert an USB transfer into a USB * 2015 * transfer queue. 2016 * 2017 * - This function can be called multiple times in a row. 2018 *------------------------------------------------------------------------*/ 2019 void 2020 usb2_transfer_enqueue(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer) 2021 { 2022 /* 2023 * Insert the USB transfer into the queue, if it is not 2024 * already on a USB transfer queue: 2025 */ 2026 if (xfer->wait_queue == NULL) { 2027 xfer->wait_queue = pq; 2028 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); 2029 } 2030 } 2031 2032 /*------------------------------------------------------------------------* 2033 * usb2_transfer_done 2034 * 2035 * - This function is used to remove an USB transfer from the busdma, 2036 * pipe or interrupt queue. 2037 * 2038 * - This function is used to queue the USB transfer on the done 2039 * queue. 2040 * 2041 * - This function is used to stop any USB transfer timeouts. 2042 *------------------------------------------------------------------------*/ 2043 void 2044 usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error) 2045 { 2046 struct usb2_xfer_queue *pq; 2047 2048 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2049 2050 DPRINTF("err=%s\n", usb2_errstr(error)); 2051 2052 /* 2053 * If we are not transferring then just return. 2054 * This can happen during transfer cancel. 2055 */ 2056 if (!xfer->flags_int.transferring) { 2057 DPRINTF("not transferring\n"); 2058 return; 2059 } 2060 /* only set transfer error if not already set */ 2061 if (!xfer->error) { 2062 xfer->error = error; 2063 } 2064 /* stop any callouts */ 2065 usb2_callout_stop(&xfer->timeout_handle); 2066 2067 /* 2068 * If we are waiting on a queue, just remove the USB transfer 2069 * from the queue, if any. We should have the required locks 2070 * locked to do the remove when this function is called. 2071 */ 2072 usb2_transfer_dequeue(xfer); 2073 2074 if (mtx_owned(xfer->xroot->xfer_mtx)) { 2075 /* 2076 * If the private USB lock is not locked, then we assume 2077 * that the BUS-DMA load stage has been passed: 2078 */ 2079 pq = &xfer->xroot->dma_q; 2080 2081 if (pq->curr == xfer) { 2082 /* start the next BUS-DMA load, if any */ 2083 usb2_command_wrapper(pq, NULL); 2084 } 2085 } 2086 /* keep some statistics */ 2087 if (xfer->error) { 2088 xfer->xroot->bus->stats_err.uds_requests 2089 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++; 2090 } else { 2091 xfer->xroot->bus->stats_ok.uds_requests 2092 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++; 2093 } 2094 2095 /* call the USB transfer callback */ 2096 usb2_callback_ss_done_defer(xfer); 2097 } 2098 2099 /*------------------------------------------------------------------------* 2100 * usb2_transfer_start_cb 2101 * 2102 * This function is called to start the USB transfer when 2103 * "xfer->interval" is greater than zero, and and the endpoint type is 2104 * BULK or CONTROL. 2105 *------------------------------------------------------------------------*/ 2106 static void 2107 usb2_transfer_start_cb(void *arg) 2108 { 2109 struct usb2_xfer *xfer = arg; 2110 struct usb2_pipe *pipe = xfer->pipe; 2111 2112 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2113 2114 DPRINTF("start\n"); 2115 2116 /* start the transfer */ 2117 (pipe->methods->start) (xfer); 2118 2119 /* check cancelability */ 2120 if (pipe->methods->start_is_cancelable) { 2121 xfer->flags_int.can_cancel_immed = 1; 2122 if (xfer->error) { 2123 /* some error has happened */ 2124 usb2_transfer_done(xfer, 0); 2125 } 2126 } else { 2127 xfer->flags_int.can_cancel_immed = 0; 2128 } 2129 } 2130 2131 /*------------------------------------------------------------------------* 2132 * usb2_transfer_set_stall 2133 * 2134 * This function is used to set the stall flag outside the 2135 * callback. This function is NULL safe. 2136 *------------------------------------------------------------------------*/ 2137 void 2138 usb2_transfer_set_stall(struct usb2_xfer *xfer) 2139 { 2140 if (xfer == NULL) { 2141 /* tearing down */ 2142 return; 2143 } 2144 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2145 2146 /* avoid any races by locking the USB mutex */ 2147 USB_BUS_LOCK(xfer->xroot->bus); 2148 2149 xfer->flags.stall_pipe = 1; 2150 2151 USB_BUS_UNLOCK(xfer->xroot->bus); 2152 } 2153 2154 /*------------------------------------------------------------------------* 2155 * usb2_transfer_clear_stall 2156 * 2157 * This function is used to clear the stall flag outside the 2158 * callback. This function is NULL safe. 2159 *------------------------------------------------------------------------*/ 2160 void 2161 usb2_transfer_clear_stall(struct usb2_xfer *xfer) 2162 { 2163 if (xfer == NULL) { 2164 /* tearing down */ 2165 return; 2166 } 2167 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2168 2169 /* avoid any races by locking the USB mutex */ 2170 USB_BUS_LOCK(xfer->xroot->bus); 2171 2172 xfer->flags.stall_pipe = 0; 2173 2174 USB_BUS_UNLOCK(xfer->xroot->bus); 2175 } 2176 2177 /*------------------------------------------------------------------------* 2178 * usb2_pipe_start 2179 * 2180 * This function is used to add an USB transfer to the pipe transfer list. 2181 *------------------------------------------------------------------------*/ 2182 void 2183 usb2_pipe_start(struct usb2_xfer_queue *pq) 2184 { 2185 struct usb2_pipe *pipe; 2186 struct usb2_xfer *xfer; 2187 uint8_t type; 2188 2189 xfer = pq->curr; 2190 pipe = xfer->pipe; 2191 2192 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2193 2194 /* 2195 * If the pipe is already stalled we do nothing ! 2196 */ 2197 if (pipe->is_stalled) { 2198 return; 2199 } 2200 /* 2201 * Check if we are supposed to stall the pipe: 2202 */ 2203 if (xfer->flags.stall_pipe) { 2204 /* clear stall command */ 2205 xfer->flags.stall_pipe = 0; 2206 2207 /* 2208 * Only stall BULK and INTERRUPT endpoints. 2209 */ 2210 type = (pipe->edesc->bmAttributes & UE_XFERTYPE); 2211 if ((type == UE_BULK) || 2212 (type == UE_INTERRUPT)) { 2213 struct usb2_device *udev; 2214 struct usb2_xfer_root *info; 2215 2216 info = xfer->xroot; 2217 udev = info->udev; 2218 pipe->is_stalled = 1; 2219 2220 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 2221 (udev->bus->methods->set_stall) ( 2222 udev, NULL, pipe); 2223 } else if (udev->default_xfer[1]) { 2224 info = udev->default_xfer[1]->xroot; 2225 if (usb2_proc_msignal( 2226 &info->bus->non_giant_callback_proc, 2227 &udev->cs_msg[0], &udev->cs_msg[1])) { 2228 /* ignore */ 2229 } 2230 } else { 2231 /* should not happen */ 2232 DPRINTFN(0, "No stall handler!\n"); 2233 } 2234 /* 2235 * We get started again when the stall is cleared! 2236 */ 2237 return; 2238 } 2239 } 2240 /* Set or clear stall complete - special case */ 2241 if (xfer->nframes == 0) { 2242 /* we are complete */ 2243 xfer->aframes = 0; 2244 usb2_transfer_done(xfer, 0); 2245 return; 2246 } 2247 /* 2248 * Handled cases: 2249 * 2250 * 1) Start the first transfer queued. 2251 * 2252 * 2) Re-start the current USB transfer. 2253 */ 2254 /* 2255 * Check if there should be any 2256 * pre transfer start delay: 2257 */ 2258 if (xfer->interval > 0) { 2259 type = (pipe->edesc->bmAttributes & UE_XFERTYPE); 2260 if ((type == UE_BULK) || 2261 (type == UE_CONTROL)) { 2262 usb2_transfer_timeout_ms(xfer, 2263 &usb2_transfer_start_cb, 2264 xfer->interval); 2265 return; 2266 } 2267 } 2268 DPRINTF("start\n"); 2269 2270 /* start USB transfer */ 2271 (pipe->methods->start) (xfer); 2272 2273 /* check cancelability */ 2274 if (pipe->methods->start_is_cancelable) { 2275 xfer->flags_int.can_cancel_immed = 1; 2276 if (xfer->error) { 2277 /* some error has happened */ 2278 usb2_transfer_done(xfer, 0); 2279 } 2280 } else { 2281 xfer->flags_int.can_cancel_immed = 0; 2282 } 2283 } 2284 2285 /*------------------------------------------------------------------------* 2286 * usb2_transfer_timeout_ms 2287 * 2288 * This function is used to setup a timeout on the given USB 2289 * transfer. If the timeout has been deferred the callback given by 2290 * "cb" will get called after "ms" milliseconds. 2291 *------------------------------------------------------------------------*/ 2292 void 2293 usb2_transfer_timeout_ms(struct usb2_xfer *xfer, 2294 void (*cb) (void *arg), uint32_t ms) 2295 { 2296 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2297 2298 /* defer delay */ 2299 usb2_callout_reset(&xfer->timeout_handle, 2300 USB_MS_TO_TICKS(ms), cb, xfer); 2301 } 2302 2303 /*------------------------------------------------------------------------* 2304 * usb2_callback_wrapper_sub 2305 * 2306 * - This function will update variables in an USB transfer after 2307 * that the USB transfer is complete. 2308 * 2309 * - This function is used to start the next USB transfer on the 2310 * pipe transfer queue, if any. 2311 * 2312 * NOTE: In some special cases the USB transfer will not be removed from 2313 * the pipe queue, but remain first. To enforce USB transfer removal call 2314 * this function passing the error code "USB_ERR_CANCELLED". 2315 * 2316 * Return values: 2317 * 0: Success. 2318 * Else: The callback has been deferred. 2319 *------------------------------------------------------------------------*/ 2320 static uint8_t 2321 usb2_callback_wrapper_sub(struct usb2_xfer *xfer) 2322 { 2323 struct usb2_pipe *pipe; 2324 uint32_t x; 2325 2326 if ((!xfer->flags_int.open) && 2327 (!xfer->flags_int.did_close)) { 2328 DPRINTF("close\n"); 2329 USB_BUS_LOCK(xfer->xroot->bus); 2330 (xfer->pipe->methods->close) (xfer); 2331 USB_BUS_UNLOCK(xfer->xroot->bus); 2332 /* only close once */ 2333 xfer->flags_int.did_close = 1; 2334 return (1); /* wait for new callback */ 2335 } 2336 /* 2337 * If we have a non-hardware induced error we 2338 * need to do the DMA delay! 2339 */ 2340 if (((xfer->error == USB_ERR_CANCELLED) || 2341 (xfer->error == USB_ERR_TIMEOUT)) && 2342 (!xfer->flags_int.did_dma_delay)) { 2343 2344 uint32_t temp; 2345 2346 /* only delay once */ 2347 xfer->flags_int.did_dma_delay = 1; 2348 2349 /* we can not cancel this delay */ 2350 xfer->flags_int.can_cancel_immed = 0; 2351 2352 temp = usb2_get_dma_delay(xfer->xroot->bus); 2353 2354 DPRINTFN(3, "DMA delay, %u ms, " 2355 "on %p\n", temp, xfer); 2356 2357 if (temp != 0) { 2358 USB_BUS_LOCK(xfer->xroot->bus); 2359 usb2_transfer_timeout_ms(xfer, 2360 &usb2_dma_delay_done_cb, temp); 2361 USB_BUS_UNLOCK(xfer->xroot->bus); 2362 return (1); /* wait for new callback */ 2363 } 2364 } 2365 /* check actual number of frames */ 2366 if (xfer->aframes > xfer->nframes) { 2367 if (xfer->error == 0) { 2368 panic("%s: actual number of frames, %d, is " 2369 "greater than initial number of frames, %d!\n", 2370 __FUNCTION__, xfer->aframes, xfer->nframes); 2371 } else { 2372 /* just set some valid value */ 2373 xfer->aframes = xfer->nframes; 2374 } 2375 } 2376 /* compute actual length */ 2377 xfer->actlen = 0; 2378 2379 for (x = 0; x != xfer->aframes; x++) { 2380 xfer->actlen += xfer->frlengths[x]; 2381 } 2382 2383 /* 2384 * Frames that were not transferred get zero actual length in 2385 * case the USB device driver does not check the actual number 2386 * of frames transferred, "xfer->aframes": 2387 */ 2388 for (; x < xfer->nframes; x++) { 2389 xfer->frlengths[x] = 0; 2390 } 2391 2392 /* check actual length */ 2393 if (xfer->actlen > xfer->sumlen) { 2394 if (xfer->error == 0) { 2395 panic("%s: actual length, %d, is greater than " 2396 "initial length, %d!\n", 2397 __FUNCTION__, xfer->actlen, xfer->sumlen); 2398 } else { 2399 /* just set some valid value */ 2400 xfer->actlen = xfer->sumlen; 2401 } 2402 } 2403 DPRINTFN(6, "xfer=%p pipe=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n", 2404 xfer, xfer->pipe, xfer->error, xfer->actlen, xfer->sumlen, 2405 xfer->aframes, xfer->nframes); 2406 2407 if (xfer->error) { 2408 /* end of control transfer, if any */ 2409 xfer->flags_int.control_act = 0; 2410 2411 /* check if we should block the execution queue */ 2412 if ((xfer->error != USB_ERR_CANCELLED) && 2413 (xfer->flags.pipe_bof)) { 2414 DPRINTFN(2, "xfer=%p: Block On Failure " 2415 "on pipe=%p\n", xfer, xfer->pipe); 2416 goto done; 2417 } 2418 } else { 2419 /* check for short transfers */ 2420 if (xfer->actlen < xfer->sumlen) { 2421 2422 /* end of control transfer, if any */ 2423 xfer->flags_int.control_act = 0; 2424 2425 if (!xfer->flags_int.short_xfer_ok) { 2426 xfer->error = USB_ERR_SHORT_XFER; 2427 if (xfer->flags.pipe_bof) { 2428 DPRINTFN(2, "xfer=%p: Block On Failure on " 2429 "Short Transfer on pipe %p.\n", 2430 xfer, xfer->pipe); 2431 goto done; 2432 } 2433 } 2434 } else { 2435 /* 2436 * Check if we are in the middle of a 2437 * control transfer: 2438 */ 2439 if (xfer->flags_int.control_act) { 2440 DPRINTFN(5, "xfer=%p: Control transfer " 2441 "active on pipe=%p\n", xfer, xfer->pipe); 2442 goto done; 2443 } 2444 } 2445 } 2446 2447 pipe = xfer->pipe; 2448 2449 /* 2450 * If the current USB transfer is completing we need to start the 2451 * next one: 2452 */ 2453 USB_BUS_LOCK(xfer->xroot->bus); 2454 if (pipe->pipe_q.curr == xfer) { 2455 usb2_command_wrapper(&pipe->pipe_q, NULL); 2456 2457 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) { 2458 /* there is another USB transfer waiting */ 2459 } else { 2460 /* this is the last USB transfer */ 2461 /* clear isochronous sync flag */ 2462 xfer->pipe->is_synced = 0; 2463 } 2464 } 2465 USB_BUS_UNLOCK(xfer->xroot->bus); 2466 done: 2467 return (0); 2468 } 2469 2470 /*------------------------------------------------------------------------* 2471 * usb2_command_wrapper 2472 * 2473 * This function is used to execute commands non-recursivly on an USB 2474 * transfer. 2475 *------------------------------------------------------------------------*/ 2476 void 2477 usb2_command_wrapper(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer) 2478 { 2479 if (xfer) { 2480 /* 2481 * If the transfer is not already processing, 2482 * queue it! 2483 */ 2484 if (pq->curr != xfer) { 2485 usb2_transfer_enqueue(pq, xfer); 2486 if (pq->curr != NULL) { 2487 /* something is already processing */ 2488 DPRINTFN(6, "busy %p\n", pq->curr); 2489 return; 2490 } 2491 } 2492 } else { 2493 /* Get next element in queue */ 2494 pq->curr = NULL; 2495 } 2496 2497 if (!pq->recurse_1) { 2498 2499 do { 2500 2501 /* set both recurse flags */ 2502 pq->recurse_1 = 1; 2503 pq->recurse_2 = 1; 2504 2505 if (pq->curr == NULL) { 2506 xfer = TAILQ_FIRST(&pq->head); 2507 if (xfer) { 2508 TAILQ_REMOVE(&pq->head, xfer, 2509 wait_entry); 2510 xfer->wait_queue = NULL; 2511 pq->curr = xfer; 2512 } else { 2513 break; 2514 } 2515 } 2516 DPRINTFN(6, "cb %p (enter)\n", pq->curr); 2517 (pq->command) (pq); 2518 DPRINTFN(6, "cb %p (leave)\n", pq->curr); 2519 2520 } while (!pq->recurse_2); 2521 2522 /* clear first recurse flag */ 2523 pq->recurse_1 = 0; 2524 2525 } else { 2526 /* clear second recurse flag */ 2527 pq->recurse_2 = 0; 2528 } 2529 } 2530 2531 /*------------------------------------------------------------------------* 2532 * usb2_default_transfer_setup 2533 * 2534 * This function is used to setup the default USB control endpoint 2535 * transfer. 2536 *------------------------------------------------------------------------*/ 2537 void 2538 usb2_default_transfer_setup(struct usb2_device *udev) 2539 { 2540 struct usb2_xfer *xfer; 2541 uint8_t no_resetup; 2542 uint8_t iface_index; 2543 2544 repeat: 2545 2546 xfer = udev->default_xfer[0]; 2547 if (xfer) { 2548 USB_XFER_LOCK(xfer); 2549 no_resetup = 2550 ((xfer->address == udev->address) && 2551 (udev->default_ep_desc.wMaxPacketSize[0] == 2552 udev->ddesc.bMaxPacketSize)); 2553 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 2554 if (no_resetup) { 2555 /* 2556 * NOTE: checking "xfer->address" and 2557 * starting the USB transfer must be 2558 * atomic! 2559 */ 2560 usb2_transfer_start(xfer); 2561 } 2562 } 2563 USB_XFER_UNLOCK(xfer); 2564 } else { 2565 no_resetup = 0; 2566 } 2567 2568 if (no_resetup) { 2569 /* 2570 * All parameters are exactly the same like before. 2571 * Just return. 2572 */ 2573 return; 2574 } 2575 /* 2576 * Update wMaxPacketSize for the default control endpoint: 2577 */ 2578 udev->default_ep_desc.wMaxPacketSize[0] = 2579 udev->ddesc.bMaxPacketSize; 2580 2581 /* 2582 * Unsetup any existing USB transfer: 2583 */ 2584 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 2585 2586 /* 2587 * Try to setup a new USB transfer for the 2588 * default control endpoint: 2589 */ 2590 iface_index = 0; 2591 if (usb2_transfer_setup(udev, &iface_index, 2592 udev->default_xfer, usb2_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL, 2593 udev->default_mtx)) { 2594 DPRINTFN(0, "could not setup default " 2595 "USB transfer!\n"); 2596 } else { 2597 goto repeat; 2598 } 2599 } 2600 2601 /*------------------------------------------------------------------------* 2602 * usb2_clear_data_toggle - factored out code 2603 * 2604 * NOTE: the intention of this function is not to reset the hardware 2605 * data toggle. 2606 *------------------------------------------------------------------------*/ 2607 void 2608 usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe) 2609 { 2610 DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe); 2611 2612 USB_BUS_LOCK(udev->bus); 2613 pipe->toggle_next = 0; 2614 USB_BUS_UNLOCK(udev->bus); 2615 } 2616 2617 /*------------------------------------------------------------------------* 2618 * usb2_clear_stall_callback - factored out clear stall callback 2619 * 2620 * Input parameters: 2621 * xfer1: Clear Stall Control Transfer 2622 * xfer2: Stalled USB Transfer 2623 * 2624 * This function is NULL safe. 2625 * 2626 * Return values: 2627 * 0: In progress 2628 * Else: Finished 2629 * 2630 * Clear stall config example: 2631 * 2632 * static const struct usb2_config my_clearstall = { 2633 * .type = UE_CONTROL, 2634 * .endpoint = 0, 2635 * .direction = UE_DIR_ANY, 2636 * .interval = 50, //50 milliseconds 2637 * .bufsize = sizeof(struct usb2_device_request), 2638 * .mh.timeout = 1000, //1.000 seconds 2639 * .mh.flags = { }, 2640 * .mh.callback = &my_clear_stall_callback, // ** 2641 * }; 2642 * 2643 * ** "my_clear_stall_callback" calls "usb2_clear_stall_callback" 2644 * passing the correct parameters. 2645 *------------------------------------------------------------------------*/ 2646 uint8_t 2647 usb2_clear_stall_callback(struct usb2_xfer *xfer1, 2648 struct usb2_xfer *xfer2) 2649 { 2650 struct usb2_device_request req; 2651 2652 if (xfer2 == NULL) { 2653 /* looks like we are tearing down */ 2654 DPRINTF("NULL input parameter\n"); 2655 return (0); 2656 } 2657 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED); 2658 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED); 2659 2660 switch (USB_GET_STATE(xfer1)) { 2661 case USB_ST_SETUP: 2662 2663 /* 2664 * pre-clear the data toggle to DATA0 ("umass.c" and 2665 * "ata-usb.c" depends on this) 2666 */ 2667 2668 usb2_clear_data_toggle(xfer2->xroot->udev, xfer2->pipe); 2669 2670 /* setup a clear-stall packet */ 2671 2672 req.bmRequestType = UT_WRITE_ENDPOINT; 2673 req.bRequest = UR_CLEAR_FEATURE; 2674 USETW(req.wValue, UF_ENDPOINT_HALT); 2675 req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress; 2676 req.wIndex[1] = 0; 2677 USETW(req.wLength, 0); 2678 2679 /* 2680 * "usb2_transfer_setup_sub()" will ensure that 2681 * we have sufficient room in the buffer for 2682 * the request structure! 2683 */ 2684 2685 /* copy in the transfer */ 2686 2687 usb2_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); 2688 2689 /* set length */ 2690 xfer1->frlengths[0] = sizeof(req); 2691 xfer1->nframes = 1; 2692 2693 usb2_start_hardware(xfer1); 2694 return (0); 2695 2696 case USB_ST_TRANSFERRED: 2697 break; 2698 2699 default: /* Error */ 2700 if (xfer1->error == USB_ERR_CANCELLED) { 2701 return (0); 2702 } 2703 break; 2704 } 2705 return (1); /* Clear Stall Finished */ 2706 } 2707 2708 void 2709 usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max) 2710 { 2711 static uint8_t once = 0; 2712 /* polling is currently not supported */ 2713 if (!once) { 2714 once = 1; 2715 printf("usb2_do_poll: USB polling is " 2716 "not supported!\n"); 2717 } 2718 } 2719