Deleted Added
sdiff udiff text old ( 170235 ) new ( 170815 )
full compact
1/*-
2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 14 unchanged lines hidden (view full) ---

23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/sound/pcm/sound.h>
28
29#include "mixer_if.h"
30
31SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/mixer.c 170235 2007-06-03 10:56:22Z ariff $");
32
33MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
34
35#define MIXER_NAMELEN 16
36struct snd_mixer {
37 KOBJ_FIELDS;
38 const char *type;
39 void *devinfo;
40 int busy;
41 int hwvol_muted;
42 int hwvol_mixer;
43 int hwvol_step;
44 device_t dev;
45 u_int32_t hwvol_mute_level;
46 u_int32_t devs;
47 u_int32_t recdevs;
48 u_int32_t recsrc;
49 u_int16_t level[32];
50 u_int8_t parent[32];
51 u_int32_t child[32];

--- 26 unchanged lines hidden (view full) ---

78 [SOUND_MIXER_OGAIN] = 50,
79 [SOUND_MIXER_MONITOR] = 75,
80};
81
82static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
83
84static d_open_t mixer_open;
85static d_close_t mixer_close;
86
87static struct cdevsw mixer_cdevsw = {
88 .d_version = D_VERSION,
89 .d_flags = D_NEEDGIANT,
90 .d_open = mixer_open,
91 .d_close = mixer_close,
92 .d_ioctl = mixer_ioctl,
93 .d_name = "mixer",
94};
95
96/**
97 * Keeps a count of mixer devices; used only by OSSv4 SNDCTL_SYSINFO ioctl.

--- 23 unchanged lines hidden (view full) ---

121 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
122 if (strncmp(devname, snd_mixernames[i],
123 strlen(snd_mixernames[i])) == 0)
124 return i;
125 return -1;
126}
127#endif
128
129static int
130mixer_set_softpcmvol(struct snd_mixer *mixer, struct snddev_info *d,
131 unsigned left, unsigned right)
132{
133 struct pcm_channel *c;
134 int locked;
135
136 locked = (mixer->lock != NULL &&
137 mtx_owned((struct mtx *)(mixer->lock))) ? 1 : 0;
138 if (locked)
139 snd_mtxunlock(mixer->lock);
140
141 if (CHN_EMPTY(d, channels.pcm.busy)) {
142 CHN_FOREACH(c, d, channels.pcm) {
143 CHN_LOCK(c);
144 if (c->direction == PCMDIR_PLAY &&
145 (c->feederflags & (1 << FEEDER_VOLUME)))
146 chn_setvolume(c, left, right);
147 CHN_UNLOCK(c);
148 }
149 } else {
150 CHN_FOREACH(c, d, channels.pcm.busy) {
151 CHN_LOCK(c);
152 if (c->direction == PCMDIR_PLAY &&
153 (c->feederflags & (1 << FEEDER_VOLUME)))
154 chn_setvolume(c, left, right);
155 CHN_UNLOCK(c);
156 }
157 }
158
159 if (locked)
160 snd_mtxlock(mixer->lock);
161
162 return 0;
163}
164
165static int
166mixer_set(struct snd_mixer *m, unsigned dev, unsigned lev)
167{
168 struct snddev_info *d;
169 unsigned l, r, tl, tr;
170 u_int32_t parent = SOUND_MIXER_NONE, child = 0;
171 u_int32_t realdev;
172 int i;
173
174 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
175 (0 == (m->devs & (1 << dev))))
176 return -1;
177
178 l = min((lev & 0x00ff), 100);
179 r = min(((lev & 0xff00) >> 8), 100);
180 realdev = m->realdev[dev];
181
182 d = device_get_softc(m->dev);
183 if (d == NULL)
184 return -1;
185
186 /* TODO: recursive handling */
187 parent = m->parent[dev];
188 if (parent >= SOUND_MIXER_NRDEVICES)
189 parent = SOUND_MIXER_NONE;
190 if (parent == SOUND_MIXER_NONE)
191 child = m->child[dev];
192
193 if (parent != SOUND_MIXER_NONE) {
194 tl = (l * (m->level[parent] & 0x00ff)) / 100;
195 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
196 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
197 mixer_set_softpcmvol(m, d, tl, tr);
198 else if (realdev != SOUND_MIXER_NONE &&
199 MIXER_SET(m, realdev, tl, tr) < 0)
200 return -1;
201 } else if (child != 0) {
202 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
203 if (!(child & (1 << i)) || m->parent[i] != dev)
204 continue;
205 realdev = m->realdev[i];
206 tl = (l * (m->level[i] & 0x00ff)) / 100;
207 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
208 if (i == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
209 mixer_set_softpcmvol(m, d, tl, tr);
210 else if (realdev != SOUND_MIXER_NONE)
211 MIXER_SET(m, realdev, tl, tr);
212 }
213 realdev = m->realdev[dev];
214 if (realdev != SOUND_MIXER_NONE &&
215 MIXER_SET(m, realdev, l, r) < 0)
216 return -1;
217 } else {
218 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
219 mixer_set_softpcmvol(m, d, l, r);
220 else if (realdev != SOUND_MIXER_NONE &&
221 MIXER_SET(m, realdev, l, r) < 0)
222 return -1;
223 }
224
225 m->level[dev] = l | (r << 8);
226
227 return 0;
228}
229
230static int
231mixer_get(struct snd_mixer *mixer, int dev)
232{
233 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
234 return mixer->level[dev];
235 else return -1;
236}
237
238static int
239mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
240{
241 src &= mixer->recdevs;
242 if (src == 0)
243 src = SOUND_MASK_MIC;
244 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
245 return 0;
246}
247
248static int
249mixer_getrecsrc(struct snd_mixer *mixer)
250{
251 return mixer->recsrc;
252}

