Add loading HAL module before runnig API 29/252629/3
authorYunmi Ha <yunmi.ha@samsung.com>
Mon, 1 Feb 2021 02:10:06 +0000 (11:10 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Mon, 1 Feb 2021 06:01:29 +0000 (15:01 +0900)
Change-Id: I5b2b45fbb0cb5e722331dc70f796ee5f40bcea73
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
src/battery.c
src/bezel.c
src/board.c
src/display.c
src/external_connection.c
src/haptic.c
src/ir.c
src/led.c
src/thermal.c
src/touchscreen.c
src/usb_gadget.c

index 68ba29f..01bad7d 100644 (file)
  * limitations under the License.
  */
 
-
 #include <hal/hal-common.h>
 
 #include "hal-battery-interface.h"
 #include "common.h"
 
-static hal_backend_battery_funcs *g_battery_funcs = NULL;
+static hal_backend_battery_funcs *hal_battery_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_battery_get_backend(void)
 {
        int ret;
 
-       if (g_battery_funcs)
+       if (hal_battery_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BATTERY, (void **)&g_battery_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BATTERY, (void **)&hal_battery_funcs);
        if (ret < 0) {
                 _E("Failed to get battery backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_battery_put_backend(void)
 {
-       if (!g_battery_funcs)
+       if (!hal_battery_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_BATTERY, (void *)g_battery_funcs);
-       g_battery_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_BATTERY, (void *)hal_battery_funcs);
+       hal_battery_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
-
 int hal_device_battery_register_changed_event(BatteryUpdated updated_cb, void *data)
 {
-       if (!g_battery_funcs ||
-           !g_battery_funcs->register_changed_event)
+       int ret;
+
+       if (!hal_battery_funcs && !hal_initialized) {
+               if ((ret = hal_device_battery_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_battery_funcs ||
+           !hal_battery_funcs->register_changed_event)
                return -ENODEV;
 
-       return g_battery_funcs->register_changed_event(updated_cb, data);
+       return hal_battery_funcs->register_changed_event(updated_cb, data);
 }
 
 int hal_device_battery_unregister_changed_event(BatteryUpdated updated_cb)
 {
-       if (!g_battery_funcs ||
-           !g_battery_funcs->unregister_changed_event)
+       int ret;
+
+       if (!hal_battery_funcs && !hal_initialized) {
+               if ((ret = hal_device_battery_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_battery_funcs ||
+           !hal_battery_funcs->unregister_changed_event)
                return -ENODEV;
 
-       g_battery_funcs->unregister_changed_event(updated_cb);
+       hal_battery_funcs->unregister_changed_event(updated_cb);
        return 0;
 }
 
 int hal_device_battery_get_current_state(BatteryUpdated updated_cb, void *data)
 {
-       if (!g_battery_funcs ||
-           !g_battery_funcs->get_current_state)
+       int ret;
+
+       if (!hal_battery_funcs && !hal_initialized) {
+               if ((ret = hal_device_battery_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_battery_funcs ||
+           !hal_battery_funcs->get_current_state)
                return -ENODEV;
 
-       return g_battery_funcs->get_current_state(updated_cb, data);
+       return hal_battery_funcs->get_current_state(updated_cb, data);
 }
index 6b1e9a3..f650b8d 100644 (file)
 #include "hal-bezel-interface.h"
 #include "common.h"
 
-static hal_backend_bezel_funcs *g_bezel_funcs = NULL;
+static hal_backend_bezel_funcs *hal_bezel_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_bezel_get_backend(void)
 {
        int ret;
 
-       if (g_bezel_funcs)
+       if (hal_bezel_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BEZEL, (void **)&g_bezel_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BEZEL, (void **)&hal_bezel_funcs);
        if (ret < 0) {
                 _E("Failed to get bezel backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_bezel_put_backend(void)
 {
-       if (!g_bezel_funcs)
+       if (!hal_bezel_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_BEZEL, (void *)g_bezel_funcs);
-       g_bezel_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_BEZEL, (void *)hal_bezel_funcs);
+       hal_bezel_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
-
 int hal_device_bezel_get_state(enum bezel_state *state)
 {
-       if (!g_bezel_funcs ||
-           !g_bezel_funcs->get_state)
+       int ret;
+
+       if (!hal_bezel_funcs && !hal_initialized) {
+               if ((ret = hal_device_bezel_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_bezel_funcs ||
+           !hal_bezel_funcs->get_state)
                return -ENODEV;
 
-       return g_bezel_funcs->get_state(state);
+       return hal_bezel_funcs->get_state(state);
 }
 
 int hal_device_bezel_set_state(enum bezel_state state)
 {
-       if (!g_bezel_funcs ||
-           !g_bezel_funcs->set_state)
+       int ret;
+
+       if (!hal_bezel_funcs && !hal_initialized) {
+               if ((ret = hal_device_bezel_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_bezel_funcs ||
+           !hal_bezel_funcs->set_state)
                return -ENODEV;
 
-       return g_bezel_funcs->set_state(state);
+       return hal_bezel_funcs->set_state(state);
 }
 
 int hal_device_bezel_get_sw_state(enum bezel_state *state)
 {
-       if (!g_bezel_funcs ||
-           !g_bezel_funcs->get_sw_state)
+       int ret;
+
+       if (!hal_bezel_funcs && !hal_initialized) {
+               if ((ret = hal_device_bezel_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_bezel_funcs ||
+           !hal_bezel_funcs->get_sw_state)
                return -ENODEV;
 
-       return g_bezel_funcs->get_sw_state(state);
+       return hal_bezel_funcs->get_sw_state(state);
 }
 
 int hal_device_bezel_set_sw_state(enum bezel_state state)
 {
-       if (!g_bezel_funcs ||
-           !g_bezel_funcs->set_sw_state)
+       int ret;
+
+       if (!hal_bezel_funcs && !hal_initialized) {
+               if ((ret = hal_device_bezel_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_bezel_funcs ||
+           !hal_bezel_funcs->set_sw_state)
                return -ENODEV;
 
-       return g_bezel_funcs->set_sw_state(state);
+       return hal_bezel_funcs->set_sw_state(state);
 }
 
 int hal_device_bezel_get_vib_state(enum bezel_vib_state *state)
 {
-       if (!g_bezel_funcs ||
-           !g_bezel_funcs->get_vib_state)
+       int ret;
+
+       if (!hal_bezel_funcs && !hal_initialized) {
+               if ((ret = hal_device_bezel_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_bezel_funcs ||
+           !hal_bezel_funcs->get_vib_state)
                return -ENODEV;
 
-       return g_bezel_funcs->get_vib_state(state);
+       return hal_bezel_funcs->get_vib_state(state);
 }
 
 int hal_device_bezel_set_vib_state(enum bezel_vib_state state)
 {
-       if (!g_bezel_funcs ||
-           !g_bezel_funcs->set_vib_state)
+       int ret;
+
+       if (!hal_bezel_funcs && !hal_initialized) {
+               if ((ret = hal_device_bezel_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_bezel_funcs ||
+           !hal_bezel_funcs->set_vib_state)
                return -ENODEV;
 
-       return g_bezel_funcs->set_vib_state(state);
+       return hal_bezel_funcs->set_vib_state(state);
 }
 
index 3fde9fa..a9321bb 100644 (file)
 #include "hal-board-interface.h"
 #include "common.h"
 
-static hal_backend_board_funcs *g_board_funcs = NULL;
+static hal_backend_board_funcs *hal_board_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_board_get_backend(void)
 {
        int ret;
 
-       if (g_board_funcs)
+       if (hal_board_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BOARD, (void **)&g_board_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_BOARD, (void **)&hal_board_funcs);
        if (ret < 0) {
                 _E("Failed to get board backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_board_put_backend(void)
 {
-       if (!g_board_funcs)
+       if (!hal_board_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_BOARD, (void *)g_board_funcs);
-       g_board_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_BOARD, (void *)hal_board_funcs);
+       hal_board_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_board_get_device_serial(char **serial)
 {
-       if (!g_board_funcs ||
-           !g_board_funcs->get_device_serial)
+       int ret ;
+
+       if (!hal_board_funcs && !hal_initialized) {
+               if ((ret = hal_device_board_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_board_funcs ||
+           !hal_board_funcs->get_device_serial)
                return -ENODEV;
 
-       return g_board_funcs->get_device_serial(serial);
+       return hal_board_funcs->get_device_serial(serial);
 }
 
 int hal_device_board_get_device_revision(int *revision)
 {
-       if (!g_board_funcs ||
-           !g_board_funcs->get_device_revision)
+       int ret ;
+
+       if (!hal_board_funcs && !hal_initialized) {
+               if ((ret = hal_device_board_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_board_funcs ||
+           !hal_board_funcs->get_device_revision)
                return -ENODEV;
 
-       return g_board_funcs->get_device_revision(revision);
+       return hal_board_funcs->get_device_revision(revision);
 }
 
index 75c465f..52ba9f9 100644 (file)
  * limitations under the License.
  */
 
-
 #include <hal/hal-common.h>
 
 #include "hal-display-interface.h"
 #include "common.h"
 
-static hal_backend_display_funcs *g_display_funcs = NULL;
+static hal_backend_display_funcs *hal_display_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_display_get_backend(void)
 {
        int ret;
 
-       if (g_display_funcs)
+       if (hal_display_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_DISPLAY, (void **)&g_display_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_DISPLAY, (void **)&hal_display_funcs);
        if (ret < 0) {
                 _E("Failed to get display backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_display_put_backend(void)
 {
-       if (!g_display_funcs)
+       if (!hal_display_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_DISPLAY, (void *)g_display_funcs);
-       g_display_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_DISPLAY, (void *)hal_display_funcs);
+       hal_display_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_display_get_max_brightness(int *brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_max_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_max_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_max_brightness(brightness);
+       return hal_display_funcs->get_max_brightness(brightness);
 }
 
 int hal_device_display_get_brightness(int *brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_brightness(brightness);
+       return hal_display_funcs->get_brightness(brightness);
 }
 
 int hal_device_display_set_brightness(int brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_brightness)
                return -ENODEV;
 
-       return g_display_funcs->set_brightness(brightness);
+       return hal_display_funcs->set_brightness(brightness);
 }
 
 int hal_device_display_set_multi_brightness(int brightness, int step, int delay)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_multi_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_multi_brightness)
                return -ENODEV;
 
-       return g_display_funcs->set_multi_brightness(brightness, step, delay);
+       return hal_display_funcs->set_multi_brightness(brightness, step, delay);
 }
 
 int hal_device_display_get_auto_brightness(float lmax, float lmin, float light, int *brightness)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_auto_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_auto_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_auto_brightness(lmax, lmin, light, brightness);
+       return hal_display_funcs->get_auto_brightness(lmax, lmin, light, brightness);
 }
 
 int hal_device_display_get_state(enum display_state *state)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_state)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_state)
                return -ENODEV;
 
-       return g_display_funcs->get_state(state);
+       return hal_display_funcs->get_state(state);
 }
 
 int hal_device_display_set_state(enum display_state state)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_state)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_state)
                return -ENODEV;
 
-       return g_display_funcs->set_state(state);
+       return hal_display_funcs->set_state(state);
 }
 
 int hal_device_display_get_image_effect(enum display_image_effect *effect)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_image_effect)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_image_effect)
                return -ENODEV;
 
-       return g_display_funcs->get_image_effect(effect);
+       return hal_display_funcs->get_image_effect(effect);
 }
 
 int hal_device_display_set_image_effect(enum display_image_effect effect)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_image_effect)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_image_effect)
                return -ENODEV;
 
-       return g_display_funcs->set_image_effect(effect);
+       return hal_display_funcs->set_image_effect(effect);
 }
 
 int hal_device_display_get_panel_mode(enum display_panel_mode *mode)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_panel_mode)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_panel_mode)
                return -ENODEV;
 
-       return g_display_funcs->get_panel_mode(mode);
+       return hal_display_funcs->get_panel_mode(mode);
 }
 
 int hal_device_display_set_panel_mode(enum display_panel_mode mode)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_panel_mode)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_panel_mode)
                return -ENODEV;
 
-       return g_display_funcs->set_panel_mode(mode);
+       return hal_display_funcs->set_panel_mode(mode);
 }
 
 int hal_device_display_get_aod_mode(enum display_aod_mode *mode)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_aod_mode)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_aod_mode)
                return -ENODEV;
 
-       return g_display_funcs->get_aod_mode(mode);
+       return hal_display_funcs->get_aod_mode(mode);
 }
 
 int hal_device_display_get_aod_brightness(int *max, int *normal, int *min, int *charging)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_aod_brightness)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_aod_brightness)
                return -ENODEV;
 
-       return g_display_funcs->get_aod_brightness(max, normal, min, charging);
+       return hal_display_funcs->get_aod_brightness(max, normal, min, charging);
 }
 
 int hal_device_display_get_max_frame_rate(int *rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_max_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_max_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->get_max_frame_rate(rate);
+       return hal_display_funcs->get_max_frame_rate(rate);
 }
 
 int hal_device_display_get_min_frame_rate(int *rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_min_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_min_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->get_min_frame_rate(rate);
+       return hal_display_funcs->get_min_frame_rate(rate);
 }
 
 int hal_device_display_get_frame_rate(int *rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->get_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->get_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->get_frame_rate(rate);
+       return hal_display_funcs->get_frame_rate(rate);
 }
 
 int hal_device_display_set_frame_rate(int rate)
 {
-       if (!g_display_funcs ||
-           !g_display_funcs->set_frame_rate)
+       int ret;
+
+       if (!hal_display_funcs && !hal_initialized) {
+               if ((ret = hal_device_display_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_display_funcs ||
+           !hal_display_funcs->set_frame_rate)
                return -ENODEV;
 
-       return g_display_funcs->set_frame_rate(rate);
+       return hal_display_funcs->set_frame_rate(rate);
 }
index dfe7a7a..7ecc2d8 100644 (file)
 #include "hal-external_connection-interface.h"
 #include "common.h"
 
-static hal_backend_external_connection_funcs *g_external_connection_funcs = NULL;
+static hal_backend_external_connection_funcs *hal_external_connection_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_external_connection_get_backend(void)
 {
        int ret;
 
-       if (g_external_connection_funcs)
+       if (hal_external_connection_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_EXTERNAL_CONNECTION, (void **)&g_external_connection_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_EXTERNAL_CONNECTION, (void **)&hal_external_connection_funcs);
        if (ret < 0) {
                 _E("Failed to get external connection backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_external_connection_put_backend(void)
 {
-       if (!g_external_connection_funcs)
+       if (!hal_external_connection_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_EXTERNAL_CONNECTION, (void *)g_external_connection_funcs);
-       g_external_connection_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_EXTERNAL_CONNECTION, (void *)hal_external_connection_funcs);
+       hal_external_connection_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
@@ -52,29 +61,50 @@ int hal_device_external_connection_put_backend(void)
 
 int hal_device_external_connection_register_changed_event(ConnectionUpdated updated_cb, void *data)
 {
-       if (!g_external_connection_funcs ||
-           !g_external_connection_funcs->register_changed_event)
+       int ret;
+
+       if (!hal_external_connection_funcs && !hal_initialized) {
+               if ((ret = hal_device_external_connection_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_external_connection_funcs ||
+           !hal_external_connection_funcs->register_changed_event)
                return -ENODEV;
 
-       return g_external_connection_funcs->register_changed_event(updated_cb, data);
+       return hal_external_connection_funcs->register_changed_event(updated_cb, data);
 }
 
 int hal_device_external_connection_unregister_changed_event(ConnectionUpdated updated_cb)
 {
-       if (!g_external_connection_funcs ||
-           !g_external_connection_funcs->unregister_changed_event)
+       int ret;
+
+       if (!hal_external_connection_funcs && !hal_initialized) {
+               if ((ret = hal_device_external_connection_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_external_connection_funcs ||
+           !hal_external_connection_funcs->unregister_changed_event)
                return -ENODEV;
 
-       g_external_connection_funcs->unregister_changed_event(updated_cb);
+       hal_external_connection_funcs->unregister_changed_event(updated_cb);
        return 0;
 }
 
 int hal_device_external_connection_get_current_state(ConnectionUpdated updated_cb, void *data)
 {
-       if (!g_external_connection_funcs ||
-           !g_external_connection_funcs->get_current_state)
+       int ret;
+
+       if (!hal_external_connection_funcs && !hal_initialized) {
+               if ((ret = hal_device_external_connection_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_external_connection_funcs ||
+           !hal_external_connection_funcs->get_current_state)
                return -ENODEV;
 
-       return g_external_connection_funcs->get_current_state(updated_cb, data);
+       return hal_external_connection_funcs->get_current_state(updated_cb, data);
 }
 
index cb8d9ae..2d255bf 100644 (file)
 
 #include "common.h"
 
-static hal_backend_haptic_funcs *g_haptic_funcs = NULL;
+static hal_backend_haptic_funcs *hal_haptic_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_haptic_get_backend(void)
 {
        int ret;
 
-       if (g_haptic_funcs)
+       if (hal_haptic_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_HAPTIC, (void **)&g_haptic_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_HAPTIC, (void **)&hal_haptic_funcs);
        if (ret < 0) {
                 _E("Failed to get haptic backend");
+               hal_initialized = -1;
                return -ENODEV;
        }
 
-       if (!g_haptic_funcs)
-               return -ENODEV;
-
-       if (!g_haptic_funcs->is_valid || !(g_haptic_funcs->is_valid())) {
+       if (!hal_haptic_funcs->is_valid || !(hal_haptic_funcs->is_valid())) {
                hal_device_haptic_put_backend();
+               hal_initialized = -1;
                return -ENODEV;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_haptic_put_backend(void)
 {
-       if (!g_haptic_funcs)
+       if (!hal_haptic_funcs)
                return -ENODEV;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_HAPTIC, (void *)g_haptic_funcs);
-       g_haptic_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_HAPTIC, (void *)hal_haptic_funcs);
+       hal_haptic_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_haptic_check_backend(void)
 {
-       if (!g_haptic_funcs)
+       if (!hal_haptic_funcs)
                return -ENODEV;
 
        return 0;
@@ -69,55 +76,97 @@ int hal_device_haptic_check_backend(void)
 
 int hal_device_haptic_get_device_count(int *count)
 {
-       if (!g_haptic_funcs || !g_haptic_funcs->get_device_count)
+       int ret;
+
+       if (!hal_haptic_funcs && !hal_initialized) {
+               if ((ret = hal_device_haptic_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_haptic_funcs || !hal_haptic_funcs->get_device_count)
                return -ENODEV;
 
        if(!count)
                return -EINVAL;
 
-       return g_haptic_funcs->get_device_count(count);
+       return hal_haptic_funcs->get_device_count(count);
 }
 
 int hal_device_haptic_open_device(int *handle)
 {
-       if (!g_haptic_funcs || !g_haptic_funcs->open_device)
+       int ret;
+
+       if (!hal_haptic_funcs && !hal_initialized) {
+               if ((ret = hal_device_haptic_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_haptic_funcs || !hal_haptic_funcs->open_device)
                return -ENODEV;
 
        if (!handle)
                return -EINVAL;
 
-       return g_haptic_funcs->open_device(handle);
+       return hal_haptic_funcs->open_device(handle);
 }
 
 int hal_device_haptic_close_device(int handle)
 {
-       if (!g_haptic_funcs || !g_haptic_funcs->close_device)
+       int ret;
+
+       if (!hal_haptic_funcs && !hal_initialized) {
+               if ((ret = hal_device_haptic_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_haptic_funcs || !hal_haptic_funcs->close_device)
                return -ENODEV;
 
-       return g_haptic_funcs->close_device(handle);
+       return hal_haptic_funcs->close_device(handle);
 }
 
 bool hal_device_haptic_is_valid(void)
 {
-       if (!g_haptic_funcs || !g_haptic_funcs->is_valid)
+       int ret;
+
+       if (!hal_haptic_funcs && !hal_initialized) {
+               if ((ret = hal_device_haptic_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_haptic_funcs || !hal_haptic_funcs->is_valid)
                return -ENODEV;
 
-       return g_haptic_funcs->is_valid();
+       return hal_haptic_funcs->is_valid();
 }
 
 int hal_device_haptic_vibrate(int handle, int duration, int frequency, int overdrive, int level, int intensity, int priority)
 {
-       if (!g_haptic_funcs || !g_haptic_funcs->vibrate_monotone)
+       int ret;
+
+       if (!hal_haptic_funcs && !hal_initialized) {
+               if ((ret = hal_device_haptic_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_haptic_funcs || !hal_haptic_funcs->vibrate_monotone)
                return -ENODEV;
 
-       return g_haptic_funcs->vibrate_monotone(handle, duration, frequency, overdrive,
+       return hal_haptic_funcs->vibrate_monotone(handle, duration, frequency, overdrive,
                                                                                        level, intensity, priority);
 }
 
 int hal_device_haptic_stop_device(int handle)
 {
-       if (!g_haptic_funcs || !g_haptic_funcs->stop_device)
+       int ret;
+
+       if (!hal_haptic_funcs && !hal_initialized) {
+               if ((ret = hal_device_haptic_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_haptic_funcs || !hal_haptic_funcs->stop_device)
                return -ENODEV;
 
-       return g_haptic_funcs->stop_device(handle);
+       return hal_haptic_funcs->stop_device(handle);
 }
index cf8de7d..3d64039 100644 (file)
--- a/src/ir.c
+++ b/src/ir.c
 #include "hal-ir-interface.h"
 #include "common.h"
 
-static hal_backend_ir_funcs *g_ir_funcs = NULL;
+static hal_backend_ir_funcs *hal_ir_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_ir_get_backend(void)
 {
        int ret;
 
-       if (g_ir_funcs)
+       if (hal_ir_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_IR, (void **)&g_ir_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_IR, (void **)&hal_ir_funcs);
        if (ret < 0) {
                 _E("Failed to get ir backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_ir_put_backend(void)
 {
-       if (!g_ir_funcs)
+       if (!hal_ir_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_IR, (void *)g_ir_funcs);
-       g_ir_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_IR, (void *)hal_ir_funcs);
+       hal_ir_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
@@ -52,18 +61,32 @@ int hal_device_ir_put_backend(void)
 
 int hal_device_ir_is_available(bool *available)
 {
-       if (!g_ir_funcs ||
-           !g_ir_funcs->is_available)
+       int ret;
+
+       if (!hal_ir_funcs && !hal_initialized) {
+               if ((ret = hal_device_ir_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_ir_funcs ||
+           !hal_ir_funcs->is_available)
                return -ENODEV;
 
-       return g_ir_funcs->is_available(available);
+       return hal_ir_funcs->is_available(available);
 }
 
 int hal_device_ir_transmit(int *frequency_pattern, int size)
 {
-       if (!g_ir_funcs ||
-           !g_ir_funcs->transmit)
+       int ret;
+
+       if (!hal_ir_funcs && !hal_initialized) {
+               if ((ret = hal_device_ir_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_ir_funcs ||
+           !hal_ir_funcs->transmit)
                return -ENODEV;
 
-       return g_ir_funcs->transmit(frequency_pattern, size);
+       return hal_ir_funcs->transmit(frequency_pattern, size);
 }
index 530beb0..7f04937 100644 (file)
--- a/src/led.c
+++ b/src/led.c
  * limitations under the License.
  */
 
-
-#include <stdbool.h>
 #include <hal/hal-common.h>
 
 #include "common.h"
 #include "hal-led-interface.h"
 
-static hal_backend_led_funcs *g_led_funcs = NULL;
+static hal_backend_led_funcs *hal_led_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_led_get_backend(void)
 {
        int ret;
 
-       if (g_led_funcs)
+       if (hal_led_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_LED, (void **)&g_led_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_LED, (void **)&hal_led_funcs);
        if (ret < 0) {
                 _E("Failed to get led backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_led_put_backend(void)
 {
-       if (!g_led_funcs)
+       if (!hal_led_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_LED, (void *)g_led_funcs);
-       g_led_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_LED, (void *)hal_led_funcs);
+       hal_led_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_led_set_state(enum led_device_type type, struct led_state *state)
 {
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
        switch (type) {
        case CAMERA_FRONT:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->camera_front) ||
-                       !(g_led_funcs->camera_front->set_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->camera_front) ||
+                       !(hal_led_funcs->camera_front->set_state))
                        return -ENODEV;
-               return g_led_funcs->camera_front->set_state(type, state);
+               return hal_led_funcs->camera_front->set_state(type, state);
 
        case CAMERA_BACK:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->camera_back) ||
-                       !(g_led_funcs->camera_back->set_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->camera_back) ||
+                       !(hal_led_funcs->camera_back->set_state))
                        return -ENODEV;
-               return g_led_funcs->camera_back->set_state(type, state);
+               return hal_led_funcs->camera_back->set_state(type, state);
 
        case NOTIFICATION:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->notification) ||
-                       !(g_led_funcs->notification->set_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->notification) ||
+                       !(hal_led_funcs->notification->set_state))
                        return -ENODEV;
-               return g_led_funcs->notification->set_state(type, state);
+               return hal_led_funcs->notification->set_state(type, state);
 
        case TOUCH_KEY:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->touch_key) ||
-                       !(g_led_funcs->touch_key->set_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->touch_key) ||
+                       !(hal_led_funcs->touch_key->set_state))
                        return -ENODEV;
-               return g_led_funcs->touch_key->set_state(type, state);
+               return hal_led_funcs->touch_key->set_state(type, state);
 
        default:
                _E("Invalid led type: %d", type);
@@ -89,34 +103,41 @@ int hal_device_led_set_state(enum led_device_type type, struct led_state *state)
 
 int hal_device_led_get_state(enum led_device_type type, struct led_state **state)
 {
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
        switch (type) {
        case CAMERA_FRONT:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->camera_front) ||
-                       !(g_led_funcs->camera_front->get_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->camera_front) ||
+                       !(hal_led_funcs->camera_front->get_state))
                        return -ENODEV;
-               return g_led_funcs->camera_front->get_state(type, state);
+               return hal_led_funcs->camera_front->get_state(type, state);
 
        case CAMERA_BACK:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->camera_back) ||
-                       !(g_led_funcs->camera_back->get_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->camera_back) ||
+                       !(hal_led_funcs->camera_back->get_state))
                        return -ENODEV;
-               return g_led_funcs->camera_back->get_state(type, state);
+               return hal_led_funcs->camera_back->get_state(type, state);
 
        case NOTIFICATION:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->notification) ||
-                       !(g_led_funcs->notification->get_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->notification) ||
+                       !(hal_led_funcs->notification->get_state))
                        return -ENODEV;
-               return g_led_funcs->notification->get_state(type, state);
+               return hal_led_funcs->notification->get_state(type, state);
 
        case TOUCH_KEY:
-               if (!g_led_funcs ||
-                       !(g_led_funcs->touch_key) ||
-                       !(g_led_funcs->touch_key->get_state))
+               if (!hal_led_funcs ||
+                       !(hal_led_funcs->touch_key) ||
+                       !(hal_led_funcs->touch_key->get_state))
                        return -ENODEV;
-               return g_led_funcs->touch_key->get_state(type, state);
+               return hal_led_funcs->touch_key->get_state(type, state);
 
        default:
                _E("Invalid led type: %d", type);
@@ -126,53 +147,88 @@ int hal_device_led_get_state(enum led_device_type type, struct led_state **state
 
 int hal_device_led_get_number(void)
 {
-       if (!g_led_funcs ||
-               !(g_led_funcs->notification) ||
-               !(g_led_funcs->notification->get_number))
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_led_funcs ||
+               !(hal_led_funcs->notification) ||
+               !(hal_led_funcs->notification->get_number))
                return -ENODEV;
 
-       return g_led_funcs->notification->get_number();
+       return hal_led_funcs->notification->get_number();
 }
 
 int hal_device_led_set_number(int number)
 {
-       if (!g_led_funcs ||
-               !(g_led_funcs->notification) ||
-               !(g_led_funcs->notification->set_num))
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_led_funcs ||
+               !(hal_led_funcs->notification) ||
+               !(hal_led_funcs->notification->set_num))
                return -ENODEV;
 
-       g_led_funcs->notification->set_num(number);
+       hal_led_funcs->notification->set_num(number);
 
        return 0;
 }
 
 int hal_device_led_get_max_num(void)
 {
-       if (!g_led_funcs ||
-               !(g_led_funcs->notification) ||
-               !(g_led_funcs->notification->get_max_num))
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_led_funcs ||
+               !(hal_led_funcs->notification) ||
+               !(hal_led_funcs->notification->get_max_num))
                return -ENODEV;
 
-       return g_led_funcs->notification->get_max_num();
+       return hal_led_funcs->notification->get_max_num();
 }
 
 int hal_device_keyled_set_state(struct keyled_state *state)
 {
-       if (!g_led_funcs ||
-               !(g_led_funcs->touch_key) ||
-               !(g_led_funcs->touch_key->keyled_set_state))
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_led_funcs ||
+               !(hal_led_funcs->touch_key) ||
+               !(hal_led_funcs->touch_key->keyled_set_state))
                return -ENODEV;
 
-       return g_led_funcs->touch_key->keyled_set_state(state);
+       return hal_led_funcs->touch_key->keyled_set_state(state);
 }
 
 int hal_device_keyled_get_state(int *keycode, int *brightness)
 {
-       if (!g_led_funcs ||
-               !(g_led_funcs->touch_key) ||
-               !(g_led_funcs->touch_key->keyled_get_state))
+       int ret;
+
+       if (!hal_led_funcs && !hal_initialized) {
+               if ((ret = hal_device_led_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_led_funcs ||
+               !(hal_led_funcs->touch_key) ||
+               !(hal_led_funcs->touch_key->keyled_get_state))
                return -ENODEV;
 
-       return g_led_funcs->touch_key->keyled_get_state(keycode, brightness);
+       return hal_led_funcs->touch_key->keyled_get_state(keycode, brightness);
 }
 
index ae0ac70..70d13ed 100644 (file)
  * limitations under the License.
  */
 
-
 #include <hal/hal-common.h>
 
 #include "common.h"
 #include "hal-thermal-interface.h"
 
-static hal_backend_thermal_funcs *g_thermal_funcs = NULL;
+static hal_backend_thermal_funcs *hal_thermal_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_thermal_get_backend(void)
 {
        int ret;
 
-       if (g_thermal_funcs)
+       if (hal_thermal_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_THERMAL, (void **)&g_thermal_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_THERMAL, (void **)&hal_thermal_funcs);
        if (ret < 0) {
                 _E("Failed to get thermal backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_thermal_put_backend(void)
 {
-       if (!g_thermal_funcs)
+       if (!hal_thermal_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_THERMAL, (void *)g_thermal_funcs);
-       g_thermal_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_THERMAL, (void *)hal_thermal_funcs);
+       hal_thermal_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_thermal_get_info(device_thermal_e type, struct thermal_info *info)
 {
-       if (!g_thermal_funcs ||
-           !g_thermal_funcs->get_info)
+       int ret;
+
+       if (!hal_thermal_funcs && !hal_initialized) {
+               if ((ret = hal_device_thermal_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_thermal_funcs ||
+           !hal_thermal_funcs->get_info)
                return -ENODEV;
 
-       return g_thermal_funcs->get_info(type, info);
+       return hal_thermal_funcs->get_info(type, info);
 }
 
 int hal_device_thermal_register_changed_event(ThermalUpdated updated_cb, void *data)
 {
-       if (!g_thermal_funcs ||
-           !g_thermal_funcs->register_changed_event)
+       int ret;
+
+       if (!hal_thermal_funcs && !hal_initialized) {
+               if ((ret = hal_device_thermal_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_thermal_funcs ||
+           !hal_thermal_funcs->register_changed_event)
                return -ENODEV;
 
-       return g_thermal_funcs->register_changed_event(updated_cb, data);
+       return hal_thermal_funcs->register_changed_event(updated_cb, data);
 }
 
 int hal_device_thermal_unregister_changed_event(ThermalUpdated updated_cb)
 {
-       if (!g_thermal_funcs ||
-           !g_thermal_funcs->unregister_changed_event)
+       int ret;
+
+       if (!hal_thermal_funcs && !hal_initialized) {
+               if ((ret = hal_device_thermal_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_thermal_funcs ||
+           !hal_thermal_funcs->unregister_changed_event)
                return -ENODEV;
 
-       return g_thermal_funcs->unregister_changed_event(updated_cb);
+       return hal_thermal_funcs->unregister_changed_event(updated_cb);
 }
 
index 35540a3..d4e0560 100644 (file)
 #include "hal-touchscreen-interface.h"
 #include "common.h"
 
-static hal_backend_touchscreen_funcs *g_touchscreen_funcs = NULL;
+static hal_backend_touchscreen_funcs *hal_touchscreen_funcs = NULL;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 int hal_device_touchscreen_get_backend(void)
 {
        int ret;
 
-       if (g_touchscreen_funcs)
+       if (hal_touchscreen_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_TOUCHSCREEN, (void **)&g_touchscreen_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_TOUCHSCREEN, (void **)&hal_touchscreen_funcs);
        if (ret < 0) {
                 _E("Failed to get touchscreen backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_touchscreen_put_backend(void)
 {
-       if (!g_touchscreen_funcs)
+       if (!hal_touchscreen_funcs)
                return 0;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_TOUCHSCREEN, (void *)g_touchscreen_funcs);
-       g_touchscreen_funcs = NULL;
+       hal_common_put_backend(HAL_MODULE_DEVICE_TOUCHSCREEN, (void *)hal_touchscreen_funcs);
+       hal_touchscreen_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
@@ -53,54 +62,96 @@ int hal_device_touchscreen_put_backend(void)
 
 int hal_device_touchscreen_get_state(enum touchscreen_state *state)
 {
-       if (!g_touchscreen_funcs ||
-           !g_touchscreen_funcs->get_state)
+       int ret;
+
+       if (!hal_touchscreen_funcs && !hal_initialized) {
+               if ((ret = hal_device_touchscreen_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_touchscreen_funcs ||
+           !hal_touchscreen_funcs->get_state)
                return -ENODEV;
 
-       return g_touchscreen_funcs->get_state(state);
+       return hal_touchscreen_funcs->get_state(state);
 }
 
 int hal_device_touchscreen_set_state(enum touchscreen_state state)
 {
-       if (!g_touchscreen_funcs ||
-           !g_touchscreen_funcs->set_state)
+       int ret;
+
+       if (!hal_touchscreen_funcs && !hal_initialized) {
+               if ((ret = hal_device_touchscreen_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_touchscreen_funcs ||
+           !hal_touchscreen_funcs->set_state)
                return -ENODEV;
 
-       return g_touchscreen_funcs->set_state(state);
+       return hal_touchscreen_funcs->set_state(state);
 }
 
 int hal_device_touchscreen_get_powersaving(int *state)
 {
-       if (!g_touchscreen_funcs ||
-           !g_touchscreen_funcs->get_powersaving)
+       int ret;
+
+       if (!hal_touchscreen_funcs && !hal_initialized) {
+               if ((ret = hal_device_touchscreen_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_touchscreen_funcs ||
+           !hal_touchscreen_funcs->get_powersaving)
                return -ENODEV;
 
-       return g_touchscreen_funcs->get_powersaving(state);
+       return hal_touchscreen_funcs->get_powersaving(state);
 }
 
 int hal_device_touchscreen_set_powersaving(int state)
 {
-       if (!g_touchscreen_funcs ||
-           !g_touchscreen_funcs->set_powersaving)
+       int ret;
+
+       if (!hal_touchscreen_funcs && !hal_initialized) {
+               if ((ret = hal_device_touchscreen_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_touchscreen_funcs ||
+           !hal_touchscreen_funcs->set_powersaving)
                return -ENODEV;
 
-       return g_touchscreen_funcs->set_powersaving(state);
+       return hal_touchscreen_funcs->set_powersaving(state);
 }
 
 int hal_device_touchscreen_glove_mode_get_state(int *state)
 {
-       if (!g_touchscreen_funcs ||
-           !g_touchscreen_funcs->glove_mode_get_state)
+       int ret;
+
+       if (!hal_touchscreen_funcs && !hal_initialized) {
+               if ((ret = hal_device_touchscreen_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_touchscreen_funcs ||
+           !hal_touchscreen_funcs->glove_mode_get_state)
                return -ENODEV;
 
-       return g_touchscreen_funcs->glove_mode_get_state(state);
+       return hal_touchscreen_funcs->glove_mode_get_state(state);
 }
 
 int hal_device_touchscreen_glove_mode_set_state(int state)
 {
-       if (!g_touchscreen_funcs ||
-           !g_touchscreen_funcs->glove_mode_set_state)
+       int ret;
+
+       if (!hal_touchscreen_funcs && !hal_initialized) {
+               if ((ret = hal_device_touchscreen_get_backend()) < 0)
+                       return ret;
+       }
+
+       if (!hal_touchscreen_funcs ||
+           !hal_touchscreen_funcs->glove_mode_set_state)
                return -ENODEV;
 
-       return g_touchscreen_funcs->glove_mode_set_state(state);
+       return hal_touchscreen_funcs->glove_mode_set_state(state);
 }
index b07af00..95b75d5 100644 (file)
 #include "hal-board.h"
 #include "hal-usb_gadget-interface.h"
 
-hal_backend_usb_gadget_funcs *g_usb_gadget_funcs;
+static hal_backend_usb_gadget_funcs *hal_usb_gadget_funcs;
+/*
+-1 : failed to initialize
+0  : not initialized
+1  : succeeded to initialize
+*/
+static int hal_initialized = 0;
 
 static int usb_gadget_is_valid(hal_backend_usb_gadget_funcs *usb_gadget_funcs)
 {
@@ -73,56 +79,74 @@ int hal_device_usb_gadget_get_backend(void)
 {
        int ret;
 
-       if (g_usb_gadget_funcs)
+       if (hal_usb_gadget_funcs)
                return 0;
 
-       ret = hal_common_get_backend(HAL_MODULE_DEVICE_USB_GADGET, (void **)&g_usb_gadget_funcs);
+       ret = hal_common_get_backend(HAL_MODULE_DEVICE_USB_GADGET, (void **)&hal_usb_gadget_funcs);
        if (ret < 0) {
                _E("Failed to get USB gadget backend");
+               hal_initialized = -1;
                return -EINVAL;
        }
 
-       (void) get_board_serial(g_usb_gadget_funcs);
+       (void) get_board_serial(hal_usb_gadget_funcs);
 
-       if(!usb_gadget_is_valid(g_usb_gadget_funcs)) {
+       if(!usb_gadget_is_valid(hal_usb_gadget_funcs)) {
                _E("USB gadget backend is not valid");
-               hal_common_put_backend(HAL_MODULE_DEVICE_USB_GADGET, (void *)g_usb_gadget_funcs);
+               hal_common_put_backend(HAL_MODULE_DEVICE_USB_GADGET, (void *)hal_usb_gadget_funcs);
+               hal_initialized = -1;
                return -ENODEV;
        }
 
+       hal_initialized = 1;
        return 0;
 }
 
 int hal_device_usb_gadget_put_backend(void)
 {
-       if (!g_usb_gadget_funcs)
+       if (!hal_usb_gadget_funcs)
                return 0;
 
-       if(!usb_gadget_is_valid(g_usb_gadget_funcs))
+       if(!usb_gadget_is_valid(hal_usb_gadget_funcs))
                return -ENODEV;
 
-       hal_common_put_backend(HAL_MODULE_DEVICE_USB_GADGET, (void *)g_usb_gadget_funcs);
+       hal_common_put_backend(HAL_MODULE_DEVICE_USB_GADGET, (void *)hal_usb_gadget_funcs);
 
-       free(g_usb_gadget_funcs->board_serial);
-       g_usb_gadget_funcs = NULL;
+       free(hal_usb_gadget_funcs->board_serial);
+       hal_usb_gadget_funcs = NULL;
+       hal_initialized = 0;
 
        return 0;
 }
 
 int hal_device_usb_gadget_enable(void)
 {
-       if(!usb_gadget_is_valid(g_usb_gadget_funcs))
+       int ret;
+
+       if (!hal_usb_gadget_funcs && !hal_initialized) {
+               if ((ret = hal_device_usb_gadget_get_backend()) < 0)
+                       return ret;
+       }
+
+       if(!usb_gadget_is_valid(hal_usb_gadget_funcs))
                return -ENODEV;
 
-       return g_usb_gadget_funcs->enable();
+       return hal_usb_gadget_funcs->enable();
 }
 
 int hal_device_usb_gadget_disable(void)
 {
-       if(!usb_gadget_is_valid(g_usb_gadget_funcs))
+       int ret;
+
+       if (!hal_usb_gadget_funcs && !hal_initialized) {
+               if ((ret = hal_device_usb_gadget_get_backend()) < 0)
+                       return ret;
+       }
+
+       if(!usb_gadget_is_valid(hal_usb_gadget_funcs))
                return -ENODEV;
 
-       return g_usb_gadget_funcs->disable();
+       return hal_usb_gadget_funcs->disable();
 }
 
 int hal_device_usb_gadget_change_mode(unsigned mode)
@@ -131,20 +155,25 @@ int hal_device_usb_gadget_change_mode(unsigned mode)
        struct usb_gadget *gadget;
        struct usb_gadget_id gadget_id;
 
-       if(!usb_gadget_is_valid(g_usb_gadget_funcs))
+       if (!hal_usb_gadget_funcs && !hal_initialized) {
+               if ((ret = hal_device_usb_gadget_get_backend()) < 0)
+                       return ret;
+       }
+
+       if(!usb_gadget_is_valid(hal_usb_gadget_funcs))
                return -ENODEV;
 
        memset(&gadget_id, 0, sizeof(gadget_id));
        gadget_id.function_mask = mode;
 
-       ret = g_usb_gadget_funcs->id_to_gadget(&gadget_id, g_usb_gadget_funcs->board_serial, &gadget);
+       ret = hal_usb_gadget_funcs->id_to_gadget(&gadget_id, hal_usb_gadget_funcs->board_serial, &gadget);
        if (ret) {
                _E("Unable to translate USB mode id into gadget: %d", ret);
                return ret;
        }
 
-       ret = g_usb_gadget_funcs->reconfigure_gadget(gadget);
-       g_usb_gadget_funcs->cleanup_gadget(gadget);
+       ret = hal_usb_gadget_funcs->reconfigure_gadget(gadget);
+       hal_usb_gadget_funcs->cleanup_gadget(gadget);
        if (ret) {
                _E("Unable to configure USB gadget: %d", ret);
                return ret;