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>
35 #include "feeder_if.h"
38 SND_DECLARE_FILE("$FreeBSD$");
40 static MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
42 static int mixer_bypass = 1;
43 TUNABLE_INT("hw.snd.vpc_mixer_bypass", &mixer_bypass);
44 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_mixer_bypass, CTLFLAG_RW,
46 "control channel pcm/rec volume, bypassing real mixer device");
48 #define MIXER_NAMELEN 16
58 u_int32_t hwvol_mute_level;
66 char name[MIXER_NAMELEN];
68 oss_mixer_enuminfo enuminfo;
70 * Counter is incremented when applications change any of this
71 * mixer's controls. A change in value indicates that persistent
72 * mixer applications should update their displays.
77 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
78 [SOUND_MIXER_VOLUME] = 75,
79 [SOUND_MIXER_BASS] = 50,
80 [SOUND_MIXER_TREBLE] = 50,
81 [SOUND_MIXER_SYNTH] = 75,
82 [SOUND_MIXER_PCM] = 75,
83 [SOUND_MIXER_SPEAKER] = 75,
84 [SOUND_MIXER_LINE] = 75,
85 [SOUND_MIXER_MIC] = 25,
86 [SOUND_MIXER_CD] = 75,
87 [SOUND_MIXER_IGAIN] = 0,
88 [SOUND_MIXER_LINE1] = 75,
89 [SOUND_MIXER_VIDEO] = 75,
90 [SOUND_MIXER_RECLEV] = 75,
91 [SOUND_MIXER_OGAIN] = 50,
92 [SOUND_MIXER_MONITOR] = 75,
95 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
97 static d_open_t mixer_open;
98 static d_close_t mixer_close;
99 static d_ioctl_t mixer_ioctl;
101 static struct cdevsw mixer_cdevsw = {
102 .d_version = D_VERSION,
103 .d_open = mixer_open,
104 .d_close = mixer_close,
105 .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_DETACHING(d) || !PCM_REGISTERED(d))
158 if (mtx_owned(m->lock))
163 if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 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_DETACHING(d) || !PCM_REGISTERED(d))
211 if (mtx_owned(m->lock))
216 if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 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) && mtx_owned(m->lock) != 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);
332 mixer_get(struct snd_mixer *mixer, int dev)
334 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
335 return mixer->level[dev];
341 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
343 struct snddev_info *d;
347 d = device_get_softc(mixer->dev);
350 if (!(d->flags & SD_F_MPSAFE) && mtx_owned(mixer->lock) != 0)
354 src &= mixer->recdevs;
356 src = mixer->recdevs & SOUND_MASK_MIC;
358 src = mixer->recdevs & SOUND_MASK_MONITOR;
360 src = mixer->recdevs & SOUND_MASK_LINE;
361 if (src == 0 && mixer->recdevs != 0)
362 src = (1 << (ffs(mixer->recdevs) - 1));
363 /* It is safe to drop this mutex due to Giant. */
364 MIXER_SET_UNLOCK(mixer, dropmtx);
365 recsrc = MIXER_SETRECSRC(mixer, src);
366 MIXER_SET_LOCK(mixer, dropmtx);
368 mixer->recsrc = recsrc;
374 mixer_getrecsrc(struct snd_mixer *mixer)
376 return mixer->recsrc;
380 * @brief Retrieve the route number of the current recording device
382 * OSSv4 assigns routing numbers to recording devices, unlike the previous
383 * API which relied on a fixed table of device numbers and names. This
384 * function returns the routing number of the device currently selected
387 * For now, this function is kind of a goofy compatibility stub atop the
388 * existing sound system. (For example, in theory, the old sound system
389 * allows multiple recording devices to be specified via a bitmask.)
391 * @param m mixer context container thing
394 * @retval EIDRM no recording device found (generally not possible)
395 * @todo Ask about error code
398 mixer_get_recroute(struct snd_mixer *m, int *route)
404 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
405 /** @todo can user set a multi-device mask? (== or &?) */
406 if ((1 << i) == m->recsrc)
408 if ((1 << i) & m->recdevs)
412 if (i == SOUND_MIXER_NRDEVICES)
420 * @brief Select a device for recording
422 * This function sets a recording source based on a recording device's
423 * routing number. Said number is translated to an old school recdev
424 * mask and passed over mixer_setrecsrc.
426 * @param m mixer context container thing
428 * @retval 0 success(?)
429 * @retval EINVAL User specified an invalid device number
430 * @retval otherwise error from mixer_setrecsrc
433 mixer_set_recroute(struct snd_mixer *m, int route)
440 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
441 if ((1 << i) & m->recdevs) {
448 if (i == SOUND_MIXER_NRDEVICES)
451 ret = mixer_setrecsrc(m, (1 << i));
457 mix_setdevs(struct snd_mixer *m, u_int32_t v)
459 struct snddev_info *d;
465 d = device_get_softc(m->dev);
466 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
468 if (d != NULL && (d->flags & SD_F_EQ))
469 v |= SOUND_MASK_TREBLE | SOUND_MASK_BASS;
470 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
471 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
472 v |= 1 << m->parent[i];
479 * @brief Record mask of available recording devices
481 * Calling functions are responsible for defining the mask of available
482 * recording devices. This function records that value in a structure
483 * used by the rest of the mixer code.
485 * This function also populates a structure used by the SNDCTL_DSP_*RECSRC*
486 * family of ioctls that are part of OSSV4. All recording device labels
487 * are concatenated in ascending order corresponding to their routing
488 * numbers. (Ex: a system might have 0 => 'vol', 1 => 'cd', 2 => 'line',
489 * etc.) For now, these labels are just the standard recording device
490 * names (cd, line1, etc.), but will eventually be fully dynamic and user
493 * @param m mixer device context container thing
494 * @param v mask of recording devices
497 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
499 oss_mixer_enuminfo *ei;
501 int i, nvalues, nwrote, nleft, ncopied;
507 nleft = sizeof(ei->strings);
510 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
512 ei->strindex[nvalues] = nwrote;
513 ncopied = strlcpy(loc, snd_mixernames[i], nleft) + 1;
514 /* strlcpy retval doesn't include terminator */
521 * XXX I don't think this should ever be possible.
522 * Even with a move to dynamic device/channel names,
523 * each label is limited to ~16 characters, so that'd
524 * take a LOT to fill this buffer.
526 if ((nleft <= 0) || (nvalues >= OSS_ENUM_MAXVALUE)) {
527 device_printf(m->dev,
528 "mix_setrecdevs: Not enough room to store device names--please file a bug report.\n");
529 device_printf(m->dev,
530 "mix_setrecdevs: Please include details about your sound hardware, OS version, etc.\n");
534 loc = &ei->strings[nwrote];
539 * NB: The SNDCTL_DSP_GET_RECSRC_NAMES ioctl ignores the dev
542 ei->nvalues = nvalues;
547 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
552 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
554 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
557 if (childs & (1 << i)) {
559 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
560 m->child[m->parent[i]] &= ~(1 << i);
561 m->parent[i] = parent;
565 mask &= ~(1 << parent);
566 m->child[parent] = mask;
570 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
572 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
573 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
575 m->realdev[dev] = realdev;
579 mix_getparent(struct snd_mixer *m, u_int32_t dev)
581 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
582 return SOUND_MIXER_NONE;
583 return m->parent[dev];
587 mix_getchild(struct snd_mixer *m, u_int32_t dev)
589 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
591 return m->child[dev];
595 mix_getdevs(struct snd_mixer *m)
601 mix_getrecdevs(struct snd_mixer *m)
607 mix_getdevinfo(struct snd_mixer *m)
612 static struct snd_mixer *
613 mixer_obj_create(device_t dev, kobj_class_t cls, void *devinfo,
614 int type, const char *desc)
619 KASSERT(dev != NULL && cls != NULL && devinfo != NULL,
620 ("%s(): NULL data dev=%p cls=%p devinfo=%p",
621 __func__, dev, cls, devinfo));
622 KASSERT(type == MIXER_TYPE_PRIMARY || type == MIXER_TYPE_SECONDARY,
623 ("invalid mixer type=%d", type));
625 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
626 snprintf(m->name, sizeof(m->name), "%s:mixer",
627 device_get_nameunit(dev));
629 strlcat(m->name, ":", sizeof(m->name));
630 strlcat(m->name, desc, sizeof(m->name));
632 m->lock = snd_mtxcreate(m->name, (type == MIXER_TYPE_PRIMARY) ?
633 "primary pcm mixer" : "secondary pcm mixer");
635 m->devinfo = devinfo;
638 for (i = 0; i < (sizeof(m->parent) / sizeof(m->parent[0])); i++) {
639 m->parent[i] = SOUND_MIXER_NONE;
645 snd_mtxlock(m->lock);
646 snd_mtxfree(m->lock);
647 kobj_delete((kobj_t)m, M_MIXER);
655 mixer_delete(struct snd_mixer *m)
657 KASSERT(m != NULL, ("NULL snd_mixer"));
658 KASSERT(m->type == MIXER_TYPE_SECONDARY,
659 ("%s(): illegal mixer type=%d", __func__, m->type));
661 /* mixer uninit can sleep --hps */
665 snd_mtxfree(m->lock);
666 kobj_delete((kobj_t)m, M_MIXER);
674 mixer_create(device_t dev, kobj_class_t cls, void *devinfo, const char *desc)
678 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_SECONDARY, desc);
687 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
689 struct snddev_info *snddev;
693 int i, unit, devunit, val;
695 snddev = device_get_softc(dev);
699 if (resource_int_value(device_get_name(dev),
700 device_get_unit(dev), "eq", &val) == 0 && val != 0) {
701 snddev->flags |= SD_F_EQ;
702 if ((val & SD_F_EQ_MASK) == val)
703 snddev->flags |= val;
705 snddev->flags |= SD_F_EQ_DEFAULT;
706 snddev->eqpreamp = 0;
709 m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_PRIMARY, NULL);
713 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
714 v = snd_mixerdefaults[i];
716 if (resource_int_value(device_get_name(dev),
717 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
718 if (val >= 0 && val <= 100) {
723 mixer_set(m, i, v | (v << 8));
726 mixer_setrecsrc(m, 0); /* Set default input. */
728 unit = device_get_unit(dev);
729 devunit = snd_mkunit(unit, SND_DEV_CTL, 0);
730 pdev = make_dev(&mixer_cdevsw, PCMMINOR(devunit),
731 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
733 snddev->mixer_dev = pdev;
738 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
739 if (!(m->devs & (1 << i)))
741 if (m->realdev[i] != i) {
742 device_printf(dev, "Mixer \"%s\" -> \"%s\":",
744 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
745 snd_mixernames[m->realdev[i]] : "none");
747 device_printf(dev, "Mixer \"%s\":",
750 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
751 printf(" parent=\"%s\"",
752 snd_mixernames[m->parent[i]]);
753 if (m->child[i] != 0)
754 printf(" child=0x%08x", m->child[i]);
757 if (snddev->flags & SD_F_SOFTPCMVOL)
758 device_printf(dev, "Soft PCM mixer ENABLED\n");
759 if (snddev->flags & SD_F_EQ)
760 device_printf(dev, "EQ Treble/Bass ENABLED\n");
767 mixer_uninit(device_t dev)
770 struct snddev_info *d;
774 d = device_get_softc(dev);
775 pdev = mixer_get_devt(dev);
776 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
780 KASSERT(m != NULL, ("NULL snd_mixer"));
781 KASSERT(m->type == MIXER_TYPE_PRIMARY,
782 ("%s(): illegal mixer type=%d", __func__, m->type));
784 snd_mtxlock(m->lock);
787 snd_mtxunlock(m->lock);
791 /* destroy dev can sleep --hps */
793 snd_mtxunlock(m->lock);
795 pdev->si_drv1 = NULL;
798 snd_mtxlock(m->lock);
800 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
803 mixer_setrecsrc(m, SOUND_MASK_MIC);
805 snd_mtxunlock(m->lock);
807 /* mixer uninit can sleep --hps */
811 snd_mtxfree(m->lock);
812 kobj_delete((kobj_t)m, M_MIXER);
822 mixer_reinit(device_t dev)
828 pdev = mixer_get_devt(dev);
830 snd_mtxlock(m->lock);
834 snd_mtxunlock(m->lock);
838 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
839 mixer_set(m, i, m->level[i]);
841 mixer_setrecsrc(m, m->recsrc);
842 snd_mtxunlock(m->lock);
848 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
855 snd_mtxlock(m->lock);
856 strlcpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
857 snd_mtxunlock(m->lock);
858 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
859 snd_mtxlock(m->lock);
860 if (error == 0 && req->newptr != NULL) {
861 dev = mixer_lookup(devname);
863 snd_mtxunlock(m->lock);
866 else if (dev != m->hwvol_mixer) {
867 m->hwvol_mixer = dev;
871 snd_mtxunlock(m->lock);
876 mixer_hwvol_init(device_t dev)
881 pdev = mixer_get_devt(dev);
884 m->hwvol_mixer = SOUND_MIXER_VOLUME;
886 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
887 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
888 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
889 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
890 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
891 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
892 sysctl_hw_snd_hwvol_mixer, "A", "");
897 mixer_hwvol_mute_locked(struct snd_mixer *m)
899 if (m->hwvol_muted) {
901 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
904 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
905 mixer_set(m, m->hwvol_mixer, 0);
910 mixer_hwvol_mute(device_t dev)
915 pdev = mixer_get_devt(dev);
917 snd_mtxlock(m->lock);
918 mixer_hwvol_mute_locked(m);
919 snd_mtxunlock(m->lock);
923 mixer_hwvol_step_locked(struct snd_mixer *m, int left_step, int right_step)
925 int level, left, right;
927 if (m->hwvol_muted) {
929 level = m->hwvol_mute_level;
931 level = mixer_get(m, m->hwvol_mixer);
934 right = (level >> 8) & 0xff;
935 left += left_step * m->hwvol_step;
940 right += right_step * m->hwvol_step;
943 else if (right > 100)
945 mixer_set(m, m->hwvol_mixer, left | right << 8);
950 mixer_hwvol_step(device_t dev, int left_step, int right_step)
955 pdev = mixer_get_devt(dev);
957 snd_mtxlock(m->lock);
958 mixer_hwvol_step_locked(m, left_step, right_step);
959 snd_mtxunlock(m->lock);
963 mixer_busy(struct snd_mixer *m)
965 KASSERT(m != NULL, ("NULL snd_mixer"));
971 mix_set(struct snd_mixer *m, u_int dev, u_int left, u_int right)
975 KASSERT(m != NULL, ("NULL snd_mixer"));
977 snd_mtxlock(m->lock);
978 ret = mixer_set(m, dev, left | (right << 8));
979 snd_mtxunlock(m->lock);
981 return ((ret != 0) ? ENXIO : 0);
985 mix_get(struct snd_mixer *m, u_int dev)
989 KASSERT(m != NULL, ("NULL snd_mixer"));
991 snd_mtxlock(m->lock);
992 ret = mixer_get(m, dev);
993 snd_mtxunlock(m->lock);
999 mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
1003 KASSERT(m != NULL, ("NULL snd_mixer"));
1005 snd_mtxlock(m->lock);
1006 ret = mixer_setrecsrc(m, src);
1007 snd_mtxunlock(m->lock);
1009 return ((ret != 0) ? ENXIO : 0);
1013 mix_getrecsrc(struct snd_mixer *m)
1017 KASSERT(m != NULL, ("NULL snd_mixer"));
1019 snd_mtxlock(m->lock);
1020 ret = mixer_getrecsrc(m);
1021 snd_mtxunlock(m->lock);
1027 mix_get_type(struct snd_mixer *m)
1029 KASSERT(m != NULL, ("NULL snd_mixer"));
1035 mix_get_dev(struct snd_mixer *m)
1037 KASSERT(m != NULL, ("NULL snd_mixer"));
1042 /* ----------------------------------------------------------------------- */
1045 mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
1047 struct snddev_info *d;
1048 struct snd_mixer *m;
1051 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1055 d = device_get_softc(m->dev);
1056 if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
1059 /* XXX Need Giant magic entry ??? */
1061 snd_mtxlock(m->lock);
1063 snd_mtxunlock(m->lock);
1069 mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
1071 struct snddev_info *d;
1072 struct snd_mixer *m;
1075 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1079 d = device_get_softc(m->dev);
1080 if (!PCM_REGISTERED(d))
1083 /* XXX Need Giant magic entry ??? */
1085 snd_mtxlock(m->lock);
1086 ret = (m->busy == 0) ? EBADF : 0;
1088 snd_mtxunlock(m->lock);
1094 mixer_ioctl_channel(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
1095 struct thread *td, int from)
1097 struct snddev_info *d;
1098 struct snd_mixer *m;
1099 struct pcm_channel *c, *rdch, *wrch;
1103 if (td == NULL || td->td_proc == NULL)
1107 d = device_get_softc(m->dev);
1111 case SOUND_MIXER_PCM:
1112 case SOUND_MIXER_RECLEV:
1113 case SOUND_MIXER_DEVMASK:
1114 case SOUND_MIXER_CAPS:
1115 case SOUND_MIXER_STEREODEVS:
1122 pid = td->td_proc->p_pid;
1129 * This is unfair. Imagine single proc opening multiple
1130 * instances of same direction. What we do right now
1131 * is looking for the first matching proc/pid, and just
1132 * that. Nothing more. Consider it done.
1134 * The better approach of controlling specific channel
1135 * pcm or rec volume is by doing mixer ioctl
1136 * (SNDCTL_DSP_[SET|GET][PLAY|REC]VOL / SOUND_MIXER_[PCM|RECLEV]
1137 * on its open fd, rather than cracky mixer bypassing here.
1139 CHN_FOREACH(c, d, channels.pcm.opened) {
1141 if (c->pid != pid ||
1142 !(c->feederflags & (1 << FEEDER_VOLUME))) {
1146 if (rdch == NULL && c->direction == PCMDIR_REC) {
1148 if (j == SOUND_MIXER_RECLEV)
1149 goto mixer_ioctl_channel_proc;
1150 } else if (wrch == NULL && c->direction == PCMDIR_PLAY) {
1152 if (j == SOUND_MIXER_PCM)
1153 goto mixer_ioctl_channel_proc;
1156 if (rdch != NULL && wrch != NULL)
1160 if (rdch == NULL && wrch == NULL)
1163 if ((j == SOUND_MIXER_DEVMASK || j == SOUND_MIXER_CAPS ||
1164 j == SOUND_MIXER_STEREODEVS) &&
1165 (cmd & ~0xff) == MIXER_READ(0)) {
1166 snd_mtxlock(m->lock);
1167 *(int *)arg = mix_getdevs(m);
1168 snd_mtxunlock(m->lock);
1170 *(int *)arg |= SOUND_MASK_RECLEV;
1172 *(int *)arg |= SOUND_MASK_PCM;
1178 mixer_ioctl_channel_proc:
1180 KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1183 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1184 int left, right, center;
1186 left = *(int *)arg & 0x7f;
1187 right = (*(int *)arg >> 8) & 0x7f;
1188 center = (left + right) >> 1;
1189 chn_setvolume_multi(c, SND_VOL_C_PCM, left, right, center);
1190 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1191 *(int *)arg = CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FL);
1193 CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1202 mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1205 struct snddev_info *d;
1208 if (i_dev == NULL || i_dev->si_drv1 == NULL)
1211 d = device_get_softc(((struct snd_mixer *)i_dev->si_drv1)->dev);
1212 if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
1216 PCM_ACQUIRE_QUICK(d);
1220 if (mixer_bypass != 0 && (d->flags & SD_F_VPC))
1221 ret = mixer_ioctl_channel(i_dev, cmd, arg, mode, td,
1225 ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td,
1228 PCM_RELEASE_QUICK(d);
1235 mixer_mixerinfo(struct snd_mixer *m, mixer_info *mi)
1237 bzero((void *)mi, sizeof(*mi));
1238 strlcpy(mi->id, m->name, sizeof(mi->id));
1239 strlcpy(mi->name, device_get_desc(m->dev), sizeof(mi->name));
1240 mi->modify_counter = m->modify_counter;
1244 * XXX Make sure you can guarantee concurrency safety before calling this
1245 * function, be it through Giant, PCM_*, etc !
1248 mixer_ioctl_cmd(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1249 struct thread *td, int from)
1251 struct snd_mixer *m;
1252 int ret = EINVAL, *arg_i = (int *)arg;
1253 int v = -1, j = cmd & 0xff;
1256 * Certain ioctls may be made on any type of device (audio, mixer,
1257 * and MIDI). Handle those special cases here.
1259 if (IOCGROUP(cmd) == 'X') {
1261 case SNDCTL_SYSINFO:
1262 sound_oss_sysinfo((oss_sysinfo *)arg);
1264 case SNDCTL_CARDINFO:
1265 return (sound_oss_card_info((oss_card_info *)arg));
1266 case SNDCTL_AUDIOINFO:
1267 case SNDCTL_AUDIOINFO_EX:
1268 case SNDCTL_ENGINEINFO:
1269 return (dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg));
1270 case SNDCTL_MIXERINFO:
1271 return (mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg));
1281 snd_mtxlock(m->lock);
1282 if (from == MIXER_CMD_CDEV && !m->busy) {
1283 snd_mtxunlock(m->lock);
1287 case SNDCTL_DSP_GET_RECSRC_NAMES:
1288 bcopy((void *)&m->enuminfo, arg, sizeof(oss_mixer_enuminfo));
1291 case SNDCTL_DSP_GET_RECSRC:
1292 ret = mixer_get_recroute(m, arg_i);
1294 case SNDCTL_DSP_SET_RECSRC:
1295 ret = mixer_set_recroute(m, *arg_i);
1297 case OSS_GETVERSION:
1298 *arg_i = SOUND_VERSION;
1301 case SOUND_MIXER_INFO:
1302 mixer_mixerinfo(m, (mixer_info *)arg);
1306 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1307 if (j == SOUND_MIXER_RECSRC)
1308 ret = mixer_setrecsrc(m, *arg_i);
1310 ret = mixer_set(m, j, *arg_i);
1311 snd_mtxunlock(m->lock);
1312 return ((ret == 0) ? 0 : ENXIO);
1314 if ((cmd & ~0xff) == MIXER_READ(0)) {
1316 case SOUND_MIXER_DEVMASK:
1317 case SOUND_MIXER_CAPS:
1318 case SOUND_MIXER_STEREODEVS:
1321 case SOUND_MIXER_RECMASK:
1322 v = mix_getrecdevs(m);
1324 case SOUND_MIXER_RECSRC:
1325 v = mixer_getrecsrc(m);
1328 v = mixer_get(m, j);
1331 snd_mtxunlock(m->lock);
1332 return ((v != -1) ? 0 : ENXIO);
1335 snd_mtxunlock(m->lock);
1340 mixer_clone(void *arg,
1341 #if __FreeBSD_version >= 600034
1344 char *name, int namelen, struct cdev **dev)
1346 struct snddev_info *d;
1350 if (strcmp(name, "mixer") == 0) {
1351 d = devclass_get_softc(pcm_devclass, snd_unit);
1352 if (PCM_REGISTERED(d) && d->mixer_dev != NULL) {
1353 *dev = d->mixer_dev;
1360 mixer_sysinit(void *p)
1362 if (mixer_ehtag != NULL)
1364 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
1368 mixer_sysuninit(void *p)
1370 if (mixer_ehtag == NULL)
1372 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
1376 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
1377 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
1380 * @brief Handler for SNDCTL_MIXERINFO
1382 * This function searches for a mixer based on the numeric ID stored
1383 * in oss_miserinfo::dev. If set to -1, then information about the
1384 * current mixer handling the request is provided. Note, however, that
1385 * this ioctl may be made with any sound device (audio, mixer, midi).
1387 * @note Caller must not hold any PCM device, channel, or mixer locks.
1389 * See http://manuals.opensound.com/developer/SNDCTL_MIXERINFO.html for
1392 * @param i_dev character device on which the ioctl arrived
1393 * @param arg user argument (oss_mixerinfo *)
1395 * @retval EINVAL oss_mixerinfo::dev specified a bad value
1399 mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi)
1401 struct snddev_info *d;
1402 struct snd_mixer *m;
1406 * If probing the device handling the ioctl, make sure it's a mixer
1407 * device. (This ioctl is valid on audio, mixer, and midi devices.)
1409 if (mi->dev == -1 && i_dev->si_devsw != &mixer_cdevsw)
1417 * There's a 1:1 relationship between mixers and PCM devices, so
1418 * begin by iterating over PCM devices and search for our mixer.
1420 for (i = 0; pcm_devclass != NULL &&
1421 i < devclass_get_maxunit(pcm_devclass); i++) {
1422 d = devclass_get_softc(pcm_devclass, i);
1423 if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
1426 /* XXX Need Giant magic entry */
1428 /* See the note in function docblock. */
1429 PCM_UNLOCKASSERT(d);
1432 if (d->mixer_dev != NULL && d->mixer_dev->si_drv1 != NULL &&
1433 ((mi->dev == -1 && d->mixer_dev == i_dev) ||
1435 m = d->mixer_dev->si_drv1;
1439 * At this point, the following synchronization stuff
1441 * - a specific PCM device is locked.
1442 * - a specific mixer device has been locked, so be
1443 * sure to unlock when existing.
1445 bzero((void *)mi, sizeof(*mi));
1447 snprintf(mi->id, sizeof(mi->id), "mixer%d", i);
1448 strlcpy(mi->name, m->name, sizeof(mi->name));
1449 mi->modify_counter = m->modify_counter;
1450 mi->card_number = i;
1452 * Currently, FreeBSD assumes 1:1 relationship between
1453 * a pcm and mixer devices, so this is hardcoded to 0.
1455 mi->port_number = 0;
1458 * @todo Fill in @sa oss_mixerinfo::mixerhandle.
1459 * @note From 4Front: "mixerhandle is an arbitrary
1460 * string that identifies the mixer better than
1461 * the device number (mixerinfo.dev). Device
1462 * numbers may change depending on the order the
1463 * drivers are loaded. However the handle should
1464 * remain the same provided that the sound card
1465 * is not moved to another PCI slot."
1470 * @sa oss_mixerinfo::magic is a reserved field.
1473 * From 4Front: "magic is usually 0. However some
1474 * devices may have dedicated setup utilities and the
1475 * magic field may contain an unique driver specific
1476 * value (managed by [4Front])."
1479 mi->enabled = device_is_attached(m->dev) ? 1 : 0;
1481 * The only flag for @sa oss_mixerinfo::caps is
1482 * currently MIXER_CAP_VIRTUAL, which I'm not sure we
1483 * really worry about.
1486 * Mixer extensions currently aren't supported, so
1487 * leave @sa oss_mixerinfo::nrext blank for now.
1490 * @todo Fill in @sa oss_mixerinfo::priority (requires
1491 * touching drivers?)
1492 * @note The priority field is for mixer applets to
1493 * determine which mixer should be the default, with 0
1494 * being least preferred and 10 being most preferred.
1495 * From 4Front: "OSS drivers like ICH use higher
1496 * values (10) because such chips are known to be used
1497 * only on motherboards. Drivers for high end pro
1498 * devices use 0 because they will never be the
1499 * default mixer. Other devices use values 1 to 9
1500 * depending on the estimated probability of being the
1503 * XXX Described by Hannu@4Front, but not found in
1505 strlcpy(mi->devnode, devtoname(d->mixer_dev),
1506 sizeof(mi->devnode));
1507 mi->legacy_device = i;
1509 mtx_unlock(m->lock);
1523 * Allow the sound driver to use the mixer lock to protect its mixer
1527 mixer_get_lock(struct snd_mixer *m)
1529 if (m->lock == NULL) {
1536 mix_get_locked(struct snd_mixer *m, u_int dev, int *pleft, int *pright)
1540 level = mixer_get(m, dev);
1542 *pright = *pleft = -1;
1546 *pleft = level & 0xFF;
1547 *pright = (level >> 8) & 0xFF;
1553 mix_set_locked(struct snd_mixer *m, u_int dev, int left, int right)
1557 level = (left & 0xFF) | ((right & 0xFF) << 8);
1559 return (mixer_set(m, dev, level));