hv_drv_UsbGadgetConfig.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * @file hv_drv_UsbGadgetConfig.c
  3. * @brief Simplify building config descriptors.
  4. *
  5. * @author HiView SoC Software Team
  6. * @version 1.0.0
  7. * @date 2022-06-15
  8. */
  9. #include "hv_drv_UsbErrNo.h"
  10. #include <string.h>
  11. #include "hv_drv_UsbCh9.h"
  12. #include "hv_drv_UsbGadget.h"
  13. #include "hv_drv_UsbGadgetComposite.h"
  14. /**
  15. * usb_descriptor_fillbuf - fill buffer with descriptors
  16. * @buf: Buffer to be filled
  17. * @buflen: Size of buf
  18. * @src: Array of descriptor pointers, terminated by null pointer.
  19. *
  20. * Copies descriptors into the buffer, returning the length or a
  21. * negative error code if they can't all be copied. Useful when
  22. * assembling descriptors for an associated set of interfaces used
  23. * as part of configuring a composite device; or in other cases where
  24. * sets of descriptors need to be marshaled.
  25. */
  26. int usb_descriptor_fillbuf(void *buf, unsigned buflen,
  27. const struct usb_descriptor_header **src)
  28. {
  29. u8 *dest = buf;
  30. if (!src)
  31. return -EINVAL;
  32. /* fill buffer from src[] until null descriptor ptr */
  33. for (; NULL != *src; src++) {
  34. unsigned len = (*src)->bLength;
  35. if (len > buflen)
  36. return -EINVAL;
  37. HV_MEMCPY(dest, *src, len);
  38. buflen -= len;
  39. dest += len;
  40. }
  41. return dest - (u8 *)buf;
  42. }
  43. /**
  44. * usb_gadget_config_buf - builts a complete configuration descriptor
  45. * @config: Header for the descriptor, including characteristics such
  46. * as power requirements and number of interfaces.
  47. * @desc: Null-terminated vector of pointers to the descriptors (interface,
  48. * endpoint, etc) defining all functions in this device configuration.
  49. * @buf: Buffer for the resulting configuration descriptor.
  50. * @length: Length of buffer. If this is not big enough to hold the
  51. * entire configuration descriptor, an error code will be returned.
  52. *
  53. * This copies descriptors into the response buffer, building a descriptor
  54. * for that configuration. It returns the buffer length or a negative
  55. * status code. The config.wTotalLength field is set to match the length
  56. * of the result, but other descriptor fields (including power usage and
  57. * interface count) must be set by the caller.
  58. *
  59. * Gadget drivers could use this when constructing a config descriptor
  60. * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  61. * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  62. */
  63. int usb_gadget_config_buf(
  64. const struct usb_config_descriptor *config,
  65. void *buf,
  66. unsigned length,
  67. const struct usb_descriptor_header **desc
  68. )
  69. {
  70. struct usb_config_descriptor *cp = buf;
  71. int len;
  72. /* config descriptor first */
  73. if (length < USB_DT_CONFIG_SIZE || !desc)
  74. return -EINVAL;
  75. /* config need not be aligned */
  76. HV_MEMCPY(cp, config, sizeof(*cp));
  77. /* then interface/endpoint/class/vendor/... */
  78. len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
  79. length - USB_DT_CONFIG_SIZE, desc);
  80. if (len < 0)
  81. return len;
  82. len += USB_DT_CONFIG_SIZE;
  83. if (len > 0xffff)
  84. return -EINVAL;
  85. /* patch up the config descriptor */
  86. cp->bLength = USB_DT_CONFIG_SIZE;
  87. cp->bDescriptorType = USB_DT_CONFIG;
  88. put_unaligned_le16(len, &cp->wTotalLength);
  89. cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  90. return len;
  91. }
  92. /**
  93. * usb_copy_descriptors - copy a vector of USB descriptors
  94. * @src: null-terminated vector to copy
  95. * Context: initialization code, which may sleep
  96. *
  97. * This makes a copy of a vector of USB descriptors. Its primary use
  98. * is to support usb_function objects which can have multiple copies,
  99. * each needing different descriptors. Functions may have static
  100. * tables of descriptors, which are used as templates and customized
  101. * with identifiers (for interfaces, strings, endpoints, and more)
  102. * as needed by a given function instance.
  103. */
  104. struct usb_descriptor_header **
  105. usb_copy_descriptors(struct usb_descriptor_header **src)
  106. {
  107. struct usb_descriptor_header **tmp;
  108. unsigned bytes;
  109. unsigned n_desc;
  110. void *mem;
  111. struct usb_descriptor_header **ret;
  112. /* count descriptors and their sizes; then add vector size */
  113. for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
  114. bytes += (*tmp)->bLength;
  115. bytes += (n_desc + 1) * sizeof(*tmp);
  116. mem = HV_MALLOC(bytes);
  117. if (!mem)
  118. return NULL;
  119. /* fill in pointers starting at "tmp",
  120. * to descriptors copied starting at "mem";
  121. * and return "ret"
  122. */
  123. tmp = mem;
  124. ret = mem;
  125. mem += (n_desc + 1) * sizeof(*tmp);
  126. while (*src) {
  127. HV_MEMCPY(mem, *src, (*src)->bLength);
  128. *tmp = mem;
  129. tmp++;
  130. mem += (*src)->bLength;
  131. src++;
  132. }
  133. *tmp = NULL;
  134. return ret;
  135. }
  136. static inline void usb_free_descriptors(struct usb_descriptor_header **v)
  137. {
  138. HV_FREE(v);
  139. }
  140. void usb_free_all_descriptors(struct usb_function *f)
  141. {
  142. usb_free_descriptors(f->fs_descriptors);
  143. usb_free_descriptors(f->hs_descriptors);
  144. }
  145. int usb_assign_descriptors(struct usb_function *f,
  146. struct usb_descriptor_header **fs,
  147. struct usb_descriptor_header **hs,
  148. struct usb_descriptor_header **ss)
  149. {
  150. struct usb_gadget *g = f->config->cdev->gadget;
  151. if (fs) {
  152. f->fs_descriptors = usb_copy_descriptors(fs);
  153. if (!f->fs_descriptors)
  154. goto err;
  155. }
  156. if (hs && gadget_is_dualspeed(g)) {
  157. f->hs_descriptors = usb_copy_descriptors(hs);
  158. if (!f->hs_descriptors)
  159. goto err;
  160. }
  161. if (hs) {
  162. f->hs_descriptors = usb_copy_descriptors(hs);
  163. if (!f->hs_descriptors)
  164. goto err;
  165. }
  166. return 0;
  167. err:
  168. usb_free_all_descriptors(f);
  169. return -ENOMEM;
  170. }