usb_transfer.c revision 191824
11590Srgrimes/* $FreeBSD: head/sys/dev/usb/usb_transfer.c 191824 2009-05-05 15:36:23Z thompsa $ */ 21590Srgrimes/*- 31590Srgrimes * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 151590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 181590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241590Srgrimes * SUCH DAMAGE. 251590Srgrimes */ 261590Srgrimes 271590Srgrimes#include <dev/usb/usb_mfunc.h> 281590Srgrimes#include <dev/usb/usb_error.h> 291590Srgrimes#include <dev/usb/usb.h> 301590Srgrimes 311590Srgrimes#define USB_DEBUG_VAR usb2_debug 321590Srgrimes 331590Srgrimes#include <dev/usb/usb_core.h> 341590Srgrimes#include <dev/usb/usb_busdma.h> 351590Srgrimes#include <dev/usb/usb_process.h> 361590Srgrimes#include <dev/usb/usb_transfer.h> 371590Srgrimes#include <dev/usb/usb_device.h> 381590Srgrimes#include <dev/usb/usb_debug.h> 3928066Scharnier#include <dev/usb/usb_util.h> 401590Srgrimes 4128066Scharnier#include <dev/usb/usb_controller.h> 4228066Scharnier#include <dev/usb/usb_bus.h> 4350477Speter 441590Srgrimesstruct usb2_std_packet_size { 451590Srgrimes struct { 461590Srgrimes uint16_t min; /* inclusive */ 471590Srgrimes uint16_t max; /* inclusive */ 481590Srgrimes } range; 491590Srgrimes 501590Srgrimes uint16_t fixed[4]; 511590Srgrimes}; 5228066Scharnier 531590Srgrimesstatic usb2_callback_t usb2_request_callback; 541590Srgrimes 551590Srgrimesstatic const struct usb2_config usb2_control_ep_cfg[USB_DEFAULT_XFER_MAX] = { 561590Srgrimes 571590Srgrimes /* This transfer is used for generic control endpoint transfers */ 581590Srgrimes 591590Srgrimes [0] = { 601590Srgrimes .type = UE_CONTROL, 611590Srgrimes .endpoint = 0x00, /* Control endpoint */ 621590Srgrimes .direction = UE_DIR_ANY, 631590Srgrimes .bufsize = USB_EP0_BUFSIZE, /* bytes */ 641590Srgrimes .flags = {.proxy_buffer = 1,}, 651590Srgrimes .callback = &usb2_request_callback, 661590Srgrimes .usb_mode = USB_MODE_MAX, /* both modes */ 671590Srgrimes }, 681590Srgrimes 691590Srgrimes /* This transfer is used for generic clear stall only */ 701590Srgrimes 711590Srgrimes [1] = { 721590Srgrimes .type = UE_CONTROL, 731590Srgrimes .endpoint = 0x00, /* Control pipe */ 741590Srgrimes .direction = UE_DIR_ANY, 751590Srgrimes .bufsize = sizeof(struct usb2_device_request), 761590Srgrimes .callback = &usb2_do_clear_stall_callback, 771590Srgrimes .timeout = 1000, /* 1 second */ 781590Srgrimes .interval = 50, /* 50ms */ 791590Srgrimes .usb_mode = USB_MODE_HOST, 801590Srgrimes }, 811590Srgrimes}; 821590Srgrimes 831590Srgrimes/* function prototypes */ 841590Srgrimes 851590Srgrimesstatic void usb2_update_max_frame_size(struct usb2_xfer *); 861590Srgrimesstatic void usb2_transfer_unsetup_sub(struct usb2_xfer_root *, uint8_t); 871590Srgrimesstatic void usb2_control_transfer_init(struct usb2_xfer *); 881590Srgrimesstatic uint8_t usb2_start_hardware_sub(struct usb2_xfer *); 891590Srgrimesstatic void usb2_callback_proc(struct usb2_proc_msg *); 901590Srgrimesstatic void usb2_callback_ss_done_defer(struct usb2_xfer *); 911590Srgrimesstatic void usb2_callback_wrapper(struct usb2_xfer_queue *); 921590Srgrimesstatic void usb2_dma_delay_done_cb(void *); 931590Srgrimesstatic void usb2_transfer_start_cb(void *); 941590Srgrimesstatic uint8_t usb2_callback_wrapper_sub(struct usb2_xfer *); 951590Srgrimesstatic void usb2_get_std_packet_size(struct usb2_std_packet_size *ptr, 961590Srgrimes uint8_t type, uint8_t usb_speed); 9741572Sarchie 9810075Sjkh/*------------------------------------------------------------------------* 991590Srgrimes * usb2_request_callback 1001590Srgrimes *------------------------------------------------------------------------*/ 1011590Srgrimesstatic void 10210075Sjkhusb2_request_callback(struct usb2_xfer *xfer) 1031590Srgrimes{ 1041590Srgrimes if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) 1051590Srgrimes usb2_handle_request_callback(xfer); 1061590Srgrimes else 1071590Srgrimes usb2_do_request_callback(xfer); 1081590Srgrimes} 1091590Srgrimes 1101590Srgrimes/*------------------------------------------------------------------------* 1111590Srgrimes * usb2_update_max_frame_size 1121590Srgrimes * 1131590Srgrimes * This function updates the maximum frame size, hence high speed USB 1141590Srgrimes * can transfer multiple consecutive packets. 1151590Srgrimes *------------------------------------------------------------------------*/ 11680286Sobrienstatic void 1171590Srgrimesusb2_update_max_frame_size(struct usb2_xfer *xfer) 11880286Sobrien{ 11980286Sobrien /* compute maximum frame size */ 1201590Srgrimes 1211590Srgrimes if (xfer->max_packet_count == 2) { 1221590Srgrimes xfer->max_frame_size = 2 * xfer->max_packet_size; 1231590Srgrimes } else if (xfer->max_packet_count == 3) { 1241590Srgrimes xfer->max_frame_size = 3 * xfer->max_packet_size; 1251590Srgrimes } else { 1261590Srgrimes xfer->max_frame_size = xfer->max_packet_size; 1271590Srgrimes } 1281590Srgrimes} 1291590Srgrimes 1301590Srgrimes/*------------------------------------------------------------------------* 1311590Srgrimes * usb2_get_dma_delay 1321590Srgrimes * 1331590Srgrimes * The following function is called when we need to 1341590Srgrimes * synchronize with DMA hardware. 1351590Srgrimes * 1361590Srgrimes * Returns: 1371590Srgrimes * 0: no DMA delay required 1381590Srgrimes * Else: milliseconds of DMA delay 1391590Srgrimes *------------------------------------------------------------------------*/ 14010075Sjkhusb2_timeout_t 1411590Srgrimesusb2_get_dma_delay(struct usb2_bus *bus) 14210075Sjkh{ 14310075Sjkh uint32_t temp = 0; 14410075Sjkh 1451590Srgrimes if (bus->methods->get_dma_delay) { 14610075Sjkh (bus->methods->get_dma_delay) (bus, &temp); 1471590Srgrimes /* 1481590Srgrimes * Round up and convert to milliseconds. Note that we use 1491590Srgrimes * 1024 milliseconds per second. to save a division. 1501590Srgrimes */ 1511590Srgrimes temp += 0x3FF; 1521590Srgrimes temp /= 0x400; 1531590Srgrimes } 1541590Srgrimes return (temp); 1551590Srgrimes} 1561590Srgrimes 1571590Srgrimes/*------------------------------------------------------------------------* 1581590Srgrimes * usb2_transfer_setup_sub_malloc 1591590Srgrimes * 1601590Srgrimes * This function will allocate one or more DMA'able memory chunks 1611590Srgrimes * according to "size", "align" and "count" arguments. "ppc" is 1621590Srgrimes * pointed to a linear array of USB page caches afterwards. 1631590Srgrimes * 1641590Srgrimes * Returns: 1651590Srgrimes * 0: Success 1661590Srgrimes * Else: Failure 1671590Srgrimes *------------------------------------------------------------------------*/ 1681590Srgrimes#if USB_HAVE_BUSDMA 1691590Srgrimesuint8_t 1701590Srgrimesusb2_transfer_setup_sub_malloc(struct usb2_setup_params *parm, 1711590Srgrimes struct usb2_page_cache **ppc, usb2_size_t size, usb2_size_t align, 1721590Srgrimes usb2_size_t count) 1731590Srgrimes{ 1741590Srgrimes struct usb2_page_cache *pc; 1751590Srgrimes struct usb2_page *pg; 1761590Srgrimes void *buf; 1771590Srgrimes usb2_size_t n_dma_pc; 1781590Srgrimes usb2_size_t n_obj; 1791590Srgrimes usb2_size_t x; 1801590Srgrimes usb2_size_t y; 1811590Srgrimes usb2_size_t r; 1821590Srgrimes usb2_size_t z; 1831590Srgrimes 1841590Srgrimes USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n", 1851590Srgrimes align)); 1861590Srgrimes USB_ASSERT(size > 0, ("Invalid size = 0!\n")); 1871590Srgrimes 1881590Srgrimes if (count == 0) { 1891590Srgrimes return (0); /* nothing to allocate */ 19010075Sjkh } 19110075Sjkh /* 19210075Sjkh * Make sure that the size is aligned properly. 1931590Srgrimes */ 1941590Srgrimes size = -((-size) & (-align)); 1951590Srgrimes 19610075Sjkh /* 1971590Srgrimes * Try multi-allocation chunks to reduce the number of DMA 1981590Srgrimes * allocations, hence DMA allocations are slow. 1991590Srgrimes */ 2001590Srgrimes if (size >= PAGE_SIZE) { 2011590Srgrimes n_dma_pc = count; 2021590Srgrimes n_obj = 1; 2031590Srgrimes } else { 2041590Srgrimes /* compute number of objects per page */ 20580286Sobrien n_obj = (PAGE_SIZE / size); 2061590Srgrimes /* 20780286Sobrien * Compute number of DMA chunks, rounded up 20880286Sobrien * to nearest one: 2091590Srgrimes */ 2101590Srgrimes n_dma_pc = ((count + n_obj - 1) / n_obj); 2111590Srgrimes } 2121590Srgrimes 2131590Srgrimes if (parm->buf == NULL) { 2141590Srgrimes /* for the future */ 2151590Srgrimes parm->dma_page_ptr += n_dma_pc; 2161590Srgrimes parm->dma_page_cache_ptr += n_dma_pc; 2171590Srgrimes parm->dma_page_ptr += count; 2181590Srgrimes parm->xfer_page_cache_ptr += count; 2191590Srgrimes return (0); 2201590Srgrimes } 2211590Srgrimes for (x = 0; x != n_dma_pc; x++) { 2221590Srgrimes /* need to initialize the page cache */ 2231590Srgrimes parm->dma_page_cache_ptr[x].tag_parent = 2241590Srgrimes &parm->curr_xfer->xroot->dma_parent_tag; 2251590Srgrimes } 2261590Srgrimes for (x = 0; x != count; x++) { 2271590Srgrimes /* need to initialize the page cache */ 2281590Srgrimes parm->xfer_page_cache_ptr[x].tag_parent = 2291590Srgrimes &parm->curr_xfer->xroot->dma_parent_tag; 23028066Scharnier } 2311590Srgrimes 23228066Scharnier if (ppc) { 2331590Srgrimes *ppc = parm->xfer_page_cache_ptr; 2341590Srgrimes } 2351590Srgrimes r = count; /* set remainder count */ 23610075Sjkh z = n_obj * size; /* set allocation size */ 2371590Srgrimes pc = parm->xfer_page_cache_ptr; 2381590Srgrimes pg = parm->dma_page_ptr; 2391590Srgrimes 2401590Srgrimes for (x = 0; x != n_dma_pc; x++) { 2411590Srgrimes 2421590Srgrimes if (r < n_obj) { 2431590Srgrimes /* compute last remainder */ 2441590Srgrimes z = r * size; 24535520Sache n_obj = r; 2461590Srgrimes } 2471590Srgrimes if (usb2_pc_alloc_mem(parm->dma_page_cache_ptr, 2481590Srgrimes pg, z, align)) { 2491590Srgrimes return (1); /* failure */ 2501590Srgrimes } 2511590Srgrimes /* Set beginning of current buffer */ 2521590Srgrimes buf = parm->dma_page_cache_ptr->buffer; 2531590Srgrimes /* Make room for one DMA page cache and one page */ 2541590Srgrimes parm->dma_page_cache_ptr++; 2551590Srgrimes pg++; 2561590Srgrimes 2571590Srgrimes for (y = 0; (y != n_obj); y++, r--, pc++, pg++) { 2581590Srgrimes 2591590Srgrimes /* Load sub-chunk into DMA */ 2601590Srgrimes if (usb2_pc_dmamap_create(pc, size)) { 2611590Srgrimes return (1); /* failure */ 2621590Srgrimes } 2631590Srgrimes pc->buffer = USB_ADD_BYTES(buf, y * size); 2641590Srgrimes pc->page_start = pg; 2651590Srgrimes 2661590Srgrimes mtx_lock(pc->tag_parent->mtx); 2671590Srgrimes if (usb2_pc_load_mem(pc, size, 1 /* synchronous */ )) { 2681590Srgrimes mtx_unlock(pc->tag_parent->mtx); 2691590Srgrimes return (1); /* failure */ 2701590Srgrimes } 2711590Srgrimes mtx_unlock(pc->tag_parent->mtx); 2721590Srgrimes } 2731590Srgrimes } 2741590Srgrimes 2751590Srgrimes parm->xfer_page_cache_ptr = pc; 2761590Srgrimes parm->dma_page_ptr = pg; 2771590Srgrimes return (0); 2781590Srgrimes} 2791590Srgrimes#endif 2801590Srgrimes 2811590Srgrimes/*------------------------------------------------------------------------* 2821590Srgrimes * usb2_transfer_setup_sub - transfer setup subroutine 2831590Srgrimes * 2841590Srgrimes * This function must be called from the "xfer_setup" callback of the 2851590Srgrimes * USB Host or Device controller driver when setting up an USB 2861590Srgrimes * transfer. This function will setup correct packet sizes, buffer 2871590Srgrimes * sizes, flags and more, that are stored in the "usb2_xfer" 2881590Srgrimes * structure. 2891590Srgrimes *------------------------------------------------------------------------*/ 2901590Srgrimesvoid 2911590Srgrimesusb2_transfer_setup_sub(struct usb2_setup_params *parm) 2921590Srgrimes{ 2931590Srgrimes enum { 2941590Srgrimes REQ_SIZE = 8, 2951590Srgrimes MIN_PKT = 8, 2961590Srgrimes }; 2971590Srgrimes struct usb2_xfer *xfer = parm->curr_xfer; 2981590Srgrimes const struct usb2_config *setup = parm->curr_setup; 2991590Srgrimes struct usb2_endpoint_descriptor *edesc; 3001590Srgrimes struct usb2_std_packet_size std_size; 3011590Srgrimes usb2_frcount_t n_frlengths; 3021590Srgrimes usb2_frcount_t n_frbuffers; 3031590Srgrimes usb2_frcount_t x; 3041590Srgrimes uint8_t type; 3051590Srgrimes uint8_t zmps; 3061590Srgrimes 3071590Srgrimes /* 3081590Srgrimes * Sanity check. The following parameters must be initialized before 3091590Srgrimes * calling this function. 3101590Srgrimes */ 3111590Srgrimes if ((parm->hc_max_packet_size == 0) || 3121590Srgrimes (parm->hc_max_packet_count == 0) || 3131590Srgrimes (parm->hc_max_frame_size == 0)) { 3141590Srgrimes parm->err = USB_ERR_INVAL; 3151590Srgrimes goto done; 3161590Srgrimes } 3171590Srgrimes edesc = xfer->pipe->edesc; 3181590Srgrimes 3191590Srgrimes type = (edesc->bmAttributes & UE_XFERTYPE); 3201590Srgrimes 3211590Srgrimes xfer->flags = setup->flags; 3221590Srgrimes xfer->nframes = setup->frames; 3231590Srgrimes xfer->timeout = setup->timeout; 3241590Srgrimes xfer->callback = setup->callback; 3251590Srgrimes xfer->interval = setup->interval; 3261590Srgrimes xfer->endpoint = edesc->bEndpointAddress; 3271590Srgrimes xfer->max_packet_size = UGETW(edesc->wMaxPacketSize); 3281590Srgrimes xfer->max_packet_count = 1; 3291590Srgrimes /* make a shadow copy: */ 3301590Srgrimes xfer->flags_int.usb2_mode = parm->udev->flags.usb2_mode; 33128066Scharnier 33228066Scharnier parm->bufsize = setup->bufsize; 3331590Srgrimes 3341590Srgrimes if (parm->speed == USB_SPEED_HIGH) { 3351590Srgrimes xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3; 3361590Srgrimes xfer->max_packet_size &= 0x7FF; 3371590Srgrimes } 3381590Srgrimes /* range check "max_packet_count" */ 3391590Srgrimes 3401590Srgrimes if (xfer->max_packet_count > parm->hc_max_packet_count) { 3411590Srgrimes xfer->max_packet_count = parm->hc_max_packet_count; 3421590Srgrimes } 3431590Srgrimes /* filter "wMaxPacketSize" according to HC capabilities */ 3441590Srgrimes 3451590Srgrimes if ((xfer->max_packet_size > parm->hc_max_packet_size) || 3461590Srgrimes (xfer->max_packet_size == 0)) { 3471590Srgrimes xfer->max_packet_size = parm->hc_max_packet_size; 3481590Srgrimes } 3491590Srgrimes /* filter "wMaxPacketSize" according to standard sizes */ 3501590Srgrimes 3511590Srgrimes usb2_get_std_packet_size(&std_size, type, parm->speed); 3521590Srgrimes 3531590Srgrimes if (std_size.range.min || std_size.range.max) { 3541590Srgrimes 35510075Sjkh if (xfer->max_packet_size < std_size.range.min) { 3561590Srgrimes xfer->max_packet_size = std_size.range.min; 3571590Srgrimes } 3581590Srgrimes if (xfer->max_packet_size > std_size.range.max) { 3591590Srgrimes xfer->max_packet_size = std_size.range.max; 3601590Srgrimes } 3611590Srgrimes } else { 36210075Sjkh 36310075Sjkh if (xfer->max_packet_size >= std_size.fixed[3]) { 3641590Srgrimes xfer->max_packet_size = std_size.fixed[3]; 36510075Sjkh } else if (xfer->max_packet_size >= std_size.fixed[2]) { 36610075Sjkh xfer->max_packet_size = std_size.fixed[2]; 3671590Srgrimes } else if (xfer->max_packet_size >= std_size.fixed[1]) { 3681590Srgrimes xfer->max_packet_size = std_size.fixed[1]; 3691590Srgrimes } else { 3701590Srgrimes /* only one possibility left */ 3711590Srgrimes xfer->max_packet_size = std_size.fixed[0]; 3721590Srgrimes } 3731590Srgrimes } 3741590Srgrimes 3751590Srgrimes /* compute "max_frame_size" */ 3761590Srgrimes 3771590Srgrimes usb2_update_max_frame_size(xfer); 3781590Srgrimes 3791590Srgrimes /* check interrupt interval and transfer pre-delay */ 3801590Srgrimes 3811590Srgrimes if (type == UE_ISOCHRONOUS) { 3821590Srgrimes 3831590Srgrimes uint16_t frame_limit; 3841590Srgrimes 3851590Srgrimes xfer->interval = 0; /* not used, must be zero */ 3861590Srgrimes xfer->flags_int.isochronous_xfr = 1; /* set flag */ 3871590Srgrimes 3881590Srgrimes if (xfer->timeout == 0) { 3891590Srgrimes /* 3901590Srgrimes * set a default timeout in 3911590Srgrimes * case something goes wrong! 3921590Srgrimes */ 3931590Srgrimes xfer->timeout = 1000 / 4; 3941590Srgrimes } 3951590Srgrimes switch (parm->speed) { 3961590Srgrimes case USB_SPEED_LOW: 3971590Srgrimes case USB_SPEED_FULL: 3981590Srgrimes frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER; 3991590Srgrimes break; 4001590Srgrimes default: 4011590Srgrimes frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER; 4021590Srgrimes break; 4031590Srgrimes } 4041590Srgrimes 4051590Srgrimes if (xfer->nframes > frame_limit) { 4061590Srgrimes /* 4071590Srgrimes * this is not going to work 4081590Srgrimes * cross hardware 4091590Srgrimes */ 4101590Srgrimes parm->err = USB_ERR_INVAL; 4111590Srgrimes goto done; 4121590Srgrimes } 4131590Srgrimes if (xfer->nframes == 0) { 4141590Srgrimes /* 41528066Scharnier * this is not a valid value 4161590Srgrimes */ 41728066Scharnier parm->err = USB_ERR_ZERO_NFRAMES; 4181590Srgrimes goto done; 4191590Srgrimes } 4201590Srgrimes } else { 4211590Srgrimes 4221590Srgrimes /* 4231590Srgrimes * if a value is specified use that else check the endpoint 4241590Srgrimes * descriptor 4251590Srgrimes */ 4261590Srgrimes if (xfer->interval == 0) { 4271590Srgrimes 4281590Srgrimes if (type == UE_INTERRUPT) { 4291590Srgrimes 4301590Srgrimes xfer->interval = edesc->bInterval; 4311590Srgrimes 4321590Srgrimes switch (parm->speed) { 4338874Srgrimes case USB_SPEED_SUPER: 4341590Srgrimes case USB_SPEED_VARIABLE: 4351590Srgrimes /* 125us -> 1ms */ 4368874Srgrimes if (xfer->interval < 4) 4371590Srgrimes xfer->interval = 1; 4381590Srgrimes else if (xfer->interval > 16) 4391590Srgrimes xfer->interval = (1<<(16-4)); 4401590Srgrimes else 4411590Srgrimes xfer->interval = 4421590Srgrimes (1 << (xfer->interval-4)); 4431590Srgrimes break; 4441590Srgrimes case USB_SPEED_HIGH: 4451590Srgrimes /* 125us -> 1ms */ 4461590Srgrimes xfer->interval /= 8; 4471590Srgrimes break; 4481590Srgrimes default: 4491590Srgrimes break; 45028066Scharnier } 4511590Srgrimes if (xfer->interval == 0) { 4521590Srgrimes /* 4531590Srgrimes * One millisecond is the smallest 4541590Srgrimes * interval we support: 4551590Srgrimes */ 45628066Scharnier xfer->interval = 1; 4571590Srgrimes } 4581590Srgrimes } 4591590Srgrimes } 4601590Srgrimes } 4611590Srgrimes 4621590Srgrimes /* 4631590Srgrimes * NOTE: we do not allow "max_packet_size" or "max_frame_size" 4641590Srgrimes * to be equal to zero when setting up USB transfers, hence 4651590Srgrimes * this leads to alot of extra code in the USB kernel. 4661590Srgrimes */ 4671590Srgrimes 4681590Srgrimes if ((xfer->max_frame_size == 0) || 46946081Simp (xfer->max_packet_size == 0)) { 47028066Scharnier 4711590Srgrimes zmps = 1; 4721590Srgrimes 4731590Srgrimes if ((parm->bufsize <= MIN_PKT) && 4741590Srgrimes (type != UE_CONTROL) && 4751590Srgrimes (type != UE_BULK)) { 4761590Srgrimes 47746081Simp /* workaround */ 4781590Srgrimes xfer->max_packet_size = MIN_PKT; 4798874Srgrimes xfer->max_packet_count = 1; 4801590Srgrimes parm->bufsize = 0; /* automatic setup length */ 4811590Srgrimes usb2_update_max_frame_size(xfer); 4821590Srgrimes 4831590Srgrimes } else { 48417522Sache parm->err = USB_ERR_ZERO_MAXP; 4851590Srgrimes goto done; 4861590Srgrimes } 4871590Srgrimes 4881590Srgrimes } else { 4891590Srgrimes zmps = 0; 49028066Scharnier } 4911590Srgrimes 4921590Srgrimes /* 4931590Srgrimes * check if we should setup a default 4941590Srgrimes * length: 4951590Srgrimes */ 4961590Srgrimes 4971590Srgrimes if (parm->bufsize == 0) { 4981590Srgrimes 4991590Srgrimes parm->bufsize = xfer->max_frame_size; 5001590Srgrimes 5011590Srgrimes if (type == UE_ISOCHRONOUS) { 50228066Scharnier parm->bufsize *= xfer->nframes; 5031590Srgrimes } 5041590Srgrimes } 5051590Srgrimes /* 5061590Srgrimes * check if we are about to setup a proxy 5071590Srgrimes * type of buffer: 5081590Srgrimes */ 5091590Srgrimes 5101590Srgrimes if (xfer->flags.proxy_buffer) { 5111590Srgrimes 5121590Srgrimes /* round bufsize up */ 5138874Srgrimes 5141590Srgrimes parm->bufsize += (xfer->max_frame_size - 1); 5151590Srgrimes 51628066Scharnier if (parm->bufsize < xfer->max_frame_size) { 5171590Srgrimes /* length wrapped around */ 5181590Srgrimes parm->err = USB_ERR_INVAL; 5191590Srgrimes goto done; 5201590Srgrimes } 5211590Srgrimes /* subtract remainder */ 5221590Srgrimes 5231590Srgrimes parm->bufsize -= (parm->bufsize % xfer->max_frame_size); 5241590Srgrimes 5258874Srgrimes /* add length of USB device request structure, if any */ 5261590Srgrimes 5271590Srgrimes if (type == UE_CONTROL) { 5281590Srgrimes parm->bufsize += REQ_SIZE; /* SETUP message */ 5291590Srgrimes } 5301590Srgrimes } 5311590Srgrimes xfer->max_data_length = parm->bufsize; 5321590Srgrimes 5331590Srgrimes /* Setup "n_frlengths" and "n_frbuffers" */ 53428066Scharnier 5351590Srgrimes if (type == UE_ISOCHRONOUS) { 5361590Srgrimes n_frlengths = xfer->nframes; 5371590Srgrimes n_frbuffers = 1; 5381590Srgrimes } else { 5391590Srgrimes 5401590Srgrimes if (type == UE_CONTROL) { 5411590Srgrimes xfer->flags_int.control_xfr = 1; 5421590Srgrimes if (xfer->nframes == 0) { 5431590Srgrimes if (parm->bufsize <= REQ_SIZE) { 5441590Srgrimes /* 5451590Srgrimes * there will never be any data 5461590Srgrimes * stage 5471590Srgrimes */ 5481590Srgrimes xfer->nframes = 1; 5491590Srgrimes } else { 5501590Srgrimes xfer->nframes = 2; 5511590Srgrimes } 5521590Srgrimes } 55380286Sobrien } else { 55480286Sobrien if (xfer->nframes == 0) { 55580286Sobrien xfer->nframes = 1; 5561590Srgrimes } 5571590Srgrimes } 5581590Srgrimes 5591590Srgrimes n_frlengths = xfer->nframes; 5601590Srgrimes n_frbuffers = xfer->nframes; 5611590Srgrimes } 5621590Srgrimes 56317522Sache /* 5641590Srgrimes * check if we have room for the 5651590Srgrimes * USB device request structure: 5661590Srgrimes */ 5671590Srgrimes 5681590Srgrimes if (type == UE_CONTROL) { 5691590Srgrimes 5701590Srgrimes if (xfer->max_data_length < REQ_SIZE) { 5711590Srgrimes /* length wrapped around or too small bufsize */ 5721590Srgrimes parm->err = USB_ERR_INVAL; 5731590Srgrimes goto done; 5741590Srgrimes } 5751590Srgrimes xfer->max_data_length -= REQ_SIZE; 5761590Srgrimes } 5771590Srgrimes /* setup "frlengths" */ 5781590Srgrimes 5791590Srgrimes xfer->frlengths = parm->xfer_length_ptr; 5801590Srgrimes 5811590Srgrimes parm->xfer_length_ptr += n_frlengths; 5821590Srgrimes 5831590Srgrimes /* setup "frbuffers" */ 5841590Srgrimes 5851590Srgrimes xfer->frbuffers = parm->xfer_page_cache_ptr; 5861590Srgrimes 5871590Srgrimes parm->xfer_page_cache_ptr += n_frbuffers; 5881590Srgrimes 5891590Srgrimes /* 5901590Srgrimes * check if we need to setup 5911590Srgrimes * a local buffer: 5921590Srgrimes */ 5931590Srgrimes 5941590Srgrimes if (!xfer->flags.ext_buffer) { 5951590Srgrimes 5961590Srgrimes /* align data */ 5971590Srgrimes parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 5981590Srgrimes 5991590Srgrimes if (parm->buf) { 6001590Srgrimes 6011590Srgrimes xfer->local_buffer = 6021590Srgrimes USB_ADD_BYTES(parm->buf, parm->size[0]); 60380286Sobrien 60480286Sobrien usb2_set_frame_offset(xfer, 0, 0); 60580286Sobrien 6061590Srgrimes if ((type == UE_CONTROL) && (n_frbuffers > 1)) { 6071590Srgrimes usb2_set_frame_offset(xfer, REQ_SIZE, 1); 6081590Srgrimes } 6091590Srgrimes } 6101590Srgrimes parm->size[0] += parm->bufsize; 6111590Srgrimes 6121590Srgrimes /* align data again */ 6131590Srgrimes parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 6141590Srgrimes } 6151590Srgrimes /* 6161590Srgrimes * Compute maximum buffer size 6171590Srgrimes */ 6181590Srgrimes 6191590Srgrimes if (parm->bufsize_max < parm->bufsize) { 6201590Srgrimes parm->bufsize_max = parm->bufsize; 6211590Srgrimes } 6221590Srgrimes#if USB_HAVE_BUSDMA 6231590Srgrimes if (xfer->flags_int.bdma_enable) { 6241590Srgrimes /* 6251590Srgrimes * Setup "dma_page_ptr". 6261590Srgrimes * 6271590Srgrimes * Proof for formula below: 62828066Scharnier * 6291590Srgrimes * Assume there are three USB frames having length "a", "b" and 6301590Srgrimes * "c". These USB frames will at maximum need "z" 6311590Srgrimes * "usb2_page" structures. "z" is given by: 6321590Srgrimes * 63328066Scharnier * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) + 6341590Srgrimes * ((c / USB_PAGE_SIZE) + 2); 6351590Srgrimes * 6361590Srgrimes * Constraining "a", "b" and "c" like this: 6371590Srgrimes * 6381590Srgrimes * (a + b + c) <= parm->bufsize 6391590Srgrimes * 6401590Srgrimes * We know that: 641 * 642 * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2)); 643 * 644 * Here is the general formula: 645 */ 646 xfer->dma_page_ptr = parm->dma_page_ptr; 647 parm->dma_page_ptr += (2 * n_frbuffers); 648 parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE); 649 } 650#endif 651 if (zmps) { 652 /* correct maximum data length */ 653 xfer->max_data_length = 0; 654 } 655 /* subtract USB frame remainder from "hc_max_frame_size" */ 656 657 xfer->max_hc_frame_size = 658 (parm->hc_max_frame_size - 659 (parm->hc_max_frame_size % xfer->max_frame_size)); 660 661 if (xfer->max_hc_frame_size == 0) { 662 parm->err = USB_ERR_INVAL; 663 goto done; 664 } 665 /* initialize max frame count */ 666 667 xfer->max_frame_count = xfer->nframes; 668 669 /* initialize frame buffers */ 670 671 if (parm->buf) { 672 for (x = 0; x != n_frbuffers; x++) { 673 xfer->frbuffers[x].tag_parent = 674 &xfer->xroot->dma_parent_tag; 675#if USB_HAVE_BUSDMA 676 if (xfer->flags_int.bdma_enable && 677 (parm->bufsize_max > 0)) { 678 679 if (usb2_pc_dmamap_create( 680 xfer->frbuffers + x, 681 parm->bufsize_max)) { 682 parm->err = USB_ERR_NOMEM; 683 goto done; 684 } 685 } 686#endif 687 } 688 } 689done: 690 if (parm->err) { 691 /* 692 * Set some dummy values so that we avoid division by zero: 693 */ 694 xfer->max_hc_frame_size = 1; 695 xfer->max_frame_size = 1; 696 xfer->max_packet_size = 1; 697 xfer->max_data_length = 0; 698 xfer->nframes = 0; 699 xfer->max_frame_count = 0; 700 } 701} 702 703/*------------------------------------------------------------------------* 704 * usb2_transfer_setup - setup an array of USB transfers 705 * 706 * NOTE: You must always call "usb2_transfer_unsetup" after calling 707 * "usb2_transfer_setup" if success was returned. 708 * 709 * The idea is that the USB device driver should pre-allocate all its 710 * transfers by one call to this function. 711 * 712 * Return values: 713 * 0: Success 714 * Else: Failure 715 *------------------------------------------------------------------------*/ 716usb2_error_t 717usb2_transfer_setup(struct usb2_device *udev, 718 const uint8_t *ifaces, struct usb2_xfer **ppxfer, 719 const struct usb2_config *setup_start, uint16_t n_setup, 720 void *priv_sc, struct mtx *xfer_mtx) 721{ 722 struct usb2_xfer dummy; 723 struct usb2_setup_params parm; 724 const struct usb2_config *setup_end = setup_start + n_setup; 725 const struct usb2_config *setup; 726 struct usb2_pipe *pipe; 727 struct usb2_xfer_root *info; 728 struct usb2_xfer *xfer; 729 void *buf = NULL; 730 uint16_t n; 731 uint16_t refcount; 732 733 parm.err = 0; 734 refcount = 0; 735 info = NULL; 736 737 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 738 "usb2_transfer_setup can sleep!"); 739 740 /* do some checking first */ 741 742 if (n_setup == 0) { 743 DPRINTFN(6, "setup array has zero length!\n"); 744 return (USB_ERR_INVAL); 745 } 746 if (ifaces == 0) { 747 DPRINTFN(6, "ifaces array is NULL!\n"); 748 return (USB_ERR_INVAL); 749 } 750 if (xfer_mtx == NULL) { 751 DPRINTFN(6, "using global lock\n"); 752 xfer_mtx = &Giant; 753 } 754 /* sanity checks */ 755 for (setup = setup_start, n = 0; 756 setup != setup_end; setup++, n++) { 757 if (setup->bufsize == (usb2_frlength_t)-1) { 758 parm.err = USB_ERR_BAD_BUFSIZE; 759 DPRINTF("invalid bufsize\n"); 760 } 761 if (setup->callback == NULL) { 762 parm.err = USB_ERR_NO_CALLBACK; 763 DPRINTF("no callback\n"); 764 } 765 ppxfer[n] = NULL; 766 } 767 768 if (parm.err) { 769 goto done; 770 } 771 bzero(&parm, sizeof(parm)); 772 773 parm.udev = udev; 774 parm.speed = usb2_get_speed(udev); 775 parm.hc_max_packet_count = 1; 776 777 if (parm.speed >= USB_SPEED_MAX) { 778 parm.err = USB_ERR_INVAL; 779 goto done; 780 } 781 /* setup all transfers */ 782 783 while (1) { 784 785 if (buf) { 786 /* 787 * Initialize the "usb2_xfer_root" structure, 788 * which is common for all our USB transfers. 789 */ 790 info = USB_ADD_BYTES(buf, 0); 791 792 info->memory_base = buf; 793 info->memory_size = parm.size[0]; 794 795#if USB_HAVE_BUSDMA 796 info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]); 797 info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]); 798#endif 799 info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]); 800 info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]); 801 802 usb2_cv_init(&info->cv_drain, "WDRAIN"); 803 804 info->xfer_mtx = xfer_mtx; 805#if USB_HAVE_BUSDMA 806 usb2_dma_tag_setup(&info->dma_parent_tag, 807 parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag, 808 xfer_mtx, &usb2_bdma_done_event, 32, parm.dma_tag_max); 809#endif 810 811 info->bus = udev->bus; 812 info->udev = udev; 813 814 TAILQ_INIT(&info->done_q.head); 815 info->done_q.command = &usb2_callback_wrapper; 816#if USB_HAVE_BUSDMA 817 TAILQ_INIT(&info->dma_q.head); 818 info->dma_q.command = &usb2_bdma_work_loop; 819#endif 820 info->done_m[0].hdr.pm_callback = &usb2_callback_proc; 821 info->done_m[0].xroot = info; 822 info->done_m[1].hdr.pm_callback = &usb2_callback_proc; 823 info->done_m[1].xroot = info; 824 825 /* 826 * In device side mode control endpoint 827 * requests need to run from a separate 828 * context, else there is a chance of 829 * deadlock! 830 */ 831 if (setup_start == usb2_control_ep_cfg) 832 info->done_p = 833 &udev->bus->control_xfer_proc; 834 else if (xfer_mtx == &Giant) 835 info->done_p = 836 &udev->bus->giant_callback_proc; 837 else 838 info->done_p = 839 &udev->bus->non_giant_callback_proc; 840 } 841 /* reset sizes */ 842 843 parm.size[0] = 0; 844 parm.buf = buf; 845 parm.size[0] += sizeof(info[0]); 846 847 for (setup = setup_start, n = 0; 848 setup != setup_end; setup++, n++) { 849 850 /* skip USB transfers without callbacks: */ 851 if (setup->callback == NULL) { 852 continue; 853 } 854 /* see if there is a matching endpoint */ 855 pipe = usb2_get_pipe(udev, 856 ifaces[setup->if_index], setup); 857 858 if ((pipe == NULL) || (pipe->methods == NULL)) { 859 if (setup->flags.no_pipe_ok) 860 continue; 861 if ((setup->usb_mode != USB_MODE_MAX) && 862 (setup->usb_mode != udev->flags.usb2_mode)) 863 continue; 864 parm.err = USB_ERR_NO_PIPE; 865 goto done; 866 } 867 868 /* align data properly */ 869 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 870 871 /* store current setup pointer */ 872 parm.curr_setup = setup; 873 874 if (buf) { 875 /* 876 * Common initialization of the 877 * "usb2_xfer" structure. 878 */ 879 xfer = USB_ADD_BYTES(buf, parm.size[0]); 880 xfer->address = udev->address; 881 xfer->priv_sc = priv_sc; 882 xfer->xroot = info; 883 884 usb2_callout_init_mtx(&xfer->timeout_handle, 885 &udev->bus->bus_mtx, 0); 886 } else { 887 /* 888 * Setup a dummy xfer, hence we are 889 * writing to the "usb2_xfer" 890 * structure pointed to by "xfer" 891 * before we have allocated any 892 * memory: 893 */ 894 xfer = &dummy; 895 bzero(&dummy, sizeof(dummy)); 896 refcount++; 897 } 898 899 /* set transfer pipe pointer */ 900 xfer->pipe = pipe; 901 902 parm.size[0] += sizeof(xfer[0]); 903 parm.methods = xfer->pipe->methods; 904 parm.curr_xfer = xfer; 905 906 /* 907 * Call the Host or Device controller transfer 908 * setup routine: 909 */ 910 (udev->bus->methods->xfer_setup) (&parm); 911 912 /* check for error */ 913 if (parm.err) 914 goto done; 915 916 if (buf) { 917 /* 918 * Increment the pipe refcount. This 919 * basically prevents setting a new 920 * configuration and alternate setting 921 * when USB transfers are in use on 922 * the given interface. Search the USB 923 * code for "pipe->refcount" if you 924 * want more information. 925 */ 926 xfer->pipe->refcount++; 927 928 /* 929 * Whenever we set ppxfer[] then we 930 * also need to increment the 931 * "setup_refcount": 932 */ 933 info->setup_refcount++; 934 935 /* 936 * Transfer is successfully setup and 937 * can be used: 938 */ 939 ppxfer[n] = xfer; 940 } 941 } 942 943 if (buf || parm.err) { 944 goto done; 945 } 946 if (refcount == 0) { 947 /* no transfers - nothing to do ! */ 948 goto done; 949 } 950 /* align data properly */ 951 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 952 953 /* store offset temporarily */ 954 parm.size[1] = parm.size[0]; 955 956 /* 957 * The number of DMA tags required depends on 958 * the number of endpoints. The current estimate 959 * for maximum number of DMA tags per endpoint 960 * is two. 961 */ 962 parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX); 963 964 /* 965 * DMA tags for QH, TD, Data and more. 966 */ 967 parm.dma_tag_max += 8; 968 969 parm.dma_tag_p += parm.dma_tag_max; 970 971 parm.size[0] += ((uint8_t *)parm.dma_tag_p) - 972 ((uint8_t *)0); 973 974 /* align data properly */ 975 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 976 977 /* store offset temporarily */ 978 parm.size[3] = parm.size[0]; 979 980 parm.size[0] += ((uint8_t *)parm.dma_page_ptr) - 981 ((uint8_t *)0); 982 983 /* align data properly */ 984 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 985 986 /* store offset temporarily */ 987 parm.size[4] = parm.size[0]; 988 989 parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) - 990 ((uint8_t *)0); 991 992 /* store end offset temporarily */ 993 parm.size[5] = parm.size[0]; 994 995 parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) - 996 ((uint8_t *)0); 997 998 /* store end offset temporarily */ 999 1000 parm.size[2] = parm.size[0]; 1001 1002 /* align data properly */ 1003 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1004 1005 parm.size[6] = parm.size[0]; 1006 1007 parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) - 1008 ((uint8_t *)0); 1009 1010 /* align data properly */ 1011 parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1012 1013 /* allocate zeroed memory */ 1014 buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO); 1015 1016 if (buf == NULL) { 1017 parm.err = USB_ERR_NOMEM; 1018 DPRINTFN(0, "cannot allocate memory block for " 1019 "configuration (%d bytes)\n", 1020 parm.size[0]); 1021 goto done; 1022 } 1023 parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]); 1024 parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]); 1025 parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]); 1026 parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]); 1027 parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]); 1028 } 1029 1030done: 1031 if (buf) { 1032 if (info->setup_refcount == 0) { 1033 /* 1034 * "usb2_transfer_unsetup_sub" will unlock 1035 * the bus mutex before returning ! 1036 */ 1037 USB_BUS_LOCK(info->bus); 1038 1039 /* something went wrong */ 1040 usb2_transfer_unsetup_sub(info, 0); 1041 } 1042 } 1043 if (parm.err) { 1044 usb2_transfer_unsetup(ppxfer, n_setup); 1045 } 1046 return (parm.err); 1047} 1048 1049/*------------------------------------------------------------------------* 1050 * usb2_transfer_unsetup_sub - factored out code 1051 *------------------------------------------------------------------------*/ 1052static void 1053usb2_transfer_unsetup_sub(struct usb2_xfer_root *info, uint8_t needs_delay) 1054{ 1055 struct usb2_page_cache *pc; 1056 1057 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 1058 1059 /* wait for any outstanding DMA operations */ 1060 1061 if (needs_delay) { 1062 usb2_timeout_t temp; 1063 temp = usb2_get_dma_delay(info->bus); 1064 usb2_pause_mtx(&info->bus->bus_mtx, 1065 USB_MS_TO_TICKS(temp)); 1066 } 1067 1068 /* make sure that our done messages are not queued anywhere */ 1069 usb2_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); 1070 1071 USB_BUS_UNLOCK(info->bus); 1072 1073#if USB_HAVE_BUSDMA 1074 /* free DMA'able memory, if any */ 1075 pc = info->dma_page_cache_start; 1076 while (pc != info->dma_page_cache_end) { 1077 usb2_pc_free_mem(pc); 1078 pc++; 1079 } 1080 1081 /* free DMA maps in all "xfer->frbuffers" */ 1082 pc = info->xfer_page_cache_start; 1083 while (pc != info->xfer_page_cache_end) { 1084 usb2_pc_dmamap_destroy(pc); 1085 pc++; 1086 } 1087 1088 /* free all DMA tags */ 1089 usb2_dma_tag_unsetup(&info->dma_parent_tag); 1090#endif 1091 1092 usb2_cv_destroy(&info->cv_drain); 1093 1094 /* 1095 * free the "memory_base" last, hence the "info" structure is 1096 * contained within the "memory_base"! 1097 */ 1098 free(info->memory_base, M_USB); 1099} 1100 1101/*------------------------------------------------------------------------* 1102 * usb2_transfer_unsetup - unsetup/free an array of USB transfers 1103 * 1104 * NOTE: All USB transfers in progress will get called back passing 1105 * the error code "USB_ERR_CANCELLED" before this function 1106 * returns. 1107 *------------------------------------------------------------------------*/ 1108void 1109usb2_transfer_unsetup(struct usb2_xfer **pxfer, uint16_t n_setup) 1110{ 1111 struct usb2_xfer *xfer; 1112 struct usb2_xfer_root *info; 1113 uint8_t needs_delay = 0; 1114 1115 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1116 "usb2_transfer_unsetup can sleep!"); 1117 1118 while (n_setup--) { 1119 xfer = pxfer[n_setup]; 1120 1121 if (xfer == NULL) 1122 continue; 1123 1124 info = xfer->xroot; 1125 1126 USB_XFER_LOCK(xfer); 1127 USB_BUS_LOCK(info->bus); 1128 1129 /* 1130 * HINT: when you start/stop a transfer, it might be a 1131 * good idea to directly use the "pxfer[]" structure: 1132 * 1133 * usb2_transfer_start(sc->pxfer[0]); 1134 * usb2_transfer_stop(sc->pxfer[0]); 1135 * 1136 * That way, if your code has many parts that will not 1137 * stop running under the same lock, in other words 1138 * "xfer_mtx", the usb2_transfer_start and 1139 * usb2_transfer_stop functions will simply return 1140 * when they detect a NULL pointer argument. 1141 * 1142 * To avoid any races we clear the "pxfer[]" pointer 1143 * while holding the private mutex of the driver: 1144 */ 1145 pxfer[n_setup] = NULL; 1146 1147 USB_BUS_UNLOCK(info->bus); 1148 USB_XFER_UNLOCK(xfer); 1149 1150 usb2_transfer_drain(xfer); 1151 1152#if USB_HAVE_BUSDMA 1153 if (xfer->flags_int.bdma_enable) 1154 needs_delay = 1; 1155#endif 1156 /* 1157 * NOTE: default pipe does not have an 1158 * interface, even if pipe->iface_index == 0 1159 */ 1160 xfer->pipe->refcount--; 1161 1162 usb2_callout_drain(&xfer->timeout_handle); 1163 1164 USB_BUS_LOCK(info->bus); 1165 1166 USB_ASSERT(info->setup_refcount != 0, ("Invalid setup " 1167 "reference count!\n")); 1168 1169 info->setup_refcount--; 1170 1171 if (info->setup_refcount == 0) { 1172 usb2_transfer_unsetup_sub(info, 1173 needs_delay); 1174 } else { 1175 USB_BUS_UNLOCK(info->bus); 1176 } 1177 } 1178} 1179 1180/*------------------------------------------------------------------------* 1181 * usb2_control_transfer_init - factored out code 1182 * 1183 * In USB Device Mode we have to wait for the SETUP packet which 1184 * containst the "struct usb2_device_request" structure, before we can 1185 * transfer any data. In USB Host Mode we already have the SETUP 1186 * packet at the moment the USB transfer is started. This leads us to 1187 * having to setup the USB transfer at two different places in 1188 * time. This function just contains factored out control transfer 1189 * initialisation code, so that we don't duplicate the code. 1190 *------------------------------------------------------------------------*/ 1191static void 1192usb2_control_transfer_init(struct usb2_xfer *xfer) 1193{ 1194 struct usb2_device_request req; 1195 1196 /* copy out the USB request header */ 1197 1198 usb2_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); 1199 1200 /* setup remainder */ 1201 1202 xfer->flags_int.control_rem = UGETW(req.wLength); 1203 1204 /* copy direction to endpoint variable */ 1205 1206 xfer->endpoint &= ~(UE_DIR_IN | UE_DIR_OUT); 1207 xfer->endpoint |= 1208 (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT; 1209} 1210 1211/*------------------------------------------------------------------------* 1212 * usb2_start_hardware_sub 1213 * 1214 * This function handles initialisation of control transfers. Control 1215 * transfers are special in that regard that they can both transmit 1216 * and receive data. 1217 * 1218 * Return values: 1219 * 0: Success 1220 * Else: Failure 1221 *------------------------------------------------------------------------*/ 1222static uint8_t 1223usb2_start_hardware_sub(struct usb2_xfer *xfer) 1224{ 1225 usb2_frlength_t len; 1226 1227 /* Check for control endpoint stall */ 1228 if (xfer->flags.stall_pipe) { 1229 /* no longer active */ 1230 xfer->flags_int.control_act = 0; 1231 } 1232 1233 /* Check for invalid number of frames */ 1234 if (xfer->nframes > 2) { 1235 /* 1236 * If you need to split a control transfer, you 1237 * have to do one part at a time. Only with 1238 * non-control transfers you can do multiple 1239 * parts a time. 1240 */ 1241 DPRINTFN(0, "Too many frames: %u\n", 1242 (unsigned int)xfer->nframes); 1243 goto error; 1244 } 1245 1246 /* 1247 * Check if there is a control 1248 * transfer in progress: 1249 */ 1250 if (xfer->flags_int.control_act) { 1251 1252 if (xfer->flags_int.control_hdr) { 1253 1254 /* clear send header flag */ 1255 1256 xfer->flags_int.control_hdr = 0; 1257 1258 /* setup control transfer */ 1259 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1260 usb2_control_transfer_init(xfer); 1261 } 1262 } 1263 /* get data length */ 1264 1265 len = xfer->sumlen; 1266 1267 } else { 1268 1269 /* the size of the SETUP structure is hardcoded ! */ 1270 1271 if (xfer->frlengths[0] != sizeof(struct usb2_device_request)) { 1272 DPRINTFN(0, "Wrong framelength %u != %zu\n", 1273 xfer->frlengths[0], sizeof(struct 1274 usb2_device_request)); 1275 goto error; 1276 } 1277 /* check USB mode */ 1278 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) { 1279 1280 /* check number of frames */ 1281 if (xfer->nframes != 1) { 1282 /* 1283 * We need to receive the setup 1284 * message first so that we know the 1285 * data direction! 1286 */ 1287 DPRINTF("Misconfigured transfer\n"); 1288 goto error; 1289 } 1290 /* 1291 * Set a dummy "control_rem" value. This 1292 * variable will be overwritten later by a 1293 * call to "usb2_control_transfer_init()" ! 1294 */ 1295 xfer->flags_int.control_rem = 0xFFFF; 1296 } else { 1297 1298 /* setup "endpoint" and "control_rem" */ 1299 1300 usb2_control_transfer_init(xfer); 1301 } 1302 1303 /* set transfer-header flag */ 1304 1305 xfer->flags_int.control_hdr = 1; 1306 1307 /* get data length */ 1308 1309 len = (xfer->sumlen - sizeof(struct usb2_device_request)); 1310 } 1311 1312 /* check if there is a length mismatch */ 1313 1314 if (len > xfer->flags_int.control_rem) { 1315 DPRINTFN(0, "Length greater than remaining length!\n"); 1316 goto error; 1317 } 1318 /* check if we are doing a short transfer */ 1319 1320 if (xfer->flags.force_short_xfer) { 1321 xfer->flags_int.control_rem = 0; 1322 } else { 1323 if ((len != xfer->max_data_length) && 1324 (len != xfer->flags_int.control_rem) && 1325 (xfer->nframes != 1)) { 1326 DPRINTFN(0, "Short control transfer without " 1327 "force_short_xfer set!\n"); 1328 goto error; 1329 } 1330 xfer->flags_int.control_rem -= len; 1331 } 1332 1333 /* the status part is executed when "control_act" is 0 */ 1334 1335 if ((xfer->flags_int.control_rem > 0) || 1336 (xfer->flags.manual_status)) { 1337 /* don't execute the STATUS stage yet */ 1338 xfer->flags_int.control_act = 1; 1339 1340 /* sanity check */ 1341 if ((!xfer->flags_int.control_hdr) && 1342 (xfer->nframes == 1)) { 1343 /* 1344 * This is not a valid operation! 1345 */ 1346 DPRINTFN(0, "Invalid parameter " 1347 "combination\n"); 1348 goto error; 1349 } 1350 } else { 1351 /* time to execute the STATUS stage */ 1352 xfer->flags_int.control_act = 0; 1353 } 1354 return (0); /* success */ 1355 1356error: 1357 return (1); /* failure */ 1358} 1359 1360/*------------------------------------------------------------------------* 1361 * usb2_start_hardware - start USB hardware for the given transfer 1362 * 1363 * This function should only be called from the USB callback. 1364 *------------------------------------------------------------------------*/ 1365void 1366usb2_start_hardware(struct usb2_xfer *xfer) 1367{ 1368 struct usb2_xfer_root *info; 1369 struct usb2_bus *bus; 1370 usb2_frcount_t x; 1371 1372 info = xfer->xroot; 1373 bus = info->bus; 1374 1375 DPRINTF("xfer=%p, pipe=%p, nframes=%d, dir=%s\n", 1376 xfer, xfer->pipe, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? 1377 "read" : "write"); 1378 1379#if USB_DEBUG 1380 if (USB_DEBUG_VAR > 0) { 1381 USB_BUS_LOCK(bus); 1382 1383 usb2_dump_pipe(xfer->pipe); 1384 1385 USB_BUS_UNLOCK(bus); 1386 } 1387#endif 1388 1389 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1390 USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED); 1391 1392 /* Only open the USB transfer once! */ 1393 if (!xfer->flags_int.open) { 1394 xfer->flags_int.open = 1; 1395 1396 DPRINTF("open\n"); 1397 1398 USB_BUS_LOCK(bus); 1399 (xfer->pipe->methods->open) (xfer); 1400 USB_BUS_UNLOCK(bus); 1401 } 1402 /* set "transferring" flag */ 1403 xfer->flags_int.transferring = 1; 1404 1405#if USB_HAVE_POWERD 1406 /* increment power reference */ 1407 usb2_transfer_power_ref(xfer, 1); 1408#endif 1409 /* 1410 * Check if the transfer is waiting on a queue, most 1411 * frequently the "done_q": 1412 */ 1413 if (xfer->wait_queue) { 1414 USB_BUS_LOCK(bus); 1415 usb2_transfer_dequeue(xfer); 1416 USB_BUS_UNLOCK(bus); 1417 } 1418 /* clear "did_dma_delay" flag */ 1419 xfer->flags_int.did_dma_delay = 0; 1420 1421 /* clear "did_close" flag */ 1422 xfer->flags_int.did_close = 0; 1423 1424#if USB_HAVE_BUSDMA 1425 /* clear "bdma_setup" flag */ 1426 xfer->flags_int.bdma_setup = 0; 1427#endif 1428 /* by default we cannot cancel any USB transfer immediately */ 1429 xfer->flags_int.can_cancel_immed = 0; 1430 1431 /* clear lengths and frame counts by default */ 1432 xfer->sumlen = 0; 1433 xfer->actlen = 0; 1434 xfer->aframes = 0; 1435 1436 /* clear any previous errors */ 1437 xfer->error = 0; 1438 1439 /* Check if the device is still alive */ 1440 if (info->udev->state < USB_STATE_POWERED) { 1441 USB_BUS_LOCK(bus); 1442 usb2_transfer_done(xfer, USB_ERR_NOT_CONFIGURED); 1443 USB_BUS_UNLOCK(bus); 1444 return; 1445 } 1446 1447 /* sanity check */ 1448 if (xfer->nframes == 0) { 1449 if (xfer->flags.stall_pipe) { 1450 /* 1451 * Special case - want to stall without transferring 1452 * any data: 1453 */ 1454 DPRINTF("xfer=%p nframes=0: stall " 1455 "or clear stall!\n", xfer); 1456 USB_BUS_LOCK(bus); 1457 xfer->flags_int.can_cancel_immed = 1; 1458 /* start the transfer */ 1459 usb2_command_wrapper(&xfer->pipe->pipe_q, xfer); 1460 USB_BUS_UNLOCK(bus); 1461 return; 1462 } 1463 USB_BUS_LOCK(bus); 1464 usb2_transfer_done(xfer, USB_ERR_INVAL); 1465 USB_BUS_UNLOCK(bus); 1466 return; 1467 } 1468 /* compute total transfer length */ 1469 1470 for (x = 0; x != xfer->nframes; x++) { 1471 xfer->sumlen += xfer->frlengths[x]; 1472 if (xfer->sumlen < xfer->frlengths[x]) { 1473 /* length wrapped around */ 1474 USB_BUS_LOCK(bus); 1475 usb2_transfer_done(xfer, USB_ERR_INVAL); 1476 USB_BUS_UNLOCK(bus); 1477 return; 1478 } 1479 } 1480 1481 /* clear some internal flags */ 1482 1483 xfer->flags_int.short_xfer_ok = 0; 1484 xfer->flags_int.short_frames_ok = 0; 1485 1486 /* check if this is a control transfer */ 1487 1488 if (xfer->flags_int.control_xfr) { 1489 1490 if (usb2_start_hardware_sub(xfer)) { 1491 USB_BUS_LOCK(bus); 1492 usb2_transfer_done(xfer, USB_ERR_STALLED); 1493 USB_BUS_UNLOCK(bus); 1494 return; 1495 } 1496 } 1497 /* 1498 * Setup filtered version of some transfer flags, 1499 * in case of data read direction 1500 */ 1501 if (USB_GET_DATA_ISREAD(xfer)) { 1502 1503 if (xfer->flags.short_frames_ok) { 1504 xfer->flags_int.short_xfer_ok = 1; 1505 xfer->flags_int.short_frames_ok = 1; 1506 } else if (xfer->flags.short_xfer_ok) { 1507 xfer->flags_int.short_xfer_ok = 1; 1508 1509 /* check for control transfer */ 1510 if (xfer->flags_int.control_xfr) { 1511 /* 1512 * 1) Control transfers do not support 1513 * reception of multiple short USB 1514 * frames in host mode and device side 1515 * mode, with exception of: 1516 * 1517 * 2) Due to sometimes buggy device 1518 * side firmware we need to do a 1519 * STATUS stage in case of short 1520 * control transfers in USB host mode. 1521 * The STATUS stage then becomes the 1522 * "alt_next" to the DATA stage. 1523 */ 1524 xfer->flags_int.short_frames_ok = 1; 1525 } 1526 } 1527 } 1528 /* 1529 * Check if BUS-DMA support is enabled and try to load virtual 1530 * buffers into DMA, if any: 1531 */ 1532#if USB_HAVE_BUSDMA 1533 if (xfer->flags_int.bdma_enable) { 1534 /* insert the USB transfer last in the BUS-DMA queue */ 1535 usb2_command_wrapper(&xfer->xroot->dma_q, xfer); 1536 return; 1537 } 1538#endif 1539 /* 1540 * Enter the USB transfer into the Host Controller or 1541 * Device Controller schedule: 1542 */ 1543 usb2_pipe_enter(xfer); 1544} 1545 1546/*------------------------------------------------------------------------* 1547 * usb2_pipe_enter - factored out code 1548 *------------------------------------------------------------------------*/ 1549void 1550usb2_pipe_enter(struct usb2_xfer *xfer) 1551{ 1552 struct usb2_pipe *pipe; 1553 1554 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1555 1556 USB_BUS_LOCK(xfer->xroot->bus); 1557 1558 pipe = xfer->pipe; 1559 1560 DPRINTF("enter\n"); 1561 1562 /* enter the transfer */ 1563 (pipe->methods->enter) (xfer); 1564 1565 xfer->flags_int.can_cancel_immed = 1; 1566 1567 /* check for transfer error */ 1568 if (xfer->error) { 1569 /* some error has happened */ 1570 usb2_transfer_done(xfer, 0); 1571 USB_BUS_UNLOCK(xfer->xroot->bus); 1572 return; 1573 } 1574 1575 /* start the transfer */ 1576 usb2_command_wrapper(&pipe->pipe_q, xfer); 1577 USB_BUS_UNLOCK(xfer->xroot->bus); 1578} 1579 1580/*------------------------------------------------------------------------* 1581 * usb2_transfer_start - start an USB transfer 1582 * 1583 * NOTE: Calling this function more than one time will only 1584 * result in a single transfer start, until the USB transfer 1585 * completes. 1586 *------------------------------------------------------------------------*/ 1587void 1588usb2_transfer_start(struct usb2_xfer *xfer) 1589{ 1590 if (xfer == NULL) { 1591 /* transfer is gone */ 1592 return; 1593 } 1594 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1595 1596 /* mark the USB transfer started */ 1597 1598 if (!xfer->flags_int.started) { 1599 xfer->flags_int.started = 1; 1600 } 1601 /* check if the USB transfer callback is already transferring */ 1602 1603 if (xfer->flags_int.transferring) { 1604 return; 1605 } 1606 USB_BUS_LOCK(xfer->xroot->bus); 1607 /* call the USB transfer callback */ 1608 usb2_callback_ss_done_defer(xfer); 1609 USB_BUS_UNLOCK(xfer->xroot->bus); 1610} 1611 1612/*------------------------------------------------------------------------* 1613 * usb2_transfer_stop - stop an USB transfer 1614 * 1615 * NOTE: Calling this function more than one time will only 1616 * result in a single transfer stop. 1617 * NOTE: When this function returns it is not safe to free nor 1618 * reuse any DMA buffers. See "usb2_transfer_drain()". 1619 *------------------------------------------------------------------------*/ 1620void 1621usb2_transfer_stop(struct usb2_xfer *xfer) 1622{ 1623 struct usb2_pipe *pipe; 1624 1625 if (xfer == NULL) { 1626 /* transfer is gone */ 1627 return; 1628 } 1629 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1630 1631 /* check if the USB transfer was ever opened */ 1632 1633 if (!xfer->flags_int.open) { 1634 /* nothing to do except clearing the "started" flag */ 1635 xfer->flags_int.started = 0; 1636 return; 1637 } 1638 /* try to stop the current USB transfer */ 1639 1640 USB_BUS_LOCK(xfer->xroot->bus); 1641 xfer->error = USB_ERR_CANCELLED;/* override any previous error */ 1642 /* 1643 * Clear "open" and "started" when both private and USB lock 1644 * is locked so that we don't get a race updating "flags_int" 1645 */ 1646 xfer->flags_int.open = 0; 1647 xfer->flags_int.started = 0; 1648 1649 /* 1650 * Check if we can cancel the USB transfer immediately. 1651 */ 1652 if (xfer->flags_int.transferring) { 1653 if (xfer->flags_int.can_cancel_immed && 1654 (!xfer->flags_int.did_close)) { 1655 DPRINTF("close\n"); 1656 /* 1657 * The following will lead to an USB_ERR_CANCELLED 1658 * error code being passed to the USB callback. 1659 */ 1660 (xfer->pipe->methods->close) (xfer); 1661 /* only close once */ 1662 xfer->flags_int.did_close = 1; 1663 } else { 1664 /* need to wait for the next done callback */ 1665 } 1666 } else { 1667 DPRINTF("close\n"); 1668 1669 /* close here and now */ 1670 (xfer->pipe->methods->close) (xfer); 1671 1672 /* 1673 * Any additional DMA delay is done by 1674 * "usb2_transfer_unsetup()". 1675 */ 1676 1677 /* 1678 * Special case. Check if we need to restart a blocked 1679 * pipe. 1680 */ 1681 pipe = xfer->pipe; 1682 1683 /* 1684 * If the current USB transfer is completing we need 1685 * to start the next one: 1686 */ 1687 if (pipe->pipe_q.curr == xfer) { 1688 usb2_command_wrapper(&pipe->pipe_q, NULL); 1689 } 1690 } 1691 1692 USB_BUS_UNLOCK(xfer->xroot->bus); 1693} 1694 1695/*------------------------------------------------------------------------* 1696 * usb2_transfer_pending 1697 * 1698 * This function will check if an USB transfer is pending which is a 1699 * little bit complicated! 1700 * Return values: 1701 * 0: Not pending 1702 * 1: Pending: The USB transfer will receive a callback in the future. 1703 *------------------------------------------------------------------------*/ 1704uint8_t 1705usb2_transfer_pending(struct usb2_xfer *xfer) 1706{ 1707 struct usb2_xfer_root *info; 1708 struct usb2_xfer_queue *pq; 1709 1710 if (xfer == NULL) { 1711 /* transfer is gone */ 1712 return (0); 1713 } 1714 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1715 1716 if (xfer->flags_int.transferring) { 1717 /* trivial case */ 1718 return (1); 1719 } 1720 USB_BUS_LOCK(xfer->xroot->bus); 1721 if (xfer->wait_queue) { 1722 /* we are waiting on a queue somewhere */ 1723 USB_BUS_UNLOCK(xfer->xroot->bus); 1724 return (1); 1725 } 1726 info = xfer->xroot; 1727 pq = &info->done_q; 1728 1729 if (pq->curr == xfer) { 1730 /* we are currently scheduled for callback */ 1731 USB_BUS_UNLOCK(xfer->xroot->bus); 1732 return (1); 1733 } 1734 /* we are not pending */ 1735 USB_BUS_UNLOCK(xfer->xroot->bus); 1736 return (0); 1737} 1738 1739/*------------------------------------------------------------------------* 1740 * usb2_transfer_drain 1741 * 1742 * This function will stop the USB transfer and wait for any 1743 * additional BUS-DMA and HW-DMA operations to complete. Buffers that 1744 * are loaded into DMA can safely be freed or reused after that this 1745 * function has returned. 1746 *------------------------------------------------------------------------*/ 1747void 1748usb2_transfer_drain(struct usb2_xfer *xfer) 1749{ 1750 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1751 "usb2_transfer_drain can sleep!"); 1752 1753 if (xfer == NULL) { 1754 /* transfer is gone */ 1755 return; 1756 } 1757 if (xfer->xroot->xfer_mtx != &Giant) { 1758 USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED); 1759 } 1760 USB_XFER_LOCK(xfer); 1761 1762 usb2_transfer_stop(xfer); 1763 1764 while (usb2_transfer_pending(xfer)) { 1765 xfer->flags_int.draining = 1; 1766 /* 1767 * Wait until the current outstanding USB 1768 * transfer is complete ! 1769 */ 1770 usb2_cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); 1771 } 1772 USB_XFER_UNLOCK(xfer); 1773} 1774 1775/*------------------------------------------------------------------------* 1776 * usb2_set_frame_data 1777 * 1778 * This function sets the pointer of the buffer that should 1779 * loaded directly into DMA for the given USB frame. Passing "ptr" 1780 * equal to NULL while the corresponding "frlength" is greater 1781 * than zero gives undefined results! 1782 *------------------------------------------------------------------------*/ 1783void 1784usb2_set_frame_data(struct usb2_xfer *xfer, void *ptr, usb2_frcount_t frindex) 1785{ 1786 /* set virtual address to load and length */ 1787 xfer->frbuffers[frindex].buffer = ptr; 1788} 1789 1790/*------------------------------------------------------------------------* 1791 * usb2_set_frame_offset 1792 * 1793 * This function sets the frame data buffer offset relative to the beginning 1794 * of the USB DMA buffer allocated for this USB transfer. 1795 *------------------------------------------------------------------------*/ 1796void 1797usb2_set_frame_offset(struct usb2_xfer *xfer, usb2_frlength_t offset, 1798 usb2_frcount_t frindex) 1799{ 1800 USB_ASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " 1801 "when the USB buffer is external!\n")); 1802 1803 /* set virtual address to load */ 1804 xfer->frbuffers[frindex].buffer = 1805 USB_ADD_BYTES(xfer->local_buffer, offset); 1806} 1807 1808/*------------------------------------------------------------------------* 1809 * usb2_callback_proc - factored out code 1810 * 1811 * This function performs USB callbacks. 1812 *------------------------------------------------------------------------*/ 1813static void 1814usb2_callback_proc(struct usb2_proc_msg *_pm) 1815{ 1816 struct usb2_done_msg *pm = (void *)_pm; 1817 struct usb2_xfer_root *info = pm->xroot; 1818 1819 /* Change locking order */ 1820 USB_BUS_UNLOCK(info->bus); 1821 1822 /* 1823 * We exploit the fact that the mutex is the same for all 1824 * callbacks that will be called from this thread: 1825 */ 1826 mtx_lock(info->xfer_mtx); 1827 USB_BUS_LOCK(info->bus); 1828 1829 /* Continue where we lost track */ 1830 usb2_command_wrapper(&info->done_q, 1831 info->done_q.curr); 1832 1833 mtx_unlock(info->xfer_mtx); 1834} 1835 1836/*------------------------------------------------------------------------* 1837 * usb2_callback_ss_done_defer 1838 * 1839 * This function will defer the start, stop and done callback to the 1840 * correct thread. 1841 *------------------------------------------------------------------------*/ 1842static void 1843usb2_callback_ss_done_defer(struct usb2_xfer *xfer) 1844{ 1845 struct usb2_xfer_root *info = xfer->xroot; 1846 struct usb2_xfer_queue *pq = &info->done_q; 1847 1848 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1849 1850 if (pq->curr != xfer) { 1851 usb2_transfer_enqueue(pq, xfer); 1852 } 1853 if (!pq->recurse_1) { 1854 1855 /* 1856 * We have to postpone the callback due to the fact we 1857 * will have a Lock Order Reversal, LOR, if we try to 1858 * proceed ! 1859 */ 1860 if (usb2_proc_msignal(info->done_p, 1861 &info->done_m[0], &info->done_m[1])) { 1862 /* ignore */ 1863 } 1864 } else { 1865 /* clear second recurse flag */ 1866 pq->recurse_2 = 0; 1867 } 1868 return; 1869 1870} 1871 1872/*------------------------------------------------------------------------* 1873 * usb2_callback_wrapper 1874 * 1875 * This is a wrapper for USB callbacks. This wrapper does some 1876 * auto-magic things like figuring out if we can call the callback 1877 * directly from the current context or if we need to wakeup the 1878 * interrupt process. 1879 *------------------------------------------------------------------------*/ 1880static void 1881usb2_callback_wrapper(struct usb2_xfer_queue *pq) 1882{ 1883 struct usb2_xfer *xfer = pq->curr; 1884 struct usb2_xfer_root *info = xfer->xroot; 1885 1886 USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 1887 if (!mtx_owned(info->xfer_mtx)) { 1888 /* 1889 * Cases that end up here: 1890 * 1891 * 5) HW interrupt done callback or other source. 1892 */ 1893 DPRINTFN(3, "case 5\n"); 1894 1895 /* 1896 * We have to postpone the callback due to the fact we 1897 * will have a Lock Order Reversal, LOR, if we try to 1898 * proceed ! 1899 */ 1900 if (usb2_proc_msignal(info->done_p, 1901 &info->done_m[0], &info->done_m[1])) { 1902 /* ignore */ 1903 } 1904 return; 1905 } 1906 /* 1907 * Cases that end up here: 1908 * 1909 * 1) We are starting a transfer 1910 * 2) We are prematurely calling back a transfer 1911 * 3) We are stopping a transfer 1912 * 4) We are doing an ordinary callback 1913 */ 1914 DPRINTFN(3, "case 1-4\n"); 1915 /* get next USB transfer in the queue */ 1916 info->done_q.curr = NULL; 1917 1918 USB_BUS_UNLOCK(info->bus); 1919 USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED); 1920 1921 /* set correct USB state for callback */ 1922 if (!xfer->flags_int.transferring) { 1923 xfer->usb2_state = USB_ST_SETUP; 1924 if (!xfer->flags_int.started) { 1925 /* we got stopped before we even got started */ 1926 USB_BUS_LOCK(info->bus); 1927 goto done; 1928 } 1929 } else { 1930 1931 if (usb2_callback_wrapper_sub(xfer)) { 1932 /* the callback has been deferred */ 1933 USB_BUS_LOCK(info->bus); 1934 goto done; 1935 } 1936#if USB_HAVE_POWERD 1937 /* decrement power reference */ 1938 usb2_transfer_power_ref(xfer, -1); 1939#endif 1940 xfer->flags_int.transferring = 0; 1941 1942 if (xfer->error) { 1943 xfer->usb2_state = USB_ST_ERROR; 1944 } else { 1945 /* set transferred state */ 1946 xfer->usb2_state = USB_ST_TRANSFERRED; 1947#if USB_HAVE_BUSDMA 1948 /* sync DMA memory, if any */ 1949 if (xfer->flags_int.bdma_enable && 1950 (!xfer->flags_int.bdma_no_post_sync)) { 1951 usb2_bdma_post_sync(xfer); 1952 } 1953#endif 1954 } 1955 } 1956 1957 /* call processing routine */ 1958 (xfer->callback) (xfer); 1959 1960 /* pickup the USB mutex again */ 1961 USB_BUS_LOCK(info->bus); 1962 1963 /* 1964 * Check if we got started after that we got cancelled, but 1965 * before we managed to do the callback. 1966 */ 1967 if ((!xfer->flags_int.open) && 1968 (xfer->flags_int.started) && 1969 (xfer->usb2_state == USB_ST_ERROR)) { 1970 /* try to loop, but not recursivly */ 1971 usb2_command_wrapper(&info->done_q, xfer); 1972 return; 1973 } 1974 1975done: 1976 /* 1977 * Check if we are draining. 1978 */ 1979 if (xfer->flags_int.draining && 1980 (!xfer->flags_int.transferring)) { 1981 /* "usb2_transfer_drain()" is waiting for end of transfer */ 1982 xfer->flags_int.draining = 0; 1983 usb2_cv_broadcast(&info->cv_drain); 1984 } 1985 1986 /* do the next callback, if any */ 1987 usb2_command_wrapper(&info->done_q, 1988 info->done_q.curr); 1989} 1990 1991/*------------------------------------------------------------------------* 1992 * usb2_dma_delay_done_cb 1993 * 1994 * This function is called when the DMA delay has been exectuded, and 1995 * will make sure that the callback is called to complete the USB 1996 * transfer. This code path is ususally only used when there is an USB 1997 * error like USB_ERR_CANCELLED. 1998 *------------------------------------------------------------------------*/ 1999static void 2000usb2_dma_delay_done_cb(void *arg) 2001{ 2002 struct usb2_xfer *xfer = arg; 2003 2004 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2005 2006 DPRINTFN(3, "Completed %p\n", xfer); 2007 2008 /* queue callback for execution, again */ 2009 usb2_transfer_done(xfer, 0); 2010} 2011 2012/*------------------------------------------------------------------------* 2013 * usb2_transfer_dequeue 2014 * 2015 * - This function is used to remove an USB transfer from a USB 2016 * transfer queue. 2017 * 2018 * - This function can be called multiple times in a row. 2019 *------------------------------------------------------------------------*/ 2020void 2021usb2_transfer_dequeue(struct usb2_xfer *xfer) 2022{ 2023 struct usb2_xfer_queue *pq; 2024 2025 pq = xfer->wait_queue; 2026 if (pq) { 2027 TAILQ_REMOVE(&pq->head, xfer, wait_entry); 2028 xfer->wait_queue = NULL; 2029 } 2030} 2031 2032/*------------------------------------------------------------------------* 2033 * usb2_transfer_enqueue 2034 * 2035 * - This function is used to insert an USB transfer into a USB * 2036 * transfer queue. 2037 * 2038 * - This function can be called multiple times in a row. 2039 *------------------------------------------------------------------------*/ 2040void 2041usb2_transfer_enqueue(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer) 2042{ 2043 /* 2044 * Insert the USB transfer into the queue, if it is not 2045 * already on a USB transfer queue: 2046 */ 2047 if (xfer->wait_queue == NULL) { 2048 xfer->wait_queue = pq; 2049 TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); 2050 } 2051} 2052 2053/*------------------------------------------------------------------------* 2054 * usb2_transfer_done 2055 * 2056 * - This function is used to remove an USB transfer from the busdma, 2057 * pipe or interrupt queue. 2058 * 2059 * - This function is used to queue the USB transfer on the done 2060 * queue. 2061 * 2062 * - This function is used to stop any USB transfer timeouts. 2063 *------------------------------------------------------------------------*/ 2064void 2065usb2_transfer_done(struct usb2_xfer *xfer, usb2_error_t error) 2066{ 2067 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2068 2069 DPRINTF("err=%s\n", usb2_errstr(error)); 2070 2071 /* 2072 * If we are not transferring then just return. 2073 * This can happen during transfer cancel. 2074 */ 2075 if (!xfer->flags_int.transferring) { 2076 DPRINTF("not transferring\n"); 2077 return; 2078 } 2079 /* only set transfer error if not already set */ 2080 if (!xfer->error) { 2081 xfer->error = error; 2082 } 2083 /* stop any callouts */ 2084 usb2_callout_stop(&xfer->timeout_handle); 2085 2086 /* 2087 * If we are waiting on a queue, just remove the USB transfer 2088 * from the queue, if any. We should have the required locks 2089 * locked to do the remove when this function is called. 2090 */ 2091 usb2_transfer_dequeue(xfer); 2092 2093#if USB_HAVE_BUSDMA 2094 if (mtx_owned(xfer->xroot->xfer_mtx)) { 2095 struct usb2_xfer_queue *pq; 2096 2097 /* 2098 * If the private USB lock is not locked, then we assume 2099 * that the BUS-DMA load stage has been passed: 2100 */ 2101 pq = &xfer->xroot->dma_q; 2102 2103 if (pq->curr == xfer) { 2104 /* start the next BUS-DMA load, if any */ 2105 usb2_command_wrapper(pq, NULL); 2106 } 2107 } 2108#endif 2109 /* keep some statistics */ 2110 if (xfer->error) { 2111 xfer->xroot->bus->stats_err.uds_requests 2112 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++; 2113 } else { 2114 xfer->xroot->bus->stats_ok.uds_requests 2115 [xfer->pipe->edesc->bmAttributes & UE_XFERTYPE]++; 2116 } 2117 2118 /* call the USB transfer callback */ 2119 usb2_callback_ss_done_defer(xfer); 2120} 2121 2122/*------------------------------------------------------------------------* 2123 * usb2_transfer_start_cb 2124 * 2125 * This function is called to start the USB transfer when 2126 * "xfer->interval" is greater than zero, and and the endpoint type is 2127 * BULK or CONTROL. 2128 *------------------------------------------------------------------------*/ 2129static void 2130usb2_transfer_start_cb(void *arg) 2131{ 2132 struct usb2_xfer *xfer = arg; 2133 struct usb2_pipe *pipe = xfer->pipe; 2134 2135 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2136 2137 DPRINTF("start\n"); 2138 2139 /* start the transfer */ 2140 (pipe->methods->start) (xfer); 2141 2142 xfer->flags_int.can_cancel_immed = 1; 2143 2144 /* check for error */ 2145 if (xfer->error) { 2146 /* some error has happened */ 2147 usb2_transfer_done(xfer, 0); 2148 } 2149} 2150 2151/*------------------------------------------------------------------------* 2152 * usb2_transfer_set_stall 2153 * 2154 * This function is used to set the stall flag outside the 2155 * callback. This function is NULL safe. 2156 *------------------------------------------------------------------------*/ 2157void 2158usb2_transfer_set_stall(struct usb2_xfer *xfer) 2159{ 2160 if (xfer == NULL) { 2161 /* tearing down */ 2162 return; 2163 } 2164 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2165 2166 /* avoid any races by locking the USB mutex */ 2167 USB_BUS_LOCK(xfer->xroot->bus); 2168 2169 xfer->flags.stall_pipe = 1; 2170 2171 USB_BUS_UNLOCK(xfer->xroot->bus); 2172} 2173 2174/*------------------------------------------------------------------------* 2175 * usb2_transfer_clear_stall 2176 * 2177 * This function is used to clear the stall flag outside the 2178 * callback. This function is NULL safe. 2179 *------------------------------------------------------------------------*/ 2180void 2181usb2_transfer_clear_stall(struct usb2_xfer *xfer) 2182{ 2183 if (xfer == NULL) { 2184 /* tearing down */ 2185 return; 2186 } 2187 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2188 2189 /* avoid any races by locking the USB mutex */ 2190 USB_BUS_LOCK(xfer->xroot->bus); 2191 2192 xfer->flags.stall_pipe = 0; 2193 2194 USB_BUS_UNLOCK(xfer->xroot->bus); 2195} 2196 2197/*------------------------------------------------------------------------* 2198 * usb2_pipe_start 2199 * 2200 * This function is used to add an USB transfer to the pipe transfer list. 2201 *------------------------------------------------------------------------*/ 2202void 2203usb2_pipe_start(struct usb2_xfer_queue *pq) 2204{ 2205 struct usb2_pipe *pipe; 2206 struct usb2_xfer *xfer; 2207 uint8_t type; 2208 2209 xfer = pq->curr; 2210 pipe = xfer->pipe; 2211 2212 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2213 2214 /* 2215 * If the pipe is already stalled we do nothing ! 2216 */ 2217 if (pipe->is_stalled) { 2218 return; 2219 } 2220 /* 2221 * Check if we are supposed to stall the pipe: 2222 */ 2223 if (xfer->flags.stall_pipe) { 2224 /* clear stall command */ 2225 xfer->flags.stall_pipe = 0; 2226 2227 /* 2228 * Only stall BULK and INTERRUPT endpoints. 2229 */ 2230 type = (pipe->edesc->bmAttributes & UE_XFERTYPE); 2231 if ((type == UE_BULK) || 2232 (type == UE_INTERRUPT)) { 2233 struct usb2_device *udev; 2234 struct usb2_xfer_root *info; 2235 2236 info = xfer->xroot; 2237 udev = info->udev; 2238 pipe->is_stalled = 1; 2239 2240 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 2241 (udev->bus->methods->set_stall) ( 2242 udev, NULL, pipe); 2243 } else if (udev->default_xfer[1]) { 2244 info = udev->default_xfer[1]->xroot; 2245 if (usb2_proc_msignal( 2246 &info->bus->non_giant_callback_proc, 2247 &udev->cs_msg[0], &udev->cs_msg[1])) { 2248 /* ignore */ 2249 } 2250 } else { 2251 /* should not happen */ 2252 DPRINTFN(0, "No stall handler!\n"); 2253 } 2254 /* 2255 * We get started again when the stall is cleared! 2256 */ 2257 return; 2258 } 2259 } 2260 /* Set or clear stall complete - special case */ 2261 if (xfer->nframes == 0) { 2262 /* we are complete */ 2263 xfer->aframes = 0; 2264 usb2_transfer_done(xfer, 0); 2265 return; 2266 } 2267 /* 2268 * Handled cases: 2269 * 2270 * 1) Start the first transfer queued. 2271 * 2272 * 2) Re-start the current USB transfer. 2273 */ 2274 /* 2275 * Check if there should be any 2276 * pre transfer start delay: 2277 */ 2278 if (xfer->interval > 0) { 2279 type = (pipe->edesc->bmAttributes & UE_XFERTYPE); 2280 if ((type == UE_BULK) || 2281 (type == UE_CONTROL)) { 2282 usb2_transfer_timeout_ms(xfer, 2283 &usb2_transfer_start_cb, 2284 xfer->interval); 2285 return; 2286 } 2287 } 2288 DPRINTF("start\n"); 2289 2290 /* start USB transfer */ 2291 (pipe->methods->start) (xfer); 2292 2293 xfer->flags_int.can_cancel_immed = 1; 2294 2295 /* check for error */ 2296 if (xfer->error) { 2297 /* some error has happened */ 2298 usb2_transfer_done(xfer, 0); 2299 } 2300} 2301 2302/*------------------------------------------------------------------------* 2303 * usb2_transfer_timeout_ms 2304 * 2305 * This function is used to setup a timeout on the given USB 2306 * transfer. If the timeout has been deferred the callback given by 2307 * "cb" will get called after "ms" milliseconds. 2308 *------------------------------------------------------------------------*/ 2309void 2310usb2_transfer_timeout_ms(struct usb2_xfer *xfer, 2311 void (*cb) (void *arg), usb2_timeout_t ms) 2312{ 2313 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2314 2315 /* defer delay */ 2316 usb2_callout_reset(&xfer->timeout_handle, 2317 USB_MS_TO_TICKS(ms), cb, xfer); 2318} 2319 2320/*------------------------------------------------------------------------* 2321 * usb2_callback_wrapper_sub 2322 * 2323 * - This function will update variables in an USB transfer after 2324 * that the USB transfer is complete. 2325 * 2326 * - This function is used to start the next USB transfer on the 2327 * pipe transfer queue, if any. 2328 * 2329 * NOTE: In some special cases the USB transfer will not be removed from 2330 * the pipe queue, but remain first. To enforce USB transfer removal call 2331 * this function passing the error code "USB_ERR_CANCELLED". 2332 * 2333 * Return values: 2334 * 0: Success. 2335 * Else: The callback has been deferred. 2336 *------------------------------------------------------------------------*/ 2337static uint8_t 2338usb2_callback_wrapper_sub(struct usb2_xfer *xfer) 2339{ 2340 struct usb2_pipe *pipe; 2341 usb2_frcount_t x; 2342 2343 if ((!xfer->flags_int.open) && 2344 (!xfer->flags_int.did_close)) { 2345 DPRINTF("close\n"); 2346 USB_BUS_LOCK(xfer->xroot->bus); 2347 (xfer->pipe->methods->close) (xfer); 2348 USB_BUS_UNLOCK(xfer->xroot->bus); 2349 /* only close once */ 2350 xfer->flags_int.did_close = 1; 2351 return (1); /* wait for new callback */ 2352 } 2353 /* 2354 * If we have a non-hardware induced error we 2355 * need to do the DMA delay! 2356 */ 2357 if (((xfer->error == USB_ERR_CANCELLED) || 2358 (xfer->error == USB_ERR_TIMEOUT)) && 2359 (!xfer->flags_int.did_dma_delay)) { 2360 2361 usb2_timeout_t temp; 2362 2363 /* only delay once */ 2364 xfer->flags_int.did_dma_delay = 1; 2365 2366 /* we can not cancel this delay */ 2367 xfer->flags_int.can_cancel_immed = 0; 2368 2369 temp = usb2_get_dma_delay(xfer->xroot->bus); 2370 2371 DPRINTFN(3, "DMA delay, %u ms, " 2372 "on %p\n", temp, xfer); 2373 2374 if (temp != 0) { 2375 USB_BUS_LOCK(xfer->xroot->bus); 2376 usb2_transfer_timeout_ms(xfer, 2377 &usb2_dma_delay_done_cb, temp); 2378 USB_BUS_UNLOCK(xfer->xroot->bus); 2379 return (1); /* wait for new callback */ 2380 } 2381 } 2382 /* check actual number of frames */ 2383 if (xfer->aframes > xfer->nframes) { 2384 if (xfer->error == 0) { 2385 panic("%s: actual number of frames, %d, is " 2386 "greater than initial number of frames, %d!\n", 2387 __FUNCTION__, xfer->aframes, xfer->nframes); 2388 } else { 2389 /* just set some valid value */ 2390 xfer->aframes = xfer->nframes; 2391 } 2392 } 2393 /* compute actual length */ 2394 xfer->actlen = 0; 2395 2396 for (x = 0; x != xfer->aframes; x++) { 2397 xfer->actlen += xfer->frlengths[x]; 2398 } 2399 2400 /* 2401 * Frames that were not transferred get zero actual length in 2402 * case the USB device driver does not check the actual number 2403 * of frames transferred, "xfer->aframes": 2404 */ 2405 for (; x < xfer->nframes; x++) { 2406 xfer->frlengths[x] = 0; 2407 } 2408 2409 /* check actual length */ 2410 if (xfer->actlen > xfer->sumlen) { 2411 if (xfer->error == 0) { 2412 panic("%s: actual length, %d, is greater than " 2413 "initial length, %d!\n", 2414 __FUNCTION__, xfer->actlen, xfer->sumlen); 2415 } else { 2416 /* just set some valid value */ 2417 xfer->actlen = xfer->sumlen; 2418 } 2419 } 2420 DPRINTFN(6, "xfer=%p pipe=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n", 2421 xfer, xfer->pipe, xfer->error, xfer->actlen, xfer->sumlen, 2422 xfer->aframes, xfer->nframes); 2423 2424 if (xfer->error) { 2425 /* end of control transfer, if any */ 2426 xfer->flags_int.control_act = 0; 2427 2428 /* check if we should block the execution queue */ 2429 if ((xfer->error != USB_ERR_CANCELLED) && 2430 (xfer->flags.pipe_bof)) { 2431 DPRINTFN(2, "xfer=%p: Block On Failure " 2432 "on pipe=%p\n", xfer, xfer->pipe); 2433 goto done; 2434 } 2435 } else { 2436 /* check for short transfers */ 2437 if (xfer->actlen < xfer->sumlen) { 2438 2439 /* end of control transfer, if any */ 2440 xfer->flags_int.control_act = 0; 2441 2442 if (!xfer->flags_int.short_xfer_ok) { 2443 xfer->error = USB_ERR_SHORT_XFER; 2444 if (xfer->flags.pipe_bof) { 2445 DPRINTFN(2, "xfer=%p: Block On Failure on " 2446 "Short Transfer on pipe %p.\n", 2447 xfer, xfer->pipe); 2448 goto done; 2449 } 2450 } 2451 } else { 2452 /* 2453 * Check if we are in the middle of a 2454 * control transfer: 2455 */ 2456 if (xfer->flags_int.control_act) { 2457 DPRINTFN(5, "xfer=%p: Control transfer " 2458 "active on pipe=%p\n", xfer, xfer->pipe); 2459 goto done; 2460 } 2461 } 2462 } 2463 2464 pipe = xfer->pipe; 2465 2466 /* 2467 * If the current USB transfer is completing we need to start the 2468 * next one: 2469 */ 2470 USB_BUS_LOCK(xfer->xroot->bus); 2471 if (pipe->pipe_q.curr == xfer) { 2472 usb2_command_wrapper(&pipe->pipe_q, NULL); 2473 2474 if (pipe->pipe_q.curr || TAILQ_FIRST(&pipe->pipe_q.head)) { 2475 /* there is another USB transfer waiting */ 2476 } else { 2477 /* this is the last USB transfer */ 2478 /* clear isochronous sync flag */ 2479 xfer->pipe->is_synced = 0; 2480 } 2481 } 2482 USB_BUS_UNLOCK(xfer->xroot->bus); 2483done: 2484 return (0); 2485} 2486 2487/*------------------------------------------------------------------------* 2488 * usb2_command_wrapper 2489 * 2490 * This function is used to execute commands non-recursivly on an USB 2491 * transfer. 2492 *------------------------------------------------------------------------*/ 2493void 2494usb2_command_wrapper(struct usb2_xfer_queue *pq, struct usb2_xfer *xfer) 2495{ 2496 if (xfer) { 2497 /* 2498 * If the transfer is not already processing, 2499 * queue it! 2500 */ 2501 if (pq->curr != xfer) { 2502 usb2_transfer_enqueue(pq, xfer); 2503 if (pq->curr != NULL) { 2504 /* something is already processing */ 2505 DPRINTFN(6, "busy %p\n", pq->curr); 2506 return; 2507 } 2508 } 2509 } else { 2510 /* Get next element in queue */ 2511 pq->curr = NULL; 2512 } 2513 2514 if (!pq->recurse_1) { 2515 2516 do { 2517 2518 /* set both recurse flags */ 2519 pq->recurse_1 = 1; 2520 pq->recurse_2 = 1; 2521 2522 if (pq->curr == NULL) { 2523 xfer = TAILQ_FIRST(&pq->head); 2524 if (xfer) { 2525 TAILQ_REMOVE(&pq->head, xfer, 2526 wait_entry); 2527 xfer->wait_queue = NULL; 2528 pq->curr = xfer; 2529 } else { 2530 break; 2531 } 2532 } 2533 DPRINTFN(6, "cb %p (enter)\n", pq->curr); 2534 (pq->command) (pq); 2535 DPRINTFN(6, "cb %p (leave)\n", pq->curr); 2536 2537 } while (!pq->recurse_2); 2538 2539 /* clear first recurse flag */ 2540 pq->recurse_1 = 0; 2541 2542 } else { 2543 /* clear second recurse flag */ 2544 pq->recurse_2 = 0; 2545 } 2546} 2547 2548/*------------------------------------------------------------------------* 2549 * usb2_default_transfer_setup 2550 * 2551 * This function is used to setup the default USB control endpoint 2552 * transfer. 2553 *------------------------------------------------------------------------*/ 2554void 2555usb2_default_transfer_setup(struct usb2_device *udev) 2556{ 2557 struct usb2_xfer *xfer; 2558 uint8_t no_resetup; 2559 uint8_t iface_index; 2560 2561 /* check for root HUB */ 2562 if (udev->parent_hub == NULL) 2563 return; 2564repeat: 2565 2566 xfer = udev->default_xfer[0]; 2567 if (xfer) { 2568 USB_XFER_LOCK(xfer); 2569 no_resetup = 2570 ((xfer->address == udev->address) && 2571 (udev->default_ep_desc.wMaxPacketSize[0] == 2572 udev->ddesc.bMaxPacketSize)); 2573 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 2574 if (no_resetup) { 2575 /* 2576 * NOTE: checking "xfer->address" and 2577 * starting the USB transfer must be 2578 * atomic! 2579 */ 2580 usb2_transfer_start(xfer); 2581 } 2582 } 2583 USB_XFER_UNLOCK(xfer); 2584 } else { 2585 no_resetup = 0; 2586 } 2587 2588 if (no_resetup) { 2589 /* 2590 * All parameters are exactly the same like before. 2591 * Just return. 2592 */ 2593 return; 2594 } 2595 /* 2596 * Update wMaxPacketSize for the default control endpoint: 2597 */ 2598 udev->default_ep_desc.wMaxPacketSize[0] = 2599 udev->ddesc.bMaxPacketSize; 2600 2601 /* 2602 * Unsetup any existing USB transfer: 2603 */ 2604 usb2_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 2605 2606 /* 2607 * Try to setup a new USB transfer for the 2608 * default control endpoint: 2609 */ 2610 iface_index = 0; 2611 if (usb2_transfer_setup(udev, &iface_index, 2612 udev->default_xfer, usb2_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL, 2613 udev->default_mtx)) { 2614 DPRINTFN(0, "could not setup default " 2615 "USB transfer!\n"); 2616 } else { 2617 goto repeat; 2618 } 2619} 2620 2621/*------------------------------------------------------------------------* 2622 * usb2_clear_data_toggle - factored out code 2623 * 2624 * NOTE: the intention of this function is not to reset the hardware 2625 * data toggle. 2626 *------------------------------------------------------------------------*/ 2627void 2628usb2_clear_data_toggle(struct usb2_device *udev, struct usb2_pipe *pipe) 2629{ 2630 DPRINTFN(5, "udev=%p pipe=%p\n", udev, pipe); 2631 2632 USB_BUS_LOCK(udev->bus); 2633 pipe->toggle_next = 0; 2634 USB_BUS_UNLOCK(udev->bus); 2635} 2636 2637/*------------------------------------------------------------------------* 2638 * usb2_clear_stall_callback - factored out clear stall callback 2639 * 2640 * Input parameters: 2641 * xfer1: Clear Stall Control Transfer 2642 * xfer2: Stalled USB Transfer 2643 * 2644 * This function is NULL safe. 2645 * 2646 * Return values: 2647 * 0: In progress 2648 * Else: Finished 2649 * 2650 * Clear stall config example: 2651 * 2652 * static const struct usb2_config my_clearstall = { 2653 * .type = UE_CONTROL, 2654 * .endpoint = 0, 2655 * .direction = UE_DIR_ANY, 2656 * .interval = 50, //50 milliseconds 2657 * .bufsize = sizeof(struct usb2_device_request), 2658 * .timeout = 1000, //1.000 seconds 2659 * .callback = &my_clear_stall_callback, // ** 2660 * .usb_mode = USB_MODE_HOST, 2661 * }; 2662 * 2663 * ** "my_clear_stall_callback" calls "usb2_clear_stall_callback" 2664 * passing the correct parameters. 2665 *------------------------------------------------------------------------*/ 2666uint8_t 2667usb2_clear_stall_callback(struct usb2_xfer *xfer1, 2668 struct usb2_xfer *xfer2) 2669{ 2670 struct usb2_device_request req; 2671 2672 if (xfer2 == NULL) { 2673 /* looks like we are tearing down */ 2674 DPRINTF("NULL input parameter\n"); 2675 return (0); 2676 } 2677 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED); 2678 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED); 2679 2680 switch (USB_GET_STATE(xfer1)) { 2681 case USB_ST_SETUP: 2682 2683 /* 2684 * pre-clear the data toggle to DATA0 ("umass.c" and 2685 * "ata-usb.c" depends on this) 2686 */ 2687 2688 usb2_clear_data_toggle(xfer2->xroot->udev, xfer2->pipe); 2689 2690 /* setup a clear-stall packet */ 2691 2692 req.bmRequestType = UT_WRITE_ENDPOINT; 2693 req.bRequest = UR_CLEAR_FEATURE; 2694 USETW(req.wValue, UF_ENDPOINT_HALT); 2695 req.wIndex[0] = xfer2->pipe->edesc->bEndpointAddress; 2696 req.wIndex[1] = 0; 2697 USETW(req.wLength, 0); 2698 2699 /* 2700 * "usb2_transfer_setup_sub()" will ensure that 2701 * we have sufficient room in the buffer for 2702 * the request structure! 2703 */ 2704 2705 /* copy in the transfer */ 2706 2707 usb2_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); 2708 2709 /* set length */ 2710 xfer1->frlengths[0] = sizeof(req); 2711 xfer1->nframes = 1; 2712 2713 usb2_start_hardware(xfer1); 2714 return (0); 2715 2716 case USB_ST_TRANSFERRED: 2717 break; 2718 2719 default: /* Error */ 2720 if (xfer1->error == USB_ERR_CANCELLED) { 2721 return (0); 2722 } 2723 break; 2724 } 2725 return (1); /* Clear Stall Finished */ 2726} 2727 2728void 2729usb2_do_poll(struct usb2_xfer **ppxfer, uint16_t max) 2730{ 2731 static uint8_t once = 0; 2732 /* polling is currently not supported */ 2733 if (!once) { 2734 once = 1; 2735 printf("usb2_do_poll: USB polling is " 2736 "not supported!\n"); 2737 } 2738} 2739 2740static void 2741usb2_get_std_packet_size(struct usb2_std_packet_size *ptr, 2742 uint8_t type, uint8_t usb_speed) 2743{ 2744 static const uint16_t intr_range_max[USB_SPEED_MAX] = { 2745 [USB_SPEED_LOW] = 8, 2746 [USB_SPEED_FULL] = 64, 2747 [USB_SPEED_HIGH] = 1024, 2748 [USB_SPEED_VARIABLE] = 1024, 2749 [USB_SPEED_SUPER] = 1024, 2750 }; 2751 2752 static const uint16_t isoc_range_max[USB_SPEED_MAX] = { 2753 [USB_SPEED_LOW] = 0, /* invalid */ 2754 [USB_SPEED_FULL] = 1023, 2755 [USB_SPEED_HIGH] = 1024, 2756 [USB_SPEED_VARIABLE] = 3584, 2757 [USB_SPEED_SUPER] = 1024, 2758 }; 2759 2760 static const uint16_t control_min[USB_SPEED_MAX] = { 2761 [USB_SPEED_LOW] = 8, 2762 [USB_SPEED_FULL] = 8, 2763 [USB_SPEED_HIGH] = 64, 2764 [USB_SPEED_VARIABLE] = 512, 2765 [USB_SPEED_SUPER] = 512, 2766 }; 2767 2768 static const uint16_t bulk_min[USB_SPEED_MAX] = { 2769 [USB_SPEED_LOW] = 0, /* not supported */ 2770 [USB_SPEED_FULL] = 8, 2771 [USB_SPEED_HIGH] = 512, 2772 [USB_SPEED_VARIABLE] = 512, 2773 [USB_SPEED_SUPER] = 1024, 2774 }; 2775 2776 uint16_t temp; 2777 2778 memset(ptr, 0, sizeof(*ptr)); 2779 2780 switch (type) { 2781 case UE_INTERRUPT: 2782 ptr->range.max = intr_range_max[usb_speed]; 2783 break; 2784 case UE_ISOCHRONOUS: 2785 ptr->range.max = isoc_range_max[usb_speed]; 2786 break; 2787 default: 2788 if (type == UE_BULK) 2789 temp = bulk_min[usb_speed]; 2790 else /* UE_CONTROL */ 2791 temp = control_min[usb_speed]; 2792 2793 /* default is fixed */ 2794 ptr->fixed[0] = temp; 2795 ptr->fixed[1] = temp; 2796 ptr->fixed[2] = temp; 2797 ptr->fixed[3] = temp; 2798 2799 if (usb_speed == USB_SPEED_FULL) { 2800 /* multiple sizes */ 2801 ptr->fixed[1] = 16; 2802 ptr->fixed[2] = 32; 2803 ptr->fixed[3] = 64; 2804 } 2805 if ((usb_speed == USB_SPEED_VARIABLE) && 2806 (type == UE_BULK)) { 2807 /* multiple sizes */ 2808 ptr->fixed[2] = 1024; 2809 ptr->fixed[3] = 1536; 2810 } 2811 break; 2812 } 2813} 2814