usbh_lwip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "netif/etharp.h"
  7. #include "lwip/netif.h"
  8. #include "lwip/pbuf.h"
  9. #include "lwip/tcpip.h"
  10. #if LWIP_DHCP
  11. #include "lwip/dhcp.h"
  12. #include "lwip/prot/dhcp.h"
  13. #endif
  14. #include "FreeRTOS.h"
  15. #include "task.h"
  16. #include "semphr.h"
  17. #include "timers.h"
  18. #include "usbh_core.h"
  19. #if LWIP_TCPIP_CORE_LOCKING_INPUT != 1
  20. #warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1, usb handles eth input with own thread
  21. #endif
  22. #if LWIP_TCPIP_CORE_LOCKING != 1
  23. #error must set LWIP_TCPIP_CORE_LOCKING to 1
  24. #endif
  25. #if PBUF_POOL_BUFSIZE < 1600
  26. #error PBUF_POOL_BUFSIZE must be larger than 1600
  27. #endif
  28. #define CONFIG_USBHOST_PLATFORM_CDC_ECM
  29. #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  30. #define CONFIG_USBHOST_PLATFORM_CDC_NCM
  31. #define CONFIG_USBHOST_PLATFORM_ASIX
  32. #define CONFIG_USBHOST_PLATFORM_RTL8152
  33. ip_addr_t g_ipaddr;
  34. ip_addr_t g_netmask;
  35. ip_addr_t g_gateway;
  36. void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf)
  37. {
  38. struct pbuf *q;
  39. uint8_t *buffer;
  40. buffer = buf;
  41. for (q = p; q != NULL; q = q->next) {
  42. usb_memcpy(buffer, q->payload, q->len);
  43. buffer += q->len;
  44. }
  45. }
  46. void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
  47. {
  48. #if LWIP_TCPIP_CORE_LOCKING_INPUT
  49. pbuf_type type = PBUF_REF;
  50. #else
  51. pbuf_type type = PBUF_POOL;
  52. #endif
  53. err_t err;
  54. struct pbuf *p;
  55. p = pbuf_alloc(PBUF_RAW, len, type);
  56. if (p != NULL) {
  57. #if LWIP_TCPIP_CORE_LOCKING_INPUT
  58. p->payload = buf;
  59. #else
  60. usb_memcpy(p->payload, buf, len);
  61. #endif
  62. err = netif->input(p, netif);
  63. if (err != ERR_OK) {
  64. pbuf_free(p);
  65. }
  66. } else {
  67. USB_LOG_ERR("No memory to alloc pbuf\r\n");
  68. }
  69. }
  70. TimerHandle_t dhcp_handle;
  71. static void dhcp_timeout(TimerHandle_t xTimer)
  72. {
  73. struct netif *netif = (struct netif *)pvTimerGetTimerID(xTimer);
  74. struct dhcp *dhcp;
  75. if (netif_is_up(netif)) {
  76. dhcp = netif_dhcp_data(netif);
  77. if (dhcp && (dhcp->state == DHCP_STATE_BOUND)) {
  78. USB_LOG_INFO("IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr));
  79. USB_LOG_INFO("IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask));
  80. USB_LOG_INFO("IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw));
  81. xTimerStop(xTimer, 0);
  82. }
  83. }
  84. }
  85. #ifdef CONFIG_USBHOST_PLATFORM_CDC_ECM
  86. #include "usbh_cdc_ecm.h"
  87. struct netif g_cdc_ecm_netif;
  88. static err_t usbh_cdc_ecm_linkoutput(struct netif *netif, struct pbuf *p)
  89. {
  90. int ret;
  91. usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
  92. ret = usbh_cdc_ecm_eth_output(p->tot_len);
  93. if (ret < 0) {
  94. return ERR_BUF;
  95. } else {
  96. return ERR_OK;
  97. }
  98. }
  99. void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen)
  100. {
  101. usbh_lwip_eth_input_common(&g_cdc_ecm_netif, buf, buflen);
  102. }
  103. static err_t usbh_cdc_ecm_if_init(struct netif *netif)
  104. {
  105. LWIP_ASSERT("netif != NULL", (netif != NULL));
  106. netif->mtu = 1500;
  107. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  108. netif->state = NULL;
  109. netif->name[0] = 'E';
  110. netif->name[1] = 'X';
  111. netif->output = etharp_output;
  112. netif->linkoutput = usbh_cdc_ecm_linkoutput;
  113. return ERR_OK;
  114. }
  115. void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class)
  116. {
  117. struct netif *netif = &g_cdc_ecm_netif;
  118. netif->hwaddr_len = 6;
  119. memcpy(netif->hwaddr, cdc_ecm_class->mac, 6);
  120. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  121. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  122. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  123. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_cdc_ecm_if_init, tcpip_input);
  124. netif_set_default(netif);
  125. while (!netif_is_up(netif)) {
  126. }
  127. dhcp_handle = xTimerCreate((const char *)"dhcp", (TickType_t)200, (UBaseType_t)pdTRUE, (void *const)netif, (TimerCallbackFunction_t)dhcp_timeout);
  128. if (dhcp_handle == NULL) {
  129. USB_LOG_ERR("timer creation failed! \r\n");
  130. while (1) {
  131. }
  132. }
  133. usb_osal_thread_create("usbh_cdc_ecm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ecm_rx_thread, NULL);
  134. #if LWIP_DHCP
  135. dhcp_start(netif);
  136. xTimerStart(dhcp_handle, 0);
  137. #endif
  138. }
  139. void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class)
  140. {
  141. struct netif *netif = &g_cdc_ecm_netif;
  142. #if LWIP_DHCP
  143. dhcp_stop(netif);
  144. dhcp_cleanup(netif);
  145. xTimerStop(dhcp_handle, 0);
  146. xTimerDelete(dhcp_handle, 0);
  147. #endif
  148. netif_set_down(netif);
  149. netif_remove(netif);
  150. }
  151. #endif
  152. #ifdef CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  153. #include "usbh_rndis.h"
  154. TimerHandle_t timer_handle;
  155. static void rndis_dev_keepalive_timeout(TimerHandle_t xTimer)
  156. {
  157. struct usbh_rndis *rndis_class = (struct usbh_rndis *)pvTimerGetTimerID(xTimer);
  158. usbh_rndis_keepalive(rndis_class);
  159. }
  160. void timer_init(struct usbh_rndis *rndis_class)
  161. {
  162. timer_handle = xTimerCreate((const char *)NULL, (TickType_t)5000, (UBaseType_t)pdTRUE, (void *const)rndis_class, (TimerCallbackFunction_t)rndis_dev_keepalive_timeout);
  163. if (NULL != timer_handle) {
  164. xTimerStart(timer_handle, 0);
  165. } else {
  166. USB_LOG_ERR("timer creation failed! \r\n");
  167. for (;;) {
  168. ;
  169. }
  170. }
  171. }
  172. struct netif g_rndis_netif;
  173. static err_t usbh_rndis_linkoutput(struct netif *netif, struct pbuf *p)
  174. {
  175. int ret;
  176. usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf());
  177. ret = usbh_rndis_eth_output(p->tot_len);
  178. if (ret < 0) {
  179. return ERR_BUF;
  180. } else {
  181. return ERR_OK;
  182. }
  183. }
  184. void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen)
  185. {
  186. usbh_lwip_eth_input_common(&g_rndis_netif, buf, buflen);
  187. }
  188. static err_t usbh_rndis_if_init(struct netif *netif)
  189. {
  190. LWIP_ASSERT("netif != NULL", (netif != NULL));
  191. netif->mtu = 1500;
  192. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  193. netif->state = NULL;
  194. netif->name[0] = 'E';
  195. netif->name[1] = 'X';
  196. netif->output = etharp_output;
  197. netif->linkoutput = usbh_rndis_linkoutput;
  198. return ERR_OK;
  199. }
  200. void usbh_rndis_run(struct usbh_rndis *rndis_class)
  201. {
  202. struct netif *netif = &g_rndis_netif;
  203. netif->hwaddr_len = 6;
  204. memcpy(netif->hwaddr, rndis_class->mac, 6);
  205. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  206. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  207. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  208. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_rndis_if_init, tcpip_input);
  209. netif_set_default(netif);
  210. while (!netif_is_up(netif)) {
  211. }
  212. dhcp_handle = xTimerCreate((const char *)"dhcp", (TickType_t)200, (UBaseType_t)pdTRUE, (void *const)netif, (TimerCallbackFunction_t)dhcp_timeout);
  213. if (dhcp_handle == NULL) {
  214. USB_LOG_ERR("timer creation failed! \r\n");
  215. while (1) {
  216. }
  217. }
  218. usb_osal_thread_create("usbh_rndis_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rndis_rx_thread, NULL);
  219. //timer_init(rndis_class);
  220. #if LWIP_DHCP
  221. dhcp_start(netif);
  222. xTimerStart(dhcp_handle, 0);
  223. #endif
  224. }
  225. void usbh_rndis_stop(struct usbh_rndis *rndis_class)
  226. {
  227. struct netif *netif = &g_rndis_netif;
  228. #if LWIP_DHCP
  229. dhcp_stop(netif);
  230. dhcp_cleanup(netif);
  231. xTimerStop(dhcp_handle, 0);
  232. xTimerDelete(dhcp_handle, 0);
  233. #endif
  234. netif_set_down(netif);
  235. netif_remove(netif);
  236. // xTimerStop(timer_handle, 0);
  237. // xTimerDelete(timer_handle, 0);
  238. }
  239. #endif
  240. #ifdef CONFIG_USBHOST_PLATFORM_CDC_NCM
  241. #include "usbh_cdc_ncm.h"
  242. struct netif g_cdc_ncm_netif;
  243. static err_t usbh_cdc_ncm_linkoutput(struct netif *netif, struct pbuf *p)
  244. {
  245. int ret;
  246. usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf());
  247. ret = usbh_cdc_ncm_eth_output(p->tot_len);
  248. if (ret < 0) {
  249. return ERR_BUF;
  250. } else {
  251. return ERR_OK;
  252. }
  253. }
  254. void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen)
  255. {
  256. usbh_lwip_eth_input_common(&g_cdc_ncm_netif, buf, buflen);
  257. }
  258. static err_t usbh_cdc_ncm_if_init(struct netif *netif)
  259. {
  260. LWIP_ASSERT("netif != NULL", (netif != NULL));
  261. netif->mtu = 1500;
  262. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  263. netif->state = NULL;
  264. netif->name[0] = 'E';
  265. netif->name[1] = 'X';
  266. netif->output = etharp_output;
  267. netif->linkoutput = usbh_cdc_ncm_linkoutput;
  268. return ERR_OK;
  269. }
  270. void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class)
  271. {
  272. struct netif *netif = &g_cdc_ncm_netif;
  273. netif->hwaddr_len = 6;
  274. memcpy(netif->hwaddr, cdc_ncm_class->mac, 6);
  275. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  276. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  277. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  278. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_cdc_ncm_if_init, tcpip_input);
  279. netif_set_default(netif);
  280. while (!netif_is_up(netif)) {
  281. }
  282. dhcp_handle = xTimerCreate((const char *)"dhcp", (TickType_t)200, (UBaseType_t)pdTRUE, (void *const)netif, (TimerCallbackFunction_t)dhcp_timeout);
  283. if (dhcp_handle == NULL) {
  284. USB_LOG_ERR("timer creation failed! \r\n");
  285. while (1) {
  286. }
  287. }
  288. usb_osal_thread_create("usbh_cdc_ncm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ncm_rx_thread, NULL);
  289. #if LWIP_DHCP
  290. dhcp_start(netif);
  291. xTimerStart(dhcp_handle, 0);
  292. #endif
  293. }
  294. void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class)
  295. {
  296. struct netif *netif = &g_cdc_ncm_netif;
  297. #if LWIP_DHCP
  298. dhcp_stop(netif);
  299. dhcp_cleanup(netif);
  300. xTimerStop(dhcp_handle, 0);
  301. xTimerDelete(dhcp_handle, 0);
  302. #endif
  303. netif_set_down(netif);
  304. netif_remove(netif);
  305. }
  306. #endif
  307. #ifdef CONFIG_USBHOST_PLATFORM_ASIX
  308. #include "usbh_asix.h"
  309. struct netif g_asix_netif;
  310. static err_t usbh_asix_linkoutput(struct netif *netif, struct pbuf *p)
  311. {
  312. int ret;
  313. usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf());
  314. ret = usbh_asix_eth_output(p->tot_len);
  315. if (ret < 0) {
  316. return ERR_BUF;
  317. } else {
  318. return ERR_OK;
  319. }
  320. }
  321. void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen)
  322. {
  323. usbh_lwip_eth_input_common(&g_asix_netif, buf, buflen);
  324. }
  325. static err_t usbh_asix_if_init(struct netif *netif)
  326. {
  327. LWIP_ASSERT("netif != NULL", (netif != NULL));
  328. netif->mtu = 1500;
  329. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  330. netif->state = NULL;
  331. netif->name[0] = 'E';
  332. netif->name[1] = 'X';
  333. netif->output = etharp_output;
  334. netif->linkoutput = usbh_asix_linkoutput;
  335. return ERR_OK;
  336. }
  337. void usbh_asix_run(struct usbh_asix *asix_class)
  338. {
  339. struct netif *netif = &g_asix_netif;
  340. netif->hwaddr_len = 6;
  341. memcpy(netif->hwaddr, asix_class->mac, 6);
  342. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  343. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  344. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  345. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_asix_if_init, tcpip_input);
  346. netif_set_default(netif);
  347. while (!netif_is_up(netif)) {
  348. }
  349. dhcp_handle = xTimerCreate((const char *)"dhcp", (TickType_t)200, (UBaseType_t)pdTRUE, (void *const)netif, (TimerCallbackFunction_t)dhcp_timeout);
  350. if (dhcp_handle == NULL) {
  351. USB_LOG_ERR("timer creation failed! \r\n");
  352. while (1) {
  353. }
  354. }
  355. usb_osal_thread_create("usbh_asix_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_asix_rx_thread, NULL);
  356. #if LWIP_DHCP
  357. dhcp_start(netif);
  358. xTimerStart(dhcp_handle, 0);
  359. #endif
  360. }
  361. void usbh_asix_stop(struct usbh_asix *asix_class)
  362. {
  363. struct netif *netif = &g_asix_netif;
  364. #if LWIP_DHCP
  365. dhcp_stop(netif);
  366. dhcp_cleanup(netif);
  367. xTimerStop(dhcp_handle, 0);
  368. xTimerDelete(dhcp_handle, 0);
  369. #endif
  370. netif_set_down(netif);
  371. netif_remove(netif);
  372. }
  373. #endif
  374. #ifdef CONFIG_USBHOST_PLATFORM_RTL8152
  375. #include "usbh_rtl8152.h"
  376. struct netif g_rtl8152_netif;
  377. static err_t usbh_rtl8152_linkoutput(struct netif *netif, struct pbuf *p)
  378. {
  379. int ret;
  380. usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf());
  381. ret = usbh_rtl8152_eth_output(p->tot_len);
  382. if (ret < 0) {
  383. return ERR_BUF;
  384. } else {
  385. return ERR_OK;
  386. }
  387. }
  388. void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen)
  389. {
  390. usbh_lwip_eth_input_common(&g_rtl8152_netif, buf, buflen);
  391. }
  392. static err_t usbh_rtl8152_if_init(struct netif *netif)
  393. {
  394. LWIP_ASSERT("netif != NULL", (netif != NULL));
  395. netif->mtu = 1500;
  396. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  397. netif->state = NULL;
  398. netif->name[0] = 'E';
  399. netif->name[1] = 'X';
  400. netif->output = etharp_output;
  401. netif->linkoutput = usbh_rtl8152_linkoutput;
  402. return ERR_OK;
  403. }
  404. void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class)
  405. {
  406. struct netif *netif = &g_rtl8152_netif;
  407. netif->hwaddr_len = 6;
  408. memcpy(netif->hwaddr, rtl8152_class->mac, 6);
  409. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  410. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  411. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  412. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_rtl8152_if_init, tcpip_input);
  413. netif_set_default(netif);
  414. while (!netif_is_up(netif)) {
  415. }
  416. dhcp_handle = xTimerCreate((const char *)"dhcp", (TickType_t)200, (UBaseType_t)pdTRUE, (void *const)netif, (TimerCallbackFunction_t)dhcp_timeout);
  417. if (dhcp_handle == NULL) {
  418. USB_LOG_ERR("timer creation failed! \r\n");
  419. while (1) {
  420. }
  421. }
  422. usb_osal_thread_create("usbh_rtl8152_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rtl8152_rx_thread, NULL);
  423. #if LWIP_DHCP
  424. dhcp_start(netif);
  425. xTimerStart(dhcp_handle, 0);
  426. #endif
  427. }
  428. void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class)
  429. {
  430. struct netif *netif = &g_rtl8152_netif;
  431. #if LWIP_DHCP
  432. dhcp_stop(netif);
  433. dhcp_cleanup(netif);
  434. xTimerStop(dhcp_handle, 0);
  435. xTimerDelete(dhcp_handle, 0);
  436. #endif
  437. netif_set_down(netif);
  438. netif_remove(netif);
  439. }
  440. #endif