usb_device.h revision 305733
1184610Salfred/* $FreeBSD: stable/11/sys/dev/usb/usb_device.h 305733 2016-09-12 10:14:30Z hselasky $ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
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/*
178184610Salfred * The following structure defines an USB device. There exists one of
179184610Salfred * these structures for every USB device.
180184610Salfred */
181192984Sthompsastruct usb_device {
182260589Shselasky	/* generic clear stall message */
183260589Shselasky	struct usb_udev_msg cs_msg[2];
184207079Sthompsa	struct sx enum_sx;
185208008Sthompsa	struct sx sr_sx;
186305733Shselasky  	struct sx ctrl_sx;
187207079Sthompsa	struct mtx device_mtx;
188207079Sthompsa	struct cv ctrlreq_cv;
189207079Sthompsa	struct cv ref_cv;
190250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0)
191192984Sthompsa	struct usb_interface *ifaces;
192250204Shselasky#else
193250204Shselasky	struct usb_interface ifaces[USB_IFACE_MAX];
194250204Shselasky#endif
195207080Sthompsa	struct usb_endpoint ctrl_ep;	/* Control Endpoint 0 */
196250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0)
197193644Sthompsa	struct usb_endpoint *endpoints;
198250204Shselasky#else
199250204Shselasky	struct usb_endpoint endpoints[USB_MAX_EP_UNITS];
200250204Shselasky#endif
201192984Sthompsa	struct usb_power_save pwr_save;/* power save data */
202192984Sthompsa	struct usb_bus *bus;		/* our USB BUS */
203184610Salfred	device_t parent_dev;		/* parent device */
204192984Sthompsa	struct usb_device *parent_hub;
205192984Sthompsa	struct usb_device *parent_hs_hub;	/* high-speed parent HUB */
206192984Sthompsa	struct usb_config_descriptor *cdesc;	/* full config descr */
207192984Sthompsa	struct usb_hub *hub;		/* only if this is a hub */
208207080Sthompsa	struct usb_xfer *ctrl_xfer[USB_CTRL_XFER_MAX];
209194228Sthompsa	struct usb_temp_data *usb_template_ptr;
210193644Sthompsa	struct usb_endpoint *ep_curr;	/* current clear stall endpoint */
211190191Sthompsa#if USB_HAVE_UGEN
212192984Sthompsa	struct usb_fifo *fifo[USB_FIFO_MAX];
213192984Sthompsa	struct usb_symlink *ugen_symlink;	/* our generic symlink */
214224777Shselasky	struct usb_fs_privdata *ctrl_dev;	/* Control Endpoint 0 device node */
215192984Sthompsa	LIST_HEAD(,usb_fs_privdata) pd_list;
216190191Sthompsa	char	ugen_name[20];		/* name of ugenX.X device */
217190191Sthompsa#endif
218193045Sthompsa	usb_ticks_t plugtime;		/* copy of "ticks" */
219184610Salfred
220192500Sthompsa	enum usb_dev_state state;
221192500Sthompsa	enum usb_dev_speed speed;
222184610Salfred	uint16_t refcount;
223184610Salfred#define	USB_DEV_REF_MAX 0xffff
224184610Salfred
225184610Salfred	uint16_t power;			/* mA the device uses */
226184610Salfred	uint16_t langid;		/* language for strings */
227225350Shselasky	uint16_t autoQuirk[USB_MAX_AUTO_QUIRK];		/* dynamic quirks */
228184610Salfred
229184610Salfred	uint8_t	address;		/* device addess */
230184610Salfred	uint8_t	device_index;		/* device index in "bus->devices" */
231213435Shselasky	uint8_t	controller_slot_id;	/* controller specific value */
232267240Shselasky	uint8_t next_config_index;	/* used by USB_RE_ENUM_SET_CONFIG */
233184610Salfred	uint8_t	curr_config_index;	/* current configuration index */
234184610Salfred	uint8_t	curr_config_no;		/* current configuration number */
235184610Salfred	uint8_t	depth;			/* distance from root HUB */
236184610Salfred	uint8_t	port_index;		/* parent HUB port index */
237184610Salfred	uint8_t	port_no;		/* parent HUB port number */
238184610Salfred	uint8_t	hs_hub_addr;		/* high-speed HUB address */
239184610Salfred	uint8_t	hs_port_no;		/* high-speed HUB port number */
240184610Salfred	uint8_t	driver_added_refcount;	/* our driver added generation count */
241184610Salfred	uint8_t	power_mode;		/* see USB_POWER_XXX */
242213432Shselasky	uint8_t re_enumerate_wait;	/* set if re-enum. is in progress */
243257206Shselasky#define	USB_RE_ENUM_DONE	0
244257206Shselasky#define	USB_RE_ENUM_START	1
245257206Shselasky#define	USB_RE_ENUM_PWR_OFF	2
246267240Shselasky#define	USB_RE_ENUM_SET_CONFIG	3
247190730Sthompsa	uint8_t ifaces_max;		/* number of interfaces present */
248193644Sthompsa	uint8_t endpoints_max;		/* number of endpoints present */
249184610Salfred
250184610Salfred	/* the "flags" field is write-protected by "bus->mtx" */
251184610Salfred
252192984Sthompsa	struct usb_device_flags flags;
253184610Salfred
254207080Sthompsa	struct usb_endpoint_descriptor ctrl_ep_desc;	/* for endpoint 0 */
255213435Shselasky	struct usb_endpoint_ss_comp_descriptor ctrl_ep_comp_desc;	/* for endpoint 0 */
256192984Sthompsa	struct usb_device_descriptor ddesc;	/* device descriptor */
257184610Salfred
258213435Shselasky	char	*serial;		/* serial number, can be NULL */
259213435Shselasky	char	*manufacturer;		/* manufacturer string, can be NULL */
260213435Shselasky	char	*product;		/* product string, can be NULL */
261192984Sthompsa
262192984Sthompsa#if USB_HAVE_COMPAT_LINUX
263192984Sthompsa	/* Linux compat */
264192984Sthompsa	struct usb_device_descriptor descriptor;
265192984Sthompsa	struct usb_host_endpoint ep0;
266192984Sthompsa	struct usb_interface *linux_iface_start;
267192984Sthompsa	struct usb_interface *linux_iface_end;
268192984Sthompsa	struct usb_host_endpoint *linux_endpoint_start;
269192984Sthompsa	struct usb_host_endpoint *linux_endpoint_end;
270192984Sthompsa	uint16_t devnum;
271192984Sthompsa#endif
272222786Shselasky
273222786Shselasky	uint32_t clear_stall_errors;	/* number of clear-stall failures */
274246616Shselasky
275246616Shselasky	union usb_device_scratch scratch;
276250207Shselasky
277250207Shselasky#if (USB_HAVE_FIXED_CONFIG != 0)
278250207Shselasky	uint32_t config_data[(USB_CONFIG_MAX + 3) / 4];
279250207Shselasky#endif
280184610Salfred};
281184610Salfred
282188987Sthompsa/* globals */
283188987Sthompsa
284194228Sthompsaextern int usb_template;
285188987Sthompsa
286184610Salfred/* function prototypes */
287184610Salfred
288194677Sthompsaconst char *usb_statestr(enum usb_dev_state state);
289194228Sthompsastruct usb_device *usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
290192984Sthompsa		    struct usb_device *parent_hub, uint8_t depth,
291192500Sthompsa		    uint8_t port_index, uint8_t port_no,
292192500Sthompsa		    enum usb_dev_speed speed, enum usb_hc_mode mode);
293224777Shselasky#if USB_HAVE_UGEN
294224777Shselaskystruct usb_fs_privdata *usb_make_dev(struct usb_device *, const char *,
295224777Shselasky		    int, int, int, uid_t, gid_t, int);
296224777Shselaskyvoid	usb_destroy_dev(struct usb_fs_privdata *);
297277136Shselaskyvoid	usb_destroy_dev_sync(struct usb_fs_privdata *);
298224777Shselasky#endif
299194228Sthompsausb_error_t	usb_probe_and_attach(struct usb_device *udev,
300185948Sthompsa		    uint8_t iface_index);
301205036Sthompsavoid		usb_detach_device(struct usb_device *, uint8_t, uint8_t);
302194228Sthompsausb_error_t	usb_reset_iface_endpoints(struct usb_device *udev,
303185948Sthompsa		    uint8_t iface_index);
304194228Sthompsausb_error_t	usbd_set_config_index(struct usb_device *udev, uint8_t index);
305194228Sthompsausb_error_t	usbd_set_endpoint_stall(struct usb_device *udev,
306193644Sthompsa		    struct usb_endpoint *ep, uint8_t do_stall);
307194228Sthompsausb_error_t	usb_suspend_resume(struct usb_device *udev,
308185948Sthompsa		    uint8_t do_suspend);
309194228Sthompsavoid	usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len);
310194228Sthompsavoid	usb_free_device(struct usb_device *, uint8_t);
311184610Salfredvoid	usb_linux_free_device(struct usb_device *dev);
312194228Sthompsauint8_t	usb_peer_can_wakeup(struct usb_device *udev);
313194228Sthompsastruct usb_endpoint *usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep);
314213435Shselaskyvoid	usb_set_device_state(struct usb_device *, enum usb_dev_state);
315213435Shselaskyenum usb_dev_state usb_get_device_state(struct usb_device *);
316213435Shselasky
317246616Shselaskyuint8_t	usbd_enum_lock(struct usb_device *);
318300667Shselasky#if USB_HAVE_UGEN
319300667Shselaskyuint8_t	usbd_enum_lock_sig(struct usb_device *);
320300667Shselasky#endif
321196498Salfredvoid	usbd_enum_unlock(struct usb_device *);
322208008Sthompsavoid	usbd_sr_lock(struct usb_device *);
323208008Sthompsavoid	usbd_sr_unlock(struct usb_device *);
324305733Shselaskyuint8_t	usbd_ctrl_lock(struct usb_device *);
325305733Shselaskyvoid	usbd_ctrl_unlock(struct usb_device *);
326196498Salfreduint8_t usbd_enum_is_locked(struct usb_device *);
327184610Salfred
328260589Shselasky#if USB_HAVE_TT_SUPPORT
329260589Shselaskyvoid	uhub_tt_buffer_reset_async_locked(struct usb_device *, struct usb_endpoint *);
330260589Shselasky#endif
331260589Shselasky
332260589Shselaskyuint8_t uhub_count_active_host_ports(struct usb_device *, enum usb_dev_speed);
333260589Shselasky
334194230Sthompsa#endif					/* _USB_DEVICE_H_ */
335