xref: /freebsd/sys/dev/usb/usb_device.h (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 _USB2_DEVICE_H_
28 #define	_USB2_DEVICE_H_
29 
30 struct usb_symlink;		/* UGEN */
31 struct usb_device;		/* linux compat */
32 
33 #define	USB_DEFAULT_XFER_MAX 2
34 
35 /* "usb2_parse_config()" commands */
36 
37 #define	USB_CFG_ALLOC 0
38 #define	USB_CFG_FREE 1
39 #define	USB_CFG_INIT 2
40 
41 /* "usb2_unconfigure()" flags */
42 
43 #define	USB_UNCFG_FLAG_NONE 0x00
44 #define	USB_UNCFG_FLAG_FREE_SUBDEV 0x01		/* subdevices are freed */
45 #define	USB_UNCFG_FLAG_FREE_EP0	0x02		/* endpoint zero is freed */
46 
47 struct usb_clear_stall_msg {
48 	struct usb_proc_msg hdr;
49 	struct usb_device *udev;
50 };
51 
52 /* The following four structures makes up a tree, where we have the
53  * leaf structure, "usb_host_endpoint", first, and the root structure,
54  * "usb_device", last. The four structures below mirror the structure
55  * of the USB descriptors belonging to an USB configuration. Please
56  * refer to the USB specification for a definition of "endpoints" and
57  * "interfaces".
58  */
59 struct usb_host_endpoint {
60 	struct usb_endpoint_descriptor desc;
61 	TAILQ_HEAD(, urb) bsd_urb_list;
62 	struct usb_xfer *bsd_xfer[2];
63 	uint8_t *extra;			/* Extra descriptors */
64 	usb_frlength_t fbsd_buf_size;
65 	uint16_t extralen;
66 	uint8_t	bsd_iface_index;
67 } __aligned(USB_HOST_ALIGN);
68 
69 struct usb_host_interface {
70 	struct usb_interface_descriptor desc;
71 	/* the following array has size "desc.bNumEndpoint" */
72 	struct usb_host_endpoint *endpoint;
73 	const char *string;		/* iInterface string, if present */
74 	uint8_t *extra;			/* Extra descriptors */
75 	uint16_t extralen;
76 	uint8_t	bsd_iface_index;
77 } __aligned(USB_HOST_ALIGN);
78 
79 /*
80  * The following structure defines an USB pipe which is equal to an
81  * USB endpoint.
82  */
83 struct usb_pipe {
84 	struct usb_xfer_queue pipe_q;	/* queue of USB transfers */
85 
86 	struct usb_endpoint_descriptor *edesc;
87 	struct usb_pipe_methods *methods;	/* set by HC driver */
88 
89 	uint16_t isoc_next;
90 	uint16_t refcount;
91 
92 	uint8_t	toggle_next:1;		/* next data toggle value */
93 	uint8_t	is_stalled:1;		/* set if pipe is stalled */
94 	uint8_t	is_synced:1;		/* set if we a synchronised */
95 	uint8_t	unused:5;
96 	uint8_t	iface_index;		/* not used by "default pipe" */
97 };
98 
99 /*
100  * The following structure defines an USB interface.
101  */
102 struct usb_interface {
103 	struct usb_interface_descriptor *idesc;
104 	device_t subdev;
105 	uint8_t	alt_index;
106 	uint8_t	parent_iface_index;
107 
108 	/* Linux compat */
109 	struct usb_host_interface *altsetting;
110 	struct usb_host_interface *cur_altsetting;
111 	struct usb_device *linux_udev;
112 	void   *bsd_priv_sc;		/* device specific information */
113 	uint8_t	num_altsetting;		/* number of alternate settings */
114 	uint8_t	bsd_iface_index;
115 };
116 
117 /*
118  * The following structure defines the USB device flags.
119  */
120 struct usb_device_flags {
121 	enum usb_hc_mode usb_mode;	/* host or device mode */
122 	uint8_t	self_powered:1;		/* set if USB device is self powered */
123 	uint8_t	no_strings:1;		/* set if USB device does not support
124 					 * strings */
125 	uint8_t	remote_wakeup:1;	/* set if remote wakeup is enabled */
126 	uint8_t	uq_bus_powered:1;	/* set if BUS powered quirk is present */
127 
128 	/*
129 	 * NOTE: Although the flags below will reach the same value
130 	 * over time, but the instant values may differ, and
131 	 * consequently the flags cannot be merged into one!
132 	 */
133 	uint8_t peer_suspended:1;	/* set if peer is suspended */
134 	uint8_t self_suspended:1;	/* set if self is suspended */
135 };
136 
137 /*
138  * The following structure is used for power-save purposes. The data
139  * in this structure is protected by the USB BUS lock.
140  */
141 struct usb_power_save {
142 	usb_ticks_t last_xfer_time;	/* copy of "ticks" */
143 	usb_size_t type_refs[4];	/* transfer reference count */
144 	usb_size_t read_refs;		/* data read references */
145 	usb_size_t write_refs;		/* data write references */
146 };
147 
148 /*
149  * The following structure defines an USB device. There exists one of
150  * these structures for every USB device.
151  */
152 struct usb_device {
153 	struct usb_clear_stall_msg cs_msg[2];	/* generic clear stall
154 						 * messages */
155 	struct sx default_sx[2];
156 	struct mtx default_mtx[1];
157 	struct cv default_cv[2];
158 	struct usb_interface *ifaces;
159 	struct usb_pipe default_pipe;	/* Control Endpoint 0 */
160 	struct usb_pipe *pipes;
161 	struct usb_power_save pwr_save;/* power save data */
162 	struct usb_bus *bus;		/* our USB BUS */
163 	device_t parent_dev;		/* parent device */
164 	struct usb_device *parent_hub;
165 	struct usb_device *parent_hs_hub;	/* high-speed parent HUB */
166 	struct usb_config_descriptor *cdesc;	/* full config descr */
167 	struct usb_hub *hub;		/* only if this is a hub */
168 	struct usb_xfer *default_xfer[USB_DEFAULT_XFER_MAX];
169 	struct usb_temp_data *usb2_template_ptr;
170 	struct usb_pipe *pipe_curr;	/* current clear stall pipe */
171 #if USB_HAVE_UGEN
172 	struct usb_fifo *fifo[USB_FIFO_MAX];
173 	struct usb_symlink *ugen_symlink;	/* our generic symlink */
174 	struct cdev *default_dev;	/* Control Endpoint 0 device node */
175 	LIST_HEAD(,usb_fs_privdata) pd_list;
176 	char	ugen_name[20];		/* name of ugenX.X device */
177 #endif
178 	usb_ticks_t plugtime;		/* copy of "ticks" */
179 
180 	enum usb_dev_state state;
181 	enum usb_dev_speed speed;
182 	uint16_t refcount;
183 #define	USB_DEV_REF_MAX 0xffff
184 
185 	uint16_t power;			/* mA the device uses */
186 	uint16_t langid;		/* language for strings */
187 
188 	uint8_t	address;		/* device addess */
189 	uint8_t	device_index;		/* device index in "bus->devices" */
190 	uint8_t	curr_config_index;	/* current configuration index */
191 	uint8_t	curr_config_no;		/* current configuration number */
192 	uint8_t	depth;			/* distance from root HUB */
193 	uint8_t	port_index;		/* parent HUB port index */
194 	uint8_t	port_no;		/* parent HUB port number */
195 	uint8_t	hs_hub_addr;		/* high-speed HUB address */
196 	uint8_t	hs_port_no;		/* high-speed HUB port number */
197 	uint8_t	driver_added_refcount;	/* our driver added generation count */
198 	uint8_t	power_mode;		/* see USB_POWER_XXX */
199 	uint8_t ifaces_max;		/* number of interfaces present */
200 	uint8_t pipes_max;		/* number of pipes present */
201 
202 	/* the "flags" field is write-protected by "bus->mtx" */
203 
204 	struct usb_device_flags flags;
205 
206 	struct usb_endpoint_descriptor default_ep_desc;	/* for pipe 0 */
207 	struct usb_device_descriptor ddesc;	/* device descriptor */
208 
209 	char	*serial;		/* serial number */
210 	char	*manufacturer;		/* manufacturer string */
211 	char	*product;		/* product string */
212 
213 #if USB_HAVE_COMPAT_LINUX
214 	/* Linux compat */
215 	struct usb_device_descriptor descriptor;
216 	struct usb_host_endpoint ep0;
217 	struct usb_interface *linux_iface_start;
218 	struct usb_interface *linux_iface_end;
219 	struct usb_host_endpoint *linux_endpoint_start;
220 	struct usb_host_endpoint *linux_endpoint_end;
221 	uint16_t devnum;
222 #endif
223 };
224 
225 /* globals */
226 
227 extern int usb2_template;
228 
229 /* function prototypes */
230 
231 struct usb_device *usb2_alloc_device(device_t parent_dev, struct usb_bus *bus,
232 		    struct usb_device *parent_hub, uint8_t depth,
233 		    uint8_t port_index, uint8_t port_no,
234 		    enum usb_dev_speed speed, enum usb_hc_mode mode);
235 struct usb_pipe *usb2_get_pipe(struct usb_device *udev, uint8_t iface_index,
236 		    const struct usb_config *setup);
237 struct usb_pipe *usb2_get_pipe_by_addr(struct usb_device *udev, uint8_t ea_val);
238 usb_error_t	usb2_interface_count(struct usb_device *udev, uint8_t *count);
239 usb_error_t	usb2_probe_and_attach(struct usb_device *udev,
240 		    uint8_t iface_index);
241 usb_error_t	usb2_reset_iface_endpoints(struct usb_device *udev,
242 		    uint8_t iface_index);
243 usb_error_t	usb2_set_config_index(struct usb_device *udev, uint8_t index);
244 usb_error_t	usb2_set_endpoint_stall(struct usb_device *udev,
245 		    struct usb_pipe *pipe, uint8_t do_stall);
246 usb_error_t	usb2_suspend_resume(struct usb_device *udev,
247 		    uint8_t do_suspend);
248 void	usb2_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len);
249 void	usb2_free_device(struct usb_device *, uint8_t);
250 void	*usb2_find_descriptor(struct usb_device *udev, void *id,
251 	    uint8_t iface_index, uint8_t type, uint8_t type_mask,
252 	    uint8_t subtype, uint8_t subtype_mask);
253 void	usb_linux_free_device(struct usb_device *dev);
254 uint8_t	usb2_peer_can_wakeup(struct usb_device *udev);
255 struct usb_pipe *usb2_pipe_foreach(struct usb_device *udev, struct usb_pipe *pipe);
256 void	usb2_set_device_state(struct usb_device *udev,
257 	    enum usb_dev_state state);
258 
259 #endif					/* _USB2_DEVICE_H_ */
260