hv_drv_UsbEpAutoConfig.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * @file hv_drv_UsbEpAutoConfig.c
  3. * @brief Endpoint autoconfiguration for usb gadget drivers.
  4. *
  5. * @author HiView SoC Software Team
  6. * @version 1.0.0
  7. * @date 2022-06-15
  8. */
  9. #include "hv_drv_UsbCh9.h"
  10. #include "hv_drv_UsbErrNo.h"
  11. #include "hv_drv_UsbGadget.h"
  12. #define isdigit(c) ('0' <= (c) && (c) <= '9')
  13. /* we must assign addresses for configurable endpoints (like net2280) */
  14. static unsigned epnum;
  15. #define MANY_ENDPOINTS
  16. #ifdef MANY_ENDPOINTS
  17. /* more than 15 configurable endpoints */
  18. static unsigned in_epnum;
  19. #endif
  20. /*
  21. * This should work with endpoints from controller drivers sharing the
  22. * same endpoint naming convention. By example:
  23. *
  24. * - ep1, ep2, ... address is fixed, not direction or type
  25. * - ep1in, ep2out, ... address and direction are fixed, not type
  26. * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
  27. * - ep1in-bulk, ep2out-iso, ... all three are fixed
  28. * - ep-* ... no functionality restrictions
  29. *
  30. * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
  31. * Less common restrictions are implied by gadget_is_*().
  32. *
  33. * NOTE: each endpoint is unidirectional, as specified by its USB
  34. * descriptor; and isn't specific to a configuration or altsetting.
  35. */
  36. static int ep_matches(
  37. struct usb_gadget *gadget,
  38. struct usb_ep *ep,
  39. struct usb_endpoint_descriptor *desc
  40. )
  41. {
  42. u8 type;
  43. const char *tmp;
  44. u16 max;
  45. /* endpoint already claimed? */
  46. if (NULL != ep->driver_data)
  47. return 0;
  48. /* only support ep0 for portable CONTROL traffic */
  49. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  50. if (USB_ENDPOINT_XFER_CONTROL == type)
  51. return 0;
  52. /* some other naming convention */
  53. if ('e' != ep->name[0])
  54. return 0;
  55. /* type-restriction: "-iso", "-bulk", or "-int".
  56. * direction-restriction: "in", "out".
  57. */
  58. if ('-' != ep->name[2]) {
  59. tmp = strrchr(ep->name, '-');
  60. if (tmp) {
  61. switch (type) {
  62. case USB_ENDPOINT_XFER_INT:
  63. /* bulk endpoints handle interrupt transfers,
  64. * except the toggle-quirky iso-synch kind
  65. */
  66. if ('s' == tmp[2]) /* == "-iso" */
  67. return 0;
  68. break;
  69. case USB_ENDPOINT_XFER_BULK:
  70. if ('b' != tmp[1]) /* != "-bulk" */
  71. return 0;
  72. break;
  73. case USB_ENDPOINT_XFER_ISOC:
  74. if ('s' != tmp[2]) /* != "-iso" */
  75. return 0;
  76. }
  77. } else {
  78. tmp = ep->name + strlen(ep->name);
  79. }
  80. /* direction-restriction: "..in-..", "out-.." */
  81. tmp--;
  82. if (!isdigit(*tmp)) {
  83. if (desc->bEndpointAddress & USB_DIR_IN) {
  84. if ('n' != *tmp)
  85. return 0;
  86. } else {
  87. if ('t' != *tmp)
  88. return 0;
  89. }
  90. }
  91. }
  92. /* endpoint maxpacket size is an input parameter, except for bulk
  93. * where it's an output parameter representing the full speed limit.
  94. * the usb spec fixes high speed bulk maxpacket at 512 bytes.
  95. */
  96. max = 0x7ff & usb_endpoint_maxp(desc);
  97. switch (type) {
  98. case USB_ENDPOINT_XFER_INT:
  99. /* INT: limit 64 bytes full speed, 1024 high speed */
  100. if (!gadget->is_dualspeed && max > 64)
  101. return 0;
  102. /* FALLTHROUGH */
  103. case USB_ENDPOINT_XFER_ISOC:
  104. /* ISO: limit 1023 bytes full speed, 1024 high speed */
  105. if (ep->maxpacket < max)
  106. return 0;
  107. if (!gadget->is_dualspeed && max > 1023)
  108. return 0;
  109. /* BOTH: "high bandwidth" works only at high speed */
  110. if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) {
  111. if (!gadget->is_dualspeed)
  112. return 0;
  113. /* configure your hardware with enough buffering!! */
  114. }
  115. break;
  116. }
  117. /* MATCH!! */
  118. /* report address */
  119. if (isdigit(ep->name[2])) {
  120. u8 num = simple_strtoul(&ep->name[2], NULL, 10);
  121. desc->bEndpointAddress |= num;
  122. #ifdef MANY_ENDPOINTS
  123. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  124. if (++in_epnum > 15)
  125. return 0;
  126. desc->bEndpointAddress = USB_DIR_IN | in_epnum;
  127. #endif
  128. } else {
  129. if (++epnum > 15)
  130. return 0;
  131. desc->bEndpointAddress |= epnum;
  132. }
  133. /* report (variable) full speed bulk maxpacket */
  134. if (USB_ENDPOINT_XFER_BULK == type) {
  135. int size = ep->maxpacket;
  136. /* min() doesn't work on bitfields with gcc-3.5 */
  137. if (size > 64)
  138. size = 64;
  139. desc->wMaxPacketSize = cpu_to_le16(size);
  140. }
  141. ep->address = desc->bEndpointAddress;
  142. return 1;
  143. }
  144. static struct usb_ep *
  145. find_ep(struct usb_gadget *gadget, const char *name)
  146. {
  147. struct usb_ep *ep;
  148. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  149. if (0 == strcmp(ep->name, name))
  150. return ep;
  151. }
  152. return NULL;
  153. }
  154. /**
  155. * usb_ep_autoconfig - choose an endpoint matching the descriptor
  156. * @gadget: The device to which the endpoint must belong.
  157. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  158. * initialized. For periodic transfers, the maximum packet
  159. * size must also be initialized. This is modified on success.
  160. *
  161. * By choosing an endpoint to use with the specified descriptor, this
  162. * routine simplifies writing gadget drivers that work with multiple
  163. * USB device controllers. The endpoint would be passed later to
  164. * usb_ep_enable(), along with some descriptor.
  165. *
  166. * That second descriptor won't always be the same as the first one.
  167. * For example, isochronous endpoints can be autoconfigured for high
  168. * bandwidth, and then used in several lower bandwidth altsettings.
  169. * Also, high and full speed descriptors will be different.
  170. *
  171. * Be sure to examine and test the results of autoconfiguration on your
  172. * hardware. This code may not make the best choices about how to use the
  173. * USB controller, and it can't know all the restrictions that may apply.
  174. * Some combinations of driver and hardware won't be able to autoconfigure.
  175. *
  176. * On success, this returns an un-claimed usb_ep, and modifies the endpoint
  177. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  178. * is initialized as if the endpoint were used at full speed. To prevent
  179. * the endpoint from being returned by a later autoconfig call, claim it
  180. * by assigning ep->driver_data to some non-null value.
  181. *
  182. * On failure, this returns a null endpoint descriptor.
  183. */
  184. struct usb_ep *usb_ep_autoconfig(
  185. struct usb_gadget *gadget,
  186. struct usb_endpoint_descriptor *desc
  187. )
  188. {
  189. struct usb_ep *ep = NULL;
  190. u8 type;
  191. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  192. /* Second, look at endpoints until an unclaimed one looks usable */
  193. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  194. if (ep_matches(gadget, ep, desc))
  195. return ep;
  196. }
  197. /* Fail */
  198. return NULL;
  199. }
  200. /**
  201. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  202. * @gadget: device for which autoconfig state will be reset
  203. *
  204. * Use this for devices where one configuration may need to assign
  205. * endpoint resources very differently from the next one. It clears
  206. * state such as ep->driver_data and the record of assigned endpoints
  207. * used by usb_ep_autoconfig().
  208. */
  209. void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
  210. {
  211. struct usb_ep *ep;
  212. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  213. ep->driver_data = NULL;
  214. }
  215. #ifdef MANY_ENDPOINTS
  216. in_epnum = 0;
  217. #endif
  218. epnum = 0;
  219. }