2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
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: src/sys/dev/sound/pcm/feeder.c,v 1.8.2.9 2003/02/08 01:43:07 orion Exp $");
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_WAITOK | M_ZERO);
64 printf("can't allocate memory for root feeder\n", fc->name);
68 fte->feederclass = fc;
71 SLIST_INSERT_HEAD(&feedertab, fte, link);
74 /* we've got our root feeder so don't veto pcm loading anymore */
80 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
82 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
84 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
85 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
86 fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
88 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
92 fte->feederclass = fc;
93 fte->desc = &fc->desc[i];
95 fte->desc->idx = feedercnt;
96 SLIST_INSERT_HEAD(&feedertab, fte, link);
100 if (feedercnt >= MAXFEEDERS)
101 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
105 feeder_unregisterall(void *p)
107 struct feedertab_entry *fte, *next;
109 next = SLIST_FIRST(&feedertab);
110 while (next != NULL) {
112 next = SLIST_NEXT(fte, link);
118 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
120 return ((n->type == m->type) &&
121 ((n->in == 0) || (n->in == m->in)) &&
122 ((n->out == 0) || (n->out == m->out)) &&
123 (n->flags == m->flags));
127 feeder_destroy(struct pcm_feeder *f)
130 kobj_delete((kobj_t)f, M_FEEDER);
133 static struct pcm_feeder *
134 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
136 struct pcm_feeder *f;
139 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
143 f->align = fc->align;
148 f->desc = &(f->desc_static);
153 f->desc->type = FEEDER_ROOT;
160 err = FEEDER_INIT(f);
162 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
171 struct feeder_class *
172 feeder_getclass(struct pcm_feederdesc *desc)
174 struct feedertab_entry *fte;
176 SLIST_FOREACH(fte, &feedertab, link) {
177 if ((desc == NULL) && (fte->desc == NULL))
178 return fte->feederclass;
179 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
180 return fte->feederclass;
186 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
188 struct pcm_feeder *nf;
190 nf = feeder_create(fc, desc);
194 nf->source = c->feeder;
197 c->align += nf->align;
198 else if (nf->align < 0 && c->align < -nf->align)
199 c->align = -nf->align;
207 chn_removefeeder(struct pcm_channel *c)
209 struct pcm_feeder *f;
211 if (c->feeder == NULL)
214 c->feeder = c->feeder->source;
221 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
223 struct pcm_feeder *f;
227 if (f->desc->type == type)
236 chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
238 u_int32_t visited[MAXFEEDERS / 32];
241 bzero(visited, sizeof(visited));
242 while (test && (test != stop)) {
243 idx = test->desc->idx;
245 panic("bad idx %d", idx);
246 if (idx >= MAXFEEDERS)
247 panic("bad idx %d", idx);
248 mask = 1 << (idx & 31);
250 if (visited[idx] & mask)
252 visited[idx] |= mask;
259 static struct pcm_feeder *
260 feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
262 struct feedertab_entry *fte;
263 struct pcm_feeder *try, *ret;
265 /* printf("trying %s (%x -> %x)...\n", source->class->name, source->desc->in, source->desc->out); */
266 if (fmtvalid(source->desc->out, to)) {
267 /* printf("got it\n"); */
274 SLIST_FOREACH(fte, &feedertab, link) {
275 if (fte->desc == NULL)
277 if (fte->desc->type != FEEDER_FMT)
279 if (fte->desc->in == source->desc->out) {
280 try = feeder_create(fte->feederclass, fte->desc);
282 try->source = source;
283 ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
291 /* printf("giving up %s...\n", source->class->name); */
297 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
299 struct pcm_feeder *try, *del, *stop;
300 u_int32_t tmpfrom[2], best, *from;
303 KASSERT(c != NULL, ("c == NULL"));
304 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
305 KASSERT(to != NULL, ("to == NULL"));
306 KASSERT(to[0] != 0, ("to[0] == 0"));
310 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
311 from = chn_getcaps(c)->fmtlist;
313 tmpfrom[0] = c->feeder->desc->out;
321 while (from[i] != 0) {
322 c->feeder->desc->out = from[i];
325 while (try == NULL && max < 8) {
326 try = feeder_fmtchain(to, c->feeder, stop, max);
330 if (try != NULL && max < bestmax) {
334 while (try != NULL && try != stop) {
344 c->feeder->desc->out = best;
345 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
352 printf("\n\nchain: ");
354 while (try && (try != stop)) {
356 printf("%s [%d]", try->class->name, try->desc->idx);
361 try->source->parent = try;
363 c->align += try->align;
364 else if (try->align < 0 && c->align < -try->align)
365 c->align = -try->align;
369 printf("%s [%d]\n", try->class->name, try->desc->idx);
372 return (c->direction == PCMDIR_REC)? best : c->feeder->desc->out;
375 /*****************************************************************************/
378 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
380 struct snd_dbuf *src = source;
384 KASSERT(count > 0, ("feed_root: count == 0"));
385 /* count &= ~((1 << ch->align) - 1); */
386 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
388 l = min(count, sndbuf_getready(src));
389 sndbuf_dispose(src, buffer, l);
391 /* When recording only return as much data as available */
392 if (ch->direction == PCMDIR_REC)
397 printf("appending %d bytes\n", count - l);
400 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
407 static kobj_method_t feeder_root_methods[] = {
408 KOBJMETHOD(feeder_feed, feed_root),
411 static struct feeder_class feeder_root_class = {
413 methods: feeder_root_methods,
414 size: sizeof(struct pcm_feeder),
419 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
420 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);