blob: 58bf916094619d39e2eb21565b2453a29286babb [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>
Ben Zhang7ed2ffb2016-04-22 17:59:40 -070031#include <stdint.h>
Simon Wilson79d39652011-05-25 13:44:23 -070032#include <string.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <errno.h>
36#include <ctype.h>
Richard Fitzgerald57a87742014-09-09 16:54:12 +010037#include <limits.h>
Dima Krasner696c4482016-03-05 19:50:02 +020038#include <time.h>
Simon Wilson79d39652011-05-25 13:44:23 -070039
Simon Wilson6a52f2c2012-05-04 16:32:10 -070040#include <sys/ioctl.h>
41
Simon Wilson79d39652011-05-25 13:44:23 -070042#include <linux/ioctl.h>
43#define __force
44#define __bitwise
45#define __user
46#include <sound/asound.h>
47
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030048#include <tinyalsa/mixer.h>
Simon Wilson79d39652011-05-25 13:44:23 -070049
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040050/** A mixer control.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080051 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040052 */
Simon Wilson79d39652011-05-25 13:44:23 -070053struct mixer_ctl {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040054 /** The mixer that the mixer control belongs to */
Simon Wilson79d39652011-05-25 13:44:23 -070055 struct mixer *mixer;
Richard Fitzgerald899cece2014-09-09 17:03:21 +010056 struct snd_ctl_elem_info info;
Simon Wilson79d39652011-05-25 13:44:23 -070057 char **ename;
58};
59
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040060/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080061 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040062 */
Simon Wilson79d39652011-05-25 13:44:23 -070063struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040064 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070065 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040066 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070067 struct snd_ctl_card_info card_info;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040068 /** A continuous array of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070069 struct mixer_ctl *ctl;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040070 /** The number of mixer controls */
Simon Wilson79d39652011-05-25 13:44:23 -070071 unsigned int count;
72};
73
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040074/** Closes a mixer returned by @ref mixer_open.
75 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080076 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040077 */
Simon Wilson79d39652011-05-25 13:44:23 -070078void mixer_close(struct mixer *mixer)
79{
80 unsigned int n,m;
81
Simon Wilson193b1c32011-06-07 23:42:38 -070082 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -070083 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -070084
Simon Wilson79d39652011-05-25 13:44:23 -070085 if (mixer->fd >= 0)
86 close(mixer->fd);
87
88 if (mixer->ctl) {
89 for (n = 0; n < mixer->count; n++) {
90 if (mixer->ctl[n].ename) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +010091 unsigned int max = mixer->ctl[n].info.value.enumerated.items;
Simon Wilson79d39652011-05-25 13:44:23 -070092 for (m = 0; m < max; m++)
93 free(mixer->ctl[n].ename[m]);
94 free(mixer->ctl[n].ename);
95 }
96 }
97 free(mixer->ctl);
98 }
99
Simon Wilson79d39652011-05-25 13:44:23 -0700100 free(mixer);
101
102 /* TODO: verify frees */
103}
104
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400105/** Opens a mixer for a given card.
106 * @param card The card to open the mixer for.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800107 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400108 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700109struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700110{
111 struct snd_ctl_elem_list elist;
Simon Wilson79d39652011-05-25 13:44:23 -0700112 struct snd_ctl_elem_id *eid = NULL;
113 struct mixer *mixer = NULL;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400114 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -0700115 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700116 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700117
Simon Wilson1bd580f2011-06-02 15:58:41 -0700118 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
119 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700120 if (fd < 0)
121 return 0;
122
123 memset(&elist, 0, sizeof(elist));
124 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
125 goto fail;
126
127 mixer = calloc(1, sizeof(*mixer));
128 if (!mixer)
129 goto fail;
130
131 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100132 if (!mixer->ctl)
Simon Wilsonec281392013-06-28 16:21:31 -0700133 goto fail;
134
135 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700136 goto fail;
137
138 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
139 if (!eid)
140 goto fail;
141
142 mixer->count = elist.count;
143 mixer->fd = fd;
144 elist.space = mixer->count;
145 elist.pids = eid;
146 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
147 goto fail;
148
149 for (n = 0; n < mixer->count; n++) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100150 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
Simon Wilson79d39652011-05-25 13:44:23 -0700151 ei->id.numid = eid[n].numid;
152 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
153 goto fail;
Simon Wilson79d39652011-05-25 13:44:23 -0700154 mixer->ctl[n].mixer = mixer;
Simon Wilson79d39652011-05-25 13:44:23 -0700155 }
156
157 free(eid);
158 return mixer;
159
160fail:
161 /* TODO: verify frees in failure case */
162 if (eid)
163 free(eid);
164 if (mixer)
165 mixer_close(mixer);
166 else if (fd >= 0)
167 close(fd);
168 return 0;
169}
170
Simon Wilsonec281392013-06-28 16:21:31 -0700171const char *mixer_get_name(struct mixer *mixer)
172{
173 return (const char *)mixer->card_info.name;
174}
175
Simon Wilsond2cb5032011-06-04 00:57:17 -0700176unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700177{
Simon Wilson193b1c32011-06-07 23:42:38 -0700178 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700179 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700180
Simon Wilson79d39652011-05-25 13:44:23 -0700181 return mixer->count;
182}
183
184struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
185{
Simon Wilson98c1f162011-06-07 16:12:32 -0700186 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700187 return mixer->ctl + id;
188
189 return NULL;
190}
191
192struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
193{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200194 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
195}
196
197struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
198 const char *name,
199 unsigned int index)
200{
Simon Wilson79d39652011-05-25 13:44:23 -0700201 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200202 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700203
Simon Wilson98c1f162011-06-07 16:12:32 -0700204 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700205 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700206
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200207 ctl = mixer->ctl;
208
Simon Wilson79d39652011-05-25 13:44:23 -0700209 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100210 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200211 if (index-- == 0)
212 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700213
214 return NULL;
215}
216
Simon Wilson710df882013-06-28 16:17:50 -0700217void mixer_ctl_update(struct mixer_ctl *ctl)
218{
219 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
220}
221
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100222unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
223{
224 if (!ctl)
225 return UINT_MAX;
226
227 /* numid values start at 1, return a 0-base value that
228 * can be passed to mixer_get_ctl()
229 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100230 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100231}
232
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800233const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700234{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800235 if (!ctl)
236 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700237
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100238 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700239}
240
241enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
242{
Simon Wilson193b1c32011-06-07 23:42:38 -0700243 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700244 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700245
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100246 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700247 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
248 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
249 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
250 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
251 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
252 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
253 default: return MIXER_CTL_TYPE_UNKNOWN;
254 };
255}
256
257const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
258{
Simon Wilson193b1c32011-06-07 23:42:38 -0700259 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700260 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700261
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100262 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700263 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
264 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
265 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
266 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
267 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
268 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
269 default: return "Unknown";
270 };
271}
272
273unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
274{
Simon Wilson193b1c32011-06-07 23:42:38 -0700275 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700276 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700277
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100278 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700279}
280
Simon Wilson79d39652011-05-25 13:44:23 -0700281static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
282{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200283 if ((percent > 100) || (percent < 0)) {
284 return -EINVAL;
285 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700286
Baptiste Robert4a484e12013-09-12 15:37:53 +0200287 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700288
Simon Wilson79d39652011-05-25 13:44:23 -0700289 return ei->value.integer.min + (range * percent) / 100;
290}
291
292static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
293{
294 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700295
Simon Wilson79d39652011-05-25 13:44:23 -0700296 if (range == 0)
297 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700298
Simon Wilson79d39652011-05-25 13:44:23 -0700299 return ((value - ei->value.integer.min) / range) * 100;
300}
301
Simon Wilsond2cb5032011-06-04 00:57:17 -0700302int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700303{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100304 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700305 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700306
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100307 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700308}
309
Simon Wilsond2cb5032011-06-04 00:57:17 -0700310int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700311{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100312 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700313 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700314
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100315 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700316}
317
Simon Wilson066c9f62011-06-05 18:23:05 -0700318int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700319{
Simon Wilson79d39652011-05-25 13:44:23 -0700320 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700321 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700322
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100323 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700324 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700325
Simon Wilson79d39652011-05-25 13:44:23 -0700326 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100327 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700328 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
329 if (ret < 0)
330 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700331
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100332 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700333 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700334 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700335
336 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700337 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700338
339 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
340 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700341
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500342 case SNDRV_CTL_ELEM_TYPE_BYTES:
343 return ev.value.bytes.data[id];
344
Simon Wilson79d39652011-05-25 13:44:23 -0700345 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700346 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700347 }
348
349 return 0;
350}
351
Simon Wilson38f87f32012-10-23 15:05:23 -0700352int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100353{
354 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530355 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700356 size_t size;
357 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100358
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100359 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100360 return -EINVAL;
361
362 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100363 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100364
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100365 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700366 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
367 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530368 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
369 if (ret < 0)
370 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700371 size = sizeof(ev.value.integer.value[0]);
372 source = ev.value.integer.value;
373 break;
374
375 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530376 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100377 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530378 struct snd_ctl_tlv *tlv;
379 int ret;
380
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700381 if (count > SIZE_MAX - sizeof(*tlv))
382 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530383 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700384 if (!tlv)
385 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100386 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530387 tlv->length = count;
388 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
389
390 source = tlv->tlv;
391 memcpy(array, source, count);
392
393 free(tlv);
394
395 return ret;
396 } else {
397 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
398 if (ret < 0)
399 return ret;
400 size = sizeof(ev.value.bytes.data[0]);
401 source = ev.value.bytes.data;
402 break;
403 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700404
405 default:
406 return -EINVAL;
407 }
408
409 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100410
411 return 0;
412}
413
Simon Wilson066c9f62011-06-05 18:23:05 -0700414int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700415{
416 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700417 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700418
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100419 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700420 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700421
422 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100423 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700424 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
425 if (ret < 0)
426 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700427
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100428 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700429 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700430 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700431 break;
432
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700433 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200434 if ((value < mixer_ctl_get_range_min(ctl)) ||
435 (value > mixer_ctl_get_range_max(ctl))) {
436 return -EINVAL;
437 }
438
Simon Wilsond2cb5032011-06-04 00:57:17 -0700439 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700440 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700441
Simon Wilson066c9f62011-06-05 18:23:05 -0700442 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
443 ev.value.enumerated.item[id] = value;
444 break;
445
David.Coutherutce2b6342013-06-11 16:22:57 +0200446 case SNDRV_CTL_ELEM_TYPE_BYTES:
447 ev.value.bytes.data[id] = value;
448 break;
449
Simon Wilson79d39652011-05-25 13:44:23 -0700450 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700451 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700452 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700453
Simon Wilson79d39652011-05-25 13:44:23 -0700454 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
455}
456
Simon Wilson38f87f32012-10-23 15:05:23 -0700457int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100458{
459 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700460 size_t size;
461 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100462
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100463 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100464 return -EINVAL;
465
466 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100467 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100468
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100469 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700470 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
471 case SNDRV_CTL_ELEM_TYPE_INTEGER:
472 size = sizeof(ev.value.integer.value[0]);
473 dest = ev.value.integer.value;
474 break;
475
476 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530477 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100478 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530479 struct snd_ctl_tlv *tlv;
480 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700481 if (count > SIZE_MAX - sizeof(*tlv))
482 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530483 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700484 if (!tlv)
485 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100486 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530487 tlv->length = count;
488 memcpy(tlv->tlv, array, count);
489
490 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
491 free(tlv);
492
493 return ret;
494 } else {
495 size = sizeof(ev.value.bytes.data[0]);
496 dest = ev.value.bytes.data;
497 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700498 break;
499
500 default:
501 return -EINVAL;
502 }
503
504 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100505
506 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
507}
508
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700509int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
510{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100511 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700512 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700513
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100514 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700515}
516
517int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
518{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100519 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700520 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700521
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100522 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700523}
524
Simon Wilson066c9f62011-06-05 18:23:05 -0700525unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
526{
Simon Wilson193b1c32011-06-07 23:42:38 -0700527 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700528 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700529
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100530 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700531}
532
David.Coutherut9b423962013-06-03 13:45:51 +0200533int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
534{
535 struct snd_ctl_elem_info tmp;
536 unsigned int m;
537 char **enames;
538
539 if (ctl->ename) {
540 return 0;
541 }
542
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400543 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200544 if (!enames)
545 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400546 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200547 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400548 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200549 tmp.value.enumerated.item = m;
550 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
551 goto fail;
552 enames[m] = strdup(tmp.value.enumerated.name);
553 if (!enames[m])
554 goto fail;
555 }
556 ctl->ename = enames;
557 return 0;
558
559fail:
560 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400561 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200562 if (enames[m]) {
563 free(enames[m]);
564 }
565 }
566 free(enames);
567 }
568 return -1;
569}
570
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800571const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
572 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700573{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100574 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400575 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200576 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800577 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700578
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800579 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700580}
581
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700582int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
583{
584 unsigned int i, num_enums;
585 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700586 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700587
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400588 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200589 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700590 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700591
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100592 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700593 for (i = 0; i < num_enums; i++) {
594 if (!strcmp(string, ctl->ename[i])) {
595 memset(&ev, 0, sizeof(ev));
596 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100597 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700598 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
599 if (ret < 0)
600 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700601 return 0;
602 }
603 }
604
Simon Wilson193b1c32011-06-07 23:42:38 -0700605 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700606}
607