epautoconf.c 12 KB

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