config.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * usb/gadget/config.c -- simplify building config descriptors
  3. *
  4. * Copyright (C) 2003 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * Ported to U-boot by: Thomas Smits <ts.smits@gmail.com> and
  21. * Remy Bohmer <linux@bohmer.net>
  22. */
  23. #include <common.h>
  24. #include <asm/errno.h>
  25. #include <linux/list.h>
  26. #include <linux/string.h>
  27. #include <linux/usb/ch9.h>
  28. #include <linux/usb/gadget.h>
  29. /**
  30. * usb_descriptor_fillbuf - fill buffer with descriptors
  31. * @buf: Buffer to be filled
  32. * @buflen: Size of buf
  33. * @src: Array of descriptor pointers, terminated by null pointer.
  34. *
  35. * Copies descriptors into the buffer, returning the length or a
  36. * negative error code if they can't all be copied. Useful when
  37. * assembling descriptors for an associated set of interfaces used
  38. * as part of configuring a composite device; or in other cases where
  39. * sets of descriptors need to be marshaled.
  40. */
  41. int
  42. usb_descriptor_fillbuf(void *buf, unsigned buflen,
  43. const struct usb_descriptor_header **src)
  44. {
  45. u8 *dest = buf;
  46. if (!src)
  47. return -EINVAL;
  48. /* fill buffer from src[] until null descriptor ptr */
  49. for (; NULL != *src; src++) {
  50. unsigned len = (*src)->bLength;
  51. if (len > buflen)
  52. return -EINVAL;
  53. memcpy(dest, *src, len);
  54. buflen -= len;
  55. dest += len;
  56. }
  57. return dest - (u8 *)buf;
  58. }
  59. /**
  60. * usb_gadget_config_buf - builts a complete configuration descriptor
  61. * @config: Header for the descriptor, including characteristics such
  62. * as power requirements and number of interfaces.
  63. * @desc: Null-terminated vector of pointers to the descriptors (interface,
  64. * endpoint, etc) defining all functions in this device configuration.
  65. * @buf: Buffer for the resulting configuration descriptor.
  66. * @length: Length of buffer. If this is not big enough to hold the
  67. * entire configuration descriptor, an error code will be returned.
  68. *
  69. * This copies descriptors into the response buffer, building a descriptor
  70. * for that configuration. It returns the buffer length or a negative
  71. * status code. The config.wTotalLength field is set to match the length
  72. * of the result, but other descriptor fields (including power usage and
  73. * interface count) must be set by the caller.
  74. *
  75. * Gadget drivers could use this when constructing a config descriptor
  76. * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  77. * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  78. */
  79. int usb_gadget_config_buf(
  80. const struct usb_config_descriptor *config,
  81. void *buf,
  82. unsigned length,
  83. const struct usb_descriptor_header **desc
  84. )
  85. {
  86. struct usb_config_descriptor *cp = buf;
  87. int len;
  88. /* config descriptor first */
  89. if (length < USB_DT_CONFIG_SIZE || !desc)
  90. return -EINVAL;
  91. *cp = *config;
  92. /* then interface/endpoint/class/vendor/... */
  93. len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8 *)buf,
  94. length - USB_DT_CONFIG_SIZE, desc);
  95. if (len < 0)
  96. return len;
  97. len += USB_DT_CONFIG_SIZE;
  98. if (len > 0xffff)
  99. return -EINVAL;
  100. /* patch up the config descriptor */
  101. cp->bLength = USB_DT_CONFIG_SIZE;
  102. cp->bDescriptorType = USB_DT_CONFIG;
  103. cp->wTotalLength = cpu_to_le16(len);
  104. cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  105. return len;
  106. }