markers: turn marker_synchronize_unregister() into an inline
[linux/fpc-iii.git] / drivers / usb / gadget / composite.c
blob85c876c1f150b734eb2193e62760bb9f6b972785
1 /*
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 /**
75 * usb_add_function() - add a function to a configuration
76 * @config: the configuration
77 * @function: the function being added
78 * Context: single threaded during gadget setup
80 * After initialization, each configuration must have one or more
81 * functions added to it. Adding a function involves calling its @bind()
82 * method to allocate resources such as interface and string identifiers
83 * and endpoints.
85 * This function returns the value of the function's bind(), which is
86 * zero for success else a negative errno value.
88 int __init usb_add_function(struct usb_configuration *config,
89 struct usb_function *function)
91 int value = -EINVAL;
93 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
94 function->name, function,
95 config->label, config);
97 if (!function->set_alt || !function->disable)
98 goto done;
100 function->config = config;
101 list_add_tail(&function->list, &config->functions);
103 /* REVISIT *require* function->bind? */
104 if (function->bind) {
105 value = function->bind(config, function);
106 if (value < 0) {
107 list_del(&function->list);
108 function->config = NULL;
110 } else
111 value = 0;
113 /* We allow configurations that don't work at both speeds.
114 * If we run into a lowspeed Linux system, treat it the same
115 * as full speed ... it's the function drivers that will need
116 * to avoid bulk and ISO transfers.
118 if (!config->fullspeed && function->descriptors)
119 config->fullspeed = true;
120 if (!config->highspeed && function->hs_descriptors)
121 config->highspeed = true;
123 done:
124 if (value)
125 DBG(config->cdev, "adding '%s'/%p --> %d\n",
126 function->name, function, value);
127 return value;
131 * usb_interface_id() - allocate an unused interface ID
132 * @config: configuration associated with the interface
133 * @function: function handling the interface
134 * Context: single threaded during gadget setup
136 * usb_interface_id() is called from usb_function.bind() callbacks to
137 * allocate new interface IDs. The function driver will then store that
138 * ID in interface, association, CDC union, and other descriptors. It
139 * will also handle any control requests targetted at that interface,
140 * particularly changing its altsetting via set_alt(). There may
141 * also be class-specific or vendor-specific requests to handle.
143 * All interface identifier should be allocated using this routine, to
144 * ensure that for example different functions don't wrongly assign
145 * different meanings to the same identifier. Note that since interface
146 * identifers are configuration-specific, functions used in more than
147 * one configuration (or more than once in a given configuration) need
148 * multiple versions of the relevant descriptors.
150 * Returns the interface ID which was allocated; or -ENODEV if no
151 * more interface IDs can be allocated.
153 int __init usb_interface_id(struct usb_configuration *config,
154 struct usb_function *function)
156 unsigned id = config->next_interface_id;
158 if (id < MAX_CONFIG_INTERFACES) {
159 config->interface[id] = function;
160 config->next_interface_id = id + 1;
161 return id;
163 return -ENODEV;
166 static int config_buf(struct usb_configuration *config,
167 enum usb_device_speed speed, void *buf, u8 type)
169 struct usb_config_descriptor *c = buf;
170 void *next = buf + USB_DT_CONFIG_SIZE;
171 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
172 struct usb_function *f;
173 int status;
175 /* write the config descriptor */
176 c = buf;
177 c->bLength = USB_DT_CONFIG_SIZE;
178 c->bDescriptorType = type;
179 /* wTotalLength is written later */
180 c->bNumInterfaces = config->next_interface_id;
181 c->bConfigurationValue = config->bConfigurationValue;
182 c->iConfiguration = config->iConfiguration;
183 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
184 c->bMaxPower = config->bMaxPower;
186 /* There may be e.g. OTG descriptors */
187 if (config->descriptors) {
188 status = usb_descriptor_fillbuf(next, len,
189 config->descriptors);
190 if (status < 0)
191 return status;
192 len -= status;
193 next += status;
196 /* add each function's descriptors */
197 list_for_each_entry(f, &config->functions, list) {
198 struct usb_descriptor_header **descriptors;
200 if (speed == USB_SPEED_HIGH)
201 descriptors = f->hs_descriptors;
202 else
203 descriptors = f->descriptors;
204 if (!descriptors)
205 continue;
206 status = usb_descriptor_fillbuf(next, len,
207 (const struct usb_descriptor_header **) descriptors);
208 if (status < 0)
209 return status;
210 len -= status;
211 next += status;
214 len = next - buf;
215 c->wTotalLength = cpu_to_le16(len);
216 return len;
219 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
221 struct usb_gadget *gadget = cdev->gadget;
222 struct usb_configuration *c;
223 u8 type = w_value >> 8;
224 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
226 if (gadget_is_dualspeed(gadget)) {
227 int hs = 0;
229 if (gadget->speed == USB_SPEED_HIGH)
230 hs = 1;
231 if (type == USB_DT_OTHER_SPEED_CONFIG)
232 hs = !hs;
233 if (hs)
234 speed = USB_SPEED_HIGH;
238 /* This is a lookup by config *INDEX* */
239 w_value &= 0xff;
240 list_for_each_entry(c, &cdev->configs, list) {
241 /* ignore configs that won't work at this speed */
242 if (speed == USB_SPEED_HIGH) {
243 if (!c->highspeed)
244 continue;
245 } else {
246 if (!c->fullspeed)
247 continue;
249 if (w_value == 0)
250 return config_buf(c, speed, cdev->req->buf, type);
251 w_value--;
253 return -EINVAL;
256 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
258 struct usb_gadget *gadget = cdev->gadget;
259 struct usb_configuration *c;
260 unsigned count = 0;
261 int hs = 0;
263 if (gadget_is_dualspeed(gadget)) {
264 if (gadget->speed == USB_SPEED_HIGH)
265 hs = 1;
266 if (type == USB_DT_DEVICE_QUALIFIER)
267 hs = !hs;
269 list_for_each_entry(c, &cdev->configs, list) {
270 /* ignore configs that won't work at this speed */
271 if (hs) {
272 if (!c->highspeed)
273 continue;
274 } else {
275 if (!c->fullspeed)
276 continue;
278 count++;
280 return count;
283 static void device_qual(struct usb_composite_dev *cdev)
285 struct usb_qualifier_descriptor *qual = cdev->req->buf;
287 qual->bLength = sizeof(*qual);
288 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
289 /* POLICY: same bcdUSB and device type info at both speeds */
290 qual->bcdUSB = cdev->desc.bcdUSB;
291 qual->bDeviceClass = cdev->desc.bDeviceClass;
292 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
293 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
294 /* ASSUME same EP0 fifo size at both speeds */
295 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
296 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
297 qual->bRESERVED = 0;
300 /*-------------------------------------------------------------------------*/
302 static void reset_config(struct usb_composite_dev *cdev)
304 struct usb_function *f;
306 DBG(cdev, "reset config\n");
308 list_for_each_entry(f, &cdev->config->functions, list) {
309 if (f->disable)
310 f->disable(f);
312 cdev->config = NULL;
315 static int set_config(struct usb_composite_dev *cdev,
316 const struct usb_ctrlrequest *ctrl, unsigned number)
318 struct usb_gadget *gadget = cdev->gadget;
319 struct usb_configuration *c = NULL;
320 int result = -EINVAL;
321 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
322 int tmp;
324 if (cdev->config)
325 reset_config(cdev);
327 if (number) {
328 list_for_each_entry(c, &cdev->configs, list) {
329 if (c->bConfigurationValue == number) {
330 result = 0;
331 break;
334 if (result < 0)
335 goto done;
336 } else
337 result = 0;
339 INFO(cdev, "%s speed config #%d: %s\n",
340 ({ char *speed;
341 switch (gadget->speed) {
342 case USB_SPEED_LOW: speed = "low"; break;
343 case USB_SPEED_FULL: speed = "full"; break;
344 case USB_SPEED_HIGH: speed = "high"; break;
345 default: speed = "?"; break;
346 } ; speed; }), number, c ? c->label : "unconfigured");
348 if (!c)
349 goto done;
351 cdev->config = c;
353 /* Initialize all interfaces by setting them to altsetting zero. */
354 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
355 struct usb_function *f = c->interface[tmp];
357 if (!f)
358 break;
360 result = f->set_alt(f, tmp, 0);
361 if (result < 0) {
362 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
363 tmp, f->name, f, result);
365 reset_config(cdev);
366 goto done;
370 /* when we return, be sure our power usage is valid */
371 power = 2 * c->bMaxPower;
372 done:
373 usb_gadget_vbus_draw(gadget, power);
374 return result;
378 * usb_add_config() - add a configuration to a device.
379 * @cdev: wraps the USB gadget
380 * @config: the configuration, with bConfigurationValue assigned
381 * Context: single threaded during gadget setup
383 * One of the main tasks of a composite driver's bind() routine is to
384 * add each of the configurations it supports, using this routine.
386 * This function returns the value of the configuration's bind(), which
387 * is zero for success else a negative errno value. Binding configurations
388 * assigns global resources including string IDs, and per-configuration
389 * resources such as interface IDs and endpoints.
391 int __init usb_add_config(struct usb_composite_dev *cdev,
392 struct usb_configuration *config)
394 int status = -EINVAL;
395 struct usb_configuration *c;
397 DBG(cdev, "adding config #%u '%s'/%p\n",
398 config->bConfigurationValue,
399 config->label, config);
401 if (!config->bConfigurationValue || !config->bind)
402 goto done;
404 /* Prevent duplicate configuration identifiers */
405 list_for_each_entry(c, &cdev->configs, list) {
406 if (c->bConfigurationValue == config->bConfigurationValue) {
407 status = -EBUSY;
408 goto done;
412 config->cdev = cdev;
413 list_add_tail(&config->list, &cdev->configs);
415 INIT_LIST_HEAD(&config->functions);
416 config->next_interface_id = 0;
418 status = config->bind(config);
419 if (status < 0) {
420 list_del(&config->list);
421 config->cdev = NULL;
422 } else {
423 unsigned i;
425 DBG(cdev, "cfg %d/%p speeds:%s%s\n",
426 config->bConfigurationValue, config,
427 config->highspeed ? " high" : "",
428 config->fullspeed
429 ? (gadget_is_dualspeed(cdev->gadget)
430 ? " full"
431 : " full/low")
432 : "");
434 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
435 struct usb_function *f = config->interface[i];
437 if (!f)
438 continue;
439 DBG(cdev, " interface %d = %s/%p\n",
440 i, f->name, f);
444 /* set_alt(), or next config->bind(), sets up
445 * ep->driver_data as needed.
447 usb_ep_autoconfig_reset(cdev->gadget);
449 done:
450 if (status)
451 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
452 config->bConfigurationValue, status);
453 return status;
456 /*-------------------------------------------------------------------------*/
458 /* We support strings in multiple languages ... string descriptor zero
459 * says which languages are supported. The typical case will be that
460 * only one language (probably English) is used, with I18N handled on
461 * the host side.
464 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
466 const struct usb_gadget_strings *s;
467 u16 language;
468 __le16 *tmp;
470 while (*sp) {
471 s = *sp;
472 language = cpu_to_le16(s->language);
473 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
474 if (*tmp == language)
475 goto repeat;
477 *tmp++ = language;
478 repeat:
479 sp++;
483 static int lookup_string(
484 struct usb_gadget_strings **sp,
485 void *buf,
486 u16 language,
487 int id
490 struct usb_gadget_strings *s;
491 int value;
493 while (*sp) {
494 s = *sp++;
495 if (s->language != language)
496 continue;
497 value = usb_gadget_get_string(s, id, buf);
498 if (value > 0)
499 return value;
501 return -EINVAL;
504 static int get_string(struct usb_composite_dev *cdev,
505 void *buf, u16 language, int id)
507 struct usb_configuration *c;
508 struct usb_function *f;
509 int len;
511 /* Yes, not only is USB's I18N support probably more than most
512 * folk will ever care about ... also, it's all supported here.
513 * (Except for UTF8 support for Unicode's "Astral Planes".)
516 /* 0 == report all available language codes */
517 if (id == 0) {
518 struct usb_string_descriptor *s = buf;
519 struct usb_gadget_strings **sp;
521 memset(s, 0, 256);
522 s->bDescriptorType = USB_DT_STRING;
524 sp = composite->strings;
525 if (sp)
526 collect_langs(sp, s->wData);
528 list_for_each_entry(c, &cdev->configs, list) {
529 sp = c->strings;
530 if (sp)
531 collect_langs(sp, s->wData);
533 list_for_each_entry(f, &c->functions, list) {
534 sp = f->strings;
535 if (sp)
536 collect_langs(sp, s->wData);
540 for (len = 0; s->wData[len] && len <= 126; len++)
541 continue;
542 if (!len)
543 return -EINVAL;
545 s->bLength = 2 * (len + 1);
546 return s->bLength;
549 /* Otherwise, look up and return a specified string. String IDs
550 * are device-scoped, so we look up each string table we're told
551 * about. These lookups are infrequent; simpler-is-better here.
553 if (composite->strings) {
554 len = lookup_string(composite->strings, buf, language, id);
555 if (len > 0)
556 return len;
558 list_for_each_entry(c, &cdev->configs, list) {
559 if (c->strings) {
560 len = lookup_string(c->strings, buf, language, id);
561 if (len > 0)
562 return len;
564 list_for_each_entry(f, &c->functions, list) {
565 if (!f->strings)
566 continue;
567 len = lookup_string(f->strings, buf, language, id);
568 if (len > 0)
569 return len;
572 return -EINVAL;
576 * usb_string_id() - allocate an unused string ID
577 * @cdev: the device whose string descriptor IDs are being allocated
578 * Context: single threaded during gadget setup
580 * @usb_string_id() is called from bind() callbacks to allocate
581 * string IDs. Drivers for functions, configurations, or gadgets will
582 * then store that ID in the appropriate descriptors and string table.
584 * All string identifier should be allocated using this routine, to
585 * ensure that for example different functions don't wrongly assign
586 * different meanings to the same identifier.
588 int __init usb_string_id(struct usb_composite_dev *cdev)
590 if (cdev->next_string_id < 254) {
591 /* string id 0 is reserved */
592 cdev->next_string_id++;
593 return cdev->next_string_id;
595 return -ENODEV;
598 /*-------------------------------------------------------------------------*/
600 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
602 if (req->status || req->actual != req->length)
603 DBG((struct usb_composite_dev *) ep->driver_data,
604 "setup complete --> %d, %d/%d\n",
605 req->status, req->actual, req->length);
609 * The setup() callback implements all the ep0 functionality that's
610 * not handled lower down, in hardware or the hardware driver(like
611 * device and endpoint feature flags, and their status). It's all
612 * housekeeping for the gadget function we're implementing. Most of
613 * the work is in config and function specific setup.
615 static int
616 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
618 struct usb_composite_dev *cdev = get_gadget_data(gadget);
619 struct usb_request *req = cdev->req;
620 int value = -EOPNOTSUPP;
621 u16 w_index = le16_to_cpu(ctrl->wIndex);
622 u16 w_value = le16_to_cpu(ctrl->wValue);
623 u16 w_length = le16_to_cpu(ctrl->wLength);
624 struct usb_function *f = NULL;
626 /* partial re-init of the response message; the function or the
627 * gadget might need to intercept e.g. a control-OUT completion
628 * when we delegate to it.
630 req->zero = 0;
631 req->complete = composite_setup_complete;
632 req->length = USB_BUFSIZ;
633 gadget->ep0->driver_data = cdev;
635 switch (ctrl->bRequest) {
637 /* we handle all standard USB descriptors */
638 case USB_REQ_GET_DESCRIPTOR:
639 if (ctrl->bRequestType != USB_DIR_IN)
640 goto unknown;
641 switch (w_value >> 8) {
643 case USB_DT_DEVICE:
644 cdev->desc.bNumConfigurations =
645 count_configs(cdev, USB_DT_DEVICE);
646 value = min(w_length, (u16) sizeof cdev->desc);
647 memcpy(req->buf, &cdev->desc, value);
648 break;
649 case USB_DT_DEVICE_QUALIFIER:
650 if (!gadget_is_dualspeed(gadget))
651 break;
652 device_qual(cdev);
653 value = min_t(int, w_length,
654 sizeof(struct usb_qualifier_descriptor));
655 break;
656 case USB_DT_OTHER_SPEED_CONFIG:
657 if (!gadget_is_dualspeed(gadget))
658 break;
659 /* FALLTHROUGH */
660 case USB_DT_CONFIG:
661 value = config_desc(cdev, w_value);
662 if (value >= 0)
663 value = min(w_length, (u16) value);
664 break;
665 case USB_DT_STRING:
666 value = get_string(cdev, req->buf,
667 w_index, w_value & 0xff);
668 if (value >= 0)
669 value = min(w_length, (u16) value);
670 break;
672 break;
674 /* any number of configs can work */
675 case USB_REQ_SET_CONFIGURATION:
676 if (ctrl->bRequestType != 0)
677 goto unknown;
678 if (gadget_is_otg(gadget)) {
679 if (gadget->a_hnp_support)
680 DBG(cdev, "HNP available\n");
681 else if (gadget->a_alt_hnp_support)
682 DBG(cdev, "HNP on another port\n");
683 else
684 VDBG(cdev, "HNP inactive\n");
686 spin_lock(&cdev->lock);
687 value = set_config(cdev, ctrl, w_value);
688 spin_unlock(&cdev->lock);
689 break;
690 case USB_REQ_GET_CONFIGURATION:
691 if (ctrl->bRequestType != USB_DIR_IN)
692 goto unknown;
693 if (cdev->config)
694 *(u8 *)req->buf = cdev->config->bConfigurationValue;
695 else
696 *(u8 *)req->buf = 0;
697 value = min(w_length, (u16) 1);
698 break;
700 /* function drivers must handle get/set altsetting; if there's
701 * no get() method, we know only altsetting zero works.
703 case USB_REQ_SET_INTERFACE:
704 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
705 goto unknown;
706 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
707 break;
708 f = cdev->config->interface[w_index];
709 if (!f)
710 break;
711 if (w_value && !f->get_alt)
712 break;
713 value = f->set_alt(f, w_index, w_value);
714 break;
715 case USB_REQ_GET_INTERFACE:
716 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
717 goto unknown;
718 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
719 break;
720 f = cdev->config->interface[w_index];
721 if (!f)
722 break;
723 /* lots of interfaces only need altsetting zero... */
724 value = f->get_alt ? f->get_alt(f, w_index) : 0;
725 if (value < 0)
726 break;
727 *((u8 *)req->buf) = value;
728 value = min(w_length, (u16) 1);
729 break;
730 default:
731 unknown:
732 VDBG(cdev,
733 "non-core control req%02x.%02x v%04x i%04x l%d\n",
734 ctrl->bRequestType, ctrl->bRequest,
735 w_value, w_index, w_length);
737 /* functions always handle their interfaces ... punt other
738 * recipients (endpoint, other, WUSB, ...) to the current
739 * configuration code.
741 * REVISIT it could make sense to let the composite device
742 * take such requests too, if that's ever needed: to work
743 * in config 0, etc.
745 if ((ctrl->bRequestType & USB_RECIP_MASK)
746 == USB_RECIP_INTERFACE) {
747 f = cdev->config->interface[w_index];
748 if (f && f->setup)
749 value = f->setup(f, ctrl);
750 else
751 f = NULL;
753 if (value < 0 && !f) {
754 struct usb_configuration *c;
756 c = cdev->config;
757 if (c && c->setup)
758 value = c->setup(c, ctrl);
761 goto done;
764 /* respond with data transfer before status phase? */
765 if (value >= 0) {
766 req->length = value;
767 req->zero = value < w_length;
768 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
769 if (value < 0) {
770 DBG(cdev, "ep_queue --> %d\n", value);
771 req->status = 0;
772 composite_setup_complete(gadget->ep0, req);
776 done:
777 /* device either stalls (value < 0) or reports success */
778 return value;
781 static void composite_disconnect(struct usb_gadget *gadget)
783 struct usb_composite_dev *cdev = get_gadget_data(gadget);
784 unsigned long flags;
786 /* REVISIT: should we have config and device level
787 * disconnect callbacks?
789 spin_lock_irqsave(&cdev->lock, flags);
790 if (cdev->config)
791 reset_config(cdev);
792 spin_unlock_irqrestore(&cdev->lock, flags);
795 /*-------------------------------------------------------------------------*/
797 static void /* __init_or_exit */
798 composite_unbind(struct usb_gadget *gadget)
800 struct usb_composite_dev *cdev = get_gadget_data(gadget);
802 /* composite_disconnect() must already have been called
803 * by the underlying peripheral controller driver!
804 * so there's no i/o concurrency that could affect the
805 * state protected by cdev->lock.
807 WARN_ON(cdev->config);
809 while (!list_empty(&cdev->configs)) {
810 struct usb_configuration *c;
812 c = list_first_entry(&cdev->configs,
813 struct usb_configuration, list);
814 while (!list_empty(&c->functions)) {
815 struct usb_function *f;
817 f = list_first_entry(&c->functions,
818 struct usb_function, list);
819 list_del(&f->list);
820 if (f->unbind) {
821 DBG(cdev, "unbind function '%s'/%p\n",
822 f->name, f);
823 f->unbind(c, f);
824 /* may free memory for "f" */
827 list_del(&c->list);
828 if (c->unbind) {
829 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
830 c->unbind(c);
831 /* may free memory for "c" */
834 if (composite->unbind)
835 composite->unbind(cdev);
837 if (cdev->req) {
838 kfree(cdev->req->buf);
839 usb_ep_free_request(gadget->ep0, cdev->req);
841 kfree(cdev);
842 set_gadget_data(gadget, NULL);
843 composite = NULL;
846 static void __init
847 string_override_one(struct usb_gadget_strings *tab, u8 id, const char *s)
849 struct usb_string *str = tab->strings;
851 for (str = tab->strings; str->s; str++) {
852 if (str->id == id) {
853 str->s = s;
854 return;
859 static void __init
860 string_override(struct usb_gadget_strings **tab, u8 id, const char *s)
862 while (*tab) {
863 string_override_one(*tab, id, s);
864 tab++;
868 static int __init composite_bind(struct usb_gadget *gadget)
870 struct usb_composite_dev *cdev;
871 int status = -ENOMEM;
873 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
874 if (!cdev)
875 return status;
877 spin_lock_init(&cdev->lock);
878 cdev->gadget = gadget;
879 set_gadget_data(gadget, cdev);
880 INIT_LIST_HEAD(&cdev->configs);
882 /* preallocate control response and buffer */
883 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
884 if (!cdev->req)
885 goto fail;
886 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
887 if (!cdev->req->buf)
888 goto fail;
889 cdev->req->complete = composite_setup_complete;
890 gadget->ep0->driver_data = cdev;
892 cdev->bufsiz = USB_BUFSIZ;
893 cdev->driver = composite;
895 usb_gadget_set_selfpowered(gadget);
897 /* interface and string IDs start at zero via kzalloc.
898 * we force endpoints to start unassigned; few controller
899 * drivers will zero ep->driver_data.
901 usb_ep_autoconfig_reset(cdev->gadget);
903 /* composite gadget needs to assign strings for whole device (like
904 * serial number), register function drivers, potentially update
905 * power state and consumption, etc
907 status = composite->bind(cdev);
908 if (status < 0)
909 goto fail;
911 cdev->desc = *composite->dev;
912 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
914 /* standardized runtime overrides for device ID data */
915 if (idVendor)
916 cdev->desc.idVendor = cpu_to_le16(idVendor);
917 if (idProduct)
918 cdev->desc.idProduct = cpu_to_le16(idProduct);
919 if (bcdDevice)
920 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
922 /* strings can't be assigned before bind() allocates the
923 * releavnt identifiers
925 if (cdev->desc.iManufacturer && iManufacturer)
926 string_override(composite->strings,
927 cdev->desc.iManufacturer, iManufacturer);
928 if (cdev->desc.iProduct && iProduct)
929 string_override(composite->strings,
930 cdev->desc.iProduct, iProduct);
931 if (cdev->desc.iSerialNumber && iSerialNumber)
932 string_override(composite->strings,
933 cdev->desc.iSerialNumber, iSerialNumber);
935 INFO(cdev, "%s ready\n", composite->name);
936 return 0;
938 fail:
939 composite_unbind(gadget);
940 return status;
943 /*-------------------------------------------------------------------------*/
945 static void
946 composite_suspend(struct usb_gadget *gadget)
948 struct usb_composite_dev *cdev = get_gadget_data(gadget);
949 struct usb_function *f;
951 /* REVISIT: should we have config and device level
952 * suspend/resume callbacks?
954 DBG(cdev, "suspend\n");
955 if (cdev->config) {
956 list_for_each_entry(f, &cdev->config->functions, list) {
957 if (f->suspend)
958 f->suspend(f);
963 static void
964 composite_resume(struct usb_gadget *gadget)
966 struct usb_composite_dev *cdev = get_gadget_data(gadget);
967 struct usb_function *f;
969 /* REVISIT: should we have config and device level
970 * suspend/resume callbacks?
972 DBG(cdev, "resume\n");
973 if (cdev->config) {
974 list_for_each_entry(f, &cdev->config->functions, list) {
975 if (f->resume)
976 f->resume(f);
981 /*-------------------------------------------------------------------------*/
983 static struct usb_gadget_driver composite_driver = {
984 .speed = USB_SPEED_HIGH,
986 .bind = composite_bind,
987 .unbind = __exit_p(composite_unbind),
989 .setup = composite_setup,
990 .disconnect = composite_disconnect,
992 .suspend = composite_suspend,
993 .resume = composite_resume,
995 .driver = {
996 .owner = THIS_MODULE,
1001 * usb_composite_register() - register a composite driver
1002 * @driver: the driver to register
1003 * Context: single threaded during gadget setup
1005 * This function is used to register drivers using the composite driver
1006 * framework. The return value is zero, or a negative errno value.
1007 * Those values normally come from the driver's @bind method, which does
1008 * all the work of setting up the driver to match the hardware.
1010 * On successful return, the gadget is ready to respond to requests from
1011 * the host, unless one of its components invokes usb_gadget_disconnect()
1012 * while it was binding. That would usually be done in order to wait for
1013 * some userspace participation.
1015 int __init usb_composite_register(struct usb_composite_driver *driver)
1017 if (!driver || !driver->dev || !driver->bind || composite)
1018 return -EINVAL;
1020 if (!driver->name)
1021 driver->name = "composite";
1022 composite_driver.function = (char *) driver->name;
1023 composite_driver.driver.name = driver->name;
1024 composite = driver;
1026 return usb_gadget_register_driver(&composite_driver);
1030 * usb_composite_unregister() - unregister a composite driver
1031 * @driver: the driver to unregister
1033 * This function is used to unregister drivers using the composite
1034 * driver framework.
1036 void __exit usb_composite_unregister(struct usb_composite_driver *driver)
1038 if (composite != driver)
1039 return;
1040 usb_gadget_unregister_driver(&composite_driver);