xref: /dragonfly/sys/dev/sound/pcm/mixer.c (revision 1de703da)
1 /*
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
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
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.
13  *
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
24  * SUCH DAMAGE.
25  *
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.2 2003/06/17 04:28:31 dillon Exp $
28  */
29 
30 #include <dev/sound/pcm/sound.h>
31 
32 #include "mixer_if.h"
33 
34 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.2 2003/06/17 04:28:31 dillon Exp $");
35 
36 MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
37 
38 #define MIXER_NAMELEN	16
39 struct snd_mixer {
40 	KOBJ_FIELDS;
41 	const char *type;
42 	void *devinfo;
43 	int busy;
44 	int hwvol_muted;
45 	int hwvol_mixer;
46 	int hwvol_step;
47 	u_int32_t hwvol_mute_level;
48 	u_int32_t devs;
49 	u_int32_t recdevs;
50 	u_int32_t recsrc;
51 	u_int16_t level[32];
52 	char name[MIXER_NAMELEN];
53 	void *lock;
54 };
55 
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,
71 };
72 
73 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
74 
75 static d_open_t mixer_open;
76 static d_close_t mixer_close;
77 
78 static struct cdevsw mixer_cdevsw = {
79 	/* open */	mixer_open,
80 	/* close */	mixer_close,
81 	/* read */	noread,
82 	/* write */	nowrite,
83 	/* ioctl */	mixer_ioctl,
84 	/* poll */	nopoll,
85 	/* mmap */	nommap,
86 	/* strategy */	nostrategy,
87 	/* name */	"mixer",
88 	/* maj */	SND_CDEV_MAJOR,
89 	/* dump */	nodump,
90 	/* psize */	nopsize,
91 	/* flags */	0,
92 };
93 
94 #ifdef USING_DEVFS
95 static eventhandler_tag mixer_ehtag;
96 #endif
97 
98 static dev_t
99 mixer_get_devt(device_t dev)
100 {
101 	dev_t pdev;
102 	int unit;
103 
104 	unit = device_get_unit(dev);
105 	pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_CTL, 0));
106 
107 	return pdev;
108 }
109 
110 #ifdef SND_DYNSYSCTL
111 static int
112 mixer_lookup(char *devname)
113 {
114 	int i;
115 
116 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
117 		if (strncmp(devname, snd_mixernames[i],
118 		    strlen(snd_mixernames[i])) == 0)
119 			return i;
120 	return -1;
121 }
122 #endif
123 
124 static int
125 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
126 {
127 	unsigned l, r;
128 	int v;
129 
130 	if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
131 		return -1;
132 
133 	l = min((lev & 0x00ff), 100);
134 	r = min(((lev & 0xff00) >> 8), 100);
135 
136 	v = MIXER_SET(mixer, dev, l, r);
137 	if (v < 0)
138 		return -1;
139 
140 	mixer->level[dev] = l | (r << 8);
141 	return 0;
142 }
143 
144 static int
145 mixer_get(struct snd_mixer *mixer, int dev)
146 {
147 	if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
148 		return mixer->level[dev];
149 	else return -1;
150 }
151 
152 static int
153 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
154 {
155 	src &= mixer->recdevs;
156 	if (src == 0)
157 		src = SOUND_MASK_MIC;
158 	mixer->recsrc = MIXER_SETRECSRC(mixer, src);
159 	return 0;
160 }
161 
162 static int
163 mixer_getrecsrc(struct snd_mixer *mixer)
164 {
165 	return mixer->recsrc;
166 }
167 
168 void
169 mix_setdevs(struct snd_mixer *m, u_int32_t v)
170 {
171 	m->devs = v;
172 }
173 
174 void
175 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
176 {
177 	m->recdevs = v;
178 }
179 
180 u_int32_t
181 mix_getdevs(struct snd_mixer *m)
182 {
183 	return m->devs;
184 }
185 
186 u_int32_t
187 mix_getrecdevs(struct snd_mixer *m)
188 {
189 	return m->recdevs;
190 }
191 
192 void *
193 mix_getdevinfo(struct snd_mixer *m)
194 {
195 	return m->devinfo;
196 }
197 
198 int
199 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
200 {
201 	struct snd_mixer *m;
202 	u_int16_t v;
203 	dev_t pdev;
204 	int i, unit;
205 
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");
209 	m->type = cls->name;
210 	m->devinfo = devinfo;
211 	m->busy = 0;
212 
213 	if (MIXER_INIT(m))
214 		goto bad;
215 
216 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
217 		v = snd_mixerdefaults[i];
218 		mixer_set(m, i, v | (v << 8));
219 	}
220 
221 	mixer_setrecsrc(m, SOUND_MASK_MIC);
222 
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);
226 	pdev->si_drv1 = m;
227 
228 	return 0;
229 
230 bad:
231 	snd_mtxlock(m->lock);
232 	snd_mtxfree(m->lock);
233 	kobj_delete((kobj_t)m, M_MIXER);
234 	return -1;
235 }
236 
237 int
238 mixer_uninit(device_t dev)
239 {
240 	int i;
241 	struct snd_mixer *m;
242 	dev_t pdev;
243 
244 	pdev = mixer_get_devt(dev);
245 	m = pdev->si_drv1;
246 	snd_mtxlock(m->lock);
247 
248 	if (m->busy) {
249 		snd_mtxunlock(m->lock);
250 		return EBUSY;
251 	}
252 
253 	pdev->si_drv1 = NULL;
254 	destroy_dev(pdev);
255 
256 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
257 		mixer_set(m, i, 0);
258 
259 	mixer_setrecsrc(m, SOUND_MASK_MIC);
260 
261 	MIXER_UNINIT(m);
262 
263 	snd_mtxfree(m->lock);
264 	kobj_delete((kobj_t)m, M_MIXER);
265 
266 	return 0;
267 }
268 
269 int
270 mixer_reinit(device_t dev)
271 {
272 	struct snd_mixer *m;
273 	dev_t pdev;
274 	int i;
275 
276 	pdev = mixer_get_devt(dev);
277 	m = pdev->si_drv1;
278 	snd_mtxlock(m->lock);
279 
280 	i = MIXER_REINIT(m);
281 	if (i) {
282 		snd_mtxunlock(m->lock);
283 		return i;
284 	}
285 
286 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
287 		mixer_set(m, i, m->level[i]);
288 
289 	mixer_setrecsrc(m, m->recsrc);
290 	snd_mtxunlock(m->lock);
291 
292 	return 0;
293 }
294 
295 #ifdef SND_DYNSYSCTL
296 static int
297 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
298 {
299 	char devname[32];
300 	int error, dev;
301 	struct snd_mixer *m;
302 
303 	m = oidp->oid_arg1;
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);
309 		if (dev == -1) {
310 			snd_mtxunlock(m->lock);
311 			return EINVAL;
312 		}
313 		else if (dev != m->hwvol_mixer) {
314 			m->hwvol_mixer = dev;
315 			m->hwvol_muted = 0;
316 		}
317 	}
318 	snd_mtxunlock(m->lock);
319 	return error;
320 }
321 #endif
322 
323 int
324 mixer_hwvol_init(device_t dev)
325 {
326 	struct snd_mixer *m;
327 	dev_t pdev;
328 
329 	pdev = mixer_get_devt(dev);
330 	m = pdev->si_drv1;
331 	snd_mtxlock(m->lock);
332 
333 	m->hwvol_mixer = SOUND_MIXER_VOLUME;
334 	m->hwvol_step = 5;
335 #ifdef SND_DYNSYSCTL
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", "")
341 #endif
342 	snd_mtxunlock(m->lock);
343 	return 0;
344 }
345 
346 void
347 mixer_hwvol_mute(device_t dev)
348 {
349 	struct snd_mixer *m;
350 	dev_t pdev;
351 
352 	pdev = mixer_get_devt(dev);
353 	m = pdev->si_drv1;
354 	snd_mtxlock(m->lock);
355 	if (m->hwvol_muted) {
356 		m->hwvol_muted = 0;
357 		mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
358 	} else {
359 		m->hwvol_muted++;
360 		m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
361 		mixer_set(m, m->hwvol_mixer, 0);
362 	}
363 	snd_mtxunlock(m->lock);
364 }
365 
366 void
367 mixer_hwvol_step(device_t dev, int left_step, int right_step)
368 {
369 	struct snd_mixer *m;
370 	int level, left, right;
371 	dev_t pdev;
372 
373 	pdev = mixer_get_devt(dev);
374 	m = pdev->si_drv1;
375 	snd_mtxlock(m->lock);
376 	if (m->hwvol_muted) {
377 		m->hwvol_muted = 0;
378 		level = m->hwvol_mute_level;
379 	} else
380 		level = mixer_get(m, m->hwvol_mixer);
381 	if (level != -1) {
382 		left = level & 0xff;
383 		right = level >> 8;
384 		left += left_step * m->hwvol_step;
385 		if (left < 0)
386 			left = 0;
387 		right += right_step * m->hwvol_step;
388 		if (right < 0)
389 			right = 0;
390 		mixer_set(m, m->hwvol_mixer, left | right << 8);
391 	}
392 	snd_mtxunlock(m->lock);
393 }
394 
395 /* ----------------------------------------------------------------------- */
396 
397 static int
398 mixer_open(dev_t i_dev, int flags, int mode, struct proc *p)
399 {
400 	struct snd_mixer *m;
401 	intrmask_t s;
402 
403 	m = i_dev->si_drv1;
404 	s = spltty();
405 	snd_mtxlock(m->lock);
406 
407 	m->busy++;
408 
409 	snd_mtxunlock(m->lock);
410 	splx(s);
411 	return 0;
412 }
413 
414 static int
415 mixer_close(dev_t i_dev, int flags, int mode, struct proc *p)
416 {
417 	struct snd_mixer *m;
418 	intrmask_t s;
419 
420 	m = i_dev->si_drv1;
421 	s = spltty();
422 	snd_mtxlock(m->lock);
423 
424 	if (!m->busy) {
425 		snd_mtxunlock(m->lock);
426 		splx(s);
427 		return EBADF;
428 	}
429 	m->busy--;
430 
431 	snd_mtxunlock(m->lock);
432 	splx(s);
433 	return 0;
434 }
435 
436 int
437 mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc *p)
438 {
439 	struct snd_mixer *m;
440 	intrmask_t s;
441 	int ret, *arg_i = (int *)arg;
442 	int v = -1, j = cmd & 0xff;
443 
444 	m = i_dev->si_drv1;
445 	if (!m->busy)
446 		return EBADF;
447 
448 	s = spltty();
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);
453 		else
454 			ret = mixer_set(m, j, *arg_i);
455 		snd_mtxunlock(m->lock);
456 		splx(s);
457 		return (ret == 0)? 0 : ENXIO;
458 	}
459 
460     	if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
461 		switch (j) {
462     		case SOUND_MIXER_DEVMASK:
463     		case SOUND_MIXER_CAPS:
464     		case SOUND_MIXER_STEREODEVS:
465 			v = mix_getdevs(m);
466 			break;
467 
468     		case SOUND_MIXER_RECMASK:
469 			v = mix_getrecdevs(m);
470 			break;
471 
472     		case SOUND_MIXER_RECSRC:
473 			v = mixer_getrecsrc(m);
474 			break;
475 
476 		default:
477 			v = mixer_get(m, j);
478 		}
479 		*arg_i = v;
480 		snd_mtxunlock(m->lock);
481 		return (v != -1)? 0 : ENXIO;
482 	}
483 	snd_mtxunlock(m->lock);
484 	splx(s);
485 	return ENXIO;
486 }
487 
488 #ifdef USING_DEVFS
489 static void
490 mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
491 {
492 	dev_t pdev;
493 
494 	if (*dev != NODEV)
495 		return;
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)
499 			*dev = pdev;
500 	}
501 }
502 
503 static void
504 mixer_sysinit(void *p)
505 {
506 	mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
507 }
508 
509 static void
510 mixer_sysuninit(void *p)
511 {
512 	if (mixer_ehtag != NULL)
513 		EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
514 }
515 
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);
518 #endif
519 
520 
521