Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 2 | /* |
| 3 | * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers |
| 4 | * |
| 5 | * Copyright (C) 2004 David Brownell |
| 6 | * |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 7 | * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 8 | * Remy Bohmer <linux@bohmer.net> |
| 9 | */ |
| 10 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 11 | #include <linux/usb/ch9.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 13 | #include <linux/usb/gadget.h> |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 14 | #include <asm/unaligned.h> |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 15 | |
| 16 | #define isdigit(c) ('0' <= (c) && (c) <= '9') |
| 17 | |
| 18 | /* we must assign addresses for configurable endpoints (like net2280) */ |
| 19 | static unsigned epnum; |
| 20 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 21 | /* #define MANY_ENDPOINTS */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 22 | #ifdef MANY_ENDPOINTS |
| 23 | /* more than 15 configurable endpoints */ |
| 24 | static unsigned in_epnum; |
| 25 | #endif |
| 26 | |
| 27 | |
| 28 | /* |
| 29 | * This should work with endpoints from controller drivers sharing the |
| 30 | * same endpoint naming convention. By example: |
| 31 | * |
| 32 | * - ep1, ep2, ... address is fixed, not direction or type |
| 33 | * - ep1in, ep2out, ... address and direction are fixed, not type |
| 34 | * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction |
| 35 | * - ep1in-bulk, ep2out-iso, ... all three are fixed |
| 36 | * - ep-* ... no functionality restrictions |
| 37 | * |
| 38 | * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal. |
| 39 | * Less common restrictions are implied by gadget_is_*(). |
| 40 | * |
| 41 | * NOTE: each endpoint is unidirectional, as specified by its USB |
| 42 | * descriptor; and isn't specific to a configuration or altsetting. |
| 43 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 44 | static int ep_matches( |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 45 | struct usb_gadget *gadget, |
| 46 | struct usb_ep *ep, |
| 47 | struct usb_endpoint_descriptor *desc |
| 48 | ) |
| 49 | { |
| 50 | u8 type; |
| 51 | const char *tmp; |
| 52 | u16 max; |
| 53 | |
| 54 | /* endpoint already claimed? */ |
| 55 | if (NULL != ep->driver_data) |
| 56 | return 0; |
| 57 | |
| 58 | /* only support ep0 for portable CONTROL traffic */ |
| 59 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
| 60 | if (USB_ENDPOINT_XFER_CONTROL == type) |
| 61 | return 0; |
| 62 | |
| 63 | /* some other naming convention */ |
| 64 | if ('e' != ep->name[0]) |
| 65 | return 0; |
| 66 | |
| 67 | /* type-restriction: "-iso", "-bulk", or "-int". |
| 68 | * direction-restriction: "in", "out". |
| 69 | */ |
| 70 | if ('-' != ep->name[2]) { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 71 | tmp = strrchr(ep->name, '-'); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 72 | if (tmp) { |
| 73 | switch (type) { |
| 74 | case USB_ENDPOINT_XFER_INT: |
| 75 | /* bulk endpoints handle interrupt transfers, |
| 76 | * except the toggle-quirky iso-synch kind |
| 77 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 78 | if ('s' == tmp[2]) /* == "-iso" */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 79 | return 0; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 80 | break; |
| 81 | case USB_ENDPOINT_XFER_BULK: |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 82 | if ('b' != tmp[1]) /* != "-bulk" */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 83 | return 0; |
| 84 | break; |
| 85 | case USB_ENDPOINT_XFER_ISOC: |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 86 | if ('s' != tmp[2]) /* != "-iso" */ |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | } else { |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 90 | tmp = ep->name + strlen(ep->name); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /* direction-restriction: "..in-..", "out-.." */ |
| 94 | tmp--; |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 95 | if (!isdigit(*tmp)) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 96 | if (desc->bEndpointAddress & USB_DIR_IN) { |
| 97 | if ('n' != *tmp) |
| 98 | return 0; |
| 99 | } else { |
| 100 | if ('t' != *tmp) |
| 101 | return 0; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* endpoint maxpacket size is an input parameter, except for bulk |
| 107 | * where it's an output parameter representing the full speed limit. |
| 108 | * the usb spec fixes high speed bulk maxpacket at 512 bytes. |
| 109 | */ |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 110 | max = 0x7ff & le16_to_cpu(get_unaligned(&desc->wMaxPacketSize)); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 111 | switch (type) { |
| 112 | case USB_ENDPOINT_XFER_INT: |
| 113 | /* INT: limit 64 bytes full speed, 1024 high speed */ |
| 114 | if (!gadget->is_dualspeed && max > 64) |
| 115 | return 0; |
| 116 | /* FALLTHROUGH */ |
| 117 | |
| 118 | case USB_ENDPOINT_XFER_ISOC: |
| 119 | /* ISO: limit 1023 bytes full speed, 1024 high speed */ |
| 120 | if (ep->maxpacket < max) |
| 121 | return 0; |
| 122 | if (!gadget->is_dualspeed && max > 1023) |
| 123 | return 0; |
| 124 | |
| 125 | /* BOTH: "high bandwidth" works only at high speed */ |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 126 | if ((get_unaligned(&desc->wMaxPacketSize) & |
| 127 | __constant_cpu_to_le16(3<<11))) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 128 | if (!gadget->is_dualspeed) |
| 129 | return 0; |
| 130 | /* configure your hardware with enough buffering!! */ |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | |
| 135 | /* MATCH!! */ |
| 136 | |
| 137 | /* report address */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 138 | if (isdigit(ep->name[2])) { |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 139 | u8 num = dectoul(&ep->name[2], NULL); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 140 | desc->bEndpointAddress |= num; |
| 141 | #ifdef MANY_ENDPOINTS |
| 142 | } else if (desc->bEndpointAddress & USB_DIR_IN) { |
| 143 | if (++in_epnum > 15) |
| 144 | return 0; |
| 145 | desc->bEndpointAddress = USB_DIR_IN | in_epnum; |
| 146 | #endif |
| 147 | } else { |
| 148 | if (++epnum > 15) |
| 149 | return 0; |
| 150 | desc->bEndpointAddress |= epnum; |
| 151 | } |
| 152 | |
| 153 | /* report (variable) full speed bulk maxpacket */ |
| 154 | if (USB_ENDPOINT_XFER_BULK == type) { |
| 155 | int size = ep->maxpacket; |
| 156 | |
| 157 | /* min() doesn't work on bitfields with gcc-3.5 */ |
| 158 | if (size > 64) |
| 159 | size = 64; |
Tom Rini | 7342452 | 2011-12-15 08:40:51 -0700 | [diff] [blame] | 160 | put_unaligned(cpu_to_le16(size), &desc->wMaxPacketSize); |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 161 | } |
Ye Li | d22fb8a | 2021-01-25 21:43:44 +0800 | [diff] [blame] | 162 | |
| 163 | if (gadget->ops->ep_conf) |
| 164 | return gadget->ops->ep_conf(gadget, ep, desc); |
| 165 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | static struct usb_ep * |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 170 | find_ep(struct usb_gadget *gadget, const char *name) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 171 | { |
| 172 | struct usb_ep *ep; |
| 173 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 174 | list_for_each_entry(ep, &gadget->ep_list, ep_list) { |
| 175 | if (0 == strcmp(ep->name, name)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 176 | return ep; |
| 177 | } |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * usb_ep_autoconfig - choose an endpoint matching the descriptor |
| 183 | * @gadget: The device to which the endpoint must belong. |
| 184 | * @desc: Endpoint descriptor, with endpoint direction and transfer mode |
| 185 | * initialized. For periodic transfers, the maximum packet |
| 186 | * size must also be initialized. This is modified on success. |
| 187 | * |
| 188 | * By choosing an endpoint to use with the specified descriptor, this |
| 189 | * routine simplifies writing gadget drivers that work with multiple |
| 190 | * USB device controllers. The endpoint would be passed later to |
| 191 | * usb_ep_enable(), along with some descriptor. |
| 192 | * |
| 193 | * That second descriptor won't always be the same as the first one. |
| 194 | * For example, isochronous endpoints can be autoconfigured for high |
| 195 | * bandwidth, and then used in several lower bandwidth altsettings. |
| 196 | * Also, high and full speed descriptors will be different. |
| 197 | * |
| 198 | * Be sure to examine and test the results of autoconfiguration on your |
| 199 | * hardware. This code may not make the best choices about how to use the |
| 200 | * USB controller, and it can't know all the restrictions that may apply. |
| 201 | * Some combinations of driver and hardware won't be able to autoconfigure. |
| 202 | * |
| 203 | * On success, this returns an un-claimed usb_ep, and modifies the endpoint |
| 204 | * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value |
| 205 | * is initialized as if the endpoint were used at full speed. To prevent |
| 206 | * the endpoint from being returned by a later autoconfig call, claim it |
| 207 | * by assigning ep->driver_data to some non-null value. |
| 208 | * |
| 209 | * On failure, this returns a null endpoint descriptor. |
| 210 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 211 | struct usb_ep *usb_ep_autoconfig( |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 212 | struct usb_gadget *gadget, |
| 213 | struct usb_endpoint_descriptor *desc |
| 214 | ) |
| 215 | { |
Marek Szyprowski | 06c7bde | 2015-03-03 17:32:11 +0100 | [diff] [blame] | 216 | struct usb_ep *ep = NULL; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 217 | u8 type; |
| 218 | |
| 219 | type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK; |
| 220 | |
| 221 | /* First, apply chip-specific "best usage" knowledge. |
| 222 | * This might make a good usb_gadget_ops hook ... |
| 223 | */ |
Marek Vasut | bd15f32 | 2024-06-09 23:32:17 +0200 | [diff] [blame] | 224 | if (!IS_ENABLED(CONFIG_SPL_BUILD) && |
| 225 | IS_ENABLED(CONFIG_USB_DWC3_GADGET) && |
| 226 | !strcmp("dwc3-gadget", gadget->name)) { |
Marek Szyprowski | 06c7bde | 2015-03-03 17:32:11 +0100 | [diff] [blame] | 227 | const char *name = NULL; |
| 228 | /* |
| 229 | * First try standard, common configuration: ep1in-bulk, |
| 230 | * ep2out-bulk, ep3in-int to match other udc drivers to avoid |
| 231 | * confusion in already deployed software (endpoint numbers |
| 232 | * hardcoded in userspace software/drivers) |
| 233 | */ |
| 234 | if ((desc->bEndpointAddress & USB_DIR_IN) && |
| 235 | type == USB_ENDPOINT_XFER_BULK) |
| 236 | name = "ep1in"; |
| 237 | else if ((desc->bEndpointAddress & USB_DIR_IN) == 0 && |
| 238 | type == USB_ENDPOINT_XFER_BULK) |
| 239 | name = "ep2out"; |
| 240 | else if ((desc->bEndpointAddress & USB_DIR_IN) && |
| 241 | type == USB_ENDPOINT_XFER_INT) |
| 242 | name = "ep3in"; |
| 243 | |
| 244 | if (name) |
| 245 | ep = find_ep(gadget, name); |
| 246 | if (ep && ep_matches(gadget, ep, desc)) |
| 247 | return ep; |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Marek Vasut | 0ee8a14 | 2024-06-09 23:32:18 +0200 | [diff] [blame^] | 250 | if (gadget->ops->match_ep) { |
Vignesh Raghavendra | cb39a96 | 2019-10-01 17:26:31 +0530 | [diff] [blame] | 251 | ep = gadget->ops->match_ep(gadget, desc, NULL); |
Marek Vasut | 0ee8a14 | 2024-06-09 23:32:18 +0200 | [diff] [blame^] | 252 | if (ep && ep_matches(gadget, ep, desc)) |
| 253 | return ep; |
| 254 | } |
Vignesh Raghavendra | cb39a96 | 2019-10-01 17:26:31 +0530 | [diff] [blame] | 255 | |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 256 | /* Second, look at endpoints until an unclaimed one looks usable */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 257 | list_for_each_entry(ep, &gadget->ep_list, ep_list) { |
| 258 | if (ep_matches(gadget, ep, desc)) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 259 | return ep; |
| 260 | } |
| 261 | |
| 262 | /* Fail */ |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * usb_ep_autoconfig_reset - reset endpoint autoconfig state |
| 268 | * @gadget: device for which autoconfig state will be reset |
| 269 | * |
| 270 | * Use this for devices where one configuration may need to assign |
| 271 | * endpoint resources very differently from the next one. It clears |
| 272 | * state such as ep->driver_data and the record of assigned endpoints |
| 273 | * used by usb_ep_autoconfig(). |
| 274 | */ |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 275 | void usb_ep_autoconfig_reset(struct usb_gadget *gadget) |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 276 | { |
| 277 | struct usb_ep *ep; |
| 278 | |
Vitaly Kuzmichev | 49ed805 | 2010-09-13 18:37:11 +0400 | [diff] [blame] | 279 | list_for_each_entry(ep, &gadget->ep_list, ep_list) { |
Remy Bohmer | df06344 | 2009-07-29 18:18:43 +0200 | [diff] [blame] | 280 | ep->driver_data = NULL; |
| 281 | } |
| 282 | #ifdef MANY_ENDPOINTS |
| 283 | in_epnum = 0; |
| 284 | #endif |
| 285 | epnum = 0; |
| 286 | } |