Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_snd.h"
#endif

#include <dev/sound/pcm/sound.h>

#include "feeder_if.h"

SND_DECLARE_FILE("$FreeBSD$");

/* chain state */
struct feeder_chain_state {
	uint32_t afmt;				/* audio format */
	uint32_t rate;				/* sampling rate */
	struct pcmchan_matrix *matrix;		/* matrix map */
};

/*
 * chain descriptor that will be passed around from the beginning until the
 * end of chain process.
 */
struct feeder_chain_desc {
	struct feeder_chain_state origin;	/* original state */
	struct feeder_chain_state current;	/* current state */
	struct feeder_chain_state target;	/* target state */
	struct pcm_feederdesc desc;		/* feeder descriptor */
	uint32_t afmt_ne;			/* prefered native endian */
	int mode;				/* chain mode */
	int use_eq;				/* need EQ? */
	int use_matrix;				/* need channel matrixing? */
	int use_volume;				/* need softpcmvol? */
	int dummy;				/* dummy passthrough */
	int expensive;				/* possibly expensive */
};

#define FEEDER_CHAIN_LEAN		0
#define FEEDER_CHAIN_16			1
#define FEEDER_CHAIN_32			2
#define FEEDER_CHAIN_MULTI		3
#define FEEDER_CHAIN_FULLMULTI		4
#define FEEDER_CHAIN_LAST		5

#if defined(SND_FEEDER_FULL_MULTIFORMAT)
#define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_FULLMULTI
#elif defined(SND_FEEDER_MULTIFORMAT)
#define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_MULTI
#else
#define FEEDER_CHAIN_DEFAULT		FEEDER_CHAIN_LEAN
#endif

/*
 * List of prefered formats that might be required during
 * processing. It will be decided through snd_fmtbest().
 */

/* 'Lean' mode, signed 16 or 32 bit native endian. */
static uint32_t feeder_chain_formats_lean[] = {
	AFMT_S16_NE, AFMT_S32_NE,
	0
};

/* Force everything to signed 16 bit native endian. */
static uint32_t feeder_chain_formats_16[] = {
	AFMT_S16_NE,
	0
};

/* Force everything to signed 32 bit native endian. */
static uint32_t feeder_chain_formats_32[] = {
	AFMT_S32_NE,
	0
};

/* Multiple choices, all except 8 bit. */
static uint32_t feeder_chain_formats_multi[] = {
	AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE,
	AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE,
	AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE,
	0
};

/* Everything that is convertible. */
static uint32_t feeder_chain_formats_fullmulti[] = {
	AFMT_S8, AFMT_U8,
	AFMT_S16_LE, AFMT_S16_BE, AFMT_U16_LE, AFMT_U16_BE,
	AFMT_S24_LE, AFMT_S24_BE, AFMT_U24_LE, AFMT_U24_BE,
	AFMT_S32_LE, AFMT_S32_BE, AFMT_U32_LE, AFMT_U32_BE,
	0
};

static uint32_t *feeder_chain_formats[FEEDER_CHAIN_LAST] = {
	[FEEDER_CHAIN_LEAN]      = feeder_chain_formats_lean,
	[FEEDER_CHAIN_16]        = feeder_chain_formats_16,
	[FEEDER_CHAIN_32]        = feeder_chain_formats_32,
	[FEEDER_CHAIN_MULTI]     = feeder_chain_formats_multi,
	[FEEDER_CHAIN_FULLMULTI] = feeder_chain_formats_fullmulti
};

static int feeder_chain_mode = FEEDER_CHAIN_DEFAULT;

#if defined(_KERNEL) && defined(SND_DEBUG) && defined(SND_FEEDER_FULL_MULTIFORMAT)
SYSCTL_INT(_hw_snd, OID_AUTO, feeder_chain_mode, CTLFLAG_RWTUN,
    &feeder_chain_mode, 0,
    "feeder chain mode "
    "(0=lean, 1=16bit, 2=32bit, 3=multiformat, 4=fullmultiformat)");
#endif

/*
 * feeder_build_format(): Chain any format converter.
 */
static int
feeder_build_format(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	struct pcm_feederdesc *desc;
	int ret;

	desc = &(cdesc->desc);
	desc->type = FEEDER_FORMAT;
	desc->in = 0;
	desc->out = 0;
	desc->flags = 0;

	fc = feeder_getclass(desc);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_format\n", __func__);
		return (ENOTSUP);
	}

	desc->in = cdesc->current.afmt;
	desc->out = cdesc->target.afmt;

	ret = chn_addfeeder(c, fc, desc);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_format\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_FORMAT;

	cdesc->current.afmt = cdesc->target.afmt;

	return (0);
}

