config.c 3.7 KB

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