feeder.c revision 75319
1/*
2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/sound/pcm/feeder.c 75319 2001-04-08 20:20:52Z cg $
27 */
28
29#include <dev/sound/pcm/sound.h>
30
31#include "feeder_if.h"
32
33MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
34
35#define MAXFEEDERS 	256
36#undef FEEDER_DEBUG
37
38struct feedertab_entry {
39	SLIST_ENTRY(feedertab_entry) link;
40	struct feeder_class *feederclass;
41	struct pcm_feederdesc *desc;
42
43	int idx;
44};
45static SLIST_HEAD(, feedertab_entry) feedertab;
46
47/*****************************************************************************/
48
49void
50feeder_register(void *p)
51{
52	struct feeder_class *fc = p;
53	struct feedertab_entry *fte;
54	static int feedercnt = 0;
55	int i;
56
57	if (feedercnt == 0) {
58		if (fc->desc)
59			panic("FIRST FEEDER NOT ROOT: %s\n", fc->name);
60		SLIST_INIT(&feedertab);
61		fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
62		fte->feederclass = fc;
63		fte->desc = NULL;
64		fte->idx = feedercnt;
65		SLIST_INSERT_HEAD(&feedertab, fte, link);
66		feedercnt++;
67		return;
68	}
69
70	i = 0;
71	while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
72		printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
73		fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
74		fte->feederclass = fc;
75		fte->desc = &fc->desc[i];
76		fte->idx = feedercnt;
77		fte->desc->idx = feedercnt;
78		SLIST_INSERT_HEAD(&feedertab, fte, link);
79		i++;
80	}
81	feedercnt++;
82	if (feedercnt >= MAXFEEDERS)
83		printf("MAXFEEDERS exceeded\n");
84}
85
86static void
87feeder_unregisterall(void *p)
88{
89	struct feedertab_entry *fte, *next;
90
91	next = SLIST_FIRST(&feedertab);
92	while (next != NULL) {
93		fte = next;
94		next = SLIST_NEXT(fte, link);
95		free(fte, M_FEEDER);
96	}
97}
98
99static int
100cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
101{
102	return ((n->type == m->type) &&
103		((n->in == 0) || (n->in == m->in)) &&
104		((n->out == 0) || (n->out == m->out)) &&
105		(n->flags == m->flags));
106}
107
108static void
109feeder_destroy(struct pcm_feeder *f)
110{
111	FEEDER_FREE(f);
112	free(f->desc, M_FEEDER);
113	kobj_delete((kobj_t)f, M_FEEDER);
114}
115
116static struct pcm_feeder *
117feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
118{
119	struct pcm_feeder *f;
120	int err;
121
122	f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
123	f->align = fc->align;
124	f->desc = malloc(sizeof(*(f->desc)), M_FEEDER, M_WAITOK | M_ZERO);
125	if (desc)
126		*(f->desc) = *desc;
127	else {
128		f->desc->type = FEEDER_ROOT;
129		f->desc->in = 0;
130		f->desc->out = 0;
131		f->desc->flags = 0;
132		f->desc->idx = 0;
133	}
134	f->data = fc->data;
135	f->source = NULL;
136	f->class = fc;
137	err = FEEDER_INIT(f);
138	if (err) {
139		printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
140		feeder_destroy(f);
141		return NULL;
142	} else
143		return f;
144}
145
146struct feeder_class *
147feeder_getclass(struct pcm_feederdesc *desc)
148{
149	struct feedertab_entry *fte;
150
151	SLIST_FOREACH(fte, &feedertab, link) {
152		if ((desc == NULL) && (fte->desc == NULL))
153			return fte->feederclass;
154		if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
155			return fte->feederclass;
156	}
157	return NULL;
158}
159
160int
161chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
162{
163	struct pcm_feeder *nf;
164
165	nf = feeder_create(fc, desc);
166	if (nf == NULL)
167		return -1;
168
169	nf->source = c->feeder;
170
171	if (nf->align > 0)
172		c->align += nf->align;
173	else if (nf->align < 0 && c->align < -nf->align)
174		c->align = -nf->align;
175
176	c->feeder = nf;
177
178	return 0;
179}
180
181int
182chn_removefeeder(struct pcm_channel *c)
183{
184	struct pcm_feeder *f;
185
186	if (c->feeder == NULL)
187		return -1;
188	f = c->feeder;
189	c->feeder = c->feeder->source;
190	feeder_destroy(f);
191	return 0;
192}
193
194struct pcm_feeder *
195chn_findfeeder(struct pcm_channel *c, u_int32_t type)
196{
197	struct pcm_feeder *f;
198
199	f = c->feeder;
200	while (f != NULL) {
201		if (f->desc->type == type)
202			return f;
203		f = f->source;
204	}
205	return NULL;
206}
207
208static int
209chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
210{
211	u_int32_t visited[MAXFEEDERS / 32];
212	u_int32_t idx, mask;
213
214	bzero(visited, sizeof(visited));
215	while (test && (test != stop)) {
216		idx = test->desc->idx;
217		if (idx < 0)
218			panic("bad idx %d", idx);
219		if (idx >= MAXFEEDERS)
220			panic("bad idx %d", idx);
221		mask = 1 << (idx & 31);
222		idx >>= 5;
223		if (visited[idx] & mask)
224			return 0;
225		visited[idx] |= mask;
226		test = test->source;
227	}
228	return 1;
229}
230
231static struct pcm_feeder *
232feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
233{
234	struct feedertab_entry *fte;
235	struct pcm_feeder *try, *ret;
236
237	/* printf("trying %s (%x -> %x)...\n", source->class->name, source->desc->in, source->desc->out); */
238	if (fmtvalid(source->desc->out, to)) {
239		/* printf("got it\n"); */
240		return source;
241	}
242
243	if (maxdepth < 0)
244		return NULL;
245
246	SLIST_FOREACH(fte, &feedertab, link) {
247		if (fte->desc == NULL)
248			goto no;
249		if (fte->desc->type != FEEDER_FMT)
250			goto no;
251		if (fte->desc->in == source->desc->out) {
252			try = feeder_create(fte->feederclass, fte->desc);
253			if (try) {
254				try->source = source;
255				ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
256				if (ret != NULL)
257					return ret;
258				feeder_destroy(try);
259			}
260		}
261no:
262	}
263	/* printf("giving up %s...\n", source->class->name); */
264	return NULL;
265}
266
267u_int32_t
268chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
269{
270	struct pcm_feeder *try, *stop;
271	int max;
272
273	stop = c->feeder;
274	try = NULL;
275	max = 0;
276	while (try == NULL && max < 8) {
277		try = feeder_fmtchain(to, c->feeder, stop, max);
278		max++;
279	}
280	if (try == NULL)
281		return 0;
282	c->feeder = try;
283	c->align = 0;
284#ifdef FEEDER_DEBUG
285	printf("chain: ");
286#endif
287	while (try && (try != stop)) {
288#ifdef FEEDER_DEBUG
289		printf("%s [%d]", try->class->name, try->desc->idx);
290		if (try->source)
291			printf(" -> ");
292#endif
293		if (try->align > 0)
294			c->align += try->align;
295		else if (try->align < 0 && c->align < -try->align)
296			c->align = -try->align;
297		try = try->source;
298	}
299#ifdef FEEDER_DEBUG
300	printf("%s [%d]\n", try->class->name, try->desc->idx);
301#endif
302	return c->feeder->desc->out;
303}
304
305/*****************************************************************************/
306
307static int
308feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
309{
310	struct snd_dbuf *src = source;
311	int l;
312	u_int8_t x;
313
314	KASSERT(count > 0, ("feed_root: count == 0"));
315	/* count &= ~((1 << ch->align) - 1); */
316	KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
317
318	l = min(count, sndbuf_getready(src));
319	sndbuf_dispose(src, buffer, l);
320
321/*
322	if (l < count)
323		printf("appending %d bytes\n", count - l);
324*/
325
326	x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
327	while (l < count)
328		buffer[l++] = x;
329
330	return count;
331}
332
333static kobj_method_t feeder_root_methods[] = {
334    	KOBJMETHOD(feeder_feed,		feed_root),
335	{ 0, 0 }
336};
337static struct feeder_class feeder_root_class = {
338	name:		"feeder_root",
339	methods:	feeder_root_methods,
340	size:		sizeof(struct pcm_feeder),
341	align:		0,
342	desc:		NULL,
343	data:		NULL,
344};
345SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
346SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
347
348
349
350
351
352