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/dsp.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  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #ifdef HAVE_KERNEL_OPTION_HEADERS
   30 #include "opt_snd.h"
   31 #endif
   32 
   33 #include <dev/sound/pcm/sound.h>
   34 #include <sys/ctype.h>
   35 #include <sys/sysent.h>
   36 
   37 #include <vm/vm.h>
   38 #include <vm/vm_object.h>
   39 #include <vm/vm_page.h>
   40 #include <vm/vm_pager.h>
   41 
   42 SND_DECLARE_FILE("$FreeBSD$");
   43 
   44 static int dsp_mmap_allow_prot_exec = 0;
   45 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW,
   46     &dsp_mmap_allow_prot_exec, 0,
   47     "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
   48 
   49 static int dsp_basename_clone = 1;
   50 SYSCTL_INT(_hw_snd, OID_AUTO, basename_clone, CTLFLAG_RWTUN,
   51     &dsp_basename_clone, 0,
   52     "DSP basename cloning (0: Disable; 1: Enabled)");
   53 TUNABLE_INT("hw.snd.basename_clone", &dsp_basename_clone);
   54 
   55 struct dsp_cdevinfo {
   56         struct pcm_channel *rdch, *wrch;
   57         struct pcm_channel *volch;
   58         int busy, simplex;
   59         TAILQ_ENTRY(dsp_cdevinfo) link;
   60 };
   61 
   62 #define PCM_RDCH(x)             (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
   63 #define PCM_WRCH(x)             (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
   64 #define PCM_VOLCH(x)            (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
   65 #define PCM_SIMPLEX(x)          (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
   66 
   67 #define DSP_CDEVINFO_CACHESIZE  8
   68 
   69 #define DSP_REGISTERED(x, y)    (PCM_REGISTERED(x) &&                   \
   70                                  (y) != NULL && (y)->si_drv1 != NULL)
   71 
   72 #define OLDPCM_IOCTL
   73 
   74 static d_open_t dsp_open;
   75 static d_close_t dsp_close;
   76 static d_read_t dsp_read;
   77 static d_write_t dsp_write;
   78 static d_ioctl_t dsp_ioctl;
   79 static d_poll_t dsp_poll;
   80 static d_mmap_t dsp_mmap;
   81 static d_mmap_single_t dsp_mmap_single;
   82 
   83 struct cdevsw dsp_cdevsw = {
   84         .d_version =    D_VERSION,
   85         .d_open =       dsp_open,
   86         .d_close =      dsp_close,
   87         .d_read =       dsp_read,
   88         .d_write =      dsp_write,
   89         .d_ioctl =      dsp_ioctl,
   90         .d_poll =       dsp_poll,
   91         .d_mmap =       dsp_mmap,
   92         .d_mmap_single = dsp_mmap_single,
   93         .d_name =       "dsp",
   94 };
   95 
   96 static eventhandler_tag dsp_ehtag = NULL;
   97 static int dsp_umax = -1;
   98 static int dsp_cmax = -1;
   99 
  100 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
  101 static int dsp_oss_syncstart(int sg_id);
  102 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
  103 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
  104 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
  105 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
  106 static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
  107 #ifdef OSSV4_EXPERIMENT
  108 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
  109 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
  110 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
  111 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
  112 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
  113 #endif
  114 
  115 static struct snddev_info *
  116 dsp_get_info(struct cdev *dev)
  117 {
  118         return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
  119 }
  120 
  121 static uint32_t
  122 dsp_get_flags(struct cdev *dev)
  123 {
  124         device_t bdev;
  125 
  126         bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
  127 
  128         return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
  129 }
  130 
  131 static void
  132 dsp_set_flags(struct cdev *dev, uint32_t flags)
  133 {
  134         device_t bdev;
  135 
  136         bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
  137 
  138         if (bdev != NULL)
  139                 pcm_setflags(bdev, flags);
  140 }
  141 
  142 /*
  143  * return the channels associated with an open device instance.
  144  * lock channels specified.
  145  */
  146 static int
  147 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
  148     uint32_t prio)
  149 {
  150         struct snddev_info *d;
  151         struct pcm_channel *ch;
  152         uint32_t flags;
  153 
  154         if (PCM_SIMPLEX(dev) != 0) {
  155                 d = dsp_get_info(dev);
  156                 if (!PCM_REGISTERED(d))
  157                         return (ENXIO);
  158                 PCM_LOCK(d);
  159                 PCM_WAIT(d);
  160                 PCM_ACQUIRE(d);
  161                 /*
  162                  * Note: order is important -
  163                  *       pcm flags -> prio query flags -> wild guess
  164                  */
  165                 ch = NULL;
  166                 flags = dsp_get_flags(dev);
  167                 if (flags & SD_F_PRIO_WR) {
  168                         ch = PCM_RDCH(dev);
  169                         PCM_RDCH(dev) = NULL;
  170                 } else if (flags & SD_F_PRIO_RD) {
  171                         ch = PCM_WRCH(dev);
  172                         PCM_WRCH(dev) = NULL;
  173                 } else if (prio & SD_F_PRIO_WR) {
  174                         ch = PCM_RDCH(dev);
  175                         PCM_RDCH(dev) = NULL;
  176                         flags |= SD_F_PRIO_WR;
  177                 } else if (prio & SD_F_PRIO_RD) {
  178                         ch = PCM_WRCH(dev);
  179                         PCM_WRCH(dev) = NULL;
  180                         flags |= SD_F_PRIO_RD;
  181                 } else if (PCM_WRCH(dev) != NULL) {
  182                         ch = PCM_RDCH(dev);
  183                         PCM_RDCH(dev) = NULL;
  184                         flags |= SD_F_PRIO_WR;
  185                 } else if (PCM_RDCH(dev) != NULL) {
  186                         ch = PCM_WRCH(dev);
  187                         PCM_WRCH(dev) = NULL;
  188                         flags |= SD_F_PRIO_RD;
  189                 }
  190                 PCM_SIMPLEX(dev) = 0;
  191                 dsp_set_flags(dev, flags);
  192                 if (ch != NULL) {
  193                         CHN_LOCK(ch);
  194                         pcm_chnref(ch, -1);
  195                         pcm_chnrelease(ch);
  196                 }
  197                 PCM_RELEASE(d);
  198                 PCM_UNLOCK(d);
  199         }
  200 
  201         *rdch = PCM_RDCH(dev);
  202         *wrch = PCM_WRCH(dev);
  203 
  204         if (*rdch != NULL && (prio & SD_F_PRIO_RD))
  205                 CHN_LOCK(*rdch);
  206         if (*wrch != NULL && (prio & SD_F_PRIO_WR))
  207                 CHN_LOCK(*wrch);
  208 
  209         return (0);
  210 }
  211 
  212 /* unlock specified channels */
  213 static void
  214 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
  215     uint32_t prio)
  216 {
  217         if (wrch != NULL && (prio & SD_F_PRIO_WR))
  218                 CHN_UNLOCK(wrch);
  219         if (rdch != NULL && (prio & SD_F_PRIO_RD))
  220                 CHN_UNLOCK(rdch);
  221 }
  222 
  223 static void
  224 dsp_cdevinfo_alloc(struct cdev *dev,
  225     struct pcm_channel *rdch, struct pcm_channel *wrch,
  226     struct pcm_channel *volch)
  227 {
  228         struct snddev_info *d;
  229         struct dsp_cdevinfo *cdi;
  230         int simplex;
  231 
  232         d = dsp_get_info(dev);
  233 
  234         KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
  235             ((rdch == NULL && wrch == NULL) || rdch != wrch),
  236             ("bogus %s(), what are you trying to accomplish here?", __func__));
  237         PCM_BUSYASSERT(d);
  238         PCM_LOCKASSERT(d);
  239 
  240         simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
  241 
  242         /*
  243          * Scan for free instance entry and put it into the end of list.
  244          * Create new one if necessary.
  245          */
  246         TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
  247                 if (cdi->busy != 0)
  248                         break;
  249                 cdi->rdch = rdch;
  250                 cdi->wrch = wrch;
  251                 cdi->volch = volch;
  252                 cdi->simplex = simplex;
  253                 cdi->busy = 1;
  254                 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
  255                 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
  256                 dev->si_drv1 = cdi;
  257                 return;
  258         }
  259         PCM_UNLOCK(d);
  260         cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
  261         PCM_LOCK(d);
  262         cdi->rdch = rdch;
  263         cdi->wrch = wrch;
  264         cdi->volch = volch;
  265         cdi->simplex = simplex;
  266         cdi->busy = 1;
  267         TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
  268         dev->si_drv1 = cdi;
  269 }
  270 
  271 static void
  272 dsp_cdevinfo_free(struct cdev *dev)
  273 {
  274         struct snddev_info *d;
  275         struct dsp_cdevinfo *cdi, *tmp;
  276         uint32_t flags;
  277         int i;
  278 
  279         d = dsp_get_info(dev);
  280 
  281         KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
  282             PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
  283             PCM_VOLCH(dev) == NULL,
  284             ("bogus %s(), what are you trying to accomplish here?", __func__));
  285         PCM_BUSYASSERT(d);
  286         PCM_LOCKASSERT(d);
  287 
  288         cdi = dev->si_drv1;
  289         dev->si_drv1 = NULL;
  290         cdi->rdch = NULL;
  291         cdi->wrch = NULL;
  292         cdi->volch = NULL;
  293         cdi->simplex = 0;
  294         cdi->busy = 0;
  295 
  296         /*
  297          * Once it is free, move it back to the beginning of list for
  298          * faster new entry allocation.
  299          */
  300         TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
  301         TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
  302 
  303         /*
  304          * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
  305          * Reset simplex flags.
  306          */
  307         flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
  308         i = DSP_CDEVINFO_CACHESIZE;
  309         TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
  310                 if (cdi->busy != 0) {
  311                         if (cdi->simplex == 0) {
  312                                 if (cdi->rdch != NULL)
  313                                         flags |= SD_F_PRIO_RD;
  314                                 if (cdi->wrch != NULL)
  315                                         flags |= SD_F_PRIO_WR;
  316                         }
  317                 } else {
  318                         if (i == 0) {
  319                                 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
  320                                 free(cdi, M_DEVBUF);
  321                         } else
  322                                 i--;
  323                 }
  324         }
  325         dsp_set_flags(dev, flags);
  326 }
  327 
  328 void
  329 dsp_cdevinfo_init(struct snddev_info *d)
  330 {
  331         struct dsp_cdevinfo *cdi;
  332         int i;
  333 
  334         KASSERT(d != NULL, ("NULL snddev_info"));
  335         PCM_BUSYASSERT(d);
  336         PCM_UNLOCKASSERT(d);
  337 
  338         TAILQ_INIT(&d->dsp_cdevinfo_pool);
  339         for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
  340                 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
  341                 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
  342         }
  343 }
  344 
  345 void
  346 dsp_cdevinfo_flush(struct snddev_info *d)
  347 {
  348         struct dsp_cdevinfo *cdi, *tmp;
  349 
  350         KASSERT(d != NULL, ("NULL snddev_info"));
  351         PCM_BUSYASSERT(d);
  352         PCM_UNLOCKASSERT(d);
  353 
  354         cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
  355         while (cdi != NULL) {
  356                 tmp = TAILQ_NEXT(cdi, link);
  357                 free(cdi, M_DEVBUF);
  358                 cdi = tmp;
  359         }
  360         TAILQ_INIT(&d->dsp_cdevinfo_pool);
  361 }
  362 
  363 /* duplex / simplex cdev type */
  364 enum {
  365         DSP_CDEV_TYPE_RDONLY,           /* simplex read-only (record)   */
  366         DSP_CDEV_TYPE_WRONLY,           /* simplex write-only (play)    */
  367         DSP_CDEV_TYPE_RDWR              /* duplex read, write, or both  */
  368 };
  369 
  370 enum {
  371         DSP_CDEV_VOLCTL_NONE,
  372         DSP_CDEV_VOLCTL_READ,
  373         DSP_CDEV_VOLCTL_WRITE
  374 };
  375 
  376 #define DSP_F_VALID(x)          ((x) & (FREAD | FWRITE))
  377 #define DSP_F_DUPLEX(x)         (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
  378 #define DSP_F_SIMPLEX(x)        (!DSP_F_DUPLEX(x))
  379 #define DSP_F_READ(x)           ((x) & FREAD)
  380 #define DSP_F_WRITE(x)          ((x) & FWRITE)
  381 
  382 static const struct {
  383         int type;
  384         char *name;
  385         char *sep;
  386         char *alias;
  387         int use_sep;
  388         int hw;
  389         int max;
  390         int volctl;
  391         uint32_t fmt, spd;
  392         int query;
  393 } dsp_cdevs[] = {
  394         { SND_DEV_DSP,         "dsp",    ".", NULL, 0, 0, 0, 0,
  395           SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
  396           DSP_CDEV_TYPE_RDWR },
  397         { SND_DEV_AUDIO,       "audio",  ".", NULL, 0, 0, 0, 0,
  398           SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
  399           DSP_CDEV_TYPE_RDWR },
  400         { SND_DEV_DSP16,       "dspW",   ".", NULL, 0, 0, 0, 0,
  401           SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
  402           DSP_CDEV_TYPE_RDWR },
  403         { SND_DEV_DSPHW_PLAY,  "dsp",   ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
  404           SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
  405         { SND_DEV_DSPHW_VPLAY, "dsp",  ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
  406           SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
  407         { SND_DEV_DSPHW_REC,   "dsp",   ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
  408           SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
  409         { SND_DEV_DSPHW_VREC,  "dsp",  ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
  410           SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
  411         { SND_DEV_DSPHW_CD,    "dspcd",  ".", NULL, 0, 0, 0, 0,
  412           SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR   },
  413         /* Low priority, OSSv4 aliases. */
  414         { SND_DEV_DSP,      "dsp_ac3",   ".", "dsp", 0, 0, 0, 0,
  415           SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
  416           DSP_CDEV_TYPE_RDWR },
  417         { SND_DEV_DSP,     "dsp_mmap",   ".", "dsp", 0, 0, 0, 0,
  418           SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
  419           DSP_CDEV_TYPE_RDWR },
  420         { SND_DEV_DSP,  "dsp_multich",   ".", "dsp", 0, 0, 0, 0,
  421           SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
  422           DSP_CDEV_TYPE_RDWR },
  423         { SND_DEV_DSP, "dsp_spdifout",   ".", "dsp", 0, 0, 0, 0,
  424           SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
  425           DSP_CDEV_TYPE_RDWR },
  426         { SND_DEV_DSP,  "dsp_spdifin",   ".", "dsp", 0, 0, 0, 0,
  427           SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
  428           DSP_CDEV_TYPE_RDWR },
  429 };
  430 
  431 #define DSP_FIXUP_ERROR()               do {                            \
  432         prio = dsp_get_flags(i_dev);                                    \
  433         if (!DSP_F_VALID(flags))                                        \
  434                 error = EINVAL;                                         \
  435         if (!DSP_F_DUPLEX(flags) &&                                     \
  436             ((DSP_F_READ(flags) && d->reccount == 0) ||                 \
  437             (DSP_F_WRITE(flags) && d->playcount == 0)))                 \
  438                 error = ENOTSUP;                                        \
  439         else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) &&       \
  440             ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) ||            \
  441             (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD))))             \
  442                 error = EBUSY;                                          \
  443         else if (DSP_REGISTERED(d, i_dev))                              \
  444                 error = EBUSY;                                          \
  445 } while (0)
  446 
  447 static int
  448 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
  449 {
  450         struct pcm_channel *rdch, *wrch;
  451         struct snddev_info *d;
  452         uint32_t fmt, spd, prio, volctl;
  453         int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
  454 
  455         /* Kind of impossible.. */
  456         if (i_dev == NULL || td == NULL)
  457                 return (ENODEV);
  458 
  459         d = dsp_get_info(i_dev);
  460         if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
  461                 return (EBADF);
  462 
  463         PCM_GIANT_ENTER(d);
  464 
  465         /* Lock snddev so nobody else can monkey with it. */
  466         PCM_LOCK(d);
  467         PCM_WAIT(d);
  468 
  469         /*
  470          * Try to acquire cloned device before someone else pick it.
  471          * ENODEV means this is not a cloned droids.
  472          */
  473         error = snd_clone_acquire(i_dev);
  474         if (!(error == 0 || error == ENODEV)) {
  475                 DSP_FIXUP_ERROR();
  476                 PCM_UNLOCK(d);
  477                 PCM_GIANT_EXIT(d);
  478                 return (error);
  479         }
  480 
  481         error = 0;
  482         DSP_FIXUP_ERROR();
  483 
  484         if (error != 0) {
  485                 (void)snd_clone_release(i_dev);
  486                 PCM_UNLOCK(d);
  487                 PCM_GIANT_EXIT(d);
  488                 return (error);
  489         }
  490 
  491         /*
  492          * That is just enough. Acquire and unlock pcm lock so
  493          * the other will just have to wait until we finish doing
  494          * everything.
  495          */
  496         PCM_ACQUIRE(d);
  497         PCM_UNLOCK(d);
  498 
  499         devtype = PCMDEV(i_dev);
  500         wdevunit = -1;
  501         rdevunit = -1;
  502         fmt = 0;
  503         spd = 0;
  504         volctl = DSP_CDEV_VOLCTL_NONE;
  505 
  506         for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
  507                 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
  508                         continue;
  509                 /*
  510                  * Volume control only valid for DSPHW devices,
  511                  * and it must be opened in opposite direction be it
  512                  * simplex or duplex. Anything else will be handled
  513                  * as usual.
  514                  */
  515                 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
  516                         if (dsp_cdevs[i].volctl != 0 &&
  517                             DSP_F_READ(flags)) {
  518                                 volctl = DSP_CDEV_VOLCTL_WRITE;
  519                                 flags &= ~FREAD;
  520                                 flags |= FWRITE;
  521                         }
  522                         if (DSP_F_READ(flags)) {
  523                                 (void)snd_clone_release(i_dev);
  524                                 PCM_RELEASE_QUICK(d);
  525                                 PCM_GIANT_EXIT(d);
  526                                 return (ENOTSUP);
  527                         }
  528                         wdevunit = dev2unit(i_dev);
  529                 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
  530                         if (dsp_cdevs[i].volctl != 0 &&
  531                             DSP_F_WRITE(flags)) {
  532                                 volctl = DSP_CDEV_VOLCTL_READ;
  533                                 flags &= ~FWRITE;
  534                                 flags |= FREAD;
  535                         }
  536                         if (DSP_F_WRITE(flags)) {
  537                                 (void)snd_clone_release(i_dev);
  538                                 PCM_RELEASE_QUICK(d);
  539                                 PCM_GIANT_EXIT(d);
  540                                 return (ENOTSUP);
  541                         }
  542                         rdevunit = dev2unit(i_dev);
  543                 }
  544                 fmt = dsp_cdevs[i].fmt;
  545                 spd = dsp_cdevs[i].spd;
  546                 break;
  547         }
  548 
  549         /* No matching devtype? */
  550         if (fmt == 0 || spd == 0)
  551                 panic("impossible devtype %d", devtype);
  552 
  553         rdch = NULL;
  554         wrch = NULL;
  555         rderror = 0;
  556         wrerror = 0;
  557 
  558         /*
  559          * if we get here, the open request is valid- either:
  560          *   * we were previously not open
  561          *   * we were open for play xor record and the opener wants
  562          *     the non-open direction
  563          */
  564         if (DSP_F_READ(flags)) {
  565                 /* open for read */
  566                 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
  567                     td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
  568 
  569                 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
  570                         rderror = ENXIO;
  571 
  572                 if (volctl == DSP_CDEV_VOLCTL_READ)
  573                         rderror = 0;
  574 
  575                 if (rderror != 0) {
  576                         if (rdch != NULL)
  577                                 pcm_chnrelease(rdch);
  578                         if (!DSP_F_DUPLEX(flags)) {
  579                                 (void)snd_clone_release(i_dev);
  580                                 PCM_RELEASE_QUICK(d);
  581                                 PCM_GIANT_EXIT(d);
  582                                 return (rderror);
  583                         }
  584                         rdch = NULL;
  585                 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
  586                         if (rdch != NULL) {
  587                                 pcm_chnref(rdch, 1);
  588                                 pcm_chnrelease(rdch);
  589                         }
  590                 } else {
  591                         if (flags & O_NONBLOCK)
  592                                 rdch->flags |= CHN_F_NBIO;
  593                         if (flags & O_EXCL)
  594                                 rdch->flags |= CHN_F_EXCLUSIVE;
  595                         pcm_chnref(rdch, 1);
  596                         if (volctl == DSP_CDEV_VOLCTL_NONE)
  597                                 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
  598                         CHN_UNLOCK(rdch);
  599                 }
  600         }
  601 
  602         if (DSP_F_WRITE(flags)) {
  603                 /* open for write */
  604                 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
  605                     td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
  606 
  607                 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
  608                         wrerror = ENXIO;
  609 
  610                 if (volctl == DSP_CDEV_VOLCTL_WRITE)
  611                         wrerror = 0;
  612 
  613                 if (wrerror != 0) {
  614                         if (wrch != NULL)
  615                                 pcm_chnrelease(wrch);
  616                         if (!DSP_F_DUPLEX(flags)) {
  617                                 if (rdch != NULL) {
  618                                         /*
  619                                          * Lock, deref and release previously
  620                                          * created record channel
  621                                          */
  622                                         CHN_LOCK(rdch);
  623                                         pcm_chnref(rdch, -1);
  624                                         pcm_chnrelease(rdch);
  625                                 }
  626                                 (void)snd_clone_release(i_dev);
  627                                 PCM_RELEASE_QUICK(d);
  628                                 PCM_GIANT_EXIT(d);
  629                                 return (wrerror);
  630                         }
  631                         wrch = NULL;
  632                 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
  633                         if (wrch != NULL) {
  634                                 pcm_chnref(wrch, 1);
  635                                 pcm_chnrelease(wrch);
  636                         }
  637                 } else {
  638                         if (flags & O_NONBLOCK)
  639                                 wrch->flags |= CHN_F_NBIO;
  640                         if (flags & O_EXCL)
  641                                 wrch->flags |= CHN_F_EXCLUSIVE;
  642                         pcm_chnref(wrch, 1);
  643                         if (volctl == DSP_CDEV_VOLCTL_NONE)
  644                                 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
  645                         CHN_UNLOCK(wrch);
  646                 }
  647         }
  648 
  649 
  650         PCM_LOCK(d);
  651 
  652         /*
  653          * We're done. Allocate channels information for this cdev.
  654          */
  655         switch (volctl) {
  656         case DSP_CDEV_VOLCTL_READ:
  657                 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
  658                 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
  659                 break;
  660         case DSP_CDEV_VOLCTL_WRITE:
  661                 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
  662                 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
  663                 break;
  664         case DSP_CDEV_VOLCTL_NONE:
  665         default:
  666                 if (wrch == NULL && rdch == NULL) {
  667                         (void)snd_clone_release(i_dev);
  668                         PCM_RELEASE(d);
  669                         PCM_UNLOCK(d);
  670                         PCM_GIANT_EXIT(d);
  671                         if (wrerror != 0)
  672                                 return (wrerror);
  673                         if (rderror != 0)
  674                                 return (rderror);
  675                         return (EINVAL);
  676                 }
  677                 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
  678                 if (rdch != NULL)
  679                         CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
  680                 if (wrch != NULL)
  681                         CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
  682                 break;
  683         }
  684 
  685         /*
  686          * Increase clone refcount for its automatic garbage collector.
  687          */
  688         (void)snd_clone_ref(i_dev);
  689 
  690         PCM_RELEASE(d);
  691         PCM_UNLOCK(d);
  692 
  693         PCM_GIANT_LEAVE(d);
  694 
  695         return (0);
  696 }
  697 
  698 static int
  699 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
  700 {
  701         struct pcm_channel *rdch, *wrch, *volch;
  702         struct snddev_info *d;
  703         int sg_ids, rdref, wdref;
  704 
  705         d = dsp_get_info(i_dev);
  706         if (!DSP_REGISTERED(d, i_dev))
  707                 return (EBADF);
  708 
  709         PCM_GIANT_ENTER(d);
  710 
  711         PCM_LOCK(d);
  712         PCM_WAIT(d);
  713         PCM_ACQUIRE(d);
  714 
  715         rdch = PCM_RDCH(i_dev);
  716         wrch = PCM_WRCH(i_dev);
  717         volch = PCM_VOLCH(i_dev);
  718 
  719         PCM_RDCH(i_dev) = NULL;
  720         PCM_WRCH(i_dev) = NULL;
  721         PCM_VOLCH(i_dev) = NULL;
  722 
  723         rdref = -1;
  724         wdref = -1;
  725 
  726         if (volch != NULL) {
  727                 if (volch == rdch)
  728                         rdref--;
  729                 else if (volch == wrch)
  730                         wdref--;
  731                 else {
  732                         CHN_LOCK(volch);
  733                         pcm_chnref(volch, -1);
  734                         CHN_UNLOCK(volch);
  735                 }
  736         }
  737 
  738         if (rdch != NULL)
  739                 CHN_REMOVE(d, rdch, channels.pcm.opened);
  740         if (wrch != NULL)
  741                 CHN_REMOVE(d, wrch, channels.pcm.opened);
  742 
  743         if (rdch != NULL || wrch != NULL) {
  744                 PCM_UNLOCK(d);
  745                 if (rdch != NULL) {
  746                         /*
  747                          * The channel itself need not be locked because:
  748                          *   a)  Adding a channel to a syncgroup happens only
  749                          *       in dsp_ioctl(), which cannot run concurrently
  750                          *       to dsp_close().
  751                          *   b)  The syncmember pointer (sm) is protected by
  752                          *       the global syncgroup list lock.
  753                          *   c)  A channel can't just disappear, invalidating
  754                          *       pointers, unless it's closed/dereferenced
  755                          *       first.
  756                          */
  757                         PCM_SG_LOCK();
  758                         sg_ids = chn_syncdestroy(rdch);
  759                         PCM_SG_UNLOCK();
  760                         if (sg_ids != 0)
  761                                 free_unr(pcmsg_unrhdr, sg_ids);
  762 
  763                         CHN_LOCK(rdch);
  764                         pcm_chnref(rdch, rdref);
  765                         chn_abort(rdch); /* won't sleep */
  766                         rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
  767                             CHN_F_DEAD | CHN_F_EXCLUSIVE);
  768                         chn_reset(rdch, 0, 0);
  769                         pcm_chnrelease(rdch);
  770                 }
  771                 if (wrch != NULL) {
  772                         /*
  773                          * Please see block above.
  774                          */
  775                         PCM_SG_LOCK();
  776                         sg_ids = chn_syncdestroy(wrch);
  777                         PCM_SG_UNLOCK();
  778                         if (sg_ids != 0)
  779                                 free_unr(pcmsg_unrhdr, sg_ids);
  780 
  781                         CHN_LOCK(wrch);
  782                         pcm_chnref(wrch, wdref);
  783                         chn_flush(wrch); /* may sleep */
  784                         wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
  785                             CHN_F_DEAD | CHN_F_EXCLUSIVE);
  786                         chn_reset(wrch, 0, 0);
  787                         pcm_chnrelease(wrch);
  788                 }
  789                 PCM_LOCK(d);
  790         }
  791 
  792         dsp_cdevinfo_free(i_dev);
  793         /*
  794          * Release clone busy state and unref it so the automatic
  795          * garbage collector will get the hint and do the remaining
  796          * cleanup process.
  797          */
  798         (void)snd_clone_release(i_dev);
  799 
  800         /*
  801          * destroy_dev() might sleep, so release pcm lock
  802          * here and rely on pcm cv serialization.
  803          */
  804         PCM_UNLOCK(d);
  805         (void)snd_clone_unref(i_dev);
  806         PCM_LOCK(d);
  807 
  808         PCM_RELEASE(d);
  809         PCM_UNLOCK(d);
  810 
  811         PCM_GIANT_LEAVE(d);
  812 
  813         return (0);
  814 }
  815 
  816 static __inline int
  817 dsp_io_ops(struct cdev *i_dev, struct uio *buf)
  818 {
  819         struct snddev_info *d;
  820         struct pcm_channel **ch, *rdch, *wrch;
  821         int (*chn_io)(struct pcm_channel *, struct uio *);
  822         int prio, ret;
  823         pid_t runpid;
  824 
  825         KASSERT(i_dev != NULL && buf != NULL &&
  826             (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
  827             ("%s(): io train wreck!", __func__));
  828 
  829         d = dsp_get_info(i_dev);
  830         if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
  831                 return (EBADF);
  832 
  833         PCM_GIANT_ENTER(d);
  834 
  835         switch (buf->uio_rw) {
  836         case UIO_READ:
  837                 prio = SD_F_PRIO_RD;
  838                 ch = &rdch;
  839                 chn_io = chn_read;
  840                 break;
  841         case UIO_WRITE:
  842                 prio = SD_F_PRIO_WR;
  843                 ch = &wrch;
  844                 chn_io = chn_write;
  845                 break;
  846         default:
  847                 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
  848                 break;
  849         }
  850 
  851         rdch = NULL;
  852         wrch = NULL;
  853         runpid = buf->uio_td->td_proc->p_pid;
  854 
  855         getchns(i_dev, &rdch, &wrch, prio);
  856 
  857         if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
  858                 PCM_GIANT_EXIT(d);
  859                 return (EBADF);
  860         }
  861 
  862         if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
  863             (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
  864                 relchns(i_dev, rdch, wrch, prio);
  865                 PCM_GIANT_EXIT(d);
  866                 return (EINVAL);
  867         } else if (!((*ch)->flags & CHN_F_RUNNING)) {
  868                 (*ch)->flags |= CHN_F_RUNNING;
  869                 (*ch)->pid = runpid;
  870         }
  871 
  872         /*
  873          * chn_read/write must give up channel lock in order to copy bytes
  874          * from/to userland, so up the "in progress" counter to make sure
  875          * someone else doesn't come along and muss up the buffer.
  876          */
  877         ++(*ch)->inprog;
  878         ret = chn_io(*ch, buf);
  879         --(*ch)->inprog;
  880 
  881         CHN_BROADCAST(&(*ch)->cv);
  882 
  883         relchns(i_dev, rdch, wrch, prio);
  884 
  885         PCM_GIANT_LEAVE(d);
  886 
  887         return (ret);
  888 }
  889 
  890 static int
  891 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
  892 {
  893         return (dsp_io_ops(i_dev, buf));
  894 }
  895 
  896 static int
  897 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
  898 {
  899         return (dsp_io_ops(i_dev, buf));
  900 }
  901 
  902 static int
  903 dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
  904 {
  905         struct snddev_info *d;
  906         struct pcm_channel *c;
  907         int unit;
  908 
  909         KASSERT(dev != NULL && volch != NULL,
  910             ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
  911 
  912         d = dsp_get_info(dev);
  913         if (!PCM_REGISTERED(d)) {
  914                 *volch = NULL;
  915                 return (EINVAL);
  916         }
  917 
  918         PCM_UNLOCKASSERT(d);
  919 
  920         *volch = NULL;
  921 
  922         c = PCM_VOLCH(dev);
  923         if (c != NULL) {
  924                 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
  925                         return (-1);
  926                 *volch = c;
  927                 return (0);
  928         }
  929 
  930         PCM_LOCK(d);
  931         PCM_WAIT(d);
  932         PCM_ACQUIRE(d);
  933 
  934         unit = dev2unit(dev);
  935 
  936         CHN_FOREACH(c, d, channels.pcm) {
  937                 CHN_LOCK(c);
  938                 if (c->unit != unit) {
  939                         CHN_UNLOCK(c);
  940                         continue;
  941                 }
  942                 *volch = c;
  943                 pcm_chnref(c, 1);
  944                 PCM_VOLCH(dev) = c;
  945                 CHN_UNLOCK(c);
  946                 PCM_RELEASE(d);
  947                 PCM_UNLOCK(d);
  948                 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
  949         }
  950 
  951         PCM_RELEASE(d);
  952         PCM_UNLOCK(d);
  953 
  954         return (EINVAL);
  955 }
  956 
  957 static int
  958 dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
  959     caddr_t arg)
  960 {
  961         struct snddev_info *d;
  962         struct pcm_channel *rdch, *wrch;
  963         int j, devtype, ret;
  964 
  965         d = dsp_get_info(dev);
  966         if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
  967                 return (-1);
  968 
  969         PCM_UNLOCKASSERT(d);
  970 
  971         j = cmd & 0xff;
  972 
  973         rdch = PCM_RDCH(dev);
  974         wrch = PCM_WRCH(dev);
  975 
  976         /* No specific channel, look into cache */
  977         if (volch == NULL)
  978                 volch = PCM_VOLCH(dev);
  979 
  980         /* Look harder */
  981         if (volch == NULL) {
  982                 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
  983                         volch = rdch;
  984                 else if (j == SOUND_MIXER_PCM && wrch != NULL)
  985                         volch = wrch;
  986         }
  987 
  988         devtype = PCMDEV(dev);
  989 
  990         /* Look super harder */
  991         if (volch == NULL &&
  992             (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
  993             devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
  994                 ret = dsp_get_volume_channel(dev, &volch);
  995                 if (ret != 0)
  996                         return (ret);
  997                 if (volch == NULL)
  998                         return (EINVAL);
  999         }
 1000 
 1001         /* Final validation */
 1002         if (volch != NULL) {
 1003                 CHN_LOCK(volch);
 1004                 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
 1005                         CHN_UNLOCK(volch);
 1006                         return (-1);
 1007                 }
 1008                 if (volch->direction == PCMDIR_PLAY)
 1009                         wrch = volch;
 1010                 else
 1011                         rdch = volch;
 1012         }
 1013 
 1014         ret = EINVAL;
 1015 
 1016         if (volch != NULL &&
 1017             ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
 1018             (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
 1019                 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
 1020                         int left, right, center;
 1021 
 1022                         left = *(int *)arg & 0x7f;
 1023                         right = ((*(int *)arg) >> 8) & 0x7f;
 1024                         center = (left + right) >> 1;
 1025                         chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
 1026                             center);
 1027                 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
 1028                         *(int *)arg = CHN_GETVOLUME(volch,
 1029                                 SND_VOL_C_PCM, SND_CHN_T_FL);
 1030                         *(int *)arg |= CHN_GETVOLUME(volch,
 1031                                 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
 1032                 }
 1033                 ret = 0;
 1034         } else if (rdch != NULL || wrch != NULL) {
 1035                 switch (j) {
 1036                 case SOUND_MIXER_DEVMASK:
 1037                 case SOUND_MIXER_CAPS:
 1038                 case SOUND_MIXER_STEREODEVS:
 1039                         if ((cmd & ~0xff) == MIXER_READ(0)) {
 1040                                 *(int *)arg = 0;
 1041                                 if (rdch != NULL)
 1042                                         *(int *)arg |= SOUND_MASK_RECLEV;
 1043                                 if (wrch != NULL)
 1044                                         *(int *)arg |= SOUND_MASK_PCM;
 1045                         }
 1046                         ret = 0;
 1047                         break;
 1048                 case SOUND_MIXER_RECMASK:
 1049                 case SOUND_MIXER_RECSRC:
 1050                         if ((cmd & ~0xff) == MIXER_READ(0))
 1051                                 *(int *)arg = 0;
 1052                         ret = 0;
 1053                         break;
 1054                 default:
 1055                         break;
 1056                 }
 1057         }
 1058 
 1059         if (volch != NULL)
 1060                 CHN_UNLOCK(volch);
 1061 
 1062         return (ret);
 1063 }
 1064 
 1065 static int
 1066 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
 1067     struct thread *td)
 1068 {
 1069         struct pcm_channel *chn, *rdch, *wrch;
 1070         struct snddev_info *d;
 1071         u_long xcmd;
 1072         int *arg_i, ret, tmp;
 1073 
 1074         d = dsp_get_info(i_dev);
 1075         if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
 1076                 return (EBADF);
 1077 
 1078         PCM_GIANT_ENTER(d);
 1079 
 1080         arg_i = (int *)arg;
 1081         ret = 0;
 1082         xcmd = 0;
 1083         chn = NULL;
 1084 
 1085         if (IOCGROUP(cmd) == 'M') {
 1086                 if (cmd == OSS_GETVERSION) {
 1087                         *arg_i = SOUND_VERSION;
 1088                         PCM_GIANT_EXIT(d);
 1089                         return (0);
 1090                 }
 1091                 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
 1092                 if (ret != -1) {
 1093                         PCM_GIANT_EXIT(d);
 1094                         return (ret);
 1095                 }
 1096 
 1097                 if (d->mixer_dev != NULL) {
 1098                         PCM_ACQUIRE_QUICK(d);
 1099                         ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
 1100                             MIXER_CMD_DIRECT);
 1101                         PCM_RELEASE_QUICK(d);
 1102                 } else
 1103                         ret = EBADF;
 1104 
 1105                 PCM_GIANT_EXIT(d);
 1106 
 1107                 return (ret);
 1108         }
 1109 
 1110         /*
 1111          * Certain ioctls may be made on any type of device (audio, mixer,
 1112          * and MIDI).  Handle those special cases here.
 1113          */
 1114         if (IOCGROUP(cmd) == 'X') {
 1115                 PCM_ACQUIRE_QUICK(d);
 1116                 switch(cmd) {
 1117                 case SNDCTL_SYSINFO:
 1118                         sound_oss_sysinfo((oss_sysinfo *)arg);
 1119                         break;
 1120                 case SNDCTL_CARDINFO:
 1121                         ret = sound_oss_card_info((oss_card_info *)arg);
 1122                         break;
 1123                 case SNDCTL_AUDIOINFO:
 1124                 case SNDCTL_AUDIOINFO_EX:
 1125                 case SNDCTL_ENGINEINFO:
 1126                         ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
 1127                         break;
 1128                 case SNDCTL_MIXERINFO:
 1129                         ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
 1130                         break;
 1131                 default:
 1132                         ret = EINVAL;
 1133                 }
 1134                 PCM_RELEASE_QUICK(d);
 1135                 PCM_GIANT_EXIT(d);
 1136                 return (ret);
 1137         }
 1138 
 1139         getchns(i_dev, &rdch, &wrch, 0);
 1140 
 1141         if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
 1142                 wrch = NULL;
 1143         if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
 1144                 rdch = NULL;
 1145 
 1146         if (wrch == NULL && rdch == NULL) {
 1147                 PCM_GIANT_EXIT(d);
 1148                 return (EINVAL);
 1149         }
 1150 
 1151         switch(cmd) {
 1152 #ifdef OLDPCM_IOCTL
 1153         /*
 1154          * we start with the new ioctl interface.
 1155          */
 1156         case AIONWRITE: /* how many bytes can write ? */
 1157                 if (wrch) {
 1158                         CHN_LOCK(wrch);
 1159 /*
 1160                 if (wrch && wrch->bufhard.dl)
 1161                         while (chn_wrfeed(wrch) == 0);
 1162 */
 1163                         *arg_i = sndbuf_getfree(wrch->bufsoft);
 1164                         CHN_UNLOCK(wrch);
 1165                 } else {
 1166                         *arg_i = 0;
 1167                         ret = EINVAL;
 1168                 }
 1169                 break;
 1170 
 1171         case AIOSSIZE:     /* set the current blocksize */
 1172                 {
 1173                         struct snd_size *p = (struct snd_size *)arg;
 1174 
 1175                         p->play_size = 0;
 1176                         p->rec_size = 0;
 1177                         PCM_ACQUIRE_QUICK(d);
 1178                         if (wrch) {
 1179                                 CHN_LOCK(wrch);
 1180                                 chn_setblocksize(wrch, 2, p->play_size);
 1181                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
 1182                                 CHN_UNLOCK(wrch);
 1183                         }
 1184                         if (rdch) {
 1185                                 CHN_LOCK(rdch);
 1186                                 chn_setblocksize(rdch, 2, p->rec_size);
 1187                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
 1188                                 CHN_UNLOCK(rdch);
 1189                         }
 1190                         PCM_RELEASE_QUICK(d);
 1191                 }
 1192                 break;
 1193         case AIOGSIZE:  /* get the current blocksize */
 1194                 {
 1195                         struct snd_size *p = (struct snd_size *)arg;
 1196 
 1197                         if (wrch) {
 1198                                 CHN_LOCK(wrch);
 1199                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
 1200                                 CHN_UNLOCK(wrch);
 1201                         }
 1202                         if (rdch) {
 1203                                 CHN_LOCK(rdch);
 1204                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
 1205                                 CHN_UNLOCK(rdch);
 1206                         }
 1207                 }
 1208                 break;
 1209 
 1210         case AIOSFMT:
 1211         case AIOGFMT:
 1212                 {
 1213                         snd_chan_param *p = (snd_chan_param *)arg;
 1214 
 1215                         if (cmd == AIOSFMT &&
 1216                             ((p->play_format != 0 && p->play_rate == 0) ||
 1217                             (p->rec_format != 0 && p->rec_rate == 0))) {
 1218                                 ret = EINVAL;
 1219                                 break;
 1220                         }
 1221                         PCM_ACQUIRE_QUICK(d);
 1222                         if (wrch) {
 1223                                 CHN_LOCK(wrch);
 1224                                 if (cmd == AIOSFMT && p->play_format != 0) {
 1225                                         chn_setformat(wrch,
 1226                                             SND_FORMAT(p->play_format,
 1227                                             AFMT_CHANNEL(wrch->format),
 1228                                             AFMT_EXTCHANNEL(wrch->format)));
 1229                                         chn_setspeed(wrch, p->play_rate);
 1230                                 }
 1231                                 p->play_rate = wrch->speed;
 1232                                 p->play_format = AFMT_ENCODING(wrch->format);
 1233                                 CHN_UNLOCK(wrch);
 1234                         } else {
 1235                                 p->play_rate = 0;
 1236                                 p->play_format = 0;
 1237                         }
 1238                         if (rdch) {
 1239                                 CHN_LOCK(rdch);
 1240                                 if (cmd == AIOSFMT && p->rec_format != 0) {
 1241                                         chn_setformat(rdch,
 1242                                             SND_FORMAT(p->rec_format,
 1243                                             AFMT_CHANNEL(rdch->format),
 1244                                             AFMT_EXTCHANNEL(rdch->format)));
 1245                                         chn_setspeed(rdch, p->rec_rate);
 1246                                 }
 1247                                 p->rec_rate = rdch->speed;
 1248                                 p->rec_format = AFMT_ENCODING(rdch->format);
 1249                                 CHN_UNLOCK(rdch);
 1250                         } else {
 1251                                 p->rec_rate = 0;
 1252                                 p->rec_format = 0;
 1253                         }
 1254                         PCM_RELEASE_QUICK(d);
 1255                 }
 1256                 break;
 1257 
 1258         case AIOGCAP:     /* get capabilities */
 1259                 {
 1260                         snd_capabilities *p = (snd_capabilities *)arg;
 1261                         struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
 1262                         struct cdev *pdev;
 1263 
 1264                         PCM_LOCK(d);
 1265                         if (rdch) {
 1266                                 CHN_LOCK(rdch);
 1267                                 rcaps = chn_getcaps(rdch);
 1268                         }
 1269                         if (wrch) {
 1270                                 CHN_LOCK(wrch);
 1271                                 pcaps = chn_getcaps(wrch);
 1272                         }
 1273                         p->rate_min = max(rcaps? rcaps->minspeed : 0,
 1274                                           pcaps? pcaps->minspeed : 0);
 1275                         p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
 1276                                           pcaps? pcaps->maxspeed : 1000000);
 1277                         p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
 1278                                          wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
 1279                         /* XXX bad on sb16 */
 1280                         p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
 1281                                      (wrch? chn_getformats(wrch) : 0xffffffff);
 1282                         if (rdch && wrch)
 1283                                 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
 1284                         pdev = d->mixer_dev;
 1285                         p->mixers = 1; /* default: one mixer */
 1286                         p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
 1287                         p->left = p->right = 100;
 1288                         if (wrch)
 1289                                 CHN_UNLOCK(wrch);
 1290                         if (rdch)
 1291                                 CHN_UNLOCK(rdch);
 1292                         PCM_UNLOCK(d);
 1293                 }
 1294                 break;
 1295 
 1296         case AIOSTOP:
 1297                 if (*arg_i == AIOSYNC_PLAY && wrch) {
 1298                         CHN_LOCK(wrch);
 1299                         *arg_i = chn_abort(wrch);
 1300                         CHN_UNLOCK(wrch);
 1301                 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
 1302                         CHN_LOCK(rdch);
 1303                         *arg_i = chn_abort(rdch);
 1304                         CHN_UNLOCK(rdch);
 1305                 } else {
 1306                         printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
 1307                         *arg_i = 0;
 1308                 }
 1309                 break;
 1310 
 1311         case AIOSYNC:
 1312                 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
 1313                         ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
 1314                 break;
 1315 #endif
 1316         /*
 1317          * here follow the standard ioctls (filio.h etc.)
 1318          */
 1319         case FIONREAD: /* get # bytes to read */
 1320                 if (rdch) {
 1321                         CHN_LOCK(rdch);
 1322 /*                      if (rdch && rdch->bufhard.dl)
 1323                                 while (chn_rdfeed(rdch) == 0);
 1324 */
 1325                         *arg_i = sndbuf_getready(rdch->bufsoft);
 1326                         CHN_UNLOCK(rdch);
 1327                 } else {
 1328                         *arg_i = 0;
 1329                         ret = EINVAL;
 1330                 }
 1331                 break;
 1332 
 1333         case FIOASYNC: /*set/clear async i/o */
 1334                 DEB( printf("FIOASYNC\n") ; )
 1335                 break;
 1336 
 1337         case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
 1338         case FIONBIO: /* set/clear non-blocking i/o */
 1339                 if (rdch) {
 1340                         CHN_LOCK(rdch);
 1341                         if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
 1342                                 rdch->flags |= CHN_F_NBIO;
 1343                         else
 1344                                 rdch->flags &= ~CHN_F_NBIO;
 1345                         CHN_UNLOCK(rdch);
 1346                 }
 1347                 if (wrch) {
 1348                         CHN_LOCK(wrch);
 1349                         if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
 1350                                 wrch->flags |= CHN_F_NBIO;
 1351                         else
 1352                                 wrch->flags &= ~CHN_F_NBIO;
 1353                         CHN_UNLOCK(wrch);
 1354                 }
 1355                 break;
 1356 
 1357         /*
 1358          * Finally, here is the linux-compatible ioctl interface
 1359          */
 1360 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
 1361         case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
 1362         case SNDCTL_DSP_GETBLKSIZE:
 1363                 chn = wrch ? wrch : rdch;
 1364                 if (chn) {
 1365                         CHN_LOCK(chn);
 1366                         *arg_i = sndbuf_getblksz(chn->bufsoft);
 1367                         CHN_UNLOCK(chn);
 1368                 } else {
 1369                         *arg_i = 0;
 1370                         ret = EINVAL;
 1371                 }
 1372                 break;
 1373 
 1374         case SNDCTL_DSP_SETBLKSIZE:
 1375                 RANGE(*arg_i, 16, 65536);
 1376                 PCM_ACQUIRE_QUICK(d);
 1377                 if (wrch) {
 1378                         CHN_LOCK(wrch);
 1379                         chn_setblocksize(wrch, 2, *arg_i);
 1380                         CHN_UNLOCK(wrch);
 1381                 }
 1382                 if (rdch) {
 1383                         CHN_LOCK(rdch);
 1384                         chn_setblocksize(rdch, 2, *arg_i);
 1385                         CHN_UNLOCK(rdch);
 1386                 }
 1387                 PCM_RELEASE_QUICK(d);
 1388                 break;
 1389 
 1390         case SNDCTL_DSP_RESET:
 1391                 DEB(printf("dsp reset\n"));
 1392                 if (wrch) {
 1393                         CHN_LOCK(wrch);
 1394                         chn_abort(wrch);
 1395                         chn_resetbuf(wrch);
 1396                         CHN_UNLOCK(wrch);
 1397                 }
 1398                 if (rdch) {
 1399                         CHN_LOCK(rdch);
 1400                         chn_abort(rdch);
 1401                         chn_resetbuf(rdch);
 1402                         CHN_UNLOCK(rdch);
 1403                 }
 1404                 break;
 1405 
 1406         case SNDCTL_DSP_SYNC:
 1407                 DEB(printf("dsp sync\n"));
 1408                 /* chn_sync may sleep */
 1409                 if (wrch) {
 1410                         CHN_LOCK(wrch);
 1411                         chn_sync(wrch, 0);
 1412                         CHN_UNLOCK(wrch);
 1413                 }
 1414                 break;
 1415 
 1416         case SNDCTL_DSP_SPEED:
 1417                 /* chn_setspeed may sleep */
 1418                 tmp = 0;
 1419                 PCM_ACQUIRE_QUICK(d);
 1420                 if (wrch) {
 1421                         CHN_LOCK(wrch);
 1422                         ret = chn_setspeed(wrch, *arg_i);
 1423                         tmp = wrch->speed;
 1424                         CHN_UNLOCK(wrch);
 1425                 }
 1426                 if (rdch && ret == 0) {
 1427                         CHN_LOCK(rdch);
 1428                         ret = chn_setspeed(rdch, *arg_i);
 1429                         if (tmp == 0)
 1430                                 tmp = rdch->speed;
 1431                         CHN_UNLOCK(rdch);
 1432                 }
 1433                 PCM_RELEASE_QUICK(d);
 1434                 *arg_i = tmp;
 1435                 break;
 1436 
 1437         case SOUND_PCM_READ_RATE:
 1438                 chn = wrch ? wrch : rdch;
 1439                 if (chn) {
 1440                         CHN_LOCK(chn);
 1441                         *arg_i = chn->speed;
 1442                         CHN_UNLOCK(chn);
 1443                 } else {
 1444                         *arg_i = 0;
 1445                         ret = EINVAL;
 1446                 }
 1447                 break;
 1448 
 1449         case SNDCTL_DSP_STEREO:
 1450                 tmp = -1;
 1451                 *arg_i = (*arg_i)? 2 : 1;
 1452                 PCM_ACQUIRE_QUICK(d);
 1453                 if (wrch) {
 1454                         CHN_LOCK(wrch);
 1455                         ret = chn_setformat(wrch,
 1456                             SND_FORMAT(wrch->format, *arg_i, 0));
 1457                         tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
 1458                         CHN_UNLOCK(wrch);
 1459                 }
 1460                 if (rdch && ret == 0) {
 1461                         CHN_LOCK(rdch);
 1462                         ret = chn_setformat(rdch,
 1463                             SND_FORMAT(rdch->format, *arg_i, 0));
 1464                         if (tmp == -1)
 1465                                 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
 1466                         CHN_UNLOCK(rdch);
 1467                 }
 1468                 PCM_RELEASE_QUICK(d);
 1469                 *arg_i = tmp;
 1470                 break;
 1471 
 1472         case SOUND_PCM_WRITE_CHANNELS:
 1473 /*      case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
 1474                 if (*arg_i < 0) {
 1475                         *arg_i = 0;
 1476                         ret = EINVAL;
 1477                         break;
 1478                 }
 1479                 if (*arg_i != 0) {
 1480                         struct pcmchan_matrix *m;
 1481                         uint32_t ext;
 1482 
 1483                         tmp = 0;
 1484                         if (*arg_i > SND_CHN_MAX)
 1485                                 *arg_i = SND_CHN_MAX;
 1486 
 1487                         m = feeder_matrix_default_channel_map(*arg_i);
 1488                         if (m != NULL)
 1489                                 ext = m->ext;
 1490                         else
 1491                                 ext = 0;
 1492 
 1493                         PCM_ACQUIRE_QUICK(d);
 1494                         if (wrch) {
 1495                                 CHN_LOCK(wrch);
 1496                                 ret = chn_setformat(wrch,
 1497                                     SND_FORMAT(wrch->format, *arg_i, ext));
 1498                                 tmp = AFMT_CHANNEL(wrch->format);
 1499                                 CHN_UNLOCK(wrch);
 1500                         }
 1501                         if (rdch && ret == 0) {
 1502                                 CHN_LOCK(rdch);
 1503                                 ret = chn_setformat(rdch,
 1504                                     SND_FORMAT(rdch->format, *arg_i, ext));
 1505                                 if (tmp == 0)
 1506                                         tmp = AFMT_CHANNEL(rdch->format);
 1507                                 CHN_UNLOCK(rdch);
 1508                         }
 1509                         PCM_RELEASE_QUICK(d);
 1510                         *arg_i = tmp;
 1511                 } else {
 1512                         chn = wrch ? wrch : rdch;
 1513                         CHN_LOCK(chn);
 1514                         *arg_i = AFMT_CHANNEL(chn->format);
 1515                         CHN_UNLOCK(chn);
 1516                 }
 1517                 break;
 1518 
 1519         case SOUND_PCM_READ_CHANNELS:
 1520                 chn = wrch ? wrch : rdch;
 1521                 if (chn) {
 1522                         CHN_LOCK(chn);
 1523                         *arg_i = AFMT_CHANNEL(chn->format);
 1524                         CHN_UNLOCK(chn);
 1525                 } else {
 1526                         *arg_i = 0;
 1527                         ret = EINVAL;
 1528                 }
 1529                 break;
 1530 
 1531         case SNDCTL_DSP_GETFMTS:        /* returns a mask of supported fmts */
 1532                 chn = wrch ? wrch : rdch;
 1533                 if (chn) {
 1534                         CHN_LOCK(chn);
 1535                         *arg_i = chn_getformats(chn);
 1536                         CHN_UNLOCK(chn);
 1537                 } else {
 1538                         *arg_i = 0;
 1539                         ret = EINVAL;
 1540                 }
 1541                 break;
 1542 
 1543         case SNDCTL_DSP_SETFMT: /* sets _one_ format */
 1544                 if (*arg_i != AFMT_QUERY) {
 1545                         tmp = 0;
 1546                         PCM_ACQUIRE_QUICK(d);
 1547                         if (wrch) {
 1548                                 CHN_LOCK(wrch);
 1549                                 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
 1550                                     AFMT_CHANNEL(wrch->format),
 1551                                     AFMT_EXTCHANNEL(wrch->format)));
 1552                                 tmp = wrch->format;
 1553                                 CHN_UNLOCK(wrch);
 1554                         }
 1555                         if (rdch && ret == 0) {
 1556                                 CHN_LOCK(rdch);
 1557                                 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
 1558                                     AFMT_CHANNEL(rdch->format),
 1559                                     AFMT_EXTCHANNEL(rdch->format)));
 1560                                 if (tmp == 0)
 1561                                         tmp = rdch->format;
 1562                                 CHN_UNLOCK(rdch);
 1563                         }
 1564                         PCM_RELEASE_QUICK(d);
 1565                         *arg_i = AFMT_ENCODING(tmp);
 1566                 } else {
 1567                         chn = wrch ? wrch : rdch;
 1568                         CHN_LOCK(chn);
 1569                         *arg_i = AFMT_ENCODING(chn->format);
 1570                         CHN_UNLOCK(chn);
 1571                 }
 1572                 break;
 1573 
 1574         case SNDCTL_DSP_SETFRAGMENT:
 1575                 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
 1576                 {
 1577                         uint32_t fragln = (*arg_i) & 0x0000ffff;
 1578                         uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
 1579                         uint32_t fragsz;
 1580                         uint32_t r_maxfrags, r_fragsz;
 1581 
 1582                         RANGE(fragln, 4, 16);
 1583                         fragsz = 1 << fragln;
 1584 
 1585                         if (maxfrags == 0)
 1586                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
 1587                         if (maxfrags < 2)
 1588                                 maxfrags = 2;
 1589                         if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
 1590                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
 1591 
 1592                         DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
 1593                         PCM_ACQUIRE_QUICK(d);
 1594                         if (rdch) {
 1595                                 CHN_LOCK(rdch);
 1596                                 ret = chn_setblocksize(rdch, maxfrags, fragsz);
 1597                                 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
 1598                                 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
 1599                                 CHN_UNLOCK(rdch);
 1600                         } else {
 1601                                 r_maxfrags = maxfrags;
 1602                                 r_fragsz = fragsz;
 1603                         }
 1604                         if (wrch && ret == 0) {
 1605                                 CHN_LOCK(wrch);
 1606                                 ret = chn_setblocksize(wrch, maxfrags, fragsz);
 1607                                 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
 1608                                 fragsz = sndbuf_getblksz(wrch->bufsoft);
 1609                                 CHN_UNLOCK(wrch);
 1610                         } else { /* use whatever came from the read channel */
 1611                                 maxfrags = r_maxfrags;
 1612                                 fragsz = r_fragsz;
 1613                         }
 1614                         PCM_RELEASE_QUICK(d);
 1615 
 1616                         fragln = 0;
 1617                         while (fragsz > 1) {
 1618                                 fragln++;
 1619                                 fragsz >>= 1;
 1620                         }
 1621                         *arg_i = (maxfrags << 16) | fragln;
 1622                 }
 1623                 break;
 1624 
 1625         case SNDCTL_DSP_GETISPACE:
 1626                 /* return the size of data available in the input queue */
 1627                 {
 1628                         audio_buf_info *a = (audio_buf_info *)arg;
 1629                         if (rdch) {
 1630                                 struct snd_dbuf *bs = rdch->bufsoft;
 1631 
 1632                                 CHN_LOCK(rdch);
 1633                                 a->bytes = sndbuf_getready(bs);
 1634                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
 1635                                 a->fragstotal = sndbuf_getblkcnt(bs);
 1636                                 a->fragsize = sndbuf_getblksz(bs);
 1637                                 CHN_UNLOCK(rdch);
 1638                         } else
 1639                                 ret = EINVAL;
 1640                 }
 1641                 break;
 1642 
 1643         case SNDCTL_DSP_GETOSPACE:
 1644                 /* return space available in the output queue */
 1645                 {
 1646                         audio_buf_info *a = (audio_buf_info *)arg;
 1647                         if (wrch) {
 1648                                 struct snd_dbuf *bs = wrch->bufsoft;
 1649 
 1650                                 CHN_LOCK(wrch);
 1651                                 /* XXX abusive DMA update: chn_wrupdate(wrch); */
 1652                                 a->bytes = sndbuf_getfree(bs);
 1653                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
 1654                                 a->fragstotal = sndbuf_getblkcnt(bs);
 1655                                 a->fragsize = sndbuf_getblksz(bs);
 1656                                 CHN_UNLOCK(wrch);
 1657                         } else
 1658                                 ret = EINVAL;
 1659                 }
 1660                 break;
 1661 
 1662         case SNDCTL_DSP_GETIPTR:
 1663                 {
 1664                         count_info *a = (count_info *)arg;
 1665                         if (rdch) {
 1666                                 struct snd_dbuf *bs = rdch->bufsoft;
 1667 
 1668                                 CHN_LOCK(rdch);
 1669                                 /* XXX abusive DMA update: chn_rdupdate(rdch); */
 1670                                 a->bytes = sndbuf_gettotal(bs);
 1671                                 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
 1672                                 a->ptr = sndbuf_getfreeptr(bs);
 1673                                 rdch->blocks = sndbuf_getblocks(bs);
 1674                                 CHN_UNLOCK(rdch);
 1675                         } else
 1676                                 ret = EINVAL;
 1677                 }
 1678                 break;
 1679 
 1680         case SNDCTL_DSP_GETOPTR:
 1681                 {
 1682                         count_info *a = (count_info *)arg;
 1683                         if (wrch) {
 1684                                 struct snd_dbuf *bs = wrch->bufsoft;
 1685 
 1686                                 CHN_LOCK(wrch);
 1687                                 /* XXX abusive DMA update: chn_wrupdate(wrch); */
 1688                                 a->bytes = sndbuf_gettotal(bs);
 1689                                 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
 1690                                 a->ptr = sndbuf_getreadyptr(bs);
 1691                                 wrch->blocks = sndbuf_getblocks(bs);
 1692                                 CHN_UNLOCK(wrch);
 1693                         } else
 1694                                 ret = EINVAL;
 1695                 }
 1696                 break;
 1697 
 1698         case SNDCTL_DSP_GETCAPS:
 1699                 PCM_LOCK(d);
 1700                 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
 1701                 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
 1702                         *arg_i |= PCM_CAP_DUPLEX;
 1703                 PCM_UNLOCK(d);
 1704                 break;
 1705 
 1706         case SOUND_PCM_READ_BITS:
 1707                 chn = wrch ? wrch : rdch;
 1708                 if (chn) {
 1709                         CHN_LOCK(chn);
 1710                         if (chn->format & AFMT_8BIT)
 1711                                 *arg_i = 8;
 1712                         else if (chn->format & AFMT_16BIT)
 1713                                 *arg_i = 16;
 1714                         else if (chn->format & AFMT_24BIT)
 1715                                 *arg_i = 24;
 1716                         else if (chn->format & AFMT_32BIT)
 1717                                 *arg_i = 32;
 1718                         else
 1719                                 ret = EINVAL;
 1720                         CHN_UNLOCK(chn);
 1721                 } else {
 1722                         *arg_i = 0;
 1723                         ret = EINVAL;
 1724                 }
 1725                 break;
 1726 
 1727         case SNDCTL_DSP_SETTRIGGER:
 1728                 if (rdch) {
 1729                         CHN_LOCK(rdch);
 1730                         rdch->flags &= ~CHN_F_NOTRIGGER;
 1731                         if (*arg_i & PCM_ENABLE_INPUT)
 1732                                 chn_start(rdch, 1);
 1733                         else {
 1734                                 chn_abort(rdch);
 1735                                 chn_resetbuf(rdch);
 1736                                 rdch->flags |= CHN_F_NOTRIGGER;
 1737                         }
 1738                         CHN_UNLOCK(rdch);
 1739                 }
 1740                 if (wrch) {
 1741                         CHN_LOCK(wrch);
 1742                         wrch->flags &= ~CHN_F_NOTRIGGER;
 1743                         if (*arg_i & PCM_ENABLE_OUTPUT)
 1744                                 chn_start(wrch, 1);
 1745                         else {
 1746                                 chn_abort(wrch);
 1747                                 chn_resetbuf(wrch);
 1748                                 wrch->flags |= CHN_F_NOTRIGGER;
 1749                         }
 1750                         CHN_UNLOCK(wrch);
 1751                 }
 1752                 break;
 1753 
 1754         case SNDCTL_DSP_GETTRIGGER:
 1755                 *arg_i = 0;
 1756                 if (wrch) {
 1757                         CHN_LOCK(wrch);
 1758                         if (wrch->flags & CHN_F_TRIGGERED)
 1759                                 *arg_i |= PCM_ENABLE_OUTPUT;
 1760                         CHN_UNLOCK(wrch);
 1761                 }
 1762                 if (rdch) {
 1763                         CHN_LOCK(rdch);
 1764                         if (rdch->flags & CHN_F_TRIGGERED)
 1765                                 *arg_i |= PCM_ENABLE_INPUT;
 1766                         CHN_UNLOCK(rdch);
 1767                 }
 1768                 break;
 1769 
 1770         case SNDCTL_DSP_GETODELAY:
 1771                 if (wrch) {
 1772                         struct snd_dbuf *bs = wrch->bufsoft;
 1773 
 1774                         CHN_LOCK(wrch);
 1775                         /* XXX abusive DMA update: chn_wrupdate(wrch); */
 1776                         *arg_i = sndbuf_getready(bs);
 1777                         CHN_UNLOCK(wrch);
 1778                 } else
 1779                         ret = EINVAL;
 1780                 break;
 1781 
 1782         case SNDCTL_DSP_POST:
 1783                 if (wrch) {
 1784                         CHN_LOCK(wrch);
 1785                         wrch->flags &= ~CHN_F_NOTRIGGER;
 1786                         chn_start(wrch, 1);
 1787                         CHN_UNLOCK(wrch);
 1788                 }
 1789                 break;
 1790 
 1791         case SNDCTL_DSP_SETDUPLEX:
 1792                 /*
 1793                  * switch to full-duplex mode if card is in half-duplex
 1794                  * mode and is able to work in full-duplex mode
 1795                  */
 1796                 PCM_LOCK(d);
 1797                 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
 1798                         dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
 1799                 PCM_UNLOCK(d);
 1800                 break;
 1801 
 1802         /*
 1803          * The following four ioctls are simple wrappers around mixer_ioctl
 1804          * with no further processing.  xcmd is short for "translated
 1805          * command".
 1806          */
 1807         case SNDCTL_DSP_GETRECVOL:
 1808                 if (xcmd == 0) {
 1809                         xcmd = SOUND_MIXER_READ_RECLEV;
 1810                         chn = rdch;
 1811                 }
 1812                 /* FALLTHROUGH */
 1813         case SNDCTL_DSP_SETRECVOL:
 1814                 if (xcmd == 0) {
 1815                         xcmd = SOUND_MIXER_WRITE_RECLEV;
 1816                         chn = rdch;
 1817                 }
 1818                 /* FALLTHROUGH */
 1819         case SNDCTL_DSP_GETPLAYVOL:
 1820                 if (xcmd == 0) {
 1821                         xcmd = SOUND_MIXER_READ_PCM;
 1822                         chn = wrch;
 1823                 }
 1824                 /* FALLTHROUGH */
 1825         case SNDCTL_DSP_SETPLAYVOL:
 1826                 if (xcmd == 0) {
 1827                         xcmd = SOUND_MIXER_WRITE_PCM;
 1828                         chn = wrch;
 1829                 }
 1830 
 1831                 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
 1832                 if (ret != -1) {
 1833                         PCM_GIANT_EXIT(d);
 1834                         return (ret);
 1835                 }
 1836 
 1837                 if (d->mixer_dev != NULL) {
 1838                         PCM_ACQUIRE_QUICK(d);
 1839                         ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
 1840                             MIXER_CMD_DIRECT);
 1841                         PCM_RELEASE_QUICK(d);
 1842                 } else
 1843                         ret = ENOTSUP;
 1844 
 1845                 break;
 1846 
 1847         case SNDCTL_DSP_GET_RECSRC_NAMES:
 1848         case SNDCTL_DSP_GET_RECSRC:
 1849         case SNDCTL_DSP_SET_RECSRC:
 1850                 if (d->mixer_dev != NULL) {
 1851                         PCM_ACQUIRE_QUICK(d);
 1852                         ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
 1853                             MIXER_CMD_DIRECT);
 1854                         PCM_RELEASE_QUICK(d);
 1855                 } else
 1856                         ret = ENOTSUP;
 1857                 break;
 1858 
 1859         /*
 1860          * The following 3 ioctls aren't very useful at the moment.  For
 1861          * now, only a single channel is associated with a cdev (/dev/dspN
 1862          * instance), so there's only a single output routing to use (i.e.,
 1863          * the wrch bound to this cdev).
 1864          */
 1865         case SNDCTL_DSP_GET_PLAYTGT_NAMES:
 1866                 {
 1867                         oss_mixer_enuminfo *ei;
 1868                         ei = (oss_mixer_enuminfo *)arg;
 1869                         ei->dev = 0;
 1870                         ei->ctrl = 0;
 1871                         ei->version = 0; /* static for now */
 1872                         ei->strindex[0] = 0;
 1873 
 1874                         if (wrch != NULL) {
 1875                                 ei->nvalues = 1;
 1876                                 strlcpy(ei->strings, wrch->name,
 1877                                         sizeof(ei->strings));
 1878                         } else {
 1879                                 ei->nvalues = 0;
 1880                                 ei->strings[0] = '\0';
 1881                         }
 1882                 }
 1883                 break;
 1884         case SNDCTL_DSP_GET_PLAYTGT:
 1885         case SNDCTL_DSP_SET_PLAYTGT:    /* yes, they are the same for now */
 1886                 /*
 1887                  * Re: SET_PLAYTGT
 1888                  *   OSSv4: "The value that was accepted by the device will
 1889                  *   be returned back in the variable pointed by the
 1890                  *   argument."
 1891                  */
 1892                 if (wrch != NULL)
 1893                         *arg_i = 0;
 1894                 else
 1895                         ret = EINVAL;
 1896                 break;
 1897 
 1898         case SNDCTL_DSP_SILENCE:
 1899         /*
 1900          * Flush the software (pre-feed) buffer, but try to minimize playback
 1901          * interruption.  (I.e., record unplayed samples with intent to
 1902          * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
 1903          * functionality.
 1904          */
 1905                 if (wrch == NULL)
 1906                         ret = EINVAL;
 1907                 else {
 1908                         struct snd_dbuf *bs;
 1909                         CHN_LOCK(wrch);
 1910                         while (wrch->inprog != 0)
 1911                                 cv_wait(&wrch->cv, wrch->lock);
 1912                         bs = wrch->bufsoft;
 1913                         if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
 1914                                 bs->sl = sndbuf_getready(bs);
 1915                                 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
 1916                                 sndbuf_fillsilence(bs);
 1917                                 chn_start(wrch, 0);
 1918                         }
 1919                         CHN_UNLOCK(wrch);
 1920                 }
 1921                 break;
 1922 
 1923         case SNDCTL_DSP_SKIP:
 1924         /*
 1925          * OSSv4 docs: "This ioctl call discards all unplayed samples in the
 1926          * playback buffer by moving the current write position immediately
 1927          * before the point where the device is currently reading the samples."
 1928          */
 1929                 if (wrch == NULL)
 1930                         ret = EINVAL;
 1931                 else {
 1932                         struct snd_dbuf *bs;
 1933                         CHN_LOCK(wrch);
 1934                         while (wrch->inprog != 0)
 1935                                 cv_wait(&wrch->cv, wrch->lock);
 1936                         bs = wrch->bufsoft;
 1937                         if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
 1938                                 sndbuf_softreset(bs);
 1939                                 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
 1940                                 bs->sl = 0;
 1941                                 chn_start(wrch, 0);
 1942                         }
 1943                         CHN_UNLOCK(wrch);
 1944                 }
 1945                 break;
 1946 
 1947         case SNDCTL_DSP_CURRENT_OPTR:
 1948         case SNDCTL_DSP_CURRENT_IPTR:
 1949         /**
 1950          * @note Changing formats resets the buffer counters, which differs
 1951          *       from the 4Front drivers.  However, I don't expect this to be
 1952          *       much of a problem.
 1953          *
 1954          * @note In a test where @c CURRENT_OPTR is called immediately after write
 1955          *       returns, this driver is about 32K samples behind whereas
 1956          *       4Front's is about 8K samples behind.  Should determine source
 1957          *       of discrepancy, even if only out of curiosity.
 1958          *
 1959          * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
 1960          */
 1961                 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
 1962                 if (chn == NULL) 
 1963                         ret = EINVAL;
 1964                 else {
 1965                         struct snd_dbuf *bs;
 1966                         /* int tmp; */
 1967 
 1968                         oss_count_t *oc = (oss_count_t *)arg;
 1969 
 1970                         CHN_LOCK(chn);
 1971                         bs = chn->bufsoft;
 1972 #if 0
 1973                         tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
 1974                         oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
 1975                         oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
 1976 #else
 1977                         oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
 1978                         oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
 1979 #endif
 1980                         CHN_UNLOCK(chn);
 1981                 }
 1982                 break;
 1983 
 1984         case SNDCTL_DSP_HALT_OUTPUT:
 1985         case SNDCTL_DSP_HALT_INPUT:
 1986                 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
 1987                 if (chn == NULL)
 1988                         ret = EINVAL;
 1989                 else {
 1990                         CHN_LOCK(chn);
 1991                         chn_abort(chn);
 1992                         CHN_UNLOCK(chn);
 1993                 }
 1994                 break;
 1995 
 1996         case SNDCTL_DSP_LOW_WATER:
 1997         /*
 1998          * Set the number of bytes required to attract attention by
 1999          * select/poll.
 2000          */
 2001                 if (wrch != NULL) {
 2002                         CHN_LOCK(wrch);
 2003                         wrch->lw = (*arg_i > 1) ? *arg_i : 1;
 2004                         CHN_UNLOCK(wrch);
 2005                 }
 2006                 if (rdch != NULL) {
 2007                         CHN_LOCK(rdch);
 2008                         rdch->lw = (*arg_i > 1) ? *arg_i : 1;
 2009                         CHN_UNLOCK(rdch);
 2010                 }
 2011                 break;
 2012 
 2013         case SNDCTL_DSP_GETERROR:
 2014         /*
 2015          * OSSv4 docs:  "All errors and counters will automatically be
 2016          * cleared to zeroes after the call so each call will return only
 2017          * the errors that occurred after the previous invocation. ... The
 2018          * play_underruns and rec_overrun fields are the only usefull fields
 2019          * returned by OSS 4.0."
 2020          */
 2021                 {
 2022                         audio_errinfo *ei = (audio_errinfo *)arg;
 2023 
 2024                         bzero((void *)ei, sizeof(*ei));
 2025 
 2026                         if (wrch != NULL) {
 2027                                 CHN_LOCK(wrch);
 2028                                 ei->play_underruns = wrch->xruns;
 2029                                 wrch->xruns = 0;
 2030                                 CHN_UNLOCK(wrch);
 2031                         }
 2032                         if (rdch != NULL) {
 2033                                 CHN_LOCK(rdch);
 2034                                 ei->rec_overruns = rdch->xruns;
 2035                                 rdch->xruns = 0;
 2036                                 CHN_UNLOCK(rdch);
 2037                         }
 2038                 }
 2039                 break;
 2040 
 2041         case SNDCTL_DSP_SYNCGROUP:
 2042                 PCM_ACQUIRE_QUICK(d);
 2043                 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
 2044                 PCM_RELEASE_QUICK(d);
 2045                 break;
 2046 
 2047         case SNDCTL_DSP_SYNCSTART:
 2048                 PCM_ACQUIRE_QUICK(d);
 2049                 ret = dsp_oss_syncstart(*arg_i);
 2050                 PCM_RELEASE_QUICK(d);
 2051                 break;
 2052 
 2053         case SNDCTL_DSP_POLICY:
 2054                 PCM_ACQUIRE_QUICK(d);
 2055                 ret = dsp_oss_policy(wrch, rdch, *arg_i);
 2056                 PCM_RELEASE_QUICK(d);
 2057                 break;
 2058 
 2059         case SNDCTL_DSP_COOKEDMODE:
 2060                 PCM_ACQUIRE_QUICK(d);
 2061                 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
 2062                         ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
 2063                 PCM_RELEASE_QUICK(d);
 2064                 break;
 2065         case SNDCTL_DSP_GET_CHNORDER:
 2066                 PCM_ACQUIRE_QUICK(d);
 2067                 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
 2068                 PCM_RELEASE_QUICK(d);
 2069                 break;
 2070         case SNDCTL_DSP_SET_CHNORDER:
 2071                 PCM_ACQUIRE_QUICK(d);
 2072                 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
 2073                 PCM_RELEASE_QUICK(d);
 2074                 break;
 2075         case SNDCTL_DSP_GETCHANNELMASK:         /* XXX vlc */
 2076                 PCM_ACQUIRE_QUICK(d);
 2077                 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
 2078                 PCM_RELEASE_QUICK(d);
 2079                 break;
 2080         case SNDCTL_DSP_BIND_CHANNEL:           /* XXX what?!? */
 2081                 ret = EINVAL;
 2082                 break;
 2083 #ifdef  OSSV4_EXPERIMENT
 2084         /*
 2085          * XXX The following ioctls are not yet supported and just return
 2086          * EINVAL.
 2087          */
 2088         case SNDCTL_DSP_GETOPEAKS:
 2089         case SNDCTL_DSP_GETIPEAKS:
 2090                 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
 2091                 if (chn == NULL)
 2092                         ret = EINVAL;
 2093                 else {
 2094                         oss_peaks_t *op = (oss_peaks_t *)arg;
 2095                         int lpeak, rpeak;
 2096 
 2097                         CHN_LOCK(chn);
 2098                         ret = chn_getpeaks(chn, &lpeak, &rpeak);
 2099                         if (ret == -1)
 2100                                 ret = EINVAL;
 2101                         else {
 2102                                 (*op)[0] = lpeak;
 2103                                 (*op)[1] = rpeak;
 2104                         }
 2105                         CHN_UNLOCK(chn);
 2106                 }
 2107                 break;
 2108 
 2109         /*
 2110          * XXX Once implemented, revisit this for proper cv protection
 2111          *     (if necessary).
 2112          */
 2113         case SNDCTL_GETLABEL:
 2114                 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
 2115                 break;
 2116         case SNDCTL_SETLABEL:
 2117                 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
 2118                 break;
 2119         case SNDCTL_GETSONG:
 2120                 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
 2121                 break;
 2122         case SNDCTL_SETSONG:
 2123                 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
 2124                 break;
 2125         case SNDCTL_SETNAME:
 2126                 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
 2127                 break;
 2128 #if 0
 2129         /**
 2130          * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
 2131          * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
 2132          * 4Front Technologies.
 2133          */
 2134         case SNDCTL_DSP_READCTL:
 2135         case SNDCTL_DSP_WRITECTL:
 2136                 ret = EINVAL;
 2137                 break;
 2138 #endif  /* !0 (explicitly omitted ioctls) */
 2139 
 2140 #endif  /* !OSSV4_EXPERIMENT */
 2141         case SNDCTL_DSP_MAPINBUF:
 2142         case SNDCTL_DSP_MAPOUTBUF:
 2143         case SNDCTL_DSP_SETSYNCRO:
 2144                 /* undocumented */
 2145 
 2146         case SNDCTL_DSP_SUBDIVIDE:
 2147         case SOUND_PCM_WRITE_FILTER:
 2148         case SOUND_PCM_READ_FILTER:
 2149                 /* dunno what these do, don't sound important */
 2150 
 2151         default:
 2152                 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
 2153                 ret = EINVAL;
 2154                 break;
 2155         }
 2156 
 2157         PCM_GIANT_LEAVE(d);
 2158 
 2159         return (ret);
 2160 }
 2161 
 2162 static int
 2163 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
 2164 {
 2165         struct snddev_info *d;
 2166         struct pcm_channel *wrch, *rdch;
 2167         int ret, e;
 2168 
 2169         d = dsp_get_info(i_dev);
 2170         if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev)) {
 2171                 /* XXX many clients don't understand POLLNVAL */
 2172                 return (events & (POLLHUP | POLLPRI | POLLIN |
 2173                     POLLRDNORM | POLLOUT | POLLWRNORM));
 2174         }
 2175         PCM_GIANT_ENTER(d);
 2176 
 2177         wrch = NULL;
 2178         rdch = NULL;
 2179         ret = 0;
 2180 
 2181         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
 2182 
 2183         if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
 2184                 e = (events & (POLLOUT | POLLWRNORM));
 2185                 if (e)
 2186                         ret |= chn_poll(wrch, e, td);
 2187         }
 2188 
 2189         if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
 2190                 e = (events & (POLLIN | POLLRDNORM));
 2191                 if (e)
 2192                         ret |= chn_poll(rdch, e, td);
 2193         }
 2194 
 2195         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
 2196 
 2197         PCM_GIANT_LEAVE(d);
 2198 
 2199         return (ret);
 2200 }
 2201 
 2202 static int
 2203 dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
 2204     int nprot, vm_memattr_t *memattr)
 2205 {
 2206 
 2207         /* XXX memattr is not honored */
 2208         *paddr = vtophys(offset);
 2209         return (0);
 2210 }
 2211 
 2212 static int
 2213 dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
 2214     vm_size_t size, struct vm_object **object, int nprot)
 2215 {
 2216         struct snddev_info *d;
 2217         struct pcm_channel *wrch, *rdch, *c;
 2218 
 2219         /*
 2220          * Reject PROT_EXEC by default. It just doesn't makes sense.
 2221          * Unfortunately, we have to give up this one due to linux_mmap
 2222          * changes.
 2223          *
 2224          * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
 2225          *
 2226          */
 2227 #ifdef SV_ABI_LINUX
 2228         if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
 2229             (dsp_mmap_allow_prot_exec == 0 &&
 2230             SV_CURPROC_ABI() != SV_ABI_LINUX)))
 2231 #else
 2232         if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
 2233 #endif
 2234                 return (EINVAL);
 2235 
 2236         /*
 2237          * PROT_READ (alone) selects the input buffer.
 2238          * PROT_WRITE (alone) selects the output buffer.
 2239          * PROT_WRITE|PROT_READ together select the output buffer.
 2240          */
 2241         if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
 2242                 return (EINVAL);
 2243 
 2244         d = dsp_get_info(i_dev);
 2245         if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
 2246                 return (EINVAL);
 2247 
 2248         PCM_GIANT_ENTER(d);
 2249 
 2250         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
 2251 
 2252         c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
 2253         if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
 2254             (*offset  + size) > sndbuf_getsize(c->bufsoft) ||
 2255             (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
 2256             (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
 2257                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
 2258                 PCM_GIANT_EXIT(d);
 2259                 return (EINVAL);
 2260         }
 2261 
 2262         if (wrch != NULL)
 2263                 wrch->flags |= CHN_F_MMAP;
 2264         if (rdch != NULL)
 2265                 rdch->flags |= CHN_F_MMAP;
 2266 
 2267         *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
 2268         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
 2269         *object = vm_pager_allocate(OBJT_DEVICE, i_dev,
 2270             size, nprot, *offset, curthread->td_ucred);
 2271 
 2272         PCM_GIANT_LEAVE(d);
 2273 
 2274         if (*object == NULL)
 2275                  return (EINVAL);
 2276         return (0);
 2277 }
 2278 
 2279 /* So much for dev_stdclone() */
 2280 static int
 2281 dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
 2282 {
 2283         size_t len;
 2284 
 2285         len = strlen(namep);
 2286 
 2287         if (bcmp(name, namep, len) != 0)
 2288                 return (ENODEV);
 2289 
 2290         name += len;
 2291 
 2292         if (isdigit(*name) == 0)
 2293                 return (ENODEV);
 2294 
 2295         len = strlen(sep);
 2296 
 2297         if (*name == '' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
 2298                 return (ENODEV);
 2299 
 2300         for (*u = 0; isdigit(*name) != 0; name++) {
 2301                 *u *= 10;
 2302                 *u += *name - '';
 2303                 if (*u > dsp_umax)
 2304                         return (ENODEV);
 2305         }
 2306 
 2307         if (*name == '\0')
 2308                 return ((use_sep == 0) ? 0 : ENODEV);
 2309 
 2310         if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
 2311                 return (ENODEV);
 2312 
 2313         name += len;
 2314 
 2315         if (*name == '' && name[1] != '\0')
 2316                 return (ENODEV);
 2317 
 2318         for (*c = 0; isdigit(*name) != 0; name++) {
 2319                 *c *= 10;
 2320                 *c += *name - '';
 2321                 if (*c > dsp_cmax)
 2322                         return (ENODEV);
 2323         }
 2324 
 2325         if (*name != '\0')
 2326                 return (ENODEV);
 2327 
 2328         return (0);
 2329 }
 2330 
 2331 static void
 2332 dsp_clone(void *arg,
 2333 #if __FreeBSD_version >= 600034
 2334     struct ucred *cred,
 2335 #endif
 2336     char *name, int namelen, struct cdev **dev)
 2337 {
 2338         struct snddev_info *d;
 2339         struct snd_clone_entry *ce;
 2340         struct pcm_channel *c;
 2341         int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
 2342         char *devname, *devcmp, *devsep;
 2343 
 2344         KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
 2345 
 2346         if (*dev != NULL)
 2347                 return;
 2348 
 2349         unit = -1;
 2350         cunit = -1;
 2351         devtype = -1;
 2352         devhw = 0;
 2353         devcmax = -1;
 2354         tumax = -1;
 2355         devname = NULL;
 2356         devsep = NULL;
 2357 
 2358         for (i = 0; unit == -1 &&
 2359             i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
 2360                 devtype = dsp_cdevs[i].type;
 2361                 devcmp = dsp_cdevs[i].name;
 2362                 devsep = dsp_cdevs[i].sep;
 2363                 devname = dsp_cdevs[i].alias;
 2364                 if (devname == NULL)
 2365                         devname = devcmp;
 2366                 devhw = dsp_cdevs[i].hw;
 2367                 devcmax = dsp_cdevs[i].max - 1;
 2368                 if (strcmp(name, devcmp) == 0) {
 2369                         if (dsp_basename_clone != 0)
 2370                                 unit = snd_unit;
 2371                 } else if (dsp_stdclone(name, devcmp, devsep,
 2372                     dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
 2373                         unit = -1;
 2374                         cunit = -1;
 2375                 }
 2376         }
 2377 
 2378         d = devclass_get_softc(pcm_devclass, unit);
 2379         if (!PCM_REGISTERED(d) || d->clones == NULL)
 2380                 return;
 2381 
 2382         /* XXX Need Giant magic entry ??? */
 2383 
 2384         PCM_LOCK(d);
 2385         if (snd_clone_disabled(d->clones)) {
 2386                 PCM_UNLOCK(d);
 2387                 return;
 2388         }
 2389 
 2390         PCM_WAIT(d);
 2391         PCM_ACQUIRE(d);
 2392         PCM_UNLOCK(d);
 2393 
 2394         udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
 2395 
 2396         if (devhw != 0) {
 2397                 KASSERT(devcmax <= dsp_cmax,
 2398                     ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
 2399                 if (cunit > devcmax) {
 2400                         PCM_RELEASE_QUICK(d);
 2401                         return;
 2402                 }
 2403                 udcmask |= snd_c2unit(cunit);
 2404                 CHN_FOREACH(c, d, channels.pcm) {
 2405                         CHN_LOCK(c);
 2406                         if (c->unit != udcmask) {
 2407                                 CHN_UNLOCK(c);
 2408                                 continue;
 2409                         }
 2410                         CHN_UNLOCK(c);
 2411                         udcmask &= ~snd_c2unit(cunit);
 2412                         /*
 2413                          * Temporarily increase clone maxunit to overcome
 2414                          * vchan flexibility.
 2415                          *
 2416                          * # sysctl dev.pcm.0.play.vchans=256
 2417                          * dev.pcm.0.play.vchans: 1 -> 256
 2418                          * # cat /dev/zero > /dev/dsp0.vp255 &
 2419                          * [1] 17296
 2420                          * # sysctl dev.pcm.0.play.vchans=0
 2421                          * dev.pcm.0.play.vchans: 256 -> 1
 2422                          * # fg
 2423                          * [1]  + running    cat /dev/zero > /dev/dsp0.vp255
 2424                          * ^C
 2425                          * # cat /dev/zero > /dev/dsp0.vp255
 2426                          * zsh: operation not supported: /dev/dsp0.vp255
 2427                          */
 2428                         tumax = snd_clone_getmaxunit(d->clones);
 2429                         if (cunit > tumax)
 2430                                 snd_clone_setmaxunit(d->clones, cunit);
 2431                         else
 2432                                 tumax = -1;
 2433                         goto dsp_clone_alloc;
 2434                 }
 2435                 /*
 2436                  * Ok, so we're requesting unallocated vchan, but still
 2437                  * within maximum vchan limit.
 2438                  */
 2439                 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
 2440                     (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
 2441                     cunit < snd_maxautovchans) {
 2442                         udcmask &= ~snd_c2unit(cunit);
 2443                         tumax = snd_clone_getmaxunit(d->clones);
 2444                         if (cunit > tumax)
 2445                                 snd_clone_setmaxunit(d->clones, cunit);
 2446                         else
 2447                                 tumax = -1;
 2448                         goto dsp_clone_alloc;
 2449                 }
 2450                 PCM_RELEASE_QUICK(d);
 2451                 return;
 2452         }
 2453 
 2454 dsp_clone_alloc:
 2455         ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
 2456         if (tumax != -1)
 2457                 snd_clone_setmaxunit(d->clones, tumax);
 2458         if (ce != NULL) {
 2459                 udcmask |= snd_c2unit(cunit);
 2460                 *dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
 2461                     UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
 2462                     devname, unit, devsep, cunit);
 2463                 snd_clone_register(ce, *dev);
 2464         }
 2465 
 2466         PCM_RELEASE_QUICK(d);
 2467 
 2468         if (*dev != NULL)
 2469                 dev_ref(*dev);
 2470 }
 2471 
 2472 static void
 2473 dsp_sysinit(void *p)
 2474 {
 2475         if (dsp_ehtag != NULL)
 2476                 return;
 2477         /* initialize unit numbering */
 2478         snd_unit_init();
 2479         dsp_umax = PCMMAXUNIT;
 2480         dsp_cmax = PCMMAXCHAN;
 2481         dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
 2482 }
 2483 
 2484 static void
 2485 dsp_sysuninit(void *p)
 2486 {
 2487         if (dsp_ehtag == NULL)
 2488                 return;
 2489         EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
 2490         dsp_ehtag = NULL;
 2491 }
 2492 
 2493 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
 2494 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
 2495 
 2496 char *
 2497 dsp_unit2name(char *buf, size_t len, int unit)
 2498 {
 2499         int i, dtype;
 2500 
 2501         KASSERT(buf != NULL && len != 0,
 2502             ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
 2503 
 2504         dtype = snd_unit2d(unit);
 2505 
 2506         for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
 2507                 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
 2508                         continue;
 2509                 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
 2510                     snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
 2511                 return (buf);
 2512         }
 2513 
 2514         return (NULL);
 2515 }
 2516 
 2517 /**
 2518  * @brief Handler for SNDCTL_AUDIOINFO.
 2519  *
 2520  * Gathers information about the audio device specified in ai->dev.  If
 2521  * ai->dev == -1, then this function gathers information about the current
 2522  * device.  If the call comes in on a non-audio device and ai->dev == -1,
 2523  * return EINVAL.
 2524  *
 2525  * This routine is supposed to go practically straight to the hardware,
 2526  * getting capabilities directly from the sound card driver, side-stepping
 2527  * the intermediate channel interface.
 2528  *
 2529  * Note, however, that the usefulness of this command is significantly
 2530  * decreased when requesting info about any device other than the one serving
 2531  * the request. While each snddev_channel refers to a specific device node,
 2532  * the converse is *not* true.  Currently, when a sound device node is opened,
 2533  * the sound subsystem scans for an available audio channel (or channels, if
 2534  * opened in read+write) and then assigns them to the si_drv[12] private
 2535  * data fields.  As a result, any information returned linking a channel to
 2536  * a specific character device isn't necessarily accurate.
 2537  *
 2538  * @note
 2539  * Calling threads must not hold any snddev_info or pcm_channel locks.
 2540  * 
 2541  * @param dev           device on which the ioctl was issued
 2542  * @param ai            ioctl request data container
 2543  *
 2544  * @retval 0            success
 2545  * @retval EINVAL       ai->dev specifies an invalid device
 2546  *
 2547  * @todo Verify correctness of Doxygen tags.  ;)
 2548  */
 2549 int
 2550 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
 2551 {
 2552         struct pcmchan_caps *caps;
 2553         struct pcm_channel *ch;
 2554         struct snddev_info *d;
 2555         uint32_t fmts;
 2556         int i, nchan, *rates, minch, maxch;
 2557         char *devname, buf[CHN_NAMELEN];
 2558 
 2559         /*
 2560          * If probing the device that received the ioctl, make sure it's a
 2561          * DSP device.  (Users may use this ioctl with /dev/mixer and
 2562          * /dev/midi.)
 2563          */
 2564         if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
 2565                 return (EINVAL);
 2566 
 2567         ch = NULL;
 2568         devname = NULL;
 2569         nchan = 0;
 2570         bzero(buf, sizeof(buf));
 2571 
 2572         /*
 2573          * Search for the requested audio device (channel).  Start by
 2574          * iterating over pcm devices.
 2575          */ 
 2576         for (i = 0; pcm_devclass != NULL &&
 2577             i < devclass_get_maxunit(pcm_devclass); i++) {
 2578                 d = devclass_get_softc(pcm_devclass, i);
 2579                 if (!PCM_REGISTERED(d))
 2580                         continue;
 2581 
 2582                 /* XXX Need Giant magic entry ??? */
 2583 
 2584                 /* See the note in function docblock */
 2585                 PCM_UNLOCKASSERT(d);
 2586                 PCM_LOCK(d);
 2587 
 2588                 CHN_FOREACH(ch, d, channels.pcm) {
 2589                         CHN_UNLOCKASSERT(ch);
 2590                         CHN_LOCK(ch);
 2591                         if (ai->dev == -1) {
 2592                                 if (DSP_REGISTERED(d, i_dev) &&
 2593                                     (ch == PCM_RDCH(i_dev) ||   /* record ch */
 2594                                     ch == PCM_WRCH(i_dev))) {   /* playback ch */
 2595                                         devname = dsp_unit2name(buf,
 2596                                             sizeof(buf), ch->unit);
 2597                                 }
 2598                         } else if (ai->dev == nchan) {
 2599                                 devname = dsp_unit2name(buf, sizeof(buf),
 2600                                     ch->unit);
 2601                         }
 2602                         if (devname != NULL)
 2603                                 break;
 2604                         CHN_UNLOCK(ch);
 2605                         ++nchan;
 2606                 }
 2607 
 2608                 if (devname != NULL) {
 2609                         /*
 2610                          * At this point, the following synchronization stuff
 2611                          * has happened:
 2612                          * - a specific PCM device is locked.
 2613                          * - a specific audio channel has been locked, so be
 2614                          *   sure to unlock when exiting;
 2615                          */
 2616 
 2617                         caps = chn_getcaps(ch);
 2618 
 2619                         /*
 2620                          * With all handles collected, zero out the user's
 2621                          * container and begin filling in its fields.
 2622                          */
 2623                         bzero((void *)ai, sizeof(oss_audioinfo));
 2624 
 2625                         ai->dev = nchan;
 2626                         strlcpy(ai->name, ch->name,  sizeof(ai->name));
 2627 
 2628                         if ((ch->flags & CHN_F_BUSY) == 0)
 2629                                 ai->busy = 0;
 2630                         else
 2631                                 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
 2632 
 2633                         /**
 2634                          * @note
 2635                          * @c cmd - OSSv4 docs: "Only supported under Linux at
 2636                          *    this moment." Cop-out, I know, but I'll save
 2637                          *    running around in the process table for later.
 2638                          *    Is there a risk of leaking information?
 2639                          */
 2640                         ai->pid = ch->pid;
 2641 
 2642                         /*
 2643                          * These flags stolen from SNDCTL_DSP_GETCAPS handler.
 2644                          * Note, however, that a single channel operates in
 2645                          * only one direction, so PCM_CAP_DUPLEX is out.
 2646                          */
 2647                         /**
 2648                          * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
 2649                          *       these in pcmchan::caps?
 2650                          */
 2651                         ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
 2652                             ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
 2653 
 2654                         /*
 2655                          * Collect formats supported @b natively by the
 2656                          * device.  Also determine min/max channels.  (I.e.,
 2657                          * mono, stereo, or both?)
 2658                          *
 2659                          * If any channel is stereo, maxch = 2;
 2660                          * if all channels are stereo, minch = 2, too;
 2661                          * if any channel is mono, minch = 1;
 2662                          * and if all channels are mono, maxch = 1.
 2663                          */
 2664                         minch = 0;
 2665                         maxch = 0;
 2666                         fmts = 0;
 2667                         for (i = 0; caps->fmtlist[i]; i++) {
 2668                                 fmts |= caps->fmtlist[i];
 2669                                 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
 2670                                         minch = (minch == 0) ? 2 : minch;
 2671                                         maxch = 2;
 2672                                 } else {
 2673                                         minch = 1;
 2674                                         maxch = (maxch == 0) ? 1 : maxch;
 2675                                 }
 2676                         }
 2677 
 2678                         if (ch->direction == PCMDIR_PLAY)
 2679                                 ai->oformats = fmts;
 2680                         else
 2681                                 ai->iformats = fmts;
 2682 
 2683                         /**
 2684                          * @note
 2685                          * @c magic - OSSv4 docs: "Reserved for internal use
 2686                          *    by OSS."
 2687                          *
 2688                          * @par
 2689                          * @c card_number - OSSv4 docs: "Number of the sound
 2690                          *    card where this device belongs or -1 if this
 2691                          *    information is not available.  Applications
 2692                          *    should normally not use this field for any
 2693                          *    purpose."
 2694                          */
 2695                         ai->card_number = -1;
 2696                         /**
 2697                          * @todo @c song_name - depends first on
 2698                          *          SNDCTL_[GS]ETSONG @todo @c label - depends
 2699                          *          on SNDCTL_[GS]ETLABEL
 2700                          * @todo @c port_number - routing information?
 2701                          */
 2702                         ai->port_number = -1;
 2703                         ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
 2704                         /**
 2705                          * @note
 2706                          * @c real_device - OSSv4 docs:  "Obsolete."
 2707                          */
 2708                         ai->real_device = -1;
 2709                         strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
 2710                         strlcat(ai->devnode, devname, sizeof(ai->devnode));
 2711                         ai->enabled = device_is_attached(d->dev) ? 1 : 0;
 2712                         /**
 2713                          * @note
 2714                          * @c flags - OSSv4 docs: "Reserved for future use."
 2715                          *
 2716                          * @note
 2717                          * @c binding - OSSv4 docs: "Reserved for future use."
 2718                          *
 2719                          * @todo @c handle - haven't decided how to generate
 2720                          *       this yet; bus, vendor, device IDs?
 2721                          */
 2722                         ai->min_rate = caps->minspeed;
 2723                         ai->max_rate = caps->maxspeed;
 2724 
 2725                         ai->min_channels = minch;
 2726                         ai->max_channels = maxch;
 2727 
 2728                         ai->nrates = chn_getrates(ch, &rates);
 2729                         if (ai->nrates > OSS_MAX_SAMPLE_RATES)
 2730                                 ai->nrates = OSS_MAX_SAMPLE_RATES;
 2731 
 2732                         for (i = 0; i < ai->nrates; i++)
 2733                                 ai->rates[i] = rates[i];
 2734                         
 2735                         ai->next_play_engine = 0;
 2736                         ai->next_rec_engine = 0;
 2737 
 2738                         CHN_UNLOCK(ch);
 2739                 }
 2740 
 2741                 PCM_UNLOCK(d);
 2742 
 2743                 if (devname != NULL)
 2744                         return (0);
 2745         }
 2746 
 2747         /* Exhausted the search -- nothing is locked, so return. */
 2748         return (EINVAL);
 2749 }
 2750 
 2751 /**
 2752  * @brief Assigns a PCM channel to a sync group.
 2753  *
 2754  * Sync groups are used to enable audio operations on multiple devices
 2755  * simultaneously.  They may be used with any number of devices and may
 2756  * span across applications.  Devices are added to groups with
 2757  * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
 2758  * SNDCTL_DSP_SYNCSTART ioctl.
 2759  *
 2760  * If the @c id field of the @c group parameter is set to zero, then a new
 2761  * sync group is created.  Otherwise, wrch and rdch (if set) are added to
 2762  * the group specified.
 2763  *
 2764  * @todo As far as memory allocation, should we assume that things are
 2765  *       okay and allocate with M_WAITOK before acquiring channel locks,
 2766  *       freeing later if not?
 2767  *
 2768  * @param wrch  output channel associated w/ device (if any)
 2769  * @param rdch  input channel associated w/ device (if any)
 2770  * @param group Sync group parameters
 2771  *
 2772  * @retval 0            success
 2773  * @retval non-zero     error to be propagated upstream
 2774  */
 2775 static int
 2776 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
 2777 {
 2778         struct pcmchan_syncmember *smrd, *smwr;
 2779         struct pcmchan_syncgroup *sg;
 2780         int ret, sg_ids[3];
 2781 
 2782         smrd = NULL;
 2783         smwr = NULL;
 2784         sg = NULL;
 2785         ret = 0;
 2786 
 2787         /*
 2788          * Free_unr() may sleep, so store released syncgroup IDs until after
 2789          * all locks are released.
 2790          */
 2791         sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
 2792 
 2793         PCM_SG_LOCK();
 2794 
 2795         /*
 2796          * - Insert channel(s) into group's member list.
 2797          * - Set CHN_F_NOTRIGGER on channel(s).
 2798          * - Stop channel(s).  
 2799          */
 2800 
 2801         /*
 2802          * If device's channels are already mapped to a group, unmap them.
 2803          */
 2804         if (wrch) {
 2805                 CHN_LOCK(wrch);
 2806                 sg_ids[0] = chn_syncdestroy(wrch);
 2807         }
 2808 
 2809         if (rdch) {
 2810                 CHN_LOCK(rdch);
 2811                 sg_ids[1] = chn_syncdestroy(rdch);
 2812         }
 2813 
 2814         /*
 2815          * Verify that mode matches character device properites.
 2816          *  - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
 2817          *  - Bail if PCM_ENABLE_INPUT && rdch == NULL.
 2818          */
 2819         if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
 2820             ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
 2821                 ret = EINVAL;
 2822                 goto out;
 2823         }
 2824 
 2825         /*
 2826          * An id of zero indicates the user wants to create a new
 2827          * syncgroup.
 2828          */
 2829         if (group->id == 0) {
 2830                 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
 2831                 if (sg != NULL) {
 2832                         SLIST_INIT(&sg->members);
 2833                         sg->id = alloc_unr(pcmsg_unrhdr);
 2834 
 2835                         group->id = sg->id;
 2836                         SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
 2837                 } else
 2838                         ret = ENOMEM;
 2839         } else {
 2840                 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
 2841                         if (sg->id == group->id)
 2842                                 break;
 2843                 }
 2844                 if (sg == NULL)
 2845                         ret = EINVAL;
 2846         }
 2847 
 2848         /* Couldn't create or find a syncgroup.  Fail. */
 2849         if (sg == NULL)
 2850                 goto out;
 2851 
 2852         /*
 2853          * Allocate a syncmember, assign it and a channel together, and
 2854          * insert into syncgroup.
 2855          */
 2856         if (group->mode & PCM_ENABLE_INPUT) {
 2857                 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
 2858                 if (smrd == NULL) {
 2859                         ret = ENOMEM;
 2860                         goto out;
 2861                 }
 2862 
 2863                 SLIST_INSERT_HEAD(&sg->members, smrd, link);
 2864                 smrd->parent = sg;
 2865                 smrd->ch = rdch;
 2866 
 2867                 chn_abort(rdch);
 2868                 rdch->flags |= CHN_F_NOTRIGGER;
 2869                 rdch->sm = smrd;
 2870         }
 2871 
 2872         if (group->mode & PCM_ENABLE_OUTPUT) {
 2873                 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
 2874                 if (smwr == NULL) {
 2875                         ret = ENOMEM;
 2876                         goto out;
 2877                 }
 2878 
 2879                 SLIST_INSERT_HEAD(&sg->members, smwr, link);
 2880                 smwr->parent = sg;
 2881                 smwr->ch = wrch;
 2882 
 2883                 chn_abort(wrch);
 2884                 wrch->flags |= CHN_F_NOTRIGGER;
 2885                 wrch->sm = smwr;
 2886         }
 2887 
 2888 
 2889 out:
 2890         if (ret != 0) {
 2891                 if (smrd != NULL)
 2892                         free(smrd, M_DEVBUF);
 2893                 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
 2894                         sg_ids[2] = sg->id;
 2895                         SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
 2896                         free(sg, M_DEVBUF);
 2897                 }
 2898 
 2899                 if (wrch)
 2900                         wrch->sm = NULL;
 2901                 if (rdch)
 2902                         rdch->sm = NULL;
 2903         }
 2904 
 2905         if (wrch)
 2906                 CHN_UNLOCK(wrch);
 2907         if (rdch)
 2908                 CHN_UNLOCK(rdch);
 2909 
 2910         PCM_SG_UNLOCK();
 2911 
 2912         if (sg_ids[0])
 2913                 free_unr(pcmsg_unrhdr, sg_ids[0]);
 2914         if (sg_ids[1])
 2915                 free_unr(pcmsg_unrhdr, sg_ids[1]);
 2916         if (sg_ids[2])
 2917                 free_unr(pcmsg_unrhdr, sg_ids[2]);
 2918 
 2919         return (ret);
 2920 }
 2921 
 2922 /**
 2923  * @brief Launch a sync group into action
 2924  *
 2925  * Sync groups are established via SNDCTL_DSP_SYNCGROUP.  This function
 2926  * iterates over all members, triggering them along the way.
 2927  *
 2928  * @note Caller must not hold any channel locks.
 2929  *
 2930  * @param sg_id sync group identifier
 2931  *
 2932  * @retval 0    success
 2933  * @retval non-zero     error worthy of propagating upstream to user
 2934  */
 2935 static int
 2936 dsp_oss_syncstart(int sg_id)
 2937 {
 2938         struct pcmchan_syncmember *sm, *sm_tmp;
 2939         struct pcmchan_syncgroup *sg;
 2940         struct pcm_channel *c;
 2941         int ret, needlocks;
 2942 
 2943         /* Get the synclists lock */
 2944         PCM_SG_LOCK();
 2945 
 2946         do {
 2947                 ret = 0;
 2948                 needlocks = 0;
 2949 
 2950                 /* Search for syncgroup by ID */
 2951                 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
 2952                         if (sg->id == sg_id)
 2953                                 break;
 2954                 }
 2955 
 2956                 /* Return EINVAL if not found */
 2957                 if (sg == NULL) {
 2958                         ret = EINVAL;
 2959                         break;
 2960                 }
 2961 
 2962                 /* Any removals resulting in an empty group should've handled this */
 2963                 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
 2964 
 2965                 /*
 2966                  * Attempt to lock all member channels - if any are already
 2967                  * locked, unlock those acquired, sleep for a bit, and try
 2968                  * again.
 2969                  */
 2970                 SLIST_FOREACH(sm, &sg->members, link) {
 2971                         if (CHN_TRYLOCK(sm->ch) == 0) {
 2972                                 int timo = hz * 5/1000; 
 2973                                 if (timo < 1)
 2974                                         timo = 1;
 2975 
 2976                                 /* Release all locked channels so far, retry */
 2977                                 SLIST_FOREACH(sm_tmp, &sg->members, link) {
 2978                                         /* sm is the member already locked */
 2979                                         if (sm == sm_tmp)
 2980                                                 break;
 2981                                         CHN_UNLOCK(sm_tmp->ch);
 2982                                 }
 2983 
 2984                                 /** @todo Is PRIBIO correct/ */
 2985                                 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
 2986                                     PRIBIO | PCATCH, "pcmsg", timo);
 2987                                 if (ret == EINTR || ret == ERESTART)
 2988                                         break;
 2989 
 2990                                 needlocks = 1;
 2991                                 ret = 0; /* Assumes ret == EAGAIN... */
 2992                         }
 2993                 }
 2994         } while (needlocks && ret == 0);
 2995 
 2996         /* Proceed only if no errors encountered. */
 2997         if (ret == 0) {
 2998                 /* Launch channels */
 2999                 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
 3000                         SLIST_REMOVE_HEAD(&sg->members, link);
 3001 
 3002                         c = sm->ch;
 3003                         c->sm = NULL;
 3004                         chn_start(c, 1);
 3005                         c->flags &= ~CHN_F_NOTRIGGER;
 3006                         CHN_UNLOCK(c);
 3007 
 3008                         free(sm, M_DEVBUF);
 3009                 }
 3010 
 3011                 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
 3012                 free(sg, M_DEVBUF);
 3013         }
 3014 
 3015         PCM_SG_UNLOCK();
 3016 
 3017         /*
 3018          * Free_unr() may sleep, so be sure to give up the syncgroup lock
 3019          * first.
 3020          */
 3021         if (ret == 0)
 3022                 free_unr(pcmsg_unrhdr, sg_id);
 3023 
 3024         return (ret);
 3025 }
 3026 
 3027 /**
 3028  * @brief Handler for SNDCTL_DSP_POLICY
 3029  *
 3030  * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
 3031  * size and count like with SNDCTL_DSP_SETFRAGMENT.  Instead of the user
 3032  * specifying those two parameters, s/he simply selects a number from 0..10
 3033  * which corresponds to a buffer size.  Smaller numbers request smaller
 3034  * buffers with lower latencies (at greater overhead from more frequent
 3035  * interrupts), while greater numbers behave in the opposite manner.
 3036  *
 3037  * The 4Front spec states that a value of 5 should be the default.  However,
 3038  * this implementation deviates slightly by using a linear scale without
 3039  * consulting drivers.  I.e., even though drivers may have different default
 3040  * buffer sizes, a policy argument of 5 will have the same result across
 3041  * all drivers.
 3042  *
 3043  * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
 3044  * more information.
 3045  *
 3046  * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
 3047  *       work with hardware drivers directly.
 3048  *
 3049  * @note PCM channel arguments must not be locked by caller.
 3050  *
 3051  * @param wrch  Pointer to opened playback channel (optional; may be NULL)
 3052  * @param rdch  " recording channel (optional; may be NULL)
 3053  * @param policy Integer from [0:10]
 3054  *
 3055  * @retval 0    constant (for now)
 3056  */
 3057 static int
 3058 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
 3059 {
 3060         int ret;
 3061 
 3062         if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
 3063                 return (EIO);
 3064 
 3065         /* Default: success */
 3066         ret = 0;
 3067 
 3068         if (rdch) {
 3069                 CHN_LOCK(rdch);
 3070                 ret = chn_setlatency(rdch, policy);
 3071                 CHN_UNLOCK(rdch);
 3072         }
 3073 
 3074         if (wrch && ret == 0) {
 3075                 CHN_LOCK(wrch);
 3076                 ret = chn_setlatency(wrch, policy);
 3077                 CHN_UNLOCK(wrch);
 3078         }
 3079 
 3080         if (ret)
 3081                 ret = EIO;
 3082 
 3083         return (ret);
 3084 }
 3085 
 3086 /**
 3087  * @brief Enable or disable "cooked" mode
 3088  *
 3089  * This is a handler for @c SNDCTL_DSP_COOKEDMODE.  When in cooked mode, which
 3090  * is the default, the sound system handles rate and format conversions
 3091  * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
 3092  * operates with 44100Hz/16bit/signed samples).
 3093  *
 3094  * Disabling cooked mode is intended for applications wanting to mmap()
 3095  * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
 3096  * feeder architecture, presumably to gain as much control over audio
 3097  * hardware as possible.
 3098  *
 3099  * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
 3100  * for more details.
 3101  *
 3102  * @param wrch          playback channel (optional; may be NULL)
 3103  * @param rdch          recording channel (optional; may be NULL)
 3104  * @param enabled       0 = raw mode, 1 = cooked mode
 3105  *
 3106  * @retval EINVAL       Operation not yet supported.
 3107  */
 3108 static int
 3109 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
 3110 {
 3111 
 3112         /*
 3113          * XXX I just don't get it. Why don't they call it
 3114          * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
 3115          * This is just plain so confusing, incoherent,
 3116          * <insert any non-printable characters here>.
 3117          */
 3118         if (!(enabled == 1 || enabled == 0))
 3119                 return (EINVAL);
 3120 
 3121         /*
 3122          * I won't give in. I'm inverting its logic here and now.
 3123          * Brag all you want, but "BITPERFECT" should be the better
 3124          * term here.
 3125          */
 3126         enabled ^= 0x00000001;
 3127 
 3128         if (wrch != NULL) {
 3129                 CHN_LOCK(wrch);
 3130                 wrch->flags &= ~CHN_F_BITPERFECT;
 3131                 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
 3132                 CHN_UNLOCK(wrch);
 3133         }
 3134 
 3135         if (rdch != NULL) {
 3136                 CHN_LOCK(rdch);
 3137                 rdch->flags &= ~CHN_F_BITPERFECT;
 3138                 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
 3139                 CHN_UNLOCK(rdch);
 3140         }
 3141 
 3142         return (0);
 3143 }
 3144 
 3145 /**
 3146  * @brief Retrieve channel interleaving order
 3147  *
 3148  * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
 3149  *
 3150  * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
 3151  * for more details.
 3152  *
 3153  * @note As the ioctl definition is still under construction, FreeBSD
 3154  *       does not currently support SNDCTL_DSP_GET_CHNORDER.
 3155  *
 3156  * @param wrch  playback channel (optional; may be NULL)
 3157  * @param rdch  recording channel (optional; may be NULL)
 3158  * @param map   channel map (result will be stored there)
 3159  *
 3160  * @retval EINVAL       Operation not yet supported.
 3161  */
 3162 static int
 3163 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
 3164 {
 3165         struct pcm_channel *ch;
 3166         int ret;
 3167 
 3168         ch = (wrch != NULL) ? wrch : rdch;
 3169         if (ch != NULL) {
 3170                 CHN_LOCK(ch);
 3171                 ret = chn_oss_getorder(ch, map);
 3172                 CHN_UNLOCK(ch);
 3173         } else
 3174                 ret = EINVAL;
 3175 
 3176         return (ret);
 3177 }
 3178 
 3179 /**
 3180  * @brief Specify channel interleaving order
 3181  *
 3182  * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
 3183  *
 3184  * @note As the ioctl definition is still under construction, FreeBSD
 3185  *       does not currently support @c SNDCTL_DSP_SET_CHNORDER.
 3186  *
 3187  * @param wrch  playback channel (optional; may be NULL)
 3188  * @param rdch  recording channel (optional; may be NULL)
 3189  * @param map   channel map
 3190  *
 3191  * @retval EINVAL       Operation not yet supported.
 3192  */
 3193 static int
 3194 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
 3195 {
 3196         int ret;
 3197 
 3198         ret = 0;
 3199 
 3200         if (wrch != NULL) {
 3201                 CHN_LOCK(wrch);
 3202                 ret = chn_oss_setorder(wrch, map);
 3203                 CHN_UNLOCK(wrch);
 3204         }
 3205 
 3206         if (ret == 0 && rdch != NULL) {
 3207                 CHN_LOCK(rdch);
 3208                 ret = chn_oss_setorder(rdch, map);
 3209                 CHN_UNLOCK(rdch);
 3210         }
 3211 
 3212         return (ret);
 3213 }
 3214 
 3215 static int
 3216 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
 3217     int *mask)
 3218 {
 3219         struct pcm_channel *ch;
 3220         uint32_t chnmask;
 3221         int ret;
 3222 
 3223         chnmask = 0;
 3224         ch = (wrch != NULL) ? wrch : rdch;
 3225 
 3226         if (ch != NULL) {
 3227                 CHN_LOCK(ch);
 3228                 ret = chn_oss_getmask(ch, &chnmask);
 3229                 CHN_UNLOCK(ch);
 3230         } else
 3231                 ret = EINVAL;
 3232 
 3233         if (ret == 0)
 3234                 *mask = chnmask;
 3235 
 3236         return (ret);
 3237 }
 3238 
 3239 #ifdef OSSV4_EXPERIMENT
 3240 /**
 3241  * @brief Retrieve an audio device's label
 3242  *
 3243  * This is a handler for the @c SNDCTL_GETLABEL ioctl.
 3244  *
 3245  * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
 3246  * for more details.
 3247  *
 3248  * From Hannu@4Front:  "For example ossxmix (just like some HW mixer
 3249  * consoles) can show variable "labels" for certain controls. By default
 3250  * the application name (say quake) is shown as the label but
 3251  * applications may change the labels themselves."
 3252  *
 3253  * @note As the ioctl definition is still under construction, FreeBSD
 3254  *       does not currently support @c SNDCTL_GETLABEL.
 3255  *
 3256  * @param wrch  playback channel (optional; may be NULL)
 3257  * @param rdch  recording channel (optional; may be NULL)
 3258  * @param label label gets copied here
 3259  *
 3260  * @retval EINVAL       Operation not yet supported.
 3261  */
 3262 static int
 3263 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
 3264 {
 3265         return (EINVAL);
 3266 }
 3267 
 3268 /**
 3269  * @brief Specify an audio device's label
 3270  *
 3271  * This is a handler for the @c SNDCTL_SETLABEL ioctl.  Please see the
 3272  * comments for @c dsp_oss_getlabel immediately above.
 3273  *
 3274  * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
 3275  * for more details.
 3276  *
 3277  * @note As the ioctl definition is still under construction, FreeBSD
 3278  *       does not currently support SNDCTL_SETLABEL.
 3279  *
 3280  * @param wrch  playback channel (optional; may be NULL)
 3281  * @param rdch  recording channel (optional; may be NULL)
 3282  * @param label label gets copied from here
 3283  *
 3284  * @retval EINVAL       Operation not yet supported.
 3285  */
 3286 static int
 3287 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
 3288 {
 3289         return (EINVAL);
 3290 }
 3291 
 3292 /**
 3293  * @brief Retrieve name of currently played song
 3294  *
 3295  * This is a handler for the @c SNDCTL_GETSONG ioctl.  Audio players could
 3296  * tell the system the name of the currently playing song, which would be
 3297  * visible in @c /dev/sndstat.
 3298  *
 3299  * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
 3300  * for more details.
 3301  *
 3302  * @note As the ioctl definition is still under construction, FreeBSD
 3303  *       does not currently support SNDCTL_GETSONG.
 3304  *
 3305  * @param wrch  playback channel (optional; may be NULL)
 3306  * @param rdch  recording channel (optional; may be NULL)
 3307  * @param song  song name gets copied here
 3308  *
 3309  * @retval EINVAL       Operation not yet supported.
 3310  */
 3311 static int
 3312 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
 3313 {
 3314         return (EINVAL);
 3315 }
 3316 
 3317 /**
 3318  * @brief Retrieve name of currently played song
 3319  *
 3320  * This is a handler for the @c SNDCTL_SETSONG ioctl.  Audio players could
 3321  * tell the system the name of the currently playing song, which would be
 3322  * visible in @c /dev/sndstat.
 3323  *
 3324  * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
 3325  * for more details.
 3326  *
 3327  * @note As the ioctl definition is still under construction, FreeBSD
 3328  *       does not currently support SNDCTL_SETSONG.
 3329  *
 3330  * @param wrch  playback channel (optional; may be NULL)
 3331  * @param rdch  recording channel (optional; may be NULL)
 3332  * @param song  song name gets copied from here
 3333  *
 3334  * @retval EINVAL       Operation not yet supported.
 3335  */
 3336 static int
 3337 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
 3338 {
 3339         return (EINVAL);
 3340 }
 3341 
 3342 /**
 3343  * @brief Rename a device
 3344  *
 3345  * This is a handler for the @c SNDCTL_SETNAME ioctl.
 3346  *
 3347  * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
 3348  * more details.
 3349  *
 3350  * From Hannu@4Front:  "This call is used to change the device name
 3351  * reported in /dev/sndstat and ossinfo. So instead of  using some generic
 3352  * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
 3353  * name depending on the current context (for example 'OSS virtual wave table
 3354  * synth' or 'VoIP link to London')."
 3355  *
 3356  * @note As the ioctl definition is still under construction, FreeBSD
 3357  *       does not currently support SNDCTL_SETNAME.
 3358  *
 3359  * @param wrch  playback channel (optional; may be NULL)
 3360  * @param rdch  recording channel (optional; may be NULL)
 3361  * @param name  new device name gets copied from here
 3362  *
 3363  * @retval EINVAL       Operation not yet supported.
 3364  */
 3365 static int
 3366 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
 3367 {
 3368         return (EINVAL);
 3369 }
 3370 #endif  /* !OSSV4_EXPERIMENT */

Cache object: 972c6e9263529f5495bc997dbe69f768


[ 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.