epautoconf.c 9.0 KB

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