#include <stdio.h>
#include <stdint.h>
+#include <stdlib.h>
#include <dlfcn.h>
#include <dlog.h>
return 0;
}
+static int init_backend(void **data, void *user_data)
+{
+ hal_backend_power_funcs *power_funcs = NULL;
+ int ret;
+
+ power_funcs = calloc(1, sizeof(hal_backend_power_funcs));
+ if (!power_funcs)
+ return -ENOMEM;
+
+ power_funcs->cpu = calloc(1, sizeof(struct pass_resource_cpu));
+ if (!power_funcs->cpu) {
+ ret = -ENOMEM;
+ goto err_funcs;
+ }
+
+ power_funcs->memory = calloc(1, sizeof(struct pass_resource_memory));
+ if (!power_funcs->memory) {
+ ret = -ENOMEM;
+ goto err_cpu;
+ }
+
+ *data = (void *)power_funcs;
+
+ return 0;
+err_cpu:
+ if (power_funcs->cpu)
+ free(power_funcs->cpu);
+err_funcs:
+ free(power_funcs);
+
+ return ret;
+}
+
+static int exit_backend(void *data, void *user_data)
+{
+ hal_backend_power_funcs *funcs;
+
+ if (!data)
+ return -EINVAL;
+
+ funcs = (hal_backend_power_funcs *)data;
+
+ if (funcs->cpu)
+ free(funcs->cpu);
+ if (funcs->memory)
+ free(funcs->memory);
+
+ free(funcs);
+
+ return 0;
+}
+
EXPORT int hal_power_get_backend(unsigned int res_type)
{
g_power_funcs_count++;
if (!g_power_funcs) {
int ret;
- ret = hal_common_get_backend(HAL_MODULE_POWER,
- (void **)&g_power_funcs);
+ ret = hal_common_get_backend_v2(HAL_MODULE_POWER,
+ (void **)&g_power_funcs, NULL, init_backend);
if (ret < 0) {
_E("Failed to get backend of HAL_MODULE_POWER\n");
return ret;
if (--g_power_funcs_count > 0)
return 0;
- hal_common_put_backend(HAL_MODULE_POWER, (void *)g_power_funcs);
+ hal_common_put_backend_v2(HAL_MODULE_POWER,
+ (void *)g_power_funcs, NULL, exit_backend);
g_power_funcs = NULL;
return 0;
static int power_init(void **data)
{
- hal_backend_power_funcs *power_funcs = NULL;
- struct pass_resource_cpu *cpu = NULL;
- struct pass_resource_bus *bus = NULL;
- struct pass_resource_gpu *gpu = NULL;
- struct pass_resource_memory *memory = NULL;
- int ret;
-
- /* Allocate memory */
- power_funcs = calloc(1, sizeof(hal_backend_power_funcs));
+ hal_backend_power_funcs *power_funcs = *(hal_backend_power_funcs **)data;
+
if (!power_funcs)
- return -ENOMEM;
-
- cpu = calloc(1, sizeof(struct pass_resource_cpu));
- if (!cpu) {
- ret = -ENOMEM;
- goto err_cpu;
- }
-
- bus = calloc(1, sizeof(struct pass_resource_bus));
- if (!bus) {
- ret = -ENOMEM;
- goto err_bus;
- }
-
- gpu = calloc(1, sizeof(struct pass_resource_gpu));
- if (!gpu) {
- ret = -ENOMEM;
- goto err_gpu;
- }
-
- memory = calloc(1, sizeof(struct pass_resource_memory));
- if (!memory) {
- ret = -ENOMEM;
- goto err_memory;
- }
-
- /* Initialize each h/w resource */
- cpu->dvfs = cpufreq_dvfs_ops;
- cpu->hotplug = cpu_hotplus_ops;
- cpu->tmu = tmu_ops;
-
- bus->dvfs = devfreq_dvfs_ops;
- bus->tmu = tmu_ops;
-
- gpu->dvfs = devfreq_dvfs_ops;
- gpu->tmu = tmu_ops;
-
- memory->get_fault_around_bytes = get_value;
- memory->set_fault_around_bytes = set_value;
-
- /* Initialize hal_backend_power_funcs */
- power_funcs->cpu = cpu;
- power_funcs->bus = bus;
- power_funcs->gpu = gpu;
- power_funcs->memory = memory;
-
- *data = (void *)power_funcs;
+ return -EINVAL;
- return 0;
+ power_funcs->cpu->dvfs = cpufreq_dvfs_ops;
+ power_funcs->cpu->hotplug = cpu_hotplus_ops;
+ power_funcs->cpu->tmu = tmu_ops;
-err_memory:
- free(gpu);
-err_gpu:
- free(bus);
-err_bus:
- free(cpu);
-err_cpu:
- free(power_funcs);
+ power_funcs->bus->dvfs = devfreq_dvfs_ops;
+ power_funcs->bus->tmu = tmu_ops;
- return ret;
+ power_funcs->gpu->dvfs = devfreq_dvfs_ops;
+ power_funcs->gpu->tmu = tmu_ops;
+
+ power_funcs->memory->get_fault_around_bytes = get_value;
+ power_funcs->memory->set_fault_around_bytes = set_value;
+
+ return 0;
}
static int power_exit(void *data)
{
- hal_backend_power_funcs *funcs;
-
- if (!data)
- return -EINVAL;
-
- funcs = (hal_backend_power_funcs *)data;
-
- free(funcs->cpu);
- free(funcs->bus);
- free(funcs->gpu);
- free(funcs->memory);
- free(funcs);
-
return 0;
}
extern hal_backend hal_backend_power_data;
/*
- * Re-implement hal_common_get_backend/hal_common_put_backend for hooking
+ * Re-implement hal_common_get_backend_v2/hal_common_put_backend_v2 for hooking
* in order to test hal-api-power functions when building package.
*/
-int hal_common_get_backend(enum hal_module module, void **data) {
+
+static int __init_backend(void **data, void *user_data)
+{
+ hal_backend_power_funcs *power_funcs = NULL;
+ int ret;
+
+ power_funcs = (hal_backend_power_funcs *)calloc(1,
+ sizeof(hal_backend_power_funcs));
+ if (!power_funcs)
+ return -ENOMEM;
+
+ power_funcs->cpu = (struct pass_resource_cpu *)calloc(1,
+ sizeof(struct pass_resource_cpu));
+ if (!power_funcs->cpu) {
+ ret = -ENOMEM;
+ goto err_cpu;
+ }
+
+ power_funcs->bus = (struct pass_resource_bus *)calloc(1,
+ sizeof(struct pass_resource_bus));
+ if (!power_funcs->bus) {
+ ret = -ENOMEM;
+ goto err_bus;
+ }
+
+ power_funcs->gpu = (struct pass_resource_gpu *)calloc(1,
+ sizeof(struct pass_resource_gpu));
+ if (!power_funcs->gpu) {
+ ret = -ENOMEM;
+ goto err_gpu;
+ }
+
+ power_funcs->memory = (struct pass_resource_memory *)calloc(1,
+ sizeof(struct pass_resource_memory));
+ if (!power_funcs->memory) {
+ ret = -ENOMEM;
+ goto err_memory;
+ }
+
+ *data = (void *)power_funcs;
+
+ return 0;
+
+err_memory:
+ free(power_funcs->gpu);
+err_gpu:
+ free(power_funcs->bus);
+err_bus:
+ free(power_funcs->cpu);
+err_cpu:
+ free(power_funcs);
+
+ return ret;
+}
+
+static int __exit_backend(void *data, void *user_data)
+{
+ hal_backend_power_funcs *funcs;
+
+ if (!data)
+ return -EINVAL;
+
+ funcs = (hal_backend_power_funcs *)data;
+
+ free(funcs->cpu);
+ free(funcs->bus);
+ free(funcs->gpu);
+ free(funcs->memory);
+ free(funcs);
+
+ return 0;
+}
+
+int hal_common_get_backend_v2(enum hal_module module,
+ void **data, void *user_data,
+ int (*init_backend)(void **data, void *user_data))
+{
hal_backend *backend = &hal_backend_power_data;
+ int ret;
+
+ ret = __init_backend(data, user_data);
+ if (ret < 0)
+ return ret;
return backend->init(data);
}
-int hal_common_put_backend(enum hal_module module, void *data) {
+int hal_common_put_backend_v2(enum hal_module module,
+ void *data, void *user_data,
+ int (*exit_backend)(void *data, void *user_data))
+{
hal_backend *backend = &hal_backend_power_data;
+ int ret;
+
+ ret = backend->exit(data);
+ if (ret < 0)
+ return ret;
- return backend->exit(data);
+ return __exit_backend(data, user_data);
}
static int resources[] = {