epautoconf.c 7.2 KB

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