blob: a413c60d0d458d5d209d7f8d6b8e571fc8dda810 [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 Wilson710df882013-06-28 16:17:50 -0700198void mixer_ctl_update(struct mixer_ctl *ctl)
199{
200 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
201}
202
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800203const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700204{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800205 if (!ctl)
206 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700207
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800208 return (const char *)ctl->info->id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700209}
210
211enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
212{
Simon Wilson193b1c32011-06-07 23:42:38 -0700213 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700214 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700215
216 switch (ctl->info->type) {
217 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
218 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
219 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
220 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
221 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
222 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
223 default: return MIXER_CTL_TYPE_UNKNOWN;
224 };
225}
226
227const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
228{
Simon Wilson193b1c32011-06-07 23:42:38 -0700229 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700230 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700231
232 switch (ctl->info->type) {
233 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
234 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
235 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
236 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
237 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
238 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
239 default: return "Unknown";
240 };
241}
242
243unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
244{
Simon Wilson193b1c32011-06-07 23:42:38 -0700245 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700246 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700247
248 return ctl->info->count;
249}
250
Simon Wilson79d39652011-05-25 13:44:23 -0700251static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
252{
253 int range;
Simon Wilson98c1f162011-06-07 16:12:32 -0700254
Simon Wilson79d39652011-05-25 13:44:23 -0700255 if (percent > 100)
256 percent = 100;
257 else if (percent < 0)
258 percent = 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700259
Simon Wilson79d39652011-05-25 13:44:23 -0700260 range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700261
Simon Wilson79d39652011-05-25 13:44:23 -0700262 return ei->value.integer.min + (range * percent) / 100;
263}
264
265static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
266{
267 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700268
Simon Wilson79d39652011-05-25 13:44:23 -0700269 if (range == 0)
270 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700271
Simon Wilson79d39652011-05-25 13:44:23 -0700272 return ((value - ei->value.integer.min) / range) * 100;
273}
274
Simon Wilsond2cb5032011-06-04 00:57:17 -0700275int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700276{
Simon Wilson193b1c32011-06-07 23:42:38 -0700277 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
278 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700279
Simon Wilson066c9f62011-06-05 18:23:05 -0700280 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700281}
282
Simon Wilsond2cb5032011-06-04 00:57:17 -0700283int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700284{
Simon Wilson193b1c32011-06-07 23:42:38 -0700285 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
286 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700287
Simon Wilson066c9f62011-06-05 18:23:05 -0700288 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700289}
290
Simon Wilson066c9f62011-06-05 18:23:05 -0700291int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700292{
Simon Wilson79d39652011-05-25 13:44:23 -0700293 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700294 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700295
Simon Wilson193b1c32011-06-07 23:42:38 -0700296 if (!ctl || (id >= ctl->info->count))
297 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700298
Simon Wilson79d39652011-05-25 13:44:23 -0700299 memset(&ev, 0, sizeof(ev));
300 ev.id.numid = ctl->info->id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700301 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
302 if (ret < 0)
303 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700304
305 switch (ctl->info->type) {
306 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700307 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700308
309 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700310 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700311
312 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
313 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700314
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500315 case SNDRV_CTL_ELEM_TYPE_BYTES:
316 return ev.value.bytes.data[id];
317
Simon Wilson79d39652011-05-25 13:44:23 -0700318 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700319 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700320 }
321
322 return 0;
323}
324
Simon Wilson38f87f32012-10-23 15:05:23 -0700325int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100326{
327 struct snd_ctl_elem_value ev;
328 int ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700329 size_t size;
330 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100331
Simon Wilson38f87f32012-10-23 15:05:23 -0700332 if (!ctl || (count > ctl->info->count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100333 return -EINVAL;
334
335 memset(&ev, 0, sizeof(ev));
336 ev.id.numid = ctl->info->id.numid;
337
338 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
339 if (ret < 0)
340 return ret;
341
Simon Wilson38f87f32012-10-23 15:05:23 -0700342 switch (ctl->info->type) {
343 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
344 case SNDRV_CTL_ELEM_TYPE_INTEGER:
345 size = sizeof(ev.value.integer.value[0]);
346 source = ev.value.integer.value;
347 break;
348
349 case SNDRV_CTL_ELEM_TYPE_BYTES:
350 size = sizeof(ev.value.bytes.data[0]);
351 source = ev.value.bytes.data;
352 break;
353
354 default:
355 return -EINVAL;
356 }
357
358 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100359
360 return 0;
361}
362
Simon Wilson066c9f62011-06-05 18:23:05 -0700363int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700364{
365 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700366 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700367
Simon Wilson193b1c32011-06-07 23:42:38 -0700368 if (!ctl || (id >= ctl->info->count))
369 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700370
371 memset(&ev, 0, sizeof(ev));
372 ev.id.numid = ctl->info->id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700373 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
374 if (ret < 0)
375 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700376
Simon Wilson79d39652011-05-25 13:44:23 -0700377 switch (ctl->info->type) {
378 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700379 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700380 break;
381
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700382 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700383 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700384 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700385
Simon Wilson066c9f62011-06-05 18:23:05 -0700386 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
387 ev.value.enumerated.item[id] = value;
388 break;
389
Simon Wilson79d39652011-05-25 13:44:23 -0700390 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700391 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700392 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700393
Simon Wilson79d39652011-05-25 13:44:23 -0700394 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
395}
396
Simon Wilson38f87f32012-10-23 15:05:23 -0700397int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100398{
399 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700400 size_t size;
401 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100402
Simon Wilson38f87f32012-10-23 15:05:23 -0700403 if (!ctl || (count > ctl->info->count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100404 return -EINVAL;
405
406 memset(&ev, 0, sizeof(ev));
407 ev.id.numid = ctl->info->id.numid;
408
Simon Wilson38f87f32012-10-23 15:05:23 -0700409 switch (ctl->info->type) {
410 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
411 case SNDRV_CTL_ELEM_TYPE_INTEGER:
412 size = sizeof(ev.value.integer.value[0]);
413 dest = ev.value.integer.value;
414 break;
415
416 case SNDRV_CTL_ELEM_TYPE_BYTES:
417 size = sizeof(ev.value.bytes.data[0]);
418 dest = ev.value.bytes.data;
419 break;
420
421 default:
422 return -EINVAL;
423 }
424
425 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100426
427 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
428}
429
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700430int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
431{
Simon Wilson193b1c32011-06-07 23:42:38 -0700432 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
433 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700434
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700435 return ctl->info->value.integer.min;
436}
437
438int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
439{
Simon Wilson193b1c32011-06-07 23:42:38 -0700440 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
441 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700442
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700443 return ctl->info->value.integer.max;
444}
445
Simon Wilson066c9f62011-06-05 18:23:05 -0700446unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
447{
Simon Wilson193b1c32011-06-07 23:42:38 -0700448 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700449 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700450
451 return ctl->info->value.enumerated.items;
452}
453
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800454const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
455 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700456{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700457 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Simon Wilson193b1c32011-06-07 23:42:38 -0700458 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800459 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700460
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800461 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700462}
463
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700464int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
465{
466 unsigned int i, num_enums;
467 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700468 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700469
Simon Wilson193b1c32011-06-07 23:42:38 -0700470 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
471 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700472
473 num_enums = ctl->info->value.enumerated.items;
474 for (i = 0; i < num_enums; i++) {
475 if (!strcmp(string, ctl->ename[i])) {
476 memset(&ev, 0, sizeof(ev));
477 ev.value.enumerated.item[0] = i;
478 ev.id.numid = ctl->info->id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700479 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
480 if (ret < 0)
481 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700482 return 0;
483 }
484 }
485
Simon Wilson193b1c32011-06-07 23:42:38 -0700486 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700487}
488