Linux 2.6.26-rc5
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / usb / gadget / epautoconf.c
blob8bdad221fa91adc094c45ee1dea65cb0677c84e2
1 /*
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
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/types.h>
25 #include <linux/device.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
33 #include "gadget_chips.h"
36 /* we must assign addresses for configurable endpoints (like net2280) */
37 static __initdata unsigned epnum;
39 // #define MANY_ENDPOINTS
40 #ifdef MANY_ENDPOINTS
41 /* more than 15 configurable endpoints */
42 static __initdata unsigned in_epnum;
43 #endif
47 * This should work with endpoints from controller drivers sharing the
48 * same endpoint naming convention. By example:
50 * - ep1, ep2, ... address is fixed, not direction or type
51 * - ep1in, ep2out, ... address and direction are fixed, not type
52 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
53 * - ep1in-bulk, ep2out-iso, ... all three are fixed
54 * - ep-* ... no functionality restrictions
56 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
57 * Less common restrictions are implied by gadget_is_*().
59 * NOTE: each endpoint is unidirectional, as specified by its USB
60 * descriptor; and isn't specific to a configuration or altsetting.
62 static int __init
63 ep_matches (
64 struct usb_gadget *gadget,
65 struct usb_ep *ep,
66 struct usb_endpoint_descriptor *desc
69 u8 type;
70 const char *tmp;
71 u16 max;
73 /* endpoint already claimed? */
74 if (NULL != ep->driver_data)
75 return 0;
77 /* only support ep0 for portable CONTROL traffic */
78 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
79 if (USB_ENDPOINT_XFER_CONTROL == type)
80 return 0;
82 /* some other naming convention */
83 if ('e' != ep->name[0])
84 return 0;
86 /* type-restriction: "-iso", "-bulk", or "-int".
87 * direction-restriction: "in", "out".
89 if ('-' != ep->name[2]) {
90 tmp = strrchr (ep->name, '-');
91 if (tmp) {
92 switch (type) {
93 case USB_ENDPOINT_XFER_INT:
94 /* bulk endpoints handle interrupt transfers,
95 * except the toggle-quirky iso-synch kind
97 if ('s' == tmp[2]) // == "-iso"
98 return 0;
99 /* for now, avoid PXA "interrupt-in";
100 * it's documented as never using DATA1.
102 if (gadget_is_pxa (gadget)
103 && 'i' == tmp [1])
104 return 0;
105 break;
106 case USB_ENDPOINT_XFER_BULK:
107 if ('b' != tmp[1]) // != "-bulk"
108 return 0;
109 break;
110 case USB_ENDPOINT_XFER_ISOC:
111 if ('s' != tmp[2]) // != "-iso"
112 return 0;
114 } else {
115 tmp = ep->name + strlen (ep->name);
118 /* direction-restriction: "..in-..", "out-.." */
119 tmp--;
120 if (!isdigit (*tmp)) {
121 if (desc->bEndpointAddress & USB_DIR_IN) {
122 if ('n' != *tmp)
123 return 0;
124 } else {
125 if ('t' != *tmp)
126 return 0;
131 /* endpoint maxpacket size is an input parameter, except for bulk
132 * where it's an output parameter representing the full speed limit.
133 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
135 max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
136 switch (type) {
137 case USB_ENDPOINT_XFER_INT:
138 /* INT: limit 64 bytes full speed, 1024 high speed */
139 if (!gadget->is_dualspeed && max > 64)
140 return 0;
141 /* FALLTHROUGH */
143 case USB_ENDPOINT_XFER_ISOC:
144 /* ISO: limit 1023 bytes full speed, 1024 high speed */
145 if (ep->maxpacket < max)
146 return 0;
147 if (!gadget->is_dualspeed && max > 1023)
148 return 0;
150 /* BOTH: "high bandwidth" works only at high speed */
151 if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) {
152 if (!gadget->is_dualspeed)
153 return 0;
154 /* configure your hardware with enough buffering!! */
156 break;
159 /* MATCH!! */
161 /* report address */
162 if (isdigit (ep->name [2])) {
163 u8 num = simple_strtol (&ep->name [2], NULL, 10);
164 desc->bEndpointAddress |= num;
165 #ifdef MANY_ENDPOINTS
166 } else if (desc->bEndpointAddress & USB_DIR_IN) {
167 if (++in_epnum > 15)
168 return 0;
169 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
170 #endif
171 } else {
172 if (++epnum > 15)
173 return 0;
174 desc->bEndpointAddress |= epnum;
177 /* report (variable) full speed bulk maxpacket */
178 if (USB_ENDPOINT_XFER_BULK == type) {
179 int size = ep->maxpacket;
181 /* min() doesn't work on bitfields with gcc-3.5 */
182 if (size > 64)
183 size = 64;
184 desc->wMaxPacketSize = cpu_to_le16(size);
186 return 1;
189 static struct usb_ep * __init
190 find_ep (struct usb_gadget *gadget, const char *name)
192 struct usb_ep *ep;
194 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
195 if (0 == strcmp (ep->name, name))
196 return ep;
198 return NULL;
202 * usb_ep_autoconfig - choose an endpoint matching the descriptor
203 * @gadget: The device to which the endpoint must belong.
204 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
205 * initialized. For periodic transfers, the maximum packet
206 * size must also be initialized. This is modified on success.
208 * By choosing an endpoint to use with the specified descriptor, this
209 * routine simplifies writing gadget drivers that work with multiple
210 * USB device controllers. The endpoint would be passed later to
211 * usb_ep_enable(), along with some descriptor.
213 * That second descriptor won't always be the same as the first one.
214 * For example, isochronous endpoints can be autoconfigured for high
215 * bandwidth, and then used in several lower bandwidth altsettings.
216 * Also, high and full speed descriptors will be different.
218 * Be sure to examine and test the results of autoconfiguration on your
219 * hardware. This code may not make the best choices about how to use the
220 * USB controller, and it can't know all the restrictions that may apply.
221 * Some combinations of driver and hardware won't be able to autoconfigure.
223 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
224 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
225 * is initialized as if the endpoint were used at full speed. To prevent
226 * the endpoint from being returned by a later autoconfig call, claim it
227 * by assigning ep->driver_data to some non-null value.
229 * On failure, this returns a null endpoint descriptor.
231 struct usb_ep * __init usb_ep_autoconfig (
232 struct usb_gadget *gadget,
233 struct usb_endpoint_descriptor *desc
236 struct usb_ep *ep;
237 u8 type;
239 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
241 /* First, apply chip-specific "best usage" knowledge.
242 * This might make a good usb_gadget_ops hook ...
244 if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
245 /* ep-e, ep-f are PIO with only 64 byte fifos */
246 ep = find_ep (gadget, "ep-e");
247 if (ep && ep_matches (gadget, ep, desc))
248 return ep;
249 ep = find_ep (gadget, "ep-f");
250 if (ep && ep_matches (gadget, ep, desc))
251 return ep;
253 } else if (gadget_is_goku (gadget)) {
254 if (USB_ENDPOINT_XFER_INT == type) {
255 /* single buffering is enough */
256 ep = find_ep (gadget, "ep3-bulk");
257 if (ep && ep_matches (gadget, ep, desc))
258 return ep;
259 } else if (USB_ENDPOINT_XFER_BULK == type
260 && (USB_DIR_IN & desc->bEndpointAddress)) {
261 /* DMA may be available */
262 ep = find_ep (gadget, "ep2-bulk");
263 if (ep && ep_matches (gadget, ep, desc))
264 return ep;
267 } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) {
268 /* single buffering is enough; maybe 8 byte fifo is too */
269 ep = find_ep (gadget, "ep3in-bulk");
270 if (ep && ep_matches (gadget, ep, desc))
271 return ep;
273 } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) {
274 ep = find_ep (gadget, "ep1-bulk");
275 if (ep && ep_matches (gadget, ep, desc))
276 return ep;
279 /* Second, look at endpoints until an unclaimed one looks usable */
280 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
281 if (ep_matches (gadget, ep, desc))
282 return ep;
285 /* Fail */
286 return NULL;
290 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
291 * @gadget: device for which autoconfig state will be reset
293 * Use this for devices where one configuration may need to assign
294 * endpoint resources very differently from the next one. It clears
295 * state such as ep->driver_data and the record of assigned endpoints
296 * used by usb_ep_autoconfig().
298 void __init usb_ep_autoconfig_reset (struct usb_gadget *gadget)
300 struct usb_ep *ep;
302 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
303 ep->driver_data = NULL;
305 #ifdef MANY_ENDPOINTS
306 in_epnum = 0;
307 #endif
308 epnum = 0;