earlysuspend.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* kernel/power/earlysuspend.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/earlysuspend.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/rtc.h>
  19. #include <linux/syscalls.h> /* sys_sync */
  20. #include <linux/wakelock.h>
  21. #include <linux/workqueue.h>
  22. #include "power.h"
  23. enum {
  24. DEBUG_USER_STATE = 1U << 0,
  25. DEBUG_SUSPEND = 1U << 2,
  26. DEBUG_VERBOSE = 1U << 3,
  27. };
  28. static int debug_mask = DEBUG_USER_STATE;
  29. module_param_named(debug_mask, debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP);
  30. static DEFINE_MUTEX(early_suspend_lock);
  31. static LIST_HEAD(early_suspend_handlers);
  32. static void early_suspend(struct work_struct *work);
  33. static void late_resume(struct work_struct *work);
  34. static DECLARE_WORK(early_suspend_work, early_suspend);
  35. static DECLARE_WORK(late_resume_work, late_resume);
  36. static DEFINE_SPINLOCK(state_lock);
  37. enum {
  38. SUSPEND_REQUESTED = 0x1,
  39. SUSPENDED = 0x2,
  40. SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
  41. };
  42. static int state;
  43. void register_early_suspend(struct early_suspend *handler)
  44. {
  45. struct list_head *pos;
  46. mutex_lock(&early_suspend_lock);
  47. list_for_each(pos, &early_suspend_handlers) {
  48. struct early_suspend *e;
  49. e = list_entry(pos, struct early_suspend, link);
  50. if (e->level > handler->level)
  51. break;
  52. }
  53. list_add_tail(&handler->link, pos);
  54. if ((state & SUSPENDED) && handler->suspend)
  55. handler->suspend(handler);
  56. mutex_unlock(&early_suspend_lock);
  57. }
  58. EXPORT_SYMBOL(register_early_suspend);
  59. void unregister_early_suspend(struct early_suspend *handler)
  60. {
  61. mutex_lock(&early_suspend_lock);
  62. list_del(&handler->link);
  63. mutex_unlock(&early_suspend_lock);
  64. }
  65. EXPORT_SYMBOL(unregister_early_suspend);
  66. static void early_suspend(struct work_struct *work)
  67. {
  68. struct early_suspend *pos;
  69. unsigned long irqflags;
  70. int abort = 0;
  71. mutex_lock(&early_suspend_lock);
  72. spin_lock_irqsave(&state_lock, irqflags);
  73. if (state == SUSPEND_REQUESTED)
  74. state |= SUSPENDED;
  75. else
  76. abort = 1;
  77. spin_unlock_irqrestore(&state_lock, irqflags);
  78. if (abort) {
  79. if (debug_mask & DEBUG_SUSPEND)
  80. pr_info("early_suspend: abort, state %d\n", state);
  81. mutex_unlock(&early_suspend_lock);
  82. goto abort;
  83. }
  84. if (debug_mask & DEBUG_SUSPEND)
  85. pr_info("early_suspend: call handlers\n");
  86. list_for_each_entry(pos, &early_suspend_handlers, link) {
  87. if (pos->suspend != NULL) {
  88. if (debug_mask & DEBUG_VERBOSE)
  89. pr_info("early_suspend: calling %pf\n", pos->suspend);
  90. pos->suspend(pos);
  91. }
  92. }
  93. mutex_unlock(&early_suspend_lock);
  94. if (debug_mask & DEBUG_SUSPEND)
  95. pr_info("early_suspend: sync\n");
  96. sys_sync();
  97. abort:
  98. spin_lock_irqsave(&state_lock, irqflags);
  99. if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
  100. wake_unlock(&main_wake_lock);
  101. spin_unlock_irqrestore(&state_lock, irqflags);
  102. }
  103. static void late_resume(struct work_struct *work)
  104. {
  105. struct early_suspend *pos;
  106. unsigned long irqflags;
  107. int abort = 0;
  108. mutex_lock(&early_suspend_lock);
  109. spin_lock_irqsave(&state_lock, irqflags);
  110. if (state == SUSPENDED)
  111. state &= ~SUSPENDED;
  112. else
  113. abort = 1;
  114. spin_unlock_irqrestore(&state_lock, irqflags);
  115. if (abort) {
  116. if (debug_mask & DEBUG_SUSPEND)
  117. pr_info("late_resume: abort, state %d\n", state);
  118. goto abort;
  119. }
  120. if (debug_mask & DEBUG_SUSPEND)
  121. pr_info("late_resume: call handlers\n");
  122. list_for_each_entry_reverse(pos, &early_suspend_handlers, link) {
  123. if (pos->resume != NULL) {
  124. if (debug_mask & DEBUG_VERBOSE)
  125. pr_info("late_resume: calling %pf\n", pos->resume);
  126. pos->resume(pos);
  127. }
  128. }
  129. if (debug_mask & DEBUG_SUSPEND)
  130. pr_info("late_resume: done\n");
  131. abort:
  132. mutex_unlock(&early_suspend_lock);
  133. }
  134. void request_suspend_state(suspend_state_t new_state)
  135. {
  136. unsigned long irqflags;
  137. int old_sleep;
  138. spin_lock_irqsave(&state_lock, irqflags);
  139. old_sleep = state & SUSPEND_REQUESTED;
  140. if (debug_mask & DEBUG_USER_STATE) {
  141. struct timespec ts;
  142. struct rtc_time tm;
  143. getnstimeofday(&ts);
  144. rtc_time_to_tm(ts.tv_sec, &tm);
  145. pr_info("request_suspend_state: %s (%d->%d) at %lld "
  146. "(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
  147. new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
  148. requested_suspend_state, new_state,
  149. ktime_to_ns(ktime_get()),
  150. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  151. tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
  152. }
  153. if (!old_sleep && new_state != PM_SUSPEND_ON) {
  154. state |= SUSPEND_REQUESTED;
  155. queue_work(suspend_work_queue, &early_suspend_work);
  156. } else if (old_sleep && new_state == PM_SUSPEND_ON) {
  157. state &= ~SUSPEND_REQUESTED;
  158. wake_lock(&main_wake_lock);
  159. queue_work(suspend_work_queue, &late_resume_work);
  160. }
  161. requested_suspend_state = new_state;
  162. spin_unlock_irqrestore(&state_lock, irqflags);
  163. }
  164. suspend_state_t get_suspend_state(void)
  165. {
  166. return requested_suspend_state;
  167. }