Sebastian Andrzej Siewior | e220ff7 | 2012-09-06 20:11:06 +0200 | [diff] [blame] | 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 | |
| 40 | /* |
| 41 | * USB function drivers should return USB_GADGET_DELAYED_STATUS if they |
| 42 | * wish to delay the data/status stages of the control transfer till they |
| 43 | * are ready. The control transfer will then be kept from completing till |
| 44 | * all the function drivers that requested for USB_GADGET_DELAYED_STAUS |
| 45 | * invoke usb_composite_setup_continue(). |
| 46 | */ |
| 47 | #define USB_GADGET_DELAYED_STATUS 0x7fff /* Impossibly large value */ |
| 48 | |
| 49 | struct usb_configuration; |
| 50 | |
| 51 | /** |
| 52 | * struct usb_function - describes one function of a configuration |
| 53 | * @name: For diagnostics, identifies the function. |
| 54 | * @strings: tables of strings, keyed by identifiers assigned during bind() |
| 55 | * and by language IDs provided in control requests |
| 56 | * @descriptors: Table of full (or low) speed descriptors, using interface and |
| 57 | * string identifiers assigned during @bind(). If this pointer is null, |
| 58 | * the function will not be available at full speed (or at low speed). |
| 59 | * @hs_descriptors: Table of high speed descriptors, using interface and |
| 60 | * string identifiers assigned during @bind(). If this pointer is null, |
| 61 | * the function will not be available at high speed. |
| 62 | * @ss_descriptors: Table of super speed descriptors, using interface and |
| 63 | * string identifiers assigned during @bind(). If this |
| 64 | * pointer is null after initiation, the function will not |
| 65 | * be available at super speed. |
| 66 | * @config: assigned when @usb_add_function() is called; this is the |
| 67 | * configuration with which this function is associated. |
| 68 | * @bind: Before the gadget can register, all of its functions bind() to the |
| 69 | * available resources including string and interface identifiers used |
| 70 | * in interface or class descriptors; endpoints; I/O buffers; and so on. |
| 71 | * @unbind: Reverses @bind; called as a side effect of unregistering the |
| 72 | * driver which added this function. |
| 73 | * @set_alt: (REQUIRED) Reconfigures altsettings; function drivers may |
| 74 | * initialize usb_ep.driver data at this time (when it is used). |
| 75 | * Note that setting an interface to its current altsetting resets |
| 76 | * interface state, and that all interfaces have a disabled state. |
| 77 | * @get_alt: Returns the active altsetting. If this is not provided, |
| 78 | * then only altsetting zero is supported. |
| 79 | * @disable: (REQUIRED) Indicates the function should be disabled. Reasons |
| 80 | * include host resetting or reconfiguring the gadget, and disconnection. |
| 81 | * @setup: Used for interface-specific control requests. |
| 82 | * @suspend: Notifies functions when the host stops sending USB traffic. |
| 83 | * @resume: Notifies functions when the host restarts USB traffic. |
| 84 | * @get_status: Returns function status as a reply to |
| 85 | * GetStatus() request when the recepient is Interface. |
| 86 | * @func_suspend: callback to be called when |
| 87 | * SetFeature(FUNCTION_SUSPEND) is reseived |
| 88 | * |
| 89 | * A single USB function uses one or more interfaces, and should in most |
| 90 | * cases support operation at both full and high speeds. Each function is |
| 91 | * associated by @usb_add_function() with a one configuration; that function |
| 92 | * causes @bind() to be called so resources can be allocated as part of |
| 93 | * setting up a gadget driver. Those resources include endpoints, which |
| 94 | * should be allocated using @usb_ep_autoconfig(). |
| 95 | * |
| 96 | * To support dual speed operation, a function driver provides descriptors |
| 97 | * for both high and full speed operation. Except in rare cases that don't |
| 98 | * involve bulk endpoints, each speed needs different endpoint descriptors. |
| 99 | * |
| 100 | * Function drivers choose their own strategies for managing instance data. |
| 101 | * The simplest strategy just declares it "static', which means the function |
| 102 | * can only be activated once. If the function needs to be exposed in more |
| 103 | * than one configuration at a given speed, it needs to support multiple |
| 104 | * usb_function structures (one for each configuration). |
| 105 | * |
| 106 | * A more complex strategy might encapsulate a @usb_function structure inside |
| 107 | * a driver-specific instance structure to allows multiple activations. An |
| 108 | * example of multiple activations might be a CDC ACM function that supports |
| 109 | * two or more distinct instances within the same configuration, providing |
| 110 | * several independent logical data links to a USB host. |
| 111 | */ |
| 112 | struct usb_function { |
| 113 | const char *name; |
| 114 | struct usb_gadget_strings **strings; |
| 115 | struct usb_descriptor_header **descriptors; |
| 116 | struct usb_descriptor_header **hs_descriptors; |
| 117 | struct usb_descriptor_header **ss_descriptors; |
| 118 | |
| 119 | struct usb_configuration *config; |
| 120 | |
| 121 | /* REVISIT: bind() functions can be marked __init, which |
| 122 | * makes trouble for section mismatch analysis. See if |
| 123 | * we can't restructure things to avoid mismatching. |
| 124 | * Related: unbind() may kfree() but bind() won't... |
| 125 | */ |
| 126 | |
| 127 | /* configuration management: bind/unbind */ |
| 128 | int (*bind)(struct usb_configuration *, |
| 129 | struct usb_function *); |
| 130 | void (*unbind)(struct usb_configuration *, |
| 131 | struct usb_function *); |
| 132 | |
| 133 | /* runtime state management */ |
| 134 | int (*set_alt)(struct usb_function *, |
| 135 | unsigned interface, unsigned alt); |
| 136 | int (*get_alt)(struct usb_function *, |
| 137 | unsigned interface); |
| 138 | void (*disable)(struct usb_function *); |
| 139 | int (*setup)(struct usb_function *, |
| 140 | const struct usb_ctrlrequest *); |
| 141 | void (*suspend)(struct usb_function *); |
| 142 | void (*resume)(struct usb_function *); |
| 143 | |
| 144 | /* USB 3.0 additions */ |
| 145 | int (*get_status)(struct usb_function *); |
| 146 | int (*func_suspend)(struct usb_function *, |
| 147 | u8 suspend_opt); |
| 148 | /* private: */ |
| 149 | /* internals */ |
| 150 | struct list_head list; |
| 151 | DECLARE_BITMAP(endpoints, 32); |
| 152 | }; |
| 153 | |
| 154 | int usb_add_function(struct usb_configuration *, struct usb_function *); |
| 155 | |
| 156 | int usb_function_deactivate(struct usb_function *); |
| 157 | int usb_function_activate(struct usb_function *); |
| 158 | |
| 159 | int usb_interface_id(struct usb_configuration *, struct usb_function *); |
| 160 | |
| 161 | int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f, |
| 162 | struct usb_ep *_ep); |
| 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 | * @unbind: Reverses @bind; called as a side effect of unregistering the |
| 174 | * driver which added this configuration. |
| 175 | * @setup: Used to delegate control requests that aren't handled by standard |
| 176 | * device infrastructure or directed at a specific interface. |
| 177 | * @bConfigurationValue: Copied into configuration descriptor. |
| 178 | * @iConfiguration: Copied into configuration descriptor. |
| 179 | * @bmAttributes: Copied into configuration descriptor. |
| 180 | * @bMaxPower: Copied into configuration descriptor. |
| 181 | * @cdev: assigned by @usb_add_config() before calling @bind(); this is |
| 182 | * the device associated with this configuration. |
| 183 | * |
| 184 | * Configurations are building blocks for gadget drivers structured around |
| 185 | * function drivers. Simple USB gadgets require only one function and one |
| 186 | * configuration, and handle dual-speed hardware by always providing the same |
| 187 | * functionality. Slightly more complex gadgets may have more than one |
| 188 | * single-function configuration at a given speed; or have configurations |
| 189 | * that only work at one speed. |
| 190 | * |
| 191 | * Composite devices are, by definition, ones with configurations which |
| 192 | * include more than one function. |
| 193 | * |
| 194 | * The lifecycle of a usb_configuration includes allocation, initialization |
| 195 | * of the fields described above, and calling @usb_add_config() to set up |
| 196 | * internal data and bind it to a specific device. The configuration's |
| 197 | * @bind() method is then used to initialize all the functions and then |
| 198 | * call @usb_add_function() for them. |
| 199 | * |
| 200 | * Those functions would normally be independent of each other, but that's |
| 201 | * not mandatory. CDC WMC devices are an example where functions often |
| 202 | * depend on other functions, with some functions subsidiary to others. |
| 203 | * Such interdependency may be managed in any way, so long as all of the |
| 204 | * descriptors complete by the time the composite driver returns from |
| 205 | * its bind() routine. |
| 206 | */ |
| 207 | struct usb_configuration { |
| 208 | const char *label; |
| 209 | struct usb_gadget_strings **strings; |
| 210 | const struct usb_descriptor_header **descriptors; |
| 211 | |
| 212 | /* REVISIT: bind() functions can be marked __init, which |
| 213 | * makes trouble for section mismatch analysis. See if |
| 214 | * we can't restructure things to avoid mismatching... |
| 215 | */ |
| 216 | |
| 217 | /* configuration management: unbind/setup */ |
| 218 | void (*unbind)(struct usb_configuration *); |
| 219 | int (*setup)(struct usb_configuration *, |
| 220 | const struct usb_ctrlrequest *); |
| 221 | |
| 222 | /* fields in the config descriptor */ |
| 223 | u8 bConfigurationValue; |
| 224 | u8 iConfiguration; |
| 225 | u8 bmAttributes; |
| 226 | u8 bMaxPower; |
| 227 | |
| 228 | struct usb_composite_dev *cdev; |
| 229 | |
| 230 | /* private: */ |
| 231 | /* internals */ |
| 232 | struct list_head list; |
| 233 | struct list_head functions; |
| 234 | u8 next_interface_id; |
| 235 | unsigned superspeed:1; |
| 236 | unsigned highspeed:1; |
| 237 | unsigned fullspeed:1; |
| 238 | struct usb_function *interface[MAX_CONFIG_INTERFACES]; |
| 239 | }; |
| 240 | |
| 241 | int usb_add_config(struct usb_composite_dev *, |
| 242 | struct usb_configuration *, |
| 243 | int (*)(struct usb_configuration *)); |
| 244 | |
| 245 | void usb_remove_config(struct usb_composite_dev *, |
| 246 | struct usb_configuration *); |
| 247 | |
| 248 | /** |
| 249 | * struct usb_composite_driver - groups configurations into a gadget |
| 250 | * @name: For diagnostics, identifies the driver. |
| 251 | * @iProduct: Used as iProduct override if @dev->iProduct is not set. |
| 252 | * If NULL value of @name is taken. |
| 253 | * @iManufacturer: Used as iManufacturer override if @dev->iManufacturer is |
| 254 | * not set. If NULL a default "<system> <release> with <udc>" value |
| 255 | * will be used. |
| 256 | * @iSerialNumber: Used as iSerialNumber override if @dev->iSerialNumber is |
| 257 | * not set. |
| 258 | * @dev: Template descriptor for the device, including default device |
| 259 | * identifiers. |
| 260 | * @strings: tables of strings, keyed by identifiers assigned during @bind |
| 261 | * and language IDs provided in control requests |
| 262 | * @max_speed: Highest speed the driver supports. |
| 263 | * @needs_serial: set to 1 if the gadget needs userspace to provide |
| 264 | * a serial number. If one is not provided, warning will be printed. |
| 265 | * @bind: (REQUIRED) Used to allocate resources that are shared across the |
| 266 | * whole device, such as string IDs, and add its configurations using |
| 267 | * @usb_add_config(). This may fail by returning a negative errno |
| 268 | * value; it should return zero on successful initialization. |
| 269 | * @unbind: Reverses @bind; called as a side effect of unregistering |
| 270 | * this driver. |
| 271 | * @disconnect: optional driver disconnect method |
| 272 | * @suspend: Notifies when the host stops sending USB traffic, |
| 273 | * after function notifications |
| 274 | * @resume: Notifies configuration when the host restarts USB traffic, |
| 275 | * before function notifications |
| 276 | * |
| 277 | * Devices default to reporting self powered operation. Devices which rely |
| 278 | * on bus powered operation should report this in their @bind method. |
| 279 | * |
| 280 | * Before returning from @bind, various fields in the template descriptor |
| 281 | * may be overridden. These include the idVendor/idProduct/bcdDevice values |
| 282 | * normally to bind the appropriate host side driver, and the three strings |
| 283 | * (iManufacturer, iProduct, iSerialNumber) normally used to provide user |
| 284 | * meaningful device identifiers. (The strings will not be defined unless |
| 285 | * they are defined in @dev and @strings.) The correct ep0 maxpacket size |
| 286 | * is also reported, as defined by the underlying controller driver. |
| 287 | */ |
| 288 | struct usb_composite_driver { |
| 289 | const char *name; |
| 290 | const char *iProduct; |
| 291 | const char *iManufacturer; |
| 292 | const char *iSerialNumber; |
| 293 | const struct usb_device_descriptor *dev; |
| 294 | struct usb_gadget_strings **strings; |
| 295 | enum usb_device_speed max_speed; |
| 296 | unsigned needs_serial:1; |
| 297 | |
| 298 | int (*bind)(struct usb_composite_dev *cdev); |
| 299 | int (*unbind)(struct usb_composite_dev *); |
| 300 | |
| 301 | void (*disconnect)(struct usb_composite_dev *); |
| 302 | |
| 303 | /* global suspend hooks */ |
| 304 | void (*suspend)(struct usb_composite_dev *); |
| 305 | void (*resume)(struct usb_composite_dev *); |
| 306 | }; |
| 307 | |
| 308 | extern int usb_composite_probe(struct usb_composite_driver *driver); |
| 309 | extern void usb_composite_unregister(struct usb_composite_driver *driver); |
| 310 | extern void usb_composite_setup_continue(struct usb_composite_dev *cdev); |
| 311 | |
| 312 | |
| 313 | /** |
| 314 | * struct usb_composite_device - represents one composite usb gadget |
| 315 | * @gadget: read-only, abstracts the gadget's usb peripheral controller |
| 316 | * @req: used for control responses; buffer is pre-allocated |
| 317 | * @bufsiz: size of buffer pre-allocated in @req |
| 318 | * @config: the currently active configuration |
| 319 | * |
| 320 | * One of these devices is allocated and initialized before the |
| 321 | * associated device driver's bind() is called. |
| 322 | * |
| 323 | * OPEN ISSUE: it appears that some WUSB devices will need to be |
| 324 | * built by combining a normal (wired) gadget with a wireless one. |
| 325 | * This revision of the gadget framework should probably try to make |
| 326 | * sure doing that won't hurt too much. |
| 327 | * |
| 328 | * One notion for how to handle Wireless USB devices involves: |
| 329 | * (a) a second gadget here, discovery mechanism TBD, but likely |
| 330 | * needing separate "register/unregister WUSB gadget" calls; |
| 331 | * (b) updates to usb_gadget to include flags "is it wireless", |
| 332 | * "is it wired", plus (presumably in a wrapper structure) |
| 333 | * bandgroup and PHY info; |
| 334 | * (c) presumably a wireless_ep wrapping a usb_ep, and reporting |
| 335 | * wireless-specific parameters like maxburst and maxsequence; |
| 336 | * (d) configurations that are specific to wireless links; |
| 337 | * (e) function drivers that understand wireless configs and will |
| 338 | * support wireless for (additional) function instances; |
| 339 | * (f) a function to support association setup (like CBAF), not |
| 340 | * necessarily requiring a wireless adapter; |
| 341 | * (g) composite device setup that can create one or more wireless |
| 342 | * configs, including appropriate association setup support; |
| 343 | * (h) more, TBD. |
| 344 | */ |
| 345 | struct usb_composite_dev { |
| 346 | struct usb_gadget *gadget; |
| 347 | struct usb_request *req; |
| 348 | unsigned bufsiz; |
| 349 | |
| 350 | struct usb_configuration *config; |
| 351 | |
| 352 | /* private: */ |
| 353 | /* internals */ |
| 354 | unsigned int suspended:1; |
| 355 | struct usb_device_descriptor desc; |
| 356 | struct list_head configs; |
| 357 | struct usb_composite_driver *driver; |
| 358 | u8 next_string_id; |
| 359 | u8 manufacturer_override; |
| 360 | u8 product_override; |
| 361 | u8 serial_override; |
| 362 | |
| 363 | /* the gadget driver won't enable the data pullup |
| 364 | * while the deactivation count is nonzero. |
| 365 | */ |
| 366 | unsigned deactivations; |
| 367 | |
| 368 | /* the composite driver won't complete the control transfer's |
| 369 | * data/status stages till delayed_status is zero. |
| 370 | */ |
| 371 | int delayed_status; |
| 372 | |
| 373 | /* protects deactivations and delayed_status counts*/ |
| 374 | spinlock_t lock; |
| 375 | }; |
| 376 | |
| 377 | extern int usb_string_id(struct usb_composite_dev *c); |
| 378 | extern int usb_string_ids_tab(struct usb_composite_dev *c, |
| 379 | struct usb_string *str); |
| 380 | extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n); |
| 381 | |
| 382 | |
| 383 | /* messaging utils */ |
| 384 | #define DBG(d, fmt, args...) \ |
| 385 | dev_dbg(&(d)->gadget->dev , fmt , ## args) |
| 386 | #define VDBG(d, fmt, args...) \ |
| 387 | dev_vdbg(&(d)->gadget->dev , fmt , ## args) |
| 388 | #define ERROR(d, fmt, args...) \ |
| 389 | dev_err(&(d)->gadget->dev , fmt , ## args) |
| 390 | #define WARNING(d, fmt, args...) \ |
| 391 | dev_warn(&(d)->gadget->dev , fmt , ## args) |
| 392 | #define INFO(d, fmt, args...) \ |
| 393 | dev_info(&(d)->gadget->dev , fmt , ## args) |
| 394 | |
| 395 | #endif /* __LINUX_USB_COMPOSITE_H */ |