[PATCH 018/112] cdev: implement setter/getter for cdev device node

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jan 3 10:11:38 PST 2024


A cdev has two device tree node pointers, one directly at struct
cdev.device_node and another indirectly via cdev.dev->device_node.

We may want to remove cdev::device_node in future, but till then to
avoid users having to guess, which device_node is the correct one, add a
helper to set and get the device tree node.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 arch/sandbox/board/hostfile.c |  2 +-
 drivers/mci/mci-core.c        |  2 +-
 drivers/nvmem/core.c          |  2 +-
 drivers/of/of_path.c          |  4 ++--
 drivers/of/partition.c        | 12 ++++++------
 fs/devfs-core.c               |  2 +-
 fs/fs.c                       |  2 +-
 include/driver.h              | 11 +++++++++++
 8 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/arch/sandbox/board/hostfile.c b/arch/sandbox/board/hostfile.c
index 225617fe910f..88d4d6605fd1 100644
--- a/arch/sandbox/board/hostfile.c
+++ b/arch/sandbox/board/hostfile.c
@@ -153,7 +153,7 @@ static int hf_probe(struct device *dev)
 
 	cdev = is_blockdev ? &priv->blk.cdev : &priv->cdev;
 
-	cdev->device_node = np;
+	cdev_set_of_node(cdev, np);
 
 	if (is_blockdev) {
 		cdev->name = np->name;
diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 2b39985d5e72..663d366666b0 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -557,7 +557,7 @@ static void mci_part_add(struct mci *mci, uint64_t size,
 	part->idx = idx;
 
 	if (area_type == MMC_BLK_DATA_AREA_MAIN) {
-		part->blk.cdev.device_node = mci->host->hw_dev->of_node;
+		cdev_set_of_node(&part->blk.cdev, mci->host->hw_dev->of_node);
 		part->blk.cdev.flags |= DEVFS_IS_MCI_MAIN_PART_DEV;
 	}
 
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 67bb1d799376..e7341b62f6e6 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -213,7 +213,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 	nvmem->dev.parent = config->dev;
 	nvmem->reg_read = config->reg_read;
 	nvmem->reg_write = config->reg_write;
-	np = config->cdev ? config->cdev->device_node : config->dev->of_node;
+	np = config->cdev ? cdev_of_node(config->cdev) : config->dev->of_node;
 	nvmem->dev.of_node = np;
 	nvmem->priv = config->priv;
 	nvmem->cell_post_process = config->cell_post_process;
diff --git a/drivers/of/of_path.c b/drivers/of/of_path.c
index 12a2dfce553e..42efb1ad1dbf 100644
--- a/drivers/of/of_path.c
+++ b/drivers/of/of_path.c
@@ -181,9 +181,9 @@ struct device_node *of_find_node_by_devpath(struct device_node *root, const char
 		part_size = cdev->size;
 		pr_debug("%s path %s: is a partition with offset 0x%08llx, size 0x%08llx\n",
 			 __func__, path, part_offset, part_size);
-		np = cdev->master->device_node;
+		np = cdev_of_node(cdev->master);
 	} else {
-		np = cdev->device_node;
+		np = cdev_of_node(cdev);
 	}
 
 	/*
diff --git a/drivers/of/partition.c b/drivers/of/partition.c
index b91fe616d990..395ebbc45ac8 100644
--- a/drivers/of/partition.c
+++ b/drivers/of/partition.c
@@ -95,7 +95,7 @@ int of_parse_partitions(struct cdev *cdev, struct device_node *node)
 	if (!node)
 		return -EINVAL;
 
-	cdev->device_node = node;
+	cdev_set_of_node(cdev, node);
 
 	subnode = of_get_child_by_name(node, "partitions");
 	if (subnode) {
@@ -276,21 +276,21 @@ int of_fixup_partitions(struct device_node *np, struct cdev *cdev)
 static int of_partition_fixup(struct device_node *root, void *ctx)
 {
 	struct cdev *cdev = ctx;
-	struct device_node *np;
+	struct device_node *cdev_np, *np;
 	char *name;
 
-	if (!cdev->device_node)
+	cdev_np = cdev_of_node(cdev);
+	if (!cdev_np)
 		return -EINVAL;
 
 	if (list_empty(&cdev->partitions))
 		return 0;
 
-	name = of_get_reproducible_name(cdev->device_node);
+	name = of_get_reproducible_name(cdev_np);
 	np = of_find_node_by_reproducible_name(root, name);
 	free(name);
 	if (!np) {
-		dev_err(cdev->dev, "Cannot find nodepath %pOF, cannot fixup\n",
-				cdev->device_node);
+		dev_err(cdev->dev, "Cannot find nodepath %pOF, cannot fixup\n", cdev_np);
 		return -EINVAL;
 	}
 
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 9f5b41761f52..c5c62028a03f 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -91,7 +91,7 @@ struct cdev *cdev_by_device_node(struct device_node *node)
 		return NULL;
 
 	for_each_cdev(cdev) {
-		if (cdev->device_node == node)
+		if (cdev_of_node(cdev) == node)
 			return cdev_readlink(cdev);
 	}
 	return NULL;
diff --git a/fs/fs.c b/fs/fs.c
index 4b64f6fcf1df..1e357872f6ae 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -3038,7 +3038,7 @@ static char *get_linux_mmcblkdev(const struct cdev *root_cdev)
 	if (!cdevm || !cdev_is_mci_main_part_dev(cdevm))
 		return NULL;
 
-	id = of_alias_get_id(cdevm->device_node, "mmc");
+	id = of_alias_get_id(cdev_of_node(cdevm), "mmc");
 	if (id < 0)
 		return NULL;
 
diff --git a/include/driver.h b/include/driver.h
index f4f26151e365..c6da5a3d0d54 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -556,6 +556,17 @@ struct cdev {
 	};
 };
 
+static inline struct device_node *cdev_of_node(const struct cdev *cdev)
+{
+	return IS_ENABLED(CONFIG_OFDEVICE) ? cdev->device_node : NULL;
+}
+
+static inline void cdev_set_of_node(struct cdev *cdev, struct device_node *np)
+{
+	if (IS_ENABLED(CONFIG_OFDEVICE))
+		cdev->device_node = np;
+}
+
 int devfs_create(struct cdev *);
 int devfs_create_link(struct cdev *, const char *name);
 int devfs_remove(struct cdev *);
-- 
2.39.2




More information about the barebox mailing list