2 * SPDX-License-Identifier: BSD-2-Clause
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include <dev/sound/pcm/sound.h>
37 #include <dev/sound/pcm/vchan.h>
39 #include "feeder_if.h"
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");
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");
49 int chn_latency = CHN_LATENCY_DEFAULT;
52 sysctl_hw_snd_latency(SYSCTL_HANDLER_ARGS)
57 err = sysctl_handle_int(oidp, &val, 0, req);
58 if (err != 0 || req->newptr == NULL)
60 if (val < CHN_LATENCY_MIN || val > CHN_LATENCY_MAX)
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)");
72 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
75 sysctl_hw_snd_latency_profile(SYSCTL_HANDLER_ARGS)
79 val = chn_latency_profile;
80 err = sysctl_handle_int(oidp, &val, 0, req);
81 if (err != 0 || req->newptr == NULL)
83 if (val < CHN_LATENCY_PROFILE_MIN || val > CHN_LATENCY_PROFILE_MAX)
86 chn_latency_profile = val;
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)");
95 static int chn_timeout = CHN_TIMEOUT;
98 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS)
103 err = sysctl_handle_int(oidp, &val, 0, req);
104 if (err != 0 || req->newptr == NULL)
106 if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX)
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");
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");
122 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM;
125 chn_vpc_proc(int reset, int db)
127 struct snddev_info *d;
128 struct pcm_channel *c;
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))
139 CHN_FOREACH(c, d, channels.pcm) {
141 CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db);
143 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
152 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS)
156 val = chn_vol_0db_pcm;
157 err = sysctl_handle_int(oidp, &val, 0, req);
158 if (err != 0 || req->newptr == NULL)
160 if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX)
163 chn_vol_0db_pcm = val;
164 chn_vpc_proc(0, val);
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");
174 sysctl_hw_snd_vpc_reset(SYSCTL_HANDLER_ARGS)
179 err = sysctl_handle_int(oidp, &val, 0, req);
180 if (err != 0 || req->newptr == NULL || val == 0)
183 chn_vol_0db_pcm = SND_VOL_0DB_PCM;
184 chn_vpc_proc(1, SND_VOL_0DB_PCM);
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");
193 static int chn_usefrags = 0;
194 static int chn_syncdelay = -1;
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,
200 "append (0-1000) millisecond trailing buffer delay on each sync");
203 * @brief Channel sync group lock
205 * Clients should acquire this lock @b without holding any channel locks
206 * before touching syncgroups or the main syncgroup list.
208 struct mtx snd_pcm_syncgroups_mtx;
209 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF);
211 * @brief syncgroups' master list
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.
216 * See SNDCTL_DSP_SYNCGROUP for more information.
218 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups);
221 chn_lockinit(struct pcm_channel *c, int dir)
225 c->lock = snd_mtxcreate(c->name, "pcm play channel");
226 cv_init(&c->intr_cv, "pcmwr");
228 case PCMDIR_PLAY_VIRTUAL:
229 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
230 cv_init(&c->intr_cv, "pcmwrv");
233 c->lock = snd_mtxcreate(c->name, "pcm record channel");
234 cv_init(&c->intr_cv, "pcmrd");
236 case PCMDIR_REC_VIRTUAL:
237 c->lock = snd_mtxcreate(c->name, "pcm virtual record channel");
238 cv_init(&c->intr_cv, "pcmrdv");
241 panic("%s(): Invalid direction=%d", __func__, dir);
245 cv_init(&c->cv, "pcmchn");
249 chn_lockdestroy(struct pcm_channel *c)
253 CHN_BROADCAST(&c->cv);
254 CHN_BROADCAST(&c->intr_cv);
257 cv_destroy(&c->intr_cv);
259 snd_mtxfree(c->lock);
263 * @brief Determine channel is ready for I/O
265 * @retval 1 = ready for I/O
266 * @retval 0 = not ready for I/O
269 chn_polltrigger(struct pcm_channel *c)
271 struct snd_dbuf *bs = c->bufsoft;
276 if (c->flags & CHN_F_MMAP) {
277 if (sndbuf_getprevtotal(bs) < c->lw)
280 delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs);
282 if (c->direction == PCMDIR_PLAY)
283 delta = sndbuf_getfree(bs);
285 delta = sndbuf_getready(bs);
288 return ((delta < c->lw) ? 0 : 1);
292 chn_pollreset(struct pcm_channel *c)
296 sndbuf_updateprevtotal(c->bufsoft);
300 chn_wakeup(struct pcm_channel *c)
303 struct pcm_channel *ch;
309 if (CHN_EMPTY(c, children.busy)) {
310 if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c))
311 selwakeuppri(sndbuf_getsel(bs), PRIBIO);
312 if (c->flags & CHN_F_SLEEPING) {
314 * Ok, I can just panic it right here since it is
315 * quite obvious that we never allow multiple waiters
316 * from userland. I'm too generous...
318 CHN_BROADCAST(&c->intr_cv);
321 CHN_FOREACH(ch, c, children.busy) {
330 chn_sleep(struct pcm_channel *c, int timeout)
335 KASSERT((c->flags & CHN_F_SLEEPING) == 0,
336 ("%s(): entered with CHN_F_SLEEPING", __func__));
338 if (c->flags & CHN_F_DEAD)
341 c->flags |= CHN_F_SLEEPING;
342 ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout);
343 c->flags &= ~CHN_F_SLEEPING;
345 return ((c->flags & CHN_F_DEAD) ? EINVAL : ret);
349 * chn_dmaupdate() tracks the status of a dma transfer,
354 chn_dmaupdate(struct pcm_channel *c)
356 struct snd_dbuf *b = c->bufhard;
357 unsigned int delta, old, hwptr, amt;
359 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
362 old = sndbuf_gethwptr(b);
363 hwptr = chn_getptr(c);
364 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
365 sndbuf_sethwptr(b, hwptr);
367 if (c->direction == PCMDIR_PLAY) {
368 amt = min(delta, sndbuf_getready(b));
369 amt -= amt % sndbuf_getalign(b);
371 sndbuf_dispose(b, NULL, amt);
373 amt = min(delta, sndbuf_getfree(b));
374 amt -= amt % sndbuf_getalign(b);
376 sndbuf_acquire(b, NULL, amt);
378 if (snd_verbose > 3 && CHN_STARTED(c) && delta == 0) {
379 device_printf(c->dev, "WARNING: %s DMA completion "
380 "too fast/slow ! hwptr=%u, old=%u "
381 "delta=%u amt=%u ready=%u free=%u\n",
382 CHN_DIRSTR(c), hwptr, old, delta, amt,
383 sndbuf_getready(b), sndbuf_getfree(b));
390 chn_wrfeed(struct pcm_channel *c)
392 struct snd_dbuf *b = c->bufhard;
393 struct snd_dbuf *bs = c->bufsoft;
394 unsigned int amt, want, wasfree;
398 if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING))
399 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
401 wasfree = sndbuf_getfree(b);
402 want = min(sndbuf_getsize(b),
403 imax(0, sndbuf_xbytes(sndbuf_getsize(bs), bs, b) -
404 sndbuf_getready(b)));
405 amt = min(wasfree, want);
407 sndbuf_feed(bs, b, c, c->feeder, amt);
410 * Possible xruns. There should be no empty space left in buffer.
412 if (sndbuf_getready(b) < want)
415 if (sndbuf_getfree(b) < wasfree)
421 chn_wrupdate(struct pcm_channel *c)
425 KASSERT(c->direction == PCMDIR_PLAY, ("%s(): bad channel", __func__));
427 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
431 /* tell the driver we've updated the primary buffer */
432 chn_trigger(c, PCMTRIG_EMLDMAWR);
437 chn_wrintr(struct pcm_channel *c)
441 /* update pointers in primary buffer */
443 /* ...and feed from secondary to primary */
445 /* tell the driver we've updated the primary buffer */
446 chn_trigger(c, PCMTRIG_EMLDMAWR);
450 * user write routine - uiomove data into secondary buffer, trigger if necessary
451 * if blocking, sleep, rinse and repeat.
453 * called externally, so must handle locking
457 chn_write(struct pcm_channel *c, struct uio *buf)
459 struct snd_dbuf *bs = c->bufsoft;
461 int ret, timeout, sz, t, p;
466 timeout = chn_timeout * hz;
468 while (ret == 0 && buf->uio_resid > 0) {
469 sz = min(buf->uio_resid, sndbuf_getfree(bs));
472 * The following assumes that the free space in
473 * the buffer can never be less around the
474 * unlock-uiomove-lock sequence.
476 while (ret == 0 && sz > 0) {
477 p = sndbuf_getfreeptr(bs);
478 t = min(sz, sndbuf_getsize(bs) - p);
479 off = sndbuf_getbufofs(bs, p);
481 ret = uiomove(off, t, buf);
484 sndbuf_acquire(bs, NULL, t);
487 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
488 ret = chn_start(c, 0);
490 c->flags |= CHN_F_DEAD;
492 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) {
494 * @todo Evaluate whether EAGAIN is truly desirable.
495 * 4Front drivers behave like this, but I'm
496 * not sure if it at all violates the "write
497 * should be allowed to block" model.
499 * The idea is that, while set with CHN_F_NOTRIGGER,
500 * a channel isn't playing, *but* without this we
501 * end up with "interrupt timeout / channel dead".
505 ret = chn_sleep(c, timeout);
508 c->flags |= CHN_F_DEAD;
509 device_printf(c->dev, "%s(): %s: "
510 "play interrupt timeout, channel dead\n",
512 } else if (ret == ERESTART || ret == EINTR)
513 c->flags |= CHN_F_ABORTING;
521 * Feed new data from the read buffer. Can be called in the bottom half.
524 chn_rdfeed(struct pcm_channel *c)
526 struct snd_dbuf *b = c->bufhard;
527 struct snd_dbuf *bs = c->bufsoft;
532 if (c->flags & CHN_F_MMAP)
533 sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
535 amt = sndbuf_getfree(bs);
537 sndbuf_feed(b, bs, c, c->feeder, amt);
539 amt = sndbuf_getready(b);
542 sndbuf_dispose(b, NULL, amt);
545 if (sndbuf_getready(bs) > 0)
551 chn_rdupdate(struct pcm_channel *c)
555 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
557 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
559 chn_trigger(c, PCMTRIG_EMLDMARD);
565 /* read interrupt routine. Must be called with interrupts blocked. */
567 chn_rdintr(struct pcm_channel *c)
571 /* tell the driver to update the primary buffer if non-dma */
572 chn_trigger(c, PCMTRIG_EMLDMARD);
573 /* update pointers in primary buffer */
575 /* ...and feed from primary to secondary */
580 * user read routine - trigger if necessary, uiomove data from secondary buffer
581 * if blocking, sleep, rinse and repeat.
583 * called externally, so must handle locking
587 chn_read(struct pcm_channel *c, struct uio *buf)
589 struct snd_dbuf *bs = c->bufsoft;
591 int ret, timeout, sz, t, p;
595 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
596 ret = chn_start(c, 0);
598 c->flags |= CHN_F_DEAD;
604 timeout = chn_timeout * hz;
606 while (ret == 0 && buf->uio_resid > 0) {
607 sz = min(buf->uio_resid, sndbuf_getready(bs));
610 * The following assumes that the free space in
611 * the buffer can never be less around the
612 * unlock-uiomove-lock sequence.
614 while (ret == 0 && sz > 0) {
615 p = sndbuf_getreadyptr(bs);
616 t = min(sz, sndbuf_getsize(bs) - p);
617 off = sndbuf_getbufofs(bs, p);
619 ret = uiomove(off, t, buf);
622 sndbuf_dispose(bs, NULL, t);
625 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER))
628 ret = chn_sleep(c, timeout);
631 c->flags |= CHN_F_DEAD;
632 device_printf(c->dev, "%s(): %s: "
633 "record interrupt timeout, channel dead\n",
635 } else if (ret == ERESTART || ret == EINTR)
636 c->flags |= CHN_F_ABORTING;
644 chn_intr_locked(struct pcm_channel *c)
651 if (c->direction == PCMDIR_PLAY)
658 chn_intr(struct pcm_channel *c)
661 if (CHN_LOCKOWNED(c)) {
672 chn_start(struct pcm_channel *c, int force)
675 struct snd_dbuf *b = c->bufhard;
676 struct snd_dbuf *bs = c->bufsoft;
680 /* if we're running, or if we're prevented from triggering, bail */
681 if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force))
690 if (c->direction == PCMDIR_REC) {
691 i = sndbuf_getfree(bs);
692 j = (i > 0) ? 1 : sndbuf_getready(b);
694 if (sndbuf_getfree(bs) == 0) {
700 pb = CHN_BUF_PARENT(c, b);
701 i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb);
702 j = sndbuf_getalign(pb);
705 if (snd_verbose > 3 && CHN_EMPTY(c, children))
706 device_printf(c->dev, "%s(): %s (%s) threshold "
707 "i=%d j=%d\n", __func__, CHN_DIRSTR(c),
708 (c->flags & CHN_F_VIRTUAL) ? "virtual" :
713 c->flags |= CHN_F_TRIGGERED;
715 if (c->flags & CHN_F_CLOSING)
722 if (c->parentchannel == NULL) {
723 if (c->direction == PCMDIR_PLAY)
724 sndbuf_fillsilence_rl(b,
725 sndbuf_xbytes(sndbuf_getsize(bs), bs, b));
727 device_printf(c->dev,
728 "%s(): %s starting! (%s/%s) "
729 "(ready=%d force=%d i=%d j=%d "
730 "intrtimeout=%u latency=%dms)\n",
732 (c->flags & CHN_F_HAS_VCHAN) ?
733 "VCHAN PARENT" : "HW", CHN_DIRSTR(c),
734 (c->flags & CHN_F_CLOSING) ? "closing" :
737 force, i, j, c->timeout,
738 (sndbuf_getsize(b) * 1000) /
739 (sndbuf_getalign(b) * sndbuf_getspd(b)));
741 err = chn_trigger(c, PCMTRIG_START);
748 chn_resetbuf(struct pcm_channel *c)
750 struct snd_dbuf *b = c->bufhard;
751 struct snd_dbuf *bs = c->bufsoft;
759 * chn_sync waits until the space in the given channel goes above
760 * a threshold. The threshold is checked against fl or rl respectively.
761 * Assume that the condition can become true, do not check here...
764 chn_sync(struct pcm_channel *c, int threshold)
766 struct snd_dbuf *b, *bs;
767 int ret, count, hcount, minflush, resid, residp, syncdelay, blksz;
772 if (c->direction != PCMDIR_PLAY)
777 if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) ||
778 (threshold < 1 && sndbuf_getready(bs) < 1))
781 /* if we haven't yet started and nothing is buffered, else start*/
782 if (CHN_STOPPED(c)) {
783 if (threshold > 0 || sndbuf_getready(bs) > 0) {
784 ret = chn_start(c, 1);
791 b = CHN_BUF_PARENT(c, c->bufhard);
793 minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs);
795 syncdelay = chn_syncdelay;
797 if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0))
798 minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs);
801 * Append (0-1000) millisecond trailing buffer (if needed)
802 * for slower / high latency hardwares (notably USB audio)
803 * to avoid audible truncation.
806 minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) *
807 ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000;
809 minflush -= minflush % sndbuf_getalign(bs);
812 threshold = min(minflush, sndbuf_getfree(bs));
813 sndbuf_clear(bs, threshold);
814 sndbuf_acquire(bs, NULL, threshold);
815 minflush -= threshold;
818 resid = sndbuf_getready(bs);
820 blksz = sndbuf_getblksz(b);
822 device_printf(c->dev,
823 "%s(): WARNING: blksz < 1 ! maxsize=%d [%d/%d/%d]\n",
824 __func__, sndbuf_getmaxsize(b), sndbuf_getsize(b),
825 sndbuf_getblksz(b), sndbuf_getblkcnt(b));
826 if (sndbuf_getblkcnt(b) > 0)
827 blksz = sndbuf_getsize(b) / sndbuf_getblkcnt(b);
831 count = sndbuf_xbytes(minflush + resid, bs, b) / blksz;
836 device_printf(c->dev, "%s(): [begin] timeout=%d count=%d "
837 "minflush=%d resid=%d\n", __func__, c->timeout, count,
840 cflag = c->flags & CHN_F_CLOSING;
841 c->flags |= CHN_F_CLOSING;
842 while (count > 0 && (resid > 0 || minflush > 0)) {
843 ret = chn_sleep(c, c->timeout);
844 if (ret == ERESTART || ret == EINTR) {
845 c->flags |= CHN_F_ABORTING;
847 } else if (ret == 0 || ret == EAGAIN) {
848 resid = sndbuf_getready(bs);
849 if (resid == residp) {
852 device_printf(c->dev,
853 "%s(): [stalled] timeout=%d "
854 "count=%d hcount=%d "
855 "resid=%d minflush=%d\n",
856 __func__, c->timeout, count,
857 hcount, resid, minflush);
858 } else if (resid < residp && count < hcount) {
861 device_printf(c->dev,
862 "%s((): [resume] timeout=%d "
863 "count=%d hcount=%d "
864 "resid=%d minflush=%d\n",
865 __func__, c->timeout, count,
866 hcount, resid, minflush);
868 if (minflush > 0 && sndbuf_getfree(bs) > 0) {
869 threshold = min(minflush,
871 sndbuf_clear(bs, threshold);
872 sndbuf_acquire(bs, NULL, threshold);
873 resid = sndbuf_getready(bs);
874 minflush -= threshold;
880 c->flags &= ~CHN_F_CLOSING;
884 device_printf(c->dev,
885 "%s(): timeout=%d count=%d hcount=%d resid=%d residp=%d "
886 "minflush=%d ret=%d\n",
887 __func__, c->timeout, count, hcount, resid, residp,
893 /* called externally, handle locking */
895 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
897 struct snd_dbuf *bs = c->bufsoft;
902 if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) {
903 ret = chn_start(c, 1);
909 if (chn_polltrigger(c)) {
913 selrecord(td, sndbuf_getsel(bs));
919 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
920 * it returns the number of bytes that have not been transferred.
922 * called from: dsp_close, dsp_ioctl, with channel locked
925 chn_abort(struct pcm_channel *c)
928 struct snd_dbuf *b = c->bufhard;
929 struct snd_dbuf *bs = c->bufsoft;
934 c->flags |= CHN_F_ABORTING;
936 c->flags &= ~CHN_F_TRIGGERED;
937 /* kill the channel */
938 chn_trigger(c, PCMTRIG_ABORT);
940 if (!(c->flags & CHN_F_VIRTUAL))
942 missing = sndbuf_getready(bs);
944 c->flags &= ~CHN_F_ABORTING;
949 * this routine tries to flush the dma transfer. It is called
950 * on a close of a playback channel.
951 * first, if there is data in the buffer, but the dma has not yet
952 * begun, we need to start it.
953 * next, we wait for the play buffer to drain
954 * finally, we stop the dma.
956 * called from: dsp_close, not valid for record channels.
960 chn_flush(struct pcm_channel *c)
962 struct snd_dbuf *b = c->bufhard;
965 KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
966 DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags));
968 c->flags |= CHN_F_CLOSING;
970 c->flags &= ~CHN_F_TRIGGERED;
971 /* kill the channel */
972 chn_trigger(c, PCMTRIG_ABORT);
975 c->flags &= ~CHN_F_CLOSING;
980 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist)
984 for (i = 0; fmtlist[i] != 0; i++) {
985 if (fmt == fmtlist[i] ||
986 ((fmt & AFMT_PASSTHROUGH) &&
987 (AFMT_ENCODING(fmt) & fmtlist[i])))
994 static const struct {
995 char *name, *alias1, *alias2;
998 { "alaw", NULL, NULL, AFMT_A_LAW },
999 { "mulaw", NULL, NULL, AFMT_MU_LAW },
1000 { "u8", "8", NULL, AFMT_U8 },
1001 { "s8", NULL, NULL, AFMT_S8 },
1002 #if BYTE_ORDER == LITTLE_ENDIAN
1003 { "s16le", "s16", "16", AFMT_S16_LE },
1004 { "s16be", NULL, NULL, AFMT_S16_BE },
1006 { "s16le", NULL, NULL, AFMT_S16_LE },
1007 { "s16be", "s16", "16", AFMT_S16_BE },
1009 { "u16le", NULL, NULL, AFMT_U16_LE },
1010 { "u16be", NULL, NULL, AFMT_U16_BE },
1011 { "s24le", NULL, NULL, AFMT_S24_LE },
1012 { "s24be", NULL, NULL, AFMT_S24_BE },
1013 { "u24le", NULL, NULL, AFMT_U24_LE },
1014 { "u24be", NULL, NULL, AFMT_U24_BE },
1015 #if BYTE_ORDER == LITTLE_ENDIAN
1016 { "s32le", "s32", "32", AFMT_S32_LE },
1017 { "s32be", NULL, NULL, AFMT_S32_BE },
1019 { "s32le", NULL, NULL, AFMT_S32_LE },
1020 { "s32be", "s32", "32", AFMT_S32_BE },
1022 { "u32le", NULL, NULL, AFMT_U32_LE },
1023 { "u32be", NULL, NULL, AFMT_U32_BE },
1024 { "ac3", NULL, NULL, AFMT_AC3 },
1025 { NULL, NULL, NULL, 0 }
1029 snd_str2afmt(const char *req)
1037 memset(b1, 0, sizeof(b1));
1038 memset(b2, 0, sizeof(b2));
1040 i = sscanf(req, "%5[^:]:%6s", b1, b2);
1043 if (strlen(req) != strlen(b1))
1045 strlcpy(b2, "2.0", sizeof(b2));
1046 } else if (i == 2) {
1047 if (strlen(req) != (strlen(b1) + 1 + strlen(b2)))
1052 i = sscanf(b2, "%d.%d", &ch, &ext);
1055 if (strcasecmp(b2, "mono") == 0) {
1058 } else if (strcasecmp(b2, "stereo") == 0) {
1061 } else if (strcasecmp(b2, "quad") == 0) {
1066 } else if (i == 1) {
1067 if (ch < 1 || ch > AFMT_CHANNEL_MAX)
1070 } else if (i == 2) {
1071 if (ext < 0 || ext > AFMT_EXTCHANNEL_MAX)
1073 if (ch < 1 || (ch + ext) > AFMT_CHANNEL_MAX)
1078 for (i = 0; afmt_tab[i].name != NULL; i++) {
1079 if (strcasecmp(afmt_tab[i].name, b1) != 0) {
1080 if (afmt_tab[i].alias1 == NULL)
1082 if (strcasecmp(afmt_tab[i].alias1, b1) != 0) {
1083 if (afmt_tab[i].alias2 == NULL)
1085 if (strcasecmp(afmt_tab[i].alias2, b1) != 0)
1090 return (SND_FORMAT(afmt_tab[i].afmt, ch + ext, ext));
1092 /* not a valid format */
1097 snd_afmt2str(uint32_t afmt, char *buf, size_t len)
1104 if (buf == NULL || len < AFMTSTR_LEN)
1107 memset(buf, 0, len);
1109 enc = AFMT_ENCODING(afmt);
1110 ch = AFMT_CHANNEL(afmt);
1111 ext = AFMT_EXTCHANNEL(afmt);
1112 /* check there is at least one channel */
1115 for (i = 0; afmt_tab[i].name != NULL; i++) {
1116 if (enc != afmt_tab[i].afmt)
1119 snprintf(buf, len, "%s:%d.%d",
1120 afmt_tab[i].name, ch - ext, ext);
1121 return (SND_FORMAT(enc, ch, ext));
1127 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
1133 c->flags &= CHN_F_RESET;
1138 c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ?
1139 CHN_F_BITPERFECT : 0;
1141 r = CHANNEL_RESET(c->methods, c->devinfo);
1142 if (r == 0 && fmt != 0 && spd != 0) {
1143 r = chn_setparam(c, fmt, spd);
1147 if (r == 0 && fmt != 0)
1148 r = chn_setformat(c, fmt);
1149 if (r == 0 && spd != 0)
1150 r = chn_setspeed(c, spd);
1152 r = chn_setlatency(c, chn_latency);
1155 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
1161 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
1163 struct feeder_class *fc;
1164 struct snd_dbuf *b, *bs;
1167 if (chn_timeout < CHN_TIMEOUT_MIN || chn_timeout > CHN_TIMEOUT_MAX)
1168 chn_timeout = CHN_TIMEOUT;
1170 chn_lockinit(c, dir);
1174 CHN_INIT(c, children);
1175 CHN_INIT(c, children.busy);
1182 b = sndbuf_create(c->dev, c->name, "primary", c);
1185 bs = sndbuf_create(c->dev, c->name, "secondary", c);
1192 fc = feeder_getclass(NULL);
1195 if (chn_addfeeder(c, fc, NULL))
1199 * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
1200 * with the channel unlocked because they are also called
1201 * from driver methods that don't know about locking
1204 sndbuf_setup(bs, NULL, 0);
1211 c->format = SND_FORMAT(AFMT_U8, 1, 0);
1212 c->speed = DSP_DEFAULT_SPEED;
1214 c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
1215 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1217 for (i = 0; i < SND_CHN_T_MAX; i++) {
1218 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
1221 c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
1222 c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
1224 memset(c->muted, 0, sizeof(c->muted));
1226 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
1229 CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() malloc() call */
1230 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
1232 if (c->devinfo == NULL)
1236 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
1240 c->direction = direction;
1242 sndbuf_setfmt(b, c->format);
1243 sndbuf_setspd(b, c->speed);
1244 sndbuf_setfmt(bs, c->format);
1245 sndbuf_setspd(bs, c->speed);
1248 * @todo Should this be moved somewhere else? The primary buffer
1249 * is allocated by the driver or via DMA map setup, and tmpbuf
1250 * seems to only come into existence in sndbuf_resize().
1252 if (c->direction == PCMDIR_PLAY) {
1253 bs->sl = sndbuf_getmaxsize(bs);
1254 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_NOWAIT);
1255 if (bs->shadbuf == NULL) {
1265 if (CHANNEL_FREE(c->methods, c->devinfo))
1273 c->flags |= CHN_F_DEAD;
1283 chn_kill(struct pcm_channel *c)
1285 struct snd_dbuf *b = c->bufhard;
1286 struct snd_dbuf *bs = c->bufsoft;
1288 PCM_BUSYASSERT(c->parentsnddev);
1290 if (CHN_STARTED(c)) {
1292 chn_trigger(c, PCMTRIG_ABORT);
1295 while (chn_removefeeder(c) == 0)
1297 if (CHANNEL_FREE(c->methods, c->devinfo))
1302 c->flags |= CHN_F_DEAD;
1304 kobj_delete(c->methods, M_DEVBUF);
1309 chn_shutdown(struct pcm_channel *c)
1314 c->flags |= CHN_F_DEAD;
1318 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
1325 for (i = 0; i < SND_CHN_T_MAX; i++) {
1326 if ((1 << i) & SND_CHN_LEFT_MASK)
1327 ret |= chn_setvolume_matrix(c, vc, i, left);
1328 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1329 ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
1331 ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
1338 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
1342 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1343 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1344 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
1345 vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
1346 (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
1347 ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
1348 __func__, c, vc, vt, val));
1356 c->volume[vc][vt] = val;
1359 * Do relative calculation here and store it into class + 1
1360 * to ease the job of feeder_volume.
1362 if (vc == SND_VOL_C_MASTER) {
1363 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1364 vc += SND_VOL_C_STEP)
1365 c->volume[SND_VOL_C_VAL(vc)][vt] =
1366 SND_VOL_CALC_VAL(c->volume, vc, vt);
1367 } else if (vc & 1) {
1368 if (vt == SND_CHN_T_VOL_0DB)
1369 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1370 i += SND_CHN_T_STEP) {
1371 c->volume[SND_VOL_C_VAL(vc)][i] =
1372 SND_VOL_CALC_VAL(c->volume, vc, i);
1375 c->volume[SND_VOL_C_VAL(vc)][vt] =
1376 SND_VOL_CALC_VAL(c->volume, vc, vt);
1383 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
1385 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1386 (vt == SND_CHN_T_VOL_0DB ||
1387 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1388 ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
1389 __func__, c, vc, vt));
1392 return (c->volume[vc][vt]);
1396 chn_setmute_multi(struct pcm_channel *c, int vc, int mute)
1402 for (i = 0; i < SND_CHN_T_MAX; i++) {
1403 if ((1 << i) & SND_CHN_LEFT_MASK)
1404 ret |= chn_setmute_matrix(c, vc, i, mute);
1405 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1406 ret |= chn_setmute_matrix(c, vc, i, mute) << 8;
1408 ret |= chn_setmute_matrix(c, vc, i, mute) << 16;
1414 chn_setmute_matrix(struct pcm_channel *c, int vc, int vt, int mute)
1418 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1419 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1420 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1421 ("%s(): invalid mute matrix c=%p vc=%d vt=%d mute=%d",
1422 __func__, c, vc, vt, mute));
1428 c->muted[vc][vt] = mute;
1431 * Do relative calculation here and store it into class + 1
1432 * to ease the job of feeder_volume.
1434 if (vc == SND_VOL_C_MASTER) {
1435 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1436 vc += SND_VOL_C_STEP)
1437 c->muted[SND_VOL_C_VAL(vc)][vt] = mute;
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->muted[SND_VOL_C_VAL(vc)][i] = mute;
1445 c->muted[SND_VOL_C_VAL(vc)][vt] = mute;
1452 chn_getmute_matrix(struct pcm_channel *c, int vc, int vt)
1454 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1455 (vt == SND_CHN_T_VOL_0DB ||
1456 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1457 ("%s(): invalid mute matrix c=%p vc=%d vt=%d",
1458 __func__, c, vc, vt));
1461 return (c->muted[vc][vt]);
1464 struct pcmchan_matrix *
1465 chn_getmatrix(struct pcm_channel *c)
1468 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1471 if (!(c->format & AFMT_CONVERTIBLE))
1474 return (&c->matrix);
1478 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
1481 KASSERT(c != NULL && m != NULL,
1482 ("%s(): NULL channel or matrix", __func__));
1485 if (!(c->format & AFMT_CONVERTIBLE))
1489 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1491 return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
1495 * XXX chn_oss_* exists for the sake of compatibility.
1498 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
1501 KASSERT(c != NULL && map != NULL,
1502 ("%s(): NULL channel or map", __func__));
1505 if (!(c->format & AFMT_CONVERTIBLE))
1508 return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
1512 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
1514 struct pcmchan_matrix m;
1517 KASSERT(c != NULL && map != NULL,
1518 ("%s(): NULL channel or map", __func__));
1521 if (!(c->format & AFMT_CONVERTIBLE))
1525 ret = feeder_matrix_oss_set_channel_order(&m, map);
1529 return (chn_setmatrix(c, &m));
1532 #define SND_CHN_OSS_FRONT (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
1533 #define SND_CHN_OSS_SURR (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
1534 #define SND_CHN_OSS_CENTER_LFE (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
1535 #define SND_CHN_OSS_REAR (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
1538 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
1540 struct pcmchan_matrix *m;
1541 struct pcmchan_caps *caps;
1544 KASSERT(c != NULL && retmask != NULL,
1545 ("%s(): NULL channel or retmask", __func__));
1548 caps = chn_getcaps(c);
1549 if (caps == NULL || caps->fmtlist == NULL)
1552 for (i = 0; caps->fmtlist[i] != 0; i++) {
1553 format = caps->fmtlist[i];
1554 if (!(format & AFMT_CONVERTIBLE)) {
1555 *retmask |= DSP_BIND_SPDIF;
1558 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
1561 if (m->mask & SND_CHN_OSS_FRONT)
1562 *retmask |= DSP_BIND_FRONT;
1563 if (m->mask & SND_CHN_OSS_SURR)
1564 *retmask |= DSP_BIND_SURR;
1565 if (m->mask & SND_CHN_OSS_CENTER_LFE)
1566 *retmask |= DSP_BIND_CENTER_LFE;
1567 if (m->mask & SND_CHN_OSS_REAR)
1568 *retmask |= DSP_BIND_REAR;
1571 /* report software-supported binding mask */
1572 if (!CHN_BITPERFECT(c) && report_soft_matrix)
1573 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
1574 DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
1580 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
1584 KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
1585 ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
1588 if (force == 0 && chn_vpc_autoreset == 0)
1591 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
1592 CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
1596 round_pow2(u_int32_t v)
1605 ret = 1 << (ret - 1);
1612 round_blksz(u_int32_t v, int round)
1619 ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
1621 if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
1624 tmp = ret - (ret % round);
1625 while (tmp < 16 || tmp < round) {
1627 tmp = ret - (ret % round);
1634 * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
1635 * is to keep 2nd buffer short so that it doesn't cause long queue during
1638 * Latency reference table for 48khz stereo 16bit: (PLAY)
1640 * +---------+------------+-----------+------------+
1641 * | Latency | Blockcount | Blocksize | Buffersize |
1642 * +---------+------------+-----------+------------+
1643 * | 0 | 2 | 64 | 128 |
1644 * +---------+------------+-----------+------------+
1645 * | 1 | 4 | 128 | 512 |
1646 * +---------+------------+-----------+------------+
1647 * | 2 | 8 | 512 | 4096 |
1648 * +---------+------------+-----------+------------+
1649 * | 3 | 16 | 512 | 8192 |
1650 * +---------+------------+-----------+------------+
1651 * | 4 | 32 | 512 | 16384 |
1652 * +---------+------------+-----------+------------+
1653 * | 5 | 32 | 1024 | 32768 |
1654 * +---------+------------+-----------+------------+
1655 * | 6 | 16 | 2048 | 32768 |
1656 * +---------+------------+-----------+------------+
1657 * | 7 | 8 | 4096 | 32768 |
1658 * +---------+------------+-----------+------------+
1659 * | 8 | 4 | 8192 | 32768 |
1660 * +---------+------------+-----------+------------+
1661 * | 9 | 2 | 16384 | 32768 |
1662 * +---------+------------+-----------+------------+
1663 * | 10 | 2 | 32768 | 65536 |
1664 * +---------+------------+-----------+------------+
1666 * Recording need a different reference table. All we care is
1667 * gobbling up everything within reasonable buffering threshold.
1669 * Latency reference table for 48khz stereo 16bit: (REC)
1671 * +---------+------------+-----------+------------+
1672 * | Latency | Blockcount | Blocksize | Buffersize |
1673 * +---------+------------+-----------+------------+
1674 * | 0 | 512 | 32 | 16384 |
1675 * +---------+------------+-----------+------------+
1676 * | 1 | 256 | 64 | 16384 |
1677 * +---------+------------+-----------+------------+
1678 * | 2 | 128 | 128 | 16384 |
1679 * +---------+------------+-----------+------------+
1680 * | 3 | 64 | 256 | 16384 |
1681 * +---------+------------+-----------+------------+
1682 * | 4 | 32 | 512 | 16384 |
1683 * +---------+------------+-----------+------------+
1684 * | 5 | 32 | 1024 | 32768 |
1685 * +---------+------------+-----------+------------+
1686 * | 6 | 16 | 2048 | 32768 |
1687 * +---------+------------+-----------+------------+
1688 * | 7 | 8 | 4096 | 32768 |
1689 * +---------+------------+-----------+------------+
1690 * | 8 | 4 | 8192 | 32768 |
1691 * +---------+------------+-----------+------------+
1692 * | 9 | 2 | 16384 | 32768 |
1693 * +---------+------------+-----------+------------+
1694 * | 10 | 2 | 32768 | 65536 |
1695 * +---------+------------+-----------+------------+
1697 * Calculations for other data rate are entirely based on these reference
1698 * tables. For normal operation, Latency 5 seems give the best, well
1699 * balanced performance for typical workload. Anything below 5 will
1700 * eat up CPU to keep up with increasing context switches because of
1701 * shorter buffer space and usually require the application to handle it
1702 * aggressively through possibly real time programming technique.
1705 #define CHN_LATENCY_PBLKCNT_REF \
1706 {{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}, \
1707 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}}
1708 #define CHN_LATENCY_PBUFSZ_REF \
1709 {{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16}, \
1710 {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}}
1712 #define CHN_LATENCY_RBLKCNT_REF \
1713 {{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}, \
1714 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}}
1715 #define CHN_LATENCY_RBUFSZ_REF \
1716 {{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16}, \
1717 {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}}
1719 #define CHN_LATENCY_DATA_REF 192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
1722 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
1723 u_int32_t max, int *rblksz, int *rblkcnt)
1725 static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1726 CHN_LATENCY_PBLKCNT_REF;
1727 static int pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1728 CHN_LATENCY_PBUFSZ_REF;
1729 static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1730 CHN_LATENCY_RBLKCNT_REF;
1731 static int rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1732 CHN_LATENCY_RBUFSZ_REF;
1734 int lprofile, blksz, blkcnt;
1736 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
1737 bps < 1 || datarate < 1 ||
1738 !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
1740 *rblksz = CHN_2NDBUFMAXSIZE >> 1;
1741 if (rblkcnt != NULL)
1743 printf("%s(): FAILED dir=%d latency=%d bps=%d "
1744 "datarate=%u max=%u\n",
1745 __func__, dir, latency, bps, datarate, max);
1746 return CHN_2NDBUFMAXSIZE;
1749 lprofile = chn_latency_profile;
1751 if (dir == PCMDIR_PLAY) {
1752 blkcnt = pblkcnts[lprofile][latency];
1753 bufsz = pbufszs[lprofile][latency];
1755 blkcnt = rblkcnts[lprofile][latency];
1756 bufsz = rbufszs[lprofile][latency];
1759 bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
1763 blksz = round_blksz(bufsz >> blkcnt, bps);
1767 if (rblkcnt != NULL)
1768 *rblkcnt = 1 << blkcnt;
1770 return blksz << blkcnt;
1774 chn_resizebuf(struct pcm_channel *c, int latency,
1775 int blkcnt, int blksz)
1777 struct snd_dbuf *b, *bs, *pb;
1778 int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
1783 if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
1784 !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
1787 if (latency == -1) {
1789 latency = chn_latency;
1790 } else if (latency == -2) {
1791 latency = c->latency;
1792 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1793 latency = chn_latency;
1794 } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1797 c->latency = latency;
1803 if (!(blksz == 0 || blkcnt == -1) &&
1804 (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 ||
1805 (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
1808 chn_calclatency(c->direction, latency, sndbuf_getalign(bs),
1809 sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE,
1812 if (blksz == 0 || blkcnt == -1) {
1814 c->flags &= ~CHN_F_HAS_SIZE;
1815 if (c->flags & CHN_F_HAS_SIZE) {
1816 blksz = sndbuf_getblksz(bs);
1817 blkcnt = sndbuf_getblkcnt(bs);
1820 c->flags |= CHN_F_HAS_SIZE;
1822 if (c->flags & CHN_F_HAS_SIZE) {
1824 * The application has requested their own blksz/blkcnt.
1825 * Just obey with it, and let them toast alone. We can
1826 * clamp it to the nearest latency profile, but that would
1827 * defeat the purpose of having custom control. The least
1828 * we can do is round it to the nearest ^2 and align it.
1830 sblksz = round_blksz(blksz, sndbuf_getalign(bs));
1831 sblkcnt = round_pow2(blkcnt);
1834 if (c->parentchannel != NULL) {
1835 pb = c->parentchannel->bufsoft;
1837 CHN_LOCK(c->parentchannel);
1838 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
1839 CHN_UNLOCK(c->parentchannel);
1841 if (c->direction == PCMDIR_PLAY) {
1842 limit = (pb != NULL) ?
1843 sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0;
1845 limit = (pb != NULL) ?
1846 sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0;
1850 if (c->flags & CHN_F_HAS_SIZE) {
1851 hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
1852 sndbuf_getalign(b));
1853 hblkcnt = round_pow2(sndbuf_getblkcnt(bs));
1855 chn_calclatency(c->direction, latency,
1857 sndbuf_getalign(b) * sndbuf_getspd(b),
1858 CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
1860 if ((hblksz << 1) > sndbuf_getmaxsize(b))
1861 hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1,
1862 sndbuf_getalign(b));
1864 while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) {
1871 hblksz -= hblksz % sndbuf_getalign(b);
1874 hblksz = sndbuf_getmaxsize(b) >> 1;
1875 hblksz -= hblksz % sndbuf_getalign(b);
1880 if (chn_usefrags == 0 ||
1881 CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
1882 hblksz, hblkcnt) != 0)
1883 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods,
1884 c->devinfo, hblksz));
1887 if (!CHN_EMPTY(c, children)) {
1888 nsblksz = round_blksz(
1889 sndbuf_xbytes(sndbuf_getblksz(b), b, bs),
1890 sndbuf_getalign(bs));
1891 nsblkcnt = sndbuf_getblkcnt(b);
1892 if (c->direction == PCMDIR_PLAY) {
1895 } while (nsblkcnt >= 2 &&
1896 nsblksz * nsblkcnt >= sblksz * sblkcnt);
1903 limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2;
1906 if (limit > CHN_2NDBUFMAXSIZE)
1907 limit = CHN_2NDBUFMAXSIZE;
1910 while (limit > 0 && (sblksz * sblkcnt) > limit) {
1917 while ((sblksz * sblkcnt) < limit)
1920 while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
1927 sblksz -= sblksz % sndbuf_getalign(bs);
1929 if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz ||
1930 sndbuf_getsize(bs) != (sblkcnt * sblksz)) {
1931 ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
1933 device_printf(c->dev, "%s(): Failed: %d %d\n",
1934 __func__, sblkcnt, sblksz);
1942 c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) /
1943 ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs));
1944 if (c->parentchannel != NULL)
1945 c->timeout = min(c->timeout, c->parentchannel->timeout);
1950 * OSSv4 docs: "By default OSS will set the low water level equal
1951 * to the fragment size which is optimal in most cases."
1953 c->lw = sndbuf_getblksz(bs);
1956 if (snd_verbose > 3)
1957 device_printf(c->dev, "%s(): %s (%s) timeout=%u "
1958 "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
1959 __func__, CHN_DIRSTR(c),
1960 (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
1962 sndbuf_getsize(b), sndbuf_getblksz(b),
1963 sndbuf_getblkcnt(b),
1964 sndbuf_getsize(bs), sndbuf_getblksz(bs),
1965 sndbuf_getblkcnt(bs), limit);
1971 chn_setlatency(struct pcm_channel *c, int latency)
1974 /* Destroy blksz/blkcnt, enforce latency profile. */
1975 return chn_resizebuf(c, latency, -1, 0);
1979 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
1982 /* Destroy latency profile, enforce blksz/blkcnt */
1983 return chn_resizebuf(c, -1, blkcnt, blksz);
1987 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
1989 struct pcmchan_caps *caps;
1990 uint32_t hwspeed, delta;
1995 if (speed < 1 || format == 0 || CHN_STARTED(c))
2001 caps = chn_getcaps(c);
2004 RANGE(hwspeed, caps->minspeed, caps->maxspeed);
2006 sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
2008 hwspeed = sndbuf_getspd(c->bufhard);
2010 delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
2012 if (delta <= feeder_rate_round)
2015 ret = feeder_chain(c);
2018 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo,
2019 sndbuf_getfmt(c->bufhard));
2022 ret = chn_resizebuf(c, -2, 0, 0);
2028 chn_setspeed(struct pcm_channel *c, uint32_t speed)
2030 uint32_t oldformat, oldspeed, format;
2035 if (c->format & AFMT_PASSTHROUGH)
2036 speed = AFMT_PASSTHROUGH_RATE;
2039 oldformat = c->format;
2040 oldspeed = c->speed;
2043 ret = chn_setparam(c, format, speed);
2045 if (snd_verbose > 3)
2046 device_printf(c->dev,
2047 "%s(): Setting speed %d failed, "
2048 "falling back to %d\n",
2049 __func__, speed, oldspeed);
2050 chn_setparam(c, c->format, oldspeed);
2057 chn_setformat(struct pcm_channel *c, uint32_t format)
2059 uint32_t oldformat, oldspeed, speed;
2062 /* XXX force stereo */
2063 if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
2064 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
2065 AFMT_PASSTHROUGH_EXTCHANNEL);
2068 oldformat = c->format;
2069 oldspeed = c->speed;
2072 ret = chn_setparam(c, format, speed);
2074 if (snd_verbose > 3)
2075 device_printf(c->dev,
2076 "%s(): Format change 0x%08x failed, "
2077 "falling back to 0x%08x\n",
2078 __func__, format, oldformat);
2079 chn_setparam(c, oldformat, oldspeed);
2086 chn_syncstate(struct pcm_channel *c)
2088 struct snddev_info *d;
2089 struct snd_mixer *m;
2091 d = (c != NULL) ? c->parentsnddev : NULL;
2092 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
2095 if (d == NULL || m == NULL)
2100 if (c->feederflags & (1 << FEEDER_VOLUME)) {
2102 int vol, pvol, left, right, center;
2104 if (c->direction == PCMDIR_PLAY &&
2105 (d->flags & SD_F_SOFTPCMVOL)) {
2106 /* CHN_UNLOCK(c); */
2107 vol = mix_get(m, SOUND_MIXER_PCM);
2108 parent = mix_getparent(m, SOUND_MIXER_PCM);
2109 if (parent != SOUND_MIXER_NONE)
2110 pvol = mix_get(m, parent);
2112 pvol = 100 | (100 << 8);
2115 vol = 100 | (100 << 8);
2120 device_printf(c->dev,
2121 "Soft PCM Volume: Failed to read pcm "
2123 vol = 100 | (100 << 8);
2127 device_printf(c->dev,
2128 "Soft PCM Volume: Failed to read parent "
2130 pvol = 100 | (100 << 8);
2133 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
2134 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
2135 center = (left + right) >> 1;
2137 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
2140 if (c->feederflags & (1 << FEEDER_EQ)) {
2141 struct pcm_feeder *f;
2142 int treble, bass, state;
2144 /* CHN_UNLOCK(c); */
2145 treble = mix_get(m, SOUND_MIXER_TREBLE);
2146 bass = mix_get(m, SOUND_MIXER_BASS);
2152 treble = ((treble & 0x7f) +
2153 ((treble >> 8) & 0x7f)) >> 1;
2158 bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
2160 f = chn_findfeeder(c, FEEDER_EQ);
2162 if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
2163 device_printf(c->dev,
2164 "EQ: Failed to set treble -- %d\n",
2166 if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
2167 device_printf(c->dev,
2168 "EQ: Failed to set bass -- %d\n",
2170 if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
2171 device_printf(c->dev,
2172 "EQ: Failed to set preamp -- %d\n",
2174 if (d->flags & SD_F_EQ_BYPASSED)
2175 state = FEEDEQ_BYPASS;
2176 else if (d->flags & SD_F_EQ_ENABLED)
2177 state = FEEDEQ_ENABLE;
2179 state = FEEDEQ_DISABLE;
2180 if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
2181 device_printf(c->dev,
2182 "EQ: Failed to set state -- %d\n", state);
2188 chn_trigger(struct pcm_channel *c, int go)
2190 struct snddev_info *d = c->parentsnddev;
2194 if (!PCMTRIG_COMMON(go))
2195 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
2197 if (go == c->trigger)
2200 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
2206 if (snd_verbose > 3)
2207 device_printf(c->dev,
2208 "%s() %s: calling go=0x%08x , "
2209 "prev=0x%08x\n", __func__, c->name, go,
2211 if (c->trigger != PCMTRIG_START) {
2215 CHN_INSERT_HEAD(d, c, channels.pcm.busy);
2223 if (snd_verbose > 3)
2224 device_printf(c->dev,
2225 "%s() %s: calling go=0x%08x , "
2226 "prev=0x%08x\n", __func__, c->name, go,
2228 if (c->trigger == PCMTRIG_START) {
2232 CHN_REMOVE(d, c, channels.pcm.busy);
2245 * @brief Queries sound driver for sample-aligned hardware buffer pointer index
2247 * This function obtains the hardware pointer location, then aligns it to
2248 * the current bytes-per-sample value before returning. (E.g., a channel
2249 * running in 16 bit stereo mode would require 4 bytes per sample, so a
2250 * hwptr value ranging from 32-35 would be returned as 32.)
2252 * @param c PCM channel context
2253 * @returns sample-aligned hardware buffer pointer index
2256 chn_getptr(struct pcm_channel *c)
2261 hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
2262 return (hwptr - (hwptr % sndbuf_getalign(c->bufhard)));
2265 struct pcmchan_caps *
2266 chn_getcaps(struct pcm_channel *c)
2269 return CHANNEL_GETCAPS(c->methods, c->devinfo);
2273 chn_getformats(struct pcm_channel *c)
2275 u_int32_t *fmtlist, fmts;
2278 fmtlist = chn_getcaps(c)->fmtlist;
2280 for (i = 0; fmtlist[i]; i++)
2283 /* report software-supported formats */
2284 if (!CHN_BITPERFECT(c) && report_soft_formats)
2285 fmts |= AFMT_CONVERTIBLE;
2287 return (AFMT_ENCODING(fmts));
2291 chn_notify(struct pcm_channel *c, u_int32_t flags)
2293 struct pcm_channel *ch;
2294 struct pcmchan_caps *caps;
2295 uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
2297 int dirty, err, run, nrun;
2301 if (CHN_EMPTY(c, children))
2307 * If the hwchan is running, we can't change its rate, format or
2310 run = (CHN_STARTED(c)) ? 1 : 0;
2312 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
2314 if (flags & CHN_N_RATE) {
2316 * XXX I'll make good use of this someday.
2317 * However this is currently being superseded by
2318 * the availability of CHN_F_VCHAN_DYNAMIC.
2322 if (flags & CHN_N_FORMAT) {
2324 * XXX I'll make good use of this someday.
2325 * However this is currently being superseded by
2326 * the availability of CHN_F_VCHAN_DYNAMIC.
2330 if (flags & CHN_N_VOLUME) {
2332 * XXX I'll make good use of this someday, though
2333 * soft volume control is currently pretty much
2338 if (flags & CHN_N_BLOCKSIZE) {
2340 * Set to default latency profile
2342 chn_setlatency(c, chn_latency);
2345 if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) {
2346 nrun = CHN_EMPTY(c, children.busy) ? 0 : 1;
2348 err = chn_start(c, 1);
2351 flags &= ~CHN_N_TRIGGER;
2354 if (flags & CHN_N_TRIGGER) {
2355 if (c->direction == PCMDIR_PLAY) {
2356 vchanformat = &c->parentsnddev->pvchanformat;
2357 vchanrate = &c->parentsnddev->pvchanrate;
2359 vchanformat = &c->parentsnddev->rvchanformat;
2360 vchanrate = &c->parentsnddev->rvchanrate;
2363 /* Dynamic Virtual Channel */
2364 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
2365 bestformat = *vchanformat;
2366 bestspeed = *vchanrate;
2374 caps = chn_getcaps(c);
2378 CHN_FOREACH(ch, c, children.busy) {
2380 if ((ch->format & AFMT_PASSTHROUGH) &&
2381 snd_fmtvalid(ch->format, caps->fmtlist)) {
2382 bestformat = ch->format;
2383 bestspeed = ch->speed;
2385 vpflags = CHN_F_PASSTHROUGH;
2389 if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
2390 if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
2391 bestspeed = ch->speed;
2392 RANGE(bestspeed, caps->minspeed,
2394 besthwformat = snd_fmtbest(ch->format,
2396 if (besthwformat != 0)
2397 bestformat = besthwformat;
2400 vpflags = CHN_F_EXCLUSIVE;
2404 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
2410 if (ch->speed > bestspeed) {
2411 bestspeed = ch->speed;
2412 RANGE(bestspeed, caps->minspeed,
2415 besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
2416 if (!(besthwformat & AFMT_VCHAN)) {
2421 if (AFMT_CHANNEL(besthwformat) >
2422 AFMT_CHANNEL(bestformat))
2423 bestformat = besthwformat;
2424 else if (AFMT_CHANNEL(besthwformat) ==
2425 AFMT_CHANNEL(bestformat) &&
2426 AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
2427 bestformat = besthwformat;
2432 if (bestformat == 0)
2433 bestformat = c->format;
2435 bestspeed = c->speed;
2437 if (bestformat != c->format || bestspeed != c->speed)
2440 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2441 c->flags |= vpflags;
2445 bestspeed = CHANNEL_SETSPEED(c->methods,
2446 c->devinfo, bestspeed);
2447 err = chn_reset(c, bestformat, bestspeed);
2449 if (err == 0 && dirty) {
2450 CHN_FOREACH(ch, c, children.busy) {
2452 if (VCHAN_SYNC_REQUIRED(ch))
2459 c->flags |= CHN_F_DIRTY;
2460 err = chn_start(c, 1);
2464 if (nrun && run && dirty) {
2466 bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
2468 err = chn_reset(c, bestformat, bestspeed);
2470 CHN_FOREACH(ch, c, children.busy) {
2472 if (VCHAN_SYNC_REQUIRED(ch))
2478 c->flags |= CHN_F_DIRTY;
2479 err = chn_start(c, 1);
2483 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
2484 (bestformat & AFMT_VCHAN)) {
2485 *vchanformat = bestformat;
2486 *vchanrate = bestspeed;
2490 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2491 bestformat = *vchanformat;
2492 bestspeed = *vchanrate;
2494 if (c->format != bestformat || c->speed != bestspeed)
2495 chn_reset(c, bestformat, bestspeed);
2503 * @brief Fetch array of supported discrete sample rates
2505 * Wrapper for CHANNEL_GETRATES. Please see channel_if.m:getrates() for
2506 * detailed information.
2508 * @note If the operation isn't supported, this function will just return 0
2509 * (no rates in the array), and *rates will be set to NULL. Callers
2510 * should examine rates @b only if this function returns non-zero.
2512 * @param c pcm channel to examine
2513 * @param rates pointer to array of integers; rate table will be recorded here
2515 * @return number of rates in the array pointed to be @c rates
2518 chn_getrates(struct pcm_channel *c, int **rates)
2520 KASSERT(rates != NULL, ("rates is null"));
2522 return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
2526 * @brief Remove channel from a sync group, if there is one.
2528 * This function is initially intended for the following conditions:
2529 * - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
2530 * - Closing a device. (A channel can't be destroyed if it's still in use.)
2532 * @note Before calling this function, the syncgroup list mutex must be
2533 * held. (Consider pcm_channel::sm protected by the SG list mutex
2534 * whether @c c is locked or not.)
2536 * @param c channel device to be started or closed
2537 * @returns If this channel was the only member of a group, the group ID
2538 * is returned to the caller so that the caller can release it
2539 * via free_unr() after giving up the syncgroup lock. Else it
2543 chn_syncdestroy(struct pcm_channel *c)
2545 struct pcmchan_syncmember *sm;
2546 struct pcmchan_syncgroup *sg;
2551 PCM_SG_LOCKASSERT(MA_OWNED);
2553 if (c->sm != NULL) {
2558 KASSERT(sg != NULL, ("syncmember has null parent"));
2560 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
2563 if (SLIST_EMPTY(&sg->members)) {
2564 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2573 #ifdef OSSV4_EXPERIMENT
2575 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
2578 return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);