epautoconf.c 12 KB

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