2009-03-04 04:32:14 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* 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 <hardware/hardware.h>
|
|
|
|
|
|
|
|
#include <cutils/properties.h>
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#define LOG_TAG "HAL"
|
|
|
|
#include <utils/Log.h>
|
|
|
|
|
|
|
|
/** Base path of the hal modules */
|
2014-02-16 19:07:15 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
|
|
|
|
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
|
|
|
|
#else
|
2010-09-19 12:41:01 +02:00
|
|
|
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
|
|
|
|
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
|
2014-02-16 19:07:15 +01:00
|
|
|
#endif
|
2009-03-04 04:32:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* There are a set of variant filename for modules. The form of the filename
|
|
|
|
* is "<MODULE_ID>.variant.so" so for the led module the Dream variants
|
|
|
|
* of base "ro.product.board", "ro.board.platform" and "ro.arch" would be:
|
|
|
|
*
|
|
|
|
* led.trout.so
|
|
|
|
* led.msm7k.so
|
|
|
|
* led.ARMV6.so
|
|
|
|
* led.default.so
|
|
|
|
*/
|
|
|
|
|
2009-03-13 21:04:23 +01:00
|
|
|
static const char *variant_keys[] = {
|
|
|
|
"ro.hardware", /* This goes first so that it can pick up a different
|
|
|
|
file on the emulator. */
|
2009-03-04 04:32:14 +01:00
|
|
|
"ro.product.board",
|
|
|
|
"ro.board.platform",
|
2009-09-25 00:11:04 +02:00
|
|
|
"ro.arch"
|
2009-03-04 04:32:14 +01:00
|
|
|
};
|
2009-09-22 07:50:44 +02:00
|
|
|
|
|
|
|
static const int HAL_VARIANT_KEYS_COUNT =
|
|
|
|
(sizeof(variant_keys)/sizeof(variant_keys[0]));
|
2009-03-04 04:32:14 +01:00
|
|
|
|
|
|
|
/**
|
2009-06-12 01:32:05 +02:00
|
|
|
* Load the file defined by the variant and if successful
|
2009-03-04 04:32:14 +01:00
|
|
|
* return the dlopen handle and the hmi.
|
|
|
|
* @return 0 = success, !0 = failure.
|
|
|
|
*/
|
|
|
|
static int load(const char *id,
|
2009-09-22 07:50:44 +02:00
|
|
|
const char *path,
|
|
|
|
const struct hw_module_t **pHmi)
|
2009-03-04 04:32:14 +01:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
void *handle;
|
2009-04-10 23:24:31 +02:00
|
|
|
struct hw_module_t *hmi;
|
2009-03-04 04:32:14 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* load the symbols resolving undefined symbols before
|
|
|
|
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
|
|
|
|
* RTLD_NOW the external symbols will not be global
|
|
|
|
*/
|
|
|
|
handle = dlopen(path, RTLD_NOW);
|
|
|
|
if (handle == NULL) {
|
|
|
|
char const *err_str = dlerror();
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
|
2009-03-04 04:32:14 +01:00
|
|
|
status = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the address of the struct hal_module_info. */
|
|
|
|
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
|
2009-04-10 23:24:31 +02:00
|
|
|
hmi = (struct hw_module_t *)dlsym(handle, sym);
|
2009-03-04 04:32:14 +01:00
|
|
|
if (hmi == NULL) {
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE("load: couldn't find symbol %s", sym);
|
2009-03-04 04:32:14 +01:00
|
|
|
status = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the id matches */
|
|
|
|
if (strcmp(id, hmi->id) != 0) {
|
2012-01-08 11:17:53 +01:00
|
|
|
ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
|
2009-03-04 04:32:14 +01:00
|
|
|
status = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2009-09-22 07:50:44 +02:00
|
|
|
|
2009-04-10 23:24:31 +02:00
|
|
|
hmi->dso = handle;
|
2009-03-04 04:32:14 +01:00
|
|
|
|
|
|
|
/* success */
|
|
|
|
status = 0;
|
|
|
|
|
2009-09-22 07:50:44 +02:00
|
|
|
done:
|
2009-03-04 04:32:14 +01:00
|
|
|
if (status != 0) {
|
|
|
|
hmi = NULL;
|
|
|
|
if (handle != NULL) {
|
|
|
|
dlclose(handle);
|
|
|
|
handle = NULL;
|
|
|
|
}
|
2009-07-16 00:01:10 +02:00
|
|
|
} else {
|
2011-10-20 14:58:15 +02:00
|
|
|
ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
|
2009-09-22 07:50:44 +02:00
|
|
|
id, path, *pHmi, handle);
|
2009-03-04 04:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*pHmi = hmi;
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2013-12-26 22:47:56 +01:00
|
|
|
/*
|
|
|
|
* Check if a HAL with given name and subname exists, if so return 0, otherwise
|
|
|
|
* otherwise return negative. On success path will contain the path to the HAL.
|
|
|
|
*/
|
|
|
|
static int hw_module_exists(char *path, size_t path_len, const char *name,
|
|
|
|
const char *subname)
|
|
|
|
{
|
|
|
|
snprintf(path, path_len, "%s/%s.%s.so",
|
|
|
|
HAL_LIBRARY_PATH2, name, subname);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
snprintf(path, path_len, "%s/%s.%s.so",
|
|
|
|
HAL_LIBRARY_PATH1, name, subname);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2011-04-18 19:55:37 +02:00
|
|
|
int hw_get_module_by_class(const char *class_id, const char *inst,
|
|
|
|
const struct hw_module_t **module)
|
2009-03-04 04:32:14 +01:00
|
|
|
{
|
|
|
|
int i;
|
2009-03-13 21:04:23 +01:00
|
|
|
char prop[PATH_MAX];
|
2009-09-22 07:50:44 +02:00
|
|
|
char path[PATH_MAX];
|
2011-04-18 19:55:37 +02:00
|
|
|
char name[PATH_MAX];
|
2013-12-26 22:47:56 +01:00
|
|
|
char prop_name[PATH_MAX];
|
2011-04-18 19:55:37 +02:00
|
|
|
|
|
|
|
if (inst)
|
|
|
|
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
|
|
|
|
else
|
|
|
|
strlcpy(name, class_id, PATH_MAX);
|
2009-03-04 04:32:14 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we rely on the fact that calling dlopen multiple times on
|
|
|
|
* the same .so will simply increment a refcount (and not load
|
|
|
|
* a new copy of the library).
|
|
|
|
* We also assume that dlopen() is thread-safe.
|
|
|
|
*/
|
|
|
|
|
2013-12-26 22:47:56 +01:00
|
|
|
/* First try a property specific to the class and possibly instance */
|
|
|
|
snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
|
2014-01-14 08:43:23 +01:00
|
|
|
if (property_get(prop_name, prop, NULL) > 0) {
|
2013-12-26 22:47:56 +01:00
|
|
|
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 21:04:23 +01:00
|
|
|
/* Loop through the configuration variants looking for a module */
|
2013-12-26 22:47:56 +01:00
|
|
|
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
|
|
|
|
if (property_get(variant_keys[i], prop, NULL) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
|
|
|
|
goto found;
|
2009-09-22 07:50:44 +02:00
|
|
|
}
|
2009-03-04 04:32:14 +01:00
|
|
|
}
|
2009-03-13 21:04:23 +01:00
|
|
|
|
2013-12-26 22:47:56 +01:00
|
|
|
/* Nothing found, try the default */
|
|
|
|
if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
|
|
|
|
goto found;
|
2009-03-04 04:32:14 +01:00
|
|
|
}
|
2009-09-22 07:50:44 +02:00
|
|
|
|
2013-12-26 22:47:56 +01:00
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
found:
|
|
|
|
/* load the module, if this fails, we're doomed, and we should not try
|
|
|
|
* to load a different variant. */
|
|
|
|
return load(class_id, path, module);
|
2009-03-04 04:32:14 +01:00
|
|
|
}
|
2011-04-18 19:55:37 +02:00
|
|
|
|
|
|
|
int hw_get_module(const char *id, const struct hw_module_t **module)
|
|
|
|
{
|
|
|
|
return hw_get_module_by_class(id, NULL, module);
|
|
|
|
}
|