Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 1 | /* mixer.c |
| 2 | ** |
| 3 | ** Copyright 2011, The Android Open Source Project |
| 4 | ** |
| 5 | ** Redistribution and use in source and binary forms, with or without |
| 6 | ** modification, are permitted provided that the following conditions are met: |
| 7 | ** * Redistributions of source code must retain the above copyright |
| 8 | ** notice, this list of conditions and the following disclaimer. |
| 9 | ** * Redistributions in binary form must reproduce the above copyright |
| 10 | ** notice, this list of conditions and the following disclaimer in the |
| 11 | ** documentation and/or other materials provided with the distribution. |
| 12 | ** * Neither the name of The Android Open Source Project nor the names of |
| 13 | ** its contributors may be used to endorse or promote products derived |
| 14 | ** from this software without specific prior written permission. |
| 15 | ** |
| 16 | ** THIS SOFTWARE IS PROVIDED BY The Android Open Source Project ``AS IS'' AND |
| 17 | ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | ** ARE DISCLAIMED. IN NO EVENT SHALL The Android Open Source Project BE LIABLE |
| 20 | ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 24 | ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 | ** DAMAGE. |
| 27 | */ |
| 28 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 29 | #include <stdbool.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 32 | #include <stdint.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <errno.h> |
| 37 | #include <ctype.h> |
Pankaj Bharadiya | 415c0c3 | 2017-01-11 08:57:06 +0530 | [diff] [blame] | 38 | #include <poll.h> |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 39 | |
Simon Wilson | 85dc38f | 2012-05-15 17:37:19 -0700 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
| 41 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 42 | #include <linux/ioctl.h> |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 43 | |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 44 | #define __force |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 45 | #define __bitwise |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 46 | #define __user |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 47 | #include <sound/asound.h> |
| 48 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 49 | #ifndef SNDRV_CTL_ELEM_ID_NAME_MAXLEN |
| 50 | #define SNDRV_CTL_ELEM_ID_NAME_MAXLEN 44 |
| 51 | #endif |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 52 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 53 | #include <tinyalsa/asoundlib.h> |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 54 | #include "mixer_io.h" |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 55 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 56 | struct mixer_ctl { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 57 | struct mixer_ctl_group *grp; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 58 | struct snd_ctl_elem_info *info; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 59 | char **ename; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 60 | bool info_retrieved; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 63 | struct mixer_ctl_group { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 64 | struct snd_ctl_elem_info *elem_info; |
| 65 | struct mixer_ctl *ctl; |
| 66 | unsigned int count; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 67 | int event_cnt; |
| 68 | |
| 69 | struct mixer_ops *ops; |
| 70 | void *data; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 73 | struct mixer { |
| 74 | int fd; |
| 75 | struct snd_ctl_card_info card_info; |
| 76 | |
| 77 | /* hardware/physical mixer control group */ |
| 78 | struct mixer_ctl_group *hw_grp; |
| 79 | |
| 80 | /* |
| 81 | * Virutal mixer control group. |
| 82 | * Currently supports one virtual mixer (.so) |
| 83 | * per card. Could be extended to multiple |
| 84 | */ |
| 85 | struct mixer_ctl_group *virt_grp; |
| 86 | |
| 87 | unsigned int total_ctl_count; |
| 88 | }; |
| 89 | |
| 90 | static void mixer_grp_close(struct mixer_ctl_group *grp) |
| 91 | { |
| 92 | unsigned int n, m; |
| 93 | |
| 94 | if (!grp) |
| 95 | return; |
| 96 | |
| 97 | if (grp->ctl) { |
| 98 | for (n = 0; n < grp->count; n++) { |
| 99 | if (grp->ctl[n].ename) { |
| 100 | unsigned int max = grp->ctl[n].info->value.enumerated.items; |
| 101 | for (m = 0; m < max; m++) |
| 102 | free(grp->ctl[n].ename[m]); |
| 103 | free(grp->ctl[n].ename); |
| 104 | } |
| 105 | } |
| 106 | free(grp->ctl); |
| 107 | } |
| 108 | |
| 109 | if (grp->elem_info) |
| 110 | free(grp->elem_info); |
| 111 | |
| 112 | free(grp); |
| 113 | } |
| 114 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 115 | void mixer_close(struct mixer *mixer) |
| 116 | { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 117 | if (!mixer) |
| 118 | return; |
| 119 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 120 | if (mixer->fd >= 0 && mixer->hw_grp) |
| 121 | mixer->hw_grp->ops->close(mixer->hw_grp->data); |
| 122 | mixer_grp_close(mixer->hw_grp); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 123 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 124 | if (mixer->virt_grp) |
| 125 | mixer->virt_grp->ops->close(mixer->virt_grp->data); |
| 126 | mixer_grp_close(mixer->virt_grp); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 127 | |
| 128 | free(mixer); |
| 129 | |
| 130 | /* TODO: verify frees */ |
| 131 | } |
| 132 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 133 | static int mixer_grp_open(struct mixer_ctl_group **ctl_grp, |
| 134 | struct mixer_ops *ops, |
| 135 | void *data, int *num_ctls_in_grp) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 136 | { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 137 | struct mixer_ctl_group *grp; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 138 | struct snd_ctl_elem_list elist; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 139 | struct snd_ctl_elem_id *eid = NULL; |
John Muir | 22bca93 | 2017-10-23 10:58:12 -0700 | [diff] [blame] | 140 | unsigned int n; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 141 | int ret; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 142 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 143 | grp = calloc(1, sizeof(*grp)); |
| 144 | if (!grp) |
| 145 | return -ENOMEM; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 146 | |
| 147 | memset(&elist, 0, sizeof(elist)); |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 148 | ret = ops->ioctl(data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist); |
| 149 | if (ret < 0) |
| 150 | goto err_get_elem_list; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 151 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 152 | grp->ctl = calloc(elist.count, sizeof(struct mixer_ctl)); |
| 153 | grp->elem_info = calloc(elist.count, sizeof(struct snd_ctl_elem_info)); |
| 154 | if (!grp->ctl || !grp->elem_info) { |
| 155 | ret = -ENOMEM; |
| 156 | goto err_ctl_alloc; |
| 157 | } |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 158 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 159 | eid = calloc(elist.count, sizeof(*eid)); |
| 160 | if (!eid) { |
| 161 | ret = -ENOMEM; |
| 162 | goto err_ctl_alloc; |
| 163 | } |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 164 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 165 | grp->count = elist.count; |
| 166 | elist.space = grp->count; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 167 | elist.pids = eid; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 168 | ret = ops->ioctl(data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist); |
| 169 | if (ret < 0) |
| 170 | goto err_ctl_alloc; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 171 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 172 | for (n = 0; n < grp->count; n++) { |
| 173 | struct mixer_ctl *ctl = grp->ctl + n; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 174 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 175 | ctl->grp = grp; |
| 176 | ctl->info = grp->elem_info + n; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 177 | ctl->info->id.numid = eid[n].numid; |
| 178 | strncpy((char *)ctl->info->id.name, (char *)eid[n].name, |
| 179 | SNDRV_CTL_ELEM_ID_NAME_MAXLEN); |
| 180 | ctl->info->id.name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1] = 0; |
| 181 | } |
| 182 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 183 | grp->data = data; |
| 184 | grp->ops = ops; |
| 185 | *ctl_grp = grp; |
| 186 | *num_ctls_in_grp = grp->count; |
| 187 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 188 | free(eid); |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | |
| 191 | err_ctl_alloc: |
| 192 | |
| 193 | free(eid); |
| 194 | free(grp->elem_info); |
| 195 | free(grp->ctl); |
| 196 | |
| 197 | err_get_elem_list: |
| 198 | |
| 199 | free(grp); |
| 200 | return ret; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | static int mixer_do_hw_open(struct mixer *mixer, unsigned int card) |
| 205 | { |
| 206 | struct mixer_ops *ops; |
| 207 | void *data; |
| 208 | int fd, ret, num_grp_ctls = 0; |
| 209 | |
| 210 | mixer->fd = -1; |
| 211 | fd = mixer_hw_open(card, &data, &ops); |
| 212 | if (fd < 0) |
| 213 | return fd; |
| 214 | |
| 215 | ret = ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info); |
| 216 | if (ret < 0) |
| 217 | goto err_card_info; |
| 218 | |
| 219 | ret = mixer_grp_open(&mixer->hw_grp, ops, data, &num_grp_ctls); |
| 220 | if (ret < 0) |
| 221 | goto err_card_info; |
| 222 | |
| 223 | mixer->total_ctl_count += num_grp_ctls; |
| 224 | |
| 225 | mixer->fd = fd; |
| 226 | return 0; |
| 227 | |
| 228 | err_card_info: |
| 229 | ops->close(data); |
| 230 | return ret; |
| 231 | |
| 232 | } |
| 233 | |
| 234 | static int mixer_do_plugin_open(struct mixer *mixer, unsigned int card, |
| 235 | int is_hw_open_failed) |
| 236 | { |
| 237 | struct mixer_ops *ops; |
| 238 | void *data; |
| 239 | int ret, num_grp_ctls = 0; |
| 240 | |
| 241 | ret = mixer_plugin_open(card, &data, &ops); |
| 242 | if (ret < 0) |
| 243 | return ret; |
| 244 | |
| 245 | /* Get card_info if hw_open failed */ |
| 246 | if (is_hw_open_failed) { |
| 247 | ret = ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO, &mixer->card_info); |
| 248 | if (ret < 0) |
| 249 | goto err_card_info; |
| 250 | } |
| 251 | |
| 252 | ret = mixer_grp_open(&mixer->virt_grp, ops, data, &num_grp_ctls); |
| 253 | if (ret < 0) |
| 254 | goto err_card_info; |
| 255 | |
| 256 | mixer->total_ctl_count += num_grp_ctls; |
| 257 | return 0; |
| 258 | |
| 259 | err_card_info: |
| 260 | ops->close(data); |
| 261 | return ret; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | struct mixer *mixer_open(unsigned int card) |
| 266 | { |
| 267 | struct mixer *mixer = NULL; |
| 268 | int h_status, v_status; |
| 269 | |
| 270 | mixer = calloc(1, sizeof(*mixer)); |
| 271 | if (!mixer) |
| 272 | goto fail; |
| 273 | |
| 274 | /* open hardware node first */ |
| 275 | h_status = mixer_do_hw_open(mixer, card); |
| 276 | |
| 277 | /* |
| 278 | * open the virtual node even if hw_open fails |
| 279 | * since hw_open is expected to fail for virtual cards |
| 280 | * for which kernel does not register mixer node |
| 281 | */ |
| 282 | //TODO: open virtual node only if mixer is defined under snd-card-def |
| 283 | v_status = mixer_do_plugin_open(mixer, card, h_status); |
| 284 | |
| 285 | /* Fail mixer_open if both hw and plugin nodes cannot be opened */ |
| 286 | if (h_status < 0 && v_status < 0) |
| 287 | goto fail; |
| 288 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 289 | return mixer; |
| 290 | |
| 291 | fail: |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 292 | if (mixer) |
| 293 | mixer_close(mixer); |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 294 | |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 295 | return 0; |
Simon Wilson | 7e8a656 | 2013-06-28 16:47:16 -0700 | [diff] [blame] | 296 | } |
| 297 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 298 | static bool mixer_ctl_get_elem_info(struct mixer_ctl* ctl) |
| 299 | { |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 300 | struct mixer_ctl_group *grp; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 301 | unsigned int i; |
| 302 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 303 | if (!ctl || !ctl->grp) |
| 304 | return false; |
| 305 | |
| 306 | grp = ctl->grp; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 307 | if (!ctl->info_retrieved) { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 308 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, |
| 309 | ctl->info) < 0) |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 310 | return false; |
| 311 | ctl->info_retrieved = true; |
| 312 | } |
| 313 | |
| 314 | if (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED || ctl->ename) |
| 315 | return true; |
| 316 | |
| 317 | struct snd_ctl_elem_info tmp; |
| 318 | char** enames = calloc(ctl->info->value.enumerated.items, sizeof(char*)); |
| 319 | if (!enames) |
| 320 | return false; |
| 321 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 322 | for (i = 0; i < ctl->info->value.enumerated.items; i++) { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 323 | memset(&tmp, 0, sizeof(tmp)); |
| 324 | tmp.id.numid = ctl->info->id.numid; |
| 325 | tmp.value.enumerated.item = i; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 326 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0) |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 327 | goto fail; |
| 328 | enames[i] = strdup(tmp.value.enumerated.name); |
| 329 | if (!enames[i]) |
| 330 | goto fail; |
| 331 | } |
| 332 | ctl->ename = enames; |
| 333 | return true; |
| 334 | |
| 335 | fail: |
| 336 | free(enames); |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | const char *mixer_get_name(struct mixer *mixer) |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 341 | { |
| 342 | return (const char *)mixer->card_info.name; |
| 343 | } |
| 344 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 345 | unsigned int mixer_get_num_ctls(struct mixer *mixer) |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 346 | { |
| 347 | if (!mixer) |
| 348 | return 0; |
| 349 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 350 | return mixer->total_ctl_count; |
| 351 | } |
| 352 | |
| 353 | static int mixer_grp_get_count(struct mixer_ctl_group *grp) |
| 354 | { |
| 355 | if (!grp) |
| 356 | return 0; |
| 357 | |
| 358 | return grp->count; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 359 | } |
| 360 | |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 361 | struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id) |
| 362 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 363 | struct mixer_ctl *ctl; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 364 | unsigned int hw_ctl_count, virt_ctl_count; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 365 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 366 | if (!mixer || (id >= mixer->total_ctl_count)) |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 367 | return NULL; |
| 368 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 369 | hw_ctl_count = mixer_grp_get_count(mixer->hw_grp); |
| 370 | virt_ctl_count = mixer_grp_get_count(mixer->virt_grp); |
| 371 | |
| 372 | if (id < hw_ctl_count) |
| 373 | ctl = mixer->hw_grp->ctl + id; |
| 374 | else if ((id - hw_ctl_count) < virt_ctl_count) |
| 375 | ctl = mixer->virt_grp->ctl + (id - hw_ctl_count); |
| 376 | else |
| 377 | return NULL; |
| 378 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 379 | if (!mixer_ctl_get_elem_info(ctl)) |
| 380 | return NULL; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 381 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 382 | return ctl; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 383 | } |
| 384 | |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 385 | struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name) |
| 386 | { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 387 | struct mixer_ctl_group *grp; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 388 | unsigned int n; |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 389 | int hw_ctl_count; |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 390 | |
| 391 | if (!mixer) |
| 392 | return NULL; |
| 393 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 394 | hw_ctl_count = mixer_grp_get_count(mixer->hw_grp); |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 395 | if (mixer->hw_grp) { |
| 396 | grp = mixer->hw_grp; |
| 397 | |
| 398 | for (n = 0; n < grp->count; n++) |
| 399 | if (!strcmp(name, (char*) grp->elem_info[n].id.name)) |
| 400 | return mixer_get_ctl(mixer, n); |
| 401 | } |
| 402 | |
| 403 | if (mixer->virt_grp) { |
| 404 | grp = mixer->virt_grp; |
| 405 | |
| 406 | for (n = 0; n < grp->count; n++) |
| 407 | if (!strcmp(name, (char*) grp->elem_info[n].id.name)) |
| 408 | return mixer_get_ctl(mixer, n + hw_ctl_count); |
| 409 | } |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 410 | |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 411 | return NULL; |
| 412 | } |
| 413 | |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 414 | void mixer_ctl_update(struct mixer_ctl *ctl) |
| 415 | { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 416 | struct mixer_ctl_group *grp; |
| 417 | |
| 418 | if (!ctl || !ctl->grp) |
| 419 | return; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 420 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 421 | grp = ctl->grp; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 422 | grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ctl->info); |
David Li | e0f4106 | 2020-11-05 12:27:29 +0000 | [diff] [blame] | 423 | } |
| 424 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 425 | const char *mixer_ctl_get_name(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 426 | { |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 427 | if (!ctl) |
| 428 | return NULL; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 429 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 430 | return (const char *)ctl->info->id.name; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 431 | } |
| 432 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 433 | enum mixer_ctl_type mixer_ctl_get_type(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 434 | { |
| 435 | if (!ctl) |
| 436 | return MIXER_CTL_TYPE_UNKNOWN; |
| 437 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 438 | switch (ctl->info->type) { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 439 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL; |
| 440 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT; |
| 441 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM; |
| 442 | case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE; |
| 443 | case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958; |
| 444 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64; |
| 445 | default: return MIXER_CTL_TYPE_UNKNOWN; |
| 446 | }; |
| 447 | } |
| 448 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 449 | const char *mixer_ctl_get_type_string(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 450 | { |
| 451 | if (!ctl) |
| 452 | return ""; |
| 453 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 454 | switch (ctl->info->type) { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 455 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL"; |
| 456 | case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT"; |
| 457 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM"; |
| 458 | case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE"; |
| 459 | case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958"; |
| 460 | case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64"; |
| 461 | default: return "Unknown"; |
| 462 | }; |
| 463 | } |
| 464 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 465 | unsigned int mixer_ctl_get_num_values(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 466 | { |
| 467 | if (!ctl) |
| 468 | return 0; |
| 469 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 470 | return ctl->info->count; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 471 | } |
| 472 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 473 | static int percent_to_int(struct snd_ctl_elem_info *ei, int percent) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 474 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 475 | int range; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 476 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 477 | if (percent > 100) |
| 478 | percent = 100; |
| 479 | else if (percent < 0) |
| 480 | percent = 0; |
| 481 | |
| 482 | range = (ei->value.integer.max - ei->value.integer.min); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 483 | |
| 484 | return ei->value.integer.min + (range * percent) / 100; |
| 485 | } |
| 486 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 487 | static int int_to_percent(struct snd_ctl_elem_info *ei, int value) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 488 | { |
| 489 | int range = (ei->value.integer.max - ei->value.integer.min); |
| 490 | |
| 491 | if (range == 0) |
| 492 | return 0; |
| 493 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 494 | return ((value - ei->value.integer.min) / range) * 100; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 495 | } |
| 496 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 497 | int mixer_ctl_get_percent(struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 498 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 499 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 500 | return -EINVAL; |
| 501 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 502 | return int_to_percent(ctl->info, mixer_ctl_get_value(ctl, id)); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent) |
| 506 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 507 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 508 | return -EINVAL; |
| 509 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 510 | return mixer_ctl_set_value(ctl, id, percent_to_int(ctl->info, percent)); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 511 | } |
| 512 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 513 | int mixer_ctl_get_value(struct mixer_ctl *ctl, unsigned int id) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 514 | { |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 515 | struct mixer_ctl_group *grp; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 516 | struct snd_ctl_elem_value ev; |
| 517 | int ret; |
| 518 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 519 | if (!ctl || (id >= ctl->info->count) || !ctl->grp) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 520 | return -EINVAL; |
| 521 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 522 | grp = ctl->grp; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 523 | memset(&ev, 0, sizeof(ev)); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 524 | ev.id.numid = ctl->info->id.numid; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 525 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 526 | if (ret < 0) |
| 527 | return ret; |
| 528 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 529 | switch (ctl->info->type) { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 530 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 531 | return !!ev.value.integer.value[id]; |
| 532 | |
| 533 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 534 | return ev.value.integer.value[id]; |
| 535 | |
| 536 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 537 | return ev.value.enumerated.item[id]; |
| 538 | |
Simon Wilson | 5aed71d | 2011-11-16 14:45:38 -0800 | [diff] [blame] | 539 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 540 | return ev.value.bytes.data[id]; |
| 541 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 542 | default: |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 549 | int mixer_ctl_is_access_tlv_rw(struct mixer_ctl *ctl) |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame] | 550 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 551 | return (ctl->info->access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE); |
| 552 | } |
| 553 | |
| 554 | int mixer_ctl_get_array(struct mixer_ctl *ctl, void *array, size_t count) |
| 555 | { |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 556 | struct mixer_ctl_group *grp; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 557 | struct snd_ctl_elem_value ev; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 558 | int ret = 0; |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 559 | size_t size; |
| 560 | void *source; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 561 | size_t total_count; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 562 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 563 | if ((!ctl) || !count || !array || !ctl->grp) |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 564 | return -EINVAL; |
| 565 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 566 | grp = ctl->grp; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 567 | total_count = ctl->info->count; |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 568 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 569 | if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
| 570 | mixer_ctl_is_access_tlv_rw(ctl)) { |
| 571 | /* Additional two words is for the TLV header */ |
| 572 | total_count += TLV_HEADER_SIZE; |
| 573 | } |
| 574 | |
| 575 | if (count > total_count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 576 | return -EINVAL; |
| 577 | |
| 578 | memset(&ev, 0, sizeof(ev)); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 579 | ev.id.numid = ctl->info->id.numid; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 580 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 581 | switch (ctl->info->type) { |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 582 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 583 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 584 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 585 | if (ret < 0) |
| 586 | return ret; |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 587 | size = sizeof(ev.value.integer.value[0]); |
| 588 | source = ev.value.integer.value; |
| 589 | break; |
| 590 | |
| 591 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 592 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame] | 593 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 594 | struct snd_ctl_tlv *tlv; |
| 595 | int ret; |
| 596 | |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 597 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 598 | return -EINVAL; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 599 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 600 | if (!tlv) |
| 601 | return -ENOMEM; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 602 | tlv->numid = ctl->info->id.numid; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 603 | tlv->length = count; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 604 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv); |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 605 | |
| 606 | source = tlv->tlv; |
| 607 | memcpy(array, source, count); |
| 608 | |
| 609 | free(tlv); |
| 610 | |
| 611 | return ret; |
| 612 | } else { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 613 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 614 | if (ret < 0) |
| 615 | return ret; |
| 616 | size = sizeof(ev.value.bytes.data[0]); |
| 617 | source = ev.value.bytes.data; |
| 618 | break; |
| 619 | } |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 620 | |
Yogesh Agrawal | 49a6137 | 2013-08-02 20:04:58 +0530 | [diff] [blame] | 621 | case SNDRV_CTL_ELEM_TYPE_IEC958: |
David Li | 10c9ed0 | 2022-01-19 02:37:27 +0000 | [diff] [blame] | 622 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
| 623 | if (ret < 0) |
| 624 | return ret; |
Yogesh Agrawal | 49a6137 | 2013-08-02 20:04:58 +0530 | [diff] [blame] | 625 | size = sizeof(ev.value.iec958); |
| 626 | source = &ev.value.iec958; |
| 627 | break; |
| 628 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 629 | default: |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | |
| 633 | memcpy(array, source, size * count); |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 638 | int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value) |
| 639 | { |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 640 | struct mixer_ctl_group *grp; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 641 | struct snd_ctl_elem_value ev; |
| 642 | int ret; |
| 643 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 644 | if (!ctl || (id >= ctl->info->count) || !ctl->grp) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 645 | return -EINVAL; |
| 646 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 647 | grp = ctl->grp; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 648 | memset(&ev, 0, sizeof(ev)); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 649 | ev.id.numid = ctl->info->id.numid; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 650 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 651 | if (ret < 0) |
| 652 | return ret; |
| 653 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 654 | switch (ctl->info->type) { |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 655 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 656 | ev.value.integer.value[id] = !!value; |
| 657 | break; |
| 658 | |
| 659 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 660 | ev.value.integer.value[id] = value; |
| 661 | break; |
| 662 | |
| 663 | case SNDRV_CTL_ELEM_TYPE_ENUMERATED: |
| 664 | ev.value.enumerated.item[id] = value; |
| 665 | break; |
| 666 | |
David.Coutherut | a8481aa | 2013-06-11 16:22:57 +0200 | [diff] [blame] | 667 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
| 668 | ev.value.bytes.data[id] = value; |
| 669 | break; |
| 670 | |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 671 | default: |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 675 | return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 678 | int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 679 | { |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 680 | struct mixer_ctl_group *grp; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 681 | struct snd_ctl_elem_value ev; |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 682 | size_t size; |
| 683 | void *dest; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 684 | size_t total_count; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 685 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 686 | if ((!ctl) || !count || !array || !ctl->grp) |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 687 | return -EINVAL; |
| 688 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 689 | grp = ctl->grp; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 690 | total_count = ctl->info->count; |
Pawse, GuruprasadX | b4e8d0d | 2016-02-02 17:52:29 +0530 | [diff] [blame] | 691 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 692 | if ((ctl->info->type == SNDRV_CTL_ELEM_TYPE_BYTES) && |
| 693 | mixer_ctl_is_access_tlv_rw(ctl)) { |
| 694 | /* Additional two words is for the TLV header */ |
| 695 | total_count += TLV_HEADER_SIZE; |
| 696 | } |
| 697 | |
| 698 | if (count > total_count) |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 699 | return -EINVAL; |
| 700 | |
| 701 | memset(&ev, 0, sizeof(ev)); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 702 | ev.id.numid = ctl->info->id.numid; |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 703 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 704 | switch (ctl->info->type) { |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 705 | case SNDRV_CTL_ELEM_TYPE_BOOLEAN: |
| 706 | case SNDRV_CTL_ELEM_TYPE_INTEGER: |
| 707 | size = sizeof(ev.value.integer.value[0]); |
| 708 | dest = ev.value.integer.value; |
| 709 | break; |
| 710 | |
| 711 | case SNDRV_CTL_ELEM_TYPE_BYTES: |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 712 | /* check if this is new bytes TLV */ |
Pankaj Bharadiya | 10d4b8f | 2017-01-04 11:10:58 +0530 | [diff] [blame] | 713 | if (mixer_ctl_is_access_tlv_rw(ctl)) { |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 714 | struct snd_ctl_tlv *tlv; |
| 715 | int ret = 0; |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 716 | if (count > SIZE_MAX - sizeof(*tlv)) |
| 717 | return -EINVAL; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 718 | tlv = calloc(1, sizeof(*tlv) + count); |
Ben Zhang | 6cb14f2 | 2016-04-22 17:59:40 -0700 | [diff] [blame] | 719 | if (!tlv) |
| 720 | return -ENOMEM; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 721 | tlv->numid = ctl->info->id.numid; |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 722 | tlv->length = count; |
| 723 | memcpy(tlv->tlv, array, count); |
| 724 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 725 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv); |
Mythri P K | 67b1df4 | 2014-08-18 15:39:36 +0530 | [diff] [blame] | 726 | free(tlv); |
| 727 | |
| 728 | return ret; |
| 729 | } else { |
| 730 | size = sizeof(ev.value.bytes.data[0]); |
| 731 | dest = ev.value.bytes.data; |
| 732 | } |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 733 | break; |
| 734 | |
Yogesh Agrawal | 49a6137 | 2013-08-02 20:04:58 +0530 | [diff] [blame] | 735 | case SNDRV_CTL_ELEM_TYPE_IEC958: |
| 736 | size = sizeof(ev.value.iec958); |
| 737 | dest = &ev.value.iec958; |
| 738 | break; |
| 739 | |
Simon Wilson | 8813fe8 | 2013-06-24 15:40:34 -0700 | [diff] [blame] | 740 | default: |
| 741 | return -EINVAL; |
| 742 | } |
| 743 | |
| 744 | memcpy(dest, array, size * count); |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 745 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 746 | return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
Simon Wilson | da39e0b | 2012-11-09 15:16:47 -0800 | [diff] [blame] | 747 | } |
| 748 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 749 | int mixer_ctl_get_range_min(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 750 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 751 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 752 | return -EINVAL; |
| 753 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 754 | return ctl->info->value.integer.min; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 755 | } |
| 756 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 757 | int mixer_ctl_get_range_max(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 758 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 759 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_INTEGER)) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 760 | return -EINVAL; |
| 761 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 762 | return ctl->info->value.integer.max; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 763 | } |
| 764 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 765 | unsigned int mixer_ctl_get_num_enums(struct mixer_ctl *ctl) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 766 | { |
| 767 | if (!ctl) |
| 768 | return 0; |
| 769 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 770 | return ctl->info->value.enumerated.items; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 773 | const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl, |
| 774 | unsigned int enum_id) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 775 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 776 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || |
| 777 | (enum_id >= ctl->info->value.enumerated.items)) |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 778 | return NULL; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 779 | |
Simon Wilson | e44e30a | 2012-03-08 10:45:31 -0800 | [diff] [blame] | 780 | return (const char *)ctl->ename[enum_id]; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string) |
| 784 | { |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 785 | struct mixer_ctl_group *grp; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 786 | unsigned int i, num_enums; |
| 787 | struct snd_ctl_elem_value ev; |
| 788 | int ret; |
| 789 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 790 | if (!ctl || (ctl->info->type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) || !ctl->grp) |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 791 | return -EINVAL; |
| 792 | |
Akhil Karuturi | a972440 | 2020-01-17 13:53:14 -0800 | [diff] [blame] | 793 | grp = ctl->grp; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 794 | num_enums = ctl->info->value.enumerated.items; |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 795 | for (i = 0; i < num_enums; i++) { |
| 796 | if (!strcmp(string, ctl->ename[i])) { |
| 797 | memset(&ev, 0, sizeof(ev)); |
| 798 | ev.value.enumerated.item[0] = i; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 799 | ev.id.numid = ctl->info->id.numid; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 800 | ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev); |
Simon Wilson | edff708 | 2011-06-06 15:33:34 -0700 | [diff] [blame] | 801 | if (ret < 0) |
| 802 | return ret; |
| 803 | return 0; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | return -EINVAL; |
| 808 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 809 | |
| 810 | /** Subscribes for the mixer events. |
| 811 | * @param mixer A mixer handle. |
| 812 | * @param subscribe value indicating subscribe or unsubscribe for events |
| 813 | * @returns On success, zero. |
| 814 | * On failure, non-zero. |
| 815 | * @ingroup libtinyalsa-mixer |
| 816 | */ |
| 817 | int mixer_subscribe_events(struct mixer *mixer, int subscribe) |
| 818 | { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 819 | struct mixer_ctl_group *grp; |
| 820 | |
| 821 | if (mixer->hw_grp) { |
| 822 | grp = mixer->hw_grp; |
| 823 | if (!subscribe) |
| 824 | grp->event_cnt = 0; |
| 825 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | if (mixer->virt_grp) { |
| 830 | grp = mixer->virt_grp; |
| 831 | if (!subscribe) |
| 832 | grp->event_cnt = 0; |
| 833 | if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0) |
| 834 | return -1; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
| 839 | /** Wait for mixer events. |
| 840 | * @param mixer A mixer handle. |
| 841 | * @param timeout timout value |
| 842 | * @returns On success, 1. |
| 843 | * On failure, -errno. |
| 844 | * On timeout, 0 |
| 845 | * @ingroup libtinyalsa-mixer |
| 846 | */ |
| 847 | int mixer_wait_event(struct mixer *mixer, int timeout) |
| 848 | { |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 849 | struct pollfd pfd[2]; |
| 850 | struct mixer_ctl_group *grp; |
| 851 | int count = 0, num_fds = 0, i; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 852 | |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 853 | memset(pfd, 0, sizeof(struct pollfd) * 2); |
| 854 | |
| 855 | if (mixer->fd >= 0) |
| 856 | num_fds++; |
| 857 | |
| 858 | if (mixer->virt_grp) |
| 859 | num_fds++; |
| 860 | |
| 861 | |
| 862 | /* TODO wait events for virt fd */ |
| 863 | if (mixer->fd >= 0) { |
| 864 | pfd[count].fd = mixer->fd; |
| 865 | pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL; |
| 866 | count++; |
| 867 | } |
| 868 | |
| 869 | if (mixer->virt_grp) { |
| 870 | grp = mixer->virt_grp; |
| 871 | if (!grp->ops->get_poll_fd(grp->data, pfd, count)) { |
| 872 | pfd[count].events = POLLIN | POLLERR | POLLNVAL; |
| 873 | count++; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if (!count) |
| 878 | return 0; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 879 | |
| 880 | for (;;) { |
| 881 | int err; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 882 | err = poll(pfd, count, timeout); |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 883 | if (err < 0) |
| 884 | return -errno; |
| 885 | if (!err) |
| 886 | return 0; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 887 | for (i = 0; i < count; i++) { |
| 888 | if (pfd[i].revents & (POLLERR | POLLNVAL)) |
| 889 | return -EIO; |
| 890 | if (pfd[i].revents & (POLLIN | POLLOUT)) { |
| 891 | if ((i == 0) && mixer->fd >= 0) { |
| 892 | grp = mixer->hw_grp; |
| 893 | grp->event_cnt++; |
| 894 | } else { |
| 895 | grp = mixer->virt_grp; |
| 896 | grp->event_cnt++; |
| 897 | } |
| 898 | return 1; |
| 899 | } |
| 900 | } |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
| 904 | /** Consume a mixer event. |
| 905 | * If mixer_subscribe_events has been called, |
| 906 | * mixer_wait_event will identify when a control value has changed. |
| 907 | * This function will clear a single event from the mixer so that |
| 908 | * further events can be alerted. |
| 909 | * |
| 910 | * @param mixer A mixer handle. |
| 911 | * @returns 0 on success. -errno on failure. |
| 912 | * @ingroup libtinyalsa-mixer |
| 913 | */ |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 914 | int mixer_consume_event(struct mixer *mixer) |
| 915 | { |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 916 | struct snd_ctl_event ev; |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 917 | struct mixer_ctl_group *grp; |
| 918 | ssize_t count = 0; |
| 919 | |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 920 | // Exporting the actual event would require exposing snd_ctl_event |
| 921 | // via the header file, and all associated structs. |
| 922 | // The events generally tell you exactly which value changed, |
| 923 | // but reading values you're interested isn't hard and simplifies |
| 924 | // the interface greatly. |
Bhalchandra Gajare | 6b2537f | 2019-08-21 15:09:43 -0700 | [diff] [blame] | 925 | if (mixer->hw_grp) { |
| 926 | grp = mixer->hw_grp; |
| 927 | if (grp->event_cnt) { |
| 928 | grp->event_cnt--; |
| 929 | count = grp->ops->read_event(grp->data, &ev, sizeof(ev)); |
| 930 | return (count >= 0) ? 0 : -errno; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | if (mixer->virt_grp) { |
| 935 | grp = mixer->virt_grp; |
| 936 | if (grp->event_cnt) { |
| 937 | grp->event_cnt--; |
| 938 | count += grp->ops->read_event(grp->data, &ev, sizeof(ev)); |
| 939 | return (count >= 0) ? 0 : -errno; |
| 940 | } |
| 941 | } |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /** Read a mixer event. |
| 946 | * If mixer_subscribe_events has been called, |
| 947 | * mixer_wait_event will identify when a control value has changed. |
| 948 | * This function will read and clear a single event from the mixer |
| 949 | * so that further events can be alerted. |
| 950 | * |
| 951 | * @param mixer A mixer handle. |
| 952 | * @param ev snd_ctl_event pointer where event needs to be read |
| 953 | * @returns 0 on success. -errno on failure. |
| 954 | * @ingroup libtinyalsa-mixer |
| 955 | */ |
| 956 | int mixer_read_event(struct mixer *mixer, struct ctl_event *ev) |
| 957 | { |
| 958 | struct mixer_ctl_group *grp; |
| 959 | ssize_t count = 0; |
| 960 | |
| 961 | if (mixer->hw_grp) { |
| 962 | grp = mixer->hw_grp; |
| 963 | if (grp->event_cnt) { |
| 964 | grp->event_cnt--; |
| 965 | count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev)); |
| 966 | return (count >= 0) ? 0 : -errno; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | if (mixer->virt_grp) { |
| 971 | grp = mixer->virt_grp; |
| 972 | if (grp->event_cnt) { |
| 973 | grp->event_cnt--; |
| 974 | count = grp->ops->read_event(grp->data, (struct snd_ctl_event *)ev, sizeof(*ev)); |
| 975 | return (count >= 0) ? 0 : -errno; |
| 976 | } |
| 977 | } |
| 978 | return 0; |
David Li | e95e65a | 2020-12-09 02:11:18 +0000 | [diff] [blame] | 979 | } |