blob: 0c2e233700a92a6eea35b5158b308166333af4ce [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;
Simon Wilson98c1f162011-06-07 16:12:32 -0700172 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700173 }
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 Wilson98c1f162011-06-07 16:12:32 -0700180 if (mixer && (id < mixer->count))
Simon Wilson79d39652011-05-25 13:44:23 -0700181 return mixer->ctl + id;
182
Simon Wilson98c1f162011-06-07 16:12:32 -0700183 errno = EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700184 return NULL;
185}
186
187struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
188{
189 unsigned int n;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700190
Simon Wilson98c1f162011-06-07 16:12:32 -0700191 if (!mixer)
192 goto error;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700193
Simon Wilson79d39652011-05-25 13:44:23 -0700194 for (n = 0; n < mixer->count; n++)
195 if (!strcmp(name, (char*) mixer->info[n].id.name))
196 return mixer->ctl + n;
197
Simon Wilson98c1f162011-06-07 16:12:32 -0700198error:
199 errno = EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700200 return NULL;
201}
202
Simon Wilsond2cb5032011-06-04 00:57:17 -0700203int mixer_ctl_get_name(struct mixer_ctl *ctl, char *name, unsigned int size)
204{
205 if (!ctl || !name || (size == 0)) {
206 errno = EINVAL;
207 return -1;
208 }
209
210 strncpy(name, (char *)ctl->info->id.name, size);
211 return 0;
212}
213
214enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
215{
216 if (!ctl) {
217 errno = EINVAL;
218 return MIXER_CTL_TYPE_UNKNOWN;
219 }
220
221 switch (ctl->info->type) {
222 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
223 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
224 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
225 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
226 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
227 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
228 default: return MIXER_CTL_TYPE_UNKNOWN;
229 };
230}
231
232const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
233{
234 if (!ctl) {
235 errno = EINVAL;
236 return "";
237 }
238
239 switch (ctl->info->type) {
240 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
241 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
242 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
243 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
244 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
245 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
246 default: return "Unknown";
247 };
248}
249
250unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
251{
252 if (!ctl) {
253 errno = EINVAL;
254 return 0;
255 }
256
257 return ctl->info->count;
258}
259
Simon Wilson79d39652011-05-25 13:44:23 -0700260static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
261{
262 int range;
Simon Wilson98c1f162011-06-07 16:12:32 -0700263
Simon Wilson79d39652011-05-25 13:44:23 -0700264 if (percent > 100)
265 percent = 100;
266 else if (percent < 0)
267 percent = 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700268
Simon Wilson79d39652011-05-25 13:44:23 -0700269 range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700270
Simon Wilson79d39652011-05-25 13:44:23 -0700271 return ei->value.integer.min + (range * percent) / 100;
272}
273
274static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
275{
276 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700277
Simon Wilson79d39652011-05-25 13:44:23 -0700278 if (range == 0)
279 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700280
Simon Wilson79d39652011-05-25 13:44:23 -0700281 return ((value - ei->value.integer.min) / range) * 100;
282}
283
Simon Wilsond2cb5032011-06-04 00:57:17 -0700284int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700285{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700286 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700287 errno = EINVAL;
288 return -1;
289 }
290
Simon Wilson066c9f62011-06-05 18:23:05 -0700291 return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700292}
293
Simon Wilsond2cb5032011-06-04 00:57:17 -0700294int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700295{
Simon Wilsond2cb5032011-06-04 00:57:17 -0700296 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700297 errno = EINVAL;
298 return -1;
299 }
300
Simon Wilson066c9f62011-06-05 18:23:05 -0700301 return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700302}
303
Simon Wilson066c9f62011-06-05 18:23:05 -0700304int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700305{
Simon Wilson79d39652011-05-25 13:44:23 -0700306 struct snd_ctl_elem_value ev;
307
Simon Wilsond2cb5032011-06-04 00:57:17 -0700308 if (!ctl || (id >= ctl->info->count)) {
309 errno = EINVAL;
310 return -1;
311 }
312
Simon Wilson79d39652011-05-25 13:44:23 -0700313 memset(&ev, 0, sizeof(ev));
314 ev.id.numid = ctl->info->id.numid;
315 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
316 return -1;
317
318 switch (ctl->info->type) {
319 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700320 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700321
322 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700323 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700324
325 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
326 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700327
Simon Wilson79d39652011-05-25 13:44:23 -0700328 default:
329 errno = EINVAL;
330 return -1;
331 }
332
333 return 0;
334}
335
Simon Wilson066c9f62011-06-05 18:23:05 -0700336int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700337{
338 struct snd_ctl_elem_value ev;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700339
340 if (!ctl || (id >= ctl->info->count)) {
341 errno = EINVAL;
342 return -1;
343 }
Simon Wilson79d39652011-05-25 13:44:23 -0700344
345 memset(&ev, 0, sizeof(ev));
346 ev.id.numid = ctl->info->id.numid;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700347 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
348 return -1;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700349
Simon Wilson79d39652011-05-25 13:44:23 -0700350 switch (ctl->info->type) {
351 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700352 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -0700353 break;
354
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700355 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700356 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -0700357 break;
Simon Wilson79d39652011-05-25 13:44:23 -0700358
Simon Wilson066c9f62011-06-05 18:23:05 -0700359 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
360 ev.value.enumerated.item[id] = value;
361 break;
362
Simon Wilson79d39652011-05-25 13:44:23 -0700363 default:
364 errno = EINVAL;
365 return -1;
366 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700367
Simon Wilson79d39652011-05-25 13:44:23 -0700368 return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
369}
370
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -0700371int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
372{
373 struct snd_ctl_elem_value ev;
374
375 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
376 errno = EINVAL;
377 return -1;
378 }
379
380 memset(&ev, 0, sizeof(ev));
381 ev.id.numid = ctl->info->id.numid;
382 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
383 return -1;
384
385 return ctl->info->value.integer.min;
386}
387
388int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
389{
390 struct snd_ctl_elem_value ev;
391
392 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) {
393 errno = EINVAL;
394 return -1;
395 }
396
397 memset(&ev, 0, sizeof(ev));
398 ev.id.numid = ctl->info->id.numid;
399 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
400 return -1;
401
402 return ctl->info->value.integer.max;
403}
404
Simon Wilson066c9f62011-06-05 18:23:05 -0700405unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
406{
407 if (!ctl) {
408 errno = EINVAL;
409 return 0;
410 }
411
412 return ctl->info->value.enumerated.items;
413}
414
415int mixer_ctl_get_enum_string(struct mixer_ctl *ctl, unsigned int enum_id,
416 char *string, unsigned int size)
Simon Wilson79d39652011-05-25 13:44:23 -0700417{
418 struct snd_ctl_elem_value ev;
419
Simon Wilsond2cb5032011-06-04 00:57:17 -0700420 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Simon Wilson066c9f62011-06-05 18:23:05 -0700421 (enum_id >= ctl->info->value.enumerated.items)) {
Simon Wilson79d39652011-05-25 13:44:23 -0700422 errno = EINVAL;
423 return -1;
424 }
425
426 memset(&ev, 0, sizeof(ev));
Simon Wilson79d39652011-05-25 13:44:23 -0700427 ev.id.numid = ctl->info->id.numid;
Simon Wilson066c9f62011-06-05 18:23:05 -0700428 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev))
Simon Wilson79d39652011-05-25 13:44:23 -0700429 return -1;
Simon Wilson066c9f62011-06-05 18:23:05 -0700430 strncpy(string, (char *)ctl->ename[enum_id], size);
431
Simon Wilson79d39652011-05-25 13:44:23 -0700432 return 0;
433}
434
Simon Wilsonf0a20ee2011-06-05 21:18:52 -0700435int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
436{
437 unsigned int i, num_enums;
438 struct snd_ctl_elem_value ev;
439
440 if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED)) {
441 errno = EINVAL;
442 return -1;
443 }
444
445 num_enums = ctl->info->value.enumerated.items;
446 for (i = 0; i < num_enums; i++) {
447 if (!strcmp(string, ctl->ename[i])) {
448 memset(&ev, 0, sizeof(ev));
449 ev.value.enumerated.item[0] = i;
450 ev.id.numid = ctl->info->id.numid;
451 if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev) < 0)
452 return -1;
453 return 0;
454 }
455 }
456
457 errno = EINVAL;
458 return -1;
459}
460