usbd_cdc_ecm.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2023, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_cdc_ecm.h"
  8. #define CDC_ECM_OUT_EP_IDX 0
  9. #define CDC_ECM_IN_EP_IDX 1
  10. #define CDC_ECM_INT_EP_IDX 2
  11. /* Describe EndPoints configuration */
  12. static struct usbd_endpoint cdc_ecm_ep_data[3];
  13. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_cdc_ecm_rx_buffer[CONFIG_CDC_ECM_ETH_MAX_SEGSZE];
  14. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_cdc_ecm_tx_buffer[CONFIG_CDC_ECM_ETH_MAX_SEGSZE];
  15. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_cdc_ecm_notify_buf[16];
  16. volatile uint8_t *g_cdc_ecm_rx_data_buffer = NULL;
  17. volatile uint32_t g_cdc_ecm_rx_data_length = 0;
  18. volatile uint32_t g_cdc_ecm_tx_data_length = 0;
  19. static volatile uint8_t g_current_net_status = 0;
  20. static volatile uint8_t g_cmd_intf = 0;
  21. static uint32_t g_connect_speed_table[2] = { CDC_ECM_CONNECT_SPEED_UPSTREAM,
  22. CDC_ECM_CONNECT_SPEED_DOWNSTREAM };
  23. void usbd_cdc_ecm_send_notify(uint8_t notifycode, uint8_t value, uint32_t *speed)
  24. {
  25. struct cdc_eth_notification *notify = (struct cdc_eth_notification *)g_cdc_ecm_notify_buf;
  26. uint8_t bytes2send = 0;
  27. notify->bmRequestType = CDC_ECM_BMREQUEST_TYPE_ECM;
  28. notify->bNotificationType = notifycode;
  29. switch (notifycode) {
  30. case CDC_ECM_NOTIFY_CODE_NETWORK_CONNECTION:
  31. notify->wValue = value;
  32. notify->wIndex = g_cmd_intf;
  33. notify->wLength = 0U;
  34. for (uint8_t i = 0U; i < 8U; i++) {
  35. notify->data[i] = 0U;
  36. }
  37. bytes2send = 8U;
  38. break;
  39. case CDC_ECM_NOTIFY_CODE_RESPONSE_AVAILABLE:
  40. notify->wValue = 0U;
  41. notify->wIndex = g_cmd_intf;
  42. notify->wLength = 0U;
  43. for (uint8_t i = 0U; i < 8U; i++) {
  44. notify->data[i] = 0U;
  45. }
  46. bytes2send = 8U;
  47. break;
  48. case CDC_ECM_NOTIFY_CODE_CONNECTION_SPEED_CHANGE:
  49. notify->wValue = 0U;
  50. notify->wIndex = g_cmd_intf;
  51. notify->wLength = 0x0008U;
  52. bytes2send = 16U;
  53. memcpy(notify->data, speed, 8);
  54. break;
  55. default:
  56. break;
  57. }
  58. if (bytes2send) {
  59. usbd_ep_start_write(0, cdc_ecm_ep_data[CDC_ECM_INT_EP_IDX].ep_addr, g_cdc_ecm_notify_buf, bytes2send);
  60. }
  61. }
  62. static int cdc_ecm_class_interface_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
  63. {
  64. USB_LOG_DBG("CDC ECM Class request: "
  65. "bRequest 0x%02x\r\n",
  66. setup->bRequest);
  67. (void)busid;
  68. (void)data;
  69. (void)len;
  70. g_cmd_intf = LO_BYTE(setup->wIndex);
  71. switch (setup->bRequest) {
  72. case CDC_REQUEST_SET_ETHERNET_PACKET_FILTER:
  73. /* bit0 Promiscuous
  74. * bit1 ALL Multicast
  75. * bit2 Directed
  76. * bit3 Broadcast
  77. * bit4 Multicast
  78. */
  79. if (g_current_net_status == 0) {
  80. g_current_net_status = 1;
  81. usbd_cdc_ecm_send_notify(CDC_ECM_NOTIFY_CODE_NETWORK_CONNECTION, CDC_ECM_NET_CONNECTED, NULL);
  82. }
  83. break;
  84. default:
  85. USB_LOG_WRN("Unhandled CDC ECM Class bRequest 0x%02x\r\n", setup->bRequest);
  86. return -1;
  87. }
  88. return 0;
  89. }
  90. void cdc_ecm_notify_handler(uint8_t busid, uint8_t event, void *arg)
  91. {
  92. (void)busid;
  93. (void)arg;
  94. switch (event) {
  95. case USBD_EVENT_RESET:
  96. g_current_net_status = 0;
  97. g_cdc_ecm_rx_data_length = 0;
  98. g_cdc_ecm_tx_data_length = 0;
  99. g_cdc_ecm_rx_data_buffer = NULL;
  100. break;
  101. case USBD_EVENT_CONFIGURED:
  102. usbd_ep_start_read(0, cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_addr, &g_cdc_ecm_rx_buffer[g_cdc_ecm_rx_data_length], usbd_get_ep_mps(busid, cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_addr));
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. void cdc_ecm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes)
  109. {
  110. (void)busid;
  111. g_cdc_ecm_rx_data_length += nbytes;
  112. if (nbytes < usbd_get_ep_mps(0, ep)) {
  113. g_cdc_ecm_rx_data_buffer = g_cdc_ecm_rx_buffer;
  114. usbd_cdc_ecm_data_recv_done(g_cdc_ecm_rx_buffer, g_cdc_ecm_rx_data_length);
  115. } else {
  116. usbd_ep_start_read(0, ep, &g_cdc_ecm_rx_buffer[g_cdc_ecm_rx_data_length], usbd_get_ep_mps(0, ep));
  117. }
  118. }
  119. void cdc_ecm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  120. {
  121. (void)busid;
  122. if ((nbytes % usbd_get_ep_mps(0, ep)) == 0 && nbytes) {
  123. /* send zlp */
  124. usbd_ep_start_write(0, ep, NULL, 0);
  125. } else {
  126. g_cdc_ecm_tx_data_length = 0;
  127. }
  128. }
  129. void cdc_ecm_int_in(uint8_t busid, uint8_t ep, uint32_t nbytes)
  130. {
  131. (void)busid;
  132. (void)ep;
  133. (void)nbytes;
  134. if (g_current_net_status == 1) {
  135. g_current_net_status = 2;
  136. usbd_cdc_ecm_send_notify(CDC_ECM_NOTIFY_CODE_NETWORK_CONNECTION, CDC_ECM_NET_CONNECTED, g_connect_speed_table);
  137. }
  138. }
  139. int usbd_cdc_ecm_start_write(uint8_t *buf, uint32_t len)
  140. {
  141. if (g_cdc_ecm_tx_data_length > 0) {
  142. return -USB_ERR_BUSY;
  143. }
  144. g_cdc_ecm_tx_data_length = len;
  145. USB_LOG_DBG("txlen:%d\r\n", g_cdc_ecm_tx_data_length);
  146. return usbd_ep_start_write(0, cdc_ecm_ep_data[CDC_ECM_IN_EP_IDX].ep_addr, buf, g_cdc_ecm_tx_data_length);
  147. }
  148. void usbd_cdc_ecm_start_read_next(void)
  149. {
  150. g_cdc_ecm_rx_data_length = 0;
  151. g_cdc_ecm_rx_data_buffer = NULL;
  152. usbd_ep_start_read(0, cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_addr, g_cdc_ecm_rx_buffer, usbd_get_ep_mps(0, cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_addr));
  153. }
  154. #ifdef CONFIG_USBDEV_CDC_ECM_USING_LWIP
  155. struct pbuf *usbd_cdc_ecm_eth_rx(void)
  156. {
  157. struct pbuf *p;
  158. if (g_cdc_ecm_rx_data_buffer == NULL) {
  159. return NULL;
  160. }
  161. p = pbuf_alloc(PBUF_RAW, g_cdc_ecm_rx_data_length, PBUF_POOL);
  162. if (p == NULL) {
  163. usbd_cdc_ecm_start_read_next();
  164. return NULL;
  165. }
  166. usb_memcpy(p->payload, (uint8_t *)g_cdc_ecm_rx_buffer, g_cdc_ecm_rx_data_length);
  167. p->len = g_cdc_ecm_rx_data_length;
  168. USB_LOG_DBG("rxlen:%d\r\n", g_cdc_ecm_rx_data_length);
  169. usbd_cdc_ecm_start_read_next();
  170. return p;
  171. }
  172. int usbd_cdc_ecm_eth_tx(struct pbuf *p)
  173. {
  174. struct pbuf *q;
  175. uint8_t *buffer;
  176. if (g_cdc_ecm_tx_data_length > 0) {
  177. return -USB_ERR_BUSY;
  178. }
  179. if (p->tot_len > sizeof(g_cdc_ecm_tx_buffer)) {
  180. p->tot_len = sizeof(g_cdc_ecm_tx_buffer);
  181. }
  182. buffer = g_cdc_ecm_tx_buffer;
  183. for (q = p; q != NULL; q = q->next) {
  184. usb_memcpy(buffer, q->payload, q->len);
  185. buffer += q->len;
  186. }
  187. return usbd_cdc_ecm_start_write(g_cdc_ecm_tx_buffer, p->tot_len);
  188. }
  189. #endif
  190. struct usbd_interface *usbd_cdc_ecm_init_intf(struct usbd_interface *intf, const uint8_t int_ep, const uint8_t out_ep, const uint8_t in_ep)
  191. {
  192. intf->class_interface_handler = cdc_ecm_class_interface_request_handler;
  193. intf->class_endpoint_handler = NULL;
  194. intf->vendor_handler = NULL;
  195. intf->notify_handler = cdc_ecm_notify_handler;
  196. cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_addr = out_ep;
  197. cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX].ep_cb = cdc_ecm_bulk_out;
  198. cdc_ecm_ep_data[CDC_ECM_IN_EP_IDX].ep_addr = in_ep;
  199. cdc_ecm_ep_data[CDC_ECM_IN_EP_IDX].ep_cb = cdc_ecm_bulk_in;
  200. cdc_ecm_ep_data[CDC_ECM_INT_EP_IDX].ep_addr = int_ep;
  201. cdc_ecm_ep_data[CDC_ECM_INT_EP_IDX].ep_cb = cdc_ecm_int_in;
  202. usbd_add_endpoint(0, &cdc_ecm_ep_data[CDC_ECM_OUT_EP_IDX]);
  203. usbd_add_endpoint(0, &cdc_ecm_ep_data[CDC_ECM_IN_EP_IDX]);
  204. usbd_add_endpoint(0, &cdc_ecm_ep_data[CDC_ECM_INT_EP_IDX]);
  205. return intf;
  206. }
  207. void usbd_cdc_ecm_set_connect_speed(uint32_t speed[2])
  208. {
  209. memcpy(g_connect_speed_table, speed, 8);
  210. }
  211. __WEAK void usbd_cdc_ecm_data_recv_done(uint8_t *buf, uint32_t len)
  212. {
  213. (void)buf;
  214. (void)len;
  215. }