Browse Source

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

sakumisu 1 năm trước cách đây
mục cha
commit
6c92681e48

+ 5 - 4
class/cdc/usbh_cdc_ecm.c

@@ -287,16 +287,17 @@ delete:
     // clang-format on
 }
 
-int usbh_cdc_ecm_eth_output(uint8_t *buf, uint32_t buflen)
+uint8_t *usbh_cdc_ecm_get_eth_txbuf(void)
 {
-    uint8_t *buffer = g_cdc_ecm_tx_buffer;
+    return g_cdc_ecm_tx_buffer;
+}
 
+int usbh_cdc_ecm_eth_output(uint32_t buflen)
+{
     if (g_cdc_ecm_class.connect_status == false) {
         return -USB_ERR_NOTCONN;
     }
 
-    usb_memcpy(buffer, buf, buflen);
-
     USB_LOG_DBG("txlen:%d\r\n", buflen);
 
     usbh_bulk_urb_fill(&g_cdc_ecm_class.bulkout_urb, g_cdc_ecm_class.hport, g_cdc_ecm_class.bulkout, g_cdc_ecm_tx_buffer, buflen, USB_OSAL_WAITING_FOREVER, NULL, NULL);

+ 2 - 1
class/cdc/usbh_cdc_ecm.h

@@ -38,7 +38,8 @@ int usbh_cdc_ecm_get_connect_status(struct usbh_cdc_ecm *cdc_ecm_class);
 void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class);
 void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class);
 
-int usbh_cdc_ecm_eth_output(uint8_t *buf, uint32_t buflen);
+uint8_t *usbh_cdc_ecm_get_eth_txbuf(void);
+int usbh_cdc_ecm_eth_output(uint32_t buflen);
 void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen);
 void usbh_cdc_ecm_rx_thread(void *argument);
 

+ 6 - 5
class/cdc/usbh_cdc_ncm.c

@@ -343,9 +343,13 @@ delete:
     // clang-format on
 }
 
-int usbh_cdc_ncm_eth_output(uint8_t *buf, uint32_t buflen)
+uint8_t *usbh_cdc_ncm_get_eth_txbuf(void)
+{
+    return &g_cdc_ncm_tx_buffer[16];
+}
+
+int usbh_cdc_ncm_eth_output(uint32_t buflen)
 {
-    uint8_t *buffer;
     struct cdc_ncm_ndp16_datagram *ndp16_datagram;
 
     if (g_cdc_ncm_class.connect_status == false) {
@@ -374,9 +378,6 @@ int usbh_cdc_ncm_eth_output(uint8_t *buf, uint32_t buflen)
     ndp16_datagram->wDatagramIndex = 0;
     ndp16_datagram->wDatagramLength = 0;
 
-    buffer = &g_cdc_ncm_tx_buffer[16];
-    usb_memcpy(buffer, buf, buflen);
-
     USB_LOG_DBG("txlen:%d\r\n", nth16->wBlockLength);
 
     usbh_bulk_urb_fill(&g_cdc_ncm_class.bulkout_urb, g_cdc_ncm_class.hport, g_cdc_ncm_class.bulkout, g_cdc_ncm_tx_buffer, nth16->wBlockLength, USB_OSAL_WAITING_FOREVER, NULL, NULL);

+ 2 - 1
class/cdc/usbh_cdc_ncm.h

@@ -42,7 +42,8 @@ int usbh_cdc_ncm_get_connect_status(struct usbh_cdc_ncm *cdc_ncm_class);
 void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class);
 void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class);
 
-int usbh_cdc_ncm_eth_output(uint8_t *buf, uint32_t buflen);
+uint8_t *usbh_cdc_ncm_get_eth_txbuf(void);
+int usbh_cdc_ncm_eth_output(uint32_t buflen);
 void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen);
 void usbh_cdc_ncm_rx_thread(void *argument);
 

+ 6 - 5
class/vendor/net/usbh_asix.c

@@ -751,18 +751,19 @@ delete:
     // clang-format on
 }
 
-int usbh_asix_eth_output(uint8_t *buf, uint32_t buflen)
+uint8_t *usbh_asix_get_eth_txbuf(void)
+{
+    return &g_asix_tx_buffer[4];
+}
+
+int usbh_asix_eth_output(uint32_t buflen)
 {
     uint16_t actual_len;
-    uint8_t *buffer;
 
     if (g_asix_class.connect_status == false) {
         return -USB_ERR_NOTCONN;
     }
 
-    buffer = &g_asix_tx_buffer[4];
-    usb_memcpy(buffer, buf, buflen);
-
     g_asix_tx_buffer[0] = buflen & 0xff;
     g_asix_tx_buffer[1] = (buflen >> 8) & 0xff;
     g_asix_tx_buffer[2] = ~g_asix_tx_buffer[0];

+ 2 - 1
class/vendor/net/usbh_asix.h

@@ -165,7 +165,8 @@ int usbh_asix_get_connect_status(struct usbh_asix *asix_class);
 void usbh_asix_run(struct usbh_asix *asix_class);
 void usbh_asix_stop(struct usbh_asix *asix_class);
 
-int usbh_asix_eth_output(uint8_t *buf, uint32_t buflen);
+uint8_t *usbh_asix_get_eth_txbuf(void);
+int usbh_asix_eth_output(uint32_t buflen);
 void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen);
 void usbh_asix_rx_thread(void *argument);
 

