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.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/device.h>
17 #include <linux/ctype.h>
18 #include <linux/string.h>
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
23 #include "gadget_chips.h"
26 * This should work with endpoints from controller drivers sharing the
27 * same endpoint naming convention. By example:
29 * - ep1, ep2, ... address is fixed, not direction or type
30 * - ep1in, ep2out, ... address and direction are fixed, not type
31 * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
32 * - ep1in-bulk, ep2out-iso, ... all three are fixed
33 * - ep-* ... no functionality restrictions
35 * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
36 * Less common restrictions are implied by gadget_is_*().
38 * NOTE: each endpoint is unidirectional, as specified by its USB
39 * descriptor; and isn't specific to a configuration or altsetting.
43 struct usb_gadget
*gadget
,
45 struct usb_endpoint_descriptor
*desc
,
46 struct usb_ss_ep_comp_descriptor
*ep_comp
53 int num_req_streams
= 0;
55 /* endpoint already claimed? */
56 if (NULL
!= ep
->driver_data
)
59 /* only support ep0 for portable CONTROL traffic */
60 type
= usb_endpoint_type(desc
);
61 if (USB_ENDPOINT_XFER_CONTROL
== type
)
64 /* some other naming convention */
65 if ('e' != ep
->name
[0])
68 /* type-restriction: "-iso", "-bulk", or "-int".
69 * direction-restriction: "in", "out".
71 if ('-' != ep
->name
[2]) {
72 tmp
= strrchr (ep
->name
, '-');
75 case USB_ENDPOINT_XFER_INT
:
76 /* bulk endpoints handle interrupt transfers,
77 * except the toggle-quirky iso-synch kind
79 if ('s' == tmp
[2]) // == "-iso"
81 /* for now, avoid PXA "interrupt-in";
82 * it's documented as never using DATA1.
84 if (gadget_is_pxa (gadget
)
88 case USB_ENDPOINT_XFER_BULK
:
89 if ('b' != tmp
[1]) // != "-bulk"
92 case USB_ENDPOINT_XFER_ISOC
:
93 if ('s' != tmp
[2]) // != "-iso"
97 tmp
= ep
->name
+ strlen (ep
->name
);
100 /* direction-restriction: "..in-..", "out-.." */
102 if (!isdigit (*tmp
)) {
103 if (desc
->bEndpointAddress
& USB_DIR_IN
) {
114 * Get the number of required streams from the EP companion
115 * descriptor and see if the EP matches it
117 if (usb_endpoint_xfer_bulk(desc
)) {
118 if (ep_comp
&& gadget
->max_speed
>= USB_SPEED_SUPER
) {
119 num_req_streams
= ep_comp
->bmAttributes
& 0x1f;
120 if (num_req_streams
> ep
->max_streams
)
127 * If the protocol driver hasn't yet decided on wMaxPacketSize
128 * and wants to know the maximum possible, provide the info.
130 if (desc
->wMaxPacketSize
== 0)
131 desc
->wMaxPacketSize
= cpu_to_le16(ep
->maxpacket_limit
);
133 /* endpoint maxpacket size is an input parameter, except for bulk
134 * where it's an output parameter representing the full speed limit.
135 * the usb spec fixes high speed bulk maxpacket at 512 bytes.
137 max
= 0x7ff & usb_endpoint_maxp(desc
);
139 case USB_ENDPOINT_XFER_INT
:
140 /* INT: limit 64 bytes full speed, 1024 high/super speed */
141 if (!gadget_is_dualspeed(gadget
) && max
> 64)
145 case USB_ENDPOINT_XFER_ISOC
:
146 /* ISO: limit 1023 bytes full speed, 1024 high/super speed */
147 if (ep
->maxpacket_limit
< max
)
149 if (!gadget_is_dualspeed(gadget
) && max
> 1023)
152 /* BOTH: "high bandwidth" works only at high speed */
153 if ((desc
->wMaxPacketSize
& cpu_to_le16(3<<11))) {
154 if (!gadget_is_dualspeed(gadget
))
156 /* configure your hardware with enough buffering!! */
164 desc
->bEndpointAddress
&= USB_DIR_IN
;
165 if (isdigit (ep
->name
[2])) {
166 u8 num
= simple_strtoul (&ep
->name
[2], NULL
, 10);
167 desc
->bEndpointAddress
|= num
;
168 } else if (desc
->bEndpointAddress
& USB_DIR_IN
) {
169 if (++gadget
->in_epnum
> 15)
171 desc
->bEndpointAddress
= USB_DIR_IN
| gadget
->in_epnum
;
173 if (++gadget
->out_epnum
> 15)
175 desc
->bEndpointAddress
|= gadget
->out_epnum
;
178 /* report (variable) full speed bulk maxpacket */
179 if ((USB_ENDPOINT_XFER_BULK
== type
) && !ep_comp
) {
180 int size
= ep
->maxpacket_limit
;
182 /* min() doesn't work on bitfields with gcc-3.5 */
185 desc
->wMaxPacketSize
= cpu_to_le16(size
);
187 ep
->address
= desc
->bEndpointAddress
;
191 static struct usb_ep
*
192 find_ep (struct usb_gadget
*gadget
, const char *name
)
196 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
197 if (0 == strcmp (ep
->name
, name
))
204 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
205 * descriptor and ep companion 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
211 * @ep_comp: Endpoint companion descriptor, with the required
212 * number of streams. Will be modified when the chosen EP
213 * supports a different number of streams.
215 * This routine replaces the usb_ep_autoconfig when needed
216 * superspeed enhancments. If such enhancemnets are required,
217 * the FD should call usb_ep_autoconfig_ss directly and provide
218 * the additional ep_comp parameter.
220 * By choosing an endpoint to use with the specified descriptor,
221 * this routine simplifies writing gadget drivers that work with
222 * multiple USB device controllers. The endpoint would be
223 * passed later to usb_ep_enable(), along with some descriptor.
225 * That second descriptor won't always be the same as the first one.
226 * For example, isochronous endpoints can be autoconfigured for high
227 * bandwidth, and then used in several lower bandwidth altsettings.
228 * Also, high and full speed descriptors will be different.
230 * Be sure to examine and test the results of autoconfiguration
231 * on your hardware. This code may not make the best choices
232 * about how to use the USB controller, and it can't know all
233 * the restrictions that may apply. Some combinations of driver
234 * and hardware won't be able to autoconfigure.
236 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
237 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
238 * is initialized as if the endpoint were used at full speed and
239 * the bmAttribute field in the ep companion descriptor is
240 * updated with the assigned number of streams if it is
241 * different from the original value. To prevent the endpoint
242 * from being returned by a later autoconfig call, claim it by
243 * assigning ep->driver_data to some non-null value.
245 * On failure, this returns a null endpoint descriptor.
247 struct usb_ep
*usb_ep_autoconfig_ss(
248 struct usb_gadget
*gadget
,
249 struct usb_endpoint_descriptor
*desc
,
250 struct usb_ss_ep_comp_descriptor
*ep_comp
256 type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
258 /* First, apply chip-specific "best usage" knowledge.
259 * This might make a good usb_gadget_ops hook ...
261 if (gadget_is_net2280(gadget
)) {
264 if (type
== USB_ENDPOINT_XFER_INT
) {
265 /* ep-e, ep-f are PIO with only 64 byte fifos */
266 ep
= find_ep(gadget
, "ep-e");
267 if (ep
&& ep_matches(gadget
, ep
, desc
, ep_comp
))
269 ep
= find_ep(gadget
, "ep-f");
270 if (ep
&& ep_matches(gadget
, ep
, desc
, ep_comp
))
274 /* USB3380: use same address for usb and hardware endpoints */
275 snprintf(name
, sizeof(name
), "ep%d%s", usb_endpoint_num(desc
),
276 usb_endpoint_dir_in(desc
) ? "in" : "out");
277 ep
= find_ep(gadget
, name
);
278 if (ep
&& ep_matches(gadget
, ep
, desc
, ep_comp
))
280 } else if (gadget_is_goku (gadget
)) {
281 if (USB_ENDPOINT_XFER_INT
== type
) {
282 /* single buffering is enough */
283 ep
= find_ep(gadget
, "ep3-bulk");
284 if (ep
&& ep_matches(gadget
, ep
, desc
, ep_comp
))
286 } else if (USB_ENDPOINT_XFER_BULK
== type
287 && (USB_DIR_IN
& desc
->bEndpointAddress
)) {
288 /* DMA may be available */
289 ep
= find_ep(gadget
, "ep2-bulk");
290 if (ep
&& ep_matches(gadget
, ep
, desc
,
295 #ifdef CONFIG_BLACKFIN
296 } else if (gadget_is_musbhdrc(gadget
)) {
297 if ((USB_ENDPOINT_XFER_BULK
== type
) ||
298 (USB_ENDPOINT_XFER_ISOC
== type
)) {
299 if (USB_DIR_IN
& desc
->bEndpointAddress
)
300 ep
= find_ep (gadget
, "ep5in");
302 ep
= find_ep (gadget
, "ep6out");
303 } else if (USB_ENDPOINT_XFER_INT
== type
) {
304 if (USB_DIR_IN
& desc
->bEndpointAddress
)
305 ep
= find_ep(gadget
, "ep1in");
307 ep
= find_ep(gadget
, "ep2out");
310 if (ep
&& ep_matches(gadget
, ep
, desc
, ep_comp
))
315 /* Second, look at endpoints until an unclaimed one looks usable */
316 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
317 if (ep_matches(gadget
, ep
, desc
, ep_comp
))
325 ep
->comp_desc
= NULL
;
328 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss
);
331 * usb_ep_autoconfig() - choose an endpoint matching the
333 * @gadget: The device to which the endpoint must belong.
334 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
335 * initialized. For periodic transfers, the maximum packet
336 * size must also be initialized. This is modified on success.
338 * By choosing an endpoint to use with the specified descriptor, this
339 * routine simplifies writing gadget drivers that work with multiple
340 * USB device controllers. The endpoint would be passed later to
341 * usb_ep_enable(), along with some descriptor.
343 * That second descriptor won't always be the same as the first one.
344 * For example, isochronous endpoints can be autoconfigured for high
345 * bandwidth, and then used in several lower bandwidth altsettings.
346 * Also, high and full speed descriptors will be different.
348 * Be sure to examine and test the results of autoconfiguration on your
349 * hardware. This code may not make the best choices about how to use the
350 * USB controller, and it can't know all the restrictions that may apply.
351 * Some combinations of driver and hardware won't be able to autoconfigure.
353 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
354 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
355 * is initialized as if the endpoint were used at full speed. To prevent
356 * the endpoint from being returned by a later autoconfig call, claim it
357 * by assigning ep->driver_data to some non-null value.
359 * On failure, this returns a null endpoint descriptor.
361 struct usb_ep
*usb_ep_autoconfig(
362 struct usb_gadget
*gadget
,
363 struct usb_endpoint_descriptor
*desc
366 return usb_ep_autoconfig_ss(gadget
, desc
, NULL
);
368 EXPORT_SYMBOL_GPL(usb_ep_autoconfig
);
371 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
372 * @gadget: device for which autoconfig state will be reset
374 * Use this for devices where one configuration may need to assign
375 * endpoint resources very differently from the next one. It clears
376 * state such as ep->driver_data and the record of assigned endpoints
377 * used by usb_ep_autoconfig().
379 void usb_ep_autoconfig_reset (struct usb_gadget
*gadget
)
383 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
384 ep
->driver_data
= NULL
;
386 gadget
->in_epnum
= 0;
387 gadget
->out_epnum
= 0;
389 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset
);