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>
27 #include <linux/utsname.h>
29 #include <linux/usb/composite.h>
33 * The code in this file is utility code, used to build a gadget driver
34 * from one or more "function" drivers, one or more "configuration"
35 * objects, and a "usb_composite_driver" by gluing them together along
36 * with the relevant device-wide data.
39 /* big enough to hold our biggest descriptor */
40 #define USB_BUFSIZ 1024
42 static struct usb_composite_driver *composite;
43 static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
45 /* Some systems will need runtime overrides for the product identifers
46 * published in the device descriptor, either numbers or strings or both.
47 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
50 static ushort idVendor;
51 module_param(idVendor, ushort, 0);
52 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
54 static ushort idProduct;
55 module_param(idProduct, ushort, 0);
56 MODULE_PARM_DESC(idProduct, "USB Product ID");
58 static ushort bcdDevice;
59 module_param(bcdDevice, ushort, 0);
60 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
62 static char *iManufacturer;
63 module_param(iManufacturer, charp, 0);
64 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
66 static char *iProduct;
67 module_param(iProduct, charp, 0);
68 MODULE_PARM_DESC(iProduct, "USB Product string");
70 static char *iSerialNumber;
71 module_param(iSerialNumber, charp, 0);
72 MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
74 static char composite_manufacturer[50];
76 /*-------------------------------------------------------------------------*/
79 * usb_add_function() - add a function to a configuration
80 * @config: the configuration
81 * @function: the function being added
82 * Context: single threaded during gadget setup
84 * After initialization, each configuration must have one or more
85 * functions added to it. Adding a function involves calling its @bind()
86 * method to allocate resources such as interface and string identifiers
89 * This function returns the value of the function's bind(), which is
90 * zero for success else a negative errno value.
92 int usb_add_function(struct usb_configuration *config,
93 struct usb_function *function)
97 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
98 function->name, function,
99 config->label, config);
101 if (!function->set_alt || !function->disable)
104 function->config = config;
105 list_add_tail(&function->list, &config->functions);
107 /* REVISIT *require* function->bind? */
108 if (function->bind) {
109 value = function->bind(config, function);
111 list_del(&function->list);
112 function->config = NULL;
117 /* We allow configurations that don't work at both speeds.
118 * If we run into a lowspeed Linux system, treat it the same
119 * as full speed ... it's the function drivers that will need
120 * to avoid bulk and ISO transfers.
122 if (!config->fullspeed && function->descriptors)
123 config->fullspeed = true;
124 if (!config->highspeed && function->hs_descriptors)
125 config->highspeed = true;
129 DBG(config->cdev, "adding '%s'/%p --> %d\n",
130 function->name, function, value);
135 * usb_function_deactivate - prevent function and gadget enumeration
136 * @function: the function that isn't yet ready to respond
138 * Blocks response of the gadget driver to host enumeration by
139 * preventing the data line pullup from being activated. This is
140 * normally called during @bind() processing to change from the
141 * initial "ready to respond" state, or when a required resource
144 * For example, drivers that serve as a passthrough to a userspace
145 * daemon can block enumeration unless that daemon (such as an OBEX,
146 * MTP, or print server) is ready to handle host requests.
148 * Not all systems support software control of their USB peripheral
151 * Returns zero on success, else negative errno.
153 int usb_function_deactivate(struct usb_function *function)
155 struct usb_composite_dev *cdev = function->config->cdev;
159 spin_lock_irqsave(&cdev->lock, flags);
161 if (cdev->deactivations == 0)
162 status = usb_gadget_disconnect(cdev->gadget);
164 cdev->deactivations++;
166 spin_unlock_irqrestore(&cdev->lock, flags);
171 * usb_function_activate - allow function and gadget enumeration
172 * @function: function on which usb_function_activate() was called
174 * Reverses effect of usb_function_deactivate(). If no more functions
175 * are delaying their activation, the gadget driver will respond to
176 * host enumeration procedures.
178 * Returns zero on success, else negative errno.
180 int usb_function_activate(struct usb_function *function)
182 struct usb_composite_dev *cdev = function->config->cdev;
185 spin_lock(&cdev->lock);
187 if (WARN_ON(cdev->deactivations == 0))
190 cdev->deactivations--;
191 if (cdev->deactivations == 0)
192 status = usb_gadget_connect(cdev->gadget);
195 spin_unlock(&cdev->lock);
200 * usb_interface_id() - allocate an unused interface ID
201 * @config: configuration associated with the interface
202 * @function: function handling the interface
203 * Context: single threaded during gadget setup
205 * usb_interface_id() is called from usb_function.bind() callbacks to
206 * allocate new interface IDs. The function driver will then store that
207 * ID in interface, association, CDC union, and other descriptors. It
208 * will also handle any control requests targetted at that interface,
209 * particularly changing its altsetting via set_alt(). There may
210 * also be class-specific or vendor-specific requests to handle.
212 * All interface identifier should be allocated using this routine, to
213 * ensure that for example different functions don't wrongly assign
214 * different meanings to the same identifier. Note that since interface
215 * identifers are configuration-specific, functions used in more than
216 * one configuration (or more than once in a given configuration) need
217 * multiple versions of the relevant descriptors.
219 * Returns the interface ID which was allocated; or -ENODEV if no
220 * more interface IDs can be allocated.
222 int usb_interface_id(struct usb_configuration *config,
223 struct usb_function *function)
225 unsigned id = config->next_interface_id;
227 if (id < MAX_CONFIG_INTERFACES) {
228 config->interface[id] = function;
229 config->next_interface_id = id + 1;
235 static int config_buf(struct usb_configuration *config,
236 enum usb_device_speed speed, void *buf, u8 type)
238 struct usb_config_descriptor *c = buf;
239 void *next = buf + USB_DT_CONFIG_SIZE;
240 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
241 struct usb_function *f;
244 /* write the config descriptor */
246 c->bLength = USB_DT_CONFIG_SIZE;
247 c->bDescriptorType = type;
248 /* wTotalLength is written later */
249 c->bNumInterfaces = config->next_interface_id;
250 c->bConfigurationValue = config->bConfigurationValue;
251 c->iConfiguration = config->iConfiguration;
252 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
253 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
255 /* There may be e.g. OTG descriptors */
256 if (config->descriptors) {
257 status = usb_descriptor_fillbuf(next, len,
258 config->descriptors);
265 /* add each function's descriptors */
266 list_for_each_entry(f, &config->functions, list) {
267 struct usb_descriptor_header **descriptors;
269 if (speed == USB_SPEED_HIGH)
270 descriptors = f->hs_descriptors;
272 descriptors = f->descriptors;
275 status = usb_descriptor_fillbuf(next, len,
276 (const struct usb_descriptor_header **) descriptors);
284 c->wTotalLength = cpu_to_le16(len);
288 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
290 struct usb_gadget *gadget = cdev->gadget;
291 struct usb_configuration *c;
292 u8 type = w_value >> 8;
293 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
295 if (gadget_is_dualspeed(gadget)) {
298 if (gadget->speed == USB_SPEED_HIGH)
300 if (type == USB_DT_OTHER_SPEED_CONFIG)
303 speed = USB_SPEED_HIGH;
307 /* This is a lookup by config *INDEX* */
309 list_for_each_entry(c, &cdev->configs, list) {
310 /* ignore configs that won't work at this speed */
311 if (speed == USB_SPEED_HIGH) {
319 return config_buf(c, speed, cdev->req->buf, type);
325 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
327 struct usb_gadget *gadget = cdev->gadget;
328 struct usb_configuration *c;
332 if (gadget_is_dualspeed(gadget)) {
333 if (gadget->speed == USB_SPEED_HIGH)
335 if (type == USB_DT_DEVICE_QUALIFIER)
338 list_for_each_entry(c, &cdev->configs, list) {
339 /* ignore configs that won't work at this speed */
352 static void device_qual(struct usb_composite_dev *cdev)
354 struct usb_qualifier_descriptor *qual = cdev->req->buf;
356 qual->bLength = sizeof(*qual);
357 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
358 /* POLICY: same bcdUSB and device type info at both speeds */
359 qual->bcdUSB = cdev->desc.bcdUSB;
360 qual->bDeviceClass = cdev->desc.bDeviceClass;
361 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
362 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
363 /* ASSUME same EP0 fifo size at both speeds */
364 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
365 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
369 /*-------------------------------------------------------------------------*/
371 static void reset_config(struct usb_composite_dev *cdev)
373 struct usb_function *f;
375 DBG(cdev, "reset config\n");
377 list_for_each_entry(f, &cdev->config->functions, list) {
381 bitmap_zero(f->endpoints, 32);
386 static int set_config(struct usb_composite_dev *cdev,
387 const struct usb_ctrlrequest *ctrl, unsigned number)
389 struct usb_gadget *gadget = cdev->gadget;
390 struct usb_configuration *c = NULL;
391 int result = -EINVAL;
392 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
399 list_for_each_entry(c, &cdev->configs, list) {
400 if (c->bConfigurationValue == number) {
410 INFO(cdev, "%s speed config #%d: %s\n",
412 switch (gadget->speed) {
413 case USB_SPEED_LOW: speed = "low"; break;
414 case USB_SPEED_FULL: speed = "full"; break;
415 case USB_SPEED_HIGH: speed = "high"; break;
416 default: speed = "?"; break;
417 } ; speed; }), number, c ? c->label : "unconfigured");
424 /* Initialize all interfaces by setting them to altsetting zero. */
425 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
426 struct usb_function *f = c->interface[tmp];
427 struct usb_descriptor_header **descriptors;
433 * Record which endpoints are used by the function. This is used
434 * to dispatch control requests targeted at that endpoint to the
435 * function's setup callback instead of the current
436 * configuration's setup callback.
438 if (gadget->speed == USB_SPEED_HIGH)
439 descriptors = f->hs_descriptors;
441 descriptors = f->descriptors;
443 for (; *descriptors; ++descriptors) {
444 struct usb_endpoint_descriptor *ep;
447 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
450 ep = (struct usb_endpoint_descriptor *)*descriptors;
451 addr = ((ep->bEndpointAddress & 0x80) >> 3)
452 | (ep->bEndpointAddress & 0x0f);
453 set_bit(addr, f->endpoints);
456 result = f->set_alt(f, tmp, 0);
458 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
459 tmp, f->name, f, result);
466 /* when we return, be sure our power usage is valid */
467 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
469 usb_gadget_vbus_draw(gadget, power);
474 * usb_add_config() - add a configuration to a device.
475 * @cdev: wraps the USB gadget
476 * @config: the configuration, with bConfigurationValue assigned
477 * Context: single threaded during gadget setup
479 * One of the main tasks of a composite driver's bind() routine is to
480 * add each of the configurations it supports, using this routine.
482 * This function returns the value of the configuration's bind(), which
483 * is zero for success else a negative errno value. Binding configurations
484 * assigns global resources including string IDs, and per-configuration
485 * resources such as interface IDs and endpoints.
487 int usb_add_config(struct usb_composite_dev *cdev,
488 struct usb_configuration *config)
490 int status = -EINVAL;
491 struct usb_configuration *c;
493 DBG(cdev, "adding config #%u '%s'/%p\n",
494 config->bConfigurationValue,
495 config->label, config);
497 if (!config->bConfigurationValue || !config->bind)
500 /* Prevent duplicate configuration identifiers */
501 list_for_each_entry(c, &cdev->configs, list) {
502 if (c->bConfigurationValue == config->bConfigurationValue) {
509 list_add_tail(&config->list, &cdev->configs);
511 INIT_LIST_HEAD(&config->functions);
512 config->next_interface_id = 0;
514 status = config->bind(config);
516 list_del(&config->list);
521 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
522 config->bConfigurationValue, config,
523 config->highspeed ? " high" : "",
525 ? (gadget_is_dualspeed(cdev->gadget)
530 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
531 struct usb_function *f = config->interface[i];
535 DBG(cdev, " interface %d = %s/%p\n",
540 /* set_alt(), or next config->bind(), sets up
541 * ep->driver_data as needed.
543 usb_ep_autoconfig_reset(cdev->gadget);
547 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
548 config->bConfigurationValue, status);
552 /*-------------------------------------------------------------------------*/
554 /* We support strings in multiple languages ... string descriptor zero
555 * says which languages are supported. The typical case will be that
556 * only one language (probably English) is used, with I18N handled on
560 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
562 const struct usb_gadget_strings *s;
568 language = cpu_to_le16(s->language);
569 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
570 if (*tmp == language)
579 static int lookup_string(
580 struct usb_gadget_strings **sp,
586 struct usb_gadget_strings *s;
591 if (s->language != language)
593 value = usb_gadget_get_string(s, id, buf);
600 static int get_string(struct usb_composite_dev *cdev,
601 void *buf, u16 language, int id)
603 struct usb_configuration *c;
604 struct usb_function *f;
608 /* Yes, not only is USB's I18N support probably more than most
609 * folk will ever care about ... also, it's all supported here.
610 * (Except for UTF8 support for Unicode's "Astral Planes".)
613 /* 0 == report all available language codes */
615 struct usb_string_descriptor *s = buf;
616 struct usb_gadget_strings **sp;
619 s->bDescriptorType = USB_DT_STRING;
621 sp = composite->strings;
623 collect_langs(sp, s->wData);
625 list_for_each_entry(c, &cdev->configs, list) {
628 collect_langs(sp, s->wData);
630 list_for_each_entry(f, &c->functions, list) {
633 collect_langs(sp, s->wData);
637 for (len = 0; len <= 126 && s->wData[len]; len++)
642 s->bLength = 2 * (len + 1);
646 /* Otherwise, look up and return a specified string. First
647 * check if the string has not been overridden.
649 if (cdev->manufacturer_override == id)
650 str = iManufacturer ?: composite->iManufacturer ?:
651 composite_manufacturer;
652 else if (cdev->product_override == id)
653 str = iProduct ?: composite->iProduct;
654 else if (cdev->serial_override == id)
659 struct usb_gadget_strings strings = {
660 .language = language,
661 .strings = &(struct usb_string) { 0xff, str }
663 return usb_gadget_get_string(&strings, 0xff, buf);
666 /* String IDs are device-scoped, so we look up each string
667 * table we're told about. These lookups are infrequent;
668 * simpler-is-better here.
670 if (composite->strings) {
671 len = lookup_string(composite->strings, buf, language, id);
675 list_for_each_entry(c, &cdev->configs, list) {
677 len = lookup_string(c->strings, buf, language, id);
681 list_for_each_entry(f, &c->functions, list) {
684 len = lookup_string(f->strings, buf, language, id);
693 * usb_string_id() - allocate an unused string ID
694 * @cdev: the device whose string descriptor IDs are being allocated
695 * Context: single threaded during gadget setup
697 * @usb_string_id() is called from bind() callbacks to allocate
698 * string IDs. Drivers for functions, configurations, or gadgets will
699 * then store that ID in the appropriate descriptors and string table.
701 * All string identifier should be allocated using this,
702 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
703 * that for example different functions don't wrongly assign different
704 * meanings to the same identifier.
706 int usb_string_id(struct usb_composite_dev *cdev)
708 if (cdev->next_string_id < 254) {
709 /* string id 0 is reserved by USB spec for list of
710 * supported languages */
711 /* 255 reserved as well? -- mina86 */
712 cdev->next_string_id++;
713 return cdev->next_string_id;
719 * usb_string_ids() - allocate unused string IDs in batch
720 * @cdev: the device whose string descriptor IDs are being allocated
721 * @str: an array of usb_string objects to assign numbers to
722 * Context: single threaded during gadget setup
724 * @usb_string_ids() is called from bind() callbacks to allocate
725 * string IDs. Drivers for functions, configurations, or gadgets will
726 * then copy IDs from the string table to the appropriate descriptors
727 * and string table for other languages.
729 * All string identifier should be allocated using this,
730 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
731 * example different functions don't wrongly assign different meanings
732 * to the same identifier.
734 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
736 int next = cdev->next_string_id;
738 for (; str->s; ++str) {
739 if (unlikely(next >= 254))
744 cdev->next_string_id = next;
750 * usb_string_ids_n() - allocate unused string IDs in batch
751 * @c: the device whose string descriptor IDs are being allocated
752 * @n: number of string IDs to allocate
753 * Context: single threaded during gadget setup
755 * Returns the first requested ID. This ID and next @n-1 IDs are now
756 * valid IDs. At least provided that @n is non-zero because if it
757 * is, returns last requested ID which is now very useful information.
759 * @usb_string_ids_n() is called from bind() callbacks to allocate
760 * string IDs. Drivers for functions, configurations, or gadgets will
761 * then store that ID in the appropriate descriptors and string table.
763 * All string identifier should be allocated using this,
764 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
765 * example different functions don't wrongly assign different meanings
766 * to the same identifier.
768 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
770 unsigned next = c->next_string_id;
771 if (unlikely(n > 254 || (unsigned)next + n > 254))
773 c->next_string_id += n;
778 /*-------------------------------------------------------------------------*/
780 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
782 if (req->status || req->actual != req->length)
783 DBG((struct usb_composite_dev *) ep->driver_data,
784 "setup complete --> %d, %d/%d\n",
785 req->status, req->actual, req->length);
789 * The setup() callback implements all the ep0 functionality that's
790 * not handled lower down, in hardware or the hardware driver(like
791 * device and endpoint feature flags, and their status). It's all
792 * housekeeping for the gadget function we're implementing. Most of
793 * the work is in config and function specific setup.
796 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
798 struct usb_composite_dev *cdev = get_gadget_data(gadget);
799 struct usb_request *req = cdev->req;
800 int value = -EOPNOTSUPP;
801 u16 w_index = le16_to_cpu(ctrl->wIndex);
802 u8 intf = w_index & 0xFF;
803 u16 w_value = le16_to_cpu(ctrl->wValue);
804 u16 w_length = le16_to_cpu(ctrl->wLength);
805 struct usb_function *f = NULL;
808 /* partial re-init of the response message; the function or the
809 * gadget might need to intercept e.g. a control-OUT completion
810 * when we delegate to it.
813 req->complete = composite_setup_complete;
814 req->length = USB_BUFSIZ;
815 gadget->ep0->driver_data = cdev;
817 switch (ctrl->bRequest) {
819 /* we handle all standard USB descriptors */
820 case USB_REQ_GET_DESCRIPTOR:
821 if (ctrl->bRequestType != USB_DIR_IN)
823 switch (w_value >> 8) {
826 cdev->desc.bNumConfigurations =
827 count_configs(cdev, USB_DT_DEVICE);
828 value = min(w_length, (u16) sizeof cdev->desc);
829 memcpy(req->buf, &cdev->desc, value);
831 case USB_DT_DEVICE_QUALIFIER:
832 if (!gadget_is_dualspeed(gadget))
835 value = min_t(int, w_length,
836 sizeof(struct usb_qualifier_descriptor));
838 case USB_DT_OTHER_SPEED_CONFIG:
839 if (!gadget_is_dualspeed(gadget))
843 value = config_desc(cdev, w_value);
845 value = min(w_length, (u16) value);
848 value = get_string(cdev, req->buf,
849 w_index, w_value & 0xff);
851 value = min(w_length, (u16) value);
856 /* any number of configs can work */
857 case USB_REQ_SET_CONFIGURATION:
858 if (ctrl->bRequestType != 0)
860 if (gadget_is_otg(gadget)) {
861 if (gadget->a_hnp_support)
862 DBG(cdev, "HNP available\n");
863 else if (gadget->a_alt_hnp_support)
864 DBG(cdev, "HNP on another port\n");
866 VDBG(cdev, "HNP inactive\n");
868 spin_lock(&cdev->lock);
869 value = set_config(cdev, ctrl, w_value);
870 spin_unlock(&cdev->lock);
872 case USB_REQ_GET_CONFIGURATION:
873 if (ctrl->bRequestType != USB_DIR_IN)
876 *(u8 *)req->buf = cdev->config->bConfigurationValue;
879 value = min(w_length, (u16) 1);
882 /* function drivers must handle get/set altsetting; if there's
883 * no get() method, we know only altsetting zero works.
885 case USB_REQ_SET_INTERFACE:
886 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
888 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
890 f = cdev->config->interface[intf];
893 if (w_value && !f->set_alt)
895 value = f->set_alt(f, w_index, w_value);
897 case USB_REQ_GET_INTERFACE:
898 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
900 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
902 f = cdev->config->interface[intf];
905 /* lots of interfaces only need altsetting zero... */
906 value = f->get_alt ? f->get_alt(f, w_index) : 0;
909 *((u8 *)req->buf) = value;
910 value = min(w_length, (u16) 1);
915 "non-core control req%02x.%02x v%04x i%04x l%d\n",
916 ctrl->bRequestType, ctrl->bRequest,
917 w_value, w_index, w_length);
919 /* functions always handle their interfaces and endpoints...
920 * punt other recipients (other, WUSB, ...) to the current
921 * configuration code.
923 * REVISIT it could make sense to let the composite device
924 * take such requests too, if that's ever needed: to work
927 switch (ctrl->bRequestType & USB_RECIP_MASK) {
928 case USB_RECIP_INTERFACE:
929 f = cdev->config->interface[intf];
932 case USB_RECIP_ENDPOINT:
933 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
934 list_for_each_entry(f, &cdev->config->functions, list) {
935 if (test_bit(endp, f->endpoints))
938 if (&f->list == &cdev->config->functions)
944 value = f->setup(f, ctrl);
946 struct usb_configuration *c;
950 value = c->setup(c, ctrl);
956 /* respond with data transfer before status phase? */
959 req->zero = value < w_length;
960 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
962 DBG(cdev, "ep_queue --> %d\n", value);
964 composite_setup_complete(gadget->ep0, req);
969 /* device either stalls (value < 0) or reports success */
973 static void composite_disconnect(struct usb_gadget *gadget)
975 struct usb_composite_dev *cdev = get_gadget_data(gadget);
978 /* REVISIT: should we have config and device level
979 * disconnect callbacks?
981 spin_lock_irqsave(&cdev->lock, flags);
984 if (composite->disconnect)
985 composite->disconnect(cdev);
986 spin_unlock_irqrestore(&cdev->lock, flags);
989 /*-------------------------------------------------------------------------*/
991 static ssize_t composite_show_suspended(struct device *dev,
992 struct device_attribute *attr,
995 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
996 struct usb_composite_dev *cdev = get_gadget_data(gadget);
998 return sprintf(buf, "%d\n", cdev->suspended);
1001 static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1004 composite_unbind(struct usb_gadget *gadget)
1006 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1008 /* composite_disconnect() must already have been called
1009 * by the underlying peripheral controller driver!
1010 * so there's no i/o concurrency that could affect the
1011 * state protected by cdev->lock.
1013 WARN_ON(cdev->config);
1015 while (!list_empty(&cdev->configs)) {
1016 struct usb_configuration *c;
1018 c = list_first_entry(&cdev->configs,
1019 struct usb_configuration, list);
1020 while (!list_empty(&c->functions)) {
1021 struct usb_function *f;
1023 f = list_first_entry(&c->functions,
1024 struct usb_function, list);
1027 DBG(cdev, "unbind function '%s'/%p\n",
1030 /* may free memory for "f" */
1035 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1037 /* may free memory for "c" */
1040 if (composite->unbind)
1041 composite->unbind(cdev);
1044 kfree(cdev->req->buf);
1045 usb_ep_free_request(gadget->ep0, cdev->req);
1048 set_gadget_data(gadget, NULL);
1049 device_remove_file(&gadget->dev, &dev_attr_suspended);
1053 static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
1056 int ret = usb_string_id(cdev);
1057 if (unlikely(ret < 0))
1058 WARNING(cdev, "failed to override string ID\n");
1066 static int composite_bind(struct usb_gadget *gadget)
1068 struct usb_composite_dev *cdev;
1069 int status = -ENOMEM;
1071 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1075 spin_lock_init(&cdev->lock);
1076 cdev->gadget = gadget;
1077 set_gadget_data(gadget, cdev);
1078 INIT_LIST_HEAD(&cdev->configs);
1080 /* preallocate control response and buffer */
1081 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1084 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1085 if (!cdev->req->buf)
1087 cdev->req->complete = composite_setup_complete;
1088 gadget->ep0->driver_data = cdev;
1090 cdev->bufsiz = USB_BUFSIZ;
1091 cdev->driver = composite;
1094 * As per USB compliance update, a device that is actively drawing
1095 * more than 100mA from USB must report itself as bus-powered in
1096 * the GetStatus(DEVICE) call.
1098 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1099 usb_gadget_set_selfpowered(gadget);
1101 /* interface and string IDs start at zero via kzalloc.
1102 * we force endpoints to start unassigned; few controller
1103 * drivers will zero ep->driver_data.
1105 usb_ep_autoconfig_reset(cdev->gadget);
1107 /* standardized runtime overrides for device ID data */
1109 cdev->desc.idVendor = cpu_to_le16(idVendor);
1111 cdev->desc.idProduct = cpu_to_le16(idProduct);
1113 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1115 /* composite gadget needs to assign strings for whole device (like
1116 * serial number), register function drivers, potentially update
1117 * power state and consumption, etc
1119 status = composite_gadget_bind(cdev);
1123 cdev->desc = *composite->dev;
1124 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1126 /* stirng overrides */
1127 if (iManufacturer || !cdev->desc.iManufacturer) {
1128 if (!iManufacturer && !composite->iManufacturer &&
1129 !*composite_manufacturer)
1130 snprintf(composite_manufacturer,
1131 sizeof composite_manufacturer,
1133 init_utsname()->sysname,
1134 init_utsname()->release,
1137 cdev->manufacturer_override =
1138 override_id(cdev, &cdev->desc.iManufacturer);
1141 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1142 cdev->product_override =
1143 override_id(cdev, &cdev->desc.iProduct);
1146 cdev->serial_override =
1147 override_id(cdev, &cdev->desc.iSerialNumber);
1149 /* has userspace failed to provide a serial number? */
1150 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1151 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1154 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1158 INFO(cdev, "%s ready\n", composite->name);
1162 composite_unbind(gadget);
1166 /*-------------------------------------------------------------------------*/
1169 composite_suspend(struct usb_gadget *gadget)
1171 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1172 struct usb_function *f;
1174 /* REVISIT: should we have config level
1175 * suspend/resume callbacks?
1177 DBG(cdev, "suspend\n");
1179 list_for_each_entry(f, &cdev->config->functions, list) {
1184 if (composite->suspend)
1185 composite->suspend(cdev);
1187 cdev->suspended = 1;
1191 composite_resume(struct usb_gadget *gadget)
1193 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1194 struct usb_function *f;
1196 /* REVISIT: should we have config level
1197 * suspend/resume callbacks?
1199 DBG(cdev, "resume\n");
1200 if (composite->resume)
1201 composite->resume(cdev);
1203 list_for_each_entry(f, &cdev->config->functions, list) {
1209 cdev->suspended = 0;
1212 /*-------------------------------------------------------------------------*/
1214 static struct usb_gadget_driver composite_driver = {
1215 .speed = USB_SPEED_HIGH,
1217 .unbind = composite_unbind,
1219 .setup = composite_setup,
1220 .disconnect = composite_disconnect,
1222 .suspend = composite_suspend,
1223 .resume = composite_resume,
1226 .owner = THIS_MODULE,
1231 * usb_composite_probe() - register a composite driver
1232 * @driver: the driver to register
1233 * @bind: the callback used to allocate resources that are shared across the
1234 * whole device, such as string IDs, and add its configurations using
1235 * @usb_add_config(). This may fail by returning a negative errno
1236 * value; it should return zero on successful initialization.
1237 * Context: single threaded during gadget setup
1239 * This function is used to register drivers using the composite driver
1240 * framework. The return value is zero, or a negative errno value.
1241 * Those values normally come from the driver's @bind method, which does
1242 * all the work of setting up the driver to match the hardware.
1244 * On successful return, the gadget is ready to respond to requests from
1245 * the host, unless one of its components invokes usb_gadget_disconnect()
1246 * while it was binding. That would usually be done in order to wait for
1247 * some userspace participation.
1249 extern int usb_composite_probe(struct usb_composite_driver *driver,
1250 int (*bind)(struct usb_composite_dev *cdev))
1252 if (!driver || !driver->dev || !bind || composite)
1255 if (!driver->iProduct)
1256 driver->iProduct = driver->name;
1258 driver->name = "composite";
1259 composite_driver.function = (char *) driver->name;
1260 composite_driver.driver.name = driver->name;
1262 composite_gadget_bind = bind;
1264 return usb_gadget_probe_driver(&composite_driver, composite_bind);
1268 * usb_composite_unregister() - unregister a composite driver
1269 * @driver: the driver to unregister
1271 * This function is used to unregister drivers using the composite
1274 void usb_composite_unregister(struct usb_composite_driver *driver)
1276 if (composite != driver)
1278 usb_gadget_unregister_driver(&composite_driver);