blob: c8480d7490abc222dfb18a16bb9e47baffc8e160 [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
136 mixer->is_card_info_retrieved = false;
137}
138
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400139/** Closes a mixer returned by @ref mixer_open.
140 * @param mixer A mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800141 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400142 */
Simon Wilson79d39652011-05-25 13:44:23 -0700143void mixer_close(struct mixer *mixer)
144{
Simon Wilson193b1c32011-06-07 23:42:38 -0700145 if (!mixer)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700146 return;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700147
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700148 if (mixer->fd >= 0 && mixer->h_grp)
149 mixer->h_grp->ops->close(mixer->h_grp->data);
150 mixer_grp_close(mixer, mixer->h_grp);
Simon Wilson79d39652011-05-25 13:44:23 -0700151
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700152#ifdef TINYALSA_USES_PLUGINS
153 if (mixer->v_grp)
154 mixer->v_grp->ops->close(mixer->v_grp->data);
155 mixer_grp_close(mixer, mixer->v_grp);
156#endif
Simon Wilson79d39652011-05-25 13:44:23 -0700157
Simon Wilson79d39652011-05-25 13:44:23 -0700158 free(mixer);
159
160 /* TODO: verify frees */
161}
162
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100163static void *mixer_realloc_z(void *ptr, size_t curnum, size_t newnum, size_t size)
164{
165 int8_t *newp;
166
167 newp = realloc(ptr, size * newnum);
168 if (!newp)
169 return NULL;
170
171 memset(newp + (curnum * size), 0, (newnum - curnum) * size);
172 return newp;
173}
174
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700175static int add_controls(struct mixer *mixer, struct mixer_ctl_group *grp)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100176{
177 struct snd_ctl_elem_list elist;
178 struct snd_ctl_elem_id *eid = NULL;
179 struct mixer_ctl *ctl;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700180 const unsigned int old_count = grp->count;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100181 unsigned int new_count;
182 unsigned int n;
183
184 memset(&elist, 0, sizeof(elist));
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700185 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100186 goto fail;
187
188 if (old_count == elist.count)
189 return 0; /* no new controls return unchanged */
190
191 if (old_count > elist.count)
192 return -1; /* driver has removed controls - this is bad */
193
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700194 ctl = mixer_realloc_z(grp->ctl, old_count, elist.count,
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100195 sizeof(struct mixer_ctl));
196 if (!ctl)
197 goto fail;
198
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700199 grp->ctl = ctl;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100200
201 /* ALSA drivers are not supposed to remove or re-order controls that
202 * have already been created so we know that any new controls must
203 * be after the ones we have already collected
204 */
205 new_count = elist.count;
206 elist.space = new_count - old_count; /* controls we haven't seen before */
207 elist.offset = old_count; /* first control we haven't seen */
208
209 eid = calloc(elist.space, sizeof(struct snd_ctl_elem_id));
210 if (!eid)
211 goto fail;
212
213 elist.pids = eid;
214
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700215 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_LIST, &elist) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100216 goto fail;
217
218 for (n = old_count; n < new_count; n++) {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700219 struct snd_ctl_elem_info *ei = &grp->ctl[n].info;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100220 ei->id.numid = eid[n - old_count].numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700221 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, ei) < 0)
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100222 goto fail_extend;
223 ctl[n].mixer = mixer;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700224 ctl[n].grp = grp;
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100225 }
226
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700227 grp->count = new_count;
228 mixer->total_count += (new_count - old_count);
229
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100230 free(eid);
231 return 0;
232
233fail_extend:
234 /* cleanup the control we failed on but leave the ones that were already
235 * added. Also no advantage to shrinking the resized memory block, we
236 * might want to extend the controls again later
237 */
238 mixer_cleanup_control(&ctl[n]);
239
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700240 grp->count = n; /* keep controls we successfully added */
241 mixer->total_count += (n - old_count);
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100242 /* fall through... */
243fail:
244 free(eid);
245 return -1;
246}
247
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700248static int mixer_grp_open(struct mixer *mixer, unsigned int card, bool is_hw)
249{
250 struct mixer_ctl_group *grp = NULL;
251 const struct mixer_ops *ops = NULL;
252 void *data = NULL;
253 int fd, ret;
254
255 grp = calloc(1, sizeof(*grp));
256 if (!grp)
257 return -ENOMEM;
258
259 if (is_hw) {
260 mixer->fd = -1;
261 fd = mixer_hw_open(card, &data, &ops);
262 if (fd < 0) {
263 ret = fd;
264 goto err_open;
265 }
266 mixer->fd = fd;
267 mixer->h_grp = grp;
268 }
269#ifdef TINYALSA_USES_PLUGINS
270 else {
271 ret = mixer_plugin_open(card, &data, &ops);
272 if (ret < 0)
273 goto err_open;
274 mixer->v_grp = grp;
275 }
276#endif
277 grp->ops = ops;
278 grp->data = data;
279
280 if (!mixer->is_card_info_retrieved) {
281 ret = grp->ops->ioctl(data, SNDRV_CTL_IOCTL_CARD_INFO,
282 &mixer->card_info);
283 if (ret < 0)
284 goto err_card_info;
285 mixer->is_card_info_retrieved = true;
286 }
287
288 ret = add_controls(mixer, grp);
289 if (ret < 0)
290 goto err_card_info;
291
292 return 0;
293
294err_card_info:
295 grp->ops->close(grp->data);
296
297err_open:
298 free(grp);
299 return ret;
300
301}
302
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400303/** Opens a mixer for a given card.
304 * @param card The card to open the mixer for.
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500305 * @returns An initialized mixer handle.
Taylor Holberton8ae5d112016-11-19 10:35:25 -0800306 * @ingroup libtinyalsa-mixer
Taylor Holberton7c8b20a2016-10-01 19:25:19 -0400307 */
Simon Wilson1bd580f2011-06-02 15:58:41 -0700308struct mixer *mixer_open(unsigned int card)
Simon Wilson79d39652011-05-25 13:44:23 -0700309{
Simon Wilson79d39652011-05-25 13:44:23 -0700310 struct mixer *mixer = NULL;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700311 int h_status, v_status = -1;
Simon Wilson79d39652011-05-25 13:44:23 -0700312
Simon Wilson79d39652011-05-25 13:44:23 -0700313 mixer = calloc(1, sizeof(*mixer));
314 if (!mixer)
315 goto fail;
316
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700317 h_status = mixer_grp_open(mixer, card, true);
Simon Wilson79d39652011-05-25 13:44:23 -0700318
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700319#ifdef TINYALSA_USES_PLUGINS
320 v_status = mixer_grp_open(mixer, card, false);
321#endif
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100322
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700323 /* both hw and virtual should fail for mixer_open to fail */
324 if (h_status < 0 && v_status < 0)
Simon Wilson79d39652011-05-25 13:44:23 -0700325 goto fail;
326
Simon Wilson79d39652011-05-25 13:44:23 -0700327 return mixer;
328
329fail:
Simon Wilson79d39652011-05-25 13:44:23 -0700330 if (mixer)
331 mixer_close(mixer);
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700332
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100333 return NULL;
334}
335
336/** Some controls may not be present at boot time, e.g. controls from runtime
337 * loadable DSP firmware. This function adds any new controls that have appeared
338 * since mixer_open() or the last call to this function. This assumes a well-
339 * behaved codec driver that does not delete controls that already exists, so
340 * any added controls must be after the last one we already saw. Scanning only
341 * the new controls is much faster than calling mixer_close() then mixer_open()
342 * to re-scan all controls.
343 *
344 * NOTE: this invalidates any struct mixer_ctl pointers previously obtained
345 * from mixer_get_ctl() and mixer_get_ctl_by_name(). Either refresh all your
346 * stored pointers after calling mixer_update_ctls(), or (better) do not
347 * store struct mixer_ctl pointers, instead lookup the control by name or
348 * id only when you are about to use it. The overhead of lookup by id
349 * using mixer_get_ctl() is negligible.
350 * @param mixer An initialized mixer handle.
351 * @returns 0 on success, -1 on failure
352 */
353int mixer_add_new_ctls(struct mixer *mixer)
354{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700355 int rc1 = 0, rc2 = 0;
356
Richard Fitzgeraldfd329032014-09-09 17:14:09 +0100357 if (!mixer)
358 return 0;
359
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700360 /* add the h_grp controls */
361 if (mixer->h_grp)
362 rc1 = add_controls(mixer, mixer->h_grp);
363
364#ifdef TINYALSA_USES_PLUGINS
365 /* add the v_grp controls */
366 if (mixer->v_grp)
367 rc2 = add_controls(mixer, mixer->v_grp);
368#endif
369
370 if (rc1 < 0)
371 return rc1;
372 if (rc2 < 0)
373 return rc2;
374
375 return 0;
Simon Wilson79d39652011-05-25 13:44:23 -0700376}
377
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500378/** Gets the name of the mixer's card.
379 * @param mixer An initialized mixer handle.
380 * @returns The name of the mixer's card.
381 * @ingroup libtinyalsa-mixer
382 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800383const char *mixer_get_name(const struct mixer *mixer)
Simon Wilsonec281392013-06-28 16:21:31 -0700384{
385 return (const char *)mixer->card_info.name;
386}
387
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500388/** Gets the number of mixer controls for a given mixer.
389 * @param mixer An initialized mixer handle.
390 * @returns The number of mixer controls for the given mixer.
391 * @ingroup libtinyalsa-mixer
392 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800393unsigned int mixer_get_num_ctls(const struct mixer *mixer)
Simon Wilson79d39652011-05-25 13:44:23 -0700394{
Simon Wilson193b1c32011-06-07 23:42:38 -0700395 if (!mixer)
Simon Wilson98c1f162011-06-07 16:12:32 -0700396 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700397
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700398 return mixer->total_count;
Simon Wilson79d39652011-05-25 13:44:23 -0700399}
400
Taylor Holberton94c7c832016-12-01 18:35:24 -0800401/** Gets the number of mixer controls, that go by a specified name, for a given mixer.
402 * @param mixer An initialized mixer handle.
403 * @param name The name of the mixer control
404 * @returns The number of mixer controls, specified by @p name, for the given mixer.
405 * @ingroup libtinyalsa-mixer
406 */
407unsigned int mixer_get_num_ctls_by_name(const struct mixer *mixer, const char *name)
408{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700409 struct mixer_ctl_group *grp;
Taylor Holberton94c7c832016-12-01 18:35:24 -0800410 unsigned int n;
411 unsigned int count = 0;
412 struct mixer_ctl *ctl;
413
414 if (!mixer)
415 return 0;
416
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700417 if (mixer->h_grp) {
418 grp = mixer->h_grp;
419 ctl = grp->ctl;
Taylor Holberton94c7c832016-12-01 18:35:24 -0800420
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700421 for (n = 0; n < grp->count; n++)
422 if (!strcmp(name, (char*) ctl[n].info.id.name))
423 count++;
424 }
425#ifdef TINYALSA_USES_PLUGINS
426 if (mixer->v_grp) {
427 grp = mixer->v_grp;
428 ctl = grp->ctl;
429
430 for (n = 0; n < grp->count; n++)
431 if (!strcmp(name, (char*) ctl[n].info.id.name))
432 count++;
433 }
434#endif
Taylor Holberton94c7c832016-12-01 18:35:24 -0800435
436 return count;
437}
438
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530439/** Subscribes for the mixer events.
440 * @param mixer A mixer handle.
441 * @param subscribe value indicating subscribe or unsubscribe for events
442 * @returns On success, zero.
443 * On failure, non-zero.
444 * @ingroup libtinyalsa-mixer
445 */
446int mixer_subscribe_events(struct mixer *mixer, int subscribe)
447{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700448 struct mixer_ctl_group *grp;
449
450 if (mixer->h_grp) {
451 grp = mixer->h_grp;
452 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
453 return -1;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530454 }
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700455
456#ifdef TINYALSA_USES_PLUGINS
457 if (mixer->v_grp) {
458 grp = mixer->v_grp;
459 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_SUBSCRIBE_EVENTS, &subscribe) < 0)
460 return -1;
461 }
462#endif
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530463 return 0;
464}
465
466/** Wait for mixer events.
467 * @param mixer A mixer handle.
468 * @param timeout timout value
Pankaj Bharadiya95c79d82017-01-11 11:28:02 +0530469 * @returns On success, 1.
470 * On failure, -errno.
471 * On timeout, 0
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530472 * @ingroup libtinyalsa-mixer
473 */
474int mixer_wait_event(struct mixer *mixer, int timeout)
475{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700476 struct pollfd *pfd;
477 struct mixer_ctl_group *grp;
Rohit kumardf855e82020-06-02 11:41:13 +0530478 int count = 0, num_fds = 0, i, ret = 0;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530479
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700480 if (mixer->fd >= 0)
481 num_fds++;
482
483#ifdef TINYALSA_USES_PLUGINS
484 if (mixer->v_grp)
485 num_fds++;
486#endif
487
Rohit kumardf855e82020-06-02 11:41:13 +0530488 pfd = (struct pollfd *)calloc(num_fds, sizeof(struct pollfd));
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700489 if (!pfd)
490 return -ENOMEM;
491
492 if (mixer->fd >= 0) {
493 pfd[count].fd = mixer->fd;
494 pfd[count].events = POLLIN | POLLOUT | POLLERR | POLLNVAL;
495 count++;
496 }
497
498#ifdef TINYALSA_USES_PLUGINS
499 if (mixer->v_grp) {
500 grp = mixer->v_grp;
501 if (!grp->ops->get_poll_fd(grp->data, pfd, count)) {
502 pfd[count].events = POLLIN | POLLERR | POLLNVAL;
503 count++;
504 }
505 }
506#endif
507
508 if (!count)
Rohit kumardf855e82020-06-02 11:41:13 +0530509 goto exit;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530510
511 for (;;) {
512 int err;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700513 err = poll(pfd, count, timeout);
Rohit kumardf855e82020-06-02 11:41:13 +0530514 if (err < 0) {
515 ret = -errno;
516 goto exit;
517 }
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530518 if (!err)
Rohit kumardf855e82020-06-02 11:41:13 +0530519 goto exit;
520
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700521 for (i = 0; i < count; i++) {
Rohit kumardf855e82020-06-02 11:41:13 +0530522 if (pfd[i].revents & (POLLERR | POLLNVAL)) {
523 ret = -EIO;
524 goto exit;
525 }
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700526 if (pfd[i].revents & (POLLIN | POLLOUT)) {
527 if ((i == 0) && mixer->fd >= 0) {
528 grp = mixer->h_grp;
529 grp->event_cnt++;
530 }
531#ifdef TINYALSA_USES_PLUGINS
Rohit kumardf855e82020-06-02 11:41:13 +0530532 else {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700533 grp = mixer->v_grp;
534 grp->event_cnt++;
535 }
536#endif
Rohit kumardf855e82020-06-02 11:41:13 +0530537 ret = 1;
538 goto exit;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700539 }
540 }
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530541 }
Rohit kumardf855e82020-06-02 11:41:13 +0530542exit:
543 free(pfd);
544 return ret;
Pankaj Bharadiya010121a2017-01-11 08:57:06 +0530545}
546
Andrew Chantbdc1fcf2018-02-05 15:16:41 -0800547/** Consume a mixer event.
548 * If mixer_subscribe_events has been called,
549 * mixer_wait_event will identify when a control value has changed.
550 * This function will clear a single event from the mixer so that
551 * further events can be alerted.
552 *
553 * @param mixer A mixer handle.
554 * @returns 0 on success. -errno on failure.
555 * @ingroup libtinyalsa-mixer
556 */
Rohit kumarf38405c2020-06-02 11:14:38 +0530557int mixer_consume_event(struct mixer *mixer)
558{
Andrew Chantbdc1fcf2018-02-05 15:16:41 -0800559 struct snd_ctl_event ev;
Rohit kumarf38405c2020-06-02 11:14:38 +0530560
561 return mixer_read_event(mixer, &ev);
562}
563
564int mixer_read_event(struct mixer *mixer, struct snd_ctl_event *ev)
565{
566 struct mixer_ctl_group *grp;
567 ssize_t count = 0;
568
569 if (mixer->h_grp) {
570 grp = mixer->h_grp;
571 if (grp->event_cnt) {
572 grp->event_cnt--;
573 count = grp->ops->read_event(grp->data, ev, sizeof(*ev));
574 return (count >= 0) ? 0 : -errno;
575 }
576 }
577#ifdef TINYALSA_USES_PLUGINS
578 if (mixer->v_grp) {
579 grp = mixer->v_grp;
580 if (grp->event_cnt) {
581 grp->event_cnt--;
582 count = grp->ops->read_event(grp->data, ev, sizeof(*ev));
583 return (count >= 0) ? 0 : -errno;
584 }
585 }
586#endif
587 return 0;
Andrew Chantbdc1fcf2018-02-05 15:16:41 -0800588}
589
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700590static unsigned int mixer_grp_get_count(struct mixer_ctl_group *grp)
591{
592 if (!grp)
593 return 0;
594
595 return grp->count;
596}
597
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500598/** Gets a mixer control handle, by the mixer control's id.
Taylor Holbertona94295b2016-12-01 18:23:16 -0800599 * For non-const access, see @ref mixer_get_ctl
600 * @param mixer An initialized mixer handle.
601 * @param id The control's id in the given mixer.
602 * @returns A handle to the mixer control.
603 * @ingroup libtinyalsa-mixer
604 */
605const struct mixer_ctl *mixer_get_ctl_const(const struct mixer *mixer, unsigned int id)
606{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700607 unsigned int h_count;
608
609 if (!mixer || (id >= mixer->total_count))
610 return NULL;
611
612 h_count = mixer_grp_get_count(mixer->h_grp);
613
614 if (id < h_count)
615 return mixer->h_grp->ctl + id;
616#ifdef TINYALSA_USES_PLUGINS
617 else {
618 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
619 if ((id - h_count) < v_count)
620 return mixer->v_grp->ctl + (id - h_count);
621 }
622#endif
Taylor Holbertona94295b2016-12-01 18:23:16 -0800623
624 return NULL;
625}
626
627/** Gets a mixer control handle, by the mixer control's id.
628 * For const access, see @ref mixer_get_ctl_const
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500629 * @param mixer An initialized mixer handle.
630 * @param id The control's id in the given mixer.
631 * @returns A handle to the mixer control.
632 * @ingroup libtinyalsa-mixer
633 */
Simon Wilson79d39652011-05-25 13:44:23 -0700634struct mixer_ctl *mixer_get_ctl(struct mixer *mixer, unsigned int id)
635{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700636 unsigned int h_count;
Simon Wilson79d39652011-05-25 13:44:23 -0700637
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700638 if (!mixer || (id >= mixer->total_count))
639 return NULL;
640
641 h_count = mixer_grp_get_count(mixer->h_grp);
642
643 if (id < h_count)
644 return mixer->h_grp->ctl + id;
645#ifdef TINYALSA_USES_PLUGINS
646 else {
647 unsigned int v_count = mixer_grp_get_count(mixer->v_grp);
648 if ((id - h_count) < v_count)
649 return mixer->v_grp->ctl + (id - h_count);
650 }
651#endif
Simon Wilson79d39652011-05-25 13:44:23 -0700652 return NULL;
653}
654
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500655/** Gets the first instance of mixer control handle, by the mixer control's name.
656 * @param mixer An initialized mixer handle.
657 * @param name The control's name in the given mixer.
658 * @returns A handle to the mixer control.
659 * @ingroup libtinyalsa-mixer
660 */
Simon Wilson79d39652011-05-25 13:44:23 -0700661struct mixer_ctl *mixer_get_ctl_by_name(struct mixer *mixer, const char *name)
662{
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200663 return mixer_get_ctl_by_name_and_index(mixer, name, 0);
664}
665
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500666/** Gets an instance of mixer control handle, by the mixer control's name and index.
667 * For instance, if two controls have the name of 'Volume', then and index of 1 would return the second control.
668 * @param mixer An initialized mixer handle.
669 * @param name The control's name in the given mixer.
670 * @param index The control's index.
671 * @returns A handle to the mixer control.
672 * @ingroup libtinyalsa-mixer
673 */
Frédéric Boisnard9e2c2402013-09-17 22:43:18 +0200674struct mixer_ctl *mixer_get_ctl_by_name_and_index(struct mixer *mixer,
675 const char *name,
676 unsigned int index)
677{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700678 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700679 unsigned int n;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200680 struct mixer_ctl *ctl;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700681
Simon Wilson98c1f162011-06-07 16:12:32 -0700682 if (!mixer)
Simon Wilson193b1c32011-06-07 23:42:38 -0700683 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700684
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700685 if (mixer->h_grp) {
686 grp = mixer->h_grp;
687 ctl = grp->ctl;
Svyatoslav Mishync7328362016-01-24 19:43:38 +0200688
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700689 for (n = 0; n < grp->count; n++)
690 if (!strcmp(name, (char*) ctl[n].info.id.name))
691 if (index-- == 0)
692 return ctl + n;
693 }
Simon Wilson79d39652011-05-25 13:44:23 -0700694
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700695#ifdef TINYALSA_USES_PLUGINS
696 if (mixer->v_grp) {
697 grp = mixer->v_grp;
698 ctl = grp->ctl;
699
700 for (n = 0; n < grp->count; n++)
701 if (!strcmp(name, (char*) ctl[n].info.id.name))
702 if (index-- == 0)
703 return ctl + n;
704 }
705#endif
Simon Wilson79d39652011-05-25 13:44:23 -0700706 return NULL;
707}
708
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500709/** Updates the control's info.
710 * This is useful for a program that may be idle for a period of time.
711 * @param ctl An initialized control handle.
712 * @ingroup libtinyalsa-mixer
713 */
Simon Wilson710df882013-06-28 16:17:50 -0700714void mixer_ctl_update(struct mixer_ctl *ctl)
715{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700716 struct mixer_ctl_group *grp;
717
718 if (!ctl)
719 return;
720
721 grp = ctl->grp;
722 grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &ctl->info);
Simon Wilson710df882013-06-28 16:17:50 -0700723}
724
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530725/** Checks the control for TLV Read/Write access.
726 * @param ctl An initialized control handle.
727 * @returns On success, non-zero.
728 * On failure, zero.
729 * @ingroup libtinyalsa-mixer
730 */
731int mixer_ctl_is_access_tlv_rw(const struct mixer_ctl *ctl)
732{
733 return (ctl->info.access & SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE);
734}
735
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500736/** Gets the control's ID.
737 * @param ctl An initialized control handle.
738 * @returns On success, the control's ID is returned.
739 * On error, UINT_MAX is returned instead.
740 * @ingroup libtinyalsa-mixer
741 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800742unsigned int mixer_ctl_get_id(const struct mixer_ctl *ctl)
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100743{
744 if (!ctl)
745 return UINT_MAX;
746
747 /* numid values start at 1, return a 0-base value that
748 * can be passed to mixer_get_ctl()
749 */
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100750 return ctl->info.id.numid - 1;
Richard Fitzgerald57a87742014-09-09 16:54:12 +0100751}
752
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500753/** Gets the name of the control.
754 * @param ctl An initialized control handle.
755 * @returns On success, the name of the control.
756 * On error, NULL.
757 * @ingroup libtinyalsa-mixer
758 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800759const char *mixer_ctl_get_name(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700760{
Simon Wilsonb29ac1a2012-03-08 10:15:08 -0800761 if (!ctl)
762 return NULL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700763
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100764 return (const char *)ctl->info.id.name;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700765}
766
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500767/** Gets the value type of the control.
768 * @param ctl An initialized control handle
769 * @returns On success, the type of mixer control.
770 * On failure, it returns @ref MIXER_CTL_TYPE_UNKNOWN
771 * @ingroup libtinyalsa-mixer
772 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800773enum mixer_ctl_type mixer_ctl_get_type(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700774{
Simon Wilson193b1c32011-06-07 23:42:38 -0700775 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700776 return MIXER_CTL_TYPE_UNKNOWN;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700777
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100778 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700779 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return MIXER_CTL_TYPE_BOOL;
780 case SNDRV_CTL_ELEM_TYPE_INTEGER: return MIXER_CTL_TYPE_INT;
781 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return MIXER_CTL_TYPE_ENUM;
782 case SNDRV_CTL_ELEM_TYPE_BYTES: return MIXER_CTL_TYPE_BYTE;
783 case SNDRV_CTL_ELEM_TYPE_IEC958: return MIXER_CTL_TYPE_IEC958;
784 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return MIXER_CTL_TYPE_INT64;
785 default: return MIXER_CTL_TYPE_UNKNOWN;
786 };
787}
788
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500789/** Gets the string that describes the value type of the control.
790 * @param ctl An initialized control handle
791 * @returns On success, a string describing type of mixer control.
792 * @ingroup libtinyalsa-mixer
793 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800794const char *mixer_ctl_get_type_string(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700795{
Simon Wilson193b1c32011-06-07 23:42:38 -0700796 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700797 return "";
Simon Wilsond2cb5032011-06-04 00:57:17 -0700798
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100799 switch (ctl->info.type) {
Simon Wilsond2cb5032011-06-04 00:57:17 -0700800 case SNDRV_CTL_ELEM_TYPE_BOOLEAN: return "BOOL";
801 case SNDRV_CTL_ELEM_TYPE_INTEGER: return "INT";
802 case SNDRV_CTL_ELEM_TYPE_ENUMERATED: return "ENUM";
803 case SNDRV_CTL_ELEM_TYPE_BYTES: return "BYTE";
804 case SNDRV_CTL_ELEM_TYPE_IEC958: return "IEC958";
805 case SNDRV_CTL_ELEM_TYPE_INTEGER64: return "INT64";
806 default: return "Unknown";
807 };
808}
809
Taylor Holbertonb7a28572016-11-19 23:45:00 -0500810/** Gets the number of values in the control.
811 * @param ctl An initialized control handle
812 * @returns The number of values in the mixer control
813 * @ingroup libtinyalsa-mixer
814 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800815unsigned int mixer_ctl_get_num_values(const struct mixer_ctl *ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700816{
Simon Wilson193b1c32011-06-07 23:42:38 -0700817 if (!ctl)
Simon Wilsond2cb5032011-06-04 00:57:17 -0700818 return 0;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700819
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100820 return ctl->info.count;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700821}
822
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800823static int percent_to_int(const struct snd_ctl_elem_info *ei, int percent)
Simon Wilson79d39652011-05-25 13:44:23 -0700824{
Baptiste Robert4a484e12013-09-12 15:37:53 +0200825 if ((percent > 100) || (percent < 0)) {
826 return -EINVAL;
827 }
Simon Wilson98c1f162011-06-07 16:12:32 -0700828
Baptiste Robert4a484e12013-09-12 15:37:53 +0200829 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700830
Simon Wilson79d39652011-05-25 13:44:23 -0700831 return ei->value.integer.min + (range * percent) / 100;
832}
833
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800834static int int_to_percent(const struct snd_ctl_elem_info *ei, int value)
Simon Wilson79d39652011-05-25 13:44:23 -0700835{
836 int range = (ei->value.integer.max - ei->value.integer.min);
Simon Wilson98c1f162011-06-07 16:12:32 -0700837
Simon Wilson79d39652011-05-25 13:44:23 -0700838 if (range == 0)
839 return 0;
Simon Wilson98c1f162011-06-07 16:12:32 -0700840
Daniela-Marinela Bistrean9962e712019-04-12 00:49:20 +0300841 return ((value - ei->value.integer.min) * 100) / range;
Simon Wilson79d39652011-05-25 13:44:23 -0700842}
843
Taylor Holberton4e557192016-11-23 11:48:06 -0800844/** Gets a percentage representation of a specified control value.
845 * @param ctl An initialized control handle.
846 * @param id The index of the value within the control.
847 * @returns On success, the percentage representation of the control value.
848 * On failure, -EINVAL is returned.
849 * @ingroup libtinyalsa-mixer
850 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800851int mixer_ctl_get_percent(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilson79d39652011-05-25 13:44:23 -0700852{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100853 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700854 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700855
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100856 return int_to_percent(&ctl->info, mixer_ctl_get_value(ctl, id));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700857}
858
Taylor Holberton4e557192016-11-23 11:48:06 -0800859/** Sets the value of a control by percent, specified by the value index.
860 * @param ctl An initialized control handle.
861 * @param id The index of the value to set
862 * @param percent A percentage value between 0 and 100.
863 * @returns On success, zero is returned.
864 * On failure, non-zero is returned.
865 * @ingroup libtinyalsa-mixer
866 */
Simon Wilsond2cb5032011-06-04 00:57:17 -0700867int mixer_ctl_set_percent(struct mixer_ctl *ctl, unsigned int id, int percent)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700868{
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100869 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -0700870 return -EINVAL;
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700871
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100872 return mixer_ctl_set_value(ctl, id, percent_to_int(&ctl->info, percent));
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700873}
874
Taylor Holberton4e557192016-11-23 11:48:06 -0800875/** Gets the value of a control.
876 * @param ctl An initialized control handle.
877 * @param id The index of the control value.
878 * @returns On success, the specified value is returned.
879 * On failure, -EINVAL is returned.
880 * @ingroup libtinyalsa-mixer
881 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800882int mixer_ctl_get_value(const struct mixer_ctl *ctl, unsigned int id)
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700883{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700884 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700885 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -0700886 int ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700887
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100888 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -0700889 return -EINVAL;
Simon Wilsond2cb5032011-06-04 00:57:17 -0700890
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700891 grp = ctl->grp;
Simon Wilson79d39652011-05-25 13:44:23 -0700892 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100893 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700894 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -0700895 if (ret < 0)
896 return ret;
Simon Wilson79d39652011-05-25 13:44:23 -0700897
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100898 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -0700899 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700900 return !!ev.value.integer.value[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700901
902 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Simon Wilsond2cb5032011-06-04 00:57:17 -0700903 return ev.value.integer.value[id];
Simon Wilson066c9f62011-06-05 18:23:05 -0700904
905 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
906 return ev.value.enumerated.item[id];
Simon Wilsona1bb1e02011-05-26 18:22:00 -0700907
Pierre-Louis Bossart52510162011-10-24 15:00:17 -0500908 case SNDRV_CTL_ELEM_TYPE_BYTES:
909 return ev.value.bytes.data[id];
910
Simon Wilson79d39652011-05-25 13:44:23 -0700911 default:
Simon Wilson193b1c32011-06-07 23:42:38 -0700912 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -0700913 }
914
915 return 0;
916}
917
Taylor Holberton4e557192016-11-23 11:48:06 -0800918/** Gets the contents of a control's value array.
919 * @param ctl An initialized control handle.
920 * @param array A pointer to write the array data to.
921 * The size of this array must be equal to the number of items in the array
922 * multiplied by the size of each item.
923 * @param count The number of items in the array.
924 * This parameter must match the number of items in the control.
925 * The number of items in the control may be accessed via @ref mixer_ctl_get_num_values
926 * @returns On success, zero.
927 * On failure, non-zero.
928 * @ingroup libtinyalsa-mixer
929 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -0800930int mixer_ctl_get_array(const struct mixer_ctl *ctl, void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100931{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700932 struct mixer_ctl_group *grp;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100933 struct snd_ctl_elem_value ev;
Mythri P K45b2d042014-08-18 15:39:36 +0530934 int ret = 0;
Simon Wilson38f87f32012-10-23 15:05:23 -0700935 size_t size;
936 void *source;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530937 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100938
Charles Keepaxe40758b2017-09-07 16:38:52 +0100939 if (!ctl || !count || !array)
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530940 return -EINVAL;
941
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700942 grp = ctl->grp;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +0530943 total_count = ctl->info.count;
944
945 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
946 (mixer_ctl_is_access_tlv_rw(ctl))) {
947 /* Additional two words is for the TLV header */
948 total_count += TLV_HEADER_SIZE;
949 }
950
951 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100952 return -EINVAL;
953
954 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100955 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +0100956
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100957 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -0700958 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
959 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700960 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K45b2d042014-08-18 15:39:36 +0530961 if (ret < 0)
962 return ret;
Simon Wilson38f87f32012-10-23 15:05:23 -0700963 size = sizeof(ev.value.integer.value[0]);
964 source = ev.value.integer.value;
965 break;
966
967 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +0530968 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +0530969 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +0530970 struct snd_ctl_tlv *tlv;
971 int ret;
972
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700973 if (count > SIZE_MAX - sizeof(*tlv))
974 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +0530975 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -0700976 if (!tlv)
977 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +0100978 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +0530979 tlv->length = count;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700980 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_READ, tlv);
Mythri P K45b2d042014-08-18 15:39:36 +0530981
982 source = tlv->tlv;
983 memcpy(array, source, count);
984
985 free(tlv);
986
987 return ret;
988 } else {
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -0700989 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Mythri P K45b2d042014-08-18 15:39:36 +0530990 if (ret < 0)
991 return ret;
992 size = sizeof(ev.value.bytes.data[0]);
993 source = ev.value.bytes.data;
994 break;
995 }
Simon Wilson38f87f32012-10-23 15:05:23 -0700996
997 default:
998 return -EINVAL;
999 }
1000
1001 memcpy(array, source, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001002
1003 return 0;
1004}
1005
Taylor Holberton4e557192016-11-23 11:48:06 -08001006/** Sets the value of a control, specified by the value index.
1007 * @param ctl An initialized control handle.
1008 * @param id The index of the value within the control.
1009 * @param value The value to set.
1010 * This must be in a range specified by @ref mixer_ctl_get_range_min
1011 * and @ref mixer_ctl_get_range_max.
1012 * @returns On success, zero is returned.
1013 * On failure, non-zero is returned.
1014 * @ingroup libtinyalsa-mixer
1015 */
Simon Wilson066c9f62011-06-05 18:23:05 -07001016int mixer_ctl_set_value(struct mixer_ctl *ctl, unsigned int id, int value)
Simon Wilson79d39652011-05-25 13:44:23 -07001017{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001018 struct mixer_ctl_group *grp;
Simon Wilson79d39652011-05-25 13:44:23 -07001019 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -07001020 int ret;
Simon Wilsond2cb5032011-06-04 00:57:17 -07001021
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001022 if (!ctl || (id >= ctl->info.count))
Simon Wilson193b1c32011-06-07 23:42:38 -07001023 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -07001024
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001025 grp = ctl->grp;
Simon Wilson79d39652011-05-25 13:44:23 -07001026 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001027 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001028 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_READ, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -07001029 if (ret < 0)
1030 return ret;
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001031
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001032 switch (ctl->info.type) {
Simon Wilson79d39652011-05-25 13:44:23 -07001033 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
Simon Wilsond2cb5032011-06-04 00:57:17 -07001034 ev.value.integer.value[id] = !!value;
Simon Wilson79d39652011-05-25 13:44:23 -07001035 break;
1036
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001037 case SNDRV_CTL_ELEM_TYPE_INTEGER:
Baptiste Robert6cd7d5f2013-08-07 11:32:45 +02001038 if ((value < mixer_ctl_get_range_min(ctl)) ||
1039 (value > mixer_ctl_get_range_max(ctl))) {
1040 return -EINVAL;
1041 }
1042
Simon Wilsond2cb5032011-06-04 00:57:17 -07001043 ev.value.integer.value[id] = value;
Simon Wilson79d39652011-05-25 13:44:23 -07001044 break;
Simon Wilson79d39652011-05-25 13:44:23 -07001045
Simon Wilson066c9f62011-06-05 18:23:05 -07001046 case SNDRV_CTL_ELEM_TYPE_ENUMERATED:
1047 ev.value.enumerated.item[id] = value;
1048 break;
1049
David.Coutherutce2b6342013-06-11 16:22:57 +02001050 case SNDRV_CTL_ELEM_TYPE_BYTES:
1051 ev.value.bytes.data[id] = value;
1052 break;
1053
Simon Wilson79d39652011-05-25 13:44:23 -07001054 default:
Simon Wilson193b1c32011-06-07 23:42:38 -07001055 return -EINVAL;
Simon Wilson79d39652011-05-25 13:44:23 -07001056 }
Simon Wilsona1bb1e02011-05-26 18:22:00 -07001057
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001058 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilson79d39652011-05-25 13:44:23 -07001059}
1060
Taylor Holberton4e557192016-11-23 11:48:06 -08001061/** Sets the contents of a control's value array.
1062 * @param ctl An initialized control handle.
1063 * @param array The array containing control values.
1064 * @param count The number of values in the array.
1065 * This must match the number of values in the control.
1066 * The number of values in a control may be accessed via @ref mixer_ctl_get_num_values
1067 * @returns On success, zero.
1068 * On failure, non-zero.
1069 * @ingroup libtinyalsa-mixer
1070 */
Simon Wilson38f87f32012-10-23 15:05:23 -07001071int mixer_ctl_set_array(struct mixer_ctl *ctl, const void *array, size_t count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001072{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001073 struct mixer_ctl_group *grp;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001074 struct snd_ctl_elem_value ev;
Simon Wilson38f87f32012-10-23 15:05:23 -07001075 size_t size;
1076 void *dest;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301077 size_t total_count;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001078
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301079 if ((!ctl) || !count || !array)
1080 return -EINVAL;
1081
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001082 grp = ctl->grp;
Pankaj Bharadiya3f813e42017-01-09 13:42:14 +05301083 total_count = ctl->info.count;
1084
1085 if ((ctl->info.type == SNDRV_CTL_ELEM_TYPE_BYTES) &&
1086 (mixer_ctl_is_access_tlv_rw(ctl))) {
1087 /* Additional TLV header */
1088 total_count += TLV_HEADER_SIZE;
1089 }
1090
1091 if (count > total_count)
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001092 return -EINVAL;
1093
1094 memset(&ev, 0, sizeof(ev));
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001095 ev.id.numid = ctl->info.id.numid;
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001096
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001097 switch (ctl->info.type) {
Simon Wilson38f87f32012-10-23 15:05:23 -07001098 case SNDRV_CTL_ELEM_TYPE_BOOLEAN:
1099 case SNDRV_CTL_ELEM_TYPE_INTEGER:
1100 size = sizeof(ev.value.integer.value[0]);
1101 dest = ev.value.integer.value;
1102 break;
1103
1104 case SNDRV_CTL_ELEM_TYPE_BYTES:
Mythri P K45b2d042014-08-18 15:39:36 +05301105 /* check if this is new bytes TLV */
Pankaj Bharadiya9698d032017-01-09 12:23:14 +05301106 if (mixer_ctl_is_access_tlv_rw(ctl)) {
Mythri P K45b2d042014-08-18 15:39:36 +05301107 struct snd_ctl_tlv *tlv;
1108 int ret = 0;
Ben Zhang7ed2ffb2016-04-22 17:59:40 -07001109 if (count > SIZE_MAX - sizeof(*tlv))
1110 return -EINVAL;
Mythri P K45b2d042014-08-18 15:39:36 +05301111 tlv = calloc(1, sizeof(*tlv) + count);
Ben Zhang7ed2ffb2016-04-22 17:59:40 -07001112 if (!tlv)
1113 return -ENOMEM;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001114 tlv->numid = ctl->info.id.numid;
Mythri P K45b2d042014-08-18 15:39:36 +05301115 tlv->length = count;
1116 memcpy(tlv->tlv, array, count);
1117
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001118 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_TLV_WRITE, tlv);
Mythri P K45b2d042014-08-18 15:39:36 +05301119 free(tlv);
1120
1121 return ret;
1122 } else {
1123 size = sizeof(ev.value.bytes.data[0]);
1124 dest = ev.value.bytes.data;
1125 }
Simon Wilson38f87f32012-10-23 15:05:23 -07001126 break;
1127
1128 default:
1129 return -EINVAL;
1130 }
1131
1132 memcpy(dest, array, size * count);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001133
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001134 return grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Dimitris Papastamosf51c05b2012-05-28 13:40:33 +01001135}
1136
Taylor Holberton4e557192016-11-23 11:48:06 -08001137/** Gets the minimum value of an control.
1138 * The control must have an integer type.
1139 * The type of the control can be checked with @ref mixer_ctl_get_type.
1140 * @param ctl An initialized control handle.
1141 * @returns On success, the minimum value of the control.
1142 * On failure, -EINVAL.
1143 * @ingroup libtinyalsa-mixer
1144 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -08001145int mixer_ctl_get_range_min(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001146{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001147 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -07001148 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001149
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001150 return ctl->info.value.integer.min;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001151}
1152
Taylor Holberton4e557192016-11-23 11:48:06 -08001153/** Gets the maximum value of an control.
1154 * The control must have an integer type.
1155 * The type of the control can be checked with @ref mixer_ctl_get_type.
1156 * @param ctl An initialized control handle.
1157 * @returns On success, the maximum value of the control.
1158 * On failure, -EINVAL.
1159 * @ingroup libtinyalsa-mixer
1160 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -08001161int mixer_ctl_get_range_max(const struct mixer_ctl *ctl)
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001162{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001163 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_INTEGER))
Simon Wilson193b1c32011-06-07 23:42:38 -07001164 return -EINVAL;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001165
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001166 return ctl->info.value.integer.max;
Simon Wilsonb9d4f6b2011-06-06 14:41:02 -07001167}
1168
Taylor Holberton4e557192016-11-23 11:48:06 -08001169/** Get the number of enumerated items in the control.
1170 * @param ctl An initialized control handle.
1171 * @returns The number of enumerated items in the control.
1172 * @ingroup libtinyalsa-mixer
1173 */
Taylor Holbertoncac43a22016-12-01 18:11:24 -08001174unsigned int mixer_ctl_get_num_enums(const struct mixer_ctl *ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -07001175{
Simon Wilson193b1c32011-06-07 23:42:38 -07001176 if (!ctl)
Simon Wilson066c9f62011-06-05 18:23:05 -07001177 return 0;
Simon Wilson066c9f62011-06-05 18:23:05 -07001178
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001179 return ctl->info.value.enumerated.items;
Simon Wilson066c9f62011-06-05 18:23:05 -07001180}
1181
David.Coutherut9b423962013-06-03 13:45:51 +02001182int mixer_ctl_fill_enum_string(struct mixer_ctl *ctl)
1183{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001184 struct mixer_ctl_group *grp = ctl->grp;
David.Coutherut9b423962013-06-03 13:45:51 +02001185 struct snd_ctl_elem_info tmp;
1186 unsigned int m;
1187 char **enames;
1188
1189 if (ctl->ename) {
1190 return 0;
1191 }
1192
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001193 enames = calloc(ctl->info.value.enumerated.items, sizeof(char*));
David.Coutherut9b423962013-06-03 13:45:51 +02001194 if (!enames)
1195 goto fail;
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001196 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +02001197 memset(&tmp, 0, sizeof(tmp));
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001198 tmp.id.numid = ctl->info.id.numid;
David.Coutherut9b423962013-06-03 13:45:51 +02001199 tmp.value.enumerated.item = m;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001200 if (grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_INFO, &tmp) < 0)
David.Coutherut9b423962013-06-03 13:45:51 +02001201 goto fail;
1202 enames[m] = strdup(tmp.value.enumerated.name);
1203 if (!enames[m])
1204 goto fail;
1205 }
1206 ctl->ename = enames;
1207 return 0;
1208
1209fail:
1210 if (enames) {
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001211 for (m = 0; m < ctl->info.value.enumerated.items; m++) {
David.Coutherut9b423962013-06-03 13:45:51 +02001212 if (enames[m]) {
1213 free(enames[m]);
1214 }
1215 }
1216 free(enames);
1217 }
1218 return -1;
1219}
1220
Taylor Holberton4e557192016-11-23 11:48:06 -08001221/** Gets the string representation of an enumerated item.
1222 * @param ctl An initialized control handle.
1223 * @param enum_id The index of the enumerated value.
1224 * @returns A string representation of the enumerated item.
1225 * @ingroup libtinyalsa-mixer
1226 */
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001227const char *mixer_ctl_get_enum_string(struct mixer_ctl *ctl,
1228 unsigned int enum_id)
Simon Wilson79d39652011-05-25 13:44:23 -07001229{
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001230 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001231 (enum_id >= ctl->info.value.enumerated.items) ||
David.Coutherut9b423962013-06-03 13:45:51 +02001232 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001233 return NULL;
Simon Wilson79d39652011-05-25 13:44:23 -07001234
Simon Wilsonb29ac1a2012-03-08 10:15:08 -08001235 return (const char *)ctl->ename[enum_id];
Simon Wilson79d39652011-05-25 13:44:23 -07001236}
1237
Taylor Holberton4e557192016-11-23 11:48:06 -08001238/** Set an enumeration value by string value.
1239 * @param ctl An enumerated mixer control.
1240 * @param string The string representation of an enumeration.
1241 * @returns On success, zero.
1242 * On failure, zero.
1243 * @ingroup libtinyalsa-mixer
1244 */
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001245int mixer_ctl_set_enum_by_string(struct mixer_ctl *ctl, const char *string)
1246{
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001247 struct mixer_ctl_group *grp;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001248 unsigned int i, num_enums;
1249 struct snd_ctl_elem_value ev;
Simon Wilson193b1c32011-06-07 23:42:38 -07001250 int ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001251
Taylor Holberton6a38d5f2016-10-03 21:07:39 -04001252 if (!ctl || (ctl->info.type != SNDRV_CTL_ELEM_TYPE_ENUMERATED) ||
David.Coutherut9b423962013-06-03 13:45:51 +02001253 mixer_ctl_fill_enum_string(ctl) != 0)
Simon Wilson193b1c32011-06-07 23:42:38 -07001254 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001255
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001256 grp = ctl->grp;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001257 num_enums = ctl->info.value.enumerated.items;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001258 for (i = 0; i < num_enums; i++) {
1259 if (!strcmp(string, ctl->ename[i])) {
1260 memset(&ev, 0, sizeof(ev));
1261 ev.value.enumerated.item[0] = i;
Richard Fitzgerald899cece2014-09-09 17:03:21 +01001262 ev.id.numid = ctl->info.id.numid;
Bhalchandra Gajaree7c627d2019-06-19 15:30:42 -07001263 ret = grp->ops->ioctl(grp->data, SNDRV_CTL_IOCTL_ELEM_WRITE, &ev);
Simon Wilson193b1c32011-06-07 23:42:38 -07001264 if (ret < 0)
1265 return ret;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001266 return 0;
1267 }
1268 }
1269
Simon Wilson193b1c32011-06-07 23:42:38 -07001270 return -EINVAL;
Simon Wilsonf0a20ee2011-06-05 21:18:52 -07001271}