blob: b38bb9afb7196556ce0efe9088a1f0c3eb7e7fb3 [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++;
347 ws->timer_expires = jiffies;
348 ws->last_time = ktime_get();
349
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100350 /* Increment the counter of events in progress. */
351 atomic_inc(&combined_event_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200352}
353
354/**
355 * __pm_stay_awake - Notify the PM core of a wakeup event.
356 * @ws: Wakeup source object associated with the source of the event.
357 *
358 * It is safe to call this function from interrupt context.
359 */
360void __pm_stay_awake(struct wakeup_source *ws)
361{
362 unsigned long flags;
363
364 if (!ws)
365 return;
366
367 spin_lock_irqsave(&ws->lock, flags);
368 ws->event_count++;
369 if (!ws->active)
370 wakeup_source_activate(ws);
371 spin_unlock_irqrestore(&ws->lock, flags);
372}
373EXPORT_SYMBOL_GPL(__pm_stay_awake);
374
375/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200376 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
377 * @dev: Device the wakeup event is related to.
378 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200379 * Notify the PM core of a wakeup event (signaled by @dev) by calling
380 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200381 *
382 * Call this function after detecting of a wakeup event if pm_relax() is going
383 * to be called directly after processing the event (and possibly passing it to
384 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200385 */
386void pm_stay_awake(struct device *dev)
387{
388 unsigned long flags;
389
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200390 if (!dev)
391 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200392
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200393 spin_lock_irqsave(&dev->power.lock, flags);
394 __pm_stay_awake(dev->power.wakeup);
395 spin_unlock_irqrestore(&dev->power.lock, flags);
396}
397EXPORT_SYMBOL_GPL(pm_stay_awake);
398
399/**
400 * wakup_source_deactivate - Mark given wakeup source as inactive.
401 * @ws: Wakeup source to handle.
402 *
403 * Update the @ws' statistics and notify the PM core that the wakeup source has
404 * become inactive by decrementing the counter of wakeup events being processed
405 * and incrementing the counter of registered wakeup events.
406 */
407static void wakeup_source_deactivate(struct wakeup_source *ws)
408{
409 ktime_t duration;
410 ktime_t now;
411
412 ws->relax_count++;
413 /*
414 * __pm_relax() may be called directly or from a timer function.
415 * If it is called directly right after the timer function has been
416 * started, but before the timer function calls __pm_relax(), it is
417 * possible that __pm_stay_awake() will be called in the meantime and
418 * will set ws->active. Then, ws->active may be cleared immediately
419 * by the __pm_relax() called from the timer function, but in such a
420 * case ws->relax_count will be different from ws->active_count.
421 */
422 if (ws->relax_count != ws->active_count) {
423 ws->relax_count--;
424 return;
425 }
426
427 ws->active = false;
428
429 now = ktime_get();
430 duration = ktime_sub(now, ws->last_time);
431 ws->total_time = ktime_add(ws->total_time, duration);
432 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
433 ws->max_time = duration;
434
435 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100436 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200437
438 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100439 * Increment the counter of registered wakeup events and decrement the
440 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200441 */
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100442 atomic_add(MAX_IN_PROGRESS, &combined_event_count);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200443}
444
445/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200446 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
447 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200448 *
449 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200450 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200451 *
452 * It is safe to call it from interrupt context.
453 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200454void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200455{
456 unsigned long flags;
457
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200458 if (!ws)
459 return;
460
461 spin_lock_irqsave(&ws->lock, flags);
462 if (ws->active)
463 wakeup_source_deactivate(ws);
464 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200465}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200466EXPORT_SYMBOL_GPL(__pm_relax);
467
468/**
469 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
470 * @dev: Device that signaled the event.
471 *
472 * Execute __pm_relax() for the @dev's wakeup source object.
473 */
474void pm_relax(struct device *dev)
475{
476 unsigned long flags;
477
478 if (!dev)
479 return;
480
481 spin_lock_irqsave(&dev->power.lock, flags);
482 __pm_relax(dev->power.wakeup);
483 spin_unlock_irqrestore(&dev->power.lock, flags);
484}
485EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200486
487/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200488 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200489 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200490 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100491 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
492 * in @data if it is currently active and its timer has not been canceled and
493 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200494 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200495static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200496{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100497 struct wakeup_source *ws = (struct wakeup_source *)data;
498 unsigned long flags;
499
500 spin_lock_irqsave(&ws->lock, flags);
501
502 if (ws->active && ws->timer_expires
503 && time_after_eq(jiffies, ws->timer_expires))
504 wakeup_source_deactivate(ws);
505
506 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200507}
508
509/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200510 * __pm_wakeup_event - Notify the PM core of a wakeup event.
511 * @ws: Wakeup source object associated with the event source.
512 * @msec: Anticipated event processing time (in milliseconds).
513 *
514 * Notify the PM core of a wakeup event whose source is @ws that will take
515 * approximately @msec milliseconds to be processed by the kernel. If @ws is
516 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
517 * execute pm_wakeup_timer_fn() in future.
518 *
519 * It is safe to call this function from interrupt context.
520 */
521void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
522{
523 unsigned long flags;
524 unsigned long expires;
525
526 if (!ws)
527 return;
528
529 spin_lock_irqsave(&ws->lock, flags);
530
531 ws->event_count++;
532 if (!ws->active)
533 wakeup_source_activate(ws);
534
535 if (!msec) {
536 wakeup_source_deactivate(ws);
537 goto unlock;
538 }
539
540 expires = jiffies + msecs_to_jiffies(msec);
541 if (!expires)
542 expires = 1;
543
544 if (time_after(expires, ws->timer_expires)) {
545 mod_timer(&ws->timer, expires);
546 ws->timer_expires = expires;
547 }
548
549 unlock:
550 spin_unlock_irqrestore(&ws->lock, flags);
551}
552EXPORT_SYMBOL_GPL(__pm_wakeup_event);
553
554
555/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200556 * pm_wakeup_event - Notify the PM core of a wakeup event.
557 * @dev: Device the wakeup event is related to.
558 * @msec: Anticipated event processing time (in milliseconds).
559 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200560 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200561 */
562void pm_wakeup_event(struct device *dev, unsigned int msec)
563{
564 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200565
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200566 if (!dev)
567 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200568
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200569 spin_lock_irqsave(&dev->power.lock, flags);
570 __pm_wakeup_event(dev->power.wakeup, msec);
571 spin_unlock_irqrestore(&dev->power.lock, flags);
572}
573EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200574
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200575/**
576 * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
577 */
578static void pm_wakeup_update_hit_counts(void)
579{
580 unsigned long flags;
581 struct wakeup_source *ws;
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200582
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200583 rcu_read_lock();
584 list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
585 spin_lock_irqsave(&ws->lock, flags);
586 if (ws->active)
587 ws->hit_count++;
588 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200589 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200590 rcu_read_unlock();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200591}
592
593/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100594 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200595 *
596 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100597 * value from the past and return true if new wakeup events have been registered
598 * since the old value was stored. Also return true if the current number of
599 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200600 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100601bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200602{
603 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100604 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200605
606 spin_lock_irqsave(&events_lock, flags);
607 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100608 unsigned int cnt, inpr;
609
610 split_counters(&cnt, &inpr);
611 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100612 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200613 }
614 spin_unlock_irqrestore(&events_lock, flags);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100615 if (ret)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200616 pm_wakeup_update_hit_counts();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200617 return ret;
618}
619
620/**
621 * pm_get_wakeup_count - Read the number of registered wakeup events.
622 * @count: Address to store the value at.
623 *
624 * Store the number of registered wakeup events at the address in @count. Block
625 * if the current number of wakeup events being processed is nonzero.
626 *
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100627 * Return 'false' if the wait for the number of wakeup events being processed to
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200628 * drop down to zero has been interrupted by a signal (and the current number
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100629 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200630 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200631bool pm_get_wakeup_count(unsigned int *count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200632{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100633 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200634
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100635 for (;;) {
636 split_counters(&cnt, &inpr);
637 if (inpr == 0 || signal_pending(current))
638 break;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200639 pm_wakeup_update_hit_counts();
640 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200641 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200642
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100643 split_counters(&cnt, &inpr);
644 *count = cnt;
645 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200646}
647
648/**
649 * pm_save_wakeup_count - Save the current number of registered wakeup events.
650 * @count: Value to compare with the current number of registered wakeup events.
651 *
652 * If @count is equal to the current number of registered wakeup events and the
653 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100654 * old number of registered wakeup events for pm_check_wakeup_events(), enable
655 * wakeup events detection and return 'true'. Otherwise disable wakeup events
656 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200657 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200658bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200659{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100660 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200661
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100662 events_check_enabled = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200663 spin_lock_irq(&events_lock);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100664 split_counters(&cnt, &inpr);
665 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200666 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200667 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200668 }
669 spin_unlock_irq(&events_lock);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100670 if (!events_check_enabled)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200671 pm_wakeup_update_hit_counts();
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100672 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200673}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200674
675static struct dentry *wakeup_sources_stats_dentry;
676
677/**
678 * print_wakeup_source_stats - Print wakeup source statistics information.
679 * @m: seq_file to print the statistics into.
680 * @ws: Wakeup source object to print the statistics for.
681 */
682static int print_wakeup_source_stats(struct seq_file *m,
683 struct wakeup_source *ws)
684{
685 unsigned long flags;
686 ktime_t total_time;
687 ktime_t max_time;
688 unsigned long active_count;
689 ktime_t active_time;
690 int ret;
691
692 spin_lock_irqsave(&ws->lock, flags);
693
694 total_time = ws->total_time;
695 max_time = ws->max_time;
696 active_count = ws->active_count;
697 if (ws->active) {
698 active_time = ktime_sub(ktime_get(), ws->last_time);
699 total_time = ktime_add(total_time, active_time);
700 if (active_time.tv64 > max_time.tv64)
701 max_time = active_time;
702 } else {
703 active_time = ktime_set(0, 0);
704 }
705
706 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
707 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
708 ws->name, active_count, ws->event_count, ws->hit_count,
709 ktime_to_ms(active_time), ktime_to_ms(total_time),
710 ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
711
712 spin_unlock_irqrestore(&ws->lock, flags);
713
714 return ret;
715}
716
717/**
718 * wakeup_sources_stats_show - Print wakeup sources statistics information.
719 * @m: seq_file to print the statistics into.
720 */
721static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
722{
723 struct wakeup_source *ws;
724
725 seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
726 "active_since\ttotal_time\tmax_time\tlast_change\n");
727
728 rcu_read_lock();
729 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
730 print_wakeup_source_stats(m, ws);
731 rcu_read_unlock();
732
733 return 0;
734}
735
736static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
737{
738 return single_open(file, wakeup_sources_stats_show, NULL);
739}
740
741static const struct file_operations wakeup_sources_stats_fops = {
742 .owner = THIS_MODULE,
743 .open = wakeup_sources_stats_open,
744 .read = seq_read,
745 .llseek = seq_lseek,
746 .release = single_release,
747};
748
749static int __init wakeup_sources_debugfs_init(void)
750{
751 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
752 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
753 return 0;
754}
755
756postcore_initcall(wakeup_sources_debugfs_init);