OSDN Git Service

USB: gadget: composite: Move switch_set_state calls to a work queue
[android-x86/kernel.git] / drivers / usb / gadget / composite.c
1 /*
2  * composite.c - infrastructure for Composite USB Gadgets
3  *
4  * Copyright (C) 2006-2008 David Brownell
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /* #define VERBOSE_DEBUG */
22
23 #include <linux/kallsyms.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/device.h>
27 #include <linux/utsname.h>
28
29 #include <linux/usb/composite.h>
30
31
32 /*
33  * The code in this file is utility code, used to build a gadget driver
34  * from one or more "function" drivers, one or more "configuration"
35  * objects, and a "usb_composite_driver" by gluing them together along
36  * with the relevant device-wide data.
37  */
38
39 /* big enough to hold our biggest descriptor */
40 #define USB_BUFSIZ      1024
41
42 static struct usb_composite_driver *composite;
43 static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
44
45 /* Some systems will need runtime overrides for the  product identifers
46  * published in the device descriptor, either numbers or strings or both.
47  * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
48  */
49
50 static ushort idVendor;
51 module_param(idVendor, ushort, 0);
52 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
53
54 static ushort idProduct;
55 module_param(idProduct, ushort, 0);
56 MODULE_PARM_DESC(idProduct, "USB Product ID");
57
58 static ushort bcdDevice;
59 module_param(bcdDevice, ushort, 0);
60 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
61
62 static char *iManufacturer;
63 module_param(iManufacturer, charp, 0);
64 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
65
66 static char *iProduct;
67 module_param(iProduct, charp, 0);
68 MODULE_PARM_DESC(iProduct, "USB Product string");
69
70 static char *iSerialNumber;
71 module_param(iSerialNumber, charp, 0);
72 MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
73
74 static char composite_manufacturer[50];
75
76 /*-------------------------------------------------------------------------*/
77
78 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
79                 char *buf)
80 {
81         struct usb_function *f = dev_get_drvdata(dev);
82         return sprintf(buf, "%d\n", !f->disabled);
83 }
84
85 static ssize_t enable_store(
86                 struct device *dev, struct device_attribute *attr,
87                 const char *buf, size_t size)
88 {
89         struct usb_function *f = dev_get_drvdata(dev);
90         struct usb_composite_driver     *driver = f->config->cdev->driver;
91         int value;
92
93         sscanf(buf, "%d", &value);
94         if (driver->enable_function)
95                 driver->enable_function(f, value);
96         else
97                 usb_function_set_enabled(f, value);
98
99         return size;
100 }
101
102 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
103
104 void usb_function_set_enabled(struct usb_function *f, int enabled)
105 {
106         f->disabled = !enabled;
107         kobject_uevent(&f->dev->kobj, KOBJ_CHANGE);
108 }
109
110 /**
111  * usb_add_function() - add a function to a configuration
112  * @config: the configuration
113  * @function: the function being added
114  * Context: single threaded during gadget setup
115  *
116  * After initialization, each configuration must have one or more
117  * functions added to it.  Adding a function involves calling its @bind()
118  * method to allocate resources such as interface and string identifiers
119  * and endpoints.
120  *
121  * This function returns the value of the function's bind(), which is
122  * zero for success else a negative errno value.
123  */
124 int usb_add_function(struct usb_configuration *config,
125                 struct usb_function *function)
126 {
127         struct usb_composite_dev        *cdev = config->cdev;
128         int     value = -EINVAL;
129         int index;
130
131         DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
132                         function->name, function,
133                         config->label, config);
134
135         if (!function->set_alt || !function->disable)
136                 goto done;
137
138         index = atomic_inc_return(&cdev->driver->function_count);
139         function->dev = device_create(cdev->driver->class, NULL,
140                 MKDEV(0, index), NULL, function->name);
141         if (IS_ERR(function->dev))
142                 return PTR_ERR(function->dev);
143
144         value = device_create_file(function->dev, &dev_attr_enable);
145         if (value < 0) {
146                 device_destroy(cdev->driver->class, MKDEV(0, index));
147                 return value;
148         }
149         dev_set_drvdata(function->dev, function);
150
151         function->config = config;
152         list_add_tail(&function->list, &config->functions);
153
154         /* REVISIT *require* function->bind? */
155         if (function->bind) {
156                 value = function->bind(config, function);
157                 if (value < 0) {
158                         list_del(&function->list);
159                         function->config = NULL;
160                 }
161         } else
162                 value = 0;
163
164         /* We allow configurations that don't work at both speeds.
165          * If we run into a lowspeed Linux system, treat it the same
166          * as full speed ... it's the function drivers that will need
167          * to avoid bulk and ISO transfers.
168          */
169         if (!config->fullspeed && function->descriptors)
170                 config->fullspeed = true;
171         if (!config->highspeed && function->hs_descriptors)
172                 config->highspeed = true;
173
174 done:
175         if (value)
176                 DBG(cdev, "adding '%s'/%p --> %d\n",
177                                 function->name, function, value);
178         return value;
179 }
180
181 /**
182  * usb_function_deactivate - prevent function and gadget enumeration
183  * @function: the function that isn't yet ready to respond
184  *
185  * Blocks response of the gadget driver to host enumeration by
186  * preventing the data line pullup from being activated.  This is
187  * normally called during @bind() processing to change from the
188  * initial "ready to respond" state, or when a required resource
189  * becomes available.
190  *
191  * For example, drivers that serve as a passthrough to a userspace
192  * daemon can block enumeration unless that daemon (such as an OBEX,
193  * MTP, or print server) is ready to handle host requests.
194  *
195  * Not all systems support software control of their USB peripheral
196  * data pullups.
197  *
198  * Returns zero on success, else negative errno.
199  */
200 int usb_function_deactivate(struct usb_function *function)
201 {
202         struct usb_composite_dev        *cdev = function->config->cdev;
203         unsigned long                   flags;
204         int                             status = 0;
205
206         spin_lock_irqsave(&cdev->lock, flags);
207
208         if (cdev->deactivations == 0)
209                 status = usb_gadget_disconnect(cdev->gadget);
210         if (status == 0)
211                 cdev->deactivations++;
212
213         spin_unlock_irqrestore(&cdev->lock, flags);
214         return status;
215 }
216
217 /**
218  * usb_function_activate - allow function and gadget enumeration
219  * @function: function on which usb_function_activate() was called
220  *
221  * Reverses effect of usb_function_deactivate().  If no more functions
222  * are delaying their activation, the gadget driver will respond to
223  * host enumeration procedures.
224  *
225  * Returns zero on success, else negative errno.
226  */
227 int usb_function_activate(struct usb_function *function)
228 {
229         struct usb_composite_dev        *cdev = function->config->cdev;
230         int                             status = 0;
231
232         spin_lock(&cdev->lock);
233
234         if (WARN_ON(cdev->deactivations == 0))
235                 status = -EINVAL;
236         else {
237                 cdev->deactivations--;
238                 if (cdev->deactivations == 0)
239                         status = usb_gadget_connect(cdev->gadget);
240         }
241
242         spin_unlock(&cdev->lock);
243         return status;
244 }
245
246 /**
247  * usb_interface_id() - allocate an unused interface ID
248  * @config: configuration associated with the interface
249  * @function: function handling the interface
250  * Context: single threaded during gadget setup
251  *
252  * usb_interface_id() is called from usb_function.bind() callbacks to
253  * allocate new interface IDs.  The function driver will then store that
254  * ID in interface, association, CDC union, and other descriptors.  It
255  * will also handle any control requests targetted at that interface,
256  * particularly changing its altsetting via set_alt().  There may
257  * also be class-specific or vendor-specific requests to handle.
258  *
259  * All interface identifier should be allocated using this routine, to
260  * ensure that for example different functions don't wrongly assign
261  * different meanings to the same identifier.  Note that since interface
262  * identifers are configuration-specific, functions used in more than
263  * one configuration (or more than once in a given configuration) need
264  * multiple versions of the relevant descriptors.
265  *
266  * Returns the interface ID which was allocated; or -ENODEV if no
267  * more interface IDs can be allocated.
268  */
269 int usb_interface_id(struct usb_configuration *config,
270                 struct usb_function *function)
271 {
272         unsigned id = config->next_interface_id;
273
274         if (id < MAX_CONFIG_INTERFACES) {
275                 config->interface[id] = function;
276                 config->next_interface_id = id + 1;
277                 return id;
278         }
279         return -ENODEV;
280 }
281
282 static int config_buf(struct usb_configuration *config,
283                 enum usb_device_speed speed, void *buf, u8 type)
284 {
285         struct usb_config_descriptor    *c = buf;
286         struct usb_interface_descriptor *intf;
287         void                            *next = buf + USB_DT_CONFIG_SIZE;
288         int                             len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
289         struct usb_function             *f;
290         int                             status;
291         int                             interfaceCount = 0;
292         u8 *dest;
293
294         /* write the config descriptor */
295         c = buf;
296         c->bLength = USB_DT_CONFIG_SIZE;
297         c->bDescriptorType = type;
298         /* wTotalLength and bNumInterfaces are written later */
299         c->bConfigurationValue = config->bConfigurationValue;
300         c->iConfiguration = config->iConfiguration;
301         c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
302         c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
303
304         /* There may be e.g. OTG descriptors */
305         if (config->descriptors) {
306                 status = usb_descriptor_fillbuf(next, len,
307                                 config->descriptors);
308                 if (status < 0)
309                         return status;
310                 len -= status;
311                 next += status;
312         }
313
314         /* add each function's descriptors */
315         list_for_each_entry(f, &config->functions, list) {
316                 struct usb_descriptor_header **descriptors;
317                 struct usb_descriptor_header *descriptor;
318
319                 if (speed == USB_SPEED_HIGH)
320                         descriptors = f->hs_descriptors;
321                 else
322                         descriptors = f->descriptors;
323                 if (f->disabled || !descriptors || descriptors[0] == NULL)
324                         continue;
325                 status = usb_descriptor_fillbuf(next, len,
326                         (const struct usb_descriptor_header **) descriptors);
327                 if (status < 0)
328                         return status;
329
330                 /* set interface numbers dynamically */
331                 dest = next;
332                 while ((descriptor = *descriptors++) != NULL) {
333                         intf = (struct usb_interface_descriptor *)dest;
334                         if (intf->bDescriptorType == USB_DT_INTERFACE) {
335                                 /* don't increment bInterfaceNumber for alternate settings */
336                                 if (intf->bAlternateSetting == 0)
337                                         intf->bInterfaceNumber = interfaceCount++;
338                                 else
339                                         intf->bInterfaceNumber = interfaceCount - 1;
340                         }
341                         dest += intf->bLength;
342                 }
343
344                 len -= status;
345                 next += status;
346         }
347
348         len = next - buf;
349         c->wTotalLength = cpu_to_le16(len);
350         c->bNumInterfaces = interfaceCount;
351         return len;
352 }
353
354 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
355 {
356         struct usb_gadget               *gadget = cdev->gadget;
357         struct usb_configuration        *c;
358         u8                              type = w_value >> 8;
359         enum usb_device_speed           speed = USB_SPEED_UNKNOWN;
360
361         if (gadget_is_dualspeed(gadget)) {
362                 int                     hs = 0;
363
364                 if (gadget->speed == USB_SPEED_HIGH)
365                         hs = 1;
366                 if (type == USB_DT_OTHER_SPEED_CONFIG)
367                         hs = !hs;
368                 if (hs)
369                         speed = USB_SPEED_HIGH;
370
371         }
372
373         /* This is a lookup by config *INDEX* */
374         w_value &= 0xff;
375         list_for_each_entry(c, &cdev->configs, list) {
376                 /* ignore configs that won't work at this speed */
377                 if (speed == USB_SPEED_HIGH) {
378                         if (!c->highspeed)
379                                 continue;
380                 } else {
381                         if (!c->fullspeed)
382                                 continue;
383                 }
384                 if (w_value == 0)
385                         return config_buf(c, speed, cdev->req->buf, type);
386                 w_value--;
387         }
388         return -EINVAL;
389 }
390
391 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
392 {
393         struct usb_gadget               *gadget = cdev->gadget;
394         struct usb_configuration        *c;
395         unsigned                        count = 0;
396         int                             hs = 0;
397
398         if (gadget_is_dualspeed(gadget)) {
399                 if (gadget->speed == USB_SPEED_HIGH)
400                         hs = 1;
401                 if (type == USB_DT_DEVICE_QUALIFIER)
402                         hs = !hs;
403         }
404         list_for_each_entry(c, &cdev->configs, list) {
405                 /* ignore configs that won't work at this speed */
406                 if (hs) {
407                         if (!c->highspeed)
408                                 continue;
409                 } else {
410                         if (!c->fullspeed)
411                                 continue;
412                 }
413                 count++;
414         }
415         return count;
416 }
417
418 static void device_qual(struct usb_composite_dev *cdev)
419 {
420         struct usb_qualifier_descriptor *qual = cdev->req->buf;
421
422         qual->bLength = sizeof(*qual);
423         qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
424         /* POLICY: same bcdUSB and device type info at both speeds */
425         qual->bcdUSB = cdev->desc.bcdUSB;
426         qual->bDeviceClass = cdev->desc.bDeviceClass;
427         qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
428         qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
429         /* ASSUME same EP0 fifo size at both speeds */
430         qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
431         qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
432         qual->bRESERVED = 0;
433 }
434
435 /*-------------------------------------------------------------------------*/
436
437 static void reset_config(struct usb_composite_dev *cdev)
438 {
439         struct usb_function             *f;
440
441         DBG(cdev, "reset config\n");
442
443         list_for_each_entry(f, &cdev->config->functions, list) {
444                 if (f->disable)
445                         f->disable(f);
446
447                 bitmap_zero(f->endpoints, 32);
448         }
449         cdev->config = NULL;
450 }
451
452 static int set_config(struct usb_composite_dev *cdev,
453                 const struct usb_ctrlrequest *ctrl, unsigned number)
454 {
455         struct usb_gadget       *gadget = cdev->gadget;
456         struct usb_configuration *c = NULL;
457         int                     result = -EINVAL;
458         unsigned                power = gadget_is_otg(gadget) ? 8 : 100;
459         int                     tmp;
460
461         if (cdev->config)
462                 reset_config(cdev);
463
464         if (number) {
465                 list_for_each_entry(c, &cdev->configs, list) {
466                         if (c->bConfigurationValue == number) {
467                                 result = 0;
468                                 break;
469                         }
470                 }
471                 if (result < 0)
472                         goto done;
473         } else
474                 result = 0;
475
476         INFO(cdev, "%s speed config #%d: %s\n",
477                 ({ char *speed;
478                 switch (gadget->speed) {
479                 case USB_SPEED_LOW:     speed = "low"; break;
480                 case USB_SPEED_FULL:    speed = "full"; break;
481                 case USB_SPEED_HIGH:    speed = "high"; break;
482                 default:                speed = "?"; break;
483                 } ; speed; }), number, c ? c->label : "unconfigured");
484
485         if (!c)
486                 goto done;
487
488         cdev->config = c;
489
490         /* Initialize all interfaces by setting them to altsetting zero. */
491         for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
492                 struct usb_function     *f = c->interface[tmp];
493                 struct usb_descriptor_header **descriptors;
494
495                 if (!f)
496                         break;
497                 if (f->disabled)
498                         continue;
499
500                 /*
501                  * Record which endpoints are used by the function. This is used
502                  * to dispatch control requests targeted at that endpoint to the
503                  * function's setup callback instead of the current
504                  * configuration's setup callback.
505                  */
506                 if (gadget->speed == USB_SPEED_HIGH)
507                         descriptors = f->hs_descriptors;
508                 else
509                         descriptors = f->descriptors;
510
511                 for (; *descriptors; ++descriptors) {
512                         struct usb_endpoint_descriptor *ep;
513                         int addr;
514
515                         if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
516                                 continue;
517
518                         ep = (struct usb_endpoint_descriptor *)*descriptors;
519                         addr = ((ep->bEndpointAddress & 0x80) >> 3)
520                              |  (ep->bEndpointAddress & 0x0f);
521                         set_bit(addr, f->endpoints);
522                 }
523
524                 result = f->set_alt(f, tmp, 0);
525                 if (result < 0) {
526                         DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
527                                         tmp, f->name, f, result);
528
529                         reset_config(cdev);
530                         goto done;
531                 }
532         }
533
534         /* when we return, be sure our power usage is valid */
535         power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
536 done:
537         usb_gadget_vbus_draw(gadget, power);
538
539         schedule_work(&cdev->switch_work);
540         return result;
541 }
542
543 /**
544  * usb_add_config() - add a configuration to a device.
545  * @cdev: wraps the USB gadget
546  * @config: the configuration, with bConfigurationValue assigned
547  * @bind: the configuration's bind function
548  * Context: single threaded during gadget setup
549  *
550  * One of the main tasks of a composite @bind() routine is to
551  * add each of the configurations it supports, using this routine.
552  *
553  * This function returns the value of the configuration's @bind(), which
554  * is zero for success else a negative errno value.  Binding configurations
555  * assigns global resources including string IDs, and per-configuration
556  * resources such as interface IDs and endpoints.
557  */
558 int usb_add_config(struct usb_composite_dev *cdev,
559                 struct usb_configuration *config,
560                 int (*bind)(struct usb_configuration *))
561 {
562         int                             status = -EINVAL;
563         struct usb_configuration        *c;
564
565         DBG(cdev, "adding config #%u '%s'/%p\n",
566                         config->bConfigurationValue,
567                         config->label, config);
568
569         if (!config->bConfigurationValue || !bind)
570                 goto done;
571
572         /* Prevent duplicate configuration identifiers */
573         list_for_each_entry(c, &cdev->configs, list) {
574                 if (c->bConfigurationValue == config->bConfigurationValue) {
575                         status = -EBUSY;
576                         goto done;
577                 }
578         }
579
580         config->cdev = cdev;
581         list_add_tail(&config->list, &cdev->configs);
582
583         INIT_LIST_HEAD(&config->functions);
584         config->next_interface_id = 0;
585
586         status = bind(config);
587         if (status < 0) {
588                 list_del(&config->list);
589                 config->cdev = NULL;
590         } else {
591                 unsigned        i;
592
593                 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
594                         config->bConfigurationValue, config,
595                         config->highspeed ? " high" : "",
596                         config->fullspeed
597                                 ? (gadget_is_dualspeed(cdev->gadget)
598                                         ? " full"
599                                         : " full/low")
600                                 : "");
601
602                 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
603                         struct usb_function     *f = config->interface[i];
604
605                         if (!f)
606                                 continue;
607                         DBG(cdev, "  interface %d = %s/%p\n",
608                                 i, f->name, f);
609                 }
610         }
611
612         /* set_alt(), or next bind(), sets up
613          * ep->driver_data as needed.
614          */
615         usb_ep_autoconfig_reset(cdev->gadget);
616
617 done:
618         if (status)
619                 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
620                                 config->bConfigurationValue, status);
621         return status;
622 }
623
624 /*-------------------------------------------------------------------------*/
625
626 /* We support strings in multiple languages ... string descriptor zero
627  * says which languages are supported.  The typical case will be that
628  * only one language (probably English) is used, with I18N handled on
629  * the host side.
630  */
631
632 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
633 {
634         const struct usb_gadget_strings *s;
635         u16                             language;
636         __le16                          *tmp;
637
638         while (*sp) {
639                 s = *sp;
640                 language = cpu_to_le16(s->language);
641                 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
642                         if (*tmp == language)
643                                 goto repeat;
644                 }
645                 *tmp++ = language;
646 repeat:
647                 sp++;
648         }
649 }
650
651 static int lookup_string(
652         struct usb_gadget_strings       **sp,
653         void                            *buf,
654         u16                             language,
655         int                             id
656 )
657 {
658         struct usb_gadget_strings       *s;
659         int                             value;
660
661         while (*sp) {
662                 s = *sp++;
663                 if (s->language != language)
664                         continue;
665                 value = usb_gadget_get_string(s, id, buf);
666                 if (value > 0)
667                         return value;
668         }
669         return -EINVAL;
670 }
671
672 static int get_string(struct usb_composite_dev *cdev,
673                 void *buf, u16 language, int id)
674 {
675         struct usb_configuration        *c;
676         struct usb_function             *f;
677         int                             len;
678         const char                      *str;
679
680         /* Yes, not only is USB's I18N support probably more than most
681          * folk will ever care about ... also, it's all supported here.
682          * (Except for UTF8 support for Unicode's "Astral Planes".)
683          */
684
685         /* 0 == report all available language codes */
686         if (id == 0) {
687                 struct usb_string_descriptor    *s = buf;
688                 struct usb_gadget_strings       **sp;
689
690                 memset(s, 0, 256);
691                 s->bDescriptorType = USB_DT_STRING;
692
693                 sp = composite->strings;
694                 if (sp)
695                         collect_langs(sp, s->wData);
696
697                 list_for_each_entry(c, &cdev->configs, list) {
698                         sp = c->strings;
699                         if (sp)
700                                 collect_langs(sp, s->wData);
701
702                         list_for_each_entry(f, &c->functions, list) {
703                                 sp = f->strings;
704                                 if (sp)
705                                         collect_langs(sp, s->wData);
706                         }
707                 }
708
709                 for (len = 0; len <= 126 && s->wData[len]; len++)
710                         continue;
711                 if (!len)
712                         return -EINVAL;
713
714                 s->bLength = 2 * (len + 1);
715                 return s->bLength;
716         }
717
718         /* Otherwise, look up and return a specified string.  First
719          * check if the string has not been overridden.
720          */
721         if (cdev->manufacturer_override == id)
722                 str = iManufacturer ?: composite->iManufacturer ?:
723                         composite_manufacturer;
724         else if (cdev->product_override == id)
725                 str = iProduct ?: composite->iProduct;
726         else if (cdev->serial_override == id)
727                 str = iSerialNumber;
728         else
729                 str = NULL;
730         if (str) {
731                 struct usb_gadget_strings strings = {
732                         .language = language,
733                         .strings  = &(struct usb_string) { 0xff, str }
734                 };
735                 return usb_gadget_get_string(&strings, 0xff, buf);
736         }
737
738         /* String IDs are device-scoped, so we look up each string
739          * table we're told about.  These lookups are infrequent;
740          * simpler-is-better here.
741          */
742         if (composite->strings) {
743                 len = lookup_string(composite->strings, buf, language, id);
744                 if (len > 0)
745                         return len;
746         }
747         list_for_each_entry(c, &cdev->configs, list) {
748                 if (c->strings) {
749                         len = lookup_string(c->strings, buf, language, id);
750                         if (len > 0)
751                                 return len;
752                 }
753                 list_for_each_entry(f, &c->functions, list) {
754                         if (!f->strings)
755                                 continue;
756                         len = lookup_string(f->strings, buf, language, id);
757                         if (len > 0)
758                                 return len;
759                 }
760         }
761         return -EINVAL;
762 }
763
764 /**
765  * usb_string_id() - allocate an unused string ID
766  * @cdev: the device whose string descriptor IDs are being allocated
767  * Context: single threaded during gadget setup
768  *
769  * @usb_string_id() is called from bind() callbacks to allocate
770  * string IDs.  Drivers for functions, configurations, or gadgets will
771  * then store that ID in the appropriate descriptors and string table.
772  *
773  * All string identifier should be allocated using this,
774  * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
775  * that for example different functions don't wrongly assign different
776  * meanings to the same identifier.
777  */
778 int usb_string_id(struct usb_composite_dev *cdev)
779 {
780         if (cdev->next_string_id < 254) {
781                 /* string id 0 is reserved by USB spec for list of
782                  * supported languages */
783                 /* 255 reserved as well? -- mina86 */
784                 cdev->next_string_id++;
785                 return cdev->next_string_id;
786         }
787         return -ENODEV;
788 }
789
790 /**
791  * usb_string_ids() - allocate unused string IDs in batch
792  * @cdev: the device whose string descriptor IDs are being allocated
793  * @str: an array of usb_string objects to assign numbers to
794  * Context: single threaded during gadget setup
795  *
796  * @usb_string_ids() is called from bind() callbacks to allocate
797  * string IDs.  Drivers for functions, configurations, or gadgets will
798  * then copy IDs from the string table to the appropriate descriptors
799  * and string table for other languages.
800  *
801  * All string identifier should be allocated using this,
802  * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
803  * example different functions don't wrongly assign different meanings
804  * to the same identifier.
805  */
806 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
807 {
808         int next = cdev->next_string_id;
809
810         for (; str->s; ++str) {
811                 if (unlikely(next >= 254))
812                         return -ENODEV;
813                 str->id = ++next;
814         }
815
816         cdev->next_string_id = next;
817
818         return 0;
819 }
820
821 /**
822  * usb_string_ids_n() - allocate unused string IDs in batch
823  * @c: the device whose string descriptor IDs are being allocated
824  * @n: number of string IDs to allocate
825  * Context: single threaded during gadget setup
826  *
827  * Returns the first requested ID.  This ID and next @n-1 IDs are now
828  * valid IDs.  At least provided that @n is non-zero because if it
829  * is, returns last requested ID which is now very useful information.
830  *
831  * @usb_string_ids_n() is called from bind() callbacks to allocate
832  * string IDs.  Drivers for functions, configurations, or gadgets will
833  * then store that ID in the appropriate descriptors and string table.
834  *
835  * All string identifier should be allocated using this,
836  * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
837  * example different functions don't wrongly assign different meanings
838  * to the same identifier.
839  */
840 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
841 {
842         unsigned next = c->next_string_id;
843         if (unlikely(n > 254 || (unsigned)next + n > 254))
844                 return -ENODEV;
845         c->next_string_id += n;
846         return next + 1;
847 }
848
849
850 /*-------------------------------------------------------------------------*/
851
852 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
853 {
854         if (req->status || req->actual != req->length)
855                 DBG((struct usb_composite_dev *) ep->driver_data,
856                                 "setup complete --> %d, %d/%d\n",
857                                 req->status, req->actual, req->length);
858 }
859
860 /*
861  * The setup() callback implements all the ep0 functionality that's
862  * not handled lower down, in hardware or the hardware driver(like
863  * device and endpoint feature flags, and their status).  It's all
864  * housekeeping for the gadget function we're implementing.  Most of
865  * the work is in config and function specific setup.
866  */
867 static int
868 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
869 {
870         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
871         struct usb_request              *req = cdev->req;
872         int                             value = -EOPNOTSUPP;
873         u16                             w_index = le16_to_cpu(ctrl->wIndex);
874         u8                              intf = w_index & 0xFF;
875         u16                             w_value = le16_to_cpu(ctrl->wValue);
876         u16                             w_length = le16_to_cpu(ctrl->wLength);
877         struct usb_function             *f = NULL;
878         u8                              endp;
879
880         /* partial re-init of the response message; the function or the
881          * gadget might need to intercept e.g. a control-OUT completion
882          * when we delegate to it.
883          */
884         req->zero = 0;
885         req->complete = composite_setup_complete;
886         req->length = USB_BUFSIZ;
887         gadget->ep0->driver_data = cdev;
888
889         switch (ctrl->bRequest) {
890
891         /* we handle all standard USB descriptors */
892         case USB_REQ_GET_DESCRIPTOR:
893                 if (ctrl->bRequestType != USB_DIR_IN)
894                         goto unknown;
895                 switch (w_value >> 8) {
896
897                 case USB_DT_DEVICE:
898                         cdev->desc.bNumConfigurations =
899                                 count_configs(cdev, USB_DT_DEVICE);
900                         value = min(w_length, (u16) sizeof cdev->desc);
901                         memcpy(req->buf, &cdev->desc, value);
902                         break;
903                 case USB_DT_DEVICE_QUALIFIER:
904                         if (!gadget_is_dualspeed(gadget))
905                                 break;
906                         device_qual(cdev);
907                         value = min_t(int, w_length,
908                                 sizeof(struct usb_qualifier_descriptor));
909                         break;
910                 case USB_DT_OTHER_SPEED_CONFIG:
911                         if (!gadget_is_dualspeed(gadget))
912                                 break;
913                         /* FALLTHROUGH */
914                 case USB_DT_CONFIG:
915                         value = config_desc(cdev, w_value);
916                         if (value >= 0)
917                                 value = min(w_length, (u16) value);
918                         break;
919                 case USB_DT_STRING:
920                         value = get_string(cdev, req->buf,
921                                         w_index, w_value & 0xff);
922
923                         /* Allow functions to handle USB_DT_STRING.
924                          * This is required for MTP.
925                          */
926                         if (value < 0) {
927                                 struct usb_configuration        *cfg;
928                                 list_for_each_entry(cfg, &cdev->configs, list) {
929                                         if (cfg && cfg->setup) {
930                                                 value = cfg->setup(cfg, ctrl);
931                                                 if (value >= 0)
932                                                         break;
933                                         }
934                                 }
935                         }
936
937                         if (value >= 0)
938                                 value = min(w_length, (u16) value);
939                         break;
940                 }
941                 break;
942
943         /* any number of configs can work */
944         case USB_REQ_SET_CONFIGURATION:
945                 if (ctrl->bRequestType != 0)
946                         goto unknown;
947                 if (gadget_is_otg(gadget)) {
948                         if (gadget->a_hnp_support)
949                                 DBG(cdev, "HNP available\n");
950                         else if (gadget->a_alt_hnp_support)
951                                 DBG(cdev, "HNP on another port\n");
952                         else
953                                 VDBG(cdev, "HNP inactive\n");
954                 }
955                 spin_lock(&cdev->lock);
956                 value = set_config(cdev, ctrl, w_value);
957                 spin_unlock(&cdev->lock);
958                 break;
959         case USB_REQ_GET_CONFIGURATION:
960                 if (ctrl->bRequestType != USB_DIR_IN)
961                         goto unknown;
962                 if (cdev->config) {
963                         *(u8 *)req->buf = cdev->config->bConfigurationValue;
964                         value = min(w_length, (u16) 1);
965                 } else {
966                         *(u8 *)req->buf = 0;
967                 }
968                 break;
969
970         /* function drivers must handle get/set altsetting; if there's
971          * no get() method, we know only altsetting zero works.
972          */
973         case USB_REQ_SET_INTERFACE:
974                 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
975                         goto unknown;
976                 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
977                         break;
978                 f = cdev->config->interface[intf];
979                 if (!f)
980                         break;
981                 if (w_value && !f->set_alt)
982                         break;
983                 value = f->set_alt(f, w_index, w_value);
984                 break;
985         case USB_REQ_GET_INTERFACE:
986                 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
987                         goto unknown;
988                 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
989                         break;
990                 f = cdev->config->interface[intf];
991                 if (!f)
992                         break;
993                 /* lots of interfaces only need altsetting zero... */
994                 value = f->get_alt ? f->get_alt(f, w_index) : 0;
995                 if (value < 0)
996                         break;
997                 *((u8 *)req->buf) = value;
998                 value = min(w_length, (u16) 1);
999                 break;
1000         default:
1001 unknown:
1002                 VDBG(cdev,
1003                         "non-core control req%02x.%02x v%04x i%04x l%d\n",
1004                         ctrl->bRequestType, ctrl->bRequest,
1005                         w_value, w_index, w_length);
1006
1007                 /* functions always handle their interfaces and endpoints...
1008                  * punt other recipients (other, WUSB, ...) to the current
1009                  * configuration code.
1010                  *
1011                  * REVISIT it could make sense to let the composite device
1012                  * take such requests too, if that's ever needed:  to work
1013                  * in config 0, etc.
1014                  */
1015                 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1016                 case USB_RECIP_INTERFACE:
1017                         if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1018                                 break;
1019                         f = cdev->config->interface[intf];
1020                         break;
1021
1022                 case USB_RECIP_ENDPOINT:
1023                         endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1024                         list_for_each_entry(f, &cdev->config->functions, list) {
1025                                 if (test_bit(endp, f->endpoints))
1026                                         break;
1027                         }
1028                         if (&f->list == &cdev->config->functions)
1029                                 f = NULL;
1030                         break;
1031                 }
1032
1033                 if (f && f->setup)
1034                         value = f->setup(f, ctrl);
1035                 else {
1036                         struct usb_configuration        *c;
1037
1038                         c = cdev->config;
1039                         if (c && c->setup)
1040                                 value = c->setup(c, ctrl);
1041                 }
1042
1043                 /* If the vendor request is not processed (value < 0),
1044                  * call all device registered configure setup callbacks
1045                  * to process it.
1046                  * This is used to handle the following cases:
1047                  * - vendor request is for the device and arrives before
1048                  * setconfiguration.
1049                  * - Some devices are required to handle vendor request before
1050                  * setconfiguration such as MTP, USBNET.
1051                  */
1052
1053                 if (value < 0) {
1054                         struct usb_configuration        *cfg;
1055
1056                         list_for_each_entry(cfg, &cdev->configs, list) {
1057                         if (cfg && cfg->setup)
1058                                 value = cfg->setup(cfg, ctrl);
1059                         }
1060                 }
1061
1062                 goto done;
1063         }
1064
1065         /* respond with data transfer before status phase? */
1066         if (value >= 0) {
1067                 req->length = value;
1068                 req->zero = value < w_length;
1069                 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1070                 if (value < 0) {
1071                         DBG(cdev, "ep_queue --> %d\n", value);
1072                         req->status = 0;
1073                         composite_setup_complete(gadget->ep0, req);
1074                 }
1075         }
1076
1077 done:
1078         /* device either stalls (value < 0) or reports success */
1079         return value;
1080 }
1081
1082 static void composite_disconnect(struct usb_gadget *gadget)
1083 {
1084         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1085         unsigned long                   flags;
1086
1087         /* REVISIT:  should we have config and device level
1088          * disconnect callbacks?
1089          */
1090         spin_lock_irqsave(&cdev->lock, flags);
1091         if (cdev->config)
1092                 reset_config(cdev);
1093         if (composite->disconnect)
1094                 composite->disconnect(cdev);
1095         spin_unlock_irqrestore(&cdev->lock, flags);
1096
1097         schedule_work(&cdev->switch_work);
1098 }
1099
1100 /*-------------------------------------------------------------------------*/
1101
1102 static ssize_t composite_show_suspended(struct device *dev,
1103                                         struct device_attribute *attr,
1104                                         char *buf)
1105 {
1106         struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1107         struct usb_composite_dev *cdev = get_gadget_data(gadget);
1108
1109         return sprintf(buf, "%d\n", cdev->suspended);
1110 }
1111
1112 static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1113
1114 static void
1115 composite_unbind(struct usb_gadget *gadget)
1116 {
1117         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1118
1119         /* composite_disconnect() must already have been called
1120          * by the underlying peripheral controller driver!
1121          * so there's no i/o concurrency that could affect the
1122          * state protected by cdev->lock.
1123          */
1124         WARN_ON(cdev->config);
1125
1126         while (!list_empty(&cdev->configs)) {
1127                 struct usb_configuration        *c;
1128
1129                 c = list_first_entry(&cdev->configs,
1130                                 struct usb_configuration, list);
1131                 while (!list_empty(&c->functions)) {
1132                         struct usb_function             *f;
1133
1134                         f = list_first_entry(&c->functions,
1135                                         struct usb_function, list);
1136                         list_del(&f->list);
1137                         if (f->unbind) {
1138                                 DBG(cdev, "unbind function '%s'/%p\n",
1139                                                 f->name, f);
1140                                 f->unbind(c, f);
1141                                 /* may free memory for "f" */
1142                         }
1143                 }
1144                 list_del(&c->list);
1145                 if (c->unbind) {
1146                         DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1147                         c->unbind(c);
1148                         /* may free memory for "c" */
1149                 }
1150         }
1151         if (composite->unbind)
1152                 composite->unbind(cdev);
1153
1154         if (cdev->req) {
1155                 kfree(cdev->req->buf);
1156                 usb_ep_free_request(gadget->ep0, cdev->req);
1157         }
1158         switch_dev_unregister(&cdev->sdev);
1159         device_remove_file(&gadget->dev, &dev_attr_suspended);
1160         kfree(cdev);
1161         set_gadget_data(gadget, NULL);
1162         composite = NULL;
1163 }
1164
1165 static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
1166 {
1167         if (!*desc) {
1168                 int ret = usb_string_id(cdev);
1169                 if (unlikely(ret < 0))
1170                         WARNING(cdev, "failed to override string ID\n");
1171                 else
1172                         *desc = ret;
1173         }
1174
1175         return *desc;
1176 }
1177
1178 static void
1179 composite_switch_work(struct work_struct *data)
1180 {
1181         struct usb_composite_dev        *cdev =
1182                 container_of(data, struct usb_composite_dev, switch_work);
1183         struct usb_configuration *config = cdev->config;
1184
1185         if (config)
1186                 switch_set_state(&cdev->sdev, config->bConfigurationValue);
1187         else
1188                 switch_set_state(&cdev->sdev, 0);
1189 }
1190
1191 static int composite_bind(struct usb_gadget *gadget)
1192 {
1193         struct usb_composite_dev        *cdev;
1194         int                             status = -ENOMEM;
1195
1196         cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1197         if (!cdev)
1198                 return status;
1199
1200         spin_lock_init(&cdev->lock);
1201         cdev->gadget = gadget;
1202         set_gadget_data(gadget, cdev);
1203         INIT_LIST_HEAD(&cdev->configs);
1204
1205         /* preallocate control response and buffer */
1206         cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1207         if (!cdev->req)
1208                 goto fail;
1209         cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1210         if (!cdev->req->buf)
1211                 goto fail;
1212         cdev->req->complete = composite_setup_complete;
1213         gadget->ep0->driver_data = cdev;
1214
1215         cdev->bufsiz = USB_BUFSIZ;
1216         cdev->driver = composite;
1217
1218         /*
1219          * As per USB compliance update, a device that is actively drawing
1220          * more than 100mA from USB must report itself as bus-powered in
1221          * the GetStatus(DEVICE) call.
1222          */
1223         if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1224                 usb_gadget_set_selfpowered(gadget);
1225
1226         /* interface and string IDs start at zero via kzalloc.
1227          * we force endpoints to start unassigned; few controller
1228          * drivers will zero ep->driver_data.
1229          */
1230         usb_ep_autoconfig_reset(cdev->gadget);
1231
1232         /* composite gadget needs to assign strings for whole device (like
1233          * serial number), register function drivers, potentially update
1234          * power state and consumption, etc
1235          */
1236         status = composite_gadget_bind(cdev);
1237         if (status < 0)
1238                 goto fail;
1239
1240         cdev->sdev.name = "usb_configuration";
1241         status = switch_dev_register(&cdev->sdev);
1242         if (status < 0)
1243                 goto fail;
1244         INIT_WORK(&cdev->switch_work, composite_switch_work);
1245
1246         cdev->desc = *composite->dev;
1247         cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1248
1249         /* standardized runtime overrides for device ID data */
1250         if (idVendor)
1251                 cdev->desc.idVendor = cpu_to_le16(idVendor);
1252         if (idProduct)
1253                 cdev->desc.idProduct = cpu_to_le16(idProduct);
1254         if (bcdDevice)
1255                 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1256
1257         /* string overrides */
1258         if (iManufacturer || !cdev->desc.iManufacturer) {
1259                 if (!iManufacturer && !composite->iManufacturer &&
1260                     !*composite_manufacturer)
1261                         snprintf(composite_manufacturer,
1262                                  sizeof composite_manufacturer,
1263                                  "%s %s with %s",
1264                                  init_utsname()->sysname,
1265                                  init_utsname()->release,
1266                                  gadget->name);
1267
1268                 cdev->manufacturer_override =
1269                         override_id(cdev, &cdev->desc.iManufacturer);
1270         }
1271
1272         if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1273                 cdev->product_override =
1274                         override_id(cdev, &cdev->desc.iProduct);
1275
1276         if (iSerialNumber)
1277                 cdev->serial_override =
1278                         override_id(cdev, &cdev->desc.iSerialNumber);
1279
1280         /* has userspace failed to provide a serial number? */
1281         if (composite->needs_serial && !cdev->desc.iSerialNumber)
1282                 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1283
1284         /* finish up */
1285         status = device_create_file(&gadget->dev, &dev_attr_suspended);
1286         if (status)
1287                 goto fail;
1288
1289         INFO(cdev, "%s ready\n", composite->name);
1290         return 0;
1291
1292 fail:
1293         composite_unbind(gadget);
1294         return status;
1295 }
1296
1297 /*-------------------------------------------------------------------------*/
1298
1299 static void
1300 composite_suspend(struct usb_gadget *gadget)
1301 {
1302         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1303         struct usb_function             *f;
1304
1305         /* REVISIT:  should we have config level
1306          * suspend/resume callbacks?
1307          */
1308         DBG(cdev, "suspend\n");
1309         if (cdev->config) {
1310                 list_for_each_entry(f, &cdev->config->functions, list) {
1311                         if (f->suspend)
1312                                 f->suspend(f);
1313                 }
1314         }
1315         if (composite->suspend)
1316                 composite->suspend(cdev);
1317
1318         cdev->suspended = 1;
1319
1320         usb_gadget_vbus_draw(gadget, 2);
1321 }
1322
1323 static void
1324 composite_resume(struct usb_gadget *gadget)
1325 {
1326         struct usb_composite_dev        *cdev = get_gadget_data(gadget);
1327         struct usb_function             *f;
1328         u8                              maxpower;
1329
1330         /* REVISIT:  should we have config level
1331          * suspend/resume callbacks?
1332          */
1333         DBG(cdev, "resume\n");
1334         if (composite->resume)
1335                 composite->resume(cdev);
1336         if (cdev->config) {
1337                 list_for_each_entry(f, &cdev->config->functions, list) {
1338                         if (f->resume)
1339                                 f->resume(f);
1340                 }
1341
1342                 maxpower = cdev->config->bMaxPower;
1343
1344                 usb_gadget_vbus_draw(gadget, maxpower ?
1345                         (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
1346         }
1347
1348         cdev->suspended = 0;
1349 }
1350
1351 static int
1352 composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1353 {
1354         struct usb_function *f = dev_get_drvdata(dev);
1355
1356         if (!f) {
1357                 /* this happens when the device is first created */
1358                 return 0;
1359         }
1360
1361         if (add_uevent_var(env, "FUNCTION=%s", f->name))
1362                 return -ENOMEM;
1363         if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1364                 return -ENOMEM;
1365         return 0;
1366 }
1367
1368 /*-------------------------------------------------------------------------*/
1369
1370 static struct usb_gadget_driver composite_driver = {
1371         .speed          = USB_SPEED_HIGH,
1372
1373         .unbind         = composite_unbind,
1374
1375         .setup          = composite_setup,
1376         .disconnect     = composite_disconnect,
1377
1378         .suspend        = composite_suspend,
1379         .resume         = composite_resume,
1380
1381         .driver = {
1382                 .owner          = THIS_MODULE,
1383         },
1384 };
1385
1386 /**
1387  * usb_composite_probe() - register a composite driver
1388  * @driver: the driver to register
1389  * @bind: the callback used to allocate resources that are shared across the
1390  *      whole device, such as string IDs, and add its configurations using
1391  *      @usb_add_config().  This may fail by returning a negative errno
1392  *      value; it should return zero on successful initialization.
1393  * Context: single threaded during gadget setup
1394  *
1395  * This function is used to register drivers using the composite driver
1396  * framework.  The return value is zero, or a negative errno value.
1397  * Those values normally come from the driver's @bind method, which does
1398  * all the work of setting up the driver to match the hardware.
1399  *
1400  * On successful return, the gadget is ready to respond to requests from
1401  * the host, unless one of its components invokes usb_gadget_disconnect()
1402  * while it was binding.  That would usually be done in order to wait for
1403  * some userspace participation.
1404  */
1405 extern int usb_composite_probe(struct usb_composite_driver *driver,
1406                                int (*bind)(struct usb_composite_dev *cdev))
1407 {
1408         if (!driver || !driver->dev || !bind || composite)
1409                 return -EINVAL;
1410
1411         if (!driver->iProduct)
1412                 driver->iProduct = driver->name;
1413         if (!driver->name)
1414                 driver->name = "composite";
1415         composite_driver.function =  (char *) driver->name;
1416         composite_driver.driver.name = driver->name;
1417         composite = driver;
1418         composite_gadget_bind = bind;
1419
1420         driver->class = class_create(THIS_MODULE, "usb_composite");
1421         if (IS_ERR(driver->class))
1422                 return PTR_ERR(driver->class);
1423         driver->class->dev_uevent = composite_uevent;
1424
1425         return usb_gadget_probe_driver(&composite_driver, composite_bind);
1426 }
1427
1428 /**
1429  * usb_composite_unregister() - unregister a composite driver
1430  * @driver: the driver to unregister
1431  *
1432  * This function is used to unregister drivers using the composite
1433  * driver framework.
1434  */
1435 void usb_composite_unregister(struct usb_composite_driver *driver)
1436 {
1437         if (composite != driver)
1438                 return;
1439         usb_gadget_unregister_driver(&composite_driver);
1440 }