2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
4 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include <dev/sound/pcm/sound.h>
34 #include <sys/device.h>
35 #include <sys/eventhandler.h>
37 #include "feeder_if.h"
40 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/mixer.c 274035 2014-11-03 11:11:45Z bapt $");
42 static MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
44 static int mixer_bypass = 1;
45 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_mixer_bypass, CTLFLAG_RWTUN,
47 "control channel pcm/rec volume, bypassing real mixer device");
49 #define MIXER_NAMELEN 16
59 u_int32_t hwvol_mute_level;
67 char name[MIXER_NAMELEN];
69 oss_mixer_enuminfo enuminfo;
71 * Counter is incremented when applications change any of this
72 * mixer's controls. A change in value indicates that persistent
73 * mixer applications should update their displays.
78 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
79 [SOUND_MIXER_VOLUME] = 75,
80 [SOUND_MIXER_BASS] = 50,
81 [SOUND_MIXER_TREBLE] = 50,
82 [SOUND_MIXER_SYNTH] = 75,
83 [SOUND_MIXER_PCM] = 75,
84 [SOUND_MIXER_SPEAKER] = 75,
85 [SOUND_MIXER_LINE] = 75,
86 [SOUND_MIXER_MIC] = 0,
87 [SOUND_MIXER_CD] = 75,
88 [SOUND_MIXER_IGAIN] = 0,
89 [SOUND_MIXER_LINE1] = 75,
90 [SOUND_MIXER_VIDEO] = 75,
91 [SOUND_MIXER_RECLEV] = 75,
92 [SOUND_MIXER_OGAIN] = 50,
93 [SOUND_MIXER_MONITOR] = 75,
96 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
98 static d_open_t mixer_open;
99 static d_close_t mixer_close;
100 static d_ioctl_t mixer_ioctl;
102 static struct dev_ops mixer_cdevsw = {
103 { "mixer", 0, D_TRACKCLOSE },
104 .d_open = mixer_open,
105 .d_close = mixer_close,
106 .d_ioctl = mixer_ioctl,
110 * Keeps a count of mixer devices; used only by OSSv4 SNDCTL_SYSINFO ioctl.
114 static eventhandler_tag mixer_ehtag = NULL;
117 mixer_get_devt(device_t dev)
119 struct snddev_info *snddev;
121 snddev = device_get_softc(dev);
123 return snddev->mixer_dev;
127 mixer_lookup(char *devname)
131 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
132 if (strncmp(devname, snd_mixernames[i],
133 strlen(snd_mixernames[i])) == 0)
138 #define MIXER_SET_UNLOCK(x, y) do { \
140 snd_mtxunlock((x)->lock); \
143 #define MIXER_SET_LOCK(x, y) do { \
145 snd_mtxlock((x)->lock); \
149 mixer_set_softpcmvol(struct snd_mixer *m, struct snddev_info *d,
150 u_int left, u_int right)
152 struct pcm_channel *c;
153 int dropmtx, acquiremtx;
155 if (!PCM_REGISTERED(d))
158 if (lockstatus(m->lock, curthread))
163 if (!(d->flags & SD_F_MPSAFE) || lockstatus(d->lock, curthread) == 0)
169 * Be careful here. If we're coming from cdev ioctl, it is OK to
170 * not doing locking AT ALL (except on individual channel) since
171 * we've been heavily guarded by pcm cv, or if we're still
172 * under Giant influence. Since we also have mix_* calls, we cannot
173 * assume such protection and just do the lock as usuall.
175 MIXER_SET_UNLOCK(m, dropmtx);
176 MIXER_SET_LOCK(d, acquiremtx);
178 CHN_FOREACH(c, d, channels.pcm.busy) {
180 if (c->direction == PCMDIR_PLAY &&
181 (c->feederflags & (1 << FEEDER_VOLUME)))
182 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right,
183 (left + right) >> 1);
187 MIXER_SET_UNLOCK(d, acquiremtx);
188 MIXER_SET_LOCK(m, dropmtx);
194 mixer_set_eq(struct snd_mixer *m, struct snddev_info *d,
195 u_int dev, u_int level)
197 struct pcm_channel *c;
198 struct pcm_feeder *f;
199 int tone, dropmtx, acquiremtx;
201 if (dev == SOUND_MIXER_TREBLE)
202 tone = FEEDEQ_TREBLE;
203 else if (dev == SOUND_MIXER_BASS)
208 if (!PCM_REGISTERED(d))
211 if (lockstatus(m->lock, curthread))
216 if (!(d->flags & SD_F_MPSAFE) || lockstatus(d->lock, curthread) == 0)
222 * Be careful here. If we're coming from cdev ioctl, it is OK to
223 * not doing locking AT ALL (except on individual channel) since
224 * we've been heavily guarded by pcm cv, or if we're still
225 * under Giant influence. Since we also have mix_* calls, we cannot
226 * assume such protection and just do the lock as usuall.
228 MIXER_SET_UNLOCK(m, dropmtx);
229 MIXER_SET_LOCK(d, acquiremtx);
231 CHN_FOREACH(c, d, channels.pcm.busy) {
233 f = chn_findfeeder(c, FEEDER_EQ);
235 (void)FEEDER_SET(f, tone, level);
239 MIXER_SET_UNLOCK(d, acquiremtx);
240 MIXER_SET_LOCK(m, dropmtx);
246 mixer_set(struct snd_mixer *m, u_int dev, u_int lev)
248 struct snddev_info *d;
250 u_int32_t parent = SOUND_MIXER_NONE, child = 0;
254 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
255 (0 == (m->devs & (1 << dev))))
258 l = min((lev & 0x00ff), 100);
259 r = min(((lev & 0xff00) >> 8), 100);
260 realdev = m->realdev[dev];
262 d = device_get_softc(m->dev);
266 /* It is safe to drop this mutex due to Giant. */
267 if (!(d->flags & SD_F_MPSAFE) && lockstatus(m->lock, curthread) == 0)
272 MIXER_SET_UNLOCK(m, dropmtx);
274 /* TODO: recursive handling */
275 parent = m->parent[dev];
276 if (parent >= SOUND_MIXER_NRDEVICES)
277 parent = SOUND_MIXER_NONE;
278 if (parent == SOUND_MIXER_NONE)
279 child = m->child[dev];
281 if (parent != SOUND_MIXER_NONE) {
282 tl = (l * (m->level[parent] & 0x00ff)) / 100;
283 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
284 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
285 (void)mixer_set_softpcmvol(m, d, tl, tr);
286 else if (realdev != SOUND_MIXER_NONE &&
287 MIXER_SET(m, realdev, tl, tr) < 0) {
288 MIXER_SET_LOCK(m, dropmtx);
291 } else if (child != 0) {
292 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
293 if (!(child & (1 << i)) || m->parent[i] != dev)
295 realdev = m->realdev[i];
296 tl = (l * (m->level[i] & 0x00ff)) / 100;
297 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
298 if (i == SOUND_MIXER_PCM &&
299 (d->flags & SD_F_SOFTPCMVOL))
300 (void)mixer_set_softpcmvol(m, d, tl, tr);
301 else if (realdev != SOUND_MIXER_NONE)
302 MIXER_SET(m, realdev, tl, tr);
304 realdev = m->realdev[dev];
305 if (realdev != SOUND_MIXER_NONE &&
306 MIXER_SET(m, realdev, l, r) < 0) {
307 MIXER_SET_LOCK(m, dropmtx);
311 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
312 (void)mixer_set_softpcmvol(m, d, l, r);
313 else if ((dev == SOUND_MIXER_TREBLE ||
314 dev == SOUND_MIXER_BASS) && (d->flags & SD_F_EQ))
315 (void)mixer_set_eq(m, d, dev, (l + r) >> 1);
316 else if (realdev != SOUND_MIXER_NONE &&
317 MIXER_SET(m, realdev, l, r) < 0) {
318 MIXER_SET_LOCK(m, dropmtx);
323 MIXER_SET_LOCK(m, dropmtx);
325 m->level[dev] = l | (r << 8);
331 mixer_get(struct snd_mixer *mixer, int dev)
333 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
334 return mixer->level[dev];
340 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
342 struct snddev_info *d;
346 d = device_get_softc(mixer->dev);
349 if (!(d->flags & SD_F_MPSAFE) && lockstatus(mixer->lock, curthread) == 0)
353 src &= mixer->recdevs;
355 src = mixer->recdevs & SOUND_MASK_MIC;
357 src = mixer->recdevs & SOUND_MASK_MONITOR;
359 src = mixer->recdevs & SOUND_MASK_LINE;
360 if (src == 0 && mixer->recdevs != 0)
361 src = (1 << (ffs(mixer->recdevs) - 1));
362 /* It is safe to drop this mutex due to Giant. */
363 MIXER_SET_UNLOCK(mixer, dropmtx);
364 recsrc = MIXER_SETRECSRC(mixer, src);
365 MIXER_SET_LOCK(mixer, dropmtx);
367 mixer->recsrc = recsrc;
373 mixer_getrecsrc(struct snd_mixer *mixer)
375 return mixer->recsrc;
379 * @brief Retrieve the route number of the current recording device
381 * OSSv4 assigns routing numbers to recording devices, unlike the previous
382 * API which relied on a fixed table of device numbers and names. This
383 * function returns the routing number of the device currently selected
386 * For now, this function is kind of a goofy compatibility stub atop the
387 * existing sound system. (For example, in theory, the old sound system
388 * allows multiple recording devices to be specified via a bitmask.)
390 * @param m mixer context container thing
393 * @retval EIDRM no recording device found (generally not possible)
394 * @todo Ask about error code
397 mixer_get_recroute(struct snd_mixer *m, int *route)
403 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
404 /** @todo can user set a multi-device mask? (== or &?) */
405 if ((1 << i) == m->recsrc)
407 if ((1 << i) & m->recdevs)
411 if (i == SOUND_MIXER_NRDEVICES)
419 * @brief Select a device for recording
421 * This function sets a recording source based on a recording device's
422 * routing number. Said number is translated to an old school recdev
423 * mask and passed over mixer_setrecsrc.
425 * @param m mixer context container thing
427 * @retval 0 success(?)
428 * @retval EINVAL User specified an invalid device number
429 * @retval otherwise error from mixer_setrecsrc
432 mixer_set_recroute(struct snd_mixer *m, int route)
439 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
440 if ((1 << i) & m->recdevs) {
447 if (i == SOUND_MIXER_NRDEVICES)
450 ret = mixer_setrecsrc(m, (1 << i));
456 mix_setdevs(struct snd_mixer *m, u_int32_t v)
458 struct snddev_info *d;
464 d = device_get_softc(m->dev);
465 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
467 if (d != NULL && (d->flags & SD_F_EQ))
468 v |= SOUND_MASK_TREBLE | SOUND_MASK_BASS;
469 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
470 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
471 v |= 1 << m->parent[i];
478 * @brief Record mask of available recording devices
480 * Calling functions are responsible for defining the mask of available
481 * recording devices. This function records that value in a structure
482 * used by the rest of the mixer code.
484 * This function also populates a structure used by the SNDCTL_DSP_*RECSRC*
485 * family of ioctls that are part of OSSV4. All recording device labels
486 * are concatenated in ascending order corresponding to their routing
487 * numbers. (Ex: a system might have 0 => 'vol', 1 => 'cd', 2 => 'line',
488 * etc.) For now, these labels are just the standard recording device
489 * names (cd, line1, etc.), but will eventually be fully dynamic and user
492 * @param m mixer device context container thing
493 * @param v mask of recording devices
496 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
498 oss_mixer_enuminfo *ei;
500 int i, nvalues, nwrote, nleft, ncopied;
506 nleft = sizeof(ei->strings);
509 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
511 ei->strindex[nvalues] = nwrote;
512 ncopied = strlcpy(loc, snd_mixernames[i], nleft) + 1;
513 /* strlcpy retval doesn't include terminator */
520 * XXX I don't think this should ever be possible.
521 * Even with a move to dynamic device/channel names,
522 * each label is limited to ~16 characters, so that'd
523 * take a LOT to fill this buffer.
525 if ((nleft <= 0) || (nvalues >= OSS_ENUM_MAXVALUE)) {
526 device_printf(m->dev,
527 "mix_setrecdevs: Not enough room to store device names--please file a bug report.\n");
528 device_printf(m->dev,
529 "mix_setrecdevs: Please include details about your sound hardware, OS version, etc.\n");
533 loc = &ei->strings[nwrote];
538 * NB: The SNDCTL_DSP_GET_RECSRC_NAMES ioctl ignores the dev
541 ei->nvalues = nvalues;
546 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
551 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
553 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
556 if (childs & (1 << i)) {
558 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
559 m->child[m->parent[i]] &= ~(1 << i);
560 m->parent[i] = parent;
564 mask &= ~(1 << parent);
565 m->child[parent] = mask;
569 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
571 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
572 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
574 m->realdev[dev] = realdev;
578 mix_getparent(struct snd_mixer *m, u_int32_t dev)
580 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
581 return SOUND_MIXER_NONE;
582 return m->parent[dev];
586 mix_getchild(struct snd_mixer *m, u_int32_t dev)
588 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
590 return m->child[dev];
594 mix_getdevs(struct snd_mixer *m)
600 mix_getrecdevs(struct snd_mixer *m)
606 mix_getdevinfo(struct snd_mixer *m)
611 static struct snd_mixer *
612 mixer_obj_create(device_t dev, kobj_class_t cls, void *devinfo,
613 int type, const char *desc)
618 KASSERT(dev != NULL && cls != NULL && devinfo != NULL,
619 ("%s(): NULL data dev=%p cls=%p devinfo=%p",
620 __func__, dev, cls, devinfo));
621 KASSERT(type == MIXER_TYPE_PRIMARY || type == MIXER_TYPE_SECONDARY,
622 ("invalid mixer type=%d", type));
624 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
625 ksnprintf(m->name, sizeof(m->name), "%s:mixer",
626 device_get_nameunit(dev));
628 strlcat(m->name, ":", sizeof(m->name));
629 strlcat(m->name, desc, sizeof(m->name));
631 m->lock = snd_mtxcreate(m->name, (type == MIXER_TYPE_PRIMARY) ?
632 "primary pcm mixer" : "secondary pcm mixer");
634 m->devinfo = devinfo;
637 for (i = 0; i < (sizeof(m->parent) / sizeof(m->parent[0])); i++) {
638 m->parent[i] = SOUND_MIXER_NONE;
644 snd_mtxlock(m->lock);
645 snd_mtxfree(m->lock);
646 kobj_delete((kobj_t)m, M_MIXER);
654 mixer_delete(struct snd_mixer *m)
656 KASSERT(m != NULL, ("NULL snd_mixer"));
657 KASSERT(m->type == MIXER_TYPE_SECONDARY,
658 ("%s(): illegal mixer type=%d", __func__, m->type));
660 /* mixer uninit can sleep --hps */
664 snd_mtxfree(m->lock);
665 kobj_delete((kobj_t)m, M_MIXER);
673 mixer_create(device_t dev, kobj_class_t cls, void *devinfo, const char *desc)
677 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_SECONDARY, desc);
686 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
688 struct snddev_info *snddev;
692 int i, unit, devunit, val;
694 snddev = device_get_softc(dev);
698 if (resource_int_value(device_get_name(dev),
699 device_get_unit(dev), "eq", &val) == 0 && val != 0) {
700 snddev->flags |= SD_F_EQ;
701 if ((val & SD_F_EQ_MASK) == val)
702 snddev->flags |= val;
704 snddev->flags |= SD_F_EQ_DEFAULT;
705 snddev->eqpreamp = 0;
708 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_PRIMARY, NULL);
712 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
713 v = snd_mixerdefaults[i];
715 if (resource_int_value(device_get_name(dev),
716 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
717 if (val >= 0 && val <= 100) {
722 mixer_set(m, i, v | (v << 8));
725 mixer_setrecsrc(m, 0); /* Set default input. */
727 unit = device_get_unit(dev);
728 devunit = snd_mkunit(unit, SND_DEV_CTL, 0);
729 pdev = make_dev(&mixer_cdevsw, PCMMINOR(devunit),
730 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
732 snddev->mixer_dev = pdev;
737 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
738 if (!(m->devs & (1 << i)))
740 if (m->realdev[i] != i) {
741 device_printf(dev, "Mixer \"%s\" -> \"%s\":",
743 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
744 snd_mixernames[m->realdev[i]] : "none");
746 device_printf(dev, "Mixer \"%s\":",
749 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
750 kprintf(" parent=\"%s\"",
751 snd_mixernames[m->parent[i]]);
752 if (m->child[i] != 0)
753 kprintf(" child=0x%08x", m->child[i]);
756 if (snddev->flags & SD_F_SOFTPCMVOL)
757 device_printf(dev, "Soft PCM mixer ENABLED\n");
758 if (snddev->flags & SD_F_EQ)
759 device_printf(dev, "EQ Treble/Bass ENABLED\n");
766 mixer_uninit(device_t dev)
769 struct snddev_info *d;
773 d = device_get_softc(dev);
774 pdev = mixer_get_devt(dev);
775 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
779 KASSERT(m != NULL, ("NULL snd_mixer"));
780 KASSERT(m->type == MIXER_TYPE_PRIMARY,
781 ("%s(): illegal mixer type=%d", __func__, m->type));
783 snd_mtxlock(m->lock);
786 snd_mtxunlock(m->lock);
790 /* destroy dev can sleep --hps */
792 snd_mtxunlock(m->lock);
794 pdev->si_drv1 = NULL;
797 snd_mtxlock(m->lock);
799 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
802 mixer_setrecsrc(m, SOUND_MASK_MIC);
804 snd_mtxunlock(m->lock);
806 /* mixer uninit can sleep --hps */
810 snd_mtxfree(m->lock);
811 kobj_delete((kobj_t)m, M_MIXER);
821 mixer_reinit(device_t dev)
827 pdev = mixer_get_devt(dev);
829 snd_mtxlock(m->lock);
833 snd_mtxunlock(m->lock);
837 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
838 mixer_set(m, i, m->level[i]);
840 mixer_setrecsrc(m, m->recsrc);
841 snd_mtxunlock(m->lock);
847 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
854 snd_mtxlock(m->lock);
855 strlcpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
856 snd_mtxunlock(m->lock);
857 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
858 snd_mtxlock(m->lock);
859 if (error == 0 && req->newptr != NULL) {
860 dev = mixer_lookup(devname);
862 snd_mtxunlock(m->lock);
865 else if (dev != m->hwvol_mixer) {
866 m->hwvol_mixer = dev;
870 snd_mtxunlock(m->lock);
875 mixer_hwvol_init(device_t dev)
880 pdev = mixer_get_devt(dev);
883 m->hwvol_mixer = SOUND_MIXER_VOLUME;
885 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
886 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
887 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
888 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
889 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
890 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
891 sysctl_hw_snd_hwvol_mixer, "A", "");
896 mixer_hwvol_mute_locked(struct snd_mixer *m)
898 if (m->hwvol_muted) {
900 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
903 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
904 mixer_set(m, m->hwvol_mixer, 0);
909 mixer_hwvol_mute(device_t dev)
914 pdev = mixer_get_devt(dev);
916 snd_mtxlock(m->lock);
917 mixer_hwvol_mute_locked(m);
918 snd_mtxunlock(m->lock);
922 mixer_hwvol_step_locked(struct snd_mixer *m, int left_step, int right_step)
924 int level, left, right;
926 if (m->hwvol_muted) {
928 level = m->hwvol_mute_level;
930 level = mixer_get(m, m->hwvol_mixer);
933 right = (level >> 8) & 0xff;
934 left += left_step * m->hwvol_step;
939 right += right_step * m->hwvol_step;
942 else if (right > 100)
944 mixer_set(m, m->hwvol_mixer, left | right << 8);
949 mixer_hwvol_step(device_t dev, int left_step, int right_step)
954 pdev = mixer_get_devt(dev);
956 snd_mtxlock(m->lock);
957 mixer_hwvol_step_locked(m, left_step, right_step);
958 snd_mtxunlock(m->lock);
962 mixer_busy(struct snd_mixer *m)
964 KASSERT(m != NULL, ("NULL snd_mixer"));
970 mix_set(struct snd_mixer *m, u_int dev, u_int left, u_int right)
974 KASSERT(m != NULL, ("NULL snd_mixer"));
976 snd_mtxlock(m->lock);
977 ret = mixer_set(m, dev, left | (right << 8));
978 snd_mtxunlock(m->lock);
980 return ((ret != 0) ? ENXIO : 0);
984 mix_get(struct snd_mixer *m, u_int dev)
988 KASSERT(m != NULL, ("NULL snd_mixer"));
990 snd_mtxlock(m->lock);
991 ret = mixer_get(m, dev);
992 snd_mtxunlock(m->lock);
998 mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
1002 KASSERT(m != NULL, ("NULL snd_mixer"));
1004 snd_mtxlock(m->lock);
1005 ret = mixer_setrecsrc(m, src);
1006 snd_mtxunlock(m->lock);
1008 return ((ret != 0) ? ENXIO : 0);
1012 mix_getrecsrc(struct snd_mixer *m)
1016 KASSERT(m != NULL, ("NULL snd_mixer"));
1018 snd_mtxlock(m->lock);
1019 ret = mixer_getrecsrc(m);
1020 snd_mtxunlock(m->lock);
1026 mix_get_type(struct snd_mixer *m)
1028 KASSERT(m != NULL, ("NULL snd_mixer"));
1033 /* ----------------------------------------------------------------------- */
1036 mixer_open(struct dev_open_args *ap)
1038 struct cdev *i_dev = ap->a_head.a_dev;
1039 struct snddev_info *d;
1040 struct snd_mixer *m;
1043 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1047 d = device_get_softc(m->dev);
1048 if (!PCM_REGISTERED(d))
1051 /* XXX Need Giant magic entry ??? */
1053 snd_mtxlock(m->lock);
1055 snd_mtxunlock(m->lock);
1061 mixer_close(struct dev_close_args *ap)
1063 struct cdev *i_dev = ap->a_head.a_dev;
1064 struct snddev_info *d;
1065 struct snd_mixer *m;
1068 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1072 d = device_get_softc(m->dev);
1073 if (!PCM_REGISTERED(d))
1076 /* XXX Need Giant magic entry ??? */
1078 snd_mtxlock(m->lock);
1079 ret = (m->busy == 0) ? EBADF : 0;
1081 snd_mtxunlock(m->lock);
1087 mixer_ioctl_channel(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
1088 struct thread *td, int from)
1090 struct snddev_info *d;
1091 struct snd_mixer *m;
1092 struct pcm_channel *c, *rdch, *wrch;
1096 if (td == NULL || td->td_proc == NULL)
1100 d = device_get_softc(m->dev);
1104 case SOUND_MIXER_PCM:
1105 case SOUND_MIXER_RECLEV:
1106 case SOUND_MIXER_DEVMASK:
1107 case SOUND_MIXER_CAPS:
1108 case SOUND_MIXER_STEREODEVS:
1115 pid = td->td_proc->p_pid;
1122 * This is unfair. Imagine single proc opening multiple
1123 * instances of same direction. What we do right now
1124 * is looking for the first matching proc/pid, and just
1125 * that. Nothing more. Consider it done.
1127 * The better approach of controlling specific channel
1128 * pcm or rec volume is by doing mixer ioctl
1129 * (SNDCTL_DSP_[SET|GET][PLAY|REC]VOL / SOUND_MIXER_[PCM|RECLEV]
1130 * on its open fd, rather than cracky mixer bypassing here.
1132 CHN_FOREACH(c, d, channels.pcm.opened) {
1134 if (c->pid != pid ||
1135 !(c->feederflags & (1 << FEEDER_VOLUME))) {
1139 if (rdch == NULL && c->direction == PCMDIR_REC) {
1141 if (j == SOUND_MIXER_RECLEV)
1142 goto mixer_ioctl_channel_proc;
1143 } else if (wrch == NULL && c->direction == PCMDIR_PLAY) {
1145 if (j == SOUND_MIXER_PCM)
1146 goto mixer_ioctl_channel_proc;
1149 if (rdch != NULL && wrch != NULL)
1153 if (rdch == NULL && wrch == NULL)
1156 if ((j == SOUND_MIXER_DEVMASK || j == SOUND_MIXER_CAPS ||
1157 j == SOUND_MIXER_STEREODEVS) &&
1158 (cmd & ~0xff) == MIXER_READ(0)) {
1159 snd_mtxlock(m->lock);
1160 *(int *)arg = mix_getdevs(m);
1161 snd_mtxunlock(m->lock);
1163 *(int *)arg |= SOUND_MASK_RECLEV;
1165 *(int *)arg |= SOUND_MASK_PCM;
1171 mixer_ioctl_channel_proc:
1173 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1176 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1177 int left, right, center;
1179 left = *(int *)arg & 0x7f;
1180 right = (*(int *)arg >> 8) & 0x7f;
1181 center = (left + right) >> 1;
1182 chn_setvolume_multi(c, SND_VOL_C_PCM, left, right, center);
1183 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1184 *(int *)arg = CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FL);
1186 CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1195 mixer_ioctl(struct dev_ioctl_args *ap)
1197 struct cdev *i_dev = ap->a_head.a_dev;
1198 u_long cmd = ap->a_cmd;
1199 caddr_t arg = ap->a_data;
1200 int mode = ap->a_fflag;
1201 struct snddev_info *d;
1204 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1207 d = device_get_softc(((struct snd_mixer *)i_dev->si_drv1)->dev);
1208 if (!PCM_REGISTERED(d))
1212 PCM_ACQUIRE_QUICK(d);
1216 if (mixer_bypass != 0 && (d->flags & SD_F_VPC))
1217 ret = mixer_ioctl_channel(i_dev, cmd, arg, mode, td,
1221 ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td,
1224 PCM_RELEASE_QUICK(d);
1231 mixer_mixerinfo(struct snd_mixer *m, mixer_info *mi)
1233 bzero((void *)mi, sizeof(*mi));
1234 strlcpy(mi->id, m->name, sizeof(mi->id));
1235 strlcpy(mi->name, device_get_desc(m->dev), sizeof(mi->name));
1236 mi->modify_counter = m->modify_counter;
1240 * XXX Make sure you can guarantee concurrency safety before calling this
1241 * function, be it through Giant, PCM_*, etc !
1244 mixer_ioctl_cmd(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1245 struct thread *td, int from)
1247 struct snd_mixer *m;
1248 int ret = EINVAL, *arg_i = (int *)arg;
1249 int v = -1, j = cmd & 0xff;
1252 * Certain ioctls may be made on any type of device (audio, mixer,
1253 * and MIDI). Handle those special cases here.
1255 if (IOCGROUP(cmd) == 'X') {
1257 case SNDCTL_SYSINFO:
1258 sound_oss_sysinfo((oss_sysinfo *)arg);
1260 case SNDCTL_CARDINFO:
1261 return (sound_oss_card_info((oss_card_info *)arg));
1262 case SNDCTL_AUDIOINFO:
1263 case SNDCTL_AUDIOINFO_EX:
1264 case SNDCTL_ENGINEINFO:
1265 return (dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg));
1266 case SNDCTL_MIXERINFO:
1267 return (mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg));
1277 snd_mtxlock(m->lock);
1278 if (from == MIXER_CMD_CDEV && !m->busy) {
1279 snd_mtxunlock(m->lock);
1283 case SNDCTL_DSP_GET_RECSRC_NAMES:
1284 bcopy((void *)&m->enuminfo, arg, sizeof(oss_mixer_enuminfo));
1287 case SNDCTL_DSP_GET_RECSRC:
1288 ret = mixer_get_recroute(m, arg_i);
1290 case SNDCTL_DSP_SET_RECSRC:
1291 ret = mixer_set_recroute(m, *arg_i);
1293 case OSS_GETVERSION:
1294 *arg_i = SOUND_VERSION;
1297 case SOUND_MIXER_INFO:
1298 mixer_mixerinfo(m, (mixer_info *)arg);
1302 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1303 if (j == SOUND_MIXER_RECSRC)
1304 ret = mixer_setrecsrc(m, *arg_i);
1306 ret = mixer_set(m, j, *arg_i);
1307 snd_mtxunlock(m->lock);
1308 return ((ret == 0) ? 0 : ENXIO);
1310 if ((cmd & ~0xff) == MIXER_READ(0)) {
1312 case SOUND_MIXER_DEVMASK:
1313 case SOUND_MIXER_CAPS:
1314 case SOUND_MIXER_STEREODEVS:
1317 case SOUND_MIXER_RECMASK:
1318 v = mix_getrecdevs(m);
1320 case SOUND_MIXER_RECSRC:
1321 v = mixer_getrecsrc(m);
1324 v = mixer_get(m, j);
1327 snd_mtxunlock(m->lock);
1328 return ((v != -1) ? 0 : ENXIO);
1331 snd_mtxunlock(m->lock);
1336 mixer_clone(void *arg,
1337 char *name, int namelen, struct cdev **dev)
1339 struct snddev_info *d;
1343 if (strcmp(name, "mixer") == 0) {
1344 d = devclass_get_softc(pcm_devclass, snd_unit);
1345 if (PCM_REGISTERED(d) && d->mixer_dev != NULL) {
1346 *dev = d->mixer_dev;
1353 mixer_sysinit(void *p)
1355 if (mixer_ehtag != NULL)
1357 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
1361 mixer_sysuninit(void *p)
1363 if (mixer_ehtag == NULL)
1365 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
1369 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
1370 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
1373 * @brief Handler for SNDCTL_MIXERINFO
1375 * This function searches for a mixer based on the numeric ID stored
1376 * in oss_miserinfo::dev. If set to -1, then information about the
1377 * current mixer handling the request is provided. Note, however, that
1378 * this ioctl may be made with any sound device (audio, mixer, midi).
1380 * @note Caller must not hold any PCM device, channel, or mixer locks.
1382 * See http://manuals.opensound.com/developer/SNDCTL_MIXERINFO.html for
1385 * @param i_dev character device on which the ioctl arrived
1386 * @param arg user argument (oss_mixerinfo *)
1388 * @retval EINVAL oss_mixerinfo::dev specified a bad value
1392 mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi)
1394 struct snddev_info *d;
1395 struct snd_mixer *m;
1399 * If probing the device handling the ioctl, make sure it's a mixer
1400 * device. (This ioctl is valid on audio, mixer, and midi devices.)
1402 if (mi->dev == -1 && i_dev->si_ops != &mixer_cdevsw)
1410 * There's a 1:1 relationship between mixers and PCM devices, so
1411 * begin by iterating over PCM devices and search for our mixer.
1413 for (i = 0; pcm_devclass != NULL &&
1414 i < devclass_get_maxunit(pcm_devclass); i++) {
1415 d = devclass_get_softc(pcm_devclass, i);
1416 if (!PCM_REGISTERED(d))
1419 /* XXX Need Giant magic entry */
1421 /* See the note in function docblock. */
1422 PCM_UNLOCKASSERT(d);
1425 if (d->mixer_dev != NULL && d->mixer_dev->si_drv1 != NULL &&
1426 ((mi->dev == -1 && d->mixer_dev == i_dev) ||
1428 m = d->mixer_dev->si_drv1;
1429 lockmgr(m->lock, LK_EXCLUSIVE);
1432 * At this point, the following synchronization stuff
1434 * - a specific PCM device is locked.
1435 * - a specific mixer device has been locked, so be
1436 * sure to unlock when existing.
1438 bzero((void *)mi, sizeof(*mi));
1440 ksnprintf(mi->id, sizeof(mi->id), "mixer%d", i);
1441 strlcpy(mi->name, m->name, sizeof(mi->name));
1442 mi->modify_counter = m->modify_counter;
1443 mi->card_number = i;
1445 * Currently, FreeBSD assumes 1:1 relationship between
1446 * a pcm and mixer devices, so this is hardcoded to 0.
1448 mi->port_number = 0;
1451 * @todo Fill in @sa oss_mixerinfo::mixerhandle.
1452 * @note From 4Front: "mixerhandle is an arbitrary
1453 * string that identifies the mixer better than
1454 * the device number (mixerinfo.dev). Device
1455 * numbers may change depending on the order the
1456 * drivers are loaded. However the handle should
1457 * remain the same provided that the sound card
1458 * is not moved to another PCI slot."
1463 * @sa oss_mixerinfo::magic is a reserved field.
1466 * From 4Front: "magic is usually 0. However some
1467 * devices may have dedicated setup utilities and the
1468 * magic field may contain an unique driver specific
1469 * value (managed by [4Front])."
1472 mi->enabled = device_is_attached(m->dev) ? 1 : 0;
1474 * The only flag for @sa oss_mixerinfo::caps is
1475 * currently MIXER_CAP_VIRTUAL, which I'm not sure we
1476 * really worry about.
1479 * Mixer extensions currently aren't supported, so
1480 * leave @sa oss_mixerinfo::nrext blank for now.
1483 * @todo Fill in @sa oss_mixerinfo::priority (requires
1484 * touching drivers?)
1485 * @note The priority field is for mixer applets to
1486 * determine which mixer should be the default, with 0
1487 * being least preferred and 10 being most preferred.
1488 * From 4Front: "OSS drivers like ICH use higher
1489 * values (10) because such chips are known to be used
1490 * only on motherboards. Drivers for high end pro
1491 * devices use 0 because they will never be the
1492 * default mixer. Other devices use values 1 to 9
1493 * depending on the estimated probability of being the
1496 * XXX Described by Hannu@4Front, but not found in
1498 strlcpy(mi->devnode, devtoname(d->mixer_dev),
1499 sizeof(mi->devnode));
1500 mi->legacy_device = i;
1502 lockmgr(m->lock, LK_RELEASE);
1516 * Allow the sound driver to use the mixer lock to protect its mixer
1520 mixer_get_lock(struct snd_mixer *m)
1522 KASSERT(m->lock != NULL, ("mixer_get_lock() called with NULL parameter"));
1527 mix_get_locked(struct snd_mixer *m, u_int dev, int *pleft, int *pright)
1531 level = mixer_get(m, dev);
1533 *pright = *pleft = -1;
1537 *pleft = level & 0xFF;
1538 *pright = (level >> 8) & 0xFF;
1544 mix_set_locked(struct snd_mixer *m, u_int dev, int left, int right)
1548 level = (left & 0xFF) | ((right & 0xFF) << 8);
1550 return (mixer_set(m, dev, level));