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