| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 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 |  | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 17 | #define LOG_TAG "libsuspend" | 
|  | 18 |  | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 | #include <fcntl.h> | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 21 | #include <pthread.h> | 
|  | 22 | #include <stdbool.h> | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 23 | #include <stddef.h> | 
|  | 24 | #include <string.h> | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 26 | #include <sys/types.h> | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 27 | #include <unistd.h> | 
|  | 28 |  | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 29 | #include <android/log.h> | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 30 |  | 
|  | 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 Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 37 | static int sPowerStatefd; | 
|  | 38 | static const char *pwr_state_mem = "mem"; | 
|  | 39 | static const char *pwr_state_on = "on"; | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 40 | static pthread_t earlysuspend_thread; | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 41 | static pthread_mutex_t earlysuspend_mutex = PTHREAD_MUTEX_INITIALIZER; | 
|  | 42 | static pthread_cond_t earlysuspend_cond = PTHREAD_COND_INITIALIZER; | 
|  | 43 | static bool wait_for_earlysuspend; | 
|  | 44 | static enum { | 
|  | 45 | EARLYSUSPEND_ON, | 
|  | 46 | EARLYSUSPEND_MEM, | 
|  | 47 | } earlysuspend_state = EARLYSUSPEND_ON; | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 48 |  | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 49 | int wait_for_fb_wake(void) | 
|  | 50 | { | 
|  | 51 | int err = 0; | 
|  | 52 | char buf; | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 53 | int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_WAKE, O_RDONLY, 0)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 54 | // if the file doesn't exist, the error will be caught in read() below | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 55 | err = TEMP_FAILURE_RETRY(read(fd, &buf, 1)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 56 | 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 |  | 
|  | 62 | static int wait_for_fb_sleep(void) | 
|  | 63 | { | 
|  | 64 | int err = 0; | 
|  | 65 | char buf; | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 66 | int fd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, O_RDONLY, 0)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 67 | // if the file doesn't exist, the error will be caught in read() below | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 68 | err = TEMP_FAILURE_RETRY(read(fd, &buf, 1)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 69 | 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 Levitskiy | cdc1cfb | 2014-04-10 17:10:21 -0700 | [diff] [blame] | 75 | static void *earlysuspend_thread_func(void __unused *arg) | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 76 | { | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 77 | 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 Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 82 | pthread_mutex_lock(&earlysuspend_mutex); | 
|  | 83 | earlysuspend_state = EARLYSUSPEND_MEM; | 
|  | 84 | pthread_cond_signal(&earlysuspend_cond); | 
|  | 85 | pthread_mutex_unlock(&earlysuspend_mutex); | 
|  | 86 |  | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 87 | if (wait_for_fb_wake()) { | 
|  | 88 | ALOGE("Failed reading wait_for_fb_wake, exiting earlysuspend thread\n"); | 
|  | 89 | return NULL; | 
|  | 90 | } | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 91 | pthread_mutex_lock(&earlysuspend_mutex); | 
|  | 92 | earlysuspend_state = EARLYSUSPEND_ON; | 
|  | 93 | pthread_cond_signal(&earlysuspend_cond); | 
|  | 94 | pthread_mutex_unlock(&earlysuspend_mutex); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 95 | } | 
|  | 96 | } | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 97 | static 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 Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 111 | 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 Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 119 | ALOGV("autosuspend_earlysuspend_enable done\n"); | 
|  | 120 |  | 
|  | 121 | return 0; | 
|  | 122 |  | 
|  | 123 | err: | 
|  | 124 | return ret; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | static int autosuspend_earlysuspend_disable(void) | 
|  | 128 | { | 
|  | 129 | char buf[80]; | 
|  | 130 | int ret; | 
|  | 131 |  | 
|  | 132 | ALOGV("autosuspend_earlysuspend_disable\n"); | 
|  | 133 |  | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 134 | ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on))); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 135 | 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 Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 141 | 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 Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 149 | ALOGV("autosuspend_earlysuspend_disable done\n"); | 
|  | 150 |  | 
|  | 151 | return 0; | 
|  | 152 |  | 
|  | 153 | err: | 
|  | 154 | return ret; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | struct autosuspend_ops autosuspend_earlysuspend_ops = { | 
|  | 158 | .enable = autosuspend_earlysuspend_enable, | 
|  | 159 | .disable = autosuspend_earlysuspend_disable, | 
|  | 160 | }; | 
|  | 161 |  | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 162 | void start_earlysuspend_thread(void) | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 163 | { | 
|  | 164 | char buf[80]; | 
|  | 165 | int ret; | 
|  | 166 |  | 
|  | 167 | ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK); | 
|  | 168 | if (ret < 0) { | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 169 | return; | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK); | 
|  | 173 | if (ret < 0) { | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 174 | return; | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 177 | wait_for_fb_wake(); | 
|  | 178 |  | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 179 | ALOGI("Starting early suspend unblocker thread\n"); | 
|  | 180 | ret = pthread_create(&earlysuspend_thread, NULL, earlysuspend_thread_func, NULL); | 
|  | 181 | if (ret) { | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 182 | strerror_r(errno, buf, sizeof(buf)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 183 | ALOGE("Error creating thread: %s\n", buf); | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 184 | return; | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 185 | } | 
| Colin Cross | f25dd87 | 2012-06-14 12:55:47 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | wait_for_earlysuspend = true; | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
|  | 190 | struct autosuspend_ops *autosuspend_earlysuspend_init(void) | 
|  | 191 | { | 
|  | 192 | char buf[80]; | 
|  | 193 | int ret; | 
|  | 194 |  | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 195 | sPowerStatefd = TEMP_FAILURE_RETRY(open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR)); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | if (sPowerStatefd < 0) { | 
|  | 198 | strerror_r(errno, buf, sizeof(buf)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 199 | ALOGW("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf); | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 200 | return NULL; | 
|  | 201 | } | 
|  | 202 |  | 
| Jeff Brown | 0446e16 | 2015-05-15 14:56:03 -0700 | [diff] [blame] | 203 | ret = TEMP_FAILURE_RETRY(write(sPowerStatefd, "on", 2)); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 204 | 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 Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 210 | ALOGI("Selected early suspend\n"); | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 211 |  | 
|  | 212 | start_earlysuspend_thread(); | 
|  | 213 |  | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 214 | return &autosuspend_earlysuspend_ops; | 
| Colin Cross | 2146b7f | 2012-06-07 14:12:54 -0700 | [diff] [blame] | 215 |  | 
|  | 216 | err_write: | 
|  | 217 | close(sPowerStatefd); | 
|  | 218 | return NULL; | 
| Colin Cross | a2582c2 | 2012-05-03 17:30:16 -0700 | [diff] [blame] | 219 | } |