usb_core.h revision 239214
12116Sjkh/* $FreeBSD: head/sys/dev/usb/usb_core.h 239214 2012-08-12 17:53:06Z hselasky $ */ 22116Sjkh/*- 32116Sjkh * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 42116Sjkh * 52116Sjkh * Redistribution and use in source and binary forms, with or without 62116Sjkh * modification, are permitted provided that the following conditions 72116Sjkh * are met: 82116Sjkh * 1. Redistributions of source code must retain the above copyright 92116Sjkh * notice, this list of conditions and the following disclaimer. 102116Sjkh * 2. Redistributions in binary form must reproduce the above copyright 118870Srgrimes * notice, this list of conditions and the following disclaimer in the 122116Sjkh * documentation and/or other materials provided with the distribution. 132116Sjkh * 142116Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 152116Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16176451Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17176451Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 182116Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 192116Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 202116Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 212116Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 228870Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 232116Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 242116Sjkh * SUCH DAMAGE. 252116Sjkh */ 26181100Sdas 27181100Sdas/* 28181100Sdas * Including this file is mandatory for all USB related c-files in the kernel. 29181100Sdas */ 302116Sjkh 31181100Sdas#ifndef _USB_CORE_H_ 32181100Sdas#define _USB_CORE_H_ 33181100Sdas 3497407Salfred/* 3597407Salfred * The following macro will tell if an USB transfer is currently 362116Sjkh * receiving or transferring data. 37181100Sdas */ 38181405Sdas#define USB_GET_DATA_ISREAD(xfer) ((xfer)->flags_int.usb_mode == \ 392116Sjkh USB_MODE_DEVICE ? (((xfer)->endpointno & UE_DIR_IN) ? 0 : 1) : \ 402116Sjkh (((xfer)->endpointno & UE_DIR_IN) ? 1 : 0)) 412116Sjkh 42181257Sdas/* macros */ 43181257Sdas 44181257Sdas#define USB_BUS_LOCK(_b) mtx_lock(&(_b)->bus_mtx) 458870Srgrimes#define USB_BUS_UNLOCK(_b) mtx_unlock(&(_b)->bus_mtx) 462116Sjkh#define USB_BUS_LOCK_ASSERT(_b, _t) mtx_assert(&(_b)->bus_mtx, _t) 47181257Sdas#define USB_XFER_LOCK(_x) mtx_lock((_x)->xroot->xfer_mtx) 482116Sjkh#define USB_XFER_UNLOCK(_x) mtx_unlock((_x)->xroot->xfer_mtx) 49181257Sdas#define USB_XFER_LOCK_ASSERT(_x, _t) mtx_assert((_x)->xroot->xfer_mtx, _t) 50181257Sdas 51181257Sdas/* helper for converting pointers to integers */ 52181257Sdas#define USB_P2U(ptr) \ 53181257Sdas (((const uint8_t *)(ptr)) - ((const uint8_t *)0)) 54181257Sdas 552116Sjkh/* helper for computing offsets */ 562116Sjkh#define USB_ADD_BYTES(ptr,size) \ 572116Sjkh ((void *)(USB_P2U(ptr) + (size))) 582116Sjkh 59181100Sdas/* debug macro */ 60181100Sdas#define USB_ASSERT KASSERT 61181257Sdas 62181100Sdas/* structure prototypes */ 63181100Sdas 648870Srgrimesstruct file; 652116Sjkhstruct usb_bus; 66struct usb_device; 67struct usb_device_request; 68struct usb_page; 69struct usb_page_cache; 70struct usb_xfer; 71struct usb_xfer_root; 72 73/* typedefs */ 74 75/* structures */ 76 77/* 78 * The following structure defines a set of internal USB transfer 79 * flags. 80 */ 81struct usb_xfer_flags_int { 82 83 enum usb_hc_mode usb_mode; /* shadow copy of "udev->usb_mode" */ 84 uint16_t control_rem; /* remainder in bytes */ 85 86 uint8_t open:1; /* set if USB pipe has been opened */ 87 uint8_t transferring:1; /* set if an USB transfer is in 88 * progress */ 89 uint8_t did_dma_delay:1; /* set if we waited for HW DMA */ 90 uint8_t did_close:1; /* set if we closed the USB transfer */ 91 uint8_t draining:1; /* set if we are draining an USB 92 * transfer */ 93 uint8_t started:1; /* keeps track of started or stopped */ 94 uint8_t bandwidth_reclaimed:1; 95 uint8_t control_xfr:1; /* set if control transfer */ 96 uint8_t control_hdr:1; /* set if control header should be 97 * sent */ 98 uint8_t control_act:1; /* set if control transfer is active */ 99 uint8_t control_stall:1; /* set if control transfer should be stalled */ 100 101 uint8_t short_frames_ok:1; /* filtered version */ 102 uint8_t short_xfer_ok:1; /* filtered version */ 103#if USB_HAVE_BUSDMA 104 uint8_t bdma_enable:1; /* filtered version (only set if 105 * hardware supports DMA) */ 106 uint8_t bdma_no_post_sync:1; /* set if the USB callback wrapper 107 * should not do the BUS-DMA post sync 108 * operation */ 109 uint8_t bdma_setup:1; /* set if BUS-DMA has been setup */ 110#endif 111 uint8_t isochronous_xfr:1; /* set if isochronous transfer */ 112 uint8_t curr_dma_set:1; /* used by USB HC/DC driver */ 113 uint8_t can_cancel_immed:1; /* set if USB transfer can be 114 * cancelled immediately */ 115 uint8_t doing_callback:1; /* set if executing the callback */ 116}; 117 118/* 119 * The following structure defines an USB transfer. 120 */ 121struct usb_xfer { 122 struct usb_callout timeout_handle; 123 TAILQ_ENTRY(usb_xfer) wait_entry; /* used at various places */ 124 125 struct usb_page_cache *buf_fixup; /* fixup buffer(s) */ 126 struct usb_xfer_queue *wait_queue; /* pointer to queue that we 127 * are waiting on */ 128 struct usb_page *dma_page_ptr; 129 struct usb_endpoint *endpoint; /* our USB endpoint */ 130 struct usb_xfer_root *xroot; /* used by HC driver */ 131 void *qh_start[2]; /* used by HC driver */ 132 void *td_start[2]; /* used by HC driver */ 133 void *td_transfer_first; /* used by HC driver */ 134 void *td_transfer_last; /* used by HC driver */ 135 void *td_transfer_cache; /* used by HC driver */ 136 void *priv_sc; /* device driver data pointer 1 */ 137 void *priv_fifo; /* device driver data pointer 2 */ 138 void *local_buffer; 139 usb_frlength_t *frlengths; 140 struct usb_page_cache *frbuffers; 141 usb_callback_t *callback; 142 143 usb_frlength_t max_hc_frame_size; 144 usb_frlength_t max_data_length; 145 usb_frlength_t sumlen; /* sum of all lengths in bytes */ 146 usb_frlength_t actlen; /* actual length in bytes */ 147 usb_timeout_t timeout; /* milliseconds */ 148 149 usb_frcount_t max_frame_count; /* initial value of "nframes" after 150 * setup */ 151 usb_frcount_t nframes; /* number of USB frames to transfer */ 152 usb_frcount_t aframes; /* actual number of USB frames 153 * transferred */ 154 usb_stream_t stream_id; /* USB3.0 specific field */ 155 156 uint16_t max_packet_size; 157 uint16_t max_frame_size; 158 uint16_t qh_pos; 159 uint16_t isoc_time_complete; /* in ms */ 160 usb_timeout_t interval; /* milliseconds */ 161 162 uint8_t address; /* physical USB address */ 163 uint8_t endpointno; /* physical USB endpoint */ 164 uint8_t max_packet_count; 165 uint8_t usb_state; 166 uint8_t fps_shift; /* down shift of FPS, 0..3 */ 167 168 usb_error_t error; 169 170 struct usb_xfer_flags flags; 171 struct usb_xfer_flags_int flags_int; 172}; 173 174/* external variables */ 175 176extern struct mtx usb_ref_lock; 177 178/* typedefs */ 179 180typedef struct malloc_type *usb_malloc_type; 181 182/* prototypes */ 183 184#endif /* _USB_CORE_H_ */ 185