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