blob: dc34b9d3b7d88c165029ae6ec48c63f07a4b7f95 [file] [log] [blame]
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +02001/*
2 * kernel/power/wakelock.c
3 *
4 * User space wakeup sources support.
5 *
6 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
7 *
8 * This code is based on the analogous interface allowing user space to
9 * manipulate wakelocks on Android.
10 */
11
12#include <linux/ctype.h>
13#include <linux/device.h>
14#include <linux/err.h>
15#include <linux/hrtimer.h>
16#include <linux/list.h>
17#include <linux/rbtree.h>
18#include <linux/slab.h>
19
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +020020#define WL_GC_COUNT_MAX 100
21#define WL_GC_TIME_SEC 300
22
23static DEFINE_MUTEX(wakelocks_lock);
24
25struct wakelock {
26 char *name;
27 struct rb_node node;
28 struct wakeup_source ws;
29 struct list_head lru;
30};
31
32static struct rb_root wakelocks_tree = RB_ROOT;
33static LIST_HEAD(wakelocks_lru_list);
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +020034static unsigned int wakelocks_gc_count;
35
36ssize_t pm_show_wakelocks(char *buf, bool show_active)
37{
38 struct rb_node *node;
39 struct wakelock *wl;
40 char *str = buf;
41 char *end = buf + PAGE_SIZE;
42
43 mutex_lock(&wakelocks_lock);
44
45 for (node = rb_first(&wakelocks_tree); node; node = rb_next(node)) {
46 wl = rb_entry(node, struct wakelock, node);
47 if (wl->ws.active == show_active)
48 str += scnprintf(str, end - str, "%s ", wl->name);
49 }
50 if (str > buf)
51 str--;
52
53 str += scnprintf(str, end - str, "\n");
54
55 mutex_unlock(&wakelocks_lock);
56 return (str - buf);
57}
58
Rafael J. Wysockic73893e2012-05-05 21:57:20 +020059#if CONFIG_PM_WAKELOCKS_LIMIT > 0
60static unsigned int number_of_wakelocks;
61
62static inline bool wakelocks_limit_exceeded(void)
63{
64 return number_of_wakelocks > CONFIG_PM_WAKELOCKS_LIMIT;
65}
66
67static inline void increment_wakelocks_number(void)
68{
69 number_of_wakelocks++;
70}
71
72static inline void decrement_wakelocks_number(void)
73{
74 number_of_wakelocks--;
75}
76#else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
77static inline bool wakelocks_limit_exceeded(void) { return false; }
78static inline void increment_wakelocks_number(void) {}
79static inline void decrement_wakelocks_number(void) {}
80#endif /* CONFIG_PM_WAKELOCKS_LIMIT */
81
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +020082static struct wakelock *wakelock_lookup_add(const char *name, size_t len,
83 bool add_if_not_found)
84{
85 struct rb_node **node = &wakelocks_tree.rb_node;
86 struct rb_node *parent = *node;
87 struct wakelock *wl;
88
89 while (*node) {
90 int diff;
91
92 parent = *node;
93 wl = rb_entry(*node, struct wakelock, node);
94 diff = strncmp(name, wl->name, len);
95 if (diff == 0) {
96 if (wl->name[len])
97 diff = -1;
98 else
99 return wl;
100 }
101 if (diff < 0)
102 node = &(*node)->rb_left;
103 else
104 node = &(*node)->rb_right;
105 }
106 if (!add_if_not_found)
107 return ERR_PTR(-EINVAL);
108
Rafael J. Wysockic73893e2012-05-05 21:57:20 +0200109 if (wakelocks_limit_exceeded())
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200110 return ERR_PTR(-ENOSPC);
111
112 /* Not found, we have to add a new one. */
113 wl = kzalloc(sizeof(*wl), GFP_KERNEL);
114 if (!wl)
115 return ERR_PTR(-ENOMEM);
116
117 wl->name = kstrndup(name, len, GFP_KERNEL);
118 if (!wl->name) {
119 kfree(wl);
120 return ERR_PTR(-ENOMEM);
121 }
122 wl->ws.name = wl->name;
123 wakeup_source_add(&wl->ws);
124 rb_link_node(&wl->node, parent, node);
125 rb_insert_color(&wl->node, &wakelocks_tree);
126 list_add(&wl->lru, &wakelocks_lru_list);
Rafael J. Wysockic73893e2012-05-05 21:57:20 +0200127 increment_wakelocks_number();
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200128 return wl;
129}
130
131int pm_wake_lock(const char *buf)
132{
133 const char *str = buf;
134 struct wakelock *wl;
135 u64 timeout_ns = 0;
136 size_t len;
137 int ret = 0;
138
139 while (*str && !isspace(*str))
140 str++;
141
142 len = str - buf;
143 if (!len)
144 return -EINVAL;
145
146 if (*str && *str != '\n') {
147 /* Find out if there's a valid timeout string appended. */
148 ret = kstrtou64(skip_spaces(str), 10, &timeout_ns);
149 if (ret)
150 return -EINVAL;
151 }
152
153 mutex_lock(&wakelocks_lock);
154
155 wl = wakelock_lookup_add(buf, len, true);
156 if (IS_ERR(wl)) {
157 ret = PTR_ERR(wl);
158 goto out;
159 }
160 if (timeout_ns) {
161 u64 timeout_ms = timeout_ns + NSEC_PER_MSEC - 1;
162
163 do_div(timeout_ms, NSEC_PER_MSEC);
164 __pm_wakeup_event(&wl->ws, timeout_ms);
165 } else {
166 __pm_stay_awake(&wl->ws);
167 }
168
169 list_move(&wl->lru, &wakelocks_lru_list);
170
171 out:
172 mutex_unlock(&wakelocks_lock);
173 return ret;
174}
175
176static void wakelocks_gc(void)
177{
178 struct wakelock *wl, *aux;
179 ktime_t now = ktime_get();
180
181 list_for_each_entry_safe_reverse(wl, aux, &wakelocks_lru_list, lru) {
182 u64 idle_time_ns;
183 bool active;
184
185 spin_lock_irq(&wl->ws.lock);
186 idle_time_ns = ktime_to_ns(ktime_sub(now, wl->ws.last_time));
187 active = wl->ws.active;
188 spin_unlock_irq(&wl->ws.lock);
189
190 if (idle_time_ns < ((u64)WL_GC_TIME_SEC * NSEC_PER_SEC))
191 break;
192
193 if (!active) {
194 wakeup_source_remove(&wl->ws);
195 rb_erase(&wl->node, &wakelocks_tree);
196 list_del(&wl->lru);
197 kfree(wl->name);
198 kfree(wl);
Rafael J. Wysockic73893e2012-05-05 21:57:20 +0200199 decrement_wakelocks_number();
Rafael J. Wysockib86ff9822012-04-29 22:53:42 +0200200 }
201 }
202 wakelocks_gc_count = 0;
203}
204
205int pm_wake_unlock(const char *buf)
206{
207 struct wakelock *wl;
208 size_t len;
209 int ret = 0;
210
211 len = strlen(buf);
212 if (!len)
213 return -EINVAL;
214
215 if (buf[len-1] == '\n')
216 len--;
217
218 if (!len)
219 return -EINVAL;
220
221 mutex_lock(&wakelocks_lock);
222
223 wl = wakelock_lookup_add(buf, len, false);
224 if (IS_ERR(wl)) {
225 ret = PTR_ERR(wl);
226 goto out;
227 }
228 __pm_relax(&wl->ws);
229 list_move(&wl->lru, &wakelocks_lru_list);
230 if (++wakelocks_gc_count > WL_GC_COUNT_MAX)
231 wakelocks_gc();
232
233 out:
234 mutex_unlock(&wakelocks_lock);
235 return ret;
236}