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.3 2003/06/23 17:55:34 dillon Exp $
30 #include <dev/sound/pcm/sound.h>
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.3 2003/06/23 17:55:34 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 = {
79 /* open */ mixer_open,
80 /* close */ mixer_close,
83 /* ioctl */ mixer_ioctl,
86 /* strategy */ nostrategy,
88 /* maj */ SND_CDEV_MAJOR,
95 static eventhandler_tag mixer_ehtag;
99 mixer_get_devt(device_t dev)
104 unit = device_get_unit(dev);
105 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_CTL, 0));
112 mixer_lookup(char *devname)
116 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
117 if (strncmp(devname, snd_mixernames[i],
118 strlen(snd_mixernames[i])) == 0)
125 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
130 if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
133 l = min((lev & 0x00ff), 100);
134 r = min(((lev & 0xff00) >> 8), 100);
136 v = MIXER_SET(mixer, dev, l, r);
140 mixer->level[dev] = l | (r << 8);
145 mixer_get(struct snd_mixer *mixer, int dev)
147 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
148 return mixer->level[dev];
153 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
155 src &= mixer->recdevs;
157 src = SOUND_MASK_MIC;
158 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
163 mixer_getrecsrc(struct snd_mixer *mixer)
165 return mixer->recsrc;
169 mix_setdevs(struct snd_mixer *m, u_int32_t v)
175 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
181 mix_getdevs(struct snd_mixer *m)
187 mix_getrecdevs(struct snd_mixer *m)
193 mix_getdevinfo(struct snd_mixer *m)
199 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
206 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
207 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
208 m->lock = snd_mtxcreate(m->name, "pcm mixer");
210 m->devinfo = devinfo;
216 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
217 v = snd_mixerdefaults[i];
218 mixer_set(m, i, v | (v << 8));
221 mixer_setrecsrc(m, SOUND_MASK_MIC);
223 unit = device_get_unit(dev);
224 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
225 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
231 snd_mtxlock(m->lock);
232 snd_mtxfree(m->lock);
233 kobj_delete((kobj_t)m, M_MIXER);
238 mixer_uninit(device_t dev)
244 pdev = mixer_get_devt(dev);
246 snd_mtxlock(m->lock);
249 snd_mtxunlock(m->lock);
253 pdev->si_drv1 = NULL;
256 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
259 mixer_setrecsrc(m, SOUND_MASK_MIC);
263 snd_mtxfree(m->lock);
264 kobj_delete((kobj_t)m, M_MIXER);
270 mixer_reinit(device_t dev)
276 pdev = mixer_get_devt(dev);
278 snd_mtxlock(m->lock);
282 snd_mtxunlock(m->lock);
286 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
287 mixer_set(m, i, m->level[i]);
289 mixer_setrecsrc(m, m->recsrc);
290 snd_mtxunlock(m->lock);
297 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
304 snd_mtxlock(m->lock);
305 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
306 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
307 if (error == 0 && req->newptr != NULL) {
308 dev = mixer_lookup(devname);
310 snd_mtxunlock(m->lock);
313 else if (dev != m->hwvol_mixer) {
314 m->hwvol_mixer = dev;
318 snd_mtxunlock(m->lock);
324 mixer_hwvol_init(device_t dev)
329 pdev = mixer_get_devt(dev);
331 snd_mtxlock(m->lock);
333 m->hwvol_mixer = SOUND_MIXER_VOLUME;
336 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
337 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
338 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
339 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
340 sysctl_hw_snd_hwvol_mixer, "A", "")
342 snd_mtxunlock(m->lock);
347 mixer_hwvol_mute(device_t dev)
352 pdev = mixer_get_devt(dev);
354 snd_mtxlock(m->lock);
355 if (m->hwvol_muted) {
357 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
360 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
361 mixer_set(m, m->hwvol_mixer, 0);
363 snd_mtxunlock(m->lock);
367 mixer_hwvol_step(device_t dev, int left_step, int right_step)
370 int level, left, right;
373 pdev = mixer_get_devt(dev);
375 snd_mtxlock(m->lock);
376 if (m->hwvol_muted) {
378 level = m->hwvol_mute_level;
380 level = mixer_get(m, m->hwvol_mixer);
384 left += left_step * m->hwvol_step;
387 right += right_step * m->hwvol_step;
390 mixer_set(m, m->hwvol_mixer, left | right << 8);
392 snd_mtxunlock(m->lock);
395 /* ----------------------------------------------------------------------- */
398 mixer_open(dev_t i_dev, int flags, int mode, struct thread *td)
405 snd_mtxlock(m->lock);
409 snd_mtxunlock(m->lock);
415 mixer_close(dev_t i_dev, int flags, int mode, struct thread *td)
422 snd_mtxlock(m->lock);
425 snd_mtxunlock(m->lock);
431 snd_mtxunlock(m->lock);
437 mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
441 int ret, *arg_i = (int *)arg;
442 int v = -1, j = cmd & 0xff;
449 snd_mtxlock(m->lock);
450 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
451 if (j == SOUND_MIXER_RECSRC)
452 ret = mixer_setrecsrc(m, *arg_i);
454 ret = mixer_set(m, j, *arg_i);
455 snd_mtxunlock(m->lock);
457 return (ret == 0)? 0 : ENXIO;
460 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
462 case SOUND_MIXER_DEVMASK:
463 case SOUND_MIXER_CAPS:
464 case SOUND_MIXER_STEREODEVS:
468 case SOUND_MIXER_RECMASK:
469 v = mix_getrecdevs(m);
472 case SOUND_MIXER_RECSRC:
473 v = mixer_getrecsrc(m);
480 snd_mtxunlock(m->lock);
481 return (v != -1)? 0 : ENXIO;
483 snd_mtxunlock(m->lock);
490 mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
496 if (strcmp(name, "mixer") == 0) {
497 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
498 if (pdev->si_flags & SI_NAMED)
504 mixer_sysinit(void *p)
506 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
510 mixer_sysuninit(void *p)
512 if (mixer_ehtag != NULL)
513 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
516 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
517 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);