blob: 7419d3f0e28e93128b0051ee25b763fd0780c4e7 [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>
31#include <string.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <ctype.h>
36
Simon Wilson6a52f2c2012-05-04 16:32:10 -070037#include <sys/ioctl.h>
38
Simon Wilson79d39652011-05-25 13:44:23 -070039#include <linux/ioctl.h>
40#define __force
41#define __bitwise
42#define __user
43#include <sound/asound.h>
44
45#include <tinyalsa/asoundlib.h>
46
Simon Wilson79d39652011-05-25 13:44:23 -070047struct mixer_ctl {
48 struct mixer *mixer;
49 struct snd_ctl_elem_info *info;
50 char **ename;
51};
52
53struct mixer {
54 int fd;
55 struct snd_ctl_elem_info *info;
56 struct mixer_ctl *ctl;
57 unsigned int count;
58};
59
60void mixer_close(struct mixer *mixer)
61{
62 unsigned int n,m;
63
Simon Wilson193b1c32011-06-07 23:42:38 -070064 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070065 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070066
Simon Wilson79d39652011-05-25 13:44:23 -070067 if (mixer->fd >= 0)
68 close(mixer->fd);
69
70 if (mixer->ctl) {
71 for (n = 0; n < mixer->count; n++) {
72 if (mixer->ctl[n].ename) {
73 unsigned int max = mixer->ctl[n].info->value.enumerated.items;
74 for (m = 0; m < max; m++)
75 free(mixer->ctl[n].ename[m]);
76 free(mixer->ctl[n].ename);
77 }
78 }
79 free(mixer->ctl);
80 }
81
82 if (mixer->info)
83 free(mixer->info);
84
85 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));
114 mixer->info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
115 if (!mixer->ctl || !mixer->info)
116 goto fail;
117
118 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
119 if (!eid)
120 goto fail;
121
122 mixer->count = elist.count;
123 mixer->fd = fd;
124 elist.space = mixer->count;
125 elist.pids = eid;
126 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
127 goto fail;
128
129 for (n = 0; n < mixer->count; n++) {
130 struct snd_ctl_elem_info *ei = mixer->info + n;
131 ei->id.numid = eid[n].numid;
132 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
133 goto fail;
134 mixer->ctl[n].info = ei;
135 mixer->ctl[n].mixer = mixer;
136 if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
137 char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
138 if (!enames)
139 goto fail;
140 mixer->ctl[n].ename = enames;
141 for (m = 0; m < ei->value.enumerated.items; m++) {
142 memset(&tmp, 0, sizeof(tmp));
143 tmp.id.numid = ei->id.numid;
144 tmp.value.enumerated.item = m;
145 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
146 goto fail;
147 enames[m] = strdup(tmp.value.enumerated.name);
148 if (!enames[m])
149 goto fail;
150 }
151 }
152 }
153
154 free(eid);
155 return mixer;
156
157fail:
158 /* TODO: verify frees in failure case */
159 if (eid)
160 free(eid);
161 if (mixer)
162 mixer_close(mixer);
163 else if (fd >= 0)
164 close(fd);
165 return 0;
166}
167
Simon Wilsond2cb5032011-06-04 00:57:17 -0700168unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700169{
Simon Wilson193b1c32011-06-07 23:42:38 -0700170 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700171 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700172
Simon Wilson79d39652011-05-25 13:44:23 -0700173 return mixer->count;
174}
175
176struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
177{
Simon Wilson98c1f162011-06-07 16:12:32 -0700178 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700179 return mixer->ctl + id;
180
181 return NULL;
182}
183
184struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
185{
186 unsigned int n;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700187
Simon Wilson98c1f162011-06-07 16:12:32 -0700188 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700189 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700190
Simon Wilson79d39652011-05-25 13:44:23 -0700191 for (n = 0; n < mixer->count; n++)
192 if (!strcmp(name, (char*) mixer->info[n].id.name))
193 return mixer->ctl + n;
194
195 return NULL;
196}
197
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800198const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700199{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800200 if (!ctl)
201 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700202
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800203 return (const char *)ctl->info->id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700204}
205
206enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
207{
Simon Wilson193b1c32011-06-07 23:42:38 -0700208 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700209 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700210
211 switch (ctl->info->type) {
212 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
213 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
214 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
215 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
216 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
217 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
218 default: return MIXER_CTL_TYPE_UNKNOWN;
219 };
220}
221
222const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
223{
Simon Wilson193b1c32011-06-07 23:42:38 -0700224 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700225 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700226
227 switch (ctl->info->type) {
228 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
229 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
230 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
231 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
232 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
233 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
234 default: return "Unknown";
235 };
236}
237
238unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
239{
Simon Wilson193b1c32011-06-07 23:42:38 -0700240 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700241 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700242
243 return ctl->info->count;
244}
245
Simon Wilson79d39652011-05-25 13:44:23 -0700246static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
247{
248 int range;
Simon Wilson98c1f162011-06-07 16:12:32 -0700249
Simon Wilson79d39652011-05-25 13:44:23 -0700250 if (percent > 100)
251 percent = 100;
252 else if (percent < 0)
253 percent = 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700254
Simon Wilson79d39652011-05-25 13:44:23 -0700255 range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700256
Simon Wilson79d39652011-05-25 13:44:23 -0700257 return ei->value.integer.min + (range * percent) / 100;
258}
259
260static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
261{
262 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700263
Simon Wilson79d39652011-05-25 13:44:23 -0700264 if (range == 0)
265 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700266
Simon Wilson79d39652011-05-25 13:44:23 -0700267 return ((value - ei->value.integer.min) / range) * 100;
268}
269
Simon Wilsond2cb5032011-06-04 00:57:17 -0700270int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700271{
Simon Wilson193b1c32011-06-07 23:42:38 -0700272 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
273 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700274
Simon Wilson066c9f62011-06-05 18:23:05 -0700275 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700276}
277
Simon Wilsond2cb5032011-06-04 00:57:17 -0700278int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700279{
Simon Wilson193b1c32011-06-07 23:42:38 -0700280 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
281 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700282
Simon Wilson066c9f62011-06-05 18:23:05 -0700283 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700284}
285
Simon Wilson066c9f62011-06-05 18:23:05 -0700286int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700287{
Simon Wilson79d39652011-05-25 13:44:23 -0700288 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700289 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700290
Simon Wilson193b1c32011-06-07 23:42:38 -0700291 if (!ctl || (id >= ctl->info->count))
292 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700293
Simon Wilson79d39652011-05-25 13:44:23 -0700294 memset(&ev, 0, sizeof(ev));
295 ev.id.numid = ctl->info->id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700296 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
297 if (ret < 0)
298 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700299
300 switch (ctl->info->type) {
301 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700302 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700303
304 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700305 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700306
307 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
308 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700309
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500310 case SNDRV_CTL_ELEM_TYPE_BYTES:
311 return ev.value.bytes.data[id];
312
Simon Wilson79d39652011-05-25 13:44:23 -0700313 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700314 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700315 }
316
317 return 0;
318}
319
Simon Wilson38f87f32012-10-23 15:05:23 -0700320int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100321{
322 struct snd_ctl_elem_value ev;
323 int ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700324 size_t size;
325 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100326
Simon Wilson38f87f32012-10-23 15:05:23 -0700327 if (!ctl || (count > ctl->info->count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100328 return -EINVAL;
329
330 memset(&ev, 0, sizeof(ev));
331 ev.id.numid = ctl->info->id.numid;
332
333 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
334 if (ret < 0)
335 return ret;
336
Simon Wilson38f87f32012-10-23 15:05:23 -0700337 switch (ctl->info->type) {
338 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
339 case SNDRV_CTL_ELEM_TYPE_INTEGER:
340 size = sizeof(ev.value.integer.value[0]);
341 source = ev.value.integer.value;
342 break;
343
344 case SNDRV_CTL_ELEM_TYPE_BYTES:
345 size = sizeof(ev.value.bytes.data[0]);
346 source = ev.value.bytes.data;
347 break;
348
349 default:
350 return -EINVAL;
351 }
352
353 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100354
355 return 0;
356}
357
Simon Wilson066c9f62011-06-05 18:23:05 -0700358int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700359{
360 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700361 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700362
Simon Wilson193b1c32011-06-07 23:42:38 -0700363 if (!ctl || (id >= ctl->info->count))
364 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700365
366 memset(&ev, 0, sizeof(ev));
367 ev.id.numid = ctl->info->id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700368 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
369 if (ret < 0)
370 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700371
Simon Wilson79d39652011-05-25 13:44:23 -0700372 switch (ctl->info->type) {
373 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700374 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700375 break;
376
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700377 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700378 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700379 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700380
Simon Wilson066c9f62011-06-05 18:23:05 -0700381 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
382 ev.value.enumerated.item[id] = value;
383 break;
384
Simon Wilson79d39652011-05-25 13:44:23 -0700385 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700386 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700387 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700388
Simon Wilson79d39652011-05-25 13:44:23 -0700389 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
390}
391
Simon Wilson38f87f32012-10-23 15:05:23 -0700392int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100393{
394 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700395 size_t size;
396 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100397
Simon Wilson38f87f32012-10-23 15:05:23 -0700398 if (!ctl || (count > ctl->info->count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100399 return -EINVAL;
400
401 memset(&ev, 0, sizeof(ev));
402 ev.id.numid = ctl->info->id.numid;
403
Simon Wilson38f87f32012-10-23 15:05:23 -0700404 switch (ctl->info->type) {
405 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
406 case SNDRV_CTL_ELEM_TYPE_INTEGER:
407 size = sizeof(ev.value.integer.value[0]);
408 dest = ev.value.integer.value;
409 break;
410
411 case SNDRV_CTL_ELEM_TYPE_BYTES:
412 size = sizeof(ev.value.bytes.data[0]);
413 dest = ev.value.bytes.data;
414 break;
415
416 default:
417 return -EINVAL;
418 }
419
420 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100421
422 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
423}
424
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700425int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
426{
Simon Wilson193b1c32011-06-07 23:42:38 -0700427 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
428 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700429
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700430 return ctl->info->value.integer.min;
431}
432
433int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
434{
Simon Wilson193b1c32011-06-07 23:42:38 -0700435 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
436 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700437
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700438 return ctl->info->value.integer.max;
439}
440
Simon Wilson066c9f62011-06-05 18:23:05 -0700441unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
442{
Simon Wilson193b1c32011-06-07 23:42:38 -0700443 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700444 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700445
446 return ctl->info->value.enumerated.items;
447}
448
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800449const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
450 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700451{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700452 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Simon Wilson193b1c32011-06-07 23:42:38 -0700453 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800454 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700455
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800456 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700457}
458
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700459int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
460{
461 unsigned int i, num_enums;
462 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700463 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700464
Simon Wilson193b1c32011-06-07 23:42:38 -0700465 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
466 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700467
468 num_enums = ctl->info->value.enumerated.items;
469 for (i = 0; i < num_enums; i++) {
470 if (!strcmp(string, ctl->ename[i])) {
471 memset(&ev, 0, sizeof(ev));
472 ev.value.enumerated.item[0] = i;
473 ev.id.numid = ctl->info->id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700474 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
475 if (ret < 0)
476 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700477 return 0;
478 }
479 }
480
Simon Wilson193b1c32011-06-07 23:42:38 -0700481 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700482}
483