cdc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. #include "usb/device/usb_stack.h"
  2. #include "usb/usb_config.h"
  3. #include "usb/device/cdc.h"
  4. #include "app_config.h"
  5. #include "os/os_api.h"
  6. #include "cdc_defs.h" //need redefine __u8, __u16, __u32
  7. #define LOG_TAG_CONST USB
  8. #define LOG_TAG "[USB]"
  9. #define LOG_ERROR_ENABLE
  10. #define LOG_DEBUG_ENABLE
  11. #define LOG_INFO_ENABLE
  12. /* #define LOG_DUMP_ENABLE */
  13. #define LOG_CLI_ENABLE
  14. #include "debug.h"
  15. #if TCFG_USB_SLAVE_CDC_ENABLE
  16. struct usb_cdc_gadget {
  17. u8 *cdc_buffer;
  18. u8 *bulk_ep_out_buffer;
  19. u8 *bulk_ep_in_buffer;
  20. void *priv;
  21. int (*output)(void *priv, u8 *obuf, u32 olen);
  22. void (*wakeup_handler)(struct usb_device_t *usb_device);
  23. OS_MUTEX mutex_data;
  24. #if CDC_INTR_EP_ENABLE
  25. OS_MUTEX mutex_intr;
  26. u8 *intr_ep_in_buffer;
  27. #endif
  28. u8 bmTransceiver;
  29. u8 subtype_data[8];
  30. };
  31. static struct usb_cdc_gadget *cdc_hdl;
  32. #if USB_MALLOC_ENABLE
  33. #else
  34. static u8 _cdc_buffer[MAXP_SIZE_CDC_BULKOUT] SEC(.cdc_var) __attribute__((aligned(4)));
  35. static struct usb_cdc_gadget _cdc_hdl SEC(.cdc_var);
  36. #endif
  37. static const u8 cdc_virtual_comport_desc[] = {
  38. //IAD Descriptor
  39. 0x08, //bLength
  40. 0x0b, //bDescriptorType
  41. 0x00, //bFirstInterface
  42. 0x02, //bInterfaceCount
  43. 0x02, //bFunctionClass, Comunication and CDC control
  44. 0x02, //bFunctionSubClass
  45. 0x01, //bFunctionProtocol
  46. 0x00, //iFunction
  47. //Interface 0, Alt 0
  48. 0x09, //Length
  49. 0x04, //DescriptorType:Interface
  50. 0x00, //InterfaceNum
  51. 0x00, //AlternateSetting
  52. 0x01, //NumEndpoint
  53. 0x02, //InterfaceClass, Communation and CDC control
  54. 0x02, //InterfaceSubClass, Abstract Control Model
  55. 0x01, //InterfaceProtocol, AT commands defined by ITU-T V.250 etc
  56. 0x00, //Interface String
  57. //CDC Interface Descriptor
  58. 0x05, //bLength
  59. 0x24, //bDescriptorType
  60. 0x00, //bDescriptorSubType, Header Functional Desc
  61. 0x10, 0x01, //bcdCDC, version 1.10
  62. //CDC Interface Descriptor
  63. 0x05, //bLength
  64. 0x24, //bDescriptorType
  65. 0x01, //bDescriptorSubType, Call Management Functional Descriptor
  66. 0x03, //bmCapabilities, D7..D2 reversed
  67. // D7..D2 reversed
  68. // D1 sends/receives call management information only over a Data Class interface
  69. // D0 handle call management itself
  70. 0x01, //bDataInterface
  71. //CDC Interface Descriptor
  72. 0x04, //bLength
  73. 0x24, //bDescriptorType
  74. 0x02, //bDescriptorSubType, Abstract Control Management Functional Descriptor
  75. 0x02, //bmCapabilities, D7..D2 reversed
  76. // D7..D4 reversed
  77. // D3 supports the notification Network_Connection
  78. // D2 not supports the request Send_Break
  79. // D1 supports the request combination of Set_Line_Coding, Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State
  80. // D0 supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and Get_Comm_Feature
  81. //CDC Interface Descriptor
  82. 0x05, //bLength
  83. 0x24, //bDescriptorType
  84. 0x06, //bDescriptorSubType, Union Functional Descriptor
  85. 0x00, //bControlInterface
  86. 0x01, //bSubordinateInterface[0]
  87. //Endpoint In
  88. 0x07, //bLength
  89. 0x05, //bDescritorType
  90. 0x82, //bEndpointAddr
  91. 0x03, //bmAttributes, interrupt
  92. 0x08, 0x00, //wMaxPacketSize
  93. 0x01, //bInterval, 1ms
  94. //Interface 1, Alt 0
  95. 0x09, //Length
  96. 0x04, //DescriptorType:Interface
  97. 0x01, //InterfaceNum
  98. 0x00, //AlternateSetting
  99. 0x02, //NumEndpoint
  100. 0x0a, //InterfaceClass, CDC Data
  101. 0x00, //InterfaceSubClass
  102. 0x00, //InterfaceProtocol
  103. 0x00, //Interface String
  104. //Endpoint Out
  105. 0x07, //bLength
  106. 0x05, //bDescriptor
  107. 0x02, //bEndpointAddr
  108. 0x02, //bmAttributes, bulk
  109. 0x40, 0x00, //wMaxPacketSize
  110. 0x00, //bInterval
  111. //Endpoint In
  112. 0x07, //bLength
  113. 0x05, //bDescritorType
  114. 0x83, //bEndpointAddr
  115. 0x02, //bmAttributes, bulk
  116. 0x40, 0x00, //wMaxPacketSize
  117. 0x00, //bInterval
  118. };
  119. static void cdc_endpoint_init(struct usb_device_t *usb_device, u32 itf);
  120. static u32 cdc_setup_rx(struct usb_device_t *usb_device, struct usb_ctrlrequest *ctrl_req);
  121. static void usb_cdc_line_coding_init(struct usb_cdc_line_coding *lc)
  122. {
  123. lc->dwDTERate = 460800;
  124. lc->bCharFormat = USB_CDC_1_STOP_BITS;
  125. lc->bParityType = USB_CDC_NO_PARITY;
  126. lc->bDataBits = 8;
  127. }
  128. static void dump_line_coding(struct usb_cdc_line_coding *lc)
  129. {
  130. log_debug("dtw rate : %d", lc->dwDTERate);
  131. log_debug("stop bits : %d", lc->bCharFormat);
  132. log_debug("verify bits : %d", lc->bParityType);
  133. log_debug("data bits : %d", lc->bDataBits);
  134. }
  135. static u32 cdc_setup(struct usb_device_t *usb_device, struct usb_ctrlrequest *ctrl_req)
  136. {
  137. const usb_dev usb_id = usb_device2id(usb_device);
  138. int recip_type;
  139. u32 len;
  140. recip_type = ctrl_req->bRequestType & USB_TYPE_MASK;
  141. switch (recip_type) {
  142. case USB_TYPE_CLASS:
  143. switch (ctrl_req->bRequest) {
  144. case USB_CDC_REQ_SET_LINE_CODING:
  145. log_debug("set line coding");
  146. usb_set_setup_recv(usb_device, cdc_setup_rx);
  147. break;
  148. case USB_CDC_REQ_GET_LINE_CODING:
  149. log_debug("get line codling");
  150. len = ctrl_req->wLength < sizeof(struct usb_cdc_line_coding) ?
  151. ctrl_req->wLength : sizeof(struct usb_cdc_line_coding);
  152. if (cdc_hdl == NULL) {
  153. usb_set_setup_phase(usb_device, USB_EP0_SET_STALL);
  154. break;
  155. }
  156. usb_set_data_payload(usb_device, ctrl_req, cdc_hdl->subtype_data, len);
  157. dump_line_coding((struct usb_cdc_line_coding *)cdc_hdl->subtype_data);
  158. break;
  159. case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
  160. log_debug("set control line state - %d", ctrl_req->wValue);
  161. if (cdc_hdl) {
  162. /* if (ctrl_req->wValue & BIT(0)) { //DTR */
  163. cdc_hdl->bmTransceiver |= BIT(0);
  164. /* } else { */
  165. /* usb_slave->cdc->bmTransceiver &= ~BIT(0); */
  166. /* } */
  167. /* if (ctrl_req->wValue & BIT(1)) { //RTS */
  168. cdc_hdl->bmTransceiver |= BIT(1);
  169. /* } else { */
  170. /* usb_slave->cdc->bmTransceiver &= ~BIT(1); */
  171. /* } */
  172. cdc_hdl->bmTransceiver |= BIT(4); //cfg done
  173. }
  174. usb_set_setup_phase(usb_device, USB_EP0_STAGE_SETUP);
  175. cdc_endpoint_init(usb_device, (ctrl_req->wIndex & USB_RECIP_MASK));
  176. break;
  177. default:
  178. log_error("unsupported class req");
  179. usb_set_setup_phase(usb_device, USB_EP0_SET_STALL);
  180. break;
  181. }
  182. break;
  183. default:
  184. log_error("unsupported req type");
  185. usb_set_setup_phase(usb_device, USB_EP0_SET_STALL);
  186. break;
  187. }
  188. return 0;
  189. }
  190. static u32 cdc_setup_rx(struct usb_device_t *usb_device, struct usb_ctrlrequest *ctrl_req)
  191. {
  192. const usb_dev usb_id = usb_device2id(usb_device);
  193. int recip_type;
  194. struct usb_cdc_line_coding *lc = 0;
  195. u32 len;
  196. u8 read_ep[8];
  197. len = ctrl_req->wLength;
  198. usb_read_ep0(usb_id, read_ep, len);
  199. recip_type = ctrl_req->bRequestType & USB_TYPE_MASK;
  200. switch (recip_type) {
  201. case USB_TYPE_CLASS:
  202. switch (ctrl_req->bRequest) {
  203. case USB_CDC_REQ_SET_LINE_CODING:
  204. log_debug("USB_CDC_REQ_SET_LINE_CODING");
  205. if (cdc_hdl == NULL) {
  206. break;
  207. }
  208. if (len > sizeof(struct usb_cdc_line_coding)) {
  209. len = sizeof(struct usb_cdc_line_coding);
  210. }
  211. memcpy(cdc_hdl->subtype_data, read_ep, len);
  212. lc = (struct usb_cdc_line_coding *)cdc_hdl->subtype_data;
  213. dump_line_coding(lc);
  214. break;
  215. }
  216. break;
  217. }
  218. return USB_EP0_STAGE_SETUP;
  219. }
  220. static void cdc_reset(struct usb_device_t *usb_device, u32 itf)
  221. {
  222. log_error("%s()", __func__);
  223. const usb_dev usb_id = usb_device2id(usb_device);
  224. #if USB_ROOT2
  225. usb_disable_ep(usb_id, CDC_DATA_EP_IN);
  226. #if CDC_INTR_EP_ENABLE
  227. usb_disable_ep(usb_id, CDC_INTR_EP_IN);
  228. #endif
  229. #else
  230. cdc_endpoint_init(usb_device, itf);
  231. #endif
  232. }
  233. u32 cdc_desc_config(const usb_dev usb_id, u8 *ptr, u32 *itf)
  234. {
  235. u8 *tptr;
  236. tptr = ptr;
  237. memcpy(tptr, cdc_virtual_comport_desc, sizeof(cdc_virtual_comport_desc));
  238. //iad interface number
  239. tptr[2] = *itf;
  240. //control interface number
  241. tptr[8 + 2] = *itf;
  242. tptr[8 + 9 + 5 + 4] = *itf + 1;
  243. tptr[8 + 9 + 5 + 5 + 4 + 3] = *itf;
  244. tptr[8 + 9 + 5 + 5 + 4 + 4] = *itf + 1;
  245. //interrupt in ep
  246. tptr[8 + 9 + 5 + 5 + 4 + 5 + 2] = USB_DIR_IN | CDC_INTR_EP_IN;
  247. tptr[8 + 9 + 5 + 5 + 4 + 5 + 4] = MAXP_SIZE_CDC_INTRIN & 0xff;
  248. tptr[8 + 9 + 5 + 5 + 4 + 5 + 5] = (MAXP_SIZE_CDC_INTRIN >> 8) & 0xff;
  249. //data interface number
  250. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 2] = *itf + 1;
  251. //bulk out ep
  252. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 2] = CDC_DATA_EP_OUT;
  253. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 4] = MAXP_SIZE_CDC_BULKOUT & 0xff;
  254. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 5] = (MAXP_SIZE_CDC_BULKOUT >> 8) & 0xff;
  255. //bulk in ep
  256. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 7 + 2] = USB_DIR_IN | CDC_DATA_EP_IN;
  257. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 7 + 4] = MAXP_SIZE_CDC_BULKIN & 0xff;
  258. tptr[8 + 9 + 5 + 5 + 4 + 5 + 7 + 9 + 7 + 5] = (MAXP_SIZE_CDC_BULKIN >> 8) & 0xff;
  259. tptr += sizeof(cdc_virtual_comport_desc);
  260. if (usb_set_interface_hander(usb_id, *itf, cdc_setup) != *itf) {
  261. ASSERT(0, "cdc set interface_hander fail");
  262. }
  263. if (usb_set_reset_hander(usb_id, *itf, cdc_reset) != *itf) {
  264. }
  265. *itf += 2;
  266. return (u32)(tptr - ptr);
  267. }
  268. void cdc_set_wakeup_handler(void (*handle)(struct usb_device_t *usb_device))
  269. {
  270. if (cdc_hdl) {
  271. cdc_hdl->wakeup_handler = handle;
  272. }
  273. }
  274. static void cdc_wakeup_handler(struct usb_device_t *usb_device, u32 ep)
  275. {
  276. if (cdc_hdl && cdc_hdl->wakeup_handler) {
  277. cdc_hdl->wakeup_handler(usb_device);
  278. }
  279. }
  280. void cdc_set_output_handle(void *priv, int (*output_handler)(void *priv, u8 *buf, u32 len))
  281. {
  282. if (cdc_hdl) {
  283. cdc_hdl->output = output_handler;
  284. cdc_hdl->priv = priv;
  285. }
  286. }
  287. static void cdc_intrrx(struct usb_device_t *usb_device, u32 ep)
  288. {
  289. const usb_dev usb_id = usb_device2id(usb_device);
  290. if (cdc_hdl == NULL) {
  291. return;
  292. }
  293. u8 *cdc_rx_buf = cdc_hdl->cdc_buffer;
  294. //由于bulk传输使用双缓冲,无法用usb_get_ep_buffer()知道是哪一个buffer,需要外部buffer接收数据
  295. u32 len = usb_g_bulk_read(usb_id, CDC_DATA_EP_OUT, cdc_rx_buf, MAXP_SIZE_CDC_BULKOUT, 0);
  296. if (cdc_hdl->output) {
  297. cdc_hdl->output(cdc_hdl->priv, cdc_rx_buf, len);
  298. }
  299. }
  300. static void cdc_endpoint_init(struct usb_device_t *usb_device, u32 itf)
  301. {
  302. ASSERT(cdc_hdl, "cdc not register");
  303. const usb_dev usb_id = usb_device2id(usb_device);
  304. usb_g_ep_config(usb_id, CDC_DATA_EP_IN | USB_DIR_IN, USB_ENDPOINT_XFER_BULK,
  305. 0, cdc_hdl->bulk_ep_in_buffer, MAXP_SIZE_CDC_BULKIN);
  306. usb_g_ep_config(usb_id, CDC_DATA_EP_OUT | USB_DIR_OUT, USB_ENDPOINT_XFER_BULK,
  307. 1, cdc_hdl->bulk_ep_out_buffer, MAXP_SIZE_CDC_BULKOUT);
  308. /* usb_g_set_intr_hander(usb_id, CDC_DATA_EP_OUT | USB_DIR_OUT, cdc_intrrx); */
  309. usb_g_set_intr_hander(usb_id, CDC_DATA_EP_OUT | USB_DIR_OUT, cdc_wakeup_handler);
  310. usb_enable_ep(usb_id, CDC_DATA_EP_IN);
  311. #if CDC_INTR_EP_ENABLE
  312. usb_g_ep_config(usb_id, CDC_INTR_EP_IN | USB_DIR_IN, USB_ENDPOINT_XFER_INT,
  313. 0, cdc_hdl->intr_ep_in_buffer, MAXP_SIZE_CDC_INTRIN);
  314. usb_enable_ep(usb_id, CDC_INTR_EP_IN);
  315. #endif
  316. }
  317. u32 cdc_read_data(const usb_dev usb_id, u8 *buf, u32 len)
  318. {
  319. u32 rxlen;
  320. if (cdc_hdl == NULL) {
  321. return 0;
  322. }
  323. u8 *cdc_rx_buf = cdc_hdl->cdc_buffer;
  324. os_mutex_pend(&cdc_hdl->mutex_data, 0);
  325. //由于bulk传输使用双缓冲,无法用usb_get_ep_buffer()知道是哪一个buffer,需要外部buffer接收数据
  326. rxlen = usb_g_bulk_read(usb_id, CDC_DATA_EP_OUT, cdc_rx_buf, MAXP_SIZE_CDC_BULKOUT, 0);
  327. rxlen = rxlen > len ? len : rxlen;
  328. if (rxlen > 0) {
  329. memcpy(buf, cdc_rx_buf, rxlen);
  330. }
  331. os_mutex_post(&cdc_hdl->mutex_data);
  332. return rxlen;
  333. }
  334. u32 cdc_write_data(const usb_dev usb_id, u8 *buf, u32 len)
  335. {
  336. u32 txlen, offset;
  337. if (cdc_hdl == NULL) {
  338. return 0;
  339. }
  340. if ((cdc_hdl->bmTransceiver & (BIT(1) | BIT(4))) != (BIT(1) | BIT(4))) {
  341. return 0;
  342. }
  343. offset = 0;
  344. os_mutex_pend(&cdc_hdl->mutex_data, 0);
  345. while (offset < len) {
  346. txlen = len - offset > MAXP_SIZE_CDC_BULKIN ?
  347. MAXP_SIZE_CDC_BULKIN : len - offset;
  348. txlen = usb_g_bulk_write(usb_id, CDC_DATA_EP_IN, buf + offset, txlen);
  349. if (txlen == 0) {
  350. break;
  351. }
  352. if ((cdc_hdl->bmTransceiver & (BIT(1) | BIT(4))) != (BIT(1) | BIT(4))) {
  353. break;
  354. }
  355. offset += txlen;
  356. }
  357. os_mutex_post(&cdc_hdl->mutex_data);
  358. return offset;
  359. }
  360. u32 cdc_write_inir(const usb_dev usb_id, u8 *buf, u32 len)
  361. {
  362. #if CDC_INTR_EP_ENABLE
  363. u32 txlen, offset;
  364. if (cdc_hdl == NULL) {
  365. return 0;
  366. }
  367. if ((cdc_hdl->bmTransceiver & BIT(4)) == 0) {
  368. return 0;
  369. }
  370. offset = 0;
  371. os_mutex_pend(&cdc_hdl->mutex_intr, 0);
  372. while (offset < len) {
  373. txlen = len - offset > MAXP_SIZE_CDC_INTRIN ?
  374. MAXP_SIZE_CDC_INTRIN : len - offset;
  375. txlen = usb_g_intr_write(usb_id, CDC_INTR_EP_IN, buf + offset, txlen);
  376. if (txlen == 0) {
  377. break;
  378. }
  379. if ((cdc_hdl->bmTransceiver & BIT(4)) == 0) {
  380. break;
  381. }
  382. offset += txlen;
  383. }
  384. os_mutex_post(&cdc_hdl->mutex_intr);
  385. return offset;
  386. #else
  387. return 0;
  388. #endif
  389. }
  390. void cdc_register(const usb_dev usb_id)
  391. {
  392. struct usb_cdc_line_coding *lc;
  393. /* log_info("%s() %d", __func__, __LINE__); */
  394. if (!cdc_hdl) {
  395. #if USB_MALLOC_ENABLE
  396. cdc_hdl = zalloc(sizeof(struct usb_cdc_gadget));
  397. if (!cdc_hdl) {
  398. log_error("cdc_register err 1");
  399. return;
  400. }
  401. cdc_hdl->cdc_buffer = malloc(MAXP_SIZE_CDC_BULKOUT);
  402. if (!cdc_hdl->cdc_buf) {
  403. log_error("cdc_register err 2");
  404. goto __exit_err;
  405. }
  406. #else
  407. memset(&_cdc_hdl, 0, sizeof(struct usb_cdc_gadget));
  408. cdc_hdl = &_cdc_hdl;
  409. cdc_hdl->cdc_buffer = _cdc_buffer;
  410. #endif
  411. lc = (struct usb_cdc_line_coding *)cdc_hdl->subtype_data;
  412. usb_cdc_line_coding_init(lc);
  413. os_mutex_create(&cdc_hdl->mutex_data);
  414. cdc_hdl->bulk_ep_in_buffer = usb_alloc_ep_dmabuffer(usb_id, CDC_DATA_EP_IN | USB_DIR_IN, MAXP_SIZE_CDC_BULKIN + MAXP_SIZE_CDC_BULKOUT);
  415. cdc_hdl->bulk_ep_out_buffer = cdc_hdl->bulk_ep_in_buffer + MAXP_SIZE_CDC_BULKIN;
  416. #if CDC_INTR_EP_ENABLE
  417. os_mutex_create(&cdc_hdl->mutex_intr);
  418. cdc_hdl->intr_ep_in_buffer = usb_alloc_ep_dmabuffer(usb_id, CDC_INTR_EP_IN | USB_DIR_IN, MAXP_SIZE_CDC_INTRIN);
  419. #endif
  420. }
  421. return;
  422. __exit_err:
  423. #if USB_MALLOC_ENABLE
  424. if (cdc_hdl->cdc_buffer) {
  425. free(cdc_hdl->cdc_buffer);
  426. }
  427. if (cdc_hdl) {
  428. free(cdc_hdl);
  429. }
  430. #endif
  431. cdc_hdl = NULL;
  432. }
  433. void cdc_release(const usb_dev usb_id)
  434. {
  435. /* log_info("%s() %d", __func__, __LINE__); */
  436. if (cdc_hdl) {
  437. #if USB_MALLOC_ENABLE
  438. free(cdc_hdl->cdc_buffer);
  439. free(cdc_hdl);
  440. #endif
  441. cdc_hdl = NULL;
  442. }
  443. }
  444. #endif