furi-hal-usb-cdc.c 24 KB

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