2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #ifdef HAVE_KERNEL_OPTION_HEADERS
34 #include <dev/sound/pcm/sound.h>
36 #include "feeder_if.h"
38 SND_DECLARE_FILE("$FreeBSD$");
40 static MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
42 #define MAXFEEDERS 256
45 struct feedertab_entry {
46 SLIST_ENTRY(feedertab_entry) link;
47 struct feeder_class *feederclass;
48 struct pcm_feederdesc *desc;
52 static SLIST_HEAD(, feedertab_entry) feedertab;
54 /*****************************************************************************/
57 feeder_register(void *p)
59 static int feedercnt = 0;
61 struct feeder_class *fc = p;
62 struct feedertab_entry *fte;
66 KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
68 SLIST_INIT(&feedertab);
69 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
71 printf("can't allocate memory for root feeder: %s\n",
76 fte->feederclass = fc;
79 SLIST_INSERT_HEAD(&feedertab, fte, link);
82 /* initialize global variables */
84 if (snd_verbose < 0 || snd_verbose > 4)
87 /* initialize unit numbering */
89 if (snd_unit < 0 || snd_unit > PCMMAXUNIT)
92 if (snd_maxautovchans < 0 ||
93 snd_maxautovchans > SND_MAXVCHANS)
94 snd_maxautovchans = 0;
96 if (chn_latency < CHN_LATENCY_MIN ||
97 chn_latency > CHN_LATENCY_MAX)
98 chn_latency = CHN_LATENCY_DEFAULT;
100 if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN ||
101 chn_latency_profile > CHN_LATENCY_PROFILE_MAX)
102 chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
104 if (feeder_rate_min < FEEDRATE_MIN ||
105 feeder_rate_max < FEEDRATE_MIN ||
106 feeder_rate_min > FEEDRATE_MAX ||
107 feeder_rate_max > FEEDRATE_MAX ||
108 !(feeder_rate_min < feeder_rate_max)) {
109 feeder_rate_min = FEEDRATE_RATEMIN;
110 feeder_rate_max = FEEDRATE_RATEMAX;
113 if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN ||
114 feeder_rate_round > FEEDRATE_ROUNDHZ_MAX)
115 feeder_rate_round = FEEDRATE_ROUNDHZ;
118 printf("%s: snd_unit=%d snd_maxautovchans=%d "
120 "feeder_rate_min=%d feeder_rate_max=%d "
121 "feeder_rate_round=%d\n",
122 __func__, snd_unit, snd_maxautovchans,
124 feeder_rate_min, feeder_rate_max,
127 /* we've got our root feeder so don't veto pcm loading anymore */
133 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
135 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
137 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
138 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
139 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
141 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
145 fte->feederclass = fc;
146 fte->desc = &fc->desc[i];
147 fte->idx = feedercnt;
148 fte->desc->idx = feedercnt;
149 SLIST_INSERT_HEAD(&feedertab, fte, link);
153 if (feedercnt >= MAXFEEDERS)
154 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
158 feeder_unregisterall(void *p)
160 struct feedertab_entry *fte, *next;
162 next = SLIST_FIRST(&feedertab);
163 while (next != NULL) {
165 next = SLIST_NEXT(fte, link);
171 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
173 return ((n->type == m->type) &&
174 ((n->in == 0) || (n->in == m->in)) &&
175 ((n->out == 0) || (n->out == m->out)) &&
176 (n->flags == m->flags));
180 feeder_destroy(struct pcm_feeder *f)
183 kobj_delete((kobj_t)f, M_FEEDER);
186 static struct pcm_feeder *
187 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
189 struct pcm_feeder *f;
192 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
200 f->desc = &(f->desc_static);
205 f->desc->type = FEEDER_ROOT;
212 err = FEEDER_INIT(f);
214 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
223 struct feeder_class *
224 feeder_getclass(struct pcm_feederdesc *desc)
226 struct feedertab_entry *fte;
228 SLIST_FOREACH(fte, &feedertab, link) {
229 if ((desc == NULL) && (fte->desc == NULL))
230 return fte->feederclass;
231 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
232 return fte->feederclass;
238 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
240 struct pcm_feeder *nf;
242 nf = feeder_create(fc, desc);
246 nf->source = c->feeder;
248 if (c->feeder != NULL)
249 c->feeder->parent = nf;
256 chn_removefeeder(struct pcm_channel *c)
258 struct pcm_feeder *f;
260 if (c->feeder == NULL)
263 c->feeder = c->feeder->source;
270 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
272 struct pcm_feeder *f;
276 if (f->desc->type == type)
285 * 14bit format scoring
286 * --------------------
288 * 13 12 11 10 9 8 2 1 0 offset
289 * +---+---+---+---+---+---+-------------+---+---+
290 * | X | X | X | X | X | X | X X X X X X | X | X |
291 * +---+---+---+---+---+---+-------------+---+---+
293 * | | | | | | | | +--> signed?
295 * | | | | | | | +------> bigendian?
297 * | | | | | | +---------------> total channels
299 * | | | | | +------------------------> AFMT_A_LAW
301 * | | | | +----------------------------> AFMT_MU_LAW
303 * | | | +--------------------------------> AFMT_8BIT
305 * | | +------------------------------------> AFMT_16BIT
307 * | +----------------------------------------> AFMT_24BIT
309 * +--------------------------------------------> AFMT_32BIT
311 #define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1))
312 #define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2))
313 #define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc))
314 #define score_chgt(s1, s2) (((s1) & 0xfc) > ((s2) & 0xfc))
315 #define score_chlt(s1, s2) (((s1) & 0xfc) < ((s2) & 0xfc))
316 #define score_val(s1) ((s1) & 0x3f00)
317 #define score_cse(s1) ((s1) & 0x7f)
320 snd_fmtscore(u_int32_t fmt)
325 if (fmt & AFMT_SIGNED)
327 if (fmt & AFMT_BIGENDIAN)
329 /*if (fmt & AFMT_STEREO)
330 ret |= (2 & 0x3f) << 2;
332 ret |= (1 & 0x3f) << 2;*/
333 ret |= (AFMT_CHANNEL(fmt) & 0x3f) << 2;
334 if (fmt & AFMT_A_LAW)
336 else if (fmt & AFMT_MU_LAW)
338 else if (fmt & AFMT_8BIT)
340 else if (fmt & AFMT_16BIT)
342 else if (fmt & AFMT_24BIT)
344 else if (fmt & AFMT_32BIT)
351 snd_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq)
353 u_int32_t best, score, score2, oldscore;
356 if (fmt == 0 || fmts == NULL || fmts[0] == 0)
359 if (snd_fmtvalid(fmt, fmts))
363 score = snd_fmtscore(fmt);
365 for (i = 0; fmts[i] != 0; i++) {
366 score2 = snd_fmtscore(fmts[i]);
367 if (cheq && !score_cheq(score, score2) &&
368 (score_chlt(score2, score) ||
369 (oldscore != 0 && score_chgt(score2, oldscore))))
372 (score_val(score2) == score_val(score)) ||
373 (score_val(score2) == score_val(oldscore)) ||
374 (score_val(score2) > score_val(oldscore) &&
375 score_val(score2) < score_val(score)) ||
376 (score_val(score2) < score_val(oldscore) &&
377 score_val(score2) > score_val(score)) ||
378 (score_val(oldscore) < score_val(score) &&
379 score_val(score2) > score_val(oldscore))) {
380 if (score_val(oldscore) != score_val(score2) ||
381 score_cse(score) == score_cse(score2) ||
382 ((score_cse(oldscore) != score_cse(score) &&
383 !score_endianeq(score, oldscore) &&
384 (score_endianeq(score, score2) ||
385 (!score_signeq(score, oldscore) &&
386 score_signeq(score, score2)))))) {
396 snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
398 return snd_fmtbestfunc(fmt, fmts, 0);
402 snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts)
404 return snd_fmtbestfunc(fmt, fmts, 1);
408 snd_fmtbest(u_int32_t fmt, u_int32_t *fmts)
410 u_int32_t best1, best2;
411 u_int32_t score, score1, score2;
413 if (snd_fmtvalid(fmt, fmts))
416 best1 = snd_fmtbestchannel(fmt, fmts);
417 best2 = snd_fmtbestbit(fmt, fmts);
419 if (best1 != 0 && best2 != 0 && best1 != best2) {
420 /*if (fmt & AFMT_STEREO)*/
421 if (AFMT_CHANNEL(fmt) > 1)
424 score = score_val(snd_fmtscore(fmt));
425 score1 = score_val(snd_fmtscore(best1));
426 score2 = score_val(snd_fmtscore(best2));
427 if (score1 == score2 || score1 == score)
429 else if (score2 == score)
431 else if (score1 > score2)
435 } else if (best2 == 0)
442 feeder_printchain(struct pcm_feeder *head)
444 struct pcm_feeder *f;
446 printf("feeder chain (head @%p)\n", head);
449 printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
455 /*****************************************************************************/
458 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
460 struct snd_dbuf *src = source;
463 KASSERT(count > 0, ("feed_root: count == 0"));
465 if (++ch->feedcount == 0)
468 l = min(count, sndbuf_getready(src));
470 /* When recording only return as much data as available */
471 if (ch->direction == PCMDIR_REC) {
472 sndbuf_dispose(src, buffer, l);
481 printf("%s: (%s) %spending %d bytes "
482 "(count=%d l=%d feed=%d)\n",
484 (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
485 (ch->feedcount == 1) ? "pre" : "ap",
486 offset, count, l, ch->feedcount);
488 if (ch->feedcount == 1) {
490 sndbuf_zerodata(sndbuf_getfmt(src)),
493 sndbuf_dispose(src, buffer + offset, l);
498 sndbuf_dispose(src, buffer, l);
500 sndbuf_zerodata(sndbuf_getfmt(src)),
502 if (!(ch->flags & CHN_F_CLOSING))
506 sndbuf_dispose(src, buffer, l);
511 static kobj_method_t feeder_root_methods[] = {
512 KOBJMETHOD(feeder_feed, feed_root),
515 static struct feeder_class feeder_root_class = {
516 .name = "feeder_root",
517 .methods = feeder_root_methods,
518 .size = sizeof(struct pcm_feeder),
522 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
523 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);