fbearlysuspend.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* kernel/power/fbearlysuspend.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/wait.h>
  18. #include "power.h"
  19. #define MAX_BUF 100
  20. static wait_queue_head_t fb_state_wq;
  21. static int display = 1;
  22. static DEFINE_SPINLOCK(fb_state_lock);
  23. static enum {
  24. FB_STATE_STOPPED_DRAWING,
  25. FB_STATE_REQUEST_STOP_DRAWING,
  26. FB_STATE_DRAWING_OK,
  27. } fb_state;
  28. /* tell userspace to stop drawing, wait for it to stop */
  29. static void stop_drawing_early_suspend(struct early_suspend *h)
  30. {
  31. int ret;
  32. unsigned long irq_flags;
  33. spin_lock_irqsave(&fb_state_lock, irq_flags);
  34. fb_state = FB_STATE_REQUEST_STOP_DRAWING;
  35. spin_unlock_irqrestore(&fb_state_lock, irq_flags);
  36. wake_up_all(&fb_state_wq);
  37. ret = wait_event_timeout(fb_state_wq,
  38. fb_state == FB_STATE_STOPPED_DRAWING,
  39. HZ);
  40. if (unlikely(fb_state != FB_STATE_STOPPED_DRAWING))
  41. pr_warning("stop_drawing_early_suspend: timeout waiting for "
  42. "userspace to stop drawing\n");
  43. }
  44. /* tell userspace to start drawing */
  45. static void start_drawing_late_resume(struct early_suspend *h)
  46. {
  47. unsigned long irq_flags;
  48. spin_lock_irqsave(&fb_state_lock, irq_flags);
  49. fb_state = FB_STATE_DRAWING_OK;
  50. spin_unlock_irqrestore(&fb_state_lock, irq_flags);
  51. wake_up(&fb_state_wq);
  52. }
  53. static struct early_suspend stop_drawing_early_suspend_desc = {
  54. .level = EARLY_SUSPEND_LEVEL_STOP_DRAWING,
  55. .suspend = stop_drawing_early_suspend,
  56. .resume = start_drawing_late_resume,
  57. };
  58. static ssize_t wait_for_fb_sleep_show(struct kobject *kobj,
  59. struct kobj_attribute *attr, char *buf)
  60. {
  61. char *s = buf;
  62. int ret;
  63. ret = wait_event_interruptible(fb_state_wq,
  64. fb_state != FB_STATE_DRAWING_OK);
  65. if (ret && fb_state == FB_STATE_DRAWING_OK) {
  66. return ret;
  67. } else {
  68. s += sprintf(buf, "sleeping");
  69. if (display == 1) {
  70. display = 0;
  71. sysfs_notify(power_kobj, NULL, "wait_for_fb_status");
  72. }
  73. }
  74. return s - buf;
  75. }
  76. static ssize_t wait_for_fb_wake_show(struct kobject *kobj,
  77. struct kobj_attribute *attr, char *buf)
  78. {
  79. char *s = buf;
  80. int ret;
  81. unsigned long irq_flags;
  82. spin_lock_irqsave(&fb_state_lock, irq_flags);
  83. if (fb_state == FB_STATE_REQUEST_STOP_DRAWING) {
  84. fb_state = FB_STATE_STOPPED_DRAWING;
  85. wake_up(&fb_state_wq);
  86. }
  87. spin_unlock_irqrestore(&fb_state_lock, irq_flags);
  88. ret = wait_event_interruptible(fb_state_wq,
  89. fb_state == FB_STATE_DRAWING_OK);
  90. if (ret && fb_state != FB_STATE_DRAWING_OK)
  91. return ret;
  92. else {
  93. s += sprintf(buf, "awake");
  94. if (display == 0) {
  95. display = 1;
  96. sysfs_notify(power_kobj, NULL, "wait_for_fb_status");
  97. }
  98. }
  99. return s - buf;
  100. }
  101. static ssize_t wait_for_fb_status_show(struct kobject *kobj,
  102. struct kobj_attribute *attr, char *buf)
  103. {
  104. int ret = 0;
  105. if (display == 1)
  106. ret = snprintf(buf, strnlen("on", MAX_BUF) + 1, "on");
  107. else
  108. ret = snprintf(buf, strnlen("off", MAX_BUF) + 1, "off");
  109. return ret;
  110. }
  111. #define power_ro_attr(_name) \
  112. static struct kobj_attribute _name##_attr = { \
  113. .attr = { \
  114. .name = __stringify(_name), \
  115. .mode = 0444, \
  116. }, \
  117. .show = _name##_show, \
  118. .store = NULL, \
  119. }
  120. power_ro_attr(wait_for_fb_sleep);
  121. power_ro_attr(wait_for_fb_wake);
  122. power_ro_attr(wait_for_fb_status);
  123. static struct attribute *g[] = {
  124. &wait_for_fb_sleep_attr.attr,
  125. &wait_for_fb_wake_attr.attr,
  126. &wait_for_fb_status_attr.attr,
  127. NULL,
  128. };
  129. static struct attribute_group attr_group = {
  130. .attrs = g,
  131. };
  132. static int __init android_power_init(void)
  133. {
  134. int ret;
  135. init_waitqueue_head(&fb_state_wq);
  136. fb_state = FB_STATE_DRAWING_OK;
  137. ret = sysfs_create_group(power_kobj, &attr_group);
  138. if (ret) {
  139. pr_err("android_power_init: sysfs_create_group failed\n");
  140. return ret;
  141. }
  142. register_early_suspend(&stop_drawing_early_suspend_desc);
  143. return 0;
  144. }
  145. static void __exit android_power_exit(void)
  146. {
  147. unregister_early_suspend(&stop_drawing_early_suspend_desc);
  148. sysfs_remove_group(power_kobj, &attr_group);
  149. }
  150. module_init(android_power_init);
  151. module_exit(android_power_exit);