2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include <dev/sound/pcm/sound.h>
37 #include "feeder_if.h"
40 SND_DECLARE_FILE("$FreeBSD$");
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 cdevsw mixer_cdevsw = {
103 .d_version = D_VERSION,
104 .d_open = mixer_open,
105 .d_close = mixer_close,
106 .d_ioctl = mixer_ioctl,
111 * Keeps a count of mixer devices; used only by OSSv4 SNDCTL_SYSINFO ioctl.
115 static eventhandler_tag mixer_ehtag = NULL;
118 mixer_get_devt(device_t dev)
120 struct snddev_info *snddev;
122 snddev = device_get_softc(dev);
124 return snddev->mixer_dev;
128 mixer_lookup(char *devname)
132 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
133 if (strncmp(devname, snd_mixernames[i],
134 strlen(snd_mixernames[i])) == 0)
139 #define MIXER_SET_UNLOCK(x, y) do { \
141 snd_mtxunlock((x)->lock); \
144 #define MIXER_SET_LOCK(x, y) do { \
146 snd_mtxlock((x)->lock); \
150 mixer_set_softpcmvol(struct snd_mixer *m, struct snddev_info *d,
151 u_int left, u_int right)
153 struct pcm_channel *c;
154 int dropmtx, acquiremtx;
156 if (!PCM_REGISTERED(d))
159 if (mtx_owned(m->lock))
164 if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 0)
170 * Be careful here. If we're coming from cdev ioctl, it is OK to
171 * not doing locking AT ALL (except on individual channel) since
172 * we've been heavily guarded by pcm cv, or if we're still
173 * under Giant influence. Since we also have mix_* calls, we cannot
174 * assume such protection and just do the lock as usuall.
176 MIXER_SET_UNLOCK(m, dropmtx);
177 MIXER_SET_LOCK(d, acquiremtx);
179 CHN_FOREACH(c, d, channels.pcm.busy) {
181 if (c->direction == PCMDIR_PLAY &&
182 (c->feederflags & (1 << FEEDER_VOLUME)))
183 chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right,
184 (left + right) >> 1);
188 MIXER_SET_UNLOCK(d, acquiremtx);
189 MIXER_SET_LOCK(m, dropmtx);
195 mixer_set_eq(struct snd_mixer *m, struct snddev_info *d,
196 u_int dev, u_int level)
198 struct pcm_channel *c;
199 struct pcm_feeder *f;
200 int tone, dropmtx, acquiremtx;
202 if (dev == SOUND_MIXER_TREBLE)
203 tone = FEEDEQ_TREBLE;
204 else if (dev == SOUND_MIXER_BASS)
209 if (!PCM_REGISTERED(d))
212 if (mtx_owned(m->lock))
217 if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 0)
223 * Be careful here. If we're coming from cdev ioctl, it is OK to
224 * not doing locking AT ALL (except on individual channel) since
225 * we've been heavily guarded by pcm cv, or if we're still
226 * under Giant influence. Since we also have mix_* calls, we cannot
227 * assume such protection and just do the lock as usuall.
229 MIXER_SET_UNLOCK(m, dropmtx);
230 MIXER_SET_LOCK(d, acquiremtx);
232 CHN_FOREACH(c, d, channels.pcm.busy) {
234 f = chn_findfeeder(c, FEEDER_EQ);
236 (void)FEEDER_SET(f, tone, level);
240 MIXER_SET_UNLOCK(d, acquiremtx);
241 MIXER_SET_LOCK(m, dropmtx);
247 mixer_set(struct snd_mixer *m, u_int dev, u_int lev)
249 struct snddev_info *d;
251 u_int32_t parent = SOUND_MIXER_NONE, child = 0;
255 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
256 (0 == (m->devs & (1 << dev))))
259 l = min((lev & 0x00ff), 100);
260 r = min(((lev & 0xff00) >> 8), 100);
261 realdev = m->realdev[dev];
263 d = device_get_softc(m->dev);
267 /* It is safe to drop this mutex due to Giant. */
268 if (!(d->flags & SD_F_MPSAFE) && mtx_owned(m->lock) != 0)
273 MIXER_SET_UNLOCK(m, dropmtx);
275 /* TODO: recursive handling */
276 parent = m->parent[dev];
277 if (parent >= SOUND_MIXER_NRDEVICES)
278 parent = SOUND_MIXER_NONE;
279 if (parent == SOUND_MIXER_NONE)
280 child = m->child[dev];
282 if (parent != SOUND_MIXER_NONE) {
283 tl = (l * (m->level[parent] & 0x00ff)) / 100;
284 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
285 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
286 (void)mixer_set_softpcmvol(m, d, tl, tr);
287 else if (realdev != SOUND_MIXER_NONE &&
288 MIXER_SET(m, realdev, tl, tr) < 0) {
289 MIXER_SET_LOCK(m, dropmtx);
292 } else if (child != 0) {
293 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
294 if (!(child & (1 << i)) || m->parent[i] != dev)
296 realdev = m->realdev[i];
297 tl = (l * (m->level[i] & 0x00ff)) / 100;
298 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
299 if (i == SOUND_MIXER_PCM &&
300 (d->flags & SD_F_SOFTPCMVOL))
301 (void)mixer_set_softpcmvol(m, d, tl, tr);
302 else if (realdev != SOUND_MIXER_NONE)
303 MIXER_SET(m, realdev, tl, tr);
305 realdev = m->realdev[dev];
306 if (realdev != SOUND_MIXER_NONE &&
307 MIXER_SET(m, realdev, l, r) < 0) {
308 MIXER_SET_LOCK(m, dropmtx);
312 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
313 (void)mixer_set_softpcmvol(m, d, l, r);
314 else if ((dev == SOUND_MIXER_TREBLE ||
315 dev == SOUND_MIXER_BASS) && (d->flags & SD_F_EQ))
316 (void)mixer_set_eq(m, d, dev, (l + r) >> 1);
317 else if (realdev != SOUND_MIXER_NONE &&
318 MIXER_SET(m, realdev, l, r) < 0) {
319 MIXER_SET_LOCK(m, dropmtx);
324 MIXER_SET_LOCK(m, dropmtx);
326 m->level[dev] = l | (r << 8);
333 mixer_get(struct snd_mixer *mixer, int dev)
335 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
336 return mixer->level[dev];
342 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
344 struct snddev_info *d;
348 d = device_get_softc(mixer->dev);
351 if (!(d->flags & SD_F_MPSAFE) && mtx_owned(mixer->lock) != 0)
355 src &= mixer->recdevs;
357 src = mixer->recdevs & SOUND_MASK_MIC;
359 src = mixer->recdevs & SOUND_MASK_MONITOR;
361 src = mixer->recdevs & SOUND_MASK_LINE;
362 if (src == 0 && mixer->recdevs != 0)
363 src = (1 << (ffs(mixer->recdevs) - 1));
364 /* It is safe to drop this mutex due to Giant. */
365 MIXER_SET_UNLOCK(mixer, dropmtx);
366 recsrc = MIXER_SETRECSRC(mixer, src);
367 MIXER_SET_LOCK(mixer, dropmtx);
369 mixer->recsrc = recsrc;
375 mixer_getrecsrc(struct snd_mixer *mixer)
377 return mixer->recsrc;
381 * @brief Retrieve the route number of the current recording device
383 * OSSv4 assigns routing numbers to recording devices, unlike the previous
384 * API which relied on a fixed table of device numbers and names. This
385 * function returns the routing number of the device currently selected
388 * For now, this function is kind of a goofy compatibility stub atop the
389 * existing sound system. (For example, in theory, the old sound system
390 * allows multiple recording devices to be specified via a bitmask.)
392 * @param m mixer context container thing
395 * @retval EIDRM no recording device found (generally not possible)
396 * @todo Ask about error code
399 mixer_get_recroute(struct snd_mixer *m, int *route)
405 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
406 /** @todo can user set a multi-device mask? (== or &?) */
407 if ((1 << i) == m->recsrc)
409 if ((1 << i) & m->recdevs)
413 if (i == SOUND_MIXER_NRDEVICES)
421 * @brief Select a device for recording
423 * This function sets a recording source based on a recording device's
424 * routing number. Said number is translated to an old school recdev
425 * mask and passed over mixer_setrecsrc.
427 * @param m mixer context container thing
429 * @retval 0 success(?)
430 * @retval EINVAL User specified an invalid device number
431 * @retval otherwise error from mixer_setrecsrc
434 mixer_set_recroute(struct snd_mixer *m, int route)
441 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
442 if ((1 << i) & m->recdevs) {
449 if (i == SOUND_MIXER_NRDEVICES)
452 ret = mixer_setrecsrc(m, (1 << i));
458 mix_setdevs(struct snd_mixer *m, u_int32_t v)
460 struct snddev_info *d;
466 d = device_get_softc(m->dev);
467 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
469 if (d != NULL && (d->flags & SD_F_EQ))
470 v |= SOUND_MASK_TREBLE | SOUND_MASK_BASS;
471 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
472 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
473 v |= 1 << m->parent[i];
480 * @brief Record mask of available recording devices
482 * Calling functions are responsible for defining the mask of available
483 * recording devices. This function records that value in a structure
484 * used by the rest of the mixer code.
486 * This function also populates a structure used by the SNDCTL_DSP_*RECSRC*
487 * family of ioctls that are part of OSSV4. All recording device labels
488 * are concatenated in ascending order corresponding to their routing
489 * numbers. (Ex: a system might have 0 => 'vol', 1 => 'cd', 2 => 'line',
490 * etc.) For now, these labels are just the standard recording device
491 * names (cd, line1, etc.), but will eventually be fully dynamic and user
494 * @param m mixer device context container thing
495 * @param v mask of recording devices
498 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
500 oss_mixer_enuminfo *ei;
502 int i, nvalues, nwrote, nleft, ncopied;
508 nleft = sizeof(ei->strings);
511 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
513 ei->strindex[nvalues] = nwrote;
514 ncopied = strlcpy(loc, snd_mixernames[i], nleft) + 1;
515 /* strlcpy retval doesn't include terminator */
522 * XXX I don't think this should ever be possible.
523 * Even with a move to dynamic device/channel names,
524 * each label is limited to ~16 characters, so that'd
525 * take a LOT to fill this buffer.
527 if ((nleft <= 0) || (nvalues >= OSS_ENUM_MAXVALUE)) {
528 device_printf(m->dev,
529 "mix_setrecdevs: Not enough room to store device names--please file a bug report.\n");
530 device_printf(m->dev,
531 "mix_setrecdevs: Please include details about your sound hardware, OS version, etc.\n");
535 loc = &ei->strings[nwrote];
540 * NB: The SNDCTL_DSP_GET_RECSRC_NAMES ioctl ignores the dev
543 ei->nvalues = nvalues;
548 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
553 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
555 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
558 if (childs & (1 << i)) {
560 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
561 m->child[m->parent[i]] &= ~(1 << i);
562 m->parent[i] = parent;
566 mask &= ~(1 << parent);
567 m->child[parent] = mask;
571 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
573 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
574 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
576 m->realdev[dev] = realdev;
580 mix_getparent(struct snd_mixer *m, u_int32_t dev)
582 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
583 return SOUND_MIXER_NONE;
584 return m->parent[dev];
588 mix_getchild(struct snd_mixer *m, u_int32_t dev)
590 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
592 return m->child[dev];
596 mix_getdevs(struct snd_mixer *m)
602 mix_getrecdevs(struct snd_mixer *m)
608 mix_getdevinfo(struct snd_mixer *m)
613 static struct snd_mixer *
614 mixer_obj_create(device_t dev, kobj_class_t cls, void *devinfo,
615 int type, const char *desc)
620 KASSERT(dev != NULL && cls != NULL && devinfo != NULL,
621 ("%s(): NULL data dev=%p cls=%p devinfo=%p",
622 __func__, dev, cls, devinfo));
623 KASSERT(type == MIXER_TYPE_PRIMARY || type == MIXER_TYPE_SECONDARY,
624 ("invalid mixer type=%d", type));
626 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
627 snprintf(m->name, sizeof(m->name), "%s:mixer",
628 device_get_nameunit(dev));
630 strlcat(m->name, ":", sizeof(m->name));
631 strlcat(m->name, desc, sizeof(m->name));
633 m->lock = snd_mtxcreate(m->name, (type == MIXER_TYPE_PRIMARY) ?
634 "primary pcm mixer" : "secondary pcm mixer");
636 m->devinfo = devinfo;
639 for (i = 0; i < (sizeof(m->parent) / sizeof(m->parent[0])); i++) {
640 m->parent[i] = SOUND_MIXER_NONE;
646 snd_mtxlock(m->lock);
647 snd_mtxfree(m->lock);
648 kobj_delete((kobj_t)m, M_MIXER);
656 mixer_delete(struct snd_mixer *m)
658 KASSERT(m != NULL, ("NULL snd_mixer"));
659 KASSERT(m->type == MIXER_TYPE_SECONDARY,
660 ("%s(): illegal mixer type=%d", __func__, m->type));
662 /* mixer uninit can sleep --hps */
666 snd_mtxfree(m->lock);
667 kobj_delete((kobj_t)m, M_MIXER);
675 mixer_create(device_t dev, kobj_class_t cls, void *devinfo, const char *desc)
679 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_SECONDARY, desc);
688 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
690 struct snddev_info *snddev;
694 int i, unit, devunit, val;
696 snddev = device_get_softc(dev);
700 if (resource_int_value(device_get_name(dev),
701 device_get_unit(dev), "eq", &val) == 0 && val != 0) {
702 snddev->flags |= SD_F_EQ;
703 if ((val & SD_F_EQ_MASK) == val)
704 snddev->flags |= val;
706 snddev->flags |= SD_F_EQ_DEFAULT;
707 snddev->eqpreamp = 0;
710 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_PRIMARY, NULL);
714 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
715 v = snd_mixerdefaults[i];
717 if (resource_int_value(device_get_name(dev),
718 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
719 if (val >= 0 && val <= 100) {
724 mixer_set(m, i, v | (v << 8));
727 mixer_setrecsrc(m, 0); /* Set default input. */
729 unit = device_get_unit(dev);
730 devunit = snd_mkunit(unit, SND_DEV_CTL, 0);
731 pdev = make_dev(&mixer_cdevsw, PCMMINOR(devunit),
732 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
734 snddev->mixer_dev = pdev;
739 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
740 if (!(m->devs & (1 << i)))
742 if (m->realdev[i] != i) {
743 device_printf(dev, "Mixer \"%s\" -> \"%s\":",
745 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
746 snd_mixernames[m->realdev[i]] : "none");
748 device_printf(dev, "Mixer \"%s\":",
751 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
752 printf(" parent=\"%s\"",
753 snd_mixernames[m->parent[i]]);
754 if (m->child[i] != 0)
755 printf(" child=0x%08x", m->child[i]);
758 if (snddev->flags & SD_F_SOFTPCMVOL)
759 device_printf(dev, "Soft PCM mixer ENABLED\n");
760 if (snddev->flags & SD_F_EQ)
761 device_printf(dev, "EQ Treble/Bass ENABLED\n");
768 mixer_uninit(device_t dev)
771 struct snddev_info *d;
775 d = device_get_softc(dev);
776 pdev = mixer_get_devt(dev);
777 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
781 KASSERT(m != NULL, ("NULL snd_mixer"));
782 KASSERT(m->type == MIXER_TYPE_PRIMARY,
783 ("%s(): illegal mixer type=%d", __func__, m->type));
785 snd_mtxlock(m->lock);
788 snd_mtxunlock(m->lock);
792 /* destroy dev can sleep --hps */
794 snd_mtxunlock(m->lock);
796 pdev->si_drv1 = NULL;
799 snd_mtxlock(m->lock);
801 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
804 mixer_setrecsrc(m, SOUND_MASK_MIC);
806 snd_mtxunlock(m->lock);
808 /* mixer uninit can sleep --hps */
812 snd_mtxfree(m->lock);
813 kobj_delete((kobj_t)m, M_MIXER);
823 mixer_reinit(device_t dev)
829 pdev = mixer_get_devt(dev);
831 snd_mtxlock(m->lock);
835 snd_mtxunlock(m->lock);
839 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
840 mixer_set(m, i, m->level[i]);
842 mixer_setrecsrc(m, m->recsrc);
843 snd_mtxunlock(m->lock);
849 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
856 snd_mtxlock(m->lock);
857 strlcpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
858 snd_mtxunlock(m->lock);
859 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
860 snd_mtxlock(m->lock);
861 if (error == 0 && req->newptr != NULL) {
862 dev = mixer_lookup(devname);
864 snd_mtxunlock(m->lock);
867 else if (dev != m->hwvol_mixer) {
868 m->hwvol_mixer = dev;
872 snd_mtxunlock(m->lock);
877 mixer_hwvol_init(device_t dev)
882 pdev = mixer_get_devt(dev);
885 m->hwvol_mixer = SOUND_MIXER_VOLUME;
887 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
888 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
889 OID_AUTO, "hwvol_step", CTLFLAG_RWTUN, &m->hwvol_step, 0, "");
890 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
891 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
892 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RWTUN, m, 0,
893 sysctl_hw_snd_hwvol_mixer, "A", "");
898 mixer_hwvol_mute_locked(struct snd_mixer *m)
900 if (m->hwvol_muted) {
902 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
905 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
906 mixer_set(m, m->hwvol_mixer, 0);
911 mixer_hwvol_mute(device_t dev)
916 pdev = mixer_get_devt(dev);
918 snd_mtxlock(m->lock);
919 mixer_hwvol_mute_locked(m);
920 snd_mtxunlock(m->lock);
924 mixer_hwvol_step_locked(struct snd_mixer *m, int left_step, int right_step)
926 int level, left, right;
928 if (m->hwvol_muted) {
930 level = m->hwvol_mute_level;
932 level = mixer_get(m, m->hwvol_mixer);
935 right = (level >> 8) & 0xff;
936 left += left_step * m->hwvol_step;
941 right += right_step * m->hwvol_step;
944 else if (right > 100)
946 mixer_set(m, m->hwvol_mixer, left | right << 8);
951 mixer_hwvol_step(device_t dev, int left_step, int right_step)
956 pdev = mixer_get_devt(dev);
958 snd_mtxlock(m->lock);
959 mixer_hwvol_step_locked(m, left_step, right_step);
960 snd_mtxunlock(m->lock);
964 mixer_busy(struct snd_mixer *m)
966 KASSERT(m != NULL, ("NULL snd_mixer"));
972 mix_set(struct snd_mixer *m, u_int dev, u_int left, u_int right)
976 KASSERT(m != NULL, ("NULL snd_mixer"));
978 snd_mtxlock(m->lock);
979 ret = mixer_set(m, dev, left | (right << 8));
980 snd_mtxunlock(m->lock);
982 return ((ret != 0) ? ENXIO : 0);
986 mix_get(struct snd_mixer *m, u_int dev)
990 KASSERT(m != NULL, ("NULL snd_mixer"));
992 snd_mtxlock(m->lock);
993 ret = mixer_get(m, dev);
994 snd_mtxunlock(m->lock);
1000 mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
1004 KASSERT(m != NULL, ("NULL snd_mixer"));
1006 snd_mtxlock(m->lock);
1007 ret = mixer_setrecsrc(m, src);
1008 snd_mtxunlock(m->lock);
1010 return ((ret != 0) ? ENXIO : 0);
1014 mix_getrecsrc(struct snd_mixer *m)
1018 KASSERT(m != NULL, ("NULL snd_mixer"));
1020 snd_mtxlock(m->lock);
1021 ret = mixer_getrecsrc(m);
1022 snd_mtxunlock(m->lock);
1028 mix_get_type(struct snd_mixer *m)
1030 KASSERT(m != NULL, ("NULL snd_mixer"));
1035 /* ----------------------------------------------------------------------- */
1038 mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
1040 struct snddev_info *d;
1041 struct snd_mixer *m;
1044 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1048 d = device_get_softc(m->dev);
1049 if (!PCM_REGISTERED(d))
1052 /* XXX Need Giant magic entry ??? */
1054 snd_mtxlock(m->lock);
1056 snd_mtxunlock(m->lock);
1062 mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
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 cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1198 struct snddev_info *d;
1201 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1204 d = device_get_softc(((struct snd_mixer *)i_dev->si_drv1)->dev);
1205 if (!PCM_REGISTERED(d))
1209 PCM_ACQUIRE_QUICK(d);
1213 if (mixer_bypass != 0 && (d->flags & SD_F_VPC))
1214 ret = mixer_ioctl_channel(i_dev, cmd, arg, mode, td,
1218 ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td,
1221 PCM_RELEASE_QUICK(d);
1228 mixer_mixerinfo(struct snd_mixer *m, mixer_info *mi)
1230 bzero((void *)mi, sizeof(*mi));
1231 strlcpy(mi->id, m->name, sizeof(mi->id));
1232 strlcpy(mi->name, device_get_desc(m->dev), sizeof(mi->name));
1233 mi->modify_counter = m->modify_counter;
1237 * XXX Make sure you can guarantee concurrency safety before calling this
1238 * function, be it through Giant, PCM_*, etc !
1241 mixer_ioctl_cmd(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1242 struct thread *td, int from)
1244 struct snd_mixer *m;
1245 int ret = EINVAL, *arg_i = (int *)arg;
1246 int v = -1, j = cmd & 0xff;
1249 * Certain ioctls may be made on any type of device (audio, mixer,
1250 * and MIDI). Handle those special cases here.
1252 if (IOCGROUP(cmd) == 'X') {
1254 case SNDCTL_SYSINFO:
1255 sound_oss_sysinfo((oss_sysinfo *)arg);
1257 case SNDCTL_CARDINFO:
1258 return (sound_oss_card_info((oss_card_info *)arg));
1259 case SNDCTL_AUDIOINFO:
1260 case SNDCTL_AUDIOINFO_EX:
1261 case SNDCTL_ENGINEINFO:
1262 return (dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg));
1263 case SNDCTL_MIXERINFO:
1264 return (mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg));
1274 snd_mtxlock(m->lock);
1275 if (from == MIXER_CMD_CDEV && !m->busy) {
1276 snd_mtxunlock(m->lock);
1280 case SNDCTL_DSP_GET_RECSRC_NAMES:
1281 bcopy((void *)&m->enuminfo, arg, sizeof(oss_mixer_enuminfo));
1284 case SNDCTL_DSP_GET_RECSRC:
1285 ret = mixer_get_recroute(m, arg_i);
1287 case SNDCTL_DSP_SET_RECSRC:
1288 ret = mixer_set_recroute(m, *arg_i);
1290 case OSS_GETVERSION:
1291 *arg_i = SOUND_VERSION;
1294 case SOUND_MIXER_INFO:
1295 mixer_mixerinfo(m, (mixer_info *)arg);
1299 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1300 if (j == SOUND_MIXER_RECSRC)
1301 ret = mixer_setrecsrc(m, *arg_i);
1303 ret = mixer_set(m, j, *arg_i);
1304 snd_mtxunlock(m->lock);
1305 return ((ret == 0) ? 0 : ENXIO);
1307 if ((cmd & ~0xff) == MIXER_READ(0)) {
1309 case SOUND_MIXER_DEVMASK:
1310 case SOUND_MIXER_CAPS:
1311 case SOUND_MIXER_STEREODEVS:
1314 case SOUND_MIXER_RECMASK:
1315 v = mix_getrecdevs(m);
1317 case SOUND_MIXER_RECSRC:
1318 v = mixer_getrecsrc(m);
1321 v = mixer_get(m, j);
1324 snd_mtxunlock(m->lock);
1325 return ((v != -1) ? 0 : ENXIO);
1328 snd_mtxunlock(m->lock);
1333 mixer_clone(void *arg,
1335 char *name, int namelen, struct cdev **dev)
1337 struct snddev_info *d;
1341 if (strcmp(name, "mixer") == 0) {
1342 d = devclass_get_softc(pcm_devclass, snd_unit);
1343 if (PCM_REGISTERED(d) && d->mixer_dev != NULL) {
1344 *dev = d->mixer_dev;
1351 mixer_sysinit(void *p)
1353 if (mixer_ehtag != NULL)
1355 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
1359 mixer_sysuninit(void *p)
1361 if (mixer_ehtag == NULL)
1363 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
1367 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
1368 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
1371 * @brief Handler for SNDCTL_MIXERINFO
1373 * This function searches for a mixer based on the numeric ID stored
1374 * in oss_miserinfo::dev. If set to -1, then information about the
1375 * current mixer handling the request is provided. Note, however, that
1376 * this ioctl may be made with any sound device (audio, mixer, midi).
1378 * @note Caller must not hold any PCM device, channel, or mixer locks.
1380 * See http://manuals.opensound.com/developer/SNDCTL_MIXERINFO.html for
1383 * @param i_dev character device on which the ioctl arrived
1384 * @param arg user argument (oss_mixerinfo *)
1386 * @retval EINVAL oss_mixerinfo::dev specified a bad value
1390 mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi)
1392 struct snddev_info *d;
1393 struct snd_mixer *m;
1397 * If probing the device handling the ioctl, make sure it's a mixer
1398 * device. (This ioctl is valid on audio, mixer, and midi devices.)
1400 if (mi->dev == -1 && i_dev->si_devsw != &mixer_cdevsw)
1408 * There's a 1:1 relationship between mixers and PCM devices, so
1409 * begin by iterating over PCM devices and search for our mixer.
1411 for (i = 0; pcm_devclass != NULL &&
1412 i < devclass_get_maxunit(pcm_devclass); i++) {
1413 d = devclass_get_softc(pcm_devclass, i);
1414 if (!PCM_REGISTERED(d))
1417 /* XXX Need Giant magic entry */
1419 /* See the note in function docblock. */
1420 PCM_UNLOCKASSERT(d);
1423 if (d->mixer_dev != NULL && d->mixer_dev->si_drv1 != NULL &&
1424 ((mi->dev == -1 && d->mixer_dev == i_dev) ||
1426 m = d->mixer_dev->si_drv1;
1430 * At this point, the following synchronization stuff
1432 * - a specific PCM device is locked.
1433 * - a specific mixer device has been locked, so be
1434 * sure to unlock when existing.
1436 bzero((void *)mi, sizeof(*mi));
1438 snprintf(mi->id, sizeof(mi->id), "mixer%d", i);
1439 strlcpy(mi->name, m->name, sizeof(mi->name));
1440 mi->modify_counter = m->modify_counter;
1441 mi->card_number = i;
1443 * Currently, FreeBSD assumes 1:1 relationship between
1444 * a pcm and mixer devices, so this is hardcoded to 0.
1446 mi->port_number = 0;
1449 * @todo Fill in @sa oss_mixerinfo::mixerhandle.
1450 * @note From 4Front: "mixerhandle is an arbitrary
1451 * string that identifies the mixer better than
1452 * the device number (mixerinfo.dev). Device
1453 * numbers may change depending on the order the
1454 * drivers are loaded. However the handle should
1455 * remain the same provided that the sound card
1456 * is not moved to another PCI slot."
1461 * @sa oss_mixerinfo::magic is a reserved field.
1464 * From 4Front: "magic is usually 0. However some
1465 * devices may have dedicated setup utilities and the
1466 * magic field may contain an unique driver specific
1467 * value (managed by [4Front])."
1470 mi->enabled = device_is_attached(m->dev) ? 1 : 0;
1472 * The only flag for @sa oss_mixerinfo::caps is
1473 * currently MIXER_CAP_VIRTUAL, which I'm not sure we
1474 * really worry about.
1477 * Mixer extensions currently aren't supported, so
1478 * leave @sa oss_mixerinfo::nrext blank for now.
1481 * @todo Fill in @sa oss_mixerinfo::priority (requires
1482 * touching drivers?)
1483 * @note The priority field is for mixer applets to
1484 * determine which mixer should be the default, with 0
1485 * being least preferred and 10 being most preferred.
1486 * From 4Front: "OSS drivers like ICH use higher
1487 * values (10) because such chips are known to be used
1488 * only on motherboards. Drivers for high end pro
1489 * devices use 0 because they will never be the
1490 * default mixer. Other devices use values 1 to 9
1491 * depending on the estimated probability of being the
1494 * XXX Described by Hannu@4Front, but not found in
1496 strlcpy(mi->devnode, devtoname(d->mixer_dev),
1497 sizeof(mi->devnode));
1498 mi->legacy_device = i;
1500 mtx_unlock(m->lock);
1514 * Allow the sound driver to use the mixer lock to protect its mixer
1518 mixer_get_lock(struct snd_mixer *m)
1520 if (m->lock == NULL) {
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));