blob: 6e591a8a49da1aa873e1ae61197abde0008c9575 [file] [log] [blame]
Rafael J. Wysockic125e962010-07-05 22:43:53 +02001/*
2 * drivers/base/power/wakeup.c - System wakeup events framework
3 *
4 * Copyright (c) 2010 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5 *
6 * This file is released under the GPLv2.
7 */
8
9#include <linux/device.h>
10#include <linux/slab.h>
11#include <linux/sched.h>
12#include <linux/capability.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040013#include <linux/export.h>
Rafael J. Wysockic125e962010-07-05 22:43:53 +020014#include <linux/suspend.h>
Rafael J. Wysocki9c034392010-10-19 23:42:49 +020015#include <linux/seq_file.h>
16#include <linux/debugfs.h>
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020017
18#include "power.h"
19
20#define TIMEOUT 100
Rafael J. Wysockic125e962010-07-05 22:43:53 +020021
22/*
23 * If set, the suspend/hibernate code will abort transitions to a sleep state
24 * if wakeup events are registered during or immediately before the transition.
25 */
26bool events_check_enabled;
27
Rafael J. Wysocki023d3772011-01-31 11:06:39 +010028/*
29 * Combined counters of registered wakeup events and wakeup events in progress.
30 * They need to be modified together atomically, so it's better to use one
31 * atomic variable to hold them both.
32 */
33static atomic_t combined_event_count = ATOMIC_INIT(0);
34
35#define IN_PROGRESS_BITS (sizeof(int) * 4)
36#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
37
38static void split_counters(unsigned int *cnt, unsigned int *inpr)
39{
40 unsigned int comb = atomic_read(&combined_event_count);
41
42 *cnt = (comb >> IN_PROGRESS_BITS);
43 *inpr = comb & MAX_IN_PROGRESS;
44}
45
46/* A preserved old value of the events counter. */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020047static unsigned int saved_count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020048
49static DEFINE_SPINLOCK(events_lock);
50
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +020051static void pm_wakeup_timer_fn(unsigned long data);
52
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020053static LIST_HEAD(wakeup_sources);
54
55/**
56 * wakeup_source_create - Create a struct wakeup_source object.
57 * @name: Name of the new wakeup source.
58 */
59struct wakeup_source *wakeup_source_create(const char *name)
60{
61 struct wakeup_source *ws;
62
63 ws = kzalloc(sizeof(*ws), GFP_KERNEL);
64 if (!ws)
65 return NULL;
66
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020067 if (name)
68 ws->name = kstrdup(name, GFP_KERNEL);
69
70 return ws;
71}
72EXPORT_SYMBOL_GPL(wakeup_source_create);
73
74/**
75 * wakeup_source_destroy - Destroy a struct wakeup_source object.
76 * @ws: Wakeup source to destroy.
77 */
78void wakeup_source_destroy(struct wakeup_source *ws)
79{
80 if (!ws)
81 return;
82
83 spin_lock_irq(&ws->lock);
84 while (ws->active) {
85 spin_unlock_irq(&ws->lock);
86
87 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
88
89 spin_lock_irq(&ws->lock);
90 }
91 spin_unlock_irq(&ws->lock);
92
93 kfree(ws->name);
94 kfree(ws);
95}
96EXPORT_SYMBOL_GPL(wakeup_source_destroy);
97
98/**
99 * wakeup_source_add - Add given object to the list of wakeup sources.
100 * @ws: Wakeup source object to add to the list.
101 */
102void wakeup_source_add(struct wakeup_source *ws)
103{
104 if (WARN_ON(!ws))
105 return;
106
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100107 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200108 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
109 ws->active = false;
110
111 spin_lock_irq(&events_lock);
112 list_add_rcu(&ws->entry, &wakeup_sources);
113 spin_unlock_irq(&events_lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200114}
115EXPORT_SYMBOL_GPL(wakeup_source_add);
116
117/**
118 * wakeup_source_remove - Remove given object from the wakeup sources list.
119 * @ws: Wakeup source object to remove from the list.
120 */
121void wakeup_source_remove(struct wakeup_source *ws)
122{
123 if (WARN_ON(!ws))
124 return;
125
126 spin_lock_irq(&events_lock);
127 list_del_rcu(&ws->entry);
128 spin_unlock_irq(&events_lock);
129 synchronize_rcu();
130}
131EXPORT_SYMBOL_GPL(wakeup_source_remove);
132
133/**
134 * wakeup_source_register - Create wakeup source and add it to the list.
135 * @name: Name of the wakeup source to register.
136 */
137struct wakeup_source *wakeup_source_register(const char *name)
138{
139 struct wakeup_source *ws;
140
141 ws = wakeup_source_create(name);
142 if (ws)
143 wakeup_source_add(ws);
144
145 return ws;
146}
147EXPORT_SYMBOL_GPL(wakeup_source_register);
148
149/**
150 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
151 * @ws: Wakeup source object to unregister.
152 */
153void wakeup_source_unregister(struct wakeup_source *ws)
154{
155 wakeup_source_remove(ws);
156 wakeup_source_destroy(ws);
157}
158EXPORT_SYMBOL_GPL(wakeup_source_unregister);
159
160/**
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.
164 *
165 * This causes @dev to be treated as a wakeup device.
166 */
167static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
168{
169 spin_lock_irq(&dev->power.lock);
170 if (dev->power.wakeup) {
171 spin_unlock_irq(&dev->power.lock);
172 return -EEXIST;
173 }
174 dev->power.wakeup = ws;
175 spin_unlock_irq(&dev->power.lock);
176 return 0;
177}
178
179/**
180 * device_wakeup_enable - Enable given device to be a wakeup source.
181 * @dev: Device to handle.
182 *
183 * Create a wakeup source object, register it and attach it to @dev.
184 */
185int device_wakeup_enable(struct device *dev)
186{
187 struct wakeup_source *ws;
188 int ret;
189
190 if (!dev || !dev->power.can_wakeup)
191 return -EINVAL;
192
193 ws = wakeup_source_register(dev_name(dev));
194 if (!ws)
195 return -ENOMEM;
196
197 ret = device_wakeup_attach(dev, ws);
198 if (ret)
199 wakeup_source_unregister(ws);
200
201 return ret;
202}
203EXPORT_SYMBOL_GPL(device_wakeup_enable);
204
205/**
206 * device_wakeup_detach - Detach a device's wakeup source object from it.
207 * @dev: Device to detach the wakeup source object from.
208 *
209 * After it returns, @dev will not be treated as a wakeup device any more.
210 */
211static struct wakeup_source *device_wakeup_detach(struct device *dev)
212{
213 struct wakeup_source *ws;
214
215 spin_lock_irq(&dev->power.lock);
216 ws = dev->power.wakeup;
217 dev->power.wakeup = NULL;
218 spin_unlock_irq(&dev->power.lock);
219 return ws;
220}
221
222/**
223 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
224 * @dev: Device to handle.
225 *
226 * Detach the @dev's wakeup source object from it, unregister this wakeup source
227 * object and destroy it.
228 */
229int device_wakeup_disable(struct device *dev)
230{
231 struct wakeup_source *ws;
232
233 if (!dev || !dev->power.can_wakeup)
234 return -EINVAL;
235
236 ws = device_wakeup_detach(dev);
237 if (ws)
238 wakeup_source_unregister(ws);
239
240 return 0;
241}
242EXPORT_SYMBOL_GPL(device_wakeup_disable);
243
244/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100245 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
246 * @dev: Device to handle.
247 * @capable: Whether or not @dev is capable of waking up the system from sleep.
248 *
249 * If @capable is set, set the @dev's power.can_wakeup flag and add its
250 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
251 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
252 *
253 * This function may sleep and it can't be called from any context where
254 * sleeping is not allowed.
255 */
256void device_set_wakeup_capable(struct device *dev, bool capable)
257{
258 if (!!dev->power.can_wakeup == !!capable)
259 return;
260
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200261 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100262 if (capable) {
263 if (wakeup_sysfs_add(dev))
264 return;
265 } else {
266 wakeup_sysfs_remove(dev);
267 }
268 }
269 dev->power.can_wakeup = capable;
270}
271EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
272
273/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200274 * device_init_wakeup - Device wakeup initialization.
275 * @dev: Device to handle.
276 * @enable: Whether or not to enable @dev as a wakeup device.
277 *
278 * By default, most devices should leave wakeup disabled. The exceptions are
279 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200280 * possibly network interfaces, etc. Also, devices that don't generate their
281 * own wakeup requests but merely forward requests from one bus to another
282 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200283 */
284int device_init_wakeup(struct device *dev, bool enable)
285{
286 int ret = 0;
287
288 if (enable) {
289 device_set_wakeup_capable(dev, true);
290 ret = device_wakeup_enable(dev);
291 } else {
292 device_set_wakeup_capable(dev, false);
293 }
294
295 return ret;
296}
297EXPORT_SYMBOL_GPL(device_init_wakeup);
298
299/**
300 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
301 * @dev: Device to handle.
302 */
303int device_set_wakeup_enable(struct device *dev, bool enable)
304{
305 if (!dev || !dev->power.can_wakeup)
306 return -EINVAL;
307
308 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
309}
310EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200311
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200312/*
313 * The functions below use the observation that each wakeup event starts a
314 * period in which the system should not be suspended. The moment this period
315 * will end depends on how the wakeup event is going to be processed after being
316 * detected and all of the possible cases can be divided into two distinct
317 * groups.
318 *
319 * First, a wakeup event may be detected by the same functional unit that will
320 * carry out the entire processing of it and possibly will pass it to user space
321 * for further processing. In that case the functional unit that has detected
322 * the event may later "close" the "no suspend" period associated with it
323 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
324 * pm_relax(), balanced with each other, is supposed to be used in such
325 * situations.
326 *
327 * Second, a wakeup event may be detected by one functional unit and processed
328 * by another one. In that case the unit that has detected it cannot really
329 * "close" the "no suspend" period associated with it, unless it knows in
330 * advance what's going to happen to the event during processing. This
331 * knowledge, however, may not be available to it, so it can simply specify time
332 * to wait before the system can be suspended and pass it as the second
333 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200334 *
335 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
336 * "no suspend" period will be ended either by the pm_relax(), or by the timer
337 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200338 */
339
340/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200341 * wakup_source_activate - Mark given wakeup source as active.
342 * @ws: Wakeup source to handle.
343 *
344 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
345 * core of the event by incrementing the counter of of wakeup events being
346 * processed.
347 */
348static void wakeup_source_activate(struct wakeup_source *ws)
349{
350 ws->active = true;
351 ws->active_count++;
352 ws->timer_expires = jiffies;
353 ws->last_time = ktime_get();
354
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100355 /* Increment the counter of events in progress. */
356 atomic_inc(&combined_event_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200357}
358
359/**
360 * __pm_stay_awake - Notify the PM core of a wakeup event.
361 * @ws: Wakeup source object associated with the source of the event.
362 *
363 * It is safe to call this function from interrupt context.
364 */
365void __pm_stay_awake(struct wakeup_source *ws)
366{
367 unsigned long flags;
368
369 if (!ws)
370 return;
371
372 spin_lock_irqsave(&ws->lock, flags);
373 ws->event_count++;
374 if (!ws->active)
375 wakeup_source_activate(ws);
376 spin_unlock_irqrestore(&ws->lock, flags);
377}
378EXPORT_SYMBOL_GPL(__pm_stay_awake);
379
380/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200381 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
382 * @dev: Device the wakeup event is related to.
383 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200384 * Notify the PM core of a wakeup event (signaled by @dev) by calling
385 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200386 *
387 * Call this function after detecting of a wakeup event if pm_relax() is going
388 * to be called directly after processing the event (and possibly passing it to
389 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200390 */
391void pm_stay_awake(struct device *dev)
392{
393 unsigned long flags;
394
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200395 if (!dev)
396 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200397
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200398 spin_lock_irqsave(&dev->power.lock, flags);
399 __pm_stay_awake(dev->power.wakeup);
400 spin_unlock_irqrestore(&dev->power.lock, flags);
401}
402EXPORT_SYMBOL_GPL(pm_stay_awake);
403
404/**
405 * wakup_source_deactivate - Mark given wakeup source as inactive.
406 * @ws: Wakeup source to handle.
407 *
408 * Update the @ws' statistics and notify the PM core that the wakeup source has
409 * become inactive by decrementing the counter of wakeup events being processed
410 * and incrementing the counter of registered wakeup events.
411 */
412static void wakeup_source_deactivate(struct wakeup_source *ws)
413{
414 ktime_t duration;
415 ktime_t now;
416
417 ws->relax_count++;
418 /*
419 * __pm_relax() may be called directly or from a timer function.
420 * If it is called directly right after the timer function has been
421 * started, but before the timer function calls __pm_relax(), it is
422 * possible that __pm_stay_awake() will be called in the meantime and
423 * will set ws->active. Then, ws->active may be cleared immediately
424 * by the __pm_relax() called from the timer function, but in such a
425 * case ws->relax_count will be different from ws->active_count.
426 */
427 if (ws->relax_count != ws->active_count) {
428 ws->relax_count--;
429 return;
430 }
431
432 ws->active = false;
433
434 now = ktime_get();
435 duration = ktime_sub(now, ws->last_time);
436 ws->total_time = ktime_add(ws->total_time, duration);
437 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
438 ws->max_time = duration;
439
440 del_timer(&ws->timer);
441
442 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100443 * Increment the counter of registered wakeup events and decrement the
444 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200445 */
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100446 atomic_add(MAX_IN_PROGRESS, &combined_event_count);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200447}
448
449/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200450 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
451 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200452 *
453 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200454 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200455 *
456 * It is safe to call it from interrupt context.
457 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200458void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200459{
460 unsigned long flags;
461
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200462 if (!ws)
463 return;
464
465 spin_lock_irqsave(&ws->lock, flags);
466 if (ws->active)
467 wakeup_source_deactivate(ws);
468 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200469}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200470EXPORT_SYMBOL_GPL(__pm_relax);
471
472/**
473 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
474 * @dev: Device that signaled the event.
475 *
476 * Execute __pm_relax() for the @dev's wakeup source object.
477 */
478void pm_relax(struct device *dev)
479{
480 unsigned long flags;
481
482 if (!dev)
483 return;
484
485 spin_lock_irqsave(&dev->power.lock, flags);
486 __pm_relax(dev->power.wakeup);
487 spin_unlock_irqrestore(&dev->power.lock, flags);
488}
489EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200490
491/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200492 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200493 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200494 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200495 * Call __pm_relax() for the wakeup source whose address is stored in @data.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200496 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200497static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200498{
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200499 __pm_relax((struct wakeup_source *)data);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200500}
501
502/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200503 * __pm_wakeup_event - Notify the PM core of a wakeup event.
504 * @ws: Wakeup source object associated with the event source.
505 * @msec: Anticipated event processing time (in milliseconds).
506 *
507 * Notify the PM core of a wakeup event whose source is @ws that will take
508 * approximately @msec milliseconds to be processed by the kernel. If @ws is
509 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
510 * execute pm_wakeup_timer_fn() in future.
511 *
512 * It is safe to call this function from interrupt context.
513 */
514void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
515{
516 unsigned long flags;
517 unsigned long expires;
518
519 if (!ws)
520 return;
521
522 spin_lock_irqsave(&ws->lock, flags);
523
524 ws->event_count++;
525 if (!ws->active)
526 wakeup_source_activate(ws);
527
528 if (!msec) {
529 wakeup_source_deactivate(ws);
530 goto unlock;
531 }
532
533 expires = jiffies + msecs_to_jiffies(msec);
534 if (!expires)
535 expires = 1;
536
537 if (time_after(expires, ws->timer_expires)) {
538 mod_timer(&ws->timer, expires);
539 ws->timer_expires = expires;
540 }
541
542 unlock:
543 spin_unlock_irqrestore(&ws->lock, flags);
544}
545EXPORT_SYMBOL_GPL(__pm_wakeup_event);
546
547
548/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200549 * pm_wakeup_event - Notify the PM core of a wakeup event.
550 * @dev: Device the wakeup event is related to.
551 * @msec: Anticipated event processing time (in milliseconds).
552 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200553 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200554 */
555void pm_wakeup_event(struct device *dev, unsigned int msec)
556{
557 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200558
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200559 if (!dev)
560 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200561
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200562 spin_lock_irqsave(&dev->power.lock, flags);
563 __pm_wakeup_event(dev->power.wakeup, msec);
564 spin_unlock_irqrestore(&dev->power.lock, flags);
565}
566EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200567
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200568/**
569 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
570 */
571static void pm_wakeup_update_hit_counts(void)
572{
573 unsigned long flags;
574 struct wakeup_source *ws;
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200575
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200576 rcu_read_lock();
577 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
578 spin_lock_irqsave(&ws->lock, flags);
579 if (ws->active)
580 ws->hit_count++;
581 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200582 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200583 rcu_read_unlock();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200584}
585
586/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100587 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200588 *
589 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100590 * value from the past and return true if new wakeup events have been registered
591 * since the old value was stored. Also return true if the current number of
592 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200593 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100594bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200595{
596 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100597 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200598
599 spin_lock_irqsave(&events_lock, flags);
600 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100601 unsigned int cnt, inpr;
602
603 split_counters(&cnt, &inpr);
604 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100605 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200606 }
607 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100608 if (ret)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200609 pm_wakeup_update_hit_counts();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200610 return ret;
611}
612
613/**
614 * pm_get_wakeup_count - Read the number of registered wakeup events.
615 * @count: Address to store the value at.
616 *
617 * Store the number of registered wakeup events at the address in @count. Block
618 * if the current number of wakeup events being processed is nonzero.
619 *
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100620 * Return 'false' if the wait for the number of wakeup events being processed to
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200621 * drop down to zero has been interrupted by a signal (and the current number
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100622 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200623 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200624bool pm_get_wakeup_count(unsigned int *count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200625{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100626 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200627
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100628 for (;;) {
629 split_counters(&cnt, &inpr);
630 if (inpr == 0 || signal_pending(current))
631 break;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200632 pm_wakeup_update_hit_counts();
633 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200634 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200635
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100636 split_counters(&cnt, &inpr);
637 *count = cnt;
638 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200639}
640
641/**
642 * pm_save_wakeup_count - Save the current number of registered wakeup events.
643 * @count: Value to compare with the current number of registered wakeup events.
644 *
645 * If @count is equal to the current number of registered wakeup events and the
646 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100647 * old number of registered wakeup events for pm_check_wakeup_events(), enable
648 * wakeup events detection and return 'true'. Otherwise disable wakeup events
649 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200650 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200651bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200652{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100653 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200654
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100655 events_check_enabled = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200656 spin_lock_irq(&events_lock);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100657 split_counters(&cnt, &inpr);
658 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200659 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200660 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200661 }
662 spin_unlock_irq(&events_lock);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100663 if (!events_check_enabled)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200664 pm_wakeup_update_hit_counts();
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100665 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200666}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200667
668static struct dentry *wakeup_sources_stats_dentry;
669
670/**
671 * print_wakeup_source_stats - Print wakeup source statistics information.
672 * @m: seq_file to print the statistics into.
673 * @ws: Wakeup source object to print the statistics for.
674 */
675static int print_wakeup_source_stats(struct seq_file *m,
676 struct wakeup_source *ws)
677{
678 unsigned long flags;
679 ktime_t total_time;
680 ktime_t max_time;
681 unsigned long active_count;
682 ktime_t active_time;
683 int ret;
684
685 spin_lock_irqsave(&ws->lock, flags);
686
687 total_time = ws->total_time;
688 max_time = ws->max_time;
689 active_count = ws->active_count;
690 if (ws->active) {
691 active_time = ktime_sub(ktime_get(), ws->last_time);
692 total_time = ktime_add(total_time, active_time);
693 if (active_time.tv64 > max_time.tv64)
694 max_time = active_time;
695 } else {
696 active_time = ktime_set(0, 0);
697 }
698
699 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
700 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
701 ws->name, active_count, ws->event_count, ws->hit_count,
702 ktime_to_ms(active_time), ktime_to_ms(total_time),
703 ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
704
705 spin_unlock_irqrestore(&ws->lock, flags);
706
707 return ret;
708}
709
710/**
711 * wakeup_sources_stats_show - Print wakeup sources statistics information.
712 * @m: seq_file to print the statistics into.
713 */
714static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
715{
716 struct wakeup_source *ws;
717
718 seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
719 "active_since\ttotal_time\tmax_time\tlast_change\n");
720
721 rcu_read_lock();
722 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
723 print_wakeup_source_stats(m, ws);
724 rcu_read_unlock();
725
726 return 0;
727}
728
729static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
730{
731 return single_open(file, wakeup_sources_stats_show, NULL);
732}
733
734static const struct file_operations wakeup_sources_stats_fops = {
735 .owner = THIS_MODULE,
736 .open = wakeup_sources_stats_open,
737 .read = seq_read,
738 .llseek = seq_lseek,
739 .release = single_release,
740};
741
742static int __init wakeup_sources_debugfs_init(void)
743{
744 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
745 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
746 return 0;
747}
748
749postcore_initcall(wakeup_sources_debugfs_init);