Blame src/ucm/main.c

Packit 4a16fb
/*
Packit 4a16fb
 *  This library is free software; you can redistribute it and/or
Packit 4a16fb
 *  modify it under the terms of the GNU Lesser General Public
Packit 4a16fb
 *  License as published by the Free Software Foundation; either
Packit 4a16fb
 *  version 2 of the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *  This library is distributed in the hope that it will be useful,
Packit 4a16fb
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4a16fb
 *  Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *  You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *  License along with this library; if not, write to the Free Software  
Packit 4a16fb
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 *  Support for the verb/device/modifier core logic and API,
Packit 4a16fb
 *  command line tool and file parser was kindly sponsored by
Packit 4a16fb
 *  Texas Instruments Inc.
Packit 4a16fb
 *  Support for multiple active modifiers and devices,
Packit 4a16fb
 *  transition sequences, multiple client access and user defined use
Packit 4a16fb
 *  cases was kindly sponsored by Wolfson Microelectronics PLC.
Packit 4a16fb
 *
Packit 4a16fb
 *  Copyright (C) 2008-2010 SlimLogic Ltd
Packit 4a16fb
 *  Copyright (C) 2010 Wolfson Microelectronics PLC
Packit 4a16fb
 *  Copyright (C) 2010 Texas Instruments Inc.
Packit 4a16fb
 *  Copyright (C) 2010 Red Hat Inc.
Packit 4a16fb
 *  Authors: Liam Girdwood <lrg@slimlogic.co.uk>
Packit 4a16fb
 *	         Stefan Schmidt <stefan@slimlogic.co.uk>
Packit 4a16fb
 *	         Justin Xu <justinx@slimlogic.co.uk>
Packit 4a16fb
 *               Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include "ucm_local.h"
Packit 4a16fb
#include <ctype.h>
Packit 4a16fb
#include <stdarg.h>
Packit 4a16fb
#include <pthread.h>
Packit 4a16fb
#include <sys/stat.h>
Packit 4a16fb
#include <limits.h>
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * misc
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
static int get_value1(snd_use_case_mgr_t *uc_mgr, char **value,
Packit 4a16fb
                      struct list_head *value_list, const char *identifier);
Packit 4a16fb
static int get_value3(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                      char **value,
Packit 4a16fb
		      const char *identifier,
Packit 4a16fb
		      struct list_head *value_list1,
Packit 4a16fb
		      struct list_head *value_list2,
Packit 4a16fb
		      struct list_head *value_list3);
Packit 4a16fb
Packit 4a16fb
static int execute_component_seq(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
				 struct component_sequence *cmpt_seq,
Packit 4a16fb
				 struct list_head *value_list1,
Packit 4a16fb
				 struct list_head *value_list2,
Packit 4a16fb
				 struct list_head *value_list3,
Packit 4a16fb
				 char *cdev);
Packit 4a16fb
Packit 4a16fb
static int check_identifier(const char *identifier, const char *prefix)
Packit 4a16fb
{
Packit 4a16fb
	int len;
Packit 4a16fb
Packit 4a16fb
	if (strcmp(identifier, prefix) == 0)
Packit 4a16fb
		return 1;
Packit 4a16fb
	len = strlen(prefix);
Packit 4a16fb
	if (memcmp(identifier, prefix, len) == 0 && identifier[len] == '/')
Packit 4a16fb
		return 1;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int list_count(struct list_head *list)
Packit 4a16fb
{
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
        int count = 0;
Packit 4a16fb
        
Packit 4a16fb
        list_for_each(pos, list) {
Packit 4a16fb
                count += 1;
Packit 4a16fb
        }
Packit 4a16fb
        return count;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int alloc_str_list(struct list_head *list, int mult, char **result[])
Packit 4a16fb
{
Packit 4a16fb
        char **res;
Packit 4a16fb
        int cnt;
Packit 4a16fb
        
Packit 4a16fb
        cnt = list_count(list) * mult;
Packit 4a16fb
        if (cnt == 0) {
Packit 4a16fb
		*result = NULL;
Packit 4a16fb
                return cnt;
Packit 4a16fb
	}
Packit 4a16fb
        res = calloc(mult, cnt * sizeof(char *));
Packit 4a16fb
        if (res == NULL)
Packit 4a16fb
                return -ENOMEM;
Packit 4a16fb
        *result = res;
Packit 4a16fb
        return cnt;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Create an identifier
Packit 4a16fb
 * \param fmt Format (sprintf like)
Packit 4a16fb
 * \param ... Optional arguments for sprintf like format
Packit 4a16fb
 * \return Allocated string identifier or NULL on error
Packit 4a16fb
 */
