tinyalsa: add support for tinyalsa pcm plugins

Update the pcm framework to support plugins. Resolve the pcm device node
to be either kernel device or virtual device and setup function pointers
accordingly. Implement framework functionality for pcm plugin for ease
of plugin development.

Bug: 166482201
Test: audio smoke tests

CRs-Fixed: 2563258
Change-Id: I47c8fea4fe20864c7bba2b13377874be96cb2706
Merged-In: I47c8fea4fe20864c7bba2b13377874be96cb2706
Signed-off-by: Bhalchandra Gajare <gajare@codeaurora.org>
(cherry picked from commit d407ef86493217e7874c3d826d83aae0cd570ce5)
diff --git a/Android.bp b/Android.bp
index 410b1de..01f1573 100644
--- a/Android.bp
+++ b/Android.bp
@@ -9,6 +9,9 @@
     srcs: [
         "mixer.c",
         "pcm.c",
+        "pcm_hw.c",
+        "pcm_plugin.c",
+        "snd_utils.c",
     ],
     cflags: ["-Werror", "-Wno-macro-redefined"],
     export_include_dirs: ["include"],
@@ -19,6 +22,8 @@
             enabled: false,
         },
     },
+
+    system_shared_libs: ["libc","libdl"],
 }
 
 cc_binary {
diff --git a/include/tinyalsa/pcm_plugin.h b/include/tinyalsa/pcm_plugin.h
new file mode 100644
index 0000000..5d6f503
--- /dev/null
+++ b/include/tinyalsa/pcm_plugin.h
@@ -0,0 +1,97 @@
+/* pcm_plugin.h
+** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted 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 The Linux Foundation nor the names of its
+**     contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
+**/
+
+#ifndef __PCM_PLUGIN_H__
+#define __PCM_PLUGIN_H__
+
+#define PCM_PLUGIN_OPEN_FN(name)                    \
+    int name##_open(struct pcm_plugin **plugin,     \
+                    unsigned int card,              \
+                    unsigned int device,            \
+                    int mode)
+
+#define PCM_PLUGIN_OPEN_FN_PTR()                        \
+    int (*plugin_open_fn) (struct pcm_plugin **plugin,  \
+                           unsigned int card,           \
+                           unsigned int device,         \
+                           int mode);
+
+struct pcm_plugin;
+
+struct pcm_plugin_ops {
+    int (*close) (struct pcm_plugin *plugin);
+    int (*hw_params) (struct pcm_plugin *plugin,
+                      struct snd_pcm_hw_params *params);
+    int (*sw_params) (struct pcm_plugin *plugin,
+                      struct snd_pcm_sw_params *params);
+    int (*sync_ptr) (struct pcm_plugin *plugin,
+                     struct snd_pcm_sync_ptr *sync_ptr);
+    int (*writei_frames) (struct pcm_plugin *plugin,
+                          struct snd_xferi *x);
+    int (*readi_frames) (struct pcm_plugin *plugin,
+                         struct snd_xferi *x);
+    int (*ttstamp) (struct pcm_plugin *plugin,
+                    int *tstamp);
+    int (*prepare) (struct pcm_plugin *plugin);
+    int (*start) (struct pcm_plugin *plugin);
+    int (*drop) (struct pcm_plugin *plugin);
+    int (*ioctl) (struct pcm_plugin *plugin,
+                  int cmd, void *arg);
+};
+
+struct pcm_plugin_min_max {
+    unsigned int min;
+    unsigned int max;
+};
+
+struct pcm_plugin_hw_constraints {
+    uint64_t access;
+    /* As of this implementation ALSA supports 52 formats */
+    uint64_t format;
+    struct pcm_plugin_min_max bit_width;
+    struct pcm_plugin_min_max channels;
+    struct pcm_plugin_min_max rate;
+    struct pcm_plugin_min_max periods;
+    struct pcm_plugin_min_max period_bytes;
+};
+
+struct pcm_plugin {
+    unsigned int card;
+
+    struct pcm_plugin_ops *ops;
+    struct pcm_plugin_hw_constraints *constraints;
+
+    void *node;
+    int mode;
+    void *priv;
+
+    unsigned int state;
+};
+
+#endif /* end of __PCM_PLUGIN_H__ */
diff --git a/pcm.c b/pcm.c
index 735fb1d..7572f4b 100644
--- a/pcm.c
+++ b/pcm.c
@@ -47,6 +47,13 @@
 #include <sound/asound.h>
 
 #include <tinyalsa/asoundlib.h>
+#include "pcm_io.h"
+#include "snd_utils.h"
+
+enum {
+    PCM_NODE_TYPE_HW = 0,
+    PCM_NODE_TYPE_PLUGIN,
+};
 
 #define PARAM_MAX SNDRV_PCM_HW_PARAM_LAST_INTERVAL
 
@@ -123,6 +130,9 @@
 #endif
 };
 
+extern struct pcm_ops hw_ops;
+extern struct pcm_ops plug_ops;
+
 /* refer to SNDRV_PCM_SUBFORMAT_##index in sound/asound.h. */
 static const char * const subformat_lookup[] = {
         "STD",
@@ -257,6 +267,10 @@
     unsigned int noirq_frames_per_msec;
     int wait_for_avail_min;
     unsigned int subdevice;
+
+    struct pcm_ops *ops;
+    void *data;
+    void *snd_node;
 };
 
 unsigned int pcm_get_buffer_size(struct pcm *pcm)
@@ -336,7 +350,8 @@
 static int pcm_sync_ptr(struct pcm *pcm, int flags) {
     if (pcm->sync_ptr) {
         pcm->sync_ptr->flags = flags;
-        if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SYNC_PTR, pcm->sync_ptr) < 0)
+        if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SYNC_PTR,
+                            pcm->sync_ptr) < 0)
             return -1;
     }
     return 0;
