2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
4 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
5 * Portions Copyright (c) Luigi Rizzo <luigi@FreeBSD.org> - 1997-99
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * 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 SND_DECLARE_FILE("$FreeBSD$");
43 int report_soft_formats = 1;
44 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_formats, CTLFLAG_RW,
45 &report_soft_formats, 1, "report software-emulated formats");
47 int report_soft_matrix = 1;
48 SYSCTL_INT(_hw_snd, OID_AUTO, report_soft_matrix, CTLFLAG_RW,
49 &report_soft_matrix, 1, "report software-emulated channel matrixing");
51 int chn_latency = CHN_LATENCY_DEFAULT;
52 TUNABLE_INT("hw.snd.latency", &chn_latency);
55 sysctl_hw_snd_latency(SYSCTL_HANDLER_ARGS)
60 err = sysctl_handle_int(oidp, &val, 0, req);
61 if (err != 0 || req->newptr == NULL)
63 if (val < CHN_LATENCY_MIN || val > CHN_LATENCY_MAX)
70 SYSCTL_PROC(_hw_snd, OID_AUTO, latency, CTLTYPE_INT | CTLFLAG_RW,
71 0, sizeof(int), sysctl_hw_snd_latency, "I",
72 "buffering latency (0=low ... 10=high)");
74 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
75 TUNABLE_INT("hw.snd.latency_profile", &chn_latency_profile);
78 sysctl_hw_snd_latency_profile(SYSCTL_HANDLER_ARGS)
82 val = chn_latency_profile;
83 err = sysctl_handle_int(oidp, &val, 0, req);
84 if (err != 0 || req->newptr == NULL)
86 if (val < CHN_LATENCY_PROFILE_MIN || val > CHN_LATENCY_PROFILE_MAX)
89 chn_latency_profile = val;
93 SYSCTL_PROC(_hw_snd, OID_AUTO, latency_profile, CTLTYPE_INT | CTLFLAG_RW,
94 0, sizeof(int), sysctl_hw_snd_latency_profile, "I",
95 "buffering latency profile (0=aggresive 1=safe)");
97 static int chn_timeout = CHN_TIMEOUT;
98 TUNABLE_INT("hw.snd.timeout", &chn_timeout);
101 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS)
106 err = sysctl_handle_int(oidp, &val, 0, req);
107 if (err != 0 || req->newptr == NULL)
109 if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX)
116 SYSCTL_PROC(_hw_snd, OID_AUTO, timeout, CTLTYPE_INT | CTLFLAG_RW,
117 0, sizeof(int), sysctl_hw_snd_timeout, "I",
118 "interrupt timeout (1 - 10) seconds");
121 static int chn_vpc_autoreset = 1;
122 TUNABLE_INT("hw.snd.vpc_autoreset", &chn_vpc_autoreset);
123 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_autoreset, CTLFLAG_RW,
124 &chn_vpc_autoreset, 0, "automatically reset channels volume to 0db");
126 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM;
127 TUNABLE_INT("hw.snd.vpc_0db", &chn_vol_0db_pcm);
130 chn_vpc_proc(int reset, int db)
132 struct snddev_info *d;
133 struct pcm_channel *c;
136 for (i = 0; pcm_devclass != NULL &&
137 i < devclass_get_maxunit(pcm_devclass); i++) {
138 d = devclass_get_softc(pcm_devclass, i);
139 if (!PCM_REGISTERED(d))
144 CHN_FOREACH(c, d, channels.pcm) {
146 CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db);
148 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
157 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS)
161 val = chn_vol_0db_pcm;
162 err = sysctl_handle_int(oidp, &val, 0, req);
163 if (err != 0 || req->newptr == NULL)
165 if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX)
168 chn_vol_0db_pcm = val;
169 chn_vpc_proc(0, val);
173 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_0db, CTLTYPE_INT | CTLFLAG_RW,
174 0, sizeof(int), 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, CTLTYPE_INT | CTLFLAG_RW,
193 0, sizeof(int), sysctl_hw_snd_vpc_reset, "I",
194 "reset volume on all channels");
196 static int chn_usefrags = 0;
197 TUNABLE_INT("hw.snd.usefrags", &chn_usefrags);
198 static int chn_syncdelay = -1;
199 TUNABLE_INT("hw.snd.syncdelay", &chn_syncdelay);
201 SYSCTL_INT(_hw_snd, OID_AUTO, usefrags, CTLFLAG_RW,
202 &chn_usefrags, 1, "prefer setfragments() over setblocksize()");
203 SYSCTL_INT(_hw_snd, OID_AUTO, syncdelay, CTLFLAG_RW,
205 "append (0-1000) millisecond trailing buffer delay on each sync");
209 * @brief Channel sync group lock
211 * Clients should acquire this lock @b without holding any channel locks
212 * before touching syncgroups or the main syncgroup list.
214 struct mtx snd_pcm_syncgroups_mtx;
215 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF);
217 * @brief syncgroups' master list
219 * Each time a channel syncgroup is created, it's added to this list. This
220 * list should only be accessed with @sa snd_pcm_syncgroups_mtx held.
222 * See SNDCTL_DSP_SYNCGROUP for more information.
224 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups);
227 chn_lockinit(struct pcm_channel *c, int dir)
231 c->lock = snd_mtxcreate(c->name, "pcm play channel");
232 cv_init(&c->intr_cv, "pcmwr");
234 case PCMDIR_PLAY_VIRTUAL:
235 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
236 cv_init(&c->intr_cv, "pcmwrv");
239 c->lock = snd_mtxcreate(c->name, "pcm record channel");
240 cv_init(&c->intr_cv, "pcmrd");
242 case PCMDIR_REC_VIRTUAL:
243 c->lock = snd_mtxcreate(c->name, "pcm virtual record channel");
244 cv_init(&c->intr_cv, "pcmrdv");
247 panic("%s(): Invalid direction=%d", __func__, dir);
251 cv_init(&c->cv, "pcmchn");
255 chn_lockdestroy(struct pcm_channel *c)
259 CHN_BROADCAST(&c->cv);
260 CHN_BROADCAST(&c->intr_cv);
263 cv_destroy(&c->intr_cv);
265 snd_mtxfree(c->lock);
269 * @brief Determine channel is ready for I/O
271 * @retval 1 = ready for I/O
272 * @retval 0 = not ready for I/O
275 chn_polltrigger(struct pcm_channel *c)
277 struct snd_dbuf *bs = c->bufsoft;
282 if (c->flags & CHN_F_MMAP) {
283 if (sndbuf_getprevtotal(bs) < c->lw)
286 delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs);
288 if (c->direction == PCMDIR_PLAY)
289 delta = sndbuf_getfree(bs);
291 delta = sndbuf_getready(bs);
294 return ((delta < c->lw) ? 0 : 1);
298 chn_pollreset(struct pcm_channel *c)
302 sndbuf_updateprevtotal(c->bufsoft);
306 chn_wakeup(struct pcm_channel *c)
309 struct pcm_channel *ch;
315 if (CHN_EMPTY(c, children.busy)) {
316 if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c))
317 selwakeuppri(sndbuf_getsel(bs), PRIBIO);
318 if (c->flags & CHN_F_SLEEPING) {
320 * Ok, I can just panic it right here since it is
321 * quite obvious that we never allow multiple waiters
322 * from userland. I'm too generous...
324 CHN_BROADCAST(&c->intr_cv);
327 CHN_FOREACH(ch, c, children.busy) {
336 chn_sleep(struct pcm_channel *c, int timeout)
342 if (c->flags & CHN_F_DEAD)
345 c->flags |= CHN_F_SLEEPING;
346 ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout);
347 c->flags &= ~CHN_F_SLEEPING;
349 return ((c->flags & CHN_F_DEAD) ? EINVAL : ret);
353 * chn_dmaupdate() tracks the status of a dma transfer,
358 chn_dmaupdate(struct pcm_channel *c)
360 struct snd_dbuf *b = c->bufhard;
361 unsigned int delta, old, hwptr, amt;
363 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
366 old = sndbuf_gethwptr(b);
367 hwptr = chn_getptr(c);
368 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
369 sndbuf_sethwptr(b, hwptr);
371 if (c->direction == PCMDIR_PLAY) {
372 amt = min(delta, sndbuf_getready(b));
373 amt -= amt % sndbuf_getalign(b);
375 sndbuf_dispose(b, NULL, amt);
377 amt = min(delta, sndbuf_getfree(b));
378 amt -= amt % sndbuf_getalign(b);
380 sndbuf_acquire(b, NULL, amt);
382 if (snd_verbose > 3 && CHN_STARTED(c) && delta == 0) {
383 device_printf(c->dev, "WARNING: %s DMA completion "
384 "too fast/slow ! hwptr=%u, old=%u "
385 "delta=%u amt=%u ready=%u free=%u\n",
386 CHN_DIRSTR(c), hwptr, old, delta, amt,
387 sndbuf_getready(b), sndbuf_getfree(b));
394 chn_wrfeed(struct pcm_channel *c)
396 struct snd_dbuf *b = c->bufhard;
397 struct snd_dbuf *bs = c->bufsoft;
398 unsigned int amt, want, wasfree;
402 if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING))
403 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
405 wasfree = sndbuf_getfree(b);
406 want = min(sndbuf_getsize(b),
407 imax(0, sndbuf_xbytes(sndbuf_getsize(bs), bs, b) -
408 sndbuf_getready(b)));
409 amt = min(wasfree, want);
411 sndbuf_feed(bs, b, c, c->feeder, amt);
414 * Possible xruns. There should be no empty space left in buffer.
416 if (sndbuf_getready(b) < want)
419 if (sndbuf_getfree(b) < wasfree)
425 chn_wrupdate(struct pcm_channel *c)
429 KASSERT(c->direction == PCMDIR_PLAY, ("%s(): bad channel", __func__));
431 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
435 /* tell the driver we've updated the primary buffer */
436 chn_trigger(c, PCMTRIG_EMLDMAWR);
441 chn_wrintr(struct pcm_channel *c)
445 /* update pointers in primary buffer */
447 /* ...and feed from secondary to primary */
449 /* tell the driver we've updated the primary buffer */
450 chn_trigger(c, PCMTRIG_EMLDMAWR);
454 * user write routine - uiomove data into secondary buffer, trigger if necessary
455 * if blocking, sleep, rinse and repeat.
457 * called externally, so must handle locking
461 chn_write(struct pcm_channel *c, struct uio *buf)
463 struct snd_dbuf *bs = c->bufsoft;
465 int ret, timeout, sz, t, p;
470 timeout = chn_timeout * hz;
472 while (ret == 0 && buf->uio_resid > 0) {
473 sz = min(buf->uio_resid, sndbuf_getfree(bs));
476 * The following assumes that the free space in
477 * the buffer can never be less around the
478 * unlock-uiomove-lock sequence.
480 while (ret == 0 && sz > 0) {
481 p = sndbuf_getfreeptr(bs);
482 t = min(sz, sndbuf_getsize(bs) - p);
483 off = sndbuf_getbufofs(bs, p);
485 ret = uiomove(off, t, buf);
488 sndbuf_acquire(bs, NULL, t);
491 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
492 ret = chn_start(c, 0);
494 c->flags |= CHN_F_DEAD;
496 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) {
498 * @todo Evaluate whether EAGAIN is truly desirable.
499 * 4Front drivers behave like this, but I'm
500 * not sure if it at all violates the "write
501 * should be allowed to block" model.
503 * The idea is that, while set with CHN_F_NOTRIGGER,
504 * a channel isn't playing, *but* without this we
505 * end up with "interrupt timeout / channel dead".
509 ret = chn_sleep(c, timeout);
512 c->flags |= CHN_F_DEAD;
513 device_printf(c->dev, "%s(): %s: "
514 "play interrupt timeout, channel dead\n",
516 } else if (ret == ERESTART || ret == EINTR)
517 c->flags |= CHN_F_ABORTING;
525 * Feed new data from the read buffer. Can be called in the bottom half.
528 chn_rdfeed(struct pcm_channel *c)
530 struct snd_dbuf *b = c->bufhard;
531 struct snd_dbuf *bs = c->bufsoft;
536 if (c->flags & CHN_F_MMAP)
537 sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
539 amt = sndbuf_getfree(bs);
541 sndbuf_feed(b, bs, c, c->feeder, amt);
543 amt = sndbuf_getready(b);
546 sndbuf_dispose(b, NULL, amt);
549 if (sndbuf_getready(bs) > 0)
555 chn_rdupdate(struct pcm_channel *c)
559 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
561 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
563 chn_trigger(c, PCMTRIG_EMLDMARD);
569 /* read interrupt routine. Must be called with interrupts blocked. */
571 chn_rdintr(struct pcm_channel *c)
575 /* tell the driver to update the primary buffer if non-dma */
576 chn_trigger(c, PCMTRIG_EMLDMARD);
577 /* update pointers in primary buffer */
579 /* ...and feed from primary to secondary */
584 * user read routine - trigger if necessary, uiomove data from secondary buffer
585 * if blocking, sleep, rinse and repeat.
587 * called externally, so must handle locking
591 chn_read(struct pcm_channel *c, struct uio *buf)
593 struct snd_dbuf *bs = c->bufsoft;
595 int ret, timeout, sz, t, p;
599 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
600 ret = chn_start(c, 0);
602 c->flags |= CHN_F_DEAD;
608 timeout = chn_timeout * hz;
610 while (ret == 0 && buf->uio_resid > 0) {
611 sz = min(buf->uio_resid, sndbuf_getready(bs));
614 * The following assumes that the free space in
615 * the buffer can never be less around the
616 * unlock-uiomove-lock sequence.
618 while (ret == 0 && sz > 0) {
619 p = sndbuf_getreadyptr(bs);
620 t = min(sz, sndbuf_getsize(bs) - p);
621 off = sndbuf_getbufofs(bs, p);
623 ret = uiomove(off, t, buf);
626 sndbuf_dispose(bs, NULL, t);
629 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER))
632 ret = chn_sleep(c, timeout);
635 c->flags |= CHN_F_DEAD;
636 device_printf(c->dev, "%s(): %s: "
637 "record interrupt timeout, channel dead\n",
639 } else if (ret == ERESTART || ret == EINTR)
640 c->flags |= CHN_F_ABORTING;
648 chn_intr_locked(struct pcm_channel *c)
655 if (c->direction == PCMDIR_PLAY)
662 chn_intr(struct pcm_channel *c)
665 if (CHN_LOCKOWNED(c)) {
676 chn_start(struct pcm_channel *c, int force)
679 struct snd_dbuf *b = c->bufhard;
680 struct snd_dbuf *bs = c->bufsoft;
684 /* if we're running, or if we're prevented from triggering, bail */
685 if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force))
694 if (c->direction == PCMDIR_REC) {
695 i = sndbuf_getfree(bs);
696 j = (i > 0) ? 1 : sndbuf_getready(b);
698 if (sndbuf_getfree(bs) == 0) {
704 pb = CHN_BUF_PARENT(c, b);
705 i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb);
706 j = sndbuf_getalign(pb);
709 if (snd_verbose > 3 && CHN_EMPTY(c, children))
710 device_printf(c->dev, "%s(): %s (%s) threshold "
711 "i=%d j=%d\n", __func__, CHN_DIRSTR(c),
712 (c->flags & CHN_F_VIRTUAL) ? "virtual" :
717 c->flags |= CHN_F_TRIGGERED;
719 if (c->flags & CHN_F_CLOSING)
726 if (c->parentchannel == NULL) {
727 if (c->direction == PCMDIR_PLAY)
728 sndbuf_fillsilence_rl(b,
729 sndbuf_xbytes(sndbuf_getsize(bs), bs, b));
731 device_printf(c->dev,
732 "%s(): %s starting! (%s/%s) "
733 "(ready=%d force=%d i=%d j=%d "
734 "intrtimeout=%u latency=%dms)\n",
736 (c->flags & CHN_F_HAS_VCHAN) ?
737 "VCHAN PARENT" : "HW", CHN_DIRSTR(c),
738 (c->flags & CHN_F_CLOSING) ? "closing" :
741 force, i, j, c->timeout,
742 (sndbuf_getsize(b) * 1000) /
743 (sndbuf_getalign(b) * sndbuf_getspd(b)));
745 err = chn_trigger(c, PCMTRIG_START);
752 chn_resetbuf(struct pcm_channel *c)
754 struct snd_dbuf *b = c->bufhard;
755 struct snd_dbuf *bs = c->bufsoft;
763 * chn_sync waits until the space in the given channel goes above
764 * a threshold. The threshold is checked against fl or rl respectively.
765 * Assume that the condition can become true, do not check here...
768 chn_sync(struct pcm_channel *c, int threshold)
770 struct snd_dbuf *b, *bs;
771 int ret, count, hcount, minflush, resid, residp, syncdelay, blksz;
776 if (c->direction != PCMDIR_PLAY)
781 if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) ||
782 (threshold < 1 && sndbuf_getready(bs) < 1))
785 /* if we haven't yet started and nothing is buffered, else start*/
786 if (CHN_STOPPED(c)) {
787 if (threshold > 0 || sndbuf_getready(bs) > 0) {
788 ret = chn_start(c, 1);
795 b = CHN_BUF_PARENT(c, c->bufhard);
797 minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs);
799 syncdelay = chn_syncdelay;
801 if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0))
802 minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs);
805 * Append (0-1000) millisecond trailing buffer (if needed)
806 * for slower / high latency hardwares (notably USB audio)
807 * to avoid audible truncation.
810 minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) *
811 ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000;
813 minflush -= minflush % sndbuf_getalign(bs);
816 threshold = min(minflush, sndbuf_getfree(bs));
817 sndbuf_clear(bs, threshold);
818 sndbuf_acquire(bs, NULL, threshold);
819 minflush -= threshold;
822 resid = sndbuf_getready(bs);
824 blksz = sndbuf_getblksz(b);
826 device_printf(c->dev,
827 "%s(): WARNING: blksz < 1 ! maxsize=%d [%d/%d/%d]\n",
828 __func__, sndbuf_getmaxsize(b), sndbuf_getsize(b),
829 sndbuf_getblksz(b), sndbuf_getblkcnt(b));
830 if (sndbuf_getblkcnt(b) > 0)
831 blksz = sndbuf_getsize(b) / sndbuf_getblkcnt(b);
835 count = sndbuf_xbytes(minflush + resid, bs, b) / blksz;
840 device_printf(c->dev, "%s(): [begin] timeout=%d count=%d "
841 "minflush=%d resid=%d\n", __func__, c->timeout, count,
844 cflag = c->flags & CHN_F_CLOSING;
845 c->flags |= CHN_F_CLOSING;
846 while (count > 0 && (resid > 0 || minflush > 0)) {
847 ret = chn_sleep(c, c->timeout);
848 if (ret == ERESTART || ret == EINTR) {
849 c->flags |= CHN_F_ABORTING;
851 } else if (ret == 0 || ret == EAGAIN) {
852 resid = sndbuf_getready(bs);
853 if (resid == residp) {
856 device_printf(c->dev,
857 "%s(): [stalled] timeout=%d "
858 "count=%d hcount=%d "
859 "resid=%d minflush=%d\n",
860 __func__, c->timeout, count,
861 hcount, resid, minflush);
862 } else if (resid < residp && count < hcount) {
865 device_printf(c->dev,
866 "%s((): [resume] timeout=%d "
867 "count=%d hcount=%d "
868 "resid=%d minflush=%d\n",
869 __func__, c->timeout, count,
870 hcount, resid, minflush);
872 if (minflush > 0 && sndbuf_getfree(bs) > 0) {
873 threshold = min(minflush,
875 sndbuf_clear(bs, threshold);
876 sndbuf_acquire(bs, NULL, threshold);
877 resid = sndbuf_getready(bs);
878 minflush -= threshold;
884 c->flags &= ~CHN_F_CLOSING;
888 device_printf(c->dev,
889 "%s(): timeout=%d count=%d hcount=%d resid=%d residp=%d "
890 "minflush=%d ret=%d\n",
891 __func__, c->timeout, count, hcount, resid, residp,
897 /* called externally, handle locking */
899 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
901 struct snd_dbuf *bs = c->bufsoft;
906 if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) {
907 ret = chn_start(c, 1);
913 if (chn_polltrigger(c)) {
917 selrecord(td, sndbuf_getsel(bs));
923 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
924 * it returns the number of bytes that have not been transferred.
926 * called from: dsp_close, dsp_ioctl, with channel locked
929 chn_abort(struct pcm_channel *c)
932 struct snd_dbuf *b = c->bufhard;
933 struct snd_dbuf *bs = c->bufsoft;
938 c->flags |= CHN_F_ABORTING;
940 c->flags &= ~CHN_F_TRIGGERED;
941 /* kill the channel */
942 chn_trigger(c, PCMTRIG_ABORT);
944 if (!(c->flags & CHN_F_VIRTUAL))
946 missing = sndbuf_getready(bs);
948 c->flags &= ~CHN_F_ABORTING;
953 * this routine tries to flush the dma transfer. It is called
954 * on a close of a playback channel.
955 * first, if there is data in the buffer, but the dma has not yet
956 * begun, we need to start it.
957 * next, we wait for the play buffer to drain
958 * finally, we stop the dma.
960 * called from: dsp_close, not valid for record channels.
964 chn_flush(struct pcm_channel *c)
966 struct snd_dbuf *b = c->bufhard;
969 KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
970 DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags));
972 c->flags |= CHN_F_CLOSING;
974 c->flags &= ~CHN_F_TRIGGERED;
975 /* kill the channel */
976 chn_trigger(c, PCMTRIG_ABORT);
979 c->flags &= ~CHN_F_CLOSING;
984 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist)
988 for (i = 0; fmtlist[i] != 0; i++) {
989 if (fmt == fmtlist[i] ||
990 ((fmt & AFMT_PASSTHROUGH) &&
991 (AFMT_ENCODING(fmt) & fmtlist[i])))
998 static const struct {
999 char *name, *alias1, *alias2;
1002 { "alaw", NULL, NULL, AFMT_A_LAW },
1003 { "mulaw", NULL, NULL, AFMT_MU_LAW },
1004 { "u8", "8", NULL, AFMT_U8 },
1005 { "s8", NULL, NULL, AFMT_S8 },
1006 #if BYTE_ORDER == LITTLE_ENDIAN
1007 { "s16le", "s16", "16", AFMT_S16_LE },
1008 { "s16be", NULL, NULL, AFMT_S16_BE },
1010 { "s16le", NULL, NULL, AFMT_S16_LE },
1011 { "s16be", "s16", "16", AFMT_S16_BE },
1013 { "u16le", NULL, NULL, AFMT_U16_LE },
1014 { "u16be", NULL, NULL, AFMT_U16_BE },
1015 { "s24le", NULL, NULL, AFMT_S24_LE },
1016 { "s24be", NULL, NULL, AFMT_S24_BE },
1017 { "u24le", NULL, NULL, AFMT_U24_LE },
1018 { "u24be", NULL, NULL, AFMT_U24_BE },
1019 #if BYTE_ORDER == LITTLE_ENDIAN
1020 { "s32le", "s32", "32", AFMT_S32_LE },
1021 { "s32be", NULL, NULL, AFMT_S32_BE },
1023 { "s32le", NULL, NULL, AFMT_S32_LE },
1024 { "s32be", "s32", "32", AFMT_S32_BE },
1026 { "u32le", NULL, NULL, AFMT_U32_LE },
1027 { "u32be", NULL, NULL, AFMT_U32_BE },
1028 { "ac3", NULL, NULL, AFMT_AC3 },
1029 { NULL, NULL, NULL, 0 }
1032 static const struct {
1033 char *name, *alias1, *alias2;
1035 } matrix_id_tab[] = {
1036 { "1.0", "1", "mono", SND_CHN_MATRIX_1_0 },
1037 { "2.0", "2", "stereo", SND_CHN_MATRIX_2_0 },
1038 { "2.1", NULL, NULL, SND_CHN_MATRIX_2_1 },
1039 { "3.0", "3", NULL, SND_CHN_MATRIX_3_0 },
1040 { "3.1", NULL, NULL, SND_CHN_MATRIX_3_1 },
1041 { "4.0", "4", "quad", SND_CHN_MATRIX_4_0 },
1042 { "4.1", NULL, NULL, SND_CHN_MATRIX_4_1 },
1043 { "5.0", "5", NULL, SND_CHN_MATRIX_5_0 },
1044 { "5.1", "6", NULL, SND_CHN_MATRIX_5_1 },
1045 { "6.0", NULL, NULL, SND_CHN_MATRIX_6_0 },
1046 { "6.1", "7", NULL, SND_CHN_MATRIX_6_1 },
1047 { "7.0", NULL, NULL, SND_CHN_MATRIX_7_0 },
1048 { "7.1", "8", NULL, SND_CHN_MATRIX_7_1 },
1049 { NULL, NULL, NULL, SND_CHN_MATRIX_UNKNOWN }
1053 snd_str2afmt(const char *req)
1059 i = sscanf(req, "%5[^:]:%6s", b1, b2);
1062 if (strlen(req) != strlen(b1))
1064 strlcpy(b2, "2.0", sizeof(b2));
1065 } else if (i == 2) {
1066 if (strlen(req) != (strlen(b1) + 1 + strlen(b2)))
1072 matrix_id = SND_CHN_MATRIX_UNKNOWN;
1074 for (i = 0; afmt == 0 && afmt_tab[i].name != NULL; i++) {
1075 if (strcasecmp(afmt_tab[i].name, b1) == 0 ||
1076 (afmt_tab[i].alias1 != NULL &&
1077 strcasecmp(afmt_tab[i].alias1, b1) == 0) ||
1078 (afmt_tab[i].alias2 != NULL &&
1079 strcasecmp(afmt_tab[i].alias2, b1) == 0)) {
1080 afmt = afmt_tab[i].afmt;
1081 strlcpy(b1, afmt_tab[i].name, sizeof(b1));
1088 for (i = 0; matrix_id == SND_CHN_MATRIX_UNKNOWN &&
1089 matrix_id_tab[i].name != NULL; i++) {
1090 if (strcmp(matrix_id_tab[i].name, b2) == 0 ||
1091 (matrix_id_tab[i].alias1 != NULL &&
1092 strcmp(matrix_id_tab[i].alias1, b2) == 0) ||
1093 (matrix_id_tab[i].alias2 != NULL &&
1094 strcasecmp(matrix_id_tab[i].alias2, b2) == 0)) {
1095 matrix_id = matrix_id_tab[i].matrix_id;
1096 strlcpy(b2, matrix_id_tab[i].name, sizeof(b2));
1100 if (matrix_id == SND_CHN_MATRIX_UNKNOWN)
1104 printf("Parse OK: '%s' -> '%s:%s' %d\n", req, b1, b2,
1105 (int)(b2[0]) - '0' + (int)(b2[2]) - '0');
1108 return (SND_FORMAT(afmt, b2[0] - '0' + b2[2] - '0', b2[2] - '0'));
1112 snd_afmt2str(uint32_t afmt, char *buf, size_t len)
1114 uint32_t i, enc, ch, ext;
1115 char tmp[AFMTSTR_LEN];
1117 if (buf == NULL || len < AFMTSTR_LEN)
1121 bzero(tmp, sizeof(tmp));
1123 enc = AFMT_ENCODING(afmt);
1124 ch = AFMT_CHANNEL(afmt);
1125 ext = AFMT_EXTCHANNEL(afmt);
1127 for (i = 0; afmt_tab[i].name != NULL; i++) {
1128 if (enc == afmt_tab[i].afmt) {
1129 strlcpy(tmp, afmt_tab[i].name, sizeof(tmp));
1130 strlcat(tmp, ":", sizeof(tmp));
1135 if (strlen(tmp) == 0)
1138 for (i = 0; matrix_id_tab[i].name != NULL; i++) {
1139 if (ch == (matrix_id_tab[i].name[0] - '0' +
1140 matrix_id_tab[i].name[2] - '0') &&
1141 ext == (matrix_id_tab[i].name[2] - '0')) {
1142 strlcat(tmp, matrix_id_tab[i].name, sizeof(tmp));
1147 if (strlen(tmp) == 0)
1150 strlcpy(buf, tmp, len);
1152 return (snd_str2afmt(buf));
1156 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
1162 c->flags &= CHN_F_RESET;
1167 c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ?
1168 CHN_F_BITPERFECT : 0;
1170 r = CHANNEL_RESET(c->methods, c->devinfo);
1171 if (r == 0 && fmt != 0 && spd != 0) {
1172 r = chn_setparam(c, fmt, spd);
1176 if (r == 0 && fmt != 0)
1177 r = chn_setformat(c, fmt);
1178 if (r == 0 && spd != 0)
1179 r = chn_setspeed(c, spd);
1181 r = chn_setlatency(c, chn_latency);
1184 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
1190 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
1192 struct feeder_class *fc;
1193 struct snd_dbuf *b, *bs;
1196 if (chn_timeout < CHN_TIMEOUT_MIN || chn_timeout > CHN_TIMEOUT_MAX)
1197 chn_timeout = CHN_TIMEOUT;
1199 chn_lockinit(c, dir);
1203 CHN_INIT(c, children);
1204 CHN_INIT(c, children.busy);
1211 b = sndbuf_create(c->dev, c->name, "primary", c);
1214 bs = sndbuf_create(c->dev, c->name, "secondary", c);
1221 fc = feeder_getclass(NULL);
1224 if (chn_addfeeder(c, fc, NULL))
1228 * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
1229 * with the channel unlocked because they are also called
1230 * from driver methods that don't know about locking
1233 sndbuf_setup(bs, NULL, 0);
1240 c->format = SND_FORMAT(AFMT_U8, 1, 0);
1241 c->speed = DSP_DEFAULT_SPEED;
1243 c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
1244 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1246 for (i = 0; i < SND_CHN_T_MAX; i++) {
1247 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
1250 c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
1251 c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
1253 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
1256 CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() malloc() call */
1257 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
1259 if (c->devinfo == NULL)
1263 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
1267 c->direction = direction;
1269 sndbuf_setfmt(b, c->format);
1270 sndbuf_setspd(b, c->speed);
1271 sndbuf_setfmt(bs, c->format);
1272 sndbuf_setspd(bs, c->speed);
1275 * @todo Should this be moved somewhere else? The primary buffer
1276 * is allocated by the driver or via DMA map setup, and tmpbuf
1277 * seems to only come into existence in sndbuf_resize().
1279 if (c->direction == PCMDIR_PLAY) {
1280 bs->sl = sndbuf_getmaxsize(bs);
1281 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_NOWAIT);
1282 if (bs->shadbuf == NULL) {
1292 if (CHANNEL_FREE(c->methods, c->devinfo))
1300 c->flags |= CHN_F_DEAD;
1310 chn_kill(struct pcm_channel *c)
1312 struct snd_dbuf *b = c->bufhard;
1313 struct snd_dbuf *bs = c->bufsoft;
1315 if (CHN_STARTED(c)) {
1317 chn_trigger(c, PCMTRIG_ABORT);
1320 while (chn_removefeeder(c) == 0)
1322 if (CHANNEL_FREE(c->methods, c->devinfo))
1327 c->flags |= CHN_F_DEAD;
1333 /* XXX Obsolete. Use *_matrix() variant instead. */
1335 chn_setvolume(struct pcm_channel *c, int left, int right)
1339 ret = chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FL, left);
1340 ret |= chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FR,
1347 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
1354 for (i = 0; i < SND_CHN_T_MAX; i++) {
1355 if ((1 << i) & SND_CHN_LEFT_MASK)
1356 ret |= chn_setvolume_matrix(c, vc, i, left);
1357 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1358 ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
1360 ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
1367 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
1371 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1372 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1373 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
1374 vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
1375 (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
1376 ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
1377 __func__, c, vc, vt, val));
1385 c->volume[vc][vt] = val;
1388 * Do relative calculation here and store it into class + 1
1389 * to ease the job of feeder_volume.
1391 if (vc == SND_VOL_C_MASTER) {
1392 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1393 vc += SND_VOL_C_STEP)
1394 c->volume[SND_VOL_C_VAL(vc)][vt] =
1395 SND_VOL_CALC_VAL(c->volume, vc, vt);
1396 } else if (vc & 1) {
1397 if (vt == SND_CHN_T_VOL_0DB)
1398 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1399 i += SND_CHN_T_STEP) {
1400 c->volume[SND_VOL_C_VAL(vc)][i] =
1401 SND_VOL_CALC_VAL(c->volume, vc, i);
1404 c->volume[SND_VOL_C_VAL(vc)][vt] =
1405 SND_VOL_CALC_VAL(c->volume, vc, vt);
1412 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
1414 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1415 (vt == SND_CHN_T_VOL_0DB ||
1416 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1417 ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
1418 __func__, c, vc, vt));
1421 return (c->volume[vc][vt]);
1424 struct pcmchan_matrix *
1425 chn_getmatrix(struct pcm_channel *c)
1428 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1431 if (!(c->format & AFMT_CONVERTIBLE))
1434 return (&c->matrix);
1438 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
1441 KASSERT(c != NULL && m != NULL,
1442 ("%s(): NULL channel or matrix", __func__));
1445 if (!(c->format & AFMT_CONVERTIBLE))
1449 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1451 return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
1455 * XXX chn_oss_* exists for the sake of compatibility.
1458 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
1461 KASSERT(c != NULL && map != NULL,
1462 ("%s(): NULL channel or map", __func__));
1465 if (!(c->format & AFMT_CONVERTIBLE))
1468 return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
1472 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
1474 struct pcmchan_matrix m;
1477 KASSERT(c != NULL && map != NULL,
1478 ("%s(): NULL channel or map", __func__));
1481 if (!(c->format & AFMT_CONVERTIBLE))
1485 ret = feeder_matrix_oss_set_channel_order(&m, map);
1489 return (chn_setmatrix(c, &m));
1492 #define SND_CHN_OSS_FRONT (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
1493 #define SND_CHN_OSS_SURR (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
1494 #define SND_CHN_OSS_CENTER_LFE (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
1495 #define SND_CHN_OSS_REAR (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
1498 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
1500 struct pcmchan_matrix *m;
1501 struct pcmchan_caps *caps;
1504 KASSERT(c != NULL && retmask != NULL,
1505 ("%s(): NULL channel or retmask", __func__));
1508 caps = chn_getcaps(c);
1509 if (caps == NULL || caps->fmtlist == NULL)
1512 for (i = 0; caps->fmtlist[i] != 0; i++) {
1513 format = caps->fmtlist[i];
1514 if (!(format & AFMT_CONVERTIBLE)) {
1515 *retmask |= DSP_BIND_SPDIF;
1518 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
1521 if (m->mask & SND_CHN_OSS_FRONT)
1522 *retmask |= DSP_BIND_FRONT;
1523 if (m->mask & SND_CHN_OSS_SURR)
1524 *retmask |= DSP_BIND_SURR;
1525 if (m->mask & SND_CHN_OSS_CENTER_LFE)
1526 *retmask |= DSP_BIND_CENTER_LFE;
1527 if (m->mask & SND_CHN_OSS_REAR)
1528 *retmask |= DSP_BIND_REAR;
1531 /* report software-supported binding mask */
1532 if (!CHN_BITPERFECT(c) && report_soft_matrix)
1533 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
1534 DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
1540 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
1544 KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
1545 ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
1548 if (force == 0 && chn_vpc_autoreset == 0)
1551 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
1552 CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
1556 round_pow2(u_int32_t v)
1565 ret = 1 << (ret - 1);
1572 round_blksz(u_int32_t v, int round)
1579 ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
1581 if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
1584 tmp = ret - (ret % round);
1585 while (tmp < 16 || tmp < round) {
1587 tmp = ret - (ret % round);
1594 * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
1595 * is to keep 2nd buffer short so that it doesn't cause long queue during
1598 * Latency reference table for 48khz stereo 16bit: (PLAY)
1600 * +---------+------------+-----------+------------+
1601 * | Latency | Blockcount | Blocksize | Buffersize |
1602 * +---------+------------+-----------+------------+
1603 * | 0 | 2 | 64 | 128 |
1604 * +---------+------------+-----------+------------+
1605 * | 1 | 4 | 128 | 512 |
1606 * +---------+------------+-----------+------------+
1607 * | 2 | 8 | 512 | 4096 |
1608 * +---------+------------+-----------+------------+
1609 * | 3 | 16 | 512 | 8192 |
1610 * +---------+------------+-----------+------------+
1611 * | 4 | 32 | 512 | 16384 |
1612 * +---------+------------+-----------+------------+
1613 * | 5 | 32 | 1024 | 32768 |
1614 * +---------+------------+-----------+------------+
1615 * | 6 | 16 | 2048 | 32768 |
1616 * +---------+------------+-----------+------------+
1617 * | 7 | 8 | 4096 | 32768 |
1618 * +---------+------------+-----------+------------+
1619 * | 8 | 4 | 8192 | 32768 |
1620 * +---------+------------+-----------+------------+
1621 * | 9 | 2 | 16384 | 32768 |
1622 * +---------+------------+-----------+------------+
1623 * | 10 | 2 | 32768 | 65536 |
1624 * +---------+------------+-----------+------------+
1626 * Recording need a different reference table. All we care is
1627 * gobbling up everything within reasonable buffering threshold.
1629 * Latency reference table for 48khz stereo 16bit: (REC)
1631 * +---------+------------+-----------+------------+
1632 * | Latency | Blockcount | Blocksize | Buffersize |
1633 * +---------+------------+-----------+------------+
1634 * | 0 | 512 | 32 | 16384 |
1635 * +---------+------------+-----------+------------+
1636 * | 1 | 256 | 64 | 16384 |
1637 * +---------+------------+-----------+------------+
1638 * | 2 | 128 | 128 | 16384 |
1639 * +---------+------------+-----------+------------+
1640 * | 3 | 64 | 256 | 16384 |
1641 * +---------+------------+-----------+------------+
1642 * | 4 | 32 | 512 | 16384 |
1643 * +---------+------------+-----------+------------+
1644 * | 5 | 32 | 1024 | 32768 |
1645 * +---------+------------+-----------+------------+
1646 * | 6 | 16 | 2048 | 32768 |
1647 * +---------+------------+-----------+------------+
1648 * | 7 | 8 | 4096 | 32768 |
1649 * +---------+------------+-----------+------------+
1650 * | 8 | 4 | 8192 | 32768 |
1651 * +---------+------------+-----------+------------+
1652 * | 9 | 2 | 16384 | 32768 |
1653 * +---------+------------+-----------+------------+
1654 * | 10 | 2 | 32768 | 65536 |
1655 * +---------+------------+-----------+------------+
1657 * Calculations for other data rate are entirely based on these reference
1658 * tables. For normal operation, Latency 5 seems give the best, well
1659 * balanced performance for typical workload. Anything below 5 will
1660 * eat up CPU to keep up with increasing context switches because of
1661 * shorter buffer space and usually require the application to handle it
1662 * aggresively through possibly real time programming technique.
1665 #define CHN_LATENCY_PBLKCNT_REF \
1666 {{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}, \
1667 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}}
1668 #define CHN_LATENCY_PBUFSZ_REF \
1669 {{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16}, \
1670 {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}}
1672 #define CHN_LATENCY_RBLKCNT_REF \
1673 {{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}, \
1674 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}}
1675 #define CHN_LATENCY_RBUFSZ_REF \
1676 {{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16}, \
1677 {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}}
1679 #define CHN_LATENCY_DATA_REF 192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
1682 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
1683 u_int32_t max, int *rblksz, int *rblkcnt)
1685 static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1686 CHN_LATENCY_PBLKCNT_REF;
1687 static int pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1688 CHN_LATENCY_PBUFSZ_REF;
1689 static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1690 CHN_LATENCY_RBLKCNT_REF;
1691 static int rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1692 CHN_LATENCY_RBUFSZ_REF;
1694 int lprofile, blksz, blkcnt;
1696 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
1697 bps < 1 || datarate < 1 ||
1698 !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
1700 *rblksz = CHN_2NDBUFMAXSIZE >> 1;
1701 if (rblkcnt != NULL)
1703 printf("%s(): FAILED dir=%d latency=%d bps=%d "
1704 "datarate=%u max=%u\n",
1705 __func__, dir, latency, bps, datarate, max);
1706 return CHN_2NDBUFMAXSIZE;
1709 lprofile = chn_latency_profile;
1711 if (dir == PCMDIR_PLAY) {
1712 blkcnt = pblkcnts[lprofile][latency];
1713 bufsz = pbufszs[lprofile][latency];
1715 blkcnt = rblkcnts[lprofile][latency];
1716 bufsz = rbufszs[lprofile][latency];
1719 bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
1723 blksz = round_blksz(bufsz >> blkcnt, bps);
1727 if (rblkcnt != NULL)
1728 *rblkcnt = 1 << blkcnt;
1730 return blksz << blkcnt;
1734 chn_resizebuf(struct pcm_channel *c, int latency,
1735 int blkcnt, int blksz)
1737 struct snd_dbuf *b, *bs, *pb;
1738 int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
1743 if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
1744 !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
1747 if (latency == -1) {
1749 latency = chn_latency;
1750 } else if (latency == -2) {
1751 latency = c->latency;
1752 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1753 latency = chn_latency;
1754 } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1757 c->latency = latency;
1763 if (!(blksz == 0 || blkcnt == -1) &&
1764 (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 ||
1765 (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
1768 chn_calclatency(c->direction, latency, sndbuf_getalign(bs),
1769 sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE,
1772 if (blksz == 0 || blkcnt == -1) {
1774 c->flags &= ~CHN_F_HAS_SIZE;
1775 if (c->flags & CHN_F_HAS_SIZE) {
1776 blksz = sndbuf_getblksz(bs);
1777 blkcnt = sndbuf_getblkcnt(bs);
1780 c->flags |= CHN_F_HAS_SIZE;
1782 if (c->flags & CHN_F_HAS_SIZE) {
1784 * The application has requested their own blksz/blkcnt.
1785 * Just obey with it, and let them toast alone. We can
1786 * clamp it to the nearest latency profile, but that would
1787 * defeat the purpose of having custom control. The least
1788 * we can do is round it to the nearest ^2 and align it.
1790 sblksz = round_blksz(blksz, sndbuf_getalign(bs));
1791 sblkcnt = round_pow2(blkcnt);
1794 if (c->parentchannel != NULL) {
1795 pb = c->parentchannel->bufsoft;
1797 CHN_LOCK(c->parentchannel);
1798 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
1799 CHN_UNLOCK(c->parentchannel);
1801 if (c->direction == PCMDIR_PLAY) {
1802 limit = (pb != NULL) ?
1803 sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0;
1805 limit = (pb != NULL) ?
1806 sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0;
1810 if (c->flags & CHN_F_HAS_SIZE) {
1811 hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
1812 sndbuf_getalign(b));
1813 hblkcnt = round_pow2(sndbuf_getblkcnt(bs));
1815 chn_calclatency(c->direction, latency,
1817 sndbuf_getalign(b) * sndbuf_getspd(b),
1818 CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
1820 if ((hblksz << 1) > sndbuf_getmaxsize(b))
1821 hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1,
1822 sndbuf_getalign(b));
1824 while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) {
1831 hblksz -= hblksz % sndbuf_getalign(b);
1834 hblksz = sndbuf_getmaxsize(b) >> 1;
1835 hblksz -= hblksz % sndbuf_getalign(b);
1840 if (chn_usefrags == 0 ||
1841 CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
1842 hblksz, hblkcnt) != 0)
1843 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods,
1844 c->devinfo, hblksz));
1847 if (!CHN_EMPTY(c, children)) {
1848 nsblksz = round_blksz(
1849 sndbuf_xbytes(sndbuf_getblksz(b), b, bs),
1850 sndbuf_getalign(bs));
1851 nsblkcnt = sndbuf_getblkcnt(b);
1852 if (c->direction == PCMDIR_PLAY) {
1855 } while (nsblkcnt >= 2 &&
1856 nsblksz * nsblkcnt >= sblksz * sblkcnt);
1863 limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2;
1866 if (limit > CHN_2NDBUFMAXSIZE)
1867 limit = CHN_2NDBUFMAXSIZE;
1870 while (limit > 0 && (sblksz * sblkcnt) > limit) {
1877 while ((sblksz * sblkcnt) < limit)
1880 while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
1887 sblksz -= sblksz % sndbuf_getalign(bs);
1889 if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz ||
1890 sndbuf_getsize(bs) != (sblkcnt * sblksz)) {
1891 ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
1893 device_printf(c->dev, "%s(): Failed: %d %d\n",
1894 __func__, sblkcnt, sblksz);
1902 c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) /
1903 ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs));
1904 if (c->parentchannel != NULL)
1905 c->timeout = min(c->timeout, c->parentchannel->timeout);
1910 * OSSv4 docs: "By default OSS will set the low water level equal
1911 * to the fragment size which is optimal in most cases."
1913 c->lw = sndbuf_getblksz(bs);
1916 if (snd_verbose > 3)
1917 device_printf(c->dev, "%s(): %s (%s) timeout=%u "
1918 "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
1919 __func__, CHN_DIRSTR(c),
1920 (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
1922 sndbuf_getsize(b), sndbuf_getblksz(b),
1923 sndbuf_getblkcnt(b),
1924 sndbuf_getsize(bs), sndbuf_getblksz(bs),
1925 sndbuf_getblkcnt(bs), limit);
1931 chn_setlatency(struct pcm_channel *c, int latency)
1934 /* Destroy blksz/blkcnt, enforce latency profile. */
1935 return chn_resizebuf(c, latency, -1, 0);
1939 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
1942 /* Destroy latency profile, enforce blksz/blkcnt */
1943 return chn_resizebuf(c, -1, blkcnt, blksz);
1947 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
1949 struct pcmchan_caps *caps;
1950 uint32_t hwspeed, delta;
1955 if (speed < 1 || format == 0 || CHN_STARTED(c))
1961 caps = chn_getcaps(c);
1964 RANGE(hwspeed, caps->minspeed, caps->maxspeed);
1966 sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
1968 hwspeed = sndbuf_getspd(c->bufhard);
1970 delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
1972 if (delta <= feeder_rate_round)
1975 ret = feeder_chain(c);
1978 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo,
1979 sndbuf_getfmt(c->bufhard));
1982 ret = chn_resizebuf(c, -2, 0, 0);
1988 chn_setspeed(struct pcm_channel *c, uint32_t speed)
1990 uint32_t oldformat, oldspeed, format;
1995 if (c->format & AFMT_PASSTHROUGH)
1996 speed = AFMT_PASSTHROUGH_RATE;
1999 oldformat = c->format;
2000 oldspeed = c->speed;
2003 ret = chn_setparam(c, format, speed);
2005 if (snd_verbose > 3)
2006 device_printf(c->dev,
2007 "%s(): Setting speed %d failed, "
2008 "falling back to %d\n",
2009 __func__, speed, oldspeed);
2010 chn_setparam(c, c->format, oldspeed);
2017 chn_setformat(struct pcm_channel *c, uint32_t format)
2019 uint32_t oldformat, oldspeed, speed;
2022 /* XXX force stereo */
2023 if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
2024 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
2025 AFMT_PASSTHROUGH_EXTCHANNEL);
2028 oldformat = c->format;
2029 oldspeed = c->speed;
2032 ret = chn_setparam(c, format, speed);
2034 if (snd_verbose > 3)
2035 device_printf(c->dev,
2036 "%s(): Format change 0x%08x failed, "
2037 "falling back to 0x%08x\n",
2038 __func__, format, oldformat);
2039 chn_setparam(c, oldformat, oldspeed);
2046 chn_syncstate(struct pcm_channel *c)
2048 struct snddev_info *d;
2049 struct snd_mixer *m;
2051 d = (c != NULL) ? c->parentsnddev : NULL;
2052 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
2055 if (d == NULL || m == NULL)
2060 if (c->feederflags & (1 << FEEDER_VOLUME)) {
2062 int vol, pvol, left, right, center;
2064 if (c->direction == PCMDIR_PLAY &&
2065 (d->flags & SD_F_SOFTPCMVOL)) {
2066 /* CHN_UNLOCK(c); */
2067 vol = mix_get(m, SOUND_MIXER_PCM);
2068 parent = mix_getparent(m, SOUND_MIXER_PCM);
2069 if (parent != SOUND_MIXER_NONE)
2070 pvol = mix_get(m, parent);
2072 pvol = 100 | (100 << 8);
2075 vol = 100 | (100 << 8);
2080 device_printf(c->dev,
2081 "Soft PCM Volume: Failed to read pcm "
2083 vol = 100 | (100 << 8);
2087 device_printf(c->dev,
2088 "Soft PCM Volume: Failed to read parent "
2090 pvol = 100 | (100 << 8);
2093 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
2094 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
2095 center = (left + right) >> 1;
2097 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
2100 if (c->feederflags & (1 << FEEDER_EQ)) {
2101 struct pcm_feeder *f;
2102 int treble, bass, state;
2104 /* CHN_UNLOCK(c); */
2105 treble = mix_get(m, SOUND_MIXER_TREBLE);
2106 bass = mix_get(m, SOUND_MIXER_BASS);
2112 treble = ((treble & 0x7f) +
2113 ((treble >> 8) & 0x7f)) >> 1;
2118 bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
2120 f = chn_findfeeder(c, FEEDER_EQ);
2122 if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
2123 device_printf(c->dev,
2124 "EQ: Failed to set treble -- %d\n",
2126 if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
2127 device_printf(c->dev,
2128 "EQ: Failed to set bass -- %d\n",
2130 if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
2131 device_printf(c->dev,
2132 "EQ: Failed to set preamp -- %d\n",
2134 if (d->flags & SD_F_EQ_BYPASSED)
2135 state = FEEDEQ_BYPASS;
2136 else if (d->flags & SD_F_EQ_ENABLED)
2137 state = FEEDEQ_ENABLE;
2139 state = FEEDEQ_DISABLE;
2140 if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
2141 device_printf(c->dev,
2142 "EQ: Failed to set state -- %d\n", state);
2148 chn_trigger(struct pcm_channel *c, int go)
2151 struct snd_dbuf *b = c->bufhard;
2153 struct snddev_info *d = c->parentsnddev;
2158 if (SND_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
2159 sndbuf_dmabounce(b);
2161 if (!PCMTRIG_COMMON(go))
2162 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
2164 if (go == c->trigger)
2167 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
2173 if (snd_verbose > 3)
2174 device_printf(c->dev,
2175 "%s() %s: calling go=0x%08x , "
2176 "prev=0x%08x\n", __func__, c->name, go,
2178 if (c->trigger != PCMTRIG_START) {
2182 CHN_INSERT_HEAD(d, c, channels.pcm.busy);
2190 if (snd_verbose > 3)
2191 device_printf(c->dev,
2192 "%s() %s: calling go=0x%08x , "
2193 "prev=0x%08x\n", __func__, c->name, go,
2195 if (c->trigger == PCMTRIG_START) {
2199 CHN_REMOVE(d, c, channels.pcm.busy);
2212 * @brief Queries sound driver for sample-aligned hardware buffer pointer index
2214 * This function obtains the hardware pointer location, then aligns it to
2215 * the current bytes-per-sample value before returning. (E.g., a channel
2216 * running in 16 bit stereo mode would require 4 bytes per sample, so a
2217 * hwptr value ranging from 32-35 would be returned as 32.)
2219 * @param c PCM channel context
2220 * @returns sample-aligned hardware buffer pointer index
2223 chn_getptr(struct pcm_channel *c)
2228 hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
2229 return (hwptr - (hwptr % sndbuf_getalign(c->bufhard)));
2232 struct pcmchan_caps *
2233 chn_getcaps(struct pcm_channel *c)
2236 return CHANNEL_GETCAPS(c->methods, c->devinfo);
2240 chn_getformats(struct pcm_channel *c)
2242 u_int32_t *fmtlist, fmts;
2245 fmtlist = chn_getcaps(c)->fmtlist;
2247 for (i = 0; fmtlist[i]; i++)
2250 /* report software-supported formats */
2251 if (!CHN_BITPERFECT(c) && report_soft_formats)
2252 fmts |= AFMT_CONVERTIBLE;
2254 return (AFMT_ENCODING(fmts));
2258 chn_notify(struct pcm_channel *c, u_int32_t flags)
2260 struct pcm_channel *ch;
2261 struct pcmchan_caps *caps;
2262 uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
2264 int dirty, err, run, nrun;
2268 if (CHN_EMPTY(c, children))
2274 * If the hwchan is running, we can't change its rate, format or
2277 run = (CHN_STARTED(c)) ? 1 : 0;
2279 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
2281 if (flags & CHN_N_RATE) {
2283 * XXX I'll make good use of this someday.
2284 * However this is currently being superseded by
2285 * the availability of CHN_F_VCHAN_DYNAMIC.
2289 if (flags & CHN_N_FORMAT) {
2291 * XXX I'll make good use of this someday.
2292 * However this is currently being superseded by
2293 * the availability of CHN_F_VCHAN_DYNAMIC.
2297 if (flags & CHN_N_VOLUME) {
2299 * XXX I'll make good use of this someday, though
2300 * soft volume control is currently pretty much
2305 if (flags & CHN_N_BLOCKSIZE) {
2307 * Set to default latency profile
2309 chn_setlatency(c, chn_latency);
2312 if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) {
2313 nrun = CHN_EMPTY(c, children.busy) ? 0 : 1;
2315 err = chn_start(c, 1);
2318 flags &= ~CHN_N_TRIGGER;
2321 if (flags & CHN_N_TRIGGER) {
2322 if (c->direction == PCMDIR_PLAY) {
2323 vchanformat = &c->parentsnddev->pvchanformat;
2324 vchanrate = &c->parentsnddev->pvchanrate;
2326 vchanformat = &c->parentsnddev->rvchanformat;
2327 vchanrate = &c->parentsnddev->rvchanrate;
2330 /* Dynamic Virtual Channel */
2331 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
2332 bestformat = *vchanformat;
2333 bestspeed = *vchanrate;
2341 caps = chn_getcaps(c);
2345 CHN_FOREACH(ch, c, children.busy) {
2347 if ((ch->format & AFMT_PASSTHROUGH) &&
2348 snd_fmtvalid(ch->format, caps->fmtlist)) {
2349 bestformat = ch->format;
2350 bestspeed = ch->speed;
2352 vpflags = CHN_F_PASSTHROUGH;
2356 if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
2357 if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
2358 bestspeed = ch->speed;
2359 RANGE(bestspeed, caps->minspeed,
2361 besthwformat = snd_fmtbest(ch->format,
2363 if (besthwformat != 0)
2364 bestformat = besthwformat;
2367 vpflags = CHN_F_EXCLUSIVE;
2371 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
2377 if (ch->speed > bestspeed) {
2378 bestspeed = ch->speed;
2379 RANGE(bestspeed, caps->minspeed,
2382 besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
2383 if (!(besthwformat & AFMT_VCHAN)) {
2388 if (AFMT_CHANNEL(besthwformat) >
2389 AFMT_CHANNEL(bestformat))
2390 bestformat = besthwformat;
2391 else if (AFMT_CHANNEL(besthwformat) ==
2392 AFMT_CHANNEL(bestformat) &&
2393 AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
2394 bestformat = besthwformat;
2399 if (bestformat == 0)
2400 bestformat = c->format;
2402 bestspeed = c->speed;
2404 if (bestformat != c->format || bestspeed != c->speed)
2407 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2408 c->flags |= vpflags;
2412 bestspeed = CHANNEL_SETSPEED(c->methods,
2413 c->devinfo, bestspeed);
2414 err = chn_reset(c, bestformat, bestspeed);
2416 if (err == 0 && dirty) {
2417 CHN_FOREACH(ch, c, children.busy) {
2419 if (VCHAN_SYNC_REQUIRED(ch))
2426 c->flags |= CHN_F_DIRTY;
2427 err = chn_start(c, 1);
2431 if (nrun && run && dirty) {
2433 bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
2435 err = chn_reset(c, bestformat, bestspeed);
2437 CHN_FOREACH(ch, c, children.busy) {
2439 if (VCHAN_SYNC_REQUIRED(ch))
2445 c->flags |= CHN_F_DIRTY;
2446 err = chn_start(c, 1);
2450 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
2451 (bestformat & AFMT_VCHAN)) {
2452 *vchanformat = bestformat;
2453 *vchanrate = bestspeed;
2457 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2458 bestformat = *vchanformat;
2459 bestspeed = *vchanrate;
2461 if (c->format != bestformat || c->speed != bestspeed)
2462 chn_reset(c, bestformat, bestspeed);
2470 * @brief Fetch array of supported discrete sample rates
2472 * Wrapper for CHANNEL_GETRATES. Please see channel_if.m:getrates() for
2473 * detailed information.
2475 * @note If the operation isn't supported, this function will just return 0
2476 * (no rates in the array), and *rates will be set to NULL. Callers
2477 * should examine rates @b only if this function returns non-zero.
2479 * @param c pcm channel to examine
2480 * @param rates pointer to array of integers; rate table will be recorded here
2482 * @return number of rates in the array pointed to be @c rates
2485 chn_getrates(struct pcm_channel *c, int **rates)
2487 KASSERT(rates != NULL, ("rates is null"));
2489 return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
2493 * @brief Remove channel from a sync group, if there is one.
2495 * This function is initially intended for the following conditions:
2496 * - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
2497 * - Closing a device. (A channel can't be destroyed if it's still in use.)
2499 * @note Before calling this function, the syncgroup list mutex must be
2500 * held. (Consider pcm_channel::sm protected by the SG list mutex
2501 * whether @c c is locked or not.)
2503 * @param c channel device to be started or closed
2504 * @returns If this channel was the only member of a group, the group ID
2505 * is returned to the caller so that the caller can release it
2506 * via free_unr() after giving up the syncgroup lock. Else it
2510 chn_syncdestroy(struct pcm_channel *c)
2512 struct pcmchan_syncmember *sm;
2513 struct pcmchan_syncgroup *sg;
2518 PCM_SG_LOCKASSERT(MA_OWNED);
2520 if (c->sm != NULL) {
2525 KASSERT(sg != NULL, ("syncmember has null parent"));
2527 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
2530 if (SLIST_EMPTY(&sg->members)) {
2531 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2540 #ifdef OSSV4_EXPERIMENT
2542 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
2545 return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);