xref: /rk3399_rockchip-uboot/drivers/usb/gadget/composite.c (revision 088d8eb9be6001e4c1be13ee95c2beb044456c72)
17010f5b9SLukasz Majewski /*
27010f5b9SLukasz Majewski  * composite.c - infrastructure for Composite USB Gadgets
37010f5b9SLukasz Majewski  *
47010f5b9SLukasz Majewski  * Copyright (C) 2006-2008 David Brownell
5a187559eSBin Meng  * U-Boot porting: Lukasz Majewski <l.majewski@samsung.com>
67010f5b9SLukasz Majewski  *
71a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
87010f5b9SLukasz Majewski  */
97010f5b9SLukasz Majewski #undef DEBUG
107010f5b9SLukasz Majewski 
117010f5b9SLukasz Majewski #include <linux/bitops.h>
127010f5b9SLukasz Majewski #include <linux/usb/composite.h>
137010f5b9SLukasz Majewski 
147010f5b9SLukasz Majewski #define USB_BUFSIZ	4096
157010f5b9SLukasz Majewski 
167010f5b9SLukasz Majewski static struct usb_composite_driver *composite;
177010f5b9SLukasz Majewski 
187010f5b9SLukasz Majewski /**
197010f5b9SLukasz Majewski  * usb_add_function() - add a function to a configuration
207010f5b9SLukasz Majewski  * @config: the configuration
217010f5b9SLukasz Majewski  * @function: the function being added
227010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
237010f5b9SLukasz Majewski  *
247010f5b9SLukasz Majewski  * After initialization, each configuration must have one or more
257010f5b9SLukasz Majewski  * functions added to it.  Adding a function involves calling its @bind()
267010f5b9SLukasz Majewski  * method to allocate resources such as interface and string identifiers
277010f5b9SLukasz Majewski  * and endpoints.
287010f5b9SLukasz Majewski  *
297010f5b9SLukasz Majewski  * This function returns the value of the function's bind(), which is
307010f5b9SLukasz Majewski  * zero for success else a negative errno value.
317010f5b9SLukasz Majewski  */
327010f5b9SLukasz Majewski int usb_add_function(struct usb_configuration *config,
337010f5b9SLukasz Majewski 		struct usb_function *function)
347010f5b9SLukasz Majewski {
357010f5b9SLukasz Majewski 	int	value = -EINVAL;
367010f5b9SLukasz Majewski 
377010f5b9SLukasz Majewski 	debug("adding '%s'/%p to config '%s'/%p\n",
387010f5b9SLukasz Majewski 			function->name, function,
397010f5b9SLukasz Majewski 			config->label, config);
407010f5b9SLukasz Majewski 
417010f5b9SLukasz Majewski 	if (!function->set_alt || !function->disable)
427010f5b9SLukasz Majewski 		goto done;
437010f5b9SLukasz Majewski 
447010f5b9SLukasz Majewski 	function->config = config;
457010f5b9SLukasz Majewski 	list_add_tail(&function->list, &config->functions);
467010f5b9SLukasz Majewski 
477010f5b9SLukasz Majewski 	if (function->bind) {
487010f5b9SLukasz Majewski 		value = function->bind(config, function);
497010f5b9SLukasz Majewski 		if (value < 0) {
507010f5b9SLukasz Majewski 			list_del(&function->list);
517010f5b9SLukasz Majewski 			function->config = NULL;
527010f5b9SLukasz Majewski 		}
537010f5b9SLukasz Majewski 	} else
547010f5b9SLukasz Majewski 		value = 0;
557010f5b9SLukasz Majewski 
567010f5b9SLukasz Majewski 	if (!config->fullspeed && function->descriptors)
577010f5b9SLukasz Majewski 		config->fullspeed = 1;
587010f5b9SLukasz Majewski 	if (!config->highspeed && function->hs_descriptors)
597010f5b9SLukasz Majewski 		config->highspeed = 1;
6026dd3474SWilliam Wu 	if (!config->superspeed && function->ss_descriptors)
6126dd3474SWilliam Wu 		config->superspeed = 1;
627010f5b9SLukasz Majewski 
637010f5b9SLukasz Majewski done:
647010f5b9SLukasz Majewski 	if (value)
657010f5b9SLukasz Majewski 		debug("adding '%s'/%p --> %d\n",
667010f5b9SLukasz Majewski 				function->name, function, value);
677010f5b9SLukasz Majewski 	return value;
687010f5b9SLukasz Majewski }
697010f5b9SLukasz Majewski 
707010f5b9SLukasz Majewski /**
717010f5b9SLukasz Majewski  * usb_function_deactivate - prevent function and gadget enumeration
727010f5b9SLukasz Majewski  * @function: the function that isn't yet ready to respond
737010f5b9SLukasz Majewski  *
747010f5b9SLukasz Majewski  * Blocks response of the gadget driver to host enumeration by
757010f5b9SLukasz Majewski  * preventing the data line pullup from being activated.  This is
767010f5b9SLukasz Majewski  * normally called during @bind() processing to change from the
777010f5b9SLukasz Majewski  * initial "ready to respond" state, or when a required resource
787010f5b9SLukasz Majewski  * becomes available.
797010f5b9SLukasz Majewski  *
807010f5b9SLukasz Majewski  * For example, drivers that serve as a passthrough to a userspace
817010f5b9SLukasz Majewski  * daemon can block enumeration unless that daemon (such as an OBEX,
827010f5b9SLukasz Majewski  * MTP, or print server) is ready to handle host requests.
837010f5b9SLukasz Majewski  *
847010f5b9SLukasz Majewski  * Not all systems support software control of their USB peripheral
857010f5b9SLukasz Majewski  * data pullups.
867010f5b9SLukasz Majewski  *
877010f5b9SLukasz Majewski  * Returns zero on success, else negative errno.
887010f5b9SLukasz Majewski  */
897010f5b9SLukasz Majewski int usb_function_deactivate(struct usb_function *function)
907010f5b9SLukasz Majewski {
917010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = function->config->cdev;
927010f5b9SLukasz Majewski 	int				status = 0;
937010f5b9SLukasz Majewski 
947010f5b9SLukasz Majewski 	if (cdev->deactivations == 0)
957010f5b9SLukasz Majewski 		status = usb_gadget_disconnect(cdev->gadget);
967010f5b9SLukasz Majewski 	if (status == 0)
977010f5b9SLukasz Majewski 		cdev->deactivations++;
987010f5b9SLukasz Majewski 
997010f5b9SLukasz Majewski 	return status;
1007010f5b9SLukasz Majewski }
1017010f5b9SLukasz Majewski 
1027010f5b9SLukasz Majewski /**
1037010f5b9SLukasz Majewski  * usb_function_activate - allow function and gadget enumeration
1047010f5b9SLukasz Majewski  * @function: function on which usb_function_activate() was called
1057010f5b9SLukasz Majewski  *
1067010f5b9SLukasz Majewski  * Reverses effect of usb_function_deactivate().  If no more functions
1077010f5b9SLukasz Majewski  * are delaying their activation, the gadget driver will respond to
1087010f5b9SLukasz Majewski  * host enumeration procedures.
1097010f5b9SLukasz Majewski  *
1107010f5b9SLukasz Majewski  * Returns zero on success, else negative errno.
1117010f5b9SLukasz Majewski  */
1127010f5b9SLukasz Majewski int usb_function_activate(struct usb_function *function)
1137010f5b9SLukasz Majewski {
1147010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = function->config->cdev;
1157010f5b9SLukasz Majewski 	int				status = 0;
1167010f5b9SLukasz Majewski 
1177010f5b9SLukasz Majewski 	if (cdev->deactivations == 0)
1187010f5b9SLukasz Majewski 		status = -EINVAL;
1197010f5b9SLukasz Majewski 	else {
1207010f5b9SLukasz Majewski 		cdev->deactivations--;
1217010f5b9SLukasz Majewski 		if (cdev->deactivations == 0)
1227010f5b9SLukasz Majewski 			status = usb_gadget_connect(cdev->gadget);
1237010f5b9SLukasz Majewski 	}
1247010f5b9SLukasz Majewski 
1257010f5b9SLukasz Majewski 	return status;
1267010f5b9SLukasz Majewski }
1277010f5b9SLukasz Majewski 
1287010f5b9SLukasz Majewski /**
1297010f5b9SLukasz Majewski  * usb_interface_id() - allocate an unused interface ID
1307010f5b9SLukasz Majewski  * @config: configuration associated with the interface
1317010f5b9SLukasz Majewski  * @function: function handling the interface
1327010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
1337010f5b9SLukasz Majewski  *
1347010f5b9SLukasz Majewski  * usb_interface_id() is called from usb_function.bind() callbacks to
1357010f5b9SLukasz Majewski  * allocate new interface IDs.  The function driver will then store that
1367010f5b9SLukasz Majewski  * ID in interface, association, CDC union, and other descriptors.  It
1377010f5b9SLukasz Majewski  * will also handle any control requests targetted at that interface,
1387010f5b9SLukasz Majewski  * particularly changing its altsetting via set_alt().  There may
1397010f5b9SLukasz Majewski  * also be class-specific or vendor-specific requests to handle.
1407010f5b9SLukasz Majewski  *
1417010f5b9SLukasz Majewski  * All interface identifier should be allocated using this routine, to
1427010f5b9SLukasz Majewski  * ensure that for example different functions don't wrongly assign
1437010f5b9SLukasz Majewski  * different meanings to the same identifier.  Note that since interface
1447010f5b9SLukasz Majewski  * identifers are configuration-specific, functions used in more than
1457010f5b9SLukasz Majewski  * one configuration (or more than once in a given configuration) need
1467010f5b9SLukasz Majewski  * multiple versions of the relevant descriptors.
1477010f5b9SLukasz Majewski  *
1487010f5b9SLukasz Majewski  * Returns the interface ID which was allocated; or -ENODEV if no
1497010f5b9SLukasz Majewski  * more interface IDs can be allocated.
1507010f5b9SLukasz Majewski  */
1517010f5b9SLukasz Majewski int usb_interface_id(struct usb_configuration *config,
1527010f5b9SLukasz Majewski 		struct usb_function *function)
1537010f5b9SLukasz Majewski {
1547010f5b9SLukasz Majewski 	unsigned char id = config->next_interface_id;
1557010f5b9SLukasz Majewski 
1567010f5b9SLukasz Majewski 	if (id < MAX_CONFIG_INTERFACES) {
1577010f5b9SLukasz Majewski 		config->interface[id] = function;
1587010f5b9SLukasz Majewski 		config->next_interface_id = id + 1;
1597010f5b9SLukasz Majewski 		return id;
1607010f5b9SLukasz Majewski 	}
1617010f5b9SLukasz Majewski 	return -ENODEV;
1627010f5b9SLukasz Majewski }
1637010f5b9SLukasz Majewski 
1647010f5b9SLukasz Majewski static int config_buf(struct usb_configuration *config,
1657010f5b9SLukasz Majewski 		enum usb_device_speed speed, void *buf, u8 type)
1667010f5b9SLukasz Majewski {
1677010f5b9SLukasz Majewski 	int				len = USB_BUFSIZ - USB_DT_CONFIG_SIZE;
1687010f5b9SLukasz Majewski 	void				*next = buf + USB_DT_CONFIG_SIZE;
1697010f5b9SLukasz Majewski 	struct usb_descriptor_header    **descriptors;
1707010f5b9SLukasz Majewski 	struct usb_config_descriptor	*c = buf;
1717010f5b9SLukasz Majewski 	int				status;
1727010f5b9SLukasz Majewski 	struct usb_function		*f;
1737010f5b9SLukasz Majewski 
1747010f5b9SLukasz Majewski 	/* write the config descriptor */
1757010f5b9SLukasz Majewski 	c = buf;
1767010f5b9SLukasz Majewski 	c->bLength = USB_DT_CONFIG_SIZE;
1777010f5b9SLukasz Majewski 	c->bDescriptorType = type;
1787010f5b9SLukasz Majewski 
1797010f5b9SLukasz Majewski 	c->bNumInterfaces = config->next_interface_id;
1807010f5b9SLukasz Majewski 	c->bConfigurationValue = config->bConfigurationValue;
1817010f5b9SLukasz Majewski 	c->iConfiguration = config->iConfiguration;
1827010f5b9SLukasz Majewski 	c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
1837010f5b9SLukasz Majewski 	c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
1847010f5b9SLukasz Majewski 
1857010f5b9SLukasz Majewski 	/* There may be e.g. OTG descriptors */
1867010f5b9SLukasz Majewski 	if (config->descriptors) {
1877010f5b9SLukasz Majewski 		status = usb_descriptor_fillbuf(next, len,
1887010f5b9SLukasz Majewski 				config->descriptors);
1897010f5b9SLukasz Majewski 		if (status < 0)
1907010f5b9SLukasz Majewski 			return status;
1917010f5b9SLukasz Majewski 		len -= status;
1927010f5b9SLukasz Majewski 		next += status;
1937010f5b9SLukasz Majewski 	}
1947010f5b9SLukasz Majewski 
1957010f5b9SLukasz Majewski 	/* add each function's descriptors */
1967010f5b9SLukasz Majewski 	list_for_each_entry(f, &config->functions, list) {
19726dd3474SWilliam Wu 		switch (speed) {
19826dd3474SWilliam Wu 		case USB_SPEED_SUPER:
19926dd3474SWilliam Wu 			descriptors = f->ss_descriptors;
20026dd3474SWilliam Wu 			break;
20126dd3474SWilliam Wu 		case USB_SPEED_HIGH:
2027010f5b9SLukasz Majewski 			descriptors = f->hs_descriptors;
20326dd3474SWilliam Wu 			break;
20426dd3474SWilliam Wu 		default:
2057010f5b9SLukasz Majewski 			descriptors = f->descriptors;
20626dd3474SWilliam Wu 		}
20726dd3474SWilliam Wu 
2087010f5b9SLukasz Majewski 		if (!descriptors)
2097010f5b9SLukasz Majewski 			continue;
2107010f5b9SLukasz Majewski 		status = usb_descriptor_fillbuf(next, len,
2117010f5b9SLukasz Majewski 			(const struct usb_descriptor_header **) descriptors);
2127010f5b9SLukasz Majewski 		if (status < 0)
2137010f5b9SLukasz Majewski 			return status;
2147010f5b9SLukasz Majewski 		len -= status;
2157010f5b9SLukasz Majewski 		next += status;
2167010f5b9SLukasz Majewski 	}
2177010f5b9SLukasz Majewski 
2187010f5b9SLukasz Majewski 	len = next - buf;
2197010f5b9SLukasz Majewski 	c->wTotalLength = cpu_to_le16(len);
2207010f5b9SLukasz Majewski 	return len;
2217010f5b9SLukasz Majewski }
2227010f5b9SLukasz Majewski 
2237010f5b9SLukasz Majewski static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
2247010f5b9SLukasz Majewski {
2257010f5b9SLukasz Majewski 	enum usb_device_speed		speed = USB_SPEED_UNKNOWN;
2267010f5b9SLukasz Majewski 	struct usb_gadget		*gadget = cdev->gadget;
2277010f5b9SLukasz Majewski 	u8				type = w_value >> 8;
2287010f5b9SLukasz Majewski 	int                             hs = 0;
2297010f5b9SLukasz Majewski 	struct usb_configuration	*c;
2307010f5b9SLukasz Majewski 
23126dd3474SWilliam Wu 	if (gadget->speed == USB_SPEED_SUPER)
23226dd3474SWilliam Wu 		speed = gadget->speed;
23326dd3474SWilliam Wu 	else if (gadget_is_dualspeed(gadget)) {
2347010f5b9SLukasz Majewski 		if (gadget->speed == USB_SPEED_HIGH)
2357010f5b9SLukasz Majewski 			hs = 1;
2367010f5b9SLukasz Majewski 		if (type == USB_DT_OTHER_SPEED_CONFIG)
2377010f5b9SLukasz Majewski 			hs = !hs;
2387010f5b9SLukasz Majewski 		if (hs)
2397010f5b9SLukasz Majewski 			speed = USB_SPEED_HIGH;
2407010f5b9SLukasz Majewski 	}
2417010f5b9SLukasz Majewski 
2427010f5b9SLukasz Majewski 	w_value &= 0xff;
2437010f5b9SLukasz Majewski 	list_for_each_entry(c, &cdev->configs, list) {
24426dd3474SWilliam Wu 		switch (speed) {
24526dd3474SWilliam Wu 		case USB_SPEED_SUPER:
24626dd3474SWilliam Wu 			if (!c->superspeed)
24726dd3474SWilliam Wu 				continue;
24826dd3474SWilliam Wu 			break;
24926dd3474SWilliam Wu 		case USB_SPEED_HIGH:
2507010f5b9SLukasz Majewski 			if (!c->highspeed)
2517010f5b9SLukasz Majewski 				continue;
25226dd3474SWilliam Wu 			break;
25326dd3474SWilliam Wu 		default:
2547010f5b9SLukasz Majewski 			if (!c->fullspeed)
2557010f5b9SLukasz Majewski 				continue;
2567010f5b9SLukasz Majewski 		}
25726dd3474SWilliam Wu 
2587010f5b9SLukasz Majewski 		if (w_value == 0)
2597010f5b9SLukasz Majewski 			return config_buf(c, speed, cdev->req->buf, type);
2607010f5b9SLukasz Majewski 		w_value--;
2617010f5b9SLukasz Majewski 	}
2627010f5b9SLukasz Majewski 	return -EINVAL;
2637010f5b9SLukasz Majewski }
2647010f5b9SLukasz Majewski 
2657010f5b9SLukasz Majewski static int count_configs(struct usb_composite_dev *cdev, unsigned type)
2667010f5b9SLukasz Majewski {
2677010f5b9SLukasz Majewski 	struct usb_gadget		*gadget = cdev->gadget;
2687010f5b9SLukasz Majewski 	unsigned			count = 0;
2697010f5b9SLukasz Majewski 	int				hs = 0;
2707010f5b9SLukasz Majewski 	struct usb_configuration	*c;
2717010f5b9SLukasz Majewski 
2727010f5b9SLukasz Majewski 	if (gadget_is_dualspeed(gadget)) {
2737010f5b9SLukasz Majewski 		if (gadget->speed == USB_SPEED_HIGH)
2747010f5b9SLukasz Majewski 			hs = 1;
2757010f5b9SLukasz Majewski 		if (type == USB_DT_DEVICE_QUALIFIER)
2767010f5b9SLukasz Majewski 			hs = !hs;
2777010f5b9SLukasz Majewski 	}
2787010f5b9SLukasz Majewski 	list_for_each_entry(c, &cdev->configs, list) {
2797010f5b9SLukasz Majewski 		/* ignore configs that won't work at this speed */
2807010f5b9SLukasz Majewski 		if (hs) {
2817010f5b9SLukasz Majewski 			if (!c->highspeed)
2827010f5b9SLukasz Majewski 				continue;
2837010f5b9SLukasz Majewski 		} else {
2847010f5b9SLukasz Majewski 			if (!c->fullspeed)
2857010f5b9SLukasz Majewski 				continue;
2867010f5b9SLukasz Majewski 		}
2877010f5b9SLukasz Majewski 		count++;
2887010f5b9SLukasz Majewski 	}
2897010f5b9SLukasz Majewski 	return count;
2907010f5b9SLukasz Majewski }
2917010f5b9SLukasz Majewski 
2928ddd5824SFrank Wang static int bos_desc(struct usb_composite_dev *cdev)
2938ddd5824SFrank Wang {
294912bc5c1SFrank Wang 	struct usb_dev_cap_header	*cap;
29526dd3474SWilliam Wu 	struct usb_ext_cap_descriptor	*usb_ext;
29626dd3474SWilliam Wu 	struct usb_ss_cap_descriptor	*ss_cap;
2978ddd5824SFrank Wang 	struct usb_bos_descriptor	*bos = cdev->req->buf;
2988ddd5824SFrank Wang 
2998ddd5824SFrank Wang 	bos->bLength = USB_DT_BOS_SIZE;
3008ddd5824SFrank Wang 	bos->bDescriptorType = USB_DT_BOS;
3018ddd5824SFrank Wang 	bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
3028ddd5824SFrank Wang 	bos->bNumDeviceCaps = 0;
3038ddd5824SFrank Wang 
30426dd3474SWilliam Wu 	if (cdev->gadget->speed < USB_SPEED_SUPER) {
30526dd3474SWilliam Wu 		/* For rockusb with bcdUSB (0x0201) */
306912bc5c1SFrank Wang 		cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
307912bc5c1SFrank Wang 		bos->bNumDeviceCaps++;
30826dd3474SWilliam Wu 		bos->wTotalLength = cpu_to_le16(bos->wTotalLength +
30926dd3474SWilliam Wu 						sizeof(*cap));
310912bc5c1SFrank Wang 		cap->bLength = sizeof(*cap);
311912bc5c1SFrank Wang 		cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
312912bc5c1SFrank Wang 		cap->bDevCapabilityType = 0;
31326dd3474SWilliam Wu 	} else {
31426dd3474SWilliam Wu 		/*
31526dd3474SWilliam Wu 		 * A SuperSpeed device shall include the USB2.0
31626dd3474SWilliam Wu 		 * extension descriptor and shall support LPM when
31726dd3474SWilliam Wu 		 * operating in USB2.0 HS mode.
31826dd3474SWilliam Wu 		 */
31926dd3474SWilliam Wu 		usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
32026dd3474SWilliam Wu 		bos->bNumDeviceCaps++;
32126dd3474SWilliam Wu 		bos->wTotalLength = cpu_to_le16(bos->wTotalLength +
32226dd3474SWilliam Wu 						USB_DT_USB_EXT_CAP_SIZE);
32326dd3474SWilliam Wu 		usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
32426dd3474SWilliam Wu 		usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
32526dd3474SWilliam Wu 		usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
32626dd3474SWilliam Wu 		usb_ext->bmAttributes = USB_LPM_SUPPORT;
32726dd3474SWilliam Wu 
32826dd3474SWilliam Wu 		/*
32926dd3474SWilliam Wu 		 * The Superspeed USB Capability descriptor shall be
33026dd3474SWilliam Wu 		 * implemented by all SuperSpeed devices.
33126dd3474SWilliam Wu 		 */
33226dd3474SWilliam Wu 		ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
33326dd3474SWilliam Wu 		bos->bNumDeviceCaps++;
33426dd3474SWilliam Wu 		bos->wTotalLength = cpu_to_le16(bos->wTotalLength +
33526dd3474SWilliam Wu 						USB_DT_USB_SS_CAP_SIZE);
33626dd3474SWilliam Wu 		ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
33726dd3474SWilliam Wu 		ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
33826dd3474SWilliam Wu 		ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
33926dd3474SWilliam Wu 		ss_cap->bmAttributes = 0; /* LTM is not supported yet */
34026dd3474SWilliam Wu 		ss_cap->wSpeedSupported = cpu_to_le16(USB_FULL_SPEED_OPERATION |
34126dd3474SWilliam Wu 				USB_HIGH_SPEED_OPERATION |
34226dd3474SWilliam Wu 				USB_5GBPS_OPERATION);
34326dd3474SWilliam Wu 		ss_cap->bFunctionalitySupport = USB_FULL_SPEED_OPERATION;
34426dd3474SWilliam Wu 		ss_cap->bU1devExitLat = USB_DEFAULT_U1_DEV_EXIT_LAT;
34526dd3474SWilliam Wu 		ss_cap->bU2DevExitLat = cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
34626dd3474SWilliam Wu 	}
347912bc5c1SFrank Wang 
3488ddd5824SFrank Wang 	return le16_to_cpu(bos->wTotalLength);
3498ddd5824SFrank Wang }
3508ddd5824SFrank Wang 
3517010f5b9SLukasz Majewski static void device_qual(struct usb_composite_dev *cdev)
3527010f5b9SLukasz Majewski {
3537010f5b9SLukasz Majewski 	struct usb_qualifier_descriptor	*qual = cdev->req->buf;
3547010f5b9SLukasz Majewski 
3557010f5b9SLukasz Majewski 	qual->bLength = sizeof(*qual);
3567010f5b9SLukasz Majewski 	qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
3577010f5b9SLukasz Majewski 	/* POLICY: same bcdUSB and device type info at both speeds */
3587010f5b9SLukasz Majewski 	qual->bcdUSB = cdev->desc.bcdUSB;
3597010f5b9SLukasz Majewski 	qual->bDeviceClass = cdev->desc.bDeviceClass;
3607010f5b9SLukasz Majewski 	qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
3617010f5b9SLukasz Majewski 	qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
3627010f5b9SLukasz Majewski 	/* ASSUME same EP0 fifo size at both speeds */
36304afd5b5SKishon Vijay Abraham I 	qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
3647010f5b9SLukasz Majewski 	qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
3657010f5b9SLukasz Majewski 	qual->bRESERVED = 0;
3667010f5b9SLukasz Majewski }
3677010f5b9SLukasz Majewski 
3687010f5b9SLukasz Majewski static void reset_config(struct usb_composite_dev *cdev)
3697010f5b9SLukasz Majewski {
3707010f5b9SLukasz Majewski 	struct usb_function		*f;
3717010f5b9SLukasz Majewski 
3727010f5b9SLukasz Majewski 	debug("%s:\n", __func__);
3737010f5b9SLukasz Majewski 
3747010f5b9SLukasz Majewski 	list_for_each_entry(f, &cdev->config->functions, list) {
3757010f5b9SLukasz Majewski 		if (f->disable)
3767010f5b9SLukasz Majewski 			f->disable(f);
3777010f5b9SLukasz Majewski 
3787010f5b9SLukasz Majewski 		bitmap_zero(f->endpoints, 32);
3797010f5b9SLukasz Majewski 	}
3807010f5b9SLukasz Majewski 	cdev->config = NULL;
3817010f5b9SLukasz Majewski }
3827010f5b9SLukasz Majewski 
3837010f5b9SLukasz Majewski static int set_config(struct usb_composite_dev *cdev,
3847010f5b9SLukasz Majewski 		const struct usb_ctrlrequest *ctrl, unsigned number)
3857010f5b9SLukasz Majewski {
3867010f5b9SLukasz Majewski 	struct usb_gadget	*gadget = cdev->gadget;
3877010f5b9SLukasz Majewski 	unsigned		power = gadget_is_otg(gadget) ? 8 : 100;
3887010f5b9SLukasz Majewski 	struct usb_descriptor_header **descriptors;
3897010f5b9SLukasz Majewski 	int			result = -EINVAL;
3907010f5b9SLukasz Majewski 	struct usb_endpoint_descriptor *ep;
3917010f5b9SLukasz Majewski 	struct usb_configuration *c = NULL;
3927010f5b9SLukasz Majewski 	int                     addr;
3937010f5b9SLukasz Majewski 	int			tmp;
3947010f5b9SLukasz Majewski 	struct usb_function	*f;
3957010f5b9SLukasz Majewski 
3967010f5b9SLukasz Majewski 	if (cdev->config)
3977010f5b9SLukasz Majewski 		reset_config(cdev);
3987010f5b9SLukasz Majewski 
3997010f5b9SLukasz Majewski 	if (number) {
4007010f5b9SLukasz Majewski 		list_for_each_entry(c, &cdev->configs, list) {
4017010f5b9SLukasz Majewski 			if (c->bConfigurationValue == number) {
4027010f5b9SLukasz Majewski 				result = 0;
4037010f5b9SLukasz Majewski 				break;
4047010f5b9SLukasz Majewski 			}
4057010f5b9SLukasz Majewski 		}
4067010f5b9SLukasz Majewski 		if (result < 0)
4077010f5b9SLukasz Majewski 			goto done;
4087010f5b9SLukasz Majewski 	} else
4097010f5b9SLukasz Majewski 		result = 0;
4107010f5b9SLukasz Majewski 
4117010f5b9SLukasz Majewski 	debug("%s: %s speed config #%d: %s\n", __func__,
4127010f5b9SLukasz Majewski 	      ({ char *speed;
4137010f5b9SLukasz Majewski 		switch (gadget->speed) {
4147010f5b9SLukasz Majewski 		case USB_SPEED_LOW:
4157010f5b9SLukasz Majewski 			speed = "low";
4167010f5b9SLukasz Majewski 			break;
4177010f5b9SLukasz Majewski 		case USB_SPEED_FULL:
4187010f5b9SLukasz Majewski 			speed = "full";
4197010f5b9SLukasz Majewski 			break;
4207010f5b9SLukasz Majewski 		case USB_SPEED_HIGH:
4217010f5b9SLukasz Majewski 			speed = "high";
4227010f5b9SLukasz Majewski 			break;
42326dd3474SWilliam Wu 		case USB_SPEED_SUPER:
42426dd3474SWilliam Wu 			speed = "super";
42526dd3474SWilliam Wu 			break;
4267010f5b9SLukasz Majewski 		default:
4277010f5b9SLukasz Majewski 			speed = "?";
4287010f5b9SLukasz Majewski 			break;
4297010f5b9SLukasz Majewski 		};
4307010f5b9SLukasz Majewski 		     speed;
4317010f5b9SLukasz Majewski 	     }), number, c ? c->label : "unconfigured");
4327010f5b9SLukasz Majewski 
4337010f5b9SLukasz Majewski 	if (!c)
4347010f5b9SLukasz Majewski 		goto done;
4357010f5b9SLukasz Majewski 
4367010f5b9SLukasz Majewski 	cdev->config = c;
4377010f5b9SLukasz Majewski 
4387010f5b9SLukasz Majewski 	/* Initialize all interfaces by setting them to altsetting zero. */
4397010f5b9SLukasz Majewski 	for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
4407010f5b9SLukasz Majewski 		f = c->interface[tmp];
4417010f5b9SLukasz Majewski 		if (!f)
4427010f5b9SLukasz Majewski 			break;
4437010f5b9SLukasz Majewski 
4447010f5b9SLukasz Majewski 		/*
4457010f5b9SLukasz Majewski 		 * Record which endpoints are used by the function. This is used
4467010f5b9SLukasz Majewski 		 * to dispatch control requests targeted at that endpoint to the
4477010f5b9SLukasz Majewski 		 * function's setup callback instead of the current
4487010f5b9SLukasz Majewski 		 * configuration's setup callback.
4497010f5b9SLukasz Majewski 		 */
45026dd3474SWilliam Wu 		switch (gadget->speed) {
45126dd3474SWilliam Wu 		case USB_SPEED_SUPER:
45226dd3474SWilliam Wu 			descriptors = f->ss_descriptors;
45326dd3474SWilliam Wu 			break;
45426dd3474SWilliam Wu 		case USB_SPEED_HIGH:
4557010f5b9SLukasz Majewski 			descriptors = f->hs_descriptors;
45626dd3474SWilliam Wu 			break;
45726dd3474SWilliam Wu 		default:
4587010f5b9SLukasz Majewski 			descriptors = f->descriptors;
45926dd3474SWilliam Wu 		}
4607010f5b9SLukasz Majewski 
4617010f5b9SLukasz Majewski 		for (; *descriptors; ++descriptors) {
4627010f5b9SLukasz Majewski 			if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
4637010f5b9SLukasz Majewski 				continue;
4647010f5b9SLukasz Majewski 
4657010f5b9SLukasz Majewski 			ep = (struct usb_endpoint_descriptor *)*descriptors;
4667010f5b9SLukasz Majewski 			addr = ((ep->bEndpointAddress & 0x80) >> 3)
4677010f5b9SLukasz Majewski 			     |	(ep->bEndpointAddress & 0x0f);
4687010f5b9SLukasz Majewski 			__set_bit(addr, f->endpoints);
4697010f5b9SLukasz Majewski 		}
4707010f5b9SLukasz Majewski 
4717010f5b9SLukasz Majewski 		result = f->set_alt(f, tmp, 0);
4727010f5b9SLukasz Majewski 		if (result < 0) {
4737010f5b9SLukasz Majewski 			debug("interface %d (%s/%p) alt 0 --> %d\n",
4747010f5b9SLukasz Majewski 					tmp, f->name, f, result);
4757010f5b9SLukasz Majewski 
4767010f5b9SLukasz Majewski 			reset_config(cdev);
4777010f5b9SLukasz Majewski 			goto done;
4787010f5b9SLukasz Majewski 		}
4797010f5b9SLukasz Majewski 	}
4807010f5b9SLukasz Majewski 
4817010f5b9SLukasz Majewski 	/* when we return, be sure our power usage is valid */
4827010f5b9SLukasz Majewski 	power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
4837010f5b9SLukasz Majewski done:
4847010f5b9SLukasz Majewski 	usb_gadget_vbus_draw(gadget, power);
4857010f5b9SLukasz Majewski 	return result;
4867010f5b9SLukasz Majewski }
4877010f5b9SLukasz Majewski 
4887010f5b9SLukasz Majewski /**
4897010f5b9SLukasz Majewski  * usb_add_config() - add a configuration to a device.
4907010f5b9SLukasz Majewski  * @cdev: wraps the USB gadget
4917010f5b9SLukasz Majewski  * @config: the configuration, with bConfigurationValue assigned
4927010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
4937010f5b9SLukasz Majewski  *
4947010f5b9SLukasz Majewski  * One of the main tasks of a composite driver's bind() routine is to
4957010f5b9SLukasz Majewski  * add each of the configurations it supports, using this routine.
4967010f5b9SLukasz Majewski  *
4977010f5b9SLukasz Majewski  * This function returns the value of the configuration's bind(), which
4987010f5b9SLukasz Majewski  * is zero for success else a negative errno value.  Binding configurations
4997010f5b9SLukasz Majewski  * assigns global resources including string IDs, and per-configuration
5007010f5b9SLukasz Majewski  * resources such as interface IDs and endpoints.
5017010f5b9SLukasz Majewski  */
5027010f5b9SLukasz Majewski int usb_add_config(struct usb_composite_dev *cdev,
5037010f5b9SLukasz Majewski 		struct usb_configuration *config)
5047010f5b9SLukasz Majewski {
5057010f5b9SLukasz Majewski 	int				status = -EINVAL;
5067010f5b9SLukasz Majewski 	struct usb_configuration	*c;
5077010f5b9SLukasz Majewski 	struct usb_function		*f;
5087010f5b9SLukasz Majewski 	unsigned int			i;
5097010f5b9SLukasz Majewski 
5107010f5b9SLukasz Majewski 	debug("%s: adding config #%u '%s'/%p\n", __func__,
5117010f5b9SLukasz Majewski 			config->bConfigurationValue,
5127010f5b9SLukasz Majewski 			config->label, config);
5137010f5b9SLukasz Majewski 
5147010f5b9SLukasz Majewski 	if (!config->bConfigurationValue || !config->bind)
5157010f5b9SLukasz Majewski 		goto done;
5167010f5b9SLukasz Majewski 
5177010f5b9SLukasz Majewski 	/* Prevent duplicate configuration identifiers */
5187010f5b9SLukasz Majewski 	list_for_each_entry(c, &cdev->configs, list) {
5197010f5b9SLukasz Majewski 		if (c->bConfigurationValue == config->bConfigurationValue) {
5207010f5b9SLukasz Majewski 			status = -EBUSY;
5217010f5b9SLukasz Majewski 			goto done;
5227010f5b9SLukasz Majewski 		}
5237010f5b9SLukasz Majewski 	}
5247010f5b9SLukasz Majewski 
5257010f5b9SLukasz Majewski 	config->cdev = cdev;
5267010f5b9SLukasz Majewski 	list_add_tail(&config->list, &cdev->configs);
5277010f5b9SLukasz Majewski 
5287010f5b9SLukasz Majewski 	INIT_LIST_HEAD(&config->functions);
5297010f5b9SLukasz Majewski 	config->next_interface_id = 0;
5307010f5b9SLukasz Majewski 
5317010f5b9SLukasz Majewski 	status = config->bind(config);
5327010f5b9SLukasz Majewski 	if (status < 0) {
5337010f5b9SLukasz Majewski 		list_del(&config->list);
5347010f5b9SLukasz Majewski 		config->cdev = NULL;
5357010f5b9SLukasz Majewski 	} else {
53626dd3474SWilliam Wu 		debug("cfg %d/%p speeds:%s%s%s\n",
5377010f5b9SLukasz Majewski 		      config->bConfigurationValue, config,
53826dd3474SWilliam Wu 		      config->superspeed ? " super" : "",
5397010f5b9SLukasz Majewski 		      config->highspeed ? " high" : "",
54026dd3474SWilliam Wu 		      config->fullspeed ?
54126dd3474SWilliam Wu 		      (gadget_is_dualspeed(cdev->gadget) ?
54226dd3474SWilliam Wu 		      " full" : " full/low") : "");
5437010f5b9SLukasz Majewski 
5447010f5b9SLukasz Majewski 		for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
5457010f5b9SLukasz Majewski 			f = config->interface[i];
5467010f5b9SLukasz Majewski 			if (!f)
5477010f5b9SLukasz Majewski 				continue;
5487010f5b9SLukasz Majewski 			debug("%s: interface %d = %s/%p\n",
5497010f5b9SLukasz Majewski 			      __func__, i, f->name, f);
5507010f5b9SLukasz Majewski 		}
5517010f5b9SLukasz Majewski 	}
5527010f5b9SLukasz Majewski 
5537010f5b9SLukasz Majewski 	usb_ep_autoconfig_reset(cdev->gadget);
5547010f5b9SLukasz Majewski 
5557010f5b9SLukasz Majewski done:
5567010f5b9SLukasz Majewski 	if (status)
5577010f5b9SLukasz Majewski 		debug("added config '%s'/%u --> %d\n", config->label,
5587010f5b9SLukasz Majewski 				config->bConfigurationValue, status);
5597010f5b9SLukasz Majewski 	return status;
5607010f5b9SLukasz Majewski }
5617010f5b9SLukasz Majewski 
5627010f5b9SLukasz Majewski /*
5637010f5b9SLukasz Majewski  * We support strings in multiple languages ... string descriptor zero
5647010f5b9SLukasz Majewski  * says which languages are supported.	The typical case will be that
5657010f5b9SLukasz Majewski  * only one language (probably English) is used, with I18N handled on
5667010f5b9SLukasz Majewski  * the host side.
5677010f5b9SLukasz Majewski  */
5687010f5b9SLukasz Majewski 
5697010f5b9SLukasz Majewski static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
5707010f5b9SLukasz Majewski {
5717010f5b9SLukasz Majewski 	const struct usb_gadget_strings	*s;
5727010f5b9SLukasz Majewski 	u16				language;
5737010f5b9SLukasz Majewski 	__le16				*tmp;
5747010f5b9SLukasz Majewski 
5757010f5b9SLukasz Majewski 	while (*sp) {
5767010f5b9SLukasz Majewski 		s = *sp;
5777010f5b9SLukasz Majewski 		language = cpu_to_le16(s->language);
5787010f5b9SLukasz Majewski 		for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
5797010f5b9SLukasz Majewski 			if (*tmp == language)
5807010f5b9SLukasz Majewski 				goto repeat;
5817010f5b9SLukasz Majewski 		}
5827010f5b9SLukasz Majewski 		*tmp++ = language;
5837010f5b9SLukasz Majewski repeat:
5847010f5b9SLukasz Majewski 		sp++;
5857010f5b9SLukasz Majewski 	}
5867010f5b9SLukasz Majewski }
5877010f5b9SLukasz Majewski 
5887010f5b9SLukasz Majewski static int lookup_string(
5897010f5b9SLukasz Majewski 	struct usb_gadget_strings	**sp,
5907010f5b9SLukasz Majewski 	void				*buf,
5917010f5b9SLukasz Majewski 	u16				language,
5927010f5b9SLukasz Majewski 	int				id
5937010f5b9SLukasz Majewski )
5947010f5b9SLukasz Majewski {
5957010f5b9SLukasz Majewski 	int				value;
5967010f5b9SLukasz Majewski 	struct usb_gadget_strings	*s;
5977010f5b9SLukasz Majewski 
5987010f5b9SLukasz Majewski 	while (*sp) {
5997010f5b9SLukasz Majewski 		s = *sp++;
6007010f5b9SLukasz Majewski 		if (s->language != language)
6017010f5b9SLukasz Majewski 			continue;
6027010f5b9SLukasz Majewski 		value = usb_gadget_get_string(s, id, buf);
6037010f5b9SLukasz Majewski 		if (value > 0)
6047010f5b9SLukasz Majewski 			return value;
6057010f5b9SLukasz Majewski 	}
6067010f5b9SLukasz Majewski 	return -EINVAL;
6077010f5b9SLukasz Majewski }
6087010f5b9SLukasz Majewski 
6097010f5b9SLukasz Majewski static int get_string(struct usb_composite_dev *cdev,
6107010f5b9SLukasz Majewski 		void *buf, u16 language, int id)
6117010f5b9SLukasz Majewski {
6127010f5b9SLukasz Majewski 	struct usb_string_descriptor	*s = buf;
6137010f5b9SLukasz Majewski 	struct usb_gadget_strings	**sp;
6147010f5b9SLukasz Majewski 	int				len;
6157010f5b9SLukasz Majewski 	struct usb_configuration	*c;
6167010f5b9SLukasz Majewski 	struct usb_function		*f;
6177010f5b9SLukasz Majewski 
6187010f5b9SLukasz Majewski 	/*
6197010f5b9SLukasz Majewski 	 * Yes, not only is USB's I18N support probably more than most
6207010f5b9SLukasz Majewski 	 * folk will ever care about ... also, it's all supported here.
6217010f5b9SLukasz Majewski 	 * (Except for UTF8 support for Unicode's "Astral Planes".)
6227010f5b9SLukasz Majewski 	 */
6237010f5b9SLukasz Majewski 
6247010f5b9SLukasz Majewski 	/* 0 == report all available language codes */
6257010f5b9SLukasz Majewski 	if (id == 0) {
6267010f5b9SLukasz Majewski 		memset(s, 0, 256);
6277010f5b9SLukasz Majewski 		s->bDescriptorType = USB_DT_STRING;
6287010f5b9SLukasz Majewski 
6297010f5b9SLukasz Majewski 		sp = composite->strings;
6307010f5b9SLukasz Majewski 		if (sp)
6317010f5b9SLukasz Majewski 			collect_langs(sp, s->wData);
6327010f5b9SLukasz Majewski 
6337010f5b9SLukasz Majewski 		list_for_each_entry(c, &cdev->configs, list) {
6347010f5b9SLukasz Majewski 			sp = c->strings;
6357010f5b9SLukasz Majewski 			if (sp)
6367010f5b9SLukasz Majewski 				collect_langs(sp, s->wData);
6377010f5b9SLukasz Majewski 
6387010f5b9SLukasz Majewski 			list_for_each_entry(f, &c->functions, list) {
6397010f5b9SLukasz Majewski 				sp = f->strings;
6407010f5b9SLukasz Majewski 				if (sp)
6417010f5b9SLukasz Majewski 					collect_langs(sp, s->wData);
6427010f5b9SLukasz Majewski 			}
6437010f5b9SLukasz Majewski 		}
6447010f5b9SLukasz Majewski 
6457010f5b9SLukasz Majewski 		for (len = 0; len <= 126 && s->wData[len]; len++)
6467010f5b9SLukasz Majewski 			continue;
6477010f5b9SLukasz Majewski 		if (!len)
6487010f5b9SLukasz Majewski 			return -EINVAL;
6497010f5b9SLukasz Majewski 
6507010f5b9SLukasz Majewski 		s->bLength = 2 * (len + 1);
6517010f5b9SLukasz Majewski 		return s->bLength;
6527010f5b9SLukasz Majewski 	}
6537010f5b9SLukasz Majewski 
6547010f5b9SLukasz Majewski 	/*
6557010f5b9SLukasz Majewski 	 * Otherwise, look up and return a specified string.  String IDs
6567010f5b9SLukasz Majewski 	 * are device-scoped, so we look up each string table we're told
6577010f5b9SLukasz Majewski 	 * about.  These lookups are infrequent; simpler-is-better here.
6587010f5b9SLukasz Majewski 	 */
6597010f5b9SLukasz Majewski 	if (composite->strings) {
6607010f5b9SLukasz Majewski 		len = lookup_string(composite->strings, buf, language, id);
6617010f5b9SLukasz Majewski 		if (len > 0)
6627010f5b9SLukasz Majewski 			return len;
6637010f5b9SLukasz Majewski 	}
6647010f5b9SLukasz Majewski 	list_for_each_entry(c, &cdev->configs, list) {
6657010f5b9SLukasz Majewski 		if (c->strings) {
6667010f5b9SLukasz Majewski 			len = lookup_string(c->strings, buf, language, id);
6677010f5b9SLukasz Majewski 			if (len > 0)
6687010f5b9SLukasz Majewski 				return len;
6697010f5b9SLukasz Majewski 		}
6707010f5b9SLukasz Majewski 		list_for_each_entry(f, &c->functions, list) {
6717010f5b9SLukasz Majewski 			if (!f->strings)
6727010f5b9SLukasz Majewski 				continue;
6737010f5b9SLukasz Majewski 			len = lookup_string(f->strings, buf, language, id);
6747010f5b9SLukasz Majewski 			if (len > 0)
6757010f5b9SLukasz Majewski 				return len;
6767010f5b9SLukasz Majewski 		}
6777010f5b9SLukasz Majewski 	}
6787010f5b9SLukasz Majewski 	return -EINVAL;
6797010f5b9SLukasz Majewski }
6807010f5b9SLukasz Majewski 
6817010f5b9SLukasz Majewski /**
6827010f5b9SLukasz Majewski  * usb_string_id() - allocate an unused string ID
6837010f5b9SLukasz Majewski  * @cdev: the device whose string descriptor IDs are being allocated
6847010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
6857010f5b9SLukasz Majewski  *
6867010f5b9SLukasz Majewski  * @usb_string_id() is called from bind() callbacks to allocate
6877010f5b9SLukasz Majewski  * string IDs.	Drivers for functions, configurations, or gadgets will
6887010f5b9SLukasz Majewski  * then store that ID in the appropriate descriptors and string table.
6897010f5b9SLukasz Majewski  *
6907010f5b9SLukasz Majewski  * All string identifier should be allocated using this,
6917010f5b9SLukasz Majewski  * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
6927010f5b9SLukasz Majewski  * that for example different functions don't wrongly assign different
6937010f5b9SLukasz Majewski  * meanings to the same identifier.
6947010f5b9SLukasz Majewski  */
6957010f5b9SLukasz Majewski int usb_string_id(struct usb_composite_dev *cdev)
6967010f5b9SLukasz Majewski {
6977010f5b9SLukasz Majewski 	if (cdev->next_string_id < 254) {
6987010f5b9SLukasz Majewski 		/*
6997010f5b9SLukasz Majewski 		 * string id 0 is reserved by USB spec for list of
7007010f5b9SLukasz Majewski 		 * supported languages
7017010f5b9SLukasz Majewski 		 * 255 reserved as well? -- mina86
7027010f5b9SLukasz Majewski 		 */
7037010f5b9SLukasz Majewski 		cdev->next_string_id++;
7047010f5b9SLukasz Majewski 		return cdev->next_string_id;
7057010f5b9SLukasz Majewski 	}
7067010f5b9SLukasz Majewski 	return -ENODEV;
7077010f5b9SLukasz Majewski }
7087010f5b9SLukasz Majewski 
7097010f5b9SLukasz Majewski /**
7107010f5b9SLukasz Majewski  * usb_string_ids() - allocate unused string IDs in batch
7117010f5b9SLukasz Majewski  * @cdev: the device whose string descriptor IDs are being allocated
7127010f5b9SLukasz Majewski  * @str: an array of usb_string objects to assign numbers to
7137010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
7147010f5b9SLukasz Majewski  *
7157010f5b9SLukasz Majewski  * @usb_string_ids() is called from bind() callbacks to allocate
7167010f5b9SLukasz Majewski  * string IDs.	Drivers for functions, configurations, or gadgets will
7177010f5b9SLukasz Majewski  * then copy IDs from the string table to the appropriate descriptors
7187010f5b9SLukasz Majewski  * and string table for other languages.
7197010f5b9SLukasz Majewski  *
7207010f5b9SLukasz Majewski  * All string identifier should be allocated using this,
7217010f5b9SLukasz Majewski  * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
7227010f5b9SLukasz Majewski  * example different functions don't wrongly assign different meanings
7237010f5b9SLukasz Majewski  * to the same identifier.
7247010f5b9SLukasz Majewski  */
7257010f5b9SLukasz Majewski int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
7267010f5b9SLukasz Majewski {
7277010f5b9SLukasz Majewski 	u8 next = cdev->next_string_id;
7287010f5b9SLukasz Majewski 
7297010f5b9SLukasz Majewski 	for (; str->s; ++str) {
7307010f5b9SLukasz Majewski 		if (next >= 254)
7317010f5b9SLukasz Majewski 			return -ENODEV;
7327010f5b9SLukasz Majewski 		str->id = ++next;
7337010f5b9SLukasz Majewski 	}
7347010f5b9SLukasz Majewski 
7357010f5b9SLukasz Majewski 	cdev->next_string_id = next;
7367010f5b9SLukasz Majewski 
7377010f5b9SLukasz Majewski 	return 0;
7387010f5b9SLukasz Majewski }
7397010f5b9SLukasz Majewski 
7407010f5b9SLukasz Majewski /**
7417010f5b9SLukasz Majewski  * usb_string_ids_n() - allocate unused string IDs in batch
7427010f5b9SLukasz Majewski  * @c: the device whose string descriptor IDs are being allocated
7437010f5b9SLukasz Majewski  * @n: number of string IDs to allocate
7447010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
7457010f5b9SLukasz Majewski  *
7467010f5b9SLukasz Majewski  * Returns the first requested ID.  This ID and next @n-1 IDs are now
7477010f5b9SLukasz Majewski  * valid IDs.  At least provided that @n is non-zero because if it
7487010f5b9SLukasz Majewski  * is, returns last requested ID which is now very useful information.
7497010f5b9SLukasz Majewski  *
7507010f5b9SLukasz Majewski  * @usb_string_ids_n() is called from bind() callbacks to allocate
7517010f5b9SLukasz Majewski  * string IDs.	Drivers for functions, configurations, or gadgets will
7527010f5b9SLukasz Majewski  * then store that ID in the appropriate descriptors and string table.
7537010f5b9SLukasz Majewski  *
7547010f5b9SLukasz Majewski  * All string identifier should be allocated using this,
7557010f5b9SLukasz Majewski  * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
7567010f5b9SLukasz Majewski  * example different functions don't wrongly assign different meanings
7577010f5b9SLukasz Majewski  * to the same identifier.
7587010f5b9SLukasz Majewski  */
7597010f5b9SLukasz Majewski int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
7607010f5b9SLukasz Majewski {
7617010f5b9SLukasz Majewski 	u8 next = c->next_string_id;
7627010f5b9SLukasz Majewski 
7637010f5b9SLukasz Majewski 	if (n > 254 || next + n > 254)
7647010f5b9SLukasz Majewski 		return -ENODEV;
7657010f5b9SLukasz Majewski 
7667010f5b9SLukasz Majewski 	c->next_string_id += n;
7677010f5b9SLukasz Majewski 	return next + 1;
7687010f5b9SLukasz Majewski }
7697010f5b9SLukasz Majewski 
7707010f5b9SLukasz Majewski static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
7717010f5b9SLukasz Majewski {
7727010f5b9SLukasz Majewski 	if (req->status || req->actual != req->length)
7737010f5b9SLukasz Majewski 		debug("%s: setup complete --> %d, %d/%d\n", __func__,
7747010f5b9SLukasz Majewski 				req->status, req->actual, req->length);
7757010f5b9SLukasz Majewski }
7767010f5b9SLukasz Majewski 
7777010f5b9SLukasz Majewski /*
7787010f5b9SLukasz Majewski  * The setup() callback implements all the ep0 functionality that's
7797010f5b9SLukasz Majewski  * not handled lower down, in hardware or the hardware driver(like
7807010f5b9SLukasz Majewski  * device and endpoint feature flags, and their status).  It's all
7817010f5b9SLukasz Majewski  * housekeeping for the gadget function we're implementing.  Most of
7827010f5b9SLukasz Majewski  * the work is in config and function specific setup.
7837010f5b9SLukasz Majewski  */
7847010f5b9SLukasz Majewski static int
7857010f5b9SLukasz Majewski composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
7867010f5b9SLukasz Majewski {
7877010f5b9SLukasz Majewski 	u16				w_length = le16_to_cpu(ctrl->wLength);
7887010f5b9SLukasz Majewski 	u16				w_index = le16_to_cpu(ctrl->wIndex);
7897010f5b9SLukasz Majewski 	u16				w_value = le16_to_cpu(ctrl->wValue);
7907010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = get_gadget_data(gadget);
7917010f5b9SLukasz Majewski 	u8				intf = w_index & 0xFF;
7927010f5b9SLukasz Majewski 	int				value = -EOPNOTSUPP;
7937010f5b9SLukasz Majewski 	struct usb_request		*req = cdev->req;
7947010f5b9SLukasz Majewski 	struct usb_function		*f = NULL;
7957010f5b9SLukasz Majewski 	int				standard;
7967010f5b9SLukasz Majewski 	u8				endp;
7977010f5b9SLukasz Majewski 	struct usb_configuration	*c;
7987010f5b9SLukasz Majewski 
7997010f5b9SLukasz Majewski 	/*
8007010f5b9SLukasz Majewski 	 * partial re-init of the response message; the function or the
8017010f5b9SLukasz Majewski 	 * gadget might need to intercept e.g. a control-OUT completion
8027010f5b9SLukasz Majewski 	 * when we delegate to it.
8037010f5b9SLukasz Majewski 	 */
8047010f5b9SLukasz Majewski 	req->zero = 0;
8057010f5b9SLukasz Majewski 	req->complete = composite_setup_complete;
8067010f5b9SLukasz Majewski 	req->length = USB_BUFSIZ;
8077010f5b9SLukasz Majewski 	gadget->ep0->driver_data = cdev;
8087010f5b9SLukasz Majewski 	standard = (ctrl->bRequestType & USB_TYPE_MASK)
8097010f5b9SLukasz Majewski 						== USB_TYPE_STANDARD;
81026dd3474SWilliam Wu 
8117010f5b9SLukasz Majewski 	if (!standard)
8127010f5b9SLukasz Majewski 		goto unknown;
8137010f5b9SLukasz Majewski 
8147010f5b9SLukasz Majewski 	switch (ctrl->bRequest) {
8157010f5b9SLukasz Majewski 
8167010f5b9SLukasz Majewski 	/* we handle all standard USB descriptors */
8177010f5b9SLukasz Majewski 	case USB_REQ_GET_DESCRIPTOR:
8187010f5b9SLukasz Majewski 		if (ctrl->bRequestType != USB_DIR_IN)
8197010f5b9SLukasz Majewski 			goto unknown;
8207010f5b9SLukasz Majewski 		switch (w_value >> 8) {
8217010f5b9SLukasz Majewski 
8227010f5b9SLukasz Majewski 		case USB_DT_DEVICE:
8237010f5b9SLukasz Majewski 			cdev->desc.bNumConfigurations =
8247010f5b9SLukasz Majewski 				count_configs(cdev, USB_DT_DEVICE);
82504afd5b5SKishon Vijay Abraham I 			cdev->desc.bMaxPacketSize0 =
82604afd5b5SKishon Vijay Abraham I 				cdev->gadget->ep0->maxpacket;
82726dd3474SWilliam Wu 			if (gadget_is_superspeed(gadget) &&
82826dd3474SWilliam Wu 			    gadget->speed >= USB_SPEED_SUPER) {
82926dd3474SWilliam Wu 				/*
83026dd3474SWilliam Wu 				 * bcdUSB should be 0x0300 for superspeed,
83126dd3474SWilliam Wu 				 * but we change it to 0x0301 for rockusb.
83226dd3474SWilliam Wu 				 */
83326dd3474SWilliam Wu 				if (!strncmp(cdev->driver->name,
83426dd3474SWilliam Wu 					     "rkusb_ums_dnl", 13))
83526dd3474SWilliam Wu 					cdev->desc.bcdUSB = cpu_to_le16(0x0301);
83626dd3474SWilliam Wu 				else
83726dd3474SWilliam Wu 					cdev->desc.bcdUSB = cpu_to_le16(0x0300);
83826dd3474SWilliam Wu 				cdev->desc.bMaxPacketSize0 = 9;
83926dd3474SWilliam Wu 			}
84026dd3474SWilliam Wu 
8417010f5b9SLukasz Majewski 			value = min(w_length, (u16) sizeof cdev->desc);
8427010f5b9SLukasz Majewski 			memcpy(req->buf, &cdev->desc, value);
8437010f5b9SLukasz Majewski 			break;
8447010f5b9SLukasz Majewski 		case USB_DT_DEVICE_QUALIFIER:
8457010f5b9SLukasz Majewski 			if (!gadget_is_dualspeed(gadget))
8467010f5b9SLukasz Majewski 				break;
8477010f5b9SLukasz Majewski 			device_qual(cdev);
848b4141195SMasahiro Yamada 			value = min_t(int, w_length,
8497010f5b9SLukasz Majewski 				      sizeof(struct usb_qualifier_descriptor));
8507010f5b9SLukasz Majewski 			break;
8517010f5b9SLukasz Majewski 		case USB_DT_OTHER_SPEED_CONFIG:
8527010f5b9SLukasz Majewski 			if (!gadget_is_dualspeed(gadget))
8537010f5b9SLukasz Majewski 				break;
8547010f5b9SLukasz Majewski 
8557010f5b9SLukasz Majewski 		case USB_DT_CONFIG:
8567010f5b9SLukasz Majewski 			value = config_desc(cdev, w_value);
8577010f5b9SLukasz Majewski 			if (value >= 0)
8587010f5b9SLukasz Majewski 				value = min(w_length, (u16) value);
8597010f5b9SLukasz Majewski 			break;
8607010f5b9SLukasz Majewski 		case USB_DT_STRING:
8617010f5b9SLukasz Majewski 			value = get_string(cdev, req->buf,
8627010f5b9SLukasz Majewski 					w_index, w_value & 0xff);
8637010f5b9SLukasz Majewski 			if (value >= 0)
8647010f5b9SLukasz Majewski 				value = min(w_length, (u16) value);
8657010f5b9SLukasz Majewski 			break;
86687ed6b10SStefan Roese 		case USB_DT_BOS:
8678ddd5824SFrank Wang 			/* HACK: only for rockusb command.
8688ddd5824SFrank Wang 			 * Rockchip upgrade tool use bcdUSB (0x0201) field
8698ddd5824SFrank Wang 			 * distinguishing maskrom or loader device at present.
8708ddd5824SFrank Wang 			 * Unfortunately, it conflict with Windows 8 and beyond
8718ddd5824SFrank Wang 			 * which request BOS descriptor in this case that bcdUSB
8728ddd5824SFrank Wang 			 * is set to 0x0201.
8738ddd5824SFrank Wang 			 */
87426dd3474SWilliam Wu 			if (gadget_is_superspeed(gadget) ||
87526dd3474SWilliam Wu 			    !strncmp(cdev->driver->name, "rkusb_ums_dnl", 13)) {
8768ddd5824SFrank Wang 				value = bos_desc(cdev);
8778ddd5824SFrank Wang 				value = min(w_length, (u16) value);
8788ddd5824SFrank Wang 			}
8798ddd5824SFrank Wang 
88087ed6b10SStefan Roese 			/*
88187ed6b10SStefan Roese 			 * The USB compliance test (USB 2.0 Command Verifier)
88287ed6b10SStefan Roese 			 * issues this request. We should not run into the
88387ed6b10SStefan Roese 			 * default path here. But return for now until
88487ed6b10SStefan Roese 			 * the superspeed support is added.
88587ed6b10SStefan Roese 			 */
88687ed6b10SStefan Roese 			break;
8877010f5b9SLukasz Majewski 		default:
8887010f5b9SLukasz Majewski 			goto unknown;
8897010f5b9SLukasz Majewski 		}
8907010f5b9SLukasz Majewski 		break;
8917010f5b9SLukasz Majewski 
8927010f5b9SLukasz Majewski 	/* any number of configs can work */
8937010f5b9SLukasz Majewski 	case USB_REQ_SET_CONFIGURATION:
8947010f5b9SLukasz Majewski 		if (ctrl->bRequestType != 0)
8957010f5b9SLukasz Majewski 			goto unknown;
8967010f5b9SLukasz Majewski 		if (gadget_is_otg(gadget)) {
8977010f5b9SLukasz Majewski 			if (gadget->a_hnp_support)
8987010f5b9SLukasz Majewski 				debug("HNP available\n");
8997010f5b9SLukasz Majewski 			else if (gadget->a_alt_hnp_support)
9007010f5b9SLukasz Majewski 				debug("HNP on another port\n");
9017010f5b9SLukasz Majewski 			else
9027010f5b9SLukasz Majewski 				debug("HNP inactive\n");
9037010f5b9SLukasz Majewski 		}
9047010f5b9SLukasz Majewski 
9057010f5b9SLukasz Majewski 		value = set_config(cdev, ctrl, w_value);
9067010f5b9SLukasz Majewski 		break;
9077010f5b9SLukasz Majewski 	case USB_REQ_GET_CONFIGURATION:
9087010f5b9SLukasz Majewski 		if (ctrl->bRequestType != USB_DIR_IN)
9097010f5b9SLukasz Majewski 			goto unknown;
9107010f5b9SLukasz Majewski 		if (cdev->config)
9117010f5b9SLukasz Majewski 			*(u8 *)req->buf = cdev->config->bConfigurationValue;
9127010f5b9SLukasz Majewski 		else
9137010f5b9SLukasz Majewski 			*(u8 *)req->buf = 0;
9147010f5b9SLukasz Majewski 		value = min(w_length, (u16) 1);
9157010f5b9SLukasz Majewski 		break;
9167010f5b9SLukasz Majewski 
9177010f5b9SLukasz Majewski 	/*
9187010f5b9SLukasz Majewski 	 * function drivers must handle get/set altsetting; if there's
9197010f5b9SLukasz Majewski 	 * no get() method, we know only altsetting zero works.
9207010f5b9SLukasz Majewski 	 */
9217010f5b9SLukasz Majewski 	case USB_REQ_SET_INTERFACE:
9227010f5b9SLukasz Majewski 		if (ctrl->bRequestType != USB_RECIP_INTERFACE)
9237010f5b9SLukasz Majewski 			goto unknown;
9247010f5b9SLukasz Majewski 		if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
9257010f5b9SLukasz Majewski 			break;
9267010f5b9SLukasz Majewski 		f = cdev->config->interface[intf];
9277010f5b9SLukasz Majewski 		if (!f)
9287010f5b9SLukasz Majewski 			break;
9297010f5b9SLukasz Majewski 		if (w_value && !f->set_alt)
9307010f5b9SLukasz Majewski 			break;
9317010f5b9SLukasz Majewski 		value = f->set_alt(f, w_index, w_value);
9327010f5b9SLukasz Majewski 		break;
9337010f5b9SLukasz Majewski 	case USB_REQ_GET_INTERFACE:
9347010f5b9SLukasz Majewski 		if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
9357010f5b9SLukasz Majewski 			goto unknown;
9367010f5b9SLukasz Majewski 		if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
9377010f5b9SLukasz Majewski 			break;
9387010f5b9SLukasz Majewski 		f = cdev->config->interface[intf];
9397010f5b9SLukasz Majewski 		if (!f)
9407010f5b9SLukasz Majewski 			break;
9417010f5b9SLukasz Majewski 		/* lots of interfaces only need altsetting zero... */
9427010f5b9SLukasz Majewski 		value = f->get_alt ? f->get_alt(f, w_index) : 0;
9437010f5b9SLukasz Majewski 		if (value < 0)
9447010f5b9SLukasz Majewski 			break;
9457010f5b9SLukasz Majewski 		*((u8 *)req->buf) = value;
9467010f5b9SLukasz Majewski 		value = min(w_length, (u16) 1);
9477010f5b9SLukasz Majewski 		break;
9487010f5b9SLukasz Majewski 	default:
9497010f5b9SLukasz Majewski unknown:
9507010f5b9SLukasz Majewski 		debug("non-core control req%02x.%02x v%04x i%04x l%d\n",
9517010f5b9SLukasz Majewski 			ctrl->bRequestType, ctrl->bRequest,
9527010f5b9SLukasz Majewski 			w_value, w_index, w_length);
9537010f5b9SLukasz Majewski 
954*088d8eb9SChristophe Kerello 		if (!cdev->config)
955*088d8eb9SChristophe Kerello 			goto done;
956*088d8eb9SChristophe Kerello 
9577010f5b9SLukasz Majewski 		/*
9587010f5b9SLukasz Majewski 		 * functions always handle their interfaces and endpoints...
9597010f5b9SLukasz Majewski 		 * punt other recipients (other, WUSB, ...) to the current
9607010f5b9SLukasz Majewski 		 * configuration code.
9617010f5b9SLukasz Majewski 		 */
9627010f5b9SLukasz Majewski 		switch (ctrl->bRequestType & USB_RECIP_MASK) {
9637010f5b9SLukasz Majewski 		case USB_RECIP_INTERFACE:
9641bca07a1SFrank Wang 			if (!cdev->config)
9651bca07a1SFrank Wang 				break;
9667010f5b9SLukasz Majewski 			f = cdev->config->interface[intf];
9677010f5b9SLukasz Majewski 			break;
9687010f5b9SLukasz Majewski 
9697010f5b9SLukasz Majewski 		case USB_RECIP_ENDPOINT:
9701bca07a1SFrank Wang 			if (!cdev->config)
9711bca07a1SFrank Wang 				break;
9727010f5b9SLukasz Majewski 			endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
9737010f5b9SLukasz Majewski 			list_for_each_entry(f, &cdev->config->functions, list) {
9747010f5b9SLukasz Majewski 				if (test_bit(endp, f->endpoints))
9757010f5b9SLukasz Majewski 					break;
9767010f5b9SLukasz Majewski 			}
9777010f5b9SLukasz Majewski 			if (&f->list == &cdev->config->functions)
9787010f5b9SLukasz Majewski 				f = NULL;
9797010f5b9SLukasz Majewski 			break;
980f7b4162eSLukasz Majewski 		/*
981f7b4162eSLukasz Majewski 		 * dfu-util (version 0.5) sets bmRequestType.Receipent = Device
982f7b4162eSLukasz Majewski 		 * for non-standard request (w_value = 0x21,
983f7b4162eSLukasz Majewski 		 * bRequest = GET_DESCRIPTOR in this case).
984f7b4162eSLukasz Majewski 		 * When only one interface is registered (as it is done now),
985f7b4162eSLukasz Majewski 		 * then this request shall be handled as it was requested for
986f7b4162eSLukasz Majewski 		 * interface.
987f7b4162eSLukasz Majewski 		 *
988f7b4162eSLukasz Majewski 		 * In the below code it is checked if only one interface is
989f7b4162eSLukasz Majewski 		 * present and proper function for it is extracted. Due to that
990f7b4162eSLukasz Majewski 		 * function's setup (f->setup) is called to handle this
991f7b4162eSLukasz Majewski 		 * special non-standard request.
992f7b4162eSLukasz Majewski 		 */
993f7b4162eSLukasz Majewski 		case USB_RECIP_DEVICE:
994e290ced6SFrank Wang 			if (cdev->config) {
995f7b4162eSLukasz Majewski 				debug("cdev->config->next_interface_id: %d intf: %d\n",
996f7b4162eSLukasz Majewski 				      cdev->config->next_interface_id, intf);
997f7b4162eSLukasz Majewski 				if (cdev->config->next_interface_id == 1)
998f7b4162eSLukasz Majewski 					f = cdev->config->interface[intf];
999e290ced6SFrank Wang 			}
1000f7b4162eSLukasz Majewski 			break;
10017010f5b9SLukasz Majewski 		}
10027010f5b9SLukasz Majewski 
10037010f5b9SLukasz Majewski 		if (f && f->setup)
10047010f5b9SLukasz Majewski 			value = f->setup(f, ctrl);
10057010f5b9SLukasz Majewski 		else {
10067010f5b9SLukasz Majewski 			c = cdev->config;
1007*088d8eb9SChristophe Kerello 			if (c->setup)
10087010f5b9SLukasz Majewski 				value = c->setup(c, ctrl);
10097010f5b9SLukasz Majewski 		}
10107010f5b9SLukasz Majewski 
10117010f5b9SLukasz Majewski 		goto done;
10127010f5b9SLukasz Majewski 	}
10137010f5b9SLukasz Majewski 
10147010f5b9SLukasz Majewski 	/* respond with data transfer before status phase? */
10157010f5b9SLukasz Majewski 	if (value >= 0) {
10167010f5b9SLukasz Majewski 		req->length = value;
10177010f5b9SLukasz Majewski 		req->zero = value < w_length;
10187010f5b9SLukasz Majewski 		value = usb_ep_queue(gadget->ep0, req, GFP_KERNEL);
10197010f5b9SLukasz Majewski 		if (value < 0) {
10207010f5b9SLukasz Majewski 			debug("ep_queue --> %d\n", value);
10217010f5b9SLukasz Majewski 			req->status = 0;
10227010f5b9SLukasz Majewski 			composite_setup_complete(gadget->ep0, req);
10237010f5b9SLukasz Majewski 		}
10247010f5b9SLukasz Majewski 	}
10257010f5b9SLukasz Majewski 
10267010f5b9SLukasz Majewski done:
10277010f5b9SLukasz Majewski 	/* device either stalls (value < 0) or reports success */
10287010f5b9SLukasz Majewski 	return value;
10297010f5b9SLukasz Majewski }
10307010f5b9SLukasz Majewski 
10317010f5b9SLukasz Majewski static void composite_disconnect(struct usb_gadget *gadget)
10327010f5b9SLukasz Majewski {
10337010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = get_gadget_data(gadget);
10347010f5b9SLukasz Majewski 
10357010f5b9SLukasz Majewski 	if (cdev->config)
10367010f5b9SLukasz Majewski 		reset_config(cdev);
10377010f5b9SLukasz Majewski 	if (composite->disconnect)
10387010f5b9SLukasz Majewski 		composite->disconnect(cdev);
10397010f5b9SLukasz Majewski }
10407010f5b9SLukasz Majewski 
10417010f5b9SLukasz Majewski static void composite_unbind(struct usb_gadget *gadget)
10427010f5b9SLukasz Majewski {
10437010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = get_gadget_data(gadget);
10447010f5b9SLukasz Majewski 	struct usb_configuration	*c;
10457010f5b9SLukasz Majewski 	struct usb_function		*f;
10467010f5b9SLukasz Majewski 
10477010f5b9SLukasz Majewski 	/*
10487010f5b9SLukasz Majewski 	 * composite_disconnect() must already have been called
10497010f5b9SLukasz Majewski 	 * by the underlying peripheral controller driver!
10507010f5b9SLukasz Majewski 	 * so there's no i/o concurrency that could affect the
10517010f5b9SLukasz Majewski 	 * state protected by cdev->lock.
10527010f5b9SLukasz Majewski 	 */
10537010f5b9SLukasz Majewski 	BUG_ON(cdev->config);
10547010f5b9SLukasz Majewski 
10557010f5b9SLukasz Majewski 	while (!list_empty(&cdev->configs)) {
10567010f5b9SLukasz Majewski 		c = list_first_entry(&cdev->configs,
10577010f5b9SLukasz Majewski 				struct usb_configuration, list);
10587010f5b9SLukasz Majewski 		while (!list_empty(&c->functions)) {
10597010f5b9SLukasz Majewski 			f = list_first_entry(&c->functions,
10607010f5b9SLukasz Majewski 					struct usb_function, list);
10617010f5b9SLukasz Majewski 			list_del(&f->list);
10627010f5b9SLukasz Majewski 			if (f->unbind) {
10637010f5b9SLukasz Majewski 				debug("unbind function '%s'/%p\n",
10647010f5b9SLukasz Majewski 						f->name, f);
10657010f5b9SLukasz Majewski 				f->unbind(c, f);
10667010f5b9SLukasz Majewski 			}
10677010f5b9SLukasz Majewski 		}
10687010f5b9SLukasz Majewski 		list_del(&c->list);
10697010f5b9SLukasz Majewski 		if (c->unbind) {
10707010f5b9SLukasz Majewski 			debug("unbind config '%s'/%p\n", c->label, c);
10717010f5b9SLukasz Majewski 			c->unbind(c);
10727010f5b9SLukasz Majewski 		}
107344bfb43fSStephen Warren 		free(c);
10747010f5b9SLukasz Majewski 	}
10757010f5b9SLukasz Majewski 	if (composite->unbind)
10767010f5b9SLukasz Majewski 		composite->unbind(cdev);
10777010f5b9SLukasz Majewski 
10787010f5b9SLukasz Majewski 	if (cdev->req) {
10797010f5b9SLukasz Majewski 		kfree(cdev->req->buf);
10807010f5b9SLukasz Majewski 		usb_ep_free_request(gadget->ep0, cdev->req);
10817010f5b9SLukasz Majewski 	}
10827010f5b9SLukasz Majewski 	kfree(cdev);
10837010f5b9SLukasz Majewski 	set_gadget_data(gadget, NULL);
10847010f5b9SLukasz Majewski 
10857010f5b9SLukasz Majewski 	composite = NULL;
10867010f5b9SLukasz Majewski }
10877010f5b9SLukasz Majewski 
10887010f5b9SLukasz Majewski static int composite_bind(struct usb_gadget *gadget)
10897010f5b9SLukasz Majewski {
10907010f5b9SLukasz Majewski 	int				status = -ENOMEM;
10917010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev;
10927010f5b9SLukasz Majewski 
10937010f5b9SLukasz Majewski 	cdev = calloc(sizeof *cdev, 1);
10947010f5b9SLukasz Majewski 	if (!cdev)
10957010f5b9SLukasz Majewski 		return status;
10967010f5b9SLukasz Majewski 
10977010f5b9SLukasz Majewski 	cdev->gadget = gadget;
10987010f5b9SLukasz Majewski 	set_gadget_data(gadget, cdev);
10997010f5b9SLukasz Majewski 	INIT_LIST_HEAD(&cdev->configs);
11007010f5b9SLukasz Majewski 
11017010f5b9SLukasz Majewski 	/* preallocate control response and buffer */
11027010f5b9SLukasz Majewski 	cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
11037010f5b9SLukasz Majewski 	if (!cdev->req)
11047010f5b9SLukasz Majewski 		goto fail;
11057010f5b9SLukasz Majewski 	cdev->req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, USB_BUFSIZ);
11067010f5b9SLukasz Majewski 	if (!cdev->req->buf)
11077010f5b9SLukasz Majewski 		goto fail;
11087010f5b9SLukasz Majewski 	cdev->req->complete = composite_setup_complete;
11097010f5b9SLukasz Majewski 	gadget->ep0->driver_data = cdev;
11107010f5b9SLukasz Majewski 
11117010f5b9SLukasz Majewski 	cdev->bufsiz = USB_BUFSIZ;
11127010f5b9SLukasz Majewski 	cdev->driver = composite;
11137010f5b9SLukasz Majewski 
11147010f5b9SLukasz Majewski 	usb_gadget_set_selfpowered(gadget);
11157010f5b9SLukasz Majewski 	usb_ep_autoconfig_reset(cdev->gadget);
11167010f5b9SLukasz Majewski 
11177010f5b9SLukasz Majewski 	status = composite->bind(cdev);
11187010f5b9SLukasz Majewski 	if (status < 0)
11197010f5b9SLukasz Majewski 		goto fail;
11207010f5b9SLukasz Majewski 
112112595e99SPiotr Wilczek 	memcpy(&cdev->desc, composite->dev,
112212595e99SPiotr Wilczek 	       sizeof(struct usb_device_descriptor));
11237010f5b9SLukasz Majewski 	cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
11247010f5b9SLukasz Majewski 
11257010f5b9SLukasz Majewski 	debug("%s: ready\n", composite->name);
11267010f5b9SLukasz Majewski 	return 0;
11277010f5b9SLukasz Majewski 
11287010f5b9SLukasz Majewski fail:
11297010f5b9SLukasz Majewski 	composite_unbind(gadget);
11307010f5b9SLukasz Majewski 	return status;
11317010f5b9SLukasz Majewski }
11327010f5b9SLukasz Majewski 
11337010f5b9SLukasz Majewski static void
11347010f5b9SLukasz Majewski composite_suspend(struct usb_gadget *gadget)
11357010f5b9SLukasz Majewski {
11367010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = get_gadget_data(gadget);
11377010f5b9SLukasz Majewski 	struct usb_function		*f;
11387010f5b9SLukasz Majewski 
11397010f5b9SLukasz Majewski 	debug("%s: suspend\n", __func__);
11407010f5b9SLukasz Majewski 	if (cdev->config) {
11417010f5b9SLukasz Majewski 		list_for_each_entry(f, &cdev->config->functions, list) {
11427010f5b9SLukasz Majewski 			if (f->suspend)
11437010f5b9SLukasz Majewski 				f->suspend(f);
11447010f5b9SLukasz Majewski 		}
11457010f5b9SLukasz Majewski 	}
11467010f5b9SLukasz Majewski 	if (composite->suspend)
11477010f5b9SLukasz Majewski 		composite->suspend(cdev);
11487010f5b9SLukasz Majewski 
11497010f5b9SLukasz Majewski 	cdev->suspended = 1;
11507010f5b9SLukasz Majewski }
11517010f5b9SLukasz Majewski 
11527010f5b9SLukasz Majewski static void
11537010f5b9SLukasz Majewski composite_resume(struct usb_gadget *gadget)
11547010f5b9SLukasz Majewski {
11557010f5b9SLukasz Majewski 	struct usb_composite_dev	*cdev = get_gadget_data(gadget);
11567010f5b9SLukasz Majewski 	struct usb_function		*f;
11577010f5b9SLukasz Majewski 
11587010f5b9SLukasz Majewski 	debug("%s: resume\n", __func__);
11597010f5b9SLukasz Majewski 	if (composite->resume)
11607010f5b9SLukasz Majewski 		composite->resume(cdev);
11617010f5b9SLukasz Majewski 	if (cdev->config) {
11627010f5b9SLukasz Majewski 		list_for_each_entry(f, &cdev->config->functions, list) {
11637010f5b9SLukasz Majewski 			if (f->resume)
11647010f5b9SLukasz Majewski 				f->resume(f);
11657010f5b9SLukasz Majewski 		}
11667010f5b9SLukasz Majewski 	}
11677010f5b9SLukasz Majewski 
11687010f5b9SLukasz Majewski 	cdev->suspended = 0;
11697010f5b9SLukasz Majewski }
11707010f5b9SLukasz Majewski 
11717010f5b9SLukasz Majewski static struct usb_gadget_driver composite_driver = {
11727010f5b9SLukasz Majewski 	.speed		= USB_SPEED_HIGH,
11737010f5b9SLukasz Majewski 
11747010f5b9SLukasz Majewski 	.bind		= composite_bind,
11757010f5b9SLukasz Majewski 	.unbind         = composite_unbind,
11767010f5b9SLukasz Majewski 
11777010f5b9SLukasz Majewski 	.setup		= composite_setup,
11786d691732SLukasz Majewski 	.reset          = composite_disconnect,
11797010f5b9SLukasz Majewski 	.disconnect	= composite_disconnect,
11807010f5b9SLukasz Majewski 
11817010f5b9SLukasz Majewski 	.suspend        = composite_suspend,
11827010f5b9SLukasz Majewski 	.resume         = composite_resume,
11837010f5b9SLukasz Majewski };
11847010f5b9SLukasz Majewski 
11857010f5b9SLukasz Majewski /**
11867010f5b9SLukasz Majewski  * usb_composite_register() - register a composite driver
11877010f5b9SLukasz Majewski  * @driver: the driver to register
11887010f5b9SLukasz Majewski  * Context: single threaded during gadget setup
11897010f5b9SLukasz Majewski  *
11907010f5b9SLukasz Majewski  * This function is used to register drivers using the composite driver
11917010f5b9SLukasz Majewski  * framework.  The return value is zero, or a negative errno value.
11927010f5b9SLukasz Majewski  * Those values normally come from the driver's @bind method, which does
11937010f5b9SLukasz Majewski  * all the work of setting up the driver to match the hardware.
11947010f5b9SLukasz Majewski  *
11957010f5b9SLukasz Majewski  * On successful return, the gadget is ready to respond to requests from
11967010f5b9SLukasz Majewski  * the host, unless one of its components invokes usb_gadget_disconnect()
11977010f5b9SLukasz Majewski  * while it was binding.  That would usually be done in order to wait for
11987010f5b9SLukasz Majewski  * some userspace participation.
11997010f5b9SLukasz Majewski  */
12007010f5b9SLukasz Majewski int usb_composite_register(struct usb_composite_driver *driver)
12017010f5b9SLukasz Majewski {
12028038f6d2SSam Protsenko 	int res;
12038038f6d2SSam Protsenko 
12047010f5b9SLukasz Majewski 	if (!driver || !driver->dev || !driver->bind || composite)
12057010f5b9SLukasz Majewski 		return -EINVAL;
12067010f5b9SLukasz Majewski 
12077010f5b9SLukasz Majewski 	if (!driver->name)
12087010f5b9SLukasz Majewski 		driver->name = "composite";
12097010f5b9SLukasz Majewski 	composite = driver;
12107010f5b9SLukasz Majewski 
12118038f6d2SSam Protsenko 	res = usb_gadget_register_driver(&composite_driver);
12128038f6d2SSam Protsenko 	if (res != 0)
12138038f6d2SSam Protsenko 		composite = NULL;
12148038f6d2SSam Protsenko 
12158038f6d2SSam Protsenko 	return res;
12167010f5b9SLukasz Majewski }
12177010f5b9SLukasz Majewski 
12187010f5b9SLukasz Majewski /**
12197010f5b9SLukasz Majewski  * usb_composite_unregister() - unregister a composite driver
12207010f5b9SLukasz Majewski  * @driver: the driver to unregister
12217010f5b9SLukasz Majewski  *
12227010f5b9SLukasz Majewski  * This function is used to unregister drivers using the composite
12237010f5b9SLukasz Majewski  * driver framework.
12247010f5b9SLukasz Majewski  */
12257010f5b9SLukasz Majewski void usb_composite_unregister(struct usb_composite_driver *driver)
12267010f5b9SLukasz Majewski {
12277010f5b9SLukasz Majewski 	if (composite != driver)
12287010f5b9SLukasz Majewski 		return;
12297010f5b9SLukasz Majewski 	usb_gadget_unregister_driver(&composite_driver);
1230c67b0e42SHeiko Schocher 	composite = NULL;
12317010f5b9SLukasz Majewski }
1232