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 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
27 * descriptor and ep companion descriptor
28 * @gadget: The device to which the endpoint must belong.
29 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
30 * initialized. For periodic transfers, the maximum packet
31 * size must also be initialized. This is modified on
33 * @ep_comp: Endpoint companion descriptor, with the required
34 * number of streams. Will be modified when the chosen EP
35 * supports a different number of streams.
37 * This routine replaces the usb_ep_autoconfig when needed
38 * superspeed enhancments. If such enhancemnets are required,
39 * the FD should call usb_ep_autoconfig_ss directly and provide
40 * the additional ep_comp parameter.
42 * By choosing an endpoint to use with the specified descriptor,
43 * this routine simplifies writing gadget drivers that work with
44 * multiple USB device controllers. The endpoint would be
45 * passed later to usb_ep_enable(), along with some descriptor.
47 * That second descriptor won't always be the same as the first one.
48 * For example, isochronous endpoints can be autoconfigured for high
49 * bandwidth, and then used in several lower bandwidth altsettings.
50 * Also, high and full speed descriptors will be different.
52 * Be sure to examine and test the results of autoconfiguration
53 * on your hardware. This code may not make the best choices
54 * about how to use the USB controller, and it can't know all
55 * the restrictions that may apply. Some combinations of driver
56 * and hardware won't be able to autoconfigure.
58 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
59 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
60 * is initialized as if the endpoint were used at full speed and
61 * the bmAttribute field in the ep companion descriptor is
62 * updated with the assigned number of streams if it is
63 * different from the original value. To prevent the endpoint
64 * from being returned by a later autoconfig call, claim it by
65 * assigning ep->claimed to true.
67 * On failure, this returns a null endpoint descriptor.
69 struct usb_ep
*usb_ep_autoconfig_ss(
70 struct usb_gadget
*gadget
,
71 struct usb_endpoint_descriptor
*desc
,
72 struct usb_ss_ep_comp_descriptor
*ep_comp
78 type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
80 if (gadget
->ops
->match_ep
) {
81 ep
= gadget
->ops
->match_ep(gadget
, desc
, ep_comp
);
86 /* First, apply chip-specific "best usage" knowledge.
87 * This might make a good usb_gadget_ops hook ...
89 #ifdef CONFIG_BLACKFIN
90 if (gadget_is_musbhdrc(gadget
)) {
91 if ((USB_ENDPOINT_XFER_BULK
== type
) ||
92 (USB_ENDPOINT_XFER_ISOC
== type
)) {
93 if (USB_DIR_IN
& desc
->bEndpointAddress
)
94 ep
= gadget_find_ep_by_name(gadget
, "ep5in");
96 ep
= gadget_find_ep_by_name(gadget
, "ep6out");
97 } else if (USB_ENDPOINT_XFER_INT
== type
) {
98 if (USB_DIR_IN
& desc
->bEndpointAddress
)
99 ep
= gadget_find_ep_by_name(gadget
, "ep1in");
101 ep
= gadget_find_ep_by_name(gadget
, "ep2out");
104 if (ep
&& usb_gadget_ep_match_desc(gadget
, ep
, desc
, ep_comp
))
109 /* Second, look at endpoints until an unclaimed one looks usable */
110 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
111 if (usb_gadget_ep_match_desc(gadget
, ep
, desc
, ep_comp
))
120 * If the protocol driver hasn't yet decided on wMaxPacketSize
121 * and wants to know the maximum possible, provide the info.
123 if (desc
->wMaxPacketSize
== 0)
124 desc
->wMaxPacketSize
= cpu_to_le16(ep
->maxpacket_limit
);
127 desc
->bEndpointAddress
&= USB_DIR_IN
;
128 if (isdigit(ep
->name
[2])) {
129 u8 num
= simple_strtoul(&ep
->name
[2], NULL
, 10);
130 desc
->bEndpointAddress
|= num
;
131 } else if (desc
->bEndpointAddress
& USB_DIR_IN
) {
132 if (++gadget
->in_epnum
> 15)
134 desc
->bEndpointAddress
= USB_DIR_IN
| gadget
->in_epnum
;
136 if (++gadget
->out_epnum
> 15)
138 desc
->bEndpointAddress
|= gadget
->out_epnum
;
141 /* report (variable) full speed bulk maxpacket */
142 if ((type
== USB_ENDPOINT_XFER_BULK
) && !ep_comp
) {
143 int size
= ep
->maxpacket_limit
;
145 /* min() doesn't work on bitfields with gcc-3.5 */
148 desc
->wMaxPacketSize
= cpu_to_le16(size
);
151 ep
->address
= desc
->bEndpointAddress
;
153 ep
->comp_desc
= NULL
;
157 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss
);
160 * usb_ep_autoconfig() - choose an endpoint matching the
162 * @gadget: The device to which the endpoint must belong.
163 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
164 * initialized. For periodic transfers, the maximum packet
165 * size must also be initialized. This is modified on success.
167 * By choosing an endpoint to use with the specified descriptor, this
168 * routine simplifies writing gadget drivers that work with multiple
169 * USB device controllers. The endpoint would be passed later to
170 * usb_ep_enable(), along with some descriptor.
172 * That second descriptor won't always be the same as the first one.
173 * For example, isochronous endpoints can be autoconfigured for high
174 * bandwidth, and then used in several lower bandwidth altsettings.
175 * Also, high and full speed descriptors will be different.
177 * Be sure to examine and test the results of autoconfiguration on your
178 * hardware. This code may not make the best choices about how to use the
179 * USB controller, and it can't know all the restrictions that may apply.
180 * Some combinations of driver and hardware won't be able to autoconfigure.
182 * On success, this returns an un-claimed usb_ep, and modifies the endpoint
183 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
184 * is initialized as if the endpoint were used at full speed. To prevent
185 * the endpoint from being returned by a later autoconfig call, claim it
186 * by assigning ep->claimed to true.
188 * On failure, this returns a null endpoint descriptor.
190 struct usb_ep
*usb_ep_autoconfig(
191 struct usb_gadget
*gadget
,
192 struct usb_endpoint_descriptor
*desc
195 return usb_ep_autoconfig_ss(gadget
, desc
, NULL
);
197 EXPORT_SYMBOL_GPL(usb_ep_autoconfig
);
200 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
201 * @gadget: device for which autoconfig state will be reset
203 * Use this for devices where one configuration may need to assign
204 * endpoint resources very differently from the next one. It clears
205 * state such as ep->claimed and the record of assigned endpoints
206 * used by usb_ep_autoconfig().
208 void usb_ep_autoconfig_reset (struct usb_gadget
*gadget
)
212 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
215 gadget
->in_epnum
= 0;
216 gadget
->out_epnum
= 0;
218 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset
);