feeder.c revision 167644
1/*-
2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/sound/pcm/sound.h>
28
29#include "feeder_if.h"
30
31SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder.c 167644 2007-03-16 17:15:33Z ariff $");
32
33MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
34
35#define MAXFEEDERS 	256
36#undef FEEDER_DEBUG
37
38int feeder_buffersize = FEEDBUFSZ;
39TUNABLE_INT("hw.snd.feeder_buffersize", &feeder_buffersize);
40
41#ifdef SND_DEBUG
42static int
43sysctl_hw_snd_feeder_buffersize(SYSCTL_HANDLER_ARGS)
44{
45	int i, err, val;
46
47	val = feeder_buffersize;
48	err = sysctl_handle_int(oidp, &val, sizeof(val), req);
49
50	if (err != 0 || req->newptr == NULL)
51		return err;
52
53	if (val < FEEDBUFSZ_MIN || val > FEEDBUFSZ_MAX)
54		return EINVAL;
55
56	i = 0;
57	while (val >> i)
58		i++;
59	i = 1 << i;
60	if (i > val && (i >> 1) > 0 && (i >> 1) >= ((val * 3) >> 2))
61		i >>= 1;
62
63	feeder_buffersize = i;
64
65	return err;
66}
67SYSCTL_PROC(_hw_snd, OID_AUTO, feeder_buffersize, CTLTYPE_INT | CTLFLAG_RW,
68	0, sizeof(int), sysctl_hw_snd_feeder_buffersize, "I",
69	"feeder buffer size");
70#else
71SYSCTL_INT(_hw_snd, OID_AUTO, feeder_buffersize, CTLFLAG_RD,
72	&feeder_buffersize, FEEDBUFSZ, "feeder buffer size");
73#endif
74
75struct feedertab_entry {
76	SLIST_ENTRY(feedertab_entry) link;
77	struct feeder_class *feederclass;
78	struct pcm_feederdesc *desc;
79
80	int idx;
81};
82static SLIST_HEAD(, feedertab_entry) feedertab;
83
84/*****************************************************************************/
85
86void
87feeder_register(void *p)
88{
89	static int feedercnt = 0;
90
91	struct feeder_class *fc = p;
92	struct feedertab_entry *fte;
93	int i;
94
95	if (feedercnt == 0) {
96		KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
97
98		SLIST_INIT(&feedertab);
99		fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
100		if (fte == NULL) {
101			printf("can't allocate memory for root feeder: %s\n",
102			    fc->name);
103
104			return;
105		}
106		fte->feederclass = fc;
107		fte->desc = NULL;
108		fte->idx = feedercnt;
109		SLIST_INSERT_HEAD(&feedertab, fte, link);
110		feedercnt++;
111
112		/* initialize global variables */
113
114		if (snd_verbose < 0 || snd_verbose > 3)
115			snd_verbose = 1;
116
117		if (snd_unit < 0 || snd_unit > PCMMAXDEV)
118			snd_unit = 0;
119
120		if (snd_maxautovchans < 0 ||
121		    snd_maxautovchans > SND_MAXVCHANS)
122			snd_maxautovchans = 0;
123
124		if (chn_latency < CHN_LATENCY_MIN ||
125		    chn_latency > CHN_LATENCY_MAX)
126			chn_latency = CHN_LATENCY_DEFAULT;
127
128		if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN ||
129		    chn_latency_profile > CHN_LATENCY_PROFILE_MAX)
130			chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
131
132		if (feeder_buffersize < FEEDBUFSZ_MIN ||
133		    	    feeder_buffersize > FEEDBUFSZ_MAX)
134			feeder_buffersize = FEEDBUFSZ;
135
136		if (feeder_rate_min < FEEDRATE_MIN ||
137			    feeder_rate_max < FEEDRATE_MIN ||
138			    feeder_rate_min > FEEDRATE_MAX ||
139			    feeder_rate_max > FEEDRATE_MAX ||
140			    !(feeder_rate_min < feeder_rate_max)) {
141			feeder_rate_min = FEEDRATE_RATEMIN;
142			feeder_rate_max = FEEDRATE_RATEMAX;
143		}
144
145		if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN ||
146		    	    feeder_rate_round > FEEDRATE_ROUNDHZ_MAX)
147			feeder_rate_round = FEEDRATE_ROUNDHZ;
148
149		if (bootverbose)
150			printf("%s: snd_unit=%d snd_maxautovchans=%d "
151			    "latency=%d feeder_buffersize=%d "
152			    "feeder_rate_min=%d feeder_rate_max=%d "
153			    "feeder_rate_round=%d\n",
154			    __func__, snd_unit, snd_maxautovchans,
155			    chn_latency, feeder_buffersize,
156			    feeder_rate_min, feeder_rate_max,
157			    feeder_rate_round);
158
159		/* we've got our root feeder so don't veto pcm loading anymore */
160		pcm_veto_load = 0;
161
162		return;
163	}
164
165	KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
166
167	/* beyond this point failure is non-fatal but may result in some translations being unavailable */
168	i = 0;
169	while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
170		/* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
171		fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
172		if (fte == NULL) {
173			printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
174
175			return;
176		}
177		fte->feederclass = fc;
178		fte->desc = &fc->desc[i];
179		fte->idx = feedercnt;
180		fte->desc->idx = feedercnt;
181		SLIST_INSERT_HEAD(&feedertab, fte, link);
182		i++;
183	}
184	feedercnt++;
185	if (feedercnt >= MAXFEEDERS)
186		printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
187}
188
189static void
190feeder_unregisterall(void *p)
191{
192	struct feedertab_entry *fte, *next;
193
194	next = SLIST_FIRST(&feedertab);
195	while (next != NULL) {
196		fte = next;
197		next = SLIST_NEXT(fte, link);
198		free(fte, M_FEEDER);
199	}
200}
201
202static int
203cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
204{
205	return ((n->type == m->type) &&
206		((n->in == 0) || (n->in == m->in)) &&
207		((n->out == 0) || (n->out == m->out)) &&
208		(n->flags == m->flags));
209}
210
211static void
212feeder_destroy(struct pcm_feeder *f)
213{
214	FEEDER_FREE(f);
215	kobj_delete((kobj_t)f, M_FEEDER);
216}
217
218static struct pcm_feeder *
219feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
220{
221	struct pcm_feeder *f;
222	int err;
223
224	f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
225	if (f == NULL)
226		return NULL;
227
228	f->align = fc->align;
229	f->data = fc->data;
230	f->source = NULL;
231	f->parent = NULL;
232	f->class = fc;
233	f->desc = &(f->desc_static);
234
235	if (desc) {
236		*(f->desc) = *desc;
237	} else {
238		f->desc->type = FEEDER_ROOT;
239		f->desc->in = 0;
240		f->desc->out = 0;
241		f->desc->flags = 0;
242		f->desc->idx = 0;
243	}
244
245	err = FEEDER_INIT(f);
246	if (err) {
247		printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
248		feeder_destroy(f);
249
250		return NULL;
251	}
252
253	return f;
254}
255
256struct feeder_class *
257feeder_getclass(struct pcm_feederdesc *desc)
258{
259	struct feedertab_entry *fte;
260
261	SLIST_FOREACH(fte, &feedertab, link) {
262		if ((desc == NULL) && (fte->desc == NULL))
263			return fte->feederclass;
264		if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
265			return fte->feederclass;
266	}
267	return NULL;
268}
269
270int
271chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
272{
273	struct pcm_feeder *nf;
274
275	nf = feeder_create(fc, desc);
276	if (nf == NULL)
277		return ENOSPC;
278
279	nf->source = c->feeder;
280
281	/* XXX we should use the lowest common denominator for align */
282	if (nf->align > 0)
283		c->align += nf->align;
284	else if (nf->align < 0 && c->align < -nf->align)
285		c->align = -nf->align;
286	if (c->feeder != NULL)
287		c->feeder->parent = nf;
288	c->feeder = nf;
289
290	return 0;
291}
292
293int
294chn_removefeeder(struct pcm_channel *c)
295{
296	struct pcm_feeder *f;
297
298	if (c->feeder == NULL)
299		return -1;
300	f = c->feeder;
301	c->feeder = c->feeder->source;
302	feeder_destroy(f);
303
304	return 0;
305}
306
307struct pcm_feeder *
308chn_findfeeder(struct pcm_channel *c, u_int32_t type)
309{
310	struct pcm_feeder *f;
311
312	f = c->feeder;
313	while (f != NULL) {
314		if (f->desc->type == type)
315			return f;
316		f = f->source;
317	}
318
319	return NULL;
320}
321
322static int
323chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
324{
325	u_int32_t visited[MAXFEEDERS / 32];
326	u_int32_t idx, mask;
327
328	bzero(visited, sizeof(visited));
329	while (test && (test != stop)) {
330		idx = test->desc->idx;
331		if (idx < 0)
332			panic("bad idx %d", idx);
333		if (idx >= MAXFEEDERS)
334			panic("bad idx %d", idx);
335		mask = 1 << (idx & 31);
336		idx >>= 5;
337		if (visited[idx] & mask)
338			return 0;
339		visited[idx] |= mask;
340		test = test->source;
341	}
342
343	return 1;
344}
345
346/*
347 * See feeder_fmtchain() for the mumbo-jumbo ridiculous explaination
348 * of what the heck is this FMT_Q_*
349 */
350#define FMT_Q_UP	1
351#define FMT_Q_DOWN	2
352#define FMT_Q_EQ	3
353#define FMT_Q_MULTI	4
354
355/*
356 * 14bit format scoring
357 * --------------------
358 *
359 *  13  12  11  10   9   8        2        1   0    offset
360 * +---+---+---+---+---+---+-------------+---+---+
361 * | X | X | X | X | X | X | X X X X X X | X | X |
362 * +---+---+---+---+---+---+-------------+---+---+
363 *   |   |   |   |   |   |        |        |   |
364 *   |   |   |   |   |   |        |        |   +--> signed?
365 *   |   |   |   |   |   |        |        |
366 *   |   |   |   |   |   |        |        +------> bigendian?
367 *   |   |   |   |   |   |        |
368 *   |   |   |   |   |   |        +---------------> total channels
369 *   |   |   |   |   |   |
370 *   |   |   |   |   |   +------------------------> AFMT_A_LAW
371 *   |   |   |   |   |
372 *   |   |   |   |   +----------------------------> AFMT_MU_LAW
373 *   |   |   |   |
374 *   |   |   |   +--------------------------------> AFMT_8BIT
375 *   |   |   |
376 *   |   |   +------------------------------------> AFMT_16BIT
377 *   |   |
378 *   |   +----------------------------------------> AFMT_24BIT
379 *   |
380 *   +--------------------------------------------> AFMT_32BIT
381 */
382#define score_signeq(s1, s2)	(((s1) & 0x1) == ((s2) & 0x1))
383#define score_endianeq(s1, s2)	(((s1) & 0x2) == ((s2) & 0x2))
384#define score_cheq(s1, s2)	(((s1) & 0xfc) == ((s2) & 0xfc))
385#define score_val(s1)		((s1) & 0x3f00)
386#define score_cse(s1)		((s1) & 0x7f)
387
388u_int32_t
389chn_fmtscore(u_int32_t fmt)
390{
391	u_int32_t ret;
392
393	ret = 0;
394	if (fmt & AFMT_SIGNED)
395		ret |= 1 << 0;
396	if (fmt & AFMT_BIGENDIAN)
397		ret |= 1 << 1;
398	if (fmt & AFMT_STEREO)
399		ret |= (2 & 0x3f) << 2;
400	else
401		ret |= (1 & 0x3f) << 2;
402	if (fmt & AFMT_A_LAW)
403		ret |= 1 << 8;
404	else if (fmt & AFMT_MU_LAW)
405		ret |= 1 << 9;
406	else if (fmt & AFMT_8BIT)
407		ret |= 1 << 10;
408	else if (fmt & AFMT_16BIT)
409		ret |= 1 << 11;
410	else if (fmt & AFMT_24BIT)
411		ret |= 1 << 12;
412	else if (fmt & AFMT_32BIT)
413		ret |= 1 << 13;
414
415	return ret;
416}
417
418static u_int32_t
419chn_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq)
420{
421	u_int32_t best, score, score2, oldscore;
422	int i;
423
424	if (fmt == 0 || fmts == NULL || fmts[0] == 0)
425		return 0;
426
427	if (fmtvalid(fmt, fmts))
428		return fmt;
429
430	best = 0;
431	score = chn_fmtscore(fmt);
432	oldscore = 0;
433	for (i = 0; fmts[i] != 0; i++) {
434		score2 = chn_fmtscore(fmts[i]);
435		if (cheq && !score_cheq(score, score2))
436			continue;
437		if (oldscore == 0 ||
438			    (score_val(score2) == score_val(score)) ||
439			    (score_val(score2) == score_val(oldscore)) ||
440			    (score_val(score2) > score_val(oldscore) &&
441			    score_val(score2) < score_val(score)) ||
442			    (score_val(score2) < score_val(oldscore) &&
443			    score_val(score2) > score_val(score)) ||
444			    (score_val(oldscore) < score_val(score) &&
445			    score_val(score2) > score_val(oldscore))) {
446			if (score_val(oldscore) != score_val(score2) ||
447				    score_cse(score) == score_cse(score2) ||
448				    ((score_cse(oldscore) != score_cse(score) &&
449				    !score_endianeq(score, oldscore) &&
450				    (score_endianeq(score, score2) ||
451				    (!score_signeq(score, oldscore) &&
452				    score_signeq(score, score2)))))) {
453				best = fmts[i];
454				oldscore = score2;
455			}
456		}
457	}
458	return best;
459}
460
461u_int32_t
462chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
463{
464	return chn_fmtbestfunc(fmt, fmts, 0);
465}
466
467u_int32_t
468chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts)
469{
470	return chn_fmtbestfunc(fmt, fmts, 1);
471}
472
473u_int32_t
474chn_fmtbest(u_int32_t fmt, u_int32_t *fmts)
475{
476	u_int32_t best1, best2;
477	u_int32_t score, score1, score2;
478
479	if (fmtvalid(fmt, fmts))
480		return fmt;
481
482	best1 = chn_fmtbeststereo(fmt, fmts);
483	best2 = chn_fmtbestbit(fmt, fmts);
484
485	if (best1 != 0 && best2 != 0 && best1 != best2) {
486		if (fmt & AFMT_STEREO)
487			return best1;
488		else {
489			score = score_val(chn_fmtscore(fmt));
490			score1 = score_val(chn_fmtscore(best1));
491			score2 = score_val(chn_fmtscore(best2));
492			if (score1 == score2 || score1 == score)
493				return best1;
494			else if (score2 == score)
495				return best2;
496			else if (score1 > score2)
497				return best1;
498			return best2;
499		}
500	} else if (best2 == 0)
501		return best1;
502	else
503		return best2;
504}
505
506static struct pcm_feeder *
507feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
508{
509	struct feedertab_entry *fte, *ftebest;
510	struct pcm_feeder *try, *ret;
511	uint32_t fl, qout, qsrc, qdst;
512	int qtype;
513
514	if (to == NULL || to[0] == 0)
515		return NULL;
516
517	DEB(printf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out));
518	if (fmtvalid(source->desc->out, to)) {
519		DEB(printf("got it\n"));
520		return source;
521	}
522
523	if (maxdepth < 0)
524		return NULL;
525
526	/*
527	 * WARNING: THIS IS _NOT_ FOR THE FAINT HEART
528	 * Disclaimer: I don't expect anybody could understand this
529	 *             without deep logical and mathematical analysis
530	 *             involving various unnamed probability theorem.
531	 *
532	 * This "Best Fit Random Chain Selection" (BLEHBLEHWHATEVER) algorithm
533	 * is **extremely** difficult to digest especially when applied to
534	 * large sets / numbers of random chains (feeders), each with
535	 * unique characteristic providing different sets of in/out format.
536	 *
537	 * Basically, our FEEDER_FMT (see feeder_fmt.c) chains characteristic:
538	 * 1) Format chains
539	 *    1.1 "8bit to any, not to 8bit"
540	 *      1.1.1 sign can remain consistent, e.g: u8 -> u16[le|be]
541	 *      1.1.2 sign can be changed, e.g: u8 -> s16[le|be]
542	 *      1.1.3 endian can be changed, e.g: u8 -> u16[le|be]
543	 *      1.1.4 both can be changed, e.g: u8 -> [u|s]16[le|be]
544	 *    1.2 "Any to 8bit, not from 8bit"
545	 *      1.2.1 sign can remain consistent, e.g: s16le -> s8
546	 *      1.2.2 sign can be changed, e.g: s16le -> u8
547	 *      1.2.3 source endian can be anything e.g: s16[le|be] -> s8
548	 *      1.2.4 source endian / sign can be anything e.g: [u|s]16[le|be] -> u8
549	 *    1.3 "Any to any where BOTH input and output either 8bit or non-8bit"
550	 *      1.3.1 endian MUST remain consistent
551	 *      1.3.2 sign CAN be changed
552	 *    1.4 "Long jump" is allowed, e.g: from 16bit to 32bit, excluding
553	 *        16bit to 24bit .
554	 * 2) Channel chains (mono <-> stereo)
555	 *    2.1 Both endian and sign MUST remain consistent
556	 * 3) Endian chains (big endian <-> little endian)
557	 *    3.1 Channels and sign MUST remain consistent
558	 * 4) Sign chains (signed <-> unsigned)
559	 *    4.1 Channels and endian MUST remain consistent
560	 *
561	 * .. and the mother of all chaining rules:
562	 *
563	 * Rules 0: Source and destination MUST not contain multiple selections.
564	 *          (qtype != FMT_Q_MULTI)
565	 *
566	 * First of all, our caller ( chn_fmtchain() ) will reduce the possible
567	 * multiple from/to formats to a single best format using chn_fmtbest().
568	 * Then, using chn_fmtscore(), we determine the chaining characteristic.
569	 * Our main goal is to narrow it down until it reach FMT_Q_EQ chaining
570	 * type while still adhering above chaining rules.
571	 *
572	 * The need for this complicated chaining procedures is inevitable,
573	 * since currently we have more than 200 different types of FEEDER_FMT
574	 * doing various unique format conversion. Without this (the old way),
575	 * it is possible to generate broken chain since it doesn't do any
576	 * sanity checking to ensure that the output format is "properly aligned"
577	 * with the direction of conversion (quality up/down/equal).
578	 *
579	 *   Conversion: s24le to s32le
580	 *   Possible chain: 1) s24le -> s32le (correct, optimized)
581	 *                   2) s24le -> s16le -> s32le
582	 *                      (since we have feeder_24to16 and feeder_16to32)
583	 *                      +-- obviously broken!
584	 *
585	 * Using scoring mechanisme, this will ensure that the chaining
586	 * process do the right thing, or at least, give the best chain
587	 * possible without causing quality (the 'Q') degradation.
588	 */
589
590	qdst = chn_fmtscore(to[0]);
591	qsrc = chn_fmtscore(source->desc->out);
592
593#define score_q(s1)			score_val(s1)
594#define score_8bit(s1)			((s1) & 0x700)
595#define score_non8bit(s1)		(!score_8bit(s1))
596#define score_across8bit(s1, s2)	((score_8bit(s1) && score_non8bit(s2)) || \
597					(score_8bit(s2) && score_non8bit(s1)))
598
599#define FMT_CHAIN_Q_UP(s1, s2)		(score_q(s1) < score_q(s2))
600#define FMT_CHAIN_Q_DOWN(s1, s2)	(score_q(s1) > score_q(s2))
601#define FMT_CHAIN_Q_EQ(s1, s2)		(score_q(s1) == score_q(s2))
602#define FMT_Q_DOWN_FLAGS(s1, s2)	(0x1 | (score_across8bit(s1, s2) ? \
603						0x2 : 0x0))
604#define FMT_Q_UP_FLAGS(s1, s2)		FMT_Q_DOWN_FLAGS(s1, s2)
605#define FMT_Q_EQ_FLAGS(s1, s2)		(0x3ffc | \
606					((score_cheq(s1, s2) && \
607						score_endianeq(s1, s2)) ? \
608						0x1 : 0x0) | \
609					((score_cheq(s1, s2) && \
610						score_signeq(s1, s2)) ? \
611						0x2 : 0x0))
612
613	/* Determine chaining direction and set matching flag */
614	fl = 0x3fff;
615	if (to[1] != 0) {
616		qtype = FMT_Q_MULTI;
617		printf("%s: WARNING: FMT_Q_MULTI chaining. Expect the unexpected.\n", __func__);
618	} else if (FMT_CHAIN_Q_DOWN(qsrc, qdst)) {
619		qtype = FMT_Q_DOWN;
620		fl = FMT_Q_DOWN_FLAGS(qsrc, qdst);
621	} else if (FMT_CHAIN_Q_UP(qsrc, qdst)) {
622		qtype = FMT_Q_UP;
623		fl = FMT_Q_UP_FLAGS(qsrc, qdst);
624	} else {
625		qtype = FMT_Q_EQ;
626		fl = FMT_Q_EQ_FLAGS(qsrc, qdst);
627	}
628
629	ftebest = NULL;
630
631	SLIST_FOREACH(fte, &feedertab, link) {
632		if (fte->desc == NULL)
633			continue;
634		if (fte->desc->type != FEEDER_FMT)
635			continue;
636		qout = chn_fmtscore(fte->desc->out);
637#define FMT_Q_MULTI_VALIDATE(qt)		((qt) == FMT_Q_MULTI)
638#define FMT_Q_FL_MATCH(qfl, s1, s2)		(((s1) & (qfl)) == ((s2) & (qfl)))
639#define FMT_Q_UP_VALIDATE(qt, s1, s2, s3)	((qt) == FMT_Q_UP && \
640						score_q(s3) >= score_q(s1) && \
641						score_q(s3) <= score_q(s2))
642#define FMT_Q_DOWN_VALIDATE(qt, s1, s2, s3)	((qt) == FMT_Q_DOWN && \
643						score_q(s3) <= score_q(s1) && \
644						score_q(s3) >= score_q(s2))
645#define FMT_Q_EQ_VALIDATE(qt, s1, s2)		((qt) == FMT_Q_EQ && \
646						score_q(s1) == score_q(s2))
647		if (fte->desc->in == source->desc->out &&
648			    (FMT_Q_MULTI_VALIDATE(qtype) ||
649			    (FMT_Q_FL_MATCH(fl, qout, qdst) &&
650			    (FMT_Q_UP_VALIDATE(qtype, qsrc, qdst, qout) ||
651			    FMT_Q_DOWN_VALIDATE(qtype, qsrc, qdst, qout) ||
652			    FMT_Q_EQ_VALIDATE(qtype, qdst, qout))))) {
653			try = feeder_create(fte->feederclass, fte->desc);
654			if (try) {
655				try->source = source;
656				ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
657				if (ret != NULL)
658					return ret;
659				feeder_destroy(try);
660			}
661		} else if (fte->desc->in == source->desc->out) {
662			/* XXX quality must be considered! */
663			if (ftebest == NULL)
664				ftebest = fte;
665		}
666	}
667
668	if (ftebest != NULL) {
669		try = feeder_create(ftebest->feederclass, ftebest->desc);
670		if (try) {
671			try->source = source;
672			ret = chainok(try, stop) ? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
673			if (ret != NULL)
674				return ret;
675			feeder_destroy(try);
676		}
677	}
678
679	/* printf("giving up %s...\n", source->class->name); */
680
681	return NULL;
682}
683
684u_int32_t
685chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
686{
687	struct pcm_feeder *try, *del, *stop;
688	u_int32_t tmpfrom[2], tmpto[2], best, *from;
689	int i, max, bestmax;
690
691	KASSERT(c != NULL, ("c == NULL"));
692	KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
693	KASSERT(to != NULL, ("to == NULL"));
694	KASSERT(to[0] != 0, ("to[0] == 0"));
695
696	if (c == NULL || c->feeder == NULL || to == NULL || to[0] == 0)
697		return 0;
698
699	stop = c->feeder;
700	best = 0;
701
702	if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
703		from = chn_getcaps(c)->fmtlist;
704		if (from[1] != 0) {
705			best = chn_fmtbest(to[0], from);
706			if (best != 0) {
707				tmpfrom[0] = best;
708				tmpfrom[1] = 0;
709				from = tmpfrom;
710			}
711		}
712	} else {
713		tmpfrom[0] = c->feeder->desc->out;
714		tmpfrom[1] = 0;
715		from = tmpfrom;
716		if (to[1] != 0) {
717			best = chn_fmtbest(from[0], to);
718			if (best != 0) {
719				tmpto[0] = best;
720				tmpto[1] = 0;
721				to = tmpto;
722			}
723		}
724	}
725
726#define FEEDER_FMTCHAIN_MAXDEPTH	8
727
728	try = NULL;
729
730	if (to[0] != 0 && from[0] != 0 &&
731		    to[1] == 0 && from[1] == 0) {
732		max = 0;
733		best = from[0];
734		c->feeder->desc->out = best;
735		do {
736			try = feeder_fmtchain(to, c->feeder, stop, max);
737			DEB(if (try != NULL) {
738				printf("%s: 0x%08x -> 0x%08x (maxdepth: %d)\n",
739					__func__, from[0], to[0], max);
740			});
741		} while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH);
742	} else {
743		printf("%s: Using the old-way format chaining!\n", __func__);
744		i = 0;
745		best = 0;
746		bestmax = 100;
747		while (from[i] != 0) {
748			c->feeder->desc->out = from[i];
749			try = NULL;
750			max = 0;
751			do {
752				try = feeder_fmtchain(to, c->feeder, stop, max);
753			} while (try == NULL && max++ < FEEDER_FMTCHAIN_MAXDEPTH);
754			if (try != NULL && max < bestmax) {
755				bestmax = max;
756				best = from[i];
757			}
758			while (try != NULL && try != stop) {
759				del = try;
760				try = try->source;
761				feeder_destroy(del);
762			}
763			i++;
764		}
765		if (best == 0)
766			return 0;
767
768		c->feeder->desc->out = best;
769		try = feeder_fmtchain(to, c->feeder, stop, bestmax);
770	}
771	if (try == NULL)
772		return 0;
773
774	c->feeder = try;
775	c->align = 0;
776#ifdef FEEDER_DEBUG
777	printf("\n\nchain: ");
778#endif
779	while (try && (try != stop)) {
780#ifdef FEEDER_DEBUG
781		printf("%s [%d]", try->class->name, try->desc->idx);
782		if (try->source)
783			printf(" -> ");
784#endif
785		if (try->source)
786			try->source->parent = try;
787		if (try->align > 0)
788			c->align += try->align;
789		else if (try->align < 0 && c->align < -try->align)
790			c->align = -try->align;
791		try = try->source;
792	}
793#ifdef FEEDER_DEBUG
794	printf("%s [%d]\n", try->class->name, try->desc->idx);
795#endif
796
797	if (c->direction == PCMDIR_REC) {
798		try = c->feeder;
799		while (try != NULL) {
800			if (try->desc->type == FEEDER_ROOT)
801				return try->desc->out;
802			try = try->source;
803		}
804		return best;
805	} else
806		return c->feeder->desc->out;
807}
808
809void
810feeder_printchain(struct pcm_feeder *head)
811{
812	struct pcm_feeder *f;
813
814	printf("feeder chain (head @%p)\n", head);
815	f = head;
816	while (f != NULL) {
817		printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
818		f = f->source;
819	}
820	printf("[end]\n\n");
821}
822
823/*****************************************************************************/
824
825static int
826feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
827{
828	struct snd_dbuf *src = source;
829	int l, offset;
830
831	KASSERT(count > 0, ("feed_root: count == 0"));
832	/* count &= ~((1 << ch->align) - 1); */
833	KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
834
835	if (++ch->feedcount == 0)
836		ch->feedcount = 2;
837
838	l = min(count, sndbuf_getready(src));
839
840	/* When recording only return as much data as available */
841	if (ch->direction == PCMDIR_REC) {
842		sndbuf_dispose(src, buffer, l);
843		return l;
844	}
845
846
847	offset = count - l;
848
849	if (offset > 0) {
850		if (snd_verbose > 3)
851			printf("%s: (%s) %spending %d bytes "
852			    "(count=%d l=%d feed=%d)\n",
853			    __func__,
854			    (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
855			    (ch->feedcount == 1) ? "pre" : "ap",
856			    offset, count, l, ch->feedcount);
857
858		if (ch->feedcount == 1) {
859			memset(buffer,
860			    sndbuf_zerodata(sndbuf_getfmt(src)),
861			    offset);
862			if (l > 0)
863				sndbuf_dispose(src, buffer + offset, l);
864			else
865				ch->feedcount--;
866		} else {
867			if (l > 0)
868				sndbuf_dispose(src, buffer, l);
869#if 1
870			memset(buffer + l,
871			    sndbuf_zerodata(sndbuf_getfmt(src)),
872			    offset);
873			if (!(ch->flags & CHN_F_CLOSING))
874				ch->xruns++;
875#else
876			if (l < 1 || (ch->flags & CHN_F_CLOSING)) {
877				memset(buffer + l,
878				    sndbuf_zerodata(sndbuf_getfmt(src)),
879				    offset);
880				if (!(ch->flags & CHN_F_CLOSING))
881					ch->xruns++;
882			} else {
883				int cp, tgt;
884
885				tgt = l;
886				while (offset > 0) {
887					cp = min(l, offset);
888					memcpy(buffer + tgt, buffer, cp);
889					offset -= cp;
890					tgt += cp;
891				}
892				ch->xruns++;
893			}
894#endif
895		}
896	} else if (l > 0)
897		sndbuf_dispose(src, buffer, l);
898
899	return count;
900}
901
902static kobj_method_t feeder_root_methods[] = {
903    	KOBJMETHOD(feeder_feed,		feed_root),
904	{ 0, 0 }
905};
906static struct feeder_class feeder_root_class = {
907	.name =		"feeder_root",
908	.methods =	feeder_root_methods,
909	.size =		sizeof(struct pcm_feeder),
910	.align =	0,
911	.desc =		NULL,
912	.data =		NULL,
913};
914SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
915SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
916