usb_descriptors.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include "tusb.h"
  26. /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
  27. * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
  28. *
  29. * Auto ProductID layout's Bitmap:
  30. * [MSB] NET | VENDOR | MIDI | HID | MSC | CDC [LSB]
  31. */
  32. #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
  33. #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
  34. _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) | _PID_MAP(ECM_RNDIS, 5) | _PID_MAP(NCM, 5) )
  35. // String Descriptor Index
  36. enum
  37. {
  38. STRID_LANGID = 0,
  39. STRID_MANUFACTURER,
  40. STRID_PRODUCT,
  41. STRID_SERIAL,
  42. STRID_INTERFACE,
  43. STRID_MAC
  44. };
  45. enum
  46. {
  47. ITF_NUM_CDC = 0,
  48. ITF_NUM_CDC_DATA,
  49. ITF_NUM_TOTAL
  50. };
  51. enum
  52. {
  53. #if CFG_TUD_ECM_RNDIS
  54. CONFIG_ID_RNDIS = 0,
  55. CONFIG_ID_ECM = 1,
  56. #else
  57. CONFIG_ID_NCM = 0,
  58. #endif
  59. CONFIG_ID_COUNT
  60. };
  61. //--------------------------------------------------------------------+
  62. // Device Descriptors
  63. //--------------------------------------------------------------------+
  64. tusb_desc_device_t const desc_device =
  65. {
  66. .bLength = sizeof(tusb_desc_device_t),
  67. .bDescriptorType = TUSB_DESC_DEVICE,
  68. .bcdUSB = 0x0200,
  69. // Use Interface Association Descriptor (IAD) device class
  70. .bDeviceClass = TUSB_CLASS_MISC,
  71. .bDeviceSubClass = MISC_SUBCLASS_COMMON,
  72. .bDeviceProtocol = MISC_PROTOCOL_IAD,
  73. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  74. .idVendor = 0xCafe,
  75. .idProduct = USB_PID,
  76. .bcdDevice = 0x0101,
  77. .iManufacturer = STRID_MANUFACTURER,
  78. .iProduct = STRID_PRODUCT,
  79. .iSerialNumber = STRID_SERIAL,
  80. .bNumConfigurations = CONFIG_ID_COUNT // multiple configurations
  81. };
  82. // Invoked when received GET DEVICE DESCRIPTOR
  83. // Application return pointer to descriptor
  84. uint8_t const * tud_descriptor_device_cb(void)
  85. {
  86. return (uint8_t const *) &desc_device;
  87. }
  88. //--------------------------------------------------------------------+
  89. // Configuration Descriptor
  90. //--------------------------------------------------------------------+
  91. #define MAIN_CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_RNDIS_DESC_LEN)
  92. #define ALT_CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_ECM_DESC_LEN)
  93. #define NCM_CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_NCM_DESC_LEN)
  94. #if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
  95. // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
  96. // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ...
  97. #define EPNUM_NET_NOTIF 0x81
  98. #define EPNUM_NET_OUT 0x02
  99. #define EPNUM_NET_IN 0x82
  100. #elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X
  101. // SAMG & SAME70 don't support a same endpoint number with different direction IN and OUT
  102. // e.g EP1 OUT & EP1 IN cannot exist together
  103. #define EPNUM_NET_NOTIF 0x81
  104. #define EPNUM_NET_OUT 0x02
  105. #define EPNUM_NET_IN 0x83
  106. #else
  107. #define EPNUM_NET_NOTIF 0x81
  108. #define EPNUM_NET_OUT 0x02
  109. #define EPNUM_NET_IN 0x82
  110. #endif
  111. #if CFG_TUD_ECM_RNDIS
  112. static uint8_t const rndis_configuration[] =
  113. {
  114. // Config number (index+1), interface count, string index, total length, attribute, power in mA
  115. TUD_CONFIG_DESCRIPTOR(CONFIG_ID_RNDIS+1, ITF_NUM_TOTAL, 0, MAIN_CONFIG_TOTAL_LEN, 0, 100),
  116. // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
  117. TUD_RNDIS_DESCRIPTOR(ITF_NUM_CDC, STRID_INTERFACE, EPNUM_NET_NOTIF, 8, EPNUM_NET_OUT, EPNUM_NET_IN, CFG_TUD_NET_ENDPOINT_SIZE),
  118. };
  119. static uint8_t const ecm_configuration[] =
  120. {
  121. // Config number (index+1), interface count, string index, total length, attribute, power in mA
  122. TUD_CONFIG_DESCRIPTOR(CONFIG_ID_ECM+1, ITF_NUM_TOTAL, 0, ALT_CONFIG_TOTAL_LEN, 0, 100),
  123. // Interface number, description string index, MAC address string index, EP notification address and size, EP data address (out, in), and size, max segment size.
  124. TUD_CDC_ECM_DESCRIPTOR(ITF_NUM_CDC, STRID_INTERFACE, STRID_MAC, EPNUM_NET_NOTIF, 64, EPNUM_NET_OUT, EPNUM_NET_IN, CFG_TUD_NET_ENDPOINT_SIZE, CFG_TUD_NET_MTU),
  125. };
  126. #else
  127. static uint8_t const ncm_configuration[] =
  128. {
  129. // Config number (index+1), interface count, string index, total length, attribute, power in mA
  130. TUD_CONFIG_DESCRIPTOR(CONFIG_ID_NCM+1, ITF_NUM_TOTAL, 0, NCM_CONFIG_TOTAL_LEN, 0, 100),
  131. // Interface number, description string index, MAC address string index, EP notification address and size, EP data address (out, in), and size, max segment size.
  132. TUD_CDC_NCM_DESCRIPTOR(ITF_NUM_CDC, STRID_INTERFACE, STRID_MAC, EPNUM_NET_NOTIF, 64, EPNUM_NET_OUT, EPNUM_NET_IN, CFG_TUD_NET_ENDPOINT_SIZE, CFG_TUD_NET_MTU),
  133. };
  134. #endif
  135. // Configuration array: RNDIS and CDC-ECM
  136. // - Windows only works with RNDIS
  137. // - MacOS only works with CDC-ECM
  138. // - Linux will work on both
  139. static uint8_t const * const configuration_arr[2] =
  140. {
  141. #if CFG_TUD_ECM_RNDIS
  142. [CONFIG_ID_RNDIS] = rndis_configuration,
  143. [CONFIG_ID_ECM ] = ecm_configuration
  144. #else
  145. [CONFIG_ID_NCM ] = ncm_configuration
  146. #endif
  147. };
  148. // Invoked when received GET CONFIGURATION DESCRIPTOR
  149. // Application return pointer to descriptor
  150. // Descriptor contents must exist long enough for transfer to complete
  151. uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
  152. {
  153. return (index < CONFIG_ID_COUNT) ? configuration_arr[index] : NULL;
  154. }
  155. //--------------------------------------------------------------------+
  156. // String Descriptors
  157. //--------------------------------------------------------------------+
  158. // array of pointer to string descriptors
  159. static char const* string_desc_arr [] =
  160. {
  161. [STRID_LANGID] = (const char[]) { 0x09, 0x04 }, // supported language is English (0x0409)
  162. [STRID_MANUFACTURER] = "TinyUSB", // Manufacturer
  163. [STRID_PRODUCT] = "TinyUSB Device", // Product
  164. [STRID_SERIAL] = "123456", // Serial
  165. [STRID_INTERFACE] = "TinyUSB Network Interface" // Interface Description
  166. // STRID_MAC index is handled separately
  167. };
  168. static uint16_t _desc_str[32];
  169. // Invoked when received GET STRING DESCRIPTOR request
  170. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  171. uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
  172. {
  173. (void) langid;
  174. unsigned int chr_count = 0;
  175. if (STRID_LANGID == index)
  176. {
  177. memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2);
  178. chr_count = 1;
  179. }
  180. else if (STRID_MAC == index)
  181. {
  182. // Convert MAC address into UTF-16
  183. for (unsigned i=0; i<sizeof(tud_network_mac_address); i++)
  184. {
  185. _desc_str[1+chr_count++] = "0123456789ABCDEF"[(tud_network_mac_address[i] >> 4) & 0xf];
  186. _desc_str[1+chr_count++] = "0123456789ABCDEF"[(tud_network_mac_address[i] >> 0) & 0xf];
  187. }
  188. }
  189. else
  190. {
  191. // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
  192. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
  193. if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
  194. const char* str = string_desc_arr[index];
  195. // Cap at max char
  196. chr_count = (uint8_t) strlen(str);
  197. if ( chr_count > (TU_ARRAY_SIZE(_desc_str) - 1)) chr_count = TU_ARRAY_SIZE(_desc_str) - 1;
  198. // Convert ASCII string into UTF-16
  199. for (unsigned int i=0; i<chr_count; i++)
  200. {
  201. _desc_str[1+i] = str[i];
  202. }
  203. }
  204. // first byte is length (including header), second byte is string type
  205. _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2));
  206. return _desc_str;
  207. }