linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] ACPI / PM: Export ACPI power states of devices via sysfs
@ 2012-12-05  0:39 Rafael J. Wysocki
  2012-12-05  0:40 ` [PATCH 1/3] ACPI / PM: More visible function for retrieving device power states Rafael J. Wysocki
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2012-12-05  0:39 UTC (permalink / raw)
  To: ACPI Devel Maling List; +Cc: Linux PM list, LKML, Greg Kroah-Hartman, Len Brown

Hi All,

In some situations it is very useful to know the current ACPI power states of
devices.  For this reason, the following series of patches makes it possible
to retrieve that information via sysfs.

Patches [1-2/3] are trivial function renames, patch [3/3] is the real thing.

The series is on top of the master branch of linux-pm.git.

Thanks,
Rafael


-- 
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] ACPI / PM: More visible function for retrieving device power states
  2012-12-05  0:39 [PATCH 0/3] ACPI / PM: Export ACPI power states of devices via sysfs Rafael J. Wysocki
@ 2012-12-05  0:40 ` Rafael J. Wysocki
  2012-12-05  0:41 ` [PATCH 2/3] ACPI / PM: Common string representations of " Rafael J. Wysocki
  2012-12-05  0:42 ` [PATCH 3/3] ACPI / PM: Export power states of ACPI devices via sysfs Rafael J. Wysocki
  2 siblings, 0 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2012-12-05  0:40 UTC (permalink / raw)
  To: ACPI Devel Maling List; +Cc: Linux PM list, LKML, Greg Kroah-Hartman, Len Brown

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The function used for retrieving ACPI device power states,
__acpi_bus_get_power(), is now static, because it is only used
internally in drivers/acpi/bus.c.  However, it will be used
outside of that file going forward, so rename it to
acpi_device_get_power(), in analogy with acpi_device_set_power(),
add a kerneldoc comment to it and add its header to acpi_bus.h.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/bus.c      |   15 ++++++++++++---
 include/acpi/acpi_bus.h |    1 +
 2 files changed, 13 insertions(+), 3 deletions(-)

Index: linux/drivers/acpi/bus.c
===================================================================
--- linux.orig/drivers/acpi/bus.c
+++ linux/drivers/acpi/bus.c
@@ -200,7 +200,16 @@ static const char *state_string(int stat
 	}
 }
 
