config.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * usb/gadget/config.c -- simplify building config descriptors
  4. *
  5. * Copyright (C) 2003 David Brownell
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/list.h>
  12. #include <linux/string.h>
  13. #include <linux/device.h>
  14. #include <linux/usb/ch9.h>
  15. #include <linux/usb/gadget.h>
  16. #include <linux/usb/composite.h>
  17. #include <linux/usb/otg.h>
  18. /**
  19. * usb_descriptor_fillbuf - fill buffer with descriptors
  20. * @buf: Buffer to be filled
  21. * @buflen: Size of buf
  22. * @src: Array of descriptor pointers, terminated by null pointer.
  23. *
  24. * Copies descriptors into the buffer, returning the length or a
  25. * negative error code if they can't all be copied. Useful when
  26. * assembling descriptors for an associated set of interfaces used
  27. * as part of configuring a composite device; or in other cases where
  28. * sets of descriptors need to be marshaled.
  29. */
  30. int
  31. usb_descriptor_fillbuf(void *buf, unsigned buflen,
  32. const struct usb_descriptor_header **src)
  33. {
  34. u8 *dest = buf;
  35. if (!src)
  36. return -EINVAL;
  37. /* fill buffer from src[] until null descriptor ptr */
  38. for (; NULL != *src; src++) {
  39. unsigned len = (*src)->bLength;
  40. if (len > buflen)
  41. return -EINVAL;
  42. memcpy(dest, *src, len);
  43. buflen -= len;
  44. dest += len;
  45. }
  46. return dest - (u8 *)buf;
  47. }
  48. EXPORT_SYMBOL_GPL(usb_descriptor_fillbuf);
  49. /**
  50. * usb_gadget_config_buf - builts a complete configuration descriptor
  51. * @config: Header for the descriptor, including characteristics such
  52. * as power requirements and number of interfaces.
  53. * @desc: Null-terminated vector of pointers to the descriptors (interface,
  54. * endpoint, etc) defining all functions in this device configuration.
  55. * @buf: Buffer for the resulting configuration descriptor.
  56. * @length: Length of buffer. If this is not big enough to hold the
  57. * entire configuration descriptor, an error code will be returned.
  58. *
  59. * This copies descriptors into the response buffer, building a descriptor
  60. * for that configuration. It returns the buffer length or a negative
  61. * status code. The config.wTotalLength field is set to match the length
  62. * of the result, but other descriptor fields (including power usage and
  63. * interface count) must be set by the caller.
  64. *
  65. * Gadget drivers could use this when constructing a config descriptor
  66. * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  67. * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  68. */
  69. int usb_gadget_config_buf(
  70. const struct usb_config_descriptor *config,
  71. void *buf,
  72. unsigned length,
  73. const struct usb_descriptor_header **desc
  74. )
  75. {
  76. struct usb_config_descriptor *cp = buf;
  77. int len;
  78. /* config descriptor first */
  79. if (length < USB_DT_CONFIG_SIZE || !desc)
  80. return -EINVAL;
  81. *cp = *config;
  82. /* then interface/endpoint/class/vendor/... */
  83. len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
  84. length - USB_DT_CONFIG_SIZE, desc);
  85. if (len < 0)
  86. return len;
  87. len += USB_DT_CONFIG_SIZE;
  88. if (len > 0xffff)
  89. return -EINVAL;
  90. /* patch up the config descriptor */
  91. cp->bLength = USB_DT_CONFIG_SIZE;
  92. cp->bDescriptorType = USB_DT_CONFIG;
  93. cp->wTotalLength = cpu_to_le16(len);
  94. cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  95. return len;
  96. }
  97. EXPORT_SYMBOL_GPL(usb_gadget_config_buf);
  98. /**
  99. * usb_copy_descriptors - copy a vector of USB descriptors
  100. * @src: null-terminated vector to copy
  101. * Context: initialization code, which may sleep
  102. *
  103. * This makes a copy of a vector of USB descriptors. Its primary use
  104. * is to support usb_function objects which can have multiple copies,
  105. * each needing different descriptors. Functions may have static
  106. * tables of descriptors, which are used as templates and customized
  107. * with identifiers (for interfaces, strings, endpoints, and more)
  108. * as needed by a given function instance.
  109. */
  110. struct usb_descriptor_header **
  111. usb_copy_descriptors(struct usb_descriptor_header **src)
  112. {
  113. struct usb_descriptor_header **tmp;
  114. unsigned bytes;
  115. unsigned n_desc;
  116. void *mem;
  117. struct usb_descriptor_header **ret;
  118. /* count descriptors and their sizes; then add vector size */
  119. for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
  120. bytes += (*tmp)->bLength;
  121. bytes += (n_desc + 1) * sizeof(*tmp);
  122. mem = kmalloc(bytes, GFP_KERNEL);
  123. if (!mem)
  124. return NULL;
  125. /* fill in pointers starting at "tmp",
  126. * to descriptors copied starting at "mem";
  127. * and return "ret"
  128. */
  129. tmp = mem;
  130. ret = mem;
  131. mem += (n_desc + 1) * sizeof(*tmp);
  132. while (*src) {
  133. memcpy(mem, *src, (*src)->bLength);
  134. *tmp = mem;
  135. tmp++;
  136. mem += (*src)->bLength;
  137. src++;
  138. }
  139. *tmp = NULL;
  140. return ret;
  141. }
  142. EXPORT_SYMBOL_GPL(usb_copy_descriptors);
  143. int usb_assign_descriptors(struct usb_function *f,
  144. struct usb_descriptor_header **fs,
  145. struct usb_descriptor_header **hs,
  146. struct usb_descriptor_header **ss,
  147. struct usb_descriptor_header **ssp)
  148. {
  149. struct usb_gadget *g = f->config->cdev->gadget;
  150. /* super-speed-plus descriptor falls back to super-speed one,
  151. * if such a descriptor was provided, thus avoiding a NULL
  152. * pointer dereference if a 5gbps capable gadget is used with
  153. * a 10gbps capable config (device port + cable + host port)
  154. */
  155. if (!ssp)
  156. ssp = ss;
  157. if (fs) {
  158. f->fs_descriptors = usb_copy_descriptors(fs);
  159. if (!f->fs_descriptors)
  160. goto err;
  161. }
  162. if (hs && gadget_is_dualspeed(g)) {
  163. f->hs_descriptors = usb_copy_descriptors(hs);
  164. if (!f->hs_descriptors)
  165. goto err;
  166. }
  167. if (ss && gadget_is_superspeed(g)) {
  168. f->ss_descriptors = usb_copy_descriptors(ss);
  169. if (!f->ss_descriptors)
  170. goto err;
  171. }
  172. if (ssp && gadget_is_superspeed_plus(g)) {
  173. f->ssp_descriptors = usb_copy_descriptors(ssp);
  174. if (!f->ssp_descriptors)
  175. goto err;
  176. }
  177. return 0;
  178. err:
  179. usb_free_all_descriptors(f);
  180. return -ENOMEM;
  181. }
  182. EXPORT_SYMBOL_GPL(usb_assign_descriptors);
  183. void usb_free_all_descriptors(struct usb_function *f)
  184. {
  185. usb_free_descriptors(f->fs_descriptors);
  186. f->fs_descriptors = NULL;
  187. usb_free_descriptors(f->hs_descriptors);
  188. f->hs_descriptors = NULL;
  189. usb_free_descriptors(f->ss_descriptors);
  190. f->ss_descriptors = NULL;
  191. usb_free_descriptors(f->ssp_descriptors);
  192. f->ssp_descriptors = NULL;
  193. }
  194. EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
  195. struct usb_descriptor_header *usb_otg_descriptor_alloc(
  196. struct usb_gadget *gadget)
  197. {
  198. struct usb_descriptor_header *otg_desc;
  199. unsigned length = 0;
  200. if (gadget->otg_caps && (gadget->otg_caps->otg_rev >= 0x0200))
  201. length = sizeof(struct usb_otg20_descriptor);
  202. else
  203. length = sizeof(struct usb_otg_descriptor);
  204. otg_desc = kzalloc(length, GFP_KERNEL);
  205. return otg_desc;
  206. }
  207. EXPORT_SYMBOL_GPL(usb_otg_descriptor_alloc);
  208. int usb_otg_descriptor_init(struct usb_gadget *gadget,
  209. struct usb_descriptor_header *otg_desc)
  210. {
  211. struct usb_otg_descriptor *otg1x_desc;
  212. struct usb_otg20_descriptor *otg20_desc;
  213. struct usb_otg_caps *otg_caps = gadget->otg_caps;
  214. u8 otg_attributes = 0;
  215. if (!otg_desc)
  216. return -EINVAL;
  217. if (otg_caps && otg_caps->otg_rev) {
  218. if (otg_caps->hnp_support)
  219. otg_attributes |= USB_OTG_HNP;
  220. if (otg_caps->srp_support)
  221. otg_attributes |= USB_OTG_SRP;
  222. if (otg_caps->adp_support && (otg_caps->otg_rev >= 0x0200))
  223. otg_attributes |= USB_OTG_ADP;
  224. } else {
  225. otg_attributes = USB_OTG_SRP | USB_OTG_HNP;
  226. }
  227. if (otg_caps && (otg_caps->otg_rev >= 0x0200)) {
  228. otg20_desc = (struct usb_otg20_descriptor *)otg_desc;
  229. otg20_desc->bLength = sizeof(struct usb_otg20_descriptor);
  230. otg20_desc->bDescriptorType = USB_DT_OTG;
  231. otg20_desc->bmAttributes = otg_attributes;
  232. otg20_desc->bcdOTG = cpu_to_le16(otg_caps->otg_rev);
  233. } else {
  234. otg1x_desc = (struct usb_otg_descriptor *)otg_desc;
  235. otg1x_desc->bLength = sizeof(struct usb_otg_descriptor);
  236. otg1x_desc->bDescriptorType = USB_DT_OTG;
  237. otg1x_desc->bmAttributes = otg_attributes;
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(usb_otg_descriptor_init);