xref: /dragonfly/sys/dev/sound/pcm/feeder.c (revision 6e285212)
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: 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 $
28  */
29 
30 #include <dev/sound/pcm/sound.h>
31 
32 #include "feeder_if.h"
33 
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/feeder.c,v 1.2 2003/06/17 04:28:31 dillon Exp $");
35 
36 MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
37 
38 #define MAXFEEDERS 	256
39 #undef FEEDER_DEBUG
40 
41 struct feedertab_entry {
42 	SLIST_ENTRY(feedertab_entry) link;
43 	struct feeder_class *feederclass;
44 	struct pcm_feederdesc *desc;
45 
46 	int idx;
47 };
48 static SLIST_HEAD(, feedertab_entry) feedertab;
49 
50 /*****************************************************************************/
51 
52 void
53 feeder_register(void *p)
54 {
55 	static int feedercnt = 0;
56 
57 	struct feeder_class *fc = p;
58 	struct feedertab_entry *fte;
59 	int i;
60 
61 	if (feedercnt == 0) {
62 		KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
63 
64 		SLIST_INIT(&feedertab);
65 		fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
66 		if (fte == NULL) {
67 			printf("can't allocate memory for root feeder\n", fc->name);
68 
69 			return;
70 		}
71 		fte->feederclass = fc;
72 		fte->desc = NULL;
73 		fte->idx = feedercnt;
74 		SLIST_INSERT_HEAD(&feedertab, fte, link);
75 		feedercnt++;
76 
77 		/* we've got our root feeder so don't veto pcm loading anymore */
78 		pcm_veto_load = 0;
79 
80 		return;
81 	}
82 
83 	KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
84 
85 	/* beyond this point failure is non-fatal but may result in some translations being unavailable */
86 	i = 0;
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);
90 		if (fte == NULL) {
91 			printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out);
92 
93 			return;
94 		}
95 		fte->feederclass = fc;
96 		fte->desc = &fc->desc[i];
97 		fte->idx = feedercnt;
98 		fte->desc->idx = feedercnt;
99 		SLIST_INSERT_HEAD(&feedertab, fte, link);
100 		i++;
101 	}
102 	feedercnt++;
103 	if (feedercnt >= MAXFEEDERS)
104 		printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
105 }
106 
107 static void
108 feeder_unregisterall(void *p)
109 {
110 	struct feedertab_entry *fte, *next;
111 
112 	next = SLIST_FIRST(&feedertab);
113 	while (next != NULL) {
114 		fte = next;
115 		next = SLIST_NEXT(fte, link);
116 		free(fte, M_FEEDER);
117 	}
118 }
119 
120 static int
121 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
122 {
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));
127 }
128 
129 static void
130 feeder_destroy(struct pcm_feeder *f)
131 {
132 	FEEDER_FREE(f);
133 	kobj_delete((kobj_t)f, M_FEEDER);
134 }
135 
136 static struct pcm_feeder *
137 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
138 {
139 	struct pcm_feeder *f;
140 	int err;
141 
142 	f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
143 	if (f == NULL)
144 		return NULL;
145 
146 	f->align = fc->align;
147 	f->data = fc->data;
148 	f->source = NULL;
149 	f->parent = NULL;
150 	f->class = fc;
151 	f->desc = &(f->desc_static);
152 
153 	if (desc) {
154 		*(f->desc) = *desc;
155 	} else {
156 		f->desc->type = FEEDER_ROOT;
157 		f->desc->in = 0;
158 		f->desc->out = 0;
159 		f->desc->flags = 0;
160 		f->desc->idx = 0;
161 	}
162 
163 	err = FEEDER_INIT(f);
164 	if (err) {
165 		printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
166 		feeder_destroy(f);
167 
168 		return NULL;
169 	}
170 
171 	return f;
172 }
173 
174 struct feeder_class *
175 feeder_getclass(struct pcm_feederdesc *desc)
176 {
177 	struct feedertab_entry *fte;
178 
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;
184 	}
185 	return NULL;
186 }
187 
188 int
189 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
190 {
191 	struct pcm_feeder *nf;
192 
193 	nf = feeder_create(fc, desc);
194 	if (nf == NULL)
195 		return ENOSPC;
196 
197 	nf->source = c->feeder;
198 
199 	if (nf->align > 0)
200 		c->align += nf->align;
201 	else if (nf->align < 0 && c->align < -nf->align)
202 		c->align = -nf->align;
203 
204 	c->feeder = nf;
205 
206 	return 0;
207 }
208 
209 int
210 chn_removefeeder(struct pcm_channel *c)
211 {
212 	struct pcm_feeder *f;
213 
214 	if (c->feeder == NULL)
215 		return -1;
216 	f = c->feeder;
217 	c->feeder = c->feeder->source;
218 	feeder_destroy(f);
219 
220 	return 0;
221 }
222 
223 struct pcm_feeder *
224 chn_findfeeder(struct pcm_channel *c, u_int32_t type)
225 {
226 	struct pcm_feeder *f;
227 
228 	f = c->feeder;
229 	while (f != NULL) {
230 		if (f->desc->type == type)
231 			return f;
232 		f = f->source;
233 	}
234 
235 	return NULL;
236 }
237 
238 static int
239 chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
240 {
241 	u_int32_t visited[MAXFEEDERS / 32];
242 	u_int32_t idx, mask;
243 
244 	bzero(visited, sizeof(visited));
245 	while (test && (test != stop)) {
246 		idx = test->desc->idx;
247 		if (idx < 0)
248 			panic("bad idx %d", idx);
249 		if (idx >= MAXFEEDERS)
250 			panic("bad idx %d", idx);
251 		mask = 1 << (idx & 31);
252 		idx >>= 5;
253 		if (visited[idx] & mask)
254 			return 0;
255 		visited[idx] |= mask;
256 		test = test->source;
257 	}
258 
259 	return 1;
260 }
261 
262 static struct pcm_feeder *
263 feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
264 {
265 	struct feedertab_entry *fte;
266 	struct pcm_feeder *try, *ret;
267 
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"); */
271 		return source;
272 	}
273 
274 	if (maxdepth < 0)
275 		return NULL;
276 
277 	SLIST_FOREACH(fte, &feedertab, link) {
278 		if (fte->desc == NULL)
279 			goto no;
280 		if (fte->desc->type != FEEDER_FMT)
281 			goto no;
282 		if (fte->desc->in == source->desc->out) {
283 			try = feeder_create(fte->feederclass, fte->desc);
284 			if (try) {
285 				try->source = source;
286 				ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
287 				if (ret != NULL)
288 					return ret;
289 				feeder_destroy(try);
290 			}
291 		}
292 no:
293 	}
294 	/* printf("giving up %s...\n", source->class->name); */
295 
296 	return NULL;
297 }
298 
299 u_int32_t
300 chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
301 {
302 	struct pcm_feeder *try, *del, *stop;
303 	u_int32_t tmpfrom[2], best, *from;
304 	int i, max, bestmax;
305 
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"));
310 
311 	stop = c->feeder;
312 
313 	if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
314 		from = chn_getcaps(c)->fmtlist;
315 	} else {
316 		tmpfrom[0] = c->feeder->desc->out;
317 		tmpfrom[1] = 0;
318 		from = tmpfrom;
319 	}
320 
321 	i = 0;
322 	best = 0;
323 	bestmax = 100;
324 	while (from[i] != 0) {
325 		c->feeder->desc->out = from[i];
326 		try = NULL;
327 		max = 0;
328 		while (try == NULL && max < 8) {
329 			try = feeder_fmtchain(to, c->feeder, stop, max);
330 			if (try == NULL)
331 				max++;
332 		}
333 		if (try != NULL && max < bestmax) {
334 			bestmax = max;
335 			best = from[i];
336 		}
337 		while (try != NULL && try != stop) {
338 			del = try;
339 			try = try->source;
340 			feeder_destroy(del);
341 		}
342 		i++;
343 	}
344 	if (best == 0)
345 		return 0;
346 
347 	c->feeder->desc->out = best;
348 	try = feeder_fmtchain(to, c->feeder, stop, bestmax);
349 	if (try == NULL)
350 		return 0;
351 
352 	c->feeder = try;
353 	c->align = 0;
354 #ifdef FEEDER_DEBUG
355 	printf("\n\nchain: ");
356 #endif
357 	while (try && (try != stop)) {
358 #ifdef FEEDER_DEBUG
359 		printf("%s [%d]", try->class->name, try->desc->idx);
360 		if (try->source)
361 			printf(" -> ");
362 #endif
363 		if (try->source)
364 			try->source->parent = try;
365 		if (try->align > 0)
366 			c->align += try->align;
367 		else if (try->align < 0 && c->align < -try->align)
368 			c->align = -try->align;
369 		try = try->source;
370 	}
371 #ifdef FEEDER_DEBUG
372 	printf("%s [%d]\n", try->class->name, try->desc->idx);
373 #endif
374 
375 	return (c->direction == PCMDIR_REC)? best : c->feeder->desc->out;
376 }
377 
378 /*****************************************************************************/
379 
380 static int
381 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
382 {
383 	struct snd_dbuf *src = source;
384 	int l;
385 	u_int8_t x;
386 
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));
390 
391 	l = min(count, sndbuf_getready(src));
392 	sndbuf_dispose(src, buffer, l);
393 
394 	/* When recording only return as much data as available */
395 	if (ch->direction == PCMDIR_REC)
396 		return l;
397 
398 /*
399 	if (l < count)
400 		printf("appending %d bytes\n", count - l);
401 */
402 
403 	x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
404 	while (l < count)
405 		buffer[l++] = x;
406 
407 	return count;
408 }
409 
410 static kobj_method_t feeder_root_methods[] = {
411     	KOBJMETHOD(feeder_feed,		feed_root),
412 	{ 0, 0 }
413 };
414 static struct feeder_class feeder_root_class = {
415 	name:		"feeder_root",
416 	methods:	feeder_root_methods,
417 	size:		sizeof(struct pcm_feeder),
418 	align:		0,
419 	desc:		NULL,
420 	data:		NULL,
421 };
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);
424 
425 
426 
427 
428 
429