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
26 * $FreeBSD: src/sys/dev/sound/pcm/feeder.c,v 1.33.2.3 2006/03/07 15:51:19 jhb Exp $
29 #include <dev/sound/pcm/sound.h>
31 #include "feeder_if.h"
33 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/feeder.c,v 1.6 2007/01/04 21:47:03 corecode Exp $");
35 MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
37 #define MAXFEEDERS 256
40 struct feedertab_entry {
41 SLIST_ENTRY(feedertab_entry) link;
42 struct feeder_class *feederclass;
43 struct pcm_feederdesc *desc;
47 static SLIST_HEAD(, feedertab_entry) feedertab;
49 /*****************************************************************************/
52 feeder_register(void *p)
54 static int feedercnt = 0;
56 struct feeder_class *fc = p;
57 struct feedertab_entry *fte;
61 KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
63 SLIST_INIT(&feedertab);
64 fte = kmalloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
65 fte->feederclass = fc;
68 SLIST_INSERT_HEAD(&feedertab, fte, link);
71 /* we've got our root feeder so don't veto pcm loading anymore */
77 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
79 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
81 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
82 /* kprintf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
83 fte = kmalloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
84 fte->feederclass = fc;
85 fte->desc = &fc->desc[i];
87 fte->desc->idx = feedercnt;
88 SLIST_INSERT_HEAD(&feedertab, fte, link);
92 if (feedercnt >= MAXFEEDERS)
93 kprintf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
97 feeder_unregisterall(void *p)
99 struct feedertab_entry *fte, *next;
101 next = SLIST_FIRST(&feedertab);
102 while (next != NULL) {
104 next = SLIST_NEXT(fte, link);
105 kfree(fte, M_FEEDER);
110 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
112 return ((n->type == m->type) &&
113 ((n->in == 0) || (n->in == m->in)) &&
114 ((n->out == 0) || (n->out == m->out)) &&
115 (n->flags == m->flags));
119 feeder_destroy(struct pcm_feeder *f)
122 kobj_delete((kobj_t)f, M_FEEDER);
125 static struct pcm_feeder *
126 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
128 struct pcm_feeder *f;
131 f = (void *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
135 f->align = fc->align;
140 f->desc = &(f->desc_static);
145 f->desc->type = FEEDER_ROOT;
152 err = FEEDER_INIT(f);
154 kprintf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
163 struct feeder_class *
164 feeder_getclass(struct pcm_feederdesc *desc)
166 struct feedertab_entry *fte;
168 SLIST_FOREACH(fte, &feedertab, link) {
169 if ((desc == NULL) && (fte->desc == NULL))
170 return fte->feederclass;
171 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
172 return fte->feederclass;
178 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
180 struct pcm_feeder *nf;
182 nf = feeder_create(fc, desc);
186 nf->source = c->feeder;
188 /* XXX we should use the lowest common denominator for align */
190 c->align += nf->align;
191 else if (nf->align < 0 && c->align < -nf->align)
192 c->align = -nf->align;
193 if (c->feeder != NULL)
194 c->feeder->parent = nf;
201 chn_removefeeder(struct pcm_channel *c)
203 struct pcm_feeder *f;
205 if (c->feeder == NULL)
208 c->feeder = c->feeder->source;
215 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
217 struct pcm_feeder *f;
221 if (f->desc->type == type)
230 chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
232 u_int32_t visited[MAXFEEDERS / 32];
235 bzero(visited, sizeof(visited));
236 while (test && (test != stop)) {
237 idx = test->desc->idx;
239 panic("bad idx %d", idx);
240 if (idx >= MAXFEEDERS)
241 panic("bad idx %d", idx);
242 mask = 1 << (idx & 31);
244 if (visited[idx] & mask)
246 visited[idx] |= mask;
253 static struct pcm_feeder *
254 feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
256 struct feedertab_entry *fte;
257 struct pcm_feeder *try, *ret;
259 DEB(kprintf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out));
260 if (fmtvalid(source->desc->out, to)) {
261 DEB(kprintf("got it\n"));
268 SLIST_FOREACH(fte, &feedertab, link) {
269 if (fte->desc == NULL)
271 if (fte->desc->type != FEEDER_FMT)
273 if (fte->desc->in == source->desc->out) {
274 try = feeder_create(fte->feederclass, fte->desc);
276 try->source = source;
277 ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
284 /* kprintf("giving up %s...\n", source->class->name); */
290 chn_fmtscore(u_int32_t fmt)
292 if (fmt & AFMT_32BIT)
294 if (fmt & AFMT_24BIT)
296 if (fmt & AFMT_16BIT)
298 if (fmt & (AFMT_U8|AFMT_S8))
300 if (fmt & AFMT_MU_LAW)
302 if (fmt & AFMT_A_LAW)
308 chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
311 int i, score, score2, oldscore;
314 score = chn_fmtscore(fmt);
316 for (i = 0; fmts[i] != 0; i++) {
317 score2 = chn_fmtscore(fmts[i]);
318 if (oldscore == 0 || (score2 == score) ||
319 (score2 > oldscore && score2 < score) ||
320 (score2 < oldscore && score2 > score) ||
321 (oldscore < score && score2 > oldscore)) {
330 chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts)
333 int i, score, score2, oldscore;
336 score = chn_fmtscore(fmt);
338 for (i = 0; fmts[i] != 0; i++) {
339 if ((fmt & AFMT_STEREO) == (fmts[i] & AFMT_STEREO)) {
340 score2 = chn_fmtscore(fmts[i]);
341 if (oldscore == 0 || (score2 == score) ||
342 (score2 > oldscore && score2 < score) ||
343 (score2 < oldscore && score2 > score) ||
344 (oldscore < score && score2 > oldscore)) {
354 chn_fmtbest(u_int32_t fmt, u_int32_t *fmts)
356 u_int32_t best1, best2;
357 int score, score1, score2;
359 best1 = chn_fmtbeststereo(fmt, fmts);
360 best2 = chn_fmtbestbit(fmt, fmts);
362 if (best1 != 0 && best2 != 0) {
363 if (fmt & AFMT_STEREO)
366 score = chn_fmtscore(fmt);
367 score1 = chn_fmtscore(best1);
368 score2 = chn_fmtscore(best2);
369 if (score1 == score2 || score1 == score)
371 else if (score2 == score)
373 else if (score1 > score2)
377 } else if (best2 == 0)
384 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
386 struct pcm_feeder *try, *del, *stop;
387 u_int32_t tmpfrom[2], tmpto[2], best, *from;
390 KASSERT(c != NULL, ("c == NULL"));
391 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
392 KASSERT(to != NULL, ("to == NULL"));
393 KASSERT(to[0] != 0, ("to[0] == 0"));
397 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
398 from = chn_getcaps(c)->fmtlist;
399 if (fmtvalid(to[0], from))
402 best = chn_fmtbest(to[0], from);
410 tmpfrom[0] = c->feeder->desc->out;
414 if (fmtvalid(tmpfrom[0], to)) {
415 tmpto[0] = tmpfrom[0];
419 best = chn_fmtbest(tmpfrom[0], to);
432 while (from[i] != 0) {
433 c->feeder->desc->out = from[i];
436 while (try == NULL && max < 8) {
437 try = feeder_fmtchain(to, c->feeder, stop, max);
441 if (try != NULL && max < bestmax) {
445 while (try != NULL && try != stop) {
455 c->feeder->desc->out = best;
456 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
463 kprintf("\n\nchain: ");
465 while (try && (try != stop)) {
467 kprintf("%s [%d]", try->class->name, try->desc->idx);
472 try->source->parent = try;
474 c->align += try->align;
475 else if (try->align < 0 && c->align < -try->align)
476 c->align = -try->align;
480 kprintf("%s [%d]\n", try->class->name, try->desc->idx);
483 if (c->direction == PCMDIR_REC) {
485 while (try != NULL) {
486 if (try->desc->type == FEEDER_ROOT)
487 return try->desc->out;
492 return c->feeder->desc->out;
496 feeder_printchain(struct pcm_feeder *head)
498 struct pcm_feeder *f;
500 kprintf("feeder chain (head @%p)\n", head);
503 kprintf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
506 kprintf("[end]\n\n");
509 /*****************************************************************************/
512 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
514 struct snd_dbuf *src = source;
518 KASSERT(count > 0, ("feed_root: count == 0"));
519 /* count &= ~((1 << ch->align) - 1); */
520 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
522 l = min(count, sndbuf_getready(src));
523 sndbuf_dispose(src, buffer, l);
525 /* When recording only return as much data as available */
526 if (ch->direction == PCMDIR_REC)
531 kprintf("appending %d bytes\n", count - l);
534 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
541 static kobj_method_t feeder_root_methods[] = {
542 KOBJMETHOD(feeder_feed, feed_root),
545 static struct feeder_class feeder_root_class = {
546 .name = "feeder_root",
547 .methods = feeder_root_methods,
548 .size = sizeof(struct pcm_feeder),
553 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
554 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);