Simon Wilson | edff708 | 2011-06-06 15:33:34 -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 | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 31 | #include <stdint.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | #include <unistd.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <errno.h> |
| 36 | #include <ctype.h> |
| 37 | |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 38 | #include <sys/ioctl.h> |
| 39 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 40 | #include <linux/ioctl.h> |
| 41 | #define __force |
| 42 | #define __bitwise |
| 43 | #define __user |
| 44 | #include <sound/asound.h> |
| 45 | |
| 46 | #include <tinyalsa/asoundlib.h> |
| 47 | |
| 48 | struct mixer_ctl { |
| 49 | struct mixer *mixer; |
| 50 | struct snd_ctl_elem_info *info; |
| 51 | char **ename; |
| 52 | }; |
| 53 | |
| 54 | struct mixer { |
| 55 | int fd; |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 56 | struct snd_ctl_card_info card_info; |
| 57 | struct snd_ctl_elem_info *elem_info; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 58 | struct mixer_ctl *ctl; |
| 59 | unsigned int count; |
| 60 | }; |
| 61 | |
| 62 | void mixer_close(struct mixer *mixer) |
| 63 | { |
| 64 | unsigned int n,m; |
| 65 | |
| 66 | if (!mixer) |
| 67 | return; |
| 68 | |
| 69 | if (mixer->fd >= 0) |
| 70 | close(mixer->fd); |
| 71 | |
| 72 | if (mixer->ctl) { |
| 73 | for (n = 0; n < mixer->count; n++) { |
| 74 | if (mixer->ctl[n].ename) { |
| 75 | unsigned int max = mixer->ctl[n].info->value.enumerated.items; |
| 76 | for (m = 0; m < max; m++) |
| 77 | free(mixer->ctl[n].ename[m]); |
| 78 | free(mixer->ctl[n].ename); |
| 79 | } |
| 80 | } |
| 81 | free(mixer->ctl); |
| 82 | } |
| 83 | |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 84 | if (mixer->elem_info) |
| 85 | free(mixer->elem_info); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 86 | |
| 87 | free(mixer); |
| 88 | |
| 89 | /* TODO: verify frees */ |
| 90 | } |
| 91 | |
| 92 | struct mixer *mixer_open(unsigned int card) |
| 93 | { |
| 94 | struct snd_ctl_elem_list elist; |
| 95 | struct snd_ctl_elem_info tmp; |
| 96 | struct snd_ctl_elem_id *eid = NULL; |
| 97 | struct mixer *mixer = NULL; |
| 98 | unsigned int n, m; |
| 99 | int fd; |
| 100 | char fn[256]; |
| 101 | |
| 102 | snprintf(fn, sizeof(fn), "/dev/snd/controlC%u", card); |
| 103 | fd = open(fn, O_RDWR); |
| 104 | if (fd < 0) |
| 105 | return 0; |
| 106 | |
| 107 | memset(&elist, 0, sizeof(elist)); |
| 108 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 109 | goto fail; |
| 110 | |
| 111 | mixer = calloc(1, sizeof(*mixer)); |
| 112 | if (!mixer) |
| 113 | goto fail; |
| 114 | |
| 115 | mixer->ctl = calloc(elist.count, sizeof(struct mixer_ctl)); |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 116 | mixer->elem_info = calloc(elist.count, sizeof(struct snd_ctl_elem_info)); |
| 117 | if (!mixer->ctl || !mixer->elem_info) |
| 118 | goto fail; |
| 119 | |
| 120 | if (ioctl(fd, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info) < 0) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 121 | goto fail; |
| 122 | |
| 123 | eid = calloc(elist.count, sizeof(struct snd_ctl_elem_id)); |
| 124 | if (!eid) |
| 125 | goto fail; |
| 126 | |
| 127 | mixer->count = elist.count; |
| 128 | mixer->fd = fd; |
| 129 | elist.space = mixer->count; |
| 130 | elist.pids = eid; |
| 131 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
| 132 | goto fail; |
| 133 | |
| 134 | for (n = 0; n < mixer->count; n++) { |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 135 | struct snd_ctl_elem_info *ei = mixer->elem_info + n; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 136 | ei->id.numid = eid[n].numid; |
| 137 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0) |
| 138 | goto fail; |
| 139 | mixer->ctl[n].info = ei; |
| 140 | mixer->ctl[n].mixer = mixer; |
| 141 | if (ei->type == SNDRV_CTL_ELEM_TYPE_ENUMERATED) { |
| 142 | char **enames = calloc(ei->value.enumerated.items, sizeof(char*)); |
| 143 | if (!enames) |
| 144 | goto fail; |
| 145 | mixer->ctl[n].ename = enames; |
| 146 | for (m = 0; m < ei->value.enumerated.items; m++) { |
| 147 | memset(&tmp, 0, sizeof(tmp)); |
| 148 | tmp.id.numid = ei->id.numid; |
| 149 | tmp.value.enumerated.item = m; |
| 150 | if (ioctl(fd, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
| 151 | goto fail; |
| 152 | enames[m] = strdup(tmp.value.enumerated.name); |
| 153 | if (!enames[m]) |
| 154 | goto fail; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | free(eid); |
| 160 | return mixer; |
| 161 | |
| 162 | fail: |
| 163 | /* TODO: verify frees in failure case */ |
| 164 | if (eid) |
| 165 | free(eid); |
| 166 | if (mixer) |
| 167 | mixer_close(mixer); |
| 168 | else if (fd >= 0) |
| 169 | close(fd); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 173 | const char *mixer_get_name(struct mixer *mixer) |
| 174 | { |
| 175 | return (const char *)mixer->card_info.name; |
| 176 | } |
| 177 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 178 | unsigned int mixer_get_num_ctls(struct mixer *mixer) |
| 179 | { |
| 180 | if (!mixer) |
| 181 | return 0; |
| 182 | |
| 183 | return mixer->count; |
| 184 | } |
| 185 | |
| 186 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 187 | { |
| 188 | if (mixer && (id < mixer->count)) |
| 189 | return mixer->ctl + id; |
| 190 | |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 195 | { |
| 196 | unsigned int n; |
| 197 | |
| 198 | if (!mixer) |
| 199 | return NULL; |
| 200 | |
| 201 | for (n = 0; n < mixer->count; n++) |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 202 | if (!strcmp(name, (char*) mixer->elem_info[n].id.name)) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 203 | return mixer->ctl + n; |
| 204 | |
| 205 | return NULL; |
| 206 | } |
| 207 | |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 208 | void mixer_ctl_update(struct mixer_ctl *ctl) |
| 209 | { |
| 210 | ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info); |
| 211 | } |
| 212 | |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 213 | const char *mixer_ctl_get_name(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 214 | { |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 215 | if (!ctl) |
| 216 | return NULL; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 217 | |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 218 | return (const char *)ctl->info->id.name; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl) |
| 222 | { |
| 223 | if (!ctl) |
| 224 | return MIXER_CTL_TYPE_UNKNOWN; |
| 225 | |
| 226 | switch (ctl->info->type) { |
| 227 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 228 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 229 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 230 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 231 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 232 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 233 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 234 | }; |
| 235 | } |
| 236 | |
| 237 | const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl) |
| 238 | { |
| 239 | if (!ctl) |
| 240 | return ""; |
| 241 | |
| 242 | switch (ctl->info->type) { |
| 243 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 244 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 245 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 246 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 247 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 248 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 249 | default: return "Unknown"; |
| 250 | }; |
| 251 | } |
| 252 | |
| 253 | unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl) |
| 254 | { |
| 255 | if (!ctl) |
| 256 | return 0; |
| 257 | |
| 258 | return ctl->info->count; |
| 259 | } |
| 260 | |
| 261 | static int percent_to_int(struct snd_ctl_elem_info *ei, int percent) |
| 262 | { |
| 263 | int range; |
| 264 | |
| 265 | if (percent > 100) |
| 266 | percent = 100; |
| 267 | else if (percent < 0) |
| 268 | percent = 0; |
| 269 | |
| 270 | range = (ei->value.integer.max - ei->value.integer.min); |
| 271 | |
| 272 | return ei->value.integer.min + (range * percent) / 100; |
| 273 | } |
| 274 | |
| 275 | static int int_to_percent(struct snd_ctl_elem_info *ei, int value) |
| 276 | { |
| 277 | int range = (ei->value.integer.max - ei->value.integer.min); |
| 278 | |
| 279 | if (range == 0) |
| 280 | return 0; |
| 281 | |
| 282 | return ((value - ei->value.integer.min) / range) * 100; |
| 283 | } |
| 284 | |
| 285 | int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id) |
| 286 | { |
| 287 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id)); |
| 291 | } |
| 292 | |
| 293 | int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent) |
| 294 | { |
| 295 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent)); |
| 299 | } |
| 300 | |
| 301 | int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id) |
| 302 | { |
| 303 | struct snd_ctl_elem_value ev; |
| 304 | int ret; |
| 305 | |
| 306 | if (!ctl || (id >= ctl->info->count)) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | memset(&ev, 0, sizeof(ev)); |
| 310 | ev.id.numid = ctl->info->id.numid; |
| 311 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 312 | if (ret < 0) |
| 313 | return ret; |
| 314 | |
| 315 | switch (ctl->info->type) { |
| 316 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 317 | return !!ev.value.integer.value[id]; |
| 318 | |
| 319 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 320 | return ev.value.integer.value[id]; |
| 321 | |
| 322 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 323 | return ev.value.enumerated.item[id]; |
| 324 | |
Simon Wilson | 5aed71d | 2011-11-16 14:45:38 -0800 | [diff] [blame] | 325 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 326 | return ev.value.bytes.data[id]; |
| 327 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 328 | default: |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame^] | 335 | int mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl) |
| 336 | { |
| 337 | return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE); |
| 338 | } |
| 339 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 340 | int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 341 | { |
| 342 | struct snd_ctl_elem_value ev; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 343 | int ret = 0; |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 344 | size_t size; |
| 345 | void *source; |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 346 | size_t total_count; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 347 | |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 348 | if ((!ctl) || !count || !array) |
| 349 | return -EINVAL; |
| 350 | |
| 351 | total_count = ctl->info->count; |
| 352 | |
| 353 | if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame^] | 354 | mixer_ctl_is_access_tlv_rw(ctl)) { |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 355 | /* Additional two words is for the TLV header */ |
| 356 | total_count += TLV_HEADER_SIZE; |
| 357 | } |
| 358 | |
| 359 | if (count > total_count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 360 | return -EINVAL; |
| 361 | |
| 362 | memset(&ev, 0, sizeof(ev)); |
| 363 | ev.id.numid = ctl->info->id.numid; |
| 364 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 365 | switch (ctl->info->type) { |
| 366 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 367 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Mythri P K | 67b1df4 | 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 | 8813fe8 | 2013-06-24 15:40:34 -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 | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 376 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame^] | 377 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 378 | struct snd_ctl_tlv *tlv; |
| 379 | int ret; |
| 380 | |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 381 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 382 | return -EINVAL; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 383 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 384 | if (!tlv) |
| 385 | return -ENOMEM; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 386 | tlv->numid = ctl->info->id.numid; |
| 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 | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 404 | |
Yogesh Agrawal | 49a6137 | 2013-08-02 20:04:58 +0530 | [diff] [blame] | 405 | case SNDRV_CTL_ELEM_TYPE_IEC958: |
| 406 | size = sizeof(ev.value.iec958); |
| 407 | source = &ev.value.iec958; |
| 408 | break; |
| 409 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 410 | default: |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
| 414 | memcpy(array, source, size * count); |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 419 | int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value) |
| 420 | { |
| 421 | struct snd_ctl_elem_value ev; |
| 422 | int ret; |
| 423 | |
| 424 | if (!ctl || (id >= ctl->info->count)) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | memset(&ev, 0, sizeof(ev)); |
| 428 | ev.id.numid = ctl->info->id.numid; |
| 429 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 430 | if (ret < 0) |
| 431 | return ret; |
| 432 | |
| 433 | switch (ctl->info->type) { |
| 434 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 435 | ev.value.integer.value[id] = !!value; |
| 436 | break; |
| 437 | |
| 438 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 439 | ev.value.integer.value[id] = value; |
| 440 | break; |
| 441 | |
| 442 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 443 | ev.value.enumerated.item[id] = value; |
| 444 | break; |
| 445 | |
David.Coutherut | a8481aa | 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 | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 450 | default: |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | |
| 454 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 455 | } |
| 456 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 457 | int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 458 | { |
| 459 | struct snd_ctl_elem_value ev; |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 460 | size_t size; |
| 461 | void *dest; |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 462 | size_t total_count; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 463 | |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 464 | if ((!ctl) || !count || !array) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | total_count = ctl->info->count; |
| 468 | |
| 469 | if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame^] | 470 | mixer_ctl_is_access_tlv_rw(ctl)) { |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 471 | /* Additional two words is for the TLV header */ |
| 472 | total_count += TLV_HEADER_SIZE; |
| 473 | } |
| 474 | |
| 475 | if (count > total_count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 476 | return -EINVAL; |
| 477 | |
| 478 | memset(&ev, 0, sizeof(ev)); |
| 479 | ev.id.numid = ctl->info->id.numid; |
| 480 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 481 | switch (ctl->info->type) { |
| 482 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 483 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 484 | size = sizeof(ev.value.integer.value[0]); |
| 485 | dest = ev.value.integer.value; |
| 486 | break; |
| 487 | |
| 488 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 489 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame^] | 490 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 491 | struct snd_ctl_tlv *tlv; |
| 492 | int ret = 0; |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 493 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 494 | return -EINVAL; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 495 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 496 | if (!tlv) |
| 497 | return -ENOMEM; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 498 | tlv->numid = ctl->info->id.numid; |
| 499 | tlv->length = count; |
| 500 | memcpy(tlv->tlv, array, count); |
| 501 | |
| 502 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_TLV_WRITE, tlv); |
| 503 | free(tlv); |
| 504 | |
| 505 | return ret; |
| 506 | } else { |
| 507 | size = sizeof(ev.value.bytes.data[0]); |
| 508 | dest = ev.value.bytes.data; |
| 509 | } |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 510 | break; |
| 511 | |
Yogesh Agrawal | 49a6137 | 2013-08-02 20:04:58 +0530 | [diff] [blame] | 512 | case SNDRV_CTL_ELEM_TYPE_IEC958: |
| 513 | size = sizeof(ev.value.iec958); |
| 514 | dest = &ev.value.iec958; |
| 515 | break; |
| 516 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 517 | default: |
| 518 | return -EINVAL; |
| 519 | } |
| 520 | |
| 521 | memcpy(dest, array, size * count); |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 522 | |
| 523 | return ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 524 | } |
| 525 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 526 | int mixer_ctl_get_range_min(struct mixer_ctl *ctl) |
| 527 | { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 528 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
| 529 | return -EINVAL; |
| 530 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 531 | return ctl->info->value.integer.min; |
| 532 | } |
| 533 | |
| 534 | int mixer_ctl_get_range_max(struct mixer_ctl *ctl) |
| 535 | { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 536 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
| 537 | return -EINVAL; |
| 538 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 539 | return ctl->info->value.integer.max; |
| 540 | } |
| 541 | |
| 542 | unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl) |
| 543 | { |
| 544 | if (!ctl) |
| 545 | return 0; |
| 546 | |
| 547 | return ctl->info->value.enumerated.items; |
| 548 | } |
| 549 | |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 550 | const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, |
| 551 | unsigned int enum_id) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 552 | { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 553 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
| 554 | (enum_id >= ctl->info->value.enumerated.items)) |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 555 | return NULL; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 556 | |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 557 | return (const char *)ctl->ename[enum_id]; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 561 | { |
| 562 | unsigned int i, num_enums; |
| 563 | struct snd_ctl_elem_value ev; |
| 564 | int ret; |
| 565 | |
| 566 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED)) |
| 567 | return -EINVAL; |
| 568 | |
| 569 | num_enums = ctl->info->value.enumerated.items; |
| 570 | for (i = 0; i < num_enums; i++) { |
| 571 | if (!strcmp(string, ctl->ename[i])) { |
| 572 | memset(&ev, 0, sizeof(ev)); |
| 573 | ev.value.enumerated.item[0] = i; |
| 574 | ev.id.numid = ctl->info->id.numid; |
| 575 | ret = ioctl(ctl->mixer->fd, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
| 576 | if (ret < 0) |
| 577 | return ret; |
| 578 | return 0; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return -EINVAL; |
| 583 | } |
| 584 | |