blob: 809ee825eefb788a1343723dcbd1bff0e8ea46b8 [file] [log] [blame]
Colin Crossa2582c22012-05-03 17:30:16 -07001/*
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
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070017#define LOG_TAG "libsuspend"
18
Colin Crossa2582c22012-05-03 17:30:16 -070019#include <errno.h>
20#include <fcntl.h>
Colin Crossf25dd872012-06-14 12:55:47 -070021#include <pthread.h>
22#include <stdbool.h>
Colin Crossa2582c22012-05-03 17:30:16 -070023#include <stddef.h>
24#include <string.h>
Colin Crossa2582c22012-05-03 17:30:16 -070025#include <sys/stat.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070026#include <sys/types.h>
Colin Crossa2582c22012-05-03 17:30:16 -070027#include <unistd.h>
28
Mark Salyzyn30f991f2017-01-10 13:19:54 -080029#include <log/log.h>
Colin Crossa2582c22012-05-03 17:30:16 -070030
31#include "autosuspend_ops.h"
32
33#define EARLYSUSPEND_SYS_POWER_STATE "/sys/power/state"
34#define EARLYSUSPEND_WAIT_FOR_FB_SLEEP "/sys/power/wait_for_fb_sleep"
35#define EARLYSUSPEND_WAIT_FOR_FB_WAKE "/sys/power/wait_for_fb_wake"
36
Colin Crossa2582c22012-05-03 17:30:16 -070037static int sPowerStatefd;
38static const char *pwr_state_mem = "mem";
39static const char *pwr_state_on = "on";
Colin Cross2146b7f2012-06-07 14:12:54 -070040static pthread_t earlysuspend_thread;
Colin Crossf25dd872012-06-14 12:55:47 -070041static pthread_mutex_t earlysuspend_mutex = PTHREAD_MUTEX_INITIALIZER;
42static pthread_cond_t earlysuspend_cond = PTHREAD_COND_INITIALIZER;
43static bool wait_for_earlysuspend;
44static enum {
45 EARLYSUSPEND_ON,
46 EARLYSUSPEND_MEM,
47} earlysuspend_state = EARLYSUSPEND_ON;
Colin Crossa2582c22012-05-03 17:30:16 -070048
Colin Cross2146b7f2012-06-07 14:12:54 -070049int wait_for_fb_wake(void)
50{
51 int err = 0;
52 char buf;
Jeff Brown0446e162015-05-15 14:56:03 -070053 int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_WAKE, O_RDONLY, 0));
Colin Cross2146b7f2012-06-07 14:12:54 -070054 // if the file doesn't exist, the error will be caught in read() below
Jeff Brown0446e162015-05-15 14:56:03 -070055 err = TEMP_FAILURE_RETRY(read(fd, &buf, 1));
Colin Cross2146b7f2012-06-07 14:12:54 -070056 ALOGE_IF(err < 0,
57 "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno));
58 close(fd);
59 return err < 0 ? err : 0;
60}
61
62static int wait_for_fb_sleep(void)
63{
64 int err = 0;
65 char buf;
Jeff Brown0446e162015-05-15 14:56:03 -070066 int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, O_RDONLY, 0));
Colin Cross2146b7f2012-06-07 14:12:54 -070067 // if the file doesn't exist, the error will be caught in read() below
Jeff Brown0446e162015-05-15 14:56:03 -070068 err = TEMP_FAILURE_RETRY(read(fd, &buf, 1));
Colin Cross2146b7f2012-06-07 14:12:54 -070069 ALOGE_IF(err < 0,
70 "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno));
71 close(fd);
72 return err < 0 ? err : 0;
73}
74
Sasha Levitskiycdc1cfb2014-04-10 17:10:21 -070075static void *earlysuspend_thread_func(void __unused *arg)
Colin Cross2146b7f2012-06-07 14:12:54 -070076{
Colin Cross2146b7f2012-06-07 14:12:54 -070077 while (1) {
78 if (wait_for_fb_sleep()) {
79 ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread\n");
80 return NULL;
81 }
Colin Crossf25dd872012-06-14 12:55:47 -070082 pthread_mutex_lock(&earlysuspend_mutex);
83 earlysuspend_state = EARLYSUSPEND_MEM;
84 pthread_cond_signal(&earlysuspend_cond);
85 pthread_mutex_unlock(&earlysuspend_mutex);
86
Colin Cross2146b7f2012-06-07 14:12:54 -070087 if (wait_for_fb_wake()) {
88 ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread\n");
89 return NULL;
90 }
Colin Crossf25dd872012-06-14 12:55:47 -070091 pthread_mutex_lock(&earlysuspend_mutex);
92 earlysuspend_state = EARLYSUSPEND_ON;
93 pthread_cond_signal(&earlysuspend_cond);
94 pthread_mutex_unlock(&earlysuspend_mutex);
Colin Cross2146b7f2012-06-07 14:12:54 -070095 }
96}
Colin Crossa2582c22012-05-03 17:30:16 -070097static int autosuspend_earlysuspend_enable(void)
98{
99 char buf[80];
100 int ret;
101
102 ALOGV("autosuspend_earlysuspend_enable\n");
103
104 ret = write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem));
105 if (ret < 0) {
106 strerror_r(errno, buf, sizeof(buf));
107 ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
108 goto err;
109 }
110
Colin Crossf25dd872012-06-14 12:55:47 -0700111 if (wait_for_earlysuspend) {
112 pthread_mutex_lock(&earlysuspend_mutex);
113 while (earlysuspend_state != EARLYSUSPEND_MEM) {
114 pthread_cond_wait(&earlysuspend_cond, &earlysuspend_mutex);
115 }
116 pthread_mutex_unlock(&earlysuspend_mutex);
117 }
118
Colin Crossa2582c22012-05-03 17:30:16 -0700119 ALOGV("autosuspend_earlysuspend_enable done\n");
120
121 return 0;
122
123err:
124 return ret;
125}
126
127static int autosuspend_earlysuspend_disable(void)
128{
129 char buf[80];
130 int ret;
131
132 ALOGV("autosuspend_earlysuspend_disable\n");
133
Jeff Brown0446e162015-05-15 14:56:03 -0700134 ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on)));
Colin Crossa2582c22012-05-03 17:30:16 -0700135 if (ret < 0) {
136 strerror_r(errno, buf, sizeof(buf));
137 ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
138 goto err;
139 }
140
Colin Crossf25dd872012-06-14 12:55:47 -0700141 if (wait_for_earlysuspend) {
142 pthread_mutex_lock(&earlysuspend_mutex);
143 while (earlysuspend_state != EARLYSUSPEND_ON) {
144 pthread_cond_wait(&earlysuspend_cond, &earlysuspend_mutex);
145 }
146 pthread_mutex_unlock(&earlysuspend_mutex);
147 }
148
Colin Crossa2582c22012-05-03 17:30:16 -0700149 ALOGV("autosuspend_earlysuspend_disable done\n");
150
151 return 0;
152
153err:
154 return ret;
155}
156
157struct autosuspend_ops autosuspend_earlysuspend_ops = {
158 .enable = autosuspend_earlysuspend_enable,
159 .disable = autosuspend_earlysuspend_disable,
160};
161
Colin Cross2146b7f2012-06-07 14:12:54 -0700162void start_earlysuspend_thread(void)
Colin Crossa2582c22012-05-03 17:30:16 -0700163{
164 char buf[80];
165 int ret;
166
167 ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK);
168 if (ret < 0) {
Colin Cross2146b7f2012-06-07 14:12:54 -0700169 return;
Colin Crossa2582c22012-05-03 17:30:16 -0700170 }
171
172 ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK);
173 if (ret < 0) {
Colin Cross2146b7f2012-06-07 14:12:54 -0700174 return;
Colin Crossa2582c22012-05-03 17:30:16 -0700175 }
176
Colin Crossf25dd872012-06-14 12:55:47 -0700177 wait_for_fb_wake();
178
Colin Cross2146b7f2012-06-07 14:12:54 -0700179 ALOGI("Starting early suspend unblocker thread\n");
180 ret = pthread_create(&earlysuspend_thread, NULL, earlysuspend_thread_func, NULL);
181 if (ret) {
Colin Crossf25dd872012-06-14 12:55:47 -0700182 strerror_r(errno, buf, sizeof(buf));
Colin Cross2146b7f2012-06-07 14:12:54 -0700183 ALOGE("Error creating thread: %s\n", buf);
Colin Crossf25dd872012-06-14 12:55:47 -0700184 return;
Colin Cross2146b7f2012-06-07 14:12:54 -0700185 }
Colin Crossf25dd872012-06-14 12:55:47 -0700186
187 wait_for_earlysuspend = true;
Colin Cross2146b7f2012-06-07 14:12:54 -0700188}
189
190struct autosuspend_ops *autosuspend_earlysuspend_init(void)
191{
192 char buf[80];
193 int ret;
194
Jeff Brown0446e162015-05-15 14:56:03 -0700195 sPowerStatefd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR));
Colin Crossa2582c22012-05-03 17:30:16 -0700196
197 if (sPowerStatefd < 0) {
198 strerror_r(errno, buf, sizeof(buf));
Colin Cross2146b7f2012-06-07 14:12:54 -0700199 ALOGW("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
Colin Crossa2582c22012-05-03 17:30:16 -0700200 return NULL;
201 }
202
Jeff Brown0446e162015-05-15 14:56:03 -0700203 ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, "on", 2));
Colin Cross2146b7f2012-06-07 14:12:54 -0700204 if (ret < 0) {
205 strerror_r(errno, buf, sizeof(buf));
206 ALOGW("Error writing 'on' to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
207 goto err_write;
208 }
209
Colin Crossa2582c22012-05-03 17:30:16 -0700210 ALOGI("Selected early suspend\n");
Colin Cross2146b7f2012-06-07 14:12:54 -0700211
212 start_earlysuspend_thread();
213
Colin Crossa2582c22012-05-03 17:30:16 -0700214 return &autosuspend_earlysuspend_ops;
Colin Cross2146b7f2012-06-07 14:12:54 -0700215
216err_write:
217 close(sPowerStatefd);
218 return NULL;
Colin Crossa2582c22012-05-03 17:30:16 -0700219}