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 /*-------------------------------------------------------------------------*/
74 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
77 struct usb_function *f = dev_get_drvdata(dev);
78 return sprintf(buf, "%d\n", !f->disabled);
81 static ssize_t enable_store(
82 struct device *dev, struct device_attribute *attr,
83 const char *buf, size_t size)
85 struct usb_function *f = dev_get_drvdata(dev);
86 struct usb_composite_driver *driver = f->config->cdev->driver;
89 sscanf(buf, "%d", &value);
90 if (driver->enable_function)
91 driver->enable_function(f, value);
93 usb_function_set_enabled(f, value);
98 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
100 void usb_function_set_enabled(struct usb_function *f, int enabled)
102 f->disabled = !enabled;
103 kobject_uevent(&f->dev->kobj, KOBJ_CHANGE);
107 void usb_composite_force_reset(struct usb_composite_dev *cdev)
111 spin_lock_irqsave(&cdev->lock, flags);
112 /* force reenumeration */
113 if (cdev && cdev->gadget &&
114 cdev->gadget->speed != USB_SPEED_UNKNOWN) {
115 /* avoid sending a disconnect switch event until after we disconnect */
116 cdev->mute_switch = 1;
117 spin_unlock_irqrestore(&cdev->lock, flags);
119 usb_gadget_disconnect(cdev->gadget);
121 usb_gadget_connect(cdev->gadget);
123 spin_unlock_irqrestore(&cdev->lock, flags);
128 * usb_add_function() - add a function to a configuration
129 * @config: the configuration
130 * @function: the function being added
131 * Context: single threaded during gadget setup
133 * After initialization, each configuration must have one or more
134 * functions added to it. Adding a function involves calling its @bind()
135 * method to allocate resources such as interface and string identifiers
138 * This function returns the value of the function's bind(), which is
139 * zero for success else a negative errno value.
141 int __init usb_add_function(struct usb_configuration *config,
142 struct usb_function *function)
144 struct usb_composite_dev *cdev = config->cdev;
148 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
149 function->name, function,
150 config->label, config);
152 if (!function->set_alt || !function->disable)
155 index = atomic_inc_return(&cdev->driver->function_count);
156 function->dev = device_create(cdev->driver->class, NULL,
157 MKDEV(0, index), NULL, function->name);
158 if (IS_ERR(function->dev))
159 return PTR_ERR(function->dev);
161 value = device_create_file(function->dev, &dev_attr_enable);
163 device_destroy(cdev->driver->class, MKDEV(0, index));
166 dev_set_drvdata(function->dev, function);
168 function->config = config;
169 list_add_tail(&function->list, &config->functions);
171 /* REVISIT *require* function->bind? */
172 if (function->bind) {
173 value = function->bind(config, function);
175 list_del(&function->list);
176 function->config = NULL;
181 /* We allow configurations that don't work at both speeds.
182 * If we run into a lowspeed Linux system, treat it the same
183 * as full speed ... it's the function drivers that will need
184 * to avoid bulk and ISO transfers.
186 if (!config->fullspeed && function->descriptors)
187 config->fullspeed = true;
188 if (!config->highspeed && function->hs_descriptors)
189 config->highspeed = true;
193 DBG(cdev, "adding '%s'/%p --> %d\n",
194 function->name, function, value);
199 * usb_function_deactivate - prevent function and gadget enumeration
200 * @function: the function that isn't yet ready to respond
202 * Blocks response of the gadget driver to host enumeration by
203 * preventing the data line pullup from being activated. This is
204 * normally called during @bind() processing to change from the
205 * initial "ready to respond" state, or when a required resource
208 * For example, drivers that serve as a passthrough to a userspace
209 * daemon can block enumeration unless that daemon (such as an OBEX,
210 * MTP, or print server) is ready to handle host requests.
212 * Not all systems support software control of their USB peripheral
215 * Returns zero on success, else negative errno.
217 int usb_function_deactivate(struct usb_function *function)
219 struct usb_composite_dev *cdev = function->config->cdev;
223 spin_lock_irqsave(&cdev->lock, flags);
225 if (cdev->deactivations == 0)
226 status = usb_gadget_disconnect(cdev->gadget);
228 cdev->deactivations++;
230 spin_unlock_irqrestore(&cdev->lock, flags);
235 * usb_function_activate - allow function and gadget enumeration
236 * @function: function on which usb_function_activate() was called
238 * Reverses effect of usb_function_deactivate(). If no more functions
239 * are delaying their activation, the gadget driver will respond to
240 * host enumeration procedures.
242 * Returns zero on success, else negative errno.
244 int usb_function_activate(struct usb_function *function)
246 struct usb_composite_dev *cdev = function->config->cdev;
249 spin_lock(&cdev->lock);
251 if (WARN_ON(cdev->deactivations == 0))
254 cdev->deactivations--;
255 if (cdev->deactivations == 0)
256 status = usb_gadget_connect(cdev->gadget);
259 spin_unlock(&cdev->lock);
264 * usb_interface_id() - allocate an unused interface ID
265 * @config: configuration associated with the interface
266 * @function: function handling the interface
267 * Context: single threaded during gadget setup
269 * usb_interface_id() is called from usb_function.bind() callbacks to
270 * allocate new interface IDs. The function driver will then store that
271 * ID in interface, association, CDC union, and other descriptors. It
272 * will also handle any control requests targetted at that interface,
273 * particularly changing its altsetting via set_alt(). There may
274 * also be class-specific or vendor-specific requests to handle.
276 * All interface identifier should be allocated using this routine, to
277 * ensure that for example different functions don't wrongly assign
278 * different meanings to the same identifier. Note that since interface
279 * identifers are configuration-specific, functions used in more than
280 * one configuration (or more than once in a given configuration) need
281 * multiple versions of the relevant descriptors.
283 * Returns the interface ID which was allocated; or -ENODEV if no
284 * more interface IDs can be allocated.
286 int __init usb_interface_id(struct usb_configuration *config,
287 struct usb_function *function)
289 unsigned id = config->next_interface_id;
291 if (id < MAX_CONFIG_INTERFACES) {
292 config->interface[id] = function;
293 config->next_interface_id = id + 1;
299 static int config_buf(struct usb_configuration *config,
300 enum usb_device_speed speed, void *buf, u8 type)
302 struct usb_config_descriptor *c = buf;
303 struct usb_interface_descriptor *intf;
304 void *next = buf + USB_DT_CONFIG_SIZE;
305 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
306 struct usb_function *f;
308 int interfaceCount = 0;
311 /* write the config descriptor */
313 c->bLength = USB_DT_CONFIG_SIZE;
314 c->bDescriptorType = type;
315 /* wTotalLength and bNumInterfaces are written later */
316 c->bConfigurationValue = config->bConfigurationValue;
317 c->iConfiguration = config->iConfiguration;
318 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
319 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
321 /* There may be e.g. OTG descriptors */
322 if (config->descriptors) {
323 status = usb_descriptor_fillbuf(next, len,
324 config->descriptors);
331 /* add each function's descriptors */
332 list_for_each_entry(f, &config->functions, list) {
333 struct usb_descriptor_header **descriptors;
334 struct usb_descriptor_header *descriptor;
336 if (speed == USB_SPEED_HIGH)
337 descriptors = f->hs_descriptors;
339 descriptors = f->descriptors;
340 if (f->disabled || !descriptors || descriptors[0] == NULL)
342 status = usb_descriptor_fillbuf(next, len,
343 (const struct usb_descriptor_header **) descriptors);
347 /* set interface numbers dynamically */
349 while ((descriptor = *descriptors++) != NULL) {
350 intf = (struct usb_interface_descriptor *)dest;
351 if (intf->bDescriptorType == USB_DT_INTERFACE) {
352 /* don't increment bInterfaceNumber for alternate settings */
353 if (intf->bAlternateSetting == 0)
354 intf->bInterfaceNumber = interfaceCount++;
356 intf->bInterfaceNumber = interfaceCount - 1;
358 dest += intf->bLength;
366 c->wTotalLength = cpu_to_le16(len);
367 c->bNumInterfaces = interfaceCount;
371 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
373 struct usb_gadget *gadget = cdev->gadget;
374 struct usb_configuration *c;
375 u8 type = w_value >> 8;
376 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
378 if (gadget_is_dualspeed(gadget)) {
381 if (gadget->speed == USB_SPEED_HIGH)
383 if (type == USB_DT_OTHER_SPEED_CONFIG)
386 speed = USB_SPEED_HIGH;
390 /* This is a lookup by config *INDEX* */
392 list_for_each_entry(c, &cdev->configs, list) {
393 /* ignore configs that won't work at this speed */
394 if (speed == USB_SPEED_HIGH) {
402 return config_buf(c, speed, cdev->req->buf, type);
408 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
410 struct usb_gadget *gadget = cdev->gadget;
411 struct usb_configuration *c;
415 if (gadget_is_dualspeed(gadget)) {
416 if (gadget->speed == USB_SPEED_HIGH)
418 if (type == USB_DT_DEVICE_QUALIFIER)
421 list_for_each_entry(c, &cdev->configs, list) {
422 /* ignore configs that won't work at this speed */
435 static void device_qual(struct usb_composite_dev *cdev)
437 struct usb_qualifier_descriptor *qual = cdev->req->buf;
439 qual->bLength = sizeof(*qual);
440 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
441 /* POLICY: same bcdUSB and device type info at both speeds */
442 qual->bcdUSB = cdev->desc.bcdUSB;
443 qual->bDeviceClass = cdev->desc.bDeviceClass;
444 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
445 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
446 /* ASSUME same EP0 fifo size at both speeds */
447 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
448 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
452 /*-------------------------------------------------------------------------*/
454 static void reset_config(struct usb_composite_dev *cdev)
456 struct usb_function *f;
458 DBG(cdev, "reset config\n");
460 list_for_each_entry(f, &cdev->config->functions, list) {
467 static int set_config(struct usb_composite_dev *cdev,
468 const struct usb_ctrlrequest *ctrl, unsigned number)
470 struct usb_gadget *gadget = cdev->gadget;
471 struct usb_configuration *c = NULL;
472 int result = -EINVAL;
473 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
480 list_for_each_entry(c, &cdev->configs, list) {
481 if (c->bConfigurationValue == number) {
491 INFO(cdev, "%s speed config #%d: %s\n",
493 switch (gadget->speed) {
494 case USB_SPEED_LOW: speed = "low"; break;
495 case USB_SPEED_FULL: speed = "full"; break;
496 case USB_SPEED_HIGH: speed = "high"; break;
497 default: speed = "?"; break;
498 } ; speed; }), number, c ? c->label : "unconfigured");
505 /* Initialize all interfaces by setting them to altsetting zero. */
506 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
507 struct usb_function *f = c->interface[tmp];
514 result = f->set_alt(f, tmp, 0);
516 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
517 tmp, f->name, f, result);
524 /* when we return, be sure our power usage is valid */
525 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
527 usb_gadget_vbus_draw(gadget, power);
529 schedule_work(&cdev->switch_work);
534 * usb_add_config() - add a configuration to a device.
535 * @cdev: wraps the USB gadget
536 * @config: the configuration, with bConfigurationValue assigned
537 * Context: single threaded during gadget setup
539 * One of the main tasks of a composite driver's bind() routine is to
540 * add each of the configurations it supports, using this routine.
542 * This function returns the value of the configuration's bind(), which
543 * is zero for success else a negative errno value. Binding configurations
544 * assigns global resources including string IDs, and per-configuration
545 * resources such as interface IDs and endpoints.
547 int __init usb_add_config(struct usb_composite_dev *cdev,
548 struct usb_configuration *config)
550 int status = -EINVAL;
551 struct usb_configuration *c;
553 DBG(cdev, "adding config #%u '%s'/%p\n",
554 config->bConfigurationValue,
555 config->label, config);
557 if (!config->bConfigurationValue || !config->bind)
560 /* Prevent duplicate configuration identifiers */
561 list_for_each_entry(c, &cdev->configs, list) {
562 if (c->bConfigurationValue == config->bConfigurationValue) {
569 list_add_tail(&config->list, &cdev->configs);
571 INIT_LIST_HEAD(&config->functions);
572 config->next_interface_id = 0;
574 status = config->bind(config);
576 list_del(&config->list);
581 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
582 config->bConfigurationValue, config,
583 config->highspeed ? " high" : "",
585 ? (gadget_is_dualspeed(cdev->gadget)
590 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
591 struct usb_function *f = config->interface[i];
595 DBG(cdev, " interface %d = %s/%p\n",
600 /* set_alt(), or next config->bind(), sets up
601 * ep->driver_data as needed.
603 usb_ep_autoconfig_reset(cdev->gadget);
607 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
608 config->bConfigurationValue, status);
612 /*-------------------------------------------------------------------------*/
614 /* We support strings in multiple languages ... string descriptor zero
615 * says which languages are supported. The typical case will be that
616 * only one language (probably English) is used, with I18N handled on
620 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
622 const struct usb_gadget_strings *s;
628 language = cpu_to_le16(s->language);
629 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
630 if (*tmp == language)
639 static int lookup_string(
640 struct usb_gadget_strings **sp,
646 struct usb_gadget_strings *s;
651 if (s->language != language)
653 value = usb_gadget_get_string(s, id, buf);
660 static int get_string(struct usb_composite_dev *cdev,
661 void *buf, u16 language, int id)
663 struct usb_configuration *c;
664 struct usb_function *f;
667 /* Yes, not only is USB's I18N support probably more than most
668 * folk will ever care about ... also, it's all supported here.
669 * (Except for UTF8 support for Unicode's "Astral Planes".)
672 /* 0 == report all available language codes */
674 struct usb_string_descriptor *s = buf;
675 struct usb_gadget_strings **sp;
678 s->bDescriptorType = USB_DT_STRING;
680 sp = composite->strings;
682 collect_langs(sp, s->wData);
684 list_for_each_entry(c, &cdev->configs, list) {
687 collect_langs(sp, s->wData);
689 list_for_each_entry(f, &c->functions, list) {
692 collect_langs(sp, s->wData);
696 for (len = 0; len <= 126 && s->wData[len]; len++)
701 s->bLength = 2 * (len + 1);
705 /* Otherwise, look up and return a specified string. String IDs
706 * are device-scoped, so we look up each string table we're told
707 * about. These lookups are infrequent; simpler-is-better here.
709 if (composite->strings) {
710 len = lookup_string(composite->strings, buf, language, id);
714 list_for_each_entry(c, &cdev->configs, list) {
716 len = lookup_string(c->strings, buf, language, id);
720 list_for_each_entry(f, &c->functions, list) {
723 len = lookup_string(f->strings, buf, language, id);
732 * usb_string_id() - allocate an unused string ID
733 * @cdev: the device whose string descriptor IDs are being allocated
734 * Context: single threaded during gadget setup
736 * @usb_string_id() is called from bind() callbacks to allocate
737 * string IDs. Drivers for functions, configurations, or gadgets will
738 * then store that ID in the appropriate descriptors and string table.
740 * All string identifier should be allocated using this routine, to
741 * ensure that for example different functions don't wrongly assign
742 * different meanings to the same identifier.
744 int __init usb_string_id(struct usb_composite_dev *cdev)
746 if (cdev->next_string_id < 254) {
747 /* string id 0 is reserved */
748 cdev->next_string_id++;
749 return cdev->next_string_id;
754 /*-------------------------------------------------------------------------*/
756 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
758 if (req->status || req->actual != req->length)
759 DBG((struct usb_composite_dev *) ep->driver_data,
760 "setup complete --> %d, %d/%d\n",
761 req->status, req->actual, req->length);
765 * The setup() callback implements all the ep0 functionality that's
766 * not handled lower down, in hardware or the hardware driver(like
767 * device and endpoint feature flags, and their status). It's all
768 * housekeeping for the gadget function we're implementing. Most of
769 * the work is in config and function specific setup.
772 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
774 struct usb_composite_dev *cdev = get_gadget_data(gadget);
775 struct usb_request *req = cdev->req;
776 int value = -EOPNOTSUPP;
777 u16 w_index = le16_to_cpu(ctrl->wIndex);
778 u8 intf = w_index & 0xFF;
779 u16 w_value = le16_to_cpu(ctrl->wValue);
780 u16 w_length = le16_to_cpu(ctrl->wLength);
781 struct usb_function *f = NULL;
783 /* partial re-init of the response message; the function or the
784 * gadget might need to intercept e.g. a control-OUT completion
785 * when we delegate to it.
788 req->complete = composite_setup_complete;
789 req->length = USB_BUFSIZ;
790 gadget->ep0->driver_data = cdev;
792 switch (ctrl->bRequest) {
794 /* we handle all standard USB descriptors */
795 case USB_REQ_GET_DESCRIPTOR:
796 if (ctrl->bRequestType != USB_DIR_IN)
798 switch (w_value >> 8) {
801 cdev->desc.bNumConfigurations =
802 count_configs(cdev, USB_DT_DEVICE);
803 value = min(w_length, (u16) sizeof cdev->desc);
804 memcpy(req->buf, &cdev->desc, value);
806 case USB_DT_DEVICE_QUALIFIER:
807 if (!gadget_is_dualspeed(gadget))
810 value = min_t(int, w_length,
811 sizeof(struct usb_qualifier_descriptor));
813 case USB_DT_OTHER_SPEED_CONFIG:
814 if (!gadget_is_dualspeed(gadget))
818 value = config_desc(cdev, w_value);
820 value = min(w_length, (u16) value);
823 value = get_string(cdev, req->buf,
824 w_index, w_value & 0xff);
826 value = min(w_length, (u16) value);
831 /* any number of configs can work */
832 case USB_REQ_SET_CONFIGURATION:
833 if (ctrl->bRequestType != 0)
835 if (gadget_is_otg(gadget)) {
836 if (gadget->a_hnp_support)
837 DBG(cdev, "HNP available\n");
838 else if (gadget->a_alt_hnp_support)
839 DBG(cdev, "HNP on another port\n");
841 VDBG(cdev, "HNP inactive\n");
843 spin_lock(&cdev->lock);
844 value = set_config(cdev, ctrl, w_value);
845 spin_unlock(&cdev->lock);
847 case USB_REQ_GET_CONFIGURATION:
848 if (ctrl->bRequestType != USB_DIR_IN)
851 *(u8 *)req->buf = cdev->config->bConfigurationValue;
852 value = min(w_length, (u16) 1);
857 /* function drivers must handle get/set altsetting; if there's
858 * no get() method, we know only altsetting zero works.
860 case USB_REQ_SET_INTERFACE:
861 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
863 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
865 f = cdev->config->interface[intf];
868 if (w_value && !f->set_alt)
870 value = f->set_alt(f, w_index, w_value);
872 case USB_REQ_GET_INTERFACE:
873 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
875 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
877 f = cdev->config->interface[intf];
880 /* lots of interfaces only need altsetting zero... */
881 value = f->get_alt ? f->get_alt(f, w_index) : 0;
884 *((u8 *)req->buf) = value;
885 value = min(w_length, (u16) 1);
890 "non-core control req%02x.%02x v%04x i%04x l%d\n",
891 ctrl->bRequestType, ctrl->bRequest,
892 w_value, w_index, w_length);
894 /* functions always handle their interfaces ... punt other
895 * recipients (endpoint, other, WUSB, ...) to the current
896 * configuration code.
898 * REVISIT it could make sense to let the composite device
899 * take such requests too, if that's ever needed: to work
902 if ((ctrl->bRequestType & USB_RECIP_MASK)
903 == USB_RECIP_INTERFACE) {
904 if (cdev->config == NULL)
907 f = cdev->config->interface[intf];
909 value = f->setup(f, ctrl);
913 if (value < 0 && !f) {
914 struct usb_configuration *c;
918 value = c->setup(c, ctrl);
921 /* If the vendor request is not processed (value < 0),
922 * call all device registered configure setup callbacks
924 * This is used to handle the following cases:
925 * - vendor request is for the device and arrives before
927 * - Some devices are required to handle vendor request before
928 * setconfiguration such as MTP, USBNET.
932 struct usb_configuration *cfg;
934 list_for_each_entry(cfg, &cdev->configs, list) {
935 if (cfg && cfg->setup)
936 value = cfg->setup(cfg, ctrl);
943 /* respond with data transfer before status phase? */
946 req->zero = value < w_length;
947 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
949 DBG(cdev, "ep_queue --> %d\n", value);
951 composite_setup_complete(gadget->ep0, req);
956 /* device either stalls (value < 0) or reports success */
960 static void composite_disconnect(struct usb_gadget *gadget)
962 struct usb_composite_dev *cdev = get_gadget_data(gadget);
965 /* REVISIT: should we have config and device level
966 * disconnect callbacks?
968 spin_lock_irqsave(&cdev->lock, flags);
972 if (cdev->mute_switch)
973 cdev->mute_switch = 0;
975 schedule_work(&cdev->switch_work);
976 spin_unlock_irqrestore(&cdev->lock, flags);
979 /*-------------------------------------------------------------------------*/
981 static void /* __init_or_exit */
982 composite_unbind(struct usb_gadget *gadget)
984 struct usb_composite_dev *cdev = get_gadget_data(gadget);
986 /* composite_disconnect() must already have been called
987 * by the underlying peripheral controller driver!
988 * so there's no i/o concurrency that could affect the
989 * state protected by cdev->lock.
991 WARN_ON(cdev->config);
993 while (!list_empty(&cdev->configs)) {
994 struct usb_configuration *c;
996 c = list_first_entry(&cdev->configs,
997 struct usb_configuration, list);
998 while (!list_empty(&c->functions)) {
999 struct usb_function *f;
1001 f = list_first_entry(&c->functions,
1002 struct usb_function, list);
1005 DBG(cdev, "unbind function '%s'/%p\n",
1008 /* may free memory for "f" */
1013 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1015 /* may free memory for "c" */
1018 if (composite->unbind)
1019 composite->unbind(cdev);
1022 kfree(cdev->req->buf);
1023 usb_ep_free_request(gadget->ep0, cdev->req);
1026 switch_dev_unregister(&cdev->sdev);
1028 set_gadget_data(gadget, NULL);
1033 string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
1035 struct usb_string *str = tab->strings;
1037 for (str = tab->strings; str->s; str++) {
1038 if (str->id == id) {
1046 string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
1049 string_override_one(*tab, id, s);
1055 composite_switch_work(struct work_struct *data)
1057 struct usb_composite_dev *cdev =
1058 container_of(data, struct usb_composite_dev, switch_work);
1059 struct usb_configuration *config = cdev->config;
1062 switch_set_state(&cdev->sdev, config->bConfigurationValue);
1064 switch_set_state(&cdev->sdev, 0);
1067 static int __init composite_bind(struct usb_gadget *gadget)
1069 struct usb_composite_dev *cdev;
1070 int status = -ENOMEM;
1072 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1076 spin_lock_init(&cdev->lock);
1077 cdev->gadget = gadget;
1078 set_gadget_data(gadget, cdev);
1079 INIT_LIST_HEAD(&cdev->configs);
1081 /* preallocate control response and buffer */
1082 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1085 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1086 if (!cdev->req->buf)
1088 cdev->req->complete = composite_setup_complete;
1089 gadget->ep0->driver_data = cdev;
1091 cdev->bufsiz = USB_BUFSIZ;
1092 cdev->driver = composite;
1094 usb_gadget_set_selfpowered(gadget);
1096 /* interface and string IDs start at zero via kzalloc.
1097 * we force endpoints to start unassigned; few controller
1098 * drivers will zero ep->driver_data.
1100 usb_ep_autoconfig_reset(cdev->gadget);
1102 /* composite gadget needs to assign strings for whole device (like
1103 * serial number), register function drivers, potentially update
1104 * power state and consumption, etc
1106 status = composite->bind(cdev);
1110 cdev->sdev.name = "usb_configuration";
1111 status = switch_dev_register(&cdev->sdev);
1114 INIT_WORK(&cdev->switch_work, composite_switch_work);
1116 cdev->desc = *composite->dev;
1117 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1119 /* standardized runtime overrides for device ID data */
1121 cdev->desc.idVendor = cpu_to_le16(idVendor);
1123 cdev->desc.idProduct = cpu_to_le16(idProduct);
1125 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1127 /* strings can't be assigned before bind() allocates the
1128 * releavnt identifiers
1130 if (cdev->desc.iManufacturer && iManufacturer)
1131 string_override(composite->strings,
1132 cdev->desc.iManufacturer, iManufacturer);
1133 if (cdev->desc.iProduct && iProduct)
1134 string_override(composite->strings,
1135 cdev->desc.iProduct, iProduct);
1136 if (cdev->desc.iSerialNumber && iSerialNumber)
1137 string_override(composite->strings,
1138 cdev->desc.iSerialNumber, iSerialNumber);
1140 INFO(cdev, "%s ready\n", composite->name);
1144 composite_unbind(gadget);
1148 /*-------------------------------------------------------------------------*/
1151 composite_suspend(struct usb_gadget *gadget)
1153 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1154 struct usb_function *f;
1156 /* REVISIT: should we have config level
1157 * suspend/resume callbacks?
1159 DBG(cdev, "suspend\n");
1161 list_for_each_entry(f, &cdev->config->functions, list) {
1166 if (composite->suspend)
1167 composite->suspend(cdev);
1171 composite_resume(struct usb_gadget *gadget)
1173 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1174 struct usb_function *f;
1176 /* REVISIT: should we have config level
1177 * suspend/resume callbacks?
1179 DBG(cdev, "resume\n");
1180 if (composite->resume)
1181 composite->resume(cdev);
1183 list_for_each_entry(f, &cdev->config->functions, list) {
1191 composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1193 struct usb_function *f = dev_get_drvdata(dev);
1196 /* this happens when the device is first created */
1200 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1202 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1207 /*-------------------------------------------------------------------------*/
1209 static struct usb_gadget_driver composite_driver = {
1210 .speed = USB_SPEED_HIGH,
1212 .bind = composite_bind,
1213 .unbind = __exit_p(composite_unbind),
1215 .setup = composite_setup,
1216 .disconnect = composite_disconnect,
1218 .suspend = composite_suspend,
1219 .resume = composite_resume,
1222 .owner = THIS_MODULE,
1227 * usb_composite_register() - register a composite driver
1228 * @driver: the driver to register
1229 * Context: single threaded during gadget setup
1231 * This function is used to register drivers using the composite driver
1232 * framework. The return value is zero, or a negative errno value.
1233 * Those values normally come from the driver's @bind method, which does
1234 * all the work of setting up the driver to match the hardware.
1236 * On successful return, the gadget is ready to respond to requests from
1237 * the host, unless one of its components invokes usb_gadget_disconnect()
1238 * while it was binding. That would usually be done in order to wait for
1239 * some userspace participation.
1241 int __init usb_composite_register(struct usb_composite_driver *driver)
1243 if (!driver || !driver->dev || !driver->bind || composite)
1247 driver->name = "composite";
1248 composite_driver.function = (char *) driver->name;
1249 composite_driver.driver.name = driver->name;
1252 driver->class = class_create(THIS_MODULE, "usb_composite");
1253 if (IS_ERR(driver->class))
1254 return PTR_ERR(driver->class);
1255 driver->class->dev_uevent = composite_uevent;
1257 return usb_gadget_register_driver(&composite_driver);
1261 * usb_composite_unregister() - unregister a composite driver
1262 * @driver: the driver to unregister
1264 * This function is used to unregister drivers using the composite
1267 void __exit usb_composite_unregister(struct usb_composite_driver *driver)
1269 if (composite != driver)
1271 usb_gadget_unregister_driver(&composite_driver);