usb_descriptors.c 12 KB

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