config.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (fs) {
  151. f->fs_descriptors = usb_copy_descriptors(fs);
  152. if (!f->fs_descriptors)
  153. goto err;
  154. }
  155. if (hs && gadget_is_dualspeed(g)) {
  156. f->hs_descriptors = usb_copy_descriptors(hs);
  157. if (!f->hs_descriptors)
  158. goto err;
  159. }
  160. if (ss && gadget_is_superspeed(g)) {
  161. f->ss_descriptors = usb_copy_descriptors(ss);
  162. if (!f->ss_descriptors)
  163. goto err;
  164. }
  165. if (ssp && gadget_is_superspeed_plus(g)) {
  166. f->ssp_descriptors = usb_copy_descriptors(ssp);
  167. if (!f->ssp_descriptors)
  168. goto err;
  169. }
  170. return 0;
  171. err:
  172. usb_free_all_descriptors(f);
  173. return -ENOMEM;
  174. }
  175. EXPORT_SYMBOL_GPL(usb_assign_descriptors);
  176. void usb_free_all_descriptors(struct usb_function *f)
  177. {
  178. usb_free_descriptors(f->fs_descriptors);
  179. usb_free_descriptors(f->hs_descriptors);
  180. usb_free_descriptors(f->ss_descriptors);
  181. usb_free_descriptors(f->ssp_descriptors);
  182. }
  183. EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
  184. struct usb_descriptor_header *usb_otg_descriptor_alloc(
  185. struct usb_gadget *gadget)
  186. {
  187. struct usb_descriptor_header *otg_desc;
  188. unsigned length = 0;
  189. if (gadget->otg_caps && (gadget->otg_caps->otg_rev >= 0x0200))
  190. length = sizeof(struct usb_otg20_descriptor);
  191. else
  192. length = sizeof(struct usb_otg_descriptor);
  193. otg_desc = kzalloc(length, GFP_KERNEL);
  194. return otg_desc;
  195. }
  196. EXPORT_SYMBOL_GPL(usb_otg_descriptor_alloc);
  197. int usb_otg_descriptor_init(struct usb_gadget *gadget,
  198. struct usb_descriptor_header *otg_desc)
  199. {
  200. struct usb_otg_descriptor *otg1x_desc;
  201. struct usb_otg20_descriptor *otg20_desc;
  202. struct usb_otg_caps *otg_caps = gadget->otg_caps;
  203. u8 otg_attributes = 0;
  204. if (!otg_desc)
  205. return -EINVAL;
  206. if (otg_caps && otg_caps->otg_rev) {
  207. if (otg_caps->hnp_support)
  208. otg_attributes |= USB_OTG_HNP;
  209. if (otg_caps->srp_support)
  210. otg_attributes |= USB_OTG_SRP;
  211. if (otg_caps->adp_support && (otg_caps->otg_rev >= 0x0200))
  212. otg_attributes |= USB_OTG_ADP;
  213. } else {
  214. otg_attributes = USB_OTG_SRP | USB_OTG_HNP;
  215. }
  216. if (otg_caps && (otg_caps->otg_rev >= 0x0200)) {
  217. otg20_desc = (struct usb_otg20_descriptor *)otg_desc;
  218. otg20_desc->bLength = sizeof(struct usb_otg20_descriptor);
  219. otg20_desc->bDescriptorType = USB_DT_OTG;
  220. otg20_desc->bmAttributes = otg_attributes;
  221. otg20_desc->bcdOTG = cpu_to_le16(otg_caps->otg_rev);
  222. } else {
  223. otg1x_desc = (struct usb_otg_descriptor *)otg_desc;
  224. otg1x_desc->bLength = sizeof(struct usb_otg_descriptor);
  225. otg1x_desc->bDescriptorType = USB_DT_OTG;
  226. otg1x_desc->bmAttributes = otg_attributes;
  227. }
  228. return 0;
  229. }
  230. EXPORT_SYMBOL_GPL(usb_otg_descriptor_init);