usb_def.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_DEF_H
  7. #define USB_DEF_H
  8. /* Useful define */
  9. #define USB_1_1 0x0110
  10. #define USB_2_0 0x0200
  11. /* Set USB version to 2.1 so that the host will request the BOS descriptor */
  12. #define USB_2_1 0x0210
  13. #define USB_3_0 0x0300
  14. #define USB_3_1 0x0310
  15. #define USB_3_2 0x0320
  16. /* Device speeds */
  17. #define USB_SPEED_UNKNOWN 0 /* Transfer rate not yet set */
  18. #define USB_SPEED_LOW 1 /* USB 1.1 */
  19. #define USB_SPEED_FULL 2 /* USB 1.1 */
  20. #define USB_SPEED_HIGH 3 /* USB 2.0 */
  21. #define USB_SPEED_WIRELESS 4 /* Wireless USB 2.5 */
  22. #define USB_SPEED_SUPER 5 /* USB 3.0 */
  23. #define USB_SPEED_SUPER_PLUS 6 /* USB 3.1 */
  24. /* Maximum number of devices per controller */
  25. #define USB_MAX_DEVICES (127)
  26. /* Default USB control EP, always 0 and 0x80 */
  27. #define USB_CONTROL_OUT_EP0 0
  28. #define USB_CONTROL_IN_EP0 0x80
  29. /**< maximum packet size (MPS) for EP 0 */
  30. #define USB_CTRL_EP_MPS 64
  31. /**< maximum packet size (MPS) for bulk EP */
  32. #define USB_BULK_EP_MPS_HS 512
  33. #define USB_BULK_EP_MPS_FS 64
  34. /* USB PID Types */
  35. #define USB_PID_OUT (0x01) /* Tokens */
  36. #define USB_PID_IN (0x09)
  37. #define USB_PID_SOF (0x05)
  38. #define USB_PID_SETUP (0x0d)
  39. #define USB_PID_DATA0 (0x03) /* Data */
  40. #define USB_PID_DATA1 (0x0b)
  41. #define USB_PID_DATA2 (0x07)
  42. #define USB_PID_MDATA (0x0f)
  43. #define USB_PID_ACK (0x02) /* Handshake */
  44. #define USB_PID_NAK (0x0a)
  45. #define USB_PID_STALL (0x0e)
  46. #define USB_PID_NYET (0x06)
  47. #define USB_PID_PRE (0x0c) /* Special */
  48. #define USB_PID_ERR (0x0c)
  49. #define USB_PID_SPLIT (0x08)
  50. #define USB_PID_PING (0x04)
  51. #define USB_PID_RESERVED (0x00)
  52. #define USB_REQUEST_DIR_SHIFT 7U /* Bits 7: Request dir */
  53. #define USB_REQUEST_DIR_OUT (0U << USB_REQUEST_DIR_SHIFT) /* Bit 7=0: Host-to-device */
  54. #define USB_REQUEST_DIR_IN (1U << USB_REQUEST_DIR_SHIFT) /* Bit 7=1: Device-to-host */
  55. #define USB_REQUEST_DIR_MASK (1U << USB_REQUEST_DIR_SHIFT) /* Bit 7=1: Direction bit */
  56. #define USB_REQUEST_TYPE_SHIFT 5U /* Bits 5:6: Request type */
  57. #define USB_REQUEST_STANDARD (0U << USB_REQUEST_TYPE_SHIFT)
  58. #define USB_REQUEST_CLASS (1U << USB_REQUEST_TYPE_SHIFT)
  59. #define USB_REQUEST_VENDOR (2U << USB_REQUEST_TYPE_SHIFT)
  60. #define USB_REQUEST_RESERVED (3U << USB_REQUEST_TYPE_SHIFT)
  61. #define USB_REQUEST_TYPE_MASK (3U << USB_REQUEST_TYPE_SHIFT)
  62. #define USB_REQUEST_RECIPIENT_SHIFT 0U /* Bits 0:4: Recipient */
  63. #define USB_REQUEST_RECIPIENT_DEVICE (0U << USB_REQUEST_RECIPIENT_SHIFT)
  64. #define USB_REQUEST_RECIPIENT_INTERFACE (1U << USB_REQUEST_RECIPIENT_SHIFT)
  65. #define USB_REQUEST_RECIPIENT_ENDPOINT (2U << USB_REQUEST_RECIPIENT_SHIFT)
  66. #define USB_REQUEST_RECIPIENT_OTHER (3U << USB_REQUEST_RECIPIENT_SHIFT)
  67. #define USB_REQUEST_RECIPIENT_MASK (3U << USB_REQUEST_RECIPIENT_SHIFT)
  68. /* USB Standard Request Codes */
  69. #define USB_REQUEST_GET_STATUS 0x00
  70. #define USB_REQUEST_CLEAR_FEATURE 0x01
  71. #define USB_REQUEST_SET_FEATURE 0x03
  72. #define USB_REQUEST_SET_ADDRESS 0x05
  73. #define USB_REQUEST_GET_DESCRIPTOR 0x06
  74. #define USB_REQUEST_SET_DESCRIPTOR 0x07
  75. #define USB_REQUEST_GET_CONFIGURATION 0x08
  76. #define USB_REQUEST_SET_CONFIGURATION 0x09
  77. #define USB_REQUEST_GET_INTERFACE 0x0A
  78. #define USB_REQUEST_SET_INTERFACE 0x0B
  79. #define USB_REQUEST_SYNCH_FRAME 0x0C
  80. #define USB_REQUEST_SET_ENCRYPTION 0x0D
  81. #define USB_REQUEST_GET_ENCRYPTION 0x0E
  82. #define USB_REQUEST_RPIPE_ABORT 0x0E
  83. #define USB_REQUEST_SET_HANDSHAKE 0x0F
  84. #define USB_REQUEST_RPIPE_RESET 0x0F
  85. #define USB_REQUEST_GET_HANDSHAKE 0x10
  86. #define USB_REQUEST_SET_CONNECTION 0x11
  87. #define USB_REQUEST_SET_SECURITY_DATA 0x12
  88. #define USB_REQUEST_GET_SECURITY_DATA 0x13
  89. #define USB_REQUEST_SET_WUSB_DATA 0x14
  90. #define USB_REQUEST_LOOPBACK_DATA_WRITE 0x15
  91. #define USB_REQUEST_LOOPBACK_DATA_READ 0x16
  92. #define USB_REQUEST_SET_INTERFACE_DS 0x17
  93. /* USB Standard Feature selectors */
  94. #define USB_FEATURE_ENDPOINT_HALT 0
  95. #define USB_FEATURE_SELF_POWERED 0
  96. #define USB_FEATURE_REMOTE_WAKEUP 1
  97. #define USB_FEATURE_TEST_MODE 2
  98. #define USB_FEATURE_BATTERY 2
  99. #define USB_FEATURE_BHNPENABLE 3
  100. #define USB_FEATURE_WUSBDEVICE 3
  101. #define USB_FEATURE_AHNPSUPPORT 4
  102. #define USB_FEATURE_AALTHNPSUPPORT 5
  103. #define USB_FEATURE_DEBUGMODE 6
  104. /* USB GET_STATUS Bit Values */
  105. #define USB_GETSTATUS_ENDPOINT_HALT 0x01
  106. #define USB_GETSTATUS_SELF_POWERED 0x01
  107. #define USB_GETSTATUS_REMOTE_WAKEUP 0x02
  108. /* USB Descriptor Types */
  109. #define USB_DESCRIPTOR_TYPE_DEVICE 0x01U
  110. #define USB_DESCRIPTOR_TYPE_CONFIGURATION 0x02U
  111. #define USB_DESCRIPTOR_TYPE_STRING 0x03U
  112. #define USB_DESCRIPTOR_TYPE_INTERFACE 0x04U
  113. #define USB_DESCRIPTOR_TYPE_ENDPOINT 0x05U
  114. #define USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER 0x06U
  115. #define USB_DESCRIPTOR_TYPE_OTHER_SPEED 0x07U
  116. #define USB_DESCRIPTOR_TYPE_INTERFACE_POWER 0x08U
  117. #define USB_DESCRIPTOR_TYPE_OTG 0x09U
  118. #define USB_DESCRIPTOR_TYPE_DEBUG 0x0AU
  119. #define USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION 0x0BU
  120. #define USB_DESCRIPTOR_TYPE_BINARY_OBJECT_STORE 0x0FU
  121. #define USB_DESCRIPTOR_TYPE_DEVICE_CAPABILITY 0x10U
  122. #define USB_DESCRIPTOR_TYPE_WIRELESS_ENDPOINTCOMP 0x11U
  123. /* Class Specific Descriptor */
  124. #define USB_CS_DESCRIPTOR_TYPE_DEVICE 0x21U
  125. #define USB_CS_DESCRIPTOR_TYPE_CONFIGURATION 0x22U
  126. #define USB_CS_DESCRIPTOR_TYPE_STRING 0x23U
  127. #define USB_CS_DESCRIPTOR_TYPE_INTERFACE 0x24U
  128. #define USB_CS_DESCRIPTOR_TYPE_ENDPOINT 0x25U
  129. #define USB_DESCRIPTOR_TYPE_SUPERSPEED_ENDPOINT_COMPANION 0x30U
  130. #define USB_DESCRIPTOR_TYPE_SUPERSPEED_ISO_ENDPOINT_COMPANION 0x31U
  131. /* USB Device Classes */
  132. #define USB_DEVICE_CLASS_RESERVED 0x00
  133. #define USB_DEVICE_CLASS_AUDIO 0x01
  134. #define USB_DEVICE_CLASS_CDC 0x02
  135. #define USB_DEVICE_CLASS_HID 0x03
  136. #define USB_DEVICE_CLASS_MONITOR 0x04
  137. #define USB_DEVICE_CLASS_PHYSICAL 0x05
  138. #define USB_DEVICE_CLASS_IMAGE 0x06
  139. #define USB_DEVICE_CLASS_PRINTER 0x07
  140. #define USB_DEVICE_CLASS_MASS_STORAGE 0x08
  141. #define USB_DEVICE_CLASS_HUB 0x09
  142. #define USB_DEVICE_CLASS_CDC_DATA 0x0a
  143. #define USB_DEVICE_CLASS_SMART_CARD 0x0b
  144. #define USB_DEVICE_CLASS_SECURITY 0x0d
  145. #define USB_DEVICE_CLASS_VIDEO 0x0e
  146. #define USB_DEVICE_CLASS_HEALTHCARE 0x0f
  147. #define USB_DEVICE_CLASS_DIAG_DEVICE 0xdc
  148. #define USB_DEVICE_CLASS_WIRELESS 0xe0
  149. #define USB_DEVICE_CLASS_MISC 0xef
  150. #define USB_DEVICE_CLASS_APP_SPECIFIC 0xfe
  151. #define USB_DEVICE_CLASS_VEND_SPECIFIC 0xff
  152. /* usb string index define */
  153. #define USB_STRING_LANGID_INDEX 0x00
  154. #define USB_STRING_MFC_INDEX 0x01
  155. #define USB_STRING_PRODUCT_INDEX 0x02
  156. #define USB_STRING_SERIAL_INDEX 0x03
  157. #define USB_STRING_CONFIG_INDEX 0x04
  158. #define USB_STRING_INTERFACE_INDEX 0x05
  159. #define USB_STRING_OS_INDEX 0x06
  160. #define USB_STRING_MAX USB_STRING_OS_INDEX
  161. /*
  162. * Devices supporting Microsoft OS Descriptors store special string
  163. * descriptor at fixed index (0xEE). It is read when a new device is
  164. * attached to a computer for the first time.
  165. */
  166. #define USB_OSDESC_STRING_DESC_INDEX 0xEE
  167. /* bmAttributes in Configuration Descriptor */
  168. #define USB_CONFIG_REMOTE_WAKEUP 0x20
  169. #define USB_CONFIG_POWERED_MASK 0x40
  170. #define USB_CONFIG_BUS_POWERED 0x80
  171. #define USB_CONFIG_SELF_POWERED 0xC0
  172. /* bMaxPower in Configuration Descriptor */
  173. #define USB_CONFIG_POWER_MA(mA) ((mA) / 2)
  174. /* bEndpointAddress in Endpoint Descriptor */
  175. #define USB_ENDPOINT_DIRECTION_MASK 0x80
  176. #define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
  177. #define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
  178. /**
  179. * USB endpoint direction and number.
  180. */
  181. #define USB_EP_DIR_MASK 0x80U
  182. #define USB_EP_DIR_IN 0x80U
  183. #define USB_EP_DIR_OUT 0x00U
  184. /** Get endpoint index (number) from endpoint address */
  185. #define USB_EP_GET_IDX(ep) ((ep) & ~USB_EP_DIR_MASK)
  186. /** Get direction from endpoint address */
  187. #define USB_EP_GET_DIR(ep) ((ep)&USB_EP_DIR_MASK)
  188. /** Get endpoint address from endpoint index and direction */
  189. #define USB_EP_GET_ADDR(idx, dir) ((idx) | ((dir)&USB_EP_DIR_MASK))
  190. /** True if the endpoint is an IN endpoint */
  191. #define USB_EP_DIR_IS_IN(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_IN)
  192. /** True if the endpoint is an OUT endpoint */
  193. #define USB_EP_DIR_IS_OUT(ep) (USB_EP_GET_DIR(ep) == USB_EP_DIR_OUT)
  194. /* bmAttributes in Endpoint Descriptor */
  195. #define USB_ENDPOINT_TYPE_SHIFT 0
  196. #define USB_ENDPOINT_TYPE_CONTROL (0 << USB_ENDPOINT_TYPE_SHIFT)
  197. #define USB_ENDPOINT_TYPE_ISOCHRONOUS (1 << USB_ENDPOINT_TYPE_SHIFT)
  198. #define USB_ENDPOINT_TYPE_BULK (2 << USB_ENDPOINT_TYPE_SHIFT)
  199. #define USB_ENDPOINT_TYPE_INTERRUPT (3 << USB_ENDPOINT_TYPE_SHIFT)
  200. #define USB_ENDPOINT_TYPE_MASK (3 << USB_ENDPOINT_TYPE_SHIFT)
  201. #define USB_GET_ENDPOINT_TYPE(x) ((x & USB_ENDPOINT_TYPE_MASK) >> USB_ENDPOINT_TYPE_SHIFT)
  202. #define USB_ENDPOINT_SYNC_SHIFT 2
  203. #define USB_ENDPOINT_SYNC_NO_SYNCHRONIZATION (0 << USB_ENDPOINT_SYNC_SHIFT)
  204. #define USB_ENDPOINT_SYNC_ASYNCHRONOUS (1 << USB_ENDPOINT_SYNC_SHIFT)
  205. #define USB_ENDPOINT_SYNC_ADAPTIVE (2 << USB_ENDPOINT_SYNC_SHIFT)
  206. #define USB_ENDPOINT_SYNC_SYNCHRONOUS (3 << USB_ENDPOINT_SYNC_SHIFT)
  207. #define USB_ENDPOINT_SYNC_MASK (3 << USB_ENDPOINT_SYNC_SHIFT)
  208. #define USB_ENDPOINT_USAGE_SHIFT 4
  209. #define USB_ENDPOINT_USAGE_DATA (0 << USB_ENDPOINT_USAGE_SHIFT)
  210. #define USB_ENDPOINT_USAGE_FEEDBACK (1 << USB_ENDPOINT_USAGE_SHIFT)
  211. #define USB_ENDPOINT_USAGE_IMPLICIT_FEEDBACK (2 << USB_ENDPOINT_USAGE_SHIFT)
  212. #define USB_ENDPOINT_USAGE_MASK (3 << USB_ENDPOINT_USAGE_SHIFT)
  213. #define USB_ENDPOINT_MAX_ADJUSTABLE (1 << 7)
  214. /* wMaxPacketSize in Endpoint Descriptor */
  215. #define USB_MAXPACKETSIZE_SHIFT 0
  216. #define USB_MAXPACKETSIZE_MASK (0x7ff << USB_MAXPACKETSIZE_SHIFT)
  217. #define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT 11
  218. #define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_NONE (0 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
  219. #define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_ONE (1 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
  220. #define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_TWO (2 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
  221. #define USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK (3 << USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
  222. #define USB_GET_MAXPACKETSIZE(x) ((x & USB_MAXPACKETSIZE_MASK) >> USB_MAXPACKETSIZE_SHIFT)
  223. #define USB_GET_MULT(x) ((x & USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_MASK) >> USB_MAXPACKETSIZE_ADDITIONAL_TRANSCATION_SHIFT)
  224. /* bDevCapabilityType in Device Capability Descriptor */
  225. #define USB_DEVICE_CAPABILITY_WIRELESS_USB 1
  226. #define USB_DEVICE_CAPABILITY_USB_2_0_EXTENSION 2
  227. #define USB_DEVICE_CAPABILITY_SUPERSPEED_USB 3
  228. #define USB_DEVICE_CAPABILITY_CONTAINER_ID 4
  229. #define USB_DEVICE_CAPABILITY_PLATFORM 5
  230. #define USB_DEVICE_CAPABILITY_POWER_DELIVERY_CAPABILITY 6
  231. #define USB_DEVICE_CAPABILITY_BATTERY_INFO_CAPABILITY 7
  232. #define USB_DEVICE_CAPABILITY_PD_CONSUMER_PORT_CAPABILITY 8
  233. #define USB_DEVICE_CAPABILITY_PD_PROVIDER_PORT_CAPABILITY 9
  234. #define USB_DEVICE_CAPABILITY_SUPERSPEED_PLUS 10
  235. #define USB_DEVICE_CAPABILITY_PRECISION_TIME_MEASUREMENT 11
  236. #define USB_DEVICE_CAPABILITY_WIRELESS_USB_EXT 12
  237. #define USB_BOS_CAPABILITY_EXTENSION 0x02
  238. #define USB_BOS_CAPABILITY_PLATFORM 0x05
  239. /* OTG SET FEATURE Constants */
  240. #define USB_OTG_FEATURE_B_HNP_ENABLE 3 /* Enable B device to perform HNP */
  241. #define USB_OTG_FEATURE_A_HNP_SUPPORT 4 /* A device supports HNP */
  242. #define USB_OTG_FEATURE_A_ALT_HNP_SUPPORT 5 /* Another port on the A device supports HNP */
  243. /* WinUSB Microsoft OS 2.0 descriptor request codes */
  244. #define WINUSB_REQUEST_GET_DESCRIPTOR_SET 0x07
  245. #define WINUSB_REQUEST_SET_ALT_ENUM 0x08
  246. /* WinUSB Microsoft OS 2.0 descriptor sizes */
  247. #define WINUSB_DESCRIPTOR_SET_HEADER_SIZE 10
  248. #define WINUSB_FUNCTION_SUBSET_HEADER_SIZE 8
  249. #define WINUSB_FEATURE_COMPATIBLE_ID_SIZE 20
  250. /* WinUSB Microsoft OS 2.0 Descriptor Types */
  251. #define WINUSB_SET_HEADER_DESCRIPTOR_TYPE 0x00
  252. #define WINUSB_SUBSET_HEADER_CONFIGURATION_TYPE 0x01
  253. #define WINUSB_SUBSET_HEADER_FUNCTION_TYPE 0x02
  254. #define WINUSB_FEATURE_COMPATIBLE_ID_TYPE 0x03
  255. #define WINUSB_FEATURE_REG_PROPERTY_TYPE 0x04
  256. #define WINUSB_FEATURE_MIN_RESUME_TIME_TYPE 0x05
  257. #define WINUSB_FEATURE_MODEL_ID_TYPE 0x06
  258. #define WINUSB_FEATURE_CCGP_DEVICE_TYPE 0x07
  259. #define WINUSB_PROP_DATA_TYPE_REG_SZ 0x01
  260. #define WINUSB_PROP_DATA_TYPE_REG_MULTI_SZ 0x07
  261. /* WebUSB Descriptor Types */
  262. #define WEBUSB_DESCRIPTOR_SET_HEADER_TYPE 0x00
  263. #define WEBUSB_CONFIGURATION_SUBSET_HEADER_TYPE 0x01
  264. #define WEBUSB_FUNCTION_SUBSET_HEADER_TYPE 0x02
  265. #define WEBUSB_URL_TYPE 0x03
  266. /* WebUSB Request Codes */
  267. #define WEBUSB_REQUEST_GET_URL 0x02
  268. /* bScheme in URL descriptor */
  269. #define WEBUSB_URL_SCHEME_HTTP 0x00
  270. #define WEBUSB_URL_SCHEME_HTTPS 0x01
  271. /* WebUSB Descriptor sizes */
  272. #define WEBUSB_DESCRIPTOR_SET_HEADER_SIZE 5
  273. #define WEBUSB_CONFIGURATION_SUBSET_HEADER_SIZE 4
  274. #define WEBUSB_FUNCTION_SUBSET_HEADER_SIZE 3
  275. /* Setup packet definition used to read raw data from USB line */
  276. struct usb_setup_packet {
  277. /** Request type. Bits 0:4 determine recipient, see
  278. * \ref usb_request_recipient. Bits 5:6 determine type, see
  279. * \ref usb_request_type. Bit 7 determines data transfer direction, see
  280. * \ref usb_endpoint_direction.
  281. */
  282. uint8_t bmRequestType;
  283. /** Request. If the type bits of bmRequestType are equal to
  284. * \ref usb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
  285. * "USB_REQUEST_TYPE_STANDARD" then this field refers to
  286. * \ref usb_standard_request. For other cases, use of this field is
  287. * application-specific. */
  288. uint8_t bRequest;
  289. /** Value. Varies according to request */
  290. uint16_t wValue;
  291. /** Index. Varies according to request, typically used to pass an index
  292. * or offset */
  293. uint16_t wIndex;
  294. /** Number of bytes to transfer */
  295. uint16_t wLength;
  296. } __PACKED;
  297. #define USB_SIZEOF_SETUP_PACKET 8
  298. /** Standard Device Descriptor */
  299. struct usb_device_descriptor {
  300. uint8_t bLength; /* Descriptor size in bytes = 18 */
  301. uint8_t bDescriptorType; /* DEVICE descriptor type = 1 */
  302. uint16_t bcdUSB; /* USB spec in BCD, e.g. 0x0200 */
  303. uint8_t bDeviceClass; /* Class code, if 0 see interface */
  304. uint8_t bDeviceSubClass; /* Sub-Class code, 0 if class = 0 */
  305. uint8_t bDeviceProtocol; /* Protocol, if 0 see interface */
  306. uint8_t bMaxPacketSize0; /* Endpoint 0 max. size */
  307. uint16_t idVendor; /* Vendor ID per USB-IF */
  308. uint16_t idProduct; /* Product ID per manufacturer */
  309. uint16_t bcdDevice; /* Device release # in BCD */
  310. uint8_t iManufacturer; /* Index to manufacturer string */
  311. uint8_t iProduct; /* Index to product string */
  312. uint8_t iSerialNumber; /* Index to serial number string */
  313. uint8_t bNumConfigurations; /* Number of possible configurations */
  314. } __PACKED;
  315. #define USB_SIZEOF_DEVICE_DESC 18
  316. /** Standard Configuration Descriptor */
  317. struct usb_configuration_descriptor {
  318. uint8_t bLength; /* Descriptor size in bytes = 9 */
  319. uint8_t bDescriptorType; /* CONFIGURATION type = 2 or 7 */
  320. uint16_t wTotalLength; /* Length of concatenated descriptors */
  321. uint8_t bNumInterfaces; /* Number of interfaces, this config. */
  322. uint8_t bConfigurationValue; /* Value to set this config. */
  323. uint8_t iConfiguration; /* Index to configuration string */
  324. uint8_t bmAttributes; /* Config. characteristics */
  325. uint8_t bMaxPower; /* Max.power from bus, 2mA units */
  326. } __PACKED;
  327. #define USB_SIZEOF_CONFIG_DESC 9
  328. /** Standard Interface Descriptor */
  329. struct usb_interface_descriptor {
  330. uint8_t bLength; /* Descriptor size in bytes = 9 */
  331. uint8_t bDescriptorType; /* INTERFACE descriptor type = 4 */
  332. uint8_t bInterfaceNumber; /* Interface no.*/
  333. uint8_t bAlternateSetting; /* Value to select this IF */
  334. uint8_t bNumEndpoints; /* Number of endpoints excluding 0 */
  335. uint8_t bInterfaceClass; /* Class code, 0xFF = vendor */
  336. uint8_t bInterfaceSubClass; /* Sub-Class code, 0 if class = 0 */
  337. uint8_t bInterfaceProtocol; /* Protocol, 0xFF = vendor */
  338. uint8_t iInterface; /* Index to interface string */
  339. } __PACKED;
  340. #define USB_SIZEOF_INTERFACE_DESC 9
  341. /** Standard Endpoint Descriptor */
  342. struct usb_endpoint_descriptor {
  343. uint8_t bLength; /* Descriptor size in bytes = 7 */
  344. uint8_t bDescriptorType; /* ENDPOINT descriptor type = 5 */
  345. uint8_t bEndpointAddress; /* Endpoint # 0 - 15 | IN/OUT */
  346. uint8_t bmAttributes; /* Transfer type */
  347. uint16_t wMaxPacketSize; /* Bits 10:0 = max. packet size */
  348. uint8_t bInterval; /* Polling interval in (micro) frames */
  349. } __PACKED;
  350. #define USB_SIZEOF_ENDPOINT_DESC 7
  351. /** Unicode (UTF16LE) String Descriptor */
  352. struct usb_string_descriptor {
  353. uint8_t bLength;
  354. uint8_t bDescriptorType;
  355. uint16_t bString;
  356. } __PACKED;
  357. #define USB_SIZEOF_STRING_LANGID_DESC 4
  358. /* USB Interface Association Descriptor */
  359. struct usb_interface_association_descriptor {
  360. uint8_t bLength;
  361. uint8_t bDescriptorType;
  362. uint8_t bFirstInterface;
  363. uint8_t bInterfaceCount;
  364. uint8_t bFunctionClass;
  365. uint8_t bFunctionSubClass;
  366. uint8_t bFunctionProtocol;
  367. uint8_t iFunction;
  368. } __PACKED;
  369. #define USB_SIZEOF_IAD_DESC 8
  370. /** USB device_qualifier descriptor */
  371. struct usb_device_qualifier_descriptor {
  372. uint8_t bLength; /* Descriptor size in bytes = 10 */
  373. uint8_t bDescriptorType; /* DEVICE QUALIFIER type = 6 */
  374. uint16_t bcdUSB; /* USB spec in BCD, e.g. 0x0200 */
  375. uint8_t bDeviceClass; /* Class code, if 0 see interface */
  376. uint8_t bDeviceSubClass; /* Sub-Class code, 0 if class = 0 */
  377. uint8_t bDeviceProtocol; /* Protocol, if 0 see interface */
  378. uint8_t bMaxPacketSize; /* Endpoint 0 max. size */
  379. uint8_t bNumConfigurations; /* Number of possible configurations */
  380. uint8_t bReserved; /* Reserved = 0 */
  381. } __PACKED;
  382. #define USB_SIZEOF_DEVICE_QUALIFIER_DESC 10
  383. /* Microsoft OS function descriptor.
  384. * This can be used to request a specific driver (such as WINUSB) to be
  385. * loaded on Windows. Unlike other descriptors, it is requested by a special
  386. * request USB_REQ_GETMSFTOSDESCRIPTOR.
  387. * More details:
  388. * https://msdn.microsoft.com/en-us/windows/hardware/gg463179
  389. * And excellent explanation:
  390. * https://github.com/pbatard/libwdi/wiki/WCID-Devices
  391. *
  392. * The device will have exactly one "Extended Compat ID Feature Descriptor",
  393. * which may contain multiple "Function Descriptors" associated with
  394. * different interfaces.
  395. */
  396. /* MS OS 1.0 string descriptor */
  397. struct usb_msosv1_string_descriptor {
  398. uint8_t bLength;
  399. uint8_t bDescriptorType;
  400. uint8_t bString[14];
  401. uint8_t bMS_VendorCode; /* Vendor Code, used for a control request */
  402. uint8_t bPad; /* Padding byte for VendorCode look as UTF16 */
  403. } __PACKED;
  404. /* MS OS 1.0 Header descriptor */
  405. struct usb_msosv1_compat_id_header_descriptor {
  406. uint32_t dwLength;
  407. uint16_t bcdVersion;
  408. uint16_t wIndex;
  409. uint8_t bCount;
  410. uint8_t reserved[7];
  411. } __PACKED;
  412. /* MS OS 1.0 Function descriptor */
  413. struct usb_msosv1_comp_id_function_descriptor {
  414. uint8_t bFirstInterfaceNumber;
  415. uint8_t reserved1;
  416. uint8_t compatibleID[8];
  417. uint8_t subCompatibleID[8];
  418. uint8_t reserved2[6];
  419. } __PACKED;
  420. #define usb_msosv1_comp_id_create(x) \
  421. struct usb_msosv1_comp_id { \
  422. struct usb_msosv1_compat_id_header_descriptor compat_id_header; \
  423. struct usb_msosv1_comp_id_function_descriptor compat_id_function[x]; \
  424. };
  425. struct usb_msosv1_descriptor {
  426. const uint8_t *string;
  427. uint8_t vendor_code;
  428. const uint8_t *compat_id;
  429. const uint8_t **comp_id_property;
  430. };
  431. /* MS OS 2.0 Header descriptor */
  432. struct usb_msosv2_header_descriptor {
  433. uint32_t dwLength;
  434. uint16_t bcdVersion;
  435. uint16_t wIndex;
  436. uint8_t bCount;
  437. } __PACKED;
  438. /*Microsoft OS 2.0 set header descriptor*/
  439. struct usb_msosv2_set_header_descriptor {
  440. uint16_t wLength;
  441. uint16_t wDescriptorType;
  442. uint32_t dwWindowsVersion;
  443. uint16_t wDescriptorSetTotalLength;
  444. } __PACKED;
  445. /* Microsoft OS 2.0 compatibleID descriptor*/
  446. struct usb_msosv2_comp_id_descriptor {
  447. uint16_t wLength;
  448. uint16_t wDescriptorType;
  449. uint8_t compatibleID[8];
  450. uint8_t subCompatibleID[8];
  451. } __PACKED;
  452. /* MS OS 2.0 property descriptor */
  453. struct usb_msosv2_property_descriptor {
  454. uint16_t wLength;
  455. uint16_t wDescriptorType;
  456. uint32_t dwPropertyDataType;
  457. uint16_t wPropertyNameLength;
  458. const char *bPropertyName;
  459. uint32_t dwPropertyDataLength;
  460. const char *bPropertyData;
  461. };
  462. /* Microsoft OS 2.0 subset function descriptor */
  463. struct usb_msosv2_subset_function_descriptor {
  464. uint16_t wLength;
  465. uint16_t wDescriptorType;
  466. uint8_t bFirstInterface;
  467. uint8_t bReserved;
  468. uint16_t wSubsetLength;
  469. } __PACKED;
  470. struct usb_msosv2_descriptor {
  471. const uint8_t *compat_id;
  472. uint16_t compat_id_len;
  473. uint8_t vendor_code;
  474. };
  475. /* BOS header Descriptor */
  476. struct usb_bos_header_descriptor {
  477. uint8_t bLength;
  478. uint8_t bDescriptorType;
  479. uint16_t wTotalLength;
  480. uint8_t bNumDeviceCaps;
  481. } __PACKED;
  482. /* BOS Capability platform Descriptor */
  483. struct usb_bos_capability_platform_descriptor {
  484. uint8_t bLength;
  485. uint8_t bDescriptorType;
  486. uint8_t bDevCapabilityType;
  487. uint8_t bReserved;
  488. uint8_t PlatformCapabilityUUID[16];
  489. } __PACKED;
  490. /* BOS Capability MS OS Descriptors version 2 */
  491. struct usb_bos_capability_msosv2_descriptor {
  492. uint32_t dwWindowsVersion;
  493. uint16_t wMSOSDescriptorSetTotalLength;
  494. uint8_t bVendorCode;
  495. uint8_t bAltEnumCode;
  496. } __PACKED;
  497. /* BOS Capability webusb */
  498. struct usb_bos_capability_webusb_descriptor {
  499. uint16_t bcdVersion;
  500. uint8_t bVendorCode;
  501. uint8_t iLandingPage;
  502. } __PACKED;
  503. /* BOS Capability extension Descriptor*/
  504. struct usb_bos_capability_extension_descriptor {
  505. uint8_t bLength;
  506. uint8_t bDescriptorType;
  507. uint8_t bDevCapabilityType;
  508. uint32_t bmAttributes;
  509. } __PACKED;
  510. /* Microsoft OS 2.0 Platform Capability Descriptor
  511. * See https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/
  512. * microsoft-defined-usb-descriptors
  513. * Adapted from the source:
  514. * https://github.com/sowbug/weblight/blob/master/firmware/webusb.c
  515. * (BSD-2) Thanks http://janaxelson.com/files/ms_os_20_descriptors.c
  516. */
  517. struct usb_bos_capability_platform_msosv2_descriptor {
  518. struct usb_bos_capability_platform_descriptor platform_msos;
  519. struct usb_bos_capability_msosv2_descriptor data_msosv2;
  520. } __PACKED;
  521. /* WebUSB Platform Capability Descriptor:
  522. * https://wicg.github.io/webusb/#webusb-platform-capability-descriptor
  523. */
  524. struct usb_bos_capability_platform_webusb_descriptor {
  525. struct usb_bos_capability_platform_descriptor platform_webusb;
  526. struct usb_bos_capability_webusb_descriptor data_webusb;
  527. } __PACKED;
  528. struct usb_webusb_url_descriptor {
  529. uint8_t bLength;
  530. uint8_t bDescriptorType;
  531. uint8_t bScheme;
  532. char URL[];
  533. } __PACKED;
  534. struct usb_webusb_descriptor {
  535. uint8_t vendor_code;
  536. const uint8_t *string;
  537. uint32_t string_len;
  538. } __PACKED;
  539. struct usb_bos_descriptor {
  540. const uint8_t *string;
  541. uint32_t string_len;
  542. };
  543. /* USB Device Capability Descriptor */
  544. struct usb_device_capability_descriptor {
  545. uint8_t bLength;
  546. uint8_t bDescriptorType;
  547. uint8_t bDevCapabilityType;
  548. } __PACKED;
  549. /** USB descriptor header */
  550. struct usb_desc_header {
  551. uint8_t bLength; /**< descriptor length */
  552. uint8_t bDescriptorType; /**< descriptor type */
  553. };
  554. // clang-format off
  555. #define USB_DEVICE_DESCRIPTOR_INIT(bcdUSB, bDeviceClass, bDeviceSubClass, bDeviceProtocol, idVendor, idProduct, bcdDevice, bNumConfigurations) \
  556. 0x12, /* bLength */ \
  557. USB_DESCRIPTOR_TYPE_DEVICE, /* bDescriptorType */ \
  558. WBVAL(bcdUSB), /* bcdUSB */ \
  559. bDeviceClass, /* bDeviceClass */ \
  560. bDeviceSubClass, /* bDeviceSubClass */ \
  561. bDeviceProtocol, /* bDeviceProtocol */ \
  562. 0x40, /* bMaxPacketSize */ \
  563. WBVAL(idVendor), /* idVendor */ \
  564. WBVAL(idProduct), /* idProduct */ \
  565. WBVAL(bcdDevice), /* bcdDevice */ \
  566. USB_STRING_MFC_INDEX, /* iManufacturer */ \
  567. USB_STRING_PRODUCT_INDEX, /* iProduct */ \
  568. USB_STRING_SERIAL_INDEX, /* iSerial */ \
  569. bNumConfigurations /* bNumConfigurations */
  570. #define USB_CONFIG_DESCRIPTOR_INIT(wTotalLength, bNumInterfaces, bConfigurationValue, bmAttributes, bMaxPower) \
  571. 0x09, /* bLength */ \
  572. USB_DESCRIPTOR_TYPE_CONFIGURATION, /* bDescriptorType */ \
  573. WBVAL(wTotalLength), /* wTotalLength */ \
  574. bNumInterfaces, /* bNumInterfaces */ \
  575. bConfigurationValue, /* bConfigurationValue */ \
  576. 0x00, /* iConfiguration */ \
  577. bmAttributes, /* bmAttributes */ \
  578. USB_CONFIG_POWER_MA(bMaxPower) /* bMaxPower */
  579. #define USB_DEVICE_QUALIFIER_DESCRIPTOR_INIT(bcdUSB, bDeviceClass, bDeviceSubClass, bDeviceProtocol, bNumConfigurations) \
  580. 0x0A, /* bLength */ \
  581. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, /* bDescriptorType */ \
  582. WBVAL(bcdUSB), /* bcdUSB */ \
  583. bDeviceClass, /* bDeviceClass */ \
  584. bDeviceSubClass, /* bDeviceSubClass */ \
  585. bDeviceProtocol, /* bDeviceProtocol */ \
  586. 0x40, /* bMaxPacketSize */ \
  587. bNumConfigurations, /* bNumConfigurations */ \
  588. 0x00 /* bReserved */
  589. #define USB_OTHER_SPEED_CONFIG_DESCRIPTOR_INIT(wTotalLength, bNumInterfaces, bConfigurationValue, bmAttributes, bMaxPower) \
  590. 0x09, /* bLength */ \
  591. USB_DESCRIPTOR_TYPE_OTHER_SPEED, /* bDescriptorType */ \
  592. WBVAL(wTotalLength), /* wTotalLength */ \
  593. bNumInterfaces, /* bNumInterfaces */ \
  594. bConfigurationValue, /* bConfigurationValue */ \
  595. 0x00, /* iConfiguration */ \
  596. bmAttributes, /* bmAttributes */ \
  597. USB_CONFIG_POWER_MA(bMaxPower) /* bMaxPower */
  598. #define USB_INTERFACE_DESCRIPTOR_INIT(bInterfaceNumber, bAlternateSetting, bNumEndpoints, \
  599. bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol, iInterface) \
  600. 0x09, /* bLength */ \
  601. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  602. bInterfaceNumber, /* bInterfaceNumber */ \
  603. bAlternateSetting, /* bAlternateSetting */ \
  604. bNumEndpoints, /* bNumEndpoints */ \
  605. bInterfaceClass, /* bInterfaceClass */ \
  606. bInterfaceSubClass, /* bInterfaceSubClass */ \
  607. bInterfaceProtocol, /* bInterfaceProtocol */ \
  608. iInterface /* iInterface */
  609. #define USB_ENDPOINT_DESCRIPTOR_INIT(bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval) \
  610. 0x07, /* bLength */ \
  611. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  612. bEndpointAddress, /* bEndpointAddress */ \
  613. bmAttributes, /* bmAttributes */ \
  614. WBVAL(wMaxPacketSize), /* wMaxPacketSize */ \
  615. bInterval /* bInterval */
  616. #define USB_IAD_INIT(bFirstInterface, bInterfaceCount, bFunctionClass, bFunctionSubClass, bFunctionProtocol) \
  617. 0x08, /* bLength */ \
  618. USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION, /* bDescriptorType */ \
  619. bFirstInterface, /* bFirstInterface */ \
  620. bInterfaceCount, /* bInterfaceCount */ \
  621. bFunctionClass, /* bFunctionClass */ \
  622. bFunctionSubClass, /* bFunctionSubClass */ \
  623. bFunctionProtocol, /* bFunctionProtocol */ \
  624. 0x00 /* iFunction */
  625. #define USB_LANGID_INIT(id) \
  626. 0x04, /* bLength */ \
  627. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ \
  628. WBVAL(id) /* wLangID0 */
  629. // clang-format on
  630. #endif /* USB_DEF_H */