xref: /freebsd/sys/dev/sound/pcm/feeder.c (revision 47ef2a131091508e049ab10cad7f91a3c1342cd9)
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 static int feedercnt = 0;
52 
53 /*****************************************************************************/
54 
55 static void
56 feeder_register_root(void *p)
57 {
58 	struct feeder_class *fc = p;
59 	struct feedertab_entry *fte;
60 
61 	MPASS(feedercnt == 0);
62 	KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
63 
64 	SLIST_INIT(&feedertab);
65 	fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
66 	fte->feederclass = fc;
67 	fte->desc = NULL;
68 	fte->idx = feedercnt;
69 	SLIST_INSERT_HEAD(&feedertab, fte, link);
70 	feedercnt++;
71 
72 	/* we've got our root feeder so don't veto pcm loading anymore */
73 	pcm_veto_load = 0;
74 }
75 
76 void
77 feeder_register(void *p)
78 {
79 	struct feeder_class *fc = p;
80 	struct feedertab_entry *fte;
81 	int i;
82 
83 	KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
84 
85 	/*
86 	 * beyond this point failure is non-fatal but may result in some
87 	 * translations being unavailable
88 	 */
89 	i = 0;
90 	while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
91 		fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
92 		fte->feederclass = fc;
93 		fte->desc = &fc->desc[i];
94 		fte->idx = feedercnt;
95 		fte->desc->idx = feedercnt;
96 		SLIST_INSERT_HEAD(&feedertab, fte, link);
97 		i++;
98 	}
99 	feedercnt++;
100 	if (feedercnt >= MAXFEEDERS) {
101 		printf("MAXFEEDERS (%d >= %d) exceeded\n",
102 		    feedercnt, MAXFEEDERS);
103 	}
104 }
105 
106 static void
107 feeder_unregisterall(void *p)
108 {
109 	struct feedertab_entry *fte, *next;
110 
111 	next = SLIST_FIRST(&feedertab);
112 	while (next != NULL) {
113 		fte = next;
114 		next = SLIST_NEXT(fte, link);
115 		free(fte, M_FEEDER);
116 	}
117 }
118 
119 static int
120 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
121 {
122 	return ((n->type == m->type) &&
123 		((n->in == 0) || (n->in == m->in)) &&
124 		((n->out == 0) || (n->out == m->out)) &&
125 		(n->flags == m->flags));
126 }
127 
128 static void
129 feeder_destroy(struct pcm_feeder *f)
130 {
131 	FEEDER_FREE(f);
132 	kobj_delete((kobj_t)f, M_FEEDER);
133 }
134 
135 static struct pcm_feeder *
136 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
137 {
138 	struct pcm_feeder *f;
139 	int err;
140 
141 	f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
142 	if (f == NULL)
143 		return NULL;
144 
145 	f->data = fc->data;
146 	f->source = NULL;
147 	f->parent = NULL;
148 	f->class = fc;
149 	f->desc = &(f->desc_static);
150 
151 	if (desc) {
152 		*(f->desc) = *desc;
153 	} else {
154 		f->desc->type = FEEDER_ROOT;
155 		f->desc->in = 0;
156 		f->desc->out = 0;
157 		f->desc->flags = 0;
158 		f->desc->idx = 0;
159 	}
160 
161 	err = FEEDER_INIT(f);
162 	if (err) {
163 		printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
164 		feeder_destroy(f);
165 
166 		return NULL;
167 	}
168 
169 	return f;
170 }
171 
172 struct feeder_class *
173 feeder_getclass(struct pcm_feederdesc *desc)
174 {
175 	struct feedertab_entry *fte;
176 
177 	SLIST_FOREACH(fte, &feedertab, link) {
178 		if ((desc == NULL) && (fte->desc == NULL))
179 			return fte->feederclass;
180 		if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
181 			return fte->feederclass;
182 	}
183 	return NULL;
184 }
185 
186 int
187 feeder_add(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
188 {
189 	struct pcm_feeder *nf;
190 
191 	nf = feeder_create(fc, desc);
192 	if (nf == NULL)
193 		return ENOSPC;
194 
195 	nf->source = c->feeder;
196 
197 	if (c->feeder != NULL)
198 		c->feeder->parent = nf;
199 	c->feeder = nf;
200 
201 	return 0;
202 }
203 
204 void
205 feeder_remove(struct pcm_channel *c)
206 {
207 	struct pcm_feeder *f;
208 
209 	while (c->feeder != NULL) {
210 		f = c->feeder;
211 		c->feeder = c->feeder->source;
212 		feeder_destroy(f);
213 	}
214 }
215 
216 struct pcm_feeder *
217 feeder_find(struct pcm_channel *c, u_int32_t type)
218 {
219 	struct pcm_feeder *f;
220 
221 	f = c->feeder;
222 	while (f != NULL) {
223 		if (f->desc->type == type)
224 			return f;
225 		f = f->source;
226 	}
227 
228 	return NULL;
229 }
230 
231 /*
232  * 14bit format scoring
233  * --------------------
234  *
235  *  13  12  11  10   9   8        2        1   0    offset
236  * +---+---+---+---+---+---+-------------+---+---+
237  * | X | X | X | X | X | X | X X X X X X | X | X |
238  * +---+---+---+---+---+---+-------------+---+---+
239  *   |   |   |   |   |   |        |        |   |
240  *   |   |   |   |   |   |        |        |   +--> signed?
241  *   |   |   |   |   |   |        |        |
242  *   |   |   |   |   |   |        |        +------> bigendian?
243  *   |   |   |   |   |   |        |
244  *   |   |   |   |   |   |        +---------------> total channels
245  *   |   |   |   |   |   |
246  *   |   |   |   |   |   +------------------------> AFMT_A_LAW
247  *   |   |   |   |   |
248  *   |   |   |   |   +----------------------------> AFMT_MU_LAW
249  *   |   |   |   |
250  *   |   |   |   +--------------------------------> AFMT_8BIT
251  *   |   |   |
252  *   |   |   +------------------------------------> AFMT_16BIT
253  *   |   |
254  *   |   +----------------------------------------> AFMT_24BIT
255  *   |
256  *   +--------------------------------------------> AFMT_32BIT
257  */
258 #define score_signeq(s1, s2)	(((s1) & 0x1) == ((s2) & 0x1))
259 #define score_endianeq(s1, s2)	(((s1) & 0x2) == ((s2) & 0x2))
260 #define score_cheq(s1, s2)	(((s1) & 0xfc) == ((s2) & 0xfc))
261 #define score_chgt(s1, s2)	(((s1) & 0xfc) > ((s2) & 0xfc))
262 #define score_chlt(s1, s2)	(((s1) & 0xfc) < ((s2) & 0xfc))
263 #define score_val(s1)		((s1) & 0x3f00)
264 #define score_cse(s1)		((s1) & 0x7f)
265 
266 u_int32_t
267 snd_fmtscore(u_int32_t fmt)
268 {
269 	u_int32_t ret;
270 
271 	ret = 0;
272 	if (fmt & AFMT_SIGNED)
273 		ret |= 1 << 0;
274 	if (fmt & AFMT_BIGENDIAN)
275 		ret |= 1 << 1;
276 	/*if (fmt & AFMT_STEREO)
277 		ret |= (2 & 0x3f) << 2;
278 	else
279 		ret |= (1 & 0x3f) << 2;*/
280 	ret |= (AFMT_CHANNEL(fmt) & 0x3f) << 2;
281 	if (fmt & AFMT_A_LAW)
282 		ret |= 1 << 8;
283 	else if (fmt & AFMT_MU_LAW)
284 		ret |= 1 << 9;
285 	else if (fmt & AFMT_8BIT)
286 		ret |= 1 << 10;
287 	else if (fmt & AFMT_16BIT)
288 		ret |= 1 << 11;
289 	else if (fmt & AFMT_24BIT)
290 		ret |= 1 << 12;
291 	else if (fmt & AFMT_32BIT)
292 		ret |= 1 << 13;
293 
294 	return ret;
295 }
296 
297 static u_int32_t
298 snd_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq)
299 {
300 	u_int32_t best, score, score2, oldscore;
301 	int i;
302 
303 	if (fmt == 0 || fmts == NULL || fmts[0] == 0)
304 		return 0;
305 
306 	if (snd_fmtvalid(fmt, fmts))
307 		return fmt;
308 
309 	best = 0;
310 	score = snd_fmtscore(fmt);
311 	oldscore = 0;
312 	for (i = 0; fmts[i] != 0; i++) {
313 		score2 = snd_fmtscore(fmts[i]);
314 		if (cheq && !score_cheq(score, score2) &&
315 		    (score_chlt(score2, score) ||
316 		    (oldscore != 0 && score_chgt(score2, oldscore))))
317 				continue;
318 		if (oldscore == 0 ||
319 			    (score_val(score2) == score_val(score)) ||
320 			    (score_val(score2) == score_val(oldscore)) ||
321 			    (score_val(score2) > score_val(oldscore) &&
322 			    score_val(score2) < score_val(score)) ||
323 			    (score_val(score2) < score_val(oldscore) &&
324 			    score_val(score2) > score_val(score)) ||
325 			    (score_val(oldscore) < score_val(score) &&
326 			    score_val(score2) > score_val(oldscore))) {
327 			if (score_val(oldscore) != score_val(score2) ||
328 				    score_cse(score) == score_cse(score2) ||
329 				    ((score_cse(oldscore) != score_cse(score) &&
330 				    !score_endianeq(score, oldscore) &&
331 				    (score_endianeq(score, score2) ||
332 				    (!score_signeq(score, oldscore) &&
333 				    score_signeq(score, score2)))))) {
334 				best = fmts[i];
335 				oldscore = score2;
336 			}
337 		}
338 	}
339 	return best;
340 }
341 
342 u_int32_t
343 snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
344 {
345 	return snd_fmtbestfunc(fmt, fmts, 0);
346 }
347 
348 u_int32_t
349 snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts)
350 {
351 	return snd_fmtbestfunc(fmt, fmts, 1);
352 }
353 
354 u_int32_t
355 snd_fmtbest(u_int32_t fmt, u_int32_t *fmts)
356 {
357 	u_int32_t best1, best2;
358 	u_int32_t score, score1, score2;
359 
360 	if (snd_fmtvalid(fmt, fmts))
361 		return fmt;
362 
363 	best1 = snd_fmtbestchannel(fmt, fmts);
364 	best2 = snd_fmtbestbit(fmt, fmts);
365 
366 	if (best1 != 0 && best2 != 0 && best1 != best2) {
367 		/*if (fmt & AFMT_STEREO)*/
368 		if (AFMT_CHANNEL(fmt) > 1)
369 			return best1;
370 		else {
371 			score = score_val(snd_fmtscore(fmt));
372 			score1 = score_val(snd_fmtscore(best1));
373 			score2 = score_val(snd_fmtscore(best2));
374 			if (score1 == score2 || score1 == score)
375 				return best1;
376 			else if (score2 == score)
377 				return best2;
378 			else if (score1 > score2)
379 				return best1;
380 			return best2;
381 		}
382 	} else if (best2 == 0)
383 		return best1;
384 	else
385 		return best2;
386 }
387 
388 void
389 feeder_printchain(struct pcm_feeder *head)
390 {
391 	struct pcm_feeder *f;
392 
393 	printf("feeder chain (head @%p)\n", head);
394 	f = head;
395 	while (f != NULL) {
396 		printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
397 		f = f->source;
398 	}
399 	printf("[end]\n\n");
400 }
401 
402 /*****************************************************************************/
403 
404 static int
405 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
406 {
407 	struct snd_dbuf *src = source;
408 	int l, offset;
409 
410 	KASSERT(count > 0, ("feed_root: count == 0"));
411 
412 	if (++ch->feedcount == 0)
413 		ch->feedcount = 2;
414 
415 	l = min(count, sndbuf_getready(src));
416 
417 	/* When recording only return as much data as available */
418 	if (ch->direction == PCMDIR_REC) {
419 		sndbuf_dispose(src, buffer, l);
420 		return l;
421 	}
422 
423 	offset = count - l;
424 
425 	if (offset > 0) {
426 		if (snd_verbose > 3)
427 			printf("%s: (%s) %spending %d bytes "
428 			    "(count=%d l=%d feed=%d)\n",
429 			    __func__,
430 			    (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
431 			    (ch->feedcount == 1) ? "pre" : "ap",
432 			    offset, count, l, ch->feedcount);
433 
434 		if (ch->feedcount == 1) {
435 			memset(buffer,
436 			    sndbuf_zerodata(sndbuf_getfmt(src)),
437 			    offset);
438 			if (l > 0)
439 				sndbuf_dispose(src, buffer + offset, l);
440 			else
441 				ch->feedcount--;
442 		} else {
443 			if (l > 0)
444 				sndbuf_dispose(src, buffer, l);
445 			memset(buffer + l,
446 			    sndbuf_zerodata(sndbuf_getfmt(src)),
447 			    offset);
448 			if (!(ch->flags & CHN_F_CLOSING))
449 				ch->xruns++;
450 		}
451 	} else if (l > 0)
452 		sndbuf_dispose(src, buffer, l);
453 
454 	return count;
455 }
456 
457 static kobj_method_t feeder_root_methods[] = {
458     	KOBJMETHOD(feeder_feed,		feed_root),
459 	KOBJMETHOD_END
460 };
461 static struct feeder_class feeder_root_class = {
462 	.name =		"feeder_root",
463 	.methods =	feeder_root_methods,
464 	.size =		sizeof(struct pcm_feeder),
465 	.desc =		NULL,
466 	.data =		NULL,
467 };
468 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register_root,
469     &feeder_root_class);
470 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
471