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