2 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
4 * Copyright (C) 2004 David Brownell
6 * SPDX-License-Identifier: GPL-2.0+
8 * SPDX-License-Identifier: GPL-2.0+
10 * Ported to U-boot by: Thomas Smits <ts.smits@gmail.com> and
11 * Remy Bohmer <linux@bohmer.net>
15 #include <linux/usb/ch9.h>
16 #include <asm/errno.h>
17 #include <linux/usb/gadget.h>
18 #include <asm/unaligned.h>
19 #include "gadget_chips.h"
21 #define isdigit(c) ('0' <= (c) && (c) <= '9')
23 /* we must assign addresses for configurable endpoints (like net2280) */
24 static unsigned epnum
;
26 /* #define MANY_ENDPOINTS */
28 /* more than 15 configurable endpoints */
29 static unsigned in_epnum
;
34 * This should work with endpoints from controller drivers sharing the
35 * same endpoint naming convention. By example:
37 * - ep1, ep2, ... address is fixed, not direction or type
38 * - ep1in, ep2out, ... address and direction are fixed, not type
39 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
40 * - ep1in-bulk, ep2out-iso, ... all three are fixed
41 * - ep-* ... no functionality restrictions
43 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
44 * Less common restrictions are implied by gadget_is_*().
46 * NOTE: each endpoint is unidirectional, as specified by its USB
47 * descriptor; and isn't specific to a configuration or altsetting.
49 static int ep_matches(
50 struct usb_gadget
*gadget
,
52 struct usb_endpoint_descriptor
*desc
59 /* endpoint already claimed? */
60 if (NULL
!= ep
->driver_data
)
63 /* only support ep0 for portable CONTROL traffic */
64 type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
65 if (USB_ENDPOINT_XFER_CONTROL
== type
)
68 /* some other naming convention */
69 if ('e' != ep
->name
[0])
72 /* type-restriction: "-iso", "-bulk", or "-int".
73 * direction-restriction: "in", "out".
75 if ('-' != ep
->name
[2]) {
76 tmp
= strrchr(ep
->name
, '-');
79 case USB_ENDPOINT_XFER_INT
:
80 /* bulk endpoints handle interrupt transfers,
81 * except the toggle-quirky iso-synch kind
83 if ('s' == tmp
[2]) /* == "-iso" */
85 /* for now, avoid PXA "interrupt-in";
86 * it's documented as never using DATA1.
88 if (gadget_is_pxa(gadget
)
92 case USB_ENDPOINT_XFER_BULK
:
93 if ('b' != tmp
[1]) /* != "-bulk" */
96 case USB_ENDPOINT_XFER_ISOC
:
97 if ('s' != tmp
[2]) /* != "-iso" */
101 tmp
= ep
->name
+ strlen(ep
->name
);
104 /* direction-restriction: "..in-..", "out-.." */
106 if (!isdigit(*tmp
)) {
107 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
117 /* endpoint maxpacket size is an input parameter, except for bulk
118 * where it's an output parameter representing the full speed limit.
119 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
121 max
= 0x7ff & le16_to_cpu(get_unaligned(&desc
->wMaxPacketSize
));
123 case USB_ENDPOINT_XFER_INT
:
124 /* INT: limit 64 bytes full speed, 1024 high speed */
125 if (!gadget
->is_dualspeed
&& max
> 64)
129 case USB_ENDPOINT_XFER_ISOC
:
130 /* ISO: limit 1023 bytes full speed, 1024 high speed */
131 if (ep
->maxpacket
< max
)
133 if (!gadget
->is_dualspeed
&& max
> 1023)
136 /* BOTH: "high bandwidth" works only at high speed */
137 if ((get_unaligned(&desc
->wMaxPacketSize
) &
138 __constant_cpu_to_le16(3<<11))) {
139 if (!gadget
->is_dualspeed
)
141 /* configure your hardware with enough buffering!! */
149 if (isdigit(ep
->name
[2])) {
150 u8 num
= simple_strtoul(&ep
->name
[2], NULL
, 10);
151 desc
->bEndpointAddress
|= num
;
152 #ifdef MANY_ENDPOINTS
153 } else if (desc
->bEndpointAddress
& USB_DIR_IN
) {
156 desc
->bEndpointAddress
= USB_DIR_IN
| in_epnum
;
161 desc
->bEndpointAddress
|= epnum
;
164 /* report (variable) full speed bulk maxpacket */
165 if (USB_ENDPOINT_XFER_BULK
== type
) {
166 int size
= ep
->maxpacket
;
168 /* min() doesn't work on bitfields with gcc-3.5 */
171 put_unaligned(cpu_to_le16(size
), &desc
->wMaxPacketSize
);
176 static struct usb_ep
*
177 find_ep(struct usb_gadget
*gadget
, const char *name
)
181 list_for_each_entry(ep
, &gadget
->ep_list
, ep_list
) {
182 if (0 == strcmp(ep
->name
, name
))
189 * usb_ep_autoconfig - choose an endpoint matching the descriptor
190 * @gadget: The device to which the endpoint must belong.
191 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
192 * initialized. For periodic transfers, the maximum packet
193 * size must also be initialized. This is modified on success.
195 * By choosing an endpoint to use with the specified descriptor, this
196 * routine simplifies writing gadget drivers that work with multiple
197 * USB device controllers. The endpoint would be passed later to
198 * usb_ep_enable(), along with some descriptor.
200 * That second descriptor won't always be the same as the first one.
201 * For example, isochronous endpoints can be autoconfigured for high
202 * bandwidth, and then used in several lower bandwidth altsettings.
203 * Also, high and full speed descriptors will be different.
205 * Be sure to examine and test the results of autoconfiguration on your
206 * hardware. This code may not make the best choices about how to use the
207 * USB controller, and it can't know all the restrictions that may apply.
208 * Some combinations of driver and hardware won't be able to autoconfigure.
210 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
211 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
212 * is initialized as if the endpoint were used at full speed. To prevent
213 * the endpoint from being returned by a later autoconfig call, claim it
214 * by assigning ep->driver_data to some non-null value.
216 * On failure, this returns a null endpoint descriptor.
218 struct usb_ep
*usb_ep_autoconfig(
219 struct usb_gadget
*gadget
,
220 struct usb_endpoint_descriptor
*desc
226 type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
228 /* First, apply chip-specific "best usage" knowledge.
229 * This might make a good usb_gadget_ops hook ...
231 if (gadget_is_net2280(gadget
) && type
== USB_ENDPOINT_XFER_INT
) {
232 /* ep-e, ep-f are PIO with only 64 byte fifos */
233 ep
= find_ep(gadget
, "ep-e");
234 if (ep
&& ep_matches(gadget
, ep
, desc
))
236 ep
= find_ep(gadget
, "ep-f");
237 if (ep
&& ep_matches(gadget
, ep
, desc
))
240 } else if (gadget_is_goku(gadget
)) {
241 if (USB_ENDPOINT_XFER_INT
== type
) {
242 /* single buffering is enough */
243 ep
= find_ep(gadget
, "ep3-bulk");
244 if (ep
&& ep_matches(gadget
, ep
, desc
))
246 } else if (USB_ENDPOINT_XFER_BULK
== type
247 && (USB_DIR_IN
& desc
->bEndpointAddress
)) {
248 /* DMA may be available */
249 ep
= find_ep(gadget
, "ep2-bulk");
250 if (ep
&& ep_matches(gadget
, ep
, desc
))
254 } else if (gadget_is_sh(gadget
) && USB_ENDPOINT_XFER_INT
== type
) {
255 /* single buffering is enough; maybe 8 byte fifo is too */
256 ep
= find_ep(gadget
, "ep3in-bulk");
257 if (ep
&& ep_matches(gadget
, ep
, desc
))
260 } else if (gadget_is_mq11xx(gadget
) && USB_ENDPOINT_XFER_INT
== type
) {
261 ep
= find_ep(gadget
, "ep1-bulk");
262 if (ep
&& ep_matches(gadget
, ep
, desc
))
266 /* Second, look at endpoints until an unclaimed one looks usable */
267 list_for_each_entry(ep
, &gadget
->ep_list
, ep_list
) {
268 if (ep_matches(gadget
, ep
, desc
))
277 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
278 * @gadget: device for which autoconfig state will be reset
280 * Use this for devices where one configuration may need to assign
281 * endpoint resources very differently from the next one. It clears
282 * state such as ep->driver_data and the record of assigned endpoints
283 * used by usb_ep_autoconfig().
285 void usb_ep_autoconfig_reset(struct usb_gadget
*gadget
)
289 list_for_each_entry(ep
, &gadget
->ep_list
, ep_list
) {
290 ep
->driver_data
= NULL
;
292 #ifdef MANY_ENDPOINTS