config.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * Ported to U-Boot by: Thomas Smits <ts.smits@gmail.com> and
  8. * Remy Bohmer <linux@bohmer.net>
  9. */
  10. #include <common.h>
  11. #include <asm/unaligned.h>
  12. #include <linux/errno.h>
  13. #include <linux/list.h>
  14. #include <linux/string.h>
  15. #include <linux/usb/ch9.h>
  16. #include <linux/usb/gadget.h>
  17. /**
  18. * usb_descriptor_fillbuf - fill buffer with descriptors
  19. * @buf: Buffer to be filled
  20. * @buflen: Size of buf
  21. * @src: Array of descriptor pointers, terminated by null pointer.
  22. *
  23. * Copies descriptors into the buffer, returning the length or a
  24. * negative error code if they can't all be copied. Useful when
  25. * assembling descriptors for an associated set of interfaces used
  26. * as part of configuring a composite device; or in other cases where
  27. * sets of descriptors need to be marshaled.
  28. */
  29. int
  30. usb_descriptor_fillbuf(void *buf, unsigned buflen,
  31. const struct usb_descriptor_header **src)
  32. {
  33. u8 *dest = buf;
  34. if (!src)
  35. return -EINVAL;
  36. /* fill buffer from src[] until null descriptor ptr */
  37. for (; NULL != *src; src++) {
  38. unsigned len = (*src)->bLength;
  39. if (len > buflen)
  40. return -EINVAL;
  41. memcpy(dest, *src, len);
  42. buflen -= len;
  43. dest += len;
  44. }
  45. return dest - (u8 *)buf;
  46. }
  47. /**
  48. * usb_gadget_config_buf - builts a complete configuration descriptor
  49. * @config: Header for the descriptor, including characteristics such
  50. * as power requirements and number of interfaces.
  51. * @desc: Null-terminated vector of pointers to the descriptors (interface,
  52. * endpoint, etc) defining all functions in this device configuration.
  53. * @buf: Buffer for the resulting configuration descriptor.
  54. * @length: Length of buffer. If this is not big enough to hold the
  55. * entire configuration descriptor, an error code will be returned.
  56. *
  57. * This copies descriptors into the response buffer, building a descriptor
  58. * for that configuration. It returns the buffer length or a negative
  59. * status code. The config.wTotalLength field is set to match the length
  60. * of the result, but other descriptor fields (including power usage and
  61. * interface count) must be set by the caller.
  62. *
  63. * Gadget drivers could use this when constructing a config descriptor
  64. * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  65. * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  66. */
  67. int usb_gadget_config_buf(
  68. const struct usb_config_descriptor *config,
  69. void *buf,
  70. unsigned length,
  71. const struct usb_descriptor_header **desc
  72. )
  73. {
  74. struct usb_config_descriptor *cp = buf;
  75. int len;
  76. /* config descriptor first */
  77. if (length < USB_DT_CONFIG_SIZE || !desc)
  78. return -EINVAL;
  79. /* config need not be aligned */
  80. memcpy(cp, config, sizeof(*cp));
  81. /* then interface/endpoint/class/vendor/... */
  82. len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
  83. length - USB_DT_CONFIG_SIZE, desc);
  84. if (len < 0)
  85. return len;
  86. len += USB_DT_CONFIG_SIZE;
  87. if (len > 0xffff)
  88. return -EINVAL;
  89. /* patch up the config descriptor */
  90. cp->bLength = USB_DT_CONFIG_SIZE;
  91. cp->bDescriptorType = USB_DT_CONFIG;
  92. put_unaligned_le16(len, &cp->wTotalLength);
  93. cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  94. return len;
  95. }