usb_common.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * SPDX-FileCopyrightText: 2015,2016 Intel Corporation
  3. * SPDX-FileContributor: 2017 PHYTEC Messtechnik GmbH
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. /**
  8. * @file
  9. * @brief useful constants and macros for the USB application
  10. *
  11. * This file contains useful constants and macros for the USB applications.
  12. */
  13. #pragma once
  14. #include <stdint.h>
  15. #include <sys/cdefs.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define BCD(x) ((((x) / 10) << 4) | ((x) / 10))
  20. /* Descriptor size in bytes */
  21. #define USB_DEVICE_DESC_SIZE 18
  22. #define USB_CONFIGURATION_DESC_SIZE 9
  23. #define USB_INTERFACE_DESC_SIZE 9
  24. #define USB_ENDPOINT_DESC_SIZE 7
  25. #define USB_STRING_DESC_SIZE 4
  26. #define USB_HID_DESC_SIZE 9
  27. #define USB_DFU_DESC_SIZE 9
  28. #define USB_DEVICE_QUAL_DESC_SIZE 10
  29. #define USB_INTERFACE_ASSOC_DESC_SIZE 8
  30. /* Descriptor type */
  31. #define USB_DEVICE_DESC 0x01
  32. #define USB_CONFIGURATION_DESC 0x02
  33. #define USB_STRING_DESC 0x03
  34. #define USB_INTERFACE_DESC 0x04
  35. #define USB_ENDPOINT_DESC 0x05
  36. #define USB_DEVICE_QUAL_DESC 0x06
  37. #define USB_INTERFACE_ASSOC_DESC 0x0B
  38. #define USB_DEVICE_CAPABILITY_DESC 0x10
  39. #define USB_HID_DESC 0x21
  40. #define USB_HID_REPORT_DESC 0x22
  41. #define USB_DFU_FUNCTIONAL_DESC 0x21
  42. #define USB_ASSOCIATION_DESC 0x0B
  43. #define USB_BINARY_OBJECT_STORE_DESC 0x0F
  44. /* Useful define */
  45. #define USB_1_1 0x0110
  46. #define USB_2_0 0x0200
  47. /* Set USB version to 2.1 so that the host will request the BOS descriptor */
  48. #define USB_2_1 0x0210
  49. #define BCDDEVICE_RELNUM (BCD(KERNEL_VERSION_MAJOR) << 8 | \
  50. BCD(KERNEL_VERSION_MINOR))
  51. /* 100mA max power, per 2mA units */
  52. /* USB 1.1 spec indicates 100mA(max) per unit load, up to 5 loads */
  53. #define MAX_LOW_POWER 0x32
  54. #define MAX_HIGH_POWER 0xFA
  55. /* bmAttributes:
  56. * D7:Reserved, always 1,
  57. * D6:Self-Powered -> 1,
  58. * D5:Remote Wakeup -> 0,
  59. * D4...0:Reserved -> 0
  60. */
  61. #define USB_CONFIGURATION_ATTRIBUTES 0xC0
  62. /* Classes */
  63. #define COMMUNICATION_DEVICE_CLASS 0x02
  64. #define COMMUNICATION_DEVICE_CLASS_DATA 0x0A
  65. #define HID_CLASS 0x03
  66. #define MASS_STORAGE_CLASS 0x08
  67. #define WIRELESS_DEVICE_CLASS 0xE0
  68. #define MISC_CLASS 0xEF
  69. #define CUSTOM_CLASS 0xFF
  70. #define DFU_DEVICE_CLASS 0xFE
  71. /* Sub-classes */
  72. #define CDC_NCM_SUBCLASS 0x0d
  73. #define BOOT_INTERFACE_SUBCLASS 0x01
  74. #define SCSI_TRANSPARENT_SUBCLASS 0x06
  75. #define DFU_INTERFACE_SUBCLASS 0x01
  76. #define RF_SUBCLASS 0x01
  77. #define CUSTOM_SUBCLASS 0xFF
  78. #define COMMON_SUBCLASS 0x02
  79. /* Misc subclasses */
  80. #define MISC_RNDIS_SUBCLASS 0x04
  81. #define CDC_ABSTRACT_CONTROL_MODEL 0x02
  82. /* Protocols */
  83. #define V25TER_PROTOCOL 0x01
  84. #define MOUSE_PROTOCOL 0x02
  85. #define BULK_ONLY_PROTOCOL 0x50
  86. #define DFU_RUNTIME_PROTOCOL 0x01
  87. #define DFU_MODE_PROTOCOL 0x02
  88. #define BLUETOOTH_PROTOCOL 0x01
  89. /* CDC ACM protocols */
  90. #define ACM_VENDOR_PROTOCOL 0xFF
  91. /* Misc protocols */
  92. #define MISC_ETHERNET_PROTOCOL 0x01
  93. #define IAD_PROTOCOL 0x01
  94. /** Standard Device Descriptor */
  95. struct usb_device_descriptor {
  96. uint8_t bLength;
  97. uint8_t bDescriptorType;
  98. uint16_t bcdUSB;
  99. uint8_t bDeviceClass;
  100. uint8_t bDeviceSubClass;
  101. uint8_t bDeviceProtocol;
  102. uint8_t bMaxPacketSize0;
  103. uint16_t idVendor;
  104. uint16_t idProduct;
  105. uint16_t bcdDevice;
  106. uint8_t iManufacturer;
  107. uint8_t iProduct;
  108. uint8_t iSerialNumber;
  109. uint8_t bNumConfigurations;
  110. } __packed;
  111. /** Unicode (UTF16LE) String Descriptor */
  112. struct usb_string_descriptor {
  113. uint8_t bLength;
  114. uint8_t bDescriptorType;
  115. uint16_t bString;
  116. } __packed;
  117. /** Association Descriptor */
  118. struct usb_association_descriptor {
  119. uint8_t bLength;
  120. uint8_t bDescriptorType;
  121. uint8_t bFirstInterface;
  122. uint8_t bInterfaceCount;
  123. uint8_t bFunctionClass;
  124. uint8_t bFunctionSubClass;
  125. uint8_t bFunctionProtocol;
  126. uint8_t iFunction;
  127. } __packed;
  128. /** Standard Configuration Descriptor */
  129. struct usb_cfg_descriptor {
  130. uint8_t bLength;
  131. uint8_t bDescriptorType;
  132. uint16_t wTotalLength;
  133. uint8_t bNumInterfaces;
  134. uint8_t bConfigurationValue;
  135. uint8_t iConfiguration;
  136. uint8_t bmAttributes;
  137. uint8_t bMaxPower;
  138. } __packed;
  139. /** Standard Interface Descriptor */
  140. struct usb_if_descriptor {
  141. uint8_t bLength;
  142. uint8_t bDescriptorType;
  143. uint8_t bInterfaceNumber;
  144. uint8_t bAlternateSetting;
  145. uint8_t bNumEndpoints;
  146. uint8_t bInterfaceClass;
  147. uint8_t bInterfaceSubClass;
  148. uint8_t bInterfaceProtocol;
  149. uint8_t iInterface;
  150. } __packed;
  151. /** Standard Endpoint Descriptor */
  152. struct usb_ep_descriptor {
  153. uint8_t bLength;
  154. uint8_t bDescriptorType;
  155. uint8_t bEndpointAddress;
  156. uint8_t bmAttributes;
  157. uint16_t wMaxPacketSize;
  158. uint8_t bInterval;
  159. } __packed;
  160. struct string_descriptor_zero {
  161. uint8_t bLength;
  162. uint8_t bDescriptorType;
  163. uint16_t wBcdLang[];
  164. } __packed;
  165. struct string_descriptor {
  166. uint8_t bLength;
  167. uint8_t bDescriptorType;
  168. uint16_t bString[];
  169. } __packed;
  170. #define ROM_MAX_CFG_DESC_CNT 1
  171. struct rom_usb_descriptors {
  172. const struct usb_device_descriptor *device_descr;
  173. const void *config_descr[ROM_MAX_CFG_DESC_CNT];
  174. int string_count; // including string_descriptor_zero
  175. const struct string_descriptor_zero *string0_descr;
  176. const struct string_descriptor *string_descrs[];
  177. };
  178. /* Descriptors defined in the ROM */
  179. extern struct usb_device_descriptor general_device_descr;
  180. extern const void* acm_config_descr;
  181. extern const void* dfu_config_descr;
  182. extern const struct string_descriptor str_manu_descr;
  183. extern const struct string_descriptor str_prod_descr;
  184. extern const struct string_descriptor_zero string0_descr;
  185. extern const struct rom_usb_descriptors acm_usb_descriptors;
  186. extern const struct rom_usb_descriptors dfu_usb_descriptors;
  187. extern const struct rom_usb_descriptors *rom_usb_curr_desc;
  188. /* ROM patch: set the ACM descriptor with the correct serial number.
  189. * Only needed on ESP32-S2, on later chips the ROM descriptor is correct.
  190. */
  191. void rom_usb_cdc_set_descriptor_patch(void);
  192. #ifdef __cplusplus
  193. }
  194. #endif