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