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.
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.
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
21 /* #define VERBOSE_DEBUG */
23 #include <linux/kallsyms.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/device.h>
28 #include <linux/usb/composite.h>
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.
38 /* big enough to hold our biggest descriptor */
39 #define USB_BUFSIZ 512
41 static struct usb_composite_driver
*composite
;
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).
48 static ushort idVendor
;
49 module_param(idVendor
, ushort
, 0);
50 MODULE_PARM_DESC(idVendor
, "USB Vendor ID");
52 static ushort idProduct
;
53 module_param(idProduct
, ushort
, 0);
54 MODULE_PARM_DESC(idProduct
, "USB Product ID");
56 static ushort bcdDevice
;
57 module_param(bcdDevice
, ushort
, 0);
58 MODULE_PARM_DESC(bcdDevice
, "USB Device version (BCD)");
60 static char *iManufacturer
;
61 module_param(iManufacturer
, charp
, 0);
62 MODULE_PARM_DESC(iManufacturer
, "USB Manufacturer string");
64 static char *iProduct
;
65 module_param(iProduct
, charp
, 0);
66 MODULE_PARM_DESC(iProduct
, "USB Product string");
68 static char *iSerialNumber
;
69 module_param(iSerialNumber
, charp
, 0);
70 MODULE_PARM_DESC(iSerialNumber
, "SerialNumber string");
72 /*-------------------------------------------------------------------------*/
75 * usb_add_function() - add a function to a configuration
76 * @config: the configuration
77 * @function: the function being added
78 * Context: single threaded during gadget setup
80 * After initialization, each configuration must have one or more
81 * functions added to it. Adding a function involves calling its @bind()
82 * method to allocate resources such as interface and string identifiers
85 * This function returns the value of the function's bind(), which is
86 * zero for success else a negative errno value.
88 int __init
usb_add_function(struct usb_configuration
*config
,
89 struct usb_function
*function
)
93 DBG(config
->cdev
, "adding '%s'/%p to config '%s'/%p\n",
94 function
->name
, function
,
95 config
->label
, config
);
97 if (!function
->set_alt
|| !function
->disable
)
100 function
->config
= config
;
101 list_add_tail(&function
->list
, &config
->functions
);
103 /* REVISIT *require* function->bind? */
104 if (function
->bind
) {
105 value
= function
->bind(config
, function
);
107 list_del(&function
->list
);
108 function
->config
= NULL
;
113 /* We allow configurations that don't work at both speeds.
114 * If we run into a lowspeed Linux system, treat it the same
115 * as full speed ... it's the function drivers that will need
116 * to avoid bulk and ISO transfers.
118 if (!config
->fullspeed
&& function
->descriptors
)
119 config
->fullspeed
= true;
120 if (!config
->highspeed
&& function
->hs_descriptors
)
121 config
->highspeed
= true;
125 DBG(config
->cdev
, "adding '%s'/%p --> %d\n",
126 function
->name
, function
, value
);
131 * usb_function_deactivate - prevent function and gadget enumeration
132 * @function: the function that isn't yet ready to respond
134 * Blocks response of the gadget driver to host enumeration by
135 * preventing the data line pullup from being activated. This is
136 * normally called during @bind() processing to change from the
137 * initial "ready to respond" state, or when a required resource
140 * For example, drivers that serve as a passthrough to a userspace
141 * daemon can block enumeration unless that daemon (such as an OBEX,
142 * MTP, or print server) is ready to handle host requests.
144 * Not all systems support software control of their USB peripheral
147 * Returns zero on success, else negative errno.
149 int usb_function_deactivate(struct usb_function
*function
)
151 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
155 spin_lock_irqsave(&cdev
->lock
, flags
);
157 if (cdev
->deactivations
== 0)
158 status
= usb_gadget_disconnect(cdev
->gadget
);
160 cdev
->deactivations
++;
162 spin_unlock_irqrestore(&cdev
->lock
, flags
);
167 * usb_function_activate - allow function and gadget enumeration
168 * @function: function on which usb_function_activate() was called
170 * Reverses effect of usb_function_deactivate(). If no more functions
171 * are delaying their activation, the gadget driver will respond to
172 * host enumeration procedures.
174 * Returns zero on success, else negative errno.
176 int usb_function_activate(struct usb_function
*function
)
178 struct usb_composite_dev
*cdev
= function
->config
->cdev
;
181 spin_lock(&cdev
->lock
);
183 if (WARN_ON(cdev
->deactivations
== 0))
186 cdev
->deactivations
--;
187 if (cdev
->deactivations
== 0)
188 status
= usb_gadget_connect(cdev
->gadget
);
191 spin_unlock(&cdev
->lock
);
196 * usb_interface_id() - allocate an unused interface ID
197 * @config: configuration associated with the interface
198 * @function: function handling the interface
199 * Context: single threaded during gadget setup
201 * usb_interface_id() is called from usb_function.bind() callbacks to
202 * allocate new interface IDs. The function driver will then store that
203 * ID in interface, association, CDC union, and other descriptors. It
204 * will also handle any control requests targetted at that interface,
205 * particularly changing its altsetting via set_alt(). There may
206 * also be class-specific or vendor-specific requests to handle.
208 * All interface identifier should be allocated using this routine, to
209 * ensure that for example different functions don't wrongly assign
210 * different meanings to the same identifier. Note that since interface
211 * identifers are configuration-specific, functions used in more than
212 * one configuration (or more than once in a given configuration) need
213 * multiple versions of the relevant descriptors.
215 * Returns the interface ID which was allocated; or -ENODEV if no
216 * more interface IDs can be allocated.
218 int __init
usb_interface_id(struct usb_configuration
*config
,
219 struct usb_function
*function
)
221 unsigned id
= config
->next_interface_id
;
223 if (id
< MAX_CONFIG_INTERFACES
) {
224 config
->interface
[id
] = function
;
225 config
->next_interface_id
= id
+ 1;
231 static int config_buf(struct usb_configuration
*config
,
232 enum usb_device_speed speed
, void *buf
, u8 type
)
234 struct usb_config_descriptor
*c
= buf
;
235 void *next
= buf
+ USB_DT_CONFIG_SIZE
;
236 int len
= USB_BUFSIZ
- USB_DT_CONFIG_SIZE
;
237 struct usb_function
*f
;
240 /* write the config descriptor */
242 c
->bLength
= USB_DT_CONFIG_SIZE
;
243 c
->bDescriptorType
= type
;
244 /* wTotalLength is written later */
245 c
->bNumInterfaces
= config
->next_interface_id
;
246 c
->bConfigurationValue
= config
->bConfigurationValue
;
247 c
->iConfiguration
= config
->iConfiguration
;
248 c
->bmAttributes
= USB_CONFIG_ATT_ONE
| config
->bmAttributes
;
249 c
->bMaxPower
= config
->bMaxPower
? : (CONFIG_USB_GADGET_VBUS_DRAW
/ 2);
251 /* There may be e.g. OTG descriptors */
252 if (config
->descriptors
) {
253 status
= usb_descriptor_fillbuf(next
, len
,
254 config
->descriptors
);
261 /* add each function's descriptors */
262 list_for_each_entry(f
, &config
->functions
, list
) {
263 struct usb_descriptor_header
**descriptors
;
265 if (speed
== USB_SPEED_HIGH
)
266 descriptors
= f
->hs_descriptors
;
268 descriptors
= f
->descriptors
;
271 status
= usb_descriptor_fillbuf(next
, len
,
272 (const struct usb_descriptor_header
**) descriptors
);
280 c
->wTotalLength
= cpu_to_le16(len
);
284 static int config_desc(struct usb_composite_dev
*cdev
, unsigned w_value
)
286 struct usb_gadget
*gadget
= cdev
->gadget
;
287 struct usb_configuration
*c
;
288 u8 type
= w_value
>> 8;
289 enum usb_device_speed speed
= USB_SPEED_UNKNOWN
;
291 if (gadget_is_dualspeed(gadget
)) {
294 if (gadget
->speed
== USB_SPEED_HIGH
)
296 if (type
== USB_DT_OTHER_SPEED_CONFIG
)
299 speed
= USB_SPEED_HIGH
;
303 /* This is a lookup by config *INDEX* */
305 list_for_each_entry(c
, &cdev
->configs
, list
) {
306 /* ignore configs that won't work at this speed */
307 if (speed
== USB_SPEED_HIGH
) {
315 return config_buf(c
, speed
, cdev
->req
->buf
, type
);
321 static int count_configs(struct usb_composite_dev
*cdev
, unsigned type
)
323 struct usb_gadget
*gadget
= cdev
->gadget
;
324 struct usb_configuration
*c
;
328 if (gadget_is_dualspeed(gadget
)) {
329 if (gadget
->speed
== USB_SPEED_HIGH
)
331 if (type
== USB_DT_DEVICE_QUALIFIER
)
334 list_for_each_entry(c
, &cdev
->configs
, list
) {
335 /* ignore configs that won't work at this speed */
348 static void device_qual(struct usb_composite_dev
*cdev
)
350 struct usb_qualifier_descriptor
*qual
= cdev
->req
->buf
;
352 qual
->bLength
= sizeof(*qual
);
353 qual
->bDescriptorType
= USB_DT_DEVICE_QUALIFIER
;
354 /* POLICY: same bcdUSB and device type info at both speeds */
355 qual
->bcdUSB
= cdev
->desc
.bcdUSB
;
356 qual
->bDeviceClass
= cdev
->desc
.bDeviceClass
;
357 qual
->bDeviceSubClass
= cdev
->desc
.bDeviceSubClass
;
358 qual
->bDeviceProtocol
= cdev
->desc
.bDeviceProtocol
;
359 /* ASSUME same EP0 fifo size at both speeds */
360 qual
->bMaxPacketSize0
= cdev
->desc
.bMaxPacketSize0
;
361 qual
->bNumConfigurations
= count_configs(cdev
, USB_DT_DEVICE_QUALIFIER
);
365 /*-------------------------------------------------------------------------*/
367 static void reset_config(struct usb_composite_dev
*cdev
)
369 struct usb_function
*f
;
371 DBG(cdev
, "reset config\n");
373 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
380 static int set_config(struct usb_composite_dev
*cdev
,
381 const struct usb_ctrlrequest
*ctrl
, unsigned number
)
383 struct usb_gadget
*gadget
= cdev
->gadget
;
384 struct usb_configuration
*c
= NULL
;
385 int result
= -EINVAL
;
386 unsigned power
= gadget_is_otg(gadget
) ? 8 : 100;
393 list_for_each_entry(c
, &cdev
->configs
, list
) {
394 if (c
->bConfigurationValue
== number
) {
404 INFO(cdev
, "%s speed config #%d: %s\n",
406 switch (gadget
->speed
) {
407 case USB_SPEED_LOW
: speed
= "low"; break;
408 case USB_SPEED_FULL
: speed
= "full"; break;
409 case USB_SPEED_HIGH
: speed
= "high"; break;
410 default: speed
= "?"; break;
411 } ; speed
; }), number
, c
? c
->label
: "unconfigured");
418 /* Initialize all interfaces by setting them to altsetting zero. */
419 for (tmp
= 0; tmp
< MAX_CONFIG_INTERFACES
; tmp
++) {
420 struct usb_function
*f
= c
->interface
[tmp
];
425 result
= f
->set_alt(f
, tmp
, 0);
427 DBG(cdev
, "interface %d (%s/%p) alt 0 --> %d\n",
428 tmp
, f
->name
, f
, result
);
435 /* when we return, be sure our power usage is valid */
436 power
= c
->bMaxPower
? (2 * c
->bMaxPower
) : CONFIG_USB_GADGET_VBUS_DRAW
;
438 usb_gadget_vbus_draw(gadget
, power
);
443 * usb_add_config() - add a configuration to a device.
444 * @cdev: wraps the USB gadget
445 * @config: the configuration, with bConfigurationValue assigned
446 * Context: single threaded during gadget setup
448 * One of the main tasks of a composite driver's bind() routine is to
449 * add each of the configurations it supports, using this routine.
451 * This function returns the value of the configuration's bind(), which
452 * is zero for success else a negative errno value. Binding configurations
453 * assigns global resources including string IDs, and per-configuration
454 * resources such as interface IDs and endpoints.
456 int __init
usb_add_config(struct usb_composite_dev
*cdev
,
457 struct usb_configuration
*config
)
459 int status
= -EINVAL
;
460 struct usb_configuration
*c
;
462 DBG(cdev
, "adding config #%u '%s'/%p\n",
463 config
->bConfigurationValue
,
464 config
->label
, config
);
466 if (!config
->bConfigurationValue
|| !config
->bind
)
469 /* Prevent duplicate configuration identifiers */
470 list_for_each_entry(c
, &cdev
->configs
, list
) {
471 if (c
->bConfigurationValue
== config
->bConfigurationValue
) {
478 list_add_tail(&config
->list
, &cdev
->configs
);
480 INIT_LIST_HEAD(&config
->functions
);
481 config
->next_interface_id
= 0;
483 status
= config
->bind(config
);
485 list_del(&config
->list
);
490 DBG(cdev
, "cfg %d/%p speeds:%s%s\n",
491 config
->bConfigurationValue
, config
,
492 config
->highspeed
? " high" : "",
494 ? (gadget_is_dualspeed(cdev
->gadget
)
499 for (i
= 0; i
< MAX_CONFIG_INTERFACES
; i
++) {
500 struct usb_function
*f
= config
->interface
[i
];
504 DBG(cdev
, " interface %d = %s/%p\n",
509 /* set_alt(), or next config->bind(), sets up
510 * ep->driver_data as needed.
512 usb_ep_autoconfig_reset(cdev
->gadget
);
516 DBG(cdev
, "added config '%s'/%u --> %d\n", config
->label
,
517 config
->bConfigurationValue
, status
);
521 /*-------------------------------------------------------------------------*/
523 /* We support strings in multiple languages ... string descriptor zero
524 * says which languages are supported. The typical case will be that
525 * only one language (probably English) is used, with I18N handled on
529 static void collect_langs(struct usb_gadget_strings
**sp
, __le16
*buf
)
531 const struct usb_gadget_strings
*s
;
537 language
= cpu_to_le16(s
->language
);
538 for (tmp
= buf
; *tmp
&& tmp
< &buf
[126]; tmp
++) {
539 if (*tmp
== language
)
548 static int lookup_string(
549 struct usb_gadget_strings
**sp
,
555 struct usb_gadget_strings
*s
;
560 if (s
->language
!= language
)
562 value
= usb_gadget_get_string(s
, id
, buf
);
569 static int get_string(struct usb_composite_dev
*cdev
,
570 void *buf
, u16 language
, int id
)
572 struct usb_configuration
*c
;
573 struct usb_function
*f
;
576 /* Yes, not only is USB's I18N support probably more than most
577 * folk will ever care about ... also, it's all supported here.
578 * (Except for UTF8 support for Unicode's "Astral Planes".)
581 /* 0 == report all available language codes */
583 struct usb_string_descriptor
*s
= buf
;
584 struct usb_gadget_strings
**sp
;
587 s
->bDescriptorType
= USB_DT_STRING
;
589 sp
= composite
->strings
;
591 collect_langs(sp
, s
->wData
);
593 list_for_each_entry(c
, &cdev
->configs
, list
) {
596 collect_langs(sp
, s
->wData
);
598 list_for_each_entry(f
, &c
->functions
, list
) {
601 collect_langs(sp
, s
->wData
);
605 for (len
= 0; len
<= 126 && s
->wData
[len
]; len
++)
610 s
->bLength
= 2 * (len
+ 1);
614 /* Otherwise, look up and return a specified string. String IDs
615 * are device-scoped, so we look up each string table we're told
616 * about. These lookups are infrequent; simpler-is-better here.
618 if (composite
->strings
) {
619 len
= lookup_string(composite
->strings
, buf
, language
, id
);
623 list_for_each_entry(c
, &cdev
->configs
, list
) {
625 len
= lookup_string(c
->strings
, buf
, language
, id
);
629 list_for_each_entry(f
, &c
->functions
, list
) {
632 len
= lookup_string(f
->strings
, buf
, language
, id
);
641 * usb_string_id() - allocate an unused string ID
642 * @cdev: the device whose string descriptor IDs are being allocated
643 * Context: single threaded during gadget setup
645 * @usb_string_id() is called from bind() callbacks to allocate
646 * string IDs. Drivers for functions, configurations, or gadgets will
647 * then store that ID in the appropriate descriptors and string table.
649 * All string identifier should be allocated using this routine, to
650 * ensure that for example different functions don't wrongly assign
651 * different meanings to the same identifier.
653 int __init
usb_string_id(struct usb_composite_dev
*cdev
)
655 if (cdev
->next_string_id
< 254) {
656 /* string id 0 is reserved */
657 cdev
->next_string_id
++;
658 return cdev
->next_string_id
;
663 /*-------------------------------------------------------------------------*/
665 static void composite_setup_complete(struct usb_ep
*ep
, struct usb_request
*req
)
667 if (req
->status
|| req
->actual
!= req
->length
)
668 DBG((struct usb_composite_dev
*) ep
->driver_data
,
669 "setup complete --> %d, %d/%d\n",
670 req
->status
, req
->actual
, req
->length
);
674 * The setup() callback implements all the ep0 functionality that's
675 * not handled lower down, in hardware or the hardware driver(like
676 * device and endpoint feature flags, and their status). It's all
677 * housekeeping for the gadget function we're implementing. Most of
678 * the work is in config and function specific setup.
681 composite_setup(struct usb_gadget
*gadget
, const struct usb_ctrlrequest
*ctrl
)
683 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
684 struct usb_request
*req
= cdev
->req
;
685 int value
= -EOPNOTSUPP
;
686 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
687 u8 intf
= w_index
& 0xFF;
688 u16 w_value
= le16_to_cpu(ctrl
->wValue
);
689 u16 w_length
= le16_to_cpu(ctrl
->wLength
);
690 struct usb_function
*f
= NULL
;
692 /* partial re-init of the response message; the function or the
693 * gadget might need to intercept e.g. a control-OUT completion
694 * when we delegate to it.
697 req
->complete
= composite_setup_complete
;
698 req
->length
= USB_BUFSIZ
;
699 gadget
->ep0
->driver_data
= cdev
;
701 switch (ctrl
->bRequest
) {
703 /* we handle all standard USB descriptors */
704 case USB_REQ_GET_DESCRIPTOR
:
705 if (ctrl
->bRequestType
!= USB_DIR_IN
)
707 switch (w_value
>> 8) {
710 cdev
->desc
.bNumConfigurations
=
711 count_configs(cdev
, USB_DT_DEVICE
);
712 value
= min(w_length
, (u16
) sizeof cdev
->desc
);
713 memcpy(req
->buf
, &cdev
->desc
, value
);
715 case USB_DT_DEVICE_QUALIFIER
:
716 if (!gadget_is_dualspeed(gadget
))
719 value
= min_t(int, w_length
,
720 sizeof(struct usb_qualifier_descriptor
));
722 case USB_DT_OTHER_SPEED_CONFIG
:
723 if (!gadget_is_dualspeed(gadget
))
727 value
= config_desc(cdev
, w_value
);
729 value
= min(w_length
, (u16
) value
);
732 value
= get_string(cdev
, req
->buf
,
733 w_index
, w_value
& 0xff);
735 value
= min(w_length
, (u16
) value
);
740 /* any number of configs can work */
741 case USB_REQ_SET_CONFIGURATION
:
742 if (ctrl
->bRequestType
!= 0)
744 if (gadget_is_otg(gadget
)) {
745 if (gadget
->a_hnp_support
)
746 DBG(cdev
, "HNP available\n");
747 else if (gadget
->a_alt_hnp_support
)
748 DBG(cdev
, "HNP on another port\n");
750 VDBG(cdev
, "HNP inactive\n");
752 spin_lock(&cdev
->lock
);
753 value
= set_config(cdev
, ctrl
, w_value
);
754 spin_unlock(&cdev
->lock
);
756 case USB_REQ_GET_CONFIGURATION
:
757 if (ctrl
->bRequestType
!= USB_DIR_IN
)
760 *(u8
*)req
->buf
= cdev
->config
->bConfigurationValue
;
763 value
= min(w_length
, (u16
) 1);
766 /* function drivers must handle get/set altsetting; if there's
767 * no get() method, we know only altsetting zero works.
769 case USB_REQ_SET_INTERFACE
:
770 if (ctrl
->bRequestType
!= USB_RECIP_INTERFACE
)
772 if (!cdev
->config
|| w_index
>= MAX_CONFIG_INTERFACES
)
774 f
= cdev
->config
->interface
[intf
];
777 if (w_value
&& !f
->set_alt
)
779 value
= f
->set_alt(f
, w_index
, w_value
);
781 case USB_REQ_GET_INTERFACE
:
782 if (ctrl
->bRequestType
!= (USB_DIR_IN
|USB_RECIP_INTERFACE
))
784 if (!cdev
->config
|| w_index
>= MAX_CONFIG_INTERFACES
)
786 f
= cdev
->config
->interface
[intf
];
789 /* lots of interfaces only need altsetting zero... */
790 value
= f
->get_alt
? f
->get_alt(f
, w_index
) : 0;
793 *((u8
*)req
->buf
) = value
;
794 value
= min(w_length
, (u16
) 1);
799 "non-core control req%02x.%02x v%04x i%04x l%d\n",
800 ctrl
->bRequestType
, ctrl
->bRequest
,
801 w_value
, w_index
, w_length
);
803 /* functions always handle their interfaces ... punt other
804 * recipients (endpoint, other, WUSB, ...) to the current
805 * configuration code.
807 * REVISIT it could make sense to let the composite device
808 * take such requests too, if that's ever needed: to work
811 if ((ctrl
->bRequestType
& USB_RECIP_MASK
)
812 == USB_RECIP_INTERFACE
) {
813 f
= cdev
->config
->interface
[intf
];
815 value
= f
->setup(f
, ctrl
);
819 if (value
< 0 && !f
) {
820 struct usb_configuration
*c
;
824 value
= c
->setup(c
, ctrl
);
830 /* respond with data transfer before status phase? */
833 req
->zero
= value
< w_length
;
834 value
= usb_ep_queue(gadget
->ep0
, req
, GFP_ATOMIC
);
836 DBG(cdev
, "ep_queue --> %d\n", value
);
838 composite_setup_complete(gadget
->ep0
, req
);
843 /* device either stalls (value < 0) or reports success */
847 static void composite_disconnect(struct usb_gadget
*gadget
)
849 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
852 /* REVISIT: should we have config and device level
853 * disconnect callbacks?
855 spin_lock_irqsave(&cdev
->lock
, flags
);
858 spin_unlock_irqrestore(&cdev
->lock
, flags
);
861 /*-------------------------------------------------------------------------*/
863 static void /* __init_or_exit */
864 composite_unbind(struct usb_gadget
*gadget
)
866 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
868 /* composite_disconnect() must already have been called
869 * by the underlying peripheral controller driver!
870 * so there's no i/o concurrency that could affect the
871 * state protected by cdev->lock.
873 WARN_ON(cdev
->config
);
875 while (!list_empty(&cdev
->configs
)) {
876 struct usb_configuration
*c
;
878 c
= list_first_entry(&cdev
->configs
,
879 struct usb_configuration
, list
);
880 while (!list_empty(&c
->functions
)) {
881 struct usb_function
*f
;
883 f
= list_first_entry(&c
->functions
,
884 struct usb_function
, list
);
887 DBG(cdev
, "unbind function '%s'/%p\n",
890 /* may free memory for "f" */
895 DBG(cdev
, "unbind config '%s'/%p\n", c
->label
, c
);
897 /* may free memory for "c" */
900 if (composite
->unbind
)
901 composite
->unbind(cdev
);
904 kfree(cdev
->req
->buf
);
905 usb_ep_free_request(gadget
->ep0
, cdev
->req
);
908 set_gadget_data(gadget
, NULL
);
913 string_override_one(struct usb_gadget_strings
*tab
, u8 id
, const char *s
)
915 struct usb_string
*str
= tab
->strings
;
917 for (str
= tab
->strings
; str
->s
; str
++) {
926 string_override(struct usb_gadget_strings
**tab
, u8 id
, const char *s
)
929 string_override_one(*tab
, id
, s
);
934 static int __init
composite_bind(struct usb_gadget
*gadget
)
936 struct usb_composite_dev
*cdev
;
937 int status
= -ENOMEM
;
939 cdev
= kzalloc(sizeof *cdev
, GFP_KERNEL
);
943 spin_lock_init(&cdev
->lock
);
944 cdev
->gadget
= gadget
;
945 set_gadget_data(gadget
, cdev
);
946 INIT_LIST_HEAD(&cdev
->configs
);
948 /* preallocate control response and buffer */
949 cdev
->req
= usb_ep_alloc_request(gadget
->ep0
, GFP_KERNEL
);
952 cdev
->req
->buf
= kmalloc(USB_BUFSIZ
, GFP_KERNEL
);
955 cdev
->req
->complete
= composite_setup_complete
;
956 gadget
->ep0
->driver_data
= cdev
;
958 cdev
->bufsiz
= USB_BUFSIZ
;
959 cdev
->driver
= composite
;
961 usb_gadget_set_selfpowered(gadget
);
963 /* interface and string IDs start at zero via kzalloc.
964 * we force endpoints to start unassigned; few controller
965 * drivers will zero ep->driver_data.
967 usb_ep_autoconfig_reset(cdev
->gadget
);
969 /* composite gadget needs to assign strings for whole device (like
970 * serial number), register function drivers, potentially update
971 * power state and consumption, etc
973 status
= composite
->bind(cdev
);
977 cdev
->desc
= *composite
->dev
;
978 cdev
->desc
.bMaxPacketSize0
= gadget
->ep0
->maxpacket
;
980 /* standardized runtime overrides for device ID data */
982 cdev
->desc
.idVendor
= cpu_to_le16(idVendor
);
984 cdev
->desc
.idProduct
= cpu_to_le16(idProduct
);
986 cdev
->desc
.bcdDevice
= cpu_to_le16(bcdDevice
);
988 /* strings can't be assigned before bind() allocates the
989 * releavnt identifiers
991 if (cdev
->desc
.iManufacturer
&& iManufacturer
)
992 string_override(composite
->strings
,
993 cdev
->desc
.iManufacturer
, iManufacturer
);
994 if (cdev
->desc
.iProduct
&& iProduct
)
995 string_override(composite
->strings
,
996 cdev
->desc
.iProduct
, iProduct
);
997 if (cdev
->desc
.iSerialNumber
&& iSerialNumber
)
998 string_override(composite
->strings
,
999 cdev
->desc
.iSerialNumber
, iSerialNumber
);
1001 INFO(cdev
, "%s ready\n", composite
->name
);
1005 composite_unbind(gadget
);
1009 /*-------------------------------------------------------------------------*/
1012 composite_suspend(struct usb_gadget
*gadget
)
1014 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1015 struct usb_function
*f
;
1017 /* REVISIT: should we have config level
1018 * suspend/resume callbacks?
1020 DBG(cdev
, "suspend\n");
1022 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1027 if (composite
->suspend
)
1028 composite
->suspend(cdev
);
1032 composite_resume(struct usb_gadget
*gadget
)
1034 struct usb_composite_dev
*cdev
= get_gadget_data(gadget
);
1035 struct usb_function
*f
;
1037 /* REVISIT: should we have config level
1038 * suspend/resume callbacks?
1040 DBG(cdev
, "resume\n");
1041 if (composite
->resume
)
1042 composite
->resume(cdev
);
1044 list_for_each_entry(f
, &cdev
->config
->functions
, list
) {
1051 /*-------------------------------------------------------------------------*/
1053 static struct usb_gadget_driver composite_driver
= {
1054 .speed
= USB_SPEED_HIGH
,
1056 .bind
= composite_bind
,
1057 .unbind
= __exit_p(composite_unbind
),
1059 .setup
= composite_setup
,
1060 .disconnect
= composite_disconnect
,
1062 .suspend
= composite_suspend
,
1063 .resume
= composite_resume
,
1066 .owner
= THIS_MODULE
,
1071 * usb_composite_register() - register a composite driver
1072 * @driver: the driver to register
1073 * Context: single threaded during gadget setup
1075 * This function is used to register drivers using the composite driver
1076 * framework. The return value is zero, or a negative errno value.
1077 * Those values normally come from the driver's @bind method, which does
1078 * all the work of setting up the driver to match the hardware.
1080 * On successful return, the gadget is ready to respond to requests from
1081 * the host, unless one of its components invokes usb_gadget_disconnect()
1082 * while it was binding. That would usually be done in order to wait for
1083 * some userspace participation.
1085 int __init
usb_composite_register(struct usb_composite_driver
*driver
)
1087 if (!driver
|| !driver
->dev
|| !driver
->bind
|| composite
)
1091 driver
->name
= "composite";
1092 composite_driver
.function
= (char *) driver
->name
;
1093 composite_driver
.driver
.name
= driver
->name
;
1096 return usb_gadget_register_driver(&composite_driver
);
1100 * usb_composite_unregister() - unregister a composite driver
1101 * @driver: the driver to unregister
1103 * This function is used to unregister drivers using the composite
1106 void __exit
usb_composite_unregister(struct usb_composite_driver
*driver
)
1108 if (composite
!= driver
)
1110 usb_gadget_unregister_driver(&composite_driver
);