epautoconf.c 12 KB

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