wakeup.c 20 KB

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