Skip to contentSkip to conversation

device property: Add fwnode_get_name() helper

PATCH Superseded Heikki Krogerus 2 participants 8 messages Nov 26, 2018

Subsystems (4)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Heikki Krogerus

author reviewer
#0 lore raw

v3 cover device property: Add fwnode_get_name() helper

Hi,

This is the third version of my proposal for this helper. The second version can be checked here:
https://lkml.org/lkml/2018/11/8/1005

I'm now passing the length as proposed by both Rob and Andy. I'm also leaving the .get_named_child_node fwnode op untouched.

--
heikki

Heikki Krogerus (3):
device property: Introduce fwnode_get_name()
ACPI: property: Add acpi_fwnode_name()
of/property: Add of_fwnode_name()

 drivers/acpi/property.c  | 24 ++++++++++++++++++++++++
 drivers/base/property.c  | 26 ++++++++++++++++++++++++++
 drivers/of/property.c    |  8 ++++++++
 include/linux/fwnode.h   |  3 +++
 include/linux/property.h |  2 ++
 5 files changed, 63 insertions(+)
Signature · 1 line
2.19.1
author reviewer
#1 lore raw

PATCH v3 1/3 device property: Introduce fwnode_get_name()

This helper returns the name of the node. The name is primarily expected to be returned from a new fwnode operation meant for this purpose, but when no name is returned, the helper will also attempt to read a device property "name".

---
 drivers/base/property.c  | 26 ++++++++++++++++++++++++++
 include/linux/fwnode.h   |  3 +++
 include/linux/property.h |  2 ++
 3 files changed, 31 insertions(+)
drivers/base/property.c +26 -0
index 240ab5230ff6..f4a3270b3de6 100644
@@ -1156,6 +1156,32 @@ void fwnode_handle_put(struct fwnode_handle *fwnode)
 }
 EXPORT_SYMBOL_GPL(fwnode_handle_put);
 
