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