blob: 2bece4c3b8d8c093171e6f548847d345a106fbf3 [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;
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
63static 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
Sasha Levitskiycdc1cfb2014-04-10 17:10:21 -070078static void *earlysuspend_thread_func(void __unused *arg)
Colin Cross2146b7f2012-06-07 14:12:54 -070079{
Colin Cross2146b7f2012-06-07 14:12:54 -070080 while (1) {
81 if (wait_for_fb_sleep()) {
82 ALOGE("Failed reading wait_for_fb_sleep, exiting earlysuspend thread\n");
83 return NULL;
84 }
Colin Crossf25dd872012-06-14 12:55:47 -070085 pthread_mutex_lock(&earlysuspend_mutex);
86 earlysuspend_state = EARLYSUSPEND_MEM;
87 pthread_cond_signal(&earlysuspend_cond);
88 pthread_mutex_unlock(&earlysuspend_mutex);
89
Colin Cross2146b7f2012-06-07 14:12:54 -070090 if (wait_for_fb_wake()) {
91 ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread\n");
92 return NULL;
93 }
Colin Crossf25dd872012-06-14 12:55:47 -070094 pthread_mutex_lock(&earlysuspend_mutex);
95 earlysuspend_state = EARLYSUSPEND_ON;
96 pthread_cond_signal(&earlysuspend_cond);
97 pthread_mutex_unlock(&earlysuspend_mutex);
Colin Cross2146b7f2012-06-07 14:12:54 -070098 }
99}
Colin Crossa2582c22012-05-03 17:30:16 -0700100static 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
Colin Crossf25dd872012-06-14 12:55:47 -0700114 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
Colin Crossa2582c22012-05-03 17:30:16 -0700122 ALOGV("autosuspend_earlysuspend_enable done\n");
123
124 return 0;
125
126err:
127 return ret;
128}
129
130static 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
Colin Crossf25dd872012-06-14 12:55:47 -0700144 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
Colin Crossa2582c22012-05-03 17:30:16 -0700152 ALOGV("autosuspend_earlysuspend_disable done\n");
153
154 return 0;
155
156err:
157 return ret;
158}
159
160struct autosuspend_ops autosuspend_earlysuspend_ops = {
161 .enable = autosuspend_earlysuspend_enable,
162 .disable = autosuspend_earlysuspend_disable,
163};
164
Colin Cross2146b7f2012-06-07 14:12:54 -0700165void start_earlysuspend_thread(void)
Colin Crossa2582c22012-05-03 17:30:16 -0700166{
167 char buf[80];
168 int ret;
169
170 ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK);
171 if (ret < 0) {
Colin Cross2146b7f2012-06-07 14:12:54 -0700172 return;
Colin Crossa2582c22012-05-03 17:30:16 -0700173 }
174
175 ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK);
176 if (ret < 0) {
Colin Cross2146b7f2012-06-07 14:12:54 -0700177 return;
Colin Crossa2582c22012-05-03 17:30:16 -0700178 }
179
Colin Crossf25dd872012-06-14 12:55:47 -0700180 wait_for_fb_wake();
181
Colin Cross2146b7f2012-06-07 14:12:54 -0700182 ALOGI("Starting early suspend unblocker thread\n");
183 ret = pthread_create(&earlysuspend_thread, NULL, earlysuspend_thread_func, NULL);
184 if (ret) {
Colin Crossf25dd872012-06-14 12:55:47 -0700185 strerror_r(errno, buf, sizeof(buf));
Colin Cross2146b7f2012-06-07 14:12:54 -0700186 ALOGE("Error creating thread: %s\n", buf);
Colin Crossf25dd872012-06-14 12:55:47 -0700187 return;
Colin Cross2146b7f2012-06-07 14:12:54 -0700188 }
Colin Crossf25dd872012-06-14 12:55:47 -0700189
190 wait_for_earlysuspend = true;
Colin Cross2146b7f2012-06-07 14:12:54 -0700191}
192
193struct autosuspend_ops *autosuspend_earlysuspend_init(void)
194{
195 char buf[80];
196 int ret;
197
Colin Crossa2582c22012-05-03 17:30:16 -0700198 sPowerStatefd = open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR);
199
200 if (sPowerStatefd < 0) {
201 strerror_r(errno, buf, sizeof(buf));
Colin Cross2146b7f2012-06-07 14:12:54 -0700202 ALOGW("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
Colin Crossa2582c22012-05-03 17:30:16 -0700203 return NULL;
204 }
205
Colin Cross2146b7f2012-06-07 14:12:54 -0700206 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
Colin Crossa2582c22012-05-03 17:30:16 -0700213 ALOGI("Selected early suspend\n");
Colin Cross2146b7f2012-06-07 14:12:54 -0700214
215 start_earlysuspend_thread();
216
Colin Crossa2582c22012-05-03 17:30:16 -0700217 return &autosuspend_earlysuspend_ops;
Colin Cross2146b7f2012-06-07 14:12:54 -0700218
219err_write:
220 close(sPowerStatefd);
221 return NULL;
Colin Crossa2582c22012-05-03 17:30:16 -0700222}