2 * drivers/base/power/wakeup.c - System wakeup events framework
4 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6 * This file is released under the GPLv2.
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/sched.h>
12 #include <linux/capability.h>
13 #include <linux/suspend.h>
14 #include <linux/seq_file.h>
15 #include <linux/debugfs.h>
22 * If set, the suspend/hibernate code will abort transitions to a sleep state
23 * if wakeup events are registered during or immediately before the transition.
25 bool events_check_enabled;
28 * Combined counters of registered wakeup events and wakeup events in progress.
29 * They need to be modified together atomically, so it's better to use one
30 * atomic variable to hold them both.
32 static atomic_t combined_event_count = ATOMIC_INIT(0);
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);
41 *cnt = (comb >> IN_PROGRESS_BITS);
42 *inpr = comb & MAX_IN_PROGRESS;
45 /* A preserved old value of the events counter. */
46 static unsigned int saved_count;
48 static DEFINE_SPINLOCK(events_lock);
50 static void pm_wakeup_timer_fn(unsigned long data);
52 static LIST_HEAD(wakeup_sources);
55 * wakeup_source_create - Create a struct wakeup_source object.
56 * @name: Name of the new wakeup source.
58 struct wakeup_source *wakeup_source_create(const char *name)
60 struct wakeup_source *ws;
62 ws = kzalloc(sizeof(*ws), GFP_KERNEL);
66 spin_lock_init(&ws->lock);
68 ws->name = kstrdup(name, GFP_KERNEL);
72 EXPORT_SYMBOL_GPL(wakeup_source_create);
75 * wakeup_source_destroy - Destroy a struct wakeup_source object.
76 * @ws: Wakeup source to destroy.
78 void wakeup_source_destroy(struct wakeup_source *ws)
83 spin_lock_irq(&ws->lock);
85 spin_unlock_irq(&ws->lock);
87 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
89 spin_lock_irq(&ws->lock);
91 spin_unlock_irq(&ws->lock);
96 EXPORT_SYMBOL_GPL(wakeup_source_destroy);
99 * wakeup_source_add - Add given object to the list of wakeup sources.
100 * @ws: Wakeup source object to add to the list.
102 void wakeup_source_add(struct wakeup_source *ws)
107 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
110 spin_lock_irq(&events_lock);
111 list_add_rcu(&ws->entry, &wakeup_sources);
112 spin_unlock_irq(&events_lock);
115 EXPORT_SYMBOL_GPL(wakeup_source_add);
118 * wakeup_source_remove - Remove given object from the wakeup sources list.
119 * @ws: Wakeup source object to remove from the list.
121 void wakeup_source_remove(struct wakeup_source *ws)
126 spin_lock_irq(&events_lock);
127 list_del_rcu(&ws->entry);
128 spin_unlock_irq(&events_lock);
131 EXPORT_SYMBOL_GPL(wakeup_source_remove);
134 * wakeup_source_register - Create wakeup source and add it to the list.
135 * @name: Name of the wakeup source to register.
137 struct wakeup_source *wakeup_source_register(const char *name)
139 struct wakeup_source *ws;
141 ws = wakeup_source_create(name);
143 wakeup_source_add(ws);
147 EXPORT_SYMBOL_GPL(wakeup_source_register);
150 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
151 * @ws: Wakeup source object to unregister.
153 void wakeup_source_unregister(struct wakeup_source *ws)
155 wakeup_source_remove(ws);
156 wakeup_source_destroy(ws);
158 EXPORT_SYMBOL_GPL(wakeup_source_unregister);
161 * device_wakeup_attach - Attach a wakeup source object to a device object.
162 * @dev: Device to handle.
163 * @ws: Wakeup source object to attach to @dev.
165 * This causes @dev to be treated as a wakeup device.
167 static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
169 spin_lock_irq(&dev->power.lock);
170 if (dev->power.wakeup) {
171 spin_unlock_irq(&dev->power.lock);
174 dev->power.wakeup = ws;
175 spin_unlock_irq(&dev->power.lock);
180 * device_wakeup_enable - Enable given device to be a wakeup source.
181 * @dev: Device to handle.
183 * Create a wakeup source object, register it and attach it to @dev.
185 int device_wakeup_enable(struct device *dev)
187 struct wakeup_source *ws;
190 if (!dev || !dev->power.can_wakeup)
193 ws = wakeup_source_register(dev_name(dev));
197 ret = device_wakeup_attach(dev, ws);
199 wakeup_source_unregister(ws);
203 EXPORT_SYMBOL_GPL(device_wakeup_enable);
206 * device_wakeup_detach - Detach a device's wakeup source object from it.
207 * @dev: Device to detach the wakeup source object from.
209 * After it returns, @dev will not be treated as a wakeup device any more.
211 static struct wakeup_source *device_wakeup_detach(struct device *dev)
213 struct wakeup_source *ws;
215 spin_lock_irq(&dev->power.lock);
216 ws = dev->power.wakeup;
217 dev->power.wakeup = NULL;
218 spin_unlock_irq(&dev->power.lock);
223 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
224 * @dev: Device to handle.
226 * Detach the @dev's wakeup source object from it, unregister this wakeup source
227 * object and destroy it.
229 int device_wakeup_disable(struct device *dev)
231 struct wakeup_source *ws;
233 if (!dev || !dev->power.can_wakeup)
236 ws = device_wakeup_detach(dev);
238 wakeup_source_unregister(ws);
242 EXPORT_SYMBOL_GPL(device_wakeup_disable);
245 * device_init_wakeup - Device wakeup initialization.
246 * @dev: Device to handle.
247 * @enable: Whether or not to enable @dev as a wakeup device.
249 * By default, most devices should leave wakeup disabled. The exceptions are
250 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
251 * possibly network interfaces, etc.
253 int device_init_wakeup(struct device *dev, bool enable)
258 device_set_wakeup_capable(dev, true);
259 ret = device_wakeup_enable(dev);
261 device_set_wakeup_capable(dev, false);
266 EXPORT_SYMBOL_GPL(device_init_wakeup);
269 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
270 * @dev: Device to handle.
272 int device_set_wakeup_enable(struct device *dev, bool enable)
274 if (!dev || !dev->power.can_wakeup)
277 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
279 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
282 * The functions below use the observation that each wakeup event starts a
283 * period in which the system should not be suspended. The moment this period
284 * will end depends on how the wakeup event is going to be processed after being
285 * detected and all of the possible cases can be divided into two distinct
288 * First, a wakeup event may be detected by the same functional unit that will
289 * carry out the entire processing of it and possibly will pass it to user space
290 * for further processing. In that case the functional unit that has detected
291 * the event may later "close" the "no suspend" period associated with it
292 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
293 * pm_relax(), balanced with each other, is supposed to be used in such
296 * Second, a wakeup event may be detected by one functional unit and processed
297 * by another one. In that case the unit that has detected it cannot really
298 * "close" the "no suspend" period associated with it, unless it knows in
299 * advance what's going to happen to the event during processing. This
300 * knowledge, however, may not be available to it, so it can simply specify time
301 * to wait before the system can be suspended and pass it as the second
302 * argument of pm_wakeup_event().
304 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
305 * "no suspend" period will be ended either by the pm_relax(), or by the timer
306 * function executed when the timer expires, whichever comes first.
310 * wakup_source_activate - Mark given wakeup source as active.
311 * @ws: Wakeup source to handle.
313 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
314 * core of the event by incrementing the counter of of wakeup events being
317 static void wakeup_source_activate(struct wakeup_source *ws)
321 ws->timer_expires = jiffies;
322 ws->last_time = ktime_get();
324 /* Increment the counter of events in progress. */
325 atomic_inc(&combined_event_count);
329 * __pm_stay_awake - Notify the PM core of a wakeup event.
330 * @ws: Wakeup source object associated with the source of the event.
332 * It is safe to call this function from interrupt context.
334 void __pm_stay_awake(struct wakeup_source *ws)
341 spin_lock_irqsave(&ws->lock, flags);
344 wakeup_source_activate(ws);
345 spin_unlock_irqrestore(&ws->lock, flags);
347 EXPORT_SYMBOL_GPL(__pm_stay_awake);
350 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
351 * @dev: Device the wakeup event is related to.
353 * Notify the PM core of a wakeup event (signaled by @dev) by calling
354 * __pm_stay_awake for the @dev's wakeup source object.
356 * Call this function after detecting of a wakeup event if pm_relax() is going
357 * to be called directly after processing the event (and possibly passing it to
358 * user space for further processing).
360 void pm_stay_awake(struct device *dev)
367 spin_lock_irqsave(&dev->power.lock, flags);
368 __pm_stay_awake(dev->power.wakeup);
369 spin_unlock_irqrestore(&dev->power.lock, flags);
371 EXPORT_SYMBOL_GPL(pm_stay_awake);
374 * wakup_source_deactivate - Mark given wakeup source as inactive.
375 * @ws: Wakeup source to handle.
377 * Update the @ws' statistics and notify the PM core that the wakeup source has
378 * become inactive by decrementing the counter of wakeup events being processed
379 * and incrementing the counter of registered wakeup events.
381 static void wakeup_source_deactivate(struct wakeup_source *ws)
388 * __pm_relax() may be called directly or from a timer function.
389 * If it is called directly right after the timer function has been
390 * started, but before the timer function calls __pm_relax(), it is
391 * possible that __pm_stay_awake() will be called in the meantime and
392 * will set ws->active. Then, ws->active may be cleared immediately
393 * by the __pm_relax() called from the timer function, but in such a
394 * case ws->relax_count will be different from ws->active_count.
396 if (ws->relax_count != ws->active_count) {
404 duration = ktime_sub(now, ws->last_time);
405 ws->total_time = ktime_add(ws->total_time, duration);
406 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
407 ws->max_time = duration;
409 del_timer(&ws->timer);
412 * Increment the counter of registered wakeup events and decrement the
413 * couter of wakeup events in progress simultaneously.
415 atomic_add(MAX_IN_PROGRESS, &combined_event_count);
419 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
420 * @ws: Wakeup source object associated with the source of the event.
422 * Call this function for wakeup events whose processing started with calling
425 * It is safe to call it from interrupt context.
427 void __pm_relax(struct wakeup_source *ws)
434 spin_lock_irqsave(&ws->lock, flags);
436 wakeup_source_deactivate(ws);
437 spin_unlock_irqrestore(&ws->lock, flags);
439 EXPORT_SYMBOL_GPL(__pm_relax);
442 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
443 * @dev: Device that signaled the event.
445 * Execute __pm_relax() for the @dev's wakeup source object.
447 void pm_relax(struct device *dev)
454 spin_lock_irqsave(&dev->power.lock, flags);
455 __pm_relax(dev->power.wakeup);
456 spin_unlock_irqrestore(&dev->power.lock, flags);
458 EXPORT_SYMBOL_GPL(pm_relax);
461 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
462 * @data: Address of the wakeup source object associated with the event source.
464 * Call __pm_relax() for the wakeup source whose address is stored in @data.
466 static void pm_wakeup_timer_fn(unsigned long data)
468 __pm_relax((struct wakeup_source *)data);
472 * __pm_wakeup_event - Notify the PM core of a wakeup event.
473 * @ws: Wakeup source object associated with the event source.
474 * @msec: Anticipated event processing time (in milliseconds).
476 * Notify the PM core of a wakeup event whose source is @ws that will take
477 * approximately @msec milliseconds to be processed by the kernel. If @ws is
478 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
479 * execute pm_wakeup_timer_fn() in future.
481 * It is safe to call this function from interrupt context.
483 void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
486 unsigned long expires;
491 spin_lock_irqsave(&ws->lock, flags);
495 wakeup_source_activate(ws);
498 wakeup_source_deactivate(ws);
502 expires = jiffies + msecs_to_jiffies(msec);
506 if (time_after(expires, ws->timer_expires)) {
507 mod_timer(&ws->timer, expires);
508 ws->timer_expires = expires;
512 spin_unlock_irqrestore(&ws->lock, flags);
514 EXPORT_SYMBOL_GPL(__pm_wakeup_event);
518 * pm_wakeup_event - Notify the PM core of a wakeup event.
519 * @dev: Device the wakeup event is related to.
520 * @msec: Anticipated event processing time (in milliseconds).
522 * Call __pm_wakeup_event() for the @dev's wakeup source object.
524 void pm_wakeup_event(struct device *dev, unsigned int msec)
531 spin_lock_irqsave(&dev->power.lock, flags);
532 __pm_wakeup_event(dev->power.wakeup, msec);
533 spin_unlock_irqrestore(&dev->power.lock, flags);
535 EXPORT_SYMBOL_GPL(pm_wakeup_event);
538 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
540 static void pm_wakeup_update_hit_counts(void)
543 struct wakeup_source *ws;
546 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
547 spin_lock_irqsave(&ws->lock, flags);
550 spin_unlock_irqrestore(&ws->lock, flags);
556 * pm_wakeup_pending - Check if power transition in progress should be aborted.
558 * Compare the current number of registered wakeup events with its preserved
559 * value from the past and return true if new wakeup events have been registered
560 * since the old value was stored. Also return true if the current number of
561 * wakeup events being processed is different from zero.
563 bool pm_wakeup_pending(void)
568 spin_lock_irqsave(&events_lock, flags);
569 if (events_check_enabled) {
570 unsigned int cnt, inpr;
572 split_counters(&cnt, &inpr);
573 ret = (cnt != saved_count || inpr > 0);
574 events_check_enabled = !ret;
576 spin_unlock_irqrestore(&events_lock, flags);
578 pm_wakeup_update_hit_counts();
583 * pm_get_wakeup_count - Read the number of registered wakeup events.
584 * @count: Address to store the value at.
586 * Store the number of registered wakeup events at the address in @count. Block
587 * if the current number of wakeup events being processed is nonzero.
589 * Return 'false' if the wait for the number of wakeup events being processed to
590 * drop down to zero has been interrupted by a signal (and the current number
591 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
593 bool pm_get_wakeup_count(unsigned int *count)
595 unsigned int cnt, inpr;
598 split_counters(&cnt, &inpr);
599 if (inpr == 0 || signal_pending(current))
601 pm_wakeup_update_hit_counts();
602 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
605 split_counters(&cnt, &inpr);
611 * pm_save_wakeup_count - Save the current number of registered wakeup events.
612 * @count: Value to compare with the current number of registered wakeup events.
614 * If @count is equal to the current number of registered wakeup events and the
615 * current number of wakeup events being processed is zero, store @count as the
616 * old number of registered wakeup events for pm_check_wakeup_events(), enable
617 * wakeup events detection and return 'true'. Otherwise disable wakeup events
618 * detection and return 'false'.
620 bool pm_save_wakeup_count(unsigned int count)
622 unsigned int cnt, inpr;
624 events_check_enabled = false;
625 spin_lock_irq(&events_lock);
626 split_counters(&cnt, &inpr);
627 if (cnt == count && inpr == 0) {
629 events_check_enabled = true;
631 spin_unlock_irq(&events_lock);
632 if (!events_check_enabled)
633 pm_wakeup_update_hit_counts();
634 return events_check_enabled;
637 static struct dentry *wakeup_sources_stats_dentry;
640 * print_wakeup_source_stats - Print wakeup source statistics information.
641 * @m: seq_file to print the statistics into.
642 * @ws: Wakeup source object to print the statistics for.
644 static int print_wakeup_source_stats(struct seq_file *m,
645 struct wakeup_source *ws)
650 unsigned long active_count;
654 spin_lock_irqsave(&ws->lock, flags);
656 total_time = ws->total_time;
657 max_time = ws->max_time;
658 active_count = ws->active_count;
660 active_time = ktime_sub(ktime_get(), ws->last_time);
661 total_time = ktime_add(total_time, active_time);
662 if (active_time.tv64 > max_time.tv64)
663 max_time = active_time;
665 active_time = ktime_set(0, 0);
668 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
669 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
670 ws->name, active_count, ws->event_count, ws->hit_count,
671 ktime_to_ms(active_time), ktime_to_ms(total_time),
672 ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
674 spin_unlock_irqrestore(&ws->lock, flags);
680 * wakeup_sources_stats_show - Print wakeup sources statistics information.
681 * @m: seq_file to print the statistics into.
683 static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
685 struct wakeup_source *ws;
687 seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
688 "active_since\ttotal_time\tmax_time\tlast_change\n");
691 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
692 print_wakeup_source_stats(m, ws);
698 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
700 return single_open(file, wakeup_sources_stats_show, NULL);
703 static const struct file_operations wakeup_sources_stats_fops = {
704 .owner = THIS_MODULE,
705 .open = wakeup_sources_stats_open,
708 .release = single_release,
711 static int __init wakeup_sources_debugfs_init(void)
713 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
714 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
718 postcore_initcall(wakeup_sources_debugfs_init);