1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2004 David Brownell
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
11*4882a593Smuzhiyun * Remy Bohmer <linux@bohmer.net>
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <common.h>
15*4882a593Smuzhiyun #include <linux/usb/ch9.h>
16*4882a593Smuzhiyun #include <linux/errno.h>
17*4882a593Smuzhiyun #include <linux/usb/gadget.h>
18*4882a593Smuzhiyun #include <asm/unaligned.h>
19*4882a593Smuzhiyun #include "gadget_chips.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define isdigit(c) ('0' <= (c) && (c) <= '9')
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* we must assign addresses for configurable endpoints (like net2280) */
24*4882a593Smuzhiyun static unsigned epnum;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /* #define MANY_ENDPOINTS */
27*4882a593Smuzhiyun #ifdef MANY_ENDPOINTS
28*4882a593Smuzhiyun /* more than 15 configurable endpoints */
29*4882a593Smuzhiyun static unsigned in_epnum;
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun * This should work with endpoints from controller drivers sharing the
35*4882a593Smuzhiyun * same endpoint naming convention. By example:
36*4882a593Smuzhiyun *
37*4882a593Smuzhiyun * - ep1, ep2, ... address is fixed, not direction or type
38*4882a593Smuzhiyun * - ep1in, ep2out, ... address and direction are fixed, not type
39*4882a593Smuzhiyun * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
40*4882a593Smuzhiyun * - ep1in-bulk, ep2out-iso, ... all three are fixed
41*4882a593Smuzhiyun * - ep-* ... no functionality restrictions
42*4882a593Smuzhiyun *
43*4882a593Smuzhiyun * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
44*4882a593Smuzhiyun * Less common restrictions are implied by gadget_is_*().
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * NOTE: each endpoint is unidirectional, as specified by its USB
47*4882a593Smuzhiyun * descriptor; and isn't specific to a configuration or altsetting.
48*4882a593Smuzhiyun */
ep_matches(struct usb_gadget * gadget,struct usb_ep * ep,struct usb_endpoint_descriptor * desc)49*4882a593Smuzhiyun static int ep_matches(
50*4882a593Smuzhiyun struct usb_gadget *gadget,
51*4882a593Smuzhiyun struct usb_ep *ep,
52*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc
53*4882a593Smuzhiyun )
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun u8 type;
56*4882a593Smuzhiyun const char *tmp;
57*4882a593Smuzhiyun u16 max;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* endpoint already claimed? */
60*4882a593Smuzhiyun if (NULL != ep->driver_data)
61*4882a593Smuzhiyun return 0;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* only support ep0 for portable CONTROL traffic */
64*4882a593Smuzhiyun type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
65*4882a593Smuzhiyun if (USB_ENDPOINT_XFER_CONTROL == type)
66*4882a593Smuzhiyun return 0;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* some other naming convention */
69*4882a593Smuzhiyun if ('e' != ep->name[0])
70*4882a593Smuzhiyun return 0;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* type-restriction: "-iso", "-bulk", or "-int".
73*4882a593Smuzhiyun * direction-restriction: "in", "out".
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun if ('-' != ep->name[2]) {
76*4882a593Smuzhiyun tmp = strrchr(ep->name, '-');
77*4882a593Smuzhiyun if (tmp) {
78*4882a593Smuzhiyun switch (type) {
79*4882a593Smuzhiyun case USB_ENDPOINT_XFER_INT:
80*4882a593Smuzhiyun /* bulk endpoints handle interrupt transfers,
81*4882a593Smuzhiyun * except the toggle-quirky iso-synch kind
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun if ('s' == tmp[2]) /* == "-iso" */
84*4882a593Smuzhiyun return 0;
85*4882a593Smuzhiyun /* for now, avoid PXA "interrupt-in";
86*4882a593Smuzhiyun * it's documented as never using DATA1.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun if (gadget_is_pxa(gadget)
89*4882a593Smuzhiyun && 'i' == tmp[1])
90*4882a593Smuzhiyun return 0;
91*4882a593Smuzhiyun break;
92*4882a593Smuzhiyun case USB_ENDPOINT_XFER_BULK:
93*4882a593Smuzhiyun if ('b' != tmp[1]) /* != "-bulk" */
94*4882a593Smuzhiyun return 0;
95*4882a593Smuzhiyun break;
96*4882a593Smuzhiyun case USB_ENDPOINT_XFER_ISOC:
97*4882a593Smuzhiyun if ('s' != tmp[2]) /* != "-iso" */
98*4882a593Smuzhiyun return 0;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun } else {
101*4882a593Smuzhiyun tmp = ep->name + strlen(ep->name);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* direction-restriction: "..in-..", "out-.." */
105*4882a593Smuzhiyun tmp--;
106*4882a593Smuzhiyun if (!isdigit(*tmp)) {
107*4882a593Smuzhiyun if (desc->bEndpointAddress & USB_DIR_IN) {
108*4882a593Smuzhiyun if ('n' != *tmp)
109*4882a593Smuzhiyun return 0;
110*4882a593Smuzhiyun } else {
111*4882a593Smuzhiyun if ('t' != *tmp)
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* endpoint maxpacket size is an input parameter, except for bulk
118*4882a593Smuzhiyun * where it's an output parameter representing the full speed limit.
119*4882a593Smuzhiyun * the usb spec fixes high speed bulk maxpacket at 512 bytes.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun max = 0x7ff & le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
122*4882a593Smuzhiyun switch (type) {
123*4882a593Smuzhiyun case USB_ENDPOINT_XFER_INT:
124*4882a593Smuzhiyun /* INT: limit 64 bytes full speed, 1024 high speed */
125*4882a593Smuzhiyun if (!gadget->is_dualspeed && max > 64)
126*4882a593Smuzhiyun return 0;
127*4882a593Smuzhiyun /* FALLTHROUGH */
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun case USB_ENDPOINT_XFER_ISOC:
130*4882a593Smuzhiyun /* ISO: limit 1023 bytes full speed, 1024 high speed */
131*4882a593Smuzhiyun if (ep->maxpacket < max)
132*4882a593Smuzhiyun return 0;
133*4882a593Smuzhiyun if (!gadget->is_dualspeed && max > 1023)
134*4882a593Smuzhiyun return 0;
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* BOTH: "high bandwidth" works only at high speed */
137*4882a593Smuzhiyun if ((get_unaligned(&desc->wMaxPacketSize) &
138*4882a593Smuzhiyun __constant_cpu_to_le16(3<<11))) {
139*4882a593Smuzhiyun if (!gadget->is_dualspeed)
140*4882a593Smuzhiyun return 0;
141*4882a593Smuzhiyun /* configure your hardware with enough buffering!! */
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /* MATCH!! */
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* report address */
149*4882a593Smuzhiyun if (isdigit(ep->name[2])) {
150*4882a593Smuzhiyun u8 num = simple_strtoul(&ep->name[2], NULL, 10);
151*4882a593Smuzhiyun desc->bEndpointAddress |= num;
152*4882a593Smuzhiyun #ifdef MANY_ENDPOINTS
153*4882a593Smuzhiyun } else if (desc->bEndpointAddress & USB_DIR_IN) {
154*4882a593Smuzhiyun if (++in_epnum > 15)
155*4882a593Smuzhiyun return 0;
156*4882a593Smuzhiyun desc->bEndpointAddress = USB_DIR_IN | in_epnum;
157*4882a593Smuzhiyun #endif
158*4882a593Smuzhiyun } else {
159*4882a593Smuzhiyun if (++epnum > 15)
160*4882a593Smuzhiyun return 0;
161*4882a593Smuzhiyun desc->bEndpointAddress |= epnum;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /* report (variable) full speed bulk maxpacket */
165*4882a593Smuzhiyun if (USB_ENDPOINT_XFER_BULK == type) {
166*4882a593Smuzhiyun int size = ep->maxpacket;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* min() doesn't work on bitfields with gcc-3.5 */
169*4882a593Smuzhiyun if (size > 64)
170*4882a593Smuzhiyun size = 64;
171*4882a593Smuzhiyun put_unaligned(cpu_to_le16(size), &desc->wMaxPacketSize);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun return 1;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static struct usb_ep *
find_ep(struct usb_gadget * gadget,const char * name)177*4882a593Smuzhiyun find_ep(struct usb_gadget *gadget, const char *name)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun struct usb_ep *ep;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun list_for_each_entry(ep, &gadget->ep_list, ep_list) {
182*4882a593Smuzhiyun if (0 == strcmp(ep->name, name))
183*4882a593Smuzhiyun return ep;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun return NULL;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun * usb_ep_autoconfig - choose an endpoint matching the descriptor
190*4882a593Smuzhiyun * @gadget: The device to which the endpoint must belong.
191*4882a593Smuzhiyun * @desc: Endpoint descriptor, with endpoint direction and transfer mode
192*4882a593Smuzhiyun * initialized. For periodic transfers, the maximum packet
193*4882a593Smuzhiyun * size must also be initialized. This is modified on success.
194*4882a593Smuzhiyun *
195*4882a593Smuzhiyun * By choosing an endpoint to use with the specified descriptor, this
196*4882a593Smuzhiyun * routine simplifies writing gadget drivers that work with multiple
197*4882a593Smuzhiyun * USB device controllers. The endpoint would be passed later to
198*4882a593Smuzhiyun * usb_ep_enable(), along with some descriptor.
199*4882a593Smuzhiyun *
200*4882a593Smuzhiyun * That second descriptor won't always be the same as the first one.
201*4882a593Smuzhiyun * For example, isochronous endpoints can be autoconfigured for high
202*4882a593Smuzhiyun * bandwidth, and then used in several lower bandwidth altsettings.
203*4882a593Smuzhiyun * Also, high and full speed descriptors will be different.
204*4882a593Smuzhiyun *
205*4882a593Smuzhiyun * Be sure to examine and test the results of autoconfiguration on your
206*4882a593Smuzhiyun * hardware. This code may not make the best choices about how to use the
207*4882a593Smuzhiyun * USB controller, and it can't know all the restrictions that may apply.
208*4882a593Smuzhiyun * Some combinations of driver and hardware won't be able to autoconfigure.
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * On success, this returns an un-claimed usb_ep, and modifies the endpoint
211*4882a593Smuzhiyun * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
212*4882a593Smuzhiyun * is initialized as if the endpoint were used at full speed. To prevent
213*4882a593Smuzhiyun * the endpoint from being returned by a later autoconfig call, claim it
214*4882a593Smuzhiyun * by assigning ep->driver_data to some non-null value.
215*4882a593Smuzhiyun *
216*4882a593Smuzhiyun * On failure, this returns a null endpoint descriptor.
217*4882a593Smuzhiyun */
usb_ep_autoconfig(struct usb_gadget * gadget,struct usb_endpoint_descriptor * desc)218*4882a593Smuzhiyun struct usb_ep *usb_ep_autoconfig(
219*4882a593Smuzhiyun struct usb_gadget *gadget,
220*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc
221*4882a593Smuzhiyun )
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct usb_ep *ep = NULL;
224*4882a593Smuzhiyun u8 type;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /* First, apply chip-specific "best usage" knowledge.
229*4882a593Smuzhiyun * This might make a good usb_gadget_ops hook ...
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
232*4882a593Smuzhiyun /* ep-e, ep-f are PIO with only 64 byte fifos */
233*4882a593Smuzhiyun ep = find_ep(gadget, "ep-e");
234*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
235*4882a593Smuzhiyun return ep;
236*4882a593Smuzhiyun ep = find_ep(gadget, "ep-f");
237*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
238*4882a593Smuzhiyun return ep;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun } else if (gadget_is_goku(gadget)) {
241*4882a593Smuzhiyun if (USB_ENDPOINT_XFER_INT == type) {
242*4882a593Smuzhiyun /* single buffering is enough */
243*4882a593Smuzhiyun ep = find_ep(gadget, "ep3-bulk");
244*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
245*4882a593Smuzhiyun return ep;
246*4882a593Smuzhiyun } else if (USB_ENDPOINT_XFER_BULK == type
247*4882a593Smuzhiyun && (USB_DIR_IN & desc->bEndpointAddress)) {
248*4882a593Smuzhiyun /* DMA may be available */
249*4882a593Smuzhiyun ep = find_ep(gadget, "ep2-bulk");
250*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
251*4882a593Smuzhiyun return ep;
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
255*4882a593Smuzhiyun /* single buffering is enough; maybe 8 byte fifo is too */
256*4882a593Smuzhiyun ep = find_ep(gadget, "ep3in-bulk");
257*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
258*4882a593Smuzhiyun return ep;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
261*4882a593Smuzhiyun ep = find_ep(gadget, "ep1-bulk");
262*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
263*4882a593Smuzhiyun return ep;
264*4882a593Smuzhiyun } else if (gadget_is_dwc3(gadget)) {
265*4882a593Smuzhiyun const char *name = NULL;
266*4882a593Smuzhiyun /*
267*4882a593Smuzhiyun * First try standard, common configuration: ep1in-bulk,
268*4882a593Smuzhiyun * ep2out-bulk, ep3in-int to match other udc drivers to avoid
269*4882a593Smuzhiyun * confusion in already deployed software (endpoint numbers
270*4882a593Smuzhiyun * hardcoded in userspace software/drivers)
271*4882a593Smuzhiyun */
272*4882a593Smuzhiyun if ((desc->bEndpointAddress & USB_DIR_IN) &&
273*4882a593Smuzhiyun type == USB_ENDPOINT_XFER_BULK)
274*4882a593Smuzhiyun name = "ep1in";
275*4882a593Smuzhiyun else if ((desc->bEndpointAddress & USB_DIR_IN) == 0 &&
276*4882a593Smuzhiyun type == USB_ENDPOINT_XFER_BULK)
277*4882a593Smuzhiyun name = "ep2out";
278*4882a593Smuzhiyun else if ((desc->bEndpointAddress & USB_DIR_IN) &&
279*4882a593Smuzhiyun type == USB_ENDPOINT_XFER_INT)
280*4882a593Smuzhiyun name = "ep3in";
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun if (name)
283*4882a593Smuzhiyun ep = find_ep(gadget, name);
284*4882a593Smuzhiyun if (ep && ep_matches(gadget, ep, desc))
285*4882a593Smuzhiyun return ep;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun if (gadget->ops->match_ep)
289*4882a593Smuzhiyun ep = gadget->ops->match_ep(gadget, desc, NULL);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /* Second, look at endpoints until an unclaimed one looks usable */
292*4882a593Smuzhiyun list_for_each_entry(ep, &gadget->ep_list, ep_list) {
293*4882a593Smuzhiyun if (ep_matches(gadget, ep, desc))
294*4882a593Smuzhiyun return ep;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /* Fail */
298*4882a593Smuzhiyun return NULL;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun * usb_ep_autoconfig_reset - reset endpoint autoconfig state
303*4882a593Smuzhiyun * @gadget: device for which autoconfig state will be reset
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * Use this for devices where one configuration may need to assign
306*4882a593Smuzhiyun * endpoint resources very differently from the next one. It clears
307*4882a593Smuzhiyun * state such as ep->driver_data and the record of assigned endpoints
308*4882a593Smuzhiyun * used by usb_ep_autoconfig().
309*4882a593Smuzhiyun */
usb_ep_autoconfig_reset(struct usb_gadget * gadget)310*4882a593Smuzhiyun void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun struct usb_ep *ep;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun list_for_each_entry(ep, &gadget->ep_list, ep_list) {
315*4882a593Smuzhiyun ep->driver_data = NULL;
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun #ifdef MANY_ENDPOINTS
318*4882a593Smuzhiyun in_epnum = 0;
319*4882a593Smuzhiyun #endif
320*4882a593Smuzhiyun epnum = 0;
321*4882a593Smuzhiyun }
322