Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b86ff982 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki
Browse files

PM / Sleep: Add user space interface for manipulating wakeup sources, v3



Android allows user space to manipulate wakelocks using two
sysfs file located in /sys/power/, wake_lock and wake_unlock.
Writing a wakelock name and optionally a timeout to the wake_lock
file causes the wakelock whose name was written to be acquired (it
is created before is necessary), optionally with the given timeout.
Writing the name of a wakelock to wake_unlock causes that wakelock
to be released.

Implement an analogous interface for user space using wakeup sources.
Add the /sys/power/wake_lock and /sys/power/wake_unlock files
allowing user space to create, activate and deactivate wakeup
sources, such that writing a name and optionally a timeout to
wake_lock causes the wakeup source of that name to be activated,
optionally with the given timeout.  If that wakeup source doesn't
exist, it will be created and then activated.  Writing a name to
wake_unlock causes the wakeup source of that name, if there is one,
to be deactivated.  Wakeup sources created with the help of
wake_lock that haven't been used for more than 5 minutes are garbage
collected and destroyed.  Moreover, there can be only WL_NUMBER_LIMIT
wakeup sources created with the help of wake_lock present at a time.

The data type used to track wakeup sources created by user space is
called "struct wakelock" to indicate the origins of this feature.

This version of the patch includes an rbtree manipulation fix from John Stultz.

Signed-off-by: default avatarRafael J. Wysocki <rjw@sisk.pl>
Acked-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: default avatarNeilBrown <neilb@suse.de>
parent 55850945
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -189,3 +189,45 @@ Description:

		Reading from this file causes the last string successfully
		written to it to be returned.

What:		/sys/power/wake_lock
Date:		February 2012
Contact:	Rafael J. Wysocki <rjw@sisk.pl>
Description:
		The /sys/power/wake_lock file allows user space to create
		wakeup source objects and activate them on demand (if one of
		those wakeup sources is active, reads from the
		/sys/power/wakeup_count file block or return false).  When a
		string without white space is written to /sys/power/wake_lock,
		it will be assumed to represent a wakeup source name.  If there
		is a wakeup source object with that name, it will be activated
		(unless active already).  Otherwise, a new wakeup source object
		will be registered, assigned the given name and activated.
		If a string written to /sys/power/wake_lock contains white
		space, the part of the string preceding the white space will be
		regarded as a wakeup source name and handled as descrived above.
		The other part of the string will be regarded as a timeout (in
		nanoseconds) such that the wakeup source will be automatically
		deactivated after it has expired.  The timeout, if present, is
		set regardless of the current state of the wakeup source object
		in question.

		Reads from this file return a string consisting of the names of
		wakeup sources created with the help of it that are active at
		the moment, separated with spaces.


What:		/sys/power/wake_unlock
Date:		February 2012
Contact:	Rafael J. Wysocki <rjw@sisk.pl>
Description:
		The /sys/power/wake_unlock file allows user space to deactivate
		wakeup sources created with the help of /sys/power/wake_lock.
		When a string is written to /sys/power/wake_unlock, it will be
		assumed to represent the name of a wakeup source to deactivate.
		If a wakeup source object of that name exists and is active at
		the moment, it will be deactivated.

		Reads from this file return a string consisting of the names of
		wakeup sources created with the help of /sys/power/wake_lock
		that are inactive at the moment, separated with spaces.
+1 −0
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ void wakeup_source_add(struct wakeup_source *ws)
	spin_lock_init(&ws->lock);
	setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
	ws->active = false;
	ws->last_time = ktime_get();

	spin_lock_irq(&events_lock);
	list_add_rcu(&ws->entry, &wakeup_sources);
+8 −0
Original line number Diff line number Diff line
@@ -111,6 +111,14 @@ config PM_AUTOSLEEP
	Allow the kernel to trigger a system transition into a global sleep
	state automatically whenever there are no active wakeup sources.

config PM_WAKELOCKS
	bool "User space wakeup sources interface"
	depends on PM_SLEEP
	default n
	---help---
	Allow user space to create, activate and deactivate wakeup source
	objects with the help of a sysfs-based interface.

config PM_RUNTIME
	bool "Run-time PM core functionality"
	depends on !IA64_HP_SIM
+1 −0
Original line number Diff line number Diff line
@@ -10,5 +10,6 @@ obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o
obj-$(CONFIG_HIBERNATION)	+= hibernate.o snapshot.o swap.o user.o \
				   block_io.o
obj-$(CONFIG_PM_AUTOSLEEP)	+= autosleep.o
obj-$(CONFIG_PM_WAKELOCKS)	+= wakelock.o

obj-$(CONFIG_MAGIC_SYSRQ)	+= poweroff.o
+41 −0
Original line number Diff line number Diff line
@@ -431,6 +431,43 @@ static ssize_t autosleep_store(struct kobject *kobj,

power_attr(autosleep);
#endif /* CONFIG_PM_AUTOSLEEP */

#ifdef CONFIG_PM_WAKELOCKS
static ssize_t wake_lock_show(struct kobject *kobj,
			      struct kobj_attribute *attr,
			      char *buf)
{
	return pm_show_wakelocks(buf, true);
}

static ssize_t wake_lock_store(struct kobject *kobj,
			       struct kobj_attribute *attr,
			       const char *buf, size_t n)
{
	int error = pm_wake_lock(buf);
	return error ? error : n;
}

power_attr(wake_lock);

static ssize_t wake_unlock_show(struct kobject *kobj,
				struct kobj_attribute *attr,
				char *buf)
{
	return pm_show_wakelocks(buf, false);
}

static ssize_t wake_unlock_store(struct kobject *kobj,
				 struct kobj_attribute *attr,
				 const char *buf, size_t n)
{
	int error = pm_wake_unlock(buf);
	return error ? error : n;
}

power_attr(wake_unlock);

#endif /* CONFIG_PM_WAKELOCKS */
#endif /* CONFIG_PM_SLEEP */

#ifdef CONFIG_PM_TRACE
@@ -487,6 +524,10 @@ static struct attribute * g[] = {
#ifdef CONFIG_PM_AUTOSLEEP
	&autosleep_attr.attr,
#endif
#ifdef CONFIG_PM_WAKELOCKS
	&wake_lock_attr.attr,
	&wake_unlock_attr.attr,
#endif
#ifdef CONFIG_PM_DEBUG
	&pm_test_attr.attr,
#endif
Loading