furi_hal_usb_cdc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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_i.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. usb_dev = dev;
  407. cdc_if_cur = intf;
  408. char* name = (char*)furi_hal_version_get_device_name_ptr();
  409. uint8_t len = (name == NULL) ? (0) : (strlen(name));
  410. struct usb_string_descriptor* dev_prod_desc = malloc(len * 2 + 2);
  411. dev_prod_desc->bLength = len * 2 + 2;
  412. dev_prod_desc->bDescriptorType = USB_DTYPE_STRING;
  413. for(uint8_t i = 0; i < len; i++) dev_prod_desc->wString[i] = name[i];
  414. name = (char*)furi_hal_version_get_name_ptr();
  415. len = (name == NULL) ? (0) : (strlen(name));
  416. struct usb_string_descriptor* dev_serial_desc = malloc((len + 5) * 2 + 2);
  417. dev_serial_desc->bLength = (len + 5) * 2 + 2;
  418. dev_serial_desc->bDescriptorType = USB_DTYPE_STRING;
  419. memcpy(dev_serial_desc->wString, "f\0l\0i\0p\0_\0", 5 * 2);
  420. for(uint8_t i = 0; i < len; i++) dev_serial_desc->wString[i + 5] = name[i];
  421. cdc_if_cur->str_prod_descr = dev_prod_desc;
  422. cdc_if_cur->str_serial_descr = dev_serial_desc;
  423. usbd_reg_config(dev, cdc_ep_config);
  424. usbd_reg_control(dev, cdc_control);
  425. usbd_connect(dev, true);
  426. }
  427. static void cdc_deinit(usbd_device* dev) {
  428. usbd_reg_config(dev, NULL);
  429. usbd_reg_control(dev, NULL);
  430. free(cdc_if_cur->str_prod_descr);
  431. free(cdc_if_cur->str_serial_descr);
  432. cdc_if_cur = NULL;
  433. }
  434. void furi_hal_cdc_set_callbacks(uint8_t if_num, CdcCallbacks* cb, void* context) {
  435. furi_assert(if_num < IF_NUM_MAX);
  436. if(callbacks[if_num] != NULL) {
  437. if(callbacks[if_num]->state_callback != NULL) {
  438. if(connected == true) callbacks[if_num]->state_callback(cb_ctx[if_num], 0);
  439. }
  440. }
  441. callbacks[if_num] = cb;
  442. cb_ctx[if_num] = context;
  443. if(callbacks[if_num] != NULL) {
  444. if(callbacks[if_num]->state_callback != NULL) {
  445. if(connected == true) callbacks[if_num]->state_callback(cb_ctx[if_num], 1);
  446. }
  447. }
  448. }
  449. struct usb_cdc_line_coding* furi_hal_cdc_get_port_settings(uint8_t if_num) {
  450. furi_assert(if_num < IF_NUM_MAX);
  451. return &cdc_config[if_num];
  452. }
  453. uint8_t furi_hal_cdc_get_ctrl_line_state(uint8_t if_num) {
  454. furi_assert(if_num < IF_NUM_MAX);
  455. return cdc_ctrl_line_state[if_num];
  456. }
  457. void furi_hal_cdc_send(uint8_t if_num, uint8_t* buf, uint16_t len) {
  458. if(if_num == 0)
  459. usbd_ep_write(usb_dev, CDC0_TXD_EP, buf, len);
  460. else
  461. usbd_ep_write(usb_dev, CDC1_TXD_EP, buf, len);
  462. }
  463. int32_t furi_hal_cdc_receive(uint8_t if_num, uint8_t* buf, uint16_t max_len) {
  464. int32_t len = 0;
  465. if(if_num == 0)
  466. len = usbd_ep_read(usb_dev, CDC0_RXD_EP, buf, max_len);
  467. else
  468. len = usbd_ep_read(usb_dev, CDC1_RXD_EP, buf, max_len);
  469. return ((len < 0) ? 0 : len);
  470. }
  471. static void cdc_on_wakeup(usbd_device* dev) {
  472. connected = true;
  473. for(uint8_t i = 0; i < IF_NUM_MAX; i++) {
  474. if(callbacks[i] != NULL) {
  475. if(callbacks[i]->state_callback != NULL) callbacks[i]->state_callback(cb_ctx[i], 1);
  476. }
  477. }
  478. }
  479. static void cdc_on_suspend(usbd_device* dev) {
  480. connected = false;
  481. for(uint8_t i = 0; i < IF_NUM_MAX; i++) {
  482. cdc_ctrl_line_state[i] = 0;
  483. if(callbacks[i] != NULL) {
  484. if(callbacks[i]->state_callback != NULL) callbacks[i]->state_callback(cb_ctx[i], 0);
  485. }
  486. }
  487. }
  488. static void cdc_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  489. uint8_t if_num = 0;
  490. if(ep == CDC0_RXD_EP)
  491. if_num = 0;
  492. else
  493. if_num = 1;
  494. if(callbacks[if_num] != NULL) {
  495. if(callbacks[if_num]->rx_ep_callback != NULL)
  496. callbacks[if_num]->rx_ep_callback(cb_ctx[if_num]);
  497. }
  498. }
  499. static void cdc_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  500. uint8_t if_num = 0;
  501. if(ep == CDC0_TXD_EP)
  502. if_num = 0;
  503. else
  504. if_num = 1;
  505. if(callbacks[if_num] != NULL) {
  506. if(callbacks[if_num]->tx_ep_callback != NULL)
  507. callbacks[if_num]->tx_ep_callback(cb_ctx[if_num]);
  508. }
  509. }
  510. static void cdc_txrx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
  511. if(event == usbd_evt_eptx) {
  512. cdc_tx_ep_callback(dev, event, ep);
  513. } else {
  514. cdc_rx_ep_callback(dev, event, ep);
  515. }
  516. }
  517. /* Configure endpoints */
  518. static usbd_respond cdc_ep_config(usbd_device* dev, uint8_t cfg) {
  519. uint8_t if_cnt = ((struct usb_config_descriptor*)(cdc_if_cur->cfg_descr))->bNumInterfaces;
  520. switch(cfg) {
  521. case 0:
  522. /* deconfiguring device */
  523. if(if_cnt == 4) {
  524. usbd_ep_deconfig(dev, CDC1_NTF_EP);
  525. usbd_ep_deconfig(dev, CDC1_TXD_EP);
  526. usbd_ep_deconfig(dev, CDC1_RXD_EP);
  527. usbd_reg_endpoint(dev, CDC1_RXD_EP, 0);
  528. usbd_reg_endpoint(dev, CDC1_TXD_EP, 0);
  529. }
  530. usbd_ep_deconfig(dev, CDC0_NTF_EP);
  531. usbd_ep_deconfig(dev, CDC0_TXD_EP);
  532. usbd_ep_deconfig(dev, CDC0_RXD_EP);
  533. usbd_reg_endpoint(dev, CDC0_RXD_EP, 0);
  534. usbd_reg_endpoint(dev, CDC0_TXD_EP, 0);
  535. return usbd_ack;
  536. case 1:
  537. /* configuring device */
  538. if((CDC0_TXD_EP & 0x7F) != (CDC0_RXD_EP & 0x7F)) {
  539. // 2x unidirectional endpoint mode with dualbuf
  540. usbd_ep_config(dev, CDC0_RXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  541. usbd_ep_config(dev, CDC0_TXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  542. usbd_ep_config(dev, CDC0_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  543. usbd_reg_endpoint(dev, CDC0_RXD_EP, cdc_rx_ep_callback);
  544. usbd_reg_endpoint(dev, CDC0_TXD_EP, cdc_tx_ep_callback);
  545. } else {
  546. // 1x bidirectional endpoint mode
  547. usbd_ep_config(dev, CDC0_RXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  548. usbd_ep_config(dev, CDC0_TXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  549. usbd_ep_config(dev, CDC0_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  550. usbd_reg_endpoint(dev, CDC0_RXD_EP, cdc_txrx_ep_callback);
  551. usbd_reg_endpoint(dev, CDC0_TXD_EP, cdc_txrx_ep_callback);
  552. }
  553. usbd_ep_write(dev, CDC0_TXD_EP, 0, 0);
  554. if(if_cnt == 4) {
  555. if((CDC1_TXD_EP & 0x7F) != (CDC1_RXD_EP & 0x7F)) {
  556. usbd_ep_config(dev, CDC1_RXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  557. usbd_ep_config(dev, CDC1_TXD_EP, USB_EPTYPE_BULK | USB_EPTYPE_DBLBUF, CDC_DATA_SZ);
  558. usbd_ep_config(dev, CDC1_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  559. usbd_reg_endpoint(dev, CDC1_RXD_EP, cdc_rx_ep_callback);
  560. usbd_reg_endpoint(dev, CDC1_TXD_EP, cdc_tx_ep_callback);
  561. } else {
  562. usbd_ep_config(dev, CDC1_RXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  563. usbd_ep_config(dev, CDC1_TXD_EP, USB_EPTYPE_BULK, CDC_DATA_SZ);
  564. usbd_ep_config(dev, CDC1_NTF_EP, USB_EPTYPE_INTERRUPT, CDC_NTF_SZ);
  565. usbd_reg_endpoint(dev, CDC1_RXD_EP, cdc_txrx_ep_callback);
  566. usbd_reg_endpoint(dev, CDC1_TXD_EP, cdc_txrx_ep_callback);
  567. }
  568. usbd_ep_write(dev, CDC1_TXD_EP, 0, 0);
  569. }
  570. return usbd_ack;
  571. default:
  572. return usbd_fail;
  573. }
  574. }
  575. /* Control requests handler */
  576. static usbd_respond cdc_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
  577. /* CDC control requests */
  578. uint8_t if_num = 0;
  579. if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
  580. (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
  581. (req->wIndex == 0 || req->wIndex == 2)) {
  582. if(req->wIndex == 0)
  583. if_num = 0;
  584. else
  585. if_num = 1;
  586. switch(req->bRequest) {
  587. case USB_CDC_SET_CONTROL_LINE_STATE:
  588. if(callbacks[if_num] != NULL) {
  589. cdc_ctrl_line_state[if_num] = req->wValue;
  590. if(callbacks[if_num]->ctrl_line_callback != NULL)
  591. callbacks[if_num]->ctrl_line_callback(
  592. cb_ctx[if_num], cdc_ctrl_line_state[if_num]);
  593. }
  594. return usbd_ack;
  595. case USB_CDC_SET_LINE_CODING:
  596. memcpy(&cdc_config[if_num], req->data, sizeof(cdc_config[0]));
  597. if(callbacks[if_num] != NULL) {
  598. if(callbacks[if_num]->config_callback != NULL)
  599. callbacks[if_num]->config_callback(cb_ctx[if_num], &cdc_config[if_num]);
  600. }
  601. return usbd_ack;
  602. case USB_CDC_GET_LINE_CODING:
  603. dev->status.data_ptr = &cdc_config[if_num];
  604. dev->status.data_count = sizeof(cdc_config[0]);
  605. return usbd_ack;
  606. default:
  607. return usbd_fail;
  608. }
  609. }
  610. return usbd_fail;
  611. }