usb_device.h revision 267240
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_device.h 267240 2014-06-08 20:10:29Z 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
165246616Shselasky * protected by the enumeration 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;
186207079Sthompsa	struct mtx device_mtx;
187207079Sthompsa	struct cv ctrlreq_cv;
188207079Sthompsa	struct cv ref_cv;
189250204Shselasky#if (USB_HAVE_FIXED_IFACE == 0)
190192984Sthompsa	struct usb_interface *ifaces;
191250204Shselasky#else
192250204Shselasky	struct usb_interface ifaces[USB_IFACE_MAX];
193250204Shselasky#endif
194207080Sthompsa	struct usb_endpoint ctrl_ep;	/* Control Endpoint 0 */
195250204Shselasky#if (USB_HAVE_FIXED_ENDPOINT == 0)
196193644Sthompsa	struct usb_endpoint *endpoints;
197250204Shselasky#else
198250204Shselasky	struct usb_endpoint endpoints[USB_MAX_EP_UNITS];
199250204Shselasky#endif
200192984Sthompsa	struct usb_power_save pwr_save;/* power save data */
201192984Sthompsa	struct usb_bus *bus;		/* our USB BUS */
202184610Salfred	device_t parent_dev;		/* parent device */
203192984Sthompsa	struct usb_device *parent_hub;
204192984Sthompsa	struct usb_device *parent_hs_hub;	/* high-speed parent HUB */
205192984Sthompsa	struct usb_config_descriptor *cdesc;	/* full config descr */
206192984Sthompsa	struct usb_hub *hub;		/* only if this is a hub */
207207080Sthompsa	struct usb_xfer *ctrl_xfer[USB_CTRL_XFER_MAX];
208194228Sthompsa	struct usb_temp_data *usb_template_ptr;
209193644Sthompsa	struct usb_endpoint *ep_curr;	/* current clear stall endpoint */
210190191Sthompsa#if USB_HAVE_UGEN
211192984Sthompsa	struct usb_fifo *fifo[USB_FIFO_MAX];
212192984Sthompsa	struct usb_symlink *ugen_symlink;	/* our generic symlink */
213224777Shselasky	struct usb_fs_privdata *ctrl_dev;	/* Control Endpoint 0 device node */
214192984Sthompsa	LIST_HEAD(,usb_fs_privdata) pd_list;
215190191Sthompsa	char	ugen_name[20];		/* name of ugenX.X device */
216190191Sthompsa#endif
217193045Sthompsa	usb_ticks_t plugtime;		/* copy of "ticks" */
218184610Salfred
219192500Sthompsa	enum usb_dev_state state;
220192500Sthompsa	enum usb_dev_speed speed;
221184610Salfred	uint16_t refcount;
222184610Salfred#define	USB_DEV_REF_MAX 0xffff
223184610Salfred
224184610Salfred	uint16_t power;			/* mA the device uses */
225184610Salfred	uint16_t langid;		/* language for strings */
226225350Shselasky	uint16_t autoQuirk[USB_MAX_AUTO_QUIRK];		/* dynamic quirks */
227184610Salfred
228184610Salfred	uint8_t	address;		/* device addess */
229184610Salfred	uint8_t	device_index;		/* device index in "bus->devices" */
230213435Shselasky	uint8_t	controller_slot_id;	/* controller specific value */
231267240Shselasky	uint8_t next_config_index;	/* used by USB_RE_ENUM_SET_CONFIG */
232184610Salfred	uint8_t	curr_config_index;	/* current configuration index */
233184610Salfred	uint8_t	curr_config_no;		/* current configuration number */
234184610Salfred	uint8_t	depth;			/* distance from root HUB */
235184610Salfred	uint8_t	port_index;		/* parent HUB port index */
236184610Salfred	uint8_t	port_no;		/* parent HUB port number */
237184610Salfred	uint8_t	hs_hub_addr;		/* high-speed HUB address */
238184610Salfred	uint8_t	hs_port_no;		/* high-speed HUB port number */
239184610Salfred	uint8_t	driver_added_refcount;	/* our driver added generation count */
240184610Salfred	uint8_t	power_mode;		/* see USB_POWER_XXX */
241213432Shselasky	uint8_t re_enumerate_wait;	/* set if re-enum. is in progress */
242257206Shselasky#define	USB_RE_ENUM_DONE	0
243257206Shselasky#define	USB_RE_ENUM_START	1
244257206Shselasky#define	USB_RE_ENUM_PWR_OFF	2
245267240Shselasky#define	USB_RE_ENUM_SET_CONFIG	3
246190730Sthompsa	uint8_t ifaces_max;		/* number of interfaces present */
247193644Sthompsa	uint8_t endpoints_max;		/* number of endpoints present */
248184610Salfred
249184610Salfred	/* the "flags" field is write-protected by "bus->mtx" */
250184610Salfred
251192984Sthompsa	struct usb_device_flags flags;
252184610Salfred
253207080Sthompsa	struct usb_endpoint_descriptor ctrl_ep_desc;	/* for endpoint 0 */
254213435Shselasky	struct usb_endpoint_ss_comp_descriptor ctrl_ep_comp_desc;	/* for endpoint 0 */
255192984Sthompsa	struct usb_device_descriptor ddesc;	/* device descriptor */
256184610Salfred
257213435Shselasky	char	*serial;		/* serial number, can be NULL */
258213435Shselasky	char	*manufacturer;		/* manufacturer string, can be NULL */
259213435Shselasky	char	*product;		/* product string, can be NULL */
260192984Sthompsa
261192984Sthompsa#if USB_HAVE_COMPAT_LINUX
262192984Sthompsa	/* Linux compat */
263192984Sthompsa	struct usb_device_descriptor descriptor;
264192984Sthompsa	struct usb_host_endpoint ep0;
265192984Sthompsa	struct usb_interface *linux_iface_start;
266192984Sthompsa	struct usb_interface *linux_iface_end;
267192984Sthompsa	struct usb_host_endpoint *linux_endpoint_start;
268192984Sthompsa	struct usb_host_endpoint *linux_endpoint_end;
269192984Sthompsa	uint16_t devnum;
270192984Sthompsa#endif
271222786Shselasky
272222786Shselasky	uint32_t clear_stall_errors;	/* number of clear-stall failures */
273246616Shselasky
274246616Shselasky	union usb_device_scratch scratch;
275250207Shselasky
276250207Shselasky#if (USB_HAVE_FIXED_CONFIG != 0)
277250207Shselasky	uint32_t config_data[(USB_CONFIG_MAX + 3) / 4];
278250207Shselasky#endif
279184610Salfred};
280184610Salfred
281188987Sthompsa/* globals */
282188987Sthompsa
283194228Sthompsaextern int usb_template;
284188987Sthompsa
285184610Salfred/* function prototypes */
286184610Salfred
287194677Sthompsaconst char *usb_statestr(enum usb_dev_state state);
288194228Sthompsastruct usb_device *usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
289192984Sthompsa		    struct usb_device *parent_hub, uint8_t depth,
290192500Sthompsa		    uint8_t port_index, uint8_t port_no,
291192500Sthompsa		    enum usb_dev_speed speed, enum usb_hc_mode mode);
292224777Shselasky#if USB_HAVE_UGEN
293224777Shselaskystruct usb_fs_privdata *usb_make_dev(struct usb_device *, const char *,
294224777Shselasky		    int, int, int, uid_t, gid_t, int);
295224777Shselaskyvoid	usb_destroy_dev(struct usb_fs_privdata *);
296224777Shselasky#endif
297194228Sthompsausb_error_t	usb_probe_and_attach(struct usb_device *udev,
298185948Sthompsa		    uint8_t iface_index);
299205036Sthompsavoid		usb_detach_device(struct usb_device *, uint8_t, uint8_t);
300194228Sthompsausb_error_t	usb_reset_iface_endpoints(struct usb_device *udev,
301185948Sthompsa		    uint8_t iface_index);
302194228Sthompsausb_error_t	usbd_set_config_index(struct usb_device *udev, uint8_t index);
303194228Sthompsausb_error_t	usbd_set_endpoint_stall(struct usb_device *udev,
304193644Sthompsa		    struct usb_endpoint *ep, uint8_t do_stall);
305194228Sthompsausb_error_t	usb_suspend_resume(struct usb_device *udev,
306185948Sthompsa		    uint8_t do_suspend);
307194228Sthompsavoid	usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len);
308194228Sthompsavoid	usb_free_device(struct usb_device *, uint8_t);
309184610Salfredvoid	usb_linux_free_device(struct usb_device *dev);
310194228Sthompsauint8_t	usb_peer_can_wakeup(struct usb_device *udev);
311194228Sthompsastruct usb_endpoint *usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep);
312213435Shselaskyvoid	usb_set_device_state(struct usb_device *, enum usb_dev_state);
313213435Shselaskyenum usb_dev_state usb_get_device_state(struct usb_device *);
314213435Shselasky
315246616Shselaskyuint8_t	usbd_enum_lock(struct usb_device *);
316196498Salfredvoid	usbd_enum_unlock(struct usb_device *);
317208008Sthompsavoid	usbd_sr_lock(struct usb_device *);
318208008Sthompsavoid	usbd_sr_unlock(struct usb_device *);
319196498Salfreduint8_t usbd_enum_is_locked(struct usb_device *);
320184610Salfred
321260589Shselasky#if USB_HAVE_TT_SUPPORT
322260589Shselaskyvoid	uhub_tt_buffer_reset_async_locked(struct usb_device *, struct usb_endpoint *);
323260589Shselasky#endif
324260589Shselasky
325260589Shselaskyuint8_t uhub_count_active_host_ports(struct usb_device *, enum usb_dev_speed);
326260589Shselasky
327194230Sthompsa#endif					/* _USB_DEVICE_H_ */
328