tusb_descriptors.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**************************************************************************/
  2. /*!
  3. @file tusb_descriptors.c
  4. @author hathach (tinyusb.org)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, hathach (tinyusb.org)
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  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 the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. This file is part of the tinyusb stack.
  30. */
  31. /**************************************************************************/
  32. #include "tusb_descriptors.h"
  33. //--------------------------------------------------------------------+
  34. // USB DEVICE DESCRIPTOR
  35. //--------------------------------------------------------------------+
  36. tusb_desc_device_t const desc_device =
  37. {
  38. .bLength = sizeof(tusb_desc_device_t),
  39. .bDescriptorType = TUSB_DESC_DEVICE,
  40. .bcdUSB = 0x0200,
  41. // Use Interface Association Descriptor (IAD) for CDC
  42. // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
  43. .bDeviceClass = TUSB_CLASS_MISC,
  44. .bDeviceSubClass = MISC_SUBCLASS_COMMON,
  45. .bDeviceProtocol = MISC_PROTOCOL_IAD,
  46. .bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
  47. .idVendor = CFG_VENDORID,
  48. .idProduct = CFG_PRODUCTID,
  49. .bcdDevice = 0x0100,
  50. .iManufacturer = 0x01,
  51. .iProduct = 0x02,
  52. .iSerialNumber = 0x03,
  53. .bNumConfigurations = 0x01
  54. };
  55. //--------------------------------------------------------------------+
  56. // USB COFNIGURATION DESCRIPTOR
  57. //--------------------------------------------------------------------+
  58. app_descriptor_configuration_t const desc_configuration =
  59. {
  60. .configuration =
  61. {
  62. .bLength = sizeof(tusb_desc_configuration_t),
  63. .bDescriptorType = TUSB_DESC_CONFIGURATION,
  64. .wTotalLength = sizeof(app_descriptor_configuration_t),
  65. .bNumInterfaces = ITF_TOTAL,
  66. .bConfigurationValue = 1,
  67. .iConfiguration = 0x00,
  68. .bmAttributes = TUSB_DESC_CONFIG_ATT_BUS_POWER,
  69. .bMaxPower = TUSB_DESC_CONFIG_POWER_MA(500)
  70. },
  71. // IAD points to CDC Interfaces
  72. .cdc_iad =
  73. {
  74. .bLength = sizeof(tusb_desc_interface_assoc_t),
  75. .bDescriptorType = TUSB_DESC_INTERFACE_ASSOCIATION,
  76. .bFirstInterface = ITF_NUM_CDC,
  77. .bInterfaceCount = 2,
  78. .bFunctionClass = TUSB_CLASS_CDC,
  79. .bFunctionSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL,
  80. .bFunctionProtocol = CDC_COMM_PROTOCOL_ATCOMMAND,
  81. .iFunction = 0
  82. },
  83. //------------- CDC Communication Interface -------------//
  84. .cdc_comm_interface =
  85. {
  86. .bLength = sizeof(tusb_desc_interface_t),
  87. .bDescriptorType = TUSB_DESC_INTERFACE,
  88. .bInterfaceNumber = ITF_NUM_CDC,
  89. .bAlternateSetting = 0,
  90. .bNumEndpoints = 1,
  91. .bInterfaceClass = TUSB_CLASS_CDC,
  92. .bInterfaceSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL,
  93. .bInterfaceProtocol = CDC_COMM_PROTOCOL_ATCOMMAND,
  94. .iInterface = 0x00
  95. },
  96. .cdc_header =
  97. {
  98. .bLength = sizeof(cdc_desc_func_header_t),
  99. .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
  100. .bDescriptorSubType = CDC_FUNC_DESC_HEADER,
  101. .bcdCDC = 0x0120
  102. },
  103. .cdc_call =
  104. {
  105. .bLength = sizeof(cdc_desc_func_call_management_t),
  106. .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
  107. .bDescriptorSubType = CDC_FUNC_DESC_CALL_MANAGEMENT,
  108. .bmCapabilities = { 0 },
  109. .bDataInterface = ITF_NUM_CDC+1,
  110. },
  111. .cdc_acm =
  112. {
  113. .bLength = sizeof(cdc_desc_func_acm_t),
  114. .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
  115. .bDescriptorSubType = CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT,
  116. .bmCapabilities = { // 0x02
  117. .support_line_request = 1,
  118. }
  119. },
  120. .cdc_union =
  121. {
  122. .bLength = sizeof(cdc_desc_func_union_t), // plus number of
  123. .bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
  124. .bDescriptorSubType = CDC_FUNC_DESC_UNION,
  125. .bControlInterface = ITF_NUM_CDC,
  126. .bSubordinateInterface = ITF_NUM_CDC+1,
  127. },
  128. .cdc_endpoint_notification =
  129. {
  130. .bLength = sizeof(tusb_desc_endpoint_t),
  131. .bDescriptorType = TUSB_DESC_ENDPOINT,
  132. .bEndpointAddress = CDC_EDPT_NOTIF,
  133. .bmAttributes = { .xfer = TUSB_XFER_INTERRUPT },
  134. .wMaxPacketSize = { .size = 0x08 },
  135. .bInterval = 0x10
  136. },
  137. //------------- CDC Data Interface -------------//
  138. .cdc_data_interface =
  139. {
  140. .bLength = sizeof(tusb_desc_interface_t),
  141. .bDescriptorType = TUSB_DESC_INTERFACE,
  142. .bInterfaceNumber = ITF_NUM_CDC+1,
  143. .bAlternateSetting = 0x00,
  144. .bNumEndpoints = 2,
  145. .bInterfaceClass = TUSB_CLASS_CDC_DATA,
  146. .bInterfaceSubClass = 0,
  147. .bInterfaceProtocol = 0,
  148. .iInterface = 0x00
  149. },
  150. .cdc_endpoint_out =
  151. {
  152. .bLength = sizeof(tusb_desc_endpoint_t),
  153. .bDescriptorType = TUSB_DESC_ENDPOINT,
  154. .bEndpointAddress = CDC_EDPT_OUT,
  155. .bmAttributes = { .xfer = TUSB_XFER_BULK },
  156. .wMaxPacketSize = { .size = CDC_EDPT_SIZE },
  157. .bInterval = 0
  158. },
  159. .cdc_endpoint_in =
  160. {
  161. .bLength = sizeof(tusb_desc_endpoint_t),
  162. .bDescriptorType = TUSB_DESC_ENDPOINT,
  163. .bEndpointAddress = CDC_EDPT_IN,
  164. .bmAttributes = { .xfer = TUSB_XFER_BULK },
  165. .wMaxPacketSize = { .size = CDC_EDPT_SIZE },
  166. .bInterval = 0
  167. },
  168. };
  169. //--------------------------------------------------------------------+
  170. // STRING DESCRIPTORS
  171. //--------------------------------------------------------------------+
  172. #define STRING_LEN_UNICODE(n) (2 + (2*(n))) // also includes 2 byte header
  173. #define ENDIAN_BE16_FROM( high, low) ENDIAN_BE16(high << 8 | low)
  174. // array of pointer to string descriptors
  175. uint16_t const * const string_descriptor_arr [] =
  176. {
  177. [0] = (uint16_t []) { // supported language
  178. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(1), TUSB_DESC_STRING ),
  179. 0x0409 // English
  180. },
  181. [1] = (uint16_t []) { // manufacturer
  182. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(11), TUSB_DESC_STRING),
  183. 't', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g' // len = 11
  184. },
  185. [2] = (uint16_t []) { // product
  186. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(14), TUSB_DESC_STRING),
  187. 't', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e' // len = 14
  188. },
  189. [3] = (uint16_t []) { // serials
  190. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(4), TUSB_DESC_STRING),
  191. '1', '2', '3', '4' // len = 4
  192. },
  193. [4] = (uint16_t []) { // CDC Interface
  194. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(3), TUSB_DESC_STRING),
  195. 'c', 'd', 'c' // len = 3
  196. },
  197. [5] = (uint16_t []) { // Keyboard Interface
  198. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(5), TUSB_DESC_STRING),
  199. 'm', 'o', 'u', 's', 'e' // len = 5
  200. },
  201. [6] = (uint16_t []) { // Keyboard Interface
  202. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(8), TUSB_DESC_STRING),
  203. 'k', 'e', 'y', 'b', 'o', 'a', 'r', 'd' // len = 8
  204. },
  205. [7] = (uint16_t []) { // MSC Interface
  206. ENDIAN_BE16_FROM( STRING_LEN_UNICODE(3), TUSB_DESC_STRING),
  207. 'm', 's', 'c' // len = 3
  208. }
  209. };
  210. /*------------- Variable used by tud_set_descriptors -------------*/
  211. tud_desc_init_t usb_desc_init =
  212. {
  213. .device = (uint8_t const * ) &desc_device,
  214. .configuration = (uint8_t const * ) &desc_configuration,
  215. .string_arr = (uint8_t const **) string_descriptor_arr,
  216. };