usb_common.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /***************************************************************************
  2. *
  3. *
  4. * Copyright(c) 2015,2016 Intel Corporation.
  5. * Copyright(c) 2017 PHYTEC Messtechnik GmbH
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * * Neither the name of Intel Corporation nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ***************************************************************************/
  34. /**
  35. * @file
  36. * @brief useful constants and macros for the USB application
  37. *
  38. * This file contains useful constants and macros for the USB applications.
  39. */
  40. #include <version.h>
  41. #ifndef USB_COMMON_H_
  42. #define USB_COMMON_H_
  43. #define BCD(x) ((((x) / 10) << 4) | ((x) / 10))
  44. /* Descriptor size in bytes */
  45. #define USB_DEVICE_DESC_SIZE 18
  46. #define USB_CONFIGURATION_DESC_SIZE 9
  47. #define USB_INTERFACE_DESC_SIZE 9
  48. #define USB_ENDPOINT_DESC_SIZE 7
  49. #define USB_STRING_DESC_SIZE 4
  50. #define USB_HID_DESC_SIZE 9
  51. #define USB_DFU_DESC_SIZE 9
  52. #define USB_DEVICE_QUAL_DESC_SIZE 10
  53. #define USB_INTERFACE_ASSOC_DESC_SIZE 8
  54. /* Descriptor type */
  55. #define USB_DEVICE_DESC 0x01
  56. #define USB_CONFIGURATION_DESC 0x02
  57. #define USB_STRING_DESC 0x03
  58. #define USB_INTERFACE_DESC 0x04
  59. #define USB_ENDPOINT_DESC 0x05
  60. #define USB_DEVICE_QUAL_DESC 0x06
  61. #define USB_INTERFACE_ASSOC_DESC 0x0B
  62. #define USB_DEVICE_CAPABILITY_DESC 0x10
  63. #define USB_HID_DESC 0x21
  64. #define USB_HID_REPORT_DESC 0x22
  65. #define USB_DFU_FUNCTIONAL_DESC 0x21
  66. #define USB_ASSOCIATION_DESC 0x0B
  67. #define USB_BINARY_OBJECT_STORE_DESC 0x0F
  68. /* Useful define */
  69. #define USB_1_1 0x0110
  70. #define USB_2_0 0x0200
  71. /* Set USB version to 2.1 so that the host will request the BOS descriptor */
  72. #define USB_2_1 0x0210
  73. #define BCDDEVICE_RELNUM (BCD(KERNEL_VERSION_MAJOR) << 8 | \
  74. BCD(KERNEL_VERSION_MINOR))
  75. /* 100mA max power, per 2mA units */
  76. /* USB 1.1 spec indicates 100mA(max) per unit load, up to 5 loads */
  77. #define MAX_LOW_POWER 0x32
  78. #define MAX_HIGH_POWER 0xFA
  79. /* bmAttributes:
  80. * D7:Reserved, always 1,
  81. * D6:Self-Powered -> 1,
  82. * D5:Remote Wakeup -> 0,
  83. * D4...0:Reserved -> 0
  84. */
  85. #define USB_CONFIG_ATT_ONE (1 << 7)
  86. #ifdef CONFIG_USB_DEVICE_SELF_POWERED
  87. #define USB_CONFIG_ATT_SELFPOWER (1 << 6)
  88. #else
  89. #define USB_CONFIG_ATT_SELFPOWER (0 << 6)
  90. #endif
  91. #ifdef CONFIG_USB_DEVICE_REMOTE_WAKEUP
  92. #define USB_CONFIG_ATT_WAKEUP (1 << 5)
  93. #else
  94. #define USB_CONFIG_ATT_WAKEUP (0 << 5)
  95. #endif
  96. #define USB_CONFIGURATION_ATTRIBUTES (USB_CONFIG_ATT_ONE | \
  97. USB_CONFIG_ATT_SELFPOWER | \
  98. USB_CONFIG_ATT_WAKEUP)
  99. /* Classes */
  100. #define AUDIO_CLASS 0x01
  101. #define COMMUNICATION_DEVICE_CLASS 0x02
  102. #define COMMUNICATION_DEVICE_CLASS_DATA 0x0A
  103. #define HID_CLASS 0x03
  104. #define MASS_STORAGE_CLASS 0x08
  105. #define WIRELESS_DEVICE_CLASS 0xE0
  106. #define MISC_CLASS 0xEF
  107. #define CUSTOM_CLASS 0xFF
  108. #define DFU_DEVICE_CLASS 0xFE
  109. /* Sub-classes */
  110. #define CDC_NCM_SUBCLASS 0x0d
  111. #define BOOT_INTERFACE_SUBCLASS 0x01
  112. #define SCSI_TRANSPARENT_SUBCLASS 0x06
  113. #define DFU_INTERFACE_SUBCLASS 0x01
  114. #define RF_SUBCLASS 0x01
  115. #define CUSTOM_SUBCLASS 0xFF
  116. /* Misc subclasses */
  117. #define MISC_RNDIS_SUBCLASS 0x04
  118. /* Protocols */
  119. #define V25TER_PROTOCOL 0x01
  120. #define MOUSE_PROTOCOL 0x02
  121. #define BULK_ONLY_PROTOCOL 0x50
  122. #define DFU_RUNTIME_PROTOCOL 0x01
  123. #define DFU_MODE_PROTOCOL 0x02
  124. #define BLUETOOTH_PROTOCOL 0x01
  125. /* CDC ACM protocols */
  126. #define ACM_VENDOR_PROTOCOL 0xFF
  127. /* Misc protocols */
  128. #define MISC_ETHERNET_PROTOCOL 0x01
  129. /** Standard Device Descriptor */
  130. struct usb_device_descriptor {
  131. uint8_t bLength;
  132. uint8_t bDescriptorType;
  133. uint16_t bcdUSB;
  134. uint8_t bDeviceClass;
  135. uint8_t bDeviceSubClass;
  136. uint8_t bDeviceProtocol;
  137. uint8_t bMaxPacketSize0;
  138. uint16_t idVendor;
  139. uint16_t idProduct;
  140. uint16_t bcdDevice;
  141. uint8_t iManufacturer;
  142. uint8_t iProduct;
  143. uint8_t iSerialNumber;
  144. uint8_t bNumConfigurations;
  145. } __packed;
  146. /** Unicode (UTF16LE) String Descriptor */
  147. struct usb_string_descriptor {
  148. uint8_t bLength;
  149. uint8_t bDescriptorType;
  150. uint16_t bString;
  151. } __packed;
  152. /** Association Descriptor */
  153. struct usb_association_descriptor {
  154. uint8_t bLength;
  155. uint8_t bDescriptorType;
  156. uint8_t bFirstInterface;
  157. uint8_t bInterfaceCount;
  158. uint8_t bFunctionClass;
  159. uint8_t bFunctionSubClass;
  160. uint8_t bFunctionProtocol;
  161. uint8_t iFunction;
  162. } __packed;
  163. /** Standard Configuration Descriptor */
  164. struct usb_cfg_descriptor {
  165. uint8_t bLength;
  166. uint8_t bDescriptorType;
  167. uint16_t wTotalLength;
  168. uint8_t bNumInterfaces;
  169. uint8_t bConfigurationValue;
  170. uint8_t iConfiguration;
  171. uint8_t bmAttributes;
  172. uint8_t bMaxPower;
  173. } __packed;
  174. /** Standard Interface Descriptor */
  175. struct usb_if_descriptor {
  176. uint8_t bLength;
  177. uint8_t bDescriptorType;
  178. uint8_t bInterfaceNumber;
  179. uint8_t bAlternateSetting;
  180. uint8_t bNumEndpoints;
  181. uint8_t bInterfaceClass;
  182. uint8_t bInterfaceSubClass;
  183. uint8_t bInterfaceProtocol;
  184. uint8_t iInterface;
  185. } __packed;
  186. /** Standard Endpoint Descriptor */
  187. struct usb_ep_descriptor {
  188. uint8_t bLength;
  189. uint8_t bDescriptorType;
  190. uint8_t bEndpointAddress;
  191. uint8_t bmAttributes;
  192. uint16_t wMaxPacketSize;
  193. uint8_t bInterval;
  194. } __packed;
  195. /** Standard Endpoint Descriptor */
  196. struct usb_endpoint_descriptor {
  197. uint8_t bLength;
  198. uint8_t bDescriptorType;
  199. uint8_t bEndpointAddress;
  200. uint8_t bmAttributes;
  201. uint16_t wMaxPacketSize;
  202. uint8_t bInterval;
  203. /* NOTE: these two are _only_ in audio endpoints. */
  204. uint8_t bRefresh;
  205. uint8_t bSynchAddress;
  206. } __packed;
  207. /** Device Qualifier descriptor */
  208. struct usb_qualifier_descriptor {
  209. uint8_t bLength;
  210. uint8_t bDescriptorType;
  211. uint16_t bcdUSB;
  212. uint8_t bDeviceClass;
  213. uint8_t bDeviceSubClass;
  214. uint8_t bDeviceProtocol;
  215. uint8_t bMaxPacketSize0;
  216. uint8_t bNumConfigurations;
  217. uint8_t bRESERVED;
  218. } __packed;
  219. #endif /* USB_COMMON_H_ */