Home | History | Annotate | Download | only in libsuspend
      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 
     17 #include <errno.h>
     18 #include <fcntl.h>
     19 #include <stddef.h>
     20 #include <string.h>
     21 #include <sys/types.h>
     22 #include <sys/stat.h>
     23 #include <unistd.h>
     24 
     25 #define LOG_TAG "libsuspend"
     26 #include <cutils/log.h>
     27 
     28 #include "autosuspend_ops.h"
     29 
     30 #define EARLYSUSPEND_SYS_POWER_STATE "/sys/power/state"
     31 #define EARLYSUSPEND_WAIT_FOR_FB_SLEEP "/sys/power/wait_for_fb_sleep"
     32 #define EARLYSUSPEND_WAIT_FOR_FB_WAKE "/sys/power/wait_for_fb_wake"
     33 
     34 
     35 static int sPowerStatefd;
     36 static const char *pwr_state_mem = "mem";
     37 static const char *pwr_state_on = "on";
     38 
     39 static int autosuspend_earlysuspend_enable(void)
     40 {
     41     char buf[80];
     42     int ret;
     43 
     44     ALOGV("autosuspend_earlysuspend_enable\n");
     45 
     46     ret = write(sPowerStatefd, pwr_state_mem, strlen(pwr_state_mem));
     47     if (ret < 0) {
     48         strerror_r(errno, buf, sizeof(buf));
     49         ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
     50         goto err;
     51     }
     52 
     53     ALOGV("autosuspend_earlysuspend_enable done\n");
     54 
     55     return 0;
     56 
     57 err:
     58     return ret;
     59 }
     60 
     61 static int autosuspend_earlysuspend_disable(void)
     62 {
     63     char buf[80];
     64     int ret;
     65 
     66     ALOGV("autosuspend_earlysuspend_disable\n");
     67 
     68     ret = write(sPowerStatefd, pwr_state_on, strlen(pwr_state_on));
     69     if (ret < 0) {
     70         strerror_r(errno, buf, sizeof(buf));
     71         ALOGE("Error writing to %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
     72         goto err;
     73     }
     74 
     75     ALOGV("autosuspend_earlysuspend_disable done\n");
     76 
     77     return 0;
     78 
     79 err:
     80     return ret;
     81 }
     82 
     83 struct autosuspend_ops autosuspend_earlysuspend_ops = {
     84         .enable = autosuspend_earlysuspend_enable,
     85         .disable = autosuspend_earlysuspend_disable,
     86 };
     87 
     88 struct autosuspend_ops *autosuspend_earlysuspend_init(void)
     89 {
     90     char buf[80];
     91     int ret;
     92 
     93     ret = access(EARLYSUSPEND_WAIT_FOR_FB_SLEEP, F_OK);
     94     if (ret < 0) {
     95         return NULL;
     96     }
     97 
     98     ret = access(EARLYSUSPEND_WAIT_FOR_FB_WAKE, F_OK);
     99     if (ret < 0) {
    100         return NULL;
    101     }
    102 
    103     sPowerStatefd = open(EARLYSUSPEND_SYS_POWER_STATE, O_RDWR);
    104 
    105     if (sPowerStatefd < 0) {
    106         strerror_r(errno, buf, sizeof(buf));
    107         ALOGE("Error opening %s: %s\n", EARLYSUSPEND_SYS_POWER_STATE, buf);
    108         return NULL;
    109     }
    110 
    111     ALOGI("Selected early suspend\n");
    112     return &autosuspend_earlysuspend_ops;
    113 }
    114