epautoconf.c 12 KB

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