msm8916: hal: optimize worst case soundcard detection time

current sound card detection logic
   - iterate from sound card 0 to Max i.e. 8
   - on each iteration check if soundcard is not enumerated then
     wait for 500 ms and check again, try up-to 10 retries
   - worst case time is 40 sec  (8 * 10 * 500ms)
New sound card detection logic
   - iterate for max retries i.e. 10
   - on each iteration check if any new sound card enumerated i.e.
     probe soundcard only if on previous iteration it failed
   - on failure move to next card until all card are verified
   - at this point wait for 500ms and retry
   - worst case time is 5 sec  (10 * 500ms)

Change-Id: I63da64ce90ac037ad3a85d30cf8355a992e2c510
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 900a7e6..f2efec3 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -842,24 +842,39 @@
     }
 
     list_init(&operator_info_list);
+    bool card_verifed[MAX_SND_CARD] = {0};
+    const int retry_limit = property_get_int32("audio.snd_card.open.retries", RETRY_NUMBER);
 
-    while (snd_card_num < MAX_SND_CARD) {
-        adev->mixer = mixer_open(snd_card_num);
+    for (;;) {
+        if (snd_card_num >= MAX_SND_CARD) {
+            if (retry_num++ >= retry_limit) {
+                ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
+                free(my_data);
+                my_data = NULL;
+                return NULL;
+            }
 
-        while (!adev->mixer && retry_num < RETRY_NUMBER) {
+            snd_card_num = 0;
             usleep(RETRY_US);
-            adev->mixer = mixer_open(snd_card_num);
-            retry_num++;
+            continue;
         }
 
+        if (card_verifed[snd_card_num]) {
+            ++snd_card_num;
+            continue;
+        }
+
+        adev->mixer = mixer_open(snd_card_num);
+
         if (!adev->mixer) {
             ALOGE("%s: Unable to open the mixer card: %d", __func__,
-                   snd_card_num);
-            retry_num = 0;
-            snd_card_num++;
+               snd_card_num);
+            ++snd_card_num;
             continue;
         }
 
+        card_verifed[snd_card_num] = true;
+
         snd_card_name = mixer_get_name(adev->mixer);
         ALOGV("%s: snd_card_name: %s", __func__, snd_card_name);
 
@@ -875,21 +890,21 @@
             if (!adev->audio_route) {
                 ALOGE("%s: Failed to init audio route controls, aborting.",
                        __func__);
+                hw_info_deinit(my_data->hw_info);
+                my_data->hw_info = NULL;
                 free(my_data);
+                my_data = NULL;
+                mixer_close(adev->mixer);
+                adev->mixer = NULL;
                 return NULL;
             }
             adev->snd_card = snd_card_num;
             ALOGD("%s: Opened sound card:%d", __func__, snd_card_num);
             break;
         }
-        retry_num = 0;
-        snd_card_num++;
-    }
-
-    if (snd_card_num >= MAX_SND_CARD) {
-        ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
-        free(my_data);
-        return NULL;
+        ++snd_card_num;
+        mixer_close(adev->mixer);
+        adev->mixer = NULL;
     }
 
     //set max volume step for voice call