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/mixer.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 #include <dev/sound/pcm/sound.h>
   28 
   29 #include "mixer_if.h"
   30 
   31 SND_DECLARE_FILE("$FreeBSD: releng/5.0/sys/dev/sound/pcm/mixer.c 107285 2002-11-26 18:16:27Z cg $");
   32 
   33 MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
   34 
   35 #define MIXER_NAMELEN   16
   36 struct snd_mixer {
   37         KOBJ_FIELDS;
   38         const char *type;
   39         void *devinfo;
   40         int busy;
   41         int hwvol_muted;
   42         int hwvol_mixer;
   43         int hwvol_step;
   44         u_int32_t hwvol_mute_level;
   45         u_int32_t devs;
   46         u_int32_t recdevs;
   47         u_int32_t recsrc;
   48         u_int16_t level[32];
   49         char name[MIXER_NAMELEN];
   50         struct mtx *lock;
   51 };
   52 
   53 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
   54         [SOUND_MIXER_VOLUME]    = 75,
   55         [SOUND_MIXER_BASS]      = 50,
   56         [SOUND_MIXER_TREBLE]    = 50,
   57         [SOUND_MIXER_SYNTH]     = 75,
   58         [SOUND_MIXER_PCM]       = 75,
   59         [SOUND_MIXER_SPEAKER]   = 75,
   60         [SOUND_MIXER_LINE]      = 75,
   61         [SOUND_MIXER_MIC]       = 0,
   62         [SOUND_MIXER_CD]        = 75,
   63         [SOUND_MIXER_LINE1]     = 75,
   64         [SOUND_MIXER_VIDEO]     = 75,
   65         [SOUND_MIXER_RECLEV]    = 0,
   66         [SOUND_MIXER_OGAIN]     = 50,
   67         [SOUND_MIXER_MONITOR]   = 75,
   68 };
   69 
   70 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
   71 
   72 static d_open_t mixer_open;
   73 static d_close_t mixer_close;
   74 
   75 static struct cdevsw mixer_cdevsw = {
   76         /* open */      mixer_open,
   77         /* close */     mixer_close,
   78         /* read */      noread,
   79         /* write */     nowrite,
   80         /* ioctl */     mixer_ioctl,
   81         /* poll */      nopoll,
   82         /* mmap */      nommap,
   83         /* strategy */  nostrategy,
   84         /* name */      "mixer",
   85         /* maj */       SND_CDEV_MAJOR,
   86         /* dump */      nodump,
   87         /* psize */     nopsize,
   88         /* flags */     0,
   89 };
   90 
   91 #ifdef USING_DEVFS
   92 static eventhandler_tag mixer_ehtag;
   93 #endif
   94 
   95 static dev_t
   96 mixer_get_devt(device_t dev)
   97 {
   98         dev_t pdev;
   99         int unit;
  100 
  101         unit = device_get_unit(dev);
  102         pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_CTL, 0));
  103 
  104         return pdev;
  105 }
  106 
  107 #ifdef SND_DYNSYSCTL
  108 static int
  109 mixer_lookup(char *devname)
  110 {
  111         int i;
  112 
  113         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  114                 if (strncmp(devname, snd_mixernames[i],
  115                     strlen(snd_mixernames[i])) == 0)
  116                         return i;
  117         return -1;
  118 }
  119 #endif
  120 
  121 static int
  122 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
  123 {
  124         unsigned l, r;
  125         int v;
  126 
  127         if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
  128                 return -1;
  129 
  130         l = min((lev & 0x00ff), 100);
  131         r = min(((lev & 0xff00) >> 8), 100);
  132 
  133         v = MIXER_SET(mixer, dev, l, r);
  134         if (v < 0)
  135                 return -1;
  136 
  137         mixer->level[dev] = l | (r << 8);
  138         return 0;
  139 }
  140 
  141 static int
  142 mixer_get(struct snd_mixer *mixer, int dev)
  143 {
  144         if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
  145                 return mixer->level[dev];
  146         else return -1;
  147 }
  148 
  149 static int
  150 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
  151 {
  152         src &= mixer->recdevs;
  153         if (src == 0)
  154                 src = SOUND_MASK_MIC;
  155         mixer->recsrc = MIXER_SETRECSRC(mixer, src);
  156         return 0;
  157 }
  158 
  159 static int
  160 mixer_getrecsrc(struct snd_mixer *mixer)
  161 {
  162         return mixer->recsrc;
  163 }
  164 
  165 void
  166 mix_setdevs(struct snd_mixer *m, u_int32_t v)
  167 {
  168         m->devs = v;
  169 }
  170 
  171 void
  172 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
  173 {
  174         m->recdevs = v;
  175 }
  176 
  177 u_int32_t
  178 mix_getdevs(struct snd_mixer *m)
  179 {
  180         return m->devs;
  181 }
  182 
  183 u_int32_t
  184 mix_getrecdevs(struct snd_mixer *m)
  185 {
  186         return m->recdevs;
  187 }
  188 
  189 void *
  190 mix_getdevinfo(struct snd_mixer *m)
  191 {
  192         return m->devinfo;
  193 }
  194 
  195 int
  196 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
  197 {
  198         struct snd_mixer *m;
  199         u_int16_t v;
  200         dev_t pdev;
  201         int i, unit;
  202 
  203         m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
  204         snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
  205         m->lock = snd_mtxcreate(m->name, "pcm mixer");
  206         m->type = cls->name;
  207         m->devinfo = devinfo;
  208         m->busy = 0;
  209 
  210         if (MIXER_INIT(m))
  211                 goto bad;
  212 
  213         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  214                 v = snd_mixerdefaults[i];
  215                 mixer_set(m, i, v | (v << 8));
  216         }
  217 
  218         mixer_setrecsrc(m, SOUND_MASK_MIC);
  219 
  220         unit = device_get_unit(dev);
  221         pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
  222                  UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
  223         pdev->si_drv1 = m;
  224 
  225         return 0;
  226 
  227 bad:
  228         snd_mtxlock(m->lock);
  229         snd_mtxfree(m->lock);
  230         kobj_delete((kobj_t)m, M_MIXER);
  231         return -1;
  232 }
  233 
  234 int
  235 mixer_uninit(device_t dev)
  236 {
  237         int i;
  238         struct snd_mixer *m;
  239         dev_t pdev;
  240 
  241         pdev = mixer_get_devt(dev);
  242         m = pdev->si_drv1;
  243         snd_mtxlock(m->lock);
  244 
  245         if (m->busy) {
  246                 snd_mtxunlock(m->lock);
  247                 return EBUSY;
  248         }
  249 
  250         pdev->si_drv1 = NULL;
  251         destroy_dev(pdev);
  252 
  253         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  254                 mixer_set(m, i, 0);
  255 
  256         mixer_setrecsrc(m, SOUND_MASK_MIC);
  257 
  258         MIXER_UNINIT(m);
  259 
  260         snd_mtxfree(m->lock);
  261         kobj_delete((kobj_t)m, M_MIXER);
  262 
  263         return 0;
  264 }
  265 
  266 int
  267 mixer_reinit(device_t dev)
  268 {
  269         struct snd_mixer *m;
  270         dev_t pdev;
  271         int i;
  272 
  273         pdev = mixer_get_devt(dev);
  274         m = pdev->si_drv1;
  275         snd_mtxlock(m->lock);
  276 
  277         i = MIXER_REINIT(m);
  278         if (i) {
  279                 snd_mtxunlock(m->lock);
  280                 return i;
  281         }
  282 
  283         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  284                 mixer_set(m, i, m->level[i]);
  285 
  286         mixer_setrecsrc(m, m->recsrc);
  287         snd_mtxunlock(m->lock);
  288 
  289         return 0;
  290 }
  291 
  292 #ifdef SND_DYNSYSCTL
  293 static int
  294 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
  295 {
  296         char devname[32];
  297         int error, dev;
  298         struct snd_mixer *m;
  299 
  300         m = oidp->oid_arg1;
  301         snd_mtxlock(m->lock);
  302         strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
  303         snd_mtxunlock(m->lock);
  304         error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
  305         snd_mtxlock(m->lock);
  306         if (error == 0 && req->newptr != NULL) {
  307                 dev = mixer_lookup(devname);
  308                 if (dev == -1) {
  309                         snd_mtxunlock(m->lock);
  310                         return EINVAL;
  311                 }
  312                 else if (dev != m->hwvol_mixer) {
  313                         m->hwvol_mixer = dev;
  314                         m->hwvol_muted = 0;
  315                 }
  316         }
  317         snd_mtxunlock(m->lock);
  318         return error;
  319 }
  320 #endif
  321 
  322 int
  323 mixer_hwvol_init(device_t dev)
  324 {
  325         struct snd_mixer *m;
  326         dev_t pdev;
  327 
  328         pdev = mixer_get_devt(dev);
  329         m = pdev->si_drv1;
  330         snd_mtxlock(m->lock);
  331 
  332         m->hwvol_mixer = SOUND_MIXER_VOLUME;
  333         m->hwvol_step = 5;
  334 #ifdef SND_DYNSYSCTL
  335         SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  336             OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
  337         SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  338             OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
  339             sysctl_hw_snd_hwvol_mixer, "A", "");
  340 #endif
  341         snd_mtxunlock(m->lock);
  342         return 0;
  343 }
  344 
  345 void
  346 mixer_hwvol_mute(device_t dev)
  347 {
  348         struct snd_mixer *m;
  349         dev_t pdev;
  350 
  351         pdev = mixer_get_devt(dev);
  352         m = pdev->si_drv1;
  353         snd_mtxlock(m->lock);
  354         if (m->hwvol_muted) {
  355                 m->hwvol_muted = 0;
  356                 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
  357         } else {
  358                 m->hwvol_muted++;
  359                 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
  360                 mixer_set(m, m->hwvol_mixer, 0);
  361         }
  362         snd_mtxunlock(m->lock);
  363 }
  364 
  365 void
  366 mixer_hwvol_step(device_t dev, int left_step, int right_step)
  367 {
  368         struct snd_mixer *m;
  369         int level, left, right;
  370         dev_t pdev;
  371 
  372         pdev = mixer_get_devt(dev);
  373         m = pdev->si_drv1;
  374         snd_mtxlock(m->lock);
  375         if (m->hwvol_muted) {
  376                 m->hwvol_muted = 0;
  377                 level = m->hwvol_mute_level;
  378         } else
  379                 level = mixer_get(m, m->hwvol_mixer);
  380         if (level != -1) {
  381                 left = level & 0xff;
  382                 right = level >> 8;
  383                 left += left_step * m->hwvol_step;
  384                 if (left < 0)
  385                         left = 0;
  386                 right += right_step * m->hwvol_step;
  387                 if (right < 0)
  388                         right = 0;
  389                 mixer_set(m, m->hwvol_mixer, left | right << 8);
  390         }
  391         snd_mtxunlock(m->lock);
  392 }
  393 
  394 /* ----------------------------------------------------------------------- */
  395 
  396 static int
  397 mixer_open(dev_t i_dev, int flags, int mode, struct thread *td)
  398 {
  399         struct snd_mixer *m;
  400         intrmask_t s;
  401 
  402         m = i_dev->si_drv1;
  403         s = spltty();
  404         snd_mtxlock(m->lock);
  405 
  406         m->busy++;
  407 
  408         snd_mtxunlock(m->lock);
  409         splx(s);
  410         return 0;
  411 }
  412 
  413 static int
  414 mixer_close(dev_t i_dev, int flags, int mode, struct thread *td)
  415 {
  416         struct snd_mixer *m;
  417         intrmask_t s;
  418 
  419         m = i_dev->si_drv1;
  420         s = spltty();
  421         snd_mtxlock(m->lock);
  422 
  423         if (!m->busy) {
  424                 snd_mtxunlock(m->lock);
  425                 splx(s);
  426                 return EBADF;
  427         }
  428         m->busy--;
  429 
  430         snd_mtxunlock(m->lock);
  431         splx(s);
  432         return 0;
  433 }
  434 
  435 int
  436 mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
  437 {
  438         struct snd_mixer *m;
  439         intrmask_t s;
  440         int ret, *arg_i = (int *)arg;
  441         int v = -1, j = cmd & 0xff;
  442 
  443         m = i_dev->si_drv1;
  444         if (!m->busy)
  445                 return EBADF;
  446 
  447         s = spltty();
  448         snd_mtxlock(m->lock);
  449         if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
  450                 if (j == SOUND_MIXER_RECSRC)
  451                         ret = mixer_setrecsrc(m, *arg_i);
  452                 else
  453                         ret = mixer_set(m, j, *arg_i);
  454                 snd_mtxunlock(m->lock);
  455                 splx(s);
  456                 return (ret == 0)? 0 : ENXIO;
  457         }
  458 
  459         if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
  460                 switch (j) {
  461                 case SOUND_MIXER_DEVMASK:
  462                 case SOUND_MIXER_CAPS:
  463                 case SOUND_MIXER_STEREODEVS:
  464                         v = mix_getdevs(m);
  465                         break;
  466 
  467                 case SOUND_MIXER_RECMASK:
  468                         v = mix_getrecdevs(m);
  469                         break;
  470 
  471                 case SOUND_MIXER_RECSRC:
  472                         v = mixer_getrecsrc(m);
  473                         break;
  474 
  475                 default:
  476                         v = mixer_get(m, j);
  477                 }
  478                 *arg_i = v;
  479                 snd_mtxunlock(m->lock);
  480                 return (v != -1)? 0 : ENXIO;
  481         }
  482         snd_mtxunlock(m->lock);
  483         splx(s);
  484         return ENXIO;
  485 }
  486 
  487 #ifdef USING_DEVFS
  488 static void
  489 mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
  490 {
  491         dev_t pdev;
  492 
  493         if (*dev != NODEV)
  494                 return;
  495         if (strcmp(name, "mixer") == 0) {
  496                 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
  497                 if (pdev->si_flags & SI_NAMED)
  498                         *dev = pdev;
  499         }
  500 }
  501 
  502 static void
  503 mixer_sysinit(void *p)
  504 {
  505         mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
  506 }
  507 
  508 static void
  509 mixer_sysuninit(void *p)
  510 {
  511         if (mixer_ehtag != NULL)
  512                 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
  513 }
  514 
  515 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
  516 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
  517 #endif
  518 
  519 

Cache object: 0322e31dec464da213ba01aa9052ae67


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