9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/capability.h>
13 #include <linux/export.h>
34 #define IN_PROGRESS_BITS (sizeof(int) * 4)
35 #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
37 static void split_counters(
unsigned int *
cnt,
unsigned int *inpr)
39 unsigned int comb =
atomic_read(&combined_event_count);
46 static unsigned int saved_count;
50 static void pm_wakeup_timer_fn(
unsigned long data);
67 memset(ws, 0,
sizeof(*ws));
141 list_add_rcu(&ws->entry, &wakeup_sources);
142 spin_unlock_irqrestore(&events_lock, flags);
158 list_del_rcu(&ws->
entry);
159 spin_unlock_irqrestore(&events_lock, flags);
202 spin_lock_irq(&dev->
power.lock);
203 if (dev->
power.wakeup) {
204 spin_unlock_irq(&dev->
power.lock);
208 spin_unlock_irq(&dev->
power.lock);
223 if (!dev || !dev->
power.can_wakeup)
230 ret = device_wakeup_attach(dev, ws);
248 spin_lock_irq(&dev->
power.lock);
249 ws = dev->
power.wakeup;
251 spin_unlock_irq(&dev->
power.lock);
266 if (!dev || !dev->
power.can_wakeup)
269 ws = device_wakeup_detach(dev);
291 if (!!dev->
power.can_wakeup == !!capable)
294 if (device_is_registered(dev) && !list_empty(&dev->
power.entry)) {
338 if (!dev || !dev->
power.can_wakeup)
394 trace_wakeup_source_activate(ws->
name, cec);
401 static void wakeup_source_report_event(
struct wakeup_source *ws)
405 if (events_check_enabled)
409 wakeup_source_activate(ws);
427 wakeup_source_report_event(ws);
431 spin_unlock_irqrestore(&ws->
lock, flags);
455 spin_unlock_irqrestore(&dev->
power.lock, flags);
459 #ifdef CONFIG_PM_AUTOSLEEP
466 static inline void update_prevent_sleep_time(
struct wakeup_source *ws,
478 static void wakeup_source_deactivate(
struct wakeup_source *ws)
480 unsigned int cnt, inpr, cec;
502 duration = ktime_sub(now, ws->
last_time);
504 if (ktime_to_ns(duration) > ktime_to_ns(ws->
max_time))
512 update_prevent_sleep_time(ws, now);
519 trace_wakeup_source_deactivate(ws->
name, cec);
521 split_counters(&cnt, &inpr);
522 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
523 wake_up(&wakeup_count_wait_queue);
544 wakeup_source_deactivate(ws);
545 spin_unlock_irqrestore(&ws->
lock, flags);
564 spin_unlock_irqrestore(&dev->
power.lock, flags);
576 static void pm_wakeup_timer_fn(
unsigned long data)
585 wakeup_source_deactivate(ws);
589 spin_unlock_irqrestore(&ws->
lock, flags);
607 unsigned long expires;
614 wakeup_source_report_event(ws);
617 wakeup_source_deactivate(ws);
631 spin_unlock_irqrestore(&ws->
lock, flags);
652 spin_unlock_irqrestore(&dev->
power.lock, flags);
656 static void print_active_wakeup_sources(
void)
663 list_for_each_entry_rcu(ws, &wakeup_sources,
entry) {
667 }
else if (!active &&
668 (!last_activity_ws ||
670 ktime_to_ns(last_activity_ws->
last_time))) {
671 last_activity_ws =
ws;
675 if (!active && last_activity_ws)
676 pr_info(
"last active wakeup source: %s\n",
677 last_activity_ws->
name);
695 if (events_check_enabled) {
696 unsigned int cnt, inpr;
698 split_counters(&cnt, &inpr);
699 ret = (cnt != saved_count || inpr > 0);
700 events_check_enabled = !
ret;
702 spin_unlock_irqrestore(&events_lock, flags);
705 print_active_wakeup_sources();
724 unsigned int cnt, inpr;
732 split_counters(&cnt, &inpr);
733 if (inpr == 0 || signal_pending(
current))
741 split_counters(&cnt, &inpr);
758 unsigned int cnt, inpr;
761 events_check_enabled =
false;
763 split_counters(&cnt, &inpr);
764 if (cnt == count && inpr == 0) {
766 events_check_enabled =
true;
768 spin_unlock_irqrestore(&events_lock, flags);
769 return events_check_enabled;
772 #ifdef CONFIG_PM_AUTOSLEEP
777 void pm_wakep_autosleep_enabled(
bool set)
783 list_for_each_entry_rcu(ws, &wakeup_sources,
entry) {
784 spin_lock_irq(&ws->
lock);
791 update_prevent_sleep_time(ws, now);
794 spin_unlock_irq(&ws->
lock);
800 static struct dentry *wakeup_sources_stats_dentry;
807 static int print_wakeup_source_stats(
struct seq_file *
m,
813 unsigned long active_count;
827 active_time = ktime_sub(now, ws->
last_time);
828 total_time = ktime_add(total_time, active_time);
829 if (active_time.
tv64 > max_time.
tv64)
830 max_time = active_time;
833 prevent_sleep_time = ktime_add(prevent_sleep_time,
836 active_time = ktime_set(0, 0);
839 ret =
seq_printf(m,
"%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t"
840 "%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
843 ktime_to_ms(active_time), ktime_to_ms(total_time),
844 ktime_to_ms(max_time), ktime_to_ms(ws->
last_time),
845 ktime_to_ms(prevent_sleep_time));
847 spin_unlock_irqrestore(&ws->
lock, flags);
856 static int wakeup_sources_stats_show(
struct seq_file *m,
void *
unused)
860 seq_puts(m,
"name\t\tactive_count\tevent_count\twakeup_count\t"
861 "expire_count\tactive_since\ttotal_time\tmax_time\t"
862 "last_change\tprevent_suspend_time\n");
865 list_for_each_entry_rcu(ws, &wakeup_sources,
entry)
866 print_wakeup_source_stats(m, ws);
879 .open = wakeup_sources_stats_open,
885 static int __init wakeup_sources_debugfs_init(
void)