pass-hal: tm1: Update pass-hal-tm1 by using hal-api-power interface 26/252626/1 accepted/tizen/unified/20210204.134513 submit/tizen/20210201.020414 submit/tizen/20210204.032624
authorChanwoo Choi <cw00.choi@samsung.com>
Fri, 29 Jan 2021 07:35:51 +0000 (16:35 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 29 Jan 2021 09:26:53 +0000 (18:26 +0900)
Prior pass-hal-tm1 depends on PASS HAL interface (hal.h) provided by
pass-hal-devel pkg. In order to keep the new HAL API package on Tizen 6.5,
change the pass-hal-tm1 package.

[Mandatory implementation for HAL backend]
- Power HAL backend must define 'hal_backend_power_data' symbol
including the implementation of Power HAL interface.
- Include /hal/lib64/libhal-backend-power.so

Changes from v1.1.0
- Remake the pass-hal-tm1 according to hal-api-power(/platform/hal/api/power).

Change-Id: I5f68875224bb90e476532a57f659e42a1ec9dadc
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
12 files changed:
CMakeLists.txt
packaging/pass-hal-tm1.spec
scripts/pass.conf
src/cpu/CMakeLists.txt [deleted file]
src/cpu/cpu.c [deleted file]
src/hal-backend-power.c [new file with mode: 0644]
src/nonstandard/CMakeLists.txt [deleted file]
src/nonstandard/nonstandard.c [deleted file]
src/shared/sysfs.c [deleted file]
src/shared/sysfs.h [deleted file]
src/sysfs.c [new file with mode: 0644]
src/sysfs.h [new file with mode: 0644]

index b093238f1290c0a0d1c0d6c6d63b5591ca1bb076..55ac0148cb70a96364fd4d762fabf033fd99429e 100644 (file)
@@ -1,27 +1,37 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
 PROJECT(pass-hal-tm1 C)
 
+SET(LIBRARY_NAME "hal-backend-power")
 SET(CMAKE_VERBOSE_MAKEFILE ON)
+SET(PREFIX ${CMAKE_INSTALL_PREFIX})
+SET(HAL_LIBDIR ${CMAKE_HAL_LIBDIR_PREFIX})
+SET(HAL_LICENSEDIR ${CMAKE_HAL_LICENSEDIR_PREFIX})
 
-SET(PKG_MODULES
-       dlog)
 INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED ${PKG_MODULES})
+pkg_check_modules(pkgs REQUIRED
+       hal-api-common
+       hal-api-power
+)
 
 FOREACH(flag ${pkgs_CFLAGS})
          SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
+SET(CMAKE_C_FLAGS "${CMAKE_CFLAGS} ${EXTRA_CFLAGS}")
 
-SET(PREFIX ${CMAKE_INSTALL_PREFIX})
-SET(DEST_DIR ${LIB_INSTALL_DIR}/pass)
+INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/include)
+
+SET(SRCS
+       ${PROJECT_SOURCE_DIR}/src/${LIBRARY_NAME}.c
+       ${PROJECT_SOURCE_DIR}/src/sysfs.c)
 
-ADD_SUBDIRECTORY(src/cpu)
-ADD_SUBDIRECTORY(src/nonstandard)
+ADD_LIBRARY(${LIBRARY_NAME} SHARED ${SRCS})
+TARGET_LINK_LIBRARIES(${LIBRARY_NAME} ${pkgs_LDFLAGS} -ldl)
 
-SET(CONF_DIR /etc/pass)
 SET(CONF_FILES
        ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass.conf
        ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-resource0.conf
        ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-resource1.conf)
-INSTALL(FILES ${CONF_FILES} DESTINATION ${CONF_DIR})
-INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-hal.conf DESTINATION ${LIB_INSTALL_DIR}/tmpfiles.d)
+INSTALL(FILES ${CONF_FILES} DESTINATION /hal/etc/pass)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/scripts/pass-hal.conf DESTINATION /hal/lib/tmpfiles.d)
+INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE DESTINATION ${HAL_LICENSEDIR}/${PROJECT_NAME})
+INSTALL(TARGETS ${LIBRARY_NAME} DESTINATION ${HAL_LIBDIR} COMPONENT RuntimeLibraries)
index 0fa4bd1e6a449cc04eb01dec6b3a7513e37df1e3..8e0a5a7e06dba472c417afc5210f6b8f3a34d0fa 100644 (file)
@@ -1,4 +1,4 @@
-%define version 1.1.0
+%define version 1.2.0
 %define release 1
 
 Name:       pass-hal-tm1
@@ -17,11 +17,9 @@ Requires(post): /bin/systemctl
 Requires(post): security-config
 Requires(postun): /sbin/ldconfig
 Requires(postun): /bin/systemctl
-Requires: pass >= 1.1.0
 BuildRequires:  cmake
-BuildRequires:  pkgconfig(dlog)
-BuildRequires:  pkgconfig(glib-2.0)
-BuildRequires:         pass-hal-devel >= 1.1.0
+BuildRequires:  pkgconfig(hal-api-common)
+BuildRequires:  pkgconfig(hal-api-power)
 
 %description
 PASS hal for TM1
@@ -31,7 +29,9 @@ PASS hal for TM1
 cp %{SOURCE1} .
 
 %build
-%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} -DHAL_VERSION=%{version}.%{release}
+%cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
+       -DCMAKE_HAL_LIBDIR_PREFIX=%{_hal_libdir} \
+       -DCMAKE_HAL_LICENSEDIR_PREFIX=%{_hal_licensedir} \
 
 make %{?jobs:-j%jobs}
 
