Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / u-boot / include / linux / usb / composite.h
1 /*
2  * composite.h -- framework for usb gadgets which are composite devices
3  *
4  * Copyright (C) 2006-2008 David Brownell
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #ifndef __LINUX_USB_COMPOSITE_H
10 #define __LINUX_USB_COMPOSITE_H
11
12 /*
13  * This framework is an optional layer on top of the USB Gadget interface,
14  * making it easier to build (a) Composite devices, supporting multiple
15  * functions within any single configuration, and (b) Multi-configuration
16  * devices, also supporting multiple functions but without necessarily
17  * having more than one function per configuration.
18  *
19  * Example:  a device with a single configuration supporting both network
20  * link and mass storage functions is a composite device.  Those functions
21  * might alternatively be packaged in individual configurations, but in
22  * the composite model the host can use both functions at the same time.
23  */
24
25 #include <common.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
28 #include <usb/lin_gadget_compat.h>
29
30 struct usb_configuration;
31
32 /**
33  * struct usb_function - describes one function of a configuration
34  * @name: For diagnostics, identifies the function.
35  * @strings: tables of strings, keyed by identifiers assigned during bind()
36  *      and by language IDs provided in control requests
37  * @descriptors: Table of full (or low) speed descriptors, using interface and
38  *      string identifiers assigned during @bind().  If this pointer is null,
39  *      the function will not be available at full speed (or at low speed).
40  * @hs_descriptors: Table of high speed descriptors, using interface and
41  *      string identifiers assigned during @bind().  If this pointer is null,
42  *      the function will not be available at high speed.
43  * @config: assigned when @usb_add_function() is called; this is the
44  *      configuration with which this function is associated.
45  * @bind: Before the gadget can register, all of its functions bind() to the
46  *      available resources including string and interface identifiers used
47  *      in interface or class descriptors; endpoints; I/O buffers; and so on.
48  * @unbind: Reverses @bind; called as a side effect of unregistering the
49  *      driver which added this function.
50  * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may
51  *      initialize usb_ep.driver data at this time (when it is used).
52  *      Note that setting an interface to its current altsetting resets
53  *      interface state, and that all interfaces have a disabled state.
54  * @get_alt: Returns the active altsetting.  If this is not provided,
55  *      then only altsetting zero is supported.
56  * @disable: (REQUIRED) Indicates the function should be disabled.  Reasons
57  *      include host resetting or reconfiguring the gadget, and disconnection.
58  * @setup: Used for interface-specific control requests.
59  * @suspend: Notifies functions when the host stops sending USB traffic.
60  * @resume: Notifies functions when the host restarts USB traffic.
61  *
62  * A single USB function uses one or more interfaces, and should in most
63  * cases support operation at both full and high speeds.  Each function is
64  * associated by @usb_add_function() with a one configuration; that function
65  * causes @bind() to be called so resources can be allocated as part of
66  * setting up a gadget driver.  Those resources include endpoints, which
67  * should be allocated using @usb_ep_autoconfig().
68  *
69  * To support dual speed operation, a function driver provides descriptors
70  * for both high and full speed operation.  Except in rare cases that don't
71  * involve bulk endpoints, each speed needs different endpoint descriptors.
72  *
73  * Function drivers choose their own strategies for managing instance data.
74  * The simplest strategy just declares it "static', which means the function
75  * can only be activated once.  If the function needs to be exposed in more
76  * than one configuration at a given speed, it needs to support multiple
77  * usb_function structures (one for each configuration).
78  *
79  * A more complex strategy might encapsulate a @usb_function structure inside
80  * a driver-specific instance structure to allows multiple activations.  An
81  * example of multiple activations might be a CDC ACM function that supports
82  * two or more distinct instances within the same configuration, providing
83  * several independent logical data links to a USB host.
84  */
85 struct usb_function {
86         const char                      *name;
87         struct usb_gadget_strings       **strings;
88         struct usb_descriptor_header    **descriptors;
89         struct usb_descriptor_header    **hs_descriptors;
90
91         struct usb_configuration        *config;
92
93         /* REVISIT:  bind() functions can be marked __init, which
94          * makes trouble for section mismatch analysis.  See if
95          * we can't restructure things to avoid mismatching.
96          * Related:  unbind() may kfree() but bind() won't...
97          */
98
99         /* configuration management:  bind/unbind */
100         int                     (*bind)(struct usb_configuration *,
101                                         struct usb_function *);
102         void                    (*unbind)(struct usb_configuration *,
103                                         struct usb_function *);
104
105         /* runtime state management */
106         int                     (*set_alt)(struct usb_function *,
107                                         unsigned interface, unsigned alt);
108         int                     (*get_alt)(struct usb_function *,
109                                         unsigned interface);
110         void                    (*disable)(struct usb_function *);
111         int                     (*setup)(struct usb_function *,
112                                         const struct usb_ctrlrequest *);
113         void                    (*suspend)(struct usb_function *);
114         void                    (*resume)(struct usb_function *);
115
116         /* private: */
117         /* internals */
118         struct list_head                list;
119         DECLARE_BITMAP(endpoints, 32);
120 };
121
122 int usb_add_function(struct usb_configuration *, struct usb_function *);
123
124 int usb_function_deactivate(struct usb_function *);
125 int usb_function_activate(struct usb_function *);
126
127 int usb_interface_id(struct usb_configuration *, struct usb_function *);
128
129 /**
130  * ep_choose - select descriptor endpoint at current device speed
131  * @g: gadget, connected and running at some speed
132  * @hs: descriptor to use for high speed operation
133  * @fs: descriptor to use for full or low speed operation
134  */
135 static inline struct usb_endpoint_descriptor *
136 ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
137                 struct usb_endpoint_descriptor *fs)
138 {
139         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
140                 return hs;
141         return fs;
142 }
143
144 #define MAX_CONFIG_INTERFACES           16      /* arbitrary; max 255 */
145
146 /**
147  * struct usb_configuration - represents one gadget configuration
148  * @label: For diagnostics, describes the configuration.
149  * @strings: Tables of strings, keyed by identifiers assigned during @bind()
150  *      and by language IDs provided in control requests.
151  * @descriptors: Table of descriptors preceding all function descriptors.
152  *      Examples include OTG and vendor-specific descriptors.
153  * @bind: Called from @usb_add_config() to allocate resources unique to this
154  *      configuration and to call @usb_add_function() for each function used.
155  * @unbind: Reverses @bind; called as a side effect of unregistering the
156  *      driver which added this configuration.
157  * @setup: Used to delegate control requests that aren't handled by standard
158  *      device infrastructure or directed at a specific interface.
159  * @bConfigurationValue: Copied into configuration descriptor.
160  * @iConfiguration: Copied into configuration descriptor.
161  * @bmAttributes: Copied into configuration descriptor.
162  * @bMaxPower: Copied into configuration descriptor.
163  * @cdev: assigned by @usb_add_config() before calling @bind(); this is
164  *      the device associated with this configuration.
165  *
166  * Configurations are building blocks for gadget drivers structured around
167  * function drivers.  Simple USB gadgets require only one function and one
168  * configuration, and handle dual-speed hardware by always providing the same
169  * functionality.  Slightly more complex gadgets may have more than one
170  * single-function configuration at a given speed; or have configurations
171  * that only work at one speed.
172  *
173  * Composite devices are, by definition, ones with configurations which
174  * include more than one function.
175  *
176  * The lifecycle of a usb_configuration includes allocation, initialization
177  * of the fields described above, and calling @usb_add_config() to set up
178  * internal data and bind it to a specific device.  The configuration's
179  * @bind() method is then used to initialize all the functions and then
180  * call @usb_add_function() for them.
181  *
182  * Those functions would normally be independant of each other, but that's
183  * not mandatory.  CDC WMC devices are an example where functions often
184  * depend on other functions, with some functions subsidiary to others.
185  * Such interdependency may be managed in any way, so long as all of the
186  * descriptors complete by the time the composite driver returns from
187  * its bind() routine.
188  */
189 struct usb_configuration {
190         const char                      *label;
191         struct usb_gadget_strings       **strings;
192         const struct usb_descriptor_header **descriptors;
193
194         /* REVISIT:  bind() functions can be marked __init, which
195          * makes trouble for section mismatch analysis.  See if
196          * we can't restructure things to avoid mismatching...
197          */
198
199         /* configuration management:  bind/unbind */
200         int                     (*bind)(struct usb_configuration *);
201         void                    (*unbind)(struct usb_configuration *);
202         int                     (*setup)(struct usb_configuration *,
203                                         const struct usb_ctrlrequest *);
204
205         /* fields in the config descriptor */
206         u8                      bConfigurationValue;
207         u8                      iConfiguration;
208         u8                      bmAttributes;
209         u8                      bMaxPower;
210
211         struct usb_composite_dev        *cdev;
212
213         /* private: */
214         /* internals */
215         struct list_head        list;
216         struct list_head        functions;
217         u8                      next_interface_id;
218         unsigned                highspeed:1;
219         unsigned                fullspeed:1;
220         struct usb_function     *interface[MAX_CONFIG_INTERFACES];
221 };
222
223 int usb_add_config(struct usb_composite_dev *,
224                 struct usb_configuration *);
225
226 /**
227  * struct usb_composite_driver - groups configurations into a gadget
228  * @name: For diagnostics, identifies the driver.
229  * @dev: Template descriptor for the device, including default device
230  *      identifiers.
231  * @strings: tables of strings, keyed by identifiers assigned during bind()
232  *      and language IDs provided in control requests
233  * @bind: (REQUIRED) Used to allocate resources that are shared across the
234  *      whole device, such as string IDs, and add its configurations using
235  *      @usb_add_config().  This may fail by returning a negative errno
236  *      value; it should return zero on successful initialization.
237  * @unbind: Reverses @bind(); called as a side effect of unregistering
238  *      this driver.
239  * @disconnect: optional driver disconnect method
240  * @suspend: Notifies when the host stops sending USB traffic,
241  *      after function notifications
242  * @resume: Notifies configuration when the host restarts USB traffic,
243  *      before function notifications
244  *
245  * Devices default to reporting self powered operation.  Devices which rely
246  * on bus powered operation should report this in their @bind() method.
247  *
248  * Before returning from @bind, various fields in the template descriptor
249  * may be overridden.  These include the idVendor/idProduct/bcdDevice values
250  * normally to bind the appropriate host side driver, and the three strings
251  * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
252  * meaningful device identifiers.  (The strings will not be defined unless
253  * they are defined in @dev and @strings.)  The correct ep0 maxpacket size
254  * is also reported, as defined by the underlying controller driver.
255  */
256 struct usb_composite_driver {
257         const char                              *name;
258         const struct usb_device_descriptor      *dev;
259         struct usb_gadget_strings               **strings;
260
261         /* REVISIT:  bind() functions can be marked __init, which
262          * makes trouble for section mismatch analysis.  See if
263          * we can't restructure things to avoid mismatching...
264          */
265
266         int                     (*bind)(struct usb_composite_dev *);
267         int                     (*unbind)(struct usb_composite_dev *);
268
269         void                    (*disconnect)(struct usb_composite_dev *);
270
271         /* global suspend hooks */
272         void                    (*suspend)(struct usb_composite_dev *);
273         void                    (*resume)(struct usb_composite_dev *);
274 };
275
276 extern int usb_composite_register(struct usb_composite_driver *);
277 extern void usb_composite_unregister(struct usb_composite_driver *);
278
279
280 /**
281  * struct usb_composite_device - represents one composite usb gadget
282  * @gadget: read-only, abstracts the gadget's usb peripheral controller
283  * @req: used for control responses; buffer is pre-allocated
284  * @bufsiz: size of buffer pre-allocated in @req
285  * @config: the currently active configuration
286  *
287  * One of these devices is allocated and initialized before the
288  * associated device driver's bind() is called.
289  *
290  * OPEN ISSUE:  it appears that some WUSB devices will need to be
291  * built by combining a normal (wired) gadget with a wireless one.
292  * This revision of the gadget framework should probably try to make
293  * sure doing that won't hurt too much.
294  *
295  * One notion for how to handle Wireless USB devices involves:
296  * (a) a second gadget here, discovery mechanism TBD, but likely
297  *     needing separate "register/unregister WUSB gadget" calls;
298  * (b) updates to usb_gadget to include flags "is it wireless",
299  *     "is it wired", plus (presumably in a wrapper structure)
300  *     bandgroup and PHY info;
301  * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
302  *     wireless-specific parameters like maxburst and maxsequence;
303  * (d) configurations that are specific to wireless links;
304  * (e) function drivers that understand wireless configs and will
305  *     support wireless for (additional) function instances;
306  * (f) a function to support association setup (like CBAF), not
307  *     necessarily requiring a wireless adapter;
308  * (g) composite device setup that can create one or more wireless
309  *     configs, including appropriate association setup support;
310  * (h) more, TBD.
311  */
312 struct usb_composite_dev {
313         struct usb_gadget               *gadget;
314         struct usb_request              *req;
315         unsigned                        bufsiz;
316
317         struct usb_configuration        *config;
318
319         /* private: */
320         /* internals */
321         unsigned int                    suspended:1;
322         struct usb_device_descriptor __aligned(CONFIG_SYS_CACHELINE_SIZE) desc;
323         struct list_head                configs;
324         struct usb_composite_driver     *driver;
325         u8                              next_string_id;
326
327         /* the gadget driver won't enable the data pullup
328          * while the deactivation count is nonzero.
329          */
330         unsigned                        deactivations;
331 };
332
333 extern int usb_string_id(struct usb_composite_dev *c);
334 extern int usb_string_ids_tab(struct usb_composite_dev *c,
335                               struct usb_string *str);
336 extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n);
337
338 #endif  /* __LINUX_USB_COMPOSITE_H */