usb_descriptors.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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] 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. #define USB_VID 0xCafe
  37. #define USB_BCD 0x0200
  38. //--------------------------------------------------------------------+
  39. // Device Descriptors
  40. //--------------------------------------------------------------------+
  41. tusb_desc_device_t const desc_device =
  42. {
  43. .bLength = sizeof(tusb_desc_device_t),
  44. .bDescriptorType = TUSB_DESC_DEVICE,
  45. .bcdUSB = USB_BCD,
  46. .bDeviceClass = 0x00,
  47. .bDeviceSubClass = 0x00,
  48. .bDeviceProtocol = 0x00,
  49. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  50. .idVendor = USB_VID,
  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. // HID Report Descriptor
  66. //--------------------------------------------------------------------+
  67. uint8_t const desc_hid_report[] =
  68. {
  69. TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD ))
  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. ITF_NUM_HID,
  85. ITF_NUM_TOTAL
  86. };
  87. #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN)
  88. #define EPNUM_HID 0x81
  89. uint8_t const desc_configuration[] =
  90. {
  91. // Config number, interface count, string index, total length, attribute, power in mA
  92. TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
  93. // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
  94. TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
  95. };
  96. #if TUD_OPT_HIGH_SPEED
  97. // Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration
  98. // other speed configuration
  99. uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
  100. // device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
  101. tusb_desc_device_qualifier_t const desc_device_qualifier =
  102. {
  103. .bLength = sizeof(tusb_desc_device_qualifier_t),
  104. .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
  105. .bcdUSB = USB_BCD,
  106. .bDeviceClass = 0x00,
  107. .bDeviceSubClass = 0x00,
  108. .bDeviceProtocol = 0x00,
  109. .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
  110. .bNumConfigurations = 0x01,
  111. .bReserved = 0x00
  112. };
  113. // Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
  114. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
  115. // device_qualifier descriptor describes information about a high-speed capable device that would
  116. // change if the device were operating at the other speed. If not highspeed capable stall this request.
  117. uint8_t const* tud_descriptor_device_qualifier_cb(void)
  118. {
  119. return (uint8_t const*) &desc_device_qualifier;
  120. }
  121. // Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
  122. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  123. // Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa
  124. uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
  125. {
  126. (void) index; // for multiple configurations
  127. // other speed config is basically configuration with type = OHER_SPEED_CONFIG
  128. memcpy(desc_other_speed_config, desc_configuration, CONFIG_TOTAL_LEN);
  129. desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
  130. // this example use the same configuration for both high and full speed mode
  131. return desc_other_speed_config;
  132. }
  133. #endif // highspeed
  134. // Invoked when received GET CONFIGURATION DESCRIPTOR
  135. // Application return pointer to descriptor
  136. // Descriptor contents must exist long enough for transfer to complete
  137. uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
  138. {
  139. (void) index; // for multiple configurations
  140. // This example use the same configuration for both high and full speed mode
  141. return desc_configuration;
  142. }
  143. //--------------------------------------------------------------------+
  144. // String Descriptors
  145. //--------------------------------------------------------------------+
  146. // array of pointer to string descriptors
  147. char const* string_desc_arr [] =
  148. {
  149. (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
  150. "Danielinux", // 1: Manufacturer
  151. "Granite-arcade-JS", // 2: Product
  152. "031980", // 3: Serials, should use chip ID
  153. };
  154. static uint16_t _desc_str[32];
  155. // Invoked when received GET STRING DESCRIPTOR request
  156. // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
  157. uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
  158. {
  159. (void) langid;
  160. uint8_t chr_count;
  161. if ( index == 0)
  162. {
  163. memcpy(&_desc_str[1], string_desc_arr[0], 2);
  164. chr_count = 1;
  165. }else
  166. {
  167. // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
  168. // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
  169. if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL;
  170. const char* str = string_desc_arr[index];
  171. // Cap at max char
  172. chr_count = strlen(str);
  173. if ( chr_count > 31 ) chr_count = 31;
  174. // Convert ASCII string into UTF-16
  175. for(uint8_t i=0; i<chr_count; i++)
  176. {
  177. _desc_str[1+i] = str[i];
  178. }
  179. }
  180. // first byte is length (including header), second byte is string type
  181. _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
  182. return _desc_str;
  183. }