blob: 27c862957f3e3250a46c740c0e8d34de3c4f9a7a [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//#define LOG_NDEBUG 0
19
Colin Crossa2582c22012-05-03 17:30:16 -070020#include <fcntl.h>
21#include <pthread.h>
22#include <semaphore.h>
Ruchi Kandoid3027d82015-05-13 14:57:08 -070023#include <stdbool.h>
Steve Paikdb887742017-12-20 15:09:21 -080024#include <stddef.h>
Colin Crossa2582c22012-05-03 17:30:16 -070025#include <string.h>
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070026#include <sys/param.h>
Colin Crossa2582c22012-05-03 17:30:16 -070027#include <sys/stat.h>
28#include <sys/types.h>
29#include <unistd.h>
30
Steve Paikd5dc8072017-12-21 12:44:46 -080031#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/strings.h>
Colin Crossa2582c22012-05-03 17:30:16 -070034
35#include "autosuspend_ops.h"
36
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070037#define BASE_SLEEP_TIME 100000
Steve Paikd5dc8072017-12-21 12:44:46 -080038#define MAX_SLEEP_TIME 60000000
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070039
Steve Paik10b149f2018-01-03 20:27:58 -080040static int state_fd = -1;
Colin Crossa2582c22012-05-03 17:30:16 -070041static int wakeup_count_fd;
Steve Paikd5dc8072017-12-21 12:44:46 -080042
43using android::base::ReadFdToString;
44using android::base::Trim;
45using android::base::WriteStringToFd;
46
Colin Crossa2582c22012-05-03 17:30:16 -070047static pthread_t suspend_thread;
48static sem_t suspend_lockout;
Steve Paik10b149f2018-01-03 20:27:58 -080049static constexpr char sleep_state[] = "mem";
Ruchi Kandoid3027d82015-05-13 14:57:08 -070050static void (*wakeup_func)(bool success) = NULL;
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070051static int sleep_time = BASE_SLEEP_TIME;
Steve Paikd5dc8072017-12-21 12:44:46 -080052static constexpr char sys_power_state[] = "/sys/power/state";
53static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count";
Steve Paik10b149f2018-01-03 20:27:58 -080054static bool autosuspend_is_init = false;
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070055
56static void update_sleep_time(bool success) {
57 if (success) {
58 sleep_time = BASE_SLEEP_TIME;
59 return;
60 }
61 // double sleep time after each failure up to one minute
Steve Paikd5dc8072017-12-21 12:44:46 -080062 sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME);
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070063}
Colin Crossa2582c22012-05-03 17:30:16 -070064
Steve Paik2d441902017-12-15 13:26:28 -080065static void* suspend_thread_func(void* arg __attribute__((unused))) {
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070066 bool success = true;
Colin Crossa2582c22012-05-03 17:30:16 -070067
Steve Paik10b149f2018-01-03 20:27:58 -080068 while (true) {
Ajay Dudani0c5f7d82017-04-17 16:54:15 -070069 update_sleep_time(success);
70 usleep(sleep_time);
71 success = false;
Steve Paikd5dc8072017-12-21 12:44:46 -080072 LOG(VERBOSE) << "read wakeup_count";
Colin Crossa2582c22012-05-03 17:30:16 -070073 lseek(wakeup_count_fd, 0, SEEK_SET);
Steve Paikd5dc8072017-12-21 12:44:46 -080074 std::string wakeup_count;
75 if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) {
76 PLOG(ERROR) << "error reading from " << sys_power_wakeup_count;
Colin Crossa2582c22012-05-03 17:30:16 -070077 continue;
78 }
79
Steve Paikd5dc8072017-12-21 12:44:46 -080080 wakeup_count = Trim(wakeup_count);
81 if (wakeup_count.empty()) {
82 LOG(ERROR) << "empty wakeup count";
83 continue;
84 }
85
86 LOG(VERBOSE) << "wait";
Steve Paik10b149f2018-01-03 20:27:58 -080087 int ret = sem_wait(&suspend_lockout);
Colin Crossa2582c22012-05-03 17:30:16 -070088 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -080089 PLOG(ERROR) << "error waiting on semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -070090 continue;
91 }
92
Steve Paikd5dc8072017-12-21 12:44:46 -080093 LOG(VERBOSE) << "write " << wakeup_count << " to wakeup_count";
94 if (WriteStringToFd(wakeup_count, wakeup_count_fd)) {
95 LOG(VERBOSE) << "write " << sleep_state << " to " << sys_power_state;
96 success = WriteStringToFd(sleep_state, state_fd);
97
Ruchi Kandoid3027d82015-05-13 14:57:08 -070098 void (*func)(bool success) = wakeup_func;
99 if (func != NULL) {
100 (*func)(success);
Colin Crossa2582c22012-05-03 17:30:16 -0700101 }
Steve Paikd5dc8072017-12-21 12:44:46 -0800102 } else {
103 PLOG(ERROR) << "error writing to " << sys_power_wakeup_count;
Colin Crossa2582c22012-05-03 17:30:16 -0700104 }
105
Steve Paikd5dc8072017-12-21 12:44:46 -0800106 LOG(VERBOSE) << "release sem";
Colin Crossa2582c22012-05-03 17:30:16 -0700107 ret = sem_post(&suspend_lockout);
108 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800109 PLOG(ERROR) << "error releasing semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700110 }
111 }
112 return NULL;
113}
114
Steve Paik10b149f2018-01-03 20:27:58 -0800115static int init_state_fd(void) {
116 if (state_fd >= 0) {
117 return 0;
118 }
Colin Crossa2582c22012-05-03 17:30:16 -0700119
Steve Paik10b149f2018-01-03 20:27:58 -0800120 int fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_CLOEXEC | O_RDWR));
121 if (fd < 0) {
122 PLOG(ERROR) << "error opening " << sys_power_state;
123 return -1;
124 }
125
126 state_fd = fd;
127 LOG(INFO) << "init_state_fd success";
128 return 0;
129}
130
131static int autosuspend_init(void) {
132 if (autosuspend_is_init) {
133 return 0;
134 }
135
136 int ret = init_state_fd();
137 if (ret < 0) {
138 return -1;
139 }
140
141 wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_CLOEXEC | O_RDWR));
142 if (wakeup_count_fd < 0) {
143 PLOG(ERROR) << "error opening " << sys_power_wakeup_count;
144 goto err_open_wakeup_count;
145 }
146
147 ret = sem_init(&suspend_lockout, 0, 0);
148 if (ret < 0) {
149 PLOG(ERROR) << "error creating suspend_lockout semaphore";
150 goto err_sem_init;
151 }
152
153 ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
154 if (ret) {
155 LOG(ERROR) << "error creating thread: " << strerror(ret);
156 goto err_pthread_create;
157 }
158
159 LOG(VERBOSE) << "autosuspend_init success";
160 autosuspend_is_init = true;
161 return 0;
162
163err_pthread_create:
164 sem_destroy(&suspend_lockout);
165err_sem_init:
166 close(wakeup_count_fd);
167err_open_wakeup_count:
168 return -1;
169}
170
171static int autosuspend_wakeup_count_enable(void) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800172 LOG(VERBOSE) << "autosuspend_wakeup_count_enable";
Colin Crossa2582c22012-05-03 17:30:16 -0700173
Steve Paik10b149f2018-01-03 20:27:58 -0800174 int ret = autosuspend_init();
175 if (ret < 0) {
176 LOG(ERROR) << "autosuspend_init failed";
177 return ret;
178 }
Colin Crossa2582c22012-05-03 17:30:16 -0700179
Steve Paik10b149f2018-01-03 20:27:58 -0800180 ret = sem_post(&suspend_lockout);
Colin Crossa2582c22012-05-03 17:30:16 -0700181 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800182 PLOG(ERROR) << "error changing semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700183 }
184
Steve Paikd5dc8072017-12-21 12:44:46 -0800185 LOG(VERBOSE) << "autosuspend_wakeup_count_enable done";
Colin Crossa2582c22012-05-03 17:30:16 -0700186
187 return ret;
188}
189
Steve Paik2d441902017-12-15 13:26:28 -0800190static int autosuspend_wakeup_count_disable(void) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800191 LOG(VERBOSE) << "autosuspend_wakeup_count_disable";
Colin Crossa2582c22012-05-03 17:30:16 -0700192
Steve Paik10b149f2018-01-03 20:27:58 -0800193 if (!autosuspend_is_init) {
194 return 0; // always successful if no thread is running yet
195 }
196
197 int ret = sem_wait(&suspend_lockout);
Colin Crossa2582c22012-05-03 17:30:16 -0700198
199 if (ret < 0) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800200 PLOG(ERROR) << "error changing semaphore";
Colin Crossa2582c22012-05-03 17:30:16 -0700201 }
202
Steve Paikd5dc8072017-12-21 12:44:46 -0800203 LOG(VERBOSE) << "autosuspend_wakeup_count_disable done";
Colin Crossa2582c22012-05-03 17:30:16 -0700204
205 return ret;
206}
207
Steve Paik10b149f2018-01-03 20:27:58 -0800208static int force_suspend(int timeout_ms) {
209 LOG(VERBOSE) << "force_suspend called with timeout: " << timeout_ms;
210
211 int ret = init_state_fd();
212 if (ret < 0) {
213 return ret;
214 }
215
216 return WriteStringToFd(sleep_state, state_fd) ? 0 : -1;
217}
218
Steve Paik2d441902017-12-15 13:26:28 -0800219static void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
Dianne Hackborn9552cdf2014-03-07 11:00:39 -0800220 if (wakeup_func != NULL) {
Steve Paikd5dc8072017-12-21 12:44:46 -0800221 LOG(ERROR) << "duplicate wakeup callback applied, keeping original";
Dianne Hackborn9552cdf2014-03-07 11:00:39 -0800222 return;
223 }
224 wakeup_func = func;
225}
226
Colin Crossa2582c22012-05-03 17:30:16 -0700227struct autosuspend_ops autosuspend_wakeup_count_ops = {
Steve Paik2d441902017-12-15 13:26:28 -0800228 .enable = autosuspend_wakeup_count_enable,
229 .disable = autosuspend_wakeup_count_disable,
Steve Paik10b149f2018-01-03 20:27:58 -0800230 .force_suspend = force_suspend,
Steve Paik2d441902017-12-15 13:26:28 -0800231 .set_wakeup_callback = autosuspend_set_wakeup_callback,
Colin Crossa2582c22012-05-03 17:30:16 -0700232};
233
Steve Paik2d441902017-12-15 13:26:28 -0800234struct autosuspend_ops* autosuspend_wakeup_count_init(void) {
Colin Crossa2582c22012-05-03 17:30:16 -0700235 return &autosuspend_wakeup_count_ops;
Colin Crossa2582c22012-05-03 17:30:16 -0700236}