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