-static int __acpi_bus_get_power(struct acpi_device *device, int *state)
+/**
+ * acpi_device_get_power - Get power state of an ACPI device.
+ * @device: Device to get the power state of.
+ * @state: Place to store the power state of the device.
+ *
+ * This function does not update the device's power.state field, but it may
+ * update its parent's power.state field (when the parent's power state is
+ * unknown and the device's power state turns out to be D0).
+ */
+int acpi_device_get_power(struct acpi_device *device, int *state)
 {
 	int result = ACPI_STATE_UNKNOWN;
 
@@ -389,7 +398,7 @@ int acpi_bus_init_power(struct acpi_devi
 
 	device->power.state = ACPI_STATE_UNKNOWN;
 
-	result = __acpi_bus_get_power(device, &state);
+	result = acpi_device_get_power(device, &state);
 	if (result)
 		return result;
 
@@ -413,7 +422,7 @@ int acpi_bus_update_power(acpi_handle ha
 	if (result)
 		return result;
 
-	result = __acpi_bus_get_power(device, &state);
+	result = acpi_device_get_power(device, &state);
 	if (result)
 		return result;
 
Index: linux/include/acpi/acpi_bus.h
===================================================================
--- linux.orig/include/acpi/acpi_bus.h
+++ linux/include/acpi/acpi_bus.h
@@ -340,6 +340,7 @@ acpi_status acpi_bus_get_status_handle(a
 				       unsigned long long *sta);
 int acpi_bus_get_status(struct acpi_device *device);
 int acpi_bus_set_power(acpi_handle handle, int state);
+int acpi_device_get_power(struct acpi_device *device, int *state);
 int acpi_device_set_power(struct acpi_device *device, int state);
 int acpi_bus_update_power(acpi_handle handle, int *state_p);
 bool acpi_bus_power_manageable(acpi_handle handle);


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] ACPI / PM: Common string representations of device power states
  2012-12-05  0:39 [PATCH 0/3] ACPI / PM: Export ACPI power states of devices via sysfs Rafael J. Wysocki
  2012-12-05  0:40 ` [PATCH 1/3] ACPI / PM: More visible function for retrieving device power states Rafael J. Wysocki
@ 2012-12-05  0:41 ` Rafael J. Wysocki
  2012-12-05  0:42 ` [PATCH 3/3] ACPI / PM: Export power states of ACPI devices via sysfs Rafael J. Wysocki
  2 siblings, 0 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2012-12-05  0:41 UTC (permalink / raw)
  To: ACPI Devel Maling List; +Cc: Linux PM list, LKML, Greg Kroah-Hartman, Len Brown

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The function returning string representations of ACPI device power
states, state_string((), is now static, because it is only used
internally in drivers/acpi/bus.c.  However, it will be used outside
of that file going forward, so rename it to
acpi_power_state_string(), add a kerneldoc comment to it and add its
header to acpi_bus.h.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/acpi/bus.c      |   18 ++++++++++++------
 include/acpi/acpi_bus.h |    1 +
 2 files changed, 13 insertions(+), 6 deletions(-)

Index: linux/drivers/acpi/bus.c
===================================================================
--- linux.orig/drivers/acpi/bus.c
+++ linux/drivers/acpi/bus.c
@@ -182,7 +182,11 @@ EXPORT_SYMBOL(acpi_bus_get_private_data)
                                  Power Management
    -------------------------------------------------------------------------- */
 
-static const char *state_string(int state)
+/**
+ * acpi_power_state_string - String representation of ACPI device power state.
+ * @state: ACPI device power state to return the string representation of.
+ */
+const char *acpi_power_state_string(int state)
 {
 	switch (state) {
 	case ACPI_STATE_D0:
@@ -260,7 +264,7 @@ int acpi_device_get_power(struct acpi_de
 
  out:
 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
-			  device->pnp.bus_id, state_string(*state)));
+			  device->pnp.bus_id, acpi_power_state_string(*state)));
 
 	return 0;
 }
@@ -287,13 +291,13 @@ int acpi_device_set_power(struct acpi_de
 
 	if (state == device->power.state) {
 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n",
-				  state_string(state)));
+				  acpi_power_state_string(state)));
 		return 0;
 	}
 
 	if (!device->power.states[state].flags.valid) {
 		printk(KERN_WARNING PREFIX "Device does not support %s\n",
-		       state_string(state));
+		       acpi_power_state_string(state));
 		return -ENODEV;
 	}
 	if (device->parent && (state < device->parent->power.state)) {
@@ -354,12 +358,14 @@ int acpi_device_set_power(struct acpi_de
 	if (result)
 		printk(KERN_WARNING PREFIX
 			      "Device [%s] failed to transition to %s\n",
-			      device->pnp.bus_id, state_string(state));
+			      device->pnp.bus_id,
+			      acpi_power_state_string(state));
 	else {
 		device->power.state = state;
 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 				  "Device [%s] transitioned to %s\n",
-				  device->pnp.bus_id, state_string(state)));
+				  device->pnp.bus_id,
+				  acpi_power_state_string(state)));
 	}
 
 	return result;
Index: linux/include/acpi/acpi_bus.h
===================================================================
--- linux.orig/include/acpi/acpi_bus.h
+++ linux/include/acpi/acpi_bus.h
@@ -340,6 +340,7 @@ acpi_status acpi_bus_get_status_handle(a
 				       unsigned long long *sta);
 int acpi_bus_get_status(struct acpi_device *device);
 int acpi_bus_set_power(acpi_handle handle, int state);
+const char *acpi_power_state_string(int state);
 int acpi_device_get_power(struct acpi_device *device, int *state);
 int acpi_device_set_power(struct acpi_device *device, int state);
 int acpi_bus_update_power(acpi_handle handle, int *state_p);


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] ACPI / PM: Export power states of ACPI devices via sysfs
  2012-12-05  0:39 [PATCH 0/3] ACPI / PM: Export ACPI power states of devices via sysfs Rafael J. Wysocki
  2012-12-05  0:40 ` [PATCH 1/3] ACPI / PM: More visible function for retrieving device power states Rafael J. Wysocki
  2012-12-05  0:41 ` [PATCH 2/3] ACPI / PM: Common string representations of " Rafael J. Wysocki
@ 2012-12-05  0:42 ` Rafael J. Wysocki
  2 siblings, 0 replies; 4+ messages in thread
From: Rafael J. Wysocki @ 2012-12-05  0:42 UTC (permalink / raw)
  To: ACPI Devel Maling List; +Cc: Linux PM list, LKML, Greg Kroah-Hartman, Len Brown

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

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

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 Documentation/ABI/testing/sysfs-devices-power |   13 ++++++++
 drivers/acpi/scan.c                           |   41 ++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)

Index: linux/drivers/acpi/scan.c
===================================================================
--- linux.orig/drivers/acpi/scan.c
+++ linux/drivers/acpi/scan.c
@@ -174,6 +174,43 @@ err_out:
 }
 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
 
