[PATCH v2025.09.y 11/58] driver: implement get_free_deviceid_from()

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Mar 13 06:24:55 PDT 2026


From: Sascha Hauer <s.hauer at pengutronix.de>

get_free_deviceid() starts looking for a free device ID starting from
zero. Add get_free_deviceid_from() which takes a start ID to look from
which is useful when some lower device IDs shall be reserved, for
example by device tree aliases.

(cherry picked from commit 0850fd8e22fa5d270000dc5267416884e9fb9770)

Link: https://lore.barebox.org/20251208-nvmem-eeprom-v1-1-41ba588e2f3a@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 drivers/base/driver.c | 10 ++++------
 include/driver.h      |  7 ++++++-
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 61118385f1df..f15b252ea318 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -95,14 +95,12 @@ static struct device *get_device_by_name_id(const char *name, int id)
 	return NULL;
 }
 
-int get_free_deviceid(const char *name_template)
+int get_free_deviceid_from(const char *name_template, int id_from)
 {
-	int i = 0;
-
 	while (1) {
-		if (!get_device_by_name_id(name_template, i))
-			return i;
-		i++;
+		if (!get_device_by_name_id(name_template, id_from))
+			return id_from;
+		id_from++;
 	};
 }
 
diff --git a/include/driver.h b/include/driver.h
index 143ca18e4e39..6c7751bbb419 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -124,7 +124,12 @@ struct device *find_device(const char *str);
  * appending a number to the template. Dynamically created devices should
  * use this function rather than filling the id field themselves.
  */
-int get_free_deviceid(const char *name_template);
+int get_free_deviceid_from(const char *name_template, int id_from);
+
+static inline int get_free_deviceid(const char *name_template)
+{
+	return get_free_deviceid_from(name_template, 0);
+}
 
 int dev_add_alias(struct device *dev, const char *fmt, ...) __printf(2, 3);
 
-- 
2.47.3




More information about the barebox mailing list