DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / dev / sound / pcm / mixer.c
CommitLineData
558a398b
SS
1/*-
2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
984263bc
MD
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.
1de703da 25 *
4886ec58 26 * $FreeBSD: src/sys/dev/sound/pcm/mixer.c,v 1.43.2.5 2007/05/13 20:53:39 ariff Exp $
4138091c 27 * $DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.18 2007/06/22 21:41:16 corecode Exp $
984263bc
MD
28 */
29
30#include <dev/sound/pcm/sound.h>
34e3e97f 31#include <dev/sound/pcm/dsp.h>
984263bc
MD
32
33#include "mixer_if.h"
34
4138091c 35SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.18 2007/06/22 21:41:16 corecode Exp $");
984263bc
MD
36
37MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
38
39#define MIXER_NAMELEN 16
40struct snd_mixer {
41 KOBJ_FIELDS;
42 const char *type;
43 void *devinfo;
44 int busy;
45 int hwvol_muted;
46 int hwvol_mixer;
47 int hwvol_step;
558a398b 48 device_t dev;
984263bc
MD
49 u_int32_t hwvol_mute_level;
50 u_int32_t devs;
51 u_int32_t recdevs;
52 u_int32_t recsrc;
53 u_int16_t level[32];
4886ec58
HT
54 u_int8_t parent[32];
55 u_int32_t child[32];
56 u_int8_t realdev[32];
984263bc 57 char name[MIXER_NAMELEN];
cad195a6 58 sndlock_t lock;
984263bc
MD
59};
60
61static u_int16_t snd_mixerdefaults[SOUND_MIXER_NRDEVICES] = {
62 [SOUND_MIXER_VOLUME] = 75,
63 [SOUND_MIXER_BASS] = 50,
64 [SOUND_MIXER_TREBLE] = 50,
65 [SOUND_MIXER_SYNTH] = 75,
66 [SOUND_MIXER_PCM] = 75,
67 [SOUND_MIXER_SPEAKER] = 75,
68 [SOUND_MIXER_LINE] = 75,
69 [SOUND_MIXER_MIC] = 0,
70 [SOUND_MIXER_CD] = 75,
558a398b 71 [SOUND_MIXER_IGAIN] = 0,
984263bc
MD
72 [SOUND_MIXER_LINE1] = 75,
73 [SOUND_MIXER_VIDEO] = 75,
74 [SOUND_MIXER_RECLEV] = 0,
75 [SOUND_MIXER_OGAIN] = 50,
76 [SOUND_MIXER_MONITOR] = 75,
77};
78
79static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
80
81static d_open_t mixer_open;
82static d_close_t mixer_close;
83
558a398b
SS
84static struct dev_ops mixer_cdevsw = {
85 { "mixer", SND_CDEV_MAJOR, D_TRACKCLOSE },
86 /* .d_flags = D_TRACKCLOSE | D_NEEDGIANT, */
fef8985e
MD
87 .d_open = mixer_open,
88 .d_close = mixer_close,
89 .d_ioctl = mixer_ioctl,
984263bc
MD
90};
91
92#ifdef USING_DEVFS
93static eventhandler_tag mixer_ehtag;
94#endif
95
558a398b 96static struct cdev *
984263bc
MD
97mixer_get_devt(device_t dev)
98{
558a398b 99 struct snddev_info *snddev;
984263bc 100
558a398b 101 snddev = device_get_softc(dev);
984263bc 102
558a398b 103 return snddev->mixer_dev;
984263bc
MD
104}
105
106#ifdef SND_DYNSYSCTL
107static int
108mixer_lookup(char *devname)
109{
110 int i;
111
112 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
113 if (strncmp(devname, snd_mixernames[i],
114 strlen(snd_mixernames[i])) == 0)
115 return i;
116 return -1;
117}
118#endif
119
558a398b
SS
120/*
121 * Always called with mixer->lock held.
122 */
984263bc 123static int
4886ec58
HT
124mixer_set_softpcmvol(struct snd_mixer *mixer, struct snddev_info *d,
125 unsigned left, unsigned right)
126{
127 struct snddev_channel *sce;
128 struct pcm_channel *ch;
129
130 snd_mtxunlock(mixer->lock);
131 SLIST_FOREACH(sce, &d->channels, link) {
132 ch = sce->channel;
133 CHN_LOCK(ch);
134 if (ch->direction == PCMDIR_PLAY &&
135 (ch->feederflags & (1 << FEEDER_VOLUME)))
136 chn_setvolume(ch, left, right);
137 CHN_UNLOCK(ch);
138 }
139 snd_mtxlock(mixer->lock);
140 return 0;
141}
142
143static int
144mixer_set(struct snd_mixer *m, unsigned dev, unsigned lev)
984263bc 145{
558a398b 146 struct snddev_info *d;
4886ec58
HT
147 unsigned l, r, tl, tr;
148 u_int32_t parent = SOUND_MIXER_NONE, child = 0;
149 u_int32_t realdev;
150 int i;
984263bc 151
4886ec58
HT
152 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
153 (0 == (m->devs & (1 << dev))))
984263bc
MD
154 return -1;
155
156 l = min((lev & 0x00ff), 100);
157 r = min(((lev & 0xff00) >> 8), 100);
4886ec58
HT
158 realdev = m->realdev[dev];
159
160 d = device_get_softc(m->dev);
161 if (d == NULL)
162 return -1;
984263bc 163
4886ec58
HT
164 /* TODO: recursive handling */
165 parent = m->parent[dev];
166 if (parent >= SOUND_MIXER_NRDEVICES)
167 parent = SOUND_MIXER_NONE;
168 if (parent == SOUND_MIXER_NONE)
169 child = m->child[dev];
170
171 if (parent != SOUND_MIXER_NONE) {
172 tl = (l * (m->level[parent] & 0x00ff)) / 100;
173 tr = (r * ((m->level[parent] & 0xff00) >> 8)) / 100;
174 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
175 mixer_set_softpcmvol(m, d, tl, tr);
176 else if (realdev != SOUND_MIXER_NONE &&
177 MIXER_SET(m, realdev, tl, tr) < 0)
178 return -1;
179 } else if (child != 0) {
180 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
181 if (!(child & (1 << i)) || m->parent[i] != dev)
182 continue;
183 realdev = m->realdev[i];
184 tl = (l * (m->level[i] & 0x00ff)) / 100;
185 tr = (r * ((m->level[i] & 0xff00) >> 8)) / 100;
186 if (i == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
187 mixer_set_softpcmvol(m, d, tl, tr);
188 else if (realdev != SOUND_MIXER_NONE)
189 MIXER_SET(m, realdev, tl, tr);
558a398b 190 }
4886ec58
HT
191 realdev = m->realdev[dev];
192 if (realdev != SOUND_MIXER_NONE &&
193 MIXER_SET(m, realdev, l, r) < 0)
194 return -1;
558a398b 195 } else {
4886ec58
HT
196 if (dev == SOUND_MIXER_PCM && (d->flags & SD_F_SOFTPCMVOL))
197 mixer_set_softpcmvol(m, d, l, r);
198 else if (realdev != SOUND_MIXER_NONE &&
199 MIXER_SET(m, realdev, l, r) < 0)
558a398b
SS
200 return -1;
201 }
984263bc 202
4886ec58
HT
203 m->level[dev] = l | (r << 8);
204
984263bc
MD
205 return 0;
206}
207
208static int
209mixer_get(struct snd_mixer *mixer, int dev)
210{
211 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
212 return mixer->level[dev];
213 else return -1;
214}
215
216static int
217mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
218{
219 src &= mixer->recdevs;
220 if (src == 0)
221 src = SOUND_MASK_MIC;
222 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
223 return 0;
224}
225
226static int
227mixer_getrecsrc(struct snd_mixer *mixer)
228{
229 return mixer->recsrc;
230}
231
232void
233mix_setdevs(struct snd_mixer *m, u_int32_t v)
234{
4886ec58
HT
235 struct snddev_info *d;
236 int i;
237
238 if (m == NULL)
239 return;
240
241 d = device_get_softc(m->dev);
242 if (d != NULL && (d->flags & SD_F_SOFTPCMVOL))
558a398b 243 v |= SOUND_MASK_PCM;
4886ec58
HT
244 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
245 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
246 v |= 1 << m->parent[i];
247 v |= m->child[i];
248 }
984263bc
MD
249 m->devs = v;
250}
251
252void
253mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
254{
255 m->recdevs = v;
256}
257
4886ec58
HT
258void
259mix_setparentchild(struct snd_mixer *m, u_int32_t parent, u_int32_t childs)
260{
261 u_int32_t mask = 0;
262 int i;
263
264 if (m == NULL || parent >= SOUND_MIXER_NRDEVICES)
265 return;
266 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
267 if (i == parent)
268 continue;
269 if (childs & (1 << i)) {
270 mask |= 1 << i;
271 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
272 m->child[m->parent[i]] &= ~(1 << i);
273 m->parent[i] = parent;
274 m->child[i] = 0;
275 }
276 }
277 mask &= ~(1 << parent);
278 m->child[parent] = mask;
279}
280
281void
282mix_setrealdev(struct snd_mixer *m, u_int32_t dev, u_int32_t realdev)
283{
284 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES ||
285 !(realdev == SOUND_MIXER_NONE || realdev < SOUND_MIXER_NRDEVICES))
286 return;
287 m->realdev[dev] = realdev;
288}
289
290u_int32_t
291mix_getparent(struct snd_mixer *m, u_int32_t dev)
292{
293 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
294 return SOUND_MIXER_NONE;
295 return m->parent[dev];
296}
297
298u_int32_t
299mix_getchild(struct snd_mixer *m, u_int32_t dev)
300{
301 if (m == NULL || dev >= SOUND_MIXER_NRDEVICES)
302 return 0;
303 return m->child[dev];
304}
305
984263bc
MD
306u_int32_t
307mix_getdevs(struct snd_mixer *m)
308{
309 return m->devs;
310}
311
312u_int32_t
313mix_getrecdevs(struct snd_mixer *m)
314{
315 return m->recdevs;
316}
317
318void *
319mix_getdevinfo(struct snd_mixer *m)
320{
321 return m->devinfo;
322}
323
324int
325mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
326{
558a398b 327 struct snddev_info *snddev;
984263bc
MD
328 struct snd_mixer *m;
329 u_int16_t v;
558a398b
SS
330 struct cdev *pdev;
331 int i, unit, val;
984263bc
MD
332
333 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
f8c7a42d 334 ksnprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
984263bc
MD
335 m->lock = snd_mtxcreate(m->name, "pcm mixer");
336 m->type = cls->name;
337 m->devinfo = devinfo;
338 m->busy = 0;
558a398b 339 m->dev = dev;
4886ec58
HT
340 for (i = 0; i < 32; i++) {
341 m->parent[i] = SOUND_MIXER_NONE;
342 m->child[i] = 0;
343 m->realdev[i] = i;
344 }
984263bc
MD
345
346 if (MIXER_INIT(m))
347 goto bad;
348
349 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
350 v = snd_mixerdefaults[i];
558a398b
SS
351
352 if (resource_int_value(device_get_name(dev),
353 device_get_unit(dev), snd_mixernames[i], &val) == 0) {
354 if (val >= 0 && val <= 100) {
355 v = (u_int16_t) val;
356 }
357 }
358
359 snd_mtxlock(m->lock);
984263bc 360 mixer_set(m, i, v | (v << 8));
558a398b 361 snd_mtxunlock(m->lock);
984263bc
MD
362 }
363
364 mixer_setrecsrc(m, SOUND_MASK_MIC);
365
366 unit = device_get_unit(dev);
558a398b 367 pdev = make_dev(&mixer_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
3e82b46c 368 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
558a398b 369 reference_dev(pdev);
984263bc 370 pdev->si_drv1 = m;
558a398b
SS
371 snddev = device_get_softc(dev);
372 snddev->mixer_dev = pdev;
984263bc 373
4886ec58
HT
374 if (bootverbose) {
375 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
376 if (!(m->devs & (1 << i)))
377 continue;
378 if (m->realdev[i] != i) {
379 device_printf(dev, "Mixer \"%s\" -> \"%s\":",
380 snd_mixernames[i],
381 (m->realdev[i] < SOUND_MIXER_NRDEVICES) ?
382 snd_mixernames[m->realdev[i]] : "none");
383 } else {
384 device_printf(dev, "Mixer \"%s\":",
385 snd_mixernames[i]);
386 }
387 if (m->parent[i] < SOUND_MIXER_NRDEVICES)
388 kprintf(" parent=\"%s\"",
389 snd_mixernames[m->parent[i]]);
390 if (m->child[i] != 0)
391 kprintf(" child=0x%08x", m->child[i]);
392 kprintf("\n");
393 }
394 if (snddev->flags & SD_F_SOFTPCMVOL)
395 device_printf(dev, "Soft PCM mixer ENABLED\n");
396 }
397
984263bc
MD
398 return 0;
399
400bad:
984263bc
MD
401 snd_mtxfree(m->lock);
402 kobj_delete((kobj_t)m, M_MIXER);
403 return -1;
404}
405
406int
407mixer_uninit(device_t dev)
408{
409 int i;
77934f9e 410 int unit;
558a398b 411 struct snddev_info *d;
984263bc 412 struct snd_mixer *m;
558a398b 413 struct cdev *pdev;
984263bc 414
558a398b 415 d = device_get_softc(dev);
984263bc 416 pdev = mixer_get_devt(dev);
558a398b
SS
417 if (d == NULL || pdev == NULL || pdev->si_drv1 == NULL)
418 return EBADF;
984263bc
MD
419 m = pdev->si_drv1;
420 snd_mtxlock(m->lock);
421
422 if (m->busy) {
423 snd_mtxunlock(m->lock);
424 return EBUSY;
425 }
426
427 pdev->si_drv1 = NULL;
558a398b 428 release_dev(pdev);
77934f9e 429 unit = device_get_unit(dev);
cd29885a 430 dev_ops_remove_minor(&mixer_cdevsw, /*-1, */PCMMKMINOR(unit, SND_DEV_CTL, 0));
984263bc
MD
431
432 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
433 mixer_set(m, i, 0);
434
435 mixer_setrecsrc(m, SOUND_MASK_MIC);
436
437 MIXER_UNINIT(m);
438
558a398b 439 snd_mtxunlock(m->lock);
984263bc
MD
440 snd_mtxfree(m->lock);
441 kobj_delete((kobj_t)m, M_MIXER);
442
558a398b
SS
443 d->mixer_dev = NULL;
444
984263bc
MD
445 return 0;
446}
447
448int
449mixer_reinit(device_t dev)
450{
451 struct snd_mixer *m;
558a398b 452 struct cdev *pdev;
984263bc
MD
453 int i;
454
455 pdev = mixer_get_devt(dev);
456 m = pdev->si_drv1;
457 snd_mtxlock(m->lock);
458
459 i = MIXER_REINIT(m);
460 if (i) {
461 snd_mtxunlock(m->lock);
462 return i;
463 }
464
465 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
466 mixer_set(m, i, m->level[i]);
467
468 mixer_setrecsrc(m, m->recsrc);
469 snd_mtxunlock(m->lock);
470
471 return 0;
472}
473
474#ifdef SND_DYNSYSCTL
475static int
476sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
477{
478 char devname[32];
479 int error, dev;
480 struct snd_mixer *m;
481
482 m = oidp->oid_arg1;
483 snd_mtxlock(m->lock);
484 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
558a398b 485 snd_mtxunlock(m->lock);
984263bc 486 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
558a398b 487 snd_mtxlock(m->lock);
984263bc
MD
488 if (error == 0 && req->newptr != NULL) {
489 dev = mixer_lookup(devname);
490 if (dev == -1) {
491 snd_mtxunlock(m->lock);
492 return EINVAL;
493 }
494 else if (dev != m->hwvol_mixer) {
495 m->hwvol_mixer = dev;
496 m->hwvol_muted = 0;
497 }
498 }
499 snd_mtxunlock(m->lock);
500 return error;
501}
502#endif
503
504int
505mixer_hwvol_init(device_t dev)
506{
507 struct snd_mixer *m;
558a398b 508 struct cdev *pdev;
984263bc
MD
509
510 pdev = mixer_get_devt(dev);
511 m = pdev->si_drv1;
984263bc
MD
512
513 m->hwvol_mixer = SOUND_MIXER_VOLUME;
514 m->hwvol_step = 5;
515#ifdef SND_DYNSYSCTL
516 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
517 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
518 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
519 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
558a398b 520 sysctl_hw_snd_hwvol_mixer, "A", "");
984263bc 521#endif
984263bc
MD
522 return 0;
523}
524
525void
526mixer_hwvol_mute(device_t dev)
527{
528 struct snd_mixer *m;
558a398b 529 struct cdev *pdev;
984263bc
MD
530
531 pdev = mixer_get_devt(dev);
532 m = pdev->si_drv1;
533 snd_mtxlock(m->lock);
534 if (m->hwvol_muted) {
535 m->hwvol_muted = 0;
536 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
537 } else {
538 m->hwvol_muted++;
539 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
540 mixer_set(m, m->hwvol_mixer, 0);
541 }
542 snd_mtxunlock(m->lock);
543}
544
545void
546mixer_hwvol_step(device_t dev, int left_step, int right_step)
547{
548 struct snd_mixer *m;
549 int level, left, right;
558a398b 550 struct cdev *pdev;
984263bc
MD
551
552 pdev = mixer_get_devt(dev);
553 m = pdev->si_drv1;
554 snd_mtxlock(m->lock);
555 if (m->hwvol_muted) {
556 m->hwvol_muted = 0;
557 level = m->hwvol_mute_level;
558 } else
559 level = mixer_get(m, m->hwvol_mixer);
560 if (level != -1) {
561 left = level & 0xff;
562 right = level >> 8;
563 left += left_step * m->hwvol_step;
564 if (left < 0)
565 left = 0;
566 right += right_step * m->hwvol_step;
567 if (right < 0)
568 right = 0;
569 mixer_set(m, m->hwvol_mixer, left | right << 8);
570 }
571 snd_mtxunlock(m->lock);
572}
573
574/* ----------------------------------------------------------------------- */
575
576static int
34e3e97f
SS
577vchanvolume(cdev_t i_dev, int write, int *volume, int *ret,
578 struct thread *td)
579{
580 struct snddev_info *d;
581 void *cookie;
582 struct pcm_channel *ch;
583 int vol_left, vol_right;
584
585 crit_enter();
586 d = dsp_get_info(i_dev);
587 if (d == NULL) {
588 crit_exit();
589 return 0;
590 }
591 /*
592 * We search for a vchan which is owned by the current process.
593 */
594 for (cookie = NULL; (ch = pcm_chn_iterate(d, &cookie)) != NULL;)
595 if (ch->flags & CHN_F_VIRTUAL &&
596 ch->pid == td->td_proc->p_pid)
597 break;
598
599 if (ch == NULL) {
600 crit_exit();
601 return 0;
602 }
603
604 if (write) {
605 vol_left = min(*volume & 0x00ff, 100);
606 vol_right = min((*volume & 0xff00) >> 8, 100);
607 *ret = chn_setvolume(ch, vol_left, vol_right);
608 } else {
609 *volume = ch->volume;
610 *ret = 0;
611 }
612 crit_exit();
613 return 1;
614}
615
616/* ----------------------------------------------------------------------- */
617
618static int
fef8985e 619mixer_open(struct dev_open_args *ap)
984263bc 620{
558a398b 621 struct cdev *i_dev = ap->a_head.a_dev;
984263bc 622 struct snd_mixer *m;
984263bc 623
558a398b 624 m = i_dev->si_drv1;
984263bc
MD
625 snd_mtxlock(m->lock);
626
627 m->busy++;
628
629 snd_mtxunlock(m->lock);
984263bc
MD
630 return 0;
631}
632
633static int
fef8985e 634mixer_close(struct dev_close_args *ap)
984263bc 635{
558a398b 636 struct cdev *i_dev = ap->a_head.a_dev;
984263bc 637 struct snd_mixer *m;
984263bc 638
558a398b 639 m = i_dev->si_drv1;
984263bc
MD
640 snd_mtxlock(m->lock);
641
642 if (!m->busy) {
643 snd_mtxunlock(m->lock);
984263bc
MD
644 return EBADF;
645 }
646 m->busy--;
647
648 snd_mtxunlock(m->lock);
984263bc
MD
649 return 0;
650}
651
652int
fef8985e 653mixer_ioctl(struct dev_ioctl_args *ap)
984263bc 654{
558a398b
SS
655 struct cdev *i_dev = ap->a_head.a_dev;
656 u_long cmd = ap->a_cmd;
fef8985e 657 caddr_t arg = ap->a_data;
558a398b 658 int mode = ap->a_fflag;
984263bc 659 struct snd_mixer *m;
984263bc 660 int ret, *arg_i = (int *)arg;
558a398b
SS
661 int v = -1, j = cmd & 0xff;
662
663 m = i_dev->si_drv1;
984263bc 664
558a398b 665 if (m == NULL)
984263bc
MD
666 return EBADF;
667
34e3e97f
SS
668 /*
669 * If we are handling PCM, maybe the app really wants to
670 * set its vchan, and fails to use the correct fd.
671 */
672 if (j == SOUND_MIXER_PCM) {
34e3e97f
SS
673 if (vchanvolume(i_dev,
674 (cmd & MIXER_WRITE(0)) == MIXER_WRITE(0),
675 (int *)arg, &ret, curthread))
676 return ret;
677 /* else proceed as usual */
678 }
679
984263bc 680 snd_mtxlock(m->lock);
558a398b
SS
681 if (mode != -1 && !m->busy) {
682 snd_mtxunlock(m->lock);
683 return EBADF;
684 }
685
686 if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
984263bc
MD
687 if (j == SOUND_MIXER_RECSRC)
688 ret = mixer_setrecsrc(m, *arg_i);
689 else
690 ret = mixer_set(m, j, *arg_i);
691 snd_mtxunlock(m->lock);
984263bc
MD
692 return (ret == 0)? 0 : ENXIO;
693 }
694
558a398b 695 if ((cmd & MIXER_READ(0)) == MIXER_READ(0)) {
984263bc
MD
696 switch (j) {
697 case SOUND_MIXER_DEVMASK:
698 case SOUND_MIXER_CAPS:
699 case SOUND_MIXER_STEREODEVS:
700 v = mix_getdevs(m);
701 break;
702
703 case SOUND_MIXER_RECMASK:
704 v = mix_getrecdevs(m);
705 break;
706
707 case SOUND_MIXER_RECSRC:
708 v = mixer_getrecsrc(m);
709 break;
710
711 default:
712 v = mixer_get(m, j);
713 }
714 *arg_i = v;
715 snd_mtxunlock(m->lock);
716 return (v != -1)? 0 : ENXIO;
717 }
718 snd_mtxunlock(m->lock);
984263bc
MD
719 return ENXIO;
720}
721
722#ifdef USING_DEVFS
723static void
558a398b
SS
724mixer_clone(void *arg, struct ucred *cred, char *name, int namelen,
725 struct cdev **dev)
984263bc 726{
558a398b 727 struct snddev_info *sd;
984263bc 728
558a398b 729 if (*dev != NULL)
984263bc
MD
730 return;
731 if (strcmp(name, "mixer") == 0) {
558a398b
SS
732 sd = devclass_get_softc(pcm_devclass, snd_unit);
733 if (sd != NULL && sd->mixer_dev != NULL) {
734 *dev = sd->mixer_dev;
735 dev_ref(*dev);
736 }
984263bc
MD
737 }
738}
739
740static void
741mixer_sysinit(void *p)
742{
743 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
744}
745
746static void
747mixer_sysuninit(void *p)
748{
749 if (mixer_ehtag != NULL)
750 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
751}
752
753SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
754SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
755#endif
756
757