xref: /freebsd/sys/dev/sound/pcm/feeder.c (revision 357378bbdedf24ce2b90e9bd831af4a9db3ec70a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifdef HAVE_KERNEL_OPTION_HEADERS
31 #include "opt_snd.h"
32 #endif
33 
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pcm/vchan.h>
36 
37 #include "feeder_if.h"
38 
39 static MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
40 
41 #define MAXFEEDERS 	256
42 
43 struct feedertab_entry {
44 	SLIST_ENTRY(feedertab_entry) link;
45 	struct feeder_class *feederclass;
46 	struct pcm_feederdesc *desc;
47 
48 	int idx;
49 };
50 static SLIST_HEAD(, feedertab_entry) feedertab;
51 
52 /*****************************************************************************/
53 
54 void
55 feeder_register(void *p)
56 {
57 	static int feedercnt = 0;
58 
59 	struct feeder_class *fc = p;
60 	struct feedertab_entry *fte;
61 	int i;
62 
63 	if (feedercnt == 0) {
64 		KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
65 
66 		SLIST_INIT(&feedertab);
67 		fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
68 		if (fte == NULL) {
69 			printf("can't allocate memory for root feeder: %s\n",
70 			    fc->name);
71 
72 			return;
73 		}
74 		fte->feederclass = fc;
75 		fte->desc = NULL;
76 		fte->idx = feedercnt;
77 		SLIST_INSERT_HEAD(&feedertab, fte, link);
78 		feedercnt++;
79 
80 		/* initialize global variables */
81 
82 		if (snd_verbose < 0 || snd_verbose > 4)
83 			snd_verbose = 1;
84 
85 		if (snd_unit < 0)
86 			snd_unit = -1;
87 
88 		if (snd_maxautovchans < 0 ||
89 		    snd_maxautovchans > SND_MAXVCHANS)
90 			snd_maxautovchans = 0;
91 
92 		if (chn_latency < CHN_LATENCY_MIN ||
93 		    chn_latency > CHN_LATENCY_MAX)
94 			chn_latency = CHN_LATENCY_DEFAULT;
95 
96 		if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN ||
97 		    chn_latency_profile > CHN_LATENCY_PROFILE_MAX)
98 			chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
99 
100 		if (feeder_rate_min < FEEDRATE_MIN ||
101 			    feeder_rate_max < FEEDRATE_MIN ||
102 			    feeder_rate_min > FEEDRATE_MAX ||
103 			    feeder_rate_max > FEEDRATE_MAX ||
104 			    !(feeder_rate_min < feeder_rate_max)) {
105 			feeder_rate_min = FEEDRATE_RATEMIN;
106 			feeder_rate_max = FEEDRATE_RATEMAX;
107 		}
108 
109 		if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN ||
110 		    	    feeder_rate_round > FEEDRATE_ROUNDHZ_MAX)
111 			feeder_rate_round = FEEDRATE_ROUNDHZ;
112 
113 		if (bootverbose)
114 			printf("%s: snd_unit=%d snd_maxautovchans=%d "
115 			    "latency=%d "
116 			    "feeder_rate_min=%d feeder_rate_max=%d "
117 			    "feeder_rate_round=%d\n",
118 			    __func__, snd_unit, snd_maxautovchans,
119 			    chn_latency,
120 			    feeder_rate_min, feeder_rate_max,
121 			    feeder_rate_round);
122 
123 		/* we've got our root feeder so don't veto pcm loading anymore */
124 		pcm_veto_load = 0;
125 
126 		return;
127 	}
128 
129 	KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
130 
131 	/* beyond this point failure is non-fatal but may result in some translations being unavailable */
132 	i = 0;
133 	while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
134 		/* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
135 		fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
136 		if (fte == NULL) {
137 			printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
138 
139 			return;
140 		}
141 		fte->feederclass = fc;
142 		fte->desc = &fc->desc[i];
143 		fte->idx = feedercnt;
144 		fte->desc->idx = feedercnt;
145 		SLIST_INSERT_HEAD(&feedertab, fte, link);
146 		i++;
147 	}
148 	feedercnt++;
149 	if (feedercnt >= MAXFEEDERS)
150 		printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
151 }
152 
153 static void
154 feeder_unregisterall(void *p)
155 {
156 	struct feedertab_entry *fte, *next;
157 
158 	next = SLIST_FIRST(&feedertab);
159 	while (next != NULL) {
160 		fte = next;
161 		next = SLIST_NEXT(fte, link);
162 		free(fte, M_FEEDER);
163 	}
164 }
165 
166 static int
167 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
168 {
169 	return ((n->type == m->type) &&
170 		((n->in == 0) || (n->in == m->in)) &&
171 		((n->out == 0) || (n->out == m->out)) &&
172 		(n->flags == m->flags));
173 }
174 
175 static void
176 feeder_destroy(struct pcm_feeder *f)
177 {
178 	FEEDER_FREE(f);
179 	kobj_delete((kobj_t)f, M_FEEDER);
180 }
181 
182 static struct pcm_feeder *
183 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
184 {
185 	struct pcm_feeder *f;
186 	int err;
187 
188 	f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
189 	if (f == NULL)
190 		return NULL;
191 
192 	f->data = fc->data;
193 	f->source = NULL;
194 	f->parent = NULL;
195 	f->class = fc;
196 	f->desc = &(f->desc_static);
197 
198 	if (desc) {
199 		*(f->desc) = *desc;
200 	} else {
201 		f->desc->type = FEEDER_ROOT;
202 		f->desc->in = 0;
203 		f->desc->out = 0;
204 		f->desc->flags = 0;
205 		f->desc->idx = 0;
206 	}
207 
208 	err = FEEDER_INIT(f);
209 	if (err) {
210 		printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
211 		feeder_destroy(f);
212 
213 		return NULL;
214 	}
215 
216 	return f;
217 }
218 
219 struct feeder_class *
220 feeder_getclass(struct pcm_feederdesc *desc)
221 {
222 	struct feedertab_entry *fte;
223 
224 	SLIST_FOREACH(fte, &feedertab, link) {
225 		if ((desc == NULL) && (fte->desc == NULL))
226 			return fte->feederclass;
227 		if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
228 			return fte->feederclass;
229 	}
230 	return NULL;
231 }
232 
233 int
234 feeder_add(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
235 {
236 	struct pcm_feeder *nf;
237 
238 	nf = feeder_create(fc, desc);
239 	if (nf == NULL)
240 		return ENOSPC;
241 
242 	nf->source = c->feeder;
243 
244 	if (c->feeder != NULL)
245 		c->feeder->parent = nf;
246 	c->feeder = nf;
247 
248 	return 0;
249 }
250 
251 void
252 feeder_remove(struct pcm_channel *c)
253 {
254 	struct pcm_feeder *f;
255 
256 	while (c->feeder != NULL) {
257 		f = c->feeder;
258 		c->feeder = c->feeder->source;
259 		feeder_destroy(f);
260 	}
261 }
262 
263 struct pcm_feeder *
264 feeder_find(struct pcm_channel *c, u_int32_t type)
265 {
266 	struct pcm_feeder *f;
267 
268 	f = c->feeder;
269 	while (f != NULL) {
270 		if (f->desc->type == type)
271 			return f;
272 		f = f->source;
273 	}
274 
275 	return NULL;
276 }
277 
278 /*
279  * 14bit format scoring
280  * --------------------
281  *
282  *  13  12  11  10   9   8        2        1   0    offset
283  * +---+---+---+---+---+---+-------------+---+---+
284  * | X | X | X | X | X | X | X X X X X X | X | X |
285  * +---+---+---+---+---+---+-------------+---+---+
286  *   |   |   |   |   |   |        |        |   |
287  *   |   |   |   |   |   |        |        |   +--> signed?
288  *   |   |   |   |   |   |        |        |
289  *   |   |   |   |   |   |        |        +------> bigendian?
290  *   |   |   |   |   |   |        |
291  *   |   |   |   |   |   |        +---------------> total channels
292  *   |   |   |   |   |   |
293  *   |   |   |   |   |   +------------------------> AFMT_A_LAW
294  *   |   |   |   |   |
295  *   |   |   |   |   +----------------------------> AFMT_MU_LAW
296  *   |   |   |   |
297  *   |   |   |   +--------------------------------> AFMT_8BIT
298  *   |   |   |
299  *   |   |   +------------------------------------> AFMT_16BIT
300  *   |   |
301  *   |   +----------------------------------------> AFMT_24BIT
302  *   |
303  *   +--------------------------------------------> AFMT_32BIT
304  */
305 #define score_signeq(s1, s2)	(((s1) & 0x1) == ((s2) & 0x1))
306 #define score_endianeq(s1, s2)	(((s1) & 0x2) == ((s2) & 0x2))
307 #define score_cheq(s1, s2)	(((s1) & 0xfc) == ((s2) & 0xfc))
308 #define score_chgt(s1, s2)	(((s1) & 0xfc) > ((s2) & 0xfc))
309 #define score_chlt(s1, s2)	(((s1) & 0xfc) < ((s2) & 0xfc))
310 #define score_val(s1)		((s1) & 0x3f00)
311 #define score_cse(s1)		((s1) & 0x7f)
312 
313 u_int32_t
314 snd_fmtscore(u_int32_t fmt)
315 {
316 	u_int32_t ret;
317 
318 	ret = 0;
319 	if (fmt & AFMT_SIGNED)
320 		ret |= 1 << 0;
321 	if (fmt & AFMT_BIGENDIAN)
322 		ret |= 1 << 1;
323 	/*if (fmt & AFMT_STEREO)
324 		ret |= (2 & 0x3f) << 2;
325 	else
326 		ret |= (1 & 0x3f) << 2;*/
327 	ret |= (AFMT_CHANNEL(fmt) & 0x3f) << 2;
328 	if (fmt & AFMT_A_LAW)
329 		ret |= 1 << 8;
330 	else if (fmt & AFMT_MU_LAW)
331 		ret |= 1 << 9;
332 	else if (fmt & AFMT_8BIT)
333 		ret |= 1 << 10;
334 	else if (fmt & AFMT_16BIT)
335 		ret |= 1 << 11;
336 	else if (fmt & AFMT_24BIT)
337 		ret |= 1 << 12;
338 	else if (fmt & AFMT_32BIT)
339 		ret |= 1 << 13;
340 
341 	return ret;
342 }
343 
344 static u_int32_t
345 snd_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq)
346 {
347 	u_int32_t best, score, score2, oldscore;
348 	int i;
349 
350 	if (fmt == 0 || fmts == NULL || fmts[0] == 0)
351 		return 0;
352 
353 	if (snd_fmtvalid(fmt, fmts))
354 		return fmt;
355 
356 	best = 0;
357 	score = snd_fmtscore(fmt);
358 	oldscore = 0;
359 	for (i = 0; fmts[i] != 0; i++) {
360 		score2 = snd_fmtscore(fmts[i]);
361 		if (cheq && !score_cheq(score, score2) &&
362 		    (score_chlt(score2, score) ||
363 		    (oldscore != 0 && score_chgt(score2, oldscore))))
364 				continue;
365 		if (oldscore == 0 ||
366 			    (score_val(score2) == score_val(score)) ||
367 			    (score_val(score2) == score_val(oldscore)) ||
368 			    (score_val(score2) > score_val(oldscore) &&
369 			    score_val(score2) < score_val(score)) ||
370 			    (score_val(score2) < score_val(oldscore) &&
371 			    score_val(score2) > score_val(score)) ||
372 			    (score_val(oldscore) < score_val(score) &&
373 			    score_val(score2) > score_val(oldscore))) {
374 			if (score_val(oldscore) != score_val(score2) ||
375 				    score_cse(score) == score_cse(score2) ||
376 				    ((score_cse(oldscore) != score_cse(score) &&
377 				    !score_endianeq(score, oldscore) &&
378 				    (score_endianeq(score, score2) ||
379 				    (!score_signeq(score, oldscore) &&
380 				    score_signeq(score, score2)))))) {
381 				best = fmts[i];
382 				oldscore = score2;
383 			}
384 		}
385 	}
386 	return best;
387 }
388 
389 u_int32_t
390 snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
391 {
392 	return snd_fmtbestfunc(fmt, fmts, 0);
393 }
394 
395 u_int32_t
396 snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts)
397 {
398 	return snd_fmtbestfunc(fmt, fmts, 1);
399 }
400 
401 u_int32_t
402 snd_fmtbest(u_int32_t fmt, u_int32_t *fmts)
403 {
404 	u_int32_t best1, best2;
405 	u_int32_t score, score1, score2;
406 
407 	if (snd_fmtvalid(fmt, fmts))
408 		return fmt;
409 
410 	best1 = snd_fmtbestchannel(fmt, fmts);
411 	best2 = snd_fmtbestbit(fmt, fmts);
412 
413 	if (best1 != 0 && best2 != 0 && best1 != best2) {
414 		/*if (fmt & AFMT_STEREO)*/
415 		if (AFMT_CHANNEL(fmt) > 1)
416 			return best1;
417 		else {
418 			score = score_val(snd_fmtscore(fmt));
419 			score1 = score_val(snd_fmtscore(best1));
420 			score2 = score_val(snd_fmtscore(best2));
421 			if (score1 == score2 || score1 == score)
422 				return best1;
423 			else if (score2 == score)
424 				return best2;
425 			else if (score1 > score2)
426 				return best1;
427 			return best2;
428 		}
429 	} else if (best2 == 0)
430 		return best1;
431 	else
432 		return best2;
433 }
434 
435 void
436 feeder_printchain(struct pcm_feeder *head)
437 {
438 	struct pcm_feeder *f;
439 
440 	printf("feeder chain (head @%p)\n", head);
441 	f = head;
442 	while (f != NULL) {
443 		printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
444 		f = f->source;
445 	}
446 	printf("[end]\n\n");
447 }
448 
449 /*****************************************************************************/
450 
451 static int
452 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
453 {
454 	struct snd_dbuf *src = source;
455 	int l, offset;
456 
457 	KASSERT(count > 0, ("feed_root: count == 0"));
458 
459 	if (++ch->feedcount == 0)
460 		ch->feedcount = 2;
461 
462 	l = min(count, sndbuf_getready(src));
463 
464 	/* When recording only return as much data as available */
465 	if (ch->direction == PCMDIR_REC) {
466 		sndbuf_dispose(src, buffer, l);
467 		return l;
468 	}
469 
470 	offset = count - l;
471 
472 	if (offset > 0) {
473 		if (snd_verbose > 3)
474 			printf("%s: (%s) %spending %d bytes "
475 			    "(count=%d l=%d feed=%d)\n",
476 			    __func__,
477 			    (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
478 			    (ch->feedcount == 1) ? "pre" : "ap",
479 			    offset, count, l, ch->feedcount);
480 
481 		if (ch->feedcount == 1) {
482 			memset(buffer,
483 			    sndbuf_zerodata(sndbuf_getfmt(src)),
484 			    offset);
485 			if (l > 0)
486 				sndbuf_dispose(src, buffer + offset, l);
487 			else
488 				ch->feedcount--;
489 		} else {
490 			if (l > 0)
491 				sndbuf_dispose(src, buffer, l);
492 			memset(buffer + l,
493 			    sndbuf_zerodata(sndbuf_getfmt(src)),
494 			    offset);
495 			if (!(ch->flags & CHN_F_CLOSING))
496 				ch->xruns++;
497 		}
498 	} else if (l > 0)
499 		sndbuf_dispose(src, buffer, l);
500 
501 	return count;
502 }
503 
504 static kobj_method_t feeder_root_methods[] = {
505     	KOBJMETHOD(feeder_feed,		feed_root),
506 	KOBJMETHOD_END
507 };
508 static struct feeder_class feeder_root_class = {
509 	.name =		"feeder_root",
510 	.methods =	feeder_root_methods,
511 	.size =		sizeof(struct pcm_feeder),
512 	.desc =		NULL,
513 	.data =		NULL,
514 };
515 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
516 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
517