/*
 * feeder_build_formatne(): Chain format converter that suite best for native
 *                          endian format.
 */
static int
feeder_build_formatne(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_chain_state otarget;
	int ret;

	if (cdesc->afmt_ne == 0 ||
	    AFMT_ENCODING(cdesc->current.afmt) == cdesc->afmt_ne)
		return (0);

	otarget = cdesc->target;
	cdesc->target = cdesc->current;
	cdesc->target.afmt = SND_FORMAT(cdesc->afmt_ne,
	    cdesc->current.matrix->channels, cdesc->current.matrix->ext);

	ret = feeder_build_format(c, cdesc);
	if (ret != 0)
		return (ret);

	cdesc->target = otarget;

	return (0);
}

/*
 * feeder_build_rate(): Chain sample rate converter.
 */
static int
feeder_build_rate(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	struct pcm_feeder *f;
	struct pcm_feederdesc *desc;
	int ret;

	ret = feeder_build_formatne(c, cdesc);
	if (ret != 0)
		return (ret);

	desc = &(cdesc->desc);
	desc->type = FEEDER_RATE;
	desc->in = 0;
	desc->out = 0;
	desc->flags = 0;

	fc = feeder_getclass(desc);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_rate\n", __func__);
		return (ENOTSUP);
	}

	desc->in = cdesc->current.afmt;
	desc->out = desc->in;

	ret = chn_addfeeder(c, fc, desc);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_rate\n", __func__);
		return (ret);
	}

	f = c->feeder;

	/*
	 * If in 'dummy' mode (possibly due to passthrough mode), set the
	 * conversion quality to the lowest possible (should be fastest) since
	 * listener won't be hearing anything. Theoretically we can just
	 * disable it, but that will cause weird runtime behaviour:
	 * application appear to play something that is either too fast or too
	 * slow.
	 */
	if (cdesc->dummy != 0) {
		ret = FEEDER_SET(f, FEEDRATE_QUALITY, 0);
		if (ret != 0) {
			device_printf(c->dev,
			    "%s(): can't set resampling quality\n", __func__);
			return (ret);
		}
	}

	ret = FEEDER_SET(f, FEEDRATE_SRC, cdesc->current.rate);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't set source rate\n", __func__);
		return (ret);
	}

	ret = FEEDER_SET(f, FEEDRATE_DST, cdesc->target.rate);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't set destination rate\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_RATE;

	cdesc->current.rate = cdesc->target.rate;

	return (0);
}

/*
 * feeder_build_matrix(): Chain channel matrixing converter.
 */
static int
feeder_build_matrix(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	struct pcm_feeder *f;
	struct pcm_feederdesc *desc;
	int ret;

	ret = feeder_build_formatne(c, cdesc);
	if (ret != 0)
		return (ret);

	desc = &(cdesc->desc);
	desc->type = FEEDER_MATRIX;
	desc->in = 0;
	desc->out = 0;
	desc->flags = 0;

	fc = feeder_getclass(desc);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_matrix\n", __func__);
		return (ENOTSUP);
	}

	desc->in = cdesc->current.afmt;
	desc->out = SND_FORMAT(cdesc->current.afmt,
	    cdesc->target.matrix->channels, cdesc->target.matrix->ext);

	ret = chn_addfeeder(c, fc, desc);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_matrix\n", __func__);
		return (ret);
	}

	f = c->feeder;
	ret = feeder_matrix_setup(f, cdesc->current.matrix,
	    cdesc->target.matrix);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): feeder_matrix_setup() failed\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_MATRIX;

	cdesc->current.afmt = desc->out;
	cdesc->current.matrix = cdesc->target.matrix;
	cdesc->use_matrix = 0;

	return (0);
}

/*
 * feeder_build_volume(): Chain soft volume.
 */
