usb_descriptors.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. #include "usb_descriptors.h"
  27. /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
  28. * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
  29. *
  30. * Auto ProductID layout's Bitmap:
  31. * [MSB] MIDI | HID | MSC | CDC [LSB]
  32. */
  33. #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
  34. #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
  35. _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
  36. //--------------------------------------------------------------------+
  37. // Device Descriptors
  38. //--------------------------------------------------------------------+
  39. tusb_desc_device_t const desc_device =
  40. {
  41. .bLength = sizeof(tusb_desc_device_t),
  42. .bDescriptorType = TUSB_DESC_DEVICE,
  43. .bcdUSB = 0x0210, // at least 2.1 or 3.x for BOS & webUSB
  44. // Use Interface Association Descriptor (IAD) for CDC
  45. // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
  46. .bDeviceClass = TUSB_CLASS_MISC,
  47. .bDeviceSubClass = MISC_SUBCLASS_COMMON,
  48. .bDeviceProtocol = MISC_PROTOCOL_IAD,
  49. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  50. .idVendor = 0xCafe,
  51. .idProduct = USB_PID,
  52. .bcdDevice = 0x0100,
  53. .iManufacturer = 0x01,
  54. .iProduct = 0x02,
  55. .iSerialNumber = 0x03,
  56. .bNumConfigurations = 0x01
  57. };
  58. // Invoked when received GET DEVICE DESCRIPTOR
  59. // Application return pointer to descriptor
  60. uint8_t const * tud_descriptor_device_cb(void)
  61. {
  62. return (uint8_t const *) &desc_device;
  63. }
  64. //--------------------------------------------------------------------+
  65. // Configuration Descriptor
  66. //--------------------------------------------------------------------+
  67. enum
  68. {
  69. ITF_NUM_CDC = 0,
  70. ITF_NUM_CDC_DATA,
  71. ITF_NUM_VENDOR,
  72. ITF_NUM_TOTAL
  73. };
  74. #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_VENDOR_DESC_LEN)
  75. #if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX
  76. // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
  77. // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ...
  78. #define EPNUM_CDC 2
  79. #define EPNUM_VENDOR 5
  80. #else
  81. #define EPNUM_CDC 2
  82. #define EPNUM_VENDOR 3
  83. #endif
  84. uint8_t const desc_configuration[] =
  85. {
  86. // Config number, interface count, string index, total length, attribute, power in mA
  87. TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
  88. // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
  89. TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, EPNUM_CDC, 0x80 | EPNUM_CDC, 64),
  90. // Interface number, string index, EP Out & IN address, EP size
  91. TUD_VENDOR_DESCRIPTOR(ITF_NUM_VENDOR, 5, EPNUM_VENDOR, 0x80 | EPNUM_VENDOR, 64)
  92. };
  93. // Invoked when received GET CONFIGURATION DESCRIPTOR
  94. // Application return pointer to descriptor
  95. // Descriptor contents must exist long enough for transfer to complete
  96. uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
  97. {
  98. (void) index; // for multiple configurations
  99. return desc_configuration;
  100. }
  101. //--------------------------------------------------------------------+
  102. // BOS Descriptor
  103. //--------------------------------------------------------------------+
  104. /* Microsoft OS 2.0 registry property descriptor
  105. Per MS requirements https://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx
  106. device should create DeviceInterfaceGUIDs. It can be done by driver and
  107. in case of real PnP solution device should expose MS "Microsoft OS 2.0
  108. registry property descriptor". Such descriptor can insert any record
  109. into Windows registry per device/configuration/interface. In our case it
  110. will insert "DeviceInterfaceGUIDs" multistring property.
  111. GUID is freshly generated and should be OK to use.
  112. https://developers.google.com/web/fundamentals/native-hardware/build-for-webusb/
  113. (Section Microsoft OS compatibility descriptors)
  114. */
  115. #define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN)
  116. #define MS_OS_20_DESC_LEN 0xB2
  117. // BOS Descriptor is required for webUSB
  118. uint8_t const desc_bos[] =
  119. {
  120. // total length, number of device caps
  121. TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 2),
  122. // Vendor Code, iLandingPage
  123. TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1),
  124. // Microsoft OS 2.0 descriptor
  125. TUD_BOS_MS_OS_20_DESCRIPTOR(MS_OS_20_DESC_LEN, VENDOR_REQUEST_MICROSOFT)
  126. };
  127. uint8_t const * tud_descriptor_bos_cb(void)
  128. {
  129. return desc_bos;
  130. }
  131. uint8_t const desc_ms_os_20[] =
  132. {
  133. // Set header: length, type, windows version, total length
  134. U16_TO_U8S_LE(0x000A), U16_TO_U8S_LE(MS_OS_20_SET_HEADER_DESCRIPTOR), U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(MS_OS_20_DESC_LEN),
  135. // Configuration subset header: length, type, configuration index, reserved, configuration total length
  136. U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_CONFIGURATION), 0, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A),
  137. // Function Subset header: length, type, first interface, reserved, subset length
  138. U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), ITF_NUM_VENDOR, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08),
  139. // MS OS 2.0 Compatible ID descriptor: length, type, compatible ID, sub compatible ID
  140. U16_TO_U8S_LE(0x0014), U16_TO_U8S_LE(MS_OS_20_FEATURE_COMPATBLE_ID), 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00,
  141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sub-compatible
  142. // MS OS 2.0 Registry property descriptor: length, type
  143. U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08-0x08-0x14), U16_TO_U8S_LE(MS_OS_20_FEATURE_REG_PROPERTY),
  144. U16_TO_U8S_LE(0x0007), U16_TO_U8S_LE(0x002A), // wPropertyDataType, wPropertyNameLength and PropertyName "DeviceInterfaceGUIDs\0" in UTF-16
  145. 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00,
  146. 'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00, 0x00, 0x00,
  147. U16_TO_U8S_LE(0x0050), // wPropertyDataLength
  148. //bPropertyData: “{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}”.
  149. '{', 0x00, '9', 0x00, '7', 0x00, '5', 0x00, 'F', 0x00, '4', 0x00, '4', 0x00, 'D', 0x00, '9', 0x00, '-', 0x00,
  150. '0', 0x00, 'D', 0x00, '0', 0x00, '8', 0x00, '-', 0x00, '4', 0x00, '3', 0x00, 'F', 0x00, 'D', 0x00, '-', 0x00,
  151. '8', 0x00, 'B', 0x00, '3', 0x00, 'E', 0x00, '-', 0x00, '1', 0x00, '2', 0x00, '7', 0x00, 'C', 0x00, 'A', 0x00,
  152. '8', 0x00, 'A', 0x00, 'F', 0x00, 'F', 0x00, 'F', 0x00, '9', 0x00, 'D', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00
  153. };
  154. TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size");
  155. //--------------------------------------------------------------------+
  156. // String Descriptors
  157. //--------------------------------------------------------------------+
  158. // array of pointer to string descriptors
  159. char const* string_desc_arr [] =
  160. {
  161. (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
  162. "TinyUSB", // 1: Manufacturer
  163. "TinyUSB Device", // 2: Product
  164. "123456", // 3: Serials, should use chip ID
  165. "TinyUSB CDC", // 4: CDC Interface
  166. "TinyUSB WebUSB" // 5: Vendor Interface
  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. uint8_t chr_count;
  175. if ( index == 0)
  176. {
  177. memcpy(&_desc_str[1], string_desc_arr[0], 2);
  178. chr_count = 1;
  179. }else
  180. {
  181. // Convert ASCII string into UTF-16
  182. if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
  183. const char* str = string_desc_arr[index];
  184. // Cap at max char
  185. chr_count = strlen(str);
  186. if ( chr_count > 31 ) chr_count = 31;
  187. for(uint8_t i=0; i<chr_count; i++)
  188. {
  189. _desc_str[1+i] = str[i];
  190. }
  191. }
  192. // first byte is length (including header), second byte is string type
  193. _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
  194. return _desc_str;
  195. }