Skip to contentSkip to conversation

DM conversion of usb ether gadget

PATCH Superseded Mugunthan V N 3 participants 16 messages May 10, 2016 – May 19, 2016 USB

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover DM conversion of usb ether gadget

This patch series adopts driver model for usb ether gadget driver. This series is tested with MUSB driver model conversion on AM335x GP evm and AM335x BBB (logs [1]).

Also pushed a branch for testing [2]

Changes from initial version:
* Separated out the usb gadget driver patches from earlier musb series [3] for testing and submitting of dwc3 dm musb patches.

[1] - http://pastebin.ubuntu.com/16345030/
[2] - git://git.ti.com/~mugunthanvnm/ti-u-boot/mugunth-ti-u-boot.git dm-musb-v2
[3] - http://lists.denx.de/pipermail/u-boot/2016-February/246827.html

Note:
~~~~~
The following checkpatch warning can be ignored as this has to be fixed all over the file which should be a separate patch.
CHECK: Avoid CamelCase: <configNr>
#281: FILE: drivers/usb/gadget/rndis.c:1159:
+int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,

total: 0 errors, 0 warnings, 1 checks, 303 lines checked

NOTE: Ignored message types: COMPLEX_MACRO CONSIDER_KSTRTO MINMAX MULTISTATEMENT_MACRO_USE_DO_WHILE NETWORKING_BLOCK_COMMENT_STYLE PREFER_ETHER_ADDR_COPY USLEEP_RANGE

patches/usb_gadget/v2.00/0006-drivers-usb-gadget-ether-rndis-convert-driver-to-ado.patch has style problems, please review.

Mugunthan V N (6):
drivers: usb: gadget: ether: adopt to usb driver model
drivers: usb: gadget: ether: access network_started using local
variable
drivers: usb: gadget: ether: consolidate global devices to single
struct
drivers: usb: gadget: ether: use net device priv to pass usb ether
priv
drivers: usb: gadget: ether: prepare driver for driver model migration
drivers: usb: gadget: ether/rndis: convert driver to adopt device
driver model

 drivers/usb/gadget/ether.c | 314 ++++++++++++++++++++++++++++++++++++---------
 drivers/usb/gadget/rndis.c |  13 +-
 drivers/usb/gadget/rndis.h |  19 ++-
 include/net.h              |   7 +
 4 files changed, 288 insertions(+), 65 deletions(-)
Signature · 1 line
2.8.2.372.g63a3502
#1 lore raw

PATCH v2 1/6 drivers: usb: gadget: ether: adopt to usb driver model

Convert usb ether gadget to adopt usb driver model

---
 drivers/usb/gadget/ether.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
drivers/usb/gadget/ether.c +36 -0
index 9b06f02..ae5ffcd 100644
@@ -24,6 +24,10 @@
 #include "gadget_chips.h"
 #include "rndis.h"
 
+#include <dm.h>
+#include <dm/uclass-internal.h>
+#include <dm/device-internal.h>
+
 #define USB_NET_NAME "usb_ether"
 
 #define atomic_read
@@ -101,6 +105,9 @@ struct eth_dev {
 	struct usb_gadget	*gadget;
 	struct usb_request	*req;		/* for control responses */
 	struct usb_request	*stat_req;	/* for cdc & rndis status */
+#ifdef CONFIG_DM_USB
+	struct udevice		*usb_udev;
+#endif
 
 	u8			config;
 	struct usb_ep		*in_ep, *out_ep, *status_ep;
@@ -2303,6 +2310,24 @@ fail:
 
 /*-------------------------------------------------------------------------*/
 
+#ifdef CONFIG_DM_USB
+int dm_usb_init(struct eth_dev *e_dev)
+{
+	struct udevice *dev = NULL;
+	int ret;
+
+	ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &dev);
+	if (!dev || ret) {
+		error("No USB device found\n");
+		return -ENODEV;
+	}
+
+	e_dev->usb_udev = dev;
+
+	return ret;
+}
+#endif
+
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
 	struct eth_dev *dev = &l_ethdev;
@@ -2315,7 +2340,14 @@ static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 		goto fail;
 	}
 
+#ifdef CONFIG_DM_USB
+	if (dm_usb_init(dev)) {
+		error("USB ether not found\n");
+		return -ENODEV;
+	}
+#else
 	board_usb_init(0, USB_INIT_DEVICE);
+#endif
 
 	/* Configure default mac-addresses for the USB ethernet device */
 #ifdef CONFIG_USBNET_DEV_ADDR
@@ -2497,7 +2529,11 @@ void usb_eth_halt(struct eth_device *netdev)
 	}
 
 	usb_gadget_unregister_driver(&eth_driver);
+#ifdef CONFIG_DM_USB
+	device_remove(dev->usb_udev);
+#else
 	board_usb_cleanup(0, USB_INIT_DEVICE);
+#endif
 }
 
 static struct usb_gadget_driver eth_driver = {
Signature · 1 line
2.8.2.372.g63a3502
#2 lore raw

PATCH v2 1/6 drivers: usb: gadget: ether: adopt to usb driver model

Hi,

Quoted 6 lines · Mugunthan V N

Convert usb ether gadget to adopt usb driver model

---
 drivers/usb/gadget/ether.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

This seems a little messy but I understand the need to fit within the existing framework.

Regards,
Simon

#3 lore raw

PATCH v2 2/6 drivers: usb: gadget: ether: access network_started using local variable

In reply to Mugunthan V N

network_started of struct eth_dev can be accessed using local variable dev and no reason to access it with the global struct.

---
 drivers/usb/gadget/ether.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/usb/gadget/ether.c +5 -5
index ae5ffcd..89e5ab8 100644
@@ -1142,7 +1142,7 @@ static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
 			event->bNotificationType, value);
 		if (event->bNotificationType ==
 				USB_CDC_NOTIFY_SPEED_CHANGE) {
-			l_ethdev.network_started = 1;
+			dev->network_started = 1;
 			printf("USB network up!\n");
 		}
 	}
@@ -1330,7 +1330,7 @@ eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
 			 * that network is working. So we signalize it
 			 * here.
 			 */
-			l_ethdev.network_started = 1;
+			dev->network_started = 1;
 			debug("USB network up!\n");
 			goto done_set_intf;
 		}
