diff mbox series

[v2,1/1] device property: Add fwnode_name_eq()

Message ID 20231101072729.1142578-1-sakari.ailus@linux.intel.com
State Superseded
Headers show
Series [v2,1/1] device property: Add fwnode_name_eq() | expand

Commit Message

Sakari Ailus Nov. 1, 2023, 7:27 a.m. UTC
Add fwnode_name_eq() to implement the functionality of of_node_name_eq()
on fwnode property API. The same convention of ending the comparison at
'@' (besides '\0') is applied on also both ACPI and swnode. The function
is intended for comparing unit address-less node names on DT and firmware
or swnodes compliant with DT bindings.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
since v1:

- Use str_has_prefix().

- Fix kernel-doc for @fwnode argument.

- Use size_t type for len.

The patch is untested.

 drivers/base/property.c  | 28 ++++++++++++++++++++++++++++
 include/linux/property.h |  1 +
 2 files changed, 29 insertions(+)

Comments

Andy Shevchenko Nov. 1, 2023, 9:50 a.m. UTC | #1
On Wed, Nov 01, 2023 at 09:27:29AM +0200, Sakari Ailus wrote:
> Add fwnode_name_eq() to implement the functionality of of_node_name_eq()
> on fwnode property API. The same convention of ending the comparison at
> '@' (besides '\0') is applied on also both ACPI and swnode. The function
> is intended for comparing unit address-less node names on DT and firmware
> or swnodes compliant with DT bindings.

...

> - Use size_t type for len.

Strictly speaking it should be ptrdiff_t according to the code.

...

> + * the comparison to either '\0' or '@' character

NUL is a human-readable substitution to less parsable '\0'.
Sakari Ailus Nov. 1, 2023, 10 a.m. UTC | #2
On Wed, Nov 01, 2023 at 11:50:00AM +0200, Andy Shevchenko wrote:
> On Wed, Nov 01, 2023 at 09:27:29AM +0200, Sakari Ailus wrote:
> > Add fwnode_name_eq() to implement the functionality of of_node_name_eq()
> > on fwnode property API. The same convention of ending the comparison at
> > '@' (besides '\0') is applied on also both ACPI and swnode. The function
> > is intended for comparing unit address-less node names on DT and firmware
> > or swnodes compliant with DT bindings.
> 
> ...
> 
> > - Use size_t type for len.
> 
> Strictly speaking it should be ptrdiff_t according to the code.

It'll be compared to size_t right after. ptrdiff_t is signed and we know we
have a positive number here so size_t seems like a better choice.

> 
> ...
> 
> > + * the comparison to either '\0' or '@' character
> 
> NUL is a human-readable substitution to less parsable '\0'.

I can send v3 with that but I'll wait a bit if there are more comments.
Laurent Pinchart Nov. 2, 2023, 12:51 a.m. UTC | #3
Hi Sakari,

Thank you for the patch.

On Wed, Nov 01, 2023 at 09:27:29AM +0200, Sakari Ailus wrote:
> Add fwnode_name_eq() to implement the functionality of of_node_name_eq()
> on fwnode property API. The same convention of ending the comparison at
> '@' (besides '\0') is applied on also both ACPI and swnode. The function
> is intended for comparing unit address-less node names on DT and firmware
> or swnodes compliant with DT bindings.
> 
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Tested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
> since v1:
> 
> - Use str_has_prefix().
> 
> - Fix kernel-doc for @fwnode argument.
> 
> - Use size_t type for len.
> 
> The patch is untested.

Not anymore :-)

Which tree will this get merged through ? The thp7312 driver depends on
this patch, and I would like to get it in v6.8. If we can merge this
through the media tree it could be the simplest option, otherwise I'll
need a stable branch based on v6.7-rc1.