static int
feeder_build_volume(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	struct pcm_feeder *f;
	struct pcm_feederdesc *desc;
	int ret;

	ret = feeder_build_formatne(c, cdesc);
	if (ret != 0)
		return (ret);

	desc = &(cdesc->desc);
	desc->type = FEEDER_VOLUME;
	desc->in = 0;
	desc->out = 0;
	desc->flags = 0;

	fc = feeder_getclass(desc);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_volume\n", __func__);
		return (ENOTSUP);
	}

	desc->in = cdesc->current.afmt;
	desc->out = desc->in;

	ret = chn_addfeeder(c, fc, desc);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_volume\n", __func__);
		return (ret);
	}

	f = c->feeder;

	/*
	 * If in 'dummy' mode (possibly due to passthrough mode), set BYPASS
	 * mode since listener won't be hearing anything. Theoretically we can
	 * just disable it, but that will confuse volume per channel mixer.
	 */
	if (cdesc->dummy != 0) {
		ret = FEEDER_SET(f, FEEDVOLUME_STATE, FEEDVOLUME_BYPASS);
		if (ret != 0) {
			device_printf(c->dev,
			    "%s(): can't set volume bypass\n", __func__);
			return (ret);
		}
	}

	ret = feeder_volume_apply_matrix(f, cdesc->current.matrix);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): feeder_volume_apply_matrix() failed\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_VOLUME;

	cdesc->use_volume = 0;

	return (0);
}

/*
 * feeder_build_eq(): Chain parametric software equalizer.
 */
static int
feeder_build_eq(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	struct pcm_feeder *f;
	struct pcm_feederdesc *desc;
	int ret;

	ret = feeder_build_formatne(c, cdesc);
	if (ret != 0)
		return (ret);

	desc = &(cdesc->desc);
	desc->type = FEEDER_EQ;
	desc->in = 0;
	desc->out = 0;
	desc->flags = 0;

	fc = feeder_getclass(desc);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_eq\n", __func__);
		return (ENOTSUP);
	}

	desc->in = cdesc->current.afmt;
	desc->out = desc->in;

	ret = chn_addfeeder(c, fc, desc);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_eq\n", __func__);
		return (ret);
	}

	f = c->feeder;

	ret = FEEDER_SET(f, FEEDEQ_RATE, cdesc->current.rate);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't set rate on feeder_eq\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_EQ;

	cdesc->use_eq = 0;

	return (0);
}

/*
 * feeder_build_root(): Chain root feeder, the top, father of all.
 */
static int
feeder_build_root(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	int ret;

	fc = feeder_getclass(NULL);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_root\n", __func__);
		return (ENOTSUP);
	}

	ret = chn_addfeeder(c, fc, NULL);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_root\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_ROOT;

	c->feeder->desc->in = cdesc->current.afmt;
	c->feeder->desc->out = cdesc->current.afmt;

	return (0);
}

/*
 * feeder_build_mixer(): Chain software mixer for virtual channels.
 */
static int
feeder_build_mixer(struct pcm_channel *c, struct feeder_chain_desc *cdesc)
{
	struct feeder_class *fc;
	struct pcm_feederdesc *desc;
	int ret;

	desc = &(cdesc->desc);
	desc->type = FEEDER_MIXER;
	desc->in = 0;
	desc->out = 0;
	desc->flags = 0;

	fc = feeder_getclass(desc);
	if (fc == NULL) {
		device_printf(c->dev,
		    "%s(): can't find feeder_mixer\n", __func__);
		return (ENOTSUP);
	}

	desc->in = cdesc->current.afmt;
	desc->out = desc->in;

	ret = chn_addfeeder(c, fc, desc);
	if (ret != 0) {
		device_printf(c->dev,
		    "%s(): can't add feeder_mixer\n", __func__);
		return (ret);
	}

	c->feederflags |= 1 << FEEDER_MIXER;

	return (0);
}

/* Macrosses to ease our job doing stuffs later. */
#define FEEDER_BW(c, t)		((c)->t.matrix->channels * (c)->t.rate)

#define FEEDRATE_UP(c)		((c)->target.rate > (c)->current.rate)
#define FEEDRATE_DOWN(c)	((c)->target.rate < (c)->current.rate)
#define FEEDRATE_REQUIRED(c)	(FEEDRATE_UP(c) || FEEDRATE_DOWN(c))

#define FEEDMATRIX_UP(c)	((c)->target.matrix->channels >		\
				 (c)->current.matrix->channels)
#define FEEDMATRIX_DOWN(c)	((c)->target.matrix->channels <		\
				 (c)->current.matrix->channels)
#define FEEDMATRIX_REQUIRED(c)	(FEEDMATRIX_UP(c) ||			\
				 FEEDMATRIX_DOWN(c) || (c)->use_matrix != 0)

#define FEEDFORMAT_REQUIRED(c)	(AFMT_ENCODING((c)->current.afmt) !=	\
				 AFMT_ENCODING((c)->target.afmt))

#define FEEDVOLUME_REQUIRED(c)	((c)->use_volume != 0)

