config.c 5.6 KB

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