Create an ETH_UCLASS udevice and bind it to a driver

Signed-off-by: Adriano Cordova <adriano.cord...@canonical.com>
---
 include/net-common.h |  2 ++
 net/eth-uclass.c     | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/net-common.h b/include/net-common.h
index 1d507b13b0..ba343af4c6 100644
--- a/include/net-common.h
+++ b/include/net-common.h
@@ -203,6 +203,8 @@ int eth_receive(void *packet, int length); /* Receive a 
packet*/
 extern void (*push_packet)(void *packet, int length);
 #endif
 int eth_rx(void);                      /* Check for received packets */
+int eth_create_device(struct udevice *parent, const char *drv_name,
+                     const char *name, struct udevice **devp);
 
 /**
  * reset_phy() - Reset the Ethernet PHY
diff --git a/net/eth-uclass.c b/net/eth-uclass.c
index 5555f82f23..952df419b7 100644
--- a/net/eth-uclass.c
+++ b/net/eth-uclass.c
@@ -12,10 +12,12 @@
 #include <dm.h>
 #include <env.h>
 #include <log.h>
+#include <malloc.h>
 #include <net.h>
 #include <nvmem.h>
 #include <asm/global_data.h>
 #include <dm/device-internal.h>
+#include <dm/lists.h>
 #include <dm/uclass-internal.h>
 #include <net/pcap.h>
 #include "eth_internal.h"
@@ -528,6 +530,36 @@ int eth_initialize(void)
        return num_devices;
 }
 
+int eth_create_device(struct udevice *parent, const char *drv_name,
+                     const char *name, struct udevice **devp)
+{
+       char temp[40], *str;
+       int devnum;
+       struct uclass *uc;
+       int ret;
+
+       ret = uclass_get(UCLASS_ETH, &uc);
+       if (ret)
+               return -1;
+
+       devnum = uclass_find_next_free_seq(uc);
+       if (devnum < 0)
+               return -1;
+
+       snprintf(temp, sizeof(temp), "%s_eth%d", name, devnum);
+       temp[sizeof(temp) - 1] = '\0';
+       str = strdup(temp);
+
+       ret = device_bind_driver(parent, drv_name, str, devp);
+       if (ret) {
+               free(str);
+               return ret;
+       }
+       device_set_name_alloced(*devp);
+
+       return 0;
+}
+
 static int eth_post_bind(struct udevice *dev)
 {
        struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
-- 
2.43.0

Reply via email to