From c1eb5905fdce35a66173658a93819641e5220c18 Mon Sep 17 00:00:00 2001 From: Vincent Cloutier Date: Sun, 26 Jul 2026 21:12:59 -0400 Subject: [PATCH] power: supply: Add registration init callback Some battery drivers need to consume monitored-battery data before their power_supply is visible. That lets them prepare hardware configuration from parsed battery information without racing userspace exposure. power_supply_get_battery_info() already runs in __power_supply_register() for battery devices before device_add(). Add an optional descriptor init callback after driver data and battery info are available. The callback runs in sleepable process context while the power supply is still unpublished. Keep the callback synchronous: it must not publish changes or start asynchronous activity that can access the power supply before registration completes. Require callbacks to return zero or a negative errno. Defensively reject positive returns so registration cannot return an invalid error pointer. Assisted-by: OpenCode:gpt-5.6-sol Signed-off-by: Vincent Cloutier Link: https://patch.msgid.link/20260727011319.621794-2-vincent.cloutier@icloud.com Signed-off-by: Sebastian Reichel --- drivers/power/supply/power_supply_core.c | 8 ++++++++ include/linux/power_supply.h | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index fe48dfc6ff7b..00d8bc98d588 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -1777,6 +1777,14 @@ __power_supply_register(struct device *parent, init_rwsem(&psy->extensions_sem); INIT_LIST_HEAD(&psy->extensions); + if (desc->init) { + rc = desc->init(psy); + if (WARN_ON_ONCE(rc > 0)) + rc = -EINVAL; + if (rc) + goto check_supplies_failed; + } + rc = device_add(dev); if (rc) goto device_add_failed; diff --git a/include/linux/power_supply.h b/include/linux/power_supply.h index d9c8cce9faad..a9c056f13077 100644 --- a/include/linux/power_supply.h +++ b/include/linux/power_supply.h @@ -285,6 +285,15 @@ struct power_supply_desc { int (*property_is_writeable)(struct power_supply *psy, enum power_supply_property psp); void (*external_power_changed)(struct power_supply *psy); + /* + * Optional registration-time initialization. This runs in sleepable + * process context after driver data and any battery info are available, + * but before the device is added. The callback must not publish changes or + * start asynchronous activity that can access the power supply before + * registration completes. Return 0 on success or a negative errno on + * failure. + */ + int (*init)(struct power_supply *psy); /* * Set if thermal zone should not be created for this power supply.