[PATCH 1/4] driver: implement get_free_deviceid_from()
Sascha Hauer
s.hauer at pengutronix.de
Mon Dec 8 04:00:51 PST 2025
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.
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 eca23d7c308800ead5e2f4e25208dc68ab2fd92e..1a5a3598be5d4b2c6ce8558b1ac8c3cba4d59485 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 c130a3cd63fd5e6dc365a33c765a22dc4e2ca90a..84fb5c0aa9a085e39d2905d453d69adff50d2cd5 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