Blame src/control/tlv.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file control/tlv.c
Packit 4a16fb
 * \brief dB conversion functions from control TLV information
Packit 4a16fb
 * \author Takashi Iwai <tiwai@suse.de>
Packit 4a16fb
 * \date 2007
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Control Interface - dB conversion functions from control TLV information
Packit 4a16fb
 *
Packit 4a16fb
 *  Copyright (c) 2007 Takashi Iwai <tiwai@suse.de>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program 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
Packit 4a16fb
 *   GNU 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
 */
Packit 4a16fb
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#ifndef HAVE_SOFT_FLOAT
Packit 4a16fb
#include <math.h>
Packit 4a16fb
#endif
Packit 4a16fb
#include "control_local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
/* convert to index of integer array */
Packit 4a16fb
#define int_index(size)	(((size) + sizeof(int) - 1) / sizeof(int))
Packit 4a16fb
/* max size of a TLV entry for dB information (including compound one) */
Packit 4a16fb
#define MAX_TLV_RANGE_SIZE	256
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Parse TLV stream and retrieve dB information
Packit 4a16fb
 * \param tlv the TLV source
Packit 4a16fb
 * \param tlv_size the byte size of TLV source
Packit 4a16fb
 * \param db_tlvp the pointer stored the dB TLV information
Packit 4a16fb
 * \return the byte size of dB TLV information if found in the given
Packit 4a16fb
 *   TLV source, or a negative error code.
Packit 4a16fb
 *
Packit 4a16fb
 * This function parses the given TLV source and stores the TLV start
Packit 4a16fb
 * point if the TLV information regarding dB conversion is found.
Packit 4a16fb
 * The stored TLV pointer can be passed to the convesion functions
Packit 4a16fb
 * #snd_tlv_convert_to_dB(), #snd_tlv_convert_from_dB() and
Packit 4a16fb
 * #snd_tlv_get_dB_range().
Packit 4a16fb
 */
Packit 4a16fb
int snd_tlv_parse_dB_info(unsigned int *tlv,
Packit 4a16fb
			  unsigned int tlv_size,
Packit 4a16fb
			  unsigned int **db_tlvp)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int type;
Packit 4a16fb
	unsigned int size;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	*db_tlvp = NULL;
Packit 4a16fb
	type = tlv[SNDRV_CTL_TLVO_TYPE];
Packit 4a16fb
	size = tlv[SNDRV_CTL_TLVO_LEN];
Packit 4a16fb
	tlv_size -= 2 * sizeof(int);
