blob: 94aa019e71a6c86ac12f561726c3c2bcc50c3e01 [file] [log] [blame]
Simon Wilson79d39652011-05-25 13:44:23 -07001/* 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 Zhang7ed2ffb2016-04-22 17:59:40 -070031#include <stdint.h>
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -070032#include <stdbool.h>
Simon Wilson79d39652011-05-25 13:44:23 -070033#include <string.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include <errno.h>
37#include <ctype.h>
Richard Fitzgerald57a87742014-09-09 16:54:12 +010038#include <limits.h>
Dima Krasner696c4482016-03-05 19:50:02 +020039#include <time.h>
Pankaj Bharadiya010121a2017-01-11 08:57:06 +053040#include <poll.h>
Simon Wilson79d39652011-05-25 13:44:23 -070041
Simon Wilson6a52f2c2012-05-04 16:32:10 -070042#include <sys/ioctl.h>
43
Simon Wilson79d39652011-05-25 13:44:23 -070044#include <linux/ioctl.h>
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050045
46#ifndef __force
Simon Wilson79d39652011-05-25 13:44:23 -070047#define __force
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050048#endif
49
50#ifndef __bitwise
Simon Wilson79d39652011-05-25 13:44:23 -070051#define __bitwise
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050052#endif
53
54#ifndef __user
Simon Wilson79d39652011-05-25 13:44:23 -070055#define __user
Taylor Holberton4c5a11d2018-11-28 14:29:52 -050056#endif
57
Simon Wilson79d39652011-05-25 13:44:23 -070058#include <sound/asound.h>
59
Ricardo Biehl Pasquali04952ee2016-10-05 20:32:09 -030060#include <tinyalsa/mixer.h>
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -070061#include <tinyalsa/plugin.h>
62
63#include "mixer_io.h"
Simon Wilson79d39652011-05-25 13:44:23 -070064
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040065/** A mixer control.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080066 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040067 */
Simon Wilson79d39652011-05-25 13:44:23 -070068struct mixer_ctl {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040069 /** The mixer that the mixer control belongs to */
Simon Wilson79d39652011-05-25 13:44:23 -070070 struct mixer *mixer;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080071 /** Information on the control's value (i.e. type, number of values) */
Richard Fitzgerald899cece2014-09-09 17:03:21 +010072 struct snd_ctl_elem_info info;
Taylor Holbertonad4d7d72016-11-23 13:34:18 -080073 /** A list of string representations of enumerated values (only valid for enumerated controls) */
Simon Wilson79d39652011-05-25 13:44:23 -070074 char **ename;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -070075 /** Pointer to the group that the control belongs to */
76 struct mixer_ctl_group *grp;
77};
78
79struct 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 Wilson79d39652011-05-25 13:44:23 -070090};
91
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040092/** A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -080093 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040094 */
Simon Wilson79d39652011-05-25 13:44:23 -070095struct mixer {
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040096 /** File descriptor for the card */
Simon Wilson79d39652011-05-25 13:44:23 -070097 int fd;
Taylor Holberton7c8b20a2016-10-01 19:25:19 -040098 /** Card information */
Simon Wilsonec281392013-06-28 16:21:31 -070099 struct snd_ctl_card_info card_info;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700100 /* 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 Wilson79d39652011-05-25 13:44:23 -0700109};
110
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100111static 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 Gajaree7c627d2019-06-19 15:30:42 -0700123static 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 Holbertonab224a02020-06-03 09:47:50 -0400136 free(grp);
137
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700138 mixer->is_card_info_retrieved = false;
139}
140
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400141/** Closes a mixer returned by @ref mixer_open.
142 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800143 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400144 */
Simon Wilson79d39652011-05-25 13:44:23 -0700145void mixer_close(struct mixer *mixer)
146{
Simon Wilson193b1c32011-06-07 23:42:38 -0700147 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700148 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700149
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700150 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 Wilson79d39652011-05-25 13:44:23 -0700153
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700154#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 Wilson79d39652011-05-25 13:44:23 -0700159
Simon Wilson79d39652011-05-25 13:44:23 -0700160 free(mixer);
161
162 /* TODO: verify frees */
163}
164
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100165static 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 Gajaree7c627d2019-06-19 15:30:42 -0700177static int add_controls(struct mixer *mixer, struct mixer_ctl_group *grp)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100178{
179 struct snd_ctl_elem_list elist;
180 struct snd_ctl_elem_id *eid = NULL;
181 struct mixer_ctl *ctl;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700182 const unsigned int old_count = grp->count;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100183 unsigned int new_count;
184 unsigned int n;
185
186 memset(&elist, 0, sizeof(elist));
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700187 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100188 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 Gajaree7c627d2019-06-19 15:30:42 -0700196 ctl = mixer_realloc_z(grp->ctl, old_count, elist.count,
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100197 sizeof(struct mixer_ctl));
198 if (!ctl)
199 goto fail;
200
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700201 grp->ctl = ctl;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100202
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 Gajaree7c627d2019-06-19 15:30:42 -0700217 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100218 goto fail;
219
220 for (n = old_count; n < new_count; n++) {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700221 struct snd_ctl_elem_info *ei = &grp->ctl[n].info;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100222 ei->id.numid = eid[n - old_count].numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700223 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100224 goto fail_extend;
225 ctl[n].mixer = mixer;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700226 ctl[n].grp = grp;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100227 }
228
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700229 grp->count = new_count;
230 mixer->total_count += (new_count - old_count);
231
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100232 free(eid);
233 return 0;
234
235fail_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 Gajaree7c627d2019-06-19 15:30:42 -0700242 grp->count = n; /* keep controls we successfully added */
243 mixer->total_count += (n - old_count);
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100244 /* fall through... */
245fail:
246 free(eid);
247 return -1;
248}
249
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700250static 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
296err_card_info:
297 grp->ops->close(grp->data);
298
299err_open:
300 free(grp);
301 return ret;
302
303}
304
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400305/** Opens a mixer for a given card.
306 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500307 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800308 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400309 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700310struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700311{
Simon Wilson79d39652011-05-25 13:44:23 -0700312 struct mixer *mixer = NULL;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700313 int h_status, v_status = -1;
Simon Wilson79d39652011-05-25 13:44:23 -0700314
Simon Wilson79d39652011-05-25 13:44:23 -0700315 mixer = calloc(1, sizeof(*mixer));
316 if (!mixer)
317 goto fail;
318
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700319 h_status = mixer_grp_open(mixer, card, true);
Simon Wilson79d39652011-05-25 13:44:23 -0700320
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700321#ifdef TINYALSA_USES_PLUGINS
322 v_status = mixer_grp_open(mixer, card, false);
323#endif
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100324
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700325 /* both hw and virtual should fail for mixer_open to fail */
326 if (h_status < 0 && v_status < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700327 goto fail;
328
Simon Wilson79d39652011-05-25 13:44:23 -0700329 return mixer;
330
331fail:
Simon Wilson79d39652011-05-25 13:44:23 -0700332 if (mixer)
333 mixer_close(mixer);
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700334
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100335 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 */
355int mixer_add_new_ctls(struct mixer *mixer)
356{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700357 int rc1 = 0, rc2 = 0;
358
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100359 if (!mixer)
360 return 0;
361
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700362 /* 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 Wilson79d39652011-05-25 13:44:23 -0700378}
379
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500380/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800385const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700386{
387 return (const char *)mixer->card_info.name;
388}
389
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500390/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800395unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700396{
Simon Wilson193b1c32011-06-07 23:42:38 -0700397 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700398 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700399
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700400 return mixer->total_count;
Simon Wilson79d39652011-05-25 13:44:23 -0700401}
402
Taylor Holberton94c7c832016-12-01 18:35:24 -0800403/** 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 */
409unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
410{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700411 struct mixer_ctl_group *grp;
Taylor Holberton94c7c832016-12-01 18:35:24 -0800412 unsigned int n;
413 unsigned int count = 0;
414 struct mixer_ctl *ctl;
415
416 if (!mixer)
417 return 0;
418
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700419 if (mixer->h_grp) {
420 grp = mixer->h_grp;
421 ctl = grp->ctl;
Taylor Holberton94c7c832016-12-01 18:35:24 -0800422
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700423 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 Holberton94c7c832016-12-01 18:35:24 -0800437
438 return count;
439}
440
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530441/** 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 */
448int mixer_subscribe_events(struct mixer *mixer, int subscribe)
449{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700450 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 Bharadiya010121a2017-01-11 08:57:06 +0530456 }
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700457
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 Bharadiya010121a2017-01-11 08:57:06 +0530465 return 0;
466}
467
468/** Wait for mixer events.
469 * @param mixer A mixer handle.
470 * @param timeout timout value
Pankaj Bharadiya95c79d82017-01-11 11:28:02 +0530471 * @returns On success, 1.
472 * On failure, -errno.
473 * On timeout, 0
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530474 * @ingroup libtinyalsa-mixer
475 */
476int mixer_wait_event(struct mixer *mixer, int timeout)
477{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700478 struct pollfd *pfd;
479 struct mixer_ctl_group *grp;
Rohit kumardf855e82020-06-02 11:41:13 +0530480 int count = 0, num_fds = 0, i, ret = 0;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530481
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700482 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 kumardf855e82020-06-02 11:41:13 +0530490 pfd = (struct pollfd *)calloc(num_fds, sizeof(struct pollfd));
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700491 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 kumardf855e82020-06-02 11:41:13 +0530511 goto exit;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530512
513 for (;;) {
514 int err;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700515 err = poll(pfd, count, timeout);
Rohit kumardf855e82020-06-02 11:41:13 +0530516 if (err < 0) {
517 ret = -errno;
518 goto exit;
519 }
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530520 if (!err)
Rohit kumardf855e82020-06-02 11:41:13 +0530521 goto exit;
522
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700523 for (i = 0; i < count; i++) {
Rohit kumardf855e82020-06-02 11:41:13 +0530524 if (pfd[i].revents & (POLLERR | POLLNVAL)) {
525 ret = -EIO;
526 goto exit;
527 }
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700528 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 kumardf855e82020-06-02 11:41:13 +0530534 else {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700535 grp = mixer->v_grp;
536 grp->event_cnt++;
537 }
538#endif
Rohit kumardf855e82020-06-02 11:41:13 +0530539 ret = 1;
540 goto exit;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700541 }
542 }
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530543 }
Rohit kumardf855e82020-06-02 11:41:13 +0530544exit:
545 free(pfd);
546 return ret;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530547}
548
Andrew Chantbdc1fcf2018-02-05 15:16:41 -0800549/** 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 kumarf38405c2020-06-02 11:14:38 +0530559int mixer_consume_event(struct mixer *mixer)
560{
Andrew Chantbdc1fcf2018-02-05 15:16:41 -0800561 struct snd_ctl_event ev;
Rohit kumarf38405c2020-06-02 11:14:38 +0530562
563 return mixer_read_event(mixer, &ev);
564}
565
566int 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 Chantbdc1fcf2018-02-05 15:16:41 -0800590}
591
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700592static 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 Holbertonb7a28572016-11-19 23:45:00 -0500600/** Gets a mixer control handle, by the mixer control's id.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800601 * 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 */
607const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
608{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700609 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 Holbertona94295b2016-12-01 18:23:16 -0800625
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 Holbertonb7a28572016-11-19 23:45:00 -0500631 * @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 Wilson79d39652011-05-25 13:44:23 -0700636struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
637{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700638 unsigned int h_count;
Simon Wilson79d39652011-05-25 13:44:23 -0700639
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700640 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 Wilson79d39652011-05-25 13:44:23 -0700654 return NULL;
655}
656
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500657/** 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 Wilson79d39652011-05-25 13:44:23 -0700663struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
664{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200665 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
666}
667
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500668/** 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 Boisnard9e2c2402013-09-17 22:43:18 +0200676struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
677 const char *name,
678 unsigned int index)
679{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700680 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700681 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200682 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700683
Simon Wilson98c1f162011-06-07 16:12:32 -0700684 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700685 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700686
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700687 if (mixer->h_grp) {
688 grp = mixer->h_grp;
689 ctl = grp->ctl;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200690
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700691 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 Wilson79d39652011-05-25 13:44:23 -0700696
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700697#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 Wilson79d39652011-05-25 13:44:23 -0700708 return NULL;
709}
710
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500711/** 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 Wilson710df882013-06-28 16:17:50 -0700716void mixer_ctl_update(struct mixer_ctl *ctl)
717{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700718 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 Wilson710df882013-06-28 16:17:50 -0700725}
726
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530727/** 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 */
733int 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 Holbertonb7a28572016-11-19 23:45:00 -0500738/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800744unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100745{
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 Fitzgerald899cece2014-09-09 17:03:21 +0100752 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100753}
754
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500755/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800761const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700762{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800763 if (!ctl)
764 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700765
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100766 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700767}
768
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500769/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800775enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700776{
Simon Wilson193b1c32011-06-07 23:42:38 -0700777 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700778 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700779
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100780 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700781 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 Holbertonb7a28572016-11-19 23:45:00 -0500791/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800796const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700797{
Simon Wilson193b1c32011-06-07 23:42:38 -0700798 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700799 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700800
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100801 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700802 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 Holbertonb7a28572016-11-19 23:45:00 -0500812/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800817unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700818{
Simon Wilson193b1c32011-06-07 23:42:38 -0700819 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700820 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700821
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100822 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700823}
824
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800825static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700826{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200827 if ((percent > 100) || (percent < 0)) {
828 return -EINVAL;
829 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700830
Baptiste Robert4a484e12013-09-12 15:37:53 +0200831 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700832
Simon Wilson79d39652011-05-25 13:44:23 -0700833 return ei->value.integer.min + (range * percent) / 100;
834}
835
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800836static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700837{
838 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700839
Simon Wilson79d39652011-05-25 13:44:23 -0700840 if (range == 0)
841 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700842
Daniela-Marinela Bistrean9962e712019-04-12 00:49:20 +0300843 return ((value - ei->value.integer.min) * 100) / range;
Simon Wilson79d39652011-05-25 13:44:23 -0700844}
845
Taylor Holberton4e557192016-11-23 11:48:06 -0800846/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800853int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700854{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100855 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700856 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700857
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100858 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700859}
860
Taylor Holberton4e557192016-11-23 11:48:06 -0800861/** 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 Wilsond2cb5032011-06-04 00:57:17 -0700869int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700870{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100871 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700872 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700873
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100874 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700875}
876
Taylor Holberton4e557192016-11-23 11:48:06 -0800877/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800884int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700885{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700886 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700887 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700888 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700889
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100890 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700891 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700892
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700893 grp = ctl->grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700894 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100895 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700896 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -0700897 if (ret < 0)
898 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700899
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100900 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700901 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700902 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700903
904 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700905 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700906
907 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
908 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700909
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500910 case SNDRV_CTL_ELEM_TYPE_BYTES:
911 return ev.value.bytes.data[id];
912
Simon Wilson79d39652011-05-25 13:44:23 -0700913 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700914 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700915 }
916
917 return 0;
918}
919
Taylor Holberton4e557192016-11-23 11:48:06 -0800920/** 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 Holbertoncac43a22016-12-01 18:11:24 -0800932int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100933{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700934 struct mixer_ctl_group *grp;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100935 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530936 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700937 size_t size;
938 void *source;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530939 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100940
Charles Keepaxe40758b2017-09-07 16:38:52 +0100941 if (!ctl || !count || !array)
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530942 return -EINVAL;
943
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700944 grp = ctl->grp;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530945 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 Papastamosf51c05b2012-05-28 13:40:33 +0100954 return -EINVAL;
955
956 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100957 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100958
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100959 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700960 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
961 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700962 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K45b2d042014-08-18 15:39:36 +0530963 if (ret < 0)
964 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700965 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 K45b2d042014-08-18 15:39:36 +0530970 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530971 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530972 struct snd_ctl_tlv *tlv;
973 int ret;
974
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700975 if (count > SIZE_MAX - sizeof(*tlv))
976 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530977 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700978 if (!tlv)
979 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100980 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530981 tlv->length = count;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700982 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K45b2d042014-08-18 15:39:36 +0530983
984 source = tlv->tlv;
985 memcpy(array, source, count);
986
987 free(tlv);
988
989 return ret;
990 } else {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700991 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K45b2d042014-08-18 15:39:36 +0530992 if (ret < 0)
993 return ret;
994 size = sizeof(ev.value.bytes.data[0]);
995 source = ev.value.bytes.data;
996 break;
997 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700998
999 default:
1000 return -EINVAL;
1001 }
1002
1003 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001004
1005 return 0;
1006}
1007
Taylor Holberton4e557192016-11-23 11:48:06 -08001008/** 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 Wilson066c9f62011-06-05 18:23:05 -07001018int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -07001019{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001020 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -07001021 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -07001022 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -07001023
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001024 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -07001025 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -07001026
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001027 grp = ctl->grp;
Simon Wilson79d39652011-05-25 13:44:23 -07001028 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001029 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001030 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -07001031 if (ret < 0)
1032 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001033
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001034 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -07001035 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -07001036 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -07001037 break;
1038
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001039 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +02001040 if ((value < mixer_ctl_get_range_min(ctl)) ||
1041 (value > mixer_ctl_get_range_max(ctl))) {
1042 return -EINVAL;
1043 }
1044
Simon Wilsond2cb5032011-06-04 00:57:17 -07001045 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -07001046 break;
Simon Wilson79d39652011-05-25 13:44:23 -07001047
Simon Wilson066c9f62011-06-05 18:23:05 -07001048 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1049 ev.value.enumerated.item[id] = value;
1050 break;
1051
David.Coutherutce2b6342013-06-11 16:22:57 +02001052 case SNDRV_CTL_ELEM_TYPE_BYTES:
1053 ev.value.bytes.data[id] = value;
1054 break;
1055
Simon Wilson79d39652011-05-25 13:44:23 -07001056 default:
Simon Wilson193b1c32011-06-07 23:42:38 -07001057 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -07001058 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001059
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001060 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilson79d39652011-05-25 13:44:23 -07001061}
1062
Taylor Holberton4e557192016-11-23 11:48:06 -08001063/** 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 Wilson38f87f32012-10-23 15:05:23 -07001073int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001074{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001075 struct mixer_ctl_group *grp;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001076 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -07001077 size_t size;
1078 void *dest;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301079 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001080
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301081 if ((!ctl) || !count || !array)
1082 return -EINVAL;
1083
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001084 grp = ctl->grp;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301085 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 Papastamosf51c05b2012-05-28 13:40:33 +01001094 return -EINVAL;
1095
1096 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001097 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001098
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001099 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -07001100 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 K45b2d042014-08-18 15:39:36 +05301107 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +05301108 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +05301109 struct snd_ctl_tlv *tlv;
1110 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -07001111 if (count > SIZE_MAX - sizeof(*tlv))
1112 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +05301113 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -07001114 if (!tlv)
1115 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001116 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +05301117 tlv->length = count;
1118 memcpy(tlv->tlv, array, count);
1119
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001120 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K45b2d042014-08-18 15:39:36 +05301121 free(tlv);
1122
1123 return ret;
1124 } else {
1125 size = sizeof(ev.value.bytes.data[0]);
1126 dest = ev.value.bytes.data;
1127 }
Simon Wilson38f87f32012-10-23 15:05:23 -07001128 break;
1129
1130 default:
1131 return -EINVAL;
1132 }
1133
1134 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001135
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001136 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001137}
1138
Taylor Holberton4e557192016-11-23 11:48:06 -08001139/** 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 Holbertoncac43a22016-12-01 18:11:24 -08001147int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001148{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001149 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -07001150 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001151
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001152 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001153}
1154
Taylor Holberton4e557192016-11-23 11:48:06 -08001155/** 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 Holbertoncac43a22016-12-01 18:11:24 -08001163int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001164{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001165 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -07001166 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001167
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001168 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001169}
1170
Taylor Holberton4e557192016-11-23 11:48:06 -08001171/** 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 Holbertoncac43a22016-12-01 18:11:24 -08001176unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -07001177{
Simon Wilson193b1c32011-06-07 23:42:38 -07001178 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -07001179 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -07001180
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001181 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -07001182}
1183
David.Coutherut9b423962013-06-03 13:45:51 +02001184int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
1185{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001186 struct mixer_ctl_group *grp = ctl->grp;
David.Coutherut9b423962013-06-03 13:45:51 +02001187 struct snd_ctl_elem_info tmp;
1188 unsigned int m;
1189 char **enames;
1190
1191 if (ctl->ename) {
1192 return 0;
1193 }
1194
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001195 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +02001196 if (!enames)
1197 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001198 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +02001199 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001200 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +02001201 tmp.value.enumerated.item = m;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001202 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
David.Coutherut9b423962013-06-03 13:45:51 +02001203 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
1211fail:
1212 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001213 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +02001214 if (enames[m]) {
1215 free(enames[m]);
1216 }
1217 }
1218 free(enames);
1219 }
1220 return -1;
1221}
1222
Taylor Holberton4e557192016-11-23 11:48:06 -08001223/** 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 Wilsonb29ac1a2012-03-08 10:15:08 -08001229const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
1230 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -07001231{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001232 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001233 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +02001234 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001235 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -07001236
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001237 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -07001238}
1239
Taylor Holberton4e557192016-11-23 11:48:06 -08001240/** 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 Wilsonf0a20ee2011-06-05 21:18:52 -07001247int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
1248{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001249 struct mixer_ctl_group *grp;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001250 unsigned int i, num_enums;
1251 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -07001252 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001253
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001254 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +02001255 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -07001256 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001257
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001258 grp = ctl->grp;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001259 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001260 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 Fitzgerald899cece2014-09-09 17:03:21 +01001264 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001265 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -07001266 if (ret < 0)
1267 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001268 return 0;
1269 }
1270 }
1271
Simon Wilson193b1c32011-06-07 23:42:38 -07001272 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001273}