OSDN Git Service

Merge "Camera3: Add PASSIVE_UNFOCUSED AF state and clean up INACTIVE" into klp-dev
[android-x86/hardware-libhardware.git] / hardware.c
index bff4215..1f831cb 100644 (file)
 #include <utils/Log.h>
 
 /** Base path of the hal modules */
-#define HAL_LIBRARY_PATH "/system/lib/hw"
+#define HAL_LIBRARY_PATH1 "/system/lib/hw"
+#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
 
 /**
- * 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 are
- * "ro.product.board" and "ro.arch" would be:
+ * 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
  */
-#define HAL_DEFAULT_VARIANT "default"
-#define HAL_VARIANT_KEYS_COUNT 3
-static const char *variant_keys[HAL_VARIANT_KEYS_COUNT] = {
+
+static const char *variant_keys[] = {
+    "ro.hardware",  /* This goes first so that it can pick up a different
+                       file on the emulator. */
     "ro.product.board",
-    "ro.arch",
-    HAL_DEFAULT_VARIANT
+    "ro.board.platform",
+    "ro.arch"
 };
 
+static const int HAL_VARIANT_KEYS_COUNT =
+    (sizeof(variant_keys)/sizeof(variant_keys[0]));
+
 /**
- * Load the file defined by the path and if succesfull
+ * Load the file defined by the variant and if successful
  * return the dlopen handle and the hmi.
  * @return 0 = success, !0 = failure.
  */
 static int load(const char *id,
-                const char *path,
-                void **pHandle,
-                const struct hw_module_t **pHmi)
+        const char *path,
+        const struct hw_module_t **pHmi)
 {
     int status;
     void *handle;
-    const struct hw_module_t *hmi;
-
-    LOGV("load: E id=%s path=%s", id, path);
+    struct hw_module_t *hmi;
 
     /*
      * load the symbols resolving undefined symbols before
@@ -73,56 +74,63 @@ static int load(const char *id,
     handle = dlopen(path, RTLD_NOW);
     if (handle == NULL) {
         char const *err_str = dlerror();
-        LOGE("load: module=%s error=%s", path, err_str);
+        ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
         status = -EINVAL;
         goto done;
     }
 
     /* Get the address of the struct hal_module_info. */
     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
-    hmi = (const struct hw_module_t *)dlsym(handle, sym);
+    hmi = (struct hw_module_t *)dlsym(handle, sym);
     if (hmi == NULL) {
-        char const *err_str = dlerror();
-        LOGE("load: couldn't find symbol %s", sym);
+        ALOGE("load: couldn't find symbol %s", sym);
         status = -EINVAL;
         goto done;
     }
 
     /* Check that the id matches */
     if (strcmp(id, hmi->id) != 0) {
-        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
+        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
         status = -EINVAL;
         goto done;
     }
 
+    hmi->dso = handle;
+
     /* success */
     status = 0;
 
-done:
+    done:
     if (status != 0) {
         hmi = NULL;
         if (handle != NULL) {
             dlclose(handle);
             handle = NULL;
         }
+    } else {
+        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
+                id, path, *pHmi, handle);
     }
 
     *pHmi = hmi;
-    *pHandle = handle;
 
-    LOGV("load: X id=%s path=%s hmi=%p pHandle=%p status=%d",
-         id, path, *pHmi, *pHandle, status);
     return status;
 }
 
-int hw_get_module(const char *id, const struct hw_module_t **module) 
+int hw_get_module_by_class(const char *class_id, const char *inst,
+                           const struct hw_module_t **module)
 {
     int status;
+    int i;
     const struct hw_module_t *hmi = NULL;
+    char prop[PATH_MAX];
     char path[PATH_MAX];
-    char variant[PATH_MAX];
-    void *handle = NULL;
-    int i;
+    char name[PATH_MAX];
+
+    if (inst)
+        snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
+    else
+        strlcpy(name, class_id, PATH_MAX);
 
     /*
      * Here we rely on the fact that calling dlopen multiple times on
@@ -130,37 +138,38 @@ int hw_get_module(const char *id, const struct hw_module_t **module)
      * a new copy of the library).
      * We also assume that dlopen() is thread-safe.
      */
-    
-    LOGV("hal_module_info_get: Load module id=%s", id);
 
     /* Loop through the configuration variants looking for a module */
-    status = -EINVAL;
-    for (i = 0; (status != 0) && (i < HAL_VARIANT_KEYS_COUNT); i++) {
-
-        /* Get variant or default */
-        if (strcmp(variant_keys[i], HAL_DEFAULT_VARIANT) == 0) {
-            strncpy(variant, HAL_DEFAULT_VARIANT, sizeof(variant)-1);
-            variant[sizeof(variant)-1] = 0;
-        } else {
-            if (property_get(variant_keys[i], variant, NULL) == 0) {
+    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
+        if (i < HAL_VARIANT_KEYS_COUNT) {
+            if (property_get(variant_keys[i], prop, NULL) == 0) {
                 continue;
             }
-        }
+            snprintf(path, sizeof(path), "%s/%s.%s.so",
+                     HAL_LIBRARY_PATH2, name, prop);
+            if (access(path, R_OK) == 0) break;
 
-        /* Construct the path then try to load */
-        snprintf(path, sizeof(path), "%s/%s.%s.so",
-                HAL_LIBRARY_PATH, id, variant);
-        status = load(id, path, &handle, &hmi);
-    }
-    if (status != 0) {
-        hmi = NULL;
-        if (handle != NULL) {
-            dlclose(handle);
+            snprintf(path, sizeof(path), "%s/%s.%s.so",
+                     HAL_LIBRARY_PATH1, name, prop);
+            if (access(path, R_OK) == 0) break;
+        } else {
+            snprintf(path, sizeof(path), "%s/%s.default.so",
+                     HAL_LIBRARY_PATH1, name);
+            if (access(path, R_OK) == 0) break;
         }
     }
-    
-    *module = hmi;
-    LOGV("hal_module_info_get: X id=%s hmi=%p status=%d", id, hmi, status);
+
+    status = -ENOENT;
+    if (i < HAL_VARIANT_KEYS_COUNT+1) {
+        /* load the module, if this fails, we're doomed, and we should not try
+         * to load a different variant. */
+        status = load(class_id, path, module);
+    }
 
     return status;
 }
+
+int hw_get_module(const char *id, const struct hw_module_t **module)
+{
+    return hw_get_module_by_class(id, NULL, module);
+}