Packit 4a16fb
	if (size > tlv_size) {
Packit 4a16fb
		SNDERR("TLV size error");
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	switch (type) {
Packit 4a16fb
	case SND_CTL_TLVT_CONTAINER:
Packit 4a16fb
		size = int_index(size) * sizeof(int);
Packit 4a16fb
		tlv += 2;
Packit 4a16fb
		while (size > 0) {
Packit 4a16fb
			unsigned int len;
Packit 4a16fb
			err = snd_tlv_parse_dB_info(tlv, size, db_tlvp);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				return err; /* error */
Packit 4a16fb
			if (err > 0)
Packit 4a16fb
				return err; /* found */
Packit 4a16fb
			len = int_index(tlv[SNDRV_CTL_TLVO_LEN]) + 2;
Packit 4a16fb
			size -= len * sizeof(int);
Packit 4a16fb
			tlv += len;
Packit 4a16fb
		}
Packit 4a16fb
		break;
Packit 4a16fb
	case SND_CTL_TLVT_DB_SCALE:
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX:
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX_MUTE:
Packit 4a16fb
#ifndef HAVE_SOFT_FLOAT
Packit 4a16fb
	case SND_CTL_TLVT_DB_LINEAR:
Packit 4a16fb
#endif
Packit 4a16fb
	case SND_CTL_TLVT_DB_RANGE: {
Packit 4a16fb
		unsigned int minsize;
Packit 4a16fb
		if (type == SND_CTL_TLVT_DB_RANGE)
Packit 4a16fb
			minsize = 4 * sizeof(int);
Packit 4a16fb
		else
Packit 4a16fb
			minsize = 2 * sizeof(int);
Packit 4a16fb
		if (size < minsize) {
Packit 4a16fb
			SNDERR("Invalid dB_scale TLV size");
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		}
Packit 4a16fb
		if (size > MAX_TLV_RANGE_SIZE) {
Packit 4a16fb
			SNDERR("Too big dB_scale TLV size: %d", size);
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		}
Packit 4a16fb
		*db_tlvp = tlv;
Packit 4a16fb
		return size + sizeof(int) * 2;
Packit 4a16fb
	}
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	return -EINVAL; /* not found */
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get the dB min/max values
Packit 4a16fb
 * \param tlv the TLV source returned by #snd_tlv_parse_dB_info()
Packit 4a16fb
 * \param rangemin the minimum value of the raw volume
Packit 4a16fb
 * \param rangemax the maximum value of the raw volume
Packit 4a16fb
 * \param min the pointer to store the minimum dB value (in 0.01dB unit)
Packit 4a16fb
 * \param max the pointer to store the maximum dB value (in 0.01dB unit)
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_tlv_get_dB_range(unsigned int *tlv, long rangemin, long rangemax,
Packit 4a16fb
			 long *min, long *max)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	switch (tlv[SNDRV_CTL_TLVO_TYPE]) {
Packit 4a16fb
	case SND_CTL_TLVT_DB_RANGE: {
Packit 4a16fb
		unsigned int pos, len;
Packit 4a16fb
		len = int_index(tlv[SNDRV_CTL_TLVO_LEN]);
Packit 4a16fb
		if (len > MAX_TLV_RANGE_SIZE)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		pos = 2;
Packit 4a16fb
		while (pos + 4 <= len) {
Packit 4a16fb
			long rmin, rmax;
Packit 4a16fb
			long submin, submax;
Packit 4a16fb
			submin = (int)tlv[pos];
Packit 4a16fb
			submax = (int)tlv[pos + 1];
Packit 4a16fb
			if (rangemax < submax)
Packit 4a16fb
				submax = rangemax;
Packit 4a16fb
			err = snd_tlv_get_dB_range(tlv + pos + 2,
Packit 4a16fb
						   submin, submax,
Packit 4a16fb
						   &rmin, &rmax);
Packit 4a16fb
			if (err < 0)
Packit 4a16fb
				return err;
Packit 4a16fb
			if (pos > 2) {
Packit 4a16fb
				if (rmin < *min)
Packit 4a16fb
					*min = rmin;
Packit 4a16fb
				if (rmax > *max)
Packit 4a16fb
					*max = rmax;
Packit 4a16fb
			} else {
Packit 4a16fb
				*min = rmin;
Packit 4a16fb
				*max = rmax;
Packit 4a16fb
			}
Packit 4a16fb
			if (rangemax == submax)
Packit 4a16fb
				return 0;
Packit 4a16fb
			pos += int_index(tlv[pos + 3]) + 4;
Packit 4a16fb
		}
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_CTL_TLVT_DB_SCALE: {
Packit 4a16fb
		int step;
Packit 4a16fb
		if (tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 0x10000)
Packit 4a16fb
			*min = SND_CTL_TLV_DB_GAIN_MUTE;
Packit 4a16fb
		else
Packit 4a16fb
			*min = (int)tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN];
Packit 4a16fb
		step = (tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 0xffff);
Packit 4a16fb
		*max = (int)tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] +
Packit 4a16fb
						step * (rangemax - rangemin);
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX:
Packit 4a16fb
	case SND_CTL_TLVT_DB_LINEAR:
Packit 4a16fb
		*min = (int)tlv[SNDRV_CTL_TLVO_DB_LINEAR_MIN];
Packit 4a16fb
		*max = (int)tlv[SNDRV_CTL_TLVO_DB_LINEAR_MAX];
Packit 4a16fb
		return 0;
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX_MUTE:
Packit 4a16fb
		*min = SND_CTL_TLV_DB_GAIN_MUTE;
Packit 4a16fb
		*max = (int)tlv[SNDRV_CTL_TLVO_DB_MINMAX_MAX];
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	return -EINVAL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Convert the given raw volume value to a dB gain
Packit 4a16fb
 * \param tlv the TLV source returned by #snd_tlv_parse_dB_info()
Packit 4a16fb
 * \param rangemin the minimum value of the raw volume
Packit 4a16fb
 * \param rangemax the maximum value of the raw volume
Packit 4a16fb
 * \param volume the raw volume value to convert
Packit 4a16fb
 * \param db_gain the dB gain (in 0.01dB unit)
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_tlv_convert_to_dB(unsigned int *tlv, long rangemin, long rangemax,
Packit 4a16fb
			  long volume, long *db_gain)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int type = tlv[SNDRV_CTL_TLVO_TYPE];
Packit 4a16fb
Packit 4a16fb
	switch (type) {
Packit 4a16fb
	case SND_CTL_TLVT_DB_RANGE: {
Packit 4a16fb
		unsigned int pos, len;
Packit 4a16fb
		len = int_index(tlv[SNDRV_CTL_TLVO_LEN]);
Packit 4a16fb
		if (len > MAX_TLV_RANGE_SIZE)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		pos = 2;
Packit 4a16fb
		while (pos + 4 <= len) {
Packit 4a16fb
			rangemin = (int)tlv[pos];
Packit 4a16fb
			rangemax = (int)tlv[pos + 1];
Packit 4a16fb
			if (volume >= rangemin && volume <= rangemax)
Packit 4a16fb
				return snd_tlv_convert_to_dB(tlv + pos + 2,
Packit 4a16fb
							     rangemin, rangemax,
Packit 4a16fb
							     volume, db_gain);
Packit 4a16fb
			pos += int_index(tlv[pos + 3]) + 4;
Packit 4a16fb
		}
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_CTL_TLVT_DB_SCALE: {
Packit 4a16fb
		int min, step, mute;
Packit 4a16fb
		min = tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN];
Packit 4a16fb
		step = (tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 0xffff);
Packit 4a16fb
		mute = (tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] >> 16) & 1;
Packit 4a16fb
		if (mute && volume <= rangemin)
Packit 4a16fb
			*db_gain = SND_CTL_TLV_DB_GAIN_MUTE;
Packit 4a16fb
		else
Packit 4a16fb
			*db_gain = (volume - rangemin) * step + min;
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX:
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX_MUTE: {
Packit 4a16fb
		int mindb, maxdb;
Packit 4a16fb
		mindb = tlv[SNDRV_CTL_TLVO_DB_MINMAX_MIN];
Packit 4a16fb
		maxdb = tlv[SNDRV_CTL_TLVO_DB_MINMAX_MAX];
Packit 4a16fb
		if (volume <= rangemin || rangemax <= rangemin) {
Packit 4a16fb
			if (type == SND_CTL_TLVT_DB_MINMAX_MUTE)
Packit 4a16fb
				*db_gain = SND_CTL_TLV_DB_GAIN_MUTE;
Packit 4a16fb
			else
Packit 4a16fb
				*db_gain = mindb;
Packit 4a16fb
		} else if (volume >= rangemax)
Packit 4a16fb
			*db_gain = maxdb;
Packit 4a16fb
		else
Packit 4a16fb
			*db_gain = (maxdb - mindb) * (volume - rangemin) /
Packit 4a16fb
				(rangemax - rangemin) + mindb;
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
#ifndef HAVE_SOFT_FLOAT
Packit 4a16fb
	case SND_CTL_TLVT_DB_LINEAR: {
Packit 4a16fb
		int mindb = tlv[SNDRV_CTL_TLVO_DB_LINEAR_MIN];
Packit 4a16fb
		int maxdb = tlv[SNDRV_CTL_TLVO_DB_LINEAR_MAX];
Packit 4a16fb
		if (volume <= rangemin || rangemax <= rangemin)
Packit 4a16fb
			*db_gain = mindb;
Packit 4a16fb
		else if (volume >= rangemax)
Packit 4a16fb
			*db_gain = maxdb;
Packit 4a16fb
		else {
Packit 4a16fb
			double val = (double)(volume - rangemin) /
Packit 4a16fb
				(double)(rangemax - rangemin);
Packit 4a16fb
			if (mindb <= SND_CTL_TLV_DB_GAIN_MUTE)
Packit 4a16fb
				*db_gain = (long)(100.0 * 20.0 * log10(val)) +
Packit 4a16fb
					maxdb;
Packit 4a16fb
			else {
Packit 4a16fb
				/* FIXME: precalculate and cache these values */
Packit 4a16fb
				double lmin = pow(10.0, mindb/2000.0);
Packit 4a16fb
				double lmax = pow(10.0, maxdb/2000.0);
Packit 4a16fb
				val = (lmax - lmin) * val + lmin;
Packit 4a16fb
				*db_gain = (long)(100.0 * 20.0 * log10(val));
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	}
Packit 4a16fb
	return -EINVAL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Convert from dB gain to the corresponding raw value
Packit 4a16fb
 * \param tlv the TLV source returned by #snd_tlv_parse_dB_info()
Packit 4a16fb
 * \param rangemin the minimum value of the raw volume
Packit 4a16fb
 * \param rangemax the maximum value of the raw volume
Packit 4a16fb
 * \param db_gain the dB gain to convert (in 0.01dB unit)
Packit 4a16fb
 * \param value the pointer to store the converted raw volume value
Packit 4a16fb
 * \param xdir the direction for round-up. The value is round up
Packit 4a16fb
 *        when this is positive.
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_tlv_convert_from_dB(unsigned int *tlv, long rangemin, long rangemax,
Packit 4a16fb
			    long db_gain, long *value, int xdir)
Packit 4a16fb
{
Packit 4a16fb
	unsigned int type = tlv[SNDRV_CTL_TLVO_TYPE];
Packit 4a16fb
Packit 4a16fb
	switch (type) {
Packit 4a16fb
	case SND_CTL_TLVT_DB_RANGE: {
Packit 4a16fb
		long dbmin, dbmax, prev_submax;
Packit 4a16fb
		unsigned int pos, len;
Packit 4a16fb
		len = int_index(tlv[SNDRV_CTL_TLVO_LEN]);
Packit 4a16fb
		if (len < 6 || len > MAX_TLV_RANGE_SIZE)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		pos = 2;
Packit 4a16fb
		prev_submax = 0;
Packit 4a16fb
		while (pos + 4 <= len) {
Packit 4a16fb
			long submin, submax;
Packit 4a16fb
			submin = (int)tlv[pos];
Packit 4a16fb
			submax = (int)tlv[pos + 1];
Packit 4a16fb
			if (rangemax < submax)
Packit 4a16fb
				submax = rangemax;
Packit 4a16fb
			if (!snd_tlv_get_dB_range(tlv + pos + 2,
Packit 4a16fb
						  submin, submax,
Packit 4a16fb
						  &dbmin, &dbmax) &&
Packit 4a16fb
			    db_gain >= dbmin && db_gain <= dbmax)
Packit 4a16fb
				return snd_tlv_convert_from_dB(tlv + pos + 2,
Packit 4a16fb
							       submin, submax,
Packit 4a16fb
							       db_gain, value, xdir);
Packit 4a16fb
			else if (db_gain < dbmin) {
Packit 4a16fb
				*value = xdir > 0 || pos == 2 ? submin : prev_submax;
Packit 4a16fb
				return 0;
Packit 4a16fb
			}
Packit 4a16fb
			prev_submax = submax;
Packit 4a16fb
			if (rangemax == submax)
Packit 4a16fb
				break;
Packit 4a16fb
			pos += int_index(tlv[pos + 3]) + 4;
Packit 4a16fb
		}
Packit 4a16fb
		*value = prev_submax;
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_CTL_TLVT_DB_SCALE: {
Packit 4a16fb
		int min, step, max, mute;
Packit 4a16fb
		min = tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN];
Packit 4a16fb
		step = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 0xffff;
Packit 4a16fb
		mute = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] & 0x10000;
Packit 4a16fb
		max = min + (int)(step * (rangemax - rangemin));
Packit 4a16fb
		if (db_gain <= min)
Packit 4a16fb
			if (db_gain > SND_CTL_TLV_DB_GAIN_MUTE && xdir > 0 &&
Packit 4a16fb
			    mute)
Packit 4a16fb
				*value = rangemin + 1;
Packit 4a16fb
			else
Packit 4a16fb
				*value = rangemin;
Packit 4a16fb
		else if (db_gain >= max)
Packit 4a16fb
			*value = rangemax;
Packit 4a16fb
		else {
Packit 4a16fb
			long v = (db_gain - min) * (rangemax - rangemin);
Packit 4a16fb
			if (xdir > 0)
Packit 4a16fb
				v += (max - min) - 1;
Packit 4a16fb
			v = v / (max - min) + rangemin;
Packit 4a16fb
			*value = v;
Packit 4a16fb
		}
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX:
Packit 4a16fb
	case SND_CTL_TLVT_DB_MINMAX_MUTE: {
Packit 4a16fb
		int min, max;
Packit 4a16fb
		min = tlv[SNDRV_CTL_TLVO_DB_MINMAX_MIN];
Packit 4a16fb
		max = tlv[SNDRV_CTL_TLVO_DB_MINMAX_MAX];
Packit 4a16fb
		if (db_gain <= min)
Packit 4a16fb
			if (db_gain > SND_CTL_TLV_DB_GAIN_MUTE && xdir > 0 &&
Packit 4a16fb
			    type == SND_CTL_TLVT_DB_MINMAX_MUTE)
Packit 4a16fb
				*value = rangemin + 1;
Packit 4a16fb
			else
Packit 4a16fb
				*value = rangemin;
Packit 4a16fb
		else if (db_gain >= max)
Packit 4a16fb
			*value = rangemax;
Packit 4a16fb
		else {
Packit 4a16fb
			long v = (db_gain - min) * (rangemax - rangemin);
Packit 4a16fb
			if (xdir > 0)
Packit 4a16fb
				v += (max - min) - 1;
Packit 4a16fb
			v = v / (max - min) + rangemin;
Packit 4a16fb
			*value = v;
Packit 4a16fb
		}
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
#ifndef HAVE_SOFT_FLOAT
Packit 4a16fb
	case SND_CTL_TLVT_DB_LINEAR: {
Packit 4a16fb
		int min, max;
Packit 4a16fb
		min = tlv[SNDRV_CTL_TLVO_DB_LINEAR_MIN];
Packit 4a16fb
		max = tlv[SNDRV_CTL_TLVO_DB_LINEAR_MAX];
Packit 4a16fb
		if (db_gain <= min)
Packit 4a16fb
			*value = rangemin;
Packit 4a16fb
		else if (db_gain >= max)
Packit 4a16fb
			*value = rangemax;
Packit 4a16fb
		else {
Packit 4a16fb
			/* FIXME: precalculate and cache vmin and vmax */
Packit 4a16fb
			double vmin, vmax, v;
Packit 4a16fb
			vmin = (min <= SND_CTL_TLV_DB_GAIN_MUTE) ? 0.0 :
Packit 4a16fb
				pow(10.0,  (double)min / 2000.0);
Packit 4a16fb
			vmax = !max ? 1.0 : pow(10.0,  (double)max / 2000.0);
Packit 4a16fb
			v = pow(10.0, (double)db_gain / 2000.0);
Packit 4a16fb
			v = (v - vmin) * (rangemax - rangemin) / (vmax - vmin);
Packit 4a16fb
			if (xdir > 0)
Packit 4a16fb
				v = ceil(v);
Packit 4a16fb
			*value = (long)v + rangemin;
Packit 4a16fb
		}
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	default:
Packit 4a16fb
		break;
Packit 4a16fb
	}
Packit 4a16fb
	return -EINVAL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#define TEMP_TLV_SIZE		4096
Packit 4a16fb
struct tlv_info {
Packit 4a16fb
	long minval, maxval;
Packit 4a16fb
	unsigned int *tlv;
Packit 4a16fb
	unsigned int buf[TEMP_TLV_SIZE];
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int get_tlv_info(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
Packit 4a16fb
			struct tlv_info *rec)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_elem_info_t info = {0};
Packit 4a16fb
	int err;
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
		return err;
Packit 4a16fb
	if (!snd_ctl_elem_info_is_tlv_readable(&info))
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if (snd_ctl_elem_info_get_type(&info) != SND_CTL_ELEM_TYPE_INTEGER)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	rec->minval = snd_ctl_elem_info_get_min(&info;;
Packit 4a16fb
	rec->maxval = snd_ctl_elem_info_get_max(&info;;
Packit 4a16fb
	err = snd_ctl_elem_tlv_read(ctl, id, rec->buf, sizeof(rec->buf));
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	err = snd_tlv_parse_dB_info(rec->buf, sizeof(rec->buf), &rec->tlv);
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get the dB min/max values on the given control element
Packit 4a16fb
 * \param ctl the control handler
Packit 4a16fb
 * \param id the element id
Packit 4a16fb
 * \param min the pointer to store the minimum dB value (in 0.01dB unit)
Packit 4a16fb
 * \param max the pointer to store the maximum dB value (in 0.01dB unit)
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_ctl_get_dB_range(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
Packit 4a16fb
			 long *min, long *max)
Packit 4a16fb
{
Packit 4a16fb
	struct tlv_info info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	err = get_tlv_info(ctl, id, &info;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	return snd_tlv_get_dB_range(info.tlv, info.minval, info.maxval,
Packit 4a16fb
				    min, max);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Convert the volume value to dB on the given control element
Packit 4a16fb
 * \param ctl the control handler
Packit 4a16fb
 * \param id the element id
Packit 4a16fb
 * \param volume the raw volume value to convert
Packit 4a16fb
 * \param db_gain the dB gain (in 0.01dB unit)
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_ctl_convert_to_dB(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
Packit 4a16fb
			  long volume, long *db_gain)
Packit 4a16fb
{
Packit 4a16fb
	struct tlv_info info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	err = get_tlv_info(ctl, id, &info;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	return snd_tlv_convert_to_dB(info.tlv, info.minval, info.maxval,
Packit 4a16fb
				     volume, db_gain);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Convert from dB gain to the raw volume value on the given control element
Packit 4a16fb
 * \param ctl the control handler
Packit 4a16fb
 * \param id the element id
Packit 4a16fb
 * \param db_gain the dB gain to convert (in 0.01dB unit)
Packit 4a16fb
 * \param value the pointer to store the converted raw volume value
Packit 4a16fb
 * \param xdir the direction for round-up. The value is round up
Packit 4a16fb
 *        when this is positive.
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_ctl_convert_from_dB(snd_ctl_t *ctl, const snd_ctl_elem_id_t *id,
Packit 4a16fb
			    long db_gain, long *value, int xdir)
Packit 4a16fb
{
Packit 4a16fb
	struct tlv_info info;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	err = get_tlv_info(ctl, id, &info;;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	return snd_tlv_convert_from_dB(info.tlv, info.minval, info.maxval,
Packit 4a16fb
				       db_gain, value, xdir);
Packit 4a16fb
}