feeder.c revision 154969
1/*- 2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org> 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 31SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder.c 154969 2006-01-29 01:32:37Z ariff $"); 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 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_NOWAIT | 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_NOWAIT | 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 105static void 106feeder_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 118static int 119cmpdesc(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 127static void 128feeder_destroy(struct pcm_feeder *f) 129{ 130 FEEDER_FREE(f); 131 kobj_delete((kobj_t)f, M_FEEDER); 132} 133 134static struct pcm_feeder * 135feeder_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_NOWAIT | 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 172struct feeder_class * 173feeder_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 186int 187chn_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 /* XXX we should use the lowest common denominator for align */ 198 if (nf->align > 0) 199 c->align += nf->align; 200 else if (nf->align < 0 && c->align < -nf->align) 201 c->align = -nf->align; 202 if (c->feeder != NULL) 203 c->feeder->parent = nf; 204 c->feeder = nf; 205 206 return 0; 207} 208 209int 210chn_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 223struct pcm_feeder * 224chn_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 238static int 239chainok(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 262static struct pcm_feeder * 263feeder_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 DEB(printf("trying %s (0x%08x -> 0x%08x)...\n", source->class->name, source->desc->in, source->desc->out)); 269 if (fmtvalid(source->desc->out, to)) { 270 DEB(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 continue; 280 if (fte->desc->type != FEEDER_FMT) 281 continue; 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 } 293 /* printf("giving up %s...\n", source->class->name); */ 294 295 return NULL; 296} 297 298int 299chn_fmtscore(u_int32_t fmt) 300{ 301 if (fmt & AFMT_32BIT) 302 return 60; 303 if (fmt & AFMT_24BIT) 304 return 50; 305 if (fmt & AFMT_16BIT) 306 return 40; 307 if (fmt & (AFMT_U8|AFMT_S8)) 308 return 30; 309 if (fmt & AFMT_MU_LAW) 310 return 20; 311 if (fmt & AFMT_A_LAW) 312 return 10; 313 return 0; 314} 315 316u_int32_t 317chn_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) 318{ 319 u_int32_t best; 320 int i, score, score2, oldscore; 321 322 best = 0; 323 score = chn_fmtscore(fmt); 324 oldscore = 0; 325 for (i = 0; fmts[i] != 0; i++) { 326 score2 = chn_fmtscore(fmts[i]); 327 if (oldscore == 0 || (score2 == score) || 328 (score2 > oldscore && score2 < score) || 329 (score2 < oldscore && score2 > score) || 330 (oldscore < score && score2 > oldscore)) { 331 best = fmts[i]; 332 oldscore = score2; 333 } 334 } 335 return best; 336} 337 338u_int32_t 339chn_fmtbeststereo(u_int32_t fmt, u_int32_t *fmts) 340{ 341 u_int32_t best; 342 int i, score, score2, oldscore; 343 344 best = 0; 345 score = chn_fmtscore(fmt); 346 oldscore = 0; 347 for (i = 0; fmts[i] != 0; i++) { 348 if ((fmt & AFMT_STEREO) == (fmts[i] & AFMT_STEREO)) { 349 score2 = chn_fmtscore(fmts[i]); 350 if (oldscore == 0 || (score2 == score) || 351 (score2 > oldscore && score2 < score) || 352 (score2 < oldscore && score2 > score) || 353 (oldscore < score && score2 > oldscore)) { 354 best = fmts[i]; 355 oldscore = score2; 356 } 357 } 358 } 359 return best; 360} 361 362u_int32_t 363chn_fmtbest(u_int32_t fmt, u_int32_t *fmts) 364{ 365 u_int32_t best1, best2; 366 int score, score1, score2; 367 368 best1 = chn_fmtbeststereo(fmt, fmts); 369 best2 = chn_fmtbestbit(fmt, fmts); 370 371 if (best1 != 0 && best2 != 0) { 372 if (fmt & AFMT_STEREO) 373 return best1; 374 else { 375 score = chn_fmtscore(fmt); 376 score1 = chn_fmtscore(best1); 377 score2 = chn_fmtscore(best2); 378 if (score1 == score2 || score1 == score) 379 return best1; 380 else if (score2 == score) 381 return best2; 382 else if (score1 > score2) 383 return best1; 384 return best2; 385 } 386 } else if (best2 == 0) 387 return best1; 388 else if (best1 == 0) 389 return best2; 390 391 return best1; 392} 393 394u_int32_t 395chn_fmtchain(struct pcm_channel *c, u_int32_t *to) 396{ 397 struct pcm_feeder *try, *del, *stop; 398 u_int32_t tmpfrom[2], tmpto[2], best, *from; 399 int i, max, bestmax; 400 401 KASSERT(c != NULL, ("c == NULL")); 402 KASSERT(c->feeder != NULL, ("c->feeder == NULL")); 403 KASSERT(to != NULL, ("to == NULL")); 404 KASSERT(to[0] != 0, ("to[0] == 0")); 405 406 stop = c->feeder; 407 408 if (c->direction == PCMDIR_REC && c->feeder->desc->type == FEEDER_ROOT) { 409 from = chn_getcaps(c)->fmtlist; 410 if (fmtvalid(to[0], from)) 411 from = to; 412 else { 413 best = chn_fmtbest(to[0], from); 414 if (best != 0) { 415 tmpfrom[0] = best; 416 tmpfrom[1] = 0; 417 from = tmpfrom; 418 } 419 } 420 } else { 421 tmpfrom[0] = c->feeder->desc->out; 422 tmpfrom[1] = 0; 423 from = tmpfrom; 424 if (to[1] != 0) { 425 if (fmtvalid(tmpfrom[0], to)) { 426 tmpto[0] = tmpfrom[0]; 427 tmpto[1] = 0; 428 to = tmpto; 429 } else { 430 best = chn_fmtbest(tmpfrom[0], to); 431 if (best != 0) { 432 tmpto[0] = best; 433 tmpto[1] = 0; 434 to = tmpto; 435 } 436 } 437 } 438 } 439 440 i = 0; 441 best = 0; 442 bestmax = 100; 443 while (from[i] != 0) { 444 c->feeder->desc->out = from[i]; 445 try = NULL; 446 max = 0; 447 while (try == NULL && max < 8) { 448 try = feeder_fmtchain(to, c->feeder, stop, max); 449 if (try == NULL) 450 max++; 451 } 452 if (try != NULL && max < bestmax) { 453 bestmax = max; 454 best = from[i]; 455 } 456 while (try != NULL && try != stop) { 457 del = try; 458 try = try->source; 459 feeder_destroy(del); 460 } 461 i++; 462 } 463 if (best == 0) 464 return 0; 465 466 c->feeder->desc->out = best; 467 try = feeder_fmtchain(to, c->feeder, stop, bestmax); 468 if (try == NULL) 469 return 0; 470 471 c->feeder = try; 472 c->align = 0; 473#ifdef FEEDER_DEBUG 474 printf("\n\nchain: "); 475#endif 476 while (try && (try != stop)) { 477#ifdef FEEDER_DEBUG 478 printf("%s [%d]", try->class->name, try->desc->idx); 479 if (try->source) 480 printf(" -> "); 481#endif 482 if (try->source) 483 try->source->parent = try; 484 if (try->align > 0) 485 c->align += try->align; 486 else if (try->align < 0 && c->align < -try->align) 487 c->align = -try->align; 488 try = try->source; 489 } 490#ifdef FEEDER_DEBUG 491 printf("%s [%d]\n", try->class->name, try->desc->idx); 492#endif 493 494 if (c->direction == PCMDIR_REC) { 495 try = c->feeder; 496 while (try != NULL) { 497 if (try->desc->type == FEEDER_ROOT) 498 return try->desc->out; 499 try = try->source; 500 } 501 return best; 502 } else 503 return c->feeder->desc->out; 504} 505 506void 507feeder_printchain(struct pcm_feeder *head) 508{ 509 struct pcm_feeder *f; 510 511 printf("feeder chain (head @%p)\n", head); 512 f = head; 513 while (f != NULL) { 514 printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f); 515 f = f->source; 516 } 517 printf("[end]\n\n"); 518} 519 520/*****************************************************************************/ 521 522static int 523feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source) 524{ 525 struct snd_dbuf *src = source; 526 int l; 527 u_int8_t x; 528 529 KASSERT(count > 0, ("feed_root: count == 0")); 530 /* count &= ~((1 << ch->align) - 1); */ 531 KASSERT(count > 0, ("feed_root: aligned count == 0 (align = %d)", ch->align)); 532 533 l = min(count, sndbuf_getready(src)); 534 sndbuf_dispose(src, buffer, l); 535 536 /* When recording only return as much data as available */ 537 if (ch->direction == PCMDIR_REC) 538 return l; 539 540/* 541 if (l < count) 542 printf("appending %d bytes\n", count - l); 543*/ 544 545 x = (sndbuf_getfmt(src) & AFMT_SIGNED)? 0 : 0x80; 546 while (l < count) 547 buffer[l++] = x; 548 549 return count; 550} 551 552static kobj_method_t feeder_root_methods[] = { 553 KOBJMETHOD(feeder_feed, feed_root), 554 { 0, 0 } 555}; 556static struct feeder_class feeder_root_class = { 557 .name = "feeder_root", 558 .methods = feeder_root_methods, 559 .size = sizeof(struct pcm_feeder), 560 .align = 0, 561 .desc = NULL, 562 .data = NULL, 563}; 564SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class); 565SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL); 566 567 568 569 570 571