audio: changes for audio device API 2.0.

Removed implementations of obsolete function get_supported_devices()

Audio HAL wrapper provides conversion between new and old device enums.
It exposes a rev 2.0 audio device API to the audio framework and allow
legacy implementation to use old device enums.

TODO: make sure that Qualcomm proprietary device IDs are defined properly.

Change-Id: I6779d6c9bccb531d70d84136cdc8d56208b5c934
diff --git a/alsa_sound/audio_hw_hal.cpp b/alsa_sound/audio_hw_hal.cpp
index 98edcf7..08f9025 100644
--- a/alsa_sound/audio_hw_hal.cpp
+++ b/alsa_sound/audio_hw_hal.cpp
@@ -53,6 +53,91 @@
     AudioStreamIn *qcom_in;
 };
 
+
+enum {
+    HAL_API_REV_1_0,
+    HAL_API_REV_2_0,
+    HAL_API_REV_NUM
+} hal_api_rev;
+
+static uint32_t audio_device_conv_table[][HAL_API_REV_NUM] =
+{
+        /* output devices */
+    { AudioSystem::DEVICE_OUT_EARPIECE, AUDIO_DEVICE_OUT_EARPIECE },
+    { AudioSystem::DEVICE_OUT_SPEAKER, AUDIO_DEVICE_OUT_SPEAKER },
+    { AudioSystem::DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADSET },
+    { AudioSystem::DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADPHONE },
+    { AudioSystem::DEVICE_OUT_BLUETOOTH_SCO, AUDIO_DEVICE_OUT_BLUETOOTH_SCO },
+    { AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET },
+    { AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT },
+    { AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP },
+    { AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES },
+    { AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER },
+    { AudioSystem::DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_AUX_DIGITAL },
+    { AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET },
+    { AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET },
+    { AudioSystem::DEVICE_OUT_DEFAULT, AUDIO_DEVICE_OUT_DEFAULT },
+#ifdef QCOM_ANC_HEADSET_ENABLED
+    { AudioSystem::DEVICE_OUT_ANC_HEADSET, AUDIO_DEVICE_OUT_ANC_HEADSET },
+    { AudioSystem::DEVICE_OUT_ANC_HEADPHONE, AUDIO_DEVICE_OUT_ANC_HEADPHONE },
+#endif
+#ifdef QCOM_FM_ENABLED
+    { AudioSystem::DEVICE_OUT_FM, AUDIO_DEVICE_OUT_FM },
+#endif
+#ifdef QCOM_FM_TX_ENABLED
+    { AudioSystem::DEVICE_OUT_FM_TX, AUDIO_DEVICE_OUT_FM_TX },
+#endif
+#ifdef QCOM_VOIP_ENABLED
+    { AudioSystem::DEVICE_OUT_DIRECTOUTPUT, AUDIO_DEVICE_OUT_DIRECTOUTPUT },
+#endif
+#ifdef QCOM_PROXY_DEVICE_ENABLED
+    { AudioSystem::DEVICE_OUT_PROXY, AUDIO_DEVICE_OUT_PROXY },
+#endif
+    /* input devices */
+    { AudioSystem::DEVICE_IN_COMMUNICATION, AUDIO_DEVICE_IN_COMMUNICATION },
+    { AudioSystem::DEVICE_IN_AMBIENT, AUDIO_DEVICE_IN_AMBIENT },
+    { AudioSystem::DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC },
+    { AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET },
+    { AudioSystem::DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET },
+    { AudioSystem::DEVICE_IN_AUX_DIGITAL, AUDIO_DEVICE_IN_AUX_DIGITAL },
+    { AudioSystem::DEVICE_IN_VOICE_CALL, AUDIO_DEVICE_IN_VOICE_CALL },
+    { AudioSystem::DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BACK_MIC },
+    { AudioSystem::DEVICE_IN_DEFAULT, AUDIO_DEVICE_IN_DEFAULT },
+#ifdef QCOM_ANC_HEADSET_ENABLED
+    { AudioSystem::DEVICE_IN_ANC_HEADSET, AUDIO_DEVICE_IN_ANC_HEADSET },
+#endif
+#ifdef QCOM_FM_ENABLED
+    { AudioSystem::DEVICE_IN_FM_RX, AUDIO_DEVICE_IN_FM_RX },
+    { AudioSystem::DEVICE_IN_FM_RX_A2DP, AUDIO_DEVICE_IN_FM_RX_A2DP },
+#endif
+};
+
+static uint32_t convert_audio_device(uint32_t from_device, int from_rev, int to_rev)
+{
+    const uint32_t k_num_devices = sizeof(audio_device_conv_table)/sizeof(uint32_t)/HAL_API_REV_NUM;
+    uint32_t to_device =  AUDIO_DEVICE_NONE;
+    uint32_t in_bit = 0;
+
+    if (from_rev != HAL_API_REV_1_0) {
+        in_bit = from_device & AUDIO_DEVICE_BIT_IN;
+        from_device &= ~AUDIO_DEVICE_BIT_IN;
+    }
+
+    while (from_device) {
+        uint32_t i = 31 - __builtin_clz(from_device);
+        uint32_t cur_device = (1 << i) | in_bit;
+
+        for (i = 0; i < k_num_devices; i++) {
+            if (audio_device_conv_table[i][from_rev] == cur_device) {
+                to_device |= audio_device_conv_table[i][to_rev];
+                break;
+            }
+        }
+        from_device &= ~cur_device;
+    }
+    return to_device;
+}
+
 /** audio_stream_out implementation **/
 static uint32_t out_get_sample_rate(const struct audio_stream *stream)
 {
@@ -120,7 +205,18 @@
 {
     struct qcom_stream_out *out =
         reinterpret_cast<struct qcom_stream_out *>(stream);
-    return out->qcom_out->setParameters(String8(kvpairs));
+    int val;
+    String8 s8 = String8(kvpairs);
+    AudioParameter parms = AudioParameter(String8(kvpairs));
+
+    if (parms.getInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val) == NO_ERROR) {
+        val = convert_audio_device(val, HAL_API_REV_2_0, HAL_API_REV_1_0);
+        parms.remove(String8(AUDIO_PARAMETER_STREAM_ROUTING));
+        parms.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val);
+        s8 = parms.toString();
+    }
+
+    return out->qcom_out->setParameters(s8);
 }
 
 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
