blob: 66599ce8efabab373f46f9c55ec3f7fbb916e6e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmerdf063442009-07-29 18:18:43 +02002/*
3 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
4 *
5 * Copyright (C) 2004 David Brownell
6 *
Bin Meng75574052016-02-05 19:30:11 -08007 * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
Remy Bohmerdf063442009-07-29 18:18:43 +02008 * Remy Bohmer <linux@bohmer.net>
9 */
10
Remy Bohmerdf063442009-07-29 18:18:43 +020011#include <linux/usb/ch9.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090012#include <linux/errno.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020013#include <linux/usb/gadget.h>
Tom Rini73424522011-12-15 08:40:51 -070014#include <asm/unaligned.h>
Remy Bohmerdf063442009-07-29 18:18:43 +020015
16#define isdigit(c) ('0' <= (c) && (c) <= '9')
17
18/* we must assign addresses for configurable endpoints (like net2280) */
19static unsigned epnum;
20
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040021/* #define MANY_ENDPOINTS */
Remy Bohmerdf063442009-07-29 18:18:43 +020022#ifdef MANY_ENDPOINTS
23/* more than 15 configurable endpoints */
24static unsigned in_epnum;
25#endif
26
27
28/*
29 * This should work with endpoints from controller drivers sharing the
30 * same endpoint naming convention. By example:
31 *
32 * - ep1, ep2, ... address is fixed, not direction or type
33 * - ep1in, ep2out, ... address and direction are fixed, not type
34 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
35 * - ep1in-bulk, ep2out-iso, ... all three are fixed
36 * - ep-* ... no functionality restrictions
37 *
38 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
39 * Less common restrictions are implied by gadget_is_*().
40 *
41 * NOTE: each endpoint is unidirectional, as specified by its USB
42 * descriptor; and isn't specific to a configuration or altsetting.
43 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040044static int ep_matches(
Remy Bohmerdf063442009-07-29 18:18:43 +020045 struct usb_gadget *gadget,
46 struct usb_ep *ep,
47 struct usb_endpoint_descriptor *desc
48)
49{
50 u8 type;
51 const char *tmp;
52 u16 max;
53
54 /* endpoint already claimed? */
55 if (NULL != ep->driver_data)
56 return 0;
57
58 /* only support ep0 for portable CONTROL traffic */
59 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
60 if (USB_ENDPOINT_XFER_CONTROL == type)
61 return 0;
62
63 /* some other naming convention */
64 if ('e' != ep->name[0])
65 return 0;
66
67 /* type-restriction: "-iso", "-bulk", or "-int".
68 * direction-restriction: "in", "out".
69 */
70 if ('-' != ep->name[2]) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040071 tmp = strrchr(ep->name, '-');
Remy Bohmerdf063442009-07-29 18:18:43 +020072 if (tmp) {
73 switch (type) {
74 case USB_ENDPOINT_XFER_INT:
75 /* bulk endpoints handle interrupt transfers,
76 * except the toggle-quirky iso-synch kind
77 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040078 if ('s' == tmp[2]) /* == "-iso" */
Remy Bohmerdf063442009-07-29 18:18:43 +020079 return 0;
Remy Bohmerdf063442009-07-29 18:18:43 +020080 break;
81 case USB_ENDPOINT_XFER_BULK:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040082 if ('b' != tmp[1]) /* != "-bulk" */
Remy Bohmerdf063442009-07-29 18:18:43 +020083 return 0;
84 break;
85 case USB_ENDPOINT_XFER_ISOC:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040086 if ('s' != tmp[2]) /* != "-iso" */
Remy Bohmerdf063442009-07-29 18:18:43 +020087 return 0;
88 }
89 } else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040090 tmp = ep->name + strlen(ep->name);
Remy Bohmerdf063442009-07-29 18:18:43 +020091 }
92
93 /* direction-restriction: "..in-..", "out-.." */
94 tmp--;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040095 if (!isdigit(*tmp)) {
Remy Bohmerdf063442009-07-29 18:18:43 +020096 if (desc->bEndpointAddress & USB_DIR_IN) {
97 if ('n' != *tmp)
98 return 0;
99 } else {
100 if ('t' != *tmp)
101 return 0;
102 }
103 }
104 }
105
106 /* endpoint maxpacket size is an input parameter, except for bulk
107 * where it's an output parameter representing the full speed limit.
108 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
109 */
Tom Rini73424522011-12-15 08:40:51 -0700110 max = 0x7ff & le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Remy Bohmerdf063442009-07-29 18:18:43 +0200111 switch (type) {
112 case USB_ENDPOINT_XFER_INT:
113 /* INT: limit 64 bytes full speed, 1024 high speed */
114 if (!gadget->is_dualspeed && max > 64)
115 return 0;
116 /* FALLTHROUGH */
117
118 case USB_ENDPOINT_XFER_ISOC:
119 /* ISO: limit 1023 bytes full speed, 1024 high speed */
120 if (ep->maxpacket < max)
121 return 0;
122 if (!gadget->is_dualspeed && max > 1023)
123 return 0;
124
125 /* BOTH: "high bandwidth" works only at high speed */
Tom Rini73424522011-12-15 08:40:51 -0700126 if ((get_unaligned(&desc->wMaxPacketSize) &
127 __constant_cpu_to_le16(3<<11))) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200128 if (!gadget->is_dualspeed)
129 return 0;
130 /* configure your hardware with enough buffering!! */
131 }
132 break;
133 }
134
135 /* MATCH!! */
136
137 /* report address */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400138 if (isdigit(ep->name[2])) {
Simon Glassff9b9032021-07-24 09:03:30 -0600139 u8 num = dectoul(&ep->name[2], NULL);
Remy Bohmerdf063442009-07-29 18:18:43 +0200140 desc->bEndpointAddress |= num;
141#ifdef MANY_ENDPOINTS
142 } else if (desc->bEndpointAddress & USB_DIR_IN) {
143 if (++in_epnum > 15)
144 return 0;
145 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
146#endif
147 } else {
148 if (++epnum > 15)
149 return 0;
150 desc->bEndpointAddress |= epnum;
151 }
152
153 /* report (variable) full speed bulk maxpacket */
154 if (USB_ENDPOINT_XFER_BULK == type) {
155 int size = ep->maxpacket;
156
157 /* min() doesn't work on bitfields with gcc-3.5 */
158 if (size > 64)
159 size = 64;
Tom Rini73424522011-12-15 08:40:51 -0700160 put_unaligned(cpu_to_le16(size), &desc->wMaxPacketSize);
Remy Bohmerdf063442009-07-29 18:18:43 +0200161 }
Ye Lid22fb8a2021-01-25 21:43:44 +0800162
163 if (gadget->ops->ep_conf)
164 return gadget->ops->ep_conf(gadget, ep, desc);
165
Remy Bohmerdf063442009-07-29 18:18:43 +0200166 return 1;
167}
168
169static struct usb_ep *
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400170find_ep(struct usb_gadget *gadget, const char *name)
Remy Bohmerdf063442009-07-29 18:18:43 +0200171{
172 struct usb_ep *ep;
173
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400174 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
175 if (0 == strcmp(ep->name, name))
Remy Bohmerdf063442009-07-29 18:18:43 +0200176 return ep;
177 }
178 return NULL;
179}
180
181/**
182 * usb_ep_autoconfig - choose an endpoint matching the descriptor
183 * @gadget: The device to which the endpoint must belong.
184 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
185 * initialized. For periodic transfers, the maximum packet
186 * size must also be initialized. This is modified on success.
187 *
188 * By choosing an endpoint to use with the specified descriptor, this
189 * routine simplifies writing gadget drivers that work with multiple
190 * USB device controllers. The endpoint would be passed later to
191 * usb_ep_enable(), along with some descriptor.
192 *
193 * That second descriptor won't always be the same as the first one.
194 * For example, isochronous endpoints can be autoconfigured for high
195 * bandwidth, and then used in several lower bandwidth altsettings.
196 * Also, high and full speed descriptors will be different.
197 *
198 * Be sure to examine and test the results of autoconfiguration on your
199 * hardware. This code may not make the best choices about how to use the
200 * USB controller, and it can't know all the restrictions that may apply.
201 * Some combinations of driver and hardware won't be able to autoconfigure.
202 *
203 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
204 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
205 * is initialized as if the endpoint were used at full speed. To prevent
206 * the endpoint from being returned by a later autoconfig call, claim it
207 * by assigning ep->driver_data to some non-null value.
208 *
209 * On failure, this returns a null endpoint descriptor.
210 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400211struct usb_ep *usb_ep_autoconfig(
Remy Bohmerdf063442009-07-29 18:18:43 +0200212 struct usb_gadget *gadget,
213 struct usb_endpoint_descriptor *desc
214)
215{
Marek Szyprowski06c7bde2015-03-03 17:32:11 +0100216 struct usb_ep *ep = NULL;
Remy Bohmerdf063442009-07-29 18:18:43 +0200217 u8 type;
218
219 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
220
221 /* First, apply chip-specific "best usage" knowledge.
222 * This might make a good usb_gadget_ops hook ...
223 */
Marek Vasutbd15f322024-06-09 23:32:17 +0200224 if (!IS_ENABLED(CONFIG_SPL_BUILD) &&
225 IS_ENABLED(CONFIG_USB_DWC3_GADGET) &&
226 !strcmp("dwc3-gadget", gadget->name)) {
Marek Szyprowski06c7bde2015-03-03 17:32:11 +0100227 const char *name = NULL;
228 /*
229 * First try standard, common configuration: ep1in-bulk,
230 * ep2out-bulk, ep3in-int to match other udc drivers to avoid
231 * confusion in already deployed software (endpoint numbers
232 * hardcoded in userspace software/drivers)
233 */
234 if ((desc->bEndpointAddress & USB_DIR_IN) &&
235 type == USB_ENDPOINT_XFER_BULK)
236 name = "ep1in";
237 else if ((desc->bEndpointAddress & USB_DIR_IN) == 0 &&
238 type == USB_ENDPOINT_XFER_BULK)
239 name = "ep2out";
240 else if ((desc->bEndpointAddress & USB_DIR_IN) &&
241 type == USB_ENDPOINT_XFER_INT)
242 name = "ep3in";
243
244 if (name)
245 ep = find_ep(gadget, name);
246 if (ep && ep_matches(gadget, ep, desc))
247 return ep;
Remy Bohmerdf063442009-07-29 18:18:43 +0200248 }
249
Marek Vasut0ee8a142024-06-09 23:32:18 +0200250 if (gadget->ops->match_ep) {
Vignesh Raghavendracb39a962019-10-01 17:26:31 +0530251 ep = gadget->ops->match_ep(gadget, desc, NULL);
Marek Vasut0ee8a142024-06-09 23:32:18 +0200252 if (ep && ep_matches(gadget, ep, desc))
253 return ep;
254 }
Vignesh Raghavendracb39a962019-10-01 17:26:31 +0530255
Remy Bohmerdf063442009-07-29 18:18:43 +0200256 /* Second, look at endpoints until an unclaimed one looks usable */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400257 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
258 if (ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200259 return ep;
260 }
261
262 /* Fail */
263 return NULL;
264}
265
266/**
267 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
268 * @gadget: device for which autoconfig state will be reset
269 *
270 * Use this for devices where one configuration may need to assign
271 * endpoint resources very differently from the next one. It clears
272 * state such as ep->driver_data and the record of assigned endpoints
273 * used by usb_ep_autoconfig().
274 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400275void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +0200276{
277 struct usb_ep *ep;
278
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400279 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200280 ep->driver_data = NULL;
281 }
282#ifdef MANY_ENDPOINTS
283 in_epnum = 0;
284#endif
285 epnum = 0;
286}