usb_transfer.c revision 199816
1135446Strhodes/* $FreeBSD: head/sys/dev/usb/usb_transfer.c 199816 2009-11-26 00:43:17Z thompsa $ */ 2135446Strhodes/*- 3135446Strhodes * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4135446Strhodes * 5135446Strhodes * Redistribution and use in source and binary forms, with or without 6135446Strhodes * modification, are permitted provided that the following conditions 7135446Strhodes * are met: 8135446Strhodes * 1. Redistributions of source code must retain the above copyright 9135446Strhodes * notice, this list of conditions and the following disclaimer. 10135446Strhodes * 2. Redistributions in binary form must reproduce the above copyright 11135446Strhodes * notice, this list of conditions and the following disclaimer in the 12135446Strhodes * documentation and/or other materials provided with the distribution. 13135446Strhodes * 14135446Strhodes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15135446Strhodes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16135446Strhodes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17135446Strhodes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18135446Strhodes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19135446Strhodes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20135446Strhodes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21135446Strhodes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22135446Strhodes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23135446Strhodes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24135446Strhodes * SUCH DAMAGE. 25135446Strhodes */ 26135446Strhodes 27135446Strhodes#include <sys/stdint.h> 28135446Strhodes#include <sys/stddef.h> 29135446Strhodes#include <sys/param.h> 30135446Strhodes#include <sys/queue.h> 31135446Strhodes#include <sys/types.h> 32135446Strhodes#include <sys/systm.h> 33135446Strhodes#include <sys/kernel.h> 34135446Strhodes#include <sys/bus.h> 35135446Strhodes#include <sys/linker_set.h> 36135446Strhodes#include <sys/module.h> 37135446Strhodes#include <sys/lock.h> 38135446Strhodes#include <sys/mutex.h> 39135446Strhodes#include <sys/condvar.h> 40135446Strhodes#include <sys/sysctl.h> 41135446Strhodes#include <sys/sx.h> 42135446Strhodes#include <sys/unistd.h> 43135446Strhodes#include <sys/callout.h> 44135446Strhodes#include <sys/malloc.h> 45135446Strhodes#include <sys/priv.h> 46135446Strhodes 47135446Strhodes#include <dev/usb/usb.h> 48135446Strhodes#include <dev/usb/usbdi.h> 49135446Strhodes#include <dev/usb/usbdi_util.h> 50135446Strhodes 51135446Strhodes#define USB_DEBUG_VAR usb_debug 52135446Strhodes 53135446Strhodes#include <dev/usb/usb_core.h> 54135446Strhodes#include <dev/usb/usb_busdma.h> 55135446Strhodes#include <dev/usb/usb_process.h> 56135446Strhodes#include <dev/usb/usb_transfer.h> 57135446Strhodes#include <dev/usb/usb_device.h> 58135446Strhodes#include <dev/usb/usb_debug.h> 59135446Strhodes#include <dev/usb/usb_util.h> 60135446Strhodes 61135446Strhodes#include <dev/usb/usb_controller.h> 62135446Strhodes#include <dev/usb/usb_bus.h> 63135446Strhodes 64135446Strhodesstruct usb_std_packet_size { 65135446Strhodes struct { 66135446Strhodes uint16_t min; /* inclusive */ 67135446Strhodes uint16_t max; /* inclusive */ 68135446Strhodes } range; 69135446Strhodes 70135446Strhodes uint16_t fixed[4]; 71135446Strhodes}; 72135446Strhodes 73135446Strhodesstatic usb_callback_t usb_request_callback; 74135446Strhodes 75135446Strhodesstatic const struct usb_config usb_control_ep_cfg[USB_DEFAULT_XFER_MAX] = { 76135446Strhodes 77135446Strhodes /* This transfer is used for generic control endpoint transfers */ 78135446Strhodes 79135446Strhodes [0] = { 80135446Strhodes .type = UE_CONTROL, 81135446Strhodes .endpoint = 0x00, /* Control endpoint */ 82135446Strhodes .direction = UE_DIR_ANY, 83135446Strhodes .bufsize = USB_EP0_BUFSIZE, /* bytes */ 84135446Strhodes .flags = {.proxy_buffer = 1,}, 85135446Strhodes .callback = &usb_request_callback, 86135446Strhodes .usb_mode = USB_MODE_DUAL, /* both modes */ 87135446Strhodes }, 88135446Strhodes 89135446Strhodes /* This transfer is used for generic clear stall only */ 90135446Strhodes 91135446Strhodes [1] = { 92135446Strhodes .type = UE_CONTROL, 93135446Strhodes .endpoint = 0x00, /* Control pipe */ 94135446Strhodes .direction = UE_DIR_ANY, 95135446Strhodes .bufsize = sizeof(struct usb_device_request), 96135446Strhodes .callback = &usb_do_clear_stall_callback, 97135446Strhodes .timeout = 1000, /* 1 second */ 98135446Strhodes .interval = 50, /* 50ms */ 99135446Strhodes .usb_mode = USB_MODE_HOST, 100135446Strhodes }, 101135446Strhodes}; 102135446Strhodes 103135446Strhodes/* function prototypes */ 104135446Strhodes 105135446Strhodesstatic void usbd_update_max_frame_size(struct usb_xfer *); 106135446Strhodesstatic void usbd_transfer_unsetup_sub(struct usb_xfer_root *, uint8_t); 107135446Strhodesstatic void usbd_control_transfer_init(struct usb_xfer *); 108135446Strhodesstatic int usbd_setup_ctrl_transfer(struct usb_xfer *); 109135446Strhodesstatic void usb_callback_proc(struct usb_proc_msg *); 110135446Strhodesstatic void usbd_callback_ss_done_defer(struct usb_xfer *); 111135446Strhodesstatic void usbd_callback_wrapper(struct usb_xfer_queue *); 112135446Strhodesstatic void usb_dma_delay_done_cb(void *); 113135446Strhodesstatic void usbd_transfer_start_cb(void *); 114135446Strhodesstatic uint8_t usbd_callback_wrapper_sub(struct usb_xfer *); 115135446Strhodesstatic void usbd_get_std_packet_size(struct usb_std_packet_size *ptr, 116135446Strhodes uint8_t type, enum usb_dev_speed speed); 117135446Strhodes 118135446Strhodes/*------------------------------------------------------------------------* 119135446Strhodes * usb_request_callback 120135446Strhodes *------------------------------------------------------------------------*/ 121135446Strhodesstatic void 122135446Strhodesusb_request_callback(struct usb_xfer *xfer, usb_error_t error) 123135446Strhodes{ 124135446Strhodes if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) 125135446Strhodes usb_handle_request_callback(xfer, error); 126135446Strhodes else 127135446Strhodes usbd_do_request_callback(xfer, error); 128135446Strhodes} 129135446Strhodes 130135446Strhodes/*------------------------------------------------------------------------* 131135446Strhodes * usbd_update_max_frame_size 132135446Strhodes * 133135446Strhodes * This function updates the maximum frame size, hence high speed USB 134135446Strhodes * can transfer multiple consecutive packets. 135135446Strhodes *------------------------------------------------------------------------*/ 136135446Strhodesstatic void 137135446Strhodesusbd_update_max_frame_size(struct usb_xfer *xfer) 138135446Strhodes{ 139135446Strhodes /* compute maximum frame size */ 140135446Strhodes 141135446Strhodes if (xfer->max_packet_count == 2) { 142135446Strhodes xfer->max_frame_size = 2 * xfer->max_packet_size; 143135446Strhodes } else if (xfer->max_packet_count == 3) { 144135446Strhodes xfer->max_frame_size = 3 * xfer->max_packet_size; 145135446Strhodes } else { 146135446Strhodes xfer->max_frame_size = xfer->max_packet_size; 147135446Strhodes } 148135446Strhodes} 149135446Strhodes 150135446Strhodes/*------------------------------------------------------------------------* 151135446Strhodes * usbd_get_dma_delay 152135446Strhodes * 153135446Strhodes * The following function is called when we need to 154135446Strhodes * synchronize with DMA hardware. 155135446Strhodes * 156135446Strhodes * Returns: 157135446Strhodes * 0: no DMA delay required 158135446Strhodes * Else: milliseconds of DMA delay 159135446Strhodes *------------------------------------------------------------------------*/ 160135446Strhodesusb_timeout_t 161135446Strhodesusbd_get_dma_delay(struct usb_bus *bus) 162135446Strhodes{ 163135446Strhodes uint32_t temp = 0; 164135446Strhodes 165135446Strhodes if (bus->methods->get_dma_delay) { 166135446Strhodes (bus->methods->get_dma_delay) (bus, &temp); 167135446Strhodes /* 168135446Strhodes * Round up and convert to milliseconds. Note that we use 169135446Strhodes * 1024 milliseconds per second. to save a division. 170135446Strhodes */ 171135446Strhodes temp += 0x3FF; 172135446Strhodes temp /= 0x400; 173135446Strhodes } 174135446Strhodes return (temp); 175135446Strhodes} 176135446Strhodes 177135446Strhodes/*------------------------------------------------------------------------* 178135446Strhodes * usbd_transfer_setup_sub_malloc 179135446Strhodes * 180135446Strhodes * This function will allocate one or more DMA'able memory chunks 181135446Strhodes * according to "size", "align" and "count" arguments. "ppc" is 182135446Strhodes * pointed to a linear array of USB page caches afterwards. 183135446Strhodes * 184135446Strhodes * Returns: 185135446Strhodes * 0: Success 186135446Strhodes * Else: Failure 187135446Strhodes *------------------------------------------------------------------------*/ 188135446Strhodes#if USB_HAVE_BUSDMA 189135446Strhodesuint8_t 190135446Strhodesusbd_transfer_setup_sub_malloc(struct usb_setup_params *parm, 191135446Strhodes struct usb_page_cache **ppc, usb_size_t size, usb_size_t align, 192135446Strhodes usb_size_t count) 193135446Strhodes{ 194135446Strhodes struct usb_page_cache *pc; 195135446Strhodes struct usb_page *pg; 196135446Strhodes void *buf; 197135446Strhodes usb_size_t n_dma_pc; 198135446Strhodes usb_size_t n_obj; 199135446Strhodes usb_size_t x; 200135446Strhodes usb_size_t y; 201135446Strhodes usb_size_t r; 202135446Strhodes usb_size_t z; 203135446Strhodes 204135446Strhodes USB_ASSERT(align > 1, ("Invalid alignment, 0x%08x\n", 205135446Strhodes align)); 206135446Strhodes USB_ASSERT(size > 0, ("Invalid size = 0\n")); 207135446Strhodes 208135446Strhodes if (count == 0) { 209135446Strhodes return (0); /* nothing to allocate */ 210135446Strhodes } 211135446Strhodes /* 212135446Strhodes * Make sure that the size is aligned properly. 213135446Strhodes */ 214135446Strhodes size = -((-size) & (-align)); 215135446Strhodes 216135446Strhodes /* 217135446Strhodes * Try multi-allocation chunks to reduce the number of DMA 218135446Strhodes * allocations, hence DMA allocations are slow. 219135446Strhodes */ 220135446Strhodes if (size >= PAGE_SIZE) { 221135446Strhodes n_dma_pc = count; 222135446Strhodes n_obj = 1; 223135446Strhodes } else { 224135446Strhodes /* compute number of objects per page */ 225135446Strhodes n_obj = (PAGE_SIZE / size); 226135446Strhodes /* 227135446Strhodes * Compute number of DMA chunks, rounded up 228135446Strhodes * to nearest one: 229135446Strhodes */ 230135446Strhodes n_dma_pc = ((count + n_obj - 1) / n_obj); 231135446Strhodes } 232135446Strhodes 233135446Strhodes if (parm->buf == NULL) { 234135446Strhodes /* for the future */ 235135446Strhodes parm->dma_page_ptr += n_dma_pc; 236135446Strhodes parm->dma_page_cache_ptr += n_dma_pc; 237135446Strhodes parm->dma_page_ptr += count; 238135446Strhodes parm->xfer_page_cache_ptr += count; 239135446Strhodes return (0); 240135446Strhodes } 241135446Strhodes for (x = 0; x != n_dma_pc; x++) { 242135446Strhodes /* need to initialize the page cache */ 243135446Strhodes parm->dma_page_cache_ptr[x].tag_parent = 244135446Strhodes &parm->curr_xfer->xroot->dma_parent_tag; 245135446Strhodes } 246135446Strhodes for (x = 0; x != count; x++) { 247135446Strhodes /* need to initialize the page cache */ 248135446Strhodes parm->xfer_page_cache_ptr[x].tag_parent = 249135446Strhodes &parm->curr_xfer->xroot->dma_parent_tag; 250135446Strhodes } 251135446Strhodes 252135446Strhodes if (ppc) { 253135446Strhodes *ppc = parm->xfer_page_cache_ptr; 254135446Strhodes } 255135446Strhodes r = count; /* set remainder count */ 256135446Strhodes z = n_obj * size; /* set allocation size */ 257135446Strhodes pc = parm->xfer_page_cache_ptr; 258135446Strhodes pg = parm->dma_page_ptr; 259135446Strhodes 260135446Strhodes for (x = 0; x != n_dma_pc; x++) { 261135446Strhodes 262135446Strhodes if (r < n_obj) { 263135446Strhodes /* compute last remainder */ 264135446Strhodes z = r * size; 265135446Strhodes n_obj = r; 266135446Strhodes } 267135446Strhodes if (usb_pc_alloc_mem(parm->dma_page_cache_ptr, 268135446Strhodes pg, z, align)) { 269135446Strhodes return (1); /* failure */ 270135446Strhodes } 271135446Strhodes /* Set beginning of current buffer */ 272135446Strhodes buf = parm->dma_page_cache_ptr->buffer; 273135446Strhodes /* Make room for one DMA page cache and one page */ 274135446Strhodes parm->dma_page_cache_ptr++; 275135446Strhodes pg++; 276135446Strhodes 277135446Strhodes for (y = 0; (y != n_obj); y++, r--, pc++, pg++) { 278135446Strhodes 279135446Strhodes /* Load sub-chunk into DMA */ 280135446Strhodes if (usb_pc_dmamap_create(pc, size)) { 281135446Strhodes return (1); /* failure */ 282135446Strhodes } 283135446Strhodes pc->buffer = USB_ADD_BYTES(buf, y * size); 284135446Strhodes pc->page_start = pg; 285135446Strhodes 286135446Strhodes mtx_lock(pc->tag_parent->mtx); 287135446Strhodes if (usb_pc_load_mem(pc, size, 1 /* synchronous */ )) { 288135446Strhodes mtx_unlock(pc->tag_parent->mtx); 289135446Strhodes return (1); /* failure */ 290135446Strhodes } 291135446Strhodes mtx_unlock(pc->tag_parent->mtx); 292135446Strhodes } 293135446Strhodes } 294135446Strhodes 295135446Strhodes parm->xfer_page_cache_ptr = pc; 296135446Strhodes parm->dma_page_ptr = pg; 297135446Strhodes return (0); 298135446Strhodes} 299135446Strhodes#endif 300135446Strhodes 301135446Strhodes/*------------------------------------------------------------------------* 302135446Strhodes * usbd_transfer_setup_sub - transfer setup subroutine 303135446Strhodes * 304135446Strhodes * This function must be called from the "xfer_setup" callback of the 305135446Strhodes * USB Host or Device controller driver when setting up an USB 306135446Strhodes * transfer. This function will setup correct packet sizes, buffer 307135446Strhodes * sizes, flags and more, that are stored in the "usb_xfer" 308135446Strhodes * structure. 309135446Strhodes *------------------------------------------------------------------------*/ 310135446Strhodesvoid 311135446Strhodesusbd_transfer_setup_sub(struct usb_setup_params *parm) 312135446Strhodes{ 313135446Strhodes enum { 314135446Strhodes REQ_SIZE = 8, 315135446Strhodes MIN_PKT = 8, 316135446Strhodes }; 317135446Strhodes struct usb_xfer *xfer = parm->curr_xfer; 318135446Strhodes const struct usb_config *setup = parm->curr_setup; 319135446Strhodes struct usb_endpoint_descriptor *edesc; 320135446Strhodes struct usb_std_packet_size std_size; 321135446Strhodes usb_frcount_t n_frlengths; 322135446Strhodes usb_frcount_t n_frbuffers; 323135446Strhodes usb_frcount_t x; 324135446Strhodes uint8_t type; 325135446Strhodes uint8_t zmps; 326135446Strhodes 327135446Strhodes /* 328135446Strhodes * Sanity check. The following parameters must be initialized before 329135446Strhodes * calling this function. 330135446Strhodes */ 331135446Strhodes if ((parm->hc_max_packet_size == 0) || 332135446Strhodes (parm->hc_max_packet_count == 0) || 333135446Strhodes (parm->hc_max_frame_size == 0)) { 334135446Strhodes parm->err = USB_ERR_INVAL; 335135446Strhodes goto done; 336135446Strhodes } 337135446Strhodes edesc = xfer->endpoint->edesc; 338135446Strhodes 339135446Strhodes type = (edesc->bmAttributes & UE_XFERTYPE); 340135446Strhodes 341135446Strhodes xfer->flags = setup->flags; 342135446Strhodes xfer->nframes = setup->frames; 343135446Strhodes xfer->timeout = setup->timeout; 344135446Strhodes xfer->callback = setup->callback; 345135446Strhodes xfer->interval = setup->interval; 346135446Strhodes xfer->endpointno = edesc->bEndpointAddress; 347135446Strhodes xfer->max_packet_size = UGETW(edesc->wMaxPacketSize); 348135446Strhodes xfer->max_packet_count = 1; 349135446Strhodes /* make a shadow copy: */ 350135446Strhodes xfer->flags_int.usb_mode = parm->udev->flags.usb_mode; 351135446Strhodes 352135446Strhodes parm->bufsize = setup->bufsize; 353135446Strhodes 354135446Strhodes if (parm->speed == USB_SPEED_HIGH) { 355135446Strhodes xfer->max_packet_count += (xfer->max_packet_size >> 11) & 3; 356135446Strhodes xfer->max_packet_size &= 0x7FF; 357135446Strhodes } 358135446Strhodes /* range check "max_packet_count" */ 359135446Strhodes 360135446Strhodes if (xfer->max_packet_count > parm->hc_max_packet_count) { 361135446Strhodes xfer->max_packet_count = parm->hc_max_packet_count; 362135446Strhodes } 363135446Strhodes /* filter "wMaxPacketSize" according to HC capabilities */ 364135446Strhodes 365135446Strhodes if ((xfer->max_packet_size > parm->hc_max_packet_size) || 366135446Strhodes (xfer->max_packet_size == 0)) { 367135446Strhodes xfer->max_packet_size = parm->hc_max_packet_size; 368135446Strhodes } 369135446Strhodes /* filter "wMaxPacketSize" according to standard sizes */ 370135446Strhodes 371135446Strhodes usbd_get_std_packet_size(&std_size, type, parm->speed); 372135446Strhodes 373135446Strhodes if (std_size.range.min || std_size.range.max) { 374135446Strhodes 375135446Strhodes if (xfer->max_packet_size < std_size.range.min) { 376135446Strhodes xfer->max_packet_size = std_size.range.min; 377135446Strhodes } 378135446Strhodes if (xfer->max_packet_size > std_size.range.max) { 379135446Strhodes xfer->max_packet_size = std_size.range.max; 380135446Strhodes } 381135446Strhodes } else { 382135446Strhodes 383135446Strhodes if (xfer->max_packet_size >= std_size.fixed[3]) { 384135446Strhodes xfer->max_packet_size = std_size.fixed[3]; 385135446Strhodes } else if (xfer->max_packet_size >= std_size.fixed[2]) { 386135446Strhodes xfer->max_packet_size = std_size.fixed[2]; 387135446Strhodes } else if (xfer->max_packet_size >= std_size.fixed[1]) { 388135446Strhodes xfer->max_packet_size = std_size.fixed[1]; 389135446Strhodes } else { 390135446Strhodes /* only one possibility left */ 391135446Strhodes xfer->max_packet_size = std_size.fixed[0]; 392135446Strhodes } 393135446Strhodes } 394135446Strhodes 395135446Strhodes /* compute "max_frame_size" */ 396135446Strhodes 397135446Strhodes usbd_update_max_frame_size(xfer); 398135446Strhodes 399135446Strhodes /* check interrupt interval and transfer pre-delay */ 400135446Strhodes 401135446Strhodes if (type == UE_ISOCHRONOUS) { 402135446Strhodes 403135446Strhodes uint16_t frame_limit; 404135446Strhodes 405135446Strhodes xfer->interval = 0; /* not used, must be zero */ 406135446Strhodes xfer->flags_int.isochronous_xfr = 1; /* set flag */ 407135446Strhodes 408135446Strhodes if (xfer->timeout == 0) { 409135446Strhodes /* 410135446Strhodes * set a default timeout in 411135446Strhodes * case something goes wrong! 412135446Strhodes */ 413135446Strhodes xfer->timeout = 1000 / 4; 414135446Strhodes } 415135446Strhodes switch (parm->speed) { 416135446Strhodes case USB_SPEED_LOW: 417135446Strhodes case USB_SPEED_FULL: 418135446Strhodes frame_limit = USB_MAX_FS_ISOC_FRAMES_PER_XFER; 419135446Strhodes xfer->fps_shift = 0; 420135446Strhodes break; 421135446Strhodes default: 422135446Strhodes frame_limit = USB_MAX_HS_ISOC_FRAMES_PER_XFER; 423135446Strhodes xfer->fps_shift = edesc->bInterval; 424135446Strhodes if (xfer->fps_shift > 0) 425135446Strhodes xfer->fps_shift--; 426135446Strhodes if (xfer->fps_shift > 3) 427135446Strhodes xfer->fps_shift = 3; 428135446Strhodes break; 429135446Strhodes } 430135446Strhodes 431135446Strhodes if (xfer->nframes > frame_limit) { 432135446Strhodes /* 433135446Strhodes * this is not going to work 434135446Strhodes * cross hardware 435135446Strhodes */ 436135446Strhodes parm->err = USB_ERR_INVAL; 437135446Strhodes goto done; 438135446Strhodes } 439135446Strhodes if (xfer->nframes == 0) { 440135446Strhodes /* 441135446Strhodes * this is not a valid value 442135446Strhodes */ 443135446Strhodes parm->err = USB_ERR_ZERO_NFRAMES; 444135446Strhodes goto done; 445135446Strhodes } 446135446Strhodes } else { 447135446Strhodes 448135446Strhodes /* 449135446Strhodes * if a value is specified use that else check the endpoint 450135446Strhodes * descriptor 451135446Strhodes */ 452135446Strhodes if (xfer->interval == 0) { 453135446Strhodes 454135446Strhodes if (type == UE_INTERRUPT) { 455135446Strhodes 456135446Strhodes xfer->interval = edesc->bInterval; 457135446Strhodes 458135446Strhodes switch (parm->speed) { 459135446Strhodes case USB_SPEED_SUPER: 460135446Strhodes case USB_SPEED_VARIABLE: 461135446Strhodes /* 125us -> 1ms */ 462135446Strhodes if (xfer->interval < 4) 463135446Strhodes xfer->interval = 1; 464135446Strhodes else if (xfer->interval > 16) 465135446Strhodes xfer->interval = (1<<(16-4)); 466135446Strhodes else 467135446Strhodes xfer->interval = 468135446Strhodes (1 << (xfer->interval-4)); 469135446Strhodes break; 470135446Strhodes case USB_SPEED_HIGH: 471135446Strhodes /* 125us -> 1ms */ 472135446Strhodes xfer->interval /= 8; 473135446Strhodes break; 474135446Strhodes default: 475135446Strhodes break; 476135446Strhodes } 477135446Strhodes if (xfer->interval == 0) { 478135446Strhodes /* 479135446Strhodes * One millisecond is the smallest 480135446Strhodes * interval we support: 481135446Strhodes */ 482135446Strhodes xfer->interval = 1; 483135446Strhodes } 484135446Strhodes } 485135446Strhodes } 486135446Strhodes } 487135446Strhodes 488135446Strhodes /* 489135446Strhodes * NOTE: we do not allow "max_packet_size" or "max_frame_size" 490135446Strhodes * to be equal to zero when setting up USB transfers, hence 491135446Strhodes * this leads to alot of extra code in the USB kernel. 492135446Strhodes */ 493135446Strhodes 494135446Strhodes if ((xfer->max_frame_size == 0) || 495135446Strhodes (xfer->max_packet_size == 0)) { 496135446Strhodes 497135446Strhodes zmps = 1; 498135446Strhodes 499135446Strhodes if ((parm->bufsize <= MIN_PKT) && 500135446Strhodes (type != UE_CONTROL) && 501135446Strhodes (type != UE_BULK)) { 502135446Strhodes 503135446Strhodes /* workaround */ 504135446Strhodes xfer->max_packet_size = MIN_PKT; 505135446Strhodes xfer->max_packet_count = 1; 506135446Strhodes parm->bufsize = 0; /* automatic setup length */ 507135446Strhodes usbd_update_max_frame_size(xfer); 508135446Strhodes 509135446Strhodes } else { 510135446Strhodes parm->err = USB_ERR_ZERO_MAXP; 511135446Strhodes goto done; 512135446Strhodes } 513135446Strhodes 514135446Strhodes } else { 515135446Strhodes zmps = 0; 516135446Strhodes } 517135446Strhodes 518135446Strhodes /* 519135446Strhodes * check if we should setup a default 520135446Strhodes * length: 521135446Strhodes */ 522135446Strhodes 523135446Strhodes if (parm->bufsize == 0) { 524135446Strhodes 525135446Strhodes parm->bufsize = xfer->max_frame_size; 526135446Strhodes 527135446Strhodes if (type == UE_ISOCHRONOUS) { 528135446Strhodes parm->bufsize *= xfer->nframes; 529135446Strhodes } 530135446Strhodes } 531135446Strhodes /* 532135446Strhodes * check if we are about to setup a proxy 533135446Strhodes * type of buffer: 534135446Strhodes */ 535135446Strhodes 536135446Strhodes if (xfer->flags.proxy_buffer) { 537135446Strhodes 538135446Strhodes /* round bufsize up */ 539135446Strhodes 540135446Strhodes parm->bufsize += (xfer->max_frame_size - 1); 541135446Strhodes 542135446Strhodes if (parm->bufsize < xfer->max_frame_size) { 543135446Strhodes /* length wrapped around */ 544135446Strhodes parm->err = USB_ERR_INVAL; 545135446Strhodes goto done; 546135446Strhodes } 547135446Strhodes /* subtract remainder */ 548135446Strhodes 549135446Strhodes parm->bufsize -= (parm->bufsize % xfer->max_frame_size); 550135446Strhodes 551135446Strhodes /* add length of USB device request structure, if any */ 552135446Strhodes 553135446Strhodes if (type == UE_CONTROL) { 554135446Strhodes parm->bufsize += REQ_SIZE; /* SETUP message */ 555135446Strhodes } 556135446Strhodes } 557135446Strhodes xfer->max_data_length = parm->bufsize; 558135446Strhodes 559135446Strhodes /* Setup "n_frlengths" and "n_frbuffers" */ 560135446Strhodes 561135446Strhodes if (type == UE_ISOCHRONOUS) { 562135446Strhodes n_frlengths = xfer->nframes; 563135446Strhodes n_frbuffers = 1; 564135446Strhodes } else { 565135446Strhodes 566135446Strhodes if (type == UE_CONTROL) { 567135446Strhodes xfer->flags_int.control_xfr = 1; 568135446Strhodes if (xfer->nframes == 0) { 569135446Strhodes if (parm->bufsize <= REQ_SIZE) { 570135446Strhodes /* 571135446Strhodes * there will never be any data 572135446Strhodes * stage 573135446Strhodes */ 574135446Strhodes xfer->nframes = 1; 575135446Strhodes } else { 576135446Strhodes xfer->nframes = 2; 577135446Strhodes } 578135446Strhodes } 579135446Strhodes } else { 580135446Strhodes if (xfer->nframes == 0) { 581135446Strhodes xfer->nframes = 1; 582135446Strhodes } 583135446Strhodes } 584135446Strhodes 585135446Strhodes n_frlengths = xfer->nframes; 586135446Strhodes n_frbuffers = xfer->nframes; 587135446Strhodes } 588135446Strhodes 589135446Strhodes /* 590135446Strhodes * check if we have room for the 591135446Strhodes * USB device request structure: 592135446Strhodes */ 593135446Strhodes 594135446Strhodes if (type == UE_CONTROL) { 595135446Strhodes 596135446Strhodes if (xfer->max_data_length < REQ_SIZE) { 597135446Strhodes /* length wrapped around or too small bufsize */ 598135446Strhodes parm->err = USB_ERR_INVAL; 599135446Strhodes goto done; 600135446Strhodes } 601135446Strhodes xfer->max_data_length -= REQ_SIZE; 602135446Strhodes } 603135446Strhodes /* setup "frlengths" */ 604135446Strhodes xfer->frlengths = parm->xfer_length_ptr; 605135446Strhodes parm->xfer_length_ptr += n_frlengths; 606135446Strhodes 607135446Strhodes /* setup "frbuffers" */ 608135446Strhodes xfer->frbuffers = parm->xfer_page_cache_ptr; 609135446Strhodes parm->xfer_page_cache_ptr += n_frbuffers; 610135446Strhodes 611135446Strhodes /* initialize max frame count */ 612135446Strhodes xfer->max_frame_count = xfer->nframes; 613135446Strhodes 614135446Strhodes /* 615135446Strhodes * check if we need to setup 616135446Strhodes * a local buffer: 617135446Strhodes */ 618135446Strhodes 619135446Strhodes if (!xfer->flags.ext_buffer) { 620135446Strhodes 621135446Strhodes /* align data */ 622135446Strhodes parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 623135446Strhodes 624135446Strhodes if (parm->buf) { 625135446Strhodes 626135446Strhodes xfer->local_buffer = 627135446Strhodes USB_ADD_BYTES(parm->buf, parm->size[0]); 628135446Strhodes 629135446Strhodes usbd_xfer_set_frame_offset(xfer, 0, 0); 630135446Strhodes 631135446Strhodes if ((type == UE_CONTROL) && (n_frbuffers > 1)) { 632135446Strhodes usbd_xfer_set_frame_offset(xfer, REQ_SIZE, 1); 633135446Strhodes } 634135446Strhodes } 635135446Strhodes parm->size[0] += parm->bufsize; 636135446Strhodes 637135446Strhodes /* align data again */ 638135446Strhodes parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 639135446Strhodes } 640135446Strhodes /* 641135446Strhodes * Compute maximum buffer size 642135446Strhodes */ 643135446Strhodes 644135446Strhodes if (parm->bufsize_max < parm->bufsize) { 645135446Strhodes parm->bufsize_max = parm->bufsize; 646135446Strhodes } 647135446Strhodes#if USB_HAVE_BUSDMA 648135446Strhodes if (xfer->flags_int.bdma_enable) { 649135446Strhodes /* 650135446Strhodes * Setup "dma_page_ptr". 651135446Strhodes * 652135446Strhodes * Proof for formula below: 653135446Strhodes * 654135446Strhodes * Assume there are three USB frames having length "a", "b" and 655135446Strhodes * "c". These USB frames will at maximum need "z" 656135446Strhodes * "usb_page" structures. "z" is given by: 657135446Strhodes * 658135446Strhodes * z = ((a / USB_PAGE_SIZE) + 2) + ((b / USB_PAGE_SIZE) + 2) + 659135446Strhodes * ((c / USB_PAGE_SIZE) + 2); 660135446Strhodes * 661135446Strhodes * Constraining "a", "b" and "c" like this: 662135446Strhodes * 663135446Strhodes * (a + b + c) <= parm->bufsize 664135446Strhodes * 665135446Strhodes * We know that: 666135446Strhodes * 667135446Strhodes * z <= ((parm->bufsize / USB_PAGE_SIZE) + (3*2)); 668135446Strhodes * 669135446Strhodes * Here is the general formula: 670135446Strhodes */ 671135446Strhodes xfer->dma_page_ptr = parm->dma_page_ptr; 672135446Strhodes parm->dma_page_ptr += (2 * n_frbuffers); 673135446Strhodes parm->dma_page_ptr += (parm->bufsize / USB_PAGE_SIZE); 674135446Strhodes } 675135446Strhodes#endif 676135446Strhodes if (zmps) { 677135446Strhodes /* correct maximum data length */ 678135446Strhodes xfer->max_data_length = 0; 679135446Strhodes } 680135446Strhodes /* subtract USB frame remainder from "hc_max_frame_size" */ 681135446Strhodes 682135446Strhodes xfer->max_hc_frame_size = 683135446Strhodes (parm->hc_max_frame_size - 684135446Strhodes (parm->hc_max_frame_size % xfer->max_frame_size)); 685135446Strhodes 686135446Strhodes if (xfer->max_hc_frame_size == 0) { 687135446Strhodes parm->err = USB_ERR_INVAL; 688135446Strhodes goto done; 689135446Strhodes } 690135446Strhodes 691135446Strhodes /* initialize frame buffers */ 692135446Strhodes 693135446Strhodes if (parm->buf) { 694135446Strhodes for (x = 0; x != n_frbuffers; x++) { 695135446Strhodes xfer->frbuffers[x].tag_parent = 696135446Strhodes &xfer->xroot->dma_parent_tag; 697135446Strhodes#if USB_HAVE_BUSDMA 698135446Strhodes if (xfer->flags_int.bdma_enable && 699135446Strhodes (parm->bufsize_max > 0)) { 700135446Strhodes 701135446Strhodes if (usb_pc_dmamap_create( 702135446Strhodes xfer->frbuffers + x, 703135446Strhodes parm->bufsize_max)) { 704135446Strhodes parm->err = USB_ERR_NOMEM; 705135446Strhodes goto done; 706135446Strhodes } 707135446Strhodes } 708135446Strhodes#endif 709135446Strhodes } 710135446Strhodes } 711135446Strhodesdone: 712135446Strhodes if (parm->err) { 713135446Strhodes /* 714135446Strhodes * Set some dummy values so that we avoid division by zero: 715135446Strhodes */ 716135446Strhodes xfer->max_hc_frame_size = 1; 717135446Strhodes xfer->max_frame_size = 1; 718135446Strhodes xfer->max_packet_size = 1; 719135446Strhodes xfer->max_data_length = 0; 720135446Strhodes xfer->nframes = 0; 721135446Strhodes xfer->max_frame_count = 0; 722135446Strhodes } 723135446Strhodes} 724135446Strhodes 725135446Strhodes/*------------------------------------------------------------------------* 726135446Strhodes * usbd_transfer_setup - setup an array of USB transfers 727135446Strhodes * 728135446Strhodes * NOTE: You must always call "usbd_transfer_unsetup" after calling 729135446Strhodes * "usbd_transfer_setup" if success was returned. 730135446Strhodes * 731135446Strhodes * The idea is that the USB device driver should pre-allocate all its 732135446Strhodes * transfers by one call to this function. 733135446Strhodes * 734135446Strhodes * Return values: 735135446Strhodes * 0: Success 736135446Strhodes * Else: Failure 737135446Strhodes *------------------------------------------------------------------------*/ 738135446Strhodesusb_error_t 739135446Strhodesusbd_transfer_setup(struct usb_device *udev, 740135446Strhodes const uint8_t *ifaces, struct usb_xfer **ppxfer, 741135446Strhodes const struct usb_config *setup_start, uint16_t n_setup, 742135446Strhodes void *priv_sc, struct mtx *xfer_mtx) 743135446Strhodes{ 744135446Strhodes struct usb_xfer dummy; 745135446Strhodes struct usb_setup_params parm; 746135446Strhodes const struct usb_config *setup_end = setup_start + n_setup; 747135446Strhodes const struct usb_config *setup; 748135446Strhodes struct usb_endpoint *ep; 749135446Strhodes struct usb_xfer_root *info; 750135446Strhodes struct usb_xfer *xfer; 751135446Strhodes void *buf = NULL; 752135446Strhodes uint16_t n; 753135446Strhodes uint16_t refcount; 754135446Strhodes 755135446Strhodes parm.err = 0; 756135446Strhodes refcount = 0; 757135446Strhodes info = NULL; 758135446Strhodes 759135446Strhodes WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 760135446Strhodes "usbd_transfer_setup can sleep!"); 761135446Strhodes 762135446Strhodes /* do some checking first */ 763135446Strhodes 764135446Strhodes if (n_setup == 0) { 765135446Strhodes DPRINTFN(6, "setup array has zero length!\n"); 766135446Strhodes return (USB_ERR_INVAL); 767135446Strhodes } 768135446Strhodes if (ifaces == 0) { 769135446Strhodes DPRINTFN(6, "ifaces array is NULL!\n"); 770135446Strhodes return (USB_ERR_INVAL); 771135446Strhodes } 772135446Strhodes if (xfer_mtx == NULL) { 773135446Strhodes DPRINTFN(6, "using global lock\n"); 774135446Strhodes xfer_mtx = &Giant; 775135446Strhodes } 776135446Strhodes /* sanity checks */ 777135446Strhodes for (setup = setup_start, n = 0; 778135446Strhodes setup != setup_end; setup++, n++) { 779135446Strhodes if (setup->bufsize == (usb_frlength_t)-1) { 780135446Strhodes parm.err = USB_ERR_BAD_BUFSIZE; 781135446Strhodes DPRINTF("invalid bufsize\n"); 782135446Strhodes } 783135446Strhodes if (setup->callback == NULL) { 784135446Strhodes parm.err = USB_ERR_NO_CALLBACK; 785135446Strhodes DPRINTF("no callback\n"); 786135446Strhodes } 787135446Strhodes ppxfer[n] = NULL; 788135446Strhodes } 789135446Strhodes 790135446Strhodes if (parm.err) { 791135446Strhodes goto done; 792135446Strhodes } 793135446Strhodes bzero(&parm, sizeof(parm)); 794135446Strhodes 795135446Strhodes parm.udev = udev; 796135446Strhodes parm.speed = usbd_get_speed(udev); 797135446Strhodes parm.hc_max_packet_count = 1; 798135446Strhodes 799135446Strhodes if (parm.speed >= USB_SPEED_MAX) { 800135446Strhodes parm.err = USB_ERR_INVAL; 801135446Strhodes goto done; 802135446Strhodes } 803135446Strhodes /* setup all transfers */ 804135446Strhodes 805135446Strhodes while (1) { 806135446Strhodes 807135446Strhodes if (buf) { 808135446Strhodes /* 809135446Strhodes * Initialize the "usb_xfer_root" structure, 810135446Strhodes * which is common for all our USB transfers. 811135446Strhodes */ 812135446Strhodes info = USB_ADD_BYTES(buf, 0); 813135446Strhodes 814135446Strhodes info->memory_base = buf; 815135446Strhodes info->memory_size = parm.size[0]; 816135446Strhodes 817135446Strhodes#if USB_HAVE_BUSDMA 818135446Strhodes info->dma_page_cache_start = USB_ADD_BYTES(buf, parm.size[4]); 819135446Strhodes info->dma_page_cache_end = USB_ADD_BYTES(buf, parm.size[5]); 820135446Strhodes#endif 821135446Strhodes info->xfer_page_cache_start = USB_ADD_BYTES(buf, parm.size[5]); 822135446Strhodes info->xfer_page_cache_end = USB_ADD_BYTES(buf, parm.size[2]); 823135446Strhodes 824135446Strhodes cv_init(&info->cv_drain, "WDRAIN"); 825135446Strhodes 826135446Strhodes info->xfer_mtx = xfer_mtx; 827135446Strhodes#if USB_HAVE_BUSDMA 828135446Strhodes usb_dma_tag_setup(&info->dma_parent_tag, 829135446Strhodes parm.dma_tag_p, udev->bus->dma_parent_tag[0].tag, 830135446Strhodes xfer_mtx, &usb_bdma_done_event, 32, parm.dma_tag_max); 831135446Strhodes#endif 832135446Strhodes 833135446Strhodes info->bus = udev->bus; 834135446Strhodes info->udev = udev; 835135446Strhodes 836135446Strhodes TAILQ_INIT(&info->done_q.head); 837135446Strhodes info->done_q.command = &usbd_callback_wrapper; 838135446Strhodes#if USB_HAVE_BUSDMA 839135446Strhodes TAILQ_INIT(&info->dma_q.head); 840135446Strhodes info->dma_q.command = &usb_bdma_work_loop; 841135446Strhodes#endif 842135446Strhodes info->done_m[0].hdr.pm_callback = &usb_callback_proc; 843135446Strhodes info->done_m[0].xroot = info; 844135446Strhodes info->done_m[1].hdr.pm_callback = &usb_callback_proc; 845135446Strhodes info->done_m[1].xroot = info; 846135446Strhodes 847135446Strhodes /* 848135446Strhodes * In device side mode control endpoint 849135446Strhodes * requests need to run from a separate 850135446Strhodes * context, else there is a chance of 851135446Strhodes * deadlock! 852135446Strhodes */ 853135446Strhodes if (setup_start == usb_control_ep_cfg) 854135446Strhodes info->done_p = 855135446Strhodes &udev->bus->control_xfer_proc; 856135446Strhodes else if (xfer_mtx == &Giant) 857135446Strhodes info->done_p = 858135446Strhodes &udev->bus->giant_callback_proc; 859135446Strhodes else 860135446Strhodes info->done_p = 861135446Strhodes &udev->bus->non_giant_callback_proc; 862135446Strhodes } 863135446Strhodes /* reset sizes */ 864135446Strhodes 865135446Strhodes parm.size[0] = 0; 866135446Strhodes parm.buf = buf; 867135446Strhodes parm.size[0] += sizeof(info[0]); 868135446Strhodes 869135446Strhodes for (setup = setup_start, n = 0; 870135446Strhodes setup != setup_end; setup++, n++) { 871135446Strhodes 872135446Strhodes /* skip USB transfers without callbacks: */ 873135446Strhodes if (setup->callback == NULL) { 874135446Strhodes continue; 875135446Strhodes } 876135446Strhodes /* see if there is a matching endpoint */ 877135446Strhodes ep = usbd_get_endpoint(udev, 878135446Strhodes ifaces[setup->if_index], setup); 879135446Strhodes 880135446Strhodes if ((ep == NULL) || (ep->methods == NULL)) { 881135446Strhodes if (setup->flags.no_pipe_ok) 882135446Strhodes continue; 883135446Strhodes if ((setup->usb_mode != USB_MODE_DUAL) && 884135446Strhodes (setup->usb_mode != udev->flags.usb_mode)) 885135446Strhodes continue; 886135446Strhodes parm.err = USB_ERR_NO_PIPE; 887135446Strhodes goto done; 888135446Strhodes } 889135446Strhodes 890135446Strhodes /* align data properly */ 891135446Strhodes parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 892135446Strhodes 893135446Strhodes /* store current setup pointer */ 894135446Strhodes parm.curr_setup = setup; 895135446Strhodes 896135446Strhodes if (buf) { 897135446Strhodes /* 898135446Strhodes * Common initialization of the 899135446Strhodes * "usb_xfer" structure. 900135446Strhodes */ 901135446Strhodes xfer = USB_ADD_BYTES(buf, parm.size[0]); 902135446Strhodes xfer->address = udev->address; 903135446Strhodes xfer->priv_sc = priv_sc; 904135446Strhodes xfer->xroot = info; 905135446Strhodes 906135446Strhodes usb_callout_init_mtx(&xfer->timeout_handle, 907135446Strhodes &udev->bus->bus_mtx, 0); 908135446Strhodes } else { 909135446Strhodes /* 910135446Strhodes * Setup a dummy xfer, hence we are 911135446Strhodes * writing to the "usb_xfer" 912135446Strhodes * structure pointed to by "xfer" 913135446Strhodes * before we have allocated any 914135446Strhodes * memory: 915135446Strhodes */ 916135446Strhodes xfer = &dummy; 917135446Strhodes bzero(&dummy, sizeof(dummy)); 918135446Strhodes refcount++; 919135446Strhodes } 920135446Strhodes 921135446Strhodes /* set transfer endpoint pointer */ 922135446Strhodes xfer->endpoint = ep; 923135446Strhodes 924135446Strhodes parm.size[0] += sizeof(xfer[0]); 925135446Strhodes parm.methods = xfer->endpoint->methods; 926135446Strhodes parm.curr_xfer = xfer; 927135446Strhodes 928135446Strhodes /* 929135446Strhodes * Call the Host or Device controller transfer 930135446Strhodes * setup routine: 931135446Strhodes */ 932135446Strhodes (udev->bus->methods->xfer_setup) (&parm); 933135446Strhodes 934135446Strhodes /* check for error */ 935135446Strhodes if (parm.err) 936135446Strhodes goto done; 937135446Strhodes 938135446Strhodes if (buf) { 939135446Strhodes /* 940135446Strhodes * Increment the endpoint refcount. This 941135446Strhodes * basically prevents setting a new 942135446Strhodes * configuration and alternate setting 943135446Strhodes * when USB transfers are in use on 944135446Strhodes * the given interface. Search the USB 945135446Strhodes * code for "endpoint->refcount_alloc" if you 946135446Strhodes * want more information. 947135446Strhodes */ 948135446Strhodes USB_BUS_LOCK(info->bus); 949135446Strhodes if (xfer->endpoint->refcount_alloc >= USB_EP_REF_MAX) 950135446Strhodes parm.err = USB_ERR_INVAL; 951135446Strhodes 952135446Strhodes xfer->endpoint->refcount_alloc++; 953135446Strhodes 954135446Strhodes if (xfer->endpoint->refcount_alloc == 0) 955135446Strhodes panic("usbd_transfer_setup(): Refcount wrapped to zero\n"); 956135446Strhodes USB_BUS_UNLOCK(info->bus); 957135446Strhodes 958135446Strhodes /* 959135446Strhodes * Whenever we set ppxfer[] then we 960135446Strhodes * also need to increment the 961135446Strhodes * "setup_refcount": 962135446Strhodes */ 963135446Strhodes info->setup_refcount++; 964135446Strhodes 965135446Strhodes /* 966135446Strhodes * Transfer is successfully setup and 967135446Strhodes * can be used: 968135446Strhodes */ 969135446Strhodes ppxfer[n] = xfer; 970135446Strhodes } 971135446Strhodes 972135446Strhodes /* check for error */ 973135446Strhodes if (parm.err) 974135446Strhodes goto done; 975135446Strhodes } 976135446Strhodes 977135446Strhodes if (buf || parm.err) { 978135446Strhodes goto done; 979135446Strhodes } 980135446Strhodes if (refcount == 0) { 981135446Strhodes /* no transfers - nothing to do ! */ 982135446Strhodes goto done; 983135446Strhodes } 984135446Strhodes /* align data properly */ 985135446Strhodes parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 986135446Strhodes 987135446Strhodes /* store offset temporarily */ 988135446Strhodes parm.size[1] = parm.size[0]; 989135446Strhodes 990135446Strhodes /* 991135446Strhodes * The number of DMA tags required depends on 992135446Strhodes * the number of endpoints. The current estimate 993135446Strhodes * for maximum number of DMA tags per endpoint 994135446Strhodes * is two. 995135446Strhodes */ 996135446Strhodes parm.dma_tag_max += 2 * MIN(n_setup, USB_EP_MAX); 997135446Strhodes 998135446Strhodes /* 999135446Strhodes * DMA tags for QH, TD, Data and more. 1000135446Strhodes */ 1001135446Strhodes parm.dma_tag_max += 8; 1002135446Strhodes 1003135446Strhodes parm.dma_tag_p += parm.dma_tag_max; 1004135446Strhodes 1005135446Strhodes parm.size[0] += ((uint8_t *)parm.dma_tag_p) - 1006135446Strhodes ((uint8_t *)0); 1007135446Strhodes 1008135446Strhodes /* align data properly */ 1009135446Strhodes parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1010135446Strhodes 1011135446Strhodes /* store offset temporarily */ 1012135446Strhodes parm.size[3] = parm.size[0]; 1013135446Strhodes 1014135446Strhodes parm.size[0] += ((uint8_t *)parm.dma_page_ptr) - 1015135446Strhodes ((uint8_t *)0); 1016135446Strhodes 1017135446Strhodes /* align data properly */ 1018135446Strhodes parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1019135446Strhodes 1020135446Strhodes /* store offset temporarily */ 1021135446Strhodes parm.size[4] = parm.size[0]; 1022135446Strhodes 1023135446Strhodes parm.size[0] += ((uint8_t *)parm.dma_page_cache_ptr) - 1024135446Strhodes ((uint8_t *)0); 1025135446Strhodes 1026135446Strhodes /* store end offset temporarily */ 1027135446Strhodes parm.size[5] = parm.size[0]; 1028135446Strhodes 1029135446Strhodes parm.size[0] += ((uint8_t *)parm.xfer_page_cache_ptr) - 1030135446Strhodes ((uint8_t *)0); 1031135446Strhodes 1032135446Strhodes /* store end offset temporarily */ 1033135446Strhodes 1034135446Strhodes parm.size[2] = parm.size[0]; 1035135446Strhodes 1036135446Strhodes /* align data properly */ 1037135446Strhodes parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1038135446Strhodes 1039135446Strhodes parm.size[6] = parm.size[0]; 1040135446Strhodes 1041135446Strhodes parm.size[0] += ((uint8_t *)parm.xfer_length_ptr) - 1042135446Strhodes ((uint8_t *)0); 1043135446Strhodes 1044135446Strhodes /* align data properly */ 1045135446Strhodes parm.size[0] += ((-parm.size[0]) & (USB_HOST_ALIGN - 1)); 1046135446Strhodes 1047135446Strhodes /* allocate zeroed memory */ 1048135446Strhodes buf = malloc(parm.size[0], M_USB, M_WAITOK | M_ZERO); 1049135446Strhodes 1050135446Strhodes if (buf == NULL) { 1051135446Strhodes parm.err = USB_ERR_NOMEM; 1052135446Strhodes DPRINTFN(0, "cannot allocate memory block for " 1053135446Strhodes "configuration (%d bytes)\n", 1054135446Strhodes parm.size[0]); 1055135446Strhodes goto done; 1056135446Strhodes } 1057135446Strhodes parm.dma_tag_p = USB_ADD_BYTES(buf, parm.size[1]); 1058135446Strhodes parm.dma_page_ptr = USB_ADD_BYTES(buf, parm.size[3]); 1059135446Strhodes parm.dma_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[4]); 1060135446Strhodes parm.xfer_page_cache_ptr = USB_ADD_BYTES(buf, parm.size[5]); 1061135446Strhodes parm.xfer_length_ptr = USB_ADD_BYTES(buf, parm.size[6]); 1062135446Strhodes } 1063135446Strhodes 1064135446Strhodesdone: 1065135446Strhodes if (buf) { 1066135446Strhodes if (info->setup_refcount == 0) { 1067135446Strhodes /* 1068135446Strhodes * "usbd_transfer_unsetup_sub" will unlock 1069135446Strhodes * the bus mutex before returning ! 1070135446Strhodes */ 1071135446Strhodes USB_BUS_LOCK(info->bus); 1072135446Strhodes 1073135446Strhodes /* something went wrong */ 1074135446Strhodes usbd_transfer_unsetup_sub(info, 0); 1075135446Strhodes } 1076135446Strhodes } 1077135446Strhodes if (parm.err) { 1078135446Strhodes usbd_transfer_unsetup(ppxfer, n_setup); 1079135446Strhodes } 1080135446Strhodes return (parm.err); 1081135446Strhodes} 1082135446Strhodes 1083135446Strhodes/*------------------------------------------------------------------------* 1084135446Strhodes * usbd_transfer_unsetup_sub - factored out code 1085135446Strhodes *------------------------------------------------------------------------*/ 1086135446Strhodesstatic void 1087135446Strhodesusbd_transfer_unsetup_sub(struct usb_xfer_root *info, uint8_t needs_delay) 1088135446Strhodes{ 1089135446Strhodes struct usb_page_cache *pc; 1090135446Strhodes 1091135446Strhodes USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 1092135446Strhodes 1093135446Strhodes /* wait for any outstanding DMA operations */ 1094135446Strhodes 1095135446Strhodes if (needs_delay) { 1096135446Strhodes usb_timeout_t temp; 1097135446Strhodes temp = usbd_get_dma_delay(info->bus); 1098135446Strhodes usb_pause_mtx(&info->bus->bus_mtx, 1099135446Strhodes USB_MS_TO_TICKS(temp)); 1100135446Strhodes } 1101135446Strhodes 1102135446Strhodes /* make sure that our done messages are not queued anywhere */ 1103135446Strhodes usb_proc_mwait(info->done_p, &info->done_m[0], &info->done_m[1]); 1104135446Strhodes 1105135446Strhodes USB_BUS_UNLOCK(info->bus); 1106135446Strhodes 1107135446Strhodes#if USB_HAVE_BUSDMA 1108135446Strhodes /* free DMA'able memory, if any */ 1109135446Strhodes pc = info->dma_page_cache_start; 1110135446Strhodes while (pc != info->dma_page_cache_end) { 1111135446Strhodes usb_pc_free_mem(pc); 1112135446Strhodes pc++; 1113135446Strhodes } 1114135446Strhodes 1115135446Strhodes /* free DMA maps in all "xfer->frbuffers" */ 1116135446Strhodes pc = info->xfer_page_cache_start; 1117135446Strhodes while (pc != info->xfer_page_cache_end) { 1118135446Strhodes usb_pc_dmamap_destroy(pc); 1119135446Strhodes pc++; 1120135446Strhodes } 1121135446Strhodes 1122135446Strhodes /* free all DMA tags */ 1123135446Strhodes usb_dma_tag_unsetup(&info->dma_parent_tag); 1124135446Strhodes#endif 1125135446Strhodes 1126135446Strhodes cv_destroy(&info->cv_drain); 1127135446Strhodes 1128135446Strhodes /* 1129135446Strhodes * free the "memory_base" last, hence the "info" structure is 1130135446Strhodes * contained within the "memory_base"! 1131135446Strhodes */ 1132135446Strhodes free(info->memory_base, M_USB); 1133135446Strhodes} 1134135446Strhodes 1135135446Strhodes/*------------------------------------------------------------------------* 1136135446Strhodes * usbd_transfer_unsetup - unsetup/free an array of USB transfers 1137135446Strhodes * 1138135446Strhodes * NOTE: All USB transfers in progress will get called back passing 1139135446Strhodes * the error code "USB_ERR_CANCELLED" before this function 1140135446Strhodes * returns. 1141135446Strhodes *------------------------------------------------------------------------*/ 1142135446Strhodesvoid 1143135446Strhodesusbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup) 1144135446Strhodes{ 1145135446Strhodes struct usb_xfer *xfer; 1146135446Strhodes struct usb_xfer_root *info; 1147135446Strhodes uint8_t needs_delay = 0; 1148135446Strhodes 1149135446Strhodes WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1150135446Strhodes "usbd_transfer_unsetup can sleep!"); 1151135446Strhodes 1152135446Strhodes while (n_setup--) { 1153135446Strhodes xfer = pxfer[n_setup]; 1154135446Strhodes 1155135446Strhodes if (xfer == NULL) 1156135446Strhodes continue; 1157135446Strhodes 1158135446Strhodes info = xfer->xroot; 1159135446Strhodes 1160135446Strhodes USB_XFER_LOCK(xfer); 1161135446Strhodes USB_BUS_LOCK(info->bus); 1162135446Strhodes 1163135446Strhodes /* 1164135446Strhodes * HINT: when you start/stop a transfer, it might be a 1165135446Strhodes * good idea to directly use the "pxfer[]" structure: 1166135446Strhodes * 1167135446Strhodes * usbd_transfer_start(sc->pxfer[0]); 1168135446Strhodes * usbd_transfer_stop(sc->pxfer[0]); 1169135446Strhodes * 1170135446Strhodes * That way, if your code has many parts that will not 1171135446Strhodes * stop running under the same lock, in other words 1172135446Strhodes * "xfer_mtx", the usbd_transfer_start and 1173135446Strhodes * usbd_transfer_stop functions will simply return 1174135446Strhodes * when they detect a NULL pointer argument. 1175135446Strhodes * 1176135446Strhodes * To avoid any races we clear the "pxfer[]" pointer 1177135446Strhodes * while holding the private mutex of the driver: 1178135446Strhodes */ 1179135446Strhodes pxfer[n_setup] = NULL; 1180135446Strhodes 1181135446Strhodes USB_BUS_UNLOCK(info->bus); 1182135446Strhodes USB_XFER_UNLOCK(xfer); 1183135446Strhodes 1184135446Strhodes usbd_transfer_drain(xfer); 1185135446Strhodes 1186135446Strhodes#if USB_HAVE_BUSDMA 1187135446Strhodes if (xfer->flags_int.bdma_enable) 1188135446Strhodes needs_delay = 1; 1189135446Strhodes#endif 1190135446Strhodes /* 1191135446Strhodes * NOTE: default endpoint does not have an 1192135446Strhodes * interface, even if endpoint->iface_index == 0 1193135446Strhodes */ 1194135446Strhodes USB_BUS_LOCK(info->bus); 1195135446Strhodes xfer->endpoint->refcount_alloc--; 1196135446Strhodes USB_BUS_UNLOCK(info->bus); 1197135446Strhodes 1198135446Strhodes usb_callout_drain(&xfer->timeout_handle); 1199135446Strhodes 1200135446Strhodes USB_BUS_LOCK(info->bus); 1201135446Strhodes 1202135446Strhodes USB_ASSERT(info->setup_refcount != 0, ("Invalid setup " 1203135446Strhodes "reference count\n")); 1204135446Strhodes 1205135446Strhodes info->setup_refcount--; 1206135446Strhodes 1207135446Strhodes if (info->setup_refcount == 0) { 1208135446Strhodes usbd_transfer_unsetup_sub(info, 1209135446Strhodes needs_delay); 1210135446Strhodes } else { 1211135446Strhodes USB_BUS_UNLOCK(info->bus); 1212135446Strhodes } 1213135446Strhodes } 1214135446Strhodes} 1215135446Strhodes 1216135446Strhodes/*------------------------------------------------------------------------* 1217135446Strhodes * usbd_control_transfer_init - factored out code 1218135446Strhodes * 1219135446Strhodes * In USB Device Mode we have to wait for the SETUP packet which 1220135446Strhodes * containst the "struct usb_device_request" structure, before we can 1221135446Strhodes * transfer any data. In USB Host Mode we already have the SETUP 1222135446Strhodes * packet at the moment the USB transfer is started. This leads us to 1223135446Strhodes * having to setup the USB transfer at two different places in 1224135446Strhodes * time. This function just contains factored out control transfer 1225135446Strhodes * initialisation code, so that we don't duplicate the code. 1226135446Strhodes *------------------------------------------------------------------------*/ 1227135446Strhodesstatic void 1228135446Strhodesusbd_control_transfer_init(struct usb_xfer *xfer) 1229135446Strhodes{ 1230135446Strhodes struct usb_device_request req; 1231135446Strhodes 1232135446Strhodes /* copy out the USB request header */ 1233135446Strhodes 1234135446Strhodes usbd_copy_out(xfer->frbuffers, 0, &req, sizeof(req)); 1235135446Strhodes 1236135446Strhodes /* setup remainder */ 1237135446Strhodes 1238135446Strhodes xfer->flags_int.control_rem = UGETW(req.wLength); 1239135446Strhodes 1240135446Strhodes /* copy direction to endpoint variable */ 1241135446Strhodes 1242135446Strhodes xfer->endpointno &= ~(UE_DIR_IN | UE_DIR_OUT); 1243135446Strhodes xfer->endpointno |= 1244135446Strhodes (req.bmRequestType & UT_READ) ? UE_DIR_IN : UE_DIR_OUT; 1245135446Strhodes} 1246135446Strhodes 1247135446Strhodes/*------------------------------------------------------------------------* 1248135446Strhodes * usbd_setup_ctrl_transfer 1249135446Strhodes * 1250135446Strhodes * This function handles initialisation of control transfers. Control 1251135446Strhodes * transfers are special in that regard that they can both transmit 1252135446Strhodes * and receive data. 1253135446Strhodes * 1254135446Strhodes * Return values: 1255135446Strhodes * 0: Success 1256135446Strhodes * Else: Failure 1257135446Strhodes *------------------------------------------------------------------------*/ 1258135446Strhodesstatic int 1259135446Strhodesusbd_setup_ctrl_transfer(struct usb_xfer *xfer) 1260135446Strhodes{ 1261135446Strhodes usb_frlength_t len; 1262135446Strhodes 1263135446Strhodes /* Check for control endpoint stall */ 1264135446Strhodes if (xfer->flags.stall_pipe && xfer->flags_int.control_act) { 1265135446Strhodes /* the control transfer is no longer active */ 1266135446Strhodes xfer->flags_int.control_stall = 1; 1267135446Strhodes xfer->flags_int.control_act = 0; 1268135446Strhodes } else { 1269135446Strhodes /* don't stall control transfer by default */ 1270135446Strhodes xfer->flags_int.control_stall = 0; 1271135446Strhodes } 1272135446Strhodes 1273135446Strhodes /* Check for invalid number of frames */ 1274135446Strhodes if (xfer->nframes > 2) { 1275135446Strhodes /* 1276135446Strhodes * If you need to split a control transfer, you 1277135446Strhodes * have to do one part at a time. Only with 1278135446Strhodes * non-control transfers you can do multiple 1279135446Strhodes * parts a time. 1280135446Strhodes */ 1281135446Strhodes DPRINTFN(0, "Too many frames: %u\n", 1282135446Strhodes (unsigned int)xfer->nframes); 1283135446Strhodes goto error; 1284135446Strhodes } 1285135446Strhodes 1286135446Strhodes /* 1287135446Strhodes * Check if there is a control 1288135446Strhodes * transfer in progress: 1289135446Strhodes */ 1290135446Strhodes if (xfer->flags_int.control_act) { 1291135446Strhodes 1292135446Strhodes if (xfer->flags_int.control_hdr) { 1293135446Strhodes 1294135446Strhodes /* clear send header flag */ 1295135446Strhodes 1296135446Strhodes xfer->flags_int.control_hdr = 0; 1297135446Strhodes 1298135446Strhodes /* setup control transfer */ 1299135446Strhodes if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1300135446Strhodes usbd_control_transfer_init(xfer); 1301135446Strhodes } 1302135446Strhodes } 1303135446Strhodes /* get data length */ 1304135446Strhodes 1305135446Strhodes len = xfer->sumlen; 1306135446Strhodes 1307135446Strhodes } else { 1308135446Strhodes 1309135446Strhodes /* the size of the SETUP structure is hardcoded ! */ 1310135446Strhodes 1311135446Strhodes if (xfer->frlengths[0] != sizeof(struct usb_device_request)) { 1312135446Strhodes DPRINTFN(0, "Wrong framelength %u != %zu\n", 1313135446Strhodes xfer->frlengths[0], sizeof(struct 1314135446Strhodes usb_device_request)); 1315135446Strhodes goto error; 1316135446Strhodes } 1317135446Strhodes /* check USB mode */ 1318135446Strhodes if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1319135446Strhodes 1320135446Strhodes /* check number of frames */ 1321135446Strhodes if (xfer->nframes != 1) { 1322135446Strhodes /* 1323135446Strhodes * We need to receive the setup 1324135446Strhodes * message first so that we know the 1325135446Strhodes * data direction! 1326135446Strhodes */ 1327135446Strhodes DPRINTF("Misconfigured transfer\n"); 1328135446Strhodes goto error; 1329135446Strhodes } 1330135446Strhodes /* 1331135446Strhodes * Set a dummy "control_rem" value. This 1332135446Strhodes * variable will be overwritten later by a 1333135446Strhodes * call to "usbd_control_transfer_init()" ! 1334135446Strhodes */ 1335135446Strhodes xfer->flags_int.control_rem = 0xFFFF; 1336135446Strhodes } else { 1337135446Strhodes 1338135446Strhodes /* setup "endpoint" and "control_rem" */ 1339135446Strhodes 1340135446Strhodes usbd_control_transfer_init(xfer); 1341135446Strhodes } 1342135446Strhodes 1343135446Strhodes /* set transfer-header flag */ 1344135446Strhodes 1345135446Strhodes xfer->flags_int.control_hdr = 1; 1346135446Strhodes 1347135446Strhodes /* get data length */ 1348135446Strhodes 1349135446Strhodes len = (xfer->sumlen - sizeof(struct usb_device_request)); 1350135446Strhodes } 1351135446Strhodes 1352135446Strhodes /* check if there is a length mismatch */ 1353135446Strhodes 1354135446Strhodes if (len > xfer->flags_int.control_rem) { 1355135446Strhodes DPRINTFN(0, "Length (%d) greater than " 1356135446Strhodes "remaining length (%d)\n", len, 1357135446Strhodes xfer->flags_int.control_rem); 1358135446Strhodes goto error; 1359135446Strhodes } 1360135446Strhodes /* check if we are doing a short transfer */ 1361135446Strhodes 1362135446Strhodes if (xfer->flags.force_short_xfer) { 1363135446Strhodes xfer->flags_int.control_rem = 0; 1364135446Strhodes } else { 1365135446Strhodes if ((len != xfer->max_data_length) && 1366135446Strhodes (len != xfer->flags_int.control_rem) && 1367135446Strhodes (xfer->nframes != 1)) { 1368135446Strhodes DPRINTFN(0, "Short control transfer without " 1369135446Strhodes "force_short_xfer set\n"); 1370135446Strhodes goto error; 1371135446Strhodes } 1372135446Strhodes xfer->flags_int.control_rem -= len; 1373135446Strhodes } 1374135446Strhodes 1375135446Strhodes /* the status part is executed when "control_act" is 0 */ 1376135446Strhodes 1377135446Strhodes if ((xfer->flags_int.control_rem > 0) || 1378135446Strhodes (xfer->flags.manual_status)) { 1379135446Strhodes /* don't execute the STATUS stage yet */ 1380135446Strhodes xfer->flags_int.control_act = 1; 1381135446Strhodes 1382135446Strhodes /* sanity check */ 1383135446Strhodes if ((!xfer->flags_int.control_hdr) && 1384135446Strhodes (xfer->nframes == 1)) { 1385135446Strhodes /* 1386135446Strhodes * This is not a valid operation! 1387135446Strhodes */ 1388135446Strhodes DPRINTFN(0, "Invalid parameter " 1389135446Strhodes "combination\n"); 1390135446Strhodes goto error; 1391135446Strhodes } 1392135446Strhodes } else { 1393135446Strhodes /* time to execute the STATUS stage */ 1394135446Strhodes xfer->flags_int.control_act = 0; 1395135446Strhodes } 1396135446Strhodes return (0); /* success */ 1397135446Strhodes 1398135446Strhodeserror: 1399135446Strhodes return (1); /* failure */ 1400135446Strhodes} 1401135446Strhodes 1402135446Strhodes/*------------------------------------------------------------------------* 1403135446Strhodes * usbd_transfer_submit - start USB hardware for the given transfer 1404135446Strhodes * 1405135446Strhodes * This function should only be called from the USB callback. 1406135446Strhodes *------------------------------------------------------------------------*/ 1407135446Strhodesvoid 1408135446Strhodesusbd_transfer_submit(struct usb_xfer *xfer) 1409135446Strhodes{ 1410135446Strhodes struct usb_xfer_root *info; 1411135446Strhodes struct usb_bus *bus; 1412135446Strhodes usb_frcount_t x; 1413135446Strhodes 1414135446Strhodes info = xfer->xroot; 1415135446Strhodes bus = info->bus; 1416135446Strhodes 1417135446Strhodes DPRINTF("xfer=%p, endpoint=%p, nframes=%d, dir=%s\n", 1418135446Strhodes xfer, xfer->endpoint, xfer->nframes, USB_GET_DATA_ISREAD(xfer) ? 1419135446Strhodes "read" : "write"); 1420135446Strhodes 1421135446Strhodes#if USB_DEBUG 1422135446Strhodes if (USB_DEBUG_VAR > 0) { 1423135446Strhodes USB_BUS_LOCK(bus); 1424135446Strhodes 1425135446Strhodes usb_dump_endpoint(xfer->endpoint); 1426135446Strhodes 1427135446Strhodes USB_BUS_UNLOCK(bus); 1428135446Strhodes } 1429135446Strhodes#endif 1430135446Strhodes 1431135446Strhodes USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1432135446Strhodes USB_BUS_LOCK_ASSERT(bus, MA_NOTOWNED); 1433135446Strhodes 1434135446Strhodes /* Only open the USB transfer once! */ 1435135446Strhodes if (!xfer->flags_int.open) { 1436135446Strhodes xfer->flags_int.open = 1; 1437135446Strhodes 1438135446Strhodes DPRINTF("open\n"); 1439135446Strhodes 1440135446Strhodes USB_BUS_LOCK(bus); 1441135446Strhodes (xfer->endpoint->methods->open) (xfer); 1442135446Strhodes USB_BUS_UNLOCK(bus); 1443135446Strhodes } 1444135446Strhodes /* set "transferring" flag */ 1445135446Strhodes xfer->flags_int.transferring = 1; 1446135446Strhodes 1447135446Strhodes#if USB_HAVE_POWERD 1448135446Strhodes /* increment power reference */ 1449135446Strhodes usbd_transfer_power_ref(xfer, 1); 1450135446Strhodes#endif 1451135446Strhodes /* 1452135446Strhodes * Check if the transfer is waiting on a queue, most 1453135446Strhodes * frequently the "done_q": 1454135446Strhodes */ 1455135446Strhodes if (xfer->wait_queue) { 1456135446Strhodes USB_BUS_LOCK(bus); 1457135446Strhodes usbd_transfer_dequeue(xfer); 1458135446Strhodes USB_BUS_UNLOCK(bus); 1459135446Strhodes } 1460135446Strhodes /* clear "did_dma_delay" flag */ 1461135446Strhodes xfer->flags_int.did_dma_delay = 0; 1462135446Strhodes 1463135446Strhodes /* clear "did_close" flag */ 1464135446Strhodes xfer->flags_int.did_close = 0; 1465135446Strhodes 1466135446Strhodes#if USB_HAVE_BUSDMA 1467135446Strhodes /* clear "bdma_setup" flag */ 1468135446Strhodes xfer->flags_int.bdma_setup = 0; 1469135446Strhodes#endif 1470135446Strhodes /* by default we cannot cancel any USB transfer immediately */ 1471135446Strhodes xfer->flags_int.can_cancel_immed = 0; 1472135446Strhodes 1473135446Strhodes /* clear lengths and frame counts by default */ 1474135446Strhodes xfer->sumlen = 0; 1475135446Strhodes xfer->actlen = 0; 1476135446Strhodes xfer->aframes = 0; 1477135446Strhodes 1478135446Strhodes /* clear any previous errors */ 1479135446Strhodes xfer->error = 0; 1480135446Strhodes 1481135446Strhodes /* Check if the device is still alive */ 1482135446Strhodes if (info->udev->state < USB_STATE_POWERED) { 1483135446Strhodes USB_BUS_LOCK(bus); 1484135446Strhodes /* 1485135446Strhodes * Must return cancelled error code else 1486135446Strhodes * device drivers can hang. 1487135446Strhodes */ 1488135446Strhodes usbd_transfer_done(xfer, USB_ERR_CANCELLED); 1489135446Strhodes USB_BUS_UNLOCK(bus); 1490135446Strhodes return; 1491135446Strhodes } 1492135446Strhodes 1493135446Strhodes /* sanity check */ 1494135446Strhodes if (xfer->nframes == 0) { 1495135446Strhodes if (xfer->flags.stall_pipe) { 1496135446Strhodes /* 1497135446Strhodes * Special case - want to stall without transferring 1498135446Strhodes * any data: 1499135446Strhodes */ 1500135446Strhodes DPRINTF("xfer=%p nframes=0: stall " 1501135446Strhodes "or clear stall!\n", xfer); 1502135446Strhodes USB_BUS_LOCK(bus); 1503135446Strhodes xfer->flags_int.can_cancel_immed = 1; 1504135446Strhodes /* start the transfer */ 1505135446Strhodes usb_command_wrapper(&xfer->endpoint->endpoint_q, xfer); 1506135446Strhodes USB_BUS_UNLOCK(bus); 1507135446Strhodes return; 1508135446Strhodes } 1509135446Strhodes USB_BUS_LOCK(bus); 1510135446Strhodes usbd_transfer_done(xfer, USB_ERR_INVAL); 1511135446Strhodes USB_BUS_UNLOCK(bus); 1512135446Strhodes return; 1513135446Strhodes } 1514135446Strhodes /* compute total transfer length */ 1515135446Strhodes 1516135446Strhodes for (x = 0; x != xfer->nframes; x++) { 1517135446Strhodes xfer->sumlen += xfer->frlengths[x]; 1518135446Strhodes if (xfer->sumlen < xfer->frlengths[x]) { 1519135446Strhodes /* length wrapped around */ 1520135446Strhodes USB_BUS_LOCK(bus); 1521135446Strhodes usbd_transfer_done(xfer, USB_ERR_INVAL); 1522135446Strhodes USB_BUS_UNLOCK(bus); 1523135446Strhodes return; 1524135446Strhodes } 1525135446Strhodes } 1526135446Strhodes 1527135446Strhodes /* clear some internal flags */ 1528135446Strhodes 1529135446Strhodes xfer->flags_int.short_xfer_ok = 0; 1530135446Strhodes xfer->flags_int.short_frames_ok = 0; 1531135446Strhodes 1532135446Strhodes /* check if this is a control transfer */ 1533135446Strhodes 1534135446Strhodes if (xfer->flags_int.control_xfr) { 1535135446Strhodes 1536135446Strhodes if (usbd_setup_ctrl_transfer(xfer)) { 1537135446Strhodes USB_BUS_LOCK(bus); 1538135446Strhodes usbd_transfer_done(xfer, USB_ERR_STALLED); 1539135446Strhodes USB_BUS_UNLOCK(bus); 1540135446Strhodes return; 1541135446Strhodes } 1542135446Strhodes } 1543135446Strhodes /* 1544135446Strhodes * Setup filtered version of some transfer flags, 1545135446Strhodes * in case of data read direction 1546135446Strhodes */ 1547135446Strhodes if (USB_GET_DATA_ISREAD(xfer)) { 1548135446Strhodes 1549135446Strhodes if (xfer->flags.short_frames_ok) { 1550135446Strhodes xfer->flags_int.short_xfer_ok = 1; 1551135446Strhodes xfer->flags_int.short_frames_ok = 1; 1552135446Strhodes } else if (xfer->flags.short_xfer_ok) { 1553135446Strhodes xfer->flags_int.short_xfer_ok = 1; 1554135446Strhodes 1555135446Strhodes /* check for control transfer */ 1556135446Strhodes if (xfer->flags_int.control_xfr) { 1557135446Strhodes /* 1558135446Strhodes * 1) Control transfers do not support 1559135446Strhodes * reception of multiple short USB 1560135446Strhodes * frames in host mode and device side 1561135446Strhodes * mode, with exception of: 1562135446Strhodes * 1563135446Strhodes * 2) Due to sometimes buggy device 1564135446Strhodes * side firmware we need to do a 1565135446Strhodes * STATUS stage in case of short 1566135446Strhodes * control transfers in USB host mode. 1567135446Strhodes * The STATUS stage then becomes the 1568135446Strhodes * "alt_next" to the DATA stage. 1569135446Strhodes */ 1570135446Strhodes xfer->flags_int.short_frames_ok = 1; 1571135446Strhodes } 1572135446Strhodes } 1573135446Strhodes } 1574135446Strhodes /* 1575135446Strhodes * Check if BUS-DMA support is enabled and try to load virtual 1576135446Strhodes * buffers into DMA, if any: 1577135446Strhodes */ 1578135446Strhodes#if USB_HAVE_BUSDMA 1579135446Strhodes if (xfer->flags_int.bdma_enable) { 1580135446Strhodes /* insert the USB transfer last in the BUS-DMA queue */ 1581135446Strhodes usb_command_wrapper(&xfer->xroot->dma_q, xfer); 1582135446Strhodes return; 1583135446Strhodes } 1584135446Strhodes#endif 1585135446Strhodes /* 1586135446Strhodes * Enter the USB transfer into the Host Controller or 1587135446Strhodes * Device Controller schedule: 1588135446Strhodes */ 1589135446Strhodes usbd_pipe_enter(xfer); 1590135446Strhodes} 1591135446Strhodes 1592135446Strhodes/*------------------------------------------------------------------------* 1593135446Strhodes * usbd_pipe_enter - factored out code 1594135446Strhodes *------------------------------------------------------------------------*/ 1595135446Strhodesvoid 1596135446Strhodesusbd_pipe_enter(struct usb_xfer *xfer) 1597135446Strhodes{ 1598135446Strhodes struct usb_endpoint *ep; 1599135446Strhodes 1600135446Strhodes USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1601135446Strhodes 1602135446Strhodes USB_BUS_LOCK(xfer->xroot->bus); 1603135446Strhodes 1604135446Strhodes ep = xfer->endpoint; 1605135446Strhodes 1606135446Strhodes DPRINTF("enter\n"); 1607135446Strhodes 1608135446Strhodes /* enter the transfer */ 1609135446Strhodes (ep->methods->enter) (xfer); 1610135446Strhodes 1611135446Strhodes xfer->flags_int.can_cancel_immed = 1; 1612135446Strhodes 1613135446Strhodes /* check for transfer error */ 1614135446Strhodes if (xfer->error) { 1615135446Strhodes /* some error has happened */ 1616135446Strhodes usbd_transfer_done(xfer, 0); 1617135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1618135446Strhodes return; 1619135446Strhodes } 1620135446Strhodes 1621135446Strhodes /* start the transfer */ 1622135446Strhodes usb_command_wrapper(&ep->endpoint_q, xfer); 1623135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1624135446Strhodes} 1625135446Strhodes 1626135446Strhodes/*------------------------------------------------------------------------* 1627135446Strhodes * usbd_transfer_start - start an USB transfer 1628135446Strhodes * 1629135446Strhodes * NOTE: Calling this function more than one time will only 1630135446Strhodes * result in a single transfer start, until the USB transfer 1631135446Strhodes * completes. 1632135446Strhodes *------------------------------------------------------------------------*/ 1633135446Strhodesvoid 1634135446Strhodesusbd_transfer_start(struct usb_xfer *xfer) 1635135446Strhodes{ 1636135446Strhodes if (xfer == NULL) { 1637135446Strhodes /* transfer is gone */ 1638135446Strhodes return; 1639135446Strhodes } 1640135446Strhodes USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1641135446Strhodes 1642135446Strhodes /* mark the USB transfer started */ 1643135446Strhodes 1644135446Strhodes if (!xfer->flags_int.started) { 1645135446Strhodes /* lock the BUS lock to avoid races updating flags_int */ 1646135446Strhodes USB_BUS_LOCK(xfer->xroot->bus); 1647135446Strhodes xfer->flags_int.started = 1; 1648135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1649135446Strhodes } 1650135446Strhodes /* check if the USB transfer callback is already transferring */ 1651135446Strhodes 1652135446Strhodes if (xfer->flags_int.transferring) { 1653135446Strhodes return; 1654135446Strhodes } 1655135446Strhodes USB_BUS_LOCK(xfer->xroot->bus); 1656135446Strhodes /* call the USB transfer callback */ 1657135446Strhodes usbd_callback_ss_done_defer(xfer); 1658135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1659135446Strhodes} 1660135446Strhodes 1661135446Strhodes/*------------------------------------------------------------------------* 1662135446Strhodes * usbd_transfer_stop - stop an USB transfer 1663135446Strhodes * 1664135446Strhodes * NOTE: Calling this function more than one time will only 1665135446Strhodes * result in a single transfer stop. 1666135446Strhodes * NOTE: When this function returns it is not safe to free nor 1667135446Strhodes * reuse any DMA buffers. See "usbd_transfer_drain()". 1668135446Strhodes *------------------------------------------------------------------------*/ 1669135446Strhodesvoid 1670135446Strhodesusbd_transfer_stop(struct usb_xfer *xfer) 1671135446Strhodes{ 1672135446Strhodes struct usb_endpoint *ep; 1673135446Strhodes 1674135446Strhodes if (xfer == NULL) { 1675135446Strhodes /* transfer is gone */ 1676135446Strhodes return; 1677135446Strhodes } 1678135446Strhodes USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1679135446Strhodes 1680135446Strhodes /* check if the USB transfer was ever opened */ 1681135446Strhodes 1682135446Strhodes if (!xfer->flags_int.open) { 1683135446Strhodes if (xfer->flags_int.started) { 1684135446Strhodes /* nothing to do except clearing the "started" flag */ 1685135446Strhodes /* lock the BUS lock to avoid races updating flags_int */ 1686135446Strhodes USB_BUS_LOCK(xfer->xroot->bus); 1687135446Strhodes xfer->flags_int.started = 0; 1688135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1689135446Strhodes } 1690135446Strhodes return; 1691135446Strhodes } 1692135446Strhodes /* try to stop the current USB transfer */ 1693135446Strhodes 1694135446Strhodes USB_BUS_LOCK(xfer->xroot->bus); 1695135446Strhodes /* override any previous error */ 1696135446Strhodes xfer->error = USB_ERR_CANCELLED; 1697135446Strhodes 1698135446Strhodes /* 1699135446Strhodes * Clear "open" and "started" when both private and USB lock 1700135446Strhodes * is locked so that we don't get a race updating "flags_int" 1701135446Strhodes */ 1702135446Strhodes xfer->flags_int.open = 0; 1703135446Strhodes xfer->flags_int.started = 0; 1704135446Strhodes 1705135446Strhodes /* 1706135446Strhodes * Check if we can cancel the USB transfer immediately. 1707135446Strhodes */ 1708135446Strhodes if (xfer->flags_int.transferring) { 1709135446Strhodes if (xfer->flags_int.can_cancel_immed && 1710135446Strhodes (!xfer->flags_int.did_close)) { 1711135446Strhodes DPRINTF("close\n"); 1712135446Strhodes /* 1713135446Strhodes * The following will lead to an USB_ERR_CANCELLED 1714135446Strhodes * error code being passed to the USB callback. 1715135446Strhodes */ 1716135446Strhodes (xfer->endpoint->methods->close) (xfer); 1717135446Strhodes /* only close once */ 1718135446Strhodes xfer->flags_int.did_close = 1; 1719135446Strhodes } else { 1720135446Strhodes /* need to wait for the next done callback */ 1721135446Strhodes } 1722135446Strhodes } else { 1723135446Strhodes DPRINTF("close\n"); 1724135446Strhodes 1725135446Strhodes /* close here and now */ 1726135446Strhodes (xfer->endpoint->methods->close) (xfer); 1727135446Strhodes 1728135446Strhodes /* 1729135446Strhodes * Any additional DMA delay is done by 1730135446Strhodes * "usbd_transfer_unsetup()". 1731135446Strhodes */ 1732135446Strhodes 1733135446Strhodes /* 1734135446Strhodes * Special case. Check if we need to restart a blocked 1735135446Strhodes * endpoint. 1736135446Strhodes */ 1737135446Strhodes ep = xfer->endpoint; 1738135446Strhodes 1739135446Strhodes /* 1740135446Strhodes * If the current USB transfer is completing we need 1741135446Strhodes * to start the next one: 1742135446Strhodes */ 1743135446Strhodes if (ep->endpoint_q.curr == xfer) { 1744135446Strhodes usb_command_wrapper(&ep->endpoint_q, NULL); 1745135446Strhodes } 1746135446Strhodes } 1747135446Strhodes 1748135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1749135446Strhodes} 1750135446Strhodes 1751135446Strhodes/*------------------------------------------------------------------------* 1752135446Strhodes * usbd_transfer_pending 1753135446Strhodes * 1754135446Strhodes * This function will check if an USB transfer is pending which is a 1755135446Strhodes * little bit complicated! 1756135446Strhodes * Return values: 1757135446Strhodes * 0: Not pending 1758135446Strhodes * 1: Pending: The USB transfer will receive a callback in the future. 1759135446Strhodes *------------------------------------------------------------------------*/ 1760135446Strhodesuint8_t 1761135446Strhodesusbd_transfer_pending(struct usb_xfer *xfer) 1762135446Strhodes{ 1763135446Strhodes struct usb_xfer_root *info; 1764135446Strhodes struct usb_xfer_queue *pq; 1765135446Strhodes 1766135446Strhodes if (xfer == NULL) { 1767135446Strhodes /* transfer is gone */ 1768135446Strhodes return (0); 1769135446Strhodes } 1770135446Strhodes USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 1771135446Strhodes 1772135446Strhodes if (xfer->flags_int.transferring) { 1773135446Strhodes /* trivial case */ 1774135446Strhodes return (1); 1775135446Strhodes } 1776135446Strhodes USB_BUS_LOCK(xfer->xroot->bus); 1777135446Strhodes if (xfer->wait_queue) { 1778135446Strhodes /* we are waiting on a queue somewhere */ 1779135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1780135446Strhodes return (1); 1781135446Strhodes } 1782135446Strhodes info = xfer->xroot; 1783135446Strhodes pq = &info->done_q; 1784135446Strhodes 1785135446Strhodes if (pq->curr == xfer) { 1786135446Strhodes /* we are currently scheduled for callback */ 1787135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1788135446Strhodes return (1); 1789135446Strhodes } 1790135446Strhodes /* we are not pending */ 1791135446Strhodes USB_BUS_UNLOCK(xfer->xroot->bus); 1792135446Strhodes return (0); 1793135446Strhodes} 1794135446Strhodes 1795135446Strhodes/*------------------------------------------------------------------------* 1796135446Strhodes * usbd_transfer_drain 1797135446Strhodes * 1798135446Strhodes * This function will stop the USB transfer and wait for any 1799135446Strhodes * additional BUS-DMA and HW-DMA operations to complete. Buffers that 1800135446Strhodes * are loaded into DMA can safely be freed or reused after that this 1801135446Strhodes * function has returned. 1802135446Strhodes *------------------------------------------------------------------------*/ 1803135446Strhodesvoid 1804135446Strhodesusbd_transfer_drain(struct usb_xfer *xfer) 1805135446Strhodes{ 1806135446Strhodes WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1807135446Strhodes "usbd_transfer_drain can sleep!"); 1808135446Strhodes 1809135446Strhodes if (xfer == NULL) { 1810135446Strhodes /* transfer is gone */ 1811135446Strhodes return; 1812135446Strhodes } 1813135446Strhodes if (xfer->xroot->xfer_mtx != &Giant) { 1814135446Strhodes USB_XFER_LOCK_ASSERT(xfer, MA_NOTOWNED); 1815135446Strhodes } 1816135446Strhodes USB_XFER_LOCK(xfer); 1817135446Strhodes 1818135446Strhodes usbd_transfer_stop(xfer); 1819135446Strhodes 1820135446Strhodes while (usbd_transfer_pending(xfer) || 1821135446Strhodes xfer->flags_int.doing_callback) { 1822135446Strhodes 1823135446Strhodes /* 1824135446Strhodes * It is allowed that the callback can drop its 1825135446Strhodes * transfer mutex. In that case checking only 1826135446Strhodes * "usbd_transfer_pending()" is not enough to tell if 1827135446Strhodes * the USB transfer is fully drained. We also need to 1828135446Strhodes * check the internal "doing_callback" flag. 1829135446Strhodes */ 1830135446Strhodes xfer->flags_int.draining = 1; 1831135446Strhodes 1832135446Strhodes /* 1833135446Strhodes * Wait until the current outstanding USB 1834135446Strhodes * transfer is complete ! 1835135446Strhodes */ 1836135446Strhodes cv_wait(&xfer->xroot->cv_drain, xfer->xroot->xfer_mtx); 1837135446Strhodes } 1838135446Strhodes USB_XFER_UNLOCK(xfer); 1839135446Strhodes} 1840135446Strhodes 1841135446Strhodesstruct usb_page_cache * 1842135446Strhodesusbd_xfer_get_frame(struct usb_xfer *xfer, usb_frcount_t frindex) 1843135446Strhodes{ 1844135446Strhodes KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1845135446Strhodes 1846135446Strhodes return (&xfer->frbuffers[frindex]); 1847135446Strhodes} 1848135446Strhodes 1849135446Strhodes/*------------------------------------------------------------------------* 1850135446Strhodes * usbd_xfer_get_fps_shift 1851135446Strhodes * 1852135446Strhodes * The following function is only useful for isochronous transfers. It 1853135446Strhodes * returns how many times the frame execution rate has been shifted 1854135446Strhodes * down. 1855135446Strhodes * 1856135446Strhodes * Return value: 1857135446Strhodes * Success: 0..3 1858135446Strhodes * Failure: 0 1859135446Strhodes *------------------------------------------------------------------------*/ 1860135446Strhodesuint8_t 1861135446Strhodesusbd_xfer_get_fps_shift(struct usb_xfer *xfer) 1862135446Strhodes{ 1863135446Strhodes return (xfer->fps_shift); 1864135446Strhodes} 1865135446Strhodes 1866135446Strhodesusb_frlength_t 1867135446Strhodesusbd_xfer_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex) 1868135446Strhodes{ 1869135446Strhodes KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1870135446Strhodes 1871135446Strhodes return (xfer->frlengths[frindex]); 1872135446Strhodes} 1873135446Strhodes 1874135446Strhodes/*------------------------------------------------------------------------* 1875135446Strhodes * usbd_xfer_set_frame_data 1876135446Strhodes * 1877135446Strhodes * This function sets the pointer of the buffer that should 1878135446Strhodes * loaded directly into DMA for the given USB frame. Passing "ptr" 1879135446Strhodes * equal to NULL while the corresponding "frlength" is greater 1880135446Strhodes * than zero gives undefined results! 1881135446Strhodes *------------------------------------------------------------------------*/ 1882135446Strhodesvoid 1883135446Strhodesusbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 1884135446Strhodes void *ptr, usb_frlength_t len) 1885135446Strhodes{ 1886135446Strhodes KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1887135446Strhodes 1888135446Strhodes /* set virtual address to load and length */ 1889135446Strhodes xfer->frbuffers[frindex].buffer = ptr; 1890135446Strhodes usbd_xfer_set_frame_len(xfer, frindex, len); 1891135446Strhodes} 1892135446Strhodes 1893135446Strhodesvoid 1894135446Strhodesusbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex, 1895135446Strhodes void **ptr, int *len) 1896135446Strhodes{ 1897135446Strhodes KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1898135446Strhodes 1899135446Strhodes if (ptr != NULL) 1900135446Strhodes *ptr = xfer->frbuffers[frindex].buffer; 1901135446Strhodes if (len != NULL) 1902135446Strhodes *len = xfer->frlengths[frindex]; 1903135446Strhodes} 1904135446Strhodes 1905135446Strhodesvoid 1906135446Strhodesusbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen, int *aframes, 1907135446Strhodes int *nframes) 1908135446Strhodes{ 1909135446Strhodes if (actlen != NULL) 1910135446Strhodes *actlen = xfer->actlen; 1911135446Strhodes if (sumlen != NULL) 1912135446Strhodes *sumlen = xfer->sumlen; 1913135446Strhodes if (aframes != NULL) 1914135446Strhodes *aframes = xfer->aframes; 1915135446Strhodes if (nframes != NULL) 1916135446Strhodes *nframes = xfer->nframes; 1917135446Strhodes} 1918135446Strhodes 1919135446Strhodes/*------------------------------------------------------------------------* 1920135446Strhodes * usbd_xfer_set_frame_offset 1921135446Strhodes * 1922135446Strhodes * This function sets the frame data buffer offset relative to the beginning 1923135446Strhodes * of the USB DMA buffer allocated for this USB transfer. 1924135446Strhodes *------------------------------------------------------------------------*/ 1925135446Strhodesvoid 1926135446Strhodesusbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset, 1927135446Strhodes usb_frcount_t frindex) 1928135446Strhodes{ 1929135446Strhodes KASSERT(!xfer->flags.ext_buffer, ("Cannot offset data frame " 1930135446Strhodes "when the USB buffer is external\n")); 1931135446Strhodes KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1932135446Strhodes 1933135446Strhodes /* set virtual address to load */ 1934135446Strhodes xfer->frbuffers[frindex].buffer = 1935135446Strhodes USB_ADD_BYTES(xfer->local_buffer, offset); 1936135446Strhodes} 1937135446Strhodes 1938135446Strhodesvoid 1939135446Strhodesusbd_xfer_set_interval(struct usb_xfer *xfer, int i) 1940135446Strhodes{ 1941135446Strhodes xfer->interval = i; 1942135446Strhodes} 1943135446Strhodes 1944135446Strhodesvoid 1945135446Strhodesusbd_xfer_set_timeout(struct usb_xfer *xfer, int t) 1946135446Strhodes{ 1947135446Strhodes xfer->timeout = t; 1948135446Strhodes} 1949135446Strhodes 1950135446Strhodesvoid 1951135446Strhodesusbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n) 1952135446Strhodes{ 1953135446Strhodes xfer->nframes = n; 1954135446Strhodes} 1955135446Strhodes 1956135446Strhodesusb_frcount_t 1957135446Strhodesusbd_xfer_max_frames(struct usb_xfer *xfer) 1958135446Strhodes{ 1959135446Strhodes return (xfer->max_frame_count); 1960135446Strhodes} 1961135446Strhodes 1962135446Strhodesusb_frlength_t 1963135446Strhodesusbd_xfer_max_len(struct usb_xfer *xfer) 1964135446Strhodes{ 1965135446Strhodes return (xfer->max_data_length); 1966135446Strhodes} 1967135446Strhodes 1968135446Strhodesusb_frlength_t 1969135446Strhodesusbd_xfer_max_framelen(struct usb_xfer *xfer) 1970135446Strhodes{ 1971135446Strhodes return (xfer->max_frame_size); 1972135446Strhodes} 1973135446Strhodes 1974135446Strhodesvoid 1975135446Strhodesusbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex, 1976135446Strhodes usb_frlength_t len) 1977135446Strhodes{ 1978135446Strhodes KASSERT(frindex < xfer->max_frame_count, ("frame index overflow")); 1979135446Strhodes 1980135446Strhodes xfer->frlengths[frindex] = len; 1981135446Strhodes} 1982135446Strhodes 1983135446Strhodes/*------------------------------------------------------------------------* 1984135446Strhodes * usb_callback_proc - factored out code 1985135446Strhodes * 1986135446Strhodes * This function performs USB callbacks. 1987135446Strhodes *------------------------------------------------------------------------*/ 1988135446Strhodesstatic void 1989135446Strhodesusb_callback_proc(struct usb_proc_msg *_pm) 1990135446Strhodes{ 1991135446Strhodes struct usb_done_msg *pm = (void *)_pm; 1992135446Strhodes struct usb_xfer_root *info = pm->xroot; 1993135446Strhodes 1994135446Strhodes /* Change locking order */ 1995135446Strhodes USB_BUS_UNLOCK(info->bus); 1996135446Strhodes 1997135446Strhodes /* 1998135446Strhodes * We exploit the fact that the mutex is the same for all 1999135446Strhodes * callbacks that will be called from this thread: 2000135446Strhodes */ 2001135446Strhodes mtx_lock(info->xfer_mtx); 2002135446Strhodes USB_BUS_LOCK(info->bus); 2003135446Strhodes 2004135446Strhodes /* Continue where we lost track */ 2005135446Strhodes usb_command_wrapper(&info->done_q, 2006135446Strhodes info->done_q.curr); 2007135446Strhodes 2008135446Strhodes mtx_unlock(info->xfer_mtx); 2009135446Strhodes} 2010135446Strhodes 2011135446Strhodes/*------------------------------------------------------------------------* 2012135446Strhodes * usbd_callback_ss_done_defer 2013135446Strhodes * 2014135446Strhodes * This function will defer the start, stop and done callback to the 2015135446Strhodes * correct thread. 2016135446Strhodes *------------------------------------------------------------------------*/ 2017135446Strhodesstatic void 2018135446Strhodesusbd_callback_ss_done_defer(struct usb_xfer *xfer) 2019135446Strhodes{ 2020135446Strhodes struct usb_xfer_root *info = xfer->xroot; 2021135446Strhodes struct usb_xfer_queue *pq = &info->done_q; 2022135446Strhodes 2023135446Strhodes USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2024135446Strhodes 2025135446Strhodes if (pq->curr != xfer) { 2026135446Strhodes usbd_transfer_enqueue(pq, xfer); 2027135446Strhodes } 2028135446Strhodes if (!pq->recurse_1) { 2029135446Strhodes 2030135446Strhodes /* 2031135446Strhodes * We have to postpone the callback due to the fact we 2032135446Strhodes * will have a Lock Order Reversal, LOR, if we try to 2033135446Strhodes * proceed ! 2034135446Strhodes */ 2035135446Strhodes if (usb_proc_msignal(info->done_p, 2036135446Strhodes &info->done_m[0], &info->done_m[1])) { 2037135446Strhodes /* ignore */ 2038135446Strhodes } 2039135446Strhodes } else { 2040135446Strhodes /* clear second recurse flag */ 2041135446Strhodes pq->recurse_2 = 0; 2042135446Strhodes } 2043135446Strhodes return; 2044135446Strhodes 2045135446Strhodes} 2046135446Strhodes 2047135446Strhodes/*------------------------------------------------------------------------* 2048135446Strhodes * usbd_callback_wrapper 2049135446Strhodes * 2050135446Strhodes * This is a wrapper for USB callbacks. This wrapper does some 2051135446Strhodes * auto-magic things like figuring out if we can call the callback 2052135446Strhodes * directly from the current context or if we need to wakeup the 2053135446Strhodes * interrupt process. 2054135446Strhodes *------------------------------------------------------------------------*/ 2055135446Strhodesstatic void 2056135446Strhodesusbd_callback_wrapper(struct usb_xfer_queue *pq) 2057135446Strhodes{ 2058135446Strhodes struct usb_xfer *xfer = pq->curr; 2059135446Strhodes struct usb_xfer_root *info = xfer->xroot; 2060135446Strhodes 2061135446Strhodes USB_BUS_LOCK_ASSERT(info->bus, MA_OWNED); 2062135446Strhodes if (!mtx_owned(info->xfer_mtx)) { 2063135446Strhodes /* 2064135446Strhodes * Cases that end up here: 2065135446Strhodes * 2066135446Strhodes * 5) HW interrupt done callback or other source. 2067135446Strhodes */ 2068135446Strhodes DPRINTFN(3, "case 5\n"); 2069135446Strhodes 2070135446Strhodes /* 2071135446Strhodes * We have to postpone the callback due to the fact we 2072135446Strhodes * will have a Lock Order Reversal, LOR, if we try to 2073135446Strhodes * proceed ! 2074135446Strhodes */ 2075135446Strhodes if (usb_proc_msignal(info->done_p, 2076135446Strhodes &info->done_m[0], &info->done_m[1])) { 2077135446Strhodes /* ignore */ 2078135446Strhodes } 2079135446Strhodes return; 2080135446Strhodes } 2081135446Strhodes /* 2082135446Strhodes * Cases that end up here: 2083135446Strhodes * 2084135446Strhodes * 1) We are starting a transfer 2085135446Strhodes * 2) We are prematurely calling back a transfer 2086135446Strhodes * 3) We are stopping a transfer 2087135446Strhodes * 4) We are doing an ordinary callback 2088135446Strhodes */ 2089135446Strhodes DPRINTFN(3, "case 1-4\n"); 2090135446Strhodes /* get next USB transfer in the queue */ 2091135446Strhodes info->done_q.curr = NULL; 2092135446Strhodes 2093135446Strhodes /* set flag in case of drain */ 2094135446Strhodes xfer->flags_int.doing_callback = 1; 2095135446Strhodes 2096135446Strhodes USB_BUS_UNLOCK(info->bus); 2097135446Strhodes USB_BUS_LOCK_ASSERT(info->bus, MA_NOTOWNED); 2098135446Strhodes 2099135446Strhodes /* set correct USB state for callback */ 2100135446Strhodes if (!xfer->flags_int.transferring) { 2101135446Strhodes xfer->usb_state = USB_ST_SETUP; 2102135446Strhodes if (!xfer->flags_int.started) { 2103135446Strhodes /* we got stopped before we even got started */ 2104135446Strhodes USB_BUS_LOCK(info->bus); 2105135446Strhodes goto done; 2106135446Strhodes } 2107135446Strhodes } else { 2108135446Strhodes 2109135446Strhodes if (usbd_callback_wrapper_sub(xfer)) { 2110135446Strhodes /* the callback has been deferred */ 2111135446Strhodes USB_BUS_LOCK(info->bus); 2112135446Strhodes goto done; 2113135446Strhodes } 2114135446Strhodes#if USB_HAVE_POWERD 2115135446Strhodes /* decrement power reference */ 2116135446Strhodes usbd_transfer_power_ref(xfer, -1); 2117135446Strhodes#endif 2118135446Strhodes xfer->flags_int.transferring = 0; 2119135446Strhodes 2120135446Strhodes if (xfer->error) { 2121135446Strhodes xfer->usb_state = USB_ST_ERROR; 2122135446Strhodes } else { 2123135446Strhodes /* set transferred state */ 2124135446Strhodes xfer->usb_state = USB_ST_TRANSFERRED; 2125135446Strhodes#if USB_HAVE_BUSDMA 2126135446Strhodes /* sync DMA memory, if any */ 2127135446Strhodes if (xfer->flags_int.bdma_enable && 2128135446Strhodes (!xfer->flags_int.bdma_no_post_sync)) { 2129135446Strhodes usb_bdma_post_sync(xfer); 2130135446Strhodes } 2131135446Strhodes#endif 2132135446Strhodes } 2133135446Strhodes } 2134135446Strhodes 2135135446Strhodes /* call processing routine */ 2136135446Strhodes (xfer->callback) (xfer, xfer->error); 2137135446Strhodes 2138135446Strhodes /* pickup the USB mutex again */ 2139135446Strhodes USB_BUS_LOCK(info->bus); 2140135446Strhodes 2141135446Strhodes /* 2142135446Strhodes * Check if we got started after that we got cancelled, but 2143135446Strhodes * before we managed to do the callback. 2144135446Strhodes */ 2145135446Strhodes if ((!xfer->flags_int.open) && 2146135446Strhodes (xfer->flags_int.started) && 2147135446Strhodes (xfer->usb_state == USB_ST_ERROR)) { 2148135446Strhodes /* clear flag in case of drain */ 2149135446Strhodes xfer->flags_int.doing_callback = 0; 2150135446Strhodes /* try to loop, but not recursivly */ 2151135446Strhodes usb_command_wrapper(&info->done_q, xfer); 2152135446Strhodes return; 2153135446Strhodes } 2154135446Strhodes 2155135446Strhodesdone: 2156135446Strhodes /* clear flag in case of drain */ 2157135446Strhodes xfer->flags_int.doing_callback = 0; 2158135446Strhodes 2159135446Strhodes /* 2160135446Strhodes * Check if we are draining. 2161135446Strhodes */ 2162135446Strhodes if (xfer->flags_int.draining && 2163135446Strhodes (!xfer->flags_int.transferring)) { 2164135446Strhodes /* "usbd_transfer_drain()" is waiting for end of transfer */ 2165135446Strhodes xfer->flags_int.draining = 0; 2166135446Strhodes cv_broadcast(&info->cv_drain); 2167135446Strhodes } 2168135446Strhodes 2169135446Strhodes /* do the next callback, if any */ 2170135446Strhodes usb_command_wrapper(&info->done_q, 2171135446Strhodes info->done_q.curr); 2172135446Strhodes} 2173135446Strhodes 2174135446Strhodes/*------------------------------------------------------------------------* 2175135446Strhodes * usb_dma_delay_done_cb 2176135446Strhodes * 2177135446Strhodes * This function is called when the DMA delay has been exectuded, and 2178135446Strhodes * will make sure that the callback is called to complete the USB 2179135446Strhodes * transfer. This code path is ususally only used when there is an USB 2180135446Strhodes * error like USB_ERR_CANCELLED. 2181135446Strhodes *------------------------------------------------------------------------*/ 2182135446Strhodesstatic void 2183135446Strhodesusb_dma_delay_done_cb(void *arg) 2184135446Strhodes{ 2185135446Strhodes struct usb_xfer *xfer = arg; 2186135446Strhodes 2187135446Strhodes USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2188135446Strhodes 2189135446Strhodes DPRINTFN(3, "Completed %p\n", xfer); 2190135446Strhodes 2191135446Strhodes /* queue callback for execution, again */ 2192135446Strhodes usbd_transfer_done(xfer, 0); 2193135446Strhodes} 2194135446Strhodes 2195135446Strhodes/*------------------------------------------------------------------------* 2196135446Strhodes * usbd_transfer_dequeue 2197135446Strhodes * 2198135446Strhodes * - This function is used to remove an USB transfer from a USB 2199135446Strhodes * transfer queue. 2200135446Strhodes * 2201135446Strhodes * - This function can be called multiple times in a row. 2202135446Strhodes *------------------------------------------------------------------------*/ 2203135446Strhodesvoid 2204135446Strhodesusbd_transfer_dequeue(struct usb_xfer *xfer) 2205135446Strhodes{ 2206135446Strhodes struct usb_xfer_queue *pq; 2207135446Strhodes 2208135446Strhodes pq = xfer->wait_queue; 2209135446Strhodes if (pq) { 2210135446Strhodes TAILQ_REMOVE(&pq->head, xfer, wait_entry); 2211135446Strhodes xfer->wait_queue = NULL; 2212135446Strhodes } 2213135446Strhodes} 2214135446Strhodes 2215135446Strhodes/*------------------------------------------------------------------------* 2216135446Strhodes * usbd_transfer_enqueue 2217135446Strhodes * 2218135446Strhodes * - This function is used to insert an USB transfer into a USB * 2219135446Strhodes * transfer queue. 2220135446Strhodes * 2221135446Strhodes * - This function can be called multiple times in a row. 2222135446Strhodes *------------------------------------------------------------------------*/ 2223135446Strhodesvoid 2224135446Strhodesusbd_transfer_enqueue(struct usb_xfer_queue *pq, struct usb_xfer *xfer) 2225135446Strhodes{ 2226135446Strhodes /* 2227135446Strhodes * Insert the USB transfer into the queue, if it is not 2228135446Strhodes * already on a USB transfer queue: 2229135446Strhodes */ 2230135446Strhodes if (xfer->wait_queue == NULL) { 2231135446Strhodes xfer->wait_queue = pq; 2232135446Strhodes TAILQ_INSERT_TAIL(&pq->head, xfer, wait_entry); 2233135446Strhodes } 2234135446Strhodes} 2235135446Strhodes 2236135446Strhodes/*------------------------------------------------------------------------* 2237135446Strhodes * usbd_transfer_done 2238135446Strhodes * 2239135446Strhodes * - This function is used to remove an USB transfer from the busdma, 2240135446Strhodes * pipe or interrupt queue. 2241135446Strhodes * 2242135446Strhodes * - This function is used to queue the USB transfer on the done 2243135446Strhodes * queue. 2244135446Strhodes * 2245135446Strhodes * - This function is used to stop any USB transfer timeouts. 2246135446Strhodes *------------------------------------------------------------------------*/ 2247135446Strhodesvoid 2248135446Strhodesusbd_transfer_done(struct usb_xfer *xfer, usb_error_t error) 2249135446Strhodes{ 2250135446Strhodes USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2251135446Strhodes 2252135446Strhodes DPRINTF("err=%s\n", usbd_errstr(error)); 2253135446Strhodes 2254135446Strhodes /* 2255135446Strhodes * If we are not transferring then just return. 2256135446Strhodes * This can happen during transfer cancel. 2257135446Strhodes */ 2258135446Strhodes if (!xfer->flags_int.transferring) { 2259135446Strhodes DPRINTF("not transferring\n"); 2260135446Strhodes /* end of control transfer, if any */ 2261135446Strhodes xfer->flags_int.control_act = 0; 2262135446Strhodes return; 2263135446Strhodes } 2264135446Strhodes /* only set transfer error if not already set */ 2265135446Strhodes if (!xfer->error) { 2266135446Strhodes xfer->error = error; 2267135446Strhodes } 2268135446Strhodes /* stop any callouts */ 2269135446Strhodes usb_callout_stop(&xfer->timeout_handle); 2270135446Strhodes 2271135446Strhodes /* 2272135446Strhodes * If we are waiting on a queue, just remove the USB transfer 2273135446Strhodes * from the queue, if any. We should have the required locks 2274135446Strhodes * locked to do the remove when this function is called. 2275135446Strhodes */ 2276135446Strhodes usbd_transfer_dequeue(xfer); 2277135446Strhodes 2278135446Strhodes#if USB_HAVE_BUSDMA 2279135446Strhodes if (mtx_owned(xfer->xroot->xfer_mtx)) { 2280135446Strhodes struct usb_xfer_queue *pq; 2281135446Strhodes 2282135446Strhodes /* 2283135446Strhodes * If the private USB lock is not locked, then we assume 2284135446Strhodes * that the BUS-DMA load stage has been passed: 2285135446Strhodes */ 2286135446Strhodes pq = &xfer->xroot->dma_q; 2287135446Strhodes 2288135446Strhodes if (pq->curr == xfer) { 2289135446Strhodes /* start the next BUS-DMA load, if any */ 2290 usb_command_wrapper(pq, NULL); 2291 } 2292 } 2293#endif 2294 /* keep some statistics */ 2295 if (xfer->error) { 2296 xfer->xroot->bus->stats_err.uds_requests 2297 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; 2298 } else { 2299 xfer->xroot->bus->stats_ok.uds_requests 2300 [xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE]++; 2301 } 2302 2303 /* call the USB transfer callback */ 2304 usbd_callback_ss_done_defer(xfer); 2305} 2306 2307/*------------------------------------------------------------------------* 2308 * usbd_transfer_start_cb 2309 * 2310 * This function is called to start the USB transfer when 2311 * "xfer->interval" is greater than zero, and and the endpoint type is 2312 * BULK or CONTROL. 2313 *------------------------------------------------------------------------*/ 2314static void 2315usbd_transfer_start_cb(void *arg) 2316{ 2317 struct usb_xfer *xfer = arg; 2318 struct usb_endpoint *ep = xfer->endpoint; 2319 2320 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2321 2322 DPRINTF("start\n"); 2323 2324 /* start the transfer */ 2325 (ep->methods->start) (xfer); 2326 2327 xfer->flags_int.can_cancel_immed = 1; 2328 2329 /* check for error */ 2330 if (xfer->error) { 2331 /* some error has happened */ 2332 usbd_transfer_done(xfer, 0); 2333 } 2334} 2335 2336/*------------------------------------------------------------------------* 2337 * usbd_xfer_set_stall 2338 * 2339 * This function is used to set the stall flag outside the 2340 * callback. This function is NULL safe. 2341 *------------------------------------------------------------------------*/ 2342void 2343usbd_xfer_set_stall(struct usb_xfer *xfer) 2344{ 2345 if (xfer == NULL) { 2346 /* tearing down */ 2347 return; 2348 } 2349 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2350 2351 /* avoid any races by locking the USB mutex */ 2352 USB_BUS_LOCK(xfer->xroot->bus); 2353 xfer->flags.stall_pipe = 1; 2354 USB_BUS_UNLOCK(xfer->xroot->bus); 2355} 2356 2357int 2358usbd_xfer_is_stalled(struct usb_xfer *xfer) 2359{ 2360 return (xfer->endpoint->is_stalled); 2361} 2362 2363/*------------------------------------------------------------------------* 2364 * usbd_transfer_clear_stall 2365 * 2366 * This function is used to clear the stall flag outside the 2367 * callback. This function is NULL safe. 2368 *------------------------------------------------------------------------*/ 2369void 2370usbd_transfer_clear_stall(struct usb_xfer *xfer) 2371{ 2372 if (xfer == NULL) { 2373 /* tearing down */ 2374 return; 2375 } 2376 USB_XFER_LOCK_ASSERT(xfer, MA_OWNED); 2377 2378 /* avoid any races by locking the USB mutex */ 2379 USB_BUS_LOCK(xfer->xroot->bus); 2380 2381 xfer->flags.stall_pipe = 0; 2382 2383 USB_BUS_UNLOCK(xfer->xroot->bus); 2384} 2385 2386/*------------------------------------------------------------------------* 2387 * usbd_pipe_start 2388 * 2389 * This function is used to add an USB transfer to the pipe transfer list. 2390 *------------------------------------------------------------------------*/ 2391void 2392usbd_pipe_start(struct usb_xfer_queue *pq) 2393{ 2394 struct usb_endpoint *ep; 2395 struct usb_xfer *xfer; 2396 uint8_t type; 2397 2398 xfer = pq->curr; 2399 ep = xfer->endpoint; 2400 2401 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2402 2403 /* 2404 * If the endpoint is already stalled we do nothing ! 2405 */ 2406 if (ep->is_stalled) { 2407 return; 2408 } 2409 /* 2410 * Check if we are supposed to stall the endpoint: 2411 */ 2412 if (xfer->flags.stall_pipe) { 2413 /* clear stall command */ 2414 xfer->flags.stall_pipe = 0; 2415 2416 /* 2417 * Only stall BULK and INTERRUPT endpoints. 2418 */ 2419 type = (ep->edesc->bmAttributes & UE_XFERTYPE); 2420 if ((type == UE_BULK) || 2421 (type == UE_INTERRUPT)) { 2422 struct usb_device *udev; 2423 struct usb_xfer_root *info; 2424 uint8_t did_stall; 2425 2426 info = xfer->xroot; 2427 udev = info->udev; 2428 did_stall = 1; 2429 2430 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2431 (udev->bus->methods->set_stall) ( 2432 udev, NULL, ep, &did_stall); 2433 } else if (udev->default_xfer[1]) { 2434 info = udev->default_xfer[1]->xroot; 2435 usb_proc_msignal( 2436 &info->bus->non_giant_callback_proc, 2437 &udev->cs_msg[0], &udev->cs_msg[1]); 2438 } else { 2439 /* should not happen */ 2440 DPRINTFN(0, "No stall handler\n"); 2441 } 2442 /* 2443 * Check if we should stall. Some USB hardware 2444 * handles set- and clear-stall in hardware. 2445 */ 2446 if (did_stall) { 2447 /* 2448 * The transfer will be continued when 2449 * the clear-stall control endpoint 2450 * message is received. 2451 */ 2452 ep->is_stalled = 1; 2453 return; 2454 } 2455 } 2456 } 2457 /* Set or clear stall complete - special case */ 2458 if (xfer->nframes == 0) { 2459 /* we are complete */ 2460 xfer->aframes = 0; 2461 usbd_transfer_done(xfer, 0); 2462 return; 2463 } 2464 /* 2465 * Handled cases: 2466 * 2467 * 1) Start the first transfer queued. 2468 * 2469 * 2) Re-start the current USB transfer. 2470 */ 2471 /* 2472 * Check if there should be any 2473 * pre transfer start delay: 2474 */ 2475 if (xfer->interval > 0) { 2476 type = (ep->edesc->bmAttributes & UE_XFERTYPE); 2477 if ((type == UE_BULK) || 2478 (type == UE_CONTROL)) { 2479 usbd_transfer_timeout_ms(xfer, 2480 &usbd_transfer_start_cb, 2481 xfer->interval); 2482 return; 2483 } 2484 } 2485 DPRINTF("start\n"); 2486 2487 /* start USB transfer */ 2488 (ep->methods->start) (xfer); 2489 2490 xfer->flags_int.can_cancel_immed = 1; 2491 2492 /* check for error */ 2493 if (xfer->error) { 2494 /* some error has happened */ 2495 usbd_transfer_done(xfer, 0); 2496 } 2497} 2498 2499/*------------------------------------------------------------------------* 2500 * usbd_transfer_timeout_ms 2501 * 2502 * This function is used to setup a timeout on the given USB 2503 * transfer. If the timeout has been deferred the callback given by 2504 * "cb" will get called after "ms" milliseconds. 2505 *------------------------------------------------------------------------*/ 2506void 2507usbd_transfer_timeout_ms(struct usb_xfer *xfer, 2508 void (*cb) (void *arg), usb_timeout_t ms) 2509{ 2510 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 2511 2512 /* defer delay */ 2513 usb_callout_reset(&xfer->timeout_handle, 2514 USB_MS_TO_TICKS(ms), cb, xfer); 2515} 2516 2517/*------------------------------------------------------------------------* 2518 * usbd_callback_wrapper_sub 2519 * 2520 * - This function will update variables in an USB transfer after 2521 * that the USB transfer is complete. 2522 * 2523 * - This function is used to start the next USB transfer on the 2524 * ep transfer queue, if any. 2525 * 2526 * NOTE: In some special cases the USB transfer will not be removed from 2527 * the pipe queue, but remain first. To enforce USB transfer removal call 2528 * this function passing the error code "USB_ERR_CANCELLED". 2529 * 2530 * Return values: 2531 * 0: Success. 2532 * Else: The callback has been deferred. 2533 *------------------------------------------------------------------------*/ 2534static uint8_t 2535usbd_callback_wrapper_sub(struct usb_xfer *xfer) 2536{ 2537 struct usb_endpoint *ep; 2538 usb_frcount_t x; 2539 2540 if ((!xfer->flags_int.open) && 2541 (!xfer->flags_int.did_close)) { 2542 DPRINTF("close\n"); 2543 USB_BUS_LOCK(xfer->xroot->bus); 2544 (xfer->endpoint->methods->close) (xfer); 2545 USB_BUS_UNLOCK(xfer->xroot->bus); 2546 /* only close once */ 2547 xfer->flags_int.did_close = 1; 2548 return (1); /* wait for new callback */ 2549 } 2550 /* 2551 * If we have a non-hardware induced error we 2552 * need to do the DMA delay! 2553 */ 2554 if (((xfer->error == USB_ERR_CANCELLED) || 2555 (xfer->error == USB_ERR_TIMEOUT)) && 2556 (!xfer->flags_int.did_dma_delay)) { 2557 2558 usb_timeout_t temp; 2559 2560 /* only delay once */ 2561 xfer->flags_int.did_dma_delay = 1; 2562 2563 /* we can not cancel this delay */ 2564 xfer->flags_int.can_cancel_immed = 0; 2565 2566 temp = usbd_get_dma_delay(xfer->xroot->bus); 2567 2568 DPRINTFN(3, "DMA delay, %u ms, " 2569 "on %p\n", temp, xfer); 2570 2571 if (temp != 0) { 2572 USB_BUS_LOCK(xfer->xroot->bus); 2573 usbd_transfer_timeout_ms(xfer, 2574 &usb_dma_delay_done_cb, temp); 2575 USB_BUS_UNLOCK(xfer->xroot->bus); 2576 return (1); /* wait for new callback */ 2577 } 2578 } 2579 /* check actual number of frames */ 2580 if (xfer->aframes > xfer->nframes) { 2581 if (xfer->error == 0) { 2582 panic("%s: actual number of frames, %d, is " 2583 "greater than initial number of frames, %d\n", 2584 __FUNCTION__, xfer->aframes, xfer->nframes); 2585 } else { 2586 /* just set some valid value */ 2587 xfer->aframes = xfer->nframes; 2588 } 2589 } 2590 /* compute actual length */ 2591 xfer->actlen = 0; 2592 2593 for (x = 0; x != xfer->aframes; x++) { 2594 xfer->actlen += xfer->frlengths[x]; 2595 } 2596 2597 /* 2598 * Frames that were not transferred get zero actual length in 2599 * case the USB device driver does not check the actual number 2600 * of frames transferred, "xfer->aframes": 2601 */ 2602 for (; x < xfer->nframes; x++) { 2603 usbd_xfer_set_frame_len(xfer, x, 0); 2604 } 2605 2606 /* check actual length */ 2607 if (xfer->actlen > xfer->sumlen) { 2608 if (xfer->error == 0) { 2609 panic("%s: actual length, %d, is greater than " 2610 "initial length, %d\n", 2611 __FUNCTION__, xfer->actlen, xfer->sumlen); 2612 } else { 2613 /* just set some valid value */ 2614 xfer->actlen = xfer->sumlen; 2615 } 2616 } 2617 DPRINTFN(1, "xfer=%p endpoint=%p sts=%d alen=%d, slen=%d, afrm=%d, nfrm=%d\n", 2618 xfer, xfer->endpoint, xfer->error, xfer->actlen, xfer->sumlen, 2619 xfer->aframes, xfer->nframes); 2620 2621 if (xfer->error) { 2622 /* end of control transfer, if any */ 2623 xfer->flags_int.control_act = 0; 2624 2625 /* check if we should block the execution queue */ 2626 if ((xfer->error != USB_ERR_CANCELLED) && 2627 (xfer->flags.pipe_bof)) { 2628 DPRINTFN(2, "xfer=%p: Block On Failure " 2629 "on endpoint=%p\n", xfer, xfer->endpoint); 2630 goto done; 2631 } 2632 } else { 2633 /* check for short transfers */ 2634 if (xfer->actlen < xfer->sumlen) { 2635 2636 /* end of control transfer, if any */ 2637 xfer->flags_int.control_act = 0; 2638 2639 if (!xfer->flags_int.short_xfer_ok) { 2640 xfer->error = USB_ERR_SHORT_XFER; 2641 if (xfer->flags.pipe_bof) { 2642 DPRINTFN(2, "xfer=%p: Block On Failure on " 2643 "Short Transfer on endpoint %p.\n", 2644 xfer, xfer->endpoint); 2645 goto done; 2646 } 2647 } 2648 } else { 2649 /* 2650 * Check if we are in the middle of a 2651 * control transfer: 2652 */ 2653 if (xfer->flags_int.control_act) { 2654 DPRINTFN(5, "xfer=%p: Control transfer " 2655 "active on endpoint=%p\n", xfer, xfer->endpoint); 2656 goto done; 2657 } 2658 } 2659 } 2660 2661 ep = xfer->endpoint; 2662 2663 /* 2664 * If the current USB transfer is completing we need to start the 2665 * next one: 2666 */ 2667 USB_BUS_LOCK(xfer->xroot->bus); 2668 if (ep->endpoint_q.curr == xfer) { 2669 usb_command_wrapper(&ep->endpoint_q, NULL); 2670 2671 if (ep->endpoint_q.curr || TAILQ_FIRST(&ep->endpoint_q.head)) { 2672 /* there is another USB transfer waiting */ 2673 } else { 2674 /* this is the last USB transfer */ 2675 /* clear isochronous sync flag */ 2676 xfer->endpoint->is_synced = 0; 2677 } 2678 } 2679 USB_BUS_UNLOCK(xfer->xroot->bus); 2680done: 2681 return (0); 2682} 2683 2684/*------------------------------------------------------------------------* 2685 * usb_command_wrapper 2686 * 2687 * This function is used to execute commands non-recursivly on an USB 2688 * transfer. 2689 *------------------------------------------------------------------------*/ 2690void 2691usb_command_wrapper(struct usb_xfer_queue *pq, struct usb_xfer *xfer) 2692{ 2693 if (xfer) { 2694 /* 2695 * If the transfer is not already processing, 2696 * queue it! 2697 */ 2698 if (pq->curr != xfer) { 2699 usbd_transfer_enqueue(pq, xfer); 2700 if (pq->curr != NULL) { 2701 /* something is already processing */ 2702 DPRINTFN(6, "busy %p\n", pq->curr); 2703 return; 2704 } 2705 } 2706 } else { 2707 /* Get next element in queue */ 2708 pq->curr = NULL; 2709 } 2710 2711 if (!pq->recurse_1) { 2712 2713 do { 2714 2715 /* set both recurse flags */ 2716 pq->recurse_1 = 1; 2717 pq->recurse_2 = 1; 2718 2719 if (pq->curr == NULL) { 2720 xfer = TAILQ_FIRST(&pq->head); 2721 if (xfer) { 2722 TAILQ_REMOVE(&pq->head, xfer, 2723 wait_entry); 2724 xfer->wait_queue = NULL; 2725 pq->curr = xfer; 2726 } else { 2727 break; 2728 } 2729 } 2730 DPRINTFN(6, "cb %p (enter)\n", pq->curr); 2731 (pq->command) (pq); 2732 DPRINTFN(6, "cb %p (leave)\n", pq->curr); 2733 2734 } while (!pq->recurse_2); 2735 2736 /* clear first recurse flag */ 2737 pq->recurse_1 = 0; 2738 2739 } else { 2740 /* clear second recurse flag */ 2741 pq->recurse_2 = 0; 2742 } 2743} 2744 2745/*------------------------------------------------------------------------* 2746 * usbd_default_transfer_setup 2747 * 2748 * This function is used to setup the default USB control endpoint 2749 * transfer. 2750 *------------------------------------------------------------------------*/ 2751void 2752usbd_default_transfer_setup(struct usb_device *udev) 2753{ 2754 struct usb_xfer *xfer; 2755 uint8_t no_resetup; 2756 uint8_t iface_index; 2757 2758 /* check for root HUB */ 2759 if (udev->parent_hub == NULL) 2760 return; 2761repeat: 2762 2763 xfer = udev->default_xfer[0]; 2764 if (xfer) { 2765 USB_XFER_LOCK(xfer); 2766 no_resetup = 2767 ((xfer->address == udev->address) && 2768 (udev->default_ep_desc.wMaxPacketSize[0] == 2769 udev->ddesc.bMaxPacketSize)); 2770 if (udev->flags.usb_mode == USB_MODE_DEVICE) { 2771 if (no_resetup) { 2772 /* 2773 * NOTE: checking "xfer->address" and 2774 * starting the USB transfer must be 2775 * atomic! 2776 */ 2777 usbd_transfer_start(xfer); 2778 } 2779 } 2780 USB_XFER_UNLOCK(xfer); 2781 } else { 2782 no_resetup = 0; 2783 } 2784 2785 if (no_resetup) { 2786 /* 2787 * All parameters are exactly the same like before. 2788 * Just return. 2789 */ 2790 return; 2791 } 2792 /* 2793 * Update wMaxPacketSize for the default control endpoint: 2794 */ 2795 udev->default_ep_desc.wMaxPacketSize[0] = 2796 udev->ddesc.bMaxPacketSize; 2797 2798 /* 2799 * Unsetup any existing USB transfer: 2800 */ 2801 usbd_transfer_unsetup(udev->default_xfer, USB_DEFAULT_XFER_MAX); 2802 2803 /* 2804 * Try to setup a new USB transfer for the 2805 * default control endpoint: 2806 */ 2807 iface_index = 0; 2808 if (usbd_transfer_setup(udev, &iface_index, 2809 udev->default_xfer, usb_control_ep_cfg, USB_DEFAULT_XFER_MAX, NULL, 2810 udev->default_mtx)) { 2811 DPRINTFN(0, "could not setup default " 2812 "USB transfer\n"); 2813 } else { 2814 goto repeat; 2815 } 2816} 2817 2818/*------------------------------------------------------------------------* 2819 * usbd_clear_data_toggle - factored out code 2820 * 2821 * NOTE: the intention of this function is not to reset the hardware 2822 * data toggle. 2823 *------------------------------------------------------------------------*/ 2824void 2825usbd_clear_data_toggle(struct usb_device *udev, struct usb_endpoint *ep) 2826{ 2827 DPRINTFN(5, "udev=%p endpoint=%p\n", udev, ep); 2828 2829 USB_BUS_LOCK(udev->bus); 2830 ep->toggle_next = 0; 2831 USB_BUS_UNLOCK(udev->bus); 2832} 2833 2834/*------------------------------------------------------------------------* 2835 * usbd_clear_stall_callback - factored out clear stall callback 2836 * 2837 * Input parameters: 2838 * xfer1: Clear Stall Control Transfer 2839 * xfer2: Stalled USB Transfer 2840 * 2841 * This function is NULL safe. 2842 * 2843 * Return values: 2844 * 0: In progress 2845 * Else: Finished 2846 * 2847 * Clear stall config example: 2848 * 2849 * static const struct usb_config my_clearstall = { 2850 * .type = UE_CONTROL, 2851 * .endpoint = 0, 2852 * .direction = UE_DIR_ANY, 2853 * .interval = 50, //50 milliseconds 2854 * .bufsize = sizeof(struct usb_device_request), 2855 * .timeout = 1000, //1.000 seconds 2856 * .callback = &my_clear_stall_callback, // ** 2857 * .usb_mode = USB_MODE_HOST, 2858 * }; 2859 * 2860 * ** "my_clear_stall_callback" calls "usbd_clear_stall_callback" 2861 * passing the correct parameters. 2862 *------------------------------------------------------------------------*/ 2863uint8_t 2864usbd_clear_stall_callback(struct usb_xfer *xfer1, 2865 struct usb_xfer *xfer2) 2866{ 2867 struct usb_device_request req; 2868 2869 if (xfer2 == NULL) { 2870 /* looks like we are tearing down */ 2871 DPRINTF("NULL input parameter\n"); 2872 return (0); 2873 } 2874 USB_XFER_LOCK_ASSERT(xfer1, MA_OWNED); 2875 USB_XFER_LOCK_ASSERT(xfer2, MA_OWNED); 2876 2877 switch (USB_GET_STATE(xfer1)) { 2878 case USB_ST_SETUP: 2879 2880 /* 2881 * pre-clear the data toggle to DATA0 ("umass.c" and 2882 * "ata-usb.c" depends on this) 2883 */ 2884 2885 usbd_clear_data_toggle(xfer2->xroot->udev, xfer2->endpoint); 2886 2887 /* setup a clear-stall packet */ 2888 2889 req.bmRequestType = UT_WRITE_ENDPOINT; 2890 req.bRequest = UR_CLEAR_FEATURE; 2891 USETW(req.wValue, UF_ENDPOINT_HALT); 2892 req.wIndex[0] = xfer2->endpoint->edesc->bEndpointAddress; 2893 req.wIndex[1] = 0; 2894 USETW(req.wLength, 0); 2895 2896 /* 2897 * "usbd_transfer_setup_sub()" will ensure that 2898 * we have sufficient room in the buffer for 2899 * the request structure! 2900 */ 2901 2902 /* copy in the transfer */ 2903 2904 usbd_copy_in(xfer1->frbuffers, 0, &req, sizeof(req)); 2905 2906 /* set length */ 2907 xfer1->frlengths[0] = sizeof(req); 2908 xfer1->nframes = 1; 2909 2910 usbd_transfer_submit(xfer1); 2911 return (0); 2912 2913 case USB_ST_TRANSFERRED: 2914 break; 2915 2916 default: /* Error */ 2917 if (xfer1->error == USB_ERR_CANCELLED) { 2918 return (0); 2919 } 2920 break; 2921 } 2922 return (1); /* Clear Stall Finished */ 2923} 2924 2925/*------------------------------------------------------------------------* 2926 * usbd_transfer_poll 2927 * 2928 * The following function gets called from the USB keyboard driver and 2929 * UMASS when the system has paniced. 2930 * 2931 * NOTE: It is currently not possible to resume normal operation on 2932 * the USB controller which has been polled, due to clearing of the 2933 * "up_dsleep" and "up_msleep" flags. 2934 *------------------------------------------------------------------------*/ 2935void 2936usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max) 2937{ 2938 struct usb_xfer *xfer; 2939 struct usb_xfer_root *xroot; 2940 struct usb_device *udev; 2941 struct usb_proc_msg *pm; 2942 uint16_t n; 2943 uint16_t drop_bus; 2944 uint16_t drop_xfer; 2945 2946 for (n = 0; n != max; n++) { 2947 /* Extra checks to avoid panic */ 2948 xfer = ppxfer[n]; 2949 if (xfer == NULL) 2950 continue; /* no USB transfer */ 2951 xroot = xfer->xroot; 2952 if (xroot == NULL) 2953 continue; /* no USB root */ 2954 udev = xroot->udev; 2955 if (udev == NULL) 2956 continue; /* no USB device */ 2957 if (udev->bus == NULL) 2958 continue; /* no BUS structure */ 2959 if (udev->bus->methods == NULL) 2960 continue; /* no BUS methods */ 2961 if (udev->bus->methods->xfer_poll == NULL) 2962 continue; /* no poll method */ 2963 2964 /* make sure that the BUS mutex is not locked */ 2965 drop_bus = 0; 2966 while (mtx_owned(&xroot->udev->bus->bus_mtx)) { 2967 mtx_unlock(&xroot->udev->bus->bus_mtx); 2968 drop_bus++; 2969 } 2970 2971 /* make sure that the transfer mutex is not locked */ 2972 drop_xfer = 0; 2973 while (mtx_owned(xroot->xfer_mtx)) { 2974 mtx_unlock(xroot->xfer_mtx); 2975 drop_xfer++; 2976 } 2977 2978 /* Make sure cv_signal() and cv_broadcast() is not called */ 2979 udev->bus->control_xfer_proc.up_msleep = 0; 2980 udev->bus->explore_proc.up_msleep = 0; 2981 udev->bus->giant_callback_proc.up_msleep = 0; 2982 udev->bus->non_giant_callback_proc.up_msleep = 0; 2983 2984 /* poll USB hardware */ 2985 (udev->bus->methods->xfer_poll) (udev->bus); 2986 2987 USB_BUS_LOCK(xroot->bus); 2988 2989 /* check for clear stall */ 2990 if (udev->default_xfer[1] != NULL) { 2991 2992 /* poll clear stall start */ 2993 pm = &udev->cs_msg[0].hdr; 2994 (pm->pm_callback) (pm); 2995 /* poll clear stall done thread */ 2996 pm = &udev->default_xfer[1]-> 2997 xroot->done_m[0].hdr; 2998 (pm->pm_callback) (pm); 2999 } 3000 3001 /* poll done thread */ 3002 pm = &xroot->done_m[0].hdr; 3003 (pm->pm_callback) (pm); 3004 3005 USB_BUS_UNLOCK(xroot->bus); 3006 3007 /* restore transfer mutex */ 3008 while (drop_xfer--) 3009 mtx_lock(xroot->xfer_mtx); 3010 3011 /* restore BUS mutex */ 3012 while (drop_bus--) 3013 mtx_lock(&xroot->udev->bus->bus_mtx); 3014 } 3015} 3016 3017static void 3018usbd_get_std_packet_size(struct usb_std_packet_size *ptr, 3019 uint8_t type, enum usb_dev_speed speed) 3020{ 3021 static const uint16_t intr_range_max[USB_SPEED_MAX] = { 3022 [USB_SPEED_LOW] = 8, 3023 [USB_SPEED_FULL] = 64, 3024 [USB_SPEED_HIGH] = 1024, 3025 [USB_SPEED_VARIABLE] = 1024, 3026 [USB_SPEED_SUPER] = 1024, 3027 }; 3028 3029 static const uint16_t isoc_range_max[USB_SPEED_MAX] = { 3030 [USB_SPEED_LOW] = 0, /* invalid */ 3031 [USB_SPEED_FULL] = 1023, 3032 [USB_SPEED_HIGH] = 1024, 3033 [USB_SPEED_VARIABLE] = 3584, 3034 [USB_SPEED_SUPER] = 1024, 3035 }; 3036 3037 static const uint16_t control_min[USB_SPEED_MAX] = { 3038 [USB_SPEED_LOW] = 8, 3039 [USB_SPEED_FULL] = 8, 3040 [USB_SPEED_HIGH] = 64, 3041 [USB_SPEED_VARIABLE] = 512, 3042 [USB_SPEED_SUPER] = 512, 3043 }; 3044 3045 static const uint16_t bulk_min[USB_SPEED_MAX] = { 3046 [USB_SPEED_LOW] = 0, /* not supported */ 3047 [USB_SPEED_FULL] = 8, 3048 [USB_SPEED_HIGH] = 512, 3049 [USB_SPEED_VARIABLE] = 512, 3050 [USB_SPEED_SUPER] = 1024, 3051 }; 3052 3053 uint16_t temp; 3054 3055 memset(ptr, 0, sizeof(*ptr)); 3056 3057 switch (type) { 3058 case UE_INTERRUPT: 3059 ptr->range.max = intr_range_max[speed]; 3060 break; 3061 case UE_ISOCHRONOUS: 3062 ptr->range.max = isoc_range_max[speed]; 3063 break; 3064 default: 3065 if (type == UE_BULK) 3066 temp = bulk_min[speed]; 3067 else /* UE_CONTROL */ 3068 temp = control_min[speed]; 3069 3070 /* default is fixed */ 3071 ptr->fixed[0] = temp; 3072 ptr->fixed[1] = temp; 3073 ptr->fixed[2] = temp; 3074 ptr->fixed[3] = temp; 3075 3076 if (speed == USB_SPEED_FULL) { 3077 /* multiple sizes */ 3078 ptr->fixed[1] = 16; 3079 ptr->fixed[2] = 32; 3080 ptr->fixed[3] = 64; 3081 } 3082 if ((speed == USB_SPEED_VARIABLE) && 3083 (type == UE_BULK)) { 3084 /* multiple sizes */ 3085 ptr->fixed[2] = 1024; 3086 ptr->fixed[3] = 1536; 3087 } 3088 break; 3089 } 3090} 3091 3092void * 3093usbd_xfer_softc(struct usb_xfer *xfer) 3094{ 3095 return (xfer->priv_sc); 3096} 3097 3098void * 3099usbd_xfer_get_priv(struct usb_xfer *xfer) 3100{ 3101 return (xfer->priv_fifo); 3102} 3103 3104void 3105usbd_xfer_set_priv(struct usb_xfer *xfer, void *ptr) 3106{ 3107 xfer->priv_fifo = ptr; 3108} 3109 3110uint8_t 3111usbd_xfer_state(struct usb_xfer *xfer) 3112{ 3113 return (xfer->usb_state); 3114} 3115 3116void 3117usbd_xfer_set_flag(struct usb_xfer *xfer, int flag) 3118{ 3119 switch (flag) { 3120 case USB_FORCE_SHORT_XFER: 3121 xfer->flags.force_short_xfer = 1; 3122 break; 3123 case USB_SHORT_XFER_OK: 3124 xfer->flags.short_xfer_ok = 1; 3125 break; 3126 case USB_MULTI_SHORT_OK: 3127 xfer->flags.short_frames_ok = 1; 3128 break; 3129 case USB_MANUAL_STATUS: 3130 xfer->flags.manual_status = 1; 3131 break; 3132 } 3133} 3134 3135void 3136usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag) 3137{ 3138 switch (flag) { 3139 case USB_FORCE_SHORT_XFER: 3140 xfer->flags.force_short_xfer = 0; 3141 break; 3142 case USB_SHORT_XFER_OK: 3143 xfer->flags.short_xfer_ok = 0; 3144 break; 3145 case USB_MULTI_SHORT_OK: 3146 xfer->flags.short_frames_ok = 0; 3147 break; 3148 case USB_MANUAL_STATUS: 3149 xfer->flags.manual_status = 0; 3150 break; 3151 } 3152} 3153 3154/* 3155 * The following function returns in milliseconds when the isochronous 3156 * transfer was completed by the hardware. The returned value wraps 3157 * around 65536 milliseconds. 3158 */ 3159uint16_t 3160usbd_xfer_get_timestamp(struct usb_xfer *xfer) 3161{ 3162 return (xfer->isoc_time_complete); 3163} 3164