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