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
26 * $FreeBSD: src/sys/dev/sound/pcm/mixer.c,v 1.4.2.8 2002/04/22 15:49:36 cg Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.9 2006/06/06 19:30:12 dillon Exp $
30 #include <dev/sound/pcm/sound.h>
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.9 2006/06/06 19:30:12 dillon Exp $");
36 MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
38 #define MIXER_NAMELEN 16
47 u_int32_t hwvol_mute_level;
52 char name[MIXER_NAMELEN];
56 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
57 [SOUND_MIXER_VOLUME] = 75,
58 [SOUND_MIXER_BASS] = 50,
59 [SOUND_MIXER_TREBLE] = 50,
60 [SOUND_MIXER_SYNTH] = 75,
61 [SOUND_MIXER_PCM] = 75,
62 [SOUND_MIXER_SPEAKER] = 75,
63 [SOUND_MIXER_LINE] = 75,
64 [SOUND_MIXER_MIC] = 0,
65 [SOUND_MIXER_CD] = 75,
66 [SOUND_MIXER_LINE1] = 75,
67 [SOUND_MIXER_VIDEO] = 75,
68 [SOUND_MIXER_RECLEV] = 0,
69 [SOUND_MIXER_OGAIN] = 50,
70 [SOUND_MIXER_MONITOR] = 75,
73 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
75 static d_open_t mixer_open;
76 static d_close_t mixer_close;
78 static struct cdevsw mixer_cdevsw = {
80 /* maj */ SND_CDEV_MAJOR,
85 /* open */ mixer_open,
86 /* close */ mixer_close,
89 /* ioctl */ mixer_ioctl,
92 /* strategy */ nostrategy,
98 static eventhandler_tag mixer_ehtag;
102 mixer_get_devt(device_t dev)
107 unit = device_get_unit(dev);
108 pdev = make_adhoc_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0));
115 mixer_lookup(char *devname)
119 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
120 if (strncmp(devname, snd_mixernames[i],
121 strlen(snd_mixernames[i])) == 0)
128 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
133 if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
136 l = min((lev & 0x00ff), 100);
137 r = min(((lev & 0xff00) >> 8), 100);
139 v = MIXER_SET(mixer, dev, l, r);
143 mixer->level[dev] = l | (r << 8);
148 mixer_get(struct snd_mixer *mixer, int dev)
150 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
151 return mixer->level[dev];
156 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
158 src &= mixer->recdevs;
160 src = SOUND_MASK_MIC;
161 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
166 mixer_getrecsrc(struct snd_mixer *mixer)
168 return mixer->recsrc;
172 mix_setdevs(struct snd_mixer *m, u_int32_t v)
178 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
184 mix_getdevs(struct snd_mixer *m)
190 mix_getrecdevs(struct snd_mixer *m)
196 mix_getdevinfo(struct snd_mixer *m)
202 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
209 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
210 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
211 m->lock = snd_mtxcreate(m->name, "pcm mixer");
213 m->devinfo = devinfo;
219 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
220 v = snd_mixerdefaults[i];
221 mixer_set(m, i, v | (v << 8));
224 mixer_setrecsrc(m, SOUND_MASK_MIC);
226 unit = device_get_unit(dev);
227 cdevsw_add(&mixer_cdevsw,
228 PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_CTL, 0));
229 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
230 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
236 snd_mtxlock(m->lock);
237 snd_mtxfree(m->lock);
238 kobj_delete((kobj_t)m, M_MIXER);
243 mixer_uninit(device_t dev)
250 pdev = mixer_get_devt(dev);
252 snd_mtxlock(m->lock);
255 snd_mtxunlock(m->lock);
259 pdev->si_drv1 = NULL;
261 unit = device_get_unit(dev);
262 cdevsw_remove(&mixer_cdevsw,
263 PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_CTL, 0));
265 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
268 mixer_setrecsrc(m, SOUND_MASK_MIC);
272 snd_mtxfree(m->lock);
273 kobj_delete((kobj_t)m, M_MIXER);
279 mixer_reinit(device_t dev)
285 pdev = mixer_get_devt(dev);
287 snd_mtxlock(m->lock);
291 snd_mtxunlock(m->lock);
295 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
296 mixer_set(m, i, m->level[i]);
298 mixer_setrecsrc(m, m->recsrc);
299 snd_mtxunlock(m->lock);
306 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
313 snd_mtxlock(m->lock);
314 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
315 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
316 if (error == 0 && req->newptr != NULL) {
317 dev = mixer_lookup(devname);
319 snd_mtxunlock(m->lock);
322 else if (dev != m->hwvol_mixer) {
323 m->hwvol_mixer = dev;
327 snd_mtxunlock(m->lock);
333 mixer_hwvol_init(device_t dev)
338 pdev = mixer_get_devt(dev);
340 snd_mtxlock(m->lock);
342 m->hwvol_mixer = SOUND_MIXER_VOLUME;
345 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
346 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
347 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
348 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
349 sysctl_hw_snd_hwvol_mixer, "A", "")
351 snd_mtxunlock(m->lock);
356 mixer_hwvol_mute(device_t dev)
361 pdev = mixer_get_devt(dev);
363 snd_mtxlock(m->lock);
364 if (m->hwvol_muted) {
366 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
369 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
370 mixer_set(m, m->hwvol_mixer, 0);
372 snd_mtxunlock(m->lock);
376 mixer_hwvol_step(device_t dev, int left_step, int right_step)
379 int level, left, right;
382 pdev = mixer_get_devt(dev);
384 snd_mtxlock(m->lock);
385 if (m->hwvol_muted) {
387 level = m->hwvol_mute_level;
389 level = mixer_get(m, m->hwvol_mixer);
393 left += left_step * m->hwvol_step;
396 right += right_step * m->hwvol_step;
399 mixer_set(m, m->hwvol_mixer, left | right << 8);
401 snd_mtxunlock(m->lock);
404 /* ----------------------------------------------------------------------- */
407 mixer_open(dev_t i_dev, int flags, int mode, struct thread *td)
413 snd_mtxlock(m->lock);
417 snd_mtxunlock(m->lock);
423 mixer_close(dev_t i_dev, int flags, int mode, struct thread *td)
429 snd_mtxlock(m->lock);
432 snd_mtxunlock(m->lock);
438 snd_mtxunlock(m->lock);
444 mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
447 int ret, *arg_i = (int *)arg;
448 int v = -1, j = cmd & 0xff;
455 snd_mtxlock(m->lock);
456 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
457 if (j == SOUND_MIXER_RECSRC)
458 ret = mixer_setrecsrc(m, *arg_i);
460 ret = mixer_set(m, j, *arg_i);
461 snd_mtxunlock(m->lock);
463 return (ret == 0)? 0 : ENXIO;
466 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
468 case SOUND_MIXER_DEVMASK:
469 case SOUND_MIXER_CAPS:
470 case SOUND_MIXER_STEREODEVS:
474 case SOUND_MIXER_RECMASK:
475 v = mix_getrecdevs(m);
478 case SOUND_MIXER_RECSRC:
479 v = mixer_getrecsrc(m);
486 snd_mtxunlock(m->lock);
488 return (v != -1)? 0 : ENXIO;
490 snd_mtxunlock(m->lock);
497 mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
503 if (strcmp(name, "mixer") == 0) {
504 pdev = make_adhoc_dev(&mixer_cdevsw,
505 PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
506 if (pdev->si_flags & SI_NAMED)
512 mixer_sysinit(void *p)
514 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
518 mixer_sysuninit(void *p)
520 if (mixer_ehtag != NULL)
521 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
524 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
525 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);