blob: a02c79053718e639a95e518b56963ef57bd7ad81 [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;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800341
Simon Wilson8813fe82013-06-24 15:40:34 -0700342 if (!ctl || (count > ctl->info->count) || !count || !array)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800343 return -EINVAL;
344
345 memset(&ev, 0, sizeof(ev));
346 ev.id.numid = ctl->info->id.numid;
347
Simon Wilson8813fe82013-06-24 15:40:34 -0700348 switch (ctl->info->type) {
349 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
350 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K67b1df42014-08-18 15:39:36 +0530351 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
352 if (ret < 0)
353 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700354 size = sizeof(ev.value.integer.value[0]);
355 source = ev.value.integer.value;
356 break;
357
358 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530359 /* check if this is new bytes TLV */
360 if (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
361 struct snd_ctl_tlv *tlv;
362 int ret;
363
Ben Zhang6cb14f22016-04-22 17:59:40 -0700364 if (count > SIZE_MAX - sizeof(*tlv))
365 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530366 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700367 if (!tlv)
368 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530369 tlv->numid = ctl->info->id.numid;
370 tlv->length = count;
371 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
372
373 source = tlv->tlv;
374 memcpy(array, source, count);
375
376 free(tlv);
377
378 return ret;
379 } else {
380 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
381 if (ret < 0)
382 return ret;
383 size = sizeof(ev.value.bytes.data[0]);
384 source = ev.value.bytes.data;
385 break;
386 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700387
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530388 case SNDRV_CTL_ELEM_TYPE_IEC958:
389 size = sizeof(ev.value.iec958);
390 source = &ev.value.iec958;
391 break;
392
Simon Wilson8813fe82013-06-24 15:40:34 -0700393 default:
394 return -EINVAL;
395 }
396
397 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800398
399 return 0;
400}
401
Simon Wilsonedff7082011-06-06 15:33:34 -0700402int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
403{
404 struct snd_ctl_elem_value ev;
405 int ret;
406
407 if (!ctl || (id >= ctl->info->count))
408 return -EINVAL;
409
410 memset(&ev, 0, sizeof(ev));
411 ev.id.numid = ctl->info->id.numid;
412 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
413 if (ret < 0)
414 return ret;
415
416 switch (ctl->info->type) {
417 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
418 ev.value.integer.value[id] = !!value;
419 break;
420
Danny Baumann076d79c2016-02-09 14:11:52 +0100421 case SNDRV_CTL_ELEM_TYPE_INTEGER: {
422 int min = mixer_ctl_get_range_min(ctl);
423 int max = mixer_ctl_get_range_max(ctl);
424 if (min < max && (value < min || value > max))
Scott Mertzee1cf2b2015-11-03 12:41:51 -0800425 return -EINVAL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700426 ev.value.integer.value[id] = value;
427 break;
Danny Baumann076d79c2016-02-09 14:11:52 +0100428 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700429
430 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
Chirayu Desai0a99bf32016-08-28 02:52:27 +0530431 if (value < 0 || (value >= 0 && (unsigned int)value >= mixer_ctl_get_num_enums(ctl)))
Scott Mertzee1cf2b2015-11-03 12:41:51 -0800432 return -EINVAL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700433 ev.value.enumerated.item[id] = value;
434 break;
435
David.Coutheruta8481aa2013-06-11 16:22:57 +0200436 case SNDRV_CTL_ELEM_TYPE_BYTES:
437 ev.value.bytes.data[id] = value;
438 break;
439
Simon Wilsonedff7082011-06-06 15:33:34 -0700440 default:
441 return -EINVAL;
442 }
443
444 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
445}
446
Simon Wilson8813fe82013-06-24 15:40:34 -0700447int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800448{
449 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700450 size_t size;
451 void *dest;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800452
Simon Wilson8813fe82013-06-24 15:40:34 -0700453 if (!ctl || (count > ctl->info->count) || !count || !array)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800454 return -EINVAL;
455
456 memset(&ev, 0, sizeof(ev));
457 ev.id.numid = ctl->info->id.numid;
458
Simon Wilson8813fe82013-06-24 15:40:34 -0700459 switch (ctl->info->type) {
460 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
461 case SNDRV_CTL_ELEM_TYPE_INTEGER:
462 size = sizeof(ev.value.integer.value[0]);
463 dest = ev.value.integer.value;
464 break;
465
466 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530467 /* check if this is new bytes TLV */
468 if (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
469 struct snd_ctl_tlv *tlv;
470 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700471 if (count > SIZE_MAX - sizeof(*tlv))
472 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530473 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700474 if (!tlv)
475 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530476 tlv->numid = ctl->info->id.numid;
477 tlv->length = count;
478 memcpy(tlv->tlv, array, count);
479
480 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
481 free(tlv);
482
483 return ret;
484 } else {
485 size = sizeof(ev.value.bytes.data[0]);
486 dest = ev.value.bytes.data;
487 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700488 break;
489
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530490 case SNDRV_CTL_ELEM_TYPE_IEC958:
491 size = sizeof(ev.value.iec958);
492 dest = &ev.value.iec958;
493 break;
494
Simon Wilson8813fe82013-06-24 15:40:34 -0700495 default:
496 return -EINVAL;
497 }
498
499 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800500
501 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
502}
503
Simon Wilsonedff7082011-06-06 15:33:34 -0700504int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
505{
Simon Wilsonedff7082011-06-06 15:33:34 -0700506 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
507 return -EINVAL;
508
Simon Wilsonedff7082011-06-06 15:33:34 -0700509 return ctl->info->value.integer.min;
510}
511
512int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
513{
Simon Wilsonedff7082011-06-06 15:33:34 -0700514 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
515 return -EINVAL;
516
Simon Wilsonedff7082011-06-06 15:33:34 -0700517 return ctl->info->value.integer.max;
518}
519
520unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
521{
522 if (!ctl)
523 return 0;
524
525 return ctl->info->value.enumerated.items;
526}
527
Simon Wilsone44e30a2012-03-08 10:45:31 -0800528const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
529 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700530{
Simon Wilsonedff7082011-06-06 15:33:34 -0700531 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
532 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800533 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700534
Simon Wilsone44e30a2012-03-08 10:45:31 -0800535 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700536}
537
538int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
539{
540 unsigned int i, num_enums;
541 struct snd_ctl_elem_value ev;
542 int ret;
543
544 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
545 return -EINVAL;
546
547 num_enums = ctl->info->value.enumerated.items;
548 for (i = 0; i < num_enums; i++) {
549 if (!strcmp(string, ctl->ename[i])) {
550 memset(&ev, 0, sizeof(ev));
551 ev.value.enumerated.item[0] = i;
552 ev.id.numid = ctl->info->id.numid;
553 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
554 if (ret < 0)
555 return ret;
556 return 0;
557 }
558 }
559
560 return -EINVAL;
561}
562