>  drivers/base/property.c  | 28 ++++++++++++++++++++++++++++
>  include/linux/property.h |  1 +
>  2 files changed, 29 insertions(+)
> 
> diff --git a/drivers/base/property.c b/drivers/base/property.c
> index 8c40abed7852..89a6e0833356 100644
> --- a/drivers/base/property.c
> +++ b/drivers/base/property.c
> @@ -594,6 +594,34 @@ const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
>  	return fwnode_call_ptr_op(fwnode, get_name_prefix);
>  }
>  
> +/**
> + * fwnode_name_eq - Return true if node name is equal
> + * @fwnode: The firmware node
> + * @name: The name to which to compare the node name
> + *
> + * Compare the name provided as an argument to the name of the node, stopping
> + * the comparison to either '\0' or '@' character, whichever comes first. This
> + * function is generally used for comparing node names while ignoring the
> + * possible unit address of the node.
> + *
> + * Return: true if the node name matches with the name provided in the @name
> + * argument, false otherwise.
> + */
> +bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
> +{
> +	const char *node_name;
> +	size_t len;
> +
> +	node_name = fwnode_get_name(fwnode);
> +	if (!node_name)
> +		return false;
> +
> +	len = strchrnul(node_name, '@') - node_name;
> +
> +	return str_has_prefix(node_name, name) == len;
> +}
> +EXPORT_SYMBOL_GPL(fwnode_name_eq);
> +
>  /**
>   * fwnode_get_parent - Return parent firwmare node
>   * @fwnode: Firmware whose parent is retrieved
> diff --git a/include/linux/property.h b/include/linux/property.h
> index 083a1f41364b..096ade186601 100644
> --- a/include/linux/property.h
> +++ b/include/linux/property.h
> @@ -108,6 +108,7 @@ struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
>  
>  const char *fwnode_get_name(const struct fwnode_handle *fwnode);
>  const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
> +bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name);
>  
>  struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
>  struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);
Andy Shevchenko Nov. 2, 2023, 12:57 p.m. UTC | #4
On Wed, Nov 01, 2023 at 10:00:15AM +0000, Sakari Ailus wrote:
> On Wed, Nov 01, 2023 at 11:50:00AM +0200, Andy Shevchenko wrote:
> > On Wed, Nov 01, 2023 at 09:27:29AM +0200, Sakari Ailus wrote:

...

> > > - Use size_t type for len.
> > 
> > Strictly speaking it should be ptrdiff_t according to the code.
> 
> It'll be compared to size_t right after. ptrdiff_t is signed and we know we
> have a positive number here so size_t seems like a better choice.

Yeah, this is downsides of C language and typing system.
But diff between pointers returns ptrdiff_t result.

...

> > > + * the comparison to either '\0' or '@' character
> > 
> > NUL is a human-readable substitution to less parsable '\0'.
> 
> I can send v3 with that but I'll wait a bit if there are more comments.

Why not? You got tags, so I'll give mine with this being addressed.
diff mbox series

Patch

diff --git a/drivers/base/property.c b/drivers/base/property.c
index 8c40abed7852..89a6e0833356 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -594,6 +594,34 @@  const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
 	return fwnode_call_ptr_op(fwnode, get_name_prefix);
 }
 
+/**
+ * fwnode_name_eq - Return true if node name is equal
+ * @fwnode: The firmware node
+ * @name: The name to which to compare the node name
+ *
+ * Compare the name provided as an argument to the name of the node, stopping
+ * the comparison to either '\0' or '@' character, whichever comes first. This
+ * function is generally used for comparing node names while ignoring the
+ * possible unit address of the node.
+ *
+ * Return: true if the node name matches with the name provided in the @name
+ * argument, false otherwise.
+ */
+bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name)
+{
+	const char *node_name;
+	size_t len;
+
+	node_name = fwnode_get_name(fwnode);
+	if (!node_name)
+		return false;
+
+	len = strchrnul(node_name, '@') - node_name;
+
+	return str_has_prefix(node_name, name) == len;
+}
+EXPORT_SYMBOL_GPL(fwnode_name_eq);
+
 /**
  * fwnode_get_parent - Return parent firwmare node
  * @fwnode: Firmware whose parent is retrieved
diff --git a/include/linux/property.h b/include/linux/property.h
index 083a1f41364b..096ade186601 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -108,6 +108,7 @@  struct fwnode_handle *fwnode_find_reference(const struct fwnode_handle *fwnode,
 
 const char *fwnode_get_name(const struct fwnode_handle *fwnode);
 const char *fwnode_get_name_prefix(const struct fwnode_handle *fwnode);
+bool fwnode_name_eq(const struct fwnode_handle *fwnode, const char *name);
 
 struct fwnode_handle *fwnode_get_parent(const struct fwnode_handle *fwnode);
 struct fwnode_handle *fwnode_get_next_parent(struct fwnode_handle *fwnode);