wakeup.c 20 KB

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