Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / usb / gadget / composite.c
blobe4e56faa161bb874a8c57f323e13091a3f6b08f9
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/module.h>
27 #include <linux/device.h>
28 #include <linux/utsname.h>
30 #include <linux/usb/composite.h>
31 #include <asm/unaligned.h>
34 * The code in this file is utility code, used to build a gadget driver
35 * from one or more "function" drivers, one or more "configuration"
36 * objects, and a "usb_composite_driver" by gluing them together along
37 * with the relevant device-wide data.
40 /* big enough to hold our biggest descriptor */
41 #define USB_BUFSIZ 1024
43 static struct usb_composite_driver *composite;
44 static int (*composite_gadget_bind)(struct usb_composite_dev *cdev);
46 /* Some systems will need runtime overrides for the product identifiers
47 * published in the device descriptor, either numbers or strings or both.
48 * String parameters are in UTF-8 (superset of ASCII's 7 bit characters).
51 static ushort idVendor;
52 module_param(idVendor, ushort, 0);
53 MODULE_PARM_DESC(idVendor, "USB Vendor ID");
55 static ushort idProduct;
56 module_param(idProduct, ushort, 0);
57 MODULE_PARM_DESC(idProduct, "USB Product ID");
59 static ushort bcdDevice;
60 module_param(bcdDevice, ushort, 0);
61 MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
63 static char *iManufacturer;
64 module_param(iManufacturer, charp, 0);
65 MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
67 static char *iProduct;
68 module_param(iProduct, charp, 0);
69 MODULE_PARM_DESC(iProduct, "USB Product string");
71 static char *iSerialNumber;
72 module_param(iSerialNumber, charp, 0);
73 MODULE_PARM_DESC(iSerialNumber, "SerialNumber string");
75 static char composite_manufacturer[50];
77 /*-------------------------------------------------------------------------*/
78 /**
79 * next_ep_desc() - advance to the next EP descriptor
80 * @t: currect pointer within descriptor array
82 * Return: next EP descriptor or NULL
84 * Iterate over @t until either EP descriptor found or
85 * NULL (that indicates end of list) encountered
87 static struct usb_descriptor_header**
88 next_ep_desc(struct usb_descriptor_header **t)
90 for (; *t; t++) {
91 if ((*t)->bDescriptorType == USB_DT_ENDPOINT)
92 return t;
94 return NULL;
98 * for_each_ep_desc()- iterate over endpoint descriptors in the
99 * descriptors list
100 * @start: pointer within descriptor array.
101 * @ep_desc: endpoint descriptor to use as the loop cursor
103 #define for_each_ep_desc(start, ep_desc) \
104 for (ep_desc = next_ep_desc(start); \
105 ep_desc; ep_desc = next_ep_desc(ep_desc+1))
108 * config_ep_by_speed() - configures the given endpoint
109 * according to gadget speed.
110 * @g: pointer to the gadget
111 * @f: usb function
112 * @_ep: the endpoint to configure
114 * Return: error code, 0 on success
116 * This function chooses the right descriptors for a given
117 * endpoint according to gadget speed and saves it in the
118 * endpoint desc field. If the endpoint already has a descriptor
119 * assigned to it - overwrites it with currently corresponding
120 * descriptor. The endpoint maxpacket field is updated according
121 * to the chosen descriptor.
122 * Note: the supplied function should hold all the descriptors
123 * for supported speeds
125 int config_ep_by_speed(struct usb_gadget *g,
126 struct usb_function *f,
127 struct usb_ep *_ep)
129 struct usb_endpoint_descriptor *chosen_desc = NULL;
130 struct usb_descriptor_header **speed_desc = NULL;
132 struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
133 int want_comp_desc = 0;
135 struct usb_descriptor_header **d_spd; /* cursor for speed desc */
137 if (!g || !f || !_ep)
138 return -EIO;
140 /* select desired speed */
141 switch (g->speed) {
142 case USB_SPEED_SUPER:
143 if (gadget_is_superspeed(g)) {
144 speed_desc = f->ss_descriptors;
145 want_comp_desc = 1;
146 break;
148 /* else: Fall trough */
149 case USB_SPEED_HIGH:
150 if (gadget_is_dualspeed(g)) {
151 speed_desc = f->hs_descriptors;
152 break;
154 /* else: fall through */
155 default:
156 speed_desc = f->descriptors;
158 /* find descriptors */
159 for_each_ep_desc(speed_desc, d_spd) {
160 chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
161 if (chosen_desc->bEndpointAddress == _ep->address)
162 goto ep_found;
164 return -EIO;
166 ep_found:
167 /* commit results */
168 _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
169 _ep->desc = chosen_desc;
170 _ep->comp_desc = NULL;
171 _ep->maxburst = 0;
172 _ep->mult = 0;
173 if (!want_comp_desc)
174 return 0;
177 * Companion descriptor should follow EP descriptor
178 * USB 3.0 spec, #9.6.7
180 comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
181 if (!comp_desc ||
182 (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
183 return -EIO;
184 _ep->comp_desc = comp_desc;
185 if (g->speed == USB_SPEED_SUPER) {
186 switch (usb_endpoint_type(_ep->desc)) {
187 case USB_ENDPOINT_XFER_BULK:
188 case USB_ENDPOINT_XFER_INT:
189 _ep->maxburst = comp_desc->bMaxBurst;
190 break;
191 case USB_ENDPOINT_XFER_ISOC:
192 /* mult: bits 1:0 of bmAttributes */
193 _ep->mult = comp_desc->bmAttributes & 0x3;
194 break;
195 default:
196 /* Do nothing for control endpoints */
197 break;
200 return 0;
204 * usb_add_function() - add a function to a configuration
205 * @config: the configuration
206 * @function: the function being added
207 * Context: single threaded during gadget setup
209 * After initialization, each configuration must have one or more
210 * functions added to it. Adding a function involves calling its @bind()
211 * method to allocate resources such as interface and string identifiers
212 * and endpoints.
214 * This function returns the value of the function's bind(), which is
215 * zero for success else a negative errno value.
217 int usb_add_function(struct usb_configuration *config,
218 struct usb_function *function)
220 int value = -EINVAL;
222 DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
223 function->name, function,
224 config->label, config);
226 if (!function->set_alt || !function->disable)
227 goto done;
229 function->config = config;
230 list_add_tail(&function->list, &config->functions);
232 /* REVISIT *require* function->bind? */
233 if (function->bind) {
234 value = function->bind(config, function);
235 if (value < 0) {
236 list_del(&function->list);
237 function->config = NULL;
239 } else
240 value = 0;
242 /* We allow configurations that don't work at both speeds.
243 * If we run into a lowspeed Linux system, treat it the same
244 * as full speed ... it's the function drivers that will need
245 * to avoid bulk and ISO transfers.
247 if (!config->fullspeed && function->descriptors)
248 config->fullspeed = true;
249 if (!config->highspeed && function->hs_descriptors)
250 config->highspeed = true;
251 if (!config->superspeed && function->ss_descriptors)
252 config->superspeed = true;
254 done:
255 if (value)
256 DBG(config->cdev, "adding '%s'/%p --> %d\n",
257 function->name, function, value);
258 return value;
262 * usb_function_deactivate - prevent function and gadget enumeration
263 * @function: the function that isn't yet ready to respond
265 * Blocks response of the gadget driver to host enumeration by
266 * preventing the data line pullup from being activated. This is
267 * normally called during @bind() processing to change from the
268 * initial "ready to respond" state, or when a required resource
269 * becomes available.
271 * For example, drivers that serve as a passthrough to a userspace
272 * daemon can block enumeration unless that daemon (such as an OBEX,
273 * MTP, or print server) is ready to handle host requests.
275 * Not all systems support software control of their USB peripheral
276 * data pullups.
278 * Returns zero on success, else negative errno.
280 int usb_function_deactivate(struct usb_function *function)
282 struct usb_composite_dev *cdev = function->config->cdev;
283 unsigned long flags;
284 int status = 0;
286 spin_lock_irqsave(&cdev->lock, flags);
288 if (cdev->deactivations == 0)
289 status = usb_gadget_disconnect(cdev->gadget);
290 if (status == 0)
291 cdev->deactivations++;
293 spin_unlock_irqrestore(&cdev->lock, flags);
294 return status;
298 * usb_function_activate - allow function and gadget enumeration
299 * @function: function on which usb_function_activate() was called
301 * Reverses effect of usb_function_deactivate(). If no more functions
302 * are delaying their activation, the gadget driver will respond to
303 * host enumeration procedures.
305 * Returns zero on success, else negative errno.
307 int usb_function_activate(struct usb_function *function)
309 struct usb_composite_dev *cdev = function->config->cdev;
310 int status = 0;
312 spin_lock(&cdev->lock);
314 if (WARN_ON(cdev->deactivations == 0))
315 status = -EINVAL;
316 else {
317 cdev->deactivations--;
318 if (cdev->deactivations == 0)
319 status = usb_gadget_connect(cdev->gadget);
322 spin_unlock(&cdev->lock);
323 return status;
327 * usb_interface_id() - allocate an unused interface ID
328 * @config: configuration associated with the interface
329 * @function: function handling the interface
330 * Context: single threaded during gadget setup
332 * usb_interface_id() is called from usb_function.bind() callbacks to
333 * allocate new interface IDs. The function driver will then store that
334 * ID in interface, association, CDC union, and other descriptors. It
335 * will also handle any control requests targeted at that interface,
336 * particularly changing its altsetting via set_alt(). There may
337 * also be class-specific or vendor-specific requests to handle.
339 * All interface identifier should be allocated using this routine, to
340 * ensure that for example different functions don't wrongly assign
341 * different meanings to the same identifier. Note that since interface
342 * identifiers are configuration-specific, functions used in more than
343 * one configuration (or more than once in a given configuration) need
344 * multiple versions of the relevant descriptors.
346 * Returns the interface ID which was allocated; or -ENODEV if no
347 * more interface IDs can be allocated.
349 int usb_interface_id(struct usb_configuration *config,
350 struct usb_function *function)
352 unsigned id = config->next_interface_id;
354 if (id < MAX_CONFIG_INTERFACES) {
355 config->interface[id] = function;
356 config->next_interface_id = id + 1;
357 return id;
359 return -ENODEV;
362 static int config_buf(struct usb_configuration *config,
363 enum usb_device_speed speed, void *buf, u8 type)
365 struct usb_config_descriptor *c = buf;
366 void *next = buf + USB_DT_CONFIG_SIZE;
367 int len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
368 struct usb_function *f;
369 int status;
371 /* write the config descriptor */
372 c = buf;
373 c->bLength = USB_DT_CONFIG_SIZE;
374 c->bDescriptorType = type;
375 /* wTotalLength is written later */
376 c->bNumInterfaces = config->next_interface_id;
377 c->bConfigurationValue = config->bConfigurationValue;
378 c->iConfiguration = config->iConfiguration;
379 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
380 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
382 /* There may be e.g. OTG descriptors */
383 if (config->descriptors) {
384 status = usb_descriptor_fillbuf(next, len,
385 config->descriptors);
386 if (status < 0)
387 return status;
388 len -= status;
389 next += status;
392 /* add each function's descriptors */
393 list_for_each_entry(f, &config->functions, list) {
394 struct usb_descriptor_header **descriptors;
396 switch (speed) {
397 case USB_SPEED_SUPER:
398 descriptors = f->ss_descriptors;
399 break;
400 case USB_SPEED_HIGH:
401 descriptors = f->hs_descriptors;
402 break;
403 default:
404 descriptors = f->descriptors;
407 if (!descriptors)
408 continue;
409 status = usb_descriptor_fillbuf(next, len,
410 (const struct usb_descriptor_header **) descriptors);
411 if (status < 0)
412 return status;
413 len -= status;
414 next += status;
417 len = next - buf;
418 c->wTotalLength = cpu_to_le16(len);
419 return len;
422 static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
424 struct usb_gadget *gadget = cdev->gadget;
425 struct usb_configuration *c;
426 u8 type = w_value >> 8;
427 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
429 if (gadget->speed == USB_SPEED_SUPER)
430 speed = gadget->speed;
431 else if (gadget_is_dualspeed(gadget)) {
432 int hs = 0;
433 if (gadget->speed == USB_SPEED_HIGH)
434 hs = 1;
435 if (type == USB_DT_OTHER_SPEED_CONFIG)
436 hs = !hs;
437 if (hs)
438 speed = USB_SPEED_HIGH;
442 /* This is a lookup by config *INDEX* */
443 w_value &= 0xff;
444 list_for_each_entry(c, &cdev->configs, list) {
445 /* ignore configs that won't work at this speed */
446 switch (speed) {
447 case USB_SPEED_SUPER:
448 if (!c->superspeed)
449 continue;
450 break;
451 case USB_SPEED_HIGH:
452 if (!c->highspeed)
453 continue;
454 break;
455 default:
456 if (!c->fullspeed)
457 continue;
460 if (w_value == 0)
461 return config_buf(c, speed, cdev->req->buf, type);
462 w_value--;
464 return -EINVAL;
467 static int count_configs(struct usb_composite_dev *cdev, unsigned type)
469 struct usb_gadget *gadget = cdev->gadget;
470 struct usb_configuration *c;
471 unsigned count = 0;
472 int hs = 0;
473 int ss = 0;
475 if (gadget_is_dualspeed(gadget)) {
476 if (gadget->speed == USB_SPEED_HIGH)
477 hs = 1;
478 if (gadget->speed == USB_SPEED_SUPER)
479 ss = 1;
480 if (type == USB_DT_DEVICE_QUALIFIER)
481 hs = !hs;
483 list_for_each_entry(c, &cdev->configs, list) {
484 /* ignore configs that won't work at this speed */
485 if (ss) {
486 if (!c->superspeed)
487 continue;
488 } else if (hs) {
489 if (!c->highspeed)
490 continue;
491 } else {
492 if (!c->fullspeed)
493 continue;
495 count++;
497 return count;
501 * bos_desc() - prepares the BOS descriptor.
502 * @cdev: pointer to usb_composite device to generate the bos
503 * descriptor for
505 * This function generates the BOS (Binary Device Object)
506 * descriptor and its device capabilities descriptors. The BOS
507 * descriptor should be supported by a SuperSpeed device.
509 static int bos_desc(struct usb_composite_dev *cdev)
511 struct usb_ext_cap_descriptor *usb_ext;
512 struct usb_ss_cap_descriptor *ss_cap;
513 struct usb_dcd_config_params dcd_config_params;
514 struct usb_bos_descriptor *bos = cdev->req->buf;
516 bos->bLength = USB_DT_BOS_SIZE;
517 bos->bDescriptorType = USB_DT_BOS;
519 bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
520 bos->bNumDeviceCaps = 0;
523 * A SuperSpeed device shall include the USB2.0 extension descriptor
524 * and shall support LPM when operating in USB2.0 HS mode.
526 usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
527 bos->bNumDeviceCaps++;
528 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
529 usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
530 usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
531 usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
532 usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT);
535 * The Superspeed USB Capability descriptor shall be implemented by all
536 * SuperSpeed devices.
538 ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
539 bos->bNumDeviceCaps++;
540 le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
541 ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
542 ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
543 ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
544 ss_cap->bmAttributes = 0; /* LTM is not supported yet */
545 ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
546 USB_FULL_SPEED_OPERATION |
547 USB_HIGH_SPEED_OPERATION |
548 USB_5GBPS_OPERATION);
549 ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
551 /* Get Controller configuration */
552 if (cdev->gadget->ops->get_config_params)
553 cdev->gadget->ops->get_config_params(&dcd_config_params);
554 else {
555 dcd_config_params.bU1devExitLat = USB_DEFULT_U1_DEV_EXIT_LAT;
556 dcd_config_params.bU2DevExitLat =
557 cpu_to_le16(USB_DEFULT_U2_DEV_EXIT_LAT);
559 ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
560 ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
562 return le16_to_cpu(bos->wTotalLength);
565 static void device_qual(struct usb_composite_dev *cdev)
567 struct usb_qualifier_descriptor *qual = cdev->req->buf;
569 qual->bLength = sizeof(*qual);
570 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
571 /* POLICY: same bcdUSB and device type info at both speeds */
572 qual->bcdUSB = cdev->desc.bcdUSB;
573 qual->bDeviceClass = cdev->desc.bDeviceClass;
574 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
575 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
576 /* ASSUME same EP0 fifo size at both speeds */
577 qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
578 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
579 qual->bRESERVED = 0;
582 /*-------------------------------------------------------------------------*/
584 static void reset_config(struct usb_composite_dev *cdev)
586 struct usb_function *f;
588 DBG(cdev, "reset config\n");
590 list_for_each_entry(f, &cdev->config->functions, list) {
591 if (f->disable)
592 f->disable(f);
594 bitmap_zero(f->endpoints, 32);
596 cdev->config = NULL;
599 static int set_config(struct usb_composite_dev *cdev,
600 const struct usb_ctrlrequest *ctrl, unsigned number)
602 struct usb_gadget *gadget = cdev->gadget;
603 struct usb_configuration *c = NULL;
604 int result = -EINVAL;
605 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
606 int tmp;
608 if (number) {
609 list_for_each_entry(c, &cdev->configs, list) {
610 if (c->bConfigurationValue == number) {
612 * We disable the FDs of the previous
613 * configuration only if the new configuration
614 * is a valid one
616 if (cdev->config)
617 reset_config(cdev);
618 result = 0;
619 break;
622 if (result < 0)
623 goto done;
624 } else { /* Zero configuration value - need to reset the config */
625 if (cdev->config)
626 reset_config(cdev);
627 result = 0;
630 INFO(cdev, "%s speed config #%d: %s\n",
631 ({ char *speed;
632 switch (gadget->speed) {
633 case USB_SPEED_LOW:
634 speed = "low";
635 break;
636 case USB_SPEED_FULL:
637 speed = "full";
638 break;
639 case USB_SPEED_HIGH:
640 speed = "high";
641 break;
642 case USB_SPEED_SUPER:
643 speed = "super";
644 break;
645 default:
646 speed = "?";
647 break;
648 } ; speed; }), number, c ? c->label : "unconfigured");
650 if (!c)
651 goto done;
653 cdev->config = c;
655 /* Initialize all interfaces by setting them to altsetting zero. */
656 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
657 struct usb_function *f = c->interface[tmp];
658 struct usb_descriptor_header **descriptors;
660 if (!f)
661 break;
664 * Record which endpoints are used by the function. This is used
665 * to dispatch control requests targeted at that endpoint to the
666 * function's setup callback instead of the current
667 * configuration's setup callback.
669 switch (gadget->speed) {
670 case USB_SPEED_SUPER:
671 descriptors = f->ss_descriptors;
672 break;
673 case USB_SPEED_HIGH:
674 descriptors = f->hs_descriptors;
675 break;
676 default:
677 descriptors = f->descriptors;
680 for (; *descriptors; ++descriptors) {
681 struct usb_endpoint_descriptor *ep;
682 int addr;
684 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
685 continue;
687 ep = (struct usb_endpoint_descriptor *)*descriptors;
688 addr = ((ep->bEndpointAddress & 0x80) >> 3)
689 | (ep->bEndpointAddress & 0x0f);
690 set_bit(addr, f->endpoints);
693 result = f->set_alt(f, tmp, 0);
694 if (result < 0) {
695 DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
696 tmp, f->name, f, result);
698 reset_config(cdev);
699 goto done;
702 if (result == USB_GADGET_DELAYED_STATUS) {
703 DBG(cdev,
704 "%s: interface %d (%s) requested delayed status\n",
705 __func__, tmp, f->name);
706 cdev->delayed_status++;
707 DBG(cdev, "delayed_status count %d\n",
708 cdev->delayed_status);
712 /* when we return, be sure our power usage is valid */
713 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
714 done:
715 usb_gadget_vbus_draw(gadget, power);
716 if (result >= 0 && cdev->delayed_status)
717 result = USB_GADGET_DELAYED_STATUS;
718 return result;
722 * usb_add_config() - add a configuration to a device.
723 * @cdev: wraps the USB gadget
724 * @config: the configuration, with bConfigurationValue assigned
725 * @bind: the configuration's bind function
726 * Context: single threaded during gadget setup
728 * One of the main tasks of a composite @bind() routine is to
729 * add each of the configurations it supports, using this routine.
731 * This function returns the value of the configuration's @bind(), which
732 * is zero for success else a negative errno value. Binding configurations
733 * assigns global resources including string IDs, and per-configuration
734 * resources such as interface IDs and endpoints.
736 int usb_add_config(struct usb_composite_dev *cdev,
737 struct usb_configuration *config,
738 int (*bind)(struct usb_configuration *))
740 int status = -EINVAL;
741 struct usb_configuration *c;
743 DBG(cdev, "adding config #%u '%s'/%p\n",
744 config->bConfigurationValue,
745 config->label, config);
747 if (!config->bConfigurationValue || !bind)
748 goto done;
750 /* Prevent duplicate configuration identifiers */
751 list_for_each_entry(c, &cdev->configs, list) {
752 if (c->bConfigurationValue == config->bConfigurationValue) {
753 status = -EBUSY;
754 goto done;
758 config->cdev = cdev;
759 list_add_tail(&config->list, &cdev->configs);
761 INIT_LIST_HEAD(&config->functions);
762 config->next_interface_id = 0;
764 status = bind(config);
765 if (status < 0) {
766 list_del(&config->list);
767 config->cdev = NULL;
768 } else {
769 unsigned i;
771 DBG(cdev, "cfg %d/%p speeds:%s%s%s\n",
772 config->bConfigurationValue, config,
773 config->superspeed ? " super" : "",
774 config->highspeed ? " high" : "",
775 config->fullspeed
776 ? (gadget_is_dualspeed(cdev->gadget)
777 ? " full"
778 : " full/low")
779 : "");
781 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
782 struct usb_function *f = config->interface[i];
784 if (!f)
785 continue;
786 DBG(cdev, " interface %d = %s/%p\n",
787 i, f->name, f);
791 /* set_alt(), or next bind(), sets up
792 * ep->driver_data as needed.
794 usb_ep_autoconfig_reset(cdev->gadget);
796 done:
797 if (status)
798 DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
799 config->bConfigurationValue, status);
800 return status;
803 /*-------------------------------------------------------------------------*/
805 /* We support strings in multiple languages ... string descriptor zero
806 * says which languages are supported. The typical case will be that
807 * only one language (probably English) is used, with I18N handled on
808 * the host side.
811 static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
813 const struct usb_gadget_strings *s;
814 u16 language;
815 __le16 *tmp;
817 while (*sp) {
818 s = *sp;
819 language = cpu_to_le16(s->language);
820 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
821 if (*tmp == language)
822 goto repeat;
824 *tmp++ = language;
825 repeat:
826 sp++;
830 static int lookup_string(
831 struct usb_gadget_strings **sp,
832 void *buf,
833 u16 language,
834 int id
837 struct usb_gadget_strings *s;
838 int value;
840 while (*sp) {
841 s = *sp++;
842 if (s->language != language)
843 continue;
844 value = usb_gadget_get_string(s, id, buf);
845 if (value > 0)
846 return value;
848 return -EINVAL;
851 static int get_string(struct usb_composite_dev *cdev,
852 void *buf, u16 language, int id)
854 struct usb_configuration *c;
855 struct usb_function *f;
856 int len;
857 const char *str;
859 /* Yes, not only is USB's I18N support probably more than most
860 * folk will ever care about ... also, it's all supported here.
861 * (Except for UTF8 support for Unicode's "Astral Planes".)
864 /* 0 == report all available language codes */
865 if (id == 0) {
866 struct usb_string_descriptor *s = buf;
867 struct usb_gadget_strings **sp;
869 memset(s, 0, 256);
870 s->bDescriptorType = USB_DT_STRING;
872 sp = composite->strings;
873 if (sp)
874 collect_langs(sp, s->wData);
876 list_for_each_entry(c, &cdev->configs, list) {
877 sp = c->strings;
878 if (sp)
879 collect_langs(sp, s->wData);
881 list_for_each_entry(f, &c->functions, list) {
882 sp = f->strings;
883 if (sp)
884 collect_langs(sp, s->wData);
888 for (len = 0; len <= 126 && s->wData[len]; len++)
889 continue;
890 if (!len)
891 return -EINVAL;
893 s->bLength = 2 * (len + 1);
894 return s->bLength;
897 /* Otherwise, look up and return a specified string. First
898 * check if the string has not been overridden.
900 if (cdev->manufacturer_override == id)
901 str = iManufacturer ?: composite->iManufacturer ?:
902 composite_manufacturer;
903 else if (cdev->product_override == id)
904 str = iProduct ?: composite->iProduct;
905 else if (cdev->serial_override == id)
906 str = iSerialNumber;
907 else
908 str = NULL;
909 if (str) {
910 struct usb_gadget_strings strings = {
911 .language = language,
912 .strings = &(struct usb_string) { 0xff, str }
914 return usb_gadget_get_string(&strings, 0xff, buf);
917 /* String IDs are device-scoped, so we look up each string
918 * table we're told about. These lookups are infrequent;
919 * simpler-is-better here.
921 if (composite->strings) {
922 len = lookup_string(composite->strings, buf, language, id);
923 if (len > 0)
924 return len;
926 list_for_each_entry(c, &cdev->configs, list) {
927 if (c->strings) {
928 len = lookup_string(c->strings, buf, language, id);
929 if (len > 0)
930 return len;
932 list_for_each_entry(f, &c->functions, list) {
933 if (!f->strings)
934 continue;
935 len = lookup_string(f->strings, buf, language, id);
936 if (len > 0)
937 return len;
940 return -EINVAL;
944 * usb_string_id() - allocate an unused string ID
945 * @cdev: the device whose string descriptor IDs are being allocated
946 * Context: single threaded during gadget setup
948 * @usb_string_id() is called from bind() callbacks to allocate
949 * string IDs. Drivers for functions, configurations, or gadgets will
950 * then store that ID in the appropriate descriptors and string table.
952 * All string identifier should be allocated using this,
953 * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
954 * that for example different functions don't wrongly assign different
955 * meanings to the same identifier.
957 int usb_string_id(struct usb_composite_dev *cdev)
959 if (cdev->next_string_id < 254) {
960 /* string id 0 is reserved by USB spec for list of
961 * supported languages */
962 /* 255 reserved as well? -- mina86 */
963 cdev->next_string_id++;
964 return cdev->next_string_id;
966 return -ENODEV;
970 * usb_string_ids() - allocate unused string IDs in batch
971 * @cdev: the device whose string descriptor IDs are being allocated
972 * @str: an array of usb_string objects to assign numbers to
973 * Context: single threaded during gadget setup
975 * @usb_string_ids() is called from bind() callbacks to allocate
976 * string IDs. Drivers for functions, configurations, or gadgets will
977 * then copy IDs from the string table to the appropriate descriptors
978 * and string table for other languages.
980 * All string identifier should be allocated using this,
981 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
982 * example different functions don't wrongly assign different meanings
983 * to the same identifier.
985 int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
987 int next = cdev->next_string_id;
989 for (; str->s; ++str) {
990 if (unlikely(next >= 254))
991 return -ENODEV;
992 str->id = ++next;
995 cdev->next_string_id = next;
997 return 0;
1001 * usb_string_ids_n() - allocate unused string IDs in batch
1002 * @c: the device whose string descriptor IDs are being allocated
1003 * @n: number of string IDs to allocate
1004 * Context: single threaded during gadget setup
1006 * Returns the first requested ID. This ID and next @n-1 IDs are now
1007 * valid IDs. At least provided that @n is non-zero because if it
1008 * is, returns last requested ID which is now very useful information.
1010 * @usb_string_ids_n() is called from bind() callbacks to allocate
1011 * string IDs. Drivers for functions, configurations, or gadgets will
1012 * then store that ID in the appropriate descriptors and string table.
1014 * All string identifier should be allocated using this,
1015 * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
1016 * example different functions don't wrongly assign different meanings
1017 * to the same identifier.
1019 int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
1021 unsigned next = c->next_string_id;
1022 if (unlikely(n > 254 || (unsigned)next + n > 254))
1023 return -ENODEV;
1024 c->next_string_id += n;
1025 return next + 1;
1029 /*-------------------------------------------------------------------------*/
1031 static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
1033 if (req->status || req->actual != req->length)
1034 DBG((struct usb_composite_dev *) ep->driver_data,
1035 "setup complete --> %d, %d/%d\n",
1036 req->status, req->actual, req->length);
1040 * The setup() callback implements all the ep0 functionality that's
1041 * not handled lower down, in hardware or the hardware driver(like
1042 * device and endpoint feature flags, and their status). It's all
1043 * housekeeping for the gadget function we're implementing. Most of
1044 * the work is in config and function specific setup.
1046 static int
1047 composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
1049 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1050 struct usb_request *req = cdev->req;
1051 int value = -EOPNOTSUPP;
1052 int status = 0;
1053 u16 w_index = le16_to_cpu(ctrl->wIndex);
1054 u8 intf = w_index & 0xFF;
1055 u16 w_value = le16_to_cpu(ctrl->wValue);
1056 u16 w_length = le16_to_cpu(ctrl->wLength);
1057 struct usb_function *f = NULL;
1058 u8 endp;
1060 /* partial re-init of the response message; the function or the
1061 * gadget might need to intercept e.g. a control-OUT completion
1062 * when we delegate to it.
1064 req->zero = 0;
1065 req->complete = composite_setup_complete;
1066 req->length = 0;
1067 gadget->ep0->driver_data = cdev;
1069 switch (ctrl->bRequest) {
1071 /* we handle all standard USB descriptors */
1072 case USB_REQ_GET_DESCRIPTOR:
1073 if (ctrl->bRequestType != USB_DIR_IN)
1074 goto unknown;
1075 switch (w_value >> 8) {
1077 case USB_DT_DEVICE:
1078 cdev->desc.bNumConfigurations =
1079 count_configs(cdev, USB_DT_DEVICE);
1080 cdev->desc.bMaxPacketSize0 =
1081 cdev->gadget->ep0->maxpacket;
1082 if (gadget_is_superspeed(gadget)) {
1083 if (gadget->speed >= USB_SPEED_SUPER) {
1084 cdev->desc.bcdUSB = cpu_to_le16(0x0300);
1085 cdev->desc.bMaxPacketSize0 = 9;
1086 } else {
1087 cdev->desc.bcdUSB = cpu_to_le16(0x0210);
1091 value = min(w_length, (u16) sizeof cdev->desc);
1092 memcpy(req->buf, &cdev->desc, value);
1093 break;
1094 case USB_DT_DEVICE_QUALIFIER:
1095 if (!gadget_is_dualspeed(gadget) ||
1096 gadget->speed >= USB_SPEED_SUPER)
1097 break;
1098 device_qual(cdev);
1099 value = min_t(int, w_length,
1100 sizeof(struct usb_qualifier_descriptor));
1101 break;
1102 case USB_DT_OTHER_SPEED_CONFIG:
1103 if (!gadget_is_dualspeed(gadget) ||
1104 gadget->speed >= USB_SPEED_SUPER)
1105 break;
1106 /* FALLTHROUGH */
1107 case USB_DT_CONFIG:
1108 value = config_desc(cdev, w_value);
1109 if (value >= 0)
1110 value = min(w_length, (u16) value);
1111 break;
1112 case USB_DT_STRING:
1113 value = get_string(cdev, req->buf,
1114 w_index, w_value & 0xff);
1115 if (value >= 0)
1116 value = min(w_length, (u16) value);
1117 break;
1118 case USB_DT_BOS:
1119 if (gadget_is_superspeed(gadget)) {
1120 value = bos_desc(cdev);
1121 value = min(w_length, (u16) value);
1123 break;
1125 break;
1127 /* any number of configs can work */
1128 case USB_REQ_SET_CONFIGURATION:
1129 if (ctrl->bRequestType != 0)
1130 goto unknown;
1131 if (gadget_is_otg(gadget)) {
1132 if (gadget->a_hnp_support)
1133 DBG(cdev, "HNP available\n");
1134 else if (gadget->a_alt_hnp_support)
1135 DBG(cdev, "HNP on another port\n");
1136 else
1137 VDBG(cdev, "HNP inactive\n");
1139 spin_lock(&cdev->lock);
1140 value = set_config(cdev, ctrl, w_value);
1141 spin_unlock(&cdev->lock);
1142 break;
1143 case USB_REQ_GET_CONFIGURATION:
1144 if (ctrl->bRequestType != USB_DIR_IN)
1145 goto unknown;
1146 if (cdev->config)
1147 *(u8 *)req->buf = cdev->config->bConfigurationValue;
1148 else
1149 *(u8 *)req->buf = 0;
1150 value = min(w_length, (u16) 1);
1151 break;
1153 /* function drivers must handle get/set altsetting; if there's
1154 * no get() method, we know only altsetting zero works.
1156 case USB_REQ_SET_INTERFACE:
1157 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
1158 goto unknown;
1159 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1160 break;
1161 f = cdev->config->interface[intf];
1162 if (!f)
1163 break;
1164 if (w_value && !f->set_alt)
1165 break;
1166 value = f->set_alt(f, w_index, w_value);
1167 if (value == USB_GADGET_DELAYED_STATUS) {
1168 DBG(cdev,
1169 "%s: interface %d (%s) requested delayed status\n",
1170 __func__, intf, f->name);
1171 cdev->delayed_status++;
1172 DBG(cdev, "delayed_status count %d\n",
1173 cdev->delayed_status);
1175 break;
1176 case USB_REQ_GET_INTERFACE:
1177 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1178 goto unknown;
1179 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1180 break;
1181 f = cdev->config->interface[intf];
1182 if (!f)
1183 break;
1184 /* lots of interfaces only need altsetting zero... */
1185 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1186 if (value < 0)
1187 break;
1188 *((u8 *)req->buf) = value;
1189 value = min(w_length, (u16) 1);
1190 break;
1193 * USB 3.0 additions:
1194 * Function driver should handle get_status request. If such cb
1195 * wasn't supplied we respond with default value = 0
1196 * Note: function driver should supply such cb only for the first
1197 * interface of the function
1199 case USB_REQ_GET_STATUS:
1200 if (!gadget_is_superspeed(gadget))
1201 goto unknown;
1202 if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
1203 goto unknown;
1204 value = 2; /* This is the length of the get_status reply */
1205 put_unaligned_le16(0, req->buf);
1206 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1207 break;
1208 f = cdev->config->interface[intf];
1209 if (!f)
1210 break;
1211 status = f->get_status ? f->get_status(f) : 0;
1212 if (status < 0)
1213 break;
1214 put_unaligned_le16(status & 0x0000ffff, req->buf);
1215 break;
1217 * Function drivers should handle SetFeature/ClearFeature
1218 * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
1219 * only for the first interface of the function
1221 case USB_REQ_CLEAR_FEATURE:
1222 case USB_REQ_SET_FEATURE:
1223 if (!gadget_is_superspeed(gadget))
1224 goto unknown;
1225 if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
1226 goto unknown;
1227 switch (w_value) {
1228 case USB_INTRF_FUNC_SUSPEND:
1229 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1230 break;
1231 f = cdev->config->interface[intf];
1232 if (!f)
1233 break;
1234 value = 0;
1235 if (f->func_suspend)
1236 value = f->func_suspend(f, w_index >> 8);
1237 if (value < 0) {
1238 ERROR(cdev,
1239 "func_suspend() returned error %d\n",
1240 value);
1241 value = 0;
1243 break;
1245 break;
1246 default:
1247 unknown:
1248 VDBG(cdev,
1249 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1250 ctrl->bRequestType, ctrl->bRequest,
1251 w_value, w_index, w_length);
1253 /* functions always handle their interfaces and endpoints...
1254 * punt other recipients (other, WUSB, ...) to the current
1255 * configuration code.
1257 * REVISIT it could make sense to let the composite device
1258 * take such requests too, if that's ever needed: to work
1259 * in config 0, etc.
1261 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1262 case USB_RECIP_INTERFACE:
1263 if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
1264 break;
1265 f = cdev->config->interface[intf];
1266 break;
1268 case USB_RECIP_ENDPOINT:
1269 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1270 list_for_each_entry(f, &cdev->config->functions, list) {
1271 if (test_bit(endp, f->endpoints))
1272 break;
1274 if (&f->list == &cdev->config->functions)
1275 f = NULL;
1276 break;
1279 if (f && f->setup)
1280 value = f->setup(f, ctrl);
1281 else {
1282 struct usb_configuration *c;
1284 c = cdev->config;
1285 if (c && c->setup)
1286 value = c->setup(c, ctrl);
1289 goto done;
1292 /* respond with data transfer before status phase? */
1293 if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
1294 req->length = value;
1295 req->zero = value < w_length;
1296 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1297 if (value < 0) {
1298 DBG(cdev, "ep_queue --> %d\n", value);
1299 req->status = 0;
1300 composite_setup_complete(gadget->ep0, req);
1302 } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
1303 WARN(cdev,
1304 "%s: Delayed status not supported for w_length != 0",
1305 __func__);
1308 done:
1309 /* device either stalls (value < 0) or reports success */
1310 return value;
1313 static void composite_disconnect(struct usb_gadget *gadget)
1315 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1316 unsigned long flags;
1318 /* REVISIT: should we have config and device level
1319 * disconnect callbacks?
1321 spin_lock_irqsave(&cdev->lock, flags);
1322 if (cdev->config)
1323 reset_config(cdev);
1324 if (composite->disconnect)
1325 composite->disconnect(cdev);
1326 spin_unlock_irqrestore(&cdev->lock, flags);
1329 /*-------------------------------------------------------------------------*/
1331 static ssize_t composite_show_suspended(struct device *dev,
1332 struct device_attribute *attr,
1333 char *buf)
1335 struct usb_gadget *gadget = dev_to_usb_gadget(dev);
1336 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1338 return sprintf(buf, "%d\n", cdev->suspended);
1341 static DEVICE_ATTR(suspended, 0444, composite_show_suspended, NULL);
1343 static void
1344 composite_unbind(struct usb_gadget *gadget)
1346 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1348 /* composite_disconnect() must already have been called
1349 * by the underlying peripheral controller driver!
1350 * so there's no i/o concurrency that could affect the
1351 * state protected by cdev->lock.
1353 WARN_ON(cdev->config);
1355 while (!list_empty(&cdev->configs)) {
1356 struct usb_configuration *c;
1358 c = list_first_entry(&cdev->configs,
1359 struct usb_configuration, list);
1360 while (!list_empty(&c->functions)) {
1361 struct usb_function *f;
1363 f = list_first_entry(&c->functions,
1364 struct usb_function, list);
1365 list_del(&f->list);
1366 if (f->unbind) {
1367 DBG(cdev, "unbind function '%s'/%p\n",
1368 f->name, f);
1369 f->unbind(c, f);
1370 /* may free memory for "f" */
1373 list_del(&c->list);
1374 if (c->unbind) {
1375 DBG(cdev, "unbind config '%s'/%p\n", c->label, c);
1376 c->unbind(c);
1377 /* may free memory for "c" */
1380 if (composite->unbind)
1381 composite->unbind(cdev);
1383 if (cdev->req) {
1384 kfree(cdev->req->buf);
1385 usb_ep_free_request(gadget->ep0, cdev->req);
1387 device_remove_file(&gadget->dev, &dev_attr_suspended);
1388 kfree(cdev);
1389 set_gadget_data(gadget, NULL);
1390 composite = NULL;
1393 static u8 override_id(struct usb_composite_dev *cdev, u8 *desc)
1395 if (!*desc) {
1396 int ret = usb_string_id(cdev);
1397 if (unlikely(ret < 0))
1398 WARNING(cdev, "failed to override string ID\n");
1399 else
1400 *desc = ret;
1403 return *desc;
1406 static int composite_bind(struct usb_gadget *gadget)
1408 struct usb_composite_dev *cdev;
1409 int status = -ENOMEM;
1411 cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
1412 if (!cdev)
1413 return status;
1415 spin_lock_init(&cdev->lock);
1416 cdev->gadget = gadget;
1417 set_gadget_data(gadget, cdev);
1418 INIT_LIST_HEAD(&cdev->configs);
1420 /* preallocate control response and buffer */
1421 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1422 if (!cdev->req)
1423 goto fail;
1424 cdev->req->buf = kmalloc(USB_BUFSIZ, GFP_KERNEL);
1425 if (!cdev->req->buf)
1426 goto fail;
1427 cdev->req->complete = composite_setup_complete;
1428 gadget->ep0->driver_data = cdev;
1430 cdev->bufsiz = USB_BUFSIZ;
1431 cdev->driver = composite;
1434 * As per USB compliance update, a device that is actively drawing
1435 * more than 100mA from USB must report itself as bus-powered in
1436 * the GetStatus(DEVICE) call.
1438 if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
1439 usb_gadget_set_selfpowered(gadget);
1441 /* interface and string IDs start at zero via kzalloc.
1442 * we force endpoints to start unassigned; few controller
1443 * drivers will zero ep->driver_data.
1445 usb_ep_autoconfig_reset(cdev->gadget);
1447 /* composite gadget needs to assign strings for whole device (like
1448 * serial number), register function drivers, potentially update
1449 * power state and consumption, etc
1451 status = composite_gadget_bind(cdev);
1452 if (status < 0)
1453 goto fail;
1455 cdev->desc = *composite->dev;
1457 /* standardized runtime overrides for device ID data */
1458 if (idVendor)
1459 cdev->desc.idVendor = cpu_to_le16(idVendor);
1460 if (idProduct)
1461 cdev->desc.idProduct = cpu_to_le16(idProduct);
1462 if (bcdDevice)
1463 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1465 /* string overrides */
1466 if (iManufacturer || !cdev->desc.iManufacturer) {
1467 if (!iManufacturer && !composite->iManufacturer &&
1468 !*composite_manufacturer)
1469 snprintf(composite_manufacturer,
1470 sizeof composite_manufacturer,
1471 "%s %s with %s",
1472 init_utsname()->sysname,
1473 init_utsname()->release,
1474 gadget->name);
1476 cdev->manufacturer_override =
1477 override_id(cdev, &cdev->desc.iManufacturer);
1480 if (iProduct || (!cdev->desc.iProduct && composite->iProduct))
1481 cdev->product_override =
1482 override_id(cdev, &cdev->desc.iProduct);
1484 if (iSerialNumber)
1485 cdev->serial_override =
1486 override_id(cdev, &cdev->desc.iSerialNumber);
1488 /* has userspace failed to provide a serial number? */
1489 if (composite->needs_serial && !cdev->desc.iSerialNumber)
1490 WARNING(cdev, "userspace failed to provide iSerialNumber\n");
1492 /* finish up */
1493 status = device_create_file(&gadget->dev, &dev_attr_suspended);
1494 if (status)
1495 goto fail;
1497 INFO(cdev, "%s ready\n", composite->name);
1498 return 0;
1500 fail:
1501 composite_unbind(gadget);
1502 return status;
1505 /*-------------------------------------------------------------------------*/
1507 static void
1508 composite_suspend(struct usb_gadget *gadget)
1510 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1511 struct usb_function *f;
1513 /* REVISIT: should we have config level
1514 * suspend/resume callbacks?
1516 DBG(cdev, "suspend\n");
1517 if (cdev->config) {
1518 list_for_each_entry(f, &cdev->config->functions, list) {
1519 if (f->suspend)
1520 f->suspend(f);
1523 if (composite->suspend)
1524 composite->suspend(cdev);
1526 cdev->suspended = 1;
1528 usb_gadget_vbus_draw(gadget, 2);
1531 static void
1532 composite_resume(struct usb_gadget *gadget)
1534 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1535 struct usb_function *f;
1536 u8 maxpower;
1538 /* REVISIT: should we have config level
1539 * suspend/resume callbacks?
1541 DBG(cdev, "resume\n");
1542 if (composite->resume)
1543 composite->resume(cdev);
1544 if (cdev->config) {
1545 list_for_each_entry(f, &cdev->config->functions, list) {
1546 if (f->resume)
1547 f->resume(f);
1550 maxpower = cdev->config->bMaxPower;
1552 usb_gadget_vbus_draw(gadget, maxpower ?
1553 (2 * maxpower) : CONFIG_USB_GADGET_VBUS_DRAW);
1556 cdev->suspended = 0;
1559 /*-------------------------------------------------------------------------*/
1561 static struct usb_gadget_driver composite_driver = {
1562 #ifdef CONFIG_USB_GADGET_SUPERSPEED
1563 .speed = USB_SPEED_SUPER,
1564 #else
1565 .speed = USB_SPEED_HIGH,
1566 #endif
1568 .unbind = composite_unbind,
1570 .setup = composite_setup,
1571 .disconnect = composite_disconnect,
1573 .suspend = composite_suspend,
1574 .resume = composite_resume,
1576 .driver = {
1577 .owner = THIS_MODULE,
1582 * usb_composite_probe() - register a composite driver
1583 * @driver: the driver to register
1584 * @bind: the callback used to allocate resources that are shared across the
1585 * whole device, such as string IDs, and add its configurations using
1586 * @usb_add_config(). This may fail by returning a negative errno
1587 * value; it should return zero on successful initialization.
1588 * Context: single threaded during gadget setup
1590 * This function is used to register drivers using the composite driver
1591 * framework. The return value is zero, or a negative errno value.
1592 * Those values normally come from the driver's @bind method, which does
1593 * all the work of setting up the driver to match the hardware.
1595 * On successful return, the gadget is ready to respond to requests from
1596 * the host, unless one of its components invokes usb_gadget_disconnect()
1597 * while it was binding. That would usually be done in order to wait for
1598 * some userspace participation.
1600 int usb_composite_probe(struct usb_composite_driver *driver,
1601 int (*bind)(struct usb_composite_dev *cdev))
1603 if (!driver || !driver->dev || !bind || composite)
1604 return -EINVAL;
1606 if (!driver->name)
1607 driver->name = "composite";
1608 if (!driver->iProduct)
1609 driver->iProduct = driver->name;
1610 composite_driver.function = (char *) driver->name;
1611 composite_driver.driver.name = driver->name;
1612 composite_driver.speed = min((u8)composite_driver.speed,
1613 (u8)driver->max_speed);
1614 composite = driver;
1615 composite_gadget_bind = bind;
1617 return usb_gadget_probe_driver(&composite_driver, composite_bind);
1621 * usb_composite_unregister() - unregister a composite driver
1622 * @driver: the driver to unregister
1624 * This function is used to unregister drivers using the composite
1625 * driver framework.
1627 void usb_composite_unregister(struct usb_composite_driver *driver)
1629 if (composite != driver)
1630 return;
1631 usb_gadget_unregister_driver(&composite_driver);
1635 * usb_composite_setup_continue() - Continue with the control transfer
1636 * @cdev: the composite device who's control transfer was kept waiting
1638 * This function must be called by the USB function driver to continue
1639 * with the control transfer's data/status stage in case it had requested to
1640 * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
1641 * can request the composite framework to delay the setup request's data/status
1642 * stages by returning USB_GADGET_DELAYED_STATUS.
1644 void usb_composite_setup_continue(struct usb_composite_dev *cdev)
1646 int value;
1647 struct usb_request *req = cdev->req;
1648 unsigned long flags;
1650 DBG(cdev, "%s\n", __func__);
1651 spin_lock_irqsave(&cdev->lock, flags);
1653 if (cdev->delayed_status == 0) {
1654 WARN(cdev, "%s: Unexpected call\n", __func__);
1656 } else if (--cdev->delayed_status == 0) {
1657 DBG(cdev, "%s: Completing delayed status\n", __func__);
1658 req->length = 0;
1659 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
1660 if (value < 0) {
1661 DBG(cdev, "ep_queue --> %d\n", value);
1662 req->status = 0;
1663 composite_setup_complete(cdev->gadget->ep0, req);
1667 spin_unlock_irqrestore(&cdev->lock, flags);