config.c 5.6 KB

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