[PATCH v17 05/12] power: reset: reboot-mode: Expose sysfs for registered reboot_modes

Shivendra Pratap
2025-11-09T14:39:03Z

Currently, there is no standardized mechanism for userspace to
discover which reboot-modes are supported on a given platform.
This limitation forces tools and scripts to rely on hardcoded
assumptions about the supported reboot-modes.

Create a class 'reboot-mode' and a device under it to expose a
sysfs interface to show the available reboot mode arguments to
userspace. Use the driver_name field of the struct
reboot_mode_driver to create the device. For device-based
drivers, configure the device driver name as driver_name.

This results in the creation of:
/sys/class/reboot-mode/<driver>/reboot_modes

This read-only sysfs file will exposes the list of supported
reboot modes arguments provided by the driver, enabling userspace
to query the list of arguments.

Signed-off-by: Shivendra Pratap <shivendra.pratap@oss.qualcomm.com>
---
drivers/power/reset/reboot-mode.c | 62 ++++++++++++++++++++++++++++++++++++++-
include/linux/reboot-mode.h | 2 ++
2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/drivers/power/reset/reboot-mode.c b/drivers/power/reset/reboot-mode.c
index 873ac45cd7659b214b7c21958f580ca381e0a63d..582aa7f8ed7fa485c5a67877558c9b15d3600ef4 100644
--- a/drivers/power/reset/reboot-mode.c
+++ b/drivers/power/reset/reboot-mode.c
@@ -6,6 +6,7 @@
#define pr_fmt(fmt) "reboot-mode: " fmt

#include <linux/device.h>
+#include <linux/err.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/list.h>
@@ -23,6 +24,8 @@ struct mode_info {
struct list_head list;
};

+static struct class *rb_class;
+
static u64 get_reboot_mode_magic(struct reboot_mode_driver *reboot, const char *cmd)
{
const char *normal = "normal";
@@ -65,6 +68,51 @@ static int reboot_mode_notify(struct notifier_block *this,
return NOTIFY_DONE;
}

+static ssize_t reboot_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct reboot_mode_driver *reboot;
+ struct mode_info *info;
+ ssize_t size = 0;
+
+ reboot = (struct reboot_mode_driver *)dev_get_drvdata(dev);
+ if (!reboot)
+ return -ENODATA;
+
+ list_for_each_entry(info, &reboot->head, list)
+ size += sysfs_emit_at(buf, size, "%s ", info->mode);
+
+ if (size) {
+ size += sysfs_emit_at(buf, size - 1, "\n");
+ return size;
+ }
+
+ return -ENODATA;
+}
+static DEVICE_ATTR_RO(reboot_modes);
+
+static int create_reboot_mode_device(struct reboot_mode_driver *reboot)
+{
+ int ret = 0;
+
+ if (!rb_class) {
+ rb_class = class_create("reboot-mode");
+ if (IS_ERR(rb_class))
+ return PTR_ERR(rb_class);
+ }
+
+ reboot->reboot_dev = device_create(rb_class, NULL, 0, (void *)reboot, reboot->driver_name);
+ if (IS_ERR(reboot->reboot_dev))
+ return PTR_ERR(reboot->reboot_dev);
+
+ ret = device_create_file(reboot->reboot_dev, &dev_attr_reboot_modes);
+ if (ret) {
+ device_unregister(reboot->reboot_dev);
+ return ret;
+ }
+
+ return ret;
+}
+
/**
* reboot_mode_register - register a reboot mode driver
* @reboot: reboot mode driver
@@ -83,13 +131,17 @@ int reboot_mode_register(struct reboot_mode_driver *reboot, struct fwnode_handle
u32 magic_arg2;
int ret;

- if (!fwnode)
+ if (!fwnode || !reboot->driver_name)
return -EINVAL;

np = to_of_node(fwnode);
if (!np)
return -EINVAL;

+ ret = create_reboot_mode_device(reboot);
+ if (ret)
+ return ret;
+
INIT_LIST_HEAD(&reboot->head);

for_each_property_of_node(np, prop) {
@@ -142,6 +194,8 @@ int reboot_mode_register(struct reboot_mode_driver *reboot, struct fwnode_handle
kfree(info);
}

+ device_remove_file(reboot->reboot_dev, &dev_attr_reboot_modes);
+ device_unregister(reboot->reboot_dev);
return ret;
}
EXPORT_SYMBOL_GPL(reboot_mode_register);
@@ -155,6 +209,9 @@ int reboot_mode_unregister(struct reboot_mode_driver *reboot)
struct mode_info *info;
struct mode_info *next;

+ if (!reboot->reboot_dev)
+ return -EINVAL;
+
unregister_reboot_notifier(&reboot->reboot_notifier);

list_for_each_entry_safe(info, next, &reboot->head, list) {
@@ -163,6 +220,8 @@ int reboot_mode_unregister(struct reboot_mode_driver *reboot)
kfree(info);
}

+ device_remove_file(reboot->reboot_dev, &dev_attr_reboot_modes);
+ device_unregister(reboot->reboot_dev);
return 0;
}
EXPORT_SYMBOL_GPL(reboot_mode_unregister);
@@ -192,6 +251,7 @@ int devm_reboot_mode_register(struct device *dev,
if (!dr)
return -ENOMEM;

+ reboot->driver_name = reboot->dev->driver->name;
rc = reboot_mode_register(reboot, of_fwnode_handle(reboot->dev->of_node));
if (rc) {
devres_free(dr);
diff --git a/include/linux/reboot-mode.h b/include/linux/reboot-mode.h
index e0d3e8a54050a76f26846f456120b4c7e371d284..81c149edf40fbcf0d3427c2e12eb415199cb153b 100644
--- a/include/linux/reboot-mode.h
+++ b/include/linux/reboot-mode.h
@@ -7,6 +7,8 @@

struct reboot_mode_driver {
struct device *dev;
+ struct device *reboot_dev;
+ const char *driver_name;
struct list_head head;
int (*write)(struct reboot_mode_driver *reboot, u64 magic);
struct notifier_block reboot_notifier;

--
2.34.1