usb_descriptor.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_descriptor.h"
  27. //--------------------------------------------------------------------+
  28. // Device Descriptors
  29. //--------------------------------------------------------------------+
  30. tusb_desc_device_t const desc_device =
  31. {
  32. .bLength = sizeof(tusb_desc_device_t),
  33. .bDescriptorType = TUSB_DESC_DEVICE,
  34. .bcdUSB = 0x0200,
  35. .bDeviceClass = 0x00,
  36. .bDeviceSubClass = 0x00,
  37. .bDeviceProtocol = 0x00,
  38. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  39. .idVendor = PKG_TINYUSB_DEVICE_VID,
  40. .idProduct = PKG_TINYUSB_DEVICE_PID,
  41. .bcdDevice = 0x0100,
  42. .iManufacturer = 0x01,
  43. .iProduct = 0x02,
  44. .iSerialNumber = 0x03,
  45. .bNumConfigurations = 0x01
  46. };
  47. // Invoked when received GET DEVICE DESCRIPTOR
  48. // Application return pointer to descriptor
  49. uint8_t const *tud_descriptor_device_cb(void)
  50. {
  51. return (uint8_t const *) &desc_device;
  52. }
  53. //--------------------------------------------------------------------+
  54. // HID Report Descriptor
  55. //--------------------------------------------------------------------+
  56. uint8_t const desc_hid_report[] =
  57. {
  58. #ifdef PKG_TINYUSB_DEVICE_HID_KEYBOARD
  59. TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(REPORT_ID_KEYBOARD)),
  60. #endif
  61. #ifdef PKG_TINYUSB_DEVICE_HID_MOUSE
  62. TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(REPORT_ID_MOUSE)),
  63. #endif
  64. #ifdef PKG_TINYUSB_DEVICE_HID_CONSUMER
  65. TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL)),
  66. #endif
  67. #ifdef PKG_TINYUSB_DEVICE_HID_GAMEPAD
  68. TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(REPORT_ID_GAMEPAD))
  69. #endif
  70. };
  71. // Invoked when received GET HID REPORT DESCRIPTOR
  72. // Application return pointer to descriptor
  73. // Descriptor contents must exist long enough for transfer to complete
  74. uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance)
  75. {
  76. (void)instance;
  77. return desc_hid_report;
  78. }
  79. //--------------------------------------------------------------------+
  80. // Configuration Descriptor
  81. //--------------------------------------------------------------------+
  82. enum
  83. {
  84. #if CFG_TUD_CDC
  85. ITF_NUM_CDC = 0,
  86. ITF_NUM_CDC_DATA,
  87. #endif
  88. #if CFG_TUD_MSC
  89. ITF_NUM_MSC,
  90. #endif
  91. #if CFG_TUD_HID
  92. ITF_NUM_HID,
  93. #endif
  94. ITF_NUM_TOTAL
  95. };
  96. #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \
  97. TUD_MSC_DESC_LEN * CFG_TUD_MSC + \
  98. TUD_CDC_DESC_LEN * CFG_TUD_CDC + \
  99. TUD_HID_DESC_LEN * CFG_TUD_HID)
  100. #define EPNUM_CDC_NOTIF 0x81
  101. #define EPNUM_CDC_OUT 0x02
  102. #define EPNUM_CDC_IN 0x82
  103. #define EPNUM_MSC_OUT 0x03
  104. #define EPNUM_MSC_IN 0x83
  105. #define EPNUM_HID 0x84
  106. uint8_t const desc_fs_configuration[] =
  107. {
  108. TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
  109. #if CFG_TUD_CDC
  110. TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
  111. #endif
  112. #if CFG_TUD_MSC
  113. TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
  114. #endif
  115. #if CFG_TUD_HID
  116. TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
  117. #endif
  118. };
  119. // Invoked when received GET CONFIGURATION DESCRIPTOR
  120. // Application return pointer to descriptor
  121. // Descriptor contents must exist long enough for transfer to complete
  122. uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
  123. {
  124. (void) index; // for multiple configurations
  125. return desc_fs_configuration;
  126. }
  127. //--------------------------------------------------------------------+
  128. // String Descriptors
  129. //--------------------------------------------------------------------+
  130. // array of pointer to string descriptors
  131. char const *string_desc_arr[] =
  132. {
  133. (const char[]) {0x09, 0x04}, // 0: is supported language is English (0x0409)
  134. "TinyUSB", // 1: Manufacturer
  135. "TinyUSB Device", // 2: Product
  136. "123456", // 3: Serials, should use chip ID
  137. "TinyUSB CDC",
  138. "TinyUSB MSC",
  139. "TinyUSB HID",
  140. };
  141. static uint16_t desc_str[32];
  142. // Invoked when received GET STRING DESCRIPTOR request
  143. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  144. uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
  145. {
  146. (void) langid;
  147. uint8_t chr_count;
  148. if (index == 0)
  149. {
  150. memcpy(&desc_str[1], string_desc_arr[0], 2);
  151. chr_count = 1;
  152. }
  153. else
  154. {
  155. // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
  156. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
  157. if (index >= sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) return NULL;
  158. const char *str = string_desc_arr[index];
  159. // Cap at max char
  160. chr_count = strlen(str);
  161. if (chr_count > 31) chr_count = 31;
  162. // Convert ASCII string into UTF-16
  163. for (uint8_t i = 0; i < chr_count; i++)
  164. {
  165. desc_str[1 + i] = str[i];
  166. }
  167. }
  168. // first byte is length (including header), second byte is string type
  169. desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * chr_count + 2);
  170. return desc_str;
  171. }