PM / Wakeup: Don't update events_check_enabled in pm_get_wakeup_count()
[pandora-kernel.git] / drivers / base / power / wakeup.c
1 /*
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>
13 #include <linux/suspend.h>
14 #include <linux/seq_file.h>
15 #include <linux/debugfs.h>
16
17 #include "power.h"
18
19 #define TIMEOUT         100
20
21 /*
22  * If set, the suspend/hibernate code will abort transitions to a sleep state
23  * if wakeup events are registered during or immediately before the transition.
24  */
25 bool events_check_enabled;
26
27 /*
28  * Combined counters of registered wakeup events and wakeup events in progress.
29  * They need to be modified together atomically, so it's better to use one
30  * atomic variable to hold them both.
31  */
32 static atomic_t combined_event_count = ATOMIC_INIT(0);
33
34 #define IN_PROGRESS_BITS        (sizeof(int) * 4)
35 #define MAX_IN_PROGRESS         ((1 << IN_PROGRESS_BITS) - 1)
36
37 static void split_counters(unsigned int *cnt, unsigned int *inpr)
38 {
39         unsigned int comb = atomic_read(&combined_event_count);
40
41         *cnt = (comb >> IN_PROGRESS_BITS);
42         *inpr = comb & MAX_IN_PROGRESS;
43 }
44
45 /* A preserved old value of the events counter. */
46 static unsigned int saved_count;
47
48 static DEFINE_SPINLOCK(events_lock);
49
50 static void pm_wakeup_timer_fn(unsigned long data);
51
52 static LIST_HEAD(wakeup_sources);
53
54 /**
55  * wakeup_source_create - Create a struct wakeup_source object.
56  * @name: Name of the new wakeup source.
57  */
58 struct wakeup_source *wakeup_source_create(const char *name)
59 {
60         struct wakeup_source *ws;
61
62         ws = kzalloc(sizeof(*ws), GFP_KERNEL);
63         if (!ws)
64                 return NULL;
65
66         spin_lock_init(&ws->lock);
67         if (name)
68                 ws->name = kstrdup(name, GFP_KERNEL);
69
70         return ws;
71 }
72 EXPORT_SYMBOL_GPL(wakeup_source_create);
73
74 /**
75  * wakeup_source_destroy - Destroy a struct wakeup_source object.
76  * @ws: Wakeup source to destroy.
77  */
78 void 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 }
96 EXPORT_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  */
102 void wakeup_source_add(struct wakeup_source *ws)
103 {
104         if (WARN_ON(!ws))
105                 return;
106
107         setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
108         ws->active = false;
109
110         spin_lock_irq(&events_lock);
111         list_add_rcu(&ws->entry, &wakeup_sources);
112         spin_unlock_irq(&events_lock);
113         synchronize_rcu();
114 }
115 EXPORT_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  */
121 void 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 }
131 EXPORT_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  */
137 struct 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 }
147 EXPORT_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  */
153 void wakeup_source_unregister(struct wakeup_source *ws)
154 {
155         wakeup_source_remove(ws);
156         wakeup_source_destroy(ws);
157 }
158 EXPORT_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  */
167 static 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  */
185 int 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 }
203 EXPORT_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  */
211 static 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  */
229 int 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 }
242 EXPORT_SYMBOL_GPL(device_wakeup_disable);
243
244 /**
245  * device_init_wakeup - Device wakeup initialization.
246  * @dev: Device to handle.
247  * @enable: Whether or not to enable @dev as a wakeup device.
248  *
249  * By default, most devices should leave wakeup disabled.  The exceptions are
250  * devices that everyone expects to be wakeup sources: keyboards, power buttons,
251  * possibly network interfaces, etc.
252  */
253 int device_init_wakeup(struct device *dev, bool enable)
254 {
255         int ret = 0;
256
257         if (enable) {
258                 device_set_wakeup_capable(dev, true);
259                 ret = device_wakeup_enable(dev);
260         } else {
261                 device_set_wakeup_capable(dev, false);
262         }
263
264         return ret;
265 }
266 EXPORT_SYMBOL_GPL(device_init_wakeup);
267
268 /**
269  * device_set_wakeup_enable - Enable or disable a device to wake up the system.
270  * @dev: Device to handle.
271  */
272 int device_set_wakeup_enable(struct device *dev, bool enable)
273 {
274         if (!dev || !dev->power.can_wakeup)
275                 return -EINVAL;
276
277         return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
278 }
279 EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
280
281 /*
282  * The functions below use the observation that each wakeup event starts a
283  * period in which the system should not be suspended.  The moment this period
284  * will end depends on how the wakeup event is going to be processed after being
285  * detected and all of the possible cases can be divided into two distinct
286  * groups.
287  *
288  * First, a wakeup event may be detected by the same functional unit that will
289  * carry out the entire processing of it and possibly will pass it to user space
290  * for further processing.  In that case the functional unit that has detected
291  * the event may later "close" the "no suspend" period associated with it
292  * directly as soon as it has been dealt with.  The pair of pm_stay_awake() and
293  * pm_relax(), balanced with each other, is supposed to be used in such
294  * situations.
295  *
296  * Second, a wakeup event may be detected by one functional unit and processed
297  * by another one.  In that case the unit that has detected it cannot really
298  * "close" the "no suspend" period associated with it, unless it knows in
299  * advance what's going to happen to the event during processing.  This
300  * knowledge, however, may not be available to it, so it can simply specify time
301  * to wait before the system can be suspended and pass it as the second
302  * argument of pm_wakeup_event().
303  *
304  * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
305  * "no suspend" period will be ended either by the pm_relax(), or by the timer
306  * function executed when the timer expires, whichever comes first.
307  */
308
309 /**
310  * wakup_source_activate - Mark given wakeup source as active.
311  * @ws: Wakeup source to handle.
312  *
313  * Update the @ws' statistics and, if @ws has just been activated, notify the PM
314  * core of the event by incrementing the counter of of wakeup events being
315  * processed.
316  */
317 static void wakeup_source_activate(struct wakeup_source *ws)
318 {
319         ws->active = true;
320         ws->active_count++;
321         ws->timer_expires = jiffies;
322         ws->last_time = ktime_get();
323
324         /* Increment the counter of events in progress. */
325         atomic_inc(&combined_event_count);
326 }
327
328 /**
329  * __pm_stay_awake - Notify the PM core of a wakeup event.
330  * @ws: Wakeup source object associated with the source of the event.
331  *
332  * It is safe to call this function from interrupt context.
333  */
334 void __pm_stay_awake(struct wakeup_source *ws)
335 {
336         unsigned long flags;
337
338         if (!ws)
339                 return;
340
341         spin_lock_irqsave(&ws->lock, flags);
342         ws->event_count++;
343         if (!ws->active)
344                 wakeup_source_activate(ws);
345         spin_unlock_irqrestore(&ws->lock, flags);
346 }
347 EXPORT_SYMBOL_GPL(__pm_stay_awake);
348
349 /**
350  * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
351  * @dev: Device the wakeup event is related to.
352  *
353  * Notify the PM core of a wakeup event (signaled by @dev) by calling
354  * __pm_stay_awake for the @dev's wakeup source object.
355  *
356  * Call this function after detecting of a wakeup event if pm_relax() is going
357  * to be called directly after processing the event (and possibly passing it to
358  * user space for further processing).
359  */
360 void pm_stay_awake(struct device *dev)
361 {
362         unsigned long flags;
363
364         if (!dev)
365                 return;
366
367         spin_lock_irqsave(&dev->power.lock, flags);
368         __pm_stay_awake(dev->power.wakeup);
369         spin_unlock_irqrestore(&dev->power.lock, flags);
370 }
371 EXPORT_SYMBOL_GPL(pm_stay_awake);
372
373 /**
374  * wakup_source_deactivate - Mark given wakeup source as inactive.
375  * @ws: Wakeup source to handle.
376  *
377  * Update the @ws' statistics and notify the PM core that the wakeup source has
378  * become inactive by decrementing the counter of wakeup events being processed
379  * and incrementing the counter of registered wakeup events.
380  */
381 static void wakeup_source_deactivate(struct wakeup_source *ws)
382 {
383         ktime_t duration;
384         ktime_t now;
385
386         ws->relax_count++;
387         /*
388          * __pm_relax() may be called directly or from a timer function.
389          * If it is called directly right after the timer function has been
390          * started, but before the timer function calls __pm_relax(), it is
391          * possible that __pm_stay_awake() will be called in the meantime and
392          * will set ws->active.  Then, ws->active may be cleared immediately
393          * by the __pm_relax() called from the timer function, but in such a
394          * case ws->relax_count will be different from ws->active_count.
395          */
396         if (ws->relax_count != ws->active_count) {
397                 ws->relax_count--;
398                 return;
399         }
400
401         ws->active = false;
402
403         now = ktime_get();
404         duration = ktime_sub(now, ws->last_time);
405         ws->total_time = ktime_add(ws->total_time, duration);
406         if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
407                 ws->max_time = duration;
408
409         del_timer(&ws->timer);
410
411         /*
412          * Increment the counter of registered wakeup events and decrement the
413          * couter of wakeup events in progress simultaneously.
414          */
415         atomic_add(MAX_IN_PROGRESS, &combined_event_count);
416 }
417
418 /**
419  * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
420  * @ws: Wakeup source object associated with the source of the event.
421  *
422  * Call this function for wakeup events whose processing started with calling
423  * __pm_stay_awake().
424  *
425  * It is safe to call it from interrupt context.
426  */
427 void __pm_relax(struct wakeup_source *ws)
428 {
429         unsigned long flags;
430
431         if (!ws)
432                 return;
433
434         spin_lock_irqsave(&ws->lock, flags);
435         if (ws->active)
436                 wakeup_source_deactivate(ws);
437         spin_unlock_irqrestore(&ws->lock, flags);
438 }
439 EXPORT_SYMBOL_GPL(__pm_relax);
440
441 /**
442  * pm_relax - Notify the PM core that processing of a wakeup event has ended.
443  * @dev: Device that signaled the event.
444  *
445  * Execute __pm_relax() for the @dev's wakeup source object.
446  */
447 void pm_relax(struct device *dev)
448 {
449         unsigned long flags;
450
451         if (!dev)
452                 return;
453
454         spin_lock_irqsave(&dev->power.lock, flags);
455         __pm_relax(dev->power.wakeup);
456         spin_unlock_irqrestore(&dev->power.lock, flags);
457 }
458 EXPORT_SYMBOL_GPL(pm_relax);
459
460 /**
461  * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
462  * @data: Address of the wakeup source object associated with the event source.
463  *
464  * Call __pm_relax() for the wakeup source whose address is stored in @data.
465  */
466 static void pm_wakeup_timer_fn(unsigned long data)
467 {
468         __pm_relax((struct wakeup_source *)data);
469 }
470
471 /**
472  * __pm_wakeup_event - Notify the PM core of a wakeup event.
473  * @ws: Wakeup source object associated with the event source.
474  * @msec: Anticipated event processing time (in milliseconds).
475  *
476  * Notify the PM core of a wakeup event whose source is @ws that will take
477  * approximately @msec milliseconds to be processed by the kernel.  If @ws is
478  * not active, activate it.  If @msec is nonzero, set up the @ws' timer to
479  * execute pm_wakeup_timer_fn() in future.
480  *
481  * It is safe to call this function from interrupt context.
482  */
483 void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
484 {
485         unsigned long flags;
486         unsigned long expires;
487
488         if (!ws)
489                 return;
490
491         spin_lock_irqsave(&ws->lock, flags);
492
493         ws->event_count++;
494         if (!ws->active)
495                 wakeup_source_activate(ws);
496
497         if (!msec) {
498                 wakeup_source_deactivate(ws);
499                 goto unlock;
500         }
501
502         expires = jiffies + msecs_to_jiffies(msec);
503         if (!expires)
504                 expires = 1;
505
506         if (time_after(expires, ws->timer_expires)) {
507                 mod_timer(&ws->timer, expires);
508                 ws->timer_expires = expires;
509         }
510
511  unlock:
512         spin_unlock_irqrestore(&ws->lock, flags);
513 }
514 EXPORT_SYMBOL_GPL(__pm_wakeup_event);
515
516
517 /**
518  * pm_wakeup_event - Notify the PM core of a wakeup event.
519  * @dev: Device the wakeup event is related to.
520  * @msec: Anticipated event processing time (in milliseconds).
521  *
522  * Call __pm_wakeup_event() for the @dev's wakeup source object.
523  */
524 void pm_wakeup_event(struct device *dev, unsigned int msec)
525 {
526         unsigned long flags;
527
528         if (!dev)
529                 return;
530
531         spin_lock_irqsave(&dev->power.lock, flags);
532         __pm_wakeup_event(dev->power.wakeup, msec);
533         spin_unlock_irqrestore(&dev->power.lock, flags);
534 }
535 EXPORT_SYMBOL_GPL(pm_wakeup_event);
536
537 /**
538  * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
539  */
540 static void pm_wakeup_update_hit_counts(void)
541 {
542         unsigned long flags;
543         struct wakeup_source *ws;
544
545         rcu_read_lock();
546         list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
547                 spin_lock_irqsave(&ws->lock, flags);
548                 if (ws->active)
549                         ws->hit_count++;
550                 spin_unlock_irqrestore(&ws->lock, flags);
551         }
552         rcu_read_unlock();
553 }
554
555 /**
556  * pm_wakeup_pending - Check if power transition in progress should be aborted.
557  *
558  * Compare the current number of registered wakeup events with its preserved
559  * value from the past and return true if new wakeup events have been registered
560  * since the old value was stored.  Also return true if the current number of
561  * wakeup events being processed is different from zero.
562  */
563 bool pm_wakeup_pending(void)
564 {
565         unsigned long flags;
566         bool ret = false;
567
568         spin_lock_irqsave(&events_lock, flags);
569         if (events_check_enabled) {
570                 unsigned int cnt, inpr;
571
572                 split_counters(&cnt, &inpr);
573                 ret = (cnt != saved_count || inpr > 0);
574                 events_check_enabled = !ret;
575         }
576         spin_unlock_irqrestore(&events_lock, flags);
577         if (ret)
578                 pm_wakeup_update_hit_counts();
579         return ret;
580 }
581
582 /**
583  * pm_get_wakeup_count - Read the number of registered wakeup events.
584  * @count: Address to store the value at.
585  *
586  * Store the number of registered wakeup events at the address in @count.  Block
587  * if the current number of wakeup events being processed is nonzero.
588  *
589  * Return 'false' if the wait for the number of wakeup events being processed to
590  * drop down to zero has been interrupted by a signal (and the current number
591  * of wakeup events being processed is still nonzero).  Otherwise return 'true'.
592  */
593 bool pm_get_wakeup_count(unsigned int *count)
594 {
595         unsigned int cnt, inpr;
596
597         for (;;) {
598                 split_counters(&cnt, &inpr);
599                 if (inpr == 0 || signal_pending(current))
600                         break;
601                 pm_wakeup_update_hit_counts();
602                 schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
603         }
604
605         split_counters(&cnt, &inpr);
606         *count = cnt;
607         return !inpr;
608 }
609
610 /**
611  * pm_save_wakeup_count - Save the current number of registered wakeup events.
612  * @count: Value to compare with the current number of registered wakeup events.
613  *
614  * If @count is equal to the current number of registered wakeup events and the
615  * current number of wakeup events being processed is zero, store @count as the
616  * old number of registered wakeup events for pm_check_wakeup_events(), enable
617  * wakeup events detection and return 'true'.  Otherwise disable wakeup events
618  * detection and return 'false'.
619  */
620 bool pm_save_wakeup_count(unsigned int count)
621 {
622         unsigned int cnt, inpr;
623
624         events_check_enabled = false;
625         spin_lock_irq(&events_lock);
626         split_counters(&cnt, &inpr);
627         if (cnt == count && inpr == 0) {
628                 saved_count = count;
629                 events_check_enabled = true;
630         }
631         spin_unlock_irq(&events_lock);
632         if (!events_check_enabled)
633                 pm_wakeup_update_hit_counts();
634         return events_check_enabled;
635 }
636
637 static struct dentry *wakeup_sources_stats_dentry;
638
639 /**
640  * print_wakeup_source_stats - Print wakeup source statistics information.
641  * @m: seq_file to print the statistics into.
642  * @ws: Wakeup source object to print the statistics for.
643  */
644 static int print_wakeup_source_stats(struct seq_file *m,
645                                      struct wakeup_source *ws)
646 {
647         unsigned long flags;
648         ktime_t total_time;
649         ktime_t max_time;
650         unsigned long active_count;
651         ktime_t active_time;
652         int ret;
653
654         spin_lock_irqsave(&ws->lock, flags);
655
656         total_time = ws->total_time;
657         max_time = ws->max_time;
658         active_count = ws->active_count;
659         if (ws->active) {
660                 active_time = ktime_sub(ktime_get(), ws->last_time);
661                 total_time = ktime_add(total_time, active_time);
662                 if (active_time.tv64 > max_time.tv64)
663                         max_time = active_time;
664         } else {
665                 active_time = ktime_set(0, 0);
666         }
667
668         ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
669                         "%lld\t\t%lld\t\t%lld\t\t%lld\n",
670                         ws->name, active_count, ws->event_count, ws->hit_count,
671                         ktime_to_ms(active_time), ktime_to_ms(total_time),
672                         ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
673
674         spin_unlock_irqrestore(&ws->lock, flags);
675
676         return ret;
677 }
678
679 /**
680  * wakeup_sources_stats_show - Print wakeup sources statistics information.
681  * @m: seq_file to print the statistics into.
682  */
683 static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
684 {
685         struct wakeup_source *ws;
686
687         seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
688                 "active_since\ttotal_time\tmax_time\tlast_change\n");
689
690         rcu_read_lock();
691         list_for_each_entry_rcu(ws, &wakeup_sources, entry)
692                 print_wakeup_source_stats(m, ws);
693         rcu_read_unlock();
694
695         return 0;
696 }
697
698 static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
699 {
700         return single_open(file, wakeup_sources_stats_show, NULL);
701 }
702
703 static const struct file_operations wakeup_sources_stats_fops = {
704         .owner = THIS_MODULE,
705         .open = wakeup_sources_stats_open,
706         .read = seq_read,
707         .llseek = seq_lseek,
708         .release = single_release,
709 };
710
711 static int __init wakeup_sources_debugfs_init(void)
712 {
713         wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
714                         S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
715         return 0;
716 }
717
718 postcore_initcall(wakeup_sources_debugfs_init);