config.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * usb/gadget/config.c -- simplify building config descriptors
  3. *
  4. * Copyright (C) 2003 David Brownell
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
  9. * Remy Bohmer <linux@bohmer.net>
  10. */
  11. #include <common.h>
  12. #include <asm/unaligned.h>
  13. #include <linux/errno.h>
  14. #include <linux/list.h>
  15. #include <linux/string.h>
  16. #include <linux/usb/ch9.h>
  17. #include <linux/usb/gadget.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. /**
  49. * usb_gadget_config_buf - builts a complete configuration descriptor
  50. * @config: Header for the descriptor, including characteristics such
  51. * as power requirements and number of interfaces.
  52. * @desc: Null-terminated vector of pointers to the descriptors (interface,
  53. * endpoint, etc) defining all functions in this device configuration.
  54. * @buf: Buffer for the resulting configuration descriptor.
  55. * @length: Length of buffer. If this is not big enough to hold the
  56. * entire configuration descriptor, an error code will be returned.
  57. *
  58. * This copies descriptors into the response buffer, building a descriptor
  59. * for that configuration. It returns the buffer length or a negative
  60. * status code. The config.wTotalLength field is set to match the length
  61. * of the result, but other descriptor fields (including power usage and
  62. * interface count) must be set by the caller.
  63. *
  64. * Gadget drivers could use this when constructing a config descriptor
  65. * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  66. * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  67. */
  68. int usb_gadget_config_buf(
  69. const struct usb_config_descriptor *config,
  70. void *buf,
  71. unsigned length,
  72. const struct usb_descriptor_header **desc
  73. )
  74. {
  75. struct usb_config_descriptor *cp = buf;
  76. int len;
  77. /* config descriptor first */
  78. if (length < USB_DT_CONFIG_SIZE || !desc)
  79. return -EINVAL;
  80. /* config need not be aligned */
  81. memcpy(cp, config, sizeof(*cp));
  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. put_unaligned_le16(len, &cp->wTotalLength);
  94. cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  95. return len;
  96. }