usb_core.h revision 198775
190792Sgshapiro/* $FreeBSD: head/sys/dev/usb/usb_core.h 198775 2009-11-01 21:44:37Z thompsa $ */
2120256Sgshapiro/*-
390792Sgshapiro * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
490792Sgshapiro *
590792Sgshapiro * Redistribution and use in source and binary forms, with or without
690792Sgshapiro * modification, are permitted provided that the following conditions
790792Sgshapiro * are met:
890792Sgshapiro * 1. Redistributions of source code must retain the above copyright
990792Sgshapiro *    notice, this list of conditions and the following disclaimer.
1090792Sgshapiro * 2. Redistributions in binary form must reproduce the above copyright
11120256Sgshapiro *    notice, this list of conditions and the following disclaimer in the
1290792Sgshapiro *    documentation and/or other materials provided with the distribution.
1390792Sgshapiro *
1490792Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1590792Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1690792Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1790792Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1890792Sgshapiro * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19120256Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2090792Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2190792Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2290792Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2390792Sgshapiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2490792Sgshapiro * SUCH DAMAGE.
2590792Sgshapiro */
2690792Sgshapiro
2790792Sgshapiro/*
2890792Sgshapiro * Including this file is mandatory for all USB related c-files in the kernel.
2990792Sgshapiro */
3090792Sgshapiro
3190792Sgshapiro#ifndef _USB_CORE_H_
3290792Sgshapiro#define	_USB_CORE_H_
3390792Sgshapiro
3490792Sgshapiro/*
3590792Sgshapiro * The following macro will tell if an USB transfer is currently
3690792Sgshapiro * receiving or transferring data.
3790792Sgshapiro */
3890792Sgshapiro#define	USB_GET_DATA_ISREAD(xfer) ((xfer)->flags_int.usb_mode == \
3990792Sgshapiro	USB_MODE_DEVICE ? (((xfer)->endpointno & UE_DIR_IN) ? 0 : 1) : \
4090792Sgshapiro	(((xfer)->endpointno & UE_DIR_IN) ? 1 : 0))
4190792Sgshapiro
4290792Sgshapiro/* macros */
4390792Sgshapiro
4490792Sgshapiro#define	USB_BUS_LOCK(_b)		mtx_lock(&(_b)->bus_mtx)
4590792Sgshapiro#define	USB_BUS_UNLOCK(_b)		mtx_unlock(&(_b)->bus_mtx)
4690792Sgshapiro#define	USB_BUS_LOCK_ASSERT(_b, _t)	mtx_assert(&(_b)->bus_mtx, _t)
4790792Sgshapiro#define	USB_XFER_LOCK(_x)		mtx_lock((_x)->xroot->xfer_mtx)
4890792Sgshapiro#define	USB_XFER_UNLOCK(_x)		mtx_unlock((_x)->xroot->xfer_mtx)
4990792Sgshapiro#define	USB_XFER_LOCK_ASSERT(_x, _t)	mtx_assert((_x)->xroot->xfer_mtx, _t)
5090792Sgshapiro
5190792Sgshapiro/* helper for converting pointers to integers */
5290792Sgshapiro#define	USB_P2U(ptr) \
5390792Sgshapiro  (((const uint8_t *)(ptr)) - ((const uint8_t *)0))
5490792Sgshapiro
5590792Sgshapiro/* helper for computing offsets */
5690792Sgshapiro#define	USB_ADD_BYTES(ptr,size) \
5790792Sgshapiro  ((void *)(USB_P2U(ptr) + (size)))
5890792Sgshapiro
5990792Sgshapiro/* debug macro */
6090792Sgshapiro#define	USB_ASSERT KASSERT
6190792Sgshapiro
6290792Sgshapiro/* structure prototypes */
6390792Sgshapiro
6490792Sgshapirostruct file;
6590792Sgshapirostruct usb_bus;
6690792Sgshapirostruct usb_device;
6790792Sgshapirostruct usb_device_request;
6890792Sgshapirostruct usb_page;
6990792Sgshapirostruct usb_page_cache;
7090792Sgshapirostruct usb_xfer;
7190792Sgshapirostruct usb_xfer_root;
7290792Sgshapiro
73120256Sgshapiro/* typedefs */
7490792Sgshapiro
7590792Sgshapiro/* structures */
7690792Sgshapiro
7790792Sgshapiro/*
7890792Sgshapiro * The following structure defines a set of internal USB transfer
7990792Sgshapiro * flags.
8090792Sgshapiro */
8190792Sgshapirostruct usb_xfer_flags_int {
8290792Sgshapiro
8390792Sgshapiro	enum usb_hc_mode usb_mode;	/* shadow copy of "udev->usb_mode" */
8490792Sgshapiro	uint16_t control_rem;		/* remainder in bytes */
8590792Sgshapiro
8690792Sgshapiro	uint8_t	open:1;			/* set if USB pipe has been opened */
8790792Sgshapiro	uint8_t	transferring:1;		/* set if an USB transfer is in
8890792Sgshapiro					 * progress */
8990792Sgshapiro	uint8_t	did_dma_delay:1;	/* set if we waited for HW DMA */
90120256Sgshapiro	uint8_t	did_close:1;		/* set if we closed the USB transfer */
9190792Sgshapiro	uint8_t	draining:1;		/* set if we are draining an USB
9290792Sgshapiro					 * transfer */
9390792Sgshapiro	uint8_t	started:1;		/* keeps track of started or stopped */
9490792Sgshapiro	uint8_t	bandwidth_reclaimed:1;
9590792Sgshapiro	uint8_t	control_xfr:1;		/* set if control transfer */
9690792Sgshapiro	uint8_t	control_hdr:1;		/* set if control header should be
9790792Sgshapiro					 * sent */
9890792Sgshapiro	uint8_t	control_act:1;		/* set if control transfer is active */
9990792Sgshapiro	uint8_t	control_stall:1;	/* set if control transfer should be stalled */
10090792Sgshapiro
10190792Sgshapiro	uint8_t	short_frames_ok:1;	/* filtered version */
10290792Sgshapiro	uint8_t	short_xfer_ok:1;	/* filtered version */
10390792Sgshapiro#if USB_HAVE_BUSDMA
10490792Sgshapiro	uint8_t	bdma_enable:1;		/* filtered version (only set if
105120256Sgshapiro					 * hardware supports DMA) */
106120256Sgshapiro	uint8_t	bdma_no_post_sync:1;	/* set if the USB callback wrapper
10790792Sgshapiro					 * 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
155	uint16_t max_packet_size;
156	uint16_t max_frame_size;
157	uint16_t qh_pos;
158	uint16_t isoc_time_complete;	/* in ms */
159	usb_timeout_t interval;	/* milliseconds */
160
161	uint8_t	address;		/* physical USB address */
162	uint8_t	endpointno;		/* physical USB endpoint */
163	uint8_t	max_packet_count;
164	uint8_t	usb_smask;
165	uint8_t	usb_cmask;
166	uint8_t	usb_uframe;
167	uint8_t	usb_state;
168
169	usb_error_t error;
170
171	struct usb_xfer_flags flags;
172	struct usb_xfer_flags_int flags_int;
173};
174
175/* external variables */
176
177extern struct mtx usb_ref_lock;
178
179/* typedefs */
180
181typedef struct malloc_type *usb_malloc_type;
182
183/* prototypes */
184
185#endif					/* _USB_CORE_H_ */
186