Skip to contentSkip to conversation

dt: Add of_device_compatible_match()

RFC/PATCH Benjamin Herrenschmidt 4 participants 6 messages Jun 26, 2016 – Jul 7, 2016 3 messages not in the archive Device tree

lore
Share & tools
lore
#0 lore raw

This provides an equivalent of of_fdt_match() for non-flat trees.

This is more practical than matching an array of of_device_id structs when converting a bunch of existing users of of_fdt_match().

---

This is a pre-requisite for some work I'm doing to move the platform identification in arch/powerpc until after we have unflattened the device-tree. Rewriting all those lists as of_device_id's would be significantly cumbersome.

Note: untested other than it compiles. I want an agreement on the interface ASAP since the conversion of all the platforms is a burden I'd like to avoid doing twice.

drivers/of/base.c +22 -0
index ebf84e3..429c594 100644
@@ -493,6 +493,28 @@ int of_device_is_compatible(const struct device_node *device,
 }
 EXPORT_SYMBOL(of_device_is_compatible);
 
+/** Checks if the device is compatible with any of the entries in
+ *  a NULL terminated array of strings. Returns the best match
+ *  score or 0.
+ */
+int of_device_compatible_match(struct device_node *device,
+			       const char *const *compat)
+{
+	unsigned int tmp, score = 0;
+
+	if (!compat)
+		return 0;
+
+	while (*compat) {
+		tmp = of_device_is_compatible(device, *compat);
+		if (tmp && (score == 0 || (tmp < score)))
+			score = tmp;
+		compat++;
+	}
+
+	return score;
+}
+
 /**
  * of_machine_is_compatible - Test root of device tree for a given compatible value
  * @compat: compatible string to look for in root node's compatible property.
include/linux/of.h +2 -0
index 74eb28c..33c184d 100644
@@ -324,6 +324,8 @@ extern int of_property_read_string_helper(const struct device_node *np,
 					      const char **out_strs, size_t sz, int index);
 extern int of_device_is_compatible(const struct device_node *device,
 				   const char *);
+extern int of_device_compatible_match(struct device_node *device,
+				      const char *const *compat);
 extern bool of_device_is_available(const struct device_node *device);
 extern bool of_device_is_big_endian(const struct device_node *device);
 extern const void *of_get_property(const struct device_node *node,

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1466981246.20278.27.camel-XVmvHMARGAS8U2dJNN8I7kB+6BGkLq7r@public.gmane.org

#2 lore raw
Quoted 6 lines · Benjamin Herrenschmidt

This provides an equivalent of of_fdt_match() for non-flat trees.
This is more practical than matching an array of of_device_id structs when converting a bunch of existing users of of_fdt_match().

Quoted 10 lines

---

This is a pre-requisite for some work I'm doing to move the platform identification in arch/powerpc until after we have unflattened the device-tree. Rewriting all those lists as of_device_id's would be significantly cumbersome.

Note: untested other than it compiles. I want an agreement on the interface ASAP since the conversion of all the platforms is a burden I'd like to avoid doing twice.

Any comments on this?

We need it as a pre-req for a powerpc series, so unless anyone yells I'll merge it via there.

cheers

Quoted 48 lines

drivers/of/base.c +0 -0
index ebf84e3..429c594 100644
@@ -493,6 +493,28 @@ int of_device_is_compatible(const struct device_node *device,
  }
  EXPORT_SYMBOL(of_device_is_compatible);

drivers/of/base.c +22 -0
+/** Checks if the device is compatible with any of the entries in
+ *  a NULL terminated array of strings. Returns the best match
+ *  score or 0.
+ */
+int of_device_compatible_match(struct device_node *device,
+			       const char *const *compat)
+{
+	unsigned int tmp, score = 0;
+
+	if (!compat)
+		return 0;
+
+	while (*compat) {
+		tmp = of_device_is_compatible(device, *compat);
+		if (tmp && (score == 0 || (tmp < score)))
+			score = tmp;
+		compat++;
+	}
+
+	return score;
+}
+

 /**
  * of_machine_is_compatible - Test root of device tree for a given compatible value
  * @compat: compatible string to look for in root node's compatible property.

include/linux/of.h +2 -1
index 74eb28c..33c184d 100644
@@ -324,6 +324,8 @@ extern int of_property_read_string_helper(const struct device_node *np,
  					      const char **out_strs, size_t sz, int index);
  extern int of_device_is_compatible(const struct device_node *device,
  				   const char *);
+extern int of_device_compatible_match(struct device_node *device,
+				      const char *const *compat);
  extern bool of_device_is_available(const struct device_node *device);
  extern bool of_device_is_big_endian(const struct device_node *device);
  extern const void *of_get_property(const struct device_node *node,
--
Quoted 4 lines

To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): 1467872962.20157.1.camel-Gsx/[email protected]

#4 lore raw
Quoted 19 lines · CEST Michael Ellerman
Quoted 8 lines · Benjamin Herrenschmidt

This provides an equivalent of of_fdt_match() for non-flat trees.

This is more practical than matching an array of of_device_id structs when converting a bunch of existing users of of_fdt_match().

---

Quoted 8 lines

This is a pre-requisite for some work I'm doing to move the platform identification in arch/powerpc until after we have unflattened the device-tree. Rewriting all those lists as of_device_id's would be significantly cumbersome.

Note: untested other than it compiles. I want an agreement on the interface ASAP since the conversion of all the platforms is a burden I'd like to avoid doing twice.

Quoted 5 lines

Any comments on this?

We need it as a pre-req for a powerpc series, so unless anyone yells I'll merge it via there.

I like the idea, we already have a number of drivers that would benefit from this.

Arnd

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
In reply to a message not in the archive
Quoted 19 lines · Michael Ellerman
Quoted 8 lines · Benjamin Herrenschmidt

This provides an equivalent of of_fdt_match() for non-flat trees.

This is more practical than matching an array of of_device_id structs when converting a bunch of existing users of of_fdt_match().

---

Quoted 8 lines

This is a pre-requisite for some work I'm doing to move the platform identification in arch/powerpc until after we have unflattened the device-tree. Rewriting all those lists as of_device_id's would be significantly cumbersome.

Note: untested other than it compiles. I want an agreement on the interface ASAP since the conversion of all the platforms is a burden I'd like to avoid doing twice.

Quoted 4 lines

Any comments on this?

We need it as a pre-req for a powerpc series, so unless anyone yells I'll merge it via there.

Just replied to Benjamin. The logic is not quite right.

Rob has been handling the device tree merges, so I'll let him comment on whether it is ok for you to merge, once it is correct.

-Frank
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
In reply to a message not in the archive
Quoted 37 lines · Benjamin Herrenschmidt

This provides an equivalent of of_fdt_match() for non-flat trees.

This is more practical than matching an array of of_device_id structs when converting a bunch of existing users of of_fdt_match().

---

This is a pre-requisite for some work I'm doing to move the platform identification in arch/powerpc until after we have unflattened the device-tree. Rewriting all those lists as of_device_id's would be significantly cumbersome.

Note: untested other than it compiles. I want an agreement on the interface ASAP since the conversion of all the platforms is a burden I'd like to avoid doing twice.

drivers/of/base.c +12 -0
index ebf84e3..429c594 100644
@@ -493,6 +493,28 @@ int of_device_is_compatible(const struct device_node *device,
 }
 EXPORT_SYMBOL(of_device_is_compatible);
 
+/** Checks if the device is compatible with any of the entries in
+ *  a NULL terminated array of strings. Returns the best match
+ *  score or 0.
+ */
+int of_device_compatible_match(struct device_node *device,
+			       const char *const *compat)
+{
+	unsigned int tmp, score = 0;
+
+	if (!compat)
+		return 0;
+

drivers/of/base.c int of_device_is_compatible(const struct device_node *device, · line 517

Quoted 10 lines
drivers/of/base.c +10 -0
+	while (*compat) {
+		tmp = of_device_is_compatible(device, *compat);
+		if (tmp && (score == 0 || (tmp < score)))
+			score = tmp;
+		compat++;
+	}
+
+	return score;
+}
+

of_device_is_compatible() returns a higher value for a better match.

of_fdt_match() returns a lower value for a better match.

So checking for (tmp < score) will give you the opposite of what you want.

And if you are replacing of_fdt_match() with of_device_compatible_match(), the caller will have the expectation that a smaller score is a better match.

Quoted 9 lines
drivers/of/base.c +0 -0
 /**
  * of_machine_is_compatible - Test root of device tree for a given compatible value
  * @compat: compatible string to look for in root node's compatible property.
include/linux/of.h +0 -0
index 74eb28c..33c184d 100644

include/linux/of.h extern int of_property_read_string_helper(const struct device_node *np, · line 331

Quoted 8 lines
include/linux/of.h +2 -0
@@ -324,6 +324,8 @@ extern int of_property_read_string_helper(const struct device_node *np,
 					      const char **out_strs, size_t sz, int index);
 extern int of_device_is_compatible(const struct device_node *device,
 				   const char *);
+extern int of_device_compatible_match(struct device_node *device,
+				      const char *const *compat);
 extern bool of_device_is_available(const struct device_node *device);
 extern bool of_device_is_big_endian(const struct device_node *device);
 extern const void *of_get_property(const struct device_node *node,

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw
Quoted 4 lines · Frank Rowand

of_device_is_compatible() returns a higher value for a better match.

of_fdt_match() returns a lower value for a better match.

Ugh ... that smells of trainwreck...

Quoted 1 line

So checking for (tmp < score) will give you the opposite of what you want.

Yup. I'll fix it.

Quoted 3 lines

And if you are replacing of_fdt_match() with of_device_compatible_match(), the caller will have the expectation that a smaller score is a better match.

The few callers I converted only really cared about exact matches.

Cheers,
Ben.

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html