epautoconf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. struct usb_ep *usb_ep_autoconfig_ss_ex(
  117. struct usb_gadget *gadget,
  118. struct usb_endpoint_descriptor *desc,
  119. struct usb_ss_ep_comp_descriptor *ep_comp,
  120. uint8_t pre_ep_num
  121. )
  122. {
  123. struct usb_ep *ep;
  124. u8 type;
  125. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  126. if (gadget->ops->match_ep) {
  127. ep = gadget->ops->match_ep(gadget, desc, ep_comp);
  128. if (ep)
  129. goto found_ep;
  130. }
  131. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  132. if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp)) {
  133. if (isdigit(ep->name[2])) {
  134. u8 num = simple_strtoul(&ep->name[2], NULL, 10);
  135. if (num == (pre_ep_num & (~USB_DIR_IN))) {
  136. goto found_ep;
  137. }
  138. }
  139. }
  140. }
  141. printk("ep:%02x can not be assigned\n", pre_ep_num);
  142. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  143. if (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
  144. goto found_ep;
  145. }
  146. /* Fail */
  147. return NULL;
  148. found_ep:
  149. /*
  150. * If the protocol driver hasn't yet decided on wMaxPacketSize
  151. * and wants to know the maximum possible, provide the info.
  152. */
  153. if (desc->wMaxPacketSize == 0)
  154. desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket_limit);
  155. /* report address */
  156. desc->bEndpointAddress &= USB_DIR_IN;
  157. if (isdigit(ep->name[2])) {
  158. u8 num = simple_strtoul(&ep->name[2], NULL, 10);
  159. desc->bEndpointAddress |= num;
  160. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  161. if (++gadget->in_epnum > 15)
  162. return NULL;
  163. desc->bEndpointAddress = USB_DIR_IN | gadget->in_epnum;
  164. } else {
  165. if (++gadget->out_epnum > 15)
  166. return NULL;
  167. desc->bEndpointAddress |= gadget->out_epnum;
  168. }
  169. /* report (variable) full speed bulk maxpacket */
  170. if ((type == USB_ENDPOINT_XFER_BULK) && !ep_comp) {
  171. int size = ep->maxpacket_limit;
  172. /* min() doesn't work on bitfields with gcc-3.5 */
  173. if (size > 64)
  174. size = 64;
  175. desc->wMaxPacketSize = cpu_to_le16(size);
  176. }
  177. ep->address = desc->bEndpointAddress;
  178. ep->desc = NULL;
  179. ep->comp_desc = NULL;
  180. ep->claimed = true;
  181. return ep;
  182. }
  183. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ss_ex);
  184. /**
  185. * usb_ep_autoconfig() - choose an endpoint matching the
  186. * descriptor
  187. * @gadget: The device to which the endpoint must belong.
  188. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  189. * initialized. For periodic transfers, the maximum packet
  190. * size must also be initialized. This is modified on success.
  191. *
  192. * By choosing an endpoint to use with the specified descriptor, this
  193. * routine simplifies writing gadget drivers that work with multiple
  194. * USB device controllers. The endpoint would be passed later to
  195. * usb_ep_enable(), along with some descriptor.
  196. *
  197. * That second descriptor won't always be the same as the first one.
  198. * For example, isochronous endpoints can be autoconfigured for high
  199. * bandwidth, and then used in several lower bandwidth altsettings.
  200. * Also, high and full speed descriptors will be different.
  201. *
  202. * Be sure to examine and test the results of autoconfiguration on your
  203. * hardware. This code may not make the best choices about how to use the
  204. * USB controller, and it can't know all the restrictions that may apply.
  205. * Some combinations of driver and hardware won't be able to autoconfigure.
  206. *
  207. * On success, this returns an claimed usb_ep, and modifies the endpoint
  208. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  209. * is initialized as if the endpoint were used at full speed. To prevent
  210. * the endpoint from being returned by a later autoconfig call, claims it
  211. * by assigning ep->claimed to true.
  212. *
  213. * On failure, this returns a null endpoint descriptor.
  214. */
  215. struct usb_ep *usb_ep_autoconfig(
  216. struct usb_gadget *gadget,
  217. struct usb_endpoint_descriptor *desc
  218. )
  219. {
  220. return usb_ep_autoconfig_ss(gadget, desc, NULL);
  221. }
  222. EXPORT_SYMBOL_GPL(usb_ep_autoconfig);
  223. struct usb_ep *usb_ep_autoconfig_ex(
  224. struct usb_gadget *gadget,
  225. struct usb_endpoint_descriptor *desc,
  226. uint8_t pre_ep_num
  227. )
  228. {
  229. return usb_ep_autoconfig_ss_ex(gadget, desc, NULL, pre_ep_num);
  230. }
  231. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_ex);
  232. /**
  233. * usb_ep_autoconfig_release - releases endpoint and set it to initial state
  234. * @ep: endpoint which should be released
  235. *
  236. * This function can be used during function bind for endpoints obtained
  237. * from usb_ep_autoconfig(). It unclaims endpoint claimed by
  238. * usb_ep_autoconfig() to make it available for other functions. Endpoint
  239. * which was released is no longer invalid and shouldn't be used in
  240. * context of function which released it.
  241. */
  242. void usb_ep_autoconfig_release(struct usb_ep *ep)
  243. {
  244. ep->claimed = false;
  245. ep->driver_data = NULL;
  246. }
  247. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_release);
  248. /**
  249. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  250. * @gadget: device for which autoconfig state will be reset
  251. *
  252. * Use this for devices where one configuration may need to assign
  253. * endpoint resources very differently from the next one. It clears
  254. * state such as ep->claimed and the record of assigned endpoints
  255. * used by usb_ep_autoconfig().
  256. */
  257. void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
  258. {
  259. struct usb_ep *ep;
  260. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  261. ep->claimed = false;
  262. ep->driver_data = NULL;
  263. }
  264. gadget->in_epnum = 0;
  265. gadget->out_epnum = 0;
  266. }
  267. EXPORT_SYMBOL_GPL(usb_ep_autoconfig_reset);