From: Rafael J. Wysocki <rafael.j.wyso...@intel.com>
Subject: ACPI / PM: Export power states of ACPI devices via sysfs

Make it possible to retrieve the current power state of a device with
ACPI power management from user space via sysfs by adding a new
attribute power_state to the sysfs directory associated with the
struct acpi_device object representing the device's ACPI node.

Signed-off-by: Rafael J. Wysocki <rafael.j.wyso...@intel.com>
---

I've changed my mind here.  It looks like it's more convenient to put the
power_state attribute directly into the ACPI device node's directory in sysfs
rather than into its power subdirectory.

Thanks,
Rafael

---
 Documentation/ABI/testing/sysfs-devices-power |   13 +++++++++++++
 drivers/acpi/scan.c                           |   25 ++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/acpi/scan.c
===================================================================
--- linux-pm.orig/drivers/acpi/scan.c
+++ linux-pm/drivers/acpi/scan.c
@@ -184,6 +184,19 @@ err_out:
 }
 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
 
+static ssize_t power_state_show(struct device *dev,
+                               struct device_attribute *attr, char *buf)
+{
+       struct acpi_device *adev = to_acpi_device(dev);
+       int state;
+       int ret;
+
+       ret = acpi_device_get_power(adev, &state);
+       return ret ? ret : sprintf(buf, "%s\n", acpi_power_state_string(state));
+}
+
+static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
+
 static ssize_t
 acpi_eject_store(struct device *d, struct device_attribute *attr,
                const char *buf, size_t count)
@@ -375,8 +388,15 @@ static int acpi_device_setup_files(struc
          * hot-removal function from userland.
          */
        status = acpi_get_handle(dev->handle, "_EJ0", &temp);
-       if (ACPI_SUCCESS(status))
+       if (ACPI_SUCCESS(status)) {
                result = device_create_file(&dev->dev, &dev_attr_eject);
+               if (result)
+                       goto end;
+       }
+
+       if (dev->flags.power_manageable)
+               result = device_create_file(&dev->dev, &dev_attr_power_state);
+
 end:
        return result;
 }
@@ -386,6 +406,9 @@ static void acpi_device_remove_files(str
        acpi_status status;
        acpi_handle temp;
 
+       if (dev->flags.power_manageable)
+               device_remove_file(&dev->dev, &dev_attr_power_state);
+
        /*
         * If device has _STR, remove 'description' file
         */

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to