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