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/kdev_t.h>
29 #include <ubi_uboot.h>
30 #include <linux/mtd/compat.h>
31 #include <linux/types.h>
33 #include <linux/usb/composite.h>
35 #define CONFIG_USB_GADGET_VBUS_DRAW 2
37 struct usb_function * usb_func[4];
38 #define GFP_ATOMIC ((gfp_t) 0)
39 //#define GFP_KERNEL ((gfp_t) 0)
40 extern void udelay(unsigned long usec);
41 #define mdelay(cnt) udelay(cnt*1000)
42 #define msleep(cnt) udelay(cnt*1000)
46 * The code in this file is utility code, used to build a gadget driver
47 * from one or more "function" drivers, one or more "configuration"
48 * objects, and a "usb_composite_driver" by gluing them together along
49 * with the relevant device-wide data.
52 /* big enough to hold our biggest descriptor */
53 #define USB_BUFSIZ 512
55 static struct usb_composite_driver *composite;
57 /* Some systems will need runtime overrides for the product identifers
58 * published in the device descriptor, either numbers or strings or both.
59 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
62 static ushort idVendor;
63 module_param(idVendor, ushort, 0);
64 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
66 static ushort idProduct;
67 module_param(idProduct, ushort, 0);
68 MODULE_PARM_DESC(idProduct, "USB Product ID");
70 static ushort bcdDevice;
71 module_param(bcdDevice, ushort, 0);
72 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
74 static char *iManufacturer;
75 module_param(iManufacturer, charp, 0);
76 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
78 static char *iProduct;
79 module_param(iProduct, charp, 0);
80 MODULE_PARM_DESC(iProduct, "USB Product string");
82 static char *iSerialNumber;
83 module_param(iSerialNumber, charp, 0);
84 MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
86 /*-------------------------------------------------------------------------*/
88 static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
91 struct usb_function *f = dev_get_drvdata(dev);
92 return sprintf(buf, "%d\n", !f->disabled);
95 static ssize_t enable_store(
96 struct device *dev, struct device_attribute *attr,
97 const char *buf, size_t size)
99 struct usb_function *f = dev_get_drvdata(dev);
100 struct usb_composite_driver *driver = f->config->cdev->driver;
103 sscanf(buf, "%d", &value);
104 if (driver->enable_function)
105 driver->enable_function(f, value);
107 usb_function_set_enabled(f, value);
112 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, enable_show, enable_store);
115 void usb_function_set_enabled(struct usb_function *f, int enabled)
117 f->disabled = !enabled;
118 //kobject_uevent(&f->dev->kobj, KOBJ_CHANGE);
122 void usb_composite_force_reset(struct usb_composite_dev *cdev)
124 //unsigned long flags;
126 spin_lock_irqsave(&cdev->lock, flags);
127 /* force reenumeration */
128 if (cdev && cdev->gadget &&
129 cdev->gadget->speed != USB_SPEED_UNKNOWN) {
130 /* avoid sending a disconnect switch event until after we disconnect */
131 cdev->mute_switch = 1;
132 spin_unlock_irqrestore(&cdev->lock, flags);
134 usb_gadget_disconnect(cdev->gadget);
136 usb_gadget_connect(cdev->gadget);
138 spin_unlock_irqrestore(&cdev->lock, flags);
143 * usb_add_function() - add a function to a configuration
144 * @config: the configuration
145 * @function: the function being added
146 * Context: single threaded during gadget setup
148 * After initialization, each configuration must have one or more
149 * functions added to it. Adding a function involves calling its @bind()
150 * method to allocate resources such as interface and string identifiers
153 * This function returns the value of the function's bind(), which is
154 * zero for success else a negative errno value.
156 int __init usb_add_function(struct usb_configuration *config,
157 struct usb_function *function)
159 struct usb_composite_dev *cdev = config->cdev;
163 DBG(cdev, "adding '%s'/%p to config '%s'/%p\n",
164 function->name, function,
165 config->label, config);
167 if (!function->set_alt || !function->disable)
170 index = atomic_inc_return(&cdev->driver->function_count);
172 index = cdev->driver->function_count.counter + 1;
175 function->dev = device_create(cdev->driver->class, NULL,
176 MKDEV(0, index), NULL, function->name);
177 if (IS_ERR(function->dev))
178 return PTR_ERR(function->dev);
180 value = device_create_file(function->dev, &dev_attr_enable);
182 device_destroy(cdev->driver->class, MKDEV(0, index));
185 dev_set_drvdata(function->dev, function);
187 usb_func[index]=function;
190 function->config = config;
191 list_add_tail(&function->list, &config->functions);
193 /* REVISIT *require* function->bind? */
194 if (function->bind) {
195 value = function->bind(config, function);
197 list_del(&function->list);
198 function->config = NULL;
203 /* We allow configurations that don't work at both speeds.
204 * If we run into a lowspeed Linux system, treat it the same
205 * as full speed ... it's the function drivers that will need
206 * to avoid bulk and ISO transfers.
208 if (!config->fullspeed && function->descriptors)
209 config->fullspeed = true;
210 if (!config->highspeed && function->hs_descriptors)
211 config->highspeed = true;
215 DBG(cdev, "adding '%s'/%p --> %d\n",
216 function->name, function, value);
221 * usb_function_deactivate - prevent function and gadget enumeration
222 * @function: the function that isn't yet ready to respond
224 * Blocks response of the gadget driver to host enumeration by
225 * preventing the data line pullup from being activated. This is
226 * normally called during @bind() processing to change from the
227 * initial "ready to respond" state, or when a required resource
230 * For example, drivers that serve as a passthrough to a userspace
231 * daemon can block enumeration unless that daemon (such as an OBEX,
232 * MTP, or print server) is ready to handle host requests.
234 * Not all systems support software control of their USB peripheral
237 * Returns zero on success, else negative errno.
239 int usb_function_deactivate(struct usb_function *function)
241 struct usb_composite_dev *cdev = function->config->cdev;
242 //unsigned long flags;
245 spin_lock_irqsave(&cdev->lock, flags);
247 if (cdev->deactivations == 0)
248 status = usb_gadget_disconnect(cdev->gadget);
250 cdev->deactivations++;
252 spin_unlock_irqrestore(&cdev->lock, flags);
257 * usb_function_activate - allow function and gadget enumeration
258 * @function: function on which usb_function_activate() was called
260 * Reverses effect of usb_function_deactivate(). If no more functions
261 * are delaying their activation, the gadget driver will respond to
262 * host enumeration procedures.
264 * Returns zero on success, else negative errno.
266 int usb_function_activate(struct usb_function *function)
268 struct usb_composite_dev *cdev = function->config->cdev;
271 spin_lock(&cdev->lock);
273 if (cdev->deactivations == 0)
276 cdev->deactivations--;
277 if (cdev->deactivations == 0)
278 status = usb_gadget_connect(cdev->gadget);
281 spin_unlock(&cdev->lock);
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 targetted 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 * identifers 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 __init 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 struct usb_interface_descriptor *intf;
326 void *next = buf + USB_DT_CONFIG_SIZE;
327 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
328 struct usb_function *f;
330 int interfaceCount = 0;
333 /* write the config descriptor */
335 c->bLength = USB_DT_CONFIG_SIZE;
336 c->bDescriptorType = type;
337 /* wTotalLength and bNumInterfaces are written later */
338 c->bConfigurationValue = config->bConfigurationValue;
339 c->iConfiguration = config->iConfiguration;
340 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
342 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
344 /* There may be e.g. OTG descriptors */
345 if (config->descriptors) {
346 status = usb_descriptor_fillbuf(next, len,
347 config->descriptors);
354 /* add each function's descriptors */
355 list_for_each_entry(f, &config->functions, list) {
356 struct usb_descriptor_header **descriptors;
357 struct usb_descriptor_header *descriptor;
359 if (speed == USB_SPEED_HIGH)
360 descriptors = f->hs_descriptors;
362 descriptors = f->descriptors;
363 if (f->disabled || !descriptors || descriptors[0] == NULL)
365 status = usb_descriptor_fillbuf(next, len,
366 (const struct usb_descriptor_header **) descriptors);
370 /* set interface numbers dynamically */
372 while ((descriptor = *descriptors++) != NULL) {
373 intf = (struct usb_interface_descriptor *)dest;
374 if (intf->bDescriptorType == USB_DT_INTERFACE) {
375 /* don't increment bInterfaceNumber for alternate settings */
376 if (intf->bAlternateSetting == 0)
377 intf->bInterfaceNumber = interfaceCount++;
379 intf->bInterfaceNumber = interfaceCount - 1;
381 dest += intf->bLength;
389 c->wTotalLength = cpu_to_le16(len);
390 c->bNumInterfaces = interfaceCount;
394 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
396 struct usb_gadget *gadget = cdev->gadget;
397 struct usb_configuration *c;
398 u8 type = w_value >> 8;
399 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
401 if (gadget_is_dualspeed(gadget)) {
404 if (gadget->speed == USB_SPEED_HIGH)
406 if (type == USB_DT_OTHER_SPEED_CONFIG)
409 speed = USB_SPEED_HIGH;
413 /* This is a lookup by config *INDEX* */
415 list_for_each_entry(c, &cdev->configs, list) {
416 /* ignore configs that won't work at this speed */
417 if (speed == USB_SPEED_HIGH) {
425 return config_buf(c, speed, cdev->req->buf, type);
431 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
433 struct usb_gadget *gadget = cdev->gadget;
434 struct usb_configuration *c;
438 if (gadget_is_dualspeed(gadget)) {
439 if (gadget->speed == USB_SPEED_HIGH)
441 if (type == USB_DT_DEVICE_QUALIFIER)
444 list_for_each_entry(c, &cdev->configs, list) {
445 /* ignore configs that won't work at this speed */
458 static void device_qual(struct usb_composite_dev *cdev)
460 struct usb_qualifier_descriptor *qual = cdev->req->buf;
462 qual->bLength = sizeof(*qual);
463 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
464 /* POLICY: same bcdUSB and device type info at both speeds */
465 qual->bcdUSB = cdev->desc.bcdUSB;
466 qual->bDeviceClass = cdev->desc.bDeviceClass;
467 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
468 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
469 /* ASSUME same EP0 fifo size at both speeds */
470 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
471 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
475 /*-------------------------------------------------------------------------*/
477 static void reset_config(struct usb_composite_dev *cdev)
479 struct usb_function *f;
481 DBG(cdev, "reset config\n");
483 list_for_each_entry(f, &cdev->config->functions, list) {
490 static int set_config(struct usb_composite_dev *cdev,
491 const struct usb_ctrlrequest *ctrl, unsigned number)
493 struct usb_gadget *gadget = cdev->gadget;
494 struct usb_configuration *c = NULL;
495 int result = -EINVAL;
496 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
503 list_for_each_entry(c, &cdev->configs, list) {
504 if (c->bConfigurationValue == number) {
514 INFO(cdev, "%s speed config #%d: %s\n",
516 switch (gadget->speed) {
517 case USB_SPEED_LOW: speed = "low"; break;
518 case USB_SPEED_FULL: speed = "full"; break;
519 case USB_SPEED_HIGH: speed = "high"; break;
520 default: speed = "?"; break;
521 } ; speed; }), number, c ? c->label : "unconfigured");
528 /* Initialize all interfaces by setting them to altsetting zero. */
529 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
530 struct usb_function *f = c->interface[tmp];
537 result = f->set_alt(f, tmp, 0);
539 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
540 tmp, f->name, f, result);
547 /* when we return, be sure our power usage is valid */
548 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
550 usb_gadget_vbus_draw(gadget, power);
552 //schedule_work(&cdev->switch_work);
553 //composite_switch_work(&cdev->switch_work);
558 * usb_add_config() - add a configuration to a device.
559 * @cdev: wraps the USB gadget
560 * @config: the configuration, with bConfigurationValue assigned
561 * Context: single threaded during gadget setup
563 * One of the main tasks of a composite driver's bind() routine is to
564 * add each of the configurations it supports, using this routine.
566 * This function returns the value of the configuration's bind(), which
567 * is zero for success else a negative errno value. Binding configurations
568 * assigns global resources including string IDs, and per-configuration
569 * resources such as interface IDs and endpoints.
571 int __init usb_add_config(struct usb_composite_dev *cdev,
572 struct usb_configuration *config)
574 int status = -EINVAL;
575 struct usb_configuration *c;
577 DBG(cdev, "adding config #%u '%s'/%p\n",
578 config->bConfigurationValue,
579 config->label, config);
581 if (!config->bConfigurationValue || !config->bind)
584 /* Prevent duplicate configuration identifiers */
585 list_for_each_entry(c, &cdev->configs, list) {
586 if (c->bConfigurationValue == config->bConfigurationValue) {
593 list_add_tail(&config->list, &cdev->configs);
595 INIT_LIST_HEAD(&config->functions);
596 config->next_interface_id = 0;
598 status = config->bind(config);
600 list_del(&config->list);
605 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
606 config->bConfigurationValue, config,
607 config->highspeed ? " high" : "",
609 ? (gadget_is_dualspeed(cdev->gadget)
614 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
615 struct usb_function *f = config->interface[i];
619 DBG(cdev, " interface %d = %s/%p\n",
624 /* set_alt(), or next config->bind(), sets up
625 * ep->driver_data as needed.
627 usb_ep_autoconfig_reset(cdev->gadget);
631 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
632 config->bConfigurationValue, status);
636 /*-------------------------------------------------------------------------*/
638 /* We support strings in multiple languages ... string descriptor zero
639 * says which languages are supported. The typical case will be that
640 * only one language (probably English) is used, with I18N handled on
644 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
646 const struct usb_gadget_strings *s;
652 language = cpu_to_le16(s->language);
653 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
654 if (*tmp == language)
663 static int lookup_string(
664 struct usb_gadget_strings **sp,
670 struct usb_gadget_strings *s;
675 if (s->language != language)
677 value = usb_gadget_get_string(s, id, buf);
684 static int get_string(struct usb_composite_dev *cdev,
685 void *buf, u16 language, int id)
687 struct usb_configuration *c;
688 struct usb_function *f;
691 /* Yes, not only is USB's I18N support probably more than most
692 * folk will ever care about ... also, it's all supported here.
693 * (Except for UTF8 support for Unicode's "Astral Planes".)
696 /* 0 == report all available language codes */
698 struct usb_string_descriptor *s = buf;
699 struct usb_gadget_strings **sp;
702 s->bDescriptorType = USB_DT_STRING;
704 sp = composite->strings;
706 collect_langs(sp, s->wData);
708 list_for_each_entry(c, &cdev->configs, list) {
711 collect_langs(sp, s->wData);
713 list_for_each_entry(f, &c->functions, list) {
716 collect_langs(sp, s->wData);
720 for (len = 0; len <= 126 && s->wData[len]; len++)
725 s->bLength = 2 * (len + 1);
729 /* Otherwise, look up and return a specified string. String IDs
730 * are device-scoped, so we look up each string table we're told
731 * about. These lookups are infrequent; simpler-is-better here.
733 if (composite->strings) {
734 len = lookup_string(composite->strings, buf, language, id);
738 list_for_each_entry(c, &cdev->configs, list) {
740 len = lookup_string(c->strings, buf, language, id);
744 list_for_each_entry(f, &c->functions, list) {
747 len = lookup_string(f->strings, buf, language, id);
756 * usb_string_id() - allocate an unused string ID
757 * @cdev: the device whose string descriptor IDs are being allocated
758 * Context: single threaded during gadget setup
760 * @usb_string_id() is called from bind() callbacks to allocate
761 * string IDs. Drivers for functions, configurations, or gadgets will
762 * then store that ID in the appropriate descriptors and string table.
764 * All string identifier should be allocated using this routine, to
765 * ensure that for example different functions don't wrongly assign
766 * different meanings to the same identifier.
768 int __init usb_string_id(struct usb_composite_dev *cdev)
770 if (cdev->next_string_id < 254) {
771 /* string id 0 is reserved */
772 cdev->next_string_id++;
773 return cdev->next_string_id;
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.
795 int usb_serial_configed;
797 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
799 struct usb_composite_dev *cdev = get_gadget_data(gadget);
800 struct usb_request *req = cdev->req;
801 int value = -EOPNOTSUPP;
802 u16 w_index = le16_to_cpu(ctrl->wIndex);
803 u8 intf = w_index & 0xFF;
804 u16 w_value = le16_to_cpu(ctrl->wValue);
805 u16 w_length = le16_to_cpu(ctrl->wLength);
806 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);
871 usb_serial_configed = 1;
873 case USB_REQ_GET_CONFIGURATION:
874 if (ctrl->bRequestType != USB_DIR_IN)
877 *(u8 *)req->buf = cdev->config->bConfigurationValue;
878 value = min(w_length, (u16) 1);
883 /* function drivers must handle get/set altsetting; if there's
884 * no get() method, we know only altsetting zero works.
886 case USB_REQ_SET_INTERFACE:
887 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
889 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
891 f = cdev->config->interface[intf];
894 if (w_value && !f->set_alt)
896 value = f->set_alt(f, w_index, w_value);
898 case USB_REQ_GET_INTERFACE:
899 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
901 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
903 f = cdev->config->interface[intf];
906 /* lots of interfaces only need altsetting zero... */
907 value = f->get_alt ? f->get_alt(f, w_index) : 0;
910 *((u8 *)req->buf) = value;
911 value = min(w_length, (u16) 1);
916 "non-core control req%02x.%02x v%04x i%04x l%d\n",
917 ctrl->bRequestType, ctrl->bRequest,
918 w_value, w_index, w_length);
920 /* functions always handle their interfaces ... punt other
921 * recipients (endpoint, other, WUSB, ...) to the current
922 * configuration code.
924 * REVISIT it could make sense to let the composite device
925 * take such requests too, if that's ever needed: to work
928 if ((ctrl->bRequestType & USB_RECIP_MASK)
929 == USB_RECIP_INTERFACE) {
930 if (cdev->config == NULL)
933 f = cdev->config->interface[intf];
935 value = f->setup(f, ctrl);
939 if (value < 0 && !f) {
940 struct usb_configuration *c;
944 value = c->setup(c, ctrl);
947 /* If the vendor request is not processed (value < 0),
948 * call all device registered configure setup callbacks
950 * This is used to handle the following cases:
951 * - vendor request is for the device and arrives before
953 * - Some devices are required to handle vendor request before
954 * setconfiguration such as MTP, USBNET.
958 struct usb_configuration *cfg;
960 list_for_each_entry(cfg, &cdev->configs, list) {
961 if (cfg && cfg->setup)
962 value = cfg->setup(cfg, ctrl);
969 /* respond with data transfer before status phase? */
972 req->zero = value < w_length;
973 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
975 DBG(cdev, "ep_queue --> %d\n", value);
977 composite_setup_complete(gadget->ep0, req);
982 /* device either stalls (value < 0) or reports success */
986 static void composite_disconnect(struct usb_gadget *gadget)
988 struct usb_composite_dev *cdev = get_gadget_data(gadget);
989 //unsigned long flags;
991 /* REVISIT: should we have config and device level
992 * disconnect callbacks?
994 spin_lock_irqsave(&cdev->lock, flags);
998 if (cdev->mute_switch)
999 cdev->mute_switch = 0;
1001 ; //schedule_work(&cdev->switch_work);
1002 // composite_switch_work(&cdev->switch_work);
1003 spin_unlock_irqrestore(&cdev->lock, flags);
1004 usb_serial_configed = 0;
1007 /*-------------------------------------------------------------------------*/
1009 static void /* __init_or_exit */
1010 composite_unbind(struct usb_gadget *gadget)
1012 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1014 /* composite_disconnect() must already have been called
1015 * by the underlying peripheral controller driver!
1016 * so there's no i/o concurrency that could affect the
1017 * state protected by cdev->lock.
1019 // WARN_ON(cdev->config);
1021 while (!list_empty(&cdev->configs)) {
1022 struct usb_configuration *c;
1024 c = list_first_entry(&cdev->configs,
1025 struct usb_configuration, list);
1026 while (!list_empty(&c->functions)) {
1027 struct usb_function *f;
1029 f = list_first_entry(&c->functions,
1030 struct usb_function, list);
1033 DBG(cdev, "unbind function '%s'/%p\n",
1036 /* may free memory for "f" */
1041 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1043 /* may free memory for "c" */
1046 if (composite->unbind)
1047 composite->unbind(cdev);
1050 kfree(cdev->req->buf);
1051 usb_ep_free_request(gadget->ep0, cdev->req);
1054 // switch_dev_unregister(&cdev->sdev);
1056 set_gadget_data(gadget, NULL);
1061 string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
1063 struct usb_string *str = tab->strings;
1065 for (str = tab->strings; str->s; str++) {
1066 if (str->id == id) {
1074 string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
1077 string_override_one(*tab, id, s);
1083 composite_switch_work(struct work_struct *data)
1085 struct usb_composite_dev *cdev =
1086 container_of(data, struct usb_composite_dev, switch_work);
1087 struct usb_configuration *config = cdev->config;
1090 switch_set_state(&cdev->sdev, config->bConfigurationValue);
1092 switch_set_state(&cdev->sdev, 0);
1095 static int __init composite_bind(struct usb_gadget *gadget)
1097 struct usb_composite_dev *cdev;
1098 int status = -ENOMEM;
1100 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1104 spin_lock_init(&cdev->lock);
1105 cdev->gadget = gadget;
1106 set_gadget_data(gadget, cdev);
1107 INIT_LIST_HEAD(&cdev->configs);
1109 /* preallocate control response and buffer */
1110 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1113 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1114 if (!cdev->req->buf)
1116 cdev->req->complete = composite_setup_complete;
1117 gadget->ep0->driver_data = cdev;
1119 cdev->bufsiz = USB_BUFSIZ;
1120 cdev->driver = composite;
1122 usb_gadget_set_selfpowered(gadget);
1123 /* interface and string IDs start at zero via kzalloc.
1124 * we force endpoints to start unassigned; few controller
1125 * drivers will zero ep->driver_data.
1127 usb_ep_autoconfig_reset(cdev->gadget);
1129 /* composite gadget needs to assign strings for whole device (like
1130 * serial number), register function drivers, potentially update
1131 * power state and consumption, etc
1133 status = composite->bind(cdev);
1137 cdev->sdev.name = "usb_configuration";
1138 status = switch_dev_register(&cdev->sdev);
1141 INIT_WORK(&cdev->switch_work, composite_switch_work);
1143 cdev->desc = *composite->dev;
1144 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1146 /* standardized runtime overrides for device ID data */
1148 cdev->desc.idVendor = cpu_to_le16(idVendor);
1150 cdev->desc.idProduct = cpu_to_le16(idProduct);
1152 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1154 /* strings can't be assigned before bind() allocates the
1155 * releavnt identifiers
1157 if (cdev->desc.iManufacturer && iManufacturer)
1158 string_override(composite->strings,
1159 cdev->desc.iManufacturer, iManufacturer);
1160 if (cdev->desc.iProduct && iProduct)
1161 string_override(composite->strings,
1162 cdev->desc.iProduct, iProduct);
1163 if (cdev->desc.iSerialNumber && iSerialNumber)
1164 string_override(composite->strings,
1165 cdev->desc.iSerialNumber, iSerialNumber);
1167 INFO(cdev, "%s ready\n", composite->name);
1171 composite_unbind(gadget);
1175 /*-------------------------------------------------------------------------*/
1178 composite_suspend(struct usb_gadget *gadget)
1180 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1181 struct usb_function *f;
1183 /* REVISIT: should we have config level
1184 * suspend/resume callbacks?
1186 DBG(cdev, "suspend\n");
1188 list_for_each_entry(f, &cdev->config->functions, list) {
1193 if (composite->suspend)
1194 composite->suspend(cdev);
1198 composite_resume(struct usb_gadget *gadget)
1200 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1201 struct usb_function *f;
1203 /* REVISIT: should we have config level
1204 * suspend/resume callbacks?
1206 DBG(cdev, "resume\n");
1207 if (composite->resume)
1208 composite->resume(cdev);
1210 list_for_each_entry(f, &cdev->config->functions, list) {
1219 composite_uevent(struct device *dev, struct kobj_uevent_env *env)
1221 struct usb_function *f = dev_get_drvdata(dev);
1224 /* this happens when the device is first created */
1228 if (add_uevent_var(env, "FUNCTION=%s", f->name))
1230 if (add_uevent_var(env, "ENABLED=%d", !f->disabled))
1236 /*-------------------------------------------------------------------------*/
1238 static struct usb_gadget_driver composite_driver = {
1239 .speed = USB_SPEED_HIGH,
1241 .bind = composite_bind,
1242 .unbind = composite_unbind,
1244 .setup = composite_setup,
1245 .disconnect = composite_disconnect,
1247 .suspend = composite_suspend,
1248 .resume = composite_resume,
1251 // .owner = THIS_MODULE,
1256 * usb_composite_register() - register a composite driver
1257 * @driver: the driver to register
1258 * Context: single threaded during gadget setup
1260 * This function is used to register drivers using the composite driver
1261 * framework. The return value is zero, or a negative errno value.
1262 * Those values normally come from the driver's @bind method, which does
1263 * all the work of setting up the driver to match the hardware.
1265 * On successful return, the gadget is ready to respond to requests from
1266 * the host, unless one of its components invokes usb_gadget_disconnect()
1267 * while it was binding. That would usually be done in order to wait for
1268 * some userspace participation.
1270 int __init usb_composite_register(struct usb_composite_driver *driver)
1272 if (!driver || !driver->dev || !driver->bind || composite)
1276 driver->name = "composite";
1277 //composite_driver.function = (char *) driver->name;
1278 //composite_driver.driver.name = driver->name;
1281 driver->class = class_create(THIS_MODULE, "usb_composite");
1282 if (IS_ERR(driver->class))
1283 return PTR_ERR(driver->class);
1284 //driver->class->dev_uevent = composite_uevent;
1286 return usb_gadget_register_driver(&composite_driver);
1290 * usb_composite_unregister() - unregister a composite driver
1291 * @driver: the driver to unregister
1293 * This function is used to unregister drivers using the composite
1296 void __exit usb_composite_unregister(struct usb_composite_driver *driver)
1298 if (composite != driver)
1300 usb_gadget_unregister_driver(&composite_driver);
1302 void calibration_reset_composite(void)