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.c - infrastructure for Composite USB Gadgets |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 David Brownell |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 6 | * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com> |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 7 | */ |
| 8 | #undef DEBUG |
| 9 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <dm/devres.h> |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 13 | #include <linux/bug.h> |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 14 | #include <linux/usb/composite.h> |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 15 | #include "u_os_desc.h" |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 16 | |
| 17 | #define USB_BUFSIZ 4096 |
| 18 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 19 | /* Helper type for accessing packed u16 pointers */ |
| 20 | typedef struct { __le16 val; } __packed __le16_packed; |
| 21 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 22 | static struct usb_composite_driver *composite; |
Li Jun | 62936de | 2021-01-25 21:43:50 +0800 | [diff] [blame] | 23 | static struct usb_configuration *os_desc_config; |
| 24 | |
| 25 | /* Microsoft OS String Descriptor */ |
| 26 | static char qw_sign_buf[OS_STRING_QW_SIGN_LEN / 2] = {'M', 'S', 'F', 'T', '1', '0', '0'}; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 27 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 28 | static inline void le16_add_cpu_packed(__le16_packed *var, u16 val) |
| 29 | { |
| 30 | var->val = cpu_to_le16(le16_to_cpu(var->val) + val); |
| 31 | } |
| 32 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 33 | /** |
Li Jun | 2a16354 | 2021-01-25 21:43:46 +0800 | [diff] [blame] | 34 | * struct usb_os_string - represents OS String to be reported by a gadget |
| 35 | * @bLength: total length of the entire descritor, always 0x12 |
| 36 | * @bDescriptorType: USB_DT_STRING |
| 37 | * @qwSignature: the OS String proper |
| 38 | * @bMS_VendorCode: code used by the host for subsequent requests |
| 39 | * @bPad: not used, must be zero |
| 40 | */ |
| 41 | struct usb_os_string { |
| 42 | __u8 bLength; |
| 43 | __u8 bDescriptorType; |
| 44 | __u8 qwSignature[OS_STRING_QW_SIGN_LEN]; |
| 45 | __u8 bMS_VendorCode; |
| 46 | __u8 bPad; |
| 47 | } __packed; |
| 48 | |
| 49 | /** |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 50 | * usb_add_function() - add a function to a configuration |
| 51 | * @config: the configuration |
| 52 | * @function: the function being added |
| 53 | * Context: single threaded during gadget setup |
| 54 | * |
| 55 | * After initialization, each configuration must have one or more |
| 56 | * functions added to it. Adding a function involves calling its @bind() |
| 57 | * method to allocate resources such as interface and string identifiers |
| 58 | * and endpoints. |
| 59 | * |
| 60 | * This function returns the value of the function's bind(), which is |
| 61 | * zero for success else a negative errno value. |
| 62 | */ |
| 63 | int usb_add_function(struct usb_configuration *config, |
| 64 | struct usb_function *function) |
| 65 | { |
| 66 | int value = -EINVAL; |
| 67 | |
| 68 | debug("adding '%s'/%p to config '%s'/%p\n", |
| 69 | function->name, function, |
| 70 | config->label, config); |
| 71 | |
| 72 | if (!function->set_alt || !function->disable) |
| 73 | goto done; |
| 74 | |
| 75 | function->config = config; |
| 76 | list_add_tail(&function->list, &config->functions); |
| 77 | |
| 78 | if (function->bind) { |
| 79 | value = function->bind(config, function); |
| 80 | if (value < 0) { |
| 81 | list_del(&function->list); |
| 82 | function->config = NULL; |
| 83 | } |
| 84 | } else |
| 85 | value = 0; |
| 86 | |
| 87 | if (!config->fullspeed && function->descriptors) |
| 88 | config->fullspeed = 1; |
| 89 | if (!config->highspeed && function->hs_descriptors) |
| 90 | config->highspeed = 1; |
| 91 | |
| 92 | done: |
| 93 | if (value) |
| 94 | debug("adding '%s'/%p --> %d\n", |
| 95 | function->name, function, value); |
| 96 | return value; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * usb_function_deactivate - prevent function and gadget enumeration |
| 101 | * @function: the function that isn't yet ready to respond |
| 102 | * |
| 103 | * Blocks response of the gadget driver to host enumeration by |
| 104 | * preventing the data line pullup from being activated. This is |
| 105 | * normally called during @bind() processing to change from the |
| 106 | * initial "ready to respond" state, or when a required resource |
| 107 | * becomes available. |
| 108 | * |
| 109 | * For example, drivers that serve as a passthrough to a userspace |
| 110 | * daemon can block enumeration unless that daemon (such as an OBEX, |
| 111 | * MTP, or print server) is ready to handle host requests. |
| 112 | * |
| 113 | * Not all systems support software control of their USB peripheral |
| 114 | * data pullups. |
| 115 | * |
| 116 | * Returns zero on success, else negative errno. |
| 117 | */ |
| 118 | int usb_function_deactivate(struct usb_function *function) |
| 119 | { |
| 120 | struct usb_composite_dev *cdev = function->config->cdev; |
| 121 | int status = 0; |
| 122 | |
| 123 | if (cdev->deactivations == 0) |
| 124 | status = usb_gadget_disconnect(cdev->gadget); |
| 125 | if (status == 0) |
| 126 | cdev->deactivations++; |
| 127 | |
| 128 | return status; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * usb_function_activate - allow function and gadget enumeration |
| 133 | * @function: function on which usb_function_activate() was called |
| 134 | * |
| 135 | * Reverses effect of usb_function_deactivate(). If no more functions |
| 136 | * are delaying their activation, the gadget driver will respond to |
| 137 | * host enumeration procedures. |
| 138 | * |
| 139 | * Returns zero on success, else negative errno. |
| 140 | */ |
| 141 | int usb_function_activate(struct usb_function *function) |
| 142 | { |
| 143 | struct usb_composite_dev *cdev = function->config->cdev; |
| 144 | int status = 0; |
| 145 | |
| 146 | if (cdev->deactivations == 0) |
| 147 | status = -EINVAL; |
| 148 | else { |
| 149 | cdev->deactivations--; |
| 150 | if (cdev->deactivations == 0) |
| 151 | status = usb_gadget_connect(cdev->gadget); |
| 152 | } |
| 153 | |
| 154 | return status; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * usb_interface_id() - allocate an unused interface ID |
| 159 | * @config: configuration associated with the interface |
| 160 | * @function: function handling the interface |
| 161 | * Context: single threaded during gadget setup |
| 162 | * |
| 163 | * usb_interface_id() is called from usb_function.bind() callbacks to |
| 164 | * allocate new interface IDs. The function driver will then store that |
| 165 | * ID in interface, association, CDC union, and other descriptors. It |
| 166 | * will also handle any control requests targetted at that interface, |
| 167 | * particularly changing its altsetting via set_alt(). There may |
| 168 | * also be class-specific or vendor-specific requests to handle. |
| 169 | * |
| 170 | * All interface identifier should be allocated using this routine, to |
| 171 | * ensure that for example different functions don't wrongly assign |
| 172 | * different meanings to the same identifier. Note that since interface |
| 173 | * identifers are configuration-specific, functions used in more than |
| 174 | * one configuration (or more than once in a given configuration) need |
| 175 | * multiple versions of the relevant descriptors. |
| 176 | * |
| 177 | * Returns the interface ID which was allocated; or -ENODEV if no |
| 178 | * more interface IDs can be allocated. |
| 179 | */ |
| 180 | int usb_interface_id(struct usb_configuration *config, |
| 181 | struct usb_function *function) |
| 182 | { |
| 183 | unsigned char id = config->next_interface_id; |
| 184 | |
| 185 | if (id < MAX_CONFIG_INTERFACES) { |
| 186 | config->interface[id] = function; |
| 187 | config->next_interface_id = id + 1; |
| 188 | return id; |
| 189 | } |
| 190 | return -ENODEV; |
| 191 | } |
| 192 | |
| 193 | static int config_buf(struct usb_configuration *config, |
| 194 | enum usb_device_speed speed, void *buf, u8 type) |
| 195 | { |
| 196 | int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; |
| 197 | void *next = buf + USB_DT_CONFIG_SIZE; |
| 198 | struct usb_descriptor_header **descriptors; |
Heinrich Schuchardt | 629c3b4 | 2018-03-18 13:05:58 +0100 | [diff] [blame] | 199 | struct usb_config_descriptor *c; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 200 | int status; |
| 201 | struct usb_function *f; |
| 202 | |
| 203 | /* write the config descriptor */ |
| 204 | c = buf; |
| 205 | c->bLength = USB_DT_CONFIG_SIZE; |
| 206 | c->bDescriptorType = type; |
| 207 | |
| 208 | c->bNumInterfaces = config->next_interface_id; |
| 209 | c->bConfigurationValue = config->bConfigurationValue; |
| 210 | c->iConfiguration = config->iConfiguration; |
| 211 | c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; |
| 212 | c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); |
| 213 | |
| 214 | /* There may be e.g. OTG descriptors */ |
| 215 | if (config->descriptors) { |
| 216 | status = usb_descriptor_fillbuf(next, len, |
| 217 | config->descriptors); |
| 218 | if (status < 0) |
| 219 | return status; |
| 220 | len -= status; |
| 221 | next += status; |
| 222 | } |
| 223 | |
| 224 | /* add each function's descriptors */ |
| 225 | list_for_each_entry(f, &config->functions, list) { |
| 226 | if (speed == USB_SPEED_HIGH) |
| 227 | descriptors = f->hs_descriptors; |
| 228 | else |
| 229 | descriptors = f->descriptors; |
| 230 | if (!descriptors) |
| 231 | continue; |
| 232 | status = usb_descriptor_fillbuf(next, len, |
| 233 | (const struct usb_descriptor_header **) descriptors); |
| 234 | if (status < 0) |
| 235 | return status; |
| 236 | len -= status; |
| 237 | next += status; |
| 238 | } |
| 239 | |
| 240 | len = next - buf; |
| 241 | c->wTotalLength = cpu_to_le16(len); |
| 242 | return len; |
| 243 | } |
| 244 | |
| 245 | static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) |
| 246 | { |
| 247 | enum usb_device_speed speed = USB_SPEED_UNKNOWN; |
| 248 | struct usb_gadget *gadget = cdev->gadget; |
| 249 | u8 type = w_value >> 8; |
| 250 | int hs = 0; |
| 251 | struct usb_configuration *c; |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 252 | struct list_head *pos; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 253 | |
| 254 | if (gadget_is_dualspeed(gadget)) { |
| 255 | if (gadget->speed == USB_SPEED_HIGH) |
| 256 | hs = 1; |
| 257 | if (type == USB_DT_OTHER_SPEED_CONFIG) |
| 258 | hs = !hs; |
| 259 | if (hs) |
| 260 | speed = USB_SPEED_HIGH; |
| 261 | } |
| 262 | |
| 263 | w_value &= 0xff; |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 264 | |
| 265 | pos = &cdev->configs; |
| 266 | c = cdev->os_desc_config; |
| 267 | if (c) |
| 268 | goto check_config; |
| 269 | |
| 270 | while ((pos = pos->next) != &cdev->configs) { |
| 271 | c = list_entry(pos, typeof(*c), list); |
| 272 | |
| 273 | /* skip OS Descriptors config which is handled separately */ |
| 274 | if (c == cdev->os_desc_config) |
| 275 | continue; |
| 276 | |
| 277 | check_config: |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 278 | if (speed == USB_SPEED_HIGH) { |
| 279 | if (!c->highspeed) |
| 280 | continue; |
| 281 | } else { |
| 282 | if (!c->fullspeed) |
| 283 | continue; |
| 284 | } |
| 285 | if (w_value == 0) |
| 286 | return config_buf(c, speed, cdev->req->buf, type); |
| 287 | w_value--; |
| 288 | } |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | |
| 292 | static int count_configs(struct usb_composite_dev *cdev, unsigned type) |
| 293 | { |
| 294 | struct usb_gadget *gadget = cdev->gadget; |
| 295 | unsigned count = 0; |
| 296 | int hs = 0; |
| 297 | struct usb_configuration *c; |
| 298 | |
| 299 | if (gadget_is_dualspeed(gadget)) { |
| 300 | if (gadget->speed == USB_SPEED_HIGH) |
| 301 | hs = 1; |
| 302 | if (type == USB_DT_DEVICE_QUALIFIER) |
| 303 | hs = !hs; |
| 304 | } |
| 305 | list_for_each_entry(c, &cdev->configs, list) { |
| 306 | /* ignore configs that won't work at this speed */ |
| 307 | if (hs) { |
| 308 | if (!c->highspeed) |
| 309 | continue; |
| 310 | } else { |
| 311 | if (!c->fullspeed) |
| 312 | continue; |
| 313 | } |
| 314 | count++; |
| 315 | } |
| 316 | return count; |
| 317 | } |
| 318 | |
| 319 | static void device_qual(struct usb_composite_dev *cdev) |
| 320 | { |
| 321 | struct usb_qualifier_descriptor *qual = cdev->req->buf; |
| 322 | |
| 323 | qual->bLength = sizeof(*qual); |
| 324 | qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; |
| 325 | /* POLICY: same bcdUSB and device type info at both speeds */ |
| 326 | qual->bcdUSB = cdev->desc.bcdUSB; |
| 327 | qual->bDeviceClass = cdev->desc.bDeviceClass; |
| 328 | qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; |
| 329 | qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; |
| 330 | /* ASSUME same EP0 fifo size at both speeds */ |
Kishon Vijay Abraham I | 58d701e | 2015-02-23 18:40:17 +0530 | [diff] [blame] | 331 | qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 332 | qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); |
| 333 | qual->bRESERVED = 0; |
| 334 | } |
| 335 | |
| 336 | static void reset_config(struct usb_composite_dev *cdev) |
| 337 | { |
| 338 | struct usb_function *f; |
| 339 | |
| 340 | debug("%s:\n", __func__); |
| 341 | |
| 342 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 343 | if (f->disable) |
| 344 | f->disable(f); |
| 345 | |
| 346 | bitmap_zero(f->endpoints, 32); |
| 347 | } |
| 348 | cdev->config = NULL; |
| 349 | } |
| 350 | |
| 351 | static int set_config(struct usb_composite_dev *cdev, |
| 352 | const struct usb_ctrlrequest *ctrl, unsigned number) |
| 353 | { |
| 354 | struct usb_gadget *gadget = cdev->gadget; |
| 355 | unsigned power = gadget_is_otg(gadget) ? 8 : 100; |
| 356 | struct usb_descriptor_header **descriptors; |
| 357 | int result = -EINVAL; |
| 358 | struct usb_endpoint_descriptor *ep; |
| 359 | struct usb_configuration *c = NULL; |
| 360 | int addr; |
| 361 | int tmp; |
| 362 | struct usb_function *f; |
| 363 | |
| 364 | if (cdev->config) |
| 365 | reset_config(cdev); |
| 366 | |
| 367 | if (number) { |
| 368 | list_for_each_entry(c, &cdev->configs, list) { |
| 369 | if (c->bConfigurationValue == number) { |
| 370 | result = 0; |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | if (result < 0) |
| 375 | goto done; |
| 376 | } else |
| 377 | result = 0; |
| 378 | |
| 379 | debug("%s: %s speed config #%d: %s\n", __func__, |
| 380 | ({ char *speed; |
| 381 | switch (gadget->speed) { |
| 382 | case USB_SPEED_LOW: |
| 383 | speed = "low"; |
| 384 | break; |
| 385 | case USB_SPEED_FULL: |
| 386 | speed = "full"; |
| 387 | break; |
| 388 | case USB_SPEED_HIGH: |
| 389 | speed = "high"; |
| 390 | break; |
| 391 | default: |
| 392 | speed = "?"; |
| 393 | break; |
| 394 | }; |
| 395 | speed; |
| 396 | }), number, c ? c->label : "unconfigured"); |
| 397 | |
| 398 | if (!c) |
| 399 | goto done; |
| 400 | |
| 401 | cdev->config = c; |
| 402 | |
| 403 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 404 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 405 | f = c->interface[tmp]; |
| 406 | if (!f) |
| 407 | break; |
| 408 | |
| 409 | /* |
| 410 | * Record which endpoints are used by the function. This is used |
| 411 | * to dispatch control requests targeted at that endpoint to the |
| 412 | * function's setup callback instead of the current |
| 413 | * configuration's setup callback. |
| 414 | */ |
| 415 | if (gadget->speed == USB_SPEED_HIGH) |
| 416 | descriptors = f->hs_descriptors; |
| 417 | else |
| 418 | descriptors = f->descriptors; |
| 419 | |
| 420 | for (; *descriptors; ++descriptors) { |
| 421 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 422 | continue; |
| 423 | |
| 424 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 425 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 426 | | (ep->bEndpointAddress & 0x0f); |
Bryan O'Donoghue | 0b88876 | 2018-04-30 15:56:10 +0100 | [diff] [blame] | 427 | generic_set_bit(addr, f->endpoints); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | result = f->set_alt(f, tmp, 0); |
| 431 | if (result < 0) { |
| 432 | debug("interface %d (%s/%p) alt 0 --> %d\n", |
| 433 | tmp, f->name, f, result); |
| 434 | |
| 435 | reset_config(cdev); |
| 436 | goto done; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /* when we return, be sure our power usage is valid */ |
| 441 | power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; |
| 442 | done: |
| 443 | usb_gadget_vbus_draw(gadget, power); |
| 444 | return result; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * usb_add_config() - add a configuration to a device. |
| 449 | * @cdev: wraps the USB gadget |
| 450 | * @config: the configuration, with bConfigurationValue assigned |
| 451 | * Context: single threaded during gadget setup |
| 452 | * |
| 453 | * One of the main tasks of a composite driver's bind() routine is to |
| 454 | * add each of the configurations it supports, using this routine. |
| 455 | * |
| 456 | * This function returns the value of the configuration's bind(), which |
| 457 | * is zero for success else a negative errno value. Binding configurations |
| 458 | * assigns global resources including string IDs, and per-configuration |
| 459 | * resources such as interface IDs and endpoints. |
| 460 | */ |
| 461 | int usb_add_config(struct usb_composite_dev *cdev, |
| 462 | struct usb_configuration *config) |
| 463 | { |
| 464 | int status = -EINVAL; |
| 465 | struct usb_configuration *c; |
| 466 | struct usb_function *f; |
| 467 | unsigned int i; |
| 468 | |
| 469 | debug("%s: adding config #%u '%s'/%p\n", __func__, |
| 470 | config->bConfigurationValue, |
| 471 | config->label, config); |
| 472 | |
| 473 | if (!config->bConfigurationValue || !config->bind) |
| 474 | goto done; |
| 475 | |
| 476 | /* Prevent duplicate configuration identifiers */ |
| 477 | list_for_each_entry(c, &cdev->configs, list) { |
| 478 | if (c->bConfigurationValue == config->bConfigurationValue) { |
| 479 | status = -EBUSY; |
| 480 | goto done; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | config->cdev = cdev; |
| 485 | list_add_tail(&config->list, &cdev->configs); |
| 486 | |
| 487 | INIT_LIST_HEAD(&config->functions); |
| 488 | config->next_interface_id = 0; |
| 489 | |
| 490 | status = config->bind(config); |
| 491 | if (status < 0) { |
| 492 | list_del(&config->list); |
| 493 | config->cdev = NULL; |
| 494 | } else { |
| 495 | debug("cfg %d/%p speeds:%s%s\n", |
| 496 | config->bConfigurationValue, config, |
| 497 | config->highspeed ? " high" : "", |
| 498 | config->fullspeed |
| 499 | ? (gadget_is_dualspeed(cdev->gadget) |
| 500 | ? " full" |
| 501 | : " full/low") |
| 502 | : ""); |
| 503 | |
| 504 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 505 | f = config->interface[i]; |
| 506 | if (!f) |
| 507 | continue; |
| 508 | debug("%s: interface %d = %s/%p\n", |
| 509 | __func__, i, f->name, f); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | usb_ep_autoconfig_reset(cdev->gadget); |
| 514 | |
Jun Li | be4a943 | 2021-01-25 21:43:52 +0800 | [diff] [blame^] | 515 | if (os_desc_config) |
| 516 | cdev->os_desc_config = os_desc_config; |
| 517 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 518 | done: |
| 519 | if (status) |
| 520 | debug("added config '%s'/%u --> %d\n", config->label, |
| 521 | config->bConfigurationValue, status); |
| 522 | return status; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * We support strings in multiple languages ... string descriptor zero |
| 527 | * says which languages are supported. The typical case will be that |
| 528 | * only one language (probably English) is used, with I18N handled on |
| 529 | * the host side. |
| 530 | */ |
| 531 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 532 | static void collect_langs(struct usb_gadget_strings **sp, void *buf) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 533 | { |
| 534 | const struct usb_gadget_strings *s; |
| 535 | u16 language; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 536 | __le16_packed *tmp; |
| 537 | __le16_packed *end = (buf + 252); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 538 | |
| 539 | while (*sp) { |
| 540 | s = *sp; |
| 541 | language = cpu_to_le16(s->language); |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 542 | for (tmp = buf; tmp->val && tmp < end; tmp++) { |
| 543 | if (tmp->val == language) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 544 | goto repeat; |
| 545 | } |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 546 | tmp->val = language; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 547 | repeat: |
| 548 | sp++; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static int lookup_string( |
| 553 | struct usb_gadget_strings **sp, |
| 554 | void *buf, |
| 555 | u16 language, |
| 556 | int id |
| 557 | ) |
| 558 | { |
| 559 | int value; |
| 560 | struct usb_gadget_strings *s; |
| 561 | |
| 562 | while (*sp) { |
| 563 | s = *sp++; |
| 564 | if (s->language != language) |
| 565 | continue; |
| 566 | value = usb_gadget_get_string(s, id, buf); |
| 567 | if (value > 0) |
| 568 | return value; |
| 569 | } |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | static int get_string(struct usb_composite_dev *cdev, |
| 574 | void *buf, u16 language, int id) |
| 575 | { |
| 576 | struct usb_string_descriptor *s = buf; |
| 577 | struct usb_gadget_strings **sp; |
| 578 | int len; |
| 579 | struct usb_configuration *c; |
| 580 | struct usb_function *f; |
| 581 | |
| 582 | /* |
| 583 | * Yes, not only is USB's I18N support probably more than most |
| 584 | * folk will ever care about ... also, it's all supported here. |
| 585 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 586 | */ |
| 587 | |
| 588 | /* 0 == report all available language codes */ |
| 589 | if (id == 0) { |
| 590 | memset(s, 0, 256); |
| 591 | s->bDescriptorType = USB_DT_STRING; |
| 592 | |
| 593 | sp = composite->strings; |
| 594 | if (sp) |
| 595 | collect_langs(sp, s->wData); |
| 596 | |
| 597 | list_for_each_entry(c, &cdev->configs, list) { |
| 598 | sp = c->strings; |
| 599 | if (sp) |
| 600 | collect_langs(sp, s->wData); |
| 601 | |
| 602 | list_for_each_entry(f, &c->functions, list) { |
| 603 | sp = f->strings; |
| 604 | if (sp) |
| 605 | collect_langs(sp, s->wData); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | for (len = 0; len <= 126 && s->wData[len]; len++) |
| 610 | continue; |
| 611 | if (!len) |
| 612 | return -EINVAL; |
| 613 | |
| 614 | s->bLength = 2 * (len + 1); |
| 615 | return s->bLength; |
| 616 | } |
| 617 | |
Li Jun | 2a16354 | 2021-01-25 21:43:46 +0800 | [diff] [blame] | 618 | if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) { |
| 619 | struct usb_os_string *b = buf; |
| 620 | b->bLength = sizeof(*b); |
| 621 | b->bDescriptorType = USB_DT_STRING; |
| 622 | memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature)); |
| 623 | b->bMS_VendorCode = cdev->b_vendor_code; |
| 624 | b->bPad = 0; |
| 625 | return sizeof(*b); |
| 626 | } |
| 627 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 628 | /* |
| 629 | * Otherwise, look up and return a specified string. String IDs |
| 630 | * are device-scoped, so we look up each string table we're told |
| 631 | * about. These lookups are infrequent; simpler-is-better here. |
| 632 | */ |
| 633 | if (composite->strings) { |
| 634 | len = lookup_string(composite->strings, buf, language, id); |
| 635 | if (len > 0) |
| 636 | return len; |
| 637 | } |
| 638 | list_for_each_entry(c, &cdev->configs, list) { |
| 639 | if (c->strings) { |
| 640 | len = lookup_string(c->strings, buf, language, id); |
| 641 | if (len > 0) |
| 642 | return len; |
| 643 | } |
| 644 | list_for_each_entry(f, &c->functions, list) { |
| 645 | if (!f->strings) |
| 646 | continue; |
| 647 | len = lookup_string(f->strings, buf, language, id); |
| 648 | if (len > 0) |
| 649 | return len; |
| 650 | } |
| 651 | } |
| 652 | return -EINVAL; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * usb_string_id() - allocate an unused string ID |
| 657 | * @cdev: the device whose string descriptor IDs are being allocated |
| 658 | * Context: single threaded during gadget setup |
| 659 | * |
| 660 | * @usb_string_id() is called from bind() callbacks to allocate |
| 661 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 662 | * then store that ID in the appropriate descriptors and string table. |
| 663 | * |
| 664 | * All string identifier should be allocated using this, |
| 665 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 666 | * that for example different functions don't wrongly assign different |
| 667 | * meanings to the same identifier. |
| 668 | */ |
| 669 | int usb_string_id(struct usb_composite_dev *cdev) |
| 670 | { |
| 671 | if (cdev->next_string_id < 254) { |
| 672 | /* |
| 673 | * string id 0 is reserved by USB spec for list of |
| 674 | * supported languages |
| 675 | * 255 reserved as well? -- mina86 |
| 676 | */ |
| 677 | cdev->next_string_id++; |
| 678 | return cdev->next_string_id; |
| 679 | } |
| 680 | return -ENODEV; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * usb_string_ids() - allocate unused string IDs in batch |
| 685 | * @cdev: the device whose string descriptor IDs are being allocated |
| 686 | * @str: an array of usb_string objects to assign numbers to |
| 687 | * Context: single threaded during gadget setup |
| 688 | * |
| 689 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 690 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 691 | * then copy IDs from the string table to the appropriate descriptors |
| 692 | * and string table for other languages. |
| 693 | * |
| 694 | * All string identifier should be allocated using this, |
| 695 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 696 | * example different functions don't wrongly assign different meanings |
| 697 | * to the same identifier. |
| 698 | */ |
| 699 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 700 | { |
| 701 | u8 next = cdev->next_string_id; |
| 702 | |
| 703 | for (; str->s; ++str) { |
| 704 | if (next >= 254) |
| 705 | return -ENODEV; |
| 706 | str->id = ++next; |
| 707 | } |
| 708 | |
| 709 | cdev->next_string_id = next; |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * usb_string_ids_n() - allocate unused string IDs in batch |
| 716 | * @c: the device whose string descriptor IDs are being allocated |
| 717 | * @n: number of string IDs to allocate |
| 718 | * Context: single threaded during gadget setup |
| 719 | * |
| 720 | * Returns the first requested ID. This ID and next @n-1 IDs are now |
| 721 | * valid IDs. At least provided that @n is non-zero because if it |
| 722 | * is, returns last requested ID which is now very useful information. |
| 723 | * |
| 724 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 725 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 726 | * then store that ID in the appropriate descriptors and string table. |
| 727 | * |
| 728 | * All string identifier should be allocated using this, |
| 729 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 730 | * example different functions don't wrongly assign different meanings |
| 731 | * to the same identifier. |
| 732 | */ |
| 733 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 734 | { |
| 735 | u8 next = c->next_string_id; |
| 736 | |
| 737 | if (n > 254 || next + n > 254) |
| 738 | return -ENODEV; |
| 739 | |
| 740 | c->next_string_id += n; |
| 741 | return next + 1; |
| 742 | } |
| 743 | |
| 744 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 745 | { |
| 746 | if (req->status || req->actual != req->length) |
| 747 | debug("%s: setup complete --> %d, %d/%d\n", __func__, |
| 748 | req->status, req->actual, req->length); |
| 749 | } |
| 750 | |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 751 | static int bos_desc(struct usb_composite_dev *cdev) |
| 752 | { |
| 753 | struct usb_ext_cap_descriptor *usb_ext; |
| 754 | struct usb_bos_descriptor *bos = cdev->req->buf; |
| 755 | |
| 756 | bos->bLength = USB_DT_BOS_SIZE; |
| 757 | bos->bDescriptorType = USB_DT_BOS; |
| 758 | |
| 759 | bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); |
| 760 | bos->bNumDeviceCaps = 0; |
| 761 | |
| 762 | /* |
| 763 | * A SuperSpeed device shall include the USB2.0 extension descriptor |
| 764 | * and shall support LPM when operating in USB2.0 HS mode. |
| 765 | */ |
| 766 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 767 | bos->bNumDeviceCaps++; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 768 | le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, |
| 769 | USB_DT_USB_EXT_CAP_SIZE); |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 770 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; |
| 771 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 772 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; |
| 773 | usb_ext->bmAttributes = |
| 774 | cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT); |
| 775 | |
| 776 | /* |
| 777 | * The Superspeed USB Capability descriptor shall be implemented |
| 778 | * by all SuperSpeed devices. |
| 779 | */ |
| 780 | if (gadget_is_superspeed(cdev->gadget)) { |
| 781 | struct usb_ss_cap_descriptor *ss_cap; |
| 782 | |
| 783 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 784 | bos->bNumDeviceCaps++; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 785 | le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, |
| 786 | USB_DT_USB_SS_CAP_SIZE); |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 787 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; |
| 788 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 789 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; |
| 790 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ |
| 791 | ss_cap->wSpeedSupported = |
| 792 | cpu_to_le16(USB_LOW_SPEED_OPERATION | |
| 793 | USB_FULL_SPEED_OPERATION | |
| 794 | USB_HIGH_SPEED_OPERATION | |
| 795 | USB_5GBPS_OPERATION); |
| 796 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; |
| 797 | ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; |
| 798 | ss_cap->bU2DevExitLat = |
| 799 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
| 800 | } |
| 801 | return le16_to_cpu(bos->wTotalLength); |
| 802 | } |
| 803 | |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 804 | static int count_ext_compat(struct usb_configuration *c) |
| 805 | { |
| 806 | int i, res; |
| 807 | |
| 808 | res = 0; |
| 809 | for (i = 0; i < c->next_interface_id; ++i) { |
| 810 | struct usb_function *f; |
| 811 | int j; |
| 812 | |
| 813 | f = c->interface[i]; |
| 814 | for (j = 0; j < f->os_desc_n; ++j) { |
| 815 | struct usb_os_desc *d; |
| 816 | |
| 817 | if (i != f->os_desc_table[j].if_id) |
| 818 | continue; |
| 819 | d = f->os_desc_table[j].os_desc; |
| 820 | if (d && d->ext_compat_id) |
| 821 | ++res; |
| 822 | } |
| 823 | } |
| 824 | BUG_ON(res > 255); |
| 825 | return res; |
| 826 | } |
| 827 | |
| 828 | static void fill_ext_compat(struct usb_configuration *c, u8 *buf) |
| 829 | { |
| 830 | int i, count; |
| 831 | |
| 832 | count = 16; |
| 833 | for (i = 0; i < c->next_interface_id; ++i) { |
| 834 | struct usb_function *f; |
| 835 | int j; |
| 836 | |
| 837 | f = c->interface[i]; |
| 838 | for (j = 0; j < f->os_desc_n; ++j) { |
| 839 | struct usb_os_desc *d; |
| 840 | |
| 841 | if (i != f->os_desc_table[j].if_id) |
| 842 | continue; |
| 843 | d = f->os_desc_table[j].os_desc; |
| 844 | if (d && d->ext_compat_id) { |
| 845 | *buf++ = i; |
| 846 | *buf++ = 0x01; |
| 847 | memcpy(buf, d->ext_compat_id, 16); |
| 848 | buf += 22; |
| 849 | } else { |
| 850 | ++buf; |
| 851 | *buf = 0x01; |
| 852 | buf += 23; |
| 853 | } |
| 854 | count += 24; |
| 855 | if (count >= 4096) |
| 856 | return; |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static int count_ext_prop(struct usb_configuration *c, int interface) |
| 862 | { |
| 863 | struct usb_function *f; |
| 864 | int j; |
| 865 | |
| 866 | f = c->interface[interface]; |
| 867 | for (j = 0; j < f->os_desc_n; ++j) { |
| 868 | struct usb_os_desc *d; |
| 869 | |
| 870 | if (interface != f->os_desc_table[j].if_id) |
| 871 | continue; |
| 872 | d = f->os_desc_table[j].os_desc; |
| 873 | if (d && d->ext_compat_id) |
| 874 | return d->ext_prop_count; |
| 875 | } |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | static int len_ext_prop(struct usb_configuration *c, int interface) |
| 880 | { |
| 881 | struct usb_function *f; |
| 882 | struct usb_os_desc *d; |
| 883 | int j, res; |
| 884 | |
| 885 | res = 10; /* header length */ |
| 886 | f = c->interface[interface]; |
| 887 | for (j = 0; j < f->os_desc_n; ++j) { |
| 888 | if (interface != f->os_desc_table[j].if_id) |
| 889 | continue; |
| 890 | d = f->os_desc_table[j].os_desc; |
| 891 | if (d) |
| 892 | return min(res + d->ext_prop_len, 4096); |
| 893 | } |
| 894 | return res; |
| 895 | } |
| 896 | |
| 897 | static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf) |
| 898 | { |
| 899 | struct usb_function *f; |
| 900 | struct usb_os_desc *d; |
| 901 | struct usb_os_desc_ext_prop *ext_prop; |
| 902 | int j, count, n, ret; |
| 903 | u8 *start = buf; |
| 904 | |
| 905 | f = c->interface[interface]; |
| 906 | for (j = 0; j < f->os_desc_n; ++j) { |
| 907 | if (interface != f->os_desc_table[j].if_id) |
| 908 | continue; |
| 909 | d = f->os_desc_table[j].os_desc; |
| 910 | if (d) |
| 911 | list_for_each_entry(ext_prop, &d->ext_prop, entry) { |
| 912 | /* 4kB minus header length */ |
| 913 | n = buf - start; |
| 914 | if (n >= 4086) |
| 915 | return 0; |
| 916 | |
| 917 | count = ext_prop->data_len + |
| 918 | ext_prop->name_len + 14; |
| 919 | if (count > 4086 - n) |
| 920 | return -EINVAL; |
| 921 | usb_ext_prop_put_size(buf, count); |
| 922 | usb_ext_prop_put_type(buf, ext_prop->type); |
| 923 | ret = usb_ext_prop_put_name(buf, ext_prop->name, |
| 924 | ext_prop->name_len); |
| 925 | if (ret < 0) |
| 926 | return ret; |
| 927 | switch (ext_prop->type) { |
| 928 | case USB_EXT_PROP_UNICODE: |
| 929 | case USB_EXT_PROP_UNICODE_ENV: |
| 930 | case USB_EXT_PROP_UNICODE_LINK: |
| 931 | usb_ext_prop_put_unicode(buf, ret, |
| 932 | ext_prop->data, |
| 933 | ext_prop->data_len); |
| 934 | break; |
| 935 | case USB_EXT_PROP_BINARY: |
| 936 | usb_ext_prop_put_binary(buf, ret, |
| 937 | ext_prop->data, |
| 938 | ext_prop->data_len); |
| 939 | break; |
| 940 | case USB_EXT_PROP_LE32: |
| 941 | /* not implemented */ |
| 942 | case USB_EXT_PROP_BE32: |
| 943 | /* not implemented */ |
| 944 | default: |
| 945 | return -EINVAL; |
| 946 | } |
| 947 | buf += count; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 954 | /* |
| 955 | * The setup() callback implements all the ep0 functionality that's |
| 956 | * not handled lower down, in hardware or the hardware driver(like |
| 957 | * device and endpoint feature flags, and their status). It's all |
| 958 | * housekeeping for the gadget function we're implementing. Most of |
| 959 | * the work is in config and function specific setup. |
| 960 | */ |
| 961 | static int |
| 962 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 963 | { |
| 964 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 965 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 966 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 967 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 968 | u8 intf = w_index & 0xFF; |
| 969 | int value = -EOPNOTSUPP; |
| 970 | struct usb_request *req = cdev->req; |
| 971 | struct usb_function *f = NULL; |
| 972 | int standard; |
| 973 | u8 endp; |
| 974 | struct usb_configuration *c; |
| 975 | |
| 976 | /* |
| 977 | * partial re-init of the response message; the function or the |
| 978 | * gadget might need to intercept e.g. a control-OUT completion |
| 979 | * when we delegate to it. |
| 980 | */ |
| 981 | req->zero = 0; |
| 982 | req->complete = composite_setup_complete; |
| 983 | req->length = USB_BUFSIZ; |
| 984 | gadget->ep0->driver_data = cdev; |
| 985 | standard = (ctrl->bRequestType & USB_TYPE_MASK) |
| 986 | == USB_TYPE_STANDARD; |
| 987 | if (!standard) |
| 988 | goto unknown; |
| 989 | |
| 990 | switch (ctrl->bRequest) { |
| 991 | |
| 992 | /* we handle all standard USB descriptors */ |
| 993 | case USB_REQ_GET_DESCRIPTOR: |
| 994 | if (ctrl->bRequestType != USB_DIR_IN) |
| 995 | goto unknown; |
| 996 | switch (w_value >> 8) { |
| 997 | |
| 998 | case USB_DT_DEVICE: |
| 999 | cdev->desc.bNumConfigurations = |
| 1000 | count_configs(cdev, USB_DT_DEVICE); |
Siva Durga Prasad Paladugu | c7c0718 | 2018-12-13 15:16:36 +0530 | [diff] [blame] | 1001 | |
| 1002 | /* |
| 1003 | * If the speed is Super speed, then the supported |
| 1004 | * max packet size is 512 and it should be sent as |
| 1005 | * exponent of 2. So, 9(2^9=512) should be filled in |
| 1006 | * bMaxPacketSize0. Also fill USB version as 3.0 |
| 1007 | * if speed is Super speed. |
| 1008 | */ |
| 1009 | if (cdev->gadget->speed == USB_SPEED_SUPER) { |
| 1010 | cdev->desc.bMaxPacketSize0 = 9; |
| 1011 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
| 1012 | } else { |
| 1013 | cdev->desc.bMaxPacketSize0 = |
| 1014 | cdev->gadget->ep0->maxpacket; |
| 1015 | } |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1016 | value = min(w_length, (u16) sizeof cdev->desc); |
| 1017 | memcpy(req->buf, &cdev->desc, value); |
| 1018 | break; |
| 1019 | case USB_DT_DEVICE_QUALIFIER: |
| 1020 | if (!gadget_is_dualspeed(gadget)) |
| 1021 | break; |
| 1022 | device_qual(cdev); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 1023 | value = min_t(int, w_length, |
| 1024 | sizeof(struct usb_qualifier_descriptor)); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1025 | break; |
| 1026 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1027 | if (!gadget_is_dualspeed(gadget)) |
| 1028 | break; |
| 1029 | |
| 1030 | case USB_DT_CONFIG: |
| 1031 | value = config_desc(cdev, w_value); |
| 1032 | if (value >= 0) |
| 1033 | value = min(w_length, (u16) value); |
| 1034 | break; |
| 1035 | case USB_DT_STRING: |
| 1036 | value = get_string(cdev, req->buf, |
| 1037 | w_index, w_value & 0xff); |
| 1038 | if (value >= 0) |
| 1039 | value = min(w_length, (u16) value); |
| 1040 | break; |
Stefan Roese | a56fbcc | 2015-01-09 14:54:55 +0100 | [diff] [blame] | 1041 | case USB_DT_BOS: |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 1042 | if (gadget_is_superspeed(cdev->gadget)) |
| 1043 | value = bos_desc(cdev); |
| 1044 | if (value >= 0) |
| 1045 | value = min(w_length, (u16)value); |
Stefan Roese | a56fbcc | 2015-01-09 14:54:55 +0100 | [diff] [blame] | 1046 | break; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1047 | default: |
| 1048 | goto unknown; |
| 1049 | } |
| 1050 | break; |
| 1051 | |
| 1052 | /* any number of configs can work */ |
| 1053 | case USB_REQ_SET_CONFIGURATION: |
| 1054 | if (ctrl->bRequestType != 0) |
| 1055 | goto unknown; |
| 1056 | if (gadget_is_otg(gadget)) { |
| 1057 | if (gadget->a_hnp_support) |
| 1058 | debug("HNP available\n"); |
| 1059 | else if (gadget->a_alt_hnp_support) |
| 1060 | debug("HNP on another port\n"); |
| 1061 | else |
| 1062 | debug("HNP inactive\n"); |
| 1063 | } |
| 1064 | |
| 1065 | value = set_config(cdev, ctrl, w_value); |
| 1066 | break; |
| 1067 | case USB_REQ_GET_CONFIGURATION: |
| 1068 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1069 | goto unknown; |
| 1070 | if (cdev->config) |
| 1071 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 1072 | else |
| 1073 | *(u8 *)req->buf = 0; |
| 1074 | value = min(w_length, (u16) 1); |
| 1075 | break; |
| 1076 | |
| 1077 | /* |
| 1078 | * function drivers must handle get/set altsetting; if there's |
| 1079 | * no get() method, we know only altsetting zero works. |
| 1080 | */ |
| 1081 | case USB_REQ_SET_INTERFACE: |
| 1082 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 1083 | goto unknown; |
| 1084 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 1085 | break; |
| 1086 | f = cdev->config->interface[intf]; |
| 1087 | if (!f) |
| 1088 | break; |
| 1089 | if (w_value && !f->set_alt) |
| 1090 | break; |
| 1091 | value = f->set_alt(f, w_index, w_value); |
| 1092 | break; |
| 1093 | case USB_REQ_GET_INTERFACE: |
| 1094 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 1095 | goto unknown; |
| 1096 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 1097 | break; |
| 1098 | f = cdev->config->interface[intf]; |
| 1099 | if (!f) |
| 1100 | break; |
| 1101 | /* lots of interfaces only need altsetting zero... */ |
| 1102 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 1103 | if (value < 0) |
| 1104 | break; |
| 1105 | *((u8 *)req->buf) = value; |
| 1106 | value = min(w_length, (u16) 1); |
| 1107 | break; |
| 1108 | default: |
| 1109 | unknown: |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 1110 | /* |
| 1111 | * OS descriptors handling |
| 1112 | */ |
| 1113 | if (CONFIG_IS_ENABLED(USB_GADGET_OS_DESCRIPTORS) && cdev->use_os_string && |
| 1114 | cdev->os_desc_config && (ctrl->bRequestType & USB_TYPE_VENDOR) && |
| 1115 | ctrl->bRequest == cdev->b_vendor_code) { |
| 1116 | struct usb_configuration *os_desc_cfg; |
| 1117 | u8 *buf; |
| 1118 | int interface; |
| 1119 | int count = 0; |
| 1120 | |
| 1121 | buf = req->buf; |
| 1122 | os_desc_cfg = cdev->os_desc_config; |
| 1123 | memset(buf, 0, w_length); |
| 1124 | buf[5] = 0x01; |
| 1125 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1126 | case USB_RECIP_DEVICE: |
| 1127 | if (w_index != 0x4 || (w_value >> 8)) |
| 1128 | break; |
| 1129 | buf[6] = w_index; |
| 1130 | if (w_length == 0x10) { |
| 1131 | /* Number of ext compat interfaces */ |
| 1132 | count = count_ext_compat(os_desc_cfg); |
| 1133 | buf[8] = count; |
| 1134 | count *= 24; /* 24 B/ext compat desc */ |
| 1135 | count += 16; /* header */ |
| 1136 | put_unaligned_le32(count, buf); |
| 1137 | value = w_length; |
| 1138 | } else { |
| 1139 | /* "extended compatibility ID"s */ |
| 1140 | count = count_ext_compat(os_desc_cfg); |
| 1141 | buf[8] = count; |
| 1142 | count *= 24; /* 24 B/ext compat desc */ |
| 1143 | count += 16; /* header */ |
| 1144 | put_unaligned_le32(count, buf); |
| 1145 | buf += 16; |
| 1146 | fill_ext_compat(os_desc_cfg, buf); |
| 1147 | value = w_length; |
| 1148 | } |
| 1149 | break; |
| 1150 | case USB_RECIP_INTERFACE: |
| 1151 | if (w_index != 0x5 || (w_value >> 8)) |
| 1152 | break; |
| 1153 | interface = w_value & 0xFF; |
| 1154 | buf[6] = w_index; |
| 1155 | if (w_length == 0x0A) { |
| 1156 | count = count_ext_prop(os_desc_cfg, |
| 1157 | interface); |
| 1158 | put_unaligned_le16(count, buf + 8); |
| 1159 | count = len_ext_prop(os_desc_cfg, |
| 1160 | interface); |
| 1161 | put_unaligned_le32(count, buf); |
| 1162 | |
| 1163 | value = w_length; |
| 1164 | } else { |
| 1165 | count = count_ext_prop(os_desc_cfg, |
| 1166 | interface); |
| 1167 | put_unaligned_le16(count, buf + 8); |
| 1168 | count = len_ext_prop(os_desc_cfg, |
| 1169 | interface); |
| 1170 | put_unaligned_le32(count, buf); |
| 1171 | buf += 10; |
| 1172 | value = fill_ext_prop(os_desc_cfg, |
| 1173 | interface, buf); |
| 1174 | if (value < 0) |
| 1175 | return value; |
| 1176 | |
| 1177 | value = w_length; |
| 1178 | } |
| 1179 | break; |
| 1180 | } |
| 1181 | |
| 1182 | if (value >= 0) { |
| 1183 | req->length = value; |
| 1184 | req->zero = value < w_length; |
| 1185 | value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); |
| 1186 | if (value < 0) { |
| 1187 | debug("ep_queue --> %d\n", value); |
| 1188 | req->status = 0; |
| 1189 | composite_setup_complete(gadget->ep0, req); |
| 1190 | } |
| 1191 | } |
| 1192 | return value; |
| 1193 | } |
| 1194 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1195 | debug("non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 1196 | ctrl->bRequestType, ctrl->bRequest, |
| 1197 | w_value, w_index, w_length); |
| 1198 | |
Christophe Kerello | 0c1b62c | 2018-03-15 09:34:17 +0100 | [diff] [blame] | 1199 | if (!cdev->config) |
| 1200 | goto done; |
| 1201 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1202 | /* |
| 1203 | * functions always handle their interfaces and endpoints... |
| 1204 | * punt other recipients (other, WUSB, ...) to the current |
| 1205 | * configuration code. |
| 1206 | */ |
| 1207 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1208 | case USB_RECIP_INTERFACE: |
| 1209 | f = cdev->config->interface[intf]; |
| 1210 | break; |
| 1211 | |
| 1212 | case USB_RECIP_ENDPOINT: |
| 1213 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 1214 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1215 | if (test_bit(endp, f->endpoints)) |
| 1216 | break; |
| 1217 | } |
| 1218 | if (&f->list == &cdev->config->functions) |
| 1219 | f = NULL; |
| 1220 | break; |
Lukasz Majewski | 92d7fb8 | 2013-03-01 15:30:18 +0100 | [diff] [blame] | 1221 | /* |
| 1222 | * dfu-util (version 0.5) sets bmRequestType.Receipent = Device |
| 1223 | * for non-standard request (w_value = 0x21, |
| 1224 | * bRequest = GET_DESCRIPTOR in this case). |
| 1225 | * When only one interface is registered (as it is done now), |
| 1226 | * then this request shall be handled as it was requested for |
| 1227 | * interface. |
| 1228 | * |
| 1229 | * In the below code it is checked if only one interface is |
| 1230 | * present and proper function for it is extracted. Due to that |
| 1231 | * function's setup (f->setup) is called to handle this |
| 1232 | * special non-standard request. |
| 1233 | */ |
| 1234 | case USB_RECIP_DEVICE: |
| 1235 | debug("cdev->config->next_interface_id: %d intf: %d\n", |
| 1236 | cdev->config->next_interface_id, intf); |
| 1237 | if (cdev->config->next_interface_id == 1) |
| 1238 | f = cdev->config->interface[intf]; |
| 1239 | break; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | if (f && f->setup) |
| 1243 | value = f->setup(f, ctrl); |
| 1244 | else { |
| 1245 | c = cdev->config; |
Christophe Kerello | 0c1b62c | 2018-03-15 09:34:17 +0100 | [diff] [blame] | 1246 | if (c->setup) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1247 | value = c->setup(c, ctrl); |
| 1248 | } |
| 1249 | |
| 1250 | goto done; |
| 1251 | } |
| 1252 | |
| 1253 | /* respond with data transfer before status phase? */ |
| 1254 | if (value >= 0) { |
| 1255 | req->length = value; |
| 1256 | req->zero = value < w_length; |
| 1257 | value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); |
| 1258 | if (value < 0) { |
| 1259 | debug("ep_queue --> %d\n", value); |
| 1260 | req->status = 0; |
| 1261 | composite_setup_complete(gadget->ep0, req); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | done: |
| 1266 | /* device either stalls (value < 0) or reports success */ |
| 1267 | return value; |
| 1268 | } |
| 1269 | |
| 1270 | static void composite_disconnect(struct usb_gadget *gadget) |
| 1271 | { |
| 1272 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1273 | |
| 1274 | if (cdev->config) |
| 1275 | reset_config(cdev); |
| 1276 | if (composite->disconnect) |
| 1277 | composite->disconnect(cdev); |
| 1278 | } |
| 1279 | |
| 1280 | static void composite_unbind(struct usb_gadget *gadget) |
| 1281 | { |
| 1282 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1283 | struct usb_configuration *c; |
| 1284 | struct usb_function *f; |
| 1285 | |
| 1286 | /* |
| 1287 | * composite_disconnect() must already have been called |
| 1288 | * by the underlying peripheral controller driver! |
| 1289 | * so there's no i/o concurrency that could affect the |
| 1290 | * state protected by cdev->lock. |
| 1291 | */ |
Simon Glass | b18a960 | 2019-12-29 21:19:12 -0700 | [diff] [blame] | 1292 | #ifdef __UBOOT__ |
| 1293 | assert_noisy(!cdev->config); |
| 1294 | #else |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1295 | BUG_ON(cdev->config); |
Simon Glass | b18a960 | 2019-12-29 21:19:12 -0700 | [diff] [blame] | 1296 | #endif |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1297 | |
| 1298 | while (!list_empty(&cdev->configs)) { |
| 1299 | c = list_first_entry(&cdev->configs, |
| 1300 | struct usb_configuration, list); |
| 1301 | while (!list_empty(&c->functions)) { |
| 1302 | f = list_first_entry(&c->functions, |
| 1303 | struct usb_function, list); |
| 1304 | list_del(&f->list); |
| 1305 | if (f->unbind) { |
| 1306 | debug("unbind function '%s'/%p\n", |
| 1307 | f->name, f); |
| 1308 | f->unbind(c, f); |
| 1309 | } |
| 1310 | } |
| 1311 | list_del(&c->list); |
| 1312 | if (c->unbind) { |
| 1313 | debug("unbind config '%s'/%p\n", c->label, c); |
| 1314 | c->unbind(c); |
| 1315 | } |
Stephen Warren | 1c377dc | 2015-09-04 22:03:42 -0600 | [diff] [blame] | 1316 | free(c); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1317 | } |
| 1318 | if (composite->unbind) |
| 1319 | composite->unbind(cdev); |
| 1320 | |
| 1321 | if (cdev->req) { |
| 1322 | kfree(cdev->req->buf); |
| 1323 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 1324 | } |
| 1325 | kfree(cdev); |
| 1326 | set_gadget_data(gadget, NULL); |
| 1327 | |
| 1328 | composite = NULL; |
| 1329 | } |
| 1330 | |
| 1331 | static int composite_bind(struct usb_gadget *gadget) |
| 1332 | { |
| 1333 | int status = -ENOMEM; |
| 1334 | struct usb_composite_dev *cdev; |
| 1335 | |
| 1336 | cdev = calloc(sizeof *cdev, 1); |
| 1337 | if (!cdev) |
| 1338 | return status; |
| 1339 | |
| 1340 | cdev->gadget = gadget; |
| 1341 | set_gadget_data(gadget, cdev); |
| 1342 | INIT_LIST_HEAD(&cdev->configs); |
| 1343 | |
| 1344 | /* preallocate control response and buffer */ |
| 1345 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 1346 | if (!cdev->req) |
| 1347 | goto fail; |
| 1348 | cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ); |
| 1349 | if (!cdev->req->buf) |
| 1350 | goto fail; |
| 1351 | cdev->req->complete = composite_setup_complete; |
| 1352 | gadget->ep0->driver_data = cdev; |
| 1353 | |
| 1354 | cdev->bufsiz = USB_BUFSIZ; |
| 1355 | cdev->driver = composite; |
| 1356 | |
| 1357 | usb_gadget_set_selfpowered(gadget); |
| 1358 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1359 | |
| 1360 | status = composite->bind(cdev); |
| 1361 | if (status < 0) |
| 1362 | goto fail; |
| 1363 | |
Piotr Wilczek | 7db5de6 | 2013-04-10 14:07:51 +0200 | [diff] [blame] | 1364 | memcpy(&cdev->desc, composite->dev, |
| 1365 | sizeof(struct usb_device_descriptor)); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1366 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 1367 | |
Li Jun | 62936de | 2021-01-25 21:43:50 +0800 | [diff] [blame] | 1368 | if (cdev->use_os_string) { |
| 1369 | /* TODO: Do we want to pass this via platform? */ |
| 1370 | cdev->b_vendor_code = 0x40; |
| 1371 | |
| 1372 | /* Microsoft OS String Descriptor */ |
| 1373 | utf8_to_utf16le(qw_sign_buf, (__le16 *)cdev->qw_sign, |
| 1374 | OS_STRING_QW_SIGN_LEN / 2); |
Li Jun | 62936de | 2021-01-25 21:43:50 +0800 | [diff] [blame] | 1375 | } |
| 1376 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1377 | debug("%s: ready\n", composite->name); |
| 1378 | return 0; |
| 1379 | |
| 1380 | fail: |
| 1381 | composite_unbind(gadget); |
| 1382 | return status; |
| 1383 | } |
| 1384 | |
| 1385 | static void |
| 1386 | composite_suspend(struct usb_gadget *gadget) |
| 1387 | { |
| 1388 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1389 | struct usb_function *f; |
| 1390 | |
| 1391 | debug("%s: suspend\n", __func__); |
| 1392 | if (cdev->config) { |
| 1393 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1394 | if (f->suspend) |
| 1395 | f->suspend(f); |
| 1396 | } |
| 1397 | } |
| 1398 | if (composite->suspend) |
| 1399 | composite->suspend(cdev); |
| 1400 | |
| 1401 | cdev->suspended = 1; |
| 1402 | } |
| 1403 | |
| 1404 | static void |
| 1405 | composite_resume(struct usb_gadget *gadget) |
| 1406 | { |
| 1407 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1408 | struct usb_function *f; |
| 1409 | |
| 1410 | debug("%s: resume\n", __func__); |
| 1411 | if (composite->resume) |
| 1412 | composite->resume(cdev); |
| 1413 | if (cdev->config) { |
| 1414 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1415 | if (f->resume) |
| 1416 | f->resume(f); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | cdev->suspended = 0; |
| 1421 | } |
| 1422 | |
| 1423 | static struct usb_gadget_driver composite_driver = { |
| 1424 | .speed = USB_SPEED_HIGH, |
| 1425 | |
| 1426 | .bind = composite_bind, |
| 1427 | .unbind = composite_unbind, |
| 1428 | |
| 1429 | .setup = composite_setup, |
Lukasz Majewski | d04a012 | 2015-03-03 17:32:07 +0100 | [diff] [blame] | 1430 | .reset = composite_disconnect, |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1431 | .disconnect = composite_disconnect, |
| 1432 | |
| 1433 | .suspend = composite_suspend, |
| 1434 | .resume = composite_resume, |
| 1435 | }; |
| 1436 | |
| 1437 | /** |
| 1438 | * usb_composite_register() - register a composite driver |
| 1439 | * @driver: the driver to register |
| 1440 | * Context: single threaded during gadget setup |
| 1441 | * |
| 1442 | * This function is used to register drivers using the composite driver |
| 1443 | * framework. The return value is zero, or a negative errno value. |
| 1444 | * Those values normally come from the driver's @bind method, which does |
| 1445 | * all the work of setting up the driver to match the hardware. |
| 1446 | * |
| 1447 | * On successful return, the gadget is ready to respond to requests from |
| 1448 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 1449 | * while it was binding. That would usually be done in order to wait for |
| 1450 | * some userspace participation. |
| 1451 | */ |
| 1452 | int usb_composite_register(struct usb_composite_driver *driver) |
| 1453 | { |
Sam Protsenko | 1257562 | 2016-02-16 19:59:19 +0200 | [diff] [blame] | 1454 | int res; |
| 1455 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1456 | if (!driver || !driver->dev || !driver->bind || composite) |
| 1457 | return -EINVAL; |
| 1458 | |
| 1459 | if (!driver->name) |
| 1460 | driver->name = "composite"; |
| 1461 | composite = driver; |
| 1462 | |
Sam Protsenko | 1257562 | 2016-02-16 19:59:19 +0200 | [diff] [blame] | 1463 | res = usb_gadget_register_driver(&composite_driver); |
| 1464 | if (res != 0) |
| 1465 | composite = NULL; |
| 1466 | |
| 1467 | return res; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * usb_composite_unregister() - unregister a composite driver |
| 1472 | * @driver: the driver to unregister |
| 1473 | * |
| 1474 | * This function is used to unregister drivers using the composite |
| 1475 | * driver framework. |
| 1476 | */ |
| 1477 | void usb_composite_unregister(struct usb_composite_driver *driver) |
| 1478 | { |
| 1479 | if (composite != driver) |
| 1480 | return; |
| 1481 | usb_gadget_unregister_driver(&composite_driver); |
Heiko Schocher | 9e37005 | 2013-06-04 11:21:32 +0200 | [diff] [blame] | 1482 | composite = NULL; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1483 | } |