audio: Modify the logic to find supported sound card

- Current logic iterates over sound cards 0,1,2..8 for a fixed
period of time of 5 secs each, taking the complete time to 40 secs
if a sound card is not found.
- Update the logic to reflect the above design, try for all the sound cards,
if none of them detected, sleep for 1 sec and then iterate from sound card 0
again.

Change-Id: Ie4dde8b7797dcb32f7833a25015faabb273e6e1a
diff --git a/hal/audio_extn/utils.c b/hal/audio_extn/utils.c
index cac1369..607b6ff 100644
--- a/hal/audio_extn/utils.c
+++ b/hal/audio_extn/utils.c
@@ -2069,8 +2069,8 @@
 #endif
 
 #define MAX_SND_CARD 8
-#define RETRY_US 500000
-#define RETRY_NUMBER 10
+#define RETRY_US 1000000
+#define RETRY_NUMBER 40
 
 int audio_extn_utils_get_snd_card_num()
 {
@@ -2078,24 +2078,31 @@
     void *hw_info = NULL;
     struct mixer *mixer = NULL;
     int retry_num = 0;
-    int snd_card_num = 0;
+    int snd_card_num = 0, min_snd_card_num = 0;
     char* snd_card_name = NULL;
 
-    while (snd_card_num < MAX_SND_CARD) {
-        mixer = mixer_open(snd_card_num);
+    /*
+    * Try with all the sound cards ( 0 to 8 ) and if none of them were detected
+    * sleep for 1 sec and try detections with sound card 0 again.
+    * If sound card gets detected, check if it is relevant, if not check with the
+    * other sound cards. To ensure that the irrelevant sound card is not check again,
+    * we maintain it in min_snd_card_num.
+    */
+    while (retry_num < RETRY_NUMBER) {
 
-        while (!mixer && retry_num < RETRY_NUMBER) {
-            usleep(RETRY_US);
-            mixer = mixer_open(snd_card_num);
-            retry_num++;
+        while (snd_card_num <= MAX_SND_CARD) {
+           mixer = mixer_open(snd_card_num);
+           if (!mixer)
+              snd_card_num++;
+           else
+              break;
         }
 
         if (!mixer) {
-            ALOGE("%s: Unable to open the mixer card: %d", __func__,
-                   snd_card_num);
-            retry_num = 0;
-            snd_card_num++;
-            continue;
+           usleep(RETRY_US);
+           snd_card_num = min_snd_card_num;
+           retry_num++;
+           continue;
         }
 
         snd_card_name = strdup(mixer_get_name(mixer));
@@ -2112,8 +2119,11 @@
             break;
         }
         ALOGE("%s: Failed to init hardware info", __func__);
-        retry_num = 0;
-        snd_card_num++;
+        min_snd_card_num++;
+        snd_card_num = min_snd_card_num;
+
+        if (snd_card_num >= MAX_SND_CARD)
+            break;
 
         free(snd_card_name);
         snd_card_name = NULL;
@@ -2123,12 +2133,14 @@
     }
     if (snd_card_name)
         free(snd_card_name);
+
     if (mixer)
         mixer_close(mixer);
+
     if (hw_info)
         hw_info_deinit(hw_info);
 
-    if (snd_card_num >= MAX_SND_CARD) {
+    if ((snd_card_num >= MAX_SND_CARD) || (retry_num >= RETRY_NUMBER)) {
         ALOGE("%s: Unable to find correct sound card, aborting.", __func__);
         return -1;
     }