@@ -128,7 +224,18 @@
     const struct qcom_stream_out *out =
         reinterpret_cast<const struct qcom_stream_out *>(stream);
     String8 s8;
+    int val;
+
     s8 = out->qcom_out->getParameters(String8(keys));
+
+    AudioParameter parms = AudioParameter(s8);
+    if (parms.getInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val) == NO_ERROR) {
+        val = convert_audio_device(val, HAL_API_REV_1_0, HAL_API_REV_2_0);
+        parms.remove(String8(AUDIO_PARAMETER_STREAM_ROUTING));
+        parms.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val);
+        s8 = parms.toString();
+    }
+
     return strdup(s8.string());
 }
 
@@ -247,7 +354,18 @@
 {
     struct qcom_stream_in *in =
         reinterpret_cast<struct qcom_stream_in *>(stream);
-    return in->qcom_in->setParameters(String8(kvpairs));
+    int val;
+    AudioParameter parms = AudioParameter(String8(kvpairs));
+    String8 s8 = String8(kvpairs);
+
+    if (parms.getInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val) == NO_ERROR) {
+        val = convert_audio_device(val, HAL_API_REV_2_0, HAL_API_REV_1_0);
+        parms.remove(String8(AUDIO_PARAMETER_STREAM_ROUTING));
+        parms.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val);
+        s8 = parms.toString();
+    }
+
+    return in->qcom_in->setParameters(s8);
 }
 
 static char * in_get_parameters(const struct audio_stream *stream,
@@ -256,7 +374,18 @@
     const struct qcom_stream_in *in =
         reinterpret_cast<const struct qcom_stream_in *>(stream);
     String8 s8;
+    int val;
+
     s8 = in->qcom_in->getParameters(String8(keys));
+
+    AudioParameter parms = AudioParameter(s8);
+    if (parms.getInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val) == NO_ERROR) {
+        val = convert_audio_device(val, HAL_API_REV_1_0, HAL_API_REV_2_0);
+        parms.remove(String8(AUDIO_PARAMETER_STREAM_ROUTING));
+        parms.addInt(String8(AUDIO_PARAMETER_STREAM_ROUTING), val);
+        s8 = parms.toString();
+    }
+
     return strdup(s8.string());
 }
 
@@ -307,58 +436,6 @@
     return reinterpret_cast<const struct qcom_audio_device *>(dev);
 }
 
