xref: /freebsd/sys/dev/sound/pcm/mixer.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifdef HAVE_KERNEL_OPTION_HEADERS
32 #include "opt_snd.h"
33 #endif
34 
35 #include <dev/sound/pcm/sound.h>
36 
37 #include "feeder_if.h"
38 #include "mixer_if.h"
39 
40 static MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
41 
42 static int mixer_bypass = 1;
43 SYSCTL_INT(_hw_snd, OID_AUTO, vpc_mixer_bypass, CTLFLAG_RWTUN,
44     &mixer_bypass, 0,
45     "control channel pcm/rec volume, bypassing real mixer device");
46 
47 #define MIXER_NAMELEN	16
48 struct snd_mixer {
49 	KOBJ_FIELDS;
50 	void *devinfo;
51 	int busy;
52 	int hwvol_mixer;
53 	int hwvol_step;
54 	int type;
55 	device_t dev;
56 	u_int32_t devs;
57 	u_int32_t mutedevs;
58 	u_int32_t recdevs;
59 	u_int32_t recsrc;
60 	u_int16_t level[32];
61 	u_int16_t level_muted[32];
62 	u_int8_t parent[32];
63 	u_int32_t child[32];
64 	u_int8_t realdev[32];
65 	char name[MIXER_NAMELEN];
66 	struct mtx *lock;
67 	oss_mixer_enuminfo enuminfo;
68 	/**
69 	 * Counter is incremented when applications change any of this
70 	 * mixer's controls.  A change in value indicates that persistent
71 	 * mixer applications should update their displays.
72 	 */
73 	int modify_counter;
74 };
75 
76 static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
77 	[SOUND_MIXER_VOLUME]	= 75,
78 	[SOUND_MIXER_BASS]	= 50,
79 	[SOUND_MIXER_TREBLE]	= 50,
80 	[SOUND_MIXER_SYNTH]	= 75,
81 	[SOUND_MIXER_PCM]	= 75,
82 	[SOUND_MIXER_SPEAKER]	= 75,
83 	[SOUND_MIXER_LINE]	= 75,
84 	[SOUND_MIXER_MIC] 	= 25,
85 	[SOUND_MIXER_CD]	= 75,
86 	[SOUND_MIXER_IGAIN]	= 0,
87 	[SOUND_MIXER_LINE1]	= 75,
88 	[SOUND_MIXER_VIDEO]	= 75,
89 	[SOUND_MIXER_RECLEV]	= 75,
90 	[SOUND_MIXER_OGAIN]	= 50,
91 	[SOUND_MIXER_MONITOR]	= 75,
92 };
93 
94 static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
95 
96 static d_open_t mixer_open;
97 static d_close_t mixer_close;
98 static d_ioctl_t mixer_ioctl;
99 
100 static struct cdevsw mixer_cdevsw = {
101 	.d_version =	D_VERSION,
102 	.d_open =	mixer_open,
103 	.d_close =	mixer_close,
104 	.d_ioctl =	mixer_ioctl,
105 	.d_name =	"mixer",
106 };
107 
108 /**
109  * Keeps a count of mixer devices; used only by OSSv4 SNDCTL_SYSINFO ioctl.
110  */
111 int mixer_count = 0;
112 
113 static eventhandler_tag mixer_ehtag = NULL;
114 
115 static struct cdev *
116 mixer_get_devt(device_t dev)
117 {
118 	struct snddev_info *snddev;
119 
120 	snddev = device_get_softc(dev);
121 
122 	return snddev->mixer_dev;
123 }
124 
125 static int
126 mixer_lookup(char *devname)
127 {
128 	int i;
129 
130 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
131 		if (strncmp(devname, snd_mixernames[i],
132 		    strlen(snd_mixernames[i])) == 0)
133 			return i;
134 	return -1;
135 }
136 
137 #define MIXER_SET_UNLOCK(x, y)		do {				\
138 	if ((y) != 0)							\
139 		snd_mtxunlock((x)->lock);				\
140 } while (0)
141 
142 #define MIXER_SET_LOCK(x, y)		do {				\
143 	if ((y) != 0)							\
144 		snd_mtxlock((x)->lock);					\
145 } while (0)
146 
147 static int
148 mixer_set_softpcmvol(struct snd_mixer *m, struct snddev_info *d,
149     u_int left, u_int right)
150 {
151 	struct pcm_channel *c;
152 	int dropmtx, acquiremtx;
153 
154 	if (!PCM_REGISTERED(d) || PCM_DETACHING(d))
155 		return (EINVAL);
156 
157 	if (mtx_owned(m->lock))
158 		dropmtx = 1;
159 	else
160 		dropmtx = 0;
161 
162 	if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 0)
163 		acquiremtx = 0;
164 	else
165 		acquiremtx = 1;
166 
167 	/*
168 	 * Be careful here. If we're coming from cdev ioctl, it is OK to
169 	 * not doing locking AT ALL (except on individual channel) since
170 	 * we've been heavily guarded by pcm cv, or if we're still
171 	 * under Giant influence. Since we also have mix_* calls, we cannot
172 	 * assume such protection and just do the lock as usuall.
173 	 */
174 	MIXER_SET_UNLOCK(m, dropmtx);
175 	MIXER_SET_LOCK(d, acquiremtx);
176 
177 	CHN_FOREACH(c, d, channels.pcm.busy) {
178 		CHN_LOCK(c);
179 		if (c->direction == PCMDIR_PLAY &&
180 		    (c->feederflags & (1 << FEEDER_VOLUME)))
181 			chn_setvolume_multi(c, SND_VOL_C_MASTER, left, right,
182 			    (left + right) >> 1);
183 		CHN_UNLOCK(c);
184 	}
185 
186 	MIXER_SET_UNLOCK(d, acquiremtx);
187 	MIXER_SET_LOCK(m, dropmtx);
188 
189 	return (0);
190 }
191 
192 static int
193 mixer_set_eq(struct snd_mixer *m, struct snddev_info *d,
194     u_int dev, u_int level)
195 {
196 	struct pcm_channel *c;
197 	struct pcm_feeder *f;
198 	int tone, dropmtx, acquiremtx;
199 
200 	if (dev == SOUND_MIXER_TREBLE)
201 		tone = FEEDEQ_TREBLE;
202 	else if (dev == SOUND_MIXER_BASS)
203 		tone = FEEDEQ_BASS;
204 	else
205 		return (EINVAL);
206 
207 	if (!PCM_REGISTERED(d) || PCM_DETACHING(d))
208 		return (EINVAL);
209 
210 	if (mtx_owned(m->lock))
211 		dropmtx = 1;
212 	else
213 		dropmtx = 0;
214 
215 	if (!(d->flags & SD_F_MPSAFE) || mtx_owned(d->lock) != 0)
216 		acquiremtx = 0;
217 	else
218 		acquiremtx = 1;
219 
220 	/*
221 	 * Be careful here. If we're coming from cdev ioctl, it is OK to
222 	 * not doing locking AT ALL (except on individual channel) since
223 	 * we've been heavily guarded by pcm cv, or if we're still
224 	 * under Giant influence. Since we also have mix_* calls, we cannot
225 	 * assume such protection and just do the lock as usuall.
226 	 */
227 	MIXER_SET_UNLOCK(m, dropmtx);
228 	MIXER_SET_LOCK(d, acquiremtx);
229 
230 	CHN_FOREACH(c, d, channels.pcm.busy) {
231 		CHN_LOCK(c);
232 		f = chn_findfeeder(c, FEEDER_EQ);
233 		if (f != NULL)
234 			(void)FEEDER_SET(f, tone, level);
235 		CHN_UNLOCK(c);
236 	}
237 
238 	MIXER_SET_UNLOCK(d, acquiremtx);
239 	MIXER_SET_LOCK(m, dropmtx);
240 
241 	return (0);
242 }
243 
244 static int
245 mixer_set(struct snd_mixer *m, u_int dev, u_int32_t muted, u_int lev)
246 {
247 	struct snddev_info *d;
248 	u_int l, r, tl, tr;
249 	u_int32_t parent = SOUND_MIXER_NONE, child = 0;
250 	u_int32_t realdev;
251 	int i, dropmtx;
252 
253 	if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
254 	    (0 == (m->devs & (1 << dev))))
255 		return (-1);
256 
257 	l = min((lev & 0x00ff), 100);
258 	r = min(((lev & 0xff00) >> 8), 100);
259 	realdev = m->realdev[dev];
260 
261 	d = device_get_softc(m->dev);
262 	if (d == NULL)
263 		return (-1);
264 
265 	/* It is safe to drop this mutex due to Giant. */
266 	if (!(d->flags & SD_F_MPSAFE) && mtx_owned(m->lock) != 0)
267 		dropmtx = 1;
268 	else
269 		dropmtx = 0;
270 
271 	/* Allow the volume to be "changed" while muted. */
272 	if (muted & (1 << dev)) {
273 		m->level_muted[dev] = l | (r << 8);
274 		return (0);
275 	}
276 	MIXER_SET_UNLOCK(m, dropmtx);
277 
278 	/* TODO: recursive handling */
279 	parent = m->parent[dev];
280 	if (parent >= SOUND_MIXER_NRDEVICES)
281 		parent = SOUND_MIXER_NONE;
282 	if (parent == SOUND_MIXER_NONE)
283 		child = m->child[dev];
284 
285 	if (parent != SOUND_MIXER_NONE) {
286 		tl = (l * (m->level[parent] & 0x00ff)) / 100;
287 		tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
288 		if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
289 			(void)mixer_set_softpcmvol(m, d, tl, tr);
290 		else if (realdev != SOUND_MIXER_NONE &&
291 		    MIXER_SET(m, realdev, tl, tr) < 0) {
292 			MIXER_SET_LOCK(m, dropmtx);
293 			return (-1);
294 		}
295 	} else if (child != 0) {
296 		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
297 			if (!(child & (1 << i)) || m->parent[i] != dev)
298 				continue;
299 			realdev = m->realdev[i];
300 			tl = (l * (m->level[i] & 0x00ff)) / 100;
301 			tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
302 			if (i == SOUND_MIXER_PCM &&
303 			    (d->flags & SD_F_SOFTPCMVOL))
304 				(void)mixer_set_softpcmvol(m, d, tl, tr);
305 			else if (realdev != SOUND_MIXER_NONE)
306 				MIXER_SET(m, realdev, tl, tr);
307 		}
308 		realdev = m->realdev[dev];
309 		if (realdev != SOUND_MIXER_NONE &&
310 		    MIXER_SET(m, realdev, l, r) < 0) {
311 			MIXER_SET_LOCK(m, dropmtx);
312 			return (-1);
313 		}
314 	} else {
315 		if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
316 			(void)mixer_set_softpcmvol(m, d, l, r);
317 		else if ((dev == SOUND_MIXER_TREBLE ||
318 		    dev == SOUND_MIXER_BASS) && (d->flags & SD_F_EQ))
319 			(void)mixer_set_eq(m, d, dev, (l + r) >> 1);
320 		else if (realdev != SOUND_MIXER_NONE &&
321 		    MIXER_SET(m, realdev, l, r) < 0) {
322 			MIXER_SET_LOCK(m, dropmtx);
323 			return (-1);
324 		}
325 	}
326 
327 	MIXER_SET_LOCK(m, dropmtx);
328 
329 	m->level[dev] = l | (r << 8);
330 	m->modify_counter++;
331 
332 	return (0);
333 }
334 
335 static int
336 mixer_get(struct snd_mixer *mixer, int dev)
337 {
338 	if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev))) {
339 		if (mixer->mutedevs & (1 << dev))
340 			return (mixer->level_muted[dev]);
341 		else
342 			return (mixer->level[dev]);
343 	} else {
344 		return (-1);
345 	}
346 }
347 
348 void
349 mix_setmutedevs(struct snd_mixer *mixer, u_int32_t mutedevs)
350 {
351 	u_int32_t delta;
352 
353 	/* Filter out invalid values. */
354 	mutedevs &= mixer->devs;
355 	delta = (mixer->mutedevs ^ mutedevs) & mixer->devs;
356 	mixer->mutedevs = mutedevs;
357 
358 	for (int i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
359 		if (!(delta & (1 << i)))
360 			continue;
361 		if (mutedevs & (1 << i)) {
362 			mixer->level_muted[i] = mixer->level[i];
363 			mixer_set(mixer, i, 0, 0);
364 		} else {
365 			mixer_set(mixer, i, 0, mixer->level_muted[i]);
366 		}
367 	}
368 }
369 
370 static int
371 mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
372 {
373 	struct snddev_info *d;
374 	u_int32_t recsrc;
375 	int dropmtx;
376 
377 	d = device_get_softc(mixer->dev);
378 	if (d == NULL)
379 		return -1;
380 	if (!(d->flags & SD_F_MPSAFE) && mtx_owned(mixer->lock) != 0)
381 		dropmtx = 1;
382 	else
383 		dropmtx = 0;
384 	src &= mixer->recdevs;
385 	if (src == 0)
386 		src = mixer->recdevs & SOUND_MASK_MIC;
387 	if (src == 0)
388 		src = mixer->recdevs & SOUND_MASK_MONITOR;
389 	if (src == 0)
390 		src = mixer->recdevs & SOUND_MASK_LINE;
391 	if (src == 0 && mixer->recdevs != 0)
392 		src = (1 << (ffs(mixer->recdevs) - 1));
393 	/* It is safe to drop this mutex due to Giant. */
394 	MIXER_SET_UNLOCK(mixer, dropmtx);
395 	recsrc = MIXER_SETRECSRC(mixer, src);
396 	MIXER_SET_LOCK(mixer, dropmtx);
397 
398 	mixer->recsrc = recsrc;
399 
400 	return 0;
401 }
402 
403 static int
404 mixer_getrecsrc(struct snd_mixer *mixer)
405 {
406 	return mixer->recsrc;
407 }
408 
409 /**
410  * @brief Retrieve the route number of the current recording device
411  *
412  * OSSv4 assigns routing numbers to recording devices, unlike the previous
413  * API which relied on a fixed table of device numbers and names.  This
414  * function returns the routing number of the device currently selected
415  * for recording.
416  *
417  * For now, this function is kind of a goofy compatibility stub atop the
418  * existing sound system.  (For example, in theory, the old sound system
419  * allows multiple recording devices to be specified via a bitmask.)
420  *
421  * @param m	mixer context container thing
422  *
423  * @retval 0		success
424  * @retval EIDRM	no recording device found (generally not possible)
425  * @todo Ask about error code
426  */
427 static int
428 mixer_get_recroute(struct snd_mixer *m, int *route)
429 {
430 	int i, cnt;
431 
432 	cnt = 0;
433 
434 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
435 		/** @todo can user set a multi-device mask? (== or &?) */
436 		if ((1 << i) == m->recsrc)
437 			break;
438 		if ((1 << i) & m->recdevs)
439 			++cnt;
440 	}
441 
442 	if (i == SOUND_MIXER_NRDEVICES)
443 		return EIDRM;
444 
445 	*route = cnt;
446 	return 0;
447 }
448 
449 /**
450  * @brief Select a device for recording
451  *
452  * This function sets a recording source based on a recording device's
453  * routing number.  Said number is translated to an old school recdev
454  * mask and passed over mixer_setrecsrc.
455  *
456  * @param m	mixer context container thing
457  *
458  * @retval 0		success(?)
459  * @retval EINVAL	User specified an invalid device number
460  * @retval otherwise	error from mixer_setrecsrc
461  */
462 static int
463 mixer_set_recroute(struct snd_mixer *m, int route)
464 {
465 	int i, cnt, ret;
466 
467 	ret = 0;
468 	cnt = 0;
469 
470 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
471 		if ((1 << i) & m->recdevs) {
472 			if (route == cnt)
473 				break;
474 			++cnt;
475 		}
476 	}
477 
478 	if (i == SOUND_MIXER_NRDEVICES)
479 		ret = EINVAL;
480 	else
481 		ret = mixer_setrecsrc(m, (1 << i));
482 
483 	return ret;
484 }
485 
486 void
487 mix_setdevs(struct snd_mixer *m, u_int32_t v)
488 {
489 	struct snddev_info *d;
490 	int i;
491 
492 	if (m == NULL)
493 		return;
494 
495 	d = device_get_softc(m->dev);
496 	if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
497 		v |= SOUND_MASK_PCM;
498 	if (d != NULL && (d->flags & SD_F_EQ))
499 		v |= SOUND_MASK_TREBLE | SOUND_MASK_BASS;
500 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
501 		if (m->parent[i] < SOUND_MIXER_NRDEVICES)
502 			v |= 1 << m->parent[i];
503 		v |= m->child[i];
504 	}
505 	m->devs = v;
506 }
507 
508 /**
509  * @brief Record mask of available recording devices
510  *
511  * Calling functions are responsible for defining the mask of available
512  * recording devices.  This function records that value in a structure
513  * used by the rest of the mixer code.
514  *
515  * This function also populates a structure used by the SNDCTL_DSP_*RECSRC*
516  * family of ioctls that are part of OSSV4.  All recording device labels
517  * are concatenated in ascending order corresponding to their routing
518  * numbers.  (Ex:  a system might have 0 => 'vol', 1 => 'cd', 2 => 'line',
519  * etc.)  For now, these labels are just the standard recording device
520  * names (cd, line1, etc.), but will eventually be fully dynamic and user
521  * controlled.
522  *
523  * @param m	mixer device context container thing
524  * @param v	mask of recording devices
525  */
526 void
527 mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
528 {
529 	oss_mixer_enuminfo *ei;
530 	char *loc;
531 	int i, nvalues, nwrote, nleft, ncopied;
532 
533 	ei = &m->enuminfo;
534 
535 	nvalues = 0;
536 	nwrote = 0;
537 	nleft = sizeof(ei->strings);
538 	loc = ei->strings;
539 
540 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
541 		if ((1 << i) & v) {
542 			ei->strindex[nvalues] = nwrote;
543 			ncopied = strlcpy(loc, snd_mixernames[i], nleft) + 1;
544 			    /* strlcpy retval doesn't include terminator */
545 
546 			nwrote += ncopied;
547 			nleft -= ncopied;
548 			nvalues++;
549 
550 			/*
551 			 * XXX I don't think this should ever be possible.
552 			 * Even with a move to dynamic device/channel names,
553 			 * each label is limited to ~16 characters, so that'd
554 			 * take a LOT to fill this buffer.
555 			 */
556 			if ((nleft <= 0) || (nvalues >= OSS_ENUM_MAXVALUE)) {
557 				device_printf(m->dev,
558 				    "mix_setrecdevs:  Not enough room to store device names--please file a bug report.\n");
559 				device_printf(m->dev,
560 				    "mix_setrecdevs:  Please include details about your sound hardware, OS version, etc.\n");
561 				break;
562 			}
563 
564 			loc = &ei->strings[nwrote];
565 		}
566 	}
567 
568 	/*
569 	 * NB:	The SNDCTL_DSP_GET_RECSRC_NAMES ioctl ignores the dev
570 	 * 	and ctrl fields.
571 	 */
572 	ei->nvalues = nvalues;
573 	m->recdevs = v;
574 }
575 
576 void
577 mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
578 {
579 	u_int32_t mask = 0;
580 	int i;
581 
582 	if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
583 		return;
584 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
585 		if (i == parent)
586 			continue;
587 		if (childs & (1 << i)) {
588 			mask |= 1 << i;
589 			if (m->parent[i] < SOUND_MIXER_NRDEVICES)
590 				m->child[m->parent[i]] &= ~(1 << i);
591 			m->parent[i] = parent;
592 			m->child[i] = 0;
593 		}
594 	}
595 	mask &= ~(1 << parent);
596 	m->child[parent] = mask;
597 }
598 
599 void
600 mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
601 {
602 	if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
603 	    !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
604 		return;
605 	m->realdev[dev] = realdev;
606 }
607 
608 u_int32_t
609 mix_getparent(struct snd_mixer *m, u_int32_t dev)
610 {
611 	if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
612 		return SOUND_MIXER_NONE;
613 	return m->parent[dev];
614 }
615 
616 u_int32_t
617 mix_getchild(struct snd_mixer *m, u_int32_t dev)
618 {
619 	if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
620 		return 0;
621 	return m->child[dev];
622 }
623 
624 u_int32_t
625 mix_getdevs(struct snd_mixer *m)
626 {
627 	return m->devs;
628 }
629 
630 u_int32_t
631 mix_getmutedevs(struct snd_mixer *m)
632 {
633 	return m->mutedevs;
634 }
635 
636 u_int32_t
637 mix_getrecdevs(struct snd_mixer *m)
638 {
639 	return m->recdevs;
640 }
641 
642 void *
643 mix_getdevinfo(struct snd_mixer *m)
644 {
645 	return m->devinfo;
646 }
647 
648 static struct snd_mixer *
649 mixer_obj_create(device_t dev, kobj_class_t cls, void *devinfo,
650     int type, const char *desc)
651 {
652 	struct snd_mixer *m;
653 	int i;
654 
655 	KASSERT(dev != NULL && cls != NULL && devinfo != NULL,
656 	    ("%s(): NULL data dev=%p cls=%p devinfo=%p",
657 	    __func__, dev, cls, devinfo));
658 	KASSERT(type == MIXER_TYPE_PRIMARY || type == MIXER_TYPE_SECONDARY,
659 	    ("invalid mixer type=%d", type));
660 
661 	m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
662 	snprintf(m->name, sizeof(m->name), "%s:mixer",
663 	    device_get_nameunit(dev));
664 	if (desc != NULL) {
665 		strlcat(m->name, ":", sizeof(m->name));
666 		strlcat(m->name, desc, sizeof(m->name));
667 	}
668 	m->lock = snd_mtxcreate(m->name, (type == MIXER_TYPE_PRIMARY) ?
669 	    "primary pcm mixer" : "secondary pcm mixer");
670 	m->type = type;
671 	m->devinfo = devinfo;
672 	m->busy = 0;
673 	m->dev = dev;
674 	for (i = 0; i < (sizeof(m->parent) / sizeof(m->parent[0])); i++) {
675 		m->parent[i] = SOUND_MIXER_NONE;
676 		m->child[i] = 0;
677 		m->realdev[i] = i;
678 	}
679 
680 	if (MIXER_INIT(m)) {
681 		snd_mtxlock(m->lock);
682 		snd_mtxfree(m->lock);
683 		kobj_delete((kobj_t)m, M_MIXER);
684 		return (NULL);
685 	}
686 
687 	return (m);
688 }
689 
690 int
691 mixer_delete(struct snd_mixer *m)
692 {
693 	KASSERT(m != NULL, ("NULL snd_mixer"));
694 	KASSERT(m->type == MIXER_TYPE_SECONDARY,
695 	    ("%s(): illegal mixer type=%d", __func__, m->type));
696 
697 	/* mixer uninit can sleep --hps */
698 
699 	MIXER_UNINIT(m);
700 
701 	snd_mtxfree(m->lock);
702 	kobj_delete((kobj_t)m, M_MIXER);
703 
704 	--mixer_count;
705 
706 	return (0);
707 }
708 
709 struct snd_mixer *
710 mixer_create(device_t dev, kobj_class_t cls, void *devinfo, const char *desc)
711 {
712 	struct snd_mixer *m;
713 
714 	m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_SECONDARY, desc);
715 
716 	if (m != NULL)
717 		++mixer_count;
718 
719 	return (m);
720 }
721 
722 int
723 mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
724 {
725 	struct snddev_info *snddev;
726 	struct snd_mixer *m;
727 	u_int16_t v;
728 	struct cdev *pdev;
729 	const char *name;
730 	int i, unit, val;
731 
732 	snddev = device_get_softc(dev);
733 	if (snddev == NULL)
734 		return (-1);
735 
736 	name = device_get_name(dev);
737 	unit = device_get_unit(dev);
738 	if (resource_int_value(name, unit, "eq", &val) == 0 &&
739 	    val != 0) {
740 		snddev->flags |= SD_F_EQ;
741 		if ((val & SD_F_EQ_MASK) == val)
742 			snddev->flags |= val;
743 		else
744 			snddev->flags |= SD_F_EQ_DEFAULT;
745 		snddev->eqpreamp = 0;
746 	}
747 
748 	m = mixer_obj_create(dev, cls, devinfo, MIXER_TYPE_PRIMARY, NULL);
749 	if (m == NULL)
750 		return (-1);
751 
752 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
753 		v = snd_mixerdefaults[i];
754 
755 		if (resource_int_value(name, unit, snd_mixernames[i],
756 		    &val) == 0) {
757 			if (val >= 0 && val <= 100) {
758 				v = (u_int16_t) val;
759 			}
760 		}
761 
762 		mixer_set(m, i, 0, v | (v << 8));
763 	}
764 
765 	mixer_setrecsrc(m, 0); /* Set default input. */
766 
767 	pdev = make_dev(&mixer_cdevsw, SND_DEV_CTL, UID_ROOT, GID_WHEEL, 0666,
768 	    "mixer%d", unit);
769 	pdev->si_drv1 = m;
770 	snddev->mixer_dev = pdev;
771 
772 	++mixer_count;
773 
774 	if (bootverbose) {
775 		for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
776 			if (!(m->devs & (1 << i)))
777 				continue;
778 			if (m->realdev[i] != i) {
779 				device_printf(dev, "Mixer \"%s\" -> \"%s\":",
780 				    snd_mixernames[i],
781 				    (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
782 				    snd_mixernames[m->realdev[i]] : "none");
783 			} else {
784 				device_printf(dev, "Mixer \"%s\":",
785 				    snd_mixernames[i]);
786 			}
787 			if (m->parent[i] < SOUND_MIXER_NRDEVICES)
788 				printf(" parent=\"%s\"",
789 				    snd_mixernames[m->parent[i]]);
790 			if (m->child[i] != 0)
791 				printf(" child=0x%08x", m->child[i]);
792 			printf("\n");
793 		}
794 		if (snddev->flags & SD_F_SOFTPCMVOL)
795 			device_printf(dev, "Soft PCM mixer ENABLED\n");
796 		if (snddev->flags & SD_F_EQ)
797 			device_printf(dev, "EQ Treble/Bass ENABLED\n");
798 	}
799 
800 	return (0);
801 }
802 
803 int
804 mixer_uninit(device_t dev)
805 {
806 	int i;
807 	struct snddev_info *d;
808 	struct snd_mixer *m;
809 	struct cdev *pdev;
810 
811 	d = device_get_softc(dev);
812 	pdev = mixer_get_devt(dev);
813 	if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
814 		return EBADF;
815 
816 	m = pdev->si_drv1;
817 	KASSERT(m != NULL, ("NULL snd_mixer"));
818 	KASSERT(m->type == MIXER_TYPE_PRIMARY,
819 	    ("%s(): illegal mixer type=%d", __func__, m->type));
820 
821 	pdev->si_drv1 = NULL;
822 	destroy_dev(pdev);
823 
824 	snd_mtxlock(m->lock);
825 
826 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
827 		mixer_set(m, i, 0, 0);
828 
829 	mixer_setrecsrc(m, SOUND_MASK_MIC);
830 
831 	snd_mtxunlock(m->lock);
832 
833 	/* mixer uninit can sleep --hps */
834 
835 	MIXER_UNINIT(m);
836 
837 	snd_mtxfree(m->lock);
838 	kobj_delete((kobj_t)m, M_MIXER);
839 
840 	d->mixer_dev = NULL;
841 
842 	--mixer_count;
843 
844 	return 0;
845 }
846 
847 int
848 mixer_reinit(device_t dev)
849 {
850 	struct snd_mixer *m;
851 	struct cdev *pdev;
852 	int i;
853 
854 	pdev = mixer_get_devt(dev);
855 	m = pdev->si_drv1;
856 	snd_mtxlock(m->lock);
857 
858 	i = MIXER_REINIT(m);
859 	if (i) {
860 		snd_mtxunlock(m->lock);
861 		return i;
862 	}
863 
864 	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
865 		if (m->mutedevs & (1 << i))
866 			mixer_set(m, i, 0, 0);
867 		else
868 			mixer_set(m, i, 0, m->level[i]);
869 	}
870 
871 	mixer_setrecsrc(m, m->recsrc);
872 	snd_mtxunlock(m->lock);
873 
874 	return 0;
875 }
876 
877 static int
878 sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
879 {
880 	char devname[32];
881 	int error, dev;
882 	struct snd_mixer *m;
883 
884 	m = oidp->oid_arg1;
885 	snd_mtxlock(m->lock);
886 	strlcpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
887 	snd_mtxunlock(m->lock);
888 	error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
889 	snd_mtxlock(m->lock);
890 	if (error == 0 && req->newptr != NULL) {
891 		dev = mixer_lookup(devname);
892 		if (dev == -1) {
893 			snd_mtxunlock(m->lock);
894 			return EINVAL;
895 		} else {
896 			m->hwvol_mixer = dev;
897 		}
898 	}
899 	snd_mtxunlock(m->lock);
900 	return error;
901 }
902 
903 int
904 mixer_hwvol_init(device_t dev)
905 {
906 	struct snd_mixer *m;
907 	struct cdev *pdev;
908 
909 	pdev = mixer_get_devt(dev);
910 	m = pdev->si_drv1;
911 
912 	m->hwvol_mixer = SOUND_MIXER_VOLUME;
913 	m->hwvol_step = 5;
914 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
915 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
916             OID_AUTO, "hwvol_step", CTLFLAG_RWTUN, &m->hwvol_step, 0, "");
917 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
918 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
919 	    "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE,
920 	    m, 0, sysctl_hw_snd_hwvol_mixer, "A", "");
921 	return 0;
922 }
923 
924 void
925 mixer_hwvol_mute_locked(struct snd_mixer *m)
926 {
927 	mix_setmutedevs(m, m->mutedevs ^ (1 << m->hwvol_mixer));
928 }
929 
930 void
931 mixer_hwvol_mute(device_t dev)
932 {
933 	struct snd_mixer *m;
934 	struct cdev *pdev;
935 
936 	pdev = mixer_get_devt(dev);
937 	m = pdev->si_drv1;
938 	snd_mtxlock(m->lock);
939 	mixer_hwvol_mute_locked(m);
940 	snd_mtxunlock(m->lock);
941 }
942 
943 void
944 mixer_hwvol_step_locked(struct snd_mixer *m, int left_step, int right_step)
945 {
946 	int level, left, right;
947 
948 	level = mixer_get(m, m->hwvol_mixer);
949 
950 	if (level != -1) {
951 		left = level & 0xff;
952 		right = (level >> 8) & 0xff;
953 		left += left_step * m->hwvol_step;
954 		if (left < 0)
955 			left = 0;
956 		else if (left > 100)
957 			left = 100;
958 		right += right_step * m->hwvol_step;
959 		if (right < 0)
960 			right = 0;
961 		else if (right > 100)
962 			right = 100;
963 
964 		mixer_set(m, m->hwvol_mixer, m->mutedevs, left | right << 8);
965 	}
966 }
967 
968 void
969 mixer_hwvol_step(device_t dev, int left_step, int right_step)
970 {
971 	struct snd_mixer *m;
972 	struct cdev *pdev;
973 
974 	pdev = mixer_get_devt(dev);
975 	m = pdev->si_drv1;
976 	snd_mtxlock(m->lock);
977 	mixer_hwvol_step_locked(m, left_step, right_step);
978 	snd_mtxunlock(m->lock);
979 }
980 
981 int
982 mixer_busy(struct snd_mixer *m)
983 {
984 	KASSERT(m != NULL, ("NULL snd_mixer"));
985 
986 	return (m->busy);
987 }
988 
989 int
990 mix_set(struct snd_mixer *m, u_int dev, u_int left, u_int right)
991 {
992 	int ret;
993 
994 	KASSERT(m != NULL, ("NULL snd_mixer"));
995 
996 	snd_mtxlock(m->lock);
997 	ret = mixer_set(m, dev, m->mutedevs, left | (right << 8));
998 	snd_mtxunlock(m->lock);
999 
1000 	return ((ret != 0) ? ENXIO : 0);
1001 }
1002 
1003 int
1004 mix_get(struct snd_mixer *m, u_int dev)
1005 {
1006 	int ret;
1007 
1008 	KASSERT(m != NULL, ("NULL snd_mixer"));
1009 
1010 	snd_mtxlock(m->lock);
1011 	ret = mixer_get(m, dev);
1012 	snd_mtxunlock(m->lock);
1013 
1014 	return (ret);
1015 }
1016 
1017 int
1018 mix_setrecsrc(struct snd_mixer *m, u_int32_t src)
1019 {
1020 	int ret;
1021 
1022 	KASSERT(m != NULL, ("NULL snd_mixer"));
1023 
1024 	snd_mtxlock(m->lock);
1025 	ret = mixer_setrecsrc(m, src);
1026 	snd_mtxunlock(m->lock);
1027 
1028 	return ((ret != 0) ? ENXIO : 0);
1029 }
1030 
1031 u_int32_t
1032 mix_getrecsrc(struct snd_mixer *m)
1033 {
1034 	u_int32_t ret;
1035 
1036 	KASSERT(m != NULL, ("NULL snd_mixer"));
1037 
1038 	snd_mtxlock(m->lock);
1039 	ret = mixer_getrecsrc(m);
1040 	snd_mtxunlock(m->lock);
1041 
1042 	return (ret);
1043 }
1044 
1045 int
1046 mix_get_type(struct snd_mixer *m)
1047 {
1048 	KASSERT(m != NULL, ("NULL snd_mixer"));
1049 
1050 	return (m->type);
1051 }
1052 
1053 device_t
1054 mix_get_dev(struct snd_mixer *m)
1055 {
1056 	KASSERT(m != NULL, ("NULL snd_mixer"));
1057 
1058 	return (m->dev);
1059 }
1060 
1061 /* ----------------------------------------------------------------------- */
1062 
1063 static int
1064 mixer_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
1065 {
1066 	struct snddev_info *d;
1067 	struct snd_mixer *m;
1068 
1069 	if (i_dev == NULL || i_dev->si_drv1 == NULL)
1070 		return (EBADF);
1071 
1072 	m = i_dev->si_drv1;
1073 	d = device_get_softc(m->dev);
1074 	if (!PCM_REGISTERED(d) || PCM_DETACHING(d))
1075 		return (EBADF);
1076 
1077 	/* XXX Need Giant magic entry ??? */
1078 
1079 	snd_mtxlock(m->lock);
1080 	m->busy = 1;
1081 	snd_mtxunlock(m->lock);
1082 
1083 	return (0);
1084 }
1085 
1086 static int
1087 mixer_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
1088 {
1089 	struct snddev_info *d;
1090 	struct snd_mixer *m;
1091 	int ret;
1092 
1093 	if (i_dev == NULL || i_dev->si_drv1 == NULL)
1094 		return (EBADF);
1095 
1096 	m = i_dev->si_drv1;
1097 	d = device_get_softc(m->dev);
1098 	if (!PCM_REGISTERED(d))
1099 		return (EBADF);
1100 
1101 	/* XXX Need Giant magic entry ??? */
1102 
1103 	snd_mtxlock(m->lock);
1104 	ret = (m->busy == 0) ? EBADF : 0;
1105 	m->busy = 0;
1106 	snd_mtxunlock(m->lock);
1107 
1108 	return (ret);
1109 }
1110 
1111 static int
1112 mixer_ioctl_channel(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
1113     struct thread *td, int from)
1114 {
1115 	struct snddev_info *d;
1116 	struct snd_mixer *m;
1117 	struct pcm_channel *c, *rdch, *wrch;
1118 	pid_t pid;
1119 	int j, ret;
1120 
1121 	if (td == NULL || td->td_proc == NULL)
1122 		return (-1);
1123 
1124 	m = dev->si_drv1;
1125 	d = device_get_softc(m->dev);
1126 	j = cmd & 0xff;
1127 
1128 	switch (j) {
1129 	case SOUND_MIXER_PCM:
1130 	case SOUND_MIXER_RECLEV:
1131 	case SOUND_MIXER_DEVMASK:
1132 	case SOUND_MIXER_CAPS:
1133 	case SOUND_MIXER_STEREODEVS:
1134 		break;
1135 	default:
1136 		return (-1);
1137 		break;
1138 	}
1139 
1140 	pid = td->td_proc->p_pid;
1141 	rdch = NULL;
1142 	wrch = NULL;
1143 	c = NULL;
1144 	ret = -1;
1145 
1146 	/*
1147 	 * This is unfair. Imagine single proc opening multiple
1148 	 * instances of same direction. What we do right now
1149 	 * is looking for the first matching proc/pid, and just
1150 	 * that. Nothing more. Consider it done.
1151 	 *
1152 	 * The better approach of controlling specific channel
1153 	 * pcm or rec volume is by doing mixer ioctl
1154 	 * (SNDCTL_DSP_[SET|GET][PLAY|REC]VOL / SOUND_MIXER_[PCM|RECLEV]
1155 	 * on its open fd, rather than cracky mixer bypassing here.
1156 	 */
1157 	CHN_FOREACH(c, d, channels.pcm.opened) {
1158 		CHN_LOCK(c);
1159 		if (c->pid != pid ||
1160 		    !(c->feederflags & (1 << FEEDER_VOLUME))) {
1161 			CHN_UNLOCK(c);
1162 			continue;
1163 		}
1164 		if (rdch == NULL && c->direction == PCMDIR_REC) {
1165 			rdch = c;
1166 			if (j == SOUND_MIXER_RECLEV)
1167 				goto mixer_ioctl_channel_proc;
1168 		} else if (wrch == NULL && c->direction == PCMDIR_PLAY) {
1169 			wrch = c;
1170 			if (j == SOUND_MIXER_PCM)
1171 				goto mixer_ioctl_channel_proc;
1172 		}
1173 		CHN_UNLOCK(c);
1174 		if (rdch != NULL && wrch != NULL)
1175 			break;
1176 	}
1177 
1178 	if (rdch == NULL && wrch == NULL)
1179 		return (-1);
1180 
1181 	if ((j == SOUND_MIXER_DEVMASK || j == SOUND_MIXER_CAPS ||
1182 	    j == SOUND_MIXER_STEREODEVS) &&
1183 	    (cmd & ~0xff) == MIXER_READ(0)) {
1184 		snd_mtxlock(m->lock);
1185 		*(int *)arg = mix_getdevs(m);
1186 		snd_mtxunlock(m->lock);
1187 		if (rdch != NULL)
1188 			*(int *)arg |= SOUND_MASK_RECLEV;
1189 		if (wrch != NULL)
1190 			*(int *)arg |= SOUND_MASK_PCM;
1191 		ret = 0;
1192 	}
1193 
1194 	return (ret);
1195 
1196 mixer_ioctl_channel_proc:
1197 
1198 	KASSERT(c != NULL, ("%s(): NULL channel", __func__));
1199 	CHN_LOCKASSERT(c);
1200 
1201 	if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1202 		int left, right, center;
1203 
1204 		left = *(int *)arg & 0x7f;
1205 		right = (*(int *)arg >> 8) & 0x7f;
1206 		center = (left + right) >> 1;
1207 		chn_setvolume_multi(c, SND_VOL_C_PCM, left, right, center);
1208 	} else if ((cmd & ~0xff) == MIXER_READ(0)) {
1209 		*(int *)arg = CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FL);
1210 		*(int *)arg |=
1211 		    CHN_GETVOLUME(c, SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1212 	}
1213 
1214 	CHN_UNLOCK(c);
1215 
1216 	return (0);
1217 }
1218 
1219 static int
1220 mixer_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1221     struct thread *td)
1222 {
1223 	struct snddev_info *d;
1224 	int ret;
1225 
1226 	if (i_dev == NULL || i_dev->si_drv1 == NULL)
1227 		return (EBADF);
1228 
1229 	d = device_get_softc(((struct snd_mixer *)i_dev->si_drv1)->dev);
1230 	if (!PCM_REGISTERED(d) || PCM_DETACHING(d))
1231 		return (EBADF);
1232 
1233 	PCM_GIANT_ENTER(d);
1234 	PCM_ACQUIRE_QUICK(d);
1235 
1236 	ret = -1;
1237 
1238 	if (mixer_bypass != 0 && (d->flags & SD_F_VPC))
1239 		ret = mixer_ioctl_channel(i_dev, cmd, arg, mode, td,
1240 		    MIXER_CMD_CDEV);
1241 
1242 	if (ret == -1)
1243 		ret = mixer_ioctl_cmd(i_dev, cmd, arg, mode, td,
1244 		    MIXER_CMD_CDEV);
1245 
1246 	PCM_RELEASE_QUICK(d);
1247 	PCM_GIANT_LEAVE(d);
1248 
1249 	return (ret);
1250 }
1251 
1252 static void
1253 mixer_mixerinfo(struct snd_mixer *m, mixer_info *mi)
1254 {
1255 	bzero((void *)mi, sizeof(*mi));
1256 	strlcpy(mi->id, m->name, sizeof(mi->id));
1257 	strlcpy(mi->name, device_get_desc(m->dev), sizeof(mi->name));
1258 	mi->modify_counter = m->modify_counter;
1259 }
1260 
1261 /*
1262  * XXX Make sure you can guarantee concurrency safety before calling this
1263  *     function, be it through Giant, PCM_*, etc !
1264  */
1265 int
1266 mixer_ioctl_cmd(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1267     struct thread *td, int from)
1268 {
1269 	struct snd_mixer *m;
1270 	int ret = EINVAL, *arg_i = (int *)arg;
1271 	int v = -1, j = cmd & 0xff;
1272 
1273 	/*
1274 	 * Certain ioctls may be made on any type of device (audio, mixer,
1275 	 * and MIDI).  Handle those special cases here.
1276 	 */
1277 	if (IOCGROUP(cmd) == 'X') {
1278 		switch (cmd) {
1279 		case SNDCTL_SYSINFO:
1280 			sound_oss_sysinfo((oss_sysinfo *)arg);
1281 			return (0);
1282 		case SNDCTL_CARDINFO:
1283 			return (sound_oss_card_info((oss_card_info *)arg));
1284 	    	case SNDCTL_AUDIOINFO:
1285 	    	case SNDCTL_AUDIOINFO_EX:
1286 	    	case SNDCTL_ENGINEINFO:
1287 			return (dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg));
1288 		case SNDCTL_MIXERINFO:
1289 			return (mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg));
1290 		}
1291 		return (EINVAL);
1292 	}
1293 
1294 	m = i_dev->si_drv1;
1295 
1296 	if (m == NULL)
1297 		return (EBADF);
1298 
1299 	snd_mtxlock(m->lock);
1300 	if (from == MIXER_CMD_CDEV && !m->busy) {
1301 		snd_mtxunlock(m->lock);
1302 		return (EBADF);
1303 	}
1304 	switch (cmd) {
1305 	case SNDCTL_DSP_GET_RECSRC_NAMES:
1306 		bcopy((void *)&m->enuminfo, arg, sizeof(oss_mixer_enuminfo));
1307 		ret = 0;
1308 		goto done;
1309 	case SNDCTL_DSP_GET_RECSRC:
1310 		ret = mixer_get_recroute(m, arg_i);
1311 		goto done;
1312 	case SNDCTL_DSP_SET_RECSRC:
1313 		ret = mixer_set_recroute(m, *arg_i);
1314 		goto done;
1315 	case OSS_GETVERSION:
1316 		*arg_i = SOUND_VERSION;
1317 		ret = 0;
1318 		goto done;
1319 	case SOUND_MIXER_INFO:
1320 		mixer_mixerinfo(m, (mixer_info *)arg);
1321 		ret = 0;
1322 		goto done;
1323 	}
1324 	if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1325 		switch (j) {
1326 		case SOUND_MIXER_RECSRC:
1327 			ret = mixer_setrecsrc(m, *arg_i);
1328 			break;
1329 		case SOUND_MIXER_MUTE:
1330 			mix_setmutedevs(m, *arg_i);
1331 			ret = 0;
1332 			break;
1333 		default:
1334 			ret = mixer_set(m, j, m->mutedevs, *arg_i);
1335 			break;
1336 		}
1337 		snd_mtxunlock(m->lock);
1338 		return ((ret == 0) ? 0 : ENXIO);
1339 	}
1340 	if ((cmd & ~0xff) == MIXER_READ(0)) {
1341 		switch (j) {
1342 		case SOUND_MIXER_DEVMASK:
1343 		case SOUND_MIXER_CAPS:
1344 		case SOUND_MIXER_STEREODEVS:
1345 			v = mix_getdevs(m);
1346 			break;
1347 		case SOUND_MIXER_MUTE:
1348 			v = mix_getmutedevs(m);
1349 			break;
1350 		case SOUND_MIXER_RECMASK:
1351 			v = mix_getrecdevs(m);
1352 			break;
1353 		case SOUND_MIXER_RECSRC:
1354 			v = mixer_getrecsrc(m);
1355 			break;
1356 		default:
1357 			v = mixer_get(m, j);
1358 			break;
1359 		}
1360 		*arg_i = v;
1361 		snd_mtxunlock(m->lock);
1362 		return ((v != -1) ? 0 : ENXIO);
1363 	}
1364 done:
1365 	snd_mtxunlock(m->lock);
1366 	return (ret);
1367 }
1368 
1369 static void
1370 mixer_clone(void *arg,
1371     struct ucred *cred,
1372     char *name, int namelen, struct cdev **dev)
1373 {
1374 	struct snddev_info *d;
1375 
1376 	if (*dev != NULL)
1377 		return;
1378 	if (strcmp(name, "mixer") == 0) {
1379 		bus_topo_lock();
1380 		d = devclass_get_softc(pcm_devclass, snd_unit);
1381 		/* See related comment in dsp_clone(). */
1382 		if (d != NULL && PCM_REGISTERED(d) && d->mixer_dev != NULL) {
1383 			*dev = d->mixer_dev;
1384 			dev_ref(*dev);
1385 		}
1386 		bus_topo_unlock();
1387 	}
1388 }
1389 
1390 static void
1391 mixer_sysinit(void *p)
1392 {
1393 	if (mixer_ehtag != NULL)
1394 		return;
1395 	mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
1396 }
1397 
1398 static void
1399 mixer_sysuninit(void *p)
1400 {
1401 	if (mixer_ehtag == NULL)
1402 		return;
1403 	EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
1404 	mixer_ehtag = NULL;
1405 }
1406 
1407 SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
1408 SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
1409 
1410 /**
1411  * @brief Handler for SNDCTL_MIXERINFO
1412  *
1413  * This function searches for a mixer based on the numeric ID stored
1414  * in oss_miserinfo::dev.  If set to -1, then information about the
1415  * current mixer handling the request is provided.  Note, however, that
1416  * this ioctl may be made with any sound device (audio, mixer, midi).
1417  *
1418  * @note Caller must not hold any PCM device, channel, or mixer locks.
1419  *
1420  * See http://manuals.opensound.com/developer/SNDCTL_MIXERINFO.html for
1421  * more information.
1422  *
1423  * @param i_dev	character device on which the ioctl arrived
1424  * @param arg	user argument (oss_mixerinfo *)
1425  *
1426  * @retval EINVAL	oss_mixerinfo::dev specified a bad value
1427  * @retval 0		success
1428  */
1429 int
1430 mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi)
1431 {
1432 	struct snddev_info *d;
1433 	struct snd_mixer *m;
1434 	int nmix, i;
1435 
1436 	/*
1437 	 * If probing the device handling the ioctl, make sure it's a mixer
1438 	 * device.  (This ioctl is valid on audio, mixer, and midi devices.)
1439 	 */
1440 	if (mi->dev == -1 && i_dev->si_devsw != &mixer_cdevsw)
1441 		return (EINVAL);
1442 
1443 	d = NULL;
1444 	m = NULL;
1445 	nmix = 0;
1446 
1447 	/*
1448 	 * There's a 1:1 relationship between mixers and PCM devices, so
1449 	 * begin by iterating over PCM devices and search for our mixer.
1450 	 */
1451 	for (i = 0; pcm_devclass != NULL &&
1452 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1453 		d = devclass_get_softc(pcm_devclass, i);
1454 		if (!PCM_REGISTERED(d) || PCM_DETACHING(d))
1455 			continue;
1456 
1457 		/* XXX Need Giant magic entry */
1458 
1459 		/* See the note in function docblock. */
1460 		PCM_UNLOCKASSERT(d);
1461 		PCM_LOCK(d);
1462 
1463 		if (d->mixer_dev != NULL && d->mixer_dev->si_drv1 != NULL &&
1464 		    ((mi->dev == -1 && d->mixer_dev == i_dev) ||
1465 		    mi->dev == nmix)) {
1466 			m = d->mixer_dev->si_drv1;
1467 			mtx_lock(m->lock);
1468 
1469 			/*
1470 			 * At this point, the following synchronization stuff
1471 			 * has happened:
1472 			 * - a specific PCM device is locked.
1473 			 * - a specific mixer device has been locked, so be
1474 			 *   sure to unlock when existing.
1475 			 */
1476 			bzero((void *)mi, sizeof(*mi));
1477 			mi->dev = nmix;
1478 			snprintf(mi->id, sizeof(mi->id), "mixer%d", i);
1479 			strlcpy(mi->name, m->name, sizeof(mi->name));
1480 			mi->modify_counter = m->modify_counter;
1481 			mi->card_number = i;
1482 			/*
1483 			 * Currently, FreeBSD assumes 1:1 relationship between
1484 			 * a pcm and mixer devices, so this is hardcoded to 0.
1485 			 */
1486 			mi->port_number = 0;
1487 
1488 			/**
1489 			 * @todo Fill in @sa oss_mixerinfo::mixerhandle.
1490 			 * @note From 4Front:  "mixerhandle is an arbitrary
1491 			 *       string that identifies the mixer better than
1492 			 *       the device number (mixerinfo.dev).  Device
1493 			 *       numbers may change depending on the order the
1494 			 *       drivers are loaded. However the handle should
1495 			 *       remain the same provided that the sound card
1496 			 *       is not moved to another PCI slot."
1497 			 */
1498 
1499 			/**
1500 			 * @note
1501 			 * @sa oss_mixerinfo::magic is a reserved field.
1502 			 *
1503 			 * @par
1504 			 * From 4Front:  "magic is usually 0. However some
1505 			 * devices may have dedicated setup utilities and the
1506 			 * magic field may contain an unique driver specific
1507 			 * value (managed by [4Front])."
1508 			 */
1509 
1510 			mi->enabled = device_is_attached(m->dev) ? 1 : 0;
1511 			/**
1512 			 * The only flag for @sa oss_mixerinfo::caps is
1513 			 * currently MIXER_CAP_VIRTUAL, which I'm not sure we
1514 			 * really worry about.
1515 			 */
1516 			/**
1517 			 * Mixer extensions currently aren't supported, so
1518 			 * leave @sa oss_mixerinfo::nrext blank for now.
1519 			 */
1520 			/**
1521 			 * @todo Fill in @sa oss_mixerinfo::priority (requires
1522 			 *       touching drivers?)
1523 			 * @note The priority field is for mixer applets to
1524 			 * determine which mixer should be the default, with 0
1525 			 * being least preferred and 10 being most preferred.
1526 			 * From 4Front:  "OSS drivers like ICH use higher
1527 			 * values (10) because such chips are known to be used
1528 			 * only on motherboards.  Drivers for high end pro
1529 			 * devices use 0 because they will never be the
1530 			 * default mixer. Other devices use values 1 to 9
1531 			 * depending on the estimated probability of being the
1532 			 * default device.
1533 			 *
1534 			 * XXX Described by Hannu@4Front, but not found in
1535 			 *     soundcard.h.
1536 			strlcpy(mi->devnode, devtoname(d->mixer_dev),
1537 			sizeof(mi->devnode));
1538 			mi->legacy_device = i;
1539 			 */
1540 			mtx_unlock(m->lock);
1541 		} else
1542 			++nmix;
1543 
1544 		PCM_UNLOCK(d);
1545 
1546 		if (m != NULL)
1547 			return (0);
1548 	}
1549 
1550 	return (EINVAL);
1551 }
1552 
1553 /*
1554  * Allow the sound driver to use the mixer lock to protect its mixer
1555  * data:
1556  */
1557 struct mtx *
1558 mixer_get_lock(struct snd_mixer *m)
1559 {
1560 	if (m->lock == NULL) {
1561 		return (&Giant);
1562 	}
1563 	return (m->lock);
1564 }
1565 
1566 int
1567 mix_get_locked(struct snd_mixer *m, u_int dev, int *pleft, int *pright)
1568 {
1569 	int level;
1570 
1571 	level = mixer_get(m, dev);
1572 	if (level < 0) {
1573 		*pright = *pleft = -1;
1574 		return (-1);
1575 	}
1576 
1577 	*pleft = level & 0xFF;
1578 	*pright = (level >> 8) & 0xFF;
1579 
1580 	return (0);
1581 }
1582 
1583 int
1584 mix_set_locked(struct snd_mixer *m, u_int dev, int left, int right)
1585 {
1586 	int level;
1587 
1588 	level = (left & 0xFF) | ((right & 0xFF) << 8);
1589 
1590 	return (mixer_set(m, dev, m->mutedevs, level));
1591 }
1592