usbh_lwip.c 10 KB

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