2 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
4 * Copyright (C) 2004 David Brownell
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.
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.
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
20 * Ported to U-boot by: Thomas Smits <ts.smits@gmail.com> and
21 * Remy Bohmer <linux@bohmer.net>
25 #include <linux/usb/ch9.h>
26 #include <asm/errno.h>
27 #include <linux/usb/gadget.h>
28 #include "gadget_chips.h"
30 #define isdigit(c) ('0' <= (c) && (c) <= '9')
32 /* we must assign addresses for configurable endpoints (like net2280) */
33 static unsigned epnum;
35 /* #define MANY_ENDPOINTS */
37 /* more than 15 configurable endpoints */
38 static unsigned in_epnum;
43 * This should work with endpoints from controller drivers sharing the
44 * same endpoint naming convention. By example:
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
52 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
53 * Less common restrictions are implied by gadget_is_*().
55 * NOTE: each endpoint is unidirectional, as specified by its USB
56 * descriptor; and isn't specific to a configuration or altsetting.
58 static int ep_matches(
59 struct usb_gadget *gadget,
61 struct usb_endpoint_descriptor *desc
68 /* endpoint already claimed? */
69 if (NULL != ep->driver_data)
72 /* only support ep0 for portable CONTROL traffic */
73 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
74 if (USB_ENDPOINT_XFER_CONTROL == type)
77 /* some other naming convention */
78 if ('e' != ep->name[0])
81 /* type-restriction: "-iso", "-bulk", or "-int".
82 * direction-restriction: "in", "out".
84 if ('-' != ep->name[2]) {
85 tmp = strrchr(ep->name, '-');
88 case USB_ENDPOINT_XFER_INT:
89 /* bulk endpoints handle interrupt transfers,
90 * except the toggle-quirky iso-synch kind
92 if ('s' == tmp[2]) /* == "-iso" */
94 /* for now, avoid PXA "interrupt-in";
95 * it's documented as never using DATA1.
97 if (gadget_is_pxa(gadget)
101 case USB_ENDPOINT_XFER_BULK:
102 if ('b' != tmp[1]) /* != "-bulk" */
105 case USB_ENDPOINT_XFER_ISOC:
106 if ('s' != tmp[2]) /* != "-iso" */
110 tmp = ep->name + strlen(ep->name);
113 /* direction-restriction: "..in-..", "out-.." */
115 if (!isdigit(*tmp)) {
116 if (desc->bEndpointAddress & USB_DIR_IN) {
117 #ifdef CONFIG_AUTODLOADER
118 if(('n' != *tmp) || ('5' != ep->name[2]))
124 #ifdef CONFIG_AUTODLOADER
125 if(('t' != *tmp) || ('6' != ep->name[2]))
134 /* endpoint maxpacket size is an input parameter, except for bulk
135 * where it's an output parameter representing the full speed limit.
136 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
138 max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
140 case USB_ENDPOINT_XFER_INT:
141 /* INT: limit 64 bytes full speed, 1024 high speed */
142 if (!gadget->is_dualspeed && max > 64)
146 case USB_ENDPOINT_XFER_ISOC:
147 /* ISO: limit 1023 bytes full speed, 1024 high speed */
148 if (ep->maxpacket < max)
150 if (!gadget->is_dualspeed && max > 1023)
153 /* BOTH: "high bandwidth" works only at high speed */
154 if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) {
155 if (!gadget->is_dualspeed)
157 /* configure your hardware with enough buffering!! */
165 if (isdigit(ep->name[2])) {
166 u8 num = simple_strtoul(&ep->name[2], NULL, 10);
167 desc->bEndpointAddress |= num;
168 #ifdef MANY_ENDPOINTS
169 } else if (desc->bEndpointAddress & USB_DIR_IN) {
172 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
177 desc->bEndpointAddress |= epnum;
180 /* report (variable) full speed bulk maxpacket */
181 if (USB_ENDPOINT_XFER_BULK == type) {
182 int size = ep->maxpacket;
184 /* min() doesn't work on bitfields with gcc-3.5 */
187 desc->wMaxPacketSize = cpu_to_le16(size);
192 static struct usb_ep *
193 find_ep(struct usb_gadget *gadget, const char *name)
197 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
198 if (0 == strcmp(ep->name, name))
205 * usb_ep_autoconfig - choose an endpoint matching the descriptor
206 * @gadget: The device to which the endpoint must belong.
207 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
208 * initialized. For periodic transfers, the maximum packet
209 * size must also be initialized. This is modified on success.
211 * By choosing an endpoint to use with the specified descriptor, this
212 * routine simplifies writing gadget drivers that work with multiple
213 * USB device controllers. The endpoint would be passed later to
214 * usb_ep_enable(), along with some descriptor.
216 * That second descriptor won't always be the same as the first one.
217 * For example, isochronous endpoints can be autoconfigured for high
218 * bandwidth, and then used in several lower bandwidth altsettings.
219 * Also, high and full speed descriptors will be different.
221 * Be sure to examine and test the results of autoconfiguration on your
222 * hardware. This code may not make the best choices about how to use the
223 * USB controller, and it can't know all the restrictions that may apply.
224 * Some combinations of driver and hardware won't be able to autoconfigure.
226 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
227 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
228 * is initialized as if the endpoint were used at full speed. To prevent
229 * the endpoint from being returned by a later autoconfig call, claim it
230 * by assigning ep->driver_data to some non-null value.
232 * On failure, this returns a null endpoint descriptor.
234 struct usb_ep *usb_ep_autoconfig(
235 struct usb_gadget *gadget,
236 struct usb_endpoint_descriptor *desc
242 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
244 /* First, apply chip-specific "best usage" knowledge.
245 * This might make a good usb_gadget_ops hook ...
247 if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
248 /* ep-e, ep-f are PIO with only 64 byte fifos */
249 ep = find_ep(gadget, "ep-e");
250 if (ep && ep_matches(gadget, ep, desc))
252 ep = find_ep(gadget, "ep-f");
253 if (ep && ep_matches(gadget, ep, desc))
256 } else if (gadget_is_goku(gadget)) {
257 if (USB_ENDPOINT_XFER_INT == type) {
258 /* single buffering is enough */
259 ep = find_ep(gadget, "ep3-bulk");
260 if (ep && ep_matches(gadget, ep, desc))
262 } else if (USB_ENDPOINT_XFER_BULK == type
263 && (USB_DIR_IN & desc->bEndpointAddress)) {
264 /* DMA may be available */
265 ep = find_ep(gadget, "ep2-bulk");
266 if (ep && ep_matches(gadget, ep, desc))
270 } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
271 /* single buffering is enough; maybe 8 byte fifo is too */
272 ep = find_ep(gadget, "ep3in-bulk");
273 if (ep && ep_matches(gadget, ep, desc))
276 } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
277 ep = find_ep(gadget, "ep1-bulk");
278 if (ep && ep_matches(gadget, ep, desc))
282 /* Second, look at endpoints until an unclaimed one looks usable */
283 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
284 if (ep_matches(gadget, ep, desc))
293 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
294 * @gadget: device for which autoconfig state will be reset
296 * Use this for devices where one configuration may need to assign
297 * endpoint resources very differently from the next one. It clears
298 * state such as ep->driver_data and the record of assigned endpoints
299 * used by usb_ep_autoconfig().
301 void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
305 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
306 ep->driver_data = NULL;
308 #ifdef MANY_ENDPOINTS