[RFC PATCH 1/4] dm: core: Add a way to specify an alt name for alias sequence numbering

Zixun LI admin at hifiphile.com
Wed Jul 31 15:42:16 CEST 2024


A new field name_seq_alias is added to uclass_driver structure, which
allows an uclass driver to use an alternate name for alias sequence
numbering.

For example an uclass named "usb_gadget" can share alias with
"usb" uclass :
    UCLASS_DRIVER(usb_gadget_generic) = {
	    .id = UCLASS_USB_GADGET_GENERIC,
	    .name = "usb_gadget",
	    .name_seq_alias = "usb",
    	.flags = DM_UC_FLAG_SEQ_ALIAS,
    };

Currently there are some uclasses with duplicate name which break uclass
functions like uclass_get_by_name(), with this patch it's now possible
to rename these classes without break the existing alias function.

Signed-off-by: Zixun LI <admin at hifiphile.com>
---
 drivers/core/device.c |  3 ++-
 drivers/core/read.c   |  7 ++++++-
 drivers/core/uclass.c | 11 +++++++++--
 include/dm/read.h     |  9 ++++++++-
 include/dm/uclass.h   |  2 ++
 5 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/drivers/core/device.c b/drivers/core/device.c
index 779f371b9d..a98e75b0b8 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -89,7 +89,8 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
 		 */
 		if (CONFIG_IS_ENABLED(OF_CONTROL) &&
 		    !CONFIG_IS_ENABLED(OF_PLATDATA)) {
-			if (uc->uc_drv->name && ofnode_valid(node)) {
+			if ((uc->uc_drv->name || uc->uc_drv->name_seq_alias) &&
+			    ofnode_valid(node)) {
 				if (!dev_read_alias_seq(dev, &dev->seq_)) {
 					auto_seq = false;
 					log_debug("   - seq=%d\n", dev->seq_);
diff --git a/drivers/core/read.c b/drivers/core/read.c
index 55c19f335a..97b12fb7a9 100644
--- a/drivers/core/read.c
+++ b/drivers/core/read.c
@@ -347,9 +347,14 @@ const void *dev_read_prop_by_prop(struct ofprop *prop,
 int dev_read_alias_seq(const struct udevice *dev, int *devnump)
 {
 	ofnode node = dev_ofnode(dev);
-	const char *uc_name = dev->uclass->uc_drv->name;
+	const char *uc_name;
 	int ret = -ENOTSUPP;
 
+	if (dev->uclass->uc_drv->name_seq_alias)
+		uc_name = dev->uclass->uc_drv->name_seq_alias;
+	else
+		uc_name = dev->uclass->uc_drv->name;
+
 	if (ofnode_is_np(node)) {
 		ret = of_alias_get_id(ofnode_to_np(node), uc_name);
 		if (ret >= 0) {
diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 7ae0884a75..6dfbee2e78 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -308,11 +308,18 @@ int uclass_find_next_free_seq(struct uclass *uc)
 {
 	struct udevice *dev;
 	int max = -1;
+	const char *uc_name;
 
 	/* If using aliases, start with the highest alias value */
 	if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
-	    (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
-		max = dev_read_alias_highest_id(uc->uc_drv->name);
+	    (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
+		if (uc->uc_drv->name_seq_alias)
+			uc_name = uc->uc_drv->name_seq_alias;
+		else
+			uc_name = uc->uc_drv->name;
+
+		max = dev_read_alias_highest_id(uc_name);
+	}
 
 	/* Avoid conflict with existing devices */
 	list_for_each_entry(dev, &uc->dev_head, uclass_node) {
diff --git a/include/dm/read.h b/include/dm/read.h
index 894bc698bb..5a86e43d86 100644
--- a/include/dm/read.h
+++ b/include/dm/read.h
@@ -1169,7 +1169,14 @@ static inline const void *dev_read_prop_by_prop(struct ofprop *prop,
 static inline int dev_read_alias_seq(const struct udevice *dev, int *devnump)
 {
 #if CONFIG_IS_ENABLED(OF_CONTROL)
-	return fdtdec_get_alias_seq(gd->fdt_blob, dev->uclass->uc_drv->name,
+	const char *uc_name;
+
+	if (dev->uclass->uc_drv->name_seq_alias)
+		uc_name = dev->uclass->uc_drv->name_seq_alias;
+	else
+		uc_name = dev->uclass->uc_drv->name;
+
+	return fdtdec_get_alias_seq(gd->fdt_blob, uc_name,
 				    dev_of_offset(dev), devnump);
 #else
 	return -ENOTSUPP;
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index 456eef7f2f..8339e0d88b 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -57,6 +57,7 @@ struct udevice;
  * drivers.
  *
  * @name: Name of uclass driver
+ * @name_seq_alias: Name used for alias sequence numbering
  * @id: ID number of this uclass
  * @post_bind: Called after a new device is bound to this uclass
  * @pre_unbind: Called before a device is unbound from this uclass
@@ -88,6 +89,7 @@ struct udevice;
  */
 struct uclass_driver {
 	const char *name;
+	const char *name_seq_alias;
 	enum uclass_id id;
 	int (*post_bind)(struct udevice *dev);
 	int (*pre_unbind)(struct udevice *dev);
-- 
2.45.2



More information about the U-Boot mailing list