hal: restrict 24 bit capture for unprocessed audio

- configure backend to 24 bit only when source is unprocessed
- for all other source force 16 bit only

- final result

  -----------------------------------------------------------------
  source        app request    audio policy requests    hal returns
  -----------------------------------------------------------------
  Unprocessed   16             8_24                     8_24
  Unprocessed   24/float/8_24  24/float/8_24            8_24

  any other     16             8_24                     -EINVAL
  (retry)                      16                       16

  any other     24/float/8_24  24/float/8_24            -EINVAL
  (retry)                      16                       16
  -----------------------------------------------------------------

Bug: 27348418.

Change-Id: I01139e741a414db1b9ff0079984cb5da007f9a0c
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 43a38aa..d70f9ef 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2706,19 +2706,40 @@
     in->channel_mask = config->channel_mask;
     in->capture_handle = handle;
     in->flags = flags;
-    in->format = config->format;
-    // in->frames_read = 0;
 
-    if (in->format == AUDIO_FORMAT_DEFAULT)
+    // restrict 24 bit capture for unprocessed source only
+    // for other sources if 24 bit requested reject 24 and set 16 bit capture only
+    if (config->format == AUDIO_FORMAT_DEFAULT) {
         config->format = AUDIO_FORMAT_PCM_16_BIT;
+    } else if (config->format == AUDIO_FORMAT_PCM_FLOAT ||
+               config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED ||
+               config->format == AUDIO_FORMAT_PCM_8_24_BIT) {
+        bool ret_error = false;
+        /* 24 bit is restricted to UNPROCESSED source only,also format supported
+           from HAL is 8_24
+           *> In case of UNPROCESSED source, for 24 bit, if format requested is other than
+              8_24 return error indicating supported format is 8_24
+           *> In case of any other source requesting 24 bit or float return error
+              indicating format supported is 16 bit only.
 
-    if (config->format == AUDIO_FORMAT_PCM_FLOAT ||
-        config->format == AUDIO_FORMAT_PCM_24_BIT_PACKED) {
-        config->format = AUDIO_FORMAT_PCM_8_24_BIT;
-        ret = -EINVAL;
-        goto err_open;
+           on error flinger will retry with supported format passed
+         */
+        if (source != AUDIO_SOURCE_UNPROCESSED) {
+            config->format = AUDIO_FORMAT_PCM_16_BIT;
+            ret_error = true;
+        } else if (config->format != AUDIO_FORMAT_PCM_8_24_BIT) {
+            config->format = AUDIO_FORMAT_PCM_8_24_BIT;
+            ret_error = true;
+        }
+
+        if (ret_error) {
+            ret = -EINVAL;
+            goto err_open;
+        }
     }
 
+    in->format = config->format;
+
     /* Update config params with the requested sample rate and channels */
     if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
         if (config->sample_rate == 0)