blob: fe590e8d8ce1a8e626e2b60d778e8a59c1b6fd96 [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 Li940ec442020-12-09 02:11:18 +0000556 * @returns 0 on success. -errno on failure.
David Lie0f41062020-11-05 12:27:29 +0000557 * @ingroup libtinyalsa-mixer
558 */
559int mixer_consume_event(struct mixer *mixer)
560{
David Li940ec442020-12-09 02:11:18 +0000561 struct snd_ctl_event ev;
David Lie0f41062020-11-05 12:27:29 +0000562
563 return mixer_read_event(mixer, &ev);
564}
565
David Li940ec442020-12-09 02:11:18 +0000566int mixer_read_event(struct mixer *mixer, struct snd_ctl_event *ev)
David Lie0f41062020-11-05 12:27:29 +0000567{
David Li940ec442020-12-09 02:11:18 +0000568 struct mixer_ctl_group *grp;
569 ssize_t count = 0;
David Lie0f41062020-11-05 12:27:29 +0000570
571 if (mixer->h_grp) {
David Li940ec442020-12-09 02:11:18 +0000572 grp = mixer->h_grp;
573 if (grp->event_cnt) {
574 grp->event_cnt--;
575 count = grp->ops->read_event(grp->data, ev, sizeof(*ev));
576 return (count >= 0) ? 0 : -errno;
David Lie0f41062020-11-05 12:27:29 +0000577 }
578 }
579#ifdef TINYALSA_USES_PLUGINS
580 if (mixer->v_grp) {
David Li940ec442020-12-09 02:11:18 +0000581 grp = mixer->v_grp;
582 if (grp->event_cnt) {
583 grp->event_cnt--;
584 count = grp->ops->read_event(grp->data, ev, sizeof(*ev));
585 return (count >= 0) ? 0 : -errno;
David Lie0f41062020-11-05 12:27:29 +0000586 }
587 }
588#endif
589 return 0;
590}
591
592static unsigned int mixer_grp_get_count(struct mixer_ctl_group *grp)
593{
594 if (!grp)
595 return 0;
596
597 return grp->count;
598}
599
600/** Gets a mixer control handle, by the mixer control's id.
601 * For non-const access, see @ref mixer_get_ctl
602 * @param mixer An initialized mixer handle.
603 * @param id The control's id in the given mixer.
604 * @returns A handle to the mixer control.
605 * @ingroup libtinyalsa-mixer
606 */
607const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
608{
609 unsigned int h_count;
610
611 if (!mixer || (id >= mixer->total_count))
612 return NULL;
613
614 h_count = mixer_grp_get_count(mixer->h_grp);
615
616 if (id < h_count)
617 return mixer->h_grp->ctl + id;
618#ifdef TINYALSA_USES_PLUGINS
619 else {
620 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
621 if ((id - h_count) < v_count)
622 return mixer->v_grp->ctl + (id - h_count);
623 }
624#endif
625
626 return NULL;
627}
628
629/** Gets a mixer control handle, by the mixer control's id.
630 * For const access, see @ref mixer_get_ctl_const
631 * @param mixer An initialized mixer handle.
632 * @param id The control's id in the given mixer.
633 * @returns A handle to the mixer control.
634 * @ingroup libtinyalsa-mixer
635 */
636struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
637{
638 unsigned int h_count;
639
640 if (!mixer || (id >= mixer->total_count))
641 return NULL;
642
643 h_count = mixer_grp_get_count(mixer->h_grp);
644
645 if (id < h_count)
646 return mixer->h_grp->ctl + id;
647#ifdef TINYALSA_USES_PLUGINS
648 else {
649 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
650 if ((id - h_count) < v_count)
651 return mixer->v_grp->ctl + (id - h_count);
652 }
653#endif
654 return NULL;
655}
656
657/** Gets the first instance of mixer control handle, by the mixer control's name.
658 * @param mixer An initialized mixer handle.
659 * @param name The control's name in the given mixer.
660 * @returns A handle to the mixer control.
661 * @ingroup libtinyalsa-mixer
662 */
663struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
664{
665 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
666}
667
668/** Gets an instance of mixer control handle, by the mixer control's name and index.
669 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
670 * @param mixer An initialized mixer handle.
671 * @param name The control's name in the given mixer.
672 * @param index The control's index.
673 * @returns A handle to the mixer control.
674 * @ingroup libtinyalsa-mixer
675 */
676struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
677 const char *name,
678 unsigned int index)
679{
680 struct mixer_ctl_group *grp;
681 unsigned int n;
682 struct mixer_ctl *ctl;
683
684 if (!mixer)
685 return NULL;
686
687 if (mixer->h_grp) {
688 grp = mixer->h_grp;
689 ctl = grp->ctl;
690
691 for (n = 0; n < grp->count; n++)
692 if (!strcmp(name, (char*) ctl[n].info.id.name))
693 if (index-- == 0)
694 return ctl + n;
695 }
696
697#ifdef TINYALSA_USES_PLUGINS
698 if (mixer->v_grp) {
699 grp = mixer->v_grp;
700 ctl = grp->ctl;
701
702 for (n = 0; n < grp->count; n++)
703 if (!strcmp(name, (char*) ctl[n].info.id.name))
704 if (index-- == 0)
705 return ctl + n;
706 }
707#endif
708 return NULL;
709}
710
711/** Updates the control's info.
712 * This is useful for a program that may be idle for a period of time.
713 * @param ctl An initialized control handle.
714 * @ingroup libtinyalsa-mixer
715 */
716void mixer_ctl_update(struct mixer_ctl *ctl)
717{
718 struct mixer_ctl_group *grp;
719
720 if (!ctl)
721 return;
722
723 grp = ctl->grp;
724 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &ctl->info);
725}
726
727/** Checks the control for TLV Read/Write access.
728 * @param ctl An initialized control handle.
729 * @returns On success, non-zero.
730 * On failure, zero.
731 * @ingroup libtinyalsa-mixer
732 */
733int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl)
734{
735 return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
736}
737
738/** Gets the control's ID.
739 * @param ctl An initialized control handle.
740 * @returns On success, the control's ID is returned.
741 * On error, UINT_MAX is returned instead.
742 * @ingroup libtinyalsa-mixer
743 */
744unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
745{
746 if (!ctl)
747 return UINT_MAX;
748
749 /* numid values start at 1, return a 0-base value that
750 * can be passed to mixer_get_ctl()
751 */
752 return ctl->info.id.numid - 1;
753}
754
755/** Gets the name of the control.
756 * @param ctl An initialized control handle.
757 * @returns On success, the name of the control.
758 * On error, NULL.
759 * @ingroup libtinyalsa-mixer
760 */
761const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700762{
Simon Wilsone44e30a2012-03-08 10:45:31 -0800763 if (!ctl)
764 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700765
David Lie0f41062020-11-05 12:27:29 +0000766 return (const char *)ctl->info.id.name;
Simon Wilsonedff7082011-06-06 15:33:34 -0700767}
768
David Lie0f41062020-11-05 12:27:29 +0000769/** Gets the value type of the control.
770 * @param ctl An initialized control handle
771 * @returns On success, the type of mixer control.
772 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
773 * @ingroup libtinyalsa-mixer
774 */
775enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700776{
777 if (!ctl)
778 return MIXER_CTL_TYPE_UNKNOWN;
779
David Lie0f41062020-11-05 12:27:29 +0000780 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700781 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
782 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
783 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
784 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
785 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
786 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
787 default: return MIXER_CTL_TYPE_UNKNOWN;
788 };
789}
790
David Lie0f41062020-11-05 12:27:29 +0000791/** Gets the string that describes the value type of the control.
792 * @param ctl An initialized control handle
793 * @returns On success, a string describing type of mixer control.
794 * @ingroup libtinyalsa-mixer
795 */
796const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700797{
798 if (!ctl)
799 return "";
800
David Lie0f41062020-11-05 12:27:29 +0000801 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700802 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
803 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
804 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
805 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
806 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
807 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
808 default: return "Unknown";
809 };
810}
811
David Lie0f41062020-11-05 12:27:29 +0000812/** Gets the number of values in the control.
813 * @param ctl An initialized control handle
814 * @returns The number of values in the mixer control
815 * @ingroup libtinyalsa-mixer
816 */
817unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700818{
819 if (!ctl)
820 return 0;
821
David Lie0f41062020-11-05 12:27:29 +0000822 return ctl->info.count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700823}
824
David Lie0f41062020-11-05 12:27:29 +0000825static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilsonedff7082011-06-06 15:33:34 -0700826{
David Lie0f41062020-11-05 12:27:29 +0000827 if ((percent > 100) || (percent < 0)) {
828 return -EINVAL;
829 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700830
David Lie0f41062020-11-05 12:27:29 +0000831 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilsonedff7082011-06-06 15:33:34 -0700832
833 return ei->value.integer.min + (range * percent) / 100;
834}
835
David Lie0f41062020-11-05 12:27:29 +0000836static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilsonedff7082011-06-06 15:33:34 -0700837{
838 int range = (ei->value.integer.max - ei->value.integer.min);
839
840 if (range == 0)
841 return 0;
842
David Lie0f41062020-11-05 12:27:29 +0000843 return ((value - ei->value.integer.min) * 100) / range;
Simon Wilsonedff7082011-06-06 15:33:34 -0700844}
845
David Lie0f41062020-11-05 12:27:29 +0000846/** Gets a percentage representation of a specified control value.
847 * @param ctl An initialized control handle.
848 * @param id The index of the value within the control.
849 * @returns On success, the percentage representation of the control value.
850 * On failure, -EINVAL is returned.
851 * @ingroup libtinyalsa-mixer
852 */
853int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700854{
David Lie0f41062020-11-05 12:27:29 +0000855 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700856 return -EINVAL;
857
David Lie0f41062020-11-05 12:27:29 +0000858 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsonedff7082011-06-06 15:33:34 -0700859}
860
David Lie0f41062020-11-05 12:27:29 +0000861/** Sets the value of a control by percent, specified by the value index.
862 * @param ctl An initialized control handle.
863 * @param id The index of the value to set
864 * @param percent A percentage value between 0 and 100.
865 * @returns On success, zero is returned.
866 * On failure, non-zero is returned.
867 * @ingroup libtinyalsa-mixer
868 */
Simon Wilsonedff7082011-06-06 15:33:34 -0700869int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
870{
David Lie0f41062020-11-05 12:27:29 +0000871 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -0700872 return -EINVAL;
873
David Lie0f41062020-11-05 12:27:29 +0000874 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsonedff7082011-06-06 15:33:34 -0700875}
876
David Lie0f41062020-11-05 12:27:29 +0000877/** Gets the value of a control.
878 * @param ctl An initialized control handle.
879 * @param id The index of the control value.
880 * @returns On success, the specified value is returned.
881 * On failure, -EINVAL is returned.
882 * @ingroup libtinyalsa-mixer
883 */
884int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700885{
David Lie0f41062020-11-05 12:27:29 +0000886 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700887 struct snd_ctl_elem_value ev;
888 int ret;
889
David Lie0f41062020-11-05 12:27:29 +0000890 if (!ctl || (id >= ctl->info.count))
Simon Wilsonedff7082011-06-06 15:33:34 -0700891 return -EINVAL;
892
David Lie0f41062020-11-05 12:27:29 +0000893 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700894 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +0000895 ev.id.numid = ctl->info.id.numid;
896 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700897 if (ret < 0)
898 return ret;
899
David Lie0f41062020-11-05 12:27:29 +0000900 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -0700901 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
902 return !!ev.value.integer.value[id];
903
904 case SNDRV_CTL_ELEM_TYPE_INTEGER:
905 return ev.value.integer.value[id];
906
907 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
908 return ev.value.enumerated.item[id];
909
Simon Wilson5aed71d2011-11-16 14:45:38 -0800910 case SNDRV_CTL_ELEM_TYPE_BYTES:
911 return ev.value.bytes.data[id];
912
Simon Wilsonedff7082011-06-06 15:33:34 -0700913 default:
914 return -EINVAL;
915 }
916
917 return 0;
918}
919
David Lie0f41062020-11-05 12:27:29 +0000920/** Gets the contents of a control's value array.
921 * @param ctl An initialized control handle.
922 * @param array A pointer to write the array data to.
923 * The size of this array must be equal to the number of items in the array
924 * multiplied by the size of each item.
925 * @param count The number of items in the array.
926 * This parameter must match the number of items in the control.
927 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
928 * @returns On success, zero.
929 * On failure, non-zero.
930 * @ingroup libtinyalsa-mixer
931 */
932int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530933{
David Lie0f41062020-11-05 12:27:29 +0000934 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800935 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530936 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700937 size_t size;
938 void *source;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800939
David Lie0f41062020-11-05 12:27:29 +0000940 if (!ctl || !count || !array)
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530941 return -EINVAL;
942
David Lie0f41062020-11-05 12:27:29 +0000943 grp = ctl->grp;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530944
David Lie0f41062020-11-05 12:27:29 +0000945 if (count > ctl->info.count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800946 return -EINVAL;
947
948 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +0000949 ev.id.numid = ctl->info.id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800950
David Lie0f41062020-11-05 12:27:29 +0000951 switch (ctl->info.type) {
Simon Wilson8813fe82013-06-24 15:40:34 -0700952 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
953 case SNDRV_CTL_ELEM_TYPE_INTEGER:
David Lie0f41062020-11-05 12:27:29 +0000954 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530955 if (ret < 0)
956 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700957 size = sizeof(ev.value.integer.value[0]);
958 source = ev.value.integer.value;
959 break;
960
961 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530962 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530963 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530964 struct snd_ctl_tlv *tlv;
965 int ret;
966
Ben Zhang6cb14f22016-04-22 17:59:40 -0700967 if (count > SIZE_MAX - sizeof(*tlv))
968 return -EINVAL;
David Lie0f41062020-11-05 12:27:29 +0000969
Mythri P K67b1df42014-08-18 15:39:36 +0530970 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700971 if (!tlv)
972 return -ENOMEM;
David Lie0f41062020-11-05 12:27:29 +0000973
974 tlv->numid = ctl->info.id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +0530975 tlv->length = count;
David Lie0f41062020-11-05 12:27:29 +0000976 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530977
978 source = tlv->tlv;
979 memcpy(array, source, count);
980
981 free(tlv);
982
983 return ret;
984 } else {
David Lie0f41062020-11-05 12:27:29 +0000985 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530986 if (ret < 0)
987 return ret;
988 size = sizeof(ev.value.bytes.data[0]);
989 source = ev.value.bytes.data;
990 break;
991 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700992
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530993 case SNDRV_CTL_ELEM_TYPE_IEC958:
994 size = sizeof(ev.value.iec958);
995 source = &ev.value.iec958;
996 break;
997
Simon Wilson8813fe82013-06-24 15:40:34 -0700998 default:
999 return -EINVAL;
1000 }
1001
1002 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001003
1004 return 0;
1005}
1006
David Lie0f41062020-11-05 12:27:29 +00001007/** Sets the value of a control, specified by the value index.
1008 * @param ctl An initialized control handle.
1009 * @param id The index of the value within the control.
1010 * @param value The value to set.
1011 * This must be in a range specified by @ref mixer_ctl_get_range_min
1012 * and @ref mixer_ctl_get_range_max.
1013 * @returns On success, zero is returned.
1014 * On failure, non-zero is returned.
1015 * @ingroup libtinyalsa-mixer
1016 */
Simon Wilsonedff7082011-06-06 15:33:34 -07001017int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
1018{
David Lie0f41062020-11-05 12:27:29 +00001019 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -07001020 struct snd_ctl_elem_value ev;
1021 int ret;
1022
David Lie0f41062020-11-05 12:27:29 +00001023 if (!ctl || (id >= ctl->info.count))
Simon Wilsonedff7082011-06-06 15:33:34 -07001024 return -EINVAL;
1025
David Lie0f41062020-11-05 12:27:29 +00001026 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -07001027 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +00001028 ev.id.numid = ctl->info.id.numid;
1029 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -07001030 if (ret < 0)
1031 return ret;
1032
David Lie0f41062020-11-05 12:27:29 +00001033 switch (ctl->info.type) {
Simon Wilsonedff7082011-06-06 15:33:34 -07001034 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1035 ev.value.integer.value[id] = !!value;
1036 break;
1037
1038 case SNDRV_CTL_ELEM_TYPE_INTEGER:
David Lie0f41062020-11-05 12:27:29 +00001039 if ((value < mixer_ctl_get_range_min(ctl)) ||
1040 (value > mixer_ctl_get_range_max(ctl))) {
1041 return -EINVAL;
1042 }
1043
Simon Wilsonedff7082011-06-06 15:33:34 -07001044 ev.value.integer.value[id] = value;
1045 break;
1046
1047 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1048 ev.value.enumerated.item[id] = value;
1049 break;
1050
David.Coutheruta8481aa2013-06-11 16:22:57 +02001051 case SNDRV_CTL_ELEM_TYPE_BYTES:
1052 ev.value.bytes.data[id] = value;
1053 break;
1054
Simon Wilsonedff7082011-06-06 15:33:34 -07001055 default:
1056 return -EINVAL;
1057 }
1058
David Lie0f41062020-11-05 12:27:29 +00001059 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -07001060}
1061
David Lie0f41062020-11-05 12:27:29 +00001062/** Sets the contents of a control's value array.
1063 * @param ctl An initialized control handle.
1064 * @param array The array containing control values.
1065 * @param count The number of values in the array.
1066 * This must match the number of values in the control.
1067 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
1068 * @returns On success, zero.
1069 * On failure, non-zero.
1070 * @ingroup libtinyalsa-mixer
1071 */
Simon Wilson8813fe82013-06-24 15:40:34 -07001072int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001073{
David Lie0f41062020-11-05 12:27:29 +00001074 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001075 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -07001076 size_t size;
1077 void *dest;
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001078
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +05301079 if ((!ctl) || !count || !array)
1080 return -EINVAL;
1081
David Lie0f41062020-11-05 12:27:29 +00001082 grp = ctl->grp;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +05301083
David Lie0f41062020-11-05 12:27:29 +00001084 if (count > ctl->info.count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001085 return -EINVAL;
1086
1087 memset(&ev, 0, sizeof(ev));
David Lie0f41062020-11-05 12:27:29 +00001088 ev.id.numid = ctl->info.id.numid;
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001089
David Lie0f41062020-11-05 12:27:29 +00001090 switch (ctl->info.type) {
Simon Wilson8813fe82013-06-24 15:40:34 -07001091 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1092 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1093 size = sizeof(ev.value.integer.value[0]);
1094 dest = ev.value.integer.value;
1095 break;
1096
1097 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +05301098 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +05301099 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +05301100 struct snd_ctl_tlv *tlv;
1101 int ret = 0;
David Lie0f41062020-11-05 12:27:29 +00001102
Ben Zhang6cb14f22016-04-22 17:59:40 -07001103 if (count > SIZE_MAX - sizeof(*tlv))
1104 return -EINVAL;
David Lie0f41062020-11-05 12:27:29 +00001105
Mythri P K67b1df42014-08-18 15:39:36 +05301106 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -07001107 if (!tlv)
1108 return -ENOMEM;
David Lie0f41062020-11-05 12:27:29 +00001109
1110 tlv->numid = ctl->info.id.numid;
Mythri P K67b1df42014-08-18 15:39:36 +05301111 tlv->length = count;
1112 memcpy(tlv->tlv, array, count);
1113
David Lie0f41062020-11-05 12:27:29 +00001114 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +05301115 free(tlv);
1116
1117 return ret;
1118 } else {
1119 size = sizeof(ev.value.bytes.data[0]);
1120 dest = ev.value.bytes.data;
1121 }
Simon Wilson8813fe82013-06-24 15:40:34 -07001122 break;
1123
Yogesh Agrawal49a61372013-08-02 20:04:58 +05301124 case SNDRV_CTL_ELEM_TYPE_IEC958:
1125 size = sizeof(ev.value.iec958);
1126 dest = &ev.value.iec958;
1127 break;
1128
Simon Wilson8813fe82013-06-24 15:40:34 -07001129 default:
1130 return -EINVAL;
1131 }
1132
1133 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001134
David Lie0f41062020-11-05 12:27:29 +00001135 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonda39e0b2012-11-09 15:16:47 -08001136}
1137
David Lie0f41062020-11-05 12:27:29 +00001138/** Gets the minimum value of an control.
1139 * The control must have an integer type.
1140 * The type of the control can be checked with @ref mixer_ctl_get_type.
1141 * @param ctl An initialized control handle.
1142 * @returns On success, the minimum value of the control.
1143 * On failure, -EINVAL.
1144 * @ingroup libtinyalsa-mixer
1145 */
1146int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -07001147{
David Lie0f41062020-11-05 12:27:29 +00001148 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -07001149 return -EINVAL;
1150
David Lie0f41062020-11-05 12:27:29 +00001151 return ctl->info.value.integer.min;
Simon Wilsonedff7082011-06-06 15:33:34 -07001152}
1153
David Lie0f41062020-11-05 12:27:29 +00001154/** Gets the maximum value of an control.
1155 * The control must have an integer type.
1156 * The type of the control can be checked with @ref mixer_ctl_get_type.
1157 * @param ctl An initialized control handle.
1158 * @returns On success, the maximum value of the control.
1159 * On failure, -EINVAL.
1160 * @ingroup libtinyalsa-mixer
1161 */
1162int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -07001163{
David Lie0f41062020-11-05 12:27:29 +00001164 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilsonedff7082011-06-06 15:33:34 -07001165 return -EINVAL;
1166
David Lie0f41062020-11-05 12:27:29 +00001167 return ctl->info.value.integer.max;
Simon Wilsonedff7082011-06-06 15:33:34 -07001168}
1169
David Lie0f41062020-11-05 12:27:29 +00001170/** Get the number of enumerated items in the control.
1171 * @param ctl An initialized control handle.
1172 * @returns The number of enumerated items in the control.
1173 * @ingroup libtinyalsa-mixer
1174 */
1175unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -07001176{
1177 if (!ctl)
1178 return 0;
1179
David Lie0f41062020-11-05 12:27:29 +00001180 return ctl->info.value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -07001181}
1182
David Lie0f41062020-11-05 12:27:29 +00001183int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
1184{
1185 struct mixer_ctl_group *grp = ctl->grp;
1186 struct snd_ctl_elem_info tmp;
1187 unsigned int m;
1188 char **enames;
1189
1190 if (ctl->ename) {
1191 return 0;
1192 }
1193
1194 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
1195 if (!enames)
1196 goto fail;
1197 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
1198 memset(&tmp, 0, sizeof(tmp));
1199 tmp.id.numid = ctl->info.id.numid;
1200 tmp.value.enumerated.item = m;
1201 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
1202 goto fail;
1203 enames[m] = strdup(tmp.value.enumerated.name);
1204 if (!enames[m])
1205 goto fail;
1206 }
1207 ctl->ename = enames;
1208 return 0;
1209
1210fail:
1211 if (enames) {
1212 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
1213 if (enames[m]) {
1214 free(enames[m]);
1215 }
1216 }
1217 free(enames);
1218 }
1219 return -1;
1220}
1221
1222/** Gets the string representation of an enumerated item.
1223 * @param ctl An initialized control handle.
1224 * @param enum_id The index of the enumerated value.
1225 * @returns A string representation of the enumerated item.
1226 * @ingroup libtinyalsa-mixer
1227 */
Simon Wilsone44e30a2012-03-08 10:45:31 -08001228const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
1229 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -07001230{
David Lie0f41062020-11-05 12:27:29 +00001231 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
1232 (enum_id >= ctl->info.value.enumerated.items) ||
1233 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsone44e30a2012-03-08 10:45:31 -08001234 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -07001235
Simon Wilsone44e30a2012-03-08 10:45:31 -08001236 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -07001237}
1238
David Lie0f41062020-11-05 12:27:29 +00001239/** Set an enumeration value by string value.
1240 * @param ctl An enumerated mixer control.
1241 * @param string The string representation of an enumeration.
1242 * @returns On success, zero.
1243 * On failure, zero.
1244 * @ingroup libtinyalsa-mixer
1245 */
Simon Wilsonedff7082011-06-06 15:33:34 -07001246int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
1247{
David Lie0f41062020-11-05 12:27:29 +00001248 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -07001249 unsigned int i, num_enums;
1250 struct snd_ctl_elem_value ev;
1251 int ret;
1252
David Lie0f41062020-11-05 12:27:29 +00001253 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
1254 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonedff7082011-06-06 15:33:34 -07001255 return -EINVAL;
1256
David Lie0f41062020-11-05 12:27:29 +00001257 grp = ctl->grp;
1258 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonedff7082011-06-06 15:33:34 -07001259 for (i = 0; i < num_enums; i++) {
1260 if (!strcmp(string, ctl->ename[i])) {
1261 memset(&ev, 0, sizeof(ev));
1262 ev.value.enumerated.item[0] = i;
David Lie0f41062020-11-05 12:27:29 +00001263 ev.id.numid = ctl->info.id.numid;
1264 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -07001265 if (ret < 0)
1266 return ret;
1267 return 0;
1268 }
1269 }
1270
1271 return -EINVAL;
1272}