Commit | Line | Data |
---|---|---|
6fa3eb70 S |
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 | ||
16 | #include <linux/earlysuspend.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/wait.h> | |
19 | ||
20 | #include "power.h" | |
21 | ||
22 | static wait_queue_head_t fb_state_wq; | |
23 | static DEFINE_SPINLOCK(fb_state_lock); | |
24 | static enum { | |
25 | FB_STATE_STOPPED_DRAWING, | |
26 | FB_STATE_REQUEST_STOP_DRAWING, | |
27 | FB_STATE_DRAWING_OK, | |
28 | } fb_state; | |
29 | ||
30 | /* tell userspace to stop drawing, wait for it to stop */ | |
31 | static void stop_drawing_early_suspend(struct early_suspend *h) | |
32 | { | |
33 | int ret; | |
34 | unsigned long irq_flags; | |
35 | ||
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); | |
39 | ||
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"); | |
45 | } | |
46 | ||
47 | /* tell userspace to start drawing */ | |
48 | static void start_drawing_late_resume(struct early_suspend *h) | |
49 | { | |
50 | unsigned long irq_flags; | |
51 | ||
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); | |
56 | } | |
57 | ||
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, | |
62 | }; | |
63 | ||
64 | static ssize_t wait_for_fb_sleep_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) | |
65 | { | |
66 | char *s = buf; | |
67 | int ret; | |
68 | ||
69 | ret = wait_event_interruptible(fb_state_wq, fb_state != FB_STATE_DRAWING_OK); | |
70 | if (ret && fb_state == FB_STATE_DRAWING_OK) | |
71 | return ret; | |
72 | else | |
73 | s += sprintf(buf, "sleeping"); | |
74 | return s - buf; | |
75 | } | |
76 | ||
77 | static ssize_t wait_for_fb_wake_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) | |
78 | { | |
79 | char *s = buf; | |
80 | int ret; | |
81 | unsigned long irq_flags; | |
82 | ||
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); | |
87 | } | |
88 | spin_unlock_irqrestore(&fb_state_lock, irq_flags); | |
89 | ||
90 | ret = wait_event_interruptible(fb_state_wq, fb_state == FB_STATE_DRAWING_OK); | |
91 | if (ret && fb_state != FB_STATE_DRAWING_OK) | |
92 | return ret; | |
93 | else | |
94 | s += sprintf(buf, "awake"); | |
95 | ||
96 | return s - buf; | |
97 | } | |
98 | ||
99 | #define power_ro_attr(_name) \ | |
100 | static struct kobj_attribute _name##_attr = { \ | |
101 | .attr = { \ | |
102 | .name = __stringify(_name), \ | |
103 | .mode = 0444, \ | |
104 | }, \ | |
105 | .show = _name##_show, \ | |
106 | .store = NULL, \ | |
107 | } | |
108 | ||
109 | power_ro_attr(wait_for_fb_sleep); | |
110 | power_ro_attr(wait_for_fb_wake); | |
111 | ||
112 | static struct attribute *g[] = { | |
113 | &wait_for_fb_sleep_attr.attr, | |
114 | &wait_for_fb_wake_attr.attr, | |
115 | NULL, | |
116 | }; | |
117 | ||
118 | static struct attribute_group attr_group = { | |
119 | .attrs = g, | |
120 | }; | |
121 | ||
122 | static int __init android_power_init(void) | |
123 | { | |
124 | int ret; | |
125 | ||
126 | init_waitqueue_head(&fb_state_wq); | |
127 | fb_state = FB_STATE_DRAWING_OK; | |
128 | ||
129 | ret = sysfs_create_group(power_kobj, &attr_group); | |
130 | if (ret) { | |
131 | pr_err("android_power_init: sysfs_create_group failed\n"); | |
132 | return ret; | |
133 | } | |
134 | ||
135 | register_early_suspend(&stop_drawing_early_suspend_desc); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | static void __exit android_power_exit(void) | |
140 | { | |
141 | unregister_early_suspend(&stop_drawing_early_suspend_desc); | |
142 | sysfs_remove_group(power_kobj, &attr_group); | |
143 | } | |
144 | module_init(android_power_init); | |
145 | module_exit(android_power_exit); |