blob: 0a70035ce04b087d75db871bd64e9cf7d03b58b1 [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
Remy Bohmer23cd1382009-07-29 18:18:43 +020011#include <linux/usb/ch9.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090012#include <linux/errno.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020013#include <linux/usb/gadget.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070014#include <asm/unaligned.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020015#include "gadget_chips.h"
16
17#define isdigit(c) ('0' <= (c) && (c) <= '9')
18
19/* we must assign addresses for configurable endpoints (like net2280) */
20static unsigned epnum;
21
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040022/* #define MANY_ENDPOINTS */
Remy Bohmer23cd1382009-07-29 18:18:43 +020023#ifdef MANY_ENDPOINTS
24/* more than 15 configurable endpoints */
25static unsigned in_epnum;
26#endif
27
28
29/*
30 * This should work with endpoints from controller drivers sharing the
31 * same endpoint naming convention. By example:
32 *
33 * - ep1, ep2, ... address is fixed, not direction or type
34 * - ep1in, ep2out, ... address and direction are fixed, not type
35 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
36 * - ep1in-bulk, ep2out-iso, ... all three are fixed
37 * - ep-* ... no functionality restrictions
38 *
39 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
40 * Less common restrictions are implied by gadget_is_*().
41 *
42 * NOTE: each endpoint is unidirectional, as specified by its USB
43 * descriptor; and isn't specific to a configuration or altsetting.
44 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040045static int ep_matches(
Remy Bohmer23cd1382009-07-29 18:18:43 +020046 struct usb_gadget *gadget,
47 struct usb_ep *ep,
48 struct usb_endpoint_descriptor *desc
49)
50{
51 u8 type;
52 const char *tmp;
53 u16 max;
54
55 /* endpoint already claimed? */
56 if (NULL != ep->driver_data)
57 return 0;
58
59 /* only support ep0 for portable CONTROL traffic */
60 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
61 if (USB_ENDPOINT_XFER_CONTROL == type)
62 return 0;
63
64 /* some other naming convention */
65 if ('e' != ep->name[0])
66 return 0;
67
68 /* type-restriction: "-iso", "-bulk", or "-int".
69 * direction-restriction: "in", "out".
70 */
71 if ('-' != ep->name[2]) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040072 tmp = strrchr(ep->name, '-');
Remy Bohmer23cd1382009-07-29 18:18:43 +020073 if (tmp) {
74 switch (type) {
75 case USB_ENDPOINT_XFER_INT:
76 /* bulk endpoints handle interrupt transfers,
77 * except the toggle-quirky iso-synch kind
78 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040079 if ('s' == tmp[2]) /* == "-iso" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020080 return 0;
Remy Bohmer23cd1382009-07-29 18:18:43 +020081 break;
82 case USB_ENDPOINT_XFER_BULK:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040083 if ('b' != tmp[1]) /* != "-bulk" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020084 return 0;
85 break;
86 case USB_ENDPOINT_XFER_ISOC:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040087 if ('s' != tmp[2]) /* != "-iso" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020088 return 0;
89 }
90 } else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040091 tmp = ep->name + strlen(ep->name);
Remy Bohmer23cd1382009-07-29 18:18:43 +020092 }
93
94 /* direction-restriction: "..in-..", "out-.." */
95 tmp--;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040096 if (!isdigit(*tmp)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +020097 if (desc->bEndpointAddress & USB_DIR_IN) {
98 if ('n' != *tmp)
99 return 0;
100 } else {
101 if ('t' != *tmp)
102 return 0;
103 }
104 }
105 }
106
107 /* endpoint maxpacket size is an input parameter, except for bulk
108 * where it's an output parameter representing the full speed limit.
109 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
110 */
Tom Rinib2fb47f2011-12-15 08:40:51 -0700111 max = 0x7ff & le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Remy Bohmer23cd1382009-07-29 18:18:43 +0200112 switch (type) {
113 case USB_ENDPOINT_XFER_INT:
114 /* INT: limit 64 bytes full speed, 1024 high speed */
115 if (!gadget->is_dualspeed && max > 64)
116 return 0;
117 /* FALLTHROUGH */
118
119 case USB_ENDPOINT_XFER_ISOC:
120 /* ISO: limit 1023 bytes full speed, 1024 high speed */
121 if (ep->maxpacket < max)
122 return 0;
123 if (!gadget->is_dualspeed && max > 1023)
124 return 0;
125
126 /* BOTH: "high bandwidth" works only at high speed */
Tom Rinib2fb47f2011-12-15 08:40:51 -0700127 if ((get_unaligned(&desc->wMaxPacketSize) &
128 __constant_cpu_to_le16(3<<11))) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200129 if (!gadget->is_dualspeed)
130 return 0;
131 /* configure your hardware with enough buffering!! */
132 }
133 break;
134 }
135
136 /* MATCH!! */
137
138 /* report address */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400139 if (isdigit(ep->name[2])) {
Simon Glass0b1284e2021-07-24 09:03:30 -0600140 u8 num = dectoul(&ep->name[2], NULL);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200141 desc->bEndpointAddress |= num;
142#ifdef MANY_ENDPOINTS
143 } else if (desc->bEndpointAddress & USB_DIR_IN) {
144 if (++in_epnum > 15)
145 return 0;
146 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
147#endif
148 } else {
149 if (++epnum > 15)
150 return 0;
151 desc->bEndpointAddress |= epnum;
152 }
153
154 /* report (variable) full speed bulk maxpacket */
155 if (USB_ENDPOINT_XFER_BULK == type) {
156 int size = ep->maxpacket;
157
158 /* min() doesn't work on bitfields with gcc-3.5 */
159 if (size > 64)
160 size = 64;
Tom Rinib2fb47f2011-12-15 08:40:51 -0700161 put_unaligned(cpu_to_le16(size), &desc->wMaxPacketSize);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200162 }
Ye Li71a57322021-01-25 21:43:44 +0800163
164 if (gadget->ops->ep_conf)
165 return gadget->ops->ep_conf(gadget, ep, desc);
166
Remy Bohmer23cd1382009-07-29 18:18:43 +0200167 return 1;
168}
169
170static struct usb_ep *
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400171find_ep(struct usb_gadget *gadget, const char *name)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200172{
173 struct usb_ep *ep;
174
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400175 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
176 if (0 == strcmp(ep->name, name))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200177 return ep;
178 }
179 return NULL;
180}
181
182/**
183 * usb_ep_autoconfig - choose an endpoint matching the descriptor
184 * @gadget: The device to which the endpoint must belong.
185 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
186 * initialized. For periodic transfers, the maximum packet
187 * size must also be initialized. This is modified on success.
188 *
189 * By choosing an endpoint to use with the specified descriptor, this
190 * routine simplifies writing gadget drivers that work with multiple
191 * USB device controllers. The endpoint would be passed later to
192 * usb_ep_enable(), along with some descriptor.
193 *
194 * That second descriptor won't always be the same as the first one.
195 * For example, isochronous endpoints can be autoconfigured for high
196 * bandwidth, and then used in several lower bandwidth altsettings.
197 * Also, high and full speed descriptors will be different.
198 *
199 * Be sure to examine and test the results of autoconfiguration on your
200 * hardware. This code may not make the best choices about how to use the
201 * USB controller, and it can't know all the restrictions that may apply.
202 * Some combinations of driver and hardware won't be able to autoconfigure.
203 *
204 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
205 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
206 * is initialized as if the endpoint were used at full speed. To prevent
207 * the endpoint from being returned by a later autoconfig call, claim it
208 * by assigning ep->driver_data to some non-null value.
209 *
210 * On failure, this returns a null endpoint descriptor.
211 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400212struct usb_ep *usb_ep_autoconfig(
Remy Bohmer23cd1382009-07-29 18:18:43 +0200213 struct usb_gadget *gadget,
214 struct usb_endpoint_descriptor *desc
215)
216{
Marek Szyprowski16bece52015-03-03 17:32:11 +0100217 struct usb_ep *ep = NULL;
Remy Bohmer23cd1382009-07-29 18:18:43 +0200218 u8 type;
219
220 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
221
222 /* First, apply chip-specific "best usage" knowledge.
223 * This might make a good usb_gadget_ops hook ...
224 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400225 if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200226 /* ep-e, ep-f are PIO with only 64 byte fifos */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400227 ep = find_ep(gadget, "ep-e");
228 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200229 return ep;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400230 ep = find_ep(gadget, "ep-f");
231 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200232 return ep;
233
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400234 } else if (gadget_is_goku(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200235 if (USB_ENDPOINT_XFER_INT == type) {
236 /* single buffering is enough */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400237 ep = find_ep(gadget, "ep3-bulk");
238 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200239 return ep;
240 } else if (USB_ENDPOINT_XFER_BULK == type
241 && (USB_DIR_IN & desc->bEndpointAddress)) {
242 /* DMA may be available */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400243 ep = find_ep(gadget, "ep2-bulk");
244 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200245 return ep;
246 }
247
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400248 } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200249 /* single buffering is enough; maybe 8 byte fifo is too */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400250 ep = find_ep(gadget, "ep3in-bulk");
251 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200252 return ep;
253
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400254 } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
255 ep = find_ep(gadget, "ep1-bulk");
256 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200257 return ep;
Li Junc93edbf2021-01-25 21:43:45 +0800258#ifndef CONFIG_SPL_BUILD
Marek Szyprowski16bece52015-03-03 17:32:11 +0100259 } else if (gadget_is_dwc3(gadget)) {
260 const char *name = NULL;
261 /*
262 * First try standard, common configuration: ep1in-bulk,
263 * ep2out-bulk, ep3in-int to match other udc drivers to avoid
264 * confusion in already deployed software (endpoint numbers
265 * hardcoded in userspace software/drivers)
266 */
267 if ((desc->bEndpointAddress & USB_DIR_IN) &&
268 type == USB_ENDPOINT_XFER_BULK)
269 name = "ep1in";
270 else if ((desc->bEndpointAddress & USB_DIR_IN) == 0 &&
271 type == USB_ENDPOINT_XFER_BULK)
272 name = "ep2out";
273 else if ((desc->bEndpointAddress & USB_DIR_IN) &&
274 type == USB_ENDPOINT_XFER_INT)
275 name = "ep3in";
276
277 if (name)
278 ep = find_ep(gadget, name);
279 if (ep && ep_matches(gadget, ep, desc))
280 return ep;
Li Junc93edbf2021-01-25 21:43:45 +0800281#endif
Remy Bohmer23cd1382009-07-29 18:18:43 +0200282 }
283
Vignesh Raghavendra77dcbdf2019-10-01 17:26:31 +0530284 if (gadget->ops->match_ep)
285 ep = gadget->ops->match_ep(gadget, desc, NULL);
286
Remy Bohmer23cd1382009-07-29 18:18:43 +0200287 /* Second, look at endpoints until an unclaimed one looks usable */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400288 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
289 if (ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200290 return ep;
291 }
292
293 /* Fail */
294 return NULL;
295}
296
297/**
298 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
299 * @gadget: device for which autoconfig state will be reset
300 *
301 * Use this for devices where one configuration may need to assign
302 * endpoint resources very differently from the next one. It clears
303 * state such as ep->driver_data and the record of assigned endpoints
304 * used by usb_ep_autoconfig().
305 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400306void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200307{
308 struct usb_ep *ep;
309
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400310 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200311 ep->driver_data = NULL;
312 }
313#ifdef MANY_ENDPOINTS
314 in_epnum = 0;
315#endif
316 epnum = 0;
317}