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
;
27 /* The counter of registered wakeup events. */
28 static atomic_t event_count
= ATOMIC_INIT(0);
29 /* A preserved old value of event_count. */
30 static unsigned int saved_count
;
31 /* The counter of wakeup events being processed. */
32 static atomic_t events_in_progress
= ATOMIC_INIT(0);
34 static DEFINE_SPINLOCK(events_lock
);
36 static void pm_wakeup_timer_fn(unsigned long data
);
38 static LIST_HEAD(wakeup_sources
);
41 * wakeup_source_create - Create a struct wakeup_source object.
42 * @name: Name of the new wakeup source.
44 struct wakeup_source
*wakeup_source_create(const char *name
)
46 struct wakeup_source
*ws
;
48 ws
= kzalloc(sizeof(*ws
), GFP_KERNEL
);
52 spin_lock_init(&ws
->lock
);
54 ws
->name
= kstrdup(name
, GFP_KERNEL
);
58 EXPORT_SYMBOL_GPL(wakeup_source_create
);
61 * wakeup_source_destroy - Destroy a struct wakeup_source object.
62 * @ws: Wakeup source to destroy.
64 void wakeup_source_destroy(struct wakeup_source
*ws
)
69 spin_lock_irq(&ws
->lock
);
71 spin_unlock_irq(&ws
->lock
);
73 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT
));
75 spin_lock_irq(&ws
->lock
);
77 spin_unlock_irq(&ws
->lock
);
82 EXPORT_SYMBOL_GPL(wakeup_source_destroy
);
85 * wakeup_source_add - Add given object to the list of wakeup sources.
86 * @ws: Wakeup source object to add to the list.
88 void wakeup_source_add(struct wakeup_source
*ws
)
93 setup_timer(&ws
->timer
, pm_wakeup_timer_fn
, (unsigned long)ws
);
96 spin_lock_irq(&events_lock
);
97 list_add_rcu(&ws
->entry
, &wakeup_sources
);
98 spin_unlock_irq(&events_lock
);
101 EXPORT_SYMBOL_GPL(wakeup_source_add
);
104 * wakeup_source_remove - Remove given object from the wakeup sources list.
105 * @ws: Wakeup source object to remove from the list.
107 void wakeup_source_remove(struct wakeup_source
*ws
)
112 spin_lock_irq(&events_lock
);
113 list_del_rcu(&ws
->entry
);
114 spin_unlock_irq(&events_lock
);
117 EXPORT_SYMBOL_GPL(wakeup_source_remove
);
120 * wakeup_source_register - Create wakeup source and add it to the list.
121 * @name: Name of the wakeup source to register.
123 struct wakeup_source
*wakeup_source_register(const char *name
)
125 struct wakeup_source
*ws
;
127 ws
= wakeup_source_create(name
);
129 wakeup_source_add(ws
);
133 EXPORT_SYMBOL_GPL(wakeup_source_register
);
136 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
137 * @ws: Wakeup source object to unregister.
139 void wakeup_source_unregister(struct wakeup_source
*ws
)
141 wakeup_source_remove(ws
);
142 wakeup_source_destroy(ws
);
144 EXPORT_SYMBOL_GPL(wakeup_source_unregister
);
147 * device_wakeup_attach - Attach a wakeup source object to a device object.
148 * @dev: Device to handle.
149 * @ws: Wakeup source object to attach to @dev.
151 * This causes @dev to be treated as a wakeup device.
153 static int device_wakeup_attach(struct device
*dev
, struct wakeup_source
*ws
)
155 spin_lock_irq(&dev
->power
.lock
);
156 if (dev
->power
.wakeup
) {
157 spin_unlock_irq(&dev
->power
.lock
);
160 dev
->power
.wakeup
= ws
;
161 spin_unlock_irq(&dev
->power
.lock
);
166 * device_wakeup_enable - Enable given device to be a wakeup source.
167 * @dev: Device to handle.
169 * Create a wakeup source object, register it and attach it to @dev.
171 int device_wakeup_enable(struct device
*dev
)
173 struct wakeup_source
*ws
;
176 if (!dev
|| !dev
->power
.can_wakeup
)
179 ws
= wakeup_source_register(dev_name(dev
));
183 ret
= device_wakeup_attach(dev
, ws
);
185 wakeup_source_unregister(ws
);
189 EXPORT_SYMBOL_GPL(device_wakeup_enable
);
192 * device_wakeup_detach - Detach a device's wakeup source object from it.
193 * @dev: Device to detach the wakeup source object from.
195 * After it returns, @dev will not be treated as a wakeup device any more.
197 static struct wakeup_source
*device_wakeup_detach(struct device
*dev
)
199 struct wakeup_source
*ws
;
201 spin_lock_irq(&dev
->power
.lock
);
202 ws
= dev
->power
.wakeup
;
203 dev
->power
.wakeup
= NULL
;
204 spin_unlock_irq(&dev
->power
.lock
);
209 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
210 * @dev: Device to handle.
212 * Detach the @dev's wakeup source object from it, unregister this wakeup source
213 * object and destroy it.
215 int device_wakeup_disable(struct device
*dev
)
217 struct wakeup_source
*ws
;
219 if (!dev
|| !dev
->power
.can_wakeup
)
222 ws
= device_wakeup_detach(dev
);
224 wakeup_source_unregister(ws
);
228 EXPORT_SYMBOL_GPL(device_wakeup_disable
);
231 * device_init_wakeup - Device wakeup initialization.
232 * @dev: Device to handle.
233 * @enable: Whether or not to enable @dev as a wakeup device.
235 * By default, most devices should leave wakeup disabled. The exceptions are
236 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
237 * possibly network interfaces, etc.
239 int device_init_wakeup(struct device
*dev
, bool enable
)
244 device_set_wakeup_capable(dev
, true);
245 ret
= device_wakeup_enable(dev
);
247 device_set_wakeup_capable(dev
, false);
252 EXPORT_SYMBOL_GPL(device_init_wakeup
);
255 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
256 * @dev: Device to handle.
258 int device_set_wakeup_enable(struct device
*dev
, bool enable
)
260 if (!dev
|| !dev
->power
.can_wakeup
)
263 return enable
? device_wakeup_enable(dev
) : device_wakeup_disable(dev
);
265 EXPORT_SYMBOL_GPL(device_set_wakeup_enable
);
268 * The functions below use the observation that each wakeup event starts a
269 * period in which the system should not be suspended. The moment this period
270 * will end depends on how the wakeup event is going to be processed after being
271 * detected and all of the possible cases can be divided into two distinct
274 * First, a wakeup event may be detected by the same functional unit that will
275 * carry out the entire processing of it and possibly will pass it to user space
276 * for further processing. In that case the functional unit that has detected
277 * the event may later "close" the "no suspend" period associated with it
278 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
279 * pm_relax(), balanced with each other, is supposed to be used in such
282 * Second, a wakeup event may be detected by one functional unit and processed
283 * by another one. In that case the unit that has detected it cannot really
284 * "close" the "no suspend" period associated with it, unless it knows in
285 * advance what's going to happen to the event during processing. This
286 * knowledge, however, may not be available to it, so it can simply specify time
287 * to wait before the system can be suspended and pass it as the second
288 * argument of pm_wakeup_event().
290 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
291 * "no suspend" period will be ended either by the pm_relax(), or by the timer
292 * function executed when the timer expires, whichever comes first.
296 * wakup_source_activate - Mark given wakeup source as active.
297 * @ws: Wakeup source to handle.
299 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
300 * core of the event by incrementing the counter of of wakeup events being
303 static void wakeup_source_activate(struct wakeup_source
*ws
)
307 ws
->timer_expires
= jiffies
;
308 ws
->last_time
= ktime_get();
310 atomic_inc(&events_in_progress
);
314 * __pm_stay_awake - Notify the PM core of a wakeup event.
315 * @ws: Wakeup source object associated with the source of the event.
317 * It is safe to call this function from interrupt context.
319 void __pm_stay_awake(struct wakeup_source
*ws
)
326 spin_lock_irqsave(&ws
->lock
, flags
);
329 wakeup_source_activate(ws
);
330 spin_unlock_irqrestore(&ws
->lock
, flags
);
332 EXPORT_SYMBOL_GPL(__pm_stay_awake
);
335 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
336 * @dev: Device the wakeup event is related to.
338 * Notify the PM core of a wakeup event (signaled by @dev) by calling
339 * __pm_stay_awake for the @dev's wakeup source object.
341 * Call this function after detecting of a wakeup event if pm_relax() is going
342 * to be called directly after processing the event (and possibly passing it to
343 * user space for further processing).
345 void pm_stay_awake(struct device
*dev
)
352 spin_lock_irqsave(&dev
->power
.lock
, flags
);
353 __pm_stay_awake(dev
->power
.wakeup
);
354 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
356 EXPORT_SYMBOL_GPL(pm_stay_awake
);
359 * wakup_source_deactivate - Mark given wakeup source as inactive.
360 * @ws: Wakeup source to handle.
362 * Update the @ws' statistics and notify the PM core that the wakeup source has
363 * become inactive by decrementing the counter of wakeup events being processed
364 * and incrementing the counter of registered wakeup events.
366 static void wakeup_source_deactivate(struct wakeup_source
*ws
)
373 * __pm_relax() may be called directly or from a timer function.
374 * If it is called directly right after the timer function has been
375 * started, but before the timer function calls __pm_relax(), it is
376 * possible that __pm_stay_awake() will be called in the meantime and
377 * will set ws->active. Then, ws->active may be cleared immediately
378 * by the __pm_relax() called from the timer function, but in such a
379 * case ws->relax_count will be different from ws->active_count.
381 if (ws
->relax_count
!= ws
->active_count
) {
389 duration
= ktime_sub(now
, ws
->last_time
);
390 ws
->total_time
= ktime_add(ws
->total_time
, duration
);
391 if (ktime_to_ns(duration
) > ktime_to_ns(ws
->max_time
))
392 ws
->max_time
= duration
;
394 del_timer(&ws
->timer
);
397 * event_count has to be incremented before events_in_progress is
398 * modified, so that the callers of pm_check_wakeup_events() and
399 * pm_save_wakeup_count() don't see the old value of event_count and
400 * events_in_progress equal to zero at the same time.
402 atomic_inc(&event_count
);
403 smp_mb__before_atomic_dec();
404 atomic_dec(&events_in_progress
);
408 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
409 * @ws: Wakeup source object associated with the source of the event.
411 * Call this function for wakeup events whose processing started with calling
414 * It is safe to call it from interrupt context.
416 void __pm_relax(struct wakeup_source
*ws
)
423 spin_lock_irqsave(&ws
->lock
, flags
);
425 wakeup_source_deactivate(ws
);
426 spin_unlock_irqrestore(&ws
->lock
, flags
);
428 EXPORT_SYMBOL_GPL(__pm_relax
);
431 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
432 * @dev: Device that signaled the event.
434 * Execute __pm_relax() for the @dev's wakeup source object.
436 void pm_relax(struct device
*dev
)
443 spin_lock_irqsave(&dev
->power
.lock
, flags
);
444 __pm_relax(dev
->power
.wakeup
);
445 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
447 EXPORT_SYMBOL_GPL(pm_relax
);
450 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
451 * @data: Address of the wakeup source object associated with the event source.
453 * Call __pm_relax() for the wakeup source whose address is stored in @data.
455 static void pm_wakeup_timer_fn(unsigned long data
)
457 __pm_relax((struct wakeup_source
*)data
);
461 * __pm_wakeup_event - Notify the PM core of a wakeup event.
462 * @ws: Wakeup source object associated with the event source.
463 * @msec: Anticipated event processing time (in milliseconds).
465 * Notify the PM core of a wakeup event whose source is @ws that will take
466 * approximately @msec milliseconds to be processed by the kernel. If @ws is
467 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
468 * execute pm_wakeup_timer_fn() in future.
470 * It is safe to call this function from interrupt context.
472 void __pm_wakeup_event(struct wakeup_source
*ws
, unsigned int msec
)
475 unsigned long expires
;
480 spin_lock_irqsave(&ws
->lock
, flags
);
484 wakeup_source_activate(ws
);
487 wakeup_source_deactivate(ws
);
491 expires
= jiffies
+ msecs_to_jiffies(msec
);
495 if (time_after(expires
, ws
->timer_expires
)) {
496 mod_timer(&ws
->timer
, expires
);
497 ws
->timer_expires
= expires
;
501 spin_unlock_irqrestore(&ws
->lock
, flags
);
503 EXPORT_SYMBOL_GPL(__pm_wakeup_event
);
507 * pm_wakeup_event - Notify the PM core of a wakeup event.
508 * @dev: Device the wakeup event is related to.
509 * @msec: Anticipated event processing time (in milliseconds).
511 * Call __pm_wakeup_event() for the @dev's wakeup source object.
513 void pm_wakeup_event(struct device
*dev
, unsigned int msec
)
520 spin_lock_irqsave(&dev
->power
.lock
, flags
);
521 __pm_wakeup_event(dev
->power
.wakeup
, msec
);
522 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
524 EXPORT_SYMBOL_GPL(pm_wakeup_event
);
527 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
529 static void pm_wakeup_update_hit_counts(void)
532 struct wakeup_source
*ws
;
535 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
) {
536 spin_lock_irqsave(&ws
->lock
, flags
);
539 spin_unlock_irqrestore(&ws
->lock
, flags
);
545 * pm_wakeup_pending - Check if power transition in progress should be aborted.
547 * Compare the current number of registered wakeup events with its preserved
548 * value from the past and return true if new wakeup events have been registered
549 * since the old value was stored. Also return true if the current number of
550 * wakeup events being processed is different from zero.
552 bool pm_wakeup_pending(void)
557 spin_lock_irqsave(&events_lock
, flags
);
558 if (events_check_enabled
) {
559 ret
= ((unsigned int)atomic_read(&event_count
) != saved_count
)
560 || atomic_read(&events_in_progress
);
561 events_check_enabled
= !ret
;
563 spin_unlock_irqrestore(&events_lock
, flags
);
565 pm_wakeup_update_hit_counts();
570 * pm_get_wakeup_count - Read the number of registered wakeup events.
571 * @count: Address to store the value at.
573 * Store the number of registered wakeup events at the address in @count. Block
574 * if the current number of wakeup events being processed is nonzero.
576 * Return false if the wait for the number of wakeup events being processed to
577 * drop down to zero has been interrupted by a signal (and the current number
578 * of wakeup events being processed is still nonzero). Otherwise return true.
580 bool pm_get_wakeup_count(unsigned int *count
)
584 if (capable(CAP_SYS_ADMIN
))
585 events_check_enabled
= false;
587 while (atomic_read(&events_in_progress
) && !signal_pending(current
)) {
588 pm_wakeup_update_hit_counts();
589 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT
));
592 ret
= !atomic_read(&events_in_progress
);
593 *count
= atomic_read(&event_count
);
598 * pm_save_wakeup_count - Save the current number of registered wakeup events.
599 * @count: Value to compare with the current number of registered wakeup events.
601 * If @count is equal to the current number of registered wakeup events and the
602 * current number of wakeup events being processed is zero, store @count as the
603 * old number of registered wakeup events to be used by pm_check_wakeup_events()
604 * and return true. Otherwise return false.
606 bool pm_save_wakeup_count(unsigned int count
)
610 spin_lock_irq(&events_lock
);
611 if (count
== (unsigned int)atomic_read(&event_count
)
612 && !atomic_read(&events_in_progress
)) {
614 events_check_enabled
= true;
617 spin_unlock_irq(&events_lock
);
619 pm_wakeup_update_hit_counts();
623 static struct dentry
*wakeup_sources_stats_dentry
;
626 * print_wakeup_source_stats - Print wakeup source statistics information.
627 * @m: seq_file to print the statistics into.
628 * @ws: Wakeup source object to print the statistics for.
630 static int print_wakeup_source_stats(struct seq_file
*m
,
631 struct wakeup_source
*ws
)
636 unsigned long active_count
;
640 spin_lock_irqsave(&ws
->lock
, flags
);
642 total_time
= ws
->total_time
;
643 max_time
= ws
->max_time
;
644 active_count
= ws
->active_count
;
646 active_time
= ktime_sub(ktime_get(), ws
->last_time
);
647 total_time
= ktime_add(total_time
, active_time
);
648 if (active_time
.tv64
> max_time
.tv64
)
649 max_time
= active_time
;
651 active_time
= ktime_set(0, 0);
654 ret
= seq_printf(m
, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
655 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
656 ws
->name
, active_count
, ws
->event_count
, ws
->hit_count
,
657 ktime_to_ms(active_time
), ktime_to_ms(total_time
),
658 ktime_to_ms(max_time
), ktime_to_ms(ws
->last_time
));
660 spin_unlock_irqrestore(&ws
->lock
, flags
);
666 * wakeup_sources_stats_show - Print wakeup sources statistics information.
667 * @m: seq_file to print the statistics into.
669 static int wakeup_sources_stats_show(struct seq_file
*m
, void *unused
)
671 struct wakeup_source
*ws
;
673 seq_puts(m
, "name\t\tactive_count\tevent_count\thit_count\t"
674 "active_since\ttotal_time\tmax_time\tlast_change\n");
677 list_for_each_entry_rcu(ws
, &wakeup_sources
, entry
)
678 print_wakeup_source_stats(m
, ws
);
684 static int wakeup_sources_stats_open(struct inode
*inode
, struct file
*file
)
686 return single_open(file
, wakeup_sources_stats_show
, NULL
);
689 static const struct file_operations wakeup_sources_stats_fops
= {
690 .owner
= THIS_MODULE
,
691 .open
= wakeup_sources_stats_open
,
694 .release
= single_release
,
697 static int __init
wakeup_sources_debugfs_init(void)
699 wakeup_sources_stats_dentry
= debugfs_create_file("wakeup_sources",
700 S_IRUGO
, NULL
, NULL
, &wakeup_sources_stats_fops
);
704 postcore_initcall(wakeup_sources_debugfs_init
);