• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <pthread.h>
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 
27 #define LOG_TAG "libsuspend"
28 #include <cutils/log.h>
29 
30 #include "autosuspend_ops.h"
31 
32 #define EARLYSUSPEND_SYS_POWER_STATE "/sys/power/state"
33 #define EARLYSUSPEND_WAIT_FOR_FB_SLEEP "/sys/power/wait_for_fb_sleep"
34 #define EARLYSUSPEND_WAIT_FOR_FB_WAKE "/sys/power/wait_for_fb_wake"
35 
36 static int sPowerStatefd;
37 static const char *pwr_state_mem = "mem";
38 static const char *pwr_state_on = "on";
39 static pthread_t earlysuspend_thread;
40 static pthread_mutex_t earlysuspend_mutex = PTHREAD_MUTEX_INITIALIZER;
41 static pthread_cond_t earlysuspend_cond = PTHREAD_COND_INITIALIZER;
42 static bool wait_for_earlysuspend;
43 static enum {
44     EARLYSUSPEND_ON,
45     EARLYSUSPEND_MEM,
46 } earlysuspend_state = EARLYSUSPEND_ON;
47 
wait_for_fb_wake(void)48 int wait_for_fb_wake(void)
49 {
50     int err = 0;
51     char buf;
52     int fd = open(EARLYSUSPEND_WAIT_FOR_FB_WAKE, O_RDONLY, 0);
53     // if the file doesn't exist, the error will be caught in read() below
54     do {
55         err = read(fd, &buf, 1);
56     } while (err < 0 && errno == EINTR);
57     ALOGE_IF(err < 0,
58             "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno));
59     close(fd);
60     return err < 0 ? err : 0;
61 }
62 
wait_for_fb_sleep(void)63 static int wait_for_fb_sleep(void)
64 {
65     int err = 0;
66     char buf;
67     int fd = open(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, O_RDONLY, 0);
68     // if the file doesn't exist, the error will be caught in read() below
69     do {
70         err = read(fd, &buf, 1);
71     } while (err < 0 && errno == EINTR);
72     ALOGE_IF(err < 0,
73             "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno));
74     close(fd);
75     return err < 0 ? err : 0;
76 }
77 
earlysuspend_thread_func(void __unused * arg)78 static void *earlysuspend_thread_func(void __unused *arg)
79 {
80     while (1) {
81         if (wait_for_fb_sleep()) {
82             ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread\n");
83             return NULL;
84         }
85         pthread_mutex_lock(&earlysuspend_mutex);
86         earlysuspend_state = EARLYSUSPEND_MEM;
87         pthread_cond_signal(&earlysuspend_cond);
88         pthread_mutex_unlock(&earlysuspend_mutex);
89 
90         if (wait_for_fb_wake()) {
91             ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread\n");
92             return NULL;
93         }
94         pthread_mutex_lock(&earlysuspend_mutex);
95         earlysuspend_state = EARLYSUSPEND_ON;
96         pthread_cond_signal(&earlysuspend_cond);
97         pthread_mutex_unlock(&earlysuspend_mutex);
98     }
99 }
autosuspend_earlysuspend_enable(void)100 static int autosuspend_earlysuspend_enable(void)
101 {
102     char buf[80];
103     int ret;
104 
105     ALOGV("autosuspend_earlysuspend_enable\n");
106 
107     ret = write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem));
108     if (ret < 0) {
109         strerror_r(errno, buf, sizeof(buf));
110         ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
111         goto err;
112     }
113 
114     if (wait_for_earlysuspend) {
115         pthread_mutex_lock(&earlysuspend_mutex);
116         while (earlysuspend_state != EARLYSUSPEND_MEM) {
117             pthread_cond_wait(&earlysuspend_cond, &earlysuspend_mutex);
118         }
119         pthread_mutex_unlock(&earlysuspend_mutex);
120     }
121 
122     ALOGV("autosuspend_earlysuspend_enable done\n");
123 
124     return 0;
125 
126 err:
127     return ret;
128 }
129 
autosuspend_earlysuspend_disable(void)130 static int autosuspend_earlysuspend_disable(void)
131 {
132     char buf[80];
133     int ret;
134 
135     ALOGV("autosuspend_earlysuspend_disable\n");
136 
137     ret = write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on));
138     if (ret < 0) {
139         strerror_r(errno, buf, sizeof(buf));
140         ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
141         goto err;
142     }
143 
144     if (wait_for_earlysuspend) {
145         pthread_mutex_lock(&earlysuspend_mutex);
146         while (earlysuspend_state != EARLYSUSPEND_ON) {
147             pthread_cond_wait(&earlysuspend_cond, &earlysuspend_mutex);
148         }
149         pthread_mutex_unlock(&earlysuspend_mutex);
150     }
151 
152     ALOGV("autosuspend_earlysuspend_disable done\n");
153 
154     return 0;
155 
156 err:
157     return ret;
158 }
159 
160 struct autosuspend_ops autosuspend_earlysuspend_ops = {
161         .enable = autosuspend_earlysuspend_enable,
162         .disable = autosuspend_earlysuspend_disable,
163 };
164 
start_earlysuspend_thread(void)165 void start_earlysuspend_thread(void)
166 {
167     char buf[80];
168     int ret;
169 
170     ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK);
171     if (ret < 0) {
172         return;
173     }
174 
175     ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK);
176     if (ret < 0) {
177         return;
178     }
179 
180     wait_for_fb_wake();
181 
182     ALOGI("Starting early suspend unblocker thread\n");
183     ret = pthread_create(&earlysuspend_thread, NULL, earlysuspend_thread_func, NULL);
184     if (ret) {
185         strerror_r(errno, buf, sizeof(buf));
186         ALOGE("Error creating thread: %s\n", buf);
187         return;
188     }
189 
190     wait_for_earlysuspend = true;
191 }
192 
autosuspend_earlysuspend_init(void)193 struct autosuspend_ops *autosuspend_earlysuspend_init(void)
194 {
195     char buf[80];
196     int ret;
197 
198     sPowerStatefd = open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR);
199 
200     if (sPowerStatefd < 0) {
201         strerror_r(errno, buf, sizeof(buf));
202         ALOGW("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
203         return NULL;
204     }
205 
206     ret = write(sPowerStatefd, "on", 2);
207     if (ret < 0) {
208         strerror_r(errno, buf, sizeof(buf));
209         ALOGW("Error writing 'on' to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
210         goto err_write;
211     }
212 
213     ALOGI("Selected early suspend\n");
214 
215     start_earlysuspend_thread();
216 
217     return &autosuspend_earlysuspend_ops;
218 
219 err_write:
220     close(sPowerStatefd);
221     return NULL;
222 }
223