1 /* 2 * composite.c - infrastructure for Composite USB Gadgets 3 * 4 * Copyright (C) 2006-2008 David Brownell 5 * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com> 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 #undef DEBUG 10 11 #include <linux/bitops.h> 12 #include <linux/usb/composite.h> 13 14 #define USB_BUFSIZ 4096 15 16 static struct usb_composite_driver *composite; 17 18 /** 19 * usb_add_function() - add a function to a configuration 20 * @config: the configuration 21 * @function: the function being added 22 * Context: single threaded during gadget setup 23 * 24 * After initialization, each configuration must have one or more 25 * functions added to it. Adding a function involves calling its @bind() 26 * method to allocate resources such as interface and string identifiers 27 * and endpoints. 28 * 29 * This function returns the value of the function's bind(), which is 30 * zero for success else a negative errno value. 31 */ 32 int usb_add_function(struct usb_configuration *config, 33 struct usb_function *function) 34 { 35 int value = -EINVAL; 36 37 debug("adding '%s'/%p to config '%s'/%p\n", 38 function->name, function, 39 config->label, config); 40 41 if (!function->set_alt || !function->disable) 42 goto done; 43 44 function->config = config; 45 list_add_tail(&function->list, &config->functions); 46 47 if (function->bind) { 48 value = function->bind(config, function); 49 if (value < 0) { 50 list_del(&function->list); 51 function->config = NULL; 52 } 53 } else 54 value = 0; 55 56 if (!config->fullspeed && function->descriptors) 57 config->fullspeed = 1; 58 if (!config->highspeed && function->hs_descriptors) 59 config->highspeed = 1; 60 61 done: 62 if (value) 63 debug("adding '%s'/%p --> %d\n", 64 function->name, function, value); 65 return value; 66 } 67 68 /** 69 * usb_function_deactivate - prevent function and gadget enumeration 70 * @function: the function that isn't yet ready to respond 71 * 72 * Blocks response of the gadget driver to host enumeration by 73 * preventing the data line pullup from being activated. This is 74 * normally called during @bind() processing to change from the 75 * initial "ready to respond" state, or when a required resource 76 * becomes available. 77 * 78 * For example, drivers that serve as a passthrough to a userspace 79 * daemon can block enumeration unless that daemon (such as an OBEX, 80 * MTP, or print server) is ready to handle host requests. 81 * 82 * Not all systems support software control of their USB peripheral 83 * data pullups. 84 * 85 * Returns zero on success, else negative errno. 86 */ 87 int usb_function_deactivate(struct usb_function *function) 88 { 89 struct usb_composite_dev *cdev = function->config->cdev; 90 int status = 0; 91 92 if (cdev->deactivations == 0) 93 status = usb_gadget_disconnect(cdev->gadget); 94 if (status == 0) 95 cdev->deactivations++; 96 97 return status; 98 } 99 100 /** 101 * usb_function_activate - allow function and gadget enumeration 102 * @function: function on which usb_function_activate() was called 103 * 104 * Reverses effect of usb_function_deactivate(). If no more functions 105 * are delaying their activation, the gadget driver will respond to 106 * host enumeration procedures. 107 * 108 * Returns zero on success, else negative errno. 109 */ 110 int usb_function_activate(struct usb_function *function) 111 { 112 struct usb_composite_dev *cdev = function->config->cdev; 113 int status = 0; 114 115 if (cdev->deactivations == 0) 116 status = -EINVAL; 117 else { 118 cdev->deactivations--; 119 if (cdev->deactivations == 0) 120 status = usb_gadget_connect(cdev->gadget); 121 } 122 123 return status; 124 } 125 126 /** 127 * usb_interface_id() - allocate an unused interface ID 128 * @config: configuration associated with the interface 129 * @function: function handling the interface 130 * Context: single threaded during gadget setup 131 * 132 * usb_interface_id() is called from usb_function.bind() callbacks to 133 * allocate new interface IDs. The function driver will then store that 134 * ID in interface, association, CDC union, and other descriptors. It 135 * will also handle any control requests targetted at that interface, 136 * particularly changing its altsetting via set_alt(). There may 137 * also be class-specific or vendor-specific requests to handle. 138 * 139 * All interface identifier should be allocated using this routine, to 140 * ensure that for example different functions don't wrongly assign 141 * different meanings to the same identifier. Note that since interface 142 * identifers are configuration-specific, functions used in more than 143 * one configuration (or more than once in a given configuration) need 144 * multiple versions of the relevant descriptors. 145 * 146 * Returns the interface ID which was allocated; or -ENODEV if no 147 * more interface IDs can be allocated. 148 */ 149 int usb_interface_id(struct usb_configuration *config, 150 struct usb_function *function) 151 { 152 unsigned char id = config->next_interface_id; 153 154 if (id < MAX_CONFIG_INTERFACES) { 155 config->interface[id] = function; 156 config->next_interface_id = id + 1; 157 return id; 158 } 159 return -ENODEV; 160 } 161 162 static int config_buf(struct usb_configuration *config, 163 enum usb_device_speed speed, void *buf, u8 type) 164 { 165 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE; 166 void *next = buf + USB_DT_CONFIG_SIZE; 167 struct usb_descriptor_header **descriptors; 168 struct usb_config_descriptor *c = buf; 169 int status; 170 struct usb_function *f; 171 172 /* write the config descriptor */ 173 c = buf; 174 c->bLength = USB_DT_CONFIG_SIZE; 175 c->bDescriptorType = type; 176 177 c->bNumInterfaces = config->next_interface_id; 178 c->bConfigurationValue = config->bConfigurationValue; 179 c->iConfiguration = config->iConfiguration; 180 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes; 181 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2); 182 183 /* There may be e.g. OTG descriptors */ 184 if (config->descriptors) { 185 status = usb_descriptor_fillbuf(next, len, 186 config->descriptors); 187 if (status < 0) 188 return status; 189 len -= status; 190 next += status; 191 } 192 193 /* add each function's descriptors */ 194 list_for_each_entry(f, &config->functions, list) { 195 if (speed == USB_SPEED_HIGH) 196 descriptors = f->hs_descriptors; 197 else 198 descriptors = f->descriptors; 199 if (!descriptors) 200 continue; 201 status = usb_descriptor_fillbuf(next, len, 202 (const struct usb_descriptor_header **) descriptors); 203 if (status < 0) 204 return status; 205 len -= status; 206 next += status; 207 } 208 209 len = next - buf; 210 c->wTotalLength = cpu_to_le16(len); 211 return len; 212 } 213 214 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value) 215 { 216 enum usb_device_speed speed = USB_SPEED_UNKNOWN; 217 struct usb_gadget *gadget = cdev->gadget; 218 u8 type = w_value >> 8; 219 int hs = 0; 220 struct usb_configuration *c; 221 222 if (gadget_is_dualspeed(gadget)) { 223 if (gadget->speed == USB_SPEED_HIGH) 224 hs = 1; 225 if (type == USB_DT_OTHER_SPEED_CONFIG) 226 hs = !hs; 227 if (hs) 228 speed = USB_SPEED_HIGH; 229 } 230 231 w_value &= 0xff; 232 list_for_each_entry(c, &cdev->configs, list) { 233 if (speed == USB_SPEED_HIGH) { 234 if (!c->highspeed) 235 continue; 236 } else { 237 if (!c->fullspeed) 238 continue; 239 } 240 if (w_value == 0) 241 return config_buf(c, speed, cdev->req->buf, type); 242 w_value--; 243 } 244 return -EINVAL; 245 } 246 247 static int count_configs(struct usb_composite_dev *cdev, unsigned type) 248 { 249 struct usb_gadget *gadget = cdev->gadget; 250 unsigned count = 0; 251 int hs = 0; 252 struct usb_configuration *c; 253 254 if (gadget_is_dualspeed(gadget)) { 255 if (gadget->speed == USB_SPEED_HIGH) 256 hs = 1; 257 if (type == USB_DT_DEVICE_QUALIFIER) 258 hs = !hs; 259 } 260 list_for_each_entry(c, &cdev->configs, list) { 261 /* ignore configs that won't work at this speed */ 262 if (hs) { 263 if (!c->highspeed) 264 continue; 265 } else { 266 if (!c->fullspeed) 267 continue; 268 } 269 count++; 270 } 271 return count; 272 } 273 274 static int bos_desc(struct usb_composite_dev *cdev) 275 { 276 struct usb_bos_descriptor *bos = cdev->req->buf; 277 278 bos->bLength = USB_DT_BOS_SIZE; 279 bos->bDescriptorType = USB_DT_BOS; 280 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE); 281 bos->bNumDeviceCaps = 0; 282 283 return le16_to_cpu(bos->wTotalLength); 284 } 285 286 static void device_qual(struct usb_composite_dev *cdev) 287 { 288 struct usb_qualifier_descriptor *qual = cdev->req->buf; 289 290 qual->bLength = sizeof(*qual); 291 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER; 292 /* POLICY: same bcdUSB and device type info at both speeds */ 293 qual->bcdUSB = cdev->desc.bcdUSB; 294 qual->bDeviceClass = cdev->desc.bDeviceClass; 295 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass; 296 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol; 297 /* ASSUME same EP0 fifo size at both speeds */ 298 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket; 299 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER); 300 qual->bRESERVED = 0; 301 } 302 303 static void reset_config(struct usb_composite_dev *cdev) 304 { 305 struct usb_function *f; 306 307 debug("%s:\n", __func__); 308 309 list_for_each_entry(f, &cdev->config->functions, list) { 310 if (f->disable) 311 f->disable(f); 312 313 bitmap_zero(f->endpoints, 32); 314 } 315 cdev->config = NULL; 316 } 317 318 static int set_config(struct usb_composite_dev *cdev, 319 const struct usb_ctrlrequest *ctrl, unsigned number) 320 { 321 struct usb_gadget *gadget = cdev->gadget; 322 unsigned power = gadget_is_otg(gadget) ? 8 : 100; 323 struct usb_descriptor_header **descriptors; 324 int result = -EINVAL; 325 struct usb_endpoint_descriptor *ep; 326 struct usb_configuration *c = NULL; 327 int addr; 328 int tmp; 329 struct usb_function *f; 330 331 if (cdev->config) 332 reset_config(cdev); 333 334 if (number) { 335 list_for_each_entry(c, &cdev->configs, list) { 336 if (c->bConfigurationValue == number) { 337 result = 0; 338 break; 339 } 340 } 341 if (result < 0) 342 goto done; 343 } else 344 result = 0; 345 346 debug("%s: %s speed config #%d: %s\n", __func__, 347 ({ char *speed; 348 switch (gadget->speed) { 349 case USB_SPEED_LOW: 350 speed = "low"; 351 break; 352 case USB_SPEED_FULL: 353 speed = "full"; 354 break; 355 case USB_SPEED_HIGH: 356 speed = "high"; 357 break; 358 default: 359 speed = "?"; 360 break; 361 }; 362 speed; 363 }), number, c ? c->label : "unconfigured"); 364 365 if (!c) 366 goto done; 367 368 cdev->config = c; 369 370 /* Initialize all interfaces by setting them to altsetting zero. */ 371 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) { 372 f = c->interface[tmp]; 373 if (!f) 374 break; 375 376 /* 377 * Record which endpoints are used by the function. This is used 378 * to dispatch control requests targeted at that endpoint to the 379 * function's setup callback instead of the current 380 * configuration's setup callback. 381 */ 382 if (gadget->speed == USB_SPEED_HIGH) 383 descriptors = f->hs_descriptors; 384 else 385 descriptors = f->descriptors; 386 387 for (; *descriptors; ++descriptors) { 388 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT) 389 continue; 390 391 ep = (struct usb_endpoint_descriptor *)*descriptors; 392 addr = ((ep->bEndpointAddress & 0x80) >> 3) 393 | (ep->bEndpointAddress & 0x0f); 394 __set_bit(addr, f->endpoints); 395 } 396 397 result = f->set_alt(f, tmp, 0); 398 if (result < 0) { 399 debug("interface %d (%s/%p) alt 0 --> %d\n", 400 tmp, f->name, f, result); 401 402 reset_config(cdev); 403 goto done; 404 } 405 } 406 407 /* when we return, be sure our power usage is valid */ 408 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW; 409 done: 410 usb_gadget_vbus_draw(gadget, power); 411 return result; 412 } 413 414 /** 415 * usb_add_config() - add a configuration to a device. 416 * @cdev: wraps the USB gadget 417 * @config: the configuration, with bConfigurationValue assigned 418 * Context: single threaded during gadget setup 419 * 420 * One of the main tasks of a composite driver's bind() routine is to 421 * add each of the configurations it supports, using this routine. 422 * 423 * This function returns the value of the configuration's bind(), which 424 * is zero for success else a negative errno value. Binding configurations 425 * assigns global resources including string IDs, and per-configuration 426 * resources such as interface IDs and endpoints. 427 */ 428 int usb_add_config(struct usb_composite_dev *cdev, 429 struct usb_configuration *config) 430 { 431 int status = -EINVAL; 432 struct usb_configuration *c; 433 struct usb_function *f; 434 unsigned int i; 435 436 debug("%s: adding config #%u '%s'/%p\n", __func__, 437 config->bConfigurationValue, 438 config->label, config); 439 440 if (!config->bConfigurationValue || !config->bind) 441 goto done; 442 443 /* Prevent duplicate configuration identifiers */ 444 list_for_each_entry(c, &cdev->configs, list) { 445 if (c->bConfigurationValue == config->bConfigurationValue) { 446 status = -EBUSY; 447 goto done; 448 } 449 } 450 451 config->cdev = cdev; 452 list_add_tail(&config->list, &cdev->configs); 453 454 INIT_LIST_HEAD(&config->functions); 455 config->next_interface_id = 0; 456 457 status = config->bind(config); 458 if (status < 0) { 459 list_del(&config->list); 460 config->cdev = NULL; 461 } else { 462 debug("cfg %d/%p speeds:%s%s\n", 463 config->bConfigurationValue, config, 464 config->highspeed ? " high" : "", 465 config->fullspeed 466 ? (gadget_is_dualspeed(cdev->gadget) 467 ? " full" 468 : " full/low") 469 : ""); 470 471 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) { 472 f = config->interface[i]; 473 if (!f) 474 continue; 475 debug("%s: interface %d = %s/%p\n", 476 __func__, i, f->name, f); 477 } 478 } 479 480 usb_ep_autoconfig_reset(cdev->gadget); 481 482 done: 483 if (status) 484 debug("added config '%s'/%u --> %d\n", config->label, 485 config->bConfigurationValue, status); 486 return status; 487 } 488 489 /* 490 * We support strings in multiple languages ... string descriptor zero 491 * says which languages are supported. The typical case will be that 492 * only one language (probably English) is used, with I18N handled on 493 * the host side. 494 */ 495 496 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf) 497 { 498 const struct usb_gadget_strings *s; 499 u16 language; 500 __le16 *tmp; 501 502 while (*sp) { 503 s = *sp; 504 language = cpu_to_le16(s->language); 505 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) { 506 if (*tmp == language) 507 goto repeat; 508 } 509 *tmp++ = language; 510 repeat: 511 sp++; 512 } 513 } 514 515 static int lookup_string( 516 struct usb_gadget_strings **sp, 517 void *buf, 518 u16 language, 519 int id 520 ) 521 { 522 int value; 523 struct usb_gadget_strings *s; 524 525 while (*sp) { 526 s = *sp++; 527 if (s->language != language) 528 continue; 529 value = usb_gadget_get_string(s, id, buf); 530 if (value > 0) 531 return value; 532 } 533 return -EINVAL; 534 } 535 536 static int get_string(struct usb_composite_dev *cdev, 537 void *buf, u16 language, int id) 538 { 539 struct usb_string_descriptor *s = buf; 540 struct usb_gadget_strings **sp; 541 int len; 542 struct usb_configuration *c; 543 struct usb_function *f; 544 545 /* 546 * Yes, not only is USB's I18N support probably more than most 547 * folk will ever care about ... also, it's all supported here. 548 * (Except for UTF8 support for Unicode's "Astral Planes".) 549 */ 550 551 /* 0 == report all available language codes */ 552 if (id == 0) { 553 memset(s, 0, 256); 554 s->bDescriptorType = USB_DT_STRING; 555 556 sp = composite->strings; 557 if (sp) 558 collect_langs(sp, s->wData); 559 560 list_for_each_entry(c, &cdev->configs, list) { 561 sp = c->strings; 562 if (sp) 563 collect_langs(sp, s->wData); 564 565 list_for_each_entry(f, &c->functions, list) { 566 sp = f->strings; 567 if (sp) 568 collect_langs(sp, s->wData); 569 } 570 } 571 572 for (len = 0; len <= 126 && s->wData[len]; len++) 573 continue; 574 if (!len) 575 return -EINVAL; 576 577 s->bLength = 2 * (len + 1); 578 return s->bLength; 579 } 580 581 /* 582 * Otherwise, look up and return a specified string. String IDs 583 * are device-scoped, so we look up each string table we're told 584 * about. These lookups are infrequent; simpler-is-better here. 585 */ 586 if (composite->strings) { 587 len = lookup_string(composite->strings, buf, language, id); 588 if (len > 0) 589 return len; 590 } 591 list_for_each_entry(c, &cdev->configs, list) { 592 if (c->strings) { 593 len = lookup_string(c->strings, buf, language, id); 594 if (len > 0) 595 return len; 596 } 597 list_for_each_entry(f, &c->functions, list) { 598 if (!f->strings) 599 continue; 600 len = lookup_string(f->strings, buf, language, id); 601 if (len > 0) 602 return len; 603 } 604 } 605 return -EINVAL; 606 } 607 608 /** 609 * usb_string_id() - allocate an unused string ID 610 * @cdev: the device whose string descriptor IDs are being allocated 611 * Context: single threaded during gadget setup 612 * 613 * @usb_string_id() is called from bind() callbacks to allocate 614 * string IDs. Drivers for functions, configurations, or gadgets will 615 * then store that ID in the appropriate descriptors and string table. 616 * 617 * All string identifier should be allocated using this, 618 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure 619 * that for example different functions don't wrongly assign different 620 * meanings to the same identifier. 621 */ 622 int usb_string_id(struct usb_composite_dev *cdev) 623 { 624 if (cdev->next_string_id < 254) { 625 /* 626 * string id 0 is reserved by USB spec for list of 627 * supported languages 628 * 255 reserved as well? -- mina86 629 */ 630 cdev->next_string_id++; 631 return cdev->next_string_id; 632 } 633 return -ENODEV; 634 } 635 636 /** 637 * usb_string_ids() - allocate unused string IDs in batch 638 * @cdev: the device whose string descriptor IDs are being allocated 639 * @str: an array of usb_string objects to assign numbers to 640 * Context: single threaded during gadget setup 641 * 642 * @usb_string_ids() is called from bind() callbacks to allocate 643 * string IDs. Drivers for functions, configurations, or gadgets will 644 * then copy IDs from the string table to the appropriate descriptors 645 * and string table for other languages. 646 * 647 * All string identifier should be allocated using this, 648 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for 649 * example different functions don't wrongly assign different meanings 650 * to the same identifier. 651 */ 652 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str) 653 { 654 u8 next = cdev->next_string_id; 655 656 for (; str->s; ++str) { 657 if (next >= 254) 658 return -ENODEV; 659 str->id = ++next; 660 } 661 662 cdev->next_string_id = next; 663 664 return 0; 665 } 666 667 /** 668 * usb_string_ids_n() - allocate unused string IDs in batch 669 * @c: the device whose string descriptor IDs are being allocated 670 * @n: number of string IDs to allocate 671 * Context: single threaded during gadget setup 672 * 673 * Returns the first requested ID. This ID and next @n-1 IDs are now 674 * valid IDs. At least provided that @n is non-zero because if it 675 * is, returns last requested ID which is now very useful information. 676 * 677 * @usb_string_ids_n() is called from bind() callbacks to allocate 678 * string IDs. Drivers for functions, configurations, or gadgets will 679 * then store that ID in the appropriate descriptors and string table. 680 * 681 * All string identifier should be allocated using this, 682 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for 683 * example different functions don't wrongly assign different meanings 684 * to the same identifier. 685 */ 686 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n) 687 { 688 u8 next = c->next_string_id; 689 690 if (n > 254 || next + n > 254) 691 return -ENODEV; 692 693 c->next_string_id += n; 694 return next + 1; 695 } 696 697 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req) 698 { 699 if (req->status || req->actual != req->length) 700 debug("%s: setup complete --> %d, %d/%d\n", __func__, 701 req->status, req->actual, req->length); 702 } 703 704 /* 705 * The setup() callback implements all the ep0 functionality that's 706 * not handled lower down, in hardware or the hardware driver(like 707 * device and endpoint feature flags, and their status). It's all 708 * housekeeping for the gadget function we're implementing. Most of 709 * the work is in config and function specific setup. 710 */ 711 static int 712 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl) 713 { 714 u16 w_length = le16_to_cpu(ctrl->wLength); 715 u16 w_index = le16_to_cpu(ctrl->wIndex); 716 u16 w_value = le16_to_cpu(ctrl->wValue); 717 struct usb_composite_dev *cdev = get_gadget_data(gadget); 718 u8 intf = w_index & 0xFF; 719 int value = -EOPNOTSUPP; 720 struct usb_request *req = cdev->req; 721 struct usb_function *f = NULL; 722 int standard; 723 u8 endp; 724 struct usb_configuration *c; 725 726 /* 727 * partial re-init of the response message; the function or the 728 * gadget might need to intercept e.g. a control-OUT completion 729 * when we delegate to it. 730 */ 731 req->zero = 0; 732 req->complete = composite_setup_complete; 733 req->length = USB_BUFSIZ; 734 gadget->ep0->driver_data = cdev; 735 standard = (ctrl->bRequestType & USB_TYPE_MASK) 736 == USB_TYPE_STANDARD; 737 if (!standard) 738 goto unknown; 739 740 switch (ctrl->bRequest) { 741 742 /* we handle all standard USB descriptors */ 743 case USB_REQ_GET_DESCRIPTOR: 744 if (ctrl->bRequestType != USB_DIR_IN) 745 goto unknown; 746 switch (w_value >> 8) { 747 748 case USB_DT_DEVICE: 749 cdev->desc.bNumConfigurations = 750 count_configs(cdev, USB_DT_DEVICE); 751 cdev->desc.bMaxPacketSize0 = 752 cdev->gadget->ep0->maxpacket; 753 value = min(w_length, (u16) sizeof cdev->desc); 754 memcpy(req->buf, &cdev->desc, value); 755 break; 756 case USB_DT_DEVICE_QUALIFIER: 757 if (!gadget_is_dualspeed(gadget)) 758 break; 759 device_qual(cdev); 760 value = min_t(int, w_length, 761 sizeof(struct usb_qualifier_descriptor)); 762 break; 763 case USB_DT_OTHER_SPEED_CONFIG: 764 if (!gadget_is_dualspeed(gadget)) 765 break; 766 767 case USB_DT_CONFIG: 768 value = config_desc(cdev, w_value); 769 if (value >= 0) 770 value = min(w_length, (u16) value); 771 break; 772 case USB_DT_STRING: 773 value = get_string(cdev, req->buf, 774 w_index, w_value & 0xff); 775 if (value >= 0) 776 value = min(w_length, (u16) value); 777 break; 778 case USB_DT_BOS: 779 /* HACK: only for rockusb command. 780 * Rockchip upgrade tool use bcdUSB (0x0201) field 781 * distinguishing maskrom or loader device at present. 782 * Unfortunately, it conflict with Windows 8 and beyond 783 * which request BOS descriptor in this case that bcdUSB 784 * is set to 0x0201. 785 */ 786 if (!strncmp(cdev->driver->name, "rkusb_ums_dnl", 13)) { 787 value = bos_desc(cdev); 788 value = min(w_length, (u16) value); 789 } 790 791 /* 792 * The USB compliance test (USB 2.0 Command Verifier) 793 * issues this request. We should not run into the 794 * default path here. But return for now until 795 * the superspeed support is added. 796 */ 797 break; 798 default: 799 goto unknown; 800 } 801 break; 802 803 /* any number of configs can work */ 804 case USB_REQ_SET_CONFIGURATION: 805 if (ctrl->bRequestType != 0) 806 goto unknown; 807 if (gadget_is_otg(gadget)) { 808 if (gadget->a_hnp_support) 809 debug("HNP available\n"); 810 else if (gadget->a_alt_hnp_support) 811 debug("HNP on another port\n"); 812 else 813 debug("HNP inactive\n"); 814 } 815 816 value = set_config(cdev, ctrl, w_value); 817 break; 818 case USB_REQ_GET_CONFIGURATION: 819 if (ctrl->bRequestType != USB_DIR_IN) 820 goto unknown; 821 if (cdev->config) 822 *(u8 *)req->buf = cdev->config->bConfigurationValue; 823 else 824 *(u8 *)req->buf = 0; 825 value = min(w_length, (u16) 1); 826 break; 827 828 /* 829 * function drivers must handle get/set altsetting; if there's 830 * no get() method, we know only altsetting zero works. 831 */ 832 case USB_REQ_SET_INTERFACE: 833 if (ctrl->bRequestType != USB_RECIP_INTERFACE) 834 goto unknown; 835 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) 836 break; 837 f = cdev->config->interface[intf]; 838 if (!f) 839 break; 840 if (w_value && !f->set_alt) 841 break; 842 value = f->set_alt(f, w_index, w_value); 843 break; 844 case USB_REQ_GET_INTERFACE: 845 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)) 846 goto unknown; 847 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES) 848 break; 849 f = cdev->config->interface[intf]; 850 if (!f) 851 break; 852 /* lots of interfaces only need altsetting zero... */ 853 value = f->get_alt ? f->get_alt(f, w_index) : 0; 854 if (value < 0) 855 break; 856 *((u8 *)req->buf) = value; 857 value = min(w_length, (u16) 1); 858 break; 859 default: 860 unknown: 861 debug("non-core control req%02x.%02x v%04x i%04x l%d\n", 862 ctrl->bRequestType, ctrl->bRequest, 863 w_value, w_index, w_length); 864 865 /* 866 * functions always handle their interfaces and endpoints... 867 * punt other recipients (other, WUSB, ...) to the current 868 * configuration code. 869 */ 870 switch (ctrl->bRequestType & USB_RECIP_MASK) { 871 case USB_RECIP_INTERFACE: 872 f = cdev->config->interface[intf]; 873 break; 874 875 case USB_RECIP_ENDPOINT: 876 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f); 877 list_for_each_entry(f, &cdev->config->functions, list) { 878 if (test_bit(endp, f->endpoints)) 879 break; 880 } 881 if (&f->list == &cdev->config->functions) 882 f = NULL; 883 break; 884 /* 885 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device 886 * for non-standard request (w_value = 0x21, 887 * bRequest = GET_DESCRIPTOR in this case). 888 * When only one interface is registered (as it is done now), 889 * then this request shall be handled as it was requested for 890 * interface. 891 * 892 * In the below code it is checked if only one interface is 893 * present and proper function for it is extracted. Due to that 894 * function's setup (f->setup) is called to handle this 895 * special non-standard request. 896 */ 897 case USB_RECIP_DEVICE: 898 debug("cdev->config->next_interface_id: %d intf: %d\n", 899 cdev->config->next_interface_id, intf); 900 if (cdev->config->next_interface_id == 1) 901 f = cdev->config->interface[intf]; 902 break; 903 } 904 905 if (f && f->setup) 906 value = f->setup(f, ctrl); 907 else { 908 c = cdev->config; 909 if (c && c->setup) 910 value = c->setup(c, ctrl); 911 } 912 913 goto done; 914 } 915 916 /* respond with data transfer before status phase? */ 917 if (value >= 0) { 918 req->length = value; 919 req->zero = value < w_length; 920 value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL); 921 if (value < 0) { 922 debug("ep_queue --> %d\n", value); 923 req->status = 0; 924 composite_setup_complete(gadget->ep0, req); 925 } 926 } 927 928 done: 929 /* device either stalls (value < 0) or reports success */ 930 return value; 931 } 932 933 static void composite_disconnect(struct usb_gadget *gadget) 934 { 935 struct usb_composite_dev *cdev = get_gadget_data(gadget); 936 937 if (cdev->config) 938 reset_config(cdev); 939 if (composite->disconnect) 940 composite->disconnect(cdev); 941 } 942 943 static void composite_unbind(struct usb_gadget *gadget) 944 { 945 struct usb_composite_dev *cdev = get_gadget_data(gadget); 946 struct usb_configuration *c; 947 struct usb_function *f; 948 949 /* 950 * composite_disconnect() must already have been called 951 * by the underlying peripheral controller driver! 952 * so there's no i/o concurrency that could affect the 953 * state protected by cdev->lock. 954 */ 955 BUG_ON(cdev->config); 956 957 while (!list_empty(&cdev->configs)) { 958 c = list_first_entry(&cdev->configs, 959 struct usb_configuration, list); 960 while (!list_empty(&c->functions)) { 961 f = list_first_entry(&c->functions, 962 struct usb_function, list); 963 list_del(&f->list); 964 if (f->unbind) { 965 debug("unbind function '%s'/%p\n", 966 f->name, f); 967 f->unbind(c, f); 968 } 969 } 970 list_del(&c->list); 971 if (c->unbind) { 972 debug("unbind config '%s'/%p\n", c->label, c); 973 c->unbind(c); 974 } 975 free(c); 976 } 977 if (composite->unbind) 978 composite->unbind(cdev); 979 980 if (cdev->req) { 981 kfree(cdev->req->buf); 982 usb_ep_free_request(gadget->ep0, cdev->req); 983 } 984 kfree(cdev); 985 set_gadget_data(gadget, NULL); 986 987 composite = NULL; 988 } 989 990 static int composite_bind(struct usb_gadget *gadget) 991 { 992 int status = -ENOMEM; 993 struct usb_composite_dev *cdev; 994 995 cdev = calloc(sizeof *cdev, 1); 996 if (!cdev) 997 return status; 998 999 cdev->gadget = gadget; 1000 set_gadget_data(gadget, cdev); 1001 INIT_LIST_HEAD(&cdev->configs); 1002 1003 /* preallocate control response and buffer */ 1004 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL); 1005 if (!cdev->req) 1006 goto fail; 1007 cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ); 1008 if (!cdev->req->buf) 1009 goto fail; 1010 cdev->req->complete = composite_setup_complete; 1011 gadget->ep0->driver_data = cdev; 1012 1013 cdev->bufsiz = USB_BUFSIZ; 1014 cdev->driver = composite; 1015 1016 usb_gadget_set_selfpowered(gadget); 1017 usb_ep_autoconfig_reset(cdev->gadget); 1018 1019 status = composite->bind(cdev); 1020 if (status < 0) 1021 goto fail; 1022 1023 memcpy(&cdev->desc, composite->dev, 1024 sizeof(struct usb_device_descriptor)); 1025 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket; 1026 1027 debug("%s: ready\n", composite->name); 1028 return 0; 1029 1030 fail: 1031 composite_unbind(gadget); 1032 return status; 1033 } 1034 1035 static void 1036 composite_suspend(struct usb_gadget *gadget) 1037 { 1038 struct usb_composite_dev *cdev = get_gadget_data(gadget); 1039 struct usb_function *f; 1040 1041 debug("%s: suspend\n", __func__); 1042 if (cdev->config) { 1043 list_for_each_entry(f, &cdev->config->functions, list) { 1044 if (f->suspend) 1045 f->suspend(f); 1046 } 1047 } 1048 if (composite->suspend) 1049 composite->suspend(cdev); 1050 1051 cdev->suspended = 1; 1052 } 1053 1054 static void 1055 composite_resume(struct usb_gadget *gadget) 1056 { 1057 struct usb_composite_dev *cdev = get_gadget_data(gadget); 1058 struct usb_function *f; 1059 1060 debug("%s: resume\n", __func__); 1061 if (composite->resume) 1062 composite->resume(cdev); 1063 if (cdev->config) { 1064 list_for_each_entry(f, &cdev->config->functions, list) { 1065 if (f->resume) 1066 f->resume(f); 1067 } 1068 } 1069 1070 cdev->suspended = 0; 1071 } 1072 1073 static struct usb_gadget_driver composite_driver = { 1074 .speed = USB_SPEED_HIGH, 1075 1076 .bind = composite_bind, 1077 .unbind = composite_unbind, 1078 1079 .setup = composite_setup, 1080 .reset = composite_disconnect, 1081 .disconnect = composite_disconnect, 1082 1083 .suspend = composite_suspend, 1084 .resume = composite_resume, 1085 }; 1086 1087 /** 1088 * usb_composite_register() - register a composite driver 1089 * @driver: the driver to register 1090 * Context: single threaded during gadget setup 1091 * 1092 * This function is used to register drivers using the composite driver 1093 * framework. The return value is zero, or a negative errno value. 1094 * Those values normally come from the driver's @bind method, which does 1095 * all the work of setting up the driver to match the hardware. 1096 * 1097 * On successful return, the gadget is ready to respond to requests from 1098 * the host, unless one of its components invokes usb_gadget_disconnect() 1099 * while it was binding. That would usually be done in order to wait for 1100 * some userspace participation. 1101 */ 1102 int usb_composite_register(struct usb_composite_driver *driver) 1103 { 1104 int res; 1105 1106 if (!driver || !driver->dev || !driver->bind || composite) 1107 return -EINVAL; 1108 1109 if (!driver->name) 1110 driver->name = "composite"; 1111 composite = driver; 1112 1113 res = usb_gadget_register_driver(&composite_driver); 1114 if (res != 0) 1115 composite = NULL; 1116 1117 return res; 1118 } 1119 1120 /** 1121 * usb_composite_unregister() - unregister a composite driver 1122 * @driver: the driver to unregister 1123 * 1124 * This function is used to unregister drivers using the composite 1125 * driver framework. 1126 */ 1127 void usb_composite_unregister(struct usb_composite_driver *driver) 1128 { 1129 if (composite != driver) 1130 return; 1131 usb_gadget_unregister_driver(&composite_driver); 1132 composite = NULL; 1133 } 1134