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