1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009 Andrew Thompson
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28#ifndef _USB_USBDI_H_
29#define _USB_USBDI_H_
30
31struct usb_fifo;
32struct usb_xfer;
33struct usb_device;
34struct usb_attach_arg;
35struct usb_interface;
36struct usb_endpoint;
37struct usb_page_cache;
38struct usb_page_search;
39struct usb_process;
40struct usb_proc_msg;
41struct usb_mbuf;
42struct usb_fs_privdata;
43struct mbuf;
44
45typedef enum {	/* keep in sync with usb_errstr_table */
46	USB_ERR_NORMAL_COMPLETION = 0,
47	USB_ERR_PENDING_REQUESTS,	/* 1 */
48	USB_ERR_NOT_STARTED,		/* 2 */
49	USB_ERR_INVAL,			/* 3 */
50	USB_ERR_NOMEM,			/* 4 */
51	USB_ERR_CANCELLED,		/* 5 */
52	USB_ERR_BAD_ADDRESS,		/* 6 */
53	USB_ERR_BAD_BUFSIZE,		/* 7 */
54	USB_ERR_BAD_FLAG,		/* 8 */
55	USB_ERR_NO_CALLBACK,		/* 9 */
56	USB_ERR_IN_USE,			/* 10 */
57	USB_ERR_NO_ADDR,		/* 11 */
58	USB_ERR_NO_PIPE,		/* 12 */
59	USB_ERR_ZERO_NFRAMES,		/* 13 */
60	USB_ERR_ZERO_MAXP,		/* 14 */
61	USB_ERR_SET_ADDR_FAILED,	/* 15 */
62	USB_ERR_NO_POWER,		/* 16 */
63	USB_ERR_TOO_DEEP,		/* 17 */
64	USB_ERR_IOERROR,		/* 18 */
65	USB_ERR_NOT_CONFIGURED,		/* 19 */
66	USB_ERR_TIMEOUT,		/* 20 */
67	USB_ERR_SHORT_XFER,		/* 21 */
68	USB_ERR_STALLED,		/* 22 */
69	USB_ERR_INTERRUPTED,		/* 23 */
70	USB_ERR_DMA_LOAD_FAILED,	/* 24 */
71	USB_ERR_BAD_CONTEXT,		/* 25 */
72	USB_ERR_NO_ROOT_HUB,		/* 26 */
73	USB_ERR_NO_INTR_THREAD,		/* 27 */
74	USB_ERR_NOT_LOCKED,		/* 28 */
75	USB_ERR_MAX
76} usb_error_t;
77
78/*
79 * Flags for transfers
80 */
81#define	USB_FORCE_SHORT_XFER	0x0001	/* force a short transmit last */
82#define	USB_SHORT_XFER_OK	0x0004	/* allow short reads */
83#define	USB_DELAY_STATUS_STAGE	0x0010	/* insert delay before STATUS stage */
84#define	USB_USER_DATA_PTR	0x0020	/* internal flag */
85#define	USB_MULTI_SHORT_OK	0x0040	/* allow multiple short frames */
86#define	USB_MANUAL_STATUS	0x0080	/* manual ctrl status */
87
88#define	USB_NO_TIMEOUT 0
89#define	USB_DEFAULT_TIMEOUT 5000	/* 5000 ms = 5 seconds */
90
91#if defined(_KERNEL)
92/* typedefs */
93
94typedef void (usb_callback_t)(struct usb_xfer *, usb_error_t);
95typedef void (usb_proc_callback_t)(struct usb_proc_msg *);
96typedef usb_error_t (usb_handle_req_t)(struct usb_device *,
97    struct usb_device_request *, const void **, uint16_t *);
98
99typedef int (usb_fifo_open_t)(struct usb_fifo *fifo, int fflags);
100typedef void (usb_fifo_close_t)(struct usb_fifo *fifo, int fflags);
101typedef int (usb_fifo_ioctl_t)(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags);
102typedef void (usb_fifo_cmd_t)(struct usb_fifo *fifo);
103typedef void (usb_fifo_filter_t)(struct usb_fifo *fifo, struct usb_mbuf *m);
104
105
106/* USB events */
107#ifndef USB_GLOBAL_INCLUDE_FILE
108#include <sys/eventhandler.h>
109#endif
110typedef void (*usb_dev_configured_t)(void *, struct usb_device *,
111    struct usb_attach_arg *);
112EVENTHANDLER_DECLARE(usb_dev_configured, usb_dev_configured_t);
113
114/*
115 * The following macros are used used to convert milliseconds into
116 * HZ. We use 1024 instead of 1000 milliseconds per second to save a
117 * full division.
118 */
119#define	USB_MS_HZ 1024
120
121#define	USB_MS_TO_TICKS(ms) \
122  (((uint32_t)((((uint32_t)(ms)) * ((uint32_t)(hz))) + USB_MS_HZ - 1)) / USB_MS_HZ)
123
124/*
125 * Common queue structure for USB transfers.
126 */
127struct usb_xfer_queue {
128	TAILQ_HEAD(, usb_xfer) head;
129	struct usb_xfer *curr;		/* current USB transfer processed */
130	void    (*command) (struct usb_xfer_queue *pq);
131	uint8_t	recurse_1:1;
132	uint8_t	recurse_2:1;
133	uint8_t	recurse_3:1;
134	uint8_t	reserved:5;
135};
136
137/*
138 * The following structure defines an USB endpoint
139 * USB endpoint.
140 */
141struct usb_endpoint {
142	/* queue of USB transfers */
143	struct usb_xfer_queue endpoint_q[USB_MAX_EP_STREAMS];
144
145	struct usb_endpoint_descriptor *edesc;
146	struct usb_endpoint_ss_comp_descriptor *ecomp;
147	const struct usb_pipe_methods *methods;	/* set by HC driver */
148
149	uint16_t isoc_next;
150
151	uint8_t	toggle_next:1;		/* next data toggle value */
152	uint8_t	is_stalled:1;		/* set if endpoint is stalled */
153	uint8_t	is_synced:1;		/* set if we a synchronised */
154	uint8_t	unused:5;
155	uint8_t	iface_index;		/* not used by "default endpoint" */
156
157	uint8_t refcount_alloc;		/* allocation refcount */
158	uint8_t refcount_bw;		/* bandwidth refcount */
159#define	USB_EP_REF_MAX 0x3f
160
161	/* High-Speed resource allocation (valid if "refcount_bw" > 0) */
162
163	uint8_t	usb_smask;		/* USB start mask */
164	uint8_t	usb_cmask;		/* USB complete mask */
165	uint8_t	usb_uframe;		/* USB microframe */
166
167	/* USB endpoint mode, see USB_EP_MODE_XXX */
168
169	uint8_t ep_mode;
170};
171
172/*
173 * The following structure defines an USB interface.
174 */
175struct usb_interface {
176	struct usb_interface_descriptor *idesc;
177	device_t subdev;
178	/* Total number of alternate settings, from 1 to 256 */
179	uint16_t num_altsetting;
180	/* Current alternate interface index, from 0 to 255 */
181	uint8_t	alt_index;
182	uint8_t	parent_iface_index;
183
184	/* Linux compat */
185	struct usb_host_interface *altsetting;
186	struct usb_host_interface *cur_altsetting;
187	struct usb_device *linux_udev;
188	void   *bsd_priv_sc;		/* device specific information */
189	char   *pnpinfo;		/* additional PnP-info for this interface */
190	uint8_t	bsd_iface_index;
191};
192
193/*
194 * The following structure defines a set of USB transfer flags.
195 */
196struct usb_xfer_flags {
197	uint8_t	force_short_xfer:1;	/* force a short transmit transfer
198					 * last */
199	uint8_t	short_xfer_ok:1;	/* allow short receive transfers */
200	uint8_t	short_frames_ok:1;	/* allow short frames */
201	uint8_t	pipe_bof:1;		/* block pipe on failure */
202	uint8_t	proxy_buffer:1;		/* makes buffer size a factor of
203					 * "max_frame_size" */
204	uint8_t	ext_buffer:1;		/* uses external DMA buffer */
205	uint8_t	manual_status:1;	/* non automatic status stage on
206					 * control transfers */
207	uint8_t	no_pipe_ok:1;		/* set if "USB_ERR_NO_PIPE" error can
208					 * be ignored */
209	uint8_t	stall_pipe:1;		/* set if the endpoint belonging to
210					 * this USB transfer should be stalled
211					 * before starting this transfer! */
212	uint8_t pre_scale_frames:1;	/* "usb_config->frames" is
213					 * assumed to give the
214					 * buffering time in
215					 * milliseconds and is
216					 * converted into the nearest
217					 * number of frames when the
218					 * USB transfer is setup. This
219					 * option only has effect for
220					 * ISOCHRONOUS transfers.
221					 */
222};
223
224/*
225 * The following structure define an USB configuration, that basically
226 * is used when setting up an USB transfer.
227 */
228struct usb_config {
229	usb_callback_t *callback;	/* USB transfer callback */
230	usb_frlength_t bufsize;	/* total pipe buffer size in bytes */
231	usb_frcount_t frames;		/* maximum number of USB frames */
232	usb_timeout_t interval;	/* interval in milliseconds */
233#define	USB_DEFAULT_INTERVAL	0
234	usb_timeout_t timeout;		/* transfer timeout in milliseconds */
235	struct usb_xfer_flags flags;	/* transfer flags */
236	usb_stream_t stream_id;		/* USB3.0 specific */
237	enum usb_hc_mode usb_mode;	/* host or device mode */
238	uint8_t	type;			/* pipe type */
239	uint8_t	endpoint;		/* pipe number */
240	uint8_t	direction;		/* pipe direction */
241	uint8_t	ep_index;		/* pipe index match to use */
242	uint8_t	if_index;		/* "ifaces" index to use */
243};
244
245/*
246 * Use these macro when defining USB device ID arrays if you want to
247 * have your driver module automatically loaded in host, device or
248 * both modes respectively:
249 */
250#if USB_HAVE_ID_SECTION
251#define	STRUCT_USB_HOST_ID \
252    struct usb_device_id __section("usb_host_id")
253#define	STRUCT_USB_DEVICE_ID \
254    struct usb_device_id __section("usb_device_id")
255#define	STRUCT_USB_DUAL_ID \
256    struct usb_device_id __section("usb_dual_id")
257#else
258#define	STRUCT_USB_HOST_ID \
259    struct usb_device_id
260#define	STRUCT_USB_DEVICE_ID \
261    struct usb_device_id
262#define	STRUCT_USB_DUAL_ID \
263    struct usb_device_id
264#endif			/* USB_HAVE_ID_SECTION */
265
266/*
267 * The following structure is used when looking up an USB driver for
268 * an USB device. It is inspired by the Linux structure called
269 * "usb_device_id".
270 */
271struct usb_device_id {
272
273	/* Select which fields to match against */
274#if BYTE_ORDER == LITTLE_ENDIAN
275	uint16_t
276		match_flag_vendor:1,
277		match_flag_product:1,
278		match_flag_dev_lo:1,
279		match_flag_dev_hi:1,
280
281		match_flag_dev_class:1,
282		match_flag_dev_subclass:1,
283		match_flag_dev_protocol:1,
284		match_flag_int_class:1,
285
286		match_flag_int_subclass:1,
287		match_flag_int_protocol:1,
288		match_flag_unused:6;
289#else
290	uint16_t
291		match_flag_unused:6,
292		match_flag_int_protocol:1,
293		match_flag_int_subclass:1,
294
295		match_flag_int_class:1,
296		match_flag_dev_protocol:1,
297		match_flag_dev_subclass:1,
298		match_flag_dev_class:1,
299
300		match_flag_dev_hi:1,
301		match_flag_dev_lo:1,
302		match_flag_product:1,
303		match_flag_vendor:1;
304#endif
305
306	/* Used for product specific matches; the BCD range is inclusive */
307	uint16_t idVendor;
308	uint16_t idProduct;
309	uint16_t bcdDevice_lo;
310	uint16_t bcdDevice_hi;
311
312	/* Used for device class matches */
313	uint8_t	bDeviceClass;
314	uint8_t	bDeviceSubClass;
315	uint8_t	bDeviceProtocol;
316
317	/* Used for interface class matches */
318	uint8_t	bInterfaceClass;
319	uint8_t	bInterfaceSubClass;
320	uint8_t	bInterfaceProtocol;
321
322#if USB_HAVE_COMPAT_LINUX
323	/* which fields to match against */
324	uint16_t match_flags;
325#define	USB_DEVICE_ID_MATCH_VENDOR		0x0001
326#define	USB_DEVICE_ID_MATCH_PRODUCT		0x0002
327#define	USB_DEVICE_ID_MATCH_DEV_LO		0x0004
328#define	USB_DEVICE_ID_MATCH_DEV_HI		0x0008
329#define	USB_DEVICE_ID_MATCH_DEV_CLASS		0x0010
330#define	USB_DEVICE_ID_MATCH_DEV_SUBCLASS	0x0020
331#define	USB_DEVICE_ID_MATCH_DEV_PROTOCOL	0x0040
332#define	USB_DEVICE_ID_MATCH_INT_CLASS		0x0080
333#define	USB_DEVICE_ID_MATCH_INT_SUBCLASS	0x0100
334#define	USB_DEVICE_ID_MATCH_INT_PROTOCOL	0x0200
335#endif
336
337	/* Hook for driver specific information */
338	unsigned long driver_info;
339} __aligned(32);
340
341#define USB_STD_PNP_INFO "M16:mask;U16:vendor;U16:product;L16:release;G16:release;" \
342	"U8:devclass;U8:devsubclass;U8:devproto;" \
343	"U8:intclass;U8:intsubclass;U8:intprotocol;"
344#define USB_STD_PNP_HOST_INFO USB_STD_PNP_INFO "T:mode=host;"
345#define USB_STD_PNP_DEVICE_INFO USB_STD_PNP_INFO "T:mode=device;"
346#define USB_PNP_HOST_INFO(table)					\
347	MODULE_PNP_INFO(USB_STD_PNP_HOST_INFO, uhub, table, table,	\
348	    sizeof(table) / sizeof(table[0]))
349#define USB_PNP_DEVICE_INFO(table)					\
350	MODULE_PNP_INFO(USB_STD_PNP_DEVICE_INFO, uhub, table, table,	\
351	    sizeof(table) / sizeof(table[0]))
352#define USB_PNP_DUAL_INFO(table)					\
353	MODULE_PNP_INFO(USB_STD_PNP_INFO, uhub, table, table,		\
354	    sizeof(table) / sizeof(table[0]))
355
356/* check that the size of the structure above is correct */
357extern char usb_device_id_assert[(sizeof(struct usb_device_id) == 32) ? 1 : -1];
358
359#define	USB_VENDOR(vend)			\
360  .match_flag_vendor = 1, .idVendor = (vend)
361
362#define	USB_PRODUCT(prod)			\
363  .match_flag_product = 1, .idProduct = (prod)
364
365#define	USB_VP(vend,prod)			\
366  USB_VENDOR(vend), USB_PRODUCT(prod)
367
368#define	USB_VPI(vend,prod,info)			\
369  USB_VENDOR(vend), USB_PRODUCT(prod), USB_DRIVER_INFO(info)
370
371#define	USB_DEV_BCD_GTEQ(lo)	/* greater than or equal */ \
372  .match_flag_dev_lo = 1, .bcdDevice_lo = (lo)
373
374#define	USB_DEV_BCD_LTEQ(hi)	/* less than or equal */ \
375  .match_flag_dev_hi = 1, .bcdDevice_hi = (hi)
376
377#define	USB_DEV_CLASS(dc)			\
378  .match_flag_dev_class = 1, .bDeviceClass = (dc)
379
380#define	USB_DEV_SUBCLASS(dsc)			\
381  .match_flag_dev_subclass = 1, .bDeviceSubClass = (dsc)
382
383#define	USB_DEV_PROTOCOL(dp)			\
384  .match_flag_dev_protocol = 1, .bDeviceProtocol = (dp)
385
386#define	USB_IFACE_CLASS(ic)			\
387  .match_flag_int_class = 1, .bInterfaceClass = (ic)
388
389#define	USB_IFACE_SUBCLASS(isc)			\
390  .match_flag_int_subclass = 1, .bInterfaceSubClass = (isc)
391
392#define	USB_IFACE_PROTOCOL(ip)			\
393  .match_flag_int_protocol = 1, .bInterfaceProtocol = (ip)
394
395#define	USB_IF_CSI(class,subclass,info)			\
396  USB_IFACE_CLASS(class), USB_IFACE_SUBCLASS(subclass), USB_DRIVER_INFO(info)
397
398#define	USB_DRIVER_INFO(n)			\
399  .driver_info = (n)
400
401#define	USB_GET_DRIVER_INFO(did)		\
402  (did)->driver_info
403
404/*
405 * The following structure keeps information that is used to match
406 * against an array of "usb_device_id" elements.
407 */
408struct usbd_lookup_info {
409	uint16_t idVendor;
410	uint16_t idProduct;
411	uint16_t bcdDevice;
412	uint8_t	bDeviceClass;
413	uint8_t	bDeviceSubClass;
414	uint8_t	bDeviceProtocol;
415	uint8_t	bInterfaceClass;
416	uint8_t	bInterfaceSubClass;
417	uint8_t	bInterfaceProtocol;
418	uint8_t	bIfaceIndex;
419	uint8_t	bIfaceNum;
420	uint8_t	bConfigIndex;
421	uint8_t	bConfigNum;
422};
423
424/* Structure used by probe and attach */
425
426struct usb_attach_arg {
427	struct usbd_lookup_info info;
428	device_t temp_dev;		/* for internal use */
429	unsigned long driver_info;	/* for internal use */
430	void *driver_ivar;
431	struct usb_device *device;	/* current device */
432	struct usb_interface *iface;	/* current interface */
433	enum usb_hc_mode usb_mode;	/* host or device mode */
434	uint8_t	port;
435	uint8_t dev_state;
436#define UAA_DEV_READY		0
437#define UAA_DEV_DISABLED	1
438#define UAA_DEV_EJECTING	2
439};
440
441/*
442 * General purpose locking wrappers to ease supporting
443 * USB polled mode:
444 */
445#ifdef INVARIANTS
446#define	USB_MTX_ASSERT(_m, _t) do {		\
447	if (!USB_IN_POLLING_MODE_FUNC())	\
448		mtx_assert(_m, _t);		\
449} while (0)
450#else
451#define	USB_MTX_ASSERT(_m, _t) do { } while (0)
452#endif
453
454#define	USB_MTX_LOCK(_m) do {			\
455	if (!USB_IN_POLLING_MODE_FUNC())	\
456		mtx_lock(_m);			\
457} while (0)
458
459#define	USB_MTX_UNLOCK(_m) do {			\
460	if (!USB_IN_POLLING_MODE_FUNC())	\
461		mtx_unlock(_m);			\
462} while (0)
463
464#define	USB_MTX_LOCK_SPIN(_m) do {		\
465	if (!USB_IN_POLLING_MODE_FUNC())	\
466		mtx_lock_spin(_m);		\
467} while (0)
468
469#define	USB_MTX_UNLOCK_SPIN(_m) do {		\
470	if (!USB_IN_POLLING_MODE_FUNC())	\
471		mtx_unlock_spin(_m);		\
472} while (0)
473
474/*
475 * The following is a wrapper for the callout structure to ease
476 * porting the code to other platforms.
477 */
478struct usb_callout {
479	struct callout co;
480};
481#define	usb_callout_init_mtx(c,m,f) callout_init_mtx(&(c)->co,m,f)
482#define	usb_callout_reset(c,...) do {			\
483	if (!USB_IN_POLLING_MODE_FUNC())		\
484		callout_reset(&(c)->co, __VA_ARGS__);	\
485} while (0)
486#define	usb_callout_reset_sbt(c,...) do {			\
487	if (!USB_IN_POLLING_MODE_FUNC())			\
488		callout_reset_sbt(&(c)->co, __VA_ARGS__);	\
489} while (0)
490#define	usb_callout_stop(c) do {			\
491	if (!USB_IN_POLLING_MODE_FUNC()) {		\
492		callout_stop(&(c)->co);			\
493	} else {					\
494		/*					\
495		 * Cannot stop callout when		\
496		 * polling. Set dummy callback		\
497		 * function instead:			\
498		 */					\
499		(c)->co.c_func = &usbd_dummy_timeout;	\
500	}						\
501} while (0)
502#define	usb_callout_drain(c) callout_drain(&(c)->co)
503#define	usb_callout_pending(c) callout_pending(&(c)->co)
504
505/* USB transfer states */
506
507#define	USB_ST_SETUP       0
508#define	USB_ST_TRANSFERRED 1
509#define	USB_ST_ERROR       2
510
511/* USB handle request states */
512#define	USB_HR_NOT_COMPLETE	0
513#define	USB_HR_COMPLETE_OK	1
514#define	USB_HR_COMPLETE_ERR	2
515
516/*
517 * The following macro will return the current state of an USB
518 * transfer like defined by the "USB_ST_XXX" enums.
519 */
520#define	USB_GET_STATE(xfer) (usbd_xfer_state(xfer))
521
522/*
523 * The following structure defines the USB process message header.
524 */
525struct usb_proc_msg {
526	TAILQ_ENTRY(usb_proc_msg) pm_qentry;
527	usb_proc_callback_t *pm_callback;
528	usb_size_t pm_num;
529};
530
531#define	USB_FIFO_TX 0
532#define	USB_FIFO_RX 1
533
534/*
535 * Locking note for the following functions.  All the
536 * "usb_fifo_cmd_t" and "usb_fifo_filter_t" functions are called
537 * locked. The others are called unlocked.
538 */
539struct usb_fifo_methods {
540	usb_fifo_open_t *f_open;
541	usb_fifo_close_t *f_close;
542	usb_fifo_ioctl_t *f_ioctl;
543	/*
544	 * NOTE: The post-ioctl callback is called after the USB reference
545	 * gets locked in the IOCTL handler:
546	 */
547	usb_fifo_ioctl_t *f_ioctl_post;
548	usb_fifo_cmd_t *f_start_read;
549	usb_fifo_cmd_t *f_stop_read;
550	usb_fifo_cmd_t *f_start_write;
551	usb_fifo_cmd_t *f_stop_write;
552	usb_fifo_filter_t *f_filter_read;
553	usb_fifo_filter_t *f_filter_write;
554	const char *basename[4];
555	const char *postfix[4];
556};
557
558struct usb_fifo_sc {
559	struct usb_fifo *fp[2];
560	struct usb_fs_privdata *dev;
561};
562
563const char *usbd_errstr(usb_error_t error);
564void	*usbd_find_descriptor(struct usb_device *udev, void *id,
565	    uint8_t iface_index, uint8_t type, uint8_t type_mask,
566	    uint8_t subtype, uint8_t subtype_mask);
567struct usb_config_descriptor *usbd_get_config_descriptor(
568	    struct usb_device *udev);
569struct usb_device_descriptor *usbd_get_device_descriptor(
570	    struct usb_device *udev);
571struct usb_interface *usbd_get_iface(struct usb_device *udev,
572	    uint8_t iface_index);
573struct usb_interface_descriptor *usbd_get_interface_descriptor(
574	    struct usb_interface *iface);
575struct usb_endpoint *usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index,
576		    const struct usb_config *setup);
577struct usb_endpoint *usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val);
578usb_error_t	usbd_interface_count(struct usb_device *udev, uint8_t *count);
579enum usb_hc_mode usbd_get_mode(struct usb_device *udev);
580enum usb_dev_speed usbd_get_speed(struct usb_device *udev);
581void	device_set_usb_desc(device_t dev);
582void	usb_pause_mtx(struct mtx *mtx, int _ticks);
583usb_error_t	usbd_set_pnpinfo(struct usb_device *udev,
584			uint8_t iface_index, const char *pnpinfo);
585usb_error_t	usbd_add_dynamic_quirk(struct usb_device *udev,
586			uint16_t quirk);
587usb_error_t	usbd_set_endpoint_mode(struct usb_device *udev,
588			struct usb_endpoint *ep, uint8_t ep_mode);
589uint8_t		usbd_get_endpoint_mode(struct usb_device *udev,
590			struct usb_endpoint *ep);
591
592const struct usb_device_id *usbd_lookup_id_by_info(
593	    const struct usb_device_id *id, usb_size_t sizeof_id,
594	    const struct usbd_lookup_info *info);
595int	usbd_lookup_id_by_uaa(const struct usb_device_id *id,
596	    usb_size_t sizeof_id, struct usb_attach_arg *uaa);
597
598usb_error_t usbd_do_request_flags(struct usb_device *udev, struct mtx *mtx,
599		    struct usb_device_request *req, void *data, uint16_t flags,
600		    uint16_t *actlen, usb_timeout_t timeout);
601#define	usbd_do_request(u,m,r,d) \
602  usbd_do_request_flags(u,m,r,d,0,NULL,USB_DEFAULT_TIMEOUT)
603
604uint8_t	usbd_clear_stall_callback(struct usb_xfer *xfer1,
605	    struct usb_xfer *xfer2);
606uint8_t	usbd_get_interface_altindex(struct usb_interface *iface);
607usb_error_t usbd_set_alt_interface_index(struct usb_device *udev,
608	    uint8_t iface_index, uint8_t alt_index);
609uint32_t usbd_get_isoc_fps(struct usb_device *udev);
610uint32_t usbd_get_max_frame_length(const struct usb_endpoint_descriptor *,
611    const struct usb_endpoint_ss_comp_descriptor *,
612    enum usb_dev_speed);
613usb_error_t usbd_transfer_setup(struct usb_device *udev,
614	    const uint8_t *ifaces, struct usb_xfer **pxfer,
615	    const struct usb_config *setup_start, uint16_t n_setup,
616	    void *priv_sc, struct mtx *priv_mtx);
617void	usbd_transfer_submit(struct usb_xfer *xfer);
618void	usbd_transfer_clear_stall(struct usb_xfer *xfer);
619void	usbd_transfer_drain(struct usb_xfer *xfer);
620uint8_t	usbd_transfer_pending(struct usb_xfer *xfer);
621void	usbd_transfer_start(struct usb_xfer *xfer);
622void	usbd_transfer_stop(struct usb_xfer *xfer);
623void	usbd_transfer_unsetup(struct usb_xfer **pxfer, uint16_t n_setup);
624void	usbd_transfer_poll(struct usb_xfer **ppxfer, uint16_t max);
625void	usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index,
626	    uint8_t parent_index);
627uint8_t	usbd_get_bus_index(struct usb_device *udev);
628uint8_t	usbd_get_device_index(struct usb_device *udev);
629void	usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode);
630uint8_t	usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode);
631uint8_t	usbd_device_attached(struct usb_device *udev);
632
633usb_frlength_t
634	usbd_xfer_old_frame_length(struct usb_xfer *xfer, usb_frcount_t frindex);
635void	usbd_xfer_status(struct usb_xfer *xfer, int *actlen, int *sumlen,
636	    int *aframes, int *nframes);
637struct usb_page_cache *usbd_xfer_get_frame(struct usb_xfer *, usb_frcount_t);
638void	*usbd_xfer_get_frame_buffer(struct usb_xfer *, usb_frcount_t);
639void	*usbd_xfer_softc(struct usb_xfer *xfer);
640void	*usbd_xfer_get_priv(struct usb_xfer *xfer);
641void	usbd_xfer_set_priv(struct usb_xfer *xfer, void *);
642void	usbd_xfer_set_interval(struct usb_xfer *xfer, int);
643uint8_t	usbd_xfer_state(struct usb_xfer *xfer);
644void	usbd_xfer_set_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
645	    void *ptr, usb_frlength_t len);
646void	usbd_xfer_frame_data(struct usb_xfer *xfer, usb_frcount_t frindex,
647	    void **ptr, int *len);
648void	usbd_xfer_set_frame_offset(struct usb_xfer *xfer, usb_frlength_t offset,
649	    usb_frcount_t frindex);
650usb_frlength_t usbd_xfer_max_len(struct usb_xfer *xfer);
651usb_frlength_t usbd_xfer_max_framelen(struct usb_xfer *xfer);
652usb_frcount_t usbd_xfer_max_frames(struct usb_xfer *xfer);
653uint8_t	usbd_xfer_get_fps_shift(struct usb_xfer *xfer);
654usb_frlength_t usbd_xfer_frame_len(struct usb_xfer *xfer,
655	    usb_frcount_t frindex);
656void	usbd_xfer_set_frame_len(struct usb_xfer *xfer, usb_frcount_t frindex,
657	    usb_frlength_t len);
658void	usbd_xfer_set_timeout(struct usb_xfer *xfer, int timeout);
659void	usbd_xfer_set_frames(struct usb_xfer *xfer, usb_frcount_t n);
660void	usbd_xfer_set_stall(struct usb_xfer *xfer);
661int	usbd_xfer_is_stalled(struct usb_xfer *xfer);
662void	usbd_xfer_set_flag(struct usb_xfer *xfer, int flag);
663void	usbd_xfer_clr_flag(struct usb_xfer *xfer, int flag);
664uint16_t usbd_xfer_get_timestamp(struct usb_xfer *xfer);
665uint8_t usbd_xfer_maxp_was_clamped(struct usb_xfer *xfer);
666
667void	usbd_copy_in(struct usb_page_cache *cache, usb_frlength_t offset,
668	    const void *ptr, usb_frlength_t len);
669int	usbd_copy_in_user(struct usb_page_cache *cache, usb_frlength_t offset,
670	    const void *ptr, usb_frlength_t len);
671void	usbd_copy_out(struct usb_page_cache *cache, usb_frlength_t offset,
672	    void *ptr, usb_frlength_t len);
673int	usbd_copy_out_user(struct usb_page_cache *cache, usb_frlength_t offset,
674	    void *ptr, usb_frlength_t len);
675void	usbd_get_page(struct usb_page_cache *pc, usb_frlength_t offset,
676	    struct usb_page_search *res);
677void	usbd_m_copy_in(struct usb_page_cache *cache, usb_frlength_t dst_offset,
678	    struct mbuf *m, usb_size_t src_offset, usb_frlength_t src_len);
679void	usbd_frame_zero(struct usb_page_cache *cache, usb_frlength_t offset,
680	    usb_frlength_t len);
681void	usbd_start_re_enumerate(struct usb_device *udev);
682usb_error_t
683	usbd_start_set_config(struct usb_device *, uint8_t);
684int	usbd_in_polling_mode(void);
685void	usbd_dummy_timeout(void *);
686
687int	usb_fifo_attach(struct usb_device *udev, void *priv_sc,
688	    struct mtx *priv_mtx, struct usb_fifo_methods *pm,
689	    struct usb_fifo_sc *f_sc, uint16_t unit, int16_t subunit,
690	    uint8_t iface_index, uid_t uid, gid_t gid, int mode);
691void	usb_fifo_detach(struct usb_fifo_sc *f_sc);
692int	usb_fifo_alloc_buffer(struct usb_fifo *f, uint32_t bufsize,
693	    uint16_t nbuf);
694void	usb_fifo_free_buffer(struct usb_fifo *f);
695uint32_t usb_fifo_put_bytes_max(struct usb_fifo *fifo);
696void	usb_fifo_put_data(struct usb_fifo *fifo, struct usb_page_cache *pc,
697	    usb_frlength_t offset, usb_frlength_t len, uint8_t what);
698void	usb_fifo_put_data_linear(struct usb_fifo *fifo, void *ptr,
699	    usb_size_t len, uint8_t what);
700uint8_t	usb_fifo_put_data_buffer(struct usb_fifo *f, void *ptr, usb_size_t len);
701void	usb_fifo_put_data_error(struct usb_fifo *fifo);
702uint8_t	usb_fifo_get_data(struct usb_fifo *fifo, struct usb_page_cache *pc,
703	    usb_frlength_t offset, usb_frlength_t len, usb_frlength_t *actlen,
704	    uint8_t what);
705uint8_t	usb_fifo_get_data_linear(struct usb_fifo *fifo, void *ptr,
706	    usb_size_t len, usb_size_t *actlen, uint8_t what);
707uint8_t	usb_fifo_get_data_buffer(struct usb_fifo *f, void **pptr,
708	    usb_size_t *plen);
709void	usb_fifo_reset(struct usb_fifo *f);
710void	usb_fifo_wakeup(struct usb_fifo *f);
711void	usb_fifo_get_data_error(struct usb_fifo *fifo);
712void	*usb_fifo_softc(struct usb_fifo *fifo);
713void	usb_fifo_set_close_zlp(struct usb_fifo *, uint8_t);
714void	usb_fifo_set_write_defrag(struct usb_fifo *, uint8_t);
715void	usb_fifo_free(struct usb_fifo *f);
716#endif /* _KERNEL */
717#endif /* _USB_USBDI_H_ */
718