upload tizen1.0 source
[kernel/linux-2.6.36.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
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         DECLARE_BITMAP(endpoints, 32);
136         struct device                   *dev;
137 };
138
139 int usb_add_function(struct usb_configuration *, struct usb_function *);
140
141 int usb_function_deactivate(struct usb_function *);
142 int usb_function_activate(struct usb_function *);
143
144 int usb_interface_id(struct usb_configuration *, struct usb_function *);
145
146 void usb_function_set_enabled(struct usb_function *, int);
147 void usb_composite_force_reset(struct usb_composite_dev *);
148
149 /**
150  * ep_choose - select descriptor endpoint at current device speed
151  * @g: gadget, connected and running at some speed
152  * @hs: descriptor to use for high speed operation
153  * @fs: descriptor to use for full or low speed operation
154  */
155 static inline struct usb_endpoint_descriptor *
156 ep_choose(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
157                 struct usb_endpoint_descriptor *fs)
158 {
159         if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
160                 return hs;
161         return fs;
162 }
163
164 #define MAX_CONFIG_INTERFACES           16      /* arbitrary; max 255 */
165
166 /**
167  * struct usb_configuration - represents one gadget configuration
168  * @label: For diagnostics, describes the configuration.
169  * @strings: Tables of strings, keyed by identifiers assigned during @bind()
170  *      and by language IDs provided in control requests.
171  * @descriptors: Table of descriptors preceding all function descriptors.
172  *      Examples include OTG and vendor-specific descriptors.
173  * @bind: Called from @usb_add_config() to allocate resources unique to this
174  *      configuration and to call @usb_add_function() for each function used.
175  * @unbind: Reverses @bind; called as a side effect of unregistering the
176  *      driver which added this configuration.
177  * @setup: Used to delegate control requests that aren't handled by standard
178  *      device infrastructure or directed at a specific interface.
179  * @bConfigurationValue: Copied into configuration descriptor.
180  * @iConfiguration: Copied into configuration descriptor.
181  * @bmAttributes: Copied into configuration descriptor.
182  * @bMaxPower: Copied into configuration descriptor.
183  * @cdev: assigned by @usb_add_config() before calling @bind(); this is
184  *      the device associated with this configuration.
185  *
186  * Configurations are building blocks for gadget drivers structured around
187  * function drivers.  Simple USB gadgets require only one function and one
188  * configuration, and handle dual-speed hardware by always providing the same
189  * functionality.  Slightly more complex gadgets may have more than one
190  * single-function configuration at a given speed; or have configurations
191  * that only work at one speed.
192  *
193  * Composite devices are, by definition, ones with configurations which
194  * include more than one function.
195  *
196  * The lifecycle of a usb_configuration includes allocation, initialization
197  * of the fields described above, and calling @usb_add_config() to set up
198  * internal data and bind it to a specific device.  The configuration's
199  * @bind() method is then used to initialize all the functions and then
200  * call @usb_add_function() for them.
201  *
202  * Those functions would normally be independant of each other, but that's
203  * not mandatory.  CDC WMC devices are an example where functions often
204  * depend on other functions, with some functions subsidiary to others.
205  * Such interdependency may be managed in any way, so long as all of the
206  * descriptors complete by the time the composite driver returns from
207  * its bind() routine.
208  */
209 struct usb_configuration {
210         const char                      *label;
211         struct usb_gadget_strings       **strings;
212         const struct usb_descriptor_header **descriptors;
213
214         /* REVISIT:  bind() functions can be marked __init, which
215          * makes trouble for section mismatch analysis.  See if
216          * we can't restructure things to avoid mismatching...
217          */
218
219         /* configuration management:  bind/unbind */
220         int                     (*bind)(struct usb_configuration *);
221         void                    (*unbind)(struct usb_configuration *);
222         int                     (*setup)(struct usb_configuration *,
223                                         const struct usb_ctrlrequest *);
224
225         /* fields in the config descriptor */
226         u8                      bConfigurationValue;
227         u8                      iConfiguration;
228         u8                      bmAttributes;
229         u8                      bMaxPower;
230
231         struct usb_composite_dev        *cdev;
232
233         /* private: */
234         /* internals */
235         struct list_head        list;
236         struct list_head        functions;
237         u8                      next_interface_id;
238         unsigned                highspeed:1;
239         unsigned                fullspeed:1;
240         struct usb_function     *interface[MAX_CONFIG_INTERFACES];
241 };
242
243 int usb_add_config(struct usb_composite_dev *,
244                 struct usb_configuration *);
245
246 /**
247  * struct usb_composite_driver - groups configurations into a gadget
248  * @name: For diagnostics, identifies the driver.
249  * @dev: Template descriptor for the device, including default device
250  *      identifiers.
251  * @strings: tables of strings, keyed by identifiers assigned during bind()
252  *      and language IDs provided in control requests
253  * @bind: (REQUIRED) Used to allocate resources that are shared across the
254  *      whole device, such as string IDs, and add its configurations using
255  *      @usb_add_config().  This may fail by returning a negative errno
256  *      value; it should return zero on successful initialization.
257  * @unbind: Reverses @bind(); called as a side effect of unregistering
258  *      this driver.
259  * @suspend: Notifies when the host stops sending USB traffic,
260  *      after function notifications
261  * @resume: Notifies configuration when the host restarts USB traffic,
262  *      before function notifications
263  *
264  * Devices default to reporting self powered operation.  Devices which rely
265  * on bus powered operation should report this in their @bind() method.
266  *
267  * Before returning from @bind, various fields in the template descriptor
268  * may be overridden.  These include the idVendor/idProduct/bcdDevice values
269  * normally to bind the appropriate host side driver, and the three strings
270  * (iManufacturer, iProduct, iSerialNumber) normally used to provide user
271  * meaningful device identifiers.  (The strings will not be defined unless
272  * they are defined in @dev and @strings.)  The correct ep0 maxpacket size
273  * is also reported, as defined by the underlying controller driver.
274  */
275 struct usb_composite_driver {
276         const char                              *name;
277         const struct usb_device_descriptor      *dev;
278         struct usb_gadget_strings               **strings;
279
280         struct class            *class;
281         atomic_t                function_count;
282
283         /* REVISIT:  bind() functions can be marked __init, which
284          * makes trouble for section mismatch analysis.  See if
285          * we can't restructure things to avoid mismatching...
286          */
287
288         int                     (*bind)(struct usb_composite_dev *);
289         int                     (*unbind)(struct usb_composite_dev *);
290
291         /* global suspend hooks */
292         void                    (*suspend)(struct usb_composite_dev *);
293         void                    (*resume)(struct usb_composite_dev *);
294
295         void                    (*enable_function)(struct usb_function *f, int enable);
296 };
297
298 extern int usb_composite_register(struct usb_composite_driver *);
299 extern void usb_composite_unregister(struct usb_composite_driver *);
300
301
302 /**
303  * struct usb_composite_device - represents one composite usb gadget
304  * @gadget: read-only, abstracts the gadget's usb peripheral controller
305  * @req: used for control responses; buffer is pre-allocated
306  * @bufsiz: size of buffer pre-allocated in @req
307  * @config: the currently active configuration
308  *
309  * One of these devices is allocated and initialized before the
310  * associated device driver's bind() is called.
311  *
312  * OPEN ISSUE:  it appears that some WUSB devices will need to be
313  * built by combining a normal (wired) gadget with a wireless one.
314  * This revision of the gadget framework should probably try to make
315  * sure doing that won't hurt too much.
316  *
317  * One notion for how to handle Wireless USB devices involves:
318  * (a) a second gadget here, discovery mechanism TBD, but likely
319  *     needing separate "register/unregister WUSB gadget" calls;
320  * (b) updates to usb_gadget to include flags "is it wireless",
321  *     "is it wired", plus (presumably in a wrapper structure)
322  *     bandgroup and PHY info;
323  * (c) presumably a wireless_ep wrapping a usb_ep, and reporting
324  *     wireless-specific parameters like maxburst and maxsequence;
325  * (d) configurations that are specific to wireless links;
326  * (e) function drivers that understand wireless configs and will
327  *     support wireless for (additional) function instances;
328  * (f) a function to support association setup (like CBAF), not
329  *     necessarily requiring a wireless adapter;
330  * (g) composite device setup that can create one or more wireless
331  *     configs, including appropriate association setup support;
332  * (h) more, TBD.
333  */
334 struct usb_composite_dev {
335         struct usb_gadget               *gadget;
336         struct usb_request              *req;
337         unsigned                        bufsiz;
338
339         struct usb_configuration        *config;
340
341         /* private: */
342         /* internals */
343         unsigned int                    suspended:1;
344         struct usb_device_descriptor    desc;
345         struct list_head                configs;
346         struct usb_composite_driver     *driver;
347         u8                              next_string_id;
348
349         /* the gadget driver won't enable the data pullup
350          * while the deactivation count is nonzero.
351          */
352         unsigned                        deactivations;
353
354         /* protects at least deactivation count */
355         spinlock_t                      lock;
356
357         struct switch_dev sdev;
358         /* used by usb_composite_force_reset to avoid signalling switch changes */
359         bool                            mute_switch;
360         struct work_struct switch_work;
361 #ifdef CONFIG_USB_ANDROID_SAMSUNG_COMPOSITE
362 /* soonyong.cho : Below values are used for samsung composite framework. */
363         struct android_usb_product      *products;      /* products list */
364 #endif
365 };
366
367 extern int usb_string_id(struct usb_composite_dev *c);
368
369 /* messaging utils */
370 #define DBG(d, fmt, args...) \
371         dev_dbg(&(d)->gadget->dev , fmt , ## args)
372 #define VDBG(d, fmt, args...) \
373         dev_vdbg(&(d)->gadget->dev , fmt , ## args)
374 #define ERROR(d, fmt, args...) \
375         dev_err(&(d)->gadget->dev , fmt , ## args)
376 #define WARNING(d, fmt, args...) \
377         dev_warn(&(d)->gadget->dev , fmt , ## args)
378 #define INFO(d, fmt, args...) \
379         dev_info(&(d)->gadget->dev , fmt , ## args)
380
381 #endif  /* __LINUX_USB_COMPOSITE_H */