blob: a45502e8d73ca5da0952d71fc9080dbd1cb4a3ea [file] [log] [blame]
Simon Wilsonedff7082011-06-06 15:33:34 -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 Zhang6cb14f22016-04-22 17:59:40 -070031#include <stdint.h>
David Lie0f41062020-11-05 12:27:29 +000032#include <stdbool.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070033#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <ctype.h>
David Lie0f41062020-11-05 12:27:29 +000038#include <limits.h>
39#include <time.h>
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +053040#include <poll.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070041
Simon Wilson85dc38f2012-05-15 17:37:19 -070042#include <sys/ioctl.h>
43
Simon Wilsonedff7082011-06-06 15:33:34 -070044#include <linux/ioctl.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070045
David Lie0f41062020-11-05 12:27:29 +000046#ifndef __force
47#define __force
John Muir22bca932017-10-23 10:58:12 -070048#endif
49
David Lie0f41062020-11-05 12:27:29 +000050#ifndef __bitwise
51#define __bitwise
52#endif
Simon Wilsonedff7082011-06-06 15:33:34 -070053
David Lie0f41062020-11-05 12:27:29 +000054#ifndef __user
55#define __user
56#endif
57
58#include <sound/asound.h>
59
60#include <tinyalsa/mixer.h>
61#include <tinyalsa/plugin.h>
62
63#include "mixer_io.h"
64
65/** A mixer control.
66 * @ingroup libtinyalsa-mixer
67 */
Simon Wilsonedff7082011-06-06 15:33:34 -070068struct mixer_ctl {
David Lie0f41062020-11-05 12:27:29 +000069 /** The mixer that the mixer control belongs to */
Simon Wilsonedff7082011-06-06 15:33:34 -070070 struct mixer *mixer;
David Lie0f41062020-11-05 12:27:29 +000071 /** Information on the control's value (i.e. type, number of values) */
72 struct snd_ctl_elem_info info;
73 /** A list of string representations of enumerated values (only valid for enumerated controls) */
Simon Wilsonedff7082011-06-06 15:33:34 -070074 char **ename;
David Lie0f41062020-11-05 12:27:29 +000075 /** Pointer to the group that the control belongs to */
76 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -070077};
78
David Lie0f41062020-11-05 12:27:29 +000079struct mixer_ctl_group {
80 /** A continuous array of mixer controls */
Simon Wilsonedff7082011-06-06 15:33:34 -070081 struct mixer_ctl *ctl;
David Lie0f41062020-11-05 12:27:29 +000082 /** The number of mixer controls */
Simon Wilsonedff7082011-06-06 15:33:34 -070083 unsigned int count;
David Lie0f41062020-11-05 12:27:29 +000084 /** 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 Wilsonedff7082011-06-06 15:33:34 -070090};
91
David Lie0f41062020-11-05 12:27:29 +000092/** A mixer handle.
93 * @ingroup libtinyalsa-mixer
94 */
95struct mixer {
96 /** File descriptor for the card */
97 int fd;
98 /** Card information */
99 struct snd_ctl_card_info card_info;
100 /* 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
109};
110
111static 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
123static 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 free(grp);
137
138 mixer->is_card_info_retrieved = false;
139}
140
141/** Closes a mixer returned by @ref mixer_open.
142 * @param mixer A mixer handle.
143 * @ingroup libtinyalsa-mixer
144 */
Simon Wilsonedff7082011-06-06 15:33:34 -0700145void mixer_close(struct mixer *mixer)
146{
Simon Wilsonedff7082011-06-06 15:33:34 -0700147 if (!mixer)
148 return;
149
David Lie0f41062020-11-05 12:27:29 +0000150 if (mixer->fd >= 0 && mixer->h_grp)
151 mixer->h_grp->ops->close(mixer->h_grp->data);
152 mixer_grp_close(mixer, mixer->h_grp);
Simon Wilsonedff7082011-06-06 15:33:34 -0700153
David Lie0f41062020-11-05 12:27:29 +0000154#ifdef TINYALSA_USES_PLUGINS
155 if (mixer->v_grp)
156 mixer->v_grp->ops->close(mixer->v_grp->data);
157 mixer_grp_close(mixer, mixer->v_grp);
158#endif
Simon Wilsonedff7082011-06-06 15:33:34 -0700159
160 free(mixer);
161
162 /* TODO: verify frees */
163}
164
David Lie0f41062020-11-05 12:27:29 +0000165static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size)
166{
167 int8_t *newp;
168
169 newp = realloc(ptr, size * newnum);
170 if (!newp)
171 return NULL;
172
173 memset(newp + (curnum * size), 0, (newnum - curnum) * size);
174 return newp;
175}
176
177static int add_controls(struct mixer *mixer, struct mixer_ctl_group *grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700178{
179 struct snd_ctl_elem_list elist;
Simon Wilsonedff7082011-06-06 15:33:34 -0700180 struct snd_ctl_elem_id *eid = NULL;
David Lie0f41062020-11-05 12:27:29 +0000181 struct mixer_ctl *ctl;
182 const unsigned int old_count = grp->count;
183 unsigned int new_count;
John Muir22bca932017-10-23 10:58:12 -0700184 unsigned int n;
Simon Wilsonedff7082011-06-06 15:33:34 -0700185
186 memset(&elist, 0, sizeof(elist));
David Lie0f41062020-11-05 12:27:29 +0000187 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Simon Wilsonedff7082011-06-06 15:33:34 -0700188 goto fail;
189
David Lie0f41062020-11-05 12:27:29 +0000190 if (old_count == elist.count)
191 return 0; /* no new controls return unchanged */
192
193 if (old_count > elist.count)
194 return -1; /* driver has removed controls - this is bad */
195
196 ctl = mixer_realloc_z(grp->ctl, old_count, elist.count,
197 sizeof(struct mixer_ctl));
198 if (!ctl)
199 goto fail;
200
201 grp->ctl = ctl;
202
203 /* ALSA drivers are not supposed to remove or re-order controls that
204 * have already been created so we know that any new controls must
205 * be after the ones we have already collected
206 */
207 new_count = elist.count;
208 elist.space = new_count - old_count; /* controls we haven't seen before */
209 elist.offset = old_count; /* first control we haven't seen */
210
211 eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id));
212 if (!eid)
213 goto fail;
214
215 elist.pids = eid;
216
217 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
218 goto fail;
219
220 for (n = old_count; n < new_count; n++) {
221 struct snd_ctl_elem_info *ei = &grp->ctl[n].info;
222 ei->id.numid = eid[n - old_count].numid;
223 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
224 goto fail_extend;
225 ctl[n].mixer = mixer;
226 ctl[n].grp = grp;
227 }
228
229 grp->count = new_count;
230 mixer->total_count += (new_count - old_count);
231
232 free(eid);
233 return 0;
234
235fail_extend:
236 /* cleanup the control we failed on but leave the ones that were already
237 * added. Also no advantage to shrinking the resized memory block, we
238 * might want to extend the controls again later
239 */
240 mixer_cleanup_control(&ctl[n]);
241
242 grp->count = n; /* keep controls we successfully added */
243 mixer->total_count += (n - old_count);
244 /* fall through... */
245fail:
246 free(eid);
247 return -1;
248}
249
250static int mixer_grp_open(struct mixer *mixer, unsigned int card, bool is_hw)
251{
252 struct mixer_ctl_group *grp = NULL;
253 const struct mixer_ops *ops = NULL;
254 void *data = NULL;
255 int fd, ret;
256
257 grp = calloc(1, sizeof(*grp));
258 if (!grp)
259 return -ENOMEM;
260
261 if (is_hw) {
262 mixer->fd = -1;
263 fd = mixer_hw_open(card, &data, &ops);
264 if (fd < 0) {
265 ret = fd;
266 goto err_open;
267 }
268 mixer->fd = fd;
269 mixer->h_grp = grp;
270 }
271#ifdef TINYALSA_USES_PLUGINS
272 else {
273 ret = mixer_plugin_open(card, &data, &ops);
274 if (ret < 0)
275 goto err_open;
276 mixer->v_grp = grp;
277 }
278#endif
279 grp->ops = ops;
280 grp->data = data;
281
282 if (!mixer->is_card_info_retrieved) {
283 ret = grp->ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO,
284 &mixer->card_info);
285 if (ret < 0)
286 goto err_card_info;
287 mixer->is_card_info_retrieved = true;
288 }
289
290 ret = add_controls(mixer, grp);
291 if (ret < 0)
292 goto err_card_info;
293
294 return 0;
295
296err_card_info:
297 grp->ops->close(grp->data);
298
299err_open:
300 free(grp);
301 return ret;
302
303}
304
305/** Opens a mixer for a given card.
306 * @param card The card to open the mixer for.
307 * @returns An initialized mixer handle.
308 * @ingroup libtinyalsa-mixer
309 */
310struct mixer *mixer_open(unsigned int card)
311{
312 struct mixer *mixer = NULL;
313 int h_status, v_status = -1;
314
Simon Wilsonedff7082011-06-06 15:33:34 -0700315 mixer = calloc(1, sizeof(*mixer));
316 if (!mixer)
317 goto fail;
318
David Lie0f41062020-11-05 12:27:29 +0000319 h_status = mixer_grp_open(mixer, card, true);
320
321#ifdef TINYALSA_USES_PLUGINS
322 v_status = mixer_grp_open(mixer, card, false);
323#endif
324
325 /* both hw and virtual should fail for mixer_open to fail */
326 if (h_status < 0 && v_status < 0)
Simon Wilson7e8a6562013-06-28 16:47:16 -0700327 goto fail;
328
Simon Wilsonedff7082011-06-06 15:33:34 -0700329 return mixer;
330
331fail:
Simon Wilsonedff7082011-06-06 15:33:34 -0700332 if (mixer)
333 mixer_close(mixer);
Simon Wilsonedff7082011-06-06 15:33:34 -0700334
Andrew Chantb5701062018-02-05 15:16:41 -0800335 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700336}
337
David Lie0f41062020-11-05 12:27:29 +0000338/** Some controls may not be present at boot time, e.g. controls from runtime
339 * loadable DSP firmware. This function adds any new controls that have appeared
340 * since mixer_open() or the last call to this function. This assumes a well-
341 * behaved codec driver that does not delete controls that already exists, so
342 * any added controls must be after the last one we already saw. Scanning only
343 * the new controls is much faster than calling mixer_close() then mixer_open()
344 * to re-scan all controls.
345 *
346 * NOTE: this invalidates any struct mixer_ctl pointers previously obtained
347 * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your
348 * stored pointers after calling mixer_update_ctls(), or (better) do not
349 * store struct mixer_ctl pointers, instead lookup the control by name or
350 * id only when you are about to use it. The overhead of lookup by id
351 * using mixer_get_ctl() is negligible.
352 * @param mixer An initialized mixer handle.
353 * @returns 0 on success, -1 on failure
354 */
355int mixer_add_new_ctls(struct mixer *mixer)
Simon Wilson7e8a6562013-06-28 16:47:16 -0700356{
David Lie0f41062020-11-05 12:27:29 +0000357 int rc1 = 0, rc2 = 0;
358
359 if (!mixer)
360 return 0;
361
362 /* add the h_grp controls */
363 if (mixer->h_grp)
364 rc1 = add_controls(mixer, mixer->h_grp);
365
366#ifdef TINYALSA_USES_PLUGINS
367 /* add the v_grp controls */
368 if (mixer->v_grp)
369 rc2 = add_controls(mixer, mixer->v_grp);
370#endif
371
372 if (rc1 < 0)
373 return rc1;
374 if (rc2 < 0)
375 return rc2;
376
377 return 0;
Simon Wilson7e8a6562013-06-28 16:47:16 -0700378}
379
David Lie0f41062020-11-05 12:27:29 +0000380/** Gets the name of the mixer's card.
381 * @param mixer An initialized mixer handle.
382 * @returns The name of the mixer's card.
383 * @ingroup libtinyalsa-mixer
384 */
385const char *mixer_get_name(const struct mixer *mixer)
386{
387 return (const char *)mixer->card_info.name;
388}
389
390/** Gets the number of mixer controls for a given mixer.
391 * @param mixer An initialized mixer handle.
392 * @returns The number of mixer controls for the given mixer.
393 * @ingroup libtinyalsa-mixer
394 */
395unsigned int mixer_get_num_ctls(const struct mixer *mixer)
396{
397 if (!mixer)
398 return 0;
399
400 return mixer->total_count;
401}
402
403/** Gets the number of mixer controls, that go by a specified name, for a given mixer.
404 * @param mixer An initialized mixer handle.
405 * @param name The name of the mixer control
406 * @returns The number of mixer controls, specified by @p name, for the given mixer.
407 * @ingroup libtinyalsa-mixer
408 */
409unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
410{
411 struct mixer_ctl_group *grp;
412 unsigned int n;
413 unsigned int count = 0;
414 struct mixer_ctl *ctl;
415
416 if (!mixer)
417 return 0;
418
419 if (mixer->h_grp) {
420 grp = mixer->h_grp;
421 ctl = grp->ctl;
422
423 for (n = 0; n < grp->count; n++)
424 if (!strcmp(name, (char*) ctl[n].info.id.name))
425 count++;
426 }
427#ifdef TINYALSA_USES_PLUGINS
428 if (mixer->v_grp) {
429 grp = mixer->v_grp;
430 ctl = grp->ctl;
431
432 for (n = 0; n < grp->count; n++)
433 if (!strcmp(name, (char*) ctl[n].info.id.name))
434 count++;
435 }
436#endif
437
438 return count;
439}
440
441/** Subscribes for the mixer events.
442 * @param mixer A mixer handle.
443 * @param subscribe value indicating subscribe or unsubscribe for events
444 * @returns On success, zero.
445 * On failure, non-zero.
446 * @ingroup libtinyalsa-mixer
447 */
448int mixer_subscribe_events(struct mixer *mixer, int subscribe)
449{
450 struct mixer_ctl_group *grp;
451
452 if (mixer->h_grp) {
453 grp = mixer->h_grp;
454 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
455 return -1;
456 }
457
458#ifdef TINYALSA_USES_PLUGINS
459 if (mixer->v_grp) {
460 grp = mixer->v_grp;
461 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
462 return -1;
463 }
464#endif
465 return 0;
466}
467
468/** Wait for mixer events.
469 * @param mixer A mixer handle.
470 * @param timeout timout value
471 * @returns On success, 1.
472 * On failure, -errno.
473 * On timeout, 0
474 * @ingroup libtinyalsa-mixer
475 */
476int mixer_wait_event(struct mixer *mixer, int timeout)
477{
478 struct pollfd *pfd;
479 struct mixer_ctl_group *grp;
480 int count = 0, num_fds = 0, i, ret = 0;
481
482 if (mixer->fd >= 0)
483 num_fds++;
484
485#ifdef TINYALSA_USES_PLUGINS
486 if (mixer->v_grp)
487 num_fds++;
488#endif
489
490 pfd = (struct pollfd *)calloc(num_fds, sizeof(struct pollfd));
491 if (!pfd)
492 return -ENOMEM;
493
494 if (mixer->fd >= 0) {
495 pfd[count].fd = mixer->fd;
496 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
497 count++;
498 }
499
500#ifdef TINYALSA_USES_PLUGINS
501 if (mixer->v_grp) {
502 grp = mixer->v_grp;
503 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
504 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
505 count++;
506 }
507 }
508#endif
509
510 if (!count)
511 goto exit;
512
513 for (;;) {
514 int err;
515 err = poll(pfd, count, timeout);
516 if (err < 0) {
517 ret = -errno;
518 goto exit;
519 }
520 if (!err)
521 goto exit;
522
523 for (i = 0; i < count; i++) {
524 if (pfd[i].revents & (POLLERR | POLLNVAL)) {
525 ret = -EIO;
526 goto exit;
527 }
528 if (pfd[i].revents & (POLLIN | POLLOUT)) {
529 if ((i == 0) && mixer->fd >= 0) {
530 grp = mixer->h_grp;
531 grp->event_cnt++;
532 }
533#ifdef TINYALSA_USES_PLUGINS
534 else {
535 grp = mixer->v_grp;
536 grp->event_cnt++;
537 }
538#endif
539 ret = 1;
540 goto exit;
541 }
542 }
543 }
544exit:
545 free(pfd);
546 return ret;
547}
548
549/** Consume a mixer event.
550 * If mixer_subscribe_events has been called,
551 * mixer_wait_event will identify when a control value has changed.
552 * This function will clear a single event from the mixer so that
553 * further events can be alerted.
554 *
555 * @param mixer A mixer handle.
David Lifc1fe5f2020-11-05 12:29:26 +0000556 * @returns 1 on success. 0, if no pending event. -errno on failure.
David Lie0f41062020-11-05 12:27:29 +0000557 * @ingroup libtinyalsa-mixer
558 */
559int mixer_consume_event(struct mixer *mixer)
560{
David Lifc1fe5f2020-11-05 12:29:26 +0000561 struct mixer_ctl_event ev;
David Lie0f41062020-11-05 12:27:29 +0000562
563 return mixer_read_event(mixer, &ev);
564}
565
David Lifc1fe5f2020-11-05 12:29:26 +0000566/** Read a mixer control event.
567 * Try to read an control event from mixer.
568 *
569 * @param mixer A mixer handle.
570 * @param event Output parameter. If there is an event read form the mixer, this function will fill
571 * the event data into it.
572 * @returns 1 on success. 0, if no pending event. -errno on failure.
573 * @ingroup libtinyalsa-mixer
574 */
575int mixer_read_event(struct mixer *mixer, struct mixer_ctl_event *event)
David Lie0f41062020-11-05 12:27:29 +0000576{
David Lifc1fe5f2020-11-05 12:29:26 +0000577 struct mixer_ctl_group *grp = NULL;
578 struct snd_ctl_event ev;
579 ssize_t bytes = 0;
580
581 if (!mixer || !event) {
582 return -EINVAL;
583 }
David Lie0f41062020-11-05 12:27:29 +0000584
585 if (mixer->h_grp) {
David Lifc1fe5f2020-11-05 12:29:26 +0000586 if (mixer->h_grp->event_cnt > 0) {
587 grp = mixer->h_grp;
David Lie0f41062020-11-05 12:27:29 +0000588 }
589 }
590#ifdef TINYALSA_USES_PLUGINS
591 if (mixer->v_grp) {
David Lifc1fe5f2020-11-05 12:29:26 +0000592 if (mixer->v_grp->event_cnt > 0) {
593 grp = mixer->v_grp;
David Lie0f41062020-11-05 12:27:29 +0000594 }
595 }
596#endif
David Lifc1fe5f2020-11-05 12:29:26 +0000597 if (grp) {
598 grp->event_cnt--;
599 bytes = grp->ops->read_event(grp->data, &ev, sizeof(ev));
600
601 if (bytes < 0) {
602 return -errno;
603 }
604
605 if (bytes == sizeof(*event)) {
606 memcpy(event, &ev, sizeof(*event));
607 return 1;
608 }
609 }
610
David Lie0f41062020-11-05 12:27:29 +0000611 return 0;
612}
613
614static unsigned int mixer_grp_get_count(struct mixer_ctl_group *grp)
615{
616 if (!grp)
617 return 0;
618
619 return grp->count;
620}
621
622/** Gets a mixer control handle, by the mixer control's id.
623 * For non-const access, see @ref mixer_get_ctl
624 * @param mixer An initialized mixer handle.
625 * @param id The control's id in the given mixer.
626 * @returns A handle to the mixer control.
627 * @ingroup libtinyalsa-mixer
628 */
629const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
630{
631 unsigned int h_count;
632
633 if (!mixer || (id >= mixer->total_count))
634 return NULL;
635
636 h_count = mixer_grp_get_count(mixer->h_grp);
637
638 if (id < h_count)
639 return mixer->h_grp->ctl + id;
640#ifdef TINYALSA_USES_PLUGINS
641 else {
642 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
643 if ((id - h_count) < v_count)
644 return mixer->v_grp->ctl + (id - h_count);
645 }
646#endif
647
648 return NULL;
649}
650
651/** Gets a mixer control handle, by the mixer control's id.
652 * For const access, see @ref mixer_get_ctl_const
653 * @param mixer An initialized mixer handle.
654 * @param id The control's id in the given mixer.
655 * @returns A handle to the mixer control.
656 * @ingroup libtinyalsa-mixer
657 */
658struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
659{
660 unsigned int h_count;
661
662 if (!mixer || (id >= mixer->total_count))
663 return NULL;
664
665 h_count = mixer_grp_get_count(mixer->h_grp);
666
667 if (id < h_count)
668 return mixer->h_grp->ctl + id;
669#ifdef TINYALSA_USES_PLUGINS
670 else {
671 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
672 if ((id - h_count) < v_count)
673 return mixer->v_grp->ctl + (id - h_count);
674 }
675#endif
676 return NULL;
677}
678
679/** Gets the first instance of mixer control handle, by the mixer control's name.
680 * @param mixer An initialized mixer handle.
681 * @param name The control's name in the given mixer.
682 * @returns A handle to the mixer control.
683 * @ingroup libtinyalsa-mixer
684 */
685struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
686{
687 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
688}
689
690/** Gets an instance of mixer control handle, by the mixer control's name and index.
691 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
692 * @param mixer An initialized mixer handle.
693 * @param name The control's name in the given mixer.
694 * @param index The control's index.
695 * @returns A handle to the mixer control.
696 * @ingroup libtinyalsa-mixer
697 */
698struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
699 const char *name,
700 unsigned int index)
701{
702 struct mixer_ctl_group *grp;
703 unsigned int n;
704 struct mixer_ctl *ctl;
705
706 if (!mixer)
707 return NULL;
708
709 if (mixer->h_grp) {
710 grp = mixer->h_grp;
711 ctl = grp->ctl;
712
713 for (n = 0; n < grp->count; n++)
714 if (!strcmp(name, (char*) ctl[n].info.id.name))
715 if (index-- == 0)
716 return ctl + n;
717 }
718
719#ifdef TINYALSA_USES_PLUGINS
720 if (mixer->v_grp) {
721 grp = mixer->v_grp;
722 ctl = grp->ctl;
723
724 for (n = 0; n < grp->count; n++)
725 if (!strcmp(name, (char*) ctl[n].info.id.name))
726 if (index-- == 0)
727 return ctl + n;
728 }
729#endif
730 return NULL;
731}
732
733/** Updates the control's info.
734 * This is useful for a program that may be idle for a period of time.
735 * @param ctl An initialized control handle.
736 * @ingroup libtinyalsa-mixer
737 */
738void mixer_ctl_update(struct mixer_ctl *ctl)
739{
740 struct mixer_ctl_group *grp;
741
742 if (!ctl)
743 return;
744
745 grp = ctl->grp;
746 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &ctl->info);
747}
748
749/** Checks the control for TLV Read/Write access.
750 * @param ctl An initialized control handle.
751 * @returns On success, non-zero.
752 * On failure, zero.
753 * @ingroup libtinyalsa-mixer
754 */
755int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl)
756{
757 return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
758}
759
760/** Gets the control's ID.
761 * @param ctl An initialized control handle.
762 * @returns On success, the control's ID is returned.
763 * On error, UINT_MAX is returned instead.
764 * @ingroup libtinyalsa-mixer
765 */
766unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
767{
768 if (!ctl)
769 return UINT_MAX;
770
771 /* numid values start at 1, return a 0-base value that
772 * can be passed to mixer_get_ctl()
773 */
774 return ctl->info.id.numid - 1;
775}
776
777/** Gets the name of the control.
778 * @param ctl An initialized control handle.
779 * @returns On success, the name of the control.
780 * On error, NULL.
781 * @ingroup libtinyalsa-mixer
782 */
783const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700784{
Simon Wilsone44e30a2012-03-08 10:45:31 -0800785 if (!ctl)
786 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700787
David Lie0f41062020-11-05 12:27:29 +0000788 return (const char *)ctl->info.id.name;
Simon Wilsonedff7082011-06-06 15:33:34 -0700789}
790
David Lie0f41062020-11-05 12:27:29 +0000791/** Gets the value type of the control.
792 * @param ctl An initialized control handle
793 * @returns On success, the type of mixer control.
794 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
795 * @ingroup libtinyalsa-mixer
796 */
797enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700798{
799 if (!ctl)
800 return MIXER_CTL_TYPE_UNKNOWN;
801
David Lie0f41062020-11-05 12:27:29 +0000802 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700803 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
804 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
805 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
806 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
807 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
808 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
809 default: return MIXER_CTL_TYPE_UNKNOWN;
810 };
811}
812
David Lie0f41062020-11-05 12:27:29 +0000813/** Gets the string that describes the value type of the control.
814 * @param ctl An initialized control handle
815 * @returns On success, a string describing type of mixer control.
816 * @ingroup libtinyalsa-mixer
817 */
818const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700819{
820 if (!ctl)
821 return "";
822
David Lie0f41062020-11-05 12:27:29 +0000823 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700824 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
825 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
826 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
827 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
828 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
829 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
830 default: return "Unknown";
831 };
832}
833
David Lie0f41062020-11-05 12:27:29 +0000834/** Gets the number of values in the control.
835 * @param ctl An initialized control handle
836 * @returns The number of values in the mixer control
837 * @ingroup libtinyalsa-mixer
838 */
839unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700840{
841 if (!ctl)
842 return 0;
843
David Lie0f41062020-11-05 12:27:29 +0000844 return ctl->info.count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700845}
846
David Lie0f41062020-11-05 12:27:29 +0000847static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilsonedff7082011-06-06 15:33:34 -0700848{
David Lie0f41062020-11-05 12:27:29 +0000849 if ((percent > 100) || (percent < 0)) {
850 return -EINVAL;
851 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700852
David Lie0f41062020-11-05 12:27:29 +0000853 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilsonedff7082011-06-06 15:33:34 -0700854
855 return ei->value.integer.min + (range * percent) / 100;
856}
857
David Lie0f41062020-11-05 12:27:29 +0000858static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilsonedff7082011-06-06 15:33:34 -0700859{
860 int range = (ei->value.integer.max - ei->value.integer.min);
861
862 if (range == 0)
863 return 0;
864
David Lie0f41062020-11-05 12:27:29 +0000865 return ((value - ei->value.integer.min) * 100) / range;
Simon Wilsonedff7082011-06-06 15:33:34 -0700866}
867
David Lie0f41062020-11-05 12:27:29 +0000868/** Gets a percentage representation of a specified control value.
869 * @param ctl An initialized control handle.
870 * @param id The index of the value within the control.
871 * @returns On success, the percentage representation of the control value.
872 * On failure, -EINVAL is returned.
873 * @ingroup libtinyalsa-mixer
874 */
875int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700876{
David Lie0f41062020-11-05 12:27:29 +0000877 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700878 return -EINVAL;
879
David Lie0f41062020-11-05 12:27:29 +0000880 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsonedff7082011-06-06 15:33:34 -0700881}
882
David Lie0f41062020-11-05 12:27:29 +0000883/** Sets the value of a control by percent, specified by the value index.
884 * @param ctl An initialized control handle.
885 * @param id The index of the value to set
886 * @param percent A percentage value between 0 and 100.
887 * @returns On success, zero is returned.
888 * On failure, non-zero is returned.
889 * @ingroup libtinyalsa-mixer
890 */
Simon Wilsonedff7082011-06-06 15:33:34 -0700891int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
892{
David Lie0f41062020-11-05 12:27:29 +0000893 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700894 return -EINVAL;
895
David Lie0f41062020-11-05 12:27:29 +0000896 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsonedff7082011-06-06 15:33:34 -0700897}
898
David Lie0f41062020-11-05 12:27:29 +0000899/** Gets the value of a control.
900 * @param ctl An initialized control handle.
901 * @param id The index of the control value.
902 * @returns On success, the specified value is returned.
903 * On failure, -EINVAL is returned.
904 * @ingroup libtinyalsa-mixer
905 */
906int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700907{
David Lie0f41062020-11-05 12:27:29 +0000908 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700909 struct snd_ctl_elem_value ev;
910 int ret;
911
David Lie0f41062020-11-05 12:27:29 +0000912 if (!ctl || (id >= ctl->info.count))
Simon Wilsonedff7082011-06-06 15:33:34 -0700913 return -EINVAL;
914
David Lie0f41062020-11-05 12:27:29 +0000915 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700916 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +0000917 ev.id.numid = ctl->info.id.numid;
918 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700919 if (ret < 0)
920 return ret;
921
David Lie0f41062020-11-05 12:27:29 +0000922 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700923 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
924 return !!ev.value.integer.value[id];
925
926 case SNDRV_CTL_ELEM_TYPE_INTEGER:
927 return ev.value.integer.value[id];
928
929 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
930 return ev.value.enumerated.item[id];
931
Simon Wilson5aed71d2011-11-16 14:45:38 -0800932 case SNDRV_CTL_ELEM_TYPE_BYTES:
933 return ev.value.bytes.data[id];
934
Simon Wilsonedff7082011-06-06 15:33:34 -0700935 default:
936 return -EINVAL;
937 }
938
939 return 0;
940}
941
David Lie0f41062020-11-05 12:27:29 +0000942/** Gets the contents of a control's value array.
943 * @param ctl An initialized control handle.
944 * @param array A pointer to write the array data to.
945 * The size of this array must be equal to the number of items in the array
946 * multiplied by the size of each item.
947 * @param count The number of items in the array.
948 * This parameter must match the number of items in the control.
949 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
950 * @returns On success, zero.
951 * On failure, non-zero.
952 * @ingroup libtinyalsa-mixer
953 */
954int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530955{
David Lie0f41062020-11-05 12:27:29 +0000956 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800957 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530958 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700959 size_t size;
960 void *source;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800961
David Lie0f41062020-11-05 12:27:29 +0000962 if (!ctl || !count || !array)
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530963 return -EINVAL;
964
David Lie0f41062020-11-05 12:27:29 +0000965 grp = ctl->grp;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530966
David Lie0f41062020-11-05 12:27:29 +0000967 if (count > ctl->info.count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800968 return -EINVAL;
969
970 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +0000971 ev.id.numid = ctl->info.id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800972
David Lie0f41062020-11-05 12:27:29 +0000973 switch (ctl->info.type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700974 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
975 case SNDRV_CTL_ELEM_TYPE_INTEGER:
David Lie0f41062020-11-05 12:27:29 +0000976 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530977 if (ret < 0)
978 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700979 size = sizeof(ev.value.integer.value[0]);
980 source = ev.value.integer.value;
981 break;
982
983 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530984 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530985 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530986 struct snd_ctl_tlv *tlv;
987 int ret;
988
Ben Zhang6cb14f22016-04-22 17:59:40 -0700989 if (count > SIZE_MAX - sizeof(*tlv))
990 return -EINVAL;
David Lie0f41062020-11-05 12:27:29 +0000991
Mythri P K67b1df42014-08-18 15:39:36 +0530992 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700993 if (!tlv)
994 return -ENOMEM;
David Lie0f41062020-11-05 12:27:29 +0000995
996 tlv->numid = ctl->info.id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530997 tlv->length = count;
David Lie0f41062020-11-05 12:27:29 +0000998 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530999
1000 source = tlv->tlv;
1001 memcpy(array, source, count);
1002
1003 free(tlv);
1004
1005 return ret;
1006 } else {
David Lie0f41062020-11-05 12:27:29 +00001007 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +05301008 if (ret < 0)
1009 return ret;
1010 size = sizeof(ev.value.bytes.data[0]);
1011 source = ev.value.bytes.data;
1012 break;
1013 }
Simon Wilson8813fe82013-06-24 15:40:34 -07001014
Yogesh Agrawal49a61372013-08-02 20:04:58 +05301015 case SNDRV_CTL_ELEM_TYPE_IEC958:
1016 size = sizeof(ev.value.iec958);
1017 source = &ev.value.iec958;
1018 break;
1019
Simon Wilson8813fe82013-06-24 15:40:34 -07001020 default:
1021 return -EINVAL;
1022 }
1023
1024 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001025
1026 return 0;
1027}
1028
David Lie0f41062020-11-05 12:27:29 +00001029/** Sets the value of a control, specified by the value index.
1030 * @param ctl An initialized control handle.
1031 * @param id The index of the value within the control.
1032 * @param value The value to set.
1033 * This must be in a range specified by @ref mixer_ctl_get_range_min
1034 * and @ref mixer_ctl_get_range_max.
1035 * @returns On success, zero is returned.
1036 * On failure, non-zero is returned.
1037 * @ingroup libtinyalsa-mixer
1038 */
Simon Wilsonedff7082011-06-06 15:33:34 -07001039int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
1040{
David Lie0f41062020-11-05 12:27:29 +00001041 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -07001042 struct snd_ctl_elem_value ev;
1043 int ret;
1044
David Lie0f41062020-11-05 12:27:29 +00001045 if (!ctl || (id >= ctl->info.count))
Simon Wilsonedff7082011-06-06 15:33:34 -07001046 return -EINVAL;
1047
David Lie0f41062020-11-05 12:27:29 +00001048 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -07001049 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +00001050 ev.id.numid = ctl->info.id.numid;
1051 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -07001052 if (ret < 0)
1053 return ret;
1054
David Lie0f41062020-11-05 12:27:29 +00001055 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -07001056 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1057 ev.value.integer.value[id] = !!value;
1058 break;
1059
1060 case SNDRV_CTL_ELEM_TYPE_INTEGER:
David Lie0f41062020-11-05 12:27:29 +00001061 if ((value < mixer_ctl_get_range_min(ctl)) ||
1062 (value > mixer_ctl_get_range_max(ctl))) {
1063 return -EINVAL;
1064 }
1065
Simon Wilsonedff7082011-06-06 15:33:34 -07001066 ev.value.integer.value[id] = value;
1067 break;
1068
1069 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1070 ev.value.enumerated.item[id] = value;
1071 break;
1072
David.Coutheruta8481aa2013-06-11 16:22:57 +02001073 case SNDRV_CTL_ELEM_TYPE_BYTES:
1074 ev.value.bytes.data[id] = value;
1075 break;
1076
Simon Wilsonedff7082011-06-06 15:33:34 -07001077 default:
1078 return -EINVAL;
1079 }
1080
David Lie0f41062020-11-05 12:27:29 +00001081 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -07001082}
1083
David Lie0f41062020-11-05 12:27:29 +00001084/** Sets the contents of a control's value array.
1085 * @param ctl An initialized control handle.
1086 * @param array The array containing control values.
1087 * @param count The number of values in the array.
1088 * This must match the number of values in the control.
1089 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
1090 * @returns On success, zero.
1091 * On failure, non-zero.
1092 * @ingroup libtinyalsa-mixer
1093 */
Simon Wilson8813fe82013-06-24 15:40:34 -07001094int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001095{
David Lie0f41062020-11-05 12:27:29 +00001096 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001097 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -07001098 size_t size;
1099 void *dest;
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001100
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +05301101 if ((!ctl) || !count || !array)
1102 return -EINVAL;
1103
David Lie0f41062020-11-05 12:27:29 +00001104 grp = ctl->grp;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +05301105
David Lie0f41062020-11-05 12:27:29 +00001106 if (count > ctl->info.count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001107 return -EINVAL;
1108
1109 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +00001110 ev.id.numid = ctl->info.id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001111
David Lie0f41062020-11-05 12:27:29 +00001112 switch (ctl->info.type) {
Simon Wilson8813fe82013-06-24 15:40:34 -07001113 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1114 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1115 size = sizeof(ev.value.integer.value[0]);
1116 dest = ev.value.integer.value;
1117 break;
1118
1119 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +05301120 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +05301121 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +05301122 struct snd_ctl_tlv *tlv;
1123 int ret = 0;
David Lie0f41062020-11-05 12:27:29 +00001124
Ben Zhang6cb14f22016-04-22 17:59:40 -07001125 if (count > SIZE_MAX - sizeof(*tlv))
1126 return -EINVAL;
David Lie0f41062020-11-05 12:27:29 +00001127
Mythri P K67b1df42014-08-18 15:39:36 +05301128 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -07001129 if (!tlv)
1130 return -ENOMEM;
David Lie0f41062020-11-05 12:27:29 +00001131
1132 tlv->numid = ctl->info.id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +05301133 tlv->length = count;
1134 memcpy(tlv->tlv, array, count);
1135
David Lie0f41062020-11-05 12:27:29 +00001136 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +05301137 free(tlv);
1138
1139 return ret;
1140 } else {
1141 size = sizeof(ev.value.bytes.data[0]);
1142 dest = ev.value.bytes.data;
1143 }
Simon Wilson8813fe82013-06-24 15:40:34 -07001144 break;
1145
Yogesh Agrawal49a61372013-08-02 20:04:58 +05301146 case SNDRV_CTL_ELEM_TYPE_IEC958:
1147 size = sizeof(ev.value.iec958);
1148 dest = &ev.value.iec958;
1149 break;
1150
Simon Wilson8813fe82013-06-24 15:40:34 -07001151 default:
1152 return -EINVAL;
1153 }
1154
1155 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001156
David Lie0f41062020-11-05 12:27:29 +00001157 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001158}
1159
David Lie0f41062020-11-05 12:27:29 +00001160/** Gets the minimum value of an control.
1161 * The control must have an integer type.
1162 * The type of the control can be checked with @ref mixer_ctl_get_type.
1163 * @param ctl An initialized control handle.
1164 * @returns On success, the minimum value of the control.
1165 * On failure, -EINVAL.
1166 * @ingroup libtinyalsa-mixer
1167 */
1168int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -07001169{
David Lie0f41062020-11-05 12:27:29 +00001170 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -07001171 return -EINVAL;
1172
David Lie0f41062020-11-05 12:27:29 +00001173 return ctl->info.value.integer.min;
Simon Wilsonedff7082011-06-06 15:33:34 -07001174}
1175
David Lie0f41062020-11-05 12:27:29 +00001176/** Gets the maximum value of an control.
1177 * The control must have an integer type.
1178 * The type of the control can be checked with @ref mixer_ctl_get_type.
1179 * @param ctl An initialized control handle.
1180 * @returns On success, the maximum value of the control.
1181 * On failure, -EINVAL.
1182 * @ingroup libtinyalsa-mixer
1183 */
1184int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -07001185{
David Lie0f41062020-11-05 12:27:29 +00001186 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -07001187 return -EINVAL;
1188
David Lie0f41062020-11-05 12:27:29 +00001189 return ctl->info.value.integer.max;
Simon Wilsonedff7082011-06-06 15:33:34 -07001190}
1191
David Lie0f41062020-11-05 12:27:29 +00001192/** Get the number of enumerated items in the control.
1193 * @param ctl An initialized control handle.
1194 * @returns The number of enumerated items in the control.
1195 * @ingroup libtinyalsa-mixer
1196 */
1197unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -07001198{
1199 if (!ctl)
1200 return 0;
1201
David Lie0f41062020-11-05 12:27:29 +00001202 return ctl->info.value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -07001203}
1204
David Lie0f41062020-11-05 12:27:29 +00001205int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
1206{
1207 struct mixer_ctl_group *grp = ctl->grp;
1208 struct snd_ctl_elem_info tmp;
1209 unsigned int m;
1210 char **enames;
1211
1212 if (ctl->ename) {
1213 return 0;
1214 }
1215
1216 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
1217 if (!enames)
1218 goto fail;
1219 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
1220 memset(&tmp, 0, sizeof(tmp));
1221 tmp.id.numid = ctl->info.id.numid;
1222 tmp.value.enumerated.item = m;
1223 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
1224 goto fail;
1225 enames[m] = strdup(tmp.value.enumerated.name);
1226 if (!enames[m])
1227 goto fail;
1228 }
1229 ctl->ename = enames;
1230 return 0;
1231
1232fail:
1233 if (enames) {
1234 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
1235 if (enames[m]) {
1236 free(enames[m]);
1237 }
1238 }
1239 free(enames);
1240 }
1241 return -1;
1242}
1243
1244/** Gets the string representation of an enumerated item.
1245 * @param ctl An initialized control handle.
1246 * @param enum_id The index of the enumerated value.
1247 * @returns A string representation of the enumerated item.
1248 * @ingroup libtinyalsa-mixer
1249 */
Simon Wilsone44e30a2012-03-08 10:45:31 -08001250const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
1251 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -07001252{
David Lie0f41062020-11-05 12:27:29 +00001253 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
1254 (enum_id >= ctl->info.value.enumerated.items) ||
1255 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsone44e30a2012-03-08 10:45:31 -08001256 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -07001257
Simon Wilsone44e30a2012-03-08 10:45:31 -08001258 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -07001259}
1260
David Lie0f41062020-11-05 12:27:29 +00001261/** Set an enumeration value by string value.
1262 * @param ctl An enumerated mixer control.
1263 * @param string The string representation of an enumeration.
1264 * @returns On success, zero.
1265 * On failure, zero.
1266 * @ingroup libtinyalsa-mixer
1267 */
Simon Wilsonedff7082011-06-06 15:33:34 -07001268int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
1269{
David Lie0f41062020-11-05 12:27:29 +00001270 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -07001271 unsigned int i, num_enums;
1272 struct snd_ctl_elem_value ev;
1273 int ret;
1274
David Lie0f41062020-11-05 12:27:29 +00001275 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
1276 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonedff7082011-06-06 15:33:34 -07001277 return -EINVAL;
1278
David Lie0f41062020-11-05 12:27:29 +00001279 grp = ctl->grp;
1280 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -07001281 for (i = 0; i < num_enums; i++) {
1282 if (!strcmp(string, ctl->ename[i])) {
1283 memset(&ev, 0, sizeof(ev));
1284 ev.value.enumerated.item[0] = i;
David Lie0f41062020-11-05 12:27:29 +00001285 ev.id.numid = ctl->info.id.numid;
1286 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -07001287 if (ret < 0)
1288 return ret;
1289 return 0;
1290 }
1291 }
1292
1293 return -EINVAL;
1294}