blob: 3793a699ede29d81bbad220caf3db8f47546e0d1 [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
17#include <errno.h>
18#include <fcntl.h>
Colin Crossf25dd872012-06-14 12:55:47 -070019#include <pthread.h>
20#include <stdbool.h>
Colin Crossa2582c22012-05-03 17:30:16 -070021#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
Colin Crossa2582c22012-05-03 17:30:16 -070036static int sPowerStatefd;
37static const char *pwr_state_mem = "mem";
38static const char *pwr_state_on = "on";
Colin Cross2146b7f2012-06-07 14:12:54 -070039static pthread_t earlysuspend_thread;
Colin Crossf25dd872012-06-14 12:55:47 -070040static pthread_mutex_t earlysuspend_mutex = PTHREAD_MUTEX_INITIALIZER;
41static pthread_cond_t earlysuspend_cond = PTHREAD_COND_INITIALIZER;
42static bool wait_for_earlysuspend;
43static enum {
44 EARLYSUSPEND_ON,
45 EARLYSUSPEND_MEM,
46} earlysuspend_state = EARLYSUSPEND_ON;
Colin Crossa2582c22012-05-03 17:30:16 -070047
Colin Cross2146b7f2012-06-07 14:12:54 -070048int wait_for_fb_wake(void)
49{
50 int err = 0;
51 char buf;
Jeff Brown0446e162015-05-15 14:56:03 -070052 int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_WAKE, O_RDONLY, 0));
Colin Cross2146b7f2012-06-07 14:12:54 -070053 // if the file doesn't exist, the error will be caught in read() below
Jeff Brown0446e162015-05-15 14:56:03 -070054 err = TEMP_FAILURE_RETRY(read(fd, &buf, 1));
Colin Cross2146b7f2012-06-07 14:12:54 -070055 ALOGE_IF(err < 0,
56 "*** ANDROID_WAIT_FOR_FB_WAKE failed (%s)", strerror(errno));
57 close(fd);
58 return err < 0 ? err : 0;
59}
60
61static int wait_for_fb_sleep(void)
62{
63 int err = 0;
64 char buf;
Jeff Brown0446e162015-05-15 14:56:03 -070065 int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, O_RDONLY, 0));
Colin Cross2146b7f2012-06-07 14:12:54 -070066 // if the file doesn't exist, the error will be caught in read() below
Jeff Brown0446e162015-05-15 14:56:03 -070067 err = TEMP_FAILURE_RETRY(read(fd, &buf, 1));
Colin Cross2146b7f2012-06-07 14:12:54 -070068 ALOGE_IF(err < 0,
69 "*** ANDROID_WAIT_FOR_FB_SLEEP failed (%s)", strerror(errno));
70 close(fd);
71 return err < 0 ? err : 0;
72}
73
Sasha Levitskiycdc1cfb2014-04-10 17:10:21 -070074static void *earlysuspend_thread_func(void __unused *arg)
Colin Cross2146b7f2012-06-07 14:12:54 -070075{
Colin Cross2146b7f2012-06-07 14:12:54 -070076 while (1) {
77 if (wait_for_fb_sleep()) {
78 ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread\n");
79 return NULL;
80 }
Colin Crossf25dd872012-06-14 12:55:47 -070081 pthread_mutex_lock(&earlysuspend_mutex);
82 earlysuspend_state = EARLYSUSPEND_MEM;
83 pthread_cond_signal(&earlysuspend_cond);
84 pthread_mutex_unlock(&earlysuspend_mutex);
85
Colin Cross2146b7f2012-06-07 14:12:54 -070086 if (wait_for_fb_wake()) {
87 ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread\n");
88 return NULL;
89 }
Colin Crossf25dd872012-06-14 12:55:47 -070090 pthread_mutex_lock(&earlysuspend_mutex);
91 earlysuspend_state = EARLYSUSPEND_ON;
92 pthread_cond_signal(&earlysuspend_cond);
93 pthread_mutex_unlock(&earlysuspend_mutex);
Colin Cross2146b7f2012-06-07 14:12:54 -070094 }
95}
Colin Crossa2582c22012-05-03 17:30:16 -070096static int autosuspend_earlysuspend_enable(void)
97{
98 char buf[80];
99 int ret;
100
101 ALOGV("autosuspend_earlysuspend_enable\n");
102
103 ret = write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem));
104 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;
108 }
109
Colin Crossf25dd872012-06-14 12:55:47 -0700110 if (wait_for_earlysuspend) {
111 pthread_mutex_lock(&earlysuspend_mutex);
112 while (earlysuspend_state != EARLYSUSPEND_MEM) {
113 pthread_cond_wait(&earlysuspend_cond, &earlysuspend_mutex);
114 }
115 pthread_mutex_unlock(&earlysuspend_mutex);
116 }
117
Colin Crossa2582c22012-05-03 17:30:16 -0700118 ALOGV("autosuspend_earlysuspend_enable done\n");
119
120 return 0;
121
122err:
123 return ret;
124}
125
126static int autosuspend_earlysuspend_disable(void)
127{
128 char buf[80];
129 int ret;
130
131 ALOGV("autosuspend_earlysuspend_disable\n");
132
Jeff Brown0446e162015-05-15 14:56:03 -0700133 ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on)));
Colin Crossa2582c22012-05-03 17:30:16 -0700134 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;
138 }
139
Colin Crossf25dd872012-06-14 12:55:47 -0700140 if (wait_for_earlysuspend) {
141 pthread_mutex_lock(&earlysuspend_mutex);
142 while (earlysuspend_state != EARLYSUSPEND_ON) {
143 pthread_cond_wait(&earlysuspend_cond, &earlysuspend_mutex);
144 }
145 pthread_mutex_unlock(&earlysuspend_mutex);
146 }
147
Colin Crossa2582c22012-05-03 17:30:16 -0700148 ALOGV("autosuspend_earlysuspend_disable done\n");
149
150 return 0;
151
152err:
153 return ret;
154}
155
156struct autosuspend_ops autosuspend_earlysuspend_ops = {
157 .enable = autosuspend_earlysuspend_enable,
158 .disable = autosuspend_earlysuspend_disable,
159};
160
Colin Cross2146b7f2012-06-07 14:12:54 -0700161void start_earlysuspend_thread(void)
Colin Crossa2582c22012-05-03 17:30:16 -0700162{
163 char buf[80];
164 int ret;
165
166 ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK);
167 if (ret < 0) {
Colin Cross2146b7f2012-06-07 14:12:54 -0700168 return;
Colin Crossa2582c22012-05-03 17:30:16 -0700169 }
170
171 ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK);
172 if (ret < 0) {
Colin Cross2146b7f2012-06-07 14:12:54 -0700173 return;
Colin Crossa2582c22012-05-03 17:30:16 -0700174 }
175
Colin Crossf25dd872012-06-14 12:55:47 -0700176 wait_for_fb_wake();
177
Colin Cross2146b7f2012-06-07 14:12:54 -0700178 ALOGI("Starting early suspend unblocker thread\n");
179 ret = pthread_create(&earlysuspend_thread, NULL, earlysuspend_thread_func, NULL);
180 if (ret) {
Colin Crossf25dd872012-06-14 12:55:47 -0700181 strerror_r(errno, buf, sizeof(buf));
Colin Cross2146b7f2012-06-07 14:12:54 -0700182 ALOGE("Error creating thread: %s\n", buf);
Colin Crossf25dd872012-06-14 12:55:47 -0700183 return;
Colin Cross2146b7f2012-06-07 14:12:54 -0700184 }
Colin Crossf25dd872012-06-14 12:55:47 -0700185
186 wait_for_earlysuspend = true;
Colin Cross2146b7f2012-06-07 14:12:54 -0700187}
188
189struct autosuspend_ops *autosuspend_earlysuspend_init(void)
190{
191 char buf[80];
192 int ret;
193
Jeff Brown0446e162015-05-15 14:56:03 -0700194 sPowerStatefd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR));
Colin Crossa2582c22012-05-03 17:30:16 -0700195
196 if (sPowerStatefd < 0) {
197 strerror_r(errno, buf, sizeof(buf));
Colin Cross2146b7f2012-06-07 14:12:54 -0700198 ALOGW("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
Colin Crossa2582c22012-05-03 17:30:16 -0700199 return NULL;
200 }
201
Jeff Brown0446e162015-05-15 14:56:03 -0700202 ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, "on", 2));
Colin Cross2146b7f2012-06-07 14:12:54 -0700203 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 }
208
Colin Crossa2582c22012-05-03 17:30:16 -0700209 ALOGI("Selected early suspend\n");
Colin Cross2146b7f2012-06-07 14:12:54 -0700210
211 start_earlysuspend_thread();
212
Colin Crossa2582c22012-05-03 17:30:16 -0700213 return &autosuspend_earlysuspend_ops;
Colin Cross2146b7f2012-06-07 14:12:54 -0700214
215err_write:
216 close(sPowerStatefd);
217 return NULL;
Colin Crossa2582c22012-05-03 17:30:16 -0700218}