@@ -1830,10 +1830,10 @@ static void rndis_control_ack_complete(struct usb_ep *ep,
 		debug("rndis control ack complete --> %d, %d/%d\n",
 			req->status, req->actual, req->length);
 
-	if (!l_ethdev.network_started) {
+	if (!dev->network_started) {
 		if (rndis_get_state(dev->rndis_config)
 				== RNDIS_DATA_INITIALIZED) {
-			l_ethdev.network_started = 1;
+			dev->network_started = 1;
 			printf("USB RNDIS network up!\n");
 		}
 	}
@@ -2389,7 +2389,7 @@ static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 		timeout = simple_strtoul(getenv("cdc_connect_timeout"),
 						NULL, 10) * CONFIG_SYS_HZ;
 	ts = get_timer(0);
-	while (!l_ethdev.network_started) {
+	while (!dev->network_started) {
 		/* Handle control-c and timeouts */
 		if (ctrlc() || (get_timer(ts) > timeout)) {
 			error("The remote end did not respond in time.");
Signature · 1 line
2.8.2.372.g63a3502
#4 lore raw

PATCH v2 2/6 drivers: usb: gadget: ether: access network_started using local variable

Quoted 7 lines · Mugunthan V N

network_started of struct eth_dev can be accessed using local variable dev and no reason to access it with the global struct.

---
 drivers/usb/gadget/ether.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
#5 lore raw

PATCH v2 3/6 drivers: usb: gadget: ether: consolidate global devices to single struct

In reply to Mugunthan V N

Consolidate the net device, usb eth device and gadget device struct to single struct and a single global variable so that the same can be passed as priv of ethernet driver.

---
 drivers/usb/gadget/ether.c | 53 +++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 27 deletions(-)
drivers/usb/gadget/ether.c +26 -27
index 89e5ab8..b40d99a 100644
@@ -141,9 +141,14 @@ struct eth_dev {
  */
 
 /*-------------------------------------------------------------------------*/
-static struct eth_dev l_ethdev;
-static struct eth_device l_netdev;
-static struct usb_gadget_driver eth_driver;
+struct ether_priv {
+	struct eth_dev ethdev;
+	struct eth_device netdev;
+	struct usb_gadget_driver eth_driver;
+};
+
+struct ether_priv eth_priv;
+struct ether_priv *l_priv = &eth_priv;
 
 /*-------------------------------------------------------------------------*/
 
@@ -1848,7 +1853,7 @@ static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
 
 static int rndis_control_ack(struct eth_device *net)
 {
-	struct eth_dev		*dev = &l_ethdev;
+	struct eth_dev		*dev = &l_priv->ethdev;
 	int                     length;
 	struct usb_request      *resp = dev->stat_req;
 
@@ -1989,7 +1994,7 @@ static int get_ether_addr(const char *str, u8 *dev_addr)
 
 static int eth_bind(struct usb_gadget *gadget)
 {
-	struct eth_dev		*dev = &l_ethdev;
+	struct eth_dev		*dev = &l_priv->ethdev;
 	u8			cdc = 1, zlp = 1, rndis = 1;
 	struct usb_ep		*in_ep, *out_ep, *status_ep = NULL;
 	int			status = -ENOMEM;
@@ -2182,7 +2187,7 @@ autoconf_fail:
 
 
 	/* network device setup */
-	dev->net = &l_netdev;
+	dev->net = &l_priv->netdev;
 
 	dev->cdc = cdc;
 	dev->zlp = zlp;
@@ -2330,7 +2335,7 @@ int dm_usb_init(struct eth_dev *e_dev)
 
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
-	struct eth_dev *dev = &l_ethdev;
+	struct eth_dev *dev = &l_priv->ethdev;
 	struct usb_gadget *gadget;
 	unsigned long ts;
 	unsigned long timeout = USB_CONNECT_TIMEOUT;
@@ -2374,7 +2379,15 @@ static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 		goto fail;
 	}
 
-	if (usb_gadget_register_driver(&eth_driver) < 0)
+	l_priv->eth_driver.speed	= DEVSPEED;
+	l_priv->eth_driver.bind		= eth_bind;
+	l_priv->eth_driver.unbind	= eth_unbind;
+	l_priv->eth_driver.setup	= eth_setup;
+	l_priv->eth_driver.reset	= eth_disconnect;
+	l_priv->eth_driver.disconnect	= eth_disconnect;
+	l_priv->eth_driver.suspend	= eth_suspend;
+	l_priv->eth_driver.resume	= eth_resume;
+	if (usb_gadget_register_driver(&l_priv->eth_driver) < 0)
 		goto fail;
 
 	dev->network_started = 0;
@@ -2409,7 +2422,7 @@ static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
 {
 	int			retval;
 	void			*rndis_pkt = NULL;
-	struct eth_dev		*dev = &l_ethdev;
+	struct eth_dev		*dev = &l_priv->ethdev;
 	struct usb_request	*req = dev->tx_req;
 	unsigned long ts;
 	unsigned long timeout = USB_CONNECT_TIMEOUT;
@@ -2476,7 +2489,7 @@ drop:
 
 static int usb_eth_recv(struct eth_device *netdev)
 {
-	struct eth_dev *dev = &l_ethdev;
+	struct eth_dev *dev = &l_priv->ethdev;
 
 	usb_gadget_handle_interrupts(0);
 
@@ -2496,7 +2509,7 @@ static int usb_eth_recv(struct eth_device *netdev)
 
 void usb_eth_halt(struct eth_device *netdev)
 {
-	struct eth_dev *dev = &l_ethdev;
+	struct eth_dev *dev = &l_priv->ethdev;
 
 	if (!netdev) {
 		error("received NULL ptr");
@@ -2528,7 +2541,7 @@ void usb_eth_halt(struct eth_device *netdev)
 		dev->network_started = 0;
 	}
 
-	usb_gadget_unregister_driver(&eth_driver);
+	usb_gadget_unregister_driver(&l_priv->eth_driver);
 #ifdef CONFIG_DM_USB
 	device_remove(dev->usb_udev);
 #else
@@ -2536,23 +2549,9 @@ void usb_eth_halt(struct eth_device *netdev)
 #endif
 }
 
-static struct usb_gadget_driver eth_driver = {
-	.speed		= DEVSPEED,
-
-	.bind		= eth_bind,
-	.unbind		= eth_unbind,
-
-	.setup		= eth_setup,
-	.reset		= eth_disconnect,
-	.disconnect	= eth_disconnect,
-
-	.suspend	= eth_suspend,
-	.resume		= eth_resume,
-};
-
 int usb_eth_initialize(bd_t *bi)
 {
-	struct eth_device *netdev = &l_netdev;
+	struct eth_device *netdev = &l_priv->netdev;
 
 	strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
 
Signature · 1 line
2.8.2.372.g63a3502
#6 lore raw

PATCH v2 3/6 drivers: usb: gadget: ether: consolidate global devices to single struct

Quoted 8 lines · Mugunthan V N

Consolidate the net device, usb eth device and gadget device struct to single struct and a single global variable so that the same can be passed as priv of ethernet driver.

---
 drivers/usb/gadget/ether.c | 53 +++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 27 deletions(-)
#7 lore raw

PATCH v2 4/6 drivers: usb: gadget: ether: use net device priv to pass usb ether priv

In reply to Mugunthan V N

Use net device priv to pass usb ether priv and use it in net device ops callback.

---
 drivers/usb/gadget/ether.c | 46 +++++++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 25 deletions(-)
drivers/usb/gadget/ether.c +21 -25
index b40d99a..47071c3 100644
@@ -1853,7 +1853,8 @@ static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
 
 static int rndis_control_ack(struct eth_device *net)
 {
-	struct eth_dev		*dev = &l_priv->ethdev;
+	struct ether_priv	*priv = (struct ether_priv *)net->priv;
+	struct eth_dev		*dev = &priv->ethdev;
 	int                     length;
 	struct usb_request      *resp = dev->stat_req;
 
@@ -2335,16 +2336,12 @@ int dm_usb_init(struct eth_dev *e_dev)
 
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
-	struct eth_dev *dev = &l_priv->ethdev;
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+	struct eth_dev *dev = &priv->ethdev;
 	struct usb_gadget *gadget;
 	unsigned long ts;
 	unsigned long timeout = USB_CONNECT_TIMEOUT;
 
-	if (!netdev) {
-		error("received NULL ptr");
-		goto fail;
-	}
-
 #ifdef CONFIG_DM_USB
 	if (dm_usb_init(dev)) {
 		error("USB ether not found\n");
@@ -2379,15 +2376,15 @@ static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 		goto fail;
 	}
 
-	l_priv->eth_driver.speed	= DEVSPEED;
-	l_priv->eth_driver.bind		= eth_bind;
-	l_priv->eth_driver.unbind	= eth_unbind;
-	l_priv->eth_driver.setup	= eth_setup;
-	l_priv->eth_driver.reset	= eth_disconnect;
-	l_priv->eth_driver.disconnect	= eth_disconnect;
-	l_priv->eth_driver.suspend	= eth_suspend;
-	l_priv->eth_driver.resume	= eth_resume;
-	if (usb_gadget_register_driver(&l_priv->eth_driver) < 0)
+	priv->eth_driver.speed		= DEVSPEED;
+	priv->eth_driver.bind		= eth_bind;
+	priv->eth_driver.unbind		= eth_unbind;
+	priv->eth_driver.setup		= eth_setup;
+	priv->eth_driver.reset		= eth_disconnect;
+	priv->eth_driver.disconnect	= eth_disconnect;
+	priv->eth_driver.suspend	= eth_suspend;
+	priv->eth_driver.resume		= eth_resume;
+	if (usb_gadget_register_driver(&priv->eth_driver) < 0)
 		goto fail;
 
 	dev->network_started = 0;
@@ -2422,7 +2419,8 @@ static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
 {
 	int			retval;
 	void			*rndis_pkt = NULL;
-	struct eth_dev		*dev = &l_priv->ethdev;
+	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
+	struct eth_dev		*dev = &priv->ethdev;
 	struct usb_request	*req = dev->tx_req;
 	unsigned long ts;
 	unsigned long timeout = USB_CONNECT_TIMEOUT;
@@ -2489,7 +2487,8 @@ drop:
 
 static int usb_eth_recv(struct eth_device *netdev)
 {
-	struct eth_dev *dev = &l_priv->ethdev;
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+	struct eth_dev *dev = &priv->ethdev;
 
 	usb_gadget_handle_interrupts(0);
 
@@ -2509,12 +2508,8 @@ static int usb_eth_recv(struct eth_device *netdev)
 
 void usb_eth_halt(struct eth_device *netdev)
 {
-	struct eth_dev *dev = &l_priv->ethdev;
-
-	if (!netdev) {
-		error("received NULL ptr");
-		return;
-	}
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+	struct eth_dev *dev = &priv->ethdev;
 
 	/* If the gadget not registered, simple return */
 	if (!dev->gadget)
@@ -2541,7 +2536,7 @@ void usb_eth_halt(struct eth_device *netdev)
 		dev->network_started = 0;
 	}
 
-	usb_gadget_unregister_driver(&l_priv->eth_driver);
+	usb_gadget_unregister_driver(&priv->eth_driver);
 #ifdef CONFIG_DM_USB
 	device_remove(dev->usb_udev);
 #else
@@ -2559,6 +2554,7 @@ int usb_eth_initialize(bd_t *bi)
 	netdev->send = usb_eth_send;
 	netdev->recv = usb_eth_recv;
 	netdev->halt = usb_eth_halt;
+	netdev->priv = l_priv;
 
 #ifdef CONFIG_MCAST_TFTP
   #error not supported
Signature · 1 line
2.8.2.372.g63a3502
#8 lore raw

PATCH v2 4/6 drivers: usb: gadget: ether: use net device priv to pass usb ether priv

Quoted 7 lines · Mugunthan V N

Use net device priv to pass usb ether priv and use it in net device ops callback.

---
 drivers/usb/gadget/ether.c | 46 +++++++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 25 deletions(-)
#9 lore raw

PATCH v2 5/6 drivers: usb: gadget: ether: prepare driver for driver model migration

In reply to Mugunthan V N

prepare driver for driver model migration

---
 drivers/usb/gadget/ether.c | 72 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 51 insertions(+), 21 deletions(-)
drivers/usb/gadget/ether.c +51 -21
index 47071c3..2f70ebf 100644
@@ -2334,9 +2334,8 @@ int dm_usb_init(struct eth_dev *e_dev)
 }
 #endif
 
-static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
+static int _usb_eth_init(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev *dev = &priv->ethdev;
 	struct usb_gadget *gadget;
 	unsigned long ts;
@@ -2415,11 +2414,10 @@ fail:
 	return -1;
 }
 
-static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
+static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
 {
 	int			retval;
 	void			*rndis_pkt = NULL;
-	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev		*dev = &priv->ethdev;
 	struct usb_request	*req = dev->tx_req;
 	unsigned long ts;
@@ -2485,30 +2483,15 @@ drop:
 	return -ENOMEM;
 }
 
-static int usb_eth_recv(struct eth_device *netdev)
+static int _usb_eth_recv(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
-	struct eth_dev *dev = &priv->ethdev;
-
 	usb_gadget_handle_interrupts(0);
 
-	if (packet_received) {
-		debug("%s: packet received\n", __func__);
-		if (dev->rx_req) {
-			net_process_received_packet(net_rx_packets[0],
-						    dev->rx_req->length);
-			packet_received = 0;
-
-			rx_submit(dev, dev->rx_req, 0);
-		} else
-			error("dev->rx_req invalid");
-	}
 	return 0;
 }
 
-void usb_eth_halt(struct eth_device *netdev)
+void _usb_eth_halt(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev *dev = &priv->ethdev;
 
 	/* If the gadget not registered, simple return */
@@ -2544,6 +2527,53 @@ void usb_eth_halt(struct eth_device *netdev)
 #endif
 }
 
+static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
+{
+	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct eth_device *netdev)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+	struct eth_dev *dev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {
+		error("error packet receive\n");
+		return ret;
+	}
+
+	if (packet_received) {
+		if (dev->rx_req) {
+			net_process_received_packet(net_rx_packets[0],
+						    dev->rx_req->length);
+		} else {
+			error("dev->rx_req invalid");
+		}
+		packet_received = 0;
+		rx_submit(dev, dev->rx_req, 0);
+	}
+
+	return 0;
+}
+
+void usb_eth_halt(struct eth_device *netdev)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+
+	_usb_eth_halt(priv);
+}
+
 int usb_eth_initialize(bd_t *bi)
 {
 	struct eth_device *netdev = &l_priv->netdev;
Signature · 1 line
2.8.2.372.g63a3502
#10 lore raw

PATCH v2 5/6 drivers: usb: gadget: ether: prepare driver for driver model migration

Quoted 89 lines · Mugunthan V N

prepare driver for driver model migration

---
 drivers/usb/gadget/ether.c | 72 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 51 insertions(+), 21 deletions(-)
drivers/usb/gadget/ether.c +21 -21
index 47071c3..2f70ebf 100644
@@ -2334,9 +2334,8 @@ int dm_usb_init(struct eth_dev *e_dev)
 }
 #endif
 
-static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
+static int _usb_eth_init(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev *dev = &priv->ethdev;
 	struct usb_gadget *gadget;
 	unsigned long ts;
@@ -2415,11 +2414,10 @@ fail:
 	return -1;
 }
 
-static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
+static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
 {
 	int			retval;
 	void			*rndis_pkt = NULL;
-	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev		*dev = &priv->ethdev;
 	struct usb_request	*req = dev->tx_req;
 	unsigned long ts;
@@ -2485,30 +2483,15 @@ drop:
 	return -ENOMEM;
 }
 
-static int usb_eth_recv(struct eth_device *netdev)
+static int _usb_eth_recv(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
-	struct eth_dev *dev = &priv->ethdev;
-
 	usb_gadget_handle_interrupts(0);
 
-	if (packet_received) {
-		debug("%s: packet received\n", __func__);
-		if (dev->rx_req) {
-			net_process_received_packet(net_rx_packets[0],
-						    dev->rx_req->length);
-			packet_received = 0;
-
-			rx_submit(dev, dev->rx_req, 0);
-		} else
-			error("dev->rx_req invalid");
-	}
 	return 0;
 }
 
-void usb_eth_halt(struct eth_device *netdev)
+void _usb_eth_halt(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev *dev = &priv->ethdev;
 
 	/* If the gadget not registered, simple return */
@@ -2544,6 +2527,53 @@ void usb_eth_halt(struct eth_device *netdev)
 #endif
 }
 
+static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
+{
+	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct eth_device *netdev)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;

drivers/usb/gadget/ether.c void usb_eth_halt(struct eth_device *netdev) · line 2556

Quoted 10 lines
drivers/usb/gadget/ether.c +10 -0
+	struct eth_dev *dev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {
+		error("error packet receive\n");
+		return ret;
+	}
+
+	if (packet_received) {

if (!packet_received)
return 0;
... the rest ...

Quoted 24 lines
drivers/usb/gadget/ether.c +20 -0
+		if (dev->rx_req) {
+			net_process_received_packet(net_rx_packets[0],
+						    dev->rx_req->length);
+		} else {
+			error("dev->rx_req invalid");
+		}
+		packet_received = 0;
+		rx_submit(dev, dev->rx_req, 0);
+	}
+
+	return 0;
+}
+
+void usb_eth_halt(struct eth_device *netdev)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+
+	_usb_eth_halt(priv);
+}
+
 int usb_eth_initialize(bd_t *bi)
 {
 	struct eth_device *netdev = &l_priv->netdev;
Signature · 2 lines
Best regards,
Marek Vasut
#11 lore raw

PATCH v2 5/6 drivers: usb: gadget: ether: prepare driver for driver model migration

Quoted 96 lines · Marek Vasut
Quoted 95 lines · Mugunthan V N

prepare driver for driver model migration

---
 drivers/usb/gadget/ether.c | 72 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 51 insertions(+), 21 deletions(-)
drivers/usb/gadget/ether.c +27 -21
index 47071c3..2f70ebf 100644
@@ -2334,9 +2334,8 @@ int dm_usb_init(struct eth_dev *e_dev)
 }
 #endif
 
-static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
+static int _usb_eth_init(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev *dev = &priv->ethdev;
 	struct usb_gadget *gadget;
 	unsigned long ts;
@@ -2415,11 +2414,10 @@ fail:
 	return -1;
 }
 
-static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
+static int _usb_eth_send(struct ether_priv *priv, void *packet, int length)
 {
 	int			retval;
 	void			*rndis_pkt = NULL;
-	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev		*dev = &priv->ethdev;
 	struct usb_request	*req = dev->tx_req;
 	unsigned long ts;
@@ -2485,30 +2483,15 @@ drop:
 	return -ENOMEM;
 }
 
-static int usb_eth_recv(struct eth_device *netdev)
+static int _usb_eth_recv(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
-	struct eth_dev *dev = &priv->ethdev;
-
 	usb_gadget_handle_interrupts(0);
 
-	if (packet_received) {
-		debug("%s: packet received\n", __func__);
-		if (dev->rx_req) {
-			net_process_received_packet(net_rx_packets[0],
-						    dev->rx_req->length);
-			packet_received = 0;
-
-			rx_submit(dev, dev->rx_req, 0);
-		} else
-			error("dev->rx_req invalid");
-	}
 	return 0;
 }
 
-void usb_eth_halt(struct eth_device *netdev)
+void _usb_eth_halt(struct ether_priv *priv)
 {
-	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
 	struct eth_dev *dev = &priv->ethdev;
 
 	/* If the gadget not registered, simple return */
@@ -2544,6 +2527,53 @@ void usb_eth_halt(struct eth_device *netdev)
 #endif
 }
 
+static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct eth_device *netdev, void *packet, int length)
+{
+	struct ether_priv	*priv = (struct ether_priv *)netdev->priv;
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct eth_device *netdev)
+{
+	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
+	struct eth_dev *dev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {
+		error("error packet receive\n");
Quoted 8 lines

drivers/usb/gadget/ether.c void usb_eth_halt(struct eth_device *netdev) · line 2556

Quoted 4 lines
drivers/usb/gadget/ether.c +4 -0
+		return ret;
+	}
+
+	if (packet_received) {

if (!packet_received)
return 0;
... the rest ...

Will fix in next version.

Regards
Mugunthan V N

#12 lore raw

PATCH v2 6/6 drivers: usb: gadget: ether/rndis: convert driver to adopt device driver model

In reply to Mugunthan V N

Adopt usb ether gadget and rndis driver to adopt driver model

---
 drivers/usb/gadget/ether.c | 153 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/usb/gadget/rndis.c |  13 +++-
 drivers/usb/gadget/rndis.h |  19 ++++--
 include/net.h              |   7 +++
 4 files changed, 177 insertions(+), 15 deletions(-)
drivers/usb/gadget/ether.c +144 -9
index 2f70ebf..c436f75 100644
@@ -25,6 +25,7 @@
 #include "rndis.h"
 
 #include <dm.h>
+#include <dm/lists.h>
 #include <dm/uclass-internal.h>
 #include <dm/device-internal.h>
 
@@ -116,7 +117,11 @@ struct eth_dev {
 
 	struct usb_request	*tx_req, *rx_req;
 
+#ifndef CONFIG_DM_ETH
 	struct eth_device	*net;
+#else
+	struct udevice		*net;
+#endif
 	struct net_device_stats	stats;
 	unsigned int		tx_qlen;
 
@@ -143,7 +148,11 @@ struct eth_dev {
 /*-------------------------------------------------------------------------*/
 struct ether_priv {
 	struct eth_dev ethdev;
+#ifndef CONFIG_DM_ETH
 	struct eth_device netdev;
+#else
+	struct udevice *netdev;
+#endif
 	struct usb_gadget_driver eth_driver;
 };
 
@@ -1851,7 +1860,11 @@ static void rndis_control_ack_complete(struct usb_ep *ep,
 
 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
 
+#ifndef CONFIG_DM_ETH
 static int rndis_control_ack(struct eth_device *net)
+#else
+static int rndis_control_ack(struct udevice *net)
+#endif
 {
 	struct ether_priv	*priv = (struct ether_priv *)net->priv;
 	struct eth_dev		*dev = &priv->ethdev;
@@ -2001,6 +2014,9 @@ static int eth_bind(struct usb_gadget *gadget)
 	int			status = -ENOMEM;
 	int			gcnum;
 	u8			tmp[7];
+#ifdef CONFIG_DM_ETH
+	struct eth_pdata	*pdata = dev_get_platdata(l_priv->netdev);
+#endif
 
 	/* these flags are only ever cleared; compiler take note */
 #ifndef	CONFIG_USB_ETH_CDC
@@ -2188,7 +2204,11 @@ autoconf_fail:
 
 
 	/* network device setup */
+#ifndef CONFIG_DM_ETH
 	dev->net = &l_priv->netdev;
+#else
+	dev->net = l_priv->netdev;
+#endif
 
 	dev->cdc = cdc;
 	dev->zlp = zlp;
@@ -2197,6 +2217,7 @@ autoconf_fail:
 	dev->out_ep = out_ep;
 	dev->status_ep = status_ep;
 
+	memset(tmp, 0, sizeof(tmp));
 	/*
 	 * Module params for these addresses should come from ID proms.
 	 * The host side address is used with CDC and RNDIS, and commonly
@@ -2204,10 +2225,13 @@ autoconf_fail:
 	 * host side code for the SAFE thing cares -- its original BLAN
 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
 	 */
+#ifndef CONFIG_DM_ETH
 	get_ether_addr(dev_addr, dev->net->enetaddr);
-
-	memset(tmp, 0, sizeof(tmp));
 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
+#else
+	get_ether_addr(dev_addr, pdata->enetaddr);
+	memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
+#endif
 
 	get_ether_addr(host_addr, dev->host_mac);
 
@@ -2268,10 +2292,11 @@ autoconf_fail:
 		status_ep ? " STATUS " : "",
 		status_ep ? status_ep->name : ""
 		);
-	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
-		dev->net->enetaddr[0], dev->net->enetaddr[1],
-		dev->net->enetaddr[2], dev->net->enetaddr[3],
-		dev->net->enetaddr[4], dev->net->enetaddr[5]);
+#ifndef CONFIG_DM_ETH
+	printf("MAC %pM\n", dev->net->enetaddr);
+#else
+	printf("MAC %pM\n", pdata->enetaddr);
+#endif
 
 	if (cdc || rndis)
 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -2520,13 +2545,12 @@ void _usb_eth_halt(struct ether_priv *priv)
 	}
 
 	usb_gadget_unregister_driver(&priv->eth_driver);
-#ifdef CONFIG_DM_USB
-	device_remove(dev->usb_udev);
-#else
+#ifndef CONFIG_DM_USB
 	board_usb_cleanup(0, USB_INIT_DEVICE);
 #endif
 }
 
+#ifndef CONFIG_DM_ETH
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
@@ -2592,3 +2616,114 @@ int usb_eth_initialize(bd_t *bi)
 	eth_register(netdev);
 	return 0;
 }
+#else
+static int usb_eth_start(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct udevice *dev, void *packet, int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {
+		error("error packet receive\n");
+		return ret;
+	}
+
+	if (packet_received) {
+		if (ethdev->rx_req) {
+			*packetp = (uchar *)net_rx_packets[0];
+			return ethdev->rx_req->length;
+		} else {
+			error("dev->rx_req invalid");
+			return -EFAULT;
+		}
+	}
+
+	return -EAGAIN;
+}
+
+static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
+				   int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+
+	packet_received = 0;
+
+	return rx_submit(ethdev, ethdev->rx_req, 0);
+}
+
+static void usb_eth_stop(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	_usb_eth_halt(priv);
+}
+
+static int usb_eth_probe(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_pdata *pdata = dev_get_platdata(dev);
+
+	priv->netdev = dev;
+	l_priv = priv;
+
+	get_ether_addr("de:ad:be:ef:00:01", pdata->enetaddr);
+	eth_setenv_enetaddr("usbnet_devaddr", pdata->enetaddr);
+
+	return 0;
+}
+
+static const struct eth_ops usb_eth_ops = {
+	.start		= usb_eth_start,
+	.send		= usb_eth_send,
+	.recv		= usb_eth_recv,
+	.free_pkt	= usb_eth_free_pkt,
+	.stop		= usb_eth_stop,
+};
+
+int usb_ether_init(void)
+{
+	struct udevice *dev;
+	struct udevice *usb_dev;
+	int ret;
+
+	ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
+	if (!usb_dev || ret) {
+		error("No USB device found\n");
+		return ret;
+	}
+
+	ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
+	if (!dev || ret) {
+		error("usb - not able to bind usb_ether device\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+U_BOOT_DRIVER(eth_usb) = {
+	.name	= "usb_ether",
+	.id	= UCLASS_ETH,
+	.probe	= usb_eth_probe,
+	.ops	= &usb_eth_ops,
+	.priv_auto_alloc_size = sizeof(struct ether_priv),
+	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
+	.flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
+#endif /* CONFIG_DM_ETH */
drivers/usb/gadget/rndis.c +11 -2
index 48463db..e6029ec 100644
@@ -1123,7 +1123,11 @@ int rndis_msg_parser(u8 configNr, u8 *buf)
 	return -ENOTSUPP;
 }
 
+#ifndef CONFIG_DM_ETH
 int rndis_register(int (*rndis_control_ack)(struct eth_device *))
+#else
+int rndis_register(int (*rndis_control_ack)(struct udevice *))
+#endif
 {
 	u8 i;
 
@@ -1151,8 +1155,13 @@ void rndis_deregister(int configNr)
 	return;
 }
 
-int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
-			struct net_device_stats *stats,	u16 *cdc_filter)
+#ifndef CONFIG_DM_ETH
+int  rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter)
+#else
+int  rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter)
+#endif
 {
 	debug("%s: configNr = %d\n", __func__, configNr);
 	if (!dev || !stats)
drivers/usb/gadget/rndis.h +15 -4
index 7a389a5..084af85 100644
@@ -222,23 +222,34 @@ typedef struct rndis_params {
 
 	const u8		*host_mac;
 	u16			*filter;
-	struct eth_device	*dev;
 	struct net_device_stats *stats;
 	int			mtu;
 
 	u32			vendorID;
 	const char		*vendorDescr;
-	int			(*ack)(struct eth_device *);
+#ifndef CONFIG_DM_ETH
+	struct eth_device	*dev;
+	int (*ack)(struct eth_device *);
+#else
+	struct udevice		*dev;
+	int (*ack)(struct udevice *);
+#endif
 	struct list_head	resp_queue;
 } rndis_params;
 
 /* RNDIS Message parser and other useless functions */
 int  rndis_msg_parser(u8 configNr, u8 *buf);
 enum rndis_state rndis_get_state(int configNr);
-int  rndis_register(int (*rndis_control_ack)(struct eth_device *));
 void rndis_deregister(int configNr);
+#ifndef CONFIG_DM_ETH
+int  rndis_register(int (*rndis_control_ack)(struct eth_device *));
 int  rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
-			struct net_device_stats *stats, u16 *cdc_filter);
+			 struct net_device_stats *stats, u16 *cdc_filter);
+#else
+int  rndis_register(int (*rndis_control_ack)(struct udevice *));
+int  rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter);
+#endif
 int  rndis_set_param_vendor(u8 configNr, u32 vendorID,
 			    const char *vendorDescr);
 int  rndis_set_param_medium(u8 configNr, u32 medium, u32 speed);
include/net.h +7 -0
index 05800c4..7dcc6c8 100644
@@ -254,6 +254,13 @@ int eth_setenv_enetaddr_by_index(const char *base_name, int index,
 				 uchar *enetaddr);
 
 
+/*
+ * Initialize USB ethernet device with CONFIG_DM_ETH
+ * Returns:
+ *	0 is success, non-zero is error status.
+ */
+int usb_ether_init(void);
+
 /*
  * Get the hardware address for an ethernet interface .
  * Args:
Signature · 1 line
2.8.2.372.g63a3502
#13 lore raw

PATCH v2 6/6 drivers: usb: gadget: ether/rndis: convert driver to adopt device driver model

Quoted 163 lines · Mugunthan V N

Adopt usb ether gadget and rndis driver to adopt driver model

---
 drivers/usb/gadget/ether.c | 153 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/usb/gadget/rndis.c |  13 +++-
 drivers/usb/gadget/rndis.h |  19 ++++--
 include/net.h              |   7 +++
 4 files changed, 177 insertions(+), 15 deletions(-)
drivers/usb/gadget/ether.c +56 -9
index 2f70ebf..c436f75 100644
@@ -25,6 +25,7 @@
 #include "rndis.h"
 
 #include <dm.h>
+#include <dm/lists.h>
 #include <dm/uclass-internal.h>
 #include <dm/device-internal.h>
 
@@ -116,7 +117,11 @@ struct eth_dev {
 
 	struct usb_request	*tx_req, *rx_req;
 
+#ifndef CONFIG_DM_ETH
 	struct eth_device	*net;
+#else
+	struct udevice		*net;
+#endif
 	struct net_device_stats	stats;
 	unsigned int		tx_qlen;
 
@@ -143,7 +148,11 @@ struct eth_dev {
 /*-------------------------------------------------------------------------*/
 struct ether_priv {
 	struct eth_dev ethdev;
+#ifndef CONFIG_DM_ETH
 	struct eth_device netdev;
+#else
+	struct udevice *netdev;
+#endif
 	struct usb_gadget_driver eth_driver;
 };
 
@@ -1851,7 +1860,11 @@ static void rndis_control_ack_complete(struct usb_ep *ep,
 
 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
 
+#ifndef CONFIG_DM_ETH
 static int rndis_control_ack(struct eth_device *net)
+#else
+static int rndis_control_ack(struct udevice *net)
+#endif
 {
 	struct ether_priv	*priv = (struct ether_priv *)net->priv;
 	struct eth_dev		*dev = &priv->ethdev;
@@ -2001,6 +2014,9 @@ static int eth_bind(struct usb_gadget *gadget)
 	int			status = -ENOMEM;
 	int			gcnum;
 	u8			tmp[7];
+#ifdef CONFIG_DM_ETH
+	struct eth_pdata	*pdata = dev_get_platdata(l_priv->netdev);
+#endif
 
 	/* these flags are only ever cleared; compiler take note */
 #ifndef	CONFIG_USB_ETH_CDC
@@ -2188,7 +2204,11 @@ autoconf_fail:
 
 
 	/* network device setup */
+#ifndef CONFIG_DM_ETH
 	dev->net = &l_priv->netdev;
+#else
+	dev->net = l_priv->netdev;
+#endif
 
 	dev->cdc = cdc;
 	dev->zlp = zlp;
@@ -2197,6 +2217,7 @@ autoconf_fail:
 	dev->out_ep = out_ep;
 	dev->status_ep = status_ep;
 
+	memset(tmp, 0, sizeof(tmp));
 	/*
 	 * Module params for these addresses should come from ID proms.
 	 * The host side address is used with CDC and RNDIS, and commonly
@@ -2204,10 +2225,13 @@ autoconf_fail:
 	 * host side code for the SAFE thing cares -- its original BLAN
 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
 	 */
+#ifndef CONFIG_DM_ETH
 	get_ether_addr(dev_addr, dev->net->enetaddr);
-
-	memset(tmp, 0, sizeof(tmp));
 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
+#else
+	get_ether_addr(dev_addr, pdata->enetaddr);
+	memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
+#endif
 
 	get_ether_addr(host_addr, dev->host_mac);
 
@@ -2268,10 +2292,11 @@ autoconf_fail:
 		status_ep ? " STATUS " : "",
 		status_ep ? status_ep->name : ""
 		);
-	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
-		dev->net->enetaddr[0], dev->net->enetaddr[1],
-		dev->net->enetaddr[2], dev->net->enetaddr[3],
-		dev->net->enetaddr[4], dev->net->enetaddr[5]);
+#ifndef CONFIG_DM_ETH
+	printf("MAC %pM\n", dev->net->enetaddr);
+#else
+	printf("MAC %pM\n", pdata->enetaddr);
+#endif
 
 	if (cdc || rndis)
 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -2520,13 +2545,12 @@ void _usb_eth_halt(struct ether_priv *priv)
 	}
 
 	usb_gadget_unregister_driver(&priv->eth_driver);
-#ifdef CONFIG_DM_USB
-	device_remove(dev->usb_udev);
-#else
+#ifndef CONFIG_DM_USB
 	board_usb_cleanup(0, USB_INIT_DEVICE);
 #endif
 }
 
+#ifndef CONFIG_DM_ETH
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
@@ -2592,3 +2616,114 @@ int usb_eth_initialize(bd_t *bi)
 	eth_register(netdev);
 	return 0;
 }
+#else
+static int usb_eth_start(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct udevice *dev, void *packet, int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2651

Quoted 10 lines
drivers/usb/gadget/ether.c +10 -0
+		error("error packet receive\n");
+		return ret;
+	}
+
+	if (packet_received) {
+		if (ethdev->rx_req) {
+			*packetp = (uchar *)net_rx_packets[0];
+			return ethdev->rx_req->length;
+		} else {
+			error("dev->rx_req invalid");

Is this useful information ?

Quoted 24 lines
drivers/usb/gadget/ether.c +24 -0
+			return -EFAULT;
+		}
+	}
+
+	return -EAGAIN;
+}
+
+static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
+				   int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+
+	packet_received = 0;
+
+	return rx_submit(ethdev, ethdev->rx_req, 0);
+}
+
+static void usb_eth_stop(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	_usb_eth_halt(priv);
+}

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2685

