1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org> 5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006 6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org> 7 * Portions Copyright (c) Luigi Rizzo <luigi@FreeBSD.org> - 1997-99 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifdef HAVE_KERNEL_OPTION_HEADERS 33 #include "opt_snd.h" 34 #endif 35 36 #include <dev/sound/pcm/sound.h> 37 #include <dev/sound/pcm/vchan.h> 38 39 #include "feeder_if.h" 40 41 int report_soft_formats = 1; 42 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW, 43 &report_soft_formats, 0, "report software-emulated formats"); 44 45 int report_soft_matrix = 1; 46 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_matrix, CTLFLAG_RW, 47 &report_soft_matrix, 0, "report software-emulated channel matrixing"); 48 49 int chn_latency = CHN_LATENCY_DEFAULT; 50 51 static int 52 sysctl_hw_snd_latency(SYSCTL_HANDLER_ARGS) 53 { 54 int err, val; 55 56 val = chn_latency; 57 err = sysctl_handle_int(oidp, &val, 0, req); 58 if (err != 0 || req->newptr == NULL) 59 return err; 60 if (val < CHN_LATENCY_MIN || val > CHN_LATENCY_MAX) 61 err = EINVAL; 62 else 63 chn_latency = val; 64 65 return err; 66 } 67 SYSCTL_PROC(_hw_snd, OID_AUTO, latency, 68 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), 69 sysctl_hw_snd_latency, "I", 70 "buffering latency (0=low ... 10=high)"); 71 72 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT; 73 74 static int 75 sysctl_hw_snd_latency_profile(SYSCTL_HANDLER_ARGS) 76 { 77 int err, val; 78 79 val = chn_latency_profile; 80 err = sysctl_handle_int(oidp, &val, 0, req); 81 if (err != 0 || req->newptr == NULL) 82 return err; 83 if (val < CHN_LATENCY_PROFILE_MIN || val > CHN_LATENCY_PROFILE_MAX) 84 err = EINVAL; 85 else 86 chn_latency_profile = val; 87 88 return err; 89 } 90 SYSCTL_PROC(_hw_snd, OID_AUTO, latency_profile, 91 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), 92 sysctl_hw_snd_latency_profile, "I", 93 "buffering latency profile (0=aggressive 1=safe)"); 94 95 static int chn_timeout = CHN_TIMEOUT; 96 97 static int 98 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS) 99 { 100 int err, val; 101 102 val = chn_timeout; 103 err = sysctl_handle_int(oidp, &val, 0, req); 104 if (err != 0 || req->newptr == NULL) 105 return err; 106 if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX) 107 err = EINVAL; 108 else 109 chn_timeout = val; 110 111 return err; 112 } 113 SYSCTL_PROC(_hw_snd, OID_AUTO, timeout, 114 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 0, sizeof(int), 115 sysctl_hw_snd_timeout, "I", 116 "interrupt timeout (1 - 10) seconds"); 117 118 static int chn_vpc_autoreset = 1; 119 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_autoreset, CTLFLAG_RWTUN, 120 &chn_vpc_autoreset, 0, "automatically reset channels volume to 0db"); 121 122 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM; 123 124 static void 125 chn_vpc_proc(int reset, int db) 126 { 127 struct snddev_info *d; 128 struct pcm_channel *c; 129 int i; 130 131 for (i = 0; pcm_devclass != NULL && 132 i < devclass_get_maxunit(pcm_devclass); i++) { 133 d = devclass_get_softc(pcm_devclass, i); 134 if (!PCM_REGISTERED(d)) 135 continue; 136 PCM_LOCK(d); 137 PCM_WAIT(d); 138 PCM_ACQUIRE(d); 139 CHN_FOREACH(c, d, channels.pcm) { 140 CHN_LOCK(c); 141 CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db); 142 if (reset != 0) 143 chn_vpc_reset(c, SND_VOL_C_PCM, 1); 144 CHN_UNLOCK(c); 145 } 146 PCM_RELEASE(d); 147 PCM_UNLOCK(d); 148 } 149 } 150 151 static int 152 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS) 153 { 154 int err, val; 155 156 val = chn_vol_0db_pcm; 157 err = sysctl_handle_int(oidp, &val, 0, req); 158 if (err != 0 || req->newptr == NULL) 159 return (err); 160 if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX) 161 return (EINVAL); 162 163 chn_vol_0db_pcm = val; 164 chn_vpc_proc(0, val); 165 166 return (0); 167 } 168 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_0db, 169 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int), 170 sysctl_hw_snd_vpc_0db, "I", 171 "0db relative level"); 172 173 static int 174 sysctl_hw_snd_vpc_reset(SYSCTL_HANDLER_ARGS) 175 { 176 int err, val; 177 178 val = 0; 179 err = sysctl_handle_int(oidp, &val, 0, req); 180 if (err != 0 || req->newptr == NULL || val == 0) 181 return (err); 182 183 chn_vol_0db_pcm = SND_VOL_0DB_PCM; 184 chn_vpc_proc(1, SND_VOL_0DB_PCM); 185 186 return (0); 187 } 188 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_reset, 189 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(int), 190 sysctl_hw_snd_vpc_reset, "I", 191 "reset volume on all channels"); 192 193 static int chn_usefrags = 0; 194 static int chn_syncdelay = -1; 195 196 SYSCTL_INT(_hw_snd, OID_AUTO, usefrags, CTLFLAG_RWTUN, 197 &chn_usefrags, 0, "prefer setfragments() over setblocksize()"); 198 SYSCTL_INT(_hw_snd, OID_AUTO, syncdelay, CTLFLAG_RWTUN, 199 &chn_syncdelay, 0, 200 "append (0-1000) millisecond trailing buffer delay on each sync"); 201 202 /** 203 * @brief Channel sync group lock 204 * 205 * Clients should acquire this lock @b without holding any channel locks 206 * before touching syncgroups or the main syncgroup list. 207 */ 208 struct mtx snd_pcm_syncgroups_mtx; 209 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF); 210 /** 211 * @brief syncgroups' master list 212 * 213 * Each time a channel syncgroup is created, it's added to this list. This 214 * list should only be accessed with @sa snd_pcm_syncgroups_mtx held. 215 * 216 * See SNDCTL_DSP_SYNCGROUP for more information. 217 */ 218 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups); 219 220 static void 221 chn_lockinit(struct pcm_channel *c, int dir) 222 { 223 switch (dir) { 224 case PCMDIR_PLAY: 225 c->lock = snd_mtxcreate(c->name, "pcm play channel"); 226 cv_init(&c->intr_cv, "pcmwr"); 227 break; 228 case PCMDIR_PLAY_VIRTUAL: 229 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel"); 230 cv_init(&c->intr_cv, "pcmwrv"); 231 break; 232 case PCMDIR_REC: 233 c->lock = snd_mtxcreate(c->name, "pcm record channel"); 234 cv_init(&c->intr_cv, "pcmrd"); 235 break; 236 case PCMDIR_REC_VIRTUAL: 237 c->lock = snd_mtxcreate(c->name, "pcm virtual record channel"); 238 cv_init(&c->intr_cv, "pcmrdv"); 239 break; 240 default: 241 panic("%s(): Invalid direction=%d", __func__, dir); 242 break; 243 } 244 245 cv_init(&c->cv, "pcmchn"); 246 } 247 248 static void 249 chn_lockdestroy(struct pcm_channel *c) 250 { 251 CHN_LOCKASSERT(c); 252 253 CHN_BROADCAST(&c->cv); 254 CHN_BROADCAST(&c->intr_cv); 255 256 cv_destroy(&c->cv); 257 cv_destroy(&c->intr_cv); 258 259 snd_mtxfree(c->lock); 260 } 261 262 /** 263 * @brief Determine channel is ready for I/O 264 * 265 * @retval 1 = ready for I/O 266 * @retval 0 = not ready for I/O 267 */ 268 static int 269 chn_polltrigger(struct pcm_channel *c) 270 { 271 struct snd_dbuf *bs = c->bufsoft; 272 u_int delta; 273 274 CHN_LOCKASSERT(c); 275 276 if (c->flags & CHN_F_MMAP) { 277 if (sndbuf_getprevtotal(bs) < c->lw) 278 delta = c->lw; 279 else 280 delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs); 281 } else { 282 if (c->direction == PCMDIR_PLAY) 283 delta = sndbuf_getfree(bs); 284 else 285 delta = sndbuf_getready(bs); 286 } 287 288 return ((delta < c->lw) ? 0 : 1); 289 } 290 291 static void 292 chn_pollreset(struct pcm_channel *c) 293 { 294 295 CHN_LOCKASSERT(c); 296 sndbuf_updateprevtotal(c->bufsoft); 297 } 298 299 static void 300 chn_wakeup(struct pcm_channel *c) 301 { 302 struct snd_dbuf *bs; 303 struct pcm_channel *ch; 304 305 CHN_LOCKASSERT(c); 306 307 bs = c->bufsoft; 308 309 if (CHN_EMPTY(c, children.busy)) { 310 if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c)) 311 selwakeuppri(sndbuf_getsel(bs), PRIBIO); 312 CHN_BROADCAST(&c->intr_cv); 313 } else { 314 CHN_FOREACH(ch, c, children.busy) { 315 CHN_LOCK(ch); 316 chn_wakeup(ch); 317 CHN_UNLOCK(ch); 318 } 319 } 320 } 321 322 static int 323 chn_sleep(struct pcm_channel *c, int timeout) 324 { 325 int ret; 326 327 CHN_LOCKASSERT(c); 328 329 if (c->flags & CHN_F_DEAD) 330 return (EINVAL); 331 332 c->sleeping++; 333 ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout); 334 c->sleeping--; 335 336 return ((c->flags & CHN_F_DEAD) ? EINVAL : ret); 337 } 338 339 /* 340 * chn_dmaupdate() tracks the status of a dma transfer, 341 * updating pointers. 342 */ 343 344 static unsigned int 345 chn_dmaupdate(struct pcm_channel *c) 346 { 347 struct snd_dbuf *b = c->bufhard; 348 unsigned int delta, old, hwptr, amt; 349 350 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0")); 351 CHN_LOCKASSERT(c); 352 353 old = sndbuf_gethwptr(b); 354 hwptr = chn_getptr(c); 355 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b); 356 sndbuf_sethwptr(b, hwptr); 357 358 if (c->direction == PCMDIR_PLAY) { 359 amt = min(delta, sndbuf_getready(b)); 360 amt -= amt % sndbuf_getalign(b); 361 if (amt > 0) 362 sndbuf_dispose(b, NULL, amt); 363 } else { 364 amt = min(delta, sndbuf_getfree(b)); 365 amt -= amt % sndbuf_getalign(b); 366 if (amt > 0) 367 sndbuf_acquire(b, NULL, amt); 368 } 369 if (snd_verbose > 3 && CHN_STARTED(c) && delta == 0) { 370 device_printf(c->dev, "WARNING: %s DMA completion " 371 "too fast/slow ! hwptr=%u, old=%u " 372 "delta=%u amt=%u ready=%u free=%u\n", 373 CHN_DIRSTR(c), hwptr, old, delta, amt, 374 sndbuf_getready(b), sndbuf_getfree(b)); 375 } 376 377 return delta; 378 } 379 380 static void 381 chn_wrfeed(struct pcm_channel *c) 382 { 383 struct snd_dbuf *b = c->bufhard; 384 struct snd_dbuf *bs = c->bufsoft; 385 unsigned int amt, want, wasfree; 386 387 CHN_LOCKASSERT(c); 388 389 if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING)) 390 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs)); 391 392 wasfree = sndbuf_getfree(b); 393 want = min(sndbuf_getsize(b), 394 imax(0, sndbuf_xbytes(sndbuf_getsize(bs), bs, b) - 395 sndbuf_getready(b))); 396 amt = min(wasfree, want); 397 if (amt > 0) 398 sndbuf_feed(bs, b, c, c->feeder, amt); 399 400 /* 401 * Possible xruns. There should be no empty space left in buffer. 402 */ 403 if (sndbuf_getready(b) < want) 404 c->xruns++; 405 406 if (sndbuf_getfree(b) < wasfree) 407 chn_wakeup(c); 408 } 409 410 static void 411 chn_wrintr(struct pcm_channel *c) 412 { 413 414 CHN_LOCKASSERT(c); 415 /* update pointers in primary buffer */ 416 chn_dmaupdate(c); 417 /* ...and feed from secondary to primary */ 418 chn_wrfeed(c); 419 /* tell the driver we've updated the primary buffer */ 420 chn_trigger(c, PCMTRIG_EMLDMAWR); 421 } 422 423 /* 424 * user write routine - uiomove data into secondary buffer, trigger if necessary 425 * if blocking, sleep, rinse and repeat. 426 * 427 * called externally, so must handle locking 428 */ 429 430 int 431 chn_write(struct pcm_channel *c, struct uio *buf) 432 { 433 struct snd_dbuf *bs = c->bufsoft; 434 void *off; 435 int ret, timeout, sz, t, p; 436 437 CHN_LOCKASSERT(c); 438 439 ret = 0; 440 timeout = chn_timeout * hz; 441 442 while (ret == 0 && buf->uio_resid > 0) { 443 sz = min(buf->uio_resid, sndbuf_getfree(bs)); 444 if (sz > 0) { 445 /* 446 * The following assumes that the free space in 447 * the buffer can never be less around the 448 * unlock-uiomove-lock sequence. 449 */ 450 while (ret == 0 && sz > 0) { 451 p = sndbuf_getfreeptr(bs); 452 t = min(sz, sndbuf_getsize(bs) - p); 453 off = sndbuf_getbufofs(bs, p); 454 CHN_UNLOCK(c); 455 ret = uiomove(off, t, buf); 456 CHN_LOCK(c); 457 sz -= t; 458 sndbuf_acquire(bs, NULL, t); 459 } 460 ret = 0; 461 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) { 462 ret = chn_start(c, 0); 463 if (ret != 0) 464 c->flags |= CHN_F_DEAD; 465 } 466 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) { 467 /** 468 * @todo Evaluate whether EAGAIN is truly desirable. 469 * 4Front drivers behave like this, but I'm 470 * not sure if it at all violates the "write 471 * should be allowed to block" model. 472 * 473 * The idea is that, while set with CHN_F_NOTRIGGER, 474 * a channel isn't playing, *but* without this we 475 * end up with "interrupt timeout / channel dead". 476 */ 477 ret = EAGAIN; 478 } else { 479 ret = chn_sleep(c, timeout); 480 if (ret == EAGAIN) { 481 ret = EINVAL; 482 c->flags |= CHN_F_DEAD; 483 device_printf(c->dev, "%s(): %s: " 484 "play interrupt timeout, channel dead\n", 485 __func__, c->name); 486 } else if (ret == ERESTART || ret == EINTR) 487 c->flags |= CHN_F_ABORTING; 488 } 489 } 490 491 return (ret); 492 } 493 494 /* 495 * Feed new data from the read buffer. Can be called in the bottom half. 496 */ 497 static void 498 chn_rdfeed(struct pcm_channel *c) 499 { 500 struct snd_dbuf *b = c->bufhard; 501 struct snd_dbuf *bs = c->bufsoft; 502 unsigned int amt; 503 504 CHN_LOCKASSERT(c); 505 506 if (c->flags & CHN_F_MMAP) 507 sndbuf_dispose(bs, NULL, sndbuf_getready(bs)); 508 509 amt = sndbuf_getfree(bs); 510 if (amt > 0) 511 sndbuf_feed(b, bs, c, c->feeder, amt); 512 513 amt = sndbuf_getready(b); 514 if (amt > 0) { 515 c->xruns++; 516 sndbuf_dispose(b, NULL, amt); 517 } 518 519 if (sndbuf_getready(bs) > 0) 520 chn_wakeup(c); 521 } 522 523 /* read interrupt routine. Must be called with interrupts blocked. */ 524 static void 525 chn_rdintr(struct pcm_channel *c) 526 { 527 528 CHN_LOCKASSERT(c); 529 /* tell the driver to update the primary buffer if non-dma */ 530 chn_trigger(c, PCMTRIG_EMLDMARD); 531 /* update pointers in primary buffer */ 532 chn_dmaupdate(c); 533 /* ...and feed from primary to secondary */ 534 chn_rdfeed(c); 535 } 536 537 /* 538 * user read routine - trigger if necessary, uiomove data from secondary buffer 539 * if blocking, sleep, rinse and repeat. 540 * 541 * called externally, so must handle locking 542 */ 543 544 int 545 chn_read(struct pcm_channel *c, struct uio *buf) 546 { 547 struct snd_dbuf *bs = c->bufsoft; 548 void *off; 549 int ret, timeout, sz, t, p; 550 551 CHN_LOCKASSERT(c); 552 553 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) { 554 ret = chn_start(c, 0); 555 if (ret != 0) { 556 c->flags |= CHN_F_DEAD; 557 return (ret); 558 } 559 } 560 561 ret = 0; 562 timeout = chn_timeout * hz; 563 564 while (ret == 0 && buf->uio_resid > 0) { 565 sz = min(buf->uio_resid, sndbuf_getready(bs)); 566 if (sz > 0) { 567 /* 568 * The following assumes that the free space in 569 * the buffer can never be less around the 570 * unlock-uiomove-lock sequence. 571 */ 572 while (ret == 0 && sz > 0) { 573 p = sndbuf_getreadyptr(bs); 574 t = min(sz, sndbuf_getsize(bs) - p); 575 off = sndbuf_getbufofs(bs, p); 576 CHN_UNLOCK(c); 577 ret = uiomove(off, t, buf); 578 CHN_LOCK(c); 579 sz -= t; 580 sndbuf_dispose(bs, NULL, t); 581 } 582 ret = 0; 583 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) 584 ret = EAGAIN; 585 else { 586 ret = chn_sleep(c, timeout); 587 if (ret == EAGAIN) { 588 ret = EINVAL; 589 c->flags |= CHN_F_DEAD; 590 device_printf(c->dev, "%s(): %s: " 591 "record interrupt timeout, channel dead\n", 592 __func__, c->name); 593 } else if (ret == ERESTART || ret == EINTR) 594 c->flags |= CHN_F_ABORTING; 595 } 596 } 597 598 return (ret); 599 } 600 601 void 602 chn_intr_locked(struct pcm_channel *c) 603 { 604 605 CHN_LOCKASSERT(c); 606 607 c->interrupts++; 608 609 if (c->direction == PCMDIR_PLAY) 610 chn_wrintr(c); 611 else 612 chn_rdintr(c); 613 } 614 615 void 616 chn_intr(struct pcm_channel *c) 617 { 618 619 if (CHN_LOCKOWNED(c)) { 620 chn_intr_locked(c); 621 return; 622 } 623 624 CHN_LOCK(c); 625 chn_intr_locked(c); 626 CHN_UNLOCK(c); 627 } 628 629 u_int32_t 630 chn_start(struct pcm_channel *c, int force) 631 { 632 u_int32_t i, j; 633 struct snd_dbuf *b = c->bufhard; 634 struct snd_dbuf *bs = c->bufsoft; 635 int err; 636 637 CHN_LOCKASSERT(c); 638 /* if we're running, or if we're prevented from triggering, bail */ 639 if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force)) 640 return (EINVAL); 641 642 err = 0; 643 644 if (force) { 645 i = 1; 646 j = 0; 647 } else { 648 if (c->direction == PCMDIR_REC) { 649 i = sndbuf_getfree(bs); 650 j = (i > 0) ? 1 : sndbuf_getready(b); 651 } else { 652 if (sndbuf_getfree(bs) == 0) { 653 i = 1; 654 j = 0; 655 } else { 656 struct snd_dbuf *pb; 657 658 pb = CHN_BUF_PARENT(c, b); 659 i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb); 660 j = sndbuf_getalign(pb); 661 } 662 } 663 if (snd_verbose > 3 && CHN_EMPTY(c, children)) 664 device_printf(c->dev, "%s(): %s (%s) threshold " 665 "i=%d j=%d\n", __func__, CHN_DIRSTR(c), 666 (c->flags & CHN_F_VIRTUAL) ? "virtual" : 667 "hardware", i, j); 668 } 669 670 if (i >= j) { 671 c->flags |= CHN_F_TRIGGERED; 672 sndbuf_setrun(b, 1); 673 if (c->flags & CHN_F_CLOSING) 674 c->feedcount = 2; 675 else { 676 c->feedcount = 0; 677 c->interrupts = 0; 678 c->xruns = 0; 679 } 680 if (c->parentchannel == NULL) { 681 if (c->direction == PCMDIR_PLAY) 682 sndbuf_fillsilence_rl(b, 683 sndbuf_xbytes(sndbuf_getsize(bs), bs, b)); 684 if (snd_verbose > 3) 685 device_printf(c->dev, 686 "%s(): %s starting! (%s/%s) " 687 "(ready=%d force=%d i=%d j=%d " 688 "intrtimeout=%u latency=%dms)\n", 689 __func__, 690 (c->flags & CHN_F_HAS_VCHAN) ? 691 "VCHAN PARENT" : "HW", CHN_DIRSTR(c), 692 (c->flags & CHN_F_CLOSING) ? "closing" : 693 "running", 694 sndbuf_getready(b), 695 force, i, j, c->timeout, 696 (sndbuf_getsize(b) * 1000) / 697 (sndbuf_getalign(b) * sndbuf_getspd(b))); 698 } 699 err = chn_trigger(c, PCMTRIG_START); 700 } 701 702 return (err); 703 } 704 705 void 706 chn_resetbuf(struct pcm_channel *c) 707 { 708 struct snd_dbuf *b = c->bufhard; 709 struct snd_dbuf *bs = c->bufsoft; 710 711 c->blocks = 0; 712 sndbuf_reset(b); 713 sndbuf_reset(bs); 714 } 715 716 /* 717 * chn_sync waits until the space in the given channel goes above 718 * a threshold. The threshold is checked against fl or rl respectively. 719 * Assume that the condition can become true, do not check here... 720 */ 721 int 722 chn_sync(struct pcm_channel *c, int threshold) 723 { 724 struct snd_dbuf *b, *bs; 725 int ret, count, hcount, minflush, resid, residp, syncdelay, blksz; 726 u_int32_t cflag; 727 728 CHN_LOCKASSERT(c); 729 730 if (c->direction != PCMDIR_PLAY) 731 return (EINVAL); 732 733 bs = c->bufsoft; 734 735 if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) || 736 (threshold < 1 && sndbuf_getready(bs) < 1)) 737 return (0); 738 739 /* if we haven't yet started and nothing is buffered, else start*/ 740 if (CHN_STOPPED(c)) { 741 if (threshold > 0 || sndbuf_getready(bs) > 0) { 742 ret = chn_start(c, 1); 743 if (ret != 0) 744 return (ret); 745 } else 746 return (0); 747 } 748 749 b = CHN_BUF_PARENT(c, c->bufhard); 750 751 minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs); 752 753 syncdelay = chn_syncdelay; 754 755 if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0)) 756 minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs); 757 758 /* 759 * Append (0-1000) millisecond trailing buffer (if needed) 760 * for slower / high latency hardwares (notably USB audio) 761 * to avoid audible truncation. 762 */ 763 if (syncdelay > 0) 764 minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) * 765 ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000; 766 767 minflush -= minflush % sndbuf_getalign(bs); 768 769 if (minflush > 0) { 770 threshold = min(minflush, sndbuf_getfree(bs)); 771 sndbuf_clear(bs, threshold); 772 sndbuf_acquire(bs, NULL, threshold); 773 minflush -= threshold; 774 } 775 776 resid = sndbuf_getready(bs); 777 residp = resid; 778 blksz = sndbuf_getblksz(b); 779 if (blksz < 1) { 780 device_printf(c->dev, 781 "%s(): WARNING: blksz < 1 ! maxsize=%d [%d/%d/%d]\n", 782 __func__, sndbuf_getmaxsize(b), sndbuf_getsize(b), 783 sndbuf_getblksz(b), sndbuf_getblkcnt(b)); 784 if (sndbuf_getblkcnt(b) > 0) 785 blksz = sndbuf_getsize(b) / sndbuf_getblkcnt(b); 786 if (blksz < 1) 787 blksz = 1; 788 } 789 count = sndbuf_xbytes(minflush + resid, bs, b) / blksz; 790 hcount = count; 791 ret = 0; 792 793 if (snd_verbose > 3) 794 device_printf(c->dev, "%s(): [begin] timeout=%d count=%d " 795 "minflush=%d resid=%d\n", __func__, c->timeout, count, 796 minflush, resid); 797 798 cflag = c->flags & CHN_F_CLOSING; 799 c->flags |= CHN_F_CLOSING; 800 while (count > 0 && (resid > 0 || minflush > 0)) { 801 ret = chn_sleep(c, c->timeout); 802 if (ret == ERESTART || ret == EINTR) { 803 c->flags |= CHN_F_ABORTING; 804 break; 805 } else if (ret == 0 || ret == EAGAIN) { 806 resid = sndbuf_getready(bs); 807 if (resid == residp) { 808 --count; 809 if (snd_verbose > 3) 810 device_printf(c->dev, 811 "%s(): [stalled] timeout=%d " 812 "count=%d hcount=%d " 813 "resid=%d minflush=%d\n", 814 __func__, c->timeout, count, 815 hcount, resid, minflush); 816 } else if (resid < residp && count < hcount) { 817 ++count; 818 if (snd_verbose > 3) 819 device_printf(c->dev, 820 "%s((): [resume] timeout=%d " 821 "count=%d hcount=%d " 822 "resid=%d minflush=%d\n", 823 __func__, c->timeout, count, 824 hcount, resid, minflush); 825 } 826 if (minflush > 0 && sndbuf_getfree(bs) > 0) { 827 threshold = min(minflush, 828 sndbuf_getfree(bs)); 829 sndbuf_clear(bs, threshold); 830 sndbuf_acquire(bs, NULL, threshold); 831 resid = sndbuf_getready(bs); 832 minflush -= threshold; 833 } 834 residp = resid; 835 } else 836 break; 837 } 838 c->flags &= ~CHN_F_CLOSING; 839 c->flags |= cflag; 840 841 if (snd_verbose > 3) 842 device_printf(c->dev, 843 "%s(): timeout=%d count=%d hcount=%d resid=%d residp=%d " 844 "minflush=%d ret=%d\n", 845 __func__, c->timeout, count, hcount, resid, residp, 846 minflush, ret); 847 848 return (0); 849 } 850 851 /* called externally, handle locking */ 852 int 853 chn_poll(struct pcm_channel *c, int ev, struct thread *td) 854 { 855 struct snd_dbuf *bs = c->bufsoft; 856 int ret; 857 858 CHN_LOCKASSERT(c); 859 860 if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) { 861 ret = chn_start(c, 1); 862 if (ret != 0) 863 return (0); 864 } 865 866 ret = 0; 867 if (chn_polltrigger(c)) { 868 chn_pollreset(c); 869 ret = ev; 870 } else 871 selrecord(td, sndbuf_getsel(bs)); 872 873 return (ret); 874 } 875 876 /* 877 * chn_abort terminates a running dma transfer. it may sleep up to 200ms. 878 * it returns the number of bytes that have not been transferred. 879 * 880 * called from: dsp_close, dsp_ioctl, with channel locked 881 */ 882 int 883 chn_abort(struct pcm_channel *c) 884 { 885 int missing = 0; 886 struct snd_dbuf *b = c->bufhard; 887 struct snd_dbuf *bs = c->bufsoft; 888 889 CHN_LOCKASSERT(c); 890 if (CHN_STOPPED(c)) 891 return 0; 892 c->flags |= CHN_F_ABORTING; 893 894 c->flags &= ~CHN_F_TRIGGERED; 895 /* kill the channel */ 896 chn_trigger(c, PCMTRIG_ABORT); 897 sndbuf_setrun(b, 0); 898 if (!(c->flags & CHN_F_VIRTUAL)) 899 chn_dmaupdate(c); 900 missing = sndbuf_getready(bs); 901 902 c->flags &= ~CHN_F_ABORTING; 903 return missing; 904 } 905 906 /* 907 * this routine tries to flush the dma transfer. It is called 908 * on a close of a playback channel. 909 * first, if there is data in the buffer, but the dma has not yet 910 * begun, we need to start it. 911 * next, we wait for the play buffer to drain 912 * finally, we stop the dma. 913 * 914 * called from: dsp_close, not valid for record channels. 915 */ 916 917 int 918 chn_flush(struct pcm_channel *c) 919 { 920 struct snd_dbuf *b = c->bufhard; 921 922 CHN_LOCKASSERT(c); 923 KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel")); 924 DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags)); 925 926 c->flags |= CHN_F_CLOSING; 927 chn_sync(c, 0); 928 c->flags &= ~CHN_F_TRIGGERED; 929 /* kill the channel */ 930 chn_trigger(c, PCMTRIG_ABORT); 931 sndbuf_setrun(b, 0); 932 933 c->flags &= ~CHN_F_CLOSING; 934 return 0; 935 } 936 937 int 938 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist) 939 { 940 int i; 941 942 for (i = 0; fmtlist[i] != 0; i++) { 943 if (fmt == fmtlist[i] || 944 ((fmt & AFMT_PASSTHROUGH) && 945 (AFMT_ENCODING(fmt) & fmtlist[i]))) 946 return (1); 947 } 948 949 return (0); 950 } 951 952 static const struct { 953 char *name, *alias1, *alias2; 954 uint32_t afmt; 955 } afmt_tab[] = { 956 { "alaw", NULL, NULL, AFMT_A_LAW }, 957 { "mulaw", NULL, NULL, AFMT_MU_LAW }, 958 { "u8", "8", NULL, AFMT_U8 }, 959 { "s8", NULL, NULL, AFMT_S8 }, 960 #if BYTE_ORDER == LITTLE_ENDIAN 961 { "s16le", "s16", "16", AFMT_S16_LE }, 962 { "s16be", NULL, NULL, AFMT_S16_BE }, 963 #else 964 { "s16le", NULL, NULL, AFMT_S16_LE }, 965 { "s16be", "s16", "16", AFMT_S16_BE }, 966 #endif 967 { "u16le", NULL, NULL, AFMT_U16_LE }, 968 { "u16be", NULL, NULL, AFMT_U16_BE }, 969 { "s24le", NULL, NULL, AFMT_S24_LE }, 970 { "s24be", NULL, NULL, AFMT_S24_BE }, 971 { "u24le", NULL, NULL, AFMT_U24_LE }, 972 { "u24be", NULL, NULL, AFMT_U24_BE }, 973 #if BYTE_ORDER == LITTLE_ENDIAN 974 { "s32le", "s32", "32", AFMT_S32_LE }, 975 { "s32be", NULL, NULL, AFMT_S32_BE }, 976 #else 977 { "s32le", NULL, NULL, AFMT_S32_LE }, 978 { "s32be", "s32", "32", AFMT_S32_BE }, 979 #endif 980 { "u32le", NULL, NULL, AFMT_U32_LE }, 981 { "u32be", NULL, NULL, AFMT_U32_BE }, 982 { "ac3", NULL, NULL, AFMT_AC3 }, 983 { NULL, NULL, NULL, 0 } 984 }; 985 986 uint32_t 987 snd_str2afmt(const char *req) 988 { 989 int ext; 990 int ch; 991 int i; 992 char b1[8]; 993 char b2[8]; 994 995 memset(b1, 0, sizeof(b1)); 996 memset(b2, 0, sizeof(b2)); 997 998 i = sscanf(req, "%5[^:]:%6s", b1, b2); 999 1000 if (i == 1) { 1001 if (strlen(req) != strlen(b1)) 1002 return (0); 1003 strlcpy(b2, "2.0", sizeof(b2)); 1004 } else if (i == 2) { 1005 if (strlen(req) != (strlen(b1) + 1 + strlen(b2))) 1006 return (0); 1007 } else 1008 return (0); 1009 1010 i = sscanf(b2, "%d.%d", &ch, &ext); 1011 1012 if (i == 0) { 1013 if (strcasecmp(b2, "mono") == 0) { 1014 ch = 1; 1015 ext = 0; 1016 } else if (strcasecmp(b2, "stereo") == 0) { 1017 ch = 2; 1018 ext = 0; 1019 } else if (strcasecmp(b2, "quad") == 0) { 1020 ch = 4; 1021 ext = 0; 1022 } else 1023 return (0); 1024 } else if (i == 1) { 1025 if (ch < 1 || ch > AFMT_CHANNEL_MAX) 1026 return (0); 1027 ext = 0; 1028 } else if (i == 2) { 1029 if (ext < 0 || ext > AFMT_EXTCHANNEL_MAX) 1030 return (0); 1031 if (ch < 1 || (ch + ext) > AFMT_CHANNEL_MAX) 1032 return (0); 1033 } else 1034 return (0); 1035 1036 for (i = 0; afmt_tab[i].name != NULL; i++) { 1037 if (strcasecmp(afmt_tab[i].name, b1) != 0) { 1038 if (afmt_tab[i].alias1 == NULL) 1039 continue; 1040 if (strcasecmp(afmt_tab[i].alias1, b1) != 0) { 1041 if (afmt_tab[i].alias2 == NULL) 1042 continue; 1043 if (strcasecmp(afmt_tab[i].alias2, b1) != 0) 1044 continue; 1045 } 1046 } 1047 /* found a match */ 1048 return (SND_FORMAT(afmt_tab[i].afmt, ch + ext, ext)); 1049 } 1050 /* not a valid format */ 1051 return (0); 1052 } 1053 1054 uint32_t 1055 snd_afmt2str(uint32_t afmt, char *buf, size_t len) 1056 { 1057 uint32_t enc; 1058 uint32_t ext; 1059 uint32_t ch; 1060 int i; 1061 1062 if (buf == NULL || len < AFMTSTR_LEN) 1063 return (0); 1064 1065 memset(buf, 0, len); 1066 1067 enc = AFMT_ENCODING(afmt); 1068 ch = AFMT_CHANNEL(afmt); 1069 ext = AFMT_EXTCHANNEL(afmt); 1070 /* check there is at least one channel */ 1071 if (ch <= ext) 1072 return (0); 1073 for (i = 0; afmt_tab[i].name != NULL; i++) { 1074 if (enc != afmt_tab[i].afmt) 1075 continue; 1076 /* found a match */ 1077 snprintf(buf, len, "%s:%d.%d", 1078 afmt_tab[i].name, ch - ext, ext); 1079 return (SND_FORMAT(enc, ch, ext)); 1080 } 1081 return (0); 1082 } 1083 1084 int 1085 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd) 1086 { 1087 int r; 1088 1089 CHN_LOCKASSERT(c); 1090 c->feedcount = 0; 1091 c->flags &= CHN_F_RESET; 1092 c->interrupts = 0; 1093 c->timeout = 1; 1094 c->xruns = 0; 1095 1096 c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ? 1097 CHN_F_BITPERFECT : 0; 1098 1099 r = CHANNEL_RESET(c->methods, c->devinfo); 1100 if (r == 0 && fmt != 0 && spd != 0) { 1101 r = chn_setparam(c, fmt, spd); 1102 fmt = 0; 1103 spd = 0; 1104 } 1105 if (r == 0 && fmt != 0) 1106 r = chn_setformat(c, fmt); 1107 if (r == 0 && spd != 0) 1108 r = chn_setspeed(c, spd); 1109 if (r == 0) 1110 r = chn_setlatency(c, chn_latency); 1111 if (r == 0) { 1112 chn_resetbuf(c); 1113 r = CHANNEL_RESETDONE(c->methods, c->devinfo); 1114 } 1115 return r; 1116 } 1117 1118 static struct unrhdr * 1119 chn_getunr(struct snddev_info *d, int type) 1120 { 1121 switch (type) { 1122 case PCMDIR_PLAY: 1123 return (d->p_unr); 1124 case PCMDIR_PLAY_VIRTUAL: 1125 return (d->vp_unr); 1126 case PCMDIR_REC: 1127 return (d->r_unr); 1128 case PCMDIR_REC_VIRTUAL: 1129 return (d->vr_unr); 1130 default: 1131 __assert_unreachable(); 1132 } 1133 1134 } 1135 1136 char * 1137 chn_mkname(char *buf, size_t len, struct pcm_channel *c) 1138 { 1139 const char *str; 1140 1141 KASSERT(buf != NULL && len != 0, 1142 ("%s(): bogus buf=%p len=%zu", __func__, buf, len)); 1143 1144 switch (c->type) { 1145 case PCMDIR_PLAY: 1146 str = "play"; 1147 break; 1148 case PCMDIR_PLAY_VIRTUAL: 1149 str = "virtual_play"; 1150 break; 1151 case PCMDIR_REC: 1152 str = "record"; 1153 break; 1154 case PCMDIR_REC_VIRTUAL: 1155 str = "virtual_record"; 1156 break; 1157 default: 1158 __assert_unreachable(); 1159 } 1160 1161 snprintf(buf, len, "dsp%d.%s.%d", 1162 device_get_unit(c->dev), str, c->unit); 1163 1164 return (buf); 1165 } 1166 1167 struct pcm_channel * 1168 chn_init(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, 1169 int dir, void *devinfo) 1170 { 1171 struct pcm_channel *c; 1172 struct feeder_class *fc; 1173 struct snd_dbuf *b, *bs; 1174 char buf[CHN_NAMELEN]; 1175 int err, i, direction; 1176 1177 PCM_BUSYASSERT(d); 1178 PCM_LOCKASSERT(d); 1179 1180 switch (dir) { 1181 case PCMDIR_PLAY: 1182 d->playcount++; 1183 /* FALLTHROUGH */ 1184 case PCMDIR_PLAY_VIRTUAL: 1185 if (dir == PCMDIR_PLAY_VIRTUAL) 1186 d->pvchancount++; 1187 direction = PCMDIR_PLAY; 1188 break; 1189 case PCMDIR_REC: 1190 d->reccount++; 1191 /* FALLTHROUGH */ 1192 case PCMDIR_REC_VIRTUAL: 1193 if (dir == PCMDIR_REC_VIRTUAL) 1194 d->rvchancount++; 1195 direction = PCMDIR_REC; 1196 break; 1197 default: 1198 device_printf(d->dev, 1199 "%s(): invalid channel direction: %d\n", 1200 __func__, dir); 1201 return (NULL); 1202 } 1203 1204 PCM_UNLOCK(d); 1205 b = NULL; 1206 bs = NULL; 1207 1208 c = malloc(sizeof(*c), M_DEVBUF, M_WAITOK | M_ZERO); 1209 c->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO); 1210 chn_lockinit(c, dir); 1211 CHN_INIT(c, children); 1212 CHN_INIT(c, children.busy); 1213 c->direction = direction; 1214 c->type = dir; 1215 c->unit = alloc_unr(chn_getunr(d, c->type)); 1216 c->format = SND_FORMAT(AFMT_S16_LE, 2, 0); 1217 c->speed = 48000; 1218 c->pid = -1; 1219 c->latency = -1; 1220 c->timeout = 1; 1221 strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm)); 1222 c->parentsnddev = d; 1223 c->parentchannel = parent; 1224 c->dev = d->dev; 1225 c->trigger = PCMTRIG_STOP; 1226 strlcpy(c->name, chn_mkname(buf, sizeof(buf), c), sizeof(c->name)); 1227 1228 c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0); 1229 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL; 1230 1231 for (i = 0; i < SND_CHN_T_MAX; i++) 1232 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER; 1233 1234 c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER; 1235 c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm; 1236 1237 CHN_LOCK(c); 1238 chn_vpc_reset(c, SND_VOL_C_PCM, 1); 1239 CHN_UNLOCK(c); 1240 1241 fc = feeder_getclass(NULL); 1242 if (fc == NULL) { 1243 device_printf(d->dev, "%s(): failed to get feeder class\n", 1244 __func__); 1245 goto fail; 1246 } 1247 if (feeder_add(c, fc, NULL)) { 1248 device_printf(d->dev, "%s(): failed to add feeder\n", __func__); 1249 goto fail; 1250 } 1251 1252 b = sndbuf_create(c->dev, c->name, "primary", c); 1253 bs = sndbuf_create(c->dev, c->name, "secondary", c); 1254 if (b == NULL || bs == NULL) { 1255 device_printf(d->dev, "%s(): failed to create %s buffer\n", 1256 __func__, b == NULL ? "hardware" : "software"); 1257 goto fail; 1258 } 1259 c->bufhard = b; 1260 c->bufsoft = bs; 1261 1262 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction); 1263 if (c->devinfo == NULL) { 1264 device_printf(d->dev, "%s(): CHANNEL_INIT() failed\n", __func__); 1265 goto fail; 1266 } 1267 1268 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0)) { 1269 device_printf(d->dev, "%s(): hardware buffer's size is 0\n", 1270 __func__); 1271 goto fail; 1272 } 1273 1274 sndbuf_setfmt(b, c->format); 1275 sndbuf_setspd(b, c->speed); 1276 sndbuf_setfmt(bs, c->format); 1277 sndbuf_setspd(bs, c->speed); 1278 sndbuf_setup(bs, NULL, 0); 1279 1280 /** 1281 * @todo Should this be moved somewhere else? The primary buffer 1282 * is allocated by the driver or via DMA map setup, and tmpbuf 1283 * seems to only come into existence in sndbuf_resize(). 1284 */ 1285 if (c->direction == PCMDIR_PLAY) { 1286 bs->sl = sndbuf_getmaxsize(bs); 1287 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_WAITOK); 1288 } 1289 1290 if ((c->flags & CHN_F_VIRTUAL) == 0) { 1291 CHN_LOCK(c); 1292 err = chn_reset(c, c->format, c->speed); 1293 CHN_UNLOCK(c); 1294 if (err != 0) 1295 goto fail; 1296 } 1297 1298 PCM_LOCK(d); 1299 CHN_INSERT_SORT_ASCEND(d, c, channels.pcm); 1300 if ((c->flags & CHN_F_VIRTUAL) == 0) 1301 CHN_INSERT_SORT_ASCEND(d, c, channels.pcm.primary); 1302 1303 return (c); 1304 1305 fail: 1306 chn_kill(c); 1307 PCM_LOCK(d); 1308 1309 return (NULL); 1310 } 1311 1312 void 1313 chn_kill(struct pcm_channel *c) 1314 { 1315 struct snddev_info *d = c->parentsnddev; 1316 struct snd_dbuf *b = c->bufhard; 1317 struct snd_dbuf *bs = c->bufsoft; 1318 1319 PCM_BUSYASSERT(c->parentsnddev); 1320 1321 PCM_LOCK(d); 1322 CHN_REMOVE(d, c, channels.pcm); 1323 if ((c->flags & CHN_F_VIRTUAL) == 0) 1324 CHN_REMOVE(d, c, channels.pcm.primary); 1325 1326 switch (c->type) { 1327 case PCMDIR_PLAY: 1328 d->playcount--; 1329 break; 1330 case PCMDIR_PLAY_VIRTUAL: 1331 d->pvchancount--; 1332 break; 1333 case PCMDIR_REC: 1334 d->reccount--; 1335 break; 1336 case PCMDIR_REC_VIRTUAL: 1337 d->rvchancount--; 1338 break; 1339 default: 1340 __assert_unreachable(); 1341 } 1342 PCM_UNLOCK(d); 1343 1344 if (CHN_STARTED(c)) { 1345 CHN_LOCK(c); 1346 chn_trigger(c, PCMTRIG_ABORT); 1347 CHN_UNLOCK(c); 1348 } 1349 free_unr(chn_getunr(d, c->type), c->unit); 1350 feeder_remove(c); 1351 if (c->devinfo && CHANNEL_FREE(c->methods, c->devinfo)) 1352 sndbuf_free(b); 1353 if (bs) 1354 sndbuf_destroy(bs); 1355 if (b) 1356 sndbuf_destroy(b); 1357 CHN_LOCK(c); 1358 c->flags |= CHN_F_DEAD; 1359 chn_lockdestroy(c); 1360 kobj_delete(c->methods, M_DEVBUF); 1361 free(c, M_DEVBUF); 1362 } 1363 1364 void 1365 chn_shutdown(struct pcm_channel *c) 1366 { 1367 CHN_LOCKASSERT(c); 1368 1369 chn_wakeup(c); 1370 c->flags |= CHN_F_DEAD; 1371 } 1372 1373 /* release a locked channel and unlock it */ 1374 int 1375 chn_release(struct pcm_channel *c) 1376 { 1377 PCM_BUSYASSERT(c->parentsnddev); 1378 CHN_LOCKASSERT(c); 1379 1380 c->flags &= ~CHN_F_BUSY; 1381 c->pid = -1; 1382 strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm)); 1383 CHN_UNLOCK(c); 1384 1385 return (0); 1386 } 1387 1388 int 1389 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right, 1390 int center) 1391 { 1392 int i, ret; 1393 1394 ret = 0; 1395 1396 for (i = 0; i < SND_CHN_T_MAX; i++) { 1397 if ((1 << i) & SND_CHN_LEFT_MASK) 1398 ret |= chn_setvolume_matrix(c, vc, i, left); 1399 else if ((1 << i) & SND_CHN_RIGHT_MASK) 1400 ret |= chn_setvolume_matrix(c, vc, i, right) << 8; 1401 else 1402 ret |= chn_setvolume_matrix(c, vc, i, center) << 16; 1403 } 1404 1405 return (ret); 1406 } 1407 1408 int 1409 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val) 1410 { 1411 int i; 1412 1413 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX && 1414 (vc == SND_VOL_C_MASTER || (vc & 1)) && 1415 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN && 1416 vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB || 1417 (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)), 1418 ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d", 1419 __func__, c, vc, vt, val)); 1420 CHN_LOCKASSERT(c); 1421 1422 if (val < 0) 1423 val = 0; 1424 if (val > 100) 1425 val = 100; 1426 1427 c->volume[vc][vt] = val; 1428 1429 /* 1430 * Do relative calculation here and store it into class + 1 1431 * to ease the job of feeder_volume. 1432 */ 1433 if (vc == SND_VOL_C_MASTER) { 1434 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END; 1435 vc += SND_VOL_C_STEP) 1436 c->volume[SND_VOL_C_VAL(vc)][vt] = 1437 SND_VOL_CALC_VAL(c->volume, vc, vt); 1438 } else if (vc & 1) { 1439 if (vt == SND_CHN_T_VOL_0DB) 1440 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; 1441 i += SND_CHN_T_STEP) { 1442 c->volume[SND_VOL_C_VAL(vc)][i] = 1443 SND_VOL_CALC_VAL(c->volume, vc, i); 1444 } 1445 else 1446 c->volume[SND_VOL_C_VAL(vc)][vt] = 1447 SND_VOL_CALC_VAL(c->volume, vc, vt); 1448 } 1449 1450 return (val); 1451 } 1452 1453 int 1454 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt) 1455 { 1456 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX && 1457 (vt == SND_CHN_T_VOL_0DB || 1458 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)), 1459 ("%s(): invalid volume matrix c=%p vc=%d vt=%d", 1460 __func__, c, vc, vt)); 1461 CHN_LOCKASSERT(c); 1462 1463 return (c->volume[vc][vt]); 1464 } 1465 1466 int 1467 chn_setmute_multi(struct pcm_channel *c, int vc, int mute) 1468 { 1469 int i, ret; 1470 1471 ret = 0; 1472 1473 for (i = 0; i < SND_CHN_T_MAX; i++) { 1474 if ((1 << i) & SND_CHN_LEFT_MASK) 1475 ret |= chn_setmute_matrix(c, vc, i, mute); 1476 else if ((1 << i) & SND_CHN_RIGHT_MASK) 1477 ret |= chn_setmute_matrix(c, vc, i, mute) << 8; 1478 else 1479 ret |= chn_setmute_matrix(c, vc, i, mute) << 16; 1480 } 1481 return (ret); 1482 } 1483 1484 int 1485 chn_setmute_matrix(struct pcm_channel *c, int vc, int vt, int mute) 1486 { 1487 int i; 1488 1489 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX && 1490 (vc == SND_VOL_C_MASTER || (vc & 1)) && 1491 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)), 1492 ("%s(): invalid mute matrix c=%p vc=%d vt=%d mute=%d", 1493 __func__, c, vc, vt, mute)); 1494 1495 CHN_LOCKASSERT(c); 1496 1497 mute = (mute != 0); 1498 1499 c->muted[vc][vt] = mute; 1500 1501 /* 1502 * Do relative calculation here and store it into class + 1 1503 * to ease the job of feeder_volume. 1504 */ 1505 if (vc == SND_VOL_C_MASTER) { 1506 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END; 1507 vc += SND_VOL_C_STEP) 1508 c->muted[SND_VOL_C_VAL(vc)][vt] = mute; 1509 } else if (vc & 1) { 1510 if (vt == SND_CHN_T_VOL_0DB) { 1511 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; 1512 i += SND_CHN_T_STEP) { 1513 c->muted[SND_VOL_C_VAL(vc)][i] = mute; 1514 } 1515 } else { 1516 c->muted[SND_VOL_C_VAL(vc)][vt] = mute; 1517 } 1518 } 1519 return (mute); 1520 } 1521 1522 int 1523 chn_getmute_matrix(struct pcm_channel *c, int vc, int vt) 1524 { 1525 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX && 1526 (vt == SND_CHN_T_VOL_0DB || 1527 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)), 1528 ("%s(): invalid mute matrix c=%p vc=%d vt=%d", 1529 __func__, c, vc, vt)); 1530 CHN_LOCKASSERT(c); 1531 1532 return (c->muted[vc][vt]); 1533 } 1534 1535 struct pcmchan_matrix * 1536 chn_getmatrix(struct pcm_channel *c) 1537 { 1538 1539 KASSERT(c != NULL, ("%s(): NULL channel", __func__)); 1540 CHN_LOCKASSERT(c); 1541 1542 if (!(c->format & AFMT_CONVERTIBLE)) 1543 return (NULL); 1544 1545 return (&c->matrix); 1546 } 1547 1548 int 1549 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m) 1550 { 1551 1552 KASSERT(c != NULL && m != NULL, 1553 ("%s(): NULL channel or matrix", __func__)); 1554 CHN_LOCKASSERT(c); 1555 1556 if (!(c->format & AFMT_CONVERTIBLE)) 1557 return (EINVAL); 1558 1559 c->matrix = *m; 1560 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL; 1561 1562 return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext))); 1563 } 1564 1565 /* 1566 * XXX chn_oss_* exists for the sake of compatibility. 1567 */ 1568 int 1569 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map) 1570 { 1571 1572 KASSERT(c != NULL && map != NULL, 1573 ("%s(): NULL channel or map", __func__)); 1574 CHN_LOCKASSERT(c); 1575 1576 if (!(c->format & AFMT_CONVERTIBLE)) 1577 return (EINVAL); 1578 1579 return (feeder_matrix_oss_get_channel_order(&c->matrix, map)); 1580 } 1581 1582 int 1583 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map) 1584 { 1585 struct pcmchan_matrix m; 1586 int ret; 1587 1588 KASSERT(c != NULL && map != NULL, 1589 ("%s(): NULL channel or map", __func__)); 1590 CHN_LOCKASSERT(c); 1591 1592 if (!(c->format & AFMT_CONVERTIBLE)) 1593 return (EINVAL); 1594 1595 m = c->matrix; 1596 ret = feeder_matrix_oss_set_channel_order(&m, map); 1597 if (ret != 0) 1598 return (ret); 1599 1600 return (chn_setmatrix(c, &m)); 1601 } 1602 1603 #define SND_CHN_OSS_FRONT (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR) 1604 #define SND_CHN_OSS_SURR (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR) 1605 #define SND_CHN_OSS_CENTER_LFE (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF) 1606 #define SND_CHN_OSS_REAR (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR) 1607 1608 int 1609 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask) 1610 { 1611 struct pcmchan_matrix *m; 1612 struct pcmchan_caps *caps; 1613 uint32_t i, format; 1614 1615 KASSERT(c != NULL && retmask != NULL, 1616 ("%s(): NULL channel or retmask", __func__)); 1617 CHN_LOCKASSERT(c); 1618 1619 caps = chn_getcaps(c); 1620 if (caps == NULL || caps->fmtlist == NULL) 1621 return (ENODEV); 1622 1623 for (i = 0; caps->fmtlist[i] != 0; i++) { 1624 format = caps->fmtlist[i]; 1625 if (!(format & AFMT_CONVERTIBLE)) { 1626 *retmask |= DSP_BIND_SPDIF; 1627 continue; 1628 } 1629 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format); 1630 if (m == NULL) 1631 continue; 1632 if (m->mask & SND_CHN_OSS_FRONT) 1633 *retmask |= DSP_BIND_FRONT; 1634 if (m->mask & SND_CHN_OSS_SURR) 1635 *retmask |= DSP_BIND_SURR; 1636 if (m->mask & SND_CHN_OSS_CENTER_LFE) 1637 *retmask |= DSP_BIND_CENTER_LFE; 1638 if (m->mask & SND_CHN_OSS_REAR) 1639 *retmask |= DSP_BIND_REAR; 1640 } 1641 1642 /* report software-supported binding mask */ 1643 if (!CHN_BITPERFECT(c) && report_soft_matrix) 1644 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR | 1645 DSP_BIND_CENTER_LFE | DSP_BIND_REAR; 1646 1647 return (0); 1648 } 1649 1650 void 1651 chn_vpc_reset(struct pcm_channel *c, int vc, int force) 1652 { 1653 int i; 1654 1655 KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END, 1656 ("%s(): invalid reset c=%p vc=%d", __func__, c, vc)); 1657 CHN_LOCKASSERT(c); 1658 1659 if (force == 0 && chn_vpc_autoreset == 0) 1660 return; 1661 1662 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP) 1663 CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]); 1664 } 1665 1666 static u_int32_t 1667 round_pow2(u_int32_t v) 1668 { 1669 u_int32_t ret; 1670 1671 if (v < 2) 1672 v = 2; 1673 ret = 0; 1674 while (v >> ret) 1675 ret++; 1676 ret = 1 << (ret - 1); 1677 while (ret < v) 1678 ret <<= 1; 1679 return ret; 1680 } 1681 1682 static u_int32_t 1683 round_blksz(u_int32_t v, int round) 1684 { 1685 u_int32_t ret, tmp; 1686 1687 if (round < 1) 1688 round = 1; 1689 1690 ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1); 1691 1692 if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2)) 1693 ret >>= 1; 1694 1695 tmp = ret - (ret % round); 1696 while (tmp < 16 || tmp < round) { 1697 ret <<= 1; 1698 tmp = ret - (ret % round); 1699 } 1700 1701 return ret; 1702 } 1703 1704 /* 1705 * 4Front call it DSP Policy, while we call it "Latency Profile". The idea 1706 * is to keep 2nd buffer short so that it doesn't cause long queue during 1707 * buffer transfer. 1708 * 1709 * Latency reference table for 48khz stereo 16bit: (PLAY) 1710 * 1711 * +---------+------------+-----------+------------+ 1712 * | Latency | Blockcount | Blocksize | Buffersize | 1713 * +---------+------------+-----------+------------+ 1714 * | 0 | 2 | 64 | 128 | 1715 * +---------+------------+-----------+------------+ 1716 * | 1 | 4 | 128 | 512 | 1717 * +---------+------------+-----------+------------+ 1718 * | 2 | 8 | 512 | 4096 | 1719 * +---------+------------+-----------+------------+ 1720 * | 3 | 16 | 512 | 8192 | 1721 * +---------+------------+-----------+------------+ 1722 * | 4 | 32 | 512 | 16384 | 1723 * +---------+------------+-----------+------------+ 1724 * | 5 | 32 | 1024 | 32768 | 1725 * +---------+------------+-----------+------------+ 1726 * | 6 | 16 | 2048 | 32768 | 1727 * +---------+------------+-----------+------------+ 1728 * | 7 | 8 | 4096 | 32768 | 1729 * +---------+------------+-----------+------------+ 1730 * | 8 | 4 | 8192 | 32768 | 1731 * +---------+------------+-----------+------------+ 1732 * | 9 | 2 | 16384 | 32768 | 1733 * +---------+------------+-----------+------------+ 1734 * | 10 | 2 | 32768 | 65536 | 1735 * +---------+------------+-----------+------------+ 1736 * 1737 * Recording need a different reference table. All we care is 1738 * gobbling up everything within reasonable buffering threshold. 1739 * 1740 * Latency reference table for 48khz stereo 16bit: (REC) 1741 * 1742 * +---------+------------+-----------+------------+ 1743 * | Latency | Blockcount | Blocksize | Buffersize | 1744 * +---------+------------+-----------+------------+ 1745 * | 0 | 512 | 32 | 16384 | 1746 * +---------+------------+-----------+------------+ 1747 * | 1 | 256 | 64 | 16384 | 1748 * +---------+------------+-----------+------------+ 1749 * | 2 | 128 | 128 | 16384 | 1750 * +---------+------------+-----------+------------+ 1751 * | 3 | 64 | 256 | 16384 | 1752 * +---------+------------+-----------+------------+ 1753 * | 4 | 32 | 512 | 16384 | 1754 * +---------+------------+-----------+------------+ 1755 * | 5 | 32 | 1024 | 32768 | 1756 * +---------+------------+-----------+------------+ 1757 * | 6 | 16 | 2048 | 32768 | 1758 * +---------+------------+-----------+------------+ 1759 * | 7 | 8 | 4096 | 32768 | 1760 * +---------+------------+-----------+------------+ 1761 * | 8 | 4 | 8192 | 32768 | 1762 * +---------+------------+-----------+------------+ 1763 * | 9 | 2 | 16384 | 32768 | 1764 * +---------+------------+-----------+------------+ 1765 * | 10 | 2 | 32768 | 65536 | 1766 * +---------+------------+-----------+------------+ 1767 * 1768 * Calculations for other data rate are entirely based on these reference 1769 * tables. For normal operation, Latency 5 seems give the best, well 1770 * balanced performance for typical workload. Anything below 5 will 1771 * eat up CPU to keep up with increasing context switches because of 1772 * shorter buffer space and usually require the application to handle it 1773 * aggressively through possibly real time programming technique. 1774 * 1775 */ 1776 #define CHN_LATENCY_PBLKCNT_REF \ 1777 {{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}, \ 1778 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}} 1779 #define CHN_LATENCY_PBUFSZ_REF \ 1780 {{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16}, \ 1781 {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}} 1782 1783 #define CHN_LATENCY_RBLKCNT_REF \ 1784 {{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}, \ 1785 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}} 1786 #define CHN_LATENCY_RBUFSZ_REF \ 1787 {{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16}, \ 1788 {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}} 1789 1790 #define CHN_LATENCY_DATA_REF 192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */ 1791 1792 static int 1793 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate, 1794 u_int32_t max, int *rblksz, int *rblkcnt) 1795 { 1796 static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = 1797 CHN_LATENCY_PBLKCNT_REF; 1798 static int pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = 1799 CHN_LATENCY_PBUFSZ_REF; 1800 static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = 1801 CHN_LATENCY_RBLKCNT_REF; 1802 static int rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] = 1803 CHN_LATENCY_RBUFSZ_REF; 1804 u_int32_t bufsz; 1805 int lprofile, blksz, blkcnt; 1806 1807 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX || 1808 bps < 1 || datarate < 1 || 1809 !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) { 1810 if (rblksz != NULL) 1811 *rblksz = CHN_2NDBUFMAXSIZE >> 1; 1812 if (rblkcnt != NULL) 1813 *rblkcnt = 2; 1814 printf("%s(): FAILED dir=%d latency=%d bps=%d " 1815 "datarate=%u max=%u\n", 1816 __func__, dir, latency, bps, datarate, max); 1817 return CHN_2NDBUFMAXSIZE; 1818 } 1819 1820 lprofile = chn_latency_profile; 1821 1822 if (dir == PCMDIR_PLAY) { 1823 blkcnt = pblkcnts[lprofile][latency]; 1824 bufsz = pbufszs[lprofile][latency]; 1825 } else { 1826 blkcnt = rblkcnts[lprofile][latency]; 1827 bufsz = rbufszs[lprofile][latency]; 1828 } 1829 1830 bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF, 1831 datarate)); 1832 if (bufsz > max) 1833 bufsz = max; 1834 blksz = round_blksz(bufsz >> blkcnt, bps); 1835 1836 if (rblksz != NULL) 1837 *rblksz = blksz; 1838 if (rblkcnt != NULL) 1839 *rblkcnt = 1 << blkcnt; 1840 1841 return blksz << blkcnt; 1842 } 1843 1844 static int 1845 chn_resizebuf(struct pcm_channel *c, int latency, 1846 int blkcnt, int blksz) 1847 { 1848 struct snd_dbuf *b, *bs, *pb; 1849 int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt; 1850 int ret; 1851 1852 CHN_LOCKASSERT(c); 1853 1854 if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) || 1855 !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC)) 1856 return EINVAL; 1857 1858 if (latency == -1) { 1859 c->latency = -1; 1860 latency = chn_latency; 1861 } else if (latency == -2) { 1862 latency = c->latency; 1863 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX) 1864 latency = chn_latency; 1865 } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX) 1866 return EINVAL; 1867 else { 1868 c->latency = latency; 1869 } 1870 1871 bs = c->bufsoft; 1872 b = c->bufhard; 1873 1874 if (!(blksz == 0 || blkcnt == -1) && 1875 (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 || 1876 (blksz * blkcnt) > CHN_2NDBUFMAXSIZE)) 1877 return EINVAL; 1878 1879 chn_calclatency(c->direction, latency, sndbuf_getalign(bs), 1880 sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE, 1881 &sblksz, &sblkcnt); 1882 1883 if (blksz == 0 || blkcnt == -1) { 1884 if (blkcnt == -1) 1885 c->flags &= ~CHN_F_HAS_SIZE; 1886 if (c->flags & CHN_F_HAS_SIZE) { 1887 blksz = sndbuf_getblksz(bs); 1888 blkcnt = sndbuf_getblkcnt(bs); 1889 } 1890 } else 1891 c->flags |= CHN_F_HAS_SIZE; 1892 1893 if (c->flags & CHN_F_HAS_SIZE) { 1894 /* 1895 * The application has requested their own blksz/blkcnt. 1896 * Just obey with it, and let them toast alone. We can 1897 * clamp it to the nearest latency profile, but that would 1898 * defeat the purpose of having custom control. The least 1899 * we can do is round it to the nearest ^2 and align it. 1900 */ 1901 sblksz = round_blksz(blksz, sndbuf_getalign(bs)); 1902 sblkcnt = round_pow2(blkcnt); 1903 } 1904 1905 if (c->parentchannel != NULL) { 1906 pb = c->parentchannel->bufsoft; 1907 CHN_UNLOCK(c); 1908 CHN_LOCK(c->parentchannel); 1909 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE); 1910 CHN_UNLOCK(c->parentchannel); 1911 CHN_LOCK(c); 1912 if (c->direction == PCMDIR_PLAY) { 1913 limit = (pb != NULL) ? 1914 sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0; 1915 } else { 1916 limit = (pb != NULL) ? 1917 sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0; 1918 } 1919 } else { 1920 hblkcnt = 2; 1921 if (c->flags & CHN_F_HAS_SIZE) { 1922 hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b), 1923 sndbuf_getalign(b)); 1924 hblkcnt = round_pow2(sndbuf_getblkcnt(bs)); 1925 } else 1926 chn_calclatency(c->direction, latency, 1927 sndbuf_getalign(b), 1928 sndbuf_getalign(b) * sndbuf_getspd(b), 1929 CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt); 1930 1931 if ((hblksz << 1) > sndbuf_getmaxsize(b)) 1932 hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1, 1933 sndbuf_getalign(b)); 1934 1935 while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) { 1936 if (hblkcnt < 4) 1937 hblksz >>= 1; 1938 else 1939 hblkcnt >>= 1; 1940 } 1941 1942 hblksz -= hblksz % sndbuf_getalign(b); 1943 1944 CHN_UNLOCK(c); 1945 if (chn_usefrags == 0 || 1946 CHANNEL_SETFRAGMENTS(c->methods, c->devinfo, 1947 hblksz, hblkcnt) != 0) 1948 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods, 1949 c->devinfo, hblksz)); 1950 CHN_LOCK(c); 1951 1952 if (!CHN_EMPTY(c, children)) { 1953 nsblksz = round_blksz( 1954 sndbuf_xbytes(sndbuf_getblksz(b), b, bs), 1955 sndbuf_getalign(bs)); 1956 nsblkcnt = sndbuf_getblkcnt(b); 1957 if (c->direction == PCMDIR_PLAY) { 1958 do { 1959 nsblkcnt--; 1960 } while (nsblkcnt >= 2 && 1961 nsblksz * nsblkcnt >= sblksz * sblkcnt); 1962 nsblkcnt++; 1963 } 1964 sblksz = nsblksz; 1965 sblkcnt = nsblkcnt; 1966 limit = 0; 1967 } else 1968 limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2; 1969 } 1970 1971 if (limit > CHN_2NDBUFMAXSIZE) 1972 limit = CHN_2NDBUFMAXSIZE; 1973 1974 while ((sblksz * sblkcnt) < limit) 1975 sblkcnt <<= 1; 1976 1977 while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) { 1978 if (sblkcnt < 4) 1979 sblksz >>= 1; 1980 else 1981 sblkcnt >>= 1; 1982 } 1983 1984 sblksz -= sblksz % sndbuf_getalign(bs); 1985 1986 if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz || 1987 sndbuf_getsize(bs) != (sblkcnt * sblksz)) { 1988 ret = sndbuf_remalloc(bs, sblkcnt, sblksz); 1989 if (ret != 0) { 1990 device_printf(c->dev, "%s(): Failed: %d %d\n", 1991 __func__, sblkcnt, sblksz); 1992 return ret; 1993 } 1994 } 1995 1996 /* 1997 * Interrupt timeout 1998 */ 1999 c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) / 2000 ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs)); 2001 if (c->parentchannel != NULL) 2002 c->timeout = min(c->timeout, c->parentchannel->timeout); 2003 if (c->timeout < 1) 2004 c->timeout = 1; 2005 2006 /* 2007 * OSSv4 docs: "By default OSS will set the low water level equal 2008 * to the fragment size which is optimal in most cases." 2009 */ 2010 c->lw = sndbuf_getblksz(bs); 2011 chn_resetbuf(c); 2012 2013 if (snd_verbose > 3) 2014 device_printf(c->dev, "%s(): %s (%s) timeout=%u " 2015 "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n", 2016 __func__, CHN_DIRSTR(c), 2017 (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware", 2018 c->timeout, 2019 sndbuf_getsize(b), sndbuf_getblksz(b), 2020 sndbuf_getblkcnt(b), 2021 sndbuf_getsize(bs), sndbuf_getblksz(bs), 2022 sndbuf_getblkcnt(bs), limit); 2023 2024 return 0; 2025 } 2026 2027 int 2028 chn_setlatency(struct pcm_channel *c, int latency) 2029 { 2030 CHN_LOCKASSERT(c); 2031 /* Destroy blksz/blkcnt, enforce latency profile. */ 2032 return chn_resizebuf(c, latency, -1, 0); 2033 } 2034 2035 int 2036 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz) 2037 { 2038 CHN_LOCKASSERT(c); 2039 /* Destroy latency profile, enforce blksz/blkcnt */ 2040 return chn_resizebuf(c, -1, blkcnt, blksz); 2041 } 2042 2043 int 2044 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed) 2045 { 2046 struct pcmchan_caps *caps; 2047 uint32_t hwspeed, delta; 2048 int ret; 2049 2050 CHN_LOCKASSERT(c); 2051 2052 if (speed < 1 || format == 0 || CHN_STARTED(c)) 2053 return (EINVAL); 2054 2055 c->format = format; 2056 c->speed = speed; 2057 2058 caps = chn_getcaps(c); 2059 2060 hwspeed = speed; 2061 RANGE(hwspeed, caps->minspeed, caps->maxspeed); 2062 2063 sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo, 2064 hwspeed)); 2065 hwspeed = sndbuf_getspd(c->bufhard); 2066 2067 delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed); 2068 2069 if (delta <= feeder_rate_round) 2070 c->speed = hwspeed; 2071 2072 ret = feeder_chain(c); 2073 2074 if (ret == 0) 2075 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo, 2076 sndbuf_getfmt(c->bufhard)); 2077 2078 if (ret == 0) 2079 ret = chn_resizebuf(c, -2, 0, 0); 2080 2081 return (ret); 2082 } 2083 2084 int 2085 chn_setspeed(struct pcm_channel *c, uint32_t speed) 2086 { 2087 uint32_t oldformat, oldspeed; 2088 int ret; 2089 2090 oldformat = c->format; 2091 oldspeed = c->speed; 2092 2093 if (c->speed == speed) 2094 return (0); 2095 2096 ret = chn_setparam(c, c->format, speed); 2097 if (ret != 0) { 2098 if (snd_verbose > 3) 2099 device_printf(c->dev, 2100 "%s(): Setting speed %d failed, " 2101 "falling back to %d\n", 2102 __func__, speed, oldspeed); 2103 chn_setparam(c, oldformat, oldspeed); 2104 } 2105 2106 return (ret); 2107 } 2108 2109 int 2110 chn_setformat(struct pcm_channel *c, uint32_t format) 2111 { 2112 uint32_t oldformat, oldspeed; 2113 int ret; 2114 2115 /* XXX force stereo */ 2116 if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) { 2117 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL, 2118 AFMT_PASSTHROUGH_EXTCHANNEL); 2119 } 2120 2121 oldformat = c->format; 2122 oldspeed = c->speed; 2123 2124 if (c->format == format) 2125 return (0); 2126 2127 ret = chn_setparam(c, format, c->speed); 2128 if (ret != 0) { 2129 if (snd_verbose > 3) 2130 device_printf(c->dev, 2131 "%s(): Format change 0x%08x failed, " 2132 "falling back to 0x%08x\n", 2133 __func__, format, oldformat); 2134 chn_setparam(c, oldformat, oldspeed); 2135 } 2136 2137 return (ret); 2138 } 2139 2140 void 2141 chn_syncstate(struct pcm_channel *c) 2142 { 2143 struct snddev_info *d; 2144 struct snd_mixer *m; 2145 2146 d = (c != NULL) ? c->parentsnddev : NULL; 2147 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 : 2148 NULL; 2149 2150 if (d == NULL || m == NULL) 2151 return; 2152 2153 CHN_LOCKASSERT(c); 2154 2155 if (c->feederflags & (1 << FEEDER_VOLUME)) { 2156 uint32_t parent; 2157 int vol, pvol, left, right, center; 2158 2159 if (c->direction == PCMDIR_PLAY && 2160 (d->flags & SD_F_SOFTPCMVOL)) { 2161 /* CHN_UNLOCK(c); */ 2162 vol = mix_get(m, SOUND_MIXER_PCM); 2163 parent = mix_getparent(m, SOUND_MIXER_PCM); 2164 if (parent != SOUND_MIXER_NONE) 2165 pvol = mix_get(m, parent); 2166 else 2167 pvol = 100 | (100 << 8); 2168 /* CHN_LOCK(c); */ 2169 } else { 2170 vol = 100 | (100 << 8); 2171 pvol = vol; 2172 } 2173 2174 if (vol == -1) { 2175 device_printf(c->dev, 2176 "Soft PCM Volume: Failed to read pcm " 2177 "default value\n"); 2178 vol = 100 | (100 << 8); 2179 } 2180 2181 if (pvol == -1) { 2182 device_printf(c->dev, 2183 "Soft PCM Volume: Failed to read parent " 2184 "default value\n"); 2185 pvol = 100 | (100 << 8); 2186 } 2187 2188 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100; 2189 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100; 2190 center = (left + right) >> 1; 2191 2192 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center); 2193 } 2194 2195 if (c->feederflags & (1 << FEEDER_EQ)) { 2196 struct pcm_feeder *f; 2197 int treble, bass, state; 2198 2199 /* CHN_UNLOCK(c); */ 2200 treble = mix_get(m, SOUND_MIXER_TREBLE); 2201 bass = mix_get(m, SOUND_MIXER_BASS); 2202 /* CHN_LOCK(c); */ 2203 2204 if (treble == -1) 2205 treble = 50; 2206 else 2207 treble = ((treble & 0x7f) + 2208 ((treble >> 8) & 0x7f)) >> 1; 2209 2210 if (bass == -1) 2211 bass = 50; 2212 else 2213 bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1; 2214 2215 f = feeder_find(c, FEEDER_EQ); 2216 if (f != NULL) { 2217 if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0) 2218 device_printf(c->dev, 2219 "EQ: Failed to set treble -- %d\n", 2220 treble); 2221 if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0) 2222 device_printf(c->dev, 2223 "EQ: Failed to set bass -- %d\n", 2224 bass); 2225 if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0) 2226 device_printf(c->dev, 2227 "EQ: Failed to set preamp -- %d\n", 2228 d->eqpreamp); 2229 if (d->flags & SD_F_EQ_BYPASSED) 2230 state = FEEDEQ_BYPASS; 2231 else if (d->flags & SD_F_EQ_ENABLED) 2232 state = FEEDEQ_ENABLE; 2233 else 2234 state = FEEDEQ_DISABLE; 2235 if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0) 2236 device_printf(c->dev, 2237 "EQ: Failed to set state -- %d\n", state); 2238 } 2239 } 2240 } 2241 2242 int 2243 chn_trigger(struct pcm_channel *c, int go) 2244 { 2245 struct snddev_info *d = c->parentsnddev; 2246 int ret; 2247 2248 CHN_LOCKASSERT(c); 2249 if (!PCMTRIG_COMMON(go)) 2250 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go)); 2251 2252 if (go == c->trigger) 2253 return (0); 2254 2255 if (snd_verbose > 3) { 2256 device_printf(c->dev, "%s() %s: calling go=0x%08x , " 2257 "prev=0x%08x\n", __func__, c->name, go, c->trigger); 2258 } 2259 2260 c->trigger = go; 2261 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go); 2262 if (ret != 0) 2263 return (ret); 2264 2265 CHN_UNLOCK(c); 2266 PCM_LOCK(d); 2267 CHN_LOCK(c); 2268 2269 /* 2270 * Do nothing if another thread set a different trigger while we had 2271 * dropped the mutex. 2272 */ 2273 if (go != c->trigger) { 2274 PCM_UNLOCK(d); 2275 return (0); 2276 } 2277 2278 /* 2279 * Use the SAFE variants to prevent inserting/removing an already 2280 * existing/missing element. 2281 */ 2282 switch (go) { 2283 case PCMTRIG_START: 2284 CHN_INSERT_HEAD_SAFE(d, c, channels.pcm.busy); 2285 PCM_UNLOCK(d); 2286 chn_syncstate(c); 2287 break; 2288 case PCMTRIG_STOP: 2289 case PCMTRIG_ABORT: 2290 CHN_REMOVE(d, c, channels.pcm.busy); 2291 PCM_UNLOCK(d); 2292 break; 2293 default: 2294 PCM_UNLOCK(d); 2295 break; 2296 } 2297 2298 return (0); 2299 } 2300 2301 /** 2302 * @brief Queries sound driver for sample-aligned hardware buffer pointer index 2303 * 2304 * This function obtains the hardware pointer location, then aligns it to 2305 * the current bytes-per-sample value before returning. (E.g., a channel 2306 * running in 16 bit stereo mode would require 4 bytes per sample, so a 2307 * hwptr value ranging from 32-35 would be returned as 32.) 2308 * 2309 * @param c PCM channel context 2310 * @returns sample-aligned hardware buffer pointer index 2311 */ 2312 int 2313 chn_getptr(struct pcm_channel *c) 2314 { 2315 int hwptr; 2316 2317 CHN_LOCKASSERT(c); 2318 hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0; 2319 return (hwptr - (hwptr % sndbuf_getalign(c->bufhard))); 2320 } 2321 2322 struct pcmchan_caps * 2323 chn_getcaps(struct pcm_channel *c) 2324 { 2325 CHN_LOCKASSERT(c); 2326 return CHANNEL_GETCAPS(c->methods, c->devinfo); 2327 } 2328 2329 u_int32_t 2330 chn_getformats(struct pcm_channel *c) 2331 { 2332 u_int32_t *fmtlist, fmts; 2333 int i; 2334 2335 fmtlist = chn_getcaps(c)->fmtlist; 2336 fmts = 0; 2337 for (i = 0; fmtlist[i]; i++) 2338 fmts |= fmtlist[i]; 2339 2340 /* report software-supported formats */ 2341 if (!CHN_BITPERFECT(c) && report_soft_formats) 2342 fmts |= AFMT_CONVERTIBLE; 2343 2344 return (AFMT_ENCODING(fmts)); 2345 } 2346 2347 int 2348 chn_notify(struct pcm_channel *c, u_int32_t flags) 2349 { 2350 struct pcm_channel *ch; 2351 struct pcmchan_caps *caps; 2352 uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate; 2353 uint32_t vpflags; 2354 int dirty, err, run, nrun; 2355 2356 CHN_LOCKASSERT(c); 2357 2358 if (CHN_EMPTY(c, children)) 2359 return (0); 2360 2361 err = 0; 2362 2363 /* 2364 * If the hwchan is running, we can't change its rate, format or 2365 * blocksize 2366 */ 2367 run = (CHN_STARTED(c)) ? 1 : 0; 2368 if (run) 2369 flags &= CHN_N_VOLUME | CHN_N_TRIGGER; 2370 2371 if (flags & CHN_N_RATE) { 2372 /* 2373 * XXX I'll make good use of this someday. 2374 * However this is currently being superseded by 2375 * the availability of CHN_F_VCHAN_DYNAMIC. 2376 */ 2377 } 2378 2379 if (flags & CHN_N_FORMAT) { 2380 /* 2381 * XXX I'll make good use of this someday. 2382 * However this is currently being superseded by 2383 * the availability of CHN_F_VCHAN_DYNAMIC. 2384 */ 2385 } 2386 2387 if (flags & CHN_N_VOLUME) { 2388 /* 2389 * XXX I'll make good use of this someday, though 2390 * soft volume control is currently pretty much 2391 * integrated. 2392 */ 2393 } 2394 2395 if (flags & CHN_N_BLOCKSIZE) { 2396 /* 2397 * Set to default latency profile 2398 */ 2399 chn_setlatency(c, chn_latency); 2400 } 2401 2402 if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) { 2403 nrun = CHN_EMPTY(c, children.busy) ? 0 : 1; 2404 if (nrun && !run) 2405 err = chn_start(c, 1); 2406 if (!nrun && run) 2407 chn_abort(c); 2408 flags &= ~CHN_N_TRIGGER; 2409 } 2410 2411 if (flags & CHN_N_TRIGGER) { 2412 if (c->direction == PCMDIR_PLAY) { 2413 vchanformat = &c->parentsnddev->pvchanformat; 2414 vchanrate = &c->parentsnddev->pvchanrate; 2415 } else { 2416 vchanformat = &c->parentsnddev->rvchanformat; 2417 vchanrate = &c->parentsnddev->rvchanrate; 2418 } 2419 2420 /* Dynamic Virtual Channel */ 2421 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) { 2422 bestformat = *vchanformat; 2423 bestspeed = *vchanrate; 2424 } else { 2425 bestformat = 0; 2426 bestspeed = 0; 2427 } 2428 2429 besthwformat = 0; 2430 nrun = 0; 2431 caps = chn_getcaps(c); 2432 dirty = 0; 2433 vpflags = 0; 2434 2435 CHN_FOREACH(ch, c, children.busy) { 2436 CHN_LOCK(ch); 2437 if ((ch->format & AFMT_PASSTHROUGH) && 2438 snd_fmtvalid(ch->format, caps->fmtlist)) { 2439 bestformat = ch->format; 2440 bestspeed = ch->speed; 2441 CHN_UNLOCK(ch); 2442 vpflags = CHN_F_PASSTHROUGH; 2443 nrun++; 2444 break; 2445 } 2446 if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) { 2447 if (c->flags & CHN_F_VCHAN_ADAPTIVE) { 2448 bestspeed = ch->speed; 2449 RANGE(bestspeed, caps->minspeed, 2450 caps->maxspeed); 2451 besthwformat = snd_fmtbest(ch->format, 2452 caps->fmtlist); 2453 if (besthwformat != 0) 2454 bestformat = besthwformat; 2455 } 2456 CHN_UNLOCK(ch); 2457 vpflags = CHN_F_EXCLUSIVE; 2458 nrun++; 2459 continue; 2460 } 2461 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) || 2462 vpflags != 0) { 2463 CHN_UNLOCK(ch); 2464 nrun++; 2465 continue; 2466 } 2467 if (ch->speed > bestspeed) { 2468 bestspeed = ch->speed; 2469 RANGE(bestspeed, caps->minspeed, 2470 caps->maxspeed); 2471 } 2472 besthwformat = snd_fmtbest(ch->format, caps->fmtlist); 2473 if (!(besthwformat & AFMT_VCHAN)) { 2474 CHN_UNLOCK(ch); 2475 nrun++; 2476 continue; 2477 } 2478 if (AFMT_CHANNEL(besthwformat) > 2479 AFMT_CHANNEL(bestformat)) 2480 bestformat = besthwformat; 2481 else if (AFMT_CHANNEL(besthwformat) == 2482 AFMT_CHANNEL(bestformat) && 2483 AFMT_BIT(besthwformat) > AFMT_BIT(bestformat)) 2484 bestformat = besthwformat; 2485 CHN_UNLOCK(ch); 2486 nrun++; 2487 } 2488 2489 if (bestformat == 0) 2490 bestformat = c->format; 2491 if (bestspeed == 0) 2492 bestspeed = c->speed; 2493 2494 if (bestformat != c->format || bestspeed != c->speed) 2495 dirty = 1; 2496 2497 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE); 2498 c->flags |= vpflags; 2499 2500 if (nrun && !run) { 2501 if (dirty) { 2502 bestspeed = CHANNEL_SETSPEED(c->methods, 2503 c->devinfo, bestspeed); 2504 err = chn_reset(c, bestformat, bestspeed); 2505 } 2506 if (err == 0 && dirty) { 2507 CHN_FOREACH(ch, c, children.busy) { 2508 CHN_LOCK(ch); 2509 if (VCHAN_SYNC_REQUIRED(ch)) 2510 vchan_sync(ch); 2511 CHN_UNLOCK(ch); 2512 } 2513 } 2514 if (err == 0) { 2515 if (dirty) 2516 c->flags |= CHN_F_DIRTY; 2517 err = chn_start(c, 1); 2518 } 2519 } 2520 2521 if (nrun && run && dirty) { 2522 chn_abort(c); 2523 bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo, 2524 bestspeed); 2525 err = chn_reset(c, bestformat, bestspeed); 2526 if (err == 0) { 2527 CHN_FOREACH(ch, c, children.busy) { 2528 CHN_LOCK(ch); 2529 if (VCHAN_SYNC_REQUIRED(ch)) 2530 vchan_sync(ch); 2531 CHN_UNLOCK(ch); 2532 } 2533 } 2534 if (err == 0) { 2535 c->flags |= CHN_F_DIRTY; 2536 err = chn_start(c, 1); 2537 } 2538 } 2539 2540 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) && 2541 (bestformat & AFMT_VCHAN)) { 2542 *vchanformat = bestformat; 2543 *vchanrate = bestspeed; 2544 } 2545 2546 if (!nrun && run) { 2547 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE); 2548 bestformat = *vchanformat; 2549 bestspeed = *vchanrate; 2550 chn_abort(c); 2551 if (c->format != bestformat || c->speed != bestspeed) 2552 chn_reset(c, bestformat, bestspeed); 2553 } 2554 } 2555 2556 return (err); 2557 } 2558 2559 /** 2560 * @brief Fetch array of supported discrete sample rates 2561 * 2562 * Wrapper for CHANNEL_GETRATES. Please see channel_if.m:getrates() for 2563 * detailed information. 2564 * 2565 * @note If the operation isn't supported, this function will just return 0 2566 * (no rates in the array), and *rates will be set to NULL. Callers 2567 * should examine rates @b only if this function returns non-zero. 2568 * 2569 * @param c pcm channel to examine 2570 * @param rates pointer to array of integers; rate table will be recorded here 2571 * 2572 * @return number of rates in the array pointed to be @c rates 2573 */ 2574 int 2575 chn_getrates(struct pcm_channel *c, int **rates) 2576 { 2577 KASSERT(rates != NULL, ("rates is null")); 2578 CHN_LOCKASSERT(c); 2579 return CHANNEL_GETRATES(c->methods, c->devinfo, rates); 2580 } 2581 2582 /** 2583 * @brief Remove channel from a sync group, if there is one. 2584 * 2585 * This function is initially intended for the following conditions: 2586 * - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl) 2587 * - Closing a device. (A channel can't be destroyed if it's still in use.) 2588 * 2589 * @note Before calling this function, the syncgroup list mutex must be 2590 * held. (Consider pcm_channel::sm protected by the SG list mutex 2591 * whether @c c is locked or not.) 2592 * 2593 * @param c channel device to be started or closed 2594 * @returns If this channel was the only member of a group, the group ID 2595 * is returned to the caller so that the caller can release it 2596 * via free_unr() after giving up the syncgroup lock. Else it 2597 * returns 0. 2598 */ 2599 int 2600 chn_syncdestroy(struct pcm_channel *c) 2601 { 2602 struct pcmchan_syncmember *sm; 2603 struct pcmchan_syncgroup *sg; 2604 int sg_id; 2605 2606 sg_id = 0; 2607 2608 PCM_SG_LOCKASSERT(MA_OWNED); 2609 2610 if (c->sm != NULL) { 2611 sm = c->sm; 2612 sg = sm->parent; 2613 c->sm = NULL; 2614 2615 KASSERT(sg != NULL, ("syncmember has null parent")); 2616 2617 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link); 2618 free(sm, M_DEVBUF); 2619 2620 if (SLIST_EMPTY(&sg->members)) { 2621 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link); 2622 sg_id = sg->id; 2623 free(sg, M_DEVBUF); 2624 } 2625 } 2626 2627 return sg_id; 2628 } 2629 2630 #ifdef OSSV4_EXPERIMENT 2631 int 2632 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak) 2633 { 2634 CHN_LOCKASSERT(c); 2635 return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak); 2636 } 2637 #endif 2638