usbh_lwip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <netif/ethernetif.h>
  17. #include "usbh_core.h"
  18. #include "lwip/opt.h"
  19. #ifndef RT_USING_LWIP212
  20. #error must enable RT_USING_LWIP212
  21. #endif
  22. #ifndef LWIP_NO_RX_THREAD
  23. #warning suggest you to enable LWIP_NO_RX_THREAD, we do not use rtthread eth rx thread
  24. #endif
  25. #ifndef LWIP_NO_TX_THREAD
  26. #warning suggest you to enable LWIP_NO_TX_THREAD, we do not use rtthread eth tx thread
  27. #endif
  28. #if LWIP_TCPIP_CORE_LOCKING_INPUT != 1
  29. #warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1 for better performance, usb handles eth input with own thread
  30. #endif
  31. #if LWIP_TCPIP_CORE_LOCKING != 1
  32. #error must set LWIP_TCPIP_CORE_LOCKING to 1
  33. #endif
  34. #if PBUF_POOL_BUFSIZE < 1600
  35. #error PBUF_POOL_BUFSIZE must be larger than 1600
  36. #endif
  37. #if RT_LWIP_TCPTHREAD_STACKSIZE < 2048
  38. #error RT_LWIP_TCPTHREAD_STACKSIZE must be >= 2048
  39. #endif
  40. #if !defined(CONFIG_USBHOST_PLATFORM_CDC_ECM) && \
  41. !defined(CONFIG_USBHOST_PLATFORM_CDC_RNDIS) && \
  42. !defined(CONFIG_USBHOST_PLATFORM_CDC_NCM) && \
  43. !defined(CONFIG_USBHOST_PLATFORM_ASIX) && \
  44. !defined(CONFIG_USBHOST_PLATFORM_RTL8152)
  45. #error "Please enable at least one USB Ethernet platform in usb_config.h or Kconfig"
  46. #endif
  47. // #define CONFIG_USBHOST_PLATFORM_CDC_ECM
  48. // #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  49. // #define CONFIG_USBHOST_PLATFORM_CDC_NCM
  50. // #define CONFIG_USBHOST_PLATFORM_ASIX
  51. // #define CONFIG_USBHOST_PLATFORM_RTL8152
  52. void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf)
  53. {
  54. struct pbuf *q;
  55. uint8_t *buffer;
  56. buffer = buf;
  57. for (q = p; q != NULL; q = q->next) {
  58. usb_memcpy(buffer, q->payload, q->len);
  59. buffer += q->len;
  60. }
  61. }
  62. void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
  63. {
  64. #if LWIP_TCPIP_CORE_LOCKING_INPUT
  65. pbuf_type type = PBUF_REF;
  66. #else
  67. pbuf_type type = PBUF_POOL;
  68. #endif
  69. err_t err;
  70. struct pbuf *p;
  71. p = pbuf_alloc(PBUF_RAW, len, type);
  72. if (p != NULL) {
  73. #if LWIP_TCPIP_CORE_LOCKING_INPUT
  74. p->payload = buf;
  75. #else
  76. usb_memcpy(p->payload, buf, len);
  77. #endif
  78. err = netif->input(p, netif);
  79. if (err != ERR_OK) {
  80. pbuf_free(p);
  81. }
  82. } else {
  83. USB_LOG_ERR("No memory to alloc pbuf\r\n");
  84. }
  85. }
  86. #ifdef CONFIG_USBHOST_PLATFORM_CDC_ECM
  87. #include "usbh_cdc_ecm.h"
  88. static struct eth_device g_cdc_ecm_dev;
  89. static rt_err_t rt_usbh_cdc_ecm_control(rt_device_t dev, int cmd, void *args)
  90. {
  91. struct usbh_cdc_ecm *cdc_ecm_class = (struct usbh_cdc_ecm *)dev->user_data;
  92. switch (cmd) {
  93. case NIOCTL_GADDR:
  94. /* get mac address */
  95. if (args)
  96. rt_memcpy(args, cdc_ecm_class->mac, 6);
  97. else
  98. return -RT_ERROR;
  99. break;
  100. default:
  101. break;
  102. }
  103. return RT_EOK;
  104. }
  105. static rt_err_t rt_usbh_cdc_ecm_eth_tx(rt_device_t dev, struct pbuf *p)
  106. {
  107. int ret;
  108. (void)dev;
  109. usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
  110. ret = usbh_cdc_ecm_eth_output(p->tot_len);
  111. if (ret < 0) {
  112. return -RT_ERROR;
  113. } else {
  114. return RT_EOK;
  115. }
  116. }
  117. void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen)
  118. {
  119. usbh_lwip_eth_input_common(g_cdc_ecm_dev.netif, buf, buflen);
  120. }
  121. void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class)
  122. {
  123. memset(&g_cdc_ecm_dev, 0, sizeof(struct eth_device));
  124. g_cdc_ecm_dev.parent.control = rt_usbh_cdc_ecm_control;
  125. g_cdc_ecm_dev.eth_rx = NULL;
  126. g_cdc_ecm_dev.eth_tx = rt_usbh_cdc_ecm_eth_tx;
  127. g_cdc_ecm_dev.parent.user_data = cdc_ecm_class;
  128. eth_device_init(&g_cdc_ecm_dev, "u0");
  129. eth_device_linkchange(&g_cdc_ecm_dev, RT_TRUE);
  130. usb_osal_thread_create("usbh_cdc_ecm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ecm_rx_thread, NULL);
  131. }
  132. void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class)
  133. {
  134. (void)cdc_ecm_class;
  135. eth_device_deinit(&g_cdc_ecm_dev);
  136. }
  137. #endif
  138. #ifdef CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  139. #include "usbh_rndis.h"
  140. static struct eth_device g_rndis_dev;
  141. static rt_timer_t keep_timer = RT_NULL;
  142. static void rndis_dev_keepalive_timeout(void *parameter)
  143. {
  144. struct usbh_rndis *rndis_class = (struct usbh_rndis *)parameter;
  145. usbh_rndis_keepalive(rndis_class);
  146. }
  147. static void timer_init(struct usbh_rndis *rndis_class)
  148. {
  149. keep_timer = rt_timer_create("keep",
  150. rndis_dev_keepalive_timeout,
  151. rndis_class,
  152. 5000,
  153. RT_TIMER_FLAG_PERIODIC |
  154. RT_TIMER_FLAG_SOFT_TIMER);
  155. rt_timer_start(keep_timer);
  156. }
  157. static rt_err_t rt_usbh_rndis_control(rt_device_t dev, int cmd, void *args)
  158. {
  159. struct usbh_rndis *rndis_class = (struct usbh_rndis *)dev->user_data;
  160. switch (cmd) {
  161. case NIOCTL_GADDR:
  162. /* get mac address */
  163. if (args)
  164. rt_memcpy(args, rndis_class->mac, 6);
  165. else
  166. return -RT_ERROR;
  167. break;
  168. default:
  169. break;
  170. }
  171. return RT_EOK;
  172. }
  173. static rt_err_t rt_usbh_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
  174. {
  175. int ret;
  176. (void)dev;
  177. usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf());
  178. ret = usbh_rndis_eth_output(p->tot_len);
  179. if (ret < 0) {
  180. return -RT_ERROR;
  181. } else {
  182. return RT_EOK;
  183. }
  184. }
  185. void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen)
  186. {
  187. usbh_lwip_eth_input_common(g_rndis_dev.netif, buf, buflen);
  188. }
  189. void usbh_rndis_run(struct usbh_rndis *rndis_class)
  190. {
  191. memset(&g_rndis_dev, 0, sizeof(struct eth_device));
  192. g_rndis_dev.parent.control = rt_usbh_rndis_control;
  193. g_rndis_dev.eth_rx = NULL;
  194. g_rndis_dev.eth_tx = rt_usbh_rndis_eth_tx;
  195. g_rndis_dev.parent.user_data = rndis_class;
  196. eth_device_init(&g_rndis_dev, "u2");
  197. eth_device_linkchange(&g_rndis_dev, RT_TRUE);
  198. usb_osal_thread_create("usbh_rndis_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rndis_rx_thread, NULL);
  199. //timer_init(rndis_class);
  200. }
  201. void usbh_rndis_stop(struct usbh_rndis *rndis_class)
  202. {
  203. (void)rndis_class;
  204. eth_device_deinit(&g_rndis_dev);
  205. // rt_timer_stop(keep_timer);
  206. // rt_timer_delete(keep_timer);
  207. }
  208. #endif
  209. #ifdef CONFIG_USBHOST_PLATFORM_CDC_NCM
  210. #include "usbh_cdc_ncm.h"
  211. static struct eth_device g_cdc_ncm_dev;
  212. static rt_err_t rt_usbh_cdc_ncm_control(rt_device_t dev, int cmd, void *args)
  213. {
  214. struct usbh_cdc_ncm *cdc_ncm_class = (struct usbh_cdc_ncm *)dev->user_data;
  215. switch (cmd) {
  216. case NIOCTL_GADDR:
  217. /* get mac address */
  218. if (args)
  219. rt_memcpy(args, cdc_ncm_class->mac, 6);
  220. else
  221. return -RT_ERROR;
  222. break;
  223. default:
  224. break;
  225. }
  226. return RT_EOK;
  227. }
  228. static rt_err_t rt_usbh_cdc_ncm_eth_tx(rt_device_t dev, struct pbuf *p)
  229. {
  230. int ret;
  231. (void)dev;
  232. usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf());
  233. ret = usbh_cdc_ncm_eth_output(p->tot_len);
  234. if (ret < 0) {
  235. return -RT_ERROR;
  236. } else {
  237. return RT_EOK;
  238. }
  239. }
  240. void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen)
  241. {
  242. usbh_lwip_eth_input_common(g_cdc_ncm_dev.netif, buf, buflen);
  243. }
  244. void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class)
  245. {
  246. memset(&g_cdc_ncm_dev, 0, sizeof(struct eth_device));
  247. g_cdc_ncm_dev.parent.control = rt_usbh_cdc_ncm_control;
  248. g_cdc_ncm_dev.eth_rx = NULL;
  249. g_cdc_ncm_dev.eth_tx = rt_usbh_cdc_ncm_eth_tx;
  250. g_cdc_ncm_dev.parent.user_data = cdc_ncm_class;
  251. eth_device_init(&g_cdc_ncm_dev, "u1");
  252. eth_device_linkchange(&g_cdc_ncm_dev, RT_TRUE);
  253. usb_osal_thread_create("usbh_cdc_ncm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ncm_rx_thread, NULL);
  254. }
  255. void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class)
  256. {
  257. (void)cdc_ncm_class;
  258. eth_device_deinit(&g_cdc_ncm_dev);
  259. }
  260. #endif
  261. #ifdef CONFIG_USBHOST_PLATFORM_ASIX
  262. #include "usbh_asix.h"
  263. static struct eth_device g_asix_dev;
  264. static rt_err_t rt_usbh_asix_control(rt_device_t dev, int cmd, void *args)
  265. {
  266. struct usbh_asix *asix_class = (struct usbh_asix *)dev->user_data;
  267. switch (cmd) {
  268. case NIOCTL_GADDR:
  269. /* get mac address */
  270. if (args)
  271. rt_memcpy(args, asix_class->mac, 6);
  272. else
  273. return -RT_ERROR;
  274. break;
  275. default:
  276. break;
  277. }
  278. return RT_EOK;
  279. }
  280. static rt_err_t rt_usbh_asix_eth_tx(rt_device_t dev, struct pbuf *p)
  281. {
  282. int ret;
  283. (void)dev;
  284. usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf());
  285. ret = usbh_asix_eth_output(p->tot_len);
  286. if (ret < 0) {
  287. return -RT_ERROR;
  288. } else {
  289. return RT_EOK;
  290. }
  291. }
  292. void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen)
  293. {
  294. usbh_lwip_eth_input_common(g_asix_dev.netif, buf, buflen);
  295. }
  296. void usbh_asix_run(struct usbh_asix *asix_class)
  297. {
  298. memset(&g_asix_dev, 0, sizeof(struct eth_device));
  299. g_asix_dev.parent.control = rt_usbh_asix_control;
  300. g_asix_dev.eth_rx = NULL;
  301. g_asix_dev.eth_tx = rt_usbh_asix_eth_tx;
  302. g_asix_dev.parent.user_data = asix_class;
  303. eth_device_init(&g_asix_dev, "u3");
  304. eth_device_linkchange(&g_asix_dev, RT_TRUE);
  305. usb_osal_thread_create("usbh_asix_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_asix_rx_thread, NULL);
  306. }
  307. void usbh_asix_stop(struct usbh_asix *asix_class)
  308. {
  309. (void)asix_class;
  310. eth_device_deinit(&g_asix_dev);
  311. }
  312. #endif
  313. #ifdef CONFIG_USBHOST_PLATFORM_RTL8152
  314. #include "usbh_rtl8152.h"
  315. static struct eth_device g_rtl8152_dev;
  316. static rt_err_t rt_usbh_rtl8152_control(rt_device_t dev, int cmd, void *args)
  317. {
  318. struct usbh_rtl8152 *rtl8152_class = (struct usbh_rtl8152 *)dev->user_data;
  319. switch (cmd) {
  320. case NIOCTL_GADDR:
  321. /* get mac address */
  322. if (args)
  323. rt_memcpy(args, rtl8152_class->mac, 6);
  324. else
  325. return -RT_ERROR;
  326. break;
  327. default:
  328. break;
  329. }
  330. return RT_EOK;
  331. }
  332. static rt_err_t rt_usbh_rtl8152_eth_tx(rt_device_t dev, struct pbuf *p)
  333. {
  334. int ret;
  335. (void)dev;
  336. usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf());
  337. ret = usbh_rtl8152_eth_output(p->tot_len);
  338. if (ret < 0) {
  339. return -RT_ERROR;
  340. } else {
  341. return RT_EOK;
  342. }
  343. }
  344. void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen)
  345. {
  346. usbh_lwip_eth_input_common(g_rtl8152_dev.netif, buf, buflen);
  347. }
  348. void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class)
  349. {
  350. memset(&g_rtl8152_dev, 0, sizeof(struct eth_device));
  351. g_rtl8152_dev.parent.control = rt_usbh_rtl8152_control;
  352. g_rtl8152_dev.eth_rx = NULL;
  353. g_rtl8152_dev.eth_tx = rt_usbh_rtl8152_eth_tx;
  354. g_rtl8152_dev.parent.user_data = rtl8152_class;
  355. eth_device_init(&g_rtl8152_dev, "u4");
  356. eth_device_linkchange(&g_rtl8152_dev, RT_TRUE);
  357. usb_osal_thread_create("usbh_rtl8152_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rtl8152_rx_thread, NULL);
  358. }
  359. void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class)
  360. {
  361. (void)rtl8152_class;
  362. eth_device_deinit(&g_rtl8152_dev);
  363. }
  364. #endif