Skip to contentSkip to conversation

device property: Add helpers to count items in an array

PATCH Superseded Andy Shevchenko 3 participants 4 messages May 20, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

PATCH 1/1 device property: Add helpers to count items in an array

The usual pattern to allocate the necessary space for an array of properties is to count them fist using:

  count = device_property_read_uXX_array(dev, propname, NULL, 0);

Introduce helpers device_property_count_uXX() to count items by supplying hard coded last two parameters to device_property_readXX_array().

---
 include/linux/property.h | 44 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
include/linux/property.h +44 -0
index a29369c89e6e..65e31c090f9f 100644
@@ -144,6 +144,26 @@ static inline int device_property_read_u64(struct device *dev,
 	return device_property_read_u64_array(dev, propname, val, 1);
 }
 
+static inline int device_property_count_u8(struct device *dev, const char *propname)
+{
+	return device_property_read_u8_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u16(struct device *dev, const char *propname)
+{
+	return device_property_read_u16_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u32(struct device *dev, const char *propname)
+{
+	return device_property_read_u32_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u64(struct device *dev, const char *propname)
+{
+	return device_property_read_u64_array(dev, propname, NULL, 0);
+}
+
 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
 					     const char *propname)
 {
@@ -174,6 +194,30 @@ static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
 	return fwnode_property_read_u64_array(fwnode, propname, val, 1);
 }
 
+static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
+					   const char *propname)
+{
+	return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
+}
+
 /**
  * struct property_entry - "Built-in" device property representation.
  * @name: Name of the property.
Signature · 1 line
2.20.1
reviewer Acked-by on v1 1/1
#1 lore raw

Hi Andy,

Quoted 2 lines · Andy Shevchenko

The usual pattern to allocate the necessary space for an array of properties is to count them fist using:

s/fist/first/

With that,

Quoted 76 lines
  count = device_property_read_uXX_array(dev, propname, NULL, 0);

Introduce helpers device_property_count_uXX() to count items by supplying hard coded last two parameters to device_property_readXX_array().

---
 include/linux/property.h | 44 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
include/linux/property.h +44 -0
index a29369c89e6e..65e31c090f9f 100644
@@ -144,6 +144,26 @@ static inline int device_property_read_u64(struct device *dev,
 	return device_property_read_u64_array(dev, propname, val, 1);
 }
 
+static inline int device_property_count_u8(struct device *dev, const char *propname)
+{
+	return device_property_read_u8_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u16(struct device *dev, const char *propname)
+{
+	return device_property_read_u16_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u32(struct device *dev, const char *propname)
+{
+	return device_property_read_u32_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u64(struct device *dev, const char *propname)
+{
+	return device_property_read_u64_array(dev, propname, NULL, 0);
+}
+
 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
 					     const char *propname)
 {
@@ -174,6 +194,30 @@ static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
 	return fwnode_property_read_u64_array(fwnode, propname, val, 1);
 }
 
+static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
+					   const char *propname)
+{
+	return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
+}
+
 /**
  * struct property_entry - "Built-in" device property representation.
  * @name: Name of the property.
Signature · 1 line
2.20.1
Signature · 2 lines
Sakari Ailus
[email protected]
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Andy Shevchenko
Quoted 9 lines · Andy Shevchenko

The usual pattern to allocate the necessary space for an array of properties is to count them fist using:

  count = device_property_read_uXX_array(dev, propname, NULL, 0);

Introduce helpers device_property_count_uXX() to count items by supplying hard coded last two parameters to device_property_readXX_array().

OK by me. FWIW:

Quoted 56 lines
---
 include/linux/property.h | 44 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
include/linux/property.h +37 -0
index a29369c89e6e..65e31c090f9f 100644
@@ -144,6 +144,26 @@ static inline int device_property_read_u64(struct device *dev,
 	return device_property_read_u64_array(dev, propname, val, 1);
 }
 
+static inline int device_property_count_u8(struct device *dev, const char *propname)
+{
+	return device_property_read_u8_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u16(struct device *dev, const char *propname)
+{
+	return device_property_read_u16_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u32(struct device *dev, const char *propname)
+{
+	return device_property_read_u32_array(dev, propname, NULL, 0);
+}
+
+static inline int device_property_count_u64(struct device *dev, const char *propname)
+{
+	return device_property_read_u64_array(dev, propname, NULL, 0);
+}
+
 static inline bool fwnode_property_read_bool(const struct fwnode_handle *fwnode,
 					     const char *propname)
 {
@@ -174,6 +194,30 @@ static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode,
 	return fwnode_property_read_u64_array(fwnode, propname, val, 1);
 }
 
+static inline int fwnode_property_count_u8(const struct fwnode_handle *fwnode,
+					   const char *propname)
+{
+	return fwnode_property_read_u8_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u16(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u16_array(fwnode, propname, NULL, 0);
+}
+
+static inline int fwnode_property_count_u32(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u32_array(fwnode, propname, NULL, 0);
+}

include/linux/property.h static inline int fwnode_property_read_u64(const struct fwnode_handle *fwnode, · line 223

Quoted 10 lines
include/linux/property.h +7 -0
+
+static inline int fwnode_property_count_u64(const struct fwnode_handle *fwnode,
+					    const char *propname)
+{
+	return fwnode_property_read_u64_array(fwnode, propname, NULL, 0);
+}
+
 /**
  * struct property_entry - "Built-in" device property representation.
  * @name: Name of the property.

Off topic question: Shouldn't we also be able to read the number of references a reference property holds? I mean, shouldn't fwnode_property_get_referece_args() also return the number of references in the property if called without the value parameter?

There can be "empty" references in the middle of the "array" of references which cause the function to return -ENOENT just like when called with index out of bounds, so the caller now has in practice know how many references the property actually has in advance.

thanks,

Signature · 1 line
heikki
author reviewer
#3 lore raw
Quoted 6 lines · Heikki Krogerus
Quoted 5 lines · Andy Shevchenko

The usual pattern to allocate the necessary space for an array of properties is to count them fist using:

  count = device_property_read_uXX_array(dev, propname, NULL, 0);
Quoted 8 lines
Quoted 4 lines

Introduce helpers device_property_count_uXX() to count items by supplying hard coded last two parameters to device_property_readXX_array().

OK by me. FWIW:

Thanks!

Quoted 9 lines

Off topic question: Shouldn't we also be able to read the number of references a reference property holds? I mean, shouldn't fwnode_property_get_referece_args() also return the number of references in the property if called without the value parameter?

There can be "empty" references in the middle of the "array" of references which cause the function to return -ENOENT just like when called with index out of bounds, so the caller now has in practice know how many references the property actually has in advance.

The idea is good, though I'm not so familiar with reference sub-API, don't know what would be the best approach to cover all: ACPI, DT, swnode.

Signature · 2 lines
With Best Regards,
Andy Shevchenko