channel.c revision 72136
1/* 2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk> 3 * Portions Copyright by Luigi Rizzo - 1997-99 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: head/sys/dev/sound/pcm/channel.c 72136 2001-02-07 20:45:40Z cg $ 28 */ 29 30#include <dev/sound/pcm/sound.h> 31 32#include "feeder_if.h" 33 34MALLOC_DEFINE(M_CHANNEL, "channel", "pcm channel"); 35#define MIN_CHUNK_SIZE 256 /* for uiomove etc. */ 36#define DMA_ALIGN_THRESHOLD 4 37#define DMA_ALIGN_MASK (~(DMA_ALIGN_THRESHOLD - 1)) 38 39#define CANCHANGE(c) (!(c)->buffer.dl) 40#define ROUND(x) ((x) & DMA_ALIGN_MASK) 41 42/* 43#define DEB(x) x 44*/ 45 46static void chn_dmaupdate(pcm_channel *c); 47static void chn_wrintr(pcm_channel *c); 48static void chn_rdintr(pcm_channel *c); 49static int chn_buildfeeder(pcm_channel *c); 50/* 51 * SOUND OUTPUT 52 53We use a circular buffer to store samples directed to the DAC. 54The buffer is split into two variable-size regions, each identified 55by an offset in the buffer (rp,fp) and a length (rl,fl): 56 57 0 rp,rl fp,fl bufsize 58 |__________>____________>________| 59 FREE d READY w FREE 60 61 READY: data written from the process and ready to be sent to the DAC; 62 FREE: free part of the buffer. 63 64Both regions can wrap around the end of the buffer. At initialization, 65READY is empty, FREE takes all the available space, and dma is 66idle. dl contains the length of the current DMA transfer, dl=0 67means that the dma is idle. 68 69The two boundaries (rp,fp) in the buffers are advanced by DMA [d] 70and write() [w] operations. The first portion of the READY region 71is used for DMA transfers. The transfer is started at rp and with 72chunks of length dl. During DMA operations, dsp_wr_dmaupdate() 73updates rp, rl and fl tracking the ISA DMA engine as the transfer 74makes progress. 75When a new block is written, fp advances and rl,fl are updated 76accordingly. 77 78The code works as follows: the user write routine dsp_write_body() 79fills up the READY region with new data (reclaiming space from the 80FREE region) and starts the write DMA engine if inactive. When a 81DMA transfer is complete, an interrupt causes dsp_wrintr() to be 82called which extends the FREE region and possibly starts the next 83transfer. 84 85In some cases, the code tries to track the current status of DMA 86operations by calling dsp_wr_dmaupdate() which changes rp, rl and fl. 87 88The system tries to make all DMA transfers use the same size, 89play_blocksize or rec_blocksize. The size is either selected by 90the user, or computed by the system to correspond to about .25s of 91audio. The blocksize must be within a range which is currently: 92 93 min(5ms, 40 bytes) ... 1/2 buffer size. 94 95When there aren't enough data (write) or space (read), a transfer 96is started with a reduced size. 97 98To reduce problems in case of overruns, the routine which fills up 99the buffer should initialize (e.g. by repeating the last value) a 100reasonably long area after the last block so that no noise is 101produced on overruns. 102 103 * 104 */ 105 106 107/* XXX this is broken: in the event a bounce buffer is used, data never 108 * gets copied in or out of the real buffer. fix requires mods to isa_dma.c 109 * and possibly fixes to other autodma mode clients 110 */ 111static int 112chn_polltrigger(pcm_channel *c) 113{ 114 snd_dbuf *bs = &c->buffer2nd; 115 unsigned lim = (c->flags & CHN_F_HAS_SIZE)? bs->blksz : 0; 116 int trig = 0; 117 118 if (c->flags & CHN_F_MAPPED) 119 trig = ((bs->int_count > bs->prev_int_count) || bs->prev_int_count == 0); 120 else 121 trig = (((c->direction == PCMDIR_PLAY)? bs->fl : bs->rl) > lim); 122 return trig; 123} 124 125static int 126chn_pollreset(pcm_channel *c) 127{ 128 snd_dbuf *bs = &c->buffer2nd; 129 130 if (c->flags & CHN_F_MAPPED) 131 bs->prev_int_count = bs->int_count; 132 return 1; 133} 134 135/* 136 * chn_dmadone() updates pointers and wakes up any process waiting 137 * on a select(). Must be called at spltty(). 138 */ 139static void 140chn_dmadone(pcm_channel *c) 141{ 142 snd_dbuf *b = &c->buffer; 143 144 if (c->direction == PCMDIR_PLAY) 145 chn_checkunderflow(c); 146 else 147 chn_dmaupdate(c); 148 if (ISA_DMA(b)) 149 sndbuf_isadmabounce(b); /* sync bounce buffer */ 150 b->int_count++; 151} 152 153/* 154 * chn_dmawakeup() wakes up any process sleeping. Separated from 155 * chn_dmadone() so that wakeup occurs only when feed from a 156 * secondary buffer to a DMA buffer takes place. Must be called 157 * at spltty(). 158 */ 159static void 160chn_dmawakeup(pcm_channel *c) 161{ 162 snd_dbuf *b = &c->buffer; 163 164 wakeup(b); 165} 166 167/* 168 * chn_dmaupdate() tracks the status of a dma transfer, 169 * updating pointers. It must be called at spltty(). 170 * 171 * NOTE: when we are using auto dma in the device, rl might become 172 * negative. 173 */ 174DEB (static int chn_updatecount=0); 175 176static void 177chn_dmaupdate(pcm_channel *c) 178{ 179 snd_dbuf *b = &c->buffer; 180 int delta, hwptr; 181 DEB (int b_rl=b->rl; int b_fl=b->fl; int b_rp=b->rp; int b_fp=b->fp); 182 183 hwptr = chn_getptr(c); 184 delta = (b->bufsize + hwptr - b->hp) % b->bufsize; 185 DEB( 186 if (delta >= ((b->bufsize * 15) / 16)) { 187 if (!(c->flags & (CHN_F_CLOSING | CHN_F_ABORTING))) 188 device_printf(c->parent->dev, "hwptr went backwards %d -> %d\n", b->hp, hwptr); 189 } 190 ) 191 if (c->direction == PCMDIR_PLAY) { 192 delta = (b->bufsize + hwptr - b->rp) % b->bufsize; 193 b->rp = hwptr; 194 b->rl -= delta; 195 b->fl += delta; 196 197 if (b->rl < 0) { 198 DEB(printf("OUCH!(%d) rl %d(%d) delta %d bufsize %d hwptr %d rp %d(%d)\n", chn_updatecount++, b->rl, b_rl, delta, b->bufsize, hwptr, b->rp, b_rp)); 199 } 200 } else { 201 delta = (b->bufsize + hwptr - b->fp) % b->bufsize; 202 b->fp = hwptr; 203 b->rl += delta; 204 b->fl -= delta; 205 if (b->fl < 0) { 206 DEB(printf("OUCH!(%d) fl %d(%d) delta %d bufsize %d hwptr %d fp %d(%d)\n", chn_updatecount++, b->fl, b_fl, delta, b->bufsize, hwptr, b->fp, b_fp)); 207 } 208 } 209 b->hp = hwptr; 210 b->total += delta; 211} 212 213/* 214 * Check channel for underflow occured. Reset DMA buffer in case of 215 * underflow, so that new data can go into the buffer. It must be 216 * called at spltty(). 217 */ 218void 219chn_checkunderflow(pcm_channel *c) 220{ 221 snd_dbuf *b = &c->buffer; 222 223 if (b->underflow) { 224 DEB(printf("Clear underflow condition\n")); 225 /* 226 * The DMA keeps running even after underflow occurs. 227 * Hence the value returned by chn_getptr() here soon 228 * gets a lag when we get back to chn_write(). Although 229 * there are no easy and precise methods to figure out 230 * the lag, a quarter of b->bufsize would be a fair 231 * choice, provided that a DMA interrupt generates upon 232 * each transfer of a half b->bufsize. 233 */ 234 b->rp = chn_getptr(c); 235 b->fp = (b->rp + b->bufsize / 4) % b->bufsize; 236 b->rl = b->bufsize / 4; 237 b->fl = b->bufsize - b->rl; 238 b->underflow = 0; 239 } else { 240 chn_dmaupdate(c); 241 } 242} 243 244/* 245 * Feeds new data to the write dma buffer. Can be called in the bottom half. 246 * Hence must be called at spltty. 247 */ 248int 249chn_wrfeed(pcm_channel *c) 250{ 251 snd_dbuf *b = &c->buffer; 252 snd_dbuf *bs = &c->buffer2nd; 253 int a, l, lacc; 254 255 /* ensure we always have a whole number of samples */ 256 a = (1 << c->align) - 1; 257 lacc = 0; 258 if (c->flags & CHN_F_MAPPED) { 259 bs->rl = min(b->blksz, b->fl); 260 bs->fl = 0; 261 a = 0; 262 } 263 DEB(if (c->flags & CHN_F_CLOSING) 264 printf("b: [rl: %d, rp %d, fl %d, fp %d]; bs: [rl: %d, rp %d, fl %d, fp %d]\n", 265 b->rl, b->rp, b->fl, b->fp, bs->rl, bs->rp, bs->fl, bs->fp)); 266 /* Don't allow write unaligned data */ 267 while (bs->rl > a && b->fl > a) { 268 /* ensure we always have a whole number of samples */ 269 l = min(min(bs->rl, bs->bufsize - bs->rp), min(b->fl, b->bufsize - b->fp)) & ~a; 270 if (l == 0) 271 return lacc; 272 /* Move the samples, update the markers and pointers. */ 273 bcopy(bs->buf + bs->rp, b->buf + b->fp, l); 274 bs->fl += l; 275 bs->rl -= l; 276 bs->rp = (bs->rp + l) % bs->bufsize; 277 b->rl += l; 278 b->fl -= l; 279 b->fp = (b->fp + l) % b->bufsize; 280 /* Clear the new space in the secondary buffer. */ 281 sndbuf_clear(bs, l); 282 /* Accumulate the total bytes of the moved samples. */ 283 lacc += l; 284 /* A feed to the DMA buffer is equivalent to an interrupt. */ 285 bs->total += l; 286 if (c->flags & CHN_F_MAPPED) { 287 if (bs->total - bs->prev_total >= bs->blksz) { 288 bs->prev_total = bs->total; 289 bs->int_count++; 290 c->blocks++; 291 } 292 } else 293 bs->int_count++; 294 if (bs->sel.si_pid && chn_polltrigger(c)) 295 selwakeup(&bs->sel); 296 } 297 298 return lacc; 299} 300 301/* Feeds new data to the secondary write buffer. */ 302static int 303chn_wrfeed2nd(pcm_channel *c, struct uio *buf) 304{ 305 snd_dbuf *bs = &c->buffer2nd; 306 int l, w, wacc, hl; 307 u_int8_t hackbuf[64]; 308 309 /* The DMA buffer may have some space. */ 310 while (chn_wrfeed(c) > 0); 311 312 /* ensure we always have a whole number of samples */ 313 wacc = 0; 314 hl = 0; 315 while (buf->uio_resid > 0 && bs->fl > 64) { 316 /* 317 * The size of the data to move here does not have to be 318 * aligned. We take care of it upon moving the data to a 319 * DMA buffer. 320 */ 321 l = min(bs->fl, bs->bufsize - bs->fp); 322 /* Move the samples, update the markers and pointers. */ 323 if (l < 64) { 324 w = FEEDER_FEED(c->feeder, c, hackbuf, 64, buf); 325 l = min(w, bs->bufsize - bs->fp); 326 bcopy(hackbuf, bs->buf + bs->fp, l); 327 if (w > l) 328 bcopy(hackbuf + l, bs->buf, w - l); 329 } else 330 w = FEEDER_FEED(c->feeder, c, bs->buf + bs->fp, l, buf); 331 if (w == 0) 332 panic("no feed"); 333 bs->rl += w; 334 bs->fl -= w; 335 bs->fp = (bs->fp + w) % bs->bufsize; 336 /* Accumulate the total bytes of the moved samples. */ 337 wacc += w; 338 339 /* If any pcm data gets moved, push it to the DMA buffer. */ 340 if (w > 0) 341 while (chn_wrfeed(c) > 0); 342 } 343 344 return wacc; 345} 346 347/* 348 * Write interrupt routine. Can be called from other places (e.g. 349 * to start a paused transfer), but with interrupts disabled. 350 */ 351static void 352chn_wrintr(pcm_channel *c) 353{ 354 snd_dbuf *b = &c->buffer; 355 356 if (b->underflow && !(c->flags & CHN_F_MAPPED)) { 357/* printf("underflow return\n"); 358*/ return; /* nothing new happened */ 359 } 360 if (b->dl) 361 chn_dmadone(c); 362 363 /* 364 * start another dma operation only if have ready data in the buffer, 365 * there is no pending abort, have a full-duplex device, or have a 366 * half duplex device and there is no pending op on the other side. 367 * 368 * Force transfers to be aligned to a boundary of 4, which is 369 * needed when doing stereo and 16-bit. 370 */ 371 372 /* Check underflow and update the pointers. */ 373 chn_checkunderflow(c); 374 375 /* 376 * Fill up the DMA buffer, followed by waking up the top half. 377 * If some of the pcm data in uio are still left, the top half 378 * goes to sleep by itself. 379 */ 380 if (c->flags & CHN_F_MAPPED) 381 chn_wrfeed(c); 382 else { 383 while (chn_wrfeed(c) > 0); 384 sndbuf_clear(b, b->fl); 385 } 386 chn_dmawakeup(c); 387 if (c->flags & CHN_F_TRIGGERED) { 388 chn_dmaupdate(c); 389 /* 390 * check if we need to reprogram the DMA on the sound card. 391 * This happens if the size has changed from zero 392 */ 393 if (b->dl == 0) { 394 /* Start DMA operation */ 395 b->dl = b->blksz; /* record new transfer size */ 396 chn_trigger(c, PCMTRIG_START); 397 } 398 /* 399 * Emulate writing by DMA, i.e. transfer the pcm data from 400 * the emulated-DMA buffer to the device itself. 401 */ 402 chn_trigger(c, PCMTRIG_EMLDMAWR); 403 if (b->rl < b->dl) { 404 DEB(printf("near underflow (%d < %d), %d\n", b->rl, b->dl, b->fl)); 405 /* 406 * we are near to underflow condition, so to prevent 407 * audio 'clicks' clear next b->fl bytes 408 */ 409 sndbuf_clear(b, b->fl); 410 if (b->rl < DMA_ALIGN_THRESHOLD) 411 b->underflow = 1; 412 } 413 } else { 414 /* cannot start a new dma transfer */ 415 DEB(printf("underflow, flags 0x%08x rp %d rl %d\n", c->flags, b->rp, b->rl)); 416 if (b->dl) { /* DMA was active */ 417 b->underflow = 1; /* set underflow flag */ 418 sndbuf_clear(b, b->bufsize); 419 } 420 } 421} 422 423/* 424 * user write routine 425 * 426 * advance the boundary between READY and FREE, fill the space with 427 * uiomove(), and possibly start DMA. Do the above until the transfer 428 * is complete. 429 * 430 * To minimize latency in case a pending DMA transfer is about to end, 431 * we do the transfer in pieces of increasing sizes, extending the 432 * READY area at every checkpoint. In the (necessary) assumption that 433 * memory bandwidth is larger than the rate at which the dma consumes 434 * data, we reduce the latency to something proportional to the length 435 * of the first piece, while keeping the overhead low and being able 436 * to feed the DMA with large blocks. 437 */ 438 439int 440chn_write(pcm_channel *c, struct uio *buf) 441{ 442 int ret = 0, timeout, res, newsize, count; 443 long s; 444 snd_dbuf *b = &c->buffer; 445 snd_dbuf *bs = &c->buffer2nd; 446 447 if (c->flags & CHN_F_WRITING) { 448 /* This shouldn't happen and is actually silly 449 * - will never wake up, just timeout; why not sleep on b? 450 */ 451 tsleep(&s, PZERO, "pcmwrW", hz); 452 return EBUSY; 453 } 454 c->flags |= CHN_F_WRITING; 455 c->flags &= ~CHN_F_ABORTING; 456 s = spltty(); 457 458 /* 459 * XXX Certain applications attempt to write larger size 460 * of pcm data than c->blocksize2nd without blocking, 461 * resulting partial write. Expand the block size so that 462 * the write operation avoids blocking. 463 */ 464 if ((c->flags & CHN_F_NBIO) && buf->uio_resid > bs->blksz) { 465 DEB(printf("pcm warning: broken app, nbio and tried to write %d bytes with fragsz %d\n", 466 buf->uio_resid, bs->blksz)); 467 newsize = 16; 468 while (newsize < min(buf->uio_resid, CHN_2NDBUFMAXSIZE / 2)) 469 newsize <<= 1; 470 chn_setblocksize(c, bs->blkcnt, newsize); 471 DEB(printf("pcm warning: frags reset to %d x %d\n", bs->blkcnt, bs->blksz)); 472 } 473 474 /* 475 * Fill up the secondary and DMA buffer. 476 * chn_wrfeed*() takes care of the alignment. 477 */ 478 479 /* Check for underflow before writing into the buffers. */ 480 chn_checkunderflow(c); 481 while (chn_wrfeed2nd(c, buf) > 0); 482 if ((c->flags & CHN_F_NBIO) && (buf->uio_resid > 0)) 483 ret = EAGAIN; 484 485 /* Start playing if not yet. */ 486 if (!b->dl) 487 chn_start(c, 0); 488 489 if (ret == 0) { 490 count = hz; 491 /* Wait until all samples are played in blocking mode. */ 492 while ((buf->uio_resid > 0) && (count > 0)) { 493 /* Check for underflow before writing into the buffers. */ 494 chn_checkunderflow(c); 495 /* Fill up the buffers with new pcm data. */ 496 res = buf->uio_resid; 497 while (chn_wrfeed2nd(c, buf) > 0); 498 if (buf->uio_resid < res) 499 count = hz; 500 else 501 count--; 502 503 /* Have we finished to feed the secondary buffer? */ 504 if (buf->uio_resid == 0) 505 break; 506 507 /* Wait for new free space to write new pcm samples. */ 508 /* splx(s); */ 509 timeout = 1; /*(buf->uio_resid >= b->dl)? hz / 20 : 1; */ 510 ret = tsleep(b, PRIBIO | PCATCH, "pcmwr", timeout); 511 /* s = spltty(); */ 512 /* if (ret == EINTR) chn_abort(c); */ 513 if (ret == EINTR || ret == ERESTART) 514 break; 515 } 516 if (count == 0) { 517 c->flags |= CHN_F_DEAD; 518 device_printf(c->parent->dev, "play interrupt timeout, channel dead\n"); 519 } 520 } else 521 ret = 0; 522 c->flags &= ~CHN_F_WRITING; 523 splx(s); 524 return ret; 525} 526 527/* 528 * SOUND INPUT 529 * 530 531The input part is similar to the output one, with a circular buffer 532split in two regions, and boundaries advancing because of read() calls 533[r] or dma operation [d]. At initialization, as for the write 534routine, READY is empty, and FREE takes all the space. 535 536 0 rp,rl fp,fl bufsize 537 |__________>____________>________| 538 FREE r READY d FREE 539 540Operation is as follows: upon user read (dsp_read_body()) a DMA read 541is started if not already active (marked by b->dl > 0), 542then as soon as data are available in the READY region they are 543transferred to the user buffer, thus advancing the boundary between FREE 544and READY. Upon interrupts, caused by a completion of a DMA transfer, 545the READY region is extended and possibly a new transfer is started. 546 547When necessary, dsp_rd_dmaupdate() is called to advance fp (and update 548rl,fl accordingly). Upon user reads, rp is advanced and rl,fl are 549updated accordingly. 550 551The rules to choose the size of the new DMA area are similar to 552the other case, with a preferred constant transfer size equal to 553rec_blocksize, and fallback to smaller sizes if no space is available. 554 555 */ 556 557static int 558chn_rddump(pcm_channel *c, int cnt) 559{ 560 snd_dbuf *b = &c->buffer; 561 int maxover, ss; 562 563 ss = 1; 564 ss <<= (b->fmt & AFMT_STEREO)? 1 : 0; 565 ss <<= (b->fmt & AFMT_16BIT)? 1 : 0; 566 maxover = c->speed * ss; 567 568 b->overrun += cnt; 569 if (b->overrun > maxover) { 570 device_printf(c->parent->dev, "record overrun, dumping %d bytes\n", 571 b->overrun); 572 b->overrun = 0; 573 } 574 b->rl -= cnt; 575 b->fl += cnt; 576 b->rp = (b->rp + cnt) % b->bufsize; 577 return cnt; 578} 579 580/* 581 * Feed new data from the read buffer. Can be called in the bottom half. 582 * Hence must be called at spltty. 583 */ 584int 585chn_rdfeed(pcm_channel *c) 586{ 587 snd_dbuf *b = &c->buffer; 588 snd_dbuf *bs = &c->buffer2nd; 589 int l, lacc; 590 591 /* 592 printf("b: [rl: %d, rp %d, fl %d, fp %d]; bs: [rl: %d, rp %d, fl %d, fp %d]\n", 593 b->rl, b->rp, b->fl, b->fp, bs->rl, bs->rp, bs->fl, bs->fp); 594 */ 595 /* ensure we always have a whole number of samples */ 596 lacc = 0; 597 while (bs->fl >= DMA_ALIGN_THRESHOLD && b->rl >= DMA_ALIGN_THRESHOLD) { 598 l = min(min(bs->fl, bs->bufsize - bs->fp), min(b->rl, b->bufsize - b->rp)) & DMA_ALIGN_MASK; 599 /* Move the samples, update the markers and pointers. */ 600 bcopy(b->buf + b->rp, bs->buf + bs->fp, l); 601 bs->fl -= l; 602 bs->rl += l; 603 bs->fp = (bs->fp + l) % bs->bufsize; 604 b->rl -= l; 605 b->fl += l; 606 b->rp = (b->rp + l) % b->bufsize; 607 /* Accumulate the total bytes of the moved samples. */ 608 lacc += l; 609 /* A feed from the DMA buffer is equivalent to an interrupt. */ 610 bs->int_count++; 611 if (bs->sel.si_pid && chn_polltrigger(c)) 612 selwakeup(&bs->sel); 613 } 614 615 return lacc; 616} 617 618/* Feeds new data from the secondary read buffer. */ 619static int 620chn_rdfeed2nd(pcm_channel *c, struct uio *buf) 621{ 622 snd_dbuf *bs = &c->buffer2nd; 623 int l, w, wacc; 624 625 /* ensure we always have a whole number of samples */ 626 wacc = 0; 627 while ((buf->uio_resid > 0) && (bs->rl > 0)) { 628 /* The DMA buffer may have pcm data. */ 629 /* while (chn_rdfeed(c) > 0); */ 630 /* 631 * The size of the data to move here does not have to be 632 * aligned. We take care of it upon moving the data to a 633 * DMA buffer. 634 */ 635 l = min(bs->rl, bs->bufsize - bs->rp); 636 /* Move the samples, update the markers and pointers. */ 637 w = FEEDER_FEED(c->feeder, c, bs->buf + bs->rp, l, buf); 638 if (w == 0) 639 panic("no feed"); 640 bs->fl += w; 641 bs->rl -= w; 642 bs->rp = (bs->rp + w) % bs->bufsize; 643 /* Clear the new space in the secondary buffer. */ 644 sndbuf_clear(bs, l); 645 /* Accumulate the total bytes of the moved samples. */ 646 bs->total += w; 647 wacc += w; 648 } 649 650 return wacc; 651} 652 653/* read interrupt routine. Must be called with interrupts blocked. */ 654static void 655chn_rdintr(pcm_channel *c) 656{ 657 snd_dbuf *b = &c->buffer; 658 659 if (b->dl) chn_dmadone(c); 660 661 DEB(printf("rdintr: start dl %d, rp:rl %d:%d, fp:fl %d:%d\n", 662 b->dl, b->rp, b->rl, b->fp, b->fl)); 663 664 /* Update the pointers. */ 665 chn_dmaupdate(c); 666 667 /* 668 * Suck up the DMA buffer, followed by waking up the top half. 669 * If some of the pcm data in the secondary buffer are still left, 670 * the top half goes to sleep by itself. 671 */ 672 while(chn_rdfeed(c) > 0); 673 chn_dmawakeup(c); 674 675 if (b->fl < b->dl) { 676 DEB(printf("near overflow (%d < %d), %d\n", b->fl, b->dl, b->rl)); 677 chn_rddump(c, b->blksz - b->fl); 678 } 679 680 if (c->flags & CHN_F_TRIGGERED) { 681 /* 682 * check if we need to reprogram the DMA on the sound card. 683 * This happens if the size has changed from zero 684 */ 685 if (b->dl == 0) { 686 /* Start DMA operation */ 687 b->dl = b->blksz; /* record new transfer size */ 688 chn_trigger(c, PCMTRIG_START); 689 } 690 /* 691 * Emulate writing by DMA, i.e. transfer the pcm data from 692 * the emulated-DMA buffer to the device itself. 693 */ 694 chn_trigger(c, PCMTRIG_EMLDMARD); 695 } else { 696 if (b->dl) { /* was active */ 697 b->dl = 0; 698 chn_trigger(c, PCMTRIG_STOP); 699 chn_dmaupdate(c); 700 } 701 } 702} 703 704/* 705 * body of user-read routine 706 * 707 * Start DMA if not active; wait for READY not empty. 708 * Transfer data from READY region using uiomove(), advance boundary 709 * between FREE and READY. Repeat until transfer is complete. 710 * 711 * To avoid excessive latency in freeing up space for the DMA 712 * engine, transfers are done in blocks of increasing size, so that 713 * the latency is proportional to the size of the smallest block, but 714 * we have a low overhead and are able to feed the dma engine with 715 * large blocks. 716 * 717 * NOTE: in the current version, read will not return more than 718 * blocksize bytes at once (unless more are already available), to 719 * avoid that requests using very large buffers block for too long. 720 */ 721 722int 723chn_read(pcm_channel *c, struct uio *buf) 724{ 725 int ret = 0, timeout, limit, res, count; 726 long s; 727 snd_dbuf *b = &c->buffer; 728 snd_dbuf *bs = &c->buffer2nd; 729 730 if (c->flags & CHN_F_READING) { 731 /* This shouldn't happen and is actually silly */ 732 tsleep(&s, PZERO, "pcmrdR", hz); 733 return (EBUSY); 734 } 735 736 s = spltty(); 737 738 /* Store the initial size in the uio. */ 739 res = buf->uio_resid; 740 741 c->flags |= CHN_F_READING; 742 c->flags &= ~CHN_F_ABORTING; 743 744 /* suck up the DMA and secondary buffers. */ 745 while (chn_rdfeed2nd(c, buf) > 0); 746 747 if (buf->uio_resid == 0) 748 goto skip; 749 750 limit = res - b->blksz; 751 if (limit < 0) 752 limit = 0; 753 754 /* Start capturing if not yet. */ 755 if ((!bs->rl || !b->rl) && !b->dl) 756 chn_start(c, 0); 757 758 if (!(c->flags & CHN_F_NBIO)) { 759 count = hz; 760 /* Wait until all samples are captured. */ 761 while ((buf->uio_resid > 0) && (count > 0)) { 762 /* Suck up the DMA and secondary buffers. */ 763 chn_dmaupdate(c); 764 res = buf->uio_resid; 765 while (chn_rdfeed(c) > 0); 766 while (chn_rdfeed2nd(c, buf) > 0); 767 if (buf->uio_resid < res) 768 count = hz; 769 else 770 count--; 771 772 /* Have we finished to feed the uio? */ 773 if (buf->uio_resid == 0) 774 break; 775 776 /* Wait for new pcm samples. */ 777 /* splx(s); */ 778 timeout = (buf->uio_resid - limit >= b->dl)? hz / 20 : 1; 779 ret = tsleep(b, PRIBIO | PCATCH, "pcmrd", 1); 780 /* s = spltty(); */ 781 /* if (ret == EINTR) chn_abort(c); */ 782 if (ret == EINTR || ret == ERESTART) 783 break; 784 } 785 if (count == 0) { 786 c->flags |= CHN_F_DEAD; 787 device_printf(c->parent->dev, "record interrupt timeout, channel dead\n"); 788 } 789 } else { 790 /* If no pcm data was read on nonblocking, return EAGAIN. */ 791 if (buf->uio_resid == res) 792 ret = EAGAIN; 793 } 794 795skip: 796 c->flags &= ~CHN_F_READING; 797 splx(s); 798 return ret; 799} 800 801void 802chn_intr(pcm_channel *c) 803{ 804 if (c->flags & CHN_F_INIT) 805 chn_reinit(c); 806 if (c->direction == PCMDIR_PLAY) 807 chn_wrintr(c); 808 else 809 chn_rdintr(c); 810} 811 812u_int32_t 813chn_start(pcm_channel *c, int force) 814{ 815 u_int32_t r, s; 816 snd_dbuf *b = &c->buffer; 817 818 r = 0; 819 s = spltty(); 820 if (b->dl == 0 && !(c->flags & CHN_F_NOTRIGGER)) { 821 if (c->direction == PCMDIR_PLAY) { 822 if (!(c->flags & CHN_F_MAPPED)) 823 while (chn_wrfeed(c) > 0); /* Fill up the DMA buffer. */ 824 if (force || (b->rl >= b->blksz)) 825 r = CHN_F_TRIGGERED; 826 } else { 827 if (!(c->flags & CHN_F_MAPPED)) 828 while (chn_rdfeed(c) > 0); /* Suck up the DMA buffer. */ 829 if (force || (b->fl >= b->blksz)) 830 r = CHN_F_TRIGGERED; 831 } 832 c->flags |= r; 833 chn_intr(c); 834 } 835 splx(s); 836 return r; 837} 838 839void 840chn_resetbuf(pcm_channel *c) 841{ 842 snd_dbuf *b = &c->buffer; 843 snd_dbuf *bs = &c->buffer2nd; 844 845 c->blocks = 0; 846 sndbuf_reset(b); 847 sndbuf_reset(bs); 848} 849 850/* 851 * chn_sync waits until the space in the given channel goes above 852 * a threshold. The threshold is checked against fl or rl respectively. 853 * Assume that the condition can become true, do not check here... 854 */ 855int 856chn_sync(pcm_channel *c, int threshold) 857{ 858 u_long s, rdy; 859 int ret; 860 snd_dbuf *b = &c->buffer; 861 snd_dbuf *bs = &c->buffer2nd; 862 863 for (;;) { 864 s = spltty(); 865 chn_checkunderflow(c); 866 while (chn_wrfeed(c) > 0); 867 rdy = (c->direction == PCMDIR_PLAY)? bs->fl : bs->rl; 868 if (rdy <= threshold) { 869 ret = tsleep((caddr_t)b, PRIBIO | PCATCH, "pcmsyn", 1); 870 splx(s); 871 if (ret == ERESTART || ret == EINTR) { 872 DEB(printf("chn_sync: tsleep returns %d\n", ret)); 873 return -1; 874 } 875 } else break; 876 } 877 splx(s); 878 return 0; 879} 880 881int 882chn_poll(pcm_channel *c, int ev, struct proc *p) 883{ 884 snd_dbuf *b = &c->buffer; 885 snd_dbuf *bs = &c->buffer2nd; 886 u_long s; 887 int ret; 888 889 s = spltty(); 890 if (!(c->flags & CHN_F_MAPPED)) { 891 if (c->direction == PCMDIR_PLAY) { 892 /* Fill up the DMA buffer. */ 893 chn_checkunderflow(c); 894 while (chn_wrfeed(c) > 0); 895 } else { 896 /* Suck up the DMA buffer. */ 897 chn_dmaupdate(c); 898 while (chn_rdfeed(c) > 0); 899 } 900 if (!b->dl) 901 chn_start(c, 1); 902 } 903 ret = 0; 904 if (chn_polltrigger(c) && chn_pollreset(c)) 905 ret = ev; 906 else 907 selrecord(p, &bs->sel); 908 splx(s); 909 return ret; 910} 911 912/* 913 * chn_abort is a non-blocking function which aborts a pending 914 * DMA transfer and flushes the buffers. 915 * It returns the number of bytes that have not been transferred. 916 */ 917int 918chn_abort(pcm_channel *c) 919{ 920 int missing = 0, cnt = 0; 921 snd_dbuf *b = &c->buffer; 922 snd_dbuf *bs = &c->buffer2nd; 923 924 if (!b->dl) 925 return 0; 926 c->flags |= CHN_F_ABORTING; 927 c->flags &= ~CHN_F_TRIGGERED; 928 cnt = 10; 929 while (!b->underflow && (cnt-- > 0)) 930 tsleep((caddr_t)b, PRIBIO | PCATCH, "pcmabr", hz / 50); 931 chn_trigger(c, PCMTRIG_ABORT); 932 b->dl = 0; 933 chn_dmaupdate(c); 934 missing = bs->rl + b->rl; 935 return missing; 936} 937 938/* 939 * this routine tries to flush the dma transfer. It is called 940 * on a close. We immediately abort any read DMA 941 * operation, and then wait for the play buffer to drain. 942 */ 943 944int 945chn_flush(pcm_channel *c) 946{ 947 int ret, count, s, resid, resid_p; 948 snd_dbuf *b = &c->buffer; 949 snd_dbuf *bs = &c->buffer2nd; 950 951 DEB(printf("chn_flush c->flags 0x%08x\n", c->flags)); 952 if (!b->dl) 953 return 0; 954 955 c->flags |= CHN_F_CLOSING; 956 if (c->direction == PCMDIR_REC) 957 chn_abort(c); 958 else { 959 resid = b->rl + bs->rl; 960 resid_p = resid; 961 count = 10; 962 while ((count > 0) && (resid > 0) && !b->underflow) { 963 /* still pending output data. */ 964 ret = tsleep((caddr_t)b, PRIBIO | PCATCH, "pcmflu", hz / 10); 965 if (ret == EINTR || ret == ERESTART) { 966 DEB(printf("chn_flush: tsleep returns %d\n", ret)); 967 return ret; 968 } 969 s = spltty(); 970 chn_dmaupdate(c); 971 splx(s); 972 DEB(printf("chn_flush: now rl = %d, fl = %d, resid = %d\n", b->rl, b->fl, resid)); 973 resid = b->rl + bs->rl; 974 if (resid >= resid_p) 975 count--; 976 resid_p = resid; 977 } 978 if (count == 0) 979 DEB(printf("chn_flush: timeout flushing dbuf_out, cnt 0x%x flags 0x%x\n", b->rl, c->flags)); 980 if (b->dl) 981 chn_abort(c); 982 } 983 c->flags &= ~CHN_F_CLOSING; 984 return 0; 985} 986 987int 988fmtvalid(u_int32_t fmt, u_int32_t *fmtlist) 989{ 990 int i; 991 992 for (i = 0; fmtlist[i]; i++) 993 if (fmt == fmtlist[i]) 994 return 1; 995 return 0; 996} 997 998int 999chn_reset(pcm_channel *c, u_int32_t fmt) 1000{ 1001 int hwspd, r = 0; 1002 1003 chn_abort(c); 1004 c->flags &= CHN_F_RESET; 1005 CHANNEL_RESET(c->methods, c->devinfo); 1006 if (fmt) { 1007 hwspd = DSP_DEFAULT_SPEED; 1008 RANGE(hwspd, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed); 1009 c->speed = hwspd; 1010 1011 r = chn_setformat(c, fmt); 1012 if (r == 0) 1013 r = chn_setspeed(c, hwspd); 1014 if (r == 0) 1015 r = chn_setvolume(c, 100, 100); 1016 } 1017 r = chn_setblocksize(c, 0, 0); 1018 if (r) 1019 return r; 1020 chn_resetbuf(c); 1021 CHANNEL_RESETDONE(c->methods, c->devinfo); 1022 /* c->flags |= CHN_F_INIT; */ 1023 return 0; 1024} 1025 1026int 1027chn_reinit(pcm_channel *c) 1028{ 1029 if ((c->flags & CHN_F_INIT) && CANCHANGE(c)) { 1030 chn_setformat(c, c->format); 1031 chn_setspeed(c, c->speed); 1032 chn_setvolume(c, (c->volume >> 8) & 0xff, c->volume & 0xff); 1033 c->flags &= ~CHN_F_INIT; 1034 return 1; 1035 } 1036 return 0; 1037} 1038 1039int 1040chn_init(pcm_channel *c, void *devinfo, int dir) 1041{ 1042 struct feeder_class *fc; 1043 snd_dbuf *b = &c->buffer; 1044 snd_dbuf *bs = &c->buffer2nd; 1045 1046 /* Initialize the hardware and DMA buffer first. */ 1047 c->feeder = NULL; 1048 fc = feeder_getclass(NULL); 1049 if (fc == NULL) 1050 return EINVAL; 1051 if (chn_addfeeder(c, fc, NULL)) 1052 return EINVAL; 1053 1054 c->flags = 0; 1055 c->feederflags = 0; 1056 c->buffer.chan = -1; 1057 c->devinfo = CHANNEL_INIT(c->methods, devinfo, &c->buffer, c, dir); 1058 if (c->devinfo == NULL) 1059 return ENODEV; 1060 if (c->buffer.bufsize == 0) 1061 return ENOMEM; 1062 chn_setdir(c, dir); 1063 1064 /* And the secondary buffer. */ 1065 bs->buf = NULL; 1066 sndbuf_setfmt(b, AFMT_U8); 1067 sndbuf_setfmt(bs, AFMT_U8); 1068 bs->bufsize = 0; 1069 return 0; 1070} 1071 1072int 1073chn_kill(pcm_channel *c) 1074{ 1075 if (c->flags & CHN_F_TRIGGERED) 1076 chn_trigger(c, PCMTRIG_ABORT); 1077 while (chn_removefeeder(c) == 0); 1078 if (CHANNEL_FREE(c->methods, c->devinfo)) 1079 sndbuf_free(&c->buffer); 1080 c->flags |= CHN_F_DEAD; 1081 return 0; 1082} 1083 1084int 1085chn_setdir(pcm_channel *c, int dir) 1086{ 1087 int r; 1088 1089 c->direction = dir; 1090 r = CHANNEL_SETDIR(c->methods, c->devinfo, c->direction); 1091 if (!r && ISA_DMA(&c->buffer)) 1092 c->buffer.dir = (dir == PCMDIR_PLAY)? ISADMA_WRITE : ISADMA_READ; 1093 return r; 1094} 1095 1096int 1097chn_setvolume(pcm_channel *c, int left, int right) 1098{ 1099 /* could add a feeder for volume changing if channel returns -1 */ 1100 if (CANCHANGE(c)) { 1101 c->volume = (left << 8) | right; 1102 return 0; 1103 } 1104 c->volume = (left << 8) | right; 1105 c->flags |= CHN_F_INIT; 1106 return 0; 1107} 1108 1109int 1110chn_setspeed(pcm_channel *c, int speed) 1111{ 1112 pcm_feeder *f; 1113 snd_dbuf *b = &c->buffer; 1114 snd_dbuf *bs = &c->buffer2nd; 1115 int r, delta; 1116 1117 DEB(printf("want speed %d, ", speed)); 1118 if (speed <= 0) 1119 return EINVAL; 1120 if (CANCHANGE(c)) { 1121 c->speed = speed; 1122 b->spd = speed; 1123 bs->spd = speed; 1124 RANGE(b->spd, chn_getcaps(c)->minspeed, chn_getcaps(c)->maxspeed); 1125 DEB(printf("try speed %d, ", b->spd)); 1126 b->spd = CHANNEL_SETSPEED(c->methods, c->devinfo, b->spd); 1127 DEB(printf("got speed %d, ", b->spd)); 1128 1129 delta = b->spd - bs->spd; 1130 if (delta < 0) 1131 delta = -delta; 1132 1133 c->feederflags &= ~(1 << FEEDER_RATE); 1134 if (delta > 500) 1135 c->feederflags |= 1 << FEEDER_RATE; 1136 else 1137 bs->spd = b->spd; 1138 1139 r = chn_buildfeeder(c); 1140 DEB(printf("r = %d\n", r)); 1141 if (r) 1142 return r; 1143 1144 r = chn_setblocksize(c, 0, 0); 1145 if (r) 1146 return r; 1147 1148 if (!(c->feederflags & (1 << FEEDER_RATE))) 1149 return 0; 1150 1151 f = chn_findfeeder(c, FEEDER_RATE); 1152 DEB(printf("feedrate = %p\n", f)); 1153 if (f == NULL) 1154 return EINVAL; 1155 1156 r = FEEDER_SET(f, FEEDRATE_SRC, bs->spd); 1157 DEB(printf("feeder_set(FEEDRATE_SRC, %d) = %d\n", bs->spd, r)); 1158 if (r) 1159 return r; 1160 1161 r = FEEDER_SET(f, FEEDRATE_DST, b->spd); 1162 DEB(printf("feeder_set(FEEDRATE_DST, %d) = %d\n", b->spd, r)); 1163 if (r) 1164 return r; 1165 1166 return 0; 1167 } 1168 c->speed = speed; 1169 c->flags |= CHN_F_INIT; 1170 return 0; 1171} 1172 1173int 1174chn_setformat(pcm_channel *c, u_int32_t fmt) 1175{ 1176 snd_dbuf *b = &c->buffer; 1177 snd_dbuf *bs = &c->buffer2nd; 1178 int r; 1179 u_int32_t hwfmt; 1180 1181 if (CANCHANGE(c)) { 1182 DEB(printf("want format %d\n", fmt)); 1183 c->format = fmt; 1184 hwfmt = c->format; 1185 c->feederflags &= ~(1 << FEEDER_FMT); 1186 if (!fmtvalid(hwfmt, chn_getcaps(c)->fmtlist)) 1187 c->feederflags |= 1 << FEEDER_FMT; 1188 r = chn_buildfeeder(c); 1189 if (r) 1190 return r; 1191 hwfmt = c->feeder->desc->out; 1192 sndbuf_setfmt(b, hwfmt); 1193 sndbuf_setfmt(bs, hwfmt); 1194 chn_resetbuf(c); 1195 CHANNEL_SETFORMAT(c->methods, c->devinfo, hwfmt); 1196 return chn_setspeed(c, c->speed); 1197 } 1198 c->format = fmt; 1199 c->flags |= CHN_F_INIT; 1200 return 0; 1201} 1202 1203int 1204chn_setblocksize(pcm_channel *c, int blkcnt, int blksz) 1205{ 1206 snd_dbuf *b = &c->buffer; 1207 snd_dbuf *bs = &c->buffer2nd; 1208 int s, bufsz, irqhz, tmp; 1209 1210 if (!CANCHANGE(c) || (c->flags & CHN_F_MAPPED)) 1211 return EINVAL; 1212 1213 if (blksz == 0 || blksz == -1) { 1214 if (blksz == -1) 1215 c->flags &= ~CHN_F_HAS_SIZE; 1216 if (!(c->flags & CHN_F_HAS_SIZE)) { 1217 blksz = (bs->bps * bs->spd) / CHN_DEFAULT_HZ; 1218 tmp = 32; 1219 while (tmp <= blksz) 1220 tmp <<= 1; 1221 tmp >>= 1; 1222 blksz = tmp; 1223 blkcnt = CHN_2NDBUFMAXSIZE / blksz; 1224 1225 RANGE(blksz, 16, CHN_2NDBUFMAXSIZE / 2); 1226 RANGE(blkcnt, 2, CHN_2NDBUFMAXSIZE / blksz); 1227 } else { 1228 blksz = bs->blksz; 1229 blkcnt = bs->blkcnt; 1230 } 1231 } else { 1232 if ((blksz < 16) || (blkcnt < 2) || (blkcnt * blksz > CHN_2NDBUFMAXSIZE)) 1233 return EINVAL; 1234 c->flags |= CHN_F_HAS_SIZE; 1235 } 1236 1237 bufsz = blkcnt * blksz; 1238 1239 s = spltty(); 1240 1241 if (bs->buf != NULL) 1242 free(bs->buf, M_DEVBUF); 1243 bs->buf = malloc(bufsz, M_DEVBUF, M_WAITOK); 1244 if (bs->buf == NULL) { 1245 splx(s); 1246 DEB(printf("chn_setblocksize: out of memory\n")); 1247 return ENOSPC; 1248 } 1249 1250 bs->bufsize = bufsz; 1251 bs->blkcnt = blkcnt; 1252 bs->blksz = blksz; 1253 1254 /* adjust for different hw format/speed */ 1255 irqhz = (bs->bps * bs->spd) / bs->blksz; 1256 RANGE(irqhz, 16, 512); 1257 1258 b->blksz = (b->bps * b->spd) / irqhz; 1259 1260 /* round down to 2^x */ 1261 blksz = 32; 1262 while (blksz <= b->blksz) 1263 blksz <<= 1; 1264 blksz >>= 1; 1265 1266 /* round down to fit hw buffer size */ 1267 RANGE(blksz, 16, b->maxsize / 2); 1268 1269 b->blksz = CHANNEL_SETBLOCKSIZE(c->methods, c->devinfo, blksz); 1270 1271 chn_resetbuf(c); 1272 splx(s); 1273 1274 return 0; 1275} 1276 1277int 1278chn_trigger(pcm_channel *c, int go) 1279{ 1280 return CHANNEL_TRIGGER(c->methods, c->devinfo, go); 1281} 1282 1283int 1284chn_getptr(pcm_channel *c) 1285{ 1286 int hwptr; 1287 int a = (1 << c->align) - 1; 1288 snd_dbuf *b = &c->buffer; 1289 1290 hwptr = b->dl? CHANNEL_GETPTR(c->methods, c->devinfo) : 0; 1291 /* don't allow unaligned values in the hwa ptr */ 1292 hwptr &= ~a ; /* Apply channel align mask */ 1293 hwptr &= DMA_ALIGN_MASK; /* Apply DMA align mask */ 1294 return hwptr; 1295} 1296 1297pcmchan_caps * 1298chn_getcaps(pcm_channel *c) 1299{ 1300 return CHANNEL_GETCAPS(c->methods, c->devinfo); 1301} 1302 1303u_int32_t 1304chn_getformats(pcm_channel *c) 1305{ 1306 u_int32_t *fmtlist, fmts; 1307 int i; 1308 1309 fmtlist = chn_getcaps(c)->fmtlist; 1310 fmts = 0; 1311 for (i = 0; fmtlist[i]; i++) 1312 fmts |= fmtlist[i]; 1313 1314 return fmts; 1315} 1316 1317static int 1318chn_buildfeeder(pcm_channel *c) 1319{ 1320 struct feeder_class *fc; 1321 struct pcm_feederdesc desc; 1322 u_int32_t tmp[2], src, dst, type, flags; 1323 1324 while (chn_removefeeder(c) == 0); 1325 KASSERT((c->feeder == NULL), ("feeder chain not empty")); 1326 c->align = 0; 1327 fc = feeder_getclass(NULL); 1328 if (fc == NULL) 1329 return EINVAL; 1330 if (chn_addfeeder(c, fc, NULL)) 1331 return EINVAL; 1332 c->feeder->desc->out = c->format; 1333 1334 flags = c->feederflags; 1335 src = c->feeder->desc->out; 1336 if ((c->flags & CHN_F_MAPPED) && (flags != 0)) 1337 return EINVAL; 1338 DEB(printf("not mapped, flags %x, ", flags)); 1339 for (type = FEEDER_RATE; type <= FEEDER_LAST; type++) { 1340 if (flags & (1 << type)) { 1341 desc.type = type; 1342 desc.in = 0; 1343 desc.out = 0; 1344 desc.flags = 0; 1345 DEB(printf("find feeder type %d, ", type)); 1346 fc = feeder_getclass(&desc); 1347 DEB(printf("got %p\n", fc)); 1348 if (fc == NULL) 1349 return EINVAL; 1350 dst = fc->desc->in; 1351 if (src != dst) { 1352 DEB(printf("build fmtchain from %x to %x: ", src, dst)); 1353 tmp[0] = dst; 1354 tmp[1] = 0; 1355 if (chn_fmtchain(c, tmp) == 0) 1356 return EINVAL; 1357 DEB(printf("ok\n")); 1358 } 1359 if (chn_addfeeder(c, fc, fc->desc)) 1360 return EINVAL; 1361 src = fc->desc->out; 1362 DEB(printf("added feeder %p, output %x\n", fc, src)); 1363 dst = 0; 1364 flags &= ~(1 << type); 1365 } 1366 } 1367 if (!fmtvalid(src, chn_getcaps(c)->fmtlist)) { 1368 if (chn_fmtchain(c, chn_getcaps(c)->fmtlist) == 0) 1369 return EINVAL; 1370 DEB(printf("built fmtchain from %x to %x\n", src, c->feeder->desc->out)); 1371 flags &= ~(1 << FEEDER_FMT); 1372 } 1373 return 0; 1374} 1375