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