usb_descriptor.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-20 tfx2001 first version
  9. */
  10. #include "tusb.h"
  11. #include "usb_descriptor.h"
  12. //--------------------------------------------------------------------+
  13. // Device Descriptors
  14. //--------------------------------------------------------------------+
  15. static tusb_desc_device_t const desc_device =
  16. {
  17. .bLength = sizeof(tusb_desc_device_t),
  18. .bDescriptorType = TUSB_DESC_DEVICE,
  19. .bcdUSB = 0x0200,
  20. .bDeviceClass = 0x00,
  21. .bDeviceSubClass = 0x00,
  22. .bDeviceProtocol = 0x00,
  23. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  24. .idVendor = PKG_TINYUSB_DEVICE_VID,
  25. .idProduct = PKG_TINYUSB_DEVICE_PID,
  26. .bcdDevice = 0x0100,
  27. .iManufacturer = 0x01,
  28. .iProduct = 0x02,
  29. .iSerialNumber = 0x03,
  30. .bNumConfigurations = 0x01
  31. };
  32. // Invoked when received GET DEVICE DESCRIPTOR
  33. // Application return pointer to descriptor
  34. TU_ATTR_WEAK uint8_t const *tud_descriptor_device_cb(void)
  35. {
  36. return (uint8_t const *) &desc_device;
  37. }
  38. //--------------------------------------------------------------------+
  39. // HID Report Descriptor
  40. //--------------------------------------------------------------------+
  41. static uint8_t const desc_hid_report[] =
  42. {
  43. #ifdef PKG_TINYUSB_DEVICE_HID_KEYBOARD
  44. TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(REPORT_ID_KEYBOARD)),
  45. #endif
  46. #ifdef PKG_TINYUSB_DEVICE_HID_MOUSE
  47. TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(REPORT_ID_MOUSE)),
  48. #endif
  49. #ifdef PKG_TINYUSB_DEVICE_HID_CONSUMER
  50. TUD_HID_REPORT_DESC_CONSUMER(HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL)),
  51. #endif
  52. #ifdef PKG_TINYUSB_DEVICE_HID_GAMEPAD
  53. TUD_HID_REPORT_DESC_GAMEPAD(HID_REPORT_ID(REPORT_ID_GAMEPAD))
  54. #endif
  55. };
  56. // Invoked when received GET HID REPORT DESCRIPTOR
  57. // Application return pointer to descriptor
  58. // Descriptor contents must exist long enough for transfer to complete
  59. TU_ATTR_WEAK uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance)
  60. {
  61. (void) instance;
  62. return desc_hid_report;
  63. }
  64. //--------------------------------------------------------------------+
  65. // Configuration Descriptor
  66. //--------------------------------------------------------------------+
  67. static uint8_t const desc_fs_configuration[] =
  68. {
  69. TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, PKG_TINYUSB_DEVICE_CURRENT),
  70. #if CFG_TUD_CDC
  71. TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, CFG_TUD_CDC_EP_BUFSIZE),
  72. #endif
  73. #if CFG_TUD_MSC
  74. TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, TUD_OPT_HIGH_SPEED ? 512 : 64),
  75. #endif
  76. #if CFG_TUD_HID
  77. TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, PKG_TINYUSB_DEVICE_HID_INT)
  78. #endif
  79. };
  80. // Invoked when received GET CONFIGURATION DESCRIPTOR
  81. // Application return pointer to descriptor
  82. // Descriptor contents must exist long enough for transfer to complete
  83. TU_ATTR_WEAK uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
  84. {
  85. (void) index; // for multiple configurations
  86. return desc_fs_configuration;
  87. }
  88. //--------------------------------------------------------------------+
  89. // String Descriptors
  90. //--------------------------------------------------------------------+
  91. // array of pointer to string descriptors
  92. static char _serial_number[32] = "123456";
  93. static char *string_desc_arr[] =
  94. {
  95. (char[]) {0x09, 0x04}, // 0: is supported language is English (0x0409)
  96. PKG_TINYUSB_DEVICE_MANUFACTURER, // 1: Manufacturer
  97. PKG_TINYUSB_DEVICE_PRODUCT, // 2: Product
  98. _serial_number, // 3: Serials, should use chip ID
  99. PKG_TINYUSB_DEVICE_CDC_STRING,
  100. PKG_TINYUSB_DEVICE_MSC_STRING,
  101. PKG_TINYUSB_DEVICE_HID_STRING,
  102. };
  103. TU_ATTR_WEAK void tud_descriptor_set_serial(char *serial_number, uint8_t length)
  104. {
  105. if (length > 31) {
  106. length = 31;
  107. }
  108. rt_memcpy(_serial_number, serial_number, length);
  109. _serial_number[length] = '\0';
  110. }
  111. static uint16_t desc_str[32];
  112. // Invoked when received GET STRING DESCRIPTOR request
  113. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  114. TU_ATTR_WEAK uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
  115. {
  116. (void) langid;
  117. uint8_t chr_count;
  118. if (index == 0)
  119. {
  120. rt_memcpy(&desc_str[1], string_desc_arr[0], 2);
  121. chr_count = 1;
  122. }
  123. else
  124. {
  125. // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
  126. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
  127. if (index >= sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) return NULL;
  128. const char *str = string_desc_arr[index];
  129. // Cap at max char
  130. chr_count = strlen(str);
  131. if (chr_count > 31) chr_count = 31;
  132. // Convert ASCII string into UTF-16
  133. for (uint8_t i = 0; i < chr_count; i++)
  134. {
  135. desc_str[1 + i] = str[i];
  136. }
  137. }
  138. // first byte is length (including header), second byte is string type
  139. desc_str[0] = (TUSB_DESC_STRING << 8) | (2 * chr_count + 2);
  140. return desc_str;
  141. }