epautoconf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. if (gadget->ops->match_ep) {
  67. ep = gadget->ops->match_ep(gadget, desc, ep_comp);
  68. if (ep)
  69. goto found_ep;
  70. }
  71. /* Second, look at endpoints until an unclaimed one looks usable */
  72. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  73. if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
  74. goto found_ep;
  75. }
  76. /* Fail */
  77. return NULL;
  78. found_ep:
  79. /*
  80. * If the protocol driver hasn't yet decided on wMaxPacketSize
  81. * and wants to know the maximum possible, provide the info.
  82. */
  83. if (desc->wMaxPacketSize == 0)
  84. desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
  85. /* report address */
  86. desc->bEndpointAddress &= USB_DIR_IN;
  87. if (isdigit(ep->name[2])) {
  88. u8 num = simple_strtoul(&ep->name[2], NULL, 10);
  89. desc->bEndpointAddress |= num;
  90. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  91. if (++gadget->in_epnum > 15)
  92. return NULL;
  93. desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
  94. } else {
  95. if (++gadget->out_epnum > 15)
  96. return NULL;
  97. desc->bEndpointAddress |= gadget->out_epnum;
  98. }
  99. ep->address = desc->bEndpointAddress;
  100. ep->desc = NULL;
  101. ep->comp_desc = NULL;
  102. ep->claimed = true;
  103. return ep;
  104. }
  105. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss);
  106. /**
  107. * usb_ep_autoconfig() - choose an endpoint matching the
  108. * descriptor
  109. * @gadget: The device to which the endpoint must belong.
  110. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  111. * initialized. For periodic transfers, the maximum packet
  112. * size must also be initialized. This is modified on success.
  113. *
  114. * By choosing an endpoint to use with the specified descriptor, this
  115. * routine simplifies writing gadget drivers that work with multiple
  116. * USB device controllers. The endpoint would be passed later to
  117. * usb_ep_enable(), along with some descriptor.
  118. *
  119. * That second descriptor won't always be the same as the first one.
  120. * For example, isochronous endpoints can be autoconfigured for high
  121. * bandwidth, and then used in several lower bandwidth altsettings.
  122. * Also, high and full speed descriptors will be different.
  123. *
  124. * Be sure to examine and test the results of autoconfiguration on your
  125. * hardware. This code may not make the best choices about how to use the
  126. * USB controller, and it can't know all the restrictions that may apply.
  127. * Some combinations of driver and hardware won't be able to autoconfigure.
  128. *
  129. * On success, this returns an claimed usb_ep, and modifies the endpoint
  130. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  131. * is initialized as if the endpoint were used at full speed. Because of
  132. * that the users must consider adjusting the autoconfigured descriptor.
  133. * To prevent the endpoint from being returned by a later autoconfig call,
  134. * claims it by assigning ep->claimed to true.
  135. *
  136. * On failure, this returns a null endpoint descriptor.
  137. */
  138. struct usb_ep *usb_ep_autoconfig(
  139. struct usb_gadget *gadget,
  140. struct usb_endpoint_descriptor *desc
  141. )
  142. {
  143. struct usb_ep *ep;
  144. u8 type;
  145. ep = usb_ep_autoconfig_ss(gadget, desc, NULL);
  146. if (!ep)
  147. return NULL;
  148. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  149. /* report (variable) full speed bulk maxpacket */
  150. if (type == USB_ENDPOINT_XFER_BULK) {
  151. int size = ep->maxpacket_limit;
  152. /* min() doesn't work on bitfields with gcc-3.5 */
  153. if (size > 64)
  154. size = 64;
  155. desc->wMaxPacketSize = cpu_to_le16(size);
  156. }
  157. return ep;
  158. }
  159. EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
  160. /**
  161. * usb_ep_autoconfig_release - releases endpoint and set it to initial state
  162. * @ep: endpoint which should be released
  163. *
  164. * This function can be used during function bind for endpoints obtained
  165. * from usb_ep_autoconfig(). It unclaims endpoint claimed by
  166. * usb_ep_autoconfig() to make it available for other functions. Endpoint
  167. * which was released is no longer valid and shouldn't be used in
  168. * context of function which released it.
  169. */
  170. void usb_ep_autoconfig_release(struct usb_ep *ep)
  171. {
  172. ep->claimed = false;
  173. ep->driver_data = NULL;
  174. }
  175. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release);
  176. /**
  177. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  178. * @gadget: device for which autoconfig state will be reset
  179. *
  180. * Use this for devices where one configuration may need to assign
  181. * endpoint resources very differently from the next one. It clears
  182. * state such as ep->claimed and the record of assigned endpoints
  183. * used by usb_ep_autoconfig().
  184. */
  185. void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
  186. {
  187. struct usb_ep *ep;
  188. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  189. ep->claimed = false;
  190. ep->driver_data = NULL;
  191. }
  192. gadget->in_epnum = 0;
  193. gadget->out_epnum = 0;
  194. }
  195. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);