-static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
-{
-    /* XXX: The old AudioHardwareInterface interface is not smart enough to
-     * tell us this, so we'll lie and basically tell AF that we support the
-     * below input/output devices and cross our fingers. To do things properly,
-     * audio hardware interfaces that need advanced features (like this) should
-     * convert to the new HAL interface and not use this wrapper. */
-    return (/* OUT */
-            AUDIO_DEVICE_OUT_EARPIECE |
-            AUDIO_DEVICE_OUT_SPEAKER |
-            AUDIO_DEVICE_OUT_WIRED_HEADSET |
-            AUDIO_DEVICE_OUT_WIRED_HEADPHONE |
-            AUDIO_DEVICE_OUT_AUX_DIGITAL |
-            AUDIO_DEVICE_OUT_ALL_SCO |
-#ifdef QCOM_ANC_HEADSET_ENABLED
-            AUDIO_DEVICE_OUT_ANC_HEADSET |
-            AUDIO_DEVICE_OUT_ANC_HEADPHONE |
-#endif
-            AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET |
-            AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET |
-#ifdef QCOM_FM_ENABLED
-            AUDIO_DEVICE_OUT_FM |
-#endif
-#ifdef QCOM_FM_TX_ENABLED
-            AUDIO_DEVICE_OUT_FM_TX |
-#endif
-#ifdef QCOM_VOIP_ENABLED
-            AUDIO_DEVICE_OUT_DIRECTOUTPUT |
-#endif
-#ifdef QCOM_PROXY_DEVICE_ENABLED
-            AUDIO_DEVICE_OUT_PROXY |
-#endif
-            AUDIO_DEVICE_OUT_DEFAULT |
-            /* IN */
-            AUDIO_DEVICE_IN_VOICE_CALL |
-            AUDIO_DEVICE_IN_COMMUNICATION |
-            AUDIO_DEVICE_IN_AMBIENT |
-            AUDIO_DEVICE_IN_BUILTIN_MIC |
-            AUDIO_DEVICE_IN_WIRED_HEADSET |
-            AUDIO_DEVICE_IN_AUX_DIGITAL |
-            AUDIO_DEVICE_IN_BACK_MIC |
-            AUDIO_DEVICE_IN_ALL_SCO |
-#ifdef QCOM_ANC_HEADSET_ENABLED
-            AUDIO_DEVICE_IN_ANC_HEADSET |
-#endif
-#ifdef QCOM_FM_ENABLED
-            AUDIO_DEVICE_IN_FM_RX |
-            AUDIO_DEVICE_IN_FM_RX_A2DP |
-#endif
-            AUDIO_DEVICE_IN_DEFAULT);
-}
-
 static int adev_init_check(const struct audio_hw_device *dev)
 {
     const struct qcom_audio_device *qadev = to_cladev(dev);
@@ -487,6 +564,8 @@
     if (!out)
         return -ENOMEM;
 
+    devices = convert_audio_device(devices, HAL_API_REV_2_0, HAL_API_REV_1_0);
+
     out->qcom_out = qadev->hwif->openOutputStream(devices,
                                                     (int *)&config->format,
                                                     &config->channel_mask,
@@ -550,6 +629,8 @@
     if (!in)
         return -ENOMEM;
 
+    devices = convert_audio_device(devices, HAL_API_REV_2_0, HAL_API_REV_1_0);
+
     in->qcom_in = qadev->hwif->openInputStream(devices, (int *)&config->format,
                                     &config->channel_mask,
                                     &config->sample_rate,
@@ -634,11 +715,10 @@
         return -ENOMEM;
 
     qadev->device.common.tag = HARDWARE_DEVICE_TAG;
-    qadev->device.common.version = AUDIO_DEVICE_API_VERSION_1_0;
+    qadev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
     qadev->device.common.module = const_cast<hw_module_t*>(module);
     qadev->device.common.close = qcom_adev_close;
 
-    qadev->device.get_supported_devices = adev_get_supported_devices;
     qadev->device.init_check = adev_init_check;
     qadev->device.set_voice_volume = adev_set_voice_volume;
     qadev->device.set_master_volume = adev_set_master_volume;
@@ -684,8 +764,8 @@
     module: {
         common: {
             tag: HARDWARE_MODULE_TAG,
-            version_major: 1,
-            version_minor: 0,
+            module_api_version: AUDIO_MODULE_API_VERSION_0_1,
+            hal_api_version: HARDWARE_HAL_API_VERSION,
             id: AUDIO_HARDWARE_MODULE_ID,
             name: "QCOM Audio HW HAL",
             author: "Code Aurora Forum",