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 $
29 #include <dev/sound/pcm/sound.h>
30 #include <dev/sound/pcm/dsp.h>
35 MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
37 #define MIXER_NAMELEN 16
47 u_int32_t hwvol_mute_level;
55 char name[MIXER_NAMELEN];
59 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
60 [SOUND_MIXER_VOLUME] = 75,
61 [SOUND_MIXER_BASS] = 50,
62 [SOUND_MIXER_TREBLE] = 50,
63 [SOUND_MIXER_SYNTH] = 75,
64 [SOUND_MIXER_PCM] = 75,
65 [SOUND_MIXER_SPEAKER] = 75,
66 [SOUND_MIXER_LINE] = 75,
67 [SOUND_MIXER_MIC] = 0,
68 [SOUND_MIXER_CD] = 75,
69 [SOUND_MIXER_IGAIN] = 0,
70 [SOUND_MIXER_LINE1] = 75,
71 [SOUND_MIXER_VIDEO] = 75,
72 [SOUND_MIXER_RECLEV] = 0,
73 [SOUND_MIXER_OGAIN] = 50,
74 [SOUND_MIXER_MONITOR] = 75,
77 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
79 static d_open_t mixer_open;
80 static d_close_t mixer_close;
82 static struct dev_ops mixer_cdevsw = {
83 { "mixer", 0, D_TRACKCLOSE },
84 /* .d_flags = D_TRACKCLOSE | D_NEEDGIANT, */
86 .d_close = mixer_close,
87 .d_ioctl = mixer_ioctl,
91 static eventhandler_tag mixer_ehtag;
95 mixer_get_devt(device_t dev)
97 struct snddev_info *snddev;
99 snddev = device_get_softc(dev);
101 return snddev->mixer_dev;
106 mixer_lookup(char *devname)
110 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
111 if (strncmp(devname, snd_mixernames[i],
112 strlen(snd_mixernames[i])) == 0)
119 * Always called with mixer->lock held.
122 mixer_set_softpcmvol(struct snd_mixer *mixer, struct snddev_info *d,
123 unsigned left, unsigned right)
125 struct snddev_channel *sce;
126 struct pcm_channel *ch;
128 snd_mtxunlock(mixer->lock);
129 SLIST_FOREACH(sce, &d->channels, link) {
132 if (ch->direction == PCMDIR_PLAY &&
133 (ch->feederflags & (1 << FEEDER_VOLUME)))
134 chn_setvolume(ch, left, right);
137 snd_mtxlock(mixer->lock);
142 mixer_set(struct snd_mixer *m, unsigned dev, unsigned lev)
144 struct snddev_info *d;
145 unsigned l, r, tl, tr;
146 u_int32_t parent = SOUND_MIXER_NONE, child = 0;
150 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
151 (0 == (m->devs & (1 << dev))))
154 l = min((lev & 0x00ff), 100);
155 r = min(((lev & 0xff00) >> 8), 100);
156 realdev = m->realdev[dev];
158 d = device_get_softc(m->dev);
162 /* TODO: recursive handling */
163 parent = m->parent[dev];
164 if (parent >= SOUND_MIXER_NRDEVICES)
165 parent = SOUND_MIXER_NONE;
166 if (parent == SOUND_MIXER_NONE)
167 child = m->child[dev];
169 if (parent != SOUND_MIXER_NONE) {
170 tl = (l * (m->level[parent] & 0x00ff)) / 100;
171 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
172 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
173 mixer_set_softpcmvol(m, d, tl, tr);
174 else if (realdev != SOUND_MIXER_NONE &&
175 MIXER_SET(m, realdev, tl, tr) < 0)
177 } else if (child != 0) {
178 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
179 if (!(child & (1 << i)) || m->parent[i] != dev)
181 realdev = m->realdev[i];
182 tl = (l * (m->level[i] & 0x00ff)) / 100;
183 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
184 if (i == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
185 mixer_set_softpcmvol(m, d, tl, tr);
186 else if (realdev != SOUND_MIXER_NONE)
187 MIXER_SET(m, realdev, tl, tr);
189 realdev = m->realdev[dev];
190 if (realdev != SOUND_MIXER_NONE &&
191 MIXER_SET(m, realdev, l, r) < 0)
194 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
195 mixer_set_softpcmvol(m, d, l, r);
196 else if (realdev != SOUND_MIXER_NONE &&
197 MIXER_SET(m, realdev, l, r) < 0)
201 m->level[dev] = l | (r << 8);
207 mixer_get(struct snd_mixer *mixer, int dev)
209 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
210 return mixer->level[dev];
215 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
217 src &= mixer->recdevs;
219 src = SOUND_MASK_MIC;
220 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
225 mixer_getrecsrc(struct snd_mixer *mixer)
227 return mixer->recsrc;
231 mix_setdevs(struct snd_mixer *m, u_int32_t v)
233 struct snddev_info *d;
239 d = device_get_softc(m->dev);
240 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
242 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
243 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
244 v |= 1 << m->parent[i];
251 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
257 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
262 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
264 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
267 if (childs & (1 << i)) {
269 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
270 m->child[m->parent[i]] &= ~(1 << i);
271 m->parent[i] = parent;
275 mask &= ~(1 << parent);
276 m->child[parent] = mask;
280 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
282 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
283 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
285 m->realdev[dev] = realdev;
289 mix_getparent(struct snd_mixer *m, u_int32_t dev)
291 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
292 return SOUND_MIXER_NONE;
293 return m->parent[dev];
297 mix_getchild(struct snd_mixer *m, u_int32_t dev)
299 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
301 return m->child[dev];
305 mix_getdevs(struct snd_mixer *m)
311 mix_getrecdevs(struct snd_mixer *m)
317 mix_getdevinfo(struct snd_mixer *m)
323 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
325 struct snddev_info *snddev;
331 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
332 ksnprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
333 m->lock = snd_mtxcreate(m->name, "pcm mixer");
335 m->devinfo = devinfo;
338 for (i = 0; i < 32; i++) {
339 m->parent[i] = SOUND_MIXER_NONE;
347 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
348 v = snd_mixerdefaults[i];
350 if (resource_int_value(device_get_name(dev),
351 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
352 if (val >= 0 && val <= 100) {
357 snd_mtxlock(m->lock);
358 mixer_set(m, i, v | (v << 8));
359 snd_mtxunlock(m->lock);
362 mixer_setrecsrc(m, SOUND_MASK_MIC);
364 unit = device_get_unit(dev);
365 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, 0),
366 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
369 snddev = device_get_softc(dev);
370 snddev->mixer_dev = pdev;
373 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
374 if (!(m->devs & (1 << i)))
376 if (m->realdev[i] != i) {
377 device_printf(dev, "Mixer \"%s\" -> \"%s\":",
379 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
380 snd_mixernames[m->realdev[i]] : "none");
382 device_printf(dev, "Mixer \"%s\":",
385 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
386 kprintf(" parent=\"%s\"",
387 snd_mixernames[m->parent[i]]);
388 if (m->child[i] != 0)
389 kprintf(" child=0x%08x", m->child[i]);
392 if (snddev->flags & SD_F_SOFTPCMVOL)
393 device_printf(dev, "Soft PCM mixer ENABLED\n");
399 snd_mtxfree(m->lock);
400 kobj_delete((kobj_t)m, M_MIXER);
405 mixer_uninit(device_t dev)
409 struct snddev_info *d;
413 d = device_get_softc(dev);
414 pdev = mixer_get_devt(dev);
415 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
418 snd_mtxlock(m->lock);
421 snd_mtxunlock(m->lock);
425 pdev->si_drv1 = NULL;
427 unit = device_get_unit(dev);
428 dev_ops_remove_minor(&mixer_cdevsw, /*-1, */PCMMKMINOR(unit, 0));
430 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
433 mixer_setrecsrc(m, SOUND_MASK_MIC);
437 snd_mtxunlock(m->lock);
438 snd_mtxfree(m->lock);
439 kobj_delete((kobj_t)m, M_MIXER);
447 mixer_reinit(device_t dev)
453 pdev = mixer_get_devt(dev);
455 snd_mtxlock(m->lock);
459 snd_mtxunlock(m->lock);
463 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
464 mixer_set(m, i, m->level[i]);
466 mixer_setrecsrc(m, m->recsrc);
467 snd_mtxunlock(m->lock);
474 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
481 snd_mtxlock(m->lock);
482 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
483 snd_mtxunlock(m->lock);
484 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
485 snd_mtxlock(m->lock);
486 if (error == 0 && req->newptr != NULL) {
487 dev = mixer_lookup(devname);
489 snd_mtxunlock(m->lock);
492 else if (dev != m->hwvol_mixer) {
493 m->hwvol_mixer = dev;
497 snd_mtxunlock(m->lock);
503 mixer_hwvol_init(device_t dev)
508 pdev = mixer_get_devt(dev);
511 m->hwvol_mixer = SOUND_MIXER_VOLUME;
514 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
515 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
516 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
517 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
518 sysctl_hw_snd_hwvol_mixer, "A", "");
524 mixer_hwvol_mute(device_t dev)
529 pdev = mixer_get_devt(dev);
531 snd_mtxlock(m->lock);
532 if (m->hwvol_muted) {
534 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
537 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
538 mixer_set(m, m->hwvol_mixer, 0);
540 snd_mtxunlock(m->lock);
544 mixer_hwvol_step(device_t dev, int left_step, int right_step)
547 int level, left, right;
550 pdev = mixer_get_devt(dev);
552 snd_mtxlock(m->lock);
553 if (m->hwvol_muted) {
555 level = m->hwvol_mute_level;
557 level = mixer_get(m, m->hwvol_mixer);
561 left += left_step * m->hwvol_step;
564 right += right_step * m->hwvol_step;
567 mixer_set(m, m->hwvol_mixer, left | right << 8);
569 snd_mtxunlock(m->lock);
572 /* ----------------------------------------------------------------------- */
575 vchanvolume(cdev_t i_dev, int write, int *volume, int *ret,
578 struct snddev_info *d;
580 struct pcm_channel *ch;
581 int vol_left, vol_right;
584 d = dsp_get_info(i_dev);
590 * We search for a vchan which is owned by the current process.
592 for (cookie = NULL; (ch = pcm_chn_iterate(d, &cookie)) != NULL;)
593 if (ch->flags & CHN_F_VIRTUAL &&
594 ch->pid == td->td_proc->p_pid)
603 vol_left = min(*volume & 0x00ff, 100);
604 vol_right = min((*volume & 0xff00) >> 8, 100);
605 *ret = chn_setvolume(ch, vol_left, vol_right);
607 *volume = ch->volume;
614 /* ----------------------------------------------------------------------- */
617 mixer_open(struct dev_open_args *ap)
619 struct cdev *i_dev = ap->a_head.a_dev;
623 snd_mtxlock(m->lock);
627 snd_mtxunlock(m->lock);
632 mixer_close(struct dev_close_args *ap)
634 struct cdev *i_dev = ap->a_head.a_dev;
638 snd_mtxlock(m->lock);
641 snd_mtxunlock(m->lock);
646 snd_mtxunlock(m->lock);
651 mixer_ioctl(struct dev_ioctl_args *ap)
653 struct cdev *i_dev = ap->a_head.a_dev;
654 u_long cmd = ap->a_cmd;
655 caddr_t arg = ap->a_data;
656 int mode = ap->a_fflag;
658 int ret, *arg_i = (int *)arg;
659 int v = -1, j = cmd & 0xff;
667 * If we are handling PCM, maybe the app really wants to
668 * set its vchan, and fails to use the correct fd.
670 if (j == SOUND_MIXER_PCM && curthread->td_proc) {
671 if (vchanvolume(i_dev,
672 (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0),
673 (int *)arg, &ret, curthread))
675 /* else proceed as usual */
678 snd_mtxlock(m->lock);
679 if (mode != -1 && !m->busy) {
680 snd_mtxunlock(m->lock);
684 if (cmd == OSS_GETVERSION) {
685 *arg_i = SOUND_VERSION;
686 snd_mtxunlock(m->lock);
690 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
691 if (j == SOUND_MIXER_RECSRC)
692 ret = mixer_setrecsrc(m, *arg_i);
694 ret = mixer_set(m, j, *arg_i);
695 snd_mtxunlock(m->lock);
696 return (ret == 0)? 0 : ENXIO;
699 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
701 case SOUND_MIXER_DEVMASK:
702 case SOUND_MIXER_CAPS:
703 case SOUND_MIXER_STEREODEVS:
707 case SOUND_MIXER_RECMASK:
708 v = mix_getrecdevs(m);
711 case SOUND_MIXER_RECSRC:
712 v = mixer_getrecsrc(m);
719 snd_mtxunlock(m->lock);
720 return (v != -1)? 0 : ENXIO;
722 snd_mtxunlock(m->lock);
728 mixer_clone(void *arg, struct ucred *cred, char *name, int namelen,
731 struct snddev_info *sd;
735 if (strcmp(name, "mixer") == 0) {
736 sd = devclass_get_softc(pcm_devclass, snd_unit);
737 if (sd != NULL && sd->mixer_dev != NULL) {
738 *dev = sd->mixer_dev;
745 mixer_sysinit(void *p)
747 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
751 mixer_sysuninit(void *p)
753 if (mixer_ehtag != NULL)
754 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
757 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
758 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);