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