Quoted 10 lines
drivers/usb/gadget/ether.c +10 -0
+
+static int usb_eth_probe(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_pdata *pdata = dev_get_platdata(dev);
+
+	priv->netdev = dev;
+	l_priv = priv;
+
+	get_ether_addr("de:ad:be:ef:00:01", pdata->enetaddr);

Can we avoid hard-coding this default MAC ?

Quoted 18 lines
drivers/usb/gadget/ether.c +18 -0
+	eth_setenv_enetaddr("usbnet_devaddr", pdata->enetaddr);
+
+	return 0;
+}
+
+static const struct eth_ops usb_eth_ops = {
+	.start		= usb_eth_start,
+	.send		= usb_eth_send,
+	.recv		= usb_eth_recv,
+	.free_pkt	= usb_eth_free_pkt,
+	.stop		= usb_eth_stop,
+};
+
+int usb_ether_init(void)
+{
+	struct udevice *dev;
+	struct udevice *usb_dev;
+	int ret;

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2713

Quoted 10 lines
drivers/usb/gadget/ether.c +10 -0
+
+	ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
+	if (!usb_dev || ret) {
+		error("No USB device found\n");
+		return ret;
+	}
+
+	ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
+	if (!dev || ret) {
+		error("usb - not able to bind usb_ether device\n");

Can you keep the messages consistent in some way ? Some start with capital letter, some don't . I would much rather see something like "%s: unable to bind usb_ether device\n", __func__

Quoted 110 lines
drivers/usb/gadget/ether.c +16 -0
+		return ret;
+	}
+
+	return 0;
+}
+
+U_BOOT_DRIVER(eth_usb) = {
+	.name	= "usb_ether",
+	.id	= UCLASS_ETH,
+	.probe	= usb_eth_probe,
+	.ops	= &usb_eth_ops,
+	.priv_auto_alloc_size = sizeof(struct ether_priv),
+	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
+	.flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
+#endif /* CONFIG_DM_ETH */
drivers/usb/gadget/rndis.c +11 -2
index 48463db..e6029ec 100644
@@ -1123,7 +1123,11 @@ int rndis_msg_parser(u8 configNr, u8 *buf)
 	return -ENOTSUPP;
 }
 
