blob: ac294ca27253cc00daf240ae01a19488bf44be3f [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* mixer.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are 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 copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
Ben Zhang7ed2ffb2016-04-22 17:59:40 -070031#include <stdint.h>
Simon Wilson79d39652011-05-25 13:44:23 -070032#include <string.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <ctype.h>
Richard Fitzgerald57a87742014-09-09 16:54:12 +010037#include <limits.h>
Dima Krasner696c4482016-03-05 19:50:02 +020038#include <time.h>
Simon Wilson79d39652011-05-25 13:44:23 -070039
Simon Wilson6a52f2c2012-05-04 16:32:10 -070040#include <sys/ioctl.h>
41
Simon Wilson79d39652011-05-25 13:44:23 -070042#include <linux/ioctl.h>
43#define __force
44#define __bitwise
45#define __user
46#include <sound/asound.h>
47
48#include <tinyalsa/asoundlib.h>
49
Simon Wilson79d39652011-05-25 13:44:23 -070050struct mixer_ctl {
51 struct mixer *mixer;
Richard Fitzgerald899cece2014-09-09 17:03:21 +010052 struct snd_ctl_elem_info info;
Simon Wilson79d39652011-05-25 13:44:23 -070053 char **ename;
54};
55
56struct mixer {
57 int fd;
Simon Wilsonec281392013-06-28 16:21:31 -070058 struct snd_ctl_card_info card_info;
Simon Wilson79d39652011-05-25 13:44:23 -070059 struct mixer_ctl *ctl;
60 unsigned int count;
61};
62
63void mixer_close(struct mixer *mixer)
64{
65 unsigned int n,m;
66
Simon Wilson193b1c32011-06-07 23:42:38 -070067 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070068 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070069
Simon Wilson79d39652011-05-25 13:44:23 -070070 if (mixer->fd >= 0)
71 close(mixer->fd);
72
73 if (mixer->ctl) {
74 for (n = 0; n < mixer->count; n++) {
75 if (mixer->ctl[n].ename) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +010076 unsigned int max = mixer->ctl[n].info.value.enumerated.items;
Simon Wilson79d39652011-05-25 13:44:23 -070077 for (m = 0; m < max; m++)
78 free(mixer->ctl[n].ename[m]);
79 free(mixer->ctl[n].ename);
80 }
81 }
82 free(mixer->ctl);
83 }
84
Simon Wilson79d39652011-05-25 13:44:23 -070085 free(mixer);
86
87 /* TODO: verify frees */
88}
89
Simon Wilson1bd580f2011-06-02 15:58:41 -070090struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -070091{
92 struct snd_ctl_elem_list elist;
93 struct snd_ctl_elem_info tmp;
94 struct snd_ctl_elem_id *eid = NULL;
95 struct mixer *mixer = NULL;
96 unsigned int n, m;
97 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -070098 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -070099
Simon Wilson1bd580f2011-06-02 15:58:41 -0700100 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
101 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700102 if (fd < 0)
103 return 0;
104
105 memset(&elist, 0, sizeof(elist));
106 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
107 goto fail;
108
109 mixer = calloc(1, sizeof(*mixer));
110 if (!mixer)
111 goto fail;
112
113 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100114 if (!mixer->ctl)
Simon Wilsonec281392013-06-28 16:21:31 -0700115 goto fail;
116
117 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700118 goto fail;
119
120 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
121 if (!eid)
122 goto fail;
123
124 mixer->count = elist.count;
125 mixer->fd = fd;
126 elist.space = mixer->count;
127 elist.pids = eid;
128 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
129 goto fail;
130
131 for (n = 0; n < mixer->count; n++) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100132 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
Simon Wilson79d39652011-05-25 13:44:23 -0700133 ei->id.numid = eid[n].numid;
134 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
135 goto fail;
Simon Wilson79d39652011-05-25 13:44:23 -0700136 mixer->ctl[n].mixer = mixer;
137 if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
138 char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
139 if (!enames)
140 goto fail;
141 mixer->ctl[n].ename = enames;
142 for (m = 0; m < ei->value.enumerated.items; m++) {
143 memset(&tmp, 0, sizeof(tmp));
144 tmp.id.numid = ei->id.numid;
145 tmp.value.enumerated.item = m;
146 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
147 goto fail;
148 enames[m] = strdup(tmp.value.enumerated.name);
149 if (!enames[m])
150 goto fail;
151 }
152 }
153 }
154
155 free(eid);
156 return mixer;
157
158fail:
159 /* TODO: verify frees in failure case */
160 if (eid)
161 free(eid);
162 if (mixer)
163 mixer_close(mixer);
164 else if (fd >= 0)
165 close(fd);
166 return 0;
167}
168
Simon Wilsonec281392013-06-28 16:21:31 -0700169const char *mixer_get_name(struct mixer *mixer)
170{
171 return (const char *)mixer->card_info.name;
172}
173
Simon Wilsond2cb5032011-06-04 00:57:17 -0700174unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700175{
Simon Wilson193b1c32011-06-07 23:42:38 -0700176 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700177 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700178
Simon Wilson79d39652011-05-25 13:44:23 -0700179 return mixer->count;
180}
181
182struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
183{
Simon Wilson98c1f162011-06-07 16:12:32 -0700184 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700185 return mixer->ctl + id;
186
187 return NULL;
188}
189
190struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
191{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200192 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
193}
194
195struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
196 const char *name,
197 unsigned int index)
198{
Simon Wilson79d39652011-05-25 13:44:23 -0700199 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200200 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700201
Simon Wilson98c1f162011-06-07 16:12:32 -0700202 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700203 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700204
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200205 ctl = mixer->ctl;
206
Simon Wilson79d39652011-05-25 13:44:23 -0700207 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100208 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200209 if (index-- == 0)
210 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700211
212 return NULL;
213}
214
Simon Wilson710df882013-06-28 16:17:50 -0700215void mixer_ctl_update(struct mixer_ctl *ctl)
216{
217 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
218}
219
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100220unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
221{
222 if (!ctl)
223 return UINT_MAX;
224
225 /* numid values start at 1, return a 0-base value that
226 * can be passed to mixer_get_ctl()
227 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100228 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100229}
230
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800231const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700232{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800233 if (!ctl)
234 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700235
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100236 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700237}
238
239enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
240{
Simon Wilson193b1c32011-06-07 23:42:38 -0700241 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700242 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700243
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100244 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700245 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
246 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
247 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
248 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
249 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
250 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
251 default: return MIXER_CTL_TYPE_UNKNOWN;
252 };
253}
254
255const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
256{
Simon Wilson193b1c32011-06-07 23:42:38 -0700257 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700258 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700259
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100260 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700261 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
262 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
263 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
264 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
265 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
266 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
267 default: return "Unknown";
268 };
269}
270
271unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
272{
Simon Wilson193b1c32011-06-07 23:42:38 -0700273 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700274 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700275
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100276 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700277}
278
Simon Wilson79d39652011-05-25 13:44:23 -0700279static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
280{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200281 if ((percent > 100) || (percent < 0)) {
282 return -EINVAL;
283 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700284
Baptiste Robert4a484e12013-09-12 15:37:53 +0200285 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700286
Simon Wilson79d39652011-05-25 13:44:23 -0700287 return ei->value.integer.min + (range * percent) / 100;
288}
289
290static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
291{
292 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700293
Simon Wilson79d39652011-05-25 13:44:23 -0700294 if (range == 0)
295 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700296
Simon Wilson79d39652011-05-25 13:44:23 -0700297 return ((value - ei->value.integer.min) / range) * 100;
298}
299
Simon Wilsond2cb5032011-06-04 00:57:17 -0700300int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700301{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100302 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700303 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700304
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100305 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700306}
307
Simon Wilsond2cb5032011-06-04 00:57:17 -0700308int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700309{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100310 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700311 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700312
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100313 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700314}
315
Simon Wilson066c9f62011-06-05 18:23:05 -0700316int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700317{
Simon Wilson79d39652011-05-25 13:44:23 -0700318 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700319 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700320
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100321 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700322 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700323
Simon Wilson79d39652011-05-25 13:44:23 -0700324 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100325 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700326 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
327 if (ret < 0)
328 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700329
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100330 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700331 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700332 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700333
334 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700335 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700336
337 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
338 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700339
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500340 case SNDRV_CTL_ELEM_TYPE_BYTES:
341 return ev.value.bytes.data[id];
342
Simon Wilson79d39652011-05-25 13:44:23 -0700343 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700344 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700345 }
346
347 return 0;
348}
349
Simon Wilson38f87f32012-10-23 15:05:23 -0700350int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100351{
352 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530353 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700354 size_t size;
355 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100356
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100357 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100358 return -EINVAL;
359
360 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100361 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100362
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100363 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700364 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
365 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530366 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
367 if (ret < 0)
368 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700369 size = sizeof(ev.value.integer.value[0]);
370 source = ev.value.integer.value;
371 break;
372
373 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530374 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100375 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530376 struct snd_ctl_tlv *tlv;
377 int ret;
378
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700379 if (count > SIZE_MAX - sizeof(*tlv))
380 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530381 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700382 if (!tlv)
383 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100384 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530385 tlv->length = count;
386 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
387
388 source = tlv->tlv;
389 memcpy(array, source, count);
390
391 free(tlv);
392
393 return ret;
394 } else {
395 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
396 if (ret < 0)
397 return ret;
398 size = sizeof(ev.value.bytes.data[0]);
399 source = ev.value.bytes.data;
400 break;
401 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700402
403 default:
404 return -EINVAL;
405 }
406
407 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100408
409 return 0;
410}
411
Simon Wilson066c9f62011-06-05 18:23:05 -0700412int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700413{
414 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700415 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700416
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100417 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700418 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700419
420 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100421 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700422 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
423 if (ret < 0)
424 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700425
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100426 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700427 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700428 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700429 break;
430
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700431 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200432 if ((value < mixer_ctl_get_range_min(ctl)) ||
433 (value > mixer_ctl_get_range_max(ctl))) {
434 return -EINVAL;
435 }
436
Simon Wilsond2cb5032011-06-04 00:57:17 -0700437 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700438 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700439
Simon Wilson066c9f62011-06-05 18:23:05 -0700440 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
441 ev.value.enumerated.item[id] = value;
442 break;
443
David.Coutherutce2b6342013-06-11 16:22:57 +0200444 case SNDRV_CTL_ELEM_TYPE_BYTES:
445 ev.value.bytes.data[id] = value;
446 break;
447
Simon Wilson79d39652011-05-25 13:44:23 -0700448 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700449 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700450 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700451
Simon Wilson79d39652011-05-25 13:44:23 -0700452 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
453}
454
Simon Wilson38f87f32012-10-23 15:05:23 -0700455int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100456{
457 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700458 size_t size;
459 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100460
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100461 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100462 return -EINVAL;
463
464 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100465 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100466
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100467 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700468 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
469 case SNDRV_CTL_ELEM_TYPE_INTEGER:
470 size = sizeof(ev.value.integer.value[0]);
471 dest = ev.value.integer.value;
472 break;
473
474 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530475 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100476 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530477 struct snd_ctl_tlv *tlv;
478 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700479 if (count > SIZE_MAX - sizeof(*tlv))
480 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530481 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700482 if (!tlv)
483 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100484 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530485 tlv->length = count;
486 memcpy(tlv->tlv, array, count);
487
488 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
489 free(tlv);
490
491 return ret;
492 } else {
493 size = sizeof(ev.value.bytes.data[0]);
494 dest = ev.value.bytes.data;
495 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700496 break;
497
498 default:
499 return -EINVAL;
500 }
501
502 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100503
504 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
505}
506
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700507int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
508{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100509 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700510 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700511
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100512 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700513}
514
515int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
516{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100517 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700518 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700519
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100520 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700521}
522
Simon Wilson066c9f62011-06-05 18:23:05 -0700523unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
524{
Simon Wilson193b1c32011-06-07 23:42:38 -0700525 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700526 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700527
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100528 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700529}
530
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800531const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
532 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700533{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100534 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
535 (enum_id >= ctl->info.value.enumerated.items))
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800536 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700537
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800538 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700539}
540
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700541int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
542{
543 unsigned int i, num_enums;
544 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700545 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700546
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100547 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
Simon Wilson193b1c32011-06-07 23:42:38 -0700548 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700549
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100550 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700551 for (i = 0; i < num_enums; i++) {
552 if (!strcmp(string, ctl->ename[i])) {
553 memset(&ev, 0, sizeof(ev));
554 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100555 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700556 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
557 if (ret < 0)
558 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700559 return 0;
560 }
561 }
562
Simon Wilson193b1c32011-06-07 23:42:38 -0700563 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700564}
565