Skip to contentSkip to conversation

core: Add functions to set properties in live-tree

PATCH New Mario Six 2 participants 13 messages Jun 26, 2018 – Oct 2, 2018

lore
Share & tools
lore

Mario Six

author
#0 lore raw

PATCH v3 1/4 core: Add functions to set properties in live-tree

Implement a set of functions to manipulate properties in a live device tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
drivers/core/ofnode.c +70 -0
index 3cf3205a2f1..e8becf7f04d 100644
@@ -697,3 +697,73 @@ int ofnode_device_is_compatible(ofnode node, const char *compat)
 						  ofnode_to_offset(node),
 						  compat);
 }
+
+int ofnode_write_prop(ofnode node, const char *propname, int len,
+		      const void *value)
+{
+	const struct device_node *np = ofnode_to_np(node);
+	struct property *pp;
+	struct property *pp_last = NULL;
+	struct property *new;
+
+	if (!of_live_active())
+		return -ENOSYS;
+
+	if (!np)
+		return -EINVAL;
+
+	for (pp = np->properties; pp; pp = pp->next) {
+		if (strcmp(pp->name, propname) == 0) {
+			/* Property exists -> change value */
+			pp->value = (void *)value;
+			pp->length = len;
+			return 0;
+		}
+		pp_last = pp;
+	}
+
+	if (!pp_last)
+		return -ENOENT;
+
+	/* Property does not exist -> append new property */
+	new = malloc(sizeof(struct property));
+	if (!new)
+		return -ENOMEM;
+
+	new->name = strdup(propname);
+	if (!new->name)
+		return -ENOMEM;
+
+	new->value = (void *)value;
+	new->length = len;
+	new->next = NULL;
+
+	pp_last->next = new;
+
+	return 0;
+}
+
+int ofnode_write_string(ofnode node, const char *propname, const char *value)
+{
+	if (!of_live_active())
+		return -ENOSYS;
+
+	assert(ofnode_valid(node));
+
+	debug("%s: %s = %s", __func__, propname, value);
+
+	return ofnode_write_prop(node, propname, strlen(value) + 1, value);
+}
+
+int ofnode_set_enabled(ofnode node, bool value)
+{
+	if (!of_live_active())
+		return -ENOSYS;
+
+	assert(ofnode_valid(node));
+
+	if (value)
+		return ofnode_write_string(node, "status", "okay");
+	else
+		return ofnode_write_string(node, "status", "disable");
+}
include/dm/ofnode.h +46 -0

50 lines not on this page · raw message

--
2.11.0

author
#1 lore raw

PATCH v3 2/4 test: Add tests for DT-manipulation functions

Add tests for the ofnode_set_enabled, ofnode_write_string, and ofnode_write_property functions.

---

v2 -> v3:
* Fixed style violation

v1 -> v2:
New in v2

---
 test/dm/test-fdt.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
test/dm/test-fdt.c +54 -0
index 8196844e89a..1e3faf15227 100644
@@ -14,6 +14,8 @@
 #include <dm/device-internal.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
+#include <dm/lists.h>
+#include <dm/of_access.h>
 #include <test/ut.h>
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -461,3 +463,55 @@ static int dm_test_fdt_translation(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+	ofnode node;
+
+	if (!of_live_active()) {
+		printf("Live tree not active; ignore test\n");
+		return 0;
+	}
+
+	/* Test enabling devices */
+
+	node = ofnode_path("/usb at 2");
+
+	ut_assert(!of_device_is_available(ofnode_to_np(node)));
+	ofnode_set_enabled(node, true);
+	ut_assert(of_device_is_available(ofnode_to_np(node)));
+
+	device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb at 2", node,
+				   &dev);
+	ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
+
+	/* Test string property setting */
+
+	ut_assert(device_is_compatible(dev, "sandbox,usb"));
+	ofnode_write_string(node, "compatible", "gdsys,super-usb");
+	ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
+	ofnode_write_string(node, "compatible", "sandbox,usb");
+	ut_assert(device_is_compatible(dev, "sandbox,usb"));
+
+	/* Test setting generic properties */
+
+	/* Non-existent in DTB */
+	ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
+	/* reg = 0x42, size = 0x100 */
+	ut_assertok(ofnode_write_prop(node, "reg", 8,
+				      "\x00\x00\x00\x42\x00\x00\x01\x00"));
+	ut_asserteq(0x42, dev_read_addr(dev));
+
+	/* Test disabling devices */
+
+	device_remove(dev, DM_REMOVE_NORMAL);
+	device_unbind(dev);
+
+	ut_assert(of_device_is_available(ofnode_to_np(node)));
+	ofnode_set_enabled(node, false);
+	ut_assert(!of_device_is_available(ofnode_to_np(node)));
+
+	return 0;
+}
+DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);

--
2.11.0

#2 lore raw

PATCH v3 2/4 test: Add tests for DT-manipulation functions

Quoted 11 lines · Mario Six

Add tests for the ofnode_set_enabled, ofnode_write_string, and ofnode_write_property functions.

---

v2 -> v3:
* Fixed style violation

Quoted 6 lines

v1 -> v2:
New in v2

---
 test/dm/test-fdt.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

Applied to u-boot-dm, and now in mainline, thanks!

author
#3 lore raw

PATCH v3 3/4 core: Add dev_{disable,enable}_by_path

In reply to Mario Six

We cannot use device structures to disable devices, since getting them with the API functions would bind and activate the device, which would fail if the underlying device does not exist.

---

Hence, add a function to disable devices by path in a live device tree.

v2 -> v3:
* Renamed res to ret
* Introduced device_find_by_ofnode() function
* Fixed error handling in some places

v1 -> v2:
* Simplified np_to_ofnode(of_find_node_by_path(path)) to ofnode_path(path)
* Switched to returning -ENOSYS if livetree is not enabled

---
 drivers/core/device.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/device.h   | 16 +++++++++++
 2 files changed, 94 insertions(+)
drivers/core/device.c +78 -0
index e048e1a6595..c0037a2eeaf 100644
@@ -501,6 +501,33 @@ static int device_get_device_tail(struct udevice *dev, int ret,
 	return 0;
 }
 
+/**
+ * device_find_by_ofnode() - Return device associated with given ofnode
+ *
+ * The returned device is *not* activated.
+ *
+ * @node: The ofnode for which a associated device should be looked up
+ * @devp: Pointer to structure to hold the found device
+ * Return: 0 if OK, -ve on error
+ */
+static int device_find_by_ofnode(ofnode node, struct udevice **devp)
+{
+	struct uclass *uc;
+	struct udevice *dev;
+	int ret;
+
+	list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
+		ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
+						   &dev);
+		if (!ret || dev) {
+			*devp = dev;
+			return 0;
+		}
+	}
+
+	return -ENODEV;
+}
+
 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
 {
 	struct udevice *dev;
@@ -717,3 +744,54 @@ bool of_machine_is_compatible(const char *compat)
 
 	return !fdt_node_check_compatible(fdt, 0, compat);
 }
+
+int dev_disable_by_path(const char *path)
+{
+	struct uclass *uc;
+	ofnode node = ofnode_path(path);
+	struct udevice *dev;
+	int ret = 1;
+
+	if (!of_live_active())
+		return -ENOSYS;
+
+	list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
+		ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
+		if (!ret)
+			break;
+	}
+
+	if (ret)
+		return ret;
+
+	ret = device_remove(dev, DM_REMOVE_NORMAL);
+	if (ret)
+		return ret;
+
+	ret = device_unbind(dev);
+	if (ret)
+		return ret;
+
+	return ofnode_set_enabled(node, false);
+}
+
+int dev_enable_by_path(const char *path)
+{
+	ofnode node = ofnode_path(path);
+	ofnode pnode = ofnode_get_parent(node);
+	struct udevice *parent;
+	int ret = 1;
+
+	if (!of_live_active())
+		return -ENOSYS;
+
+	ret = device_find_by_ofnode(pnode, &parent);
+	if (ret)
+		return ret;
+
+	ret = ofnode_set_enabled(node, true);
+	if (ret)
+		return ret;
+
+	return lists_bind_fdt(parent, node, NULL);
+}
include/dm/device.h +16 -0
index 49078bc6b36..7d569f91979 100644
@@ -586,6 +586,22 @@ bool device_is_compatible(struct udevice *dev, const char *compat);
 bool of_machine_is_compatible(const char *compat);
 
 /**
+ * dev_disable_by_path() - Disable a device given its device tree path
+ *
+ * @path:	The device tree path identifying the device to be disabled
+ * @return 0 on success, -ve on error
+ */
+int dev_disable_by_path(const char *path);
+
+/**
+ * dev_enable_by_path() - Enable a device given its device tree path
+ *
+ * @path:	The device tree path identifying the device to be enabled
+ * @return 0 on success, -ve on error
+ */
+int dev_enable_by_path(const char *path);
+
+/**
  * device_is_on_pci_bus - Test if a device is on a PCI bus
  *
  * @dev:	device to test

--
2.11.0

#4 lore raw

PATCH v3 3/4 core: Add dev_{disable,enable}_by_path

Quoted 15 lines · Mario Six

We cannot use device structures to disable devices, since getting them with the API functions would bind and activate the device, which would fail if the underlying device does not exist.

---

Hence, add a function to disable devices by path in a live device tree.

v2 -> v3:
* Renamed res to ret
* Introduced device_find_by_ofnode() function
* Fixed error handling in some places

Quoted 9 lines

v1 -> v2:
* Simplified np_to_ofnode(of_find_node_by_path(path)) to ofnode_path(path)
* Switched to returning -ENOSYS if livetree is not enabled

---
 drivers/core/device.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/device.h   | 16 +++++++++++
 2 files changed, 94 insertions(+)
#5 lore raw

PATCH v3 3/4 core: Add dev_{disable,enable}_by_path

Quoted 20 lines · Simon Glass
Quoted 19 lines · Mario Six

We cannot use device structures to disable devices, since getting them with the API functions would bind and activate the device, which would fail if the underlying device does not exist.

---

Hence, add a function to disable devices by path in a live device tree.

v2 -> v3:
* Renamed res to ret
* Introduced device_find_by_ofnode() function
* Fixed error handling in some places

v1 -> v2:
* Simplified np_to_ofnode(of_find_node_by_path(path)) to ofnode_path(path)
* Switched to returning -ENOSYS if livetree is not enabled

Quoted 7 lines
Quoted 5 lines
---
 drivers/core/device.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/device.h   | 16 +++++++++++
 2 files changed, 94 insertions(+)

Applied to u-boot-dm, and now in mainline, thanks!

author
#6 lore raw

PATCH v3 4/4 test: Add tests for dev_{enable, disable}_by_path

In reply to Mario Six

Add tests for the dev_{enable,disable}_by_path functions.

---

v2 -> v3:
* Fixed style violations

v1 -> v2:
New in v2

---
 test/dm/test-fdt.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
test/dm/test-fdt.c +28 -0
index 1e3faf15227..dae99ed7e5c 100644
@@ -515,3 +515,31 @@ static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
+
+static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
+{
+	ofnode node;
+
+	if (!of_live_active()) {
+		printf("Live tree not active; ignore test\n");
+		return 0;
+	}
+
+	node = ofnode_path("/usb at 2");
+
+	/* Test enabling devices */
+
+	ut_assert(!of_device_is_available(ofnode_to_np(node)));
+	dev_enable_by_path("/usb at 2");
+	ut_assert(of_device_is_available(ofnode_to_np(node)));
+
+	/* Test disabling devices */
+
+	ut_assert(of_device_is_available(ofnode_to_np(node)));
+	dev_disable_by_path("/usb at 2");
+	ut_assert(!of_device_is_available(ofnode_to_np(node)));
+
+	return 0;
+}
+DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA |
+					    DM_TESTF_SCAN_FDT);

