blob: 7da334f5d31d3ece60362bb9d37bab3e84b40461 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Remy Bohmer23cd1382009-07-29 18:18:43 +02002/*
3 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
4 *
5 * Copyright (C) 2004 David Brownell
6 *
Bin Menga1875592016-02-05 19:30:11 -08007 * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
Remy Bohmer23cd1382009-07-29 18:18:43 +02008 * Remy Bohmer <linux@bohmer.net>
9 */
10
11#include <common.h>
12#include <linux/usb/ch9.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020014#include <linux/usb/gadget.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070015#include <asm/unaligned.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020016#include "gadget_chips.h"
17
18#define isdigit(c) ('0' <= (c) && (c) <= '9')
19
20/* we must assign addresses for configurable endpoints (like net2280) */
21static unsigned epnum;
22
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040023/* #define MANY_ENDPOINTS */
Remy Bohmer23cd1382009-07-29 18:18:43 +020024#ifdef MANY_ENDPOINTS
25/* more than 15 configurable endpoints */
26static unsigned in_epnum;
27#endif
28
29
30/*
31 * This should work with endpoints from controller drivers sharing the
32 * same endpoint naming convention. By example:
33 *
34 * - ep1, ep2, ... address is fixed, not direction or type
35 * - ep1in, ep2out, ... address and direction are fixed, not type
36 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
37 * - ep1in-bulk, ep2out-iso, ... all three are fixed
38 * - ep-* ... no functionality restrictions
39 *
40 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
41 * Less common restrictions are implied by gadget_is_*().
42 *
43 * NOTE: each endpoint is unidirectional, as specified by its USB
44 * descriptor; and isn't specific to a configuration or altsetting.
45 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040046static int ep_matches(
Remy Bohmer23cd1382009-07-29 18:18:43 +020047 struct usb_gadget *gadget,
48 struct usb_ep *ep,
49 struct usb_endpoint_descriptor *desc
50)
51{
52 u8 type;
53 const char *tmp;
54 u16 max;
55
56 /* endpoint already claimed? */
57 if (NULL != ep->driver_data)
58 return 0;
59
60 /* only support ep0 for portable CONTROL traffic */
61 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
62 if (USB_ENDPOINT_XFER_CONTROL == type)
63 return 0;
64
65 /* some other naming convention */
66 if ('e' != ep->name[0])
67 return 0;
68
69 /* type-restriction: "-iso", "-bulk", or "-int".
70 * direction-restriction: "in", "out".
71 */
72 if ('-' != ep->name[2]) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040073 tmp = strrchr(ep->name, '-');
Remy Bohmer23cd1382009-07-29 18:18:43 +020074 if (tmp) {
75 switch (type) {
76 case USB_ENDPOINT_XFER_INT:
77 /* bulk endpoints handle interrupt transfers,
78 * except the toggle-quirky iso-synch kind
79 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040080 if ('s' == tmp[2]) /* == "-iso" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020081 return 0;
82 /* for now, avoid PXA "interrupt-in";
83 * it's documented as never using DATA1.
84 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040085 if (gadget_is_pxa(gadget)
86 && 'i' == tmp[1])
Remy Bohmer23cd1382009-07-29 18:18:43 +020087 return 0;
88 break;
89 case USB_ENDPOINT_XFER_BULK:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040090 if ('b' != tmp[1]) /* != "-bulk" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020091 return 0;
92 break;
93 case USB_ENDPOINT_XFER_ISOC:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040094 if ('s' != tmp[2]) /* != "-iso" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020095 return 0;
96 }
97 } else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040098 tmp = ep->name + strlen(ep->name);
Remy Bohmer23cd1382009-07-29 18:18:43 +020099 }
100
101 /* direction-restriction: "..in-..", "out-.." */
102 tmp--;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400103 if (!isdigit(*tmp)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200104 if (desc->bEndpointAddress & USB_DIR_IN) {
105 if ('n' != *tmp)
106 return 0;
107 } else {
108 if ('t' != *tmp)
109 return 0;
110 }
111 }
112 }
113
114 /* endpoint maxpacket size is an input parameter, except for bulk
115 * where it's an output parameter representing the full speed limit.
116 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
117 */
Tom Rinib2fb47f2011-12-15 08:40:51 -0700118 max = 0x7ff & le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Remy Bohmer23cd1382009-07-29 18:18:43 +0200119 switch (type) {
120 case USB_ENDPOINT_XFER_INT:
121 /* INT: limit 64 bytes full speed, 1024 high speed */
122 if (!gadget->is_dualspeed && max > 64)
123 return 0;
124 /* FALLTHROUGH */
125
126 case USB_ENDPOINT_XFER_ISOC:
127 /* ISO: limit 1023 bytes full speed, 1024 high speed */
128 if (ep->maxpacket < max)
129 return 0;
130 if (!gadget->is_dualspeed && max > 1023)
131 return 0;
132
133 /* BOTH: "high bandwidth" works only at high speed */
Tom Rinib2fb47f2011-12-15 08:40:51 -0700134 if ((get_unaligned(&desc->wMaxPacketSize) &
135 __constant_cpu_to_le16(3<<11))) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200136 if (!gadget->is_dualspeed)
137 return 0;
138 /* configure your hardware with enough buffering!! */
139 }
140 break;
141 }
142
143 /* MATCH!! */
144
145 /* report address */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400146 if (isdigit(ep->name[2])) {
147 u8 num = simple_strtoul(&ep->name[2], NULL, 10);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200148 desc->bEndpointAddress |= num;
149#ifdef MANY_ENDPOINTS
150 } else if (desc->bEndpointAddress & USB_DIR_IN) {
151 if (++in_epnum > 15)
152 return 0;
153 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
154#endif
155 } else {
156 if (++epnum > 15)
157 return 0;
158 desc->bEndpointAddress |= epnum;
159 }
160
161 /* report (variable) full speed bulk maxpacket */
162 if (USB_ENDPOINT_XFER_BULK == type) {
163 int size = ep->maxpacket;
164
165 /* min() doesn't work on bitfields with gcc-3.5 */
166 if (size > 64)
167 size = 64;
Tom Rinib2fb47f2011-12-15 08:40:51 -0700168 put_unaligned(cpu_to_le16(size), &desc->wMaxPacketSize);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200169 }
Ye Li71a57322021-01-25 21:43:44 +0800170
171 if (gadget->ops->ep_conf)
172 return gadget->ops->ep_conf(gadget, ep, desc);
173
Remy Bohmer23cd1382009-07-29 18:18:43 +0200174 return 1;
175}
176
177static struct usb_ep *
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400178find_ep(struct usb_gadget *gadget, const char *name)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200179{
180 struct usb_ep *ep;
181
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400182 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
183 if (0 == strcmp(ep->name, name))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200184 return ep;
185 }
186 return NULL;
187}
188
189/**
190 * usb_ep_autoconfig - choose an endpoint matching the descriptor
191 * @gadget: The device to which the endpoint must belong.
192 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
193 * initialized. For periodic transfers, the maximum packet
194 * size must also be initialized. This is modified on success.
195 *
196 * By choosing an endpoint to use with the specified descriptor, this
197 * routine simplifies writing gadget drivers that work with multiple
198 * USB device controllers. The endpoint would be passed later to
199 * usb_ep_enable(), along with some descriptor.
200 *
201 * That second descriptor won't always be the same as the first one.
202 * For example, isochronous endpoints can be autoconfigured for high
203 * bandwidth, and then used in several lower bandwidth altsettings.
204 * Also, high and full speed descriptors will be different.
205 *
206 * Be sure to examine and test the results of autoconfiguration on your
207 * hardware. This code may not make the best choices about how to use the
208 * USB controller, and it can't know all the restrictions that may apply.
209 * Some combinations of driver and hardware won't be able to autoconfigure.
210 *
211 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
212 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
213 * is initialized as if the endpoint were used at full speed. To prevent
214 * the endpoint from being returned by a later autoconfig call, claim it
215 * by assigning ep->driver_data to some non-null value.
216 *
217 * On failure, this returns a null endpoint descriptor.
218 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400219struct usb_ep *usb_ep_autoconfig(
Remy Bohmer23cd1382009-07-29 18:18:43 +0200220 struct usb_gadget *gadget,
221 struct usb_endpoint_descriptor *desc
222)
223{
Marek Szyprowski16bece52015-03-03 17:32:11 +0100224 struct usb_ep *ep = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200225 u8 type;
226
227 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
228
229 /* First, apply chip-specific "best usage" knowledge.
230 * This might make a good usb_gadget_ops hook ...
231 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400232 if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200233 /* ep-e, ep-f are PIO with only 64 byte fifos */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400234 ep = find_ep(gadget, "ep-e");
235 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200236 return ep;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400237 ep = find_ep(gadget, "ep-f");
238 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200239 return ep;
240
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400241 } else if (gadget_is_goku(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200242 if (USB_ENDPOINT_XFER_INT == type) {
243 /* single buffering is enough */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400244 ep = find_ep(gadget, "ep3-bulk");
245 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200246 return ep;
247 } else if (USB_ENDPOINT_XFER_BULK == type
248 && (USB_DIR_IN & desc->bEndpointAddress)) {
249 /* DMA may be available */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400250 ep = find_ep(gadget, "ep2-bulk");
251 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200252 return ep;
253 }
254
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400255 } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200256 /* single buffering is enough; maybe 8 byte fifo is too */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400257 ep = find_ep(gadget, "ep3in-bulk");
258 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200259 return ep;
260
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400261 } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
262 ep = find_ep(gadget, "ep1-bulk");
263 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200264 return ep;
Li Junc93edbf2021-01-25 21:43:45 +0800265#ifndef CONFIG_SPL_BUILD
Marek Szyprowski16bece52015-03-03 17:32:11 +0100266 } else if (gadget_is_dwc3(gadget)) {
267 const char *name = NULL;
268 /*
269 * First try standard, common configuration: ep1in-bulk,
270 * ep2out-bulk, ep3in-int to match other udc drivers to avoid
271 * confusion in already deployed software (endpoint numbers
272 * hardcoded in userspace software/drivers)
273 */
274 if ((desc->bEndpointAddress & USB_DIR_IN) &&
275 type == USB_ENDPOINT_XFER_BULK)
276 name = "ep1in";
277 else if ((desc->bEndpointAddress & USB_DIR_IN) == 0 &&
278 type == USB_ENDPOINT_XFER_BULK)
279 name = "ep2out";
280 else if ((desc->bEndpointAddress & USB_DIR_IN) &&
281 type == USB_ENDPOINT_XFER_INT)
282 name = "ep3in";
283
284 if (name)
285 ep = find_ep(gadget, name);
286 if (ep && ep_matches(gadget, ep, desc))
287 return ep;
Li Junc93edbf2021-01-25 21:43:45 +0800288#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200289 }
290
Vignesh Raghavendra77dcbdf2019-10-01 17:26:31 +0530291 if (gadget->ops->match_ep)
292 ep = gadget->ops->match_ep(gadget, desc, NULL);
293
Remy Bohmer23cd1382009-07-29 18:18:43 +0200294 /* Second, look at endpoints until an unclaimed one looks usable */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400295 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
296 if (ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200297 return ep;
298 }
299
300 /* Fail */
301 return NULL;
302}
303
304/**
305 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
306 * @gadget: device for which autoconfig state will be reset
307 *
308 * Use this for devices where one configuration may need to assign
309 * endpoint resources very differently from the next one. It clears
310 * state such as ep->driver_data and the record of assigned endpoints
311 * used by usb_ep_autoconfig().
312 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400313void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200314{
315 struct usb_ep *ep;
316
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400317 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200318 ep->driver_data = NULL;
319 }
320#ifdef MANY_ENDPOINTS
321 in_epnum = 0;
322#endif
323 epnum = 0;
324}