4 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include "hal-api-conf.h"
29 #define EXPORT __attribute__ ((visibility("default")))
32 static enum hal_abi_version g_platform_curr_abi_version;
35 const char *hal_common_get_backend_library_name(enum hal_module module)
38 struct __hal_module_info *info = NULL;
40 /* Check parameter whether is valid or not */
41 if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
42 _E("Invalid parameter of HAL module (%d)\n", module);
46 info = _hal_api_conf_get_module_info(module);
48 _E("Failed to get HAL module(%d) information\n", module);
52 #if defined(__aarch64__)
53 library_name = info->library_name_64bit;
55 library_name = info->library_name;
59 _E("%s backend library name is NULL\n", info->module_name);
66 const char *hal_common_get_backend_symbol_name(enum hal_module module)
68 struct __hal_module_info *info = NULL;
70 /* Check parameter whether is valid or not */
71 if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
72 _E("Invalid paramer of HAL module (%d)\n", module);
76 info = _hal_api_conf_get_module_info(module);
78 _E("Failed to get HAL module(%d) information\n", module);
82 if (!info->symbol_name) {
83 _E("%s backend library name is NULL\n", info->module_name);
87 return info->symbol_name;
91 int hal_common_get_backend(enum hal_module module, void **data)
93 struct __hal_module_info *info = NULL;
96 const char *library_name, *symbol_name;
99 info = _hal_api_conf_get_module_info(module);
101 _E("Failed to get HAL module(%d) information\n", module);
102 ret = TIZEN_ERROR_UNKNOWN;
107 library_name = hal_common_get_backend_library_name(module);
109 _E("%s: Failed to get backend library name\n",
111 return TIZEN_ERROR_INVALID_PARAMETER;
114 handle = dlopen(library_name, RTLD_LAZY);
116 _E("%s: Failed to load shared library (%s)\n",
117 info->module_name, dlerror());
118 return TIZEN_ERROR_INVALID_PARAMETER;
121 symbol_name = hal_common_get_backend_symbol_name(module);
123 _E("%s: Failed to get backend symbol name\n",
125 ret = TIZEN_ERROR_INVALID_PARAMETER;
129 backend = dlsym(handle, symbol_name);
131 _E("%s: Failed to find backend data (%s)\n",
132 info->module_name, dlerror());
133 ret = TIZEN_ERROR_INVALID_PARAMETER;
137 /* Check HAL ABI Version */
138 ret = hal_common_check_backend_abi_version(module, backend->abi_version);
140 _E("%s: Failed to check ABI version\n",
142 ret = TIZEN_ERROR_INVALID_PARAMETER;
146 /* Get the backend module data */
147 if (!backend->init) {
148 _E("%s: hal_backend->init() is NULL\n",
150 ret = TIZEN_ERROR_INVALID_PARAMETER;
154 ret = backend->init(data);
156 _E("%s: Failed to initialize backend: name(%s)/vendor(%s)\n",
157 info->module_name, backend->name, backend->vendor);
158 ret = TIZEN_ERROR_INVALID_PARAMETER;
162 info->library_backend = backend;
163 info->library_handle = handle;
165 _I("%s: Get HAL backend: name(%s)/vendor(%s)\n",
166 info->module_name, backend->name, backend->vendor);
168 return TIZEN_ERROR_NONE;
175 int hal_common_put_backend(enum hal_module module, void *data)
177 struct __hal_module_info *info = NULL;
178 hal_backend *backend = NULL;
182 /* Check parameter whether is valid or not */
183 if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
184 _E("Invalid parameter of HAL module (%d)\n", module);
185 return TIZEN_ERROR_INVALID_PARAMETER;
188 info = _hal_api_conf_get_module_info(module);
190 _E("Failed to get HAL module(%d) information\n", module);
191 return TIZEN_ERROR_UNKNOWN;
194 backend = info->library_backend;
195 handle = info->library_handle;
198 _I("%s: Already fully put for HAL module\n", info->module_name);
199 return TIZEN_ERROR_NONE;
203 ret = backend->exit(data);
205 _E("%s: Failed to exit backend: name(%s)/vendor(%s)\n",
206 info->module_name, backend->name, backend->vendor);
207 return TIZEN_ERROR_INVALID_PARAMETER;
211 _I("%s: Put HAL backend: name(%s)/vendor(%s)\n",
212 info->module_name, backend->name, backend->vendor);
217 info->library_backend = NULL;
218 info->library_handle = NULL;
220 return TIZEN_ERROR_NONE;
224 int hal_common_check_backend_abi_version(enum hal_module module,
225 enum hal_abi_version abi_version)
227 struct __hal_module_info *info = NULL;
230 /* Check parameter whether is valid or not */
231 if (module <= HAL_MODULE_UNKNOWN || module >= HAL_MODULE_END) {
232 _E("Invalid paramer of HAL module(%d)\n", module);
233 return TIZEN_ERROR_INVALID_PARAMETER;
236 if (abi_version <= HAL_ABI_VERSION_UNKNOWN
237 || abi_version >= HAL_ABI_VERSION_END) {
238 _E("Invalid paramer of HAL ABI version(%d) for HAL module(%d)\n",
239 abi_version, module);
240 return TIZEN_ERROR_INVALID_PARAMETER;
243 info = _hal_api_conf_get_module_info(module);
245 _E("Failed to get HAL module(%d) information\n", module);
246 return TIZEN_ERROR_UNKNOWN;
249 /* Check abi_version whether is supported or not */
250 if (!info->hal_api) {
251 _E("%s: Doesn't support HAL API\n", info->module_name);
252 return TIZEN_ERROR_INVALID_PARAMETER;
255 if (!info->num_abi_versions
256 || !info->abi_versions) {
257 _E("%s: Doesn't have the ABI version information\n",
259 return TIZEN_ERROR_INVALID_PARAMETER;
262 g_platform_curr_abi_version = _hal_api_conf_get_platform_abi_version();
263 for (i = 0; i < info->num_abi_versions; i++) {
264 struct hal_abi_version_match *data
265 = &info->abi_versions[i];
267 if (g_platform_curr_abi_version != data->platform_abi_version)
270 if (data->backend_min_abi_version <= HAL_ABI_VERSION_UNKNOWN ||
271 data->backend_min_abi_version >= HAL_ABI_VERSION_END) {
272 _E("%s: abi_versions[%d].backend_min_abi_version(%d) is invalid\n",
273 info->module_name, i, data->backend_min_abi_version);
274 return TIZEN_ERROR_INVALID_PARAMETER;
277 if (abi_version <= data->platform_abi_version
278 && abi_version >= data->backend_min_abi_version)
279 return TIZEN_ERROR_NONE;
281 _E("%s: \'%s\' doesn't support \'%s\'\n",
283 hal_abi_version_str[g_platform_curr_abi_version],
284 hal_abi_version_str[abi_version]);
285 _E("%s: Must use ABI versions from \'%s\' to \'%s\'\n",
287 hal_abi_version_str[data->backend_min_abi_version],
288 hal_abi_version_str[data->platform_abi_version]);
291 return TIZEN_ERROR_INVALID_PARAMETER;