Home | History | Annotate | Download | only in tinyalsa
      1 /* 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 
     45 struct mixer_ctl {
     46     struct mixer *mixer;
     47     struct snd_ctl_elem_info *info;
     48     char **ename;
     49 };
     50 
     51 struct mixer {
     52     int fd;
     53     struct snd_ctl_elem_info *info;
     54     struct mixer_ctl *ctl;
     55     unsigned int count;
     56 };
     57 
     58 void mixer_close(struct mixer *mixer)
     59 {
     60     unsigned int n,m;
     61 
     62     if (!mixer)
     63         return;
     64 
     65     if (mixer->fd >= 0)
     66         close(mixer->fd);
     67 
     68     if (mixer->ctl) {
     69         for (n = 0; n < mixer->count; n++) {
     70             if (mixer->ctl[n].ename) {
     71                 unsigned int max = mixer->ctl[n].info->value.enumerated.items;
     72                 for (m = 0; m < max; m++)
     73                     free(mixer->ctl[n].ename[m]);
     74                 free(mixer->ctl[n].ename);
     75             }
     76         }
     77         free(mixer->ctl);
     78     }
     79 
     80     if (mixer->info)
     81         free(mixer->info);
     82 
     83     free(mixer);
     84 
     85     /* TODO: verify frees */
     86 }
     87 
     88 struct mixer *mixer_open(unsigned int card)
     89 {
     90     struct snd_ctl_elem_list elist;
     91     struct snd_ctl_elem_info tmp;
     92     struct snd_ctl_elem_id *eid = NULL;
     93     struct mixer *mixer = NULL;
     94     unsigned int n, m;
     95     int fd;
     96     char fn[256];
     97 
     98     snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card);
     99     fd = open(fn, O_RDWR);
    100     if (fd < 0)
    101         return 0;
    102 
    103     memset(&elist, 0, sizeof(elist));
    104     if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
    105         goto fail;
    106 
    107     mixer = calloc(1, sizeof(*mixer));
    108     if (!mixer)
    109         goto fail;
    110 
    111     mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl));
    112     mixer->info = calloc(elist.count, sizeof(struct snd_ctl_elem_info));
    113     if (!mixer->ctl || !mixer->info)
    114         goto fail;
    115 
    116     eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id));
    117     if (!eid)
    118         goto fail;
    119 
    120     mixer->count = elist.count;
    121     mixer->fd = fd;
    122     elist.space = mixer->count;
    123     elist.pids = eid;
    124     if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
    125         goto fail;
    126 
    127     for (n = 0; n < mixer->count; n++) {
    128         struct snd_ctl_elem_info *ei = mixer->info + n;
    129         ei->id.numid = eid[n].numid;
    130         if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
    131             goto fail;
    132         mixer->ctl[n].info = ei;
    133         mixer->ctl[n].mixer = mixer;
    134         if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) {
    135             char **enames = calloc(ei->value.enumerated.items, sizeof(char*));
    136             if (!enames)
    137                 goto fail;
    138             mixer->ctl[n].ename = enames;
    139             for (m = 0; m < ei->value.enumerated.items; m++) {
    140                 memset(&tmp, 0, sizeof(tmp));
    141                 tmp.id.numid = ei->id.numid;
    142                 tmp.value.enumerated.item = m;
    143                 if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
    144                     goto fail;
    145                 enames[m] = strdup(tmp.value.enumerated.name);
    146                 if (!enames[m])
    147                     goto fail;
    148             }
    149         }
    150     }
    151 
    152     free(eid);
    153     return mixer;
    154 
    155 fail:
    156     /* TODO: verify frees in failure case */
    157     if (eid)
    158         free(eid);
    159     if (mixer)
    160         mixer_close(mixer);
    161     else if (fd >= 0)
    162         close(fd);
    163     return 0;
    164 }
    165 
    166 unsigned int mixer_get_num_ctls(struct mixer *mixer)
    167 {
    168     if (!mixer)
    169         return 0;
    170 
    171     return mixer->count;
    172 }
    173 
    174 struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
    175 {
    176     if (mixer && (id < mixer->count))
    177         return mixer->ctl + id;
    178 
    179     return NULL;
    180 }
    181 
    182 struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
    183 {
    184     unsigned int n;
    185 
    186     if (!mixer)
    187         return NULL;
    188 
    189     for (n = 0; n < mixer->count; n++)
    190         if (!strcmp(name, (char*) mixer->info[n].id.name))
    191             return mixer->ctl + n;
    192 
    193     return NULL;
    194 }
    195 
    196 const char *mixer_ctl_get_name(struct mixer_ctl *ctl)
    197 {
    198     if (!ctl)
    199         return NULL;
    200 
    201     return (const char *)ctl->info->id.name;
    202 }
    203 
    204 enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl)
    205 {
    206     if (!ctl)
    207         return MIXER_CTL_TYPE_UNKNOWN;
    208 
    209     switch (ctl->info->type) {
    210     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:    return MIXER_CTL_TYPE_BOOL;
    211     case SNDRV_CTL_ELEM_TYPE_INTEGER:    return MIXER_CTL_TYPE_INT;
    212     case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
    213     case SNDRV_CTL_ELEM_TYPE_BYTES:      return MIXER_CTL_TYPE_BYTE;
    214     case SNDRV_CTL_ELEM_TYPE_IEC958:     return MIXER_CTL_TYPE_IEC958;
    215     case SNDRV_CTL_ELEM_TYPE_INTEGER64:  return MIXER_CTL_TYPE_INT64;
    216     default:                             return MIXER_CTL_TYPE_UNKNOWN;
    217     };
    218 }
    219 
    220 const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl)
    221 {
    222     if (!ctl)
    223         return "";
    224 
    225     switch (ctl->info->type) {
    226     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:    return "BOOL";
    227     case SNDRV_CTL_ELEM_TYPE_INTEGER:    return "INT";
    228     case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
    229     case SNDRV_CTL_ELEM_TYPE_BYTES:      return "BYTE";
    230     case SNDRV_CTL_ELEM_TYPE_IEC958:     return "IEC958";
    231     case SNDRV_CTL_ELEM_TYPE_INTEGER64:  return "INT64";
    232     default:                             return "Unknown";
    233     };
    234 }
    235 
    236 unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl)
    237 {
    238     if (!ctl)
    239         return 0;
    240 
    241     return ctl->info->count;
    242 }
    243 
    244 static int percent_to_int(struct snd_ctl_elem_info *ei, int percent)
    245 {
    246     int range;
    247 
    248     if (percent > 100)
    249         percent = 100;
    250     else if (percent < 0)
    251         percent = 0;
    252 
    253     range = (ei->value.integer.max - ei->value.integer.min);
    254 
    255     return ei->value.integer.min + (range * percent) / 100;
    256 }
    257 
    258 static int int_to_percent(struct snd_ctl_elem_info *ei, int value)
    259 {
    260     int range = (ei->value.integer.max - ei->value.integer.min);
    261 
    262     if (range == 0)
    263         return 0;
    264 
    265     return ((value - ei->value.integer.min) / range) * 100;
    266 }
    267 
    268 int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id)
    269 {
    270     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
    271         return -EINVAL;
    272 
    273     return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id));
    274 }
    275 
    276 int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
    277 {
    278     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
    279         return -EINVAL;
    280 
    281     return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent));
    282 }
    283 
    284 int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id)
    285 {
    286     struct snd_ctl_elem_value ev;
    287     int ret;
    288 
    289     if (!ctl || (id >= ctl->info->count))
    290         return -EINVAL;
    291 
    292     memset(&ev, 0, sizeof(ev));
    293     ev.id.numid = ctl->info->id.numid;
    294     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
    295     if (ret < 0)
    296         return ret;
    297 
    298     switch (ctl->info->type) {
    299     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
    300         return !!ev.value.integer.value[id];
    301 
    302     case SNDRV_CTL_ELEM_TYPE_INTEGER:
    303         return ev.value.integer.value[id];
    304 
    305     case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
    306         return ev.value.enumerated.item[id];
    307 
    308     case SNDRV_CTL_ELEM_TYPE_BYTES:
    309         return ev.value.bytes.data[id];
    310 
    311     default:
    312         return -EINVAL;
    313     }
    314 
    315     return 0;
    316 }
    317 
    318 int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
    319 {
    320     struct snd_ctl_elem_value ev;
    321     int ret;
    322 
    323     if (!ctl || (id >= ctl->info->count))
    324         return -EINVAL;
    325 
    326     memset(&ev, 0, sizeof(ev));
    327     ev.id.numid = ctl->info->id.numid;
    328     ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
    329     if (ret < 0)
    330         return ret;
    331 
    332     switch (ctl->info->type) {
    333     case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
    334         ev.value.integer.value[id] = !!value;
    335         break;
    336 
    337     case SNDRV_CTL_ELEM_TYPE_INTEGER:
    338         ev.value.integer.value[id] = value;
    339         break;
    340 
    341     case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
    342         ev.value.enumerated.item[id] = value;
    343         break;
    344 
    345     default:
    346         return -EINVAL;
    347     }
    348 
    349     return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
    350 }
    351 
    352 int mixer_ctl_get_range_min(struct mixer_ctl *ctl)
    353 {
    354     int ret;
    355 
    356     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
    357         return -EINVAL;
    358 
    359     return ctl->info->value.integer.min;
    360 }
    361 
    362 int mixer_ctl_get_range_max(struct mixer_ctl *ctl)
    363 {
    364     int ret;
    365 
    366     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER))
    367         return -EINVAL;
    368 
    369     return ctl->info->value.integer.max;
    370 }
    371 
    372 unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl)
    373 {
    374     if (!ctl)
    375         return 0;
    376 
    377     return ctl->info->value.enumerated.items;
    378 }
    379 
    380 const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
    381                                       unsigned int enum_id)
    382 {
    383     int ret;
    384 
    385     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
    386         (enum_id >= ctl->info->value.enumerated.items))
    387         return NULL;
    388 
    389     return (const char *)ctl->ename[enum_id];
    390 }
    391 
    392 int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
    393 {
    394     unsigned int i, num_enums;
    395     struct snd_ctl_elem_value ev;
    396     int ret;
    397 
    398     if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED))
    399         return -EINVAL;
    400 
    401     num_enums = ctl->info->value.enumerated.items;
    402     for (i = 0; i < num_enums; i++) {
    403         if (!strcmp(string, ctl->ename[i])) {
    404             memset(&ev, 0, sizeof(ev));
    405             ev.value.enumerated.item[0] = i;
    406             ev.id.numid = ctl->info->id.numid;
    407             ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
    408             if (ret < 0)
    409                 return ret;
    410             return 0;
    411         }
    412     }
    413 
    414     return -EINVAL;
    415 }
    416 
    417