+/**
+ * fwnode_get_name - Read the name of a node
+ * @fwnode: Pointer to the node.
+ * @buf: Buffer where the name is copied to
+ * @len: Size of the buffer
+ *
+ * Reads the node name of @fwnode with get_name fwnode op. If get_name op fails,
+ * the routine attempts to also read a property "name".
+ */
+int fwnode_get_name(const struct fwnode_handle *fwnode, char *buf, size_t len)
+{
+	const char *name;
+	int ret;
+
+	if (!fwnode_call_int_op(fwnode, get_name, buf, len))
+		return 0;
+
+	ret = fwnode_call_int_op(fwnode, property_read_string_array,
+				 "name", &name, 1);
+	if (!ret)
+		snprintf(buf, len, "%s", name);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(fwnode_get_name);
+
 /**
  * fwnode_device_is_available - check if a device is available for use
  * @fwnode: Pointer to the fwnode of the device.
include/linux/fwnode.h +3 -0
index faebf0ca0686..a211b55a5da1 100644
@@ -52,6 +52,7 @@ struct fwnode_reference_args {
  * struct fwnode_operations - Operations for fwnode interface
  * @get: Get a reference to an fwnode.
  * @put: Put a reference to an fwnode.
+ * @get_name: Get the name of an fwnode
  * @device_get_match_data: Return the device driver match data.
  * @property_present: Return true if a property is present.
  * @property_read_integer_array: Read an array of integer properties. Return
@@ -72,6 +73,8 @@ struct fwnode_reference_args {
 struct fwnode_operations {
 	struct fwnode_handle *(*get)(struct fwnode_handle *fwnode);
 	void (*put)(struct fwnode_handle *fwnode);
+	int (*get_name)(const struct fwnode_handle *fwnode, char *buf,
+			size_t len);
 	bool (*device_is_available)(const struct fwnode_handle *fwnode);
 	const void *(*device_get_match_data)(const struct fwnode_handle *fwnode,
 					     const struct device *dev);
include/linux/property.h +2 -0
index ac8a1ebc4c1b..e77f08f49c48 100644
@@ -109,6 +109,8 @@ struct fwnode_handle *device_get_named_child_node(struct device *dev,
 struct fwnode_handle *fwnode_handle_get(struct fwnode_handle *fwnode);
 void fwnode_handle_put(struct fwnode_handle *fwnode);
 
+int fwnode_get_name(const struct fwnode_handle *fwnode, char *name, size_t len);
+
 int fwnode_irq_get(struct fwnode_handle *fwnode, unsigned int index);
 
 unsigned int device_get_child_node_count(struct device *dev);
Signature · 1 line
2.19.1
author reviewer
#3 lore raw

PATCH v3 2/3 ACPI: property: Add acpi_fwnode_name()

In reply to Heikki Krogerus

This implements the get_name fwnode op for ACPI.

---
 drivers/acpi/property.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/acpi/property.c +24 -0
index 8c7c4583b52d..a38daa6f32cb 100644
@@ -1219,6 +1219,29 @@ acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
 	return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
 }
 
+static int
+acpi_fwnode_get_name(const struct fwnode_handle *fwnode, char *buf, size_t len)
+{
+	struct acpi_buffer buffer;
+	acpi_handle handle;
+	acpi_status status;
+
+	if (is_acpi_data_node(fwnode)) {
+		snprintf(buf, len, "%s", to_acpi_data_node(fwnode)->name);
+		return 0;
+	}
+
+	handle = to_acpi_device_node(fwnode)->handle;
+
+	buffer.length = min((size_t)ACPI_NAME_SIZE + 1, len);
+	buffer.pointer = buf;
+
+	status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
+	if (ACPI_FAILURE(status))
+		return -ENXIO;
+	return 0;
+}
+
 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
 {
 	if (!is_acpi_device_node(fwnode))
@@ -1310,6 +1333,7 @@ acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
 
 #define DECLARE_ACPI_FWNODE_OPS(ops) \
 	const struct fwnode_operations ops = {				\
+		.get_name = acpi_fwnode_get_name,			\
 		.device_is_available = acpi_fwnode_device_is_available, \
 		.device_get_match_data = acpi_fwnode_device_get_match_data, \
 		.property_present = acpi_fwnode_property_present,	\
Signature · 1 line
2.19.1
author reviewer
#5 lore raw

PATCH v3 3/3 of/property: Add of_fwnode_name()

In reply to Heikki Krogerus

This implements get_name fwnode op for DT.

---
 drivers/of/property.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/of/property.c +8 -0
index f46828e3b082..253ed0e9d804 100644
@@ -823,6 +823,13 @@ static void of_fwnode_put(struct fwnode_handle *fwnode)
 	of_node_put(to_of_node(fwnode));
 }
 
+static int of_fwnode_get_name(const struct fwnode_handle *fwnode, char *buf,
+			      size_t len)
+{
+	snprintf(buf, len, "%pOFn", to_of_node(fwnode));
+	return 0;
+}
+
 static bool of_fwnode_device_is_available(const struct fwnode_handle *fwnode)
 {
 	return of_device_is_available(to_of_node(fwnode));
@@ -987,6 +994,7 @@ of_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
 const struct fwnode_operations of_fwnode_ops = {
 	.get = of_fwnode_get,
 	.put = of_fwnode_put,
+	.get_name = of_fwnode_get_name,
 	.device_is_available = of_fwnode_device_is_available,
 	.device_get_match_data = of_fwnode_device_get_match_data,
 	.property_present = of_fwnode_property_present,
Signature · 1 line
2.19.1
#2 lore raw

Re: [PATCH v3 1/3] device property: Introduce fwnode_get_name()

In reply to Heikki Krogerus
Quoted 8 lines · Heikki Krogerus

This helper returns the name of the node. The name is primarily expected to be returned from a new fwnode operation meant for this purpose, but when no name is returned, the helper will also attempt to read a device property "name".

drivers/base/property.c +2 -0
+int fwnode_get_name(const struct fwnode_handle *fwnode, char *buf, size_t len)
+{

drivers/base/property.c void fwnode_handle_put(struct fwnode_handle *fwnode) · line 1179

Quoted 10 lines
drivers/base/property.c +10 -0
+	const char *name;
+	int ret;
+
+	if (!fwnode_call_int_op(fwnode, get_name, buf, len))
+		return 0;
+
+	ret = fwnode_call_int_op(fwnode, property_read_string_array,
+				 "name", &name, 1);
+	if (!ret)
+		snprintf(buf, len, "%s", name);

What are we going to do in case snprintf has found no room for entire string?

Quoted 3 lines
drivers/base/property.c +3 -0
+
+	return ret;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH v3 2/3] ACPI: property: Add acpi_fwnode_name()

In reply to Heikki Krogerus

drivers/acpi/property.c acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode) · line 1230

Quoted 11 lines · Heikki Krogerus

This implements the get_name fwnode op for ACPI.

drivers/acpi/property.c +9 -0
+static int
+acpi_fwnode_get_name(const struct fwnode_handle *fwnode, char *buf, size_t len)
+{
+	struct acpi_buffer buffer;
+	acpi_handle handle;
+	acpi_status status;
+
+	if (is_acpi_data_node(fwnode)) {
+		snprintf(buf, len, "%s", to_acpi_data_node(fwnode)->name);

Same question as per patch 1. How are we going to handle bigger strings?

drivers/acpi/property.c acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode) · line 1236

Quoted 7 lines
drivers/acpi/property.c +6 -0
+		return 0;
+	}
+
+	handle = to_acpi_device_node(fwnode)->handle;
+
 
+	buffer.length = min((size_t)ACPI_NAME_SIZE + 1, len);

Hmm...

min_t(size_t, ...) ?

drivers/acpi/property.c acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode) · line 1239

Quoted 3 lines
drivers/acpi/property.c +3 -0
+	buffer.pointer = buf;
+
+	status = acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);

And same question here. Is it possible to get length of the name and check?

Perhaps, I would return -ENAMETOOLONG (-EOVERFLOW) or alike for such cases.

Quoted 4 lines
drivers/acpi/property.c +4 -0
+	if (ACPI_FAILURE(status))
+		return -ENXIO;
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v3 3/3] of/property: Add of_fwnode_name()

In reply to Heikki Krogerus

drivers/of/property.c static void of_fwnode_put(struct fwnode_handle *fwnode) · line 829

Quoted 6 lines · Heikki Krogerus

This implements get_name fwnode op for DT.

drivers/of/property.c +4 -0
+static int of_fwnode_get_name(const struct fwnode_handle *fwnode, char *buf,
+			      size_t len)
+{
+	snprintf(buf, len, "%pOFn", to_of_node(fwnode));

Same concerns.

Quoted 2 lines
drivers/of/property.c +2 -0
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author reviewer
#7 lore raw

Re: [PATCH v3 3/3] of/property: Add of_fwnode_name()

Quoted 9 lines · Andy Shevchenko

drivers/of/property.c static void of_fwnode_put(struct fwnode_handle *fwnode) · line 829

Quoted 6 lines · Heikki Krogerus

This implements get_name fwnode op for DT.

drivers/of/property.c +4 -0
+static int of_fwnode_get_name(const struct fwnode_handle *fwnode, char *buf,
+			      size_t len)
+{
+	snprintf(buf, len, "%pOFn", to_of_node(fwnode));

Same concerns.

OK, I'll fix all of these.

thanks,

Signature · 1 line
heikki