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; |
Li Jun | 62936de | 2021-01-25 21:43:50 +0800 | [diff] [blame^] | 402 | if (cdev->use_os_string) { |
| 403 | cdev->os_desc_config = c; |
| 404 | os_desc_config = c; |
| 405 | } |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 406 | |
| 407 | /* Initialize all interfaces by setting them to altsetting zero. */ |
| 408 | for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { |
| 409 | f = c->interface[tmp]; |
| 410 | if (!f) |
| 411 | break; |
| 412 | |
| 413 | /* |
| 414 | * Record which endpoints are used by the function. This is used |
| 415 | * to dispatch control requests targeted at that endpoint to the |
| 416 | * function's setup callback instead of the current |
| 417 | * configuration's setup callback. |
| 418 | */ |
| 419 | if (gadget->speed == USB_SPEED_HIGH) |
| 420 | descriptors = f->hs_descriptors; |
| 421 | else |
| 422 | descriptors = f->descriptors; |
| 423 | |
| 424 | for (; *descriptors; ++descriptors) { |
| 425 | if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) |
| 426 | continue; |
| 427 | |
| 428 | ep = (struct usb_endpoint_descriptor *)*descriptors; |
| 429 | addr = ((ep->bEndpointAddress & 0x80) >> 3) |
| 430 | | (ep->bEndpointAddress & 0x0f); |
Bryan O'Donoghue | 0b88876 | 2018-04-30 15:56:10 +0100 | [diff] [blame] | 431 | generic_set_bit(addr, f->endpoints); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | result = f->set_alt(f, tmp, 0); |
| 435 | if (result < 0) { |
| 436 | debug("interface %d (%s/%p) alt 0 --> %d\n", |
| 437 | tmp, f->name, f, result); |
| 438 | |
| 439 | reset_config(cdev); |
| 440 | goto done; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /* when we return, be sure our power usage is valid */ |
| 445 | power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; |
| 446 | done: |
| 447 | usb_gadget_vbus_draw(gadget, power); |
| 448 | return result; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * usb_add_config() - add a configuration to a device. |
| 453 | * @cdev: wraps the USB gadget |
| 454 | * @config: the configuration, with bConfigurationValue assigned |
| 455 | * Context: single threaded during gadget setup |
| 456 | * |
| 457 | * One of the main tasks of a composite driver's bind() routine is to |
| 458 | * add each of the configurations it supports, using this routine. |
| 459 | * |
| 460 | * This function returns the value of the configuration's bind(), which |
| 461 | * is zero for success else a negative errno value. Binding configurations |
| 462 | * assigns global resources including string IDs, and per-configuration |
| 463 | * resources such as interface IDs and endpoints. |
| 464 | */ |
| 465 | int usb_add_config(struct usb_composite_dev *cdev, |
| 466 | struct usb_configuration *config) |
| 467 | { |
| 468 | int status = -EINVAL; |
| 469 | struct usb_configuration *c; |
| 470 | struct usb_function *f; |
| 471 | unsigned int i; |
| 472 | |
| 473 | debug("%s: adding config #%u '%s'/%p\n", __func__, |
| 474 | config->bConfigurationValue, |
| 475 | config->label, config); |
| 476 | |
| 477 | if (!config->bConfigurationValue || !config->bind) |
| 478 | goto done; |
| 479 | |
| 480 | /* Prevent duplicate configuration identifiers */ |
| 481 | list_for_each_entry(c, &cdev->configs, list) { |
| 482 | if (c->bConfigurationValue == config->bConfigurationValue) { |
| 483 | status = -EBUSY; |
| 484 | goto done; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | config->cdev = cdev; |
| 489 | list_add_tail(&config->list, &cdev->configs); |
| 490 | |
| 491 | INIT_LIST_HEAD(&config->functions); |
| 492 | config->next_interface_id = 0; |
| 493 | |
| 494 | status = config->bind(config); |
| 495 | if (status < 0) { |
| 496 | list_del(&config->list); |
| 497 | config->cdev = NULL; |
| 498 | } else { |
| 499 | debug("cfg %d/%p speeds:%s%s\n", |
| 500 | config->bConfigurationValue, config, |
| 501 | config->highspeed ? " high" : "", |
| 502 | config->fullspeed |
| 503 | ? (gadget_is_dualspeed(cdev->gadget) |
| 504 | ? " full" |
| 505 | : " full/low") |
| 506 | : ""); |
| 507 | |
| 508 | for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { |
| 509 | f = config->interface[i]; |
| 510 | if (!f) |
| 511 | continue; |
| 512 | debug("%s: interface %d = %s/%p\n", |
| 513 | __func__, i, f->name, f); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | usb_ep_autoconfig_reset(cdev->gadget); |
| 518 | |
| 519 | done: |
| 520 | if (status) |
| 521 | debug("added config '%s'/%u --> %d\n", config->label, |
| 522 | config->bConfigurationValue, status); |
| 523 | return status; |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * We support strings in multiple languages ... string descriptor zero |
| 528 | * says which languages are supported. The typical case will be that |
| 529 | * only one language (probably English) is used, with I18N handled on |
| 530 | * the host side. |
| 531 | */ |
| 532 | |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 533 | static void collect_langs(struct usb_gadget_strings **sp, void *buf) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 534 | { |
| 535 | const struct usb_gadget_strings *s; |
| 536 | u16 language; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 537 | __le16_packed *tmp; |
| 538 | __le16_packed *end = (buf + 252); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 539 | |
| 540 | while (*sp) { |
| 541 | s = *sp; |
| 542 | language = cpu_to_le16(s->language); |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 543 | for (tmp = buf; tmp->val && tmp < end; tmp++) { |
| 544 | if (tmp->val == language) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 545 | goto repeat; |
| 546 | } |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 547 | tmp->val = language; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 548 | repeat: |
| 549 | sp++; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static int lookup_string( |
| 554 | struct usb_gadget_strings **sp, |
| 555 | void *buf, |
| 556 | u16 language, |
| 557 | int id |
| 558 | ) |
| 559 | { |
| 560 | int value; |
| 561 | struct usb_gadget_strings *s; |
| 562 | |
| 563 | while (*sp) { |
| 564 | s = *sp++; |
| 565 | if (s->language != language) |
| 566 | continue; |
| 567 | value = usb_gadget_get_string(s, id, buf); |
| 568 | if (value > 0) |
| 569 | return value; |
| 570 | } |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | |
| 574 | static int get_string(struct usb_composite_dev *cdev, |
| 575 | void *buf, u16 language, int id) |
| 576 | { |
| 577 | struct usb_string_descriptor *s = buf; |
| 578 | struct usb_gadget_strings **sp; |
| 579 | int len; |
| 580 | struct usb_configuration *c; |
| 581 | struct usb_function *f; |
| 582 | |
| 583 | /* |
| 584 | * Yes, not only is USB's I18N support probably more than most |
| 585 | * folk will ever care about ... also, it's all supported here. |
| 586 | * (Except for UTF8 support for Unicode's "Astral Planes".) |
| 587 | */ |
| 588 | |
| 589 | /* 0 == report all available language codes */ |
| 590 | if (id == 0) { |
| 591 | memset(s, 0, 256); |
| 592 | s->bDescriptorType = USB_DT_STRING; |
| 593 | |
| 594 | sp = composite->strings; |
| 595 | if (sp) |
| 596 | collect_langs(sp, s->wData); |
| 597 | |
| 598 | list_for_each_entry(c, &cdev->configs, list) { |
| 599 | sp = c->strings; |
| 600 | if (sp) |
| 601 | collect_langs(sp, s->wData); |
| 602 | |
| 603 | list_for_each_entry(f, &c->functions, list) { |
| 604 | sp = f->strings; |
| 605 | if (sp) |
| 606 | collect_langs(sp, s->wData); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | for (len = 0; len <= 126 && s->wData[len]; len++) |
| 611 | continue; |
| 612 | if (!len) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | s->bLength = 2 * (len + 1); |
| 616 | return s->bLength; |
| 617 | } |
| 618 | |
Li Jun | 2a16354 | 2021-01-25 21:43:46 +0800 | [diff] [blame] | 619 | if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) { |
| 620 | struct usb_os_string *b = buf; |
| 621 | b->bLength = sizeof(*b); |
| 622 | b->bDescriptorType = USB_DT_STRING; |
| 623 | memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature)); |
| 624 | b->bMS_VendorCode = cdev->b_vendor_code; |
| 625 | b->bPad = 0; |
| 626 | return sizeof(*b); |
| 627 | } |
| 628 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 629 | /* |
| 630 | * Otherwise, look up and return a specified string. String IDs |
| 631 | * are device-scoped, so we look up each string table we're told |
| 632 | * about. These lookups are infrequent; simpler-is-better here. |
| 633 | */ |
| 634 | if (composite->strings) { |
| 635 | len = lookup_string(composite->strings, buf, language, id); |
| 636 | if (len > 0) |
| 637 | return len; |
| 638 | } |
| 639 | list_for_each_entry(c, &cdev->configs, list) { |
| 640 | if (c->strings) { |
| 641 | len = lookup_string(c->strings, buf, language, id); |
| 642 | if (len > 0) |
| 643 | return len; |
| 644 | } |
| 645 | list_for_each_entry(f, &c->functions, list) { |
| 646 | if (!f->strings) |
| 647 | continue; |
| 648 | len = lookup_string(f->strings, buf, language, id); |
| 649 | if (len > 0) |
| 650 | return len; |
| 651 | } |
| 652 | } |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | |
| 656 | /** |
| 657 | * usb_string_id() - allocate an unused string ID |
| 658 | * @cdev: the device whose string descriptor IDs are being allocated |
| 659 | * Context: single threaded during gadget setup |
| 660 | * |
| 661 | * @usb_string_id() is called from bind() callbacks to allocate |
| 662 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 663 | * then store that ID in the appropriate descriptors and string table. |
| 664 | * |
| 665 | * All string identifier should be allocated using this, |
| 666 | * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure |
| 667 | * that for example different functions don't wrongly assign different |
| 668 | * meanings to the same identifier. |
| 669 | */ |
| 670 | int usb_string_id(struct usb_composite_dev *cdev) |
| 671 | { |
| 672 | if (cdev->next_string_id < 254) { |
| 673 | /* |
| 674 | * string id 0 is reserved by USB spec for list of |
| 675 | * supported languages |
| 676 | * 255 reserved as well? -- mina86 |
| 677 | */ |
| 678 | cdev->next_string_id++; |
| 679 | return cdev->next_string_id; |
| 680 | } |
| 681 | return -ENODEV; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * usb_string_ids() - allocate unused string IDs in batch |
| 686 | * @cdev: the device whose string descriptor IDs are being allocated |
| 687 | * @str: an array of usb_string objects to assign numbers to |
| 688 | * Context: single threaded during gadget setup |
| 689 | * |
| 690 | * @usb_string_ids() is called from bind() callbacks to allocate |
| 691 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 692 | * then copy IDs from the string table to the appropriate descriptors |
| 693 | * and string table for other languages. |
| 694 | * |
| 695 | * All string identifier should be allocated using this, |
| 696 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 697 | * example different functions don't wrongly assign different meanings |
| 698 | * to the same identifier. |
| 699 | */ |
| 700 | int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) |
| 701 | { |
| 702 | u8 next = cdev->next_string_id; |
| 703 | |
| 704 | for (; str->s; ++str) { |
| 705 | if (next >= 254) |
| 706 | return -ENODEV; |
| 707 | str->id = ++next; |
| 708 | } |
| 709 | |
| 710 | cdev->next_string_id = next; |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * usb_string_ids_n() - allocate unused string IDs in batch |
| 717 | * @c: the device whose string descriptor IDs are being allocated |
| 718 | * @n: number of string IDs to allocate |
| 719 | * Context: single threaded during gadget setup |
| 720 | * |
| 721 | * Returns the first requested ID. This ID and next @n-1 IDs are now |
| 722 | * valid IDs. At least provided that @n is non-zero because if it |
| 723 | * is, returns last requested ID which is now very useful information. |
| 724 | * |
| 725 | * @usb_string_ids_n() is called from bind() callbacks to allocate |
| 726 | * string IDs. Drivers for functions, configurations, or gadgets will |
| 727 | * then store that ID in the appropriate descriptors and string table. |
| 728 | * |
| 729 | * All string identifier should be allocated using this, |
| 730 | * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for |
| 731 | * example different functions don't wrongly assign different meanings |
| 732 | * to the same identifier. |
| 733 | */ |
| 734 | int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) |
| 735 | { |
| 736 | u8 next = c->next_string_id; |
| 737 | |
| 738 | if (n > 254 || next + n > 254) |
| 739 | return -ENODEV; |
| 740 | |
| 741 | c->next_string_id += n; |
| 742 | return next + 1; |
| 743 | } |
| 744 | |
| 745 | static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) |
| 746 | { |
| 747 | if (req->status || req->actual != req->length) |
| 748 | debug("%s: setup complete --> %d, %d/%d\n", __func__, |
| 749 | req->status, req->actual, req->length); |
| 750 | } |
| 751 | |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 752 | static int bos_desc(struct usb_composite_dev *cdev) |
| 753 | { |
| 754 | struct usb_ext_cap_descriptor *usb_ext; |
| 755 | struct usb_bos_descriptor *bos = cdev->req->buf; |
| 756 | |
| 757 | bos->bLength = USB_DT_BOS_SIZE; |
| 758 | bos->bDescriptorType = USB_DT_BOS; |
| 759 | |
| 760 | bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); |
| 761 | bos->bNumDeviceCaps = 0; |
| 762 | |
| 763 | /* |
| 764 | * A SuperSpeed device shall include the USB2.0 extension descriptor |
| 765 | * and shall support LPM when operating in USB2.0 HS mode. |
| 766 | */ |
| 767 | usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 768 | bos->bNumDeviceCaps++; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 769 | le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, |
| 770 | USB_DT_USB_EXT_CAP_SIZE); |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 771 | usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; |
| 772 | usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 773 | usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; |
| 774 | usb_ext->bmAttributes = |
| 775 | cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT); |
| 776 | |
| 777 | /* |
| 778 | * The Superspeed USB Capability descriptor shall be implemented |
| 779 | * by all SuperSpeed devices. |
| 780 | */ |
| 781 | if (gadget_is_superspeed(cdev->gadget)) { |
| 782 | struct usb_ss_cap_descriptor *ss_cap; |
| 783 | |
| 784 | ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); |
| 785 | bos->bNumDeviceCaps++; |
Simon Goldschmidt | 7e509a3 | 2019-11-21 22:15:22 +0100 | [diff] [blame] | 786 | le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength, |
| 787 | USB_DT_USB_SS_CAP_SIZE); |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 788 | ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; |
| 789 | ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; |
| 790 | ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; |
| 791 | ss_cap->bmAttributes = 0; /* LTM is not supported yet */ |
| 792 | ss_cap->wSpeedSupported = |
| 793 | cpu_to_le16(USB_LOW_SPEED_OPERATION | |
| 794 | USB_FULL_SPEED_OPERATION | |
| 795 | USB_HIGH_SPEED_OPERATION | |
| 796 | USB_5GBPS_OPERATION); |
| 797 | ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; |
| 798 | ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT; |
| 799 | ss_cap->bU2DevExitLat = |
| 800 | cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); |
| 801 | } |
| 802 | return le16_to_cpu(bos->wTotalLength); |
| 803 | } |
| 804 | |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 805 | static int count_ext_compat(struct usb_configuration *c) |
| 806 | { |
| 807 | int i, res; |
| 808 | |
| 809 | res = 0; |
| 810 | for (i = 0; i < c->next_interface_id; ++i) { |
| 811 | struct usb_function *f; |
| 812 | int j; |
| 813 | |
| 814 | f = c->interface[i]; |
| 815 | for (j = 0; j < f->os_desc_n; ++j) { |
| 816 | struct usb_os_desc *d; |
| 817 | |
| 818 | if (i != f->os_desc_table[j].if_id) |
| 819 | continue; |
| 820 | d = f->os_desc_table[j].os_desc; |
| 821 | if (d && d->ext_compat_id) |
| 822 | ++res; |
| 823 | } |
| 824 | } |
| 825 | BUG_ON(res > 255); |
| 826 | return res; |
| 827 | } |
| 828 | |
| 829 | static void fill_ext_compat(struct usb_configuration *c, u8 *buf) |
| 830 | { |
| 831 | int i, count; |
| 832 | |
| 833 | count = 16; |
| 834 | for (i = 0; i < c->next_interface_id; ++i) { |
| 835 | struct usb_function *f; |
| 836 | int j; |
| 837 | |
| 838 | f = c->interface[i]; |
| 839 | for (j = 0; j < f->os_desc_n; ++j) { |
| 840 | struct usb_os_desc *d; |
| 841 | |
| 842 | if (i != f->os_desc_table[j].if_id) |
| 843 | continue; |
| 844 | d = f->os_desc_table[j].os_desc; |
| 845 | if (d && d->ext_compat_id) { |
| 846 | *buf++ = i; |
| 847 | *buf++ = 0x01; |
| 848 | memcpy(buf, d->ext_compat_id, 16); |
| 849 | buf += 22; |
| 850 | } else { |
| 851 | ++buf; |
| 852 | *buf = 0x01; |
| 853 | buf += 23; |
| 854 | } |
| 855 | count += 24; |
| 856 | if (count >= 4096) |
| 857 | return; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | static int count_ext_prop(struct usb_configuration *c, int interface) |
| 863 | { |
| 864 | struct usb_function *f; |
| 865 | int j; |
| 866 | |
| 867 | f = c->interface[interface]; |
| 868 | for (j = 0; j < f->os_desc_n; ++j) { |
| 869 | struct usb_os_desc *d; |
| 870 | |
| 871 | if (interface != f->os_desc_table[j].if_id) |
| 872 | continue; |
| 873 | d = f->os_desc_table[j].os_desc; |
| 874 | if (d && d->ext_compat_id) |
| 875 | return d->ext_prop_count; |
| 876 | } |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int len_ext_prop(struct usb_configuration *c, int interface) |
| 881 | { |
| 882 | struct usb_function *f; |
| 883 | struct usb_os_desc *d; |
| 884 | int j, res; |
| 885 | |
| 886 | res = 10; /* header length */ |
| 887 | f = c->interface[interface]; |
| 888 | for (j = 0; j < f->os_desc_n; ++j) { |
| 889 | if (interface != f->os_desc_table[j].if_id) |
| 890 | continue; |
| 891 | d = f->os_desc_table[j].os_desc; |
| 892 | if (d) |
| 893 | return min(res + d->ext_prop_len, 4096); |
| 894 | } |
| 895 | return res; |
| 896 | } |
| 897 | |
| 898 | static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf) |
| 899 | { |
| 900 | struct usb_function *f; |
| 901 | struct usb_os_desc *d; |
| 902 | struct usb_os_desc_ext_prop *ext_prop; |
| 903 | int j, count, n, ret; |
| 904 | u8 *start = buf; |
| 905 | |
| 906 | f = c->interface[interface]; |
| 907 | for (j = 0; j < f->os_desc_n; ++j) { |
| 908 | if (interface != f->os_desc_table[j].if_id) |
| 909 | continue; |
| 910 | d = f->os_desc_table[j].os_desc; |
| 911 | if (d) |
| 912 | list_for_each_entry(ext_prop, &d->ext_prop, entry) { |
| 913 | /* 4kB minus header length */ |
| 914 | n = buf - start; |
| 915 | if (n >= 4086) |
| 916 | return 0; |
| 917 | |
| 918 | count = ext_prop->data_len + |
| 919 | ext_prop->name_len + 14; |
| 920 | if (count > 4086 - n) |
| 921 | return -EINVAL; |
| 922 | usb_ext_prop_put_size(buf, count); |
| 923 | usb_ext_prop_put_type(buf, ext_prop->type); |
| 924 | ret = usb_ext_prop_put_name(buf, ext_prop->name, |
| 925 | ext_prop->name_len); |
| 926 | if (ret < 0) |
| 927 | return ret; |
| 928 | switch (ext_prop->type) { |
| 929 | case USB_EXT_PROP_UNICODE: |
| 930 | case USB_EXT_PROP_UNICODE_ENV: |
| 931 | case USB_EXT_PROP_UNICODE_LINK: |
| 932 | usb_ext_prop_put_unicode(buf, ret, |
| 933 | ext_prop->data, |
| 934 | ext_prop->data_len); |
| 935 | break; |
| 936 | case USB_EXT_PROP_BINARY: |
| 937 | usb_ext_prop_put_binary(buf, ret, |
| 938 | ext_prop->data, |
| 939 | ext_prop->data_len); |
| 940 | break; |
| 941 | case USB_EXT_PROP_LE32: |
| 942 | /* not implemented */ |
| 943 | case USB_EXT_PROP_BE32: |
| 944 | /* not implemented */ |
| 945 | default: |
| 946 | return -EINVAL; |
| 947 | } |
| 948 | buf += count; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 955 | /* |
| 956 | * The setup() callback implements all the ep0 functionality that's |
| 957 | * not handled lower down, in hardware or the hardware driver(like |
| 958 | * device and endpoint feature flags, and their status). It's all |
| 959 | * housekeeping for the gadget function we're implementing. Most of |
| 960 | * the work is in config and function specific setup. |
| 961 | */ |
| 962 | static int |
| 963 | composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) |
| 964 | { |
| 965 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 966 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 967 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 968 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 969 | u8 intf = w_index & 0xFF; |
| 970 | int value = -EOPNOTSUPP; |
| 971 | struct usb_request *req = cdev->req; |
| 972 | struct usb_function *f = NULL; |
| 973 | int standard; |
| 974 | u8 endp; |
| 975 | struct usb_configuration *c; |
| 976 | |
| 977 | /* |
| 978 | * partial re-init of the response message; the function or the |
| 979 | * gadget might need to intercept e.g. a control-OUT completion |
| 980 | * when we delegate to it. |
| 981 | */ |
| 982 | req->zero = 0; |
| 983 | req->complete = composite_setup_complete; |
| 984 | req->length = USB_BUFSIZ; |
| 985 | gadget->ep0->driver_data = cdev; |
| 986 | standard = (ctrl->bRequestType & USB_TYPE_MASK) |
| 987 | == USB_TYPE_STANDARD; |
| 988 | if (!standard) |
| 989 | goto unknown; |
| 990 | |
| 991 | switch (ctrl->bRequest) { |
| 992 | |
| 993 | /* we handle all standard USB descriptors */ |
| 994 | case USB_REQ_GET_DESCRIPTOR: |
| 995 | if (ctrl->bRequestType != USB_DIR_IN) |
| 996 | goto unknown; |
| 997 | switch (w_value >> 8) { |
| 998 | |
| 999 | case USB_DT_DEVICE: |
| 1000 | cdev->desc.bNumConfigurations = |
| 1001 | count_configs(cdev, USB_DT_DEVICE); |
Siva Durga Prasad Paladugu | c7c0718 | 2018-12-13 15:16:36 +0530 | [diff] [blame] | 1002 | |
| 1003 | /* |
| 1004 | * If the speed is Super speed, then the supported |
| 1005 | * max packet size is 512 and it should be sent as |
| 1006 | * exponent of 2. So, 9(2^9=512) should be filled in |
| 1007 | * bMaxPacketSize0. Also fill USB version as 3.0 |
| 1008 | * if speed is Super speed. |
| 1009 | */ |
| 1010 | if (cdev->gadget->speed == USB_SPEED_SUPER) { |
| 1011 | cdev->desc.bMaxPacketSize0 = 9; |
| 1012 | cdev->desc.bcdUSB = cpu_to_le16(0x0300); |
| 1013 | } else { |
| 1014 | cdev->desc.bMaxPacketSize0 = |
| 1015 | cdev->gadget->ep0->maxpacket; |
| 1016 | } |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1017 | value = min(w_length, (u16) sizeof cdev->desc); |
| 1018 | memcpy(req->buf, &cdev->desc, value); |
| 1019 | break; |
| 1020 | case USB_DT_DEVICE_QUALIFIER: |
| 1021 | if (!gadget_is_dualspeed(gadget)) |
| 1022 | break; |
| 1023 | device_qual(cdev); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 1024 | value = min_t(int, w_length, |
| 1025 | sizeof(struct usb_qualifier_descriptor)); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1026 | break; |
| 1027 | case USB_DT_OTHER_SPEED_CONFIG: |
| 1028 | if (!gadget_is_dualspeed(gadget)) |
| 1029 | break; |
| 1030 | |
| 1031 | case USB_DT_CONFIG: |
| 1032 | value = config_desc(cdev, w_value); |
| 1033 | if (value >= 0) |
| 1034 | value = min(w_length, (u16) value); |
| 1035 | break; |
| 1036 | case USB_DT_STRING: |
| 1037 | value = get_string(cdev, req->buf, |
| 1038 | w_index, w_value & 0xff); |
| 1039 | if (value >= 0) |
| 1040 | value = min(w_length, (u16) value); |
| 1041 | break; |
Stefan Roese | a56fbcc | 2015-01-09 14:54:55 +0100 | [diff] [blame] | 1042 | case USB_DT_BOS: |
T Karthik Reddy | 851bddd | 2019-10-14 14:52:50 +0200 | [diff] [blame] | 1043 | if (gadget_is_superspeed(cdev->gadget)) |
| 1044 | value = bos_desc(cdev); |
| 1045 | if (value >= 0) |
| 1046 | value = min(w_length, (u16)value); |
Stefan Roese | a56fbcc | 2015-01-09 14:54:55 +0100 | [diff] [blame] | 1047 | break; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1048 | default: |
| 1049 | goto unknown; |
| 1050 | } |
| 1051 | break; |
| 1052 | |
| 1053 | /* any number of configs can work */ |
| 1054 | case USB_REQ_SET_CONFIGURATION: |
| 1055 | if (ctrl->bRequestType != 0) |
| 1056 | goto unknown; |
| 1057 | if (gadget_is_otg(gadget)) { |
| 1058 | if (gadget->a_hnp_support) |
| 1059 | debug("HNP available\n"); |
| 1060 | else if (gadget->a_alt_hnp_support) |
| 1061 | debug("HNP on another port\n"); |
| 1062 | else |
| 1063 | debug("HNP inactive\n"); |
| 1064 | } |
| 1065 | |
| 1066 | value = set_config(cdev, ctrl, w_value); |
| 1067 | break; |
| 1068 | case USB_REQ_GET_CONFIGURATION: |
| 1069 | if (ctrl->bRequestType != USB_DIR_IN) |
| 1070 | goto unknown; |
| 1071 | if (cdev->config) |
| 1072 | *(u8 *)req->buf = cdev->config->bConfigurationValue; |
| 1073 | else |
| 1074 | *(u8 *)req->buf = 0; |
| 1075 | value = min(w_length, (u16) 1); |
| 1076 | break; |
| 1077 | |
| 1078 | /* |
| 1079 | * function drivers must handle get/set altsetting; if there's |
| 1080 | * no get() method, we know only altsetting zero works. |
| 1081 | */ |
| 1082 | case USB_REQ_SET_INTERFACE: |
| 1083 | if (ctrl->bRequestType != USB_RECIP_INTERFACE) |
| 1084 | goto unknown; |
| 1085 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 1086 | break; |
| 1087 | f = cdev->config->interface[intf]; |
| 1088 | if (!f) |
| 1089 | break; |
| 1090 | if (w_value && !f->set_alt) |
| 1091 | break; |
| 1092 | value = f->set_alt(f, w_index, w_value); |
| 1093 | break; |
| 1094 | case USB_REQ_GET_INTERFACE: |
| 1095 | if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) |
| 1096 | goto unknown; |
| 1097 | if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) |
| 1098 | break; |
| 1099 | f = cdev->config->interface[intf]; |
| 1100 | if (!f) |
| 1101 | break; |
| 1102 | /* lots of interfaces only need altsetting zero... */ |
| 1103 | value = f->get_alt ? f->get_alt(f, w_index) : 0; |
| 1104 | if (value < 0) |
| 1105 | break; |
| 1106 | *((u8 *)req->buf) = value; |
| 1107 | value = min(w_length, (u16) 1); |
| 1108 | break; |
| 1109 | default: |
| 1110 | unknown: |
Li Jun | 68bda3a | 2021-01-25 21:43:49 +0800 | [diff] [blame] | 1111 | /* |
| 1112 | * OS descriptors handling |
| 1113 | */ |
| 1114 | if (CONFIG_IS_ENABLED(USB_GADGET_OS_DESCRIPTORS) && cdev->use_os_string && |
| 1115 | cdev->os_desc_config && (ctrl->bRequestType & USB_TYPE_VENDOR) && |
| 1116 | ctrl->bRequest == cdev->b_vendor_code) { |
| 1117 | struct usb_configuration *os_desc_cfg; |
| 1118 | u8 *buf; |
| 1119 | int interface; |
| 1120 | int count = 0; |
| 1121 | |
| 1122 | buf = req->buf; |
| 1123 | os_desc_cfg = cdev->os_desc_config; |
| 1124 | memset(buf, 0, w_length); |
| 1125 | buf[5] = 0x01; |
| 1126 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1127 | case USB_RECIP_DEVICE: |
| 1128 | if (w_index != 0x4 || (w_value >> 8)) |
| 1129 | break; |
| 1130 | buf[6] = w_index; |
| 1131 | if (w_length == 0x10) { |
| 1132 | /* Number of ext compat interfaces */ |
| 1133 | count = count_ext_compat(os_desc_cfg); |
| 1134 | buf[8] = count; |
| 1135 | count *= 24; /* 24 B/ext compat desc */ |
| 1136 | count += 16; /* header */ |
| 1137 | put_unaligned_le32(count, buf); |
| 1138 | value = w_length; |
| 1139 | } else { |
| 1140 | /* "extended compatibility ID"s */ |
| 1141 | count = count_ext_compat(os_desc_cfg); |
| 1142 | buf[8] = count; |
| 1143 | count *= 24; /* 24 B/ext compat desc */ |
| 1144 | count += 16; /* header */ |
| 1145 | put_unaligned_le32(count, buf); |
| 1146 | buf += 16; |
| 1147 | fill_ext_compat(os_desc_cfg, buf); |
| 1148 | value = w_length; |
| 1149 | } |
| 1150 | break; |
| 1151 | case USB_RECIP_INTERFACE: |
| 1152 | if (w_index != 0x5 || (w_value >> 8)) |
| 1153 | break; |
| 1154 | interface = w_value & 0xFF; |
| 1155 | buf[6] = w_index; |
| 1156 | if (w_length == 0x0A) { |
| 1157 | count = count_ext_prop(os_desc_cfg, |
| 1158 | interface); |
| 1159 | put_unaligned_le16(count, buf + 8); |
| 1160 | count = len_ext_prop(os_desc_cfg, |
| 1161 | interface); |
| 1162 | put_unaligned_le32(count, buf); |
| 1163 | |
| 1164 | value = w_length; |
| 1165 | } else { |
| 1166 | count = count_ext_prop(os_desc_cfg, |
| 1167 | interface); |
| 1168 | put_unaligned_le16(count, buf + 8); |
| 1169 | count = len_ext_prop(os_desc_cfg, |
| 1170 | interface); |
| 1171 | put_unaligned_le32(count, buf); |
| 1172 | buf += 10; |
| 1173 | value = fill_ext_prop(os_desc_cfg, |
| 1174 | interface, buf); |
| 1175 | if (value < 0) |
| 1176 | return value; |
| 1177 | |
| 1178 | value = w_length; |
| 1179 | } |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | if (value >= 0) { |
| 1184 | req->length = value; |
| 1185 | req->zero = value < w_length; |
| 1186 | value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); |
| 1187 | if (value < 0) { |
| 1188 | debug("ep_queue --> %d\n", value); |
| 1189 | req->status = 0; |
| 1190 | composite_setup_complete(gadget->ep0, req); |
| 1191 | } |
| 1192 | } |
| 1193 | return value; |
| 1194 | } |
| 1195 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1196 | debug("non-core control req%02x.%02x v%04x i%04x l%d\n", |
| 1197 | ctrl->bRequestType, ctrl->bRequest, |
| 1198 | w_value, w_index, w_length); |
| 1199 | |
Christophe Kerello | 0c1b62c | 2018-03-15 09:34:17 +0100 | [diff] [blame] | 1200 | if (!cdev->config) |
| 1201 | goto done; |
| 1202 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1203 | /* |
| 1204 | * functions always handle their interfaces and endpoints... |
| 1205 | * punt other recipients (other, WUSB, ...) to the current |
| 1206 | * configuration code. |
| 1207 | */ |
| 1208 | switch (ctrl->bRequestType & USB_RECIP_MASK) { |
| 1209 | case USB_RECIP_INTERFACE: |
| 1210 | f = cdev->config->interface[intf]; |
| 1211 | break; |
| 1212 | |
| 1213 | case USB_RECIP_ENDPOINT: |
| 1214 | endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); |
| 1215 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1216 | if (test_bit(endp, f->endpoints)) |
| 1217 | break; |
| 1218 | } |
| 1219 | if (&f->list == &cdev->config->functions) |
| 1220 | f = NULL; |
| 1221 | break; |
Lukasz Majewski | 92d7fb8 | 2013-03-01 15:30:18 +0100 | [diff] [blame] | 1222 | /* |
| 1223 | * dfu-util (version 0.5) sets bmRequestType.Receipent = Device |
| 1224 | * for non-standard request (w_value = 0x21, |
| 1225 | * bRequest = GET_DESCRIPTOR in this case). |
| 1226 | * When only one interface is registered (as it is done now), |
| 1227 | * then this request shall be handled as it was requested for |
| 1228 | * interface. |
| 1229 | * |
| 1230 | * In the below code it is checked if only one interface is |
| 1231 | * present and proper function for it is extracted. Due to that |
| 1232 | * function's setup (f->setup) is called to handle this |
| 1233 | * special non-standard request. |
| 1234 | */ |
| 1235 | case USB_RECIP_DEVICE: |
| 1236 | debug("cdev->config->next_interface_id: %d intf: %d\n", |
| 1237 | cdev->config->next_interface_id, intf); |
| 1238 | if (cdev->config->next_interface_id == 1) |
| 1239 | f = cdev->config->interface[intf]; |
| 1240 | break; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | if (f && f->setup) |
| 1244 | value = f->setup(f, ctrl); |
| 1245 | else { |
| 1246 | c = cdev->config; |
Christophe Kerello | 0c1b62c | 2018-03-15 09:34:17 +0100 | [diff] [blame] | 1247 | if (c->setup) |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1248 | value = c->setup(c, ctrl); |
| 1249 | } |
| 1250 | |
| 1251 | goto done; |
| 1252 | } |
| 1253 | |
| 1254 | /* respond with data transfer before status phase? */ |
| 1255 | if (value >= 0) { |
| 1256 | req->length = value; |
| 1257 | req->zero = value < w_length; |
| 1258 | value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); |
| 1259 | if (value < 0) { |
| 1260 | debug("ep_queue --> %d\n", value); |
| 1261 | req->status = 0; |
| 1262 | composite_setup_complete(gadget->ep0, req); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | done: |
| 1267 | /* device either stalls (value < 0) or reports success */ |
| 1268 | return value; |
| 1269 | } |
| 1270 | |
| 1271 | static void composite_disconnect(struct usb_gadget *gadget) |
| 1272 | { |
| 1273 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1274 | |
| 1275 | if (cdev->config) |
| 1276 | reset_config(cdev); |
| 1277 | if (composite->disconnect) |
| 1278 | composite->disconnect(cdev); |
| 1279 | } |
| 1280 | |
| 1281 | static void composite_unbind(struct usb_gadget *gadget) |
| 1282 | { |
| 1283 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1284 | struct usb_configuration *c; |
| 1285 | struct usb_function *f; |
| 1286 | |
| 1287 | /* |
| 1288 | * composite_disconnect() must already have been called |
| 1289 | * by the underlying peripheral controller driver! |
| 1290 | * so there's no i/o concurrency that could affect the |
| 1291 | * state protected by cdev->lock. |
| 1292 | */ |
Simon Glass | b18a960 | 2019-12-29 21:19:12 -0700 | [diff] [blame] | 1293 | #ifdef __UBOOT__ |
| 1294 | assert_noisy(!cdev->config); |
| 1295 | #else |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1296 | BUG_ON(cdev->config); |
Simon Glass | b18a960 | 2019-12-29 21:19:12 -0700 | [diff] [blame] | 1297 | #endif |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1298 | |
| 1299 | while (!list_empty(&cdev->configs)) { |
| 1300 | c = list_first_entry(&cdev->configs, |
| 1301 | struct usb_configuration, list); |
| 1302 | while (!list_empty(&c->functions)) { |
| 1303 | f = list_first_entry(&c->functions, |
| 1304 | struct usb_function, list); |
| 1305 | list_del(&f->list); |
| 1306 | if (f->unbind) { |
| 1307 | debug("unbind function '%s'/%p\n", |
| 1308 | f->name, f); |
| 1309 | f->unbind(c, f); |
| 1310 | } |
| 1311 | } |
| 1312 | list_del(&c->list); |
| 1313 | if (c->unbind) { |
| 1314 | debug("unbind config '%s'/%p\n", c->label, c); |
| 1315 | c->unbind(c); |
| 1316 | } |
Stephen Warren | 1c377dc | 2015-09-04 22:03:42 -0600 | [diff] [blame] | 1317 | free(c); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1318 | } |
| 1319 | if (composite->unbind) |
| 1320 | composite->unbind(cdev); |
| 1321 | |
| 1322 | if (cdev->req) { |
| 1323 | kfree(cdev->req->buf); |
| 1324 | usb_ep_free_request(gadget->ep0, cdev->req); |
| 1325 | } |
| 1326 | kfree(cdev); |
| 1327 | set_gadget_data(gadget, NULL); |
| 1328 | |
| 1329 | composite = NULL; |
| 1330 | } |
| 1331 | |
| 1332 | static int composite_bind(struct usb_gadget *gadget) |
| 1333 | { |
| 1334 | int status = -ENOMEM; |
| 1335 | struct usb_composite_dev *cdev; |
| 1336 | |
| 1337 | cdev = calloc(sizeof *cdev, 1); |
| 1338 | if (!cdev) |
| 1339 | return status; |
| 1340 | |
| 1341 | cdev->gadget = gadget; |
| 1342 | set_gadget_data(gadget, cdev); |
| 1343 | INIT_LIST_HEAD(&cdev->configs); |
| 1344 | |
| 1345 | /* preallocate control response and buffer */ |
| 1346 | cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); |
| 1347 | if (!cdev->req) |
| 1348 | goto fail; |
| 1349 | cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ); |
| 1350 | if (!cdev->req->buf) |
| 1351 | goto fail; |
| 1352 | cdev->req->complete = composite_setup_complete; |
| 1353 | gadget->ep0->driver_data = cdev; |
| 1354 | |
| 1355 | cdev->bufsiz = USB_BUFSIZ; |
| 1356 | cdev->driver = composite; |
| 1357 | |
| 1358 | usb_gadget_set_selfpowered(gadget); |
| 1359 | usb_ep_autoconfig_reset(cdev->gadget); |
| 1360 | |
| 1361 | status = composite->bind(cdev); |
| 1362 | if (status < 0) |
| 1363 | goto fail; |
| 1364 | |
Piotr Wilczek | 7db5de6 | 2013-04-10 14:07:51 +0200 | [diff] [blame] | 1365 | memcpy(&cdev->desc, composite->dev, |
| 1366 | sizeof(struct usb_device_descriptor)); |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1367 | cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; |
| 1368 | |
Li Jun | 62936de | 2021-01-25 21:43:50 +0800 | [diff] [blame^] | 1369 | if (cdev->use_os_string) { |
| 1370 | /* TODO: Do we want to pass this via platform? */ |
| 1371 | cdev->b_vendor_code = 0x40; |
| 1372 | |
| 1373 | /* Microsoft OS String Descriptor */ |
| 1374 | utf8_to_utf16le(qw_sign_buf, (__le16 *)cdev->qw_sign, |
| 1375 | OS_STRING_QW_SIGN_LEN / 2); |
| 1376 | |
| 1377 | if (os_desc_config) |
| 1378 | cdev->os_desc_config = os_desc_config; |
| 1379 | } |
| 1380 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1381 | debug("%s: ready\n", composite->name); |
| 1382 | return 0; |
| 1383 | |
| 1384 | fail: |
| 1385 | composite_unbind(gadget); |
| 1386 | return status; |
| 1387 | } |
| 1388 | |
| 1389 | static void |
| 1390 | composite_suspend(struct usb_gadget *gadget) |
| 1391 | { |
| 1392 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1393 | struct usb_function *f; |
| 1394 | |
| 1395 | debug("%s: suspend\n", __func__); |
| 1396 | if (cdev->config) { |
| 1397 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1398 | if (f->suspend) |
| 1399 | f->suspend(f); |
| 1400 | } |
| 1401 | } |
| 1402 | if (composite->suspend) |
| 1403 | composite->suspend(cdev); |
| 1404 | |
| 1405 | cdev->suspended = 1; |
| 1406 | } |
| 1407 | |
| 1408 | static void |
| 1409 | composite_resume(struct usb_gadget *gadget) |
| 1410 | { |
| 1411 | struct usb_composite_dev *cdev = get_gadget_data(gadget); |
| 1412 | struct usb_function *f; |
| 1413 | |
| 1414 | debug("%s: resume\n", __func__); |
| 1415 | if (composite->resume) |
| 1416 | composite->resume(cdev); |
| 1417 | if (cdev->config) { |
| 1418 | list_for_each_entry(f, &cdev->config->functions, list) { |
| 1419 | if (f->resume) |
| 1420 | f->resume(f); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | cdev->suspended = 0; |
| 1425 | } |
| 1426 | |
| 1427 | static struct usb_gadget_driver composite_driver = { |
| 1428 | .speed = USB_SPEED_HIGH, |
| 1429 | |
| 1430 | .bind = composite_bind, |
| 1431 | .unbind = composite_unbind, |
| 1432 | |
| 1433 | .setup = composite_setup, |
Lukasz Majewski | d04a012 | 2015-03-03 17:32:07 +0100 | [diff] [blame] | 1434 | .reset = composite_disconnect, |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1435 | .disconnect = composite_disconnect, |
| 1436 | |
| 1437 | .suspend = composite_suspend, |
| 1438 | .resume = composite_resume, |
| 1439 | }; |
| 1440 | |
| 1441 | /** |
| 1442 | * usb_composite_register() - register a composite driver |
| 1443 | * @driver: the driver to register |
| 1444 | * Context: single threaded during gadget setup |
| 1445 | * |
| 1446 | * This function is used to register drivers using the composite driver |
| 1447 | * framework. The return value is zero, or a negative errno value. |
| 1448 | * Those values normally come from the driver's @bind method, which does |
| 1449 | * all the work of setting up the driver to match the hardware. |
| 1450 | * |
| 1451 | * On successful return, the gadget is ready to respond to requests from |
| 1452 | * the host, unless one of its components invokes usb_gadget_disconnect() |
| 1453 | * while it was binding. That would usually be done in order to wait for |
| 1454 | * some userspace participation. |
| 1455 | */ |
| 1456 | int usb_composite_register(struct usb_composite_driver *driver) |
| 1457 | { |
Sam Protsenko | 1257562 | 2016-02-16 19:59:19 +0200 | [diff] [blame] | 1458 | int res; |
| 1459 | |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1460 | if (!driver || !driver->dev || !driver->bind || composite) |
| 1461 | return -EINVAL; |
| 1462 | |
| 1463 | if (!driver->name) |
| 1464 | driver->name = "composite"; |
| 1465 | composite = driver; |
| 1466 | |
Sam Protsenko | 1257562 | 2016-02-16 19:59:19 +0200 | [diff] [blame] | 1467 | res = usb_gadget_register_driver(&composite_driver); |
| 1468 | if (res != 0) |
| 1469 | composite = NULL; |
| 1470 | |
| 1471 | return res; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * usb_composite_unregister() - unregister a composite driver |
| 1476 | * @driver: the driver to unregister |
| 1477 | * |
| 1478 | * This function is used to unregister drivers using the composite |
| 1479 | * driver framework. |
| 1480 | */ |
| 1481 | void usb_composite_unregister(struct usb_composite_driver *driver) |
| 1482 | { |
| 1483 | if (composite != driver) |
| 1484 | return; |
| 1485 | usb_gadget_unregister_driver(&composite_driver); |
Heiko Schocher | 9e37005 | 2013-06-04 11:21:32 +0200 | [diff] [blame] | 1486 | composite = NULL; |
Lukasz Majewski | 1dc6a67 | 2012-05-02 17:47:02 +0200 | [diff] [blame] | 1487 | } |