2 * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <dev/sound/pcm/sound.h>
31 SND_DECLARE_FILE("$FreeBSD: src/sys/dev/sound/pcm/mixer.c,v 1.4.2.8 2002/04/22 15:49:36 cg Exp $");
33 MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
35 #define MIXER_NAMELEN 16
44 u_int32_t hwvol_mute_level;
49 char name[MIXER_NAMELEN];
53 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
54 [SOUND_MIXER_VOLUME] = 75,
55 [SOUND_MIXER_BASS] = 50,
56 [SOUND_MIXER_TREBLE] = 50,
57 [SOUND_MIXER_SYNTH] = 75,
58 [SOUND_MIXER_PCM] = 75,
59 [SOUND_MIXER_SPEAKER] = 75,
60 [SOUND_MIXER_LINE] = 75,
61 [SOUND_MIXER_MIC] = 0,
62 [SOUND_MIXER_CD] = 75,
63 [SOUND_MIXER_LINE1] = 75,
64 [SOUND_MIXER_VIDEO] = 75,
65 [SOUND_MIXER_RECLEV] = 0,
66 [SOUND_MIXER_OGAIN] = 50,
67 [SOUND_MIXER_MONITOR] = 75,
70 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
72 static d_open_t mixer_open;
73 static d_close_t mixer_close;
75 static struct cdevsw mixer_cdevsw = {
76 /* open */ mixer_open,
77 /* close */ mixer_close,
80 /* ioctl */ mixer_ioctl,
83 /* strategy */ nostrategy,
85 /* maj */ SND_CDEV_MAJOR,
92 static eventhandler_tag mixer_ehtag;
96 mixer_get_devt(device_t dev)
101 unit = device_get_unit(dev);
102 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_CTL, 0));
109 mixer_lookup(char *devname)
113 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
114 if (strncmp(devname, snd_mixernames[i],
115 strlen(snd_mixernames[i])) == 0)
122 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
127 if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
130 l = min((lev & 0x00ff), 100);
131 r = min(((lev & 0xff00) >> 8), 100);
133 v = MIXER_SET(mixer, dev, l, r);
137 mixer->level[dev] = l | (r << 8);
142 mixer_get(struct snd_mixer *mixer, int dev)
144 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
145 return mixer->level[dev];
150 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
152 src &= mixer->recdevs;
154 src = SOUND_MASK_MIC;
155 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
160 mixer_getrecsrc(struct snd_mixer *mixer)
162 return mixer->recsrc;
166 mix_setdevs(struct snd_mixer *m, u_int32_t v)
172 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
178 mix_getdevs(struct snd_mixer *m)
184 mix_getrecdevs(struct snd_mixer *m)
190 mix_getdevinfo(struct snd_mixer *m)
196 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
203 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
204 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
205 m->lock = snd_mtxcreate(m->name, "pcm mixer");
207 m->devinfo = devinfo;
213 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
214 v = snd_mixerdefaults[i];
215 mixer_set(m, i, v | (v << 8));
218 mixer_setrecsrc(m, SOUND_MASK_MIC);
220 unit = device_get_unit(dev);
221 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
222 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
228 snd_mtxlock(m->lock);
229 snd_mtxfree(m->lock);
230 kobj_delete((kobj_t)m, M_MIXER);
235 mixer_uninit(device_t dev)
241 pdev = mixer_get_devt(dev);
243 snd_mtxlock(m->lock);
246 snd_mtxunlock(m->lock);
250 pdev->si_drv1 = NULL;
253 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
256 mixer_setrecsrc(m, SOUND_MASK_MIC);
260 snd_mtxfree(m->lock);
261 kobj_delete((kobj_t)m, M_MIXER);
267 mixer_reinit(device_t dev)
273 pdev = mixer_get_devt(dev);
275 snd_mtxlock(m->lock);
279 snd_mtxunlock(m->lock);
283 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
284 mixer_set(m, i, m->level[i]);
286 mixer_setrecsrc(m, m->recsrc);
287 snd_mtxunlock(m->lock);
294 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
301 snd_mtxlock(m->lock);
302 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
303 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
304 if (error == 0 && req->newptr != NULL) {
305 dev = mixer_lookup(devname);
307 snd_mtxunlock(m->lock);
310 else if (dev != m->hwvol_mixer) {
311 m->hwvol_mixer = dev;
315 snd_mtxunlock(m->lock);
321 mixer_hwvol_init(device_t dev)
326 pdev = mixer_get_devt(dev);
328 snd_mtxlock(m->lock);
330 m->hwvol_mixer = SOUND_MIXER_VOLUME;
333 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
334 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
335 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
336 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
337 sysctl_hw_snd_hwvol_mixer, "A", "")
339 snd_mtxunlock(m->lock);
344 mixer_hwvol_mute(device_t dev)
349 pdev = mixer_get_devt(dev);
351 snd_mtxlock(m->lock);
352 if (m->hwvol_muted) {
354 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
357 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
358 mixer_set(m, m->hwvol_mixer, 0);
360 snd_mtxunlock(m->lock);
364 mixer_hwvol_step(device_t dev, int left_step, int right_step)
367 int level, left, right;
370 pdev = mixer_get_devt(dev);
372 snd_mtxlock(m->lock);
373 if (m->hwvol_muted) {
375 level = m->hwvol_mute_level;
377 level = mixer_get(m, m->hwvol_mixer);
381 left += left_step * m->hwvol_step;
384 right += right_step * m->hwvol_step;
387 mixer_set(m, m->hwvol_mixer, left | right << 8);
389 snd_mtxunlock(m->lock);
392 /* ----------------------------------------------------------------------- */
395 mixer_open(dev_t i_dev, int flags, int mode, struct proc *p)
402 snd_mtxlock(m->lock);
406 snd_mtxunlock(m->lock);
412 mixer_close(dev_t i_dev, int flags, int mode, struct proc *p)
419 snd_mtxlock(m->lock);
422 snd_mtxunlock(m->lock);
428 snd_mtxunlock(m->lock);
434 mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc *p)
438 int ret, *arg_i = (int *)arg;
439 int v = -1, j = cmd & 0xff;
446 snd_mtxlock(m->lock);
447 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
448 if (j == SOUND_MIXER_RECSRC)
449 ret = mixer_setrecsrc(m, *arg_i);
451 ret = mixer_set(m, j, *arg_i);
452 snd_mtxunlock(m->lock);
454 return (ret == 0)? 0 : ENXIO;
457 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
459 case SOUND_MIXER_DEVMASK:
460 case SOUND_MIXER_CAPS:
461 case SOUND_MIXER_STEREODEVS:
465 case SOUND_MIXER_RECMASK:
466 v = mix_getrecdevs(m);
469 case SOUND_MIXER_RECSRC:
470 v = mixer_getrecsrc(m);
477 snd_mtxunlock(m->lock);
478 return (v != -1)? 0 : ENXIO;
480 snd_mtxunlock(m->lock);
487 mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
493 if (strcmp(name, "mixer") == 0) {
494 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
495 if (pdev->si_flags & SI_NAMED)
501 mixer_sysinit(void *p)
503 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
507 mixer_sysuninit(void *p)
509 if (mixer_ehtag != NULL)
510 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
513 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
514 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);