]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ACPI: bus: Introduce devm_acpi_install_notify_handler()
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 21 May 2026 13:59:50 +0000 (15:59 +0200)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Mon, 25 May 2026 14:43:50 +0000 (16:43 +0200)
Introduce devm_acpi_install_notify_handler() for installing an ACPI
notify handler managed by devres that will be removed automatically on
driver detach.

It installs the notify handler on the device object in the ACPI
namespace that corresponds to the owner device's ACPI companion, if
present (an error is returned if the owner device doesn't have an ACPI
companion).

Currently, there is no way to manually remove the notify handler
installed by it because none of its users brought on subsequently
will need to do that.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
[ rjw: Kerneldoc comment refinement ]
Link: https://patch.msgid.link/2268031.irdbgypaU6@rafael.j.wysocki
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/acpi/bus.c
include/acpi/acpi_bus.h

index 2ec095e2009e40b0645f654fb051429e5679dc7c..e9eee0d1f0dafc452880ef3ed416d2e40ae7e76f 100644 (file)
@@ -679,6 +679,72 @@ void acpi_dev_remove_notify_handler(struct acpi_device *adev,
 }
 EXPORT_SYMBOL_GPL(acpi_dev_remove_notify_handler);
 
+struct acpi_notify_handler_devres {
+       acpi_notify_handler handler;
+       u32 handler_type;
+};
+
+static void devm_acpi_notify_handler_release(struct device *dev, void *res)
+{
+       struct acpi_notify_handler_devres *dr = res;
+
+       acpi_dev_remove_notify_handler(ACPI_COMPANION(dev), dr->handler_type,
+                                      dr->handler);
+}
+
+/**
+ * devm_acpi_install_notify_handler - Install an ACPI notify handler for a
+ *                                   managed device
+ * @dev: Device to install a notify handler for
+ * @handler_type: Type of the notify handler
+ * @handler: Handler function to install
+ * @context: Data passed back to the handler function
+ *
+ * This function performs the same function as acpi_dev_install_notify_handler()
+ * called for the ACPI companion of @dev with the same @handler_type, @handler,
+ * and @context arguments, but the ACPI notify handler installed by it will be
+ * automatically removed on driver detach.
+ *
+ * Callers should ensure that all resources used by @handler have been allocated
+ * prior to invoking this function, in which case those resources should be
+ * devres-managed so that they won't be released before the notify handler
+ * removal.  Otherwise, special synchronization between @handler and the
+ * management of those resources is required.
+ *
+ * When the request fails, an error message is printed.  Don't add extra error
+ * messages at the call sites.
+ *
+ * Return: 0 on success or a negative error number.
+ */
+int devm_acpi_install_notify_handler(struct device *dev, u32 handler_type,
+                                    acpi_notify_handler handler, void *context)
+{
+       struct acpi_notify_handler_devres *dr;
+       struct acpi_device *adev;
+       int ret;
+
+       adev = ACPI_COMPANION(dev);
+       if (!adev)
+               return dev_err_probe(dev, -ENODEV, "No ACPI companion in %s()\n", __func__);
+
+       dr = devres_alloc(devm_acpi_notify_handler_release, sizeof(*dr), GFP_KERNEL);
+       if (!dr)
+               return -ENOMEM;
+
+       ret = acpi_dev_install_notify_handler(adev, handler_type, handler, context);
+       if (ret) {
+               devres_free(dr);
+               return dev_err_probe(dev, ret, "Failed to install an ACPI notify handler\n");
+       }
+
+       dr->handler = handler;
+       dr->handler_type = handler_type;
+       devres_add(dev, dr);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(devm_acpi_install_notify_handler);
+
 /* Handle events targeting \_SB device (at present only graceful shutdown) */
 
 #define ACPI_SB_NOTIFY_SHUTDOWN_REQUEST 0x81
index c41d9a7565cf1185593b680551cd3f1315b41e59..7e57f9698f7ced4b43d7d349e898513a864567e9 100644 (file)
@@ -629,6 +629,8 @@ int acpi_dev_install_notify_handler(struct acpi_device *adev,
 void acpi_dev_remove_notify_handler(struct acpi_device *adev,
                                    u32 handler_type,
                                    acpi_notify_handler handler);
+int devm_acpi_install_notify_handler(struct device *dev, u32 handler_type,
+                                    acpi_notify_handler handler, void *context);
 extern int acpi_notifier_call_chain(const char *device_class,
                                    const char *bus_id, u32 type, u32 data);
 extern int register_acpi_notifier(struct notifier_block *);