blob: 0326c6a4ccfa540eed458b06b6e13fdb7bb98883 [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -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 Zhang6cb14f22016-04-22 17:59:40 -070031#include <stdint.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070032#include <string.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <ctype.h>
37
Simon Wilson85dc38f2012-05-15 17:37:19 -070038#include <sys/ioctl.h>
39
Simon Wilsonedff7082011-06-06 15:33:34 -070040#include <linux/ioctl.h>
41#define __force
42#define __bitwise
43#define __user
44#include <sound/asound.h>
45
46#include <tinyalsa/asoundlib.h>
47
48struct mixer_ctl {
49 struct mixer *mixer;
50 struct snd_ctl_elem_info *info;
51 char **ename;
52};
53
54struct mixer {
55 int fd;
Simon Wilson7e8a6562013-06-28 16:47:16 -070056 struct snd_ctl_card_info card_info;
57 struct snd_ctl_elem_info *elem_info;
Simon Wilsonedff7082011-06-06 15:33:34 -070058 struct mixer_ctl *ctl;
59 unsigned int count;
60};
61
62void mixer_close(struct mixer *mixer)
63{
64 unsigned int n,m;
65
66 if (!mixer)
67 return;
68
69 if (mixer->fd >= 0)
70 close(mixer->fd);
71
72 if (mixer->ctl) {
73 for (n = 0; n < mixer->count; n++) {
74 if (mixer->ctl[n].ename) {
75 unsigned int max = mixer->ctl[n].info->value.enumerated.items;
76 for (m = 0; m < max; m++)
77 free(mixer->ctl[n].ename[m]);
78 free(mixer->ctl[n].ename);
79 }
80 }
81 free(mixer->ctl);
82 }
83
Simon Wilson7e8a6562013-06-28 16:47:16 -070084 if (mixer->elem_info)
85 free(mixer->elem_info);
Simon Wilsonedff7082011-06-06 15:33:34 -070086
87 free(mixer);
88
89 /* TODO: verify frees */
90}
91
92struct mixer *mixer_open(unsigned int card)
93{
94 struct snd_ctl_elem_list elist;
95 struct snd_ctl_elem_info tmp;
96 struct snd_ctl_elem_id *eid = NULL;
97 struct mixer *mixer = NULL;
98 unsigned int n, m;
99 int fd;
100 char fn[256];
101
102 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
103 fd = open(fn, O_RDWR);
104 if (fd < 0)
105 return 0;
106
107 memset(&elist, 0, sizeof(elist));
108 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
109 goto fail;
110
111 mixer = calloc(1, sizeof(*mixer));
112 if (!mixer)
113 goto fail;
114
115 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Simon Wilson7e8a6562013-06-28 16:47:16 -0700116 mixer->elem_info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
117 if (!mixer->ctl || !mixer->elem_info)
118 goto fail;
119
120 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilsonedff7082011-06-06 15:33:34 -0700121 goto fail;
122
123 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
124 if (!eid)
125 goto fail;
126
127 mixer->count = elist.count;
128 mixer->fd = fd;
129 elist.space = mixer->count;
130 elist.pids = eid;
131 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
132 goto fail;
133
134 for (n = 0; n < mixer->count; n++) {
Simon Wilson7e8a6562013-06-28 16:47:16 -0700135 struct snd_ctl_elem_info *ei = mixer->elem_info + n;
Simon Wilsonedff7082011-06-06 15:33:34 -0700136 ei->id.numid = eid[n].numid;
137 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
138 goto fail;
139 mixer->ctl[n].info = ei;
140 mixer->ctl[n].mixer = mixer;
141 if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
142 char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
143 if (!enames)
144 goto fail;
145 mixer->ctl[n].ename = enames;
146 for (m = 0; m < ei->value.enumerated.items; m++) {
147 memset(&tmp, 0, sizeof(tmp));
148 tmp.id.numid = ei->id.numid;
149 tmp.value.enumerated.item = m;
150 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
151 goto fail;
152 enames[m] = strdup(tmp.value.enumerated.name);
153 if (!enames[m])
154 goto fail;
155 }
156 }
157 }
158
159 free(eid);
160 return mixer;
161
162fail:
163 /* TODO: verify frees in failure case */
164 if (eid)
165 free(eid);
166 if (mixer)
167 mixer_close(mixer);
168 else if (fd >= 0)
169 close(fd);
170 return 0;
171}
172
Simon Wilson7e8a6562013-06-28 16:47:16 -0700173const char *mixer_get_name(struct mixer *mixer)
174{
175 return (const char *)mixer->card_info.name;
176}
177
Simon Wilsonedff7082011-06-06 15:33:34 -0700178unsigned int mixer_get_num_ctls(struct mixer *mixer)
179{
180 if (!mixer)
181 return 0;
182
183 return mixer->count;
184}
185
186struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
187{
188 if (mixer && (id < mixer->count))
189 return mixer->ctl + id;
190
191 return NULL;
192}
193
194struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
195{
196 unsigned int n;
197
198 if (!mixer)
199 return NULL;
200
201 for (n = 0; n < mixer->count; n++)
Simon Wilson7e8a6562013-06-28 16:47:16 -0700202 if (!strcmp(name, (char*) mixer->elem_info[n].id.name))
Simon Wilsonedff7082011-06-06 15:33:34 -0700203 return mixer->ctl + n;
204
205 return NULL;
206}
207
Simon Wilson7e8a6562013-06-28 16:47:16 -0700208void mixer_ctl_update(struct mixer_ctl *ctl)
209{
210 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
211}
212
Simon Wilsone44e30a2012-03-08 10:45:31 -0800213const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700214{
Simon Wilsone44e30a2012-03-08 10:45:31 -0800215 if (!ctl)
216 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700217
Simon Wilsone44e30a2012-03-08 10:45:31 -0800218 return (const char *)ctl->info->id.name;
Simon Wilsonedff7082011-06-06 15:33:34 -0700219}
220
221enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
222{
223 if (!ctl)
224 return MIXER_CTL_TYPE_UNKNOWN;
225
226 switch (ctl->info->type) {
227 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
228 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
229 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
230 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
231 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
232 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
233 default: return MIXER_CTL_TYPE_UNKNOWN;
234 };
235}
236
237const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
238{
239 if (!ctl)
240 return "";
241
242 switch (ctl->info->type) {
243 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
244 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
245 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
246 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
247 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
248 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
249 default: return "Unknown";
250 };
251}
252
253unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
254{
255 if (!ctl)
256 return 0;
257
258 return ctl->info->count;
259}
260
261static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
262{
263 int range;
264
265 if (percent > 100)
266 percent = 100;
267 else if (percent < 0)
268 percent = 0;
269
270 range = (ei->value.integer.max - ei->value.integer.min);
271
272 return ei->value.integer.min + (range * percent) / 100;
273}
274
275static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
276{
277 int range = (ei->value.integer.max - ei->value.integer.min);
278
279 if (range == 0)
280 return 0;
281
282 return ((value - ei->value.integer.min) / range) * 100;
283}
284
285int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
286{
287 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
288 return -EINVAL;
289
290 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
291}
292
293int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
294{
295 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
296 return -EINVAL;
297
298 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
299}
300
301int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
302{
303 struct snd_ctl_elem_value ev;
304 int ret;
305
306 if (!ctl || (id >= ctl->info->count))
307 return -EINVAL;
308
309 memset(&ev, 0, sizeof(ev));
310 ev.id.numid = ctl->info->id.numid;
311 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
312 if (ret < 0)
313 return ret;
314
315 switch (ctl->info->type) {
316 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
317 return !!ev.value.integer.value[id];
318
319 case SNDRV_CTL_ELEM_TYPE_INTEGER:
320 return ev.value.integer.value[id];
321
322 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
323 return ev.value.enumerated.item[id];
324
Simon Wilson5aed71d2011-11-16 14:45:38 -0800325 case SNDRV_CTL_ELEM_TYPE_BYTES:
326 return ev.value.bytes.data[id];
327
Simon Wilsonedff7082011-06-06 15:33:34 -0700328 default:
329 return -EINVAL;
330 }
331
332 return 0;
333}
334
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530335int mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl)
336{
337 return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
338}
339
Simon Wilson8813fe82013-06-24 15:40:34 -0700340int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800341{
342 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530343 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700344 size_t size;
345 void *source;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530346 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800347
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530348 if ((!ctl) || !count || !array)
349 return -EINVAL;
350
351 total_count = ctl->info->count;
352
353 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530354 mixer_ctl_is_access_tlv_rw(ctl)) {
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530355 /* Additional two words is for the TLV header */
356 total_count += TLV_HEADER_SIZE;
357 }
358
359 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800360 return -EINVAL;
361
362 memset(&ev, 0, sizeof(ev));
363 ev.id.numid = ctl->info->id.numid;
364
Simon Wilson8813fe82013-06-24 15:40:34 -0700365 switch (ctl->info->type) {
366 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
367 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K67b1df42014-08-18 15:39:36 +0530368 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
369 if (ret < 0)
370 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700371 size = sizeof(ev.value.integer.value[0]);
372 source = ev.value.integer.value;
373 break;
374
375 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530376 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530377 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530378 struct snd_ctl_tlv *tlv;
379 int ret;
380
Ben Zhang6cb14f22016-04-22 17:59:40 -0700381 if (count > SIZE_MAX - sizeof(*tlv))
382 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530383 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700384 if (!tlv)
385 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530386 tlv->numid = ctl->info->id.numid;
387 tlv->length = count;
388 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
389
390 source = tlv->tlv;
391 memcpy(array, source, count);
392
393 free(tlv);
394
395 return ret;
396 } else {
397 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
398 if (ret < 0)
399 return ret;
400 size = sizeof(ev.value.bytes.data[0]);
401 source = ev.value.bytes.data;
402 break;
403 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700404
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530405 case SNDRV_CTL_ELEM_TYPE_IEC958:
406 size = sizeof(ev.value.iec958);
407 source = &ev.value.iec958;
408 break;
409
Simon Wilson8813fe82013-06-24 15:40:34 -0700410 default:
411 return -EINVAL;
412 }
413
414 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800415
416 return 0;
417}
418
Simon Wilsonedff7082011-06-06 15:33:34 -0700419int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
420{
421 struct snd_ctl_elem_value ev;
422 int ret;
423
424 if (!ctl || (id >= ctl->info->count))
425 return -EINVAL;
426
427 memset(&ev, 0, sizeof(ev));
428 ev.id.numid = ctl->info->id.numid;
429 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
430 if (ret < 0)
431 return ret;
432
433 switch (ctl->info->type) {
434 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
435 ev.value.integer.value[id] = !!value;
436 break;
437
438 case SNDRV_CTL_ELEM_TYPE_INTEGER:
439 ev.value.integer.value[id] = value;
440 break;
441
442 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
443 ev.value.enumerated.item[id] = value;
444 break;
445
David.Coutheruta8481aa2013-06-11 16:22:57 +0200446 case SNDRV_CTL_ELEM_TYPE_BYTES:
447 ev.value.bytes.data[id] = value;
448 break;
449
Simon Wilsonedff7082011-06-06 15:33:34 -0700450 default:
451 return -EINVAL;
452 }
453
454 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
455}
456
Simon Wilson8813fe82013-06-24 15:40:34 -0700457int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800458{
459 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700460 size_t size;
461 void *dest;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530462 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800463
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530464 if ((!ctl) || !count || !array)
465 return -EINVAL;
466
467 total_count = ctl->info->count;
468
469 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530470 mixer_ctl_is_access_tlv_rw(ctl)) {
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530471 /* Additional two words is for the TLV header */
472 total_count += TLV_HEADER_SIZE;
473 }
474
475 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800476 return -EINVAL;
477
478 memset(&ev, 0, sizeof(ev));
479 ev.id.numid = ctl->info->id.numid;
480
Simon Wilson8813fe82013-06-24 15:40:34 -0700481 switch (ctl->info->type) {
482 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
483 case SNDRV_CTL_ELEM_TYPE_INTEGER:
484 size = sizeof(ev.value.integer.value[0]);
485 dest = ev.value.integer.value;
486 break;
487
488 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530489 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530490 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530491 struct snd_ctl_tlv *tlv;
492 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700493 if (count > SIZE_MAX - sizeof(*tlv))
494 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530495 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700496 if (!tlv)
497 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530498 tlv->numid = ctl->info->id.numid;
499 tlv->length = count;
500 memcpy(tlv->tlv, array, count);
501
502 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
503 free(tlv);
504
505 return ret;
506 } else {
507 size = sizeof(ev.value.bytes.data[0]);
508 dest = ev.value.bytes.data;
509 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700510 break;
511
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530512 case SNDRV_CTL_ELEM_TYPE_IEC958:
513 size = sizeof(ev.value.iec958);
514 dest = &ev.value.iec958;
515 break;
516
Simon Wilson8813fe82013-06-24 15:40:34 -0700517 default:
518 return -EINVAL;
519 }
520
521 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800522
523 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
524}
525
Simon Wilsonedff7082011-06-06 15:33:34 -0700526int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
527{
Simon Wilsonedff7082011-06-06 15:33:34 -0700528 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
529 return -EINVAL;
530
Simon Wilsonedff7082011-06-06 15:33:34 -0700531 return ctl->info->value.integer.min;
532}
533
534int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
535{
Simon Wilsonedff7082011-06-06 15:33:34 -0700536 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
537 return -EINVAL;
538
Simon Wilsonedff7082011-06-06 15:33:34 -0700539 return ctl->info->value.integer.max;
540}
541
542unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
543{
544 if (!ctl)
545 return 0;
546
547 return ctl->info->value.enumerated.items;
548}
549
Simon Wilsone44e30a2012-03-08 10:45:31 -0800550const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
551 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700552{
Simon Wilsonedff7082011-06-06 15:33:34 -0700553 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
554 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800555 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700556
Simon Wilsone44e30a2012-03-08 10:45:31 -0800557 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700558}
559
560int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
561{
562 unsigned int i, num_enums;
563 struct snd_ctl_elem_value ev;
564 int ret;
565
566 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
567 return -EINVAL;
568
569 num_enums = ctl->info->value.enumerated.items;
570 for (i = 0; i < num_enums; i++) {
571 if (!strcmp(string, ctl->ename[i])) {
572 memset(&ev, 0, sizeof(ev));
573 ev.value.enumerated.item[0] = i;
574 ev.id.numid = ctl->info->id.numid;
575 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
576 if (ret < 0)
577 return ret;
578 return 0;
579 }
580 }
581
582 return -EINVAL;
583}
584