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