diff mbox series

[U-Boot,2/4] fdt: Introduce fdtdec_get_alias_highest_id()

Message ID 35e51fb93782118f7c06be2d1e449476e98bb69a.1547824392.git.michal.simek@xilinx.com
State Superseded
Delegated to: Heiko Schocher
Headers show
Series Align U-Boot I2C DM bus ID handling with Linux | expand

Commit Message

Michal Simek Jan. 18, 2019, 3:13 p.m. UTC
Find out the highest alias ID used for certain subsystem.
This call will be used for alocating IDs for i2c buses which are not
described in DT.

Signed-off-by: Michal Simek <michal.simek@xilinx.com>
---

 include/fdtdec.h | 13 +++++++++++++
 lib/fdtdec.c     | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

Comments

Simon Glass Jan. 31, 2019, 10:04 a.m. UTC | #1
Hi Michal,

On Fri, 18 Jan 2019 at 08:13, Michal Simek <michal.simek@xilinx.com> wrote:
>
> Find out the highest alias ID used for certain subsystem.
> This call will be used for alocating IDs for i2c buses which are not
> described in DT.
>
> Signed-off-by: Michal Simek <michal.simek@xilinx.com>
> ---
>
>  include/fdtdec.h | 13 +++++++++++++
>  lib/fdtdec.c     | 33 +++++++++++++++++++++++++++++++++
>  2 files changed, 46 insertions(+)
>
> diff --git a/include/fdtdec.h b/include/fdtdec.h
> index f1bcbf837ffb..c2dd87ede226 100644
> --- a/include/fdtdec.h
> +++ b/include/fdtdec.h
> @@ -626,6 +626,19 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
>                          int *seqp);
>
>  /**
> + * Get the highest alias number for susbystem.
> + *
> + * It parses all aliases and find out highest recorded alias for subsystem.
> + * Aliases are of the form <base><num> where <num> is the sequence number.
> + *
> + * @param blob         Device tree blob (if NULL, then error is returned)
> + * @param base         Base name for alias susbystem (before the number)
> + *
> + * @return 0 highest alias ID, -1 if not found
> + */
> +int fdtdec_get_alias_highest_id(const void *blob, const char *base);
> +
> +/**
>   * Get a property from the /chosen node
>   *
>   * @param blob         Device tree blob (if NULL, then NULL is returned)
> diff --git a/lib/fdtdec.c b/lib/fdtdec.c
> index 18663ce6bdac..55811975ef54 100644
> --- a/lib/fdtdec.c
> +++ b/lib/fdtdec.c
> @@ -549,6 +549,39 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
>         return -ENOENT;
>  }
>
> +int fdtdec_get_alias_highest_id(const void *blob, const char *base)
> +{
> +       int base_len = strlen(base);
> +       int prop_offset;
> +       int aliases;
> +       int max = -1;
> +
> +       debug("Looking for highest alias id for '%s'\n", base);
> +
> +       aliases = fdt_path_offset(blob, "/aliases");
> +       for (prop_offset = fdt_first_property_offset(blob, aliases);
> +            prop_offset > 0;
> +            prop_offset = fdt_next_property_offset(blob, prop_offset)) {
> +               const char *prop;
> +               const char *name;
> +               int len, val;
> +
> +               prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
> +               debug("   - %s, %s\n", name, prop);
> +               if (*prop != '/' || prop[len - 1] ||
> +                   strncmp(name, base, base_len))
> +                       continue;
> +
> +               val = trailing_strtol(name);
> +               if (val > max) {
> +                       debug("Found seq %d\n", val);
> +                       max = val;
> +               }
> +       }
> +
> +       return max;

This looks right to me. Can you please add a test that calls this for
a few sandbox aliases?

> +}
> +
>  const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
>  {
>         int chosen_node;
> --
> 1.9.1
>

Regards,
Simon


On Fri, 18 Jan 2019 at 08:13, Michal Simek <michal.simek@xilinx.com> wrote:
>
> Find out the highest alias ID used for certain subsystem.
> This call will be used for alocating IDs for i2c buses which are not
> described in DT.
>
> Signed-off-by: Michal Simek <michal.simek@xilinx.com>
> ---
>
>  include/fdtdec.h | 13 +++++++++++++
>  lib/fdtdec.c     | 33 +++++++++++++++++++++++++++++++++
>  2 files changed, 46 insertions(+)
>
> diff --git a/include/fdtdec.h b/include/fdtdec.h
> index f1bcbf837ffb..c2dd87ede226 100644
> --- a/include/fdtdec.h
> +++ b/include/fdtdec.h
> @@ -626,6 +626,19 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
>                          int *seqp);
>
>  /**
> + * Get the highest alias number for susbystem.
> + *
> + * It parses all aliases and find out highest recorded alias for subsystem.
> + * Aliases are of the form <base><num> where <num> is the sequence number.
> + *
> + * @param blob         Device tree blob (if NULL, then error is returned)
> + * @param base         Base name for alias susbystem (before the number)
> + *
> + * @return 0 highest alias ID, -1 if not found
> + */
> +int fdtdec_get_alias_highest_id(const void *blob, const char *base);
> +
> +/**
>   * Get a property from the /chosen node
>   *
>   * @param blob         Device tree blob (if NULL, then NULL is returned)
> diff --git a/lib/fdtdec.c b/lib/fdtdec.c
> index 18663ce6bdac..55811975ef54 100644
> --- a/lib/fdtdec.c
> +++ b/lib/fdtdec.c
> @@ -549,6 +549,39 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
>         return -ENOENT;
>  }
>
> +int fdtdec_get_alias_highest_id(const void *blob, const char *base)
> +{
> +       int base_len = strlen(base);
> +       int prop_offset;
> +       int aliases;
> +       int max = -1;
> +
> +       debug("Looking for highest alias id for '%s'\n", base);
> +
> +       aliases = fdt_path_offset(blob, "/aliases");
> +       for (prop_offset = fdt_first_property_offset(blob, aliases);
> +            prop_offset > 0;
> +            prop_offset = fdt_next_property_offset(blob, prop_offset)) {
> +               const char *prop;
> +               const char *name;
> +               int len, val;
> +
> +               prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
> +               debug("   - %s, %s\n", name, prop);
> +               if (*prop != '/' || prop[len - 1] ||
> +                   strncmp(name, base, base_len))
> +                       continue;
> +
> +               val = trailing_strtol(name);
> +               if (val > max) {
> +                       debug("Found seq %d\n", val);
> +                       max = val;
> +               }
> +       }
> +
> +       return max;
> +}
> +
>  const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
>  {
>         int chosen_node;
> --
> 1.9.1
>
Michal Simek Jan. 31, 2019, 10:14 a.m. UTC | #2
On 31. 01. 19 11:04, Simon Glass wrote:
> Hi Michal,
> 
> On Fri, 18 Jan 2019 at 08:13, Michal Simek <michal.simek@xilinx.com> wrote:
>>
>> Find out the highest alias ID used for certain subsystem.
>> This call will be used for alocating IDs for i2c buses which are not
>> described in DT.
>>
>> Signed-off-by: Michal Simek <michal.simek@xilinx.com>
>> ---
>>
>>  include/fdtdec.h | 13 +++++++++++++
>>  lib/fdtdec.c     | 33 +++++++++++++++++++++++++++++++++
>>  2 files changed, 46 insertions(+)
>>
>> diff --git a/include/fdtdec.h b/include/fdtdec.h
>> index f1bcbf837ffb..c2dd87ede226 100644
>> --- a/include/fdtdec.h
>> +++ b/include/fdtdec.h
>> @@ -626,6 +626,19 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
>>                          int *seqp);
>>
>>  /**
>> + * Get the highest alias number for susbystem.
>> + *
>> + * It parses all aliases and find out highest recorded alias for subsystem.
>> + * Aliases are of the form <base><num> where <num> is the sequence number.
>> + *
>> + * @param blob         Device tree blob (if NULL, then error is returned)
>> + * @param base         Base name for alias susbystem (before the number)
>> + *
>> + * @return 0 highest alias ID, -1 if not found
>> + */
>> +int fdtdec_get_alias_highest_id(const void *blob, const char *base);
>> +
>> +/**
>>   * Get a property from the /chosen node
>>   *
>>   * @param blob         Device tree blob (if NULL, then NULL is returned)
>> diff --git a/lib/fdtdec.c b/lib/fdtdec.c
>> index 18663ce6bdac..55811975ef54 100644
>> --- a/lib/fdtdec.c
>> +++ b/lib/fdtdec.c
>> @@ -549,6 +549,39 @@ int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
>>         return -ENOENT;
>>  }
>>
>> +int fdtdec_get_alias_highest_id(const void *blob, const char *base)
>> +{
>> +       int base_len = strlen(base);
>> +       int prop_offset;
>> +       int aliases;
>> +       int max = -1;
>> +
>> +       debug("Looking for highest alias id for '%s'\n", base);
>> +
>> +       aliases = fdt_path_offset(blob, "/aliases");
>> +       for (prop_offset = fdt_first_property_offset(blob, aliases);
>> +            prop_offset > 0;
>> +            prop_offset = fdt_next_property_offset(blob, prop_offset)) {
>> +               const char *prop;
>> +               const char *name;
>> +               int len, val;
>> +
>> +               prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
>> +               debug("   - %s, %s\n", name, prop);
>> +               if (*prop != '/' || prop[len - 1] ||
>> +                   strncmp(name, base, base_len))
>> +                       continue;
>> +
>> +               val = trailing_strtol(name);
>> +               if (val > max) {
>> +                       debug("Found seq %d\n", val);
>> +                       max = val;
>> +               }
>> +       }
>> +
>> +       return max;
> 
> This looks right to me. Can you please add a test that calls this for
> a few sandbox aliases?

let's see how this can be done.

M
diff mbox series

Patch

diff --git a/include/fdtdec.h b/include/fdtdec.h
index f1bcbf837ffb..c2dd87ede226 100644
--- a/include/fdtdec.h
+++ b/include/fdtdec.h
@@ -626,6 +626,19 @@  int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
 			 int *seqp);
 
 /**
+ * Get the highest alias number for susbystem.
+ *
+ * It parses all aliases and find out highest recorded alias for subsystem.
+ * Aliases are of the form <base><num> where <num> is the sequence number.
+ *
+ * @param blob		Device tree blob (if NULL, then error is returned)
+ * @param base		Base name for alias susbystem (before the number)
+ *
+ * @return 0 highest alias ID, -1 if not found
+ */
+int fdtdec_get_alias_highest_id(const void *blob, const char *base);
+
+/**
  * Get a property from the /chosen node
  *
  * @param blob		Device tree blob (if NULL, then NULL is returned)
diff --git a/lib/fdtdec.c b/lib/fdtdec.c
index 18663ce6bdac..55811975ef54 100644
--- a/lib/fdtdec.c
+++ b/lib/fdtdec.c
@@ -549,6 +549,39 @@  int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
 	return -ENOENT;
 }
 
+int fdtdec_get_alias_highest_id(const void *blob, const char *base)
+{
+	int base_len = strlen(base);
+	int prop_offset;
+	int aliases;
+	int max = -1;
+
+	debug("Looking for highest alias id for '%s'\n", base);
+
+	aliases = fdt_path_offset(blob, "/aliases");
+	for (prop_offset = fdt_first_property_offset(blob, aliases);
+	     prop_offset > 0;
+	     prop_offset = fdt_next_property_offset(blob, prop_offset)) {
+		const char *prop;
+		const char *name;
+		int len, val;
+
+		prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
+		debug("   - %s, %s\n", name, prop);
+		if (*prop != '/' || prop[len - 1] ||
+		    strncmp(name, base, base_len))
+			continue;
+
+		val = trailing_strtol(name);
+		if (val > max) {
+			debug("Found seq %d\n", val);
+			max = val;
+		}
+	}
+
+	return max;
+}
+
 const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
 {
 	int chosen_node;