+#ifndef CONFIG_DM_ETH
 int rndis_register(int (*rndis_control_ack)(struct eth_device *))
+#else
+int rndis_register(int (*rndis_control_ack)(struct udevice *))
+#endif
 {
 	u8 i;
 
@@ -1151,8 +1155,13 @@ void rndis_deregister(int configNr)
 	return;
 }
 
-int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
-			struct net_device_stats *stats,	u16 *cdc_filter)
+#ifndef CONFIG_DM_ETH
+int  rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter)
+#else
+int  rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter)
+#endif
 {
 	debug("%s: configNr = %d\n", __func__, configNr);
 	if (!dev || !stats)
drivers/usb/gadget/rndis.h +15 -4
index 7a389a5..084af85 100644
@@ -222,23 +222,34 @@ typedef struct rndis_params {
 
 	const u8		*host_mac;
 	u16			*filter;
-	struct eth_device	*dev;
 	struct net_device_stats *stats;
 	int			mtu;
 
 	u32			vendorID;
 	const char		*vendorDescr;
-	int			(*ack)(struct eth_device *);
+#ifndef CONFIG_DM_ETH
+	struct eth_device	*dev;
+	int (*ack)(struct eth_device *);
+#else
+	struct udevice		*dev;
+	int (*ack)(struct udevice *);
+#endif
 	struct list_head	resp_queue;
 } rndis_params;
 
 /* RNDIS Message parser and other useless functions */
 int  rndis_msg_parser(u8 configNr, u8 *buf);
 enum rndis_state rndis_get_state(int configNr);
-int  rndis_register(int (*rndis_control_ack)(struct eth_device *));
 void rndis_deregister(int configNr);
+#ifndef CONFIG_DM_ETH
+int  rndis_register(int (*rndis_control_ack)(struct eth_device *));
 int  rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
-			struct net_device_stats *stats, u16 *cdc_filter);
+			 struct net_device_stats *stats, u16 *cdc_filter);
+#else
+int  rndis_register(int (*rndis_control_ack)(struct udevice *));
+int  rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter);
+#endif
 int  rndis_set_param_vendor(u8 configNr, u32 vendorID,
 			    const char *vendorDescr);
 int  rndis_set_param_medium(u8 configNr, u32 medium, u32 speed);
