epautoconf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 = dectoul(&ep->name[2], NULL);
  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. if (gadget->ops->ep_conf)
  153. return gadget->ops->ep_conf(gadget, ep, desc);
  154. return 1;
  155. }
  156. static struct usb_ep *
  157. find_ep(struct usb_gadget *gadget, const char *name)
  158. {
  159. struct usb_ep *ep;
  160. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  161. if (0 == strcmp(ep->name, name))
  162. return ep;
  163. }
  164. return NULL;
  165. }
  166. /**
  167. * usb_ep_autoconfig - choose an endpoint matching the descriptor
  168. * @gadget: The device to which the endpoint must belong.
  169. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  170. * initialized. For periodic transfers, the maximum packet
  171. * size must also be initialized. This is modified on success.
  172. *
  173. * By choosing an endpoint to use with the specified descriptor, this
  174. * routine simplifies writing gadget drivers that work with multiple
  175. * USB device controllers. The endpoint would be passed later to
  176. * usb_ep_enable(), along with some descriptor.
  177. *
  178. * That second descriptor won't always be the same as the first one.
  179. * For example, isochronous endpoints can be autoconfigured for high
  180. * bandwidth, and then used in several lower bandwidth altsettings.
  181. * Also, high and full speed descriptors will be different.
  182. *
  183. * Be sure to examine and test the results of autoconfiguration on your
  184. * hardware. This code may not make the best choices about how to use the
  185. * USB controller, and it can't know all the restrictions that may apply.
  186. * Some combinations of driver and hardware won't be able to autoconfigure.
  187. *
  188. * On success, this returns an un-claimed usb_ep, and modifies the endpoint
  189. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  190. * is initialized as if the endpoint were used at full speed. To prevent
  191. * the endpoint from being returned by a later autoconfig call, claim it
  192. * by assigning ep->driver_data to some non-null value.
  193. *
  194. * On failure, this returns a null endpoint descriptor.
  195. */
  196. struct usb_ep *usb_ep_autoconfig(
  197. struct usb_gadget *gadget,
  198. struct usb_endpoint_descriptor *desc
  199. )
  200. {
  201. struct usb_ep *ep = NULL;
  202. u8 type;
  203. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  204. /* First, apply chip-specific "best usage" knowledge.
  205. * This might make a good usb_gadget_ops hook ...
  206. */
  207. if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
  208. /* ep-e, ep-f are PIO with only 64 byte fifos */
  209. ep = find_ep(gadget, "ep-e");
  210. if (ep && ep_matches(gadget, ep, desc))
  211. return ep;
  212. ep = find_ep(gadget, "ep-f");
  213. if (ep && ep_matches(gadget, ep, desc))
  214. return ep;
  215. } else if (gadget_is_goku(gadget)) {
  216. if (USB_ENDPOINT_XFER_INT == type) {
  217. /* single buffering is enough */
  218. ep = find_ep(gadget, "ep3-bulk");
  219. if (ep && ep_matches(gadget, ep, desc))
  220. return ep;
  221. } else if (USB_ENDPOINT_XFER_BULK == type
  222. && (USB_DIR_IN & desc->bEndpointAddress)) {
  223. /* DMA may be available */
  224. ep = find_ep(gadget, "ep2-bulk");
  225. if (ep && ep_matches(gadget, ep, desc))
  226. return ep;
  227. }
  228. } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
  229. /* single buffering is enough; maybe 8 byte fifo is too */
  230. ep = find_ep(gadget, "ep3in-bulk");
  231. if (ep && ep_matches(gadget, ep, desc))
  232. return ep;
  233. } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
  234. ep = find_ep(gadget, "ep1-bulk");
  235. if (ep && ep_matches(gadget, ep, desc))
  236. return ep;
  237. #ifndef CONFIG_SPL_BUILD
  238. } else if (gadget_is_dwc3(gadget)) {
  239. const char *name = NULL;
  240. /*
  241. * First try standard, common configuration: ep1in-bulk,
  242. * ep2out-bulk, ep3in-int to match other udc drivers to avoid
  243. * confusion in already deployed software (endpoint numbers
  244. * hardcoded in userspace software/drivers)
  245. */
  246. if ((desc->bEndpointAddress & USB_DIR_IN) &&
  247. type == USB_ENDPOINT_XFER_BULK)
  248. name = "ep1in";
  249. else if ((desc->bEndpointAddress & USB_DIR_IN) == 0 &&
  250. type == USB_ENDPOINT_XFER_BULK)
  251. name = "ep2out";
  252. else if ((desc->bEndpointAddress & USB_DIR_IN) &&
  253. type == USB_ENDPOINT_XFER_INT)
  254. name = "ep3in";
  255. if (name)
  256. ep = find_ep(gadget, name);
  257. if (ep && ep_matches(gadget, ep, desc))
  258. return ep;
  259. #endif
  260. }
  261. if (gadget->ops->match_ep)
  262. ep = gadget->ops->match_ep(gadget, desc, NULL);
  263. /* Second, look at endpoints until an unclaimed one looks usable */
  264. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  265. if (ep_matches(gadget, ep, desc))
  266. return ep;
  267. }
  268. /* Fail */
  269. return NULL;
  270. }
  271. /**
  272. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  273. * @gadget: device for which autoconfig state will be reset
  274. *
  275. * Use this for devices where one configuration may need to assign
  276. * endpoint resources very differently from the next one. It clears
  277. * state such as ep->driver_data and the record of assigned endpoints
  278. * used by usb_ep_autoconfig().
  279. */
  280. void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
  281. {
  282. struct usb_ep *ep;
  283. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  284. ep->driver_data = NULL;
  285. }
  286. #ifdef MANY_ENDPOINTS
  287. in_epnum = 0;
  288. #endif
  289. epnum = 0;
  290. }