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