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