epautoconf.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/types.h>
  10. #include <linux/device.h>
  11. #include <linux/ctype.h>
  12. #include <linux/string.h>
  13. #include <linux/usb/ch9.h>
  14. #include <linux/usb/gadget.h>
  15. /**
  16. * usb_ep_autoconfig_ss() - choose an endpoint matching the ep
  17. * descriptor and ep companion descriptor
  18. * @gadget: The device to which the endpoint must belong.
  19. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  20. * initialized. For periodic transfers, the maximum packet
  21. * size must also be initialized. This is modified on
  22. * success.
  23. * @ep_comp: Endpoint companion descriptor, with the required
  24. * number of streams. Will be modified when the chosen EP
  25. * supports a different number of streams.
  26. *
  27. * This routine replaces the usb_ep_autoconfig when needed
  28. * superspeed enhancments. If such enhancemnets are required,
  29. * the FD should call usb_ep_autoconfig_ss directly and provide
  30. * the additional ep_comp parameter.
  31. *
  32. * By choosing an endpoint to use with the specified descriptor,
  33. * this routine simplifies writing gadget drivers that work with
  34. * multiple USB device controllers. The endpoint would be
  35. * passed later to usb_ep_enable(), along with some descriptor.
  36. *
  37. * That second descriptor won't always be the same as the first one.
  38. * For example, isochronous endpoints can be autoconfigured for high
  39. * bandwidth, and then used in several lower bandwidth altsettings.
  40. * Also, high and full speed descriptors will be different.
  41. *
  42. * Be sure to examine and test the results of autoconfiguration
  43. * on your hardware. This code may not make the best choices
  44. * about how to use the USB controller, and it can't know all
  45. * the restrictions that may apply. Some combinations of driver
  46. * and hardware won't be able to autoconfigure.
  47. *
  48. * On success, this returns an claimed usb_ep, and modifies the endpoint
  49. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  50. * is initialized as if the endpoint were used at full speed and
  51. * the bmAttribute field in the ep companion descriptor is
  52. * updated with the assigned number of streams if it is
  53. * different from the original value. To prevent the endpoint
  54. * from being returned by a later autoconfig call, claims it by
  55. * assigning ep->claimed to true.
  56. *
  57. * On failure, this returns a null endpoint descriptor.
  58. */
  59. struct usb_ep *usb_ep_autoconfig_ss(
  60. struct usb_gadget *gadget,
  61. struct usb_endpoint_descriptor *desc,
  62. struct usb_ss_ep_comp_descriptor *ep_comp
  63. )
  64. {
  65. struct usb_ep *ep;
  66. u8 type;
  67. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  68. if (gadget->ops->match_ep) {
  69. ep = gadget->ops->match_ep(gadget, desc, ep_comp);
  70. if (ep)
  71. goto found_ep;
  72. }
  73. /* Second, look at endpoints until an unclaimed one looks usable */
  74. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  75. if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
  76. goto found_ep;
  77. }
  78. /* Fail */
  79. return NULL;
  80. found_ep:
  81. /*
  82. * If the protocol driver hasn't yet decided on wMaxPacketSize
  83. * and wants to know the maximum possible, provide the info.
  84. */
  85. if (desc->wMaxPacketSize == 0)
  86. desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
  87. /* report address */
  88. desc->bEndpointAddress &= USB_DIR_IN;
  89. if (isdigit(ep->name[2])) {
  90. u8 num = simple_strtoul(&ep->name[2], NULL, 10);
  91. desc->bEndpointAddress |= num;
  92. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  93. if (++gadget->in_epnum > 15)
  94. return NULL;
  95. desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
  96. } else {
  97. if (++gadget->out_epnum > 15)
  98. return NULL;
  99. desc->bEndpointAddress |= gadget->out_epnum;
  100. }
  101. /* report (variable) full speed bulk maxpacket */
  102. if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) {
  103. int size = ep->maxpacket_limit;
  104. /* min() doesn't work on bitfields with gcc-3.5 */
  105. if (size > 64)
  106. size = 64;
  107. desc->wMaxPacketSize = cpu_to_le16(size);
  108. }
  109. ep->address = desc->bEndpointAddress;
  110. ep->desc = NULL;
  111. ep->comp_desc = NULL;
  112. ep->claimed = true;
  113. return ep;
  114. }
  115. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
  116. /**
  117. * usb_ep_autoconfig() - choose an endpoint matching the
  118. * descriptor
  119. * @gadget: The device to which the endpoint must belong.
  120. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  121. * initialized. For periodic transfers, the maximum packet
  122. * size must also be initialized. This is modified on success.
  123. *
  124. * By choosing an endpoint to use with the specified descriptor, this
  125. * routine simplifies writing gadget drivers that work with multiple
  126. * USB device controllers. The endpoint would be passed later to
  127. * usb_ep_enable(), along with some descriptor.
  128. *
  129. * That second descriptor won't always be the same as the first one.
  130. * For example, isochronous endpoints can be autoconfigured for high
  131. * bandwidth, and then used in several lower bandwidth altsettings.
  132. * Also, high and full speed descriptors will be different.
  133. *
  134. * Be sure to examine and test the results of autoconfiguration on your
  135. * hardware. This code may not make the best choices about how to use the
  136. * USB controller, and it can't know all the restrictions that may apply.
  137. * Some combinations of driver and hardware won't be able to autoconfigure.
  138. *
  139. * On success, this returns an claimed usb_ep, and modifies the endpoint
  140. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  141. * is initialized as if the endpoint were used at full speed. To prevent
  142. * the endpoint from being returned by a later autoconfig call, claims it
  143. * by assigning ep->claimed to true.
  144. *
  145. * On failure, this returns a null endpoint descriptor.
  146. */
  147. struct usb_ep *usb_ep_autoconfig(
  148. struct usb_gadget *gadget,
  149. struct usb_endpoint_descriptor *desc
  150. )
  151. {
  152. return usb_ep_autoconfig_ss(gadget, desc, NULL);
  153. }
  154. EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
  155. /**
  156. * usb_ep_autoconfig_release - releases endpoint and set it to initial state
  157. * @ep: endpoint which should be released
  158. *
  159. * This function can be used during function bind for endpoints obtained
  160. * from usb_ep_autoconfig(). It unclaims endpoint claimed by
  161. * usb_ep_autoconfig() to make it available for other functions. Endpoint
  162. * which was released is no longer invalid and shouldn't be used in
  163. * context of function which released it.
  164. */
  165. void usb_ep_autoconfig_release(struct usb_ep *ep)
  166. {
  167. ep->claimed = false;
  168. ep->driver_data = NULL;
  169. }
  170. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release);
  171. /**
  172. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  173. * @gadget: device for which autoconfig state will be reset
  174. *
  175. * Use this for devices where one configuration may need to assign
  176. * endpoint resources very differently from the next one. It clears
  177. * state such as ep->claimed and the record of assigned endpoints
  178. * used by usb_ep_autoconfig().
  179. */
  180. void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
  181. {
  182. struct usb_ep *ep;
  183. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  184. ep->claimed = false;
  185. ep->driver_data = NULL;
  186. }
  187. gadget->in_epnum = 0;
  188. gadget->out_epnum = 0;
  189. }
  190. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);