blob: e6f50bb74734986346bb1d9976ba819bef7885c8 [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 Holbertonb7a28572016-11-19 23:45:00 -0500107 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800108 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400109 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700110struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700111{
112 struct snd_ctl_elem_list elist;
Simon Wilson79d39652011-05-25 13:44:23 -0700113 struct snd_ctl_elem_id *eid = NULL;
114 struct mixer *mixer = NULL;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400115 unsigned int n;
Simon Wilson79d39652011-05-25 13:44:23 -0700116 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;
Simon Wilson79d39652011-05-25 13:44:23 -0700156 }
157
158 free(eid);
159 return mixer;
160
161fail:
162 /* TODO: verify frees in failure case */
163 if (eid)
164 free(eid);
165 if (mixer)
166 mixer_close(mixer);
167 else if (fd >= 0)
168 close(fd);
169 return 0;
170}
171
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500172/** Gets the name of the mixer's card.
173 * @param mixer An initialized mixer handle.
174 * @returns The name of the mixer's card.
175 * @ingroup libtinyalsa-mixer
176 */
Simon Wilsonec281392013-06-28 16:21:31 -0700177const char *mixer_get_name(struct mixer *mixer)
178{
179 return (const char *)mixer->card_info.name;
180}
181
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500182/** Gets the number of mixer controls for a given mixer.
183 * @param mixer An initialized mixer handle.
184 * @returns The number of mixer controls for the given mixer.
185 * @ingroup libtinyalsa-mixer
186 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700187unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700188{
Simon Wilson193b1c32011-06-07 23:42:38 -0700189 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700190 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700191
Simon Wilson79d39652011-05-25 13:44:23 -0700192 return mixer->count;
193}
194
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500195/** Gets a mixer control handle, by the mixer control's id.
196 * @param mixer An initialized mixer handle.
197 * @param id The control's id in the given mixer.
198 * @returns A handle to the mixer control.
199 * @ingroup libtinyalsa-mixer
200 */
Simon Wilson79d39652011-05-25 13:44:23 -0700201struct 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
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500209/** Gets the first instance of mixer control handle, by the mixer control's name.
210 * @param mixer An initialized mixer handle.
211 * @param name The control's name in the given mixer.
212 * @returns A handle to the mixer control.
213 * @ingroup libtinyalsa-mixer
214 */
Simon Wilson79d39652011-05-25 13:44:23 -0700215struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
216{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200217 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
218}
219
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500220/** Gets an instance of mixer control handle, by the mixer control's name and index.
221 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
222 * @param mixer An initialized mixer handle.
223 * @param name The control's name in the given mixer.
224 * @param index The control's index.
225 * @returns A handle to the mixer control.
226 * @ingroup libtinyalsa-mixer
227 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200228struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
229 const char *name,
230 unsigned int index)
231{
Simon Wilson79d39652011-05-25 13:44:23 -0700232 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200233 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700234
Simon Wilson98c1f162011-06-07 16:12:32 -0700235 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700236 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700237
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200238 ctl = mixer->ctl;
239
Simon Wilson79d39652011-05-25 13:44:23 -0700240 for (n = 0; n < mixer->count; n++)
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100241 if (!strcmp(name, (char*) ctl[n].info.id.name))
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200242 if (index-- == 0)
243 return mixer->ctl + n;
Simon Wilson79d39652011-05-25 13:44:23 -0700244
245 return NULL;
246}
247
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500248/** Updates the control's info.
249 * This is useful for a program that may be idle for a period of time.
250 * @param ctl An initialized control handle.
251 * @ingroup libtinyalsa-mixer
252 */
Simon Wilson710df882013-06-28 16:17:50 -0700253void mixer_ctl_update(struct mixer_ctl *ctl)
254{
255 ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
256}
257
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500258/** Gets the control's ID.
259 * @param ctl An initialized control handle.
260 * @returns On success, the control's ID is returned.
261 * On error, UINT_MAX is returned instead.
262 * @ingroup libtinyalsa-mixer
263 */
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100264unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl)
265{
266 if (!ctl)
267 return UINT_MAX;
268
269 /* numid values start at 1, return a 0-base value that
270 * can be passed to mixer_get_ctl()
271 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100272 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100273}
274
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500275/** Gets the name of the control.
276 * @param ctl An initialized control handle.
277 * @returns On success, the name of the control.
278 * On error, NULL.
279 * @ingroup libtinyalsa-mixer
280 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800281const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700282{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800283 if (!ctl)
284 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700285
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100286 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700287}
288
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500289/** Gets the value type of the control.
290 * @param ctl An initialized control handle
291 * @returns On success, the type of mixer control.
292 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
293 * @ingroup libtinyalsa-mixer
294 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700295enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
296{
Simon Wilson193b1c32011-06-07 23:42:38 -0700297 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700298 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700299
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100300 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700301 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
302 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
303 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
304 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
305 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
306 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
307 default: return MIXER_CTL_TYPE_UNKNOWN;
308 };
309}
310
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500311/** Gets the string that describes the value type of the control.
312 * @param ctl An initialized control handle
313 * @returns On success, a string describing type of mixer control.
314 * @ingroup libtinyalsa-mixer
315 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700316const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
317{
Simon Wilson193b1c32011-06-07 23:42:38 -0700318 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700319 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700320
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100321 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700322 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
323 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
324 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
325 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
326 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
327 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
328 default: return "Unknown";
329 };
330}
331
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500332/** Gets the number of values in the control.
333 * @param ctl An initialized control handle
334 * @returns The number of values in the mixer control
335 * @ingroup libtinyalsa-mixer
336 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700337unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
338{
Simon Wilson193b1c32011-06-07 23:42:38 -0700339 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700340 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700341
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100342 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700343}
344
Simon Wilson79d39652011-05-25 13:44:23 -0700345static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
346{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200347 if ((percent > 100) || (percent < 0)) {
348 return -EINVAL;
349 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700350
Baptiste Robert4a484e12013-09-12 15:37:53 +0200351 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700352
Simon Wilson79d39652011-05-25 13:44:23 -0700353 return ei->value.integer.min + (range * percent) / 100;
354}
355
356static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
357{
358 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700359
Simon Wilson79d39652011-05-25 13:44:23 -0700360 if (range == 0)
361 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700362
Simon Wilson79d39652011-05-25 13:44:23 -0700363 return ((value - ei->value.integer.min) / range) * 100;
364}
365
Simon Wilsond2cb5032011-06-04 00:57:17 -0700366int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700367{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100368 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700369 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700370
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100371 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700372}
373
Simon Wilsond2cb5032011-06-04 00:57:17 -0700374int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700375{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100376 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700377 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700378
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100379 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700380}
381
Simon Wilson066c9f62011-06-05 18:23:05 -0700382int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700383{
Simon Wilson79d39652011-05-25 13:44:23 -0700384 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700385 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700386
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100387 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700388 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700389
Simon Wilson79d39652011-05-25 13:44:23 -0700390 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100391 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700392 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
393 if (ret < 0)
394 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700395
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100396 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700397 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700398 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700399
400 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700401 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700402
403 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
404 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700405
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500406 case SNDRV_CTL_ELEM_TYPE_BYTES:
407 return ev.value.bytes.data[id];
408
Simon Wilson79d39652011-05-25 13:44:23 -0700409 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700410 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700411 }
412
413 return 0;
414}
415
Simon Wilson38f87f32012-10-23 15:05:23 -0700416int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100417{
418 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530419 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700420 size_t size;
421 void *source;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100422
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100423 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100424 return -EINVAL;
425
426 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100427 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100428
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100429 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700430 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
431 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Mythri P K45b2d042014-08-18 15:39:36 +0530432 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
433 if (ret < 0)
434 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700435 size = sizeof(ev.value.integer.value[0]);
436 source = ev.value.integer.value;
437 break;
438
439 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530440 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100441 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530442 struct snd_ctl_tlv *tlv;
443 int ret;
444
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700445 if (count > SIZE_MAX - sizeof(*tlv))
446 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530447 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700448 if (!tlv)
449 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100450 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530451 tlv->length = count;
452 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv);
453
454 source = tlv->tlv;
455 memcpy(array, source, count);
456
457 free(tlv);
458
459 return ret;
460 } else {
461 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
462 if (ret < 0)
463 return ret;
464 size = sizeof(ev.value.bytes.data[0]);
465 source = ev.value.bytes.data;
466 break;
467 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700468
469 default:
470 return -EINVAL;
471 }
472
473 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100474
475 return 0;
476}
477
Simon Wilson066c9f62011-06-05 18:23:05 -0700478int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700479{
480 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700481 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700482
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100483 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700484 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700485
486 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100487 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700488 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
489 if (ret < 0)
490 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700491
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100492 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700493 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700494 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700495 break;
496
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700497 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200498 if ((value < mixer_ctl_get_range_min(ctl)) ||
499 (value > mixer_ctl_get_range_max(ctl))) {
500 return -EINVAL;
501 }
502
Simon Wilsond2cb5032011-06-04 00:57:17 -0700503 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700504 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700505
Simon Wilson066c9f62011-06-05 18:23:05 -0700506 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
507 ev.value.enumerated.item[id] = value;
508 break;
509
David.Coutherutce2b6342013-06-11 16:22:57 +0200510 case SNDRV_CTL_ELEM_TYPE_BYTES:
511 ev.value.bytes.data[id] = value;
512 break;
513
Simon Wilson79d39652011-05-25 13:44:23 -0700514 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700515 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700516 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700517
Simon Wilson79d39652011-05-25 13:44:23 -0700518 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
519}
520
Simon Wilson38f87f32012-10-23 15:05:23 -0700521int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100522{
523 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -0700524 size_t size;
525 void *dest;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100526
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100527 if (!ctl || (count > ctl->info.count) || !count || !array)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100528 return -EINVAL;
529
530 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100531 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100532
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100533 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700534 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
535 case SNDRV_CTL_ELEM_TYPE_INTEGER:
536 size = sizeof(ev.value.integer.value[0]);
537 dest = ev.value.integer.value;
538 break;
539
540 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530541 /* check if this is new bytes TLV */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100542 if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) {
Mythri P K45b2d042014-08-18 15:39:36 +0530543 struct snd_ctl_tlv *tlv;
544 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700545 if (count > SIZE_MAX - sizeof(*tlv))
546 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530547 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700548 if (!tlv)
549 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100550 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530551 tlv->length = count;
552 memcpy(tlv->tlv, array, count);
553
554 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
555 free(tlv);
556
557 return ret;
558 } else {
559 size = sizeof(ev.value.bytes.data[0]);
560 dest = ev.value.bytes.data;
561 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700562 break;
563
564 default:
565 return -EINVAL;
566 }
567
568 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100569
570 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
571}
572
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700573int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
574{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100575 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700576 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700577
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100578 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700579}
580
581int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
582{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100583 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700584 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700585
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100586 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700587}
588
Simon Wilson066c9f62011-06-05 18:23:05 -0700589unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
590{
Simon Wilson193b1c32011-06-07 23:42:38 -0700591 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -0700592 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -0700593
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100594 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -0700595}
596
David.Coutherut9b423962013-06-03 13:45:51 +0200597int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
598{
599 struct snd_ctl_elem_info tmp;
600 unsigned int m;
601 char **enames;
602
603 if (ctl->ename) {
604 return 0;
605 }
606
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400607 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +0200608 if (!enames)
609 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400610 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200611 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400612 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +0200613 tmp.value.enumerated.item = m;
614 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
615 goto fail;
616 enames[m] = strdup(tmp.value.enumerated.name);
617 if (!enames[m])
618 goto fail;
619 }
620 ctl->ename = enames;
621 return 0;
622
623fail:
624 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400625 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +0200626 if (enames[m]) {
627 free(enames[m]);
628 }
629 }
630 free(enames);
631 }
632 return -1;
633}
634
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800635const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
636 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -0700637{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100638 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400639 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200640 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800641 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -0700642
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800643 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -0700644}
645
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700646int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
647{
648 unsigned int i, num_enums;
649 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700650 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700651
Taylor Holberton6a38d5f2016-10-03 21:07:39 -0400652 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +0200653 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -0700654 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700655
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100656 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700657 for (i = 0; i < num_enums; i++) {
658 if (!strcmp(string, ctl->ename[i])) {
659 memset(&ev, 0, sizeof(ev));
660 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100661 ev.id.numid = ctl->info.id.numid;
Simon Wilson193b1c32011-06-07 23:42:38 -0700662 ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
663 if (ret < 0)
664 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700665 return 0;
666 }
667 }
668
Simon Wilson193b1c32011-06-07 23:42:38 -0700669 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700670}
671