blob: 7cf3c673ddd702641ce620205a0571a7df90bb94 [file] [log] [blame]
Remy Bohmerdf063442009-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>
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) */
33static unsigned epnum;
34
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040035/* #define MANY_ENDPOINTS */
Remy Bohmerdf063442009-07-29 18:18:43 +020036#ifdef MANY_ENDPOINTS
37/* more than 15 configurable endpoints */
38static 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 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040058static int ep_matches(
Remy Bohmerdf063442009-07-29 18:18:43 +020059 struct usb_gadget *gadget,
60 struct usb_ep *ep,
61 struct usb_endpoint_descriptor *desc
62)
63{
64 u8 type;
65 const char *tmp;
66 u16 max;
67
68 /* endpoint already claimed? */
69 if (NULL != ep->driver_data)
70 return 0;
71
72 /* only support ep0 for portable CONTROL traffic */
73 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
74 if (USB_ENDPOINT_XFER_CONTROL == type)
75 return 0;
76
77 /* some other naming convention */
78 if ('e' != ep->name[0])
79 return 0;
80
81 /* type-restriction: "-iso", "-bulk", or "-int".
82 * direction-restriction: "in", "out".
83 */
84 if ('-' != ep->name[2]) {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040085 tmp = strrchr(ep->name, '-');
Remy Bohmerdf063442009-07-29 18:18:43 +020086 if (tmp) {
87 switch (type) {
88 case USB_ENDPOINT_XFER_INT:
89 /* bulk endpoints handle interrupt transfers,
90 * except the toggle-quirky iso-synch kind
91 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040092 if ('s' == tmp[2]) /* == "-iso" */
Remy Bohmerdf063442009-07-29 18:18:43 +020093 return 0;
94 /* for now, avoid PXA "interrupt-in";
95 * it's documented as never using DATA1.
96 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +040097 if (gadget_is_pxa(gadget)
98 && 'i' == tmp[1])
Remy Bohmerdf063442009-07-29 18:18:43 +020099 return 0;
100 break;
101 case USB_ENDPOINT_XFER_BULK:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400102 if ('b' != tmp[1]) /* != "-bulk" */
Remy Bohmerdf063442009-07-29 18:18:43 +0200103 return 0;
104 break;
105 case USB_ENDPOINT_XFER_ISOC:
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400106 if ('s' != tmp[2]) /* != "-iso" */
Remy Bohmerdf063442009-07-29 18:18:43 +0200107 return 0;
108 }
109 } else {
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400110 tmp = ep->name + strlen(ep->name);
Remy Bohmerdf063442009-07-29 18:18:43 +0200111 }
112
113 /* direction-restriction: "..in-..", "out-.." */
114 tmp--;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400115 if (!isdigit(*tmp)) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200116 if (desc->bEndpointAddress & USB_DIR_IN) {
117 if ('n' != *tmp)
118 return 0;
119 } else {
120 if ('t' != *tmp)
121 return 0;
122 }
123 }
124 }
125
126 /* endpoint maxpacket size is an input parameter, except for bulk
127 * where it's an output parameter representing the full speed limit.
128 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
129 */
130 max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
131 switch (type) {
132 case USB_ENDPOINT_XFER_INT:
133 /* INT: limit 64 bytes full speed, 1024 high speed */
134 if (!gadget->is_dualspeed && max > 64)
135 return 0;
136 /* FALLTHROUGH */
137
138 case USB_ENDPOINT_XFER_ISOC:
139 /* ISO: limit 1023 bytes full speed, 1024 high speed */
140 if (ep->maxpacket < max)
141 return 0;
142 if (!gadget->is_dualspeed && max > 1023)
143 return 0;
144
145 /* BOTH: "high bandwidth" works only at high speed */
146 if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) {
147 if (!gadget->is_dualspeed)
148 return 0;
149 /* configure your hardware with enough buffering!! */
150 }
151 break;
152 }
153
154 /* MATCH!! */
155
156 /* report address */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400157 if (isdigit(ep->name[2])) {
158 u8 num = simple_strtoul(&ep->name[2], NULL, 10);
Remy Bohmerdf063442009-07-29 18:18:43 +0200159 desc->bEndpointAddress |= num;
160#ifdef MANY_ENDPOINTS
161 } else if (desc->bEndpointAddress & USB_DIR_IN) {
162 if (++in_epnum > 15)
163 return 0;
164 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
165#endif
166 } else {
167 if (++epnum > 15)
168 return 0;
169 desc->bEndpointAddress |= epnum;
170 }
171
172 /* report (variable) full speed bulk maxpacket */
173 if (USB_ENDPOINT_XFER_BULK == type) {
174 int size = ep->maxpacket;
175
176 /* min() doesn't work on bitfields with gcc-3.5 */
177 if (size > 64)
178 size = 64;
179 desc->wMaxPacketSize = cpu_to_le16(size);
180 }
181 return 1;
182}
183
184static struct usb_ep *
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400185find_ep(struct usb_gadget *gadget, const char *name)
Remy Bohmerdf063442009-07-29 18:18:43 +0200186{
187 struct usb_ep *ep;
188
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400189 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
190 if (0 == strcmp(ep->name, name))
Remy Bohmerdf063442009-07-29 18:18:43 +0200191 return ep;
192 }
193 return NULL;
194}
195
196/**
197 * usb_ep_autoconfig - choose an endpoint matching the descriptor
198 * @gadget: The device to which the endpoint must belong.
199 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
200 * initialized. For periodic transfers, the maximum packet
201 * size must also be initialized. This is modified on success.
202 *
203 * By choosing an endpoint to use with the specified descriptor, this
204 * routine simplifies writing gadget drivers that work with multiple
205 * USB device controllers. The endpoint would be passed later to
206 * usb_ep_enable(), along with some descriptor.
207 *
208 * That second descriptor won't always be the same as the first one.
209 * For example, isochronous endpoints can be autoconfigured for high
210 * bandwidth, and then used in several lower bandwidth altsettings.
211 * Also, high and full speed descriptors will be different.
212 *
213 * Be sure to examine and test the results of autoconfiguration on your
214 * hardware. This code may not make the best choices about how to use the
215 * USB controller, and it can't know all the restrictions that may apply.
216 * Some combinations of driver and hardware won't be able to autoconfigure.
217 *
218 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
219 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
220 * is initialized as if the endpoint were used at full speed. To prevent
221 * the endpoint from being returned by a later autoconfig call, claim it
222 * by assigning ep->driver_data to some non-null value.
223 *
224 * On failure, this returns a null endpoint descriptor.
225 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400226struct usb_ep *usb_ep_autoconfig(
Remy Bohmerdf063442009-07-29 18:18:43 +0200227 struct usb_gadget *gadget,
228 struct usb_endpoint_descriptor *desc
229)
230{
231 struct usb_ep *ep;
232 u8 type;
233
234 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
235
236 /* First, apply chip-specific "best usage" knowledge.
237 * This might make a good usb_gadget_ops hook ...
238 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400239 if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200240 /* ep-e, ep-f are PIO with only 64 byte fifos */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400241 ep = find_ep(gadget, "ep-e");
242 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200243 return ep;
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400244 ep = find_ep(gadget, "ep-f");
245 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200246 return ep;
247
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400248 } else if (gadget_is_goku(gadget)) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200249 if (USB_ENDPOINT_XFER_INT == type) {
250 /* single buffering is enough */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400251 ep = find_ep(gadget, "ep3-bulk");
252 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200253 return ep;
254 } else if (USB_ENDPOINT_XFER_BULK == type
255 && (USB_DIR_IN & desc->bEndpointAddress)) {
256 /* DMA may be available */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400257 ep = find_ep(gadget, "ep2-bulk");
258 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200259 return ep;
260 }
261
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400262 } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200263 /* single buffering is enough; maybe 8 byte fifo is too */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400264 ep = find_ep(gadget, "ep3in-bulk");
265 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200266 return ep;
267
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400268 } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
269 ep = find_ep(gadget, "ep1-bulk");
270 if (ep && ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200271 return ep;
272 }
273
274 /* Second, look at endpoints until an unclaimed one looks usable */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400275 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
276 if (ep_matches(gadget, ep, desc))
Remy Bohmerdf063442009-07-29 18:18:43 +0200277 return ep;
278 }
279
280 /* Fail */
281 return NULL;
282}
283
284/**
285 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
286 * @gadget: device for which autoconfig state will be reset
287 *
288 * Use this for devices where one configuration may need to assign
289 * endpoint resources very differently from the next one. It clears
290 * state such as ep->driver_data and the record of assigned endpoints
291 * used by usb_ep_autoconfig().
292 */
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400293void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
Remy Bohmerdf063442009-07-29 18:18:43 +0200294{
295 struct usb_ep *ep;
296
Vitaly Kuzmichev49ed8052010-09-13 18:37:11 +0400297 list_for_each_entry(ep, &gadget->ep_list, ep_list) {
Remy Bohmerdf063442009-07-29 18:18:43 +0200298 ep->driver_data = NULL;
299 }
300#ifdef MANY_ENDPOINTS
301 in_epnum = 0;
302#endif
303 epnum = 0;
304}
305