Merge 587b0e3208702d1cbb3fc8290f9109b386573636 on remote branch

Change-Id: Id9bbda1a57e081b88b58f80004d95f009f999715
diff --git a/ipc/HwBinders/agm_ipc_client/src/agm_client_wrapper.cpp b/ipc/HwBinders/agm_ipc_client/src/agm_client_wrapper.cpp
index 49860a0..ac58347 100644
--- a/ipc/HwBinders/agm_ipc_client/src/agm_client_wrapper.cpp
+++ b/ipc/HwBinders/agm_ipc_client/src/agm_client_wrapper.cpp
@@ -1,6 +1,5 @@
 /*
  * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -28,38 +27,10 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  * Changes from Qualcomm Innovation Center are provided under the following license:
- * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted (subject to the limitations in the
- * disclaimer below) provided that the following conditions are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *
- *     * Redistributions in binary form must reproduce the above
- *       copyright notice, this list of conditions and the following
- *       disclaimer in the documentation and/or other materials provided
- *       with the distribution.
- *
- *     * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
- * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
- * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
- * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
- * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
  */
+
 #define LOG_TAG "agm_client_wrapper"
 
 #include <cutils/list.h>
@@ -106,8 +77,6 @@
     agm_server_died = true;
     if (cb_ != NULL)
         cb_(cookie_);
-    // We exit the client process here, so that it also can restart
-    // leading to a fresh start on both the sides.
 }
 
 android::sp<IAGM> get_agm_server() {
diff --git a/plugins/tinyalsa/src/agm_pcm_plugin.c b/plugins/tinyalsa/src/agm_pcm_plugin.c
index 92561fc..52cc4ec 100644
--- a/plugins/tinyalsa/src/agm_pcm_plugin.c
+++ b/plugins/tinyalsa/src/agm_pcm_plugin.c
@@ -709,8 +709,11 @@
 {
     struct agm_pcm_priv *priv = plugin->priv;
     snd_pcm_sframes_t avail = 0;
+    enum direction dir;
 
-    if (plugin->mode & PCM_OUT) {
+    dir = (plugin->mode & PCM_IN) ? TX : RX;
+
+    if (dir == RX) {
         avail = priv->pos_buf->hw_ptr +
             priv->total_size_frames -
             priv->pos_buf->appl_ptr;
@@ -719,7 +722,7 @@
             avail += priv->pos_buf->boundary;
         else if ((snd_pcm_uframes_t)avail >= priv->pos_buf->boundary)
             avail -= priv->pos_buf->boundary;
-    } else if (plugin->mode & PCM_IN) {
+    } else if (dir == TX) {
         __builtin_sub_overflow(priv->pos_buf->hw_ptr, priv->pos_buf->appl_ptr, &avail);
         if (avail < 0)
             avail += priv->pos_buf->boundary;
diff --git a/service/src/device.c b/service/src/device.c
index 78a3679..e59f640 100644
--- a/service/src/device.c
+++ b/service/src/device.c
@@ -1,6 +1,5 @@
 /*
 ** Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
-** Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
 **
 ** Redistribution and use in source and binary forms, with or without
 ** modification, are permitted provided that the following conditions are
@@ -26,6 +25,10 @@
 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+**
+** Changes from Qualcomm Innovation Center are provided under the following license:
+** Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
+** SPDX-License-Identifier: BSD-3-Clause-Clear
 **/
 
 #define LOG_TAG "AGM: device"
@@ -34,7 +37,6 @@
 #include <pthread.h>
 #include <sched.h>
 #include <stdio.h>
-#include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -63,9 +65,6 @@
 #define TRUE 1
 #define FALSE 0
 
-#define DEVICE_ENABLE 1
-#define DEVICE_DISABLE 0
-
 #define BUF_SIZE 1024
 #define FILE_PATH_EXTN_MAX_SIZE 80
 #define MAX_RETRY_CNT 20
@@ -83,9 +82,6 @@
 static struct mixer *mixer = NULL;
 #endif
 
-#define SYSFS_FD_PATH "/sys/kernel/aud_dev/state"
-static int sysfs_fd = -1;
-
 #define MAX_BUF_SIZE                 2048
 /**
   * The maximum period bytes for dummy dai is 8192 bytes.
@@ -150,36 +146,6 @@
      return bits_per_sample;
 }
 
-static void update_sysfs_fd (int8_t pcm_id, int8_t state)
-{
-    char buf[MAX_USR_INPUT]={0};
-    snprintf(buf, MAX_USR_INPUT,"%d %d", pcm_id, state);
-    if (sysfs_fd >= 0)
-        write(sysfs_fd, buf, MAX_USR_INPUT);
-    else {
-        /*AGM service and sysfs file creation are async events.
-         *Also when the syfs node is first created the default
-         *user attribute for the sysfs file is root.
-         *To change the same we need to execute a command from
-         *init scripts, which again are async and there is no
-         *deterministic way of scheduling this command only after
-         *the sysfs node is created. And all this should happen before
-         *we try to open the file from agm context, otherwise the open
-         *call would fail with permission denied error.
-         *Hence we try to open the file first time when we access it instead
-         *of doing it from agm_init.
-         *This gives the system enough time for the file attributes to
-         *be changed.
-         */
-        sysfs_fd = open(SYSFS_FD_PATH, O_WRONLY);
-        if (sysfs_fd >= 0) {
-            write(sysfs_fd, buf, MAX_USR_INPUT);
-        } else {
-            AGM_LOGE("invalid file handle\n");
-        }
-    }
-}
-
 int device_get_snd_card_id()
 {
     struct device_obj *dev_obj = node_to_item(list_head(&device_list),
@@ -297,8 +263,6 @@
                  __func__, pcm_name, rate, channels, format);
         goto done;
     }
-
-    update_sysfs_fd(obj->pcm_id, DEVICE_ENABLE);
     obj->pcm = pcm;
     obj->state = DEV_OPENED;
     obj->refcnt.open++;
@@ -413,7 +377,6 @@
         ret = -EIO;
         goto done;
     }
-    update_sysfs_fd(obj->pcm_id, DEVICE_ENABLE);
     obj->pcm = pcm;
     obj->state = DEV_OPENED;
     obj->refcnt.open++;
@@ -587,7 +550,6 @@
     }
 
     if (--obj->refcnt.open == 0) {
-        update_sysfs_fd(obj->pcm_id, DEVICE_DISABLE);
 #ifdef DEVICE_USES_ALSALIB
         ret = snd_pcm_close(obj->pcm);
 #else
@@ -1174,9 +1136,6 @@
 
     list_remove(&device_group_data_list);
     list_remove(&device_list);
-    if (sysfs_fd >= 0)
-        close(sysfs_fd);
-    sysfs_fd = -1;
 
 #ifdef DEVICE_USES_ALSALIB
     if (mixer)