usb_core.h revision 194064
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_core.h 194064 2009-06-12 15:58:55Z thompsa $ */
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
31184610Salfred#ifndef _USB2_CORE_H_
32184610Salfred#define	_USB2_CORE_H_
33184610Salfred
34190185Sthompsa#define	USB_STACK_VERSION 2000		/* 2.0 */
35190185Sthompsa
36190180Sthompsa/* Allow defines in "opt_usb.h" to override configuration */
37190180Sthompsa
38190180Sthompsa#include "opt_usb.h"
39190180Sthompsa#include "opt_bus.h"
40190180Sthompsa
41184610Salfred/* Default USB configuration */
42184610Salfred
43190180Sthompsa/*
44190180Sthompsa * The following macro defines if the code shall use cv_xxx() instead
45190180Sthompsa * of msleep() and wakeup().
46190180Sthompsa */
47190180Sthompsa#ifndef USB_HAVE_CONDVAR
48190180Sthompsa#define	USB_HAVE_CONDVAR 0
49184610Salfred#endif
50184610Salfred
51190180Sthompsa/*
52190180Sthompsa * The following macro defines if the code shall support
53190180Sthompsa * /dev/usb/x.y.z.
54190180Sthompsa */
55189599Sthompsa#ifndef USB_HAVE_UGEN
56189599Sthompsa#define	USB_HAVE_UGEN 1
57189599Sthompsa#endif
58189599Sthompsa
59190180Sthompsa/*
60190180Sthompsa * The following macro defines if the code shall support BUS-DMA.
61190180Sthompsa */
62190180Sthompsa#ifndef USB_HAVE_BUSDMA
63190180Sthompsa#define	USB_HAVE_BUSDMA 1
64190180Sthompsa#endif
65190180Sthompsa
66190180Sthompsa/*
67190180Sthompsa * The following macro defines if the code shall support the Linux
68190180Sthompsa * compatibility layer.
69190180Sthompsa */
70190180Sthompsa#ifndef USB_HAVE_COMPAT_LINUX
71190180Sthompsa#define	USB_HAVE_COMPAT_LINUX 1
72190180Sthompsa#endif
73190180Sthompsa
74190180Sthompsa/*
75190180Sthompsa * The following macro defines if the code shall support
76190180Sthompsa * userland data transfer via copyin() and copyout()
77190180Sthompsa */
78190180Sthompsa#ifndef USB_HAVE_USER_IO
79190180Sthompsa#define	USB_HAVE_USER_IO 1
80190180Sthompsa#endif
81190180Sthompsa
82190180Sthompsa/*
83190180Sthompsa * The following macro defines if the code shall support copy in via
84190180Sthompsa * bsd-mbufs to USB.
85190180Sthompsa */
86190180Sthompsa#ifndef USB_HAVE_MBUF
87190180Sthompsa#define	USB_HAVE_MBUF 1
88190180Sthompsa#endif
89190180Sthompsa
90190180Sthompsa/*
91190180Sthompsa * The following macro defines if the code shall compile a table
92190180Sthompsa * describing USB vendor and product IDs.
93190180Sthompsa */
94190180Sthompsa#ifndef USB_VERBOSE
95190180Sthompsa#define	USB_VERBOSE 1
96190180Sthompsa#endif
97190180Sthompsa
98190180Sthompsa/*
99190180Sthompsa * The following macro defines if USB debugging support shall be
100190180Sthompsa * compiled for the USB core and all drivers.
101190180Sthompsa */
102190180Sthompsa#ifndef USB_DEBUG
103190180Sthompsa#define	USB_DEBUG 1
104190180Sthompsa#endif
105190180Sthompsa
106190734Sthompsa/*
107190734Sthompsa * The following macro defines if USB transaction translator support
108190735Sthompsa * shall be supported for the USB HUB and USB controller drivers.
109190734Sthompsa */
110190734Sthompsa#ifndef	USB_HAVE_TT_SUPPORT
111190734Sthompsa#define	USB_HAVE_TT_SUPPORT 1
112190734Sthompsa#endif
113190734Sthompsa
114190734Sthompsa/*
115190734Sthompsa * The following macro defines if the USB power daemon shall
116190735Sthompsa * be supported in the USB core.
117190734Sthompsa */
118190734Sthompsa#ifndef	USB_HAVE_POWERD
119190734Sthompsa#define	USB_HAVE_POWERD 1
120190734Sthompsa#endif
121190734Sthompsa
122190735Sthompsa/*
123190735Sthompsa * The following macro defines if the USB autoinstall detection shall
124190735Sthompsa * be supported in the USB core.
125190735Sthompsa */
126190735Sthompsa#ifndef USB_HAVE_MSCTEST
127190735Sthompsa#define	USB_HAVE_MSCTEST 1
128190735Sthompsa#endif
129190735Sthompsa
130184610Salfred#ifndef USB_TD_GET_PROC
131184610Salfred#define	USB_TD_GET_PROC(td) (td)->td_proc
132184610Salfred#endif
133184610Salfred
134184610Salfred#ifndef USB_PROC_GET_GID
135184610Salfred#define	USB_PROC_GET_GID(td) (td)->p_pgid
136184610Salfred#endif
137184610Salfred
138184610Salfred/* Include files */
139184610Salfred
140184610Salfred#include <sys/stdint.h>
141184610Salfred#include <sys/stddef.h>
142184610Salfred#include <sys/param.h>
143184610Salfred#include <sys/queue.h>
144184610Salfred#include <sys/types.h>
145184610Salfred#include <sys/systm.h>
146184610Salfred#include <sys/kernel.h>
147184610Salfred#include <sys/bus.h>
148184610Salfred#include <sys/linker_set.h>
149184610Salfred#include <sys/module.h>
150184610Salfred#include <sys/lock.h>
151184610Salfred#include <sys/mutex.h>
152184610Salfred#include <sys/condvar.h>
153184610Salfred#include <sys/sysctl.h>
154184610Salfred#include <sys/sx.h>
155184610Salfred#include <sys/unistd.h>
156184610Salfred#include <sys/callout.h>
157184610Salfred#include <sys/malloc.h>
158184610Salfred#include <sys/priv.h>
159184610Salfred
160190185Sthompsa#include <dev/usb/usb_defs.h>
161188942Sthompsa#include <dev/usb/usb_revision.h>
162184610Salfred
163188942Sthompsa#include "usb_if.h"
164184610Salfred
165190185Sthompsa#ifndef USB_HOST_ALIGN
166184610Salfred#define	USB_HOST_ALIGN    8		/* bytes, must be power of two */
167190185Sthompsa#endif
168184610Salfred
169190185Sthompsa#ifndef USB_FS_ISOC_UFRAME_MAX
170184610Salfred#define	USB_FS_ISOC_UFRAME_MAX 4	/* exclusive unit */
171190185Sthompsa#endif
172184610Salfred
173184610Salfred#if (USB_FS_ISOC_UFRAME_MAX > 6)
174184610Salfred#error "USB_FS_ISOC_UFRAME_MAX cannot be set higher than 6"
175184610Salfred#endif
176184610Salfred
177190185Sthompsa#ifndef USB_BUS_MAX
178190185Sthompsa#define	USB_BUS_MAX 256			/* units */
179190185Sthompsa#endif
180190185Sthompsa
181190185Sthompsa#ifndef USB_MAX_DEVICES
182190185Sthompsa#define	USB_MAX_DEVICES 128		/* units */
183190185Sthompsa#endif
184190185Sthompsa
185190185Sthompsa#if (USB_MAX_DEVICES < USB_MIN_DEVICES)
186190185Sthompsa#error "Minimum number of devices is greater than maximum number of devices."
187190185Sthompsa#endif
188190185Sthompsa
189190185Sthompsa#ifndef USB_IFACE_MAX
190190185Sthompsa#define	USB_IFACE_MAX 32		/* units */
191190185Sthompsa#endif
192190185Sthompsa
193190185Sthompsa#ifndef USB_FIFO_MAX
194190185Sthompsa#define	USB_FIFO_MAX 128		/* units */
195190185Sthompsa#endif
196190185Sthompsa
197190185Sthompsa#if (USB_FIFO_MAX & 1)
198190185Sthompsa#error "Number of FIFOs must be odd."
199190185Sthompsa#endif
200190185Sthompsa
201184610Salfred#define	USB_MAX_FS_ISOC_FRAMES_PER_XFER (120)	/* units */
202184610Salfred#define	USB_MAX_HS_ISOC_FRAMES_PER_XFER (8*120)	/* units */
203184610Salfred
204190185Sthompsa#ifndef USB_HUB_MAX_DEPTH
205190185Sthompsa#define	USB_HUB_MAX_DEPTH	5
206190185Sthompsa#endif
207184610Salfred
208190181Sthompsa#ifndef USB_EP0_BUFSIZE
209190181Sthompsa#define	USB_EP0_BUFSIZE		1024	/* bytes */
210190181Sthompsa#endif
211190181Sthompsa
212184610Salfred/* USB transfer states */
213184610Salfred
214184610Salfred#define	USB_ST_SETUP       0
215184610Salfred#define	USB_ST_TRANSFERRED 1
216184610Salfred#define	USB_ST_ERROR       2
217184610Salfred
218194064Sthompsa/* USB handle request states */
219194064Sthompsa#define	USB_HR_NOT_COMPLETE 0
220194064Sthompsa#define	USB_HR_COMPLETE_OK  1
221194064Sthompsa#define	USB_HR_COMPLETE_ERR 2
222194064Sthompsa
223184610Salfred/*
224184610Salfred * The following macro will return the current state of an USB
225184610Salfred * transfer like defined by the "USB_ST_XXX" enums.
226184610Salfred */
227184610Salfred#define	USB_GET_STATE(xfer) ((xfer)->usb2_state)
228184610Salfred
229184610Salfred/*
230184610Salfred * The following macro will tell if an USB transfer is currently
231184610Salfred * receiving or transferring data.
232184610Salfred */
233192499Sthompsa#define	USB_GET_DATA_ISREAD(xfer) ((xfer)->flags_int.usb_mode == \
234193644Sthompsa	USB_MODE_DEVICE ? (((xfer)->endpointno & UE_DIR_IN) ? 0 : 1) : \
235193644Sthompsa	(((xfer)->endpointno & UE_DIR_IN) ? 1 : 0))
236184610Salfred
237184610Salfred/*
238184610Salfred * The following macros are used used to convert milliseconds into
239184610Salfred * HZ. We use 1024 instead of 1000 milliseconds per second to save a
240184610Salfred * full division.
241184610Salfred */
242184610Salfred#define	USB_MS_HZ 1024
243184610Salfred
244184610Salfred#define	USB_MS_TO_TICKS(ms) \
245184610Salfred  (((uint32_t)((((uint32_t)(ms)) * ((uint32_t)(hz))) + USB_MS_HZ - 1)) / USB_MS_HZ)
246184610Salfred
247184610Salfred/* macros */
248184610Salfred
249184610Salfred#define	usb2_callout_init_mtx(c,m,f) callout_init_mtx(&(c)->co,m,f)
250184610Salfred#define	usb2_callout_reset(c,t,f,d) callout_reset(&(c)->co,t,f,d)
251184610Salfred#define	usb2_callout_stop(c) callout_stop(&(c)->co)
252184610Salfred#define	usb2_callout_drain(c) callout_drain(&(c)->co)
253184610Salfred#define	usb2_callout_pending(c) callout_pending(&(c)->co)
254184610Salfred
255185087Salfred#define	USB_BUS_LOCK(_b)		mtx_lock(&(_b)->bus_mtx)
256185087Salfred#define	USB_BUS_UNLOCK(_b)		mtx_unlock(&(_b)->bus_mtx)
257185087Salfred#define	USB_BUS_LOCK_ASSERT(_b, _t)	mtx_assert(&(_b)->bus_mtx, _t)
258187173Sthompsa#define	USB_XFER_LOCK(_x)		mtx_lock((_x)->xroot->xfer_mtx)
259187173Sthompsa#define	USB_XFER_UNLOCK(_x)		mtx_unlock((_x)->xroot->xfer_mtx)
260187173Sthompsa#define	USB_XFER_LOCK_ASSERT(_x, _t)	mtx_assert((_x)->xroot->xfer_mtx, _t)
261184610Salfred/* structure prototypes */
262184610Salfred
263184610Salfredstruct file;
264192984Sthompsastruct usb_bus;
265192984Sthompsastruct usb_device;
266192984Sthompsastruct usb_device_request;
267192984Sthompsastruct usb_page;
268192984Sthompsastruct usb_page_cache;
269192984Sthompsastruct usb_xfer;
270192984Sthompsastruct usb_xfer_root;
271184610Salfred
272184610Salfred/* typedefs */
273184610Salfred
274193045Sthompsatypedef void (usb_callback_t)(struct usb_xfer *);
275184610Salfred
276190174Sthompsa#ifndef USB_HAVE_USB_ERROR_T
277193045Sthompsatypedef uint8_t usb_error_t;		/* see "USB_ERR_XXX" */
278190174Sthompsa#endif
279190174Sthompsa
280190174Sthompsa#ifndef USB_HAVE_TIMEOUT_T
281193045Sthompsatypedef uint32_t usb_timeout_t;	/* milliseconds */
282190174Sthompsa#endif
283190174Sthompsa
284190181Sthompsa#ifndef USB_HAVE_FRLENGTH_T
285193045Sthompsatypedef uint32_t usb_frlength_t;	/* bytes */
286190174Sthompsa#endif
287190174Sthompsa
288190181Sthompsa#ifndef USB_HAVE_FRCOUNT_T
289193045Sthompsatypedef uint32_t usb_frcount_t;	/* units */
290190174Sthompsa#endif
291190174Sthompsa
292193074Sthompsa#ifndef USB_HAVE_SIZE_T
293193074Sthompsatypedef uint32_t usb_size_t;		/* bytes */
294193074Sthompsa#endif
295193074Sthompsa
296190174Sthompsa#ifndef USB_HAVE_TICKS_T
297193045Sthompsatypedef uint32_t usb_ticks_t;		/* system defined */
298190174Sthompsa#endif
299190174Sthompsa
300190181Sthompsa#ifndef USB_HAVE_POWER_MASK_T
301193045Sthompsatypedef uint16_t usb_power_mask_t;	/* see "USB_HW_POWER_XXX" */
302190181Sthompsa#endif
303190181Sthompsa
304193045Sthompsatypedef usb_error_t (usb_handle_req_t)(struct usb_device *,
305192984Sthompsa    struct usb_device_request *, const void **, uint16_t *);
306191402Sthompsa
307184610Salfred/* structures */
308184610Salfred
309184610Salfred/*
310184610Salfred * Common queue structure for USB transfers.
311184610Salfred */
312192984Sthompsastruct usb_xfer_queue {
313192984Sthompsa	TAILQ_HEAD(, usb_xfer) head;
314192984Sthompsa	struct usb_xfer *curr;		/* current USB transfer processed */
315192984Sthompsa	void    (*command) (struct usb_xfer_queue *pq);
316184610Salfred	uint8_t	recurse_1:1;
317184610Salfred	uint8_t	recurse_2:1;
318184610Salfred};
319184610Salfred
320184610Salfred/*
321184610Salfred * The following is a wrapper for the callout structure to ease
322184610Salfred * porting the code to other platforms.
323184610Salfred */
324192984Sthompsastruct usb_callout {
325184610Salfred	struct callout co;
326184610Salfred};
327184610Salfred
328184610Salfred/*
329184610Salfred * The following structure defines a set of USB transfer flags.
330184610Salfred */
331192984Sthompsastruct usb_xfer_flags {
332184610Salfred	uint8_t	force_short_xfer:1;	/* force a short transmit transfer
333184610Salfred					 * last */
334184610Salfred	uint8_t	short_xfer_ok:1;	/* allow short receive transfers */
335184610Salfred	uint8_t	short_frames_ok:1;	/* allow short frames */
336184610Salfred	uint8_t	pipe_bof:1;		/* block pipe on failure */
337184610Salfred	uint8_t	proxy_buffer:1;		/* makes buffer size a factor of
338184610Salfred					 * "max_frame_size" */
339184610Salfred	uint8_t	ext_buffer:1;		/* uses external DMA buffer */
340184610Salfred	uint8_t	manual_status:1;	/* non automatic status stage on
341184610Salfred					 * control transfers */
342184610Salfred	uint8_t	no_pipe_ok:1;		/* set if "USB_ERR_NO_PIPE" error can
343184610Salfred					 * be ignored */
344184610Salfred	uint8_t	stall_pipe:1;		/* set if the endpoint belonging to
345184610Salfred					 * this USB transfer should be stalled
346184610Salfred					 * before starting this transfer! */
347184610Salfred};
348184610Salfred
349184610Salfred/*
350184610Salfred * The following structure defines a set of internal USB transfer
351184610Salfred * flags.
352184610Salfred */
353192984Sthompsastruct usb_xfer_flags_int {
354192499Sthompsa
355192499Sthompsa	enum usb_hc_mode usb_mode;	/* shadow copy of "udev->usb_mode" */
356184610Salfred	uint16_t control_rem;		/* remainder in bytes */
357184610Salfred
358184610Salfred	uint8_t	open:1;			/* set if USB pipe has been opened */
359184610Salfred	uint8_t	transferring:1;		/* set if an USB transfer is in
360184610Salfred					 * progress */
361184610Salfred	uint8_t	did_dma_delay:1;	/* set if we waited for HW DMA */
362184610Salfred	uint8_t	did_close:1;		/* set if we closed the USB transfer */
363184610Salfred	uint8_t	draining:1;		/* set if we are draining an USB
364184610Salfred					 * transfer */
365184610Salfred	uint8_t	started:1;		/* keeps track of started or stopped */
366184610Salfred	uint8_t	bandwidth_reclaimed:1;
367184610Salfred	uint8_t	control_xfr:1;		/* set if control transfer */
368184610Salfred	uint8_t	control_hdr:1;		/* set if control header should be
369184610Salfred					 * sent */
370184610Salfred	uint8_t	control_act:1;		/* set if control transfer is active */
371192552Sthompsa	uint8_t	control_stall:1;	/* set if control transfer should be stalled */
372184610Salfred
373184610Salfred	uint8_t	short_frames_ok:1;	/* filtered version */
374184610Salfred	uint8_t	short_xfer_ok:1;	/* filtered version */
375190180Sthompsa#if USB_HAVE_BUSDMA
376184610Salfred	uint8_t	bdma_enable:1;		/* filtered version (only set if
377184610Salfred					 * hardware supports DMA) */
378184610Salfred	uint8_t	bdma_no_post_sync:1;	/* set if the USB callback wrapper
379184610Salfred					 * should not do the BUS-DMA post sync
380184610Salfred					 * operation */
381184610Salfred	uint8_t	bdma_setup:1;		/* set if BUS-DMA has been setup */
382190180Sthompsa#endif
383184610Salfred	uint8_t	isochronous_xfr:1;	/* set if isochronous transfer */
384184610Salfred	uint8_t	curr_dma_set:1;		/* used by USB HC/DC driver */
385184610Salfred	uint8_t	can_cancel_immed:1;	/* set if USB transfer can be
386184610Salfred					 * cancelled immediately */
387184610Salfred};
388184610Salfred
389184610Salfred/*
390190734Sthompsa * The following structure define an USB configuration, that basically
391190734Sthompsa * is used when setting up an USB transfer.
392184610Salfred */
393192984Sthompsastruct usb_config {
394193045Sthompsa	usb_callback_t *callback;	/* USB transfer callback */
395193045Sthompsa	usb_frlength_t bufsize;	/* total pipe buffer size in bytes */
396193045Sthompsa	usb_frcount_t frames;		/* maximum number of USB frames */
397193045Sthompsa	usb_timeout_t interval;	/* interval in milliseconds */
398184610Salfred#define	USB_DEFAULT_INTERVAL	0
399193045Sthompsa	usb_timeout_t timeout;		/* transfer timeout in milliseconds */
400192984Sthompsa	struct usb_xfer_flags flags;	/* transfer flags */
401192499Sthompsa	enum usb_hc_mode usb_mode;	/* host or device mode */
402184610Salfred	uint8_t	type;			/* pipe type */
403184610Salfred	uint8_t	endpoint;		/* pipe number */
404184610Salfred	uint8_t	direction;		/* pipe direction */
405184610Salfred	uint8_t	ep_index;		/* pipe index match to use */
406184610Salfred	uint8_t	if_index;		/* "ifaces" index to use */
407184610Salfred};
408184610Salfred
409184610Salfred/*
410184610Salfred * The following structure defines an USB transfer.
411184610Salfred */
412192984Sthompsastruct usb_xfer {
413192984Sthompsa	struct usb_callout timeout_handle;
414192984Sthompsa	TAILQ_ENTRY(usb_xfer) wait_entry;	/* used at various places */
415184610Salfred
416192984Sthompsa	struct usb_page_cache *buf_fixup;	/* fixup buffer(s) */
417192984Sthompsa	struct usb_xfer_queue *wait_queue;	/* pointer to queue that we
418184610Salfred						 * are waiting on */
419192984Sthompsa	struct usb_page *dma_page_ptr;
420193644Sthompsa	struct usb_endpoint *endpoint;	/* our USB endpoint */
421192984Sthompsa	struct usb_xfer_root *xroot;	/* used by HC driver */
422184610Salfred	void   *qh_start[2];		/* used by HC driver */
423184610Salfred	void   *td_start[2];		/* used by HC driver */
424184610Salfred	void   *td_transfer_first;	/* used by HC driver */
425184610Salfred	void   *td_transfer_last;	/* used by HC driver */
426184610Salfred	void   *td_transfer_cache;	/* used by HC driver */
427184610Salfred	void   *priv_sc;		/* device driver data pointer 1 */
428184610Salfred	void   *priv_fifo;		/* device driver data pointer 2 */
429184610Salfred	void   *local_buffer;
430193045Sthompsa	usb_frlength_t *frlengths;
431192984Sthompsa	struct usb_page_cache *frbuffers;
432193045Sthompsa	usb_callback_t *callback;
433184610Salfred
434193045Sthompsa	usb_frlength_t max_hc_frame_size;
435193045Sthompsa	usb_frlength_t max_data_length;
436193045Sthompsa	usb_frlength_t sumlen;		/* sum of all lengths in bytes */
437193045Sthompsa	usb_frlength_t actlen;		/* actual length in bytes */
438193045Sthompsa	usb_timeout_t timeout;		/* milliseconds */
439184610Salfred#define	USB_NO_TIMEOUT 0
440184610Salfred#define	USB_DEFAULT_TIMEOUT 5000	/* 5000 ms = 5 seconds */
441184610Salfred
442193045Sthompsa	usb_frcount_t max_frame_count;	/* initial value of "nframes" after
443184610Salfred					 * setup */
444193045Sthompsa	usb_frcount_t nframes;		/* number of USB frames to transfer */
445193045Sthompsa	usb_frcount_t aframes;		/* actual number of USB frames
446184610Salfred					 * transferred */
447184610Salfred
448184610Salfred	uint16_t max_packet_size;
449184610Salfred	uint16_t max_frame_size;
450184610Salfred	uint16_t qh_pos;
451184610Salfred	uint16_t isoc_time_complete;	/* in ms */
452193045Sthompsa	usb_timeout_t interval;	/* milliseconds */
453184610Salfred
454184610Salfred	uint8_t	address;		/* physical USB address */
455193644Sthompsa	uint8_t	endpointno;		/* physical USB endpoint */
456184610Salfred	uint8_t	max_packet_count;
457184610Salfred	uint8_t	usb2_smask;
458184610Salfred	uint8_t	usb2_cmask;
459184610Salfred	uint8_t	usb2_uframe;
460184610Salfred	uint8_t	usb2_state;
461184610Salfred
462193045Sthompsa	usb_error_t error;
463184610Salfred
464192984Sthompsa	struct usb_xfer_flags flags;
465192984Sthompsa	struct usb_xfer_flags_int flags_int;
466184610Salfred};
467184610Salfred
468184610Salfred/*
469184610Salfred * The following structure keeps information that is used to match
470192984Sthompsa * against an array of "usb_device_id" elements.
471184610Salfred */
472192984Sthompsastruct usb_lookup_info {
473184610Salfred	uint16_t idVendor;
474184610Salfred	uint16_t idProduct;
475184610Salfred	uint16_t bcdDevice;
476184610Salfred	uint8_t	bDeviceClass;
477184610Salfred	uint8_t	bDeviceSubClass;
478184610Salfred	uint8_t	bDeviceProtocol;
479184610Salfred	uint8_t	bInterfaceClass;
480184610Salfred	uint8_t	bInterfaceSubClass;
481184610Salfred	uint8_t	bInterfaceProtocol;
482184610Salfred	uint8_t	bIfaceIndex;
483184610Salfred	uint8_t	bIfaceNum;
484184610Salfred	uint8_t	bConfigIndex;
485184610Salfred	uint8_t	bConfigNum;
486184610Salfred};
487184610Salfred
488184610Salfred/* Structure used by probe and attach */
489184610Salfred
490192984Sthompsastruct usb_attach_arg {
491192984Sthompsa	struct usb_lookup_info info;
492184610Salfred	device_t temp_dev;		/* for internal use */
493193733Sthompsa	unsigned long driver_info;	/* for internal use */
494193733Sthompsa	void *driver_ivar;
495192984Sthompsa	struct usb_device *device;	/* current device */
496192984Sthompsa	struct usb_interface *iface;	/* current interface */
497192499Sthompsa	enum usb_hc_mode usb_mode;	/* host or device mode */
498184610Salfred	uint8_t	port;
499184610Salfred	uint8_t	use_generic;		/* hint for generic drivers */
500184610Salfred};
501184610Salfred
502184610Salfred/* external variables */
503184610Salfred
504184610SalfredMALLOC_DECLARE(M_USB);
505184610SalfredMALLOC_DECLARE(M_USBDEV);
506184610SalfredMALLOC_DECLARE(M_USBHC);
507184610Salfred
508184610Salfredextern struct mtx usb2_ref_lock;
509184610Salfred
510184610Salfred/* prototypes */
511184610Salfred
512193045Sthompsaconst char *usb2_errstr(usb_error_t error);
513192500Sthompsaconst char *usb2_statestr(enum usb_dev_state state);
514192984Sthompsastruct usb_config_descriptor *usb2_get_config_descriptor(
515192984Sthompsa	    struct usb_device *udev);
516192984Sthompsastruct usb_device_descriptor *usb2_get_device_descriptor(
517192984Sthompsa	    struct usb_device *udev);
518192984Sthompsastruct usb_interface *usb2_get_iface(struct usb_device *udev,
519185948Sthompsa	    uint8_t iface_index);
520192984Sthompsastruct usb_interface_descriptor *usb2_get_interface_descriptor(
521192984Sthompsa	    struct usb_interface *iface);
522192984Sthompsauint8_t	usb2_clear_stall_callback(struct usb_xfer *xfer1,
523192984Sthompsa	    struct usb_xfer *xfer2);
524192984Sthompsauint8_t	usb2_get_interface_altindex(struct usb_interface *iface);
525193045Sthompsausb_error_t usb2_set_alt_interface_index(struct usb_device *udev,
526185948Sthompsa	    uint8_t iface_index, uint8_t alt_index);
527192984Sthompsaenum usb_hc_mode	usb2_get_mode(struct usb_device *udev);
528192984Sthompsaenum usb_dev_speed	usb2_get_speed(struct usb_device *udev);
529192984Sthompsauint32_t usb2_get_isoc_fps(struct usb_device *udev);
530193045Sthompsausb_error_t usb2_transfer_setup(struct usb_device *udev,
531192984Sthompsa	    const uint8_t *ifaces, struct usb_xfer **pxfer,
532192984Sthompsa	    const struct usb_config *setup_start, uint16_t n_setup,
533185948Sthompsa	    void *priv_sc, struct mtx *priv_mtx);
534192984Sthompsavoid	usb2_set_frame_data(struct usb_xfer *xfer, void *ptr,
535193045Sthompsa	    usb_frcount_t frindex);
536193045Sthompsavoid	usb2_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
537193045Sthompsa	    usb_frcount_t frindex);
538192984Sthompsavoid	usb2_start_hardware(struct usb_xfer *xfer);
539192984Sthompsavoid	usb2_transfer_clear_stall(struct usb_xfer *xfer);
540192984Sthompsavoid	usb2_transfer_drain(struct usb_xfer *xfer);
541192984Sthompsavoid	usb2_transfer_set_stall(struct usb_xfer *xfer);
542192984Sthompsauint8_t	usb2_transfer_pending(struct usb_xfer *xfer);
543192984Sthompsavoid	usb2_transfer_start(struct usb_xfer *xfer);
544192984Sthompsavoid	usb2_transfer_stop(struct usb_xfer *xfer);
545192984Sthompsavoid	usb2_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup);
546192984Sthompsavoid	usb2_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
547185948Sthompsa	    uint8_t parent_index);
548192984Sthompsauint8_t	usb2_get_bus_index(struct usb_device *udev);
549192984Sthompsauint8_t	usb2_get_device_index(struct usb_device *udev);
550192984Sthompsavoid	usb2_set_power_mode(struct usb_device *udev, uint8_t power_mode);
551192984Sthompsauint8_t	usb2_device_attached(struct usb_device *udev);
552184610Salfred
553184610Salfred#endif					/* _USB2_CORE_H_ */
554