wakeup.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. #include <linux/device.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <linux/capability.h>
  12. #include <linux/suspend.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/debugfs.h>
  15. #include "power.h"
  16. #define TIMEOUT 100
  17. /*
  18. * If set, the suspend/hibernate code will abort transitions to a sleep state
  19. * if wakeup events are registered during or immediately before the transition.
  20. */
  21. bool events_check_enabled;
  22. /* The counter of registered wakeup events. */
  23. static atomic_t event_count = ATOMIC_INIT(0);
  24. /* A preserved old value of event_count. */
  25. static unsigned int saved_count;
  26. /* The counter of wakeup events being processed. */
  27. static atomic_t events_in_progress = ATOMIC_INIT(0);
  28. static DEFINE_SPINLOCK(events_lock);
  29. static void pm_wakeup_timer_fn(unsigned long data);
  30. static LIST_HEAD(wakeup_sources);
  31. /**
  32. * wakeup_source_create - Create a struct wakeup_source object.
  33. * @name: Name of the new wakeup source.
  34. */
  35. struct wakeup_source *wakeup_source_create(const char *name)
  36. {
  37. struct wakeup_source *ws;
  38. ws = kzalloc(sizeof(*ws), GFP_KERNEL);
  39. if (!ws)
  40. return NULL;
  41. spin_lock_init(&ws->lock);
  42. if (name)
  43. ws->name = kstrdup(name, GFP_KERNEL);
  44. return ws;
  45. }
  46. EXPORT_SYMBOL_GPL(wakeup_source_create);
  47. /**
  48. * wakeup_source_destroy - Destroy a struct wakeup_source object.
  49. * @ws: Wakeup source to destroy.
  50. */
  51. void wakeup_source_destroy(struct wakeup_source *ws)
  52. {
  53. if (!ws)
  54. return;
  55. spin_lock_irq(&ws->lock);
  56. while (ws->active) {
  57. spin_unlock_irq(&ws->lock);
  58. schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
  59. spin_lock_irq(&ws->lock);
  60. }
  61. spin_unlock_irq(&ws->lock);
  62. kfree(ws->name);
  63. kfree(ws);
  64. }
  65. EXPORT_SYMBOL_GPL(wakeup_source_destroy);
  66. /**
  67. * wakeup_source_add - Add given object to the list of wakeup sources.
  68. * @ws: Wakeup source object to add to the list.
  69. */
  70. void wakeup_source_add(struct wakeup_source *ws)
  71. {
  72. if (WARN_ON(!ws))
  73. return;
  74. setup_timer(&ws->timer, pm_wakeup_timer_fn, (unsigned long)ws);
  75. ws->active = false;
  76. spin_lock_irq(&events_lock);
  77. list_add_rcu(&ws->entry, &wakeup_sources);
  78. spin_unlock_irq(&events_lock);
  79. synchronize_rcu();
  80. }
  81. EXPORT_SYMBOL_GPL(wakeup_source_add);
  82. /**
  83. * wakeup_source_remove - Remove given object from the wakeup sources list.
  84. * @ws: Wakeup source object to remove from the list.
  85. */
  86. void wakeup_source_remove(struct wakeup_source *ws)
  87. {
  88. if (WARN_ON(!ws))
  89. return;
  90. spin_lock_irq(&events_lock);
  91. list_del_rcu(&ws->entry);
  92. spin_unlock_irq(&events_lock);
  93. synchronize_rcu();
  94. }
  95. EXPORT_SYMBOL_GPL(wakeup_source_remove);
  96. /**
  97. * wakeup_source_register - Create wakeup source and add it to the list.
  98. * @name: Name of the wakeup source to register.
  99. */
  100. struct wakeup_source *wakeup_source_register(const char *name)
  101. {
  102. struct wakeup_source *ws;
  103. ws = wakeup_source_create(name);
  104. if (ws)
  105. wakeup_source_add(ws);
  106. return ws;
  107. }
  108. EXPORT_SYMBOL_GPL(wakeup_source_register);
  109. /**
  110. * wakeup_source_unregister - Remove wakeup source from the list and remove it.
  111. * @ws: Wakeup source object to unregister.
  112. */
  113. void wakeup_source_unregister(struct wakeup_source *ws)
  114. {
  115. wakeup_source_remove(ws);
  116. wakeup_source_destroy(ws);
  117. }
  118. EXPORT_SYMBOL_GPL(wakeup_source_unregister);
  119. /**
  120. * device_wakeup_attach - Attach a wakeup source object to a device object.
  121. * @dev: Device to handle.
  122. * @ws: Wakeup source object to attach to @dev.
  123. *
  124. * This causes @dev to be treated as a wakeup device.
  125. */
  126. static int device_wakeup_attach(struct device *dev, struct wakeup_source *ws)
  127. {
  128. spin_lock_irq(&dev->power.lock);
  129. if (dev->power.wakeup) {
  130. spin_unlock_irq(&dev->power.lock);
  131. return -EEXIST;
  132. }
  133. dev->power.wakeup = ws;
  134. spin_unlock_irq(&dev->power.lock);
  135. return 0;
  136. }
  137. /**
  138. * device_wakeup_enable - Enable given device to be a wakeup source.
  139. * @dev: Device to handle.
  140. *
  141. * Create a wakeup source object, register it and attach it to @dev.
  142. */
  143. int device_wakeup_enable(struct device *dev)
  144. {
  145. struct wakeup_source *ws;
  146. int ret;
  147. if (!dev || !dev->power.can_wakeup)
  148. return -EINVAL;
  149. ws = wakeup_source_register(dev_name(dev));
  150. if (!ws)
  151. return -ENOMEM;
  152. ret = device_wakeup_attach(dev, ws);
  153. if (ret)
  154. wakeup_source_unregister(ws);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL_GPL(device_wakeup_enable);
  158. /**
  159. * device_wakeup_detach - Detach a device's wakeup source object from it.
  160. * @dev: Device to detach the wakeup source object from.
  161. *
  162. * After it returns, @dev will not be treated as a wakeup device any more.
  163. */
  164. static struct wakeup_source *device_wakeup_detach(struct device *dev)
  165. {
  166. struct wakeup_source *ws;
  167. spin_lock_irq(&dev->power.lock);
  168. ws = dev->power.wakeup;
  169. dev->power.wakeup = NULL;
  170. spin_unlock_irq(&dev->power.lock);
  171. return ws;
  172. }
  173. /**
  174. * device_wakeup_disable - Do not regard a device as a wakeup source any more.
  175. * @dev: Device to handle.
  176. *
  177. * Detach the @dev's wakeup source object from it, unregister this wakeup source
  178. * object and destroy it.
  179. */
  180. int device_wakeup_disable(struct device *dev)
  181. {
  182. struct wakeup_source *ws;
  183. if (!dev || !dev->power.can_wakeup)
  184. return -EINVAL;
  185. ws = device_wakeup_detach(dev);
  186. if (ws)
  187. wakeup_source_unregister(ws);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(device_wakeup_disable);
  191. /**
  192. * device_init_wakeup - Device wakeup initialization.
  193. * @dev: Device to handle.
  194. * @enable: Whether or not to enable @dev as a wakeup device.
  195. *
  196. * By default, most devices should leave wakeup disabled. The exceptions are
  197. * devices that everyone expects to be wakeup sources: keyboards, power buttons,
  198. * possibly network interfaces, etc.
  199. */
  200. int device_init_wakeup(struct device *dev, bool enable)
  201. {
  202. int ret = 0;
  203. if (enable) {
  204. device_set_wakeup_capable(dev, true);
  205. ret = device_wakeup_enable(dev);
  206. } else {
  207. device_set_wakeup_capable(dev, false);
  208. }
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(device_init_wakeup);
  212. /**
  213. * device_set_wakeup_enable - Enable or disable a device to wake up the system.
  214. * @dev: Device to handle.
  215. */
  216. int device_set_wakeup_enable(struct device *dev, bool enable)
  217. {
  218. if (!dev || !dev->power.can_wakeup)
  219. return -EINVAL;
  220. return enable ? device_wakeup_enable(dev) : device_wakeup_disable(dev);
  221. }
  222. EXPORT_SYMBOL_GPL(device_set_wakeup_enable);
  223. /*
  224. * The functions below use the observation that each wakeup event starts a
  225. * period in which the system should not be suspended. The moment this period
  226. * will end depends on how the wakeup event is going to be processed after being
  227. * detected and all of the possible cases can be divided into two distinct
  228. * groups.
  229. *
  230. * First, a wakeup event may be detected by the same functional unit that will
  231. * carry out the entire processing of it and possibly will pass it to user space
  232. * for further processing. In that case the functional unit that has detected
  233. * the event may later "close" the "no suspend" period associated with it
  234. * directly as soon as it has been dealt with. The pair of pm_stay_awake() and
  235. * pm_relax(), balanced with each other, is supposed to be used in such
  236. * situations.
  237. *
  238. * Second, a wakeup event may be detected by one functional unit and processed
  239. * by another one. In that case the unit that has detected it cannot really
  240. * "close" the "no suspend" period associated with it, unless it knows in
  241. * advance what's going to happen to the event during processing. This
  242. * knowledge, however, may not be available to it, so it can simply specify time
  243. * to wait before the system can be suspended and pass it as the second
  244. * argument of pm_wakeup_event().
  245. *
  246. * It is valid to call pm_relax() after pm_wakeup_event(), in which case the
  247. * "no suspend" period will be ended either by the pm_relax(), or by the timer
  248. * function executed when the timer expires, whichever comes first.
  249. */
  250. /**
  251. * wakup_source_activate - Mark given wakeup source as active.
  252. * @ws: Wakeup source to handle.
  253. *
  254. * Update the @ws' statistics and, if @ws has just been activated, notify the PM
  255. * core of the event by incrementing the counter of of wakeup events being
  256. * processed.
  257. */
  258. static void wakeup_source_activate(struct wakeup_source *ws)
  259. {
  260. ws->active = true;
  261. ws->active_count++;
  262. ws->timer_expires = jiffies;
  263. ws->last_time = ktime_get();
  264. atomic_inc(&events_in_progress);
  265. }
  266. /**
  267. * __pm_stay_awake - Notify the PM core of a wakeup event.
  268. * @ws: Wakeup source object associated with the source of the event.
  269. *
  270. * It is safe to call this function from interrupt context.
  271. */
  272. void __pm_stay_awake(struct wakeup_source *ws)
  273. {
  274. unsigned long flags;
  275. if (!ws)
  276. return;
  277. spin_lock_irqsave(&ws->lock, flags);
  278. ws->event_count++;
  279. if (!ws->active)
  280. wakeup_source_activate(ws);
  281. spin_unlock_irqrestore(&ws->lock, flags);
  282. }
  283. EXPORT_SYMBOL_GPL(__pm_stay_awake);
  284. /**
  285. * pm_stay_awake - Notify the PM core that a wakeup event is being processed.
  286. * @dev: Device the wakeup event is related to.
  287. *
  288. * Notify the PM core of a wakeup event (signaled by @dev) by calling
  289. * __pm_stay_awake for the @dev's wakeup source object.
  290. *
  291. * Call this function after detecting of a wakeup event if pm_relax() is going
  292. * to be called directly after processing the event (and possibly passing it to
  293. * user space for further processing).
  294. */
  295. void pm_stay_awake(struct device *dev)
  296. {
  297. unsigned long flags;
  298. if (!dev)
  299. return;
  300. spin_lock_irqsave(&dev->power.lock, flags);
  301. __pm_stay_awake(dev->power.wakeup);
  302. spin_unlock_irqrestore(&dev->power.lock, flags);
  303. }
  304. EXPORT_SYMBOL_GPL(pm_stay_awake);
  305. /**
  306. * wakup_source_deactivate - Mark given wakeup source as inactive.
  307. * @ws: Wakeup source to handle.
  308. *
  309. * Update the @ws' statistics and notify the PM core that the wakeup source has
  310. * become inactive by decrementing the counter of wakeup events being processed
  311. * and incrementing the counter of registered wakeup events.
  312. */
  313. static void wakeup_source_deactivate(struct wakeup_source *ws)
  314. {
  315. ktime_t duration;
  316. ktime_t now;
  317. ws->relax_count++;
  318. /*
  319. * __pm_relax() may be called directly or from a timer function.
  320. * If it is called directly right after the timer function has been
  321. * started, but before the timer function calls __pm_relax(), it is
  322. * possible that __pm_stay_awake() will be called in the meantime and
  323. * will set ws->active. Then, ws->active may be cleared immediately
  324. * by the __pm_relax() called from the timer function, but in such a
  325. * case ws->relax_count will be different from ws->active_count.
  326. */
  327. if (ws->relax_count != ws->active_count) {
  328. ws->relax_count--;
  329. return;
  330. }
  331. ws->active = false;
  332. now = ktime_get();
  333. duration = ktime_sub(now, ws->last_time);
  334. ws->total_time = ktime_add(ws->total_time, duration);
  335. if (ktime_to_ns(duration) > ktime_to_ns(ws->max_time))
  336. ws->max_time = duration;
  337. del_timer(&ws->timer);
  338. /*
  339. * event_count has to be incremented before events_in_progress is
  340. * modified, so that the callers of pm_check_wakeup_events() and
  341. * pm_save_wakeup_count() don't see the old value of event_count and
  342. * events_in_progress equal to zero at the same time.
  343. */
  344. atomic_inc(&event_count);
  345. smp_mb__before_atomic_dec();
  346. atomic_dec(&events_in_progress);
  347. }
  348. /**
  349. * __pm_relax - Notify the PM core that processing of a wakeup event has ended.
  350. * @ws: Wakeup source object associated with the source of the event.
  351. *
  352. * Call this function for wakeup events whose processing started with calling
  353. * __pm_stay_awake().
  354. *
  355. * It is safe to call it from interrupt context.
  356. */
  357. void __pm_relax(struct wakeup_source *ws)
  358. {
  359. unsigned long flags;
  360. if (!ws)
  361. return;
  362. spin_lock_irqsave(&ws->lock, flags);
  363. if (ws->active)
  364. wakeup_source_deactivate(ws);
  365. spin_unlock_irqrestore(&ws->lock, flags);
  366. }
  367. EXPORT_SYMBOL_GPL(__pm_relax);
  368. /**
  369. * pm_relax - Notify the PM core that processing of a wakeup event has ended.
  370. * @dev: Device that signaled the event.
  371. *
  372. * Execute __pm_relax() for the @dev's wakeup source object.
  373. */
  374. void pm_relax(struct device *dev)
  375. {
  376. unsigned long flags;
  377. if (!dev)
  378. return;
  379. spin_lock_irqsave(&dev->power.lock, flags);
  380. __pm_relax(dev->power.wakeup);
  381. spin_unlock_irqrestore(&dev->power.lock, flags);
  382. }
  383. EXPORT_SYMBOL_GPL(pm_relax);
  384. /**
  385. * pm_wakeup_timer_fn - Delayed finalization of a wakeup event.
  386. * @data: Address of the wakeup source object associated with the event source.
  387. *
  388. * Call __pm_relax() for the wakeup source whose address is stored in @data.
  389. */
  390. static void pm_wakeup_timer_fn(unsigned long data)
  391. {
  392. __pm_relax((struct wakeup_source *)data);
  393. }
  394. /**
  395. * __pm_wakeup_event - Notify the PM core of a wakeup event.
  396. * @ws: Wakeup source object associated with the event source.
  397. * @msec: Anticipated event processing time (in milliseconds).
  398. *
  399. * Notify the PM core of a wakeup event whose source is @ws that will take
  400. * approximately @msec milliseconds to be processed by the kernel. If @ws is
  401. * not active, activate it. If @msec is nonzero, set up the @ws' timer to
  402. * execute pm_wakeup_timer_fn() in future.
  403. *
  404. * It is safe to call this function from interrupt context.
  405. */
  406. void __pm_wakeup_event(struct wakeup_source *ws, unsigned int msec)
  407. {
  408. unsigned long flags;
  409. unsigned long expires;
  410. if (!ws)
  411. return;
  412. spin_lock_irqsave(&ws->lock, flags);
  413. ws->event_count++;
  414. if (!ws->active)
  415. wakeup_source_activate(ws);
  416. if (!msec) {
  417. wakeup_source_deactivate(ws);
  418. goto unlock;
  419. }
  420. expires = jiffies + msecs_to_jiffies(msec);
  421. if (!expires)
  422. expires = 1;
  423. if (time_after(expires, ws->timer_expires)) {
  424. mod_timer(&ws->timer, expires);
  425. ws->timer_expires = expires;
  426. }
  427. unlock:
  428. spin_unlock_irqrestore(&ws->lock, flags);
  429. }
  430. EXPORT_SYMBOL_GPL(__pm_wakeup_event);
  431. /**
  432. * pm_wakeup_event - Notify the PM core of a wakeup event.
  433. * @dev: Device the wakeup event is related to.
  434. * @msec: Anticipated event processing time (in milliseconds).
  435. *
  436. * Call __pm_wakeup_event() for the @dev's wakeup source object.
  437. */
  438. void pm_wakeup_event(struct device *dev, unsigned int msec)
  439. {
  440. unsigned long flags;
  441. if (!dev)
  442. return;
  443. spin_lock_irqsave(&dev->power.lock, flags);
  444. __pm_wakeup_event(dev->power.wakeup, msec);
  445. spin_unlock_irqrestore(&dev->power.lock, flags);
  446. }
  447. EXPORT_SYMBOL_GPL(pm_wakeup_event);
  448. /**
  449. * pm_wakeup_update_hit_counts - Update hit counts of all active wakeup sources.
  450. */
  451. static void pm_wakeup_update_hit_counts(void)
  452. {
  453. unsigned long flags;
  454. struct wakeup_source *ws;
  455. rcu_read_lock();
  456. list_for_each_entry_rcu(ws, &wakeup_sources, entry) {
  457. spin_lock_irqsave(&ws->lock, flags);
  458. if (ws->active)
  459. ws->hit_count++;
  460. spin_unlock_irqrestore(&ws->lock, flags);
  461. }
  462. rcu_read_unlock();
  463. }
  464. /**
  465. * pm_wakeup_pending - Check if power transition in progress should be aborted.
  466. *
  467. * Compare the current number of registered wakeup events with its preserved
  468. * value from the past and return true if new wakeup events have been registered
  469. * since the old value was stored. Also return true if the current number of
  470. * wakeup events being processed is different from zero.
  471. */
  472. bool pm_wakeup_pending(void)
  473. {
  474. unsigned long flags;
  475. bool ret = false;
  476. spin_lock_irqsave(&events_lock, flags);
  477. if (events_check_enabled) {
  478. ret = ((unsigned int)atomic_read(&event_count) != saved_count)
  479. || atomic_read(&events_in_progress);
  480. events_check_enabled = !ret;
  481. }
  482. spin_unlock_irqrestore(&events_lock, flags);
  483. if (ret)
  484. pm_wakeup_update_hit_counts();
  485. return ret;
  486. }
  487. /**
  488. * pm_get_wakeup_count - Read the number of registered wakeup events.
  489. * @count: Address to store the value at.
  490. *
  491. * Store the number of registered wakeup events at the address in @count. Block
  492. * if the current number of wakeup events being processed is nonzero.
  493. *
  494. * Return false if the wait for the number of wakeup events being processed to
  495. * drop down to zero has been interrupted by a signal (and the current number
  496. * of wakeup events being processed is still nonzero). Otherwise return true.
  497. */
  498. bool pm_get_wakeup_count(unsigned int *count)
  499. {
  500. bool ret;
  501. if (capable(CAP_SYS_ADMIN))
  502. events_check_enabled = false;
  503. while (atomic_read(&events_in_progress) && !signal_pending(current)) {
  504. pm_wakeup_update_hit_counts();
  505. schedule_timeout_interruptible(msecs_to_jiffies(TIMEOUT));
  506. }
  507. ret = !atomic_read(&events_in_progress);
  508. *count = atomic_read(&event_count);
  509. return ret;
  510. }
  511. /**
  512. * pm_save_wakeup_count - Save the current number of registered wakeup events.
  513. * @count: Value to compare with the current number of registered wakeup events.
  514. *
  515. * If @count is equal to the current number of registered wakeup events and the
  516. * current number of wakeup events being processed is zero, store @count as the
  517. * old number of registered wakeup events to be used by pm_check_wakeup_events()
  518. * and return true. Otherwise return false.
  519. */
  520. bool pm_save_wakeup_count(unsigned int count)
  521. {
  522. bool ret = false;
  523. spin_lock_irq(&events_lock);
  524. if (count == (unsigned int)atomic_read(&event_count)
  525. && !atomic_read(&events_in_progress)) {
  526. saved_count = count;
  527. events_check_enabled = true;
  528. ret = true;
  529. }
  530. spin_unlock_irq(&events_lock);
  531. if (!ret)
  532. pm_wakeup_update_hit_counts();
  533. return ret;
  534. }
  535. static struct dentry *wakeup_sources_stats_dentry;
  536. /**
  537. * print_wakeup_source_stats - Print wakeup source statistics information.
  538. * @m: seq_file to print the statistics into.
  539. * @ws: Wakeup source object to print the statistics for.
  540. */
  541. static int print_wakeup_source_stats(struct seq_file *m,
  542. struct wakeup_source *ws)
  543. {
  544. unsigned long flags;
  545. ktime_t total_time;
  546. ktime_t max_time;
  547. unsigned long active_count;
  548. ktime_t active_time;
  549. int ret;
  550. spin_lock_irqsave(&ws->lock, flags);
  551. total_time = ws->total_time;
  552. max_time = ws->max_time;
  553. active_count = ws->active_count;
  554. if (ws->active) {
  555. active_time = ktime_sub(ktime_get(), ws->last_time);
  556. total_time = ktime_add(total_time, active_time);
  557. if (active_time.tv64 > max_time.tv64)
  558. max_time = active_time;
  559. } else {
  560. active_time = ktime_set(0, 0);
  561. }
  562. ret = seq_printf(m, "%-12s\t%lu\t\t%lu\t\t%lu\t\t"
  563. "%lld\t\t%lld\t\t%lld\t\t%lld\n",
  564. ws->name, active_count, ws->event_count, ws->hit_count,
  565. ktime_to_ms(active_time), ktime_to_ms(total_time),
  566. ktime_to_ms(max_time), ktime_to_ms(ws->last_time));
  567. spin_unlock_irqrestore(&ws->lock, flags);
  568. return ret;
  569. }
  570. /**
  571. * wakeup_sources_stats_show - Print wakeup sources statistics information.
  572. * @m: seq_file to print the statistics into.
  573. */
  574. static int wakeup_sources_stats_show(struct seq_file *m, void *unused)
  575. {
  576. struct wakeup_source *ws;
  577. seq_puts(m, "name\t\tactive_count\tevent_count\thit_count\t"
  578. "active_since\ttotal_time\tmax_time\tlast_change\n");
  579. rcu_read_lock();
  580. list_for_each_entry_rcu(ws, &wakeup_sources, entry)
  581. print_wakeup_source_stats(m, ws);
  582. rcu_read_unlock();
  583. return 0;
  584. }
  585. static int wakeup_sources_stats_open(struct inode *inode, struct file *file)
  586. {
  587. return single_open(file, wakeup_sources_stats_show, NULL);
  588. }
  589. static const struct file_operations wakeup_sources_stats_fops = {
  590. .owner = THIS_MODULE,
  591. .open = wakeup_sources_stats_open,
  592. .read = seq_read,
  593. .llseek = seq_lseek,
  594. .release = single_release,
  595. };
  596. static int __init wakeup_sources_debugfs_init(void)
  597. {
  598. wakeup_sources_stats_dentry = debugfs_create_file("wakeup_sources",
  599. S_IRUGO, NULL, NULL, &wakeup_sources_stats_fops);
  600. return 0;
  601. }
  602. postcore_initcall(wakeup_sources_debugfs_init);