#define FEEDEQ_VALIDRATE(c, t)	(feeder_eq_validrate((c)->t.rate) != 0)
#define FEEDEQ_ECONOMY(c)	(FEEDER_BW(c, current) < FEEDER_BW(c, target))
#define FEEDEQ_REQUIRED(c)	((c)->use_eq != 0 &&			\
				 FEEDEQ_VALIDRATE(c, current))

#define FEEDFORMAT_NE_REQUIRED(c)					\
	((c)->afmt_ne != AFMT_S32_NE &&					\
	(((c)->mode == FEEDER_CHAIN_16 &&				\
	AFMT_ENCODING((c)->current.afmt) != AFMT_S16_NE) ||		\
	((c)->mode == FEEDER_CHAIN_32 &&				\
	AFMT_ENCODING((c)->current.afmt) != AFMT_S32_NE) ||		\
	(c)->mode == FEEDER_CHAIN_FULLMULTI ||				\
	((c)->mode == FEEDER_CHAIN_MULTI &&				\
	((c)->current.afmt & AFMT_8BIT)) ||				\
	((c)->mode == FEEDER_CHAIN_LEAN &&				\
	!((c)->current.afmt & (AFMT_S16_NE | AFMT_S32_NE)))))

static void
feeder_default_matrix(struct pcmchan_matrix *m, uint32_t fmt, int id)
{
	int x;

	memset(m, 0, sizeof(*m));

	m->id = id;
	m->channels = AFMT_CHANNEL(fmt);
	m->ext = AFMT_EXTCHANNEL(fmt);
	for (x = 0; x != SND_CHN_T_MAX; x++)
		m->offset[x] = -1;
}

