epautoconf.c 9.0 KB

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