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