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