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 | |
Li Jun | 2a16354 | 2021-01-25 21:43:46 +0800 | [diff] [blame^] | 287 | #define OS_STRING_QW_SIGN_LEN 14 |
| 288 | #define OS_STRING_IDX 0xEE |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * struct usb_composite_device - represents one composite usb gadget |
| 292 | * @gadget: read-only, abstracts the gadget's usb peripheral controller |
| 293 | * @req: used for control responses; buffer is pre-allocated |
| 294 | * @bufsiz: size of buffer pre-allocated in @req |
| 295 | * @config: the currently active configuration |
Li Jun | 2a16354 | 2021-01-25 21:43:46 +0800 | [diff] [blame^] | 296 | * @qw_sign: qwSignature part of the OS string |
| 297 | * @b_vendor_code: bMS_VendorCode part of the OS string |
| 298 | * @use_os_string: false by default, interested gadgets set it |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 299 | * |
| 300 | * One of these devices is allocated and initialized before the |
| 301 | * associated device driver's bind() is called. |
| 302 | * |
| 303 | * OPEN ISSUE: it appears that some WUSB devices will need to be |
| 304 | * built by combining a normal (wired) gadget with a wireless one. |
| 305 | * This revision of the gadget framework should probably try to make |
| 306 | * sure doing that won't hurt too much. |
| 307 | * |
| 308 | * One notion for how to handle Wireless USB devices involves: |
| 309 | * (a) a second gadget here, discovery mechanism TBD, but likely |
| 310 | * needing separate "register/unregister WUSB gadget" calls; |
| 311 | * (b) updates to usb_gadget to include flags "is it wireless", |
| 312 | * "is it wired", plus (presumably in a wrapper structure) |
| 313 | * bandgroup and PHY info; |
| 314 | * (c) presumably a wireless_ep wrapping a usb_ep, and reporting |
| 315 | * wireless-specific parameters like maxburst and maxsequence; |
| 316 | * (d) configurations that are specific to wireless links; |
| 317 | * (e) function drivers that understand wireless configs and will |
| 318 | * support wireless for (additional) function instances; |
| 319 | * (f) a function to support association setup (like CBAF), not |
| 320 | * necessarily requiring a wireless adapter; |
| 321 | * (g) composite device setup that can create one or more wireless |
| 322 | * configs, including appropriate association setup support; |
| 323 | * (h) more, TBD. |
| 324 | */ |
| 325 | struct usb_composite_dev { |
| 326 | struct usb_gadget *gadget; |
| 327 | struct usb_request *req; |
| 328 | unsigned bufsiz; |
| 329 | |
| 330 | struct usb_configuration *config; |
| 331 | |
Li Jun | 2a16354 | 2021-01-25 21:43:46 +0800 | [diff] [blame^] | 332 | /* OS String is a custom (yet popular) extension to the USB standard. */ |
| 333 | u8 qw_sign[OS_STRING_QW_SIGN_LEN]; |
| 334 | u8 b_vendor_code; |
| 335 | unsigned int use_os_string:1; |
| 336 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 337 | /* private: */ |
| 338 | /* internals */ |
| 339 | unsigned int suspended:1; |
Heiko Schocher | 19bb245 | 2013-06-27 10:04:57 +0200 | [diff] [blame] | 340 | struct usb_device_descriptor __aligned(CONFIG_SYS_CACHELINE_SIZE) desc; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 341 | struct list_head configs; |
| 342 | struct usb_composite_driver *driver; |
| 343 | u8 next_string_id; |
| 344 | |
| 345 | /* the gadget driver won't enable the data pullup |
| 346 | * while the deactivation count is nonzero. |
| 347 | */ |
| 348 | unsigned deactivations; |
| 349 | }; |
| 350 | |
| 351 | extern int usb_string_id(struct usb_composite_dev *c); |
| 352 | extern int usb_string_ids_tab(struct usb_composite_dev *c, |
| 353 | struct usb_string *str); |
| 354 | extern int usb_string_ids_n(struct usb_composite_dev *c, unsigned n); |
| 355 | |
| 356 | #endif /* __LINUX_USB_COMPOSITE_H */ |