blob: b656c8b9f481f984dad8b291d0323a100d4d03f8 [file] [log] [blame]
Remy Bohmer23cd1382009-07-29 18:18:43 +02001/*
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>
Lukasz Majewskib9300532012-05-02 13:11:34 +020026#include <usbdescriptors.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020027#include <asm/errno.h>
28#include <linux/usb/gadget.h>
Tom Rinib2fb47f2011-12-15 08:40:51 -070029#include <asm/unaligned.h>
Remy Bohmer23cd1382009-07-29 18:18:43 +020030#include "gadget_chips.h"
31
32#define isdigit(c) ('0' <= (c) && (c) <= '9')
33
34/* we must assign addresses for configurable endpoints (like net2280) */
35static unsigned epnum;
36
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040037/* #define MANY_ENDPOINTS */
Remy Bohmer23cd1382009-07-29 18:18:43 +020038#ifdef MANY_ENDPOINTS
39/* more than 15 configurable endpoints */
40static unsigned in_epnum;
41#endif
42
43
44/*
45 * This should work with endpoints from controller drivers sharing the
46 * same endpoint naming convention. By example:
47 *
48 * - ep1, ep2, ... address is fixed, not direction or type
49 * - ep1in, ep2out, ... address and direction are fixed, not type
50 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
51 * - ep1in-bulk, ep2out-iso, ... all three are fixed
52 * - ep-* ... no functionality restrictions
53 *
54 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
55 * Less common restrictions are implied by gadget_is_*().
56 *
57 * NOTE: each endpoint is unidirectional, as specified by its USB
58 * descriptor; and isn't specific to a configuration or altsetting.
59 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040060static int ep_matches(
Remy Bohmer23cd1382009-07-29 18:18:43 +020061 struct usb_gadget *gadget,
62 struct usb_ep *ep,
63 struct usb_endpoint_descriptor *desc
64)
65{
66 u8 type;
67 const char *tmp;
68 u16 max;
69
70 /* endpoint already claimed? */
71 if (NULL != ep->driver_data)
72 return 0;
73
74 /* only support ep0 for portable CONTROL traffic */
75 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
76 if (USB_ENDPOINT_XFER_CONTROL == type)
77 return 0;
78
79 /* some other naming convention */
80 if ('e' != ep->name[0])
81 return 0;
82
83 /* type-restriction: "-iso", "-bulk", or "-int".
84 * direction-restriction: "in", "out".
85 */
86 if ('-' != ep->name[2]) {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040087 tmp = strrchr(ep->name, '-');
Remy Bohmer23cd1382009-07-29 18:18:43 +020088 if (tmp) {
89 switch (type) {
90 case USB_ENDPOINT_XFER_INT:
91 /* bulk endpoints handle interrupt transfers,
92 * except the toggle-quirky iso-synch kind
93 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040094 if ('s' == tmp[2]) /* == "-iso" */
Remy Bohmer23cd1382009-07-29 18:18:43 +020095 return 0;
96 /* for now, avoid PXA "interrupt-in";
97 * it's documented as never using DATA1.
98 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +040099 if (gadget_is_pxa(gadget)
100 && 'i' == tmp[1])
Remy Bohmer23cd1382009-07-29 18:18:43 +0200101 return 0;
102 break;
103 case USB_ENDPOINT_XFER_BULK:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400104 if ('b' != tmp[1]) /* != "-bulk" */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200105 return 0;
106 break;
107 case USB_ENDPOINT_XFER_ISOC:
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400108 if ('s' != tmp[2]) /* != "-iso" */
Remy Bohmer23cd1382009-07-29 18:18:43 +0200109 return 0;
110 }
111 } else {
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400112 tmp = ep->name + strlen(ep->name);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200113 }
114
115 /* direction-restriction: "..in-..", "out-.." */
116 tmp--;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400117 if (!isdigit(*tmp)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200118 if (desc->bEndpointAddress & USB_DIR_IN) {
119 if ('n' != *tmp)
120 return 0;
121 } else {
122 if ('t' != *tmp)
123 return 0;
124 }
125 }
126 }
127
128 /* endpoint maxpacket size is an input parameter, except for bulk
129 * where it's an output parameter representing the full speed limit.
130 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
131 */
Tom Rinib2fb47f2011-12-15 08:40:51 -0700132 max = 0x7ff & le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
Remy Bohmer23cd1382009-07-29 18:18:43 +0200133 switch (type) {
134 case USB_ENDPOINT_XFER_INT:
135 /* INT: limit 64 bytes full speed, 1024 high speed */
136 if (!gadget->is_dualspeed && max > 64)
137 return 0;
138 /* FALLTHROUGH */
139
140 case USB_ENDPOINT_XFER_ISOC:
141 /* ISO: limit 1023 bytes full speed, 1024 high speed */
142 if (ep->maxpacket < max)
143 return 0;
144 if (!gadget->is_dualspeed && max > 1023)
145 return 0;
146
147 /* BOTH: "high bandwidth" works only at high speed */
Tom Rinib2fb47f2011-12-15 08:40:51 -0700148 if ((get_unaligned(&desc->wMaxPacketSize) &
149 __constant_cpu_to_le16(3<<11))) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200150 if (!gadget->is_dualspeed)
151 return 0;
152 /* configure your hardware with enough buffering!! */
153 }
154 break;
155 }
156
157 /* MATCH!! */
158
159 /* report address */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400160 if (isdigit(ep->name[2])) {
161 u8 num = simple_strtoul(&ep->name[2], NULL, 10);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200162 desc->bEndpointAddress |= num;
163#ifdef MANY_ENDPOINTS
164 } else if (desc->bEndpointAddress & USB_DIR_IN) {
165 if (++in_epnum > 15)
166 return 0;
167 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
168#endif
169 } else {
170 if (++epnum > 15)
171 return 0;
172 desc->bEndpointAddress |= epnum;
173 }
174
175 /* report (variable) full speed bulk maxpacket */
176 if (USB_ENDPOINT_XFER_BULK == type) {
177 int size = ep->maxpacket;
178
179 /* min() doesn't work on bitfields with gcc-3.5 */
180 if (size > 64)
181 size = 64;
Tom Rinib2fb47f2011-12-15 08:40:51 -0700182 put_unaligned(cpu_to_le16(size), &desc->wMaxPacketSize);
Remy Bohmer23cd1382009-07-29 18:18:43 +0200183 }
184 return 1;
185}
186
187static struct usb_ep *
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400188find_ep(struct usb_gadget *gadget, const char *name)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200189{
190 struct usb_ep *ep;
191
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400192 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
193 if (0 == strcmp(ep->name, name))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200194 return ep;
195 }
196 return NULL;
197}
198
199/**
200 * usb_ep_autoconfig - choose an endpoint matching the descriptor
201 * @gadget: The device to which the endpoint must belong.
202 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
203 * initialized. For periodic transfers, the maximum packet
204 * size must also be initialized. This is modified on success.
205 *
206 * By choosing an endpoint to use with the specified descriptor, this
207 * routine simplifies writing gadget drivers that work with multiple
208 * USB device controllers. The endpoint would be passed later to
209 * usb_ep_enable(), along with some descriptor.
210 *
211 * That second descriptor won't always be the same as the first one.
212 * For example, isochronous endpoints can be autoconfigured for high
213 * bandwidth, and then used in several lower bandwidth altsettings.
214 * Also, high and full speed descriptors will be different.
215 *
216 * Be sure to examine and test the results of autoconfiguration on your
217 * hardware. This code may not make the best choices about how to use the
218 * USB controller, and it can't know all the restrictions that may apply.
219 * Some combinations of driver and hardware won't be able to autoconfigure.
220 *
221 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
222 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
223 * is initialized as if the endpoint were used at full speed. To prevent
224 * the endpoint from being returned by a later autoconfig call, claim it
225 * by assigning ep->driver_data to some non-null value.
226 *
227 * On failure, this returns a null endpoint descriptor.
228 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400229struct usb_ep *usb_ep_autoconfig(
Remy Bohmer23cd1382009-07-29 18:18:43 +0200230 struct usb_gadget *gadget,
231 struct usb_endpoint_descriptor *desc
232)
233{
234 struct usb_ep *ep;
235 u8 type;
236
237 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
238
239 /* First, apply chip-specific "best usage" knowledge.
240 * This might make a good usb_gadget_ops hook ...
241 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400242 if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200243 /* ep-e, ep-f are PIO with only 64 byte fifos */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400244 ep = find_ep(gadget, "ep-e");
245 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200246 return ep;
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400247 ep = find_ep(gadget, "ep-f");
248 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200249 return ep;
250
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400251 } else if (gadget_is_goku(gadget)) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200252 if (USB_ENDPOINT_XFER_INT == type) {
253 /* single buffering is enough */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400254 ep = find_ep(gadget, "ep3-bulk");
255 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200256 return ep;
257 } else if (USB_ENDPOINT_XFER_BULK == type
258 && (USB_DIR_IN & desc->bEndpointAddress)) {
259 /* DMA may be available */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400260 ep = find_ep(gadget, "ep2-bulk");
261 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200262 return ep;
263 }
264
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400265 } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200266 /* single buffering is enough; maybe 8 byte fifo is too */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400267 ep = find_ep(gadget, "ep3in-bulk");
268 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200269 return ep;
270
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400271 } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
272 ep = find_ep(gadget, "ep1-bulk");
273 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200274 return ep;
275 }
276
277 /* Second, look at endpoints until an unclaimed one looks usable */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400278 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
279 if (ep_matches(gadget, ep, desc))
Remy Bohmer23cd1382009-07-29 18:18:43 +0200280 return ep;
281 }
282
283 /* Fail */
284 return NULL;
285}
286
287/**
288 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
289 * @gadget: device for which autoconfig state will be reset
290 *
291 * Use this for devices where one configuration may need to assign
292 * endpoint resources very differently from the next one. It clears
293 * state such as ep->driver_data and the record of assigned endpoints
294 * used by usb_ep_autoconfig().
295 */
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400296void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
Remy Bohmer23cd1382009-07-29 18:18:43 +0200297{
298 struct usb_ep *ep;
299
Vitaly Kuzmichev6142e0a2010-09-13 18:37:11 +0400300 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
Remy Bohmer23cd1382009-07-29 18:18:43 +0200301 ep->driver_data = NULL;
302 }
303#ifdef MANY_ENDPOINTS
304 in_epnum = 0;
305#endif
306 epnum = 0;
307}