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