1 /*- 2 * Copyright (c) 1999 Cameron Grant <[email protected]> 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 31 SND_DECLARE_FILE("$FreeBSD$"); 32 33 struct snd_dbuf * 34 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel) 35 { 36 struct snd_dbuf *b; 37 38 b = malloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO); 39 snprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc); 40 b->dev = dev; 41 b->channel = channel; 42 43 return b; 44 } 45 46 void 47 sndbuf_destroy(struct snd_dbuf *b) 48 { 49 if (b->tmpbuf) 50 free(b->tmpbuf, M_DEVBUF); 51 if (b->shadbuf) 52 free(b->shadbuf, M_DEVBUF); 53 if (!(b->flags & SNDBUF_F_MANAGED) && b->buf) 54 free(b->buf, M_DEVBUF); 55 free(b, M_DEVBUF); 56 } 57 58 bus_addr_t 59 sndbuf_getbufaddr(struct snd_dbuf *buf) 60 { 61 return (buf->buf_addr); 62 } 63 64 static void 65 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error) 66 { 67 struct snd_dbuf *b = (struct snd_dbuf *)arg; 68 69 if (bootverbose) { 70 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ", 71 (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len); 72 printf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr); 73 } 74 if (error == 0) 75 b->buf_addr = segs[0].ds_addr; 76 else 77 b->buf_addr = 0; 78 } 79 80 /* 81 * Allocate memory for DMA buffer. If the device does not use DMA transfers, 82 * the driver can call malloc(9) and sndbuf_setup() itself. 83 */ 84 85 int 86 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size) 87 { 88 int ret; 89 90 b->dmatag = dmatag; 91 b->maxsize = size; 92 b->bufsize = b->maxsize; 93 b->buf_addr = 0; 94 b->flags |= SNDBUF_F_MANAGED; 95 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, 96 &b->dmamap)) 97 return (ENOMEM); 98 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, 99 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) { 100 bus_dmamem_free(b->dmatag, b->buf, b->dmamap); 101 b->dmamap = NULL; 102 return (ENOMEM); 103 } 104 105 ret = sndbuf_resize(b, 2, b->maxsize / 2); 106 if (ret != 0) 107 sndbuf_free(b); 108 return (ret); 109 } 110 111 int 112 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size) 113 { 114 if (buf) 115 b->flags |= SNDBUF_F_MANAGED; 116 b->buf = buf; 117 b->maxsize = size; 118 b->bufsize = b->maxsize; 119 return sndbuf_resize(b, 2, b->maxsize / 2); 120 } 121 122 void 123 sndbuf_free(struct snd_dbuf *b) 124 { 125 if (b->tmpbuf) 126 free(b->tmpbuf, M_DEVBUF); 127 b->tmpbuf = NULL; 128 129 if (b->shadbuf) 130 free(b->shadbuf, M_DEVBUF); 131 b->shadbuf = NULL; 132 b->sl = 0; 133 134 if (!(b->flags & SNDBUF_F_MANAGED) && b->buf) 135 free(b->buf, M_DEVBUF); 136 137 if (b->dmamap) 138 bus_dmamap_unload(b->dmatag, b->dmamap); 139 140 if (b->dmamap && b->buf) 141 bus_dmamem_free(b->dmatag, b->buf, b->dmamap); 142 b->dmamap = NULL; 143 b->buf = NULL; 144 } 145 146 int 147 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) 148 { 149 u_int8_t *tmpbuf, *f2; 150 151 chn_lock(b->channel); 152 if (b->maxsize == 0) 153 goto out; 154 if (blkcnt == 0) 155 blkcnt = b->blkcnt; 156 if (blksz == 0) 157 blksz = b->blksz; 158 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize)) { 159 chn_unlock(b->channel); 160 return EINVAL; 161 } 162 if (blkcnt == b->blkcnt && blksz == b->blksz) 163 goto out; 164 165 chn_unlock(b->channel); 166 tmpbuf = malloc(blkcnt * blksz, M_DEVBUF, M_NOWAIT); 167 if (tmpbuf == NULL) 168 return ENOMEM; 169 chn_lock(b->channel); 170 b->blkcnt = blkcnt; 171 b->blksz = blksz; 172 b->bufsize = blkcnt * blksz; 173 f2 = b->tmpbuf; 174 b->tmpbuf = tmpbuf; 175 sndbuf_reset(b); 176 chn_unlock(b->channel); 177 free(f2, M_DEVBUF); 178 return 0; 179 out: 180 chn_unlock(b->channel); 181 return 0; 182 } 183 184 int 185 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) 186 { 187 u_int8_t *buf, *tmpbuf, *f1, *f2; 188 u_int8_t *shadbuf, *f3; 189 unsigned int bufsize; 190 int ret; 191 192 if (blkcnt < 2 || blksz < 16) 193 return EINVAL; 194 195 bufsize = blksz * blkcnt; 196 197 chn_unlock(b->channel); 198 buf = malloc(bufsize, M_DEVBUF, M_WAITOK); 199 tmpbuf = malloc(bufsize, M_DEVBUF, M_WAITOK); 200 shadbuf = malloc(bufsize, M_DEVBUF, M_WAITOK); 201 202 chn_lock(b->channel); 203 204 b->blkcnt = blkcnt; 205 b->blksz = blksz; 206 b->bufsize = bufsize; 207 b->maxsize = bufsize; 208 f1 = b->buf; 209 f2 = b->tmpbuf; 210 b->buf = buf; 211 b->tmpbuf = tmpbuf; 212 f3 = b->shadbuf; 213 b->shadbuf = shadbuf; 214 b->sl = bufsize; 215 216 sndbuf_reset(b); 217 218 chn_unlock(b->channel); 219 if (f1) 220 free(f1, M_DEVBUF); 221 if (f2) 222 free(f2, M_DEVBUF); 223 if (f3) 224 free(f3, M_DEVBUF); 225 226 ret = 0; 227 228 chn_lock(b->channel); 229 return ret; 230 } 231 232 /** 233 * @brief Zero out space in buffer free area 234 * 235 * This function clears a chunk of @c length bytes in the buffer free area 236 * (i.e., where the next write will be placed). 237 * 238 * @param b buffer context 239 * @param length number of bytes to blank 240 */ 241 void 242 sndbuf_clear(struct snd_dbuf *b, unsigned int length) 243 { 244 int i; 245 u_char data, *p; 246 247 if (length == 0) 248 return; 249 if (length > b->bufsize) 250 length = b->bufsize; 251 252 data = sndbuf_zerodata(b->fmt); 253 254 i = sndbuf_getfreeptr(b); 255 p = sndbuf_getbuf(b); 256 while (length > 0) { 257 p[i] = data; 258 length--; 259 i++; 260 if (i >= b->bufsize) 261 i = 0; 262 } 263 } 264 265 /** 266 * @brief Zap buffer contents, resetting "ready area" fields 267 * 268 * @param b buffer context 269 */ 270 void 271 sndbuf_fillsilence(struct snd_dbuf *b) 272 { 273 if (b->bufsize > 0) 274 memset(sndbuf_getbuf(b), sndbuf_zerodata(b->fmt), b->bufsize); 275 b->rp = 0; 276 b->rl = b->bufsize; 277 } 278 279 /** 280 * @brief Reset buffer w/o flushing statistics 281 * 282 * This function just zeroes out buffer contents and sets the "ready length" 283 * to zero. This was originally to facilitate minimal playback interruption 284 * (i.e., dropped samples) in SNDCTL_DSP_SILENCE/SKIP ioctls. 285 * 286 * @param b buffer context 287 */ 288 void 289 sndbuf_softreset(struct snd_dbuf *b) 290 { 291 b->rl = 0; 292 if (b->buf && b->bufsize > 0) 293 sndbuf_clear(b, b->bufsize); 294 } 295 296 void 297 sndbuf_reset(struct snd_dbuf *b) 298 { 299 b->hp = 0; 300 b->rp = 0; 301 b->rl = 0; 302 b->dl = 0; 303 b->prev_total = 0; 304 b->total = 0; 305 b->xrun = 0; 306 if (b->buf && b->bufsize > 0) 307 sndbuf_clear(b, b->bufsize); 308 sndbuf_clearshadow(b); 309 } 310 311 u_int32_t 312 sndbuf_getfmt(struct snd_dbuf *b) 313 { 314 return b->fmt; 315 } 316 317 int 318 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt) 319 { 320 b->fmt = fmt; 321 b->bps = 1; 322 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0; 323 if (b->fmt & AFMT_16BIT) 324 b->bps <<= 1; 325 else if (b->fmt & AFMT_24BIT) 326 b->bps *= 3; 327 else if (b->fmt & AFMT_32BIT) 328 b->bps <<= 2; 329 return 0; 330 } 331 332 unsigned int 333 sndbuf_getspd(struct snd_dbuf *b) 334 { 335 return b->spd; 336 } 337 338 void 339 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd) 340 { 341 b->spd = spd; 342 } 343 344 unsigned int 345 sndbuf_getalign(struct snd_dbuf *b) 346 { 347 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3}; 348 349 return align[b->bps - 1]; 350 } 351 352 unsigned int 353 sndbuf_getblkcnt(struct snd_dbuf *b) 354 { 355 return b->blkcnt; 356 } 357 358 void 359 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt) 360 { 361 b->blkcnt = blkcnt; 362 } 363 364 unsigned int 365 sndbuf_getblksz(struct snd_dbuf *b) 366 { 367 return b->blksz; 368 } 369 370 void 371 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz) 372 { 373 b->blksz = blksz; 374 } 375 376 unsigned int 377 sndbuf_getbps(struct snd_dbuf *b) 378 { 379 return b->bps; 380 } 381 382 void * 383 sndbuf_getbuf(struct snd_dbuf *b) 384 { 385 return b->buf; 386 } 387 388 void * 389 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs) 390 { 391 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs)); 392 393 return b->buf + ofs; 394 } 395 396 unsigned int 397 sndbuf_getsize(struct snd_dbuf *b) 398 { 399 return b->bufsize; 400 } 401 402 unsigned int 403 sndbuf_getmaxsize(struct snd_dbuf *b) 404 { 405 return b->maxsize; 406 } 407 408 unsigned int 409 sndbuf_runsz(struct snd_dbuf *b) 410 { 411 return b->dl; 412 } 413 414 void 415 sndbuf_setrun(struct snd_dbuf *b, int go) 416 { 417 b->dl = go? b->blksz : 0; 418 } 419 420 struct selinfo * 421 sndbuf_getsel(struct snd_dbuf *b) 422 { 423 return &b->sel; 424 } 425 426 /************************************************************/ 427 unsigned int 428 sndbuf_getxrun(struct snd_dbuf *b) 429 { 430 SNDBUF_LOCKASSERT(b); 431 432 return b->xrun; 433 } 434 435 void 436 sndbuf_setxrun(struct snd_dbuf *b, unsigned int xrun) 437 { 438 SNDBUF_LOCKASSERT(b); 439 440 b->xrun = xrun; 441 } 442 443 unsigned int 444 sndbuf_gethwptr(struct snd_dbuf *b) 445 { 446 SNDBUF_LOCKASSERT(b); 447 448 return b->hp; 449 } 450 451 void 452 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr) 453 { 454 SNDBUF_LOCKASSERT(b); 455 456 b->hp = ptr; 457 } 458 459 unsigned int 460 sndbuf_getready(struct snd_dbuf *b) 461 { 462 SNDBUF_LOCKASSERT(b); 463 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 464 465 return b->rl; 466 } 467 468 unsigned int 469 sndbuf_getreadyptr(struct snd_dbuf *b) 470 { 471 SNDBUF_LOCKASSERT(b); 472 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp)); 473 474 return b->rp; 475 } 476 477 unsigned int 478 sndbuf_getfree(struct snd_dbuf *b) 479 { 480 SNDBUF_LOCKASSERT(b); 481 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 482 483 return b->bufsize - b->rl; 484 } 485 486 unsigned int 487 sndbuf_getfreeptr(struct snd_dbuf *b) 488 { 489 SNDBUF_LOCKASSERT(b); 490 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp)); 491 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 492 493 return (b->rp + b->rl) % b->bufsize; 494 } 495 496 unsigned int 497 sndbuf_getblocks(struct snd_dbuf *b) 498 { 499 SNDBUF_LOCKASSERT(b); 500 501 return b->total / b->blksz; 502 } 503 504 unsigned int 505 sndbuf_getprevblocks(struct snd_dbuf *b) 506 { 507 SNDBUF_LOCKASSERT(b); 508 509 return b->prev_total / b->blksz; 510 } 511 512 unsigned int 513 sndbuf_gettotal(struct snd_dbuf *b) 514 { 515 SNDBUF_LOCKASSERT(b); 516 517 return b->total; 518 } 519 520 void 521 sndbuf_updateprevtotal(struct snd_dbuf *b) 522 { 523 SNDBUF_LOCKASSERT(b); 524 525 b->prev_total = b->total; 526 } 527 528 unsigned int 529 snd_xbytes(unsigned int v, unsigned int from, unsigned int to) 530 { 531 unsigned int w, x, y; 532 533 if (from == to) 534 return v; 535 536 if (from == 0 || to == 0 || v == 0) 537 return 0; 538 539 x = from; 540 y = to; 541 while (y != 0) { 542 w = x % y; 543 x = y; 544 y = w; 545 } 546 from /= x; 547 to /= x; 548 549 return (unsigned int)(((u_int64_t)v * to) / from); 550 } 551 552 unsigned int 553 sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to) 554 { 555 if (from == NULL || to == NULL || v == 0) 556 return 0; 557 558 return snd_xbytes(v, sndbuf_getbps(from) * sndbuf_getspd(from), 559 sndbuf_getbps(to) * sndbuf_getspd(to)); 560 } 561 562 u_int8_t 563 sndbuf_zerodata(u_int32_t fmt) 564 { 565 if (fmt & AFMT_SIGNED) 566 return (0x00); 567 else if (fmt & AFMT_MU_LAW) 568 return (0x7f); 569 else if (fmt & AFMT_A_LAW) 570 return (0x55); 571 return (0x80); 572 } 573 574 /************************************************************/ 575 576 /** 577 * @brief Acquire buffer space to extend ready area 578 * 579 * This function extends the ready area length by @c count bytes, and may 580 * optionally copy samples from another location stored in @c from. The 581 * counter @c snd_dbuf::total is also incremented by @c count bytes. 582 * 583 * @param b audio buffer 584 * @param from sample source (optional) 585 * @param count number of bytes to acquire 586 * 587 * @retval 0 Unconditional 588 */ 589 int 590 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count) 591 { 592 int l; 593 594 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > free %d", __func__, count, sndbuf_getfree(b))); 595 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 596 b->total += count; 597 if (from != NULL) { 598 while (count > 0) { 599 l = min(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b)); 600 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l); 601 from += l; 602 b->rl += l; 603 count -= l; 604 } 605 } else 606 b->rl += count; 607 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count)); 608 609 return 0; 610 } 611 612 /** 613 * @brief Dispose samples from channel buffer, increasing size of ready area 614 * 615 * This function discards samples from the supplied buffer by advancing the 616 * ready area start pointer and decrementing the ready area length. If 617 * @c to is not NULL, then the discard samples will be copied to the location 618 * it points to. 619 * 620 * @param b PCM channel sound buffer 621 * @param to destination buffer (optional) 622 * @param count number of bytes to discard 623 * 624 * @returns 0 unconditionally 625 */ 626 int 627 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count) 628 { 629 int l; 630 631 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b))); 632 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 633 if (to != NULL) { 634 while (count > 0) { 635 l = min(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b)); 636 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l); 637 to += l; 638 b->rl -= l; 639 b->rp = (b->rp + l) % b->bufsize; 640 count -= l; 641 } 642 } else { 643 b->rl -= count; 644 b->rp = (b->rp + count) % b->bufsize; 645 } 646 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count)); 647 648 return 0; 649 } 650 651 /* count is number of bytes we want added to destination buffer */ 652 int 653 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count) 654 { 655 unsigned int cnt; 656 657 KASSERT(count > 0, ("can't feed 0 bytes")); 658 659 if (sndbuf_getfree(to) < count) 660 return EINVAL; 661 662 do { 663 cnt = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from); 664 if (cnt) 665 sndbuf_acquire(to, to->tmpbuf, cnt); 666 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */ 667 count -= cnt; 668 } while (count && cnt); 669 670 return 0; 671 } 672 673 /************************************************************/ 674 675 void 676 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what) 677 { 678 printf("%s: [", s); 679 if (what & 0x01) 680 printf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize); 681 if (what & 0x02) 682 printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp); 683 if (what & 0x04) 684 printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun); 685 if (what & 0x08) 686 printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd); 687 if (what & 0x10) 688 printf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags); 689 printf(" ]\n"); 690 } 691 692 /************************************************************/ 693 u_int32_t 694 sndbuf_getflags(struct snd_dbuf *b) 695 { 696 return b->flags; 697 } 698 699 void 700 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on) 701 { 702 b->flags &= ~flags; 703 if (on) 704 b->flags |= flags; 705 } 706 707 /** 708 * @brief Clear the shadow buffer by filling with samples equal to zero. 709 * 710 * @param b buffer to clear 711 */ 712 void 713 sndbuf_clearshadow(struct snd_dbuf *b) 714 { 715 KASSERT(b != NULL, ("b is a null pointer")); 716 KASSERT(b->sl >= 0, ("illegal shadow length")); 717 718 if ((b->shadbuf != NULL) && (b->sl > 0)) 719 memset(b->shadbuf, sndbuf_zerodata(b->fmt), b->sl); 720 } 721 722 #ifdef OSSV4_EXPERIMENT 723 /** 724 * @brief Return peak value from samples in buffer ready area. 725 * 726 * Peak ranges from 0-32767. If channel is monaural, most significant 16 727 * bits will be zero. For now, only expects to work with 1-2 channel 728 * buffers. 729 * 730 * @note Currently only operates with linear PCM formats. 731 * 732 * @param b buffer to analyze 733 * @param lpeak pointer to store left peak value 734 * @param rpeak pointer to store right peak value 735 */ 736 void 737 sndbuf_getpeaks(struct snd_dbuf *b, int *lp, int *rp) 738 { 739 u_int32_t lpeak, rpeak; 740 741 lpeak = 0; 742 rpeak = 0; 743 744 /** 745 * @todo fill this in later 746 */ 747 } 748 #endif 749