blob: 2bb37a12a396b29f70d7aed7c35cefeb914eae2b [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* mixer.c
2**
3** Copyright 2011, The Android Open Source Project
4**
5** Redistribution and use in source and binary forms, with or without
6** modification, are permitted provided that the following conditions are met:
7** * Redistributions of source code must retain the above copyright
8** notice, this list of conditions and the following disclaimer.
9** * Redistributions in binary form must reproduce the above copyright
10** notice, this list of conditions and the following disclaimer in the
11** documentation and/or other materials provided with the distribution.
12** * Neither the name of The Android Open Source Project nor the names of
13** its contributors may be used to endorse or promote products derived
14** from this software without specific prior written permission.
15**
16** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND
17** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE
20** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26** DAMAGE.
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <ctype.h>
36
37#include <linux/ioctl.h>
38#define __force
39#define __bitwise
40#define __user
41#include <sound/asound.h>
42
43#include <tinyalsa/asoundlib.h>
44
Simon Wilson79d39652011-05-25 13:44:23 -070045struct mixer_ctl {
46 struct mixer *mixer;
47 struct snd_ctl_elem_info *info;
48 char **ename;
49};
50
51struct mixer {
52 int fd;
53 struct snd_ctl_elem_info *info;
54 struct mixer_ctl *ctl;
55 unsigned int count;
56};
57
58void mixer_close(struct mixer *mixer)
59{
60 unsigned int n,m;
61
Simon Wilsond2cb5032011-06-04 00:57:17 -070062 if (!mixer) {
63 errno = EINVAL;
64 return;
65 }
66
Simon Wilson79d39652011-05-25 13:44:23 -070067 if (mixer->fd >= 0)
68 close(mixer->fd);
69
70 if (mixer->ctl) {
71 for (n = 0; n < mixer->count; n++) {
72 if (mixer->ctl[n].ename) {
73 unsigned int max = mixer->ctl[n].info->value.enumerated.items;
74 for (m = 0; m < max; m++)
75 free(mixer->ctl[n].ename[m]);
76 free(mixer->ctl[n].ename);
77 }
78 }
79 free(mixer->ctl);
80 }
81
82 if (mixer->info)
83 free(mixer->info);
84
85 free(mixer);
86
87 /* TODO: verify frees */
88}
89
Simon Wilson1bd580f2011-06-02 15:58:41 -070090struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -070091{
92 struct snd_ctl_elem_list elist;
93 struct snd_ctl_elem_info tmp;
94 struct snd_ctl_elem_id *eid = NULL;
95 struct mixer *mixer = NULL;
96 unsigned int n, m;
97 int fd;
Simon Wilson1bd580f2011-06-02 15:58:41 -070098 char fn[256];
Simon Wilson79d39652011-05-25 13:44:23 -070099
Simon Wilson1bd580f2011-06-02 15:58:41 -0700100 snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
101 fd = open(fn, O_RDWR);
Simon Wilson79d39652011-05-25 13:44:23 -0700102 if (fd < 0)
103 return 0;
104
105 memset(&elist, 0, sizeof(elist));
106 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
107 goto fail;
108
109 mixer = calloc(1, sizeof(*mixer));
110 if (!mixer)
111 goto fail;
112
113 mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
114 mixer->info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
115 if (!mixer->ctl || !mixer->info)
116 goto fail;
117
118 eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
119 if (!eid)
120 goto fail;
121
122 mixer->count = elist.count;
123 mixer->fd = fd;
124 elist.space = mixer->count;
125 elist.pids = eid;
126 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
127 goto fail;
128
129 for (n = 0; n < mixer->count; n++) {
130 struct snd_ctl_elem_info *ei = mixer->info + n;
131 ei->id.numid = eid[n].numid;
132 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
133 goto fail;
134 mixer->ctl[n].info = ei;
135 mixer->ctl[n].mixer = mixer;
136 if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
137 char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
138 if (!enames)
139 goto fail;
140 mixer->ctl[n].ename = enames;
141 for (m = 0; m < ei->value.enumerated.items; m++) {
142 memset(&tmp, 0, sizeof(tmp));
143 tmp.id.numid = ei->id.numid;
144 tmp.value.enumerated.item = m;
145 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
146 goto fail;
147 enames[m] = strdup(tmp.value.enumerated.name);
148 if (!enames[m])
149 goto fail;
150 }
151 }
152 }
153
154 free(eid);
155 return mixer;
156
157fail:
158 /* TODO: verify frees in failure case */
159 if (eid)
160 free(eid);
161 if (mixer)
162 mixer_close(mixer);
163 else if (fd >= 0)
164 close(fd);
165 return 0;
166}
167
Simon Wilsond2cb5032011-06-04 00:57:17 -0700168unsigned int mixer_get_num_ctls(struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700169{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700170 if (!mixer) {
171 errno = EINVAL;
172 return -1;
173 }
174
Simon Wilson79d39652011-05-25 13:44:23 -0700175 return mixer->count;
176}
177
178struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
179{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700180 if (!mixer) {
181 errno = EINVAL;
182 return NULL;
183 }
184
Simon Wilson79d39652011-05-25 13:44:23 -0700185 if (id < mixer->count)
186 return mixer->ctl + id;
187
188 return NULL;
189}
190
191struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
192{
193 unsigned int n;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700194
195 if (!mixer) {
196 errno = EINVAL;
197 return NULL;
198 }
199
Simon Wilson79d39652011-05-25 13:44:23 -0700200 for (n = 0; n < mixer->count; n++)
201 if (!strcmp(name, (char*) mixer->info[n].id.name))
202 return mixer->ctl + n;
203
204 return NULL;
205}
206
Simon Wilsond2cb5032011-06-04 00:57:17 -0700207int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size)
208{
209 if (!ctl || !name || (size == 0)) {
210 errno = EINVAL;
211 return -1;
212 }
213
214 strncpy(name, (char *)ctl->info->id.name, size);
215 return 0;
216}
217
218enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
219{
220 if (!ctl) {
221 errno = EINVAL;
222 return MIXER_CTL_TYPE_UNKNOWN;
223 }
224
225 switch (ctl->info->type) {
226 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
227 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
228 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
229 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
230 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
231 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
232 default: return MIXER_CTL_TYPE_UNKNOWN;
233 };
234}
235
236const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
237{
238 if (!ctl) {
239 errno = EINVAL;
240 return "";
241 }
242
243 switch (ctl->info->type) {
244 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
245 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
246 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
247 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
248 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
249 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
250 default: return "Unknown";
251 };
252}
253
254unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
255{
256 if (!ctl) {
257 errno = EINVAL;
258 return 0;
259 }
260
261 return ctl->info->count;
262}
263
Simon Wilson79d39652011-05-25 13:44:23 -0700264static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
265{
266 int range;
267
268 if (percent > 100)
269 percent = 100;
270 else if (percent < 0)
271 percent = 0;
272
273 range = (ei->value.integer.max - ei->value.integer.min);
274
275 return ei->value.integer.min + (range * percent) / 100;
276}
277
278static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
279{
280 int range = (ei->value.integer.max - ei->value.integer.min);
281
282 if (range == 0)
283 return 0;
284
285 return ((value - ei->value.integer.min) / range) * 100;
286}
287
Simon Wilsond2cb5032011-06-04 00:57:17 -0700288int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700289{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700290 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700291 errno = EINVAL;
292 return -1;
293 }
294
Simon Wilson066c9f62011-06-05 18:23:05 -0700295 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700296}
297
Simon Wilsond2cb5032011-06-04 00:57:17 -0700298int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700299{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700300 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700301 errno = EINVAL;
302 return -1;
303 }
304
Simon Wilson066c9f62011-06-05 18:23:05 -0700305 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700306}
307
Simon Wilson066c9f62011-06-05 18:23:05 -0700308int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700309{
Simon Wilson79d39652011-05-25 13:44:23 -0700310 struct snd_ctl_elem_value ev;
311
Simon Wilsond2cb5032011-06-04 00:57:17 -0700312 if (!ctl || (id >= ctl->info->count)) {
313 errno = EINVAL;
314 return -1;
315 }
316
Simon Wilson79d39652011-05-25 13:44:23 -0700317 memset(&ev, 0, sizeof(ev));
318 ev.id.numid = ctl->info->id.numid;
319 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
320 return -1;
321
322 switch (ctl->info->type) {
323 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700324 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700325
326 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700327 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700328
329 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
330 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700331
Simon Wilson79d39652011-05-25 13:44:23 -0700332 default:
333 errno = EINVAL;
334 return -1;
335 }
336
337 return 0;
338}
339
Simon Wilson066c9f62011-06-05 18:23:05 -0700340int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700341{
342 struct snd_ctl_elem_value ev;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700343
344 if (!ctl || (id >= ctl->info->count)) {
345 errno = EINVAL;
346 return -1;
347 }
Simon Wilson79d39652011-05-25 13:44:23 -0700348
349 memset(&ev, 0, sizeof(ev));
350 ev.id.numid = ctl->info->id.numid;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700351 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
352 return -1;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700353
Simon Wilson79d39652011-05-25 13:44:23 -0700354 switch (ctl->info->type) {
355 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700356 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700357 break;
358
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700359 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700360 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700361 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700362
Simon Wilson066c9f62011-06-05 18:23:05 -0700363 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
364 ev.value.enumerated.item[id] = value;
365 break;
366
Simon Wilson79d39652011-05-25 13:44:23 -0700367 default:
368 errno = EINVAL;
369 return -1;
370 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700371
Simon Wilson79d39652011-05-25 13:44:23 -0700372 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
373}
374
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700375int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
376{
377 struct snd_ctl_elem_value ev;
378
379 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
380 errno = EINVAL;
381 return -1;
382 }
383
384 memset(&ev, 0, sizeof(ev));
385 ev.id.numid = ctl->info->id.numid;
386 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
387 return -1;
388
389 return ctl->info->value.integer.min;
390}
391
392int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
393{
394 struct snd_ctl_elem_value ev;
395
396 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
397 errno = EINVAL;
398 return -1;
399 }
400
401 memset(&ev, 0, sizeof(ev));
402 ev.id.numid = ctl->info->id.numid;
403 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
404 return -1;
405
406 return ctl->info->value.integer.max;
407}
408
Simon Wilson066c9f62011-06-05 18:23:05 -0700409unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
410{
411 if (!ctl) {
412 errno = EINVAL;
413 return 0;
414 }
415
416 return ctl->info->value.enumerated.items;
417}
418
419int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id,
420 char *string, unsigned int size)
Simon Wilson79d39652011-05-25 13:44:23 -0700421{
422 struct snd_ctl_elem_value ev;
423
Simon Wilsond2cb5032011-06-04 00:57:17 -0700424 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Simon Wilson066c9f62011-06-05 18:23:05 -0700425 (enum_id >= ctl->info->value.enumerated.items)) {
Simon Wilson79d39652011-05-25 13:44:23 -0700426 errno = EINVAL;
427 return -1;
428 }
429
430 memset(&ev, 0, sizeof(ev));
Simon Wilson79d39652011-05-25 13:44:23 -0700431 ev.id.numid = ctl->info->id.numid;
Simon Wilson066c9f62011-06-05 18:23:05 -0700432 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
Simon Wilson79d39652011-05-25 13:44:23 -0700433 return -1;
Simon Wilson066c9f62011-06-05 18:23:05 -0700434 strncpy(string, (char *)ctl->ename[enum_id], size);
435
Simon Wilson79d39652011-05-25 13:44:23 -0700436 return 0;
437}
438
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700439int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
440{
441 unsigned int i, num_enums;
442 struct snd_ctl_elem_value ev;
443
444 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED)) {
445 errno = EINVAL;
446 return -1;
447 }
448
449 num_enums = ctl->info->value.enumerated.items;
450 for (i = 0; i < num_enums; i++) {
451 if (!strcmp(string, ctl->ename[i])) {
452 memset(&ev, 0, sizeof(ev));
453 ev.value.enumerated.item[0] = i;
454 ev.id.numid = ctl->info->id.numid;
455 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev) < 0)
456 return -1;
457 return 0;
458 }
459 }
460
461 errno = EINVAL;
462 return -1;
463}
464