blob: bff421572270b3bf6b2ad7774772bd8ba9890dcc [file] [log] [blame]
The Android Open Source Project51704be2008-12-17 18:05:50 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <hardware/hardware.h>
18
19#include <cutils/properties.h>
20
21#include <dlfcn.h>
22#include <string.h>
23#include <pthread.h>
24#include <errno.h>
25#include <limits.h>
26
27#define LOG_TAG "HAL"
28#include <utils/Log.h>
29
30/** Base path of the hal modules */
31#define HAL_LIBRARY_PATH "/system/lib/hw"
32
33/**
34 * There are a set of variant filename
35 * for modules. The form of the filename
36 * is "<MODULE_ID>.variant.so" so for the
37 * led module the Dream variants of base are
38 * "ro.product.board" and "ro.arch" would be:
39 *
40 * led.trout.so
41 * led.ARMV6.so
42 * led.default.so
43 */
44#define HAL_DEFAULT_VARIANT "default"
45#define HAL_VARIANT_KEYS_COUNT 3
46static const char *variant_keys[HAL_VARIANT_KEYS_COUNT] = {
47 "ro.product.board",
48 "ro.arch",
49 HAL_DEFAULT_VARIANT
50};
51
52/**
53 * Load the file defined by the path and if succesfull
54 * return the dlopen handle and the hmi.
55 * @return 0 = success, !0 = failure.
56 */
57static int load(const char *id,
58 const char *path,
59 void **pHandle,
60 const struct hw_module_t **pHmi)
61{
62 int status;
63 void *handle;
64 const struct hw_module_t *hmi;
65
66 LOGV("load: E id=%s path=%s", id, path);
67
68 /*
69 * load the symbols resolving undefined symbols before
70 * dlopen returns. Since RTLD_GLOBAL is not or'd in with
71 * RTLD_NOW the external symbols will not be global
72 */
73 handle = dlopen(path, RTLD_NOW);
74 if (handle == NULL) {
75 char const *err_str = dlerror();
76 LOGE("load: module=%s error=%s", path, err_str);
77 status = -EINVAL;
78 goto done;
79 }
80
81 /* Get the address of the struct hal_module_info. */
82 const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
83 hmi = (const struct hw_module_t *)dlsym(handle, sym);
84 if (hmi == NULL) {
85 char const *err_str = dlerror();
86 LOGE("load: couldn't find symbol %s", sym);
87 status = -EINVAL;
88 goto done;
89 }
90
91 /* Check that the id matches */
92 if (strcmp(id, hmi->id) != 0) {
93 LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
94 status = -EINVAL;
95 goto done;
96 }
97
98 /* success */
99 status = 0;
100
101done:
102 if (status != 0) {
103 hmi = NULL;
104 if (handle != NULL) {
105 dlclose(handle);
106 handle = NULL;
107 }
108 }
109
110 *pHmi = hmi;
111 *pHandle = handle;
112
113 LOGV("load: X id=%s path=%s hmi=%p pHandle=%p status=%d",
114 id, path, *pHmi, *pHandle, status);
115 return status;
116}
117
118int hw_get_module(const char *id, const struct hw_module_t **module)
119{
120 int status;
121 const struct hw_module_t *hmi = NULL;
122 char path[PATH_MAX];
123 char variant[PATH_MAX];
124 void *handle = NULL;
125 int i;
126
127 /*
128 * Here we rely on the fact that calling dlopen multiple times on
129 * the same .so will simply increment a refcount (and not load
130 * a new copy of the library).
131 * We also assume that dlopen() is thread-safe.
132 */
133
134 LOGV("hal_module_info_get: Load module id=%s", id);
135
136 /* Loop through the configuration variants looking for a module */
137 status = -EINVAL;
138 for (i = 0; (status != 0) && (i < HAL_VARIANT_KEYS_COUNT); i++) {
139
140 /* Get variant or default */
141 if (strcmp(variant_keys[i], HAL_DEFAULT_VARIANT) == 0) {
142 strncpy(variant, HAL_DEFAULT_VARIANT, sizeof(variant)-1);
143 variant[sizeof(variant)-1] = 0;
144 } else {
145 if (property_get(variant_keys[i], variant, NULL) == 0) {
146 continue;
147 }
148 }
149
150 /* Construct the path then try to load */
151 snprintf(path, sizeof(path), "%s/%s.%s.so",
152 HAL_LIBRARY_PATH, id, variant);
153 status = load(id, path, &handle, &hmi);
154 }
155 if (status != 0) {
156 hmi = NULL;
157 if (handle != NULL) {
158 dlclose(handle);
159 }
160 }
161
162 *module = hmi;
163 LOGV("hal_module_info_get: X id=%s hmi=%p status=%d", id, hmi, status);
164
165 return status;
166}