int
feeder_chain(struct pcm_channel *c)
{
	struct snddev_info *d;
	struct pcmchan_caps *caps;
	struct feeder_chain_desc cdesc;
	struct pcmchan_matrix *hwmatrix, *softmatrix;
	uint32_t hwfmt, softfmt;
	int ret;

	CHN_LOCKASSERT(c);

	/* Remove everything first. */
	while (chn_removefeeder(c) == 0)
		;

	KASSERT(c->feeder == NULL, ("feeder chain not empty"));

	/* clear and populate chain descriptor. */
	bzero(&cdesc, sizeof(cdesc));

	switch (feeder_chain_mode) {
	case FEEDER_CHAIN_LEAN:
	case FEEDER_CHAIN_16:
	case FEEDER_CHAIN_32:
#if defined(SND_FEEDER_MULTIFORMAT) || defined(SND_FEEDER_FULL_MULTIFORMAT)
	case FEEDER_CHAIN_MULTI:
#endif
#if defined(SND_FEEDER_FULL_MULTIFORMAT)
	case FEEDER_CHAIN_FULLMULTI:
#endif
		break;
	default:
		feeder_chain_mode = FEEDER_CHAIN_DEFAULT;
		break;
	}

	cdesc.mode = feeder_chain_mode;
	cdesc.expensive = 1;	/* XXX faster.. */

#define VCHAN_PASSTHROUGH(c)	(((c)->flags & (CHN_F_VIRTUAL |		\
				 CHN_F_PASSTHROUGH)) ==			\
				 (CHN_F_VIRTUAL | CHN_F_PASSTHROUGH))

	/* Get the best possible hardware format. */
	if (VCHAN_PASSTHROUGH(c))
		hwfmt = c->parentchannel->format;
	else {
		caps = chn_getcaps(c);
		if (caps == NULL || caps->fmtlist == NULL) {
			device_printf(c->dev,
			    "%s(): failed to get channel caps\n", __func__);
			return (ENODEV);
		}

		if ((c->format & AFMT_PASSTHROUGH) &&
		    !snd_fmtvalid(c->format, caps->fmtlist))
			return (ENODEV);

		hwfmt = snd_fmtbest(c->format, caps->fmtlist);
		if (hwfmt == 0 || !snd_fmtvalid(hwfmt, caps->fmtlist)) {
			device_printf(c->dev,
			    "%s(): invalid hardware format 0x%08x\n",
			    __func__, hwfmt);
			{
				int i;
				for (i = 0; caps->fmtlist[i] != 0; i++)
					printf("0x%08x\n", caps->fmtlist[i]);
				printf("Req: 0x%08x\n", c->format);
			}
			return (ENODEV);
		}
	}

	/*
	 * The 'hardware' possibly have different intepretation of channel
	 * matrixing, so get it first .....
	 */
	hwmatrix = CHANNEL_GETMATRIX(c->methods, c->devinfo, hwfmt);
	if (hwmatrix == NULL) {
		/* setup a default matrix */
		hwmatrix = &c->matrix_scratch;
		feeder_default_matrix(hwmatrix, hwfmt,
		    SND_CHN_MATRIX_UNKNOWN);
	}
	/* ..... and rebuild hwfmt. */
	hwfmt = SND_FORMAT(hwfmt, hwmatrix->channels, hwmatrix->ext);

	/* Reset and rebuild default channel format/matrix map. */
	softfmt = c->format;
	softmatrix = &c->matrix;
	if (softmatrix->channels != AFMT_CHANNEL(softfmt) ||
	    softmatrix->ext != AFMT_EXTCHANNEL(softfmt)) {
		softmatrix = feeder_matrix_format_map(softfmt);
		if (softmatrix == NULL) {
			/* setup a default matrix */
		  	softmatrix = &c->matrix;
			feeder_default_matrix(softmatrix, softfmt,
			    SND_CHN_MATRIX_PCMCHANNEL);
		} else {
			c->matrix = *softmatrix;
			c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
		}
	}
	softfmt = SND_FORMAT(softfmt, softmatrix->channels, softmatrix->ext);
	if (softfmt != c->format)
		device_printf(c->dev,
		    "%s(): WARNING: %s Soft format 0x%08x -> 0x%08x\n",
		    __func__, CHN_DIRSTR(c), c->format, softfmt);

	/*
	 * PLAY and REC are opposite.
	 */
	if (c->direction == PCMDIR_PLAY) {
		cdesc.origin.afmt    = softfmt;
		cdesc.origin.matrix  = softmatrix;
		cdesc.origin.rate    = c->speed;
		cdesc.target.afmt    = hwfmt;
		cdesc.target.matrix  = hwmatrix;
		cdesc.target.rate    = sndbuf_getspd(c->bufhard);
	} else {
		cdesc.origin.afmt    = hwfmt;
		cdesc.origin.matrix  = hwmatrix;
		cdesc.origin.rate    = sndbuf_getspd(c->bufhard);
		cdesc.target.afmt    = softfmt;
		cdesc.target.matrix  = softmatrix;
		cdesc.target.rate    = c->speed;
	}

	d = c->parentsnddev;

	/*
	 * If channel is in bitperfect or passthrough mode, make it appear
	 * that 'origin' and 'target' identical, skipping mostly chain
	 * procedures.
	 */
	if (CHN_BITPERFECT(c) || (c->format & AFMT_PASSTHROUGH)) {
		if (c->direction == PCMDIR_PLAY)
			cdesc.origin = cdesc.target;
		else
			cdesc.target = cdesc.origin;
		c->format = cdesc.target.afmt;
		c->speed  = cdesc.target.rate;
	} else {
		/* hwfmt is not convertible, so 'dummy' it. */
		if (hwfmt & AFMT_PASSTHROUGH)
			cdesc.dummy = 1;

		if ((softfmt & AFMT_CONVERTIBLE) &&
		    (((d->flags & SD_F_VPC) && !(c->flags & CHN_F_HAS_VCHAN)) ||
		    (!(d->flags & SD_F_VPC) && (d->flags & SD_F_SOFTPCMVOL) &&
		    !(c->flags & CHN_F_VIRTUAL))))
			cdesc.use_volume = 1;

		if (feeder_matrix_compare(cdesc.origin.matrix,
		    cdesc.target.matrix) != 0)
			cdesc.use_matrix = 1;

		/* Soft EQ only applicable for PLAY. */
		if (cdesc.dummy == 0 &&
		    c->direction == PCMDIR_PLAY && (d->flags & SD_F_EQ) &&
		    (((d->flags & SD_F_EQ_PC) &&
		    !(c->flags & CHN_F_HAS_VCHAN)) ||
		    (!(d->flags & SD_F_EQ_PC) && !(c->flags & CHN_F_VIRTUAL))))
			cdesc.use_eq = 1;

		if (FEEDFORMAT_NE_REQUIRED(&cdesc)) {
			cdesc.afmt_ne =
			    (cdesc.dummy != 0) ?
			    snd_fmtbest(AFMT_ENCODING(softfmt),
			    feeder_chain_formats[cdesc.mode]) :
			    snd_fmtbest(AFMT_ENCODING(cdesc.target.afmt),
			    feeder_chain_formats[cdesc.mode]);
			if (cdesc.afmt_ne == 0) {
				device_printf(c->dev,
				    "%s(): snd_fmtbest failed!\n", __func__);
				cdesc.afmt_ne =
				    (((cdesc.dummy != 0) ? softfmt :
				    cdesc.target.afmt) &
				    (AFMT_24BIT | AFMT_32BIT)) ?
				    AFMT_S32_NE : AFMT_S16_NE;
			}
		}
	}

	cdesc.current = cdesc.origin;

	/* Build everything. */

	c->feederflags = 0;