include/net.h +7 -0
index 05800c4..7dcc6c8 100644
@@ -254,6 +254,13 @@ int eth_setenv_enetaddr_by_index(const char *base_name, int index,
 				 uchar *enetaddr);
 
 
+/*
+ * Initialize USB ethernet device with CONFIG_DM_ETH
+ * Returns:
+ *	0 is success, non-zero is error status.
+ */
+int usb_ether_init(void);
+
 /*
  * Get the hardware address for an ethernet interface .
  * Args:
Signature · 2 lines
Best regards,
Marek Vasut
#14 lore raw

PATCH v2 6/6 drivers: usb: gadget: ether/rndis: convert driver to adopt device driver model

Quoted 168 lines · Marek Vasut
Quoted 167 lines · Mugunthan V N

Adopt usb ether gadget and rndis driver to adopt driver model

---
 drivers/usb/gadget/ether.c | 153 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/usb/gadget/rndis.c |  13 +++-
 drivers/usb/gadget/rndis.h |  19 ++++--
 include/net.h              |   7 +++
 4 files changed, 177 insertions(+), 15 deletions(-)
drivers/usb/gadget/ether.c +60 -9
index 2f70ebf..c436f75 100644
@@ -25,6 +25,7 @@
 #include "rndis.h"
 
 #include <dm.h>
+#include <dm/lists.h>
 #include <dm/uclass-internal.h>
 #include <dm/device-internal.h>
 
@@ -116,7 +117,11 @@ struct eth_dev {
 
 	struct usb_request	*tx_req, *rx_req;
 
+#ifndef CONFIG_DM_ETH
 	struct eth_device	*net;
+#else
+	struct udevice		*net;
+#endif
 	struct net_device_stats	stats;
 	unsigned int		tx_qlen;
 
@@ -143,7 +148,11 @@ struct eth_dev {
 /*-------------------------------------------------------------------------*/
 struct ether_priv {
 	struct eth_dev ethdev;
+#ifndef CONFIG_DM_ETH
 	struct eth_device netdev;
+#else
+	struct udevice *netdev;
+#endif
 	struct usb_gadget_driver eth_driver;
 };
 
