hal: gef: update gef extn to deinit gef library on hal deinit

Update gef extension to call deinit of gef library if the call is
available, so that gef is aware of audio hal state and gef init/deinit
is not tightly coupled with hal init/deinit.

Change-Id: I72ced9d6b9535563ee0b8e6d81efd634fcdc3ee3
diff --git a/hal/audio_extn/gef.c b/hal/audio_extn/gef.c
index 19f9dfb..0781f4c 100644
--- a/hal/audio_extn/gef.c
+++ b/hal/audio_extn/gef.c
@@ -62,6 +62,7 @@
 #endif
 
 typedef void* (*gef_init_t)(void*);
+typedef void (*gef_deinit_t)(void*);
 typedef void (*gef_device_config_cb_t)(void*, audio_devices_t,
     audio_channel_mask_t, int, int);
 
@@ -69,6 +70,7 @@
     void* handle;
     void* gef_ptr;
     gef_init_t init;
+    gef_deinit_t deinit;
     gef_device_config_cb_t device_config_cb;
 } gef_data;
 
@@ -138,6 +140,18 @@
             }
 
             //call dlerror to clear the error
+            dlerror();
+            gef_hal_handle.deinit =
+                (gef_deinit_t)dlsym(gef_hal_handle.handle, "gef_deinit");
+            error = dlerror();
+
+            if(error != NULL) {
+                ALOGE("%s: dlsym of %s failed with error %s",
+                     __func__, "gef_deinit", error);
+                goto ERROR_RETURN;
+            }
+
+            //call dlerror to clear the error
             error = dlerror();
             gef_hal_handle.device_config_cb =
                  (gef_device_config_cb_t)dlsym(gef_hal_handle.handle,
@@ -289,6 +303,8 @@
     ALOGV("%s: Enter", __func__);
 
     if (gef_hal_handle.handle) {
+        if (gef_hal_handle.handle && gef_hal_handle.deinit)
+            gef_hal_handle.deinit(gef_hal_handle.gef_ptr);
         dlclose(gef_hal_handle.handle);
     }