db845c: audio_hal: Select the output/input card a bit more dynamically am: 57a023620c

Original change: https://android-review.googlesource.com/c/device/linaro/dragonboard/+/1817579

Change-Id: I12f7b06efd5368a9792094a245c11a5a94995be3
diff --git a/audio/audio_hw.c b/audio/audio_hw.c
index 5a0953c..c0cd55d 100644
--- a/audio/audio_hw.c
+++ b/audio/audio_hw.c
@@ -72,6 +72,20 @@
     return PORT_HDMI;
 }
 
+static int get_audio_card(int direction, int port) {
+    struct pcm_params* params = NULL;
+    int card = 0;
+
+    while (!params && card < 8) {
+	/* Find the first input/output device that works */
+        params = pcm_params_get(card, port, direction);
+	card++;
+    }
+    pcm_params_free(params);
+
+    return card - 1;
+}
+
 static void timestamp_adjust(struct timespec* ts, ssize_t frames, uint32_t sampling_rate) {
     /* This function assumes the adjustment (in nsec) is less than the max value of long,
      * which for 32-bit long this is 2^31 * 1e-9 seconds, slightly over 2 seconds.
@@ -173,9 +187,10 @@
     out->unavailable = true;
     unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
     int out_port = get_audio_output_port(out->devices);
+    int out_card = get_audio_card(PCM_OUT, out_port);
 
     while (1) {
-        out->pcm = pcm_open(CARD_OUT, out_port, PCM_OUT | PCM_MONOTONIC, &out->config);
+        out->pcm = pcm_open(out_card, out_port, PCM_OUT | PCM_MONOTONIC, &out->config);
         if ((out->pcm != NULL) && pcm_is_ready(out->pcm)) {
             break;
         } else {
@@ -433,9 +448,10 @@
     struct alsa_audio_device *adev = in->dev;
     in->unavailable = true;
     unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
+    int in_card = get_audio_card(PCM_IN, PORT_BUILTIN_MIC);
 
     while (1) {
-        in->pcm = pcm_open(CARD_IN, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config);
+        in->pcm = pcm_open(in_card, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config);
         if ((in->pcm != NULL) && pcm_is_ready(in->pcm)) {
             break;
         } else {
@@ -795,7 +811,8 @@
 
     struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
     int out_port = get_audio_output_port(devices);
-    struct pcm_params* params = pcm_params_get(CARD_OUT, out_port, PCM_OUT);
+    int out_card = get_audio_card(PCM_OUT, out_port);
+    struct pcm_params* params = pcm_params_get(out_card, out_port, PCM_OUT);
     if (!params) {
         return -ENOSYS;
     }
@@ -991,7 +1008,8 @@
 
     struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
 
-    struct pcm_params* params = pcm_params_get(CARD_IN, PORT_BUILTIN_MIC, PCM_IN);
+    int in_card = get_audio_card(PCM_IN, PORT_BUILTIN_MIC);
+    struct pcm_params* params = pcm_params_get(in_card, PORT_BUILTIN_MIC, PCM_IN);
     if (!params) {
         return -ENOSYS;
     }
@@ -1139,13 +1157,14 @@
 
     *device = &adev->hw_device.common;
 
-    adev->mixer = mixer_open(CARD_OUT);
+    int out_card = get_audio_card(PCM_OUT, 0);
+    adev->mixer = mixer_open(out_card);
     if (!adev->mixer) {
         ALOGE("Unable to open the mixer, aborting.");
         goto error_1;
     }
 
-    adev->audio_route = audio_route_init(CARD_OUT, MIXER_XML_PATH);
+    adev->audio_route = audio_route_init(out_card, MIXER_XML_PATH);
     if (!adev->audio_route) {
         ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
         goto error_2;
diff --git a/audio/audio_hw.h b/audio/audio_hw.h
index 3e8e27c..2e45e02 100644
--- a/audio/audio_hw.h
+++ b/audio/audio_hw.h
@@ -22,10 +22,8 @@
 
 #include "fir_filter.h"
 
-#define CARD_OUT 0
 #define PORT_HDMI 0
 #define PORT_INTERNAL_SPEAKER 1
-#define CARD_IN 0
 #define PORT_BUILTIN_MIC 3
 
 #define MIXER_XML_PATH "/vendor/etc/mixer_paths.xml"