epautoconf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 __initdata unsigned epnum;
  32. // #define MANY_ENDPOINTS
  33. #ifdef MANY_ENDPOINTS
  34. /* more than 15 configurable endpoints */
  35. static __initdata 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 __init
  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 (0 != 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_cpup (&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 & __constant_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. if (isdigit (ep->name [2])) {
  144. u8 num = simple_strtol (&ep->name [2], NULL, 10);
  145. desc->bEndpointAddress |= num;
  146. #ifdef MANY_ENDPOINTS
  147. } else if (desc->bEndpointAddress & USB_DIR_IN) {
  148. if (++in_epnum > 15)
  149. return 0;
  150. desc->bEndpointAddress = USB_DIR_IN | in_epnum;
  151. #endif
  152. } else {
  153. if (++epnum > 15)
  154. return 0;
  155. desc->bEndpointAddress |= epnum;
  156. }
  157. /* report (variable) full speed bulk maxpacket */
  158. if (USB_ENDPOINT_XFER_BULK == type) {
  159. int size = ep->maxpacket;
  160. /* min() doesn't work on bitfields with gcc-3.5 */
  161. if (size > 64)
  162. size = 64;
  163. desc->wMaxPacketSize = cpu_to_le16(size);
  164. }
  165. return 1;
  166. }
  167. static struct usb_ep * __init
  168. find_ep (struct usb_gadget *gadget, const char *name)
  169. {
  170. struct usb_ep *ep;
  171. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  172. if (0 == strcmp (ep->name, name))
  173. return ep;
  174. }
  175. return NULL;
  176. }
  177. /**
  178. * usb_ep_autoconfig - choose an endpoint matching the descriptor
  179. * @gadget: The device to which the endpoint must belong.
  180. * @desc: Endpoint descriptor, with endpoint direction and transfer mode
  181. * initialized. For periodic transfers, the maximum packet
  182. * size must also be initialized. This is modified on success.
  183. *
  184. * By choosing an endpoint to use with the specified descriptor, this
  185. * routine simplifies writing gadget drivers that work with multiple
  186. * USB device controllers. The endpoint would be passed later to
  187. * usb_ep_enable(), along with some descriptor.
  188. *
  189. * That second descriptor won't always be the same as the first one.
  190. * For example, isochronous endpoints can be autoconfigured for high
  191. * bandwidth, and then used in several lower bandwidth altsettings.
  192. * Also, high and full speed descriptors will be different.
  193. *
  194. * Be sure to examine and test the results of autoconfiguration on your
  195. * hardware. This code may not make the best choices about how to use the
  196. * USB controller, and it can't know all the restrictions that may apply.
  197. * Some combinations of driver and hardware won't be able to autoconfigure.
  198. *
  199. * On success, this returns an un-claimed usb_ep, and modifies the endpoint
  200. * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
  201. * is initialized as if the endpoint were used at full speed. To prevent
  202. * the endpoint from being returned by a later autoconfig call, claim it
  203. * by assigning ep->driver_data to some non-null value.
  204. *
  205. * On failure, this returns a null endpoint descriptor.
  206. */
  207. struct usb_ep * __init usb_ep_autoconfig (
  208. struct usb_gadget *gadget,
  209. struct usb_endpoint_descriptor *desc
  210. )
  211. {
  212. struct usb_ep *ep;
  213. u8 type;
  214. type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
  215. /* First, apply chip-specific "best usage" knowledge.
  216. * This might make a good usb_gadget_ops hook ...
  217. */
  218. if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
  219. /* ep-e, ep-f are PIO with only 64 byte fifos */
  220. ep = find_ep (gadget, "ep-e");
  221. if (ep && ep_matches (gadget, ep, desc))
  222. return ep;
  223. ep = find_ep (gadget, "ep-f");
  224. if (ep && ep_matches (gadget, ep, desc))
  225. return ep;
  226. } else if (gadget_is_goku (gadget)) {
  227. if (USB_ENDPOINT_XFER_INT == type) {
  228. /* single buffering is enough */
  229. ep = find_ep (gadget, "ep3-bulk");
  230. if (ep && ep_matches (gadget, ep, desc))
  231. return ep;
  232. } else if (USB_ENDPOINT_XFER_BULK == type
  233. && (USB_DIR_IN & desc->bEndpointAddress)) {
  234. /* DMA may be available */
  235. ep = find_ep (gadget, "ep2-bulk");
  236. if (ep && ep_matches (gadget, ep, desc))
  237. return ep;
  238. }
  239. } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) {
  240. /* single buffering is enough; maybe 8 byte fifo is too */
  241. ep = find_ep (gadget, "ep3in-bulk");
  242. if (ep && ep_matches (gadget, ep, desc))
  243. return ep;
  244. } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) {
  245. ep = find_ep (gadget, "ep1-bulk");
  246. if (ep && ep_matches (gadget, ep, desc))
  247. return ep;
  248. }
  249. /* Second, look at endpoints until an unclaimed one looks usable */
  250. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  251. if (ep_matches (gadget, ep, desc))
  252. return ep;
  253. }
  254. /* Fail */
  255. return NULL;
  256. }
  257. /**
  258. * usb_ep_autoconfig_reset - reset endpoint autoconfig state
  259. * @gadget: device for which autoconfig state will be reset
  260. *
  261. * Use this for devices where one configuration may need to assign
  262. * endpoint resources very differently from the next one. It clears
  263. * state such as ep->driver_data and the record of assigned endpoints
  264. * used by usb_ep_autoconfig().
  265. */
  266. void __init usb_ep_autoconfig_reset (struct usb_gadget *gadget)
  267. {
  268. struct usb_ep *ep;
  269. list_for_each_entry (ep, &gadget->ep_list, ep_list) {
  270. ep->driver_data = NULL;
  271. }
  272. #ifdef MANY_ENDPOINTS
  273. in_epnum = 0;
  274. #endif
  275. epnum = 0;
  276. }