@@ -532,12 +547,12 @@
             int prepare_error = pcm_prepare(pcm);
             if (prepare_error)
                 return prepare_error;
-            if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
+            if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x))
                 return oops(pcm, errno, "cannot write initial data");
             pcm->running = 1;
             return 0;
         }
-        if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
+        if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_WRITEI_FRAMES, &x)) {
             pcm->prepared = 0;
             pcm->running = 0;
             if (errno == EPIPE) {
@@ -573,7 +588,7 @@
                 return -errno;
             }
         }
-        if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
+        if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_READI_FRAMES, &x)) {
             pcm->prepared = 0;
             pcm->running = 0;
             if (errno == EPIPE) {
@@ -595,15 +610,22 @@
                                   unsigned int flags)
 {
     struct snd_pcm_hw_params *params;
-    char fn[256];
+    enum snd_node_type pcm_type;
+    struct pcm_ops *ops;
+    void *snd_node, *data;
     int fd;
 
-    snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
-             flags & PCM_IN ? 'c' : 'p');
+    snd_node = snd_utils_get_dev_node(card, device, NODE_PCM);
+    pcm_type = snd_utils_get_node_type(snd_node);
+    if (pcm_type == SND_NODE_TYPE_PLUGIN)
+        ops = &plug_ops;
+    else
+        ops = &hw_ops;
 
-    fd = open(fn, O_RDWR);
+    fd = ops->open(card, device, flags, &data, snd_node);
     if (fd < 0) {
-        fprintf(stderr, "cannot open device '%s'\n", fn);
+        fprintf(stderr, "cannot open device %u for card %u\n",
+                device, card);
         goto err_open;
     }
 
@@ -612,20 +634,22 @@
         goto err_calloc;
 
     param_init(params);
-    if (ioctl(fd, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
+    if (ops->ioctl(data, SNDRV_PCM_IOCTL_HW_REFINE, params)) {
         fprintf(stderr, "SNDRV_PCM_IOCTL_HW_REFINE error (%d)\n", errno);
         goto err_hw_refine;
     }
 
-    close(fd);
+    snd_utils_put_dev_node(snd_node);
+    ops->close(data);
 
     return (struct pcm_params *)params;
 
 err_hw_refine:
     free(params);
 err_calloc:
-    close(fd);
+    ops->close(data);
 err_open:
+    snd_utils_put_dev_node(snd_node);
     return NULL;
 }
 
@@ -862,8 +886,9 @@
         munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
     }
 
-    if (pcm->fd >= 0)
-        close(pcm->fd);
+    pcm->ops->close(pcm->data);
+    snd_utils_put_dev_node(pcm->snd_node);
+
     pcm->prepared = 0;
     pcm->running = 0;
     pcm->buffer_size = 0;
@@ -879,8 +904,7 @@
     struct snd_pcm_info info;
     struct snd_pcm_hw_params params;
     struct snd_pcm_sw_params sparams;
-    char fn[256];
-    int rc;
+    int rc, pcm_type;
 
     if (!config) {
         return &bad_pcm; /* TODO: could support default config here */
@@ -890,24 +914,23 @@
         return &bad_pcm; /* TODO: could support default config here */
 
     pcm->config = *config;
-
-    snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
-             flags & PCM_IN ? 'c' : 'p');
-
     pcm->flags = flags;
-    pcm->fd = open(fn, O_RDWR|O_NONBLOCK);
+
+    pcm->snd_node = snd_utils_get_dev_node(card, device, NODE_PCM);
+    pcm_type = snd_utils_get_node_type(pcm->snd_node);
+    if (pcm_type == SND_NODE_TYPE_PLUGIN)
+        pcm->ops = &plug_ops;
+    else
+        pcm->ops = &hw_ops;
+
+    pcm->fd = pcm->ops->open(card, device, flags, &pcm->data, pcm->snd_node);
     if (pcm->fd < 0) {
-        oops(pcm, errno, "cannot open device '%s'", fn);
-        return pcm;
+        oops(pcm, errno, "cannot open device %u for card %u",
+             device, card);
+        goto fail_open;
     }
 
-    if (fcntl(pcm->fd, F_SETFL, fcntl(pcm->fd, F_GETFL) &
-              ~O_NONBLOCK) < 0) {
-        oops(pcm, errno, "failed to reset blocking mode '%s'", fn);
-        goto fail_close;
-    }
-
-    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_INFO, &info)) {
+    if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_INFO, &info)) {
         oops(pcm, errno, "cannot get info");
         goto fail_close;
     }
@@ -945,7 +968,7 @@
         param_set_mask(&params, SNDRV_PCM_HW_PARAM_ACCESS,
                        SNDRV_PCM_ACCESS_RW_INTERLEAVED);
 
-    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
+    if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {
         oops(pcm, errno, "cannot set hw params");
         goto fail_close;
     }
@@ -1006,7 +1029,7 @@
     while (pcm->boundary * 2 <= INT_MAX - pcm->buffer_size)
         pcm->boundary *= 2;
 
-    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
+    if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_SW_PARAMS, &sparams)) {
         oops(pcm, errno, "cannot set sw params");
         goto fail;
     }
