xref: /openbmc/linux/drivers/usb/gadget/epautoconf.c (revision 6b0be25c)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2004 David Brownell
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/kernel.h>
9dc995fc2SSebastian Andrzej Siewior #include <linux/module.h>
101da177e4SLinus Torvalds #include <linux/types.h>
111da177e4SLinus Torvalds #include <linux/device.h>
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include <linux/ctype.h>
141da177e4SLinus Torvalds #include <linux/string.h>
151da177e4SLinus Torvalds 
165f848137SDavid Brownell #include <linux/usb/ch9.h>
179454a57aSDavid Brownell #include <linux/usb/gadget.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds /**
20a59d6b91STatyana Brokhman  * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
21a59d6b91STatyana Brokhman  * descriptor and ep companion descriptor
22a59d6b91STatyana Brokhman  * @gadget: The device to which the endpoint must belong.
23a59d6b91STatyana Brokhman  * @desc: Endpoint descriptor, with endpoint direction and transfer mode
24a59d6b91STatyana Brokhman  *    initialized.  For periodic transfers, the maximum packet
25a59d6b91STatyana Brokhman  *    size must also be initialized.  This is modified on
26a59d6b91STatyana Brokhman  *    success.
27a59d6b91STatyana Brokhman  * @ep_comp: Endpoint companion descriptor, with the required
28a59d6b91STatyana Brokhman  *    number of streams. Will be modified when the chosen EP
29a59d6b91STatyana Brokhman  *    supports a different number of streams.
30a59d6b91STatyana Brokhman  *
31a59d6b91STatyana Brokhman  * This routine replaces the usb_ep_autoconfig when needed
32a59d6b91STatyana Brokhman  * superspeed enhancments. If such enhancemnets are required,
33a59d6b91STatyana Brokhman  * the FD should call usb_ep_autoconfig_ss directly and provide
34a59d6b91STatyana Brokhman  * the additional ep_comp parameter.
35a59d6b91STatyana Brokhman  *
36a59d6b91STatyana Brokhman  * By choosing an endpoint to use with the specified descriptor,
37a59d6b91STatyana Brokhman  * this routine simplifies writing gadget drivers that work with
38a59d6b91STatyana Brokhman  * multiple USB device controllers.  The endpoint would be
39a59d6b91STatyana Brokhman  * passed later to usb_ep_enable(), along with some descriptor.
40a59d6b91STatyana Brokhman  *
41a59d6b91STatyana Brokhman  * That second descriptor won't always be the same as the first one.
42a59d6b91STatyana Brokhman  * For example, isochronous endpoints can be autoconfigured for high
43a59d6b91STatyana Brokhman  * bandwidth, and then used in several lower bandwidth altsettings.
44a59d6b91STatyana Brokhman  * Also, high and full speed descriptors will be different.
45a59d6b91STatyana Brokhman  *
46a59d6b91STatyana Brokhman  * Be sure to examine and test the results of autoconfiguration
47a59d6b91STatyana Brokhman  * on your hardware.  This code may not make the best choices
48a59d6b91STatyana Brokhman  * about how to use the USB controller, and it can't know all
49a59d6b91STatyana Brokhman  * the restrictions that may apply. Some combinations of driver
50a59d6b91STatyana Brokhman  * and hardware won't be able to autoconfigure.
51a59d6b91STatyana Brokhman  *
52f871cb9bSRobert Baldyga  * On success, this returns an claimed usb_ep, and modifies the endpoint
53a59d6b91STatyana Brokhman  * descriptor bEndpointAddress.  For bulk endpoints, the wMaxPacket value
54a59d6b91STatyana Brokhman  * is initialized as if the endpoint were used at full speed and
55a59d6b91STatyana Brokhman  * the bmAttribute field in the ep companion descriptor is
56a59d6b91STatyana Brokhman  * updated with the assigned number of streams if it is
57a59d6b91STatyana Brokhman  * different from the original value. To prevent the endpoint
58f871cb9bSRobert Baldyga  * from being returned by a later autoconfig call, claims it by
59cc476b42SRobert Baldyga  * assigning ep->claimed to true.
60a59d6b91STatyana Brokhman  *
61a59d6b91STatyana Brokhman  * On failure, this returns a null endpoint descriptor.
62a59d6b91STatyana Brokhman  */
usb_ep_autoconfig_ss(struct usb_gadget * gadget,struct usb_endpoint_descriptor * desc,struct usb_ss_ep_comp_descriptor * ep_comp)63a59d6b91STatyana Brokhman struct usb_ep *usb_ep_autoconfig_ss(
64a59d6b91STatyana Brokhman 	struct usb_gadget		*gadget,
65a59d6b91STatyana Brokhman 	struct usb_endpoint_descriptor	*desc,
66a59d6b91STatyana Brokhman 	struct usb_ss_ep_comp_descriptor *ep_comp
67a59d6b91STatyana Brokhman )
68a59d6b91STatyana Brokhman {
69a59d6b91STatyana Brokhman 	struct usb_ep	*ep;
70a59d6b91STatyana Brokhman 
71596c154dSRobert Baldyga 	if (gadget->ops->match_ep) {
72596c154dSRobert Baldyga 		ep = gadget->ops->match_ep(gadget, desc, ep_comp);
73596c154dSRobert Baldyga 		if (ep)
74596c154dSRobert Baldyga 			goto found_ep;
75596c154dSRobert Baldyga 	}
76596c154dSRobert Baldyga 
77a59d6b91STatyana Brokhman 	/* Second, look at endpoints until an unclaimed one looks usable */
78a59d6b91STatyana Brokhman 	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
794278c687SRobert Baldyga 		if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
80609ca228SSebastian Andrzej Siewior 			goto found_ep;
81a59d6b91STatyana Brokhman 	}
82a59d6b91STatyana Brokhman 
83a59d6b91STatyana Brokhman 	/* Fail */
84a59d6b91STatyana Brokhman 	return NULL;
85609ca228SSebastian Andrzej Siewior found_ep:
865dbe135aSRobert Baldyga 
875dbe135aSRobert Baldyga 	/*
885dbe135aSRobert Baldyga 	 * If the protocol driver hasn't yet decided on wMaxPacketSize
895dbe135aSRobert Baldyga 	 * and wants to know the maximum possible, provide the info.
905dbe135aSRobert Baldyga 	 */
915dbe135aSRobert Baldyga 	if (desc->wMaxPacketSize == 0)
925dbe135aSRobert Baldyga 		desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
935dbe135aSRobert Baldyga 
945dbe135aSRobert Baldyga 	/* report address */
955dbe135aSRobert Baldyga 	desc->bEndpointAddress &= USB_DIR_IN;
965dbe135aSRobert Baldyga 	if (isdigit(ep->name[2])) {
975dbe135aSRobert Baldyga 		u8 num = simple_strtoul(&ep->name[2], NULL, 10);
985dbe135aSRobert Baldyga 		desc->bEndpointAddress |= num;
995dbe135aSRobert Baldyga 	} else if (desc->bEndpointAddress & USB_DIR_IN) {
1005dbe135aSRobert Baldyga 		if (++gadget->in_epnum > 15)
1015dbe135aSRobert Baldyga 			return NULL;
1025dbe135aSRobert Baldyga 		desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
1035dbe135aSRobert Baldyga 	} else {
1045dbe135aSRobert Baldyga 		if (++gadget->out_epnum > 15)
1055dbe135aSRobert Baldyga 			return NULL;
1065dbe135aSRobert Baldyga 		desc->bEndpointAddress |= gadget->out_epnum;
1075dbe135aSRobert Baldyga 	}
1085dbe135aSRobert Baldyga 
1095dbe135aSRobert Baldyga 	ep->address = desc->bEndpointAddress;
110609ca228SSebastian Andrzej Siewior 	ep->desc = NULL;
111609ca228SSebastian Andrzej Siewior 	ep->comp_desc = NULL;
112cc476b42SRobert Baldyga 	ep->claimed = true;
113609ca228SSebastian Andrzej Siewior 	return ep;
114a59d6b91STatyana Brokhman }
115dc995fc2SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
116a59d6b91STatyana Brokhman 
117a59d6b91STatyana Brokhman /**
118a59d6b91STatyana Brokhman  * usb_ep_autoconfig() - choose an endpoint matching the
119a59d6b91STatyana Brokhman  * descriptor
1201da177e4SLinus Torvalds  * @gadget: The device to which the endpoint must belong.
1211da177e4SLinus Torvalds  * @desc: Endpoint descriptor, with endpoint direction and transfer mode
1221da177e4SLinus Torvalds  *	initialized.  For periodic transfers, the maximum packet
1231da177e4SLinus Torvalds  *	size must also be initialized.  This is modified on success.
1241da177e4SLinus Torvalds  *
1251da177e4SLinus Torvalds  * By choosing an endpoint to use with the specified descriptor, this
1261da177e4SLinus Torvalds  * routine simplifies writing gadget drivers that work with multiple
1271da177e4SLinus Torvalds  * USB device controllers.  The endpoint would be passed later to
1281da177e4SLinus Torvalds  * usb_ep_enable(), along with some descriptor.
1291da177e4SLinus Torvalds  *
1301da177e4SLinus Torvalds  * That second descriptor won't always be the same as the first one.
1311da177e4SLinus Torvalds  * For example, isochronous endpoints can be autoconfigured for high
1321da177e4SLinus Torvalds  * bandwidth, and then used in several lower bandwidth altsettings.
1331da177e4SLinus Torvalds  * Also, high and full speed descriptors will be different.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * Be sure to examine and test the results of autoconfiguration on your
1361da177e4SLinus Torvalds  * hardware.  This code may not make the best choices about how to use the
1371da177e4SLinus Torvalds  * USB controller, and it can't know all the restrictions that may apply.
1381da177e4SLinus Torvalds  * Some combinations of driver and hardware won't be able to autoconfigure.
1391da177e4SLinus Torvalds  *
140f871cb9bSRobert Baldyga  * On success, this returns an claimed usb_ep, and modifies the endpoint
1411da177e4SLinus Torvalds  * descriptor bEndpointAddress.  For bulk endpoints, the wMaxPacket value
142dffe2d7fSAndrzej Pietrasiewicz  * is initialized as if the endpoint were used at full speed. Because of
143dffe2d7fSAndrzej Pietrasiewicz  * that the users must consider adjusting the autoconfigured descriptor.
144dffe2d7fSAndrzej Pietrasiewicz  * To prevent the endpoint from being returned by a later autoconfig call,
145dffe2d7fSAndrzej Pietrasiewicz  * claims it by assigning ep->claimed to true.
1461da177e4SLinus Torvalds  *
1471da177e4SLinus Torvalds  * On failure, this returns a null endpoint descriptor.
1481da177e4SLinus Torvalds  */
usb_ep_autoconfig(struct usb_gadget * gadget,struct usb_endpoint_descriptor * desc)14928824b18SMichal Nazarewicz struct usb_ep *usb_ep_autoconfig(
1501da177e4SLinus Torvalds 	struct usb_gadget		*gadget,
1511da177e4SLinus Torvalds 	struct usb_endpoint_descriptor	*desc
1521da177e4SLinus Torvalds )
1531da177e4SLinus Torvalds {
154dffe2d7fSAndrzej Pietrasiewicz 	struct usb_ep	*ep;
155dffe2d7fSAndrzej Pietrasiewicz 	u8		type;
156dffe2d7fSAndrzej Pietrasiewicz 
157dffe2d7fSAndrzej Pietrasiewicz 	ep = usb_ep_autoconfig_ss(gadget, desc, NULL);
158dffe2d7fSAndrzej Pietrasiewicz 	if (!ep)
159dffe2d7fSAndrzej Pietrasiewicz 		return NULL;
160dffe2d7fSAndrzej Pietrasiewicz 
161dffe2d7fSAndrzej Pietrasiewicz 	type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
162dffe2d7fSAndrzej Pietrasiewicz 
163dffe2d7fSAndrzej Pietrasiewicz 	/* report (variable) full speed bulk maxpacket */
164dffe2d7fSAndrzej Pietrasiewicz 	if (type == USB_ENDPOINT_XFER_BULK) {
165dffe2d7fSAndrzej Pietrasiewicz 		int size = ep->maxpacket_limit;
166dffe2d7fSAndrzej Pietrasiewicz 
167dffe2d7fSAndrzej Pietrasiewicz 		/* min() doesn't work on bitfields with gcc-3.5 */
168dffe2d7fSAndrzej Pietrasiewicz 		if (size > 64)
169dffe2d7fSAndrzej Pietrasiewicz 			size = 64;
170dffe2d7fSAndrzej Pietrasiewicz 		desc->wMaxPacketSize = cpu_to_le16(size);
171dffe2d7fSAndrzej Pietrasiewicz 	}
172dffe2d7fSAndrzej Pietrasiewicz 
173dffe2d7fSAndrzej Pietrasiewicz 	return ep;
1741da177e4SLinus Torvalds }
175dc995fc2SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /**
178b67f628cSRobert Baldyga  * usb_ep_autoconfig_release - releases endpoint and set it to initial state
179b67f628cSRobert Baldyga  * @ep: endpoint which should be released
180b67f628cSRobert Baldyga  *
181b67f628cSRobert Baldyga  * This function can be used during function bind for endpoints obtained
182b67f628cSRobert Baldyga  * from usb_ep_autoconfig(). It unclaims endpoint claimed by
183b67f628cSRobert Baldyga  * usb_ep_autoconfig() to make it available for other functions. Endpoint
184*6b0be25cSAjay Garg  * which was released is no longer valid and shouldn't be used in
185b67f628cSRobert Baldyga  * context of function which released it.
186b67f628cSRobert Baldyga  */
usb_ep_autoconfig_release(struct usb_ep * ep)187b67f628cSRobert Baldyga void usb_ep_autoconfig_release(struct usb_ep *ep)
188b67f628cSRobert Baldyga {
189b67f628cSRobert Baldyga 	ep->claimed = false;
190b67f628cSRobert Baldyga 	ep->driver_data = NULL;
191b67f628cSRobert Baldyga }
192b67f628cSRobert Baldyga EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release);
193b67f628cSRobert Baldyga 
194b67f628cSRobert Baldyga /**
1951da177e4SLinus Torvalds  * usb_ep_autoconfig_reset - reset endpoint autoconfig state
1961da177e4SLinus Torvalds  * @gadget: device for which autoconfig state will be reset
1971da177e4SLinus Torvalds  *
1981da177e4SLinus Torvalds  * Use this for devices where one configuration may need to assign
1991da177e4SLinus Torvalds  * endpoint resources very differently from the next one.  It clears
200cc476b42SRobert Baldyga  * state such as ep->claimed and the record of assigned endpoints
2011da177e4SLinus Torvalds  * used by usb_ep_autoconfig().
2021da177e4SLinus Torvalds  */
usb_ep_autoconfig_reset(struct usb_gadget * gadget)20328824b18SMichal Nazarewicz void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	struct usb_ep	*ep;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	list_for_each_entry (ep, &gadget->ep_list, ep_list) {
208cc476b42SRobert Baldyga 		ep->claimed = false;
209e4c1b1baSRobert Baldyga 		ep->driver_data = NULL;
2101da177e4SLinus Torvalds 	}
211e87bb711SSebastian Andrzej Siewior 	gadget->in_epnum = 0;
212e87bb711SSebastian Andrzej Siewior 	gadget->out_epnum = 0;
2131da177e4SLinus Torvalds }
214dc995fc2SSebastian Andrzej Siewior EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);
215