config.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 (; NULL != *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. }
  105. /**
  106. * usb_copy_descriptors - copy a vector of USB descriptors
  107. * @src: null-terminated vector to copy
  108. * Context: initialization code, which may sleep
  109. *
  110. * This makes a copy of a vector of USB descriptors. Its primary use
  111. * is to support usb_function objects which can have multiple copies,
  112. * each needing different descriptors. Functions may have static
  113. * tables of descriptors, which are used as templates and customized
  114. * with identifiers (for interfaces, strings, endpoints, and more)
  115. * as needed by a given function instance.
  116. */
  117. struct usb_descriptor_header **__init
  118. usb_copy_descriptors(struct usb_descriptor_header **src)
  119. {
  120. struct usb_descriptor_header **tmp;
  121. unsigned bytes;
  122. unsigned n_desc;
  123. void *mem;
  124. struct usb_descriptor_header **ret;
  125. /* count descriptors and their sizes; then add vector size */
  126. for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
  127. bytes += (*tmp)->bLength;
  128. bytes += (n_desc + 1) * sizeof(*tmp);
  129. mem = kmalloc(bytes, GFP_KERNEL);
  130. if (!mem)
  131. return NULL;
  132. /* fill in pointers starting at "tmp",
  133. * to descriptors copied starting at "mem";
  134. * and return "ret"
  135. */
  136. tmp = mem;
  137. ret = mem;
  138. mem += (n_desc + 1) * sizeof(*tmp);
  139. while (*src) {
  140. memcpy(mem, *src, (*src)->bLength);
  141. *tmp = mem;
  142. tmp++;
  143. mem += (*src)->bLength;
  144. src++;
  145. }
  146. *tmp = NULL;
  147. return ret;
  148. }
  149. /**
  150. * usb_find_endpoint - find a copy of an endpoint descriptor
  151. * @src: original vector of descriptors
  152. * @copy: copy of @src
  153. * @ep: endpoint descriptor found in @src
  154. *
  155. * This returns the copy of the @match descriptor made for @copy. Its
  156. * intended use is to help remembering the endpoint descriptor to use
  157. * when enabling a given endpoint.
  158. */
  159. struct usb_endpoint_descriptor *__init
  160. usb_find_endpoint(
  161. struct usb_descriptor_header **src,
  162. struct usb_descriptor_header **copy,
  163. struct usb_endpoint_descriptor *match
  164. )
  165. {
  166. while (*src) {
  167. if (*src == (void *) match)
  168. return (void *)*copy;
  169. src++;
  170. copy++;
  171. }
  172. return NULL;
  173. }