@@ -1020,7 +1043,7 @@
 #ifdef SNDRV_PCM_IOCTL_TTSTAMP
     if (pcm->flags & PCM_MONOTONIC) {
         int arg = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
-        rc = ioctl(pcm->fd, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
+        rc = pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_TTSTAMP, &arg);
         if (rc < 0) {
             oops(pcm, errno, "cannot set timestamp type");
             goto fail;
@@ -1035,8 +1058,11 @@
     if (flags & PCM_MMAP)
         munmap(pcm->mmap_buffer, pcm_frames_to_bytes(pcm, pcm->buffer_size));
 fail_close:
-    close(pcm->fd);
+    pcm->ops->close(pcm->data);
     pcm->fd = -1;
+
+fail_open:
+    snd_utils_put_dev_node(pcm->snd_node);
     return pcm;
 }
 
@@ -1050,7 +1076,7 @@
     if (pcm->prepared)
         return 0;
 
-    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_PREPARE) < 0)
+    if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_PREPARE) < 0)
         return oops(pcm, errno, "cannot prepare channel");
 
     pcm->prepared = 1;
@@ -1064,9 +1090,9 @@
         return prepare_error;
 
     if (pcm->flags & PCM_MMAP)
-	    pcm_sync_ptr(pcm, 0);
+        pcm_sync_ptr(pcm, 0);
 
-    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_START) < 0)
+    if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_START) < 0)
         return oops(pcm, errno, "cannot start channel");
 
     pcm->running = 1;
@@ -1075,7 +1101,7 @@
 
 int pcm_stop(struct pcm *pcm)
 {
-    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_DROP) < 0)
+    if (pcm->ops->ioctl(pcm->data, SNDRV_PCM_IOCTL_DROP) < 0)
         return oops(pcm, errno, "cannot stop channel");
 
     pcm->prepared = 0;
@@ -1251,7 +1277,7 @@
         }
 
         /* start the audio if we reach the threshold */
