blob: fc9123d02b1e532ccbc545a0976f2198e7faf33b [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;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100191 struct mixer_ctl *ctl = mixer->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
Simon Wilson79d39652011-05-25 13:44:23 -0700196 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100197 if (!strcmp(name, (char*) ctl[n].info.id.name))
198 return &ctl[n];
Simon Wilson79d39652011-05-25 13:44:23 -0700199
200 return NULL;
201}
202
Simon Wilson710df882013-06-28 16:17:50 -0700203void mixer_ctl_update(struct mixer_ctl *ctl)
204{
205 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
206}
207
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100208unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
209{
210 if (!ctl)
211 return UINT_MAX;
212
213 /* numid values start at 1, return a 0-base value that
214 * can be passed to mixer_get_ctl()
215 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100216 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100217}
218
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800219const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700220{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800221 if (!ctl)
222 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700223
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100224 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700225}
226
227enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
228{
Simon Wilson193b1c32011-06-07 23:42:38 -0700229 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700230 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700231
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100232 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700233 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
234 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
235 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
236 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
237 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
238 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
239 default: return MIXER_CTL_TYPE_UNKNOWN;
240 };
241}
242
243const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
244{
Simon Wilson193b1c32011-06-07 23:42:38 -0700245 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700246 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700247
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100248 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700249 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
250 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
251 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
252 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
253 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
254 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
255 default: return "Unknown";
256 };
257}
258
259unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
260{
Simon Wilson193b1c32011-06-07 23:42:38 -0700261 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700262 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700263
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100264 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700265}
266
Simon Wilson79d39652011-05-25 13:44:23 -0700267static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
268{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200269 if ((percent > 100) || (percent < 0)) {
270 return -EINVAL;
271 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700272
Baptiste Robert4a484e12013-09-12 15:37:53 +0200273 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700274
Simon Wilson79d39652011-05-25 13:44:23 -0700275 return ei->value.integer.min + (range * percent) / 100;
276}
277
278static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
279{
280 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700281
Simon Wilson79d39652011-05-25 13:44:23 -0700282 if (range == 0)
283 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700284
Simon Wilson79d39652011-05-25 13:44:23 -0700285 return ((value - ei->value.integer.min) / range) * 100;
286}
287
Simon Wilsond2cb5032011-06-04 00:57:17 -0700288int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700289{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100290 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700291 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700292
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100293 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700294}
295
Simon Wilsond2cb5032011-06-04 00:57:17 -0700296int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700297{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100298 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700299 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700300
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100301 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700302}
303
Simon Wilson066c9f62011-06-05 18:23:05 -0700304int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700305{
Simon Wilson79d39652011-05-25 13:44:23 -0700306 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700307 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700308
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100309 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700310 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700311
Simon Wilson79d39652011-05-25 13:44:23 -0700312 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100313 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700314 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
315 if (ret < 0)
316 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700317
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100318 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700319 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700320 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700321
322 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700323 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700324
325 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
326 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700327
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500328 case SNDRV_CTL_ELEM_TYPE_BYTES:
329 return ev.value.bytes.data[id];
330
Simon Wilson79d39652011-05-25 13:44:23 -0700331 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700332 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700333 }
334
335 return 0;
336}
337
Simon Wilson38f87f32012-10-23 15:05:23 -0700338int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100339{
340 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530341 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700342 size_t size;
343 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100344
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100345 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100346 return -EINVAL;
347
348 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100349 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100350
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100351 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700352 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
353 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530354 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
355 if (ret < 0)
356 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700357 size = sizeof(ev.value.integer.value[0]);
358 source = ev.value.integer.value;
359 break;
360
361 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530362 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100363 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530364 struct snd_ctl_tlv *tlv;
365 int ret;
366
367 tlv = calloc(1, sizeof(*tlv) + count);
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100368 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530369 tlv->length = count;
370 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
371
372 source = tlv->tlv;
373 memcpy(array, source, count);
374
375 free(tlv);
376
377 return ret;
378 } else {
379 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
380 if (ret < 0)
381 return ret;
382 size = sizeof(ev.value.bytes.data[0]);
383 source = ev.value.bytes.data;
384 break;
385 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700386
387 default:
388 return -EINVAL;
389 }
390
391 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100392
393 return 0;
394}
395
Simon Wilson066c9f62011-06-05 18:23:05 -0700396int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700397{
398 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700399 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700400
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100401 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700402 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700403
404 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100405 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700406 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
407 if (ret < 0)
408 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700409
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100410 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700411 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700412 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700413 break;
414
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700415 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200416 if ((value < mixer_ctl_get_range_min(ctl)) ||
417 (value > mixer_ctl_get_range_max(ctl))) {
418 return -EINVAL;
419 }
420
Simon Wilsond2cb5032011-06-04 00:57:17 -0700421 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700422 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700423
Simon Wilson066c9f62011-06-05 18:23:05 -0700424 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
425 ev.value.enumerated.item[id] = value;
426 break;
427
David.Coutherutce2b6342013-06-11 16:22:57 +0200428 case SNDRV_CTL_ELEM_TYPE_BYTES:
429 ev.value.bytes.data[id] = value;
430 break;
431
Simon Wilson79d39652011-05-25 13:44:23 -0700432 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700433 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700434 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700435
Simon Wilson79d39652011-05-25 13:44:23 -0700436 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
437}
438
Simon Wilson38f87f32012-10-23 15:05:23 -0700439int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100440{
441 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700442 size_t size;
443 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100444
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100445 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100446 return -EINVAL;
447
448 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100449 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100450
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100451 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700452 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
453 case SNDRV_CTL_ELEM_TYPE_INTEGER:
454 size = sizeof(ev.value.integer.value[0]);
455 dest = ev.value.integer.value;
456 break;
457
458 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530459 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100460 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530461 struct snd_ctl_tlv *tlv;
462 int ret = 0;
463 tlv = calloc(1, sizeof(*tlv) + count);
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100464 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530465 tlv->length = count;
466 memcpy(tlv->tlv, array, count);
467
468 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
469 free(tlv);
470
471 return ret;
472 } else {
473 size = sizeof(ev.value.bytes.data[0]);
474 dest = ev.value.bytes.data;
475 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700476 break;
477
478 default:
479 return -EINVAL;
480 }
481
482 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100483
484 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
485}
486
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700487int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
488{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100489 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700490 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700491
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100492 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700493}
494
495int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
496{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100497 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700498 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700499
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100500 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700501}
502
Simon Wilson066c9f62011-06-05 18:23:05 -0700503unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
504{
Simon Wilson193b1c32011-06-07 23:42:38 -0700505 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700506 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700507
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100508 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700509}
510
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800511const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
512 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700513{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100514 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
515 (enum_id >= ctl->info.value.enumerated.items))
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800516 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700517
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800518 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700519}
520
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700521int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
522{
523 unsigned int i, num_enums;
524 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700525 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700526
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100527 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
Simon Wilson193b1c32011-06-07 23:42:38 -0700528 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700529
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100530 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700531 for (i = 0; i < num_enums; i++) {
532 if (!strcmp(string, ctl->ename[i])) {
533 memset(&ev, 0, sizeof(ev));
534 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100535 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700536 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
537 if (ret < 0)
538 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700539 return 0;
540 }
541 }
542
Simon Wilson193b1c32011-06-07 23:42:38 -0700543 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700544}
545