epautoconf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Ported to U-boot by: Thomas Smits <ts.smits@gmail.com> and
  21. * Remy Bohmer <linux@bohmer.net>
  22. */
  23. #include <common.h>
  24. #include <linux/usb/ch9.h>
  25. #include <asm/errno.h>
  26. #include <linux/usb/gadget.h>
  27. #include "gadget_chips.h"
  28. #define isdigit(c) ('0' <= (c) && (c) <= '9')
  29. /* we must assign addresses for configurable endpoints (like net2280) */
  30. static unsigned epnum;
  31. /* #define MANY_ENDPOINTS */
  32. #ifdef MANY_ENDPOINTS
  33. /* more than 15 configurable endpoints */
  34. static unsigned in_epnum;
  35. #endif
  36. /*
  37. * This should work with endpoints from controller drivers sharing the
  38. * same endpoint naming convention. By example:
  39. *
  40. * - ep1, ep2, ... address is fixed, not direction or type
  41. * - ep1in, ep2out, ... address and direction are fixed, not type
  42. * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
  43. * - ep1in-bulk, ep2out-iso, ... all three are fixed
  44. * - ep-* ... no functionality restrictions
  45. *
  46. * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
  47. * Less common restrictions are implied by gadget_is_*().
  48. *
  49. * NOTE: each endpoint is unidirectional, as specified by its USB
  50. * descriptor; and isn't specific to a configuration or altsetting.
  51. */
  52. static int ep_matches(
  53. struct usb_gadget *gadget,
  54. struct usb_ep *ep,
  55. struct usb_endpoint_descriptor *desc
  56. )
  57. {
  58. u8 type;
  59. const char *tmp;
  60. u16 max;
  61. /* endpoint already claimed? */
  62. if (NULL != ep->driver_data)
  63. return 0;
  64. /* only support ep0 for portable CONTROL traffic */
  65. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  66. if (USB_ENDPOINT_XFER_CONTROL == type)
  67. return 0;
  68. /* some other naming convention */
  69. if ('e' != ep->name[0])
  70. return 0;
  71. /* type-restriction: "-iso", "-bulk", or "-int".
  72. * direction-restriction: "in", "out".
  73. */
  74. if ('-' != ep->name[2]) {
  75. tmp = strrchr(ep->name, '-');
  76. if (tmp) {
  77. switch (type) {
  78. case USB_ENDPOINT_XFER_INT:
  79. /* bulk endpoints handle interrupt transfers,
  80. * except the toggle-quirky iso-synch kind
  81. */
  82. if ('s' == tmp[2]) /* == "-iso" */
  83. return 0;
  84. /* for now, avoid PXA "interrupt-in";
  85. * it's documented as never using DATA1.
  86. */
  87. if (gadget_is_pxa(gadget)
  88. && 'i' == tmp[1])
  89. return 0;
  90. break;
  91. case USB_ENDPOINT_XFER_BULK:
  92. if ('b' != tmp[1]) /* != "-bulk" */
  93. return 0;
  94. break;
  95. case USB_ENDPOINT_XFER_ISOC:
  96. if ('s' != tmp[2]) /* != "-iso" */
  97. return 0;
  98. }
  99. } else {
  100. tmp = ep->name + strlen(ep->name);
  101. }
  102. /* direction-restriction: "..in-..", "out-.." */
  103. tmp--;
  104. if (!isdigit(*tmp)) {
  105. if (desc->bEndpointAddress & USB_DIR_IN) {
  106. if ('n' != *tmp)
  107. return 0;
  108. } else {
  109. if ('t' != *tmp)
  110. return 0;
  111. }
  112. }
  113. }
  114. /* endpoint maxpacket size is an input parameter, except for bulk
  115. * where it's an output parameter representing the full speed limit.
  116. * the usb spec fixes high speed bulk maxpacket at 512 bytes.
  117. */
  118. max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
  119. switch (type) {
  120. case USB_ENDPOINT_XFER_INT:
  121. /* INT: limit 64 bytes full speed, 1024 high speed */
  122. if (!gadget->is_dualspeed && max > 64)
  123. return 0;
  124. /* FALLTHROUGH */
  125. case USB_ENDPOINT_XFER_ISOC:
  126. /* ISO: limit 1023 bytes full speed, 1024 high speed */
  127. if (ep->maxpacket < max)
  128. return 0;
  129. if (!gadget->is_dualspeed && max > 1023)
  130. return 0;
  131. /* BOTH: "high bandwidth" works only at high speed */
  132. if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) {
  133. if (!gadget->is_dualspeed)
  134. return 0;
  135. /* configure your hardware with enough buffering!! */
  136. }
  137. break;
  138. }
  139. /* MATCH!! */
  140. /* report address */
  141. if (isdigit(ep->name[2])) {
  142. u8 num = simple_strtoul(&ep->name[2], NULL, 10);
  143. desc->bEndpointAddress |= num;
  144. #ifdef MANY_ENDPOINTS
  145. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  146. if (++in_epnum > 15)
  147. return 0;
  148. desc->bEndpointAddress = USB_DIR_IN | in_epnum;
  149. #endif
  150. } else {
  151. if (++epnum > 15)
  152. return 0;
  153. desc->bEndpointAddress |= epnum;
  154. }
  155. /* report (variable) full speed bulk maxpacket */
  156. if (USB_ENDPOINT_XFER_BULK == type) {
  157. int size = ep->maxpacket;
  158. /* min() doesn't work on bitfields with gcc-3.5 */
  159. if (size > 64)
  160. size = 64;
  161. desc->wMaxPacketSize = cpu_to_le16(size);
  162. }
  163. return 1;
  164. }
  165. static struct usb_ep *
  166. find_ep(struct usb_gadget *gadget, const char *name)
  167. {
  168. struct usb_ep *ep;
  169. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  170. if (0 == strcmp(ep->name, name))
  171. return ep;
  172. }
  173. return NULL;
  174. }
  175. /**
  176. * usb_ep_autoconfig - choose an endpoint matching the descriptor
  177. * @gadget: The device to which the endpoint must belong.
  178. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  179. * initialized. For periodic transfers, the maximum packet
  180. * size must also be initialized. This is modified on success.
  181. *
  182. * By choosing an endpoint to use with the specified descriptor, this
  183. * routine simplifies writing gadget drivers that work with multiple
  184. * USB device controllers. The endpoint would be passed later to
  185. * usb_ep_enable(), along with some descriptor.
  186. *
  187. * That second descriptor won't always be the same as the first one.
  188. * For example, isochronous endpoints can be autoconfigured for high
  189. * bandwidth, and then used in several lower bandwidth altsettings.
  190. * Also, high and full speed descriptors will be different.
  191. *
  192. * Be sure to examine and test the results of autoconfiguration on your
  193. * hardware. This code may not make the best choices about how to use the
  194. * USB controller, and it can't know all the restrictions that may apply.
  195. * Some combinations of driver and hardware won't be able to autoconfigure.
  196. *
  197. * On success, this returns an un-claimed usb_ep, and modifies the endpoint
  198. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  199. * is initialized as if the endpoint were used at full speed. To prevent
  200. * the endpoint from being returned by a later autoconfig call, claim it
  201. * by assigning ep->driver_data to some non-null value.
  202. *
  203. * On failure, this returns a null endpoint descriptor.
  204. */
  205. struct usb_ep *usb_ep_autoconfig(
  206. struct usb_gadget *gadget,
  207. struct usb_endpoint_descriptor *desc
  208. )
  209. {
  210. struct usb_ep *ep;
  211. u8 type;
  212. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  213. /* First, apply chip-specific "best usage" knowledge.
  214. * This might make a good usb_gadget_ops hook ...
  215. */
  216. if (gadget_is_net2280(gadget) && type == USB_ENDPOINT_XFER_INT) {
  217. /* ep-e, ep-f are PIO with only 64 byte fifos */
  218. ep = find_ep(gadget, "ep-e");
  219. if (ep && ep_matches(gadget, ep, desc))
  220. return ep;
  221. ep = find_ep(gadget, "ep-f");
  222. if (ep && ep_matches(gadget, ep, desc))
  223. return ep;
  224. } else if (gadget_is_goku(gadget)) {
  225. if (USB_ENDPOINT_XFER_INT == type) {
  226. /* single buffering is enough */
  227. ep = find_ep(gadget, "ep3-bulk");
  228. if (ep && ep_matches(gadget, ep, desc))
  229. return ep;
  230. } else if (USB_ENDPOINT_XFER_BULK == type
  231. && (USB_DIR_IN & desc->bEndpointAddress)) {
  232. /* DMA may be available */
  233. ep = find_ep(gadget, "ep2-bulk");
  234. if (ep && ep_matches(gadget, ep, desc))
  235. return ep;
  236. }
  237. } else if (gadget_is_sh(gadget) && USB_ENDPOINT_XFER_INT == type) {
  238. /* single buffering is enough; maybe 8 byte fifo is too */
  239. ep = find_ep(gadget, "ep3in-bulk");
  240. if (ep && ep_matches(gadget, ep, desc))
  241. return ep;
  242. } else if (gadget_is_mq11xx(gadget) && USB_ENDPOINT_XFER_INT == type) {
  243. ep = find_ep(gadget, "ep1-bulk");
  244. if (ep && ep_matches(gadget, ep, desc))
  245. return ep;
  246. }
  247. /* Second, look at endpoints until an unclaimed one looks usable */
  248. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  249. if (ep_matches(gadget, ep, desc))
  250. return ep;
  251. }
  252. /* Fail */
  253. return NULL;
  254. }
  255. /**
  256. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  257. * @gadget: device for which autoconfig state will be reset
  258. *
  259. * Use this for devices where one configuration may need to assign
  260. * endpoint resources very differently from the next one. It clears
  261. * state such as ep->driver_data and the record of assigned endpoints
  262. * used by usb_ep_autoconfig().
  263. */
  264. void usb_ep_autoconfig_reset(struct usb_gadget *gadget)
  265. {
  266. struct usb_ep *ep;
  267. list_for_each_entry(ep, &gadget->ep_list, ep_list) {
  268. ep->driver_data = NULL;
  269. }
  270. #ifdef MANY_ENDPOINTS
  271. in_epnum = 0;
  272. #endif
  273. epnum = 0;
  274. }