-	    if (!pcm->running &&
+        if (!pcm->running &&
             (pcm->buffer_size - avail) >= pcm->config.start_threshold) {
             if (pcm_start(pcm) < 0) {
                fprintf(stderr, "start error: hw 0x%x app 0x%x avail 0x%x\n",
@@ -1347,5 +1373,5 @@
     arg = va_arg(ap, void *);
     va_end(ap);
 
-    return ioctl(pcm->fd, request, arg);
+    return pcm->ops->ioctl(pcm->data, request, arg);
 }
diff --git a/pcm_hw.c b/pcm_hw.c
new file mode 100644
index 0000000..8fd78fb
--- /dev/null
+++ b/pcm_hw.c
@@ -0,0 +1,123 @@
+/* pcm_hw.c
+**
+** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted 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 The Linux Foundation nor the names of its
+**     contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
+**/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <poll.h>
+
+#include <sys/ioctl.h>
+#include <linux/ioctl.h>
+#include <sound/asound.h>
+#include <tinyalsa/asoundlib.h>
+
+#include "pcm_io.h"
+
+struct pcm_hw_data {
+    unsigned int card;
+    unsigned int device;
+    unsigned int fd;
+    void *snd_node;
+};
+
+static void pcm_hw_close(void *data)
+{
+    struct pcm_hw_data *hw_data = data;
+
+    if (hw_data->fd >= 0)
+        close(hw_data->fd);
+
+    free(hw_data);
+}
+
+static int pcm_hw_ioctl(void *data, unsigned int cmd, ...)
+{
+    struct pcm_hw_data *hw_data = data;
+    va_list ap;
+    void *arg;
+
+    va_start(ap, cmd);
+    arg = va_arg(ap, void *);
+    va_end(ap);
+
+    return ioctl(hw_data->fd, cmd, arg);
+}
+
+static int pcm_hw_open(unsigned int card, unsigned int device,
+                unsigned int flags, void **data,
+                __attribute__((unused)) void *node)
+{
+    struct pcm_hw_data *hw_data;
+    char fn[256];
+    int fd;
+
+    hw_data = calloc(1, sizeof(*hw_data));
+    if (!hw_data) {
+        return -ENOMEM;
+    }
+
+    snprintf(fn, sizeof(fn), "/dev/snd/pcmC%uD%u%c", card, device,
+             flags & PCM_IN ? 'c' : 'p');
+    fd = open(fn, O_RDWR|O_NONBLOCK);
+    if (fd < 0) {
+        printf("%s: cannot open device '%s'", __func__, fn);
+        return fd;
+    }
+
+    if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK) < 0) {
+        printf("%s: failed to reset blocking mode '%s'",
+                __func__, fn);
+        goto err_close;
+    }
+
+    hw_data->snd_node = node;
+    hw_data->card = card;
+    hw_data->device = device;
+    hw_data->fd = fd;
+
+    *data = hw_data;
+
+    return fd;
+
+err_close:
+    close(fd);
+    free(hw_data);
+    return -ENODEV;
+}
+
+struct pcm_ops hw_ops = {
+    .open = pcm_hw_open,
+    .close = pcm_hw_close,
+    .ioctl = pcm_hw_ioctl,
+};
diff --git a/pcm_io.h b/pcm_io.h
new file mode 100644
index 0000000..2271d76
--- /dev/null
+++ b/pcm_io.h
@@ -0,0 +1,41 @@
+/* pcm.h
+**
+** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted 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 The Linux Foundation nor the names of its
+**     contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
+**/
+
+#ifndef __PCM_H__
+#define __PCM_H__
+
+struct pcm_ops {
+    int (*open) (unsigned int card, unsigned int device,
+                 unsigned int flags, void **data, void *node);
+    void (*close) (void *data);
+    int (*ioctl) (void *data, unsigned int cmd, ...);
+};
+
+#endif /* end of __PCM_H__ */
diff --git a/pcm_plugin.c b/pcm_plugin.c
new file mode 100644
index 0000000..5daf979
--- /dev/null
+++ b/pcm_plugin.c
@@ -0,0 +1,740 @@
+/* pcm_plugin.c
+**
+** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted 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 The Linux Foundation nor the names of its
+**     contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
+**/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <poll.h>
+#include <dlfcn.h>
+
+#include <sys/ioctl.h>
+#include <linux/ioctl.h>
+#include <sound/asound.h>
+#include <tinyalsa/asoundlib.h>
+#include <tinyalsa/pcm_plugin.h>
+
+#include "pcm_io.h"
+#include "snd_utils.h"
+
+/* 2 words of uint32_t = 64 bits of mask */
+#define PCM_MASK_SIZE (2)
+#define ARRAY_SIZE(a)         \
+    (sizeof(a) / sizeof(a[0]))
+
+#define PCM_PARAM_GET_MASK(p, n)    \
+    &p->masks[n - SNDRV_PCM_HW_PARAM_FIRST_MASK];
+
+enum {
+    PCM_PLUG_HW_PARAM_SELECT_MIN,
+    PCM_PLUG_HW_PARAM_SELECT_MAX,
+    PCM_PLUG_HW_PARAM_SELECT_VAL,
+};
+
+enum {
+    PCM_PLUG_STATE_OPEN,
+    PCM_PLUG_STATE_SETUP,
+    PCM_PLUG_STATE_PREPARED,
+    PCM_PLUG_STATE_RUNNING,
+};
+
+struct pcm_plug_data {
+    unsigned int card;
+    unsigned int device;
+    unsigned int fd;
+    unsigned int flags;
+
+    void *dl_hdl;
+    PCM_PLUGIN_OPEN_FN_PTR();
+
+    struct pcm_plugin *plugin;
+    void *dev_node;
+};
+
+static unsigned int my_params[] = {
+    SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
+    SNDRV_PCM_HW_PARAM_CHANNELS,
+    SNDRV_PCM_HW_PARAM_RATE,
+    SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+    SNDRV_PCM_HW_PARAM_PERIODS,
+};
+
+static void pcm_plug_close(void *data)
+{
+    struct pcm_plug_data *plug_data = data;
+    struct pcm_plugin *plugin = plug_data->plugin;
+
+    plugin->ops->close(plugin);
+    dlclose(plug_data->dl_hdl);
+
+    free(plug_data);
+}
+
+static int pcm_plug_info(struct pcm_plug_data *plug_data,
+                struct snd_pcm_info *info)
+{
+    int stream = SNDRV_PCM_STREAM_PLAYBACK;
+    int ret = 0, val = -1;
+    char *name;
+
+    memset(info, 0, sizeof(*info));
+
+    if (plug_data->flags & PCM_IN) {
+        stream = SNDRV_PCM_STREAM_CAPTURE;
+        ret = snd_utils_get_int(plug_data->dev_node, "capture", &val);
+        if (ret || !val) {
+            fprintf(stderr, "%s: not a capture device\n", __func__);
+            return -EINVAL;
+        }
+    } else {
+        stream = SNDRV_PCM_STREAM_PLAYBACK;
+        ret = snd_utils_get_int(plug_data->dev_node, "playback", &val);
+        if (ret || !val) {
+            fprintf(stderr, "%s: not a playback device\n", __func__);
+            return -EINVAL;
+        }
+    }
+
+    info->stream = stream;
+    info->card = plug_data->card;
+    info->device = plug_data->device;
+
+    ret = snd_utils_get_str(plug_data->dev_node, "name", &name);
+    if (ret) {
+        fprintf(stderr, "%s: failed to get pcm device name\n", __func__);
+        return ret;
+    }
+
+    strncpy((char *)info->id, name, sizeof(info->id));
+    strncpy((char *)info->name, name, sizeof(info->name));
+    strncpy((char *)info->subname, name, sizeof(info->subname));
+
+    info->subdevices_count = 1;
+
+    return ret;
+}
+
+static void pcm_plug_set_mask(struct snd_pcm_hw_params *p, int n, uint64_t v)
+{
+    struct snd_mask *mask;
+
+    mask = PCM_PARAM_GET_MASK(p, n);
+
+    mask->bits[0] |= (v & 0xFFFFFFFF);
+    mask->bits[1] |= ((v >> 32) & 0xFFFFFFFF);
+    /*
+     * currently only supporting 64 bits, may need to update to support
+     * more than 64 bits
+     */
+}
+
+static void pcm_plug_set_interval(struct snd_pcm_hw_params *params,
+                    int p, struct pcm_plugin_min_max *v, int is_integer)
+{
+    struct snd_interval *i;
+
+    i = &params->intervals[p - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
+
+    i->min = v->min;
+    i->max = v->max;
+
+    if (is_integer)
+        i->integer = 1;
+}
+
+static int pcm_plug_frames_to_bytes(unsigned int frames,
+                                    unsigned int frame_bits)
+{
+    return (frames * (frame_bits / 8));
+}
+
+static int pcm_plug_bytes_to_frames(unsigned int size,
+                                    unsigned int frame_bits)
+{
+    return (size * 8)  / frame_bits;
+}
+
+static int pcm_plug_get_params(struct pcm_plugin *plugin,
+                struct snd_pcm_hw_params *params)
+{
+    struct pcm_plugin_min_max bw, ch, pb, periods;
+    struct pcm_plugin_min_max val;
+    struct pcm_plugin_min_max frame_bits, buffer_bytes;
+
+    /*
+     * populate the struct snd_pcm_hw_params structure
+     * using the hw_param constraints provided by plugin
+     * via the plugin->constraints
+     */
+
+    /* Set the mask params */
+    pcm_plug_set_mask(params, SNDRV_PCM_HW_PARAM_ACCESS,
+                      plugin->constraints->access);
+    pcm_plug_set_mask(params, SNDRV_PCM_HW_PARAM_FORMAT,
+                      plugin->constraints->format);
+    pcm_plug_set_mask(params, SNDRV_PCM_HW_PARAM_SUBFORMAT,
+                      SNDRV_PCM_SUBFORMAT_STD);
+
+    /* Set the standard interval params */
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
+                          &plugin->constraints->bit_width, 1);
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS,
+                          &plugin->constraints->channels, 1);
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_RATE,
+                          &plugin->constraints->rate, 1);
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
+                          &plugin->constraints->period_bytes, 0);
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_PERIODS,
+                          &plugin->constraints->periods, 1);
+
+    /* set the calculated interval params */
+
+    bw.min = plugin->constraints->bit_width.min;
+    bw.max = plugin->constraints->bit_width.max;
+
+    ch.min = plugin->constraints->channels.min;
+    ch.max = plugin->constraints->channels.max;
+
+    pb.min = plugin->constraints->period_bytes.min;
+    pb.max = plugin->constraints->period_bytes.max;
+
+    periods.min = plugin->constraints->periods.min;
+    periods.max = plugin->constraints->periods.max;
+
+    /* Calculate and set frame bits */
+    frame_bits.min = bw.min * ch.min;
+    frame_bits.max = bw.max * ch.max;
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_FRAME_BITS,
+                          &frame_bits, 1);
+
+
+    /* Calculate and set period_size in frames */
+    val.min = pcm_plug_bytes_to_frames(pb.min, frame_bits.min);
+    val.max = pcm_plug_bytes_to_frames(pb.max, frame_bits.min);
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
+                          &val, 1);
+
+    /* Calculate and set buffer_bytes */
+    buffer_bytes.min = pb.min * periods.min;
+    buffer_bytes.max = pb.max * periods.max;
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
+                          &buffer_bytes, 1);
+
+    /* Calculate and set buffer_size in frames */
+    val.min = pcm_plug_bytes_to_frames(buffer_bytes.min, frame_bits.min);
+    val.max = pcm_plug_bytes_to_frames(buffer_bytes.max, frame_bits.min);
+    pcm_plug_set_interval(params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
+                          &val, 1);
+    return 0;
+}
+
+static int pcm_plug_masks_refine(struct snd_pcm_hw_params *p,
+                struct snd_pcm_hw_params *c)
+{
+    struct snd_mask *req_mask;
+    struct snd_mask *con_mask;
+    unsigned int idx, i, masks;
+
+    masks = SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK;
+
+    for (idx = 0; idx <= masks; idx++) {
+
+        if (!(p->rmask & (1 << (idx + SNDRV_PCM_HW_PARAM_FIRST_MASK))))
+            continue;
+
+        req_mask = PCM_PARAM_GET_MASK(p, idx);
+        con_mask = PCM_PARAM_GET_MASK(c, idx);
+
+        /*
+         * set the changed mask if requested mask value is not the same as
+         * constrained mask value
+         */
+        if (memcmp(req_mask, con_mask, PCM_MASK_SIZE * sizeof(uint32_t)))
+            p->cmask |= 1 << (idx + SNDRV_PCM_HW_PARAM_FIRST_MASK);
+
+        /* Actually change the requested mask to constrained mask */
+        for (i = 0; i < PCM_MASK_SIZE; i++)
+            req_mask->bits[i] &= con_mask->bits[i];
+    }
+
+    return 0;
+}
+
+static int pcm_plug_interval_refine(struct snd_pcm_hw_params *p,
+                struct snd_pcm_hw_params *c)
+{
+    struct snd_interval *ri;
+    struct snd_interval *ci;
+    unsigned int idx;
+    unsigned int intervals;
+    int changed = 0;
+
+    intervals = SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
+                SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
+
+    for (idx = 0; idx <= intervals; idx++) {
+        ri = &p->intervals[idx];
+        ci = &c->intervals[idx];
+
+        if (!(p->rmask & (1 << (idx + SNDRV_PCM_HW_PARAM_FIRST_INTERVAL)) ))
+            continue;
+
+        if (ri->min < ci->min) {
+            ri->min = ci->min;
+            ri->openmin = ci->openmin;
+            changed = 1;
+        } else if (ri->min == ci->min && !ri->openmin && ci->openmin) {
+            ri->openmin = 1;
+            changed = 1;
+        }
+
+        if (ri->max > ci->max) {
+            ri->max = ci->max;
+            ri->openmax = ci->openmax;
+            changed = 1;
+        } else if (ri->max == ci->max && !ri->openmax && ci->openmax) {
+            ri->openmax = 1;
+            changed = 1;
+        };
+
+        if (!ri->integer && ci->integer) {
+            ri->integer = 1;
+            changed = 1;
+        }
+
+        if (ri->integer) {
+            if (ri->openmin) {
+                ri->min++;
+                ri->openmin = 0;
+            }
+            if (ri->openmax) {
+                ri->max--;
+                ri->openmax = 0;
+            }
+        } else if (!ri->openmin && !ri->openmax && ri->min == ri->max) {
+            ri->integer = 1;
+        }
+
+        /* Set the changed mask */
+        if (changed)
+            p->cmask |= (1 << (idx + SNDRV_PCM_HW_PARAM_FIRST_INTERVAL));
+    }
+
+    return 0;
+}
+
+
+static int pcm_plug_hw_params_refine(struct snd_pcm_hw_params *p,
+                struct snd_pcm_hw_params *c)
+{
+    int rc;
+
+    rc = pcm_plug_masks_refine(p, c);
+    if (rc) {
+        fprintf(stderr, "%s: masks refine failed %d\n", __func__, rc);
+        return rc;
+    }
+
+    rc = pcm_plug_interval_refine(p, c);
+    if (rc) {
+        fprintf(stderr, "%s: interval refine failed %d\n", __func__, rc);
+        return rc;
+    }
+
+    /* clear the requested params */
+    p->rmask = 0;
+
+    return rc;
+}
+
+static int __pcm_plug_hrefine(struct pcm_plug_data *plug_data,
+                struct snd_pcm_hw_params *params)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+    struct snd_pcm_hw_params plug_params;
+    int rc;
+
+    memset(&plug_params, 0, sizeof(plug_params));
+    rc = pcm_plug_get_params(plugin, &plug_params);
+    if (rc) {
+        fprintf(stderr, "%s: pcm_plug_get_params failed %d\n",
+               __func__, rc);
+        return -EINVAL;
+    }
+
+    return pcm_plug_hw_params_refine(params, &plug_params);
+
+}
+
+static int pcm_plug_hrefine(struct pcm_plug_data *plug_data,
+                struct snd_pcm_hw_params *params)
+{
+    return __pcm_plug_hrefine(plug_data, params);
+}
+
+static int pcm_plug_interval_select(struct snd_pcm_hw_params *p,
+        unsigned int param, unsigned int select, unsigned int val)
+{
+    struct snd_interval *i;
+
+    if (param < SNDRV_PCM_HW_PARAM_FIRST_INTERVAL ||
+        param > SNDRV_PCM_HW_PARAM_LAST_INTERVAL)
+        return -EINVAL;
+
+    i = &p->intervals[param - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
+
+    if (!i->min)
+        return -EINVAL;
+
+    switch (select) {
+
+    case PCM_PLUG_HW_PARAM_SELECT_MIN:
+        i->max = i->min;
+        break;
+
+    case PCM_PLUG_HW_PARAM_SELECT_MAX:
+        i->min = i->max;
+        break;
+
+    case PCM_PLUG_HW_PARAM_SELECT_VAL:
+        i->min = i->max = val;
+        break;
+
+    default:
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static void pcm_plug_hw_params_set(struct snd_pcm_hw_params *p)
+{
+    unsigned int i, select;
+    unsigned int bw, ch, period_sz, periods;
+    unsigned int val1, val2, offset;
+
+    offset = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL;
+
+    /* Select the min values first */
+    select = PCM_PLUG_HW_PARAM_SELECT_MIN;
+    for (i = 0; i < ARRAY_SIZE(my_params); i++)
+        pcm_plug_interval_select(p, my_params[i], select, 0);
+
+    /* Select calculated values */
+    select = PCM_PLUG_HW_PARAM_SELECT_VAL;
+    bw = (p->intervals[SNDRV_PCM_HW_PARAM_SAMPLE_BITS - offset]).min;
+    ch = (p->intervals[SNDRV_PCM_HW_PARAM_CHANNELS - offset]).min;
+    period_sz = (p->intervals[SNDRV_PCM_HW_PARAM_PERIOD_SIZE - offset]).min;
+    periods = (p->intervals[SNDRV_PCM_HW_PARAM_PERIODS - offset]).min;
+
+    val1 = bw * ch;        // frame_bits;
+    pcm_plug_interval_select(p, SNDRV_PCM_HW_PARAM_FRAME_BITS, select, val1);
+
+    val2 = pcm_plug_frames_to_bytes(period_sz, val1); // period_bytes;
+    pcm_plug_interval_select(p, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, select,
+                             val2);
+
+    val2 = period_sz * periods; //buffer_size;
+    pcm_plug_interval_select(p, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, select, val2);
+
+    val2 = pcm_plug_frames_to_bytes(period_sz * periods, val1); //buffer_bytes;
+    pcm_plug_interval_select(p, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, select, val2);
+}
+
+static int pcm_plug_hparams(struct pcm_plug_data *plug_data,
+                struct snd_pcm_hw_params *params)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+    int rc;
+
+    if (plugin->state != PCM_PLUG_STATE_OPEN)
+            return -EBADFD;
+
+    params->rmask = ~0U;
+
+    rc = __pcm_plug_hrefine(plug_data, params);
+    if (rc) {
+        fprintf(stderr, "%s: __pcm_plug_hrefine failed %d\n",
+               __func__, rc);
+        return rc;
+    }
+
+    pcm_plug_hw_params_set(params);
+
+    rc = plugin->ops->hw_params(plugin, params);
+    if (!rc)
+        plugin->state = PCM_PLUG_STATE_SETUP;
+
+    return rc;
+}
+
+static int pcm_plug_sparams(struct pcm_plug_data *plug_data,
+                struct snd_pcm_sw_params *params)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+
+    if (plugin->state != PCM_PLUG_STATE_SETUP)
+        return -EBADFD;
+
+    return plugin->ops->sw_params(plugin, params);
+}
+
+static int pcm_plug_sync_ptr(struct pcm_plug_data *plug_data,
+                struct snd_pcm_sync_ptr *sync_ptr)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+
+    return plugin->ops->sync_ptr(plugin, sync_ptr);
+}
+
+static int pcm_plug_writei_frames(struct pcm_plug_data *plug_data,
+                struct snd_xferi *x)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+
+    if (plugin->state != PCM_PLUG_STATE_PREPARED &&
+        plugin->state != PCM_PLUG_STATE_RUNNING)
+        return -EBADFD;
+
+    return plugin->ops->writei_frames(plugin, x);
+}
+
+static int pcm_plug_readi_frames(struct pcm_plug_data *plug_data,
+                struct snd_xferi *x)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+
+    if (plugin->state != PCM_PLUG_STATE_RUNNING)
+        return -EBADFD;
+
+    return plugin->ops->readi_frames(plugin, x);
+}
+
+static int pcm_plug_ttstamp(struct pcm_plug_data *plug_data,
+                int *tstamp)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+
+    if (plugin->state != PCM_PLUG_STATE_RUNNING)
+        return -EBADFD;
+
+    return plugin->ops->ttstamp(plugin, tstamp);
+}
+
+static int pcm_plug_prepare(struct pcm_plug_data *plug_data)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+    int rc;
+
+    if (plugin->state != PCM_PLUG_STATE_SETUP)
+        return -EBADFD;
+
+    rc = plugin->ops->prepare(plugin);
+    if (!rc)
+        plugin->state = PCM_PLUG_STATE_PREPARED;
+
+    return rc;
+}
+
+static int pcm_plug_start(struct pcm_plug_data *plug_data)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+    int rc;
+
+    if (plugin->state != PCM_PLUG_STATE_PREPARED)
+        return -EBADFD;
+
+    rc = plugin->ops->start(plugin);
+    if (!rc)
+        plugin->state = PCM_PLUG_STATE_RUNNING;
+
+    return rc;
+}
+
+static int pcm_plug_drop(struct pcm_plug_data *plug_data)
+{
+    struct pcm_plugin *plugin = plug_data->plugin;
+    int rc = 0;
+
+    rc = plugin->ops->drop(plugin);
+    if (!rc)
+        plugin->state = PCM_PLUG_STATE_SETUP;
+
+    return rc;
+}
+
+static int pcm_plug_ioctl(void *data, unsigned int cmd, ...)
+{
+    struct pcm_plug_data *plug_data = data;
+    struct pcm_plugin *plugin = plug_data->plugin;
+    int ret;
+    va_list ap;
+    void *arg;
+
+    va_start(ap, cmd);
+    arg = va_arg(ap, void *);
+    va_end(ap);
+
+    switch (cmd) {
+    case SNDRV_PCM_IOCTL_INFO:
+        ret = pcm_plug_info(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_TTSTAMP:
+        ret = pcm_plug_ttstamp(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_HW_REFINE:
+        ret = pcm_plug_hrefine(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_HW_PARAMS:
+        ret = pcm_plug_hparams(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_SW_PARAMS:
+        ret = pcm_plug_sparams(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_SYNC_PTR:
+        ret = pcm_plug_sync_ptr(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_PREPARE:
+        ret = pcm_plug_prepare(plug_data);
+        break;
+    case SNDRV_PCM_IOCTL_START:
+        ret = pcm_plug_start(plug_data);
+        break;
+    case SNDRV_PCM_IOCTL_DROP:
+        ret = pcm_plug_drop(plug_data);
+        break;
+    case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
+        ret = pcm_plug_writei_frames(plug_data, arg);
+        break;
+    case SNDRV_PCM_IOCTL_READI_FRAMES:
+        ret = pcm_plug_readi_frames(plug_data, arg);
+        break;
+    default:
+        ret = plugin->ops->ioctl(plugin, cmd, arg);
+        break;
+    }
+
+    return ret;
+}
+
+static int pcm_plug_open(unsigned int card, unsigned int device,
+                  unsigned int flags, void **data, void *pcm_node)
+{
+    struct pcm_plug_data *plug_data;
+    const char *err = NULL;
+    void *dl_hdl;
+    int rc = 0;
+    char *so_name, token[80], *name, *open_fn;
+
+    plug_data = calloc(1, sizeof(*plug_data));
+    if (!plug_data) {
+        return -ENOMEM;
+    }
+
+    rc = snd_utils_get_str(pcm_node, "so-name", &so_name);
+    if (rc) {
+        fprintf(stderr, "%s: failed to get plugin lib name\n", __func__);
+        goto err_get_lib;
+    }
+
+    dl_hdl = dlopen(so_name, RTLD_NOW);
+    if (!dl_hdl) {
+        fprintf(stderr, "%s: unable to open %s\n", __func__, so_name);
+        goto err_dl_open;
+    } else {
+        fprintf(stderr, "%s: dlopen successful for %s\n", __func__, so_name);
+    }
+
+    dlerror();
+
+    sscanf(so_name, "lib%s", token);
+    name = strtok(token, ".");
+
+    open_fn = calloc(1, strlen(name) + strlen("_open") + 1);
+    if (!open_fn) {
+        rc = -ENOMEM;
+        goto err_open_fn;
+    }
+
+    strncpy(open_fn, name, strlen(name) + 1);
+    strcat(open_fn, "_open");
+
+    printf("%s - %s\n", __func__, open_fn);
+    plug_data->plugin_open_fn = dlsym(dl_hdl, open_fn);
+    err = dlerror();
+
+    if (err) {
+        fprintf(stderr, "%s: dlsym to open fn failed, err = '%s'\n",
+                __func__, err);
+        goto err_dlsym;
+    }
+
+    rc = plug_data->plugin_open_fn(&plug_data->plugin,
+                    card, device, flags);
+    if (rc) {
+        fprintf(stderr, "%s: failed to open plugin\n", __func__);
+        goto err_dlsym;
+    }
+
+    /* Call snd-card-def to get card and pcm nodes */
+    /* Check how to manage fd for plugin */
+
+    plug_data->dl_hdl = dl_hdl;
+    plug_data->card = card;
+    plug_data->device = device;
+    plug_data->dev_node = pcm_node;
+    plug_data->flags = flags;
+
+    *data = plug_data;
+
+    plug_data->plugin->state = PCM_PLUG_STATE_OPEN;
+
+    return 0;
+
+err_dlsym:
+    free(open_fn);
+err_open_fn:
+    dlclose(dl_hdl);
+err_get_lib:
+err_dl_open:
+    free(plug_data);
+
+    return rc;
+}
+
+struct pcm_ops plug_ops = {
+    .open = pcm_plug_open,
+    .close = pcm_plug_close,
+    .ioctl = pcm_plug_ioctl,
+};
diff --git a/snd_utils.c b/snd_utils.c
new file mode 100644
index 0000000..ac3ddf9
--- /dev/null
+++ b/snd_utils.c
@@ -0,0 +1,149 @@
+/* snd_utils.c
+**
+** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted 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 The Linux Foundation nor the names of its
+**     contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
+**/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "snd_utils.h"
+
+#define SND_DLSYM(h, p, s, err) \
+do {                            \
+    err = 0;                    \
+    p = dlsym(h, s);            \
+    if (!p)                        \
+        err = -ENODEV;            \
+} while(0)
+
+int snd_utils_get_int(struct snd_node *node, const char *prop, int *val)
+{
+    if (!node || !node->card_node || !node->dev_node)
+        return SND_NODE_TYPE_HW;
+
+    return node->get_int(node->dev_node, prop, val);
+}
+
+int snd_utils_get_str(struct snd_node *node, const char *prop, char **val)
+{
+    if (!node || !node->card_node || !node->dev_node)
+        return SND_NODE_TYPE_HW;
+
+    return node->get_str(node->dev_node, prop, val);
+}
+
+void snd_utils_put_dev_node(struct snd_node *node)
+{
+    if (!node)
+        return;
+
+    if (node->card_node)
+        node->put_card(node->card_node);
+
+    if (node->dl_hdl)
+        dlclose(node->dl_hdl);
+
+    free(node);
+}
+
+enum snd_node_type snd_utils_get_node_type(struct snd_node *node)
+{
+    int val = SND_NODE_TYPE_HW;
+
+    if (!node || !node->card_node || !node->dev_node)
+        return SND_NODE_TYPE_HW;
+
+    node->get_int(node->dev_node, "type", &val);
+
+    return val;
+};
+
+
+static int snd_utils_resolve_symbols(struct snd_node *node)
+{
+    void *dl = node->dl_hdl;
+    int err;
+
+    SND_DLSYM(dl, node->get_card, "snd_card_def_get_card", err);
+    if (err)
+        goto done;
+    SND_DLSYM(dl, node->put_card, "snd_card_def_put_card", err);
+    if (err)
+        goto done;
+    SND_DLSYM(dl, node->get_node, "snd_card_def_get_node", err);
+    if (err)
+        goto done;
+    SND_DLSYM(dl, node->get_int, "snd_card_def_get_int", err);
+    if (err)
+        goto done;
+    SND_DLSYM(dl, node->get_str, "snd_card_def_get_str", err);
+
+done:
+    return err;
+}
+
+struct snd_node *snd_utils_get_dev_node(unsigned int card,
+        unsigned int device, int dev_type)
+{
+    struct snd_node *node;
+    int rc = 0;
+
+    node = calloc(1, sizeof(*node));
+    if (!node)
+        return NULL;
+
+    node->dl_hdl = dlopen("libsndcardparser.so", RTLD_NOW);
+    if (!node->dl_hdl) {
+        goto err_dl_open;
+    }
+
+    rc = snd_utils_resolve_symbols(node);
+    if (rc < 0)
+        goto err_resolve_symbols;
+
+    node->card_node = node->get_card(card);
+    if (!node->card_node)
+        goto err_resolve_symbols;
+
+    node->dev_node = node->get_node(node->card_node,
+                                    device, dev_type);
+    if (!node->dev_node)
+        goto err_get_node;
+
+    return node;
+
+err_get_node:
+    node->put_card(node->card_node);
+
+err_resolve_symbols:
+    dlclose(node->dl_hdl);
+
+err_dl_open:
+    free(node);
+    return NULL;
+}
diff --git a/snd_utils.h b/snd_utils.h
new file mode 100644
index 0000000..64bfeb8
--- /dev/null
+++ b/snd_utils.h
@@ -0,0 +1,71 @@
+/* snd_utils.h
+**
+** Copyright (c) 2019, The Linux Foundation. All rights reserved.
+**
+** Redistribution and use in source and binary forms, with or without
+** modification, are permitted 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 The Linux Foundation nor the names of its
+**     contributors may be used to endorse or promote products derived
+**     from this software without specific prior written permission.
+**
+** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER 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.
+**/
+
+#ifndef __SND_CARD_UTILS_H__
+#define __SND_CARD_UTILS_H__
+
+#include <dlfcn.h>
+
+struct snd_node {
+    void *card_node;
+    void *dev_node;
+    void *dl_hdl;
+
+    void* (*get_card) (unsigned int card);
+    void (*put_card) (void *card);
+    void* (*get_node) (void *card, unsigned int id,
+                      int type);
+    int (*get_int) (void *node, const char *prop, int *val);
+    int (*get_str) (void *node, const char *prop, char **val);
+};
+
+enum {
+    NODE_PCM,
+    NODE_MIXER,
+};
+
+enum snd_node_type {
+    SND_NODE_TYPE_HW = 0,
+    SND_NODE_TYPE_PLUGIN,
+    SND_NODE_TYPE_INVALID,
+};
+
+struct snd_node *snd_utils_get_dev_node(unsigned int card,
+        unsigned int device, int dev_type);
+
+void snd_utils_put_dev_node(struct snd_node *node);
+
+enum snd_node_type snd_utils_get_node_type(struct snd_node *node);
+
+int snd_utils_get_int(struct snd_node *node, const char *prop, int *val);
+
+int snd_utils_get_str(struct snd_node *node, const char *prop, char **val);
+
+#endif /* end of __SND_CARD_UTILS_H__ */