@@ -41,7 +41,7 @@ rm -rf %{buildroot}
 %make_install
 %post
 /sbin/ldconfig
-systemd-tmpfiles %{_libdir}/tmpfiles.d/pass-hal.conf --create
+systemd-tmpfiles /hal/lib/tmpfiles.d/pass-hal.conf --create
 if [ -f %{_unitdir}/pass.service ]; then
        systemctl try-restart pass.service
 fi
@@ -54,8 +54,9 @@ fi
 
 %files
 %manifest %{name}.manifest
-%{_libdir}/pass/*.so
-%{_libdir}/tmpfiles.d/pass-hal.conf
-%config %{_sysconfdir}/pass/pass.conf
-%config %{_sysconfdir}/pass/pass-resource0.conf
-%config %{_sysconfdir}/pass/pass-resource1.conf
+%{_hal_licensedir}/%{name}/LICENSE
+%{_hal_libdir}/*.so
+/hal/lib/tmpfiles.d/pass-hal.conf
+%config %{_hal_sysconfdir}/pass/pass.conf
+%config %{_hal_sysconfdir}/pass/pass-resource0.conf
+%config %{_hal_sysconfdir}/pass/pass-resource1.conf
index fb705b1ee8f5c3c7ab69dc35b09eaff4d0081e4b..1e46fe3d03431facad9347d368653757c3786e6b 100644 (file)
@@ -10,11 +10,11 @@ pass_num_resources=2
 pass_res_type=cpu
 pass_res_name=cpu0
 pass_res_thermal_name=ap_therm
-pass_path_conf_file=/etc/pass/pass-resource0.conf
+pass_path_conf_file=/hal/etc/pass/pass-resource0.conf
 pass_first_cpu=0
 pass_num_cpus=4
 
 [PassResource1]
 pass_res_type=nonstandard
 pass_res_name=trm
-pass_path_conf_file=/etc/pass/pass-resource1.conf
+pass_path_conf_file=/hal/etc/pass/pass-resource1.conf
diff --git a/src/cpu/CMakeLists.txt b/src/cpu/CMakeLists.txt
deleted file mode 100644 (file)
index 3cd62b8..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-PROJECT(cpu C)
-
-INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED dlog glib-2.0)
-
-#HAL version
-string(REPLACE "." ";" HAL_VERSION_LIST ${HAL_VERSION})
-list(GET HAL_VERSION_LIST 0 HAL_VERSION_MAJOR)
-list(GET HAL_VERSION_LIST 1 HAL_VERSION_MINOR)
-list(GET HAL_VERSION_LIST 2 HAL_VERSION_REVISION)
-list(GET HAL_VERSION_LIST 3 HAL_VERSION_RELEASE)
-add_definitions(-DVER_MAJOR=${HAL_VERSION_MAJOR} -DVER_MINOR=${HAL_VERSION_MINOR})
-add_definitions(-DVER_REVISION=${HAL_VERSION_REVISION} -DVER_RELEASE=${HAL_VERSION_RELEASE})
-
-SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
-SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
-
-SET(SRCS ${PROJECT_NAME}.c
-       ../shared/sysfs.c)
-
-ADD_LIBRARY(${PROJECT_NAME} MODULE ${SRCS})
-SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
-INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${DEST_DIR} COMPONENT RuntimeLibraries)
diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c
deleted file mode 100644 (file)
index 5e2b09d..0000000
+++ /dev/null
@@ -1,362 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <errno.h>
-#include <limits.h>
-#include <pass/hal.h>
-#include <pass/hal-log.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "../shared/sysfs.h"
-
-#define CPUFREQ_PMQOS_PATH_PREFIX              "/sys/devices/system/cpu/cpufreq/pmqos"
-#define CPUFREQ_PATH_PREFIX                    "/sys/devices/system/cpu/"
-#define TMU_PATH_PREFIX                                "/sys/class/sec/temperature/"
-
-#define CPUFREQ_CPU_ONLINE_MIN_NUM_PATH_SUFFIX "/cpu_online_min"
-#define CPUFREQ_CPU_ONLINE_MAX_NUM_PATH_SUFFIX "/cpu_online_max"
-#define CPUFREQ_MAX_FREQ_PATH_SUFFIX           "/cpufreq_max"
-#define CPUFREQ_MIN_FREQ_PATH_SUFFIX           "/cpufreq_min"
-#define CPUFREQ_CURR_FREQ_PATH_SUFFIX          "/cpufreq/cpuinfo_cur_freq"
-
-#define CPUFREQ_DEFAULT_MIN_FREQ               0
-#define CPUFREQ_DEFAULT_MAX_FREQ               INT_MAX
-#define CPUFREQ_DEFAULT_ONLINE_MIN_NUM         0
-#define CPUFREQ_DEFAULT_ONLINE_MAX_NUM         4
-
-#define CPUFREQ_PMQOS_REQUEST_UNLOCK           -1
-
-static int tm1_dvfs_get_curr_freq(char *res_name)
-{
-       char path[PATH_MAX];
-       int freq, ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s%s",
-               CPUFREQ_PATH_PREFIX,
-               res_name,
-               CPUFREQ_CURR_FREQ_PATH_SUFFIX);
-
-       ret = sysfs_read_int(path, &freq);
-       if (ret < 0)
-               return ret;
-
-       return freq;
-}
-
-static int tm1_dvfs_get_min_freq(char *res_name)
-{
-       char path[PATH_MAX];
-       int min_freq;
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s",
-                CPUFREQ_PMQOS_PATH_PREFIX,
-                CPUFREQ_MIN_FREQ_PATH_SUFFIX);
-
-       ret = sysfs_read_int(path, &min_freq);
-       if (ret < 0)
-               return ret;
-
-       return min_freq;
-}
-
-static int tm1_dvfs_set_min_freq(char *res_name, int freq)
-{
-       char path[PATH_MAX];
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       ret = snprintf(path, PATH_MAX, "%s%s",
-               CPUFREQ_PMQOS_PATH_PREFIX,
-               CPUFREQ_MIN_FREQ_PATH_SUFFIX);
-
-       ret = sysfs_write_int(path, freq);
-       if (ret < 0)
-               return ret;
-
-       return 0;
-}
-
-static int tm1_dvfs_get_max_freq(char *res_name)
-{
-       char path[PATH_MAX];
-       int max_freq;
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s",
-                CPUFREQ_PMQOS_PATH_PREFIX,
-                CPUFREQ_MAX_FREQ_PATH_SUFFIX);
-
-       ret = sysfs_read_int(path, &max_freq);
-       if (ret < 0)
-               return ret;
-
-       return max_freq;
-}
-
-static int tm1_dvfs_set_max_freq(char *res_name, int freq)
-{
-       char path[PATH_MAX];
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s",
-               CPUFREQ_PMQOS_PATH_PREFIX,
-               CPUFREQ_MAX_FREQ_PATH_SUFFIX);
-
-       ret = sysfs_write_int(path, freq);
-       if (ret < 0)
-               return ret;
-
-       return 0;
-}
-
-static struct pass_resource_dvfs_ops tm1_cpu_dvfs_ops =  {
-       .get_curr_governor = NULL,
-       .set_curr_governor = NULL,
-       .get_avail_governor = NULL,
-       .get_curr_freq = tm1_dvfs_get_curr_freq,
-       .get_min_freq = tm1_dvfs_get_min_freq,
-       .set_min_freq = tm1_dvfs_set_min_freq,
-       .get_max_freq = tm1_dvfs_get_max_freq,
-       .set_max_freq = tm1_dvfs_set_max_freq,
-       .get_up_threshold = NULL,
-       .set_up_threshold = NULL,
-       .get_load_table = NULL,
-};
-
-static int tm1_hotplug_get_online_min_num(char *res_name)
-{
-       char path[PATH_MAX];
-       int online_min_num;
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s",
-               CPUFREQ_PMQOS_PATH_PREFIX,
-               CPUFREQ_CPU_ONLINE_MIN_NUM_PATH_SUFFIX);
-
-       ret = sysfs_read_int(path, &online_min_num);
-       if (ret < 0)
-               return ret;
-
-       return online_min_num;
-}
-
-static int tm1_hotplug_set_online_min_num(char *res_name, int num)
-{
-       char path[PATH_MAX];
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       /* TODO: Can we turn off CPU0? */
-       snprintf(path, PATH_MAX, "%s%s",
-               CPUFREQ_PMQOS_PATH_PREFIX,
-               CPUFREQ_CPU_ONLINE_MIN_NUM_PATH_SUFFIX);
-
-       ret = sysfs_write_int(path, num);
-       if (ret < 0)
-               return ret;
-
-       return 0;
-}
-
-static int tm1_hotplug_get_online_max_num(char *res_name)
-{
-       char path[PATH_MAX];
-       int online_max_num;
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s",
-               CPUFREQ_PMQOS_PATH_PREFIX,
-               CPUFREQ_CPU_ONLINE_MAX_NUM_PATH_SUFFIX);
-
-       ret = sysfs_read_int(path, &online_max_num);
-       if (ret < 0)
-               return ret;
-
-       return online_max_num;
-}
-
-static int tm1_hotplug_set_online_max_num(char *res_name, int num)
-{
-       char path[PATH_MAX];
-       int ret;
-
-       if (!res_name)
-               return -EINVAL;
-
-       /* TODO: Can we turn off CPU0? */
-       snprintf(path, PATH_MAX, "%s%s",
-               CPUFREQ_PMQOS_PATH_PREFIX,
-               CPUFREQ_CPU_ONLINE_MAX_NUM_PATH_SUFFIX);
-
-       ret = sysfs_write_int(path, num);
-       if (ret < 0)
-               return ret;
-
-       return 0;
-}
-static struct pass_resource_hotplug_ops tm1_cpu_hotplug_ops = {
-       .get_online_state = NULL,
-       .set_online_state = NULL,
-       .get_online_min_num = tm1_hotplug_get_online_min_num,
-       .set_online_min_num = tm1_hotplug_set_online_min_num,
-       .get_online_max_num = tm1_hotplug_get_online_max_num,
-       .set_online_max_num = tm1_hotplug_set_online_max_num,
-};
-
-static int tm1_cpu_get_temp(char *res_thermal_name)
-{
-       char path[PATH_MAX + 1];
-       char buf[PATH_MAX + 1];
-       char *pos;
-       int temp = 0;
-       int ret;
-
-       if (!res_thermal_name)
-               return -EINVAL;
-
-       snprintf(path, PATH_MAX, "%s%s", TMU_PATH_PREFIX, res_thermal_name);
-
-       ret = sysfs_read_str(path, buf, PATH_MAX);
-       if (ret < 0)
-               return ret;
-
-       pos = strstr(buf, "temp:");
-       if (!pos || !(pos[5]))
-               return -EINVAL;
-       pos += 5;
-       temp = atoi(pos);
-
-       return temp;
-}
-
-static struct pass_resource_tmu_ops tm1_cpu_tmu_ops = {
-       .get_temp = tm1_cpu_get_temp,
-       .get_policy = NULL,
-};
-
-static int tm1_cpu_open(char *res_name, struct pass_resource_info *info,
-               struct pass_resource_common **common)
-{
-       struct pass_resource_cpu *cpu_res;
-
-       if (!res_name || !info)
-               return -EINVAL;
-
-       /* TODO: Possibility of a memory leak */
-       cpu_res = calloc(1, sizeof(struct pass_resource_cpu));
-       if (!cpu_res)
-               return -ENOMEM;
-
-       cpu_res->common.info = info;
-       cpu_res->dvfs = tm1_cpu_dvfs_ops;
-       cpu_res->hotplug = tm1_cpu_hotplug_ops;
-       cpu_res->tmu = tm1_cpu_tmu_ops;
-
-       *common = (struct pass_resource_common *) cpu_res;
-
-       return 0;
-}
-
-static int tm1_cpu_close(char *res_name, struct pass_resource_common *common)
-{
-       struct pass_resource_cpu *cpu_res;
-       int ret = 0;
-
-       if (!res_name || !common)
-               return -EINVAL;
-
-       cpu_res = (struct pass_resource_cpu *) common;
-
-       /*
-        * tm1 has weird pmqos sysfs node, thus we cannot apply save/restore
-        * feature as pass-hal-standard does. Instead of using standard
-        * interface, we manually restore the each resource state; pmqos
-        * request is unlocked by passing argument '-1' first, and resource
-        * state is restored by passing default value.
-        */
-       ret = cpu_res->hotplug.set_online_max_num(res_name,
-                                       CPUFREQ_PMQOS_REQUEST_UNLOCK);
-       if (ret < 0)
-               goto exit;
-       ret = cpu_res->hotplug.set_online_max_num(res_name,
-                                       CPUFREQ_DEFAULT_ONLINE_MAX_NUM);
-       if (ret < 0)
-               goto exit;
-
-       ret = cpu_res->hotplug.set_online_min_num(res_name,
-                                       CPUFREQ_PMQOS_REQUEST_UNLOCK);
-       if (ret < 0)
-               goto exit;
-       ret = cpu_res->hotplug.set_online_min_num(res_name,
-                                       CPUFREQ_DEFAULT_ONLINE_MIN_NUM);
-       if (ret < 0)
-               goto exit;
-
-       ret = cpu_res->dvfs.set_min_freq(res_name,
-                                       CPUFREQ_PMQOS_REQUEST_UNLOCK);
-       if (ret < 0)
-               goto exit;
-       ret = cpu_res->dvfs.set_min_freq(res_name, CPUFREQ_DEFAULT_MIN_FREQ);
-       if (ret < 0)
-               goto exit;
-
-       ret = cpu_res->dvfs.set_max_freq(res_name,
-                                       CPUFREQ_PMQOS_REQUEST_UNLOCK);
-       if (ret < 0)
-               goto exit;
-       ret = cpu_res->dvfs.set_max_freq(res_name, CPUFREQ_DEFAULT_MAX_FREQ);
-       if (ret < 0)
-               goto exit;
-
-exit:
-       free(common);
-
-       return ret;
-}
-
-HAL_MODULE_STRUCTURE = {
-       .magic = HAL_INFO_TAG,
-       .id = PASS_RESOURCE_CPU_ID,
-       .name = PASS_RESOURCE_CPU_NAME,
-       .open = tm1_cpu_open,
-       .close = tm1_cpu_close,
-};
diff --git a/src/hal-backend-power.c b/src/hal-backend-power.c
new file mode 100644 (file)
index 0000000..dd71049
--- /dev/null
@@ -0,0 +1,435 @@
+/*
+ * HAL backend for hal-api-power which is used for PASS daemon
+ *
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <limits.h>
+#include <linux/types.h>
+#include <linux/un.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#include <hal/hal-power-interface.h>
+
+#include "sysfs.h"
+
+#define BUFF_MAX       255
+
+/*************************
+ * H/W Resource Defintions
+ */
+#define CPUFREQ_PMQOS_PATH_PREFIX              "/sys/devices/system/cpu/cpufreq/pmqos"
+#define CPUFREQ_PATH_PREFIX                    "/sys/devices/system/cpu/"
+#define TMU_PATH_PREFIX                                "/sys/class/sec/temperature/"
+
+#define CPUFREQ_CPU_ONLINE_MIN_NUM_PATH_SUFFIX "/cpu_online_min"
+#define CPUFREQ_CPU_ONLINE_MAX_NUM_PATH_SUFFIX "/cpu_online_max"
+#define CPUFREQ_MAX_FREQ_PATH_SUFFIX           "/cpufreq_max"
+#define CPUFREQ_MIN_FREQ_PATH_SUFFIX           "/cpufreq_min"
+#define CPUFREQ_CURR_FREQ_PATH_SUFFIX          "/cpufreq/cpuinfo_cur_freq"
+
+#define CPUFREQ_DEFAULT_MIN_FREQ               0
+#define CPUFREQ_DEFAULT_MAX_FREQ               INT_MAX
+#define CPUFREQ_DEFAULT_ONLINE_MIN_NUM         0
+#define CPUFREQ_DEFAULT_ONLINE_MAX_NUM         4
+
+#define CPUFREQ_PMQOS_REQUEST_UNLOCK           -1
+
+#define SCENARIO_INFO_SOCKET_PATH      "/dev/socket/scenario_info"
+
+/*************************************************
+ * HAL backend implementation for CPU H/W Resource
+ */
+static int tm1_dvfs_get_curr_freq(char *res_name)
+{
+       char path[PATH_MAX];
+       int freq, ret;
+
+       snprintf(path, PATH_MAX, "%s%s%s",
+               CPUFREQ_PATH_PREFIX,
+               res_name,
+               CPUFREQ_CURR_FREQ_PATH_SUFFIX);
+
+       ret = sysfs_read_int(path, &freq);
+       if (ret < 0)
+               return ret;
+
+       return freq;
+}
+
+static int tm1_dvfs_get_min_freq(char *res_name)
+{
+       char path[PATH_MAX];
+       int min_freq;
+       int ret;
+
+       snprintf(path, PATH_MAX, "%s%s",
+                CPUFREQ_PMQOS_PATH_PREFIX,
+                CPUFREQ_MIN_FREQ_PATH_SUFFIX);
+
+       ret = sysfs_read_int(path, &min_freq);
+       if (ret < 0)
+               return ret;
+
+       return min_freq;
+}
+
+static int tm1_dvfs_set_min_freq(char *res_name, int freq)
+{
+       char path[PATH_MAX];
+       int ret;
+
+       ret = snprintf(path, PATH_MAX, "%s%s",
+               CPUFREQ_PMQOS_PATH_PREFIX,
+               CPUFREQ_MIN_FREQ_PATH_SUFFIX);
+
+       ret = sysfs_write_int(path, freq);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int tm1_dvfs_get_max_freq(char *res_name)
+{
+       char path[PATH_MAX];
+       int max_freq;
+       int ret;
+
+       snprintf(path, PATH_MAX, "%s%s",
+                CPUFREQ_PMQOS_PATH_PREFIX,
+                CPUFREQ_MAX_FREQ_PATH_SUFFIX);
+
+       ret = sysfs_read_int(path, &max_freq);
+       if (ret < 0)
+               return ret;
+
+       return max_freq;
+}
+
+static int tm1_dvfs_set_max_freq(char *res_name, int freq)
+{
+       char path[PATH_MAX];
+       int ret;
+
+       snprintf(path, PATH_MAX, "%s%s",
+               CPUFREQ_PMQOS_PATH_PREFIX,
+               CPUFREQ_MAX_FREQ_PATH_SUFFIX);
+
+       ret = sysfs_write_int(path, freq);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static struct pass_resource_dvfs_ops tm1_cpu_dvfs_ops =  {
+       .get_curr_governor = NULL,
+       .set_curr_governor = NULL,
+       .get_avail_governor = NULL,
+       .get_curr_freq = tm1_dvfs_get_curr_freq,
+       .get_min_freq = tm1_dvfs_get_min_freq,
+       .set_min_freq = tm1_dvfs_set_min_freq,
+       .get_max_freq = tm1_dvfs_get_max_freq,
+       .set_max_freq = tm1_dvfs_set_max_freq,
+       .get_up_threshold = NULL,
+       .set_up_threshold = NULL,
+       .get_load_table = NULL,
+};
+
+static int tm1_hotplug_get_online_min_num(char *res_name)
+{
+       char path[PATH_MAX];
+       int online_min_num;
+       int ret;
+
+       snprintf(path, PATH_MAX, "%s%s",
+               CPUFREQ_PMQOS_PATH_PREFIX,
+               CPUFREQ_CPU_ONLINE_MIN_NUM_PATH_SUFFIX);
+
+       ret = sysfs_read_int(path, &online_min_num);
+       if (ret < 0)
+               return ret;
+
+       return online_min_num;
+}
+
+static int tm1_hotplug_set_online_min_num(char *res_name, int num)
+{
+       char path[PATH_MAX];
+       int ret;
+
+       /* TODO: Can we turn off CPU0? */
+       snprintf(path, PATH_MAX, "%s%s",
+               CPUFREQ_PMQOS_PATH_PREFIX,
+               CPUFREQ_CPU_ONLINE_MIN_NUM_PATH_SUFFIX);
+
+       ret = sysfs_write_int(path, num);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+
+static int tm1_hotplug_get_online_max_num(char *res_name)
+{
+       char path[PATH_MAX];
+       int online_max_num;
+       int ret;
+
+       snprintf(path, PATH_MAX, "%s%s",
+               CPUFREQ_PMQOS_PATH_PREFIX,
+               CPUFREQ_CPU_ONLINE_MAX_NUM_PATH_SUFFIX);
+
+       ret = sysfs_read_int(path, &online_max_num);
+       if (ret < 0)
+               return ret;
+
+       return online_max_num;
+}
+
+static int tm1_hotplug_set_online_max_num(char *res_name, int num)
+{
+       char path[PATH_MAX];
+       int ret;
+
+       /* TODO: Can we turn off CPU0? */
+       snprintf(path, PATH_MAX, "%s%s",
+               CPUFREQ_PMQOS_PATH_PREFIX,
+               CPUFREQ_CPU_ONLINE_MAX_NUM_PATH_SUFFIX);
+
+       ret = sysfs_write_int(path, num);
+       if (ret < 0)
+               return ret;
+
+       return 0;
+}
+static struct pass_resource_hotplug_ops tm1_cpu_hotplug_ops = {
+       .get_online_state = NULL,
+       .set_online_state = NULL,
+       .get_online_min_num = tm1_hotplug_get_online_min_num,
+       .set_online_min_num = tm1_hotplug_set_online_min_num,
+       .get_online_max_num = tm1_hotplug_get_online_max_num,
+       .set_online_max_num = tm1_hotplug_set_online_max_num,
+};
+
+static int tm1_cpu_get_temp(char *res_thermal_name)
+{
+       char path[PATH_MAX + 1];
+       char buf[PATH_MAX + 1];
+       char *pos;
+       int temp = 0;
+       int ret;
+
+       if (!res_thermal_name)
+               return -EINVAL;
+
+       snprintf(path, PATH_MAX, "%s%s", TMU_PATH_PREFIX, res_thermal_name);
+
+       ret = sysfs_read_str(path, buf, PATH_MAX);
+       if (ret < 0)
+               return ret;
+
+       pos = strstr(buf, "temp:");
+       if (!pos || !(pos[5]))
+               return -EINVAL;
+       pos += 5;
+       temp = atoi(pos);
+
+       return temp;
+}
+
+static struct pass_resource_tmu_ops tm1_cpu_tmu_ops = {
+       .get_temp = tm1_cpu_get_temp,
+       .get_policy = NULL,
+};
+
+/*************************************************
+ * HAL backend implementation for Non-standard H/W Resource
+ */
+static int tm1_set_pmqos_data(char *res_name, void *data)
+{
+       struct sockaddr_un addr;
+       int socket_fd, ret;
+
+       if (!res_name || !data)
+               return -EINVAL;
+
+       errno = 0;
+       socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
+       if (socket_fd == -1)
+               return -errno;
+
+       memset(&addr, 0, sizeof(addr));
+       snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", SCENARIO_INFO_SOCKET_PATH);
+       addr.sun_family = AF_LOCAL;
+
+       errno = 0;
+       ret = connect(socket_fd, (struct sockaddr *) &addr, sizeof(addr));
+       if (ret) {
+               close(socket_fd);
+               return -errno;
+       }
+
+       errno = 0;
+       ret = send(socket_fd, data, strlen(data) + 1, MSG_NOSIGNAL);
+       if (ret)
+               ret = errno;
+
+       close(socket_fd);
+
+       return ret;
+}
+
+static int tm1_set_pmqos_data_nop(char *res_name, void *data)
+{
+       /*
+        * The tm1_set_pmqos_data function is required to send the PMQoS
+        * scenario information to the socket interface of the TRM and it
+        * is optional, which means that this HAL for nonstandard should work
+        * without TRM. For this reason, if there is no unix domain socket file
+        * for the PMQoS scenario, this nop function is assigned to the
+        * set_pmqos_data pointer rather than returning error.
+        */
+       return 0;
+}
+
+
+static int power_tm1_init(void **data)
+{
+       hal_backend_power_funcs *funcs = NULL;
+       struct pass_resource_cpu *cpu = NULL;
+       struct pass_resource_nonstandard *nonstandard = NULL;
+       int ret;
+
+       /* Allocate memory */
+       funcs = calloc(1, sizeof(hal_backend_power_funcs));
+       if (!funcs)
+               return -ENOMEM;
+
+       cpu = calloc(1, sizeof(struct pass_resource_cpu));
+       if (!cpu) {
+               return -ENOMEM;
+               goto err;
+       }
+
+       nonstandard = calloc(1, sizeof(struct pass_resource_nonstandard));
+       if (!nonstandard) {
+               ret = -ENOMEM;
+               goto err;
+       }
+
+       /* Initialize each h/w resource */
+       cpu->dvfs = tm1_cpu_dvfs_ops;
+       cpu->hotplug = tm1_cpu_hotplug_ops;
+       cpu->tmu = tm1_cpu_tmu_ops;
+
+       ret = access(SCENARIO_INFO_SOCKET_PATH, F_OK);
+       if (!ret)
+               nonstandard->set_pmqos_data = tm1_set_pmqos_data;
+       else
+               nonstandard->set_pmqos_data = tm1_set_pmqos_data_nop;
+
+       /* Initialize hal_backend_power_funcs  */
+       funcs->cpu = cpu;
+       funcs->nonstandard = nonstandard;
+
+       *data = (void *)funcs;
+
+       return 0;
+err:
+       if (nonstandard)
+               free(nonstandard);
+       if (cpu)
+               free(cpu);
+
+       free(funcs);
+
+       return ret;
+}
+
+static int power_tm1_exit(void *data)
+{
+       hal_backend_power_funcs *funcs = NULL;
+       struct pass_resource_cpu *cpu = NULL;
+       int ret;
+
+       if (!data)
+               return -EINVAL;
+
+       funcs = (hal_backend_power_funcs *)data;
+       cpu = funcs->cpu;
+
+       if (funcs->cpu) {
+               /*
+                * tm1 has weird pmqos sysfs node, thus we cannot apply save/restore
+                * feature as pass-hal-standard does. Instead of using standard
+                * interface, we manually restore the each resource state; pmqos
+                * request is unlocked by passing argument '-1' first, and resource
+                * state is restored by passing default value.
+                */
+               ret = cpu->hotplug.set_online_max_num(NULL, CPUFREQ_PMQOS_REQUEST_UNLOCK);
+               if (ret < 0)
+                       goto exit;
+               ret = cpu->hotplug.set_online_max_num(NULL, CPUFREQ_DEFAULT_ONLINE_MAX_NUM);
+               if (ret < 0)
+                       goto exit;
+
+               ret = cpu->hotplug.set_online_min_num(NULL, CPUFREQ_PMQOS_REQUEST_UNLOCK);
+               if (ret < 0)
+                       goto exit;
+               ret = cpu->hotplug.set_online_min_num(NULL, CPUFREQ_DEFAULT_ONLINE_MIN_NUM);
+               if (ret < 0)
+                       goto exit;
+
+               ret = cpu->dvfs.set_min_freq(NULL, CPUFREQ_PMQOS_REQUEST_UNLOCK);
+               if (ret < 0)
+                       goto exit;
+               ret = cpu->dvfs.set_min_freq(NULL, CPUFREQ_DEFAULT_MIN_FREQ);
+               if (ret < 0)
+                       goto exit;
+
+               ret = cpu->dvfs.set_max_freq(NULL,
+                                               CPUFREQ_PMQOS_REQUEST_UNLOCK);
+               if (ret < 0)
+                       goto exit;
+               ret = cpu->dvfs.set_max_freq(NULL, CPUFREQ_DEFAULT_MAX_FREQ);
+               if (ret < 0)
+                       goto exit;
+       }
+
+exit:
+       if (funcs->cpu)
+               free(funcs->cpu);
+       if (funcs->nonstandard)
+               free(funcs->nonstandard);
+
+       free(funcs);
+
+       return 0;
+}
+
+hal_backend hal_backend_power_data = {
+       .name = "power-tm1",
+       .vendor = "Samsung TM1",
+       .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
+       .init = power_tm1_init,
+       .exit = power_tm1_exit,
+};
diff --git a/src/nonstandard/CMakeLists.txt b/src/nonstandard/CMakeLists.txt
deleted file mode 100644 (file)
index 11777c0..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
-PROJECT(nonstandard C)
-
-INCLUDE(FindPkgConfig)
-pkg_check_modules(pkgs REQUIRED dlog glib-2.0)
-
-#HAL version
-string(REPLACE "." ";" HAL_VERSION_LIST ${HAL_VERSION})
-list(GET HAL_VERSION_LIST 0 HAL_VERSION_MAJOR)
-list(GET HAL_VERSION_LIST 1 HAL_VERSION_MINOR)
-list(GET HAL_VERSION_LIST 2 HAL_VERSION_REVISION)
-list(GET HAL_VERSION_LIST 3 HAL_VERSION_RELEASE)
-add_definitions(-DVER_MAJOR=${HAL_VERSION_MAJOR} -DVER_MINOR=${HAL_VERSION_MINOR})
-add_definitions(-DVER_REVISION=${HAL_VERSION_REVISION} -DVER_RELEASE=${HAL_VERSION_RELEASE})
-
-SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden")
-SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
-
-SET(SRCS ${PROJECT_NAME}.c
-       ../shared/sysfs.c)
-
-ADD_LIBRARY(${PROJECT_NAME} MODULE ${SRCS})
-SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES PREFIX "")
-INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${DEST_DIR} COMPONENT RuntimeLibraries)
diff --git a/src/nonstandard/nonstandard.c b/src/nonstandard/nonstandard.c
deleted file mode 100644 (file)
index 7392d39..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <errno.h>
-#include <limits.h>
-#include <linux/types.h>
-#include <linux/un.h>
-#include <pass/hal.h>
-#include <pass/hal-log.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <unistd.h>
-
-#include "../shared/sysfs.h"
-
-#define SCENARIO_INFO_SOCKET_PATH      "/dev/socket/scenario_info"
-
-static int tm1_set_pmqos_data(char *res_name, void *data)
-{
-       struct sockaddr_un addr;
-       int socket_fd, ret;
-
-       if (!res_name || !data)
-               return -EINVAL;
-
-       errno = 0;
-       socket_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
-       if (socket_fd == -1)
-               return -errno;
-
-       memset(&addr, 0, sizeof(addr));
-       snprintf(addr.sun_path, UNIX_PATH_MAX, "%s", SCENARIO_INFO_SOCKET_PATH);
-       addr.sun_family = AF_LOCAL;
-
-       errno = 0;
-       ret = connect(socket_fd, (struct sockaddr *) &addr, sizeof(addr));
-       if (ret) {
-               close(socket_fd);
-               return -errno;
-       }
-
-       errno = 0;
-       ret = send(socket_fd, data, strlen(data) + 1, MSG_NOSIGNAL);
-       if (ret)
-               ret = errno;
-
-       close(socket_fd);
-
-       return ret;
-}
-
-static int tm1_set_pmqos_data_nop(char *res_name, void *data)
-{
-       /*
-        * The tm1_set_pmqos_data function is required to send the PMQoS
-        * scenario information to the socket interface of the TRM and it
-        * is optional, which means that this HAL for nonstandard should work
-        * without TRM. For this reason, if there is no unix domain socket file
-        * for the PMQoS scenario, this nop function is assigned to the
-        * set_pmqos_data pointer rather than returning error.
-        */
-       return 0;
-}
-
-static int tm1_nonstandard_open(char *res_name, struct pass_resource_info *info,
-               struct pass_resource_common **common)
-{
-       struct pass_resource_nonstandard *nonstandard_res;
-       int ret;
-
-       if (!res_name || !info)
-               return -EINVAL;
-
-       /* TODO: Possibility of a memory leak */
-       nonstandard_res = calloc(1, sizeof(struct pass_resource_nonstandard));
-       if (!nonstandard_res)
-               return -ENOMEM;
-
-       nonstandard_res->common.info = info;
-
-       ret = access(SCENARIO_INFO_SOCKET_PATH, F_OK);
-       if (!ret)
-               nonstandard_res->set_pmqos_data = tm1_set_pmqos_data;
-       else
-               nonstandard_res->set_pmqos_data = tm1_set_pmqos_data_nop;
-
-       *common = (struct pass_resource_common *) nonstandard_res;
-
-       return 0;
-}
-
-static int tm1_nonstandard_close(char *res_name, struct pass_resource_common *common)
-{
-       if (!res_name || !common)
-               return -EINVAL;
-
-       free(common);
-
-       return 0;
-}
-
-HAL_MODULE_STRUCTURE = {
-       .magic = HAL_INFO_TAG,
-       .id = PASS_RESOURCE_NONSTANDARD_ID,
-       .name = PASS_RESOURCE_NONSTANDARD_NAME,
-       .open = tm1_nonstandard_open,
-       .close = tm1_nonstandard_close,
-};
diff --git a/src/shared/sysfs.c b/src/shared/sysfs.c
deleted file mode 100644 (file)
index 1256076..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "sysfs.h"
-
-static int sysfs_read_buf(char *path, char *buf, int len)
-{
-       int r, fd;
-
-       if ((!path) || (!buf) || (len < 0))
-               return -EINVAL;
-
-       fd = open(path, O_RDONLY);
-       if (fd == -1)
-               return -ENOENT;
-
-       r = read(fd, buf, len);
-       close(fd);
-
-       if ((r < 0) || (r > len))
-               return -EIO;
-
-       buf[r] = '\0';
-
-       return 0;
-}
-
-static int sysfs_write_buf(char *path, char *buf)
-{
-       int w, fd;
-
-       if ((!path) || (!buf))
-               return -EINVAL;
-
-       fd = open(path, O_WRONLY);
-       if (fd == -1)
-               return -ENOENT;
-
-       w = write(fd, buf, strlen(buf));
-       close(fd);
-
-       if (w < 0)
-               return -EIO;
-
-       return 0;
-}
-
-int sysfs_read_int(char *path, int *val)
-{
-       char buf[MAX_BUF_SIZE + 1];
-       int r;
-
-       if ((!path) || (!val))
-               return -EINVAL;
-
-       r = sysfs_read_buf(path, buf, MAX_BUF_SIZE);
-       if (r < 0)
-               return r;
-
-       *val = atoi(buf);
-       return 0;
-}
-
-int sysfs_read_str(char *path, char *str, int len)
-{
-       int r;
-
-       if ((!path) || (!str) || (len <= 0))
-               return -EINVAL;
-
-       r = sysfs_read_buf(path, str, len);
-       if (r < 0)
-               return r;
-
-       return 0;
-}
-
-int sysfs_write_int(char *path, int val)
-{
-       char buf[MAX_BUF_SIZE + 1];
-       int w;
-
-       if (!path)
-               return -EINVAL;
-
-       snprintf(buf, MAX_BUF_SIZE, "%d", val);
-       w = sysfs_write_buf(path, buf);
-       if (w < 0)
-               return w;
-
-       return 0;
-}
-
-int sysfs_write_str(char *path, char *str)
-{
-       int w;
-
-       if ((!path) || (!str))
-               return -EINVAL;
-
-       w = sysfs_write_buf(path, str);
-       if (w < 0)
-               return w;
-
-       return 0;
-}
diff --git a/src/shared/sysfs.h b/src/shared/sysfs.h
deleted file mode 100644 (file)
index 157eb15..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define MAX_BUF_SIZE   255
-
-int sysfs_read_int(char *path, int *val);
-int sysfs_read_str(char *path, char *str, int len);
-int sysfs_write_int(char *path, int val);
-int sysfs_write_str(char *path, char *str);
diff --git a/src/sysfs.c b/src/sysfs.c
new file mode 100644 (file)
index 0000000..1256076
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * PASS (Power Aware System Service)
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "sysfs.h"
+
+static int sysfs_read_buf(char *path, char *buf, int len)
+{
+       int r, fd;
+
+       if ((!path) || (!buf) || (len < 0))
+               return -EINVAL;
+
+       fd = open(path, O_RDONLY);
+       if (fd == -1)
+               return -ENOENT;
+
+       r = read(fd, buf, len);
+       close(fd);
+
+       if ((r < 0) || (r > len))
+               return -EIO;
+
+       buf[r] = '\0';
+
+       return 0;
+}
+
+static int sysfs_write_buf(char *path, char *buf)
+{
+       int w, fd;
+
+       if ((!path) || (!buf))
+               return -EINVAL;
+
+       fd = open(path, O_WRONLY);
+       if (fd == -1)
+               return -ENOENT;
+
+       w = write(fd, buf, strlen(buf));
+       close(fd);
+
+       if (w < 0)
+               return -EIO;
+
+       return 0;
+}
+
+int sysfs_read_int(char *path, int *val)
+{
+       char buf[MAX_BUF_SIZE + 1];
+       int r;
+
+       if ((!path) || (!val))
+               return -EINVAL;
+
+       r = sysfs_read_buf(path, buf, MAX_BUF_SIZE);
+       if (r < 0)
+               return r;
+
+       *val = atoi(buf);
+       return 0;
+}
+
+int sysfs_read_str(char *path, char *str, int len)
+{
+       int r;
+
+       if ((!path) || (!str) || (len <= 0))
+               return -EINVAL;
+
+       r = sysfs_read_buf(path, str, len);
+       if (r < 0)
+               return r;
+
+       return 0;
+}
+
+int sysfs_write_int(char *path, int val)
+{
+       char buf[MAX_BUF_SIZE + 1];
+       int w;
+
+       if (!path)
+               return -EINVAL;
+
+       snprintf(buf, MAX_BUF_SIZE, "%d", val);
+       w = sysfs_write_buf(path, buf);
+       if (w < 0)
+               return w;
+
+       return 0;
+}
+
+int sysfs_write_str(char *path, char *str)
+{
+       int w;
+
+       if ((!path) || (!str))
+               return -EINVAL;
+
+       w = sysfs_write_buf(path, str);
+       if (w < 0)
+               return w;
+
+       return 0;
+}
diff --git a/src/sysfs.h b/src/sysfs.h
new file mode 100644 (file)
index 0000000..157eb15
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * PASS (Power Aware System Service)
+ *
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define MAX_BUF_SIZE   255
+
+int sysfs_read_int(char *path, int *val);
+int sysfs_read_str(char *path, char *str, int len);
+int sysfs_write_int(char *path, int val);
+int sysfs_write_str(char *path, char *str);