dap_v2_usb.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. #include <furi.h>
  2. #include <usb.h>
  3. #include <usb_std.h>
  4. #include <usb_hid.h>
  5. #include <usb_cdc.h>
  6. #include <furi_hal_console.h>
  7. #include "dap_v2_usb.h"
  8. // #define DAP_USB_LOG
  9. #define HID_EP_IN 0x80
  10. #define HID_EP_OUT 0x00
  11. #define DAP_HID_EP_SEND 1
  12. #define DAP_HID_EP_RECV 2
  13. #define DAP_HID_EP_BULK_RECV 3
  14. #define DAP_HID_EP_BULK_SEND 4
  15. #define DAP_CDC_EP_COMM 5
  16. #define DAP_CDC_EP_SEND 6
  17. #define DAP_CDC_EP_RECV 7
  18. #define DAP_HID_EP_IN (HID_EP_IN | DAP_HID_EP_SEND)
  19. #define DAP_HID_EP_OUT (HID_EP_OUT | DAP_HID_EP_RECV)
  20. #define DAP_HID_EP_BULK_IN (HID_EP_IN | DAP_HID_EP_BULK_SEND)
  21. #define DAP_HID_EP_BULK_OUT (HID_EP_OUT | DAP_HID_EP_BULK_RECV)
  22. #define DAP_HID_EP_SIZE 64
  23. #define DAP_CDC_COMM_EP_SIZE 8
  24. #define DAP_CDC_EP_SIZE 64
  25. #define DAP_BULK_INTERVAL 0
  26. #define DAP_HID_INTERVAL 1
  27. #define DAP_CDC_INTERVAL 0
  28. #define DAP_CDC_COMM_INTERVAL 1
  29. #define DAP_HID_VID 0x0483
  30. #define DAP_HID_PID 0x5740
  31. #define DAP_USB_EP0_SIZE 8
  32. #define EP_CFG_DECONFIGURE 0
  33. #define EP_CFG_CONFIGURE 1
  34. enum {
  35. USB_INTF_HID,
  36. USB_INTF_BULK,
  37. USB_INTF_CDC_COMM,
  38. USB_INTF_CDC_DATA,
  39. USB_INTF_COUNT,
  40. };
  41. enum {
  42. USB_STR_ZERO,
  43. USB_STR_MANUFACTURER,
  44. USB_STR_PRODUCT,
  45. USB_STR_SERIAL_NUMBER,
  46. USB_STR_CMSIS_DAP_V1,
  47. USB_STR_CMSIS_DAP_V2,
  48. USB_STR_COM_PORT,
  49. USB_STR_COUNT,
  50. };
  51. // static const char* usb_str[] = {
  52. // [USB_STR_MANUFACTURER] = "Flipper Devices Inc.",
  53. // [USB_STR_PRODUCT] = "Combined VCP and CMSIS-DAP Adapter",
  54. // [USB_STR_COM_PORT] = "Virtual COM-Port",
  55. // [USB_STR_CMSIS_DAP_V1] = "CMSIS-DAP v1 Adapter",
  56. // [USB_STR_CMSIS_DAP_V2] = "CMSIS-DAP v2 Adapter",
  57. // [USB_STR_SERIAL_NUMBER] = "01234567890ABCDEF",
  58. // };
  59. static const struct usb_string_descriptor dev_manuf_descr =
  60. USB_STRING_DESC("Flipper Devices Inc.");
  61. static const struct usb_string_descriptor dev_prod_descr =
  62. USB_STRING_DESC("Combined VCP and CMSIS-DAP Adapter");
  63. static struct usb_string_descriptor* dev_serial_descr = NULL;
  64. static const struct usb_string_descriptor dev_dap_v1_descr =
  65. USB_STRING_DESC("CMSIS-DAP v1 Adapter");
  66. static const struct usb_string_descriptor dev_dap_v2_descr =
  67. USB_STRING_DESC("CMSIS-DAP v2 Adapter");
  68. static const struct usb_string_descriptor dev_com_descr = USB_STRING_DESC("Virtual COM-Port");
  69. struct HidConfigDescriptor {
  70. struct usb_config_descriptor configuration;
  71. // CMSIS-DAP v1
  72. struct usb_interface_descriptor hid_interface;
  73. struct usb_hid_descriptor hid;
  74. struct usb_endpoint_descriptor hid_ep_in;
  75. struct usb_endpoint_descriptor hid_ep_out;
  76. // CMSIS-DAP v2
  77. struct usb_interface_descriptor bulk_interface;
  78. struct usb_endpoint_descriptor bulk_ep_out;
  79. struct usb_endpoint_descriptor bulk_ep_in;
  80. // CDC
  81. struct usb_iad_descriptor iad;
  82. struct usb_interface_descriptor interface_comm;
  83. struct usb_cdc_header_desc cdc_header;
  84. struct usb_cdc_call_mgmt_desc cdc_acm;
  85. struct usb_cdc_acm_desc cdc_call_mgmt;
  86. struct usb_cdc_union_desc cdc_union;
  87. struct usb_endpoint_descriptor ep_comm;
  88. struct usb_interface_descriptor interface_data;
  89. struct usb_endpoint_descriptor ep_in;
  90. struct usb_endpoint_descriptor ep_out;
  91. } __attribute__((packed));
  92. static const struct usb_device_descriptor hid_device_desc = {
  93. .bLength = sizeof(struct usb_device_descriptor),
  94. .bDescriptorType = USB_DTYPE_DEVICE,
  95. .bcdUSB = VERSION_BCD(2, 1, 0),
  96. .bDeviceClass = USB_CLASS_MISC,
  97. .bDeviceSubClass = USB_SUBCLASS_IAD,
  98. .bDeviceProtocol = USB_PROTO_IAD,
  99. .bMaxPacketSize0 = DAP_USB_EP0_SIZE,
  100. .idVendor = DAP_HID_VID,
  101. .idProduct = DAP_HID_PID,
  102. .bcdDevice = VERSION_BCD(1, 0, 0),
  103. .iManufacturer = USB_STR_MANUFACTURER,
  104. .iProduct = USB_STR_PRODUCT,
  105. .iSerialNumber = USB_STR_SERIAL_NUMBER,
  106. .bNumConfigurations = 1,
  107. };
  108. static const uint8_t hid_report_desc[] = {
  109. 0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
  110. 0x09, 0x00, // Usage (Undefined)
  111. 0xa1, 0x01, // Collection (Application)
  112. 0x15, 0x00, // Logical Minimum (0)
  113. 0x26, 0xff, 0x00, // Logical Maximum (255)
  114. 0x75, 0x08, // Report Size (8)
  115. 0x95, 0x40, // Report Count (64)
  116. 0x09, 0x00, // Usage (Undefined)
  117. 0x81, 0x82, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  118. 0x75, 0x08, // Report Size (8)
  119. 0x95, 0x40, // Report Count (64)
  120. 0x09, 0x00, // Usage (Undefined)
  121. 0x91, 0x82, // Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Volatile)
  122. 0xc0, // End Collection
  123. };
  124. static const struct HidConfigDescriptor hid_cfg_desc = {
  125. .configuration =
  126. {
  127. .bLength = sizeof(struct usb_config_descriptor),
  128. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  129. .wTotalLength = sizeof(struct HidConfigDescriptor),
  130. .bNumInterfaces = USB_INTF_COUNT,
  131. .bConfigurationValue = 1,
  132. .iConfiguration = NO_DESCRIPTOR,
  133. .bmAttributes = USB_CFG_ATTR_RESERVED,
  134. .bMaxPower = USB_CFG_POWER_MA(500),
  135. },
  136. // CMSIS-DAP v1
  137. .hid_interface =
  138. {
  139. .bLength = sizeof(struct usb_interface_descriptor),
  140. .bDescriptorType = USB_DTYPE_INTERFACE,
  141. .bInterfaceNumber = USB_INTF_HID,
  142. .bAlternateSetting = 0,
  143. .bNumEndpoints = 2,
  144. .bInterfaceClass = USB_CLASS_HID,
  145. .bInterfaceSubClass = USB_HID_SUBCLASS_NONBOOT,
  146. .bInterfaceProtocol = USB_HID_PROTO_NONBOOT,
  147. .iInterface = USB_STR_CMSIS_DAP_V1,
  148. },
  149. .hid =
  150. {
  151. .bLength = sizeof(struct usb_hid_descriptor),
  152. .bDescriptorType = USB_DTYPE_HID,
  153. .bcdHID = VERSION_BCD(1, 1, 1),
  154. .bCountryCode = USB_HID_COUNTRY_NONE,
  155. .bNumDescriptors = 1,
  156. .bDescriptorType0 = USB_DTYPE_HID_REPORT,
  157. .wDescriptorLength0 = sizeof(hid_report_desc),
  158. },
  159. .hid_ep_in =
  160. {
  161. .bLength = sizeof(struct usb_endpoint_descriptor),
  162. .bDescriptorType = USB_DTYPE_ENDPOINT,
  163. .bEndpointAddress = DAP_HID_EP_IN,
  164. .bmAttributes = USB_EPTYPE_INTERRUPT,
  165. .wMaxPacketSize = DAP_HID_EP_SIZE,
  166. .bInterval = DAP_HID_INTERVAL,
  167. },
  168. .hid_ep_out =
  169. {
  170. .bLength = sizeof(struct usb_endpoint_descriptor),
  171. .bDescriptorType = USB_DTYPE_ENDPOINT,
  172. .bEndpointAddress = DAP_HID_EP_OUT,
  173. .bmAttributes = USB_EPTYPE_INTERRUPT,
  174. .wMaxPacketSize = DAP_HID_EP_SIZE,
  175. .bInterval = DAP_HID_INTERVAL,
  176. },
  177. // CMSIS-DAP v2
  178. .bulk_interface =
  179. {
  180. .bLength = sizeof(struct usb_interface_descriptor),
  181. .bDescriptorType = USB_DTYPE_INTERFACE,
  182. .bInterfaceNumber = USB_INTF_BULK,
  183. .bAlternateSetting = 0,
  184. .bNumEndpoints = 2,
  185. .bInterfaceClass = USB_CLASS_VENDOR,
  186. .bInterfaceSubClass = 0,
  187. .bInterfaceProtocol = 0,
  188. .iInterface = USB_STR_CMSIS_DAP_V2,
  189. },
  190. .bulk_ep_out =
  191. {
  192. .bLength = sizeof(struct usb_endpoint_descriptor),
  193. .bDescriptorType = USB_DTYPE_ENDPOINT,
  194. .bEndpointAddress = DAP_HID_EP_BULK_OUT,
  195. .bmAttributes = USB_EPTYPE_BULK,
  196. .wMaxPacketSize = DAP_HID_EP_SIZE,
  197. .bInterval = DAP_BULK_INTERVAL,
  198. },
  199. .bulk_ep_in =
  200. {
  201. .bLength = sizeof(struct usb_endpoint_descriptor),
  202. .bDescriptorType = USB_DTYPE_ENDPOINT,
  203. .bEndpointAddress = DAP_HID_EP_BULK_IN,
  204. .bmAttributes = USB_EPTYPE_BULK,
  205. .wMaxPacketSize = DAP_HID_EP_SIZE,
  206. .bInterval = DAP_BULK_INTERVAL,
  207. },
  208. // CDC
  209. .iad =
  210. {
  211. .bLength = sizeof(struct usb_iad_descriptor),
  212. .bDescriptorType = USB_DTYPE_INTERFASEASSOC,
  213. .bFirstInterface = USB_INTF_CDC_COMM,
  214. .bInterfaceCount = 2,
  215. .bFunctionClass = USB_CLASS_CDC,
  216. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  217. .bFunctionProtocol = USB_PROTO_NONE,
  218. .iFunction = USB_STR_COM_PORT,
  219. },
  220. .interface_comm =
  221. {
  222. .bLength = sizeof(struct usb_interface_descriptor),
  223. .bDescriptorType = USB_DTYPE_INTERFACE,
  224. .bInterfaceNumber = USB_INTF_CDC_COMM,
  225. .bAlternateSetting = 0,
  226. .bNumEndpoints = 1,
  227. .bInterfaceClass = USB_CLASS_CDC,
  228. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  229. .bInterfaceProtocol = USB_PROTO_NONE,
  230. .iInterface = 0,
  231. },
  232. .cdc_header =
  233. {
  234. .bFunctionLength = sizeof(struct usb_cdc_header_desc),
  235. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  236. .bDescriptorSubType = USB_DTYPE_CDC_HEADER,
  237. .bcdCDC = VERSION_BCD(1, 1, 0),
  238. },
  239. .cdc_acm =
  240. {
  241. .bFunctionLength = sizeof(struct usb_cdc_call_mgmt_desc),
  242. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  243. .bDescriptorSubType = USB_DTYPE_CDC_CALL_MANAGEMENT,
  244. // .bmCapabilities = USB_CDC_CAP_LINE | USB_CDC_CAP_BRK,
  245. .bmCapabilities = 0,
  246. },
  247. .cdc_call_mgmt =
  248. {
  249. .bFunctionLength = sizeof(struct usb_cdc_acm_desc),
  250. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  251. .bDescriptorSubType = USB_DTYPE_CDC_ACM,
  252. .bmCapabilities = USB_CDC_CALL_MGMT_CAP_DATA_INTF,
  253. // .bDataInterface = USB_INTF_CDC_DATA,
  254. },
  255. .cdc_union =
  256. {
  257. .bFunctionLength = sizeof(struct usb_cdc_union_desc),
  258. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  259. .bDescriptorSubType = USB_DTYPE_CDC_UNION,
  260. .bMasterInterface0 = USB_INTF_CDC_COMM,
  261. .bSlaveInterface0 = USB_INTF_CDC_DATA,
  262. },
  263. .ep_comm =
  264. {
  265. .bLength = sizeof(struct usb_endpoint_descriptor),
  266. .bDescriptorType = USB_DTYPE_ENDPOINT,
  267. .bEndpointAddress = HID_EP_IN | DAP_CDC_EP_COMM,
  268. .bmAttributes = USB_EPTYPE_INTERRUPT,
  269. .wMaxPacketSize = DAP_CDC_COMM_EP_SIZE,
  270. .bInterval = DAP_CDC_COMM_INTERVAL,
  271. },
  272. .interface_data =
  273. {
  274. .bLength = sizeof(struct usb_interface_descriptor),
  275. .bDescriptorType = USB_DTYPE_INTERFACE,
  276. .bInterfaceNumber = USB_INTF_CDC_DATA,
  277. .bAlternateSetting = 0,
  278. .bNumEndpoints = 2,
  279. .bInterfaceClass = USB_CLASS_CDC_DATA,
  280. .bInterfaceSubClass = USB_SUBCLASS_NONE,
  281. .bInterfaceProtocol = USB_PROTO_NONE,
  282. .iInterface = NO_DESCRIPTOR,
  283. },
  284. .ep_in =
  285. {
  286. .bLength = sizeof(struct usb_endpoint_descriptor),
  287. .bDescriptorType = USB_DTYPE_ENDPOINT,
  288. .bEndpointAddress = HID_EP_IN | DAP_CDC_EP_SEND,
  289. .bmAttributes = USB_EPTYPE_BULK,
  290. .wMaxPacketSize = DAP_CDC_EP_SIZE,
  291. .bInterval = DAP_CDC_INTERVAL,
  292. },
  293. .ep_out =
  294. {
  295. .bLength = sizeof(struct usb_endpoint_descriptor),
  296. .bDescriptorType = USB_DTYPE_ENDPOINT,
  297. .bEndpointAddress = HID_EP_OUT | DAP_CDC_EP_RECV,
  298. .bmAttributes = USB_EPTYPE_BULK,
  299. .wMaxPacketSize = DAP_CDC_EP_SIZE,
  300. .bInterval = DAP_CDC_INTERVAL,
  301. },
  302. };
  303. // WinUSB
  304. #include "usb_winusb.h"
  305. typedef struct USB_PACK {
  306. usb_binary_object_store_descriptor_t bos;
  307. usb_winusb_capability_descriptor_t winusb;
  308. } usb_bos_hierarchy_t;
  309. typedef struct USB_PACK {
  310. usb_winusb_subset_header_function_t header;
  311. usb_winusb_feature_compatble_id_t comp_id;
  312. usb_winusb_feature_reg_property_guids_t property;
  313. } usb_msos_descriptor_subset_t;
  314. typedef struct USB_PACK {
  315. usb_winusb_set_header_descriptor_t header;
  316. usb_msos_descriptor_subset_t subset;
  317. } usb_msos_descriptor_set_t;
  318. #define USB_DTYPE_BINARY_OBJECT_STORE 15
  319. #define USB_DTYPE_DEVICE_CAPABILITY_DESCRIPTOR 16
  320. #define USB_DC_TYPE_PLATFORM 5
  321. const usb_bos_hierarchy_t usb_bos_hierarchy = {
  322. .bos =
  323. {
  324. .bLength = sizeof(usb_binary_object_store_descriptor_t),
  325. .bDescriptorType = USB_DTYPE_BINARY_OBJECT_STORE,
  326. .wTotalLength = sizeof(usb_bos_hierarchy_t),
  327. .bNumDeviceCaps = 1,
  328. },
  329. .winusb =
  330. {
  331. .bLength = sizeof(usb_winusb_capability_descriptor_t),
  332. .bDescriptorType = USB_DTYPE_DEVICE_CAPABILITY_DESCRIPTOR,
  333. .bDevCapabilityType = USB_DC_TYPE_PLATFORM,
  334. .bReserved = 0,
  335. .PlatformCapabilityUUID = USB_WINUSB_PLATFORM_CAPABILITY_ID,
  336. .dwWindowsVersion = USB_WINUSB_WINDOWS_VERSION,
  337. .wMSOSDescriptorSetTotalLength = sizeof(usb_msos_descriptor_set_t),
  338. .bMS_VendorCode = USB_WINUSB_VENDOR_CODE,
  339. .bAltEnumCode = 0,
  340. },
  341. };
  342. const usb_msos_descriptor_set_t usb_msos_descriptor_set = {
  343. .header =
  344. {
  345. .wLength = sizeof(usb_winusb_set_header_descriptor_t),
  346. .wDescriptorType = USB_WINUSB_SET_HEADER_DESCRIPTOR,
  347. .dwWindowsVersion = USB_WINUSB_WINDOWS_VERSION,
  348. .wDescriptorSetTotalLength = sizeof(usb_msos_descriptor_set_t),
  349. },
  350. .subset =
  351. {
  352. .header =
  353. {
  354. .wLength = sizeof(usb_winusb_subset_header_function_t),
  355. .wDescriptorType = USB_WINUSB_SUBSET_HEADER_FUNCTION,
  356. .bFirstInterface = USB_INTF_BULK,
  357. .bReserved = 0,
  358. .wSubsetLength = sizeof(usb_msos_descriptor_subset_t),
  359. },
  360. .comp_id =
  361. {
  362. .wLength = sizeof(usb_winusb_feature_compatble_id_t),
  363. .wDescriptorType = USB_WINUSB_FEATURE_COMPATBLE_ID,
  364. .CompatibleID = "WINUSB\0\0",
  365. .SubCompatibleID = {0},
  366. },
  367. .property =
  368. {
  369. .wLength = sizeof(usb_winusb_feature_reg_property_guids_t),
  370. .wDescriptorType = USB_WINUSB_FEATURE_REG_PROPERTY,
  371. .wPropertyDataType = USB_WINUSB_PROPERTY_DATA_TYPE_MULTI_SZ,
  372. .wPropertyNameLength =
  373. sizeof(usb_msos_descriptor_set.subset.property.PropertyName),
  374. .PropertyName = {'D', 0, 'e', 0, 'v', 0, 'i', 0, 'c', 0, 'e', 0, 'I', 0,
  375. 'n', 0, 't', 0, 'e', 0, 'r', 0, 'f', 0, 'a', 0, 'c', 0,
  376. 'e', 0, 'G', 0, 'U', 0, 'I', 0, 'D', 0, 's', 0, 0, 0},
  377. .wPropertyDataLength =
  378. sizeof(usb_msos_descriptor_set.subset.property.PropertyData),
  379. .PropertyData = {'{', 0, 'C', 0, 'D', 0, 'B', 0, '3', 0, 'B', 0, '5', 0,
  380. 'A', 0, 'D', 0, '-', 0, '2', 0, '9', 0, '3', 0, 'B', 0,
  381. '-', 0, '4', 0, '6', 0, '6', 0, '3', 0, '-', 0, 'A', 0,
  382. 'A', 0, '3', 0, '6', 0, '-', 0, '1', 0, 'A', 0, 'A', 0,
  383. 'E', 0, '4', 0, '6', 0, '4', 0, '6', 0, '3', 0, '7', 0,
  384. '7', 0, '6', 0, '}', 0, 0, 0, 0, 0},
  385. },
  386. },
  387. };
  388. typedef struct {
  389. FuriSemaphore* semaphore_v1;
  390. FuriSemaphore* semaphore_v2;
  391. FuriSemaphore* semaphore_cdc;
  392. bool connected;
  393. usbd_device* usb_dev;
  394. DapStateCallback state_callback;
  395. DapRxCallback rx_callback_v1;
  396. DapRxCallback rx_callback_v2;
  397. DapRxCallback rx_callback_cdc;
  398. DapRxCallback tx_complete_cdc;
  399. DapCDCControlLineCallback control_line_callback_cdc;
  400. DapCDCConfigCallback config_callback_cdc;
  401. void* context;
  402. void* context_cdc;
  403. } DAPState;
  404. static DAPState dap_state = {
  405. .semaphore_v1 = NULL,
  406. .semaphore_v2 = NULL,
  407. .semaphore_cdc = NULL,
  408. .connected = false,
  409. .usb_dev = NULL,
  410. .state_callback = NULL,
  411. .rx_callback_v1 = NULL,
  412. .rx_callback_v2 = NULL,
  413. .rx_callback_cdc = NULL,
  414. .control_line_callback_cdc = NULL,
  415. .config_callback_cdc = NULL,
  416. .context = NULL,
  417. .context_cdc = NULL,
  418. };
  419. static struct usb_cdc_line_coding cdc_config = {0};
  420. static uint8_t cdc_ctrl_line_state = 0;
  421. #ifdef DAP_USB_LOG
  422. void furi_console_log_printf(const char* format, ...) _ATTRIBUTE((__format__(__printf__, 1, 2)));
  423. void furi_console_log_printf(const char* format, ...) {
  424. char buffer[256];
  425. va_list args;
  426. va_start(args, format);
  427. vsnprintf(buffer, sizeof(buffer), format, args);
  428. va_end(args);
  429. furi_hal_console_puts(buffer);
  430. furi_hal_console_puts("\r\n");
  431. UNUSED(format);
  432. }
  433. #else
  434. #define furi_console_log_printf(...)
  435. #endif
  436. int32_t dap_v1_usb_tx(uint8_t* buffer, uint8_t size) {
  437. if((dap_state.semaphore_v1 == NULL) || (dap_state.connected == false)) return 0;
  438. furi_check(furi_semaphore_acquire(dap_state.semaphore_v1, FuriWaitForever) == FuriStatusOk);
  439. if(dap_state.connected) {
  440. int32_t len = usbd_ep_write(dap_state.usb_dev, DAP_HID_EP_IN, buffer, size);
  441. furi_console_log_printf("v1 tx %ld", len);
  442. return len;
  443. } else {
  444. return 0;
  445. }
  446. }
  447. int32_t dap_v2_usb_tx(uint8_t* buffer, uint8_t size) {
  448. if((dap_state.semaphore_v2 == NULL) || (dap_state.connected == false)) return 0;
  449. furi_check(furi_semaphore_acquire(dap_state.semaphore_v2, FuriWaitForever) == FuriStatusOk);
  450. if(dap_state.connected) {
  451. int32_t len = usbd_ep_write(dap_state.usb_dev, DAP_HID_EP_BULK_IN, buffer, size);
  452. furi_console_log_printf("v2 tx %ld", len);
  453. return len;
  454. } else {
  455. return 0;
  456. }
  457. }
  458. int32_t dap_cdc_usb_tx(uint8_t* buffer, uint8_t size) {
  459. if((dap_state.semaphore_cdc == NULL) || (dap_state.connected == false)) return 0;
  460. if(furi_semaphore_acquire(dap_state.semaphore_cdc, 100) == FuriStatusOk) {
  461. if(dap_state.connected) {
  462. int32_t len =
  463. usbd_ep_write(dap_state.usb_dev, HID_EP_IN | DAP_CDC_EP_SEND, buffer, size);
  464. furi_console_log_printf("cdc tx %ld", len);
  465. return len;
  466. }
  467. }
  468. return 0;
  469. }
  470. void dap_v1_usb_set_rx_callback(DapRxCallback callback) {
  471. dap_state.rx_callback_v1 = callback;
  472. }
  473. void dap_v2_usb_set_rx_callback(DapRxCallback callback) {
  474. dap_state.rx_callback_v2 = callback;
  475. }
  476. void dap_cdc_usb_set_rx_callback(DapRxCallback callback) {
  477. dap_state.rx_callback_cdc = callback;
  478. }
  479. void dap_cdc_usb_set_tx_complete_callback(DapRxCallback callback) {
  480. dap_state.tx_complete_cdc = callback;
  481. }
  482. void dap_cdc_usb_set_control_line_callback(DapCDCControlLineCallback callback) {
  483. dap_state.control_line_callback_cdc = callback;
  484. }
  485. void dap_cdc_usb_set_config_callback(DapCDCConfigCallback callback) {
  486. dap_state.config_callback_cdc = callback;
  487. }
  488. void dap_cdc_usb_set_context(void* context) {
  489. dap_state.context_cdc = context;
  490. }
  491. void dap_common_usb_set_context(void* context) {
  492. dap_state.context = context;
  493. }
  494. void dap_common_usb_set_state_callback(DapStateCallback callback) {
  495. dap_state.state_callback = callback;
  496. }
  497. static void* dap_usb_alloc_string_descr(const char* str) {
  498. furi_assert(str);
  499. size_t len = strlen(str);
  500. size_t wlen = (len + 1) * sizeof(uint16_t);
  501. struct usb_string_descriptor* dev_str_desc = malloc(wlen);
  502. dev_str_desc->bLength = wlen;
  503. dev_str_desc->bDescriptorType = USB_DTYPE_STRING;
  504. for(size_t i = 0; i < len; i++) {
  505. dev_str_desc->wString[i] = str[i];
  506. }
  507. return dev_str_desc;
  508. }
  509. void dap_common_usb_alloc_name(const char* name) {
  510. dev_serial_descr = dap_usb_alloc_string_descr(name);
  511. }
  512. void dap_common_usb_free_name() {
  513. free(dev_serial_descr);
  514. }
  515. static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
  516. static void hid_deinit(usbd_device* dev);
  517. static void hid_on_wakeup(usbd_device* dev);
  518. static void hid_on_suspend(usbd_device* dev);
  519. static usbd_respond hid_ep_config(usbd_device* dev, uint8_t cfg);
  520. static usbd_respond hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
  521. FuriHalUsbInterface dap_v2_usb_hid = {
  522. .init = hid_init,
  523. .deinit = hid_deinit,
  524. .wakeup = hid_on_wakeup,
  525. .suspend = hid_on_suspend,
  526. .dev_descr = (struct usb_device_descriptor*)&hid_device_desc,
  527. .cfg_descr = (void*)&hid_cfg_desc,
  528. };
  529. static void hid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  530. UNUSED(intf);
  531. UNUSED(ctx);
  532. dap_v2_usb_hid.str_manuf_descr = (void*)&dev_manuf_descr;
  533. dap_v2_usb_hid.str_prod_descr = (void*)&dev_prod_descr;
  534. dap_v2_usb_hid.str_serial_descr = (void*)dev_serial_descr;
  535. dap_state.usb_dev = dev;
  536. if(dap_state.semaphore_v1 == NULL) dap_state.semaphore_v1 = furi_semaphore_alloc(1, 1);
  537. if(dap_state.semaphore_v2 == NULL) dap_state.semaphore_v2 = furi_semaphore_alloc(1, 1);
  538. if(dap_state.semaphore_cdc == NULL) dap_state.semaphore_cdc = furi_semaphore_alloc(1, 1);
  539. usbd_reg_config(dev, hid_ep_config);
  540. usbd_reg_control(dev, hid_control);
  541. usbd_connect(dev, true);
  542. }
  543. static void hid_deinit(usbd_device* dev) {
  544. dap_state.usb_dev = NULL;
  545. furi_semaphore_free(dap_state.semaphore_v1);
  546. furi_semaphore_free(dap_state.semaphore_v2);
  547. furi_semaphore_free(dap_state.semaphore_cdc);
  548. dap_state.semaphore_v1 = NULL;
  549. dap_state.semaphore_v2 = NULL;
  550. dap_state.semaphore_cdc = NULL;
  551. usbd_reg_config(dev, NULL);
  552. usbd_reg_control(dev, NULL);
  553. }
  554. static void hid_on_wakeup(usbd_device* dev) {
  555. UNUSED(dev);
  556. if(!dap_state.connected) {
  557. dap_state.connected = true;
  558. if(dap_state.state_callback != NULL) {
  559. dap_state.state_callback(dap_state.connected, dap_state.context);
  560. }
  561. }
  562. }
  563. static void hid_on_suspend(usbd_device* dev) {
  564. UNUSED(dev);
  565. if(dap_state.connected) {
  566. dap_state.connected = false;
  567. if(dap_state.state_callback != NULL) {
  568. dap_state.state_callback(dap_state.connected, dap_state.context);
  569. }
  570. }
  571. }
  572. size_t dap_v1_usb_rx(uint8_t* buffer, size_t size) {
  573. size_t len = 0;
  574. if(dap_state.connected) {
  575. len = usbd_ep_read(dap_state.usb_dev, DAP_HID_EP_OUT, buffer, size);
  576. }
  577. return len;
  578. }
  579. size_t dap_v2_usb_rx(uint8_t* buffer, size_t size) {
  580. size_t len = 0;
  581. if(dap_state.connected) {
  582. len = usbd_ep_read(dap_state.usb_dev, DAP_HID_EP_BULK_OUT, buffer, size);
  583. }
  584. return len;
  585. }
  586. size_t dap_cdc_usb_rx(uint8_t* buffer, size_t size) {
  587. size_t len = 0;
  588. if(dap_state.connected) {
  589. len = usbd_ep_read(dap_state.usb_dev, HID_EP_OUT | DAP_CDC_EP_RECV, buffer, size);
  590. }
  591. return len;
  592. }
  593. static void hid_txrx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  594. UNUSED(dev);
  595. UNUSED(ep);
  596. switch(event) {
  597. case usbd_evt_eptx:
  598. furi_semaphore_release(dap_state.semaphore_v1);
  599. furi_console_log_printf("hid tx complete");
  600. break;
  601. case usbd_evt_eprx:
  602. if(dap_state.rx_callback_v1 != NULL) {
  603. dap_state.rx_callback_v1(dap_state.context);
  604. }
  605. break;
  606. default:
  607. furi_console_log_printf("hid %d, %d", event, ep);
  608. break;
  609. }
  610. }
  611. static void hid_txrx_ep_bulk_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  612. UNUSED(dev);
  613. UNUSED(ep);
  614. switch(event) {
  615. case usbd_evt_eptx:
  616. furi_semaphore_release(dap_state.semaphore_v2);
  617. furi_console_log_printf("bulk tx complete");
  618. break;
  619. case usbd_evt_eprx:
  620. if(dap_state.rx_callback_v2 != NULL) {
  621. dap_state.rx_callback_v2(dap_state.context);
  622. }
  623. break;
  624. default:
  625. furi_console_log_printf("bulk %d, %d", event, ep);
  626. break;
  627. }
  628. }
  629. static void cdc_txrx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  630. UNUSED(dev);
  631. UNUSED(ep);
  632. switch(event) {
  633. case usbd_evt_eptx:
  634. furi_semaphore_release(dap_state.semaphore_cdc);
  635. if(dap_state.tx_complete_cdc != NULL) {
  636. dap_state.tx_complete_cdc(dap_state.context_cdc);
  637. }
  638. furi_console_log_printf("cdc tx complete");
  639. break;
  640. case usbd_evt_eprx:
  641. if(dap_state.rx_callback_cdc != NULL) {
  642. dap_state.rx_callback_cdc(dap_state.context_cdc);
  643. }
  644. break;
  645. default:
  646. furi_console_log_printf("cdc %d, %d", event, ep);
  647. break;
  648. }
  649. }
  650. static usbd_respond hid_ep_config(usbd_device* dev, uint8_t cfg) {
  651. switch(cfg) {
  652. case EP_CFG_DECONFIGURE:
  653. usbd_ep_deconfig(dev, DAP_HID_EP_OUT);
  654. usbd_ep_deconfig(dev, DAP_HID_EP_IN);
  655. usbd_ep_deconfig(dev, DAP_HID_EP_BULK_IN);
  656. usbd_ep_deconfig(dev, DAP_HID_EP_BULK_OUT);
  657. usbd_ep_deconfig(dev, HID_EP_IN | DAP_CDC_EP_COMM);
  658. usbd_ep_deconfig(dev, HID_EP_IN | DAP_CDC_EP_SEND);
  659. usbd_ep_deconfig(dev, HID_EP_OUT | DAP_CDC_EP_RECV);
  660. usbd_reg_endpoint(dev, DAP_HID_EP_OUT, NULL);
  661. usbd_reg_endpoint(dev, DAP_HID_EP_IN, NULL);
  662. usbd_reg_endpoint(dev, DAP_HID_EP_BULK_IN, NULL);
  663. usbd_reg_endpoint(dev, DAP_HID_EP_BULK_OUT, NULL);
  664. usbd_reg_endpoint(dev, HID_EP_IN | DAP_CDC_EP_SEND, 0);
  665. usbd_reg_endpoint(dev, HID_EP_OUT | DAP_CDC_EP_RECV, 0);
  666. return usbd_ack;
  667. case EP_CFG_CONFIGURE:
  668. usbd_ep_config(dev, DAP_HID_EP_IN, USB_EPTYPE_INTERRUPT, DAP_HID_EP_SIZE);
  669. usbd_ep_config(dev, DAP_HID_EP_OUT, USB_EPTYPE_INTERRUPT, DAP_HID_EP_SIZE);
  670. usbd_ep_config(dev, DAP_HID_EP_BULK_OUT, USB_EPTYPE_BULK, DAP_HID_EP_SIZE);
  671. usbd_ep_config(dev, DAP_HID_EP_BULK_IN, USB_EPTYPE_BULK, DAP_HID_EP_SIZE);
  672. usbd_ep_config(dev, HID_EP_OUT | DAP_CDC_EP_RECV, USB_EPTYPE_BULK, DAP_CDC_EP_SIZE);
  673. usbd_ep_config(dev, HID_EP_IN | DAP_CDC_EP_SEND, USB_EPTYPE_BULK, DAP_CDC_EP_SIZE);
  674. usbd_ep_config(dev, HID_EP_IN | DAP_CDC_EP_COMM, USB_EPTYPE_INTERRUPT, DAP_CDC_EP_SIZE);
  675. usbd_reg_endpoint(dev, DAP_HID_EP_IN, hid_txrx_ep_callback);
  676. usbd_reg_endpoint(dev, DAP_HID_EP_OUT, hid_txrx_ep_callback);
  677. usbd_reg_endpoint(dev, DAP_HID_EP_BULK_OUT, hid_txrx_ep_bulk_callback);
  678. usbd_reg_endpoint(dev, DAP_HID_EP_BULK_IN, hid_txrx_ep_bulk_callback);
  679. usbd_reg_endpoint(dev, HID_EP_OUT | DAP_CDC_EP_RECV, cdc_txrx_ep_callback);
  680. usbd_reg_endpoint(dev, HID_EP_IN | DAP_CDC_EP_SEND, cdc_txrx_ep_callback);
  681. // usbd_ep_write(dev, DAP_HID_EP_IN, NULL, 0);
  682. // usbd_ep_write(dev, DAP_HID_EP_BULK_IN, NULL, 0);
  683. // usbd_ep_write(dev, HID_EP_IN | DAP_CDC_EP_SEND, NULL, 0);
  684. return usbd_ack;
  685. default:
  686. return usbd_fail;
  687. }
  688. }
  689. #ifdef DAP_USB_LOG
  690. static void dump_request_type(uint8_t type) {
  691. switch(type & USB_REQ_DIRECTION) {
  692. case USB_REQ_HOSTTODEV:
  693. furi_hal_console_puts("host to dev, ");
  694. break;
  695. case USB_REQ_DEVTOHOST:
  696. furi_hal_console_puts("dev to host, ");
  697. break;
  698. }
  699. switch(type & USB_REQ_TYPE) {
  700. case USB_REQ_STANDARD:
  701. furi_hal_console_puts("standard, ");
  702. break;
  703. case USB_REQ_CLASS:
  704. furi_hal_console_puts("class, ");
  705. break;
  706. case USB_REQ_VENDOR:
  707. furi_hal_console_puts("vendor, ");
  708. break;
  709. }
  710. switch(type & USB_REQ_RECIPIENT) {
  711. case USB_REQ_DEVICE:
  712. furi_hal_console_puts("device");
  713. break;
  714. case USB_REQ_INTERFACE:
  715. furi_hal_console_puts("interface");
  716. break;
  717. case USB_REQ_ENDPOINT:
  718. furi_hal_console_puts("endpoint");
  719. break;
  720. case USB_REQ_OTHER:
  721. furi_hal_console_puts("other");
  722. break;
  723. }
  724. furi_hal_console_puts("\r\n");
  725. }
  726. #else
  727. #define dump_request_type(...)
  728. #endif
  729. static usbd_respond hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  730. UNUSED(callback);
  731. dump_request_type(req->bmRequestType);
  732. furi_console_log_printf(
  733. "control: RT %02x, R %02x, V %04x, I %04x, L %04x",
  734. req->bmRequestType,
  735. req->bRequest,
  736. req->wValue,
  737. req->wIndex,
  738. req->wLength);
  739. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE | USB_REQ_DIRECTION) & req->bmRequestType) ==
  740. (USB_REQ_STANDARD | USB_REQ_VENDOR | USB_REQ_DEVTOHOST)) {
  741. // vendor request, device to host
  742. furi_console_log_printf("vendor request");
  743. if(USB_WINUSB_VENDOR_CODE == req->bRequest) {
  744. // WINUSB request
  745. if(USB_WINUSB_DESCRIPTOR_INDEX == req->wIndex) {
  746. furi_console_log_printf("WINUSB descriptor");
  747. uint16_t length = req->wLength;
  748. if(length > sizeof(usb_msos_descriptor_set_t)) {
  749. length = sizeof(usb_msos_descriptor_set_t);
  750. }
  751. dev->status.data_ptr = (uint8_t*)&usb_msos_descriptor_set;
  752. dev->status.data_count = length;
  753. return usbd_ack;
  754. }
  755. }
  756. }
  757. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  758. (USB_REQ_STANDARD | USB_REQ_DEVICE)) {
  759. // device request
  760. if(req->bRequest == USB_STD_GET_DESCRIPTOR) {
  761. const uint8_t dtype = req->wValue >> 8;
  762. const uint8_t dnumber = req->wValue & 0xFF;
  763. // get string descriptor
  764. if(USB_DTYPE_STRING == dtype) {
  765. if(dnumber == USB_STR_CMSIS_DAP_V1) {
  766. furi_console_log_printf("str CMSIS-DAP v1");
  767. dev->status.data_ptr = (uint8_t*)&dev_dap_v1_descr;
  768. dev->status.data_count = dev_dap_v1_descr.bLength;
  769. return usbd_ack;
  770. } else if(dnumber == USB_STR_CMSIS_DAP_V2) {
  771. furi_console_log_printf("str CMSIS-DAP v2");
  772. dev->status.data_ptr = (uint8_t*)&dev_dap_v2_descr;
  773. dev->status.data_count = dev_dap_v2_descr.bLength;
  774. return usbd_ack;
  775. } else if(dnumber == USB_STR_COM_PORT) {
  776. furi_console_log_printf("str COM port");
  777. dev->status.data_ptr = (uint8_t*)&dev_com_descr;
  778. dev->status.data_count = dev_com_descr.bLength;
  779. return usbd_ack;
  780. }
  781. } else if(USB_DTYPE_BINARY_OBJECT_STORE == dtype) {
  782. furi_console_log_printf("BOS descriptor");
  783. uint16_t length = req->wLength;
  784. if(length > sizeof(usb_bos_hierarchy_t)) {
  785. length = sizeof(usb_bos_hierarchy_t);
  786. }
  787. dev->status.data_ptr = (uint8_t*)&usb_bos_hierarchy;
  788. dev->status.data_count = length;
  789. return usbd_ack;
  790. }
  791. }
  792. }
  793. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  794. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  795. req->wIndex == 0) {
  796. // class request
  797. switch(req->bRequest) {
  798. // get hid descriptor
  799. case USB_HID_GETREPORT:
  800. furi_console_log_printf("get report");
  801. return usbd_fail;
  802. // set hid idle
  803. case USB_HID_SETIDLE:
  804. furi_console_log_printf("set idle");
  805. return usbd_ack;
  806. default:
  807. break;
  808. }
  809. }
  810. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  811. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  812. req->wIndex == 2) {
  813. // class request
  814. switch(req->bRequest) {
  815. // control line state
  816. case USB_CDC_SET_CONTROL_LINE_STATE:
  817. furi_console_log_printf("set control line state");
  818. cdc_ctrl_line_state = req->wValue;
  819. if(dap_state.control_line_callback_cdc != NULL) {
  820. dap_state.control_line_callback_cdc(cdc_ctrl_line_state, dap_state.context_cdc);
  821. }
  822. return usbd_ack;
  823. // set cdc line coding
  824. case USB_CDC_SET_LINE_CODING:
  825. furi_console_log_printf("set line coding");
  826. memcpy(&cdc_config, req->data, sizeof(cdc_config));
  827. if(dap_state.config_callback_cdc != NULL) {
  828. dap_state.config_callback_cdc(&cdc_config, dap_state.context_cdc);
  829. }
  830. return usbd_ack;
  831. // get cdc line coding
  832. case USB_CDC_GET_LINE_CODING:
  833. furi_console_log_printf("get line coding");
  834. dev->status.data_ptr = &cdc_config;
  835. dev->status.data_count = sizeof(cdc_config);
  836. return usbd_ack;
  837. default:
  838. break;
  839. }
  840. }
  841. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  842. (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
  843. req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
  844. // standard request
  845. switch(req->wValue >> 8) {
  846. // get hid descriptor
  847. case USB_DTYPE_HID:
  848. furi_console_log_printf("get hid descriptor");
  849. dev->status.data_ptr = (uint8_t*)&(hid_cfg_desc.hid);
  850. dev->status.data_count = sizeof(hid_cfg_desc.hid);
  851. return usbd_ack;
  852. // get hid report descriptor
  853. case USB_DTYPE_HID_REPORT:
  854. furi_console_log_printf("get hid report descriptor");
  855. dev->status.data_ptr = (uint8_t*)hid_report_desc;
  856. dev->status.data_count = sizeof(hid_report_desc);
  857. return usbd_ack;
  858. default:
  859. break;
  860. }
  861. }
  862. return usbd_fail;
  863. }