2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/sound/pcm/mixer.c,v 1.43.2.5 2007/05/13 20:53:39 ariff Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.18 2007/06/22 21:41:16 corecode Exp $
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/pcm/dsp.h>
35 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.18 2007/06/22 21:41:16 corecode Exp $");
37 MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
39 #define MIXER_NAMELEN 16
49 u_int32_t hwvol_mute_level;
57 char name[MIXER_NAMELEN];
61 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
62 [SOUND_MIXER_VOLUME] = 75,
63 [SOUND_MIXER_BASS] = 50,
64 [SOUND_MIXER_TREBLE] = 50,
65 [SOUND_MIXER_SYNTH] = 75,
66 [SOUND_MIXER_PCM] = 75,
67 [SOUND_MIXER_SPEAKER] = 75,
68 [SOUND_MIXER_LINE] = 75,
69 [SOUND_MIXER_MIC] = 0,
70 [SOUND_MIXER_CD] = 75,
71 [SOUND_MIXER_IGAIN] = 0,
72 [SOUND_MIXER_LINE1] = 75,
73 [SOUND_MIXER_VIDEO] = 75,
74 [SOUND_MIXER_RECLEV] = 0,
75 [SOUND_MIXER_OGAIN] = 50,
76 [SOUND_MIXER_MONITOR] = 75,
79 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
81 static d_open_t mixer_open;
82 static d_close_t mixer_close;
84 static struct dev_ops mixer_cdevsw = {
85 { "mixer", SND_CDEV_MAJOR, D_TRACKCLOSE },
86 /* .d_flags = D_TRACKCLOSE | D_NEEDGIANT, */
88 .d_close = mixer_close,
89 .d_ioctl = mixer_ioctl,
93 static eventhandler_tag mixer_ehtag;
97 mixer_get_devt(device_t dev)
99 struct snddev_info *snddev;
101 snddev = device_get_softc(dev);
103 return snddev->mixer_dev;
108 mixer_lookup(char *devname)
112 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
113 if (strncmp(devname, snd_mixernames[i],
114 strlen(snd_mixernames[i])) == 0)
121 * Always called with mixer->lock held.
124 mixer_set_softpcmvol(struct snd_mixer *mixer, struct snddev_info *d,
125 unsigned left, unsigned right)
127 struct snddev_channel *sce;
128 struct pcm_channel *ch;
130 snd_mtxunlock(mixer->lock);
131 SLIST_FOREACH(sce, &d->channels, link) {
134 if (ch->direction == PCMDIR_PLAY &&
135 (ch->feederflags & (1 << FEEDER_VOLUME)))
136 chn_setvolume(ch, left, right);
139 snd_mtxlock(mixer->lock);
144 mixer_set(struct snd_mixer *m, unsigned dev, unsigned lev)
146 struct snddev_info *d;
147 unsigned l, r, tl, tr;
148 u_int32_t parent = SOUND_MIXER_NONE, child = 0;
152 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
153 (0 == (m->devs & (1 << dev))))
156 l = min((lev & 0x00ff), 100);
157 r = min(((lev & 0xff00) >> 8), 100);
158 realdev = m->realdev[dev];
160 d = device_get_softc(m->dev);
164 /* TODO: recursive handling */
165 parent = m->parent[dev];
166 if (parent >= SOUND_MIXER_NRDEVICES)
167 parent = SOUND_MIXER_NONE;
168 if (parent == SOUND_MIXER_NONE)
169 child = m->child[dev];
171 if (parent != SOUND_MIXER_NONE) {
172 tl = (l * (m->level[parent] & 0x00ff)) / 100;
173 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
174 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
175 mixer_set_softpcmvol(m, d, tl, tr);
176 else if (realdev != SOUND_MIXER_NONE &&
177 MIXER_SET(m, realdev, tl, tr) < 0)
179 } else if (child != 0) {
180 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
181 if (!(child & (1 << i)) || m->parent[i] != dev)
183 realdev = m->realdev[i];
184 tl = (l * (m->level[i] & 0x00ff)) / 100;
185 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
186 if (i == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
187 mixer_set_softpcmvol(m, d, tl, tr);
188 else if (realdev != SOUND_MIXER_NONE)
189 MIXER_SET(m, realdev, tl, tr);
191 realdev = m->realdev[dev];
192 if (realdev != SOUND_MIXER_NONE &&
193 MIXER_SET(m, realdev, l, r) < 0)
196 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
197 mixer_set_softpcmvol(m, d, l, r);
198 else if (realdev != SOUND_MIXER_NONE &&
199 MIXER_SET(m, realdev, l, r) < 0)
203 m->level[dev] = l | (r << 8);
209 mixer_get(struct snd_mixer *mixer, int dev)
211 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
212 return mixer->level[dev];
217 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
219 src &= mixer->recdevs;
221 src = SOUND_MASK_MIC;
222 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
227 mixer_getrecsrc(struct snd_mixer *mixer)
229 return mixer->recsrc;
233 mix_setdevs(struct snd_mixer *m, u_int32_t v)
235 struct snddev_info *d;
241 d = device_get_softc(m->dev);
242 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
244 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
245 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
246 v |= 1 << m->parent[i];
253 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
259 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
264 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
266 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
269 if (childs & (1 << i)) {
271 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
272 m->child[m->parent[i]] &= ~(1 << i);
273 m->parent[i] = parent;
277 mask &= ~(1 << parent);
278 m->child[parent] = mask;
282 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
284 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
285 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
287 m->realdev[dev] = realdev;
291 mix_getparent(struct snd_mixer *m, u_int32_t dev)
293 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
294 return SOUND_MIXER_NONE;
295 return m->parent[dev];
299 mix_getchild(struct snd_mixer *m, u_int32_t dev)
301 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
303 return m->child[dev];
307 mix_getdevs(struct snd_mixer *m)
313 mix_getrecdevs(struct snd_mixer *m)
319 mix_getdevinfo(struct snd_mixer *m)
325 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
327 struct snddev_info *snddev;
333 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
334 ksnprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
335 m->lock = snd_mtxcreate(m->name, "pcm mixer");
337 m->devinfo = devinfo;
340 for (i = 0; i < 32; i++) {
341 m->parent[i] = SOUND_MIXER_NONE;
349 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
350 v = snd_mixerdefaults[i];
352 if (resource_int_value(device_get_name(dev),
353 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
354 if (val >= 0 && val <= 100) {
359 snd_mtxlock(m->lock);
360 mixer_set(m, i, v | (v << 8));
361 snd_mtxunlock(m->lock);
364 mixer_setrecsrc(m, SOUND_MASK_MIC);
366 unit = device_get_unit(dev);
367 dev_ops_add(&mixer_cdevsw, -1, PCMMKMINOR(unit, SND_DEV_CTL, 0));
368 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
369 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
372 snddev = device_get_softc(dev);
373 snddev->mixer_dev = pdev;
376 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
377 if (!(m->devs & (1 << i)))
379 if (m->realdev[i] != i) {
380 device_printf(dev, "Mixer \"%s\" -> \"%s\":",
382 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
383 snd_mixernames[m->realdev[i]] : "none");
385 device_printf(dev, "Mixer \"%s\":",
388 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
389 kprintf(" parent=\"%s\"",
390 snd_mixernames[m->parent[i]]);
391 if (m->child[i] != 0)
392 kprintf(" child=0x%08x", m->child[i]);
395 if (snddev->flags & SD_F_SOFTPCMVOL)
396 device_printf(dev, "Soft PCM mixer ENABLED\n");
402 snd_mtxfree(m->lock);
403 kobj_delete((kobj_t)m, M_MIXER);
408 mixer_uninit(device_t dev)
412 struct snddev_info *d;
416 d = device_get_softc(dev);
417 pdev = mixer_get_devt(dev);
418 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
421 snd_mtxlock(m->lock);
424 snd_mtxunlock(m->lock);
428 pdev->si_drv1 = NULL;
430 unit = device_get_unit(dev);
431 dev_ops_remove(&mixer_cdevsw, -1, PCMMKMINOR(unit, SND_DEV_CTL, 0));
433 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
436 mixer_setrecsrc(m, SOUND_MASK_MIC);
440 snd_mtxunlock(m->lock);
441 snd_mtxfree(m->lock);
442 kobj_delete((kobj_t)m, M_MIXER);
450 mixer_reinit(device_t dev)
456 pdev = mixer_get_devt(dev);
458 snd_mtxlock(m->lock);
462 snd_mtxunlock(m->lock);
466 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
467 mixer_set(m, i, m->level[i]);
469 mixer_setrecsrc(m, m->recsrc);
470 snd_mtxunlock(m->lock);
477 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
484 snd_mtxlock(m->lock);
485 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
486 snd_mtxunlock(m->lock);
487 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
488 snd_mtxlock(m->lock);
489 if (error == 0 && req->newptr != NULL) {
490 dev = mixer_lookup(devname);
492 snd_mtxunlock(m->lock);
495 else if (dev != m->hwvol_mixer) {
496 m->hwvol_mixer = dev;
500 snd_mtxunlock(m->lock);
506 mixer_hwvol_init(device_t dev)
511 pdev = mixer_get_devt(dev);
514 m->hwvol_mixer = SOUND_MIXER_VOLUME;
517 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
518 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
519 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
520 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
521 sysctl_hw_snd_hwvol_mixer, "A", "");
527 mixer_hwvol_mute(device_t dev)
532 pdev = mixer_get_devt(dev);
534 snd_mtxlock(m->lock);
535 if (m->hwvol_muted) {
537 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
540 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
541 mixer_set(m, m->hwvol_mixer, 0);
543 snd_mtxunlock(m->lock);
547 mixer_hwvol_step(device_t dev, int left_step, int right_step)
550 int level, left, right;
553 pdev = mixer_get_devt(dev);
555 snd_mtxlock(m->lock);
556 if (m->hwvol_muted) {
558 level = m->hwvol_mute_level;
560 level = mixer_get(m, m->hwvol_mixer);
564 left += left_step * m->hwvol_step;
567 right += right_step * m->hwvol_step;
570 mixer_set(m, m->hwvol_mixer, left | right << 8);
572 snd_mtxunlock(m->lock);
575 /* ----------------------------------------------------------------------- */
578 vchanvolume(cdev_t i_dev, int write, int *volume, int *ret,
581 struct snddev_info *d;
583 struct pcm_channel *ch;
584 int vol_left, vol_right;
587 d = dsp_get_info(i_dev);
593 * We search for a vchan which is owned by the current process.
595 for (cookie = NULL; (ch = pcm_chn_iterate(d, &cookie)) != NULL;)
596 if (ch->flags & CHN_F_VIRTUAL &&
597 ch->pid == td->td_proc->p_pid)
606 vol_left = min(*volume & 0x00ff, 100);
607 vol_right = min((*volume & 0xff00) >> 8, 100);
608 *ret = chn_setvolume(ch, vol_left, vol_right);
610 *volume = ch->volume;
617 /* ----------------------------------------------------------------------- */
620 mixer_open(struct dev_open_args *ap)
622 struct cdev *i_dev = ap->a_head.a_dev;
626 snd_mtxlock(m->lock);
630 snd_mtxunlock(m->lock);
635 mixer_close(struct dev_close_args *ap)
637 struct cdev *i_dev = ap->a_head.a_dev;
641 snd_mtxlock(m->lock);
644 snd_mtxunlock(m->lock);
649 snd_mtxunlock(m->lock);
654 mixer_ioctl(struct dev_ioctl_args *ap)
656 struct cdev *i_dev = ap->a_head.a_dev;
657 u_long cmd = ap->a_cmd;
658 caddr_t arg = ap->a_data;
659 int mode = ap->a_fflag;
661 int ret, *arg_i = (int *)arg;
662 int v = -1, j = cmd & 0xff;
670 * If we are handling PCM, maybe the app really wants to
671 * set its vchan, and fails to use the correct fd.
673 if (j == SOUND_MIXER_PCM) {
674 if (vchanvolume(i_dev,
675 (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0),
676 (int *)arg, &ret, curthread))
678 /* else proceed as usual */
681 snd_mtxlock(m->lock);
682 if (mode != -1 && !m->busy) {
683 snd_mtxunlock(m->lock);
687 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
688 if (j == SOUND_MIXER_RECSRC)
689 ret = mixer_setrecsrc(m, *arg_i);
691 ret = mixer_set(m, j, *arg_i);
692 snd_mtxunlock(m->lock);
693 return (ret == 0)? 0 : ENXIO;
696 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
698 case SOUND_MIXER_DEVMASK:
699 case SOUND_MIXER_CAPS:
700 case SOUND_MIXER_STEREODEVS:
704 case SOUND_MIXER_RECMASK:
705 v = mix_getrecdevs(m);
708 case SOUND_MIXER_RECSRC:
709 v = mixer_getrecsrc(m);
716 snd_mtxunlock(m->lock);
717 return (v != -1)? 0 : ENXIO;
719 snd_mtxunlock(m->lock);
725 mixer_clone(void *arg, struct ucred *cred, char *name, int namelen,
728 struct snddev_info *sd;
732 if (strcmp(name, "mixer") == 0) {
733 sd = devclass_get_softc(pcm_devclass, snd_unit);
734 if (sd != NULL && sd->mixer_dev != NULL) {
735 *dev = sd->mixer_dev;
742 mixer_sysinit(void *p)
744 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
748 mixer_sysuninit(void *p)
750 if (mixer_ehtag != NULL)
751 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
754 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
755 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);