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.4 2006/09/05 00:55:43 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.4 2006/09/05 00:55:43 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 = kmalloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
67 printf("can't allocate memory for root feeder\n");
70 fte->feederclass = fc;
73 SLIST_INSERT_HEAD(&feedertab, fte, link);
76 /* we've got our root feeder so don't veto pcm loading anymore */
82 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
84 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
86 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
87 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
88 fte = kmalloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
90 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
94 fte->feederclass = fc;
95 fte->desc = &fc->desc[i];
97 fte->desc->idx = feedercnt;
98 SLIST_INSERT_HEAD(&feedertab, fte, link);
102 if (feedercnt >= MAXFEEDERS)
103 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
107 feeder_unregisterall(void *p)
109 struct feedertab_entry *fte, *next;
111 next = SLIST_FIRST(&feedertab);
112 while (next != NULL) {
114 next = SLIST_NEXT(fte, link);
115 kfree(fte, M_FEEDER);
120 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
122 return ((n->type == m->type) &&
123 ((n->in == 0) || (n->in == m->in)) &&
124 ((n->out == 0) || (n->out == m->out)) &&
125 (n->flags == m->flags));
129 feeder_destroy(struct pcm_feeder *f)
132 kobj_delete((kobj_t)f, M_FEEDER);
135 static struct pcm_feeder *
136 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
138 struct pcm_feeder *f;
141 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
145 f->align = fc->align;
150 f->desc = &(f->desc_static);
155 f->desc->type = FEEDER_ROOT;
162 err = FEEDER_INIT(f);
164 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
173 struct feeder_class *
174 feeder_getclass(struct pcm_feederdesc *desc)
176 struct feedertab_entry *fte;
178 SLIST_FOREACH(fte, &feedertab, link) {
179 if ((desc == NULL) && (fte->desc == NULL))
180 return fte->feederclass;
181 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
182 return fte->feederclass;
188 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
190 struct pcm_feeder *nf;
192 nf = feeder_create(fc, desc);
196 nf->source = c->feeder;
199 c->align += nf->align;
200 else if (nf->align < 0 && c->align < -nf->align)
201 c->align = -nf->align;
209 chn_removefeeder(struct pcm_channel *c)
211 struct pcm_feeder *f;
213 if (c->feeder == NULL)
216 c->feeder = c->feeder->source;
223 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
225 struct pcm_feeder *f;
229 if (f->desc->type == type)
238 chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
240 u_int32_t visited[MAXFEEDERS / 32];
243 bzero(visited, sizeof(visited));
244 while (test && (test != stop)) {
245 idx = test->desc->idx;
247 panic("bad idx %d", idx);
248 if (idx >= MAXFEEDERS)
249 panic("bad idx %d", idx);
250 mask = 1 << (idx & 31);
252 if (visited[idx] & mask)
254 visited[idx] |= mask;
261 static struct pcm_feeder *
262 feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
264 struct feedertab_entry *fte;
265 struct pcm_feeder *try, *ret;
267 /* printf("trying %s (%x -> %x)...\n", source->class->name, source->desc->in, source->desc->out); */
268 if (fmtvalid(source->desc->out, to)) {
269 /* printf("got it\n"); */
276 SLIST_FOREACH(fte, &feedertab, link) {
277 if (fte->desc == NULL)
279 if (fte->desc->type != FEEDER_FMT)
281 if (fte->desc->in == source->desc->out) {
282 try = feeder_create(fte->feederclass, fte->desc);
284 try->source = source;
285 ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
292 /* printf("giving up %s...\n", source->class->name); */
298 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
300 struct pcm_feeder *try, *del, *stop;
301 u_int32_t tmpfrom[2], best, *from;
304 KASSERT(c != NULL, ("c == NULL"));
305 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
306 KASSERT(to != NULL, ("to == NULL"));
307 KASSERT(to[0] != 0, ("to[0] == 0"));
311 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
312 from = chn_getcaps(c)->fmtlist;
314 tmpfrom[0] = c->feeder->desc->out;
322 while (from[i] != 0) {
323 c->feeder->desc->out = from[i];
326 while (try == NULL && max < 8) {
327 try = feeder_fmtchain(to, c->feeder, stop, max);
331 if (try != NULL && max < bestmax) {
335 while (try != NULL && try != stop) {
345 c->feeder->desc->out = best;
346 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
353 printf("\n\nchain: ");
355 while (try && (try != stop)) {
357 printf("%s [%d]", try->class->name, try->desc->idx);
362 try->source->parent = try;
364 c->align += try->align;
365 else if (try->align < 0 && c->align < -try->align)
366 c->align = -try->align;
370 printf("%s [%d]\n", try->class->name, try->desc->idx);
373 return (c->direction == PCMDIR_REC)? best : c->feeder->desc->out;
376 /*****************************************************************************/
379 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
381 struct snd_dbuf *src = source;
385 KASSERT(count > 0, ("feed_root: count == 0"));
386 /* count &= ~((1 << ch->align) - 1); */
387 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
389 l = min(count, sndbuf_getready(src));
390 sndbuf_dispose(src, buffer, l);
392 /* When recording only return as much data as available */
393 if (ch->direction == PCMDIR_REC)
398 printf("appending %d bytes\n", count - l);
401 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
408 static kobj_method_t feeder_root_methods[] = {
409 KOBJMETHOD(feeder_feed, feed_root),
412 static struct feeder_class feeder_root_class = {
414 methods: feeder_root_methods,
415 size: sizeof(struct pcm_feeder),
420 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
421 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);