]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/usb/gadget/epautoconf.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / gadget / epautoconf.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
4 *
5 * Copyright (C) 2004 David Brownell
6 *
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.
11 */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/device.h>
17
18 #include <linux/ctype.h>
19 #include <linux/string.h>
20
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/gadget.h>
23
24 /**
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
31 * success.
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.
35 *
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.
40 *
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.
45 *
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.
50 *
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.
56 *
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.
65 *
66 * On failure, this returns a null endpoint descriptor.
67 */
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
72 )
73 {
74 struct usb_ep *ep;
75 u8 type;
76
77 type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
78
79 if (gadget->ops->match_ep) {
80 ep = gadget->ops->match_ep(gadget, desc, ep_comp);
81 if (ep)
82 goto found_ep;
83 }
84
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))
88 goto found_ep;
89 }
90
91 /* Fail */
92 return NULL;
93 found_ep:
94
95 /*
96 * If the protocol driver hasn't yet decided on wMaxPacketSize
97 * and wants to know the maximum possible, provide the info.
98 */
99 if (desc->wMaxPacketSize == 0)
100 desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
101
102 /* report address */
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)
109 return NULL;
110 desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
111 } else {
112 if (++gadget->out_epnum > 15)
113 return NULL;
114 desc->bEndpointAddress |= gadget->out_epnum;
115 }
116
117 /* report (variable) full speed bulk maxpacket */
118 if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) {
119 int size = ep->maxpacket_limit;
120
121 /* min() doesn't work on bitfields with gcc-3.5 */
122 if (size > 64)
123 size = 64;
124 desc->wMaxPacketSize = cpu_to_le16(size);
125 }
126
127 ep->address = desc->bEndpointAddress;
128 ep->desc = NULL;
129 ep->comp_desc = NULL;
130 ep->claimed = true;
131 return ep;
132 }
133 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
134
135 /**
136 * usb_ep_autoconfig() - choose an endpoint matching the
137 * descriptor
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.
142 *
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.
147 *
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.
152 *
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.
157 *
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.
163 *
164 * On failure, this returns a null endpoint descriptor.
165 */
166 struct usb_ep *usb_ep_autoconfig(
167 struct usb_gadget *gadget,
168 struct usb_endpoint_descriptor *desc
169 )
170 {
171 return usb_ep_autoconfig_ss(gadget, desc, NULL);
172 }
173 EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
174
175 /**
176 * usb_ep_autoconfig_release - releases endpoint and set it to initial state
177 * @ep: endpoint which should be released
178 *
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.
184 */
185 void usb_ep_autoconfig_release(struct usb_ep *ep)
186 {
187 ep->claimed = false;
188 ep->driver_data = NULL;
189 }
190 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release);
191
192 /**
193 * usb_ep_autoconfig_reset - reset endpoint autoconfig state
194 * @gadget: device for which autoconfig state will be reset
195 *
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().
200 */
201 void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
202 {
203 struct usb_ep *ep;
204
205 list_for_each_entry (ep, &gadget->ep_list, ep_list) {
206 ep->claimed = false;
207 ep->driver_data = NULL;
208 }
209 gadget->in_epnum = 0;
210 gadget->out_epnum = 0;
211 }
212 EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);