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