hal: add sidetone support for USB

if usb device provides sidetone contols
- disable sidetone when usb is detected
- enable side tone only for calls.

Bug: 37404459
Test: made voice call, confirmed sidetone. Following call,
music capture and playback does not have sidetone present.

Change-Id: If2e4926f50aaaae07ea72fd5528ab0cd6658d8ab
Signed-off-by: Andrew Chant <achant@google.com>
diff --git a/hal/Android.mk b/hal/Android.mk
index 2de6c16..0d2b6b9 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -80,6 +80,10 @@
     LOCAL_SRC_FILES += audio_extn/usb.c
 endif
 
+ifeq ($(strip $(AUDIO_FEATURE_ENABLED_USB_SIDETONE_VOLUME)),true)
+    LOCAL_CFLAGS += -DUSB_SIDETONE_VOLUME
+endif
+
 LOCAL_SHARED_LIBRARIES := \
 	libaudioutils \
 	liblog \
diff --git a/hal/audio_extn/usb.c b/hal/audio_extn/usb.c
index 4e4c70d..6b3ee7b 100644
--- a/hal/audio_extn/usb.c
+++ b/hal/audio_extn/usb.c
@@ -505,9 +505,12 @@
             usb_card_info->usb_sidetone_index[USB_SIDETONE_ENABLE_INDEX] = index;
             /* Disable device sidetone by default */
             mixer_ctl_set_value(ctl, 0, false);
+            ALOGV("%s:: sidetone mixer Control found(%s) ... disabling by default",
+                   __func__, usb_sidetone_enable_str[index]);
             break;
         }
     }
+#ifdef USB_SIDETONE_VOLUME
     for (index = 0;
          index < sizeof(usb_sidetone_volume_str)/sizeof(usb_sidetone_volume_str[0]);
          index++) {
@@ -520,7 +523,7 @@
             break;
         }
     }
-
+#endif // USB_SIDETONE_VOLUME
     if ((usb_card_info->usb_snd_mixer != NULL) && (usb_audio_debug_enable))
         usb_soundcard_list_controls(usb_card_info->usb_snd_mixer);
 
@@ -848,6 +851,7 @@
                 else
                     break;
 
+#ifdef USB_SIDETONE_VOLUME
                 if ((i = card_info->usb_sidetone_index[USB_SIDETONE_VOLUME_INDEX]) != -1) {
                     ctl = mixer_get_ctl_by_name(
                                 card_info->usb_snd_mixer,
@@ -859,6 +863,7 @@
                         mixer_ctl_set_value(ctl, 0,
                                             usb_get_sidetone_gain(card_info));
                 }
+#endif // USB_SIDETONE_VOLUME
                 ret = 0;
                 break;
             }
diff --git a/hal/msm8916/platform.c b/hal/msm8916/platform.c
index 6d92d93..628d1d6 100644
--- a/hal/msm8916/platform.c
+++ b/hal/msm8916/platform.c
@@ -2335,3 +2335,25 @@
     return -ENOSYS;
 }
 
+int platform_set_sidetone(struct audio_device *adev,
+                          snd_device_t out_snd_device,
+                          bool enable, char *str)
+{
+    int ret;
+    if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
+        out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
+            ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
+            if (ret)
+                ALOGI("%s: usb device %d does not support device sidetone\n",
+                  __func__, out_snd_device);
+    } else {
+        ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
+              __func__, out_snd_device, str);
+
+        if (enable)
+            audio_route_apply_and_update_path(adev->audio_route, str);
+        else
+            audio_route_reset_and_update_path(adev->audio_route, str);
+    }
+    return 0;
+}
diff --git a/hal/msm8960/platform.c b/hal/msm8960/platform.c
index cd7305d..c30ccda 100644
--- a/hal/msm8960/platform.c
+++ b/hal/msm8960/platform.c
@@ -1169,3 +1169,25 @@
 int platform_get_snd_device_backend_index(snd_device_t snd_device) {
     return -ENOSYS;
 }
