blob: cb1d5cbf6c0746d1e5a96e0b0dcefb997f7a1dde [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
48#include <tinyalsa/asoundlib.h>
49
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040050/** A mixer control.
51 * @ingroup tinyalsa-mixer
52 */
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.
61 * @ingroup tinyalsa-mixer
62 */
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.
76 * @ingroup tinyalsa-mixer
77 */
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.
107 * @ingroup tinyalsa-mixer
108 */
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;
112 struct snd_ctl_elem_info tmp;
113 struct snd_ctl_elem_id *eid = NULL;
114 struct mixer *mixer = NULL;
115 unsigned int n, m;
116 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -0700117 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -0700118
Simon Wilson1bd580f2011-06-02 15:58:41 -0700119 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
120 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700121 if (fd < 0)
122 return 0;
123
124 memset(&elist, 0, sizeof(elist));
125 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
126 goto fail;
127
128 mixer = calloc(1, sizeof(*mixer));
129 if (!mixer)
130 goto fail;
131
132 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100133 if (!mixer->ctl)
Simon Wilsonec281392013-06-28 16:21:31 -0700134 goto fail;
135
136 if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700137 goto fail;
138
139 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
140 if (!eid)
141 goto fail;
142
143 mixer->count = elist.count;
144 mixer->fd = fd;
145 elist.space = mixer->count;
146 elist.pids = eid;
147 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
148 goto fail;
149
150 for (n = 0; n < mixer->count; n++) {
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100151 struct snd_ctl_elem_info *ei = &mixer->ctl[n].info;
Simon Wilson79d39652011-05-25 13:44:23 -0700152 ei->id.numid = eid[n].numid;
153 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
154 goto fail;
Simon Wilson79d39652011-05-25 13:44:23 -0700155 mixer->ctl[n].mixer = mixer;
156 if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
157 char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
158 if (!enames)
159 goto fail;
160 mixer->ctl[n].ename = enames;
161 for (m = 0; m < ei->value.enumerated.items; m++) {
162 memset(&tmp, 0, sizeof(tmp));
163 tmp.id.numid = ei->id.numid;
164 tmp.value.enumerated.item = m;
165 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
166 goto fail;
167 enames[m] = strdup(tmp.value.enumerated.name);
168 if (!enames[m])
169 goto fail;
170 }
171 }
172 }
173
174 free(eid);
175 return mixer;
176
177fail:
178 /* TODO: verify frees in failure case */
179 if (eid)
180 free(eid);
181 if (mixer)
182 mixer_close(mixer);
183 else if (fd >= 0)
184 close(fd);
185 return 0;
186}
187
Simon Wilsonec281392013-06-28 16:21:31 -0700188const char *mixer_get_name(struct mixer *mixer)
189{
190 return (const char *)mixer->card_info.name;
191}
192
Simon Wilsond2cb5032011-06-04 00:57:17 -0700193unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700194{
Simon Wilson193b1c32011-06-07 23:42:38 -0700195 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700196 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700197
Simon Wilson79d39652011-05-25 13:44:23 -0700198 return mixer->count;
199}
200
201struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
202{
Simon Wilson98c1f162011-06-07 16:12:32 -0700203 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700204 return mixer->ctl + id;
205
206 return NULL;
207}
208
209struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
210{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200211 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
212}
213
214struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
215 const char *name,
216 unsigned int index)
217{
Simon Wilson79d39652011-05-25 13:44:23 -0700218 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200219 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700220
Simon Wilson98c1f162011-06-07 16:12:32 -0700221 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700222 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700223
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200224 ctl = mixer->ctl;
225
Simon Wilson79d39652011-05-25 13:44:23 -0700226 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100227 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200228 if (index-- == 0)
229 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700230
231 return NULL;
232}
233
Simon Wilson710df882013-06-28 16:17:50 -0700234void mixer_ctl_update(struct mixer_ctl *ctl)
235{
236 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
237}
238
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100239unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
240{
241 if (!ctl)
242 return UINT_MAX;
243
244 /* numid values start at 1, return a 0-base value that
245 * can be passed to mixer_get_ctl()
246 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100247 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100248}
249
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800250const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700251{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800252 if (!ctl)
253 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700254
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100255 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700256}
257
258enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
259{
Simon Wilson193b1c32011-06-07 23:42:38 -0700260 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700261 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700262
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100263 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700264 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
265 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
266 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
267 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
268 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
269 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
270 default: return MIXER_CTL_TYPE_UNKNOWN;
271 };
272}
273
274const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
275{
Simon Wilson193b1c32011-06-07 23:42:38 -0700276 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700277 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700278
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100279 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700280 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
281 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
282 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
283 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
284 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
285 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
286 default: return "Unknown";
287 };
288}
289
290unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
291{
Simon Wilson193b1c32011-06-07 23:42:38 -0700292 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700293 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700294
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100295 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700296}
297
Simon Wilson79d39652011-05-25 13:44:23 -0700298static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
299{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200300 if ((percent > 100) || (percent < 0)) {
301 return -EINVAL;
302 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700303
Baptiste Robert4a484e12013-09-12 15:37:53 +0200304 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700305
Simon Wilson79d39652011-05-25 13:44:23 -0700306 return ei->value.integer.min + (range * percent) / 100;
307}
308
309static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
310{
311 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700312
Simon Wilson79d39652011-05-25 13:44:23 -0700313 if (range == 0)
314 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700315
Simon Wilson79d39652011-05-25 13:44:23 -0700316 return ((value - ei->value.integer.min) / range) * 100;
317}
318
Simon Wilsond2cb5032011-06-04 00:57:17 -0700319int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700320{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100321 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700322 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700323
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100324 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700325}
326
Simon Wilsond2cb5032011-06-04 00:57:17 -0700327int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700328{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100329 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700330 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700331
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100332 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700333}
334
Simon Wilson066c9f62011-06-05 18:23:05 -0700335int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700336{
Simon Wilson79d39652011-05-25 13:44:23 -0700337 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700338 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700339
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100340 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700341 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700342
Simon Wilson79d39652011-05-25 13:44:23 -0700343 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100344 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700345 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
346 if (ret < 0)
347 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700348
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100349 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700350 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700351 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700352
353 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700354 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700355
356 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
357 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700358
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500359 case SNDRV_CTL_ELEM_TYPE_BYTES:
360 return ev.value.bytes.data[id];
361
Simon Wilson79d39652011-05-25 13:44:23 -0700362 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700363 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700364 }
365
366 return 0;
367}
368
Simon Wilson38f87f32012-10-23 15:05:23 -0700369int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100370{
371 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530372 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700373 size_t size;
374 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100375
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100376 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100377 return -EINVAL;
378
379 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100380 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100381
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100382 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700383 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
384 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530385 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
386 if (ret < 0)
387 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700388 size = sizeof(ev.value.integer.value[0]);
389 source = ev.value.integer.value;
390 break;
391
392 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530393 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100394 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530395 struct snd_ctl_tlv *tlv;
396 int ret;
397
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700398 if (count > SIZE_MAX - sizeof(*tlv))
399 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530400 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700401 if (!tlv)
402 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100403 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530404 tlv->length = count;
405 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
406
407 source = tlv->tlv;
408 memcpy(array, source, count);
409
410 free(tlv);
411
412 return ret;
413 } else {
414 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
415 if (ret < 0)
416 return ret;
417 size = sizeof(ev.value.bytes.data[0]);
418 source = ev.value.bytes.data;
419 break;
420 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700421
422 default:
423 return -EINVAL;
424 }
425
426 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100427
428 return 0;
429}
430
Simon Wilson066c9f62011-06-05 18:23:05 -0700431int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700432{
433 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700434 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700435
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100436 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700437 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700438
439 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100440 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700441 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
442 if (ret < 0)
443 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700444
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100445 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700446 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700447 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700448 break;
449
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700450 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200451 if ((value < mixer_ctl_get_range_min(ctl)) ||
452 (value > mixer_ctl_get_range_max(ctl))) {
453 return -EINVAL;
454 }
455
Simon Wilsond2cb5032011-06-04 00:57:17 -0700456 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700457 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700458
Simon Wilson066c9f62011-06-05 18:23:05 -0700459 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
460 ev.value.enumerated.item[id] = value;
461 break;
462
David.Coutherutce2b6342013-06-11 16:22:57 +0200463 case SNDRV_CTL_ELEM_TYPE_BYTES:
464 ev.value.bytes.data[id] = value;
465 break;
466
Simon Wilson79d39652011-05-25 13:44:23 -0700467 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700468 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700469 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700470
Simon Wilson79d39652011-05-25 13:44:23 -0700471 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
472}
473
Simon Wilson38f87f32012-10-23 15:05:23 -0700474int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100475{
476 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700477 size_t size;
478 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100479
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100480 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100481 return -EINVAL;
482
483 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100484 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100485
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100486 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700487 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
488 case SNDRV_CTL_ELEM_TYPE_INTEGER:
489 size = sizeof(ev.value.integer.value[0]);
490 dest = ev.value.integer.value;
491 break;
492
493 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530494 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100495 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530496 struct snd_ctl_tlv *tlv;
497 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700498 if (count > SIZE_MAX - sizeof(*tlv))
499 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530500 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700501 if (!tlv)
502 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100503 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530504 tlv->length = count;
505 memcpy(tlv->tlv, array, count);
506
507 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
508 free(tlv);
509
510 return ret;
511 } else {
512 size = sizeof(ev.value.bytes.data[0]);
513 dest = ev.value.bytes.data;
514 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700515 break;
516
517 default:
518 return -EINVAL;
519 }
520
521 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100522
523 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
524}
525
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700526int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
527{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100528 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700529 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700530
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100531 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700532}
533
534int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
535{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100536 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700537 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700538
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100539 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700540}
541
Simon Wilson066c9f62011-06-05 18:23:05 -0700542unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
543{
Simon Wilson193b1c32011-06-07 23:42:38 -0700544 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700545 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700546
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100547 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700548}
549
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800550const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
551 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700552{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100553 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
554 (enum_id >= ctl->info.value.enumerated.items))
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800555 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700556
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800557 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700558}
559
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700560int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
561{
562 unsigned int i, num_enums;
563 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700564 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700565
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100566 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
Simon Wilson193b1c32011-06-07 23:42:38 -0700567 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700568
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100569 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700570 for (i = 0; i < num_enums; i++) {
571 if (!strcmp(string, ctl->ename[i])) {
572 memset(&ev, 0, sizeof(ev));
573 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100574 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700575 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
576 if (ret < 0)
577 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700578 return 0;
579 }
580 }
581
Simon Wilson193b1c32011-06-07 23:42:38 -0700582 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700583}
584