device property: Add a function to obtain a node's prefix
authorSakari Ailus <sakari.ailus@linux.intel.com>
Thu, 3 Oct 2019 12:32:13 +0000 (15:32 +0300)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Fri, 11 Oct 2019 09:26:55 +0000 (11:26 +0200)
The prefix is used for printing purpose before a node, and it also works
as a separator between two nodes.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Acked-by: Rob Herring <robh@kernel.org> (for OF)
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/acpi/property.c
drivers/base/property.c
drivers/base/swnode.c
drivers/of/property.c
include/linux/fwnode.h
include/linux/property.h

index a501c1c..e601c45 100644 (file)
@@ -1342,6 +1342,27 @@ static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
        return acpi_device_bid(adev);
 }
 
+static const char *
+acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
+{
+       struct fwnode_handle *parent;
+
+       /* Is this the root node? */
+       parent = fwnode_get_parent(fwnode);
+       if (!parent)
+               return "";
+
+       /* Is this 2nd node from the root? */
+       parent = fwnode_get_next_parent(parent);
+       if (!parent)
+               return "";
+
+       fwnode_handle_put(parent);
+
+       /* ACPI device or data node. */
+       return ".";
+}
+
 static struct fwnode_handle *
 acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
 {
@@ -1383,6 +1404,7 @@ acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
                .get_next_child_node = acpi_get_next_subnode,           \
                .get_named_child_node = acpi_fwnode_get_named_child_node, \
                .get_name = acpi_fwnode_get_name,                       \
+               .get_name_prefix = acpi_fwnode_get_name_prefix,         \
                .get_reference_args = acpi_fwnode_get_reference_args,   \
                .graph_get_next_endpoint =                              \
                        acpi_graph_get_next_endpoint,                   \
index 9b5ec88..511f6d7 100644 (file)
@@ -567,6 +567,18 @@ const char *fwnode_get_name(const struct fwnode_handle *fwnode)
        return fwnode_call_ptr_op(fwnode, get_name);
 }
 
+/**
+ * fwnode_get_name_prefix - Return the prefix of node for printing purposes
+ * @fwnode: The firmware node
+ *
+ * Returns the prefix of a node, intended to be printed right before the node.
+ * The prefix works also as a separator between the nodes.
+ */
+const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
+{
+       return fwnode_call_ptr_op(fwnode, get_name_prefix);
+}
+
 /**
  * fwnode_get_parent - Return parent firwmare node
  * @fwnode: Firmware whose parent is retrieved
index eacca6a..12ca439 100644 (file)
@@ -526,6 +526,27 @@ software_node_get_name(const struct fwnode_handle *fwnode)
        return kobject_name(&swnode->kobj);
 }
 
+static const char *
+software_node_get_name_prefix(const struct fwnode_handle *fwnode)
+{
+       struct fwnode_handle *parent;
+       const char *prefix;
+
+       parent = fwnode_get_parent(fwnode);
+       if (!parent)
+               return "";
+
+       /* Figure out the prefix from the parents. */
+       while (is_software_node(parent))
+               parent = fwnode_get_next_parent(parent);
+
+       prefix = fwnode_get_name_prefix(parent);
+       fwnode_handle_put(parent);
+
+       /* Guess something if prefix was NULL. */
+       return prefix ?: "/";
+}
+
 static struct fwnode_handle *
 software_node_get_parent(const struct fwnode_handle *fwnode)
 {
@@ -627,6 +648,7 @@ static const struct fwnode_operations software_node_ops = {
        .property_read_int_array = software_node_read_int_array,
        .property_read_string_array = software_node_read_string_array,
        .get_name = software_node_get_name,
+       .get_name_prefix = software_node_get_name_prefix,
        .get_parent = software_node_get_parent,
        .get_next_child_node = software_node_get_next_child,
        .get_named_child_node = software_node_get_named_child_node,
index 5bed634..e8202f6 100644 (file)
@@ -877,6 +877,15 @@ static const char *of_fwnode_get_name(const struct fwnode_handle *fwnode)
        return kbasename(to_of_node(fwnode)->full_name);
 }
 
+static const char *of_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
+{
+       /* Root needs no prefix here (its name is "/"). */
+       if (!to_of_node(fwnode)->parent)
+               return "";
+
+       return "/";
+}
+
 static struct fwnode_handle *
 of_fwnode_get_parent(const struct fwnode_handle *fwnode)
 {
@@ -999,6 +1008,7 @@ const struct fwnode_operations of_fwnode_ops = {
        .property_read_int_array = of_fwnode_property_read_int_array,
        .property_read_string_array = of_fwnode_property_read_string_array,
        .get_name = of_fwnode_get_name,
+       .get_name_prefix = of_fwnode_get_name_prefix,
        .get_parent = of_fwnode_get_parent,
        .get_next_child_node = of_fwnode_get_next_child_node,
        .get_named_child_node = of_fwnode_get_named_child_node,
index 2bbf557..a5673c4 100644 (file)
@@ -57,6 +57,7 @@ struct fwnode_reference_args {
  * @property_read_string_array: Read an array of string properties. Return zero
  *                             on success, a negative error code otherwise.
  * @get_name: Return the name of an fwnode.
+ * @get_name_prefix: Get a prefix for a node (for printing purposes).
  * @get_parent: Return the parent of an fwnode.
  * @get_next_child_node: Return the next child node in an iteration.
  * @get_named_child_node: Return a child node with a given name.
@@ -84,6 +85,7 @@ struct fwnode_operations {
                                      const char *propname, const char **val,
                                      size_t nval);
        const char *(*get_name)(const struct fwnode_handle *fwnode);
+       const char *(*get_name_prefix)(const struct fwnode_handle *fwnode);
        struct fwnode_handle *(*get_parent)(const struct fwnode_handle *fwnode);
        struct fwnode_handle *
        (*get_next_child_node)(const struct fwnode_handle *fwnode,
index afa84c4..0546611 100644 (file)
@@ -81,6 +81,7 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
                                            unsigned int index);
 
 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
+const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
 struct fwnode_handle *fwnode_get_next_parent(
        struct fwnode_handle *fwnode);