blob: 7c5c5dc39f03647020c6755f997f1342c9d519cd [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>
Richard Fitzgerald57a87742014-09-09 16:54:12 +010036#include <limits.h>
Simon Wilson79d39652011-05-25 13:44:23 -070037
Simon Wilson6a52f2c2012-05-04 16:32:10 -070038#include <sys/ioctl.h>
39
Simon Wilson79d39652011-05-25 13:44:23 -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
Simon Wilson79d39652011-05-25 13:44:23 -070048struct mixer_ctl {
49 struct mixer *mixer;
Richard Fitzgerald899cece2014-09-09 17:03:21 +010050 struct snd_ctl_elem_info info;
Simon Wilson79d39652011-05-25 13:44:23 -070051 char **ename;
52};
53
54struct mixer {
55 int fd;
Simon Wilsonec281392013-06-28 16:21:31 -070056 struct snd_ctl_card_info card_info;
Simon Wilson79d39652011-05-25 13:44:23 -070057 struct mixer_ctl *ctl;
58 unsigned int count;
59};
60
61void mixer_close(struct mixer *mixer)
62{
63 unsigned int n,m;
64
Simon Wilson193b1c32011-06-07 23:42:38 -070065 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070066 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070067
Simon Wilson79d39652011-05-25 13:44:23 -070068 if (mixer->fd >= 0)
69 close(mixer->fd);
70
71 if (mixer->ctl) {
72 for (n = 0; n < mixer->count; n++) {
73 if (mixer->ctl[n].ename) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +010074 unsigned int max = mixer->ctl[n].info.value.enumerated.items;
Simon Wilson79d39652011-05-25 13:44:23 -070075 for (m = 0; m < max; m++)
76 free(mixer->ctl[n].ename[m]);
77 free(mixer->ctl[n].ename);
78 }
79 }
80 free(mixer->ctl);
81 }
82
Simon Wilson79d39652011-05-25 13:44:23 -070083 free(mixer);
84
85 /* TODO: verify frees */
86}
87
Simon Wilson1bd580f2011-06-02 15:58:41 -070088struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -070089{
90 struct snd_ctl_elem_list elist;
91 struct snd_ctl_elem_info tmp;
92 struct snd_ctl_elem_id *eid = NULL;
93 struct mixer *mixer = NULL;
94 unsigned int n, m;
95 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -070096 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -070097
Simon Wilson1bd580f2011-06-02 15:58:41 -070098 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
99 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700100 if (fd < 0)
101 return 0;
102
103 memset(&elist, 0, sizeof(elist));
104 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
105 goto fail;
106
107 mixer = calloc(1, sizeof(*mixer));
108 if (!mixer)
109 goto fail;
110
111 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100112 if (!mixer->ctl)
Simon Wilsonec281392013-06-28 16:21:31 -0700113 goto fail;
114
115 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700116 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++) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100130 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
Simon Wilson79d39652011-05-25 13:44:23 -0700131 ei->id.numid = eid[n].numid;
132 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
133 goto fail;
Simon Wilson79d39652011-05-25 13:44:23 -0700134 mixer->ctl[n].mixer = mixer;
135 if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
136 char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
137 if (!enames)
138 goto fail;
139 mixer->ctl[n].ename = enames;
140 for (m = 0; m < ei->value.enumerated.items; m++) {
141 memset(&tmp, 0, sizeof(tmp));
142 tmp.id.numid = ei->id.numid;
143 tmp.value.enumerated.item = m;
144 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
145 goto fail;
146 enames[m] = strdup(tmp.value.enumerated.name);
147 if (!enames[m])
148 goto fail;
149 }
150 }
151 }
152
153 free(eid);
154 return mixer;
155
156fail:
157 /* TODO: verify frees in failure case */
158 if (eid)
159 free(eid);
160 if (mixer)
161 mixer_close(mixer);
162 else if (fd >= 0)
163 close(fd);
164 return 0;
165}
166
Simon Wilsonec281392013-06-28 16:21:31 -0700167const char *mixer_get_name(struct mixer *mixer)
168{
169 return (const char *)mixer->card_info.name;
170}
171
Simon Wilsond2cb5032011-06-04 00:57:17 -0700172unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700173{
Simon Wilson193b1c32011-06-07 23:42:38 -0700174 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700175 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700176
Simon Wilson79d39652011-05-25 13:44:23 -0700177 return mixer->count;
178}
179
180struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
181{
Simon Wilson98c1f162011-06-07 16:12:32 -0700182 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700183 return mixer->ctl + id;
184
185 return NULL;
186}
187
188struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
189{
190 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200191 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700192
Simon Wilson98c1f162011-06-07 16:12:32 -0700193 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700194 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700195
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200196 ctl = mixer->ctl;
197
Simon Wilson79d39652011-05-25 13:44:23 -0700198 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100199 if (!strcmp(name, (char*) ctl[n].info.id.name))
200 return &ctl[n];
Simon Wilson79d39652011-05-25 13:44:23 -0700201
202 return NULL;
203}
204
Simon Wilson710df882013-06-28 16:17:50 -0700205void mixer_ctl_update(struct mixer_ctl *ctl)
206{
207 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
208}
209
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100210unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
211{
212 if (!ctl)
213 return UINT_MAX;
214
215 /* numid values start at 1, return a 0-base value that
216 * can be passed to mixer_get_ctl()
217 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100218 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100219}
220
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800221const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700222{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800223 if (!ctl)
224 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700225
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100226 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700227}
228
229enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
230{
Simon Wilson193b1c32011-06-07 23:42:38 -0700231 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700232 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700233
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100234 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700235 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
236 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
237 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
238 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
239 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
240 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
241 default: return MIXER_CTL_TYPE_UNKNOWN;
242 };
243}
244
245const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
246{
Simon Wilson193b1c32011-06-07 23:42:38 -0700247 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700248 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700249
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100250 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700251 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
252 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
253 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
254 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
255 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
256 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
257 default: return "Unknown";
258 };
259}
260
261unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
262{
Simon Wilson193b1c32011-06-07 23:42:38 -0700263 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700264 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700265
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100266 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700267}
268
Simon Wilson79d39652011-05-25 13:44:23 -0700269static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
270{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200271 if ((percent > 100) || (percent < 0)) {
272 return -EINVAL;
273 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700274
Baptiste Robert4a484e12013-09-12 15:37:53 +0200275 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700276
Simon Wilson79d39652011-05-25 13:44:23 -0700277 return ei->value.integer.min + (range * percent) / 100;
278}
279
280static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
281{
282 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700283
Simon Wilson79d39652011-05-25 13:44:23 -0700284 if (range == 0)
285 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700286
Simon Wilson79d39652011-05-25 13:44:23 -0700287 return ((value - ei->value.integer.min) / range) * 100;
288}
289
Simon Wilsond2cb5032011-06-04 00:57:17 -0700290int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700291{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100292 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700293 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700294
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100295 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700296}
297
Simon Wilsond2cb5032011-06-04 00:57:17 -0700298int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700299{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100300 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700301 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700302
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100303 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700304}
305
Simon Wilson066c9f62011-06-05 18:23:05 -0700306int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700307{
Simon Wilson79d39652011-05-25 13:44:23 -0700308 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700309 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700310
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100311 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700312 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700313
Simon Wilson79d39652011-05-25 13:44:23 -0700314 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100315 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700316 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
317 if (ret < 0)
318 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700319
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100320 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700321 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700322 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700323
324 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700325 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700326
327 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
328 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700329
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500330 case SNDRV_CTL_ELEM_TYPE_BYTES:
331 return ev.value.bytes.data[id];
332
Simon Wilson79d39652011-05-25 13:44:23 -0700333 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700334 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700335 }
336
337 return 0;
338}
339
Simon Wilson38f87f32012-10-23 15:05:23 -0700340int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100341{
342 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530343 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700344 size_t size;
345 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100346
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100347 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100348 return -EINVAL;
349
350 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100351 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100352
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100353 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700354 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
355 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530356 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
357 if (ret < 0)
358 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700359 size = sizeof(ev.value.integer.value[0]);
360 source = ev.value.integer.value;
361 break;
362
363 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530364 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100365 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530366 struct snd_ctl_tlv *tlv;
367 int ret;
368
369 tlv = calloc(1, sizeof(*tlv) + count);
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100370 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530371 tlv->length = count;
372 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
373
374 source = tlv->tlv;
375 memcpy(array, source, count);
376
377 free(tlv);
378
379 return ret;
380 } else {
381 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
382 if (ret < 0)
383 return ret;
384 size = sizeof(ev.value.bytes.data[0]);
385 source = ev.value.bytes.data;
386 break;
387 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700388
389 default:
390 return -EINVAL;
391 }
392
393 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100394
395 return 0;
396}
397
Simon Wilson066c9f62011-06-05 18:23:05 -0700398int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700399{
400 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700401 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700402
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100403 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700404 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700405
406 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100407 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700408 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
409 if (ret < 0)
410 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700411
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100412 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700413 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700414 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700415 break;
416
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700417 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200418 if ((value < mixer_ctl_get_range_min(ctl)) ||
419 (value > mixer_ctl_get_range_max(ctl))) {
420 return -EINVAL;
421 }
422
Simon Wilsond2cb5032011-06-04 00:57:17 -0700423 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700424 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700425
Simon Wilson066c9f62011-06-05 18:23:05 -0700426 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
427 ev.value.enumerated.item[id] = value;
428 break;
429
David.Coutherutce2b6342013-06-11 16:22:57 +0200430 case SNDRV_CTL_ELEM_TYPE_BYTES:
431 ev.value.bytes.data[id] = value;
432 break;
433
Simon Wilson79d39652011-05-25 13:44:23 -0700434 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700435 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700436 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700437
Simon Wilson79d39652011-05-25 13:44:23 -0700438 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
439}
440
Simon Wilson38f87f32012-10-23 15:05:23 -0700441int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100442{
443 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700444 size_t size;
445 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100446
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100447 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100448 return -EINVAL;
449
450 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100451 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100452
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100453 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700454 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
455 case SNDRV_CTL_ELEM_TYPE_INTEGER:
456 size = sizeof(ev.value.integer.value[0]);
457 dest = ev.value.integer.value;
458 break;
459
460 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530461 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100462 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530463 struct snd_ctl_tlv *tlv;
464 int ret = 0;
465 tlv = calloc(1, sizeof(*tlv) + count);
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100466 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530467 tlv->length = count;
468 memcpy(tlv->tlv, array, count);
469
470 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
471 free(tlv);
472
473 return ret;
474 } else {
475 size = sizeof(ev.value.bytes.data[0]);
476 dest = ev.value.bytes.data;
477 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700478 break;
479
480 default:
481 return -EINVAL;
482 }
483
484 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100485
486 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
487}
488
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700489int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
490{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100491 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700492 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700493
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100494 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700495}
496
497int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
498{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100499 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700500 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700501
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100502 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700503}
504
Simon Wilson066c9f62011-06-05 18:23:05 -0700505unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
506{
Simon Wilson193b1c32011-06-07 23:42:38 -0700507 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700508 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700509
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100510 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700511}
512
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800513const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
514 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700515{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100516 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
517 (enum_id >= ctl->info.value.enumerated.items))
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800518 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700519
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800520 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700521}
522
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700523int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
524{
525 unsigned int i, num_enums;
526 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700527 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700528
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100529 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
Simon Wilson193b1c32011-06-07 23:42:38 -0700530 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700531
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100532 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700533 for (i = 0; i < num_enums; i++) {
534 if (!strcmp(string, ctl->ename[i])) {
535 memset(&ev, 0, sizeof(ev));
536 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100537 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700538 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
539 if (ret < 0)
540 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700541 return 0;
542 }
543 }
544
Simon Wilson193b1c32011-06-07 23:42:38 -0700545 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700546}
547