xref: /dflybsd-src/sys/dev/sound/pcm/feeder_chain.c (revision c4356965e2cccb03602c91a7894423e0b16aa5ff)
12a1ad637SFrançois Tigeot /*-
22a1ad637SFrançois Tigeot  * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
32a1ad637SFrançois Tigeot  * All rights reserved.
42a1ad637SFrançois Tigeot  *
52a1ad637SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
62a1ad637SFrançois Tigeot  * modification, are permitted provided that the following conditions
72a1ad637SFrançois Tigeot  * are met:
82a1ad637SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
92a1ad637SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer.
102a1ad637SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
112a1ad637SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
122a1ad637SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
132a1ad637SFrançois Tigeot  *
142a1ad637SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152a1ad637SFrançois Tigeot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162a1ad637SFrançois Tigeot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172a1ad637SFrançois Tigeot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182a1ad637SFrançois Tigeot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192a1ad637SFrançois Tigeot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202a1ad637SFrançois Tigeot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212a1ad637SFrançois Tigeot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222a1ad637SFrançois Tigeot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232a1ad637SFrançois Tigeot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242a1ad637SFrançois Tigeot  * SUCH DAMAGE.
252a1ad637SFrançois Tigeot  */
262a1ad637SFrançois Tigeot 
272a1ad637SFrançois Tigeot #ifdef HAVE_KERNEL_OPTION_HEADERS
282a1ad637SFrançois Tigeot #include "opt_snd.h"
292a1ad637SFrançois Tigeot #endif
302a1ad637SFrançois Tigeot 
312a1ad637SFrançois Tigeot #include <dev/sound/pcm/sound.h>
322a1ad637SFrançois Tigeot 
332a1ad637SFrançois Tigeot #include "feeder_if.h"
342a1ad637SFrançois Tigeot 
352a1ad637SFrançois Tigeot SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder_chain.c 267992 2014-06-28 03:56:17Z hselasky $");
362a1ad637SFrançois Tigeot 
372a1ad637SFrançois Tigeot /* chain state */
382a1ad637SFrançois Tigeot struct feeder_chain_state {
392a1ad637SFrançois Tigeot 	uint32_t afmt;				/* audio format */
402a1ad637SFrançois Tigeot 	uint32_t rate;				/* sampling rate */
412a1ad637SFrançois Tigeot 	struct pcmchan_matrix *matrix;		/* matrix map */
422a1ad637SFrançois Tigeot };
432a1ad637SFrançois Tigeot 
442a1ad637SFrançois Tigeot /*
452a1ad637SFrançois Tigeot  * chain descriptor that will be passed around from the beginning until the
462a1ad637SFrançois Tigeot  * end of chain process.
472a1ad637SFrançois Tigeot  */
482a1ad637SFrançois Tigeot struct feeder_chain_desc {
492a1ad637SFrançois Tigeot 	struct feeder_chain_state origin;	/* original state */
502a1ad637SFrançois Tigeot 	struct feeder_chain_state current;	/* current state */
512a1ad637SFrançois Tigeot 	struct feeder_chain_state target;	/* target state */
522a1ad637SFrançois Tigeot 	struct pcm_feederdesc desc;		/* feeder descriptor */
532a1ad637SFrançois Tigeot 	uint32_t afmt_ne;			/* prefered native endian */
542a1ad637SFrançois Tigeot 	int mode;				/* chain mode */
552a1ad637SFrançois Tigeot 	int use_eq;				/* need EQ? */
562a1ad637SFrançois Tigeot 	int use_matrix;				/* need channel matrixing? */
572a1ad637SFrançois Tigeot 	int use_volume;				/* need softpcmvol? */
582a1ad637SFrançois Tigeot 	int dummy;				/* dummy passthrough */
592a1ad637SFrançois Tigeot 	int expensive;				/* possibly expensive */
602a1ad637SFrançois Tigeot };
612a1ad637SFrançois Tigeot 
622a1ad637SFrançois Tigeot #define FEEDER_CHAIN_LEAN		0
632a1ad637SFrançois Tigeot #define FEEDER_CHAIN_16			1
642a1ad637SFrançois Tigeot #define FEEDER_CHAIN_32			2
652a1ad637SFrançois Tigeot #define FEEDER_CHAIN_MULTI		3
662a1ad637SFrançois Tigeot #define FEEDER_CHAIN_FULLMULTI		4
672a1ad637SFrançois Tigeot #define FEEDER_CHAIN_LAST		5
682a1ad637SFrançois Tigeot 
692a1ad637SFrançois Tigeot #if defined(SND_FEEDER_FULL_MULTIFORMAT)
702a1ad637SFrançois Tigeot #define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_FULLMULTI
712a1ad637SFrançois Tigeot #elif defined(SND_FEEDER_MULTIFORMAT)
722a1ad637SFrançois Tigeot #define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_MULTI
732a1ad637SFrançois Tigeot #else
742a1ad637SFrançois Tigeot #define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_LEAN
752a1ad637SFrançois Tigeot #endif
762a1ad637SFrançois Tigeot 
772a1ad637SFrançois Tigeot /*
782a1ad637SFrançois Tigeot  * List of prefered formats that might be required during
792a1ad637SFrançois Tigeot  * processing. It will be decided through snd_fmtbest().
802a1ad637SFrançois Tigeot  */
812a1ad637SFrançois Tigeot 
822a1ad637SFrançois Tigeot /* 'Lean' mode, signed 16 or 32 bit native endian. */
832a1ad637SFrançois Tigeot static uint32_t feeder_chain_formats_lean[] = {
842a1ad637SFrançois Tigeot 	AFMT_S16_NE, AFMT_S32_NE,
852a1ad637SFrançois Tigeot 	0
862a1ad637SFrançois Tigeot };
872a1ad637SFrançois Tigeot 
882a1ad637SFrançois Tigeot /* Force everything to signed 16 bit native endian. */
892a1ad637SFrançois Tigeot static uint32_t feeder_chain_formats_16[] = {
902a1ad637SFrançois Tigeot 	AFMT_S16_NE,
912a1ad637SFrançois Tigeot 	0
922a1ad637SFrançois Tigeot };
932a1ad637SFrançois Tigeot 
942a1ad637SFrançois Tigeot /* Force everything to signed 32 bit native endian. */
952a1ad637SFrançois Tigeot static uint32_t feeder_chain_formats_32[] = {
962a1ad637SFrançois Tigeot 	AFMT_S32_NE,
972a1ad637SFrançois Tigeot 	0
982a1ad637SFrançois Tigeot };
992a1ad637SFrançois Tigeot 
1002a1ad637SFrançois Tigeot /* Multiple choices, all except 8 bit. */
1012a1ad637SFrançois Tigeot static uint32_t feeder_chain_formats_multi[] = {
1022a1ad637SFrançois Tigeot 	AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE,
1032a1ad637SFrançois Tigeot 	AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE,
1042a1ad637SFrançois Tigeot 	AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE,
1052a1ad637SFrançois Tigeot 	0
1062a1ad637SFrançois Tigeot };
1072a1ad637SFrançois Tigeot 
1082a1ad637SFrançois Tigeot /* Everything that is convertible. */
1092a1ad637SFrançois Tigeot static uint32_t feeder_chain_formats_fullmulti[] = {
1102a1ad637SFrançois Tigeot 	AFMT_S8, AFMT_U8,
1112a1ad637SFrançois Tigeot 	AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE,
1122a1ad637SFrançois Tigeot 	AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE,
1132a1ad637SFrançois Tigeot 	AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE,
1142a1ad637SFrançois Tigeot 	0
1152a1ad637SFrançois Tigeot };
1162a1ad637SFrançois Tigeot 
1172a1ad637SFrançois Tigeot static uint32_t *feeder_chain_formats[FEEDER_CHAIN_LAST] = {
1182a1ad637SFrançois Tigeot 	[FEEDER_CHAIN_LEAN]      = feeder_chain_formats_lean,
1192a1ad637SFrançois Tigeot 	[FEEDER_CHAIN_16]        = feeder_chain_formats_16,
1202a1ad637SFrançois Tigeot 	[FEEDER_CHAIN_32]        = feeder_chain_formats_32,
1212a1ad637SFrançois Tigeot 	[FEEDER_CHAIN_MULTI]     = feeder_chain_formats_multi,
1222a1ad637SFrançois Tigeot 	[FEEDER_CHAIN_FULLMULTI] = feeder_chain_formats_fullmulti
1232a1ad637SFrançois Tigeot };
1242a1ad637SFrançois Tigeot 
1252a1ad637SFrançois Tigeot static int feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
1262a1ad637SFrançois Tigeot 
1272a1ad637SFrançois Tigeot #if defined(_KERNEL) && defined(SND_DEBUG) && defined(SND_FEEDER_FULL_MULTIFORMAT)
128*c4356965SFrançois Tigeot TUNABLE_INT("hw.snd.feeder_chain_mode", &feeder_chain_mode);
129*c4356965SFrançois Tigeot SYSCTL_INT(_hw_snd, OID_AUTO, feeder_chain_mode, CTLFLAG_RW,
1302a1ad637SFrançois Tigeot     &feeder_chain_mode, 0,
1312a1ad637SFrançois Tigeot     "feeder chain mode "
1322a1ad637SFrançois Tigeot     "(0=lean, 1=16bit, 2=32bit, 3=multiformat, 4=fullmultiformat)");
1332a1ad637SFrançois Tigeot #endif
1342a1ad637SFrançois Tigeot 
1352a1ad637SFrançois Tigeot /*
1362a1ad637SFrançois Tigeot  * feeder_build_format(): Chain any format converter.
1372a1ad637SFrançois Tigeot  */
1382a1ad637SFrançois Tigeot static int
feeder_build_format(struct pcm_channel * c,struct feeder_chain_desc * cdesc)1392a1ad637SFrançois Tigeot feeder_build_format(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
1402a1ad637SFrançois Tigeot {
1412a1ad637SFrançois Tigeot 	struct feeder_class *fc;
1422a1ad637SFrançois Tigeot 	struct pcm_feederdesc *desc;
1432a1ad637SFrançois Tigeot 	int ret;
1442a1ad637SFrançois Tigeot 
1452a1ad637SFrançois Tigeot 	desc = &(cdesc->desc);
1462a1ad637SFrançois Tigeot 	desc->type = FEEDER_FORMAT;
1472a1ad637SFrançois Tigeot 	desc->in = 0;
1482a1ad637SFrançois Tigeot 	desc->out = 0;
1492a1ad637SFrançois Tigeot 	desc->flags = 0;
1502a1ad637SFrançois Tigeot 
1512a1ad637SFrançois Tigeot 	fc = feeder_getclass(desc);
1522a1ad637SFrançois Tigeot 	if (fc == NULL) {
1532a1ad637SFrançois Tigeot 		device_printf(c->dev,
1542a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_format\n", __func__);
1552a1ad637SFrançois Tigeot 		return (ENOTSUP);
1562a1ad637SFrançois Tigeot 	}
1572a1ad637SFrançois Tigeot 
1582a1ad637SFrançois Tigeot 	desc->in = cdesc->current.afmt;
1592a1ad637SFrançois Tigeot 	desc->out = cdesc->target.afmt;
1602a1ad637SFrançois Tigeot 
1612a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, desc);
1622a1ad637SFrançois Tigeot 	if (ret != 0) {
1632a1ad637SFrançois Tigeot 		device_printf(c->dev,
1642a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_format\n", __func__);
1652a1ad637SFrançois Tigeot 		return (ret);
1662a1ad637SFrançois Tigeot 	}
1672a1ad637SFrançois Tigeot 
1682a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_FORMAT;
1692a1ad637SFrançois Tigeot 
1702a1ad637SFrançois Tigeot 	cdesc->current.afmt = cdesc->target.afmt;
1712a1ad637SFrançois Tigeot 
1722a1ad637SFrançois Tigeot 	return (0);
1732a1ad637SFrançois Tigeot }
1742a1ad637SFrançois Tigeot 
1752a1ad637SFrançois Tigeot /*
1762a1ad637SFrançois Tigeot  * feeder_build_formatne(): Chain format converter that suite best for native
1772a1ad637SFrançois Tigeot  *                          endian format.
1782a1ad637SFrançois Tigeot  */
1792a1ad637SFrançois Tigeot static int
feeder_build_formatne(struct pcm_channel * c,struct feeder_chain_desc * cdesc)1802a1ad637SFrançois Tigeot feeder_build_formatne(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
1812a1ad637SFrançois Tigeot {
1822a1ad637SFrançois Tigeot 	struct feeder_chain_state otarget;
1832a1ad637SFrançois Tigeot 	int ret;
1842a1ad637SFrançois Tigeot 
1852a1ad637SFrançois Tigeot 	if (cdesc->afmt_ne == 0 ||
1862a1ad637SFrançois Tigeot 	    AFMT_ENCODING(cdesc->current.afmt) == cdesc->afmt_ne)
1872a1ad637SFrançois Tigeot 		return (0);
1882a1ad637SFrançois Tigeot 
1892a1ad637SFrançois Tigeot 	otarget = cdesc->target;
1902a1ad637SFrançois Tigeot 	cdesc->target = cdesc->current;
1912a1ad637SFrançois Tigeot 	cdesc->target.afmt = SND_FORMAT(cdesc->afmt_ne,
1922a1ad637SFrançois Tigeot 	    cdesc->current.matrix->channels, cdesc->current.matrix->ext);
1932a1ad637SFrançois Tigeot 
1942a1ad637SFrançois Tigeot 	ret = feeder_build_format(c, cdesc);
1952a1ad637SFrançois Tigeot 	if (ret != 0)
1962a1ad637SFrançois Tigeot 		return (ret);
1972a1ad637SFrançois Tigeot 
1982a1ad637SFrançois Tigeot 	cdesc->target = otarget;
1992a1ad637SFrançois Tigeot 
2002a1ad637SFrançois Tigeot 	return (0);
2012a1ad637SFrançois Tigeot }
2022a1ad637SFrançois Tigeot 
2032a1ad637SFrançois Tigeot /*
2042a1ad637SFrançois Tigeot  * feeder_build_rate(): Chain sample rate converter.
2052a1ad637SFrançois Tigeot  */
2062a1ad637SFrançois Tigeot static int
feeder_build_rate(struct pcm_channel * c,struct feeder_chain_desc * cdesc)2072a1ad637SFrançois Tigeot feeder_build_rate(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
2082a1ad637SFrançois Tigeot {
2092a1ad637SFrançois Tigeot 	struct feeder_class *fc;
2102a1ad637SFrançois Tigeot 	struct pcm_feeder *f;
2112a1ad637SFrançois Tigeot 	struct pcm_feederdesc *desc;
2122a1ad637SFrançois Tigeot 	int ret;
2132a1ad637SFrançois Tigeot 
2142a1ad637SFrançois Tigeot 	ret = feeder_build_formatne(c, cdesc);
2152a1ad637SFrançois Tigeot 	if (ret != 0)
2162a1ad637SFrançois Tigeot 		return (ret);
2172a1ad637SFrançois Tigeot 
2182a1ad637SFrançois Tigeot 	desc = &(cdesc->desc);
2192a1ad637SFrançois Tigeot 	desc->type = FEEDER_RATE;
2202a1ad637SFrançois Tigeot 	desc->in = 0;
2212a1ad637SFrançois Tigeot 	desc->out = 0;
2222a1ad637SFrançois Tigeot 	desc->flags = 0;
2232a1ad637SFrançois Tigeot 
2242a1ad637SFrançois Tigeot 	fc = feeder_getclass(desc);
2252a1ad637SFrançois Tigeot 	if (fc == NULL) {
2262a1ad637SFrançois Tigeot 		device_printf(c->dev,
2272a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_rate\n", __func__);
2282a1ad637SFrançois Tigeot 		return (ENOTSUP);
2292a1ad637SFrançois Tigeot 	}
2302a1ad637SFrançois Tigeot 
2312a1ad637SFrançois Tigeot 	desc->in = cdesc->current.afmt;
2322a1ad637SFrançois Tigeot 	desc->out = desc->in;
2332a1ad637SFrançois Tigeot 
2342a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, desc);
2352a1ad637SFrançois Tigeot 	if (ret != 0) {
2362a1ad637SFrançois Tigeot 		device_printf(c->dev,
2372a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_rate\n", __func__);
2382a1ad637SFrançois Tigeot 		return (ret);
2392a1ad637SFrançois Tigeot 	}
2402a1ad637SFrançois Tigeot 
2412a1ad637SFrançois Tigeot 	f = c->feeder;
2422a1ad637SFrançois Tigeot 
2432a1ad637SFrançois Tigeot 	/*
2442a1ad637SFrançois Tigeot 	 * If in 'dummy' mode (possibly due to passthrough mode), set the
2452a1ad637SFrançois Tigeot 	 * conversion quality to the lowest possible (should be fastest) since
2462a1ad637SFrançois Tigeot 	 * listener won't be hearing anything. Theoretically we can just
2472a1ad637SFrançois Tigeot 	 * disable it, but that will cause weird runtime behaviour:
2482a1ad637SFrançois Tigeot 	 * application appear to play something that is either too fast or too
2492a1ad637SFrançois Tigeot 	 * slow.
2502a1ad637SFrançois Tigeot 	 */
2512a1ad637SFrançois Tigeot 	if (cdesc->dummy != 0) {
2522a1ad637SFrançois Tigeot 		ret = FEEDER_SET(f, FEEDRATE_QUALITY, 0);
2532a1ad637SFrançois Tigeot 		if (ret != 0) {
2542a1ad637SFrançois Tigeot 			device_printf(c->dev,
2552a1ad637SFrançois Tigeot 			    "%s(): can't set resampling quality\n", __func__);
2562a1ad637SFrançois Tigeot 			return (ret);
2572a1ad637SFrançois Tigeot 		}
2582a1ad637SFrançois Tigeot 	}
2592a1ad637SFrançois Tigeot 
2602a1ad637SFrançois Tigeot 	ret = FEEDER_SET(f, FEEDRATE_SRC, cdesc->current.rate);
2612a1ad637SFrançois Tigeot 	if (ret != 0) {
2622a1ad637SFrançois Tigeot 		device_printf(c->dev,
2632a1ad637SFrançois Tigeot 		    "%s(): can't set source rate\n", __func__);
2642a1ad637SFrançois Tigeot 		return (ret);
2652a1ad637SFrançois Tigeot 	}
2662a1ad637SFrançois Tigeot 
2672a1ad637SFrançois Tigeot 	ret = FEEDER_SET(f, FEEDRATE_DST, cdesc->target.rate);
2682a1ad637SFrançois Tigeot 	if (ret != 0) {
2692a1ad637SFrançois Tigeot 		device_printf(c->dev,
2702a1ad637SFrançois Tigeot 		    "%s(): can't set destination rate\n", __func__);
2712a1ad637SFrançois Tigeot 		return (ret);
2722a1ad637SFrançois Tigeot 	}
2732a1ad637SFrançois Tigeot 
2742a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_RATE;
2752a1ad637SFrançois Tigeot 
2762a1ad637SFrançois Tigeot 	cdesc->current.rate = cdesc->target.rate;
2772a1ad637SFrançois Tigeot 
2782a1ad637SFrançois Tigeot 	return (0);
2792a1ad637SFrançois Tigeot }
2802a1ad637SFrançois Tigeot 
2812a1ad637SFrançois Tigeot /*
2822a1ad637SFrançois Tigeot  * feeder_build_matrix(): Chain channel matrixing converter.
2832a1ad637SFrançois Tigeot  */
2842a1ad637SFrançois Tigeot static int
feeder_build_matrix(struct pcm_channel * c,struct feeder_chain_desc * cdesc)2852a1ad637SFrançois Tigeot feeder_build_matrix(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
2862a1ad637SFrançois Tigeot {
2872a1ad637SFrançois Tigeot 	struct feeder_class *fc;
2882a1ad637SFrançois Tigeot 	struct pcm_feeder *f;
2892a1ad637SFrançois Tigeot 	struct pcm_feederdesc *desc;
2902a1ad637SFrançois Tigeot 	int ret;
2912a1ad637SFrançois Tigeot 
2922a1ad637SFrançois Tigeot 	ret = feeder_build_formatne(c, cdesc);
2932a1ad637SFrançois Tigeot 	if (ret != 0)
2942a1ad637SFrançois Tigeot 		return (ret);
2952a1ad637SFrançois Tigeot 
2962a1ad637SFrançois Tigeot 	desc = &(cdesc->desc);
2972a1ad637SFrançois Tigeot 	desc->type = FEEDER_MATRIX;
2982a1ad637SFrançois Tigeot 	desc->in = 0;
2992a1ad637SFrançois Tigeot 	desc->out = 0;
3002a1ad637SFrançois Tigeot 	desc->flags = 0;
3012a1ad637SFrançois Tigeot 
3022a1ad637SFrançois Tigeot 	fc = feeder_getclass(desc);
3032a1ad637SFrançois Tigeot 	if (fc == NULL) {
3042a1ad637SFrançois Tigeot 		device_printf(c->dev,
3052a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_matrix\n", __func__);
3062a1ad637SFrançois Tigeot 		return (ENOTSUP);
3072a1ad637SFrançois Tigeot 	}
3082a1ad637SFrançois Tigeot 
3092a1ad637SFrançois Tigeot 	desc->in = cdesc->current.afmt;
3102a1ad637SFrançois Tigeot 	desc->out = SND_FORMAT(cdesc->current.afmt,
3112a1ad637SFrançois Tigeot 	    cdesc->target.matrix->channels, cdesc->target.matrix->ext);
3122a1ad637SFrançois Tigeot 
3132a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, desc);
3142a1ad637SFrançois Tigeot 	if (ret != 0) {
3152a1ad637SFrançois Tigeot 		device_printf(c->dev,
3162a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_matrix\n", __func__);
3172a1ad637SFrançois Tigeot 		return (ret);
3182a1ad637SFrançois Tigeot 	}
3192a1ad637SFrançois Tigeot 
3202a1ad637SFrançois Tigeot 	f = c->feeder;
3212a1ad637SFrançois Tigeot 	ret = feeder_matrix_setup(f, cdesc->current.matrix,
3222a1ad637SFrançois Tigeot 	    cdesc->target.matrix);
3232a1ad637SFrançois Tigeot 	if (ret != 0) {
3242a1ad637SFrançois Tigeot 		device_printf(c->dev,
3252a1ad637SFrançois Tigeot 		    "%s(): feeder_matrix_setup() failed\n", __func__);
3262a1ad637SFrançois Tigeot 		return (ret);
3272a1ad637SFrançois Tigeot 	}
3282a1ad637SFrançois Tigeot 
3292a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_MATRIX;
3302a1ad637SFrançois Tigeot 
3312a1ad637SFrançois Tigeot 	cdesc->current.afmt = desc->out;
3322a1ad637SFrançois Tigeot 	cdesc->current.matrix = cdesc->target.matrix;
3332a1ad637SFrançois Tigeot 	cdesc->use_matrix = 0;
3342a1ad637SFrançois Tigeot 
3352a1ad637SFrançois Tigeot 	return (0);
3362a1ad637SFrançois Tigeot }
3372a1ad637SFrançois Tigeot 
3382a1ad637SFrançois Tigeot /*
3392a1ad637SFrançois Tigeot  * feeder_build_volume(): Chain soft volume.
3402a1ad637SFrançois Tigeot  */
3412a1ad637SFrançois Tigeot static int
feeder_build_volume(struct pcm_channel * c,struct feeder_chain_desc * cdesc)3422a1ad637SFrançois Tigeot feeder_build_volume(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
3432a1ad637SFrançois Tigeot {
3442a1ad637SFrançois Tigeot 	struct feeder_class *fc;
3452a1ad637SFrançois Tigeot 	struct pcm_feeder *f;
3462a1ad637SFrançois Tigeot 	struct pcm_feederdesc *desc;
3472a1ad637SFrançois Tigeot 	int ret;
3482a1ad637SFrançois Tigeot 
3492a1ad637SFrançois Tigeot 	ret = feeder_build_formatne(c, cdesc);
3502a1ad637SFrançois Tigeot 	if (ret != 0)
3512a1ad637SFrançois Tigeot 		return (ret);
3522a1ad637SFrançois Tigeot 
3532a1ad637SFrançois Tigeot 	desc = &(cdesc->desc);
3542a1ad637SFrançois Tigeot 	desc->type = FEEDER_VOLUME;
3552a1ad637SFrançois Tigeot 	desc->in = 0;
3562a1ad637SFrançois Tigeot 	desc->out = 0;
3572a1ad637SFrançois Tigeot 	desc->flags = 0;
3582a1ad637SFrançois Tigeot 
3592a1ad637SFrançois Tigeot 	fc = feeder_getclass(desc);
3602a1ad637SFrançois Tigeot 	if (fc == NULL) {
3612a1ad637SFrançois Tigeot 		device_printf(c->dev,
3622a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_volume\n", __func__);
3632a1ad637SFrançois Tigeot 		return (ENOTSUP);
3642a1ad637SFrançois Tigeot 	}
3652a1ad637SFrançois Tigeot 
3662a1ad637SFrançois Tigeot 	desc->in = cdesc->current.afmt;
3672a1ad637SFrançois Tigeot 	desc->out = desc->in;
3682a1ad637SFrançois Tigeot 
3692a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, desc);
3702a1ad637SFrançois Tigeot 	if (ret != 0) {
3712a1ad637SFrançois Tigeot 		device_printf(c->dev,
3722a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_volume\n", __func__);
3732a1ad637SFrançois Tigeot 		return (ret);
3742a1ad637SFrançois Tigeot 	}
3752a1ad637SFrançois Tigeot 
3762a1ad637SFrançois Tigeot 	f = c->feeder;
3772a1ad637SFrançois Tigeot 
3782a1ad637SFrançois Tigeot 	/*
3792a1ad637SFrançois Tigeot 	 * If in 'dummy' mode (possibly due to passthrough mode), set BYPASS
3802a1ad637SFrançois Tigeot 	 * mode since listener won't be hearing anything. Theoretically we can
3812a1ad637SFrançois Tigeot 	 * just disable it, but that will confuse volume per channel mixer.
3822a1ad637SFrançois Tigeot 	 */
3832a1ad637SFrançois Tigeot 	if (cdesc->dummy != 0) {
3842a1ad637SFrançois Tigeot 		ret = FEEDER_SET(f, FEEDVOLUME_STATE, FEEDVOLUME_BYPASS);
3852a1ad637SFrançois Tigeot 		if (ret != 0) {
3862a1ad637SFrançois Tigeot 			device_printf(c->dev,
3872a1ad637SFrançois Tigeot 			    "%s(): can't set volume bypass\n", __func__);
3882a1ad637SFrançois Tigeot 			return (ret);
3892a1ad637SFrançois Tigeot 		}
3902a1ad637SFrançois Tigeot 	}
3912a1ad637SFrançois Tigeot 
3922a1ad637SFrançois Tigeot 	ret = feeder_volume_apply_matrix(f, cdesc->current.matrix);
3932a1ad637SFrançois Tigeot 	if (ret != 0) {
3942a1ad637SFrançois Tigeot 		device_printf(c->dev,
3952a1ad637SFrançois Tigeot 		    "%s(): feeder_volume_apply_matrix() failed\n", __func__);
3962a1ad637SFrançois Tigeot 		return (ret);
3972a1ad637SFrançois Tigeot 	}
3982a1ad637SFrançois Tigeot 
3992a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_VOLUME;
4002a1ad637SFrançois Tigeot 
4012a1ad637SFrançois Tigeot 	cdesc->use_volume = 0;
4022a1ad637SFrançois Tigeot 
4032a1ad637SFrançois Tigeot 	return (0);
4042a1ad637SFrançois Tigeot }
4052a1ad637SFrançois Tigeot 
4062a1ad637SFrançois Tigeot /*
4072a1ad637SFrançois Tigeot  * feeder_build_eq(): Chain parametric software equalizer.
4082a1ad637SFrançois Tigeot  */
4092a1ad637SFrançois Tigeot static int
feeder_build_eq(struct pcm_channel * c,struct feeder_chain_desc * cdesc)4102a1ad637SFrançois Tigeot feeder_build_eq(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
4112a1ad637SFrançois Tigeot {
4122a1ad637SFrançois Tigeot 	struct feeder_class *fc;
4132a1ad637SFrançois Tigeot 	struct pcm_feeder *f;
4142a1ad637SFrançois Tigeot 	struct pcm_feederdesc *desc;
4152a1ad637SFrançois Tigeot 	int ret;
4162a1ad637SFrançois Tigeot 
4172a1ad637SFrançois Tigeot 	ret = feeder_build_formatne(c, cdesc);
4182a1ad637SFrançois Tigeot 	if (ret != 0)
4192a1ad637SFrançois Tigeot 		return (ret);
4202a1ad637SFrançois Tigeot 
4212a1ad637SFrançois Tigeot 	desc = &(cdesc->desc);
4222a1ad637SFrançois Tigeot 	desc->type = FEEDER_EQ;
4232a1ad637SFrançois Tigeot 	desc->in = 0;
4242a1ad637SFrançois Tigeot 	desc->out = 0;
4252a1ad637SFrançois Tigeot 	desc->flags = 0;
4262a1ad637SFrançois Tigeot 
4272a1ad637SFrançois Tigeot 	fc = feeder_getclass(desc);
4282a1ad637SFrançois Tigeot 	if (fc == NULL) {
4292a1ad637SFrançois Tigeot 		device_printf(c->dev,
4302a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_eq\n", __func__);
4312a1ad637SFrançois Tigeot 		return (ENOTSUP);
4322a1ad637SFrançois Tigeot 	}
4332a1ad637SFrançois Tigeot 
4342a1ad637SFrançois Tigeot 	desc->in = cdesc->current.afmt;
4352a1ad637SFrançois Tigeot 	desc->out = desc->in;
4362a1ad637SFrançois Tigeot 
4372a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, desc);
4382a1ad637SFrançois Tigeot 	if (ret != 0) {
4392a1ad637SFrançois Tigeot 		device_printf(c->dev,
4402a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_eq\n", __func__);
4412a1ad637SFrançois Tigeot 		return (ret);
4422a1ad637SFrançois Tigeot 	}
4432a1ad637SFrançois Tigeot 
4442a1ad637SFrançois Tigeot 	f = c->feeder;
4452a1ad637SFrançois Tigeot 
4462a1ad637SFrançois Tigeot 	ret = FEEDER_SET(f, FEEDEQ_RATE, cdesc->current.rate);
4472a1ad637SFrançois Tigeot 	if (ret != 0) {
4482a1ad637SFrançois Tigeot 		device_printf(c->dev,
4492a1ad637SFrançois Tigeot 		    "%s(): can't set rate on feeder_eq\n", __func__);
4502a1ad637SFrançois Tigeot 		return (ret);
4512a1ad637SFrançois Tigeot 	}
4522a1ad637SFrançois Tigeot 
4532a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_EQ;
4542a1ad637SFrançois Tigeot 
4552a1ad637SFrançois Tigeot 	cdesc->use_eq = 0;
4562a1ad637SFrançois Tigeot 
4572a1ad637SFrançois Tigeot 	return (0);
4582a1ad637SFrançois Tigeot }
4592a1ad637SFrançois Tigeot 
4602a1ad637SFrançois Tigeot /*
4612a1ad637SFrançois Tigeot  * feeder_build_root(): Chain root feeder, the top, father of all.
4622a1ad637SFrançois Tigeot  */
4632a1ad637SFrançois Tigeot static int
feeder_build_root(struct pcm_channel * c,struct feeder_chain_desc * cdesc)4642a1ad637SFrançois Tigeot feeder_build_root(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
4652a1ad637SFrançois Tigeot {
4662a1ad637SFrançois Tigeot 	struct feeder_class *fc;
4672a1ad637SFrançois Tigeot 	int ret;
4682a1ad637SFrançois Tigeot 
4692a1ad637SFrançois Tigeot 	fc = feeder_getclass(NULL);
4702a1ad637SFrançois Tigeot 	if (fc == NULL) {
4712a1ad637SFrançois Tigeot 		device_printf(c->dev,
4722a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_root\n", __func__);
4732a1ad637SFrançois Tigeot 		return (ENOTSUP);
4742a1ad637SFrançois Tigeot 	}
4752a1ad637SFrançois Tigeot 
4762a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, NULL);
4772a1ad637SFrançois Tigeot 	if (ret != 0) {
4782a1ad637SFrançois Tigeot 		device_printf(c->dev,
4792a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_root\n", __func__);
4802a1ad637SFrançois Tigeot 		return (ret);
4812a1ad637SFrançois Tigeot 	}
4822a1ad637SFrançois Tigeot 
4832a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_ROOT;
4842a1ad637SFrançois Tigeot 
4852a1ad637SFrançois Tigeot 	c->feeder->desc->in = cdesc->current.afmt;
4862a1ad637SFrançois Tigeot 	c->feeder->desc->out = cdesc->current.afmt;
4872a1ad637SFrançois Tigeot 
4882a1ad637SFrançois Tigeot 	return (0);
4892a1ad637SFrançois Tigeot }
4902a1ad637SFrançois Tigeot 
4912a1ad637SFrançois Tigeot /*
4922a1ad637SFrançois Tigeot  * feeder_build_mixer(): Chain software mixer for virtual channels.
4932a1ad637SFrançois Tigeot  */
4942a1ad637SFrançois Tigeot static int
feeder_build_mixer(struct pcm_channel * c,struct feeder_chain_desc * cdesc)4952a1ad637SFrançois Tigeot feeder_build_mixer(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
4962a1ad637SFrançois Tigeot {
4972a1ad637SFrançois Tigeot 	struct feeder_class *fc;
4982a1ad637SFrançois Tigeot 	struct pcm_feederdesc *desc;
4992a1ad637SFrançois Tigeot 	int ret;
5002a1ad637SFrançois Tigeot 
5012a1ad637SFrançois Tigeot 	desc = &(cdesc->desc);
5022a1ad637SFrançois Tigeot 	desc->type = FEEDER_MIXER;
5032a1ad637SFrançois Tigeot 	desc->in = 0;
5042a1ad637SFrançois Tigeot 	desc->out = 0;
5052a1ad637SFrançois Tigeot 	desc->flags = 0;
5062a1ad637SFrançois Tigeot 
5072a1ad637SFrançois Tigeot 	fc = feeder_getclass(desc);
5082a1ad637SFrançois Tigeot 	if (fc == NULL) {
5092a1ad637SFrançois Tigeot 		device_printf(c->dev,
5102a1ad637SFrançois Tigeot 		    "%s(): can't find feeder_mixer\n", __func__);
5112a1ad637SFrançois Tigeot 		return (ENOTSUP);
5122a1ad637SFrançois Tigeot 	}
5132a1ad637SFrançois Tigeot 
5142a1ad637SFrançois Tigeot 	desc->in = cdesc->current.afmt;
5152a1ad637SFrançois Tigeot 	desc->out = desc->in;
5162a1ad637SFrançois Tigeot 
5172a1ad637SFrançois Tigeot 	ret = chn_addfeeder(c, fc, desc);
5182a1ad637SFrançois Tigeot 	if (ret != 0) {
5192a1ad637SFrançois Tigeot 		device_printf(c->dev,
5202a1ad637SFrançois Tigeot 		    "%s(): can't add feeder_mixer\n", __func__);
5212a1ad637SFrançois Tigeot 		return (ret);
5222a1ad637SFrançois Tigeot 	}
5232a1ad637SFrançois Tigeot 
5242a1ad637SFrançois Tigeot 	c->feederflags |= 1 << FEEDER_MIXER;
5252a1ad637SFrançois Tigeot 
5262a1ad637SFrançois Tigeot 	return (0);
5272a1ad637SFrançois Tigeot }
5282a1ad637SFrançois Tigeot 
5292a1ad637SFrançois Tigeot /* Macrosses to ease our job doing stuffs later. */
5302a1ad637SFrançois Tigeot #define FEEDER_BW(c, t)		((c)->t.matrix->channels * (c)->t.rate)
5312a1ad637SFrançois Tigeot 
5322a1ad637SFrançois Tigeot #define FEEDRATE_UP(c)		((c)->target.rate > (c)->current.rate)
5332a1ad637SFrançois Tigeot #define FEEDRATE_DOWN(c)	((c)->target.rate < (c)->current.rate)
5342a1ad637SFrançois Tigeot #define FEEDRATE_REQUIRED(c)	(FEEDRATE_UP(c) || FEEDRATE_DOWN(c))
5352a1ad637SFrançois Tigeot 
5362a1ad637SFrançois Tigeot #define FEEDMATRIX_UP(c)	((c)->target.matrix->channels >		\
5372a1ad637SFrançois Tigeot 				 (c)->current.matrix->channels)
5382a1ad637SFrançois Tigeot #define FEEDMATRIX_DOWN(c)	((c)->target.matrix->channels <		\
5392a1ad637SFrançois Tigeot 				 (c)->current.matrix->channels)
5402a1ad637SFrançois Tigeot #define FEEDMATRIX_REQUIRED(c)	(FEEDMATRIX_UP(c) ||			\
5412a1ad637SFrançois Tigeot 				 FEEDMATRIX_DOWN(c) || (c)->use_matrix != 0)
5422a1ad637SFrançois Tigeot 
5432a1ad637SFrançois Tigeot #define FEEDFORMAT_REQUIRED(c)	(AFMT_ENCODING((c)->current.afmt) !=	\
5442a1ad637SFrançois Tigeot 				 AFMT_ENCODING((c)->target.afmt))
5452a1ad637SFrançois Tigeot 
5462a1ad637SFrançois Tigeot #define FEEDVOLUME_REQUIRED(c)	((c)->use_volume != 0)
5472a1ad637SFrançois Tigeot 
5482a1ad637SFrançois Tigeot #define FEEDEQ_VALIDRATE(c, t)	(feeder_eq_validrate((c)->t.rate) != 0)
5492a1ad637SFrançois Tigeot #define FEEDEQ_ECONOMY(c)	(FEEDER_BW(c, current) < FEEDER_BW(c, target))
5502a1ad637SFrançois Tigeot #define FEEDEQ_REQUIRED(c)	((c)->use_eq != 0 &&			\
5512a1ad637SFrançois Tigeot 				 FEEDEQ_VALIDRATE(c, current))
5522a1ad637SFrançois Tigeot 
5532a1ad637SFrançois Tigeot #define FEEDFORMAT_NE_REQUIRED(c)					\
5542a1ad637SFrançois Tigeot 	((c)->afmt_ne != AFMT_S32_NE &&					\
5552a1ad637SFrançois Tigeot 	(((c)->mode == FEEDER_CHAIN_16 &&				\
5562a1ad637SFrançois Tigeot 	AFMT_ENCODING((c)->current.afmt) != AFMT_S16_NE) ||		\
5572a1ad637SFrançois Tigeot 	((c)->mode == FEEDER_CHAIN_32 &&				\
5582a1ad637SFrançois Tigeot 	AFMT_ENCODING((c)->current.afmt) != AFMT_S32_NE) ||		\
5592a1ad637SFrançois Tigeot 	(c)->mode == FEEDER_CHAIN_FULLMULTI ||				\
5602a1ad637SFrançois Tigeot 	((c)->mode == FEEDER_CHAIN_MULTI &&				\
5612a1ad637SFrançois Tigeot 	((c)->current.afmt & AFMT_8BIT)) ||				\
5622a1ad637SFrançois Tigeot 	((c)->mode == FEEDER_CHAIN_LEAN &&				\
5632a1ad637SFrançois Tigeot 	!((c)->current.afmt & (AFMT_S16_NE | AFMT_S32_NE)))))
5642a1ad637SFrançois Tigeot 
5652a1ad637SFrançois Tigeot int
feeder_chain(struct pcm_channel * c)5662a1ad637SFrançois Tigeot feeder_chain(struct pcm_channel *c)
5672a1ad637SFrançois Tigeot {
5682a1ad637SFrançois Tigeot 	struct snddev_info *d;
5692a1ad637SFrançois Tigeot 	struct pcmchan_caps *caps;
5702a1ad637SFrançois Tigeot 	struct feeder_chain_desc cdesc;
5712a1ad637SFrançois Tigeot 	struct pcmchan_matrix *hwmatrix, *softmatrix;
5722a1ad637SFrançois Tigeot 	uint32_t hwfmt, softfmt;
5732a1ad637SFrançois Tigeot 	int ret;
5742a1ad637SFrançois Tigeot 
5752a1ad637SFrançois Tigeot 	CHN_LOCKASSERT(c);
5762a1ad637SFrançois Tigeot 
5772a1ad637SFrançois Tigeot 	/* Remove everything first. */
5782a1ad637SFrançois Tigeot 	while (chn_removefeeder(c) == 0)
5792a1ad637SFrançois Tigeot 		;
5802a1ad637SFrançois Tigeot 
5812a1ad637SFrançois Tigeot 	KASSERT(c->feeder == NULL, ("feeder chain not empty"));
5822a1ad637SFrançois Tigeot 
5832a1ad637SFrançois Tigeot 	/* clear and populate chain descriptor. */
5842a1ad637SFrançois Tigeot 	bzero(&cdesc, sizeof(cdesc));
5852a1ad637SFrançois Tigeot 
5862a1ad637SFrançois Tigeot 	switch (feeder_chain_mode) {
5872a1ad637SFrançois Tigeot 	case FEEDER_CHAIN_LEAN:
5882a1ad637SFrançois Tigeot 	case FEEDER_CHAIN_16:
5892a1ad637SFrançois Tigeot 	case FEEDER_CHAIN_32:
5902a1ad637SFrançois Tigeot #if defined(SND_FEEDER_MULTIFORMAT) || defined(SND_FEEDER_FULL_MULTIFORMAT)
5912a1ad637SFrançois Tigeot 	case FEEDER_CHAIN_MULTI:
5922a1ad637SFrançois Tigeot #endif
5932a1ad637SFrançois Tigeot #if defined(SND_FEEDER_FULL_MULTIFORMAT)
5942a1ad637SFrançois Tigeot 	case FEEDER_CHAIN_FULLMULTI:
5952a1ad637SFrançois Tigeot #endif
5962a1ad637SFrançois Tigeot 		break;
5972a1ad637SFrançois Tigeot 	default:
5982a1ad637SFrançois Tigeot 		feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
5992a1ad637SFrançois Tigeot 		break;
6002a1ad637SFrançois Tigeot 	}
6012a1ad637SFrançois Tigeot 
6022a1ad637SFrançois Tigeot 	cdesc.mode = feeder_chain_mode;
6032a1ad637SFrançois Tigeot 	cdesc.expensive = 1;	/* XXX faster.. */
6042a1ad637SFrançois Tigeot 
6052a1ad637SFrançois Tigeot #define VCHAN_PASSTHROUGH(c)	(((c)->flags & (CHN_F_VIRTUAL |		\
6062a1ad637SFrançois Tigeot 				 CHN_F_PASSTHROUGH)) ==			\
6072a1ad637SFrançois Tigeot 				 (CHN_F_VIRTUAL | CHN_F_PASSTHROUGH))
6082a1ad637SFrançois Tigeot 
6092a1ad637SFrançois Tigeot 	/* Get the best possible hardware format. */
6102a1ad637SFrançois Tigeot 	if (VCHAN_PASSTHROUGH(c))
6112a1ad637SFrançois Tigeot 		hwfmt = c->parentchannel->format;
6122a1ad637SFrançois Tigeot 	else {
6132a1ad637SFrançois Tigeot 		caps = chn_getcaps(c);
6142a1ad637SFrançois Tigeot 		if (caps == NULL || caps->fmtlist == NULL) {
6152a1ad637SFrançois Tigeot 			device_printf(c->dev,
6162a1ad637SFrançois Tigeot 			    "%s(): failed to get channel caps\n", __func__);
6172a1ad637SFrançois Tigeot 			return (ENODEV);
6182a1ad637SFrançois Tigeot 		}
6192a1ad637SFrançois Tigeot 
6202a1ad637SFrançois Tigeot 		if ((c->format & AFMT_PASSTHROUGH) &&
6212a1ad637SFrançois Tigeot 		    !snd_fmtvalid(c->format, caps->fmtlist))
6222a1ad637SFrançois Tigeot 			return (ENODEV);
6232a1ad637SFrançois Tigeot 
6242a1ad637SFrançois Tigeot 		hwfmt = snd_fmtbest(c->format, caps->fmtlist);
6252a1ad637SFrançois Tigeot 		if (hwfmt == 0 || !snd_fmtvalid(hwfmt, caps->fmtlist)) {
6262a1ad637SFrançois Tigeot 			device_printf(c->dev,
6272a1ad637SFrançois Tigeot 			    "%s(): invalid hardware format 0x%08x\n",
6282a1ad637SFrançois Tigeot 			    __func__, hwfmt);
6292a1ad637SFrançois Tigeot 			{
6302a1ad637SFrançois Tigeot 				int i;
6312a1ad637SFrançois Tigeot 				for (i = 0; caps->fmtlist[i] != 0; i++)
63267931cc4SFrançois Tigeot 					kprintf("0x%08x\n", caps->fmtlist[i]);
63367931cc4SFrançois Tigeot 				kprintf("Req: 0x%08x\n", c->format);
6342a1ad637SFrançois Tigeot 			}
6352a1ad637SFrançois Tigeot 			return (ENODEV);
6362a1ad637SFrançois Tigeot 		}
6372a1ad637SFrançois Tigeot 	}
6382a1ad637SFrançois Tigeot 
6392a1ad637SFrançois Tigeot 	/*
6402a1ad637SFrançois Tigeot 	 * The 'hardware' possibly have different intepretation of channel
6412a1ad637SFrançois Tigeot 	 * matrixing, so get it first .....
6422a1ad637SFrançois Tigeot 	 */
6432a1ad637SFrançois Tigeot 	hwmatrix = CHANNEL_GETMATRIX(c->methods, c->devinfo, hwfmt);
6442a1ad637SFrançois Tigeot 	if (hwmatrix == NULL) {
6452a1ad637SFrançois Tigeot 		device_printf(c->dev,
6462a1ad637SFrançois Tigeot 		    "%s(): failed to acquire hw matrix [0x%08x]\n",
6472a1ad637SFrançois Tigeot 		    __func__, hwfmt);
6482a1ad637SFrançois Tigeot 		return (ENODEV);
6492a1ad637SFrançois Tigeot 	}
6502a1ad637SFrançois Tigeot 	/* ..... and rebuild hwfmt. */
6512a1ad637SFrançois Tigeot 	hwfmt = SND_FORMAT(hwfmt, hwmatrix->channels, hwmatrix->ext);
6522a1ad637SFrançois Tigeot 
6532a1ad637SFrançois Tigeot 	/* Reset and rebuild default channel format/matrix map. */
6542a1ad637SFrançois Tigeot 	softfmt = c->format;
6552a1ad637SFrançois Tigeot 	softmatrix = &c->matrix;
6562a1ad637SFrançois Tigeot 	if (softmatrix->channels != AFMT_CHANNEL(softfmt) ||
6572a1ad637SFrançois Tigeot 	    softmatrix->ext != AFMT_EXTCHANNEL(softfmt)) {
6582a1ad637SFrançois Tigeot 		softmatrix = feeder_matrix_format_map(softfmt);
6592a1ad637SFrançois Tigeot 		if (softmatrix == NULL) {
6602a1ad637SFrançois Tigeot 			device_printf(c->dev,
6612a1ad637SFrançois Tigeot 			    "%s(): failed to acquire soft matrix [0x%08x]\n",
6622a1ad637SFrançois Tigeot 			    __func__, softfmt);
6632a1ad637SFrançois Tigeot 			return (ENODEV);
6642a1ad637SFrançois Tigeot 		}
6652a1ad637SFrançois Tigeot 		c->matrix = *softmatrix;
6662a1ad637SFrançois Tigeot 		c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
6672a1ad637SFrançois Tigeot 	}
6682a1ad637SFrançois Tigeot 	softfmt = SND_FORMAT(softfmt, softmatrix->channels, softmatrix->ext);
6692a1ad637SFrançois Tigeot 	if (softfmt != c->format)
6702a1ad637SFrançois Tigeot 		device_printf(c->dev,
6712a1ad637SFrançois Tigeot 		    "%s(): WARNING: %s Soft format 0x%08x -> 0x%08x\n",
6722a1ad637SFrançois Tigeot 		    __func__, CHN_DIRSTR(c), c->format, softfmt);
6732a1ad637SFrançois Tigeot 
6742a1ad637SFrançois Tigeot 	/*
6752a1ad637SFrançois Tigeot 	 * PLAY and REC are opposite.
6762a1ad637SFrançois Tigeot 	 */
6772a1ad637SFrançois Tigeot 	if (c->direction == PCMDIR_PLAY) {
6782a1ad637SFrançois Tigeot 		cdesc.origin.afmt    = softfmt;
6792a1ad637SFrançois Tigeot 		cdesc.origin.matrix  = softmatrix;
6802a1ad637SFrançois Tigeot 		cdesc.origin.rate    = c->speed;
6812a1ad637SFrançois Tigeot 		cdesc.target.afmt    = hwfmt;
6822a1ad637SFrançois Tigeot 		cdesc.target.matrix  = hwmatrix;
6832a1ad637SFrançois Tigeot 		cdesc.target.rate    = sndbuf_getspd(c->bufhard);
6842a1ad637SFrançois Tigeot 	} else {
6852a1ad637SFrançois Tigeot 		cdesc.origin.afmt    = hwfmt;
6862a1ad637SFrançois Tigeot 		cdesc.origin.matrix  = hwmatrix;
6872a1ad637SFrançois Tigeot 		cdesc.origin.rate    = sndbuf_getspd(c->bufhard);
6882a1ad637SFrançois Tigeot 		cdesc.target.afmt    = softfmt;
6892a1ad637SFrançois Tigeot 		cdesc.target.matrix  = softmatrix;
6902a1ad637SFrançois Tigeot 		cdesc.target.rate    = c->speed;
6912a1ad637SFrançois Tigeot 	}
6922a1ad637SFrançois Tigeot 
6932a1ad637SFrançois Tigeot 	d = c->parentsnddev;
6942a1ad637SFrançois Tigeot 
6952a1ad637SFrançois Tigeot 	/*
6962a1ad637SFrançois Tigeot 	 * If channel is in bitperfect or passthrough mode, make it appear
6972a1ad637SFrançois Tigeot 	 * that 'origin' and 'target' identical, skipping mostly chain
6982a1ad637SFrançois Tigeot 	 * procedures.
6992a1ad637SFrançois Tigeot 	 */
7002a1ad637SFrançois Tigeot 	if (CHN_BITPERFECT(c) || (c->format & AFMT_PASSTHROUGH)) {
7012a1ad637SFrançois Tigeot 		if (c->direction == PCMDIR_PLAY)
7022a1ad637SFrançois Tigeot 			cdesc.origin = cdesc.target;
7032a1ad637SFrançois Tigeot 		else
7042a1ad637SFrançois Tigeot 			cdesc.target = cdesc.origin;
7052a1ad637SFrançois Tigeot 		c->format = cdesc.target.afmt;
7062a1ad637SFrançois Tigeot 		c->speed  = cdesc.target.rate;
7072a1ad637SFrançois Tigeot 	} else {
7082a1ad637SFrançois Tigeot 		/* hwfmt is not convertible, so 'dummy' it. */
7092a1ad637SFrançois Tigeot 		if (hwfmt & AFMT_PASSTHROUGH)
7102a1ad637SFrançois Tigeot 			cdesc.dummy = 1;
7112a1ad637SFrançois Tigeot 
7122a1ad637SFrançois Tigeot 		if ((softfmt & AFMT_CONVERTIBLE) &&
7132a1ad637SFrançois Tigeot 		    (((d->flags & SD_F_VPC) && !(c->flags & CHN_F_HAS_VCHAN)) ||
7142a1ad637SFrançois Tigeot 		    (!(d->flags & SD_F_VPC) && (d->flags & SD_F_SOFTPCMVOL) &&
7152a1ad637SFrançois Tigeot 		    !(c->flags & CHN_F_VIRTUAL))))
7162a1ad637SFrançois Tigeot 			cdesc.use_volume = 1;
7172a1ad637SFrançois Tigeot 
7182a1ad637SFrançois Tigeot 		if (feeder_matrix_compare(cdesc.origin.matrix,
7192a1ad637SFrançois Tigeot 		    cdesc.target.matrix) != 0)
7202a1ad637SFrançois Tigeot 			cdesc.use_matrix = 1;
7212a1ad637SFrançois Tigeot 
7222a1ad637SFrançois Tigeot 		/* Soft EQ only applicable for PLAY. */
7232a1ad637SFrançois Tigeot 		if (cdesc.dummy == 0 &&
7242a1ad637SFrançois Tigeot 		    c->direction == PCMDIR_PLAY && (d->flags & SD_F_EQ) &&
7252a1ad637SFrançois Tigeot 		    (((d->flags & SD_F_EQ_PC) &&
7262a1ad637SFrançois Tigeot 		    !(c->flags & CHN_F_HAS_VCHAN)) ||
7272a1ad637SFrançois Tigeot 		    (!(d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_VIRTUAL))))
7282a1ad637SFrançois Tigeot 			cdesc.use_eq = 1;
7292a1ad637SFrançois Tigeot 
7302a1ad637SFrançois Tigeot 		if (FEEDFORMAT_NE_REQUIRED(&cdesc)) {
7312a1ad637SFrançois Tigeot 			cdesc.afmt_ne =
7322a1ad637SFrançois Tigeot 			    (cdesc.dummy != 0) ?
7332a1ad637SFrançois Tigeot 			    snd_fmtbest(AFMT_ENCODING(softfmt),
7342a1ad637SFrançois Tigeot 			    feeder_chain_formats[cdesc.mode]) :
7352a1ad637SFrançois Tigeot 			    snd_fmtbest(AFMT_ENCODING(cdesc.target.afmt),
7362a1ad637SFrançois Tigeot 			    feeder_chain_formats[cdesc.mode]);
7372a1ad637SFrançois Tigeot 			if (cdesc.afmt_ne == 0) {
7382a1ad637SFrançois Tigeot 				device_printf(c->dev,
7392a1ad637SFrançois Tigeot 				    "%s(): snd_fmtbest failed!\n", __func__);
7402a1ad637SFrançois Tigeot 				cdesc.afmt_ne =
7412a1ad637SFrançois Tigeot 				    (((cdesc.dummy != 0) ? softfmt :
7422a1ad637SFrançois Tigeot 				    cdesc.target.afmt) &
7432a1ad637SFrançois Tigeot 				    (AFMT_24BIT | AFMT_32BIT)) ?
7442a1ad637SFrançois Tigeot 				    AFMT_S32_NE : AFMT_S16_NE;
7452a1ad637SFrançois Tigeot 			}
7462a1ad637SFrançois Tigeot 		}
7472a1ad637SFrançois Tigeot 	}
7482a1ad637SFrançois Tigeot 
7492a1ad637SFrançois Tigeot 	cdesc.current = cdesc.origin;
7502a1ad637SFrançois Tigeot 
7512a1ad637SFrançois Tigeot 	/* Build everything. */
7522a1ad637SFrançois Tigeot 
7532a1ad637SFrançois Tigeot 	c->feederflags = 0;
7542a1ad637SFrançois Tigeot 
7552a1ad637SFrançois Tigeot #define FEEDER_BUILD(t)	do {						\
7562a1ad637SFrançois Tigeot 	ret = feeder_build_##t(c, &cdesc);				\
7572a1ad637SFrançois Tigeot 	if (ret != 0)							\
7582a1ad637SFrançois Tigeot 		return (ret);						\
7592a1ad637SFrançois Tigeot 	} while (0)
7602a1ad637SFrançois Tigeot 
7612a1ad637SFrançois Tigeot 	if (!(c->flags & CHN_F_HAS_VCHAN) || c->direction == PCMDIR_REC)
7622a1ad637SFrançois Tigeot 		FEEDER_BUILD(root);
7632a1ad637SFrançois Tigeot 	else if (c->direction == PCMDIR_PLAY && (c->flags & CHN_F_HAS_VCHAN))
7642a1ad637SFrançois Tigeot 		FEEDER_BUILD(mixer);
7652a1ad637SFrançois Tigeot 	else
7662a1ad637SFrançois Tigeot 		return (ENOTSUP);
7672a1ad637SFrançois Tigeot 
7682a1ad637SFrançois Tigeot 	/*
7692a1ad637SFrançois Tigeot 	 * The basic idea is: The smaller the bandwidth, the cheaper the
7702a1ad637SFrançois Tigeot 	 * conversion process, with following constraints:-
7712a1ad637SFrançois Tigeot 	 *
7722a1ad637SFrançois Tigeot 	 * 1) Almost all feeders work best in 16/32 native endian.
7732a1ad637SFrançois Tigeot 	 * 2) Try to avoid 8bit feeders due to poor dynamic range.
7742a1ad637SFrançois Tigeot 	 * 3) Avoid volume, format, matrix and rate in BITPERFECT or
7752a1ad637SFrançois Tigeot 	 *    PASSTHROUGH mode.
7762a1ad637SFrançois Tigeot 	 * 4) Try putting volume before EQ or rate. Should help to
7772a1ad637SFrançois Tigeot 	 *    avoid/reduce possible clipping.
7782a1ad637SFrançois Tigeot 	 * 5) EQ require specific, valid rate, unless it allow sloppy
7792a1ad637SFrançois Tigeot 	 *    conversion.
7802a1ad637SFrançois Tigeot 	 */
7812a1ad637SFrançois Tigeot 	if (FEEDMATRIX_UP(&cdesc)) {
7822a1ad637SFrançois Tigeot 		if (FEEDEQ_REQUIRED(&cdesc) &&
7832a1ad637SFrançois Tigeot 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
7842a1ad637SFrançois Tigeot 		    (cdesc.expensive == 0 && FEEDEQ_ECONOMY(&cdesc))))
7852a1ad637SFrançois Tigeot 			FEEDER_BUILD(eq);
7862a1ad637SFrançois Tigeot 		if (FEEDRATE_REQUIRED(&cdesc))
7872a1ad637SFrançois Tigeot 			FEEDER_BUILD(rate);
7882a1ad637SFrançois Tigeot 		FEEDER_BUILD(matrix);
7892a1ad637SFrançois Tigeot 		if (FEEDVOLUME_REQUIRED(&cdesc))
7902a1ad637SFrançois Tigeot 			FEEDER_BUILD(volume);
7912a1ad637SFrançois Tigeot 		if (FEEDEQ_REQUIRED(&cdesc))
7922a1ad637SFrançois Tigeot 			FEEDER_BUILD(eq);
7932a1ad637SFrançois Tigeot 	} else if (FEEDMATRIX_DOWN(&cdesc)) {
7942a1ad637SFrançois Tigeot 		FEEDER_BUILD(matrix);
7952a1ad637SFrançois Tigeot 		if (FEEDVOLUME_REQUIRED(&cdesc))
7962a1ad637SFrançois Tigeot 			FEEDER_BUILD(volume);
7972a1ad637SFrançois Tigeot 		if (FEEDEQ_REQUIRED(&cdesc) &&
7982a1ad637SFrançois Tigeot 		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
7992a1ad637SFrançois Tigeot 		    FEEDEQ_ECONOMY(&cdesc)))
8002a1ad637SFrançois Tigeot 			FEEDER_BUILD(eq);
8012a1ad637SFrançois Tigeot 		if (FEEDRATE_REQUIRED(&cdesc))
8022a1ad637SFrançois Tigeot 			FEEDER_BUILD(rate);
8032a1ad637SFrançois Tigeot 		if (FEEDEQ_REQUIRED(&cdesc))
8042a1ad637SFrançois Tigeot 			FEEDER_BUILD(eq);
8052a1ad637SFrançois Tigeot 	} else {
8062a1ad637SFrançois Tigeot 		if (FEEDRATE_DOWN(&cdesc)) {
8072a1ad637SFrançois Tigeot 			if (FEEDEQ_REQUIRED(&cdesc) &&
8082a1ad637SFrançois Tigeot 			    !FEEDEQ_VALIDRATE(&cdesc, target)) {
8092a1ad637SFrançois Tigeot 				if (FEEDVOLUME_REQUIRED(&cdesc))
8102a1ad637SFrançois Tigeot 					FEEDER_BUILD(volume);
8112a1ad637SFrançois Tigeot 				FEEDER_BUILD(eq);
8122a1ad637SFrançois Tigeot 			}
8132a1ad637SFrançois Tigeot 			FEEDER_BUILD(rate);
8142a1ad637SFrançois Tigeot 		}
8152a1ad637SFrançois Tigeot 		if (FEEDMATRIX_REQUIRED(&cdesc))
8162a1ad637SFrançois Tigeot 			FEEDER_BUILD(matrix);
8172a1ad637SFrançois Tigeot 		if (FEEDVOLUME_REQUIRED(&cdesc))
8182a1ad637SFrançois Tigeot 			FEEDER_BUILD(volume);
8192a1ad637SFrançois Tigeot 		if (FEEDRATE_UP(&cdesc)) {
8202a1ad637SFrançois Tigeot 			if (FEEDEQ_REQUIRED(&cdesc) &&
8212a1ad637SFrançois Tigeot 			    !FEEDEQ_VALIDRATE(&cdesc, target))
8222a1ad637SFrançois Tigeot 				FEEDER_BUILD(eq);
8232a1ad637SFrançois Tigeot 			FEEDER_BUILD(rate);
8242a1ad637SFrançois Tigeot 		}
8252a1ad637SFrançois Tigeot 		if (FEEDEQ_REQUIRED(&cdesc))
8262a1ad637SFrançois Tigeot 			FEEDER_BUILD(eq);
8272a1ad637SFrançois Tigeot 	}
8282a1ad637SFrançois Tigeot 
8292a1ad637SFrançois Tigeot 	if (FEEDFORMAT_REQUIRED(&cdesc))
8302a1ad637SFrançois Tigeot 		FEEDER_BUILD(format);
8312a1ad637SFrançois Tigeot 
8322a1ad637SFrançois Tigeot 	if (c->direction == PCMDIR_REC && (c->flags & CHN_F_HAS_VCHAN))
8332a1ad637SFrançois Tigeot 		FEEDER_BUILD(mixer);
8342a1ad637SFrançois Tigeot 
8352a1ad637SFrançois Tigeot 	sndbuf_setfmt(c->bufsoft, c->format);
8362a1ad637SFrançois Tigeot 	sndbuf_setspd(c->bufsoft, c->speed);
8372a1ad637SFrançois Tigeot 
8382a1ad637SFrançois Tigeot 	sndbuf_setfmt(c->bufhard, hwfmt);
8392a1ad637SFrançois Tigeot 
8402a1ad637SFrançois Tigeot 	chn_syncstate(c);
8412a1ad637SFrançois Tigeot 
8422a1ad637SFrançois Tigeot 	return (0);
8432a1ad637SFrançois Tigeot }
844