2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <dev/sound/pcm/sound.h>
29 #include "feeder_if.h"
31 SND_DECLARE_FILE("$FreeBSD$");
33 MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
35 #define MAXFEEDERS 256
38 struct feedertab_entry {
39 SLIST_ENTRY(feedertab_entry) link;
40 struct feeder_class *feederclass;
41 struct pcm_feederdesc *desc;
45 static SLIST_HEAD(, feedertab_entry) feedertab;
47 /*****************************************************************************/
50 feeder_register(void *p)
52 static int feedercnt = 0;
54 struct feeder_class *fc = p;
55 struct feedertab_entry *fte;
59 KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
61 SLIST_INIT(&feedertab);
62 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
64 printf("can't allocate memory for root feeder: %s\n",
69 fte->feederclass = fc;
72 SLIST_INSERT_HEAD(&feedertab, fte, link);
75 /* we've got our root feeder so don't veto pcm loading anymore */
81 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
83 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
85 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
86 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
87 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
89 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
93 fte->feederclass = fc;
94 fte->desc = &fc->desc[i];
96 fte->desc->idx = feedercnt;
97 SLIST_INSERT_HEAD(&feedertab, fte, link);
101 if (feedercnt >= MAXFEEDERS)
102 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
106 feeder_unregisterall(void *p)
108 struct feedertab_entry *fte, *next;
110 next = SLIST_FIRST(&feedertab);
111 while (next != NULL) {
113 next = SLIST_NEXT(fte, link);
119 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
121 return ((n->type == m->type) &&
122 ((n->in == 0) || (n->in == m->in)) &&
123 ((n->out == 0) || (n->out == m->out)) &&
124 (n->flags == m->flags));
128 feeder_destroy(struct pcm_feeder *f)
131 kobj_delete((kobj_t)f, M_FEEDER);
134 static struct pcm_feeder *
135 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
137 struct pcm_feeder *f;
140 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
144 f->align = fc->align;
149 f->desc = &(f->desc_static);
154 f->desc->type = FEEDER_ROOT;
161 err = FEEDER_INIT(f);
163 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
172 struct feeder_class *
173 feeder_getclass(struct pcm_feederdesc *desc)
175 struct feedertab_entry *fte;
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;
187 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
189 struct pcm_feeder *nf;
191 nf = feeder_create(fc, desc);
195 nf->source = c->feeder;
197 /* XXX we should use the lowest common denominator for align */
199 c->align += nf->align;
200 else if (nf->align < 0 && c->align < -nf->align)
201 c->align = -nf->align;
202 if (c->feeder != NULL)
203 c->feeder->parent = nf;
210 chn_removefeeder(struct pcm_channel *c)
212 struct pcm_feeder *f;
214 if (c->feeder == NULL)
217 c->feeder = c->feeder->source;
224 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
226 struct pcm_feeder *f;
230 if (f->desc->type == type)
239 chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
241 u_int32_t visited[MAXFEEDERS / 32];
244 bzero(visited, sizeof(visited));
245 while (test && (test != stop)) {
246 idx = test->desc->idx;
248 panic("bad idx %d", idx);
249 if (idx >= MAXFEEDERS)
250 panic("bad idx %d", idx);
251 mask = 1 << (idx & 31);
253 if (visited[idx] & mask)
255 visited[idx] |= mask;
262 static struct pcm_feeder *
263 feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
265 struct feedertab_entry *fte;
266 struct pcm_feeder *try, *ret;
268 DEB(printf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out));
269 if (fmtvalid(source->desc->out, to)) {
270 DEB(printf("got it\n"));
277 SLIST_FOREACH(fte, &feedertab, link) {
278 if (fte->desc == NULL)
280 if (fte->desc->type != FEEDER_FMT)
282 if (fte->desc->in == source->desc->out) {
283 try = feeder_create(fte->feederclass, fte->desc);
285 try->source = source;
286 ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
293 /* printf("giving up %s...\n", source->class->name); */
299 chn_fmtscore(u_int32_t fmt)
301 if (fmt & AFMT_32BIT)
303 if (fmt & AFMT_24BIT)
305 if (fmt & AFMT_16BIT)
307 if (fmt & (AFMT_U8|AFMT_S8))
309 if (fmt & AFMT_MU_LAW)
311 if (fmt & AFMT_A_LAW)
317 chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
320 int i, score, score2, oldscore;
323 score = chn_fmtscore(fmt);
325 for (i = 0; fmts[i] != 0; i++) {
326 score2 = chn_fmtscore(fmts[i]);
327 if (oldscore == 0 || (score2 == score) ||
328 (score2 > oldscore && score2 < score) ||
329 (score2 < oldscore && score2 > score) ||
330 (oldscore < score && score2 > oldscore)) {
339 chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts)
342 int i, score, score2, oldscore;
345 score = chn_fmtscore(fmt);
347 for (i = 0; fmts[i] != 0; i++) {
348 if ((fmt & AFMT_STEREO) == (fmts[i] & AFMT_STEREO)) {
349 score2 = chn_fmtscore(fmts[i]);
350 if (oldscore == 0 || (score2 == score) ||
351 (score2 > oldscore && score2 < score) ||
352 (score2 < oldscore && score2 > score) ||
353 (oldscore < score && score2 > oldscore)) {
363 chn_fmtbest(u_int32_t fmt, u_int32_t *fmts)
365 u_int32_t best1, best2;
366 int score, score1, score2;
368 best1 = chn_fmtbeststereo(fmt, fmts);
369 best2 = chn_fmtbestbit(fmt, fmts);
371 if (best1 != 0 && best2 != 0) {
372 if (fmt & AFMT_STEREO)
375 score = chn_fmtscore(fmt);
376 score1 = chn_fmtscore(best1);
377 score2 = chn_fmtscore(best2);
378 if (score1 == score2 || score1 == score)
380 else if (score2 == score)
382 else if (score1 > score2)
386 } else if (best2 == 0)
393 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
395 struct pcm_feeder *try, *del, *stop;
396 u_int32_t tmpfrom[2], tmpto[2], best, *from;
399 KASSERT(c != NULL, ("c == NULL"));
400 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
401 KASSERT(to != NULL, ("to == NULL"));
402 KASSERT(to[0] != 0, ("to[0] == 0"));
406 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
407 from = chn_getcaps(c)->fmtlist;
408 if (fmtvalid(to[0], from))
411 best = chn_fmtbest(to[0], from);
419 tmpfrom[0] = c->feeder->desc->out;
423 if (fmtvalid(tmpfrom[0], to)) {
424 tmpto[0] = tmpfrom[0];
428 best = chn_fmtbest(tmpfrom[0], to);
441 while (from[i] != 0) {
442 c->feeder->desc->out = from[i];
445 while (try == NULL && max < 8) {
446 try = feeder_fmtchain(to, c->feeder, stop, max);
450 if (try != NULL && max < bestmax) {
454 while (try != NULL && try != stop) {
464 c->feeder->desc->out = best;
465 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
472 printf("\n\nchain: ");
474 while (try && (try != stop)) {
476 printf("%s [%d]", try->class->name, try->desc->idx);
481 try->source->parent = try;
483 c->align += try->align;
484 else if (try->align < 0 && c->align < -try->align)
485 c->align = -try->align;
489 printf("%s [%d]\n", try->class->name, try->desc->idx);
492 if (c->direction == PCMDIR_REC) {
494 while (try != NULL) {
495 if (try->desc->type == FEEDER_ROOT)
496 return try->desc->out;
501 return c->feeder->desc->out;
505 feeder_printchain(struct pcm_feeder *head)
507 struct pcm_feeder *f;
509 printf("feeder chain (head @%p)\n", head);
512 printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
518 /*****************************************************************************/
521 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
523 struct snd_dbuf *src = source;
527 KASSERT(count > 0, ("feed_root: count == 0"));
528 /* count &= ~((1 << ch->align) - 1); */
529 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
531 l = min(count, sndbuf_getready(src));
532 sndbuf_dispose(src, buffer, l);
534 /* When recording only return as much data as available */
535 if (ch->direction == PCMDIR_REC)
540 printf("appending %d bytes\n", count - l);
543 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
550 static kobj_method_t feeder_root_methods[] = {
551 KOBJMETHOD(feeder_feed, feed_root),
554 static struct feeder_class feeder_root_class = {
555 .name = "feeder_root",
556 .methods = feeder_root_methods,
557 .size = sizeof(struct pcm_feeder),
562 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
563 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);