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
,
42 fb_state
== FB_STATE_STOPPED_DRAWING
,
44 if (unlikely(fb_state
!= FB_STATE_STOPPED_DRAWING
))
45 pr_warning("stop_drawing_early_suspend: timeout waiting for "
46 "userspace to stop drawing\n");
49 /* tell userspace to start drawing */
50 void start_drawing_late_resume(struct early_suspend
*h
)
52 unsigned long irq_flags
;
54 spin_lock_irqsave(&fb_state_lock
, irq_flags
);
55 fb_state
= FB_STATE_DRAWING_OK
;
56 spin_unlock_irqrestore(&fb_state_lock
, irq_flags
);
57 wake_up(&fb_state_wq
);
60 static struct early_suspend stop_drawing_early_suspend_desc
= {
61 .level
= EARLY_SUSPEND_LEVEL_STOP_DRAWING
,
62 .suspend
= stop_drawing_early_suspend
,
63 .resume
= start_drawing_late_resume
,
66 static ssize_t
wait_for_fb_sleep_show(struct kobject
*kobj
,
67 struct kobj_attribute
*attr
, char *buf
)
72 ret
= wait_event_interruptible(fb_state_wq
,
73 fb_state
!= FB_STATE_DRAWING_OK
);
74 if (ret
&& fb_state
== FB_STATE_DRAWING_OK
)
77 s
+= sprintf(buf
, "sleeping");
81 static ssize_t
wait_for_fb_wake_show(struct kobject
*kobj
,
82 struct kobj_attribute
*attr
, char *buf
)
86 unsigned long irq_flags
;
88 spin_lock_irqsave(&fb_state_lock
, irq_flags
);
89 if (fb_state
== FB_STATE_REQUEST_STOP_DRAWING
) {
90 fb_state
= FB_STATE_STOPPED_DRAWING
;
91 wake_up(&fb_state_wq
);
93 spin_unlock_irqrestore(&fb_state_lock
, irq_flags
);
95 ret
= wait_event_interruptible(fb_state_wq
,
96 fb_state
== FB_STATE_DRAWING_OK
);
97 if (ret
&& fb_state
!= FB_STATE_DRAWING_OK
)
100 s
+= sprintf(buf
, "awake");
105 #define power_ro_attr(_name) \
106 static struct kobj_attribute _name##_attr = { \
108 .name = __stringify(_name), \
111 .show = _name##_show, \
115 power_ro_attr(wait_for_fb_sleep
);
116 power_ro_attr(wait_for_fb_wake
);
118 static struct attribute
*g
[] = {
119 &wait_for_fb_sleep_attr
.attr
,
120 &wait_for_fb_wake_attr
.attr
,
124 static struct attribute_group attr_group
= {
128 static int __init
android_power_init(void)
132 init_waitqueue_head(&fb_state_wq
);
133 fb_state
= FB_STATE_DRAWING_OK
;
135 ret
= sysfs_create_group(power_kobj
, &attr_group
);
137 pr_err("android_power_init: sysfs_create_group failed\n");
141 register_early_suspend(&stop_drawing_early_suspend_desc
);
145 static void __exit
android_power_exit(void)
147 unregister_early_suspend(&stop_drawing_early_suspend_desc
);
148 sysfs_remove_group(power_kobj
, &attr_group
);
151 module_init(android_power_init
);
152 module_exit(android_power_exit
);