feeder.c revision 74763
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 74763 2001-03-24 23:10:29Z 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		fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
73		fte->feederclass = fc;
74		fte->desc = &fc->desc[i];
75		fte->idx = feedercnt;
76		fte->desc->idx = feedercnt;
77		SLIST_INSERT_HEAD(&feedertab, fte, link);
78		i++;
79	}
80	feedercnt++;
81	if (feedercnt >= MAXFEEDERS)
82		printf("MAXFEEDERS exceeded\n");
83}
84
85static void
86feeder_unregisterall(void *p)
87{
88	struct feedertab_entry *fte, *next;
89
90	next = SLIST_FIRST(&feedertab);
91	while (next != NULL) {
92		fte = next;
93		next = SLIST_NEXT(fte, link);
94		free(fte, M_FEEDER);
95	}
96}
97
98static int
99cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
100{
101	return ((n->type == m->type) &&
102		((n->in == 0) || (n->in == m->in)) &&
103		((n->out == 0) || (n->out == m->out)) &&
104		(n->flags == m->flags));
105}
106
107static void
108feeder_destroy(struct pcm_feeder *f)
109{
110	FEEDER_FREE(f);
111	free(f->desc, M_FEEDER);
112	kobj_delete((kobj_t)f, M_FEEDER);
113}
114
115static struct pcm_feeder *
116feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
117{
118	struct pcm_feeder *f;
119	int err;
120
121	f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
122	f->align = fc->align;
123	f->desc = malloc(sizeof(*(f->desc)), M_FEEDER, M_WAITOK | M_ZERO);
124	if (desc)
125		*(f->desc) = *desc;
126	else {
127		f->desc->type = FEEDER_ROOT;
128		f->desc->in = 0;
129		f->desc->out = 0;
130		f->desc->flags = 0;
131		f->desc->idx = 0;
132	}
133	f->data = fc->data;
134	f->source = NULL;
135	err = FEEDER_INIT(f);
136	if (err) {
137		feeder_destroy(f);
138		return NULL;
139	} else
140		return f;
141}
142
143struct feeder_class *
144feeder_getclass(struct pcm_feederdesc *desc)
145{
146	struct feedertab_entry *fte;
147
148	SLIST_FOREACH(fte, &feedertab, link) {
149		if ((desc == NULL) && (fte->desc == NULL))
150			return fte->feederclass;
151		if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
152			return fte->feederclass;
153	}
154	return NULL;
155}
156
157int
158chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
159{
160	struct pcm_feeder *nf;
161
162	nf = feeder_create(fc, desc);
163	if (nf == NULL)
164		return -1;
165
166	nf->source = c->feeder;
167
168	if (nf->align > 0)
169		c->align += nf->align;
170	else if (nf->align < 0 && c->align < -nf->align)
171		c->align = -nf->align;
172
173	c->feeder = nf;
174
175	return 0;
176}
177
178int
179chn_removefeeder(struct pcm_channel *c)
180{
181	struct pcm_feeder *f;
182
183	if (c->feeder == NULL)
184		return -1;
185	f = c->feeder;
186	c->feeder = c->feeder->source;
187	feeder_destroy(f);
188	return 0;
189}
190
191struct pcm_feeder *
192chn_findfeeder(struct pcm_channel *c, u_int32_t type)
193{
194	struct pcm_feeder *f;
195
196	f = c->feeder;
197	while (f != NULL) {
198		if (f->desc->type == type)
199			return f;
200		f = f->source;
201	}
202	return NULL;
203}
204
205static int
206chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
207{
208	u_int32_t visited[MAXFEEDERS / 32];
209	u_int32_t idx, mask;
210
211	bzero(visited, sizeof(visited));
212	while (test && (test != stop)) {
213		idx = test->desc->idx;
214		if (idx < 0)
215			panic("bad idx %d", idx);
216		if (idx >= MAXFEEDERS)
217			panic("bad idx %d", idx);
218		mask = 1 << (idx & 31);
219		idx >>= 5;
220		if (visited[idx] & mask)
221			return 0;
222		visited[idx] |= mask;
223		test = test->source;
224	}
225	return 1;
226}
227
228static struct pcm_feeder *
229feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
230{
231	struct feedertab_entry *fte;
232	struct pcm_feeder *try, *ret;
233	struct pcm_feederdesc trydesc;
234
235	/* printf("trying %s...\n", source->name); */
236	if (fmtvalid(source->desc->out, to)) {
237		/* printf("got it\n"); */
238		return source;
239	}
240
241	if (maxdepth < 0)
242		return NULL;
243
244	trydesc.type = FEEDER_FMT;
245	trydesc.in = source->desc->out;
246	trydesc.out = 0;
247	trydesc.flags = 0;
248	trydesc.idx = -1;
249
250	SLIST_FOREACH(fte, &feedertab, link) {
251		if ((fte->desc) && (fte->desc->in == source->desc->out)) {
252			trydesc.out = fte->desc->out;
253			trydesc.idx = fte->idx;
254			try = feeder_create(fte->feederclass, &trydesc);
255			if (try == NULL)
256				return NULL;
257			try->source = source;
258			ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
259			if (ret != NULL)
260				return ret;
261			feeder_destroy(try);
262		}
263	}
264	/* printf("giving up %s...\n", source->name); */
265	return NULL;
266}
267
268u_int32_t
269chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
270{
271	struct pcm_feeder *try, *stop;
272	int max;
273
274	stop = c->feeder;
275	try = NULL;
276	max = 0;
277	while (try == NULL && max < 8) {
278		try = feeder_fmtchain(to, c->feeder, stop, max);
279		max++;
280	}
281	if (try == NULL)
282		return 0;
283	c->feeder = try;
284	c->align = 0;
285#ifdef FEEDER_DEBUG
286	printf("chain: ");
287#endif
288	while (try && (try != stop)) {
289#ifdef FEEDER_DEBUG
290		printf("%s [%d]", try->name, try->desc->idx);
291		if (try->source)
292			printf(" -> ");
293#endif
294		if (try->align > 0)
295			c->align += try->align;
296		else if (try->align < 0 && c->align < -try->align)
297			c->align = -try->align;
298		try = try->source;
299	}
300#ifdef FEEDER_DEBUG
301	printf("%s [%d]\n", try->name, try->desc->idx);
302#endif
303	return c->feeder->desc->out;
304}
305
306/*****************************************************************************/
307
308static int
309feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
310{
311	struct snd_dbuf *src = source;
312	int l;
313	u_int8_t x;
314
315	KASSERT(count > 0, ("feed_root: count == 0"));
316	/* count &= ~((1 << ch->align) - 1); */
317	KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
318
319	l = min(count, sndbuf_getready(src));
320	sndbuf_dispose(src, buffer, l);
321
322/*
323	if (l < count)
324		printf("appending %d bytes\n", count - l);
325*/
326
327	x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
328	while (l < count)
329		buffer[l++] = x;
330
331	return count;
332}
333
334static kobj_method_t feeder_root_methods[] = {
335    	KOBJMETHOD(feeder_feed,		feed_root),
336	{ 0, 0 }
337};
338static struct feeder_class feeder_root_class = {
339	name:		"feeder_root",
340	methods:	feeder_root_methods,
341	size:		sizeof(struct pcm_feeder),
342	align:		0,
343	desc:		NULL,
344	data:		NULL,
345};
346SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
347SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
348
349
350
351
352
353