* [PATCH 0/4] driver name fixes
@ 2025-12-08 12:00 Sascha Hauer
2025-12-08 12:00 ` [PATCH 1/4] driver: implement get_free_deviceid_from() Sascha Hauer
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Sascha Hauer @ 2025-12-08 12:00 UTC (permalink / raw)
To: BAREBOX
Starting point of this series was that I had multiple EEPROMs on a board
from which one had an alias "eeprom0" and the other ones didn't have
aliases. the EEPROMs without aliases failed to register because they
tried to register themselves as "eeprom0" as well.
While EEPROMs triggered this series it fixes a longstanding problem in
barebox: It was possible to register two devices with the same name when
one device was registered as "foo0", DEVICE_ID_SINGLE and another one
as "foo", id = 0.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
Sascha Hauer (4):
driver: implement get_free_deviceid_from()
nvmem: fix device name setting
driver: fix device name clashes
eeprom: at24: reserve EEPROM names which have an alias
drivers/base/driver.c | 25 ++++++++++++++-----------
drivers/eeprom/at24.c | 12 ++++++++----
drivers/nvmem/core.c | 22 +++++++++++++++-------
include/driver.h | 7 ++++++-
4 files changed, 43 insertions(+), 23 deletions(-)
---
base-commit: 500df27eb054f86ce232c5bb30c3a7ea8f771e61
change-id: 20251208-nvmem-eeprom-94b9b05b1c04
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/4] driver: implement get_free_deviceid_from()
2025-12-08 12:00 [PATCH 0/4] driver name fixes Sascha Hauer
@ 2025-12-08 12:00 ` Sascha Hauer
2025-12-08 12:00 ` [PATCH 2/4] nvmem: fix device name setting Sascha Hauer
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2025-12-08 12:00 UTC (permalink / raw)
To: BAREBOX
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@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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 2/4] nvmem: fix device name setting
2025-12-08 12:00 [PATCH 0/4] driver name fixes Sascha Hauer
2025-12-08 12:00 ` [PATCH 1/4] driver: implement get_free_deviceid_from() Sascha Hauer
@ 2025-12-08 12:00 ` Sascha Hauer
2025-12-08 12:00 ` [PATCH 3/4] driver: fix device name clashes Sascha Hauer
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2025-12-08 12:00 UTC (permalink / raw)
To: BAREBOX
For the nvmem device ID it was only possible to specify NVMEM_DEVID_NONE
which translates to DEVICE_ID_SINGLE or NVMEM_DEVID_AUTO which
translates to DEVICE_ID_DYNAMIC. All other values default to
DEVICE_ID_DYNAMIC.
Explicitly allow to set a fixed device ID so that the caller can fully
specify the name under which the new nvmem device appears. The special
case is when no config->name is given. In this we use "nvmem" as device
name, but we also have to ignore config->id.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/nvmem/core.c | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index a96aaf0b5a7215d05e976133aacfee1fdf9c36f7..8e1cf400e5d83ad6d53d21a705913cee72bfd4ab 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -487,15 +487,23 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
if (config->read_only || !config->reg_write || of_property_read_bool(np, "read-only"))
nvmem->read_only = true;
- dev_set_name(&nvmem->dev, "%s", config->name ? : "nvmem");
- switch (config->id) {
- case NVMEM_DEVID_NONE:
- nvmem->dev.id = DEVICE_ID_SINGLE;
+ if (config->name) {
+ dev_set_name(&nvmem->dev, "%s", config->name);
+
+ switch (config->id) {
+ case NVMEM_DEVID_NONE:
+ nvmem->dev.id = DEVICE_ID_SINGLE;
+ break;
+ case NVMEM_DEVID_AUTO:
+ nvmem->dev.id = DEVICE_ID_DYNAMIC;
+ break;
+ default:
+ nvmem->dev.id = config->id;
break;
- case NVMEM_DEVID_AUTO:
- default:
+ }
+ } else {
+ dev_set_name(&nvmem->dev, "nvmem");
nvmem->dev.id = DEVICE_ID_DYNAMIC;
- break;
}
rval = register_device(&nvmem->dev);
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 3/4] driver: fix device name clashes
2025-12-08 12:00 [PATCH 0/4] driver name fixes Sascha Hauer
2025-12-08 12:00 ` [PATCH 1/4] driver: implement get_free_deviceid_from() Sascha Hauer
2025-12-08 12:00 ` [PATCH 2/4] nvmem: fix device name setting Sascha Hauer
@ 2025-12-08 12:00 ` Sascha Hauer
2025-12-08 12:00 ` [PATCH 4/4] eeprom: at24: reserve EEPROM names which have an alias Sascha Hauer
2025-12-10 7:09 ` [PATCH 0/4] driver name fixes Sascha Hauer
4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2025-12-08 12:00 UTC (permalink / raw)
To: BAREBOX
It can happen that we end up with two devices having the same name.
This happens when one device is registered with
dev->name "foo0";
dev->id = DEVICE_ID_SINGLE;
and another one with
dev->name "foo";
dev->id = 0;
Fix this by not comparing both dev->name and dev->id when testing if a
device already exists, but instead by comparing the resulting device
name.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/base/driver.c | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 1a5a3598be5d4b2c6ce8558b1ac8c3cba4d59485..c417e945ee0308028fc885101de18bd4a2adfe93 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -86,13 +86,18 @@ struct device *get_device_by_name(const char *name)
static struct device *get_device_by_name_id(const char *name, int id)
{
struct device *dev;
+ char *str = NULL;
- for_each_device(dev) {
- if(!strcmp(dev->name, name) && id == dev->id)
- return dev;
- }
+ if (id == DEVICE_ID_SINGLE)
+ return get_device_by_name(name);
- return NULL;
+ str = basprintf("%s%u", name, id);
+
+ dev = get_device_by_name(str);
+
+ free(str);
+
+ return dev;
}
int get_free_deviceid_from(const char *name_template, int id_from)
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 4/4] eeprom: at24: reserve EEPROM names which have an alias
2025-12-08 12:00 [PATCH 0/4] driver name fixes Sascha Hauer
` (2 preceding siblings ...)
2025-12-08 12:00 ` [PATCH 3/4] driver: fix device name clashes Sascha Hauer
@ 2025-12-08 12:00 ` Sascha Hauer
2025-12-10 7:09 ` [PATCH 0/4] driver name fixes Sascha Hauer
4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2025-12-08 12:00 UTC (permalink / raw)
To: BAREBOX
An EEPROM which doesn't have an alias will be registered as "eeprom0".
When another EEPROM is probed which has "eeprom0" as alias it will fail
to probe due to -EEXIST. Use of_alias_get_highest_id() to reserve
namespace for EEPROMs with aliases.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
drivers/eeprom/at24.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/drivers/eeprom/at24.c b/drivers/eeprom/at24.c
index 667d111b349d6941c757da3c6eb46ad7f2bb422e..5dc19800970093fa8c95f36d58d47d8c1db6dcdc 100644
--- a/drivers/eeprom/at24.c
+++ b/drivers/eeprom/at24.c
@@ -369,7 +369,7 @@ static int at24_probe(struct device *dev)
struct at24_platform_data chip;
bool writable;
struct at24_data *at24;
- int err;
+ int devid, err;
unsigned i, num_addresses;
const char *devname;
const char *alias;
@@ -423,10 +423,14 @@ static int at24_probe(struct device *dev)
at24->num_addresses = num_addresses;
alias = of_alias_get(dev->of_node);
- if (alias)
+ if (alias) {
devname = alias;
- else
+ devid = NVMEM_DEVID_NONE;
+ } else {
devname = "eeprom";
+ devid = get_free_deviceid_from(devname,
+ of_alias_get_highest_id(devname) + 1);
+ }
writable = !(chip.flags & AT24_FLAG_READONLY);
@@ -481,7 +485,7 @@ static int at24_probe(struct device *dev)
at24->nvmem_config.stride = 1;
at24->nvmem_config.word_size = 1;
at24->nvmem_config.size = chip.byte_len;
- at24->nvmem_config.id = alias ? NVMEM_DEVID_NONE : NVMEM_DEVID_AUTO;
+ at24->nvmem_config.id = devid;
at24->nvmem = nvmem_register(&at24->nvmem_config);
err = PTR_ERR_OR_ZERO(at24->nvmem);
--
2.47.3
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/4] driver name fixes
2025-12-08 12:00 [PATCH 0/4] driver name fixes Sascha Hauer
` (3 preceding siblings ...)
2025-12-08 12:00 ` [PATCH 4/4] eeprom: at24: reserve EEPROM names which have an alias Sascha Hauer
@ 2025-12-10 7:09 ` Sascha Hauer
4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2025-12-10 7:09 UTC (permalink / raw)
To: BAREBOX, Sascha Hauer
On Mon, 08 Dec 2025 13:00:50 +0100, Sascha Hauer wrote:
> Starting point of this series was that I had multiple EEPROMs on a board
> from which one had an alias "eeprom0" and the other ones didn't have
> aliases. the EEPROMs without aliases failed to register because they
> tried to register themselves as "eeprom0" as well.
>
> While EEPROMs triggered this series it fixes a longstanding problem in
> barebox: It was possible to register two devices with the same name when
> one device was registered as "foo0", DEVICE_ID_SINGLE and another one
> as "foo", id = 0.
>
> [...]
Applied, thanks!
[1/4] driver: implement get_free_deviceid_from()
https://git.pengutronix.de/cgit/barebox/commit/?id=0850fd8e22fa (link may not be stable)
[2/4] nvmem: fix device name setting
https://git.pengutronix.de/cgit/barebox/commit/?id=f6bf81cfc94d (link may not be stable)
[3/4] driver: fix device name clashes
https://git.pengutronix.de/cgit/barebox/commit/?id=6e30160425f3 (link may not be stable)
[4/4] eeprom: at24: reserve EEPROM names which have an alias
https://git.pengutronix.de/cgit/barebox/commit/?id=5d1577f0d3dd (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-12-10 7:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-08 12:00 [PATCH 0/4] driver name fixes Sascha Hauer
2025-12-08 12:00 ` [PATCH 1/4] driver: implement get_free_deviceid_from() Sascha Hauer
2025-12-08 12:00 ` [PATCH 2/4] nvmem: fix device name setting Sascha Hauer
2025-12-08 12:00 ` [PATCH 3/4] driver: fix device name clashes Sascha Hauer
2025-12-08 12:00 ` [PATCH 4/4] eeprom: at24: reserve EEPROM names which have an alias Sascha Hauer
2025-12-10 7:09 ` [PATCH 0/4] driver name fixes Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox