xref: /freebsd/sys/dev/usb/usb_device.h (revision e7153b2583ec32ced588706fe1996d909b23bc3c)
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 usb2_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 usb2_clear_stall_msg {
48 	struct usb2_proc_msg hdr;
49 	struct usb2_device *udev;
50 };
51 
52 /*
53  * The following structure defines an USB pipe which is equal to an
54  * USB endpoint.
55  */
56 struct usb2_pipe {
57 	struct usb2_xfer_queue pipe_q;	/* queue of USB transfers */
58 
59 	struct usb2_endpoint_descriptor *edesc;
60 	struct usb2_pipe_methods *methods;	/* set by HC driver */
61 
62 	uint16_t isoc_next;
63 	uint16_t refcount;
64 
65 	uint8_t	toggle_next:1;		/* next data toggle value */
66 	uint8_t	is_stalled:1;		/* set if pipe is stalled */
67 	uint8_t	is_synced:1;		/* set if we a synchronised */
68 	uint8_t	unused:5;
69 	uint8_t	iface_index;		/* not used by "default pipe" */
70 };
71 
72 /*
73  * The following structure defines an USB interface.
74  */
75 struct usb2_interface {
76 	struct usb2_interface_descriptor *idesc;
77 	device_t subdev;
78 	uint8_t	alt_index;
79 	uint8_t	parent_iface_index;
80 };
81 
82 /*
83  * The following structure defines the USB device flags.
84  */
85 struct usb2_device_flags {
86 	uint8_t	usb2_mode:1;		/* USB mode (see USB_MODE_XXX) */
87 	uint8_t	self_powered:1;		/* set if USB device is self powered */
88 	uint8_t	no_strings:1;		/* set if USB device does not support
89 					 * strings */
90 	uint8_t	remote_wakeup:1;	/* set if remote wakeup is enabled */
91 	uint8_t	uq_bus_powered:1;	/* set if BUS powered quirk is present */
92 
93 	/*
94 	 * NOTE: Although the flags below will reach the same value
95 	 * over time, but the instant values may differ, and
96 	 * consequently the flags cannot be merged into one!
97 	 */
98 	uint8_t peer_suspended:1;	/* set if peer is suspended */
99 	uint8_t self_suspended:1;	/* set if self is suspended */
100 };
101 
102 /*
103  * The following structure is used for power-save purposes. The data
104  * in this structure is protected by the USB BUS lock.
105  */
106 struct usb2_power_save {
107 	usb2_ticks_t last_xfer_time;	/* copy of "ticks" */
108 	usb2_size_t type_refs[4];	/* transfer reference count */
109 	usb2_size_t read_refs;		/* data read references */
110 	usb2_size_t write_refs;		/* data write references */
111 };
112 
113 /*
114  * The following structure defines an USB device. There exists one of
115  * these structures for every USB device.
116  */
117 struct usb2_device {
118 	struct usb2_clear_stall_msg cs_msg[2];	/* generic clear stall
119 						 * messages */
120 	struct sx default_sx[2];
121 	struct mtx default_mtx[1];
122 	struct cv default_cv[2];
123 	struct usb2_interface *ifaces;
124 	struct usb2_pipe default_pipe;	/* Control Endpoint 0 */
125 	struct usb2_pipe *pipes;
126 	struct usb2_power_save pwr_save;/* power save data */
127 	struct usb2_bus *bus;		/* our USB BUS */
128 	device_t parent_dev;		/* parent device */
129 	struct usb2_device *parent_hub;
130 	struct usb2_device *parent_hs_hub;	/* high-speed parent HUB */
131 	struct usb2_config_descriptor *cdesc;	/* full config descr */
132 	struct usb2_hub *hub;		/* only if this is a hub */
133 #if USB_HAVE_COMPAT_LINUX
134 	struct usb_device *linux_dev;
135 #endif
136 	struct usb2_xfer *default_xfer[USB_DEFAULT_XFER_MAX];
137 	struct usb2_temp_data *usb2_template_ptr;
138 	struct usb2_pipe *pipe_curr;	/* current clear stall pipe */
139 #if USB_HAVE_UGEN
140 	struct usb2_fifo *fifo[USB_FIFO_MAX];
141 	struct usb2_symlink *ugen_symlink;	/* our generic symlink */
142 	struct cdev *default_dev;	/* Control Endpoint 0 device node */
143 	LIST_HEAD(,usb2_fs_privdata) pd_list;
144 	char	ugen_name[20];		/* name of ugenX.X device */
145 #endif
146 	usb2_ticks_t plugtime;		/* copy of "ticks" */
147 
148 	enum usb2_dev_state state;
149 	uint16_t refcount;
150 #define	USB_DEV_REF_MAX 0xffff
151 
152 	uint16_t power;			/* mA the device uses */
153 	uint16_t langid;		/* language for strings */
154 
155 	uint8_t	address;		/* device addess */
156 	uint8_t	device_index;		/* device index in "bus->devices" */
157 	uint8_t	curr_config_index;	/* current configuration index */
158 	uint8_t	curr_config_no;		/* current configuration number */
159 	uint8_t	depth;			/* distance from root HUB */
160 	uint8_t	speed;			/* low/full/high speed */
161 	uint8_t	port_index;		/* parent HUB port index */
162 	uint8_t	port_no;		/* parent HUB port number */
163 	uint8_t	hs_hub_addr;		/* high-speed HUB address */
164 	uint8_t	hs_port_no;		/* high-speed HUB port number */
165 	uint8_t	driver_added_refcount;	/* our driver added generation count */
166 	uint8_t	power_mode;		/* see USB_POWER_XXX */
167 	uint8_t ifaces_max;		/* number of interfaces present */
168 	uint8_t pipes_max;		/* number of pipes present */
169 
170 	/* the "flags" field is write-protected by "bus->mtx" */
171 
172 	struct usb2_device_flags flags;
173 
174 	struct usb2_endpoint_descriptor default_ep_desc;	/* for pipe 0 */
175 	struct usb2_device_descriptor ddesc;	/* device descriptor */
176 
177 #if USB_HAVE_STRINGS
178 	char	serial[64];		/* serial number */
179 	char	manufacturer[64];	/* manufacturer string */
180 	char	product[64];		/* product string */
181 #endif
182 };
183 
184 /* globals */
185 
186 extern int usb2_template;
187 
188 /* function prototypes */
189 
190 struct usb2_device *usb2_alloc_device(device_t parent_dev, struct usb2_bus *bus,
191 		    struct usb2_device *parent_hub, uint8_t depth,
192 		    uint8_t port_index, uint8_t port_no, uint8_t speed,
193 		    uint8_t usb2_mode);
194 struct usb2_pipe *usb2_get_pipe(struct usb2_device *udev, uint8_t iface_index,
195 		    const struct usb2_config *setup);
196 struct usb2_pipe *usb2_get_pipe_by_addr(struct usb2_device *udev, uint8_t ea_val);
197 usb2_error_t	usb2_interface_count(struct usb2_device *udev, uint8_t *count);
198 usb2_error_t	usb2_probe_and_attach(struct usb2_device *udev,
199 		    uint8_t iface_index);
200 usb2_error_t	usb2_reset_iface_endpoints(struct usb2_device *udev,
201 		    uint8_t iface_index);
202 usb2_error_t	usb2_set_config_index(struct usb2_device *udev, uint8_t index);
203 usb2_error_t	usb2_set_endpoint_stall(struct usb2_device *udev,
204 		    struct usb2_pipe *pipe, uint8_t do_stall);
205 usb2_error_t	usb2_suspend_resume(struct usb2_device *udev,
206 		    uint8_t do_suspend);
207 void	usb2_devinfo(struct usb2_device *udev, char *dst_ptr, uint16_t dst_len);
208 void	usb2_free_device(struct usb2_device *, uint8_t);
209 void	*usb2_find_descriptor(struct usb2_device *udev, void *id,
210 	    uint8_t iface_index, uint8_t type, uint8_t type_mask,
211 	    uint8_t subtype, uint8_t subtype_mask);
212 void	usb_linux_free_device(struct usb_device *dev);
213 uint8_t	usb2_peer_can_wakeup(struct usb2_device *udev);
214 struct usb2_pipe *usb2_pipe_foreach(struct usb2_device *udev, struct usb2_pipe *pipe);
215 void	usb2_set_device_state(struct usb2_device *udev,
216 	    enum usb2_dev_state state);
217 
218 #endif					/* _USB2_DEVICE_H_ */
219