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, CTLTYPE_INT | CTLFLAG_RWTUN,
72 0, sizeof(int), sysctl_hw_snd_latency, "I",
73 "buffering latency (0=low ... 10=high)");
75 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
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_RWTUN,
94 0, sizeof(int), sysctl_hw_snd_latency_profile, "I",
95 "buffering latency profile (0=aggressive 1=safe)");
97 static int chn_timeout = CHN_TIMEOUT;
100 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS)
105 err = sysctl_handle_int(oidp, &val, 0, req);
106 if (err != 0 || req->newptr == NULL)
108 if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX)
115 SYSCTL_PROC(_hw_snd, OID_AUTO, timeout, CTLTYPE_INT | CTLFLAG_RWTUN,
116 0, sizeof(int), sysctl_hw_snd_timeout, "I",
117 "interrupt timeout (1 - 10) seconds");
119 static int chn_vpc_autoreset = 1;
120 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_autoreset, CTLFLAG_RWTUN,
121 &chn_vpc_autoreset, 0, "automatically reset channels volume to 0db");
123 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM;
126 chn_vpc_proc(int reset, int db)
128 struct snddev_info *d;
129 struct pcm_channel *c;
132 for (i = 0; pcm_devclass != NULL &&
133 i < devclass_get_maxunit(pcm_devclass); i++) {
134 d = devclass_get_softc(pcm_devclass, i);
135 if (!PCM_REGISTERED(d))
140 CHN_FOREACH(c, d, channels.pcm) {
142 CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db);
144 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
153 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS)
157 val = chn_vol_0db_pcm;
158 err = sysctl_handle_int(oidp, &val, 0, req);
159 if (err != 0 || req->newptr == NULL)
161 if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX)
164 chn_vol_0db_pcm = val;
165 chn_vpc_proc(0, val);
169 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_0db, CTLTYPE_INT | CTLFLAG_RWTUN,
170 0, sizeof(int), sysctl_hw_snd_vpc_0db, "I",
171 "0db relative level");
174 sysctl_hw_snd_vpc_reset(SYSCTL_HANDLER_ARGS)
179 err = sysctl_handle_int(oidp, &val, 0, req);
180 if (err != 0 || req->newptr == NULL || val == 0)
183 chn_vol_0db_pcm = SND_VOL_0DB_PCM;
184 chn_vpc_proc(1, SND_VOL_0DB_PCM);
188 SYSCTL_PROC(_hw_snd, OID_AUTO, vpc_reset, CTLTYPE_INT | CTLFLAG_RW,
189 0, sizeof(int), sysctl_hw_snd_vpc_reset, "I",
190 "reset volume on all channels");
192 static int chn_usefrags = 0;
193 static int chn_syncdelay = -1;
195 SYSCTL_INT(_hw_snd, OID_AUTO, usefrags, CTLFLAG_RWTUN,
196 &chn_usefrags, 0, "prefer setfragments() over setblocksize()");
197 SYSCTL_INT(_hw_snd, OID_AUTO, syncdelay, CTLFLAG_RWTUN,
199 "append (0-1000) millisecond trailing buffer delay on each sync");
202 * @brief Channel sync group lock
204 * Clients should acquire this lock @b without holding any channel locks
205 * before touching syncgroups or the main syncgroup list.
207 struct mtx snd_pcm_syncgroups_mtx;
208 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF);
210 * @brief syncgroups' master list
212 * Each time a channel syncgroup is created, it's added to this list. This
213 * list should only be accessed with @sa snd_pcm_syncgroups_mtx held.
215 * See SNDCTL_DSP_SYNCGROUP for more information.
217 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups);
220 chn_lockinit(struct pcm_channel *c, int dir)
224 c->lock = snd_mtxcreate(c->name, "pcm play channel");
225 cv_init(&c->intr_cv, "pcmwr");
227 case PCMDIR_PLAY_VIRTUAL:
228 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
229 cv_init(&c->intr_cv, "pcmwrv");
232 c->lock = snd_mtxcreate(c->name, "pcm record channel");
233 cv_init(&c->intr_cv, "pcmrd");
235 case PCMDIR_REC_VIRTUAL:
236 c->lock = snd_mtxcreate(c->name, "pcm virtual record channel");
237 cv_init(&c->intr_cv, "pcmrdv");
240 panic("%s(): Invalid direction=%d", __func__, dir);
244 cv_init(&c->cv, "pcmchn");
248 chn_lockdestroy(struct pcm_channel *c)
252 CHN_BROADCAST(&c->cv);
253 CHN_BROADCAST(&c->intr_cv);
256 cv_destroy(&c->intr_cv);
258 snd_mtxfree(c->lock);
262 * @brief Determine channel is ready for I/O
264 * @retval 1 = ready for I/O
265 * @retval 0 = not ready for I/O
268 chn_polltrigger(struct pcm_channel *c)
270 struct snd_dbuf *bs = c->bufsoft;
275 if (c->flags & CHN_F_MMAP) {
276 if (sndbuf_getprevtotal(bs) < c->lw)
279 delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs);
281 if (c->direction == PCMDIR_PLAY)
282 delta = sndbuf_getfree(bs);
284 delta = sndbuf_getready(bs);
287 return ((delta < c->lw) ? 0 : 1);
291 chn_pollreset(struct pcm_channel *c)
295 sndbuf_updateprevtotal(c->bufsoft);
299 chn_wakeup(struct pcm_channel *c)
302 struct pcm_channel *ch;
308 if (CHN_EMPTY(c, children.busy)) {
309 if (SEL_WAITING(sndbuf_getsel(bs)) && chn_polltrigger(c))
310 selwakeuppri(sndbuf_getsel(bs), PRIBIO);
311 if (c->flags & CHN_F_SLEEPING) {
313 * Ok, I can just panic it right here since it is
314 * quite obvious that we never allow multiple waiters
315 * from userland. I'm too generous...
317 CHN_BROADCAST(&c->intr_cv);
320 CHN_FOREACH(ch, c, children.busy) {
329 chn_sleep(struct pcm_channel *c, int timeout)
335 if (c->flags & CHN_F_DEAD)
338 c->flags |= CHN_F_SLEEPING;
339 ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout);
340 c->flags &= ~CHN_F_SLEEPING;
342 return ((c->flags & CHN_F_DEAD) ? EINVAL : ret);
346 * chn_dmaupdate() tracks the status of a dma transfer,
351 chn_dmaupdate(struct pcm_channel *c)
353 struct snd_dbuf *b = c->bufhard;
354 unsigned int delta, old, hwptr, amt;
356 KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
359 old = sndbuf_gethwptr(b);
360 hwptr = chn_getptr(c);
361 delta = (sndbuf_getsize(b) + hwptr - old) % sndbuf_getsize(b);
362 sndbuf_sethwptr(b, hwptr);
364 if (c->direction == PCMDIR_PLAY) {
365 amt = min(delta, sndbuf_getready(b));
366 amt -= amt % sndbuf_getalign(b);
368 sndbuf_dispose(b, NULL, amt);
370 amt = min(delta, sndbuf_getfree(b));
371 amt -= amt % sndbuf_getalign(b);
373 sndbuf_acquire(b, NULL, amt);
375 if (snd_verbose > 3 && CHN_STARTED(c) && delta == 0) {
376 device_printf(c->dev, "WARNING: %s DMA completion "
377 "too fast/slow ! hwptr=%u, old=%u "
378 "delta=%u amt=%u ready=%u free=%u\n",
379 CHN_DIRSTR(c), hwptr, old, delta, amt,
380 sndbuf_getready(b), sndbuf_getfree(b));
387 chn_wrfeed(struct pcm_channel *c)
389 struct snd_dbuf *b = c->bufhard;
390 struct snd_dbuf *bs = c->bufsoft;
391 unsigned int amt, want, wasfree;
395 if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING))
396 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
398 wasfree = sndbuf_getfree(b);
399 want = min(sndbuf_getsize(b),
400 imax(0, sndbuf_xbytes(sndbuf_getsize(bs), bs, b) -
401 sndbuf_getready(b)));
402 amt = min(wasfree, want);
404 sndbuf_feed(bs, b, c, c->feeder, amt);
407 * Possible xruns. There should be no empty space left in buffer.
409 if (sndbuf_getready(b) < want)
412 if (sndbuf_getfree(b) < wasfree)
418 chn_wrupdate(struct pcm_channel *c)
422 KASSERT(c->direction == PCMDIR_PLAY, ("%s(): bad channel", __func__));
424 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
428 /* tell the driver we've updated the primary buffer */
429 chn_trigger(c, PCMTRIG_EMLDMAWR);
434 chn_wrintr(struct pcm_channel *c)
438 /* update pointers in primary buffer */
440 /* ...and feed from secondary to primary */
442 /* tell the driver we've updated the primary buffer */
443 chn_trigger(c, PCMTRIG_EMLDMAWR);
447 * user write routine - uiomove data into secondary buffer, trigger if necessary
448 * if blocking, sleep, rinse and repeat.
450 * called externally, so must handle locking
454 chn_write(struct pcm_channel *c, struct uio *buf)
456 struct snd_dbuf *bs = c->bufsoft;
458 int ret, timeout, sz, t, p;
463 timeout = chn_timeout * hz;
465 while (ret == 0 && buf->uio_resid > 0) {
466 sz = min(buf->uio_resid, sndbuf_getfree(bs));
469 * The following assumes that the free space in
470 * the buffer can never be less around the
471 * unlock-uiomove-lock sequence.
473 while (ret == 0 && sz > 0) {
474 p = sndbuf_getfreeptr(bs);
475 t = min(sz, sndbuf_getsize(bs) - p);
476 off = sndbuf_getbufofs(bs, p);
478 ret = uiomove(off, t, buf);
481 sndbuf_acquire(bs, NULL, t);
484 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
485 ret = chn_start(c, 0);
487 c->flags |= CHN_F_DEAD;
489 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) {
491 * @todo Evaluate whether EAGAIN is truly desirable.
492 * 4Front drivers behave like this, but I'm
493 * not sure if it at all violates the "write
494 * should be allowed to block" model.
496 * The idea is that, while set with CHN_F_NOTRIGGER,
497 * a channel isn't playing, *but* without this we
498 * end up with "interrupt timeout / channel dead".
502 ret = chn_sleep(c, timeout);
505 c->flags |= CHN_F_DEAD;
506 device_printf(c->dev, "%s(): %s: "
507 "play interrupt timeout, channel dead\n",
509 } else if (ret == ERESTART || ret == EINTR)
510 c->flags |= CHN_F_ABORTING;
518 * Feed new data from the read buffer. Can be called in the bottom half.
521 chn_rdfeed(struct pcm_channel *c)
523 struct snd_dbuf *b = c->bufhard;
524 struct snd_dbuf *bs = c->bufsoft;
529 if (c->flags & CHN_F_MMAP)
530 sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
532 amt = sndbuf_getfree(bs);
534 sndbuf_feed(b, bs, c, c->feeder, amt);
536 amt = sndbuf_getready(b);
539 sndbuf_dispose(b, NULL, amt);
542 if (sndbuf_getready(bs) > 0)
548 chn_rdupdate(struct pcm_channel *c)
552 KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
554 if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
556 chn_trigger(c, PCMTRIG_EMLDMARD);
562 /* read interrupt routine. Must be called with interrupts blocked. */
564 chn_rdintr(struct pcm_channel *c)
568 /* tell the driver to update the primary buffer if non-dma */
569 chn_trigger(c, PCMTRIG_EMLDMARD);
570 /* update pointers in primary buffer */
572 /* ...and feed from primary to secondary */
577 * user read routine - trigger if necessary, uiomove data from secondary buffer
578 * if blocking, sleep, rinse and repeat.
580 * called externally, so must handle locking
584 chn_read(struct pcm_channel *c, struct uio *buf)
586 struct snd_dbuf *bs = c->bufsoft;
588 int ret, timeout, sz, t, p;
592 if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
593 ret = chn_start(c, 0);
595 c->flags |= CHN_F_DEAD;
601 timeout = chn_timeout * hz;
603 while (ret == 0 && buf->uio_resid > 0) {
604 sz = min(buf->uio_resid, sndbuf_getready(bs));
607 * The following assumes that the free space in
608 * the buffer can never be less around the
609 * unlock-uiomove-lock sequence.
611 while (ret == 0 && sz > 0) {
612 p = sndbuf_getreadyptr(bs);
613 t = min(sz, sndbuf_getsize(bs) - p);
614 off = sndbuf_getbufofs(bs, p);
616 ret = uiomove(off, t, buf);
619 sndbuf_dispose(bs, NULL, t);
622 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER))
625 ret = chn_sleep(c, timeout);
628 c->flags |= CHN_F_DEAD;
629 device_printf(c->dev, "%s(): %s: "
630 "record interrupt timeout, channel dead\n",
632 } else if (ret == ERESTART || ret == EINTR)
633 c->flags |= CHN_F_ABORTING;
641 chn_intr_locked(struct pcm_channel *c)
648 if (c->direction == PCMDIR_PLAY)
655 chn_intr(struct pcm_channel *c)
658 if (CHN_LOCKOWNED(c)) {
669 chn_start(struct pcm_channel *c, int force)
672 struct snd_dbuf *b = c->bufhard;
673 struct snd_dbuf *bs = c->bufsoft;
677 /* if we're running, or if we're prevented from triggering, bail */
678 if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force))
687 if (c->direction == PCMDIR_REC) {
688 i = sndbuf_getfree(bs);
689 j = (i > 0) ? 1 : sndbuf_getready(b);
691 if (sndbuf_getfree(bs) == 0) {
697 pb = CHN_BUF_PARENT(c, b);
698 i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb);
699 j = sndbuf_getalign(pb);
702 if (snd_verbose > 3 && CHN_EMPTY(c, children))
703 device_printf(c->dev, "%s(): %s (%s) threshold "
704 "i=%d j=%d\n", __func__, CHN_DIRSTR(c),
705 (c->flags & CHN_F_VIRTUAL) ? "virtual" :
710 c->flags |= CHN_F_TRIGGERED;
712 if (c->flags & CHN_F_CLOSING)
719 if (c->parentchannel == NULL) {
720 if (c->direction == PCMDIR_PLAY)
721 sndbuf_fillsilence_rl(b,
722 sndbuf_xbytes(sndbuf_getsize(bs), bs, b));
724 device_printf(c->dev,
725 "%s(): %s starting! (%s/%s) "
726 "(ready=%d force=%d i=%d j=%d "
727 "intrtimeout=%u latency=%dms)\n",
729 (c->flags & CHN_F_HAS_VCHAN) ?
730 "VCHAN PARENT" : "HW", CHN_DIRSTR(c),
731 (c->flags & CHN_F_CLOSING) ? "closing" :
734 force, i, j, c->timeout,
735 (sndbuf_getsize(b) * 1000) /
736 (sndbuf_getalign(b) * sndbuf_getspd(b)));
738 err = chn_trigger(c, PCMTRIG_START);
745 chn_resetbuf(struct pcm_channel *c)
747 struct snd_dbuf *b = c->bufhard;
748 struct snd_dbuf *bs = c->bufsoft;
756 * chn_sync waits until the space in the given channel goes above
757 * a threshold. The threshold is checked against fl or rl respectively.
758 * Assume that the condition can become true, do not check here...
761 chn_sync(struct pcm_channel *c, int threshold)
763 struct snd_dbuf *b, *bs;
764 int ret, count, hcount, minflush, resid, residp, syncdelay, blksz;
769 if (c->direction != PCMDIR_PLAY)
774 if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) ||
775 (threshold < 1 && sndbuf_getready(bs) < 1))
778 /* if we haven't yet started and nothing is buffered, else start*/
779 if (CHN_STOPPED(c)) {
780 if (threshold > 0 || sndbuf_getready(bs) > 0) {
781 ret = chn_start(c, 1);
788 b = CHN_BUF_PARENT(c, c->bufhard);
790 minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs);
792 syncdelay = chn_syncdelay;
794 if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0))
795 minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs);
798 * Append (0-1000) millisecond trailing buffer (if needed)
799 * for slower / high latency hardwares (notably USB audio)
800 * to avoid audible truncation.
803 minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) *
804 ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000;
806 minflush -= minflush % sndbuf_getalign(bs);
809 threshold = min(minflush, sndbuf_getfree(bs));
810 sndbuf_clear(bs, threshold);
811 sndbuf_acquire(bs, NULL, threshold);
812 minflush -= threshold;
815 resid = sndbuf_getready(bs);
817 blksz = sndbuf_getblksz(b);
819 device_printf(c->dev,
820 "%s(): WARNING: blksz < 1 ! maxsize=%d [%d/%d/%d]\n",
821 __func__, sndbuf_getmaxsize(b), sndbuf_getsize(b),
822 sndbuf_getblksz(b), sndbuf_getblkcnt(b));
823 if (sndbuf_getblkcnt(b) > 0)
824 blksz = sndbuf_getsize(b) / sndbuf_getblkcnt(b);
828 count = sndbuf_xbytes(minflush + resid, bs, b) / blksz;
833 device_printf(c->dev, "%s(): [begin] timeout=%d count=%d "
834 "minflush=%d resid=%d\n", __func__, c->timeout, count,
837 cflag = c->flags & CHN_F_CLOSING;
838 c->flags |= CHN_F_CLOSING;
839 while (count > 0 && (resid > 0 || minflush > 0)) {
840 ret = chn_sleep(c, c->timeout);
841 if (ret == ERESTART || ret == EINTR) {
842 c->flags |= CHN_F_ABORTING;
844 } else if (ret == 0 || ret == EAGAIN) {
845 resid = sndbuf_getready(bs);
846 if (resid == residp) {
849 device_printf(c->dev,
850 "%s(): [stalled] timeout=%d "
851 "count=%d hcount=%d "
852 "resid=%d minflush=%d\n",
853 __func__, c->timeout, count,
854 hcount, resid, minflush);
855 } else if (resid < residp && count < hcount) {
858 device_printf(c->dev,
859 "%s((): [resume] timeout=%d "
860 "count=%d hcount=%d "
861 "resid=%d minflush=%d\n",
862 __func__, c->timeout, count,
863 hcount, resid, minflush);
865 if (minflush > 0 && sndbuf_getfree(bs) > 0) {
866 threshold = min(minflush,
868 sndbuf_clear(bs, threshold);
869 sndbuf_acquire(bs, NULL, threshold);
870 resid = sndbuf_getready(bs);
871 minflush -= threshold;
877 c->flags &= ~CHN_F_CLOSING;
881 device_printf(c->dev,
882 "%s(): timeout=%d count=%d hcount=%d resid=%d residp=%d "
883 "minflush=%d ret=%d\n",
884 __func__, c->timeout, count, hcount, resid, residp,
890 /* called externally, handle locking */
892 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
894 struct snd_dbuf *bs = c->bufsoft;
899 if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) {
900 ret = chn_start(c, 1);
906 if (chn_polltrigger(c)) {
910 selrecord(td, sndbuf_getsel(bs));
916 * chn_abort terminates a running dma transfer. it may sleep up to 200ms.
917 * it returns the number of bytes that have not been transferred.
919 * called from: dsp_close, dsp_ioctl, with channel locked
922 chn_abort(struct pcm_channel *c)
925 struct snd_dbuf *b = c->bufhard;
926 struct snd_dbuf *bs = c->bufsoft;
931 c->flags |= CHN_F_ABORTING;
933 c->flags &= ~CHN_F_TRIGGERED;
934 /* kill the channel */
935 chn_trigger(c, PCMTRIG_ABORT);
937 if (!(c->flags & CHN_F_VIRTUAL))
939 missing = sndbuf_getready(bs);
941 c->flags &= ~CHN_F_ABORTING;
946 * this routine tries to flush the dma transfer. It is called
947 * on a close of a playback channel.
948 * first, if there is data in the buffer, but the dma has not yet
949 * begun, we need to start it.
950 * next, we wait for the play buffer to drain
951 * finally, we stop the dma.
953 * called from: dsp_close, not valid for record channels.
957 chn_flush(struct pcm_channel *c)
959 struct snd_dbuf *b = c->bufhard;
962 KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
963 DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags));
965 c->flags |= CHN_F_CLOSING;
967 c->flags &= ~CHN_F_TRIGGERED;
968 /* kill the channel */
969 chn_trigger(c, PCMTRIG_ABORT);
972 c->flags &= ~CHN_F_CLOSING;
977 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist)
981 for (i = 0; fmtlist[i] != 0; i++) {
982 if (fmt == fmtlist[i] ||
983 ((fmt & AFMT_PASSTHROUGH) &&
984 (AFMT_ENCODING(fmt) & fmtlist[i])))
991 static const struct {
992 char *name, *alias1, *alias2;
995 { "alaw", NULL, NULL, AFMT_A_LAW },
996 { "mulaw", NULL, NULL, AFMT_MU_LAW },
997 { "u8", "8", NULL, AFMT_U8 },
998 { "s8", NULL, NULL, AFMT_S8 },
999 #if BYTE_ORDER == LITTLE_ENDIAN
1000 { "s16le", "s16", "16", AFMT_S16_LE },
1001 { "s16be", NULL, NULL, AFMT_S16_BE },
1003 { "s16le", NULL, NULL, AFMT_S16_LE },
1004 { "s16be", "s16", "16", AFMT_S16_BE },
1006 { "u16le", NULL, NULL, AFMT_U16_LE },
1007 { "u16be", NULL, NULL, AFMT_U16_BE },
1008 { "s24le", NULL, NULL, AFMT_S24_LE },
1009 { "s24be", NULL, NULL, AFMT_S24_BE },
1010 { "u24le", NULL, NULL, AFMT_U24_LE },
1011 { "u24be", NULL, NULL, AFMT_U24_BE },
1012 #if BYTE_ORDER == LITTLE_ENDIAN
1013 { "s32le", "s32", "32", AFMT_S32_LE },
1014 { "s32be", NULL, NULL, AFMT_S32_BE },
1016 { "s32le", NULL, NULL, AFMT_S32_LE },
1017 { "s32be", "s32", "32", AFMT_S32_BE },
1019 { "u32le", NULL, NULL, AFMT_U32_LE },
1020 { "u32be", NULL, NULL, AFMT_U32_BE },
1021 { "ac3", NULL, NULL, AFMT_AC3 },
1022 { NULL, NULL, NULL, 0 }
1026 snd_str2afmt(const char *req)
1034 memset(b1, 0, sizeof(b1));
1035 memset(b2, 0, sizeof(b2));
1037 i = sscanf(req, "%5[^:]:%6s", b1, b2);
1040 if (strlen(req) != strlen(b1))
1042 strlcpy(b2, "2.0", sizeof(b2));
1043 } else if (i == 2) {
1044 if (strlen(req) != (strlen(b1) + 1 + strlen(b2)))
1049 i = sscanf(b2, "%d.%d", &ch, &ext);
1052 if (strcasecmp(b2, "mono") == 0) {
1055 } else if (strcasecmp(b2, "stereo") == 0) {
1058 } else if (strcasecmp(b2, "quad") == 0) {
1063 } else if (i == 1) {
1064 if (ch < 1 || ch > AFMT_CHANNEL_MAX)
1067 } else if (i == 2) {
1068 if (ext < 0 || ext > AFMT_EXTCHANNEL_MAX)
1070 if (ch < 1 || (ch + ext) > AFMT_CHANNEL_MAX)
1075 for (i = 0; afmt_tab[i].name != NULL; i++) {
1076 if (strcasecmp(afmt_tab[i].name, b1) != 0) {
1077 if (afmt_tab[i].alias1 == NULL)
1079 if (strcasecmp(afmt_tab[i].alias1, b1) != 0) {
1080 if (afmt_tab[i].alias2 == NULL)
1082 if (strcasecmp(afmt_tab[i].alias2, b1) != 0)
1087 return (SND_FORMAT(afmt_tab[i].afmt, ch + ext, ext));
1089 /* not a valid format */
1094 snd_afmt2str(uint32_t afmt, char *buf, size_t len)
1101 if (buf == NULL || len < AFMTSTR_LEN)
1104 memset(buf, 0, len);
1106 enc = AFMT_ENCODING(afmt);
1107 ch = AFMT_CHANNEL(afmt);
1108 ext = AFMT_EXTCHANNEL(afmt);
1109 /* check there is at least one channel */
1112 for (i = 0; afmt_tab[i].name != NULL; i++) {
1113 if (enc != afmt_tab[i].afmt)
1116 snprintf(buf, len, "%s:%d.%d",
1117 afmt_tab[i].name, ch - ext, ext);
1118 return (SND_FORMAT(enc, ch, ext));
1124 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
1130 c->flags &= CHN_F_RESET;
1135 c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ?
1136 CHN_F_BITPERFECT : 0;
1138 r = CHANNEL_RESET(c->methods, c->devinfo);
1139 if (r == 0 && fmt != 0 && spd != 0) {
1140 r = chn_setparam(c, fmt, spd);
1144 if (r == 0 && fmt != 0)
1145 r = chn_setformat(c, fmt);
1146 if (r == 0 && spd != 0)
1147 r = chn_setspeed(c, spd);
1149 r = chn_setlatency(c, chn_latency);
1152 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
1158 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
1160 struct feeder_class *fc;
1161 struct snd_dbuf *b, *bs;
1164 if (chn_timeout < CHN_TIMEOUT_MIN || chn_timeout > CHN_TIMEOUT_MAX)
1165 chn_timeout = CHN_TIMEOUT;
1167 chn_lockinit(c, dir);
1171 CHN_INIT(c, children);
1172 CHN_INIT(c, children.busy);
1179 b = sndbuf_create(c->dev, c->name, "primary", c);
1182 bs = sndbuf_create(c->dev, c->name, "secondary", c);
1189 fc = feeder_getclass(NULL);
1192 if (chn_addfeeder(c, fc, NULL))
1196 * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
1197 * with the channel unlocked because they are also called
1198 * from driver methods that don't know about locking
1201 sndbuf_setup(bs, NULL, 0);
1208 c->format = SND_FORMAT(AFMT_U8, 1, 0);
1209 c->speed = DSP_DEFAULT_SPEED;
1211 c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
1212 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1214 for (i = 0; i < SND_CHN_T_MAX; i++) {
1215 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
1218 c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
1219 c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
1221 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
1224 CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() malloc() call */
1225 c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
1227 if (c->devinfo == NULL)
1231 if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
1235 c->direction = direction;
1237 sndbuf_setfmt(b, c->format);
1238 sndbuf_setspd(b, c->speed);
1239 sndbuf_setfmt(bs, c->format);
1240 sndbuf_setspd(bs, c->speed);
1243 * @todo Should this be moved somewhere else? The primary buffer
1244 * is allocated by the driver or via DMA map setup, and tmpbuf
1245 * seems to only come into existence in sndbuf_resize().
1247 if (c->direction == PCMDIR_PLAY) {
1248 bs->sl = sndbuf_getmaxsize(bs);
1249 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_NOWAIT);
1250 if (bs->shadbuf == NULL) {
1260 if (CHANNEL_FREE(c->methods, c->devinfo))
1268 c->flags |= CHN_F_DEAD;
1278 chn_kill(struct pcm_channel *c)
1280 struct snd_dbuf *b = c->bufhard;
1281 struct snd_dbuf *bs = c->bufsoft;
1283 if (CHN_STARTED(c)) {
1285 chn_trigger(c, PCMTRIG_ABORT);
1288 while (chn_removefeeder(c) == 0)
1290 if (CHANNEL_FREE(c->methods, c->devinfo))
1295 c->flags |= CHN_F_DEAD;
1301 /* XXX Obsolete. Use *_matrix() variant instead. */
1303 chn_setvolume(struct pcm_channel *c, int left, int right)
1307 ret = chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FL, left);
1308 ret |= chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FR,
1315 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
1322 for (i = 0; i < SND_CHN_T_MAX; i++) {
1323 if ((1 << i) & SND_CHN_LEFT_MASK)
1324 ret |= chn_setvolume_matrix(c, vc, i, left);
1325 else if ((1 << i) & SND_CHN_RIGHT_MASK)
1326 ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
1328 ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
1335 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
1339 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1340 (vc == SND_VOL_C_MASTER || (vc & 1)) &&
1341 (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
1342 vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
1343 (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
1344 ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
1345 __func__, c, vc, vt, val));
1353 c->volume[vc][vt] = val;
1356 * Do relative calculation here and store it into class + 1
1357 * to ease the job of feeder_volume.
1359 if (vc == SND_VOL_C_MASTER) {
1360 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
1361 vc += SND_VOL_C_STEP)
1362 c->volume[SND_VOL_C_VAL(vc)][vt] =
1363 SND_VOL_CALC_VAL(c->volume, vc, vt);
1364 } else if (vc & 1) {
1365 if (vt == SND_CHN_T_VOL_0DB)
1366 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
1367 i += SND_CHN_T_STEP) {
1368 c->volume[SND_VOL_C_VAL(vc)][i] =
1369 SND_VOL_CALC_VAL(c->volume, vc, i);
1372 c->volume[SND_VOL_C_VAL(vc)][vt] =
1373 SND_VOL_CALC_VAL(c->volume, vc, vt);
1380 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
1382 KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
1383 (vt == SND_CHN_T_VOL_0DB ||
1384 (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
1385 ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
1386 __func__, c, vc, vt));
1389 return (c->volume[vc][vt]);
1392 struct pcmchan_matrix *
1393 chn_getmatrix(struct pcm_channel *c)
1396 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1399 if (!(c->format & AFMT_CONVERTIBLE))
1402 return (&c->matrix);
1406 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
1409 KASSERT(c != NULL && m != NULL,
1410 ("%s(): NULL channel or matrix", __func__));
1413 if (!(c->format & AFMT_CONVERTIBLE))
1417 c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
1419 return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
1423 * XXX chn_oss_* exists for the sake of compatibility.
1426 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
1429 KASSERT(c != NULL && map != NULL,
1430 ("%s(): NULL channel or map", __func__));
1433 if (!(c->format & AFMT_CONVERTIBLE))
1436 return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
1440 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
1442 struct pcmchan_matrix m;
1445 KASSERT(c != NULL && map != NULL,
1446 ("%s(): NULL channel or map", __func__));
1449 if (!(c->format & AFMT_CONVERTIBLE))
1453 ret = feeder_matrix_oss_set_channel_order(&m, map);
1457 return (chn_setmatrix(c, &m));
1460 #define SND_CHN_OSS_FRONT (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
1461 #define SND_CHN_OSS_SURR (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
1462 #define SND_CHN_OSS_CENTER_LFE (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
1463 #define SND_CHN_OSS_REAR (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
1466 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
1468 struct pcmchan_matrix *m;
1469 struct pcmchan_caps *caps;
1472 KASSERT(c != NULL && retmask != NULL,
1473 ("%s(): NULL channel or retmask", __func__));
1476 caps = chn_getcaps(c);
1477 if (caps == NULL || caps->fmtlist == NULL)
1480 for (i = 0; caps->fmtlist[i] != 0; i++) {
1481 format = caps->fmtlist[i];
1482 if (!(format & AFMT_CONVERTIBLE)) {
1483 *retmask |= DSP_BIND_SPDIF;
1486 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
1489 if (m->mask & SND_CHN_OSS_FRONT)
1490 *retmask |= DSP_BIND_FRONT;
1491 if (m->mask & SND_CHN_OSS_SURR)
1492 *retmask |= DSP_BIND_SURR;
1493 if (m->mask & SND_CHN_OSS_CENTER_LFE)
1494 *retmask |= DSP_BIND_CENTER_LFE;
1495 if (m->mask & SND_CHN_OSS_REAR)
1496 *retmask |= DSP_BIND_REAR;
1499 /* report software-supported binding mask */
1500 if (!CHN_BITPERFECT(c) && report_soft_matrix)
1501 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
1502 DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
1508 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
1512 KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
1513 ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
1516 if (force == 0 && chn_vpc_autoreset == 0)
1519 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
1520 CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
1524 round_pow2(u_int32_t v)
1533 ret = 1 << (ret - 1);
1540 round_blksz(u_int32_t v, int round)
1547 ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
1549 if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
1552 tmp = ret - (ret % round);
1553 while (tmp < 16 || tmp < round) {
1555 tmp = ret - (ret % round);
1562 * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
1563 * is to keep 2nd buffer short so that it doesn't cause long queue during
1566 * Latency reference table for 48khz stereo 16bit: (PLAY)
1568 * +---------+------------+-----------+------------+
1569 * | Latency | Blockcount | Blocksize | Buffersize |
1570 * +---------+------------+-----------+------------+
1571 * | 0 | 2 | 64 | 128 |
1572 * +---------+------------+-----------+------------+
1573 * | 1 | 4 | 128 | 512 |
1574 * +---------+------------+-----------+------------+
1575 * | 2 | 8 | 512 | 4096 |
1576 * +---------+------------+-----------+------------+
1577 * | 3 | 16 | 512 | 8192 |
1578 * +---------+------------+-----------+------------+
1579 * | 4 | 32 | 512 | 16384 |
1580 * +---------+------------+-----------+------------+
1581 * | 5 | 32 | 1024 | 32768 |
1582 * +---------+------------+-----------+------------+
1583 * | 6 | 16 | 2048 | 32768 |
1584 * +---------+------------+-----------+------------+
1585 * | 7 | 8 | 4096 | 32768 |
1586 * +---------+------------+-----------+------------+
1587 * | 8 | 4 | 8192 | 32768 |
1588 * +---------+------------+-----------+------------+
1589 * | 9 | 2 | 16384 | 32768 |
1590 * +---------+------------+-----------+------------+
1591 * | 10 | 2 | 32768 | 65536 |
1592 * +---------+------------+-----------+------------+
1594 * Recording need a different reference table. All we care is
1595 * gobbling up everything within reasonable buffering threshold.
1597 * Latency reference table for 48khz stereo 16bit: (REC)
1599 * +---------+------------+-----------+------------+
1600 * | Latency | Blockcount | Blocksize | Buffersize |
1601 * +---------+------------+-----------+------------+
1602 * | 0 | 512 | 32 | 16384 |
1603 * +---------+------------+-----------+------------+
1604 * | 1 | 256 | 64 | 16384 |
1605 * +---------+------------+-----------+------------+
1606 * | 2 | 128 | 128 | 16384 |
1607 * +---------+------------+-----------+------------+
1608 * | 3 | 64 | 256 | 16384 |
1609 * +---------+------------+-----------+------------+
1610 * | 4 | 32 | 512 | 16384 |
1611 * +---------+------------+-----------+------------+
1612 * | 5 | 32 | 1024 | 32768 |
1613 * +---------+------------+-----------+------------+
1614 * | 6 | 16 | 2048 | 32768 |
1615 * +---------+------------+-----------+------------+
1616 * | 7 | 8 | 4096 | 32768 |
1617 * +---------+------------+-----------+------------+
1618 * | 8 | 4 | 8192 | 32768 |
1619 * +---------+------------+-----------+------------+
1620 * | 9 | 2 | 16384 | 32768 |
1621 * +---------+------------+-----------+------------+
1622 * | 10 | 2 | 32768 | 65536 |
1623 * +---------+------------+-----------+------------+
1625 * Calculations for other data rate are entirely based on these reference
1626 * tables. For normal operation, Latency 5 seems give the best, well
1627 * balanced performance for typical workload. Anything below 5 will
1628 * eat up CPU to keep up with increasing context switches because of
1629 * shorter buffer space and usually require the application to handle it
1630 * aggresively through possibly real time programming technique.
1633 #define CHN_LATENCY_PBLKCNT_REF \
1634 {{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}, \
1635 {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}}
1636 #define CHN_LATENCY_PBUFSZ_REF \
1637 {{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16}, \
1638 {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}}
1640 #define CHN_LATENCY_RBLKCNT_REF \
1641 {{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}, \
1642 {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}}
1643 #define CHN_LATENCY_RBUFSZ_REF \
1644 {{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16}, \
1645 {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}}
1647 #define CHN_LATENCY_DATA_REF 192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
1650 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
1651 u_int32_t max, int *rblksz, int *rblkcnt)
1653 static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1654 CHN_LATENCY_PBLKCNT_REF;
1655 static int pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1656 CHN_LATENCY_PBUFSZ_REF;
1657 static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1658 CHN_LATENCY_RBLKCNT_REF;
1659 static int rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
1660 CHN_LATENCY_RBUFSZ_REF;
1662 int lprofile, blksz, blkcnt;
1664 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
1665 bps < 1 || datarate < 1 ||
1666 !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
1668 *rblksz = CHN_2NDBUFMAXSIZE >> 1;
1669 if (rblkcnt != NULL)
1671 printf("%s(): FAILED dir=%d latency=%d bps=%d "
1672 "datarate=%u max=%u\n",
1673 __func__, dir, latency, bps, datarate, max);
1674 return CHN_2NDBUFMAXSIZE;
1677 lprofile = chn_latency_profile;
1679 if (dir == PCMDIR_PLAY) {
1680 blkcnt = pblkcnts[lprofile][latency];
1681 bufsz = pbufszs[lprofile][latency];
1683 blkcnt = rblkcnts[lprofile][latency];
1684 bufsz = rbufszs[lprofile][latency];
1687 bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
1691 blksz = round_blksz(bufsz >> blkcnt, bps);
1695 if (rblkcnt != NULL)
1696 *rblkcnt = 1 << blkcnt;
1698 return blksz << blkcnt;
1702 chn_resizebuf(struct pcm_channel *c, int latency,
1703 int blkcnt, int blksz)
1705 struct snd_dbuf *b, *bs, *pb;
1706 int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
1711 if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
1712 !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
1715 if (latency == -1) {
1717 latency = chn_latency;
1718 } else if (latency == -2) {
1719 latency = c->latency;
1720 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1721 latency = chn_latency;
1722 } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
1725 c->latency = latency;
1731 if (!(blksz == 0 || blkcnt == -1) &&
1732 (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 ||
1733 (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
1736 chn_calclatency(c->direction, latency, sndbuf_getalign(bs),
1737 sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE,
1740 if (blksz == 0 || blkcnt == -1) {
1742 c->flags &= ~CHN_F_HAS_SIZE;
1743 if (c->flags & CHN_F_HAS_SIZE) {
1744 blksz = sndbuf_getblksz(bs);
1745 blkcnt = sndbuf_getblkcnt(bs);
1748 c->flags |= CHN_F_HAS_SIZE;
1750 if (c->flags & CHN_F_HAS_SIZE) {
1752 * The application has requested their own blksz/blkcnt.
1753 * Just obey with it, and let them toast alone. We can
1754 * clamp it to the nearest latency profile, but that would
1755 * defeat the purpose of having custom control. The least
1756 * we can do is round it to the nearest ^2 and align it.
1758 sblksz = round_blksz(blksz, sndbuf_getalign(bs));
1759 sblkcnt = round_pow2(blkcnt);
1762 if (c->parentchannel != NULL) {
1763 pb = c->parentchannel->bufsoft;
1765 CHN_LOCK(c->parentchannel);
1766 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
1767 CHN_UNLOCK(c->parentchannel);
1769 if (c->direction == PCMDIR_PLAY) {
1770 limit = (pb != NULL) ?
1771 sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0;
1773 limit = (pb != NULL) ?
1774 sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0;
1778 if (c->flags & CHN_F_HAS_SIZE) {
1779 hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
1780 sndbuf_getalign(b));
1781 hblkcnt = round_pow2(sndbuf_getblkcnt(bs));
1783 chn_calclatency(c->direction, latency,
1785 sndbuf_getalign(b) * sndbuf_getspd(b),
1786 CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
1788 if ((hblksz << 1) > sndbuf_getmaxsize(b))
1789 hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1,
1790 sndbuf_getalign(b));
1792 while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) {
1799 hblksz -= hblksz % sndbuf_getalign(b);
1802 hblksz = sndbuf_getmaxsize(b) >> 1;
1803 hblksz -= hblksz % sndbuf_getalign(b);
1808 if (chn_usefrags == 0 ||
1809 CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
1810 hblksz, hblkcnt) != 0)
1811 sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods,
1812 c->devinfo, hblksz));
1815 if (!CHN_EMPTY(c, children)) {
1816 nsblksz = round_blksz(
1817 sndbuf_xbytes(sndbuf_getblksz(b), b, bs),
1818 sndbuf_getalign(bs));
1819 nsblkcnt = sndbuf_getblkcnt(b);
1820 if (c->direction == PCMDIR_PLAY) {
1823 } while (nsblkcnt >= 2 &&
1824 nsblksz * nsblkcnt >= sblksz * sblkcnt);
1831 limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2;
1834 if (limit > CHN_2NDBUFMAXSIZE)
1835 limit = CHN_2NDBUFMAXSIZE;
1838 while (limit > 0 && (sblksz * sblkcnt) > limit) {
1845 while ((sblksz * sblkcnt) < limit)
1848 while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
1855 sblksz -= sblksz % sndbuf_getalign(bs);
1857 if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz ||
1858 sndbuf_getsize(bs) != (sblkcnt * sblksz)) {
1859 ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
1861 device_printf(c->dev, "%s(): Failed: %d %d\n",
1862 __func__, sblkcnt, sblksz);
1870 c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) /
1871 ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs));
1872 if (c->parentchannel != NULL)
1873 c->timeout = min(c->timeout, c->parentchannel->timeout);
1878 * OSSv4 docs: "By default OSS will set the low water level equal
1879 * to the fragment size which is optimal in most cases."
1881 c->lw = sndbuf_getblksz(bs);
1884 if (snd_verbose > 3)
1885 device_printf(c->dev, "%s(): %s (%s) timeout=%u "
1886 "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
1887 __func__, CHN_DIRSTR(c),
1888 (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
1890 sndbuf_getsize(b), sndbuf_getblksz(b),
1891 sndbuf_getblkcnt(b),
1892 sndbuf_getsize(bs), sndbuf_getblksz(bs),
1893 sndbuf_getblkcnt(bs), limit);
1899 chn_setlatency(struct pcm_channel *c, int latency)
1902 /* Destroy blksz/blkcnt, enforce latency profile. */
1903 return chn_resizebuf(c, latency, -1, 0);
1907 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
1910 /* Destroy latency profile, enforce blksz/blkcnt */
1911 return chn_resizebuf(c, -1, blkcnt, blksz);
1915 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
1917 struct pcmchan_caps *caps;
1918 uint32_t hwspeed, delta;
1923 if (speed < 1 || format == 0 || CHN_STARTED(c))
1929 caps = chn_getcaps(c);
1932 RANGE(hwspeed, caps->minspeed, caps->maxspeed);
1934 sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
1936 hwspeed = sndbuf_getspd(c->bufhard);
1938 delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
1940 if (delta <= feeder_rate_round)
1943 ret = feeder_chain(c);
1946 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo,
1947 sndbuf_getfmt(c->bufhard));
1950 ret = chn_resizebuf(c, -2, 0, 0);
1956 chn_setspeed(struct pcm_channel *c, uint32_t speed)
1958 uint32_t oldformat, oldspeed, format;
1963 if (c->format & AFMT_PASSTHROUGH)
1964 speed = AFMT_PASSTHROUGH_RATE;
1967 oldformat = c->format;
1968 oldspeed = c->speed;
1971 ret = chn_setparam(c, format, speed);
1973 if (snd_verbose > 3)
1974 device_printf(c->dev,
1975 "%s(): Setting speed %d failed, "
1976 "falling back to %d\n",
1977 __func__, speed, oldspeed);
1978 chn_setparam(c, c->format, oldspeed);
1985 chn_setformat(struct pcm_channel *c, uint32_t format)
1987 uint32_t oldformat, oldspeed, speed;
1990 /* XXX force stereo */
1991 if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
1992 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
1993 AFMT_PASSTHROUGH_EXTCHANNEL);
1996 oldformat = c->format;
1997 oldspeed = c->speed;
2000 ret = chn_setparam(c, format, speed);
2002 if (snd_verbose > 3)
2003 device_printf(c->dev,
2004 "%s(): Format change 0x%08x failed, "
2005 "falling back to 0x%08x\n",
2006 __func__, format, oldformat);
2007 chn_setparam(c, oldformat, oldspeed);
2014 chn_syncstate(struct pcm_channel *c)
2016 struct snddev_info *d;
2017 struct snd_mixer *m;
2019 d = (c != NULL) ? c->parentsnddev : NULL;
2020 m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
2023 if (d == NULL || m == NULL)
2028 if (c->feederflags & (1 << FEEDER_VOLUME)) {
2030 int vol, pvol, left, right, center;
2032 if (c->direction == PCMDIR_PLAY &&
2033 (d->flags & SD_F_SOFTPCMVOL)) {
2034 /* CHN_UNLOCK(c); */
2035 vol = mix_get(m, SOUND_MIXER_PCM);
2036 parent = mix_getparent(m, SOUND_MIXER_PCM);
2037 if (parent != SOUND_MIXER_NONE)
2038 pvol = mix_get(m, parent);
2040 pvol = 100 | (100 << 8);
2043 vol = 100 | (100 << 8);
2048 device_printf(c->dev,
2049 "Soft PCM Volume: Failed to read pcm "
2051 vol = 100 | (100 << 8);
2055 device_printf(c->dev,
2056 "Soft PCM Volume: Failed to read parent "
2058 pvol = 100 | (100 << 8);
2061 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
2062 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
2063 center = (left + right) >> 1;
2065 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
2068 if (c->feederflags & (1 << FEEDER_EQ)) {
2069 struct pcm_feeder *f;
2070 int treble, bass, state;
2072 /* CHN_UNLOCK(c); */
2073 treble = mix_get(m, SOUND_MIXER_TREBLE);
2074 bass = mix_get(m, SOUND_MIXER_BASS);
2080 treble = ((treble & 0x7f) +
2081 ((treble >> 8) & 0x7f)) >> 1;
2086 bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
2088 f = chn_findfeeder(c, FEEDER_EQ);
2090 if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
2091 device_printf(c->dev,
2092 "EQ: Failed to set treble -- %d\n",
2094 if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
2095 device_printf(c->dev,
2096 "EQ: Failed to set bass -- %d\n",
2098 if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
2099 device_printf(c->dev,
2100 "EQ: Failed to set preamp -- %d\n",
2102 if (d->flags & SD_F_EQ_BYPASSED)
2103 state = FEEDEQ_BYPASS;
2104 else if (d->flags & SD_F_EQ_ENABLED)
2105 state = FEEDEQ_ENABLE;
2107 state = FEEDEQ_DISABLE;
2108 if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
2109 device_printf(c->dev,
2110 "EQ: Failed to set state -- %d\n", state);
2116 chn_trigger(struct pcm_channel *c, int go)
2119 struct snd_dbuf *b = c->bufhard;
2121 struct snddev_info *d = c->parentsnddev;
2126 if (SND_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
2127 sndbuf_dmabounce(b);
2129 if (!PCMTRIG_COMMON(go))
2130 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
2132 if (go == c->trigger)
2135 ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
2141 if (snd_verbose > 3)
2142 device_printf(c->dev,
2143 "%s() %s: calling go=0x%08x , "
2144 "prev=0x%08x\n", __func__, c->name, go,
2146 if (c->trigger != PCMTRIG_START) {
2150 CHN_INSERT_HEAD(d, c, channels.pcm.busy);
2158 if (snd_verbose > 3)
2159 device_printf(c->dev,
2160 "%s() %s: calling go=0x%08x , "
2161 "prev=0x%08x\n", __func__, c->name, go,
2163 if (c->trigger == PCMTRIG_START) {
2167 CHN_REMOVE(d, c, channels.pcm.busy);
2180 * @brief Queries sound driver for sample-aligned hardware buffer pointer index
2182 * This function obtains the hardware pointer location, then aligns it to
2183 * the current bytes-per-sample value before returning. (E.g., a channel
2184 * running in 16 bit stereo mode would require 4 bytes per sample, so a
2185 * hwptr value ranging from 32-35 would be returned as 32.)
2187 * @param c PCM channel context
2188 * @returns sample-aligned hardware buffer pointer index
2191 chn_getptr(struct pcm_channel *c)
2196 hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
2197 return (hwptr - (hwptr % sndbuf_getalign(c->bufhard)));
2200 struct pcmchan_caps *
2201 chn_getcaps(struct pcm_channel *c)
2204 return CHANNEL_GETCAPS(c->methods, c->devinfo);
2208 chn_getformats(struct pcm_channel *c)
2210 u_int32_t *fmtlist, fmts;
2213 fmtlist = chn_getcaps(c)->fmtlist;
2215 for (i = 0; fmtlist[i]; i++)
2218 /* report software-supported formats */
2219 if (!CHN_BITPERFECT(c) && report_soft_formats)
2220 fmts |= AFMT_CONVERTIBLE;
2222 return (AFMT_ENCODING(fmts));
2226 chn_notify(struct pcm_channel *c, u_int32_t flags)
2228 struct pcm_channel *ch;
2229 struct pcmchan_caps *caps;
2230 uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
2232 int dirty, err, run, nrun;
2236 if (CHN_EMPTY(c, children))
2242 * If the hwchan is running, we can't change its rate, format or
2245 run = (CHN_STARTED(c)) ? 1 : 0;
2247 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
2249 if (flags & CHN_N_RATE) {
2251 * XXX I'll make good use of this someday.
2252 * However this is currently being superseded by
2253 * the availability of CHN_F_VCHAN_DYNAMIC.
2257 if (flags & CHN_N_FORMAT) {
2259 * XXX I'll make good use of this someday.
2260 * However this is currently being superseded by
2261 * the availability of CHN_F_VCHAN_DYNAMIC.
2265 if (flags & CHN_N_VOLUME) {
2267 * XXX I'll make good use of this someday, though
2268 * soft volume control is currently pretty much
2273 if (flags & CHN_N_BLOCKSIZE) {
2275 * Set to default latency profile
2277 chn_setlatency(c, chn_latency);
2280 if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) {
2281 nrun = CHN_EMPTY(c, children.busy) ? 0 : 1;
2283 err = chn_start(c, 1);
2286 flags &= ~CHN_N_TRIGGER;
2289 if (flags & CHN_N_TRIGGER) {
2290 if (c->direction == PCMDIR_PLAY) {
2291 vchanformat = &c->parentsnddev->pvchanformat;
2292 vchanrate = &c->parentsnddev->pvchanrate;
2294 vchanformat = &c->parentsnddev->rvchanformat;
2295 vchanrate = &c->parentsnddev->rvchanrate;
2298 /* Dynamic Virtual Channel */
2299 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
2300 bestformat = *vchanformat;
2301 bestspeed = *vchanrate;
2309 caps = chn_getcaps(c);
2313 CHN_FOREACH(ch, c, children.busy) {
2315 if ((ch->format & AFMT_PASSTHROUGH) &&
2316 snd_fmtvalid(ch->format, caps->fmtlist)) {
2317 bestformat = ch->format;
2318 bestspeed = ch->speed;
2320 vpflags = CHN_F_PASSTHROUGH;
2324 if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
2325 if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
2326 bestspeed = ch->speed;
2327 RANGE(bestspeed, caps->minspeed,
2329 besthwformat = snd_fmtbest(ch->format,
2331 if (besthwformat != 0)
2332 bestformat = besthwformat;
2335 vpflags = CHN_F_EXCLUSIVE;
2339 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
2345 if (ch->speed > bestspeed) {
2346 bestspeed = ch->speed;
2347 RANGE(bestspeed, caps->minspeed,
2350 besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
2351 if (!(besthwformat & AFMT_VCHAN)) {
2356 if (AFMT_CHANNEL(besthwformat) >
2357 AFMT_CHANNEL(bestformat))
2358 bestformat = besthwformat;
2359 else if (AFMT_CHANNEL(besthwformat) ==
2360 AFMT_CHANNEL(bestformat) &&
2361 AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
2362 bestformat = besthwformat;
2367 if (bestformat == 0)
2368 bestformat = c->format;
2370 bestspeed = c->speed;
2372 if (bestformat != c->format || bestspeed != c->speed)
2375 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2376 c->flags |= vpflags;
2380 bestspeed = CHANNEL_SETSPEED(c->methods,
2381 c->devinfo, bestspeed);
2382 err = chn_reset(c, bestformat, bestspeed);
2384 if (err == 0 && dirty) {
2385 CHN_FOREACH(ch, c, children.busy) {
2387 if (VCHAN_SYNC_REQUIRED(ch))
2394 c->flags |= CHN_F_DIRTY;
2395 err = chn_start(c, 1);
2399 if (nrun && run && dirty) {
2401 bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
2403 err = chn_reset(c, bestformat, bestspeed);
2405 CHN_FOREACH(ch, c, children.busy) {
2407 if (VCHAN_SYNC_REQUIRED(ch))
2413 c->flags |= CHN_F_DIRTY;
2414 err = chn_start(c, 1);
2418 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
2419 (bestformat & AFMT_VCHAN)) {
2420 *vchanformat = bestformat;
2421 *vchanrate = bestspeed;
2425 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
2426 bestformat = *vchanformat;
2427 bestspeed = *vchanrate;
2429 if (c->format != bestformat || c->speed != bestspeed)
2430 chn_reset(c, bestformat, bestspeed);
2438 * @brief Fetch array of supported discrete sample rates
2440 * Wrapper for CHANNEL_GETRATES. Please see channel_if.m:getrates() for
2441 * detailed information.
2443 * @note If the operation isn't supported, this function will just return 0
2444 * (no rates in the array), and *rates will be set to NULL. Callers
2445 * should examine rates @b only if this function returns non-zero.
2447 * @param c pcm channel to examine
2448 * @param rates pointer to array of integers; rate table will be recorded here
2450 * @return number of rates in the array pointed to be @c rates
2453 chn_getrates(struct pcm_channel *c, int **rates)
2455 KASSERT(rates != NULL, ("rates is null"));
2457 return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
2461 * @brief Remove channel from a sync group, if there is one.
2463 * This function is initially intended for the following conditions:
2464 * - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
2465 * - Closing a device. (A channel can't be destroyed if it's still in use.)
2467 * @note Before calling this function, the syncgroup list mutex must be
2468 * held. (Consider pcm_channel::sm protected by the SG list mutex
2469 * whether @c c is locked or not.)
2471 * @param c channel device to be started or closed
2472 * @returns If this channel was the only member of a group, the group ID
2473 * is returned to the caller so that the caller can release it
2474 * via free_unr() after giving up the syncgroup lock. Else it
2478 chn_syncdestroy(struct pcm_channel *c)
2480 struct pcmchan_syncmember *sm;
2481 struct pcmchan_syncgroup *sg;
2486 PCM_SG_LOCKASSERT(MA_OWNED);
2488 if (c->sm != NULL) {
2493 KASSERT(sg != NULL, ("syncmember has null parent"));
2495 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
2498 if (SLIST_EMPTY(&sg->members)) {
2499 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2508 #ifdef OSSV4_EXPERIMENT
2510 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
2513 return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);