2 * composite.c - infrastructure for Composite USB Gadgets
4 * Copyright (C) 2006-2008 David Brownell
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.
12 /* #define VERBOSE_DEBUG */
14 #include <linux/kallsyms.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/utsname.h>
21 #include <linux/usb/composite.h>
22 #include <asm/unaligned.h>
25 * The code in this file is utility code, used to build a gadget driver
26 * from one or more "function" drivers, one or more "configuration"
27 * objects, and a "usb_composite_driver" by gluing them together along
28 * with the relevant device-wide data.
31 static char composite_manufacturer
[50];
33 /*-------------------------------------------------------------------------*/
35 * next_ep_desc() - advance to the next EP descriptor
36 * @t: currect pointer within descriptor array
38 * Return: next EP descriptor or NULL
40 * Iterate over @t until either EP descriptor found or
41 * NULL (that indicates end of list) encountered
43 static struct usb_descriptor_header
**
44 next_ep_desc(struct usb_descriptor_header
**t
)
47 if ((*t
)->bDescriptorType
== USB_DT_ENDPOINT
)
54 * for_each_ep_desc()- iterate over endpoint descriptors in the
56 * @start: pointer within descriptor array.
57 * @ep_desc: endpoint descriptor to use as the loop cursor
59 #define for_each_ep_desc(start, ep_desc) \
60 for (ep_desc = next_ep_desc(start); \
61 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
64 * config_ep_by_speed() - configures the given endpoint
65 * according to gadget speed.
66 * @g: pointer to the gadget
68 * @_ep: the endpoint to configure
70 * Return: error code, 0 on success
72 * This function chooses the right descriptors for a given
73 * endpoint according to gadget speed and saves it in the
74 * endpoint desc field. If the endpoint already has a descriptor
75 * assigned to it - overwrites it with currently corresponding
76 * descriptor. The endpoint maxpacket field is updated according
77 * to the chosen descriptor.
78 * Note: the supplied function should hold all the descriptors
79 * for supported speeds
81 int config_ep_by_speed(struct usb_gadget
*g
,
82 struct usb_function
*f
,
85 struct usb_composite_dev
*cdev
= get_gadget_data(g
);
86 struct usb_endpoint_descriptor
*chosen_desc
= NULL
;
87 struct usb_descriptor_header
**speed_desc
= NULL
;
89 struct usb_ss_ep_comp_descriptor
*comp_desc
= NULL
;
90 int want_comp_desc
= 0;
92 struct usb_descriptor_header
**d_spd
; /* cursor for speed desc */
97 /* select desired speed */
100 if (gadget_is_superspeed(g
)) {
101 speed_desc
= f
->ss_descriptors
;
105 /* else: Fall trough */
107 if (gadget_is_dualspeed(g
)) {
108 speed_desc
= f
->hs_descriptors
;
111 /* else: fall through */
113 speed_desc
= f
->descriptors
;
115 /* find descriptors */
116 for_each_ep_desc(speed_desc
, d_spd
) {
117 chosen_desc
= (struct usb_endpoint_descriptor
*)*d_spd
;
118 if (chosen_desc
->bEndpointAddress
== _ep
->address
)
125 _ep
->maxpacket
= usb_endpoint_maxp(chosen_desc
);
126 _ep
->desc
= chosen_desc
;
127 _ep
->comp_desc
= NULL
;
134 * Companion descriptor should follow EP descriptor
135 * USB 3.0 spec, #9.6.7
137 comp_desc
= (struct usb_ss_ep_comp_descriptor
*)*(++d_spd
);
139 (comp_desc
->bDescriptorType
!= USB_DT_SS_ENDPOINT_COMP
))
141 _ep
->comp_desc
= comp_desc
;
142 if (g
->speed
== USB_SPEED_SUPER
) {
143 switch (usb_endpoint_type(_ep
->desc
)) {
144 case USB_ENDPOINT_XFER_ISOC
:
145 /* mult: bits 1:0 of bmAttributes */
146 _ep
->mult
= comp_desc
->bmAttributes
& 0x3;
147 case USB_ENDPOINT_XFER_BULK
:
148 case USB_ENDPOINT_XFER_INT
:
149 _ep
->maxburst
= comp_desc
->bMaxBurst
+ 1;
152 if (comp_desc
->bMaxBurst
!= 0)
153 ERROR(cdev
, "ep0 bMaxBurst must be 0\n");
162 * usb_add_function() - add a function to a configuration
163 * @config: the configuration
164 * @function: the function being added
165 * Context: single threaded during gadget setup
167 * After initialization, each configuration must have one or more
168 * functions added to it. Adding a function involves calling its @bind()
169 * method to allocate resources such as interface and string identifiers
172 * This function returns the value of the function's bind(), which is
173 * zero for success else a negative errno value.
175 int usb_add_function(struct usb_configuration
*config
,
176 struct usb_function
*function
)
180 DBG(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
181 function
->name
, function
,
182 config
->label
, config
);
184 if (!function
->set_alt
|| !function
->disable
)
187 function
->config
= config
;
188 list_add_tail(&function
->list
, &config
->functions
);
190 /* REVISIT *require* function->bind? */
191 if (function
->bind
) {
192 value
= function
->bind(config
, function
);
194 list_del(&function
->list
);
195 function
->config
= NULL
;
200 /* We allow configurations that don't work at both speeds.
201 * If we run into a lowspeed Linux system, treat it the same
202 * as full speed ... it's the function drivers that will need
203 * to avoid bulk and ISO transfers.
205 if (!config
->fullspeed
&& function
->descriptors
)
206 config
->fullspeed
= true;
207 if (!config
->highspeed
&& function
->hs_descriptors
)
208 config
->highspeed
= true;
209 if (!config
->superspeed
&& function
->ss_descriptors
)
210 config
->superspeed
= true;
214 DBG(config
->cdev
, "adding '%s'/%p --> %d\n",
215 function
->name
, function
, value
);
220 * usb_function_deactivate - prevent function and gadget enumeration
221 * @function: the function that isn't yet ready to respond
223 * Blocks response of the gadget driver to host enumeration by
224 * preventing the data line pullup from being activated. This is
225 * normally called during @bind() processing to change from the
226 * initial "ready to respond" state, or when a required resource
229 * For example, drivers that serve as a passthrough to a userspace
230 * daemon can block enumeration unless that daemon (such as an OBEX,
231 * MTP, or print server) is ready to handle host requests.
233 * Not all systems support software control of their USB peripheral
236 * Returns zero on success, else negative errno.
238 int usb_function_deactivate(struct usb_function
*function
)
240 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
244 spin_lock_irqsave(&cdev
->lock
, flags
);
246 if (cdev
->deactivations
== 0)
247 status
= usb_gadget_disconnect(cdev
->gadget
);
249 cdev
->deactivations
++;
251 spin_unlock_irqrestore(&cdev
->lock
, flags
);
256 * usb_function_activate - allow function and gadget enumeration
257 * @function: function on which usb_function_activate() was called
259 * Reverses effect of usb_function_deactivate(). If no more functions
260 * are delaying their activation, the gadget driver will respond to
261 * host enumeration procedures.
263 * Returns zero on success, else negative errno.
265 int usb_function_activate(struct usb_function
*function
)
267 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
271 spin_lock_irqsave(&cdev
->lock
, flags
);
273 if (WARN_ON(cdev
->deactivations
== 0))
276 cdev
->deactivations
--;
277 if (cdev
->deactivations
== 0)
278 status
= usb_gadget_connect(cdev
->gadget
);
281 spin_unlock_irqrestore(&cdev
->lock
, flags
);
286 * usb_interface_id() - allocate an unused interface ID
287 * @config: configuration associated with the interface
288 * @function: function handling the interface
289 * Context: single threaded during gadget setup
291 * usb_interface_id() is called from usb_function.bind() callbacks to
292 * allocate new interface IDs. The function driver will then store that
293 * ID in interface, association, CDC union, and other descriptors. It
294 * will also handle any control requests targeted at that interface,
295 * particularly changing its altsetting via set_alt(). There may
296 * also be class-specific or vendor-specific requests to handle.
298 * All interface identifier should be allocated using this routine, to
299 * ensure that for example different functions don't wrongly assign
300 * different meanings to the same identifier. Note that since interface
301 * identifiers are configuration-specific, functions used in more than
302 * one configuration (or more than once in a given configuration) need
303 * multiple versions of the relevant descriptors.
305 * Returns the interface ID which was allocated; or -ENODEV if no
306 * more interface IDs can be allocated.
308 int usb_interface_id(struct usb_configuration
*config
,
309 struct usb_function
*function
)
311 unsigned id
= config
->next_interface_id
;
313 if (id
< MAX_CONFIG_INTERFACES
) {
314 config
->interface
[id
] = function
;
315 config
->next_interface_id
= id
+ 1;
321 static int config_buf(struct usb_configuration
*config
,
322 enum usb_device_speed speed
, void *buf
, u8 type
)
324 struct usb_config_descriptor
*c
= buf
;
325 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
327 struct usb_function
*f
;
330 len
= USB_COMP_EP0_BUFSIZ
- USB_DT_CONFIG_SIZE
;
331 /* write the config descriptor */
333 c
->bLength
= USB_DT_CONFIG_SIZE
;
334 c
->bDescriptorType
= type
;
335 /* wTotalLength is written later */
336 c
->bNumInterfaces
= config
->next_interface_id
;
337 c
->bConfigurationValue
= config
->bConfigurationValue
;
338 c
->iConfiguration
= config
->iConfiguration
;
339 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
340 c
->bMaxPower
= config
->bMaxPower
? : (CONFIG_USB_GADGET_VBUS_DRAW
/ 2);
342 /* There may be e.g. OTG descriptors */
343 if (config
->descriptors
) {
344 status
= usb_descriptor_fillbuf(next
, len
,
345 config
->descriptors
);
352 /* add each function's descriptors */
353 list_for_each_entry(f
, &config
->functions
, list
) {
354 struct usb_descriptor_header
**descriptors
;
357 case USB_SPEED_SUPER
:
358 descriptors
= f
->ss_descriptors
;
361 descriptors
= f
->hs_descriptors
;
364 descriptors
= f
->descriptors
;
369 status
= usb_descriptor_fillbuf(next
, len
,
370 (const struct usb_descriptor_header
**) descriptors
);
378 c
->wTotalLength
= cpu_to_le16(len
);
382 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
384 struct usb_gadget
*gadget
= cdev
->gadget
;
385 struct usb_configuration
*c
;
386 u8 type
= w_value
>> 8;
387 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
389 if (gadget
->speed
== USB_SPEED_SUPER
)
390 speed
= gadget
->speed
;
391 else if (gadget_is_dualspeed(gadget
)) {
393 if (gadget
->speed
== USB_SPEED_HIGH
)
395 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
398 speed
= USB_SPEED_HIGH
;
402 /* This is a lookup by config *INDEX* */
404 list_for_each_entry(c
, &cdev
->configs
, list
) {
405 /* ignore configs that won't work at this speed */
407 case USB_SPEED_SUPER
:
421 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
427 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
429 struct usb_gadget
*gadget
= cdev
->gadget
;
430 struct usb_configuration
*c
;
435 if (gadget_is_dualspeed(gadget
)) {
436 if (gadget
->speed
== USB_SPEED_HIGH
)
438 if (gadget
->speed
== USB_SPEED_SUPER
)
440 if (type
== USB_DT_DEVICE_QUALIFIER
)
443 list_for_each_entry(c
, &cdev
->configs
, list
) {
444 /* ignore configs that won't work at this speed */
461 * bos_desc() - prepares the BOS descriptor.
462 * @cdev: pointer to usb_composite device to generate the bos
465 * This function generates the BOS (Binary Device Object)
466 * descriptor and its device capabilities descriptors. The BOS
467 * descriptor should be supported by a SuperSpeed device.
469 static int bos_desc(struct usb_composite_dev
*cdev
)
471 struct usb_ext_cap_descriptor
*usb_ext
;
472 struct usb_ss_cap_descriptor
*ss_cap
;
473 struct usb_dcd_config_params dcd_config_params
;
474 struct usb_bos_descriptor
*bos
= cdev
->req
->buf
;
476 bos
->bLength
= USB_DT_BOS_SIZE
;
477 bos
->bDescriptorType
= USB_DT_BOS
;
479 bos
->wTotalLength
= cpu_to_le16(USB_DT_BOS_SIZE
);
480 bos
->bNumDeviceCaps
= 0;
483 * A SuperSpeed device shall include the USB2.0 extension descriptor
484 * and shall support LPM when operating in USB2.0 HS mode.
486 usb_ext
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
487 bos
->bNumDeviceCaps
++;
488 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_EXT_CAP_SIZE
);
489 usb_ext
->bLength
= USB_DT_USB_EXT_CAP_SIZE
;
490 usb_ext
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
491 usb_ext
->bDevCapabilityType
= USB_CAP_TYPE_EXT
;
492 usb_ext
->bmAttributes
= cpu_to_le32(USB_LPM_SUPPORT
);
495 * The Superspeed USB Capability descriptor shall be implemented by all
496 * SuperSpeed devices.
498 ss_cap
= cdev
->req
->buf
+ le16_to_cpu(bos
->wTotalLength
);
499 bos
->bNumDeviceCaps
++;
500 le16_add_cpu(&bos
->wTotalLength
, USB_DT_USB_SS_CAP_SIZE
);
501 ss_cap
->bLength
= USB_DT_USB_SS_CAP_SIZE
;
502 ss_cap
->bDescriptorType
= USB_DT_DEVICE_CAPABILITY
;
503 ss_cap
->bDevCapabilityType
= USB_SS_CAP_TYPE
;
504 ss_cap
->bmAttributes
= 0; /* LTM is not supported yet */
505 ss_cap
->wSpeedSupported
= cpu_to_le16(USB_LOW_SPEED_OPERATION
|
506 USB_FULL_SPEED_OPERATION
|
507 USB_HIGH_SPEED_OPERATION
|
508 USB_5GBPS_OPERATION
);
509 ss_cap
->bFunctionalitySupport
= USB_LOW_SPEED_OPERATION
;
511 /* Get Controller configuration */
512 if (cdev
->gadget
->ops
->get_config_params
)
513 cdev
->gadget
->ops
->get_config_params(&dcd_config_params
);
515 dcd_config_params
.bU1devExitLat
= USB_DEFAULT_U1_DEV_EXIT_LAT
;
516 dcd_config_params
.bU2DevExitLat
=
517 cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT
);
519 ss_cap
->bU1devExitLat
= dcd_config_params
.bU1devExitLat
;
520 ss_cap
->bU2DevExitLat
= dcd_config_params
.bU2DevExitLat
;
522 return le16_to_cpu(bos
->wTotalLength
);
525 static void device_qual(struct usb_composite_dev
*cdev
)
527 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
529 qual
->bLength
= sizeof(*qual
);
530 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
531 /* POLICY: same bcdUSB and device type info at both speeds */
532 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
533 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
534 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
535 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
536 /* ASSUME same EP0 fifo size at both speeds */
537 qual
->bMaxPacketSize0
= cdev
->gadget
->ep0
->maxpacket
;
538 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
542 /*-------------------------------------------------------------------------*/
544 static void reset_config(struct usb_composite_dev
*cdev
)
546 struct usb_function
*f
;
548 DBG(cdev
, "reset config\n");
550 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
554 bitmap_zero(f
->endpoints
, 32);
559 static int set_config(struct usb_composite_dev
*cdev
,
560 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
562 struct usb_gadget
*gadget
= cdev
->gadget
;
563 struct usb_configuration
*c
= NULL
;
564 int result
= -EINVAL
;
565 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
569 list_for_each_entry(c
, &cdev
->configs
, list
) {
570 if (c
->bConfigurationValue
== number
) {
572 * We disable the FDs of the previous
573 * configuration only if the new configuration
584 } else { /* Zero configuration value - need to reset the config */
590 INFO(cdev
, "%s config #%d: %s\n",
591 usb_speed_string(gadget
->speed
),
592 number
, c
? c
->label
: "unconfigured");
599 /* Initialize all interfaces by setting them to altsetting zero. */
600 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
601 struct usb_function
*f
= c
->interface
[tmp
];
602 struct usb_descriptor_header
**descriptors
;
608 * Record which endpoints are used by the function. This is used
609 * to dispatch control requests targeted at that endpoint to the
610 * function's setup callback instead of the current
611 * configuration's setup callback.
613 switch (gadget
->speed
) {
614 case USB_SPEED_SUPER
:
615 descriptors
= f
->ss_descriptors
;
618 descriptors
= f
->hs_descriptors
;
621 descriptors
= f
->descriptors
;
624 for (; *descriptors
; ++descriptors
) {
625 struct usb_endpoint_descriptor
*ep
;
628 if ((*descriptors
)->bDescriptorType
!= USB_DT_ENDPOINT
)
631 ep
= (struct usb_endpoint_descriptor
*)*descriptors
;
632 addr
= ((ep
->bEndpointAddress
& 0x80) >> 3)
633 | (ep
->bEndpointAddress
& 0x0f);
634 set_bit(addr
, f
->endpoints
);
637 result
= f
->set_alt(f
, tmp
, 0);
639 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
640 tmp
, f
->name
, f
, result
);
646 if (result
== USB_GADGET_DELAYED_STATUS
) {
648 "%s: interface %d (%s) requested delayed status\n",
649 __func__
, tmp
, f
->name
);
650 cdev
->delayed_status
++;
651 DBG(cdev
, "delayed_status count %d\n",
652 cdev
->delayed_status
);
656 /* when we return, be sure our power usage is valid */
657 power
= c
->bMaxPower
? (2 * c
->bMaxPower
) : CONFIG_USB_GADGET_VBUS_DRAW
;
659 usb_gadget_vbus_draw(gadget
, power
);
660 if (result
>= 0 && cdev
->delayed_status
)
661 result
= USB_GADGET_DELAYED_STATUS
;
666 * usb_add_config() - add a configuration to a device.
667 * @cdev: wraps the USB gadget
668 * @config: the configuration, with bConfigurationValue assigned
669 * @bind: the configuration's bind function
670 * Context: single threaded during gadget setup
672 * One of the main tasks of a composite @bind() routine is to
673 * add each of the configurations it supports, using this routine.
675 * This function returns the value of the configuration's @bind(), which
676 * is zero for success else a negative errno value. Binding configurations
677 * assigns global resources including string IDs, and per-configuration
678 * resources such as interface IDs and endpoints.
680 int usb_add_config(struct usb_composite_dev
*cdev
,
681 struct usb_configuration
*config
,
682 int (*bind
)(struct usb_configuration
*))
684 int status
= -EINVAL
;
685 struct usb_configuration
*c
;
687 DBG(cdev
, "adding config #%u '%s'/%p\n",
688 config
->bConfigurationValue
,
689 config
->label
, config
);
691 if (!config
->bConfigurationValue
|| !bind
)
694 /* Prevent duplicate configuration identifiers */
695 list_for_each_entry(c
, &cdev
->configs
, list
) {
696 if (c
->bConfigurationValue
== config
->bConfigurationValue
) {
703 list_add_tail(&config
->list
, &cdev
->configs
);
705 INIT_LIST_HEAD(&config
->functions
);
706 config
->next_interface_id
= 0;
707 memset(config
->interface
, 0, sizeof(config
->interface
));
709 status
= bind(config
);
711 while (!list_empty(&config
->functions
)) {
712 struct usb_function
*f
;
714 f
= list_first_entry(&config
->functions
,
715 struct usb_function
, list
);
718 DBG(cdev
, "unbind function '%s'/%p\n",
720 f
->unbind(config
, f
);
721 /* may free memory for "f" */
724 list_del(&config
->list
);
729 DBG(cdev
, "cfg %d/%p speeds:%s%s%s\n",
730 config
->bConfigurationValue
, config
,
731 config
->superspeed
? " super" : "",
732 config
->highspeed
? " high" : "",
734 ? (gadget_is_dualspeed(cdev
->gadget
)
739 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
740 struct usb_function
*f
= config
->interface
[i
];
744 DBG(cdev
, " interface %d = %s/%p\n",
749 /* set_alt(), or next bind(), sets up
750 * ep->driver_data as needed.
752 usb_ep_autoconfig_reset(cdev
->gadget
);
756 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
757 config
->bConfigurationValue
, status
);
761 static void remove_config(struct usb_composite_dev
*cdev
,
762 struct usb_configuration
*config
)
764 while (!list_empty(&config
->functions
)) {
765 struct usb_function
*f
;
767 f
= list_first_entry(&config
->functions
,
768 struct usb_function
, list
);
771 DBG(cdev
, "unbind function '%s'/%p\n", f
->name
, f
);
772 f
->unbind(config
, f
);
773 /* may free memory for "f" */
776 list_del(&config
->list
);
777 if (config
->unbind
) {
778 DBG(cdev
, "unbind config '%s'/%p\n", config
->label
, config
);
779 config
->unbind(config
);
780 /* may free memory for "c" */
785 * usb_remove_config() - remove a configuration from a device.
786 * @cdev: wraps the USB gadget
787 * @config: the configuration
789 * Drivers must call usb_gadget_disconnect before calling this function
790 * to disconnect the device from the host and make sure the host will not
791 * try to enumerate the device while we are changing the config list.
793 void usb_remove_config(struct usb_composite_dev
*cdev
,
794 struct usb_configuration
*config
)
798 spin_lock_irqsave(&cdev
->lock
, flags
);
800 if (cdev
->config
== config
)
803 spin_unlock_irqrestore(&cdev
->lock
, flags
);
805 remove_config(cdev
, config
);
808 /*-------------------------------------------------------------------------*/
810 /* We support strings in multiple languages ... string descriptor zero
811 * says which languages are supported. The typical case will be that
812 * only one language (probably English) is used, with I18N handled on
816 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
818 const struct usb_gadget_strings
*s
;
824 language
= cpu_to_le16(s
->language
);
825 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
826 if (*tmp
== language
)
835 static int lookup_string(
836 struct usb_gadget_strings
**sp
,
842 struct usb_gadget_strings
*s
;
847 if (s
->language
!= language
)
849 value
= usb_gadget_get_string(s
, id
, buf
);
856 static int get_string(struct usb_composite_dev
*cdev
,
857 void *buf
, u16 language
, int id
)
859 struct usb_composite_driver
*composite
= cdev
->driver
;
860 struct usb_configuration
*c
;
861 struct usb_function
*f
;
865 /* Yes, not only is USB's I18N support probably more than most
866 * folk will ever care about ... also, it's all supported here.
867 * (Except for UTF8 support for Unicode's "Astral Planes".)
870 /* 0 == report all available language codes */
872 struct usb_string_descriptor
*s
= buf
;
873 struct usb_gadget_strings
**sp
;
876 s
->bDescriptorType
= USB_DT_STRING
;
878 sp
= composite
->strings
;
880 collect_langs(sp
, s
->wData
);
882 list_for_each_entry(c
, &cdev
->configs
, list
) {
885 collect_langs(sp
, s
->wData
);
887 list_for_each_entry(f
, &c
->functions
, list
) {
890 collect_langs(sp
, s
->wData
);
894 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
899 s
->bLength
= 2 * (len
+ 1);
903 /* Otherwise, look up and return a specified string. First
904 * check if the string has not been overridden.
906 if (cdev
->manufacturer_override
== id
)
907 str
= composite
->iManufacturer
?: composite_manufacturer
;
908 else if (cdev
->product_override
== id
)
909 str
= composite
->iProduct
;
910 else if (cdev
->serial_override
== id
)
911 str
= composite
->iSerialNumber
;
915 struct usb_gadget_strings strings
= {
916 .language
= language
,
917 .strings
= &(struct usb_string
) { 0xff, str
}
919 return usb_gadget_get_string(&strings
, 0xff, buf
);
922 /* String IDs are device-scoped, so we look up each string
923 * table we're told about. These lookups are infrequent;
924 * simpler-is-better here.
926 if (composite
->strings
) {
927 len
= lookup_string(composite
->strings
, buf
, language
, id
);
931 list_for_each_entry(c
, &cdev
->configs
, list
) {
933 len
= lookup_string(c
->strings
, buf
, language
, id
);
937 list_for_each_entry(f
, &c
->functions
, list
) {
940 len
= lookup_string(f
->strings
, buf
, language
, id
);
949 * usb_string_id() - allocate an unused string ID
950 * @cdev: the device whose string descriptor IDs are being allocated
951 * Context: single threaded during gadget setup
953 * @usb_string_id() is called from bind() callbacks to allocate
954 * string IDs. Drivers for functions, configurations, or gadgets will
955 * then store that ID in the appropriate descriptors and string table.
957 * All string identifier should be allocated using this,
958 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
959 * that for example different functions don't wrongly assign different
960 * meanings to the same identifier.
962 int usb_string_id(struct usb_composite_dev
*cdev
)
964 if (cdev
->next_string_id
< 254) {
965 /* string id 0 is reserved by USB spec for list of
966 * supported languages */
967 /* 255 reserved as well? -- mina86 */
968 cdev
->next_string_id
++;
969 return cdev
->next_string_id
;
975 * usb_string_ids() - allocate unused string IDs in batch
976 * @cdev: the device whose string descriptor IDs are being allocated
977 * @str: an array of usb_string objects to assign numbers to
978 * Context: single threaded during gadget setup
980 * @usb_string_ids() is called from bind() callbacks to allocate
981 * string IDs. Drivers for functions, configurations, or gadgets will
982 * then copy IDs from the string table to the appropriate descriptors
983 * and string table for other languages.
985 * All string identifier should be allocated using this,
986 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
987 * example different functions don't wrongly assign different meanings
988 * to the same identifier.
990 int usb_string_ids_tab(struct usb_composite_dev
*cdev
, struct usb_string
*str
)
992 int next
= cdev
->next_string_id
;
994 for (; str
->s
; ++str
) {
995 if (unlikely(next
>= 254))
1000 cdev
->next_string_id
= next
;
1006 * usb_string_ids_n() - allocate unused string IDs in batch
1007 * @c: the device whose string descriptor IDs are being allocated
1008 * @n: number of string IDs to allocate
1009 * Context: single threaded during gadget setup
1011 * Returns the first requested ID. This ID and next @n-1 IDs are now
1012 * valid IDs. At least provided that @n is non-zero because if it
1013 * is, returns last requested ID which is now very useful information.
1015 * @usb_string_ids_n() is called from bind() callbacks to allocate
1016 * string IDs. Drivers for functions, configurations, or gadgets will
1017 * then store that ID in the appropriate descriptors and string table.
1019 * All string identifier should be allocated using this,
1020 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1021 * example different functions don't wrongly assign different meanings
1022 * to the same identifier.
1024 int usb_string_ids_n(struct usb_composite_dev
*c
, unsigned n
)
1026 unsigned next
= c
->next_string_id
;
1027 if (unlikely(n
> 254 || (unsigned)next
+ n
> 254))
1029 c
->next_string_id
+= n
;
1034 /*-------------------------------------------------------------------------*/
1036 static void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
1038 if (req
->status
|| req
->actual
!= req
->length
)
1039 DBG((struct usb_composite_dev
*) ep
->driver_data
,
1040 "setup complete --> %d, %d/%d\n",
1041 req
->status
, req
->actual
, req
->length
);
1045 * The setup() callback implements all the ep0 functionality that's
1046 * not handled lower down, in hardware or the hardware driver(like
1047 * device and endpoint feature flags, and their status). It's all
1048 * housekeeping for the gadget function we're implementing. Most of
1049 * the work is in config and function specific setup.
1052 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
1054 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1055 struct usb_request
*req
= cdev
->req
;
1056 int value
= -EOPNOTSUPP
;
1058 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
1059 u8 intf
= w_index
& 0xFF;
1060 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
1061 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
1062 struct usb_function
*f
= NULL
;
1065 /* partial re-init of the response message; the function or the
1066 * gadget might need to intercept e.g. a control-OUT completion
1067 * when we delegate to it.
1070 req
->complete
= composite_setup_complete
;
1072 gadget
->ep0
->driver_data
= cdev
;
1074 switch (ctrl
->bRequest
) {
1076 /* we handle all standard USB descriptors */
1077 case USB_REQ_GET_DESCRIPTOR
:
1078 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1080 switch (w_value
>> 8) {
1083 cdev
->desc
.bNumConfigurations
=
1084 count_configs(cdev
, USB_DT_DEVICE
);
1085 cdev
->desc
.bMaxPacketSize0
=
1086 cdev
->gadget
->ep0
->maxpacket
;
1087 if (gadget_is_superspeed(gadget
)) {
1088 if (gadget
->speed
>= USB_SPEED_SUPER
) {
1089 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0300);
1090 cdev
->desc
.bMaxPacketSize0
= 9;
1092 cdev
->desc
.bcdUSB
= cpu_to_le16(0x0210);
1096 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
1097 memcpy(req
->buf
, &cdev
->desc
, value
);
1099 case USB_DT_DEVICE_QUALIFIER
:
1100 if (!gadget_is_dualspeed(gadget
) ||
1101 gadget
->speed
>= USB_SPEED_SUPER
)
1104 value
= min_t(int, w_length
,
1105 sizeof(struct usb_qualifier_descriptor
));
1107 case USB_DT_OTHER_SPEED_CONFIG
:
1108 if (!gadget_is_dualspeed(gadget
) ||
1109 gadget
->speed
>= USB_SPEED_SUPER
)
1113 value
= config_desc(cdev
, w_value
);
1115 value
= min(w_length
, (u16
) value
);
1118 value
= get_string(cdev
, req
->buf
,
1119 w_index
, w_value
& 0xff);
1121 value
= min(w_length
, (u16
) value
);
1124 if (gadget_is_superspeed(gadget
)) {
1125 value
= bos_desc(cdev
);
1126 value
= min(w_length
, (u16
) value
);
1132 /* any number of configs can work */
1133 case USB_REQ_SET_CONFIGURATION
:
1134 if (ctrl
->bRequestType
!= 0)
1136 if (gadget_is_otg(gadget
)) {
1137 if (gadget
->a_hnp_support
)
1138 DBG(cdev
, "HNP available\n");
1139 else if (gadget
->a_alt_hnp_support
)
1140 DBG(cdev
, "HNP on another port\n");
1142 VDBG(cdev
, "HNP inactive\n");
1144 spin_lock(&cdev
->lock
);
1145 value
= set_config(cdev
, ctrl
, w_value
);
1146 spin_unlock(&cdev
->lock
);
1148 case USB_REQ_GET_CONFIGURATION
:
1149 if (ctrl
->bRequestType
!= USB_DIR_IN
)
1152 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
1154 *(u8
*)req
->buf
= 0;
1155 value
= min(w_length
, (u16
) 1);
1158 /* function drivers must handle get/set altsetting; if there's
1159 * no get() method, we know only altsetting zero works.
1161 case USB_REQ_SET_INTERFACE
:
1162 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
1164 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1166 f
= cdev
->config
->interface
[intf
];
1169 if (w_value
&& !f
->set_alt
)
1171 value
= f
->set_alt(f
, w_index
, w_value
);
1172 if (value
== USB_GADGET_DELAYED_STATUS
) {
1174 "%s: interface %d (%s) requested delayed status\n",
1175 __func__
, intf
, f
->name
);
1176 cdev
->delayed_status
++;
1177 DBG(cdev
, "delayed_status count %d\n",
1178 cdev
->delayed_status
);
1181 case USB_REQ_GET_INTERFACE
:
1182 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
1184 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1186 f
= cdev
->config
->interface
[intf
];
1189 /* lots of interfaces only need altsetting zero... */
1190 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
1193 *((u8
*)req
->buf
) = value
;
1194 value
= min(w_length
, (u16
) 1);
1198 * USB 3.0 additions:
1199 * Function driver should handle get_status request. If such cb
1200 * wasn't supplied we respond with default value = 0
1201 * Note: function driver should supply such cb only for the first
1202 * interface of the function
1204 case USB_REQ_GET_STATUS
:
1205 if (!gadget_is_superspeed(gadget
))
1207 if (ctrl
->bRequestType
!= (USB_DIR_IN
| USB_RECIP_INTERFACE
))
1209 value
= 2; /* This is the length of the get_status reply */
1210 put_unaligned_le16(0, req
->buf
);
1211 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1213 f
= cdev
->config
->interface
[intf
];
1216 status
= f
->get_status
? f
->get_status(f
) : 0;
1219 put_unaligned_le16(status
& 0x0000ffff, req
->buf
);
1222 * Function drivers should handle SetFeature/ClearFeature
1223 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1224 * only for the first interface of the function
1226 case USB_REQ_CLEAR_FEATURE
:
1227 case USB_REQ_SET_FEATURE
:
1228 if (!gadget_is_superspeed(gadget
))
1230 if (ctrl
->bRequestType
!= (USB_DIR_OUT
| USB_RECIP_INTERFACE
))
1233 case USB_INTRF_FUNC_SUSPEND
:
1234 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1236 f
= cdev
->config
->interface
[intf
];
1240 if (f
->func_suspend
)
1241 value
= f
->func_suspend(f
, w_index
>> 8);
1244 "func_suspend() returned error %d\n",
1254 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1255 ctrl
->bRequestType
, ctrl
->bRequest
,
1256 w_value
, w_index
, w_length
);
1258 /* functions always handle their interfaces and endpoints...
1259 * punt other recipients (other, WUSB, ...) to the current
1260 * configuration code.
1262 * REVISIT it could make sense to let the composite device
1263 * take such requests too, if that's ever needed: to work
1266 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
1267 case USB_RECIP_INTERFACE
:
1268 if (!cdev
->config
|| intf
>= MAX_CONFIG_INTERFACES
)
1270 f
= cdev
->config
->interface
[intf
];
1273 case USB_RECIP_ENDPOINT
:
1274 endp
= ((w_index
& 0x80) >> 3) | (w_index
& 0x0f);
1275 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1276 if (test_bit(endp
, f
->endpoints
))
1279 if (&f
->list
== &cdev
->config
->functions
)
1285 value
= f
->setup(f
, ctrl
);
1287 struct usb_configuration
*c
;
1291 value
= c
->setup(c
, ctrl
);
1297 /* respond with data transfer before status phase? */
1298 if (value
>= 0 && value
!= USB_GADGET_DELAYED_STATUS
) {
1299 req
->length
= value
;
1300 req
->zero
= value
< w_length
;
1301 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
1303 DBG(cdev
, "ep_queue --> %d\n", value
);
1305 composite_setup_complete(gadget
->ep0
, req
);
1307 } else if (value
== USB_GADGET_DELAYED_STATUS
&& w_length
!= 0) {
1309 "%s: Delayed status not supported for w_length != 0",
1314 /* device either stalls (value < 0) or reports success */
1318 static void composite_disconnect(struct usb_gadget
*gadget
)
1320 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1321 unsigned long flags
;
1323 /* REVISIT: should we have config and device level
1324 * disconnect callbacks?
1326 spin_lock_irqsave(&cdev
->lock
, flags
);
1329 if (cdev
->driver
->disconnect
)
1330 cdev
->driver
->disconnect(cdev
);
1331 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1334 /*-------------------------------------------------------------------------*/
1336 static ssize_t
composite_show_suspended(struct device
*dev
,
1337 struct device_attribute
*attr
,
1340 struct usb_gadget
*gadget
= dev_to_usb_gadget(dev
);
1341 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1343 return sprintf(buf
, "%d\n", cdev
->suspended
);
1346 static DEVICE_ATTR(suspended
, 0444, composite_show_suspended
, NULL
);
1349 composite_unbind(struct usb_gadget
*gadget
)
1351 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1353 /* composite_disconnect() must already have been called
1354 * by the underlying peripheral controller driver!
1355 * so there's no i/o concurrency that could affect the
1356 * state protected by cdev->lock.
1358 WARN_ON(cdev
->config
);
1360 while (!list_empty(&cdev
->configs
)) {
1361 struct usb_configuration
*c
;
1362 c
= list_first_entry(&cdev
->configs
,
1363 struct usb_configuration
, list
);
1364 remove_config(cdev
, c
);
1366 if (cdev
->driver
->unbind
)
1367 cdev
->driver
->unbind(cdev
);
1370 kfree(cdev
->req
->buf
);
1371 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
1373 device_remove_file(&gadget
->dev
, &dev_attr_suspended
);
1375 set_gadget_data(gadget
, NULL
);
1378 static u8
override_id(struct usb_composite_dev
*cdev
, u8
*desc
)
1381 int ret
= usb_string_id(cdev
);
1382 if (unlikely(ret
< 0))
1383 WARNING(cdev
, "failed to override string ID\n");
1391 static void update_unchanged_dev_desc(struct usb_device_descriptor
*new,
1392 const struct usb_device_descriptor
*old
)
1402 * these variables may have been set in
1403 * usb_composite_overwrite_options()
1405 idVendor
= new->idVendor
;
1406 idProduct
= new->idProduct
;
1407 bcdDevice
= new->bcdDevice
;
1408 iSerialNumber
= new->iSerialNumber
;
1409 iManufacturer
= new->iManufacturer
;
1410 iProduct
= new->iProduct
;
1414 new->idVendor
= idVendor
;
1416 new->idProduct
= idProduct
;
1418 new->bcdDevice
= bcdDevice
;
1420 new->iSerialNumber
= iSerialNumber
;
1422 new->iManufacturer
= iManufacturer
;
1424 new->iProduct
= iProduct
;
1427 static struct usb_composite_driver
*to_cdriver(struct usb_gadget_driver
*gdrv
)
1429 return container_of(gdrv
, struct usb_composite_driver
, gadget_driver
);
1432 static int composite_bind(struct usb_gadget
*gadget
,
1433 struct usb_gadget_driver
*gdriver
)
1435 struct usb_composite_dev
*cdev
;
1436 struct usb_composite_driver
*composite
= to_cdriver(gdriver
);
1437 int status
= -ENOMEM
;
1439 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
1443 spin_lock_init(&cdev
->lock
);
1444 cdev
->gadget
= gadget
;
1445 set_gadget_data(gadget
, cdev
);
1446 INIT_LIST_HEAD(&cdev
->configs
);
1448 /* preallocate control response and buffer */
1449 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
1452 cdev
->req
->buf
= kmalloc(USB_COMP_EP0_BUFSIZ
, GFP_KERNEL
);
1453 if (!cdev
->req
->buf
)
1455 cdev
->req
->complete
= composite_setup_complete
;
1456 gadget
->ep0
->driver_data
= cdev
;
1458 cdev
->driver
= composite
;
1461 * As per USB compliance update, a device that is actively drawing
1462 * more than 100mA from USB must report itself as bus-powered in
1463 * the GetStatus(DEVICE) call.
1465 if (CONFIG_USB_GADGET_VBUS_DRAW
<= USB_SELF_POWER_VBUS_MAX_DRAW
)
1466 usb_gadget_set_selfpowered(gadget
);
1468 /* interface and string IDs start at zero via kzalloc.
1469 * we force endpoints to start unassigned; few controller
1470 * drivers will zero ep->driver_data.
1472 usb_ep_autoconfig_reset(cdev
->gadget
);
1474 /* composite gadget needs to assign strings for whole device (like
1475 * serial number), register function drivers, potentially update
1476 * power state and consumption, etc
1478 status
= composite
->bind(cdev
);
1482 update_unchanged_dev_desc(&cdev
->desc
, composite
->dev
);
1484 /* string overrides */
1485 if (!cdev
->desc
.iManufacturer
) {
1486 if (!composite
->iManufacturer
)
1487 snprintf(composite_manufacturer
,
1488 sizeof composite_manufacturer
,
1490 init_utsname()->sysname
,
1491 init_utsname()->release
,
1494 cdev
->manufacturer_override
=
1495 override_id(cdev
, &cdev
->desc
.iManufacturer
);
1498 if (!cdev
->desc
.iProduct
&& composite
->iProduct
)
1499 cdev
->product_override
=
1500 override_id(cdev
, &cdev
->desc
.iProduct
);
1502 if (composite
->iSerialNumber
)
1503 cdev
->serial_override
=
1504 override_id(cdev
, &cdev
->desc
.iSerialNumber
);
1506 /* has userspace failed to provide a serial number? */
1507 if (composite
->needs_serial
&& !cdev
->desc
.iSerialNumber
)
1508 WARNING(cdev
, "userspace failed to provide iSerialNumber\n");
1511 status
= device_create_file(&gadget
->dev
, &dev_attr_suspended
);
1515 INFO(cdev
, "%s ready\n", composite
->name
);
1519 composite_unbind(gadget
);
1523 /*-------------------------------------------------------------------------*/
1526 composite_suspend(struct usb_gadget
*gadget
)
1528 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1529 struct usb_function
*f
;
1531 /* REVISIT: should we have config level
1532 * suspend/resume callbacks?
1534 DBG(cdev
, "suspend\n");
1536 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1541 if (cdev
->driver
->suspend
)
1542 cdev
->driver
->suspend(cdev
);
1544 cdev
->suspended
= 1;
1546 usb_gadget_vbus_draw(gadget
, 2);
1550 composite_resume(struct usb_gadget
*gadget
)
1552 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1553 struct usb_function
*f
;
1556 /* REVISIT: should we have config level
1557 * suspend/resume callbacks?
1559 DBG(cdev
, "resume\n");
1560 if (cdev
->driver
->resume
)
1561 cdev
->driver
->resume(cdev
);
1563 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1568 maxpower
= cdev
->config
->bMaxPower
;
1570 usb_gadget_vbus_draw(gadget
, maxpower
?
1571 (2 * maxpower
) : CONFIG_USB_GADGET_VBUS_DRAW
);
1574 cdev
->suspended
= 0;
1577 /*-------------------------------------------------------------------------*/
1579 static const struct usb_gadget_driver composite_driver_template
= {
1580 .bind
= composite_bind
,
1581 .unbind
= composite_unbind
,
1583 .setup
= composite_setup
,
1584 .disconnect
= composite_disconnect
,
1586 .suspend
= composite_suspend
,
1587 .resume
= composite_resume
,
1590 .owner
= THIS_MODULE
,
1595 * usb_composite_probe() - register a composite driver
1596 * @driver: the driver to register
1597 * @bind: the callback used to allocate resources that are shared across the
1598 * whole device, such as string IDs, and add its configurations using
1599 * @usb_add_config(). This may fail by returning a negative errno
1600 * value; it should return zero on successful initialization.
1601 * Context: single threaded during gadget setup
1603 * This function is used to register drivers using the composite driver
1604 * framework. The return value is zero, or a negative errno value.
1605 * Those values normally come from the driver's @bind method, which does
1606 * all the work of setting up the driver to match the hardware.
1608 * On successful return, the gadget is ready to respond to requests from
1609 * the host, unless one of its components invokes usb_gadget_disconnect()
1610 * while it was binding. That would usually be done in order to wait for
1611 * some userspace participation.
1613 int usb_composite_probe(struct usb_composite_driver
*driver
)
1615 struct usb_gadget_driver
*gadget_driver
;
1617 if (!driver
|| !driver
->dev
|| !driver
->bind
)
1621 driver
->name
= "composite";
1622 if (!driver
->iProduct
)
1623 driver
->iProduct
= driver
->name
;
1625 driver
->gadget_driver
= composite_driver_template
;
1626 gadget_driver
= &driver
->gadget_driver
;
1628 gadget_driver
->function
= (char *) driver
->name
;
1629 gadget_driver
->driver
.name
= driver
->name
;
1630 gadget_driver
->max_speed
= driver
->max_speed
;
1632 return usb_gadget_probe_driver(gadget_driver
);
1636 * usb_composite_unregister() - unregister a composite driver
1637 * @driver: the driver to unregister
1639 * This function is used to unregister drivers using the composite
1642 void usb_composite_unregister(struct usb_composite_driver
*driver
)
1644 usb_gadget_unregister_driver(&driver
->gadget_driver
);
1648 * usb_composite_setup_continue() - Continue with the control transfer
1649 * @cdev: the composite device who's control transfer was kept waiting
1651 * This function must be called by the USB function driver to continue
1652 * with the control transfer's data/status stage in case it had requested to
1653 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1654 * can request the composite framework to delay the setup request's data/status
1655 * stages by returning USB_GADGET_DELAYED_STATUS.
1657 void usb_composite_setup_continue(struct usb_composite_dev
*cdev
)
1660 struct usb_request
*req
= cdev
->req
;
1661 unsigned long flags
;
1663 DBG(cdev
, "%s\n", __func__
);
1664 spin_lock_irqsave(&cdev
->lock
, flags
);
1666 if (cdev
->delayed_status
== 0) {
1667 WARN(cdev
, "%s: Unexpected call\n", __func__
);
1669 } else if (--cdev
->delayed_status
== 0) {
1670 DBG(cdev
, "%s: Completing delayed status\n", __func__
);
1672 value
= usb_ep_queue(cdev
->gadget
->ep0
, req
, GFP_ATOMIC
);
1674 DBG(cdev
, "ep_queue --> %d\n", value
);
1676 composite_setup_complete(cdev
->gadget
->ep0
, req
);
1680 spin_unlock_irqrestore(&cdev
->lock
, flags
);
1683 void usb_composite_overwrite_options(struct usb_composite_dev
*cdev
,
1684 struct usb_composite_overwrite
*covr
)
1686 struct usb_device_descriptor
*desc
= &cdev
->desc
;
1687 struct usb_gadget_strings
*gstr
= cdev
->driver
->strings
[0];
1688 struct usb_string
*dev_str
= gstr
->strings
;
1691 desc
->idVendor
= cpu_to_le16(covr
->idVendor
);
1693 if (covr
->idProduct
)
1694 desc
->idProduct
= cpu_to_le16(covr
->idProduct
);
1696 if (covr
->bcdDevice
)
1697 desc
->bcdDevice
= cpu_to_le16(covr
->bcdDevice
);
1699 if (covr
->serial_number
) {
1700 desc
->iSerialNumber
= dev_str
[USB_GADGET_SERIAL_IDX
].id
;
1701 dev_str
[USB_GADGET_SERIAL_IDX
].s
= covr
->serial_number
;
1703 if (covr
->manufacturer
) {
1704 desc
->iManufacturer
= dev_str
[USB_GADGET_MANUFACTURER_IDX
].id
;
1705 dev_str
[USB_GADGET_MANUFACTURER_IDX
].s
= covr
->manufacturer
;
1708 if (covr
->product
) {
1709 desc
->iProduct
= dev_str
[USB_GADGET_PRODUCT_IDX
].id
;
1710 dev_str
[USB_GADGET_PRODUCT_IDX
].s
= covr
->product
;