usb_descriptors.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. // Copyright (c) 2017-2022, Alex Taradov <alex@taradov.com>. All rights reserved.
  3. /*- Includes ----------------------------------------------------------------*/
  4. #include <stdalign.h>
  5. #include "usb_descriptors.h"
  6. /*- Variables ---------------------------------------------------------------*/
  7. const alignas(4) usb_device_descriptor_t usb_device_descriptor =
  8. {
  9. .bLength = sizeof(usb_device_descriptor_t),
  10. .bDescriptorType = USB_DEVICE_DESCRIPTOR,
  11. .bcdUSB = USB_BCD_VERSION,
  12. .bDeviceClass = USB_DEVICE_CLASS_MISCELLANEOUS,
  13. .bDeviceSubClass = USB_DEVICE_SUBCLASS_COMMON,
  14. .bDeviceProtocol = USB_DEVICE_PROTOCOL_INTERFACE_ASSOCIATION,
  15. .bMaxPacketSize0 = USB_CTRL_EP_SIZE,
  16. .idVendor = 0x6666,
  17. .idProduct = 0x9930,
  18. .bcdDevice = 0x0100,
  19. .iManufacturer = USB_STR_MANUFACTURER,
  20. .iProduct = USB_STR_PRODUCT,
  21. .iSerialNumber = USB_STR_SERIAL_NUMBER,
  22. .bNumConfigurations = 1,
  23. };
  24. const alignas(4) usb_configuration_hierarchy_t usb_configuration_hierarchy =
  25. {
  26. .configuration =
  27. {
  28. .bLength = sizeof(usb_configuration_descriptor_t),
  29. .bDescriptorType = USB_CONFIGURATION_DESCRIPTOR,
  30. .wTotalLength = sizeof(usb_configuration_hierarchy_t),
  31. .bNumInterfaces = USB_INTF_COUNT,
  32. .bConfigurationValue = 1,
  33. .iConfiguration = 0,
  34. .bmAttributes = USB_ATTRIBUTE_BUS_POWERED,
  35. .bMaxPower = USB_MAX_POWER(500),
  36. },
  37. // CMSIS-DAP v1
  38. .hid_interface =
  39. {
  40. .bLength = sizeof(usb_interface_descriptor_t),
  41. .bDescriptorType = USB_INTERFACE_DESCRIPTOR,
  42. .bInterfaceNumber = USB_INTF_HID,
  43. .bAlternateSetting = 0,
  44. .bNumEndpoints = 2,
  45. .bInterfaceClass = USB_HID_DEVICE_CLASS,
  46. .bInterfaceSubClass = 0,
  47. .bInterfaceProtocol = 0,
  48. .iInterface = USB_STR_CMSIS_DAP_V1,
  49. },
  50. .hid =
  51. {
  52. .bLength = sizeof(usb_hid_descriptor_t),
  53. .bDescriptorType = USB_HID_DESCRIPTOR,
  54. .bcdHID = 0x0111,
  55. .bCountryCode = 0,
  56. .bNumDescriptors = 1,
  57. .bDescriptorType1 = USB_HID_REPORT_DESCRIPTOR,
  58. .wDescriptorLength = sizeof(usb_hid_report_descriptor),
  59. },
  60. .hid_ep_in =
  61. {
  62. .bLength = sizeof(usb_endpoint_descriptor_t),
  63. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  64. .bEndpointAddress = USB_IN_ENDPOINT | USB_HID_EP_SEND,
  65. .bmAttributes = USB_INTERRUPT_ENDPOINT,
  66. .wMaxPacketSize = 64,
  67. .bInterval = 1,
  68. },
  69. .hid_ep_out =
  70. {
  71. .bLength = sizeof(usb_endpoint_descriptor_t),
  72. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  73. .bEndpointAddress = USB_OUT_ENDPOINT | USB_HID_EP_RECV,
  74. .bmAttributes = USB_INTERRUPT_ENDPOINT,
  75. .wMaxPacketSize = 64,
  76. .bInterval = 1,
  77. },
  78. // CMSIS-DAP v2
  79. .bulk_interface =
  80. {
  81. .bLength = sizeof(usb_interface_descriptor_t),
  82. .bDescriptorType = USB_INTERFACE_DESCRIPTOR,
  83. .bInterfaceNumber = USB_INTF_BULK,
  84. .bAlternateSetting = 0,
  85. .bNumEndpoints = 2,
  86. .bInterfaceClass = USB_DEVICE_CLASS_VENDOR_SPECIFIC,
  87. .bInterfaceSubClass = 0,
  88. .bInterfaceProtocol = 0,
  89. .iInterface = USB_STR_CMSIS_DAP_V2,
  90. },
  91. .bulk_ep_out =
  92. {
  93. .bLength = sizeof(usb_endpoint_descriptor_t),
  94. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  95. .bEndpointAddress = USB_OUT_ENDPOINT | USB_BULK_EP_RECV,
  96. .bmAttributes = USB_BULK_ENDPOINT,
  97. .wMaxPacketSize = 64,
  98. .bInterval = 0,
  99. },
  100. .bulk_ep_in =
  101. {
  102. .bLength = sizeof(usb_endpoint_descriptor_t),
  103. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  104. .bEndpointAddress = USB_IN_ENDPOINT | USB_BULK_EP_SEND,
  105. .bmAttributes = USB_BULK_ENDPOINT,
  106. .wMaxPacketSize = 64,
  107. .bInterval = 0,
  108. },
  109. // VCP
  110. .iad =
  111. {
  112. .bLength = sizeof(usb_interface_association_descriptor_t),
  113. .bDescriptorType = USB_INTERFACE_ASSOCIATION_DESCRIPTOR,
  114. .bFirstInterface = USB_INTF_CDC_COMM,
  115. .bInterfaceCount = 2,
  116. .bFunctionClass = USB_CDC_COMM_CLASS,
  117. .bFunctionSubClass = USB_CDC_ACM_SUBCLASS,
  118. .bFunctionProtocol = 0,
  119. .iFunction = USB_STR_COM_PORT,
  120. },
  121. .interface_comm =
  122. {
  123. .bLength = sizeof(usb_interface_descriptor_t),
  124. .bDescriptorType = USB_INTERFACE_DESCRIPTOR,
  125. .bInterfaceNumber = USB_INTF_CDC_COMM,
  126. .bAlternateSetting = 0,
  127. .bNumEndpoints = 1,
  128. .bInterfaceClass = USB_CDC_COMM_CLASS,
  129. .bInterfaceSubClass = USB_CDC_ACM_SUBCLASS,
  130. .bInterfaceProtocol = 0,
  131. .iInterface = 0,
  132. },
  133. .cdc_header =
  134. {
  135. .bFunctionalLength = sizeof(usb_cdc_header_functional_descriptor_t),
  136. .bDescriptorType = USB_CS_INTERFACE_DESCRIPTOR,
  137. .bDescriptorSubtype = USB_CDC_HEADER_SUBTYPE,
  138. .bcdCDC = USB_CDC_BCD_VERSION,
  139. },
  140. .cdc_acm =
  141. {
  142. .bFunctionalLength = sizeof(usb_cdc_abstract_control_managment_descriptor_t),
  143. .bDescriptorType = USB_CS_INTERFACE_DESCRIPTOR,
  144. .bDescriptorSubtype = USB_CDC_ACM_SUBTYPE,
  145. .bmCapabilities = USB_CDC_ACM_SUPPORT_LINE_REQUESTS | USB_CDC_ACM_SUPPORT_SENDBREAK_REQUESTS,
  146. },
  147. .cdc_call_mgmt =
  148. {
  149. .bFunctionalLength = sizeof(usb_cdc_call_managment_functional_descriptor_t),
  150. .bDescriptorType = USB_CS_INTERFACE_DESCRIPTOR,
  151. .bDescriptorSubtype = USB_CDC_CALL_MGMT_SUBTYPE,
  152. .bmCapabilities = USB_CDC_CALL_MGMT_OVER_DCI,
  153. .bDataInterface = USB_INTF_CDC_DATA,
  154. },
  155. .cdc_union =
  156. {
  157. .bFunctionalLength = sizeof(usb_cdc_union_functional_descriptor_t),
  158. .bDescriptorType = USB_CS_INTERFACE_DESCRIPTOR,
  159. .bDescriptorSubtype = USB_CDC_UNION_SUBTYPE,
  160. .bMasterInterface = USB_INTF_CDC_COMM,
  161. .bSlaveInterface0 = USB_INTF_CDC_DATA,
  162. },
  163. .ep_comm =
  164. {
  165. .bLength = sizeof(usb_endpoint_descriptor_t),
  166. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  167. .bEndpointAddress = USB_IN_ENDPOINT | USB_CDC_EP_COMM,
  168. .bmAttributes = USB_INTERRUPT_ENDPOINT,
  169. .wMaxPacketSize = 64,
  170. .bInterval = 1,
  171. },
  172. .interface_data =
  173. {
  174. .bLength = sizeof(usb_interface_descriptor_t),
  175. .bDescriptorType = USB_INTERFACE_DESCRIPTOR,
  176. .bInterfaceNumber = USB_INTF_CDC_DATA,
  177. .bAlternateSetting = 0,
  178. .bNumEndpoints = 2,
  179. .bInterfaceClass = USB_CDC_DATA_CLASS,
  180. .bInterfaceSubClass = USB_CDC_NO_SUBCLASS,
  181. .bInterfaceProtocol = 0,
  182. .iInterface = 0,
  183. },
  184. .ep_in =
  185. {
  186. .bLength = sizeof(usb_endpoint_descriptor_t),
  187. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  188. .bEndpointAddress = USB_IN_ENDPOINT | USB_CDC_EP_SEND,
  189. .bmAttributes = USB_BULK_ENDPOINT,
  190. .wMaxPacketSize = 64,
  191. .bInterval = 0,
  192. },
  193. .ep_out =
  194. {
  195. .bLength = sizeof(usb_endpoint_descriptor_t),
  196. .bDescriptorType = USB_ENDPOINT_DESCRIPTOR,
  197. .bEndpointAddress = USB_OUT_ENDPOINT | USB_CDC_EP_RECV,
  198. .bmAttributes = USB_BULK_ENDPOINT,
  199. .wMaxPacketSize = 64,
  200. .bInterval = 0,
  201. },
  202. };
  203. const alignas(4) usb_bos_hierarchy_t usb_bos_hierarchy =
  204. {
  205. .bos =
  206. {
  207. .bLength = sizeof(usb_binary_object_store_descriptor_t),
  208. .bDescriptorType = USB_BINARY_OBJECT_STORE_DESCRIPTOR,
  209. .wTotalLength = sizeof(usb_bos_hierarchy_t),
  210. .bNumDeviceCaps = 1,
  211. },
  212. .winusb =
  213. {
  214. .bLength = sizeof(usb_winusb_capability_descriptor_t),
  215. .bDescriptorType = USB_DEVICE_CAPABILITY_DESCRIPTOR,
  216. .bDevCapabilityType = USB_DEVICE_CAPABILITY_PLATFORM,
  217. .bReserved = 0,
  218. .PlatformCapabilityUUID = USB_WINUSB_PLATFORM_CAPABILITY_ID,
  219. .dwWindowsVersion = USB_WINUSB_WINDOWS_VERSION,
  220. .wMSOSDescriptorSetTotalLength = sizeof(usb_msos_descriptor_set_t),
  221. .bMS_VendorCode = USB_WINUSB_VENDOR_CODE,
  222. .bAltEnumCode = 0,
  223. },
  224. };
  225. const alignas(4) usb_msos_descriptor_set_t usb_msos_descriptor_set =
  226. {
  227. .header =
  228. {
  229. .wLength = sizeof(usb_winusb_set_header_descriptor_t),
  230. .wDescriptorType = USB_WINUSB_SET_HEADER_DESCRIPTOR,
  231. .dwWindowsVersion = USB_WINUSB_WINDOWS_VERSION,
  232. .wDescriptorSetTotalLength = sizeof(usb_msos_descriptor_set_t),
  233. },
  234. .subset =
  235. {
  236. .header = {
  237. .wLength = sizeof(usb_winusb_subset_header_function_t),
  238. .wDescriptorType = USB_WINUSB_SUBSET_HEADER_FUNCTION,
  239. .bFirstInterface = USB_INTF_BULK,
  240. .bReserved = 0,
  241. .wSubsetLength = sizeof(usb_msos_descriptor_subset_t),
  242. },
  243. .comp_id =
  244. {
  245. .wLength = sizeof(usb_winusb_feature_compatble_id_t),
  246. .wDescriptorType = USB_WINUSB_FEATURE_COMPATBLE_ID,
  247. .CompatibleID = "WINUSB\0\0",
  248. .SubCompatibleID = { 0 },
  249. },
  250. .property =
  251. {
  252. .wLength = sizeof(usb_winusb_feature_reg_property_guids_t),
  253. .wDescriptorType = USB_WINUSB_FEATURE_REG_PROPERTY,
  254. .wPropertyDataType = USB_WINUSB_PROPERTY_DATA_TYPE_MULTI_SZ,
  255. .wPropertyNameLength = sizeof(usb_msos_descriptor_set.subset.property.PropertyName),
  256. .PropertyName = {
  257. 'D',0,'e',0,'v',0,'i',0,'c',0,'e',0,'I',0,'n',0,'t',0,'e',0,'r',0,'f',0,'a',0,'c',0,'e',0,
  258. 'G',0,'U',0,'I',0,'D',0,'s',0, 0, 0 },
  259. .wPropertyDataLength = sizeof(usb_msos_descriptor_set.subset.property.PropertyData),
  260. .PropertyData = {
  261. '{',0,'C',0,'D',0,'B',0,'3',0,'B',0,'5',0,'A',0,'D',0,'-',0,'2',0,'9',0,'3',0,'B',0,'-',0,
  262. '4',0,'6',0,'6',0,'3',0,'-',0,'A',0,'A',0,'3',0,'6',0,'-',0,'1',0,'A',0,'A',0,'E',0,'4',0,
  263. '6',0,'4',0,'6',0,'3',0,'7',0,'7',0,'6',0,'}',0, 0, 0, 0, 0 },
  264. },
  265. },
  266. };
  267. const alignas(4) uint8_t usb_hid_report_descriptor[28] =
  268. {
  269. 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  270. 0x09, 0x00, // Usage (Undefined)
  271. 0xa1, 0x01, // Collection (Application)
  272. 0x15, 0x00, // Logical Minimum (0)
  273. 0x26, 0xff, 0x00, // Logical Maximum (255)
  274. 0x75, 0x08, // Report Size (8)
  275. 0x95, 0x40, // Report Count (64)
  276. 0x09, 0x00, // Usage (Undefined)
  277. 0x81, 0x82, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  278. 0x75, 0x08, // Report Size (8)
  279. 0x95, 0x40, // Report Count (64)
  280. 0x09, 0x00, // Usage (Undefined)
  281. 0x91, 0x82, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Volatile)
  282. 0xc0, // End Collection
  283. };
  284. const alignas(4) usb_string_descriptor_zero_t usb_string_descriptor_zero =
  285. {
  286. .bLength = sizeof(usb_string_descriptor_zero_t),
  287. .bDescriptorType = USB_STRING_DESCRIPTOR,
  288. .wLANGID = USB_LANGID_ENGLISH,
  289. };
  290. const char *usb_strings[] =
  291. {
  292. [USB_STR_MANUFACTURER] = "Alex Taradov",
  293. [USB_STR_PRODUCT] = "Combined VCP and CMSIS-DAP Adapter",
  294. [USB_STR_COM_PORT] = "Virtual COM-Port",
  295. [USB_STR_CMSIS_DAP_V1] = "CMSIS-DAP v1 Adapter",
  296. [USB_STR_CMSIS_DAP_V2] = "CMSIS-DAP v2 Adapter",
  297. [USB_STR_SERIAL_NUMBER] = usb_serial_number,
  298. };
  299. const usb_class_handler_t usb_class_handlers[3] =
  300. {
  301. usb_hid_handle_request,
  302. usb_cdc_handle_request,
  303. usb_winusb_handle_request,
  304. };
  305. char usb_serial_number[16];