@@ -1851,7 +1860,11 @@ static void rndis_control_ack_complete(struct usb_ep *ep,
 
 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
 
+#ifndef CONFIG_DM_ETH
 static int rndis_control_ack(struct eth_device *net)
+#else
+static int rndis_control_ack(struct udevice *net)
+#endif
 {
 	struct ether_priv	*priv = (struct ether_priv *)net->priv;
 	struct eth_dev		*dev = &priv->ethdev;
@@ -2001,6 +2014,9 @@ static int eth_bind(struct usb_gadget *gadget)
 	int			status = -ENOMEM;
 	int			gcnum;
 	u8			tmp[7];
+#ifdef CONFIG_DM_ETH
+	struct eth_pdata	*pdata = dev_get_platdata(l_priv->netdev);
+#endif
 
 	/* these flags are only ever cleared; compiler take note */
 #ifndef	CONFIG_USB_ETH_CDC
@@ -2188,7 +2204,11 @@ autoconf_fail:
 
 
 	/* network device setup */
+#ifndef CONFIG_DM_ETH
 	dev->net = &l_priv->netdev;
+#else
+	dev->net = l_priv->netdev;
+#endif
 
 	dev->cdc = cdc;
 	dev->zlp = zlp;
@@ -2197,6 +2217,7 @@ autoconf_fail:
 	dev->out_ep = out_ep;
 	dev->status_ep = status_ep;
 
+	memset(tmp, 0, sizeof(tmp));
 	/*
 	 * Module params for these addresses should come from ID proms.
 	 * The host side address is used with CDC and RNDIS, and commonly
@@ -2204,10 +2225,13 @@ autoconf_fail:
 	 * host side code for the SAFE thing cares -- its original BLAN
 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
 	 */
+#ifndef CONFIG_DM_ETH
 	get_ether_addr(dev_addr, dev->net->enetaddr);
-
-	memset(tmp, 0, sizeof(tmp));
 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
+#else
+	get_ether_addr(dev_addr, pdata->enetaddr);
+	memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
+#endif
 
 	get_ether_addr(host_addr, dev->host_mac);
 
@@ -2268,10 +2292,11 @@ autoconf_fail:
 		status_ep ? " STATUS " : "",
 		status_ep ? status_ep->name : ""
 		);
-	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
-		dev->net->enetaddr[0], dev->net->enetaddr[1],
-		dev->net->enetaddr[2], dev->net->enetaddr[3],
-		dev->net->enetaddr[4], dev->net->enetaddr[5]);
+#ifndef CONFIG_DM_ETH
+	printf("MAC %pM\n", dev->net->enetaddr);
+#else
+	printf("MAC %pM\n", pdata->enetaddr);
+#endif
 
 	if (cdc || rndis)
 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -2520,13 +2545,12 @@ void _usb_eth_halt(struct ether_priv *priv)
 	}
 
 	usb_gadget_unregister_driver(&priv->eth_driver);
