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
41 /* more than 15 configurable endpoints */
42 static __initdata unsigned in_epnum;
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_*().
61 struct usb_gadget *gadget,
63 struct usb_endpoint_descriptor *desc
70 /* endpoint already claimed? */
71 if (0 != ep->driver_data)
74 /* only support ep0 for portable CONTROL traffic */
75 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
76 if (USB_ENDPOINT_XFER_CONTROL == type)
79 /* some other naming convention */
80 if ('e' != ep->name[0])
83 /* type-restriction: "-iso", "-bulk", or "-int".
84 * direction-restriction: "in", "out".
86 if ('-' != ep->name[2]) {
87 tmp = strrchr (ep->name, '-');
90 case USB_ENDPOINT_XFER_INT:
91 /* bulk endpoints handle interrupt transfers,
92 * except the toggle-quirky iso-synch kind
94 if ('s' == tmp[2]) // == "-iso"
96 /* for now, avoid PXA "interrupt-in";
97 * it's documented as never using DATA1.
99 if (gadget_is_pxa (gadget)
103 case USB_ENDPOINT_XFER_BULK:
104 if ('b' != tmp[1]) // != "-bulk"
107 case USB_ENDPOINT_XFER_ISOC:
108 if ('s' != tmp[2]) // != "-iso"
112 tmp = ep->name + strlen (ep->name);
115 /* direction-restriction: "..in-..", "out-.." */
117 if (!isdigit (*tmp)) {
118 if (desc->bEndpointAddress & USB_DIR_IN) {
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.
132 max = 0x7ff & le16_to_cpup (&desc->wMaxPacketSize);
134 case USB_ENDPOINT_XFER_INT:
135 /* INT: limit 64 bytes full speed, 1024 high speed */
136 if (!gadget->is_dualspeed && max > 64)
140 case USB_ENDPOINT_XFER_ISOC:
141 /* ISO: limit 1023 bytes full speed, 1024 high speed */
142 if (ep->maxpacket < max)
144 if (!gadget->is_dualspeed && max > 1023)
147 /* BOTH: "high bandwidth" works only at high speed */
148 if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) {
149 if (!gadget->is_dualspeed)
151 /* configure your hardware with enough buffering!! */
159 if (isdigit (ep->name [2])) {
160 u8 num = simple_strtol (&ep->name [2], NULL, 10);
161 desc->bEndpointAddress |= num;
162 #ifdef MANY_ENDPOINTS
163 } else if (desc->bEndpointAddress & USB_DIR_IN) {
166 desc->bEndpointAddress = USB_DIR_IN | in_epnum;
171 desc->bEndpointAddress |= epnum;
174 /* report (variable) full speed bulk maxpacket */
175 if (USB_ENDPOINT_XFER_BULK == type) {
176 int size = ep->maxpacket;
178 /* min() doesn't work on bitfields with gcc-3.5 */
181 desc->wMaxPacketSize = cpu_to_le16(size);
186 static struct usb_ep * __init
187 find_ep (struct usb_gadget *gadget, const char *name)
191 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
192 if (0 == strcmp (ep->name, name))
199 * usb_ep_autoconfig - choose an endpoint matching the descriptor
200 * @gadget: The device to which the endpoint must belong.
201 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
202 * initialized. For periodic transfers, the maximum packet
203 * size must also be initialized. This is modified on success.
205 * By choosing an endpoint to use with the specified descriptor, this
206 * routine simplifies writing gadget drivers that work with multiple
207 * USB device controllers. The endpoint would be passed later to
208 * usb_ep_enable(), along with some descriptor.
210 * That second descriptor won't always be the same as the first one.
211 * For example, isochronous endpoints can be autoconfigured for high
212 * bandwidth, and then used in several lower bandwidth altsettings.
213 * Also, high and full speed descriptors will be different.
215 * Be sure to examine and test the results of autoconfiguration on your
216 * hardware. This code may not make the best choices about how to use the
217 * USB controller, and it can't know all the restrictions that may apply.
218 * Some combinations of driver and hardware won't be able to autoconfigure.
220 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
221 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
222 * is initialized as if the endpoint were used at full speed. To prevent
223 * the endpoint from being returned by a later autoconfig call, claim it
224 * by assigning ep->driver_data to some non-null value.
226 * On failure, this returns a null endpoint descriptor.
228 struct usb_ep * __init usb_ep_autoconfig (
229 struct usb_gadget *gadget,
230 struct usb_endpoint_descriptor *desc
236 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
238 /* First, apply chip-specific "best usage" knowledge.
239 * This might make a good usb_gadget_ops hook ...
241 if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
242 /* ep-e, ep-f are PIO with only 64 byte fifos */
243 ep = find_ep (gadget, "ep-e");
244 if (ep && ep_matches (gadget, ep, desc))
246 ep = find_ep (gadget, "ep-f");
247 if (ep && ep_matches (gadget, ep, desc))
250 } else if (gadget_is_goku (gadget)) {
251 if (USB_ENDPOINT_XFER_INT == type) {
252 /* single buffering is enough */
253 ep = find_ep (gadget, "ep3-bulk");
254 if (ep && ep_matches (gadget, ep, desc))
256 } else if (USB_ENDPOINT_XFER_BULK == type
257 && (USB_DIR_IN & desc->bEndpointAddress)) {
258 /* DMA may be available */
259 ep = find_ep (gadget, "ep2-bulk");
260 if (ep && ep_matches (gadget, ep, desc))
264 } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) {
265 /* single buffering is enough; maybe 8 byte fifo is too */
266 ep = find_ep (gadget, "ep3in-bulk");
267 if (ep && ep_matches (gadget, ep, desc))
270 } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) {
271 ep = find_ep (gadget, "ep1-bulk");
272 if (ep && ep_matches (gadget, ep, desc))
276 /* Second, look at endpoints until an unclaimed one looks usable */
277 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
278 if (ep_matches (gadget, ep, desc))
287 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
288 * @gadget: device for which autoconfig state will be reset
290 * Use this for devices where one configuration may need to assign
291 * endpoint resources very differently from the next one. It clears
292 * state such as ep->driver_data and the record of assigned endpoints
293 * used by usb_ep_autoconfig().
295 void __init usb_ep_autoconfig_reset (struct usb_gadget *gadget)
299 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
300 ep->driver_data = NULL;
302 #ifdef MANY_ENDPOINTS