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 * $FreeBSD: src/sys/dev/sound/pcm/buffer.c,v 1.25.2.3 2007/04/26 08:21:43 ariff Exp $ 27 * $DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.11 2008/01/06 16:55:51 swildner Exp $ 28 */ 29 30 #include <dev/sound/pcm/sound.h> 31 32 #include "feeder_if.h" 33 34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/buffer.c,v 1.11 2008/01/06 16:55:51 swildner Exp $"); 35 36 /* 37 * sndbuf_seltask is a taskqueue callback routine, called from 38 * taskqueue_swi, which runs under the MP lock. 39 * 40 * The only purpose is to be able to selwakeup() from a sound 41 * interrupt, which is running without MP lock held and thus 42 * can't call selwakeup() directly. 43 */ 44 static void 45 sndbuf_seltask(void *context, int pending) 46 { 47 struct snd_dbuf *b = context; 48 49 selwakeup(sndbuf_getsel(b)); 50 } 51 52 struct snd_dbuf * 53 sndbuf_create(device_t dev, char *drv, char *desc, struct pcm_channel *channel) 54 { 55 struct snd_dbuf *b; 56 57 b = kmalloc(sizeof(*b), M_DEVBUF, M_WAITOK | M_ZERO); 58 ksnprintf(b->name, SNDBUF_NAMELEN, "%s:%s", drv, desc); 59 b->dev = dev; 60 b->channel = channel; 61 TASK_INIT(&b->seltask, 0, sndbuf_seltask, b); 62 63 return b; 64 } 65 66 void 67 sndbuf_destroy(struct snd_dbuf *b) 68 { 69 sndbuf_free(b); 70 kfree(b, M_DEVBUF); 71 } 72 73 bus_addr_t 74 sndbuf_getbufaddr(struct snd_dbuf *buf) 75 { 76 return (buf->buf_addr); 77 } 78 79 static void 80 sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error) 81 { 82 struct snd_dbuf *b = (struct snd_dbuf *)arg; 83 84 if (bootverbose) { 85 device_printf(b->dev, "sndbuf_setmap %lx, %lx; ", 86 (u_long)segs[0].ds_addr, (u_long)segs[0].ds_len); 87 kprintf("%p -> %lx\n", b->buf, (u_long)segs[0].ds_addr); 88 } 89 if (error == 0) 90 b->buf_addr = segs[0].ds_addr; 91 else 92 b->buf_addr = 0; 93 } 94 95 /* 96 * Allocate memory for DMA buffer. If the device does not use DMA transfers, 97 * the driver can call kmalloc(9) and sndbuf_setup() itself. 98 */ 99 100 int 101 sndbuf_alloc(struct snd_dbuf *b, bus_dma_tag_t dmatag, unsigned int size) 102 { 103 int ret; 104 105 b->dmatag = dmatag; 106 b->maxsize = size; 107 b->bufsize = b->maxsize; 108 b->buf_addr = 0; 109 b->flags |= SNDBUF_F_MANAGED; 110 if (bus_dmamem_alloc(b->dmatag, (void **)&b->buf, BUS_DMA_NOWAIT, 111 &b->dmamap)) { 112 sndbuf_free(b); 113 return (ENOMEM); 114 } 115 if (bus_dmamap_load(b->dmatag, b->dmamap, b->buf, b->maxsize, 116 sndbuf_setmap, b, 0) != 0 || b->buf_addr == 0) { 117 sndbuf_free(b); 118 return (ENOMEM); 119 } 120 121 ret = sndbuf_resize(b, 2, b->maxsize / 2); 122 if (ret != 0) 123 sndbuf_free(b); 124 125 return (ret); 126 } 127 128 int 129 sndbuf_setup(struct snd_dbuf *b, void *buf, unsigned int size) 130 { 131 b->flags &= ~SNDBUF_F_MANAGED; 132 if (buf) 133 b->flags |= SNDBUF_F_MANAGED; 134 b->buf = buf; 135 b->maxsize = size; 136 b->bufsize = b->maxsize; 137 return sndbuf_resize(b, 2, b->maxsize / 2); 138 } 139 140 void 141 sndbuf_free(struct snd_dbuf *b) 142 { 143 if (b->tmpbuf) 144 kfree(b->tmpbuf, M_DEVBUF); 145 if (b->buf) { 146 if (b->flags & SNDBUF_F_MANAGED) { 147 if (b->dmamap) 148 bus_dmamap_unload(b->dmatag, b->dmamap); 149 if (b->dmatag) 150 bus_dmamem_free(b->dmatag, b->buf, b->dmamap); 151 } else 152 kfree(b->buf, M_DEVBUF); 153 } 154 155 b->tmpbuf = NULL; 156 b->buf = NULL; 157 b->dmatag = NULL; 158 b->dmamap = NULL; 159 } 160 161 int 162 sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) 163 { 164 u_int8_t *tmpbuf, *f2; 165 166 chn_lock(b->channel); 167 if (b->maxsize == 0) 168 goto out; 169 if (blkcnt == 0) 170 blkcnt = b->blkcnt; 171 if (blksz == 0) 172 blksz = b->blksz; 173 if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz > b->maxsize)) { 174 chn_unlock(b->channel); 175 return EINVAL; 176 } 177 if (blkcnt == b->blkcnt && blksz == b->blksz) 178 goto out; 179 180 chn_unlock(b->channel); 181 tmpbuf = kmalloc(blkcnt * blksz, M_DEVBUF, M_NOWAIT); 182 if (tmpbuf == NULL) 183 return ENOMEM; 184 chn_lock(b->channel); 185 b->blkcnt = blkcnt; 186 b->blksz = blksz; 187 b->bufsize = blkcnt * blksz; 188 f2 = b->tmpbuf; 189 b->tmpbuf = tmpbuf; 190 sndbuf_reset(b); 191 chn_unlock(b->channel); 192 if (f2 != NULL) 193 kfree(f2, M_DEVBUF); 194 return 0; 195 out: 196 chn_unlock(b->channel); 197 return 0; 198 } 199 200 int 201 sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) 202 { 203 u_int8_t *buf, *tmpbuf, *f1, *f2; 204 unsigned int bufsize; 205 int ret; 206 207 if (blkcnt < 2 || blksz < 16) 208 return EINVAL; 209 210 bufsize = blksz * blkcnt; 211 212 chn_unlock(b->channel); 213 buf = kmalloc(bufsize, M_DEVBUF, M_WAITOK); 214 tmpbuf = kmalloc(bufsize, M_DEVBUF, M_WAITOK); 215 chn_lock(b->channel); 216 217 b->blkcnt = blkcnt; 218 b->blksz = blksz; 219 b->bufsize = bufsize; 220 b->maxsize = bufsize; 221 f1 = b->buf; 222 f2 = b->tmpbuf; 223 b->buf = buf; 224 b->tmpbuf = tmpbuf; 225 226 sndbuf_reset(b); 227 228 chn_unlock(b->channel); 229 if (f1) 230 kfree(f1, M_DEVBUF); 231 if (f2) 232 kfree(f2, M_DEVBUF); 233 234 ret = 0; 235 chn_lock(b->channel); 236 return ret; 237 } 238 239 void 240 sndbuf_clear(struct snd_dbuf *b, unsigned int length) 241 { 242 int i; 243 u_char data, *p; 244 245 if (length == 0) 246 return; 247 if (length > b->bufsize) 248 length = b->bufsize; 249 250 if (b->fmt & AFMT_SIGNED) 251 data = 0x00; 252 else 253 data = 0x80; 254 255 i = sndbuf_getfreeptr(b); 256 p = sndbuf_getbuf(b); 257 while (length > 0) { 258 p[i] = data; 259 length--; 260 i++; 261 if (i >= b->bufsize) 262 i = 0; 263 } 264 } 265 266 void 267 sndbuf_fillsilence(struct snd_dbuf *b) 268 { 269 int i; 270 u_char data, *p; 271 272 if (b->fmt & AFMT_SIGNED) 273 data = 0x00; 274 else 275 data = 0x80; 276 277 i = 0; 278 p = sndbuf_getbuf(b); 279 while (i < b->bufsize) 280 p[i++] = data; 281 b->rp = 0; 282 b->rl = b->bufsize; 283 } 284 285 void 286 sndbuf_reset(struct snd_dbuf *b) 287 { 288 b->hp = 0; 289 b->rp = 0; 290 b->rl = 0; 291 b->dl = 0; 292 b->prev_total = 0; 293 b->total = 0; 294 b->xrun = 0; 295 if (b->buf && b->bufsize > 0) 296 sndbuf_clear(b, b->bufsize); 297 } 298 299 u_int32_t 300 sndbuf_getfmt(struct snd_dbuf *b) 301 { 302 return b->fmt; 303 } 304 305 int 306 sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt) 307 { 308 b->fmt = fmt; 309 b->bps = 1; 310 b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0; 311 if (b->fmt & AFMT_16BIT) 312 b->bps <<= 1; 313 else if (b->fmt & AFMT_24BIT) 314 b->bps *= 3; 315 else if (b->fmt & AFMT_32BIT) 316 b->bps <<= 2; 317 return 0; 318 } 319 320 unsigned int 321 sndbuf_getspd(struct snd_dbuf *b) 322 { 323 return b->spd; 324 } 325 326 void 327 sndbuf_setspd(struct snd_dbuf *b, unsigned int spd) 328 { 329 b->spd = spd; 330 } 331 332 unsigned int 333 sndbuf_getalign(struct snd_dbuf *b) 334 { 335 static int align[] = {0, 1, 1, 2, 2, 2, 2, 3}; 336 337 return align[b->bps - 1]; 338 } 339 340 unsigned int 341 sndbuf_getblkcnt(struct snd_dbuf *b) 342 { 343 return b->blkcnt; 344 } 345 346 void 347 sndbuf_setblkcnt(struct snd_dbuf *b, unsigned int blkcnt) 348 { 349 b->blkcnt = blkcnt; 350 } 351 352 unsigned int 353 sndbuf_getblksz(struct snd_dbuf *b) 354 { 355 return b->blksz; 356 } 357 358 void 359 sndbuf_setblksz(struct snd_dbuf *b, unsigned int blksz) 360 { 361 b->blksz = blksz; 362 } 363 364 unsigned int 365 sndbuf_getbps(struct snd_dbuf *b) 366 { 367 return b->bps; 368 } 369 370 void * 371 sndbuf_getbuf(struct snd_dbuf *b) 372 { 373 return b->buf; 374 } 375 376 void * 377 sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs) 378 { 379 KASSERT(ofs < b->bufsize, ("%s: ofs invalid %d", __func__, ofs)); 380 381 return b->buf + ofs; 382 } 383 384 unsigned int 385 sndbuf_getsize(struct snd_dbuf *b) 386 { 387 return b->bufsize; 388 } 389 390 unsigned int 391 sndbuf_getmaxsize(struct snd_dbuf *b) 392 { 393 return b->maxsize; 394 } 395 396 unsigned int 397 sndbuf_runsz(struct snd_dbuf *b) 398 { 399 return b->dl; 400 } 401 402 void 403 sndbuf_setrun(struct snd_dbuf *b, int go) 404 { 405 b->dl = go? b->blksz : 0; 406 } 407 408 struct selinfo * 409 sndbuf_getsel(struct snd_dbuf *b) 410 { 411 return &b->sel; 412 } 413 414 /************************************************************/ 415 unsigned int 416 sndbuf_getxrun(struct snd_dbuf *b) 417 { 418 SNDBUF_LOCKASSERT(b); 419 420 return b->xrun; 421 } 422 423 void 424 sndbuf_setxrun(struct snd_dbuf *b, unsigned int cnt) 425 { 426 SNDBUF_LOCKASSERT(b); 427 428 b->xrun = cnt; 429 } 430 431 unsigned int 432 sndbuf_gethwptr(struct snd_dbuf *b) 433 { 434 SNDBUF_LOCKASSERT(b); 435 436 return b->hp; 437 } 438 439 void 440 sndbuf_sethwptr(struct snd_dbuf *b, unsigned int ptr) 441 { 442 SNDBUF_LOCKASSERT(b); 443 444 b->hp = ptr; 445 } 446 447 unsigned int 448 sndbuf_getready(struct snd_dbuf *b) 449 { 450 SNDBUF_LOCKASSERT(b); 451 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 452 453 return b->rl; 454 } 455 456 unsigned int 457 sndbuf_getreadyptr(struct snd_dbuf *b) 458 { 459 SNDBUF_LOCKASSERT(b); 460 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp)); 461 462 return b->rp; 463 } 464 465 unsigned int 466 sndbuf_getfree(struct snd_dbuf *b) 467 { 468 SNDBUF_LOCKASSERT(b); 469 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 470 471 return b->bufsize - b->rl; 472 } 473 474 unsigned int 475 sndbuf_getfreeptr(struct snd_dbuf *b) 476 { 477 SNDBUF_LOCKASSERT(b); 478 KASSERT((b->rp >= 0) && (b->rp <= b->bufsize), ("%s: b->rp invalid %d", __func__, b->rp)); 479 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 480 481 return (b->rp + b->rl) % b->bufsize; 482 } 483 484 unsigned int 485 sndbuf_getblocks(struct snd_dbuf *b) 486 { 487 SNDBUF_LOCKASSERT(b); 488 489 return b->total / b->blksz; 490 } 491 492 unsigned int 493 sndbuf_getprevblocks(struct snd_dbuf *b) 494 { 495 SNDBUF_LOCKASSERT(b); 496 497 return b->prev_total / b->blksz; 498 } 499 500 unsigned int 501 sndbuf_gettotal(struct snd_dbuf *b) 502 { 503 SNDBUF_LOCKASSERT(b); 504 505 return b->total; 506 } 507 508 void 509 sndbuf_updateprevtotal(struct snd_dbuf *b) 510 { 511 SNDBUF_LOCKASSERT(b); 512 513 b->prev_total = b->total; 514 } 515 516 /************************************************************/ 517 518 int 519 sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count) 520 { 521 int l; 522 523 KASSERT(count <= sndbuf_getfree(b), ("%s: count %d > kfree %d", __func__, count, sndbuf_getfree(b))); 524 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 525 b->total += count; 526 if (from != NULL) { 527 while (count > 0) { 528 l = MIN(count, sndbuf_getsize(b) - sndbuf_getfreeptr(b)); 529 bcopy(from, sndbuf_getbufofs(b, sndbuf_getfreeptr(b)), l); 530 from += l; 531 b->rl += l; 532 count -= l; 533 } 534 } else 535 b->rl += count; 536 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count)); 537 538 return 0; 539 } 540 541 int 542 sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count) 543 { 544 int l; 545 546 KASSERT(count <= sndbuf_getready(b), ("%s: count %d > ready %d", __func__, count, sndbuf_getready(b))); 547 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d", __func__, b->rl)); 548 if (to != NULL) { 549 while (count > 0) { 550 l = MIN(count, sndbuf_getsize(b) - sndbuf_getreadyptr(b)); 551 bcopy(sndbuf_getbufofs(b, sndbuf_getreadyptr(b)), to, l); 552 to += l; 553 b->rl -= l; 554 b->rp = (b->rp + l) % b->bufsize; 555 count -= l; 556 } 557 } else { 558 b->rl -= count; 559 b->rp = (b->rp + count) % b->bufsize; 560 } 561 KASSERT((b->rl >= 0) && (b->rl <= b->bufsize), ("%s: b->rl invalid %d, count %d", __func__, b->rl, count)); 562 563 return 0; 564 } 565 566 /* count is number of bytes we want added to destination buffer */ 567 int 568 sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count) 569 { 570 KASSERT(count > 0, ("can't feed 0 bytes")); 571 572 if (sndbuf_getfree(to) < count) 573 return EINVAL; 574 575 count = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from); 576 if (count) 577 sndbuf_acquire(to, to->tmpbuf, count); 578 /* the root feeder has called sndbuf_dispose(from, , bytes fetched) */ 579 580 return 0; 581 } 582 583 /************************************************************/ 584 585 void 586 sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what) 587 { 588 kprintf("%s: [", s); 589 if (what & 0x01) 590 kprintf(" bufsize: %d, maxsize: %d", b->bufsize, b->maxsize); 591 if (what & 0x02) 592 kprintf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp); 593 if (what & 0x04) 594 kprintf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun); 595 if (what & 0x08) 596 kprintf(" fmt: 0x%x, spd: %d", b->fmt, b->spd); 597 if (what & 0x10) 598 kprintf(" blksz: %d, blkcnt: %d, flags: 0x%x", b->blksz, b->blkcnt, b->flags); 599 kprintf(" ]\n"); 600 } 601 602 /************************************************************/ 603 u_int32_t 604 sndbuf_getflags(struct snd_dbuf *b) 605 { 606 return b->flags; 607 } 608 609 void 610 sndbuf_setflags(struct snd_dbuf *b, u_int32_t flags, int on) 611 { 612 b->flags &= ~flags; 613 if (on) 614 b->flags |= flags; 615 } 616