system/corennnnn
修訂 | 3ac1162a06f284d7ea1f9a6f48b2057dcddd7d73 (tree) |
---|---|
時間 | 2016-12-09 16:22:09 |
作者 | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
libsuspend: enable earlysuspend for android-x86
@@ -29,7 +29,6 @@ | ||
29 | 29 | |
30 | 30 | #include "autosuspend_ops.h" |
31 | 31 | |
32 | -#define EARLYSUSPEND_SYS_POWER_STATE "/sys/power/state" | |
33 | 32 | #define EARLYSUSPEND_WAIT_FOR_FB_SLEEP "/sys/power/wait_for_fb_sleep" |
34 | 33 | #define EARLYSUSPEND_WAIT_FOR_FB_WAKE "/sys/power/wait_for_fb_wake" |
35 | 34 |
@@ -45,37 +44,58 @@ static enum { | ||
45 | 44 | EARLYSUSPEND_MEM, |
46 | 45 | } earlysuspend_state = EARLYSUSPEND_ON; |
47 | 46 | |
48 | -int wait_for_fb_wake(void) | |
47 | +static void log_err(const char *fmt, ...) | |
49 | 48 | { |
50 | - int err = 0; | |
51 | - char buf; | |
52 | - int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_WAKE, O_RDONLY, 0)); | |
49 | + char err[80]; | |
50 | + char buf[512]; | |
51 | + | |
52 | + strerror_r(errno, err, sizeof(err)); | |
53 | + | |
54 | + va_list ap; | |
55 | + va_start(ap, fmt); | |
56 | + vsnprintf(buf, sizeof(buf), fmt, ap); | |
57 | + va_end(ap); | |
58 | + ALOGE("Error %s: %s", buf, err); | |
59 | +} | |
60 | + | |
61 | +static int open_file(const char *file, char *buf, size_t sz, int *fd) | |
62 | +{ | |
63 | + int err; | |
64 | + int f = TEMP_FAILURE_RETRY(open(file, fd ? O_RDWR : O_RDONLY, 0)); | |
53 | 65 | // if the file doesn't exist, the error will be caught in read() below |
54 | - err = TEMP_FAILURE_RETRY(read(fd, &buf, 1)); | |
55 | - ALOGE_IF(err < 0, | |
56 | - "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno)); | |
57 | - close(fd); | |
58 | - return err < 0 ? err : 0; | |
66 | + err = TEMP_FAILURE_RETRY(read(f, buf, sz)); | |
67 | + | |
68 | + if (err < 0) { | |
69 | + log_err("opening %s", file); | |
70 | + } else { | |
71 | + err = 0; | |
72 | + } | |
73 | + | |
74 | + if (fd == NULL) { | |
75 | + close(f); | |
76 | + } else { | |
77 | + *fd = f; | |
78 | + } | |
79 | + return err; | |
80 | +} | |
81 | + | |
82 | +static int wait_for_fb_wake(void) | |
83 | +{ | |
84 | + char buf; | |
85 | + return open_file(EARLYSUSPEND_WAIT_FOR_FB_WAKE, &buf, 1, NULL); | |
59 | 86 | } |
60 | 87 | |
61 | 88 | static int wait_for_fb_sleep(void) |
62 | 89 | { |
63 | - int err = 0; | |
64 | 90 | char buf; |
65 | - int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, O_RDONLY, 0)); | |
66 | - // if the file doesn't exist, the error will be caught in read() below | |
67 | - err = TEMP_FAILURE_RETRY(read(fd, &buf, 1)); | |
68 | - ALOGE_IF(err < 0, | |
69 | - "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno)); | |
70 | - close(fd); | |
71 | - return err < 0 ? err : 0; | |
91 | + return open_file(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, &buf, 1, NULL); | |
72 | 92 | } |
73 | 93 | |
74 | 94 | static void *earlysuspend_thread_func(void __unused *arg) |
75 | 95 | { |
76 | 96 | while (1) { |
77 | 97 | if (wait_for_fb_sleep()) { |
78 | - ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread\n"); | |
98 | + ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread"); | |
79 | 99 | return NULL; |
80 | 100 | } |
81 | 101 | pthread_mutex_lock(&earlysuspend_mutex); |
@@ -84,7 +104,7 @@ static void *earlysuspend_thread_func(void __unused *arg) | ||
84 | 104 | pthread_mutex_unlock(&earlysuspend_mutex); |
85 | 105 | |
86 | 106 | if (wait_for_fb_wake()) { |
87 | - ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread\n"); | |
107 | + ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread"); | |
88 | 108 | return NULL; |
89 | 109 | } |
90 | 110 | pthread_mutex_lock(&earlysuspend_mutex); |
@@ -95,16 +115,14 @@ static void *earlysuspend_thread_func(void __unused *arg) | ||
95 | 115 | } |
96 | 116 | static int autosuspend_earlysuspend_enable(void) |
97 | 117 | { |
98 | - char buf[80]; | |
99 | 118 | int ret; |
100 | 119 | |
101 | - ALOGV("autosuspend_earlysuspend_enable\n"); | |
120 | + ALOGI("autosuspend_earlysuspend_enable"); | |
102 | 121 | |
103 | - ret = write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem)); | |
122 | + ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem))); | |
104 | 123 | if (ret < 0) { |
105 | - strerror_r(errno, buf, sizeof(buf)); | |
106 | - ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf); | |
107 | - goto err; | |
124 | + log_err("writing %s to %s", pwr_state_mem, SYS_POWER_STATE); | |
125 | + return ret; | |
108 | 126 | } |
109 | 127 | |
110 | 128 | if (wait_for_earlysuspend) { |
@@ -115,26 +133,22 @@ static int autosuspend_earlysuspend_enable(void) | ||
115 | 133 | pthread_mutex_unlock(&earlysuspend_mutex); |
116 | 134 | } |
117 | 135 | |
118 | - ALOGV("autosuspend_earlysuspend_enable done\n"); | |
136 | + ALOGD("autosuspend_earlysuspend_enable done"); | |
119 | 137 | |
120 | 138 | return 0; |
121 | - | |
122 | -err: | |
123 | - return ret; | |
124 | 139 | } |
125 | 140 | |
126 | 141 | static int autosuspend_earlysuspend_disable(void) |
127 | 142 | { |
128 | - char buf[80]; | |
129 | 143 | int ret; |
130 | 144 | |
131 | - ALOGV("autosuspend_earlysuspend_disable\n"); | |
145 | + ALOGI("autosuspend_earlysuspend_disable"); | |
132 | 146 | |
133 | 147 | ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on))); |
134 | 148 | if (ret < 0) { |
135 | - strerror_r(errno, buf, sizeof(buf)); | |
136 | - ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf); | |
137 | - goto err; | |
149 | +#if DEBUG | |
150 | + log_err("writing %s to %s", pwr_state_on, SYS_POWER_STATE); | |
151 | +#endif | |
138 | 152 | } |
139 | 153 | |
140 | 154 | if (wait_for_earlysuspend) { |
@@ -145,12 +159,9 @@ static int autosuspend_earlysuspend_disable(void) | ||
145 | 159 | pthread_mutex_unlock(&earlysuspend_mutex); |
146 | 160 | } |
147 | 161 | |
148 | - ALOGV("autosuspend_earlysuspend_disable done\n"); | |
162 | + ALOGD("autosuspend_earlysuspend_disable done"); | |
149 | 163 | |
150 | 164 | return 0; |
151 | - | |
152 | -err: | |
153 | - return ret; | |
154 | 165 | } |
155 | 166 | |
156 | 167 | struct autosuspend_ops autosuspend_earlysuspend_ops = { |
@@ -160,26 +171,26 @@ struct autosuspend_ops autosuspend_earlysuspend_ops = { | ||
160 | 171 | |
161 | 172 | void start_earlysuspend_thread(void) |
162 | 173 | { |
163 | - char buf[80]; | |
164 | 174 | int ret; |
165 | 175 | |
166 | 176 | ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK); |
167 | 177 | if (ret < 0) { |
178 | + log_err("accessing %s", EARLYSUSPEND_WAIT_FOR_FB_SLEEP); | |
168 | 179 | return; |
169 | 180 | } |
170 | 181 | |
171 | 182 | ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK); |
172 | 183 | if (ret < 0) { |
184 | + log_err("accessing %s", EARLYSUSPEND_WAIT_FOR_FB_WAKE); | |
173 | 185 | return; |
174 | 186 | } |
175 | 187 | |
176 | 188 | wait_for_fb_wake(); |
177 | 189 | |
178 | - ALOGI("Starting early suspend unblocker thread\n"); | |
190 | + ALOGI("Starting early suspend unblocker thread"); | |
179 | 191 | ret = pthread_create(&earlysuspend_thread, NULL, earlysuspend_thread_func, NULL); |
180 | 192 | if (ret) { |
181 | - strerror_r(errno, buf, sizeof(buf)); | |
182 | - ALOGE("Error creating thread: %s\n", buf); | |
193 | + log_err("creating thread"); | |
183 | 194 | return; |
184 | 195 | } |
185 | 196 |
@@ -188,31 +199,18 @@ void start_earlysuspend_thread(void) | ||
188 | 199 | |
189 | 200 | struct autosuspend_ops *autosuspend_earlysuspend_init(void) |
190 | 201 | { |
191 | - char buf[80]; | |
192 | 202 | int ret; |
203 | + char pwr_st[128]; | |
193 | 204 | |
194 | - sPowerStatefd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR)); | |
195 | - | |
196 | - if (sPowerStatefd < 0) { | |
197 | - strerror_r(errno, buf, sizeof(buf)); | |
198 | - ALOGW("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf); | |
199 | - return NULL; | |
200 | - } | |
201 | - | |
202 | - ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, "on", 2)); | |
205 | + ret = open_file(SYS_POWER_STATE, pwr_st, sizeof(pwr_st), &sPowerStatefd); | |
203 | 206 | if (ret < 0) { |
204 | - strerror_r(errno, buf, sizeof(buf)); | |
205 | - ALOGW("Error writing 'on' to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf); | |
206 | - goto err_write; | |
207 | + close(sPowerStatefd); | |
208 | + return NULL; | |
207 | 209 | } |
208 | 210 | |
209 | - ALOGI("Selected early suspend\n"); | |
211 | + ALOGI("Selected early suspend"); | |
210 | 212 | |
211 | 213 | start_earlysuspend_thread(); |
212 | 214 | |
213 | 215 | return &autosuspend_earlysuspend_ops; |
214 | - | |
215 | -err_write: | |
216 | - close(sPowerStatefd); | |
217 | - return NULL; | |
218 | 216 | } |
@@ -17,6 +17,8 @@ | ||
17 | 17 | #ifndef _LIBSUSPEND_AUTOSUSPEND_OPS_H_ |
18 | 18 | #define _LIBSUSPEND_AUTOSUSPEND_OPS_H_ |
19 | 19 | |
20 | +#define SYS_POWER_STATE "/sys/power/state" | |
21 | + | |
20 | 22 | struct autosuspend_ops { |
21 | 23 | int (*enable)(void); |
22 | 24 | int (*disable)(void); |
@@ -31,7 +31,6 @@ | ||
31 | 31 | |
32 | 32 | #include "autosuspend_ops.h" |
33 | 33 | |
34 | -#define SYS_POWER_STATE "/sys/power/state" | |
35 | 34 | #define SYS_POWER_WAKEUP_COUNT "/sys/power/wakeup_count" |
36 | 35 | |
37 | 36 | static int state_fd; |