usbh_net.c 12 KB

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