feeder.c revision 97274
12490Sjkh/* 22490Sjkh * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 32490Sjkh * All rights reserved. 42490Sjkh * 52490Sjkh * Redistribution and use in source and binary forms, with or without 62490Sjkh * modification, are permitted provided that the following conditions 72490Sjkh * are met: 82490Sjkh * 1. Redistributions of source code must retain the above copyright 92490Sjkh * notice, this list of conditions and the following disclaimer. 102490Sjkh * 2. Redistributions in binary form must reproduce the above copyright 112490Sjkh * notice, this list of conditions and the following disclaimer in the 122490Sjkh * documentation and/or other materials provided with the distribution. 132490Sjkh * 142490Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 152490Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 162490Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 172490Sjkh * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 182490Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 192490Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 202490Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 212490Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 222490Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 232490Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 242490Sjkh * SUCH DAMAGE. 252490Sjkh */ 262490Sjkh 272490Sjkh#include <dev/sound/pcm/sound.h> 282490Sjkh 292490Sjkh#include "feeder_if.h" 302490Sjkh 312490SjkhSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder.c 97274 2002-05-25 11:18:03Z bde $"); 322490Sjkh 332490SjkhMALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder"); 342490Sjkh 352490Sjkh#define MAXFEEDERS 256 362490Sjkh#undef FEEDER_DEBUG 372490Sjkh 3853920Sbillfstruct feedertab_entry { 392490Sjkh SLIST_ENTRY(feedertab_entry) link; 402490Sjkh struct feeder_class *feederclass; 412490Sjkh struct pcm_feederdesc *desc; 422490Sjkh 432490Sjkh int idx; 4453920Sbillf}; 4523726Speterstatic SLIST_HEAD(, feedertab_entry) feedertab; 4653920Sbillf 4753920Sbillf/*****************************************************************************/ 4853920Sbillf 492490Sjkhvoid 502490Sjkhfeeder_register(void *p) 512490Sjkh{ 522490Sjkh static int feedercnt = 0; 532490Sjkh 542490Sjkh struct feeder_class *fc = p; 552490Sjkh struct feedertab_entry *fte; 562490Sjkh int i; 572490Sjkh 582490Sjkh if (feedercnt == 0) { 592490Sjkh KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name)); 602490Sjkh 612490Sjkh SLIST_INIT(&feedertab); 622490Sjkh fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO); 632490Sjkh if (fte == NULL) { 642490Sjkh printf("can't allocate memory for root feeder: %s\n", 652490Sjkh fc->name); 662490Sjkh 672490Sjkh return; 682490Sjkh } 692490Sjkh fte->feederclass = fc; 702490Sjkh fte->desc = NULL; 712490Sjkh fte->idx = feedercnt; 722490Sjkh SLIST_INSERT_HEAD(&feedertab, fte, link); 732490Sjkh feedercnt++; 742490Sjkh 752490Sjkh /* we've got our root feeder so don't veto pcm loading anymore */ 7623726Speter pcm_veto_load = 0; 772490Sjkh 782490Sjkh return; 792490Sjkh } 802490Sjkh 812490Sjkh KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name)); 822490Sjkh 838856Srgrimes /* beyond this point failure is non-fatal but may result in some translations being unavailable */ 842490Sjkh i = 0; 852490Sjkh while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) { 862490Sjkh /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */ 872490Sjkh fte = malloc(sizeof(*fte), M_FEEDER, M_WAITOK | M_ZERO); 882490Sjkh if (fte == NULL) { 8942338Simp printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); 9042338Simp 912490Sjkh return; 922490Sjkh } 932490Sjkh fte->feederclass = fc; 942490Sjkh fte->desc = &fc->desc[i]; 952490Sjkh fte->idx = feedercnt; 962490Sjkh fte->desc->idx = feedercnt; 972490Sjkh SLIST_INSERT_HEAD(&feedertab, fte, link); 982490Sjkh i++; 992490Sjkh } 1002490Sjkh feedercnt++; 1012490Sjkh if (feedercnt >= MAXFEEDERS) 1022490Sjkh printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS); 10342338Simp} 1042490Sjkh 10542338Simpstatic void 10642338Simpfeeder_unregisterall(void *p) 10742338Simp{ 1082490Sjkh struct feedertab_entry *fte, *next; 1092490Sjkh 1102490Sjkh next = SLIST_FIRST(&feedertab); 1112490Sjkh while (next != NULL) { 1122490Sjkh fte = next; 1132490Sjkh next = SLIST_NEXT(fte, link); 1142490Sjkh free(fte, M_FEEDER); 1152490Sjkh } 1162490Sjkh} 1172490Sjkh 1182490Sjkhstatic int 1192490Sjkhcmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m) 1202490Sjkh{ 1212490Sjkh return ((n->type == m->type) && 1222490Sjkh ((n->in == 0) || (n->in == m->in)) && 1232490Sjkh ((n->out == 0) || (n->out == m->out)) && 1242490Sjkh (n->flags == m->flags)); 1252490Sjkh} 1262490Sjkh 1272490Sjkhstatic void 1282490Sjkhfeeder_destroy(struct pcm_feeder *f) 12942338Simp{ 1302490Sjkh FEEDER_FREE(f); 1312490Sjkh kobj_delete((kobj_t)f, M_FEEDER); 1322490Sjkh} 1332490Sjkh 1342490Sjkhstatic struct pcm_feeder * 1352490Sjkhfeeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) 1362490Sjkh{ 1372490Sjkh struct pcm_feeder *f; 1382490Sjkh int err; 1392490Sjkh 1402490Sjkh f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_WAITOK | M_ZERO); 1412490Sjkh if (f == NULL) 14242338Simp return NULL; 1432490Sjkh 1442490Sjkh f->align = fc->align; 1452490Sjkh f->data = fc->data; 1462490Sjkh f->source = NULL; 1472490Sjkh f->parent = NULL; 1482490Sjkh f->class = fc; 1492490Sjkh f->desc = &(f->desc_static); 1502490Sjkh 1512490Sjkh if (desc) { 1522490Sjkh *(f->desc) = *desc; 1532490Sjkh } else { 1542490Sjkh f->desc->type = FEEDER_ROOT; 1552490Sjkh f->desc->in = 0; 1562490Sjkh f->desc->out = 0; 1572490Sjkh f->desc->flags = 0; 1582490Sjkh f->desc->idx = 0; 1592490Sjkh } 16085408Sroam 1612490Sjkh err = FEEDER_INIT(f); 1622490Sjkh if (err) { 1632490Sjkh printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); 1642490Sjkh feeder_destroy(f); 1652490Sjkh 1662490Sjkh return NULL; 1672490Sjkh } 1682490Sjkh 1692490Sjkh return f; 1702490Sjkh} 1712490Sjkh 1722490Sjkhstruct feeder_class * 1732490Sjkhfeeder_getclass(struct pcm_feederdesc *desc) 1742490Sjkh{ 1752490Sjkh struct feedertab_entry *fte; 1762490Sjkh 1772490Sjkh SLIST_FOREACH(fte, &feedertab, link) { 1782490Sjkh if ((desc == NULL) && (fte->desc == NULL)) 1792490Sjkh return fte->feederclass; 18042357Simp if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc)) 1812490Sjkh return fte->feederclass; 1822490Sjkh } 1832490Sjkh return NULL; 1842490Sjkh} 1852490Sjkh 1862490Sjkhint 1872490Sjkhchn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) 1882490Sjkh{ 1892490Sjkh struct pcm_feeder *nf; 19042357Simp 1912490Sjkh nf = feeder_create(fc, desc); 1922490Sjkh if (nf == NULL) 1932490Sjkh return ENOSPC; 1942490Sjkh 1952490Sjkh nf->source = c->feeder; 19642357Simp 19742357Simp if (nf->align > 0) 19842357Simp c->align += nf->align; 1992490Sjkh else if (nf->align < 0 && c->align < -nf->align) 2002490Sjkh c->align = -nf->align; 2012490Sjkh 2022490Sjkh c->feeder = nf; 2032490Sjkh 2042490Sjkh return 0; 2052490Sjkh} 2062490Sjkh 2072490Sjkhint 2082490Sjkhchn_removefeeder(struct pcm_channel *c) 20942338Simp{ 2102490Sjkh struct pcm_feeder *f; 2112490Sjkh 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 221struct pcm_feeder * 222chn_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 236static int 237chainok(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 260static struct pcm_feeder * 261feeder_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 296u_int32_t 297chn_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 377static int 378feed_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 403static kobj_method_t feeder_root_methods[] = { 404 KOBJMETHOD(feeder_feed, feed_root), 405 { 0, 0 } 406}; 407static 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}; 415SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class); 416SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL); 417 418 419 420 421 422