--- 224 unchanged lines hidden (view full) ---

477}
478
479void *
480mix_getdevinfo(struct snd_mixer *m)
481{
482 return m->devinfo;
483}
484
485int
486mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
487{
488 struct snddev_info *snddev;
489 struct snd_mixer *m;
490 u_int16_t v;
491 struct cdev *pdev;
492 int i, unit, devunit, val;
493
494 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
495 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
496 m->lock = snd_mtxcreate(m->name, "pcm mixer");
497 m->type = cls->name;
498 m->devinfo = devinfo;
499 m->busy = 0;
500 m->dev = dev;
501 for (i = 0; i < 32; i++) {
502 m->parent[i] = SOUND_MIXER_NONE;
503 m->child[i] = 0;
504 m->realdev[i] = i;
505 }
506
507 if (MIXER_INIT(m))
508 goto bad;
509
510 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
511 v = snd_mixerdefaults[i];
512
513 if (resource_int_value(device_get_name(dev),
514 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
515 if (val >= 0 && val <= 100) {
516 v = (u_int16_t) val;
517 }

--- 33 unchanged lines hidden (view full) ---

551 if (m->child[i] != 0)
552 printf(" child=0x%08x", m->child[i]);
553 printf("\n");
554 }
555 if (snddev->flags & SD_F_SOFTPCMVOL)
556 device_printf(dev, "Soft PCM mixer ENABLED\n");
557 }
558
559 return 0;
560
561bad:
562 snd_mtxlock(m->lock);
563 snd_mtxfree(m->lock);
564 kobj_delete((kobj_t)m, M_MIXER);
565 return -1;
566}
567
568int
569mixer_uninit(device_t dev)
570{
571 int i;
572 struct snddev_info *d;
573 struct snd_mixer *m;
574 struct cdev *pdev;
575
576 d = device_get_softc(dev);
577 pdev = mixer_get_devt(dev);
578 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
579 return EBADF;
580 m = pdev->si_drv1;
581 snd_mtxlock(m->lock);
582
583 if (m->busy) {
584 snd_mtxunlock(m->lock);
585 return EBUSY;
586 }
587
588 pdev->si_drv1 = NULL;

--- 139 unchanged lines hidden (view full) ---

728 right += right_step * m->hwvol_step;
729 if (right < 0)
730 right = 0;
731 mixer_set(m, m->hwvol_mixer, left | right << 8);
732 }
733 snd_mtxunlock(m->lock);
734}
735
736/* ----------------------------------------------------------------------- */
737
738static int
739mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
740{
741 struct snd_mixer *m;
742
743 m = i_dev->si_drv1;
744 snd_mtxlock(m->lock);
745
746 m->busy = 1;
747
748 snd_mtxunlock(m->lock);
749 return 0;
750}
751
752static int
753mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
754{
755 struct snd_mixer *m;
756
757 m = i_dev->si_drv1;
758 snd_mtxlock(m->lock);
759
760 if (!m->busy) {
761 snd_mtxunlock(m->lock);
762 return EBADF;
763 }
764 m->busy = 0;
765
766 snd_mtxunlock(m->lock);
767 return 0;
768}
769
770int
771mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
772{
773 struct snd_mixer *m;
774 int ret, *arg_i = (int *)arg;
775 int v = -1, j = cmd & 0xff;
776
777 m = i_dev->si_drv1;
778
779 if (m == NULL)
780 return EBADF;
781
782 snd_mtxlock(m->lock);
783 if (mode != -1 && !m->busy) {
784 snd_mtxunlock(m->lock);
785 return EBADF;
786 }
787
788 if (cmd == SNDCTL_MIXERINFO) {
789 snd_mtxunlock(m->lock);
790 return mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
791 }
792
793 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
794 if (j == SOUND_MIXER_RECSRC)
795 ret = mixer_setrecsrc(m, *arg_i);
796 else
797 ret = mixer_set(m, j, *arg_i);
798 snd_mtxunlock(m->lock);
799 return (ret == 0)? 0 : ENXIO;
800 }
801
802 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
803 switch (j) {
804 case SOUND_MIXER_DEVMASK:
805 case SOUND_MIXER_CAPS:
806 case SOUND_MIXER_STEREODEVS:
807 v = mix_getdevs(m);

--- 7 unchanged lines hidden (view full) ---

815 v = mixer_getrecsrc(m);
816 break;
817
818 default:
819 v = mixer_get(m, j);
820 }
821 *arg_i = v;
822 snd_mtxunlock(m->lock);
823 return (v != -1)? 0 : ENXIO;
824 }
825
826 ret = 0;
827
828 switch (cmd) {
829 /** @todo Double check return values, error codes. */
830 case SNDCTL_SYSINFO:
831 sound_oss_sysinfo((oss_sysinfo *)arg);
832 break;
833 case SNDCTL_AUDIOINFO:
834 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
835 break;
836 case SNDCTL_DSP_GET_RECSRC_NAMES:
837 bcopy((void *)&m->enuminfo, arg, sizeof(oss_mixer_enuminfo));
838 break;
839 case SNDCTL_DSP_GET_RECSRC:
840 ret = mixer_get_recroute(m, arg_i);
841 break;
842 case SNDCTL_DSP_SET_RECSRC:
843 ret = mixer_set_recroute(m, *arg_i);
844 break;
845 case OSS_GETVERSION:
846 *arg_i = SOUND_VERSION;
847 break;
848 default:
849 ret = ENXIO;
850 }
851
852 snd_mtxunlock(m->lock);
853 return ret;
854}
855
856#ifdef USING_DEVFS
857static void
858mixer_clone(void *arg,
859#if __FreeBSD_version >= 600034
860 struct ucred *cred,
861#endif
862 char *name, int namelen, struct cdev **dev)
863{
864 struct snddev_info *d;
865
866 if (*dev != NULL)
867 return;
868 if (strcmp(name, "mixer") == 0) {
869 d = devclass_get_softc(pcm_devclass, snd_unit);
870 if (d != NULL && d->mixer_dev != NULL) {
871 *dev = d->mixer_dev;
872 dev_ref(*dev);
873 }
874 }
875}
876
877static void
878mixer_sysinit(void *p)

