blob: 1c9e63bfbcf513032b3de994153f00844d8e9cc3 [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>
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -070032#include <stdbool.h>
Simon Wilson79d39652011-05-25 13:44:23 -070033#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <ctype.h>
Richard Fitzgerald57a87742014-09-09 16:54:12 +010038#include <limits.h>
Dima Krasner696c4482016-03-05 19:50:02 +020039#include <time.h>
Pankaj Bharadiya010121a2017-01-11 08:57:06 +053040#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070041
Simon Wilson6a52f2c2012-05-04 16:32:10 -070042#include <sys/ioctl.h>
43
Simon Wilson79d39652011-05-25 13:44:23 -070044#include <linux/ioctl.h>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050045
46#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070047#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050048#endif
49
50#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070051#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050052#endif
53
54#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070055#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050056#endif
57
Simon Wilson79d39652011-05-25 13:44:23 -070058#include <sound/asound.h>
59
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030060#include <tinyalsa/mixer.h>
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -070061#include <tinyalsa/plugin.h>
62
63#include "mixer_io.h"
Simon Wilson79d39652011-05-25 13:44:23 -070064
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040065/** A mixer control.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080066 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040067 */
Simon Wilson79d39652011-05-25 13:44:23 -070068struct mixer_ctl {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040069 /** The mixer that the mixer control belongs to */
Simon Wilson79d39652011-05-25 13:44:23 -070070 struct mixer *mixer;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080071 /** Information on the control's value (i.e. type, number of values) */
Richard Fitzgerald899cece2014-09-09 17:03:21 +010072 struct snd_ctl_elem_info info;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080073 /** A list of string representations of enumerated values (only valid for enumerated controls) */
Simon Wilson79d39652011-05-25 13:44:23 -070074 char **ename;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -070075 /** Pointer to the group that the control belongs to */
76 struct mixer_ctl_group *grp;
77};
78
79struct mixer_ctl_group {
80 /** A continuous array of mixer controls */
81 struct mixer_ctl *ctl;
82 /** The number of mixer controls */
83 unsigned int count;
84 /** The number of events associated with this group */
85 unsigned int event_cnt;
86 /** The operations corresponding to this group */
87 const struct mixer_ops *ops;
88 /** Private data for storing group specific data */
89 void *data;
Simon Wilson79d39652011-05-25 13:44:23 -070090};
91
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040092/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080093 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040094 */
Simon Wilson79d39652011-05-25 13:44:23 -070095struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040096 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070097 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040098 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070099 struct snd_ctl_card_info card_info;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700100 /* Hardware (kernel interface) mixer control group */
101 struct mixer_ctl_group *h_grp;
102 /* Virtual (Plugin interface) mixer control group */
103 struct mixer_ctl_group *v_grp;
104 /* Total count of mixer controls from both groups */
105 unsigned int total_count;
106 /* Flag to track if card information is already retrieved */
107 bool is_card_info_retrieved;
108
Simon Wilson79d39652011-05-25 13:44:23 -0700109};
110
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100111static void mixer_cleanup_control(struct mixer_ctl *ctl)
112{
113 unsigned int m;
114
115 if (ctl->ename) {
116 unsigned int max = ctl->info.value.enumerated.items;
117 for (m = 0; m < max; m++)
118 free(ctl->ename[m]);
119 free(ctl->ename);
120 }
121}
122
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700123static void mixer_grp_close(struct mixer *mixer, struct mixer_ctl_group *grp)
124{
125 unsigned int n;
126
127 if (!grp)
128 return;
129
130 if (grp->ctl) {
131 for (n = 0; n < grp->count; n++)
132 mixer_cleanup_control(&grp->ctl[n]);
133 free(grp->ctl);
134 }
135
136 mixer->is_card_info_retrieved = false;
137}
138
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400139/** Closes a mixer returned by @ref mixer_open.
140 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800141 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400142 */
Simon Wilson79d39652011-05-25 13:44:23 -0700143void mixer_close(struct mixer *mixer)
144{
Simon Wilson193b1c32011-06-07 23:42:38 -0700145 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700146 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700147
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700148 if (mixer->fd >= 0 && mixer->h_grp)
149 mixer->h_grp->ops->close(mixer->h_grp->data);
150 mixer_grp_close(mixer, mixer->h_grp);
Simon Wilson79d39652011-05-25 13:44:23 -0700151
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700152#ifdef TINYALSA_USES_PLUGINS
153 if (mixer->v_grp)
154 mixer->v_grp->ops->close(mixer->v_grp->data);
155 mixer_grp_close(mixer, mixer->v_grp);
156#endif
Simon Wilson79d39652011-05-25 13:44:23 -0700157
Simon Wilson79d39652011-05-25 13:44:23 -0700158 free(mixer);
159
160 /* TODO: verify frees */
161}
162
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100163static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size)
164{
165 int8_t *newp;
166
167 newp = realloc(ptr, size * newnum);
168 if (!newp)
169 return NULL;
170
171 memset(newp + (curnum * size), 0, (newnum - curnum) * size);
172 return newp;
173}
174
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700175static int add_controls(struct mixer *mixer, struct mixer_ctl_group *grp)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100176{
177 struct snd_ctl_elem_list elist;
178 struct snd_ctl_elem_id *eid = NULL;
179 struct mixer_ctl *ctl;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700180 const unsigned int old_count = grp->count;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100181 unsigned int new_count;
182 unsigned int n;
183
184 memset(&elist, 0, sizeof(elist));
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700185 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100186 goto fail;
187
188 if (old_count == elist.count)
189 return 0; /* no new controls return unchanged */
190
191 if (old_count > elist.count)
192 return -1; /* driver has removed controls - this is bad */
193
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700194 ctl = mixer_realloc_z(grp->ctl, old_count, elist.count,
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100195 sizeof(struct mixer_ctl));
196 if (!ctl)
197 goto fail;
198
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700199 grp->ctl = ctl;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100200
201 /* ALSA drivers are not supposed to remove or re-order controls that
202 * have already been created so we know that any new controls must
203 * be after the ones we have already collected
204 */
205 new_count = elist.count;
206 elist.space = new_count - old_count; /* controls we haven't seen before */
207 elist.offset = old_count; /* first control we haven't seen */
208
209 eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id));
210 if (!eid)
211 goto fail;
212
213 elist.pids = eid;
214
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700215 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100216 goto fail;
217
218 for (n = old_count; n < new_count; n++) {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700219 struct snd_ctl_elem_info *ei = &grp->ctl[n].info;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100220 ei->id.numid = eid[n - old_count].numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700221 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100222 goto fail_extend;
223 ctl[n].mixer = mixer;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700224 ctl[n].grp = grp;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100225 }
226
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700227 grp->count = new_count;
228 mixer->total_count += (new_count - old_count);
229
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100230 free(eid);
231 return 0;
232
233fail_extend:
234 /* cleanup the control we failed on but leave the ones that were already
235 * added. Also no advantage to shrinking the resized memory block, we
236 * might want to extend the controls again later
237 */
238 mixer_cleanup_control(&ctl[n]);
239
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700240 grp->count = n; /* keep controls we successfully added */
241 mixer->total_count += (n - old_count);
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100242 /* fall through... */
243fail:
244 free(eid);
245 return -1;
246}
247
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700248static int mixer_grp_open(struct mixer *mixer, unsigned int card, bool is_hw)
249{
250 struct mixer_ctl_group *grp = NULL;
251 const struct mixer_ops *ops = NULL;
252 void *data = NULL;
253 int fd, ret;
254
255 grp = calloc(1, sizeof(*grp));
256 if (!grp)
257 return -ENOMEM;
258
259 if (is_hw) {
260 mixer->fd = -1;
261 fd = mixer_hw_open(card, &data, &ops);
262 if (fd < 0) {
263 ret = fd;
264 goto err_open;
265 }
266 mixer->fd = fd;
267 mixer->h_grp = grp;
268 }
269#ifdef TINYALSA_USES_PLUGINS
270 else {
271 ret = mixer_plugin_open(card, &data, &ops);
272 if (ret < 0)
273 goto err_open;
274 mixer->v_grp = grp;
275 }
276#endif
277 grp->ops = ops;
278 grp->data = data;
279
280 if (!mixer->is_card_info_retrieved) {
281 ret = grp->ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO,
282 &mixer->card_info);
283 if (ret < 0)
284 goto err_card_info;
285 mixer->is_card_info_retrieved = true;
286 }
287
288 ret = add_controls(mixer, grp);
289 if (ret < 0)
290 goto err_card_info;
291
292 return 0;
293
294err_card_info:
295 grp->ops->close(grp->data);
296
297err_open:
298 free(grp);
299 return ret;
300
301}
302
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400303/** Opens a mixer for a given card.
304 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500305 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800306 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400307 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700308struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700309{
Simon Wilson79d39652011-05-25 13:44:23 -0700310 struct mixer *mixer = NULL;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700311 int h_status, v_status = -1;
Simon Wilson79d39652011-05-25 13:44:23 -0700312
Simon Wilson79d39652011-05-25 13:44:23 -0700313 mixer = calloc(1, sizeof(*mixer));
314 if (!mixer)
315 goto fail;
316
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700317 h_status = mixer_grp_open(mixer, card, true);
Simon Wilson79d39652011-05-25 13:44:23 -0700318
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700319#ifdef TINYALSA_USES_PLUGINS
320 v_status = mixer_grp_open(mixer, card, false);
321#endif
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100322
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700323 /* both hw and virtual should fail for mixer_open to fail */
324 if (h_status < 0 && v_status < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700325 goto fail;
326
Simon Wilson79d39652011-05-25 13:44:23 -0700327 return mixer;
328
329fail:
Simon Wilson79d39652011-05-25 13:44:23 -0700330 if (mixer)
331 mixer_close(mixer);
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700332
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100333 return NULL;
334}
335
336/** Some controls may not be present at boot time, e.g. controls from runtime
337 * loadable DSP firmware. This function adds any new controls that have appeared
338 * since mixer_open() or the last call to this function. This assumes a well-
339 * behaved codec driver that does not delete controls that already exists, so
340 * any added controls must be after the last one we already saw. Scanning only
341 * the new controls is much faster than calling mixer_close() then mixer_open()
342 * to re-scan all controls.
343 *
344 * NOTE: this invalidates any struct mixer_ctl pointers previously obtained
345 * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your
346 * stored pointers after calling mixer_update_ctls(), or (better) do not
347 * store struct mixer_ctl pointers, instead lookup the control by name or
348 * id only when you are about to use it. The overhead of lookup by id
349 * using mixer_get_ctl() is negligible.
350 * @param mixer An initialized mixer handle.
351 * @returns 0 on success, -1 on failure
352 */
353int mixer_add_new_ctls(struct mixer *mixer)
354{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700355 int rc1 = 0, rc2 = 0;
356
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100357 if (!mixer)
358 return 0;
359
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700360 /* add the h_grp controls */
361 if (mixer->h_grp)
362 rc1 = add_controls(mixer, mixer->h_grp);
363
364#ifdef TINYALSA_USES_PLUGINS
365 /* add the v_grp controls */
366 if (mixer->v_grp)
367 rc2 = add_controls(mixer, mixer->v_grp);
368#endif
369
370 if (rc1 < 0)
371 return rc1;
372 if (rc2 < 0)
373 return rc2;
374
375 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700376}
377
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500378/** Gets the name of the mixer's card.
379 * @param mixer An initialized mixer handle.
380 * @returns The name of the mixer's card.
381 * @ingroup libtinyalsa-mixer
382 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800383const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700384{
385 return (const char *)mixer->card_info.name;
386}
387
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500388/** Gets the number of mixer controls for a given mixer.
389 * @param mixer An initialized mixer handle.
390 * @returns The number of mixer controls for the given mixer.
391 * @ingroup libtinyalsa-mixer
392 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800393unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700394{
Simon Wilson193b1c32011-06-07 23:42:38 -0700395 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700396 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700397
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700398 return mixer->total_count;
Simon Wilson79d39652011-05-25 13:44:23 -0700399}
400
Taylor Holberton94c7c832016-12-01 18:35:24 -0800401/** Gets the number of mixer controls, that go by a specified name, for a given mixer.
402 * @param mixer An initialized mixer handle.
403 * @param name The name of the mixer control
404 * @returns The number of mixer controls, specified by @p name, for the given mixer.
405 * @ingroup libtinyalsa-mixer
406 */
407unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
408{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700409 struct mixer_ctl_group *grp;
Taylor Holberton94c7c832016-12-01 18:35:24 -0800410 unsigned int n;
411 unsigned int count = 0;
412 struct mixer_ctl *ctl;
413
414 if (!mixer)
415 return 0;
416
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700417 if (mixer->h_grp) {
418 grp = mixer->h_grp;
419 ctl = grp->ctl;
Taylor Holberton94c7c832016-12-01 18:35:24 -0800420
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700421 for (n = 0; n < grp->count; n++)
422 if (!strcmp(name, (char*) ctl[n].info.id.name))
423 count++;
424 }
425#ifdef TINYALSA_USES_PLUGINS
426 if (mixer->v_grp) {
427 grp = mixer->v_grp;
428 ctl = grp->ctl;
429
430 for (n = 0; n < grp->count; n++)
431 if (!strcmp(name, (char*) ctl[n].info.id.name))
432 count++;
433 }
434#endif
Taylor Holberton94c7c832016-12-01 18:35:24 -0800435
436 return count;
437}
438
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530439/** Subscribes for the mixer events.
440 * @param mixer A mixer handle.
441 * @param subscribe value indicating subscribe or unsubscribe for events
442 * @returns On success, zero.
443 * On failure, non-zero.
444 * @ingroup libtinyalsa-mixer
445 */
446int mixer_subscribe_events(struct mixer *mixer, int subscribe)
447{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700448 struct mixer_ctl_group *grp;
449
450 if (mixer->h_grp) {
451 grp = mixer->h_grp;
452 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
453 return -1;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530454 }
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700455
456#ifdef TINYALSA_USES_PLUGINS
457 if (mixer->v_grp) {
458 grp = mixer->v_grp;
459 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
460 return -1;
461 }
462#endif
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530463 return 0;
464}
465
466/** Wait for mixer events.
467 * @param mixer A mixer handle.
468 * @param timeout timout value
Pankaj Bharadiya95c79d82017-01-11 11:28:02 +0530469 * @returns On success, 1.
470 * On failure, -errno.
471 * On timeout, 0
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530472 * @ingroup libtinyalsa-mixer
473 */
474int mixer_wait_event(struct mixer *mixer, int timeout)
475{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700476 struct pollfd *pfd;
477 struct mixer_ctl_group *grp;
478 int count = 0, num_fds = 0, i;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530479
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700480 if (mixer->fd >= 0)
481 num_fds++;
482
483#ifdef TINYALSA_USES_PLUGINS
484 if (mixer->v_grp)
485 num_fds++;
486#endif
487
488 pfd = (struct pollfd *)calloc(sizeof(struct pollfd), num_fds);
489 if (!pfd)
490 return -ENOMEM;
491
492 if (mixer->fd >= 0) {
493 pfd[count].fd = mixer->fd;
494 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
495 count++;
496 }
497
498#ifdef TINYALSA_USES_PLUGINS
499 if (mixer->v_grp) {
500 grp = mixer->v_grp;
501 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
502 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
503 count++;
504 }
505 }
506#endif
507
508 if (!count)
509 return 0;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530510
511 for (;;) {
512 int err;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700513 err = poll(pfd, count, timeout);
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530514 if (err < 0)
515 return -errno;
516 if (!err)
517 return 0;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700518 for (i = 0; i < count; i++) {
519 if (pfd[i].revents & (POLLERR | POLLNVAL))
520 return -EIO;
521 if (pfd[i].revents & (POLLIN | POLLOUT)) {
522 if ((i == 0) && mixer->fd >= 0) {
523 grp = mixer->h_grp;
524 grp->event_cnt++;
525 }
526#ifdef TINYALSA_USES_PLUGINS
527 else {
528 grp = mixer->v_grp;
529 grp->event_cnt++;
530 }
531#endif
532 return 1;
533 }
534 }
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530535 }
536}
537
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700538static unsigned int mixer_grp_get_count(struct mixer_ctl_group *grp)
539{
540 if (!grp)
541 return 0;
542
543 return grp->count;
544}
545
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500546/** Gets a mixer control handle, by the mixer control's id.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800547 * For non-const access, see @ref mixer_get_ctl
548 * @param mixer An initialized mixer handle.
549 * @param id The control's id in the given mixer.
550 * @returns A handle to the mixer control.
551 * @ingroup libtinyalsa-mixer
552 */
553const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
554{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700555 unsigned int h_count;
556
557 if (!mixer || (id >= mixer->total_count))
558 return NULL;
559
560 h_count = mixer_grp_get_count(mixer->h_grp);
561
562 if (id < h_count)
563 return mixer->h_grp->ctl + id;
564#ifdef TINYALSA_USES_PLUGINS
565 else {
566 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
567 if ((id - h_count) < v_count)
568 return mixer->v_grp->ctl + (id - h_count);
569 }
570#endif
Taylor Holbertona94295b2016-12-01 18:23:16 -0800571
572 return NULL;
573}
574
575/** Gets a mixer control handle, by the mixer control's id.
576 * For const access, see @ref mixer_get_ctl_const
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500577 * @param mixer An initialized mixer handle.
578 * @param id The control's id in the given mixer.
579 * @returns A handle to the mixer control.
580 * @ingroup libtinyalsa-mixer
581 */
Simon Wilson79d39652011-05-25 13:44:23 -0700582struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
583{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700584 unsigned int h_count;
Simon Wilson79d39652011-05-25 13:44:23 -0700585
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700586 if (!mixer || (id >= mixer->total_count))
587 return NULL;
588
589 h_count = mixer_grp_get_count(mixer->h_grp);
590
591 if (id < h_count)
592 return mixer->h_grp->ctl + id;
593#ifdef TINYALSA_USES_PLUGINS
594 else {
595 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
596 if ((id - h_count) < v_count)
597 return mixer->v_grp->ctl + (id - h_count);
598 }
599#endif
Simon Wilson79d39652011-05-25 13:44:23 -0700600 return NULL;
601}
602
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500603/** Gets the first instance of mixer control handle, by the mixer control's name.
604 * @param mixer An initialized mixer handle.
605 * @param name The control's name in the given mixer.
606 * @returns A handle to the mixer control.
607 * @ingroup libtinyalsa-mixer
608 */
Simon Wilson79d39652011-05-25 13:44:23 -0700609struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
610{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200611 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
612}
613
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500614/** Gets an instance of mixer control handle, by the mixer control's name and index.
615 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
616 * @param mixer An initialized mixer handle.
617 * @param name The control's name in the given mixer.
618 * @param index The control's index.
619 * @returns A handle to the mixer control.
620 * @ingroup libtinyalsa-mixer
621 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200622struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
623 const char *name,
624 unsigned int index)
625{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700626 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700627 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200628 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700629
Simon Wilson98c1f162011-06-07 16:12:32 -0700630 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700631 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700632
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700633 if (mixer->h_grp) {
634 grp = mixer->h_grp;
635 ctl = grp->ctl;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200636
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700637 for (n = 0; n < grp->count; n++)
638 if (!strcmp(name, (char*) ctl[n].info.id.name))
639 if (index-- == 0)
640 return ctl + n;
641 }
Simon Wilson79d39652011-05-25 13:44:23 -0700642
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700643#ifdef TINYALSA_USES_PLUGINS
644 if (mixer->v_grp) {
645 grp = mixer->v_grp;
646 ctl = grp->ctl;
647
648 for (n = 0; n < grp->count; n++)
649 if (!strcmp(name, (char*) ctl[n].info.id.name))
650 if (index-- == 0)
651 return ctl + n;
652 }
653#endif
Simon Wilson79d39652011-05-25 13:44:23 -0700654 return NULL;
655}
656
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500657/** Updates the control's info.
658 * This is useful for a program that may be idle for a period of time.
659 * @param ctl An initialized control handle.
660 * @ingroup libtinyalsa-mixer
661 */
Simon Wilson710df882013-06-28 16:17:50 -0700662void mixer_ctl_update(struct mixer_ctl *ctl)
663{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700664 struct mixer_ctl_group *grp;
665
666 if (!ctl)
667 return;
668
669 grp = ctl->grp;
670 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &ctl->info);
Simon Wilson710df882013-06-28 16:17:50 -0700671}
672
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530673/** Checks the control for TLV Read/Write access.
674 * @param ctl An initialized control handle.
675 * @returns On success, non-zero.
676 * On failure, zero.
677 * @ingroup libtinyalsa-mixer
678 */
679int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl)
680{
681 return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
682}
683
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500684/** Gets the control's ID.
685 * @param ctl An initialized control handle.
686 * @returns On success, the control's ID is returned.
687 * On error, UINT_MAX is returned instead.
688 * @ingroup libtinyalsa-mixer
689 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800690unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100691{
692 if (!ctl)
693 return UINT_MAX;
694
695 /* numid values start at 1, return a 0-base value that
696 * can be passed to mixer_get_ctl()
697 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100698 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100699}
700
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500701/** Gets the name of the control.
702 * @param ctl An initialized control handle.
703 * @returns On success, the name of the control.
704 * On error, NULL.
705 * @ingroup libtinyalsa-mixer
706 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800707const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700708{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800709 if (!ctl)
710 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700711
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100712 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700713}
714
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500715/** Gets the value type of the control.
716 * @param ctl An initialized control handle
717 * @returns On success, the type of mixer control.
718 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
719 * @ingroup libtinyalsa-mixer
720 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800721enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700722{
Simon Wilson193b1c32011-06-07 23:42:38 -0700723 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700724 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700725
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100726 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700727 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
728 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
729 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
730 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
731 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
732 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
733 default: return MIXER_CTL_TYPE_UNKNOWN;
734 };
735}
736
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500737/** Gets the string that describes the value type of the control.
738 * @param ctl An initialized control handle
739 * @returns On success, a string describing type of mixer control.
740 * @ingroup libtinyalsa-mixer
741 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800742const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700743{
Simon Wilson193b1c32011-06-07 23:42:38 -0700744 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700745 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700746
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100747 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700748 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
749 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
750 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
751 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
752 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
753 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
754 default: return "Unknown";
755 };
756}
757
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500758/** Gets the number of values in the control.
759 * @param ctl An initialized control handle
760 * @returns The number of values in the mixer control
761 * @ingroup libtinyalsa-mixer
762 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800763unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700764{
Simon Wilson193b1c32011-06-07 23:42:38 -0700765 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700766 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700767
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100768 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700769}
770
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800771static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700772{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200773 if ((percent > 100) || (percent < 0)) {
774 return -EINVAL;
775 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700776
Baptiste Robert4a484e12013-09-12 15:37:53 +0200777 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700778
Simon Wilson79d39652011-05-25 13:44:23 -0700779 return ei->value.integer.min + (range * percent) / 100;
780}
781
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800782static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700783{
784 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700785
Simon Wilson79d39652011-05-25 13:44:23 -0700786 if (range == 0)
787 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700788
Daniela-Marinela Bistrean9962e712019-04-12 00:49:20 +0300789 return ((value - ei->value.integer.min) * 100) / range;
Simon Wilson79d39652011-05-25 13:44:23 -0700790}
791
Taylor Holberton4e557192016-11-23 11:48:06 -0800792/** Gets a percentage representation of a specified control value.
793 * @param ctl An initialized control handle.
794 * @param id The index of the value within the control.
795 * @returns On success, the percentage representation of the control value.
796 * On failure, -EINVAL is returned.
797 * @ingroup libtinyalsa-mixer
798 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800799int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700800{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100801 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700802 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700803
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100804 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700805}
806
Taylor Holberton4e557192016-11-23 11:48:06 -0800807/** Sets the value of a control by percent, specified by the value index.
808 * @param ctl An initialized control handle.
809 * @param id The index of the value to set
810 * @param percent A percentage value between 0 and 100.
811 * @returns On success, zero is returned.
812 * On failure, non-zero is returned.
813 * @ingroup libtinyalsa-mixer
814 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700815int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700816{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100817 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700818 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700819
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100820 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700821}
822
Taylor Holberton4e557192016-11-23 11:48:06 -0800823/** Gets the value of a control.
824 * @param ctl An initialized control handle.
825 * @param id The index of the control value.
826 * @returns On success, the specified value is returned.
827 * On failure, -EINVAL is returned.
828 * @ingroup libtinyalsa-mixer
829 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800830int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700831{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700832 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700833 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700834 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700835
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100836 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700837 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700838
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700839 grp = ctl->grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700840 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100841 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700842 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -0700843 if (ret < 0)
844 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700845
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100846 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700847 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700848 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700849
850 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700851 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700852
853 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
854 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700855
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500856 case SNDRV_CTL_ELEM_TYPE_BYTES:
857 return ev.value.bytes.data[id];
858
Simon Wilson79d39652011-05-25 13:44:23 -0700859 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700860 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700861 }
862
863 return 0;
864}
865
Taylor Holberton4e557192016-11-23 11:48:06 -0800866/** Gets the contents of a control's value array.
867 * @param ctl An initialized control handle.
868 * @param array A pointer to write the array data to.
869 * The size of this array must be equal to the number of items in the array
870 * multiplied by the size of each item.
871 * @param count The number of items in the array.
872 * This parameter must match the number of items in the control.
873 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
874 * @returns On success, zero.
875 * On failure, non-zero.
876 * @ingroup libtinyalsa-mixer
877 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800878int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100879{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700880 struct mixer_ctl_group *grp;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100881 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530882 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700883 size_t size;
884 void *source;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530885 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100886
Charles Keepaxe40758b2017-09-07 16:38:52 +0100887 if (!ctl || !count || !array)
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530888 return -EINVAL;
889
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700890 grp = ctl->grp;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530891 total_count = ctl->info.count;
892
893 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
894 (mixer_ctl_is_access_tlv_rw(ctl))) {
895 /* Additional two words is for the TLV header */
896 total_count += TLV_HEADER_SIZE;
897 }
898
899 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100900 return -EINVAL;
901
902 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100903 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100904
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100905 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700906 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
907 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700908 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K45b2d042014-08-18 15:39:36 +0530909 if (ret < 0)
910 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700911 size = sizeof(ev.value.integer.value[0]);
912 source = ev.value.integer.value;
913 break;
914
915 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530916 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530917 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530918 struct snd_ctl_tlv *tlv;
919 int ret;
920
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700921 if (count > SIZE_MAX - sizeof(*tlv))
922 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530923 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700924 if (!tlv)
925 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100926 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530927 tlv->length = count;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700928 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K45b2d042014-08-18 15:39:36 +0530929
930 source = tlv->tlv;
931 memcpy(array, source, count);
932
933 free(tlv);
934
935 return ret;
936 } else {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700937 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K45b2d042014-08-18 15:39:36 +0530938 if (ret < 0)
939 return ret;
940 size = sizeof(ev.value.bytes.data[0]);
941 source = ev.value.bytes.data;
942 break;
943 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700944
945 default:
946 return -EINVAL;
947 }
948
949 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100950
951 return 0;
952}
953
Taylor Holberton4e557192016-11-23 11:48:06 -0800954/** Sets the value of a control, specified by the value index.
955 * @param ctl An initialized control handle.
956 * @param id The index of the value within the control.
957 * @param value The value to set.
958 * This must be in a range specified by @ref mixer_ctl_get_range_min
959 * and @ref mixer_ctl_get_range_max.
960 * @returns On success, zero is returned.
961 * On failure, non-zero is returned.
962 * @ingroup libtinyalsa-mixer
963 */
Simon Wilson066c9f62011-06-05 18:23:05 -0700964int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700965{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700966 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700967 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700968 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700969
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100970 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700971 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700972
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700973 grp = ctl->grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700974 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100975 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700976 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -0700977 if (ret < 0)
978 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700979
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100980 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700981 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700982 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700983 break;
984
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700985 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +0200986 if ((value < mixer_ctl_get_range_min(ctl)) ||
987 (value > mixer_ctl_get_range_max(ctl))) {
988 return -EINVAL;
989 }
990
Simon Wilsond2cb5032011-06-04 00:57:17 -0700991 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700992 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700993
Simon Wilson066c9f62011-06-05 18:23:05 -0700994 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
995 ev.value.enumerated.item[id] = value;
996 break;
997
David.Coutherutce2b6342013-06-11 16:22:57 +0200998 case SNDRV_CTL_ELEM_TYPE_BYTES:
999 ev.value.bytes.data[id] = value;
1000 break;
1001
Simon Wilson79d39652011-05-25 13:44:23 -07001002 default:
Simon Wilson193b1c32011-06-07 23:42:38 -07001003 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -07001004 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001005
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001006 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilson79d39652011-05-25 13:44:23 -07001007}
1008
Taylor Holberton4e557192016-11-23 11:48:06 -08001009/** Sets the contents of a control's value array.
1010 * @param ctl An initialized control handle.
1011 * @param array The array containing control values.
1012 * @param count The number of values in the array.
1013 * This must match the number of values in the control.
1014 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
1015 * @returns On success, zero.
1016 * On failure, non-zero.
1017 * @ingroup libtinyalsa-mixer
1018 */
Simon Wilson38f87f32012-10-23 15:05:23 -07001019int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001020{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001021 struct mixer_ctl_group *grp;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001022 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -07001023 size_t size;
1024 void *dest;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301025 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001026
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301027 if ((!ctl) || !count || !array)
1028 return -EINVAL;
1029
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001030 grp = ctl->grp;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301031 total_count = ctl->info.count;
1032
1033 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
1034 (mixer_ctl_is_access_tlv_rw(ctl))) {
1035 /* Additional TLV header */
1036 total_count += TLV_HEADER_SIZE;
1037 }
1038
1039 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001040 return -EINVAL;
1041
1042 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001043 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001044
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001045 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -07001046 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1047 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1048 size = sizeof(ev.value.integer.value[0]);
1049 dest = ev.value.integer.value;
1050 break;
1051
1052 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +05301053 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +05301054 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +05301055 struct snd_ctl_tlv *tlv;
1056 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -07001057 if (count > SIZE_MAX - sizeof(*tlv))
1058 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +05301059 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -07001060 if (!tlv)
1061 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001062 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +05301063 tlv->length = count;
1064 memcpy(tlv->tlv, array, count);
1065
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001066 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K45b2d042014-08-18 15:39:36 +05301067 free(tlv);
1068
1069 return ret;
1070 } else {
1071 size = sizeof(ev.value.bytes.data[0]);
1072 dest = ev.value.bytes.data;
1073 }
Simon Wilson38f87f32012-10-23 15:05:23 -07001074 break;
1075
1076 default:
1077 return -EINVAL;
1078 }
1079
1080 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001081
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001082 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001083}
1084
Taylor Holberton4e557192016-11-23 11:48:06 -08001085/** Gets the minimum value of an control.
1086 * The control must have an integer type.
1087 * The type of the control can be checked with @ref mixer_ctl_get_type.
1088 * @param ctl An initialized control handle.
1089 * @returns On success, the minimum value of the control.
1090 * On failure, -EINVAL.
1091 * @ingroup libtinyalsa-mixer
1092 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -08001093int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001094{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001095 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -07001096 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001097
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001098 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001099}
1100
Taylor Holberton4e557192016-11-23 11:48:06 -08001101/** Gets the maximum value of an control.
1102 * The control must have an integer type.
1103 * The type of the control can be checked with @ref mixer_ctl_get_type.
1104 * @param ctl An initialized control handle.
1105 * @returns On success, the maximum value of the control.
1106 * On failure, -EINVAL.
1107 * @ingroup libtinyalsa-mixer
1108 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -08001109int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001110{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001111 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -07001112 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001113
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001114 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001115}
1116
Taylor Holberton4e557192016-11-23 11:48:06 -08001117/** Get the number of enumerated items in the control.
1118 * @param ctl An initialized control handle.
1119 * @returns The number of enumerated items in the control.
1120 * @ingroup libtinyalsa-mixer
1121 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -08001122unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -07001123{
Simon Wilson193b1c32011-06-07 23:42:38 -07001124 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -07001125 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -07001126
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001127 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -07001128}
1129
David.Coutherut9b423962013-06-03 13:45:51 +02001130int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
1131{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001132 struct mixer_ctl_group *grp = ctl->grp;
David.Coutherut9b423962013-06-03 13:45:51 +02001133 struct snd_ctl_elem_info tmp;
1134 unsigned int m;
1135 char **enames;
1136
1137 if (ctl->ename) {
1138 return 0;
1139 }
1140
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001141 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +02001142 if (!enames)
1143 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001144 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +02001145 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001146 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +02001147 tmp.value.enumerated.item = m;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001148 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
David.Coutherut9b423962013-06-03 13:45:51 +02001149 goto fail;
1150 enames[m] = strdup(tmp.value.enumerated.name);
1151 if (!enames[m])
1152 goto fail;
1153 }
1154 ctl->ename = enames;
1155 return 0;
1156
1157fail:
1158 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001159 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +02001160 if (enames[m]) {
1161 free(enames[m]);
1162 }
1163 }
1164 free(enames);
1165 }
1166 return -1;
1167}
1168
Taylor Holberton4e557192016-11-23 11:48:06 -08001169/** Gets the string representation of an enumerated item.
1170 * @param ctl An initialized control handle.
1171 * @param enum_id The index of the enumerated value.
1172 * @returns A string representation of the enumerated item.
1173 * @ingroup libtinyalsa-mixer
1174 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001175const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
1176 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -07001177{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001178 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001179 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +02001180 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001181 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -07001182
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001183 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -07001184}
1185
Taylor Holberton4e557192016-11-23 11:48:06 -08001186/** Set an enumeration value by string value.
1187 * @param ctl An enumerated mixer control.
1188 * @param string The string representation of an enumeration.
1189 * @returns On success, zero.
1190 * On failure, zero.
1191 * @ingroup libtinyalsa-mixer
1192 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001193int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
1194{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001195 struct mixer_ctl_group *grp;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001196 unsigned int i, num_enums;
1197 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -07001198 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001199
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001200 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +02001201 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -07001202 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001203
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001204 grp = ctl->grp;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001205 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001206 for (i = 0; i < num_enums; i++) {
1207 if (!strcmp(string, ctl->ename[i])) {
1208 memset(&ev, 0, sizeof(ev));
1209 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001210 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001211 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -07001212 if (ret < 0)
1213 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001214 return 0;
1215 }
1216 }
1217
Simon Wilson193b1c32011-06-07 23:42:38 -07001218 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001219}