+
+int platform_set_sidetone(struct audio_device *adev,
+                          snd_device_t out_snd_device,
+                          bool enable, char *str)
+{
+    int ret;
+    if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
+        out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
+            ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
+            if (ret)
+                ALOGI("%s: usb device %d does not support device sidetone\n",
+                  __func__, out_snd_device);
+    } else {
+        ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
+              __func__, out_snd_device, str);
+        if (enable)
+            audio_route_apply_and_update_path(adev->audio_route, str);
+        else
+            audio_route_reset_and_update_path(adev->audio_route, str);
+    }
+    return 0;
+}
diff --git a/hal/msm8974/platform.c b/hal/msm8974/platform.c
index 58f7927..cfe2b3d 100644
--- a/hal/msm8974/platform.c
+++ b/hal/msm8974/platform.c
@@ -3010,6 +3010,8 @@
         ALOGV("%s: max_mic_count %s/%d", __func__, value, my_data->max_mic_count);
     }
 
+    // to-do: disable setting sidetone gain, will revist this later
+    // audio_extn_usb_set_sidetone_gain(parms, value, len);
 done:
     ALOGV("%s: exit with code(%d)", __func__, ret);
     if (kv_pairs != NULL)
@@ -3995,3 +3997,25 @@
     }
     return 0;
 }
+
+int platform_set_sidetone(struct audio_device *adev,
+                          snd_device_t out_snd_device,
+                          bool enable, char *str)
+{
+    int ret;
+    if (out_snd_device == SND_DEVICE_OUT_USB_HEADSET ||
+        out_snd_device == SND_DEVICE_OUT_VOICE_USB_HEADSET) {
+            ret = audio_extn_usb_enable_sidetone(out_snd_device, enable);
+            if (ret)
+                ALOGI("%s: usb device %d does not support device sidetone\n",
+                  __func__, out_snd_device);
+    } else {
+        ALOGV("%s: sidetone out device(%d) mixer cmd = %s\n",
+              __func__, out_snd_device, str);
+        if (enable)
+            audio_route_apply_and_update_path(adev->audio_route, str);
+        else
+            audio_route_reset_and_update_path(adev->audio_route, str);
+    }
+    return 0;
+}
diff --git a/hal/platform_api.h b/hal/platform_api.h
index 98e4223..bca3e2b 100644
--- a/hal/platform_api.h
+++ b/hal/platform_api.h
@@ -142,4 +142,7 @@
                            const char *mode,
                            int bw, int app_type, int max_sr);
 int platform_get_snd_device_backend_index(snd_device_t snd_device);
+int platform_set_sidetone(struct audio_device *adev,
+                          snd_device_t out_snd_device,
+                          bool enable, char * str);
 #endif // AUDIO_PLATFORM_API_H
diff --git a/hal/voice.c b/hal/voice.c
index 611d875..6f16137 100644
--- a/hal/voice.c
+++ b/hal/voice.c
@@ -68,7 +68,13 @@
     case SND_DEVICE_OUT_VOICE_HEADPHONES:
         strlcpy(mixer_path, "sidetone-headphones", MIXER_PATH_MAX_LENGTH);
         break;
+    case SND_DEVICE_OUT_VOICE_USB_HEADSET:
+    case SND_DEVICE_OUT_USB_HEADSET:
+        // USB does not use a QC mixer.
+        mixer_path[0] = '\0';
+        break;
     default:
+        ALOGW("%s: %d is not a sidetone device", __func__, out_device);
         is_sidetone_dev = false;
         break;
     }
@@ -86,21 +92,8 @@
           __func__, (enable ? "enable" : "disable"),
           out_snd_device);
 
-    is_sidetone_dev = voice_is_sidetone_device(out_snd_device, mixer_path);
-
-    if (!is_sidetone_dev) {
-        ALOGD("%s: device %d does not support sidetone\n",
-              __func__, out_snd_device);
-        return;
-    }
-
-    ALOGD("%s: sidetone out device = %s\n",
-          __func__, mixer_path);
-
-    if (enable)
-        audio_route_apply_and_update_path(adev->audio_route, mixer_path);
-    else
-        audio_route_reset_and_update_path(adev->audio_route, mixer_path);
+    if (voice_is_sidetone_device(out_snd_device, mixer_path))
+        platform_set_sidetone(adev, out_snd_device, enable, mixer_path);
 
     return;
 }