config.c 6.4 KB

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