kernel: Remove some unneeded NULL checks after kmalloc() with M_WAITOK.
[dragonfly.git] / sys / dev / sound / pcm / feeder.c
CommitLineData
558a398b
SS
1/*-
2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
984263bc
MD
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.
1de703da 25 *
558a398b 26 * $FreeBSD: src/sys/dev/sound/pcm/feeder.c,v 1.33.2.3 2006/03/07 15:51:19 jhb Exp $
984263bc
MD
27 */
28
29#include <dev/sound/pcm/sound.h>
30
31#include "feeder_if.h"
32
558a398b 33SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/feeder.c,v 1.6 2007/01/04 21:47:03 corecode Exp $");
984263bc
MD
34
35MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
36
37#define MAXFEEDERS 256
38#undef FEEDER_DEBUG
39
40struct feedertab_entry {
41 SLIST_ENTRY(feedertab_entry) link;
42 struct feeder_class *feederclass;
43 struct pcm_feederdesc *desc;
44
45 int idx;
46};
47static SLIST_HEAD(, feedertab_entry) feedertab;
48
49/*****************************************************************************/
50
51void
52feeder_register(void *p)
53{
54 static int feedercnt = 0;
55
56 struct feeder_class *fc = p;
57 struct feedertab_entry *fte;
58 int i;
59
60 if (feedercnt == 0) {
61 KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name));
62
63 SLIST_INIT(&feedertab);
20d1b212 64 fte = kmalloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
984263bc
MD
65 fte->feederclass = fc;
66 fte->desc = NULL;
67 fte->idx = feedercnt;
68 SLIST_INSERT_HEAD(&feedertab, fte, link);
69 feedercnt++;
70
71 /* we've got our root feeder so don't veto pcm loading anymore */
72 pcm_veto_load = 0;
73
74 return;
75 }
76
77 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name));
78
79 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
80 i = 0;
81 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) {
e3869ec7 82 /* kprintf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
20d1b212 83 fte = kmalloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO);
984263bc
MD
84 fte->feederclass = fc;
85 fte->desc = &fc->desc[i];
86 fte->idx = feedercnt;
87 fte->desc->idx = feedercnt;
88 SLIST_INSERT_HEAD(&feedertab, fte, link);
89 i++;
90 }
91 feedercnt++;
92 if (feedercnt >= MAXFEEDERS)
e3869ec7 93 kprintf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS);
984263bc
MD
94}
95
96static void
97feeder_unregisterall(void *p)
98{
99 struct feedertab_entry *fte, *next;
100
101 next = SLIST_FIRST(&feedertab);
102 while (next != NULL) {
103 fte = next;
104 next = SLIST_NEXT(fte, link);
efda3bd0 105 kfree(fte, M_FEEDER);
984263bc
MD
106 }
107}
108
109static int
110cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m)
111{
112 return ((n->type == m->type) &&
113 ((n->in == 0) || (n->in == m->in)) &&
114 ((n->out == 0) || (n->out == m->out)) &&
115 (n->flags == m->flags));
116}
117
118static void
119feeder_destroy(struct pcm_feeder *f)
120{
121 FEEDER_FREE(f);
122 kobj_delete((kobj_t)f, M_FEEDER);
123}
124
125static struct pcm_feeder *
126feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
127{
128 struct pcm_feeder *f;
129 int err;
130
20d1b212 131 f = (void *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO);
984263bc
MD
132 if (f == NULL)
133 return NULL;
134
135 f->align = fc->align;
136 f->data = fc->data;
137 f->source = NULL;
138 f->parent = NULL;
139 f->class = fc;
140 f->desc = &(f->desc_static);
141
142 if (desc) {
143 *(f->desc) = *desc;
144 } else {
145 f->desc->type = FEEDER_ROOT;
146 f->desc->in = 0;
147 f->desc->out = 0;
148 f->desc->flags = 0;
149 f->desc->idx = 0;
150 }
151
152 err = FEEDER_INIT(f);
153 if (err) {
e3869ec7 154 kprintf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
984263bc
MD
155 feeder_destroy(f);
156
157 return NULL;
158 }
159
160 return f;
161}
162
163struct feeder_class *
164feeder_getclass(struct pcm_feederdesc *desc)
165{
166 struct feedertab_entry *fte;
167
168 SLIST_FOREACH(fte, &feedertab, link) {
169 if ((desc == NULL) && (fte->desc == NULL))
170 return fte->feederclass;
171 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc))
172 return fte->feederclass;
173 }
174 return NULL;
175}
176
177int
178chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
179{
180 struct pcm_feeder *nf;
181
182 nf = feeder_create(fc, desc);
183 if (nf == NULL)
184 return ENOSPC;
185
186 nf->source = c->feeder;
187
558a398b 188 /* XXX we should use the lowest common denominator for align */
984263bc
MD
189 if (nf->align > 0)
190 c->align += nf->align;
191 else if (nf->align < 0 && c->align < -nf->align)
192 c->align = -nf->align;
558a398b
SS
193 if (c->feeder != NULL)
194 c->feeder->parent = nf;
984263bc
MD
195 c->feeder = nf;
196
197 return 0;
198}
199
200int
201chn_removefeeder(struct pcm_channel *c)
202{
203 struct pcm_feeder *f;
204
205 if (c->feeder == NULL)
206 return -1;
207 f = c->feeder;
208 c->feeder = c->feeder->source;
209 feeder_destroy(f);
210
211 return 0;
212}
213
214struct pcm_feeder *
215chn_findfeeder(struct pcm_channel *c, u_int32_t type)
216{
217 struct pcm_feeder *f;
218
219 f = c->feeder;
220 while (f != NULL) {
221 if (f->desc->type == type)
222 return f;
223 f = f->source;
224 }
225
226 return NULL;
227}
228
229static int
230chainok(struct pcm_feeder *test, struct pcm_feeder *stop)
231{
232 u_int32_t visited[MAXFEEDERS / 32];
233 u_int32_t idx, mask;
234
235 bzero(visited, sizeof(visited));
236 while (test && (test != stop)) {
237 idx = test->desc->idx;
238 if (idx < 0)
239 panic("bad idx %d", idx);
240 if (idx >= MAXFEEDERS)
241 panic("bad idx %d", idx);
242 mask = 1 << (idx & 31);
243 idx >>= 5;
244 if (visited[idx] & mask)
245 return 0;
246 visited[idx] |= mask;
247 test = test->source;
248 }
249
250 return 1;
251}
252
253static struct pcm_feeder *
254feeder_fmtchain(u_int32_t *to, struct pcm_feeder *source, struct pcm_feeder *stop, int maxdepth)
255{
256 struct feedertab_entry *fte;
257 struct pcm_feeder *try, *ret;
258
558a398b 259 DEB(kprintf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out));
984263bc 260 if (fmtvalid(source->desc->out, to)) {
558a398b 261 DEB(kprintf("got it\n"));
984263bc
MD
262 return source;
263 }
264
265 if (maxdepth < 0)
266 return NULL;
267
268 SLIST_FOREACH(fte, &feedertab, link) {
269 if (fte->desc == NULL)
6b08710e 270 continue;
984263bc 271 if (fte->desc->type != FEEDER_FMT)
6b08710e 272 continue;
984263bc
MD
273 if (fte->desc->in == source->desc->out) {
274 try = feeder_create(fte->feederclass, fte->desc);
275 if (try) {
276 try->source = source;
277 ret = chainok(try, stop)? feeder_fmtchain(to, try, stop, maxdepth - 1) : NULL;
278 if (ret != NULL)
279 return ret;
280 feeder_destroy(try);
281 }
282 }
984263bc 283 }
e3869ec7 284 /* kprintf("giving up %s...\n", source->class->name); */
984263bc
MD
285
286 return NULL;
287}
288
558a398b
SS
289int
290chn_fmtscore(u_int32_t fmt)
291{
292 if (fmt & AFMT_32BIT)
293 return 60;
294 if (fmt & AFMT_24BIT)
295 return 50;
296 if (fmt & AFMT_16BIT)
297 return 40;
298 if (fmt & (AFMT_U8|AFMT_S8))
299 return 30;
300 if (fmt & AFMT_MU_LAW)
301 return 20;
302 if (fmt & AFMT_A_LAW)
303 return 10;
304 return 0;
305}
306
307u_int32_t
308chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
309{
310 u_int32_t best;
311 int i, score, score2, oldscore;
312
313 best = 0;
314 score = chn_fmtscore(fmt);
315 oldscore = 0;
316 for (i = 0; fmts[i] != 0; i++) {
317 score2 = chn_fmtscore(fmts[i]);
318 if (oldscore == 0 || (score2 == score) ||
319 (score2 > oldscore && score2 < score) ||
320 (score2 < oldscore && score2 > score) ||
321 (oldscore < score && score2 > oldscore)) {
322 best = fmts[i];
323 oldscore = score2;
324 }
325 }
326 return best;
327}
328
329u_int32_t
330chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts)
331{
332 u_int32_t best;
333 int i, score, score2, oldscore;
334
335 best = 0;
336 score = chn_fmtscore(fmt);
337 oldscore = 0;
338 for (i = 0; fmts[i] != 0; i++) {
339 if ((fmt & AFMT_STEREO) == (fmts[i] & AFMT_STEREO)) {
340 score2 = chn_fmtscore(fmts[i]);
341 if (oldscore == 0 || (score2 == score) ||
342 (score2 > oldscore && score2 < score) ||
343 (score2 < oldscore && score2 > score) ||
344 (oldscore < score && score2 > oldscore)) {
345 best = fmts[i];
346 oldscore = score2;
347 }
348 }
349 }
350 return best;
351}
352
353u_int32_t
354chn_fmtbest(u_int32_t fmt, u_int32_t *fmts)
355{
356 u_int32_t best1, best2;
357 int score, score1, score2;
358
359 best1 = chn_fmtbeststereo(fmt, fmts);
360 best2 = chn_fmtbestbit(fmt, fmts);
361
362 if (best1 != 0 && best2 != 0) {
363 if (fmt & AFMT_STEREO)
364 return best1;
365 else {
366 score = chn_fmtscore(fmt);
367 score1 = chn_fmtscore(best1);
368 score2 = chn_fmtscore(best2);
369 if (score1 == score2 || score1 == score)
370 return best1;
371 else if (score2 == score)
372 return best2;
373 else if (score1 > score2)
374 return best1;
375 return best2;
376 }
377 } else if (best2 == 0)
378 return best1;
379 else
380 return best2;
381}
382
984263bc
MD
383u_int32_t
384chn_fmtchain(struct pcm_channel *c, u_int32_t *to)
385{
386 struct pcm_feeder *try, *del, *stop;
558a398b 387 u_int32_t tmpfrom[2], tmpto[2], best, *from;
984263bc
MD
388 int i, max, bestmax;
389
390 KASSERT(c != NULL, ("c == NULL"));
391 KASSERT(c->feeder != NULL, ("c->feeder == NULL"));
392 KASSERT(to != NULL, ("to == NULL"));
393 KASSERT(to[0] != 0, ("to[0] == 0"));
394
395 stop = c->feeder;
396
397 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) {
398 from = chn_getcaps(c)->fmtlist;
558a398b
SS
399 if (fmtvalid(to[0], from))
400 from = to;
401 else {
402 best = chn_fmtbest(to[0], from);
403 if (best != 0) {
404 tmpfrom[0] = best;
405 tmpfrom[1] = 0;
406 from = tmpfrom;
407 }
408 }
984263bc
MD
409 } else {
410 tmpfrom[0] = c->feeder->desc->out;
411 tmpfrom[1] = 0;
412 from = tmpfrom;
558a398b
SS
413 if (to[1] != 0) {
414 if (fmtvalid(tmpfrom[0], to)) {
415 tmpto[0] = tmpfrom[0];
416 tmpto[1] = 0;
417 to = tmpto;
418 } else {
419 best = chn_fmtbest(tmpfrom[0], to);
420 if (best != 0) {
421 tmpto[0] = best;
422 tmpto[1] = 0;
423 to = tmpto;
424 }
425 }
426 }
984263bc
MD
427 }
428
429 i = 0;
430 best = 0;
431 bestmax = 100;
432 while (from[i] != 0) {
433 c->feeder->desc->out = from[i];
434 try = NULL;
435 max = 0;
436 while (try == NULL && max < 8) {
437 try = feeder_fmtchain(to, c->feeder, stop, max);
438 if (try == NULL)
439 max++;
440 }
441 if (try != NULL && max < bestmax) {
442 bestmax = max;
443 best = from[i];
444 }
445 while (try != NULL && try != stop) {
446 del = try;
447 try = try->source;
448 feeder_destroy(del);
449 }
450 i++;
451 }
452 if (best == 0)
453 return 0;
454
455 c->feeder->desc->out = best;
456 try = feeder_fmtchain(to, c->feeder, stop, bestmax);
457 if (try == NULL)
458 return 0;
459
460 c->feeder = try;
461 c->align = 0;
462#ifdef FEEDER_DEBUG
e3869ec7 463 kprintf("\n\nchain: ");
984263bc
MD
464#endif
465 while (try && (try != stop)) {
466#ifdef FEEDER_DEBUG
e3869ec7 467 kprintf("%s [%d]", try->class->name, try->desc->idx);
984263bc 468 if (try->source)
e3869ec7 469 kprintf(" -> ");
984263bc
MD
470#endif
471 if (try->source)
472 try->source->parent = try;
473 if (try->align > 0)
474 c->align += try->align;
475 else if (try->align < 0 && c->align < -try->align)
476 c->align = -try->align;
477 try = try->source;
478 }
479#ifdef FEEDER_DEBUG
e3869ec7 480 kprintf("%s [%d]\n", try->class->name, try->desc->idx);
984263bc
MD
481#endif
482
558a398b
SS
483 if (c->direction == PCMDIR_REC) {
484 try = c->feeder;
485 while (try != NULL) {
486 if (try->desc->type == FEEDER_ROOT)
487 return try->desc->out;
488 try = try->source;
489 }
490 return best;
491 } else
492 return c->feeder->desc->out;
493}
494
495void
496feeder_printchain(struct pcm_feeder *head)
497{
498 struct pcm_feeder *f;
499
500 kprintf("feeder chain (head @%p)\n", head);
501 f = head;
502 while (f != NULL) {
503 kprintf("%s/%d @ %p\n", f->class->name, f->desc->idx, f);
504 f = f->source;
505 }
506 kprintf("[end]\n\n");
984263bc
MD
507}
508
509/*****************************************************************************/
510
511static int
512feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
513{
514 struct snd_dbuf *src = source;
515 int l;
516 u_int8_t x;
517
518 KASSERT(count > 0, ("feed_root: count == 0"));
519 /* count &= ~((1 << ch->align) - 1); */
520 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align));
521
522 l = min(count, sndbuf_getready(src));
523 sndbuf_dispose(src, buffer, l);
524
525 /* When recording only return as much data as available */
526 if (ch->direction == PCMDIR_REC)
527 return l;
528
529/*
530 if (l < count)
e3869ec7 531 kprintf("appending %d bytes\n", count - l);
984263bc
MD
532*/
533
534 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80;
535 while (l < count)
536 buffer[l++] = x;
537
538 return count;
539}
540
541static kobj_method_t feeder_root_methods[] = {
542 KOBJMETHOD(feeder_feed, feed_root),
543 { 0, 0 }
544};
545static struct feeder_class feeder_root_class = {
558a398b
SS
546 .name = "feeder_root",
547 .methods = feeder_root_methods,
548 .size = sizeof(struct pcm_feeder),
549 .align = 0,
550 .desc = NULL,
551 .data = NULL,
984263bc
MD
552};
553SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class);
554SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
555
556
557
558
559