-#ifdef CONFIG_DM_USB
-	device_remove(dev->usb_udev);
-#else
+#ifndef CONFIG_DM_USB
 	board_usb_cleanup(0, USB_INIT_DEVICE);
 #endif
 }
 
+#ifndef CONFIG_DM_ETH
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
@@ -2592,3 +2616,114 @@ int usb_eth_initialize(bd_t *bi)
 	eth_register(netdev);
 	return 0;
 }
+#else
+static int usb_eth_start(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct udevice *dev, void *packet, int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {
+		error("error packet receive\n");
+		return ret;
+	}
+
Quoted 8 lines

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2651

Quoted 6 lines
drivers/usb/gadget/ether.c +6 -0
+	if (packet_received) {
+		if (ethdev->rx_req) {
+			*packetp = (uchar *)net_rx_packets[0];
+			return ethdev->rx_req->length;
+		} else {
+			error("dev->rx_req invalid");

Is this useful information ?

It is just a carry forward from non DM code.
I do feel that without a req, packet_receviced will never be true. If you are okay, i will remove this in error log in next version.

Quoted 29 lines
Quoted 29 lines
drivers/usb/gadget/ether.c +28 -0
+			return -EFAULT;
+		}
+	}
+
+	return -EAGAIN;
+}
+
+static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
+				   int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+
+	packet_received = 0;
+
+	return rx_submit(ethdev, ethdev->rx_req, 0);
+}
+
+static void usb_eth_stop(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	_usb_eth_halt(priv);
+}
+
+static int usb_eth_probe(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
Quoted 8 lines

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2685

Quoted 6 lines
drivers/usb/gadget/ether.c +6 -0
+	struct eth_pdata *pdata = dev_get_platdata(dev);
+
+	priv->netdev = dev;
+	l_priv = priv;
+
+	get_ether_addr("de:ad:be:ef:00:01", pdata->enetaddr);

Can we avoid hard-coding this default MAC ?

Yeah, will fix in next version.

Quoted 25 lines
Quoted 25 lines
drivers/usb/gadget/ether.c +24 -0
+	eth_setenv_enetaddr("usbnet_devaddr", pdata->enetaddr);
+
+	return 0;
+}
+
+static const struct eth_ops usb_eth_ops = {
+	.start		= usb_eth_start,
+	.send		= usb_eth_send,
+	.recv		= usb_eth_recv,
+	.free_pkt	= usb_eth_free_pkt,
+	.stop		= usb_eth_stop,
+};
+
+int usb_ether_init(void)
+{
+	struct udevice *dev;
+	struct udevice *usb_dev;
+	int ret;
+
+	ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
+	if (!usb_dev || ret) {
+		error("No USB device found\n");
+		return ret;
+	}
Quoted 8 lines

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2713

Quoted 4 lines
drivers/usb/gadget/ether.c +4 -0
+
+	ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
+	if (!dev || ret) {
+		error("usb - not able to bind usb_ether device\n");

Can you keep the messages consistent in some way ? Some start with capital letter, some don't . I would much rather see something like "%s: unable to bind usb_ether device\n", __func__

Yeah, will fix in next version.

Regards
Mugunthan V N

Quoted 113 lines
Quoted 111 lines
drivers/usb/gadget/ether.c +16 -0
+		return ret;
+	}
+
+	return 0;
+}
+
+U_BOOT_DRIVER(eth_usb) = {
+	.name	= "usb_ether",
+	.id	= UCLASS_ETH,
+	.probe	= usb_eth_probe,
+	.ops	= &usb_eth_ops,
+	.priv_auto_alloc_size = sizeof(struct ether_priv),
+	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
+	.flags = DM_FLAG_ALLOC_PRIV_DMA,
+};
+#endif /* CONFIG_DM_ETH */
drivers/usb/gadget/rndis.c +11 -2
index 48463db..e6029ec 100644
@@ -1123,7 +1123,11 @@ int rndis_msg_parser(u8 configNr, u8 *buf)
 	return -ENOTSUPP;
 }
 
+#ifndef CONFIG_DM_ETH
 int rndis_register(int (*rndis_control_ack)(struct eth_device *))
+#else
+int rndis_register(int (*rndis_control_ack)(struct udevice *))
+#endif
 {
 	u8 i;
 
@@ -1151,8 +1155,13 @@ void rndis_deregister(int configNr)
 	return;
 }
 
-int rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
-			struct net_device_stats *stats,	u16 *cdc_filter)
+#ifndef CONFIG_DM_ETH
+int  rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter)
+#else
+int  rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter)
+#endif
 {
 	debug("%s: configNr = %d\n", __func__, configNr);
 	if (!dev || !stats)
drivers/usb/gadget/rndis.h +15 -4
index 7a389a5..084af85 100644
@@ -222,23 +222,34 @@ typedef struct rndis_params {
 
 	const u8		*host_mac;
 	u16			*filter;
-	struct eth_device	*dev;
 	struct net_device_stats *stats;
 	int			mtu;
 
 	u32			vendorID;
 	const char		*vendorDescr;
-	int			(*ack)(struct eth_device *);
+#ifndef CONFIG_DM_ETH
+	struct eth_device	*dev;
+	int (*ack)(struct eth_device *);
+#else
+	struct udevice		*dev;
+	int (*ack)(struct udevice *);
+#endif
 	struct list_head	resp_queue;
 } rndis_params;
 
 /* RNDIS Message parser and other useless functions */
 int  rndis_msg_parser(u8 configNr, u8 *buf);
 enum rndis_state rndis_get_state(int configNr);
-int  rndis_register(int (*rndis_control_ack)(struct eth_device *));
 void rndis_deregister(int configNr);
+#ifndef CONFIG_DM_ETH
+int  rndis_register(int (*rndis_control_ack)(struct eth_device *));
 int  rndis_set_param_dev(u8 configNr, struct eth_device *dev, int mtu,
-			struct net_device_stats *stats, u16 *cdc_filter);
+			 struct net_device_stats *stats, u16 *cdc_filter);
+#else
+int  rndis_register(int (*rndis_control_ack)(struct udevice *));
+int  rndis_set_param_dev(u8 configNr, struct udevice *dev, int mtu,
+			 struct net_device_stats *stats, u16 *cdc_filter);
+#endif
 int  rndis_set_param_vendor(u8 configNr, u32 vendorID,
 			    const char *vendorDescr);
 int  rndis_set_param_medium(u8 configNr, u32 medium, u32 speed);
include/net.h +7 -0
index 05800c4..7dcc6c8 100644
@@ -254,6 +254,13 @@ int eth_setenv_enetaddr_by_index(const char *base_name, int index,
 				 uchar *enetaddr);
 
 
