usbh_net.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2025, Loogg
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <inttypes.h>
  8. #include "lwip/netif.h"
  9. #include "esp_log.h"
  10. #include "esp_event.h"
  11. #include "esp_check.h"
  12. #include "esp_netif.h"
  13. #include "usbh_core.h"
  14. #if TCPIP_THREAD_STACKSIZE < 1024
  15. #error TCPIP_THREAD_STACKSIZE must be >= 1024
  16. #endif
  17. // #define CONFIG_USBHOST_PLATFORM_CDC_ECM
  18. // #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  19. // #define CONFIG_USBHOST_PLATFORM_CDC_NCM
  20. // #define CONFIG_USBHOST_PLATFORM_ASIX
  21. // #define CONFIG_USBHOST_PLATFORM_RTL8152
  22. struct usbh_net_netif_glue {
  23. esp_netif_driver_base_t base;
  24. esp_event_handler_instance_t ins_got_ip;
  25. void *usbh_class;
  26. esp_err_t (*transmit)(void *h, void *buffer, size_t len);
  27. };
  28. static void usbh_net_input_common(struct usbh_net_netif_glue *netif_glue, uint8_t *buf, uint32_t len)
  29. {
  30. uint8_t *input_buf = buf;
  31. #if !LWIP_TCPIP_CORE_LOCKING_INPUT
  32. input_buf = usb_osal_malloc(len);
  33. if (input_buf == NULL) {
  34. USB_LOG_ERR("No memory to alloc input buffer\r\n");
  35. return;
  36. }
  37. usb_memcpy(input_buf, buf, len);
  38. #endif
  39. esp_netif_receive(netif_glue->base.netif, input_buf, len, NULL);
  40. }
  41. static void usbh_net_free(void *h, void *buffer)
  42. {
  43. #if !LWIP_TCPIP_CORE_LOCKING_INPUT
  44. usb_osal_free(buffer);
  45. #endif
  46. }
  47. static esp_err_t usbh_net_post_attach(esp_netif_t *esp_netif, void *args)
  48. {
  49. struct usbh_net_netif_glue *netif_glue = (struct usbh_net_netif_glue *)args;
  50. netif_glue->base.netif = esp_netif;
  51. // set driver related config to esp-netif
  52. esp_netif_driver_ifconfig_t driver_ifconfig = {
  53. .handle = netif_glue,
  54. .transmit = netif_glue->transmit,
  55. .driver_free_rx_buffer = usbh_net_free,
  56. };
  57. ESP_ERROR_CHECK(esp_netif_set_driver_config(esp_netif, &driver_ifconfig));
  58. return ESP_OK;
  59. }
  60. static void usbh_net_action_got_ip(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
  61. {
  62. struct usbh_net_netif_glue *netif_glue = (struct usbh_net_netif_glue *)handler_args;
  63. ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
  64. const esp_netif_ip_info_t *ip_info = &event->ip_info;
  65. USB_LOG_INFO("NETIF %s Got IP Address\r\n", esp_netif_get_ifkey(netif_glue->base.netif));
  66. USB_LOG_INFO("IP:" IPSTR "\r\n", IP2STR(&ip_info->ip));
  67. USB_LOG_INFO("MASK:" IPSTR "\r\n", IP2STR(&ip_info->netmask));
  68. USB_LOG_INFO("GW:" IPSTR "\r\n\r\n", IP2STR(&ip_info->gw));
  69. }
  70. static esp_err_t usbh_net_netif_glue_init_common(struct usbh_net_netif_glue *netif_glue, void *usbh_class, esp_err_t (*transmit)(void *h, void *buffer, size_t len))
  71. {
  72. netif_glue->usbh_class = usbh_class;
  73. netif_glue->transmit = transmit;
  74. netif_glue->base.post_attach = usbh_net_post_attach;
  75. return esp_event_handler_instance_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, usbh_net_action_got_ip, netif_glue, &netif_glue->ins_got_ip);
  76. }
  77. static esp_err_t usbh_net_netif_glue_deinit_common(struct usbh_net_netif_glue *netif_glue)
  78. {
  79. return esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_ETH_GOT_IP, netif_glue->ins_got_ip);
  80. }
  81. #ifdef CONFIG_USBHOST_PLATFORM_CDC_ECM
  82. #include "usbh_cdc_ecm.h"
  83. struct usbh_net_netif_glue g_cdc_ecm_netif_glue;
  84. static esp_err_t usbh_cdc_ecm_transmit(void *h, void *buffer, size_t len)
  85. {
  86. int ret;
  87. (void)h;
  88. usb_memcpy(usbh_cdc_ecm_get_eth_txbuf(), buffer, len);
  89. ret = usbh_cdc_ecm_eth_output(len);
  90. if (ret < 0) {
  91. return ESP_FAIL;
  92. }
  93. return ESP_OK;
  94. }
  95. void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen)
  96. {
  97. usbh_net_input_common(&g_cdc_ecm_netif_glue, buf, buflen);
  98. }
  99. void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class)
  100. {
  101. memset(&g_cdc_ecm_netif_glue, 0, sizeof(struct usbh_net_netif_glue));
  102. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_ETH();
  103. base_cfg.if_key = "u0";
  104. base_cfg.if_desc = "usbh cdc ecm";
  105. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  106. netif_cfg.base = &base_cfg;
  107. esp_netif_t *esp_netif = esp_netif_new(&netif_cfg);
  108. usbh_net_netif_glue_init_common(&g_cdc_ecm_netif_glue, cdc_ecm_class, usbh_cdc_ecm_transmit);
  109. esp_netif_attach(esp_netif, &g_cdc_ecm_netif_glue.base);
  110. esp_netif_set_mac(esp_netif, cdc_ecm_class->mac);
  111. esp_netif_action_start(esp_netif, NULL, 0, NULL);
  112. esp_netif_action_connected(esp_netif, NULL, 0, NULL);
  113. usb_osal_thread_create("usbh_cdc_ecm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ecm_rx_thread, NULL);
  114. }
  115. void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class)
  116. {
  117. (void)cdc_ecm_class;
  118. usbh_net_netif_glue_deinit_common(&g_cdc_ecm_netif_glue);
  119. esp_netif_action_stop(g_cdc_ecm_netif_glue.base.netif, NULL, 0, NULL);
  120. esp_netif_destroy(g_cdc_ecm_netif_glue.base.netif);
  121. }
  122. #endif
  123. #ifdef CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  124. #include "usbh_rndis.h"
  125. struct usb_osal_timer *timer_handle;
  126. static void rndis_dev_keepalive_timeout(void *arg)
  127. {
  128. struct usbh_rndis *rndis_class = (struct usbh_rndis *)arg;
  129. usbh_rndis_keepalive(rndis_class);
  130. }
  131. void timer_init(struct usbh_rndis *rndis_class)
  132. {
  133. timer_handle = usb_osal_timer_create("rndis_keepalive", 5000, rndis_dev_keepalive_timeout, rndis_class, true);
  134. if (NULL != timer_handle) {
  135. usb_osal_timer_start(timer_handle);
  136. } else {
  137. USB_LOG_ERR("timer creation failed! \r\n");
  138. for (;;) {
  139. ;
  140. }
  141. }
  142. }
  143. struct usbh_net_netif_glue g_rndis_netif_glue;
  144. static esp_err_t usbh_rndis_transmit(void *h, void *buffer, size_t len)
  145. {
  146. int ret;
  147. (void)h;
  148. usb_memcpy(usbh_rndis_get_eth_txbuf(), buffer, len);
  149. ret = usbh_rndis_eth_output(len);
  150. if (ret < 0) {
  151. return ESP_FAIL;
  152. }
  153. return ESP_OK;
  154. }
  155. void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen)
  156. {
  157. usbh_net_input_common(&g_rndis_netif_glue, buf, buflen);
  158. }
  159. void usbh_rndis_run(struct usbh_rndis *rndis_class)
  160. {
  161. memset(&g_rndis_netif_glue, 0, sizeof(struct usbh_net_netif_glue));
  162. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_ETH();
  163. base_cfg.if_key = "u2";
  164. base_cfg.if_desc = "usbh rndis";
  165. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  166. netif_cfg.base = &base_cfg;
  167. esp_netif_t *esp_netif = esp_netif_new(&netif_cfg);
  168. usbh_net_netif_glue_init_common(&g_rndis_netif_glue, rndis_class, usbh_rndis_transmit);
  169. esp_netif_attach(esp_netif, &g_rndis_netif_glue.base);
  170. esp_netif_set_mac(esp_netif, rndis_class->mac);
  171. esp_netif_action_start(esp_netif, NULL, 0, NULL);
  172. esp_netif_action_connected(esp_netif, NULL, 0, NULL);
  173. usb_osal_thread_create("usbh_rndis_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rndis_rx_thread, NULL);
  174. //timer_init(rndis_class);
  175. }
  176. void usbh_rndis_stop(struct usbh_rndis *rndis_class)
  177. {
  178. (void)rndis_class;
  179. usbh_net_netif_glue_deinit_common(&g_rndis_netif_glue);
  180. esp_netif_action_stop(g_rndis_netif_glue.base.netif, NULL, 0, NULL);
  181. esp_netif_destroy(g_rndis_netif_glue.base.netif);
  182. // xTimerStop(timer_handle, 0);
  183. // xTimerDelete(timer_handle, 0);
  184. }
  185. #endif
  186. #ifdef CONFIG_USBHOST_PLATFORM_CDC_NCM
  187. #include "usbh_cdc_ncm.h"
  188. struct usbh_net_netif_glue g_cdc_ncm_netif_glue;
  189. static esp_err_t usbh_cdc_ncm_transmit(void *h, void *buffer, size_t len)
  190. {
  191. int ret;
  192. (void)h;
  193. usb_memcpy(usbh_cdc_ncm_get_eth_txbuf(), buffer, len);
  194. ret = usbh_cdc_ncm_eth_output(len);
  195. if (ret < 0) {
  196. return ESP_FAIL;
  197. }
  198. return ESP_OK;
  199. }
  200. void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen)
  201. {
  202. usbh_net_input_common(&g_cdc_ncm_netif_glue, buf, buflen);
  203. }
  204. void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class)
  205. {
  206. memset(&g_cdc_ncm_netif_glue, 0, sizeof(struct usbh_net_netif_glue));
  207. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_ETH();
  208. base_cfg.if_key = "u1";
  209. base_cfg.if_desc = "usbh cdc ncm";
  210. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  211. netif_cfg.base = &base_cfg;
  212. esp_netif_t *esp_netif = esp_netif_new(&netif_cfg);
  213. usbh_net_netif_glue_init_common(&g_cdc_ncm_netif_glue, cdc_ncm_class, usbh_cdc_ncm_transmit);
  214. esp_netif_attach(esp_netif, &g_cdc_ncm_netif_glue.base);
  215. esp_netif_set_mac(esp_netif, cdc_ncm_class->mac);
  216. esp_netif_action_start(esp_netif, NULL, 0, NULL);
  217. esp_netif_action_connected(esp_netif, NULL, 0, NULL);
  218. usb_osal_thread_create("usbh_cdc_ncm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ncm_rx_thread, NULL);
  219. }
  220. void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class)
  221. {
  222. (void)cdc_ncm_class;
  223. usbh_net_netif_glue_deinit_common(&g_cdc_ncm_netif_glue);
  224. esp_netif_action_stop(g_cdc_ncm_netif_glue.base.netif, NULL, 0, NULL);
  225. esp_netif_destroy(g_cdc_ncm_netif_glue.base.netif);
  226. }
  227. #endif
  228. #ifdef CONFIG_USBHOST_PLATFORM_ASIX
  229. #include "usbh_asix.h"
  230. struct usbh_net_netif_glue g_asix_netif_glue;
  231. static esp_err_t usbh_asix_transmit(void *h, void *buffer, size_t len)
  232. {
  233. int ret;
  234. (void)h;
  235. usb_memcpy(usbh_asix_get_eth_txbuf(), buffer, len);
  236. ret = usbh_asix_eth_output(len);
  237. if (ret < 0) {
  238. return ESP_FAIL;
  239. }
  240. return ESP_OK;
  241. }
  242. void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen)
  243. {
  244. usbh_net_input_common(&g_asix_netif_glue, buf, buflen);
  245. }
  246. void usbh_asix_run(struct usbh_asix *asix_class)
  247. {
  248. memset(&g_asix_netif_glue, 0, sizeof(struct usbh_net_netif_glue));
  249. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_ETH();
  250. base_cfg.if_key = "u3";
  251. base_cfg.if_desc = "usbh asix";
  252. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  253. netif_cfg.base = &base_cfg;
  254. esp_netif_t *esp_netif = esp_netif_new(&netif_cfg);
  255. usbh_net_netif_glue_init_common(&g_asix_netif_glue, asix_class, usbh_asix_transmit);
  256. esp_netif_attach(esp_netif, &g_asix_netif_glue.base);
  257. esp_netif_set_mac(esp_netif, asix_class->mac);
  258. esp_netif_action_start(esp_netif, NULL, 0, NULL);
  259. esp_netif_action_connected(esp_netif, NULL, 0, NULL);
  260. usb_osal_thread_create("usbh_asix_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_asix_rx_thread, NULL);
  261. }
  262. void usbh_asix_stop(struct usbh_asix *asix_class)
  263. {
  264. (void)asix_class;
  265. usbh_net_netif_glue_deinit_common(&g_asix_netif_glue);
  266. esp_netif_action_stop(g_asix_netif_glue.base.netif, NULL, 0, NULL);
  267. esp_netif_destroy(g_asix_netif_glue.base.netif);
  268. }
  269. #endif
  270. #ifdef CONFIG_USBHOST_PLATFORM_RTL8152
  271. #include "usbh_rtl8152.h"
  272. struct usbh_net_netif_glue g_rtl8152_netif_glue;
  273. static esp_err_t usbh_rtl8152_transmit(void *h, void *buffer, size_t len)
  274. {
  275. int ret;
  276. (void)h;
  277. usb_memcpy(usbh_rtl8152_get_eth_txbuf(), buffer, len);
  278. ret = usbh_rtl8152_eth_output(len);
  279. if (ret < 0) {
  280. return ESP_FAIL;
  281. }
  282. return ESP_OK;
  283. }
  284. void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen)
  285. {
  286. usbh_net_input_common(&g_rtl8152_netif_glue, buf, buflen);
  287. }
  288. void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class)
  289. {
  290. memset(&g_rtl8152_netif_glue, 0, sizeof(struct usbh_net_netif_glue));
  291. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_ETH();
  292. base_cfg.if_key = "u4";
  293. base_cfg.if_desc = "usbh rtl8152";
  294. esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
  295. netif_cfg.base = &base_cfg;
  296. esp_netif_t *esp_netif = esp_netif_new(&netif_cfg);
  297. usbh_net_netif_glue_init_common(&g_rtl8152_netif_glue, rtl8152_class, usbh_rtl8152_transmit);
  298. esp_netif_attach(esp_netif, &g_rtl8152_netif_glue.base);
  299. esp_netif_set_mac(esp_netif, rtl8152_class->mac);
  300. esp_netif_action_start(esp_netif, NULL, 0, NULL);
  301. esp_netif_action_connected(esp_netif, NULL, 0, NULL);
  302. usb_osal_thread_create("usbh_rtl8152_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rtl8152_rx_thread, NULL);
  303. }
  304. void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class)
  305. {
  306. (void)rtl8152_class;
  307. usbh_net_netif_glue_deinit_common(&g_rtl8152_netif_glue);
  308. esp_netif_action_stop(g_rtl8152_netif_glue.base.netif, NULL, 0, NULL);
  309. esp_netif_destroy(g_rtl8152_netif_glue.base.netif);
  310. }
  311. #endif