1184610Salfred/* $FreeBSD$ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24184610Salfred * SUCH DAMAGE. 25184610Salfred */ 26184610Salfred 27184610Salfred/* 28190754Sthompsa * Including this file is mandatory for all USB related c-files in the kernel. 29184610Salfred */ 30184610Salfred 31194230Sthompsa#ifndef _USB_CORE_H_ 32194230Sthompsa#define _USB_CORE_H_ 33184610Salfred 34190180Sthompsa/* 35184610Salfred * The following macro will tell if an USB transfer is currently 36184610Salfred * receiving or transferring data. 37184610Salfred */ 38192499Sthompsa#define USB_GET_DATA_ISREAD(xfer) ((xfer)->flags_int.usb_mode == \ 39193644Sthompsa USB_MODE_DEVICE ? (((xfer)->endpointno & UE_DIR_IN) ? 0 : 1) : \ 40193644Sthompsa (((xfer)->endpointno & UE_DIR_IN) ? 1 : 0)) 41184610Salfred 42184610Salfred/* macros */ 43184610Salfred 44185087Salfred#define USB_BUS_LOCK(_b) mtx_lock(&(_b)->bus_mtx) 45185087Salfred#define USB_BUS_UNLOCK(_b) mtx_unlock(&(_b)->bus_mtx) 46185087Salfred#define USB_BUS_LOCK_ASSERT(_b, _t) mtx_assert(&(_b)->bus_mtx, _t) 47266394Shselasky#define USB_BUS_SPIN_LOCK(_b) mtx_lock_spin(&(_b)->bus_spin_lock) 48266394Shselasky#define USB_BUS_SPIN_UNLOCK(_b) mtx_unlock_spin(&(_b)->bus_spin_lock) 49266394Shselasky#define USB_BUS_SPIN_LOCK_ASSERT(_b, _t) mtx_assert(&(_b)->bus_spin_lock, _t) 50187173Sthompsa#define USB_XFER_LOCK(_x) mtx_lock((_x)->xroot->xfer_mtx) 51187173Sthompsa#define USB_XFER_UNLOCK(_x) mtx_unlock((_x)->xroot->xfer_mtx) 52187173Sthompsa#define USB_XFER_LOCK_ASSERT(_x, _t) mtx_assert((_x)->xroot->xfer_mtx, _t) 53194677Sthompsa 54194677Sthompsa/* helper for converting pointers to integers */ 55194677Sthompsa#define USB_P2U(ptr) \ 56194677Sthompsa (((const uint8_t *)(ptr)) - ((const uint8_t *)0)) 57194677Sthompsa 58194677Sthompsa/* helper for computing offsets */ 59194677Sthompsa#define USB_ADD_BYTES(ptr,size) \ 60194677Sthompsa ((void *)(USB_P2U(ptr) + (size))) 61194677Sthompsa 62194677Sthompsa/* debug macro */ 63194677Sthompsa#define USB_ASSERT KASSERT 64194677Sthompsa 65184610Salfred/* structure prototypes */ 66184610Salfred 67184610Salfredstruct file; 68192984Sthompsastruct usb_bus; 69192984Sthompsastruct usb_device; 70192984Sthompsastruct usb_device_request; 71192984Sthompsastruct usb_page; 72192984Sthompsastruct usb_page_cache; 73192984Sthompsastruct usb_xfer; 74192984Sthompsastruct usb_xfer_root; 75246123Shselaskystruct usb_string_lang; 76184610Salfred 77184610Salfred/* typedefs */ 78184610Salfred 79184610Salfred/* structures */ 80184610Salfred 81184610Salfred/* 82184610Salfred * The following structure defines a set of internal USB transfer 83184610Salfred * flags. 84184610Salfred */ 85192984Sthompsastruct usb_xfer_flags_int { 86192499Sthompsa 87192499Sthompsa enum usb_hc_mode usb_mode; /* shadow copy of "udev->usb_mode" */ 88184610Salfred uint16_t control_rem; /* remainder in bytes */ 89184610Salfred 90184610Salfred uint8_t open:1; /* set if USB pipe has been opened */ 91184610Salfred uint8_t transferring:1; /* set if an USB transfer is in 92184610Salfred * progress */ 93184610Salfred uint8_t did_dma_delay:1; /* set if we waited for HW DMA */ 94184610Salfred uint8_t did_close:1; /* set if we closed the USB transfer */ 95184610Salfred uint8_t draining:1; /* set if we are draining an USB 96184610Salfred * transfer */ 97184610Salfred uint8_t started:1; /* keeps track of started or stopped */ 98184610Salfred uint8_t bandwidth_reclaimed:1; 99184610Salfred uint8_t control_xfr:1; /* set if control transfer */ 100184610Salfred uint8_t control_hdr:1; /* set if control header should be 101184610Salfred * sent */ 102184610Salfred uint8_t control_act:1; /* set if control transfer is active */ 103192552Sthompsa uint8_t control_stall:1; /* set if control transfer should be stalled */ 104278071Shselasky uint8_t control_did_data:1; /* set if control DATA has been transferred */ 105184610Salfred 106184610Salfred uint8_t short_frames_ok:1; /* filtered version */ 107184610Salfred uint8_t short_xfer_ok:1; /* filtered version */ 108190180Sthompsa#if USB_HAVE_BUSDMA 109184610Salfred uint8_t bdma_enable:1; /* filtered version (only set if 110184610Salfred * hardware supports DMA) */ 111184610Salfred uint8_t bdma_no_post_sync:1; /* set if the USB callback wrapper 112184610Salfred * should not do the BUS-DMA post sync 113184610Salfred * operation */ 114184610Salfred uint8_t bdma_setup:1; /* set if BUS-DMA has been setup */ 115190180Sthompsa#endif 116184610Salfred uint8_t isochronous_xfr:1; /* set if isochronous transfer */ 117184610Salfred uint8_t curr_dma_set:1; /* used by USB HC/DC driver */ 118184610Salfred uint8_t can_cancel_immed:1; /* set if USB transfer can be 119184610Salfred * cancelled immediately */ 120198775Sthompsa uint8_t doing_callback:1; /* set if executing the callback */ 121263159Shselasky uint8_t maxp_was_clamped:1; /* set if the max packet size 122263159Shselasky * was outside its allowed range */ 123184610Salfred}; 124184610Salfred 125184610Salfred/* 126184610Salfred * The following structure defines an USB transfer. 127184610Salfred */ 128192984Sthompsastruct usb_xfer { 129192984Sthompsa struct usb_callout timeout_handle; 130192984Sthompsa TAILQ_ENTRY(usb_xfer) wait_entry; /* used at various places */ 131184610Salfred 132192984Sthompsa struct usb_page_cache *buf_fixup; /* fixup buffer(s) */ 133192984Sthompsa struct usb_xfer_queue *wait_queue; /* pointer to queue that we 134184610Salfred * are waiting on */ 135192984Sthompsa struct usb_page *dma_page_ptr; 136193644Sthompsa struct usb_endpoint *endpoint; /* our USB endpoint */ 137192984Sthompsa struct usb_xfer_root *xroot; /* used by HC driver */ 138184610Salfred void *qh_start[2]; /* used by HC driver */ 139184610Salfred void *td_start[2]; /* used by HC driver */ 140184610Salfred void *td_transfer_first; /* used by HC driver */ 141184610Salfred void *td_transfer_last; /* used by HC driver */ 142184610Salfred void *td_transfer_cache; /* used by HC driver */ 143184610Salfred void *priv_sc; /* device driver data pointer 1 */ 144184610Salfred void *priv_fifo; /* device driver data pointer 2 */ 145184610Salfred void *local_buffer; 146193045Sthompsa usb_frlength_t *frlengths; 147192984Sthompsa struct usb_page_cache *frbuffers; 148193045Sthompsa usb_callback_t *callback; 149184610Salfred 150193045Sthompsa usb_frlength_t max_hc_frame_size; 151193045Sthompsa usb_frlength_t max_data_length; 152193045Sthompsa usb_frlength_t sumlen; /* sum of all lengths in bytes */ 153193045Sthompsa usb_frlength_t actlen; /* actual length in bytes */ 154193045Sthompsa usb_timeout_t timeout; /* milliseconds */ 155184610Salfred 156193045Sthompsa usb_frcount_t max_frame_count; /* initial value of "nframes" after 157184610Salfred * setup */ 158193045Sthompsa usb_frcount_t nframes; /* number of USB frames to transfer */ 159193045Sthompsa usb_frcount_t aframes; /* actual number of USB frames 160184610Salfred * transferred */ 161239214Shselasky usb_stream_t stream_id; /* USB3.0 specific field */ 162184610Salfred 163184610Salfred uint16_t max_packet_size; 164184610Salfred uint16_t max_frame_size; 165184610Salfred uint16_t qh_pos; 166184610Salfred uint16_t isoc_time_complete; /* in ms */ 167193045Sthompsa usb_timeout_t interval; /* milliseconds */ 168184610Salfred 169184610Salfred uint8_t address; /* physical USB address */ 170193644Sthompsa uint8_t endpointno; /* physical USB endpoint */ 171184610Salfred uint8_t max_packet_count; 172194228Sthompsa uint8_t usb_state; 173199059Sthompsa uint8_t fps_shift; /* down shift of FPS, 0..3 */ 174184610Salfred 175193045Sthompsa usb_error_t error; 176184610Salfred 177192984Sthompsa struct usb_xfer_flags flags; 178192984Sthompsa struct usb_xfer_flags_int flags_int; 179184610Salfred}; 180184610Salfred 181194677Sthompsa/* external variables */ 182184610Salfred 183194677Sthompsaextern struct mtx usb_ref_lock; 184246123Shselaskyextern const struct usb_string_lang usb_string_lang_en; 185184610Salfred 186194677Sthompsa/* typedefs */ 187184610Salfred 188194677Sthompsatypedef struct malloc_type *usb_malloc_type; 189184610Salfred 190184610Salfred/* prototypes */ 191184610Salfred 192194230Sthompsa#endif /* _USB_CORE_H_ */ 193