#define FEEDER_BUILD(t)	do {						\
	ret = feeder_build_##t(c, &cdesc);				\
	if (ret != 0)							\
		return (ret);						\
	} while (0)

	if (!(c->flags & CHN_F_HAS_VCHAN) || c->direction == PCMDIR_REC)
		FEEDER_BUILD(root);
	else if (c->direction == PCMDIR_PLAY && (c->flags & CHN_F_HAS_VCHAN))
		FEEDER_BUILD(mixer);
	else
		return (ENOTSUP);

	/*
	 * The basic idea is: The smaller the bandwidth, the cheaper the
	 * conversion process, with following constraints:-
	 *
	 * 1) Almost all feeders work best in 16/32 native endian.
	 * 2) Try to avoid 8bit feeders due to poor dynamic range.
	 * 3) Avoid volume, format, matrix and rate in BITPERFECT or
	 *    PASSTHROUGH mode.
	 * 4) Try putting volume before EQ or rate. Should help to
	 *    avoid/reduce possible clipping.
	 * 5) EQ require specific, valid rate, unless it allow sloppy
	 *    conversion.
	 */
	if (FEEDMATRIX_UP(&cdesc)) {
		if (FEEDEQ_REQUIRED(&cdesc) &&
		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
		    (cdesc.expensive == 0 && FEEDEQ_ECONOMY(&cdesc))))
			FEEDER_BUILD(eq);
		if (FEEDRATE_REQUIRED(&cdesc))
			FEEDER_BUILD(rate);
		FEEDER_BUILD(matrix);
		if (FEEDVOLUME_REQUIRED(&cdesc))
			FEEDER_BUILD(volume);
		if (FEEDEQ_REQUIRED(&cdesc))
			FEEDER_BUILD(eq);
	} else if (FEEDMATRIX_DOWN(&cdesc)) {
		FEEDER_BUILD(matrix);
		if (FEEDVOLUME_REQUIRED(&cdesc))
			FEEDER_BUILD(volume);
		if (FEEDEQ_REQUIRED(&cdesc) &&
		    (!FEEDEQ_VALIDRATE(&cdesc, target) ||
		    FEEDEQ_ECONOMY(&cdesc)))
			FEEDER_BUILD(eq);
		if (FEEDRATE_REQUIRED(&cdesc))
			FEEDER_BUILD(rate);
		if (FEEDEQ_REQUIRED(&cdesc))
			FEEDER_BUILD(eq);
	} else {
		if (FEEDRATE_DOWN(&cdesc)) {
			if (FEEDEQ_REQUIRED(&cdesc) &&
			    !FEEDEQ_VALIDRATE(&cdesc, target)) {
				if (FEEDVOLUME_REQUIRED(&cdesc))
					FEEDER_BUILD(volume);
				FEEDER_BUILD(eq);
			}
			FEEDER_BUILD(rate);
		}
		if (FEEDMATRIX_REQUIRED(&cdesc))
			FEEDER_BUILD(matrix);
		if (FEEDVOLUME_REQUIRED(&cdesc))
			FEEDER_BUILD(volume);
		if (FEEDRATE_UP(&cdesc)) {
			if (FEEDEQ_REQUIRED(&cdesc) &&
			    !FEEDEQ_VALIDRATE(&cdesc, target))
				FEEDER_BUILD(eq);
			FEEDER_BUILD(rate);
		}
		if (FEEDEQ_REQUIRED(&cdesc))
			FEEDER_BUILD(eq);
	}

	if (FEEDFORMAT_REQUIRED(&cdesc))
		FEEDER_BUILD(format);

	if (c->direction == PCMDIR_REC && (c->flags & CHN_F_HAS_VCHAN))
		FEEDER_BUILD(mixer);

	sndbuf_setfmt(c->bufsoft, c->format);
	sndbuf_setspd(c->bufsoft, c->speed);

	sndbuf_setfmt(c->bufhard, hwfmt);

	chn_syncstate(c);

	return (0);
}