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 $
27 * $DragonFly: src/sys/dev/sound/pcm/feeder.c,v 1.6 2007/01/04 21:47:03 corecode Exp $
30 #include <dev/sound/pcm/sound.h>
32 #include "feeder_if.h"
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/feeder.c,v 1.6 2007/01/04 21:47:03 corecode Exp $");
36 MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
38 #define MAXFEEDERS 256
41 struct feedertab_entry {
42 SLIST_ENTRY(feedertab_entry) link;
43 struct feeder_class *feederclass;
44 struct pcm_feederdesc *desc;
48 static SLIST_HEAD(, feedertab_entry) feedertab;
50 /*****************************************************************************/
53 feeder_register(void *p)
55 static int feedercnt = 0;
57 struct feeder_class *fc = p;
58 struct feedertab_entry *fte;
62 KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
64 SLIST_INIT(&feedertab);
65 fte = kmalloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
67 kprintf("can't allocate memory for root feeder: %s\n",
72 fte->feederclass = fc;
75 SLIST_INSERT_HEAD(&feedertab, fte, link);
78 /* we've got our root feeder so don't veto pcm loading anymore */
84 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
86 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
88 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
89 /* kprintf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
90 fte = kmalloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO);
92 kprintf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
96 fte->feederclass = fc;
97 fte->desc = &fc->desc[i];
99 fte->desc->idx = feedercnt;
100 SLIST_INSERT_HEAD(&feedertab, fte, link);
104 if (feedercnt >= MAXFEEDERS)
105 kprintf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
109 feeder_unregisterall(void *p)
111 struct feedertab_entry *fte, *next;
113 next = SLIST_FIRST(&feedertab);
114 while (next != NULL) {
116 next = SLIST_NEXT(fte, link);
117 kfree(fte, M_FEEDER);
122 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
124 return ((n->type == m->type) &&
125 ((n->in == 0) || (n->in == m->in)) &&
126 ((n->out == 0) || (n->out == m->out)) &&
127 (n->flags == m->flags));
131 feeder_destroy(struct pcm_feeder *f)
134 kobj_delete((kobj_t)f, M_FEEDER);
137 static struct pcm_feeder *
138 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
140 struct pcm_feeder *f;
143 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
147 f->align = fc->align;
152 f->desc = &(f->desc_static);
157 f->desc->type = FEEDER_ROOT;
164 err = FEEDER_INIT(f);
166 kprintf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
175 struct feeder_class *
176 feeder_getclass(struct pcm_feederdesc *desc)
178 struct feedertab_entry *fte;
180 SLIST_FOREACH(fte, &feedertab, link) {
181 if ((desc == NULL) && (fte->desc == NULL))
182 return fte->feederclass;
183 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
184 return fte->feederclass;
190 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
192 struct pcm_feeder *nf;
194 nf = feeder_create(fc, desc);
198 nf->source = c->feeder;
200 /* XXX we should use the lowest common denominator for align */
202 c->align += nf->align;
203 else if (nf->align < 0 && c->align < -nf->align)
204 c->align = -nf->align;
205 if (c->feeder != NULL)
206 c->feeder->parent = nf;
213 chn_removefeeder(struct pcm_channel *c)
215 struct pcm_feeder *f;
217 if (c->feeder == NULL)
220 c->feeder = c->feeder->source;
227 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
229 struct pcm_feeder *f;
233 if (f->desc->type == type)
242 chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
244 u_int32_t visited[MAXFEEDERS / 32];
247 bzero(visited, sizeof(visited));
248 while (test && (test != stop)) {
249 idx = test->desc->idx;
251 panic("bad idx %d", idx);
252 if (idx >= MAXFEEDERS)
253 panic("bad idx %d", idx);
254 mask = 1 << (idx & 31);
256 if (visited[idx] & mask)
258 visited[idx] |= mask;
265 static struct pcm_feeder *
266 feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
268 struct feedertab_entry *fte;
269 struct pcm_feeder *try, *ret;
271 DEB(kprintf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out));
272 if (fmtvalid(source->desc->out, to)) {
273 DEB(kprintf("got it\n"));
280 SLIST_FOREACH(fte, &feedertab, link) {
281 if (fte->desc == NULL)
283 if (fte->desc->type != FEEDER_FMT)
285 if (fte->desc->in == source->desc->out) {
286 try = feeder_create(fte->feederclass, fte->desc);
288 try->source = source;
289 ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
296 /* kprintf("giving up %s...\n", source->class->name); */
302 chn_fmtscore(u_int32_t fmt)
304 if (fmt & AFMT_32BIT)
306 if (fmt & AFMT_24BIT)
308 if (fmt & AFMT_16BIT)
310 if (fmt & (AFMT_U8|AFMT_S8))
312 if (fmt & AFMT_MU_LAW)
314 if (fmt & AFMT_A_LAW)
320 chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
323 int i, score, score2, oldscore;
326 score = chn_fmtscore(fmt);
328 for (i = 0; fmts[i] != 0; i++) {
329 score2 = chn_fmtscore(fmts[i]);
330 if (oldscore == 0 || (score2 == score) ||
331 (score2 > oldscore && score2 < score) ||
332 (score2 < oldscore && score2 > score) ||
333 (oldscore < score && score2 > oldscore)) {
342 chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts)
345 int i, score, score2, oldscore;
348 score = chn_fmtscore(fmt);
350 for (i = 0; fmts[i] != 0; i++) {
351 if ((fmt & AFMT_STEREO) == (fmts[i] & AFMT_STEREO)) {
352 score2 = chn_fmtscore(fmts[i]);
353 if (oldscore == 0 || (score2 == score) ||
354 (score2 > oldscore && score2 < score) ||
355 (score2 < oldscore && score2 > score) ||
356 (oldscore < score && score2 > oldscore)) {
366 chn_fmtbest(u_int32_t fmt, u_int32_t *fmts)
368 u_int32_t best1, best2;
369 int score, score1, score2;
371 best1 = chn_fmtbeststereo(fmt, fmts);
372 best2 = chn_fmtbestbit(fmt, fmts);
374 if (best1 != 0 && best2 != 0) {
375 if (fmt & AFMT_STEREO)
378 score = chn_fmtscore(fmt);
379 score1 = chn_fmtscore(best1);
380 score2 = chn_fmtscore(best2);
381 if (score1 == score2 || score1 == score)
383 else if (score2 == score)
385 else if (score1 > score2)
389 } else if (best2 == 0)
396 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
398 struct pcm_feeder *try, *del, *stop;
399 u_int32_t tmpfrom[2], tmpto[2], best, *from;
402 KASSERT(c != NULL, ("c == NULL"));
403 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
404 KASSERT(to != NULL, ("to == NULL"));
405 KASSERT(to[0] != 0, ("to[0] == 0"));
409 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
410 from = chn_getcaps(c)->fmtlist;
411 if (fmtvalid(to[0], from))
414 best = chn_fmtbest(to[0], from);
422 tmpfrom[0] = c->feeder->desc->out;
426 if (fmtvalid(tmpfrom[0], to)) {
427 tmpto[0] = tmpfrom[0];
431 best = chn_fmtbest(tmpfrom[0], to);
444 while (from[i] != 0) {
445 c->feeder->desc->out = from[i];
448 while (try == NULL && max < 8) {
449 try = feeder_fmtchain(to, c->feeder, stop, max);
453 if (try != NULL && max < bestmax) {
457 while (try != NULL && try != stop) {
467 c->feeder->desc->out = best;
468 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
475 kprintf("\n\nchain: ");
477 while (try && (try != stop)) {
479 kprintf("%s [%d]", try->class->name, try->desc->idx);
484 try->source->parent = try;
486 c->align += try->align;
487 else if (try->align < 0 && c->align < -try->align)
488 c->align = -try->align;
492 kprintf("%s [%d]\n", try->class->name, try->desc->idx);
495 if (c->direction == PCMDIR_REC) {
497 while (try != NULL) {
498 if (try->desc->type == FEEDER_ROOT)
499 return try->desc->out;
504 return c->feeder->desc->out;
508 feeder_printchain(struct pcm_feeder *head)
510 struct pcm_feeder *f;
512 kprintf("feeder chain (head @%p)\n", head);
515 kprintf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
518 kprintf("[end]\n\n");
521 /*****************************************************************************/
524 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
526 struct snd_dbuf *src = source;
530 KASSERT(count > 0, ("feed_root: count == 0"));
531 /* count &= ~((1 << ch->align) - 1); */
532 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
534 l = min(count, sndbuf_getready(src));
535 sndbuf_dispose(src, buffer, l);
537 /* When recording only return as much data as available */
538 if (ch->direction == PCMDIR_REC)
543 kprintf("appending %d bytes\n", count - l);
546 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
553 static kobj_method_t feeder_root_methods[] = {
554 KOBJMETHOD(feeder_feed, feed_root),
557 static struct feeder_class feeder_root_class = {
558 .name = "feeder_root",
559 .methods = feeder_root_methods,
560 .size = sizeof(struct pcm_feeder),
565 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
566 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);