The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/sound/pcm/channel.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    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
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   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.
   16  *
   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
   27  * SUCH DAMAGE.
   28  */
   29 
   30 #include "opt_isa.h"
   31 
   32 #ifdef HAVE_KERNEL_OPTION_HEADERS
   33 #include "opt_snd.h"
   34 #endif
   35 
   36 #include <dev/sound/pcm/sound.h>
   37 #include <dev/sound/pcm/vchan.h>
   38 
   39 #include "feeder_if.h"
   40 
   41 SND_DECLARE_FILE("$FreeBSD: releng/9.2/sys/dev/sound/pcm/channel.c 233164 2012-03-19 07:24:26Z mav $");
   42 
   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");
   46 
   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");
   50 
   51 int chn_latency = CHN_LATENCY_DEFAULT;
   52 TUNABLE_INT("hw.snd.latency", &chn_latency);
   53 
   54 static int
   55 sysctl_hw_snd_latency(SYSCTL_HANDLER_ARGS)
   56 {
   57         int err, val;
   58 
   59         val = chn_latency;
   60         err = sysctl_handle_int(oidp, &val, 0, req);
   61         if (err != 0 || req->newptr == NULL)
   62                 return err;
   63         if (val < CHN_LATENCY_MIN || val > CHN_LATENCY_MAX)
   64                 err = EINVAL;
   65         else
   66                 chn_latency = val;
   67 
   68         return err;
   69 }
   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)");
   73 
   74 int chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
   75 TUNABLE_INT("hw.snd.latency_profile", &chn_latency_profile);
   76 
   77 static int
   78 sysctl_hw_snd_latency_profile(SYSCTL_HANDLER_ARGS)
   79 {
   80         int err, val;
   81 
   82         val = chn_latency_profile;
   83         err = sysctl_handle_int(oidp, &val, 0, req);
   84         if (err != 0 || req->newptr == NULL)
   85                 return err;
   86         if (val < CHN_LATENCY_PROFILE_MIN || val > CHN_LATENCY_PROFILE_MAX)
   87                 err = EINVAL;
   88         else
   89                 chn_latency_profile = val;
   90 
   91         return err;
   92 }
   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)");
   96 
   97 static int chn_timeout = CHN_TIMEOUT;
   98 TUNABLE_INT("hw.snd.timeout", &chn_timeout);
   99 #ifdef SND_DEBUG
  100 static int
  101 sysctl_hw_snd_timeout(SYSCTL_HANDLER_ARGS)
  102 {
  103         int err, val;
  104 
  105         val = chn_timeout;
  106         err = sysctl_handle_int(oidp, &val, 0, req);
  107         if (err != 0 || req->newptr == NULL)
  108                 return err;
  109         if (val < CHN_TIMEOUT_MIN || val > CHN_TIMEOUT_MAX)
  110                 err = EINVAL;
  111         else
  112                 chn_timeout = val;
  113 
  114         return err;
  115 }
  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");
  119 #endif
  120 
  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");
  125 
  126 static int chn_vol_0db_pcm = SND_VOL_0DB_PCM;
  127 TUNABLE_INT("hw.snd.vpc_0db", &chn_vol_0db_pcm);
  128 
  129 static void
  130 chn_vpc_proc(int reset, int db)
  131 {
  132         struct snddev_info *d;
  133         struct pcm_channel *c;
  134         int i;
  135 
  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))
  140                         continue;
  141                 PCM_LOCK(d);
  142                 PCM_WAIT(d);
  143                 PCM_ACQUIRE(d);
  144                 CHN_FOREACH(c, d, channels.pcm) {
  145                         CHN_LOCK(c);
  146                         CHN_SETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_VOL_0DB, db);
  147                         if (reset != 0)
  148                                 chn_vpc_reset(c, SND_VOL_C_PCM, 1);
  149                         CHN_UNLOCK(c);
  150                 }
  151                 PCM_RELEASE(d);
  152                 PCM_UNLOCK(d);
  153         }
  154 }
  155 
  156 static int
  157 sysctl_hw_snd_vpc_0db(SYSCTL_HANDLER_ARGS)
  158 {
  159         int err, val;
  160 
  161         val = chn_vol_0db_pcm;
  162         err = sysctl_handle_int(oidp, &val, 0, req);
  163         if (err != 0 || req->newptr == NULL)
  164                 return (err);
  165         if (val < SND_VOL_0DB_MIN || val > SND_VOL_0DB_MAX)
  166                 return (EINVAL);
  167 
  168         chn_vol_0db_pcm = val;
  169         chn_vpc_proc(0, val);
  170 
  171         return (0);
  172 }
  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");
  176 
  177 static int
  178 sysctl_hw_snd_vpc_reset(SYSCTL_HANDLER_ARGS)
  179 {
  180         int err, val;
  181 
  182         val = 0;
  183         err = sysctl_handle_int(oidp, &val, 0, req);
  184         if (err != 0 || req->newptr == NULL || val == 0)
  185                 return (err);
  186 
  187         chn_vol_0db_pcm = SND_VOL_0DB_PCM;
  188         chn_vpc_proc(1, SND_VOL_0DB_PCM);
  189 
  190         return (0);
  191 }
  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");
  195 
  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);
  200 #ifdef SND_DEBUG
  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,
  204         &chn_syncdelay, 1,
  205         "append (0-1000) millisecond trailing buffer delay on each sync");
  206 #endif
  207 
  208 /**
  209  * @brief Channel sync group lock
  210  *
  211  * Clients should acquire this lock @b without holding any channel locks
  212  * before touching syncgroups or the main syncgroup list.
  213  */
  214 struct mtx snd_pcm_syncgroups_mtx;
  215 MTX_SYSINIT(pcm_syncgroup, &snd_pcm_syncgroups_mtx, "PCM channel sync group lock", MTX_DEF);
  216 /**
  217  * @brief syncgroups' master list
  218  *
  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.
  221  *
  222  * See SNDCTL_DSP_SYNCGROUP for more information.
  223  */
  224 struct pcm_synclist snd_pcm_syncgroups = SLIST_HEAD_INITIALIZER(snd_pcm_syncgroups);
  225 
  226 static void
  227 chn_lockinit(struct pcm_channel *c, int dir)
  228 {
  229         switch (dir) {
  230         case PCMDIR_PLAY:
  231                 c->lock = snd_mtxcreate(c->name, "pcm play channel");
  232                 cv_init(&c->intr_cv, "pcmwr");
  233                 break;
  234         case PCMDIR_PLAY_VIRTUAL:
  235                 c->lock = snd_mtxcreate(c->name, "pcm virtual play channel");
  236                 cv_init(&c->intr_cv, "pcmwrv");
  237                 break;
  238         case PCMDIR_REC:
  239                 c->lock = snd_mtxcreate(c->name, "pcm record channel");
  240                 cv_init(&c->intr_cv, "pcmrd");
  241                 break;
  242         case PCMDIR_REC_VIRTUAL:
  243                 c->lock = snd_mtxcreate(c->name, "pcm virtual record channel");
  244                 cv_init(&c->intr_cv, "pcmrdv");
  245                 break;
  246         default:
  247                 panic("%s(): Invalid direction=%d", __func__, dir);
  248                 break;
  249         }
  250 
  251         cv_init(&c->cv, "pcmchn");
  252 }
  253 
  254 static void
  255 chn_lockdestroy(struct pcm_channel *c)
  256 {
  257         CHN_LOCKASSERT(c);
  258 
  259         CHN_BROADCAST(&c->cv);
  260         CHN_BROADCAST(&c->intr_cv);
  261 
  262         cv_destroy(&c->cv);
  263         cv_destroy(&c->intr_cv);
  264 
  265         snd_mtxfree(c->lock);
  266 }
  267 
  268 /**
  269  * @brief Determine channel is ready for I/O
  270  *
  271  * @retval 1 = ready for I/O
  272  * @retval 0 = not ready for I/O
  273  */
  274 static int
  275 chn_polltrigger(struct pcm_channel *c)
  276 {
  277         struct snd_dbuf *bs = c->bufsoft;
  278         u_int delta;
  279 
  280         CHN_LOCKASSERT(c);
  281 
  282         if (c->flags & CHN_F_MMAP) {
  283                 if (sndbuf_getprevtotal(bs) < c->lw)
  284                         delta = c->lw;
  285                 else
  286                         delta = sndbuf_gettotal(bs) - sndbuf_getprevtotal(bs);
  287         } else {
  288                 if (c->direction == PCMDIR_PLAY)
  289                         delta = sndbuf_getfree(bs);
  290                 else
  291                         delta = sndbuf_getready(bs);
  292         }
  293 
  294         return ((delta < c->lw) ? 0 : 1);
  295 }
  296 
  297 static void
  298 chn_pollreset(struct pcm_channel *c)
  299 {
  300 
  301         CHN_LOCKASSERT(c);
  302         sndbuf_updateprevtotal(c->bufsoft);
  303 }
  304 
  305 static void
  306 chn_wakeup(struct pcm_channel *c)
  307 {
  308         struct snd_dbuf *bs;
  309         struct pcm_channel *ch;
  310 
  311         CHN_LOCKASSERT(c);
  312 
  313         bs = c->bufsoft;
  314 
  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) {
  319                         /*
  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...
  323                          */
  324                         CHN_BROADCAST(&c->intr_cv);
  325                 }
  326         } else {
  327                 CHN_FOREACH(ch, c, children.busy) {
  328                         CHN_LOCK(ch);
  329                         chn_wakeup(ch);
  330                         CHN_UNLOCK(ch);
  331                 }
  332         }
  333 }
  334 
  335 static int
  336 chn_sleep(struct pcm_channel *c, int timeout)
  337 {
  338         int ret;
  339 
  340         CHN_LOCKASSERT(c);
  341 
  342         if (c->flags & CHN_F_DEAD)
  343                 return (EINVAL);
  344 
  345         c->flags |= CHN_F_SLEEPING;
  346         ret = cv_timedwait_sig(&c->intr_cv, c->lock, timeout);
  347         c->flags &= ~CHN_F_SLEEPING;
  348 
  349         return ((c->flags & CHN_F_DEAD) ? EINVAL : ret);
  350 }
  351 
  352 /*
  353  * chn_dmaupdate() tracks the status of a dma transfer,
  354  * updating pointers.
  355  */
  356 
  357 static unsigned int
  358 chn_dmaupdate(struct pcm_channel *c)
  359 {
  360         struct snd_dbuf *b = c->bufhard;
  361         unsigned int delta, old, hwptr, amt;
  362 
  363         KASSERT(sndbuf_getsize(b) > 0, ("bufsize == 0"));
  364         CHN_LOCKASSERT(c);
  365 
  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);
  370 
  371         if (c->direction == PCMDIR_PLAY) {
  372                 amt = min(delta, sndbuf_getready(b));
  373                 amt -= amt % sndbuf_getalign(b);
  374                 if (amt > 0)
  375                         sndbuf_dispose(b, NULL, amt);
  376         } else {
  377                 amt = min(delta, sndbuf_getfree(b));
  378                 amt -= amt % sndbuf_getalign(b);
  379                 if (amt > 0)
  380                        sndbuf_acquire(b, NULL, amt);
  381         }
  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));
  388         }
  389 
  390         return delta;
  391 }
  392 
  393 static void
  394 chn_wrfeed(struct pcm_channel *c)
  395 {
  396         struct snd_dbuf *b = c->bufhard;
  397         struct snd_dbuf *bs = c->bufsoft;
  398         unsigned int amt, want, wasfree;
  399 
  400         CHN_LOCKASSERT(c);
  401 
  402         if ((c->flags & CHN_F_MMAP) && !(c->flags & CHN_F_CLOSING))
  403                 sndbuf_acquire(bs, NULL, sndbuf_getfree(bs));
  404 
  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);
  410         if (amt > 0)
  411                 sndbuf_feed(bs, b, c, c->feeder, amt);
  412 
  413         /*
  414          * Possible xruns. There should be no empty space left in buffer.
  415          */
  416         if (sndbuf_getready(b) < want)
  417                 c->xruns++;
  418 
  419         if (sndbuf_getfree(b) < wasfree)
  420                 chn_wakeup(c);
  421 }
  422 
  423 #if 0
  424 static void
  425 chn_wrupdate(struct pcm_channel *c)
  426 {
  427 
  428         CHN_LOCKASSERT(c);
  429         KASSERT(c->direction == PCMDIR_PLAY, ("%s(): bad channel", __func__));
  430 
  431         if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
  432                 return;
  433         chn_dmaupdate(c);
  434         chn_wrfeed(c);
  435         /* tell the driver we've updated the primary buffer */
  436         chn_trigger(c, PCMTRIG_EMLDMAWR);
  437 }
  438 #endif
  439 
  440 static void
  441 chn_wrintr(struct pcm_channel *c)
  442 {
  443 
  444         CHN_LOCKASSERT(c);
  445         /* update pointers in primary buffer */
  446         chn_dmaupdate(c);
  447         /* ...and feed from secondary to primary */
  448         chn_wrfeed(c);
  449         /* tell the driver we've updated the primary buffer */
  450         chn_trigger(c, PCMTRIG_EMLDMAWR);
  451 }
  452 
  453 /*
  454  * user write routine - uiomove data into secondary buffer, trigger if necessary
  455  * if blocking, sleep, rinse and repeat.
  456  *
  457  * called externally, so must handle locking
  458  */
  459 
  460 int
  461 chn_write(struct pcm_channel *c, struct uio *buf)
  462 {
  463         struct snd_dbuf *bs = c->bufsoft;
  464         void *off;
  465         int ret, timeout, sz, t, p;
  466 
  467         CHN_LOCKASSERT(c);
  468 
  469         ret = 0;
  470         timeout = chn_timeout * hz;
  471 
  472         while (ret == 0 && buf->uio_resid > 0) {
  473                 sz = min(buf->uio_resid, sndbuf_getfree(bs));
  474                 if (sz > 0) {
  475                         /*
  476                          * The following assumes that the free space in
  477                          * the buffer can never be less around the
  478                          * unlock-uiomove-lock sequence.
  479                          */
  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);
  484                                 CHN_UNLOCK(c);
  485                                 ret = uiomove(off, t, buf);
  486                                 CHN_LOCK(c);
  487                                 sz -= t;
  488                                 sndbuf_acquire(bs, NULL, t);
  489                         }
  490                         ret = 0;
  491                         if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
  492                                 ret = chn_start(c, 0);
  493                                 if (ret != 0)
  494                                         c->flags |= CHN_F_DEAD;
  495                         }
  496                 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER)) {
  497                         /**
  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.
  502                          *
  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".
  506                          */
  507                         ret = EAGAIN;
  508                 } else {
  509                         ret = chn_sleep(c, timeout);
  510                         if (ret == EAGAIN) {
  511                                 ret = EINVAL;
  512                                 c->flags |= CHN_F_DEAD;
  513                                 device_printf(c->dev, "%s(): %s: "
  514                                     "play interrupt timeout, channel dead\n",
  515                                     __func__, c->name);
  516                         } else if (ret == ERESTART || ret == EINTR)
  517                                 c->flags |= CHN_F_ABORTING;
  518                 }
  519         }
  520 
  521         return (ret);
  522 }
  523 
  524 /*
  525  * Feed new data from the read buffer. Can be called in the bottom half.
  526  */
  527 static void
  528 chn_rdfeed(struct pcm_channel *c)
  529 {
  530         struct snd_dbuf *b = c->bufhard;
  531         struct snd_dbuf *bs = c->bufsoft;
  532         unsigned int amt;
  533 
  534         CHN_LOCKASSERT(c);
  535 
  536         if (c->flags & CHN_F_MMAP)
  537                 sndbuf_dispose(bs, NULL, sndbuf_getready(bs));
  538 
  539         amt = sndbuf_getfree(bs);
  540         if (amt > 0)
  541                 sndbuf_feed(b, bs, c, c->feeder, amt);
  542 
  543         amt = sndbuf_getready(b);
  544         if (amt > 0) {
  545                 c->xruns++;
  546                 sndbuf_dispose(b, NULL, amt);
  547         }
  548 
  549         if (sndbuf_getready(bs) > 0)
  550                 chn_wakeup(c);
  551 }
  552 
  553 #if 0
  554 static void
  555 chn_rdupdate(struct pcm_channel *c)
  556 {
  557 
  558         CHN_LOCKASSERT(c);
  559         KASSERT(c->direction == PCMDIR_REC, ("chn_rdupdate on bad channel"));
  560 
  561         if ((c->flags & (CHN_F_MMAP | CHN_F_VIRTUAL)) || CHN_STOPPED(c))
  562                 return;
  563         chn_trigger(c, PCMTRIG_EMLDMARD);
  564         chn_dmaupdate(c);
  565         chn_rdfeed(c);
  566 }
  567 #endif
  568 
  569 /* read interrupt routine. Must be called with interrupts blocked. */
  570 static void
  571 chn_rdintr(struct pcm_channel *c)
  572 {
  573 
  574         CHN_LOCKASSERT(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 */
  578         chn_dmaupdate(c);
  579         /* ...and feed from primary to secondary */
  580         chn_rdfeed(c);
  581 }
  582 
  583 /*
  584  * user read routine - trigger if necessary, uiomove data from secondary buffer
  585  * if blocking, sleep, rinse and repeat.
  586  *
  587  * called externally, so must handle locking
  588  */
  589 
  590 int
  591 chn_read(struct pcm_channel *c, struct uio *buf)
  592 {
  593         struct snd_dbuf *bs = c->bufsoft;
  594         void *off;
  595         int ret, timeout, sz, t, p;
  596 
  597         CHN_LOCKASSERT(c);
  598 
  599         if (CHN_STOPPED(c) && !(c->flags & CHN_F_NOTRIGGER)) {
  600                 ret = chn_start(c, 0);
  601                 if (ret != 0) {
  602                         c->flags |= CHN_F_DEAD;
  603                         return (ret);
  604                 }
  605         }
  606 
  607         ret = 0;
  608         timeout = chn_timeout * hz;
  609 
  610         while (ret == 0 && buf->uio_resid > 0) {
  611                 sz = min(buf->uio_resid, sndbuf_getready(bs));
  612                 if (sz > 0) {
  613                         /*
  614                          * The following assumes that the free space in
  615                          * the buffer can never be less around the
  616                          * unlock-uiomove-lock sequence.
  617                          */
  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);
  622                                 CHN_UNLOCK(c);
  623                                 ret = uiomove(off, t, buf);
  624                                 CHN_LOCK(c);
  625                                 sz -= t;
  626                                 sndbuf_dispose(bs, NULL, t);
  627                         }
  628                         ret = 0;
  629                 } else if (c->flags & (CHN_F_NBIO | CHN_F_NOTRIGGER))
  630                         ret = EAGAIN;
  631                 else {
  632                         ret = chn_sleep(c, timeout);
  633                         if (ret == EAGAIN) {
  634                                 ret = EINVAL;
  635                                 c->flags |= CHN_F_DEAD;
  636                                 device_printf(c->dev, "%s(): %s: "
  637                                     "record interrupt timeout, channel dead\n",
  638                                     __func__, c->name);
  639                         } else if (ret == ERESTART || ret == EINTR)
  640                                 c->flags |= CHN_F_ABORTING;
  641                 }
  642         }
  643 
  644         return (ret);
  645 }
  646 
  647 void
  648 chn_intr_locked(struct pcm_channel *c)
  649 {
  650 
  651         CHN_LOCKASSERT(c);
  652 
  653         c->interrupts++;
  654 
  655         if (c->direction == PCMDIR_PLAY)
  656                 chn_wrintr(c);
  657         else
  658                 chn_rdintr(c);
  659 }
  660 
  661 void
  662 chn_intr(struct pcm_channel *c)
  663 {
  664 
  665         if (CHN_LOCKOWNED(c)) {
  666                 chn_intr_locked(c);
  667                 return;
  668         }
  669 
  670         CHN_LOCK(c);
  671         chn_intr_locked(c);
  672         CHN_UNLOCK(c);
  673 }
  674 
  675 u_int32_t
  676 chn_start(struct pcm_channel *c, int force)
  677 {
  678         u_int32_t i, j;
  679         struct snd_dbuf *b = c->bufhard;
  680         struct snd_dbuf *bs = c->bufsoft;
  681         int err;
  682 
  683         CHN_LOCKASSERT(c);
  684         /* if we're running, or if we're prevented from triggering, bail */
  685         if (CHN_STARTED(c) || ((c->flags & CHN_F_NOTRIGGER) && !force))
  686                 return (EINVAL);
  687 
  688         err = 0;
  689 
  690         if (force) {
  691                 i = 1;
  692                 j = 0;
  693         } else {
  694                 if (c->direction == PCMDIR_REC) {
  695                         i = sndbuf_getfree(bs);
  696                         j = (i > 0) ? 1 : sndbuf_getready(b);
  697                 } else {
  698                         if (sndbuf_getfree(bs) == 0) {
  699                                 i = 1;
  700                                 j = 0;
  701                         } else {
  702                                 struct snd_dbuf *pb;
  703 
  704                                 pb = CHN_BUF_PARENT(c, b);
  705                                 i = sndbuf_xbytes(sndbuf_getready(bs), bs, pb);
  706                                 j = sndbuf_getalign(pb);
  707                         }
  708                 }
  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" :
  713                             "hardware", i, j);
  714         }
  715 
  716         if (i >= j) {
  717                 c->flags |= CHN_F_TRIGGERED;
  718                 sndbuf_setrun(b, 1);
  719                 if (c->flags & CHN_F_CLOSING)
  720                         c->feedcount = 2;
  721                 else {
  722                         c->feedcount = 0;
  723                         c->interrupts = 0;
  724                         c->xruns = 0;
  725                 }
  726                 if (c->parentchannel == NULL) {
  727                         if (c->direction == PCMDIR_PLAY)
  728                                 sndbuf_fillsilence_rl(b,
  729                                     sndbuf_xbytes(sndbuf_getsize(bs), bs, b));
  730                         if (snd_verbose > 3)
  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",
  735                                     __func__,
  736                                     (c->flags & CHN_F_HAS_VCHAN) ?
  737                                     "VCHAN PARENT" : "HW", CHN_DIRSTR(c),
  738                                     (c->flags & CHN_F_CLOSING) ? "closing" :
  739                                     "running",
  740                                     sndbuf_getready(b),
  741                                     force, i, j, c->timeout,
  742                                     (sndbuf_getsize(b) * 1000) /
  743                                     (sndbuf_getalign(b) * sndbuf_getspd(b)));
  744                 }
  745                 err = chn_trigger(c, PCMTRIG_START);
  746         }
  747 
  748         return (err);
  749 }
  750 
  751 void
  752 chn_resetbuf(struct pcm_channel *c)
  753 {
  754         struct snd_dbuf *b = c->bufhard;
  755         struct snd_dbuf *bs = c->bufsoft;
  756 
  757         c->blocks = 0;
  758         sndbuf_reset(b);
  759         sndbuf_reset(bs);
  760 }
  761 
  762 /*
  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...
  766  */
  767 int
  768 chn_sync(struct pcm_channel *c, int threshold)
  769 {
  770         struct snd_dbuf *b, *bs;
  771         int ret, count, hcount, minflush, resid, residp, syncdelay, blksz;
  772         u_int32_t cflag;
  773 
  774         CHN_LOCKASSERT(c);
  775 
  776         if (c->direction != PCMDIR_PLAY)
  777                 return (EINVAL);
  778 
  779         bs = c->bufsoft;
  780 
  781         if ((c->flags & (CHN_F_DEAD | CHN_F_ABORTING)) ||
  782             (threshold < 1 && sndbuf_getready(bs) < 1))
  783                 return (0);
  784 
  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);
  789                         if (ret != 0)
  790                                 return (ret);
  791                 } else
  792                         return (0);
  793         }
  794 
  795         b = CHN_BUF_PARENT(c, c->bufhard);
  796 
  797         minflush = threshold + sndbuf_xbytes(sndbuf_getready(b), b, bs);
  798 
  799         syncdelay = chn_syncdelay;
  800 
  801         if (syncdelay < 0 && (threshold > 0 || sndbuf_getready(bs) > 0))
  802                 minflush += sndbuf_xbytes(sndbuf_getsize(b), b, bs);
  803 
  804         /*
  805          * Append (0-1000) millisecond trailing buffer (if needed)
  806          * for slower / high latency hardwares (notably USB audio)
  807          * to avoid audible truncation.
  808          */
  809         if (syncdelay > 0)
  810                 minflush += (sndbuf_getalign(bs) * sndbuf_getspd(bs) *
  811                     ((syncdelay > 1000) ? 1000 : syncdelay)) / 1000;
  812 
  813         minflush -= minflush % sndbuf_getalign(bs);
  814 
  815         if (minflush > 0) {
  816                 threshold = min(minflush, sndbuf_getfree(bs));
  817                 sndbuf_clear(bs, threshold);
  818                 sndbuf_acquire(bs, NULL, threshold);
  819                 minflush -= threshold;
  820         }
  821 
  822         resid = sndbuf_getready(bs);
  823         residp = resid;
  824         blksz = sndbuf_getblksz(b);
  825         if (blksz < 1) {
  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);
  832                 if (blksz < 1)
  833                         blksz = 1;
  834         }
  835         count = sndbuf_xbytes(minflush + resid, bs, b) / blksz;
  836         hcount = count;
  837         ret = 0;
  838 
  839         if (snd_verbose > 3)
  840                 device_printf(c->dev, "%s(): [begin] timeout=%d count=%d "
  841                     "minflush=%d resid=%d\n", __func__, c->timeout, count,
  842                     minflush, resid);
  843 
  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;
  850                         break;
  851                 } else if (ret == 0 || ret == EAGAIN) {
  852                         resid = sndbuf_getready(bs);
  853                         if (resid == residp) {
  854                                 --count;
  855                                 if (snd_verbose > 3)
  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) {
  863                                 ++count;
  864                                 if (snd_verbose > 3)
  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);
  871                         }
  872                         if (minflush > 0 && sndbuf_getfree(bs) > 0) {
  873                                 threshold = min(minflush,
  874                                     sndbuf_getfree(bs));
  875                                 sndbuf_clear(bs, threshold);
  876                                 sndbuf_acquire(bs, NULL, threshold);
  877                                 resid = sndbuf_getready(bs);
  878                                 minflush -= threshold;
  879                         }
  880                         residp = resid;
  881                 } else
  882                         break;
  883         }
  884         c->flags &= ~CHN_F_CLOSING;
  885         c->flags |= cflag;
  886 
  887         if (snd_verbose > 3)
  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,
  892                     minflush, ret);
  893 
  894         return (0);
  895 }
  896 
  897 /* called externally, handle locking */
  898 int
  899 chn_poll(struct pcm_channel *c, int ev, struct thread *td)
  900 {
  901         struct snd_dbuf *bs = c->bufsoft;
  902         int ret;
  903 
  904         CHN_LOCKASSERT(c);
  905 
  906         if (!(c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED))) {
  907                 ret = chn_start(c, 1);
  908                 if (ret != 0)
  909                         return (0);
  910         }
  911 
  912         ret = 0;
  913         if (chn_polltrigger(c)) {
  914                 chn_pollreset(c);
  915                 ret = ev;
  916         } else
  917                 selrecord(td, sndbuf_getsel(bs));
  918 
  919         return (ret);
  920 }
  921 
  922 /*
  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.
  925  *
  926  * called from: dsp_close, dsp_ioctl, with channel locked
  927  */
  928 int
  929 chn_abort(struct pcm_channel *c)
  930 {
  931         int missing = 0;
  932         struct snd_dbuf *b = c->bufhard;
  933         struct snd_dbuf *bs = c->bufsoft;
  934 
  935         CHN_LOCKASSERT(c);
  936         if (CHN_STOPPED(c))
  937                 return 0;
  938         c->flags |= CHN_F_ABORTING;
  939 
  940         c->flags &= ~CHN_F_TRIGGERED;
  941         /* kill the channel */
  942         chn_trigger(c, PCMTRIG_ABORT);
  943         sndbuf_setrun(b, 0);
  944         if (!(c->flags & CHN_F_VIRTUAL))
  945                 chn_dmaupdate(c);
  946         missing = sndbuf_getready(bs);
  947 
  948         c->flags &= ~CHN_F_ABORTING;
  949         return missing;
  950 }
  951 
  952 /*
  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.
  959  *
  960  * called from: dsp_close, not valid for record channels.
  961  */
  962 
  963 int
  964 chn_flush(struct pcm_channel *c)
  965 {
  966         struct snd_dbuf *b = c->bufhard;
  967 
  968         CHN_LOCKASSERT(c);
  969         KASSERT(c->direction == PCMDIR_PLAY, ("chn_flush on bad channel"));
  970         DEB(printf("chn_flush: c->flags 0x%08x\n", c->flags));
  971 
  972         c->flags |= CHN_F_CLOSING;
  973         chn_sync(c, 0);
  974         c->flags &= ~CHN_F_TRIGGERED;
  975         /* kill the channel */
  976         chn_trigger(c, PCMTRIG_ABORT);
  977         sndbuf_setrun(b, 0);
  978 
  979         c->flags &= ~CHN_F_CLOSING;
  980         return 0;
  981 }
  982 
  983 int
  984 snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist)
  985 {
  986         int i;
  987 
  988         for (i = 0; fmtlist[i] != 0; i++) {
  989                 if (fmt == fmtlist[i] ||
  990                     ((fmt & AFMT_PASSTHROUGH) &&
  991                     (AFMT_ENCODING(fmt) & fmtlist[i])))
  992                         return (1);
  993         }
  994 
  995         return (0);
  996 }
  997 
  998 static const struct {
  999         char *name, *alias1, *alias2;
 1000         uint32_t afmt;
 1001 } afmt_tab[] = {
 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 },
 1009 #else
 1010         { "s16le",  NULL, NULL, AFMT_S16_LE },
 1011         { "s16be", "s16", "16", AFMT_S16_BE },
 1012 #endif
 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 },
 1022 #else
 1023         { "s32le",  NULL, NULL, AFMT_S32_LE },
 1024         { "s32be", "s32", "32", AFMT_S32_BE },
 1025 #endif
 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           }
 1030 };
 1031 
 1032 static const struct {
 1033         char *name, *alias1, *alias2;
 1034         int matrix_id;
 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         { "4.0",  "4",   "quad", SND_CHN_MATRIX_4_0     },
 1041         { "4.1", NULL,     NULL, SND_CHN_MATRIX_4_1     },
 1042         { "5.0",  "5",     NULL, SND_CHN_MATRIX_5_0     },
 1043         { "5.1",  "6",     NULL, SND_CHN_MATRIX_5_1     },
 1044         { "6.0", NULL,     NULL, SND_CHN_MATRIX_6_0     },
 1045         { "6.1",  "7",     NULL, SND_CHN_MATRIX_6_1     },
 1046         { "7.1",  "8",     NULL, SND_CHN_MATRIX_7_1     },
 1047         {  NULL, NULL,     NULL, SND_CHN_MATRIX_UNKNOWN }
 1048 };
 1049 
 1050 uint32_t
 1051 snd_str2afmt(const char *req)
 1052 {
 1053         uint32_t i, afmt;
 1054         int matrix_id;
 1055         char b1[8], b2[8];
 1056 
 1057         i = sscanf(req, "%5[^:]:%6s", b1, b2);
 1058 
 1059         if (i == 1) {
 1060                 if (strlen(req) != strlen(b1))
 1061                         return (0);
 1062                 strlcpy(b2, "2.0", sizeof(b2));
 1063         } else if (i == 2) {
 1064                 if (strlen(req) != (strlen(b1) + 1 + strlen(b2)))
 1065                         return (0);
 1066         } else
 1067                 return (0);
 1068 
 1069         afmt = 0;
 1070         matrix_id = SND_CHN_MATRIX_UNKNOWN;
 1071 
 1072         for (i = 0; afmt == 0 && afmt_tab[i].name != NULL; i++) {
 1073                 if (strcasecmp(afmt_tab[i].name, b1) == 0 ||
 1074                     (afmt_tab[i].alias1 != NULL &&
 1075                     strcasecmp(afmt_tab[i].alias1, b1) == 0) ||
 1076                     (afmt_tab[i].alias2 != NULL &&
 1077                     strcasecmp(afmt_tab[i].alias2, b1) == 0)) {
 1078                         afmt = afmt_tab[i].afmt;
 1079                         strlcpy(b1, afmt_tab[i].name, sizeof(b1));
 1080                 }
 1081         }
 1082 
 1083         if (afmt == 0)
 1084                 return (0);
 1085 
 1086         for (i = 0; matrix_id == SND_CHN_MATRIX_UNKNOWN &&
 1087             matrix_id_tab[i].name != NULL; i++) {
 1088                 if (strcmp(matrix_id_tab[i].name, b2) == 0 ||
 1089                     (matrix_id_tab[i].alias1 != NULL &&
 1090                     strcmp(matrix_id_tab[i].alias1, b2) == 0) ||
 1091                     (matrix_id_tab[i].alias2 != NULL &&
 1092                     strcasecmp(matrix_id_tab[i].alias2, b2) == 0)) {
 1093                         matrix_id = matrix_id_tab[i].matrix_id;
 1094                         strlcpy(b2, matrix_id_tab[i].name, sizeof(b2));
 1095                 }
 1096         }
 1097 
 1098         if (matrix_id == SND_CHN_MATRIX_UNKNOWN)
 1099                 return (0);
 1100 
 1101 #ifndef _KERNEL
 1102         printf("Parse OK: '%s' -> '%s:%s' %d\n", req, b1, b2,
 1103             (int)(b2[0]) - '' + (int)(b2[2]) - '');
 1104 #endif
 1105 
 1106         return (SND_FORMAT(afmt, b2[0] - '' + b2[2] - '', b2[2] - ''));
 1107 }
 1108 
 1109 uint32_t
 1110 snd_afmt2str(uint32_t afmt, char *buf, size_t len)
 1111 {
 1112         uint32_t i, enc, ch, ext;
 1113         char tmp[AFMTSTR_LEN];
 1114 
 1115         if (buf == NULL || len < AFMTSTR_LEN)
 1116                 return (0);
 1117 
 1118         
 1119         bzero(tmp, sizeof(tmp));
 1120 
 1121         enc = AFMT_ENCODING(afmt);
 1122         ch = AFMT_CHANNEL(afmt);
 1123         ext = AFMT_EXTCHANNEL(afmt);
 1124 
 1125         for (i = 0; afmt_tab[i].name != NULL; i++) {
 1126                 if (enc == afmt_tab[i].afmt) {
 1127                         strlcpy(tmp, afmt_tab[i].name, sizeof(tmp));
 1128                         strlcat(tmp, ":", sizeof(tmp));
 1129                         break;
 1130                 }
 1131         }
 1132 
 1133         if (strlen(tmp) == 0)
 1134                 return (0);
 1135         
 1136         for (i = 0; matrix_id_tab[i].name != NULL; i++) {
 1137                 if (ch == (matrix_id_tab[i].name[0] - '' +
 1138                     matrix_id_tab[i].name[2] - '') &&
 1139                     ext == (matrix_id_tab[i].name[2] - '')) {
 1140                         strlcat(tmp, matrix_id_tab[i].name, sizeof(tmp));
 1141                         break;
 1142                 }
 1143         }
 1144 
 1145         if (strlen(tmp) == 0)
 1146                 return (0);
 1147 
 1148         strlcpy(buf, tmp, len);
 1149 
 1150         return (snd_str2afmt(buf));
 1151 }
 1152 
 1153 int
 1154 chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
 1155 {
 1156         int r;
 1157 
 1158         CHN_LOCKASSERT(c);
 1159         c->feedcount = 0;
 1160         c->flags &= CHN_F_RESET;
 1161         c->interrupts = 0;
 1162         c->timeout = 1;
 1163         c->xruns = 0;
 1164 
 1165         c->flags |= (pcm_getflags(c->dev) & SD_F_BITPERFECT) ?
 1166             CHN_F_BITPERFECT : 0;
 1167 
 1168         r = CHANNEL_RESET(c->methods, c->devinfo);
 1169         if (r == 0 && fmt != 0 && spd != 0) {
 1170                 r = chn_setparam(c, fmt, spd);
 1171                 fmt = 0;
 1172                 spd = 0;
 1173         }
 1174         if (r == 0 && fmt != 0)
 1175                 r = chn_setformat(c, fmt);
 1176         if (r == 0 && spd != 0)
 1177                 r = chn_setspeed(c, spd);
 1178         if (r == 0)
 1179                 r = chn_setlatency(c, chn_latency);
 1180         if (r == 0) {
 1181                 chn_resetbuf(c);
 1182                 r = CHANNEL_RESETDONE(c->methods, c->devinfo);
 1183         }
 1184         return r;
 1185 }
 1186 
 1187 int
 1188 chn_init(struct pcm_channel *c, void *devinfo, int dir, int direction)
 1189 {
 1190         struct feeder_class *fc;
 1191         struct snd_dbuf *b, *bs;
 1192         int i, ret;
 1193 
 1194         if (chn_timeout < CHN_TIMEOUT_MIN || chn_timeout > CHN_TIMEOUT_MAX)
 1195                 chn_timeout = CHN_TIMEOUT;
 1196 
 1197         chn_lockinit(c, dir);
 1198 
 1199         b = NULL;
 1200         bs = NULL;
 1201         CHN_INIT(c, children);
 1202         CHN_INIT(c, children.busy);
 1203         c->devinfo = NULL;
 1204         c->feeder = NULL;
 1205         c->latency = -1;
 1206         c->timeout = 1;
 1207 
 1208         ret = ENOMEM;
 1209         b = sndbuf_create(c->dev, c->name, "primary", c);
 1210         if (b == NULL)
 1211                 goto out;
 1212         bs = sndbuf_create(c->dev, c->name, "secondary", c);
 1213         if (bs == NULL)
 1214                 goto out;
 1215 
 1216         CHN_LOCK(c);
 1217 
 1218         ret = EINVAL;
 1219         fc = feeder_getclass(NULL);
 1220         if (fc == NULL)
 1221                 goto out;
 1222         if (chn_addfeeder(c, fc, NULL))
 1223                 goto out;
 1224 
 1225         /*
 1226          * XXX - sndbuf_setup() & sndbuf_resize() expect to be called
 1227          *       with the channel unlocked because they are also called
 1228          *       from driver methods that don't know about locking
 1229          */
 1230         CHN_UNLOCK(c);
 1231         sndbuf_setup(bs, NULL, 0);
 1232         CHN_LOCK(c);
 1233         c->bufhard = b;
 1234         c->bufsoft = bs;
 1235         c->flags = 0;
 1236         c->feederflags = 0;
 1237         c->sm = NULL;
 1238         c->format = SND_FORMAT(AFMT_U8, 1, 0);
 1239         c->speed = DSP_DEFAULT_SPEED;
 1240 
 1241         c->matrix = *feeder_matrix_id_map(SND_CHN_MATRIX_1_0);
 1242         c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
 1243 
 1244         for (i = 0; i < SND_CHN_T_MAX; i++) {
 1245                 c->volume[SND_VOL_C_MASTER][i] = SND_VOL_0DB_MASTER;
 1246         }
 1247 
 1248         c->volume[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB] = SND_VOL_0DB_MASTER;
 1249         c->volume[SND_VOL_C_PCM][SND_CHN_T_VOL_0DB] = chn_vol_0db_pcm;
 1250 
 1251         chn_vpc_reset(c, SND_VOL_C_PCM, 1);
 1252 
 1253         ret = ENODEV;
 1254         CHN_UNLOCK(c); /* XXX - Unlock for CHANNEL_INIT() malloc() call */
 1255         c->devinfo = CHANNEL_INIT(c->methods, devinfo, b, c, direction);
 1256         CHN_LOCK(c);
 1257         if (c->devinfo == NULL)
 1258                 goto out;
 1259 
 1260         ret = ENOMEM;
 1261         if ((sndbuf_getsize(b) == 0) && ((c->flags & CHN_F_VIRTUAL) == 0))
 1262                 goto out;
 1263 
 1264         ret = 0;
 1265         c->direction = direction;
 1266 
 1267         sndbuf_setfmt(b, c->format);
 1268         sndbuf_setspd(b, c->speed);
 1269         sndbuf_setfmt(bs, c->format);
 1270         sndbuf_setspd(bs, c->speed);
 1271 
 1272         /**
 1273          * @todo Should this be moved somewhere else?  The primary buffer
 1274          *       is allocated by the driver or via DMA map setup, and tmpbuf
 1275          *       seems to only come into existence in sndbuf_resize().
 1276          */
 1277         if (c->direction == PCMDIR_PLAY) {
 1278                 bs->sl = sndbuf_getmaxsize(bs);
 1279                 bs->shadbuf = malloc(bs->sl, M_DEVBUF, M_NOWAIT);
 1280                 if (bs->shadbuf == NULL) {
 1281                         ret = ENOMEM;
 1282                         goto out;
 1283                 }
 1284         }
 1285 
 1286 out:
 1287         CHN_UNLOCK(c);
 1288         if (ret) {
 1289                 if (c->devinfo) {
 1290                         if (CHANNEL_FREE(c->methods, c->devinfo))
 1291                                 sndbuf_free(b);
 1292                 }
 1293                 if (bs)
 1294                         sndbuf_destroy(bs);
 1295                 if (b)
 1296                         sndbuf_destroy(b);
 1297                 CHN_LOCK(c);
 1298                 c->flags |= CHN_F_DEAD;
 1299                 chn_lockdestroy(c);
 1300 
 1301                 return ret;
 1302         }
 1303 
 1304         return 0;
 1305 }
 1306 
 1307 int
 1308 chn_kill(struct pcm_channel *c)
 1309 {
 1310         struct snd_dbuf *b = c->bufhard;
 1311         struct snd_dbuf *bs = c->bufsoft;
 1312 
 1313         if (CHN_STARTED(c)) {
 1314                 CHN_LOCK(c);
 1315                 chn_trigger(c, PCMTRIG_ABORT);
 1316                 CHN_UNLOCK(c);
 1317         }
 1318         while (chn_removefeeder(c) == 0)
 1319                 ;
 1320         if (CHANNEL_FREE(c->methods, c->devinfo))
 1321                 sndbuf_free(b);
 1322         sndbuf_destroy(bs);
 1323         sndbuf_destroy(b);
 1324         CHN_LOCK(c);
 1325         c->flags |= CHN_F_DEAD;
 1326         chn_lockdestroy(c);
 1327 
 1328         return (0);
 1329 }
 1330 
 1331 /* XXX Obsolete. Use *_matrix() variant instead. */
 1332 int
 1333 chn_setvolume(struct pcm_channel *c, int left, int right)
 1334 {
 1335         int ret;
 1336 
 1337         ret = chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FL, left);
 1338         ret |= chn_setvolume_matrix(c, SND_VOL_C_MASTER, SND_CHN_T_FR,
 1339             right) << 8;
 1340 
 1341         return (ret);
 1342 }
 1343 
 1344 int
 1345 chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right,
 1346     int center)
 1347 {
 1348         int i, ret;
 1349 
 1350         ret = 0;
 1351 
 1352         for (i = 0; i < SND_CHN_T_MAX; i++) {
 1353                 if ((1 << i) & SND_CHN_LEFT_MASK)
 1354                         ret |= chn_setvolume_matrix(c, vc, i, left);
 1355                 else if ((1 << i) & SND_CHN_RIGHT_MASK)
 1356                         ret |= chn_setvolume_matrix(c, vc, i, right) << 8;
 1357                 else
 1358                         ret |= chn_setvolume_matrix(c, vc, i, center) << 16;
 1359         }
 1360 
 1361         return (ret);
 1362 }
 1363 
 1364 int
 1365 chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val)
 1366 {
 1367         int i;
 1368 
 1369         KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
 1370             (vc == SND_VOL_C_MASTER || (vc & 1)) &&
 1371             (vt == SND_CHN_T_VOL_0DB || (vt >= SND_CHN_T_BEGIN &&
 1372             vt <= SND_CHN_T_END)) && (vt != SND_CHN_T_VOL_0DB ||
 1373             (val >= SND_VOL_0DB_MIN && val <= SND_VOL_0DB_MAX)),
 1374             ("%s(): invalid volume matrix c=%p vc=%d vt=%d val=%d",
 1375             __func__, c, vc, vt, val));
 1376         CHN_LOCKASSERT(c);
 1377 
 1378         if (val < 0)
 1379                 val = 0;
 1380         if (val > 100)
 1381                 val = 100;
 1382 
 1383         c->volume[vc][vt] = val;
 1384 
 1385         /*
 1386          * Do relative calculation here and store it into class + 1
 1387          * to ease the job of feeder_volume.
 1388          */
 1389         if (vc == SND_VOL_C_MASTER) {
 1390                 for (vc = SND_VOL_C_BEGIN; vc <= SND_VOL_C_END;
 1391                     vc += SND_VOL_C_STEP)
 1392                         c->volume[SND_VOL_C_VAL(vc)][vt] =
 1393                             SND_VOL_CALC_VAL(c->volume, vc, vt);
 1394         } else if (vc & 1) {
 1395                 if (vt == SND_CHN_T_VOL_0DB)
 1396                         for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
 1397                             i += SND_CHN_T_STEP) {
 1398                                 c->volume[SND_VOL_C_VAL(vc)][i] =
 1399                                     SND_VOL_CALC_VAL(c->volume, vc, i);
 1400                         }
 1401                 else
 1402                         c->volume[SND_VOL_C_VAL(vc)][vt] =
 1403                             SND_VOL_CALC_VAL(c->volume, vc, vt);
 1404         }
 1405 
 1406         return (val);
 1407 }
 1408 
 1409 int
 1410 chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt)
 1411 {
 1412         KASSERT(c != NULL && vc >= SND_VOL_C_MASTER && vc < SND_VOL_C_MAX &&
 1413             (vt == SND_CHN_T_VOL_0DB ||
 1414             (vt >= SND_CHN_T_BEGIN && vt <= SND_CHN_T_END)),
 1415             ("%s(): invalid volume matrix c=%p vc=%d vt=%d",
 1416             __func__, c, vc, vt));
 1417         CHN_LOCKASSERT(c);
 1418 
 1419         return (c->volume[vc][vt]);
 1420 }
 1421 
 1422 struct pcmchan_matrix *
 1423 chn_getmatrix(struct pcm_channel *c)
 1424 {
 1425 
 1426         KASSERT(c != NULL, ("%s(): NULL channel", __func__));
 1427         CHN_LOCKASSERT(c);
 1428 
 1429         if (!(c->format & AFMT_CONVERTIBLE))
 1430                 return (NULL);
 1431 
 1432         return (&c->matrix);
 1433 }
 1434 
 1435 int
 1436 chn_setmatrix(struct pcm_channel *c, struct pcmchan_matrix *m)
 1437 {
 1438 
 1439         KASSERT(c != NULL && m != NULL,
 1440             ("%s(): NULL channel or matrix", __func__));
 1441         CHN_LOCKASSERT(c);
 1442 
 1443         if (!(c->format & AFMT_CONVERTIBLE))
 1444                 return (EINVAL);
 1445 
 1446         c->matrix = *m;
 1447         c->matrix.id = SND_CHN_MATRIX_PCMCHANNEL;
 1448 
 1449         return (chn_setformat(c, SND_FORMAT(c->format, m->channels, m->ext)));
 1450 }
 1451 
 1452 /*
 1453  * XXX chn_oss_* exists for the sake of compatibility.
 1454  */
 1455 int
 1456 chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
 1457 {
 1458 
 1459         KASSERT(c != NULL && map != NULL,
 1460             ("%s(): NULL channel or map", __func__));
 1461         CHN_LOCKASSERT(c);
 1462 
 1463         if (!(c->format & AFMT_CONVERTIBLE))
 1464                 return (EINVAL);
 1465 
 1466         return (feeder_matrix_oss_get_channel_order(&c->matrix, map));
 1467 }
 1468 
 1469 int
 1470 chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
 1471 {
 1472         struct pcmchan_matrix m;
 1473         int ret;
 1474 
 1475         KASSERT(c != NULL && map != NULL,
 1476             ("%s(): NULL channel or map", __func__));
 1477         CHN_LOCKASSERT(c);
 1478 
 1479         if (!(c->format & AFMT_CONVERTIBLE))
 1480                 return (EINVAL);
 1481 
 1482         m = c->matrix;
 1483         ret = feeder_matrix_oss_set_channel_order(&m, map);
 1484         if (ret != 0)
 1485                 return (ret);
 1486 
 1487         return (chn_setmatrix(c, &m));
 1488 }
 1489 
 1490 #define SND_CHN_OSS_FRONT       (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR)
 1491 #define SND_CHN_OSS_SURR        (SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR)
 1492 #define SND_CHN_OSS_CENTER_LFE  (SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF)
 1493 #define SND_CHN_OSS_REAR        (SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
 1494 
 1495 int
 1496 chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
 1497 {
 1498         struct pcmchan_matrix *m;
 1499         struct pcmchan_caps *caps;
 1500         uint32_t i, format;
 1501 
 1502         KASSERT(c != NULL && retmask != NULL,
 1503             ("%s(): NULL channel or retmask", __func__));
 1504         CHN_LOCKASSERT(c);
 1505 
 1506         caps = chn_getcaps(c);
 1507         if (caps == NULL || caps->fmtlist == NULL)
 1508                 return (ENODEV);
 1509 
 1510         for (i = 0; caps->fmtlist[i] != 0; i++) {
 1511                 format = caps->fmtlist[i];
 1512                 if (!(format & AFMT_CONVERTIBLE)) {
 1513                         *retmask |= DSP_BIND_SPDIF;
 1514                         continue;
 1515                 }
 1516                 m = CHANNEL_GETMATRIX(c->methods, c->devinfo, format);
 1517                 if (m == NULL)
 1518                         continue;
 1519                 if (m->mask & SND_CHN_OSS_FRONT)
 1520                         *retmask |= DSP_BIND_FRONT;
 1521                 if (m->mask & SND_CHN_OSS_SURR)
 1522                         *retmask |= DSP_BIND_SURR;
 1523                 if (m->mask & SND_CHN_OSS_CENTER_LFE)
 1524                         *retmask |= DSP_BIND_CENTER_LFE;
 1525                 if (m->mask & SND_CHN_OSS_REAR)
 1526                         *retmask |= DSP_BIND_REAR;
 1527         }
 1528 
 1529         /* report software-supported binding mask */
 1530         if (!CHN_BITPERFECT(c) && report_soft_matrix)
 1531                 *retmask |= DSP_BIND_FRONT | DSP_BIND_SURR |
 1532                     DSP_BIND_CENTER_LFE | DSP_BIND_REAR;
 1533 
 1534         return (0);
 1535 }
 1536 
 1537 void
 1538 chn_vpc_reset(struct pcm_channel *c, int vc, int force)
 1539 {
 1540         int i;
 1541 
 1542         KASSERT(c != NULL && vc >= SND_VOL_C_BEGIN && vc <= SND_VOL_C_END,
 1543             ("%s(): invalid reset c=%p vc=%d", __func__, c, vc));
 1544         CHN_LOCKASSERT(c);
 1545 
 1546         if (force == 0 && chn_vpc_autoreset == 0)
 1547                 return;
 1548 
 1549         for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END; i += SND_CHN_T_STEP)
 1550                 CHN_SETVOLUME(c, vc, i, c->volume[vc][SND_CHN_T_VOL_0DB]);
 1551 }
 1552 
 1553 static u_int32_t
 1554 round_pow2(u_int32_t v)
 1555 {
 1556         u_int32_t ret;
 1557 
 1558         if (v < 2)
 1559                 v = 2;
 1560         ret = 0;
 1561         while (v >> ret)
 1562                 ret++;
 1563         ret = 1 << (ret - 1);
 1564         while (ret < v)
 1565                 ret <<= 1;
 1566         return ret;
 1567 }
 1568 
 1569 static u_int32_t
 1570 round_blksz(u_int32_t v, int round)
 1571 {
 1572         u_int32_t ret, tmp;
 1573 
 1574         if (round < 1)
 1575                 round = 1;
 1576 
 1577         ret = min(round_pow2(v), CHN_2NDBUFMAXSIZE >> 1);
 1578 
 1579         if (ret > v && (ret >> 1) > 0 && (ret >> 1) >= ((v * 3) >> 2))
 1580                 ret >>= 1;
 1581 
 1582         tmp = ret - (ret % round);
 1583         while (tmp < 16 || tmp < round) {
 1584                 ret <<= 1;
 1585                 tmp = ret - (ret % round);
 1586         }
 1587 
 1588         return ret;
 1589 }
 1590 
 1591 /*
 1592  * 4Front call it DSP Policy, while we call it "Latency Profile". The idea
 1593  * is to keep 2nd buffer short so that it doesn't cause long queue during
 1594  * buffer transfer.
 1595  *
 1596  *    Latency reference table for 48khz stereo 16bit: (PLAY)
 1597  *
 1598  *      +---------+------------+-----------+------------+
 1599  *      | Latency | Blockcount | Blocksize | Buffersize |
 1600  *      +---------+------------+-----------+------------+
 1601  *      |     0   |       2    |   64      |    128     |
 1602  *      +---------+------------+-----------+------------+
 1603  *      |     1   |       4    |   128     |    512     |
 1604  *      +---------+------------+-----------+------------+
 1605  *      |     2   |       8    |   512     |    4096    |
 1606  *      +---------+------------+-----------+------------+
 1607  *      |     3   |      16    |   512     |    8192    |
 1608  *      +---------+------------+-----------+------------+
 1609  *      |     4   |      32    |   512     |    16384   |
 1610  *      +---------+------------+-----------+------------+
 1611  *      |     5   |      32    |   1024    |    32768   |
 1612  *      +---------+------------+-----------+------------+
 1613  *      |     6   |      16    |   2048    |    32768   |
 1614  *      +---------+------------+-----------+------------+
 1615  *      |     7   |       8    |   4096    |    32768   |
 1616  *      +---------+------------+-----------+------------+
 1617  *      |     8   |       4    |   8192    |    32768   |
 1618  *      +---------+------------+-----------+------------+
 1619  *      |     9   |       2    |   16384   |    32768   |
 1620  *      +---------+------------+-----------+------------+
 1621  *      |    10   |       2    |   32768   |    65536   |
 1622  *      +---------+------------+-----------+------------+
 1623  *
 1624  * Recording need a different reference table. All we care is
 1625  * gobbling up everything within reasonable buffering threshold.
 1626  *
 1627  *    Latency reference table for 48khz stereo 16bit: (REC)
 1628  *
 1629  *      +---------+------------+-----------+------------+
 1630  *      | Latency | Blockcount | Blocksize | Buffersize |
 1631  *      +---------+------------+-----------+------------+
 1632  *      |     0   |     512    |   32      |    16384   |
 1633  *      +---------+------------+-----------+------------+
 1634  *      |     1   |     256    |   64      |    16384   |
 1635  *      +---------+------------+-----------+------------+
 1636  *      |     2   |     128    |   128     |    16384   |
 1637  *      +---------+------------+-----------+------------+
 1638  *      |     3   |      64    |   256     |    16384   |
 1639  *      +---------+------------+-----------+------------+
 1640  *      |     4   |      32    |   512     |    16384   |
 1641  *      +---------+------------+-----------+------------+
 1642  *      |     5   |      32    |   1024    |    32768   |
 1643  *      +---------+------------+-----------+------------+
 1644  *      |     6   |      16    |   2048    |    32768   |
 1645  *      +---------+------------+-----------+------------+
 1646  *      |     7   |       8    |   4096    |    32768   |
 1647  *      +---------+------------+-----------+------------+
 1648  *      |     8   |       4    |   8192    |    32768   |
 1649  *      +---------+------------+-----------+------------+
 1650  *      |     9   |       2    |   16384   |    32768   |
 1651  *      +---------+------------+-----------+------------+
 1652  *      |    10   |       2    |   32768   |    65536   |
 1653  *      +---------+------------+-----------+------------+
 1654  *
 1655  * Calculations for other data rate are entirely based on these reference
 1656  * tables. For normal operation, Latency 5 seems give the best, well
 1657  * balanced performance for typical workload. Anything below 5 will
 1658  * eat up CPU to keep up with increasing context switches because of
 1659  * shorter buffer space and usually require the application to handle it
 1660  * aggresively through possibly real time programming technique.
 1661  *
 1662  */
 1663 #define CHN_LATENCY_PBLKCNT_REF                         \
 1664         {{1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1},             \
 1665         {1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1}}
 1666 #define CHN_LATENCY_PBUFSZ_REF                          \
 1667         {{7, 9, 12, 13, 14, 15, 15, 15, 15, 15, 16},    \
 1668         {11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 17}}
 1669 
 1670 #define CHN_LATENCY_RBLKCNT_REF                         \
 1671         {{9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1},             \
 1672         {9, 8, 7, 6, 5, 5, 4, 3, 2, 1, 1}}
 1673 #define CHN_LATENCY_RBUFSZ_REF                          \
 1674         {{14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16},  \
 1675         {15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 17}}
 1676 
 1677 #define CHN_LATENCY_DATA_REF    192000 /* 48khz stereo 16bit ~ 48000 x 2 x 2 */
 1678 
 1679 static int
 1680 chn_calclatency(int dir, int latency, int bps, u_int32_t datarate,
 1681                                 u_int32_t max, int *rblksz, int *rblkcnt)
 1682 {
 1683         static int pblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
 1684             CHN_LATENCY_PBLKCNT_REF;
 1685         static int  pbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
 1686             CHN_LATENCY_PBUFSZ_REF;
 1687         static int rblkcnts[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
 1688             CHN_LATENCY_RBLKCNT_REF;
 1689         static int  rbufszs[CHN_LATENCY_PROFILE_MAX + 1][CHN_LATENCY_MAX + 1] =
 1690             CHN_LATENCY_RBUFSZ_REF;
 1691         u_int32_t bufsz;
 1692         int lprofile, blksz, blkcnt;
 1693 
 1694         if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX ||
 1695             bps < 1 || datarate < 1 ||
 1696             !(dir == PCMDIR_PLAY || dir == PCMDIR_REC)) {
 1697                 if (rblksz != NULL)
 1698                         *rblksz = CHN_2NDBUFMAXSIZE >> 1;
 1699                 if (rblkcnt != NULL)
 1700                         *rblkcnt = 2;
 1701                 printf("%s(): FAILED dir=%d latency=%d bps=%d "
 1702                     "datarate=%u max=%u\n",
 1703                     __func__, dir, latency, bps, datarate, max);
 1704                 return CHN_2NDBUFMAXSIZE;
 1705         }
 1706 
 1707         lprofile = chn_latency_profile;
 1708 
 1709         if (dir == PCMDIR_PLAY) {
 1710                 blkcnt = pblkcnts[lprofile][latency];
 1711                 bufsz = pbufszs[lprofile][latency];
 1712         } else {
 1713                 blkcnt = rblkcnts[lprofile][latency];
 1714                 bufsz = rbufszs[lprofile][latency];
 1715         }
 1716 
 1717         bufsz = round_pow2(snd_xbytes(1 << bufsz, CHN_LATENCY_DATA_REF,
 1718             datarate));
 1719         if (bufsz > max)
 1720                 bufsz = max;
 1721         blksz = round_blksz(bufsz >> blkcnt, bps);
 1722 
 1723         if (rblksz != NULL)
 1724                 *rblksz = blksz;
 1725         if (rblkcnt != NULL)
 1726                 *rblkcnt = 1 << blkcnt;
 1727 
 1728         return blksz << blkcnt;
 1729 }
 1730 
 1731 static int
 1732 chn_resizebuf(struct pcm_channel *c, int latency,
 1733                                         int blkcnt, int blksz)
 1734 {
 1735         struct snd_dbuf *b, *bs, *pb;
 1736         int sblksz, sblkcnt, hblksz, hblkcnt, limit = 0, nsblksz, nsblkcnt;
 1737         int ret;
 1738 
 1739         CHN_LOCKASSERT(c);
 1740 
 1741         if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) ||
 1742             !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC))
 1743                 return EINVAL;
 1744 
 1745         if (latency == -1) {
 1746                 c->latency = -1;
 1747                 latency = chn_latency;
 1748         } else if (latency == -2) {
 1749                 latency = c->latency;
 1750                 if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
 1751                         latency = chn_latency;
 1752         } else if (latency < CHN_LATENCY_MIN || latency > CHN_LATENCY_MAX)
 1753                 return EINVAL;
 1754         else {
 1755                 c->latency = latency;
 1756         }
 1757 
 1758         bs = c->bufsoft;
 1759         b = c->bufhard;
 1760 
 1761         if (!(blksz == 0 || blkcnt == -1) &&
 1762             (blksz < 16 || blksz < sndbuf_getalign(bs) || blkcnt < 2 ||
 1763             (blksz * blkcnt) > CHN_2NDBUFMAXSIZE))
 1764                 return EINVAL;
 1765 
 1766         chn_calclatency(c->direction, latency, sndbuf_getalign(bs),
 1767             sndbuf_getalign(bs) * sndbuf_getspd(bs), CHN_2NDBUFMAXSIZE,
 1768             &sblksz, &sblkcnt);
 1769 
 1770         if (blksz == 0 || blkcnt == -1) {
 1771                 if (blkcnt == -1)
 1772                         c->flags &= ~CHN_F_HAS_SIZE;
 1773                 if (c->flags & CHN_F_HAS_SIZE) {
 1774                         blksz = sndbuf_getblksz(bs);
 1775                         blkcnt = sndbuf_getblkcnt(bs);
 1776                 }
 1777         } else
 1778                 c->flags |= CHN_F_HAS_SIZE;
 1779 
 1780         if (c->flags & CHN_F_HAS_SIZE) {
 1781                 /*
 1782                  * The application has requested their own blksz/blkcnt.
 1783                  * Just obey with it, and let them toast alone. We can
 1784                  * clamp it to the nearest latency profile, but that would
 1785                  * defeat the purpose of having custom control. The least
 1786                  * we can do is round it to the nearest ^2 and align it.
 1787                  */
 1788                 sblksz = round_blksz(blksz, sndbuf_getalign(bs));
 1789                 sblkcnt = round_pow2(blkcnt);
 1790         }
 1791 
 1792         if (c->parentchannel != NULL) {
 1793                 pb = c->parentchannel->bufsoft;
 1794                 CHN_UNLOCK(c);
 1795                 CHN_LOCK(c->parentchannel);
 1796                 chn_notify(c->parentchannel, CHN_N_BLOCKSIZE);
 1797                 CHN_UNLOCK(c->parentchannel);
 1798                 CHN_LOCK(c);
 1799                 if (c->direction == PCMDIR_PLAY) {
 1800                         limit = (pb != NULL) ?
 1801                             sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0;
 1802                 } else {
 1803                         limit = (pb != NULL) ?
 1804                             sndbuf_xbytes(sndbuf_getblksz(pb), pb, bs) * 2 : 0;
 1805                 }
 1806         } else {
 1807                 hblkcnt = 2;
 1808                 if (c->flags & CHN_F_HAS_SIZE) {
 1809                         hblksz = round_blksz(sndbuf_xbytes(sblksz, bs, b),
 1810                             sndbuf_getalign(b));
 1811                         hblkcnt = round_pow2(sndbuf_getblkcnt(bs));
 1812                 } else
 1813                         chn_calclatency(c->direction, latency,
 1814                             sndbuf_getalign(b),
 1815                             sndbuf_getalign(b) * sndbuf_getspd(b),
 1816                             CHN_2NDBUFMAXSIZE, &hblksz, &hblkcnt);
 1817 
 1818                 if ((hblksz << 1) > sndbuf_getmaxsize(b))
 1819                         hblksz = round_blksz(sndbuf_getmaxsize(b) >> 1,
 1820                             sndbuf_getalign(b));
 1821 
 1822                 while ((hblksz * hblkcnt) > sndbuf_getmaxsize(b)) {
 1823                         if (hblkcnt < 4)
 1824                                 hblksz >>= 1;
 1825                         else
 1826                                 hblkcnt >>= 1;
 1827                 }
 1828 
 1829                 hblksz -= hblksz % sndbuf_getalign(b);
 1830 
 1831 #if 0
 1832                 hblksz = sndbuf_getmaxsize(b) >> 1;
 1833                 hblksz -= hblksz % sndbuf_getalign(b);
 1834                 hblkcnt = 2;
 1835 #endif
 1836 
 1837                 CHN_UNLOCK(c);
 1838                 if (chn_usefrags == 0 ||
 1839                     CHANNEL_SETFRAGMENTS(c->methods, c->devinfo,
 1840                     hblksz, hblkcnt) != 0)
 1841                         sndbuf_setblksz(b, CHANNEL_SETBLOCKSIZE(c->methods,
 1842                             c->devinfo, hblksz));
 1843                 CHN_LOCK(c);
 1844 
 1845                 if (!CHN_EMPTY(c, children)) {
 1846                         nsblksz = round_blksz(
 1847                             sndbuf_xbytes(sndbuf_getblksz(b), b, bs),
 1848                             sndbuf_getalign(bs));
 1849                         nsblkcnt = sndbuf_getblkcnt(b);
 1850                         if (c->direction == PCMDIR_PLAY) {
 1851                                 do {
 1852                                         nsblkcnt--;
 1853                                 } while (nsblkcnt >= 2 &&
 1854                                     nsblksz * nsblkcnt >= sblksz * sblkcnt);
 1855                                 nsblkcnt++;
 1856                         }
 1857                         sblksz = nsblksz;
 1858                         sblkcnt = nsblkcnt;
 1859                         limit = 0;
 1860                 } else
 1861                         limit = sndbuf_xbytes(sndbuf_getblksz(b), b, bs) * 2;
 1862         }
 1863 
 1864         if (limit > CHN_2NDBUFMAXSIZE)
 1865                 limit = CHN_2NDBUFMAXSIZE;
 1866 
 1867 #if 0
 1868         while (limit > 0 && (sblksz * sblkcnt) > limit) {
 1869                 if (sblkcnt < 4)
 1870                         break;
 1871                 sblkcnt >>= 1;
 1872         }
 1873 #endif
 1874 
 1875         while ((sblksz * sblkcnt) < limit)
 1876                 sblkcnt <<= 1;
 1877 
 1878         while ((sblksz * sblkcnt) > CHN_2NDBUFMAXSIZE) {
 1879                 if (sblkcnt < 4)
 1880                         sblksz >>= 1;
 1881                 else
 1882                         sblkcnt >>= 1;
 1883         }
 1884 
 1885         sblksz -= sblksz % sndbuf_getalign(bs);
 1886 
 1887         if (sndbuf_getblkcnt(bs) != sblkcnt || sndbuf_getblksz(bs) != sblksz ||
 1888             sndbuf_getsize(bs) != (sblkcnt * sblksz)) {
 1889                 ret = sndbuf_remalloc(bs, sblkcnt, sblksz);
 1890                 if (ret != 0) {
 1891                         device_printf(c->dev, "%s(): Failed: %d %d\n",
 1892                             __func__, sblkcnt, sblksz);
 1893                         return ret;
 1894                 }
 1895         }
 1896 
 1897         /*
 1898          * Interrupt timeout
 1899          */
 1900         c->timeout = ((u_int64_t)hz * sndbuf_getsize(bs)) /
 1901             ((u_int64_t)sndbuf_getspd(bs) * sndbuf_getalign(bs));
 1902         if (c->parentchannel != NULL)
 1903                 c->timeout = min(c->timeout, c->parentchannel->timeout);
 1904         if (c->timeout < 1)
 1905                 c->timeout = 1;
 1906 
 1907         /*
 1908          * OSSv4 docs: "By default OSS will set the low water level equal
 1909          * to the fragment size which is optimal in most cases."
 1910          */
 1911         c->lw = sndbuf_getblksz(bs);
 1912         chn_resetbuf(c);
 1913 
 1914         if (snd_verbose > 3)
 1915                 device_printf(c->dev, "%s(): %s (%s) timeout=%u "
 1916                     "b[%d/%d/%d] bs[%d/%d/%d] limit=%d\n",
 1917                     __func__, CHN_DIRSTR(c),
 1918                     (c->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
 1919                     c->timeout,
 1920                     sndbuf_getsize(b), sndbuf_getblksz(b),
 1921                     sndbuf_getblkcnt(b),
 1922                     sndbuf_getsize(bs), sndbuf_getblksz(bs),
 1923                     sndbuf_getblkcnt(bs), limit);
 1924 
 1925         return 0;
 1926 }
 1927 
 1928 int
 1929 chn_setlatency(struct pcm_channel *c, int latency)
 1930 {
 1931         CHN_LOCKASSERT(c);
 1932         /* Destroy blksz/blkcnt, enforce latency profile. */
 1933         return chn_resizebuf(c, latency, -1, 0);
 1934 }
 1935 
 1936 int
 1937 chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
 1938 {
 1939         CHN_LOCKASSERT(c);
 1940         /* Destroy latency profile, enforce blksz/blkcnt */
 1941         return chn_resizebuf(c, -1, blkcnt, blksz);
 1942 }
 1943 
 1944 int
 1945 chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed)
 1946 {
 1947         struct pcmchan_caps *caps;
 1948         uint32_t hwspeed, delta;
 1949         int ret;
 1950 
 1951         CHN_LOCKASSERT(c);
 1952 
 1953         if (speed < 1 || format == 0 || CHN_STARTED(c))
 1954                 return (EINVAL);
 1955 
 1956         c->format = format;
 1957         c->speed = speed;
 1958 
 1959         caps = chn_getcaps(c);
 1960 
 1961         hwspeed = speed;
 1962         RANGE(hwspeed, caps->minspeed, caps->maxspeed);
 1963 
 1964         sndbuf_setspd(c->bufhard, CHANNEL_SETSPEED(c->methods, c->devinfo,
 1965             hwspeed));
 1966         hwspeed = sndbuf_getspd(c->bufhard);
 1967 
 1968         delta = (hwspeed > speed) ? (hwspeed - speed) : (speed - hwspeed);
 1969 
 1970         if (delta <= feeder_rate_round)
 1971                 c->speed = hwspeed;
 1972 
 1973         ret = feeder_chain(c);
 1974 
 1975         if (ret == 0)
 1976                 ret = CHANNEL_SETFORMAT(c->methods, c->devinfo,
 1977                     sndbuf_getfmt(c->bufhard));
 1978 
 1979         if (ret == 0)
 1980                 ret = chn_resizebuf(c, -2, 0, 0);
 1981 
 1982         return (ret);
 1983 }
 1984 
 1985 int
 1986 chn_setspeed(struct pcm_channel *c, uint32_t speed)
 1987 {
 1988         uint32_t oldformat, oldspeed, format;
 1989         int ret;
 1990 
 1991 #if 0
 1992         /* XXX force 48k */
 1993         if (c->format & AFMT_PASSTHROUGH)
 1994                 speed = AFMT_PASSTHROUGH_RATE;
 1995 #endif
 1996 
 1997         oldformat = c->format;
 1998         oldspeed = c->speed;
 1999         format = oldformat;
 2000 
 2001         ret = chn_setparam(c, format, speed);
 2002         if (ret != 0) {
 2003                 if (snd_verbose > 3)
 2004                         device_printf(c->dev,
 2005                             "%s(): Setting speed %d failed, "
 2006                             "falling back to %d\n",
 2007                             __func__, speed, oldspeed);
 2008                 chn_setparam(c, c->format, oldspeed);
 2009         }
 2010 
 2011         return (ret);
 2012 }
 2013 
 2014 int
 2015 chn_setformat(struct pcm_channel *c, uint32_t format)
 2016 {
 2017         uint32_t oldformat, oldspeed, speed;
 2018         int ret;
 2019 
 2020         /* XXX force stereo */
 2021         if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
 2022                 format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,
 2023                     AFMT_PASSTHROUGH_EXTCHANNEL);
 2024         }
 2025 
 2026         oldformat = c->format;
 2027         oldspeed = c->speed;
 2028         speed = oldspeed;
 2029 
 2030         ret = chn_setparam(c, format, speed);
 2031         if (ret != 0) {
 2032                 if (snd_verbose > 3)
 2033                         device_printf(c->dev,
 2034                             "%s(): Format change 0x%08x failed, "
 2035                             "falling back to 0x%08x\n",
 2036                             __func__, format, oldformat);
 2037                 chn_setparam(c, oldformat, oldspeed);
 2038         }
 2039 
 2040         return (ret);
 2041 }
 2042 
 2043 void
 2044 chn_syncstate(struct pcm_channel *c)
 2045 {
 2046         struct snddev_info *d;
 2047         struct snd_mixer *m;
 2048 
 2049         d = (c != NULL) ? c->parentsnddev : NULL;
 2050         m = (d != NULL && d->mixer_dev != NULL) ? d->mixer_dev->si_drv1 :
 2051             NULL;
 2052 
 2053         if (d == NULL || m == NULL)
 2054                 return;
 2055 
 2056         CHN_LOCKASSERT(c);
 2057 
 2058         if (c->feederflags & (1 << FEEDER_VOLUME)) {
 2059                 uint32_t parent;
 2060                 int vol, pvol, left, right, center;
 2061 
 2062                 if (c->direction == PCMDIR_PLAY &&
 2063                     (d->flags & SD_F_SOFTPCMVOL)) {
 2064                         /* CHN_UNLOCK(c); */
 2065                         vol = mix_get(m, SOUND_MIXER_PCM);
 2066                         parent = mix_getparent(m, SOUND_MIXER_PCM);
 2067                         if (parent != SOUND_MIXER_NONE)
 2068                                 pvol = mix_get(m, parent);
 2069                         else
 2070                                 pvol = 100 | (100 << 8);
 2071                         /* CHN_LOCK(c); */
 2072                 } else {
 2073                         vol = 100 | (100 << 8);
 2074                         pvol = vol;
 2075                 }
 2076 
 2077                 if (vol == -1) {
 2078                         device_printf(c->dev,
 2079                             "Soft PCM Volume: Failed to read pcm "
 2080                             "default value\n");
 2081                         vol = 100 | (100 << 8);
 2082                 }
 2083 
 2084                 if (pvol == -1) {
 2085                         device_printf(c->dev,
 2086                             "Soft PCM Volume: Failed to read parent "
 2087                             "default value\n");
 2088                         pvol = 100 | (100 << 8);
 2089                 }
 2090 
 2091                 left = ((vol & 0x7f) * (pvol & 0x7f)) / 100;
 2092                 right = (((vol >> 8) & 0x7f) * ((pvol >> 8) & 0x7f)) / 100;
 2093                 center = (left + right) >> 1;
 2094 
 2095                 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right, center);
 2096         }
 2097 
 2098         if (c->feederflags & (1 << FEEDER_EQ)) {
 2099                 struct pcm_feeder *f;
 2100                 int treble, bass, state;
 2101 
 2102                 /* CHN_UNLOCK(c); */
 2103                 treble = mix_get(m, SOUND_MIXER_TREBLE);
 2104                 bass = mix_get(m, SOUND_MIXER_BASS);
 2105                 /* CHN_LOCK(c); */
 2106 
 2107                 if (treble == -1)
 2108                         treble = 50;
 2109                 else
 2110                         treble = ((treble & 0x7f) +
 2111                             ((treble >> 8) & 0x7f)) >> 1;
 2112 
 2113                 if (bass == -1)
 2114                         bass = 50;
 2115                 else
 2116                         bass = ((bass & 0x7f) + ((bass >> 8) & 0x7f)) >> 1;
 2117 
 2118                 f = chn_findfeeder(c, FEEDER_EQ);
 2119                 if (f != NULL) {
 2120                         if (FEEDER_SET(f, FEEDEQ_TREBLE, treble) != 0)
 2121                                 device_printf(c->dev,
 2122                                     "EQ: Failed to set treble -- %d\n",
 2123                                     treble);
 2124                         if (FEEDER_SET(f, FEEDEQ_BASS, bass) != 0)
 2125                                 device_printf(c->dev,
 2126                                     "EQ: Failed to set bass -- %d\n",
 2127                                     bass);
 2128                         if (FEEDER_SET(f, FEEDEQ_PREAMP, d->eqpreamp) != 0)
 2129                                 device_printf(c->dev,
 2130                                     "EQ: Failed to set preamp -- %d\n",
 2131                                     d->eqpreamp);
 2132                         if (d->flags & SD_F_EQ_BYPASSED)
 2133                                 state = FEEDEQ_BYPASS;
 2134                         else if (d->flags & SD_F_EQ_ENABLED)
 2135                                 state = FEEDEQ_ENABLE;
 2136                         else
 2137                                 state = FEEDEQ_DISABLE;
 2138                         if (FEEDER_SET(f, FEEDEQ_STATE, state) != 0)
 2139                                 device_printf(c->dev,
 2140                                     "EQ: Failed to set state -- %d\n", state);
 2141                 }
 2142         }
 2143 }
 2144 
 2145 int
 2146 chn_trigger(struct pcm_channel *c, int go)
 2147 {
 2148 #ifdef DEV_ISA
 2149         struct snd_dbuf *b = c->bufhard;
 2150 #endif
 2151         struct snddev_info *d = c->parentsnddev;
 2152         int ret;
 2153 
 2154         CHN_LOCKASSERT(c);
 2155 #ifdef DEV_ISA
 2156         if (SND_DMA(b) && (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD))
 2157                 sndbuf_dmabounce(b);
 2158 #endif
 2159         if (!PCMTRIG_COMMON(go))
 2160                 return (CHANNEL_TRIGGER(c->methods, c->devinfo, go));
 2161 
 2162         if (go == c->trigger)
 2163                 return (0);
 2164 
 2165         ret = CHANNEL_TRIGGER(c->methods, c->devinfo, go);
 2166         if (ret != 0)
 2167                 return (ret);
 2168 
 2169         switch (go) {
 2170         case PCMTRIG_START:
 2171                 if (snd_verbose > 3)
 2172                         device_printf(c->dev,
 2173                             "%s() %s: calling go=0x%08x , "
 2174                             "prev=0x%08x\n", __func__, c->name, go,
 2175                             c->trigger);
 2176                 if (c->trigger != PCMTRIG_START) {
 2177                         c->trigger = go;
 2178                         CHN_UNLOCK(c);
 2179                         PCM_LOCK(d);
 2180                         CHN_INSERT_HEAD(d, c, channels.pcm.busy);
 2181                         PCM_UNLOCK(d);
 2182                         CHN_LOCK(c);
 2183                         chn_syncstate(c);
 2184                 }
 2185                 break;
 2186         case PCMTRIG_STOP:
 2187         case PCMTRIG_ABORT:
 2188                 if (snd_verbose > 3)
 2189                         device_printf(c->dev,
 2190                             "%s() %s: calling go=0x%08x , "
 2191                             "prev=0x%08x\n", __func__, c->name, go,
 2192                             c->trigger);
 2193                 if (c->trigger == PCMTRIG_START) {
 2194                         c->trigger = go;
 2195                         CHN_UNLOCK(c);
 2196                         PCM_LOCK(d);
 2197                         CHN_REMOVE(d, c, channels.pcm.busy);
 2198                         PCM_UNLOCK(d);
 2199                         CHN_LOCK(c);
 2200                 }
 2201                 break;
 2202         default:
 2203                 break;
 2204         }
 2205 
 2206         return (0);
 2207 }
 2208 
 2209 /**
 2210  * @brief Queries sound driver for sample-aligned hardware buffer pointer index
 2211  *
 2212  * This function obtains the hardware pointer location, then aligns it to
 2213  * the current bytes-per-sample value before returning.  (E.g., a channel
 2214  * running in 16 bit stereo mode would require 4 bytes per sample, so a
 2215  * hwptr value ranging from 32-35 would be returned as 32.)
 2216  *
 2217  * @param c     PCM channel context     
 2218  * @returns     sample-aligned hardware buffer pointer index
 2219  */
 2220 int
 2221 chn_getptr(struct pcm_channel *c)
 2222 {
 2223         int hwptr;
 2224 
 2225         CHN_LOCKASSERT(c);
 2226         hwptr = (CHN_STARTED(c)) ? CHANNEL_GETPTR(c->methods, c->devinfo) : 0;
 2227         return (hwptr - (hwptr % sndbuf_getalign(c->bufhard)));
 2228 }
 2229 
 2230 struct pcmchan_caps *
 2231 chn_getcaps(struct pcm_channel *c)
 2232 {
 2233         CHN_LOCKASSERT(c);
 2234         return CHANNEL_GETCAPS(c->methods, c->devinfo);
 2235 }
 2236 
 2237 u_int32_t
 2238 chn_getformats(struct pcm_channel *c)
 2239 {
 2240         u_int32_t *fmtlist, fmts;
 2241         int i;
 2242 
 2243         fmtlist = chn_getcaps(c)->fmtlist;
 2244         fmts = 0;
 2245         for (i = 0; fmtlist[i]; i++)
 2246                 fmts |= fmtlist[i];
 2247 
 2248         /* report software-supported formats */
 2249         if (!CHN_BITPERFECT(c) && report_soft_formats)
 2250                 fmts |= AFMT_CONVERTIBLE;
 2251 
 2252         return (AFMT_ENCODING(fmts));
 2253 }
 2254 
 2255 int
 2256 chn_notify(struct pcm_channel *c, u_int32_t flags)
 2257 {
 2258         struct pcm_channel *ch;
 2259         struct pcmchan_caps *caps;
 2260         uint32_t bestformat, bestspeed, besthwformat, *vchanformat, *vchanrate;
 2261         uint32_t vpflags;
 2262         int dirty, err, run, nrun;
 2263 
 2264         CHN_LOCKASSERT(c);
 2265 
 2266         if (CHN_EMPTY(c, children))
 2267                 return (ENODEV);
 2268 
 2269         err = 0;
 2270 
 2271         /*
 2272          * If the hwchan is running, we can't change its rate, format or
 2273          * blocksize
 2274          */
 2275         run = (CHN_STARTED(c)) ? 1 : 0;
 2276         if (run)
 2277                 flags &= CHN_N_VOLUME | CHN_N_TRIGGER;
 2278 
 2279         if (flags & CHN_N_RATE) {
 2280                 /*
 2281                  * XXX I'll make good use of this someday.
 2282                  *     However this is currently being superseded by
 2283                  *     the availability of CHN_F_VCHAN_DYNAMIC.
 2284                  */
 2285         }
 2286 
 2287         if (flags & CHN_N_FORMAT) {
 2288                 /*
 2289                  * XXX I'll make good use of this someday.
 2290                  *     However this is currently being superseded by
 2291                  *     the availability of CHN_F_VCHAN_DYNAMIC.
 2292                  */
 2293         }
 2294 
 2295         if (flags & CHN_N_VOLUME) {
 2296                 /*
 2297                  * XXX I'll make good use of this someday, though
 2298                  *     soft volume control is currently pretty much
 2299                  *     integrated.
 2300                  */
 2301         }
 2302 
 2303         if (flags & CHN_N_BLOCKSIZE) {
 2304                 /*
 2305                  * Set to default latency profile
 2306                  */
 2307                 chn_setlatency(c, chn_latency);
 2308         }
 2309 
 2310         if ((flags & CHN_N_TRIGGER) && !(c->flags & CHN_F_VCHAN_DYNAMIC)) {
 2311                 nrun = CHN_EMPTY(c, children.busy) ? 0 : 1;
 2312                 if (nrun && !run)
 2313                         err = chn_start(c, 1);
 2314                 if (!nrun && run)
 2315                         chn_abort(c);
 2316                 flags &= ~CHN_N_TRIGGER;
 2317         }
 2318 
 2319         if (flags & CHN_N_TRIGGER) {
 2320                 if (c->direction == PCMDIR_PLAY) {
 2321                         vchanformat = &c->parentsnddev->pvchanformat;
 2322                         vchanrate = &c->parentsnddev->pvchanrate;
 2323                 } else {
 2324                         vchanformat = &c->parentsnddev->rvchanformat;
 2325                         vchanrate = &c->parentsnddev->rvchanrate;
 2326                 }
 2327 
 2328                 /* Dynamic Virtual Channel */
 2329                 if (!(c->flags & CHN_F_VCHAN_ADAPTIVE)) {
 2330                         bestformat = *vchanformat;
 2331                         bestspeed = *vchanrate;
 2332                 } else {
 2333                         bestformat = 0;
 2334                         bestspeed = 0;
 2335                 }
 2336 
 2337                 besthwformat = 0;
 2338                 nrun = 0;
 2339                 caps = chn_getcaps(c);
 2340                 dirty = 0;
 2341                 vpflags = 0;
 2342 
 2343                 CHN_FOREACH(ch, c, children.busy) {
 2344                         CHN_LOCK(ch);
 2345                         if ((ch->format & AFMT_PASSTHROUGH) &&
 2346                             snd_fmtvalid(ch->format, caps->fmtlist)) {
 2347                                 bestformat = ch->format;
 2348                                 bestspeed = ch->speed;
 2349                                 CHN_UNLOCK(ch);
 2350                                 vpflags = CHN_F_PASSTHROUGH;
 2351                                 nrun++;
 2352                                 break;
 2353                         }
 2354                         if ((ch->flags & CHN_F_EXCLUSIVE) && vpflags == 0) {
 2355                                 if (c->flags & CHN_F_VCHAN_ADAPTIVE) {
 2356                                         bestspeed = ch->speed;
 2357                                         RANGE(bestspeed, caps->minspeed,
 2358                                             caps->maxspeed);
 2359                                         besthwformat = snd_fmtbest(ch->format,
 2360                                             caps->fmtlist);
 2361                                         if (besthwformat != 0)
 2362                                                 bestformat = besthwformat;
 2363                                 }
 2364                                 CHN_UNLOCK(ch);
 2365                                 vpflags = CHN_F_EXCLUSIVE;
 2366                                 nrun++;
 2367                                 continue;
 2368                         }
 2369                         if (!(c->flags & CHN_F_VCHAN_ADAPTIVE) ||
 2370                             vpflags != 0) {
 2371                                 CHN_UNLOCK(ch);
 2372                                 nrun++;
 2373                                 continue;
 2374                         }
 2375                         if (ch->speed > bestspeed) {
 2376                                 bestspeed = ch->speed;
 2377                                 RANGE(bestspeed, caps->minspeed,
 2378                                     caps->maxspeed);
 2379                         }
 2380                         besthwformat = snd_fmtbest(ch->format, caps->fmtlist);
 2381                         if (!(besthwformat & AFMT_VCHAN)) {
 2382                                 CHN_UNLOCK(ch);
 2383                                 nrun++;
 2384                                 continue;
 2385                         }
 2386                         if (AFMT_CHANNEL(besthwformat) >
 2387                             AFMT_CHANNEL(bestformat))
 2388                                 bestformat = besthwformat;
 2389                         else if (AFMT_CHANNEL(besthwformat) ==
 2390                             AFMT_CHANNEL(bestformat) &&
 2391                             AFMT_BIT(besthwformat) > AFMT_BIT(bestformat))
 2392                                 bestformat = besthwformat;
 2393                         CHN_UNLOCK(ch);
 2394                         nrun++;
 2395                 }
 2396 
 2397                 if (bestformat == 0)
 2398                         bestformat = c->format;
 2399                 if (bestspeed == 0)
 2400                         bestspeed = c->speed;
 2401 
 2402                 if (bestformat != c->format || bestspeed != c->speed)
 2403                         dirty = 1;
 2404 
 2405                 c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
 2406                 c->flags |= vpflags;
 2407 
 2408                 if (nrun && !run) {
 2409                         if (dirty) {
 2410                                 bestspeed = CHANNEL_SETSPEED(c->methods,
 2411                                     c->devinfo, bestspeed);
 2412                                 err = chn_reset(c, bestformat, bestspeed);
 2413                         }
 2414                         if (err == 0 && dirty) {
 2415                                 CHN_FOREACH(ch, c, children.busy) {
 2416                                         CHN_LOCK(ch);
 2417                                         if (VCHAN_SYNC_REQUIRED(ch))
 2418                                                 vchan_sync(ch);
 2419                                         CHN_UNLOCK(ch);
 2420                                 }
 2421                         }
 2422                         if (err == 0) {
 2423                                 if (dirty)
 2424                                         c->flags |= CHN_F_DIRTY;
 2425                                 err = chn_start(c, 1);
 2426                         }
 2427                 }
 2428 
 2429                 if (nrun && run && dirty) {
 2430                         chn_abort(c);
 2431                         bestspeed = CHANNEL_SETSPEED(c->methods, c->devinfo,
 2432                             bestspeed);
 2433                         err = chn_reset(c, bestformat, bestspeed);
 2434                         if (err == 0) {
 2435                                 CHN_FOREACH(ch, c, children.busy) {
 2436                                         CHN_LOCK(ch);
 2437                                         if (VCHAN_SYNC_REQUIRED(ch))
 2438                                                 vchan_sync(ch);
 2439                                         CHN_UNLOCK(ch);
 2440                                 }
 2441                         }
 2442                         if (err == 0) {
 2443                                 c->flags |= CHN_F_DIRTY;
 2444                                 err = chn_start(c, 1);
 2445                         }
 2446                 }
 2447 
 2448                 if (err == 0 && !(bestformat & AFMT_PASSTHROUGH) &&
 2449                     (bestformat & AFMT_VCHAN)) {
 2450                         *vchanformat = bestformat;
 2451                         *vchanrate = bestspeed;
 2452                 }
 2453 
 2454                 if (!nrun && run) {
 2455                         c->flags &= ~(CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE);
 2456                         bestformat = *vchanformat;
 2457                         bestspeed = *vchanrate;
 2458                         chn_abort(c);
 2459                         if (c->format != bestformat || c->speed != bestspeed)
 2460                                 chn_reset(c, bestformat, bestspeed);
 2461                 }
 2462         }
 2463 
 2464         return (err);
 2465 }
 2466 
 2467 /**
 2468  * @brief Fetch array of supported discrete sample rates
 2469  *
 2470  * Wrapper for CHANNEL_GETRATES.  Please see channel_if.m:getrates() for
 2471  * detailed information.
 2472  *
 2473  * @note If the operation isn't supported, this function will just return 0
 2474  *       (no rates in the array), and *rates will be set to NULL.  Callers
 2475  *       should examine rates @b only if this function returns non-zero.
 2476  *
 2477  * @param c     pcm channel to examine
 2478  * @param rates pointer to array of integers; rate table will be recorded here
 2479  *
 2480  * @return number of rates in the array pointed to be @c rates
 2481  */
 2482 int
 2483 chn_getrates(struct pcm_channel *c, int **rates)
 2484 {
 2485         KASSERT(rates != NULL, ("rates is null"));
 2486         CHN_LOCKASSERT(c);
 2487         return CHANNEL_GETRATES(c->methods, c->devinfo, rates);
 2488 }
 2489 
 2490 /**
 2491  * @brief Remove channel from a sync group, if there is one.
 2492  *
 2493  * This function is initially intended for the following conditions:
 2494  *   - Starting a syncgroup (@c SNDCTL_DSP_SYNCSTART ioctl)
 2495  *   - Closing a device.  (A channel can't be destroyed if it's still in use.)
 2496  *
 2497  * @note Before calling this function, the syncgroup list mutex must be
 2498  * held.  (Consider pcm_channel::sm protected by the SG list mutex
 2499  * whether @c c is locked or not.)
 2500  *
 2501  * @param c     channel device to be started or closed
 2502  * @returns     If this channel was the only member of a group, the group ID
 2503  *              is returned to the caller so that the caller can release it
 2504  *              via free_unr() after giving up the syncgroup lock.  Else it
 2505  *              returns 0.
 2506  */
 2507 int
 2508 chn_syncdestroy(struct pcm_channel *c)
 2509 {
 2510         struct pcmchan_syncmember *sm;
 2511         struct pcmchan_syncgroup *sg;
 2512         int sg_id;
 2513 
 2514         sg_id = 0;
 2515 
 2516         PCM_SG_LOCKASSERT(MA_OWNED);
 2517 
 2518         if (c->sm != NULL) {
 2519                 sm = c->sm;
 2520                 sg = sm->parent;
 2521                 c->sm = NULL;
 2522 
 2523                 KASSERT(sg != NULL, ("syncmember has null parent"));
 2524 
 2525                 SLIST_REMOVE(&sg->members, sm, pcmchan_syncmember, link);
 2526                 free(sm, M_DEVBUF);
 2527 
 2528                 if (SLIST_EMPTY(&sg->members)) {
 2529                         SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
 2530                         sg_id = sg->id;
 2531                         free(sg, M_DEVBUF);
 2532                 }
 2533         }
 2534 
 2535         return sg_id;
 2536 }
 2537 
 2538 #ifdef OSSV4_EXPERIMENT
 2539 int
 2540 chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak)
 2541 {
 2542         CHN_LOCKASSERT(c);
 2543         return CHANNEL_GETPEAKS(c->methods, c->devinfo, lpeak, rpeak);
 2544 }
 2545 #endif

Cache object: 3ac3ea8daa864680a4e07220f2fc1a01


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.