xref: /freebsd/sys/dev/usb/usb_device.h (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _USB_DEVICE_H_
28 #define	_USB_DEVICE_H_
29 
30 #ifndef USB_GLOBAL_INCLUDE_FILE
31 #include <dev/usb/usb_core.h>
32 #include <dev/usb/usb_busdma.h>
33 #include <dev/usb/usb_transfer.h>
34 #endif
35 
36 struct usb_bus_methods;
37 struct usb_config_descriptor;
38 struct usb_device;		/* linux compat */
39 struct usb_fs_privdata;
40 struct usb_hw_ep_profile;
41 struct usb_symlink;		/* UGEN */
42 
43 #define	USB_CTRL_XFER_MAX 2
44 
45 /* "usb_config_parse()" commands */
46 
47 #define	USB_CFG_ALLOC 0
48 #define	USB_CFG_FREE 1
49 #define	USB_CFG_INIT 2
50 
51 /* "usb_unconfigure()" flags */
52 
53 #define	USB_UNCFG_FLAG_NONE 0x00
54 #define	USB_UNCFG_FLAG_FREE_EP0	0x02		/* endpoint zero is freed */
55 
56 struct usb_udev_msg {
57 	struct usb_proc_msg hdr;
58 	struct usb_device *udev;
59 };
60 
61 /* The following four structures makes up a tree, where we have the
62  * leaf structure, "usb_host_endpoint", first, and the root structure,
63  * "usb_device", last. The four structures below mirror the structure
64  * of the USB descriptors belonging to an USB configuration. Please
65  * refer to the USB specification for a definition of "endpoints" and
66  * "interfaces".
67  */
68 struct usb_host_endpoint {
69 	struct usb_endpoint_descriptor desc;
70 	TAILQ_HEAD(, urb) bsd_urb_list;
71 	struct usb_xfer *bsd_xfer[2];
72 	uint8_t *extra;			/* Extra descriptors */
73 	usb_frlength_t fbsd_buf_size;
74 	uint16_t extralen;
75 	uint8_t	bsd_iface_index;
76 } __aligned(USB_HOST_ALIGN);
77 
78 struct usb_host_interface {
79 	struct usb_interface_descriptor desc;
80 	/* the following array has size "desc.bNumEndpoint" */
81 	struct usb_host_endpoint *endpoint;
82 	const char *string;		/* iInterface string, if present */
83 	uint8_t *extra;			/* Extra descriptors */
84 	uint16_t extralen;
85 	uint8_t	bsd_iface_index;
86 } __aligned(USB_HOST_ALIGN);
87 
88 /*
89  * The following structure defines the USB device flags.
90  */
91 struct usb_device_flags {
92 	enum usb_hc_mode usb_mode;	/* host or device mode */
93 	uint8_t	self_powered:1;		/* set if USB device is self powered */
94 	uint8_t	no_strings:1;		/* set if USB device does not support
95 					 * strings */
96 	uint8_t	remote_wakeup:1;	/* set if remote wakeup is enabled */
97 	uint8_t	uq_bus_powered:1;	/* set if BUS powered quirk is present */
98 
99 	/*
100 	 * NOTE: Although the flags below will reach the same value
101 	 * over time, but the instant values may differ, and
102 	 * consequently the flags cannot be merged into one!
103 	 */
104 	uint8_t peer_suspended:1;	/* set if peer is suspended */
105 	uint8_t self_suspended:1;	/* set if self is suspended */
106 };
107 
108 /*
109  * The following structure is used for power-save purposes. The data
110  * in this structure is protected by the USB BUS lock.
111  */
112 struct usb_power_save {
113 	usb_ticks_t last_xfer_time;	/* copy of "ticks" */
114 	usb_size_t type_refs[4];	/* transfer reference count */
115 	usb_size_t read_refs;		/* data read references */
116 	usb_size_t write_refs;		/* data write references */
117 };
118 
119 /*
120  * The following structure is used when trying to allocate hardware
121  * endpoints for an USB configuration in USB device side mode.
122  */
123 struct usb_hw_ep_scratch_sub {
124 	const struct usb_hw_ep_profile *pf;
125 	uint16_t max_frame_size;
126 	uint8_t	hw_endpoint_out;
127 	uint8_t	hw_endpoint_in;
128 	uint8_t	needs_ep_type;
129 	uint8_t	needs_in:1;
130 	uint8_t	needs_out:1;
131 };
132 
133 /*
134  * The following structure is used when trying to allocate hardware
135  * endpoints for an USB configuration in USB device side mode.
136  */
137 struct usb_hw_ep_scratch {
138 	struct usb_hw_ep_scratch_sub ep[USB_EP_MAX];
139 	struct usb_hw_ep_scratch_sub *ep_max;
140 	struct usb_config_descriptor *cd;
141 	struct usb_device *udev;
142 	const struct usb_bus_methods *methods;
143 	uint8_t	bmOutAlloc[(USB_EP_MAX + 15) / 16];
144 	uint8_t	bmInAlloc[(USB_EP_MAX + 15) / 16];
145 };
146 
147 /*
148  * The following structure is used when generating USB descriptors
149  * from USB templates.
150  */
151 struct usb_temp_setup {
152 	void   *buf;
153 	usb_size_t size;
154 	enum usb_dev_speed	usb_speed;
155 	uint8_t	self_powered;
156 	uint8_t	bNumEndpoints;
157 	uint8_t	bInterfaceNumber;
158 	uint8_t	bAlternateSetting;
159 	uint8_t	bConfigurationValue;
160 	usb_error_t err;
161 };
162 
163 /*
164  * The scratch area for USB devices. Access to this structure is
165  * protected by the control SX lock.
166  */
167 union usb_device_scratch {
168 	struct usb_hw_ep_scratch hw_ep_scratch[1];
169 	struct usb_temp_setup temp_setup[1];
170 	struct {
171 		struct usb_xfer dummy;
172 		struct usb_setup_params parm;
173 	} xfer_setup[1];
174 	uint8_t	data[255];
175 };
176 
177 /*
178  * The following structure defines an USB device. There exists one of
179  * these structures for every USB device.
180  */
181 struct usb_device {
182 	/* generic clear stall message */
183 	struct usb_udev_msg cs_msg[2];
184 	struct sx enum_sx;
185 	struct sx sr_sx;
186   	struct sx ctrl_sx;
187 	struct mtx device_mtx;
188 	struct cv ctrlreq_cv;
189 	struct cv ref_cv;
190 #if (USB_HAVE_FIXED_IFACE == 0)
191 	struct usb_interface *ifaces;
192 #else
193 	struct usb_interface ifaces[USB_IFACE_MAX];
194 #endif
195 	struct usb_endpoint ctrl_ep;	/* Control Endpoint 0 */
196 #if (USB_HAVE_FIXED_ENDPOINT == 0)
197 	struct usb_endpoint *endpoints;
198 #else
199 	struct usb_endpoint endpoints[USB_MAX_EP_UNITS];
200 #endif
201 	struct usb_power_save pwr_save;/* power save data */
202 	struct usb_bus *bus;		/* our USB BUS */
203 	device_t parent_dev;		/* parent device */
204 	struct usb_device *parent_hub;
205 	struct usb_device *parent_hs_hub;	/* high-speed parent HUB */
206 	struct usb_config_descriptor *cdesc;	/* full config descr */
207 	struct usb_hub *hub;		/* only if this is a hub */
208 	struct usb_xfer *ctrl_xfer[USB_CTRL_XFER_MAX];
209 	struct usb_temp_data *usb_template_ptr;
210 	struct usb_endpoint *ep_curr;	/* current clear stall endpoint */
211 #if USB_HAVE_UGEN
212 	struct usb_fifo *fifo[USB_FIFO_MAX];
213 	struct usb_symlink *ugen_symlink;	/* our generic symlink */
214 	struct usb_fs_privdata *ctrl_dev;	/* Control Endpoint 0 device node */
215 	LIST_HEAD(,usb_fs_privdata) pd_list;
216 	char	ugen_name[20];		/* name of ugenX.X device */
217 #endif
218 	usb_ticks_t plugtime;		/* copy of "ticks" */
219 
220 	enum usb_dev_state state;
221 	enum usb_dev_speed speed;
222 	uint16_t refcount;
223 #define	USB_DEV_REF_MAX 0xffff
224 
225 	uint16_t power;			/* mA the device uses */
226 	uint16_t langid;		/* language for strings */
227 	uint16_t autoQuirk[USB_MAX_AUTO_QUIRK];		/* dynamic quirks */
228 
229 	uint8_t	address;		/* device addess */
230 	uint8_t	device_index;		/* device index in "bus->devices" */
231 	uint8_t	controller_slot_id;	/* controller specific value */
232 	uint8_t next_config_index;	/* used by USB_RE_ENUM_SET_CONFIG */
233 	uint8_t	curr_config_index;	/* current configuration index */
234 	uint8_t	curr_config_no;		/* current configuration number */
235 	uint8_t	depth;			/* distance from root HUB */
236 	uint8_t	port_index;		/* parent HUB port index */
237 	uint8_t	port_no;		/* parent HUB port number */
238 	uint8_t	hs_hub_addr;		/* high-speed HUB address */
239 	uint8_t	hs_port_no;		/* high-speed HUB port number */
240 	uint8_t	driver_added_refcount;	/* our driver added generation count */
241 	uint8_t	power_mode;		/* see USB_POWER_XXX */
242 	uint8_t re_enumerate_wait;	/* set if re-enum. is in progress */
243 #define	USB_RE_ENUM_DONE	0
244 #define	USB_RE_ENUM_START	1
245 #define	USB_RE_ENUM_PWR_OFF	2
246 #define	USB_RE_ENUM_SET_CONFIG	3
247 	uint8_t ifaces_max;		/* number of interfaces present */
248 	uint8_t endpoints_max;		/* number of endpoints present */
249 
250 	/* the "flags" field is write-protected by "bus->mtx" */
251 
252 	struct usb_device_flags flags;
253 
254 	struct usb_endpoint_descriptor ctrl_ep_desc;	/* for endpoint 0 */
255 	struct usb_endpoint_ss_comp_descriptor ctrl_ep_comp_desc;	/* for endpoint 0 */
256 	struct usb_device_descriptor ddesc;	/* device descriptor */
257 
258 	char	*serial;		/* serial number, can be NULL */
259 	char	*manufacturer;		/* manufacturer string, can be NULL */
260 	char	*product;		/* product string, can be NULL */
261 
262 #if USB_HAVE_COMPAT_LINUX
263 	/* Linux compat */
264 	struct usb_device_descriptor descriptor;
265 	struct usb_host_endpoint ep0;
266 	struct usb_interface *linux_iface_start;
267 	struct usb_interface *linux_iface_end;
268 	struct usb_host_endpoint *linux_endpoint_start;
269 	struct usb_host_endpoint *linux_endpoint_end;
270 	uint16_t devnum;
271 #endif
272 
273 	uint32_t clear_stall_errors;	/* number of clear-stall failures */
274 
275 	union usb_device_scratch scratch;
276 
277 #if (USB_HAVE_FIXED_CONFIG != 0)
278 	uint32_t config_data[(USB_CONFIG_MAX + 3) / 4];
279 #endif
280 };
281 
282 /* globals */
283 
284 extern int usb_template;
285 
286 /* function prototypes */
287 
288 const char *usb_statestr(enum usb_dev_state state);
289 struct usb_device *usb_alloc_device(device_t parent_dev, struct usb_bus *bus,
290 		    struct usb_device *parent_hub, uint8_t depth,
291 		    uint8_t port_index, uint8_t port_no,
292 		    enum usb_dev_speed speed, enum usb_hc_mode mode);
293 #if USB_HAVE_UGEN
294 struct usb_fs_privdata *usb_make_dev(struct usb_device *, const char *,
295 		    int, int, int, uid_t, gid_t, int);
296 void	usb_destroy_dev(struct usb_fs_privdata *);
297 void	usb_destroy_dev_sync(struct usb_fs_privdata *);
298 #endif
299 usb_error_t	usb_probe_and_attach(struct usb_device *udev,
300 		    uint8_t iface_index);
301 void		usb_detach_device(struct usb_device *, uint8_t, uint8_t);
302 usb_error_t	usb_reset_iface_endpoints(struct usb_device *udev,
303 		    uint8_t iface_index);
304 usb_error_t	usbd_set_config_index(struct usb_device *udev, uint8_t index);
305 usb_error_t	usbd_set_endpoint_stall(struct usb_device *udev,
306 		    struct usb_endpoint *ep, uint8_t do_stall);
307 usb_error_t	usb_suspend_resume(struct usb_device *udev,
308 		    uint8_t do_suspend);
309 void	usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len);
310 void	usb_free_device(struct usb_device *, uint8_t);
311 void	usb_linux_free_device(struct usb_device *dev);
312 uint8_t	usb_peer_can_wakeup(struct usb_device *udev);
313 struct usb_endpoint *usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep);
314 void	usb_set_device_state(struct usb_device *, enum usb_dev_state);
315 enum usb_dev_state usb_get_device_state(struct usb_device *);
316 
317 uint8_t	usbd_enum_lock(struct usb_device *);
318 #if USB_HAVE_UGEN
319 uint8_t	usbd_enum_lock_sig(struct usb_device *);
320 #endif
321 void	usbd_enum_unlock(struct usb_device *);
322 void	usbd_sr_lock(struct usb_device *);
323 void	usbd_sr_unlock(struct usb_device *);
324 uint8_t	usbd_ctrl_lock(struct usb_device *);
325 void	usbd_ctrl_unlock(struct usb_device *);
326 uint8_t usbd_enum_is_locked(struct usb_device *);
327 
328 #if USB_HAVE_TT_SUPPORT
329 void	uhub_tt_buffer_reset_async_locked(struct usb_device *, struct usb_endpoint *);
330 #endif
331 
332 uint8_t uhub_count_active_host_ports(struct usb_device *, enum usb_dev_speed);
333 
334 #endif					/* _USB_DEVICE_H_ */
335