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
26 * $FreeBSD: src/sys/dev/sound/pcm/feeder.c,v 1.8.2.9 2003/02/08 01:43:07 orion Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/feeder.c,v 1.2 2003/06/17 04:28:31 dillon 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.2 2003/06/17 04:28:31 dillon 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 = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
67 printf("can't allocate memory for root feeder\n", fc->name);
71 fte->feederclass = fc;
74 SLIST_INSERT_HEAD(&feedertab, fte, link);
77 /* we've got our root feeder so don't veto pcm loading anymore */
83 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
85 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
87 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
88 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
89 fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
91 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
95 fte->feederclass = fc;
96 fte->desc = &fc->desc[i];
98 fte->desc->idx = feedercnt;
99 SLIST_INSERT_HEAD(&feedertab, fte, link);
103 if (feedercnt >= MAXFEEDERS)
104 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
108 feeder_unregisterall(void *p)
110 struct feedertab_entry *fte, *next;
112 next = SLIST_FIRST(&feedertab);
113 while (next != NULL) {
115 next = SLIST_NEXT(fte, link);
121 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
123 return ((n->type == m->type) &&
124 ((n->in == 0) || (n->in == m->in)) &&
125 ((n->out == 0) || (n->out == m->out)) &&
126 (n->flags == m->flags));
130 feeder_destroy(struct pcm_feeder *f)
133 kobj_delete((kobj_t)f, M_FEEDER);
136 static struct pcm_feeder *
137 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
139 struct pcm_feeder *f;
142 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
146 f->align = fc->align;
151 f->desc = &(f->desc_static);
156 f->desc->type = FEEDER_ROOT;
163 err = FEEDER_INIT(f);
165 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
174 struct feeder_class *
175 feeder_getclass(struct pcm_feederdesc *desc)
177 struct feedertab_entry *fte;
179 SLIST_FOREACH(fte, &feedertab, link) {
180 if ((desc == NULL) && (fte->desc == NULL))
181 return fte->feederclass;
182 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
183 return fte->feederclass;
189 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
191 struct pcm_feeder *nf;
193 nf = feeder_create(fc, desc);
197 nf->source = c->feeder;
200 c->align += nf->align;
201 else if (nf->align < 0 && c->align < -nf->align)
202 c->align = -nf->align;
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 /* printf("trying %s (%x -> %x)...\n", source->class->name, source->desc->in, source->desc->out); */
269 if (fmtvalid(source->desc->out, to)) {
270 /* 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;
294 /* printf("giving up %s...\n", source->class->name); */
300 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
302 struct pcm_feeder *try, *del, *stop;
303 u_int32_t tmpfrom[2], best, *from;
306 KASSERT(c != NULL, ("c == NULL"));
307 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
308 KASSERT(to != NULL, ("to == NULL"));
309 KASSERT(to[0] != 0, ("to[0] == 0"));
313 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
314 from = chn_getcaps(c)->fmtlist;
316 tmpfrom[0] = c->feeder->desc->out;
324 while (from[i] != 0) {
325 c->feeder->desc->out = from[i];
328 while (try == NULL && max < 8) {
329 try = feeder_fmtchain(to, c->feeder, stop, max);
333 if (try != NULL && max < bestmax) {
337 while (try != NULL && try != stop) {
347 c->feeder->desc->out = best;
348 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
355 printf("\n\nchain: ");
357 while (try && (try != stop)) {
359 printf("%s [%d]", try->class->name, try->desc->idx);
364 try->source->parent = try;
366 c->align += try->align;
367 else if (try->align < 0 && c->align < -try->align)
368 c->align = -try->align;
372 printf("%s [%d]\n", try->class->name, try->desc->idx);
375 return (c->direction == PCMDIR_REC)? best : c->feeder->desc->out;
378 /*****************************************************************************/
381 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
383 struct snd_dbuf *src = source;
387 KASSERT(count > 0, ("feed_root: count == 0"));
388 /* count &= ~((1 << ch->align) - 1); */
389 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
391 l = min(count, sndbuf_getready(src));
392 sndbuf_dispose(src, buffer, l);
394 /* When recording only return as much data as available */
395 if (ch->direction == PCMDIR_REC)
400 printf("appending %d bytes\n", count - l);
403 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
410 static kobj_method_t feeder_root_methods[] = {
411 KOBJMETHOD(feeder_feed, feed_root),
414 static struct feeder_class feeder_root_class = {
416 methods: feeder_root_methods,
417 size: sizeof(struct pcm_feeder),
422 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
423 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);