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