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> |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 32 | #include <stdbool.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <errno.h> |
| 37 | #include <ctype.h> |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 38 | #include <limits.h> |
Dima Krasner | 696c448 | 2016-03-05 19:50:02 +0200 | [diff] [blame] | 39 | #include <time.h> |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 40 | #include <poll.h> |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 41 | |
Simon Wilson | 6a52f2c | 2012-05-04 16:32:10 -0700 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
| 43 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 44 | #include <linux/ioctl.h> |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 45 | |
| 46 | #ifndef __force |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 47 | #define __force |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 48 | #endif |
| 49 | |
| 50 | #ifndef __bitwise |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 51 | #define __bitwise |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 52 | #endif |
| 53 | |
| 54 | #ifndef __user |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 55 | #define __user |
Taylor Holberton | 4c5a11d | 2018-11-28 14:29:52 -0500 | [diff] [blame] | 56 | #endif |
| 57 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 58 | #include <sound/asound.h> |
| 59 | |
Ricardo Biehl Pasquali | 04952ee | 2016-10-05 20:32:09 -0300 | [diff] [blame] | 60 | #include <tinyalsa/mixer.h> |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 61 | #include <tinyalsa/plugin.h> |
| 62 | |
| 63 | #include "mixer_io.h" |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 64 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 65 | /** A mixer control. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 66 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 67 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 68 | struct mixer_ctl { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 69 | /** The mixer that the mixer control belongs to */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 70 | struct mixer *mixer; |
Taylor Holberton | ad4d7d7 | 2016-11-23 13:34:18 -0800 | [diff] [blame] | 71 | /** Information on the control's value (i.e. type, number of values) */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 72 | struct snd_ctl_elem_info info; |
Taylor Holberton | ad4d7d7 | 2016-11-23 13:34:18 -0800 | [diff] [blame] | 73 | /** A list of string representations of enumerated values (only valid for enumerated controls) */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 74 | char **ename; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 75 | /** Pointer to the group that the control belongs to */ |
| 76 | struct mixer_ctl_group *grp; |
| 77 | }; |
| 78 | |
| 79 | struct mixer_ctl_group { |
| 80 | /** A continuous array of mixer controls */ |
| 81 | struct mixer_ctl *ctl; |
| 82 | /** The number of mixer controls */ |
| 83 | unsigned int count; |
| 84 | /** The number of events associated with this group */ |
| 85 | unsigned int event_cnt; |
| 86 | /** The operations corresponding to this group */ |
| 87 | const struct mixer_ops *ops; |
| 88 | /** Private data for storing group specific data */ |
| 89 | void *data; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 92 | /** A mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 93 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 94 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 95 | struct mixer { |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 96 | /** File descriptor for the card */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 97 | int fd; |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 98 | /** Card information */ |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 99 | struct snd_ctl_card_info card_info; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 100 | /* Hardware (kernel interface) mixer control group */ |
| 101 | struct mixer_ctl_group *h_grp; |
| 102 | /* Virtual (Plugin interface) mixer control group */ |
| 103 | struct mixer_ctl_group *v_grp; |
| 104 | /* Total count of mixer controls from both groups */ |
| 105 | unsigned int total_count; |
| 106 | /* Flag to track if card information is already retrieved */ |
| 107 | bool is_card_info_retrieved; |
| 108 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 111 | static void mixer_cleanup_control(struct mixer_ctl *ctl) |
| 112 | { |
| 113 | unsigned int m; |
| 114 | |
| 115 | if (ctl->ename) { |
| 116 | unsigned int max = ctl->info.value.enumerated.items; |
| 117 | for (m = 0; m < max; m++) |
| 118 | free(ctl->ename[m]); |
| 119 | free(ctl->ename); |
| 120 | } |
| 121 | } |
| 122 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 123 | static void mixer_grp_close(struct mixer *mixer, struct mixer_ctl_group *grp) |
| 124 | { |
| 125 | unsigned int n; |
| 126 | |
| 127 | if (!grp) |
| 128 | return; |
| 129 | |
| 130 | if (grp->ctl) { |
| 131 | for (n = 0; n < grp->count; n++) |
| 132 | mixer_cleanup_control(&grp->ctl[n]); |
| 133 | free(grp->ctl); |
| 134 | } |
| 135 | |
| 136 | mixer->is_card_info_retrieved = false; |
| 137 | } |
| 138 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 139 | /** Closes a mixer returned by @ref mixer_open. |
| 140 | * @param mixer A mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 141 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 142 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 143 | void mixer_close(struct mixer *mixer) |
| 144 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 145 | if (!mixer) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 146 | return; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 147 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 148 | if (mixer->fd >= 0 && mixer->h_grp) |
| 149 | mixer->h_grp->ops->close(mixer->h_grp->data); |
| 150 | mixer_grp_close(mixer, mixer->h_grp); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 151 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 152 | #ifdef TINYALSA_USES_PLUGINS |
| 153 | if (mixer->v_grp) |
| 154 | mixer->v_grp->ops->close(mixer->v_grp->data); |
| 155 | mixer_grp_close(mixer, mixer->v_grp); |
| 156 | #endif |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 157 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 158 | free(mixer); |
| 159 | |
| 160 | /* TODO: verify frees */ |
| 161 | } |
| 162 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 163 | static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size) |
| 164 | { |
| 165 | int8_t *newp; |
| 166 | |
| 167 | newp = realloc(ptr, size * newnum); |
| 168 | if (!newp) |
| 169 | return NULL; |
| 170 | |
| 171 | memset(newp + (curnum * size), 0, (newnum - curnum) * size); |
| 172 | return newp; |
| 173 | } |
| 174 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 175 | static int add_controls(struct mixer *mixer, struct mixer_ctl_group *grp) |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 176 | { |
| 177 | struct snd_ctl_elem_list elist; |
| 178 | struct snd_ctl_elem_id *eid = NULL; |
| 179 | struct mixer_ctl *ctl; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 180 | const unsigned int old_count = grp->count; |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 181 | unsigned int new_count; |
| 182 | unsigned int n; |
| 183 | |
| 184 | memset(&elist, 0, sizeof(elist)); |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 185 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 186 | goto fail; |
| 187 | |
| 188 | if (old_count == elist.count) |
| 189 | return 0; /* no new controls return unchanged */ |
| 190 | |
| 191 | if (old_count > elist.count) |
| 192 | return -1; /* driver has removed controls - this is bad */ |
| 193 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 194 | ctl = mixer_realloc_z(grp->ctl, old_count, elist.count, |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 195 | sizeof(struct mixer_ctl)); |
| 196 | if (!ctl) |
| 197 | goto fail; |
| 198 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 199 | grp->ctl = ctl; |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 200 | |
| 201 | /* ALSA drivers are not supposed to remove or re-order controls that |
| 202 | * have already been created so we know that any new controls must |
| 203 | * be after the ones we have already collected |
| 204 | */ |
| 205 | new_count = elist.count; |
| 206 | elist.space = new_count - old_count; /* controls we haven't seen before */ |
| 207 | elist.offset = old_count; /* first control we haven't seen */ |
| 208 | |
| 209 | eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id)); |
| 210 | if (!eid) |
| 211 | goto fail; |
| 212 | |
| 213 | elist.pids = eid; |
| 214 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 215 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0) |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 216 | goto fail; |
| 217 | |
| 218 | for (n = old_count; n < new_count; n++) { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 219 | struct snd_ctl_elem_info *ei = &grp->ctl[n].info; |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 220 | ei->id.numid = eid[n - old_count].numid; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 221 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0) |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 222 | goto fail_extend; |
| 223 | ctl[n].mixer = mixer; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 224 | ctl[n].grp = grp; |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 227 | grp->count = new_count; |
| 228 | mixer->total_count += (new_count - old_count); |
| 229 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 230 | free(eid); |
| 231 | return 0; |
| 232 | |
| 233 | fail_extend: |
| 234 | /* cleanup the control we failed on but leave the ones that were already |
| 235 | * added. Also no advantage to shrinking the resized memory block, we |
| 236 | * might want to extend the controls again later |
| 237 | */ |
| 238 | mixer_cleanup_control(&ctl[n]); |
| 239 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 240 | grp->count = n; /* keep controls we successfully added */ |
| 241 | mixer->total_count += (n - old_count); |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 242 | /* fall through... */ |
| 243 | fail: |
| 244 | free(eid); |
| 245 | return -1; |
| 246 | } |
| 247 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 248 | static int mixer_grp_open(struct mixer *mixer, unsigned int card, bool is_hw) |
| 249 | { |
| 250 | struct mixer_ctl_group *grp = NULL; |
| 251 | const struct mixer_ops *ops = NULL; |
| 252 | void *data = NULL; |
| 253 | int fd, ret; |
| 254 | |
| 255 | grp = calloc(1, sizeof(*grp)); |
| 256 | if (!grp) |
| 257 | return -ENOMEM; |
| 258 | |
| 259 | if (is_hw) { |
| 260 | mixer->fd = -1; |
| 261 | fd = mixer_hw_open(card, &data, &ops); |
| 262 | if (fd < 0) { |
| 263 | ret = fd; |
| 264 | goto err_open; |
| 265 | } |
| 266 | mixer->fd = fd; |
| 267 | mixer->h_grp = grp; |
| 268 | } |
| 269 | #ifdef TINYALSA_USES_PLUGINS |
| 270 | else { |
| 271 | ret = mixer_plugin_open(card, &data, &ops); |
| 272 | if (ret < 0) |
| 273 | goto err_open; |
| 274 | mixer->v_grp = grp; |
| 275 | } |
| 276 | #endif |
| 277 | grp->ops = ops; |
| 278 | grp->data = data; |
| 279 | |
| 280 | if (!mixer->is_card_info_retrieved) { |
| 281 | ret = grp->ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, |
| 282 | &mixer->card_info); |
| 283 | if (ret < 0) |
| 284 | goto err_card_info; |
| 285 | mixer->is_card_info_retrieved = true; |
| 286 | } |
| 287 | |
| 288 | ret = add_controls(mixer, grp); |
| 289 | if (ret < 0) |
| 290 | goto err_card_info; |
| 291 | |
| 292 | return 0; |
| 293 | |
| 294 | err_card_info: |
| 295 | grp->ops->close(grp->data); |
| 296 | |
| 297 | err_open: |
| 298 | free(grp); |
| 299 | return ret; |
| 300 | |
| 301 | } |
| 302 | |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 303 | /** Opens a mixer for a given card. |
| 304 | * @param card The card to open the mixer for. |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 305 | * @returns An initialized mixer handle. |
Taylor Holberton | 8ae5d11 | 2016-11-19 10:35:25 -0800 | [diff] [blame] | 306 | * @ingroup libtinyalsa-mixer |
Taylor Holberton | 7c8b20a | 2016-10-01 19:25:19 -0400 | [diff] [blame] | 307 | */ |
Simon Wilson | 1bd580f | 2011-06-02 15:58:41 -0700 | [diff] [blame] | 308 | struct mixer *mixer_open(unsigned int card) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 309 | { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 310 | struct mixer *mixer = NULL; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 311 | int h_status, v_status = -1; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 312 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 313 | mixer = calloc(1, sizeof(*mixer)); |
| 314 | if (!mixer) |
| 315 | goto fail; |
| 316 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 317 | h_status = mixer_grp_open(mixer, card, true); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 318 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 319 | #ifdef TINYALSA_USES_PLUGINS |
| 320 | v_status = mixer_grp_open(mixer, card, false); |
| 321 | #endif |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 322 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 323 | /* both hw and virtual should fail for mixer_open to fail */ |
| 324 | if (h_status < 0 && v_status < 0) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 325 | goto fail; |
| 326 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 327 | return mixer; |
| 328 | |
| 329 | fail: |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 330 | if (mixer) |
| 331 | mixer_close(mixer); |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 332 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | /** Some controls may not be present at boot time, e.g. controls from runtime |
| 337 | * loadable DSP firmware. This function adds any new controls that have appeared |
| 338 | * since mixer_open() or the last call to this function. This assumes a well- |
| 339 | * behaved codec driver that does not delete controls that already exists, so |
| 340 | * any added controls must be after the last one we already saw. Scanning only |
| 341 | * the new controls is much faster than calling mixer_close() then mixer_open() |
| 342 | * to re-scan all controls. |
| 343 | * |
| 344 | * NOTE: this invalidates any struct mixer_ctl pointers previously obtained |
| 345 | * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your |
| 346 | * stored pointers after calling mixer_update_ctls(), or (better) do not |
| 347 | * store struct mixer_ctl pointers, instead lookup the control by name or |
| 348 | * id only when you are about to use it. The overhead of lookup by id |
| 349 | * using mixer_get_ctl() is negligible. |
| 350 | * @param mixer An initialized mixer handle. |
| 351 | * @returns 0 on success, -1 on failure |
| 352 | */ |
| 353 | int mixer_add_new_ctls(struct mixer *mixer) |
| 354 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 355 | int rc1 = 0, rc2 = 0; |
| 356 | |
Richard Fitzgerald | fd32903 | 2014-09-09 17:14:09 +0100 | [diff] [blame] | 357 | if (!mixer) |
| 358 | return 0; |
| 359 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 360 | /* add the h_grp controls */ |
| 361 | if (mixer->h_grp) |
| 362 | rc1 = add_controls(mixer, mixer->h_grp); |
| 363 | |
| 364 | #ifdef TINYALSA_USES_PLUGINS |
| 365 | /* add the v_grp controls */ |
| 366 | if (mixer->v_grp) |
| 367 | rc2 = add_controls(mixer, mixer->v_grp); |
| 368 | #endif |
| 369 | |
| 370 | if (rc1 < 0) |
| 371 | return rc1; |
| 372 | if (rc2 < 0) |
| 373 | return rc2; |
| 374 | |
| 375 | return 0; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 378 | /** Gets the name of the mixer's card. |
| 379 | * @param mixer An initialized mixer handle. |
| 380 | * @returns The name of the mixer's card. |
| 381 | * @ingroup libtinyalsa-mixer |
| 382 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 383 | const char *mixer_get_name(const struct mixer *mixer) |
Simon Wilson | ec28139 | 2013-06-28 16:21:31 -0700 | [diff] [blame] | 384 | { |
| 385 | return (const char *)mixer->card_info.name; |
| 386 | } |
| 387 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 388 | /** Gets the number of mixer controls for a given mixer. |
| 389 | * @param mixer An initialized mixer handle. |
| 390 | * @returns The number of mixer controls for the given mixer. |
| 391 | * @ingroup libtinyalsa-mixer |
| 392 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 393 | unsigned int mixer_get_num_ctls(const struct mixer *mixer) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 394 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 395 | if (!mixer) |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 396 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 397 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 398 | return mixer->total_count; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Taylor Holberton | 94c7c83 | 2016-12-01 18:35:24 -0800 | [diff] [blame] | 401 | /** Gets the number of mixer controls, that go by a specified name, for a given mixer. |
| 402 | * @param mixer An initialized mixer handle. |
| 403 | * @param name The name of the mixer control |
| 404 | * @returns The number of mixer controls, specified by @p name, for the given mixer. |
| 405 | * @ingroup libtinyalsa-mixer |
| 406 | */ |
| 407 | unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name) |
| 408 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 409 | struct mixer_ctl_group *grp; |
Taylor Holberton | 94c7c83 | 2016-12-01 18:35:24 -0800 | [diff] [blame] | 410 | unsigned int n; |
| 411 | unsigned int count = 0; |
| 412 | struct mixer_ctl *ctl; |
| 413 | |
| 414 | if (!mixer) |
| 415 | return 0; |
| 416 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 417 | if (mixer->h_grp) { |
| 418 | grp = mixer->h_grp; |
| 419 | ctl = grp->ctl; |
Taylor Holberton | 94c7c83 | 2016-12-01 18:35:24 -0800 | [diff] [blame] | 420 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 421 | for (n = 0; n < grp->count; n++) |
| 422 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
| 423 | count++; |
| 424 | } |
| 425 | #ifdef TINYALSA_USES_PLUGINS |
| 426 | if (mixer->v_grp) { |
| 427 | grp = mixer->v_grp; |
| 428 | ctl = grp->ctl; |
| 429 | |
| 430 | for (n = 0; n < grp->count; n++) |
| 431 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
| 432 | count++; |
| 433 | } |
| 434 | #endif |
Taylor Holberton | 94c7c83 | 2016-12-01 18:35:24 -0800 | [diff] [blame] | 435 | |
| 436 | return count; |
| 437 | } |
| 438 | |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 439 | /** Subscribes for the mixer events. |
| 440 | * @param mixer A mixer handle. |
| 441 | * @param subscribe value indicating subscribe or unsubscribe for events |
| 442 | * @returns On success, zero. |
| 443 | * On failure, non-zero. |
| 444 | * @ingroup libtinyalsa-mixer |
| 445 | */ |
| 446 | int mixer_subscribe_events(struct mixer *mixer, int subscribe) |
| 447 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 448 | struct mixer_ctl_group *grp; |
| 449 | |
| 450 | if (mixer->h_grp) { |
| 451 | grp = mixer->h_grp; |
| 452 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) |
| 453 | return -1; |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 454 | } |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 455 | |
| 456 | #ifdef TINYALSA_USES_PLUGINS |
| 457 | if (mixer->v_grp) { |
| 458 | grp = mixer->v_grp; |
| 459 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) |
| 460 | return -1; |
| 461 | } |
| 462 | #endif |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /** Wait for mixer events. |
| 467 | * @param mixer A mixer handle. |
| 468 | * @param timeout timout value |
Pankaj Bharadiya | 95c79d8 | 2017-01-11 11:28:02 +0530 | [diff] [blame] | 469 | * @returns On success, 1. |
| 470 | * On failure, -errno. |
| 471 | * On timeout, 0 |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 472 | * @ingroup libtinyalsa-mixer |
| 473 | */ |
| 474 | int mixer_wait_event(struct mixer *mixer, int timeout) |
| 475 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 476 | struct pollfd *pfd; |
| 477 | struct mixer_ctl_group *grp; |
| 478 | int count = 0, num_fds = 0, i; |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 479 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 480 | if (mixer->fd >= 0) |
| 481 | num_fds++; |
| 482 | |
| 483 | #ifdef TINYALSA_USES_PLUGINS |
| 484 | if (mixer->v_grp) |
| 485 | num_fds++; |
| 486 | #endif |
| 487 | |
| 488 | pfd = (struct pollfd *)calloc(sizeof(struct pollfd), num_fds); |
| 489 | if (!pfd) |
| 490 | return -ENOMEM; |
| 491 | |
| 492 | if (mixer->fd >= 0) { |
| 493 | pfd[count].fd = mixer->fd; |
| 494 | pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL; |
| 495 | count++; |
| 496 | } |
| 497 | |
| 498 | #ifdef TINYALSA_USES_PLUGINS |
| 499 | if (mixer->v_grp) { |
| 500 | grp = mixer->v_grp; |
| 501 | if (!grp->ops->get_poll_fd(grp->data, pfd, count)) { |
| 502 | pfd[count].events = POLLIN | POLLERR | POLLNVAL; |
| 503 | count++; |
| 504 | } |
| 505 | } |
| 506 | #endif |
| 507 | |
| 508 | if (!count) |
| 509 | return 0; |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 510 | |
| 511 | for (;;) { |
| 512 | int err; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 513 | err = poll(pfd, count, timeout); |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 514 | if (err < 0) |
| 515 | return -errno; |
| 516 | if (!err) |
| 517 | return 0; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 518 | for (i = 0; i < count; i++) { |
| 519 | if (pfd[i].revents & (POLLERR | POLLNVAL)) |
| 520 | return -EIO; |
| 521 | if (pfd[i].revents & (POLLIN | POLLOUT)) { |
| 522 | if ((i == 0) && mixer->fd >= 0) { |
| 523 | grp = mixer->h_grp; |
| 524 | grp->event_cnt++; |
| 525 | } |
| 526 | #ifdef TINYALSA_USES_PLUGINS |
| 527 | else { |
| 528 | grp = mixer->v_grp; |
| 529 | grp->event_cnt++; |
| 530 | } |
| 531 | #endif |
| 532 | return 1; |
| 533 | } |
| 534 | } |
Pankaj Bharadiya | 010121a | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 538 | static unsigned int mixer_grp_get_count(struct mixer_ctl_group *grp) |
| 539 | { |
| 540 | if (!grp) |
| 541 | return 0; |
| 542 | |
| 543 | return grp->count; |
| 544 | } |
| 545 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 546 | /** Gets a mixer control handle, by the mixer control's id. |
Taylor Holberton | a94295b | 2016-12-01 18:23:16 -0800 | [diff] [blame] | 547 | * For non-const access, see @ref mixer_get_ctl |
| 548 | * @param mixer An initialized mixer handle. |
| 549 | * @param id The control's id in the given mixer. |
| 550 | * @returns A handle to the mixer control. |
| 551 | * @ingroup libtinyalsa-mixer |
| 552 | */ |
| 553 | const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id) |
| 554 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 555 | unsigned int h_count; |
| 556 | |
| 557 | if (!mixer || (id >= mixer->total_count)) |
| 558 | return NULL; |
| 559 | |
| 560 | h_count = mixer_grp_get_count(mixer->h_grp); |
| 561 | |
| 562 | if (id < h_count) |
| 563 | return mixer->h_grp->ctl + id; |
| 564 | #ifdef TINYALSA_USES_PLUGINS |
| 565 | else { |
| 566 | unsigned int v_count = mixer_grp_get_count(mixer->v_grp); |
| 567 | if ((id - h_count) < v_count) |
| 568 | return mixer->v_grp->ctl + (id - h_count); |
| 569 | } |
| 570 | #endif |
Taylor Holberton | a94295b | 2016-12-01 18:23:16 -0800 | [diff] [blame] | 571 | |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | /** Gets a mixer control handle, by the mixer control's id. |
| 576 | * For const access, see @ref mixer_get_ctl_const |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 577 | * @param mixer An initialized mixer handle. |
| 578 | * @param id The control's id in the given mixer. |
| 579 | * @returns A handle to the mixer control. |
| 580 | * @ingroup libtinyalsa-mixer |
| 581 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 582 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 583 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 584 | unsigned int h_count; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 585 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 586 | if (!mixer || (id >= mixer->total_count)) |
| 587 | return NULL; |
| 588 | |
| 589 | h_count = mixer_grp_get_count(mixer->h_grp); |
| 590 | |
| 591 | if (id < h_count) |
| 592 | return mixer->h_grp->ctl + id; |
| 593 | #ifdef TINYALSA_USES_PLUGINS |
| 594 | else { |
| 595 | unsigned int v_count = mixer_grp_get_count(mixer->v_grp); |
| 596 | if ((id - h_count) < v_count) |
| 597 | return mixer->v_grp->ctl + (id - h_count); |
| 598 | } |
| 599 | #endif |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 600 | return NULL; |
| 601 | } |
| 602 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 603 | /** Gets the first instance of mixer control handle, by the mixer control's name. |
| 604 | * @param mixer An initialized mixer handle. |
| 605 | * @param name The control's name in the given mixer. |
| 606 | * @returns A handle to the mixer control. |
| 607 | * @ingroup libtinyalsa-mixer |
| 608 | */ |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 609 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 610 | { |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 611 | return mixer_get_ctl_by_name_and_index(mixer, name, 0); |
| 612 | } |
| 613 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 614 | /** Gets an instance of mixer control handle, by the mixer control's name and index. |
| 615 | * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control. |
| 616 | * @param mixer An initialized mixer handle. |
| 617 | * @param name The control's name in the given mixer. |
| 618 | * @param index The control's index. |
| 619 | * @returns A handle to the mixer control. |
| 620 | * @ingroup libtinyalsa-mixer |
| 621 | */ |
Frédéric Boisnard | 9e2c240 | 2013-09-17 22:43:18 +0200 | [diff] [blame] | 622 | struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer, |
| 623 | const char *name, |
| 624 | unsigned int index) |
| 625 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 626 | struct mixer_ctl_group *grp; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 627 | unsigned int n; |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 628 | struct mixer_ctl *ctl; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 629 | |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 630 | if (!mixer) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 631 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 632 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 633 | if (mixer->h_grp) { |
| 634 | grp = mixer->h_grp; |
| 635 | ctl = grp->ctl; |
Svyatoslav Mishyn | c732836 | 2016-01-24 19:43:38 +0200 | [diff] [blame] | 636 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 637 | for (n = 0; n < grp->count; n++) |
| 638 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
| 639 | if (index-- == 0) |
| 640 | return ctl + n; |
| 641 | } |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 642 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 643 | #ifdef TINYALSA_USES_PLUGINS |
| 644 | if (mixer->v_grp) { |
| 645 | grp = mixer->v_grp; |
| 646 | ctl = grp->ctl; |
| 647 | |
| 648 | for (n = 0; n < grp->count; n++) |
| 649 | if (!strcmp(name, (char*) ctl[n].info.id.name)) |
| 650 | if (index-- == 0) |
| 651 | return ctl + n; |
| 652 | } |
| 653 | #endif |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 654 | return NULL; |
| 655 | } |
| 656 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 657 | /** Updates the control's info. |
| 658 | * This is useful for a program that may be idle for a period of time. |
| 659 | * @param ctl An initialized control handle. |
| 660 | * @ingroup libtinyalsa-mixer |
| 661 | */ |
Simon Wilson | 710df88 | 2013-06-28 16:17:50 -0700 | [diff] [blame] | 662 | void mixer_ctl_update(struct mixer_ctl *ctl) |
| 663 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 664 | struct mixer_ctl_group *grp; |
| 665 | |
| 666 | if (!ctl) |
| 667 | return; |
| 668 | |
| 669 | grp = ctl->grp; |
| 670 | grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &ctl->info); |
Simon Wilson | 710df88 | 2013-06-28 16:17:50 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Pankaj Bharadiya | 9698d03 | 2017-01-09 12:23:14 +0530 | [diff] [blame] | 673 | /** Checks the control for TLV Read/Write access. |
| 674 | * @param ctl An initialized control handle. |
| 675 | * @returns On success, non-zero. |
| 676 | * On failure, zero. |
| 677 | * @ingroup libtinyalsa-mixer |
| 678 | */ |
| 679 | int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl) |
| 680 | { |
| 681 | return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE); |
| 682 | } |
| 683 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 684 | /** Gets the control's ID. |
| 685 | * @param ctl An initialized control handle. |
| 686 | * @returns On success, the control's ID is returned. |
| 687 | * On error, UINT_MAX is returned instead. |
| 688 | * @ingroup libtinyalsa-mixer |
| 689 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 690 | unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl) |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 691 | { |
| 692 | if (!ctl) |
| 693 | return UINT_MAX; |
| 694 | |
| 695 | /* numid values start at 1, return a 0-base value that |
| 696 | * can be passed to mixer_get_ctl() |
| 697 | */ |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 698 | return ctl->info.id.numid - 1; |
Richard Fitzgerald | 57a8774 | 2014-09-09 16:54:12 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 701 | /** Gets the name of the control. |
| 702 | * @param ctl An initialized control handle. |
| 703 | * @returns On success, the name of the control. |
| 704 | * On error, NULL. |
| 705 | * @ingroup libtinyalsa-mixer |
| 706 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 707 | const char *mixer_ctl_get_name(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 708 | { |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 709 | if (!ctl) |
| 710 | return NULL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 711 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 712 | return (const char *)ctl->info.id.name; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 715 | /** Gets the value type of the control. |
| 716 | * @param ctl An initialized control handle |
| 717 | * @returns On success, the type of mixer control. |
| 718 | * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN |
| 719 | * @ingroup libtinyalsa-mixer |
| 720 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 721 | enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 722 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 723 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 724 | return MIXER_CTL_TYPE_UNKNOWN; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 725 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 726 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 727 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 728 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 729 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 730 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 731 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 732 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 733 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 734 | }; |
| 735 | } |
| 736 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 737 | /** Gets the string that describes the value type of the control. |
| 738 | * @param ctl An initialized control handle |
| 739 | * @returns On success, a string describing type of mixer control. |
| 740 | * @ingroup libtinyalsa-mixer |
| 741 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 742 | const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 743 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 744 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 745 | return ""; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 746 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 747 | switch (ctl->info.type) { |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 748 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 749 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 750 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 751 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 752 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 753 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 754 | default: return "Unknown"; |
| 755 | }; |
| 756 | } |
| 757 | |
Taylor Holberton | b7a2857 | 2016-11-19 23:45:00 -0500 | [diff] [blame] | 758 | /** Gets the number of values in the control. |
| 759 | * @param ctl An initialized control handle |
| 760 | * @returns The number of values in the mixer control |
| 761 | * @ingroup libtinyalsa-mixer |
| 762 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 763 | unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 764 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 765 | if (!ctl) |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 766 | return 0; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 767 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 768 | return ctl->info.count; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 771 | static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 772 | { |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 773 | if ((percent > 100) || (percent < 0)) { |
| 774 | return -EINVAL; |
| 775 | } |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 776 | |
Baptiste Robert | 4a484e1 | 2013-09-12 15:37:53 +0200 | [diff] [blame] | 777 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 778 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 779 | return ei->value.integer.min + (range * percent) / 100; |
| 780 | } |
| 781 | |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 782 | static int int_to_percent(const struct snd_ctl_elem_info *ei, int value) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 783 | { |
| 784 | int range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 785 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 786 | if (range == 0) |
| 787 | return 0; |
Simon Wilson | 98c1f16 | 2011-06-07 16:12:32 -0700 | [diff] [blame] | 788 | |
Daniela-Marinela Bistrean | 9962e71 | 2019-04-12 00:49:20 +0300 | [diff] [blame] | 789 | return ((value - ei->value.integer.min) * 100) / range; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 792 | /** Gets a percentage representation of a specified control value. |
| 793 | * @param ctl An initialized control handle. |
| 794 | * @param id The index of the value within the control. |
| 795 | * @returns On success, the percentage representation of the control value. |
| 796 | * On failure, -EINVAL is returned. |
| 797 | * @ingroup libtinyalsa-mixer |
| 798 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 799 | int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 800 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 801 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 802 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 803 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 804 | return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id)); |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 807 | /** Sets the value of a control by percent, specified by the value index. |
| 808 | * @param ctl An initialized control handle. |
| 809 | * @param id The index of the value to set |
| 810 | * @param percent A percentage value between 0 and 100. |
| 811 | * @returns On success, zero is returned. |
| 812 | * On failure, non-zero is returned. |
| 813 | * @ingroup libtinyalsa-mixer |
| 814 | */ |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 815 | 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] | 816 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 817 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 818 | return -EINVAL; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 819 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 820 | 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] | 821 | } |
| 822 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 823 | /** Gets the value of a control. |
| 824 | * @param ctl An initialized control handle. |
| 825 | * @param id The index of the control value. |
| 826 | * @returns On success, the specified value is returned. |
| 827 | * On failure, -EINVAL is returned. |
| 828 | * @ingroup libtinyalsa-mixer |
| 829 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 830 | int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 831 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 832 | struct mixer_ctl_group *grp; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 833 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 834 | int ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 835 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 836 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 837 | return -EINVAL; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 838 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 839 | grp = ctl->grp; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 840 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 841 | ev.id.numid = ctl->info.id.numid; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 842 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 843 | if (ret < 0) |
| 844 | return ret; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 845 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 846 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 847 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 848 | return !!ev.value.integer.value[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 849 | |
| 850 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 851 | return ev.value.integer.value[id]; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 852 | |
| 853 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 854 | return ev.value.enumerated.item[id]; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 855 | |
Pierre-Louis Bossart | 5251016 | 2011-10-24 15:00:17 -0500 | [diff] [blame] | 856 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 857 | return ev.value.bytes.data[id]; |
| 858 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 859 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 860 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 866 | /** Gets the contents of a control's value array. |
| 867 | * @param ctl An initialized control handle. |
| 868 | * @param array A pointer to write the array data to. |
| 869 | * The size of this array must be equal to the number of items in the array |
| 870 | * multiplied by the size of each item. |
| 871 | * @param count The number of items in the array. |
| 872 | * This parameter must match the number of items in the control. |
| 873 | * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values |
| 874 | * @returns On success, zero. |
| 875 | * On failure, non-zero. |
| 876 | * @ingroup libtinyalsa-mixer |
| 877 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 878 | int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 879 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 880 | struct mixer_ctl_group *grp; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 881 | struct snd_ctl_elem_value ev; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 882 | int ret = 0; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 883 | size_t size; |
| 884 | void *source; |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 885 | size_t total_count; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 886 | |
Charles Keepax | e40758b | 2017-09-07 16:38:52 +0100 | [diff] [blame] | 887 | if (!ctl || !count || !array) |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 888 | return -EINVAL; |
| 889 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 890 | grp = ctl->grp; |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 891 | total_count = ctl->info.count; |
| 892 | |
| 893 | if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
| 894 | (mixer_ctl_is_access_tlv_rw(ctl))) { |
| 895 | /* Additional two words is for the TLV header */ |
| 896 | total_count += TLV_HEADER_SIZE; |
| 897 | } |
| 898 | |
| 899 | if (count > total_count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | |
| 902 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 903 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 904 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 905 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 906 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 907 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 908 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 909 | if (ret < 0) |
| 910 | return ret; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 911 | size = sizeof(ev.value.integer.value[0]); |
| 912 | source = ev.value.integer.value; |
| 913 | break; |
| 914 | |
| 915 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 916 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 9698d03 | 2017-01-09 12:23:14 +0530 | [diff] [blame] | 917 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 918 | struct snd_ctl_tlv *tlv; |
| 919 | int ret; |
| 920 | |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 921 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 922 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 923 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 924 | if (!tlv) |
| 925 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 926 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 927 | tlv->length = count; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 928 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv); |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 929 | |
| 930 | source = tlv->tlv; |
| 931 | memcpy(array, source, count); |
| 932 | |
| 933 | free(tlv); |
| 934 | |
| 935 | return ret; |
| 936 | } else { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 937 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 938 | if (ret < 0) |
| 939 | return ret; |
| 940 | size = sizeof(ev.value.bytes.data[0]); |
| 941 | source = ev.value.bytes.data; |
| 942 | break; |
| 943 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 944 | |
| 945 | default: |
| 946 | return -EINVAL; |
| 947 | } |
| 948 | |
| 949 | memcpy(array, source, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 954 | /** Sets the value of a control, specified by the value index. |
| 955 | * @param ctl An initialized control handle. |
| 956 | * @param id The index of the value within the control. |
| 957 | * @param value The value to set. |
| 958 | * This must be in a range specified by @ref mixer_ctl_get_range_min |
| 959 | * and @ref mixer_ctl_get_range_max. |
| 960 | * @returns On success, zero is returned. |
| 961 | * On failure, non-zero is returned. |
| 962 | * @ingroup libtinyalsa-mixer |
| 963 | */ |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 964 | 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] | 965 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 966 | struct mixer_ctl_group *grp; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 967 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 968 | int ret; |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 969 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 970 | if (!ctl || (id >= ctl->info.count)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 971 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 972 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 973 | grp = ctl->grp; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 974 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 975 | ev.id.numid = ctl->info.id.numid; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 976 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 977 | if (ret < 0) |
| 978 | return ret; |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 979 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 980 | switch (ctl->info.type) { |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 981 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 982 | ev.value.integer.value[id] = !!value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 983 | break; |
| 984 | |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 985 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Baptiste Robert | 6cd7d5f | 2013-08-07 11:32:45 +0200 | [diff] [blame] | 986 | if ((value < mixer_ctl_get_range_min(ctl)) || |
| 987 | (value > mixer_ctl_get_range_max(ctl))) { |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | |
Simon Wilson | d2cb503 | 2011-06-04 00:57:17 -0700 | [diff] [blame] | 991 | ev.value.integer.value[id] = value; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 992 | break; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 993 | |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 994 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 995 | ev.value.enumerated.item[id] = value; |
| 996 | break; |
| 997 | |
David.Coutherut | ce2b634 | 2013-06-11 16:22:57 +0200 | [diff] [blame] | 998 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 999 | ev.value.bytes.data[id] = value; |
| 1000 | break; |
| 1001 | |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1002 | default: |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1003 | return -EINVAL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1004 | } |
Simon Wilson | a1bb1e0 | 2011-05-26 18:22:00 -0700 | [diff] [blame] | 1005 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1006 | return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 1009 | /** Sets the contents of a control's value array. |
| 1010 | * @param ctl An initialized control handle. |
| 1011 | * @param array The array containing control values. |
| 1012 | * @param count The number of values in the array. |
| 1013 | * This must match the number of values in the control. |
| 1014 | * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values |
| 1015 | * @returns On success, zero. |
| 1016 | * On failure, non-zero. |
| 1017 | * @ingroup libtinyalsa-mixer |
| 1018 | */ |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 1019 | 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] | 1020 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1021 | struct mixer_ctl_group *grp; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 1022 | struct snd_ctl_elem_value ev; |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 1023 | size_t size; |
| 1024 | void *dest; |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 1025 | size_t total_count; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 1026 | |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 1027 | if ((!ctl) || !count || !array) |
| 1028 | return -EINVAL; |
| 1029 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1030 | grp = ctl->grp; |
Pankaj Bharadiya | 3f813e4 | 2017-01-09 13:42:14 +0530 | [diff] [blame] | 1031 | total_count = ctl->info.count; |
| 1032 | |
| 1033 | if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
| 1034 | (mixer_ctl_is_access_tlv_rw(ctl))) { |
| 1035 | /* Additional TLV header */ |
| 1036 | total_count += TLV_HEADER_SIZE; |
| 1037 | } |
| 1038 | |
| 1039 | if (count > total_count) |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 1040 | return -EINVAL; |
| 1041 | |
| 1042 | memset(&ev, 0, sizeof(ev)); |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1043 | ev.id.numid = ctl->info.id.numid; |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 1044 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1045 | switch (ctl->info.type) { |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 1046 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 1047 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 1048 | size = sizeof(ev.value.integer.value[0]); |
| 1049 | dest = ev.value.integer.value; |
| 1050 | break; |
| 1051 | |
| 1052 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 1053 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 9698d03 | 2017-01-09 12:23:14 +0530 | [diff] [blame] | 1054 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 1055 | struct snd_ctl_tlv *tlv; |
| 1056 | int ret = 0; |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 1057 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 1058 | return -EINVAL; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 1059 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 7ed2ffb | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 1060 | if (!tlv) |
| 1061 | return -ENOMEM; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1062 | tlv->numid = ctl->info.id.numid; |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 1063 | tlv->length = count; |
| 1064 | memcpy(tlv->tlv, array, count); |
| 1065 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1066 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv); |
Mythri P K | 45b2d04 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 1067 | free(tlv); |
| 1068 | |
| 1069 | return ret; |
| 1070 | } else { |
| 1071 | size = sizeof(ev.value.bytes.data[0]); |
| 1072 | dest = ev.value.bytes.data; |
| 1073 | } |
Simon Wilson | 38f87f3 | 2012-10-23 15:05:23 -0700 | [diff] [blame] | 1074 | break; |
| 1075 | |
| 1076 | default: |
| 1077 | return -EINVAL; |
| 1078 | } |
| 1079 | |
| 1080 | memcpy(dest, array, size * count); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 1081 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1082 | return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
Dimitris Papastamos | f51c05b | 2012-05-28 13:40:33 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 1085 | /** Gets the minimum value of an control. |
| 1086 | * The control must have an integer type. |
| 1087 | * The type of the control can be checked with @ref mixer_ctl_get_type. |
| 1088 | * @param ctl An initialized control handle. |
| 1089 | * @returns On success, the minimum value of the control. |
| 1090 | * On failure, -EINVAL. |
| 1091 | * @ingroup libtinyalsa-mixer |
| 1092 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 1093 | int mixer_ctl_get_range_min(const struct mixer_ctl *ctl) |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 1094 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1095 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1096 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 1097 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1098 | return ctl->info.value.integer.min; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 1101 | /** Gets the maximum value of an control. |
| 1102 | * The control must have an integer type. |
| 1103 | * The type of the control can be checked with @ref mixer_ctl_get_type. |
| 1104 | * @param ctl An initialized control handle. |
| 1105 | * @returns On success, the maximum value of the control. |
| 1106 | * On failure, -EINVAL. |
| 1107 | * @ingroup libtinyalsa-mixer |
| 1108 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 1109 | int mixer_ctl_get_range_max(const struct mixer_ctl *ctl) |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 1110 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1111 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1112 | return -EINVAL; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 1113 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1114 | return ctl->info.value.integer.max; |
Simon Wilson | b9d4f6b | 2011-06-06 14:41:02 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 1117 | /** Get the number of enumerated items in the control. |
| 1118 | * @param ctl An initialized control handle. |
| 1119 | * @returns The number of enumerated items in the control. |
| 1120 | * @ingroup libtinyalsa-mixer |
| 1121 | */ |
Taylor Holberton | cac43a2 | 2016-12-01 18:11:24 -0800 | [diff] [blame] | 1122 | unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 1123 | { |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1124 | if (!ctl) |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 1125 | return 0; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 1126 | |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1127 | return ctl->info.value.enumerated.items; |
Simon Wilson | 066c9f6 | 2011-06-05 18:23:05 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1130 | int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl) |
| 1131 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1132 | struct mixer_ctl_group *grp = ctl->grp; |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1133 | struct snd_ctl_elem_info tmp; |
| 1134 | unsigned int m; |
| 1135 | char **enames; |
| 1136 | |
| 1137 | if (ctl->ename) { |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 1141 | enames = calloc(ctl->info.value.enumerated.items, sizeof(char*)); |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1142 | if (!enames) |
| 1143 | goto fail; |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 1144 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1145 | memset(&tmp, 0, sizeof(tmp)); |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 1146 | tmp.id.numid = ctl->info.id.numid; |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1147 | tmp.value.enumerated.item = m; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1148 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1149 | goto fail; |
| 1150 | enames[m] = strdup(tmp.value.enumerated.name); |
| 1151 | if (!enames[m]) |
| 1152 | goto fail; |
| 1153 | } |
| 1154 | ctl->ename = enames; |
| 1155 | return 0; |
| 1156 | |
| 1157 | fail: |
| 1158 | if (enames) { |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 1159 | for (m = 0; m < ctl->info.value.enumerated.items; m++) { |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1160 | if (enames[m]) { |
| 1161 | free(enames[m]); |
| 1162 | } |
| 1163 | } |
| 1164 | free(enames); |
| 1165 | } |
| 1166 | return -1; |
| 1167 | } |
| 1168 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 1169 | /** Gets the string representation of an enumerated item. |
| 1170 | * @param ctl An initialized control handle. |
| 1171 | * @param enum_id The index of the enumerated value. |
| 1172 | * @returns A string representation of the enumerated item. |
| 1173 | * @ingroup libtinyalsa-mixer |
| 1174 | */ |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 1175 | const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, |
| 1176 | unsigned int enum_id) |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1177 | { |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1178 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 1179 | (enum_id >= ctl->info.value.enumerated.items) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1180 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 1181 | return NULL; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1182 | |
Simon Wilson | b29ac1a | 2012-03-08 10:15:08 -0800 | [diff] [blame] | 1183 | return (const char *)ctl->ename[enum_id]; |
Simon Wilson | 79d3965 | 2011-05-25 13:44:23 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Taylor Holberton | 4e55719 | 2016-11-23 11:48:06 -0800 | [diff] [blame] | 1186 | /** Set an enumeration value by string value. |
| 1187 | * @param ctl An enumerated mixer control. |
| 1188 | * @param string The string representation of an enumeration. |
| 1189 | * @returns On success, zero. |
| 1190 | * On failure, zero. |
| 1191 | * @ingroup libtinyalsa-mixer |
| 1192 | */ |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1193 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 1194 | { |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1195 | struct mixer_ctl_group *grp; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1196 | unsigned int i, num_enums; |
| 1197 | struct snd_ctl_elem_value ev; |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1198 | int ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1199 | |
Taylor Holberton | 6a38d5f | 2016-10-03 21:07:39 -0400 | [diff] [blame] | 1200 | if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
David.Coutherut | 9b42396 | 2013-06-03 13:45:51 +0200 | [diff] [blame] | 1201 | mixer_ctl_fill_enum_string(ctl) != 0) |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1202 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1203 | |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1204 | grp = ctl->grp; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1205 | num_enums = ctl->info.value.enumerated.items; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1206 | for (i = 0; i < num_enums; i++) { |
| 1207 | if (!strcmp(string, ctl->ename[i])) { |
| 1208 | memset(&ev, 0, sizeof(ev)); |
| 1209 | ev.value.enumerated.item[0] = i; |
Richard Fitzgerald | 899cece | 2014-09-09 17:03:21 +0100 | [diff] [blame] | 1210 | ev.id.numid = ctl->info.id.numid; |
Bhalchandra Gajare | e7c627d | 2019-06-19 15:30:42 -0700 | [diff] [blame] | 1211 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1212 | if (ret < 0) |
| 1213 | return ret; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | } |
| 1217 | |
Simon Wilson | 193b1c3 | 2011-06-07 23:42:38 -0700 | [diff] [blame] | 1218 | return -EINVAL; |
Simon Wilson | f0a20ee | 2011-06-05 21:18:52 -0700 | [diff] [blame] | 1219 | } |