--- 35 unchanged lines hidden (view full) ---

914 * @retval EINVAL oss_mixerinfo::dev specified a bad value
915 * @retval 0 success
916 */
917int
918mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi)
919{
920 struct snddev_info *d;
921 struct snd_mixer *m;
922 struct cdev *t_cdev;
923 int nmix, ret, pcmunit, i;
924
925 /*
926 * If probing the device handling the ioctl, make sure it's a mixer
927 * device. (This ioctl is valid on audio, mixer, and midi devices.)
928 */
929 if ((mi->dev == -1) && (i_dev->si_devsw != &mixer_cdevsw))
930 return EINVAL;
931
932 d = NULL;
933 m = NULL;
934 t_cdev = NULL;
935 nmix = 0;
936 ret = 0;
937 pcmunit = -1; /* pcmX */
938
939 /*
940 * There's a 1:1 relationship between mixers and PCM devices, so
941 * begin by iterating over PCM devices and search for our mixer.
942 */
943 for (i = 0; pcm_devclass != NULL &&
944 i < devclass_get_maxunit(pcm_devclass); i++) {
945 d = devclass_get_softc(pcm_devclass, i);
946 if (d == NULL)
947 continue;
948
949 /* See the note in function docblock. */
950 mtx_assert(d->lock, MA_NOTOWNED);
951 pcm_inprog(d, 1);
952 pcm_lock(d);
953
954 if (d->mixer_dev != NULL) {
955 if (((mi->dev == -1) && (d->mixer_dev == i_dev)) || (mi->dev == nmix)) {
956 t_cdev = d->mixer_dev;
957 pcmunit = i;
958 break;
959 }
960 ++nmix;
961 }
962
963 pcm_unlock(d);
964 pcm_inprog(d, -1);
965 }
966
967 /*
968 * If t_cdev is NULL, then search was exhausted and device wasn't
969 * found. No locks are held, so just return.
970 */
971 if (t_cdev == NULL)
972 return EINVAL;
973
974 m = t_cdev->si_drv1;
975 mtx_lock(m->lock);
976
977 /*
978 * At this point, the following synchronization stuff has happened:
979 * - a specific PCM device is locked and its "in progress
980 * operations" counter has been incremented, so be sure to unlock
981 * and decrement when exiting;
982 * - a specific mixer device has been locked, so be sure to unlock
983 * when existing.
984 */
985
986 bzero((void *)mi, sizeof(*mi));
987
988 mi->dev = nmix;
989 snprintf(mi->id, sizeof(mi->id), "mixer%d", dev2unit(t_cdev));
990 strlcpy(mi->name, m->name, sizeof(mi->name));
991 mi->modify_counter = m->modify_counter;
992 mi->card_number = pcmunit;
993 /*
994 * Currently, FreeBSD assumes 1:1 relationship between a pcm and
995 * mixer devices, so this is hardcoded to 0.
996 */
997 mi->port_number = 0;
998
999 /**
1000 * @todo Fill in @sa oss_mixerinfo::mixerhandle.
1001 * @note From 4Front: "mixerhandle is an arbitrary string that
1002 * identifies the mixer better than the device number
1003 * (mixerinfo.dev). Device numbers may change depending on
1004 * the order the drivers are loaded. However the handle
1005 * should remain the same provided that the sound card is
1006 * not moved to another PCI slot."
1007 */
1008
1009 /**
1010 * @note
1011 * @sa oss_mixerinfo::magic is a reserved field.
1012 *
1013 * @par
1014 * From 4Front: "magic is usually 0. However some devices may have
1015 * dedicated setup utilities and the magic field may contain an
1016 * unique driver specific value (managed by [4Front])."
1017 */
1018
1019 mi->enabled = device_is_attached(m->dev) ? 1 : 0;
1020 /**
1021 * The only flag for @sa oss_mixerinfo::caps is currently
1022 * MIXER_CAP_VIRTUAL, which I'm not sure we really worry about.
1023 */
1024 /**
1025 * Mixer extensions currently aren't supported, so leave
1026 * @sa oss_mixerinfo::nrext blank for now.
1027 */
1028 /**
1029 * @todo Fill in @sa oss_mixerinfo::priority (requires touching
1030 * drivers?)
1031 * @note The priority field is for mixer applets to determine which
1032 * mixer should be the default, with 0 being least preferred and 10
1033 * being most preferred. From 4Front: "OSS drivers like ICH use
1034 * higher values (10) because such chips are known to be used only
1035 * on motherboards. Drivers for high end pro devices use 0 because
1036 * they will never be the default mixer. Other devices use values 1
1037 * to 9 depending on the estimated probability of being the default
1038 * device.
1039 *
1040 * XXX Described by Hannu@4Front, but not found in soundcard.h.
1041 strlcpy(mi->devnode, t_cdev->si_name, sizeof(mi->devnode));
1042 mi->legacy_device = pcmunit;
1043 */
1044
1045 mtx_unlock(m->lock);
1046 pcm_unlock(d);
1047 pcm_inprog(d, -1);
1048
1049 return ret;
1050}