[PATCH 2/3] device property: add {device,fwnode}_property_match_string_nocase()

From: Soha Jin
Date: Sun Oct 09 2022 - 12:22:50 EST


device_property_match_string_nocase and fwnode_property_match_string_nocase
are used to do case-insensitive match with match_string_nocase.

Signed-off-by: Soha Jin <soha@xxxxxxxxx>
---
drivers/base/property.c | 92 ++++++++++++++++++++++++++++++++--------
include/linux/property.h | 6 +++
2 files changed, 80 insertions(+), 18 deletions(-)

diff --git a/drivers/base/property.c b/drivers/base/property.c
index 4d6278a84868..5e579b915e77 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -242,6 +242,30 @@ int device_property_match_string(struct device *dev, const char *propname,
}
EXPORT_SYMBOL_GPL(device_property_match_string);

+/**
+ * device_property_match_string_nocase - find a string in an array and return
+ * index with case-insensitive comparison
+ * @dev: Device to get the property of
+ * @propname: Name of the property holding the array
+ * @string: String to look for
+ *
+ * Find a given string in a string array and if it is found return the
+ * index back.
+ *
+ * Return: %0 if the property was found (success),
+ * %-EINVAL if given arguments are not valid,
+ * %-ENODATA if the property does not have a value,
+ * %-EPROTO if the property is not an array of strings,
+ * %-ENXIO if no suitable firmware interface is present.
+ */
+int device_property_match_string_nocase(struct device *dev,
+ const char *propname, const char *string)
+{
+ return fwnode_property_match_string_nocase(dev_fwnode(dev), propname,
+ string);
+}
+EXPORT_SYMBOL_GPL(device_property_match_string_nocase);
+
static int fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
const char *propname,
unsigned int elem_size, void *val,
@@ -441,23 +465,9 @@ int fwnode_property_read_string(const struct fwnode_handle *fwnode,
}
EXPORT_SYMBOL_GPL(fwnode_property_read_string);

-/**
- * fwnode_property_match_string - find a string in an array and return index
- * @fwnode: Firmware node to get the property of
- * @propname: Name of the property holding the array
- * @string: String to look for
- *
- * Find a given string in a string array and if it is found return the
- * index back.
- *
- * Return: %0 if the property was found (success),
- * %-EINVAL if given arguments are not valid,
- * %-ENODATA if the property does not have a value,
- * %-EPROTO if the property is not an array of strings,
- * %-ENXIO if no suitable firmware interface is present.
- */
-int fwnode_property_match_string(const struct fwnode_handle *fwnode,
- const char *propname, const char *string)
+int fwnode_property_do_match_string(const struct fwnode_handle *fwnode,
+ const char *propname, const char *string,
+ int (*cmp)(const char *, const char *))
{
const char **values;
int nval, ret;
@@ -477,15 +487,61 @@ int fwnode_property_match_string(const struct fwnode_handle *fwnode,
if (ret < 0)
goto out;

- ret = match_string(values, nval, string);
+ ret = __match_string(values, nval, string, cmp);
if (ret < 0)
ret = -ENODATA;
out:
kfree(values);
return ret;
}
+
+/**
+ * fwnode_property_match_string - find a string in an array and return index
+ * @fwnode: Firmware node to get the property of
+ * @propname: Name of the property holding the array
+ * @string: String to look for
+ *
+ * Find a given string in a string array and if it is found return the
+ * index back.
+ *
+ * Return: %0 if the property was found (success),
+ * %-EINVAL if given arguments are not valid,
+ * %-ENODATA if the property does not have a value,
+ * %-EPROTO if the property is not an array of strings,
+ * %-ENXIO if no suitable firmware interface is present.
+ */
+int fwnode_property_match_string(const struct fwnode_handle *fwnode,
+ const char *propname, const char *string)
+{
+ return fwnode_property_do_match_string(fwnode, propname, string,
+ strcmp);
+}
EXPORT_SYMBOL_GPL(fwnode_property_match_string);

+/**
+ * fwnode_property_match_string_nocase - find a string in an array and return
+ * index with case-insensitive comparison
+ * @fwnode: Firmware node to get the property of
+ * @propname: Name of the property holding the array
+ * @string: String to look for
+ *
+ * Find a given string in a string array and if it is found return the
+ * index back.
+ *
+ * Return: %0 if the property was found (success),
+ * %-EINVAL if given arguments are not valid,
+ * %-ENODATA if the property does not have a value,
+ * %-EPROTO if the property is not an array of strings,
+ * %-ENXIO if no suitable firmware interface is present.
+ */
+int fwnode_property_match_string_nocase(const struct fwnode_handle *fwnode,
+ const char *propname, const char *string)
+{
+ return fwnode_property_do_match_string(fwnode, propname, string,
+ strcasecmp);
+}
+EXPORT_SYMBOL_GPL(fwnode_property_match_string_nocase);
+
/**
* fwnode_property_get_reference_args() - Find a reference with arguments
* @fwnode: Firmware node where to look for the reference
diff --git a/include/linux/property.h b/include/linux/property.h
index 117cc200c656..dbe747f3e3be 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -49,6 +49,9 @@ int device_property_read_string(struct device *dev, const char *propname,
const char **val);
int device_property_match_string(struct device *dev,
const char *propname, const char *string);
+int device_property_match_string_nocase(struct device *dev,
+ const char *propname,
+ const char *string);

bool fwnode_device_is_available(const struct fwnode_handle *fwnode);
bool fwnode_property_present(const struct fwnode_handle *fwnode,
@@ -72,6 +75,9 @@ int fwnode_property_read_string(const struct fwnode_handle *fwnode,
const char *propname, const char **val);
int fwnode_property_match_string(const struct fwnode_handle *fwnode,
const char *propname, const char *string);
+int fwnode_property_match_string_nocase(const struct fwnode_handle *fwnode,
+ const char *propname,
+ const char *string);
int fwnode_property_get_reference_args(const struct fwnode_handle *fwnode,
const char *prop, const char *nargs_prop,
unsigned int nargs, unsigned int index,
--
2.30.2