epautoconf.c 9.0 KB

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