2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
38 #include <dev/sound/pcm/sound.h>
39 #include <dev/sound/pcm/vchan.h>
41 #include "feeder_if.h"
43 SND_DECLARE_FILE("$FreeBSD$");
45 int report_soft_formats = 1;
46 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW,
47 &report_soft_formats, 0, "report software-emulated formats");
49 int report_soft_matrix = 1;
50 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_matrix, CTLFLAG_RW,
51 &report_soft_matrix, 0, "report software-emulated channel matrixing");
53 int chn_latency = CHN_LATENCY_DEFAULT;
56 sysctl_hw_snd_latency(SYSCTL_HANDLER_ARGS)
61 err = sysctl_handle_int(oidp, &val, 0, req);
62 if (err != 0 || req->newptr == NULL)
64 if (val < CHN_LATENCY_MIN || val > CHN_LATENCY_MAX)
71 SYSCTL_PROC(_hw_snd, OID_AUTO, latency,
72 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
73 sysctl_hw_snd_latency, "I",
74 "buffering latency (0=low ... 10=high)");
76 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
79 sysctl_hw_snd_latency_profile(SYSCTL_HANDLER_ARGS)
83 val = chn_latency_profile;
84 err = sysctl_handle_int(oidp, &val, 0, req);
85 if (err != 0 || req->newptr == NULL)
87 if (val < CHN_LATENCY_PROFILE_MIN || val > CHN_LATENCY_PROFILE_MAX)
90 chn_latency_profile = val;
94 SYSCTL_PROC(_hw_snd, OID_AUTO, latency_profile,
95 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
96 sysctl_hw_snd_latency_profile, "I",
97 "buffering latency profile (0=aggressive 1=safe)");
99 static int chn_timeout = CHN_TIMEOUT;
102 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS)
107 err = sysctl_handle_int(oidp, &val, 0, req);
108 if (err != 0 || req->newptr == NULL)
110 if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX)
117 SYSCTL_PROC(_hw_snd, OID_AUTO, timeout,
118 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
119 sysctl_hw_snd_timeout, "I",
120 "interrupt timeout (1 - 10) seconds");
122 static int chn_vpc_autoreset = 1;
123 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_autoreset, CTLFLAG_RWTUN,
124 &chn_vpc_autoreset, 0, "automatically reset channels volume to 0db");
126 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM;
129 chn_vpc_proc(int reset, int db)
131 struct snddev_info *d;
132 struct pcm_channel *c;
135 for (i = 0; pcm_devclass != NULL &&
136 i < devclass_get_maxunit(pcm_devclass); i++) {
137 d = devclass_get_softc(pcm_devclass, i);
138 if (!PCM_REGISTERED(d))
143 CHN_FOREACH(c, d, channels.pcm) {
145 CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db);
147 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
156 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS)
160 val = chn_vol_0db_pcm;
161 err = sysctl_handle_int(oidp, &val, 0, req);
162 if (err != 0 || req->newptr == NULL)
164 if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX)
167 chn_vol_0db_pcm = val;
168 chn_vpc_proc(0, val);
172 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_0db,
173 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
174 sysctl_hw_snd_vpc_0db, "I",
175 "0db relative level");
178 sysctl_hw_snd_vpc_reset(SYSCTL_HANDLER_ARGS)
183 err = sysctl_handle_int(oidp, &val, 0, req);
184 if (err != 0 || req->newptr == NULL || val == 0)
187 chn_vol_0db_pcm = SND_VOL_0DB_PCM;
188 chn_vpc_proc(1, SND_VOL_0DB_PCM);
192 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_reset,
193 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 0, sizeof(int),
194 sysctl_hw_snd_vpc_reset, "I",
195 "reset volume on all channels");
197 static int chn_usefrags = 0;
198 static int chn_syncdelay = -1;
200 SYSCTL_INT(_hw_snd, OID_AUTO, usefrags, CTLFLAG_RWTUN,
201 &chn_usefrags, 0, "prefer setfragments() over setblocksize()");
202 SYSCTL_INT(_hw_snd, OID_AUTO, syncdelay, CTLFLAG_RWTUN,
204 "append (0-1000) millisecond trailing buffer delay on each sync");
207 * @brief Channel sync group lock
209 * Clients should acquire this lock @b without holding any channel locks
210 * before touching syncgroups or the main syncgroup list.
212 struct mtx snd_pcm_syncgroups_mtx;
213 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF);
215 * @brief syncgroups' master list
217 * Each time a channel syncgroup is created, it's added to this list. This
218 * list should only be accessed with @sa snd_pcm_syncgroups_mtx held.
220 * See SNDCTL_DSP_SYNCGROUP for more information.
222 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups);
225 chn_lockinit(struct pcm_channel *c, int dir)
229 c->lock = snd_mtxcreate(c->name, "pcm play channel");
230 cv_init(&c->intr_cv, "pcmwr");
232 case PCMDIR_PLAY_VIRTUAL:
233 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
234 cv_init(&c->intr_cv, "pcmwrv");
237 c->lock = snd_mtxcreate(c->name, "pcm record channel");
238 cv_init(&c->intr_cv, "pcmrd");
240 case PCMDIR_REC_VIRTUAL:
241 c->lock = snd_mtxcreate(c->name, "pcm virtual record channel");
242 cv_init(&c->intr_cv, "pcmrdv");
245 panic("%s(): Invalid direction=%d", __func__, dir);
249 cv_init(&c->cv, "pcmchn");
253 chn_lockdestroy(struct pcm_channel *c)
257 CHN_BROADCAST(&c->cv);
258 CHN_BROADCAST(&c->intr_cv);
261 cv_destroy(&c->intr_cv);
263 snd_mtxfree(c->lock);
267 * @brief Determine channel is ready for I/O
269 * @retval 1 = ready for I/O
270 * @retval 0 = not ready for I/O
273 chn_polltrigger(struct pcm_channel *c)
275 struct snd_dbuf *bs = c->bufsoft;
280 if (c->flags & CHN_F_MMAP) {
281 if (sndbuf_getprevtotal(bs) < c->lw)
284 delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs);
286 if (c->direction == PCMDIR_PLAY)
287 delta = sndbuf_getfree(bs);
289 delta = sndbuf_getready(bs);
292 return ((delta < c->lw) ? 0 : 1);
296 chn_pollreset(struct pcm_channel *c)
300 sndbuf_updateprevtotal(c->bufsoft);
304 chn_wakeup(struct pcm_channel *c)
307 struct pcm_channel *ch;
313 if (CHN_EMPTY(c, children.busy)) {
314 if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c))
315 selwakeuppri(sndbuf_getsel(bs), PRIBIO);
316 if (c->flags & CHN_F_SLEEPING) {
318 * Ok, I can just panic it right here since it is
319 * quite obvious that we never allow multiple waiters
320 * from userland. I'm too generous...
322 CHN_BROADCAST(&c->intr_cv);
325 CHN_FOREACH(ch, c, children.busy) {
334 chn_sleep(struct pcm_channel *c, int timeout)
340 if (c->flags & CHN_F_DEAD)
343 c->flags |= CHN_F_SLEEPING;
344 ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout);
345 c->flags &= ~CHN_F_SLEEPING;
347 return ((c->flags & CHN_F_DEAD) ? EINVAL : ret);
351 * chn_dmaupdate() tracks the status of a dma transfer,
356 chn_dmaupdate(struct pcm_channel *c)
358 struct snd_dbuf *b = c->bufhard;
359 unsigned int delta, old, hwptr, amt;
361 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
364 old = sndbuf_gethwptr(b);
365 hwptr = chn_getptr(c);
366 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
367 sndbuf_sethwptr(b, hwptr);
369 if (c->direction == PCMDIR_PLAY) {
370 amt = min(delta, sndbuf_getready(b));
371 amt -= amt % sndbuf_getalign(b);
373 sndbuf_dispose(b, NULL, amt);
375 amt = min(delta, sndbuf_getfree(b));
376 amt -= amt % sndbuf_getalign(b);
378 sndbuf_acquire(b, NULL, amt);
380 if (snd_verbose > 3 && CHN_STARTED(c) && delta == 0) {
381 device_printf(c->dev, "WARNING: %s DMA completion "
382 "too fast/slow ! hwptr=%u, old=%u "
383 "delta=%u amt=%u ready=%u free=%u\n",
384 CHN_DIRSTR(c), hwptr, old, delta, amt,
385 sndbuf_getready(b), sndbuf_getfree(b));
392 chn_wrfeed(struct pcm_channel *c)
394 struct snd_dbuf *b = c->bufhard;
395 struct snd_dbuf *bs = c->bufsoft;
396 unsigned int amt, want, wasfree;
400 if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING))
401 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
403 wasfree = sndbuf_getfree(b);
404 want = min(sndbuf_getsize(b),
405 imax(0, sndbuf_xbytes(sndbuf_getsize(bs), bs, b) -
406 sndbuf_getready(b)));
407 amt = min(wasfree, want);
409 sndbuf_feed(bs, b, c, c->feeder, amt);
412 * Possible xruns. There should be no empty space left in buffer.
414 if (sndbuf_getready(b) < want)
417 if (sndbuf_getfree(b) < wasfree)
423 chn_wrupdate(struct pcm_channel *c)
427 KASSERT(c->direction == PCMDIR_PLAY, ("%s(): bad channel", __func__));
429 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
433 /* tell the driver we've updated the primary buffer */
434 chn_trigger(c, PCMTRIG_EMLDMAWR);
439 chn_wrintr(struct pcm_channel *c)
443 /* update pointers in primary buffer */
445 /* ...and feed from secondary to primary */
447 /* tell the driver we've updated the primary buffer */
448 chn_trigger(c, PCMTRIG_EMLDMAWR);
452 * user write routine - uiomove data into secondary buffer, trigger if necessary
453 * if blocking, sleep, rinse and repeat.
455 * called externally, so must handle locking
459 chn_write(struct pcm_channel *c, struct uio *buf)
461 struct snd_dbuf *bs = c->bufsoft;
463 int ret, timeout, sz, t, p;
468 timeout = chn_timeout * hz;
470 while (ret == 0 && buf->uio_resid > 0) {
471 sz = min(buf->uio_resid, sndbuf_getfree(bs));
474 * The following assumes that the free space in
475 * the buffer can never be less around the
476 * unlock-uiomove-lock sequence.
478 while (ret == 0 && sz > 0) {
479 p = sndbuf_getfreeptr(bs);
480 t = min(sz, sndbuf_getsize(bs) - p);
481 off = sndbuf_getbufofs(bs, p);
483 ret = uiomove(off, t, buf);
486 sndbuf_acquire(bs, NULL, t);
489 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
490 ret = chn_start(c, 0);
492 c->flags |= CHN_F_DEAD;
494 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) {
496 * @todo Evaluate whether EAGAIN is truly desirable.
497 * 4Front drivers behave like this, but I'm
498 * not sure if it at all violates the "write
499 * should be allowed to block" model.
501 * The idea is that, while set with CHN_F_NOTRIGGER,
502 * a channel isn't playing, *but* without this we
503 * end up with "interrupt timeout / channel dead".
507 ret = chn_sleep(c, timeout);
510 c->flags |= CHN_F_DEAD;
511 device_printf(c->dev, "%s(): %s: "
512 "play interrupt timeout, channel dead\n",
514 } else if (ret == ERESTART || ret == EINTR)
515 c->flags |= CHN_F_ABORTING;
523 * Feed new data from the read buffer. Can be called in the bottom half.
526 chn_rdfeed(struct pcm_channel *c)
528 struct snd_dbuf *b = c->bufhard;
529 struct snd_dbuf *bs = c->bufsoft;
534 if (c->flags & CHN_F_MMAP)
535 sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
537 amt = sndbuf_getfree(bs);
539 sndbuf_feed(b, bs, c, c->feeder, amt);
541 amt = sndbuf_getready(b);
544 sndbuf_dispose(b, NULL, amt);
547 if (sndbuf_getready(bs) > 0)
553 chn_rdupdate(struct pcm_channel *c)
557 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
559 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
561 chn_trigger(c, PCMTRIG_EMLDMARD);
567 /* read interrupt routine. Must be called with interrupts blocked. */
569 chn_rdintr(struct pcm_channel *c)
573 /* tell the driver to update the primary buffer if non-dma */
574 chn_trigger(c, PCMTRIG_EMLDMARD);
575 /* update pointers in primary buffer */
577 /* ...and feed from primary to secondary */
582 * user read routine - trigger if necessary, uiomove data from secondary buffer
583 * if blocking, sleep, rinse and repeat.
585 * called externally, so must handle locking
589 chn_read(struct pcm_channel *c, struct uio *buf)
591 struct snd_dbuf *bs = c->bufsoft;
593 int ret, timeout, sz, t, p;
597 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
598 ret = chn_start(c, 0);
600 c->flags |= CHN_F_DEAD;
606 timeout = chn_timeout * hz;
608 while (ret == 0 && buf->uio_resid > 0) {
609 sz = min(buf->uio_resid, sndbuf_getready(bs));
612 * The following assumes that the free space in
613 * the buffer can never be less around the
614 * unlock-uiomove-lock sequence.
616 while (ret == 0 && sz > 0) {
617 p = sndbuf_getreadyptr(bs);
618 t = min(sz, sndbuf_getsize(bs) - p);
619 off = sndbuf_getbufofs(bs, p);
621 ret = uiomove(off, t, buf);
624 sndbuf_dispose(bs, NULL, t);
627 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER))
630 ret = chn_sleep(c, timeout);
633 c->flags |= CHN_F_DEAD;
634 device_printf(c->dev, "%s(): %s: "
635 "record interrupt timeout, channel dead\n",
637 } else if (ret == ERESTART || ret == EINTR)
638 c->flags |= CHN_F_ABORTING;
646 chn_intr_locked(struct pcm_channel *c)
653 if (c->direction == PCMDIR_PLAY)
660 chn_intr(struct pcm_channel *c)
663 if (CHN_LOCKOWNED(c)) {
674 chn_start(struct pcm_channel *c, int force)
677 struct snd_dbuf *b = c->bufhard;
678 struct snd_dbuf *bs = c->bufsoft;
682 /* if we're running, or if we're prevented from triggering, bail */
683 if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force))
692 if (c->direction == PCMDIR_REC) {
693 i = sndbuf_getfree(bs);
694 j = (i > 0) ? 1 : sndbuf_getready(b);
696 if (sndbuf_getfree(bs) == 0) {
702 pb = CHN_BUF_PARENT(c, b);
703 i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb);
704 j = sndbuf_getalign(pb);
707 if (snd_verbose > 3 && CHN_EMPTY(c, children))
708 device_printf(c->dev, "%s(): %s (%s) threshold "
709 "i=%d j=%d\n", __func__, CHN_DIRSTR(c),
710 (c->flags & CHN_F_VIRTUAL) ? "virtual" :
715 c->flags |= CHN_F_TRIGGERED;
717 if (c->flags & CHN_F_CLOSING)
724 if (c->parentchannel == NULL) {
725 if (c->direction == PCMDIR_PLAY)
726 sndbuf_fillsilence_rl(b,
727 sndbuf_xbytes(sndbuf_getsize(bs), bs, b));
729 device_printf(c->dev,
730 "%s(): %s starting! (%s/%s) "
731 "(ready=%d force=%d i=%d j=%d "
732 "intrtimeout=%u latency=%dms)\n",
734 (c->flags & CHN_F_HAS_VCHAN) ?
735 "VCHAN PARENT" : "HW", CHN_DIRSTR(c),
736 (c->flags & CHN_F_CLOSING) ? "closing" :
739 force, i, j, c->timeout,
740 (sndbuf_getsize(b) * 1000) /
741 (sndbuf_getalign(b) * sndbuf_getspd(b)));
743 err = chn_trigger(c, PCMTRIG_START);
750 chn_resetbuf(struct pcm_channel *c)
752 struct snd_dbuf *b = c->bufhard;
753 struct snd_dbuf *bs = c->bufsoft;
761 * chn_sync waits until the space in the given channel goes above
762 * a threshold. The threshold is checked against fl or rl respectively.
763 * Assume that the condition can become true, do not check here...
766 chn_sync(struct pcm_channel *c, int threshold)
768 struct snd_dbuf *b, *bs;
769 int ret, count, hcount, minflush, resid, residp, syncdelay, blksz;
774 if (c->direction != PCMDIR_PLAY)
779 if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) ||
780 (threshold < 1 && sndbuf_getready(bs) < 1))
783 /* if we haven't yet started and nothing is buffered, else start*/
784 if (CHN_STOPPED(c)) {
785 if (threshold > 0 || sndbuf_getready(bs) > 0) {
786 ret = chn_start(c, 1);
793 b = CHN_BUF_PARENT(c, c->bufhard);
795 minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs);
797 syncdelay = chn_syncdelay;
799 if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0))
800 minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs);
803 * Append (0-1000) millisecond trailing buffer (if needed)
804 * for slower / high latency hardwares (notably USB audio)
805 * to avoid audible truncation.
808 minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) *
809 ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000;
811 minflush -= minflush % sndbuf_getalign(bs);
814 threshold = min(minflush, sndbuf_getfree(bs));
815 sndbuf_clear(bs, threshold);
816 sndbuf_acquire(bs, NULL, threshold);
817 minflush -= threshold;
820 resid = sndbuf_getready(bs);
822 blksz = sndbuf_getblksz(b);
824 device_printf(c->dev,
825 "%s(): WARNING: blksz < 1 ! maxsize=%d [%d/%d/%d]\n",
826 __func__, sndbuf_getmaxsize(b), sndbuf_getsize(b),
827 sndbuf_getblksz(b), sndbuf_getblkcnt(b));
828 if (sndbuf_getblkcnt(b) > 0)
829 blksz = sndbuf_getsize(b) / sndbuf_getblkcnt(b);
833 count = sndbuf_xbytes(minflush + resid, bs, b) / blksz;
838 device_printf(c->dev, "%s(): [begin] timeout=%d count=%d "
839 "minflush=%d resid=%d\n", __func__, c->timeout, count,
842 cflag = c->flags & CHN_F_CLOSING;
843 c->flags |= CHN_F_CLOSING;
844 while (count > 0 && (resid > 0 || minflush > 0)) {
845 ret = chn_sleep(c, c->timeout);
846 if (ret == ERESTART || ret == EINTR) {
847 c->flags |= CHN_F_ABORTING;
849 } else if (ret == 0 || ret == EAGAIN) {
850 resid = sndbuf_getready(bs);
851 if (resid == residp) {
854 device_printf(c->dev,
855 "%s(): [stalled] timeout=%d "
856 "count=%d hcount=%d "
857 "resid=%d minflush=%d\n",
858 __func__, c->timeout, count,
859 hcount, resid, minflush);
860 } else if (resid < residp && count < hcount) {
863 device_printf(c->dev,
864 "%s((): [resume] timeout=%d "
865 "count=%d hcount=%d "
866 "resid=%d minflush=%d\n",
867 __func__, c->timeout, count,
868 hcount, resid, minflush);
870 if (minflush > 0 && sndbuf_getfree(bs) > 0) {
871 threshold = min(minflush,
873 sndbuf_clear(bs, threshold);
874 sndbuf_acquire(bs, NULL, threshold);
875 resid = sndbuf_getready(bs);
876 minflush -= threshold;
882 c->flags &= ~CHN_F_CLOSING;
886 device_printf(c->dev,
887 "%s(): timeout=%d count=%d hcount=%d resid=%d residp=%d "
888 "minflush=%d ret=%d\n",
889 __func__, c->timeout, count, hcount, resid, residp,
895 /* called externally, handle locking */
897 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
899 struct snd_dbuf *bs = c->bufsoft;
904 if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) {
905 ret = chn_start(c, 1);
911 if (chn_polltrigger(c)) {
915 selrecord(td, sndbuf_getsel(bs));
921 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
922 * it returns the number of bytes that have not been transferred.
924 * called from: dsp_close, dsp_ioctl, with channel locked
927 chn_abort(struct pcm_channel *c)
930 struct snd_dbuf *b = c->bufhard;
931 struct snd_dbuf *bs = c->bufsoft;
936 c->flags |= CHN_F_ABORTING;
938 c->flags &= ~CHN_F_TRIGGERED;
939 /* kill the channel */
940 chn_trigger(c, PCMTRIG_ABORT);
942 if (!(c->flags & CHN_F_VIRTUAL))
944 missing = sndbuf_getready(bs);
946 c->flags &= ~CHN_F_ABORTING;
951 * this routine tries to flush the dma transfer. It is called
952 * on a close of a playback channel.
953 * first, if there is data in the buffer, but the dma has not yet
954 * begun, we need to start it.
955 * next, we wait for the play buffer to drain
956 * finally, we stop the dma.
958 * called from: dsp_close, not valid for record channels.
962 chn_flush(struct pcm_channel *c)
964 struct snd_dbuf *b = c->bufhard;
967 KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
968 DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags));
970 c->flags |= CHN_F_CLOSING;
972 c->flags &= ~CHN_F_TRIGGERED;
973 /* kill the channel */
974 chn_trigger(c, PCMTRIG_ABORT);
977 c->flags &= ~CHN_F_CLOSING;
982 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist)
986 for (i = 0; fmtlist[i] != 0; i++) {
987 if (fmt == fmtlist[i] ||
988 ((fmt & AFMT_PASSTHROUGH) &&
989 (AFMT_ENCODING(fmt) & fmtlist[i])))
996 static const struct {
997 char *name, *alias1, *alias2;
1000 { "alaw", NULL, NULL, AFMT_A_LAW },
1001 { "mulaw", NULL, NULL, AFMT_MU_LAW },
1002 { "u8", "8", NULL, AFMT_U8 },
1003 { "s8", NULL, NULL, AFMT_S8 },
1004 #if BYTE_ORDER == LITTLE_ENDIAN
1005 { "s16le", "s16", "16", AFMT_S16_LE },
1006 { "s16be", NULL, NULL, AFMT_S16_BE },
1008 { "s16le", NULL, NULL, AFMT_S16_LE },
1009 { "s16be", "s16", "16", AFMT_S16_BE },
1011 { "u16le", NULL, NULL, AFMT_U16_LE },
1012 { "u16be", NULL, NULL, AFMT_U16_BE },
1013 { "s24le", NULL, NULL, AFMT_S24_LE },
1014 { "s24be", NULL, NULL, AFMT_S24_BE },
1015 { "u24le", NULL, NULL, AFMT_U24_LE },
1016 { "u24be", NULL, NULL, AFMT_U24_BE },
1017 #if BYTE_ORDER == LITTLE_ENDIAN
1018 { "s32le", "s32", "32", AFMT_S32_LE },
1019 { "s32be", NULL, NULL, AFMT_S32_BE },
1021 { "s32le", NULL, NULL, AFMT_S32_LE },
1022 { "s32be", "s32", "32", AFMT_S32_BE },
1024 { "u32le", NULL, NULL, AFMT_U32_LE },
1025 { "u32be", NULL, NULL, AFMT_U32_BE },
1026 { "ac3", NULL, NULL, AFMT_AC3 },
1027 { NULL, NULL, NULL, 0 }
1031 snd_str2afmt(const char *req)
1039 memset(b1, 0, sizeof(b1));
1040 memset(b2, 0, sizeof(b2));
1042 i = sscanf(req, "%5[^:]:%6s", b1, b2);
1045 if (strlen(req) != strlen(b1))
1047 strlcpy(b2, "2.0", sizeof(b2));
1048 } else if (i == 2) {
1049 if (strlen(req) != (strlen(b1) + 1 + strlen(b2)))
1054 i = sscanf(b2, "%d.%d", &ch, &ext);
1057 if (strcasecmp(b2, "mono") == 0) {
1060 } else if (strcasecmp(b2, "stereo") == 0) {
1063 } else if (strcasecmp(b2, "quad") == 0) {
1068 } else if (i == 1) {
1069 if (ch < 1 || ch > AFMT_CHANNEL_MAX)
1072 } else if (i == 2) {
1073 if (ext < 0 || ext > AFMT_EXTCHANNEL_MAX)
1075 if (ch < 1 || (ch + ext) > AFMT_CHANNEL_MAX)
1080 for (i = 0; afmt_tab[i].name != NULL; i++) {
1081 if (strcasecmp(afmt_tab[i].name, b1) != 0) {
1082 if (afmt_tab[i].alias1 == NULL)
1084 if (strcasecmp(afmt_tab[i].alias1, b1) != 0) {
1085 if (afmt_tab[i].alias2 == NULL)
1087 if (strcasecmp(afmt_tab[i].alias2, b1) != 0)
1092 return (SND_FORMAT(afmt_tab[i].afmt, ch + ext, ext));
1094 /* not a valid format */
1099 snd_afmt2str(uint32_t afmt, char *buf, size_t len)
1106 if (buf == NULL || len < AFMTSTR_LEN)
1109 memset(buf, 0, len);
1111 enc = AFMT_ENCODING(afmt);
1112 ch = AFMT_CHANNEL(afmt);
1113 ext = AFMT_EXTCHANNEL(afmt);
1114 /* check there is at least one channel */
1117 for (i = 0; afmt_tab[i].name != NULL; i++) {
1118 if (enc != afmt_tab[i].afmt)
1121 snprintf(buf, len, "%s:%d.%d",
1122 afmt_tab[i].name, ch - ext, ext);
1123 return (SND_FORMAT(enc, ch, ext));
1129 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
1135 c->flags &= CHN_F_RESET;
1140 c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ?
1141 CHN_F_BITPERFECT : 0;
1143 r = CHANNEL_RESET(c->methods, c->devinfo);
1144 if (r == 0 && fmt != 0 && spd != 0) {
1145 r = chn_setparam(c, fmt, spd);
1149 if (r == 0 && fmt != 0)
1150 r = chn_setformat(c, fmt);
1151 if (r == 0 && spd != 0)
1152 r = chn_setspeed(c, spd);
1154 r = chn_setlatency(c, chn_latency);
1157 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
1163 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
1165 struct feeder_class *fc;
1166 struct snd_dbuf *b, *bs;
1169 if (chn_timeout < CHN_TIMEOUT_MIN || chn_timeout > CHN_TIMEOUT_MAX)
1170 chn_timeout = CHN_TIMEOUT;
1172 chn_lockinit(c, dir);
1176 CHN_INIT(c, children);
1177 CHN_INIT(c, children.busy);
1184 b = sndbuf_create(c->dev, c->name, "primary", c);
1187 bs = sndbuf_create(c->dev, c->name, "secondary", c);
1194 fc = feeder_getclass(NULL);
1197 if (chn_addfeeder(c, fc, NULL))
1201 * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
1202 * with the channel unlocked because they are also called
1203 * from driver methods that don't know about locking
1206 sndbuf_setup(bs, NULL, 0);
1213 c->format = SND_FORMAT(AFMT_U8, 1, 0);
1214 c->speed = DSP_DEFAULT_SPEED;
1216 c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
1217 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1219 for (i = 0; i < SND_CHN_T_MAX; i++) {
1220 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
1223 c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
1224 c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
1226 memset(c->muted, 0, sizeof(c->muted));
1228 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
1231 CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() malloc() call */
1232 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
1234 if (c->devinfo == NULL)
1238 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
1242 c->direction = direction;
1244 sndbuf_setfmt(b, c->format);
1245 sndbuf_setspd(b, c->speed);
1246 sndbuf_setfmt(bs, c->format);
1247 sndbuf_setspd(bs, c->speed);
1250 * @todo Should this be moved somewhere else? The primary buffer
1251 * is allocated by the driver or via DMA map setup, and tmpbuf
1252 * seems to only come into existence in sndbuf_resize().
1254 if (c->direction == PCMDIR_PLAY) {
1255 bs->sl = sndbuf_getmaxsize(bs);
1256 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_NOWAIT);
1257 if (bs->shadbuf == NULL) {
1267 if (CHANNEL_FREE(c->methods, c->devinfo))
1275 c->flags |= CHN_F_DEAD;
1285 chn_kill(struct pcm_channel *c)
1287 struct snd_dbuf *b = c->bufhard;
1288 struct snd_dbuf *bs = c->bufsoft;
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;
1308 /* XXX Obsolete. Use *_matrix() variant instead. */
1310 chn_setvolume(struct pcm_channel *c, int left, int right)
1314 ret = chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FL, left);
1315 ret |= chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FR,
1322 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
1329 for (i = 0; i < SND_CHN_T_MAX; i++) {
1330 if ((1 << i) & SND_CHN_LEFT_MASK)
1331 ret |= chn_setvolume_matrix(c, vc, i, left);
1332 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1333 ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
1335 ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
1342 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
1346 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1347 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1348 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
1349 vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
1350 (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
1351 ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
1352 __func__, c, vc, vt, val));
1360 c->volume[vc][vt] = val;
1363 * Do relative calculation here and store it into class + 1
1364 * to ease the job of feeder_volume.
1366 if (vc == SND_VOL_C_MASTER) {
1367 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1368 vc += SND_VOL_C_STEP)
1369 c->volume[SND_VOL_C_VAL(vc)][vt] =
1370 SND_VOL_CALC_VAL(c->volume, vc, vt);
1371 } else if (vc & 1) {
1372 if (vt == SND_CHN_T_VOL_0DB)
1373 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1374 i += SND_CHN_T_STEP) {
1375 c->volume[SND_VOL_C_VAL(vc)][i] =
1376 SND_VOL_CALC_VAL(c->volume, vc, i);
1379 c->volume[SND_VOL_C_VAL(vc)][vt] =
1380 SND_VOL_CALC_VAL(c->volume, vc, vt);
1387 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
1389 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1390 (vt == SND_CHN_T_VOL_0DB ||
1391 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1392 ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
1393 __func__, c, vc, vt));
1396 return (c->volume[vc][vt]);
1400 chn_setmute_multi(struct pcm_channel *c, int vc, int mute)
1406 for (i = 0; i < SND_CHN_T_MAX; i++) {
1407 if ((1 << i) & SND_CHN_LEFT_MASK)
1408 ret |= chn_setmute_matrix(c, vc, i, mute);
1409 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1410 ret |= chn_setmute_matrix(c, vc, i, mute) << 8;
1412 ret |= chn_setmute_matrix(c, vc, i, mute) << 16;
1418 chn_setmute_matrix(struct pcm_channel *c, int vc, int vt, int mute)
1422 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1423 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1424 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1425 ("%s(): invalid mute matrix c=%p vc=%d vt=%d mute=%d",
1426 __func__, c, vc, vt, mute));
1432 c->muted[vc][vt] = mute;
1435 * Do relative calculation here and store it into class + 1
1436 * to ease the job of feeder_volume.
1438 if (vc == SND_VOL_C_MASTER) {
1439 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1440 vc += SND_VOL_C_STEP)
1441 c->muted[SND_VOL_C_VAL(vc)][vt] = mute;
1442 } else if (vc & 1) {
1443 if (vt == SND_CHN_T_VOL_0DB) {
1444 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1445 i += SND_CHN_T_STEP) {
1446 c->muted[SND_VOL_C_VAL(vc)][i] = mute;
1449 c->muted[SND_VOL_C_VAL(vc)][vt] = mute;
1456 chn_getmute_matrix(struct pcm_channel *c, int vc, int vt)
1458 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1459 (vt == SND_CHN_T_VOL_0DB ||
1460 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1461 ("%s(): invalid mute matrix c=%p vc=%d vt=%d",
1462 __func__, c, vc, vt));
1465 return (c->muted[vc][vt]);
1468 struct pcmchan_matrix *
1469 chn_getmatrix(struct pcm_channel *c)
1472 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1475 if (!(c->format & AFMT_CONVERTIBLE))
1478 return (&c->matrix);
1482 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
1485 KASSERT(c != NULL && m != NULL,
1486 ("%s(): NULL channel or matrix", __func__));
1489 if (!(c->format & AFMT_CONVERTIBLE))
1493 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1495 return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
1499 * XXX chn_oss_* exists for the sake of compatibility.
1502 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
1505 KASSERT(c != NULL && map != NULL,
1506 ("%s(): NULL channel or map", __func__));
1509 if (!(c->format & AFMT_CONVERTIBLE))
1512 return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
1516 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
1518 struct pcmchan_matrix m;
1521 KASSERT(c != NULL && map != NULL,
1522 ("%s(): NULL channel or map", __func__));
1525 if (!(c->format & AFMT_CONVERTIBLE))
1529 ret = feeder_matrix_oss_set_channel_order(&m, map);
1533 return (chn_setmatrix(c, &m));
1536 #define SND_CHN_OSS_FRONT (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
1537 #define SND_CHN_OSS_SURR (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
1538 #define SND_CHN_OSS_CENTER_LFE (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
1539 #define SND_CHN_OSS_REAR (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
1542 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
1544 struct pcmchan_matrix *m;
1545 struct pcmchan_caps *caps;
1548 KASSERT(c != NULL && retmask != NULL,
1549 ("%s(): NULL channel or retmask", __func__));
1552 caps = chn_getcaps(c);
1553 if (caps == NULL || caps->fmtlist == NULL)
1556 for (i = 0; caps->fmtlist[i] != 0; i++) {
1557 format = caps->fmtlist[i];
1558 if (!(format & AFMT_CONVERTIBLE)) {
1559 *retmask |= DSP_BIND_SPDIF;
1562 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
1565 if (m->mask & SND_CHN_OSS_FRONT)
1566 *retmask |= DSP_BIND_FRONT;
1567 if (m->mask & SND_CHN_OSS_SURR)
1568 *retmask |= DSP_BIND_SURR;
1569 if (m->mask & SND_CHN_OSS_CENTER_LFE)
1570 *retmask |= DSP_BIND_CENTER_LFE;
1571 if (m->mask & SND_CHN_OSS_REAR)
1572 *retmask |= DSP_BIND_REAR;
1575 /* report software-supported binding mask */
1576 if (!CHN_BITPERFECT(c) && report_soft_matrix)
1577 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
1578 DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
1584 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
1588 KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
1589 ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
1592 if (force == 0 && chn_vpc_autoreset == 0)
1595 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
1596 CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
1600 round_pow2(u_int32_t v)
1609 ret = 1 << (ret - 1);
1616 round_blksz(u_int32_t v, int round)
1623 ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
1625 if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
1628 tmp = ret - (ret % round);
1629 while (tmp < 16 || tmp < round) {
1631 tmp = ret - (ret % round);
1638 * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
1639 * is to keep 2nd buffer short so that it doesn't cause long queue during
1642 * Latency reference table for 48khz stereo 16bit: (PLAY)
1644 * +---------+------------+-----------+------------+
1645 * | Latency | Blockcount | Blocksize | Buffersize |
1646 * +---------+------------+-----------+------------+
1647 * | 0 | 2 | 64 | 128 |
1648 * +---------+------------+-----------+------------+
1649 * | 1 | 4 | 128 | 512 |
1650 * +---------+------------+-----------+------------+
1651 * | 2 | 8 | 512 | 4096 |
1652 * +---------+------------+-----------+------------+
1653 * | 3 | 16 | 512 | 8192 |
1654 * +---------+------------+-----------+------------+
1655 * | 4 | 32 | 512 | 16384 |
1656 * +---------+------------+-----------+------------+
1657 * | 5 | 32 | 1024 | 32768 |
1658 * +---------+------------+-----------+------------+
1659 * | 6 | 16 | 2048 | 32768 |
1660 * +---------+------------+-----------+------------+
1661 * | 7 | 8 | 4096 | 32768 |
1662 * +---------+------------+-----------+------------+
1663 * | 8 | 4 | 8192 | 32768 |
1664 * +---------+------------+-----------+------------+
1665 * | 9 | 2 | 16384 | 32768 |
1666 * +---------+------------+-----------+------------+
1667 * | 10 | 2 | 32768 | 65536 |
1668 * +---------+------------+-----------+------------+
1670 * Recording need a different reference table. All we care is
1671 * gobbling up everything within reasonable buffering threshold.
1673 * Latency reference table for 48khz stereo 16bit: (REC)
1675 * +---------+------------+-----------+------------+
1676 * | Latency | Blockcount | Blocksize | Buffersize |
1677 * +---------+------------+-----------+------------+
1678 * | 0 | 512 | 32 | 16384 |
1679 * +---------+------------+-----------+------------+
1680 * | 1 | 256 | 64 | 16384 |
1681 * +---------+------------+-----------+------------+
1682 * | 2 | 128 | 128 | 16384 |
1683 * +---------+------------+-----------+------------+
1684 * | 3 | 64 | 256 | 16384 |
1685 * +---------+------------+-----------+------------+
1686 * | 4 | 32 | 512 | 16384 |
1687 * +---------+------------+-----------+------------+
1688 * | 5 | 32 | 1024 | 32768 |
1689 * +---------+------------+-----------+------------+
1690 * | 6 | 16 | 2048 | 32768 |
1691 * +---------+------------+-----------+------------+
1692 * | 7 | 8 | 4096 | 32768 |
1693 * +---------+------------+-----------+------------+
1694 * | 8 | 4 | 8192 | 32768 |
1695 * +---------+------------+-----------+------------+
1696 * | 9 | 2 | 16384 | 32768 |
1697 * +---------+------------+-----------+------------+
1698 * | 10 | 2 | 32768 | 65536 |
1699 * +---------+------------+-----------+------------+
1701 * Calculations for other data rate are entirely based on these reference
1702 * tables. For normal operation, Latency 5 seems give the best, well
1703 * balanced performance for typical workload. Anything below 5 will
1704 * eat up CPU to keep up with increasing context switches because of
1705 * shorter buffer space and usually require the application to handle it
1706 * aggresively through possibly real time programming technique.
1709 #define CHN_LATENCY_PBLKCNT_REF \
1710 {{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}, \
1711 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}}
1712 #define CHN_LATENCY_PBUFSZ_REF \
1713 {{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16}, \
1714 {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}}
1716 #define CHN_LATENCY_RBLKCNT_REF \
1717 {{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}, \
1718 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}}
1719 #define CHN_LATENCY_RBUFSZ_REF \
1720 {{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16}, \
1721 {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}}
1723 #define CHN_LATENCY_DATA_REF 192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
1726 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
1727 u_int32_t max, int *rblksz, int *rblkcnt)
1729 static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1730 CHN_LATENCY_PBLKCNT_REF;
1731 static int pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1732 CHN_LATENCY_PBUFSZ_REF;
1733 static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1734 CHN_LATENCY_RBLKCNT_REF;
1735 static int rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1736 CHN_LATENCY_RBUFSZ_REF;
1738 int lprofile, blksz, blkcnt;
1740 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
1741 bps < 1 || datarate < 1 ||
1742 !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
1744 *rblksz = CHN_2NDBUFMAXSIZE >> 1;
1745 if (rblkcnt != NULL)
1747 printf("%s(): FAILED dir=%d latency=%d bps=%d "
1748 "datarate=%u max=%u\n",
1749 __func__, dir, latency, bps, datarate, max);
1750 return CHN_2NDBUFMAXSIZE;
1753 lprofile = chn_latency_profile;
1755 if (dir == PCMDIR_PLAY) {
1756 blkcnt = pblkcnts[lprofile][latency];
1757 bufsz = pbufszs[lprofile][latency];
1759 blkcnt = rblkcnts[lprofile][latency];
1760 bufsz = rbufszs[lprofile][latency];
1763 bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
1767 blksz = round_blksz(bufsz >> blkcnt, bps);
1771 if (rblkcnt != NULL)
1772 *rblkcnt = 1 << blkcnt;
1774 return blksz << blkcnt;
1778 chn_resizebuf(struct pcm_channel *c, int latency,
1779 int blkcnt, int blksz)
1781 struct snd_dbuf *b, *bs, *pb;
1782 int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
1787 if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
1788 !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
1791 if (latency == -1) {
1793 latency = chn_latency;
1794 } else if (latency == -2) {
1795 latency = c->latency;
1796 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1797 latency = chn_latency;
1798 } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1801 c->latency = latency;
1807 if (!(blksz == 0 || blkcnt == -1) &&
1808 (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 ||
1809 (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
1812 chn_calclatency(c->direction, latency, sndbuf_getalign(bs),
1813 sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE,
1816 if (blksz == 0 || blkcnt == -1) {
1818 c->flags &= ~CHN_F_HAS_SIZE;
1819 if (c->flags & CHN_F_HAS_SIZE) {
1820 blksz = sndbuf_getblksz(bs);
1821 blkcnt = sndbuf_getblkcnt(bs);
1824 c->flags |= CHN_F_HAS_SIZE;
1826 if (c->flags & CHN_F_HAS_SIZE) {
1828 * The application has requested their own blksz/blkcnt.
1829 * Just obey with it, and let them toast alone. We can
1830 * clamp it to the nearest latency profile, but that would
1831 * defeat the purpose of having custom control. The least
1832 * we can do is round it to the nearest ^2 and align it.
1834 sblksz = round_blksz(blksz, sndbuf_getalign(bs));
1835 sblkcnt = round_pow2(blkcnt);
1838 if (c->parentchannel != NULL) {
1839 pb = c->parentchannel->bufsoft;
1841 CHN_LOCK(c->parentchannel);
1842 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
1843 CHN_UNLOCK(c->parentchannel);
1845 if (c->direction == PCMDIR_PLAY) {
1846 limit = (pb != NULL) ?
1847 sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0;
1849 limit = (pb != NULL) ?
1850 sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0;
1854 if (c->flags & CHN_F_HAS_SIZE) {
1855 hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
1856 sndbuf_getalign(b));
1857 hblkcnt = round_pow2(sndbuf_getblkcnt(bs));
1859 chn_calclatency(c->direction, latency,
1861 sndbuf_getalign(b) * sndbuf_getspd(b),
1862 CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
1864 if ((hblksz << 1) > sndbuf_getmaxsize(b))
1865 hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1,
1866 sndbuf_getalign(b));
1868 while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) {
1875 hblksz -= hblksz % sndbuf_getalign(b);
1878 hblksz = sndbuf_getmaxsize(b) >> 1;
1879 hblksz -= hblksz % sndbuf_getalign(b);
1884 if (chn_usefrags == 0 ||
1885 CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
1886 hblksz, hblkcnt) != 0)
1887 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods,
1888 c->devinfo, hblksz));
1891 if (!CHN_EMPTY(c, children)) {
1892 nsblksz = round_blksz(
1893 sndbuf_xbytes(sndbuf_getblksz(b), b, bs),
1894 sndbuf_getalign(bs));
1895 nsblkcnt = sndbuf_getblkcnt(b);
1896 if (c->direction == PCMDIR_PLAY) {
1899 } while (nsblkcnt >= 2 &&
1900 nsblksz * nsblkcnt >= sblksz * sblkcnt);
1907 limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2;
1910 if (limit > CHN_2NDBUFMAXSIZE)
1911 limit = CHN_2NDBUFMAXSIZE;
1914 while (limit > 0 && (sblksz * sblkcnt) > limit) {
1921 while ((sblksz * sblkcnt) < limit)
1924 while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
1931 sblksz -= sblksz % sndbuf_getalign(bs);
1933 if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz ||
1934 sndbuf_getsize(bs) != (sblkcnt * sblksz)) {
1935 ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
1937 device_printf(c->dev, "%s(): Failed: %d %d\n",
1938 __func__, sblkcnt, sblksz);
1946 c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) /
1947 ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs));
1948 if (c->parentchannel != NULL)
1949 c->timeout = min(c->timeout, c->parentchannel->timeout);
1954 * OSSv4 docs: "By default OSS will set the low water level equal
1955 * to the fragment size which is optimal in most cases."
1957 c->lw = sndbuf_getblksz(bs);
1960 if (snd_verbose > 3)
1961 device_printf(c->dev, "%s(): %s (%s) timeout=%u "
1962 "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
1963 __func__, CHN_DIRSTR(c),
1964 (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
1966 sndbuf_getsize(b), sndbuf_getblksz(b),
1967 sndbuf_getblkcnt(b),
1968 sndbuf_getsize(bs), sndbuf_getblksz(bs),
1969 sndbuf_getblkcnt(bs), limit);
1975 chn_setlatency(struct pcm_channel *c, int latency)
1978 /* Destroy blksz/blkcnt, enforce latency profile. */
1979 return chn_resizebuf(c, latency, -1, 0);
1983 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
1986 /* Destroy latency profile, enforce blksz/blkcnt */
1987 return chn_resizebuf(c, -1, blkcnt, blksz);
1991 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
1993 struct pcmchan_caps *caps;
1994 uint32_t hwspeed, delta;
1999 if (speed < 1 || format == 0 || CHN_STARTED(c))
2005 caps = chn_getcaps(c);
2008 RANGE(hwspeed, caps->minspeed, caps->maxspeed);
2010 sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
2012 hwspeed = sndbuf_getspd(c->bufhard);
2014 delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
2016 if (delta <= feeder_rate_round)
2019 ret = feeder_chain(c);
2022 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo,
2023 sndbuf_getfmt(c->bufhard));
2026 ret = chn_resizebuf(c, -2, 0, 0);
2032 chn_setspeed(struct pcm_channel *c, uint32_t speed)
2034 uint32_t oldformat, oldspeed, format;
2039 if (c->format & AFMT_PASSTHROUGH)
2040 speed = AFMT_PASSTHROUGH_RATE;
2043 oldformat = c->format;
2044 oldspeed = c->speed;
2047 ret = chn_setparam(c, format, speed);
2049 if (snd_verbose > 3)
2050 device_printf(c->dev,
2051 "%s(): Setting speed %d failed, "
2052 "falling back to %d\n",
2053 __func__, speed, oldspeed);
2054 chn_setparam(c, c->format, oldspeed);
2061 chn_setformat(struct pcm_channel *c, uint32_t format)
2063 uint32_t oldformat, oldspeed, speed;
2066 /* XXX force stereo */
2067 if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
2068 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
2069 AFMT_PASSTHROUGH_EXTCHANNEL);
2072 oldformat = c->format;
2073 oldspeed = c->speed;
2076 ret = chn_setparam(c, format, speed);
2078 if (snd_verbose > 3)
2079 device_printf(c->dev,
2080 "%s(): Format change 0x%08x failed, "
2081 "falling back to 0x%08x\n",
2082 __func__, format, oldformat);
2083 chn_setparam(c, oldformat, oldspeed);
2090 chn_syncstate(struct pcm_channel *c)
2092 struct snddev_info *d;
2093 struct snd_mixer *m;
2095 d = (c != NULL) ? c->parentsnddev : NULL;
2096 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
2099 if (d == NULL || m == NULL)
2104 if (c->feederflags & (1 << FEEDER_VOLUME)) {
2106 int vol, pvol, left, right, center;
2108 if (c->direction == PCMDIR_PLAY &&
2109 (d->flags & SD_F_SOFTPCMVOL)) {
2110 /* CHN_UNLOCK(c); */
2111 vol = mix_get(m, SOUND_MIXER_PCM);
2112 parent = mix_getparent(m, SOUND_MIXER_PCM);
2113 if (parent != SOUND_MIXER_NONE)
2114 pvol = mix_get(m, parent);
2116 pvol = 100 | (100 << 8);
2119 vol = 100 | (100 << 8);
2124 device_printf(c->dev,
2125 "Soft PCM Volume: Failed to read pcm "
2127 vol = 100 | (100 << 8);
2131 device_printf(c->dev,
2132 "Soft PCM Volume: Failed to read parent "
2134 pvol = 100 | (100 << 8);
2137 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
2138 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
2139 center = (left + right) >> 1;
2141 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
2144 if (c->feederflags & (1 << FEEDER_EQ)) {
2145 struct pcm_feeder *f;
2146 int treble, bass, state;
2148 /* CHN_UNLOCK(c); */
2149 treble = mix_get(m, SOUND_MIXER_TREBLE);
2150 bass = mix_get(m, SOUND_MIXER_BASS);
2156 treble = ((treble & 0x7f) +
2157 ((treble >> 8) & 0x7f)) >> 1;
2162 bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
2164 f = chn_findfeeder(c, FEEDER_EQ);
2166 if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
2167 device_printf(c->dev,
2168 "EQ: Failed to set treble -- %d\n",
2170 if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
2171 device_printf(c->dev,
2172 "EQ: Failed to set bass -- %d\n",
2174 if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
2175 device_printf(c->dev,
2176 "EQ: Failed to set preamp -- %d\n",
2178 if (d->flags & SD_F_EQ_BYPASSED)
2179 state = FEEDEQ_BYPASS;
2180 else if (d->flags & SD_F_EQ_ENABLED)
2181 state = FEEDEQ_ENABLE;
2183 state = FEEDEQ_DISABLE;
2184 if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
2185 device_printf(c->dev,
2186 "EQ: Failed to set state -- %d\n", state);
2192 chn_trigger(struct pcm_channel *c, int go)
2195 struct snd_dbuf *b = c->bufhard;
2197 struct snddev_info *d = c->parentsnddev;
2202 if (SND_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
2203 sndbuf_dmabounce(b);
2205 if (!PCMTRIG_COMMON(go))
2206 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
2208 if (go == c->trigger)
2211 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
2217 if (snd_verbose > 3)
2218 device_printf(c->dev,
2219 "%s() %s: calling go=0x%08x , "
2220 "prev=0x%08x\n", __func__, c->name, go,
2222 if (c->trigger != PCMTRIG_START) {
2226 CHN_INSERT_HEAD(d, c, channels.pcm.busy);
2234 if (snd_verbose > 3)
2235 device_printf(c->dev,
2236 "%s() %s: calling go=0x%08x , "
2237 "prev=0x%08x\n", __func__, c->name, go,
2239 if (c->trigger == PCMTRIG_START) {
2243 CHN_REMOVE(d, c, channels.pcm.busy);
2256 * @brief Queries sound driver for sample-aligned hardware buffer pointer index
2258 * This function obtains the hardware pointer location, then aligns it to
2259 * the current bytes-per-sample value before returning. (E.g., a channel
2260 * running in 16 bit stereo mode would require 4 bytes per sample, so a
2261 * hwptr value ranging from 32-35 would be returned as 32.)
2263 * @param c PCM channel context
2264 * @returns sample-aligned hardware buffer pointer index
2267 chn_getptr(struct pcm_channel *c)
2272 hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
2273 return (hwptr - (hwptr % sndbuf_getalign(c->bufhard)));
2276 struct pcmchan_caps *
2277 chn_getcaps(struct pcm_channel *c)
2280 return CHANNEL_GETCAPS(c->methods, c->devinfo);
2284 chn_getformats(struct pcm_channel *c)
2286 u_int32_t *fmtlist, fmts;
2289 fmtlist = chn_getcaps(c)->fmtlist;
2291 for (i = 0; fmtlist[i]; i++)
2294 /* report software-supported formats */
2295 if (!CHN_BITPERFECT(c) && report_soft_formats)
2296 fmts |= AFMT_CONVERTIBLE;
2298 return (AFMT_ENCODING(fmts));
2302 chn_notify(struct pcm_channel *c, u_int32_t flags)
2304 struct pcm_channel *ch;
2305 struct pcmchan_caps *caps;
2306 uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
2308 int dirty, err, run, nrun;
2312 if (CHN_EMPTY(c, children))
2318 * If the hwchan is running, we can't change its rate, format or
2321 run = (CHN_STARTED(c)) ? 1 : 0;
2323 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
2325 if (flags & CHN_N_RATE) {
2327 * XXX I'll make good use of this someday.
2328 * However this is currently being superseded by
2329 * the availability of CHN_F_VCHAN_DYNAMIC.
2333 if (flags & CHN_N_FORMAT) {
2335 * XXX I'll make good use of this someday.
2336 * However this is currently being superseded by
2337 * the availability of CHN_F_VCHAN_DYNAMIC.
2341 if (flags & CHN_N_VOLUME) {
2343 * XXX I'll make good use of this someday, though
2344 * soft volume control is currently pretty much
2349 if (flags & CHN_N_BLOCKSIZE) {
2351 * Set to default latency profile
2353 chn_setlatency(c, chn_latency);
2356 if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) {
2357 nrun = CHN_EMPTY(c, children.busy) ? 0 : 1;
2359 err = chn_start(c, 1);
2362 flags &= ~CHN_N_TRIGGER;
2365 if (flags & CHN_N_TRIGGER) {
2366 if (c->direction == PCMDIR_PLAY) {
2367 vchanformat = &c->parentsnddev->pvchanformat;
2368 vchanrate = &c->parentsnddev->pvchanrate;
2370 vchanformat = &c->parentsnddev->rvchanformat;
2371 vchanrate = &c->parentsnddev->rvchanrate;
2374 /* Dynamic Virtual Channel */
2375 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
2376 bestformat = *vchanformat;
2377 bestspeed = *vchanrate;
2385 caps = chn_getcaps(c);
2389 CHN_FOREACH(ch, c, children.busy) {
2391 if ((ch->format & AFMT_PASSTHROUGH) &&
2392 snd_fmtvalid(ch->format, caps->fmtlist)) {
2393 bestformat = ch->format;
2394 bestspeed = ch->speed;
2396 vpflags = CHN_F_PASSTHROUGH;
2400 if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
2401 if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
2402 bestspeed = ch->speed;
2403 RANGE(bestspeed, caps->minspeed,
2405 besthwformat = snd_fmtbest(ch->format,
2407 if (besthwformat != 0)
2408 bestformat = besthwformat;
2411 vpflags = CHN_F_EXCLUSIVE;
2415 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
2421 if (ch->speed > bestspeed) {
2422 bestspeed = ch->speed;
2423 RANGE(bestspeed, caps->minspeed,
2426 besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
2427 if (!(besthwformat & AFMT_VCHAN)) {
2432 if (AFMT_CHANNEL(besthwformat) >
2433 AFMT_CHANNEL(bestformat))
2434 bestformat = besthwformat;
2435 else if (AFMT_CHANNEL(besthwformat) ==
2436 AFMT_CHANNEL(bestformat) &&
2437 AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
2438 bestformat = besthwformat;
2443 if (bestformat == 0)
2444 bestformat = c->format;
2446 bestspeed = c->speed;
2448 if (bestformat != c->format || bestspeed != c->speed)
2451 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2452 c->flags |= vpflags;
2456 bestspeed = CHANNEL_SETSPEED(c->methods,
2457 c->devinfo, bestspeed);
2458 err = chn_reset(c, bestformat, bestspeed);
2460 if (err == 0 && dirty) {
2461 CHN_FOREACH(ch, c, children.busy) {
2463 if (VCHAN_SYNC_REQUIRED(ch))
2470 c->flags |= CHN_F_DIRTY;
2471 err = chn_start(c, 1);
2475 if (nrun && run && dirty) {
2477 bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
2479 err = chn_reset(c, bestformat, bestspeed);
2481 CHN_FOREACH(ch, c, children.busy) {
2483 if (VCHAN_SYNC_REQUIRED(ch))
2489 c->flags |= CHN_F_DIRTY;
2490 err = chn_start(c, 1);
2494 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
2495 (bestformat & AFMT_VCHAN)) {
2496 *vchanformat = bestformat;
2497 *vchanrate = bestspeed;
2501 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2502 bestformat = *vchanformat;
2503 bestspeed = *vchanrate;
2505 if (c->format != bestformat || c->speed != bestspeed)
2506 chn_reset(c, bestformat, bestspeed);
2514 * @brief Fetch array of supported discrete sample rates
2516 * Wrapper for CHANNEL_GETRATES. Please see channel_if.m:getrates() for
2517 * detailed information.
2519 * @note If the operation isn't supported, this function will just return 0
2520 * (no rates in the array), and *rates will be set to NULL. Callers
2521 * should examine rates @b only if this function returns non-zero.
2523 * @param c pcm channel to examine
2524 * @param rates pointer to array of integers; rate table will be recorded here
2526 * @return number of rates in the array pointed to be @c rates
2529 chn_getrates(struct pcm_channel *c, int **rates)
2531 KASSERT(rates != NULL, ("rates is null"));
2533 return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
2537 * @brief Remove channel from a sync group, if there is one.
2539 * This function is initially intended for the following conditions:
2540 * - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
2541 * - Closing a device. (A channel can't be destroyed if it's still in use.)
2543 * @note Before calling this function, the syncgroup list mutex must be
2544 * held. (Consider pcm_channel::sm protected by the SG list mutex
2545 * whether @c c is locked or not.)
2547 * @param c channel device to be started or closed
2548 * @returns If this channel was the only member of a group, the group ID
2549 * is returned to the caller so that the caller can release it
2550 * via free_unr() after giving up the syncgroup lock. Else it
2554 chn_syncdestroy(struct pcm_channel *c)
2556 struct pcmchan_syncmember *sm;
2557 struct pcmchan_syncgroup *sg;
2562 PCM_SG_LOCKASSERT(MA_OWNED);
2564 if (c->sm != NULL) {
2569 KASSERT(sg != NULL, ("syncmember has null parent"));
2571 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
2574 if (SLIST_EMPTY(&sg->members)) {
2575 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2584 #ifdef OSSV4_EXPERIMENT
2586 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
2589 return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);