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 <cg@freebsd.org>
    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/6.0/sys/dev/sound/pcm/mixer.c 151714 2005-10-26 21:17:40Z ariff $");
   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         .d_version =    D_VERSION,
   77         .d_flags =      D_TRACKCLOSE | D_NEEDGIANT,
   78         .d_open =       mixer_open,
   79         .d_close =      mixer_close,
   80         .d_ioctl =      mixer_ioctl,
   81         .d_name =       "mixer",
   82 };
   83 
   84 #ifdef USING_DEVFS
   85 static eventhandler_tag mixer_ehtag;
   86 #endif
   87 
   88 static struct cdev *
   89 mixer_get_devt(device_t dev)
   90 {
   91         struct snddev_info *snddev;
   92 
   93         snddev = device_get_softc(dev);
   94 
   95         return snddev->mixer_dev;
   96 }
   97 
   98 #ifdef SND_DYNSYSCTL
   99 static int
  100 mixer_lookup(char *devname)
  101 {
  102         int i;
  103 
  104         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  105                 if (strncmp(devname, snd_mixernames[i],
  106                     strlen(snd_mixernames[i])) == 0)
  107                         return i;
  108         return -1;
  109 }
  110 #endif
  111 
  112 static int
  113 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
  114 {
  115         unsigned l, r;
  116         int v;
  117 
  118         if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
  119                 return -1;
  120 
  121         l = min((lev & 0x00ff), 100);
  122         r = min(((lev & 0xff00) >> 8), 100);
  123 
  124         v = MIXER_SET(mixer, dev, l, r);
  125         if (v < 0)
  126                 return -1;
  127 
  128         mixer->level[dev] = l | (r << 8);
  129         return 0;
  130 }
  131 
  132 static int
  133 mixer_get(struct snd_mixer *mixer, int dev)
  134 {
  135         if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
  136                 return mixer->level[dev];
  137         else return -1;
  138 }
  139 
  140 static int
  141 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
  142 {
  143         src &= mixer->recdevs;
  144         if (src == 0)
  145                 src = SOUND_MASK_MIC;
  146         mixer->recsrc = MIXER_SETRECSRC(mixer, src);
  147         return 0;
  148 }
  149 
  150 static int
  151 mixer_getrecsrc(struct snd_mixer *mixer)
  152 {
  153         return mixer->recsrc;
  154 }
  155 
  156 void
  157 mix_setdevs(struct snd_mixer *m, u_int32_t v)
  158 {
  159         m->devs = v;
  160 }
  161 
  162 void
  163 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
  164 {
  165         m->recdevs = v;
  166 }
  167 
  168 u_int32_t
  169 mix_getdevs(struct snd_mixer *m)
  170 {
  171         return m->devs;
  172 }
  173 
  174 u_int32_t
  175 mix_getrecdevs(struct snd_mixer *m)
  176 {
  177         return m->recdevs;
  178 }
  179 
  180 void *
  181 mix_getdevinfo(struct snd_mixer *m)
  182 {
  183         return m->devinfo;
  184 }
  185 
  186 int
  187 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
  188 {
  189         struct snddev_info *snddev;
  190         struct snd_mixer *m;
  191         u_int16_t v;
  192         struct cdev *pdev;
  193         int i, unit, val;
  194 
  195         m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
  196         snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
  197         m->lock = snd_mtxcreate(m->name, "pcm mixer");
  198         m->type = cls->name;
  199         m->devinfo = devinfo;
  200         m->busy = 0;
  201 
  202         if (MIXER_INIT(m))
  203                 goto bad;
  204 
  205         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  206                 v = snd_mixerdefaults[i];
  207 
  208                 if (resource_int_value(device_get_name(dev),
  209                     device_get_unit(dev), snd_mixernames[i], &val) == 0) {
  210                         if (val >= 0 && val <= 100) {
  211                                 v = (u_int16_t) val;
  212                         }
  213                 }
  214 
  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         snddev = device_get_softc(dev);
  225         snddev->mixer_dev = pdev;
  226 
  227         return 0;
  228 
  229 bad:
  230         snd_mtxlock(m->lock);
  231         snd_mtxfree(m->lock);
  232         kobj_delete((kobj_t)m, M_MIXER);
  233         return -1;
  234 }
  235 
  236 int
  237 mixer_uninit(device_t dev)
  238 {
  239         int i;
  240         struct snd_mixer *m;
  241         struct cdev *pdev;
  242 
  243         pdev = mixer_get_devt(dev);
  244         m = pdev->si_drv1;
  245         snd_mtxlock(m->lock);
  246 
  247         if (m->busy) {
  248                 snd_mtxunlock(m->lock);
  249                 return EBUSY;
  250         }
  251 
  252         pdev->si_drv1 = NULL;
  253         destroy_dev(pdev);
  254 
  255         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  256                 mixer_set(m, i, 0);
  257 
  258         mixer_setrecsrc(m, SOUND_MASK_MIC);
  259 
  260         MIXER_UNINIT(m);
  261 
  262         snd_mtxfree(m->lock);
  263         kobj_delete((kobj_t)m, M_MIXER);
  264 
  265         return 0;
  266 }
  267 
  268 int
  269 mixer_reinit(device_t dev)
  270 {
  271         struct snd_mixer *m;
  272         struct cdev *pdev;
  273         int i;
  274 
  275         pdev = mixer_get_devt(dev);
  276         m = pdev->si_drv1;
  277         snd_mtxlock(m->lock);
  278 
  279         i = MIXER_REINIT(m);
  280         if (i) {
  281                 snd_mtxunlock(m->lock);
  282                 return i;
  283         }
  284 
  285         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  286                 mixer_set(m, i, m->level[i]);
  287 
  288         mixer_setrecsrc(m, m->recsrc);
  289         snd_mtxunlock(m->lock);
  290 
  291         return 0;
  292 }
  293 
  294 #ifdef SND_DYNSYSCTL
  295 static int
  296 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
  297 {
  298         char devname[32];
  299         int error, dev;
  300         struct snd_mixer *m;
  301 
  302         m = oidp->oid_arg1;
  303         snd_mtxlock(m->lock);
  304         strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
  305         snd_mtxunlock(m->lock);
  306         error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
  307         snd_mtxlock(m->lock);
  308         if (error == 0 && req->newptr != NULL) {
  309                 dev = mixer_lookup(devname);
  310                 if (dev == -1) {
  311                         snd_mtxunlock(m->lock);
  312                         return EINVAL;
  313                 }
  314                 else if (dev != m->hwvol_mixer) {
  315                         m->hwvol_mixer = dev;
  316                         m->hwvol_muted = 0;
  317                 }
  318         }
  319         snd_mtxunlock(m->lock);
  320         return error;
  321 }
  322 #endif
  323 
  324 int
  325 mixer_hwvol_init(device_t dev)
  326 {
  327         struct snd_mixer *m;
  328         struct cdev *pdev;
  329 
  330         pdev = mixer_get_devt(dev);
  331         m = pdev->si_drv1;
  332 
  333         m->hwvol_mixer = SOUND_MIXER_VOLUME;
  334         m->hwvol_step = 5;
  335 #ifdef SND_DYNSYSCTL
  336         SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  337             OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
  338         SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  339             OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
  340             sysctl_hw_snd_hwvol_mixer, "A", "");
  341 #endif
  342         return 0;
  343 }
  344 
  345 void
  346 mixer_hwvol_mute(device_t dev)
  347 {
  348         struct snd_mixer *m;
  349         struct cdev *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         struct cdev *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(struct cdev *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(struct cdev *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(struct cdev *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, struct ucred *cred, char *name, int namelen,
  490     struct cdev **dev)
  491 {
  492         struct snddev_info *sd;
  493 
  494         if (*dev != NULL)
  495                 return;
  496         if (strcmp(name, "mixer") == 0) {
  497                 sd = devclass_get_softc(pcm_devclass, snd_unit);
  498                 if (sd != NULL) {
  499                         *dev = sd->mixer_dev;
  500                         dev_ref(*dev);
  501                 }
  502         }
  503 }
  504 
  505 static void
  506 mixer_sysinit(void *p)
  507 {
  508         mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
  509 }
  510 
  511 static void
  512 mixer_sysuninit(void *p)
  513 {
  514         if (mixer_ehtag != NULL)
  515                 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
  516 }
  517 
  518 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
  519 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
  520 #endif
  521 
  522 

Cache object: 5fb76cf894bad2d236ef48ca492c146d


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