usb_def.h 29 KB

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