uboot/drivers/usb/gadget/composite.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * composite.c - infrastructure for Composite USB Gadgets
   4 *
   5 * Copyright (C) 2006-2008 David Brownell
   6 * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com>
   7 */
   8#undef DEBUG
   9
  10#include <log.h>
  11#include <dm/devres.h>
  12#include <linux/bitops.h>
  13#include <linux/bug.h>
  14#include <linux/usb/composite.h>
  15#include "u_os_desc.h"
  16
  17#define USB_BUFSIZ      4096
  18
  19/* Helper type for accessing packed u16 pointers */
  20typedef struct { __le16 val; } __packed __le16_packed;
  21
  22static struct usb_composite_driver *composite;
  23static struct usb_configuration *os_desc_config;
  24
  25/* Microsoft OS String Descriptor */
  26static char qw_sign_buf[OS_STRING_QW_SIGN_LEN / 2] = {'M', 'S', 'F', 'T', '1', '0', '0'};
  27
  28static inline void le16_add_cpu_packed(__le16_packed *var, u16 val)
  29{
  30        var->val = cpu_to_le16(le16_to_cpu(var->val) + val);
  31}
  32
  33/**
  34 * struct usb_os_string - represents OS String to be reported by a gadget
  35 * @bLength: total length of the entire descritor, always 0x12
  36 * @bDescriptorType: USB_DT_STRING
  37 * @qwSignature: the OS String proper
  38 * @bMS_VendorCode: code used by the host for subsequent requests
  39 * @bPad: not used, must be zero
  40 */
  41struct usb_os_string {
  42        __u8    bLength;
  43        __u8    bDescriptorType;
  44        __u8    qwSignature[OS_STRING_QW_SIGN_LEN];
  45        __u8    bMS_VendorCode;
  46        __u8    bPad;
  47} __packed;
  48
  49/**
  50 * usb_add_function() - add a function to a configuration
  51 * @config: the configuration
  52 * @function: the function being added
  53 * Context: single threaded during gadget setup
  54 *
  55 * After initialization, each configuration must have one or more
  56 * functions added to it.  Adding a function involves calling its @bind()
  57 * method to allocate resources such as interface and string identifiers
  58 * and endpoints.
  59 *
  60 * This function returns the value of the function's bind(), which is
  61 * zero for success else a negative errno value.
  62 */
  63int usb_add_function(struct usb_configuration *config,
  64                struct usb_function *function)
  65{
  66        int     value = -EINVAL;
  67
  68        debug("adding '%s'/%p to config '%s'/%p\n",
  69                        function->name, function,
  70                        config->label, config);
  71
  72        if (!function->set_alt || !function->disable)
  73                goto done;
  74
  75        function->config = config;
  76        list_add_tail(&function->list, &config->functions);
  77
  78        if (function->bind) {
  79                value = function->bind(config, function);
  80                if (value < 0) {
  81                        list_del(&function->list);
  82                        function->config = NULL;
  83                }
  84        } else
  85                value = 0;
  86
  87        if (!config->fullspeed && function->descriptors)
  88                config->fullspeed = 1;
  89        if (!config->highspeed && function->hs_descriptors)
  90                config->highspeed = 1;
  91        if (!config->superspeed && function->ss_descriptors)
  92                config->superspeed = 1;
  93
  94done:
  95        if (value)
  96                debug("adding '%s'/%p --> %d\n",
  97                                function->name, function, value);
  98        return value;
  99}
 100
 101/**
 102 * usb_function_deactivate - prevent function and gadget enumeration
 103 * @function: the function that isn't yet ready to respond
 104 *
 105 * Blocks response of the gadget driver to host enumeration by
 106 * preventing the data line pullup from being activated.  This is
 107 * normally called during @bind() processing to change from the
 108 * initial "ready to respond" state, or when a required resource
 109 * becomes available.
 110 *
 111 * For example, drivers that serve as a passthrough to a userspace
 112 * daemon can block enumeration unless that daemon (such as an OBEX,
 113 * MTP, or print server) is ready to handle host requests.
 114 *
 115 * Not all systems support software control of their USB peripheral
 116 * data pullups.
 117 *
 118 * Returns zero on success, else negative errno.
 119 */
 120int usb_function_deactivate(struct usb_function *function)
 121{
 122        struct usb_composite_dev        *cdev = function->config->cdev;
 123        int                             status = 0;
 124
 125        if (cdev->deactivations == 0)
 126                status = usb_gadget_disconnect(cdev->gadget);
 127        if (status == 0)
 128                cdev->deactivations++;
 129
 130        return status;
 131}
 132
 133/**
 134 * usb_function_activate - allow function and gadget enumeration
 135 * @function: function on which usb_function_activate() was called
 136 *
 137 * Reverses effect of usb_function_deactivate().  If no more functions
 138 * are delaying their activation, the gadget driver will respond to
 139 * host enumeration procedures.
 140 *
 141 * Returns zero on success, else negative errno.
 142 */
 143int usb_function_activate(struct usb_function *function)
 144{
 145        struct usb_composite_dev        *cdev = function->config->cdev;
 146        int                             status = 0;
 147
 148        if (cdev->deactivations == 0)
 149                status = -EINVAL;
 150        else {
 151                cdev->deactivations--;
 152                if (cdev->deactivations == 0)
 153                        status = usb_gadget_connect(cdev->gadget);
 154        }
 155
 156        return status;
 157}
 158
 159/**
 160 * usb_interface_id() - allocate an unused interface ID
 161 * @config: configuration associated with the interface
 162 * @function: function handling the interface
 163 * Context: single threaded during gadget setup
 164 *
 165 * usb_interface_id() is called from usb_function.bind() callbacks to
 166 * allocate new interface IDs.  The function driver will then store that
 167 * ID in interface, association, CDC union, and other descriptors.  It
 168 * will also handle any control requests targetted at that interface,
 169 * particularly changing its altsetting via set_alt().  There may
 170 * also be class-specific or vendor-specific requests to handle.
 171 *
 172 * All interface identifier should be allocated using this routine, to
 173 * ensure that for example different functions don't wrongly assign
 174 * different meanings to the same identifier.  Note that since interface
 175 * identifers are configuration-specific, functions used in more than
 176 * one configuration (or more than once in a given configuration) need
 177 * multiple versions of the relevant descriptors.
 178 *
 179 * Returns the interface ID which was allocated; or -ENODEV if no
 180 * more interface IDs can be allocated.
 181 */
 182int usb_interface_id(struct usb_configuration *config,
 183                struct usb_function *function)
 184{
 185        unsigned char id = config->next_interface_id;
 186
 187        if (id < MAX_CONFIG_INTERFACES) {
 188                config->interface[id] = function;
 189                config->next_interface_id = id + 1;
 190                return id;
 191        }
 192        return -ENODEV;
 193}
 194
 195static int config_buf(struct usb_configuration *config,
 196                enum usb_device_speed speed, void *buf, u8 type)
 197{
 198        int                             len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
 199        void                            *next = buf + USB_DT_CONFIG_SIZE;
 200        struct usb_descriptor_header    **descriptors;
 201        struct usb_config_descriptor    *c;
 202        int                             status;
 203        struct usb_function             *f;
 204
 205        /* write the config descriptor */
 206        c = buf;
 207        c->bLength = USB_DT_CONFIG_SIZE;
 208        c->bDescriptorType = type;
 209
 210        c->bNumInterfaces = config->next_interface_id;
 211        c->bConfigurationValue = config->bConfigurationValue;
 212        c->iConfiguration = config->iConfiguration;
 213        c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
 214        c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
 215
 216        /* There may be e.g. OTG descriptors */
 217        if (config->descriptors) {
 218                status = usb_descriptor_fillbuf(next, len,
 219                                config->descriptors);
 220                if (status < 0)
 221                        return status;
 222                len -= status;
 223                next += status;
 224        }
 225
 226        /* add each function's descriptors */
 227        list_for_each_entry(f, &config->functions, list) {
 228                if (speed == USB_SPEED_SUPER)
 229                        descriptors = f->ss_descriptors;
 230                else if (speed == USB_SPEED_HIGH)
 231                        descriptors = f->hs_descriptors;
 232                else
 233                        descriptors = f->descriptors;
 234                if (!descriptors)
 235                        continue;
 236                status = usb_descriptor_fillbuf(next, len,
 237                        (const struct usb_descriptor_header **) descriptors);
 238                if (status < 0)
 239                        return status;
 240                len -= status;
 241                next += status;
 242        }
 243
 244        len = next - buf;
 245        c->wTotalLength = cpu_to_le16(len);
 246        return len;
 247}
 248
 249static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
 250{
 251        enum usb_device_speed           speed = USB_SPEED_UNKNOWN;
 252        struct usb_gadget               *gadget = cdev->gadget;
 253        u8                              type = w_value >> 8;
 254        int                             hs = 0;
 255        struct usb_configuration        *c;
 256        struct list_head                *pos;
 257
 258        if (gadget_is_superspeed(gadget)) {
 259                speed = gadget->speed;
 260        } else if (gadget_is_dualspeed(gadget)) {
 261                if (gadget->speed == USB_SPEED_HIGH)
 262                        hs = 1;
 263                if (type == USB_DT_OTHER_SPEED_CONFIG)
 264                        hs = !hs;
 265                if (hs)
 266                        speed = USB_SPEED_HIGH;
 267        }
 268
 269        w_value &= 0xff;
 270
 271        pos = &cdev->configs;
 272        c = cdev->os_desc_config;
 273        if (c)
 274                goto check_config;
 275
 276        while ((pos = pos->next) !=  &cdev->configs) {
 277                c = list_entry(pos, typeof(*c), list);
 278
 279                /* skip OS Descriptors config which is handled separately */
 280                if (c == cdev->os_desc_config)
 281                        continue;
 282
 283check_config:
 284                if (speed == USB_SPEED_SUPER) {
 285                        if (!c->superspeed)
 286                                continue;
 287                } else if (speed == USB_SPEED_HIGH) {
 288                        if (!c->highspeed)
 289                                continue;
 290                } else {
 291                        if (!c->fullspeed)
 292                                continue;
 293                }
 294                if (w_value == 0)
 295                        return config_buf(c, speed, cdev->req->buf, type);
 296                w_value--;
 297        }
 298        return -EINVAL;
 299}
 300
 301static int count_configs(struct usb_composite_dev *cdev, unsigned type)
 302{
 303        struct usb_gadget               *gadget = cdev->gadget;
 304        unsigned                        count = 0;
 305        int                             hs = 0;
 306        int                             ss = 0;
 307        struct usb_configuration        *c;
 308
 309        if (gadget->speed == USB_SPEED_SUPER)
 310                ss = 1;
 311
 312        if (gadget_is_dualspeed(gadget)) {
 313                if (gadget->speed == USB_SPEED_HIGH)
 314                        hs = 1;
 315                if (type == USB_DT_DEVICE_QUALIFIER)
 316                        hs = !hs;
 317        }
 318        list_for_each_entry(c, &cdev->configs, list) {
 319                /* ignore configs that won't work at this speed */
 320                if (ss) {
 321                        if (!c->superspeed)
 322                                continue;
 323                } else if (hs) {
 324                        if (!c->highspeed)
 325                                continue;
 326                } else {
 327                        if (!c->fullspeed)
 328                                continue;
 329                }
 330                count++;
 331        }
 332        return count;
 333}
 334
 335static void device_qual(struct usb_composite_dev *cdev)
 336{
 337        struct usb_qualifier_descriptor *qual = cdev->req->buf;
 338
 339        qual->bLength = sizeof(*qual);
 340        qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
 341        /* POLICY: same bcdUSB and device type info at both speeds */
 342        qual->bcdUSB = cdev->desc.bcdUSB;
 343        qual->bDeviceClass = cdev->desc.bDeviceClass;
 344        qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
 345        qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
 346        /* ASSUME same EP0 fifo size at both speeds */
 347        qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
 348        qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
 349        qual->bRESERVED = 0;
 350}
 351
 352static void reset_config(struct usb_composite_dev *cdev)
 353{
 354        struct usb_function             *f;
 355
 356        debug("%s:\n", __func__);
 357
 358        list_for_each_entry(f, &cdev->config->functions, list) {
 359                if (f->disable)
 360                        f->disable(f);
 361
 362                bitmap_zero(f->endpoints, 32);
 363        }
 364        cdev->config = NULL;
 365}
 366
 367static int set_config(struct usb_composite_dev *cdev,
 368                const struct usb_ctrlrequest *ctrl, unsigned number)
 369{
 370        struct usb_gadget       *gadget = cdev->gadget;
 371        unsigned                power = gadget_is_otg(gadget) ? 8 : 100;
 372        struct usb_descriptor_header **descriptors;
 373        int                     result = -EINVAL;
 374        struct usb_endpoint_descriptor *ep;
 375        struct usb_configuration *c = NULL;
 376        int                     addr;
 377        int                     tmp;
 378        struct usb_function     *f;
 379
 380        if (cdev->config)
 381                reset_config(cdev);
 382
 383        if (number) {
 384                list_for_each_entry(c, &cdev->configs, list) {
 385                        if (c->bConfigurationValue == number) {
 386                                result = 0;
 387                                break;
 388                        }
 389                }
 390                if (result < 0)
 391                        goto done;
 392        } else
 393                result = 0;
 394
 395        debug("%s: %s speed config #%d: %s\n", __func__,
 396             ({ char *speed;
 397                     switch (gadget->speed) {
 398                     case USB_SPEED_LOW:
 399                             speed = "low";
 400                             break;
 401                     case USB_SPEED_FULL:
 402                             speed = "full";
 403                             break;
 404                     case USB_SPEED_HIGH:
 405                             speed = "high";
 406                             break;
 407                     case USB_SPEED_SUPER:
 408                             speed = "super";
 409                             break;
 410                     default:
 411                             speed = "?";
 412                             break;
 413                     };
 414                     speed;
 415             }), number, c ? c->label : "unconfigured");
 416
 417        if (!c)
 418                goto done;
 419
 420        cdev->config = c;
 421
 422        /* Initialize all interfaces by setting them to altsetting zero. */
 423        for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
 424                f = c->interface[tmp];
 425                if (!f)
 426                        break;
 427
 428                /*
 429                 * Record which endpoints are used by the function. This is used
 430                 * to dispatch control requests targeted at that endpoint to the
 431                 * function's setup callback instead of the current
 432                 * configuration's setup callback.
 433                 */
 434                if (gadget->speed == USB_SPEED_SUPER)
 435                        descriptors = f->ss_descriptors;
 436                else if (gadget->speed == USB_SPEED_HIGH)
 437                        descriptors = f->hs_descriptors;
 438                else
 439                        descriptors = f->descriptors;
 440
 441                for (; *descriptors; ++descriptors) {
 442                        if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
 443                                continue;
 444
 445                        ep = (struct usb_endpoint_descriptor *)*descriptors;
 446                        addr = ((ep->bEndpointAddress & 0x80) >> 3)
 447                             |  (ep->bEndpointAddress & 0x0f);
 448                        generic_set_bit(addr, f->endpoints);
 449                }
 450
 451                result = f->set_alt(f, tmp, 0);
 452                if (result < 0) {
 453                        debug("interface %d (%s/%p) alt 0 --> %d\n",
 454                                        tmp, f->name, f, result);
 455
 456                        reset_config(cdev);
 457                        goto done;
 458                }
 459        }
 460
 461        /* when we return, be sure our power usage is valid */
 462        power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
 463done:
 464        usb_gadget_vbus_draw(gadget, power);
 465        return result;
 466}
 467
 468/**
 469 * usb_add_config() - add a configuration to a device.
 470 * @cdev: wraps the USB gadget
 471 * @config: the configuration, with bConfigurationValue assigned
 472 * Context: single threaded during gadget setup
 473 *
 474 * One of the main tasks of a composite driver's bind() routine is to
 475 * add each of the configurations it supports, using this routine.
 476 *
 477 * This function returns the value of the configuration's bind(), which
 478 * is zero for success else a negative errno value.  Binding configurations
 479 * assigns global resources including string IDs, and per-configuration
 480 * resources such as interface IDs and endpoints.
 481 */
 482int usb_add_config(struct usb_composite_dev *cdev,
 483                struct usb_configuration *config)
 484{
 485        int                             status = -EINVAL;
 486        struct usb_configuration        *c;
 487        struct usb_function             *f;
 488        unsigned int                    i;
 489
 490        debug("%s: adding config #%u '%s'/%p\n", __func__,
 491                        config->bConfigurationValue,
 492                        config->label, config);
 493
 494        if (!config->bConfigurationValue || !config->bind)
 495                goto done;
 496
 497        /* Prevent duplicate configuration identifiers */
 498        list_for_each_entry(c, &cdev->configs, list) {
 499                if (c->bConfigurationValue == config->bConfigurationValue) {
 500                        status = -EBUSY;
 501                        goto done;
 502                }
 503        }
 504
 505        config->cdev = cdev;
 506        list_add_tail(&config->list, &cdev->configs);
 507
 508        INIT_LIST_HEAD(&config->functions);
 509        config->next_interface_id = 0;
 510
 511        status = config->bind(config);
 512        if (status < 0) {
 513                list_del(&config->list);
 514                config->cdev = NULL;
 515        } else {
 516                debug("cfg %d/%p speeds:%s%s%s\n",
 517                        config->bConfigurationValue, config,
 518                        config->superspeed ? " super" : "",
 519                        config->highspeed ? " high" : "",
 520                        config->fullspeed
 521                                ? (gadget_is_dualspeed(cdev->gadget)
 522                                        ? " full"
 523                                        : " full/low")
 524                                : "");
 525
 526                for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
 527                        f = config->interface[i];
 528                        if (!f)
 529                                continue;
 530                        debug("%s: interface %d = %s/%p\n",
 531                              __func__, i, f->name, f);
 532                }
 533        }
 534
 535        /*
 536         * If one function of config is not super speed capable,
 537         * force the gadget to be high speed so controller driver
 538         * can init HW to be USB 2.0
 539         */
 540        if (gadget_is_superspeed(cdev->gadget)) {
 541                list_for_each_entry(f, &config->functions, list) {
 542                        if (!f->ss_descriptors)
 543                                cdev->gadget->max_speed =
 544                                        USB_SPEED_HIGH;
 545                }
 546        }
 547
 548        usb_ep_autoconfig_reset(cdev->gadget);
 549
 550        os_desc_config = config;
 551        cdev->os_desc_config = os_desc_config;
 552
 553done:
 554        if (status)
 555                debug("added config '%s'/%u --> %d\n", config->label,
 556                                config->bConfigurationValue, status);
 557        return status;
 558}
 559
 560/*
 561 * We support strings in multiple languages ... string descriptor zero
 562 * says which languages are supported.  The typical case will be that
 563 * only one language (probably English) is used, with I18N handled on
 564 * the host side.
 565 */
 566
 567static void collect_langs(struct usb_gadget_strings **sp, void *buf)
 568{
 569        const struct usb_gadget_strings *s;
 570        u16                             language;
 571        __le16_packed                   *tmp;
 572        __le16_packed                   *end = (buf + 252);
 573
 574        while (*sp) {
 575                s = *sp;
 576                language = cpu_to_le16(s->language);
 577                for (tmp = buf; tmp->val && tmp < end; tmp++) {
 578                        if (tmp->val == language)
 579                                goto repeat;
 580                }
 581                tmp->val = language;
 582repeat:
 583                sp++;
 584        }
 585}
 586
 587static int lookup_string(
 588        struct usb_gadget_strings       **sp,
 589        void                            *buf,
 590        u16                             language,
 591        int                             id
 592)
 593{
 594        int                             value;
 595        struct usb_gadget_strings       *s;
 596
 597        while (*sp) {
 598                s = *sp++;
 599                if (s->language != language)
 600                        continue;
 601                value = usb_gadget_get_string(s, id, buf);
 602                if (value > 0)
 603                        return value;
 604        }
 605        return -EINVAL;
 606}
 607
 608static int get_string(struct usb_composite_dev *cdev,
 609                void *buf, u16 language, int id)
 610{
 611        struct usb_string_descriptor    *s = buf;
 612        struct usb_gadget_strings       **sp;
 613        int                             len;
 614        struct usb_configuration        *c;
 615        struct usb_function             *f;
 616
 617        /*
 618         * Yes, not only is USB's I18N support probably more than most
 619         * folk will ever care about ... also, it's all supported here.
 620         * (Except for UTF8 support for Unicode's "Astral Planes".)
 621         */
 622
 623        /* 0 == report all available language codes */
 624        if (id == 0) {
 625                memset(s, 0, 256);
 626                s->bDescriptorType = USB_DT_STRING;
 627
 628                sp = composite->strings;
 629                if (sp)
 630                        collect_langs(sp, s->wData);
 631
 632                list_for_each_entry(c, &cdev->configs, list) {
 633                        sp = c->strings;
 634                        if (sp)
 635                                collect_langs(sp, s->wData);
 636
 637                        list_for_each_entry(f, &c->functions, list) {
 638                                sp = f->strings;
 639                                if (sp)
 640                                        collect_langs(sp, s->wData);
 641                        }
 642                }
 643
 644                for (len = 0; len <= 126 && s->wData[len]; len++)
 645                        continue;
 646                if (!len)
 647                        return -EINVAL;
 648
 649                s->bLength = 2 * (len + 1);
 650                return s->bLength;
 651        }
 652
 653        if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
 654                struct usb_os_string *b = buf;
 655                b->bLength = sizeof(*b);
 656                b->bDescriptorType = USB_DT_STRING;
 657                memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
 658                b->bMS_VendorCode = cdev->b_vendor_code;
 659                b->bPad = 0;
 660                return sizeof(*b);
 661        }
 662
 663        /*
 664         * Otherwise, look up and return a specified string.  String IDs
 665         * are device-scoped, so we look up each string table we're told
 666         * about.  These lookups are infrequent; simpler-is-better here.
 667         */
 668        if (composite->strings) {
 669                len = lookup_string(composite->strings, buf, language, id);
 670                if (len > 0)
 671                        return len;
 672        }
 673        list_for_each_entry(c, &cdev->configs, list) {
 674                if (c->strings) {
 675                        len = lookup_string(c->strings, buf, language, id);
 676                        if (len > 0)
 677                                return len;
 678                }
 679                list_for_each_entry(f, &c->functions, list) {
 680                        if (!f->strings)
 681                                continue;
 682                        len = lookup_string(f->strings, buf, language, id);
 683                        if (len > 0)
 684                                return len;
 685                }
 686        }
 687        return -EINVAL;
 688}
 689
 690/**
 691 * usb_string_id() - allocate an unused string ID
 692 * @cdev: the device whose string descriptor IDs are being allocated
 693 * Context: single threaded during gadget setup
 694 *
 695 * @usb_string_id() is called from bind() callbacks to allocate
 696 * string IDs.  Drivers for functions, configurations, or gadgets will
 697 * then store that ID in the appropriate descriptors and string table.
 698 *
 699 * All string identifier should be allocated using this,
 700 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
 701 * that for example different functions don't wrongly assign different
 702 * meanings to the same identifier.
 703 */
 704int usb_string_id(struct usb_composite_dev *cdev)
 705{
 706        if (cdev->next_string_id < 254) {
 707                /*
 708                 * string id 0 is reserved by USB spec for list of
 709                 * supported languages
 710                 * 255 reserved as well? -- mina86
 711                 */
 712                cdev->next_string_id++;
 713                return cdev->next_string_id;
 714        }
 715        return -ENODEV;
 716}
 717
 718/**
 719 * usb_string_ids() - allocate unused string IDs in batch
 720 * @cdev: the device whose string descriptor IDs are being allocated
 721 * @str: an array of usb_string objects to assign numbers to
 722 * Context: single threaded during gadget setup
 723 *
 724 * @usb_string_ids() is called from bind() callbacks to allocate
 725 * string IDs.  Drivers for functions, configurations, or gadgets will
 726 * then copy IDs from the string table to the appropriate descriptors
 727 * and string table for other languages.
 728 *
 729 * All string identifier should be allocated using this,
 730 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
 731 * example different functions don't wrongly assign different meanings
 732 * to the same identifier.
 733 */
 734int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
 735{
 736        u8 next = cdev->next_string_id;
 737
 738        for (; str->s; ++str) {
 739                if (next >= 254)
 740                        return -ENODEV;
 741                str->id = ++next;
 742        }
 743
 744        cdev->next_string_id = next;
 745
 746        return 0;
 747}
 748
 749/**
 750 * usb_string_ids_n() - allocate unused string IDs in batch
 751 * @c: the device whose string descriptor IDs are being allocated
 752 * @n: number of string IDs to allocate
 753 * Context: single threaded during gadget setup
 754 *
 755 * Returns the first requested ID.  This ID and next @n-1 IDs are now
 756 * valid IDs.  At least provided that @n is non-zero because if it
 757 * is, returns last requested ID which is now very useful information.
 758 *
 759 * @usb_string_ids_n() is called from bind() callbacks to allocate
 760 * string IDs.  Drivers for functions, configurations, or gadgets will
 761 * then store that ID in the appropriate descriptors and string table.
 762 *
 763 * All string identifier should be allocated using this,
 764 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
 765 * example different functions don't wrongly assign different meanings
 766 * to the same identifier.
 767 */
 768int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
 769{
 770        u8 next = c->next_string_id;
 771
 772        if (n > 254 || next + n > 254)
 773                return -ENODEV;
 774
 775        c->next_string_id += n;
 776        return next + 1;
 777}
 778
 779static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
 780{
 781        if (req->status || req->actual != req->length)
 782                debug("%s: setup complete --> %d, %d/%d\n", __func__,
 783                                req->status, req->actual, req->length);
 784}
 785
 786static int bos_desc(struct usb_composite_dev *cdev)
 787{
 788        struct usb_ext_cap_descriptor   *usb_ext;
 789        struct usb_dcd_config_params    dcd_config_params;
 790        struct usb_bos_descriptor       *bos = cdev->req->buf;
 791
 792        bos->bLength = USB_DT_BOS_SIZE;
 793        bos->bDescriptorType = USB_DT_BOS;
 794
 795        bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
 796        bos->bNumDeviceCaps = 0;
 797
 798        /*
 799         * A SuperSpeed device shall include the USB2.0 extension descriptor
 800         * and shall support LPM when operating in USB2.0 HS mode.
 801         */
 802        usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
 803        bos->bNumDeviceCaps++;
 804        le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
 805                            USB_DT_USB_EXT_CAP_SIZE);
 806        usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
 807        usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
 808        usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
 809        usb_ext->bmAttributes =
 810                cpu_to_le32(USB_LPM_SUPPORT | USB_BESL_SUPPORT);
 811
 812        /*
 813         * The Superspeed USB Capability descriptor shall be implemented
 814         * by all SuperSpeed devices.
 815         */
 816        if (gadget_is_superspeed(cdev->gadget)) {
 817                struct usb_ss_cap_descriptor *ss_cap;
 818
 819                ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
 820                bos->bNumDeviceCaps++;
 821                le16_add_cpu_packed((__le16_packed *)&bos->wTotalLength,
 822                                    USB_DT_USB_SS_CAP_SIZE);
 823                ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
 824                ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
 825                ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
 826                ss_cap->bmAttributes = 0; /* LTM is not supported yet */
 827                ss_cap->wSpeedSupported =
 828                        cpu_to_le16(USB_LOW_SPEED_OPERATION |
 829                                    USB_FULL_SPEED_OPERATION |
 830                                    USB_HIGH_SPEED_OPERATION |
 831                                    USB_5GBPS_OPERATION);
 832                ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
 833
 834                /* Get Controller configuration */
 835                if (cdev->gadget->ops->get_config_params) {
 836                        cdev->gadget->ops->get_config_params(
 837                                &dcd_config_params);
 838                } else {
 839                        dcd_config_params.bU1devExitLat =
 840                                USB_DEFAULT_U1_DEV_EXIT_LAT;
 841                        dcd_config_params.bU2DevExitLat =
 842                                cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
 843                }
 844                ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
 845                ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
 846        }
 847        return le16_to_cpu(bos->wTotalLength);
 848}
 849
 850static int count_ext_compat(struct usb_configuration *c)
 851{
 852        int i, res;
 853
 854        res = 0;
 855        for (i = 0; i < c->next_interface_id; ++i) {
 856                struct usb_function *f;
 857                int j;
 858
 859                f = c->interface[i];
 860                for (j = 0; j < f->os_desc_n; ++j) {
 861                        struct usb_os_desc *d;
 862
 863                        if (i != f->os_desc_table[j].if_id)
 864                                continue;
 865                        d = f->os_desc_table[j].os_desc;
 866                        if (d && d->ext_compat_id)
 867                                ++res;
 868                }
 869        }
 870        BUG_ON(res > 255);
 871        return res;
 872}
 873
 874static void fill_ext_compat(struct usb_configuration *c, u8 *buf)
 875{
 876        int i, count;
 877
 878        count = 16;
 879        for (i = 0; i < c->next_interface_id; ++i) {
 880                struct usb_function *f;
 881                int j;
 882
 883                f = c->interface[i];
 884                for (j = 0; j < f->os_desc_n; ++j) {
 885                        struct usb_os_desc *d;
 886
 887                        if (i != f->os_desc_table[j].if_id)
 888                                continue;
 889                        d = f->os_desc_table[j].os_desc;
 890                        if (d && d->ext_compat_id) {
 891                                *buf++ = i;
 892                                *buf++ = 0x01;
 893                                memcpy(buf, d->ext_compat_id, 16);
 894                                buf += 22;
 895                        } else {
 896                                ++buf;
 897                                *buf = 0x01;
 898                                buf += 23;
 899                        }
 900                        count += 24;
 901                        if (count >= 4096)
 902                                return;
 903                }
 904        }
 905}
 906
 907static int count_ext_prop(struct usb_configuration *c, int interface)
 908{
 909        struct usb_function *f;
 910        int j;
 911
 912        f = c->interface[interface];
 913        for (j = 0; j < f->os_desc_n; ++j) {
 914                struct usb_os_desc *d;
 915
 916                if (interface != f->os_desc_table[j].if_id)
 917                        continue;
 918                d = f->os_desc_table[j].os_desc;
 919                if (d && d->ext_compat_id)
 920                        return d->ext_prop_count;
 921        }
 922        return 0;
 923}
 924
 925static int len_ext_prop(struct usb_configuration *c, int interface)
 926{
 927        struct usb_function *f;
 928        struct usb_os_desc *d;
 929        int j, res;
 930
 931        res = 10; /* header length */
 932        f = c->interface[interface];
 933        for (j = 0; j < f->os_desc_n; ++j) {
 934                if (interface != f->os_desc_table[j].if_id)
 935                        continue;
 936                d = f->os_desc_table[j].os_desc;
 937                if (d)
 938                        return min(res + d->ext_prop_len, 4096);
 939        }
 940        return res;
 941}
 942
 943static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
 944{
 945        struct usb_function *f;
 946        struct usb_os_desc *d;
 947        struct usb_os_desc_ext_prop *ext_prop;
 948        int j, count, n, ret;
 949        u8 *start = buf;
 950
 951        f = c->interface[interface];
 952        for (j = 0; j < f->os_desc_n; ++j) {
 953                if (interface != f->os_desc_table[j].if_id)
 954                        continue;
 955                d = f->os_desc_table[j].os_desc;
 956                if (d)
 957                        list_for_each_entry(ext_prop, &d->ext_prop, entry) {
 958                                /* 4kB minus header length */
 959                                n = buf - start;
 960                                if (n >= 4086)
 961                                        return 0;
 962
 963                                count = ext_prop->data_len +
 964                                        ext_prop->name_len + 14;
 965                                if (count > 4086 - n)
 966                                        return -EINVAL;
 967                                usb_ext_prop_put_size(buf, count);
 968                                usb_ext_prop_put_type(buf, ext_prop->type);
 969                                ret = usb_ext_prop_put_name(buf, ext_prop->name,
 970                                                            ext_prop->name_len);
 971                                if (ret < 0)
 972                                        return ret;
 973                                switch (ext_prop->type) {
 974                                case USB_EXT_PROP_UNICODE:
 975                                case USB_EXT_PROP_UNICODE_ENV:
 976                                case USB_EXT_PROP_UNICODE_LINK:
 977                                        usb_ext_prop_put_unicode(buf, ret,
 978                                                         ext_prop->data,
 979                                                         ext_prop->data_len);
 980                                        break;
 981                                case USB_EXT_PROP_BINARY:
 982                                        usb_ext_prop_put_binary(buf, ret,
 983                                                        ext_prop->data,
 984                                                        ext_prop->data_len);
 985                                        break;
 986                                case USB_EXT_PROP_LE32:
 987                                        /* not implemented */
 988                                case USB_EXT_PROP_BE32:
 989                                        /* not implemented */
 990                                default:
 991                                        return -EINVAL;
 992                                }
 993                                buf += count;
 994                        }
 995        }
 996
 997        return 0;
 998}
 999
