config.c 7.5 KB

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