+ 6 - 5
class/vendor/net/usbh_rtl8152.c

@@ -2223,9 +2223,13 @@ delete:
     // clang-format on
 }
 
-int usbh_rtl8152_eth_output(uint8_t *buf, uint32_t buflen)
+uint8_t *usbh_rtl8152_get_eth_txbuf(void)
+{
+    return (g_rtl8152_tx_buffer + sizeof(struct tx_desc));
+}
+
+int usbh_rtl8152_eth_output(uint32_t buflen)
 {
-    uint8_t *buffer;
     struct tx_desc *tx_desc;
 
     if (g_rtl8152_class.connect_status == false) {
@@ -2236,9 +2240,6 @@ int usbh_rtl8152_eth_output(uint8_t *buf, uint32_t buflen)
     tx_desc->opts1 = buflen | TX_FS | TX_LS;
     tx_desc->opts2 = 0;
 
-    buffer = g_rtl8152_tx_buffer + sizeof(struct tx_desc);
-    usb_memcpy(buffer, buf, buflen);
-
     USB_LOG_DBG("txlen:%d\r\n", buflen + sizeof(struct tx_desc));
 
     usbh_bulk_urb_fill(&g_rtl8152_class.bulkout_urb, g_rtl8152_class.hport, g_rtl8152_class.bulkout, g_rtl8152_tx_buffer, buflen + sizeof(struct tx_desc), USB_OSAL_WAITING_FOREVER, NULL, NULL);

+ 2 - 1
class/vendor/net/usbh_rtl8152.h

@@ -56,7 +56,8 @@ int usbh_rtl8152_get_connect_status(struct usbh_rtl8152 *rtl8152_class);
 void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class);
 void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class);
 
-int usbh_rtl8152_eth_output(uint8_t *buf, uint32_t buflen);
+uint8_t *usbh_rtl8152_get_eth_txbuf(void);
+int usbh_rtl8152_eth_output(uint32_t buflen);
 void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen);
 void usbh_rtl8152_rx_thread(void *argument);
 

+ 6 - 5
class/wireless/usbh_rndis.c

@@ -545,9 +545,13 @@ delete:
     // clang-format on
 }
 
-int usbh_rndis_eth_output(uint8_t *buf, uint32_t buflen)
+uint8_t *usbh_rndis_get_eth_txbuf(void)
+{
+    return (g_rndis_tx_buffer + sizeof(rndis_data_packet_t));
+}
+
+int usbh_rndis_eth_output(uint32_t buflen)
 {
-    uint8_t *buffer;
     rndis_data_packet_t *hdr;
     uint32_t len;
 
@@ -563,9 +567,6 @@ int usbh_rndis_eth_output(uint8_t *buf, uint32_t buflen)
     hdr->DataOffset = sizeof(rndis_data_packet_t) - sizeof(rndis_generic_msg_t);
     hdr->DataLength = buflen;
 
-    buffer = (uint8_t *)(g_rndis_tx_buffer + sizeof(rndis_data_packet_t));
-    usb_memcpy(buffer, buf, buflen);
-
     len = hdr->MessageLength;
     /* if message length is the multiple of wMaxPacketSize, we should add a short packet to tell device transfer is over. */
     if (!(len % g_rndis_class.bulkout->wMaxPacketSize)) {

+ 2 - 1
class/wireless/usbh_rndis.h

@@ -43,7 +43,8 @@ int usbh_rndis_keepalive(struct usbh_rndis *rndis_class);
 void usbh_rndis_run(struct usbh_rndis *rndis_class);
 void usbh_rndis_stop(struct usbh_rndis *rndis_class);
 
-int usbh_rndis_eth_output(uint8_t *buf, uint32_t buflen);
+uint8_t *usbh_rndis_get_eth_txbuf(void);
+int usbh_rndis_eth_output(uint32_t buflen);
 void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen);
 void usbh_rndis_rx_thread(void *argument);
 

+ 35 - 8
platform/none/usbh_lwip.c

@@ -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 {

+ 18 - 9
platform/nuttx/usbh_net.c

@@ -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)

+ 35 - 8
platform/rtthread/usbh_lwip.c

@@ -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 {