]>
Commit | Line | Data |
---|---|---|
c125e96f RW |
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 | ||
7a5bd127 JP |
9 | #define pr_fmt(fmt) "PM: " fmt |
10 | ||
c125e96f RW |
11 | #include <linux/device.h> |
12 | #include <linux/slab.h> | |
174cd4b1 | 13 | #include <linux/sched/signal.h> |
c125e96f | 14 | #include <linux/capability.h> |
1b6bc32f | 15 | #include <linux/export.h> |
c125e96f | 16 | #include <linux/suspend.h> |
9c034392 RW |
17 | #include <linux/seq_file.h> |
18 | #include <linux/debugfs.h> | |
4990d4fe | 19 | #include <linux/pm_wakeirq.h> |
6791e36c | 20 | #include <trace/events/power.h> |
074037ec RW |
21 | |
22 | #include "power.h" | |
23 | ||
0026cef0 UH |
24 | #ifndef CONFIG_SUSPEND |
25 | suspend_state_t pm_suspend_target_state; | |
26 | #define pm_suspend_target_state (PM_SUSPEND_ON) | |
27 | #endif | |
28 | ||
c125e96f RW |
29 | /* |
30 | * If set, the suspend/hibernate code will abort transitions to a sleep state | |
31 | * if wakeup events are registered during or immediately before the transition. | |
32 | */ | |
30e3ce6d | 33 | bool events_check_enabled __read_mostly; |
c125e96f | 34 | |
a6f5f0dd AY |
35 | /* First wakeup IRQ seen by the kernel in the last cycle. */ |
36 | unsigned int pm_wakeup_irq __read_mostly; | |
37 | ||
33e4f80e RW |
38 | /* If greater than 0 and the system is suspending, terminate the suspend. */ |
39 | static atomic_t pm_abort_suspend __read_mostly; | |
068765ba | 40 | |
023d3779 RW |
41 | /* |
42 | * Combined counters of registered wakeup events and wakeup events in progress. | |
43 | * They need to be modified together atomically, so it's better to use one | |
44 | * atomic variable to hold them both. | |
45 | */ | |
46 | static atomic_t combined_event_count = ATOMIC_INIT(0); | |
47 | ||
48 | #define IN_PROGRESS_BITS (sizeof(int) * 4) | |
49 | #define MAX_IN_PROGRESS ((1 << IN_PROGRESS_BITS) - 1) | |
50 | ||
51 | static void split_counters(unsigned int *cnt, unsigned int *inpr) | |
52 | { | |
53 | unsigned int comb = atomic_read(&combined_event_count); | |
54 | ||
55 | *cnt = (comb >> IN_PROGRESS_BITS); | |
56 | *inpr = comb & MAX_IN_PROGRESS; | |
57 | } | |
58 | ||
59 | /* A preserved old value of the events counter. */ | |
074037ec | 60 | static unsigned int saved_count; |
c125e96f | 61 | |
bccaadab | 62 | static DEFINE_RAW_SPINLOCK(events_lock); |
c125e96f | 63 | |
96428e98 | 64 | static void pm_wakeup_timer_fn(struct timer_list *t); |
4eb241e5 | 65 | |
074037ec RW |
66 | static LIST_HEAD(wakeup_sources); |
67 | ||
60af1066 RW |
68 | static DECLARE_WAIT_QUEUE_HEAD(wakeup_count_wait_queue); |
69 | ||
ea0212f4 TG |
70 | DEFINE_STATIC_SRCU(wakeup_srcu); |
71 | ||
7f436055 JQ |
72 | static struct wakeup_source deleted_ws = { |
73 | .name = "deleted", | |
74 | .lock = __SPIN_LOCK_UNLOCKED(deleted_ws.lock), | |
75 | }; | |
76 | ||
8671bbc1 RW |
77 | /** |
78 | * wakeup_source_prepare - Prepare a new wakeup source for initialization. | |
79 | * @ws: Wakeup source to prepare. | |
80 | * @name: Pointer to the name of the new wakeup source. | |
81 | * | |
82 | * Callers must ensure that the @name string won't be freed when @ws is still in | |
83 | * use. | |
84 | */ | |
85 | void wakeup_source_prepare(struct wakeup_source *ws, const char *name) | |
86 | { | |
87 | if (ws) { | |
88 | memset(ws, 0, sizeof(*ws)); | |
89 | ws->name = name; | |
90 | } | |
91 | } | |
92 | EXPORT_SYMBOL_GPL(wakeup_source_prepare); | |
93 | ||
074037ec RW |
94 | /** |
95 | * wakeup_source_create - Create a struct wakeup_source object. | |
96 | * @name: Name of the new wakeup source. | |
97 | */ | |
98 | struct wakeup_source *wakeup_source_create(const char *name) | |
99 | { | |
100 | struct wakeup_source *ws; | |
101 | ||
8671bbc1 | 102 | ws = kmalloc(sizeof(*ws), GFP_KERNEL); |
074037ec RW |
103 | if (!ws) |
104 | return NULL; | |
105 | ||
7236214c | 106 | wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL); |
074037ec RW |
107 | return ws; |
108 | } | |
109 | EXPORT_SYMBOL_GPL(wakeup_source_create); | |
110 | ||
7f436055 JQ |
111 | /* |
112 | * Record wakeup_source statistics being deleted into a dummy wakeup_source. | |
113 | */ | |
114 | static void wakeup_source_record(struct wakeup_source *ws) | |
115 | { | |
116 | unsigned long flags; | |
117 | ||
118 | spin_lock_irqsave(&deleted_ws.lock, flags); | |
119 | ||
120 | if (ws->event_count) { | |
121 | deleted_ws.total_time = | |
122 | ktime_add(deleted_ws.total_time, ws->total_time); | |
123 | deleted_ws.prevent_sleep_time = | |
124 | ktime_add(deleted_ws.prevent_sleep_time, | |
125 | ws->prevent_sleep_time); | |
126 | deleted_ws.max_time = | |
127 | ktime_compare(deleted_ws.max_time, ws->max_time) > 0 ? | |
128 | deleted_ws.max_time : ws->max_time; | |
129 | deleted_ws.event_count += ws->event_count; | |
130 | deleted_ws.active_count += ws->active_count; | |
131 | deleted_ws.relax_count += ws->relax_count; | |
132 | deleted_ws.expire_count += ws->expire_count; | |
133 | deleted_ws.wakeup_count += ws->wakeup_count; | |
134 | } | |
135 | ||
136 | spin_unlock_irqrestore(&deleted_ws.lock, flags); | |
137 | } | |
138 | ||
8671bbc1 RW |
139 | /** |
140 | * wakeup_source_destroy - Destroy a struct wakeup_source object. | |
141 | * @ws: Wakeup source to destroy. | |
142 | * | |
143 | * Use only for wakeup source objects created with wakeup_source_create(). | |
144 | */ | |
145 | void wakeup_source_destroy(struct wakeup_source *ws) | |
146 | { | |
147 | if (!ws) | |
148 | return; | |
149 | ||
623217a0 | 150 | __pm_relax(ws); |
7f436055 | 151 | wakeup_source_record(ws); |
7236214c | 152 | kfree_const(ws->name); |
074037ec RW |
153 | kfree(ws); |
154 | } | |
155 | EXPORT_SYMBOL_GPL(wakeup_source_destroy); | |
156 | ||
157 | /** | |
158 | * wakeup_source_add - Add given object to the list of wakeup sources. | |
159 | * @ws: Wakeup source object to add to the list. | |
160 | */ | |
161 | void wakeup_source_add(struct wakeup_source *ws) | |
162 | { | |
49550709 JS |
163 | unsigned long flags; |
164 | ||
074037ec RW |
165 | if (WARN_ON(!ws)) |
166 | return; | |
167 | ||
7c95149b | 168 | spin_lock_init(&ws->lock); |
96428e98 | 169 | timer_setup(&ws->timer, pm_wakeup_timer_fn, 0); |
074037ec RW |
170 | ws->active = false; |
171 | ||
bccaadab | 172 | raw_spin_lock_irqsave(&events_lock, flags); |
074037ec | 173 | list_add_rcu(&ws->entry, &wakeup_sources); |
bccaadab | 174 | raw_spin_unlock_irqrestore(&events_lock, flags); |
074037ec RW |
175 | } |
176 | EXPORT_SYMBOL_GPL(wakeup_source_add); | |
177 | ||
178 | /** | |
179 | * wakeup_source_remove - Remove given object from the wakeup sources list. | |
180 | * @ws: Wakeup source object to remove from the list. | |
181 | */ | |
182 | void wakeup_source_remove(struct wakeup_source *ws) | |
183 | { | |
49550709 JS |
184 | unsigned long flags; |
185 | ||
074037ec RW |
186 | if (WARN_ON(!ws)) |
187 | return; | |
188 | ||
bccaadab | 189 | raw_spin_lock_irqsave(&events_lock, flags); |
074037ec | 190 | list_del_rcu(&ws->entry); |
bccaadab | 191 | raw_spin_unlock_irqrestore(&events_lock, flags); |
ea0212f4 | 192 | synchronize_srcu(&wakeup_srcu); |
1fad17fb VK |
193 | |
194 | del_timer_sync(&ws->timer); | |
195 | /* | |
196 | * Clear timer.function to make wakeup_source_not_registered() treat | |
197 | * this wakeup source as not registered. | |
198 | */ | |
199 | ws->timer.function = NULL; | |
074037ec RW |
200 | } |
201 | EXPORT_SYMBOL_GPL(wakeup_source_remove); | |
202 | ||
203 | /** | |
204 | * wakeup_source_register - Create wakeup source and add it to the list. | |
205 | * @name: Name of the wakeup source to register. | |
206 | */ | |
207 | struct wakeup_source *wakeup_source_register(const char *name) | |
208 | { | |
209 | struct wakeup_source *ws; | |
210 | ||
211 | ws = wakeup_source_create(name); | |
212 | if (ws) | |
213 | wakeup_source_add(ws); | |
214 | ||
215 | return ws; | |
216 | } | |
217 | EXPORT_SYMBOL_GPL(wakeup_source_register); | |
218 | ||
219 | /** | |
220 | * wakeup_source_unregister - Remove wakeup source from the list and remove it. | |
221 | * @ws: Wakeup source object to unregister. | |
222 | */ | |
223 | void wakeup_source_unregister(struct wakeup_source *ws) | |
224 | { | |
8671bbc1 RW |
225 | if (ws) { |
226 | wakeup_source_remove(ws); | |
227 | wakeup_source_destroy(ws); | |
228 | } | |
074037ec RW |
229 | } |
230 | EXPORT_SYMBOL_GPL(wakeup_source_unregister); | |
231 | ||
232 | /** | |
233 | * device_wakeup_attach - Attach a wakeup source object to a device object. | |
234 | * @dev: Device to handle. | |
235 | * @ws: Wakeup source object to attach to @dev. | |
236 | * | |
237 | * This causes @dev to be treated as a wakeup device. | |
238 | */ | |
239 | static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws) | |
240 | { | |
241 | spin_lock_irq(&dev->power.lock); | |
242 | if (dev->power.wakeup) { | |
243 | spin_unlock_irq(&dev->power.lock); | |
244 | return -EEXIST; | |
245 | } | |
246 | dev->power.wakeup = ws; | |
16669bef SG |
247 | if (dev->power.wakeirq) |
248 | device_wakeup_attach_irq(dev, dev->power.wakeirq); | |
074037ec RW |
249 | spin_unlock_irq(&dev->power.lock); |
250 | return 0; | |
251 | } | |
252 | ||
253 | /** | |
254 | * device_wakeup_enable - Enable given device to be a wakeup source. | |
255 | * @dev: Device to handle. | |
256 | * | |
257 | * Create a wakeup source object, register it and attach it to @dev. | |
258 | */ | |
259 | int device_wakeup_enable(struct device *dev) | |
260 | { | |
261 | struct wakeup_source *ws; | |
262 | int ret; | |
263 | ||
264 | if (!dev || !dev->power.can_wakeup) | |
265 | return -EINVAL; | |
266 | ||
0026cef0 UH |
267 | if (pm_suspend_target_state != PM_SUSPEND_ON) |
268 | dev_dbg(dev, "Suspicious %s() during system transition!\n", __func__); | |
269 | ||
074037ec RW |
270 | ws = wakeup_source_register(dev_name(dev)); |
271 | if (!ws) | |
272 | return -ENOMEM; | |
273 | ||
274 | ret = device_wakeup_attach(dev, ws); | |
275 | if (ret) | |
276 | wakeup_source_unregister(ws); | |
277 | ||
278 | return ret; | |
279 | } | |
280 | EXPORT_SYMBOL_GPL(device_wakeup_enable); | |
281 | ||
4990d4fe TL |
282 | /** |
283 | * device_wakeup_attach_irq - Attach a wakeirq to a wakeup source | |
284 | * @dev: Device to handle | |
285 | * @wakeirq: Device specific wakeirq entry | |
286 | * | |
287 | * Attach a device wakeirq to the wakeup source so the device | |
288 | * wake IRQ can be configured automatically for suspend and | |
289 | * resume. | |
6d3dab7d RW |
290 | * |
291 | * Call under the device's power.lock lock. | |
4990d4fe | 292 | */ |
7bf4e594 | 293 | void device_wakeup_attach_irq(struct device *dev, |
4990d4fe TL |
294 | struct wake_irq *wakeirq) |
295 | { | |
296 | struct wakeup_source *ws; | |
4990d4fe | 297 | |
4990d4fe | 298 | ws = dev->power.wakeup; |
7bf4e594 RW |
299 | if (!ws) |
300 | return; | |
4990d4fe | 301 | |
6d3dab7d | 302 | if (ws->wakeirq) |
7bf4e594 | 303 | dev_err(dev, "Leftover wakeup IRQ found, overriding\n"); |
4990d4fe TL |
304 | |
305 | ws->wakeirq = wakeirq; | |
4990d4fe TL |
306 | } |
307 | ||
308 | /** | |
309 | * device_wakeup_detach_irq - Detach a wakeirq from a wakeup source | |
310 | * @dev: Device to handle | |
311 | * | |
312 | * Removes a device wakeirq from the wakeup source. | |
6d3dab7d RW |
313 | * |
314 | * Call under the device's power.lock lock. | |
4990d4fe TL |
315 | */ |
316 | void device_wakeup_detach_irq(struct device *dev) | |
317 | { | |
318 | struct wakeup_source *ws; | |
319 | ||
4990d4fe | 320 | ws = dev->power.wakeup; |
6d3dab7d RW |
321 | if (ws) |
322 | ws->wakeirq = NULL; | |
4990d4fe TL |
323 | } |
324 | ||
325 | /** | |
326 | * device_wakeup_arm_wake_irqs(void) | |
327 | * | |
328 | * Itereates over the list of device wakeirqs to arm them. | |
329 | */ | |
330 | void device_wakeup_arm_wake_irqs(void) | |
331 | { | |
332 | struct wakeup_source *ws; | |
ea0212f4 | 333 | int srcuidx; |
4990d4fe | 334 | |
ea0212f4 | 335 | srcuidx = srcu_read_lock(&wakeup_srcu); |
4f48ec8a ME |
336 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) |
337 | dev_pm_arm_wake_irq(ws->wakeirq); | |
ea0212f4 | 338 | srcu_read_unlock(&wakeup_srcu, srcuidx); |
4990d4fe TL |
339 | } |
340 | ||
341 | /** | |
342 | * device_wakeup_disarm_wake_irqs(void) | |
343 | * | |
344 | * Itereates over the list of device wakeirqs to disarm them. | |
345 | */ | |
346 | void device_wakeup_disarm_wake_irqs(void) | |
347 | { | |
348 | struct wakeup_source *ws; | |
ea0212f4 | 349 | int srcuidx; |
4990d4fe | 350 | |
ea0212f4 | 351 | srcuidx = srcu_read_lock(&wakeup_srcu); |
4f48ec8a ME |
352 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) |
353 | dev_pm_disarm_wake_irq(ws->wakeirq); | |
ea0212f4 | 354 | srcu_read_unlock(&wakeup_srcu, srcuidx); |
4990d4fe TL |
355 | } |
356 | ||
074037ec RW |
357 | /** |
358 | * device_wakeup_detach - Detach a device's wakeup source object from it. | |
359 | * @dev: Device to detach the wakeup source object from. | |
360 | * | |
361 | * After it returns, @dev will not be treated as a wakeup device any more. | |
362 | */ | |
363 | static struct wakeup_source *device_wakeup_detach(struct device *dev) | |
364 | { | |
365 | struct wakeup_source *ws; | |
366 | ||
367 | spin_lock_irq(&dev->power.lock); | |
368 | ws = dev->power.wakeup; | |
369 | dev->power.wakeup = NULL; | |
370 | spin_unlock_irq(&dev->power.lock); | |
371 | return ws; | |
372 | } | |
373 | ||
374 | /** | |
375 | * device_wakeup_disable - Do not regard a device as a wakeup source any more. | |
376 | * @dev: Device to handle. | |
377 | * | |
378 | * Detach the @dev's wakeup source object from it, unregister this wakeup source | |
379 | * object and destroy it. | |
380 | */ | |
381 | int device_wakeup_disable(struct device *dev) | |
382 | { | |
383 | struct wakeup_source *ws; | |
384 | ||
385 | if (!dev || !dev->power.can_wakeup) | |
386 | return -EINVAL; | |
387 | ||
388 | ws = device_wakeup_detach(dev); | |
4f48ec8a | 389 | wakeup_source_unregister(ws); |
074037ec RW |
390 | return 0; |
391 | } | |
392 | EXPORT_SYMBOL_GPL(device_wakeup_disable); | |
393 | ||
cb8f51bd RW |
394 | /** |
395 | * device_set_wakeup_capable - Set/reset device wakeup capability flag. | |
396 | * @dev: Device to handle. | |
397 | * @capable: Whether or not @dev is capable of waking up the system from sleep. | |
398 | * | |
399 | * If @capable is set, set the @dev's power.can_wakeup flag and add its | |
400 | * wakeup-related attributes to sysfs. Otherwise, unset the @dev's | |
401 | * power.can_wakeup flag and remove its wakeup-related attributes from sysfs. | |
402 | * | |
403 | * This function may sleep and it can't be called from any context where | |
404 | * sleeping is not allowed. | |
405 | */ | |
406 | void device_set_wakeup_capable(struct device *dev, bool capable) | |
407 | { | |
408 | if (!!dev->power.can_wakeup == !!capable) | |
409 | return; | |
410 | ||
820b9b0c | 411 | dev->power.can_wakeup = capable; |
22110faf | 412 | if (device_is_registered(dev) && !list_empty(&dev->power.entry)) { |
cb8f51bd | 413 | if (capable) { |
820b9b0c RW |
414 | int ret = wakeup_sysfs_add(dev); |
415 | ||
416 | if (ret) | |
417 | dev_info(dev, "Wakeup sysfs attributes not added\n"); | |
cb8f51bd RW |
418 | } else { |
419 | wakeup_sysfs_remove(dev); | |
420 | } | |
421 | } | |
cb8f51bd RW |
422 | } |
423 | EXPORT_SYMBOL_GPL(device_set_wakeup_capable); | |
424 | ||
074037ec RW |
425 | /** |
426 | * device_init_wakeup - Device wakeup initialization. | |
427 | * @dev: Device to handle. | |
428 | * @enable: Whether or not to enable @dev as a wakeup device. | |
429 | * | |
430 | * By default, most devices should leave wakeup disabled. The exceptions are | |
431 | * devices that everyone expects to be wakeup sources: keyboards, power buttons, | |
8f88893c AS |
432 | * possibly network interfaces, etc. Also, devices that don't generate their |
433 | * own wakeup requests but merely forward requests from one bus to another | |
434 | * (like PCI bridges) should have wakeup enabled by default. | |
074037ec RW |
435 | */ |
436 | int device_init_wakeup(struct device *dev, bool enable) | |
437 | { | |
438 | int ret = 0; | |
439 | ||
0c5ff0ef ZR |
440 | if (!dev) |
441 | return -EINVAL; | |
442 | ||
074037ec RW |
443 | if (enable) { |
444 | device_set_wakeup_capable(dev, true); | |
445 | ret = device_wakeup_enable(dev); | |
446 | } else { | |
9dbc64a5 | 447 | device_wakeup_disable(dev); |
074037ec RW |
448 | device_set_wakeup_capable(dev, false); |
449 | } | |
450 | ||
451 | return ret; | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(device_init_wakeup); | |
454 | ||
455 | /** | |
456 | * device_set_wakeup_enable - Enable or disable a device to wake up the system. | |
457 | * @dev: Device to handle. | |
458 | */ | |
459 | int device_set_wakeup_enable(struct device *dev, bool enable) | |
460 | { | |
074037ec RW |
461 | return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev); |
462 | } | |
463 | EXPORT_SYMBOL_GPL(device_set_wakeup_enable); | |
4eb241e5 | 464 | |
b6ec9452 JQ |
465 | /** |
466 | * wakeup_source_not_registered - validate the given wakeup source. | |
467 | * @ws: Wakeup source to be validated. | |
468 | */ | |
469 | static bool wakeup_source_not_registered(struct wakeup_source *ws) | |
470 | { | |
471 | /* | |
472 | * Use timer struct to check if the given source is initialized | |
473 | * by wakeup_source_add. | |
474 | */ | |
841b86f3 | 475 | return ws->timer.function != pm_wakeup_timer_fn; |
b6ec9452 JQ |
476 | } |
477 | ||
c125e96f RW |
478 | /* |
479 | * The functions below use the observation that each wakeup event starts a | |
480 | * period in which the system should not be suspended. The moment this period | |
481 | * will end depends on how the wakeup event is going to be processed after being | |
482 | * detected and all of the possible cases can be divided into two distinct | |
483 | * groups. | |
484 | * | |
485 | * First, a wakeup event may be detected by the same functional unit that will | |
486 | * carry out the entire processing of it and possibly will pass it to user space | |
487 | * for further processing. In that case the functional unit that has detected | |
488 | * the event may later "close" the "no suspend" period associated with it | |
489 | * directly as soon as it has been dealt with. The pair of pm_stay_awake() and | |
490 | * pm_relax(), balanced with each other, is supposed to be used in such | |
491 | * situations. | |
492 | * | |
493 | * Second, a wakeup event may be detected by one functional unit and processed | |
494 | * by another one. In that case the unit that has detected it cannot really | |
495 | * "close" the "no suspend" period associated with it, unless it knows in | |
496 | * advance what's going to happen to the event during processing. This | |
497 | * knowledge, however, may not be available to it, so it can simply specify time | |
498 | * to wait before the system can be suspended and pass it as the second | |
499 | * argument of pm_wakeup_event(). | |
074037ec RW |
500 | * |
501 | * It is valid to call pm_relax() after pm_wakeup_event(), in which case the | |
502 | * "no suspend" period will be ended either by the pm_relax(), or by the timer | |
503 | * function executed when the timer expires, whichever comes first. | |
c125e96f RW |
504 | */ |
505 | ||
074037ec RW |
506 | /** |
507 | * wakup_source_activate - Mark given wakeup source as active. | |
508 | * @ws: Wakeup source to handle. | |
509 | * | |
510 | * Update the @ws' statistics and, if @ws has just been activated, notify the PM | |
511 | * core of the event by incrementing the counter of of wakeup events being | |
512 | * processed. | |
513 | */ | |
60d4553b | 514 | static void wakeup_source_activate(struct wakeup_source *ws) |
074037ec | 515 | { |
6791e36c AH |
516 | unsigned int cec; |
517 | ||
b6ec9452 JQ |
518 | if (WARN_ONCE(wakeup_source_not_registered(ws), |
519 | "unregistered wakeup source\n")) | |
520 | return; | |
521 | ||
074037ec RW |
522 | ws->active = true; |
523 | ws->active_count++; | |
074037ec | 524 | ws->last_time = ktime_get(); |
55850945 RW |
525 | if (ws->autosleep_enabled) |
526 | ws->start_prevent_time = ws->last_time; | |
074037ec | 527 | |
023d3779 | 528 | /* Increment the counter of events in progress. */ |
6791e36c AH |
529 | cec = atomic_inc_return(&combined_event_count); |
530 | ||
531 | trace_wakeup_source_activate(ws->name, cec); | |
074037ec RW |
532 | } |
533 | ||
30e3ce6d RW |
534 | /** |
535 | * wakeup_source_report_event - Report wakeup event using the given source. | |
536 | * @ws: Wakeup source to report the event for. | |
8a537ece | 537 | * @hard: If set, abort suspends in progress and wake up from suspend-to-idle. |
30e3ce6d | 538 | */ |
8a537ece | 539 | static void wakeup_source_report_event(struct wakeup_source *ws, bool hard) |
30e3ce6d RW |
540 | { |
541 | ws->event_count++; | |
542 | /* This is racy, but the counter is approximate anyway. */ | |
543 | if (events_check_enabled) | |
544 | ws->wakeup_count++; | |
545 | ||
546 | if (!ws->active) | |
60d4553b RW |
547 | wakeup_source_activate(ws); |
548 | ||
549 | if (hard) | |
550 | pm_system_wakeup(); | |
30e3ce6d RW |
551 | } |
552 | ||
074037ec RW |
553 | /** |
554 | * __pm_stay_awake - Notify the PM core of a wakeup event. | |
555 | * @ws: Wakeup source object associated with the source of the event. | |
556 | * | |
557 | * It is safe to call this function from interrupt context. | |
558 | */ | |
559 | void __pm_stay_awake(struct wakeup_source *ws) | |
560 | { | |
561 | unsigned long flags; | |
562 | ||
563 | if (!ws) | |
564 | return; | |
565 | ||
566 | spin_lock_irqsave(&ws->lock, flags); | |
4782e165 | 567 | |
8a537ece | 568 | wakeup_source_report_event(ws, false); |
4782e165 RW |
569 | del_timer(&ws->timer); |
570 | ws->timer_expires = 0; | |
571 | ||
074037ec RW |
572 | spin_unlock_irqrestore(&ws->lock, flags); |
573 | } | |
574 | EXPORT_SYMBOL_GPL(__pm_stay_awake); | |
575 | ||
c125e96f RW |
576 | /** |
577 | * pm_stay_awake - Notify the PM core that a wakeup event is being processed. | |
578 | * @dev: Device the wakeup event is related to. | |
579 | * | |
074037ec RW |
580 | * Notify the PM core of a wakeup event (signaled by @dev) by calling |
581 | * __pm_stay_awake for the @dev's wakeup source object. | |
c125e96f RW |
582 | * |
583 | * Call this function after detecting of a wakeup event if pm_relax() is going | |
584 | * to be called directly after processing the event (and possibly passing it to | |
585 | * user space for further processing). | |
c125e96f RW |
586 | */ |
587 | void pm_stay_awake(struct device *dev) | |
588 | { | |
589 | unsigned long flags; | |
590 | ||
074037ec RW |
591 | if (!dev) |
592 | return; | |
c125e96f | 593 | |
074037ec RW |
594 | spin_lock_irqsave(&dev->power.lock, flags); |
595 | __pm_stay_awake(dev->power.wakeup); | |
596 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
c125e96f | 597 | } |
074037ec | 598 | EXPORT_SYMBOL_GPL(pm_stay_awake); |
c125e96f | 599 | |
55850945 RW |
600 | #ifdef CONFIG_PM_AUTOSLEEP |
601 | static void update_prevent_sleep_time(struct wakeup_source *ws, ktime_t now) | |
602 | { | |
603 | ktime_t delta = ktime_sub(now, ws->start_prevent_time); | |
604 | ws->prevent_sleep_time = ktime_add(ws->prevent_sleep_time, delta); | |
605 | } | |
606 | #else | |
607 | static inline void update_prevent_sleep_time(struct wakeup_source *ws, | |
608 | ktime_t now) {} | |
609 | #endif | |
610 | ||
c125e96f | 611 | /** |
074037ec RW |
612 | * wakup_source_deactivate - Mark given wakeup source as inactive. |
613 | * @ws: Wakeup source to handle. | |
c125e96f | 614 | * |
074037ec RW |
615 | * Update the @ws' statistics and notify the PM core that the wakeup source has |
616 | * become inactive by decrementing the counter of wakeup events being processed | |
617 | * and incrementing the counter of registered wakeup events. | |
618 | */ | |
619 | static void wakeup_source_deactivate(struct wakeup_source *ws) | |
620 | { | |
6791e36c | 621 | unsigned int cnt, inpr, cec; |
074037ec RW |
622 | ktime_t duration; |
623 | ktime_t now; | |
624 | ||
625 | ws->relax_count++; | |
626 | /* | |
627 | * __pm_relax() may be called directly or from a timer function. | |
628 | * If it is called directly right after the timer function has been | |
629 | * started, but before the timer function calls __pm_relax(), it is | |
630 | * possible that __pm_stay_awake() will be called in the meantime and | |
631 | * will set ws->active. Then, ws->active may be cleared immediately | |
632 | * by the __pm_relax() called from the timer function, but in such a | |
633 | * case ws->relax_count will be different from ws->active_count. | |
634 | */ | |
635 | if (ws->relax_count != ws->active_count) { | |
636 | ws->relax_count--; | |
637 | return; | |
638 | } | |
639 | ||
640 | ws->active = false; | |
641 | ||
642 | now = ktime_get(); | |
643 | duration = ktime_sub(now, ws->last_time); | |
644 | ws->total_time = ktime_add(ws->total_time, duration); | |
645 | if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time)) | |
646 | ws->max_time = duration; | |
647 | ||
30e3ce6d | 648 | ws->last_time = now; |
074037ec | 649 | del_timer(&ws->timer); |
da863cdd | 650 | ws->timer_expires = 0; |
074037ec | 651 | |
55850945 RW |
652 | if (ws->autosleep_enabled) |
653 | update_prevent_sleep_time(ws, now); | |
654 | ||
074037ec | 655 | /* |
023d3779 RW |
656 | * Increment the counter of registered wakeup events and decrement the |
657 | * couter of wakeup events in progress simultaneously. | |
074037ec | 658 | */ |
6791e36c AH |
659 | cec = atomic_add_return(MAX_IN_PROGRESS, &combined_event_count); |
660 | trace_wakeup_source_deactivate(ws->name, cec); | |
60af1066 RW |
661 | |
662 | split_counters(&cnt, &inpr); | |
663 | if (!inpr && waitqueue_active(&wakeup_count_wait_queue)) | |
664 | wake_up(&wakeup_count_wait_queue); | |
074037ec RW |
665 | } |
666 | ||
667 | /** | |
668 | * __pm_relax - Notify the PM core that processing of a wakeup event has ended. | |
669 | * @ws: Wakeup source object associated with the source of the event. | |
c125e96f RW |
670 | * |
671 | * Call this function for wakeup events whose processing started with calling | |
074037ec | 672 | * __pm_stay_awake(). |
c125e96f RW |
673 | * |
674 | * It is safe to call it from interrupt context. | |
675 | */ | |
074037ec | 676 | void __pm_relax(struct wakeup_source *ws) |
c125e96f RW |
677 | { |
678 | unsigned long flags; | |
679 | ||
074037ec RW |
680 | if (!ws) |
681 | return; | |
682 | ||
683 | spin_lock_irqsave(&ws->lock, flags); | |
684 | if (ws->active) | |
685 | wakeup_source_deactivate(ws); | |
686 | spin_unlock_irqrestore(&ws->lock, flags); | |
687 | } | |
688 | EXPORT_SYMBOL_GPL(__pm_relax); | |
689 | ||
690 | /** | |
691 | * pm_relax - Notify the PM core that processing of a wakeup event has ended. | |
692 | * @dev: Device that signaled the event. | |
693 | * | |
694 | * Execute __pm_relax() for the @dev's wakeup source object. | |
695 | */ | |
696 | void pm_relax(struct device *dev) | |
697 | { | |
698 | unsigned long flags; | |
699 | ||
700 | if (!dev) | |
701 | return; | |
702 | ||
703 | spin_lock_irqsave(&dev->power.lock, flags); | |
704 | __pm_relax(dev->power.wakeup); | |
705 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
c125e96f | 706 | } |
074037ec | 707 | EXPORT_SYMBOL_GPL(pm_relax); |
c125e96f RW |
708 | |
709 | /** | |
4eb241e5 | 710 | * pm_wakeup_timer_fn - Delayed finalization of a wakeup event. |
074037ec | 711 | * @data: Address of the wakeup source object associated with the event source. |
c125e96f | 712 | * |
da863cdd RW |
713 | * Call wakeup_source_deactivate() for the wakeup source whose address is stored |
714 | * in @data if it is currently active and its timer has not been canceled and | |
715 | * the expiration time of the timer is not in future. | |
c125e96f | 716 | */ |
96428e98 | 717 | static void pm_wakeup_timer_fn(struct timer_list *t) |
074037ec | 718 | { |
96428e98 | 719 | struct wakeup_source *ws = from_timer(ws, t, timer); |
da863cdd RW |
720 | unsigned long flags; |
721 | ||
722 | spin_lock_irqsave(&ws->lock, flags); | |
723 | ||
724 | if (ws->active && ws->timer_expires | |
30e3ce6d | 725 | && time_after_eq(jiffies, ws->timer_expires)) { |
da863cdd | 726 | wakeup_source_deactivate(ws); |
30e3ce6d RW |
727 | ws->expire_count++; |
728 | } | |
da863cdd RW |
729 | |
730 | spin_unlock_irqrestore(&ws->lock, flags); | |
074037ec RW |
731 | } |
732 | ||
733 | /** | |
8a537ece | 734 | * pm_wakeup_ws_event - Notify the PM core of a wakeup event. |
074037ec RW |
735 | * @ws: Wakeup source object associated with the event source. |
736 | * @msec: Anticipated event processing time (in milliseconds). | |
8a537ece | 737 | * @hard: If set, abort suspends in progress and wake up from suspend-to-idle. |
074037ec RW |
738 | * |
739 | * Notify the PM core of a wakeup event whose source is @ws that will take | |
740 | * approximately @msec milliseconds to be processed by the kernel. If @ws is | |
741 | * not active, activate it. If @msec is nonzero, set up the @ws' timer to | |
742 | * execute pm_wakeup_timer_fn() in future. | |
743 | * | |
744 | * It is safe to call this function from interrupt context. | |
745 | */ | |
8a537ece | 746 | void pm_wakeup_ws_event(struct wakeup_source *ws, unsigned int msec, bool hard) |
c125e96f | 747 | { |
4eb241e5 | 748 | unsigned long flags; |
074037ec | 749 | unsigned long expires; |
c125e96f | 750 | |
074037ec RW |
751 | if (!ws) |
752 | return; | |
753 | ||
754 | spin_lock_irqsave(&ws->lock, flags); | |
755 | ||
8a537ece | 756 | wakeup_source_report_event(ws, hard); |
074037ec RW |
757 | |
758 | if (!msec) { | |
759 | wakeup_source_deactivate(ws); | |
760 | goto unlock; | |
4eb241e5 | 761 | } |
074037ec RW |
762 | |
763 | expires = jiffies + msecs_to_jiffies(msec); | |
764 | if (!expires) | |
765 | expires = 1; | |
766 | ||
4782e165 | 767 | if (!ws->timer_expires || time_after(expires, ws->timer_expires)) { |
074037ec RW |
768 | mod_timer(&ws->timer, expires); |
769 | ws->timer_expires = expires; | |
770 | } | |
771 | ||
772 | unlock: | |
773 | spin_unlock_irqrestore(&ws->lock, flags); | |
c125e96f | 774 | } |
8a537ece | 775 | EXPORT_SYMBOL_GPL(pm_wakeup_ws_event); |
c125e96f RW |
776 | |
777 | /** | |
d1c6b41b | 778 | * pm_wakeup_dev_event - Notify the PM core of a wakeup event. |
c125e96f RW |
779 | * @dev: Device the wakeup event is related to. |
780 | * @msec: Anticipated event processing time (in milliseconds). | |
8a537ece | 781 | * @hard: If set, abort suspends in progress and wake up from suspend-to-idle. |
c125e96f | 782 | * |
8a537ece | 783 | * Call pm_wakeup_ws_event() for the @dev's wakeup source object. |
c125e96f | 784 | */ |
8a537ece | 785 | void pm_wakeup_dev_event(struct device *dev, unsigned int msec, bool hard) |
c125e96f RW |
786 | { |
787 | unsigned long flags; | |
c125e96f | 788 | |
074037ec RW |
789 | if (!dev) |
790 | return; | |
c125e96f | 791 | |
074037ec | 792 | spin_lock_irqsave(&dev->power.lock, flags); |
8a537ece | 793 | pm_wakeup_ws_event(dev->power.wakeup, msec, hard); |
074037ec RW |
794 | spin_unlock_irqrestore(&dev->power.lock, flags); |
795 | } | |
8a537ece | 796 | EXPORT_SYMBOL_GPL(pm_wakeup_dev_event); |
4eb241e5 | 797 | |
bb177fed | 798 | void pm_print_active_wakeup_sources(void) |
a938da06 TP |
799 | { |
800 | struct wakeup_source *ws; | |
ea0212f4 | 801 | int srcuidx, active = 0; |
a938da06 TP |
802 | struct wakeup_source *last_activity_ws = NULL; |
803 | ||
ea0212f4 | 804 | srcuidx = srcu_read_lock(&wakeup_srcu); |
a938da06 TP |
805 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) { |
806 | if (ws->active) { | |
9320f95c | 807 | pr_debug("active wakeup source: %s\n", ws->name); |
a938da06 TP |
808 | active = 1; |
809 | } else if (!active && | |
810 | (!last_activity_ws || | |
811 | ktime_to_ns(ws->last_time) > | |
812 | ktime_to_ns(last_activity_ws->last_time))) { | |
813 | last_activity_ws = ws; | |
814 | } | |
815 | } | |
816 | ||
817 | if (!active && last_activity_ws) | |
9320f95c | 818 | pr_debug("last active wakeup source: %s\n", |
a938da06 | 819 | last_activity_ws->name); |
ea0212f4 | 820 | srcu_read_unlock(&wakeup_srcu, srcuidx); |
a938da06 | 821 | } |
bb177fed | 822 | EXPORT_SYMBOL_GPL(pm_print_active_wakeup_sources); |
a938da06 | 823 | |
c125e96f | 824 | /** |
a2867e08 | 825 | * pm_wakeup_pending - Check if power transition in progress should be aborted. |
c125e96f RW |
826 | * |
827 | * Compare the current number of registered wakeup events with its preserved | |
a2867e08 RW |
828 | * value from the past and return true if new wakeup events have been registered |
829 | * since the old value was stored. Also return true if the current number of | |
830 | * wakeup events being processed is different from zero. | |
c125e96f | 831 | */ |
a2867e08 | 832 | bool pm_wakeup_pending(void) |
c125e96f RW |
833 | { |
834 | unsigned long flags; | |
a2867e08 | 835 | bool ret = false; |
c125e96f | 836 | |
bccaadab | 837 | raw_spin_lock_irqsave(&events_lock, flags); |
c125e96f | 838 | if (events_check_enabled) { |
023d3779 RW |
839 | unsigned int cnt, inpr; |
840 | ||
841 | split_counters(&cnt, &inpr); | |
842 | ret = (cnt != saved_count || inpr > 0); | |
a2867e08 | 843 | events_check_enabled = !ret; |
c125e96f | 844 | } |
bccaadab | 845 | raw_spin_unlock_irqrestore(&events_lock, flags); |
a938da06 | 846 | |
9350de06 | 847 | if (ret) { |
7a5bd127 | 848 | pr_debug("Wakeup pending, aborting suspend\n"); |
bb177fed | 849 | pm_print_active_wakeup_sources(); |
9350de06 | 850 | } |
a938da06 | 851 | |
33e4f80e | 852 | return ret || atomic_read(&pm_abort_suspend) > 0; |
068765ba RW |
853 | } |
854 | ||
855 | void pm_system_wakeup(void) | |
856 | { | |
33e4f80e | 857 | atomic_inc(&pm_abort_suspend); |
f02f4f9d | 858 | s2idle_wake(); |
068765ba | 859 | } |
432ec92b | 860 | EXPORT_SYMBOL_GPL(pm_system_wakeup); |
068765ba | 861 | |
33e4f80e RW |
862 | void pm_system_cancel_wakeup(void) |
863 | { | |
864 | atomic_dec(&pm_abort_suspend); | |
865 | } | |
866 | ||
867 | void pm_wakeup_clear(bool reset) | |
068765ba | 868 | { |
a6f5f0dd | 869 | pm_wakeup_irq = 0; |
33e4f80e RW |
870 | if (reset) |
871 | atomic_set(&pm_abort_suspend, 0); | |
a6f5f0dd AY |
872 | } |
873 | ||
874 | void pm_system_irq_wakeup(unsigned int irq_number) | |
875 | { | |
876 | if (pm_wakeup_irq == 0) { | |
877 | pm_wakeup_irq = irq_number; | |
878 | pm_system_wakeup(); | |
879 | } | |
c125e96f RW |
880 | } |
881 | ||
882 | /** | |
883 | * pm_get_wakeup_count - Read the number of registered wakeup events. | |
884 | * @count: Address to store the value at. | |
7483b4a4 | 885 | * @block: Whether or not to block. |
c125e96f | 886 | * |
7483b4a4 RW |
887 | * Store the number of registered wakeup events at the address in @count. If |
888 | * @block is set, block until the current number of wakeup events being | |
889 | * processed is zero. | |
c125e96f | 890 | * |
7483b4a4 RW |
891 | * Return 'false' if the current number of wakeup events being processed is |
892 | * nonzero. Otherwise return 'true'. | |
c125e96f | 893 | */ |
7483b4a4 | 894 | bool pm_get_wakeup_count(unsigned int *count, bool block) |
c125e96f | 895 | { |
023d3779 | 896 | unsigned int cnt, inpr; |
c125e96f | 897 | |
7483b4a4 RW |
898 | if (block) { |
899 | DEFINE_WAIT(wait); | |
900 | ||
901 | for (;;) { | |
902 | prepare_to_wait(&wakeup_count_wait_queue, &wait, | |
903 | TASK_INTERRUPTIBLE); | |
904 | split_counters(&cnt, &inpr); | |
905 | if (inpr == 0 || signal_pending(current)) | |
906 | break; | |
9320f95c | 907 | pm_print_active_wakeup_sources(); |
7483b4a4 RW |
908 | schedule(); |
909 | } | |
910 | finish_wait(&wakeup_count_wait_queue, &wait); | |
c125e96f | 911 | } |
074037ec | 912 | |
023d3779 RW |
913 | split_counters(&cnt, &inpr); |
914 | *count = cnt; | |
915 | return !inpr; | |
c125e96f RW |
916 | } |
917 | ||
918 | /** | |
919 | * pm_save_wakeup_count - Save the current number of registered wakeup events. | |
920 | * @count: Value to compare with the current number of registered wakeup events. | |
921 | * | |
922 | * If @count is equal to the current number of registered wakeup events and the | |
923 | * current number of wakeup events being processed is zero, store @count as the | |
378eef99 RW |
924 | * old number of registered wakeup events for pm_check_wakeup_events(), enable |
925 | * wakeup events detection and return 'true'. Otherwise disable wakeup events | |
926 | * detection and return 'false'. | |
c125e96f | 927 | */ |
074037ec | 928 | bool pm_save_wakeup_count(unsigned int count) |
c125e96f | 929 | { |
023d3779 | 930 | unsigned int cnt, inpr; |
49550709 | 931 | unsigned long flags; |
c125e96f | 932 | |
378eef99 | 933 | events_check_enabled = false; |
bccaadab | 934 | raw_spin_lock_irqsave(&events_lock, flags); |
023d3779 RW |
935 | split_counters(&cnt, &inpr); |
936 | if (cnt == count && inpr == 0) { | |
074037ec | 937 | saved_count = count; |
c125e96f | 938 | events_check_enabled = true; |
c125e96f | 939 | } |
bccaadab | 940 | raw_spin_unlock_irqrestore(&events_lock, flags); |
378eef99 | 941 | return events_check_enabled; |
c125e96f | 942 | } |
9c034392 | 943 | |
55850945 RW |
944 | #ifdef CONFIG_PM_AUTOSLEEP |
945 | /** | |
946 | * pm_wakep_autosleep_enabled - Modify autosleep_enabled for all wakeup sources. | |
947 | * @enabled: Whether to set or to clear the autosleep_enabled flags. | |
948 | */ | |
949 | void pm_wakep_autosleep_enabled(bool set) | |
950 | { | |
951 | struct wakeup_source *ws; | |
952 | ktime_t now = ktime_get(); | |
ea0212f4 | 953 | int srcuidx; |
55850945 | 954 | |
ea0212f4 | 955 | srcuidx = srcu_read_lock(&wakeup_srcu); |
55850945 RW |
956 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) { |
957 | spin_lock_irq(&ws->lock); | |
958 | if (ws->autosleep_enabled != set) { | |
959 | ws->autosleep_enabled = set; | |
960 | if (ws->active) { | |
961 | if (set) | |
962 | ws->start_prevent_time = now; | |
963 | else | |
964 | update_prevent_sleep_time(ws, now); | |
965 | } | |
966 | } | |
967 | spin_unlock_irq(&ws->lock); | |
968 | } | |
ea0212f4 | 969 | srcu_read_unlock(&wakeup_srcu, srcuidx); |
55850945 RW |
970 | } |
971 | #endif /* CONFIG_PM_AUTOSLEEP */ | |
972 | ||
9c034392 RW |
973 | static struct dentry *wakeup_sources_stats_dentry; |
974 | ||
975 | /** | |
976 | * print_wakeup_source_stats - Print wakeup source statistics information. | |
977 | * @m: seq_file to print the statistics into. | |
978 | * @ws: Wakeup source object to print the statistics for. | |
979 | */ | |
980 | static int print_wakeup_source_stats(struct seq_file *m, | |
981 | struct wakeup_source *ws) | |
982 | { | |
983 | unsigned long flags; | |
984 | ktime_t total_time; | |
985 | ktime_t max_time; | |
986 | unsigned long active_count; | |
987 | ktime_t active_time; | |
55850945 | 988 | ktime_t prevent_sleep_time; |
9c034392 RW |
989 | |
990 | spin_lock_irqsave(&ws->lock, flags); | |
991 | ||
992 | total_time = ws->total_time; | |
993 | max_time = ws->max_time; | |
55850945 | 994 | prevent_sleep_time = ws->prevent_sleep_time; |
9c034392 RW |
995 | active_count = ws->active_count; |
996 | if (ws->active) { | |
55850945 RW |
997 | ktime_t now = ktime_get(); |
998 | ||
999 | active_time = ktime_sub(now, ws->last_time); | |
9c034392 | 1000 | total_time = ktime_add(total_time, active_time); |
2456e855 | 1001 | if (active_time > max_time) |
9c034392 | 1002 | max_time = active_time; |
55850945 RW |
1003 | |
1004 | if (ws->autosleep_enabled) | |
1005 | prevent_sleep_time = ktime_add(prevent_sleep_time, | |
1006 | ktime_sub(now, ws->start_prevent_time)); | |
9c034392 | 1007 | } else { |
8b0e1953 | 1008 | active_time = 0; |
9c034392 RW |
1009 | } |
1010 | ||
9f6a240e JP |
1011 | seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t%lu\t\t%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n", |
1012 | ws->name, active_count, ws->event_count, | |
1013 | ws->wakeup_count, ws->expire_count, | |
1014 | ktime_to_ms(active_time), ktime_to_ms(total_time), | |
1015 | ktime_to_ms(max_time), ktime_to_ms(ws->last_time), | |
1016 | ktime_to_ms(prevent_sleep_time)); | |
9c034392 RW |
1017 | |
1018 | spin_unlock_irqrestore(&ws->lock, flags); | |
1019 | ||
9f6a240e | 1020 | return 0; |
9c034392 RW |
1021 | } |
1022 | ||
00ee22c2 MG |
1023 | static void *wakeup_sources_stats_seq_start(struct seq_file *m, |
1024 | loff_t *pos) | |
9c034392 RW |
1025 | { |
1026 | struct wakeup_source *ws; | |
00ee22c2 MG |
1027 | loff_t n = *pos; |
1028 | int *srcuidx = m->private; | |
9c034392 | 1029 | |
00ee22c2 MG |
1030 | if (n == 0) { |
1031 | seq_puts(m, "name\t\tactive_count\tevent_count\twakeup_count\t" | |
1032 | "expire_count\tactive_since\ttotal_time\tmax_time\t" | |
1033 | "last_change\tprevent_suspend_time\n"); | |
1034 | } | |
9c034392 | 1035 | |
00ee22c2 MG |
1036 | *srcuidx = srcu_read_lock(&wakeup_srcu); |
1037 | list_for_each_entry_rcu(ws, &wakeup_sources, entry) { | |
1038 | if (n-- <= 0) | |
1039 | return ws; | |
1040 | } | |
1041 | ||
1042 | return NULL; | |
1043 | } | |
1044 | ||
1045 | static void *wakeup_sources_stats_seq_next(struct seq_file *m, | |
1046 | void *v, loff_t *pos) | |
1047 | { | |
1048 | struct wakeup_source *ws = v; | |
1049 | struct wakeup_source *next_ws = NULL; | |
1050 | ||
1051 | ++(*pos); | |
9c034392 | 1052 | |
00ee22c2 MG |
1053 | list_for_each_entry_continue_rcu(ws, &wakeup_sources, entry) { |
1054 | next_ws = ws; | |
1055 | break; | |
1056 | } | |
1057 | ||
1058 | return next_ws; | |
1059 | } | |
1060 | ||
1061 | static void wakeup_sources_stats_seq_stop(struct seq_file *m, void *v) | |
1062 | { | |
1063 | int *srcuidx = m->private; | |
1064 | ||
1065 | srcu_read_unlock(&wakeup_srcu, *srcuidx); | |
1066 | } | |
1067 | ||
1068 | /** | |
1069 | * wakeup_sources_stats_seq_show - Print wakeup sources statistics information. | |
1070 | * @m: seq_file to print the statistics into. | |
1071 | * @v: wakeup_source of each iteration | |
1072 | */ | |
1073 | static int wakeup_sources_stats_seq_show(struct seq_file *m, void *v) | |
1074 | { | |
1075 | struct wakeup_source *ws = v; | |
1076 | ||
1077 | print_wakeup_source_stats(m, ws); | |
7f436055 | 1078 | |
9c034392 RW |
1079 | return 0; |
1080 | } | |
1081 | ||
00ee22c2 MG |
1082 | static const struct seq_operations wakeup_sources_stats_seq_ops = { |
1083 | .start = wakeup_sources_stats_seq_start, | |
1084 | .next = wakeup_sources_stats_seq_next, | |
1085 | .stop = wakeup_sources_stats_seq_stop, | |
1086 | .show = wakeup_sources_stats_seq_show, | |
1087 | }; | |
1088 | ||
9c034392 RW |
1089 | static int wakeup_sources_stats_open(struct inode *inode, struct file *file) |
1090 | { | |
00ee22c2 | 1091 | return seq_open_private(file, &wakeup_sources_stats_seq_ops, sizeof(int)); |
9c034392 RW |
1092 | } |
1093 | ||
1094 | static const struct file_operations wakeup_sources_stats_fops = { | |
1095 | .owner = THIS_MODULE, | |
1096 | .open = wakeup_sources_stats_open, | |
1097 | .read = seq_read, | |
1098 | .llseek = seq_lseek, | |
00ee22c2 | 1099 | .release = seq_release_private, |
9c034392 RW |
1100 | }; |
1101 | ||
1102 | static int __init wakeup_sources_debugfs_init(void) | |
1103 | { | |
1104 | wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources", | |
1105 | S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops); | |
1106 | return 0; | |
1107 | } | |
1108 | ||
1109 | postcore_initcall(wakeup_sources_debugfs_init); |