From: Chanwoo Choi Date: Mon, 29 Aug 2022 07:19:51 +0000 (+0900) Subject: halapi: power: Remove duplicate code X-Git-Tag: accepted/tizen/7.0/unified/20221110.063448^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cdd537a53bf797ad2c3b627d8108336a30091d53;p=platform%2Fhal%2Fapi%2Fpower.git halapi: power: Remove duplicate code Change-Id: I9fa73a2ae63cdd5d14dd6472b3a441a0b8f5530a Signed-off-by: Chanwoo Choi --- diff --git a/src/hal-api-power.c b/src/hal-api-power.c index 22846b4..ccc9b8c 100644 --- a/src/hal-api-power.c +++ b/src/hal-api-power.c @@ -79,92 +79,112 @@ static int is_supported_from_backend(hal_backend_power_funcs *funcs, int res_typ return 0; } -static struct pass_resource_dvfs_ops *get_dvfs(hal_backend_power_funcs *funcs, - int res_type) +static int get_dvfs(hal_backend_power_funcs *funcs, int res_type, char *res_name, + struct pass_resource_dvfs_ops **dvfs) { - struct pass_resource_dvfs_ops *dvfs = NULL; + if (!funcs) + return -ENOTSUP; + else if (!res_name) + return -EINVAL; switch (res_type) { case PASS_RESOURCE_CPU_ID: if (funcs && funcs->cpu) - dvfs = &(funcs->cpu->dvfs); + *dvfs = &(funcs->cpu->dvfs); break; case PASS_RESOURCE_BUS_ID: if (funcs && funcs->bus) - dvfs = &(funcs->bus->dvfs); + *dvfs = &(funcs->bus->dvfs); break; case PASS_RESOURCE_GPU_ID: if (funcs && funcs->gpu) - dvfs = &(funcs->gpu->dvfs); + *dvfs = &(funcs->gpu->dvfs); break; + default: + return -EPERM; } - return dvfs; + return 0; } -static struct pass_resource_tmu_ops *get_tmu(hal_backend_power_funcs *funcs, - int res_type) +static int get_tmu(hal_backend_power_funcs *funcs, int res_type, char *res_name, + struct pass_resource_tmu_ops **tmu) { - struct pass_resource_tmu_ops *tmu = NULL; + if (!funcs) + return -ENOTSUP; + else if (!res_name) + return -EINVAL; switch (res_type) { case PASS_RESOURCE_CPU_ID: if (funcs && funcs->cpu) - tmu = &(funcs->cpu->tmu); + *tmu = &(funcs->cpu->tmu); break; case PASS_RESOURCE_BUS_ID: if (funcs && funcs->bus) - tmu = &(funcs->bus->tmu); + *tmu = &(funcs->bus->tmu); break; case PASS_RESOURCE_GPU_ID: if (funcs && funcs->gpu) - tmu = &(funcs->gpu->tmu); + *tmu = &(funcs->gpu->tmu); break; case PASS_RESOURCE_BATTERY_ID: if (funcs && funcs->battery) - tmu = &(funcs->battery->tmu); + *tmu = &(funcs->battery->tmu); break; case PASS_RESOURCE_NONSTANDARD_ID: if (funcs && funcs->nonstandard) - tmu = &(funcs->nonstandard->tmu); + *tmu = &(funcs->nonstandard->tmu); break; + default: + return -EPERM; } - return tmu; + return 0; } -static struct pass_resource_hotplug_ops *get_hotplug(hal_backend_power_funcs *funcs, - int res_type) +static int get_hotplug(hal_backend_power_funcs *funcs, int res_type, char *res_name, + struct pass_resource_hotplug_ops **hotplug) { - struct pass_resource_hotplug_ops *hotplug = NULL; + if (!funcs) + return -ENOTSUP; + else if (!res_name) + return -EINVAL; switch (res_type) { case PASS_RESOURCE_CPU_ID: if (funcs && funcs->cpu) - hotplug = &(funcs->cpu->hotplug); + *hotplug = &(funcs->cpu->hotplug); break; + default: + return -EPERM; } - return hotplug; + return 0; } -static struct pass_resource_battery_ops *get_charging(hal_backend_power_funcs *funcs, - int res_type) +static int get_charging(hal_backend_power_funcs *funcs, int res_type, char *res_name, + struct pass_resource_battery_ops **charging) { - struct pass_resource_battery_ops *charging = NULL; + if (!funcs) + return -ENOTSUP; + else if (!res_name) + return -EINVAL; switch (res_type) { case PASS_RESOURCE_BATTERY_ID: if (funcs && funcs->battery) - charging = &(funcs->battery->battery); + *charging = &(funcs->battery->battery); break; case PASS_RESOURCE_NONSTANDARD_ID: if (funcs && funcs->nonstandard) - charging = &(funcs->nonstandard->battery); + *charging = &(funcs->nonstandard->battery); break; + default: + return -EPERM; } - return charging; + return 0; } EXPORT int hal_power_get_backend(unsigned int res_type) @@ -205,15 +225,6 @@ EXPORT int hal_power_put_backend(void) return 0; } -static int is_valid_param(char *res_name) -{ - if (!g_power_funcs) - return -ENOTSUP; - else if (!res_name) - return -EINVAL; - return 0; -} - static int is_valid_param_with_str(char *res_name, char *str) { if (!g_power_funcs) @@ -262,13 +273,12 @@ EXPORT int hal_power_dvfs_get_curr_governor(unsigned int res_type, struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param_with_str(res_name, governor); - if (ret < 0) - return ret; + if (!governor) + return -EINVAL; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) + return ret; return exec_func_with_str(dvfs->get_curr_governor, res_name, governor); } @@ -278,13 +288,12 @@ EXPORT int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_nam struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param_with_str(res_name, governor); - if (ret < 0) - return ret; + if (!governor) + return -EINVAL; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) + return ret; return exec_func_with_str(dvfs->set_curr_governor, res_name, governor); } @@ -292,16 +301,11 @@ EXPORT int hal_power_dvfs_set_curr_governor(unsigned int res_type, char *res_nam EXPORT int hal_power_dvfs_get_avail_governor(unsigned int res_type, char *res_name, char **avail_governor) { struct pass_resource_dvfs_ops *dvfs; + int ret; - if (!g_power_funcs) - return -ENOTSUP; - - if (!res_name) - return -EINVAL; - - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) + return ret; if (!dvfs->get_avail_governor) return -ENOTSUP; @@ -315,14 +319,10 @@ EXPORT int hal_power_dvfs_get_curr_freq(unsigned int res_type, char *res_name) struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - return exec_func(dvfs->get_curr_freq, res_name); } @@ -332,14 +332,10 @@ EXPORT int hal_power_dvfs_get_min_freq(unsigned int res_type, char *res_name) struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - return exec_func(dvfs->get_min_freq, res_name); } @@ -348,13 +344,12 @@ EXPORT int hal_power_dvfs_set_min_freq(unsigned int res_type, char *res_name, in struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param_with_int(res_name, freq); - if (ret < 0) - return ret; + if (freq < 0) + return -EINVAL; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) + return ret; return exec_func_with_int(dvfs->set_min_freq, res_name, freq); } @@ -365,14 +360,10 @@ EXPORT int hal_power_dvfs_get_max_freq(unsigned int res_type, char *res_name) struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - return exec_func(dvfs->get_max_freq, res_name); } @@ -381,13 +372,12 @@ EXPORT int hal_power_dvfs_set_max_freq(unsigned int res_type, char *res_name, in struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param_with_int(res_name, freq); - if (ret < 0) - return ret; + if (freq < 0) + return -EINVAL; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) + return ret; return exec_func_with_int(dvfs->set_max_freq, res_name, freq); } @@ -398,14 +388,10 @@ EXPORT int hal_power_dvfs_get_available_min_freq(unsigned int res_type, char *re struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - return exec_func(dvfs->get_available_min_freq, res_name); } @@ -414,14 +400,10 @@ EXPORT int hal_power_dvfs_get_available_max_freq(unsigned int res_type, char *re struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - return exec_func(dvfs->get_available_max_freq, res_name); } @@ -431,14 +413,10 @@ EXPORT int hal_power_dvfs_get_up_threshold(unsigned int res_type, char *res_name struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - return exec_func(dvfs->get_up_threshold, res_name); } @@ -447,13 +425,12 @@ EXPORT int hal_power_dvfs_set_up_threshold(unsigned int res_type, char *res_name struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param_with_int(res_name, up_threshold); - if (ret < 0) - return ret; + if (up_threshold < 0) + return -EINVAL; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) + return ret; return exec_func_with_int(dvfs->set_up_threshold, res_name, up_threshold); } @@ -464,14 +441,10 @@ EXPORT int hal_power_dvfs_get_load_table(unsigned int res_type, char *res_name, struct pass_resource_dvfs_ops *dvfs; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_dvfs(g_power_funcs, res_type, res_name, &dvfs); + if (ret < 0 || !dvfs) return ret; - dvfs = get_dvfs(g_power_funcs, res_type); - if (!dvfs) - return -EPERM; - if (!dvfs->get_load_table) return -ENOTSUP; @@ -487,18 +460,14 @@ EXPORT int hal_power_hotplug_get_online_state(unsigned int res_type, char *res_n struct pass_resource_hotplug_ops *hotplug; int ret; - ret = is_valid_param_with_int(res_name, cpu); - if (ret < 0) - return ret; - - hotplug = get_hotplug(g_power_funcs, res_type); - if (!hotplug) - return -EPERM; + if (cpu < 0) + return -EINVAL; - if (!hotplug->get_online_state) - return -ENOTSUP; + ret = get_hotplug(g_power_funcs, res_type, res_name, &hotplug); + if (ret < 0 || !hotplug) + return ret; - return hotplug->get_online_state(res_name, cpu); + return exec_func_with_int(hotplug->get_online_state, res_name, cpu); } EXPORT int hal_power_hotplug_set_online_state(unsigned int res_type, char *res_name, int cpu, int on) @@ -506,16 +475,12 @@ EXPORT int hal_power_hotplug_set_online_state(unsigned int res_type, char *res_n struct pass_resource_hotplug_ops *hotplug; int ret; - ret = is_valid_param_with_int(res_name, cpu); - if (ret < 0) - return ret; - - if (on < 0) + if (cpu < 0 || on < 0) return -EINVAL; - hotplug = get_hotplug(g_power_funcs, res_type); - if (!hotplug) - return -EPERM; + ret = get_hotplug(g_power_funcs, res_type, res_name, &hotplug); + if (ret < 0 || !hotplug) + return ret; if (!hotplug->set_online_state) return -ENOTSUP; @@ -529,14 +494,10 @@ EXPORT int hal_power_hotplug_get_online_min_num(unsigned int res_type, char *res struct pass_resource_hotplug_ops *hotplug; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_hotplug(g_power_funcs, res_type, res_name, &hotplug); + if (ret < 0 || !hotplug) return ret; - hotplug = get_hotplug(g_power_funcs, res_type); - if (!hotplug) - return -EPERM; - return exec_func(hotplug->get_online_min_num, res_name); } @@ -546,13 +507,12 @@ EXPORT int hal_power_hotplug_set_online_min_num(unsigned int res_type, struct pass_resource_hotplug_ops *hotplug; int ret; - ret = is_valid_param_with_int(res_name, min_num); - if (ret < 0) - return ret; + if (min_num < 0) + return -EINVAL; - hotplug = get_hotplug(g_power_funcs, res_type); - if (!hotplug) - return -EPERM; + ret = get_hotplug(g_power_funcs, res_type, res_name, &hotplug); + if (ret < 0 || !hotplug) + return ret; return exec_func_with_int(hotplug->set_online_min_num, res_name, min_num); } @@ -564,14 +524,10 @@ EXPORT int hal_power_hotplug_get_online_max_num(unsigned int res_type, struct pass_resource_hotplug_ops *hotplug; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_hotplug(g_power_funcs, res_type, res_name, &hotplug); + if (ret < 0 || !hotplug) return ret; - hotplug = get_hotplug(g_power_funcs, res_type); - if (!hotplug) - return -EPERM; - return exec_func(hotplug->get_online_max_num, res_name); } @@ -581,13 +537,12 @@ EXPORT int hal_power_hotplug_set_online_max_num(unsigned int res_type, struct pass_resource_hotplug_ops *hotplug; int ret; - ret = is_valid_param_with_int(res_name, max_num); - if (ret < 0) - return ret; + if (max_num < 0) + return -EINVAL; - hotplug = get_hotplug(g_power_funcs, res_type); - if (!hotplug) - return -EPERM; + ret = get_hotplug(g_power_funcs, res_type, res_name, &hotplug); + if (ret < 0 || !hotplug) + return ret; return exec_func_with_int(hotplug->set_online_max_num, res_name, max_num); } @@ -602,14 +557,10 @@ EXPORT int hal_power_thermal_get_temp(unsigned int res_type, struct pass_resource_tmu_ops *tmu; int ret; - ret = is_valid_param(res_thermal_name); - if (ret < 0) + ret = get_tmu(g_power_funcs, res_type, res_thermal_name, &tmu); + if (ret < 0 || !tmu) return ret; - tmu = get_tmu(g_power_funcs, res_type); - if (!tmu) - return -EPERM; - /* * In the case of the HAL TMU ops, res_thermal_name is used * as the first argument instead of res_name. @@ -625,13 +576,12 @@ EXPORT int hal_power_thermal_get_policy(unsigned int res_type, struct pass_resource_tmu_ops *tmu; int ret; - ret = is_valid_param_with_str(res_thermal_name, policy); - if (ret < 0) - return ret; + if (!policy) + return -EINVAL; - tmu = get_tmu(g_power_funcs, res_type); - if (!tmu) - return -EPERM; + ret = get_tmu(g_power_funcs, res_type, res_thermal_name, &tmu); + if (ret < 0 || !tmu) + return ret; /* * In the case of the HAL TMU ops, res_thermal_name is used @@ -648,13 +598,12 @@ EXPORT int hal_power_thermal_set_cooling_device_state(unsigned int device_type, struct pass_resource_tmu_ops *tmu; int ret; - ret = is_valid_param_with_int(cooling_device_name, state); - if (ret < 0) - return ret; + if (state < 0) + return -EINVAL; - tmu = get_tmu(g_power_funcs, device_type); - if (!tmu) - return -EPERM; + ret = get_tmu(g_power_funcs, device_type, cooling_device_name, &tmu); + if (ret < 0 || !tmu) + return ret; /* * In the case of the HAL TMU ops, cooling_device_name is used @@ -669,14 +618,10 @@ EXPORT int hal_power_thermal_get_cooling_device_state(unsigned int device_type, struct pass_resource_tmu_ops *tmu; int ret; - ret = is_valid_param(cooling_device_name); - if (ret < 0) + ret= get_tmu(g_power_funcs, device_type, cooling_device_name, &tmu); + if (ret < 0 || !tmu) return ret; - tmu = get_tmu(g_power_funcs, device_type); - if (!tmu) - return -EPERM; - /* * In the case of the HAL TMU ops, cooling_device_name is used * as the first argument instead of res_name. @@ -690,14 +635,10 @@ EXPORT int hal_power_thermal_get_cooling_device_max_state(unsigned int device_ty struct pass_resource_tmu_ops *tmu; int ret; - ret = is_valid_param(cooling_device_name); - if (ret < 0) + ret = get_tmu(g_power_funcs, device_type, cooling_device_name, &tmu); + if (ret < 0 || !tmu) return ret; - tmu = get_tmu(g_power_funcs, device_type); - if (!tmu) - return -EPERM; - /* * In the case of the HAL TMU ops, cooling_device_name is used * as the first argument instead of res_name. @@ -712,13 +653,12 @@ EXPORT int hal_power_battery_set_charging_status(unsigned int device_type, struct pass_resource_battery_ops *charging; int ret; - ret = is_valid_param_with_int(res_name, state); - if (ret < 0) - return ret; + if (state < 0) + return -EINVAL; - charging = get_charging(g_power_funcs, device_type); - if (!charging) - return -EPERM; + ret = get_charging(g_power_funcs, device_type, res_name, &charging); + if (ret < 0 || !charging) + return ret; return exec_func_with_int(charging->set_charging_status, res_name, state); } @@ -729,14 +669,10 @@ EXPORT int hal_power_battery_get_charging_status(unsigned int device_type, struct pass_resource_battery_ops *charging; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_charging(g_power_funcs, device_type, res_name, &charging); + if (ret < 0 || !charging) return ret; - charging = get_charging(g_power_funcs, device_type); - if (!charging) - return -EPERM; - return exec_func(charging->get_charging_status, res_name); } @@ -747,13 +683,12 @@ EXPORT int hal_power_battery_set_charging_current(unsigned int device_type, struct pass_resource_battery_ops *charging; int ret; - ret = is_valid_param_with_int(res_name, charing_current_uA); - if (ret < 0) - return ret; + if (charing_current_uA < 0) + return -EINVAL; - charging = get_charging(g_power_funcs, device_type); - if (!charging) - return -EPERM; + ret = get_charging(g_power_funcs, device_type, res_name, &charging); + if (ret < 0 || !charging) + return ret; return exec_func_with_int(charging->set_charging_current, res_name, charing_current_uA); } @@ -764,14 +699,10 @@ EXPORT int hal_power_battery_get_charging_current(unsigned int device_type, struct pass_resource_battery_ops *charging; int ret; - ret = is_valid_param(res_name); - if (ret < 0) + ret = get_charging(g_power_funcs, device_type, res_name, &charging); + if (ret < 0 || !charging) return ret; - charging = get_charging(g_power_funcs, device_type); - if (!charging) - return -EPERM; - return exec_func(charging->get_charging_current, res_name); } @@ -785,9 +716,8 @@ EXPORT int hal_power_memory_get_fault_around_bytes(unsigned int res_type, struct pass_resource_memory *memory; int ret; - ret = is_valid_param(res_name); - if (ret < 0) - return ret; + if (!res_name) + return -EINVAL; switch (res_type) { case PASS_RESOURCE_MEMORY_ID: