config.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/string.h>
  16. #include <linux/device.h>
  17. #include <linux/usb/ch9.h>
  18. #include <linux/usb/gadget.h>
  19. /**
  20. * usb_descriptor_fillbuf - fill buffer with descriptors
  21. * @buf: Buffer to be filled
  22. * @buflen: Size of buf
  23. * @src: Array of descriptor pointers, terminated by null pointer.
  24. *
  25. * Copies descriptors into the buffer, returning the length or a
  26. * negative error code if they can't all be copied. Useful when
  27. * assembling descriptors for an associated set of interfaces used
  28. * as part of configuring a composite device; or in other cases where
  29. * sets of descriptors need to be marshaled.
  30. */
  31. int
  32. usb_descriptor_fillbuf(void *buf, unsigned buflen,
  33. const struct usb_descriptor_header **src)
  34. {
  35. u8 *dest = buf;
  36. if (!src)
  37. return -EINVAL;
  38. /* fill buffer from src[] until null descriptor ptr */
  39. for (; NULL != *src; src++) {
  40. unsigned len = (*src)->bLength;
  41. if (len > buflen)
  42. return -EINVAL;
  43. memcpy(dest, *src, len);
  44. buflen -= len;
  45. dest += len;
  46. }
  47. return dest - (u8 *)buf;
  48. }
  49. /**
  50. * usb_gadget_config_buf - builts a complete configuration descriptor
  51. * @config: Header for the descriptor, including characteristics such
  52. * as power requirements and number of interfaces.
  53. * @desc: Null-terminated vector of pointers to the descriptors (interface,
  54. * endpoint, etc) defining all functions in this device configuration.
  55. * @buf: Buffer for the resulting configuration descriptor.
  56. * @length: Length of buffer. If this is not big enough to hold the
  57. * entire configuration descriptor, an error code will be returned.
  58. *
  59. * This copies descriptors into the response buffer, building a descriptor
  60. * for that configuration. It returns the buffer length or a negative
  61. * status code. The config.wTotalLength field is set to match the length
  62. * of the result, but other descriptor fields (including power usage and
  63. * interface count) must be set by the caller.
  64. *
  65. * Gadget drivers could use this when constructing a config descriptor
  66. * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  67. * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  68. */
  69. int usb_gadget_config_buf(
  70. const struct usb_config_descriptor *config,
  71. void *buf,
  72. unsigned length,
  73. const struct usb_descriptor_header **desc
  74. )
  75. {
  76. struct usb_config_descriptor *cp = buf;
  77. int len;
  78. /* config descriptor first */
  79. if (length < USB_DT_CONFIG_SIZE || !desc)
  80. return -EINVAL;
  81. *cp = *config;
  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. cp->wTotalLength = cpu_to_le16(len);
  94. cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  95. return len;
  96. }
  97. /**
  98. * usb_copy_descriptors - copy a vector of USB descriptors
  99. * @src: null-terminated vector to copy
  100. * Context: initialization code, which may sleep
  101. *
  102. * This makes a copy of a vector of USB descriptors. Its primary use
  103. * is to support usb_function objects which can have multiple copies,
  104. * each needing different descriptors. Functions may have static
  105. * tables of descriptors, which are used as templates and customized
  106. * with identifiers (for interfaces, strings, endpoints, and more)
  107. * as needed by a given function instance.
  108. */
  109. struct usb_descriptor_header **
  110. usb_copy_descriptors(struct usb_descriptor_header **src)
  111. {
  112. struct usb_descriptor_header **tmp;
  113. unsigned bytes;
  114. unsigned n_desc;
  115. void *mem;
  116. struct usb_descriptor_header **ret;
  117. /* count descriptors and their sizes; then add vector size */
  118. for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
  119. bytes += (*tmp)->bLength;
  120. bytes += (n_desc + 1) * sizeof(*tmp);
  121. mem = kmalloc(bytes, GFP_KERNEL);
  122. if (!mem)
  123. return NULL;
  124. /* fill in pointers starting at "tmp",
  125. * to descriptors copied starting at "mem";
  126. * and return "ret"
  127. */
  128. tmp = mem;
  129. ret = mem;
  130. mem += (n_desc + 1) * sizeof(*tmp);
  131. while (*src) {
  132. memcpy(mem, *src, (*src)->bLength);
  133. *tmp = mem;
  134. tmp++;
  135. mem += (*src)->bLength;
  136. src++;
  137. }
  138. *tmp = NULL;
  139. return ret;
  140. }