1 /* kernel/power/fbearlysuspend.c
3 * Copyright (C) 2005-2008 Google, Inc.
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.
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.
16 #include <linux/earlysuspend.h>
17 #include <linux/module.h>
18 #include <linux/wait.h>
22 static wait_queue_head_t fb_state_wq
;
23 static DEFINE_SPINLOCK(fb_state_lock
);
25 FB_STATE_STOPPED_DRAWING
,
26 FB_STATE_REQUEST_STOP_DRAWING
,
30 /* tell userspace to stop drawing, wait for it to stop */
31 static void stop_drawing_early_suspend(struct early_suspend
*h
)
34 unsigned long irq_flags
;
36 spin_lock_irqsave(&fb_state_lock
, irq_flags
);
37 fb_state
= FB_STATE_REQUEST_STOP_DRAWING
;
38 spin_unlock_irqrestore(&fb_state_lock
, irq_flags
);
40 wake_up_all(&fb_state_wq
);
41 ret
= wait_event_timeout(fb_state_wq
, fb_state
== FB_STATE_STOPPED_DRAWING
, HZ
);
42 if (unlikely(fb_state
!= FB_STATE_STOPPED_DRAWING
))
43 pr_warn("stop_drawing_early_suspend: timeout waiting for "
44 "userspace to stop drawing\n");
47 /* tell userspace to start drawing */
48 static void start_drawing_late_resume(struct early_suspend
*h
)
50 unsigned long irq_flags
;
52 spin_lock_irqsave(&fb_state_lock
, irq_flags
);
53 fb_state
= FB_STATE_DRAWING_OK
;
54 spin_unlock_irqrestore(&fb_state_lock
, irq_flags
);
55 wake_up(&fb_state_wq
);
58 static struct early_suspend stop_drawing_early_suspend_desc
= {
59 .level
= EARLY_SUSPEND_LEVEL_STOP_DRAWING
,
60 .suspend
= stop_drawing_early_suspend
,
61 .resume
= start_drawing_late_resume
,
64 static ssize_t
wait_for_fb_sleep_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
69 ret
= wait_event_interruptible(fb_state_wq
, fb_state
!= FB_STATE_DRAWING_OK
);
70 if (ret
&& fb_state
== FB_STATE_DRAWING_OK
)
73 s
+= sprintf(buf
, "sleeping");
77 static ssize_t
wait_for_fb_wake_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
81 unsigned long irq_flags
;
83 spin_lock_irqsave(&fb_state_lock
, irq_flags
);
84 if (fb_state
== FB_STATE_REQUEST_STOP_DRAWING
) {
85 fb_state
= FB_STATE_STOPPED_DRAWING
;
86 wake_up(&fb_state_wq
);
88 spin_unlock_irqrestore(&fb_state_lock
, irq_flags
);
90 ret
= wait_event_interruptible(fb_state_wq
, fb_state
== FB_STATE_DRAWING_OK
);
91 if (ret
&& fb_state
!= FB_STATE_DRAWING_OK
)
94 s
+= sprintf(buf
, "awake");
99 #define power_ro_attr(_name) \
100 static struct kobj_attribute _name##_attr = { \
102 .name = __stringify(_name), \
105 .show = _name##_show, \
109 power_ro_attr(wait_for_fb_sleep
);
110 power_ro_attr(wait_for_fb_wake
);
112 static struct attribute
*g
[] = {
113 &wait_for_fb_sleep_attr
.attr
,
114 &wait_for_fb_wake_attr
.attr
,
118 static struct attribute_group attr_group
= {
122 static int __init
android_power_init(void)
126 init_waitqueue_head(&fb_state_wq
);
127 fb_state
= FB_STATE_DRAWING_OK
;
129 ret
= sysfs_create_group(power_kobj
, &attr_group
);
131 pr_err("android_power_init: sysfs_create_group failed\n");
135 register_early_suspend(&stop_drawing_early_suspend_desc
);
139 static void __exit
android_power_exit(void)
141 unregister_early_suspend(&stop_drawing_early_suspend_desc
);
142 sysfs_remove_group(power_kobj
, &attr_group
);
144 module_init(android_power_init
);
145 module_exit(android_power_exit
);