refactor(platform): add usbh_xxx_get_eth_txbuf api, especially for lwip pbuf list

This commit is contained in:
sakumisu
2024-06-25 17:18:09 +08:00
parent 31cd834ded
commit 6c92681e48
13 changed files with 127 additions and 54 deletions

View File

@@ -19,11 +19,11 @@
#include "usbh_core.h"
#if LWIP_TCPIP_CORE_LOCKING_INPUT !=1
#if LWIP_TCPIP_CORE_LOCKING_INPUT != 1
#warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1, usb handles eth input with own thread
#endif
#if LWIP_TCPIP_CORE_LOCKING !=1
#if LWIP_TCPIP_CORE_LOCKING != 1
#error must set LWIP_TCPIP_CORE_LOCKING to 1
#endif
@@ -41,6 +41,18 @@ ip_addr_t g_ipaddr;
ip_addr_t g_netmask;
ip_addr_t g_gateway;
void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf)
{
struct pbuf *q;
uint8_t *buffer;
buffer = buf;
for (q = p; q != NULL; q = q->next) {
usb_memcpy(buffer, q->payload, q->len);
buffer += q->len;
}
}
void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
{
#if LWIP_TCPIP_CORE_LOCKING_INPUT
@@ -56,7 +68,7 @@ void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
#if LWIP_TCPIP_CORE_LOCKING_INPUT
p->payload = buf;
#else
memcpy(p->payload, buf, len);
usb_memcpy(p->payload, buf, len);
#endif
err = netif->input(p, netif);
if (err != ERR_OK) {
@@ -94,7 +106,10 @@ struct netif g_cdc_ecm_netif;
static err_t usbh_cdc_ecm_linkoutput(struct netif *netif, struct pbuf *p)
{
int ret = usbh_cdc_ecm_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
ret = usbh_cdc_ecm_eth_output(p->tot_len);
if (ret < 0) {
return ERR_BUF;
} else {
@@ -193,7 +208,10 @@ struct netif g_rndis_netif;
static err_t usbh_rndis_linkoutput(struct netif *netif, struct pbuf *p)
{
int ret = usbh_rndis_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf());
ret = usbh_rndis_eth_output(p->tot_len);
if (ret < 0) {
return ERR_BUF;
} else {
@@ -276,7 +294,10 @@ struct netif g_cdc_ncm_netif;
static err_t usbh_cdc_ncm_linkoutput(struct netif *netif, struct pbuf *p)
{
int ret = usbh_cdc_ncm_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf());
ret = usbh_cdc_ncm_eth_output(p->tot_len);
if (ret < 0) {
return ERR_BUF;
} else {
@@ -354,7 +375,10 @@ struct netif g_asix_netif;
static err_t usbh_asix_linkoutput(struct netif *netif, struct pbuf *p)
{
int ret = usbh_asix_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf());
ret = usbh_asix_eth_output(p->tot_len);
if (ret < 0) {
return ERR_BUF;
} else {
@@ -432,7 +456,10 @@ struct netif g_rtl8152_netif;
static err_t usbh_rtl8152_linkoutput(struct netif *netif, struct pbuf *p)
{
int ret = usbh_rtl8152_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf());
ret = usbh_rtl8152_eth_output(p->tot_len);
if (ret < 0) {
return ERR_BUF;
} else {

View File

@@ -18,11 +18,11 @@
#include "usbh_core.h"
#define CONFIG_USBHOST_PLATFORM_CDC_ECM
// #define CONFIG_USBHOST_PLATFORM_CDC_ECM
#define CONFIG_USBHOST_PLATFORM_CDC_RNDIS
#define CONFIG_USBHOST_PLATFORM_CDC_NCM
#define CONFIG_USBHOST_PLATFORM_ASIX
#define CONFIG_USBHOST_PLATFORM_RTL8152
// #define CONFIG_USBHOST_PLATFORM_CDC_NCM
// #define CONFIG_USBHOST_PLATFORM_ASIX
// #define CONFIG_USBHOST_PLATFORM_RTL8152
struct usbh_net {
struct net_driver_s netdev;
@@ -30,7 +30,12 @@ struct usbh_net {
bool linkup;
};
void usbh_net_eth_input_common(struct net_driver_s *dev, uint8_t *buf, size_t len, int (*eth_output)(uint8_t *buf, uint32_t buflen))
void usbh_net_eth_output_common(struct net_driver_s *dev, uint8_t *buf)
{
usb_memcpy(buf, dev->d_buf, dev->d_len);
}
void usbh_net_eth_input_common(struct net_driver_s *dev, uint8_t *buf, size_t len, int (*eth_output)(uint32_t buflen))
{
FAR struct eth_hdr_s *hdr;
@@ -56,7 +61,8 @@ void usbh_net_eth_input_common(struct net_driver_s *dev, uint8_t *buf, size_t le
ipv4_input(dev);
if (dev->d_len > 0) {
/* And send the packet */
eth_output(dev->d_buf, dev->d_len);
usbh_net_eth_output_common(dev, usbh_rndis_get_eth_txbuf());
eth_output(dev->d_len);
}
} else
#endif
@@ -70,7 +76,8 @@ void usbh_net_eth_input_common(struct net_driver_s *dev, uint8_t *buf, size_t le
if (dev->d_len > 0) {
/* And send the packet */
eth_output(dev->d_buf, dev->d_len);
usbh_net_eth_output_common(dev, usbh_rndis_get_eth_txbuf());
eth_output(dev->d_len);
}
} else
#endif
@@ -80,7 +87,8 @@ void usbh_net_eth_input_common(struct net_driver_s *dev, uint8_t *buf, size_t le
arp_input(dev);
if (dev->d_len > 0) {
eth_output(dev->d_buf, dev->d_len);
usbh_net_eth_output_common(dev, usbh_rndis_get_eth_txbuf());
eth_output(dev->d_len);
}
} else
#endif
@@ -113,7 +121,8 @@ static int rndis_ifdown(struct net_driver_s *dev)
static int rndis_txpoll(struct net_driver_s *dev)
{
return usbh_rndis_eth_output(g_rndis_dev.netdev.d_buf, g_rndis_dev.netdev.d_len);
usbh_net_eth_output_common(&g_rndis_dev, usbh_rndis_get_eth_txbuf());
return usbh_rndis_eth_output(g_rndis_dev.netdev.d_len);
}
static void rndis_txavail_work(void *arg)

View File

@@ -32,11 +32,11 @@
#warning suggest you to enable LWIP_NO_TX_THREAD, we do not use rtthread eth tx thread
#endif
#if LWIP_TCPIP_CORE_LOCKING_INPUT !=1
#if LWIP_TCPIP_CORE_LOCKING_INPUT != 1
#warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1, usb handles eth input with own thread
#endif
#if LWIP_TCPIP_CORE_LOCKING !=1
#if LWIP_TCPIP_CORE_LOCKING != 1
#error must set LWIP_TCPIP_CORE_LOCKING to 1
#endif
@@ -50,6 +50,18 @@
// #define CONFIG_USBHOST_PLATFORM_ASIX
// #define CONFIG_USBHOST_PLATFORM_RTL8152
void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf)
{
struct pbuf *q;
uint8_t *buffer;
buffer = buf;
for (q = p; q != NULL; q = q->next) {
usb_memcpy(buffer, q->payload, q->len);
buffer += q->len;
}
}
void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
{
#if LWIP_TCPIP_CORE_LOCKING_INPUT
@@ -65,7 +77,7 @@ void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
#if LWIP_TCPIP_CORE_LOCKING_INPUT
p->payload = buf;
#else
memcpy(p->payload, buf, len);
usb_memcpy(p->payload, buf, len);
#endif
err = netif->input(p, netif);
if (err != ERR_OK) {
@@ -105,7 +117,10 @@ static rt_err_t rt_usbh_cdc_ecm_control(rt_device_t dev, int cmd, void *args)
static rt_err_t rt_usbh_cdc_ecm_eth_tx(rt_device_t dev, struct pbuf *p)
{
int ret = usbh_cdc_ecm_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
ret = usbh_cdc_ecm_eth_output(p->tot_len);
if (ret < 0) {
return -RT_ERROR;
} else {
@@ -188,7 +203,10 @@ static rt_err_t rt_usbh_rndis_control(rt_device_t dev, int cmd, void *args)
static rt_err_t rt_usbh_rndis_eth_tx(rt_device_t dev, struct pbuf *p)
{
int ret = usbh_rndis_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf());
ret = usbh_rndis_eth_output(p->tot_len);
if (ret < 0) {
return -RT_ERROR;
} else {
@@ -254,7 +272,10 @@ static rt_err_t rt_usbh_cdc_ncm_control(rt_device_t dev, int cmd, void *args)
static rt_err_t rt_usbh_cdc_ncm_eth_tx(rt_device_t dev, struct pbuf *p)
{
int ret = usbh_cdc_ncm_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf());
ret = usbh_cdc_ncm_eth_output(p->tot_len);
if (ret < 0) {
return -RT_ERROR;
} else {
@@ -317,7 +338,10 @@ static rt_err_t rt_usbh_asix_control(rt_device_t dev, int cmd, void *args)
static rt_err_t rt_usbh_asix_eth_tx(rt_device_t dev, struct pbuf *p)
{
int ret = usbh_asix_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf());
ret = usbh_asix_eth_output(p->tot_len);
if (ret < 0) {
return -RT_ERROR;
} else {
@@ -380,7 +404,10 @@ static rt_err_t rt_usbh_rtl8152_control(rt_device_t dev, int cmd, void *args)
static rt_err_t rt_usbh_rtl8152_eth_tx(rt_device_t dev, struct pbuf *p)
{
int ret = usbh_rtl8152_eth_output(p->payload, p->tot_len);
int ret;
usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf());
ret = usbh_rtl8152_eth_output(p->tot_len);
if (ret < 0) {
return -RT_ERROR;
} else {