102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 302ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 602ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 702ac6454SAndrew Thompson * are met: 802ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 902ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1002ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1202ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1502ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1602ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1702ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1802ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1902ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2002ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2102ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2202ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2302ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2402ac6454SAndrew Thompson * SUCH DAMAGE. 2502ac6454SAndrew Thompson */ 2602ac6454SAndrew Thompson 27ed6d949aSAndrew Thompson #include <sys/stdint.h> 28ed6d949aSAndrew Thompson #include <sys/stddef.h> 29ed6d949aSAndrew Thompson #include <sys/param.h> 30ed6d949aSAndrew Thompson #include <sys/queue.h> 31ed6d949aSAndrew Thompson #include <sys/types.h> 32ed6d949aSAndrew Thompson #include <sys/systm.h> 33ed6d949aSAndrew Thompson #include <sys/kernel.h> 34ed6d949aSAndrew Thompson #include <sys/bus.h> 35ed6d949aSAndrew Thompson #include <sys/linker_set.h> 36ed6d949aSAndrew Thompson #include <sys/module.h> 37ed6d949aSAndrew Thompson #include <sys/lock.h> 38ed6d949aSAndrew Thompson #include <sys/mutex.h> 39ed6d949aSAndrew Thompson #include <sys/condvar.h> 40ed6d949aSAndrew Thompson #include <sys/sysctl.h> 41ed6d949aSAndrew Thompson #include <sys/sx.h> 42ed6d949aSAndrew Thompson #include <sys/unistd.h> 43ed6d949aSAndrew Thompson #include <sys/callout.h> 44ed6d949aSAndrew Thompson #include <sys/malloc.h> 45ed6d949aSAndrew Thompson #include <sys/priv.h> 46ed6d949aSAndrew Thompson 4702ac6454SAndrew Thompson #include <dev/usb/usb.h> 48ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 49ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 5002ac6454SAndrew Thompson 51a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 5202ac6454SAndrew Thompson 5302ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 5402ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 5502ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 5702ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 5802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 5902ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 6002ac6454SAndrew Thompson 6102ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 6202ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 6302ac6454SAndrew Thompson 64a593f6b8SAndrew Thompson struct usb_std_packet_size { 6502ac6454SAndrew Thompson struct { 6602ac6454SAndrew Thompson uint16_t min; /* inclusive */ 6702ac6454SAndrew Thompson uint16_t max; /* inclusive */ 6802ac6454SAndrew Thompson } range; 6902ac6454SAndrew Thompson 7002ac6454SAndrew Thompson uint16_t fixed[4]; 7102ac6454SAndrew Thompson }; 7202ac6454SAndrew Thompson 73a593f6b8SAndrew Thompson static usb_callback_t usb_request_callback; 7402ac6454SAndrew Thompson 75a593f6b8SAndrew Thompson static const struct usb_config usb_control_ep_cfg[USB_DEFAULT_XFER_MAX] = { 7602ac6454SAndrew Thompson 7702ac6454SAndrew Thompson /* This transfer is used for generic control endpoint transfers */ 7802ac6454SAndrew Thompson 7902ac6454SAndrew Thompson [0] = { 8002ac6454SAndrew Thompson .type = UE_CONTROL, 8102ac6454SAndrew Thompson .endpoint = 0x00, /* Control endpoint */ 8202ac6454SAndrew Thompson .direction = UE_DIR_ANY, 834eae601eSAndrew Thompson .bufsize = USB_EP0_BUFSIZE, /* bytes */ 844eae601eSAndrew Thompson .flags = {.proxy_buffer = 1,}, 85a593f6b8SAndrew Thompson .callback = &usb_request_callback, 86f29a0724SAndrew Thompson .usb_mode = USB_MODE_DUAL, /* both modes */ 8702ac6454SAndrew Thompson }, 8802ac6454SAndrew Thompson 8902ac6454SAndrew Thompson /* This transfer is used for generic clear stall only */ 9002ac6454SAndrew Thompson 9102ac6454SAndrew Thompson [1] = { 9202ac6454SAndrew Thompson .type = UE_CONTROL, 9302ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */ 9402ac6454SAndrew Thompson .direction = UE_DIR_ANY, 95760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request), 96a593f6b8SAndrew Thompson .callback = &usb_do_clear_stall_callback, 974eae601eSAndrew Thompson .timeout = 1000, /* 1 second */ 984eae601eSAndrew Thompson .interval = 50, /* 50ms */ 994eae601eSAndrew Thompson .usb_mode = USB_MODE_HOST, 10002ac6454SAndrew Thompson }, 10102ac6454SAndrew Thompson }; 10202ac6454SAndrew Thompson 10302ac6454SAndrew Thompson /* function prototypes */ 10402ac6454SAndrew Thompson 105a593f6b8SAndrew Thompson static void usbd_update_max_frame_size(struct usb_xfer *); 106a593f6b8SAndrew Thompson static void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t); 107a593f6b8SAndrew Thompson static void usbd_control_transfer_init(struct usb_xfer *); 108ed6d949aSAndrew Thompson static int usbd_setup_ctrl_transfer(struct usb_xfer *); 109a593f6b8SAndrew Thompson static void usb_callback_proc(struct usb_proc_msg *); 110a593f6b8SAndrew Thompson static void usbd_callback_ss_done_defer(struct usb_xfer *); 111a593f6b8SAndrew Thompson static void usbd_callback_wrapper(struct usb_xfer_queue *); 112a593f6b8SAndrew Thompson static void usb_dma_delay_done_cb(void *); 113a593f6b8SAndrew Thompson static void usbd_transfer_start_cb(void *); 114a593f6b8SAndrew Thompson static uint8_t usbd_callback_wrapper_sub(struct usb_xfer *); 115a593f6b8SAndrew Thompson static void usbd_get_std_packet_size(struct usb_std_packet_size *ptr, 1168d2dd5ddSAndrew Thompson uint8_t type, enum usb_dev_speed speed); 1174eae601eSAndrew Thompson 1184eae601eSAndrew Thompson /*------------------------------------------------------------------------* 119a593f6b8SAndrew Thompson * usb_request_callback 1204eae601eSAndrew Thompson *------------------------------------------------------------------------*/ 1214eae601eSAndrew Thompson static void 122ed6d949aSAndrew Thompson usb_request_callback(struct usb_xfer *xfer, usb_error_t error) 1234eae601eSAndrew Thompson { 124f29a0724SAndrew Thompson if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) 125ed6d949aSAndrew Thompson usb_handle_request_callback(xfer, error); 1264eae601eSAndrew Thompson else 127ed6d949aSAndrew Thompson usbd_do_request_callback(xfer, error); 1284eae601eSAndrew Thompson } 12902ac6454SAndrew Thompson 13002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 131a593f6b8SAndrew Thompson * usbd_update_max_frame_size 13202ac6454SAndrew Thompson * 13302ac6454SAndrew Thompson * This function updates the maximum frame size, hence high speed USB 13402ac6454SAndrew Thompson * can transfer multiple consecutive packets. 13502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 13602ac6454SAndrew Thompson static void 137a593f6b8SAndrew Thompson usbd_update_max_frame_size(struct usb_xfer *xfer) 13802ac6454SAndrew Thompson { 13902ac6454SAndrew Thompson /* compute maximum frame size */ 14002ac6454SAndrew Thompson 14102ac6454SAndrew Thompson if (xfer->max_packet_count == 2) { 14202ac6454SAndrew Thompson xfer->max_frame_size = 2 * xfer->max_packet_size; 14302ac6454SAndrew Thompson } else if (xfer->max_packet_count == 3) { 14402ac6454SAndrew Thompson xfer->max_frame_size = 3 * xfer->max_packet_size; 14502ac6454SAndrew Thompson } else { 14602ac6454SAndrew Thompson xfer->max_frame_size = xfer->max_packet_size; 14702ac6454SAndrew Thompson } 14802ac6454SAndrew Thompson } 14902ac6454SAndrew Thompson 15002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 151a593f6b8SAndrew Thompson * usbd_get_dma_delay 15202ac6454SAndrew Thompson * 15302ac6454SAndrew Thompson * The following function is called when we need to 15402ac6454SAndrew Thompson * synchronize with DMA hardware. 15502ac6454SAndrew Thompson * 15602ac6454SAndrew Thompson * Returns: 15702ac6454SAndrew Thompson * 0: no DMA delay required 15802ac6454SAndrew Thompson * Else: milliseconds of DMA delay 15902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 160e0a69b51SAndrew Thompson usb_timeout_t 161a593f6b8SAndrew Thompson usbd_get_dma_delay(struct usb_bus *bus) 16202ac6454SAndrew Thompson { 16302ac6454SAndrew Thompson uint32_t temp = 0; 16402ac6454SAndrew Thompson 16502ac6454SAndrew Thompson if (bus->methods->get_dma_delay) { 16602ac6454SAndrew Thompson (bus->methods->get_dma_delay) (bus, &temp); 16702ac6454SAndrew Thompson /* 16802ac6454SAndrew Thompson * Round up and convert to milliseconds. Note that we use 16902ac6454SAndrew Thompson * 1024 milliseconds per second. to save a division. 17002ac6454SAndrew Thompson */ 17102ac6454SAndrew Thompson temp += 0x3FF; 17202ac6454SAndrew Thompson temp /= 0x400; 17302ac6454SAndrew Thompson } 17402ac6454SAndrew Thompson return (temp); 17502ac6454SAndrew Thompson } 17602ac6454SAndrew Thompson 17702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 178a593f6b8SAndrew Thompson * usbd_transfer_setup_sub_malloc 17902ac6454SAndrew Thompson * 18002ac6454SAndrew Thompson * This function will allocate one or more DMA'able memory chunks 18102ac6454SAndrew Thompson * according to "size", "align" and "count" arguments. "ppc" is 18202ac6454SAndrew Thompson * pointed to a linear array of USB page caches afterwards. 18302ac6454SAndrew Thompson * 18402ac6454SAndrew Thompson * Returns: 18502ac6454SAndrew Thompson * 0: Success 18602ac6454SAndrew Thompson * Else: Failure 18702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 188bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 18902ac6454SAndrew Thompson uint8_t 190a593f6b8SAndrew Thompson usbd_transfer_setup_sub_malloc(struct usb_setup_params *parm, 191f9cb546cSAndrew Thompson struct usb_page_cache **ppc, usb_size_t size, usb_size_t align, 192f9cb546cSAndrew Thompson usb_size_t count) 19302ac6454SAndrew Thompson { 194760bc48eSAndrew Thompson struct usb_page_cache *pc; 195760bc48eSAndrew Thompson struct usb_page *pg; 19602ac6454SAndrew Thompson void *buf; 197f9cb546cSAndrew Thompson usb_size_t n_dma_pc; 198f9cb546cSAndrew Thompson usb_size_t n_obj; 199f9cb546cSAndrew Thompson usb_size_t x; 200f9cb546cSAndrew Thompson usb_size_t y; 201f9cb546cSAndrew Thompson usb_size_t r; 202f9cb546cSAndrew Thompson usb_size_t z; 20302ac6454SAndrew Thompson 20402ac6454SAndrew Thompson USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x!\n", 20502ac6454SAndrew Thompson align)); 20602ac6454SAndrew Thompson USB_ASSERT(size > 0, ("Invalid size = 0!\n")); 20702ac6454SAndrew Thompson 20802ac6454SAndrew Thompson if (count == 0) { 20902ac6454SAndrew Thompson return (0); /* nothing to allocate */ 21002ac6454SAndrew Thompson } 21102ac6454SAndrew Thompson /* 21202ac6454SAndrew Thompson * Make sure that the size is aligned properly. 21302ac6454SAndrew Thompson */ 21402ac6454SAndrew Thompson size = -((-size) & (-align)); 21502ac6454SAndrew Thompson 21602ac6454SAndrew Thompson /* 21702ac6454SAndrew Thompson * Try multi-allocation chunks to reduce the number of DMA 21802ac6454SAndrew Thompson * allocations, hence DMA allocations are slow. 21902ac6454SAndrew Thompson */ 22002ac6454SAndrew Thompson if (size >= PAGE_SIZE) { 22102ac6454SAndrew Thompson n_dma_pc = count; 22202ac6454SAndrew Thompson n_obj = 1; 22302ac6454SAndrew Thompson } else { 22402ac6454SAndrew Thompson /* compute number of objects per page */ 22502ac6454SAndrew Thompson n_obj = (PAGE_SIZE / size); 22602ac6454SAndrew Thompson /* 22702ac6454SAndrew Thompson * Compute number of DMA chunks, rounded up 22802ac6454SAndrew Thompson * to nearest one: 22902ac6454SAndrew Thompson */ 23002ac6454SAndrew Thompson n_dma_pc = ((count + n_obj - 1) / n_obj); 23102ac6454SAndrew Thompson } 23202ac6454SAndrew Thompson 23302ac6454SAndrew Thompson if (parm->buf == NULL) { 23402ac6454SAndrew Thompson /* for the future */ 23502ac6454SAndrew Thompson parm->dma_page_ptr += n_dma_pc; 23602ac6454SAndrew Thompson parm->dma_page_cache_ptr += n_dma_pc; 23702ac6454SAndrew Thompson parm->dma_page_ptr += count; 23802ac6454SAndrew Thompson parm->xfer_page_cache_ptr += count; 23902ac6454SAndrew Thompson return (0); 24002ac6454SAndrew Thompson } 24102ac6454SAndrew Thompson for (x = 0; x != n_dma_pc; x++) { 24202ac6454SAndrew Thompson /* need to initialize the page cache */ 24302ac6454SAndrew Thompson parm->dma_page_cache_ptr[x].tag_parent = 24402ac6454SAndrew Thompson &parm->curr_xfer->xroot->dma_parent_tag; 24502ac6454SAndrew Thompson } 24602ac6454SAndrew Thompson for (x = 0; x != count; x++) { 24702ac6454SAndrew Thompson /* need to initialize the page cache */ 24802ac6454SAndrew Thompson parm->xfer_page_cache_ptr[x].tag_parent = 24902ac6454SAndrew Thompson &parm->curr_xfer->xroot->dma_parent_tag; 25002ac6454SAndrew Thompson } 25102ac6454SAndrew Thompson 25202ac6454SAndrew Thompson if (ppc) { 25302ac6454SAndrew Thompson *ppc = parm->xfer_page_cache_ptr; 25402ac6454SAndrew Thompson } 25502ac6454SAndrew Thompson r = count; /* set remainder count */ 25602ac6454SAndrew Thompson z = n_obj * size; /* set allocation size */ 25702ac6454SAndrew Thompson pc = parm->xfer_page_cache_ptr; 25802ac6454SAndrew Thompson pg = parm->dma_page_ptr; 25902ac6454SAndrew Thompson 26002ac6454SAndrew Thompson for (x = 0; x != n_dma_pc; x++) { 26102ac6454SAndrew Thompson 26202ac6454SAndrew Thompson if (r < n_obj) { 26302ac6454SAndrew Thompson /* compute last remainder */ 26402ac6454SAndrew Thompson z = r * size; 26502ac6454SAndrew Thompson n_obj = r; 26602ac6454SAndrew Thompson } 267a593f6b8SAndrew Thompson if (usb_pc_alloc_mem(parm->dma_page_cache_ptr, 26802ac6454SAndrew Thompson pg, z, align)) { 26902ac6454SAndrew Thompson return (1); /* failure */ 27002ac6454SAndrew Thompson } 27102ac6454SAndrew Thompson /* Set beginning of current buffer */ 27202ac6454SAndrew Thompson buf = parm->dma_page_cache_ptr->buffer; 27302ac6454SAndrew Thompson /* Make room for one DMA page cache and one page */ 27402ac6454SAndrew Thompson parm->dma_page_cache_ptr++; 27502ac6454SAndrew Thompson pg++; 27602ac6454SAndrew Thompson 27702ac6454SAndrew Thompson for (y = 0; (y != n_obj); y++, r--, pc++, pg++) { 27802ac6454SAndrew Thompson 27902ac6454SAndrew Thompson /* Load sub-chunk into DMA */ 280a593f6b8SAndrew Thompson if (usb_pc_dmamap_create(pc, size)) { 28102ac6454SAndrew Thompson return (1); /* failure */ 28202ac6454SAndrew Thompson } 28302ac6454SAndrew Thompson pc->buffer = USB_ADD_BYTES(buf, y * size); 28402ac6454SAndrew Thompson pc->page_start = pg; 28502ac6454SAndrew Thompson 28602ac6454SAndrew Thompson mtx_lock(pc->tag_parent->mtx); 287a593f6b8SAndrew Thompson if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) { 28802ac6454SAndrew Thompson mtx_unlock(pc->tag_parent->mtx); 28902ac6454SAndrew Thompson return (1); /* failure */ 29002ac6454SAndrew Thompson } 29102ac6454SAndrew Thompson mtx_unlock(pc->tag_parent->mtx); 29202ac6454SAndrew Thompson } 29302ac6454SAndrew Thompson } 29402ac6454SAndrew Thompson 29502ac6454SAndrew Thompson parm->xfer_page_cache_ptr = pc; 29602ac6454SAndrew Thompson parm->dma_page_ptr = pg; 29702ac6454SAndrew Thompson return (0); 29802ac6454SAndrew Thompson } 299bdc081c6SAndrew Thompson #endif 30002ac6454SAndrew Thompson 30102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 302a593f6b8SAndrew Thompson * usbd_transfer_setup_sub - transfer setup subroutine 30302ac6454SAndrew Thompson * 30402ac6454SAndrew Thompson * This function must be called from the "xfer_setup" callback of the 30502ac6454SAndrew Thompson * USB Host or Device controller driver when setting up an USB 30602ac6454SAndrew Thompson * transfer. This function will setup correct packet sizes, buffer 307760bc48eSAndrew Thompson * sizes, flags and more, that are stored in the "usb_xfer" 30802ac6454SAndrew Thompson * structure. 30902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 31002ac6454SAndrew Thompson void 311a593f6b8SAndrew Thompson usbd_transfer_setup_sub(struct usb_setup_params *parm) 31202ac6454SAndrew Thompson { 31302ac6454SAndrew Thompson enum { 31402ac6454SAndrew Thompson REQ_SIZE = 8, 31502ac6454SAndrew Thompson MIN_PKT = 8, 31602ac6454SAndrew Thompson }; 317760bc48eSAndrew Thompson struct usb_xfer *xfer = parm->curr_xfer; 318760bc48eSAndrew Thompson const struct usb_config *setup = parm->curr_setup; 319760bc48eSAndrew Thompson struct usb_endpoint_descriptor *edesc; 320a593f6b8SAndrew Thompson struct usb_std_packet_size std_size; 321e0a69b51SAndrew Thompson usb_frcount_t n_frlengths; 322e0a69b51SAndrew Thompson usb_frcount_t n_frbuffers; 323e0a69b51SAndrew Thompson usb_frcount_t x; 32402ac6454SAndrew Thompson uint8_t type; 32502ac6454SAndrew Thompson uint8_t zmps; 32602ac6454SAndrew Thompson 32702ac6454SAndrew Thompson /* 32802ac6454SAndrew Thompson * Sanity check. The following parameters must be initialized before 32902ac6454SAndrew Thompson * calling this function. 33002ac6454SAndrew Thompson */ 33102ac6454SAndrew Thompson if ((parm->hc_max_packet_size == 0) || 33202ac6454SAndrew Thompson (parm->hc_max_packet_count == 0) || 33302ac6454SAndrew Thompson (parm->hc_max_frame_size == 0)) { 33402ac6454SAndrew Thompson parm->err = USB_ERR_INVAL; 33502ac6454SAndrew Thompson goto done; 33602ac6454SAndrew Thompson } 337ae60fdfbSAndrew Thompson edesc = xfer->endpoint->edesc; 33802ac6454SAndrew Thompson 33902ac6454SAndrew Thompson type = (edesc->bmAttributes & UE_XFERTYPE); 34002ac6454SAndrew Thompson 3414eae601eSAndrew Thompson xfer->flags = setup->flags; 3424eae601eSAndrew Thompson xfer->nframes = setup->frames; 3434eae601eSAndrew Thompson xfer->timeout = setup->timeout; 3444eae601eSAndrew Thompson xfer->callback = setup->callback; 3454eae601eSAndrew Thompson xfer->interval = setup->interval; 346ae60fdfbSAndrew Thompson xfer->endpointno = edesc->bEndpointAddress; 34702ac6454SAndrew Thompson xfer->max_packet_size = UGETW(edesc->wMaxPacketSize); 34802ac6454SAndrew Thompson xfer->max_packet_count = 1; 34902ac6454SAndrew Thompson /* make a shadow copy: */ 350f29a0724SAndrew Thompson xfer->flags_int.usb_mode = parm->udev->flags.usb_mode; 35102ac6454SAndrew Thompson 3524eae601eSAndrew Thompson parm->bufsize = setup->bufsize; 35302ac6454SAndrew Thompson 35402ac6454SAndrew Thompson if (parm->speed == USB_SPEED_HIGH) { 35502ac6454SAndrew Thompson xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3; 35602ac6454SAndrew Thompson xfer->max_packet_size &= 0x7FF; 35702ac6454SAndrew Thompson } 35802ac6454SAndrew Thompson /* range check "max_packet_count" */ 35902ac6454SAndrew Thompson 36002ac6454SAndrew Thompson if (xfer->max_packet_count > parm->hc_max_packet_count) { 36102ac6454SAndrew Thompson xfer->max_packet_count = parm->hc_max_packet_count; 36202ac6454SAndrew Thompson } 36302ac6454SAndrew Thompson /* filter "wMaxPacketSize" according to HC capabilities */ 36402ac6454SAndrew Thompson 36502ac6454SAndrew Thompson if ((xfer->max_packet_size > parm->hc_max_packet_size) || 36602ac6454SAndrew Thompson (xfer->max_packet_size == 0)) { 36702ac6454SAndrew Thompson xfer->max_packet_size = parm->hc_max_packet_size; 36802ac6454SAndrew Thompson } 36902ac6454SAndrew Thompson /* filter "wMaxPacketSize" according to standard sizes */ 37002ac6454SAndrew Thompson 371a593f6b8SAndrew Thompson usbd_get_std_packet_size(&std_size, type, parm->speed); 37202ac6454SAndrew Thompson 37302ac6454SAndrew Thompson if (std_size.range.min || std_size.range.max) { 37402ac6454SAndrew Thompson 37502ac6454SAndrew Thompson if (xfer->max_packet_size < std_size.range.min) { 37602ac6454SAndrew Thompson xfer->max_packet_size = std_size.range.min; 37702ac6454SAndrew Thompson } 37802ac6454SAndrew Thompson if (xfer->max_packet_size > std_size.range.max) { 37902ac6454SAndrew Thompson xfer->max_packet_size = std_size.range.max; 38002ac6454SAndrew Thompson } 38102ac6454SAndrew Thompson } else { 38202ac6454SAndrew Thompson 38302ac6454SAndrew Thompson if (xfer->max_packet_size >= std_size.fixed[3]) { 38402ac6454SAndrew Thompson xfer->max_packet_size = std_size.fixed[3]; 38502ac6454SAndrew Thompson } else if (xfer->max_packet_size >= std_size.fixed[2]) { 38602ac6454SAndrew Thompson xfer->max_packet_size = std_size.fixed[2]; 38702ac6454SAndrew Thompson } else if (xfer->max_packet_size >= std_size.fixed[1]) { 38802ac6454SAndrew Thompson xfer->max_packet_size = std_size.fixed[1]; 38902ac6454SAndrew Thompson } else { 39002ac6454SAndrew Thompson /* only one possibility left */ 39102ac6454SAndrew Thompson xfer->max_packet_size = std_size.fixed[0]; 39202ac6454SAndrew Thompson } 39302ac6454SAndrew Thompson } 39402ac6454SAndrew Thompson 39502ac6454SAndrew Thompson /* compute "max_frame_size" */ 39602ac6454SAndrew Thompson 397a593f6b8SAndrew Thompson usbd_update_max_frame_size(xfer); 39802ac6454SAndrew Thompson 39902ac6454SAndrew Thompson /* check interrupt interval and transfer pre-delay */ 40002ac6454SAndrew Thompson 40102ac6454SAndrew Thompson if (type == UE_ISOCHRONOUS) { 40202ac6454SAndrew Thompson 403578d0effSAndrew Thompson uint16_t frame_limit; 40402ac6454SAndrew Thompson 40502ac6454SAndrew Thompson xfer->interval = 0; /* not used, must be zero */ 40602ac6454SAndrew Thompson xfer->flags_int.isochronous_xfr = 1; /* set flag */ 40702ac6454SAndrew Thompson 40802ac6454SAndrew Thompson if (xfer->timeout == 0) { 40902ac6454SAndrew Thompson /* 41002ac6454SAndrew Thompson * set a default timeout in 41102ac6454SAndrew Thompson * case something goes wrong! 41202ac6454SAndrew Thompson */ 41302ac6454SAndrew Thompson xfer->timeout = 1000 / 4; 41402ac6454SAndrew Thompson } 41502ac6454SAndrew Thompson switch (parm->speed) { 41602ac6454SAndrew Thompson case USB_SPEED_LOW: 41702ac6454SAndrew Thompson case USB_SPEED_FULL: 41802ac6454SAndrew Thompson frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER; 41902ac6454SAndrew Thompson break; 42002ac6454SAndrew Thompson default: 42102ac6454SAndrew Thompson frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER; 42202ac6454SAndrew Thompson break; 42302ac6454SAndrew Thompson } 42402ac6454SAndrew Thompson 42502ac6454SAndrew Thompson if (xfer->nframes > frame_limit) { 42602ac6454SAndrew Thompson /* 42702ac6454SAndrew Thompson * this is not going to work 42802ac6454SAndrew Thompson * cross hardware 42902ac6454SAndrew Thompson */ 43002ac6454SAndrew Thompson parm->err = USB_ERR_INVAL; 43102ac6454SAndrew Thompson goto done; 43202ac6454SAndrew Thompson } 43302ac6454SAndrew Thompson if (xfer->nframes == 0) { 43402ac6454SAndrew Thompson /* 43502ac6454SAndrew Thompson * this is not a valid value 43602ac6454SAndrew Thompson */ 43702ac6454SAndrew Thompson parm->err = USB_ERR_ZERO_NFRAMES; 43802ac6454SAndrew Thompson goto done; 43902ac6454SAndrew Thompson } 44002ac6454SAndrew Thompson } else { 44102ac6454SAndrew Thompson 44202ac6454SAndrew Thompson /* 44302ac6454SAndrew Thompson * if a value is specified use that else check the endpoint 44402ac6454SAndrew Thompson * descriptor 44502ac6454SAndrew Thompson */ 44602ac6454SAndrew Thompson if (xfer->interval == 0) { 44702ac6454SAndrew Thompson 44802ac6454SAndrew Thompson if (type == UE_INTERRUPT) { 44902ac6454SAndrew Thompson 45002ac6454SAndrew Thompson xfer->interval = edesc->bInterval; 45102ac6454SAndrew Thompson 45202ac6454SAndrew Thompson switch (parm->speed) { 45302ac6454SAndrew Thompson case USB_SPEED_SUPER: 45402ac6454SAndrew Thompson case USB_SPEED_VARIABLE: 45502ac6454SAndrew Thompson /* 125us -> 1ms */ 45602ac6454SAndrew Thompson if (xfer->interval < 4) 45702ac6454SAndrew Thompson xfer->interval = 1; 45802ac6454SAndrew Thompson else if (xfer->interval > 16) 45902ac6454SAndrew Thompson xfer->interval = (1<<(16-4)); 46002ac6454SAndrew Thompson else 46102ac6454SAndrew Thompson xfer->interval = 46202ac6454SAndrew Thompson (1 << (xfer->interval-4)); 46302ac6454SAndrew Thompson break; 46402ac6454SAndrew Thompson case USB_SPEED_HIGH: 46502ac6454SAndrew Thompson /* 125us -> 1ms */ 46602ac6454SAndrew Thompson xfer->interval /= 8; 46702ac6454SAndrew Thompson break; 46802ac6454SAndrew Thompson default: 46902ac6454SAndrew Thompson break; 47002ac6454SAndrew Thompson } 47102ac6454SAndrew Thompson if (xfer->interval == 0) { 47202ac6454SAndrew Thompson /* 47302ac6454SAndrew Thompson * One millisecond is the smallest 47402ac6454SAndrew Thompson * interval we support: 47502ac6454SAndrew Thompson */ 47602ac6454SAndrew Thompson xfer->interval = 1; 47702ac6454SAndrew Thompson } 47802ac6454SAndrew Thompson } 47902ac6454SAndrew Thompson } 48002ac6454SAndrew Thompson } 48102ac6454SAndrew Thompson 48202ac6454SAndrew Thompson /* 48302ac6454SAndrew Thompson * NOTE: we do not allow "max_packet_size" or "max_frame_size" 48402ac6454SAndrew Thompson * to be equal to zero when setting up USB transfers, hence 48502ac6454SAndrew Thompson * this leads to alot of extra code in the USB kernel. 48602ac6454SAndrew Thompson */ 48702ac6454SAndrew Thompson 48802ac6454SAndrew Thompson if ((xfer->max_frame_size == 0) || 48902ac6454SAndrew Thompson (xfer->max_packet_size == 0)) { 49002ac6454SAndrew Thompson 49102ac6454SAndrew Thompson zmps = 1; 49202ac6454SAndrew Thompson 49302ac6454SAndrew Thompson if ((parm->bufsize <= MIN_PKT) && 49402ac6454SAndrew Thompson (type != UE_CONTROL) && 49502ac6454SAndrew Thompson (type != UE_BULK)) { 49602ac6454SAndrew Thompson 49702ac6454SAndrew Thompson /* workaround */ 49802ac6454SAndrew Thompson xfer->max_packet_size = MIN_PKT; 49902ac6454SAndrew Thompson xfer->max_packet_count = 1; 50002ac6454SAndrew Thompson parm->bufsize = 0; /* automatic setup length */ 501a593f6b8SAndrew Thompson usbd_update_max_frame_size(xfer); 50202ac6454SAndrew Thompson 50302ac6454SAndrew Thompson } else { 50402ac6454SAndrew Thompson parm->err = USB_ERR_ZERO_MAXP; 50502ac6454SAndrew Thompson goto done; 50602ac6454SAndrew Thompson } 50702ac6454SAndrew Thompson 50802ac6454SAndrew Thompson } else { 50902ac6454SAndrew Thompson zmps = 0; 51002ac6454SAndrew Thompson } 51102ac6454SAndrew Thompson 51202ac6454SAndrew Thompson /* 51302ac6454SAndrew Thompson * check if we should setup a default 51402ac6454SAndrew Thompson * length: 51502ac6454SAndrew Thompson */ 51602ac6454SAndrew Thompson 51702ac6454SAndrew Thompson if (parm->bufsize == 0) { 51802ac6454SAndrew Thompson 51902ac6454SAndrew Thompson parm->bufsize = xfer->max_frame_size; 52002ac6454SAndrew Thompson 52102ac6454SAndrew Thompson if (type == UE_ISOCHRONOUS) { 52202ac6454SAndrew Thompson parm->bufsize *= xfer->nframes; 52302ac6454SAndrew Thompson } 52402ac6454SAndrew Thompson } 52502ac6454SAndrew Thompson /* 52602ac6454SAndrew Thompson * check if we are about to setup a proxy 52702ac6454SAndrew Thompson * type of buffer: 52802ac6454SAndrew Thompson */ 52902ac6454SAndrew Thompson 53002ac6454SAndrew Thompson if (xfer->flags.proxy_buffer) { 53102ac6454SAndrew Thompson 53202ac6454SAndrew Thompson /* round bufsize up */ 53302ac6454SAndrew Thompson 53402ac6454SAndrew Thompson parm->bufsize += (xfer->max_frame_size - 1); 53502ac6454SAndrew Thompson 53602ac6454SAndrew Thompson if (parm->bufsize < xfer->max_frame_size) { 53702ac6454SAndrew Thompson /* length wrapped around */ 53802ac6454SAndrew Thompson parm->err = USB_ERR_INVAL; 53902ac6454SAndrew Thompson goto done; 54002ac6454SAndrew Thompson } 54102ac6454SAndrew Thompson /* subtract remainder */ 54202ac6454SAndrew Thompson 54302ac6454SAndrew Thompson parm->bufsize -= (parm->bufsize % xfer->max_frame_size); 54402ac6454SAndrew Thompson 54502ac6454SAndrew Thompson /* add length of USB device request structure, if any */ 54602ac6454SAndrew Thompson 54702ac6454SAndrew Thompson if (type == UE_CONTROL) { 54802ac6454SAndrew Thompson parm->bufsize += REQ_SIZE; /* SETUP message */ 54902ac6454SAndrew Thompson } 55002ac6454SAndrew Thompson } 55102ac6454SAndrew Thompson xfer->max_data_length = parm->bufsize; 55202ac6454SAndrew Thompson 55302ac6454SAndrew Thompson /* Setup "n_frlengths" and "n_frbuffers" */ 55402ac6454SAndrew Thompson 55502ac6454SAndrew Thompson if (type == UE_ISOCHRONOUS) { 55602ac6454SAndrew Thompson n_frlengths = xfer->nframes; 55702ac6454SAndrew Thompson n_frbuffers = 1; 55802ac6454SAndrew Thompson } else { 55902ac6454SAndrew Thompson 56002ac6454SAndrew Thompson if (type == UE_CONTROL) { 56102ac6454SAndrew Thompson xfer->flags_int.control_xfr = 1; 56202ac6454SAndrew Thompson if (xfer->nframes == 0) { 56302ac6454SAndrew Thompson if (parm->bufsize <= REQ_SIZE) { 56402ac6454SAndrew Thompson /* 56502ac6454SAndrew Thompson * there will never be any data 56602ac6454SAndrew Thompson * stage 56702ac6454SAndrew Thompson */ 56802ac6454SAndrew Thompson xfer->nframes = 1; 56902ac6454SAndrew Thompson } else { 57002ac6454SAndrew Thompson xfer->nframes = 2; 57102ac6454SAndrew Thompson } 57202ac6454SAndrew Thompson } 57302ac6454SAndrew Thompson } else { 57402ac6454SAndrew Thompson if (xfer->nframes == 0) { 57502ac6454SAndrew Thompson xfer->nframes = 1; 57602ac6454SAndrew Thompson } 57702ac6454SAndrew Thompson } 57802ac6454SAndrew Thompson 57902ac6454SAndrew Thompson n_frlengths = xfer->nframes; 58002ac6454SAndrew Thompson n_frbuffers = xfer->nframes; 58102ac6454SAndrew Thompson } 58202ac6454SAndrew Thompson 58302ac6454SAndrew Thompson /* 58402ac6454SAndrew Thompson * check if we have room for the 58502ac6454SAndrew Thompson * USB device request structure: 58602ac6454SAndrew Thompson */ 58702ac6454SAndrew Thompson 58802ac6454SAndrew Thompson if (type == UE_CONTROL) { 58902ac6454SAndrew Thompson 59002ac6454SAndrew Thompson if (xfer->max_data_length < REQ_SIZE) { 59102ac6454SAndrew Thompson /* length wrapped around or too small bufsize */ 59202ac6454SAndrew Thompson parm->err = USB_ERR_INVAL; 59302ac6454SAndrew Thompson goto done; 59402ac6454SAndrew Thompson } 59502ac6454SAndrew Thompson xfer->max_data_length -= REQ_SIZE; 59602ac6454SAndrew Thompson } 59702ac6454SAndrew Thompson /* setup "frlengths" */ 59802ac6454SAndrew Thompson xfer->frlengths = parm->xfer_length_ptr; 59902ac6454SAndrew Thompson parm->xfer_length_ptr += n_frlengths; 60002ac6454SAndrew Thompson 60102ac6454SAndrew Thompson /* setup "frbuffers" */ 60202ac6454SAndrew Thompson xfer->frbuffers = parm->xfer_page_cache_ptr; 60302ac6454SAndrew Thompson parm->xfer_page_cache_ptr += n_frbuffers; 60402ac6454SAndrew Thompson 605ed6d949aSAndrew Thompson /* initialize max frame count */ 606ed6d949aSAndrew Thompson xfer->max_frame_count = xfer->nframes; 607ed6d949aSAndrew Thompson 60802ac6454SAndrew Thompson /* 60902ac6454SAndrew Thompson * check if we need to setup 61002ac6454SAndrew Thompson * a local buffer: 61102ac6454SAndrew Thompson */ 61202ac6454SAndrew Thompson 61302ac6454SAndrew Thompson if (!xfer->flags.ext_buffer) { 61402ac6454SAndrew Thompson 61502ac6454SAndrew Thompson /* align data */ 61602ac6454SAndrew Thompson parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 61702ac6454SAndrew Thompson 61802ac6454SAndrew Thompson if (parm->buf) { 61902ac6454SAndrew Thompson 62002ac6454SAndrew Thompson xfer->local_buffer = 62102ac6454SAndrew Thompson USB_ADD_BYTES(parm->buf, parm->size[0]); 62202ac6454SAndrew Thompson 623ed6d949aSAndrew Thompson usbd_xfer_set_frame_offset(xfer, 0, 0); 62402ac6454SAndrew Thompson 62502ac6454SAndrew Thompson if ((type == UE_CONTROL) && (n_frbuffers > 1)) { 626ed6d949aSAndrew Thompson usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1); 62702ac6454SAndrew Thompson } 62802ac6454SAndrew Thompson } 62902ac6454SAndrew Thompson parm->size[0] += parm->bufsize; 63002ac6454SAndrew Thompson 63102ac6454SAndrew Thompson /* align data again */ 63202ac6454SAndrew Thompson parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 63302ac6454SAndrew Thompson } 63402ac6454SAndrew Thompson /* 63502ac6454SAndrew Thompson * Compute maximum buffer size 63602ac6454SAndrew Thompson */ 63702ac6454SAndrew Thompson 63802ac6454SAndrew Thompson if (parm->bufsize_max < parm->bufsize) { 63902ac6454SAndrew Thompson parm->bufsize_max = parm->bufsize; 64002ac6454SAndrew Thompson } 641bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 64202ac6454SAndrew Thompson if (xfer->flags_int.bdma_enable) { 64302ac6454SAndrew Thompson /* 64402ac6454SAndrew Thompson * Setup "dma_page_ptr". 64502ac6454SAndrew Thompson * 64602ac6454SAndrew Thompson * Proof for formula below: 64702ac6454SAndrew Thompson * 64802ac6454SAndrew Thompson * Assume there are three USB frames having length "a", "b" and 64902ac6454SAndrew Thompson * "c". These USB frames will at maximum need "z" 650760bc48eSAndrew Thompson * "usb_page" structures. "z" is given by: 65102ac6454SAndrew Thompson * 65202ac6454SAndrew Thompson * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) + 65302ac6454SAndrew Thompson * ((c / USB_PAGE_SIZE) + 2); 65402ac6454SAndrew Thompson * 65502ac6454SAndrew Thompson * Constraining "a", "b" and "c" like this: 65602ac6454SAndrew Thompson * 65702ac6454SAndrew Thompson * (a + b + c) <= parm->bufsize 65802ac6454SAndrew Thompson * 65902ac6454SAndrew Thompson * We know that: 66002ac6454SAndrew Thompson * 66102ac6454SAndrew Thompson * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2)); 66202ac6454SAndrew Thompson * 66302ac6454SAndrew Thompson * Here is the general formula: 66402ac6454SAndrew Thompson */ 66502ac6454SAndrew Thompson xfer->dma_page_ptr = parm->dma_page_ptr; 66602ac6454SAndrew Thompson parm->dma_page_ptr += (2 * n_frbuffers); 66702ac6454SAndrew Thompson parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE); 66802ac6454SAndrew Thompson } 669bdc081c6SAndrew Thompson #endif 67002ac6454SAndrew Thompson if (zmps) { 67102ac6454SAndrew Thompson /* correct maximum data length */ 67202ac6454SAndrew Thompson xfer->max_data_length = 0; 67302ac6454SAndrew Thompson } 67402ac6454SAndrew Thompson /* subtract USB frame remainder from "hc_max_frame_size" */ 67502ac6454SAndrew Thompson 676578d0effSAndrew Thompson xfer->max_hc_frame_size = 67702ac6454SAndrew Thompson (parm->hc_max_frame_size - 67802ac6454SAndrew Thompson (parm->hc_max_frame_size % xfer->max_frame_size)); 67902ac6454SAndrew Thompson 680578d0effSAndrew Thompson if (xfer->max_hc_frame_size == 0) { 68102ac6454SAndrew Thompson parm->err = USB_ERR_INVAL; 68202ac6454SAndrew Thompson goto done; 68302ac6454SAndrew Thompson } 68402ac6454SAndrew Thompson 68502ac6454SAndrew Thompson /* initialize frame buffers */ 68602ac6454SAndrew Thompson 68702ac6454SAndrew Thompson if (parm->buf) { 68802ac6454SAndrew Thompson for (x = 0; x != n_frbuffers; x++) { 68902ac6454SAndrew Thompson xfer->frbuffers[x].tag_parent = 69002ac6454SAndrew Thompson &xfer->xroot->dma_parent_tag; 691bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 69202ac6454SAndrew Thompson if (xfer->flags_int.bdma_enable && 69302ac6454SAndrew Thompson (parm->bufsize_max > 0)) { 69402ac6454SAndrew Thompson 695a593f6b8SAndrew Thompson if (usb_pc_dmamap_create( 69602ac6454SAndrew Thompson xfer->frbuffers + x, 69702ac6454SAndrew Thompson parm->bufsize_max)) { 69802ac6454SAndrew Thompson parm->err = USB_ERR_NOMEM; 69902ac6454SAndrew Thompson goto done; 70002ac6454SAndrew Thompson } 70102ac6454SAndrew Thompson } 702bdc081c6SAndrew Thompson #endif 70302ac6454SAndrew Thompson } 70402ac6454SAndrew Thompson } 70502ac6454SAndrew Thompson done: 70602ac6454SAndrew Thompson if (parm->err) { 70702ac6454SAndrew Thompson /* 70802ac6454SAndrew Thompson * Set some dummy values so that we avoid division by zero: 70902ac6454SAndrew Thompson */ 710578d0effSAndrew Thompson xfer->max_hc_frame_size = 1; 71102ac6454SAndrew Thompson xfer->max_frame_size = 1; 71202ac6454SAndrew Thompson xfer->max_packet_size = 1; 71302ac6454SAndrew Thompson xfer->max_data_length = 0; 71402ac6454SAndrew Thompson xfer->nframes = 0; 71502ac6454SAndrew Thompson xfer->max_frame_count = 0; 71602ac6454SAndrew Thompson } 71702ac6454SAndrew Thompson } 71802ac6454SAndrew Thompson 71902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 720a593f6b8SAndrew Thompson * usbd_transfer_setup - setup an array of USB transfers 72102ac6454SAndrew Thompson * 722a593f6b8SAndrew Thompson * NOTE: You must always call "usbd_transfer_unsetup" after calling 723a593f6b8SAndrew Thompson * "usbd_transfer_setup" if success was returned. 72402ac6454SAndrew Thompson * 72502ac6454SAndrew Thompson * The idea is that the USB device driver should pre-allocate all its 72602ac6454SAndrew Thompson * transfers by one call to this function. 72702ac6454SAndrew Thompson * 72802ac6454SAndrew Thompson * Return values: 72902ac6454SAndrew Thompson * 0: Success 73002ac6454SAndrew Thompson * Else: Failure 73102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 732e0a69b51SAndrew Thompson usb_error_t 733a593f6b8SAndrew Thompson usbd_transfer_setup(struct usb_device *udev, 734760bc48eSAndrew Thompson const uint8_t *ifaces, struct usb_xfer **ppxfer, 735760bc48eSAndrew Thompson const struct usb_config *setup_start, uint16_t n_setup, 73602ac6454SAndrew Thompson void *priv_sc, struct mtx *xfer_mtx) 73702ac6454SAndrew Thompson { 738760bc48eSAndrew Thompson struct usb_xfer dummy; 739760bc48eSAndrew Thompson struct usb_setup_params parm; 740760bc48eSAndrew Thompson const struct usb_config *setup_end = setup_start + n_setup; 741760bc48eSAndrew Thompson const struct usb_config *setup; 742ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 743760bc48eSAndrew Thompson struct usb_xfer_root *info; 744760bc48eSAndrew Thompson struct usb_xfer *xfer; 74502ac6454SAndrew Thompson void *buf = NULL; 74602ac6454SAndrew Thompson uint16_t n; 74702ac6454SAndrew Thompson uint16_t refcount; 74802ac6454SAndrew Thompson 74902ac6454SAndrew Thompson parm.err = 0; 75002ac6454SAndrew Thompson refcount = 0; 75102ac6454SAndrew Thompson info = NULL; 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 754a593f6b8SAndrew Thompson "usbd_transfer_setup can sleep!"); 75502ac6454SAndrew Thompson 75602ac6454SAndrew Thompson /* do some checking first */ 75702ac6454SAndrew Thompson 75802ac6454SAndrew Thompson if (n_setup == 0) { 75902ac6454SAndrew Thompson DPRINTFN(6, "setup array has zero length!\n"); 76002ac6454SAndrew Thompson return (USB_ERR_INVAL); 76102ac6454SAndrew Thompson } 76202ac6454SAndrew Thompson if (ifaces == 0) { 76302ac6454SAndrew Thompson DPRINTFN(6, "ifaces array is NULL!\n"); 76402ac6454SAndrew Thompson return (USB_ERR_INVAL); 76502ac6454SAndrew Thompson } 76602ac6454SAndrew Thompson if (xfer_mtx == NULL) { 76702ac6454SAndrew Thompson DPRINTFN(6, "using global lock\n"); 76802ac6454SAndrew Thompson xfer_mtx = &Giant; 76902ac6454SAndrew Thompson } 77002ac6454SAndrew Thompson /* sanity checks */ 77102ac6454SAndrew Thompson for (setup = setup_start, n = 0; 77202ac6454SAndrew Thompson setup != setup_end; setup++, n++) { 773e0a69b51SAndrew Thompson if (setup->bufsize == (usb_frlength_t)-1) { 77402ac6454SAndrew Thompson parm.err = USB_ERR_BAD_BUFSIZE; 77502ac6454SAndrew Thompson DPRINTF("invalid bufsize\n"); 77602ac6454SAndrew Thompson } 7774eae601eSAndrew Thompson if (setup->callback == NULL) { 77802ac6454SAndrew Thompson parm.err = USB_ERR_NO_CALLBACK; 77902ac6454SAndrew Thompson DPRINTF("no callback\n"); 78002ac6454SAndrew Thompson } 78102ac6454SAndrew Thompson ppxfer[n] = NULL; 78202ac6454SAndrew Thompson } 78302ac6454SAndrew Thompson 78402ac6454SAndrew Thompson if (parm.err) { 78502ac6454SAndrew Thompson goto done; 78602ac6454SAndrew Thompson } 78702ac6454SAndrew Thompson bzero(&parm, sizeof(parm)); 78802ac6454SAndrew Thompson 78902ac6454SAndrew Thompson parm.udev = udev; 790a593f6b8SAndrew Thompson parm.speed = usbd_get_speed(udev); 79102ac6454SAndrew Thompson parm.hc_max_packet_count = 1; 79202ac6454SAndrew Thompson 79302ac6454SAndrew Thompson if (parm.speed >= USB_SPEED_MAX) { 79402ac6454SAndrew Thompson parm.err = USB_ERR_INVAL; 79502ac6454SAndrew Thompson goto done; 79602ac6454SAndrew Thompson } 79702ac6454SAndrew Thompson /* setup all transfers */ 79802ac6454SAndrew Thompson 79902ac6454SAndrew Thompson while (1) { 80002ac6454SAndrew Thompson 80102ac6454SAndrew Thompson if (buf) { 80202ac6454SAndrew Thompson /* 803760bc48eSAndrew Thompson * Initialize the "usb_xfer_root" structure, 80402ac6454SAndrew Thompson * which is common for all our USB transfers. 80502ac6454SAndrew Thompson */ 80602ac6454SAndrew Thompson info = USB_ADD_BYTES(buf, 0); 80702ac6454SAndrew Thompson 80802ac6454SAndrew Thompson info->memory_base = buf; 80902ac6454SAndrew Thompson info->memory_size = parm.size[0]; 81002ac6454SAndrew Thompson 811bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 81202ac6454SAndrew Thompson info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]); 81302ac6454SAndrew Thompson info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]); 814bdc081c6SAndrew Thompson #endif 81502ac6454SAndrew Thompson info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]); 81602ac6454SAndrew Thompson info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]); 81702ac6454SAndrew Thompson 8188437751dSAndrew Thompson cv_init(&info->cv_drain, "WDRAIN"); 81902ac6454SAndrew Thompson 82002ac6454SAndrew Thompson info->xfer_mtx = xfer_mtx; 821bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 822a593f6b8SAndrew Thompson usb_dma_tag_setup(&info->dma_parent_tag, 82302ac6454SAndrew Thompson parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag, 824a593f6b8SAndrew Thompson xfer_mtx, &usb_bdma_done_event, 32, parm.dma_tag_max); 825bdc081c6SAndrew Thompson #endif 82602ac6454SAndrew Thompson 82702ac6454SAndrew Thompson info->bus = udev->bus; 82802ac6454SAndrew Thompson info->udev = udev; 82902ac6454SAndrew Thompson 83002ac6454SAndrew Thompson TAILQ_INIT(&info->done_q.head); 831a593f6b8SAndrew Thompson info->done_q.command = &usbd_callback_wrapper; 832bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 83302ac6454SAndrew Thompson TAILQ_INIT(&info->dma_q.head); 834a593f6b8SAndrew Thompson info->dma_q.command = &usb_bdma_work_loop; 835bdc081c6SAndrew Thompson #endif 836a593f6b8SAndrew Thompson info->done_m[0].hdr.pm_callback = &usb_callback_proc; 83702ac6454SAndrew Thompson info->done_m[0].xroot = info; 838a593f6b8SAndrew Thompson info->done_m[1].hdr.pm_callback = &usb_callback_proc; 83902ac6454SAndrew Thompson info->done_m[1].xroot = info; 84002ac6454SAndrew Thompson 841672c9965SAndrew Thompson /* 842672c9965SAndrew Thompson * In device side mode control endpoint 843672c9965SAndrew Thompson * requests need to run from a separate 844672c9965SAndrew Thompson * context, else there is a chance of 845672c9965SAndrew Thompson * deadlock! 846672c9965SAndrew Thompson */ 847a593f6b8SAndrew Thompson if (setup_start == usb_control_ep_cfg) 848672c9965SAndrew Thompson info->done_p = 849672c9965SAndrew Thompson &udev->bus->control_xfer_proc; 850672c9965SAndrew Thompson else if (xfer_mtx == &Giant) 85102ac6454SAndrew Thompson info->done_p = 85202ac6454SAndrew Thompson &udev->bus->giant_callback_proc; 85302ac6454SAndrew Thompson else 85402ac6454SAndrew Thompson info->done_p = 85502ac6454SAndrew Thompson &udev->bus->non_giant_callback_proc; 85602ac6454SAndrew Thompson } 85702ac6454SAndrew Thompson /* reset sizes */ 85802ac6454SAndrew Thompson 85902ac6454SAndrew Thompson parm.size[0] = 0; 86002ac6454SAndrew Thompson parm.buf = buf; 86102ac6454SAndrew Thompson parm.size[0] += sizeof(info[0]); 86202ac6454SAndrew Thompson 86302ac6454SAndrew Thompson for (setup = setup_start, n = 0; 86402ac6454SAndrew Thompson setup != setup_end; setup++, n++) { 86502ac6454SAndrew Thompson 86602ac6454SAndrew Thompson /* skip USB transfers without callbacks: */ 8674eae601eSAndrew Thompson if (setup->callback == NULL) { 86802ac6454SAndrew Thompson continue; 86902ac6454SAndrew Thompson } 87002ac6454SAndrew Thompson /* see if there is a matching endpoint */ 871a593f6b8SAndrew Thompson ep = usbd_get_endpoint(udev, 87202ac6454SAndrew Thompson ifaces[setup->if_index], setup); 87302ac6454SAndrew Thompson 874ae60fdfbSAndrew Thompson if ((ep == NULL) || (ep->methods == NULL)) { 8754eae601eSAndrew Thompson if (setup->flags.no_pipe_ok) 87602ac6454SAndrew Thompson continue; 877f29a0724SAndrew Thompson if ((setup->usb_mode != USB_MODE_DUAL) && 878f29a0724SAndrew Thompson (setup->usb_mode != udev->flags.usb_mode)) 8794eae601eSAndrew Thompson continue; 88002ac6454SAndrew Thompson parm.err = USB_ERR_NO_PIPE; 88102ac6454SAndrew Thompson goto done; 88202ac6454SAndrew Thompson } 88302ac6454SAndrew Thompson 88402ac6454SAndrew Thompson /* align data properly */ 88502ac6454SAndrew Thompson parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 88602ac6454SAndrew Thompson 8874eae601eSAndrew Thompson /* store current setup pointer */ 8884eae601eSAndrew Thompson parm.curr_setup = setup; 8894eae601eSAndrew Thompson 89002ac6454SAndrew Thompson if (buf) { 89102ac6454SAndrew Thompson /* 89202ac6454SAndrew Thompson * Common initialization of the 893760bc48eSAndrew Thompson * "usb_xfer" structure. 89402ac6454SAndrew Thompson */ 89502ac6454SAndrew Thompson xfer = USB_ADD_BYTES(buf, parm.size[0]); 89602ac6454SAndrew Thompson xfer->address = udev->address; 89702ac6454SAndrew Thompson xfer->priv_sc = priv_sc; 89802ac6454SAndrew Thompson xfer->xroot = info; 89902ac6454SAndrew Thompson 900a593f6b8SAndrew Thompson usb_callout_init_mtx(&xfer->timeout_handle, 90102ac6454SAndrew Thompson &udev->bus->bus_mtx, 0); 90202ac6454SAndrew Thompson } else { 90302ac6454SAndrew Thompson /* 90402ac6454SAndrew Thompson * Setup a dummy xfer, hence we are 905760bc48eSAndrew Thompson * writing to the "usb_xfer" 90602ac6454SAndrew Thompson * structure pointed to by "xfer" 90702ac6454SAndrew Thompson * before we have allocated any 90802ac6454SAndrew Thompson * memory: 90902ac6454SAndrew Thompson */ 91002ac6454SAndrew Thompson xfer = &dummy; 91102ac6454SAndrew Thompson bzero(&dummy, sizeof(dummy)); 91202ac6454SAndrew Thompson refcount++; 91302ac6454SAndrew Thompson } 91402ac6454SAndrew Thompson 915ae60fdfbSAndrew Thompson /* set transfer endpoint pointer */ 916ae60fdfbSAndrew Thompson xfer->endpoint = ep; 91702ac6454SAndrew Thompson 918bce32d7bSAndrew Thompson parm.size[0] += sizeof(xfer[0]); 919ae60fdfbSAndrew Thompson parm.methods = xfer->endpoint->methods; 920bce32d7bSAndrew Thompson parm.curr_xfer = xfer; 921bce32d7bSAndrew Thompson 922bce32d7bSAndrew Thompson /* 923bce32d7bSAndrew Thompson * Call the Host or Device controller transfer 924bce32d7bSAndrew Thompson * setup routine: 925bce32d7bSAndrew Thompson */ 926bce32d7bSAndrew Thompson (udev->bus->methods->xfer_setup) (&parm); 927bce32d7bSAndrew Thompson 928bce32d7bSAndrew Thompson /* check for error */ 929bce32d7bSAndrew Thompson if (parm.err) 930bce32d7bSAndrew Thompson goto done; 931bce32d7bSAndrew Thompson 93202ac6454SAndrew Thompson if (buf) { 93302ac6454SAndrew Thompson /* 934ae60fdfbSAndrew Thompson * Increment the endpoint refcount. This 93502ac6454SAndrew Thompson * basically prevents setting a new 93602ac6454SAndrew Thompson * configuration and alternate setting 93702ac6454SAndrew Thompson * when USB transfers are in use on 93802ac6454SAndrew Thompson * the given interface. Search the USB 939ae60fdfbSAndrew Thompson * code for "endpoint->refcount" if you 94002ac6454SAndrew Thompson * want more information. 94102ac6454SAndrew Thompson */ 942ae60fdfbSAndrew Thompson xfer->endpoint->refcount++; 94302ac6454SAndrew Thompson 94402ac6454SAndrew Thompson /* 945bce32d7bSAndrew Thompson * Whenever we set ppxfer[] then we 946bce32d7bSAndrew Thompson * also need to increment the 947bce32d7bSAndrew Thompson * "setup_refcount": 94802ac6454SAndrew Thompson */ 949bce32d7bSAndrew Thompson info->setup_refcount++; 95002ac6454SAndrew Thompson 951bce32d7bSAndrew Thompson /* 952bce32d7bSAndrew Thompson * Transfer is successfully setup and 953bce32d7bSAndrew Thompson * can be used: 954bce32d7bSAndrew Thompson */ 955bce32d7bSAndrew Thompson ppxfer[n] = xfer; 95602ac6454SAndrew Thompson } 95702ac6454SAndrew Thompson } 95802ac6454SAndrew Thompson 95902ac6454SAndrew Thompson if (buf || parm.err) { 96002ac6454SAndrew Thompson goto done; 96102ac6454SAndrew Thompson } 96202ac6454SAndrew Thompson if (refcount == 0) { 96302ac6454SAndrew Thompson /* no transfers - nothing to do ! */ 96402ac6454SAndrew Thompson goto done; 96502ac6454SAndrew Thompson } 96602ac6454SAndrew Thompson /* align data properly */ 96702ac6454SAndrew Thompson parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 96802ac6454SAndrew Thompson 96902ac6454SAndrew Thompson /* store offset temporarily */ 97002ac6454SAndrew Thompson parm.size[1] = parm.size[0]; 97102ac6454SAndrew Thompson 97202ac6454SAndrew Thompson /* 97302ac6454SAndrew Thompson * The number of DMA tags required depends on 97402ac6454SAndrew Thompson * the number of endpoints. The current estimate 97502ac6454SAndrew Thompson * for maximum number of DMA tags per endpoint 97602ac6454SAndrew Thompson * is two. 97702ac6454SAndrew Thompson */ 97802ac6454SAndrew Thompson parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX); 97902ac6454SAndrew Thompson 98002ac6454SAndrew Thompson /* 98102ac6454SAndrew Thompson * DMA tags for QH, TD, Data and more. 98202ac6454SAndrew Thompson */ 98302ac6454SAndrew Thompson parm.dma_tag_max += 8; 98402ac6454SAndrew Thompson 98502ac6454SAndrew Thompson parm.dma_tag_p += parm.dma_tag_max; 98602ac6454SAndrew Thompson 98702ac6454SAndrew Thompson parm.size[0] += ((uint8_t *)parm.dma_tag_p) - 98802ac6454SAndrew Thompson ((uint8_t *)0); 98902ac6454SAndrew Thompson 99002ac6454SAndrew Thompson /* align data properly */ 99102ac6454SAndrew Thompson parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 99202ac6454SAndrew Thompson 99302ac6454SAndrew Thompson /* store offset temporarily */ 99402ac6454SAndrew Thompson parm.size[3] = parm.size[0]; 99502ac6454SAndrew Thompson 99602ac6454SAndrew Thompson parm.size[0] += ((uint8_t *)parm.dma_page_ptr) - 99702ac6454SAndrew Thompson ((uint8_t *)0); 99802ac6454SAndrew Thompson 99902ac6454SAndrew Thompson /* align data properly */ 100002ac6454SAndrew Thompson parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 100102ac6454SAndrew Thompson 100202ac6454SAndrew Thompson /* store offset temporarily */ 100302ac6454SAndrew Thompson parm.size[4] = parm.size[0]; 100402ac6454SAndrew Thompson 100502ac6454SAndrew Thompson parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) - 100602ac6454SAndrew Thompson ((uint8_t *)0); 100702ac6454SAndrew Thompson 100802ac6454SAndrew Thompson /* store end offset temporarily */ 100902ac6454SAndrew Thompson parm.size[5] = parm.size[0]; 101002ac6454SAndrew Thompson 101102ac6454SAndrew Thompson parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) - 101202ac6454SAndrew Thompson ((uint8_t *)0); 101302ac6454SAndrew Thompson 101402ac6454SAndrew Thompson /* store end offset temporarily */ 101502ac6454SAndrew Thompson 101602ac6454SAndrew Thompson parm.size[2] = parm.size[0]; 101702ac6454SAndrew Thompson 101802ac6454SAndrew Thompson /* align data properly */ 101902ac6454SAndrew Thompson parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 102002ac6454SAndrew Thompson 102102ac6454SAndrew Thompson parm.size[6] = parm.size[0]; 102202ac6454SAndrew Thompson 102302ac6454SAndrew Thompson parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) - 102402ac6454SAndrew Thompson ((uint8_t *)0); 102502ac6454SAndrew Thompson 102602ac6454SAndrew Thompson /* align data properly */ 102702ac6454SAndrew Thompson parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 102802ac6454SAndrew Thompson 102902ac6454SAndrew Thompson /* allocate zeroed memory */ 103002ac6454SAndrew Thompson buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO); 103102ac6454SAndrew Thompson 103202ac6454SAndrew Thompson if (buf == NULL) { 103302ac6454SAndrew Thompson parm.err = USB_ERR_NOMEM; 103402ac6454SAndrew Thompson DPRINTFN(0, "cannot allocate memory block for " 103502ac6454SAndrew Thompson "configuration (%d bytes)\n", 103602ac6454SAndrew Thompson parm.size[0]); 103702ac6454SAndrew Thompson goto done; 103802ac6454SAndrew Thompson } 103902ac6454SAndrew Thompson parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]); 104002ac6454SAndrew Thompson parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]); 104102ac6454SAndrew Thompson parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]); 104202ac6454SAndrew Thompson parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]); 104302ac6454SAndrew Thompson parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]); 104402ac6454SAndrew Thompson } 104502ac6454SAndrew Thompson 104602ac6454SAndrew Thompson done: 104702ac6454SAndrew Thompson if (buf) { 104802ac6454SAndrew Thompson if (info->setup_refcount == 0) { 104902ac6454SAndrew Thompson /* 1050a593f6b8SAndrew Thompson * "usbd_transfer_unsetup_sub" will unlock 105102ac6454SAndrew Thompson * the bus mutex before returning ! 105202ac6454SAndrew Thompson */ 105302ac6454SAndrew Thompson USB_BUS_LOCK(info->bus); 105402ac6454SAndrew Thompson 105502ac6454SAndrew Thompson /* something went wrong */ 1056a593f6b8SAndrew Thompson usbd_transfer_unsetup_sub(info, 0); 105702ac6454SAndrew Thompson } 105802ac6454SAndrew Thompson } 105902ac6454SAndrew Thompson if (parm.err) { 1060a593f6b8SAndrew Thompson usbd_transfer_unsetup(ppxfer, n_setup); 106102ac6454SAndrew Thompson } 106202ac6454SAndrew Thompson return (parm.err); 106302ac6454SAndrew Thompson } 106402ac6454SAndrew Thompson 106502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1066a593f6b8SAndrew Thompson * usbd_transfer_unsetup_sub - factored out code 106702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 106802ac6454SAndrew Thompson static void 1069a593f6b8SAndrew Thompson usbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay) 107002ac6454SAndrew Thompson { 1071760bc48eSAndrew Thompson struct usb_page_cache *pc; 107202ac6454SAndrew Thompson 107302ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 107402ac6454SAndrew Thompson 107502ac6454SAndrew Thompson /* wait for any outstanding DMA operations */ 107602ac6454SAndrew Thompson 107702ac6454SAndrew Thompson if (needs_delay) { 1078e0a69b51SAndrew Thompson usb_timeout_t temp; 1079a593f6b8SAndrew Thompson temp = usbd_get_dma_delay(info->bus); 1080a593f6b8SAndrew Thompson usb_pause_mtx(&info->bus->bus_mtx, 108102ac6454SAndrew Thompson USB_MS_TO_TICKS(temp)); 108202ac6454SAndrew Thompson } 108302ac6454SAndrew Thompson 108402ac6454SAndrew Thompson /* make sure that our done messages are not queued anywhere */ 1085a593f6b8SAndrew Thompson usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); 108602ac6454SAndrew Thompson 108702ac6454SAndrew Thompson USB_BUS_UNLOCK(info->bus); 108802ac6454SAndrew Thompson 1089bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 109002ac6454SAndrew Thompson /* free DMA'able memory, if any */ 109102ac6454SAndrew Thompson pc = info->dma_page_cache_start; 109202ac6454SAndrew Thompson while (pc != info->dma_page_cache_end) { 1093a593f6b8SAndrew Thompson usb_pc_free_mem(pc); 109402ac6454SAndrew Thompson pc++; 109502ac6454SAndrew Thompson } 109602ac6454SAndrew Thompson 109702ac6454SAndrew Thompson /* free DMA maps in all "xfer->frbuffers" */ 109802ac6454SAndrew Thompson pc = info->xfer_page_cache_start; 109902ac6454SAndrew Thompson while (pc != info->xfer_page_cache_end) { 1100a593f6b8SAndrew Thompson usb_pc_dmamap_destroy(pc); 110102ac6454SAndrew Thompson pc++; 110202ac6454SAndrew Thompson } 110302ac6454SAndrew Thompson 110402ac6454SAndrew Thompson /* free all DMA tags */ 1105a593f6b8SAndrew Thompson usb_dma_tag_unsetup(&info->dma_parent_tag); 1106bdc081c6SAndrew Thompson #endif 110702ac6454SAndrew Thompson 11088437751dSAndrew Thompson cv_destroy(&info->cv_drain); 110902ac6454SAndrew Thompson 111002ac6454SAndrew Thompson /* 111102ac6454SAndrew Thompson * free the "memory_base" last, hence the "info" structure is 111202ac6454SAndrew Thompson * contained within the "memory_base"! 111302ac6454SAndrew Thompson */ 111402ac6454SAndrew Thompson free(info->memory_base, M_USB); 111502ac6454SAndrew Thompson } 111602ac6454SAndrew Thompson 111702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1118a593f6b8SAndrew Thompson * usbd_transfer_unsetup - unsetup/free an array of USB transfers 111902ac6454SAndrew Thompson * 112002ac6454SAndrew Thompson * NOTE: All USB transfers in progress will get called back passing 112102ac6454SAndrew Thompson * the error code "USB_ERR_CANCELLED" before this function 112202ac6454SAndrew Thompson * returns. 112302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 112402ac6454SAndrew Thompson void 1125a593f6b8SAndrew Thompson usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup) 112602ac6454SAndrew Thompson { 1127760bc48eSAndrew Thompson struct usb_xfer *xfer; 1128760bc48eSAndrew Thompson struct usb_xfer_root *info; 112902ac6454SAndrew Thompson uint8_t needs_delay = 0; 113002ac6454SAndrew Thompson 113102ac6454SAndrew Thompson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1132a593f6b8SAndrew Thompson "usbd_transfer_unsetup can sleep!"); 113302ac6454SAndrew Thompson 113402ac6454SAndrew Thompson while (n_setup--) { 113502ac6454SAndrew Thompson xfer = pxfer[n_setup]; 113602ac6454SAndrew Thompson 1137bce32d7bSAndrew Thompson if (xfer == NULL) 1138bce32d7bSAndrew Thompson continue; 1139bce32d7bSAndrew Thompson 1140bce32d7bSAndrew Thompson info = xfer->xroot; 1141bce32d7bSAndrew Thompson 114202ac6454SAndrew Thompson USB_XFER_LOCK(xfer); 1143bce32d7bSAndrew Thompson USB_BUS_LOCK(info->bus); 114402ac6454SAndrew Thompson 114502ac6454SAndrew Thompson /* 1146bce32d7bSAndrew Thompson * HINT: when you start/stop a transfer, it might be a 1147bce32d7bSAndrew Thompson * good idea to directly use the "pxfer[]" structure: 114802ac6454SAndrew Thompson * 1149a593f6b8SAndrew Thompson * usbd_transfer_start(sc->pxfer[0]); 1150a593f6b8SAndrew Thompson * usbd_transfer_stop(sc->pxfer[0]); 115102ac6454SAndrew Thompson * 1152bce32d7bSAndrew Thompson * That way, if your code has many parts that will not 1153bce32d7bSAndrew Thompson * stop running under the same lock, in other words 1154a593f6b8SAndrew Thompson * "xfer_mtx", the usbd_transfer_start and 1155a593f6b8SAndrew Thompson * usbd_transfer_stop functions will simply return 1156bce32d7bSAndrew Thompson * when they detect a NULL pointer argument. 115702ac6454SAndrew Thompson * 1158bce32d7bSAndrew Thompson * To avoid any races we clear the "pxfer[]" pointer 1159bce32d7bSAndrew Thompson * while holding the private mutex of the driver: 116002ac6454SAndrew Thompson */ 116102ac6454SAndrew Thompson pxfer[n_setup] = NULL; 116202ac6454SAndrew Thompson 1163bce32d7bSAndrew Thompson USB_BUS_UNLOCK(info->bus); 116402ac6454SAndrew Thompson USB_XFER_UNLOCK(xfer); 116502ac6454SAndrew Thompson 1166a593f6b8SAndrew Thompson usbd_transfer_drain(xfer); 116702ac6454SAndrew Thompson 1168bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 1169bce32d7bSAndrew Thompson if (xfer->flags_int.bdma_enable) 117002ac6454SAndrew Thompson needs_delay = 1; 1171bdc081c6SAndrew Thompson #endif 117202ac6454SAndrew Thompson /* 1173ae60fdfbSAndrew Thompson * NOTE: default endpoint does not have an 1174ae60fdfbSAndrew Thompson * interface, even if endpoint->iface_index == 0 117502ac6454SAndrew Thompson */ 1176ae60fdfbSAndrew Thompson xfer->endpoint->refcount--; 117702ac6454SAndrew Thompson 1178a593f6b8SAndrew Thompson usb_callout_drain(&xfer->timeout_handle); 117902ac6454SAndrew Thompson 118002ac6454SAndrew Thompson USB_BUS_LOCK(info->bus); 118102ac6454SAndrew Thompson 1182bce32d7bSAndrew Thompson USB_ASSERT(info->setup_refcount != 0, ("Invalid setup " 118302ac6454SAndrew Thompson "reference count!\n")); 118402ac6454SAndrew Thompson 118502ac6454SAndrew Thompson info->setup_refcount--; 118602ac6454SAndrew Thompson 118702ac6454SAndrew Thompson if (info->setup_refcount == 0) { 1188a593f6b8SAndrew Thompson usbd_transfer_unsetup_sub(info, 118902ac6454SAndrew Thompson needs_delay); 119002ac6454SAndrew Thompson } else { 119102ac6454SAndrew Thompson USB_BUS_UNLOCK(info->bus); 119202ac6454SAndrew Thompson } 119302ac6454SAndrew Thompson } 119402ac6454SAndrew Thompson } 119502ac6454SAndrew Thompson 119602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1197a593f6b8SAndrew Thompson * usbd_control_transfer_init - factored out code 119802ac6454SAndrew Thompson * 119902ac6454SAndrew Thompson * In USB Device Mode we have to wait for the SETUP packet which 1200760bc48eSAndrew Thompson * containst the "struct usb_device_request" structure, before we can 120102ac6454SAndrew Thompson * transfer any data. In USB Host Mode we already have the SETUP 120202ac6454SAndrew Thompson * packet at the moment the USB transfer is started. This leads us to 120302ac6454SAndrew Thompson * having to setup the USB transfer at two different places in 120402ac6454SAndrew Thompson * time. This function just contains factored out control transfer 120502ac6454SAndrew Thompson * initialisation code, so that we don't duplicate the code. 120602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 120702ac6454SAndrew Thompson static void 1208a593f6b8SAndrew Thompson usbd_control_transfer_init(struct usb_xfer *xfer) 120902ac6454SAndrew Thompson { 1210760bc48eSAndrew Thompson struct usb_device_request req; 121102ac6454SAndrew Thompson 121202ac6454SAndrew Thompson /* copy out the USB request header */ 121302ac6454SAndrew Thompson 1214a593f6b8SAndrew Thompson usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); 121502ac6454SAndrew Thompson 121602ac6454SAndrew Thompson /* setup remainder */ 121702ac6454SAndrew Thompson 121802ac6454SAndrew Thompson xfer->flags_int.control_rem = UGETW(req.wLength); 121902ac6454SAndrew Thompson 122002ac6454SAndrew Thompson /* copy direction to endpoint variable */ 122102ac6454SAndrew Thompson 1222ae60fdfbSAndrew Thompson xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT); 1223ae60fdfbSAndrew Thompson xfer->endpointno |= 122402ac6454SAndrew Thompson (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT; 122502ac6454SAndrew Thompson } 122602ac6454SAndrew Thompson 122702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1228ed6d949aSAndrew Thompson * usbd_setup_ctrl_transfer 122902ac6454SAndrew Thompson * 123002ac6454SAndrew Thompson * This function handles initialisation of control transfers. Control 123102ac6454SAndrew Thompson * transfers are special in that regard that they can both transmit 123202ac6454SAndrew Thompson * and receive data. 123302ac6454SAndrew Thompson * 123402ac6454SAndrew Thompson * Return values: 123502ac6454SAndrew Thompson * 0: Success 123602ac6454SAndrew Thompson * Else: Failure 123702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1238ed6d949aSAndrew Thompson static int 1239ed6d949aSAndrew Thompson usbd_setup_ctrl_transfer(struct usb_xfer *xfer) 124002ac6454SAndrew Thompson { 1241e0a69b51SAndrew Thompson usb_frlength_t len; 124202ac6454SAndrew Thompson 124302ac6454SAndrew Thompson /* Check for control endpoint stall */ 1244476183dfSAndrew Thompson if (xfer->flags.stall_pipe && xfer->flags_int.control_act) { 1245476183dfSAndrew Thompson /* the control transfer is no longer active */ 1246476183dfSAndrew Thompson xfer->flags_int.control_stall = 1; 124702ac6454SAndrew Thompson xfer->flags_int.control_act = 0; 1248476183dfSAndrew Thompson } else { 1249476183dfSAndrew Thompson /* don't stall control transfer by default */ 1250476183dfSAndrew Thompson xfer->flags_int.control_stall = 0; 125102ac6454SAndrew Thompson } 1252e1ccac96SAndrew Thompson 1253e1ccac96SAndrew Thompson /* Check for invalid number of frames */ 1254e1ccac96SAndrew Thompson if (xfer->nframes > 2) { 1255e1ccac96SAndrew Thompson /* 1256e1ccac96SAndrew Thompson * If you need to split a control transfer, you 1257e1ccac96SAndrew Thompson * have to do one part at a time. Only with 1258e1ccac96SAndrew Thompson * non-control transfers you can do multiple 1259e1ccac96SAndrew Thompson * parts a time. 1260e1ccac96SAndrew Thompson */ 1261e1ccac96SAndrew Thompson DPRINTFN(0, "Too many frames: %u\n", 1262e1ccac96SAndrew Thompson (unsigned int)xfer->nframes); 1263e1ccac96SAndrew Thompson goto error; 1264e1ccac96SAndrew Thompson } 1265e1ccac96SAndrew Thompson 126602ac6454SAndrew Thompson /* 126702ac6454SAndrew Thompson * Check if there is a control 126802ac6454SAndrew Thompson * transfer in progress: 126902ac6454SAndrew Thompson */ 127002ac6454SAndrew Thompson if (xfer->flags_int.control_act) { 127102ac6454SAndrew Thompson 127202ac6454SAndrew Thompson if (xfer->flags_int.control_hdr) { 127302ac6454SAndrew Thompson 127402ac6454SAndrew Thompson /* clear send header flag */ 127502ac6454SAndrew Thompson 127602ac6454SAndrew Thompson xfer->flags_int.control_hdr = 0; 127702ac6454SAndrew Thompson 127802ac6454SAndrew Thompson /* setup control transfer */ 1279f29a0724SAndrew Thompson if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1280a593f6b8SAndrew Thompson usbd_control_transfer_init(xfer); 128102ac6454SAndrew Thompson } 128202ac6454SAndrew Thompson } 128302ac6454SAndrew Thompson /* get data length */ 128402ac6454SAndrew Thompson 128502ac6454SAndrew Thompson len = xfer->sumlen; 128602ac6454SAndrew Thompson 128702ac6454SAndrew Thompson } else { 128802ac6454SAndrew Thompson 128902ac6454SAndrew Thompson /* the size of the SETUP structure is hardcoded ! */ 129002ac6454SAndrew Thompson 1291760bc48eSAndrew Thompson if (xfer->frlengths[0] != sizeof(struct usb_device_request)) { 129202ac6454SAndrew Thompson DPRINTFN(0, "Wrong framelength %u != %zu\n", 129302ac6454SAndrew Thompson xfer->frlengths[0], sizeof(struct 1294760bc48eSAndrew Thompson usb_device_request)); 129502ac6454SAndrew Thompson goto error; 129602ac6454SAndrew Thompson } 129702ac6454SAndrew Thompson /* check USB mode */ 1298f29a0724SAndrew Thompson if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 129902ac6454SAndrew Thompson 130002ac6454SAndrew Thompson /* check number of frames */ 130102ac6454SAndrew Thompson if (xfer->nframes != 1) { 130202ac6454SAndrew Thompson /* 130302ac6454SAndrew Thompson * We need to receive the setup 130402ac6454SAndrew Thompson * message first so that we know the 130502ac6454SAndrew Thompson * data direction! 130602ac6454SAndrew Thompson */ 130702ac6454SAndrew Thompson DPRINTF("Misconfigured transfer\n"); 130802ac6454SAndrew Thompson goto error; 130902ac6454SAndrew Thompson } 131002ac6454SAndrew Thompson /* 131102ac6454SAndrew Thompson * Set a dummy "control_rem" value. This 131202ac6454SAndrew Thompson * variable will be overwritten later by a 1313a593f6b8SAndrew Thompson * call to "usbd_control_transfer_init()" ! 131402ac6454SAndrew Thompson */ 131502ac6454SAndrew Thompson xfer->flags_int.control_rem = 0xFFFF; 131602ac6454SAndrew Thompson } else { 131702ac6454SAndrew Thompson 131802ac6454SAndrew Thompson /* setup "endpoint" and "control_rem" */ 131902ac6454SAndrew Thompson 1320a593f6b8SAndrew Thompson usbd_control_transfer_init(xfer); 132102ac6454SAndrew Thompson } 132202ac6454SAndrew Thompson 132302ac6454SAndrew Thompson /* set transfer-header flag */ 132402ac6454SAndrew Thompson 132502ac6454SAndrew Thompson xfer->flags_int.control_hdr = 1; 132602ac6454SAndrew Thompson 132702ac6454SAndrew Thompson /* get data length */ 132802ac6454SAndrew Thompson 1329760bc48eSAndrew Thompson len = (xfer->sumlen - sizeof(struct usb_device_request)); 133002ac6454SAndrew Thompson } 133102ac6454SAndrew Thompson 133202ac6454SAndrew Thompson /* check if there is a length mismatch */ 133302ac6454SAndrew Thompson 133402ac6454SAndrew Thompson if (len > xfer->flags_int.control_rem) { 133502ac6454SAndrew Thompson DPRINTFN(0, "Length greater than remaining length!\n"); 133602ac6454SAndrew Thompson goto error; 133702ac6454SAndrew Thompson } 133802ac6454SAndrew Thompson /* check if we are doing a short transfer */ 133902ac6454SAndrew Thompson 134002ac6454SAndrew Thompson if (xfer->flags.force_short_xfer) { 134102ac6454SAndrew Thompson xfer->flags_int.control_rem = 0; 134202ac6454SAndrew Thompson } else { 134302ac6454SAndrew Thompson if ((len != xfer->max_data_length) && 134402ac6454SAndrew Thompson (len != xfer->flags_int.control_rem) && 134502ac6454SAndrew Thompson (xfer->nframes != 1)) { 134602ac6454SAndrew Thompson DPRINTFN(0, "Short control transfer without " 134702ac6454SAndrew Thompson "force_short_xfer set!\n"); 134802ac6454SAndrew Thompson goto error; 134902ac6454SAndrew Thompson } 135002ac6454SAndrew Thompson xfer->flags_int.control_rem -= len; 135102ac6454SAndrew Thompson } 135202ac6454SAndrew Thompson 135302ac6454SAndrew Thompson /* the status part is executed when "control_act" is 0 */ 135402ac6454SAndrew Thompson 135502ac6454SAndrew Thompson if ((xfer->flags_int.control_rem > 0) || 135602ac6454SAndrew Thompson (xfer->flags.manual_status)) { 135702ac6454SAndrew Thompson /* don't execute the STATUS stage yet */ 135802ac6454SAndrew Thompson xfer->flags_int.control_act = 1; 135902ac6454SAndrew Thompson 136002ac6454SAndrew Thompson /* sanity check */ 136102ac6454SAndrew Thompson if ((!xfer->flags_int.control_hdr) && 136202ac6454SAndrew Thompson (xfer->nframes == 1)) { 136302ac6454SAndrew Thompson /* 136402ac6454SAndrew Thompson * This is not a valid operation! 136502ac6454SAndrew Thompson */ 136602ac6454SAndrew Thompson DPRINTFN(0, "Invalid parameter " 136702ac6454SAndrew Thompson "combination\n"); 136802ac6454SAndrew Thompson goto error; 136902ac6454SAndrew Thompson } 137002ac6454SAndrew Thompson } else { 137102ac6454SAndrew Thompson /* time to execute the STATUS stage */ 137202ac6454SAndrew Thompson xfer->flags_int.control_act = 0; 137302ac6454SAndrew Thompson } 137402ac6454SAndrew Thompson return (0); /* success */ 137502ac6454SAndrew Thompson 137602ac6454SAndrew Thompson error: 137702ac6454SAndrew Thompson return (1); /* failure */ 137802ac6454SAndrew Thompson } 137902ac6454SAndrew Thompson 138002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1381a593f6b8SAndrew Thompson * usbd_transfer_submit - start USB hardware for the given transfer 138202ac6454SAndrew Thompson * 138302ac6454SAndrew Thompson * This function should only be called from the USB callback. 138402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 138502ac6454SAndrew Thompson void 1386a593f6b8SAndrew Thompson usbd_transfer_submit(struct usb_xfer *xfer) 138702ac6454SAndrew Thompson { 1388760bc48eSAndrew Thompson struct usb_xfer_root *info; 1389760bc48eSAndrew Thompson struct usb_bus *bus; 1390e0a69b51SAndrew Thompson usb_frcount_t x; 139102ac6454SAndrew Thompson 1392bd216778SAndrew Thompson info = xfer->xroot; 1393bd216778SAndrew Thompson bus = info->bus; 1394bd216778SAndrew Thompson 1395ae60fdfbSAndrew Thompson DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n", 1396ae60fdfbSAndrew Thompson xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? 139702ac6454SAndrew Thompson "read" : "write"); 139802ac6454SAndrew Thompson 139902ac6454SAndrew Thompson #if USB_DEBUG 140002ac6454SAndrew Thompson if (USB_DEBUG_VAR > 0) { 1401bd216778SAndrew Thompson USB_BUS_LOCK(bus); 140202ac6454SAndrew Thompson 1403a593f6b8SAndrew Thompson usb_dump_endpoint(xfer->endpoint); 140402ac6454SAndrew Thompson 1405bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 140602ac6454SAndrew Thompson } 140702ac6454SAndrew Thompson #endif 140802ac6454SAndrew Thompson 140902ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1410bd216778SAndrew Thompson USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED); 141102ac6454SAndrew Thompson 141202ac6454SAndrew Thompson /* Only open the USB transfer once! */ 141302ac6454SAndrew Thompson if (!xfer->flags_int.open) { 141402ac6454SAndrew Thompson xfer->flags_int.open = 1; 141502ac6454SAndrew Thompson 141602ac6454SAndrew Thompson DPRINTF("open\n"); 141702ac6454SAndrew Thompson 1418bd216778SAndrew Thompson USB_BUS_LOCK(bus); 1419ae60fdfbSAndrew Thompson (xfer->endpoint->methods->open) (xfer); 1420bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 142102ac6454SAndrew Thompson } 142202ac6454SAndrew Thompson /* set "transferring" flag */ 142302ac6454SAndrew Thompson xfer->flags_int.transferring = 1; 142402ac6454SAndrew Thompson 14254eae601eSAndrew Thompson #if USB_HAVE_POWERD 142602ac6454SAndrew Thompson /* increment power reference */ 1427a593f6b8SAndrew Thompson usbd_transfer_power_ref(xfer, 1); 14284eae601eSAndrew Thompson #endif 142902ac6454SAndrew Thompson /* 143002ac6454SAndrew Thompson * Check if the transfer is waiting on a queue, most 143102ac6454SAndrew Thompson * frequently the "done_q": 143202ac6454SAndrew Thompson */ 143302ac6454SAndrew Thompson if (xfer->wait_queue) { 1434bd216778SAndrew Thompson USB_BUS_LOCK(bus); 1435a593f6b8SAndrew Thompson usbd_transfer_dequeue(xfer); 1436bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 143702ac6454SAndrew Thompson } 143802ac6454SAndrew Thompson /* clear "did_dma_delay" flag */ 143902ac6454SAndrew Thompson xfer->flags_int.did_dma_delay = 0; 144002ac6454SAndrew Thompson 144102ac6454SAndrew Thompson /* clear "did_close" flag */ 144202ac6454SAndrew Thompson xfer->flags_int.did_close = 0; 144302ac6454SAndrew Thompson 1444bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 144502ac6454SAndrew Thompson /* clear "bdma_setup" flag */ 144602ac6454SAndrew Thompson xfer->flags_int.bdma_setup = 0; 1447bdc081c6SAndrew Thompson #endif 144802ac6454SAndrew Thompson /* by default we cannot cancel any USB transfer immediately */ 144902ac6454SAndrew Thompson xfer->flags_int.can_cancel_immed = 0; 145002ac6454SAndrew Thompson 145102ac6454SAndrew Thompson /* clear lengths and frame counts by default */ 145202ac6454SAndrew Thompson xfer->sumlen = 0; 145302ac6454SAndrew Thompson xfer->actlen = 0; 145402ac6454SAndrew Thompson xfer->aframes = 0; 145502ac6454SAndrew Thompson 145602ac6454SAndrew Thompson /* clear any previous errors */ 145702ac6454SAndrew Thompson xfer->error = 0; 145802ac6454SAndrew Thompson 1459ec8f3127SAndrew Thompson /* Check if the device is still alive */ 1460ec8f3127SAndrew Thompson if (info->udev->state < USB_STATE_POWERED) { 1461ec8f3127SAndrew Thompson USB_BUS_LOCK(bus); 1462f1f88408SAndrew Thompson /* 1463f1f88408SAndrew Thompson * Must return cancelled error code else 1464f1f88408SAndrew Thompson * device drivers can hang. 1465f1f88408SAndrew Thompson */ 1466a593f6b8SAndrew Thompson usbd_transfer_done(xfer, USB_ERR_CANCELLED); 1467ec8f3127SAndrew Thompson USB_BUS_UNLOCK(bus); 1468ec8f3127SAndrew Thompson return; 1469ec8f3127SAndrew Thompson } 147002ac6454SAndrew Thompson 1471ec8f3127SAndrew Thompson /* sanity check */ 147202ac6454SAndrew Thompson if (xfer->nframes == 0) { 147302ac6454SAndrew Thompson if (xfer->flags.stall_pipe) { 147402ac6454SAndrew Thompson /* 147502ac6454SAndrew Thompson * Special case - want to stall without transferring 147602ac6454SAndrew Thompson * any data: 147702ac6454SAndrew Thompson */ 147802ac6454SAndrew Thompson DPRINTF("xfer=%p nframes=0: stall " 147902ac6454SAndrew Thompson "or clear stall!\n", xfer); 1480bd216778SAndrew Thompson USB_BUS_LOCK(bus); 148102ac6454SAndrew Thompson xfer->flags_int.can_cancel_immed = 1; 148202ac6454SAndrew Thompson /* start the transfer */ 1483a593f6b8SAndrew Thompson usb_command_wrapper(&xfer->endpoint->endpoint_q, xfer); 1484bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 148502ac6454SAndrew Thompson return; 148602ac6454SAndrew Thompson } 1487bd216778SAndrew Thompson USB_BUS_LOCK(bus); 1488a593f6b8SAndrew Thompson usbd_transfer_done(xfer, USB_ERR_INVAL); 1489bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 149002ac6454SAndrew Thompson return; 149102ac6454SAndrew Thompson } 149202ac6454SAndrew Thompson /* compute total transfer length */ 149302ac6454SAndrew Thompson 149402ac6454SAndrew Thompson for (x = 0; x != xfer->nframes; x++) { 149502ac6454SAndrew Thompson xfer->sumlen += xfer->frlengths[x]; 149602ac6454SAndrew Thompson if (xfer->sumlen < xfer->frlengths[x]) { 149702ac6454SAndrew Thompson /* length wrapped around */ 1498bd216778SAndrew Thompson USB_BUS_LOCK(bus); 1499a593f6b8SAndrew Thompson usbd_transfer_done(xfer, USB_ERR_INVAL); 1500bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 150102ac6454SAndrew Thompson return; 150202ac6454SAndrew Thompson } 150302ac6454SAndrew Thompson } 150402ac6454SAndrew Thompson 150502ac6454SAndrew Thompson /* clear some internal flags */ 150602ac6454SAndrew Thompson 150702ac6454SAndrew Thompson xfer->flags_int.short_xfer_ok = 0; 150802ac6454SAndrew Thompson xfer->flags_int.short_frames_ok = 0; 150902ac6454SAndrew Thompson 151002ac6454SAndrew Thompson /* check if this is a control transfer */ 151102ac6454SAndrew Thompson 151202ac6454SAndrew Thompson if (xfer->flags_int.control_xfr) { 151302ac6454SAndrew Thompson 1514ed6d949aSAndrew Thompson if (usbd_setup_ctrl_transfer(xfer)) { 1515bd216778SAndrew Thompson USB_BUS_LOCK(bus); 1516a593f6b8SAndrew Thompson usbd_transfer_done(xfer, USB_ERR_STALLED); 1517bd216778SAndrew Thompson USB_BUS_UNLOCK(bus); 151802ac6454SAndrew Thompson return; 151902ac6454SAndrew Thompson } 152002ac6454SAndrew Thompson } 152102ac6454SAndrew Thompson /* 152202ac6454SAndrew Thompson * Setup filtered version of some transfer flags, 152302ac6454SAndrew Thompson * in case of data read direction 152402ac6454SAndrew Thompson */ 152502ac6454SAndrew Thompson if (USB_GET_DATA_ISREAD(xfer)) { 152602ac6454SAndrew Thompson 152702ac6454SAndrew Thompson if (xfer->flags.short_frames_ok) { 152802ac6454SAndrew Thompson xfer->flags_int.short_xfer_ok = 1; 152902ac6454SAndrew Thompson xfer->flags_int.short_frames_ok = 1; 153002ac6454SAndrew Thompson } else if (xfer->flags.short_xfer_ok) { 153102ac6454SAndrew Thompson xfer->flags_int.short_xfer_ok = 1; 1532e1ccac96SAndrew Thompson 1533e1ccac96SAndrew Thompson /* check for control transfer */ 1534e1ccac96SAndrew Thompson if (xfer->flags_int.control_xfr) { 1535e1ccac96SAndrew Thompson /* 1536e1ccac96SAndrew Thompson * 1) Control transfers do not support 1537e1ccac96SAndrew Thompson * reception of multiple short USB 1538e1ccac96SAndrew Thompson * frames in host mode and device side 1539e1ccac96SAndrew Thompson * mode, with exception of: 1540e1ccac96SAndrew Thompson * 1541e1ccac96SAndrew Thompson * 2) Due to sometimes buggy device 1542e1ccac96SAndrew Thompson * side firmware we need to do a 1543e1ccac96SAndrew Thompson * STATUS stage in case of short 1544e1ccac96SAndrew Thompson * control transfers in USB host mode. 1545e1ccac96SAndrew Thompson * The STATUS stage then becomes the 1546e1ccac96SAndrew Thompson * "alt_next" to the DATA stage. 1547e1ccac96SAndrew Thompson */ 1548e1ccac96SAndrew Thompson xfer->flags_int.short_frames_ok = 1; 154902ac6454SAndrew Thompson } 155002ac6454SAndrew Thompson } 155102ac6454SAndrew Thompson } 155202ac6454SAndrew Thompson /* 155302ac6454SAndrew Thompson * Check if BUS-DMA support is enabled and try to load virtual 155402ac6454SAndrew Thompson * buffers into DMA, if any: 155502ac6454SAndrew Thompson */ 1556bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 155702ac6454SAndrew Thompson if (xfer->flags_int.bdma_enable) { 155802ac6454SAndrew Thompson /* insert the USB transfer last in the BUS-DMA queue */ 1559a593f6b8SAndrew Thompson usb_command_wrapper(&xfer->xroot->dma_q, xfer); 156002ac6454SAndrew Thompson return; 156102ac6454SAndrew Thompson } 1562bdc081c6SAndrew Thompson #endif 156302ac6454SAndrew Thompson /* 156402ac6454SAndrew Thompson * Enter the USB transfer into the Host Controller or 156502ac6454SAndrew Thompson * Device Controller schedule: 156602ac6454SAndrew Thompson */ 1567a593f6b8SAndrew Thompson usbd_pipe_enter(xfer); 156802ac6454SAndrew Thompson } 156902ac6454SAndrew Thompson 157002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1571a593f6b8SAndrew Thompson * usbd_pipe_enter - factored out code 157202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 157302ac6454SAndrew Thompson void 1574a593f6b8SAndrew Thompson usbd_pipe_enter(struct usb_xfer *xfer) 157502ac6454SAndrew Thompson { 1576ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 157702ac6454SAndrew Thompson 157802ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 157902ac6454SAndrew Thompson 158002ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 158102ac6454SAndrew Thompson 1582ae60fdfbSAndrew Thompson ep = xfer->endpoint; 158302ac6454SAndrew Thompson 158402ac6454SAndrew Thompson DPRINTF("enter\n"); 158502ac6454SAndrew Thompson 158602ac6454SAndrew Thompson /* enter the transfer */ 1587ae60fdfbSAndrew Thompson (ep->methods->enter) (xfer); 158802ac6454SAndrew Thompson 158902ac6454SAndrew Thompson xfer->flags_int.can_cancel_immed = 1; 15904565300dSAndrew Thompson 159102ac6454SAndrew Thompson /* check for transfer error */ 159202ac6454SAndrew Thompson if (xfer->error) { 159302ac6454SAndrew Thompson /* some error has happened */ 1594a593f6b8SAndrew Thompson usbd_transfer_done(xfer, 0); 159502ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 159602ac6454SAndrew Thompson return; 159702ac6454SAndrew Thompson } 159802ac6454SAndrew Thompson 159902ac6454SAndrew Thompson /* start the transfer */ 1600a593f6b8SAndrew Thompson usb_command_wrapper(&ep->endpoint_q, xfer); 160102ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 160202ac6454SAndrew Thompson } 160302ac6454SAndrew Thompson 160402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1605a593f6b8SAndrew Thompson * usbd_transfer_start - start an USB transfer 160602ac6454SAndrew Thompson * 160702ac6454SAndrew Thompson * NOTE: Calling this function more than one time will only 160802ac6454SAndrew Thompson * result in a single transfer start, until the USB transfer 160902ac6454SAndrew Thompson * completes. 161002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 161102ac6454SAndrew Thompson void 1612a593f6b8SAndrew Thompson usbd_transfer_start(struct usb_xfer *xfer) 161302ac6454SAndrew Thompson { 161402ac6454SAndrew Thompson if (xfer == NULL) { 161502ac6454SAndrew Thompson /* transfer is gone */ 161602ac6454SAndrew Thompson return; 161702ac6454SAndrew Thompson } 161802ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 161902ac6454SAndrew Thompson 162002ac6454SAndrew Thompson /* mark the USB transfer started */ 162102ac6454SAndrew Thompson 162202ac6454SAndrew Thompson if (!xfer->flags_int.started) { 162302ac6454SAndrew Thompson xfer->flags_int.started = 1; 162402ac6454SAndrew Thompson } 162502ac6454SAndrew Thompson /* check if the USB transfer callback is already transferring */ 162602ac6454SAndrew Thompson 162702ac6454SAndrew Thompson if (xfer->flags_int.transferring) { 162802ac6454SAndrew Thompson return; 162902ac6454SAndrew Thompson } 163002ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 163102ac6454SAndrew Thompson /* call the USB transfer callback */ 1632a593f6b8SAndrew Thompson usbd_callback_ss_done_defer(xfer); 163302ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 163402ac6454SAndrew Thompson } 163502ac6454SAndrew Thompson 163602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1637a593f6b8SAndrew Thompson * usbd_transfer_stop - stop an USB transfer 163802ac6454SAndrew Thompson * 163902ac6454SAndrew Thompson * NOTE: Calling this function more than one time will only 164002ac6454SAndrew Thompson * result in a single transfer stop. 164102ac6454SAndrew Thompson * NOTE: When this function returns it is not safe to free nor 1642a593f6b8SAndrew Thompson * reuse any DMA buffers. See "usbd_transfer_drain()". 164302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 164402ac6454SAndrew Thompson void 1645a593f6b8SAndrew Thompson usbd_transfer_stop(struct usb_xfer *xfer) 164602ac6454SAndrew Thompson { 1647ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 164802ac6454SAndrew Thompson 164902ac6454SAndrew Thompson if (xfer == NULL) { 165002ac6454SAndrew Thompson /* transfer is gone */ 165102ac6454SAndrew Thompson return; 165202ac6454SAndrew Thompson } 165302ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 165402ac6454SAndrew Thompson 165502ac6454SAndrew Thompson /* check if the USB transfer was ever opened */ 165602ac6454SAndrew Thompson 165702ac6454SAndrew Thompson if (!xfer->flags_int.open) { 165802ac6454SAndrew Thompson /* nothing to do except clearing the "started" flag */ 165902ac6454SAndrew Thompson xfer->flags_int.started = 0; 166002ac6454SAndrew Thompson return; 166102ac6454SAndrew Thompson } 166202ac6454SAndrew Thompson /* try to stop the current USB transfer */ 166302ac6454SAndrew Thompson 166402ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 166502ac6454SAndrew Thompson xfer->error = USB_ERR_CANCELLED;/* override any previous error */ 166602ac6454SAndrew Thompson /* 166702ac6454SAndrew Thompson * Clear "open" and "started" when both private and USB lock 166802ac6454SAndrew Thompson * is locked so that we don't get a race updating "flags_int" 166902ac6454SAndrew Thompson */ 167002ac6454SAndrew Thompson xfer->flags_int.open = 0; 167102ac6454SAndrew Thompson xfer->flags_int.started = 0; 167202ac6454SAndrew Thompson 167302ac6454SAndrew Thompson /* 167402ac6454SAndrew Thompson * Check if we can cancel the USB transfer immediately. 167502ac6454SAndrew Thompson */ 167602ac6454SAndrew Thompson if (xfer->flags_int.transferring) { 167702ac6454SAndrew Thompson if (xfer->flags_int.can_cancel_immed && 167802ac6454SAndrew Thompson (!xfer->flags_int.did_close)) { 167902ac6454SAndrew Thompson DPRINTF("close\n"); 168002ac6454SAndrew Thompson /* 168102ac6454SAndrew Thompson * The following will lead to an USB_ERR_CANCELLED 168202ac6454SAndrew Thompson * error code being passed to the USB callback. 168302ac6454SAndrew Thompson */ 1684ae60fdfbSAndrew Thompson (xfer->endpoint->methods->close) (xfer); 168502ac6454SAndrew Thompson /* only close once */ 168602ac6454SAndrew Thompson xfer->flags_int.did_close = 1; 168702ac6454SAndrew Thompson } else { 168802ac6454SAndrew Thompson /* need to wait for the next done callback */ 168902ac6454SAndrew Thompson } 169002ac6454SAndrew Thompson } else { 169102ac6454SAndrew Thompson DPRINTF("close\n"); 169202ac6454SAndrew Thompson 169302ac6454SAndrew Thompson /* close here and now */ 1694ae60fdfbSAndrew Thompson (xfer->endpoint->methods->close) (xfer); 169502ac6454SAndrew Thompson 169602ac6454SAndrew Thompson /* 169702ac6454SAndrew Thompson * Any additional DMA delay is done by 1698a593f6b8SAndrew Thompson * "usbd_transfer_unsetup()". 169902ac6454SAndrew Thompson */ 170002ac6454SAndrew Thompson 170102ac6454SAndrew Thompson /* 170202ac6454SAndrew Thompson * Special case. Check if we need to restart a blocked 1703ae60fdfbSAndrew Thompson * endpoint. 170402ac6454SAndrew Thompson */ 1705ae60fdfbSAndrew Thompson ep = xfer->endpoint; 170602ac6454SAndrew Thompson 170702ac6454SAndrew Thompson /* 170802ac6454SAndrew Thompson * If the current USB transfer is completing we need 170902ac6454SAndrew Thompson * to start the next one: 171002ac6454SAndrew Thompson */ 1711ae60fdfbSAndrew Thompson if (ep->endpoint_q.curr == xfer) { 1712a593f6b8SAndrew Thompson usb_command_wrapper(&ep->endpoint_q, NULL); 171302ac6454SAndrew Thompson } 171402ac6454SAndrew Thompson } 171502ac6454SAndrew Thompson 171602ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 171702ac6454SAndrew Thompson } 171802ac6454SAndrew Thompson 171902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1720a593f6b8SAndrew Thompson * usbd_transfer_pending 172102ac6454SAndrew Thompson * 172202ac6454SAndrew Thompson * This function will check if an USB transfer is pending which is a 172302ac6454SAndrew Thompson * little bit complicated! 172402ac6454SAndrew Thompson * Return values: 172502ac6454SAndrew Thompson * 0: Not pending 172602ac6454SAndrew Thompson * 1: Pending: The USB transfer will receive a callback in the future. 172702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 172802ac6454SAndrew Thompson uint8_t 1729a593f6b8SAndrew Thompson usbd_transfer_pending(struct usb_xfer *xfer) 173002ac6454SAndrew Thompson { 1731760bc48eSAndrew Thompson struct usb_xfer_root *info; 1732760bc48eSAndrew Thompson struct usb_xfer_queue *pq; 173302ac6454SAndrew Thompson 173402ac6454SAndrew Thompson if (xfer == NULL) { 173502ac6454SAndrew Thompson /* transfer is gone */ 173602ac6454SAndrew Thompson return (0); 173702ac6454SAndrew Thompson } 173802ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 173902ac6454SAndrew Thompson 174002ac6454SAndrew Thompson if (xfer->flags_int.transferring) { 174102ac6454SAndrew Thompson /* trivial case */ 174202ac6454SAndrew Thompson return (1); 174302ac6454SAndrew Thompson } 174402ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 174502ac6454SAndrew Thompson if (xfer->wait_queue) { 174602ac6454SAndrew Thompson /* we are waiting on a queue somewhere */ 174702ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 174802ac6454SAndrew Thompson return (1); 174902ac6454SAndrew Thompson } 175002ac6454SAndrew Thompson info = xfer->xroot; 175102ac6454SAndrew Thompson pq = &info->done_q; 175202ac6454SAndrew Thompson 175302ac6454SAndrew Thompson if (pq->curr == xfer) { 175402ac6454SAndrew Thompson /* we are currently scheduled for callback */ 175502ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 175602ac6454SAndrew Thompson return (1); 175702ac6454SAndrew Thompson } 175802ac6454SAndrew Thompson /* we are not pending */ 175902ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 176002ac6454SAndrew Thompson return (0); 176102ac6454SAndrew Thompson } 176202ac6454SAndrew Thompson 176302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1764a593f6b8SAndrew Thompson * usbd_transfer_drain 176502ac6454SAndrew Thompson * 176602ac6454SAndrew Thompson * This function will stop the USB transfer and wait for any 176702ac6454SAndrew Thompson * additional BUS-DMA and HW-DMA operations to complete. Buffers that 176802ac6454SAndrew Thompson * are loaded into DMA can safely be freed or reused after that this 176902ac6454SAndrew Thompson * function has returned. 177002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 177102ac6454SAndrew Thompson void 1772a593f6b8SAndrew Thompson usbd_transfer_drain(struct usb_xfer *xfer) 177302ac6454SAndrew Thompson { 177402ac6454SAndrew Thompson WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1775a593f6b8SAndrew Thompson "usbd_transfer_drain can sleep!"); 177602ac6454SAndrew Thompson 177702ac6454SAndrew Thompson if (xfer == NULL) { 177802ac6454SAndrew Thompson /* transfer is gone */ 177902ac6454SAndrew Thompson return; 178002ac6454SAndrew Thompson } 178102ac6454SAndrew Thompson if (xfer->xroot->xfer_mtx != &Giant) { 178202ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED); 178302ac6454SAndrew Thompson } 178402ac6454SAndrew Thompson USB_XFER_LOCK(xfer); 178502ac6454SAndrew Thompson 1786a593f6b8SAndrew Thompson usbd_transfer_stop(xfer); 178702ac6454SAndrew Thompson 1788a593f6b8SAndrew Thompson while (usbd_transfer_pending(xfer)) { 178902ac6454SAndrew Thompson xfer->flags_int.draining = 1; 179002ac6454SAndrew Thompson /* 179102ac6454SAndrew Thompson * Wait until the current outstanding USB 179202ac6454SAndrew Thompson * transfer is complete ! 179302ac6454SAndrew Thompson */ 17948437751dSAndrew Thompson cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); 179502ac6454SAndrew Thompson } 179602ac6454SAndrew Thompson USB_XFER_UNLOCK(xfer); 179702ac6454SAndrew Thompson } 179802ac6454SAndrew Thompson 1799ed6d949aSAndrew Thompson struct usb_page_cache * 1800ed6d949aSAndrew Thompson usbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex) 1801ed6d949aSAndrew Thompson { 1802ed6d949aSAndrew Thompson KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1803ed6d949aSAndrew Thompson 1804ed6d949aSAndrew Thompson return (&xfer->frbuffers[frindex]); 1805ed6d949aSAndrew Thompson } 1806ed6d949aSAndrew Thompson 1807ed6d949aSAndrew Thompson usb_frlength_t 18088f9e0ef9SAndrew Thompson usbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex) 1809ed6d949aSAndrew Thompson { 1810ed6d949aSAndrew Thompson KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1811ed6d949aSAndrew Thompson 1812ed6d949aSAndrew Thompson return (xfer->frlengths[frindex]); 1813ed6d949aSAndrew Thompson } 1814ed6d949aSAndrew Thompson 181502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1816ed6d949aSAndrew Thompson * usbd_xfer_set_frame_data 181702ac6454SAndrew Thompson * 181802ac6454SAndrew Thompson * This function sets the pointer of the buffer that should 181902ac6454SAndrew Thompson * loaded directly into DMA for the given USB frame. Passing "ptr" 182002ac6454SAndrew Thompson * equal to NULL while the corresponding "frlength" is greater 182102ac6454SAndrew Thompson * than zero gives undefined results! 182202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 182302ac6454SAndrew Thompson void 1824ed6d949aSAndrew Thompson usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 1825ed6d949aSAndrew Thompson void *ptr, usb_frlength_t len) 182602ac6454SAndrew Thompson { 1827ed6d949aSAndrew Thompson KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1828ed6d949aSAndrew Thompson 182902ac6454SAndrew Thompson /* set virtual address to load and length */ 183002ac6454SAndrew Thompson xfer->frbuffers[frindex].buffer = ptr; 1831ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, frindex, len); 1832ed6d949aSAndrew Thompson } 1833ed6d949aSAndrew Thompson 1834ed6d949aSAndrew Thompson void 18358f9e0ef9SAndrew Thompson usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 1836ed6d949aSAndrew Thompson void **ptr, int *len) 1837ed6d949aSAndrew Thompson { 1838ed6d949aSAndrew Thompson KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1839ed6d949aSAndrew Thompson 1840ed6d949aSAndrew Thompson if (ptr != NULL) 1841ed6d949aSAndrew Thompson *ptr = xfer->frbuffers[frindex].buffer; 1842ed6d949aSAndrew Thompson if (len != NULL) 1843ed6d949aSAndrew Thompson *len = xfer->frlengths[frindex]; 1844ed6d949aSAndrew Thompson } 1845ed6d949aSAndrew Thompson 1846ed6d949aSAndrew Thompson void 1847ed6d949aSAndrew Thompson usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes, 1848ed6d949aSAndrew Thompson int *nframes) 1849ed6d949aSAndrew Thompson { 1850ed6d949aSAndrew Thompson if (actlen != NULL) 1851ed6d949aSAndrew Thompson *actlen = xfer->actlen; 1852ed6d949aSAndrew Thompson if (sumlen != NULL) 1853ed6d949aSAndrew Thompson *sumlen = xfer->sumlen; 1854ed6d949aSAndrew Thompson if (aframes != NULL) 1855ed6d949aSAndrew Thompson *aframes = xfer->aframes; 1856ed6d949aSAndrew Thompson if (nframes != NULL) 1857ed6d949aSAndrew Thompson *nframes = xfer->nframes; 185802ac6454SAndrew Thompson } 185902ac6454SAndrew Thompson 186002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1861ed6d949aSAndrew Thompson * usbd_xfer_set_frame_offset 186202ac6454SAndrew Thompson * 186302ac6454SAndrew Thompson * This function sets the frame data buffer offset relative to the beginning 186402ac6454SAndrew Thompson * of the USB DMA buffer allocated for this USB transfer. 186502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 186602ac6454SAndrew Thompson void 1867ed6d949aSAndrew Thompson usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset, 1868e0a69b51SAndrew Thompson usb_frcount_t frindex) 186902ac6454SAndrew Thompson { 1870ed6d949aSAndrew Thompson KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " 187102ac6454SAndrew Thompson "when the USB buffer is external!\n")); 1872ed6d949aSAndrew Thompson KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 187302ac6454SAndrew Thompson 187402ac6454SAndrew Thompson /* set virtual address to load */ 187502ac6454SAndrew Thompson xfer->frbuffers[frindex].buffer = 187602ac6454SAndrew Thompson USB_ADD_BYTES(xfer->local_buffer, offset); 187702ac6454SAndrew Thompson } 187802ac6454SAndrew Thompson 1879ed6d949aSAndrew Thompson void 1880ed6d949aSAndrew Thompson usbd_xfer_set_interval(struct usb_xfer *xfer, int i) 1881ed6d949aSAndrew Thompson { 1882ed6d949aSAndrew Thompson xfer->interval = i; 1883ed6d949aSAndrew Thompson } 1884ed6d949aSAndrew Thompson 1885ed6d949aSAndrew Thompson void 1886ed6d949aSAndrew Thompson usbd_xfer_set_timeout(struct usb_xfer *xfer, int t) 1887ed6d949aSAndrew Thompson { 1888ed6d949aSAndrew Thompson xfer->timeout = t; 1889ed6d949aSAndrew Thompson } 1890ed6d949aSAndrew Thompson 1891ed6d949aSAndrew Thompson void 1892ed6d949aSAndrew Thompson usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n) 1893ed6d949aSAndrew Thompson { 1894ed6d949aSAndrew Thompson xfer->nframes = n; 1895ed6d949aSAndrew Thompson } 1896ed6d949aSAndrew Thompson 1897ed6d949aSAndrew Thompson usb_frcount_t 1898ed6d949aSAndrew Thompson usbd_xfer_max_frames(struct usb_xfer *xfer) 1899ed6d949aSAndrew Thompson { 1900ed6d949aSAndrew Thompson return (xfer->max_frame_count); 1901ed6d949aSAndrew Thompson } 1902ed6d949aSAndrew Thompson 1903ed6d949aSAndrew Thompson usb_frlength_t 1904ed6d949aSAndrew Thompson usbd_xfer_max_len(struct usb_xfer *xfer) 1905ed6d949aSAndrew Thompson { 1906ed6d949aSAndrew Thompson return (xfer->max_data_length); 1907ed6d949aSAndrew Thompson } 1908ed6d949aSAndrew Thompson 1909ed6d949aSAndrew Thompson usb_frlength_t 1910ed6d949aSAndrew Thompson usbd_xfer_max_framelen(struct usb_xfer *xfer) 1911ed6d949aSAndrew Thompson { 1912ed6d949aSAndrew Thompson return (xfer->max_frame_size); 1913ed6d949aSAndrew Thompson } 1914ed6d949aSAndrew Thompson 1915ed6d949aSAndrew Thompson void 1916ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex, 1917ed6d949aSAndrew Thompson usb_frlength_t len) 1918ed6d949aSAndrew Thompson { 1919ed6d949aSAndrew Thompson KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1920ed6d949aSAndrew Thompson 1921ed6d949aSAndrew Thompson xfer->frlengths[frindex] = len; 1922ed6d949aSAndrew Thompson } 1923ed6d949aSAndrew Thompson 192402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1925a593f6b8SAndrew Thompson * usb_callback_proc - factored out code 192602ac6454SAndrew Thompson * 192702ac6454SAndrew Thompson * This function performs USB callbacks. 192802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 192902ac6454SAndrew Thompson static void 1930a593f6b8SAndrew Thompson usb_callback_proc(struct usb_proc_msg *_pm) 193102ac6454SAndrew Thompson { 1932760bc48eSAndrew Thompson struct usb_done_msg *pm = (void *)_pm; 1933760bc48eSAndrew Thompson struct usb_xfer_root *info = pm->xroot; 193402ac6454SAndrew Thompson 193502ac6454SAndrew Thompson /* Change locking order */ 193602ac6454SAndrew Thompson USB_BUS_UNLOCK(info->bus); 193702ac6454SAndrew Thompson 193802ac6454SAndrew Thompson /* 193902ac6454SAndrew Thompson * We exploit the fact that the mutex is the same for all 194002ac6454SAndrew Thompson * callbacks that will be called from this thread: 194102ac6454SAndrew Thompson */ 194202ac6454SAndrew Thompson mtx_lock(info->xfer_mtx); 194302ac6454SAndrew Thompson USB_BUS_LOCK(info->bus); 194402ac6454SAndrew Thompson 194502ac6454SAndrew Thompson /* Continue where we lost track */ 1946a593f6b8SAndrew Thompson usb_command_wrapper(&info->done_q, 194702ac6454SAndrew Thompson info->done_q.curr); 194802ac6454SAndrew Thompson 194902ac6454SAndrew Thompson mtx_unlock(info->xfer_mtx); 195002ac6454SAndrew Thompson } 195102ac6454SAndrew Thompson 195202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1953a593f6b8SAndrew Thompson * usbd_callback_ss_done_defer 195402ac6454SAndrew Thompson * 195502ac6454SAndrew Thompson * This function will defer the start, stop and done callback to the 195602ac6454SAndrew Thompson * correct thread. 195702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 195802ac6454SAndrew Thompson static void 1959a593f6b8SAndrew Thompson usbd_callback_ss_done_defer(struct usb_xfer *xfer) 196002ac6454SAndrew Thompson { 1961760bc48eSAndrew Thompson struct usb_xfer_root *info = xfer->xroot; 1962760bc48eSAndrew Thompson struct usb_xfer_queue *pq = &info->done_q; 196302ac6454SAndrew Thompson 196402ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 196502ac6454SAndrew Thompson 196602ac6454SAndrew Thompson if (pq->curr != xfer) { 1967a593f6b8SAndrew Thompson usbd_transfer_enqueue(pq, xfer); 196802ac6454SAndrew Thompson } 196902ac6454SAndrew Thompson if (!pq->recurse_1) { 197002ac6454SAndrew Thompson 197102ac6454SAndrew Thompson /* 197202ac6454SAndrew Thompson * We have to postpone the callback due to the fact we 197302ac6454SAndrew Thompson * will have a Lock Order Reversal, LOR, if we try to 197402ac6454SAndrew Thompson * proceed ! 197502ac6454SAndrew Thompson */ 1976a593f6b8SAndrew Thompson if (usb_proc_msignal(info->done_p, 197702ac6454SAndrew Thompson &info->done_m[0], &info->done_m[1])) { 197802ac6454SAndrew Thompson /* ignore */ 197902ac6454SAndrew Thompson } 198002ac6454SAndrew Thompson } else { 198102ac6454SAndrew Thompson /* clear second recurse flag */ 198202ac6454SAndrew Thompson pq->recurse_2 = 0; 198302ac6454SAndrew Thompson } 198402ac6454SAndrew Thompson return; 198502ac6454SAndrew Thompson 198602ac6454SAndrew Thompson } 198702ac6454SAndrew Thompson 198802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1989a593f6b8SAndrew Thompson * usbd_callback_wrapper 199002ac6454SAndrew Thompson * 199102ac6454SAndrew Thompson * This is a wrapper for USB callbacks. This wrapper does some 199202ac6454SAndrew Thompson * auto-magic things like figuring out if we can call the callback 199302ac6454SAndrew Thompson * directly from the current context or if we need to wakeup the 199402ac6454SAndrew Thompson * interrupt process. 199502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 199602ac6454SAndrew Thompson static void 1997a593f6b8SAndrew Thompson usbd_callback_wrapper(struct usb_xfer_queue *pq) 199802ac6454SAndrew Thompson { 1999760bc48eSAndrew Thompson struct usb_xfer *xfer = pq->curr; 2000760bc48eSAndrew Thompson struct usb_xfer_root *info = xfer->xroot; 200102ac6454SAndrew Thompson 20024565300dSAndrew Thompson USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 20034565300dSAndrew Thompson if (!mtx_owned(info->xfer_mtx)) { 200402ac6454SAndrew Thompson /* 200502ac6454SAndrew Thompson * Cases that end up here: 200602ac6454SAndrew Thompson * 200702ac6454SAndrew Thompson * 5) HW interrupt done callback or other source. 200802ac6454SAndrew Thompson */ 200902ac6454SAndrew Thompson DPRINTFN(3, "case 5\n"); 201002ac6454SAndrew Thompson 201102ac6454SAndrew Thompson /* 201202ac6454SAndrew Thompson * We have to postpone the callback due to the fact we 201302ac6454SAndrew Thompson * will have a Lock Order Reversal, LOR, if we try to 201402ac6454SAndrew Thompson * proceed ! 201502ac6454SAndrew Thompson */ 2016a593f6b8SAndrew Thompson if (usb_proc_msignal(info->done_p, 201702ac6454SAndrew Thompson &info->done_m[0], &info->done_m[1])) { 201802ac6454SAndrew Thompson /* ignore */ 201902ac6454SAndrew Thompson } 202002ac6454SAndrew Thompson return; 202102ac6454SAndrew Thompson } 202202ac6454SAndrew Thompson /* 202302ac6454SAndrew Thompson * Cases that end up here: 202402ac6454SAndrew Thompson * 202502ac6454SAndrew Thompson * 1) We are starting a transfer 202602ac6454SAndrew Thompson * 2) We are prematurely calling back a transfer 202702ac6454SAndrew Thompson * 3) We are stopping a transfer 202802ac6454SAndrew Thompson * 4) We are doing an ordinary callback 202902ac6454SAndrew Thompson */ 203002ac6454SAndrew Thompson DPRINTFN(3, "case 1-4\n"); 203102ac6454SAndrew Thompson /* get next USB transfer in the queue */ 203202ac6454SAndrew Thompson info->done_q.curr = NULL; 203302ac6454SAndrew Thompson 20344565300dSAndrew Thompson USB_BUS_UNLOCK(info->bus); 20354565300dSAndrew Thompson USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED); 203602ac6454SAndrew Thompson 203702ac6454SAndrew Thompson /* set correct USB state for callback */ 203802ac6454SAndrew Thompson if (!xfer->flags_int.transferring) { 2039a593f6b8SAndrew Thompson xfer->usb_state = USB_ST_SETUP; 204002ac6454SAndrew Thompson if (!xfer->flags_int.started) { 204102ac6454SAndrew Thompson /* we got stopped before we even got started */ 20424565300dSAndrew Thompson USB_BUS_LOCK(info->bus); 204302ac6454SAndrew Thompson goto done; 204402ac6454SAndrew Thompson } 204502ac6454SAndrew Thompson } else { 204602ac6454SAndrew Thompson 2047a593f6b8SAndrew Thompson if (usbd_callback_wrapper_sub(xfer)) { 204802ac6454SAndrew Thompson /* the callback has been deferred */ 20494565300dSAndrew Thompson USB_BUS_LOCK(info->bus); 205002ac6454SAndrew Thompson goto done; 205102ac6454SAndrew Thompson } 20524eae601eSAndrew Thompson #if USB_HAVE_POWERD 205302ac6454SAndrew Thompson /* decrement power reference */ 2054a593f6b8SAndrew Thompson usbd_transfer_power_ref(xfer, -1); 20554eae601eSAndrew Thompson #endif 205602ac6454SAndrew Thompson xfer->flags_int.transferring = 0; 205702ac6454SAndrew Thompson 205802ac6454SAndrew Thompson if (xfer->error) { 2059a593f6b8SAndrew Thompson xfer->usb_state = USB_ST_ERROR; 206002ac6454SAndrew Thompson } else { 206102ac6454SAndrew Thompson /* set transferred state */ 2062a593f6b8SAndrew Thompson xfer->usb_state = USB_ST_TRANSFERRED; 2063bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 206402ac6454SAndrew Thompson /* sync DMA memory, if any */ 206502ac6454SAndrew Thompson if (xfer->flags_int.bdma_enable && 206602ac6454SAndrew Thompson (!xfer->flags_int.bdma_no_post_sync)) { 2067a593f6b8SAndrew Thompson usb_bdma_post_sync(xfer); 206802ac6454SAndrew Thompson } 2069bdc081c6SAndrew Thompson #endif 207002ac6454SAndrew Thompson } 207102ac6454SAndrew Thompson } 207202ac6454SAndrew Thompson 207302ac6454SAndrew Thompson /* call processing routine */ 2074ed6d949aSAndrew Thompson (xfer->callback) (xfer, xfer->error); 207502ac6454SAndrew Thompson 207602ac6454SAndrew Thompson /* pickup the USB mutex again */ 20774565300dSAndrew Thompson USB_BUS_LOCK(info->bus); 207802ac6454SAndrew Thompson 207902ac6454SAndrew Thompson /* 208002ac6454SAndrew Thompson * Check if we got started after that we got cancelled, but 208102ac6454SAndrew Thompson * before we managed to do the callback. 208202ac6454SAndrew Thompson */ 208302ac6454SAndrew Thompson if ((!xfer->flags_int.open) && 208402ac6454SAndrew Thompson (xfer->flags_int.started) && 2085a593f6b8SAndrew Thompson (xfer->usb_state == USB_ST_ERROR)) { 208602ac6454SAndrew Thompson /* try to loop, but not recursivly */ 2087a593f6b8SAndrew Thompson usb_command_wrapper(&info->done_q, xfer); 208802ac6454SAndrew Thompson return; 208902ac6454SAndrew Thompson } 209002ac6454SAndrew Thompson 209102ac6454SAndrew Thompson done: 209202ac6454SAndrew Thompson /* 209302ac6454SAndrew Thompson * Check if we are draining. 209402ac6454SAndrew Thompson */ 209502ac6454SAndrew Thompson if (xfer->flags_int.draining && 209602ac6454SAndrew Thompson (!xfer->flags_int.transferring)) { 2097a593f6b8SAndrew Thompson /* "usbd_transfer_drain()" is waiting for end of transfer */ 209802ac6454SAndrew Thompson xfer->flags_int.draining = 0; 20998437751dSAndrew Thompson cv_broadcast(&info->cv_drain); 210002ac6454SAndrew Thompson } 210102ac6454SAndrew Thompson 210202ac6454SAndrew Thompson /* do the next callback, if any */ 2103a593f6b8SAndrew Thompson usb_command_wrapper(&info->done_q, 210402ac6454SAndrew Thompson info->done_q.curr); 210502ac6454SAndrew Thompson } 210602ac6454SAndrew Thompson 210702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2108a593f6b8SAndrew Thompson * usb_dma_delay_done_cb 210902ac6454SAndrew Thompson * 211002ac6454SAndrew Thompson * This function is called when the DMA delay has been exectuded, and 211102ac6454SAndrew Thompson * will make sure that the callback is called to complete the USB 211202ac6454SAndrew Thompson * transfer. This code path is ususally only used when there is an USB 211302ac6454SAndrew Thompson * error like USB_ERR_CANCELLED. 211402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 211502ac6454SAndrew Thompson static void 2116a593f6b8SAndrew Thompson usb_dma_delay_done_cb(void *arg) 211702ac6454SAndrew Thompson { 2118760bc48eSAndrew Thompson struct usb_xfer *xfer = arg; 211902ac6454SAndrew Thompson 212002ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 212102ac6454SAndrew Thompson 212202ac6454SAndrew Thompson DPRINTFN(3, "Completed %p\n", xfer); 212302ac6454SAndrew Thompson 212402ac6454SAndrew Thompson /* queue callback for execution, again */ 2125a593f6b8SAndrew Thompson usbd_transfer_done(xfer, 0); 212602ac6454SAndrew Thompson } 212702ac6454SAndrew Thompson 212802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2129a593f6b8SAndrew Thompson * usbd_transfer_dequeue 213002ac6454SAndrew Thompson * 213102ac6454SAndrew Thompson * - This function is used to remove an USB transfer from a USB 213202ac6454SAndrew Thompson * transfer queue. 213302ac6454SAndrew Thompson * 213402ac6454SAndrew Thompson * - This function can be called multiple times in a row. 213502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 213602ac6454SAndrew Thompson void 2137a593f6b8SAndrew Thompson usbd_transfer_dequeue(struct usb_xfer *xfer) 213802ac6454SAndrew Thompson { 2139760bc48eSAndrew Thompson struct usb_xfer_queue *pq; 214002ac6454SAndrew Thompson 214102ac6454SAndrew Thompson pq = xfer->wait_queue; 214202ac6454SAndrew Thompson if (pq) { 214302ac6454SAndrew Thompson TAILQ_REMOVE(&pq->head, xfer, wait_entry); 214402ac6454SAndrew Thompson xfer->wait_queue = NULL; 214502ac6454SAndrew Thompson } 214602ac6454SAndrew Thompson } 214702ac6454SAndrew Thompson 214802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2149a593f6b8SAndrew Thompson * usbd_transfer_enqueue 215002ac6454SAndrew Thompson * 215102ac6454SAndrew Thompson * - This function is used to insert an USB transfer into a USB * 215202ac6454SAndrew Thompson * transfer queue. 215302ac6454SAndrew Thompson * 215402ac6454SAndrew Thompson * - This function can be called multiple times in a row. 215502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 215602ac6454SAndrew Thompson void 2157a593f6b8SAndrew Thompson usbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer) 215802ac6454SAndrew Thompson { 215902ac6454SAndrew Thompson /* 216002ac6454SAndrew Thompson * Insert the USB transfer into the queue, if it is not 216102ac6454SAndrew Thompson * already on a USB transfer queue: 216202ac6454SAndrew Thompson */ 216302ac6454SAndrew Thompson if (xfer->wait_queue == NULL) { 216402ac6454SAndrew Thompson xfer->wait_queue = pq; 216502ac6454SAndrew Thompson TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); 216602ac6454SAndrew Thompson } 216702ac6454SAndrew Thompson } 216802ac6454SAndrew Thompson 216902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2170a593f6b8SAndrew Thompson * usbd_transfer_done 217102ac6454SAndrew Thompson * 217202ac6454SAndrew Thompson * - This function is used to remove an USB transfer from the busdma, 217302ac6454SAndrew Thompson * pipe or interrupt queue. 217402ac6454SAndrew Thompson * 217502ac6454SAndrew Thompson * - This function is used to queue the USB transfer on the done 217602ac6454SAndrew Thompson * queue. 217702ac6454SAndrew Thompson * 217802ac6454SAndrew Thompson * - This function is used to stop any USB transfer timeouts. 217902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 218002ac6454SAndrew Thompson void 2181a593f6b8SAndrew Thompson usbd_transfer_done(struct usb_xfer *xfer, usb_error_t error) 218202ac6454SAndrew Thompson { 218302ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 218402ac6454SAndrew Thompson 2185a593f6b8SAndrew Thompson DPRINTF("err=%s\n", usbd_errstr(error)); 218602ac6454SAndrew Thompson 218702ac6454SAndrew Thompson /* 218802ac6454SAndrew Thompson * If we are not transferring then just return. 218902ac6454SAndrew Thompson * This can happen during transfer cancel. 219002ac6454SAndrew Thompson */ 219102ac6454SAndrew Thompson if (!xfer->flags_int.transferring) { 219202ac6454SAndrew Thompson DPRINTF("not transferring\n"); 219302ac6454SAndrew Thompson return; 219402ac6454SAndrew Thompson } 219502ac6454SAndrew Thompson /* only set transfer error if not already set */ 219602ac6454SAndrew Thompson if (!xfer->error) { 219702ac6454SAndrew Thompson xfer->error = error; 219802ac6454SAndrew Thompson } 219902ac6454SAndrew Thompson /* stop any callouts */ 2200a593f6b8SAndrew Thompson usb_callout_stop(&xfer->timeout_handle); 220102ac6454SAndrew Thompson 220202ac6454SAndrew Thompson /* 220302ac6454SAndrew Thompson * If we are waiting on a queue, just remove the USB transfer 220402ac6454SAndrew Thompson * from the queue, if any. We should have the required locks 220502ac6454SAndrew Thompson * locked to do the remove when this function is called. 220602ac6454SAndrew Thompson */ 2207a593f6b8SAndrew Thompson usbd_transfer_dequeue(xfer); 220802ac6454SAndrew Thompson 2209bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 221002ac6454SAndrew Thompson if (mtx_owned(xfer->xroot->xfer_mtx)) { 2211760bc48eSAndrew Thompson struct usb_xfer_queue *pq; 2212bdc081c6SAndrew Thompson 221302ac6454SAndrew Thompson /* 221402ac6454SAndrew Thompson * If the private USB lock is not locked, then we assume 221502ac6454SAndrew Thompson * that the BUS-DMA load stage has been passed: 221602ac6454SAndrew Thompson */ 221702ac6454SAndrew Thompson pq = &xfer->xroot->dma_q; 221802ac6454SAndrew Thompson 221902ac6454SAndrew Thompson if (pq->curr == xfer) { 222002ac6454SAndrew Thompson /* start the next BUS-DMA load, if any */ 2221a593f6b8SAndrew Thompson usb_command_wrapper(pq, NULL); 222202ac6454SAndrew Thompson } 222302ac6454SAndrew Thompson } 2224bdc081c6SAndrew Thompson #endif 222502ac6454SAndrew Thompson /* keep some statistics */ 222602ac6454SAndrew Thompson if (xfer->error) { 222702ac6454SAndrew Thompson xfer->xroot->bus->stats_err.uds_requests 2228ae60fdfbSAndrew Thompson [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; 222902ac6454SAndrew Thompson } else { 223002ac6454SAndrew Thompson xfer->xroot->bus->stats_ok.uds_requests 2231ae60fdfbSAndrew Thompson [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; 223202ac6454SAndrew Thompson } 223302ac6454SAndrew Thompson 223402ac6454SAndrew Thompson /* call the USB transfer callback */ 2235a593f6b8SAndrew Thompson usbd_callback_ss_done_defer(xfer); 223602ac6454SAndrew Thompson } 223702ac6454SAndrew Thompson 223802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2239a593f6b8SAndrew Thompson * usbd_transfer_start_cb 224002ac6454SAndrew Thompson * 224102ac6454SAndrew Thompson * This function is called to start the USB transfer when 224202ac6454SAndrew Thompson * "xfer->interval" is greater than zero, and and the endpoint type is 224302ac6454SAndrew Thompson * BULK or CONTROL. 224402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 224502ac6454SAndrew Thompson static void 2246a593f6b8SAndrew Thompson usbd_transfer_start_cb(void *arg) 224702ac6454SAndrew Thompson { 2248760bc48eSAndrew Thompson struct usb_xfer *xfer = arg; 2249ae60fdfbSAndrew Thompson struct usb_endpoint *ep = xfer->endpoint; 225002ac6454SAndrew Thompson 225102ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 225202ac6454SAndrew Thompson 225302ac6454SAndrew Thompson DPRINTF("start\n"); 225402ac6454SAndrew Thompson 225502ac6454SAndrew Thompson /* start the transfer */ 2256ae60fdfbSAndrew Thompson (ep->methods->start) (xfer); 225702ac6454SAndrew Thompson 225802ac6454SAndrew Thompson xfer->flags_int.can_cancel_immed = 1; 22594565300dSAndrew Thompson 22604565300dSAndrew Thompson /* check for error */ 226102ac6454SAndrew Thompson if (xfer->error) { 226202ac6454SAndrew Thompson /* some error has happened */ 2263a593f6b8SAndrew Thompson usbd_transfer_done(xfer, 0); 226402ac6454SAndrew Thompson } 226502ac6454SAndrew Thompson } 226602ac6454SAndrew Thompson 226702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2268ed6d949aSAndrew Thompson * usbd_xfer_set_stall 226902ac6454SAndrew Thompson * 227002ac6454SAndrew Thompson * This function is used to set the stall flag outside the 227102ac6454SAndrew Thompson * callback. This function is NULL safe. 227202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 227302ac6454SAndrew Thompson void 2274ed6d949aSAndrew Thompson usbd_xfer_set_stall(struct usb_xfer *xfer) 227502ac6454SAndrew Thompson { 227602ac6454SAndrew Thompson if (xfer == NULL) { 227702ac6454SAndrew Thompson /* tearing down */ 227802ac6454SAndrew Thompson return; 227902ac6454SAndrew Thompson } 228002ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 228102ac6454SAndrew Thompson 228202ac6454SAndrew Thompson /* avoid any races by locking the USB mutex */ 228302ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 228402ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 228502ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 228602ac6454SAndrew Thompson } 228702ac6454SAndrew Thompson 2288ed6d949aSAndrew Thompson int 2289ed6d949aSAndrew Thompson usbd_xfer_is_stalled(struct usb_xfer *xfer) 2290ed6d949aSAndrew Thompson { 2291ed6d949aSAndrew Thompson return (xfer->endpoint->is_stalled); 2292ed6d949aSAndrew Thompson } 2293ed6d949aSAndrew Thompson 229402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2295a593f6b8SAndrew Thompson * usbd_transfer_clear_stall 229602ac6454SAndrew Thompson * 229702ac6454SAndrew Thompson * This function is used to clear the stall flag outside the 229802ac6454SAndrew Thompson * callback. This function is NULL safe. 229902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 230002ac6454SAndrew Thompson void 2301a593f6b8SAndrew Thompson usbd_transfer_clear_stall(struct usb_xfer *xfer) 230202ac6454SAndrew Thompson { 230302ac6454SAndrew Thompson if (xfer == NULL) { 230402ac6454SAndrew Thompson /* tearing down */ 230502ac6454SAndrew Thompson return; 230602ac6454SAndrew Thompson } 230702ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 230802ac6454SAndrew Thompson 230902ac6454SAndrew Thompson /* avoid any races by locking the USB mutex */ 231002ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 231102ac6454SAndrew Thompson 231202ac6454SAndrew Thompson xfer->flags.stall_pipe = 0; 231302ac6454SAndrew Thompson 231402ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 231502ac6454SAndrew Thompson } 231602ac6454SAndrew Thompson 231702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2318a593f6b8SAndrew Thompson * usbd_pipe_start 231902ac6454SAndrew Thompson * 232002ac6454SAndrew Thompson * This function is used to add an USB transfer to the pipe transfer list. 232102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 232202ac6454SAndrew Thompson void 2323a593f6b8SAndrew Thompson usbd_pipe_start(struct usb_xfer_queue *pq) 232402ac6454SAndrew Thompson { 2325ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 2326760bc48eSAndrew Thompson struct usb_xfer *xfer; 232702ac6454SAndrew Thompson uint8_t type; 232802ac6454SAndrew Thompson 232902ac6454SAndrew Thompson xfer = pq->curr; 2330ae60fdfbSAndrew Thompson ep = xfer->endpoint; 233102ac6454SAndrew Thompson 233202ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 233302ac6454SAndrew Thompson 233402ac6454SAndrew Thompson /* 2335ae60fdfbSAndrew Thompson * If the endpoint is already stalled we do nothing ! 233602ac6454SAndrew Thompson */ 2337ae60fdfbSAndrew Thompson if (ep->is_stalled) { 233802ac6454SAndrew Thompson return; 233902ac6454SAndrew Thompson } 234002ac6454SAndrew Thompson /* 2341ae60fdfbSAndrew Thompson * Check if we are supposed to stall the endpoint: 234202ac6454SAndrew Thompson */ 234302ac6454SAndrew Thompson if (xfer->flags.stall_pipe) { 234402ac6454SAndrew Thompson /* clear stall command */ 234502ac6454SAndrew Thompson xfer->flags.stall_pipe = 0; 234602ac6454SAndrew Thompson 234702ac6454SAndrew Thompson /* 234802ac6454SAndrew Thompson * Only stall BULK and INTERRUPT endpoints. 234902ac6454SAndrew Thompson */ 2350ae60fdfbSAndrew Thompson type = (ep->edesc->bmAttributes & UE_XFERTYPE); 235102ac6454SAndrew Thompson if ((type == UE_BULK) || 235202ac6454SAndrew Thompson (type == UE_INTERRUPT)) { 2353760bc48eSAndrew Thompson struct usb_device *udev; 2354760bc48eSAndrew Thompson struct usb_xfer_root *info; 235502ac6454SAndrew Thompson 235602ac6454SAndrew Thompson info = xfer->xroot; 235702ac6454SAndrew Thompson udev = info->udev; 2358ae60fdfbSAndrew Thompson ep->is_stalled = 1; 235902ac6454SAndrew Thompson 2360f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 236102ac6454SAndrew Thompson (udev->bus->methods->set_stall) ( 2362ae60fdfbSAndrew Thompson udev, NULL, ep); 236302ac6454SAndrew Thompson } else if (udev->default_xfer[1]) { 236402ac6454SAndrew Thompson info = udev->default_xfer[1]->xroot; 2365a593f6b8SAndrew Thompson if (usb_proc_msignal( 236602ac6454SAndrew Thompson &info->bus->non_giant_callback_proc, 236702ac6454SAndrew Thompson &udev->cs_msg[0], &udev->cs_msg[1])) { 236802ac6454SAndrew Thompson /* ignore */ 236902ac6454SAndrew Thompson } 237002ac6454SAndrew Thompson } else { 237102ac6454SAndrew Thompson /* should not happen */ 237202ac6454SAndrew Thompson DPRINTFN(0, "No stall handler!\n"); 237302ac6454SAndrew Thompson } 237402ac6454SAndrew Thompson /* 237502ac6454SAndrew Thompson * We get started again when the stall is cleared! 237602ac6454SAndrew Thompson */ 237702ac6454SAndrew Thompson return; 237802ac6454SAndrew Thompson } 237902ac6454SAndrew Thompson } 238002ac6454SAndrew Thompson /* Set or clear stall complete - special case */ 238102ac6454SAndrew Thompson if (xfer->nframes == 0) { 238202ac6454SAndrew Thompson /* we are complete */ 238302ac6454SAndrew Thompson xfer->aframes = 0; 2384a593f6b8SAndrew Thompson usbd_transfer_done(xfer, 0); 238502ac6454SAndrew Thompson return; 238602ac6454SAndrew Thompson } 238702ac6454SAndrew Thompson /* 238802ac6454SAndrew Thompson * Handled cases: 238902ac6454SAndrew Thompson * 239002ac6454SAndrew Thompson * 1) Start the first transfer queued. 239102ac6454SAndrew Thompson * 239202ac6454SAndrew Thompson * 2) Re-start the current USB transfer. 239302ac6454SAndrew Thompson */ 239402ac6454SAndrew Thompson /* 239502ac6454SAndrew Thompson * Check if there should be any 239602ac6454SAndrew Thompson * pre transfer start delay: 239702ac6454SAndrew Thompson */ 239802ac6454SAndrew Thompson if (xfer->interval > 0) { 2399ae60fdfbSAndrew Thompson type = (ep->edesc->bmAttributes & UE_XFERTYPE); 240002ac6454SAndrew Thompson if ((type == UE_BULK) || 240102ac6454SAndrew Thompson (type == UE_CONTROL)) { 2402a593f6b8SAndrew Thompson usbd_transfer_timeout_ms(xfer, 2403a593f6b8SAndrew Thompson &usbd_transfer_start_cb, 240402ac6454SAndrew Thompson xfer->interval); 240502ac6454SAndrew Thompson return; 240602ac6454SAndrew Thompson } 240702ac6454SAndrew Thompson } 240802ac6454SAndrew Thompson DPRINTF("start\n"); 240902ac6454SAndrew Thompson 241002ac6454SAndrew Thompson /* start USB transfer */ 2411ae60fdfbSAndrew Thompson (ep->methods->start) (xfer); 241202ac6454SAndrew Thompson 241302ac6454SAndrew Thompson xfer->flags_int.can_cancel_immed = 1; 24144565300dSAndrew Thompson 24154565300dSAndrew Thompson /* check for error */ 241602ac6454SAndrew Thompson if (xfer->error) { 241702ac6454SAndrew Thompson /* some error has happened */ 2418a593f6b8SAndrew Thompson usbd_transfer_done(xfer, 0); 241902ac6454SAndrew Thompson } 242002ac6454SAndrew Thompson } 242102ac6454SAndrew Thompson 242202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2423a593f6b8SAndrew Thompson * usbd_transfer_timeout_ms 242402ac6454SAndrew Thompson * 242502ac6454SAndrew Thompson * This function is used to setup a timeout on the given USB 242602ac6454SAndrew Thompson * transfer. If the timeout has been deferred the callback given by 242702ac6454SAndrew Thompson * "cb" will get called after "ms" milliseconds. 242802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 242902ac6454SAndrew Thompson void 2430a593f6b8SAndrew Thompson usbd_transfer_timeout_ms(struct usb_xfer *xfer, 2431e0a69b51SAndrew Thompson void (*cb) (void *arg), usb_timeout_t ms) 243202ac6454SAndrew Thompson { 243302ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 243402ac6454SAndrew Thompson 243502ac6454SAndrew Thompson /* defer delay */ 2436a593f6b8SAndrew Thompson usb_callout_reset(&xfer->timeout_handle, 243702ac6454SAndrew Thompson USB_MS_TO_TICKS(ms), cb, xfer); 243802ac6454SAndrew Thompson } 243902ac6454SAndrew Thompson 244002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2441a593f6b8SAndrew Thompson * usbd_callback_wrapper_sub 244202ac6454SAndrew Thompson * 244302ac6454SAndrew Thompson * - This function will update variables in an USB transfer after 244402ac6454SAndrew Thompson * that the USB transfer is complete. 244502ac6454SAndrew Thompson * 244602ac6454SAndrew Thompson * - This function is used to start the next USB transfer on the 2447ae60fdfbSAndrew Thompson * ep transfer queue, if any. 244802ac6454SAndrew Thompson * 244902ac6454SAndrew Thompson * NOTE: In some special cases the USB transfer will not be removed from 245002ac6454SAndrew Thompson * the pipe queue, but remain first. To enforce USB transfer removal call 245102ac6454SAndrew Thompson * this function passing the error code "USB_ERR_CANCELLED". 245202ac6454SAndrew Thompson * 245302ac6454SAndrew Thompson * Return values: 245402ac6454SAndrew Thompson * 0: Success. 245502ac6454SAndrew Thompson * Else: The callback has been deferred. 245602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 245702ac6454SAndrew Thompson static uint8_t 2458a593f6b8SAndrew Thompson usbd_callback_wrapper_sub(struct usb_xfer *xfer) 245902ac6454SAndrew Thompson { 2460ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 2461e0a69b51SAndrew Thompson usb_frcount_t x; 246202ac6454SAndrew Thompson 246302ac6454SAndrew Thompson if ((!xfer->flags_int.open) && 246402ac6454SAndrew Thompson (!xfer->flags_int.did_close)) { 246502ac6454SAndrew Thompson DPRINTF("close\n"); 246602ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 2467ae60fdfbSAndrew Thompson (xfer->endpoint->methods->close) (xfer); 246802ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 246902ac6454SAndrew Thompson /* only close once */ 247002ac6454SAndrew Thompson xfer->flags_int.did_close = 1; 247102ac6454SAndrew Thompson return (1); /* wait for new callback */ 247202ac6454SAndrew Thompson } 247302ac6454SAndrew Thompson /* 247402ac6454SAndrew Thompson * If we have a non-hardware induced error we 247502ac6454SAndrew Thompson * need to do the DMA delay! 247602ac6454SAndrew Thompson */ 247702ac6454SAndrew Thompson if (((xfer->error == USB_ERR_CANCELLED) || 247802ac6454SAndrew Thompson (xfer->error == USB_ERR_TIMEOUT)) && 247902ac6454SAndrew Thompson (!xfer->flags_int.did_dma_delay)) { 248002ac6454SAndrew Thompson 2481e0a69b51SAndrew Thompson usb_timeout_t temp; 248202ac6454SAndrew Thompson 248302ac6454SAndrew Thompson /* only delay once */ 248402ac6454SAndrew Thompson xfer->flags_int.did_dma_delay = 1; 248502ac6454SAndrew Thompson 248602ac6454SAndrew Thompson /* we can not cancel this delay */ 248702ac6454SAndrew Thompson xfer->flags_int.can_cancel_immed = 0; 248802ac6454SAndrew Thompson 2489a593f6b8SAndrew Thompson temp = usbd_get_dma_delay(xfer->xroot->bus); 249002ac6454SAndrew Thompson 249102ac6454SAndrew Thompson DPRINTFN(3, "DMA delay, %u ms, " 249202ac6454SAndrew Thompson "on %p\n", temp, xfer); 249302ac6454SAndrew Thompson 249402ac6454SAndrew Thompson if (temp != 0) { 249502ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 2496a593f6b8SAndrew Thompson usbd_transfer_timeout_ms(xfer, 2497a593f6b8SAndrew Thompson &usb_dma_delay_done_cb, temp); 249802ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 249902ac6454SAndrew Thompson return (1); /* wait for new callback */ 250002ac6454SAndrew Thompson } 250102ac6454SAndrew Thompson } 250202ac6454SAndrew Thompson /* check actual number of frames */ 250302ac6454SAndrew Thompson if (xfer->aframes > xfer->nframes) { 250402ac6454SAndrew Thompson if (xfer->error == 0) { 250502ac6454SAndrew Thompson panic("%s: actual number of frames, %d, is " 250602ac6454SAndrew Thompson "greater than initial number of frames, %d!\n", 250702ac6454SAndrew Thompson __FUNCTION__, xfer->aframes, xfer->nframes); 250802ac6454SAndrew Thompson } else { 250902ac6454SAndrew Thompson /* just set some valid value */ 251002ac6454SAndrew Thompson xfer->aframes = xfer->nframes; 251102ac6454SAndrew Thompson } 251202ac6454SAndrew Thompson } 251302ac6454SAndrew Thompson /* compute actual length */ 251402ac6454SAndrew Thompson xfer->actlen = 0; 251502ac6454SAndrew Thompson 251602ac6454SAndrew Thompson for (x = 0; x != xfer->aframes; x++) { 251702ac6454SAndrew Thompson xfer->actlen += xfer->frlengths[x]; 251802ac6454SAndrew Thompson } 251902ac6454SAndrew Thompson 252002ac6454SAndrew Thompson /* 252102ac6454SAndrew Thompson * Frames that were not transferred get zero actual length in 252202ac6454SAndrew Thompson * case the USB device driver does not check the actual number 252302ac6454SAndrew Thompson * of frames transferred, "xfer->aframes": 252402ac6454SAndrew Thompson */ 252502ac6454SAndrew Thompson for (; x < xfer->nframes; x++) { 2526ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, x, 0); 252702ac6454SAndrew Thompson } 252802ac6454SAndrew Thompson 252902ac6454SAndrew Thompson /* check actual length */ 253002ac6454SAndrew Thompson if (xfer->actlen > xfer->sumlen) { 253102ac6454SAndrew Thompson if (xfer->error == 0) { 253202ac6454SAndrew Thompson panic("%s: actual length, %d, is greater than " 253302ac6454SAndrew Thompson "initial length, %d!\n", 253402ac6454SAndrew Thompson __FUNCTION__, xfer->actlen, xfer->sumlen); 253502ac6454SAndrew Thompson } else { 253602ac6454SAndrew Thompson /* just set some valid value */ 253702ac6454SAndrew Thompson xfer->actlen = xfer->sumlen; 253802ac6454SAndrew Thompson } 253902ac6454SAndrew Thompson } 2540ed6d949aSAndrew Thompson DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n", 2541ae60fdfbSAndrew Thompson xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen, 254202ac6454SAndrew Thompson xfer->aframes, xfer->nframes); 254302ac6454SAndrew Thompson 254402ac6454SAndrew Thompson if (xfer->error) { 254502ac6454SAndrew Thompson /* end of control transfer, if any */ 254602ac6454SAndrew Thompson xfer->flags_int.control_act = 0; 254702ac6454SAndrew Thompson 254802ac6454SAndrew Thompson /* check if we should block the execution queue */ 254902ac6454SAndrew Thompson if ((xfer->error != USB_ERR_CANCELLED) && 255002ac6454SAndrew Thompson (xfer->flags.pipe_bof)) { 255102ac6454SAndrew Thompson DPRINTFN(2, "xfer=%p: Block On Failure " 2552ae60fdfbSAndrew Thompson "on endpoint=%p\n", xfer, xfer->endpoint); 255302ac6454SAndrew Thompson goto done; 255402ac6454SAndrew Thompson } 255502ac6454SAndrew Thompson } else { 255602ac6454SAndrew Thompson /* check for short transfers */ 255702ac6454SAndrew Thompson if (xfer->actlen < xfer->sumlen) { 255802ac6454SAndrew Thompson 255902ac6454SAndrew Thompson /* end of control transfer, if any */ 256002ac6454SAndrew Thompson xfer->flags_int.control_act = 0; 256102ac6454SAndrew Thompson 256202ac6454SAndrew Thompson if (!xfer->flags_int.short_xfer_ok) { 256302ac6454SAndrew Thompson xfer->error = USB_ERR_SHORT_XFER; 256402ac6454SAndrew Thompson if (xfer->flags.pipe_bof) { 256502ac6454SAndrew Thompson DPRINTFN(2, "xfer=%p: Block On Failure on " 2566ae60fdfbSAndrew Thompson "Short Transfer on endpoint %p.\n", 2567ae60fdfbSAndrew Thompson xfer, xfer->endpoint); 256802ac6454SAndrew Thompson goto done; 256902ac6454SAndrew Thompson } 257002ac6454SAndrew Thompson } 257102ac6454SAndrew Thompson } else { 257202ac6454SAndrew Thompson /* 257302ac6454SAndrew Thompson * Check if we are in the middle of a 257402ac6454SAndrew Thompson * control transfer: 257502ac6454SAndrew Thompson */ 257602ac6454SAndrew Thompson if (xfer->flags_int.control_act) { 257702ac6454SAndrew Thompson DPRINTFN(5, "xfer=%p: Control transfer " 2578ae60fdfbSAndrew Thompson "active on endpoint=%p\n", xfer, xfer->endpoint); 257902ac6454SAndrew Thompson goto done; 258002ac6454SAndrew Thompson } 258102ac6454SAndrew Thompson } 258202ac6454SAndrew Thompson } 258302ac6454SAndrew Thompson 2584ae60fdfbSAndrew Thompson ep = xfer->endpoint; 258502ac6454SAndrew Thompson 258602ac6454SAndrew Thompson /* 258702ac6454SAndrew Thompson * If the current USB transfer is completing we need to start the 258802ac6454SAndrew Thompson * next one: 258902ac6454SAndrew Thompson */ 259002ac6454SAndrew Thompson USB_BUS_LOCK(xfer->xroot->bus); 2591ae60fdfbSAndrew Thompson if (ep->endpoint_q.curr == xfer) { 2592a593f6b8SAndrew Thompson usb_command_wrapper(&ep->endpoint_q, NULL); 259302ac6454SAndrew Thompson 2594ae60fdfbSAndrew Thompson if (ep->endpoint_q.curr || TAILQ_FIRST(&ep->endpoint_q.head)) { 259502ac6454SAndrew Thompson /* there is another USB transfer waiting */ 259602ac6454SAndrew Thompson } else { 259702ac6454SAndrew Thompson /* this is the last USB transfer */ 259802ac6454SAndrew Thompson /* clear isochronous sync flag */ 2599ae60fdfbSAndrew Thompson xfer->endpoint->is_synced = 0; 260002ac6454SAndrew Thompson } 260102ac6454SAndrew Thompson } 260202ac6454SAndrew Thompson USB_BUS_UNLOCK(xfer->xroot->bus); 260302ac6454SAndrew Thompson done: 260402ac6454SAndrew Thompson return (0); 260502ac6454SAndrew Thompson } 260602ac6454SAndrew Thompson 260702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2608a593f6b8SAndrew Thompson * usb_command_wrapper 260902ac6454SAndrew Thompson * 261002ac6454SAndrew Thompson * This function is used to execute commands non-recursivly on an USB 261102ac6454SAndrew Thompson * transfer. 261202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 261302ac6454SAndrew Thompson void 2614a593f6b8SAndrew Thompson usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer) 261502ac6454SAndrew Thompson { 261602ac6454SAndrew Thompson if (xfer) { 261702ac6454SAndrew Thompson /* 261802ac6454SAndrew Thompson * If the transfer is not already processing, 261902ac6454SAndrew Thompson * queue it! 262002ac6454SAndrew Thompson */ 262102ac6454SAndrew Thompson if (pq->curr != xfer) { 2622a593f6b8SAndrew Thompson usbd_transfer_enqueue(pq, xfer); 262302ac6454SAndrew Thompson if (pq->curr != NULL) { 262402ac6454SAndrew Thompson /* something is already processing */ 262502ac6454SAndrew Thompson DPRINTFN(6, "busy %p\n", pq->curr); 262602ac6454SAndrew Thompson return; 262702ac6454SAndrew Thompson } 262802ac6454SAndrew Thompson } 262902ac6454SAndrew Thompson } else { 263002ac6454SAndrew Thompson /* Get next element in queue */ 263102ac6454SAndrew Thompson pq->curr = NULL; 263202ac6454SAndrew Thompson } 263302ac6454SAndrew Thompson 263402ac6454SAndrew Thompson if (!pq->recurse_1) { 263502ac6454SAndrew Thompson 263602ac6454SAndrew Thompson do { 263702ac6454SAndrew Thompson 263802ac6454SAndrew Thompson /* set both recurse flags */ 263902ac6454SAndrew Thompson pq->recurse_1 = 1; 264002ac6454SAndrew Thompson pq->recurse_2 = 1; 264102ac6454SAndrew Thompson 264202ac6454SAndrew Thompson if (pq->curr == NULL) { 264302ac6454SAndrew Thompson xfer = TAILQ_FIRST(&pq->head); 264402ac6454SAndrew Thompson if (xfer) { 264502ac6454SAndrew Thompson TAILQ_REMOVE(&pq->head, xfer, 264602ac6454SAndrew Thompson wait_entry); 264702ac6454SAndrew Thompson xfer->wait_queue = NULL; 264802ac6454SAndrew Thompson pq->curr = xfer; 264902ac6454SAndrew Thompson } else { 265002ac6454SAndrew Thompson break; 265102ac6454SAndrew Thompson } 265202ac6454SAndrew Thompson } 265302ac6454SAndrew Thompson DPRINTFN(6, "cb %p (enter)\n", pq->curr); 265402ac6454SAndrew Thompson (pq->command) (pq); 265502ac6454SAndrew Thompson DPRINTFN(6, "cb %p (leave)\n", pq->curr); 265602ac6454SAndrew Thompson 265702ac6454SAndrew Thompson } while (!pq->recurse_2); 265802ac6454SAndrew Thompson 265902ac6454SAndrew Thompson /* clear first recurse flag */ 266002ac6454SAndrew Thompson pq->recurse_1 = 0; 266102ac6454SAndrew Thompson 266202ac6454SAndrew Thompson } else { 266302ac6454SAndrew Thompson /* clear second recurse flag */ 266402ac6454SAndrew Thompson pq->recurse_2 = 0; 266502ac6454SAndrew Thompson } 266602ac6454SAndrew Thompson } 266702ac6454SAndrew Thompson 266802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2669a593f6b8SAndrew Thompson * usbd_default_transfer_setup 267002ac6454SAndrew Thompson * 267102ac6454SAndrew Thompson * This function is used to setup the default USB control endpoint 267202ac6454SAndrew Thompson * transfer. 267302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 267402ac6454SAndrew Thompson void 2675a593f6b8SAndrew Thompson usbd_default_transfer_setup(struct usb_device *udev) 267602ac6454SAndrew Thompson { 2677760bc48eSAndrew Thompson struct usb_xfer *xfer; 267802ac6454SAndrew Thompson uint8_t no_resetup; 267902ac6454SAndrew Thompson uint8_t iface_index; 268002ac6454SAndrew Thompson 268139307315SAndrew Thompson /* check for root HUB */ 268239307315SAndrew Thompson if (udev->parent_hub == NULL) 268339307315SAndrew Thompson return; 268402ac6454SAndrew Thompson repeat: 268502ac6454SAndrew Thompson 268602ac6454SAndrew Thompson xfer = udev->default_xfer[0]; 268702ac6454SAndrew Thompson if (xfer) { 268802ac6454SAndrew Thompson USB_XFER_LOCK(xfer); 268902ac6454SAndrew Thompson no_resetup = 269002ac6454SAndrew Thompson ((xfer->address == udev->address) && 269102ac6454SAndrew Thompson (udev->default_ep_desc.wMaxPacketSize[0] == 269202ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize)); 2693f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 269402ac6454SAndrew Thompson if (no_resetup) { 269502ac6454SAndrew Thompson /* 269602ac6454SAndrew Thompson * NOTE: checking "xfer->address" and 269702ac6454SAndrew Thompson * starting the USB transfer must be 269802ac6454SAndrew Thompson * atomic! 269902ac6454SAndrew Thompson */ 2700a593f6b8SAndrew Thompson usbd_transfer_start(xfer); 270102ac6454SAndrew Thompson } 270202ac6454SAndrew Thompson } 270302ac6454SAndrew Thompson USB_XFER_UNLOCK(xfer); 270402ac6454SAndrew Thompson } else { 270502ac6454SAndrew Thompson no_resetup = 0; 270602ac6454SAndrew Thompson } 270702ac6454SAndrew Thompson 270802ac6454SAndrew Thompson if (no_resetup) { 270902ac6454SAndrew Thompson /* 271002ac6454SAndrew Thompson * All parameters are exactly the same like before. 271102ac6454SAndrew Thompson * Just return. 271202ac6454SAndrew Thompson */ 271302ac6454SAndrew Thompson return; 271402ac6454SAndrew Thompson } 271502ac6454SAndrew Thompson /* 271602ac6454SAndrew Thompson * Update wMaxPacketSize for the default control endpoint: 271702ac6454SAndrew Thompson */ 271802ac6454SAndrew Thompson udev->default_ep_desc.wMaxPacketSize[0] = 271902ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize; 272002ac6454SAndrew Thompson 272102ac6454SAndrew Thompson /* 272202ac6454SAndrew Thompson * Unsetup any existing USB transfer: 272302ac6454SAndrew Thompson */ 2724a593f6b8SAndrew Thompson usbd_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 272502ac6454SAndrew Thompson 272602ac6454SAndrew Thompson /* 272702ac6454SAndrew Thompson * Try to setup a new USB transfer for the 272802ac6454SAndrew Thompson * default control endpoint: 272902ac6454SAndrew Thompson */ 273002ac6454SAndrew Thompson iface_index = 0; 2731a593f6b8SAndrew Thompson if (usbd_transfer_setup(udev, &iface_index, 2732a593f6b8SAndrew Thompson udev->default_xfer, usb_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL, 273302ac6454SAndrew Thompson udev->default_mtx)) { 273402ac6454SAndrew Thompson DPRINTFN(0, "could not setup default " 273502ac6454SAndrew Thompson "USB transfer!\n"); 273602ac6454SAndrew Thompson } else { 273702ac6454SAndrew Thompson goto repeat; 273802ac6454SAndrew Thompson } 273902ac6454SAndrew Thompson } 274002ac6454SAndrew Thompson 274102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2742a593f6b8SAndrew Thompson * usbd_clear_data_toggle - factored out code 274302ac6454SAndrew Thompson * 274402ac6454SAndrew Thompson * NOTE: the intention of this function is not to reset the hardware 274502ac6454SAndrew Thompson * data toggle. 274602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 274702ac6454SAndrew Thompson void 2748a593f6b8SAndrew Thompson usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep) 274902ac6454SAndrew Thompson { 2750ae60fdfbSAndrew Thompson DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep); 275102ac6454SAndrew Thompson 275202ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 2753ae60fdfbSAndrew Thompson ep->toggle_next = 0; 275402ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 275502ac6454SAndrew Thompson } 275602ac6454SAndrew Thompson 275702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2758a593f6b8SAndrew Thompson * usbd_clear_stall_callback - factored out clear stall callback 275902ac6454SAndrew Thompson * 276002ac6454SAndrew Thompson * Input parameters: 276102ac6454SAndrew Thompson * xfer1: Clear Stall Control Transfer 276202ac6454SAndrew Thompson * xfer2: Stalled USB Transfer 276302ac6454SAndrew Thompson * 276402ac6454SAndrew Thompson * This function is NULL safe. 276502ac6454SAndrew Thompson * 276602ac6454SAndrew Thompson * Return values: 276702ac6454SAndrew Thompson * 0: In progress 276802ac6454SAndrew Thompson * Else: Finished 276902ac6454SAndrew Thompson * 277002ac6454SAndrew Thompson * Clear stall config example: 277102ac6454SAndrew Thompson * 2772760bc48eSAndrew Thompson * static const struct usb_config my_clearstall = { 277302ac6454SAndrew Thompson * .type = UE_CONTROL, 277402ac6454SAndrew Thompson * .endpoint = 0, 277502ac6454SAndrew Thompson * .direction = UE_DIR_ANY, 277602ac6454SAndrew Thompson * .interval = 50, //50 milliseconds 2777760bc48eSAndrew Thompson * .bufsize = sizeof(struct usb_device_request), 27784eae601eSAndrew Thompson * .timeout = 1000, //1.000 seconds 27794eae601eSAndrew Thompson * .callback = &my_clear_stall_callback, // ** 27804eae601eSAndrew Thompson * .usb_mode = USB_MODE_HOST, 278102ac6454SAndrew Thompson * }; 278202ac6454SAndrew Thompson * 2783a593f6b8SAndrew Thompson * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback" 278402ac6454SAndrew Thompson * passing the correct parameters. 278502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 278602ac6454SAndrew Thompson uint8_t 2787a593f6b8SAndrew Thompson usbd_clear_stall_callback(struct usb_xfer *xfer1, 2788760bc48eSAndrew Thompson struct usb_xfer *xfer2) 278902ac6454SAndrew Thompson { 2790760bc48eSAndrew Thompson struct usb_device_request req; 279102ac6454SAndrew Thompson 279202ac6454SAndrew Thompson if (xfer2 == NULL) { 279302ac6454SAndrew Thompson /* looks like we are tearing down */ 279402ac6454SAndrew Thompson DPRINTF("NULL input parameter\n"); 279502ac6454SAndrew Thompson return (0); 279602ac6454SAndrew Thompson } 279702ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED); 279802ac6454SAndrew Thompson USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED); 279902ac6454SAndrew Thompson 280002ac6454SAndrew Thompson switch (USB_GET_STATE(xfer1)) { 280102ac6454SAndrew Thompson case USB_ST_SETUP: 280202ac6454SAndrew Thompson 280302ac6454SAndrew Thompson /* 280402ac6454SAndrew Thompson * pre-clear the data toggle to DATA0 ("umass.c" and 280502ac6454SAndrew Thompson * "ata-usb.c" depends on this) 280602ac6454SAndrew Thompson */ 280702ac6454SAndrew Thompson 2808a593f6b8SAndrew Thompson usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint); 280902ac6454SAndrew Thompson 281002ac6454SAndrew Thompson /* setup a clear-stall packet */ 281102ac6454SAndrew Thompson 281202ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_ENDPOINT; 281302ac6454SAndrew Thompson req.bRequest = UR_CLEAR_FEATURE; 281402ac6454SAndrew Thompson USETW(req.wValue, UF_ENDPOINT_HALT); 2815ae60fdfbSAndrew Thompson req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress; 281602ac6454SAndrew Thompson req.wIndex[1] = 0; 281702ac6454SAndrew Thompson USETW(req.wLength, 0); 281802ac6454SAndrew Thompson 281902ac6454SAndrew Thompson /* 2820a593f6b8SAndrew Thompson * "usbd_transfer_setup_sub()" will ensure that 282102ac6454SAndrew Thompson * we have sufficient room in the buffer for 282202ac6454SAndrew Thompson * the request structure! 282302ac6454SAndrew Thompson */ 282402ac6454SAndrew Thompson 282502ac6454SAndrew Thompson /* copy in the transfer */ 282602ac6454SAndrew Thompson 2827a593f6b8SAndrew Thompson usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); 282802ac6454SAndrew Thompson 282902ac6454SAndrew Thompson /* set length */ 283002ac6454SAndrew Thompson xfer1->frlengths[0] = sizeof(req); 283102ac6454SAndrew Thompson xfer1->nframes = 1; 283202ac6454SAndrew Thompson 2833a593f6b8SAndrew Thompson usbd_transfer_submit(xfer1); 283402ac6454SAndrew Thompson return (0); 283502ac6454SAndrew Thompson 283602ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 283702ac6454SAndrew Thompson break; 283802ac6454SAndrew Thompson 283902ac6454SAndrew Thompson default: /* Error */ 284002ac6454SAndrew Thompson if (xfer1->error == USB_ERR_CANCELLED) { 284102ac6454SAndrew Thompson return (0); 284202ac6454SAndrew Thompson } 284302ac6454SAndrew Thompson break; 284402ac6454SAndrew Thompson } 284502ac6454SAndrew Thompson return (1); /* Clear Stall Finished */ 284602ac6454SAndrew Thompson } 284702ac6454SAndrew Thompson 284802ac6454SAndrew Thompson void 2849ed6d949aSAndrew Thompson usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max) 285002ac6454SAndrew Thompson { 28514a35cda7SAndrew Thompson static uint8_t once = 0; 28524a35cda7SAndrew Thompson /* polling is currently not supported */ 28534a35cda7SAndrew Thompson if (!once) { 28544a35cda7SAndrew Thompson once = 1; 2855ed6d949aSAndrew Thompson printf("usbd_transfer_poll: USB polling is " 28564a35cda7SAndrew Thompson "not supported!\n"); 285702ac6454SAndrew Thompson } 285802ac6454SAndrew Thompson } 28594eae601eSAndrew Thompson 28604eae601eSAndrew Thompson static void 2861a593f6b8SAndrew Thompson usbd_get_std_packet_size(struct usb_std_packet_size *ptr, 28628d2dd5ddSAndrew Thompson uint8_t type, enum usb_dev_speed speed) 28634eae601eSAndrew Thompson { 28644eae601eSAndrew Thompson static const uint16_t intr_range_max[USB_SPEED_MAX] = { 28654eae601eSAndrew Thompson [USB_SPEED_LOW] = 8, 28664eae601eSAndrew Thompson [USB_SPEED_FULL] = 64, 28674eae601eSAndrew Thompson [USB_SPEED_HIGH] = 1024, 28684eae601eSAndrew Thompson [USB_SPEED_VARIABLE] = 1024, 28694eae601eSAndrew Thompson [USB_SPEED_SUPER] = 1024, 28704eae601eSAndrew Thompson }; 28714eae601eSAndrew Thompson 28724eae601eSAndrew Thompson static const uint16_t isoc_range_max[USB_SPEED_MAX] = { 28734eae601eSAndrew Thompson [USB_SPEED_LOW] = 0, /* invalid */ 28744eae601eSAndrew Thompson [USB_SPEED_FULL] = 1023, 28754eae601eSAndrew Thompson [USB_SPEED_HIGH] = 1024, 28764eae601eSAndrew Thompson [USB_SPEED_VARIABLE] = 3584, 28774eae601eSAndrew Thompson [USB_SPEED_SUPER] = 1024, 28784eae601eSAndrew Thompson }; 28794eae601eSAndrew Thompson 28804eae601eSAndrew Thompson static const uint16_t control_min[USB_SPEED_MAX] = { 28814eae601eSAndrew Thompson [USB_SPEED_LOW] = 8, 28824eae601eSAndrew Thompson [USB_SPEED_FULL] = 8, 28834eae601eSAndrew Thompson [USB_SPEED_HIGH] = 64, 28844eae601eSAndrew Thompson [USB_SPEED_VARIABLE] = 512, 28854eae601eSAndrew Thompson [USB_SPEED_SUPER] = 512, 28864eae601eSAndrew Thompson }; 28874eae601eSAndrew Thompson 28884eae601eSAndrew Thompson static const uint16_t bulk_min[USB_SPEED_MAX] = { 28894eae601eSAndrew Thompson [USB_SPEED_LOW] = 0, /* not supported */ 28904eae601eSAndrew Thompson [USB_SPEED_FULL] = 8, 28914eae601eSAndrew Thompson [USB_SPEED_HIGH] = 512, 28924eae601eSAndrew Thompson [USB_SPEED_VARIABLE] = 512, 28934eae601eSAndrew Thompson [USB_SPEED_SUPER] = 1024, 28944eae601eSAndrew Thompson }; 28954eae601eSAndrew Thompson 28964eae601eSAndrew Thompson uint16_t temp; 28974eae601eSAndrew Thompson 28984eae601eSAndrew Thompson memset(ptr, 0, sizeof(*ptr)); 28994eae601eSAndrew Thompson 29004eae601eSAndrew Thompson switch (type) { 29014eae601eSAndrew Thompson case UE_INTERRUPT: 29028d2dd5ddSAndrew Thompson ptr->range.max = intr_range_max[speed]; 29034eae601eSAndrew Thompson break; 29044eae601eSAndrew Thompson case UE_ISOCHRONOUS: 29058d2dd5ddSAndrew Thompson ptr->range.max = isoc_range_max[speed]; 29064eae601eSAndrew Thompson break; 29074eae601eSAndrew Thompson default: 29084eae601eSAndrew Thompson if (type == UE_BULK) 29098d2dd5ddSAndrew Thompson temp = bulk_min[speed]; 29104eae601eSAndrew Thompson else /* UE_CONTROL */ 29118d2dd5ddSAndrew Thompson temp = control_min[speed]; 29124eae601eSAndrew Thompson 29134eae601eSAndrew Thompson /* default is fixed */ 29144eae601eSAndrew Thompson ptr->fixed[0] = temp; 29154eae601eSAndrew Thompson ptr->fixed[1] = temp; 29164eae601eSAndrew Thompson ptr->fixed[2] = temp; 29174eae601eSAndrew Thompson ptr->fixed[3] = temp; 29184eae601eSAndrew Thompson 29198d2dd5ddSAndrew Thompson if (speed == USB_SPEED_FULL) { 29204eae601eSAndrew Thompson /* multiple sizes */ 29214eae601eSAndrew Thompson ptr->fixed[1] = 16; 29224eae601eSAndrew Thompson ptr->fixed[2] = 32; 29234eae601eSAndrew Thompson ptr->fixed[3] = 64; 29244eae601eSAndrew Thompson } 29258d2dd5ddSAndrew Thompson if ((speed == USB_SPEED_VARIABLE) && 29264eae601eSAndrew Thompson (type == UE_BULK)) { 29274eae601eSAndrew Thompson /* multiple sizes */ 29284eae601eSAndrew Thompson ptr->fixed[2] = 1024; 29294eae601eSAndrew Thompson ptr->fixed[3] = 1536; 29304eae601eSAndrew Thompson } 29314eae601eSAndrew Thompson break; 29324eae601eSAndrew Thompson } 29334eae601eSAndrew Thompson } 2934ed6d949aSAndrew Thompson 2935ed6d949aSAndrew Thompson void * 2936ed6d949aSAndrew Thompson usbd_xfer_softc(struct usb_xfer *xfer) 2937ed6d949aSAndrew Thompson { 2938ed6d949aSAndrew Thompson return (xfer->priv_sc); 2939ed6d949aSAndrew Thompson } 2940ed6d949aSAndrew Thompson 2941ed6d949aSAndrew Thompson void * 2942ed6d949aSAndrew Thompson usbd_xfer_get_priv(struct usb_xfer *xfer) 2943ed6d949aSAndrew Thompson { 2944ed6d949aSAndrew Thompson return (xfer->priv_fifo); 2945ed6d949aSAndrew Thompson } 2946ed6d949aSAndrew Thompson 2947ed6d949aSAndrew Thompson void 2948ed6d949aSAndrew Thompson usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr) 2949ed6d949aSAndrew Thompson { 2950ed6d949aSAndrew Thompson xfer->priv_fifo = ptr; 2951ed6d949aSAndrew Thompson } 2952ed6d949aSAndrew Thompson 2953ed6d949aSAndrew Thompson uint8_t 2954ed6d949aSAndrew Thompson usbd_xfer_state(struct usb_xfer *xfer) 2955ed6d949aSAndrew Thompson { 2956ed6d949aSAndrew Thompson return (xfer->usb_state); 2957ed6d949aSAndrew Thompson } 2958ed6d949aSAndrew Thompson 2959ed6d949aSAndrew Thompson void 2960ed6d949aSAndrew Thompson usbd_xfer_set_flag(struct usb_xfer *xfer, int flag) 2961ed6d949aSAndrew Thompson { 2962ed6d949aSAndrew Thompson switch (flag) { 2963ed6d949aSAndrew Thompson case USB_FORCE_SHORT_XFER: 2964ed6d949aSAndrew Thompson xfer->flags.force_short_xfer = 1; 2965ed6d949aSAndrew Thompson break; 2966ed6d949aSAndrew Thompson case USB_SHORT_XFER_OK: 2967ed6d949aSAndrew Thompson xfer->flags.short_xfer_ok = 1; 2968ed6d949aSAndrew Thompson break; 2969ed6d949aSAndrew Thompson } 2970ed6d949aSAndrew Thompson } 2971ed6d949aSAndrew Thompson 2972ed6d949aSAndrew Thompson void 2973ed6d949aSAndrew Thompson usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag) 2974ed6d949aSAndrew Thompson { 2975ed6d949aSAndrew Thompson switch (flag) { 2976ed6d949aSAndrew Thompson case USB_FORCE_SHORT_XFER: 2977ed6d949aSAndrew Thompson xfer->flags.force_short_xfer = 0; 2978ed6d949aSAndrew Thompson break; 2979ed6d949aSAndrew Thompson case USB_SHORT_XFER_OK: 2980ed6d949aSAndrew Thompson xfer->flags.short_xfer_ok = 0; 2981ed6d949aSAndrew Thompson break; 2982ed6d949aSAndrew Thompson } 2983ed6d949aSAndrew Thompson } 2984