2 * kernel/power/wakelock.c
4 * User space wakeup sources support.
6 * Copyright (C) 2012 Rafael J. Wysocki <rjw@sisk.pl>
8 * This code is based on the analogous interface allowing user space to
9 * manipulate wakelocks on Android.
12 #include <linux/capability.h>
13 #include <linux/ctype.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/hrtimer.h>
17 #include <linux/list.h>
18 #include <linux/rbtree.h>
19 #include <linux/slab.h>
21 //<20130327> <marc.huang> add wakelock dubug log
22 int wakelock_debug
= 1;
23 #define _TAG_WAKELOCK "WAKELOCK"
24 #define wakelock_log(fmt, ...) do { if (wakelock_debug) pr_debug("[%s][%s]" fmt, _TAG_WAKELOCK, __func__, ##__VA_ARGS__); } while (0)
25 #define wakelock_warn(fmt, ...) do { if (wakelock_debug) pr_warn("[%s][%s]" fmt, _TAG_WAKELOCK, __func__, ##__VA_ARGS__); } while (0)
27 static DEFINE_MUTEX(wakelocks_lock
);
32 struct wakeup_source ws
;
33 #ifdef CONFIG_PM_WAKELOCKS_GC
38 static struct rb_root wakelocks_tree
= RB_ROOT
;
40 ssize_t
pm_show_wakelocks(char *buf
, bool show_active
)
45 char *end
= buf
+ PAGE_SIZE
;
47 mutex_lock(&wakelocks_lock
);
49 for (node
= rb_first(&wakelocks_tree
); node
; node
= rb_next(node
)) {
50 wl
= rb_entry(node
, struct wakelock
, node
);
51 if (wl
->ws
.active
== show_active
)
52 str
+= scnprintf(str
, end
- str
, "%s ", wl
->name
);
57 str
+= scnprintf(str
, end
- str
, "\n");
59 mutex_unlock(&wakelocks_lock
);
63 #if CONFIG_PM_WAKELOCKS_LIMIT > 0
64 static unsigned int number_of_wakelocks
;
66 static inline bool wakelocks_limit_exceeded(void)
68 return number_of_wakelocks
> CONFIG_PM_WAKELOCKS_LIMIT
;
71 static inline void increment_wakelocks_number(void)
73 number_of_wakelocks
++;
76 static inline void decrement_wakelocks_number(void)
78 number_of_wakelocks
--;
80 #else /* CONFIG_PM_WAKELOCKS_LIMIT = 0 */
81 static inline bool wakelocks_limit_exceeded(void) { return false; }
82 static inline void increment_wakelocks_number(void) {}
83 static inline void decrement_wakelocks_number(void) {}
84 #endif /* CONFIG_PM_WAKELOCKS_LIMIT */
86 #ifdef CONFIG_PM_WAKELOCKS_GC
87 #define WL_GC_COUNT_MAX 100
88 #define WL_GC_TIME_SEC 300
90 static LIST_HEAD(wakelocks_lru_list
);
91 static unsigned int wakelocks_gc_count
;
93 static inline void wakelocks_lru_add(struct wakelock
*wl
)
95 list_add(&wl
->lru
, &wakelocks_lru_list
);
98 static inline void wakelocks_lru_most_recent(struct wakelock
*wl
)
100 list_move(&wl
->lru
, &wakelocks_lru_list
);
103 static void wakelocks_gc(void)
105 struct wakelock
*wl
, *aux
;
108 if (++wakelocks_gc_count
<= WL_GC_COUNT_MAX
)
112 list_for_each_entry_safe_reverse(wl
, aux
, &wakelocks_lru_list
, lru
) {
116 spin_lock_irq(&wl
->ws
.lock
);
117 idle_time_ns
= ktime_to_ns(ktime_sub(now
, wl
->ws
.last_time
));
118 active
= wl
->ws
.active
;
119 spin_unlock_irq(&wl
->ws
.lock
);
121 if (idle_time_ns
< ((u64
)WL_GC_TIME_SEC
* NSEC_PER_SEC
))
125 wakeup_source_remove(&wl
->ws
);
126 rb_erase(&wl
->node
, &wakelocks_tree
);
130 decrement_wakelocks_number();
133 wakelocks_gc_count
= 0;
135 #else /* !CONFIG_PM_WAKELOCKS_GC */
136 static inline void wakelocks_lru_add(struct wakelock
*wl
) {}
137 static inline void wakelocks_lru_most_recent(struct wakelock
*wl
) {}
138 static inline void wakelocks_gc(void) {}
139 #endif /* !CONFIG_PM_WAKELOCKS_GC */
141 static struct wakelock
*wakelock_lookup_add(const char *name
, size_t len
,
142 bool add_if_not_found
)
144 struct rb_node
**node
= &wakelocks_tree
.rb_node
;
145 struct rb_node
*parent
= *node
;
152 wl
= rb_entry(*node
, struct wakelock
, node
);
153 diff
= strncmp(name
, wl
->name
, len
);
161 node
= &(*node
)->rb_left
;
163 node
= &(*node
)->rb_right
;
165 if (!add_if_not_found
)
166 return ERR_PTR(-EINVAL
);
168 if (wakelocks_limit_exceeded())
169 return ERR_PTR(-ENOSPC
);
171 /* Not found, we have to add a new one. */
172 wl
= kzalloc(sizeof(*wl
), GFP_KERNEL
);
174 return ERR_PTR(-ENOMEM
);
176 wl
->name
= kstrndup(name
, len
, GFP_KERNEL
);
179 return ERR_PTR(-ENOMEM
);
181 wl
->ws
.name
= wl
->name
;
182 wakeup_source_add(&wl
->ws
);
183 rb_link_node(&wl
->node
, parent
, node
);
184 rb_insert_color(&wl
->node
, &wakelocks_tree
);
185 wakelocks_lru_add(wl
);
186 increment_wakelocks_number();
190 int pm_wake_lock(const char *buf
)
192 const char *str
= buf
;
199 * 20130429 marc.huang
200 * remove CAP_BLOCK_SUSPEND capability check (rollback to android kernel 3.4)
202 //if (!capable(CAP_BLOCK_SUSPEND))
205 while (*str
&& !isspace(*str
))
212 if (*str
&& *str
!= '\n') {
213 /* Find out if there's a valid timeout string appended. */
214 ret
= kstrtou64(skip_spaces(str
), 10, &timeout_ns
);
219 //<20130327> <marc.huang> add wakelock dubug log
220 wakelock_log("%s\n", buf
);
222 mutex_lock(&wakelocks_lock
);
224 wl
= wakelock_lookup_add(buf
, len
, true);
230 u64 timeout_ms
= timeout_ns
+ NSEC_PER_MSEC
- 1;
232 do_div(timeout_ms
, NSEC_PER_MSEC
);
233 __pm_wakeup_event(&wl
->ws
, timeout_ms
);
235 __pm_stay_awake(&wl
->ws
);
238 wakelocks_lru_most_recent(wl
);
241 mutex_unlock(&wakelocks_lock
);
245 int pm_wake_unlock(const char *buf
)
252 * 20130429 marc.huang
253 * remove CAP_BLOCK_SUSPEND capability check (rollback to android kernel 3.4)
255 //if (!capable(CAP_BLOCK_SUSPEND))
262 if (buf
[len
-1] == '\n')
268 //<20130327> <marc.huang> add wakelock dubug log
269 wakelock_log("%s\n", buf
);
271 mutex_lock(&wakelocks_lock
);
273 wl
= wakelock_lookup_add(buf
, len
, false);
280 wakelocks_lru_most_recent(wl
);
284 mutex_unlock(&wakelocks_lock
);