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-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) 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.1/sys/dev/sound/pcm/mixer.c 157495 2006-04-04 17:43:49Z 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         device_t dev;
   45         u_int32_t hwvol_mute_level;
   46         u_int32_t devs;
   47         u_int32_t recdevs;
   48         u_int32_t recsrc;
   49         u_int16_t level[32];
   50         char name[MIXER_NAMELEN];
   51         struct mtx *lock;
   52 };
   53 
   54 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
   55         [SOUND_MIXER_VOLUME]    = 75,
   56         [SOUND_MIXER_BASS]      = 50,
   57         [SOUND_MIXER_TREBLE]    = 50,
   58         [SOUND_MIXER_SYNTH]     = 75,
   59         [SOUND_MIXER_PCM]       = 75,
   60         [SOUND_MIXER_SPEAKER]   = 75,
   61         [SOUND_MIXER_LINE]      = 75,
   62         [SOUND_MIXER_MIC]       = 0,
   63         [SOUND_MIXER_CD]        = 75,
   64         [SOUND_MIXER_IGAIN]     = 0,
   65         [SOUND_MIXER_LINE1]     = 75,
   66         [SOUND_MIXER_VIDEO]     = 75,
   67         [SOUND_MIXER_RECLEV]    = 0,
   68         [SOUND_MIXER_OGAIN]     = 50,
   69         [SOUND_MIXER_MONITOR]   = 75,
   70 };
   71 
   72 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
   73 
   74 static d_open_t mixer_open;
   75 static d_close_t mixer_close;
   76 
   77 static struct cdevsw mixer_cdevsw = {
   78         .d_version =    D_VERSION,
   79         .d_flags =      D_TRACKCLOSE | D_NEEDGIANT,
   80         .d_open =       mixer_open,
   81         .d_close =      mixer_close,
   82         .d_ioctl =      mixer_ioctl,
   83         .d_name =       "mixer",
   84 };
   85 
   86 #ifdef USING_DEVFS
   87 static eventhandler_tag mixer_ehtag;
   88 #endif
   89 
   90 static struct cdev *
   91 mixer_get_devt(device_t dev)
   92 {
   93         struct snddev_info *snddev;
   94 
   95         snddev = device_get_softc(dev);
   96 
   97         return snddev->mixer_dev;
   98 }
   99 
  100 #ifdef SND_DYNSYSCTL
  101 static int
  102 mixer_lookup(char *devname)
  103 {
  104         int i;
  105 
  106         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  107                 if (strncmp(devname, snd_mixernames[i],
  108                     strlen(snd_mixernames[i])) == 0)
  109                         return i;
  110         return -1;
  111 }
  112 #endif
  113 
  114 static int
  115 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
  116 {
  117         struct snddev_info *d;
  118         unsigned l, r;
  119         int v;
  120 
  121         if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
  122                 return -1;
  123 
  124         l = min((lev & 0x00ff), 100);
  125         r = min(((lev & 0xff00) >> 8), 100);
  126 
  127         d = device_get_softc(mixer->dev);
  128         if (dev == SOUND_MIXER_PCM && d &&
  129                         (d->flags & SD_F_SOFTVOL)) {
  130                 struct snddev_channel *sce;
  131                 struct pcm_channel *ch;
  132 #ifdef USING_MUTEX
  133                 int locked = (mixer->lock && mtx_owned((struct mtx *)(mixer->lock))) ? 1 : 0;
  134 
  135                 if (locked)
  136                         snd_mtxunlock(mixer->lock);
  137 #endif
  138                 SLIST_FOREACH(sce, &d->channels, link) {
  139                         ch = sce->channel;
  140                         CHN_LOCK(ch);
  141                         if (ch->direction == PCMDIR_PLAY &&
  142                                         (ch->feederflags & (1 << FEEDER_VOLUME)))
  143                                 chn_setvolume(ch, l, r);
  144                         CHN_UNLOCK(ch);
  145                 }
  146 #ifdef USING_MUTEX
  147                 if (locked)
  148                         snd_mtxlock(mixer->lock);
  149 #endif
  150         } else {
  151                 v = MIXER_SET(mixer, dev, l, r);
  152                 if (v < 0)
  153                         return -1;
  154         }
  155 
  156         mixer->level[dev] = l | (r << 8);
  157         return 0;
  158 }
  159 
  160 static int
  161 mixer_get(struct snd_mixer *mixer, int dev)
  162 {
  163         if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
  164                 return mixer->level[dev];
  165         else return -1;
  166 }
  167 
  168 static int
  169 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
  170 {
  171         src &= mixer->recdevs;
  172         if (src == 0)
  173                 src = SOUND_MASK_MIC;
  174         mixer->recsrc = MIXER_SETRECSRC(mixer, src);
  175         return 0;
  176 }
  177 
  178 static int
  179 mixer_getrecsrc(struct snd_mixer *mixer)
  180 {
  181         return mixer->recsrc;
  182 }
  183 
  184 void
  185 mix_setdevs(struct snd_mixer *m, u_int32_t v)
  186 {
  187         struct snddev_info *d = device_get_softc(m->dev);
  188         if (d && (d->flags & SD_F_SOFTVOL))
  189                 v |= SOUND_MASK_PCM;
  190         m->devs = v;
  191 }
  192 
  193 void
  194 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
  195 {
  196         m->recdevs = v;
  197 }
  198 
  199 u_int32_t
  200 mix_getdevs(struct snd_mixer *m)
  201 {
  202         return m->devs;
  203 }
  204 
  205 u_int32_t
  206 mix_getrecdevs(struct snd_mixer *m)
  207 {
  208         return m->recdevs;
  209 }
  210 
  211 void *
  212 mix_getdevinfo(struct snd_mixer *m)
  213 {
  214         return m->devinfo;
  215 }
  216 
  217 int
  218 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
  219 {
  220         struct snddev_info *snddev;
  221         struct snd_mixer *m;
  222         u_int16_t v;
  223         struct cdev *pdev;
  224         int i, unit, val;
  225 
  226         m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
  227         snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
  228         m->lock = snd_mtxcreate(m->name, "pcm mixer");
  229         m->type = cls->name;
  230         m->devinfo = devinfo;
  231         m->busy = 0;
  232         m->dev = dev;
  233 
  234         if (MIXER_INIT(m))
  235                 goto bad;
  236 
  237         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
  238                 v = snd_mixerdefaults[i];
  239 
  240                 if (resource_int_value(device_get_name(dev),
  241                     device_get_unit(dev), snd_mixernames[i], &val) == 0) {
  242                         if (val >= 0 && val <= 100) {
  243                                 v = (u_int16_t) val;
  244                         }
  245                 }
  246 
  247                 mixer_set(m, i, v | (v << 8));
  248         }
  249 
  250         mixer_setrecsrc(m, SOUND_MASK_MIC);
  251 
  252         unit = device_get_unit(dev);
  253         pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
  254                  UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
  255         pdev->si_drv1 = m;
  256         snddev = device_get_softc(dev);
  257         snddev->mixer_dev = pdev;
  258 
  259         return 0;
  260 
  261 bad:
  262         snd_mtxlock(m->lock);
  263         snd_mtxfree(m->lock);
  264         kobj_delete((kobj_t)m, M_MIXER);
  265         return -1;
  266 }
  267 
  268 int
  269 mixer_uninit(device_t dev)
  270 {
  271         int i;
  272         struct snddev_info *d;
  273         struct snd_mixer *m;
  274         struct cdev *pdev;
  275 
  276         d = device_get_softc(dev);
  277         pdev = mixer_get_devt(dev);
  278         if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
  279                 return EBADF;
  280         m = pdev->si_drv1;
  281         snd_mtxlock(m->lock);
  282 
  283         if (m->busy) {
  284                 snd_mtxunlock(m->lock);
  285                 return EBUSY;
  286         }
  287 
  288         pdev->si_drv1 = NULL;
  289         destroy_dev(pdev);
  290 
  291         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  292                 mixer_set(m, i, 0);
  293 
  294         mixer_setrecsrc(m, SOUND_MASK_MIC);
  295 
  296         MIXER_UNINIT(m);
  297 
  298         snd_mtxfree(m->lock);
  299         kobj_delete((kobj_t)m, M_MIXER);
  300 
  301         d->mixer_dev = NULL;
  302 
  303         return 0;
  304 }
  305 
  306 int
  307 mixer_reinit(device_t dev)
  308 {
  309         struct snd_mixer *m;
  310         struct cdev *pdev;
  311         int i;
  312 
  313         pdev = mixer_get_devt(dev);
  314         m = pdev->si_drv1;
  315         snd_mtxlock(m->lock);
  316 
  317         i = MIXER_REINIT(m);
  318         if (i) {
  319                 snd_mtxunlock(m->lock);
  320                 return i;
  321         }
  322 
  323         for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
  324                 mixer_set(m, i, m->level[i]);
  325 
  326         mixer_setrecsrc(m, m->recsrc);
  327         snd_mtxunlock(m->lock);
  328 
  329         return 0;
  330 }
  331 
  332 #ifdef SND_DYNSYSCTL
  333 static int
  334 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
  335 {
  336         char devname[32];
  337         int error, dev;
  338         struct snd_mixer *m;
  339 
  340         m = oidp->oid_arg1;
  341         snd_mtxlock(m->lock);
  342         strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
  343         snd_mtxunlock(m->lock);
  344         error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
  345         snd_mtxlock(m->lock);
  346         if (error == 0 && req->newptr != NULL) {
  347                 dev = mixer_lookup(devname);
  348                 if (dev == -1) {
  349                         snd_mtxunlock(m->lock);
  350                         return EINVAL;
  351                 }
  352                 else if (dev != m->hwvol_mixer) {
  353                         m->hwvol_mixer = dev;
  354                         m->hwvol_muted = 0;
  355                 }
  356         }
  357         snd_mtxunlock(m->lock);
  358         return error;
  359 }
  360 #endif
  361 
  362 int
  363 mixer_hwvol_init(device_t dev)
  364 {
  365         struct snd_mixer *m;
  366         struct cdev *pdev;
  367 
  368         pdev = mixer_get_devt(dev);
  369         m = pdev->si_drv1;
  370 
  371         m->hwvol_mixer = SOUND_MIXER_VOLUME;
  372         m->hwvol_step = 5;
  373 #ifdef SND_DYNSYSCTL
  374         SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  375             OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
  376         SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
  377             OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
  378             sysctl_hw_snd_hwvol_mixer, "A", "");
  379 #endif
  380         return 0;
  381 }
  382 
  383 void
  384 mixer_hwvol_mute(device_t dev)
  385 {
  386         struct snd_mixer *m;
  387         struct cdev *pdev;
  388 
  389         pdev = mixer_get_devt(dev);
  390         m = pdev->si_drv1;
  391         snd_mtxlock(m->lock);
  392         if (m->hwvol_muted) {
  393                 m->hwvol_muted = 0;
  394                 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
  395         } else {
  396                 m->hwvol_muted++;
  397                 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
  398                 mixer_set(m, m->hwvol_mixer, 0);
  399         }
  400         snd_mtxunlock(m->lock);
  401 }
  402 
  403 void
  404 mixer_hwvol_step(device_t dev, int left_step, int right_step)
  405 {
  406         struct snd_mixer *m;
  407         int level, left, right;
  408         struct cdev *pdev;
  409 
  410         pdev = mixer_get_devt(dev);
  411         m = pdev->si_drv1;
  412         snd_mtxlock(m->lock);
  413         if (m->hwvol_muted) {
  414                 m->hwvol_muted = 0;
  415                 level = m->hwvol_mute_level;
  416         } else
  417                 level = mixer_get(m, m->hwvol_mixer);
  418         if (level != -1) {
  419                 left = level & 0xff;
  420                 right = level >> 8;
  421                 left += left_step * m->hwvol_step;
  422                 if (left < 0)
  423                         left = 0;
  424                 right += right_step * m->hwvol_step;
  425                 if (right < 0)
  426                         right = 0;
  427                 mixer_set(m, m->hwvol_mixer, left | right << 8);
  428         }
  429         snd_mtxunlock(m->lock);
  430 }
  431 
  432 /* ----------------------------------------------------------------------- */
  433 
  434 static int
  435 mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
  436 {
  437         struct snd_mixer *m;
  438 
  439         m = i_dev->si_drv1;
  440         snd_mtxlock(m->lock);
  441 
  442         m->busy++;
  443 
  444         snd_mtxunlock(m->lock);
  445         return 0;
  446 }
  447 
  448 static int
  449 mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
  450 {
  451         struct snd_mixer *m;
  452 
  453         m = i_dev->si_drv1;
  454         snd_mtxlock(m->lock);
  455 
  456         if (!m->busy) {
  457                 snd_mtxunlock(m->lock);
  458                 return EBADF;
  459         }
  460         m->busy--;
  461 
  462         snd_mtxunlock(m->lock);
  463         return 0;
  464 }
  465 
  466 int
  467 mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
  468 {
  469         struct snd_mixer *m;
  470         int ret, *arg_i = (int *)arg;
  471         int v = -1, j = cmd & 0xff;
  472 
  473         m = i_dev->si_drv1;
  474 
  475         if (m == NULL)
  476                 return EBADF;
  477 
  478         snd_mtxlock(m->lock);
  479         if (mode != -1 && !m->busy) {
  480                 snd_mtxunlock(m->lock);
  481                 return EBADF;
  482         }
  483 
  484         if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
  485                 if (j == SOUND_MIXER_RECSRC)
  486                         ret = mixer_setrecsrc(m, *arg_i);
  487                 else
  488                         ret = mixer_set(m, j, *arg_i);
  489                 snd_mtxunlock(m->lock);
  490                 return (ret == 0)? 0 : ENXIO;
  491         }
  492 
  493         if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
  494                 switch (j) {
  495                 case SOUND_MIXER_DEVMASK:
  496                 case SOUND_MIXER_CAPS:
  497                 case SOUND_MIXER_STEREODEVS:
  498                         v = mix_getdevs(m);
  499                         break;
  500 
  501                 case SOUND_MIXER_RECMASK:
  502                         v = mix_getrecdevs(m);
  503                         break;
  504 
  505                 case SOUND_MIXER_RECSRC:
  506                         v = mixer_getrecsrc(m);
  507                         break;
  508 
  509                 default:
  510                         v = mixer_get(m, j);
  511                 }
  512                 *arg_i = v;
  513                 snd_mtxunlock(m->lock);
  514                 return (v != -1)? 0 : ENXIO;
  515         }
  516         snd_mtxunlock(m->lock);
  517         return ENXIO;
  518 }
  519 
  520 #ifdef USING_DEVFS
  521 static void
  522 mixer_clone(void *arg, struct ucred *cred, char *name, int namelen,
  523     struct cdev **dev)
  524 {
  525         struct snddev_info *sd;
  526 
  527         if (*dev != NULL)
  528                 return;
  529         if (strcmp(name, "mixer") == 0) {
  530                 sd = devclass_get_softc(pcm_devclass, snd_unit);
  531                 if (sd != NULL && sd->mixer_dev != NULL) {
  532                         *dev = sd->mixer_dev;
  533                         dev_ref(*dev);
  534                 }
  535         }
  536 }
  537 
  538 static void
  539 mixer_sysinit(void *p)
  540 {
  541         mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
  542 }
  543 
  544 static void
  545 mixer_sysuninit(void *p)
  546 {
  547         if (mixer_ehtag != NULL)
  548                 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
  549 }
  550 
  551 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
  552 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
  553 #endif
  554 
  555 

Cache object: d180e303adfda9b6f7f458a965de50cc


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