epautoconf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /*
  117. * If the protocol driver hasn't yet decided on wMaxPacketSize
  118. * and wants to know the maximum possible, provide the info.
  119. */
  120. if (desc->wMaxPacketSize == 0)
  121. desc->wMaxPacketSize = cpu_to_le16(ep->maxpacket);
  122. /* endpoint maxpacket size is an input parameter, except for bulk
  123. * where it's an output parameter representing the full speed limit.
  124. * the usb spec fixes high speed bulk maxpacket at 512 bytes.
  125. */
  126. max = 0x7ff & le16_to_cpu(desc->wMaxPacketSize);
  127. switch (type) {
  128. case USB_ENDPOINT_XFER_INT:
  129. /* INT: limit 64 bytes full speed, 1024 high speed */
  130. if (!gadget->is_dualspeed && max > 64)
  131. return 0;
  132. /* FALLTHROUGH */
  133. case USB_ENDPOINT_XFER_ISOC:
  134. /* ISO: limit 1023 bytes full speed, 1024 high speed */
  135. if (ep->maxpacket < max)
  136. return 0;
  137. if (!gadget->is_dualspeed && max > 1023)
  138. return 0;
  139. /* BOTH: "high bandwidth" works only at high speed */
  140. if ((desc->wMaxPacketSize & cpu_to_le16(3<<11))) {
  141. if (!gadget->is_dualspeed)
  142. return 0;
  143. /* configure your hardware with enough buffering!! */
  144. }
  145. break;
  146. }
  147. /* MATCH!! */
  148. /* report address */
  149. desc->bEndpointAddress &= USB_DIR_IN;
  150. if (isdigit (ep->name [2])) {
  151. u8 num = simple_strtoul (&ep->name [2], NULL, 10);
  152. desc->bEndpointAddress |= num;
  153. #ifdef MANY_ENDPOINTS
  154. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  155. if (++in_epnum > 15)
  156. return 0;
  157. desc->bEndpointAddress = USB_DIR_IN | in_epnum;
  158. #endif
  159. } else {
  160. if (++epnum > 15)
  161. return 0;
  162. desc->bEndpointAddress |= epnum;
  163. }
  164. /* report (variable) full speed bulk maxpacket */
  165. if (USB_ENDPOINT_XFER_BULK == type) {
  166. int size = ep->maxpacket;
  167. /* min() doesn't work on bitfields with gcc-3.5 */
  168. if (size > 64)
  169. size = 64;
  170. desc->wMaxPacketSize = cpu_to_le16(size);
  171. }
  172. return 1;
  173. }
  174. static struct usb_ep *
  175. find_ep (struct usb_gadget *gadget, const char *name)
  176. {
  177. struct usb_ep *ep;
  178. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  179. if (0 == strcmp (ep->name, name))
  180. return ep;
  181. }
  182. return NULL;
  183. }
  184. /**
  185. * usb_ep_autoconfig - choose an endpoint matching the descriptor
  186. * @gadget: The device to which the endpoint must belong.
  187. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  188. * initialized. For periodic transfers, the maximum packet
  189. * size must also be initialized. This is modified on success.
  190. *
  191. * By choosing an endpoint to use with the specified descriptor, this
  192. * routine simplifies writing gadget drivers that work with multiple
  193. * USB device controllers. The endpoint would be passed later to
  194. * usb_ep_enable(), along with some descriptor.
  195. *
  196. * That second descriptor won't always be the same as the first one.
  197. * For example, isochronous endpoints can be autoconfigured for high
  198. * bandwidth, and then used in several lower bandwidth altsettings.
  199. * Also, high and full speed descriptors will be different.
  200. *
  201. * Be sure to examine and test the results of autoconfiguration on your
  202. * hardware. This code may not make the best choices about how to use the
  203. * USB controller, and it can't know all the restrictions that may apply.
  204. * Some combinations of driver and hardware won't be able to autoconfigure.
  205. *
  206. * On success, this returns an un-claimed usb_ep, and modifies the endpoint
  207. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  208. * is initialized as if the endpoint were used at full speed. To prevent
  209. * the endpoint from being returned by a later autoconfig call, claim it
  210. * by assigning ep->driver_data to some non-null value.
  211. *
  212. * On failure, this returns a null endpoint descriptor.
  213. */
  214. struct usb_ep *usb_ep_autoconfig (
  215. struct usb_gadget *gadget,
  216. struct usb_endpoint_descriptor *desc
  217. )
  218. {
  219. struct usb_ep *ep;
  220. u8 type;
  221. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  222. /* First, apply chip-specific "best usage" knowledge.
  223. * This might make a good usb_gadget_ops hook ...
  224. */
  225. if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
  226. /* ep-e, ep-f are PIO with only 64 byte fifos */
  227. ep = find_ep (gadget, "ep-e");
  228. if (ep && ep_matches (gadget, ep, desc))
  229. return ep;
  230. ep = find_ep (gadget, "ep-f");
  231. if (ep && ep_matches (gadget, ep, desc))
  232. return ep;
  233. } else if (gadget_is_goku (gadget)) {
  234. if (USB_ENDPOINT_XFER_INT == type) {
  235. /* single buffering is enough */
  236. ep = find_ep (gadget, "ep3-bulk");
  237. if (ep && ep_matches (gadget, ep, desc))
  238. return ep;
  239. } else if (USB_ENDPOINT_XFER_BULK == type
  240. && (USB_DIR_IN & desc->bEndpointAddress)) {
  241. /* DMA may be available */
  242. ep = find_ep (gadget, "ep2-bulk");
  243. if (ep && ep_matches (gadget, ep, desc))
  244. return ep;
  245. }
  246. #ifdef CONFIG_BLACKFIN
  247. } else if (gadget_is_musbhdrc(gadget)) {
  248. if ((USB_ENDPOINT_XFER_BULK == type) ||
  249. (USB_ENDPOINT_XFER_ISOC == type)) {
  250. if (USB_DIR_IN & desc->bEndpointAddress)
  251. ep = find_ep (gadget, "ep5in");
  252. else
  253. ep = find_ep (gadget, "ep6out");
  254. } else if (USB_ENDPOINT_XFER_INT == type) {
  255. if (USB_DIR_IN & desc->bEndpointAddress)
  256. ep = find_ep(gadget, "ep1in");
  257. else
  258. ep = find_ep(gadget, "ep2out");
  259. } else
  260. ep = NULL;
  261. if (ep && ep_matches (gadget, ep, desc))
  262. return ep;
  263. #endif
  264. }
  265. /* Second, look at endpoints until an unclaimed one looks usable */
  266. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  267. if (ep_matches (gadget, ep, desc))
  268. return ep;
  269. }
  270. /* Fail */
  271. return NULL;
  272. }
  273. /**
  274. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  275. * @gadget: device for which autoconfig state will be reset
  276. *
  277. * Use this for devices where one configuration may need to assign
  278. * endpoint resources very differently from the next one. It clears
  279. * state such as ep->driver_data and the record of assigned endpoints
  280. * used by usb_ep_autoconfig().
  281. */
  282. void usb_ep_autoconfig_reset (struct usb_gadget *gadget)
  283. {
  284. struct usb_ep *ep;
  285. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  286. ep->driver_data = NULL;
  287. }
  288. #ifdef MANY_ENDPOINTS
  289. in_epnum = 0;
  290. #endif
  291. epnum = 0;
  292. }