1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * composite.c - infrastructure for Composite USB Gadgets 4 * 5 * Copyright (C) 2006-2008 David Brownell 6 */ 7 8 /* #define VERBOSE_DEBUG */ 9 10 #include <linux/kallsyms.h> 11 #include <linux/kernel.h> 12 #include <linux/slab.h> 13 #include <linux/module.h> 14 #include <linux/device.h> 15 #include <linux/utsname.h> 16 #include <linux/bitfield.h> 17 #include <linux/uuid.h> 18 19 #include <linux/usb/composite.h> 20 #include <linux/usb/otg.h> 21 #include <linux/usb/webusb.h> 22 #include <asm/unaligned.h> 23 24 #include "u_os_desc.h" 25 26 /** 27 * struct usb_os_string - represents OS String to be reported by a gadget 28 * @bLength: total length of the entire descritor, always 0x12 29 * @bDescriptorType: USB_DT_STRING 30 * @qwSignature: the OS String proper 31 * @bMS_VendorCode: code used by the host for subsequent requests 32 * @bPad: not used, must be zero 33 */ 34 struct usb_os_string { 35 __u8 bLength; 36 __u8 bDescriptorType; 37 __u8 qwSignature[OS_STRING_QW_SIGN_LEN]; 38 __u8 bMS_VendorCode; 39 __u8 bPad; 40 } __packed; 41 42 /* 43 * The code in this file is utility code, used to build a gadget driver 44 * from one or more "function" drivers, one or more "configuration" 45 * objects, and a "usb_composite_driver" by gluing them together along 46 * with the relevant device-wide data. 47 */ 48 49 static struct usb_gadget_strings **get_containers_gs( 50 struct usb_gadget_string_container *uc) 51 { 52 return (struct usb_gadget_strings **)uc->stash; 53 } 54 55 /** 56 * function_descriptors() - get function descriptors for speed 57 * @f: the function 58 * @speed: the speed 59 * 60 * Returns the descriptors or NULL if not set. 61 */ 62 static struct usb_descriptor_header ** 63 function_descriptors(struct usb_function *f, 64 enum usb_device_speed speed) 65 { 66 struct usb_descriptor_header **descriptors; 67 68 /* 69 * NOTE: we try to help gadget drivers which might not be setting 70 * max_speed appropriately. 71 */ 72 73 switch (speed) { 74 case USB_SPEED_SUPER_PLUS: 75 descriptors = f->ssp_descriptors; 76 if (descriptors) 77 break; 78 fallthrough; 79 case USB_SPEED_SUPER: 80 descriptors = f->ss_descriptors; 81 if (descriptors) 82 break; 83 fallthrough; 84 case USB_SPEED_HIGH: 85 descriptors = f->hs_descriptors; 86 if (descriptors) 87 break; 88 fallthrough; 89 default: 90 descriptors = f->fs_descriptors; 91 } 92 93 /* 94 * if we can't find any descriptors at all, then this gadget deserves to 95 * Oops with a NULL pointer dereference 96 */ 97 98 return descriptors; 99 } 100 101 /** 102 * next_desc() - advance to the next desc_type descriptor 103 * @t: currect pointer within descriptor array 104 * @desc_type: descriptor type 105 * 106 * Return: next desc_type descriptor or NULL 107 * 108 * Iterate over @t until either desc_type descriptor found or 109 * NULL (that indicates end of list) encountered 110 */ 111 static struct usb_descriptor_header** 112 next_desc(struct usb_descriptor_header **t, u8 desc_type) 113 { 114 for (; *t; t++) { 115 if ((*t)->bDescriptorType == desc_type) 116 return t; 117 } 118 return NULL; 119 } 120 121 /* 122 * for_each_desc() - iterate over desc_type descriptors in the 123 * descriptors list 124 * @start: pointer within descriptor array. 125 * @iter_desc: desc_type descriptor to use as the loop cursor 126 * @desc_type: wanted descriptr type 127 */ 128 #define for_each_desc(start, iter_desc, desc_type) \ 129 for (iter_desc = next_desc(start, desc_type); \ 130 iter_desc; iter_desc = next_desc(iter_desc + 1, desc_type)) 131 132 /** 133 * config_ep_by_speed_and_alt() - configures the given endpoint 134 * according to gadget speed. 135 * @g: pointer to the gadget 136 * @f: usb function 137 * @_ep: the endpoint to configure 138 * @alt: alternate setting number 139 * 140 * Return: error code, 0 on success 141 * 142 * This function chooses the right descriptors for a given 143 * endpoint according to gadget speed and saves it in the 144 * endpoint desc field. If the endpoint already has a descriptor 145 * assigned to it - overwrites it with currently corresponding 146 * descriptor. The endpoint maxpacket field is updated according 147 * to the chosen descriptor. 148 * Note: the supplied function should hold all the descriptors 149 * for supported speeds 150 */ 151 int config_ep_by_speed_and_alt(struct usb_gadget *g, 152 struct usb_function *f, 153 struct usb_ep *_ep, 154 u8 alt) 155 { 156 struct usb_endpoint_descriptor *chosen_desc = NULL; 157 struct usb_interface_descriptor *int_desc = NULL; 158 struct usb_descriptor_header **speed_desc = NULL; 159 160 struct usb_ss_ep_comp_descriptor *comp_desc = NULL; 161 int want_comp_desc = 0; 162 163 struct usb_descriptor_header **d_spd; /* cursor for speed desc */ 164 struct usb_composite_dev *cdev; 165 bool incomplete_desc = false; 166 167 if (!g || !f || !_ep) 168 return -EIO; 169 170 /* select desired speed */ 171 switch (g->speed) { 172 case USB_SPEED_SUPER_PLUS: 173 if (gadget_is_superspeed_plus(g)) { 174 if (f->ssp_descriptors) { 175 speed_desc = f->ssp_descriptors; 176 want_comp_desc = 1; 177 break; 178 } 179 incomplete_desc = true; 180 } 181 fallthrough; 182 case USB_SPEED_SUPER: 183 if (gadget_is_superspeed(g)) { 184 if (f->ss_descriptors) { 185 speed_desc = f->ss_descriptors; 186 want_comp_desc = 1; 187 break; 188 } 189 incomplete_desc = true; 190 } 191 fallthrough; 192 case USB_SPEED_HIGH: 193 if (gadget_is_dualspeed(g)) { 194 if (f->hs_descriptors) { 195 speed_desc = f->hs_descriptors; 196 break; 197 } 198 incomplete_desc = true; 199 } 200 fallthrough; 201 default: 202 speed_desc = f->fs_descriptors; 203 } 204 205 cdev = get_gadget_data(g); 206 if (incomplete_desc) 207 WARNING(cdev, 208 "%s doesn't hold the descriptors for current speed\n", 209 f->name); 210 211 /* find correct alternate setting descriptor */ 212 for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) { 213 int_desc = (struct usb_interface_descriptor *)*d_spd; 214 215 if (int_desc->bAlternateSetting == alt) { 216 speed_desc = d_spd; 217 goto intf_found; 218 } 219 } 220 return -EIO; 221 222 intf_found: 223 /* find descriptors */ 224 for_each_desc(speed_desc, d_spd, USB_DT_ENDPOINT) { 225 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd; 226 if (chosen_desc->bEndpointAddress == _ep->address) 227 goto ep_found; 228 } 229 return -EIO; 230 231 ep_found: 232 /* commit results */ 233 _ep->maxpacket = usb_endpoint_maxp(chosen_desc); 234 _ep->desc = chosen_desc; 235 _ep->comp_desc = NULL; 236 _ep->maxburst = 0; 237 _ep->mult = 1; 238 239 if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) || 240 usb_endpoint_xfer_int(_ep->desc))) 241 _ep->mult = usb_endpoint_maxp_mult(_ep->desc); 242 243 if (!want_comp_desc) 244 return 0; 245 246 /* 247 * Companion descriptor should follow EP descriptor 248 * USB 3.0 spec, #9.6.7 249 */ 250 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd); 251 if (!comp_desc || 252 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP)) 253 return -EIO; 254 _ep->comp_desc = comp_desc; 255 if (g->speed >= USB_SPEED_SUPER) { 256 switch (usb_endpoint_type(_ep->desc)) { 257 case USB_ENDPOINT_XFER_ISOC: 258 /* mult: bits 1:0 of bmAttributes */ 259 _ep->mult = (comp_desc->bmAttributes & 0x3) + 1; 260 fallthrough; 261 case USB_ENDPOINT_XFER_BULK: 262 case USB_ENDPOINT_XFER_INT: 263 _ep->maxburst = comp_desc->bMaxBurst + 1; 264 break; 265 default: 266 if (comp_desc->bMaxBurst != 0) 267 ERROR(cdev, "ep0 bMaxBurst must be 0\n"); 268 _ep->maxburst = 1; 269 break; 270 } 271 } 272 return 0; 273 } 274 EXPORT_SYMBOL_GPL(config_ep_by_speed_and_alt); 275 276 /** 277 * config_ep_by_speed() - configures the given endpoint 278 * according to gadget speed. 279 * @g: pointer to the gadget 280 * @f: usb function 281 * @_ep: the endpoint to configure 282 * 283 * Return: error code, 0 on success 284 * 285 * This function chooses the right descriptors for a given 286 * endpoint according to gadget speed and saves it in the 287 * endpoint desc field. If the endpoint already has a descriptor 288 * assigned to it - overwrites it with currently corresponding 289 * descriptor. The endpoint maxpacket field is updated according 290 * to the chosen descriptor. 291 * Note: the supplied function should hold all the descriptors 292 * for supported speeds 293 */ 294 int config_ep_by_speed(struct usb_gadget *g, 295 struct usb_function *f, 296 struct usb_ep *_ep) 297 { 298 return config_ep_by_speed_and_alt(g, f, _ep, 0); 299 } 300 EXPORT_SYMBOL_GPL(config_ep_by_speed); 301 302 /** 303 * usb_add_function() - add a function to a configuration 304 * @config: the configuration 305 * @function: the function being added 306 * Context: single threaded during gadget setup 307 * 308 * After initialization, each configuration must have one or more 309 * functions added to it. Adding a function involves calling its @bind() 310 * method to allocate resources such as interface and string identifiers 311 * and endpoints. 312 * 313 * This function returns the value of the function's bind(), which is 314 * zero for success else a negative errno value. 315 */ 316 int usb_add_function(struct usb_configuration *config, 317 struct usb_function *function) 318 { 319 int value = -EINVAL; 320 321 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n", 322 function->name, function, 323 config->label, config); 324 325 if (!function->set_alt || !function->disable) 326 goto done; 327 328 function->config = config; 329 list_add_tail(&function->list, &config->functions); 330 331 if (function->bind_deactivated) { 332 value = usb_function_deactivate(function); 333 if (value) 334 goto done; 335 } 336 337 /* REVISIT *require* function->bind? */ 338 if (function->bind) { 339 value = function->bind(config, function); 340 if (value < 0) { 341 list_del(&function->list); 342 function->config = NULL; 343 } 344 } else 345 value = 0; 346 347 /* We allow configurations that don't work at both speeds. 348 * If we run into a lowspeed Linux system, treat it the same 349 * as full speed ... it's the function drivers that will need 350 * to avoid bulk and ISO transfers. 351 */ 352 if (!config->fullspeed && function->fs_descriptors) 353 config->fullspeed = true; 354 if (!config->highspeed && function->hs_descriptors) 355 config->highspeed = true; 356 if (!config->superspeed && function->ss_descriptors) 357 config->superspeed = true; 358 if (!config->superspeed_plus && function->ssp_descriptors) 359 config->superspeed_plus = true; 360 361 done: 362 if (value) 363 DBG(config->cdev, "adding '%s'/%p --> %d\n", 364 function->name, function, value); 365 return value; 366 } 367 EXPORT_SYMBOL_GPL(usb_add_function); 368 369 void usb_remove_function(struct usb_configuration *c, struct usb_function *f) 370 { 371 if (f->disable) 372 f->disable(f); 373 374 bitmap_zero(f->endpoints, 32); 375 list_del(&f->list); 376 if (f->unbind) 377 f->unbind(c, f); 378 379 if (f->bind_deactivated) 380 usb_function_activate(f); 381 } 382 EXPORT_SYMBOL_GPL(usb_remove_function); 383 384 /** 385 * usb_function_deactivate - prevent function and gadget enumeration 386 * @function: the function that isn't yet ready to respond 387 * 388 * Blocks response of the gadget driver to host enumeration by 389 * preventing the data line pullup from being activated. This is 390 * normally called during @bind() processing to change from the 391 * initial "ready to respond" state, or when a required resource 392 * becomes available. 393 * 394 * For example, drivers that serve as a passthrough to a userspace 395 * daemon can block enumeration unless that daemon (such as an OBEX, 396 * MTP, or print server) is ready to handle host requests. 397 * 398 * Not all systems support software control of their USB peripheral 399 * data pullups. 400 * 401 * Returns zero on success, else negative errno. 402 */ 403 int usb_function_deactivate(struct usb_function *function) 404 { 405 struct usb_composite_dev *cdev = function->config->cdev; 406 unsigned long flags; 407 int status = 0; 408 409 spin_lock_irqsave(&cdev->lock, flags); 410 411 if (cdev->deactivations == 0) { 412 spin_unlock_irqrestore(&cdev->lock, flags); 413 status = usb_gadget_deactivate(cdev->gadget); 414 spin_lock_irqsave(&cdev->lock, flags); 415 } 416 if (status == 0) 417 cdev->deactivations++; 418 419 spin_unlock_irqrestore(&cdev->lock, flags); 420 return status; 421 } 422 EXPORT_SYMBOL_GPL(usb_function_deactivate); 423 424 /** 425 * usb_function_activate - allow function and gadget enumeration 426 * @function: function on which usb_function_activate() was called 427 * 428 * Reverses effect of usb_function_deactivate(). If no more functions 429 * are delaying their activation, the gadget driver will respond to 430 * host enumeration procedures. 431 * 432 * Returns zero on success, else negative errno. 433 */ 434 int usb_function_activate(struct usb_function *function) 435 { 436 struct usb_composite_dev *cdev = function->config->cdev; 437 unsigned long flags; 438 int status = 0; 439 440 spin_lock_irqsave(&cdev->lock, flags); 441 442 if (WARN_ON(cdev->deactivations == 0)) 443 status = -EINVAL; 444 else { 445 cdev->deactivations--; 446 if (cdev->deactivations == 0) { 447 spin_unlock_irqrestore(&cdev->lock, flags); 448 status = usb_gadget_activate(cdev->gadget); 449 spin_lock_irqsave(&cdev->lock, flags); 450 } 451 } 452 453 spin_unlock_irqrestore(&cdev->lock, flags); 454 return status; 455 } 456 EXPORT_SYMBOL_GPL(usb_function_activate); 457 458 /** 459 * usb_interface_id() - allocate an unused interface ID 460 * @config: configuration associated with the interface 461 * @function: function handling the interface 462 * Context: single threaded during gadget setup 463 * 464 * usb_interface_id() is called from usb_function.bind() callbacks to 465 * allocate new interface IDs. The function driver will then store that 466 * ID in interface, association, CDC union, and other descriptors. It 467 * will also handle any control requests targeted at that interface, 468 * particularly changing its altsetting via set_alt(). There may 469 * also be class-specific or vendor-specific requests to handle. 470 * 471 * All interface identifier should be allocated using this routine, to 472 * ensure that for example different functions don't wrongly assign 473 * different meanings to the same identifier. Note that since interface 474 * identifiers are configuration-specific, functions used in more than 475 * one configuration (or more than once in a given configuration) need 476 * multiple versions of the relevant descriptors. 477 * 478 * Returns the interface ID which was allocated; or -ENODEV if no 479 * more interface IDs can be allocated. 480 */ 481 int usb_interface_id(struct usb_configuration *config, 482 struct usb_function *function) 483 { 484 unsigned id = config->next_interface_id; 485 486 if (id < MAX_CONFIG_INTERFACES) { 487 config->interface[id] = function; 488 config->next_interface_id = id + 1; 489 return id; 490 } 491 return -ENODEV; 492 } 493 EXPORT_SYMBOL_GPL(usb_interface_id); 494 495 static u8 encode_bMaxPower(enum usb_device_speed speed, 496 struct usb_configuration *c) 497 { 498 unsigned val; 499 500 if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER)) 501 val = c->MaxPower; 502 else 503 val = CONFIG_USB_GADGET_VBUS_DRAW; 504 if (!val) 505 return 0; 506 if (speed < USB_SPEED_SUPER) 507 return min(val, 500U) / 2; 508 else 509 /* 510 * USB 3.x supports up to 900mA, but since 900 isn't divisible 511 * by 8 the integral division will effectively cap to 896mA. 512 */ 513 return min(val, 900U) / 8; 514 } 515 516 static int config_buf(struct usb_configuration *config, 517 enum usb_device_speed speed, void *buf, u8 type) 518 { 519 struct usb_config_descriptor *c = buf; 520 void *next = buf + USB_DT_CONFIG_SIZE; 521 int len; 522 struct usb_function *f; 523 int status; 524 525 len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE; 526 /* write the config descriptor */ 527 c = buf; 528 c->bLength = USB_DT_CONFIG_SIZE; 529 c->bDescriptorType = type; 530 /* wTotalLength is written later */ 531 c->bNumInterfaces = config->next_interface_id; 532 c->bConfigurationValue = config->bConfigurationValue; 533 c->iConfiguration = config->iConfiguration; 534 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; 535 c->bMaxPower = encode_bMaxPower(speed, config); 536 537 /* There may be e.g. OTG descriptors */ 538 if (config->descriptors) { 539 status = usb_descriptor_fillbuf(next, len, 540 config->descriptors); 541 if (status < 0) 542 return status; 543 len -= status; 544 next += status; 545 } 546 547 /* add each function's descriptors */ 548 list_for_each_entry(f, &config->functions, list) { 549 struct usb_descriptor_header **descriptors; 550 551 descriptors = function_descriptors(f, speed); 552 if (!descriptors) 553 continue; 554 status = usb_descriptor_fillbuf(next, len, 555 (const struct usb_descriptor_header **) descriptors); 556 if (status < 0) 557 return status; 558 len -= status; 559 next += status; 560 } 561 562 len = next - buf; 563 c->wTotalLength = cpu_to_le16(len); 564 return len; 565 } 566 567 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) 568 { 569 struct usb_gadget *gadget = cdev->gadget; 570 struct usb_configuration *c; 571 struct list_head *pos; 572 u8 type = w_value >> 8; 573 enum usb_device_speed speed = USB_SPEED_UNKNOWN; 574 575 if (gadget->speed >= USB_SPEED_SUPER) 576 speed = gadget->speed; 577 else if (gadget_is_dualspeed(gadget)) { 578 int hs = 0; 579 if (gadget->speed == USB_SPEED_HIGH) 580 hs = 1; 581 if (type == USB_DT_OTHER_SPEED_CONFIG) 582 hs = !hs; 583 if (hs) 584 speed = USB_SPEED_HIGH; 585 586 } 587 588 /* This is a lookup by config *INDEX* */ 589 w_value &= 0xff; 590 591 pos = &cdev->configs; 592 c = cdev->os_desc_config; 593 if (c) 594 goto check_config; 595 596 while ((pos = pos->next) != &cdev->configs) { 597 c = list_entry(pos, typeof(*c), list); 598 599 /* skip OS Descriptors config which is handled separately */ 600 if (c == cdev->os_desc_config) 601 continue; 602 603 check_config: 604 /* ignore configs that won't work at this speed */ 605 switch (speed) { 606 case USB_SPEED_SUPER_PLUS: 607 if (!c->superspeed_plus) 608 continue; 609 break; 610 case USB_SPEED_SUPER: 611 if (!c->superspeed) 612 continue; 613 break; 614 case USB_SPEED_HIGH: 615 if (!c->highspeed) 616 continue; 617 break; 618 default: 619 if (!c->fullspeed) 620 continue; 621 } 622 623 if (w_value == 0) 624 return config_buf(c, speed, cdev->req->buf, type); 625 w_value--; 626 } 627 return -EINVAL; 628 } 629 630 static int count_configs(struct usb_composite_dev *cdev, unsigned type) 631 { 632 struct usb_gadget *gadget = cdev->gadget; 633 struct usb_configuration *c; 634 unsigned count = 0; 635 int hs = 0; 636 int ss = 0; 637 int ssp = 0; 638 639 if (gadget_is_dualspeed(gadget)) { 640 if (gadget->speed == USB_SPEED_HIGH) 641 hs = 1; 642 if (gadget->speed == USB_SPEED_SUPER) 643 ss = 1; 644 if (gadget->speed == USB_SPEED_SUPER_PLUS) 645 ssp = 1; 646 if (type == USB_DT_DEVICE_QUALIFIER) 647 hs = !hs; 648 } 649 list_for_each_entry(c, &cdev->configs, list) { 650 /* ignore configs that won't work at this speed */ 651 if (ssp) { 652 if (!c->superspeed_plus) 653 continue; 654 } else if (ss) { 655 if (!c->superspeed) 656 continue; 657 } else if (hs) { 658 if (!c->highspeed) 659 continue; 660 } else { 661 if (!c->fullspeed) 662 continue; 663 } 664 count++; 665 } 666 return count; 667 } 668 669 /** 670 * bos_desc() - prepares the BOS descriptor. 671 * @cdev: pointer to usb_composite device to generate the bos 672 * descriptor for 673 * 674 * This function generates the BOS (Binary Device Object) 675 * descriptor and its device capabilities descriptors. The BOS 676 * descriptor should be supported by a SuperSpeed device. 677 */ 678 static int bos_desc(struct usb_composite_dev *cdev) 679 { 680 struct usb_ext_cap_descriptor *usb_ext; 681 struct usb_dcd_config_params dcd_config_params; 682 struct usb_bos_descriptor *bos = cdev->req->buf; 683 unsigned int besl = 0; 684 685 bos->bLength = USB_DT_BOS_SIZE; 686 bos->bDescriptorType = USB_DT_BOS; 687 688 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); 689 bos->bNumDeviceCaps = 0; 690 691 /* Get Controller configuration */ 692 if (cdev->gadget->ops->get_config_params) { 693 cdev->gadget->ops->get_config_params(cdev->gadget, 694 &dcd_config_params); 695 } else { 696 dcd_config_params.besl_baseline = 697 USB_DEFAULT_BESL_UNSPECIFIED; 698 dcd_config_params.besl_deep = 699 USB_DEFAULT_BESL_UNSPECIFIED; 700 dcd_config_params.bU1devExitLat = 701 USB_DEFAULT_U1_DEV_EXIT_LAT; 702 dcd_config_params.bU2DevExitLat = 703 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT); 704 } 705 706 if (dcd_config_params.besl_baseline != USB_DEFAULT_BESL_UNSPECIFIED) 707 besl = USB_BESL_BASELINE_VALID | 708 USB_SET_BESL_BASELINE(dcd_config_params.besl_baseline); 709 710 if (dcd_config_params.besl_deep != USB_DEFAULT_BESL_UNSPECIFIED) 711 besl |= USB_BESL_DEEP_VALID | 712 USB_SET_BESL_DEEP(dcd_config_params.besl_deep); 713 714 /* 715 * A SuperSpeed device shall include the USB2.0 extension descriptor 716 * and shall support LPM when operating in USB2.0 HS mode. 717 */ 718 if (cdev->gadget->lpm_capable) { 719 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 720 bos->bNumDeviceCaps++; 721 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE); 722 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE; 723 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 724 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT; 725 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT | 726 USB_BESL_SUPPORT | besl); 727 } 728 729 /* 730 * The Superspeed USB Capability descriptor shall be implemented by all 731 * SuperSpeed devices. 732 */ 733 if (gadget_is_superspeed(cdev->gadget)) { 734 struct usb_ss_cap_descriptor *ss_cap; 735 736 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 737 bos->bNumDeviceCaps++; 738 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE); 739 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE; 740 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 741 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE; 742 ss_cap->bmAttributes = 0; /* LTM is not supported yet */ 743 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION | 744 USB_FULL_SPEED_OPERATION | 745 USB_HIGH_SPEED_OPERATION | 746 USB_5GBPS_OPERATION); 747 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION; 748 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat; 749 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat; 750 } 751 752 /* The SuperSpeedPlus USB Device Capability descriptor */ 753 if (gadget_is_superspeed_plus(cdev->gadget)) { 754 struct usb_ssp_cap_descriptor *ssp_cap; 755 u8 ssac = 1; 756 u8 ssic; 757 int i; 758 759 if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x2) 760 ssac = 3; 761 762 /* 763 * Paired RX and TX sublink speed attributes share 764 * the same SSID. 765 */ 766 ssic = (ssac + 1) / 2 - 1; 767 768 ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 769 bos->bNumDeviceCaps++; 770 771 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(ssac)); 772 ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(ssac); 773 ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 774 ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE; 775 ssp_cap->bReserved = 0; 776 ssp_cap->wReserved = 0; 777 778 ssp_cap->bmAttributes = 779 cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_ATTRIBS, ssac) | 780 FIELD_PREP(USB_SSP_SUBLINK_SPEED_IDS, ssic)); 781 782 ssp_cap->wFunctionalitySupport = 783 cpu_to_le16(FIELD_PREP(USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID, 0) | 784 FIELD_PREP(USB_SSP_MIN_RX_LANE_COUNT, 1) | 785 FIELD_PREP(USB_SSP_MIN_TX_LANE_COUNT, 1)); 786 787 /* 788 * Use 1 SSID if the gadget supports up to gen2x1 or not 789 * specified: 790 * - SSID 0 for symmetric RX/TX sublink speed of 10 Gbps. 791 * 792 * Use 1 SSID if the gadget supports up to gen1x2: 793 * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps. 794 * 795 * Use 2 SSIDs if the gadget supports up to gen2x2: 796 * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps. 797 * - SSID 1 for symmetric RX/TX sublink speed of 10 Gbps. 798 */ 799 for (i = 0; i < ssac + 1; i++) { 800 u8 ssid; 801 u8 mantissa; 802 u8 type; 803 804 ssid = i >> 1; 805 806 if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x1 || 807 cdev->gadget->max_ssp_rate == USB_SSP_GEN_UNKNOWN) 808 mantissa = 10; 809 else 810 mantissa = 5 << ssid; 811 812 if (i % 2) 813 type = USB_SSP_SUBLINK_SPEED_ST_SYM_TX; 814 else 815 type = USB_SSP_SUBLINK_SPEED_ST_SYM_RX; 816 817 ssp_cap->bmSublinkSpeedAttr[i] = 818 cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_SSID, ssid) | 819 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSE, 820 USB_SSP_SUBLINK_SPEED_LSE_GBPS) | 821 FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST, type) | 822 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LP, 823 USB_SSP_SUBLINK_SPEED_LP_SSP) | 824 FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSM, mantissa)); 825 } 826 } 827 828 /* The WebUSB Platform Capability descriptor */ 829 if (cdev->use_webusb) { 830 struct usb_plat_dev_cap_descriptor *webusb_cap; 831 struct usb_webusb_cap_data *webusb_cap_data; 832 guid_t webusb_uuid = WEBUSB_UUID; 833 834 webusb_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength); 835 webusb_cap_data = (struct usb_webusb_cap_data *) webusb_cap->CapabilityData; 836 bos->bNumDeviceCaps++; 837 le16_add_cpu(&bos->wTotalLength, 838 USB_DT_USB_PLAT_DEV_CAP_SIZE(USB_WEBUSB_CAP_DATA_SIZE)); 839 840 webusb_cap->bLength = USB_DT_USB_PLAT_DEV_CAP_SIZE(USB_WEBUSB_CAP_DATA_SIZE); 841 webusb_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY; 842 webusb_cap->bDevCapabilityType = USB_PLAT_DEV_CAP_TYPE; 843 webusb_cap->bReserved = 0; 844 export_guid(webusb_cap->UUID, &webusb_uuid); 845 846 if (cdev->bcd_webusb_version != 0) 847 webusb_cap_data->bcdVersion = cpu_to_le16(cdev->bcd_webusb_version); 848 else 849 webusb_cap_data->bcdVersion = WEBUSB_VERSION_1_00; 850 851 webusb_cap_data->bVendorCode = cdev->b_webusb_vendor_code; 852 853 if (strnlen(cdev->landing_page, sizeof(cdev->landing_page)) > 0) 854 webusb_cap_data->iLandingPage = WEBUSB_LANDING_PAGE_PRESENT; 855 else 856 webusb_cap_data->iLandingPage = WEBUSB_LANDING_PAGE_NOT_PRESENT; 857 } 858 859 return le16_to_cpu(bos->wTotalLength); 860 } 861 862 static void device_qual(struct usb_composite_dev *cdev) 863 { 864 struct usb_qualifier_descriptor *qual = cdev->req->buf; 865 866 qual->bLength = sizeof(*qual); 867 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; 868 /* POLICY: same bcdUSB and device type info at both speeds */ 869 qual->bcdUSB = cdev->desc.bcdUSB; 870 qual->bDeviceClass = cdev->desc.bDeviceClass; 871 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; 872 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; 873 /* ASSUME same EP0 fifo size at both speeds */ 874 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; 875 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); 876 qual->bRESERVED = 0; 877 } 878 879 /*-------------------------------------------------------------------------*/ 880 881 static void reset_config(struct usb_composite_dev *cdev) 882 { 883 struct usb_function *f; 884 885 DBG(cdev, "reset config\n"); 886 887 list_for_each_entry(f, &cdev->config->functions, list) { 888 if (f->disable) 889 f->disable(f); 890 891 bitmap_zero(f->endpoints, 32); 892 } 893 cdev->config = NULL; 894 cdev->delayed_status = 0; 895 } 896 897 static int set_config(struct usb_composite_dev *cdev, 898 const struct usb_ctrlrequest *ctrl, unsigned number) 899 { 900 struct usb_gadget *gadget = cdev->gadget; 901 struct usb_configuration *c = NULL, *iter; 902 int result = -EINVAL; 903 unsigned power = gadget_is_otg(gadget) ? 8 : 100; 904 int tmp; 905 906 if (number) { 907 list_for_each_entry(iter, &cdev->configs, list) { 908 if (iter->bConfigurationValue != number) 909 continue; 910 /* 911 * We disable the FDs of the previous 912 * configuration only if the new configuration 913 * is a valid one 914 */ 915 if (cdev->config) 916 reset_config(cdev); 917 c = iter; 918 result = 0; 919 break; 920 } 921 if (result < 0) 922 goto done; 923 } else { /* Zero configuration value - need to reset the config */ 924 if (cdev->config) 925 reset_config(cdev); 926 result = 0; 927 } 928 929 DBG(cdev, "%s config #%d: %s\n", 930 usb_speed_string(gadget->speed), 931 number, c ? c->label : "unconfigured"); 932 933 if (!c) 934 goto done; 935 936 usb_gadget_set_state(gadget, USB_STATE_CONFIGURED); 937 cdev->config = c; 938 939 /* Initialize all interfaces by setting them to altsetting zero. */ 940 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { 941 struct usb_function *f = c->interface[tmp]; 942 struct usb_descriptor_header **descriptors; 943 944 if (!f) 945 break; 946 947 /* 948 * Record which endpoints are used by the function. This is used 949 * to dispatch control requests targeted at that endpoint to the 950 * function's setup callback instead of the current 951 * configuration's setup callback. 952 */ 953 descriptors = function_descriptors(f, gadget->speed); 954 955 for (; *descriptors; ++descriptors) { 956 struct usb_endpoint_descriptor *ep; 957 int addr; 958 959 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) 960 continue; 961 962 ep = (struct usb_endpoint_descriptor *)*descriptors; 963 addr = ((ep->bEndpointAddress & 0x80) >> 3) 964 | (ep->bEndpointAddress & 0x0f); 965 set_bit(addr, f->endpoints); 966 } 967 968 result = f->set_alt(f, tmp, 0); 969 if (result < 0) { 970 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n", 971 tmp, f->name, f, result); 972 973 reset_config(cdev); 974 goto done; 975 } 976 977 if (result == USB_GADGET_DELAYED_STATUS) { 978 DBG(cdev, 979 "%s: interface %d (%s) requested delayed status\n", 980 __func__, tmp, f->name); 981 cdev->delayed_status++; 982 DBG(cdev, "delayed_status count %d\n", 983 cdev->delayed_status); 984 } 985 } 986 987 /* when we return, be sure our power usage is valid */ 988 if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER)) 989 power = c->MaxPower; 990 else 991 power = CONFIG_USB_GADGET_VBUS_DRAW; 992 993 if (gadget->speed < USB_SPEED_SUPER) 994 power = min(power, 500U); 995 else 996 power = min(power, 900U); 997 done: 998 if (power <= USB_SELF_POWER_VBUS_MAX_DRAW) 999 usb_gadget_set_selfpowered(gadget); 1000 else 1001 usb_gadget_clear_selfpowered(gadget); 1002 1003 usb_gadget_vbus_draw(gadget, power); 1004 if (result >= 0 && cdev->delayed_status) 1005 result = USB_GADGET_DELAYED_STATUS; 1006 return result; 1007 } 1008 1009 int usb_add_config_only(struct usb_composite_dev *cdev, 1010 struct usb_configuration *config) 1011 { 1012 struct usb_configuration *c; 1013 1014 if (!config->bConfigurationValue) 1015 return -EINVAL; 1016 1017 /* Prevent duplicate configuration identifiers */ 1018 list_for_each_entry(c, &cdev->configs, list) { 1019 if (c->bConfigurationValue == config->bConfigurationValue) 1020 return -EBUSY; 1021 } 1022 1023 config->cdev = cdev; 1024 list_add_tail(&config->list, &cdev->configs); 1025 1026 INIT_LIST_HEAD(&config->functions); 1027 config->next_interface_id = 0; 1028 memset(config->interface, 0, sizeof(config->interface)); 1029 1030 return 0; 1031 } 1032 EXPORT_SYMBOL_GPL(usb_add_config_only); 1033 1034 /** 1035 * usb_add_config() - add a configuration to a device. 1036 * @cdev: wraps the USB gadget 1037 * @config: the configuration, with bConfigurationValue assigned 1038 * @bind: the configuration's bind function 1039 * Context: single threaded during gadget setup 1040 * 1041 * One of the main tasks of a composite @bind() routine is to 1042 * add each of the configurations it supports, using this routine. 1043 * 1044 * This function returns the value of the configuration's @bind(), which 1045 * is zero for success else a negative errno value. Binding configurations 1046 * assigns global resources including string IDs, and per-configuration 1047 * resources such as interface IDs and endpoints. 1048 */ 1049 int usb_add_config(struct usb_composite_dev *cdev, 1050 struct usb_configuration *config, 1051 int (*bind)(struct usb_configuration *)) 1052 { 1053 int status = -EINVAL; 1054 1055 if (!bind) 1056 goto done; 1057 1058 DBG(cdev, "adding config #%u '%s'/%p\n", 1059 config->bConfigurationValue, 1060 config->label, config); 1061 1062 status = usb_add_config_only(cdev, config); 1063 if (status) 1064 goto done; 1065 1066 status = bind(config); 1067 if (status < 0) { 1068 while (!list_empty(&config->functions)) { 1069 struct usb_function *f; 1070 1071 f = list_first_entry(&config->functions, 1072 struct usb_function, list); 1073 list_del(&f->list); 1074 if (f->unbind) { 1075 DBG(cdev, "unbind function '%s'/%p\n", 1076 f->name, f); 1077 f->unbind(config, f); 1078 /* may free memory for "f" */ 1079 } 1080 } 1081 list_del(&config->list); 1082 config->cdev = NULL; 1083 } else { 1084 unsigned i; 1085 1086 DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n", 1087 config->bConfigurationValue, config, 1088 config->superspeed_plus ? " superplus" : "", 1089 config->superspeed ? " super" : "", 1090 config->highspeed ? " high" : "", 1091 config->fullspeed 1092 ? (gadget_is_dualspeed(cdev->gadget) 1093 ? " full" 1094 : " full/low") 1095 : ""); 1096 1097 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { 1098 struct usb_function *f = config->interface[i]; 1099 1100 if (!f) 1101 continue; 1102 DBG(cdev, " interface %d = %s/%p\n", 1103 i, f->name, f); 1104 } 1105 } 1106 1107 /* set_alt(), or next bind(), sets up ep->claimed as needed */ 1108 usb_ep_autoconfig_reset(cdev->gadget); 1109 1110 done: 1111 if (status) 1112 DBG(cdev, "added config '%s'/%u --> %d\n", config->label, 1113 config->bConfigurationValue, status); 1114 return status; 1115 } 1116 EXPORT_SYMBOL_GPL(usb_add_config); 1117 1118 static void remove_config(struct usb_composite_dev *cdev, 1119 struct usb_configuration *config) 1120 { 1121 while (!list_empty(&config->functions)) { 1122 struct usb_function *f; 1123 1124 f = list_first_entry(&config->functions, 1125 struct usb_function, list); 1126 1127 usb_remove_function(config, f); 1128 } 1129 list_del(&config->list); 1130 if (config->unbind) { 1131 DBG(cdev, "unbind config '%s'/%p\n", config->label, config); 1132 config->unbind(config); 1133 /* may free memory for "c" */ 1134 } 1135 } 1136 1137 /** 1138 * usb_remove_config() - remove a configuration from a device. 1139 * @cdev: wraps the USB gadget 1140 * @config: the configuration 1141 * 1142 * Drivers must call usb_gadget_disconnect before calling this function 1143 * to disconnect the device from the host and make sure the host will not 1144 * try to enumerate the device while we are changing the config list. 1145 */ 1146 void usb_remove_config(struct usb_composite_dev *cdev, 1147 struct usb_configuration *config) 1148 { 1149 unsigned long flags; 1150 1151 spin_lock_irqsave(&cdev->lock, flags); 1152 1153 if (cdev->config == config) 1154 reset_config(cdev); 1155 1156 spin_unlock_irqrestore(&cdev->lock, flags); 1157 1158 remove_config(cdev, config); 1159 } 1160 1161 /*-------------------------------------------------------------------------*/ 1162 1163 /* We support strings in multiple languages ... string descriptor zero 1164 * says which languages are supported. The typical case will be that 1165 * only one language (probably English) is used, with i18n handled on 1166 * the host side. 1167 */ 1168 1169 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) 1170 { 1171 const struct usb_gadget_strings *s; 1172 __le16 language; 1173 __le16 *tmp; 1174 1175 while (*sp) { 1176 s = *sp; 1177 language = cpu_to_le16(s->language); 1178 for (tmp = buf; *tmp && tmp < &buf[USB_MAX_STRING_LEN]; tmp++) { 1179 if (*tmp == language) 1180 goto repeat; 1181 } 1182 *tmp++ = language; 1183 repeat: 1184 sp++; 1185 } 1186 } 1187 1188 static int lookup_string( 1189 struct usb_gadget_strings **sp, 1190 void *buf, 1191 u16 language, 1192 int id 1193 ) 1194 { 1195 struct usb_gadget_strings *s; 1196 int value; 1197 1198 while (*sp) { 1199 s = *sp++; 1200 if (s->language != language) 1201 continue; 1202 value = usb_gadget_get_string(s, id, buf); 1203 if (value > 0) 1204 return value; 1205 } 1206 return -EINVAL; 1207 } 1208 1209 static int get_string(struct usb_composite_dev *cdev, 1210 void *buf, u16 language, int id) 1211 { 1212 struct usb_composite_driver *composite = cdev->driver; 1213 struct usb_gadget_string_container *uc; 1214 struct usb_configuration *c; 1215 struct usb_function *f; 1216 int len; 1217 1218 /* Yes, not only is USB's i18n support probably more than most 1219 * folk will ever care about ... also, it's all supported here. 1220 * (Except for UTF8 support for Unicode's "Astral Planes".) 1221 */ 1222 1223 /* 0 == report all available language codes */ 1224 if (id == 0) { 1225 struct usb_string_descriptor *s = buf; 1226 struct usb_gadget_strings **sp; 1227 1228 memset(s, 0, 256); 1229 s->bDescriptorType = USB_DT_STRING; 1230 1231 sp = composite->strings; 1232 if (sp) 1233 collect_langs(sp, s->wData); 1234 1235 list_for_each_entry(c, &cdev->configs, list) { 1236 sp = c->strings; 1237 if (sp) 1238 collect_langs(sp, s->wData); 1239 1240 list_for_each_entry(f, &c->functions, list) { 1241 sp = f->strings; 1242 if (sp) 1243 collect_langs(sp, s->wData); 1244 } 1245 } 1246 list_for_each_entry(uc, &cdev->gstrings, list) { 1247 struct usb_gadget_strings **sp; 1248 1249 sp = get_containers_gs(uc); 1250 collect_langs(sp, s->wData); 1251 } 1252 1253 for (len = 0; len <= USB_MAX_STRING_LEN && s->wData[len]; len++) 1254 continue; 1255 if (!len) 1256 return -EINVAL; 1257 1258 s->bLength = 2 * (len + 1); 1259 return s->bLength; 1260 } 1261 1262 if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) { 1263 struct usb_os_string *b = buf; 1264 b->bLength = sizeof(*b); 1265 b->bDescriptorType = USB_DT_STRING; 1266 compiletime_assert( 1267 sizeof(b->qwSignature) == sizeof(cdev->qw_sign), 1268 "qwSignature size must be equal to qw_sign"); 1269 memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature)); 1270 b->bMS_VendorCode = cdev->b_vendor_code; 1271 b->bPad = 0; 1272 return sizeof(*b); 1273 } 1274 1275 list_for_each_entry(uc, &cdev->gstrings, list) { 1276 struct usb_gadget_strings **sp; 1277 1278 sp = get_containers_gs(uc); 1279 len = lookup_string(sp, buf, language, id); 1280 if (len > 0) 1281 return len; 1282 } 1283 1284 /* String IDs are device-scoped, so we look up each string 1285 * table we're told about. These lookups are infrequent; 1286 * simpler-is-better here. 1287 */ 1288 if (composite->strings) { 1289 len = lookup_string(composite->strings, buf, language, id); 1290 if (len > 0) 1291 return len; 1292 } 1293 list_for_each_entry(c, &cdev->configs, list) { 1294 if (c->strings) { 1295 len = lookup_string(c->strings, buf, language, id); 1296 if (len > 0) 1297 return len; 1298 } 1299 list_for_each_entry(f, &c->functions, list) { 1300 if (!f->strings) 1301 continue; 1302 len = lookup_string(f->strings, buf, language, id); 1303 if (len > 0) 1304 return len; 1305 } 1306 } 1307 return -EINVAL; 1308 } 1309 1310 /** 1311 * usb_string_id() - allocate an unused string ID 1312 * @cdev: the device whose string descriptor IDs are being allocated 1313 * Context: single threaded during gadget setup 1314 * 1315 * @usb_string_id() is called from bind() callbacks to allocate 1316 * string IDs. Drivers for functions, configurations, or gadgets will 1317 * then store that ID in the appropriate descriptors and string table. 1318 * 1319 * All string identifier should be allocated using this, 1320 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure 1321 * that for example different functions don't wrongly assign different 1322 * meanings to the same identifier. 1323 */ 1324 int usb_string_id(struct usb_composite_dev *cdev) 1325 { 1326 if (cdev->next_string_id < 254) { 1327 /* string id 0 is reserved by USB spec for list of 1328 * supported languages */ 1329 /* 255 reserved as well? -- mina86 */ 1330 cdev->next_string_id++; 1331 return cdev->next_string_id; 1332 } 1333 return -ENODEV; 1334 } 1335 EXPORT_SYMBOL_GPL(usb_string_id); 1336 1337 /** 1338 * usb_string_ids_tab() - allocate unused string IDs in batch 1339 * @cdev: the device whose string descriptor IDs are being allocated 1340 * @str: an array of usb_string objects to assign numbers to 1341 * Context: single threaded during gadget setup 1342 * 1343 * @usb_string_ids() is called from bind() callbacks to allocate 1344 * string IDs. Drivers for functions, configurations, or gadgets will 1345 * then copy IDs from the string table to the appropriate descriptors 1346 * and string table for other languages. 1347 * 1348 * All string identifier should be allocated using this, 1349 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for 1350 * example different functions don't wrongly assign different meanings 1351 * to the same identifier. 1352 */ 1353 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) 1354 { 1355 int next = cdev->next_string_id; 1356 1357 for (; str->s; ++str) { 1358 if (unlikely(next >= 254)) 1359 return -ENODEV; 1360 str->id = ++next; 1361 } 1362 1363 cdev->next_string_id = next; 1364 1365 return 0; 1366 } 1367 EXPORT_SYMBOL_GPL(usb_string_ids_tab); 1368 1369 static struct usb_gadget_string_container *copy_gadget_strings( 1370 struct usb_gadget_strings **sp, unsigned n_gstrings, 1371 unsigned n_strings) 1372 { 1373 struct usb_gadget_string_container *uc; 1374 struct usb_gadget_strings **gs_array; 1375 struct usb_gadget_strings *gs; 1376 struct usb_string *s; 1377 unsigned mem; 1378 unsigned n_gs; 1379 unsigned n_s; 1380 void *stash; 1381 1382 mem = sizeof(*uc); 1383 mem += sizeof(void *) * (n_gstrings + 1); 1384 mem += sizeof(struct usb_gadget_strings) * n_gstrings; 1385 mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings); 1386 uc = kmalloc(mem, GFP_KERNEL); 1387 if (!uc) 1388 return ERR_PTR(-ENOMEM); 1389 gs_array = get_containers_gs(uc); 1390 stash = uc->stash; 1391 stash += sizeof(void *) * (n_gstrings + 1); 1392 for (n_gs = 0; n_gs < n_gstrings; n_gs++) { 1393 struct usb_string *org_s; 1394 1395 gs_array[n_gs] = stash; 1396 gs = gs_array[n_gs]; 1397 stash += sizeof(struct usb_gadget_strings); 1398 gs->language = sp[n_gs]->language; 1399 gs->strings = stash; 1400 org_s = sp[n_gs]->strings; 1401 1402 for (n_s = 0; n_s < n_strings; n_s++) { 1403 s = stash; 1404 stash += sizeof(struct usb_string); 1405 if (org_s->s) 1406 s->s = org_s->s; 1407 else 1408 s->s = ""; 1409 org_s++; 1410 } 1411 s = stash; 1412 s->s = NULL; 1413 stash += sizeof(struct usb_string); 1414 1415 } 1416 gs_array[n_gs] = NULL; 1417 return uc; 1418 } 1419 1420 /** 1421 * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids 1422 * @cdev: the device whose string descriptor IDs are being allocated 1423 * and attached. 1424 * @sp: an array of usb_gadget_strings to attach. 1425 * @n_strings: number of entries in each usb_strings array (sp[]->strings) 1426 * 1427 * This function will create a deep copy of usb_gadget_strings and usb_string 1428 * and attach it to the cdev. The actual string (usb_string.s) will not be 1429 * copied but only a referenced will be made. The struct usb_gadget_strings 1430 * array may contain multiple languages and should be NULL terminated. 1431 * The ->language pointer of each struct usb_gadget_strings has to contain the 1432 * same amount of entries. 1433 * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first 1434 * usb_string entry of es-ES contains the translation of the first usb_string 1435 * entry of en-US. Therefore both entries become the same id assign. 1436 */ 1437 struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev, 1438 struct usb_gadget_strings **sp, unsigned n_strings) 1439 { 1440 struct usb_gadget_string_container *uc; 1441 struct usb_gadget_strings **n_gs; 1442 unsigned n_gstrings = 0; 1443 unsigned i; 1444 int ret; 1445 1446 for (i = 0; sp[i]; i++) 1447 n_gstrings++; 1448 1449 if (!n_gstrings) 1450 return ERR_PTR(-EINVAL); 1451 1452 uc = copy_gadget_strings(sp, n_gstrings, n_strings); 1453 if (IS_ERR(uc)) 1454 return ERR_CAST(uc); 1455 1456 n_gs = get_containers_gs(uc); 1457 ret = usb_string_ids_tab(cdev, n_gs[0]->strings); 1458 if (ret) 1459 goto err; 1460 1461 for (i = 1; i < n_gstrings; i++) { 1462 struct usb_string *m_s; 1463 struct usb_string *s; 1464 unsigned n; 1465 1466 m_s = n_gs[0]->strings; 1467 s = n_gs[i]->strings; 1468 for (n = 0; n < n_strings; n++) { 1469 s->id = m_s->id; 1470 s++; 1471 m_s++; 1472 } 1473 } 1474 list_add_tail(&uc->list, &cdev->gstrings); 1475 return n_gs[0]->strings; 1476 err: 1477 kfree(uc); 1478 return ERR_PTR(ret); 1479 } 1480 EXPORT_SYMBOL_GPL(usb_gstrings_attach); 1481 1482 /** 1483 * usb_string_ids_n() - allocate unused string IDs in batch 1484 * @c: the device whose string descriptor IDs are being allocated 1485 * @n: number of string IDs to allocate 1486 * Context: single threaded during gadget setup 1487 * 1488 * Returns the first requested ID. This ID and next @n-1 IDs are now 1489 * valid IDs. At least provided that @n is non-zero because if it 1490 * is, returns last requested ID which is now very useful information. 1491 * 1492 * @usb_string_ids_n() is called from bind() callbacks to allocate 1493 * string IDs. Drivers for functions, configurations, or gadgets will 1494 * then store that ID in the appropriate descriptors and string table. 1495 * 1496 * All string identifier should be allocated using this, 1497 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for 1498 * example different functions don't wrongly assign different meanings 1499 * to the same identifier. 1500 */ 1501 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) 1502 { 1503 unsigned next = c->next_string_id; 1504 if (unlikely(n > 254 || (unsigned)next + n > 254)) 1505 return -ENODEV; 1506 c->next_string_id += n; 1507 return next + 1; 1508 } 1509 EXPORT_SYMBOL_GPL(usb_string_ids_n); 1510 1511 /*-------------------------------------------------------------------------*/ 1512 1513 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) 1514 { 1515 struct usb_composite_dev *cdev; 1516 1517 if (req->status || req->actual != req->length) 1518 DBG((struct usb_composite_dev *) ep->driver_data, 1519 "setup complete --> %d, %d/%d\n", 1520 req->status, req->actual, req->length); 1521 1522 /* 1523 * REVIST The same ep0 requests are shared with function drivers 1524 * so they don't have to maintain the same ->complete() stubs. 1525 * 1526 * Because of that, we need to check for the validity of ->context 1527 * here, even though we know we've set it to something useful. 1528 */ 1529 if (!req->context) 1530 return; 1531 1532 cdev = req->context; 1533 1534 if (cdev->req == req) 1535 cdev->setup_pending = false; 1536 else if (cdev->os_desc_req == req) 1537 cdev->os_desc_pending = false; 1538 else 1539 WARN(1, "unknown request %p\n", req); 1540 } 1541 1542 static int composite_ep0_queue(struct usb_composite_dev *cdev, 1543 struct usb_request *req, gfp_t gfp_flags) 1544 { 1545 int ret; 1546 1547 ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags); 1548 if (ret == 0) { 1549 if (cdev->req == req) 1550 cdev->setup_pending = true; 1551 else if (cdev->os_desc_req == req) 1552 cdev->os_desc_pending = true; 1553 else 1554 WARN(1, "unknown request %p\n", req); 1555 } 1556 1557 return ret; 1558 } 1559 1560 static int count_ext_compat(struct usb_configuration *c) 1561 { 1562 int i, res; 1563 1564 res = 0; 1565 for (i = 0; i < c->next_interface_id; ++i) { 1566 struct usb_function *f; 1567 int j; 1568 1569 f = c->interface[i]; 1570 for (j = 0; j < f->os_desc_n; ++j) { 1571 struct usb_os_desc *d; 1572 1573 if (i != f->os_desc_table[j].if_id) 1574 continue; 1575 d = f->os_desc_table[j].os_desc; 1576 if (d && d->ext_compat_id) 1577 ++res; 1578 } 1579 } 1580 BUG_ON(res > 255); 1581 return res; 1582 } 1583 1584 static int fill_ext_compat(struct usb_configuration *c, u8 *buf) 1585 { 1586 int i, count; 1587 1588 count = 16; 1589 buf += 16; 1590 for (i = 0; i < c->next_interface_id; ++i) { 1591 struct usb_function *f; 1592 int j; 1593 1594 f = c->interface[i]; 1595 for (j = 0; j < f->os_desc_n; ++j) { 1596 struct usb_os_desc *d; 1597 1598 if (i != f->os_desc_table[j].if_id) 1599 continue; 1600 d = f->os_desc_table[j].os_desc; 1601 if (d && d->ext_compat_id) { 1602 *buf++ = i; 1603 *buf++ = 0x01; 1604 memcpy(buf, d->ext_compat_id, 16); 1605 buf += 22; 1606 } else { 1607 ++buf; 1608 *buf = 0x01; 1609 buf += 23; 1610 } 1611 count += 24; 1612 if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ) 1613 return count; 1614 } 1615 } 1616 1617 return count; 1618 } 1619 1620 static int count_ext_prop(struct usb_configuration *c, int interface) 1621 { 1622 struct usb_function *f; 1623 int j; 1624 1625 f = c->interface[interface]; 1626 for (j = 0; j < f->os_desc_n; ++j) { 1627 struct usb_os_desc *d; 1628 1629 if (interface != f->os_desc_table[j].if_id) 1630 continue; 1631 d = f->os_desc_table[j].os_desc; 1632 if (d && d->ext_compat_id) 1633 return d->ext_prop_count; 1634 } 1635 return 0; 1636 } 1637 1638 static int len_ext_prop(struct usb_configuration *c, int interface) 1639 { 1640 struct usb_function *f; 1641 struct usb_os_desc *d; 1642 int j, res; 1643 1644 res = 10; /* header length */ 1645 f = c->interface[interface]; 1646 for (j = 0; j < f->os_desc_n; ++j) { 1647 if (interface != f->os_desc_table[j].if_id) 1648 continue; 1649 d = f->os_desc_table[j].os_desc; 1650 if (d) 1651 return min(res + d->ext_prop_len, 4096); 1652 } 1653 return res; 1654 } 1655 1656 static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf) 1657 { 1658 struct usb_function *f; 1659 struct usb_os_desc *d; 1660 struct usb_os_desc_ext_prop *ext_prop; 1661 int j, count, n, ret; 1662 1663 f = c->interface[interface]; 1664 count = 10; /* header length */ 1665 buf += 10; 1666 for (j = 0; j < f->os_desc_n; ++j) { 1667 if (interface != f->os_desc_table[j].if_id) 1668 continue; 1669 d = f->os_desc_table[j].os_desc; 1670 if (d) 1671 list_for_each_entry(ext_prop, &d->ext_prop, entry) { 1672 n = ext_prop->data_len + 1673 ext_prop->name_len + 14; 1674 if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ) 1675 return count; 1676 usb_ext_prop_put_size(buf, n); 1677 usb_ext_prop_put_type(buf, ext_prop->type); 1678 ret = usb_ext_prop_put_name(buf, ext_prop->name, 1679 ext_prop->name_len); 1680 if (ret < 0) 1681 return ret; 1682 switch (ext_prop->type) { 1683 case USB_EXT_PROP_UNICODE: 1684 case USB_EXT_PROP_UNICODE_ENV: 1685 case USB_EXT_PROP_UNICODE_LINK: 1686 usb_ext_prop_put_unicode(buf, ret, 1687 ext_prop->data, 1688 ext_prop->data_len); 1689 break; 1690 case USB_EXT_PROP_BINARY: 1691 usb_ext_prop_put_binary(buf, ret, 1692 ext_prop->data, 1693 ext_prop->data_len); 1694 break; 1695 case USB_EXT_PROP_LE32: 1696 /* not implemented */ 1697 case USB_EXT_PROP_BE32: 1698 /* not implemented */ 1699 default: 1700 return -EINVAL; 1701 } 1702 buf += n; 1703 count += n; 1704 } 1705 } 1706 1707 return count; 1708 } 1709 1710 /* 1711 * The setup() callback implements all the ep0 functionality that's 1712 * not handled lower down, in hardware or the hardware driver(like 1713 * device and endpoint feature flags, and their status). It's all 1714 * housekeeping for the gadget function we're implementing. Most of 1715 * the work is in config and function specific setup. 1716 */ 1717 int 1718 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 1719 { 1720 struct usb_composite_dev *cdev = get_gadget_data(gadget); 1721 struct usb_request *req = cdev->req; 1722 int value = -EOPNOTSUPP; 1723 int status = 0; 1724 u16 w_index = le16_to_cpu(ctrl->wIndex); 1725 u8 intf = w_index & 0xFF; 1726 u16 w_value = le16_to_cpu(ctrl->wValue); 1727 u16 w_length = le16_to_cpu(ctrl->wLength); 1728 struct usb_function *f = NULL; 1729 struct usb_function *iter; 1730 u8 endp; 1731 1732 if (w_length > USB_COMP_EP0_BUFSIZ) { 1733 if (ctrl->bRequestType & USB_DIR_IN) { 1734 /* Cast away the const, we are going to overwrite on purpose. */ 1735 __le16 *temp = (__le16 *)&ctrl->wLength; 1736 1737 *temp = cpu_to_le16(USB_COMP_EP0_BUFSIZ); 1738 w_length = USB_COMP_EP0_BUFSIZ; 1739 } else { 1740 goto done; 1741 } 1742 } 1743 1744 /* partial re-init of the response message; the function or the 1745 * gadget might need to intercept e.g. a control-OUT completion 1746 * when we delegate to it. 1747 */ 1748 req->zero = 0; 1749 req->context = cdev; 1750 req->complete = composite_setup_complete; 1751 req->length = 0; 1752 gadget->ep0->driver_data = cdev; 1753 1754 /* 1755 * Don't let non-standard requests match any of the cases below 1756 * by accident. 1757 */ 1758 if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD) 1759 goto unknown; 1760 1761 switch (ctrl->bRequest) { 1762 1763 /* we handle all standard USB descriptors */ 1764 case USB_REQ_GET_DESCRIPTOR: 1765 if (ctrl->bRequestType != USB_DIR_IN) 1766 goto unknown; 1767 switch (w_value >> 8) { 1768 1769 case USB_DT_DEVICE: 1770 cdev->desc.bNumConfigurations = 1771 count_configs(cdev, USB_DT_DEVICE); 1772 cdev->desc.bMaxPacketSize0 = 1773 cdev->gadget->ep0->maxpacket; 1774 if (gadget_is_superspeed(gadget)) { 1775 if (gadget->speed >= USB_SPEED_SUPER) { 1776 cdev->desc.bcdUSB = cpu_to_le16(0x0320); 1777 cdev->desc.bMaxPacketSize0 = 9; 1778 } else { 1779 cdev->desc.bcdUSB = cpu_to_le16(0x0210); 1780 } 1781 } else { 1782 if (gadget->lpm_capable || cdev->use_webusb) 1783 cdev->desc.bcdUSB = cpu_to_le16(0x0201); 1784 else 1785 cdev->desc.bcdUSB = cpu_to_le16(0x0200); 1786 } 1787 1788 value = min(w_length, (u16) sizeof cdev->desc); 1789 memcpy(req->buf, &cdev->desc, value); 1790 break; 1791 case USB_DT_DEVICE_QUALIFIER: 1792 if (!gadget_is_dualspeed(gadget) || 1793 gadget->speed >= USB_SPEED_SUPER) 1794 break; 1795 device_qual(cdev); 1796 value = min_t(int, w_length, 1797 sizeof(struct usb_qualifier_descriptor)); 1798 break; 1799 case USB_DT_OTHER_SPEED_CONFIG: 1800 if (!gadget_is_dualspeed(gadget) || 1801 gadget->speed >= USB_SPEED_SUPER) 1802 break; 1803 fallthrough; 1804 case USB_DT_CONFIG: 1805 value = config_desc(cdev, w_value); 1806 if (value >= 0) 1807 value = min(w_length, (u16) value); 1808 break; 1809 case USB_DT_STRING: 1810 value = get_string(cdev, req->buf, 1811 w_index, w_value & 0xff); 1812 if (value >= 0) 1813 value = min(w_length, (u16) value); 1814 break; 1815 case USB_DT_BOS: 1816 if (gadget_is_superspeed(gadget) || 1817 gadget->lpm_capable || cdev->use_webusb) { 1818 value = bos_desc(cdev); 1819 value = min(w_length, (u16) value); 1820 } 1821 break; 1822 case USB_DT_OTG: 1823 if (gadget_is_otg(gadget)) { 1824 struct usb_configuration *config; 1825 int otg_desc_len = 0; 1826 1827 if (cdev->config) 1828 config = cdev->config; 1829 else 1830 config = list_first_entry( 1831 &cdev->configs, 1832 struct usb_configuration, list); 1833 if (!config) 1834 goto done; 1835 1836 if (gadget->otg_caps && 1837 (gadget->otg_caps->otg_rev >= 0x0200)) 1838 otg_desc_len += sizeof( 1839 struct usb_otg20_descriptor); 1840 else 1841 otg_desc_len += sizeof( 1842 struct usb_otg_descriptor); 1843 1844 value = min_t(int, w_length, otg_desc_len); 1845 memcpy(req->buf, config->descriptors[0], value); 1846 } 1847 break; 1848 } 1849 break; 1850 1851 /* any number of configs can work */ 1852 case USB_REQ_SET_CONFIGURATION: 1853 if (ctrl->bRequestType != 0) 1854 goto unknown; 1855 if (gadget_is_otg(gadget)) { 1856 if (gadget->a_hnp_support) 1857 DBG(cdev, "HNP available\n"); 1858 else if (gadget->a_alt_hnp_support) 1859 DBG(cdev, "HNP on another port\n"); 1860 else 1861 VDBG(cdev, "HNP inactive\n"); 1862 } 1863 spin_lock(&cdev->lock); 1864 value = set_config(cdev, ctrl, w_value); 1865 spin_unlock(&cdev->lock); 1866 break; 1867 case USB_REQ_GET_CONFIGURATION: 1868 if (ctrl->bRequestType != USB_DIR_IN) 1869 goto unknown; 1870 if (cdev->config) 1871 *(u8 *)req->buf = cdev->config->bConfigurationValue; 1872 else 1873 *(u8 *)req->buf = 0; 1874 value = min(w_length, (u16) 1); 1875 break; 1876 1877 /* function drivers must handle get/set altsetting */ 1878 case USB_REQ_SET_INTERFACE: 1879 if (ctrl->bRequestType != USB_RECIP_INTERFACE) 1880 goto unknown; 1881 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 1882 break; 1883 f = cdev->config->interface[intf]; 1884 if (!f) 1885 break; 1886 1887 /* 1888 * If there's no get_alt() method, we know only altsetting zero 1889 * works. There is no need to check if set_alt() is not NULL 1890 * as we check this in usb_add_function(). 1891 */ 1892 if (w_value && !f->get_alt) 1893 break; 1894 1895 spin_lock(&cdev->lock); 1896 value = f->set_alt(f, w_index, w_value); 1897 if (value == USB_GADGET_DELAYED_STATUS) { 1898 DBG(cdev, 1899 "%s: interface %d (%s) requested delayed status\n", 1900 __func__, intf, f->name); 1901 cdev->delayed_status++; 1902 DBG(cdev, "delayed_status count %d\n", 1903 cdev->delayed_status); 1904 } 1905 spin_unlock(&cdev->lock); 1906 break; 1907 case USB_REQ_GET_INTERFACE: 1908 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) 1909 goto unknown; 1910 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 1911 break; 1912 f = cdev->config->interface[intf]; 1913 if (!f) 1914 break; 1915 /* lots of interfaces only need altsetting zero... */ 1916 value = f->get_alt ? f->get_alt(f, w_index) : 0; 1917 if (value < 0) 1918 break; 1919 *((u8 *)req->buf) = value; 1920 value = min(w_length, (u16) 1); 1921 break; 1922 case USB_REQ_GET_STATUS: 1923 if (gadget_is_otg(gadget) && gadget->hnp_polling_support && 1924 (w_index == OTG_STS_SELECTOR)) { 1925 if (ctrl->bRequestType != (USB_DIR_IN | 1926 USB_RECIP_DEVICE)) 1927 goto unknown; 1928 *((u8 *)req->buf) = gadget->host_request_flag; 1929 value = 1; 1930 break; 1931 } 1932 1933 /* 1934 * USB 3.0 additions: 1935 * Function driver should handle get_status request. If such cb 1936 * wasn't supplied we respond with default value = 0 1937 * Note: function driver should supply such cb only for the 1938 * first interface of the function 1939 */ 1940 if (!gadget_is_superspeed(gadget)) 1941 goto unknown; 1942 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE)) 1943 goto unknown; 1944 value = 2; /* This is the length of the get_status reply */ 1945 put_unaligned_le16(0, req->buf); 1946 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 1947 break; 1948 f = cdev->config->interface[intf]; 1949 if (!f) 1950 break; 1951 status = f->get_status ? f->get_status(f) : 0; 1952 if (status < 0) 1953 break; 1954 put_unaligned_le16(status & 0x0000ffff, req->buf); 1955 break; 1956 /* 1957 * Function drivers should handle SetFeature/ClearFeature 1958 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied 1959 * only for the first interface of the function 1960 */ 1961 case USB_REQ_CLEAR_FEATURE: 1962 case USB_REQ_SET_FEATURE: 1963 if (!gadget_is_superspeed(gadget)) 1964 goto unknown; 1965 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE)) 1966 goto unknown; 1967 switch (w_value) { 1968 case USB_INTRF_FUNC_SUSPEND: 1969 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 1970 break; 1971 f = cdev->config->interface[intf]; 1972 if (!f) 1973 break; 1974 value = 0; 1975 if (f->func_suspend) 1976 value = f->func_suspend(f, w_index >> 8); 1977 if (value < 0) { 1978 ERROR(cdev, 1979 "func_suspend() returned error %d\n", 1980 value); 1981 value = 0; 1982 } 1983 break; 1984 } 1985 break; 1986 default: 1987 unknown: 1988 /* 1989 * OS descriptors handling 1990 */ 1991 if (cdev->use_os_string && cdev->os_desc_config && 1992 (ctrl->bRequestType & USB_TYPE_VENDOR) && 1993 ctrl->bRequest == cdev->b_vendor_code) { 1994 struct usb_configuration *os_desc_cfg; 1995 u8 *buf; 1996 int interface; 1997 int count = 0; 1998 1999 req = cdev->os_desc_req; 2000 req->context = cdev; 2001 req->complete = composite_setup_complete; 2002 buf = req->buf; 2003 os_desc_cfg = cdev->os_desc_config; 2004 w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ); 2005 memset(buf, 0, w_length); 2006 buf[5] = 0x01; 2007 switch (ctrl->bRequestType & USB_RECIP_MASK) { 2008 case USB_RECIP_DEVICE: 2009 if (w_index != 0x4 || (w_value >> 8)) 2010 break; 2011 buf[6] = w_index; 2012 /* Number of ext compat interfaces */ 2013 count = count_ext_compat(os_desc_cfg); 2014 buf[8] = count; 2015 count *= 24; /* 24 B/ext compat desc */ 2016 count += 16; /* header */ 2017 put_unaligned_le32(count, buf); 2018 value = w_length; 2019 if (w_length > 0x10) { 2020 value = fill_ext_compat(os_desc_cfg, buf); 2021 value = min_t(u16, w_length, value); 2022 } 2023 break; 2024 case USB_RECIP_INTERFACE: 2025 if (w_index != 0x5 || (w_value >> 8)) 2026 break; 2027 interface = w_value & 0xFF; 2028 if (interface >= MAX_CONFIG_INTERFACES || 2029 !os_desc_cfg->interface[interface]) 2030 break; 2031 buf[6] = w_index; 2032 count = count_ext_prop(os_desc_cfg, 2033 interface); 2034 put_unaligned_le16(count, buf + 8); 2035 count = len_ext_prop(os_desc_cfg, 2036 interface); 2037 put_unaligned_le32(count, buf); 2038 value = w_length; 2039 if (w_length > 0x0A) { 2040 value = fill_ext_prop(os_desc_cfg, 2041 interface, buf); 2042 if (value >= 0) 2043 value = min_t(u16, w_length, value); 2044 } 2045 break; 2046 } 2047 2048 goto check_value; 2049 } 2050 2051 /* 2052 * WebUSB URL descriptor handling, following: 2053 * https://wicg.github.io/webusb/#device-requests 2054 */ 2055 if (cdev->use_webusb && 2056 ctrl->bRequestType == (USB_DIR_IN | USB_TYPE_VENDOR) && 2057 w_index == WEBUSB_GET_URL && 2058 w_value == WEBUSB_LANDING_PAGE_PRESENT && 2059 ctrl->bRequest == cdev->b_webusb_vendor_code) { 2060 unsigned int landing_page_length; 2061 unsigned int landing_page_offset; 2062 struct webusb_url_descriptor *url_descriptor = 2063 (struct webusb_url_descriptor *)cdev->req->buf; 2064 2065 url_descriptor->bDescriptorType = WEBUSB_URL_DESCRIPTOR_TYPE; 2066 2067 if (strncasecmp(cdev->landing_page, "https://", 8) == 0) { 2068 landing_page_offset = 8; 2069 url_descriptor->bScheme = WEBUSB_URL_SCHEME_HTTPS; 2070 } else if (strncasecmp(cdev->landing_page, "http://", 7) == 0) { 2071 landing_page_offset = 7; 2072 url_descriptor->bScheme = WEBUSB_URL_SCHEME_HTTP; 2073 } else { 2074 landing_page_offset = 0; 2075 url_descriptor->bScheme = WEBUSB_URL_SCHEME_NONE; 2076 } 2077 2078 landing_page_length = strnlen(cdev->landing_page, 2079 sizeof(url_descriptor->URL) 2080 - WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + landing_page_offset); 2081 2082 if (ctrl->wLength < WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH 2083 + landing_page_length) 2084 landing_page_length = ctrl->wLength 2085 - WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH + landing_page_offset; 2086 2087 memcpy(url_descriptor->URL, 2088 cdev->landing_page + landing_page_offset, 2089 landing_page_length - landing_page_offset); 2090 url_descriptor->bLength = landing_page_length 2091 - landing_page_offset + WEBUSB_URL_DESCRIPTOR_HEADER_LENGTH; 2092 2093 value = url_descriptor->bLength; 2094 2095 goto check_value; 2096 } 2097 2098 VDBG(cdev, 2099 "non-core control req%02x.%02x v%04x i%04x l%d\n", 2100 ctrl->bRequestType, ctrl->bRequest, 2101 w_value, w_index, w_length); 2102 2103 /* functions always handle their interfaces and endpoints... 2104 * punt other recipients (other, WUSB, ...) to the current 2105 * configuration code. 2106 */ 2107 if (cdev->config) { 2108 list_for_each_entry(f, &cdev->config->functions, list) 2109 if (f->req_match && 2110 f->req_match(f, ctrl, false)) 2111 goto try_fun_setup; 2112 } else { 2113 struct usb_configuration *c; 2114 list_for_each_entry(c, &cdev->configs, list) 2115 list_for_each_entry(f, &c->functions, list) 2116 if (f->req_match && 2117 f->req_match(f, ctrl, true)) 2118 goto try_fun_setup; 2119 } 2120 f = NULL; 2121 2122 switch (ctrl->bRequestType & USB_RECIP_MASK) { 2123 case USB_RECIP_INTERFACE: 2124 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES) 2125 break; 2126 f = cdev->config->interface[intf]; 2127 break; 2128 2129 case USB_RECIP_ENDPOINT: 2130 if (!cdev->config) 2131 break; 2132 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); 2133 list_for_each_entry(iter, &cdev->config->functions, list) { 2134 if (test_bit(endp, iter->endpoints)) { 2135 f = iter; 2136 break; 2137 } 2138 } 2139 break; 2140 } 2141 try_fun_setup: 2142 if (f && f->setup) 2143 value = f->setup(f, ctrl); 2144 else { 2145 struct usb_configuration *c; 2146 2147 c = cdev->config; 2148 if (!c) 2149 goto done; 2150 2151 /* try current config's setup */ 2152 if (c->setup) { 2153 value = c->setup(c, ctrl); 2154 goto done; 2155 } 2156 2157 /* try the only function in the current config */ 2158 if (!list_is_singular(&c->functions)) 2159 goto done; 2160 f = list_first_entry(&c->functions, struct usb_function, 2161 list); 2162 if (f->setup) 2163 value = f->setup(f, ctrl); 2164 } 2165 2166 goto done; 2167 } 2168 2169 check_value: 2170 /* respond with data transfer before status phase? */ 2171 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) { 2172 req->length = value; 2173 req->context = cdev; 2174 req->zero = value < w_length; 2175 value = composite_ep0_queue(cdev, req, GFP_ATOMIC); 2176 if (value < 0) { 2177 DBG(cdev, "ep_queue --> %d\n", value); 2178 req->status = 0; 2179 composite_setup_complete(gadget->ep0, req); 2180 } 2181 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) { 2182 WARN(cdev, 2183 "%s: Delayed status not supported for w_length != 0", 2184 __func__); 2185 } 2186 2187 done: 2188 /* device either stalls (value < 0) or reports success */ 2189 return value; 2190 } 2191 2192 static void __composite_disconnect(struct usb_gadget *gadget) 2193 { 2194 struct usb_composite_dev *cdev = get_gadget_data(gadget); 2195 unsigned long flags; 2196 2197 /* REVISIT: should we have config and device level 2198 * disconnect callbacks? 2199 */ 2200 spin_lock_irqsave(&cdev->lock, flags); 2201 cdev->suspended = 0; 2202 if (cdev->config) 2203 reset_config(cdev); 2204 if (cdev->driver->disconnect) 2205 cdev->driver->disconnect(cdev); 2206 spin_unlock_irqrestore(&cdev->lock, flags); 2207 } 2208 2209 void composite_disconnect(struct usb_gadget *gadget) 2210 { 2211 usb_gadget_vbus_draw(gadget, 0); 2212 __composite_disconnect(gadget); 2213 } 2214 2215 void composite_reset(struct usb_gadget *gadget) 2216 { 2217 /* 2218 * Section 1.4.13 Standard Downstream Port of the USB battery charging 2219 * specification v1.2 states that a device connected on a SDP shall only 2220 * draw at max 100mA while in a connected, but unconfigured state. 2221 */ 2222 usb_gadget_vbus_draw(gadget, 100); 2223 __composite_disconnect(gadget); 2224 } 2225 2226 /*-------------------------------------------------------------------------*/ 2227 2228 static ssize_t suspended_show(struct device *dev, struct device_attribute *attr, 2229 char *buf) 2230 { 2231 struct usb_gadget *gadget = dev_to_usb_gadget(dev); 2232 struct usb_composite_dev *cdev = get_gadget_data(gadget); 2233 2234 return sprintf(buf, "%d\n", cdev->suspended); 2235 } 2236 static DEVICE_ATTR_RO(suspended); 2237 2238 static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver) 2239 { 2240 struct usb_composite_dev *cdev = get_gadget_data(gadget); 2241 struct usb_gadget_strings *gstr = cdev->driver->strings[0]; 2242 struct usb_string *dev_str = gstr->strings; 2243 2244 /* composite_disconnect() must already have been called 2245 * by the underlying peripheral controller driver! 2246 * so there's no i/o concurrency that could affect the 2247 * state protected by cdev->lock. 2248 */ 2249 WARN_ON(cdev->config); 2250 2251 while (!list_empty(&cdev->configs)) { 2252 struct usb_configuration *c; 2253 c = list_first_entry(&cdev->configs, 2254 struct usb_configuration, list); 2255 remove_config(cdev, c); 2256 } 2257 if (cdev->driver->unbind && unbind_driver) 2258 cdev->driver->unbind(cdev); 2259 2260 composite_dev_cleanup(cdev); 2261 2262 if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer) 2263 dev_str[USB_GADGET_MANUFACTURER_IDX].s = ""; 2264 2265 kfree(cdev->def_manufacturer); 2266 kfree(cdev); 2267 set_gadget_data(gadget, NULL); 2268 } 2269 2270 static void composite_unbind(struct usb_gadget *gadget) 2271 { 2272 __composite_unbind(gadget, true); 2273 } 2274 2275 static void update_unchanged_dev_desc(struct usb_device_descriptor *new, 2276 const struct usb_device_descriptor *old) 2277 { 2278 __le16 idVendor; 2279 __le16 idProduct; 2280 __le16 bcdDevice; 2281 u8 iSerialNumber; 2282 u8 iManufacturer; 2283 u8 iProduct; 2284 2285 /* 2286 * these variables may have been set in 2287 * usb_composite_overwrite_options() 2288 */ 2289 idVendor = new->idVendor; 2290 idProduct = new->idProduct; 2291 bcdDevice = new->bcdDevice; 2292 iSerialNumber = new->iSerialNumber; 2293 iManufacturer = new->iManufacturer; 2294 iProduct = new->iProduct; 2295 2296 *new = *old; 2297 if (idVendor) 2298 new->idVendor = idVendor; 2299 if (idProduct) 2300 new->idProduct = idProduct; 2301 if (bcdDevice) 2302 new->bcdDevice = bcdDevice; 2303 else 2304 new->bcdDevice = cpu_to_le16(get_default_bcdDevice()); 2305 if (iSerialNumber) 2306 new->iSerialNumber = iSerialNumber; 2307 if (iManufacturer) 2308 new->iManufacturer = iManufacturer; 2309 if (iProduct) 2310 new->iProduct = iProduct; 2311 } 2312 2313 int composite_dev_prepare(struct usb_composite_driver *composite, 2314 struct usb_composite_dev *cdev) 2315 { 2316 struct usb_gadget *gadget = cdev->gadget; 2317 int ret = -ENOMEM; 2318 2319 /* preallocate control response and buffer */ 2320 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 2321 if (!cdev->req) 2322 return -ENOMEM; 2323 2324 cdev->req->buf = kzalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL); 2325 if (!cdev->req->buf) 2326 goto fail; 2327 2328 ret = device_create_file(&gadget->dev, &dev_attr_suspended); 2329 if (ret) 2330 goto fail_dev; 2331 2332 cdev->req->complete = composite_setup_complete; 2333 cdev->req->context = cdev; 2334 gadget->ep0->driver_data = cdev; 2335 2336 cdev->driver = composite; 2337 2338 /* 2339 * As per USB compliance update, a device that is actively drawing 2340 * more than 100mA from USB must report itself as bus-powered in 2341 * the GetStatus(DEVICE) call. 2342 */ 2343 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW) 2344 usb_gadget_set_selfpowered(gadget); 2345 2346 /* interface and string IDs start at zero via kzalloc. 2347 * we force endpoints to start unassigned; few controller 2348 * drivers will zero ep->driver_data. 2349 */ 2350 usb_ep_autoconfig_reset(gadget); 2351 return 0; 2352 fail_dev: 2353 kfree(cdev->req->buf); 2354 fail: 2355 usb_ep_free_request(gadget->ep0, cdev->req); 2356 cdev->req = NULL; 2357 return ret; 2358 } 2359 2360 int composite_os_desc_req_prepare(struct usb_composite_dev *cdev, 2361 struct usb_ep *ep0) 2362 { 2363 int ret = 0; 2364 2365 cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL); 2366 if (!cdev->os_desc_req) { 2367 ret = -ENOMEM; 2368 goto end; 2369 } 2370 2371 cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ, 2372 GFP_KERNEL); 2373 if (!cdev->os_desc_req->buf) { 2374 ret = -ENOMEM; 2375 usb_ep_free_request(ep0, cdev->os_desc_req); 2376 goto end; 2377 } 2378 cdev->os_desc_req->context = cdev; 2379 cdev->os_desc_req->complete = composite_setup_complete; 2380 end: 2381 return ret; 2382 } 2383 2384 void composite_dev_cleanup(struct usb_composite_dev *cdev) 2385 { 2386 struct usb_gadget_string_container *uc, *tmp; 2387 struct usb_ep *ep, *tmp_ep; 2388 2389 list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) { 2390 list_del(&uc->list); 2391 kfree(uc); 2392 } 2393 if (cdev->os_desc_req) { 2394 if (cdev->os_desc_pending) 2395 usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req); 2396 2397 kfree(cdev->os_desc_req->buf); 2398 cdev->os_desc_req->buf = NULL; 2399 usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req); 2400 cdev->os_desc_req = NULL; 2401 } 2402 if (cdev->req) { 2403 if (cdev->setup_pending) 2404 usb_ep_dequeue(cdev->gadget->ep0, cdev->req); 2405 2406 kfree(cdev->req->buf); 2407 cdev->req->buf = NULL; 2408 usb_ep_free_request(cdev->gadget->ep0, cdev->req); 2409 cdev->req = NULL; 2410 } 2411 cdev->next_string_id = 0; 2412 device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); 2413 2414 /* 2415 * Some UDC backends have a dynamic EP allocation scheme. 2416 * 2417 * In that case, the dispose() callback is used to notify the 2418 * backend that the EPs are no longer in use. 2419 * 2420 * Note: The UDC backend can remove the EP from the ep_list as 2421 * a result, so we need to use the _safe list iterator. 2422 */ 2423 list_for_each_entry_safe(ep, tmp_ep, 2424 &cdev->gadget->ep_list, ep_list) { 2425 if (ep->ops->dispose) 2426 ep->ops->dispose(ep); 2427 } 2428 } 2429 2430 static int composite_bind(struct usb_gadget *gadget, 2431 struct usb_gadget_driver *gdriver) 2432 { 2433 struct usb_composite_dev *cdev; 2434 struct usb_composite_driver *composite = to_cdriver(gdriver); 2435 int status = -ENOMEM; 2436 2437 cdev = kzalloc(sizeof *cdev, GFP_KERNEL); 2438 if (!cdev) 2439 return status; 2440 2441 spin_lock_init(&cdev->lock); 2442 cdev->gadget = gadget; 2443 set_gadget_data(gadget, cdev); 2444 INIT_LIST_HEAD(&cdev->configs); 2445 INIT_LIST_HEAD(&cdev->gstrings); 2446 2447 status = composite_dev_prepare(composite, cdev); 2448 if (status) 2449 goto fail; 2450 2451 /* composite gadget needs to assign strings for whole device (like 2452 * serial number), register function drivers, potentially update 2453 * power state and consumption, etc 2454 */ 2455 status = composite->bind(cdev); 2456 if (status < 0) 2457 goto fail; 2458 2459 if (cdev->use_os_string) { 2460 status = composite_os_desc_req_prepare(cdev, gadget->ep0); 2461 if (status) 2462 goto fail; 2463 } 2464 2465 update_unchanged_dev_desc(&cdev->desc, composite->dev); 2466 2467 /* has userspace failed to provide a serial number? */ 2468 if (composite->needs_serial && !cdev->desc.iSerialNumber) 2469 WARNING(cdev, "userspace failed to provide iSerialNumber\n"); 2470 2471 INFO(cdev, "%s ready\n", composite->name); 2472 return 0; 2473 2474 fail: 2475 __composite_unbind(gadget, false); 2476 return status; 2477 } 2478 2479 /*-------------------------------------------------------------------------*/ 2480 2481 void composite_suspend(struct usb_gadget *gadget) 2482 { 2483 struct usb_composite_dev *cdev = get_gadget_data(gadget); 2484 struct usb_function *f; 2485 2486 /* REVISIT: should we have config level 2487 * suspend/resume callbacks? 2488 */ 2489 DBG(cdev, "suspend\n"); 2490 if (cdev->config) { 2491 list_for_each_entry(f, &cdev->config->functions, list) { 2492 if (f->suspend) 2493 f->suspend(f); 2494 } 2495 } 2496 if (cdev->driver->suspend) 2497 cdev->driver->suspend(cdev); 2498 2499 cdev->suspended = 1; 2500 2501 usb_gadget_set_selfpowered(gadget); 2502 usb_gadget_vbus_draw(gadget, 2); 2503 } 2504 2505 void composite_resume(struct usb_gadget *gadget) 2506 { 2507 struct usb_composite_dev *cdev = get_gadget_data(gadget); 2508 struct usb_function *f; 2509 unsigned maxpower; 2510 2511 /* REVISIT: should we have config level 2512 * suspend/resume callbacks? 2513 */ 2514 DBG(cdev, "resume\n"); 2515 if (cdev->driver->resume) 2516 cdev->driver->resume(cdev); 2517 if (cdev->config) { 2518 list_for_each_entry(f, &cdev->config->functions, list) { 2519 if (f->resume) 2520 f->resume(f); 2521 } 2522 2523 maxpower = cdev->config->MaxPower ? 2524 cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW; 2525 if (gadget->speed < USB_SPEED_SUPER) 2526 maxpower = min(maxpower, 500U); 2527 else 2528 maxpower = min(maxpower, 900U); 2529 2530 if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW) 2531 usb_gadget_clear_selfpowered(gadget); 2532 2533 usb_gadget_vbus_draw(gadget, maxpower); 2534 } 2535 2536 cdev->suspended = 0; 2537 } 2538 2539 /*-------------------------------------------------------------------------*/ 2540 2541 static const struct usb_gadget_driver composite_driver_template = { 2542 .bind = composite_bind, 2543 .unbind = composite_unbind, 2544 2545 .setup = composite_setup, 2546 .reset = composite_reset, 2547 .disconnect = composite_disconnect, 2548 2549 .suspend = composite_suspend, 2550 .resume = composite_resume, 2551 2552 .driver = { 2553 .owner = THIS_MODULE, 2554 }, 2555 }; 2556 2557 /** 2558 * usb_composite_probe() - register a composite driver 2559 * @driver: the driver to register 2560 * 2561 * Context: single threaded during gadget setup 2562 * 2563 * This function is used to register drivers using the composite driver 2564 * framework. The return value is zero, or a negative errno value. 2565 * Those values normally come from the driver's @bind method, which does 2566 * all the work of setting up the driver to match the hardware. 2567 * 2568 * On successful return, the gadget is ready to respond to requests from 2569 * the host, unless one of its components invokes usb_gadget_disconnect() 2570 * while it was binding. That would usually be done in order to wait for 2571 * some userspace participation. 2572 */ 2573 int usb_composite_probe(struct usb_composite_driver *driver) 2574 { 2575 struct usb_gadget_driver *gadget_driver; 2576 2577 if (!driver || !driver->dev || !driver->bind) 2578 return -EINVAL; 2579 2580 if (!driver->name) 2581 driver->name = "composite"; 2582 2583 driver->gadget_driver = composite_driver_template; 2584 gadget_driver = &driver->gadget_driver; 2585 2586 gadget_driver->function = (char *) driver->name; 2587 gadget_driver->driver.name = driver->name; 2588 gadget_driver->max_speed = driver->max_speed; 2589 2590 return usb_gadget_register_driver(gadget_driver); 2591 } 2592 EXPORT_SYMBOL_GPL(usb_composite_probe); 2593 2594 /** 2595 * usb_composite_unregister() - unregister a composite driver 2596 * @driver: the driver to unregister 2597 * 2598 * This function is used to unregister drivers using the composite 2599 * driver framework. 2600 */ 2601 void usb_composite_unregister(struct usb_composite_driver *driver) 2602 { 2603 usb_gadget_unregister_driver(&driver->gadget_driver); 2604 } 2605 EXPORT_SYMBOL_GPL(usb_composite_unregister); 2606 2607 /** 2608 * usb_composite_setup_continue() - Continue with the control transfer 2609 * @cdev: the composite device who's control transfer was kept waiting 2610 * 2611 * This function must be called by the USB function driver to continue 2612 * with the control transfer's data/status stage in case it had requested to 2613 * delay the data/status stages. A USB function's setup handler (e.g. set_alt()) 2614 * can request the composite framework to delay the setup request's data/status 2615 * stages by returning USB_GADGET_DELAYED_STATUS. 2616 */ 2617 void usb_composite_setup_continue(struct usb_composite_dev *cdev) 2618 { 2619 int value; 2620 struct usb_request *req = cdev->req; 2621 unsigned long flags; 2622 2623 DBG(cdev, "%s\n", __func__); 2624 spin_lock_irqsave(&cdev->lock, flags); 2625 2626 if (cdev->delayed_status == 0) { 2627 WARN(cdev, "%s: Unexpected call\n", __func__); 2628 2629 } else if (--cdev->delayed_status == 0) { 2630 DBG(cdev, "%s: Completing delayed status\n", __func__); 2631 req->length = 0; 2632 req->context = cdev; 2633 value = composite_ep0_queue(cdev, req, GFP_ATOMIC); 2634 if (value < 0) { 2635 DBG(cdev, "ep_queue --> %d\n", value); 2636 req->status = 0; 2637 composite_setup_complete(cdev->gadget->ep0, req); 2638 } 2639 } 2640 2641 spin_unlock_irqrestore(&cdev->lock, flags); 2642 } 2643 EXPORT_SYMBOL_GPL(usb_composite_setup_continue); 2644 2645 static char *composite_default_mfr(struct usb_gadget *gadget) 2646 { 2647 return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname, 2648 init_utsname()->release, gadget->name); 2649 } 2650 2651 void usb_composite_overwrite_options(struct usb_composite_dev *cdev, 2652 struct usb_composite_overwrite *covr) 2653 { 2654 struct usb_device_descriptor *desc = &cdev->desc; 2655 struct usb_gadget_strings *gstr = cdev->driver->strings[0]; 2656 struct usb_string *dev_str = gstr->strings; 2657 2658 if (covr->idVendor) 2659 desc->idVendor = cpu_to_le16(covr->idVendor); 2660 2661 if (covr->idProduct) 2662 desc->idProduct = cpu_to_le16(covr->idProduct); 2663 2664 if (covr->bcdDevice) 2665 desc->bcdDevice = cpu_to_le16(covr->bcdDevice); 2666 2667 if (covr->serial_number) { 2668 desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id; 2669 dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number; 2670 } 2671 if (covr->manufacturer) { 2672 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; 2673 dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer; 2674 2675 } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) { 2676 desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id; 2677 cdev->def_manufacturer = composite_default_mfr(cdev->gadget); 2678 dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer; 2679 } 2680 2681 if (covr->product) { 2682 desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id; 2683 dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product; 2684 } 2685 } 2686 EXPORT_SYMBOL_GPL(usb_composite_overwrite_options); 2687 2688 MODULE_LICENSE("GPL"); 2689 MODULE_AUTHOR("David Brownell"); 2690