--
2.11.0

#7 lore raw

PATCH v3 4/4 test: Add tests for dev_{enable, disable}_by_path

Quoted 10 lines · Mario Six

Add tests for the dev_{enable,disable}_by_path functions.

---

v2 -> v3:
* Fixed style violations

Quoted 6 lines

v1 -> v2:
New in v2

---
 test/dm/test-fdt.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

Applied to u-boot-dm, and now in mainline, thanks!

#8 lore raw

PATCH v3 1/4 core: Add functions to set properties in live-tree

In reply to Mario Six
Quoted 23 lines · Mario Six

Implement a set of functions to manipulate properties in a live device tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

Quoted 4 lines
---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
author
#9 lore raw

PATCH v3 1/4 core: Add functions to set properties in live-tree

Hi Simon,

Quoted 25 lines · Simon Glass
Quoted 23 lines · Mario Six

Implement a set of functions to manipulate properties in a live device tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

Quoted 6 lines
Quoted 4 lines
---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)

This series seems to be a candidate for the u-boot-dm tree.

I can assign it to you in Patchwork if you want.

Best regards,
Mario

#10 lore raw

PATCH v3 1/4 core: Add functions to set properties in live-tree

Hi Mario,

Quoted 33 lines · Mario Six

Hi Simon,

Quoted 30 lines · Simon Glass
Quoted 27 lines · Mario Six

Implement a set of functions to manipulate properties in a live device tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
Quoted 5 lines
Quoted 1 line

This series seems to be a candidate for the u-boot-dm tree.

I can assign it to you in Patchwork if you want.

OK that's fine with me.

Regards,
Simon

author
#11 lore raw

PATCH v3 1/4 core: Add functions to set properties in live-tree

Hi Simon,

Quoted 37 lines · Simon Glass

Hi Mario,

Quoted 33 lines · Mario Six

Hi Simon,

Quoted 30 lines · Simon Glass
Quoted 27 lines · Mario Six

Implement a set of functions to manipulate properties in a live device tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

This series seems to be a candidate for the u-boot-dm tree.

I can assign it to you in Patchwork if you want.

OK that's fine with me.

Thanks. I reassigned the series.

Quoted 3 lines

Regards,
Simon

Best regards,
Mario

#12 lore raw

PATCH v3 1/4 core: Add functions to set properties in live-tree

Quoted 41 lines · Mario Six

Hi Simon,

Quoted 39 lines · Simon Glass

Hi Mario,

Quoted 35 lines · Mario Six

Hi Simon,

Quoted 31 lines · Simon Glass
Quoted 23 lines · Mario Six

Implement a set of functions to manipulate properties in a live device tree:

* ofnode_write_prop() to set generic properties of a node
* ofnode_write_string() to set string properties of a node
* ofnode_set_enabled() to either enable or disable a node

---

v2 -> v3:
* Removed #ifdef, and used if (!of_live_active()) instead
* Removed allocation of property value (caller is now responsible for that); this also fixes a potential memory leak
* Added error handling for malloc and strdup
* Fixed style violations

v1 -> v2:
* Fix potential NULL pointer dereference in ofnode_write_property
* Squashed the enable/disable functions into one
* Renamed ofnode_set_property to ofnode_write_prop

Quoted 4 lines
---
 drivers/core/ofnode.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/ofnode.h   | 46 +++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+)
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

This series seems to be a candidate for the u-boot-dm tree.

I can assign it to you in Patchwork if you want.

OK that's fine with me.

Thanks. I reassigned the series.

Applied to u-boot-dm, and now in mainline, thanks!