config.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_copy_descriptors - copy a vector of USB descriptors
  51. * @src: null-terminated vector to copy
  52. * Context: initialization code, which may sleep
  53. *
  54. * This makes a copy of a vector of USB descriptors. Its primary use
  55. * is to support usb_function objects which can have multiple copies,
  56. * each needing different descriptors. Functions may have static
  57. * tables of descriptors, which are used as templates and customized
  58. * with identifiers (for interfaces, strings, endpoints, and more)
  59. * as needed by a given function instance.
  60. */
  61. struct usb_descriptor_header **
  62. usb_copy_descriptors(struct usb_descriptor_header **src)
  63. {
  64. struct usb_descriptor_header **tmp;
  65. unsigned bytes;
  66. unsigned n_desc;
  67. void *mem;
  68. struct usb_descriptor_header **ret;
  69. /* count descriptors and their sizes; then add vector size */
  70. for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
  71. bytes += (*tmp)->bLength;
  72. bytes += (n_desc + 1) * sizeof(*tmp);
  73. mem = kmalloc(bytes, GFP_KERNEL);
  74. if (!mem)
  75. return NULL;
  76. /* fill in pointers starting at "tmp",
  77. * to descriptors copied starting at "mem";
  78. * and return "ret"
  79. */
  80. tmp = mem;
  81. ret = mem;
  82. mem += (n_desc + 1) * sizeof(*tmp);
  83. while (*src) {
  84. memcpy(mem, *src, (*src)->bLength);
  85. *tmp = mem;
  86. tmp++;
  87. mem += (*src)->bLength;
  88. src++;
  89. }
  90. *tmp = NULL;
  91. return ret;
  92. }
  93. EXPORT_SYMBOL_GPL(usb_copy_descriptors);
  94. int usb_assign_descriptors(struct usb_function *f,
  95. struct usb_descriptor_header **fs,
  96. struct usb_descriptor_header **hs,
  97. struct usb_descriptor_header **ss,
  98. struct usb_descriptor_header **ssp)
  99. {
  100. /* super-speed-plus descriptor falls back to super-speed one,
  101. * if such a descriptor was provided, thus avoiding a NULL
  102. * pointer dereference if a 5gbps capable gadget is used with
  103. * a 10gbps capable config (device port + cable + host port)
  104. */
  105. if (!ssp)
  106. ssp = ss;
  107. if (fs) {
  108. f->fs_descriptors = usb_copy_descriptors(fs);
  109. if (!f->fs_descriptors)
  110. goto err;
  111. }
  112. if (hs) {
  113. f->hs_descriptors = usb_copy_descriptors(hs);
  114. if (!f->hs_descriptors)
  115. goto err;
  116. }
  117. if (ss) {
  118. f->ss_descriptors = usb_copy_descriptors(ss);
  119. if (!f->ss_descriptors)
  120. goto err;
  121. }
  122. if (ssp) {
  123. f->ssp_descriptors = usb_copy_descriptors(ssp);
  124. if (!f->ssp_descriptors)
  125. goto err;
  126. }
  127. return 0;
  128. err:
  129. usb_free_all_descriptors(f);
  130. return -ENOMEM;
  131. }
  132. EXPORT_SYMBOL_GPL(usb_assign_descriptors);
  133. void usb_free_all_descriptors(struct usb_function *f)
  134. {
  135. usb_free_descriptors(f->fs_descriptors);
  136. f->fs_descriptors = NULL;
  137. usb_free_descriptors(f->hs_descriptors);
  138. f->hs_descriptors = NULL;
  139. usb_free_descriptors(f->ss_descriptors);
  140. f->ss_descriptors = NULL;
  141. usb_free_descriptors(f->ssp_descriptors);
  142. f->ssp_descriptors = NULL;
  143. }
  144. EXPORT_SYMBOL_GPL(usb_free_all_descriptors);
  145. struct usb_descriptor_header *usb_otg_descriptor_alloc(
  146. struct usb_gadget *gadget)
  147. {
  148. struct usb_descriptor_header *otg_desc;
  149. unsigned length = 0;
  150. if (gadget->otg_caps && (gadget->otg_caps->otg_rev >= 0x0200))
  151. length = sizeof(struct usb_otg20_descriptor);
  152. else
  153. length = sizeof(struct usb_otg_descriptor);
  154. otg_desc = kzalloc(length, GFP_KERNEL);
  155. return otg_desc;
  156. }
  157. EXPORT_SYMBOL_GPL(usb_otg_descriptor_alloc);
  158. int usb_otg_descriptor_init(struct usb_gadget *gadget,
  159. struct usb_descriptor_header *otg_desc)
  160. {
  161. struct usb_otg_descriptor *otg1x_desc;
  162. struct usb_otg20_descriptor *otg20_desc;
  163. struct usb_otg_caps *otg_caps = gadget->otg_caps;
  164. u8 otg_attributes = 0;
  165. if (!otg_desc)
  166. return -EINVAL;
  167. if (otg_caps && otg_caps->otg_rev) {
  168. if (otg_caps->hnp_support)
  169. otg_attributes |= USB_OTG_HNP;
  170. if (otg_caps->srp_support)
  171. otg_attributes |= USB_OTG_SRP;
  172. if (otg_caps->adp_support && (otg_caps->otg_rev >= 0x0200))
  173. otg_attributes |= USB_OTG_ADP;
  174. } else {
  175. otg_attributes = USB_OTG_SRP | USB_OTG_HNP;
  176. }
  177. if (otg_caps && (otg_caps->otg_rev >= 0x0200)) {
  178. otg20_desc = (struct usb_otg20_descriptor *)otg_desc;
  179. otg20_desc->bLength = sizeof(struct usb_otg20_descriptor);
  180. otg20_desc->bDescriptorType = USB_DT_OTG;
  181. otg20_desc->bmAttributes = otg_attributes;
  182. otg20_desc->bcdOTG = cpu_to_le16(otg_caps->otg_rev);
  183. } else {
  184. otg1x_desc = (struct usb_otg_descriptor *)otg_desc;
  185. otg1x_desc->bLength = sizeof(struct usb_otg_descriptor);
  186. otg1x_desc->bDescriptorType = USB_DT_OTG;
  187. otg1x_desc->bmAttributes = otg_attributes;
  188. }
  189. return 0;
  190. }
  191. EXPORT_SYMBOL_GPL(usb_otg_descriptor_init);