blob: da043ac0cd72b6857051e78b1b8ef6da31cf3b5a [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
John Muir22bca932017-10-23 10:58:12 -070029#include <stdbool.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070030#include <stdio.h>
31#include <stdlib.h>
Ben Zhang6cb14f22016-04-22 17:59:40 -070032#include <stdint.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>
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +053038#include <poll.h>
Simon Wilsonedff7082011-06-06 15:33:34 -070039
Simon Wilson85dc38f2012-05-15 17:37:19 -070040#include <sys/ioctl.h>
41
Simon Wilsonedff7082011-06-06 15:33:34 -070042#include <linux/ioctl.h>
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -070043
Simon Wilsonedff7082011-06-06 15:33:34 -070044#define __force
45#define __bitwise
46#define __user
47#include <sound/asound.h>
48
John Muir22bca932017-10-23 10:58:12 -070049#ifndef SNDRV_CTL_ELEM_ID_NAME_MAXLEN
50#define SNDRV_CTL_ELEM_ID_NAME_MAXLEN 44
51#endif
52
Simon Wilsonedff7082011-06-06 15:33:34 -070053#include <tinyalsa/asoundlib.h>
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -070054#include "mixer_io.h"
Simon Wilsonedff7082011-06-06 15:33:34 -070055
56struct mixer_ctl {
57 struct mixer *mixer;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -070058 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -070059 struct snd_ctl_elem_info *info;
60 char **ename;
John Muir22bca932017-10-23 10:58:12 -070061 bool info_retrieved;
Simon Wilsonedff7082011-06-06 15:33:34 -070062};
63
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -070064struct mixer_ctl_group {
Simon Wilson7e8a6562013-06-28 16:47:16 -070065 struct snd_ctl_elem_info *elem_info;
Simon Wilsonedff7082011-06-06 15:33:34 -070066 struct mixer_ctl *ctl;
67 unsigned int count;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -070068 int event_cnt;
69
70 struct mixer_ops *ops;
71 void *data;
Simon Wilsonedff7082011-06-06 15:33:34 -070072};
73
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -070074struct mixer {
75 int fd;
76 struct snd_ctl_card_info card_info;
77
78 /* hardware/physical mixer control group */
79 struct mixer_ctl_group *hw_grp;
80
81 /*
82 * Virutal mixer control group.
83 * Currently supports one virtual mixer (.so)
84 * per card. Could be extended to multiple
85 */
86 struct mixer_ctl_group *virt_grp;
87
88 unsigned int total_ctl_count;
89};
90
91static void mixer_grp_close(struct mixer_ctl_group *grp)
92{
93 unsigned int n, m;
94
95 if (!grp)
96 return;
97
98 if (grp->ctl) {
99 for (n = 0; n < grp->count; n++) {
100 if (grp->ctl[n].ename) {
101 unsigned int max = grp->ctl[n].info->value.enumerated.items;
102 for (m = 0; m < max; m++)
103 free(grp->ctl[n].ename[m]);
104 free(grp->ctl[n].ename);
105 }
106 }
107 free(grp->ctl);
108 }
109
110 if (grp->elem_info)
111 free(grp->elem_info);
112
113 free(grp);
114}
115
Simon Wilsonedff7082011-06-06 15:33:34 -0700116void mixer_close(struct mixer *mixer)
117{
Simon Wilsonedff7082011-06-06 15:33:34 -0700118 if (!mixer)
119 return;
120
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700121 if (mixer->fd >= 0 && mixer->hw_grp)
122 mixer->hw_grp->ops->close(mixer->hw_grp->data);
123 mixer_grp_close(mixer->hw_grp);
Simon Wilsonedff7082011-06-06 15:33:34 -0700124
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700125 if (mixer->virt_grp)
126 mixer->virt_grp->ops->close(mixer->virt_grp->data);
127 mixer_grp_close(mixer->virt_grp);
Simon Wilsonedff7082011-06-06 15:33:34 -0700128
129 free(mixer);
130
131 /* TODO: verify frees */
132}
133
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700134static int mixer_grp_open(struct mixer *mixer,
135 struct mixer_ctl_group **ctl_grp,
136 struct mixer_ops *ops,
137 void *data, int *num_ctls_in_grp)
Simon Wilsonedff7082011-06-06 15:33:34 -0700138{
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700139 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700140 struct snd_ctl_elem_list elist;
Simon Wilsonedff7082011-06-06 15:33:34 -0700141 struct snd_ctl_elem_id *eid = NULL;
John Muir22bca932017-10-23 10:58:12 -0700142 unsigned int n;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700143 int ret;
Simon Wilsonedff7082011-06-06 15:33:34 -0700144
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700145 grp = calloc(1, sizeof(*grp));
146 if (!grp)
147 return -ENOMEM;
Simon Wilsonedff7082011-06-06 15:33:34 -0700148
149 memset(&elist, 0, sizeof(elist));
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700150 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist);
151 if (ret < 0)
152 goto err_get_elem_list;
Simon Wilsonedff7082011-06-06 15:33:34 -0700153
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700154 grp->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
155 grp->elem_info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
156 if (!grp->ctl || !grp->elem_info) {
157 ret = -ENOMEM;
158 goto err_ctl_alloc;
159 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700160
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700161 eid = calloc(elist.count, sizeof(*eid));
162 if (!eid) {
163 ret = -ENOMEM;
164 goto err_ctl_alloc;
165 }
Simon Wilson7e8a6562013-06-28 16:47:16 -0700166
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700167 grp->count = elist.count;
168 elist.space = grp->count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700169 elist.pids = eid;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700170 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist);
171 if (ret < 0)
172 goto err_ctl_alloc;
Simon Wilsonedff7082011-06-06 15:33:34 -0700173
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700174 for (n = 0; n < grp->count; n++) {
175 struct mixer_ctl *ctl = grp->ctl + n;
John Muir22bca932017-10-23 10:58:12 -0700176
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700177 ctl->grp = grp;
John Muir22bca932017-10-23 10:58:12 -0700178 ctl->mixer = mixer;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700179 ctl->info = grp->elem_info + n;
John Muir22bca932017-10-23 10:58:12 -0700180 ctl->info->id.numid = eid[n].numid;
181 strncpy((char *)ctl->info->id.name, (char *)eid[n].name,
182 SNDRV_CTL_ELEM_ID_NAME_MAXLEN);
183 ctl->info->id.name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1] = 0;
Simon Wilsonedff7082011-06-06 15:33:34 -0700184 }
185
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700186 grp->data = data;
187 grp->ops = ops;
188 *ctl_grp = grp;
189 *num_ctls_in_grp = grp->count;
190
Simon Wilsonedff7082011-06-06 15:33:34 -0700191 free(eid);
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700192 return 0;
193
194err_ctl_alloc:
195
196 if (eid)
197 free(eid);
198
199 if (grp->elem_info)
200 free(grp->elem_info);
201
202 if (grp->ctl)
203 free(grp->ctl);
204
205err_get_elem_list:
206
207 free(grp);
208 return ret;
209
210}
211
212static int mixer_do_hw_open(struct mixer *mixer, unsigned int card)
213{
214 struct mixer_ops *ops;
215 void *data;
216 int fd, ret, num_grp_ctls = 0;
217
218 mixer->fd = -1;
219 fd = mixer_hw_open(card, &data, &ops);
220 if (fd < 0)
221 return fd;
222
223 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info);
224 if (ret < 0)
225 goto err_card_info;
226
227 ret = mixer_grp_open(mixer, &mixer->hw_grp, ops, data, &num_grp_ctls);
228 if (ret < 0)
229 goto err_card_info;
230
231 mixer->total_ctl_count += num_grp_ctls;
232
233 mixer->fd = fd;
234 return 0;
235
236err_card_info:
237 ops->close(data);
238 return ret;
239
240}
241
242static int mixer_do_plugin_open(struct mixer *mixer, unsigned int card,
243 int is_hw_open_failed)
244{
245 struct mixer_ops *ops;
246 void *data;
247 int ret, num_grp_ctls = 0;
248
249 ret = mixer_plugin_open(card, &data, &ops);
250 if (ret < 0)
251 return ret;
252
253 /* Get card_info if hw_open failed */
254 if (is_hw_open_failed) {
255 ret = ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info);
256 if (ret < 0)
257 goto err_card_info;
258 }
259
260 ret = mixer_grp_open(mixer, &mixer->virt_grp, ops, data, &num_grp_ctls);
261 if (ret < 0)
262 goto err_card_info;
263
264 mixer->total_ctl_count += num_grp_ctls;
265 return 0;
266
267err_card_info:
268 ops->close(data);
269 return ret;
270
271}
272
273struct mixer *mixer_open(unsigned int card)
274{
275 struct mixer *mixer = NULL;
276 int h_status, v_status;
277
278 mixer = calloc(1, sizeof(*mixer));
279 if (!mixer)
280 goto fail;
281
282 /* open hardware node first */
283 h_status = mixer_do_hw_open(mixer, card);
284
285 /*
286 * open the virtual node even if hw_open fails
287 * since hw_open is expected to fail for virtual cards
288 * for which kernel does not register mixer node
289 */
290 //TODO: open virtual node only if mixer is defined under snd-card-def
291 v_status = mixer_do_plugin_open(mixer, card, h_status);
292
293 /* Fail mixer_open if both hw and plugin nodes cannot be opened */
294 if (h_status < 0 && v_status < 0)
295 goto fail;
296
Simon Wilsonedff7082011-06-06 15:33:34 -0700297 return mixer;
298
299fail:
Simon Wilsonedff7082011-06-06 15:33:34 -0700300 if (mixer)
301 mixer_close(mixer);
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700302
Simon Wilsonedff7082011-06-06 15:33:34 -0700303 return 0;
304}
305
John Muir22bca932017-10-23 10:58:12 -0700306static bool mixer_ctl_get_elem_info(struct mixer_ctl* ctl)
307{
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700308 struct mixer_ctl_group *grp = ctl->grp;
309 unsigned int i;
310
John Muir22bca932017-10-23 10:58:12 -0700311 if (!ctl->info_retrieved) {
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700312 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO,
313 ctl->info) < 0)
John Muir22bca932017-10-23 10:58:12 -0700314 return false;
315 ctl->info_retrieved = true;
316 }
317
318 if (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED || ctl->ename)
319 return true;
320
321 struct snd_ctl_elem_info tmp;
322 char** enames = calloc(ctl->info->value.enumerated.items, sizeof(char*));
323 if (!enames)
324 return false;
325
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700326 for (i = 0; i < ctl->info->value.enumerated.items; i++) {
John Muir22bca932017-10-23 10:58:12 -0700327 memset(&tmp, 0, sizeof(tmp));
328 tmp.id.numid = ctl->info->id.numid;
329 tmp.value.enumerated.item = i;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700330 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
John Muir22bca932017-10-23 10:58:12 -0700331 goto fail;
332 enames[i] = strdup(tmp.value.enumerated.name);
333 if (!enames[i])
334 goto fail;
335 }
336 ctl->ename = enames;
337 return true;
338
339fail:
340 free(enames);
341 return false;
342}
343
Simon Wilson7e8a6562013-06-28 16:47:16 -0700344const char *mixer_get_name(struct mixer *mixer)
345{
346 return (const char *)mixer->card_info.name;
347}
348
Simon Wilsonedff7082011-06-06 15:33:34 -0700349unsigned int mixer_get_num_ctls(struct mixer *mixer)
350{
351 if (!mixer)
352 return 0;
353
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700354 return mixer->total_ctl_count;
355}
356
357static int mixer_grp_get_count(struct mixer_ctl_group *grp)
358{
359 if (!grp)
360 return 0;
361
362 return grp->count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700363}
364
365struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
366{
John Muir22bca932017-10-23 10:58:12 -0700367 struct mixer_ctl *ctl;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700368 unsigned int hw_ctl_count, virt_ctl_count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700369
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700370 if (!mixer || (id >= mixer->total_ctl_count))
John Muir22bca932017-10-23 10:58:12 -0700371 return NULL;
372
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700373 hw_ctl_count = mixer_grp_get_count(mixer->hw_grp);
374 virt_ctl_count = mixer_grp_get_count(mixer->virt_grp);
375
376 if (id < hw_ctl_count)
377 ctl = mixer->hw_grp->ctl + id;
378 else if ((id - hw_ctl_count) < virt_ctl_count)
379 ctl = mixer->virt_grp->ctl + (id - hw_ctl_count);
380 else
381 return NULL;
382
John Muir22bca932017-10-23 10:58:12 -0700383 if (!mixer_ctl_get_elem_info(ctl))
384 return NULL;
385
386 return ctl;
Simon Wilsonedff7082011-06-06 15:33:34 -0700387}
388
389struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
390{
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700391 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700392 unsigned int n;
Akhil Karuturi42805c32020-01-17 13:53:14 -0800393 int hw_ctl_count;
Simon Wilsonedff7082011-06-06 15:33:34 -0700394
395 if (!mixer)
396 return NULL;
397
Akhil Karuturi42805c32020-01-17 13:53:14 -0800398 hw_ctl_count = mixer_grp_get_count(mixer->hw_grp);
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700399 if (mixer->hw_grp) {
400 grp = mixer->hw_grp;
401
402 for (n = 0; n < grp->count; n++)
403 if (!strcmp(name, (char*) grp->elem_info[n].id.name))
Andrew Chantb5701062018-02-05 15:16:41 -0800404 return mixer_get_ctl(mixer, n);
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700405 }
406
407 if (mixer->virt_grp) {
408 grp = mixer->virt_grp;
409
410 for (n = 0; n < grp->count; n++)
411 if (!strcmp(name, (char*) grp->elem_info[n].id.name))
412 return mixer_get_ctl(mixer, n + hw_ctl_count);
413 }
Simon Wilsonedff7082011-06-06 15:33:34 -0700414
Andrew Chantb5701062018-02-05 15:16:41 -0800415 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700416}
417
Simon Wilson7e8a6562013-06-28 16:47:16 -0700418void mixer_ctl_update(struct mixer_ctl *ctl)
419{
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700420 struct mixer_ctl_group *grp = ctl->grp;
421
422 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info);
Simon Wilson7e8a6562013-06-28 16:47:16 -0700423}
424
Simon Wilsone44e30a2012-03-08 10:45:31 -0800425const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
Simon Wilsonedff7082011-06-06 15:33:34 -0700426{
Simon Wilsone44e30a2012-03-08 10:45:31 -0800427 if (!ctl)
428 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700429
Simon Wilsone44e30a2012-03-08 10:45:31 -0800430 return (const char *)ctl->info->id.name;
Simon Wilsonedff7082011-06-06 15:33:34 -0700431}
432
433enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
434{
435 if (!ctl)
436 return MIXER_CTL_TYPE_UNKNOWN;
437
438 switch (ctl->info->type) {
439 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
440 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
441 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
442 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
443 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
444 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
445 default: return MIXER_CTL_TYPE_UNKNOWN;
446 };
447}
448
449const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
450{
451 if (!ctl)
452 return "";
453
454 switch (ctl->info->type) {
455 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
456 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
457 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
458 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
459 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
460 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
461 default: return "Unknown";
462 };
463}
464
465unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
466{
467 if (!ctl)
468 return 0;
469
470 return ctl->info->count;
471}
472
473static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
474{
475 int range;
476
477 if (percent > 100)
478 percent = 100;
479 else if (percent < 0)
480 percent = 0;
481
482 range = (ei->value.integer.max - ei->value.integer.min);
483
484 return ei->value.integer.min + (range * percent) / 100;
485}
486
487static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
488{
489 int range = (ei->value.integer.max - ei->value.integer.min);
490
491 if (range == 0)
492 return 0;
493
494 return ((value - ei->value.integer.min) / range) * 100;
495}
496
497int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
498{
499 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
500 return -EINVAL;
501
502 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
503}
504
505int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
506{
507 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
508 return -EINVAL;
509
510 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
511}
512
513int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
514{
Akhil Karuturi42805c32020-01-17 13:53:14 -0800515 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700516 struct snd_ctl_elem_value ev;
517 int ret;
518
519 if (!ctl || (id >= ctl->info->count))
520 return -EINVAL;
521
Akhil Karuturi42805c32020-01-17 13:53:14 -0800522 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700523 memset(&ev, 0, sizeof(ev));
524 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700525 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700526 if (ret < 0)
527 return ret;
528
529 switch (ctl->info->type) {
530 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
531 return !!ev.value.integer.value[id];
532
533 case SNDRV_CTL_ELEM_TYPE_INTEGER:
534 return ev.value.integer.value[id];
535
536 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
537 return ev.value.enumerated.item[id];
538
Simon Wilson5aed71d2011-11-16 14:45:38 -0800539 case SNDRV_CTL_ELEM_TYPE_BYTES:
540 return ev.value.bytes.data[id];
541
Simon Wilsonedff7082011-06-06 15:33:34 -0700542 default:
543 return -EINVAL;
544 }
545
546 return 0;
547}
548
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530549int mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl)
550{
551 return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
552}
553
Simon Wilson8813fe82013-06-24 15:40:34 -0700554int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800555{
Akhil Karuturi42805c32020-01-17 13:53:14 -0800556 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800557 struct snd_ctl_elem_value ev;
Mythri P K67b1df42014-08-18 15:39:36 +0530558 int ret = 0;
Simon Wilson8813fe82013-06-24 15:40:34 -0700559 size_t size;
560 void *source;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530561 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800562
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530563 if ((!ctl) || !count || !array)
564 return -EINVAL;
565
Akhil Karuturi42805c32020-01-17 13:53:14 -0800566 grp = ctl->grp;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530567 total_count = ctl->info->count;
568
569 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530570 mixer_ctl_is_access_tlv_rw(ctl)) {
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530571 /* Additional two words is for the TLV header */
572 total_count += TLV_HEADER_SIZE;
573 }
574
575 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800576 return -EINVAL;
577
578 memset(&ev, 0, sizeof(ev));
579 ev.id.numid = ctl->info->id.numid;
580
Simon Wilson8813fe82013-06-24 15:40:34 -0700581 switch (ctl->info->type) {
582 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
583 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700584 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530585 if (ret < 0)
586 return ret;
Simon Wilson8813fe82013-06-24 15:40:34 -0700587 size = sizeof(ev.value.integer.value[0]);
588 source = ev.value.integer.value;
589 break;
590
591 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530592 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530593 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530594 struct snd_ctl_tlv *tlv;
595 int ret;
596
Ben Zhang6cb14f22016-04-22 17:59:40 -0700597 if (count > SIZE_MAX - sizeof(*tlv))
598 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530599 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700600 if (!tlv)
601 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530602 tlv->numid = ctl->info->id.numid;
603 tlv->length = count;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700604 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530605
606 source = tlv->tlv;
607 memcpy(array, source, count);
608
609 free(tlv);
610
611 return ret;
612 } else {
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700613 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K67b1df42014-08-18 15:39:36 +0530614 if (ret < 0)
615 return ret;
616 size = sizeof(ev.value.bytes.data[0]);
617 source = ev.value.bytes.data;
618 break;
619 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700620
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530621 case SNDRV_CTL_ELEM_TYPE_IEC958:
622 size = sizeof(ev.value.iec958);
623 source = &ev.value.iec958;
624 break;
625
Simon Wilson8813fe82013-06-24 15:40:34 -0700626 default:
627 return -EINVAL;
628 }
629
630 memcpy(array, source, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800631
632 return 0;
633}
634
Simon Wilsonedff7082011-06-06 15:33:34 -0700635int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
636{
Akhil Karuturi42805c32020-01-17 13:53:14 -0800637 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700638 struct snd_ctl_elem_value ev;
639 int ret;
640
641 if (!ctl || (id >= ctl->info->count))
642 return -EINVAL;
643
Akhil Karuturi42805c32020-01-17 13:53:14 -0800644 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700645 memset(&ev, 0, sizeof(ev));
646 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700647 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700648 if (ret < 0)
649 return ret;
650
651 switch (ctl->info->type) {
652 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
653 ev.value.integer.value[id] = !!value;
654 break;
655
656 case SNDRV_CTL_ELEM_TYPE_INTEGER:
657 ev.value.integer.value[id] = value;
658 break;
659
660 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
661 ev.value.enumerated.item[id] = value;
662 break;
663
David.Coutheruta8481aa2013-06-11 16:22:57 +0200664 case SNDRV_CTL_ELEM_TYPE_BYTES:
665 ev.value.bytes.data[id] = value;
666 break;
667
Simon Wilsonedff7082011-06-06 15:33:34 -0700668 default:
669 return -EINVAL;
670 }
671
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700672 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700673}
674
Simon Wilson8813fe82013-06-24 15:40:34 -0700675int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800676{
Akhil Karuturi42805c32020-01-17 13:53:14 -0800677 struct mixer_ctl_group *grp;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800678 struct snd_ctl_elem_value ev;
Simon Wilson8813fe82013-06-24 15:40:34 -0700679 size_t size;
680 void *dest;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530681 size_t total_count;
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800682
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530683 if ((!ctl) || !count || !array)
684 return -EINVAL;
685
Akhil Karuturi42805c32020-01-17 13:53:14 -0800686 grp = ctl->grp;
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530687 total_count = ctl->info->count;
688
689 if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530690 mixer_ctl_is_access_tlv_rw(ctl)) {
Pawse, GuruprasadXb4e8d0d2016-02-02 17:52:29 +0530691 /* Additional two words is for the TLV header */
692 total_count += TLV_HEADER_SIZE;
693 }
694
695 if (count > total_count)
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800696 return -EINVAL;
697
698 memset(&ev, 0, sizeof(ev));
699 ev.id.numid = ctl->info->id.numid;
700
Simon Wilson8813fe82013-06-24 15:40:34 -0700701 switch (ctl->info->type) {
702 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
703 case SNDRV_CTL_ELEM_TYPE_INTEGER:
704 size = sizeof(ev.value.integer.value[0]);
705 dest = ev.value.integer.value;
706 break;
707
708 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K67b1df42014-08-18 15:39:36 +0530709 /* check if this is new bytes TLV */
Pankaj Bharadiya10d4b8f2017-01-04 11:10:58 +0530710 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K67b1df42014-08-18 15:39:36 +0530711 struct snd_ctl_tlv *tlv;
712 int ret = 0;
Ben Zhang6cb14f22016-04-22 17:59:40 -0700713 if (count > SIZE_MAX - sizeof(*tlv))
714 return -EINVAL;
Mythri P K67b1df42014-08-18 15:39:36 +0530715 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang6cb14f22016-04-22 17:59:40 -0700716 if (!tlv)
717 return -ENOMEM;
Mythri P K67b1df42014-08-18 15:39:36 +0530718 tlv->numid = ctl->info->id.numid;
719 tlv->length = count;
720 memcpy(tlv->tlv, array, count);
721
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700722 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K67b1df42014-08-18 15:39:36 +0530723 free(tlv);
724
725 return ret;
726 } else {
727 size = sizeof(ev.value.bytes.data[0]);
728 dest = ev.value.bytes.data;
729 }
Simon Wilson8813fe82013-06-24 15:40:34 -0700730 break;
731
Yogesh Agrawal49a61372013-08-02 20:04:58 +0530732 case SNDRV_CTL_ELEM_TYPE_IEC958:
733 size = sizeof(ev.value.iec958);
734 dest = &ev.value.iec958;
735 break;
736
Simon Wilson8813fe82013-06-24 15:40:34 -0700737 default:
738 return -EINVAL;
739 }
740
741 memcpy(dest, array, size * count);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800742
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700743 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonda39e0b2012-11-09 15:16:47 -0800744}
745
Simon Wilsonedff7082011-06-06 15:33:34 -0700746int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
747{
Simon Wilsonedff7082011-06-06 15:33:34 -0700748 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
749 return -EINVAL;
750
Simon Wilsonedff7082011-06-06 15:33:34 -0700751 return ctl->info->value.integer.min;
752}
753
754int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
755{
Simon Wilsonedff7082011-06-06 15:33:34 -0700756 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
757 return -EINVAL;
758
Simon Wilsonedff7082011-06-06 15:33:34 -0700759 return ctl->info->value.integer.max;
760}
761
762unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
763{
764 if (!ctl)
765 return 0;
766
767 return ctl->info->value.enumerated.items;
768}
769
Simon Wilsone44e30a2012-03-08 10:45:31 -0800770const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
771 unsigned int enum_id)
Simon Wilsonedff7082011-06-06 15:33:34 -0700772{
Simon Wilsonedff7082011-06-06 15:33:34 -0700773 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
774 (enum_id >= ctl->info->value.enumerated.items))
Simon Wilsone44e30a2012-03-08 10:45:31 -0800775 return NULL;
Simon Wilsonedff7082011-06-06 15:33:34 -0700776
Simon Wilsone44e30a2012-03-08 10:45:31 -0800777 return (const char *)ctl->ename[enum_id];
Simon Wilsonedff7082011-06-06 15:33:34 -0700778}
779
780int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
781{
Akhil Karuturi42805c32020-01-17 13:53:14 -0800782 struct mixer_ctl_group *grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700783 unsigned int i, num_enums;
784 struct snd_ctl_elem_value ev;
785 int ret;
786
787 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
788 return -EINVAL;
789
Akhil Karuturi42805c32020-01-17 13:53:14 -0800790 grp = ctl->grp;
Simon Wilsonedff7082011-06-06 15:33:34 -0700791 num_enums = ctl->info->value.enumerated.items;
792 for (i = 0; i < num_enums; i++) {
793 if (!strcmp(string, ctl->ename[i])) {
794 memset(&ev, 0, sizeof(ev));
795 ev.value.enumerated.item[0] = i;
796 ev.id.numid = ctl->info->id.numid;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700797 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilsonedff7082011-06-06 15:33:34 -0700798 if (ret < 0)
799 return ret;
800 return 0;
801 }
802 }
803
804 return -EINVAL;
805}
806
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +0530807/** Subscribes for the mixer events.
808 * @param mixer A mixer handle.
809 * @param subscribe value indicating subscribe or unsubscribe for events
810 * @returns On success, zero.
811 * On failure, non-zero.
812 * @ingroup libtinyalsa-mixer
813 */
814int mixer_subscribe_events(struct mixer *mixer, int subscribe)
815{
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700816 struct mixer_ctl_group *grp;
817
818 if (mixer->hw_grp) {
819 grp = mixer->hw_grp;
820 if (!subscribe)
821 grp->event_cnt = 0;
822
823 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS,
824 &subscribe) < 0)
825 return -1;
826 }
827
828 if (mixer->virt_grp) {
829 grp = mixer->virt_grp;
830 if (!subscribe)
831 grp->event_cnt = 0;
832
833 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS,
834 &subscribe) < 0)
835 return -1;
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +0530836 }
837 return 0;
838}
839
840/** Wait for mixer events.
841 * @param mixer A mixer handle.
842 * @param timeout timout value
843 * @returns On success, 1.
844 * On failure, -errno.
845 * On timeout, 0
846 * @ingroup libtinyalsa-mixer
847 */
848int mixer_wait_event(struct mixer *mixer, int timeout)
849{
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700850 struct pollfd *pfd;
851 struct mixer_ctl_group *grp;
852 int count = 0, num_fds = 0, i;
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +0530853
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700854 if (mixer->fd >= 0)
855 num_fds++;
856
857 if (mixer->virt_grp)
858 num_fds++;
859
860 pfd = (struct pollfd *)calloc(sizeof(struct pollfd), num_fds);
861 if (!pfd)
862 return -ENOMEM;
863
864 /* TODO wait events for virt fd */
865 if (mixer->fd >= 0) {
866 pfd[count].fd = mixer->fd;
867 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
868 count++;
869 }
870
871 if (mixer->virt_grp) {
872 grp = mixer->virt_grp;
873 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
874 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
875 count++;
876 }
877 }
878
879 if (!count)
880 return 0;
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +0530881
882 for (;;) {
883 int err;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700884 err = poll(pfd, count, timeout);
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +0530885 if (err < 0)
886 return -errno;
887 if (!err)
888 return 0;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700889 for (i = 0; i < count; i++) {
890 if (pfd[i].revents & (POLLERR | POLLNVAL))
891 return -EIO;
892 if (pfd[i].revents & (POLLIN | POLLOUT)) {
893 if ((i == 0) && mixer->fd >= 0) {
894 grp = mixer->hw_grp;
895 grp->event_cnt++;
896 } else {
897 grp = mixer->virt_grp;
898 grp->event_cnt++;
899 }
900 return 1;
901 }
902 }
Pankaj Bharadiya415c0c32017-01-11 08:57:06 +0530903 }
904}
Andrew Chantb5701062018-02-05 15:16:41 -0800905
906/** Consume a mixer event.
907 * If mixer_subscribe_events has been called,
908 * mixer_wait_event will identify when a control value has changed.
909 * This function will clear a single event from the mixer so that
910 * further events can be alerted.
911 *
912 * @param mixer A mixer handle.
913 * @returns 0 on success. -errno on failure.
914 * @ingroup libtinyalsa-mixer
915 */
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700916int mixer_consume_event(struct mixer *mixer)
917{
Andrew Chantb5701062018-02-05 15:16:41 -0800918 struct snd_ctl_event ev;
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700919 struct mixer_ctl_group *grp;
920 ssize_t count = 0;
921
Andrew Chantb5701062018-02-05 15:16:41 -0800922 // Exporting the actual event would require exposing snd_ctl_event
923 // via the header file, and all associated structs.
924 // The events generally tell you exactly which value changed,
925 // but reading values you're interested isn't hard and simplifies
926 // the interface greatly.
Bhalchandra Gajare4268f1b2019-08-21 15:09:43 -0700927 if (mixer->hw_grp) {
928 grp = mixer->hw_grp;
929 if (grp->event_cnt) {
930 grp->event_cnt--;
931 count = grp->ops->read_event(grp->data, &ev, sizeof(ev));
932 return (count >= 0) ? 0 : -errno;
933 }
934 }
935
936 if (mixer->virt_grp) {
937 grp = mixer->virt_grp;
938 if (grp->event_cnt) {
939 grp->event_cnt--;
940 count += grp->ops->read_event(grp->data, &ev, sizeof(ev));
941 return (count >= 0) ? 0 : -errno;
942 }
943 }
944 return (count >= 0) ? 0 : -errno;
945}
946
947/** Read a mixer event.
948 * If mixer_subscribe_events has been called,
949 * mixer_wait_event will identify when a control value has changed.
950 * This function will read and clear a single event from the mixer
951 * so that further events can be alerted.
952 *
953 * @param mixer A mixer handle.
954 * @param ev snd_ctl_event pointer where event needs to be read
955 * @returns 0 on success. -errno on failure.
956 * @ingroup libtinyalsa-mixer
957 */
958int mixer_read_event(struct mixer *mixer, struct snd_ctl_event *ev)
959{
960 struct mixer_ctl_group *grp;
961 ssize_t count = 0;
962
963 if (mixer->hw_grp) {
964 grp = mixer->hw_grp;
965 if (grp->event_cnt) {
966 grp->event_cnt--;
967 count = grp->ops->read_event(grp->data, ev, sizeof(*ev));
968 return (count >= 0) ? 0 : -errno;
969 }
970 }
971
972 if (mixer->virt_grp) {
973 grp = mixer->virt_grp;
974 if (grp->event_cnt) {
975 grp->event_cnt--;
976 count = grp->ops->read_event(grp->data, ev, sizeof(*ev));
977 return (count >= 0) ? 0 : -errno;
978 }
979 }
Andrew Chantb5701062018-02-05 15:16:41 -0800980 return (count >= 0) ? 0 : -errno;
981}