1194677Sthompsa/*-
2194677Sthompsa * Copyright (c) 2009 Andrew Thompson
3194677Sthompsa *
4194677Sthompsa * Redistribution and use in source and binary forms, with or without
5194677Sthompsa * modification, are permitted provided that the following conditions
6194677Sthompsa * are met:
7194677Sthompsa * 1. Redistributions of source code must retain the above copyright
8194677Sthompsa *    notice, this list of conditions and the following disclaimer.
9194677Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
10194677Sthompsa *    notice, this list of conditions and the following disclaimer in the
11194677Sthompsa *    documentation and/or other materials provided with the distribution.
12194677Sthompsa *
13194677Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14194677Sthompsa * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15194677Sthompsa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16194677Sthompsa * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17194677Sthompsa * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18194677Sthompsa * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19194677Sthompsa * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20194677Sthompsa * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21194677Sthompsa * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22194677Sthompsa * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23194677Sthompsa *
24194677Sthompsa * $FreeBSD: stable/10/sys/dev/usb/usbdi.h 361912 2020-06-08 09:28:26Z hselasky $
25194677Sthompsa */
26194677Sthompsa#ifndef _USB_USBDI_H_
27194677Sthompsa#define _USB_USBDI_H_
28194677Sthompsa
29194677Sthompsastruct usb_fifo;
30194677Sthompsastruct usb_xfer;
31194677Sthompsastruct usb_device;
32200653Sthompsastruct usb_attach_arg;
33194677Sthompsastruct usb_interface;
34194677Sthompsastruct usb_endpoint;
35194677Sthompsastruct usb_page_cache;
36194677Sthompsastruct usb_page_search;
37194677Sthompsastruct usb_process;
38194677Sthompsastruct usb_proc_msg;
39194677Sthompsastruct usb_mbuf;
40224777Shselaskystruct usb_fs_privdata;
41194677Sthompsastruct mbuf;
42194677Sthompsa
43194677Sthompsatypedef enum {	/* keep in sync with usb_errstr_table */
44194677Sthompsa	USB_ERR_NORMAL_COMPLETION = 0,
45194677Sthompsa	USB_ERR_PENDING_REQUESTS,	/* 1 */
46194677Sthompsa	USB_ERR_NOT_STARTED,		/* 2 */
47194677Sthompsa	USB_ERR_INVAL,			/* 3 */
48194677Sthompsa	USB_ERR_NOMEM,			/* 4 */
49194677Sthompsa	USB_ERR_CANCELLED,		/* 5 */
50194677Sthompsa	USB_ERR_BAD_ADDRESS,		/* 6 */
51194677Sthompsa	USB_ERR_BAD_BUFSIZE,		/* 7 */
52194677Sthompsa	USB_ERR_BAD_FLAG,		/* 8 */
53194677Sthompsa	USB_ERR_NO_CALLBACK,		/* 9 */
54194677Sthompsa	USB_ERR_IN_USE,			/* 10 */
55194677Sthompsa	USB_ERR_NO_ADDR,		/* 11 */
56194677Sthompsa	USB_ERR_NO_PIPE,		/* 12 */
57194677Sthompsa	USB_ERR_ZERO_NFRAMES,		/* 13 */
58194677Sthompsa	USB_ERR_ZERO_MAXP,		/* 14 */
59194677Sthompsa	USB_ERR_SET_ADDR_FAILED,	/* 15 */
60194677Sthompsa	USB_ERR_NO_POWER,		/* 16 */
61194677Sthompsa	USB_ERR_TOO_DEEP,		/* 17 */
62194677Sthompsa	USB_ERR_IOERROR,		/* 18 */
63194677Sthompsa	USB_ERR_NOT_CONFIGURED,		/* 19 */
64194677Sthompsa	USB_ERR_TIMEOUT,		/* 20 */
65194677Sthompsa	USB_ERR_SHORT_XFER,		/* 21 */
66194677Sthompsa	USB_ERR_STALLED,		/* 22 */
67194677Sthompsa	USB_ERR_INTERRUPTED,		/* 23 */
68194677Sthompsa	USB_ERR_DMA_LOAD_FAILED,	/* 24 */
69194677Sthompsa	USB_ERR_BAD_CONTEXT,		/* 25 */
70194677Sthompsa	USB_ERR_NO_ROOT_HUB,		/* 26 */
71194677Sthompsa	USB_ERR_NO_INTR_THREAD,		/* 27 */
72194677Sthompsa	USB_ERR_NOT_LOCKED,		/* 28 */
73194677Sthompsa	USB_ERR_MAX
74194677Sthompsa} usb_error_t;
75194677Sthompsa
76194677Sthompsa/*
77194677Sthompsa * Flags for transfers
78194677Sthompsa */
79194677Sthompsa#define	USB_FORCE_SHORT_XFER	0x0001	/* force a short transmit last */
80194677Sthompsa#define	USB_SHORT_XFER_OK	0x0004	/* allow short reads */
81194677Sthompsa#define	USB_DELAY_STATUS_STAGE	0x0010	/* insert delay before STATUS stage */
82194677Sthompsa#define	USB_USER_DATA_PTR	0x0020	/* internal flag */
83195121Sthompsa#define	USB_MULTI_SHORT_OK	0x0040	/* allow multiple short frames */
84195121Sthompsa#define	USB_MANUAL_STATUS	0x0080	/* manual ctrl status */
85194677Sthompsa
86194677Sthompsa#define	USB_NO_TIMEOUT 0
87194677Sthompsa#define	USB_DEFAULT_TIMEOUT 5000	/* 5000 ms = 5 seconds */
88194677Sthompsa
89194677Sthompsa#if defined(_KERNEL)
90194677Sthompsa/* typedefs */
91194677Sthompsa
92194677Sthompsatypedef void (usb_callback_t)(struct usb_xfer *, usb_error_t);
93194677Sthompsatypedef void (usb_proc_callback_t)(struct usb_proc_msg *);
94194677Sthompsatypedef usb_error_t (usb_handle_req_t)(struct usb_device *,
95194677Sthompsa    struct usb_device_request *, const void **, uint16_t *);
96194677Sthompsa
97194677Sthompsatypedef int (usb_fifo_open_t)(struct usb_fifo *fifo, int fflags);
98194677Sthompsatypedef void (usb_fifo_close_t)(struct usb_fifo *fifo, int fflags);
99194677Sthompsatypedef int (usb_fifo_ioctl_t)(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags);
100194677Sthompsatypedef void (usb_fifo_cmd_t)(struct usb_fifo *fifo);
101194677Sthompsatypedef void (usb_fifo_filter_t)(struct usb_fifo *fifo, struct usb_mbuf *m);
102194677Sthompsa
103200653Sthompsa
104200653Sthompsa/* USB events */
105246122Shselasky#ifndef USB_GLOBAL_INCLUDE_FILE
106200653Sthompsa#include <sys/eventhandler.h>
107246122Shselasky#endif
108200653Sthompsatypedef void (*usb_dev_configured_t)(void *, struct usb_device *,
109200653Sthompsa    struct usb_attach_arg *);
110200653SthompsaEVENTHANDLER_DECLARE(usb_dev_configured, usb_dev_configured_t);
111200653Sthompsa
112194677Sthompsa/*
113194677Sthompsa * The following macros are used used to convert milliseconds into
114194677Sthompsa * HZ. We use 1024 instead of 1000 milliseconds per second to save a
115194677Sthompsa * full division.
116194677Sthompsa */
117194677Sthompsa#define	USB_MS_HZ 1024
118194677Sthompsa
119194677Sthompsa#define	USB_MS_TO_TICKS(ms) \
120194677Sthompsa  (((uint32_t)((((uint32_t)(ms)) * ((uint32_t)(hz))) + USB_MS_HZ - 1)) / USB_MS_HZ)
121194677Sthompsa
122194677Sthompsa/*
123194677Sthompsa * Common queue structure for USB transfers.
124194677Sthompsa */
125194677Sthompsastruct usb_xfer_queue {
126194677Sthompsa	TAILQ_HEAD(, usb_xfer) head;
127194677Sthompsa	struct usb_xfer *curr;		/* current USB transfer processed */
128194677Sthompsa	void    (*command) (struct usb_xfer_queue *pq);
129194677Sthompsa	uint8_t	recurse_1:1;
130194677Sthompsa	uint8_t	recurse_2:1;
131287274Shselasky	uint8_t	recurse_3:1;
132287274Shselasky	uint8_t	reserved:5;
133194677Sthompsa};
134194677Sthompsa
135194677Sthompsa/*
136194677Sthompsa * The following structure defines an USB endpoint
137194677Sthompsa * USB endpoint.
138194677Sthompsa */
139194677Sthompsastruct usb_endpoint {
140239214Shselasky	/* queue of USB transfers */
141239214Shselasky	struct usb_xfer_queue endpoint_q[USB_MAX_EP_STREAMS];
142194677Sthompsa
143194677Sthompsa	struct usb_endpoint_descriptor *edesc;
144213435Shselasky	struct usb_endpoint_ss_comp_descriptor *ecomp;
145194677Sthompsa	struct usb_pipe_methods *methods;	/* set by HC driver */
146194677Sthompsa
147194677Sthompsa	uint16_t isoc_next;
148194677Sthompsa
149194677Sthompsa	uint8_t	toggle_next:1;		/* next data toggle value */
150194677Sthompsa	uint8_t	is_stalled:1;		/* set if endpoint is stalled */
151194677Sthompsa	uint8_t	is_synced:1;		/* set if we a synchronised */
152194677Sthompsa	uint8_t	unused:5;
153194677Sthompsa	uint8_t	iface_index;		/* not used by "default endpoint" */
154199672Sthompsa
155199672Sthompsa	uint8_t refcount_alloc;		/* allocation refcount */
156199672Sthompsa	uint8_t refcount_bw;		/* bandwidth refcount */
157199672Sthompsa#define	USB_EP_REF_MAX 0x3f
158199672Sthompsa
159199672Sthompsa	/* High-Speed resource allocation (valid if "refcount_bw" > 0) */
160199672Sthompsa
161199672Sthompsa	uint8_t	usb_smask;		/* USB start mask */
162199672Sthompsa	uint8_t	usb_cmask;		/* USB complete mask */
163199672Sthompsa	uint8_t	usb_uframe;		/* USB microframe */
164239214Shselasky
165239214Shselasky	/* USB endpoint mode, see USB_EP_MODE_XXX */
166239214Shselasky
167239214Shselasky	uint8_t ep_mode;
168194677Sthompsa};
169194677Sthompsa
170194677Sthompsa/*
171194677Sthompsa * The following structure defines an USB interface.
172194677Sthompsa */
173194677Sthompsastruct usb_interface {
174194677Sthompsa	struct usb_interface_descriptor *idesc;
175194677Sthompsa	device_t subdev;
176194677Sthompsa	uint8_t	alt_index;
177194677Sthompsa	uint8_t	parent_iface_index;
178194677Sthompsa
179194677Sthompsa	/* Linux compat */
180194677Sthompsa	struct usb_host_interface *altsetting;
181194677Sthompsa	struct usb_host_interface *cur_altsetting;
182194677Sthompsa	struct usb_device *linux_udev;
183194677Sthompsa	void   *bsd_priv_sc;		/* device specific information */
184214429Shselasky	char   *pnpinfo;		/* additional PnP-info for this interface */
185194677Sthompsa	uint8_t	num_altsetting;		/* number of alternate settings */
186194677Sthompsa	uint8_t	bsd_iface_index;
187194677Sthompsa};
188194677Sthompsa
189194677Sthompsa/*
190194677Sthompsa * The following structure defines a set of USB transfer flags.
191194677Sthompsa */
192194677Sthompsastruct usb_xfer_flags {
193194677Sthompsa	uint8_t	force_short_xfer:1;	/* force a short transmit transfer
194194677Sthompsa					 * last */
195194677Sthompsa	uint8_t	short_xfer_ok:1;	/* allow short receive transfers */
196194677Sthompsa	uint8_t	short_frames_ok:1;	/* allow short frames */
197194677Sthompsa	uint8_t	pipe_bof:1;		/* block pipe on failure */
198194677Sthompsa	uint8_t	proxy_buffer:1;		/* makes buffer size a factor of
199194677Sthompsa					 * "max_frame_size" */
200194677Sthompsa	uint8_t	ext_buffer:1;		/* uses external DMA buffer */
201194677Sthompsa	uint8_t	manual_status:1;	/* non automatic status stage on
202194677Sthompsa					 * control transfers */
203194677Sthompsa	uint8_t	no_pipe_ok:1;		/* set if "USB_ERR_NO_PIPE" error can
204194677Sthompsa					 * be ignored */
205194677Sthompsa	uint8_t	stall_pipe:1;		/* set if the endpoint belonging to
206194677Sthompsa					 * this USB transfer should be stalled
207194677Sthompsa					 * before starting this transfer! */
208219100Shselasky	uint8_t pre_scale_frames:1;	/* "usb_config->frames" is
209219100Shselasky					 * assumed to give the
210219100Shselasky					 * buffering time in
211219100Shselasky					 * milliseconds and is
212219100Shselasky					 * converted into the nearest
213219100Shselasky					 * number of frames when the
214219100Shselasky					 * USB transfer is setup. This
215219100Shselasky					 * option only has effect for
216219100Shselasky					 * ISOCHRONOUS transfers.
217219100Shselasky					 */
218194677Sthompsa};
219194677Sthompsa
220194677Sthompsa/*
221194677Sthompsa * The following structure define an USB configuration, that basically
222194677Sthompsa * is used when setting up an USB transfer.
223194677Sthompsa */
224194677Sthompsastruct usb_config {
225194677Sthompsa	usb_callback_t *callback;	/* USB transfer callback */
226194677Sthompsa	usb_frlength_t bufsize;	/* total pipe buffer size in bytes */
227194677Sthompsa	usb_frcount_t frames;		/* maximum number of USB frames */
228194677Sthompsa	usb_timeout_t interval;	/* interval in milliseconds */
229194677Sthompsa#define	USB_DEFAULT_INTERVAL	0
230194677Sthompsa	usb_timeout_t timeout;		/* transfer timeout in milliseconds */
231194677Sthompsa	struct usb_xfer_flags flags;	/* transfer flags */
232239214Shselasky	usb_stream_t stream_id;		/* USB3.0 specific */
233194677Sthompsa	enum usb_hc_mode usb_mode;	/* host or device mode */
234194677Sthompsa	uint8_t	type;			/* pipe type */
235194677Sthompsa	uint8_t	endpoint;		/* pipe number */
236194677Sthompsa	uint8_t	direction;		/* pipe direction */
237194677Sthompsa	uint8_t	ep_index;		/* pipe index match to use */
238194677Sthompsa	uint8_t	if_index;		/* "ifaces" index to use */
239194677Sthompsa};
240194677Sthompsa
241194677Sthompsa/*
242223486Shselasky * Use these macro when defining USB device ID arrays if you want to
243223486Shselasky * have your driver module automatically loaded in host, device or
244223486Shselasky * both modes respectivly:
245223486Shselasky */
246246194Shselasky#if USB_HAVE_ID_SECTION
247223486Shselasky#define	STRUCT_USB_HOST_ID \
248223486Shselasky    struct usb_device_id __section("usb_host_id")
249223486Shselasky#define	STRUCT_USB_DEVICE_ID \
250223486Shselasky    struct usb_device_id __section("usb_device_id")
251223486Shselasky#define	STRUCT_USB_DUAL_ID \
252223486Shselasky    struct usb_device_id __section("usb_dual_id")
253246194Shselasky#else
254246194Shselasky#define	STRUCT_USB_HOST_ID \
255246194Shselasky    struct usb_device_id
256246194Shselasky#define	STRUCT_USB_DEVICE_ID \
257246194Shselasky    struct usb_device_id
258246194Shselasky#define	STRUCT_USB_DUAL_ID \
259246194Shselasky    struct usb_device_id
260246194Shselasky#endif			/* USB_HAVE_ID_SECTION */
261223486Shselasky
262223486Shselasky/*
263194677Sthompsa * The following structure is used when looking up an USB driver for
264194677Sthompsa * an USB device. It is inspired by the Linux structure called
265194677Sthompsa * "usb_device_id".
266194677Sthompsa */
267194677Sthompsastruct usb_device_id {
268194677Sthompsa
269194677Sthompsa	/* Hook for driver specific information */
270194677Sthompsa	unsigned long driver_info;
271194677Sthompsa
272194677Sthompsa	/* Used for product specific matches; the BCD range is inclusive */
273194677Sthompsa	uint16_t idVendor;
274194677Sthompsa	uint16_t idProduct;
275194677Sthompsa	uint16_t bcdDevice_lo;
276194677Sthompsa	uint16_t bcdDevice_hi;
277194677Sthompsa
278194677Sthompsa	/* Used for device class matches */
279194677Sthompsa	uint8_t	bDeviceClass;
280194677Sthompsa	uint8_t	bDeviceSubClass;
281194677Sthompsa	uint8_t	bDeviceProtocol;
282194677Sthompsa
283194677Sthompsa	/* Used for interface class matches */
284194677Sthompsa	uint8_t	bInterfaceClass;
285194677Sthompsa	uint8_t	bInterfaceSubClass;
286194677Sthompsa	uint8_t	bInterfaceProtocol;
287194677Sthompsa
288194677Sthompsa	/* Select which fields to match against */
289194677Sthompsa	uint8_t	match_flag_vendor:1;
290194677Sthompsa	uint8_t	match_flag_product:1;
291194677Sthompsa	uint8_t	match_flag_dev_lo:1;
292194677Sthompsa	uint8_t	match_flag_dev_hi:1;
293223538Shselasky
294194677Sthompsa	uint8_t	match_flag_dev_class:1;
295194677Sthompsa	uint8_t	match_flag_dev_subclass:1;
296194677Sthompsa	uint8_t	match_flag_dev_protocol:1;
297194677Sthompsa	uint8_t	match_flag_int_class:1;
298223538Shselasky
299194677Sthompsa	uint8_t	match_flag_int_subclass:1;
300194677Sthompsa	uint8_t	match_flag_int_protocol:1;
301223538Shselasky	uint8_t match_flag_unused:6;
302194677Sthompsa
303194677Sthompsa#if USB_HAVE_COMPAT_LINUX
304194677Sthompsa	/* which fields to match against */
305194677Sthompsa	uint16_t match_flags;
306194677Sthompsa#define	USB_DEVICE_ID_MATCH_VENDOR		0x0001
307194677Sthompsa#define	USB_DEVICE_ID_MATCH_PRODUCT		0x0002
308194677Sthompsa#define	USB_DEVICE_ID_MATCH_DEV_LO		0x0004
309194677Sthompsa#define	USB_DEVICE_ID_MATCH_DEV_HI		0x0008
310194677Sthompsa#define	USB_DEVICE_ID_MATCH_DEV_CLASS		0x0010
311194677Sthompsa#define	USB_DEVICE_ID_MATCH_DEV_SUBCLASS	0x0020
312194677Sthompsa#define	USB_DEVICE_ID_MATCH_DEV_PROTOCOL	0x0040
313194677Sthompsa#define	USB_DEVICE_ID_MATCH_INT_CLASS		0x0080
314194677Sthompsa#define	USB_DEVICE_ID_MATCH_INT_SUBCLASS	0x0100
315194677Sthompsa#define	USB_DEVICE_ID_MATCH_INT_PROTOCOL	0x0200
316194677Sthompsa#endif
317223538Shselasky} __aligned(32);
318194677Sthompsa
319223538Shselasky/* check that the size of the structure above is correct */
320223538Shselaskyextern char usb_device_id_assert[(sizeof(struct usb_device_id) == 32) ? 1 : -1];
321223538Shselasky
322194677Sthompsa#define	USB_VENDOR(vend)			\
323194677Sthompsa  .match_flag_vendor = 1, .idVendor = (vend)
324194677Sthompsa
325194677Sthompsa#define	USB_PRODUCT(prod)			\
326194677Sthompsa  .match_flag_product = 1, .idProduct = (prod)
327194677Sthompsa
328194677Sthompsa#define	USB_VP(vend,prod)			\
329194677Sthompsa  USB_VENDOR(vend), USB_PRODUCT(prod)
330194677Sthompsa
331194677Sthompsa#define	USB_VPI(vend,prod,info)			\
332194677Sthompsa  USB_VENDOR(vend), USB_PRODUCT(prod), USB_DRIVER_INFO(info)
333194677Sthompsa
334194677Sthompsa#define	USB_DEV_BCD_GTEQ(lo)	/* greater than or equal */ \
335194677Sthompsa  .match_flag_dev_lo = 1, .bcdDevice_lo = (lo)
336194677Sthompsa
337194677Sthompsa#define	USB_DEV_BCD_LTEQ(hi)	/* less than or equal */ \
338194677Sthompsa  .match_flag_dev_hi = 1, .bcdDevice_hi = (hi)
339194677Sthompsa
340194677Sthompsa#define	USB_DEV_CLASS(dc)			\
341194677Sthompsa  .match_flag_dev_class = 1, .bDeviceClass = (dc)
342194677Sthompsa
343194677Sthompsa#define	USB_DEV_SUBCLASS(dsc)			\
344194677Sthompsa  .match_flag_dev_subclass = 1, .bDeviceSubClass = (dsc)
345194677Sthompsa
346194677Sthompsa#define	USB_DEV_PROTOCOL(dp)			\
347194677Sthompsa  .match_flag_dev_protocol = 1, .bDeviceProtocol = (dp)
348194677Sthompsa
349194677Sthompsa#define	USB_IFACE_CLASS(ic)			\
350194677Sthompsa  .match_flag_int_class = 1, .bInterfaceClass = (ic)
351194677Sthompsa
352194677Sthompsa#define	USB_IFACE_SUBCLASS(isc)			\
353194677Sthompsa  .match_flag_int_subclass = 1, .bInterfaceSubClass = (isc)
354194677Sthompsa
355194677Sthompsa#define	USB_IFACE_PROTOCOL(ip)			\
356194677Sthompsa  .match_flag_int_protocol = 1, .bInterfaceProtocol = (ip)
357194677Sthompsa
358194677Sthompsa#define	USB_IF_CSI(class,subclass,info)			\
359194677Sthompsa  USB_IFACE_CLASS(class), USB_IFACE_SUBCLASS(subclass), USB_DRIVER_INFO(info)
360194677Sthompsa
361194677Sthompsa#define	USB_DRIVER_INFO(n)			\
362194677Sthompsa  .driver_info = (n)
363194677Sthompsa
364194677Sthompsa#define	USB_GET_DRIVER_INFO(did)		\
365194677Sthompsa  (did)->driver_info
366194677Sthompsa
367194677Sthompsa/*
368194677Sthompsa * The following structure keeps information that is used to match
369194677Sthompsa * against an array of "usb_device_id" elements.
370194677Sthompsa */
371194677Sthompsastruct usbd_lookup_info {
372194677Sthompsa	uint16_t idVendor;
373194677Sthompsa	uint16_t idProduct;
374194677Sthompsa	uint16_t bcdDevice;
375194677Sthompsa	uint8_t	bDeviceClass;
376194677Sthompsa	uint8_t	bDeviceSubClass;
377194677Sthompsa	uint8_t	bDeviceProtocol;
378194677Sthompsa	uint8_t	bInterfaceClass;
379194677Sthompsa	uint8_t	bInterfaceSubClass;
380194677Sthompsa	uint8_t	bInterfaceProtocol;
381194677Sthompsa	uint8_t	bIfaceIndex;
382194677Sthompsa	uint8_t	bIfaceNum;
383194677Sthompsa	uint8_t	bConfigIndex;
384194677Sthompsa	uint8_t	bConfigNum;
385194677Sthompsa};
386194677Sthompsa
387194677Sthompsa/* Structure used by probe and attach */
388194677Sthompsa
389194677Sthompsastruct usb_attach_arg {
390194677Sthompsa	struct usbd_lookup_info info;
391194677Sthompsa	device_t temp_dev;		/* for internal use */
392194677Sthompsa	unsigned long driver_info;	/* for internal use */
393194677Sthompsa	void *driver_ivar;
394194677Sthompsa	struct usb_device *device;	/* current device */
395194677Sthompsa	struct usb_interface *iface;	/* current interface */
396194677Sthompsa	enum usb_hc_mode usb_mode;	/* host or device mode */
397194677Sthompsa	uint8_t	port;
398200653Sthompsa	uint8_t dev_state;
399200653Sthompsa#define UAA_DEV_READY		0
400200653Sthompsa#define UAA_DEV_DISABLED	1
401200653Sthompsa#define UAA_DEV_EJECTING	2
402194677Sthompsa};
403194677Sthompsa
404194677Sthompsa/*
405194677Sthompsa * The following is a wrapper for the callout structure to ease
406194677Sthompsa * porting the code to other platforms.
407194677Sthompsa */
408194677Sthompsastruct usb_callout {
409194677Sthompsa	struct callout co;
410194677Sthompsa};
411194677Sthompsa#define	usb_callout_init_mtx(c,m,f) callout_init_mtx(&(c)->co,m,f)
412194677Sthompsa#define	usb_callout_reset(c,t,f,d) callout_reset(&(c)->co,t,f,d)
413194677Sthompsa#define	usb_callout_stop(c) callout_stop(&(c)->co)
414194677Sthompsa#define	usb_callout_drain(c) callout_drain(&(c)->co)
415194677Sthompsa#define	usb_callout_pending(c) callout_pending(&(c)->co)
416194677Sthompsa
417194677Sthompsa/* USB transfer states */
418194677Sthompsa
419194677Sthompsa#define	USB_ST_SETUP       0
420194677Sthompsa#define	USB_ST_TRANSFERRED 1
421194677Sthompsa#define	USB_ST_ERROR       2
422194677Sthompsa
423194677Sthompsa/* USB handle request states */
424194677Sthompsa#define	USB_HR_NOT_COMPLETE	0
425194677Sthompsa#define	USB_HR_COMPLETE_OK	1
426194677Sthompsa#define	USB_HR_COMPLETE_ERR	2
427194677Sthompsa
428194677Sthompsa/*
429194677Sthompsa * The following macro will return the current state of an USB
430194677Sthompsa * transfer like defined by the "USB_ST_XXX" enums.
431194677Sthompsa */
432194677Sthompsa#define	USB_GET_STATE(xfer) (usbd_xfer_state(xfer))
433194677Sthompsa
434194677Sthompsa/*
435194677Sthompsa * The following structure defines the USB process message header.
436194677Sthompsa */
437194677Sthompsastruct usb_proc_msg {
438194677Sthompsa	TAILQ_ENTRY(usb_proc_msg) pm_qentry;
439194677Sthompsa	usb_proc_callback_t *pm_callback;
440194677Sthompsa	usb_size_t pm_num;
441194677Sthompsa};
442194677Sthompsa
443194677Sthompsa#define	USB_FIFO_TX 0
444194677Sthompsa#define	USB_FIFO_RX 1
445194677Sthompsa
446194677Sthompsa/*
447194677Sthompsa * Locking note for the following functions.  All the
448194677Sthompsa * "usb_fifo_cmd_t" and "usb_fifo_filter_t" functions are called
449194677Sthompsa * locked. The others are called unlocked.
450194677Sthompsa */
451194677Sthompsastruct usb_fifo_methods {
452194677Sthompsa	usb_fifo_open_t *f_open;
453194677Sthompsa	usb_fifo_close_t *f_close;
454194677Sthompsa	usb_fifo_ioctl_t *f_ioctl;
455194677Sthompsa	/*
456194677Sthompsa	 * NOTE: The post-ioctl callback is called after the USB reference
457194677Sthompsa	 * gets locked in the IOCTL handler:
458194677Sthompsa	 */
459194677Sthompsa	usb_fifo_ioctl_t *f_ioctl_post;
460194677Sthompsa	usb_fifo_cmd_t *f_start_read;
461194677Sthompsa	usb_fifo_cmd_t *f_stop_read;
462194677Sthompsa	usb_fifo_cmd_t *f_start_write;
463194677Sthompsa	usb_fifo_cmd_t *f_stop_write;
464194677Sthompsa	usb_fifo_filter_t *f_filter_read;
465194677Sthompsa	usb_fifo_filter_t *f_filter_write;
466194677Sthompsa	const char *basename[4];
467194677Sthompsa	const char *postfix[4];
468194677Sthompsa};
469194677Sthompsa
470194677Sthompsastruct usb_fifo_sc {
471194677Sthompsa	struct usb_fifo *fp[2];
472224777Shselasky	struct usb_fs_privdata *dev;
473194677Sthompsa};
474194677Sthompsa
475194677Sthompsaconst char *usbd_errstr(usb_error_t error);
476194677Sthompsavoid	*usbd_find_descriptor(struct usb_device *udev, void *id,
477194677Sthompsa	    uint8_t iface_index, uint8_t type, uint8_t type_mask,
478194677Sthompsa	    uint8_t subtype, uint8_t subtype_mask);
479194677Sthompsastruct usb_config_descriptor *usbd_get_config_descriptor(
480194677Sthompsa	    struct usb_device *udev);
481194677Sthompsastruct usb_device_descriptor *usbd_get_device_descriptor(
482194677Sthompsa	    struct usb_device *udev);
483194677Sthompsastruct usb_interface *usbd_get_iface(struct usb_device *udev,
484194677Sthompsa	    uint8_t iface_index);
485194677Sthompsastruct usb_interface_descriptor *usbd_get_interface_descriptor(
486194677Sthompsa	    struct usb_interface *iface);
487194677Sthompsastruct usb_endpoint *usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
488194677Sthompsa		    const struct usb_config *setup);
489194677Sthompsastruct usb_endpoint *usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val);
490194677Sthompsausb_error_t	usbd_interface_count(struct usb_device *udev, uint8_t *count);
491194677Sthompsaenum usb_hc_mode usbd_get_mode(struct usb_device *udev);
492194677Sthompsaenum usb_dev_speed usbd_get_speed(struct usb_device *udev);
493194677Sthompsavoid	device_set_usb_desc(device_t dev);
494194677Sthompsavoid	usb_pause_mtx(struct mtx *mtx, int _ticks);
495214429Shselaskyusb_error_t	usbd_set_pnpinfo(struct usb_device *udev,
496214429Shselasky			uint8_t iface_index, const char *pnpinfo);
497225350Shselaskyusb_error_t	usbd_add_dynamic_quirk(struct usb_device *udev,
498225350Shselasky			uint16_t quirk);
499239214Shselaskyusb_error_t	usbd_set_endpoint_mode(struct usb_device *udev,
500239214Shselasky			struct usb_endpoint *ep, uint8_t ep_mode);
501239214Shselaskyuint8_t		usbd_get_endpoint_mode(struct usb_device *udev,
502239214Shselasky			struct usb_endpoint *ep);
503194677Sthompsa
504194677Sthompsaconst struct usb_device_id *usbd_lookup_id_by_info(
505194677Sthompsa	    const struct usb_device_id *id, usb_size_t sizeof_id,
506194677Sthompsa	    const struct usbd_lookup_info *info);
507194677Sthompsaint	usbd_lookup_id_by_uaa(const struct usb_device_id *id,
508194677Sthompsa	    usb_size_t sizeof_id, struct usb_attach_arg *uaa);
509194677Sthompsa
510194677Sthompsausb_error_t usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
511194677Sthompsa		    struct usb_device_request *req, void *data, uint16_t flags,
512194677Sthompsa		    uint16_t *actlen, usb_timeout_t timeout);
513194677Sthompsa#define	usbd_do_request(u,m,r,d) \
514194677Sthompsa  usbd_do_request_flags(u,m,r,d,0,NULL,USB_DEFAULT_TIMEOUT)
515194677Sthompsa
516194677Sthompsauint8_t	usbd_clear_stall_callback(struct usb_xfer *xfer1,
517194677Sthompsa	    struct usb_xfer *xfer2);
518194677Sthompsauint8_t	usbd_get_interface_altindex(struct usb_interface *iface);
519194677Sthompsausb_error_t usbd_set_alt_interface_index(struct usb_device *udev,
520194677Sthompsa	    uint8_t iface_index, uint8_t alt_index);
521194677Sthompsauint32_t usbd_get_isoc_fps(struct usb_device *udev);
522361912Shselaskyuint32_t usbd_get_max_frame_length(const struct usb_endpoint_descriptor *,
523361912Shselasky    const struct usb_endpoint_ss_comp_descriptor *,
524361912Shselasky    enum usb_dev_speed);
525194677Sthompsausb_error_t usbd_transfer_setup(struct usb_device *udev,
526194677Sthompsa	    const uint8_t *ifaces, struct usb_xfer **pxfer,
527194677Sthompsa	    const struct usb_config *setup_start, uint16_t n_setup,
528194677Sthompsa	    void *priv_sc, struct mtx *priv_mtx);
529194677Sthompsavoid	usbd_transfer_submit(struct usb_xfer *xfer);
530194677Sthompsavoid	usbd_transfer_clear_stall(struct usb_xfer *xfer);
531194677Sthompsavoid	usbd_transfer_drain(struct usb_xfer *xfer);
532194677Sthompsauint8_t	usbd_transfer_pending(struct usb_xfer *xfer);
533194677Sthompsavoid	usbd_transfer_start(struct usb_xfer *xfer);
534194677Sthompsavoid	usbd_transfer_stop(struct usb_xfer *xfer);
535194677Sthompsavoid	usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup);
536194677Sthompsavoid	usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max);
537194677Sthompsavoid	usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
538194677Sthompsa	    uint8_t parent_index);
539194677Sthompsauint8_t	usbd_get_bus_index(struct usb_device *udev);
540194677Sthompsauint8_t	usbd_get_device_index(struct usb_device *udev);
541194677Sthompsavoid	usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode);
542212135Sthompsauint8_t	usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode);
543194677Sthompsauint8_t	usbd_device_attached(struct usb_device *udev);
544194677Sthompsa
545220301Shselaskyusb_frlength_t
546220301Shselasky	usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex);
547194677Sthompsavoid	usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen,
548194677Sthompsa	    int *aframes, int *nframes);
549244500Shselaskystruct usb_page_cache *usbd_xfer_get_frame(struct usb_xfer *, usb_frcount_t);
550244500Shselaskyvoid	*usbd_xfer_get_frame_buffer(struct usb_xfer *, usb_frcount_t);
551194677Sthompsavoid	*usbd_xfer_softc(struct usb_xfer *xfer);
552194677Sthompsavoid	*usbd_xfer_get_priv(struct usb_xfer *xfer);
553194677Sthompsavoid	usbd_xfer_set_priv(struct usb_xfer *xfer, void *);
554194677Sthompsavoid	usbd_xfer_set_interval(struct usb_xfer *xfer, int);
555194677Sthompsauint8_t	usbd_xfer_state(struct usb_xfer *xfer);
556194677Sthompsavoid	usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
557194677Sthompsa	    void *ptr, usb_frlength_t len);
558194682Sthompsavoid	usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
559194677Sthompsa	    void **ptr, int *len);
560194677Sthompsavoid	usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
561194677Sthompsa	    usb_frcount_t frindex);
562194677Sthompsausb_frlength_t usbd_xfer_max_len(struct usb_xfer *xfer);
563194677Sthompsausb_frlength_t usbd_xfer_max_framelen(struct usb_xfer *xfer);
564194677Sthompsausb_frcount_t usbd_xfer_max_frames(struct usb_xfer *xfer);
565199059Sthompsauint8_t	usbd_xfer_get_fps_shift(struct usb_xfer *xfer);
566194682Sthompsausb_frlength_t usbd_xfer_frame_len(struct usb_xfer *xfer,
567194677Sthompsa	    usb_frcount_t frindex);
568194677Sthompsavoid	usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
569194677Sthompsa	    usb_frlength_t len);
570194677Sthompsavoid	usbd_xfer_set_timeout(struct usb_xfer *xfer, int timeout);
571194677Sthompsavoid	usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n);
572194677Sthompsavoid	usbd_xfer_set_stall(struct usb_xfer *xfer);
573194677Sthompsaint	usbd_xfer_is_stalled(struct usb_xfer *xfer);
574194677Sthompsavoid	usbd_xfer_set_flag(struct usb_xfer *xfer, int flag);
575194677Sthompsavoid	usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag);
576195121Sthompsauint16_t usbd_xfer_get_timestamp(struct usb_xfer *xfer);
577263643Shselaskyuint8_t usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer);
578194677Sthompsa
579194677Sthompsavoid	usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
580194677Sthompsa	    const void *ptr, usb_frlength_t len);
581194677Sthompsaint	usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset,
582194677Sthompsa	    const void *ptr, usb_frlength_t len);
583194677Sthompsavoid	usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
584194677Sthompsa	    void *ptr, usb_frlength_t len);
585194677Sthompsaint	usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset,
586194677Sthompsa	    void *ptr, usb_frlength_t len);
587194677Sthompsavoid	usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
588194677Sthompsa	    struct usb_page_search *res);
589194677Sthompsavoid	usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset,
590194677Sthompsa	    struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len);
591194677Sthompsavoid	usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
592194677Sthompsa	    usb_frlength_t len);
593222786Shselaskyvoid	usbd_start_re_enumerate(struct usb_device *udev);
594267347Shselaskyusb_error_t
595267347Shselasky	usbd_start_set_config(struct usb_device *, uint8_t);
596194677Sthompsa
597194677Sthompsaint	usb_fifo_attach(struct usb_device *udev, void *priv_sc,
598194677Sthompsa	    struct mtx *priv_mtx, struct usb_fifo_methods *pm,
599233774Shselasky	    struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
600194677Sthompsa	    uint8_t iface_index, uid_t uid, gid_t gid, int mode);
601194677Sthompsavoid	usb_fifo_detach(struct usb_fifo_sc *f_sc);
602194677Sthompsaint	usb_fifo_alloc_buffer(struct usb_fifo *f, uint32_t bufsize,
603194677Sthompsa	    uint16_t nbuf);
604194677Sthompsavoid	usb_fifo_free_buffer(struct usb_fifo *f);
605194677Sthompsauint32_t usb_fifo_put_bytes_max(struct usb_fifo *fifo);
606194677Sthompsavoid	usb_fifo_put_data(struct usb_fifo *fifo, struct usb_page_cache *pc,
607194677Sthompsa	    usb_frlength_t offset, usb_frlength_t len, uint8_t what);
608194677Sthompsavoid	usb_fifo_put_data_linear(struct usb_fifo *fifo, void *ptr,
609194677Sthompsa	    usb_size_t len, uint8_t what);
610194677Sthompsauint8_t	usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len);
611194677Sthompsavoid	usb_fifo_put_data_error(struct usb_fifo *fifo);
612194677Sthompsauint8_t	usb_fifo_get_data(struct usb_fifo *fifo, struct usb_page_cache *pc,
613194677Sthompsa	    usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
614194677Sthompsa	    uint8_t what);
615194677Sthompsauint8_t	usb_fifo_get_data_linear(struct usb_fifo *fifo, void *ptr,
616194677Sthompsa	    usb_size_t len, usb_size_t *actlen, uint8_t what);
617194677Sthompsauint8_t	usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr,
618194677Sthompsa	    usb_size_t *plen);
619194677Sthompsavoid	usb_fifo_reset(struct usb_fifo *f);
620194677Sthompsavoid	usb_fifo_wakeup(struct usb_fifo *f);
621194677Sthompsavoid	usb_fifo_get_data_error(struct usb_fifo *fifo);
622194677Sthompsavoid	*usb_fifo_softc(struct usb_fifo *fifo);
623195968Salfredvoid	usb_fifo_set_close_zlp(struct usb_fifo *, uint8_t);
624195968Salfredvoid	usb_fifo_set_write_defrag(struct usb_fifo *, uint8_t);
625195968Salfredvoid	usb_fifo_free(struct usb_fifo *f);
626194677Sthompsa#endif /* _KERNEL */
627194677Sthompsa#endif /* _USB_USBDI_H_ */
628