blob: 37921591d1b7e49122a58a59c590a9d3265526f2 [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
Simon Wilson8813fe82013-06-24 15:40:34 -0700335int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800336{
337 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530338 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700339 size_t size;
340 void *source;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530341 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800342
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530343 if ((!ctl) || !count || !array)
344 return -EINVAL;
345
346 total_count = ctl->info->count;
347
348 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
349 (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) {
350 /* Additional two words is for the TLV header */
351 total_count += TLV_HEADER_SIZE;
352 }
353
354 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800355 return -EINVAL;
356
357 memset(&ev, 0, sizeof(ev));
358 ev.id.numid = ctl->info->id.numid;
359
Simon Wilson8813fe82013-06-24 15:40:34 -0700360 switch (ctl->info->type) {
361 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
362 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K67b1df42014-08-18 15:39:36 +0530363 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
364 if (ret < 0)
365 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700366 size = sizeof(ev.value.integer.value[0]);
367 source = ev.value.integer.value;
368 break;
369
370 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530371 /* check if this is new bytes TLV */
372 if (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
373 struct snd_ctl_tlv *tlv;
374 int ret;
375
Ben Zhang6cb14f22016-04-22 17:59:40 -0700376 if (count > SIZE_MAX - sizeof(*tlv))
377 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530378 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700379 if (!tlv)
380 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530381 tlv->numid = ctl->info->id.numid;
382 tlv->length = count;
383 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
384
385 source = tlv->tlv;
386 memcpy(array, source, count);
387
388 free(tlv);
389
390 return ret;
391 } else {
392 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
393 if (ret < 0)
394 return ret;
395 size = sizeof(ev.value.bytes.data[0]);
396 source = ev.value.bytes.data;
397 break;
398 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700399
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530400 case SNDRV_CTL_ELEM_TYPE_IEC958:
401 size = sizeof(ev.value.iec958);
402 source = &ev.value.iec958;
403 break;
404
Simon Wilson8813fe82013-06-24 15:40:34 -0700405 default:
406 return -EINVAL;
407 }
408
409 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800410
411 return 0;
412}
413
Simon Wilsonedff7082011-06-06 15:33:34 -0700414int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
415{
416 struct snd_ctl_elem_value ev;
417 int ret;
418
419 if (!ctl || (id >= ctl->info->count))
420 return -EINVAL;
421
422 memset(&ev, 0, sizeof(ev));
423 ev.id.numid = ctl->info->id.numid;
424 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
425 if (ret < 0)
426 return ret;
427
428 switch (ctl->info->type) {
429 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
430 ev.value.integer.value[id] = !!value;
431 break;
432
433 case SNDRV_CTL_ELEM_TYPE_INTEGER:
434 ev.value.integer.value[id] = value;
435 break;
436
437 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
438 ev.value.enumerated.item[id] = value;
439 break;
440
David.Coutheruta8481aa2013-06-11 16:22:57 +0200441 case SNDRV_CTL_ELEM_TYPE_BYTES:
442 ev.value.bytes.data[id] = value;
443 break;
444
Simon Wilsonedff7082011-06-06 15:33:34 -0700445 default:
446 return -EINVAL;
447 }
448
449 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
450}
451
Simon Wilson8813fe82013-06-24 15:40:34 -0700452int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800453{
454 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700455 size_t size;
456 void *dest;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530457 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800458
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530459 if ((!ctl) || !count || !array)
460 return -EINVAL;
461
462 total_count = ctl->info->count;
463
464 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
465 (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE)) {
466 /* Additional two words is for the TLV header */
467 total_count += TLV_HEADER_SIZE;
468 }
469
470 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800471 return -EINVAL;
472
473 memset(&ev, 0, sizeof(ev));
474 ev.id.numid = ctl->info->id.numid;
475
Simon Wilson8813fe82013-06-24 15:40:34 -0700476 switch (ctl->info->type) {
477 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
478 case SNDRV_CTL_ELEM_TYPE_INTEGER:
479 size = sizeof(ev.value.integer.value[0]);
480 dest = ev.value.integer.value;
481 break;
482
483 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530484 /* check if this is new bytes TLV */
485 if (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
486 struct snd_ctl_tlv *tlv;
487 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700488 if (count > SIZE_MAX - sizeof(*tlv))
489 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530490 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700491 if (!tlv)
492 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530493 tlv->numid = ctl->info->id.numid;
494 tlv->length = count;
495 memcpy(tlv->tlv, array, count);
496
497 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
498 free(tlv);
499
500 return ret;
501 } else {
502 size = sizeof(ev.value.bytes.data[0]);
503 dest = ev.value.bytes.data;
504 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700505 break;
506
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530507 case SNDRV_CTL_ELEM_TYPE_IEC958:
508 size = sizeof(ev.value.iec958);
509 dest = &ev.value.iec958;
510 break;
511
Simon Wilson8813fe82013-06-24 15:40:34 -0700512 default:
513 return -EINVAL;
514 }
515
516 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800517
518 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
519}
520
Simon Wilsonedff7082011-06-06 15:33:34 -0700521int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
522{
Simon Wilsonedff7082011-06-06 15:33:34 -0700523 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
524 return -EINVAL;
525
Simon Wilsonedff7082011-06-06 15:33:34 -0700526 return ctl->info->value.integer.min;
527}
528
529int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
530{
Simon Wilsonedff7082011-06-06 15:33:34 -0700531 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
532 return -EINVAL;
533
Simon Wilsonedff7082011-06-06 15:33:34 -0700534 return ctl->info->value.integer.max;
535}
536
537unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
538{
539 if (!ctl)
540 return 0;
541
542 return ctl->info->value.enumerated.items;
543}
544
Simon Wilsone44e30a2012-03-08 10:45:31 -0800545const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
546 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700547{
Simon Wilsonedff7082011-06-06 15:33:34 -0700548 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
549 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800550 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700551
Simon Wilsone44e30a2012-03-08 10:45:31 -0800552 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700553}
554
555int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
556{
557 unsigned int i, num_enums;
558 struct snd_ctl_elem_value ev;
559 int ret;
560
561 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
562 return -EINVAL;
563
564 num_enums = ctl->info->value.enumerated.items;
565 for (i = 0; i < num_enums; i++) {
566 if (!strcmp(string, ctl->ename[i])) {
567 memset(&ev, 0, sizeof(ev));
568 ev.value.enumerated.item[0] = i;
569 ev.id.numid = ctl->info->id.numid;
570 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
571 if (ret < 0)
572 return ret;
573 return 0;
574 }
575 }
576
577 return -EINVAL;
578}
579