1 // SPDX-License-Identifier: GPL-2.0+
3 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
5 * Copyright (C) 2004 David Brownell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/device.h>
18 #include <linux/ctype.h>
19 #include <linux/string.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
25 * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
26 * descriptor and ep companion descriptor
27 * @gadget: The device to which the endpoint must belong.
28 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
29 * initialized. For periodic transfers, the maximum packet
30 * size must also be initialized. This is modified on
32 * @ep_comp: Endpoint companion descriptor, with the required
33 * number of streams. Will be modified when the chosen EP
34 * supports a different number of streams.
36 * This routine replaces the usb_ep_autoconfig when needed
37 * superspeed enhancments. If such enhancemnets are required,
38 * the FD should call usb_ep_autoconfig_ss directly and provide
39 * the additional ep_comp parameter.
41 * By choosing an endpoint to use with the specified descriptor,
42 * this routine simplifies writing gadget drivers that work with
43 * multiple USB device controllers. The endpoint would be
44 * passed later to usb_ep_enable(), along with some descriptor.
46 * That second descriptor won't always be the same as the first one.
47 * For example, isochronous endpoints can be autoconfigured for high
48 * bandwidth, and then used in several lower bandwidth altsettings.
49 * Also, high and full speed descriptors will be different.
51 * Be sure to examine and test the results of autoconfiguration
52 * on your hardware. This code may not make the best choices
53 * about how to use the USB controller, and it can't know all
54 * the restrictions that may apply. Some combinations of driver
55 * and hardware won't be able to autoconfigure.
57 * On success, this returns an claimed usb_ep, and modifies the endpoint
58 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
59 * is initialized as if the endpoint were used at full speed and
60 * the bmAttribute field in the ep companion descriptor is
61 * updated with the assigned number of streams if it is
62 * different from the original value. To prevent the endpoint
63 * from being returned by a later autoconfig call, claims it by
64 * assigning ep->claimed to true.
66 * On failure, this returns a null endpoint descriptor.
68 struct usb_ep
*usb_ep_autoconfig_ss(
69 struct usb_gadget
*gadget
,
70 struct usb_endpoint_descriptor
*desc
,
71 struct usb_ss_ep_comp_descriptor
*ep_comp
77 type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
79 if (gadget
->ops
->match_ep
) {
80 ep
= gadget
->ops
->match_ep(gadget
, desc
, ep_comp
);
85 /* Second, look at endpoints until an unclaimed one looks usable */
86 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
87 if (usb_gadget_ep_match_desc(gadget
, ep
, desc
, ep_comp
))
96 * If the protocol driver hasn't yet decided on wMaxPacketSize
97 * and wants to know the maximum possible, provide the info.
99 if (desc
->wMaxPacketSize
== 0)
100 desc
->wMaxPacketSize
= cpu_to_le16(ep
->maxpacket_limit
);
103 desc
->bEndpointAddress
&= USB_DIR_IN
;
104 if (isdigit(ep
->name
[2])) {
105 u8 num
= simple_strtoul(&ep
->name
[2], NULL
, 10);
106 desc
->bEndpointAddress
|= num
;
107 } else if (desc
->bEndpointAddress
& USB_DIR_IN
) {
108 if (++gadget
->in_epnum
> 15)
110 desc
->bEndpointAddress
= USB_DIR_IN
| gadget
->in_epnum
;
112 if (++gadget
->out_epnum
> 15)
114 desc
->bEndpointAddress
|= gadget
->out_epnum
;
117 /* report (variable) full speed bulk maxpacket */
118 if ((type
== USB_ENDPOINT_XFER_BULK
) && !ep_comp
) {
119 int size
= ep
->maxpacket_limit
;
121 /* min() doesn't work on bitfields with gcc-3.5 */
124 desc
->wMaxPacketSize
= cpu_to_le16(size
);
127 ep
->address
= desc
->bEndpointAddress
;
129 ep
->comp_desc
= NULL
;
133 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss
);
136 * usb_ep_autoconfig() - choose an endpoint matching the
138 * @gadget: The device to which the endpoint must belong.
139 * @desc: Endpoint descriptor, with endpoint direction and transfer mode
140 * initialized. For periodic transfers, the maximum packet
141 * size must also be initialized. This is modified on success.
143 * By choosing an endpoint to use with the specified descriptor, this
144 * routine simplifies writing gadget drivers that work with multiple
145 * USB device controllers. The endpoint would be passed later to
146 * usb_ep_enable(), along with some descriptor.
148 * That second descriptor won't always be the same as the first one.
149 * For example, isochronous endpoints can be autoconfigured for high
150 * bandwidth, and then used in several lower bandwidth altsettings.
151 * Also, high and full speed descriptors will be different.
153 * Be sure to examine and test the results of autoconfiguration on your
154 * hardware. This code may not make the best choices about how to use the
155 * USB controller, and it can't know all the restrictions that may apply.
156 * Some combinations of driver and hardware won't be able to autoconfigure.
158 * On success, this returns an claimed usb_ep, and modifies the endpoint
159 * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
160 * is initialized as if the endpoint were used at full speed. To prevent
161 * the endpoint from being returned by a later autoconfig call, claims it
162 * by assigning ep->claimed to true.
164 * On failure, this returns a null endpoint descriptor.
166 struct usb_ep
*usb_ep_autoconfig(
167 struct usb_gadget
*gadget
,
168 struct usb_endpoint_descriptor
*desc
171 return usb_ep_autoconfig_ss(gadget
, desc
, NULL
);
173 EXPORT_SYMBOL_GPL(usb_ep_autoconfig
);
176 * usb_ep_autoconfig_release - releases endpoint and set it to initial state
177 * @ep: endpoint which should be released
179 * This function can be used during function bind for endpoints obtained
180 * from usb_ep_autoconfig(). It unclaims endpoint claimed by
181 * usb_ep_autoconfig() to make it available for other functions. Endpoint
182 * which was released is no longer invalid and shouldn't be used in
183 * context of function which released it.
185 void usb_ep_autoconfig_release(struct usb_ep
*ep
)
188 ep
->driver_data
= NULL
;
190 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release
);
193 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
194 * @gadget: device for which autoconfig state will be reset
196 * Use this for devices where one configuration may need to assign
197 * endpoint resources very differently from the next one. It clears
198 * state such as ep->claimed and the record of assigned endpoints
199 * used by usb_ep_autoconfig().
201 void usb_ep_autoconfig_reset (struct usb_gadget
*gadget
)
205 list_for_each_entry (ep
, &gadget
->ep_list
, ep_list
) {
207 ep
->driver_data
= NULL
;
209 gadget
->in_epnum
= 0;
210 gadget
->out_epnum
= 0;
212 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset
);