1000/*
1001 * The setup() callback implements all the ep0 functionality that's
1002 * not handled lower down, in hardware or the hardware driver(like
1003 * device and endpoint feature flags, and their status).  It's all
1004 * housekeeping for the gadget function we're implementing.  Most of
1005 * the work is in config and function specific setup.
1006 */
1007static int
1008composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1009{
1010        u16                             w_length = le16_to_cpu(ctrl->wLength);
1011        u16                             w_index = le16_to_cpu(ctrl->wIndex);
1012        u16                             w_value = le16_to_cpu(ctrl->wValue);
1013        struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1014        u8                              intf = w_index & 0xFF;
1015        int                             value = -EOPNOTSUPP;
1016        struct usb_request              *req = cdev->req;
1017        struct usb_function             *f = NULL;
1018        int                             standard;
1019        u8                              endp;
1020        struct usb_configuration        *c;
1021
1022        /*
1023         * partial re-init of the response message; the function or the
1024         * gadget might need to intercept e.g. a control-OUT completion
1025         * when we delegate to it.
1026         */
1027        req->zero = 0;
1028        req->complete = composite_setup_complete;
1029        req->length = USB_BUFSIZ;
1030        gadget->ep0->driver_data = cdev;
1031        standard = (ctrl->bRequestType & USB_TYPE_MASK)
1032                                                == USB_TYPE_STANDARD;
1033        if (!standard)
1034                goto unknown;
1035
1036        switch (ctrl->bRequest) {
1037
1038        /* we handle all standard USB descriptors */
1039        case USB_REQ_GET_DESCRIPTOR:
1040                if (ctrl->bRequestType != USB_DIR_IN)
1041                        goto unknown;
1042                switch (w_value >> 8) {
1043
1044                case USB_DT_DEVICE:
1045                        cdev->desc.bNumConfigurations =
1046                                count_configs(cdev, USB_DT_DEVICE);
1047
1048                        cdev->desc.bMaxPacketSize0 =
1049                                cdev->gadget->ep0->maxpacket;
1050                        if (gadget->speed >= USB_SPEED_SUPER) {
1051                                cdev->desc.bcdUSB = cpu_to_le16(0x0310);
1052                                cdev->desc.bMaxPacketSize0 = 9;
1053                        } else {
1054                                cdev->desc.bcdUSB = cpu_to_le16(0x0200);
1055                        }
1056                        value = min(w_length, (u16) sizeof cdev->desc);
1057                        memcpy(req->buf, &cdev->desc, value);
1058                        break;
1059                case USB_DT_DEVICE_QUALIFIER:
1060                        if (!gadget_is_dualspeed(gadget) ||
1061                            gadget->speed >= USB_SPEED_SUPER)
1062                                break;
1063                        device_qual(cdev);
1064                        value = min_t(int, w_length,
1065                                      sizeof(struct usb_qualifier_descriptor));
1066                        break;
1067                case USB_DT_OTHER_SPEED_CONFIG:
1068                        if (!gadget_is_dualspeed(gadget) ||
1069                            gadget->speed >= USB_SPEED_SUPER)
1070                                break;
1071                        fallthrough;
1072                case USB_DT_CONFIG:
1073                        value = config_desc(cdev, w_value);
1074                        if (value >= 0)
1075                                value = min(w_length, (u16) value);
1076                        break;
1077                case USB_DT_STRING:
1078                        value = get_string(cdev, req->buf,
1079                                        w_index, w_value & 0xff);
1080                        if (value >= 0)
1081                                value = min(w_length, (u16) value);
1082                        break;
1083                case USB_DT_BOS:
1084                        /*
1085                         * Super speed connection should support BOS, and
1086                         * USB compliance test (USB 2.0 Command Verifier)
1087                         * also issues this request, return for now for
1088                         * USB 2.0 connection.
1089                         */
1090                        if (gadget->speed >= USB_SPEED_SUPER) {
1091                                value = bos_desc(cdev);
1092                                value = min(w_length, (u16)value);
1093                        }
1094                        break;
1095                default:
1096                        goto unknown;
1097                }
1098                break;
1099
1100        /* any number of configs can work */
1101        case USB_REQ_SET_CONFIGURATION:
1102                if (ctrl->bRequestType != 0)
1103                        goto unknown;
1104                if (gadget_is_otg(gadget)) {
1105                        if (gadget->a_hnp_support)
1106                                debug("HNP available\n");
1107                        else if (gadget->a_alt_hnp_support)
1108                                debug("HNP on another port\n");
1109                        else
1110                                debug("HNP inactive\n");
1111                }
1112
1113                value = set_config(cdev, ctrl, w_value);
1114                break;
1115        case USB_REQ_GET_CONFIGURATION:
1116                if (ctrl->bRequestType != USB_DIR_IN)
1117                        goto unknown;
1118                if (cdev->config)
1119                        *(u8 *)req->buf = cdev->config->bConfigurationValue;
1120                else
1121                        *(u8 *)req->buf = 0;
1122                value = min(w_length, (u16) 1);
1123                break;
1124
1125        /*
1126         * function drivers must handle get/set altsetting; if there's
1127         * no get() method, we know only altsetting zero works.
1128         */
1129        case USB_REQ_SET_INTERFACE:
1130                if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1131                        goto unknown;
1132                if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1133                        break;
1134                f = cdev->config->interface[intf];
1135                if (!f)
1136                        break;
1137                if (w_value && !f->set_alt)
1138                        break;
1139                value = f->set_alt(f, w_index, w_value);
1140                break;
1141        case USB_REQ_GET_INTERFACE:
1142                if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1143                        goto unknown;
1144                if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1145                        break;
1146                f = cdev->config->interface[intf];
1147                if (!f)
1148                        break;
1149                /* lots of interfaces only need altsetting zero... */
1150                value = f->get_alt ? f->get_alt(f, w_index) : 0;
1151                if (value < 0)
1152                        break;
1153                *((u8 *)req->buf) = value;
1154                value = min(w_length, (u16) 1);
1155                break;
1156        default:
1157unknown:
1158                /*
1159                 * OS descriptors handling
1160                 */
1161                if (CONFIG_IS_ENABLED(USB_GADGET_OS_DESCRIPTORS) && cdev->use_os_string &&
1162                    cdev->os_desc_config && (ctrl->bRequestType & USB_TYPE_VENDOR) &&
1163                    ctrl->bRequest == cdev->b_vendor_code) {
1164                        struct usb_configuration        *os_desc_cfg;
1165                        u8                              *buf;
1166                        int                             interface;
1167                        int                             count = 0;
1168
1169                        buf = req->buf;
1170                        os_desc_cfg = cdev->os_desc_config;
1171                        memset(buf, 0, w_length);
1172                        buf[5] = 0x01;
1173                        switch (ctrl->bRequestType & USB_RECIP_MASK) {
1174                        case USB_RECIP_DEVICE:
1175                                if (w_index != 0x4 || (w_value >> 8))
1176                                        break;
1177                                buf[6] = w_index;
1178                                if (w_length == 0x10) {
1179                                        /* Number of ext compat interfaces */
1180                                        count = count_ext_compat(os_desc_cfg);
1181                                        buf[8] = count;
1182                                        count *= 24; /* 24 B/ext compat desc */
1183                                        count += 16; /* header */
1184                                        put_unaligned_le32(count, buf);
1185                                        value = w_length;
1186                                } else {
1187                                        /* "extended compatibility ID"s */
1188                                        count = count_ext_compat(os_desc_cfg);
1189                                        buf[8] = count;
1190                                        count *= 24; /* 24 B/ext compat desc */
1191                                        count += 16; /* header */
1192                                        put_unaligned_le32(count, buf);
1193                                        buf += 16;
1194                                        fill_ext_compat(os_desc_cfg, buf);
1195                                        value = w_length;
1196                                }
1197                                break;
1198                        case USB_RECIP_INTERFACE:
1199                                if (w_index != 0x5 || (w_value >> 8))
1200                                        break;
1201                                interface = w_value & 0xFF;
1202                                buf[6] = w_index;
1203                                if (w_length == 0x0A) {
1204                                        count = count_ext_prop(os_desc_cfg,
1205                                                interface);
1206                                        put_unaligned_le16(count, buf + 8);
1207                                        count = len_ext_prop(os_desc_cfg,
1208                                                interface);
1209                                        put_unaligned_le32(count, buf);
1210
1211                                        value = w_length;
1212                                } else {
1213                                        count = count_ext_prop(os_desc_cfg,
1214                                                interface);
1215                                        put_unaligned_le16(count, buf + 8);
1216                                        count = len_ext_prop(os_desc_cfg,
1217                                                interface);
1218                                        put_unaligned_le32(count, buf);
1219                                        buf += 10;
1220                                        value = fill_ext_prop(os_desc_cfg,
1221                                                              interface, buf);
1222                                        if (value < 0)
1223                                                return value;
1224
1225                                        value = w_length;
1226                                }
1227                                break;
1228                        }
1229
1230                        if (value >= 0) {
1231                                req->length = value;
1232                                req->zero = value < w_length;
1233                                value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1234                                if (value < 0) {
1235                                        debug("ep_queue --> %d\n", value);
1236                                        req->status = 0;
1237                                        composite_setup_complete(gadget->ep0, req);
1238                                }
1239                        }
1240                        return value;
1241                }
1242
1243                debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
1244                        ctrl->bRequestType, ctrl->bRequest,
1245                        w_value, w_index, w_length);
1246
1247                if (!cdev->config)
1248                        goto done;
1249
1250                /*
1251                 * functions always handle their interfaces and endpoints...
1252                 * punt other recipients (other, WUSB, ...) to the current
1253                 * configuration code.
1254                 */
1255                switch (ctrl->bRequestType & USB_RECIP_MASK) {
1256                case USB_RECIP_INTERFACE:
1257                        f = cdev->config->interface[intf];
1258                        break;
1259
1260                case USB_RECIP_ENDPOINT:
1261                        endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1262                        list_for_each_entry(f, &cdev->config->functions, list) {
1263                                if (test_bit(endp, f->endpoints))
1264                                        break;
1265                        }
1266                        if (&f->list == &cdev->config->functions)
1267                                f = NULL;
1268                        break;
1269                /*
1270                 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
1271                 * for non-standard request (w_value = 0x21,
1272                 * bRequest = GET_DESCRIPTOR in this case).
1273                 * When only one interface is registered (as it is done now),
1274                 * then this request shall be handled as it was requested for
1275                 * interface.
1276                 *
1277                 * In the below code it is checked if only one interface is
1278                 * present and proper function for it is extracted. Due to that
1279                 * function's setup (f->setup) is called to handle this
1280                 * special non-standard request.
1281                 */
1282                case USB_RECIP_DEVICE:
1283                        debug("cdev->config->next_interface_id: %d intf: %d\n",
1284                               cdev->config->next_interface_id, intf);
1285                        if (cdev->config->next_interface_id == 1)
1286                                f = cdev->config->interface[intf];
1287                        break;
1288                }
1289
1290                if (f && f->setup)
1291                        value = f->setup(f, ctrl);
1292                else {
1293                        c = cdev->config;
1294                        if (c->setup)
1295                                value = c->setup(c, ctrl);
1296                }
1297
1298                goto done;
1299        }
1300
1301        /* respond with data transfer before status phase? */
1302        if (value >= 0) {
1303                req->length = value;
1304                req->zero = value < w_length;
1305                value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
1306                if (value < 0) {
1307                        debug("ep_queue --> %d\n", value);
1308                        req->status = 0;
1309                        composite_setup_complete(gadget->ep0, req);
1310                }
1311        }
1312
1313done:
1314        /* device either stalls (value < 0) or reports success */
1315        return value;
1316}
1317
1318static void composite_disconnect(struct usb_gadget *gadget)
1319{
1320        struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1321
1322        if (cdev->config)
1323                reset_config(cdev);
1324        if (composite->disconnect)
1325                composite->disconnect(cdev);
1326}
1327
1328static void composite_unbind(struct usb_gadget *gadget)
1329{
1330        struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1331        struct usb_configuration        *c;
1332        struct usb_function             *f;
1333
1334        /*
1335         * composite_disconnect() must already have been called
1336         * by the underlying peripheral controller driver!
1337         * so there's no i/o concurrency that could affect the
1338         * state protected by cdev->lock.
1339         */
1340#ifdef __UBOOT__
1341        assert_noisy(!cdev->config);
1342#else
1343        BUG_ON(cdev->config);
1344#endif
1345
1346        while (!list_empty(&cdev->configs)) {
1347                c = list_first_entry(&cdev->configs,
1348                                struct usb_configuration, list);
1349                while (!list_empty(&c->functions)) {
1350                        f = list_first_entry(&c->functions,
1351                                        struct usb_function, list);
1352                        list_del(&f->list);
1353                        if (f->unbind) {
1354                                debug("unbind function '%s'/%p\n",
1355                                                f->name, f);
1356                                f->unbind(c, f);
1357                        }
1358                }
1359                list_del(&c->list);
1360                if (c->unbind) {
1361                        debug("unbind config '%s'/%p\n", c->label, c);
1362                        c->unbind(c);
1363                }
1364                free(c);
1365        }
1366        if (composite->unbind)
1367                composite->unbind(cdev);
1368
1369        if (cdev->req) {
1370                kfree(cdev->req->buf);
1371                usb_ep_free_request(gadget->ep0, cdev->req);
1372        }
1373        kfree(cdev);
1374        set_gadget_data(gadget, NULL);
1375
1376        composite = NULL;
1377}
1378
1379static int composite_bind(struct usb_gadget *gadget)
1380{
1381        int                             status = -ENOMEM;
1382        struct usb_composite_dev        *cdev;
1383
1384        cdev = calloc(sizeof *cdev, 1);
1385        if (!cdev)
1386                return status;
1387
1388        cdev->gadget = gadget;
1389        set_gadget_data(gadget, cdev);
1390        INIT_LIST_HEAD(&cdev->configs);
1391
1392        /* preallocate control response and buffer */
1393        cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1394        if (!cdev->req)
1395                goto fail;
1396        cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
1397        if (!cdev->req->buf)
1398                goto fail;
1399        cdev->req->complete = composite_setup_complete;
1400        gadget->ep0->driver_data = cdev;
1401
1402        cdev->bufsiz = USB_BUFSIZ;
1403        cdev->driver = composite;
1404
1405        usb_gadget_set_selfpowered(gadget);
1406        usb_ep_autoconfig_reset(cdev->gadget);
1407
1408        status = composite->bind(cdev);
1409        if (status < 0)
1410                goto fail;
1411
1412        memcpy(&cdev->desc, composite->dev,
1413               sizeof(struct usb_device_descriptor));
1414        cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1415
1416        if (cdev->use_os_string) {
1417                /* TODO: Do we want to pass this via platform? */
1418                cdev->b_vendor_code = 0x40;
1419
1420                /* Microsoft OS String Descriptor */
1421                utf8_to_utf16le(qw_sign_buf, (__le16 *)cdev->qw_sign,
1422                                OS_STRING_QW_SIGN_LEN / 2);
1423        }
1424
1425        debug("%s: ready\n", composite->name);
1426        return 0;
1427
1428fail:
1429        composite_unbind(gadget);
1430        return status;
1431}
1432
1433static void
1434composite_suspend(struct usb_gadget *gadget)
1435{
1436        struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1437        struct usb_function             *f;
1438
1439        debug("%s: suspend\n", __func__);
1440        if (cdev->config) {
1441                list_for_each_entry(f, &cdev->config->functions, list) {
1442                        if (f->suspend)
1443                                f->suspend(f);
1444                }
1445        }
1446        if (composite->suspend)
1447                composite->suspend(cdev);
1448
1449        cdev->suspended = 1;
1450}
1451
1452static void
1453composite_resume(struct usb_gadget *gadget)
1454{
1455        struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1456        struct usb_function             *f;
1457
1458        debug("%s: resume\n", __func__);
1459        if (composite->resume)
1460                composite->resume(cdev);
1461        if (cdev->config) {
1462                list_for_each_entry(f, &cdev->config->functions, list) {
1463                        if (f->resume)
1464                                f->resume(f);
1465                }
1466        }
1467
1468        cdev->suspended = 0;
1469}
1470
1471static struct usb_gadget_driver composite_driver = {
1472        .speed          = USB_SPEED_SUPER,
1473
1474        .bind           = composite_bind,
1475        .unbind         = composite_unbind,
1476
1477        .setup          = composite_setup,
1478        .reset          = composite_disconnect,
1479        .disconnect     = composite_disconnect,
1480
1481        .suspend        = composite_suspend,
1482        .resume         = composite_resume,
1483};
1484
1485/**
1486 * usb_composite_register() - register a composite driver
1487 * @driver: the driver to register
1488 * Context: single threaded during gadget setup
1489 *
1490 * This function is used to register drivers using the composite driver
1491 * framework.  The return value is zero, or a negative errno value.
1492 * Those values normally come from the driver's @bind method, which does
1493 * all the work of setting up the driver to match the hardware.
1494 *
1495 * On successful return, the gadget is ready to respond to requests from
1496 * the host, unless one of its components invokes usb_gadget_disconnect()
1497 * while it was binding.  That would usually be done in order to wait for
1498 * some userspace participation.
1499 */
1500int usb_composite_register(struct usb_composite_driver *driver)
1501{
1502        int res;
1503
1504        if (!driver || !driver->dev || !driver->bind || composite)
1505                return -EINVAL;
1506
1507        if (!driver->name)
1508                driver->name = "composite";
1509        composite = driver;
1510
1511        res = usb_gadget_register_driver(&composite_driver);
1512        if (res != 0)
1513                composite = NULL;
1514
1515        return res;
1516}
1517
1518/**
1519 * usb_composite_unregister() - unregister a composite driver
1520 * @driver: the driver to unregister
1521 *
1522 * This function is used to unregister drivers using the composite
1523 * driver framework.
1524 */
1525void usb_composite_unregister(struct usb_composite_driver *driver)
1526{
1527        if (composite != driver)
1528                return;
1529        usb_gadget_unregister_driver(&composite_driver);
1530        composite = NULL;
1531}
1532