1 /* snd_card_plugin.c
2 ** Copyright (c) 2019, The Linux Foundation.
3 **
4 ** Redistribution and use in source and binary forms, with or without
5 ** modification, are permitted provided that the following conditions are
6 ** met:
7 **   * Redistributions of source code must retain the above copyright
8 **     notice, this list of conditions and the following disclaimer.
9 **   * Redistributions in binary form must reproduce the above
10 **     copyright notice, this list of conditions and the following
11 **     disclaimer in the documentation and/or other materials provided
12 **     with the distribution.
13 **   * Neither the name of The Linux Foundation nor the names of its
14 **     contributors may be used to endorse or promote products derived
15 **     from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 ** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 ** BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 ** OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 ** IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **/
29 
30 #include "snd_card_plugin.h"
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 
36 #define SND_DLSYM(h, p, s, err) \
37 do {                            \
38     err = 0;                    \
39     p = dlsym(h, s);            \
40     if (!p)                        \
41         err = -ENODEV;            \
42 } while(0)
43 
snd_utils_get_int(struct snd_node * node,const char * prop,int * val)44 int snd_utils_get_int(struct snd_node *node, const char *prop, int *val)
45 {
46     if (!node || !node->card_node || !node->dev_node)
47         return SND_NODE_TYPE_HW;
48 
49     return node->ops->get_int(node->dev_node, prop, val);
50 }
51 
snd_utils_get_str(struct snd_node * node,const char * prop,char ** val)52 int snd_utils_get_str(struct snd_node *node, const char *prop, char **val)
53 {
54     if (!node || !node->card_node || !node->dev_node)
55         return SND_NODE_TYPE_HW;
56 
57     return node->ops->get_str(node->dev_node, prop, val);
58 }
59 
snd_utils_close_dev_node(struct snd_node * node)60 void snd_utils_close_dev_node(struct snd_node *node)
61 {
62     if (!node)
63         return;
64 
65     if (node->card_node)
66         node->ops->close_card(node->card_node);
67 
68     if (node->dl_hdl)
69         dlclose(node->dl_hdl);
70 
71     free(node);
72 }
73 
snd_utils_get_node_type(struct snd_node * node)74 enum snd_node_type snd_utils_get_node_type(struct snd_node *node)
75 {
76     int val = SND_NODE_TYPE_HW;
77 
78     if (!node || !node->card_node || !node->dev_node)
79         return SND_NODE_TYPE_HW;
80 
81     node->ops->get_int(node->dev_node, "type", &val);
82 
83     return val;
84 }
85 
snd_utils_resolve_symbols(struct snd_node * node)86 static int snd_utils_resolve_symbols(struct snd_node *node)
87 {
88     void *dl = node->dl_hdl;
89     int err;
90     SND_DLSYM(dl, node->ops, "snd_card_ops", err);
91     return err;
92 }
93 
snd_utils_open_dev_node(unsigned int card,unsigned int device,int dev_type)94 static struct snd_node *snd_utils_open_dev_node(unsigned int card,
95                                                 unsigned int device,
96                                                 int dev_type)
97 {
98     struct snd_node *node;
99     int rc = 0;
100 
101     node = calloc(1, sizeof(*node));
102     if (!node)
103         return NULL;
104 
105     node->dl_hdl = dlopen("libsndcardparser.so", RTLD_NOW);
106     if (!node->dl_hdl) {
107         goto err_dl_open;
108     }
109 
110     rc = snd_utils_resolve_symbols(node);
111     if (rc < 0)
112         goto err_resolve_symbols;
113 
114     node->card_node = node->ops->open_card(card);
115     if (!node->card_node)
116         goto err_resolve_symbols;
117 
118     if (dev_type == NODE_PCM) {
119       node->dev_node = node->ops->get_pcm(node->card_node, device);
120     } else {
121       node->dev_node = node->ops->get_mixer(node->card_node);
122     }
123 
124     if (!node->dev_node)
125         goto err_get_node;
126 
127     return node;
128 
129 err_get_node:
130     node->ops->close_card(node->card_node);
131 
132 err_resolve_symbols:
133     dlclose(node->dl_hdl);
134 
135 err_dl_open:
136     free(node);
137     return NULL;
138 }
139 
snd_utils_open_pcm(unsigned int card,unsigned int device)140 struct snd_node* snd_utils_open_pcm(unsigned int card,
141                                     unsigned int device)
142 {
143   return snd_utils_open_dev_node(card, device, NODE_PCM);
144 }
145 
snd_utils_open_mixer(unsigned int card)146 struct snd_node* snd_utils_open_mixer(unsigned int card)
147 {
148   return snd_utils_open_dev_node(card, 0, NODE_MIXER);
149 }
150