xref: /dragonfly/sys/dev/sound/pcm/mixer.c (revision c9f721c2)
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.7 2004/05/21 01:14:27 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.7 2004/05/21 01:14:27 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 	/* name */	"mixer",
80 	/* maj */	SND_CDEV_MAJOR,
81 	/* flags */	0,
82 	/* port */	NULL,
83 	/* clone */	NULL,
84 
85 	/* open */	mixer_open,
86 	/* close */	mixer_close,
87 	/* read */	noread,
88 	/* write */	nowrite,
89 	/* ioctl */	mixer_ioctl,
90 	/* poll */	nopoll,
91 	/* mmap */	nommap,
92 	/* strategy */	nostrategy,
93 	/* dump */	nodump,
94 	/* psize */	nopsize
95 };
96 
97 #ifdef USING_DEVFS
98 static eventhandler_tag mixer_ehtag;
99 #endif
100 
101 static dev_t
102 mixer_get_devt(device_t dev)
103 {
104 	dev_t pdev;
105 	int unit;
106 
107 	unit = device_get_unit(dev);
108 	pdev = make_adhoc_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0));
109 
110 	return pdev;
111 }
112 
113 #ifdef SND_DYNSYSCTL
114 static int
115 mixer_lookup(char *devname)
116 {
117 	int i;
118 
119 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
120 		if (strncmp(devname, snd_mixernames[i],
121 		    strlen(snd_mixernames[i])) == 0)
122 			return i;
123 	return -1;
124 }
125 #endif
126 
127 static int
128 mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
129 {
130 	unsigned l, r;
131 	int v;
132 
133 	if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
134 		return -1;
135 
136 	l = min((lev & 0x00ff), 100);
137 	r = min(((lev & 0xff00) >> 8), 100);
138 
139 	v = MIXER_SET(mixer, dev, l, r);
140 	if (v < 0)
141 		return -1;
142 
143 	mixer->level[dev] = l | (r << 8);
144 	return 0;
145 }
146 
147 static int
148 mixer_get(struct snd_mixer *mixer, int dev)
149 {
150 	if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
151 		return mixer->level[dev];
152 	else return -1;
153 }
154 
155 static int
156 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
157 {
158 	src &= mixer->recdevs;
159 	if (src == 0)
160 		src = SOUND_MASK_MIC;
161 	mixer->recsrc = MIXER_SETRECSRC(mixer, src);
162 	return 0;
163 }
164 
165 static int
166 mixer_getrecsrc(struct snd_mixer *mixer)
167 {
168 	return mixer->recsrc;
169 }
170 
171 void
172 mix_setdevs(struct snd_mixer *m, u_int32_t v)
173 {
174 	m->devs = v;
175 }
176 
177 void
178 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
179 {
180 	m->recdevs = v;
181 }
182 
183 u_int32_t
184 mix_getdevs(struct snd_mixer *m)
185 {
186 	return m->devs;
187 }
188 
189 u_int32_t
190 mix_getrecdevs(struct snd_mixer *m)
191 {
192 	return m->recdevs;
193 }
194 
195 void *
196 mix_getdevinfo(struct snd_mixer *m)
197 {
198 	return m->devinfo;
199 }
200 
201 int
202 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
203 {
204 	struct snd_mixer *m;
205 	u_int16_t v;
206 	dev_t pdev;
207 	int i, unit;
208 
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");
212 	m->type = cls->name;
213 	m->devinfo = devinfo;
214 	m->busy = 0;
215 
216 	if (MIXER_INIT(m))
217 		goto bad;
218 
219 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
220 		v = snd_mixerdefaults[i];
221 		mixer_set(m, i, v | (v << 8));
222 	}
223 
224 	mixer_setrecsrc(m, SOUND_MASK_MIC);
225 
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);
231 	pdev->si_drv1 = m;
232 
233 	return 0;
234 
235 bad:
236 	snd_mtxlock(m->lock);
237 	snd_mtxfree(m->lock);
238 	kobj_delete((kobj_t)m, M_MIXER);
239 	return -1;
240 }
241 
242 int
243 mixer_uninit(device_t dev)
244 {
245 	int i;
246 	int unit;
247 	struct snd_mixer *m;
248 	dev_t pdev;
249 
250 	pdev = mixer_get_devt(dev);
251 	m = pdev->si_drv1;
252 	snd_mtxlock(m->lock);
253 
254 	if (m->busy) {
255 		snd_mtxunlock(m->lock);
256 		return EBUSY;
257 	}
258 
259 	pdev->si_drv1 = NULL;
260 
261 	unit = device_get_unit(dev);
262 	cdevsw_remove(&mixer_cdevsw,
263 		    PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_CTL, 0));
264 
265 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
266 		mixer_set(m, i, 0);
267 
268 	mixer_setrecsrc(m, SOUND_MASK_MIC);
269 
270 	MIXER_UNINIT(m);
271 
272 	snd_mtxfree(m->lock);
273 	kobj_delete((kobj_t)m, M_MIXER);
274 
275 	return 0;
276 }
277 
278 int
279 mixer_reinit(device_t dev)
280 {
281 	struct snd_mixer *m;
282 	dev_t pdev;
283 	int i;
284 
285 	pdev = mixer_get_devt(dev);
286 	m = pdev->si_drv1;
287 	snd_mtxlock(m->lock);
288 
289 	i = MIXER_REINIT(m);
290 	if (i) {
291 		snd_mtxunlock(m->lock);
292 		return i;
293 	}
294 
295 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
296 		mixer_set(m, i, m->level[i]);
297 
298 	mixer_setrecsrc(m, m->recsrc);
299 	snd_mtxunlock(m->lock);
300 
301 	return 0;
302 }
303 
304 #ifdef SND_DYNSYSCTL
305 static int
306 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
307 {
308 	char devname[32];
309 	int error, dev;
310 	struct snd_mixer *m;
311 
312 	m = oidp->oid_arg1;
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);
318 		if (dev == -1) {
319 			snd_mtxunlock(m->lock);
320 			return EINVAL;
321 		}
322 		else if (dev != m->hwvol_mixer) {
323 			m->hwvol_mixer = dev;
324 			m->hwvol_muted = 0;
325 		}
326 	}
327 	snd_mtxunlock(m->lock);
328 	return error;
329 }
330 #endif
331 
332 int
333 mixer_hwvol_init(device_t dev)
334 {
335 	struct snd_mixer *m;
336 	dev_t pdev;
337 
338 	pdev = mixer_get_devt(dev);
339 	m = pdev->si_drv1;
340 	snd_mtxlock(m->lock);
341 
342 	m->hwvol_mixer = SOUND_MIXER_VOLUME;
343 	m->hwvol_step = 5;
344 #ifdef SND_DYNSYSCTL
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", "")
350 #endif
351 	snd_mtxunlock(m->lock);
352 	return 0;
353 }
354 
355 void
356 mixer_hwvol_mute(device_t dev)
357 {
358 	struct snd_mixer *m;
359 	dev_t pdev;
360 
361 	pdev = mixer_get_devt(dev);
362 	m = pdev->si_drv1;
363 	snd_mtxlock(m->lock);
364 	if (m->hwvol_muted) {
365 		m->hwvol_muted = 0;
366 		mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
367 	} else {
368 		m->hwvol_muted++;
369 		m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
370 		mixer_set(m, m->hwvol_mixer, 0);
371 	}
372 	snd_mtxunlock(m->lock);
373 }
374 
375 void
376 mixer_hwvol_step(device_t dev, int left_step, int right_step)
377 {
378 	struct snd_mixer *m;
379 	int level, left, right;
380 	dev_t pdev;
381 
382 	pdev = mixer_get_devt(dev);
383 	m = pdev->si_drv1;
384 	snd_mtxlock(m->lock);
385 	if (m->hwvol_muted) {
386 		m->hwvol_muted = 0;
387 		level = m->hwvol_mute_level;
388 	} else
389 		level = mixer_get(m, m->hwvol_mixer);
390 	if (level != -1) {
391 		left = level & 0xff;
392 		right = level >> 8;
393 		left += left_step * m->hwvol_step;
394 		if (left < 0)
395 			left = 0;
396 		right += right_step * m->hwvol_step;
397 		if (right < 0)
398 			right = 0;
399 		mixer_set(m, m->hwvol_mixer, left | right << 8);
400 	}
401 	snd_mtxunlock(m->lock);
402 }
403 
404 /* ----------------------------------------------------------------------- */
405 
406 static int
407 mixer_open(dev_t i_dev, int flags, int mode, struct thread *td)
408 {
409 	struct snd_mixer *m;
410 	intrmask_t s;
411 
412 	m = i_dev->si_drv1;
413 	s = spltty();
414 	snd_mtxlock(m->lock);
415 
416 	m->busy++;
417 
418 	snd_mtxunlock(m->lock);
419 	splx(s);
420 	return 0;
421 }
422 
423 static int
424 mixer_close(dev_t i_dev, int flags, int mode, struct thread *td)
425 {
426 	struct snd_mixer *m;
427 	intrmask_t s;
428 
429 	m = i_dev->si_drv1;
430 	s = spltty();
431 	snd_mtxlock(m->lock);
432 
433 	if (!m->busy) {
434 		snd_mtxunlock(m->lock);
435 		splx(s);
436 		return EBADF;
437 	}
438 	m->busy--;
439 
440 	snd_mtxunlock(m->lock);
441 	splx(s);
442 	return 0;
443 }
444 
445 int
446 mixer_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
447 {
448 	struct snd_mixer *m;
449 	intrmask_t s;
450 	int ret, *arg_i = (int *)arg;
451 	int v = -1, j = cmd & 0xff;
452 
453 	m = i_dev->si_drv1;
454 	if (!m->busy)
455 		return EBADF;
456 
457 	s = spltty();
458 	snd_mtxlock(m->lock);
459 	if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
460 		if (j == SOUND_MIXER_RECSRC)
461 			ret = mixer_setrecsrc(m, *arg_i);
462 		else
463 			ret = mixer_set(m, j, *arg_i);
464 		snd_mtxunlock(m->lock);
465 		splx(s);
466 		return (ret == 0)? 0 : ENXIO;
467 	}
468 
469     	if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
470 		switch (j) {
471     		case SOUND_MIXER_DEVMASK:
472     		case SOUND_MIXER_CAPS:
473     		case SOUND_MIXER_STEREODEVS:
474 			v = mix_getdevs(m);
475 			break;
476 
477     		case SOUND_MIXER_RECMASK:
478 			v = mix_getrecdevs(m);
479 			break;
480 
481     		case SOUND_MIXER_RECSRC:
482 			v = mixer_getrecsrc(m);
483 			break;
484 
485 		default:
486 			v = mixer_get(m, j);
487 		}
488 		*arg_i = v;
489 		snd_mtxunlock(m->lock);
490 		return (v != -1)? 0 : ENXIO;
491 	}
492 	snd_mtxunlock(m->lock);
493 	splx(s);
494 	return ENXIO;
495 }
496 
497 #ifdef USING_DEVFS
498 static void
499 mixer_clone(void *arg, char *name, int namelen, dev_t *dev)
500 {
501 	dev_t pdev;
502 
503 	if (*dev != NODEV)
504 		return;
505 	if (strcmp(name, "mixer") == 0) {
506 		pdev = make_adhoc_dev(&mixer_cdevsw,
507 					PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
508 		if (pdev->si_flags & SI_NAMED)
509 			*dev = pdev;
510 	}
511 }
512 
513 static void
514 mixer_sysinit(void *p)
515 {
516 	mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
517 }
518 
519 static void
520 mixer_sysuninit(void *p)
521 {
522 	if (mixer_ehtag != NULL)
523 		EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
524 }
525 
526 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
527 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
528 #endif
529 
530 
531