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