blob: 7c5ab70b9ef381a8866712d99a733819109b0842 [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.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +010077 *
78 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
79 * be run in parallel with this function for the same wakeup source object.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020080 */
81void wakeup_source_destroy(struct wakeup_source *ws)
82{
83 if (!ws)
84 return;
85
Rafael J. Wysockid94aff82012-02-17 23:39:20 +010086 del_timer_sync(&ws->timer);
87 __pm_relax(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020088 kfree(ws->name);
89 kfree(ws);
90}
91EXPORT_SYMBOL_GPL(wakeup_source_destroy);
92
93/**
94 * wakeup_source_add - Add given object to the list of wakeup sources.
95 * @ws: Wakeup source object to add to the list.
96 */
97void wakeup_source_add(struct wakeup_source *ws)
98{
99 if (WARN_ON(!ws))
100 return;
101
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100102 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200103 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
104 ws->active = false;
105
106 spin_lock_irq(&events_lock);
107 list_add_rcu(&ws->entry, &wakeup_sources);
108 spin_unlock_irq(&events_lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200109}
110EXPORT_SYMBOL_GPL(wakeup_source_add);
111
112/**
113 * wakeup_source_remove - Remove given object from the wakeup sources list.
114 * @ws: Wakeup source object to remove from the list.
115 */
116void wakeup_source_remove(struct wakeup_source *ws)
117{
118 if (WARN_ON(!ws))
119 return;
120
121 spin_lock_irq(&events_lock);
122 list_del_rcu(&ws->entry);
123 spin_unlock_irq(&events_lock);
124 synchronize_rcu();
125}
126EXPORT_SYMBOL_GPL(wakeup_source_remove);
127
128/**
129 * wakeup_source_register - Create wakeup source and add it to the list.
130 * @name: Name of the wakeup source to register.
131 */
132struct wakeup_source *wakeup_source_register(const char *name)
133{
134 struct wakeup_source *ws;
135
136 ws = wakeup_source_create(name);
137 if (ws)
138 wakeup_source_add(ws);
139
140 return ws;
141}
142EXPORT_SYMBOL_GPL(wakeup_source_register);
143
144/**
145 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
146 * @ws: Wakeup source object to unregister.
147 */
148void wakeup_source_unregister(struct wakeup_source *ws)
149{
150 wakeup_source_remove(ws);
151 wakeup_source_destroy(ws);
152}
153EXPORT_SYMBOL_GPL(wakeup_source_unregister);
154
155/**
156 * device_wakeup_attach - Attach a wakeup source object to a device object.
157 * @dev: Device to handle.
158 * @ws: Wakeup source object to attach to @dev.
159 *
160 * This causes @dev to be treated as a wakeup device.
161 */
162static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
163{
164 spin_lock_irq(&dev->power.lock);
165 if (dev->power.wakeup) {
166 spin_unlock_irq(&dev->power.lock);
167 return -EEXIST;
168 }
169 dev->power.wakeup = ws;
170 spin_unlock_irq(&dev->power.lock);
171 return 0;
172}
173
174/**
175 * device_wakeup_enable - Enable given device to be a wakeup source.
176 * @dev: Device to handle.
177 *
178 * Create a wakeup source object, register it and attach it to @dev.
179 */
180int device_wakeup_enable(struct device *dev)
181{
182 struct wakeup_source *ws;
183 int ret;
184
185 if (!dev || !dev->power.can_wakeup)
186 return -EINVAL;
187
188 ws = wakeup_source_register(dev_name(dev));
189 if (!ws)
190 return -ENOMEM;
191
192 ret = device_wakeup_attach(dev, ws);
193 if (ret)
194 wakeup_source_unregister(ws);
195
196 return ret;
197}
198EXPORT_SYMBOL_GPL(device_wakeup_enable);
199
200/**
201 * device_wakeup_detach - Detach a device's wakeup source object from it.
202 * @dev: Device to detach the wakeup source object from.
203 *
204 * After it returns, @dev will not be treated as a wakeup device any more.
205 */
206static struct wakeup_source *device_wakeup_detach(struct device *dev)
207{
208 struct wakeup_source *ws;
209
210 spin_lock_irq(&dev->power.lock);
211 ws = dev->power.wakeup;
212 dev->power.wakeup = NULL;
213 spin_unlock_irq(&dev->power.lock);
214 return ws;
215}
216
217/**
218 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
219 * @dev: Device to handle.
220 *
221 * Detach the @dev's wakeup source object from it, unregister this wakeup source
222 * object and destroy it.
223 */
224int device_wakeup_disable(struct device *dev)
225{
226 struct wakeup_source *ws;
227
228 if (!dev || !dev->power.can_wakeup)
229 return -EINVAL;
230
231 ws = device_wakeup_detach(dev);
232 if (ws)
233 wakeup_source_unregister(ws);
234
235 return 0;
236}
237EXPORT_SYMBOL_GPL(device_wakeup_disable);
238
239/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100240 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
241 * @dev: Device to handle.
242 * @capable: Whether or not @dev is capable of waking up the system from sleep.
243 *
244 * If @capable is set, set the @dev's power.can_wakeup flag and add its
245 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
246 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
247 *
248 * This function may sleep and it can't be called from any context where
249 * sleeping is not allowed.
250 */
251void device_set_wakeup_capable(struct device *dev, bool capable)
252{
253 if (!!dev->power.can_wakeup == !!capable)
254 return;
255
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200256 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100257 if (capable) {
258 if (wakeup_sysfs_add(dev))
259 return;
260 } else {
261 wakeup_sysfs_remove(dev);
262 }
263 }
264 dev->power.can_wakeup = capable;
265}
266EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
267
268/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200269 * device_init_wakeup - Device wakeup initialization.
270 * @dev: Device to handle.
271 * @enable: Whether or not to enable @dev as a wakeup device.
272 *
273 * By default, most devices should leave wakeup disabled. The exceptions are
274 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200275 * possibly network interfaces, etc. Also, devices that don't generate their
276 * own wakeup requests but merely forward requests from one bus to another
277 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200278 */
279int device_init_wakeup(struct device *dev, bool enable)
280{
281 int ret = 0;
282
283 if (enable) {
284 device_set_wakeup_capable(dev, true);
285 ret = device_wakeup_enable(dev);
286 } else {
287 device_set_wakeup_capable(dev, false);
288 }
289
290 return ret;
291}
292EXPORT_SYMBOL_GPL(device_init_wakeup);
293
294/**
295 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
296 * @dev: Device to handle.
297 */
298int device_set_wakeup_enable(struct device *dev, bool enable)
299{
300 if (!dev || !dev->power.can_wakeup)
301 return -EINVAL;
302
303 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
304}
305EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200306
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200307/*
308 * The functions below use the observation that each wakeup event starts a
309 * period in which the system should not be suspended. The moment this period
310 * will end depends on how the wakeup event is going to be processed after being
311 * detected and all of the possible cases can be divided into two distinct
312 * groups.
313 *
314 * First, a wakeup event may be detected by the same functional unit that will
315 * carry out the entire processing of it and possibly will pass it to user space
316 * for further processing. In that case the functional unit that has detected
317 * the event may later "close" the "no suspend" period associated with it
318 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
319 * pm_relax(), balanced with each other, is supposed to be used in such
320 * situations.
321 *
322 * Second, a wakeup event may be detected by one functional unit and processed
323 * by another one. In that case the unit that has detected it cannot really
324 * "close" the "no suspend" period associated with it, unless it knows in
325 * advance what's going to happen to the event during processing. This
326 * knowledge, however, may not be available to it, so it can simply specify time
327 * to wait before the system can be suspended and pass it as the second
328 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200329 *
330 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
331 * "no suspend" period will be ended either by the pm_relax(), or by the timer
332 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200333 */
334
335/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200336 * wakup_source_activate - Mark given wakeup source as active.
337 * @ws: Wakeup source to handle.
338 *
339 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
340 * core of the event by incrementing the counter of of wakeup events being
341 * processed.
342 */
343static void wakeup_source_activate(struct wakeup_source *ws)
344{
345 ws->active = true;
346 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200347 ws->last_time = ktime_get();
348
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100349 /* Increment the counter of events in progress. */
350 atomic_inc(&combined_event_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200351}
352
353/**
354 * __pm_stay_awake - Notify the PM core of a wakeup event.
355 * @ws: Wakeup source object associated with the source of the event.
356 *
357 * It is safe to call this function from interrupt context.
358 */
359void __pm_stay_awake(struct wakeup_source *ws)
360{
361 unsigned long flags;
362
363 if (!ws)
364 return;
365
366 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100367
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200368 ws->event_count++;
369 if (!ws->active)
370 wakeup_source_activate(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100371
372 del_timer(&ws->timer);
373 ws->timer_expires = 0;
374
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200375 spin_unlock_irqrestore(&ws->lock, flags);
376}
377EXPORT_SYMBOL_GPL(__pm_stay_awake);
378
379/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200380 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
381 * @dev: Device the wakeup event is related to.
382 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200383 * Notify the PM core of a wakeup event (signaled by @dev) by calling
384 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200385 *
386 * Call this function after detecting of a wakeup event if pm_relax() is going
387 * to be called directly after processing the event (and possibly passing it to
388 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200389 */
390void pm_stay_awake(struct device *dev)
391{
392 unsigned long flags;
393
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200394 if (!dev)
395 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200396
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200397 spin_lock_irqsave(&dev->power.lock, flags);
398 __pm_stay_awake(dev->power.wakeup);
399 spin_unlock_irqrestore(&dev->power.lock, flags);
400}
401EXPORT_SYMBOL_GPL(pm_stay_awake);
402
403/**
404 * wakup_source_deactivate - Mark given wakeup source as inactive.
405 * @ws: Wakeup source to handle.
406 *
407 * Update the @ws' statistics and notify the PM core that the wakeup source has
408 * become inactive by decrementing the counter of wakeup events being processed
409 * and incrementing the counter of registered wakeup events.
410 */
411static void wakeup_source_deactivate(struct wakeup_source *ws)
412{
413 ktime_t duration;
414 ktime_t now;
415
416 ws->relax_count++;
417 /*
418 * __pm_relax() may be called directly or from a timer function.
419 * If it is called directly right after the timer function has been
420 * started, but before the timer function calls __pm_relax(), it is
421 * possible that __pm_stay_awake() will be called in the meantime and
422 * will set ws->active. Then, ws->active may be cleared immediately
423 * by the __pm_relax() called from the timer function, but in such a
424 * case ws->relax_count will be different from ws->active_count.
425 */
426 if (ws->relax_count != ws->active_count) {
427 ws->relax_count--;
428 return;
429 }
430
431 ws->active = false;
432
433 now = ktime_get();
434 duration = ktime_sub(now, ws->last_time);
435 ws->total_time = ktime_add(ws->total_time, duration);
436 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
437 ws->max_time = duration;
438
439 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100440 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200441
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. Wysockida863cd2012-02-17 23:39:33 +0100495 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
496 * in @data if it is currently active and its timer has not been canceled and
497 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200498 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200499static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200500{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100501 struct wakeup_source *ws = (struct wakeup_source *)data;
502 unsigned long flags;
503
504 spin_lock_irqsave(&ws->lock, flags);
505
506 if (ws->active && ws->timer_expires
507 && time_after_eq(jiffies, ws->timer_expires))
508 wakeup_source_deactivate(ws);
509
510 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200511}
512
513/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200514 * __pm_wakeup_event - Notify the PM core of a wakeup event.
515 * @ws: Wakeup source object associated with the event source.
516 * @msec: Anticipated event processing time (in milliseconds).
517 *
518 * Notify the PM core of a wakeup event whose source is @ws that will take
519 * approximately @msec milliseconds to be processed by the kernel. If @ws is
520 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
521 * execute pm_wakeup_timer_fn() in future.
522 *
523 * It is safe to call this function from interrupt context.
524 */
525void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
526{
527 unsigned long flags;
528 unsigned long expires;
529
530 if (!ws)
531 return;
532
533 spin_lock_irqsave(&ws->lock, flags);
534
535 ws->event_count++;
536 if (!ws->active)
537 wakeup_source_activate(ws);
538
539 if (!msec) {
540 wakeup_source_deactivate(ws);
541 goto unlock;
542 }
543
544 expires = jiffies + msecs_to_jiffies(msec);
545 if (!expires)
546 expires = 1;
547
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100548 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200549 mod_timer(&ws->timer, expires);
550 ws->timer_expires = expires;
551 }
552
553 unlock:
554 spin_unlock_irqrestore(&ws->lock, flags);
555}
556EXPORT_SYMBOL_GPL(__pm_wakeup_event);
557
558
559/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200560 * pm_wakeup_event - Notify the PM core of a wakeup event.
561 * @dev: Device the wakeup event is related to.
562 * @msec: Anticipated event processing time (in milliseconds).
563 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200564 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200565 */
566void pm_wakeup_event(struct device *dev, unsigned int msec)
567{
568 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200569
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200570 if (!dev)
571 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200572
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200573 spin_lock_irqsave(&dev->power.lock, flags);
574 __pm_wakeup_event(dev->power.wakeup, msec);
575 spin_unlock_irqrestore(&dev->power.lock, flags);
576}
577EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200578
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200579/**
580 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
581 */
582static void pm_wakeup_update_hit_counts(void)
583{
584 unsigned long flags;
585 struct wakeup_source *ws;
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200586
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200587 rcu_read_lock();
588 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
589 spin_lock_irqsave(&ws->lock, flags);
590 if (ws->active)
591 ws->hit_count++;
592 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200593 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200594 rcu_read_unlock();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200595}
596
597/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100598 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200599 *
600 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100601 * value from the past and return true if new wakeup events have been registered
602 * since the old value was stored. Also return true if the current number of
603 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200604 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100605bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200606{
607 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100608 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200609
610 spin_lock_irqsave(&events_lock, flags);
611 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100612 unsigned int cnt, inpr;
613
614 split_counters(&cnt, &inpr);
615 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100616 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200617 }
618 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100619 if (ret)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200620 pm_wakeup_update_hit_counts();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200621 return ret;
622}
623
624/**
625 * pm_get_wakeup_count - Read the number of registered wakeup events.
626 * @count: Address to store the value at.
627 *
628 * Store the number of registered wakeup events at the address in @count. Block
629 * if the current number of wakeup events being processed is nonzero.
630 *
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100631 * Return 'false' if the wait for the number of wakeup events being processed to
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200632 * drop down to zero has been interrupted by a signal (and the current number
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100633 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200634 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200635bool pm_get_wakeup_count(unsigned int *count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200636{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100637 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200638
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100639 for (;;) {
640 split_counters(&cnt, &inpr);
641 if (inpr == 0 || signal_pending(current))
642 break;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200643 pm_wakeup_update_hit_counts();
644 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200645 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200646
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100647 split_counters(&cnt, &inpr);
648 *count = cnt;
649 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200650}
651
652/**
653 * pm_save_wakeup_count - Save the current number of registered wakeup events.
654 * @count: Value to compare with the current number of registered wakeup events.
655 *
656 * If @count is equal to the current number of registered wakeup events and the
657 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100658 * old number of registered wakeup events for pm_check_wakeup_events(), enable
659 * wakeup events detection and return 'true'. Otherwise disable wakeup events
660 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200661 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200662bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200663{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100664 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200665
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100666 events_check_enabled = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200667 spin_lock_irq(&events_lock);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100668 split_counters(&cnt, &inpr);
669 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200670 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200671 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200672 }
673 spin_unlock_irq(&events_lock);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100674 if (!events_check_enabled)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200675 pm_wakeup_update_hit_counts();
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100676 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200677}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200678
679static struct dentry *wakeup_sources_stats_dentry;
680
681/**
682 * print_wakeup_source_stats - Print wakeup source statistics information.
683 * @m: seq_file to print the statistics into.
684 * @ws: Wakeup source object to print the statistics for.
685 */
686static int print_wakeup_source_stats(struct seq_file *m,
687 struct wakeup_source *ws)
688{
689 unsigned long flags;
690 ktime_t total_time;
691 ktime_t max_time;
692 unsigned long active_count;
693 ktime_t active_time;
694 int ret;
695
696 spin_lock_irqsave(&ws->lock, flags);
697
698 total_time = ws->total_time;
699 max_time = ws->max_time;
700 active_count = ws->active_count;
701 if (ws->active) {
702 active_time = ktime_sub(ktime_get(), ws->last_time);
703 total_time = ktime_add(total_time, active_time);
704 if (active_time.tv64 > max_time.tv64)
705 max_time = active_time;
706 } else {
707 active_time = ktime_set(0, 0);
708 }
709
710 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
711 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
712 ws->name, active_count, ws->event_count, ws->hit_count,
713 ktime_to_ms(active_time), ktime_to_ms(total_time),
714 ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
715
716 spin_unlock_irqrestore(&ws->lock, flags);
717
718 return ret;
719}
720
721/**
722 * wakeup_sources_stats_show - Print wakeup sources statistics information.
723 * @m: seq_file to print the statistics into.
724 */
725static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
726{
727 struct wakeup_source *ws;
728
729 seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
730 "active_since\ttotal_time\tmax_time\tlast_change\n");
731
732 rcu_read_lock();
733 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
734 print_wakeup_source_stats(m, ws);
735 rcu_read_unlock();
736
737 return 0;
738}
739
740static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
741{
742 return single_open(file, wakeup_sources_stats_show, NULL);
743}
744
745static const struct file_operations wakeup_sources_stats_fops = {
746 .owner = THIS_MODULE,
747 .open = wakeup_sources_stats_open,
748 .read = seq_read,
749 .llseek = seq_lseek,
750 .release = single_release,
751};
752
753static int __init wakeup_sources_debugfs_init(void)
754{
755 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
756 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
757 return 0;
758}
759
760postcore_initcall(wakeup_sources_debugfs_init);