+/*
+ * Initialize USB ethernet device with CONFIG_DM_ETH
+ * Returns:
+ *	0 is success, non-zero is error status.
+ */
+int usb_ether_init(void);
+
 /*
  * Get the hardware address for an ethernet interface .
  * Args:
#15 lore raw

PATCH v2 6/6 drivers: usb: gadget: ether/rndis: convert driver to adopt device driver model

Quoted 178 lines · Mugunthan V N
Quoted 168 lines · Marek Vasut
Quoted 167 lines · Mugunthan V N

Adopt usb ether gadget and rndis driver to adopt driver model

---
 drivers/usb/gadget/ether.c | 153 ++++++++++++++++++++++++++++++++++++++++++---
 drivers/usb/gadget/rndis.c |  13 +++-
 drivers/usb/gadget/rndis.h |  19 ++++--
 include/net.h              |   7 +++
 4 files changed, 177 insertions(+), 15 deletions(-)
drivers/usb/gadget/ether.c +60 -9
index 2f70ebf..c436f75 100644
@@ -25,6 +25,7 @@
 #include "rndis.h"
 
 #include <dm.h>
+#include <dm/lists.h>
 #include <dm/uclass-internal.h>
 #include <dm/device-internal.h>
 
@@ -116,7 +117,11 @@ struct eth_dev {
 
 	struct usb_request	*tx_req, *rx_req;
 
+#ifndef CONFIG_DM_ETH
 	struct eth_device	*net;
+#else
+	struct udevice		*net;
+#endif
 	struct net_device_stats	stats;
 	unsigned int		tx_qlen;
 
@@ -143,7 +148,11 @@ struct eth_dev {
 /*-------------------------------------------------------------------------*/
 struct ether_priv {
 	struct eth_dev ethdev;
+#ifndef CONFIG_DM_ETH
 	struct eth_device netdev;
+#else
+	struct udevice *netdev;
+#endif
 	struct usb_gadget_driver eth_driver;
 };
 
@@ -1851,7 +1860,11 @@ static void rndis_control_ack_complete(struct usb_ep *ep,
 
 static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
 
+#ifndef CONFIG_DM_ETH
 static int rndis_control_ack(struct eth_device *net)
+#else
+static int rndis_control_ack(struct udevice *net)
+#endif
 {
 	struct ether_priv	*priv = (struct ether_priv *)net->priv;
 	struct eth_dev		*dev = &priv->ethdev;
@@ -2001,6 +2014,9 @@ static int eth_bind(struct usb_gadget *gadget)
 	int			status = -ENOMEM;
 	int			gcnum;
 	u8			tmp[7];
+#ifdef CONFIG_DM_ETH
+	struct eth_pdata	*pdata = dev_get_platdata(l_priv->netdev);
+#endif
 
 	/* these flags are only ever cleared; compiler take note */
 #ifndef	CONFIG_USB_ETH_CDC
@@ -2188,7 +2204,11 @@ autoconf_fail:
 
 
 	/* network device setup */
+#ifndef CONFIG_DM_ETH
 	dev->net = &l_priv->netdev;
+#else
+	dev->net = l_priv->netdev;
+#endif
 
 	dev->cdc = cdc;
 	dev->zlp = zlp;
@@ -2197,6 +2217,7 @@ autoconf_fail:
 	dev->out_ep = out_ep;
 	dev->status_ep = status_ep;
 
+	memset(tmp, 0, sizeof(tmp));
 	/*
 	 * Module params for these addresses should come from ID proms.
 	 * The host side address is used with CDC and RNDIS, and commonly
@@ -2204,10 +2225,13 @@ autoconf_fail:
 	 * host side code for the SAFE thing cares -- its original BLAN
 	 * thing didn't, Sharp never assigned those addresses on Zaurii.
 	 */
+#ifndef CONFIG_DM_ETH
 	get_ether_addr(dev_addr, dev->net->enetaddr);
-
-	memset(tmp, 0, sizeof(tmp));
 	memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
+#else
+	get_ether_addr(dev_addr, pdata->enetaddr);
+	memcpy(tmp, pdata->enetaddr, sizeof(pdata->enetaddr));
+#endif
 
 	get_ether_addr(host_addr, dev->host_mac);
 
@@ -2268,10 +2292,11 @@ autoconf_fail:
 		status_ep ? " STATUS " : "",
 		status_ep ? status_ep->name : ""
 		);
-	printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
-		dev->net->enetaddr[0], dev->net->enetaddr[1],
-		dev->net->enetaddr[2], dev->net->enetaddr[3],
-		dev->net->enetaddr[4], dev->net->enetaddr[5]);
+#ifndef CONFIG_DM_ETH
+	printf("MAC %pM\n", dev->net->enetaddr);
+#else
+	printf("MAC %pM\n", pdata->enetaddr);
+#endif
 
 	if (cdc || rndis)
 		printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -2520,13 +2545,12 @@ void _usb_eth_halt(struct ether_priv *priv)
 	}
 
 	usb_gadget_unregister_driver(&priv->eth_driver);
-#ifdef CONFIG_DM_USB
-	device_remove(dev->usb_udev);
-#else
+#ifndef CONFIG_DM_USB
 	board_usb_cleanup(0, USB_INIT_DEVICE);
 #endif
 }
 
+#ifndef CONFIG_DM_ETH
 static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
 {
 	struct ether_priv *priv = (struct ether_priv *)netdev->priv;
@@ -2592,3 +2616,114 @@ int usb_eth_initialize(bd_t *bi)
 	eth_register(netdev);
 	return 0;
 }
+#else
+static int usb_eth_start(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_init(priv);
+}
+
+static int usb_eth_send(struct udevice *dev, void *packet, int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	return _usb_eth_send(priv, packet, length);
+}
+
+static int usb_eth_recv(struct udevice *dev, int flags, uchar **packetp)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+	int ret;
+
+	ret = _usb_eth_recv(priv);
+	if (ret) {
+		error("error packet receive\n");
+		return ret;
+	}
+
Quoted 8 lines

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2651

Quoted 6 lines
drivers/usb/gadget/ether.c +6 -0
+	if (packet_received) {
+		if (ethdev->rx_req) {
+			*packetp = (uchar *)net_rx_packets[0];
+			return ethdev->rx_req->length;
+		} else {
+			error("dev->rx_req invalid");

Is this useful information ?

Quoted 3 lines

It is just a carry forward from non DM code.
I do feel that without a req, packet_receviced will never be true. If you are okay, i will remove this in error log in next version.

If this is just carry forward, then subsequent patch is fine too.

Quoted 74 lines
Quoted 29 lines
Quoted 29 lines
drivers/usb/gadget/ether.c +28 -0
+			return -EFAULT;
+		}
+	}
+
+	return -EAGAIN;
+}
+
+static int usb_eth_free_pkt(struct udevice *dev, uchar *packet,
+				   int length)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+	struct eth_dev *ethdev = &priv->ethdev;
+
+	packet_received = 0;
+
+	return rx_submit(ethdev, ethdev->rx_req, 0);
+}
+
+static void usb_eth_stop(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
+
+	_usb_eth_halt(priv);
+}
+
+static int usb_eth_probe(struct udevice *dev)
+{
+	struct ether_priv *priv = dev_get_priv(dev);
Quoted 8 lines

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2685

Quoted 6 lines
drivers/usb/gadget/ether.c +6 -0
+	struct eth_pdata *pdata = dev_get_platdata(dev);
+
+	priv->netdev = dev;
+	l_priv = priv;
+
+	get_ether_addr("de:ad:be:ef:00:01", pdata->enetaddr);

Can we avoid hard-coding this default MAC ?

Yeah, will fix in next version.

Quoted 25 lines
Quoted 25 lines
drivers/usb/gadget/ether.c +24 -0
+	eth_setenv_enetaddr("usbnet_devaddr", pdata->enetaddr);
+
+	return 0;
+}
+
+static const struct eth_ops usb_eth_ops = {
+	.start		= usb_eth_start,
+	.send		= usb_eth_send,
+	.recv		= usb_eth_recv,
+	.free_pkt	= usb_eth_free_pkt,
+	.stop		= usb_eth_stop,
+};
+
+int usb_ether_init(void)
+{
+	struct udevice *dev;
+	struct udevice *usb_dev;
+	int ret;
+
+	ret = uclass_first_device(UCLASS_USB_DEV_GENERIC, &usb_dev);
+	if (!usb_dev || ret) {
+		error("No USB device found\n");
+		return ret;
+	}
Quoted 8 lines

drivers/usb/gadget/ether.c int usb_eth_initialize(bd_t *bi) · line 2713

Quoted 4 lines
drivers/usb/gadget/ether.c +4 -0
+
+	ret = device_bind_driver(usb_dev, "usb_ether", "usb_ether", &dev);
+	if (!dev || ret) {
+		error("usb - not able to bind usb_ether device\n");

Can you keep the messages consistent in some way ? Some start with capital letter, some don't . I would much rather see something like "%s: unable to bind usb_ether device\n", __func__

Quoted 1 line

Yeah, will fix in next version.

Thanks

[...]

Signature · 2 lines
Best regards,
Marek Vasut