furi_hal_usb_cdc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. #include "furi_hal_version.h"
  2. #include "furi_hal_usb_i.h"
  3. #include "furi_hal_usb.h"
  4. #include "furi_hal_usb_cdc.h"
  5. #include <furi.h>
  6. #include "usb.h"
  7. #include "usb_cdc.h"
  8. #define CDC0_RXD_EP 0x01
  9. #define CDC0_TXD_EP 0x82
  10. #define CDC0_NTF_EP 0x83
  11. #define CDC1_RXD_EP 0x04
  12. #define CDC1_TXD_EP 0x85
  13. #define CDC1_NTF_EP 0x86
  14. #define CDC_NTF_SZ 0x08
  15. #define IF_NUM_MAX 2
  16. struct CdcIadDescriptor {
  17. struct usb_iad_descriptor comm_iad;
  18. struct usb_interface_descriptor comm;
  19. struct usb_cdc_header_desc cdc_hdr;
  20. struct usb_cdc_call_mgmt_desc cdc_mgmt;
  21. struct usb_cdc_acm_desc cdc_acm;
  22. struct usb_cdc_union_desc cdc_union;
  23. struct usb_endpoint_descriptor comm_ep;
  24. struct usb_interface_descriptor data;
  25. struct usb_endpoint_descriptor data_eprx;
  26. struct usb_endpoint_descriptor data_eptx;
  27. };
  28. struct CdcConfigDescriptorSingle {
  29. struct usb_config_descriptor config;
  30. struct CdcIadDescriptor iad_0;
  31. } __attribute__((packed));
  32. struct CdcConfigDescriptorDual {
  33. struct usb_config_descriptor config;
  34. struct CdcIadDescriptor iad_0;
  35. struct CdcIadDescriptor iad_1;
  36. } __attribute__((packed));
  37. static const struct usb_string_descriptor dev_manuf_desc = USB_STRING_DESC("Flipper Devices Inc.");
  38. /* Device descriptor */
  39. static const struct usb_device_descriptor cdc_device_desc = {
  40. .bLength = sizeof(struct usb_device_descriptor),
  41. .bDescriptorType = USB_DTYPE_DEVICE,
  42. .bcdUSB = VERSION_BCD(2, 0, 0),
  43. .bDeviceClass = USB_CLASS_IAD,
  44. .bDeviceSubClass = USB_SUBCLASS_IAD,
  45. .bDeviceProtocol = USB_PROTO_IAD,
  46. .bMaxPacketSize0 = USB_EP0_SIZE,
  47. .idVendor = 0x0483,
  48. .idProduct = 0x5740,
  49. .bcdDevice = VERSION_BCD(1, 0, 0),
  50. .iManufacturer = UsbDevManuf,
  51. .iProduct = UsbDevProduct,
  52. .iSerialNumber = UsbDevSerial,
  53. .bNumConfigurations = 1,
  54. };
  55. /* Device configuration descriptor - single mode*/
  56. static const struct CdcConfigDescriptorSingle cdc_cfg_desc_single = {
  57. .config =
  58. {
  59. .bLength = sizeof(struct usb_config_descriptor),
  60. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  61. .wTotalLength = sizeof(struct CdcConfigDescriptorSingle),
  62. .bNumInterfaces = 2,
  63. .bConfigurationValue = 1,
  64. .iConfiguration = NO_DESCRIPTOR,
  65. .bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
  66. .bMaxPower = USB_CFG_POWER_MA(100),
  67. },
  68. .iad_0 =
  69. {
  70. .comm_iad =
  71. {
  72. .bLength = sizeof(struct usb_iad_descriptor),
  73. .bDescriptorType = USB_DTYPE_INTERFASEASSOC,
  74. .bFirstInterface = 0,
  75. .bInterfaceCount = 2,
  76. .bFunctionClass = USB_CLASS_CDC,
  77. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  78. .bFunctionProtocol = USB_PROTO_NONE,
  79. .iFunction = NO_DESCRIPTOR,
  80. },
  81. .comm =
  82. {
  83. .bLength = sizeof(struct usb_interface_descriptor),
  84. .bDescriptorType = USB_DTYPE_INTERFACE,
  85. .bInterfaceNumber = 0,
  86. .bAlternateSetting = 0,
  87. .bNumEndpoints = 1,
  88. .bInterfaceClass = USB_CLASS_CDC,
  89. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  90. .bInterfaceProtocol = USB_PROTO_NONE,
  91. .iInterface = NO_DESCRIPTOR,
  92. },
  93. .cdc_hdr =
  94. {
  95. .bFunctionLength = sizeof(struct usb_cdc_header_desc),
  96. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  97. .bDescriptorSubType = USB_DTYPE_CDC_HEADER,
  98. .bcdCDC = VERSION_BCD(1, 1, 0),
  99. },
  100. .cdc_mgmt =
  101. {
  102. .bFunctionLength = sizeof(struct usb_cdc_call_mgmt_desc),
  103. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  104. .bDescriptorSubType = USB_DTYPE_CDC_CALL_MANAGEMENT,
  105. .bmCapabilities = 0,
  106. .bDataInterface = 1,
  107. },
  108. .cdc_acm =
  109. {
  110. .bFunctionLength = sizeof(struct usb_cdc_acm_desc),
  111. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  112. .bDescriptorSubType = USB_DTYPE_CDC_ACM,
  113. .bmCapabilities = 0,
  114. },
  115. .cdc_union =
  116. {
  117. .bFunctionLength = sizeof(struct usb_cdc_union_desc),
  118. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  119. .bDescriptorSubType = USB_DTYPE_CDC_UNION,
  120. .bMasterInterface0 = 0,
  121. .bSlaveInterface0 = 1,
  122. },
  123. .comm_ep =
  124. {
  125. .bLength = sizeof(struct usb_endpoint_descriptor),
  126. .bDescriptorType = USB_DTYPE_ENDPOINT,
  127. .bEndpointAddress = CDC0_NTF_EP,
  128. .bmAttributes = USB_EPTYPE_INTERRUPT,
  129. .wMaxPacketSize = CDC_NTF_SZ,
  130. .bInterval = 0xFF,
  131. },
  132. .data =
  133. {
  134. .bLength = sizeof(struct usb_interface_descriptor),
  135. .bDescriptorType = USB_DTYPE_INTERFACE,
  136. .bInterfaceNumber = 1,
  137. .bAlternateSetting = 0,
  138. .bNumEndpoints = 2,
  139. .bInterfaceClass = USB_CLASS_CDC_DATA,
  140. .bInterfaceSubClass = USB_SUBCLASS_NONE,
  141. .bInterfaceProtocol = USB_PROTO_NONE,
  142. .iInterface = NO_DESCRIPTOR,
  143. },
  144. .data_eprx =
  145. {
  146. .bLength = sizeof(struct usb_endpoint_descriptor),
  147. .bDescriptorType = USB_DTYPE_ENDPOINT,
  148. .bEndpointAddress = CDC0_RXD_EP,
  149. .bmAttributes = USB_EPTYPE_BULK,
  150. .wMaxPacketSize = CDC_DATA_SZ,
  151. .bInterval = 0x01,
  152. },
  153. .data_eptx =
  154. {
  155. .bLength = sizeof(struct usb_endpoint_descriptor),
  156. .bDescriptorType = USB_DTYPE_ENDPOINT,
  157. .bEndpointAddress = CDC0_TXD_EP,
  158. .bmAttributes = USB_EPTYPE_BULK,
  159. .wMaxPacketSize = CDC_DATA_SZ,
  160. .bInterval = 0x01,
  161. },
  162. },
  163. };
  164. /* Device configuration descriptor - dual mode*/
  165. static const struct CdcConfigDescriptorDual
  166. cdc_cfg_desc_dual =
  167. {
  168. .config =
  169. {
  170. .bLength = sizeof(struct usb_config_descriptor),
  171. .bDescriptorType = USB_DTYPE_CONFIGURATION,
  172. .wTotalLength = sizeof(struct CdcConfigDescriptorDual),
  173. .bNumInterfaces = 4,
  174. .bConfigurationValue = 1,
  175. .iConfiguration = NO_DESCRIPTOR,
  176. .bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
  177. .bMaxPower = USB_CFG_POWER_MA(100),
  178. },
  179. .iad_0 =
  180. {
  181. .comm_iad =
  182. {
  183. .bLength = sizeof(struct usb_iad_descriptor),
  184. .bDescriptorType = USB_DTYPE_INTERFASEASSOC,
  185. .bFirstInterface = 0,
  186. .bInterfaceCount = 2,
  187. .bFunctionClass = USB_CLASS_CDC,
  188. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  189. .bFunctionProtocol = USB_PROTO_NONE,
  190. .iFunction = NO_DESCRIPTOR,
  191. },
  192. .comm =
  193. {
  194. .bLength = sizeof(struct usb_interface_descriptor),
  195. .bDescriptorType = USB_DTYPE_INTERFACE,
  196. .bInterfaceNumber = 0,
  197. .bAlternateSetting = 0,
  198. .bNumEndpoints = 1,
  199. .bInterfaceClass = USB_CLASS_CDC,
  200. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  201. .bInterfaceProtocol = USB_PROTO_NONE,
  202. .iInterface = NO_DESCRIPTOR,
  203. },
  204. .cdc_hdr =
  205. {
  206. .bFunctionLength = sizeof(struct usb_cdc_header_desc),
  207. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  208. .bDescriptorSubType = USB_DTYPE_CDC_HEADER,
  209. .bcdCDC = VERSION_BCD(1, 1, 0),
  210. },
  211. .cdc_mgmt =
  212. {
  213. .bFunctionLength = sizeof(struct usb_cdc_call_mgmt_desc),
  214. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  215. .bDescriptorSubType = USB_DTYPE_CDC_CALL_MANAGEMENT,
  216. .bmCapabilities = 0,
  217. .bDataInterface = 1,
  218. },
  219. .cdc_acm =
  220. {
  221. .bFunctionLength = sizeof(struct usb_cdc_acm_desc),
  222. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  223. .bDescriptorSubType = USB_DTYPE_CDC_ACM,
  224. .bmCapabilities = 0,
  225. },
  226. .cdc_union =
  227. {
  228. .bFunctionLength = sizeof(struct usb_cdc_union_desc),
  229. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  230. .bDescriptorSubType = USB_DTYPE_CDC_UNION,
  231. .bMasterInterface0 = 0,
  232. .bSlaveInterface0 = 1,
  233. },
  234. .comm_ep =
  235. {
  236. .bLength = sizeof(struct usb_endpoint_descriptor),
  237. .bDescriptorType = USB_DTYPE_ENDPOINT,
  238. .bEndpointAddress = CDC0_NTF_EP,
  239. .bmAttributes = USB_EPTYPE_INTERRUPT,
  240. .wMaxPacketSize = CDC_NTF_SZ,
  241. .bInterval = 0xFF,
  242. },
  243. .data =
  244. {
  245. .bLength = sizeof(struct usb_interface_descriptor),
  246. .bDescriptorType = USB_DTYPE_INTERFACE,
  247. .bInterfaceNumber = 1,
  248. .bAlternateSetting = 0,
  249. .bNumEndpoints = 2,
  250. .bInterfaceClass = USB_CLASS_CDC_DATA,
  251. .bInterfaceSubClass = USB_SUBCLASS_NONE,
  252. .bInterfaceProtocol = USB_PROTO_NONE,
  253. .iInterface = NO_DESCRIPTOR,
  254. },
  255. .data_eprx =
  256. {
  257. .bLength = sizeof(struct usb_endpoint_descriptor),
  258. .bDescriptorType = USB_DTYPE_ENDPOINT,
  259. .bEndpointAddress = CDC0_RXD_EP,
  260. .bmAttributes = USB_EPTYPE_BULK,
  261. .wMaxPacketSize = CDC_DATA_SZ,
  262. .bInterval = 0x01,
  263. },
  264. .data_eptx =
  265. {
  266. .bLength = sizeof(struct usb_endpoint_descriptor),
  267. .bDescriptorType = USB_DTYPE_ENDPOINT,
  268. .bEndpointAddress = CDC0_TXD_EP,
  269. .bmAttributes = USB_EPTYPE_BULK,
  270. .wMaxPacketSize = CDC_DATA_SZ,
  271. .bInterval = 0x01,
  272. },
  273. },
  274. .iad_1 =
  275. {
  276. .comm_iad =
  277. {
  278. .bLength = sizeof(struct usb_iad_descriptor),
  279. .bDescriptorType = USB_DTYPE_INTERFASEASSOC,
  280. .bFirstInterface = 2,
  281. .bInterfaceCount = 2,
  282. .bFunctionClass = USB_CLASS_CDC,
  283. .bFunctionSubClass = USB_CDC_SUBCLASS_ACM,
  284. .bFunctionProtocol = USB_PROTO_NONE,
  285. .iFunction = NO_DESCRIPTOR,
  286. },
  287. .comm =
  288. {
  289. .bLength = sizeof(struct usb_interface_descriptor),
  290. .bDescriptorType = USB_DTYPE_INTERFACE,
  291. .bInterfaceNumber = 2 + 0,
  292. .bAlternateSetting = 0,
  293. .bNumEndpoints = 1,
  294. .bInterfaceClass = USB_CLASS_CDC,
  295. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  296. .bInterfaceProtocol = USB_PROTO_NONE,
  297. .iInterface = NO_DESCRIPTOR,
  298. },
  299. .cdc_hdr =
  300. {
  301. .bFunctionLength = sizeof(struct usb_cdc_header_desc),
  302. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  303. .bDescriptorSubType = USB_DTYPE_CDC_HEADER,
  304. .bcdCDC = VERSION_BCD(1, 1, 0),
  305. },
  306. .cdc_mgmt =
  307. {
  308. .bFunctionLength = sizeof(struct usb_cdc_call_mgmt_desc),
  309. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  310. .bDescriptorSubType = USB_DTYPE_CDC_CALL_MANAGEMENT,
  311. .bmCapabilities = 0,
  312. .bDataInterface = 2 + 1,
  313. },
  314. .cdc_acm =
  315. {
  316. .bFunctionLength = sizeof(struct usb_cdc_acm_desc),
  317. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  318. .bDescriptorSubType = USB_DTYPE_CDC_ACM,
  319. .bmCapabilities = 0,
  320. },
  321. .cdc_union =
  322. {
  323. .bFunctionLength = sizeof(struct usb_cdc_union_desc),
  324. .bDescriptorType = USB_DTYPE_CS_INTERFACE,
  325. .bDescriptorSubType = USB_DTYPE_CDC_UNION,
  326. .bMasterInterface0 = 2 + 0,
  327. .bSlaveInterface0 = 2 + 1,
  328. },
  329. .comm_ep =
  330. {
  331. .bLength = sizeof(struct usb_endpoint_descriptor),
  332. .bDescriptorType = USB_DTYPE_ENDPOINT,
  333. .bEndpointAddress = CDC1_NTF_EP,
  334. .bmAttributes = USB_EPTYPE_INTERRUPT,
  335. .wMaxPacketSize = CDC_NTF_SZ,
  336. .bInterval = 0xFF,
  337. },
  338. .data =
  339. {
  340. .bLength = sizeof(struct usb_interface_descriptor),
  341. .bDescriptorType = USB_DTYPE_INTERFACE,
  342. .bInterfaceNumber = 2 + 1,
  343. .bAlternateSetting = 0,
  344. .bNumEndpoints = 2,
  345. .bInterfaceClass = USB_CLASS_CDC_DATA,
  346. .bInterfaceSubClass = USB_SUBCLASS_NONE,
  347. .bInterfaceProtocol = USB_PROTO_NONE,
  348. .iInterface = NO_DESCRIPTOR,
  349. },
  350. .data_eprx =
  351. {
  352. .bLength = sizeof(struct usb_endpoint_descriptor),
  353. .bDescriptorType = USB_DTYPE_ENDPOINT,
  354. .bEndpointAddress = CDC1_RXD_EP,
  355. .bmAttributes = USB_EPTYPE_BULK,
  356. .wMaxPacketSize = CDC_DATA_SZ,
  357. .bInterval = 0x01,
  358. },
  359. .data_eptx =
  360. {
  361. .bLength = sizeof(struct usb_endpoint_descriptor),
  362. .bDescriptorType = USB_DTYPE_ENDPOINT,
  363. .bEndpointAddress = CDC1_TXD_EP,
  364. .bmAttributes = USB_EPTYPE_BULK,
  365. .wMaxPacketSize = CDC_DATA_SZ,
  366. .bInterval = 0x01,
  367. },
  368. },
  369. };
  370. static struct usb_cdc_line_coding cdc_config[IF_NUM_MAX] = {};
  371. static uint8_t cdc_ctrl_line_state[IF_NUM_MAX];
  372. static void cdc_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
  373. static void cdc_deinit(usbd_device* dev);
  374. static void cdc_on_wakeup(usbd_device* dev);
  375. static void cdc_on_suspend(usbd_device* dev);
  376. static usbd_respond cdc_ep_config(usbd_device* dev, uint8_t cfg);
  377. static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
  378. static usbd_device* usb_dev;
  379. static FuriHalUsbInterface* cdc_if_cur = NULL;
  380. static bool connected = false;
  381. static CdcCallbacks* callbacks[IF_NUM_MAX] = {NULL};
  382. static void* cb_ctx[IF_NUM_MAX];
  383. FuriHalUsbInterface usb_cdc_single = {
  384. .init = cdc_init,
  385. .deinit = cdc_deinit,
  386. .wakeup = cdc_on_wakeup,
  387. .suspend = cdc_on_suspend,
  388. .dev_descr = (struct usb_device_descriptor*)&cdc_device_desc,
  389. .str_manuf_descr = (void*)&dev_manuf_desc,
  390. .str_prod_descr = NULL,
  391. .str_serial_descr = NULL,
  392. .cfg_descr = (void*)&cdc_cfg_desc_single,
  393. };
  394. FuriHalUsbInterface usb_cdc_dual = {
  395. .init = cdc_init,
  396. .deinit = cdc_deinit,
  397. .wakeup = cdc_on_wakeup,
  398. .suspend = cdc_on_suspend,
  399. .dev_descr = (struct usb_device_descriptor*)&cdc_device_desc,
  400. .str_manuf_descr = (void*)&dev_manuf_desc,
  401. .str_prod_descr = NULL,
  402. .str_serial_descr = NULL,
  403. .cfg_descr = (void*)&cdc_cfg_desc_dual,
  404. };
  405. static void cdc_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
  406. UNUSED(ctx);
  407. usb_dev = dev;
  408. cdc_if_cur = intf;
  409. char* name = (char*)furi_hal_version_get_device_name_ptr();
  410. uint8_t len = (name == NULL) ? (0) : (strlen(name));
  411. struct usb_string_descriptor* dev_prod_desc = malloc(len * 2 + 2);
  412. dev_prod_desc->bLength = len * 2 + 2;
  413. dev_prod_desc->bDescriptorType = USB_DTYPE_STRING;
  414. for(uint8_t i = 0; i < len; i++) dev_prod_desc->wString[i] = name[i];
  415. name = (char*)furi_hal_version_get_name_ptr();
  416. len = (name == NULL) ? (0) : (strlen(name));
  417. struct usb_string_descriptor* dev_serial_desc = malloc((len + 5) * 2 + 2);
  418. dev_serial_desc->bLength = (len + 5) * 2 + 2;
  419. dev_serial_desc->bDescriptorType = USB_DTYPE_STRING;
  420. memcpy(dev_serial_desc->wString, "f\0l\0i\0p\0_\0", 5 * 2);
  421. for(uint8_t i = 0; i < len; i++) dev_serial_desc->wString[i + 5] = name[i];
  422. cdc_if_cur->str_prod_descr = dev_prod_desc;
  423. cdc_if_cur->str_serial_descr = dev_serial_desc;
  424. usbd_reg_config(dev, cdc_ep_config);
  425. usbd_reg_control(dev, cdc_control);
  426. usbd_connect(dev, true);
  427. }
  428. static void cdc_deinit(usbd_device* dev) {
  429. usbd_reg_config(dev, NULL);
  430. usbd_reg_control(dev, NULL);
  431. free(cdc_if_cur->str_prod_descr);
  432. free(cdc_if_cur->str_serial_descr);
  433. cdc_if_cur = NULL;
  434. }
  435. void furi_hal_cdc_set_callbacks(uint8_t if_num, CdcCallbacks* cb, void* context) {
  436. furi_assert(if_num < IF_NUM_MAX);
  437. if(callbacks[if_num] != NULL) {
  438. if(callbacks[if_num]->state_callback != NULL) {
  439. if(connected == true) callbacks[if_num]->state_callback(cb_ctx[if_num], 0);
  440. }
  441. }
  442. callbacks[if_num] = cb;
  443. cb_ctx[if_num] = context;
  444. if(callbacks[if_num] != NULL) {
  445. if(callbacks[if_num]->state_callback != NULL) {
  446. if(connected == true) callbacks[if_num]->state_callback(cb_ctx[if_num], 1);
  447. }
  448. if(callbacks[if_num]->ctrl_line_callback != NULL) {
  449. callbacks[if_num]->ctrl_line_callback(cb_ctx[if_num], cdc_ctrl_line_state[if_num]);
  450. }
  451. }
  452. }
  453. struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num) {
  454. furi_assert(if_num < IF_NUM_MAX);
  455. return &cdc_config[if_num];
  456. }
  457. uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num) {
  458. furi_assert(if_num < IF_NUM_MAX);
  459. return cdc_ctrl_line_state[if_num];
  460. }
  461. void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len) {
  462. if(if_num == 0)
  463. usbd_ep_write(usb_dev, CDC0_TXD_EP, buf, len);
  464. else
  465. usbd_ep_write(usb_dev, CDC1_TXD_EP, buf, len);
  466. }
  467. int32_t furi_hal_cdc_receive(uint8_t if_num, uint8_t* buf, uint16_t max_len) {
  468. int32_t len = 0;
  469. if(if_num == 0)
  470. len = usbd_ep_read(usb_dev, CDC0_RXD_EP, buf, max_len);
  471. else
  472. len = usbd_ep_read(usb_dev, CDC1_RXD_EP, buf, max_len);
  473. return ((len < 0) ? 0 : len);
  474. }
  475. static void cdc_on_wakeup(usbd_device* dev) {
  476. UNUSED(dev);
  477. connected = true;
  478. for(uint8_t i = 0; i < IF_NUM_MAX; i++) {
  479. if(callbacks[i] != NULL) {
  480. if(callbacks[i]->state_callback != NULL) callbacks[i]->state_callback(cb_ctx[i], 1);
  481. }
  482. }
  483. }
  484. static void cdc_on_suspend(usbd_device* dev) {
  485. UNUSED(dev);
  486. connected = false;
  487. for(uint8_t i = 0; i < IF_NUM_MAX; i++) {
  488. cdc_ctrl_line_state[i] = 0;
  489. if(callbacks[i] != NULL) {
  490. if(callbacks[i]->state_callback != NULL) callbacks[i]->state_callback(cb_ctx[i], 0);
  491. }
  492. }
  493. }
  494. static void cdc_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  495. UNUSED(dev);
  496. UNUSED(event);
  497. uint8_t if_num = 0;
  498. if(ep == CDC0_RXD_EP)
  499. if_num = 0;
  500. else
  501. if_num = 1;
  502. if(callbacks[if_num] != NULL) {
  503. if(callbacks[if_num]->rx_ep_callback != NULL)
  504. callbacks[if_num]->rx_ep_callback(cb_ctx[if_num]);
  505. }
  506. }
  507. static void cdc_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  508. UNUSED(dev);
  509. UNUSED(event);
  510. uint8_t if_num = 0;
  511. if(ep == CDC0_TXD_EP)
  512. if_num = 0;
  513. else
  514. if_num = 1;
  515. if(callbacks[if_num] != NULL) {
  516. if(callbacks[if_num]->tx_ep_callback != NULL)
  517. callbacks[if_num]->tx_ep_callback(cb_ctx[if_num]);
  518. }
  519. }
  520. static void cdc_txrx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  521. if(event == usbd_evt_eptx) {
  522. cdc_tx_ep_callback(dev, event, ep);
  523. } else {
  524. cdc_rx_ep_callback(dev, event, ep);
  525. }
  526. }
  527. /* Configure endpoints */
  528. static usbd_respond cdc_ep_config(usbd_device* dev, uint8_t cfg) {
  529. uint8_t if_cnt = ((struct usb_config_descriptor*)(cdc_if_cur->cfg_descr))->bNumInterfaces;
  530. switch(cfg) {
  531. case 0:
  532. /* deconfiguring device */
  533. if(if_cnt == 4) {
  534. usbd_ep_deconfig(dev, CDC1_NTF_EP);
  535. usbd_ep_deconfig(dev, CDC1_TXD_EP);
  536. usbd_ep_deconfig(dev, CDC1_RXD_EP);
  537. usbd_reg_endpoint(dev, CDC1_RXD_EP, 0);
  538. usbd_reg_endpoint(dev, CDC1_TXD_EP, 0);
  539. }
  540. usbd_ep_deconfig(dev, CDC0_NTF_EP);
  541. usbd_ep_deconfig(dev, CDC0_TXD_EP);
  542. usbd_ep_deconfig(dev, CDC0_RXD_EP);
  543. usbd_reg_endpoint(dev, CDC0_RXD_EP, 0);
  544. usbd_reg_endpoint(dev, CDC0_TXD_EP, 0);
  545. return usbd_ack;
  546. case 1:
  547. /* configuring device */
  548. if((CDC0_TXD_EP & 0x7F) != (CDC0_RXD_EP & 0x7F)) {
  549. // 2x unidirectional endpoint mode with dualbuf
  550. usbd_ep_config(dev, CDC0_RXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  551. usbd_ep_config(dev, CDC0_TXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  552. usbd_ep_config(dev, CDC0_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  553. usbd_reg_endpoint(dev, CDC0_RXD_EP, cdc_rx_ep_callback);
  554. usbd_reg_endpoint(dev, CDC0_TXD_EP, cdc_tx_ep_callback);
  555. } else {
  556. // 1x bidirectional endpoint mode
  557. usbd_ep_config(dev, CDC0_RXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  558. usbd_ep_config(dev, CDC0_TXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  559. usbd_ep_config(dev, CDC0_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  560. usbd_reg_endpoint(dev, CDC0_RXD_EP, cdc_txrx_ep_callback);
  561. usbd_reg_endpoint(dev, CDC0_TXD_EP, cdc_txrx_ep_callback);
  562. }
  563. usbd_ep_write(dev, CDC0_TXD_EP, 0, 0);
  564. if(if_cnt == 4) {
  565. if((CDC1_TXD_EP & 0x7F) != (CDC1_RXD_EP & 0x7F)) {
  566. usbd_ep_config(dev, CDC1_RXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  567. usbd_ep_config(dev, CDC1_TXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  568. usbd_ep_config(dev, CDC1_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  569. usbd_reg_endpoint(dev, CDC1_RXD_EP, cdc_rx_ep_callback);
  570. usbd_reg_endpoint(dev, CDC1_TXD_EP, cdc_tx_ep_callback);
  571. } else {
  572. usbd_ep_config(dev, CDC1_RXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  573. usbd_ep_config(dev, CDC1_TXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  574. usbd_ep_config(dev, CDC1_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  575. usbd_reg_endpoint(dev, CDC1_RXD_EP, cdc_txrx_ep_callback);
  576. usbd_reg_endpoint(dev, CDC1_TXD_EP, cdc_txrx_ep_callback);
  577. }
  578. usbd_ep_write(dev, CDC1_TXD_EP, 0, 0);
  579. }
  580. return usbd_ack;
  581. default:
  582. return usbd_fail;
  583. }
  584. }
  585. /* Control requests handler */
  586. static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  587. UNUSED(callback);
  588. /* CDC control requests */
  589. uint8_t if_num = 0;
  590. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  591. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  592. (req->wIndex == 0 || req->wIndex == 2)) {
  593. if(req->wIndex == 0)
  594. if_num = 0;
  595. else
  596. if_num = 1;
  597. switch(req->bRequest) {
  598. case USB_CDC_SET_CONTROL_LINE_STATE:
  599. if(callbacks[if_num] != NULL) {
  600. cdc_ctrl_line_state[if_num] = req->wValue;
  601. if(callbacks[if_num]->ctrl_line_callback != NULL)
  602. callbacks[if_num]->ctrl_line_callback(
  603. cb_ctx[if_num], cdc_ctrl_line_state[if_num]);
  604. }
  605. return usbd_ack;
  606. case USB_CDC_SET_LINE_CODING:
  607. memcpy(&cdc_config[if_num], req->data, sizeof(cdc_config[0]));
  608. if(callbacks[if_num] != NULL) {
  609. if(callbacks[if_num]->config_callback != NULL)
  610. callbacks[if_num]->config_callback(cb_ctx[if_num], &cdc_config[if_num]);
  611. }
  612. return usbd_ack;
  613. case USB_CDC_GET_LINE_CODING:
  614. dev->status.data_ptr = &cdc_config[if_num];
  615. dev->status.data_count = sizeof(cdc_config[0]);
  616. return usbd_ack;
  617. default:
  618. return usbd_fail;
  619. }
  620. }
  621. return usbd_fail;
  622. }