1184610Salfred/* $FreeBSD: stable/11/sys/dev/usb/usb_device.h 361208 2020-05-18 09:46:51Z hselasky $ */
2184610Salfred/*-
3356395Shselasky * Copyright (c) 2008-2019 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
27194230Sthompsa#ifndef _USB_DEVICE_H_
28194230Sthompsa#define	_USB_DEVICE_H_
29184610Salfred
30246616Shselasky#ifndef USB_GLOBAL_INCLUDE_FILE
31246616Shselasky#include <dev/usb/usb_core.h>
32246616Shselasky#include <dev/usb/usb_busdma.h>
33246616Shselasky#include <dev/usb/usb_transfer.h>
34246616Shselasky#endif
35246616Shselasky
36246616Shselaskystruct usb_bus_methods;
37246616Shselaskystruct usb_config_descriptor;
38190180Sthompsastruct usb_device;		/* linux compat */
39224777Shselaskystruct usb_fs_privdata;
40246616Shselaskystruct usb_hw_ep_profile;
41246616Shselaskystruct usb_symlink;		/* UGEN */
42184610Salfred
43207080Sthompsa#define	USB_CTRL_XFER_MAX 2
44184610Salfred
45239176Shselasky/* "usb_config_parse()" commands */
46190730Sthompsa
47190730Sthompsa#define	USB_CFG_ALLOC 0
48190730Sthompsa#define	USB_CFG_FREE 1
49190730Sthompsa#define	USB_CFG_INIT 2
50190730Sthompsa
51194228Sthompsa/* "usb_unconfigure()" flags */
52190730Sthompsa
53190730Sthompsa#define	USB_UNCFG_FLAG_NONE 0x00
54190730Sthompsa#define	USB_UNCFG_FLAG_FREE_EP0	0x02		/* endpoint zero is freed */
55190730Sthompsa
56260589Shselaskystruct usb_udev_msg {
57192984Sthompsa	struct usb_proc_msg hdr;
58192984Sthompsa	struct usb_device *udev;
59184610Salfred};
60184610Salfred
61192984Sthompsa/* The following four structures makes up a tree, where we have the
62192984Sthompsa * leaf structure, "usb_host_endpoint", first, and the root structure,
63192984Sthompsa * "usb_device", last. The four structures below mirror the structure
64192984Sthompsa * of the USB descriptors belonging to an USB configuration. Please
65192984Sthompsa * refer to the USB specification for a definition of "endpoints" and
66192984Sthompsa * "interfaces".
67192984Sthompsa */
68192984Sthompsastruct usb_host_endpoint {
69192984Sthompsa	struct usb_endpoint_descriptor desc;
70192984Sthompsa	TAILQ_HEAD(, urb) bsd_urb_list;
71192984Sthompsa	struct usb_xfer *bsd_xfer[2];
72192984Sthompsa	uint8_t *extra;			/* Extra descriptors */
73193045Sthompsa	usb_frlength_t fbsd_buf_size;
74192984Sthompsa	uint16_t extralen;
75192984Sthompsa	uint8_t	bsd_iface_index;
76192984Sthompsa} __aligned(USB_HOST_ALIGN);
77192984Sthompsa
78192984Sthompsastruct usb_host_interface {
79192984Sthompsa	struct usb_interface_descriptor desc;
80192984Sthompsa	/* the following array has size "desc.bNumEndpoint" */
81192984Sthompsa	struct usb_host_endpoint *endpoint;
82192984Sthompsa	const char *string;		/* iInterface string, if present */
83192984Sthompsa	uint8_t *extra;			/* Extra descriptors */
84192984Sthompsa	uint16_t extralen;
85192984Sthompsa	uint8_t	bsd_iface_index;
86192984Sthompsa} __aligned(USB_HOST_ALIGN);
87192984Sthompsa
88184610Salfred/*
89184610Salfred * The following structure defines the USB device flags.
90184610Salfred */
91192984Sthompsastruct usb_device_flags {
92192499Sthompsa	enum usb_hc_mode usb_mode;	/* host or device mode */
93184610Salfred	uint8_t	self_powered:1;		/* set if USB device is self powered */
94184610Salfred	uint8_t	no_strings:1;		/* set if USB device does not support
95184610Salfred					 * strings */
96184610Salfred	uint8_t	remote_wakeup:1;	/* set if remote wakeup is enabled */
97184610Salfred	uint8_t	uq_bus_powered:1;	/* set if BUS powered quirk is present */
98191824Sthompsa
99191824Sthompsa	/*
100191824Sthompsa	 * NOTE: Although the flags below will reach the same value
101191824Sthompsa	 * over time, but the instant values may differ, and
102191824Sthompsa	 * consequently the flags cannot be merged into one!
103191824Sthompsa	 */
104191824Sthompsa	uint8_t peer_suspended:1;	/* set if peer is suspended */
105191824Sthompsa	uint8_t self_suspended:1;	/* set if self is suspended */
106184610Salfred};
107184610Salfred
108184610Salfred/*
109186730Salfred * The following structure is used for power-save purposes. The data
110186730Salfred * in this structure is protected by the USB BUS lock.
111186730Salfred */
112192984Sthompsastruct usb_power_save {
113193045Sthompsa	usb_ticks_t last_xfer_time;	/* copy of "ticks" */
114193074Sthompsa	usb_size_t type_refs[4];	/* transfer reference count */
115193074Sthompsa	usb_size_t read_refs;		/* data read references */
116193074Sthompsa	usb_size_t write_refs;		/* data write references */
117186730Salfred};
118186730Salfred
119186730Salfred/*
120246616Shselasky * The following structure is used when trying to allocate hardware
121246616Shselasky * endpoints for an USB configuration in USB device side mode.
122246616Shselasky */
123246616Shselaskystruct usb_hw_ep_scratch_sub {
124246616Shselasky	const struct usb_hw_ep_profile *pf;
125246616Shselasky	uint16_t max_frame_size;
126246616Shselasky	uint8_t	hw_endpoint_out;
127246616Shselasky	uint8_t	hw_endpoint_in;
128246616Shselasky	uint8_t	needs_ep_type;
129246616Shselasky	uint8_t	needs_in:1;
130246616Shselasky	uint8_t	needs_out:1;
131246616Shselasky};
132246616Shselasky
133246616Shselasky/*
134246616Shselasky * The following structure is used when trying to allocate hardware
135246616Shselasky * endpoints for an USB configuration in USB device side mode.
136246616Shselasky */
137246616Shselaskystruct usb_hw_ep_scratch {
138246616Shselasky	struct usb_hw_ep_scratch_sub ep[USB_EP_MAX];
139246616Shselasky	struct usb_hw_ep_scratch_sub *ep_max;
140246616Shselasky	struct usb_config_descriptor *cd;
141246616Shselasky	struct usb_device *udev;
142259218Shselasky	const struct usb_bus_methods *methods;
143246616Shselasky	uint8_t	bmOutAlloc[(USB_EP_MAX + 15) / 16];
144246616Shselasky	uint8_t	bmInAlloc[(USB_EP_MAX + 15) / 16];
145246616Shselasky};
146246616Shselasky
147246616Shselasky/*
148246616Shselasky * The following structure is used when generating USB descriptors
149246616Shselasky * from USB templates.
150246616Shselasky */
151246616Shselaskystruct usb_temp_setup {
152246616Shselasky	void   *buf;
153246616Shselasky	usb_size_t size;
154246616Shselasky	enum usb_dev_speed	usb_speed;
155246616Shselasky	uint8_t	self_powered;
156246616Shselasky	uint8_t	bNumEndpoints;
157246616Shselasky	uint8_t	bInterfaceNumber;
158246616Shselasky	uint8_t	bAlternateSetting;
159246616Shselasky	uint8_t	bConfigurationValue;
160246616Shselasky	usb_error_t err;
161246616Shselasky};
162246616Shselasky
163246616Shselasky/*
164246616Shselasky * The scratch area for USB devices. Access to this structure is
165305733Shselasky * protected by the control SX lock.
166246616Shselasky */
167246616Shselaskyunion usb_device_scratch {
168246616Shselasky	struct usb_hw_ep_scratch hw_ep_scratch[1];
169246616Shselasky	struct usb_temp_setup temp_setup[1];
170246616Shselasky	struct {
171246616Shselasky		struct usb_xfer dummy;
172246616Shselasky		struct usb_setup_params parm;
173246616Shselasky	} xfer_setup[1];
174246616Shselasky	uint8_t	data[255];
175246616Shselasky};
176246616Shselasky
177246616Shselasky/*
178356395Shselasky * Helper structure to keep track of USB device statistics.
179356395Shselasky */
180356395Shselaskystruct usb_device_statistics {
181356395Shselasky	uint32_t uds_requests[4];
182356395Shselasky};
183356395Shselasky
184356395Shselasky/*
185184610Salfred * The following structure defines an USB device. There exists one of
186184610Salfred * these structures for every USB device.
187184610Salfred */
188192984Sthompsastruct usb_device {
189356395Shselasky	/* statistics */
190356395Shselasky	struct usb_device_statistics stats_err;
191356395Shselasky	struct usb_device_statistics stats_ok;
192356680Shselasky  	struct usb_device_statistics stats_cancelled;
193356395Shselasky
194260589Shselasky	/* generic clear stall message */
195260589Shselasky	struct usb_udev_msg cs_msg[2];
196207079Sthompsa	struct sx enum_sx;
197208008Sthompsa	struct sx sr_sx;
198305733Shselasky  	struct sx ctrl_sx;
199207079Sthompsa	struct mtx device_mtx;
200207079Sthompsa	struct cv ctrlreq_cv;
201207079Sthompsa	struct cv ref_cv;
202250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0)
203192984Sthompsa	struct usb_interface *ifaces;
204250204Shselasky#else
205250204Shselasky	struct usb_interface ifaces[USB_IFACE_MAX];
206250204Shselasky#endif
207207080Sthompsa	struct usb_endpoint ctrl_ep;	/* Control Endpoint 0 */
208250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0)
209193644Sthompsa	struct usb_endpoint *endpoints;
210250204Shselasky#else
211250204Shselasky	struct usb_endpoint endpoints[USB_MAX_EP_UNITS];
212250204Shselasky#endif
213192984Sthompsa	struct usb_power_save pwr_save;/* power save data */
214192984Sthompsa	struct usb_bus *bus;		/* our USB BUS */
215184610Salfred	device_t parent_dev;		/* parent device */
216192984Sthompsa	struct usb_device *parent_hub;
217192984Sthompsa	struct usb_device *parent_hs_hub;	/* high-speed parent HUB */
218192984Sthompsa	struct usb_config_descriptor *cdesc;	/* full config descr */
219192984Sthompsa	struct usb_hub *hub;		/* only if this is a hub */
220207080Sthompsa	struct usb_xfer *ctrl_xfer[USB_CTRL_XFER_MAX];
221194228Sthompsa	struct usb_temp_data *usb_template_ptr;
222193644Sthompsa	struct usb_endpoint *ep_curr;	/* current clear stall endpoint */
223190191Sthompsa#if USB_HAVE_UGEN
224192984Sthompsa	struct usb_fifo *fifo[USB_FIFO_MAX];
225192984Sthompsa	struct usb_symlink *ugen_symlink;	/* our generic symlink */
226224777Shselasky	struct usb_fs_privdata *ctrl_dev;	/* Control Endpoint 0 device node */
227192984Sthompsa	LIST_HEAD(,usb_fs_privdata) pd_list;
228190191Sthompsa	char	ugen_name[20];		/* name of ugenX.X device */
229190191Sthompsa#endif
230193045Sthompsa	usb_ticks_t plugtime;		/* copy of "ticks" */
231184610Salfred
232192500Sthompsa	enum usb_dev_state state;
233192500Sthompsa	enum usb_dev_speed speed;
234184610Salfred	uint16_t refcount;
235184610Salfred#define	USB_DEV_REF_MAX 0xffff
236184610Salfred
237184610Salfred	uint16_t power;			/* mA the device uses */
238184610Salfred	uint16_t langid;		/* language for strings */
239225350Shselasky	uint16_t autoQuirk[USB_MAX_AUTO_QUIRK];		/* dynamic quirks */
240184610Salfred
241184610Salfred	uint8_t	address;		/* device addess */
242184610Salfred	uint8_t	device_index;		/* device index in "bus->devices" */
243213435Shselasky	uint8_t	controller_slot_id;	/* controller specific value */
244267240Shselasky	uint8_t next_config_index;	/* used by USB_RE_ENUM_SET_CONFIG */
245184610Salfred	uint8_t	curr_config_index;	/* current configuration index */
246184610Salfred	uint8_t	curr_config_no;		/* current configuration number */
247184610Salfred	uint8_t	depth;			/* distance from root HUB */
248184610Salfred	uint8_t	port_index;		/* parent HUB port index */
249184610Salfred	uint8_t	port_no;		/* parent HUB port number */
250184610Salfred	uint8_t	hs_hub_addr;		/* high-speed HUB address */
251184610Salfred	uint8_t	hs_port_no;		/* high-speed HUB port number */
252184610Salfred	uint8_t	driver_added_refcount;	/* our driver added generation count */
253184610Salfred	uint8_t	power_mode;		/* see USB_POWER_XXX */
254213432Shselasky	uint8_t re_enumerate_wait;	/* set if re-enum. is in progress */
255257206Shselasky#define	USB_RE_ENUM_DONE	0
256257206Shselasky#define	USB_RE_ENUM_START	1
257257206Shselasky#define	USB_RE_ENUM_PWR_OFF	2
258267240Shselasky#define	USB_RE_ENUM_SET_CONFIG	3
259190730Sthompsa	uint8_t ifaces_max;		/* number of interfaces present */
260193644Sthompsa	uint8_t endpoints_max;		/* number of endpoints present */
261184610Salfred
262184610Salfred	/* the "flags" field is write-protected by "bus->mtx" */
263184610Salfred
264192984Sthompsa	struct usb_device_flags flags;
265184610Salfred
266207080Sthompsa	struct usb_endpoint_descriptor ctrl_ep_desc;	/* for endpoint 0 */
267213435Shselasky	struct usb_endpoint_ss_comp_descriptor ctrl_ep_comp_desc;	/* for endpoint 0 */
268192984Sthompsa	struct usb_device_descriptor ddesc;	/* device descriptor */
269184610Salfred
270213435Shselasky	char	*serial;		/* serial number, can be NULL */
271213435Shselasky	char	*manufacturer;		/* manufacturer string, can be NULL */
272213435Shselasky	char	*product;		/* product string, can be NULL */
273192984Sthompsa
274192984Sthompsa#if USB_HAVE_COMPAT_LINUX
275192984Sthompsa	/* Linux compat */
276192984Sthompsa	struct usb_device_descriptor descriptor;
277192984Sthompsa	struct usb_host_endpoint ep0;
278192984Sthompsa	struct usb_interface *linux_iface_start;
279192984Sthompsa	struct usb_interface *linux_iface_end;
280192984Sthompsa	struct usb_host_endpoint *linux_endpoint_start;
281192984Sthompsa	struct usb_host_endpoint *linux_endpoint_end;
282192984Sthompsa	uint16_t devnum;
283192984Sthompsa#endif
284222786Shselasky
285222786Shselasky	uint32_t clear_stall_errors;	/* number of clear-stall failures */
286246616Shselasky
287246616Shselasky	union usb_device_scratch scratch;
288250207Shselasky
289250207Shselasky#if (USB_HAVE_FIXED_CONFIG != 0)
290250207Shselasky	uint32_t config_data[(USB_CONFIG_MAX + 3) / 4];
291250207Shselasky#endif
292184610Salfred};
293184610Salfred
294188987Sthompsa/* globals */
295188987Sthompsa
296194228Sthompsaextern int usb_template;
297188987Sthompsa
298184610Salfred/* function prototypes */
299184610Salfred
300194677Sthompsaconst char *usb_statestr(enum usb_dev_state state);
301194228Sthompsastruct usb_device *usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
302192984Sthompsa		    struct usb_device *parent_hub, uint8_t depth,
303192500Sthompsa		    uint8_t port_index, uint8_t port_no,
304192500Sthompsa		    enum usb_dev_speed speed, enum usb_hc_mode mode);
305224777Shselasky#if USB_HAVE_UGEN
306224777Shselaskystruct usb_fs_privdata *usb_make_dev(struct usb_device *, const char *,
307224777Shselasky		    int, int, int, uid_t, gid_t, int);
308224777Shselaskyvoid	usb_destroy_dev(struct usb_fs_privdata *);
309277136Shselaskyvoid	usb_destroy_dev_sync(struct usb_fs_privdata *);
310224777Shselasky#endif
311194228Sthompsausb_error_t	usb_probe_and_attach(struct usb_device *udev,
312185948Sthompsa		    uint8_t iface_index);
313205036Sthompsavoid		usb_detach_device(struct usb_device *, uint8_t, uint8_t);
314194228Sthompsausb_error_t	usb_reset_iface_endpoints(struct usb_device *udev,
315185948Sthompsa		    uint8_t iface_index);
316194228Sthompsausb_error_t	usbd_set_config_index(struct usb_device *udev, uint8_t index);
317194228Sthompsausb_error_t	usbd_set_endpoint_stall(struct usb_device *udev,
318193644Sthompsa		    struct usb_endpoint *ep, uint8_t do_stall);
319194228Sthompsausb_error_t	usb_suspend_resume(struct usb_device *udev,
320185948Sthompsa		    uint8_t do_suspend);
321194228Sthompsavoid	usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len);
322194228Sthompsavoid	usb_free_device(struct usb_device *, uint8_t);
323184610Salfredvoid	usb_linux_free_device(struct usb_device *dev);
324194228Sthompsauint8_t	usb_peer_can_wakeup(struct usb_device *udev);
325194228Sthompsastruct usb_endpoint *usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep);
326213435Shselaskyvoid	usb_set_device_state(struct usb_device *, enum usb_dev_state);
327213435Shselaskyenum usb_dev_state usb_get_device_state(struct usb_device *);
328213435Shselasky
329361208Shselaskyvoid	usb_set_device_strings(struct usb_device *);
330361208Shselaskyvoid	usb_get_langid(struct usb_device *);
331361208Shselasky
332246616Shselaskyuint8_t	usbd_enum_lock(struct usb_device *);
333300667Shselasky#if USB_HAVE_UGEN
334300667Shselaskyuint8_t	usbd_enum_lock_sig(struct usb_device *);
335300667Shselasky#endif
336196498Salfredvoid	usbd_enum_unlock(struct usb_device *);
337208008Sthompsavoid	usbd_sr_lock(struct usb_device *);
338208008Sthompsavoid	usbd_sr_unlock(struct usb_device *);
339305733Shselaskyuint8_t	usbd_ctrl_lock(struct usb_device *);
340305733Shselaskyvoid	usbd_ctrl_unlock(struct usb_device *);
341196498Salfreduint8_t usbd_enum_is_locked(struct usb_device *);
342184610Salfred
343260589Shselasky#if USB_HAVE_TT_SUPPORT
344260589Shselaskyvoid	uhub_tt_buffer_reset_async_locked(struct usb_device *, struct usb_endpoint *);
345260589Shselasky#endif
346260589Shselasky
347260589Shselaskyuint8_t uhub_count_active_host_ports(struct usb_device *, enum usb_dev_speed);
348260589Shselasky
349194230Sthompsa#endif					/* _USB_DEVICE_H_ */
350