+#ifdef CONFIG_PM
+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 struct attribute *acpi_dev_pm_attrs[] = {
+	&dev_attr_power_state.attr,
+	NULL,
+};
+static struct attribute_group acpi_dev_pm_attr_group = {
+	.name	= power_group_name,
+	.attrs	= acpi_dev_pm_attrs,
+};
+
+static void acpi_dev_pm_sysfs_add(struct device *dev)
+{
+	sysfs_merge_group(&dev->kobj, &acpi_dev_pm_attr_group);
+}
+
+static void acpi_dev_pm_sysfs_remove(struct device *dev)
+{
+	sysfs_unmerge_group(&dev->kobj, &acpi_dev_pm_attr_group);
+}
+#else /* !CONFIG_PM */
+static inline void acpi_dev_pm_sysfs_add(struct device *dev) {}
+static inline void acpi_dev_pm_sysfs_remove(struct device *dev) {}
+#endif /* !CONFIG_PM */
+
 static ssize_t
 acpi_eject_store(struct device *d, struct device_attribute *attr,
 		const char *buf, size_t count)
@@ -367,6 +404,9 @@ static int acpi_device_setup_files(struc
 	status = acpi_get_handle(dev->handle, "_EJ0", &temp);
 	if (ACPI_SUCCESS(status))
 		result = device_create_file(&dev->dev, &dev_attr_eject);
+
+	acpi_dev_pm_sysfs_add(&dev->dev);
+
 end:
 	return result;
 }
@@ -376,6 +416,7 @@ static void acpi_device_remove_files(str
 	acpi_status status;
 	acpi_handle temp;
 
+	acpi_dev_pm_sysfs_remove(&dev->dev);
 	/*
 	 * If device has _STR, remove 'description' file
 	 */
Index: linux/Documentation/ABI/testing/sysfs-devices-power
===================================================================
--- linux.orig/Documentation/ABI/testing/sysfs-devices-power
+++ linux/Documentation/ABI/testing/sysfs-devices-power
@@ -235,3 +235,16 @@ Description:
 
 		This attribute has no effect on system-wide suspend/resume and
 		hibernation.
+
+What:		/sys/devices/.../power/power_state
+Date:		December 2012
+Contact:	Rafael J. Wysocki <rafael.j.wysocki@intel.com>
+Description:
+		The /sys/devices/.../power/power_state attribute is only present
+		for ACPI device nodes (i.e. objects of type struct acpi_device).
+
+		If present, it contains the string representation of the current
+		ACPI power state of the device represented by the given ACPI
+		device node.
+
+		This attribute is read-only.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-12-05  0:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-05  0:39 [PATCH 0/3] ACPI / PM: Export ACPI power states of devices via sysfs Rafael J. Wysocki
2012-12-05  0:40 ` [PATCH 1/3] ACPI / PM: More visible function for retrieving device power states Rafael J. Wysocki
2012-12-05  0:41 ` [PATCH 2/3] ACPI / PM: Common string representations of " Rafael J. Wysocki
2012-12-05  0:42 ` [PATCH 3/3] ACPI / PM: Export power states of ACPI devices via sysfs Rafael J. Wysocki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).