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

Cache object: da378293ebbcd1f3dff383253079b2e1


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