epautoconf.c 12 KB

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