fbearlysuspend.c 3.7 KB

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