1 From 48e1af2bdd11204f11b3770a6c8d3eee64aee2e8 Mon Sep 17 00:00:00 2001
2 From: =?utf-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@android.com>
3 Date: Thu, 9 Oct 2008 21:01:46 -0700
4 Subject: [PATCH 069/134] PM: Add user-space wake lock api.
6 This adds /sys/power/wake_lock and /sys/power/wake_unlock.
7 Writing a string to wake_lock creates a wake lock the
8 first time is sees a string and locks it. Optionally, the
9 string can be followed by a timeout.
10 To unlock the wake lock, write the same string to wake_unlock.
12 kernel/power/Kconfig | 10 ++
13 kernel/power/Makefile | 1 +
14 kernel/power/main.c | 9 ++
15 kernel/power/power.h | 11 ++
16 kernel/power/userwakelock.c | 218 +++++++++++++++++++++++++++++++++++++++++++
17 5 files changed, 249 insertions(+), 0 deletions(-)
18 create mode 100644 kernel/power/userwakelock.c
20 --- a/kernel/power/Kconfig
21 +++ b/kernel/power/Kconfig
22 @@ -138,6 +138,16 @@ config WAKELOCK_STAT
24 Report wake lock stats in /proc/wakelocks
27 + bool "Userspace wake locks"
31 + User-space wake lock api. Write "lockname" or "lockname timeout"
32 + to /sys/power/wake_lock lock and if needed create a wake lock.
33 + Write "lockname" to /sys/power/wake_unlock to unlock a user wake
39 --- a/kernel/power/Makefile
40 +++ b/kernel/power/Makefile
41 @@ -7,6 +7,7 @@ obj-$(CONFIG_PM) += main.o
42 obj-$(CONFIG_PM_SLEEP) += console.o
43 obj-$(CONFIG_FREEZER) += process.o
44 obj-$(CONFIG_WAKELOCK) += wakelock.o
45 +obj-$(CONFIG_USER_WAKELOCK) += userwakelock.o
46 obj-$(CONFIG_EARLYSUSPEND) += earlysuspend.o
47 obj-$(CONFIG_HIBERNATION) += swsusp.o disk.o snapshot.o swap.o user.o
49 --- a/kernel/power/main.c
50 +++ b/kernel/power/main.c
51 @@ -595,6 +595,11 @@ pm_trace_store(struct kobject *kobj, str
53 #endif /* CONFIG_PM_TRACE */
55 +#ifdef CONFIG_USER_WAKELOCK
56 +power_attr(wake_lock);
57 +power_attr(wake_unlock);
60 static struct attribute * g[] = {
62 #ifdef CONFIG_PM_TRACE
63 @@ -603,6 +608,10 @@ static struct attribute * g[] = {
64 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
67 +#ifdef CONFIG_USER_WAKELOCK
68 + &wake_lock_attr.attr,
69 + &wake_unlock_attr.attr,
74 --- a/kernel/power/power.h
75 +++ b/kernel/power/power.h
76 @@ -231,6 +231,17 @@ extern struct wake_lock main_wake_lock;
77 extern suspend_state_t requested_suspend_state;
80 +#ifdef CONFIG_USER_WAKELOCK
81 +ssize_t wake_lock_show(struct kobject *kobj, struct kobj_attribute *attr,
83 +ssize_t wake_lock_store(struct kobject *kobj, struct kobj_attribute *attr,
84 + const char *buf, size_t n);
85 +ssize_t wake_unlock_show(struct kobject *kobj, struct kobj_attribute *attr,
87 +ssize_t wake_unlock_store(struct kobject *kobj, struct kobj_attribute *attr,
88 + const char *buf, size_t n);
91 #ifdef CONFIG_EARLYSUSPEND
92 /* kernel/power/earlysuspend.c */
93 void request_suspend_state(suspend_state_t state);
95 +++ b/kernel/power/userwakelock.c
97 +/* kernel/power/userwakelock.c
99 + * Copyright (C) 2005-2008 Google, Inc.
101 + * This software is licensed under the terms of the GNU General Public
102 + * License version 2, as published by the Free Software Foundation, and
103 + * may be copied, distributed, and modified under those terms.
105 + * This program is distributed in the hope that it will be useful,
106 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
107 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
108 + * GNU General Public License for more details.
112 +#include <linux/ctype.h>
113 +#include <linux/module.h>
114 +#include <linux/wakelock.h>
119 + DEBUG_FAILURE = BIT(0),
120 + DEBUG_ERROR = BIT(1),
121 + DEBUG_NEW = BIT(2),
122 + DEBUG_ACCESS = BIT(3),
123 + DEBUG_LOOKUP = BIT(4),
125 +static int debug_mask = DEBUG_FAILURE;
126 +module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
128 +static DEFINE_MUTEX(tree_lock);
130 +struct user_wake_lock {
131 + struct rb_node node;
132 + struct wake_lock wake_lock;
135 +struct rb_root user_wake_locks;
137 +static struct user_wake_lock *lookup_wake_lock_name(
138 + const char *buf, int allocate, long *timeoutptr)
140 + struct rb_node **p = &user_wake_locks.rb_node;
141 + struct rb_node *parent = NULL;
142 + struct user_wake_lock *l;
148 + /* Find length of lock name and start of optional timeout string */
150 + while (*arg && !isspace(*arg))
152 + name_len = arg - buf;
155 + while (isspace(*arg))
158 + /* Process timeout string */
159 + if (timeoutptr && *arg) {
160 + timeout = simple_strtoull(arg, (char **)&arg, 0);
161 + while (isspace(*arg))
165 + /* convert timeout from nanoseconds to jiffies > 0 */
166 + timeout += (NSEC_PER_SEC / HZ) - 1;
167 + do_div(timeout, (NSEC_PER_SEC / HZ));
170 + *timeoutptr = timeout;
173 + else if (timeoutptr)
176 + /* Lookup wake lock in rbtree */
179 + l = rb_entry(parent, struct user_wake_lock, node);
180 + diff = strncmp(buf, l->name, name_len);
181 + if (!diff && l->name[name_len])
183 + if (debug_mask & DEBUG_ERROR)
184 + pr_info("lookup_wake_lock_name: compare %.*s %s %d\n",
185 + name_len, buf, l->name, diff);
188 + p = &(*p)->rb_left;
190 + p = &(*p)->rb_right;
195 + /* Allocate and add new wakelock to rbtree */
197 + if (debug_mask & DEBUG_ERROR)
198 + pr_info("lookup_wake_lock_name: %.*s not found\n",
200 + return ERR_PTR(-EINVAL);
202 + l = kzalloc(sizeof(*l) + name_len + 1, GFP_KERNEL);
204 + if (debug_mask & DEBUG_FAILURE)
205 + pr_err("lookup_wake_lock_name: failed to allocate "
206 + "memory for %.*s\n", name_len, buf);
207 + return ERR_PTR(-ENOMEM);
209 + memcpy(l->name, buf, name_len);
210 + if (debug_mask & DEBUG_NEW)
211 + pr_info("lookup_wake_lock_name: new wake lock %s\n", l->name);
212 + wake_lock_init(&l->wake_lock, WAKE_LOCK_SUSPEND, l->name);
213 + rb_link_node(&l->node, parent, p);
214 + rb_insert_color(&l->node, &user_wake_locks);
218 + if (debug_mask & DEBUG_ERROR)
219 + pr_info("lookup_wake_lock_name: wake lock, %.*s, bad arg, %s\n",
220 + name_len, buf, arg);
221 + return ERR_PTR(-EINVAL);
224 +ssize_t wake_lock_show(
225 + struct kobject *kobj, struct kobj_attribute *attr, char *buf)
228 + char *end = buf + PAGE_SIZE;
230 + struct user_wake_lock *l;
232 + mutex_lock(&tree_lock);
234 + for (n = rb_first(&user_wake_locks); n != NULL; n = rb_next(n)) {
235 + l = rb_entry(n, struct user_wake_lock, node);
236 + if (wake_lock_active(&l->wake_lock))
237 + s += scnprintf(s, end - s, "%s ", l->name);
239 + s += scnprintf(s, end - s, "\n");
241 + mutex_unlock(&tree_lock);
245 +ssize_t wake_lock_store(
246 + struct kobject *kobj, struct kobj_attribute *attr,
247 + const char *buf, size_t n)
250 + struct user_wake_lock *l;
252 + mutex_lock(&tree_lock);
253 + l = lookup_wake_lock_name(buf, 1, &timeout);
259 + if (debug_mask & DEBUG_ACCESS)
260 + pr_info("wake_lock_store: %s, timeout %ld\n", l->name, timeout);
263 + wake_lock_timeout(&l->wake_lock, timeout);
265 + wake_lock(&l->wake_lock);
267 + mutex_unlock(&tree_lock);
272 +ssize_t wake_unlock_show(
273 + struct kobject *kobj, struct kobj_attribute *attr, char *buf)
276 + char *end = buf + PAGE_SIZE;
278 + struct user_wake_lock *l;
280 + mutex_lock(&tree_lock);
282 + for (n = rb_first(&user_wake_locks); n != NULL; n = rb_next(n)) {
283 + l = rb_entry(n, struct user_wake_lock, node);
284 + if (!wake_lock_active(&l->wake_lock))
285 + s += scnprintf(s, end - s, "%s ", l->name);
287 + s += scnprintf(s, end - s, "\n");
289 + mutex_unlock(&tree_lock);
293 +ssize_t wake_unlock_store(
294 + struct kobject *kobj, struct kobj_attribute *attr,
295 + const char *buf, size_t n)
297 + struct user_wake_lock *l;
299 + mutex_lock(&tree_lock);
300 + l = lookup_wake_lock_name(buf, 0, NULL);
306 + if (debug_mask & DEBUG_ACCESS)
307 + pr_info("wake_unlock_store: %s\n", l->name);
309 + wake_unlock(&l->wake_lock);
311 + mutex_unlock(&tree_lock);