wakelock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /* kernel/power/wakelock.c
  2. *
  3. * Copyright (C) 2005-2008 Google, Inc.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/rtc.h>
  18. #include <linux/suspend.h>
  19. #include <linux/syscalls.h> /* sys_sync */
  20. #include <linux/wakelock.h>
  21. #ifdef CONFIG_WAKELOCK_STAT
  22. #include <linux/proc_fs.h>
  23. #endif
  24. #include "power.h"
  25. enum {
  26. DEBUG_EXIT_SUSPEND = 1U << 0,
  27. DEBUG_WAKEUP = 1U << 1,
  28. DEBUG_SUSPEND = 1U << 2,
  29. DEBUG_EXPIRE = 1U << 3,
  30. DEBUG_WAKE_LOCK = 1U << 4,
  31. };
  32. static int debug_mask = DEBUG_EXIT_SUSPEND | DEBUG_WAKEUP;
  33. module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
  34. #define WAKE_LOCK_TYPE_MASK (0x0f)
  35. #define WAKE_LOCK_INITIALIZED (1U << 8)
  36. #define WAKE_LOCK_ACTIVE (1U << 9)
  37. #define WAKE_LOCK_AUTO_EXPIRE (1U << 10)
  38. #define WAKE_LOCK_PREVENTING_SUSPEND (1U << 11)
  39. static DEFINE_SPINLOCK(list_lock);
  40. static LIST_HEAD(inactive_locks);
  41. static struct list_head active_wake_locks[WAKE_LOCK_TYPE_COUNT];
  42. static int current_event_num;
  43. struct workqueue_struct *suspend_work_queue;
  44. struct wake_lock main_wake_lock;
  45. suspend_state_t requested_suspend_state = PM_SUSPEND_MEM;
  46. static struct wake_lock unknown_wakeup;
  47. static struct wake_lock suspend_backoff_lock;
  48. #define SUSPEND_BACKOFF_THRESHOLD 10
  49. #define SUSPEND_BACKOFF_INTERVAL 10000
  50. static unsigned suspend_short_count;
  51. #ifdef CONFIG_WAKELOCK_STAT
  52. static struct wake_lock deleted_wake_locks;
  53. static ktime_t last_sleep_time_update;
  54. static int wait_for_wakeup;
  55. int get_expired_time(struct wake_lock *lock, ktime_t *expire_time)
  56. {
  57. struct timespec ts;
  58. struct timespec kt;
  59. struct timespec tomono;
  60. struct timespec delta;
  61. struct timespec sleep;
  62. long timeout;
  63. if (!(lock->flags & WAKE_LOCK_AUTO_EXPIRE))
  64. return 0;
  65. get_xtime_and_monotonic_and_sleep_offset(&kt, &tomono, &sleep);
  66. timeout = lock->expires - jiffies;
  67. if (timeout > 0)
  68. return 0;
  69. jiffies_to_timespec(-timeout, &delta);
  70. set_normalized_timespec(&ts, kt.tv_sec + tomono.tv_sec - delta.tv_sec,
  71. kt.tv_nsec + tomono.tv_nsec - delta.tv_nsec);
  72. *expire_time = timespec_to_ktime(ts);
  73. return 1;
  74. }
  75. static int print_lock_stat(struct seq_file *m, struct wake_lock *lock)
  76. {
  77. int lock_count = lock->stat.count;
  78. int expire_count = lock->stat.expire_count;
  79. ktime_t active_time = ktime_set(0, 0);
  80. ktime_t total_time = lock->stat.total_time;
  81. ktime_t max_time = lock->stat.max_time;
  82. ktime_t prevent_suspend_time = lock->stat.prevent_suspend_time;
  83. if (lock->flags & WAKE_LOCK_ACTIVE) {
  84. ktime_t now, add_time;
  85. int expired = get_expired_time(lock, &now);
  86. if (!expired)
  87. now = ktime_get();
  88. add_time = ktime_sub(now, lock->stat.last_time);
  89. lock_count++;
  90. if (!expired)
  91. active_time = add_time;
  92. else
  93. expire_count++;
  94. total_time = ktime_add(total_time, add_time);
  95. if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND)
  96. prevent_suspend_time = ktime_add(prevent_suspend_time,
  97. ktime_sub(now, last_sleep_time_update));
  98. if (add_time.tv64 > max_time.tv64)
  99. max_time = add_time;
  100. }
  101. return seq_printf(m,
  102. "\"%s\"\t%d\t%d\t%d\t%lld\t%lld\t%lld\t%lld\t%lld\n",
  103. lock->name, lock_count, expire_count,
  104. lock->stat.wakeup_count, ktime_to_ns(active_time),
  105. ktime_to_ns(total_time),
  106. ktime_to_ns(prevent_suspend_time), ktime_to_ns(max_time),
  107. ktime_to_ns(lock->stat.last_time));
  108. }
  109. static int wakelock_stats_show(struct seq_file *m, void *unused)
  110. {
  111. unsigned long irqflags;
  112. struct wake_lock *lock;
  113. int ret;
  114. int type;
  115. spin_lock_irqsave(&list_lock, irqflags);
  116. ret = seq_puts(m, "name\tcount\texpire_count\twake_count\tactive_since"
  117. "\ttotal_time\tsleep_time\tmax_time\tlast_change\n");
  118. list_for_each_entry(lock, &inactive_locks, link)
  119. ret = print_lock_stat(m, lock);
  120. for (type = 0; type < WAKE_LOCK_TYPE_COUNT; type++) {
  121. list_for_each_entry(lock, &active_wake_locks[type], link)
  122. ret = print_lock_stat(m, lock);
  123. }
  124. spin_unlock_irqrestore(&list_lock, irqflags);
  125. return 0;
  126. }
  127. static void wake_unlock_stat_locked(struct wake_lock *lock, int expired)
  128. {
  129. ktime_t duration;
  130. ktime_t now;
  131. if (!(lock->flags & WAKE_LOCK_ACTIVE))
  132. return;
  133. if (get_expired_time(lock, &now))
  134. expired = 1;
  135. else
  136. now = ktime_get();
  137. lock->stat.count++;
  138. if (expired)
  139. lock->stat.expire_count++;
  140. duration = ktime_sub(now, lock->stat.last_time);
  141. lock->stat.total_time = ktime_add(lock->stat.total_time, duration);
  142. if (ktime_to_ns(duration) > ktime_to_ns(lock->stat.max_time))
  143. lock->stat.max_time = duration;
  144. lock->stat.last_time = ktime_get();
  145. if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
  146. duration = ktime_sub(now, last_sleep_time_update);
  147. lock->stat.prevent_suspend_time = ktime_add(
  148. lock->stat.prevent_suspend_time, duration);
  149. lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
  150. }
  151. }
  152. static void update_sleep_wait_stats_locked(int done)
  153. {
  154. struct wake_lock *lock;
  155. ktime_t now, etime, elapsed, add;
  156. int expired;
  157. now = ktime_get();
  158. elapsed = ktime_sub(now, last_sleep_time_update);
  159. list_for_each_entry(lock, &active_wake_locks[WAKE_LOCK_SUSPEND], link) {
  160. expired = get_expired_time(lock, &etime);
  161. if (lock->flags & WAKE_LOCK_PREVENTING_SUSPEND) {
  162. if (expired)
  163. add = ktime_sub(etime, last_sleep_time_update);
  164. else
  165. add = elapsed;
  166. lock->stat.prevent_suspend_time = ktime_add(
  167. lock->stat.prevent_suspend_time, add);
  168. }
  169. if (done || expired)
  170. lock->flags &= ~WAKE_LOCK_PREVENTING_SUSPEND;
  171. else
  172. lock->flags |= WAKE_LOCK_PREVENTING_SUSPEND;
  173. }
  174. last_sleep_time_update = now;
  175. }
  176. #endif
  177. static void expire_wake_lock(struct wake_lock *lock)
  178. {
  179. #ifdef CONFIG_WAKELOCK_STAT
  180. wake_unlock_stat_locked(lock, 1);
  181. #endif
  182. lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
  183. list_del(&lock->link);
  184. list_add(&lock->link, &inactive_locks);
  185. if (debug_mask & (DEBUG_WAKE_LOCK | DEBUG_EXPIRE))
  186. pr_info("expired wake lock %s\n", lock->name);
  187. }
  188. /* Caller must acquire the list_lock spinlock */
  189. static void print_active_locks(int type)
  190. {
  191. struct wake_lock *lock;
  192. bool print_expired = true;
  193. BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
  194. list_for_each_entry(lock, &active_wake_locks[type], link) {
  195. if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
  196. long timeout = lock->expires - jiffies;
  197. if (timeout > 0)
  198. pr_info("active wake lock %s, time left %ld\n",
  199. lock->name, timeout);
  200. else if (print_expired)
  201. pr_info("wake lock %s, expired\n", lock->name);
  202. } else {
  203. pr_info("active wake lock %s\n", lock->name);
  204. if (!(debug_mask & DEBUG_EXPIRE))
  205. print_expired = false;
  206. }
  207. }
  208. }
  209. static long has_wake_lock_locked(int type)
  210. {
  211. struct wake_lock *lock, *n;
  212. long max_timeout = 0;
  213. BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
  214. list_for_each_entry_safe(lock, n, &active_wake_locks[type], link) {
  215. if (lock->flags & WAKE_LOCK_AUTO_EXPIRE) {
  216. long timeout = lock->expires - jiffies;
  217. if (timeout <= 0)
  218. expire_wake_lock(lock);
  219. else if (timeout > max_timeout)
  220. max_timeout = timeout;
  221. } else
  222. return -1;
  223. }
  224. return max_timeout;
  225. }
  226. long has_wake_lock(int type)
  227. {
  228. long ret;
  229. unsigned long irqflags;
  230. spin_lock_irqsave(&list_lock, irqflags);
  231. ret = has_wake_lock_locked(type);
  232. if (ret && (debug_mask & DEBUG_WAKEUP) && type == WAKE_LOCK_SUSPEND)
  233. print_active_locks(type);
  234. spin_unlock_irqrestore(&list_lock, irqflags);
  235. return ret;
  236. }
  237. static void suspend_backoff(void)
  238. {
  239. pr_info("suspend: too many immediate wakeups, back off\n");
  240. wake_lock_timeout(&suspend_backoff_lock,
  241. msecs_to_jiffies(SUSPEND_BACKOFF_INTERVAL));
  242. }
  243. static void suspend(struct work_struct *work)
  244. {
  245. int ret;
  246. int entry_event_num;
  247. struct timespec ts_entry, ts_exit;
  248. if (has_wake_lock(WAKE_LOCK_SUSPEND)) {
  249. if (debug_mask & DEBUG_SUSPEND)
  250. pr_info("suspend: abort suspend\n");
  251. return;
  252. }
  253. entry_event_num = current_event_num;
  254. sys_sync();
  255. if (debug_mask & DEBUG_SUSPEND)
  256. pr_info("suspend: enter suspend\n");
  257. getnstimeofday(&ts_entry);
  258. ret = pm_suspend(requested_suspend_state);
  259. getnstimeofday(&ts_exit);
  260. if (debug_mask & DEBUG_EXIT_SUSPEND) {
  261. struct rtc_time tm;
  262. rtc_time_to_tm(ts_exit.tv_sec, &tm);
  263. pr_info("suspend: exit suspend, ret = %d "
  264. "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n", ret,
  265. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  266. tm.tm_hour, tm.tm_min, tm.tm_sec, ts_exit.tv_nsec);
  267. }
  268. if (ts_exit.tv_sec - ts_entry.tv_sec <= 1) {
  269. ++suspend_short_count;
  270. if (suspend_short_count == SUSPEND_BACKOFF_THRESHOLD) {
  271. suspend_backoff();
  272. suspend_short_count = 0;
  273. }
  274. } else {
  275. suspend_short_count = 0;
  276. }
  277. if (current_event_num == entry_event_num) {
  278. if (debug_mask & DEBUG_SUSPEND)
  279. pr_info("suspend: pm_suspend returned with no event\n");
  280. wake_lock_timeout(&unknown_wakeup, HZ / 2);
  281. }
  282. }
  283. static DECLARE_WORK(suspend_work, suspend);
  284. static void expire_wake_locks(unsigned long data)
  285. {
  286. long has_lock;
  287. unsigned long irqflags;
  288. if (debug_mask & DEBUG_EXPIRE)
  289. pr_info("expire_wake_locks: start\n");
  290. spin_lock_irqsave(&list_lock, irqflags);
  291. if (debug_mask & DEBUG_SUSPEND)
  292. print_active_locks(WAKE_LOCK_SUSPEND);
  293. has_lock = has_wake_lock_locked(WAKE_LOCK_SUSPEND);
  294. if (debug_mask & DEBUG_EXPIRE)
  295. pr_info("expire_wake_locks: done, has_lock %ld\n", has_lock);
  296. if (has_lock == 0)
  297. queue_work(suspend_work_queue, &suspend_work);
  298. spin_unlock_irqrestore(&list_lock, irqflags);
  299. }
  300. static DEFINE_TIMER(expire_timer, expire_wake_locks, 0, 0);
  301. static int power_suspend_late(struct device *dev)
  302. {
  303. int ret = has_wake_lock(WAKE_LOCK_SUSPEND) ? -EAGAIN : 0;
  304. #ifdef CONFIG_WAKELOCK_STAT
  305. wait_for_wakeup = !ret;
  306. #endif
  307. if (debug_mask & DEBUG_SUSPEND)
  308. pr_info("power_suspend_late return %d\n", ret);
  309. return ret;
  310. }
  311. static struct dev_pm_ops power_driver_pm_ops = {
  312. .suspend_noirq = power_suspend_late,
  313. };
  314. static struct platform_driver power_driver = {
  315. .driver.name = "power",
  316. .driver.pm = &power_driver_pm_ops,
  317. };
  318. static struct platform_device power_device = {
  319. .name = "power",
  320. };
  321. void wake_lock_init(struct wake_lock *lock, int type, const char *name)
  322. {
  323. unsigned long irqflags = 0;
  324. if (name)
  325. lock->name = name;
  326. BUG_ON(!lock->name);
  327. if (debug_mask & DEBUG_WAKE_LOCK)
  328. pr_info("wake_lock_init name=%s\n", lock->name);
  329. #ifdef CONFIG_WAKELOCK_STAT
  330. lock->stat.count = 0;
  331. lock->stat.expire_count = 0;
  332. lock->stat.wakeup_count = 0;
  333. lock->stat.total_time = ktime_set(0, 0);
  334. lock->stat.prevent_suspend_time = ktime_set(0, 0);
  335. lock->stat.max_time = ktime_set(0, 0);
  336. lock->stat.last_time = ktime_set(0, 0);
  337. #endif
  338. lock->flags = (type & WAKE_LOCK_TYPE_MASK) | WAKE_LOCK_INITIALIZED;
  339. INIT_LIST_HEAD(&lock->link);
  340. spin_lock_irqsave(&list_lock, irqflags);
  341. list_add(&lock->link, &inactive_locks);
  342. spin_unlock_irqrestore(&list_lock, irqflags);
  343. }
  344. EXPORT_SYMBOL(wake_lock_init);
  345. void wake_lock_destroy(struct wake_lock *lock)
  346. {
  347. unsigned long irqflags;
  348. if (debug_mask & DEBUG_WAKE_LOCK)
  349. pr_info("wake_lock_destroy name=%s\n", lock->name);
  350. spin_lock_irqsave(&list_lock, irqflags);
  351. lock->flags &= ~WAKE_LOCK_INITIALIZED;
  352. #ifdef CONFIG_WAKELOCK_STAT
  353. if (lock->stat.count) {
  354. deleted_wake_locks.stat.count += lock->stat.count;
  355. deleted_wake_locks.stat.expire_count += lock->stat.expire_count;
  356. deleted_wake_locks.stat.total_time =
  357. ktime_add(deleted_wake_locks.stat.total_time,
  358. lock->stat.total_time);
  359. deleted_wake_locks.stat.prevent_suspend_time =
  360. ktime_add(deleted_wake_locks.stat.prevent_suspend_time,
  361. lock->stat.prevent_suspend_time);
  362. deleted_wake_locks.stat.max_time =
  363. ktime_add(deleted_wake_locks.stat.max_time,
  364. lock->stat.max_time);
  365. }
  366. #endif
  367. list_del(&lock->link);
  368. spin_unlock_irqrestore(&list_lock, irqflags);
  369. }
  370. EXPORT_SYMBOL(wake_lock_destroy);
  371. static void wake_lock_internal(
  372. struct wake_lock *lock, long timeout, int has_timeout)
  373. {
  374. int type;
  375. unsigned long irqflags;
  376. long expire_in;
  377. spin_lock_irqsave(&list_lock, irqflags);
  378. type = lock->flags & WAKE_LOCK_TYPE_MASK;
  379. BUG_ON(type >= WAKE_LOCK_TYPE_COUNT);
  380. BUG_ON(!(lock->flags & WAKE_LOCK_INITIALIZED));
  381. #ifdef CONFIG_WAKELOCK_STAT
  382. if (type == WAKE_LOCK_SUSPEND && wait_for_wakeup) {
  383. if (debug_mask & DEBUG_WAKEUP)
  384. pr_info("wakeup wake lock: %s\n", lock->name);
  385. wait_for_wakeup = 0;
  386. lock->stat.wakeup_count++;
  387. }
  388. if ((lock->flags & WAKE_LOCK_AUTO_EXPIRE) &&
  389. (long)(lock->expires - jiffies) <= 0) {
  390. wake_unlock_stat_locked(lock, 0);
  391. lock->stat.last_time = ktime_get();
  392. }
  393. #endif
  394. if (!(lock->flags & WAKE_LOCK_ACTIVE)) {
  395. lock->flags |= WAKE_LOCK_ACTIVE;
  396. #ifdef CONFIG_WAKELOCK_STAT
  397. lock->stat.last_time = ktime_get();
  398. #endif
  399. }
  400. list_del(&lock->link);
  401. if (has_timeout) {
  402. if (debug_mask & DEBUG_WAKE_LOCK)
  403. pr_info("wake_lock: %s, type %d, timeout %ld.%03lu\n",
  404. lock->name, type, timeout / HZ,
  405. (timeout % HZ) * MSEC_PER_SEC / HZ);
  406. lock->expires = jiffies + timeout;
  407. lock->flags |= WAKE_LOCK_AUTO_EXPIRE;
  408. list_add_tail(&lock->link, &active_wake_locks[type]);
  409. } else {
  410. if (debug_mask & DEBUG_WAKE_LOCK)
  411. pr_info("wake_lock: %s, type %d\n", lock->name, type);
  412. lock->expires = LONG_MAX;
  413. lock->flags &= ~WAKE_LOCK_AUTO_EXPIRE;
  414. list_add(&lock->link, &active_wake_locks[type]);
  415. }
  416. if (type == WAKE_LOCK_SUSPEND) {
  417. current_event_num++;
  418. #ifdef CONFIG_WAKELOCK_STAT
  419. if (lock == &main_wake_lock)
  420. update_sleep_wait_stats_locked(1);
  421. else if (!wake_lock_active(&main_wake_lock))
  422. update_sleep_wait_stats_locked(0);
  423. #endif
  424. if (has_timeout)
  425. expire_in = has_wake_lock_locked(type);
  426. else
  427. expire_in = -1;
  428. if (expire_in > 0) {
  429. if (debug_mask & DEBUG_EXPIRE)
  430. pr_info("wake_lock: %s, start expire timer, "
  431. "%ld\n", lock->name, expire_in);
  432. mod_timer(&expire_timer, jiffies + expire_in);
  433. } else {
  434. if (del_timer(&expire_timer))
  435. if (debug_mask & DEBUG_EXPIRE)
  436. pr_info("wake_lock: %s, stop expire timer\n",
  437. lock->name);
  438. if (expire_in == 0)
  439. queue_work(suspend_work_queue, &suspend_work);
  440. }
  441. }
  442. spin_unlock_irqrestore(&list_lock, irqflags);
  443. }
  444. void wake_lock(struct wake_lock *lock)
  445. {
  446. wake_lock_internal(lock, 0, 0);
  447. }
  448. EXPORT_SYMBOL(wake_lock);
  449. void wake_lock_timeout(struct wake_lock *lock, long timeout)
  450. {
  451. wake_lock_internal(lock, timeout, 1);
  452. }
  453. EXPORT_SYMBOL(wake_lock_timeout);
  454. void wake_unlock(struct wake_lock *lock)
  455. {
  456. int type;
  457. unsigned long irqflags;
  458. spin_lock_irqsave(&list_lock, irqflags);
  459. type = lock->flags & WAKE_LOCK_TYPE_MASK;
  460. #ifdef CONFIG_WAKELOCK_STAT
  461. wake_unlock_stat_locked(lock, 0);
  462. #endif
  463. if (debug_mask & DEBUG_WAKE_LOCK)
  464. pr_info("wake_unlock: %s\n", lock->name);
  465. lock->flags &= ~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
  466. list_del(&lock->link);
  467. list_add(&lock->link, &inactive_locks);
  468. if (type == WAKE_LOCK_SUSPEND) {
  469. long has_lock = has_wake_lock_locked(type);
  470. if (has_lock > 0) {
  471. if (debug_mask & DEBUG_EXPIRE)
  472. pr_info("wake_unlock: %s, start expire timer, "
  473. "%ld\n", lock->name, has_lock);
  474. mod_timer(&expire_timer, jiffies + has_lock);
  475. } else {
  476. if (del_timer(&expire_timer))
  477. if (debug_mask & DEBUG_EXPIRE)
  478. pr_info("wake_unlock: %s, stop expire "
  479. "timer\n", lock->name);
  480. if (has_lock == 0)
  481. queue_work(suspend_work_queue, &suspend_work);
  482. }
  483. if (lock == &main_wake_lock) {
  484. if (debug_mask & DEBUG_SUSPEND)
  485. print_active_locks(WAKE_LOCK_SUSPEND);
  486. #ifdef CONFIG_WAKELOCK_STAT
  487. update_sleep_wait_stats_locked(0);
  488. #endif
  489. }
  490. }
  491. spin_unlock_irqrestore(&list_lock, irqflags);
  492. }
  493. EXPORT_SYMBOL(wake_unlock);
  494. int wake_lock_active(struct wake_lock *lock)
  495. {
  496. return !!(lock->flags & WAKE_LOCK_ACTIVE);
  497. }
  498. EXPORT_SYMBOL(wake_lock_active);
  499. static int wakelock_stats_open(struct inode *inode, struct file *file)
  500. {
  501. return single_open(file, wakelock_stats_show, NULL);
  502. }
  503. static const struct file_operations wakelock_stats_fops = {
  504. .owner = THIS_MODULE,
  505. .open = wakelock_stats_open,
  506. .read = seq_read,
  507. .llseek = seq_lseek,
  508. .release = single_release,
  509. };
  510. static int __init wakelocks_init(void)
  511. {
  512. int ret;
  513. int i;
  514. for (i = 0; i < ARRAY_SIZE(active_wake_locks); i++)
  515. INIT_LIST_HEAD(&active_wake_locks[i]);
  516. #ifdef CONFIG_WAKELOCK_STAT
  517. wake_lock_init(&deleted_wake_locks, WAKE_LOCK_SUSPEND,
  518. "deleted_wake_locks");
  519. #endif
  520. wake_lock_init(&main_wake_lock, WAKE_LOCK_SUSPEND, "main");
  521. wake_lock(&main_wake_lock);
  522. wake_lock_init(&unknown_wakeup, WAKE_LOCK_SUSPEND, "unknown_wakeups");
  523. wake_lock_init(&suspend_backoff_lock, WAKE_LOCK_SUSPEND,
  524. "suspend_backoff");
  525. ret = platform_device_register(&power_device);
  526. if (ret) {
  527. pr_err("wakelocks_init: platform_device_register failed\n");
  528. goto err_platform_device_register;
  529. }
  530. ret = platform_driver_register(&power_driver);
  531. if (ret) {
  532. pr_err("wakelocks_init: platform_driver_register failed\n");
  533. goto err_platform_driver_register;
  534. }
  535. suspend_work_queue = create_singlethread_workqueue("suspend");
  536. if (suspend_work_queue == NULL) {
  537. ret = -ENOMEM;
  538. goto err_suspend_work_queue;
  539. }
  540. #ifdef CONFIG_WAKELOCK_STAT
  541. proc_create("wakelocks", S_IRUGO, NULL, &wakelock_stats_fops);
  542. #endif
  543. return 0;
  544. err_suspend_work_queue:
  545. platform_driver_unregister(&power_driver);
  546. err_platform_driver_register:
  547. platform_device_unregister(&power_device);
  548. err_platform_device_register:
  549. wake_lock_destroy(&suspend_backoff_lock);
  550. wake_lock_destroy(&unknown_wakeup);
  551. wake_lock_destroy(&main_wake_lock);
  552. #ifdef CONFIG_WAKELOCK_STAT
  553. wake_lock_destroy(&deleted_wake_locks);
  554. #endif
  555. return ret;
  556. }
  557. static void __exit wakelocks_exit(void)
  558. {
  559. #ifdef CONFIG_WAKELOCK_STAT
  560. remove_proc_entry("wakelocks", NULL);
  561. #endif
  562. destroy_workqueue(suspend_work_queue);
  563. platform_driver_unregister(&power_driver);
  564. platform_device_unregister(&power_device);
  565. wake_lock_destroy(&suspend_backoff_lock);
  566. wake_lock_destroy(&unknown_wakeup);
  567. wake_lock_destroy(&main_wake_lock);
  568. #ifdef CONFIG_WAKELOCK_STAT
  569. wake_lock_destroy(&deleted_wake_locks);
  570. #endif
  571. }
  572. core_initcall(wakelocks_init);
  573. module_exit(wakelocks_exit);