composite_desc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /****************************************************************************
  2. * drivers/usbdev/composite_desc.c
  3. *
  4. * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/usb/usbdev_trace.h>
  46. #include "composite.h"
  47. #ifdef CONFIG_USBDEV_COMPOSITE
  48. /****************************************************************************
  49. * Pre-processor Definitions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Private Types
  53. ****************************************************************************/
  54. #ifdef CONFIG_USBDEV_DUALSPEED
  55. typedef int16_t (*mkcfgdesc)(FAR uint8_t *buf, uint8_t speed, uint8_t type);
  56. #else
  57. typedef int16_t (*mkcfgdesc)(FAR uint8_t *buf);
  58. #endif
  59. /****************************************************************************
  60. * Private Function Prototypes
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Private Data
  64. ****************************************************************************/
  65. /* Device Descriptor */
  66. static const struct usb_devdesc_s g_devdesc =
  67. {
  68. USB_SIZEOF_DEVDESC, /* len */
  69. USB_DESC_TYPE_DEVICE, /* type */
  70. { /* usb */
  71. LSBYTE(0x0200),
  72. MSBYTE(0x0200)
  73. },
  74. #ifdef CONFIG_COMPOSITE_IAD
  75. USB_CLASS_MISC, /* classid */
  76. 2, /* subclass */
  77. 1, /* protocol */
  78. #else
  79. USB_CLASS_PER_INTERFACE, /* classid */
  80. 0, /* subclass */
  81. 0, /* protocol */
  82. #endif
  83. CONFIG_COMPOSITE_EP0MAXPACKET, /* maxpacketsize */
  84. {
  85. LSBYTE(CONFIG_COMPOSITE_VENDORID), /* vendor */
  86. MSBYTE(CONFIG_COMPOSITE_VENDORID)
  87. },
  88. {
  89. LSBYTE(CONFIG_COMPOSITE_PRODUCTID), /* product */
  90. MSBYTE(CONFIG_COMPOSITE_PRODUCTID)
  91. },
  92. {
  93. LSBYTE(CONFIG_COMPOSITE_VERSIONNO), /* device */
  94. MSBYTE(CONFIG_COMPOSITE_VERSIONNO)
  95. },
  96. COMPOSITE_MANUFACTURERSTRID, /* imfgr */
  97. COMPOSITE_PRODUCTSTRID, /* iproduct */
  98. COMPOSITE_SERIALSTRID, /* serno */
  99. COMPOSITE_NCONFIGS /* nconfigs */
  100. };
  101. /* Configuration descriptor for the composite device */
  102. static const struct usb_cfgdesc_s g_cfgdesc =
  103. {
  104. USB_SIZEOF_CFGDESC, /* len */
  105. USB_DESC_TYPE_CONFIG, /* type */
  106. {
  107. LSBYTE(COMPOSITE_CFGDESCSIZE), /* LS totallen */
  108. MSBYTE(COMPOSITE_CFGDESCSIZE) /* MS totallen */
  109. },
  110. COMPOSITE_NINTERFACES, /* ninterfaces */
  111. COMPOSITE_CONFIGID, /* cfgvalue */
  112. COMPOSITE_CONFIGSTRID, /* icfg */
  113. USB_CONFIG_ATTR_ONE|SELFPOWERED|REMOTEWAKEUP, /* attr */
  114. (CONFIG_USBDEV_MAXPOWER + 1) / 2 /* mxpower */
  115. };
  116. #ifdef CONFIG_USBDEV_DUALSPEED
  117. static const struct usb_qualdesc_s g_qualdesc =
  118. {
  119. USB_SIZEOF_QUALDESC, /* len */
  120. USB_DESC_TYPE_DEVICEQUALIFIER, /* type */
  121. { /* usb */
  122. LSBYTE(0x0200),
  123. MSBYTE(0x0200)
  124. },
  125. USB_CLASS_VENDOR_SPEC, /* classid */
  126. 0, /* subclass */
  127. 0, /* protocol */
  128. CONFIG_COMPOSITE_EP0MAXPACKET, /* mxpacketsize */
  129. COMPOSITE_NCONFIGS, /* nconfigs */
  130. 0, /* reserved */
  131. };
  132. #endif
  133. /****************************************************************************
  134. * Private Functions
  135. ****************************************************************************/
  136. /****************************************************************************
  137. * Public Functions
  138. ****************************************************************************/
  139. /****************************************************************************
  140. * Name: composite_mkstrdesc
  141. *
  142. * Description:
  143. * Construct a string descriptor
  144. *
  145. ****************************************************************************/
  146. int composite_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc)
  147. {
  148. const char *str;
  149. int len;
  150. int ndata;
  151. int i;
  152. switch (id)
  153. {
  154. case 0:
  155. {
  156. /* Descriptor 0 is the language id */
  157. strdesc->len = 4;
  158. strdesc->type = USB_DESC_TYPE_STRING;
  159. strdesc->data[0] = LSBYTE(COMPOSITE_STR_LANGUAGE);
  160. strdesc->data[1] = MSBYTE(COMPOSITE_STR_LANGUAGE);
  161. return 4;
  162. }
  163. case COMPOSITE_MANUFACTURERSTRID:
  164. str = g_compvendorstr;
  165. break;
  166. case COMPOSITE_PRODUCTSTRID:
  167. str = g_compproductstr;
  168. break;
  169. case COMPOSITE_SERIALSTRID:
  170. str = g_compserialstr;
  171. break;
  172. case COMPOSITE_CONFIGSTRID:
  173. str = CONFIG_COMPOSITE_CONFIGSTR;
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. /* The string is utf16-le. The poor man's utf-8 to utf16-le
  179. * conversion below will only handle 7-bit en-us ascii
  180. */
  181. len = strlen(str);
  182. for (i = 0, ndata = 0; i < len; i++, ndata += 2)
  183. {
  184. strdesc->data[ndata] = str[i];
  185. strdesc->data[ndata+1] = 0;
  186. }
  187. strdesc->len = ndata+2;
  188. strdesc->type = USB_DESC_TYPE_STRING;
  189. return strdesc->len;
  190. }
  191. /****************************************************************************
  192. * Name: composite_getepdesc
  193. *
  194. * Description:
  195. * Return a pointer to the raw device descriptor
  196. *
  197. ****************************************************************************/
  198. FAR const struct usb_devdesc_s *composite_getdevdesc(void)
  199. {
  200. return &g_devdesc;
  201. }
  202. /****************************************************************************
  203. * Name: composite_mkcfgdesc
  204. *
  205. * Description:
  206. * Construct the configuration descriptor
  207. *
  208. ****************************************************************************/
  209. #ifdef CONFIG_USBDEV_DUALSPEED
  210. int16_t composite_mkcfgdesc(uint8_t *buf, uint8_t speed, uint8_t type)
  211. #else
  212. int16_t composite_mkcfgdesc(uint8_t *buf)
  213. #endif
  214. {
  215. int16_t len;
  216. int16_t total;
  217. /* Configuration descriptor -- Copy the canned configuration descriptor. */
  218. memcpy(buf, &g_cfgdesc, USB_SIZEOF_CFGDESC);
  219. total = USB_SIZEOF_CFGDESC;
  220. buf += USB_SIZEOF_CFGDESC;
  221. /* Copy DEV1/DEV2 interface descriptors */
  222. #ifdef CONFIG_USBDEV_DUALSPEED
  223. len = DEV1_MKCFGDESC(buf, speed, type);
  224. total += len;
  225. buf += len;
  226. total += DEV2_MKCFGDESC(buf, speed, type);
  227. #else
  228. len = DEV1_MKCFGDESC(buf);
  229. total += len;
  230. buf += len;
  231. total += DEV2_MKCFGDESC(buf);
  232. #endif
  233. DEBUGASSERT(total == COMPOSITE_CFGDESCSIZE);
  234. return total;
  235. }
  236. /****************************************************************************
  237. * Name: composite_getqualdesc
  238. *
  239. * Description:
  240. * Return a pointer to the raw qual descriptor
  241. *
  242. ****************************************************************************/
  243. #ifdef CONFIG_USBDEV_DUALSPEED
  244. FAR const struct usb_qualdesc_s *composite_getqualdesc(void)
  245. {
  246. return &g_qualdesc;
  247. }
  248. #endif
  249. #endif /* CONFIG_USBDEV_COMPOSITE */