Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 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> |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 31 | #include <stdint.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <ctype.h> |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 37 | #include <limits.h> |
Dima Krasner | 696c448 | 2016-03-05 19:50:02 +0200 | [diff] [blame] | 38 | #include <time.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 39 | |
Simon Wilson | 6a52f2c | 2012-05-04 16:32:10 -0700 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
| 41 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 42 | #include <linux/ioctl.h> |
| 43 | #define __force |
| 44 | #define __bitwise |
| 45 | #define __user |
| 46 | #include <sound/asound.h> |
| 47 | |
| 48 | #include <tinyalsa/asoundlib.h> |
| 49 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 50 | /** A mixer control. |
| 51 | * @ingroup tinyalsa-mixer |
| 52 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 53 | struct mixer_ctl { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 54 | /** The mixer that the mixer control belongs to */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 55 | struct mixer *mixer; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 56 | struct snd_ctl_elem_info info; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 57 | char **ename; |
| 58 | }; |
| 59 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 60 | /** A mixer handle. |
| 61 | * @ingroup tinyalsa-mixer |
| 62 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 63 | struct mixer { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 64 | /** File descriptor for the card */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 65 | int fd; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 66 | /** Card information */ |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 67 | struct snd_ctl_card_info card_info; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 68 | /** A continuous array of mixer controls */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 69 | struct mixer_ctl *ctl; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 70 | /** The number of mixer controls */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 71 | unsigned int count; |
| 72 | }; |
| 73 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 74 | /** Closes a mixer returned by @ref mixer_open. |
| 75 | * @param mixer A mixer handle. |
| 76 | * @ingroup tinyalsa-mixer |
| 77 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 78 | void mixer_close(struct mixer *mixer) |
| 79 | { |
| 80 | unsigned int n,m; |
| 81 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 82 | if (!mixer) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 83 | return; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 84 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 85 | if (mixer->fd >= 0) |
| 86 | close(mixer->fd); |
| 87 | |
| 88 | if (mixer->ctl) { |
| 89 | for (n = 0; n < mixer->count; n++) { |
| 90 | if (mixer->ctl[n].ename) { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 91 | unsigned int max = mixer->ctl[n].info.value.enumerated.items; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 92 | for (m = 0; m < max; m++) |
| 93 | free(mixer->ctl[n].ename[m]); |
| 94 | free(mixer->ctl[n].ename); |
| 95 | } |
| 96 | } |
| 97 | free(mixer->ctl); |
| 98 | } |
| 99 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 100 | free(mixer); |
| 101 | |
| 102 | /* TODO: verify frees */ |
| 103 | } |
| 104 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 105 | /** Opens a mixer for a given card. |
| 106 | * @param card The card to open the mixer for. |
| 107 | * @ingroup tinyalsa-mixer |
| 108 | */ |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 109 | struct mixer *mixer_open(unsigned int card) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 110 | { |
| 111 | struct snd_ctl_elem_list elist; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 112 | struct snd_ctl_elem_id *eid = NULL; |
| 113 | struct mixer *mixer = NULL; |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 114 | unsigned int n; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 115 | int fd; |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 116 | char fn[256]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 117 | |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 118 | snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card); |
| 119 | fd = open(fn, O_RDWR); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 120 | if (fd < 0) |
| 121 | return 0; |
| 122 | |
| 123 | memset(&elist, 0, sizeof(elist)); |
| 124 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 125 | goto fail; |
| 126 | |
| 127 | mixer = calloc(1, sizeof(*mixer)); |
| 128 | if (!mixer) |
| 129 | goto fail; |
| 130 | |
| 131 | mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 132 | if (!mixer->ctl) |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 133 | goto fail; |
| 134 | |
| 135 | if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 136 | goto fail; |
| 137 | |
| 138 | eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id)); |
| 139 | if (!eid) |
| 140 | goto fail; |
| 141 | |
| 142 | mixer->count = elist.count; |
| 143 | mixer->fd = fd; |
| 144 | elist.space = mixer->count; |
| 145 | elist.pids = eid; |
| 146 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 147 | goto fail; |
| 148 | |
| 149 | for (n = 0; n < mixer->count; n++) { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 150 | struct snd_ctl_elem_info *ei = &mixer->ctl[n].info; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 151 | ei->id.numid = eid[n].numid; |
| 152 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0) |
| 153 | goto fail; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 154 | mixer->ctl[n].mixer = mixer; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | free(eid); |
| 158 | return mixer; |
| 159 | |
| 160 | fail: |
| 161 | /* TODO: verify frees in failure case */ |
| 162 | if (eid) |
| 163 | free(eid); |
| 164 | if (mixer) |
| 165 | mixer_close(mixer); |
| 166 | else if (fd >= 0) |
| 167 | close(fd); |
| 168 | return 0; |
| 169 | } |
| 170 | |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 171 | const char *mixer_get_name(struct mixer *mixer) |
| 172 | { |
| 173 | return (const char *)mixer->card_info.name; |
| 174 | } |
| 175 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 176 | unsigned int mixer_get_num_ctls(struct mixer *mixer) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 177 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 178 | if (!mixer) |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 179 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 180 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 181 | return mixer->count; |
| 182 | } |
| 183 | |
| 184 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 185 | { |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 186 | if (mixer && (id < mixer->count)) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 187 | return mixer->ctl + id; |
| 188 | |
| 189 | return NULL; |
| 190 | } |
| 191 | |
| 192 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 193 | { |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 194 | return mixer_get_ctl_by_name_and_index(mixer, name, 0); |
| 195 | } |
| 196 | |
| 197 | struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer, |
| 198 | const char *name, |
| 199 | unsigned int index) |
| 200 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 201 | unsigned int n; |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 202 | struct mixer_ctl *ctl; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 203 | |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 204 | if (!mixer) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 205 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 206 | |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 207 | ctl = mixer->ctl; |
| 208 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 209 | for (n = 0; n < mixer->count; n++) |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 210 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 211 | if (index-- == 0) |
| 212 | return mixer->ctl + n; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 213 | |
| 214 | return NULL; |
| 215 | } |
| 216 | |
Simon Wilson | 710df88 | 2013-06-28 16:17:50 -0700 | [diff] [blame] | 217 | void mixer_ctl_update(struct mixer_ctl *ctl) |
| 218 | { |
| 219 | ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info); |
| 220 | } |
| 221 | |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 222 | unsigned int mixer_ctl_get_id(struct mixer_ctl *ctl) |
| 223 | { |
| 224 | if (!ctl) |
| 225 | return UINT_MAX; |
| 226 | |
| 227 | /* numid values start at 1, return a 0-base value that |
| 228 | * can be passed to mixer_get_ctl() |
| 229 | */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 230 | return ctl->info.id.numid - 1; |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 233 | const char *mixer_ctl_get_name(struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 234 | { |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 235 | if (!ctl) |
| 236 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 237 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 238 | return (const char *)ctl->info.id.name; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl) |
| 242 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 243 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 244 | return MIXER_CTL_TYPE_UNKNOWN; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 245 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 246 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 247 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 248 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 249 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 250 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 251 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 252 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 253 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 254 | }; |
| 255 | } |
| 256 | |
| 257 | const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl) |
| 258 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 259 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 260 | return ""; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 261 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 262 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 263 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 264 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 265 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 266 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 267 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 268 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 269 | default: return "Unknown"; |
| 270 | }; |
| 271 | } |
| 272 | |
| 273 | unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl) |
| 274 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 275 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 276 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 277 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 278 | return ctl->info.count; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 281 | static int percent_to_int(struct snd_ctl_elem_info *ei, int percent) |
| 282 | { |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 283 | if ((percent > 100) || (percent < 0)) { |
| 284 | return -EINVAL; |
| 285 | } |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 286 | |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 287 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 288 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 289 | return ei->value.integer.min + (range * percent) / 100; |
| 290 | } |
| 291 | |
| 292 | static int int_to_percent(struct snd_ctl_elem_info *ei, int value) |
| 293 | { |
| 294 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 295 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 296 | if (range == 0) |
| 297 | return 0; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 298 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 299 | return ((value - ei->value.integer.min) / range) * 100; |
| 300 | } |
| 301 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 302 | int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 303 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 304 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 305 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 306 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 307 | return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 310 | int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent) |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 311 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 312 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 313 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 314 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 315 | return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 318 | int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 319 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 320 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 321 | int ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 322 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 323 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 324 | return -EINVAL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 325 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 326 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 327 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 328 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 329 | if (ret < 0) |
| 330 | return ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 331 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 332 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 333 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 334 | return !!ev.value.integer.value[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 335 | |
| 336 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 337 | return ev.value.integer.value[id]; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 338 | |
| 339 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 340 | return ev.value.enumerated.item[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 341 | |
Pierre-Louis Bossart | 5251016 | 2011-10-24 15:00:17 -0500 | [diff] [blame] | 342 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 343 | return ev.value.bytes.data[id]; |
| 344 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 345 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 346 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 352 | int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 353 | { |
| 354 | struct snd_ctl_elem_value ev; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 355 | int ret = 0; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 356 | size_t size; |
| 357 | void *source; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 358 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 359 | if (!ctl || (count > ctl->info.count) || !count || !array) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 360 | return -EINVAL; |
| 361 | |
| 362 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 363 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 364 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 365 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 366 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 367 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 368 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 369 | if (ret < 0) |
| 370 | return ret; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 371 | size = sizeof(ev.value.integer.value[0]); |
| 372 | source = ev.value.integer.value; |
| 373 | break; |
| 374 | |
| 375 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 376 | /* check if this is new bytes TLV */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 377 | if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 378 | struct snd_ctl_tlv *tlv; |
| 379 | int ret; |
| 380 | |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 381 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 382 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 383 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 384 | if (!tlv) |
| 385 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 386 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 387 | tlv->length = count; |
| 388 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_READ, tlv); |
| 389 | |
| 390 | source = tlv->tlv; |
| 391 | memcpy(array, source, count); |
| 392 | |
| 393 | free(tlv); |
| 394 | |
| 395 | return ret; |
| 396 | } else { |
| 397 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 398 | if (ret < 0) |
| 399 | return ret; |
| 400 | size = sizeof(ev.value.bytes.data[0]); |
| 401 | source = ev.value.bytes.data; |
| 402 | break; |
| 403 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 404 | |
| 405 | default: |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | memcpy(array, source, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 414 | int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 415 | { |
| 416 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 417 | int ret; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 418 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 419 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 420 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 421 | |
| 422 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 423 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 424 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 425 | if (ret < 0) |
| 426 | return ret; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 427 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 428 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 429 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 430 | ev.value.integer.value[id] = !!value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 431 | break; |
| 432 | |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 433 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Baptiste Robert | 6cd7d5f | 2013-08-07 11:32:45 +0200 | [diff] [blame] | 434 | if ((value < mixer_ctl_get_range_min(ctl)) || |
| 435 | (value > mixer_ctl_get_range_max(ctl))) { |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 439 | ev.value.integer.value[id] = value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 440 | break; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 441 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 442 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 443 | ev.value.enumerated.item[id] = value; |
| 444 | break; |
| 445 | |
David.Coutherut | ce2b634 | 2013-06-11 16:22:57 +0200 | [diff] [blame] | 446 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 447 | ev.value.bytes.data[id] = value; |
| 448 | break; |
| 449 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 450 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 451 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 452 | } |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 453 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 454 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 455 | } |
| 456 | |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 457 | int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 458 | { |
| 459 | struct snd_ctl_elem_value ev; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 460 | size_t size; |
| 461 | void *dest; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 462 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 463 | if (!ctl || (count > ctl->info.count) || !count || !array) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 464 | return -EINVAL; |
| 465 | |
| 466 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 467 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 468 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 469 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 470 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 471 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 472 | size = sizeof(ev.value.integer.value[0]); |
| 473 | dest = ev.value.integer.value; |
| 474 | break; |
| 475 | |
| 476 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 477 | /* check if this is new bytes TLV */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 478 | if (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 479 | struct snd_ctl_tlv *tlv; |
| 480 | int ret = 0; |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 481 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 482 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 483 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 484 | if (!tlv) |
| 485 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 486 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 487 | tlv->length = count; |
| 488 | memcpy(tlv->tlv, array, count); |
| 489 | |
| 490 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv); |
| 491 | free(tlv); |
| 492 | |
| 493 | return ret; |
| 494 | } else { |
| 495 | size = sizeof(ev.value.bytes.data[0]); |
| 496 | dest = ev.value.bytes.data; |
| 497 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 498 | break; |
| 499 | |
| 500 | default: |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
| 504 | memcpy(dest, array, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 505 | |
| 506 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 507 | } |
| 508 | |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 509 | int mixer_ctl_get_range_min(struct mixer_ctl *ctl) |
| 510 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 511 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 512 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 513 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 514 | return ctl->info.value.integer.min; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | int mixer_ctl_get_range_max(struct mixer_ctl *ctl) |
| 518 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 519 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 520 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 521 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 522 | return ctl->info.value.integer.max; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 525 | unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl) |
| 526 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 527 | if (!ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 528 | return 0; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 529 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 530 | return ctl->info.value.enumerated.items; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 531 | } |
| 532 | |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 533 | int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl) |
| 534 | { |
| 535 | struct snd_ctl_elem_info tmp; |
| 536 | unsigned int m; |
| 537 | char **enames; |
| 538 | |
| 539 | if (ctl->ename) { |
| 540 | return 0; |
| 541 | } |
| 542 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 543 | enames = calloc(ctl->info.value.enumerated.items, sizeof(char*)); |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 544 | if (!enames) |
| 545 | goto fail; |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 546 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 547 | memset(&tmp, 0, sizeof(tmp)); |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 548 | tmp.id.numid = ctl->info.id.numid; |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 549 | tmp.value.enumerated.item = m; |
| 550 | if (ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
| 551 | goto fail; |
| 552 | enames[m] = strdup(tmp.value.enumerated.name); |
| 553 | if (!enames[m]) |
| 554 | goto fail; |
| 555 | } |
| 556 | ctl->ename = enames; |
| 557 | return 0; |
| 558 | |
| 559 | fail: |
| 560 | if (enames) { |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 561 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 562 | if (enames[m]) { |
| 563 | free(enames[m]); |
| 564 | } |
| 565 | } |
| 566 | free(enames); |
| 567 | } |
| 568 | return -1; |
| 569 | } |
| 570 | |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 571 | const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, |
| 572 | unsigned int enum_id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 573 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 574 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 575 | (enum_id >= ctl->info.value.enumerated.items) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 576 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 577 | return NULL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 578 | |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 579 | return (const char *)ctl->ename[enum_id]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 582 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 583 | { |
| 584 | unsigned int i, num_enums; |
| 585 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 586 | int ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 587 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame^] | 588 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 589 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 590 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 591 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 592 | num_enums = ctl->info.value.enumerated.items; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 593 | for (i = 0; i < num_enums; i++) { |
| 594 | if (!strcmp(string, ctl->ename[i])) { |
| 595 | memset(&ev, 0, sizeof(ev)); |
| 596 | ev.value.enumerated.item[0] = i; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 597 | ev.id.numid = ctl->info.id.numid; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 598 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 599 | if (ret < 0) |
| 600 | return ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | } |
| 604 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 605 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 606 | } |
| 607 | |