blob: 7a6eada4534d5c2884b12a6eb24f605d5301e7af [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
Rafael J. Wysockic125e962010-07-05 22:43:53 +020020/*
21 * If set, the suspend/hibernate code will abort transitions to a sleep state
22 * if wakeup events are registered during or immediately before the transition.
23 */
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +020024bool events_check_enabled __read_mostly;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020025
Rafael J. Wysocki023d3772011-01-31 11:06:39 +010026/*
27 * Combined counters of registered wakeup events and wakeup events in progress.
28 * They need to be modified together atomically, so it's better to use one
29 * atomic variable to hold them both.
30 */
31static atomic_t combined_event_count = ATOMIC_INIT(0);
32
33#define IN_PROGRESS_BITS (sizeof(int) * 4)
34#define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1)
35
36static void split_counters(unsigned int *cnt, unsigned int *inpr)
37{
38 unsigned int comb = atomic_read(&combined_event_count);
39
40 *cnt = (comb >> IN_PROGRESS_BITS);
41 *inpr = comb & MAX_IN_PROGRESS;
42}
43
44/* A preserved old value of the events counter. */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020045static unsigned int saved_count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +020046
47static DEFINE_SPINLOCK(events_lock);
48
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +020049static void pm_wakeup_timer_fn(unsigned long data);
50
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020051static LIST_HEAD(wakeup_sources);
52
Rafael J. Wysocki60af1062012-04-29 22:52:34 +020053static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue);
54
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020055/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010056 * wakeup_source_prepare - Prepare a new wakeup source for initialization.
57 * @ws: Wakeup source to prepare.
58 * @name: Pointer to the name of the new wakeup source.
59 *
60 * Callers must ensure that the @name string won't be freed when @ws is still in
61 * use.
62 */
63void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
64{
65 if (ws) {
66 memset(ws, 0, sizeof(*ws));
67 ws->name = name;
68 }
69}
70EXPORT_SYMBOL_GPL(wakeup_source_prepare);
71
72/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020073 * wakeup_source_create - Create a struct wakeup_source object.
74 * @name: Name of the new wakeup source.
75 */
76struct wakeup_source *wakeup_source_create(const char *name)
77{
78 struct wakeup_source *ws;
79
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010080 ws = kmalloc(sizeof(*ws), GFP_KERNEL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020081 if (!ws)
82 return NULL;
83
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010084 wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020085 return ws;
86}
87EXPORT_SYMBOL_GPL(wakeup_source_create);
88
89/**
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010090 * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
91 * @ws: Wakeup source to prepare for destruction.
Rafael J. Wysockid94aff82012-02-17 23:39:20 +010092 *
93 * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
94 * be run in parallel with this function for the same wakeup source object.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020095 */
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +010096void wakeup_source_drop(struct wakeup_source *ws)
Rafael J. Wysocki074037e2010-09-22 22:09:10 +020097{
98 if (!ws)
99 return;
100
Rafael J. Wysockid94aff82012-02-17 23:39:20 +0100101 del_timer_sync(&ws->timer);
102 __pm_relax(ws);
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100103}
104EXPORT_SYMBOL_GPL(wakeup_source_drop);
105
106/**
107 * wakeup_source_destroy - Destroy a struct wakeup_source object.
108 * @ws: Wakeup source to destroy.
109 *
110 * Use only for wakeup source objects created with wakeup_source_create().
111 */
112void wakeup_source_destroy(struct wakeup_source *ws)
113{
114 if (!ws)
115 return;
116
117 wakeup_source_drop(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200118 kfree(ws->name);
119 kfree(ws);
120}
121EXPORT_SYMBOL_GPL(wakeup_source_destroy);
122
123/**
124 * wakeup_source_add - Add given object to the list of wakeup sources.
125 * @ws: Wakeup source object to add to the list.
126 */
127void wakeup_source_add(struct wakeup_source *ws)
128{
129 if (WARN_ON(!ws))
130 return;
131
Rafael J. Wysocki7c951492012-02-11 00:00:11 +0100132 spin_lock_init(&ws->lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200133 setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
134 ws->active = false;
135
136 spin_lock_irq(&events_lock);
137 list_add_rcu(&ws->entry, &wakeup_sources);
138 spin_unlock_irq(&events_lock);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200139}
140EXPORT_SYMBOL_GPL(wakeup_source_add);
141
142/**
143 * wakeup_source_remove - Remove given object from the wakeup sources list.
144 * @ws: Wakeup source object to remove from the list.
145 */
146void wakeup_source_remove(struct wakeup_source *ws)
147{
148 if (WARN_ON(!ws))
149 return;
150
151 spin_lock_irq(&events_lock);
152 list_del_rcu(&ws->entry);
153 spin_unlock_irq(&events_lock);
154 synchronize_rcu();
155}
156EXPORT_SYMBOL_GPL(wakeup_source_remove);
157
158/**
159 * wakeup_source_register - Create wakeup source and add it to the list.
160 * @name: Name of the wakeup source to register.
161 */
162struct wakeup_source *wakeup_source_register(const char *name)
163{
164 struct wakeup_source *ws;
165
166 ws = wakeup_source_create(name);
167 if (ws)
168 wakeup_source_add(ws);
169
170 return ws;
171}
172EXPORT_SYMBOL_GPL(wakeup_source_register);
173
174/**
175 * wakeup_source_unregister - Remove wakeup source from the list and remove it.
176 * @ws: Wakeup source object to unregister.
177 */
178void wakeup_source_unregister(struct wakeup_source *ws)
179{
Rafael J. Wysocki8671bbc2012-02-21 23:47:56 +0100180 if (ws) {
181 wakeup_source_remove(ws);
182 wakeup_source_destroy(ws);
183 }
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200184}
185EXPORT_SYMBOL_GPL(wakeup_source_unregister);
186
187/**
188 * device_wakeup_attach - Attach a wakeup source object to a device object.
189 * @dev: Device to handle.
190 * @ws: Wakeup source object to attach to @dev.
191 *
192 * This causes @dev to be treated as a wakeup device.
193 */
194static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
195{
196 spin_lock_irq(&dev->power.lock);
197 if (dev->power.wakeup) {
198 spin_unlock_irq(&dev->power.lock);
199 return -EEXIST;
200 }
201 dev->power.wakeup = ws;
202 spin_unlock_irq(&dev->power.lock);
203 return 0;
204}
205
206/**
207 * device_wakeup_enable - Enable given device to be a wakeup source.
208 * @dev: Device to handle.
209 *
210 * Create a wakeup source object, register it and attach it to @dev.
211 */
212int device_wakeup_enable(struct device *dev)
213{
214 struct wakeup_source *ws;
215 int ret;
216
217 if (!dev || !dev->power.can_wakeup)
218 return -EINVAL;
219
220 ws = wakeup_source_register(dev_name(dev));
221 if (!ws)
222 return -ENOMEM;
223
224 ret = device_wakeup_attach(dev, ws);
225 if (ret)
226 wakeup_source_unregister(ws);
227
228 return ret;
229}
230EXPORT_SYMBOL_GPL(device_wakeup_enable);
231
232/**
233 * device_wakeup_detach - Detach a device's wakeup source object from it.
234 * @dev: Device to detach the wakeup source object from.
235 *
236 * After it returns, @dev will not be treated as a wakeup device any more.
237 */
238static struct wakeup_source *device_wakeup_detach(struct device *dev)
239{
240 struct wakeup_source *ws;
241
242 spin_lock_irq(&dev->power.lock);
243 ws = dev->power.wakeup;
244 dev->power.wakeup = NULL;
245 spin_unlock_irq(&dev->power.lock);
246 return ws;
247}
248
249/**
250 * device_wakeup_disable - Do not regard a device as a wakeup source any more.
251 * @dev: Device to handle.
252 *
253 * Detach the @dev's wakeup source object from it, unregister this wakeup source
254 * object and destroy it.
255 */
256int device_wakeup_disable(struct device *dev)
257{
258 struct wakeup_source *ws;
259
260 if (!dev || !dev->power.can_wakeup)
261 return -EINVAL;
262
263 ws = device_wakeup_detach(dev);
264 if (ws)
265 wakeup_source_unregister(ws);
266
267 return 0;
268}
269EXPORT_SYMBOL_GPL(device_wakeup_disable);
270
271/**
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100272 * device_set_wakeup_capable - Set/reset device wakeup capability flag.
273 * @dev: Device to handle.
274 * @capable: Whether or not @dev is capable of waking up the system from sleep.
275 *
276 * If @capable is set, set the @dev's power.can_wakeup flag and add its
277 * wakeup-related attributes to sysfs. Otherwise, unset the @dev's
278 * power.can_wakeup flag and remove its wakeup-related attributes from sysfs.
279 *
280 * This function may sleep and it can't be called from any context where
281 * sleeping is not allowed.
282 */
283void device_set_wakeup_capable(struct device *dev, bool capable)
284{
285 if (!!dev->power.can_wakeup == !!capable)
286 return;
287
Rafael J. Wysocki22110fa2011-04-26 11:33:09 +0200288 if (device_is_registered(dev) && !list_empty(&dev->power.entry)) {
Rafael J. Wysockicb8f51b2011-02-08 23:26:02 +0100289 if (capable) {
290 if (wakeup_sysfs_add(dev))
291 return;
292 } else {
293 wakeup_sysfs_remove(dev);
294 }
295 }
296 dev->power.can_wakeup = capable;
297}
298EXPORT_SYMBOL_GPL(device_set_wakeup_capable);
299
300/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200301 * device_init_wakeup - Device wakeup initialization.
302 * @dev: Device to handle.
303 * @enable: Whether or not to enable @dev as a wakeup device.
304 *
305 * By default, most devices should leave wakeup disabled. The exceptions are
306 * devices that everyone expects to be wakeup sources: keyboards, power buttons,
Alan Stern8f888932011-09-26 17:38:50 +0200307 * possibly network interfaces, etc. Also, devices that don't generate their
308 * own wakeup requests but merely forward requests from one bus to another
309 * (like PCI bridges) should have wakeup enabled by default.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200310 */
311int device_init_wakeup(struct device *dev, bool enable)
312{
313 int ret = 0;
314
315 if (enable) {
316 device_set_wakeup_capable(dev, true);
317 ret = device_wakeup_enable(dev);
318 } else {
319 device_set_wakeup_capable(dev, false);
320 }
321
322 return ret;
323}
324EXPORT_SYMBOL_GPL(device_init_wakeup);
325
326/**
327 * device_set_wakeup_enable - Enable or disable a device to wake up the system.
328 * @dev: Device to handle.
329 */
330int device_set_wakeup_enable(struct device *dev, bool enable)
331{
332 if (!dev || !dev->power.can_wakeup)
333 return -EINVAL;
334
335 return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
336}
337EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200338
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200339/*
340 * The functions below use the observation that each wakeup event starts a
341 * period in which the system should not be suspended. The moment this period
342 * will end depends on how the wakeup event is going to be processed after being
343 * detected and all of the possible cases can be divided into two distinct
344 * groups.
345 *
346 * First, a wakeup event may be detected by the same functional unit that will
347 * carry out the entire processing of it and possibly will pass it to user space
348 * for further processing. In that case the functional unit that has detected
349 * the event may later "close" the "no suspend" period associated with it
350 * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
351 * pm_relax(), balanced with each other, is supposed to be used in such
352 * situations.
353 *
354 * Second, a wakeup event may be detected by one functional unit and processed
355 * by another one. In that case the unit that has detected it cannot really
356 * "close" the "no suspend" period associated with it, unless it knows in
357 * advance what's going to happen to the event during processing. This
358 * knowledge, however, may not be available to it, so it can simply specify time
359 * to wait before the system can be suspended and pass it as the second
360 * argument of pm_wakeup_event().
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200361 *
362 * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
363 * "no suspend" period will be ended either by the pm_relax(), or by the timer
364 * function executed when the timer expires, whichever comes first.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200365 */
366
367/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200368 * wakup_source_activate - Mark given wakeup source as active.
369 * @ws: Wakeup source to handle.
370 *
371 * Update the @ws' statistics and, if @ws has just been activated, notify the PM
372 * core of the event by incrementing the counter of of wakeup events being
373 * processed.
374 */
375static void wakeup_source_activate(struct wakeup_source *ws)
376{
377 ws->active = true;
378 ws->active_count++;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200379 ws->last_time = ktime_get();
380
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100381 /* Increment the counter of events in progress. */
382 atomic_inc(&combined_event_count);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200383}
384
385/**
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200386 * wakeup_source_report_event - Report wakeup event using the given source.
387 * @ws: Wakeup source to report the event for.
388 */
389static void wakeup_source_report_event(struct wakeup_source *ws)
390{
391 ws->event_count++;
392 /* This is racy, but the counter is approximate anyway. */
393 if (events_check_enabled)
394 ws->wakeup_count++;
395
396 if (!ws->active)
397 wakeup_source_activate(ws);
398}
399
400/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200401 * __pm_stay_awake - Notify the PM core of a wakeup event.
402 * @ws: Wakeup source object associated with the source of the event.
403 *
404 * It is safe to call this function from interrupt context.
405 */
406void __pm_stay_awake(struct wakeup_source *ws)
407{
408 unsigned long flags;
409
410 if (!ws)
411 return;
412
413 spin_lock_irqsave(&ws->lock, flags);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100414
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200415 wakeup_source_report_event(ws);
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100416 del_timer(&ws->timer);
417 ws->timer_expires = 0;
418
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200419 spin_unlock_irqrestore(&ws->lock, flags);
420}
421EXPORT_SYMBOL_GPL(__pm_stay_awake);
422
423/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200424 * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
425 * @dev: Device the wakeup event is related to.
426 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200427 * Notify the PM core of a wakeup event (signaled by @dev) by calling
428 * __pm_stay_awake for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200429 *
430 * Call this function after detecting of a wakeup event if pm_relax() is going
431 * to be called directly after processing the event (and possibly passing it to
432 * user space for further processing).
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200433 */
434void pm_stay_awake(struct device *dev)
435{
436 unsigned long flags;
437
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200438 if (!dev)
439 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200440
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200441 spin_lock_irqsave(&dev->power.lock, flags);
442 __pm_stay_awake(dev->power.wakeup);
443 spin_unlock_irqrestore(&dev->power.lock, flags);
444}
445EXPORT_SYMBOL_GPL(pm_stay_awake);
446
447/**
448 * wakup_source_deactivate - Mark given wakeup source as inactive.
449 * @ws: Wakeup source to handle.
450 *
451 * Update the @ws' statistics and notify the PM core that the wakeup source has
452 * become inactive by decrementing the counter of wakeup events being processed
453 * and incrementing the counter of registered wakeup events.
454 */
455static void wakeup_source_deactivate(struct wakeup_source *ws)
456{
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200457 unsigned int cnt, inpr;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200458 ktime_t duration;
459 ktime_t now;
460
461 ws->relax_count++;
462 /*
463 * __pm_relax() may be called directly or from a timer function.
464 * If it is called directly right after the timer function has been
465 * started, but before the timer function calls __pm_relax(), it is
466 * possible that __pm_stay_awake() will be called in the meantime and
467 * will set ws->active. Then, ws->active may be cleared immediately
468 * by the __pm_relax() called from the timer function, but in such a
469 * case ws->relax_count will be different from ws->active_count.
470 */
471 if (ws->relax_count != ws->active_count) {
472 ws->relax_count--;
473 return;
474 }
475
476 ws->active = false;
477
478 now = ktime_get();
479 duration = ktime_sub(now, ws->last_time);
480 ws->total_time = ktime_add(ws->total_time, duration);
481 if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
482 ws->max_time = duration;
483
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200484 ws->last_time = now;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200485 del_timer(&ws->timer);
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100486 ws->timer_expires = 0;
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200487
488 /*
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100489 * Increment the counter of registered wakeup events and decrement the
490 * couter of wakeup events in progress simultaneously.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200491 */
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100492 atomic_add(MAX_IN_PROGRESS, &combined_event_count);
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200493
494 split_counters(&cnt, &inpr);
495 if (!inpr && waitqueue_active(&wakeup_count_wait_queue))
496 wake_up(&wakeup_count_wait_queue);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200497}
498
499/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200500 * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
501 * @ws: Wakeup source object associated with the source of the event.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200502 *
503 * Call this function for wakeup events whose processing started with calling
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200504 * __pm_stay_awake().
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200505 *
506 * It is safe to call it from interrupt context.
507 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200508void __pm_relax(struct wakeup_source *ws)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200509{
510 unsigned long flags;
511
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200512 if (!ws)
513 return;
514
515 spin_lock_irqsave(&ws->lock, flags);
516 if (ws->active)
517 wakeup_source_deactivate(ws);
518 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200519}
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200520EXPORT_SYMBOL_GPL(__pm_relax);
521
522/**
523 * pm_relax - Notify the PM core that processing of a wakeup event has ended.
524 * @dev: Device that signaled the event.
525 *
526 * Execute __pm_relax() for the @dev's wakeup source object.
527 */
528void pm_relax(struct device *dev)
529{
530 unsigned long flags;
531
532 if (!dev)
533 return;
534
535 spin_lock_irqsave(&dev->power.lock, flags);
536 __pm_relax(dev->power.wakeup);
537 spin_unlock_irqrestore(&dev->power.lock, flags);
538}
539EXPORT_SYMBOL_GPL(pm_relax);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200540
541/**
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200542 * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200543 * @data: Address of the wakeup source object associated with the event source.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200544 *
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100545 * Call wakeup_source_deactivate() for the wakeup source whose address is stored
546 * in @data if it is currently active and its timer has not been canceled and
547 * the expiration time of the timer is not in future.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200548 */
Rafael J. Wysocki4eb241e2010-07-07 23:43:51 +0200549static void pm_wakeup_timer_fn(unsigned long data)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200550{
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100551 struct wakeup_source *ws = (struct wakeup_source *)data;
552 unsigned long flags;
553
554 spin_lock_irqsave(&ws->lock, flags);
555
556 if (ws->active && ws->timer_expires
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200557 && time_after_eq(jiffies, ws->timer_expires)) {
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100558 wakeup_source_deactivate(ws);
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200559 ws->expire_count++;
560 }
Rafael J. Wysockida863cd2012-02-17 23:39:33 +0100561
562 spin_unlock_irqrestore(&ws->lock, flags);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200563}
564
565/**
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200566 * __pm_wakeup_event - Notify the PM core of a wakeup event.
567 * @ws: Wakeup source object associated with the event source.
568 * @msec: Anticipated event processing time (in milliseconds).
569 *
570 * Notify the PM core of a wakeup event whose source is @ws that will take
571 * approximately @msec milliseconds to be processed by the kernel. If @ws is
572 * not active, activate it. If @msec is nonzero, set up the @ws' timer to
573 * execute pm_wakeup_timer_fn() in future.
574 *
575 * It is safe to call this function from interrupt context.
576 */
577void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
578{
579 unsigned long flags;
580 unsigned long expires;
581
582 if (!ws)
583 return;
584
585 spin_lock_irqsave(&ws->lock, flags);
586
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200587 wakeup_source_report_event(ws);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200588
589 if (!msec) {
590 wakeup_source_deactivate(ws);
591 goto unlock;
592 }
593
594 expires = jiffies + msecs_to_jiffies(msec);
595 if (!expires)
596 expires = 1;
597
Rafael J. Wysocki4782e162012-02-17 23:39:39 +0100598 if (!ws->timer_expires || time_after(expires, ws->timer_expires)) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200599 mod_timer(&ws->timer, expires);
600 ws->timer_expires = expires;
601 }
602
603 unlock:
604 spin_unlock_irqrestore(&ws->lock, flags);
605}
606EXPORT_SYMBOL_GPL(__pm_wakeup_event);
607
608
609/**
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200610 * pm_wakeup_event - Notify the PM core of a wakeup event.
611 * @dev: Device the wakeup event is related to.
612 * @msec: Anticipated event processing time (in milliseconds).
613 *
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200614 * Call __pm_wakeup_event() for the @dev's wakeup source object.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200615 */
616void pm_wakeup_event(struct device *dev, unsigned int msec)
617{
618 unsigned long flags;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200619
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200620 if (!dev)
621 return;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200622
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200623 spin_lock_irqsave(&dev->power.lock, flags);
624 __pm_wakeup_event(dev->power.wakeup, msec);
625 spin_unlock_irqrestore(&dev->power.lock, flags);
626}
627EXPORT_SYMBOL_GPL(pm_wakeup_event);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200628
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200629/**
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100630 * pm_wakeup_pending - Check if power transition in progress should be aborted.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200631 *
632 * Compare the current number of registered wakeup events with its preserved
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100633 * value from the past and return true if new wakeup events have been registered
634 * since the old value was stored. Also return true if the current number of
635 * wakeup events being processed is different from zero.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200636 */
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100637bool pm_wakeup_pending(void)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200638{
639 unsigned long flags;
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100640 bool ret = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200641
642 spin_lock_irqsave(&events_lock, flags);
643 if (events_check_enabled) {
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100644 unsigned int cnt, inpr;
645
646 split_counters(&cnt, &inpr);
647 ret = (cnt != saved_count || inpr > 0);
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100648 events_check_enabled = !ret;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200649 }
650 spin_unlock_irqrestore(&events_lock, flags);
651 return ret;
652}
653
654/**
655 * pm_get_wakeup_count - Read the number of registered wakeup events.
656 * @count: Address to store the value at.
657 *
658 * Store the number of registered wakeup events at the address in @count. Block
659 * if the current number of wakeup events being processed is nonzero.
660 *
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100661 * Return 'false' if the wait for the number of wakeup events being processed to
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200662 * drop down to zero has been interrupted by a signal (and the current number
Rafael J. Wysocki790c7882011-01-31 11:07:01 +0100663 * of wakeup events being processed is still nonzero). Otherwise return 'true'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200664 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200665bool pm_get_wakeup_count(unsigned int *count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200666{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100667 unsigned int cnt, inpr;
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200668 DEFINE_WAIT(wait);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200669
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100670 for (;;) {
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200671 prepare_to_wait(&wakeup_count_wait_queue, &wait,
672 TASK_INTERRUPTIBLE);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100673 split_counters(&cnt, &inpr);
674 if (inpr == 0 || signal_pending(current))
675 break;
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200676
677 schedule();
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200678 }
Rafael J. Wysocki60af1062012-04-29 22:52:34 +0200679 finish_wait(&wakeup_count_wait_queue, &wait);
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200680
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100681 split_counters(&cnt, &inpr);
682 *count = cnt;
683 return !inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200684}
685
686/**
687 * pm_save_wakeup_count - Save the current number of registered wakeup events.
688 * @count: Value to compare with the current number of registered wakeup events.
689 *
690 * If @count is equal to the current number of registered wakeup events and the
691 * current number of wakeup events being processed is zero, store @count as the
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100692 * old number of registered wakeup events for pm_check_wakeup_events(), enable
693 * wakeup events detection and return 'true'. Otherwise disable wakeup events
694 * detection and return 'false'.
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200695 */
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200696bool pm_save_wakeup_count(unsigned int count)
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200697{
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100698 unsigned int cnt, inpr;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200699
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100700 events_check_enabled = false;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200701 spin_lock_irq(&events_lock);
Rafael J. Wysocki023d3772011-01-31 11:06:39 +0100702 split_counters(&cnt, &inpr);
703 if (cnt == count && inpr == 0) {
Rafael J. Wysocki074037e2010-09-22 22:09:10 +0200704 saved_count = count;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200705 events_check_enabled = true;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200706 }
707 spin_unlock_irq(&events_lock);
Rafael J. Wysocki378eef92011-01-31 11:06:50 +0100708 return events_check_enabled;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200709}
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200710
711static struct dentry *wakeup_sources_stats_dentry;
712
713/**
714 * print_wakeup_source_stats - Print wakeup source statistics information.
715 * @m: seq_file to print the statistics into.
716 * @ws: Wakeup source object to print the statistics for.
717 */
718static int print_wakeup_source_stats(struct seq_file *m,
719 struct wakeup_source *ws)
720{
721 unsigned long flags;
722 ktime_t total_time;
723 ktime_t max_time;
724 unsigned long active_count;
725 ktime_t active_time;
726 int ret;
727
728 spin_lock_irqsave(&ws->lock, flags);
729
730 total_time = ws->total_time;
731 max_time = ws->max_time;
732 active_count = ws->active_count;
733 if (ws->active) {
734 active_time = ktime_sub(ktime_get(), ws->last_time);
735 total_time = ktime_add(total_time, active_time);
736 if (active_time.tv64 > max_time.tv64)
737 max_time = active_time;
738 } else {
739 active_time = ktime_set(0, 0);
740 }
741
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200742 ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t"
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200743 "%lld\t\t%lld\t\t%lld\t\t%lld\n",
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200744 ws->name, active_count, ws->event_count,
745 ws->wakeup_count, ws->expire_count,
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200746 ktime_to_ms(active_time), ktime_to_ms(total_time),
747 ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
748
749 spin_unlock_irqrestore(&ws->lock, flags);
750
751 return ret;
752}
753
754/**
755 * wakeup_sources_stats_show - Print wakeup sources statistics information.
756 * @m: seq_file to print the statistics into.
757 */
758static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
759{
760 struct wakeup_source *ws;
761
Rafael J. Wysocki30e3ce62012-04-29 22:52:52 +0200762 seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t"
763 "expire_count\tactive_since\ttotal_time\tmax_time\t"
764 "last_change\n");
Rafael J. Wysocki9c034392010-10-19 23:42:49 +0200765
766 rcu_read_lock();
767 list_for_each_entry_rcu(ws, &wakeup_sources, entry)
768 print_wakeup_source_stats(m, ws);
769 rcu_read_unlock();
770
771 return 0;
772}
773
774static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
775{
776 return single_open(file, wakeup_sources_stats_show, NULL);
777}
778
779static const struct file_operations wakeup_sources_stats_fops = {
780 .owner = THIS_MODULE,
781 .open = wakeup_sources_stats_open,
782 .read = seq_read,
783 .llseek = seq_lseek,
784 .release = single_release,
785};
786
787static int __init wakeup_sources_debugfs_init(void)
788{
789 wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
790 S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
791 return 0;
792}
793
794postcore_initcall(wakeup_sources_debugfs_init);