Packit 4a16fb
char *snd_use_case_identifier(const char *fmt, ...)
Packit 4a16fb
{
Packit 4a16fb
	char *str, *res;
Packit 4a16fb
	int size = strlen(fmt) + 512;
Packit 4a16fb
	va_list args;
Packit 4a16fb
Packit 4a16fb
	str = malloc(size);
Packit 4a16fb
	if (str == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	va_start(args, fmt);
Packit 4a16fb
	vsnprintf(str, size, fmt, args);
Packit 4a16fb
	va_end(args);
Packit 4a16fb
	str[size-1] = '\0';
Packit 4a16fb
	res = realloc(str, strlen(str) + 1);
Packit 4a16fb
	if (res)
Packit 4a16fb
		return res;
Packit 4a16fb
	return str;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Free a string list
Packit 4a16fb
 * \param list The string list to free
Packit 4a16fb
 * \param items Count of strings
Packit 4a16fb
 * \return Zero if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_free_list(const char *list[], int items)
Packit 4a16fb
{
Packit 4a16fb
        int i;
Packit 4a16fb
	if (list == NULL)
Packit 4a16fb
		return 0;
Packit 4a16fb
        for (i = 0; i < items; i++)
Packit 4a16fb
		free((void *)list[i]);
Packit 4a16fb
        free(list);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int read_tlv_file(unsigned int **res,
Packit 4a16fb
			 const char *filepath)
Packit 4a16fb
{
Packit 4a16fb
	int err = 0;
Packit 4a16fb
	int fd;
Packit 4a16fb
	struct stat st;
Packit 4a16fb
	size_t sz;
Packit 4a16fb
	ssize_t sz_read;
Packit 4a16fb
	struct snd_ctl_tlv *tlv;
Packit 4a16fb
Packit 4a16fb
	fd = open(filepath, O_RDONLY);
Packit 4a16fb
	if (fd < 0) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (fstat(fd, &st) == -1) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	sz = st.st_size;
Packit 4a16fb
	if (sz > 16 * 1024 * 1024 || sz < 8 || sz % 4) {
Packit 4a16fb
		uc_error("File size should be less than 16 MB "
Packit 4a16fb
			 "and multiple of 4");
Packit 4a16fb
		err = -EINVAL;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	*res = malloc(sz);
Packit 4a16fb
	if (res == NULL) {
Packit 4a16fb
		err = -ENOMEM;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	sz_read = read(fd, *res, sz);
Packit 4a16fb
	if (sz_read < 0 || (size_t)sz_read != sz) {
Packit 4a16fb
		err = -EIO;
Packit 4a16fb
		free(*res);
Packit 4a16fb
		*res = NULL;
Packit 4a16fb
	}
Packit 4a16fb
	/* Check if the tlv file specifies valid size. */
Packit 4a16fb
	tlv = (struct snd_ctl_tlv *)(*res);
Packit 4a16fb
	if (tlv->length + 2 * sizeof(unsigned int) != sz) {
Packit 4a16fb
		uc_error("Invalid tlv size: %d", tlv->length);
Packit 4a16fb
		err = -EINVAL;
Packit 4a16fb
		free(*res);
Packit 4a16fb
		*res = NULL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
__fail:
Packit 4a16fb
	close(fd);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int binary_file_parse(snd_ctl_elem_value_t *dst,
Packit 4a16fb
			      snd_ctl_elem_info_t *info,
Packit 4a16fb
			      const char *filepath)
Packit 4a16fb
{
Packit 4a16fb
	int err = 0;
Packit 4a16fb
	int fd;
Packit 4a16fb
	struct stat st;
Packit 4a16fb
	size_t sz;
Packit 4a16fb
	ssize_t sz_read;
Packit 4a16fb
	char *res;
Packit 4a16fb
	snd_ctl_elem_type_t type;
Packit 4a16fb
	unsigned int idx, count;
Packit 4a16fb
Packit 4a16fb
	type = snd_ctl_elem_info_get_type(info);
Packit 4a16fb
	if (type != SND_CTL_ELEM_TYPE_BYTES) {
Packit 4a16fb
		uc_error("only support byte type!");
Packit 4a16fb
		err = -EINVAL;
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	fd = open(filepath, O_RDONLY);
Packit 4a16fb
	if (fd < 0) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (stat(filepath, &st) == -1) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	sz = st.st_size;
Packit 4a16fb
	count = snd_ctl_elem_info_get_count(info);
Packit 4a16fb
	if (sz != count || sz > sizeof(dst->value.bytes)) {
Packit 4a16fb
		uc_error("invalid parameter size %d!", sz);
Packit 4a16fb
		err = -EINVAL;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	res = malloc(sz);
Packit 4a16fb
	if (res == NULL) {
Packit 4a16fb
		err = -ENOMEM;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	sz_read = read(fd, res, sz);
Packit 4a16fb
	if (sz_read < 0 || (size_t)sz_read != sz) {
Packit 4a16fb
		err = -errno;
Packit 4a16fb
		goto __fail_read;
Packit 4a16fb
	}
Packit 4a16fb
	for (idx = 0; idx < sz; idx++)
Packit 4a16fb
		snd_ctl_elem_value_set_byte(dst, idx, *(res + idx));
Packit 4a16fb
      __fail_read:
Packit 4a16fb
	free(res);
Packit 4a16fb
      __fail:
Packit 4a16fb
	close(fd);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
extern int __snd_ctl_ascii_elem_id_parse(snd_ctl_elem_id_t *dst,
Packit 4a16fb
					 const char *str,
Packit 4a16fb
					 const char **ret_ptr);
Packit 4a16fb
Packit 4a16fb
static int execute_cset(snd_ctl_t *ctl, const char *cset, unsigned int type)
Packit 4a16fb
{
Packit 4a16fb
	const char *pos;
Packit 4a16fb
	int err;
Packit 4a16fb
	snd_ctl_elem_id_t *id;
Packit 4a16fb
	snd_ctl_elem_value_t *value;
Packit 4a16fb
	snd_ctl_elem_info_t *info;
Packit 4a16fb
	unsigned int *res = NULL;
Packit 4a16fb
Packit 4a16fb
	snd_ctl_elem_id_malloc(&id;;
Packit 4a16fb
	snd_ctl_elem_value_malloc(&value);
Packit 4a16fb
	snd_ctl_elem_info_malloc(&info;;
Packit 4a16fb
Packit 4a16fb
	err = __snd_ctl_ascii_elem_id_parse(id, cset, &pos;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	while (*pos && isspace(*pos))
Packit 4a16fb
		pos++;
Packit 4a16fb
	if (!*pos) {
Packit 4a16fb
		uc_error("undefined value for cset >%s<", cset);
Packit 4a16fb
		err = -EINVAL;
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	snd_ctl_elem_info_set_id(info, id);
Packit 4a16fb
	err = snd_ctl_elem_info(ctl, info);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		goto __fail;
Packit 4a16fb
	if (type == SEQUENCE_ELEMENT_TYPE_CSET_TLV) {
Packit 4a16fb
		if (!snd_ctl_elem_info_is_tlv_writable(info)) {
Packit 4a16fb
			err = -EINVAL;
Packit 4a16fb
			goto __fail;
Packit 4a16fb
		}
Packit 4a16fb
		err = read_tlv_file(&res, pos);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __fail;
Packit 4a16fb
		err = snd_ctl_elem_tlv_write(ctl, id, res);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __fail;
Packit 4a16fb
	} else {
Packit 4a16fb
		snd_ctl_elem_value_set_id(value, id);
Packit 4a16fb
		err = snd_ctl_elem_read(ctl, value);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __fail;
Packit 4a16fb
		if (type == SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE)
Packit 4a16fb
			err = binary_file_parse(value, info, pos);
Packit 4a16fb
		else
Packit 4a16fb
			err = snd_ctl_ascii_value_parse(ctl, value, info, pos);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __fail;
Packit 4a16fb
		err = snd_ctl_elem_write(ctl, value);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __fail;
Packit 4a16fb
	}
Packit 4a16fb
	err = 0;
Packit 4a16fb
      __fail:
Packit 4a16fb
	if (id != NULL)
Packit 4a16fb
		free(id);
Packit 4a16fb
	if (value != NULL)
Packit 4a16fb
		free(value);
Packit 4a16fb
	if (info != NULL)
Packit 4a16fb
		free(info);
Packit 4a16fb
	if (res != NULL)
Packit 4a16fb
		free(res);
Packit 4a16fb
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Execute the sequence
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param seq Sequence
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int execute_sequence(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
			    struct list_head *seq,
Packit 4a16fb
			    struct list_head *value_list1,
Packit 4a16fb
			    struct list_head *value_list2,
Packit 4a16fb
			    struct list_head *value_list3)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	struct sequence_element *s;
Packit 4a16fb
	char *cdev = NULL;
Packit 4a16fb
	snd_ctl_t *ctl = NULL;
Packit 4a16fb
	int err = 0;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, seq) {
Packit 4a16fb
		s = list_entry(pos, struct sequence_element, list);
Packit 4a16fb
		switch (s->type) {
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_CDEV:
Packit 4a16fb
			cdev = strdup(s->data.cdev);
Packit 4a16fb
			if (cdev == NULL)
Packit 4a16fb
				goto __fail_nomem;
Packit 4a16fb
			break;
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_CSET:
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_CSET_BIN_FILE:
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_CSET_TLV:
Packit 4a16fb
			if (cdev == NULL && uc_mgr->in_component_domain) {
Packit 4a16fb
				/* For sequence of a component device, use
Packit 4a16fb
				 * its parent's cdev stored by ucm manager.
Packit 4a16fb
				 */
Packit 4a16fb
				if (uc_mgr->cdev == NULL) {
Packit 4a16fb
					uc_error("cdev is not defined!");
Packit 4a16fb
					return err;
Packit 4a16fb
				}
Packit 4a16fb
Packit 4a16fb
				cdev = strndup(uc_mgr->cdev, PATH_MAX);
Packit 4a16fb
				if (!cdev)
Packit 4a16fb
					return -ENOMEM;
Packit 4a16fb
			} else if (cdev == NULL) {
Packit 4a16fb
				char *playback_ctl = NULL;
Packit 4a16fb
				char *capture_ctl = NULL;
Packit 4a16fb
Packit 4a16fb
				err = get_value3(uc_mgr, &playback_ctl, "PlaybackCTL",
Packit 4a16fb
						 value_list1,
Packit 4a16fb
						 value_list2,
Packit 4a16fb
						 value_list3);
Packit 4a16fb
				if (err < 0 && err != -ENOENT) {
Packit 4a16fb
					uc_error("cdev is not defined!");
Packit 4a16fb
					return err;
Packit 4a16fb
				}
Packit 4a16fb
				err = get_value3(uc_mgr, &capture_ctl, "CaptureCTL",
Packit 4a16fb
						 value_list1,
Packit 4a16fb
						 value_list2,
Packit 4a16fb
						 value_list3);
Packit 4a16fb
				if (err < 0 && err != -ENOENT) {
Packit 4a16fb
					free(playback_ctl);
Packit 4a16fb
					uc_error("cdev is not defined!");
Packit 4a16fb
					return err;
Packit 4a16fb
				}
Packit 4a16fb
				if (playback_ctl == NULL &&
Packit 4a16fb
				    capture_ctl == NULL) {
Packit 4a16fb
					uc_error("cdev is not defined!");
Packit 4a16fb
					return -EINVAL;
Packit 4a16fb
				}
Packit 4a16fb
				if (playback_ctl != NULL &&
Packit 4a16fb
				    capture_ctl != NULL &&
Packit 4a16fb
				    strcmp(playback_ctl, capture_ctl) != 0) {
Packit 4a16fb
					free(playback_ctl);
Packit 4a16fb
					free(capture_ctl);
Packit 4a16fb
					uc_error("cdev is not equal for playback and capture!");
Packit 4a16fb
					return -EINVAL;
Packit 4a16fb
				}
Packit 4a16fb
				if (playback_ctl != NULL) {
Packit 4a16fb
					cdev = playback_ctl;
Packit 4a16fb
					free(capture_ctl);
Packit 4a16fb
				} else {
Packit 4a16fb
					cdev = capture_ctl;
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
			if (ctl == NULL) {
Packit 4a16fb
				err = uc_mgr_open_ctl(uc_mgr, &ctl, cdev);
Packit 4a16fb
				if (err < 0) {
Packit 4a16fb
					uc_error("unable to open ctl device '%s'", cdev);
Packit 4a16fb
					goto __fail;
Packit 4a16fb
				}
Packit 4a16fb
			}
Packit 4a16fb
			err = execute_cset(ctl, s->data.cset, s->type);
Packit 4a16fb
			if (err < 0) {
Packit 4a16fb
				uc_error("unable to execute cset '%s'", s->data.cset);
Packit 4a16fb
				goto __fail;
Packit 4a16fb
			}
Packit 4a16fb
			break;
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_SLEEP:
Packit 4a16fb
			usleep(s->data.sleep);
Packit 4a16fb
			break;
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_EXEC:
Packit 4a16fb
			err = system(s->data.exec);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __fail;
Packit 4a16fb
			break;
Packit 4a16fb
		case SEQUENCE_ELEMENT_TYPE_CMPT_SEQ:
Packit 4a16fb
			/* Execute enable or disable sequence of a component
Packit 4a16fb
			 * device. Pass the cdev defined by the machine device.
Packit 4a16fb
			 */
Packit 4a16fb
			err = execute_component_seq(uc_mgr,
Packit 4a16fb
						    &s->data.cmpt_seq,
Packit 4a16fb
						    value_list1,
Packit 4a16fb
						    value_list2,
Packit 4a16fb
						    value_list3,
Packit 4a16fb
						    cdev);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				goto __fail;
Packit 4a16fb
			break;
Packit 4a16fb
		default:
Packit 4a16fb
			uc_error("unknown sequence command %i", s->type);
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	free(cdev);
Packit 4a16fb
	return 0;
Packit 4a16fb
      __fail_nomem:
Packit 4a16fb
	err = -ENOMEM;
Packit 4a16fb
      __fail:
Packit 4a16fb
	free(cdev);
Packit 4a16fb
	return err;
Packit 4a16fb
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/* Execute enable or disable sequence of a component device.
Packit 4a16fb
 *
Packit 4a16fb
 * For a component device (a codec or embedded DSP), its sequence doesn't
Packit 4a16fb
 * specify the sound card device 'cdev', because a component can be reused
Packit 4a16fb
 * by different sound cards (machines). So when executing its sequence, a
Packit 4a16fb
 * parameter 'cdev' is used to pass cdev defined by the sequence of its
Packit 4a16fb
 * parent, the machine device. UCM manger will store the cdev when entering
Packit 4a16fb
 * the component domain.
Packit 4a16fb
 */
Packit 4a16fb
static int execute_component_seq(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
				 struct component_sequence *cmpt_seq,
Packit 4a16fb
				 struct list_head *value_list1 ATTRIBUTE_UNUSED,
Packit 4a16fb
				 struct list_head *value_list2 ATTRIBUTE_UNUSED,
Packit 4a16fb
				 struct list_head *value_list3 ATTRIBUTE_UNUSED,
Packit 4a16fb
				 char *cdev)
Packit 4a16fb
{
Packit 4a16fb
	struct use_case_device *device = cmpt_seq->device;
Packit 4a16fb
	struct list_head *seq;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	/* enter component domain and store cdev for the component */
Packit 4a16fb
	uc_mgr->in_component_domain = 1;
Packit 4a16fb
	uc_mgr->cdev = cdev;
Packit 4a16fb
Packit 4a16fb
	/* choose enable or disable sequence of the component device */
Packit 4a16fb
	if (cmpt_seq->enable)
Packit 4a16fb
		seq = &device->enable_list;
Packit 4a16fb
	else
Packit 4a16fb
		seq = &device->disable_list;
Packit 4a16fb
Packit 4a16fb
	/* excecute the sequence of the component dev */
Packit 4a16fb
	err = execute_sequence(uc_mgr, seq,
Packit 4a16fb
			       &device->value_list,
Packit 4a16fb
			       &uc_mgr->active_verb->value_list,
Packit 4a16fb
			       &uc_mgr->value_list);
Packit 4a16fb
Packit 4a16fb
	/* exit component domain and clear cdev */
Packit 4a16fb
	uc_mgr->in_component_domain = 0;
Packit 4a16fb
	uc_mgr->cdev = NULL;
Packit 4a16fb
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int add_auto_value(snd_use_case_mgr_t *uc_mgr, const char *key, char *value)
Packit 4a16fb
{
Packit 4a16fb
	char *s;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	err = get_value1(uc_mgr, &value, &uc_mgr->value_list, key);
Packit 4a16fb
	if (err == -ENOENT) {
Packit 4a16fb
		s = strdup(value);
Packit 4a16fb
		if (s == NULL)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		return uc_mgr_add_value(&uc_mgr->value_list, key, s);
Packit 4a16fb
	} else if (err < 0) {
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	free(value);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int add_auto_values(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct ctl_list *ctl_list;
Packit 4a16fb
	const char *id;
Packit 4a16fb
	char buf[40];
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	ctl_list = uc_mgr_get_one_ctl(uc_mgr);
Packit 4a16fb
	if (ctl_list) {
Packit 4a16fb
		id = snd_ctl_card_info_get_id(ctl_list->ctl_info);
Packit 4a16fb
		snprintf(buf, sizeof(buf), "hw:%s", id);
Packit 4a16fb
		err = add_auto_value(uc_mgr, "PlaybackCTL", buf);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
		err = add_auto_value(uc_mgr, "CaptureCTL", buf);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Import master config and execute the default sequence
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int import_master_config(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	err = uc_mgr_import_master_config(uc_mgr);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = add_auto_values(uc_mgr);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = execute_sequence(uc_mgr, &uc_mgr->default_list,
Packit 4a16fb
			       &uc_mgr->value_list, NULL, NULL);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		uc_error("Unable to execute default sequence");
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Universal find - string in a list
Packit 4a16fb
 * \param list List of structures
Packit 4a16fb
 * \param offset Offset of list structure
Packit 4a16fb
 * \param soffset Offset of string structure
Packit 4a16fb
 * \param match String to match
Packit 4a16fb
 * \return structure on success, otherwise a NULL (not found)
Packit 4a16fb
 */
Packit 4a16fb
static void *find0(struct list_head *list,
Packit 4a16fb
		   unsigned long offset,
Packit 4a16fb
		   unsigned long soffset,
Packit 4a16fb
		   const char *match)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	char *ptr, *str;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, list) {
Packit 4a16fb
		ptr = list_entry_offset(pos, char, offset);
Packit 4a16fb
		str = *((char **)(ptr + soffset));
Packit 4a16fb
		if (strcmp(str, match) == 0)
Packit 4a16fb
			return ptr;
Packit 4a16fb
	}
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#define find(list, type, member, value, match) \
Packit 4a16fb
	find0(list, (unsigned long)(&((type *)0)->member), \
Packit 4a16fb
		    (unsigned long)(&((type *)0)->value), match)
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Universal string list
Packit 4a16fb
 * \param list List of structures
Packit 4a16fb
 * \param result Result list
Packit 4a16fb
 * \param offset Offset of list structure
Packit 4a16fb
 * \param s1offset Offset of string structure
Packit 4a16fb
 * \return count of items on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_list0(struct list_head *list,
Packit 4a16fb
                     const char **result[],
Packit 4a16fb
                     unsigned long offset,
Packit 4a16fb
                     unsigned long s1offset)
Packit 4a16fb
{
Packit 4a16fb
        char **res;
Packit 4a16fb
        int cnt;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	char *ptr, *str1;
Packit 4a16fb
Packit 4a16fb
	cnt = alloc_str_list(list, 1, &res;;
Packit 4a16fb
	if (cnt <= 0) {
Packit 4a16fb
		*result = NULL;
Packit 4a16fb
	        return cnt;
Packit 4a16fb
	}
Packit 4a16fb
	*result = (const char **)res;
Packit 4a16fb
	list_for_each(pos, list) {
Packit 4a16fb
		ptr = list_entry_offset(pos, char, offset);
Packit 4a16fb
		str1 = *((char **)(ptr + s1offset));
Packit 4a16fb
		if (str1 != NULL) {
Packit 4a16fb
		        *res = strdup(str1);
Packit 4a16fb
		        if (*res == NULL)
Packit 4a16fb
		                goto __fail;
Packit 4a16fb
                } else {
Packit 4a16fb
                        *res = NULL;
Packit 4a16fb
                }
Packit 4a16fb
                res++;
Packit 4a16fb
	}
Packit 4a16fb
	return cnt;
Packit 4a16fb
      __fail:
Packit 4a16fb
        snd_use_case_free_list((const char **)res, cnt);
Packit 4a16fb
        return -ENOMEM;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#define get_list(list, result, type, member, s1) \
Packit 4a16fb
	get_list0(list, result, \
Packit 4a16fb
	            (unsigned long)(&((type *)0)->member), \
Packit 4a16fb
		    (unsigned long)(&((type *)0)->s1))
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Universal string list - pair of strings
Packit 4a16fb
 * \param list List of structures
Packit 4a16fb
 * \param result Result list
Packit 4a16fb
 * \param offset Offset of list structure
Packit 4a16fb
 * \param s1offset Offset of string structure
Packit 4a16fb
 * \param s1offset Offset of string structure
Packit 4a16fb
 * \return count of items on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_list20(struct list_head *list,
Packit 4a16fb
                      const char **result[],
Packit 4a16fb
                      unsigned long offset,
Packit 4a16fb
                      unsigned long s1offset,
Packit 4a16fb
                      unsigned long s2offset)
Packit 4a16fb
{
Packit 4a16fb
        char **res;
Packit 4a16fb
        int cnt;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
	char *ptr, *str1, *str2;
Packit 4a16fb
Packit 4a16fb
	cnt = alloc_str_list(list, 2, &res;;
Packit 4a16fb
	if (cnt <= 0) {
Packit 4a16fb
		*result = NULL;
Packit 4a16fb
	        return cnt;
Packit 4a16fb
	}
Packit 4a16fb
        *result = (const char **)res;
Packit 4a16fb
	list_for_each(pos, list) {
Packit 4a16fb
		ptr = list_entry_offset(pos, char, offset);
Packit 4a16fb
		str1 = *((char **)(ptr + s1offset));
Packit 4a16fb
		if (str1 != NULL) {
Packit 4a16fb
		        *res = strdup(str1);
Packit 4a16fb
		        if (*res == NULL)
Packit 4a16fb
		                goto __fail;
Packit 4a16fb
                } else {
Packit 4a16fb
                        *res = NULL;
Packit 4a16fb
                }
Packit 4a16fb
                res++;
Packit 4a16fb
		str2 = *((char **)(ptr + s2offset));
Packit 4a16fb
		if (str2 != NULL) {
Packit 4a16fb
		        *res = strdup(str2);
Packit 4a16fb
		        if (*res == NULL)
Packit 4a16fb
		                goto __fail;
Packit 4a16fb
                } else {
Packit 4a16fb
                        *res = NULL;
Packit 4a16fb
                }
Packit 4a16fb
                res++;
Packit 4a16fb
	}
Packit 4a16fb
	return cnt;
Packit 4a16fb
      __fail:
Packit 4a16fb
        snd_use_case_free_list((const char **)res, cnt);
Packit 4a16fb
        return -ENOMEM;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#define get_list2(list, result, type, member, s1, s2) \
Packit 4a16fb
	get_list20(list, result, \
Packit 4a16fb
	            (unsigned long)(&((type *)0)->member), \
Packit 4a16fb
		    (unsigned long)(&((type *)0)->s1), \
Packit 4a16fb
		    (unsigned long)(&((type *)0)->s2))
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Find verb
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param verb_name verb to find
Packit 4a16fb
 * \return structure on success, otherwise a NULL (not found)
Packit 4a16fb
 */
Packit 4a16fb
static inline struct use_case_verb *find_verb(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
					      const char *verb_name)
Packit 4a16fb
{
Packit 4a16fb
	return find(&uc_mgr->verb_list,
Packit 4a16fb
		    struct use_case_verb, list, name,
Packit 4a16fb
		    verb_name);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int is_devlist_supported(snd_use_case_mgr_t *uc_mgr, 
Packit 4a16fb
	struct dev_list *dev_list)
Packit 4a16fb
{
Packit 4a16fb
	struct dev_list_node *device;
Packit 4a16fb
	struct use_case_device *adev;
Packit 4a16fb
	struct list_head *pos, *pos1;
Packit 4a16fb
	int found_ret;
Packit 4a16fb
Packit 4a16fb
	switch (dev_list->type) {
Packit 4a16fb
	case DEVLIST_NONE:
Packit 4a16fb
	default:
Packit 4a16fb
		return 1;
Packit 4a16fb
	case DEVLIST_SUPPORTED:
Packit 4a16fb
		found_ret = 1;
Packit 4a16fb
		break;
Packit 4a16fb
	case DEVLIST_CONFLICTING:
Packit 4a16fb
		found_ret = 0;
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &dev_list->list) {
Packit 4a16fb
		device = list_entry(pos, struct dev_list_node, list);
Packit 4a16fb
Packit 4a16fb
		list_for_each(pos1, &uc_mgr->active_devices) {
Packit 4a16fb
			adev = list_entry(pos1, struct use_case_device,
Packit 4a16fb
					    active_list);
Packit 4a16fb
			if (!strcmp(device->name, adev->name))
Packit 4a16fb
				return found_ret;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	return 1 - found_ret;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline int is_modifier_supported(snd_use_case_mgr_t *uc_mgr, 
Packit 4a16fb
	struct use_case_modifier *modifier)
Packit 4a16fb
{
Packit 4a16fb
	return is_devlist_supported(uc_mgr, &modifier->dev_list);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline int is_device_supported(snd_use_case_mgr_t *uc_mgr, 
Packit 4a16fb
	struct use_case_device *device)
Packit 4a16fb
{
Packit 4a16fb
	return is_devlist_supported(uc_mgr, &device->dev_list);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Find device
Packit 4a16fb
 * \param verb Use case verb
Packit 4a16fb
 * \param device_name device to find
Packit 4a16fb
 * \return structure on success, otherwise a NULL (not found)
Packit 4a16fb
 */
Packit 4a16fb
static inline struct use_case_device *
Packit 4a16fb
        find_device(snd_use_case_mgr_t *uc_mgr, struct use_case_verb *verb,
Packit 4a16fb
		    const char *device_name, int check_supported)
Packit 4a16fb
{
Packit 4a16fb
	struct use_case_device *device;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &verb->device_list) {
Packit 4a16fb
		device = list_entry(pos, struct use_case_device, list);
Packit 4a16fb
Packit 4a16fb
		if (strcmp(device_name, device->name))
Packit 4a16fb
			continue;
Packit 4a16fb
Packit 4a16fb
		if (check_supported &&
Packit 4a16fb
		    !is_device_supported(uc_mgr, device))
Packit 4a16fb
			continue;
Packit 4a16fb
Packit 4a16fb
		return device;
Packit 4a16fb
	}
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Find modifier
Packit 4a16fb
 * \param verb Use case verb
Packit 4a16fb
 * \param modifier_name modifier to find
Packit 4a16fb
 * \return structure on success, otherwise a NULL (not found)
Packit 4a16fb
 */
Packit 4a16fb
static struct use_case_modifier *
Packit 4a16fb
        find_modifier(snd_use_case_mgr_t *uc_mgr, struct use_case_verb *verb,
Packit 4a16fb
		      const char *modifier_name, int check_supported)
Packit 4a16fb
{
Packit 4a16fb
	struct use_case_modifier *modifier;
Packit 4a16fb
	struct list_head *pos;
Packit 4a16fb
Packit 4a16fb
	list_for_each(pos, &verb->modifier_list) {
Packit 4a16fb
		modifier = list_entry(pos, struct use_case_modifier, list);
Packit 4a16fb
Packit 4a16fb
		if (strcmp(modifier->name, modifier_name))
Packit 4a16fb
			continue;
Packit 4a16fb
Packit 4a16fb
		if (check_supported &&
Packit 4a16fb
		    !is_modifier_supported(uc_mgr, modifier))
Packit 4a16fb
			continue;
Packit 4a16fb
Packit 4a16fb
		return modifier;
Packit 4a16fb
	}
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
long device_status(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                   const char *device_name)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_device *dev;
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
Packit 4a16fb
        list_for_each(pos, &uc_mgr->active_devices) {
Packit 4a16fb
                dev = list_entry(pos, struct use_case_device, active_list);
Packit 4a16fb
                if (strcmp(dev->name, device_name) == 0)
Packit 4a16fb
                        return 1;
Packit 4a16fb
        }
Packit 4a16fb
        return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
long modifier_status(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                     const char *modifier_name)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_modifier *mod;
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
Packit 4a16fb
        list_for_each(pos, &uc_mgr->active_modifiers) {
Packit 4a16fb
                mod = list_entry(pos, struct use_case_modifier, active_list);
Packit 4a16fb
                if (strcmp(mod->name, modifier_name) == 0)
Packit 4a16fb
                        return 1;
Packit 4a16fb
        }
Packit 4a16fb
        return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Set verb
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param verb verb to set
Packit 4a16fb
 * \param enable nonzero = enable, zero = disable
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int set_verb(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
		    struct use_case_verb *verb,
Packit 4a16fb
		    int enable)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *seq;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (enable) {
Packit 4a16fb
		seq = &verb->enable_list;
Packit 4a16fb
	} else {
Packit 4a16fb
		seq = &verb->disable_list;
Packit 4a16fb
	}
Packit 4a16fb
	err = execute_sequence(uc_mgr, seq,
Packit 4a16fb
			       &verb->value_list,
Packit 4a16fb
			       &uc_mgr->value_list,
Packit 4a16fb
			       NULL);
Packit 4a16fb
	if (enable && err >= 0)
Packit 4a16fb
		uc_mgr->active_verb = verb;
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Set modifier
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param modifier modifier to set
Packit 4a16fb
 * \param enable nonzero = enable, zero = disable
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int set_modifier(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
			struct use_case_modifier *modifier,
Packit 4a16fb
			int enable)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *seq;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (modifier_status(uc_mgr, modifier->name) == enable)
Packit 4a16fb
		return 0;
Packit 4a16fb
Packit 4a16fb
	if (enable) {
Packit 4a16fb
		seq = &modifier->enable_list;
Packit 4a16fb
	} else {
Packit 4a16fb
		seq = &modifier->disable_list;
Packit 4a16fb
	}
Packit 4a16fb
	err = execute_sequence(uc_mgr, seq,
Packit 4a16fb
			       &modifier->value_list,
Packit 4a16fb
			       &uc_mgr->active_verb->value_list,
Packit 4a16fb
			       &uc_mgr->value_list);
Packit 4a16fb
	if (enable && err >= 0) {
Packit 4a16fb
		list_add_tail(&modifier->active_list, &uc_mgr->active_modifiers);
Packit 4a16fb
	} else if (!enable) {
Packit 4a16fb
		list_del(&modifier->active_list);
Packit 4a16fb
	}
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Set device
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param device device to set
Packit 4a16fb
 * \param enable nonzero = enable, zero = disable
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int set_device(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
		      struct use_case_device *device,
Packit 4a16fb
		      int enable)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *seq;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
        if (device_status(uc_mgr, device->name) == enable)
Packit 4a16fb
		return 0;
Packit 4a16fb
Packit 4a16fb
	if (enable) {
Packit 4a16fb
		seq = &device->enable_list;
Packit 4a16fb
	} else {
Packit 4a16fb
		seq = &device->disable_list;
Packit 4a16fb
	}
Packit 4a16fb
	err = execute_sequence(uc_mgr, seq,
Packit 4a16fb
			       &device->value_list,
Packit 4a16fb
			       &uc_mgr->active_verb->value_list,
Packit 4a16fb
			       &uc_mgr->value_list);
Packit 4a16fb
	if (enable && err >= 0) {
Packit 4a16fb
		list_add_tail(&device->active_list, &uc_mgr->active_devices);
Packit 4a16fb
	} else if (!enable) {
Packit 4a16fb
		list_del(&device->active_list);
Packit 4a16fb
	}
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Init sound card use case manager.
Packit 4a16fb
 * \param uc_mgr Returned use case manager pointer
Packit 4a16fb
 * \param card_name name of card to open
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_mgr_open(snd_use_case_mgr_t **uc_mgr,
Packit 4a16fb
			  const char *card_name)
Packit 4a16fb
{
Packit 4a16fb
	snd_use_case_mgr_t *mgr;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	/* create a new UCM */
Packit 4a16fb
	mgr = calloc(1, sizeof(snd_use_case_mgr_t));
Packit 4a16fb
	if (mgr == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	INIT_LIST_HEAD(&mgr->verb_list);
Packit 4a16fb
	INIT_LIST_HEAD(&mgr->default_list);
Packit 4a16fb
	INIT_LIST_HEAD(&mgr->value_list);
Packit 4a16fb
	INIT_LIST_HEAD(&mgr->active_modifiers);
Packit 4a16fb
	INIT_LIST_HEAD(&mgr->active_devices);
Packit 4a16fb
	INIT_LIST_HEAD(&mgr->ctl_list);
Packit 4a16fb
	pthread_mutex_init(&mgr->mutex, NULL);
Packit 4a16fb
Packit 4a16fb
	mgr->card_name = strdup(card_name);
Packit 4a16fb
	if (mgr->card_name == NULL) {
Packit 4a16fb
		free(mgr);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	/* get info on use_cases and verify against card */
Packit 4a16fb
	err = import_master_config(mgr);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		uc_error("error: failed to import %s use case configuration %d",
Packit 4a16fb
			card_name, err);
Packit 4a16fb
		goto err;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	*uc_mgr = mgr;
Packit 4a16fb
	return 0;
Packit 4a16fb
Packit 4a16fb
err:
Packit 4a16fb
	uc_mgr_free(mgr);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Reload and reparse all use case files.
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_mgr_reload(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	pthread_mutex_lock(&uc_mgr->mutex);
Packit 4a16fb
Packit 4a16fb
	uc_mgr_free_verb(uc_mgr);
Packit 4a16fb
Packit 4a16fb
	/* reload all use cases */
Packit 4a16fb
	err = import_master_config(uc_mgr);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		uc_error("error: failed to reload use cases");
Packit 4a16fb
		pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Close use case manager.
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_mgr_close(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	uc_mgr_free(uc_mgr);
Packit 4a16fb
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/*
Packit 4a16fb
 * Tear down current use case verb, device and modifier.
Packit 4a16fb
 */
Packit 4a16fb
static int dismantle_use_case(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
	struct list_head *pos, *npos;
Packit 4a16fb
	struct use_case_modifier *modifier;
Packit 4a16fb
	struct use_case_device *device;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	list_for_each_safe(pos, npos, &uc_mgr->active_modifiers) {
Packit 4a16fb
		modifier = list_entry(pos, struct use_case_modifier,
Packit 4a16fb
				      active_list);
Packit 4a16fb
		err = set_modifier(uc_mgr, modifier, 0);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			uc_error("Unable to disable modifier %s", modifier->name);
Packit 4a16fb
	}
Packit 4a16fb
	INIT_LIST_HEAD(&uc_mgr->active_modifiers);
Packit 4a16fb
Packit 4a16fb
	list_for_each_safe(pos, npos, &uc_mgr->active_devices) {
Packit 4a16fb
		device = list_entry(pos, struct use_case_device,
Packit 4a16fb
				    active_list);
Packit 4a16fb
		err = set_device(uc_mgr, device, 0);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			uc_error("Unable to disable device %s", device->name);
Packit 4a16fb
	}
Packit 4a16fb
	INIT_LIST_HEAD(&uc_mgr->active_devices);
Packit 4a16fb
Packit 4a16fb
	err = set_verb(uc_mgr, uc_mgr->active_verb, 0);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		uc_error("Unable to disable verb %s", uc_mgr->active_verb->name);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	uc_mgr->active_verb = NULL;
Packit 4a16fb
Packit 4a16fb
	err = execute_sequence(uc_mgr, &uc_mgr->default_list,
Packit 4a16fb
			       &uc_mgr->value_list, NULL, NULL);
Packit 4a16fb
	
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Reset sound card controls to default values.
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \return zero on success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_mgr_reset(snd_use_case_mgr_t *uc_mgr)
Packit 4a16fb
{
Packit 4a16fb
        int err;
Packit 4a16fb
Packit 4a16fb
	pthread_mutex_lock(&uc_mgr->mutex);
Packit 4a16fb
	err = execute_sequence(uc_mgr, &uc_mgr->default_list,
Packit 4a16fb
			       &uc_mgr->value_list, NULL, NULL);
Packit 4a16fb
	INIT_LIST_HEAD(&uc_mgr->active_modifiers);
Packit 4a16fb
	INIT_LIST_HEAD(&uc_mgr->active_devices);
Packit 4a16fb
	uc_mgr->active_verb = NULL;
Packit 4a16fb
	pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of verbs in pair verbname+comment
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param verbname For verb (NULL = current)
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_verb_list(snd_use_case_mgr_t *uc_mgr, const char **list[])
Packit 4a16fb
{
Packit 4a16fb
        return get_list2(&uc_mgr->verb_list, list,
Packit 4a16fb
                         struct use_case_verb, list,
Packit 4a16fb
                         name, comment);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of devices in pair devicename+comment
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param verbname For verb (NULL = current)
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_device_list(snd_use_case_mgr_t *uc_mgr, const char **list[],
Packit 4a16fb
                           char *verbname)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_verb *verb;
Packit 4a16fb
        
Packit 4a16fb
        if (verbname) {
Packit 4a16fb
                verb = find_verb(uc_mgr, verbname);
Packit 4a16fb
        } else {
Packit 4a16fb
                verb = uc_mgr->active_verb;
Packit 4a16fb
        }
Packit 4a16fb
        if (verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        return get_list2(&verb->device_list, list,
Packit 4a16fb
                         struct use_case_device, list,
Packit 4a16fb
                         name, comment);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of modifiers in pair devicename+comment
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param verbname For verb (NULL = current)
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_modifier_list(snd_use_case_mgr_t *uc_mgr, const char **list[],
Packit 4a16fb
                             char *verbname)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_verb *verb;
Packit 4a16fb
        
Packit 4a16fb
        if (verbname) {
Packit 4a16fb
                verb = find_verb(uc_mgr, verbname);
Packit 4a16fb
        } else {
Packit 4a16fb
                verb = uc_mgr->active_verb;
Packit 4a16fb
        }
Packit 4a16fb
        if (verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        return get_list2(&verb->modifier_list, list,
Packit 4a16fb
                         struct use_case_modifier, list,
Packit 4a16fb
                         name, comment);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of supported/conflicting devices
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param name Name of modifier or verb to query
Packit 4a16fb
 * \param type Type of device list entries to return
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_supcon_device_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
				  const char **list[], char *name,
Packit 4a16fb
				  enum dev_list_type type)
Packit 4a16fb
{
Packit 4a16fb
	char *str;
Packit 4a16fb
	struct use_case_verb *verb;
Packit 4a16fb
	struct use_case_modifier *modifier;
Packit 4a16fb
	struct use_case_device *device;
Packit 4a16fb
Packit 4a16fb
	if (!name)
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
Packit 4a16fb
	str = strchr(name, '/');
Packit 4a16fb
	if (str) {
Packit 4a16fb
		*str = '\0';
Packit 4a16fb
		verb = find_verb(uc_mgr, str + 1);
Packit 4a16fb
	}
Packit 4a16fb
	else {
Packit 4a16fb
		verb = uc_mgr->active_verb;
Packit 4a16fb
	}
Packit 4a16fb
	if (!verb)
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
Packit 4a16fb
	modifier = find_modifier(uc_mgr, verb, name, 0);
Packit 4a16fb
	if (modifier) {
Packit 4a16fb
		if (modifier->dev_list.type != type)
Packit 4a16fb
			return 0;
Packit 4a16fb
		return get_list(&modifier->dev_list.list, list,
Packit 4a16fb
				struct dev_list_node, list,
Packit 4a16fb
				name);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	device = find_device(uc_mgr, verb, name, 0);
Packit 4a16fb
	if (device) {
Packit 4a16fb
		if (device->dev_list.type != type)
Packit 4a16fb
			return 0;
Packit 4a16fb
		return get_list(&device->dev_list.list, list,
Packit 4a16fb
				struct dev_list_node, list,
Packit 4a16fb
				name);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of supported devices
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param name Name of verb or modifier to query
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_supported_device_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
				     const char **list[], char *name)
Packit 4a16fb
{
Packit 4a16fb
	return get_supcon_device_list(uc_mgr, list, name, DEVLIST_SUPPORTED);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of conflicting devices
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param name Name of verb or modifier to query
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_conflicting_device_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
				       const char **list[], char *name)
Packit 4a16fb
{
Packit 4a16fb
	return get_supcon_device_list(uc_mgr, list, name, DEVLIST_CONFLICTING);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
struct myvalue {
Packit 4a16fb
        struct list_head list;
Packit 4a16fb
        char *value;
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int add_values(struct list_head *list,
Packit 4a16fb
                      const char *identifier,
Packit 4a16fb
                      struct list_head *source)
Packit 4a16fb
{
Packit 4a16fb
        struct ucm_value *v;
Packit 4a16fb
        struct myvalue *val;
Packit 4a16fb
        struct list_head *pos, *pos1;
Packit 4a16fb
        int match;
Packit 4a16fb
        
Packit 4a16fb
        list_for_each(pos, source) {
Packit 4a16fb
                v = list_entry(pos, struct ucm_value, list);
Packit 4a16fb
                if (check_identifier(identifier, v->name)) {
Packit 4a16fb
                        match = 0;
Packit 4a16fb
                        list_for_each(pos1, list) {
Packit 4a16fb
                                val = list_entry(pos1, struct myvalue, list);
Packit 4a16fb
                                if (strcmp(val->value, v->data) == 0) {
Packit 4a16fb
                                        match = 1;
Packit 4a16fb
                                        break;
Packit 4a16fb
                                }
Packit 4a16fb
                        }
Packit 4a16fb
                        if (!match) {
Packit 4a16fb
                                val = malloc(sizeof(struct myvalue));
Packit 4a16fb
                                if (val == NULL)
Packit 4a16fb
                                        return -ENOMEM;
Packit 4a16fb
				val->value = v->data;
Packit 4a16fb
                                list_add_tail(&val->list, list);
Packit 4a16fb
                        }
Packit 4a16fb
                }
Packit 4a16fb
        }
Packit 4a16fb
        return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of values
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param verbname For verb (NULL = current)
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_value_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                          const char *identifier,
Packit 4a16fb
                          const char **list[],
Packit 4a16fb
                          char *verbname)
Packit 4a16fb
{
Packit 4a16fb
        struct list_head mylist, *pos, *npos;
Packit 4a16fb
        struct myvalue *val;
Packit 4a16fb
        struct use_case_verb *verb;
Packit 4a16fb
        struct use_case_device *dev;
Packit 4a16fb
        struct use_case_modifier *mod;
Packit 4a16fb
        char **res;
Packit 4a16fb
        int err;
Packit 4a16fb
        
Packit 4a16fb
        if (verbname) {
Packit 4a16fb
                verb = find_verb(uc_mgr, verbname);
Packit 4a16fb
        } else {
Packit 4a16fb
                verb = uc_mgr->active_verb;
Packit 4a16fb
        }
Packit 4a16fb
        if (verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        INIT_LIST_HEAD(&mylist);
Packit 4a16fb
	err = add_values(&mylist, identifier, &uc_mgr->value_list);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		goto __fail;
Packit 4a16fb
        err = add_values(&mylist, identifier, &verb->value_list);
Packit 4a16fb
        if (err < 0)
Packit 4a16fb
                goto __fail;
Packit 4a16fb
        list_for_each(pos, &verb->device_list) {
Packit 4a16fb
                dev = list_entry(pos, struct use_case_device, list);
Packit 4a16fb
                err = add_values(&mylist, identifier, &dev->value_list);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        goto __fail;
Packit 4a16fb
        }
Packit 4a16fb
        list_for_each(pos, &verb->modifier_list) {
Packit 4a16fb
                mod = list_entry(pos, struct use_case_modifier, list);
Packit 4a16fb
                err = add_values(&mylist, identifier, &mod->value_list);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        goto __fail;
Packit 4a16fb
        }
Packit 4a16fb
        err = alloc_str_list(&mylist, 1, &res;;
Packit 4a16fb
        if (err >= 0) {
Packit 4a16fb
	        *list = (const char **)res;
Packit 4a16fb
                list_for_each(pos, &mylist) {
Packit 4a16fb
                        val = list_entry(pos, struct myvalue, list);
Packit 4a16fb
                        *res = strdup(val->value);
Packit 4a16fb
                        if (*res == NULL) {
Packit 4a16fb
                                snd_use_case_free_list((const char **)res, err);
Packit 4a16fb
                                err = -ENOMEM;
Packit 4a16fb
                                goto __fail;
Packit 4a16fb
                        }
Packit 4a16fb
                        res++;
Packit 4a16fb
                }
Packit 4a16fb
        }
Packit 4a16fb
      __fail:
Packit 4a16fb
        list_for_each_safe(pos, npos, &mylist) {
Packit 4a16fb
                val = list_entry(pos, struct myvalue, list);
Packit 4a16fb
                list_del(&val->list);
Packit 4a16fb
                free(val);
Packit 4a16fb
        }
Packit 4a16fb
        return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of enabled devices
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param verbname For verb (NULL = current)
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_enabled_device_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                                   const char **list[])
Packit 4a16fb
{
Packit 4a16fb
        if (uc_mgr->active_verb == NULL)
Packit 4a16fb
                return -EINVAL;
Packit 4a16fb
        return get_list(&uc_mgr->active_devices, list,
Packit 4a16fb
                        struct use_case_device, active_list,
Packit 4a16fb
                        name);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get list of enabled modifiers
Packit 4a16fb
 * \param list Returned list
Packit 4a16fb
 * \param verbname For verb (NULL = current)
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_enabled_modifier_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                                     const char **list[])
Packit 4a16fb
{
Packit 4a16fb
        if (uc_mgr->active_verb == NULL)
Packit 4a16fb
                return -EINVAL;
Packit 4a16fb
        return get_list(&uc_mgr->active_modifiers, list,
Packit 4a16fb
                        struct use_case_modifier, active_list,
Packit 4a16fb
                        name);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Obtain a list of entries
Packit 4a16fb
 * \param uc_mgr Use case manager (may be NULL - card list)
Packit 4a16fb
 * \param identifier (may be NULL - card list)
Packit 4a16fb
 * \param list Returned allocated list
Packit 4a16fb
 * \return Number of list entries if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_get_list(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
			  const char *identifier,
Packit 4a16fb
			  const char **list[])
Packit 4a16fb
{
Packit 4a16fb
	char *str, *str1;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (uc_mgr == NULL || identifier == NULL)
Packit 4a16fb
		return uc_mgr_scan_master_configs(list);
Packit 4a16fb
	pthread_mutex_lock(&uc_mgr->mutex);
Packit 4a16fb
	if (strcmp(identifier, "_verbs") == 0)
Packit 4a16fb
		err = get_verb_list(uc_mgr, list);
Packit 4a16fb
        else if (strcmp(identifier, "_enadevs") == 0)
Packit 4a16fb
        	err = get_enabled_device_list(uc_mgr, list);
Packit 4a16fb
        else if (strcmp(identifier, "_enamods") == 0)
Packit 4a16fb
                err = get_enabled_modifier_list(uc_mgr, list);
Packit 4a16fb
        else {
Packit 4a16fb
                str1 = strchr(identifier, '/');
Packit 4a16fb
                if (str1) {
Packit 4a16fb
                        str = strdup(str1 + 1);
Packit 4a16fb
                	if (str == NULL) {
Packit 4a16fb
                  		err = -ENOMEM;
Packit 4a16fb
                		goto __end;
Packit 4a16fb
                        }
Packit 4a16fb
                } else {
Packit 4a16fb
                        str = NULL;
Packit 4a16fb
                }
Packit 4a16fb
        	if (check_identifier(identifier, "_devices"))
Packit 4a16fb
          		err = get_device_list(uc_mgr, list, str);
Packit 4a16fb
                else if (check_identifier(identifier, "_modifiers"))
Packit 4a16fb
                        err = get_modifier_list(uc_mgr, list, str);
Packit 4a16fb
                else if (check_identifier(identifier, "_supporteddevs"))
Packit 4a16fb
                        err = get_supported_device_list(uc_mgr, list, str);
Packit 4a16fb
                else if (check_identifier(identifier, "_conflictingdevs"))
Packit 4a16fb
                        err = get_conflicting_device_list(uc_mgr, list, str);
Packit 4a16fb
		else if (identifier[0] == '_')
Packit 4a16fb
			err = -ENOENT;
Packit 4a16fb
                else
Packit 4a16fb
                        err = get_value_list(uc_mgr, identifier, list, str);
Packit 4a16fb
        	if (str)
Packit 4a16fb
        		free(str);
Packit 4a16fb
        }
Packit 4a16fb
      __end:
Packit 4a16fb
	pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_value1(snd_use_case_mgr_t *uc_mgr, char **value,
Packit 4a16fb
		      struct list_head *value_list, const char *identifier)
Packit 4a16fb
{
Packit 4a16fb
        struct ucm_value *val;
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
        
Packit 4a16fb
	if (!value_list)
Packit 4a16fb
		return -ENOENT;
Packit 4a16fb
Packit 4a16fb
        list_for_each(pos, value_list) {
Packit 4a16fb
		val = list_entry(pos, struct ucm_value, list);
Packit 4a16fb
		if (check_identifier(identifier, val->name)) {
Packit 4a16fb
			if (uc_mgr->conf_format < 2) {
Packit 4a16fb
				*value = strdup(val->data);
Packit 4a16fb
				if (*value == NULL)
Packit 4a16fb
					return -ENOMEM;
Packit 4a16fb
				return 0;
Packit 4a16fb
			}
Packit 4a16fb
			return uc_mgr_get_substituted_value(uc_mgr, value, val->data);
Packit 4a16fb
		}
Packit 4a16fb
        }
Packit 4a16fb
        return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_value3(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
		      char **value,
Packit 4a16fb
		      const char *identifier,
Packit 4a16fb
		      struct list_head *value_list1,
Packit 4a16fb
		      struct list_head *value_list2,
Packit 4a16fb
		      struct list_head *value_list3)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	err = get_value1(uc_mgr, value, value_list1, identifier);
Packit 4a16fb
	if (err >= 0 || err != -ENOENT)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = get_value1(uc_mgr, value, value_list2, identifier);
Packit 4a16fb
	if (err >= 0 || err != -ENOENT)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = get_value1(uc_mgr, value, value_list3, identifier);
Packit 4a16fb
	if (err >= 0 || err != -ENOENT)
Packit 4a16fb
		return err;
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get value
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param identifier Value identifier (string)
Packit 4a16fb
 * \param value Returned value string
Packit 4a16fb
 * \param item Modifier or Device name (string)
Packit 4a16fb
 * \return Zero on success (value is filled), otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
static int get_value(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
			const char *identifier,
Packit 4a16fb
			char **value,
Packit 4a16fb
			const char *mod_dev_name,
Packit 4a16fb
			const char *verb_name,
Packit 4a16fb
			int exact)
Packit 4a16fb
{
Packit 4a16fb
	struct use_case_verb *verb;
Packit 4a16fb
	struct use_case_modifier *mod;
Packit 4a16fb
	struct use_case_device *dev;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (mod_dev_name || verb_name || !exact) {
Packit 4a16fb
		if (verb_name && strlen(verb_name)) {
Packit 4a16fb
			verb = find_verb(uc_mgr, verb_name);
Packit 4a16fb
		} else {
Packit 4a16fb
			verb = uc_mgr->active_verb;
Packit 4a16fb
		}
Packit 4a16fb
		if (verb) {
Packit 4a16fb
			if (mod_dev_name) {
Packit 4a16fb
				mod = find_modifier(uc_mgr, verb,
Packit 4a16fb
						    mod_dev_name, 0);
Packit 4a16fb
				if (mod) {
Packit 4a16fb
					err = get_value1(uc_mgr, value,
Packit 4a16fb
							 &mod->value_list,
Packit 4a16fb
							 identifier);
Packit 4a16fb
					if (err >= 0 || err != -ENOENT)
Packit 4a16fb
						return err;
Packit 4a16fb
				}
Packit 4a16fb
Packit 4a16fb
				dev = find_device(uc_mgr, verb,
Packit 4a16fb
						  mod_dev_name, 0);
Packit 4a16fb
				if (dev) {
Packit 4a16fb
					err = get_value1(uc_mgr, value,
Packit 4a16fb
							 &dev->value_list,
Packit 4a16fb
							 identifier);
Packit 4a16fb
					if (err >= 0 || err != -ENOENT)
Packit 4a16fb
						return err;
Packit 4a16fb
				}
Packit 4a16fb
Packit 4a16fb
				if (exact)
Packit 4a16fb
					return -ENOENT;
Packit 4a16fb
			}
Packit 4a16fb
Packit 4a16fb
			err = get_value1(uc_mgr, value, &verb->value_list, identifier);
Packit 4a16fb
			if (err >= 0 || err != -ENOENT)
Packit 4a16fb
				return err;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		if (exact)
Packit 4a16fb
			return -ENOENT;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	err = get_value1(uc_mgr, value, &uc_mgr->value_list, identifier);
Packit 4a16fb
	if (err >= 0 || err != -ENOENT)
Packit 4a16fb
		return err;
Packit 4a16fb
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get current - string
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param identifier 
Packit 4a16fb
 * \param value Value pointer
Packit 4a16fb
 * \return Zero if success, otherwise a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Note: String is dynamically allocated, use free() to
Packit 4a16fb
 * deallocate this string.
Packit 4a16fb
 */      
Packit 4a16fb
int snd_use_case_get(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
		     const char *identifier,
Packit 4a16fb
		     const char **value)
Packit 4a16fb
{
Packit 4a16fb
	const char *slash1, *slash2, *mod_dev_after;
Packit 4a16fb
	const char *ident, *mod_dev, *verb;
Packit 4a16fb
	int exact = 0;
Packit 4a16fb
        int err;
Packit 4a16fb
Packit 4a16fb
	pthread_mutex_lock(&uc_mgr->mutex);
Packit 4a16fb
	if (identifier == NULL) {
Packit 4a16fb
	        *value = strdup(uc_mgr->card_name);
Packit 4a16fb
	        if (*value == NULL) {
Packit 4a16fb
	                err = -ENOMEM;
Packit 4a16fb
	                goto __end;
Packit 4a16fb
                }
Packit 4a16fb
                err = 0;
Packit 4a16fb
        } else if (strcmp(identifier, "_verb") == 0) {
Packit 4a16fb
                if (uc_mgr->active_verb == NULL) {
Packit 4a16fb
                        err = -ENOENT;
Packit 4a16fb
			goto __end;
Packit 4a16fb
		}
Packit 4a16fb
                *value = strdup(uc_mgr->active_verb->name);
Packit 4a16fb
                if (*value == NULL) {
Packit 4a16fb
                        err = -ENOMEM;
Packit 4a16fb
                        goto __end;
Packit 4a16fb
                }
Packit 4a16fb
	        err = 0;
Packit 4a16fb
	} else if (strcmp(identifier, "_file") == 0) {
Packit 4a16fb
		/* get the conf file name of the opened card */
Packit 4a16fb
		if ((uc_mgr->card_name == NULL)
Packit 4a16fb
		    || (uc_mgr->conf_file_name[0] == '\0')) {
Packit 4a16fb
			err = -ENOENT;
Packit 4a16fb
			goto __end;
Packit 4a16fb
		}
Packit 4a16fb
		*value = strndup(uc_mgr->conf_file_name, MAX_FILE);
Packit 4a16fb
		if (*value == NULL) {
Packit 4a16fb
			err = -ENOMEM;
Packit 4a16fb
			goto __end;
Packit 4a16fb
		}
Packit 4a16fb
		err = 0;
Packit 4a16fb
Packit 4a16fb
	} else if (identifier[0] == '_') {
Packit 4a16fb
		err = -ENOENT;
Packit 4a16fb
		goto __end;
Packit 4a16fb
        } else {
Packit 4a16fb
		if (identifier[0] == '=') {
Packit 4a16fb
			exact = 1;
Packit 4a16fb
			identifier++;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		slash1 = strchr(identifier, '/');
Packit 4a16fb
		if (slash1) {
Packit 4a16fb
			ident = strndup(identifier, slash1 - identifier);
Packit 4a16fb
Packit 4a16fb
			slash2 = strchr(slash1 + 1, '/');
Packit 4a16fb
			if (slash2) {
Packit 4a16fb
				mod_dev_after = slash2;
Packit 4a16fb
				verb = slash2 + 1;
Packit 4a16fb
			}
Packit 4a16fb
			else {
Packit 4a16fb
				mod_dev_after = slash1 + strlen(slash1);
Packit 4a16fb
				verb = NULL;
Packit 4a16fb
			}
Packit 4a16fb
Packit 4a16fb
			if (mod_dev_after == slash1 + 1)
Packit 4a16fb
				mod_dev = NULL;
Packit 4a16fb
			else
Packit 4a16fb
				mod_dev = strndup(slash1 + 1,
Packit 4a16fb
						  mod_dev_after - (slash1 + 1));
Packit 4a16fb
		}
Packit 4a16fb
		else {
Packit 4a16fb
			ident = identifier;
Packit 4a16fb
			mod_dev = NULL;
Packit 4a16fb
			verb = NULL;
Packit 4a16fb
		}
Packit 4a16fb
Packit 4a16fb
		err = get_value(uc_mgr, ident, (char **)value, mod_dev, verb,
Packit 4a16fb
		                exact);
Packit 4a16fb
		if (ident != identifier)
Packit 4a16fb
			free((void *)ident);
Packit 4a16fb
		if (mod_dev)
Packit 4a16fb
			free((void *)mod_dev);
Packit 4a16fb
        }
Packit 4a16fb
      __end:
Packit 4a16fb
	pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
        return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get current - integer
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param identifier 
Packit 4a16fb
 * \return Value if success, otherwise a negative error code 
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_geti(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
		      const char *identifier,
Packit 4a16fb
		      long *value)
Packit 4a16fb
{
Packit 4a16fb
        char *str, *str1;
Packit 4a16fb
        long err;
Packit 4a16fb
Packit 4a16fb
	pthread_mutex_lock(&uc_mgr->mutex);
Packit 4a16fb
        if (0) {
Packit 4a16fb
                /* nothing here - prepared for fixed identifiers */
Packit 4a16fb
        } else {
Packit 4a16fb
                str1 = strchr(identifier, '/');
Packit 4a16fb
                if (str1) {
Packit 4a16fb
                        str = strdup(str1 + 1);
Packit 4a16fb
                	if (str == NULL) {
Packit 4a16fb
                  		err = -ENOMEM;
Packit 4a16fb
                		goto __end;
Packit 4a16fb
                        }
Packit 4a16fb
                } else {
Packit 4a16fb
                        str = NULL;
Packit 4a16fb
                }
Packit 4a16fb
                if (check_identifier(identifier, "_devstatus")) {
Packit 4a16fb
			if (!str) {
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __end;
Packit 4a16fb
			}
Packit 4a16fb
                        err = device_status(uc_mgr, str);
Packit 4a16fb
			if (err >= 0) {
Packit 4a16fb
				*value = err;
Packit 4a16fb
				err = 0;
Packit 4a16fb
			}
Packit 4a16fb
		} else if (check_identifier(identifier, "_modstatus")) {
Packit 4a16fb
			if (!str) {
Packit 4a16fb
				err = -EINVAL;
Packit 4a16fb
				goto __end;
Packit 4a16fb
			}
Packit 4a16fb
                        err = modifier_status(uc_mgr, str);
Packit 4a16fb
			if (err >= 0) {
Packit 4a16fb
				*value = err;
Packit 4a16fb
				err = 0;
Packit 4a16fb
			}
Packit 4a16fb
#if 0
Packit 4a16fb
		/*
Packit 4a16fb
		 * enable this block if the else clause below is expanded to query
Packit 4a16fb
		 * user-supplied values
Packit 4a16fb
		 */
Packit 4a16fb
		} else if (identifier[0] == '_')
Packit 4a16fb
			err = -ENOENT;
Packit 4a16fb
#endif
Packit 4a16fb
		} else
Packit 4a16fb
                        err = -ENOENT;
Packit 4a16fb
                if (str)
Packit 4a16fb
                        free(str);
Packit 4a16fb
        }
Packit 4a16fb
      __end:
Packit 4a16fb
	pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
        return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int handle_transition_verb(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                                  struct use_case_verb *new_verb)
Packit 4a16fb
{
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
        struct transition_sequence *trans;
Packit 4a16fb
        int err;
Packit 4a16fb
Packit 4a16fb
        list_for_each(pos, &uc_mgr->active_verb->transition_list) {
Packit 4a16fb
                trans = list_entry(pos, struct transition_sequence, list);
Packit 4a16fb
                if (strcmp(trans->name, new_verb->name) == 0) {
Packit 4a16fb
                        err = execute_sequence(uc_mgr, &trans->transition_list,
Packit 4a16fb
					       &uc_mgr->active_verb->value_list,
Packit 4a16fb
					       &uc_mgr->value_list,
Packit 4a16fb
					       NULL);
Packit 4a16fb
                        if (err >= 0)
Packit 4a16fb
                                return 1;
Packit 4a16fb
                        return err;
Packit 4a16fb
                }
Packit 4a16fb
        }
Packit 4a16fb
        return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_verb_user(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                         const char *verb_name)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_verb *verb;
Packit 4a16fb
        int err = 0;
Packit 4a16fb
Packit 4a16fb
        if (uc_mgr->active_verb &&
Packit 4a16fb
            strcmp(uc_mgr->active_verb->name, verb_name) == 0)
Packit 4a16fb
                return 0;
Packit 4a16fb
        if (strcmp(verb_name, SND_USE_CASE_VERB_INACTIVE) != 0) {
Packit 4a16fb
                verb = find_verb(uc_mgr, verb_name);
Packit 4a16fb
                if (verb == NULL)
Packit 4a16fb
                        return -ENOENT;
Packit 4a16fb
        } else {
Packit 4a16fb
                verb = NULL;
Packit 4a16fb
        }
Packit 4a16fb
        if (uc_mgr->active_verb) {
Packit 4a16fb
                err = handle_transition_verb(uc_mgr, verb);
Packit 4a16fb
                if (err == 0) {
Packit 4a16fb
                        err = dismantle_use_case(uc_mgr);
Packit 4a16fb
                        if (err < 0)
Packit 4a16fb
                                return err;
Packit 4a16fb
                } else if (err == 1) {
Packit 4a16fb
                        uc_mgr->active_verb = verb;
Packit 4a16fb
                        verb = NULL;
Packit 4a16fb
                } else {
Packit 4a16fb
                        verb = NULL; /* show error */
Packit 4a16fb
                }
Packit 4a16fb
        }
Packit 4a16fb
        if (verb) {
Packit 4a16fb
                err = set_verb(uc_mgr, verb, 1);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        uc_error("error: failed to initialize new use case: %s",
Packit 4a16fb
                                 verb_name);
Packit 4a16fb
        }
Packit 4a16fb
        return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
Packit 4a16fb
static int set_device_user(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                           const char *device_name,
Packit 4a16fb
                           int enable)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_device *device;
Packit 4a16fb
Packit 4a16fb
        if (uc_mgr->active_verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        device = find_device(uc_mgr, uc_mgr->active_verb, device_name, 1);
Packit 4a16fb
        if (device == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        return set_device(uc_mgr, device, enable);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_modifier_user(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                             const char *modifier_name,
Packit 4a16fb
                             int enable)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_modifier *modifier;
Packit 4a16fb
Packit 4a16fb
        if (uc_mgr->active_verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
Packit 4a16fb
        modifier = find_modifier(uc_mgr, uc_mgr->active_verb, modifier_name, 1);
Packit 4a16fb
        if (modifier == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        return set_modifier(uc_mgr, modifier, enable);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int switch_device(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                         const char *old_device,
Packit 4a16fb
                         const char *new_device)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_device *xold, *xnew;
Packit 4a16fb
        struct transition_sequence *trans;
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
        int err, seq_found = 0;
Packit 4a16fb
        
Packit 4a16fb
        if (uc_mgr->active_verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        if (device_status(uc_mgr, old_device) == 0) {
Packit 4a16fb
                uc_error("error: device %s not enabled", old_device);
Packit 4a16fb
                return -EINVAL;
Packit 4a16fb
        }
Packit 4a16fb
        if (device_status(uc_mgr, new_device) != 0) {
Packit 4a16fb
                uc_error("error: device %s already enabled", new_device);
Packit 4a16fb
                return -EINVAL;
Packit 4a16fb
        }
Packit 4a16fb
        xold = find_device(uc_mgr, uc_mgr->active_verb, old_device, 1);
Packit 4a16fb
        if (xold == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        list_del(&xold->active_list);
Packit 4a16fb
        xnew = find_device(uc_mgr, uc_mgr->active_verb, new_device, 1);
Packit 4a16fb
        list_add_tail(&xold->active_list, &uc_mgr->active_devices);
Packit 4a16fb
        if (xnew == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        err = 0;
Packit 4a16fb
        list_for_each(pos, &xold->transition_list) {
Packit 4a16fb
                trans = list_entry(pos, struct transition_sequence, list);
Packit 4a16fb
                if (strcmp(trans->name, new_device) == 0) {
Packit 4a16fb
                        err = execute_sequence(uc_mgr, &trans->transition_list,
Packit 4a16fb
					       &xold->value_list,
Packit 4a16fb
					       &uc_mgr->active_verb->value_list,
Packit 4a16fb
					       &uc_mgr->value_list);
Packit 4a16fb
                        if (err >= 0) {
Packit 4a16fb
                                list_del(&xold->active_list);
Packit 4a16fb
                                list_add_tail(&xnew->active_list, &uc_mgr->active_devices);
Packit 4a16fb
                        }
Packit 4a16fb
                        seq_found = 1;
Packit 4a16fb
                        break;
Packit 4a16fb
                }
Packit 4a16fb
        }
Packit 4a16fb
        if (!seq_found) {
Packit 4a16fb
                err = set_device(uc_mgr, xold, 0);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        return err;
Packit 4a16fb
                err = set_device(uc_mgr, xnew, 1);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        return err;
Packit 4a16fb
        }
Packit 4a16fb
        return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int switch_modifier(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                           const char *old_modifier,
Packit 4a16fb
                           const char *new_modifier)
Packit 4a16fb
{
Packit 4a16fb
        struct use_case_modifier *xold, *xnew;
Packit 4a16fb
        struct transition_sequence *trans;
Packit 4a16fb
        struct list_head *pos;
Packit 4a16fb
        int err, seq_found = 0;
Packit 4a16fb
        
Packit 4a16fb
        if (uc_mgr->active_verb == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        if (modifier_status(uc_mgr, old_modifier) == 0) {
Packit 4a16fb
                uc_error("error: modifier %s not enabled", old_modifier);
Packit 4a16fb
                return -EINVAL;
Packit 4a16fb
        }
Packit 4a16fb
        if (modifier_status(uc_mgr, new_modifier) != 0) {
Packit 4a16fb
                uc_error("error: modifier %s already enabled", new_modifier);
Packit 4a16fb
                return -EINVAL;
Packit 4a16fb
        }
Packit 4a16fb
        xold = find_modifier(uc_mgr, uc_mgr->active_verb, old_modifier, 1);
Packit 4a16fb
        if (xold == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        xnew = find_modifier(uc_mgr, uc_mgr->active_verb, new_modifier, 1);
Packit 4a16fb
        if (xnew == NULL)
Packit 4a16fb
                return -ENOENT;
Packit 4a16fb
        err = 0;
Packit 4a16fb
        list_for_each(pos, &xold->transition_list) {
Packit 4a16fb
                trans = list_entry(pos, struct transition_sequence, list);
Packit 4a16fb
                if (strcmp(trans->name, new_modifier) == 0) {
Packit 4a16fb
                        err = execute_sequence(uc_mgr, &trans->transition_list,
Packit 4a16fb
					       &xold->value_list,
Packit 4a16fb
					       &uc_mgr->active_verb->value_list,
Packit 4a16fb
					       &uc_mgr->value_list);
Packit 4a16fb
                        if (err >= 0) {
Packit 4a16fb
                                list_del(&xold->active_list);
Packit 4a16fb
                                list_add_tail(&xnew->active_list, &uc_mgr->active_modifiers);
Packit 4a16fb
                        }
Packit 4a16fb
                        seq_found = 1;
Packit 4a16fb
                        break;
Packit 4a16fb
                }
Packit 4a16fb
        }
Packit 4a16fb
        if (!seq_found) {
Packit 4a16fb
                err = set_modifier(uc_mgr, xold, 0);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        return err;
Packit 4a16fb
                err = set_modifier(uc_mgr, xnew, 1);
Packit 4a16fb
                if (err < 0)
Packit 4a16fb
                        return err;
Packit 4a16fb
        }
Packit 4a16fb
        return err;        
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Set new
Packit 4a16fb
 * \param uc_mgr Use case manager
Packit 4a16fb
 * \param identifier
Packit 4a16fb
 * \param value Value
Packit 4a16fb
 * \return Zero if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_set(snd_use_case_mgr_t *uc_mgr,
Packit 4a16fb
                     const char *identifier,
Packit 4a16fb
                     const char *value)
Packit 4a16fb
{
Packit 4a16fb
	char *str, *str1;
Packit 4a16fb
	int err = 0;
Packit 4a16fb
Packit 4a16fb
	pthread_mutex_lock(&uc_mgr->mutex);
Packit 4a16fb
	if (strcmp(identifier, "_verb") == 0)
Packit 4a16fb
	        err = set_verb_user(uc_mgr, value);
Packit 4a16fb
        else if (strcmp(identifier, "_enadev") == 0)
Packit 4a16fb
                err = set_device_user(uc_mgr, value, 1);
Packit 4a16fb
        else if (strcmp(identifier, "_disdev") == 0)
Packit 4a16fb
                err = set_device_user(uc_mgr, value, 0);
Packit 4a16fb
        else if (strcmp(identifier, "_enamod") == 0)
Packit 4a16fb
                err = set_modifier_user(uc_mgr, value, 1);
Packit 4a16fb
        else if (strcmp(identifier, "_dismod") == 0)
Packit 4a16fb
                err = set_modifier_user(uc_mgr, value, 0);
Packit 4a16fb
        else {
Packit 4a16fb
                str1 = strchr(identifier, '/');
Packit 4a16fb
                if (str1) {
Packit 4a16fb
                        str = strdup(str1 + 1);
Packit 4a16fb
                	if (str == NULL) {
Packit 4a16fb
                  		err = -ENOMEM;
Packit 4a16fb
                		goto __end;
Packit 4a16fb
                        }
Packit 4a16fb
                } else {
Packit 4a16fb
                        err = -EINVAL;
Packit 4a16fb
                        goto __end;
Packit 4a16fb
                }
Packit 4a16fb
                if (check_identifier(identifier, "_swdev"))
Packit 4a16fb
                        err = switch_device(uc_mgr, str, value);
Packit 4a16fb
                else if (check_identifier(identifier, "_swmod"))
Packit 4a16fb
                        err = switch_modifier(uc_mgr, str, value);
Packit 4a16fb
                else
Packit 4a16fb
                        err = -EINVAL;
Packit 4a16fb
                if (str)
Packit 4a16fb
                        free(str);
Packit 4a16fb
        }
Packit 4a16fb
      __end:
Packit 4a16fb
	pthread_mutex_unlock(&uc_mgr->mutex);
Packit 4a16fb
        return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Parse control element identifier
Packit 4a16fb
 * \param elem_id Element identifier
Packit 4a16fb
 * \param ucm_id Use case identifier
Packit 4a16fb
 * \param value String value to be parsed
Packit 4a16fb
 * \return Zero if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_parse_ctl_elem_id(snd_ctl_elem_id_t *dst,
Packit 4a16fb
				   const char *ucm_id,
Packit 4a16fb
				   const char *value)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_elem_iface_t iface;
Packit 4a16fb
	int jack_control;
Packit 4a16fb
Packit 4a16fb
	jack_control = strcmp(ucm_id, "JackControl") == 0;
Packit 4a16fb
	if (!jack_control &&
Packit 4a16fb
	    strcmp(ucm_id, "PlaybackVolume") &&
Packit 4a16fb
	    strcmp(ucm_id, "PlaybackSwitch") &&
Packit 4a16fb
	    strcmp(ucm_id, "CaptureVolume") &&
Packit 4a16fb
	    strcmp(ucm_id, "CaptureSwitch"))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	snd_ctl_elem_id_clear(dst);
Packit 4a16fb
	if (strcasestr(ucm_id, "name="))
Packit 4a16fb
		return __snd_ctl_ascii_elem_id_parse(dst, value, NULL);
Packit 4a16fb
	iface = SND_CTL_ELEM_IFACE_MIXER;
Packit 4a16fb
	if (jack_control)
Packit 4a16fb
		iface = SND_CTL_ELEM_IFACE_CARD;
Packit 4a16fb
	snd_ctl_elem_id_set_interface(dst, iface);
Packit 4a16fb
	snd_ctl_elem_id_set_name(dst, value);
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Parse mixer element identifier
Packit 4a16fb
 * \param dst Simple mixer element identifier
Packit 4a16fb
 * \param ucm_id Use case identifier
Packit 4a16fb
 * \param value String value to be parsed
Packit 4a16fb
 * \return Zero if success, otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_use_case_parse_selem_id(snd_mixer_selem_id_t *dst,
Packit 4a16fb
				const char *ucm_id,
Packit 4a16fb
				const char *value)
Packit 4a16fb
{
Packit 4a16fb
	if (strcmp(ucm_id, "PlaybackMixerId") == 0 ||
Packit 4a16fb
	    strcmp(ucm_id, "CaptureMixerId") == 0)
Packit 4a16fb
		return snd_mixer_selem_id_parse(dst, value);
Packit 4a16fb
	return -EINVAL;
Packit 4a16fb
}