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