10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci#include <sys/statfs.h>
330d163575Sopenharmony_ci#include <sys/mount.h>
340d163575Sopenharmony_ci#include "proc_fs.h"
350d163575Sopenharmony_ci#include "internal.h"
360d163575Sopenharmony_ci#ifdef LOSCFG_KERNEL_PM
370d163575Sopenharmony_ci#include "los_pm.h"
380d163575Sopenharmony_ci
390d163575Sopenharmony_cistatic int PowerLockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
400d163575Sopenharmony_ci{
410d163575Sopenharmony_ci    (void)pf;
420d163575Sopenharmony_ci    (void)count;
430d163575Sopenharmony_ci    (void)ppos;
440d163575Sopenharmony_ci    return -LOS_PmLockRequest(buf);
450d163575Sopenharmony_ci}
460d163575Sopenharmony_ci
470d163575Sopenharmony_cistatic int PowerLockRead(struct SeqBuf *m, void *v)
480d163575Sopenharmony_ci{
490d163575Sopenharmony_ci    (void)v;
500d163575Sopenharmony_ci
510d163575Sopenharmony_ci    LOS_PmLockInfoShow(m);
520d163575Sopenharmony_ci    return 0;
530d163575Sopenharmony_ci}
540d163575Sopenharmony_ci
550d163575Sopenharmony_cistatic const struct ProcFileOperations PowerLock = {
560d163575Sopenharmony_ci    .write      = PowerLockWrite,
570d163575Sopenharmony_ci    .read       = PowerLockRead,
580d163575Sopenharmony_ci};
590d163575Sopenharmony_ci
600d163575Sopenharmony_cistatic int PowerUnlockWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
610d163575Sopenharmony_ci{
620d163575Sopenharmony_ci    (void)pf;
630d163575Sopenharmony_ci    (void)count;
640d163575Sopenharmony_ci    (void)ppos;
650d163575Sopenharmony_ci    return -LOS_PmLockRelease(buf);
660d163575Sopenharmony_ci}
670d163575Sopenharmony_ci
680d163575Sopenharmony_cistatic const struct ProcFileOperations PowerUnlock = {
690d163575Sopenharmony_ci    .write      = PowerUnlockWrite,
700d163575Sopenharmony_ci    .read       = PowerLockRead,
710d163575Sopenharmony_ci};
720d163575Sopenharmony_ci
730d163575Sopenharmony_cistatic int PowerModeWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
740d163575Sopenharmony_ci{
750d163575Sopenharmony_ci    (void)pf;
760d163575Sopenharmony_ci    (void)count;
770d163575Sopenharmony_ci    (void)ppos;
780d163575Sopenharmony_ci
790d163575Sopenharmony_ci    LOS_SysSleepEnum mode;
800d163575Sopenharmony_ci
810d163575Sopenharmony_ci    if (buf == NULL) {
820d163575Sopenharmony_ci        return 0;
830d163575Sopenharmony_ci    }
840d163575Sopenharmony_ci
850d163575Sopenharmony_ci    if (strcmp(buf, "normal") == 0) {
860d163575Sopenharmony_ci        mode = LOS_SYS_NORMAL_SLEEP;
870d163575Sopenharmony_ci    } else if (strcmp(buf, "light") == 0) {
880d163575Sopenharmony_ci        mode = LOS_SYS_LIGHT_SLEEP;
890d163575Sopenharmony_ci    } else if (strcmp(buf, "deep") == 0) {
900d163575Sopenharmony_ci        mode = LOS_SYS_DEEP_SLEEP;
910d163575Sopenharmony_ci    } else if (strcmp(buf, "shutdown") == 0) {
920d163575Sopenharmony_ci        mode = LOS_SYS_SHUTDOWN;
930d163575Sopenharmony_ci    } else {
940d163575Sopenharmony_ci        PRINT_ERR("Unsupported hibernation mode: %s\n", buf);
950d163575Sopenharmony_ci        return -EINVAL;
960d163575Sopenharmony_ci    }
970d163575Sopenharmony_ci
980d163575Sopenharmony_ci    return -LOS_PmModeSet(mode);
990d163575Sopenharmony_ci}
1000d163575Sopenharmony_ci
1010d163575Sopenharmony_cistatic int PowerModeRead(struct SeqBuf *m, void *v)
1020d163575Sopenharmony_ci{
1030d163575Sopenharmony_ci    (void)v;
1040d163575Sopenharmony_ci
1050d163575Sopenharmony_ci    LosBufPrintf(m, "normal light deep shutdown\n");
1060d163575Sopenharmony_ci    return 0;
1070d163575Sopenharmony_ci}
1080d163575Sopenharmony_ci
1090d163575Sopenharmony_cistatic const struct ProcFileOperations PowerMode = {
1100d163575Sopenharmony_ci    .write      = PowerModeWrite,
1110d163575Sopenharmony_ci    .read       = PowerModeRead,
1120d163575Sopenharmony_ci};
1130d163575Sopenharmony_ci
1140d163575Sopenharmony_cistatic int PowerCountRead(struct SeqBuf *m, void *v)
1150d163575Sopenharmony_ci{
1160d163575Sopenharmony_ci    (void)v;
1170d163575Sopenharmony_ci    UINT32 count = LOS_PmReadLock();
1180d163575Sopenharmony_ci
1190d163575Sopenharmony_ci    LosBufPrintf(m, "%u\n", count);
1200d163575Sopenharmony_ci    return 0;
1210d163575Sopenharmony_ci}
1220d163575Sopenharmony_ci
1230d163575Sopenharmony_cistatic int PowerCountWrite(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
1240d163575Sopenharmony_ci{
1250d163575Sopenharmony_ci    (void)pf;
1260d163575Sopenharmony_ci    (void)count;
1270d163575Sopenharmony_ci    (void)ppos;
1280d163575Sopenharmony_ci
1290d163575Sopenharmony_ci    int weakCount;
1300d163575Sopenharmony_ci
1310d163575Sopenharmony_ci    if (buf == NULL) {
1320d163575Sopenharmony_ci        return 0;
1330d163575Sopenharmony_ci    }
1340d163575Sopenharmony_ci
1350d163575Sopenharmony_ci    weakCount = atoi(buf);
1360d163575Sopenharmony_ci    return -LOS_PmSuspend(weakCount);
1370d163575Sopenharmony_ci}
1380d163575Sopenharmony_ci
1390d163575Sopenharmony_cistatic const struct ProcFileOperations PowerCount = {
1400d163575Sopenharmony_ci    .write      = PowerCountWrite,
1410d163575Sopenharmony_ci    .read       = PowerCountRead,
1420d163575Sopenharmony_ci};
1430d163575Sopenharmony_ci
1440d163575Sopenharmony_ci#define POWER_FILE_MODE  (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH)
1450d163575Sopenharmony_ci#define OS_POWER_PRIVILEGE 7
1460d163575Sopenharmony_ci
1470d163575Sopenharmony_civoid ProcPmInit(void)
1480d163575Sopenharmony_ci{
1490d163575Sopenharmony_ci    struct ProcDirEntry *power = CreateProcEntry("power", S_IFDIR | S_IRWXU | S_IRWXG | S_IROTH, NULL);
1500d163575Sopenharmony_ci    if (power == NULL) {
1510d163575Sopenharmony_ci        PRINT_ERR("create /proc/power error!\n");
1520d163575Sopenharmony_ci        return;
1530d163575Sopenharmony_ci    }
1540d163575Sopenharmony_ci    power->uid = OS_POWER_PRIVILEGE;
1550d163575Sopenharmony_ci    power->gid = OS_POWER_PRIVILEGE;
1560d163575Sopenharmony_ci
1570d163575Sopenharmony_ci    struct ProcDirEntry *mode = CreateProcEntry("power/power_mode", POWER_FILE_MODE, NULL);
1580d163575Sopenharmony_ci    if (mode == NULL) {
1590d163575Sopenharmony_ci        PRINT_ERR("create /proc/power/power_mode error!\n");
1600d163575Sopenharmony_ci        goto FREE_POWER;
1610d163575Sopenharmony_ci    }
1620d163575Sopenharmony_ci    mode->procFileOps = &PowerMode;
1630d163575Sopenharmony_ci    mode->uid = OS_POWER_PRIVILEGE;
1640d163575Sopenharmony_ci    mode->gid = OS_POWER_PRIVILEGE;
1650d163575Sopenharmony_ci
1660d163575Sopenharmony_ci    struct ProcDirEntry *lock = CreateProcEntry("power/power_lock", POWER_FILE_MODE, NULL);
1670d163575Sopenharmony_ci    if (lock == NULL) {
1680d163575Sopenharmony_ci        PRINT_ERR("create /proc/power/power_lock error!\n");
1690d163575Sopenharmony_ci        goto FREE_MODE;
1700d163575Sopenharmony_ci    }
1710d163575Sopenharmony_ci    lock->procFileOps = &PowerLock;
1720d163575Sopenharmony_ci    lock->uid = OS_POWER_PRIVILEGE;
1730d163575Sopenharmony_ci    lock->gid = OS_POWER_PRIVILEGE;
1740d163575Sopenharmony_ci
1750d163575Sopenharmony_ci    struct ProcDirEntry *unlock = CreateProcEntry("power/power_unlock", POWER_FILE_MODE, NULL);
1760d163575Sopenharmony_ci    if (unlock == NULL) {
1770d163575Sopenharmony_ci        PRINT_ERR("create /proc/power/power_unlock error!\n");
1780d163575Sopenharmony_ci        goto FREE_LOCK;
1790d163575Sopenharmony_ci    }
1800d163575Sopenharmony_ci    unlock->procFileOps = &PowerUnlock;
1810d163575Sopenharmony_ci    unlock->uid = OS_POWER_PRIVILEGE;
1820d163575Sopenharmony_ci    unlock->gid = OS_POWER_PRIVILEGE;
1830d163575Sopenharmony_ci
1840d163575Sopenharmony_ci    struct ProcDirEntry *count = CreateProcEntry("power/power_count", S_IRUSR | S_IRGRP | S_IROTH, NULL);
1850d163575Sopenharmony_ci    if (count == NULL) {
1860d163575Sopenharmony_ci        PRINT_ERR("create /proc/power/power_count error!\n");
1870d163575Sopenharmony_ci        goto FREE_UNLOCK;
1880d163575Sopenharmony_ci    }
1890d163575Sopenharmony_ci    count->procFileOps = &PowerCount;
1900d163575Sopenharmony_ci    count->uid = OS_POWER_PRIVILEGE;
1910d163575Sopenharmony_ci    count->gid = OS_POWER_PRIVILEGE;
1920d163575Sopenharmony_ci
1930d163575Sopenharmony_ci    return;
1940d163575Sopenharmony_ci
1950d163575Sopenharmony_ciFREE_UNLOCK:
1960d163575Sopenharmony_ci    ProcFreeEntry(unlock);
1970d163575Sopenharmony_ciFREE_LOCK:
1980d163575Sopenharmony_ci    ProcFreeEntry(lock);
1990d163575Sopenharmony_ciFREE_MODE:
2000d163575Sopenharmony_ci    ProcFreeEntry(mode);
2010d163575Sopenharmony_ciFREE_POWER:
2020d163575Sopenharmony_ci    ProcFreeEntry(power);
2030d163575Sopenharmony_ci    return;
2040d163575Sopenharmony_ci}
2050d163575Sopenharmony_ci#endif
206