usb_descriptors.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 "bsp/board_api.h"
  26. #include "tusb.h"
  27. #include "usb_descriptors.h"
  28. /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
  29. * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
  30. *
  31. * Auto ProductID layout's Bitmap:
  32. * [MSB] HID | MSC | CDC [LSB]
  33. */
  34. #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
  35. #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
  36. _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
  37. #define USB_VID 0xCafe
  38. #define USB_BCD 0x0200
  39. //--------------------------------------------------------------------+
  40. // Device Descriptors
  41. //--------------------------------------------------------------------+
  42. tusb_desc_device_t const desc_device =
  43. {
  44. .bLength = sizeof(tusb_desc_device_t),
  45. .bDescriptorType = TUSB_DESC_DEVICE,
  46. .bcdUSB = USB_BCD,
  47. .bDeviceClass = 0x00,
  48. .bDeviceSubClass = 0x00,
  49. .bDeviceProtocol = 0x00,
  50. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  51. .idVendor = USB_VID,
  52. .idProduct = USB_PID,
  53. .bcdDevice = 0x0100,
  54. .iManufacturer = 0x01,
  55. .iProduct = 0x02,
  56. .iSerialNumber = 0x03,
  57. .bNumConfigurations = 0x01
  58. };
  59. // Invoked when received GET DEVICE DESCRIPTOR
  60. // Application return pointer to descriptor
  61. uint8_t const * tud_descriptor_device_cb(void)
  62. {
  63. return (uint8_t const *) &desc_device;
  64. }
  65. //--------------------------------------------------------------------+
  66. // HID Report Descriptor
  67. //--------------------------------------------------------------------+
  68. uint8_t const desc_hid_report[] =
  69. {
  70. TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )),
  71. // TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE )),
  72. // TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )),
  73. // TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD ))
  74. };
  75. // Invoked when received GET HID REPORT DESCRIPTOR
  76. // Application return pointer to descriptor
  77. // Descriptor contents must exist long enough for transfer to complete
  78. uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance)
  79. {
  80. (void) instance;
  81. return desc_hid_report;
  82. }
  83. //--------------------------------------------------------------------+
  84. // Configuration Descriptor
  85. //--------------------------------------------------------------------+
  86. enum
  87. {
  88. ITF_NUM_HID,
  89. ITF_NUM_TOTAL
  90. };
  91. #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN)
  92. #define EPNUM_HID 0x81
  93. uint8_t const desc_configuration[] =
  94. {
  95. // Config number, interface count, string index, total length, attribute, power in mA
  96. TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
  97. // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
  98. TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
  99. };
  100. #if TUD_OPT_HIGH_SPEED
  101. // Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration
  102. // other speed configuration
  103. uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
  104. // device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
  105. tusb_desc_device_qualifier_t const desc_device_qualifier =
  106. {
  107. .bLength = sizeof(tusb_desc_device_qualifier_t),
  108. .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
  109. .bcdUSB = USB_BCD,
  110. .bDeviceClass = 0x00,
  111. .bDeviceSubClass = 0x00,
  112. .bDeviceProtocol = 0x00,
  113. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  114. .bNumConfigurations = 0x01,
  115. .bReserved = 0x00
  116. };
  117. // Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
  118. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
  119. // device_qualifier descriptor describes information about a high-speed capable device that would
  120. // change if the device were operating at the other speed. If not highspeed capable stall this request.
  121. uint8_t const* tud_descriptor_device_qualifier_cb(void)
  122. {
  123. return (uint8_t const*) &desc_device_qualifier;
  124. }
  125. // Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
  126. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  127. // Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
  128. uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
  129. {
  130. (void) index; // for multiple configurations
  131. // other speed config is basically configuration with type = OHER_SPEED_CONFIG
  132. memcpy(desc_other_speed_config, desc_configuration, CONFIG_TOTAL_LEN);
  133. desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
  134. // this example use the same configuration for both high and full speed mode
  135. return desc_other_speed_config;
  136. }
  137. #endif // highspeed
  138. // Invoked when received GET CONFIGURATION DESCRIPTOR
  139. // Application return pointer to descriptor
  140. // Descriptor contents must exist long enough for transfer to complete
  141. uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
  142. {
  143. (void) index; // for multiple configurations
  144. // This example use the same configuration for both high and full speed mode
  145. return desc_configuration;
  146. }
  147. //--------------------------------------------------------------------+
  148. // String Descriptors
  149. //--------------------------------------------------------------------+
  150. // String Descriptor Index
  151. enum {
  152. STRID_LANGID = 0,
  153. STRID_MANUFACTURER,
  154. STRID_PRODUCT,
  155. STRID_SERIAL,
  156. };
  157. // array of pointer to string descriptors
  158. char const *string_desc_arr[] =
  159. {
  160. (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
  161. "TinyUSB", // 1: Manufacturer
  162. "TinyUSB Device", // 2: Product
  163. NULL, // 3: Serials will use unique ID if possible
  164. };
  165. static uint16_t _desc_str[32 + 1];
  166. // Invoked when received GET STRING DESCRIPTOR request
  167. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  168. uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
  169. (void) langid;
  170. size_t chr_count;
  171. switch ( index ) {
  172. case STRID_LANGID:
  173. memcpy(&_desc_str[1], string_desc_arr[0], 2);
  174. chr_count = 1;
  175. break;
  176. case STRID_SERIAL:
  177. chr_count = board_usb_get_serial(_desc_str + 1, 32);
  178. break;
  179. default:
  180. // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
  181. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
  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. size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type
  187. if ( chr_count > max_count ) chr_count = max_count;
  188. // Convert ASCII string into UTF-16
  189. for ( size_t i = 0; i < chr_count; i++ ) {
  190. _desc_str[1 + i] = str[i];
  191. }
  192. break;
  193. }
  194. // first byte is length (including header), second byte is string type
  195. _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
  196. return _desc_str;
  197. }