diff options
-rw-r--r-- | src/hal-backend-power.c | 403 |
1 files changed, 138 insertions, 265 deletions
diff --git a/src/hal-backend-power.c b/src/hal-backend-power.c index c4f52ad..b25ad6f 100644 --- a/src/hal-backend-power.c +++ b/src/hal-backend-power.c @@ -65,227 +65,196 @@ #define FAULT_AROUND_BYTES_PATH "/sys/kernel/debug/fault_around_bytes" -/************************************************* - * HAL backend implementation for CPU H/W Resource - */ -static int cpufreq_dvfs_get_curr_governor(char *res_name, char *governor) +static int get_path(char *path, char *prefix, char *res_name, char *suffix) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (!governor)) + if (!path || !prefix || !res_name || !suffix) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX); + ret = snprintf(path, PATH_MAX, "%s%s%s", prefix, res_name, suffix); + if (ret <= 0) + return -EINVAL; - ret = sysfs_read_str(path, governor, BUFF_MAX); - if (ret < 0) - return ret; + path[PATH_MAX - 1] = '\0'; return 0; } -static int cpufreq_dvfs_set_curr_governor(char *res_name, char *governor) +static int __handle_value_string(int is_write, char *prefix, char *res_name, char *suffix, char *buf) { char path[PATH_MAX]; int ret; - if ((!res_name) || (!governor)) + if (!buf) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX); - - ret = sysfs_write_str(path, governor); + ret = get_path(path, prefix, res_name, suffix); if (ret < 0) return ret; - return 0; + if (!!is_write) + ret = sysfs_write_str(path, buf); + else + ret = sysfs_read_str(path, buf, BUFF_MAX); + + return (ret < 0) ? ret : 0; } -static int cpufreq_dvfs_get_curr_freq(char *res_name) +static int get_value_string(char *prefix, char *res_name, char *suffix, char *buf) +{ + return __handle_value_string(0, prefix, res_name, suffix, buf); +} + +static int set_value_string(char *prefix, char *res_name, char *suffix, char *buf) +{ + return __handle_value_string(1, prefix, res_name, suffix, buf); +} + +static int get_value_integer(char *prefix, char *res_name, char *suffix, int *value) { char path[PATH_MAX]; - int freq, ret; + int ret; - if (!res_name) + if (!value) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_CURR_FREQ_PATH_SUFFIX); + ret = get_path(path, prefix, res_name, suffix); + if (ret < 0) + return ret; - ret = sysfs_read_int(path, &freq); + ret = sysfs_read_int(path, value); if (ret < 0) return ret; - return freq; + return 0; } -static int cpufreq_dvfs_get_min_freq(char *res_name) +static int set_value_integer(char *prefix, char *res_name, char *suffix, int value) { char path[PATH_MAX]; - int freq, ret; - - if (!res_name) - return -EINVAL; + int ret; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_MIN_FREQ_PATH_SUFFIX); + ret = get_path(path, prefix, res_name, suffix); + if (ret < 0) + return ret; - ret = sysfs_read_int(path, &freq); + ret = sysfs_write_int(path, value); if (ret < 0) return ret; - return freq; + return 0; } -static int cpufreq_dvfs_set_min_freq(char *res_name, int freq) +/************************************************* + * HAL backend implementation for CPU H/W Resource + */ +static int cpufreq_dvfs_get_curr_governor(char *res_name, char *governor) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (freq < 0)) - return -EINVAL; + ret = get_value_string(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX, governor); + return (ret < 0) ? ret : 0; +} + +static int cpufreq_dvfs_set_curr_governor(char *res_name, char *governor) +{ + int ret; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_MIN_FREQ_PATH_SUFFIX); + ret = set_value_string(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX, governor); + return (ret < 0) ? ret : 0; +} - ret = sysfs_write_int(path, freq); - if (ret < 0) - return ret; +static int cpufreq_dvfs_get_curr_freq(char *res_name) +{ + int freq, ret; - return 0; + ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_CURR_FREQ_PATH_SUFFIX, &freq); + return (ret < 0) ? ret : freq; } -static int cpufreq_dvfs_get_max_freq(char *res_name) +static int cpufreq_dvfs_get_min_freq(char *res_name) { - char path[PATH_MAX]; int freq, ret; - if (!res_name) + ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_MIN_FREQ_PATH_SUFFIX, &freq); + return (ret < 0) ? ret : freq; +} + +static int cpufreq_dvfs_set_min_freq(char *res_name, int freq) +{ + int ret; + + if (freq < 0) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_MAX_FREQ_PATH_SUFFIX); + ret = set_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_MIN_FREQ_PATH_SUFFIX, freq); + return (ret < 0) ? ret : 0; +} - ret = sysfs_read_int(path, &freq); - if (ret < 0) - return ret; +static int cpufreq_dvfs_get_max_freq(char *res_name) +{ + int freq, ret; - return freq; + ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_MAX_FREQ_PATH_SUFFIX, &freq); + return (ret < 0) ? ret : freq; } static int cpufreq_dvfs_set_max_freq(char *res_name, int freq) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (freq < 0)) + if (freq < 0) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_MAX_FREQ_PATH_SUFFIX); - - ret = sysfs_write_int(path, freq); - if (ret < 0) - return ret; - return 0; + ret = set_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_MAX_FREQ_PATH_SUFFIX, freq); + return (ret < 0) ? ret : 0; } static int cpufreq_dvfs_get_available_min_freq(char *res_name) { - char path[PATH_MAX]; int val, ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_AVAILABLE_MIN_FREQ_PATH_SUFFIX); - - ret = sysfs_read_int(path, &val); - if (ret < 0) - return ret; - - return val; + ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_AVAILABLE_MIN_FREQ_PATH_SUFFIX, &val); + return (ret < 0) ? ret : val; } - static int cpufreq_dvfs_get_available_max_freq(char *res_name) { - char path[PATH_MAX]; int val, ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_AVAILABLE_MAX_FREQ_PATH_SUFFIX); - - ret = sysfs_read_int(path, &val); - if (ret < 0) - return ret; - - return val; + ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_AVAILABLE_MAX_FREQ_PATH_SUFFIX, &val); + return (ret < 0) ? ret : val; } static int cpufreq_dvfs_get_up_threshold(char *res_name) { - char path[PATH_MAX]; int val, ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_UP_THRESHOLD_PATH_SUFFIX); - - ret = sysfs_read_int(path, &val); - if (ret < 0) - return ret; - - return val; + ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_UP_THRESHOLD_PATH_SUFFIX, &val); + return (ret < 0) ? ret : val; } static int cpufreq_dvfs_set_up_threshold(char *res_name, int up_threshold) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (up_threshold < 0)) + if (up_threshold < 0) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - CPUFREQ_PATH_PREFIX, - res_name, - CPUFREQ_UP_THRESHOLD_PATH_SUFFIX); - - ret = sysfs_write_int(path, up_threshold); - if (ret < 0) - return ret; - - return 0; + ret = set_value_integer(CPUFREQ_PATH_PREFIX, res_name, + CPUFREQ_UP_THRESHOLD_PATH_SUFFIX, up_threshold); + return (ret < 0) ? ret : 0; } static struct pass_resource_dvfs_ops cpufreq_dvfs_ops = { @@ -365,19 +334,11 @@ static struct pass_resource_hotplug_ops cpu_hotplus_ops = { static int tmu_get_temp(char *res_thermal_name) { - char path[PATH_MAX]; int temp, ret; - if (!res_thermal_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - TMU_PATH_PREFIX, - res_thermal_name, - TMU_TEMP_PATH_SUFFIX); - - ret = sysfs_read_int(path, &temp); - if (ret < 0) + ret = get_value_integer(TMU_PATH_PREFIX, res_thermal_name, + TMU_TEMP_PATH_SUFFIX, &temp); + if (ret < 0) return ret; /* @@ -395,22 +356,14 @@ static int tmu_get_temp(char *res_thermal_name) static int tmu_get_policy(char *res_thermal_name, char *policy) { - char path[PATH_MAX]; int ret; - if ((!res_thermal_name) || (!policy)) + if (!policy) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - TMU_PATH_PREFIX, - res_thermal_name, - TMU_POLICY_PATH_SUFFIX); - - ret = sysfs_read_str(path, policy, BUFF_MAX); - if (ret < 0) - return ret; - - return 0; + ret = get_value_string(TMU_PATH_PREFIX, res_thermal_name, + TMU_POLICY_PATH_SUFFIX, policy); + return (ret < 0) ? ret : 0; } static struct pass_resource_tmu_ops tmu_ops = { @@ -423,160 +376,88 @@ static struct pass_resource_tmu_ops tmu_ops = { */ static int devfreq_dvfs_get_curr_governor(char *res_name, char *governor) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (!governor)) + if (!governor) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX); - - ret = sysfs_read_str(path, governor, BUFF_MAX); - if (ret < 0) - return ret; - - return 0; + ret = get_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX, governor); + return (ret < 0) ? ret : 0; } static int devfreq_dvfs_set_curr_governor(char *res_name, char *governor) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (!governor)) + if (!governor) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX); - - ret = sysfs_write_str(path, governor); - if (ret < 0) - return ret; - - return 0; + ret = set_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX, governor); + return (ret < 0) ? ret : 0; } static int devfreq_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", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_CURR_FREQ_PATH_SUFFIX); - - ret = sysfs_read_int(path, &freq); - if (ret < 0) - return ret; - - return freq; + ret = get_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_CURR_FREQ_PATH_SUFFIX, &freq); + return (ret < 0) ? ret : freq; } static int devfreq_dvfs_get_min_freq(char *res_name) { - char path[PATH_MAX]; int freq, ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX); - - ret = sysfs_read_int(path, &freq); - if (ret < 0) - return ret; - - return freq; + ret = get_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX, &freq); + return (ret < 0) ? ret : freq; } static int devfreq_dvfs_set_min_freq(char *res_name, int freq) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (freq < 0)) + if (freq < 0) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX); - - ret = sysfs_write_int(path, freq); - if (ret < 0) - return ret; - - return 0; + ret = set_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX, freq); + return (ret < 0) ? ret : 0; } static int devfreq_dvfs_get_max_freq(char *res_name) { - char path[PATH_MAX]; int freq, ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX); - - ret = sysfs_read_int(path, &freq); - if (ret < 0) - return ret; - - return freq; + ret = get_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX, &freq); + return (ret < 0) ? ret : freq; } static int devfreq_dvfs_set_max_freq(char *res_name, int freq) { - char path[PATH_MAX]; int ret; - if ((!res_name) || (freq < 0)) + if (freq < 0) return -EINVAL; - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX); - - ret = sysfs_write_int(path, freq); - if (ret < 0) - return ret; - return 0; + ret = set_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX, freq); + return (ret < 0) ? ret : 0; } static int devfreq_dvfs_get_available_min_freq(char *res_name) { - char path[PATH_MAX]; char buf[MAX_BUF_SIZE + 1]; char *p; int ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX); - - ret = sysfs_read_str(path, buf, MAX_BUF_SIZE); - if (ret < 0) + ret = get_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX, buf); + if (ret < 0) return ret; p = strchr(buf, ' '); @@ -592,21 +473,13 @@ static int devfreq_dvfs_get_available_min_freq(char *res_name) static int devfreq_dvfs_get_available_max_freq(char *res_name) { - char path[PATH_MAX]; char buf[MAX_BUF_SIZE + 1]; char *p; int ret; - if (!res_name) - return -EINVAL; - - snprintf(path, PATH_MAX, "%s%s%s", - DEVFREQ_BUS_PATH_PREFIX, - res_name, - DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX); - - ret = sysfs_read_str(path, buf, MAX_BUF_SIZE); - if (ret < 0) + ret = get_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name, + DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX, buf); + if (ret < 0) return ret; p = strrchr(buf, ' '); |