Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / dev / sound / pcm / mixer.c
CommitLineData
984263bc
MD
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.
1de703da
MD
25 *
26 * $FreeBSD: src/sys/dev/sound/pcm/mixer.c,v 1.4.2.8 2002/04/22 15:49:36 cg Exp $
b13267a5 27 * $DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.12 2006/09/10 01:26:37 dillon Exp $
984263bc
MD
28 */
29
30#include <dev/sound/pcm/sound.h>
31
32#include "mixer_if.h"
33
b13267a5 34SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/mixer.c,v 1.12 2006/09/10 01:26:37 dillon Exp $");
984263bc
MD
35
36MALLOC_DEFINE(M_MIXER, "mixer", "mixer");
37
38#define MIXER_NAMELEN 16
39struct 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
56static 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
73static char* snd_mixernames[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
74
75static d_open_t mixer_open;
76static d_close_t mixer_close;
77
fef8985e
MD
78static struct dev_ops mixer_ops = {
79 { "mixer", SND_CDEV_MAJOR, 0 },
80 .d_open = mixer_open,
81 .d_close = mixer_close,
82 .d_ioctl = mixer_ioctl,
984263bc
MD
83};
84
85#ifdef USING_DEVFS
86static eventhandler_tag mixer_ehtag;
87#endif
88
b13267a5 89static cdev_t
984263bc
MD
90mixer_get_devt(device_t dev)
91{
b13267a5 92 cdev_t pdev;
984263bc
MD
93 int unit;
94
95 unit = device_get_unit(dev);
fef8985e 96 pdev = make_adhoc_dev(&mixer_ops, PCMMKMINOR(unit, SND_DEV_CTL, 0));
984263bc
MD
97
98 return pdev;
99}
100
101#ifdef SND_DYNSYSCTL
102static int
103mixer_lookup(char *devname)
104{
105 int i;
106
107 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
108 if (strncmp(devname, snd_mixernames[i],
109 strlen(snd_mixernames[i])) == 0)
110 return i;
111 return -1;
112}
113#endif
114
115static int
116mixer_set(struct snd_mixer *mixer, unsigned dev, unsigned lev)
117{
118 unsigned l, r;
119 int v;
120
121 if ((dev >= SOUND_MIXER_NRDEVICES) || (0 == (mixer->devs & (1 << dev))))
122 return -1;
123
124 l = min((lev & 0x00ff), 100);
125 r = min(((lev & 0xff00) >> 8), 100);
126
127 v = MIXER_SET(mixer, dev, l, r);
128 if (v < 0)
129 return -1;
130
131 mixer->level[dev] = l | (r << 8);
132 return 0;
133}
134
135static int
136mixer_get(struct snd_mixer *mixer, int dev)
137{
138 if ((dev < SOUND_MIXER_NRDEVICES) && (mixer->devs & (1 << dev)))
139 return mixer->level[dev];
140 else return -1;
141}
142
143static int
144mixer_setrecsrc(struct snd_mixer *mixer, u_int32_t src)
145{
146 src &= mixer->recdevs;
147 if (src == 0)
148 src = SOUND_MASK_MIC;
149 mixer->recsrc = MIXER_SETRECSRC(mixer, src);
150 return 0;
151}
152
153static int
154mixer_getrecsrc(struct snd_mixer *mixer)
155{
156 return mixer->recsrc;
157}
158
159void
160mix_setdevs(struct snd_mixer *m, u_int32_t v)
161{
162 m->devs = v;
163}
164
165void
166mix_setrecdevs(struct snd_mixer *m, u_int32_t v)
167{
168 m->recdevs = v;
169}
170
171u_int32_t
172mix_getdevs(struct snd_mixer *m)
173{
174 return m->devs;
175}
176
177u_int32_t
178mix_getrecdevs(struct snd_mixer *m)
179{
180 return m->recdevs;
181}
182
183void *
184mix_getdevinfo(struct snd_mixer *m)
185{
186 return m->devinfo;
187}
188
189int
190mixer_init(device_t dev, kobj_class_t cls, void *devinfo)
191{
192 struct snd_mixer *m;
193 u_int16_t v;
b13267a5 194 cdev_t pdev;
984263bc
MD
195 int i, unit;
196
197 m = (struct snd_mixer *)kobj_create(cls, M_MIXER, M_WAITOK | M_ZERO);
198 snprintf(m->name, MIXER_NAMELEN, "%s:mixer", device_get_nameunit(dev));
199 m->lock = snd_mtxcreate(m->name, "pcm mixer");
200 m->type = cls->name;
201 m->devinfo = devinfo;
202 m->busy = 0;
203
204 if (MIXER_INIT(m))
205 goto bad;
206
207 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
208 v = snd_mixerdefaults[i];
209 mixer_set(m, i, v | (v << 8));
210 }
211
212 mixer_setrecsrc(m, SOUND_MASK_MIC);
213
214 unit = device_get_unit(dev);
fef8985e 215 dev_ops_add(&mixer_ops,
77934f9e 216 PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_CTL, 0));
fef8985e 217 pdev = make_dev(&mixer_ops, PCMMKMINOR(unit, SND_DEV_CTL, 0),
984263bc
MD
218 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
219 pdev->si_drv1 = m;
220
221 return 0;
222
223bad:
224 snd_mtxlock(m->lock);
225 snd_mtxfree(m->lock);
226 kobj_delete((kobj_t)m, M_MIXER);
227 return -1;
228}
229
230int
231mixer_uninit(device_t dev)
232{
233 int i;
77934f9e 234 int unit;
984263bc 235 struct snd_mixer *m;
b13267a5 236 cdev_t pdev;
984263bc
MD
237
238 pdev = mixer_get_devt(dev);
239 m = pdev->si_drv1;
240 snd_mtxlock(m->lock);
241
242 if (m->busy) {
243 snd_mtxunlock(m->lock);
244 return EBUSY;
245 }
246
247 pdev->si_drv1 = NULL;
e4c9c0c8 248
77934f9e 249 unit = device_get_unit(dev);
fef8985e 250 dev_ops_remove(&mixer_ops,
77934f9e 251 PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_CTL, 0));
984263bc
MD
252
253 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
254 mixer_set(m, i, 0);
255
256 mixer_setrecsrc(m, SOUND_MASK_MIC);
257
258 MIXER_UNINIT(m);
259
260 snd_mtxfree(m->lock);
261 kobj_delete((kobj_t)m, M_MIXER);
262
263 return 0;
264}
265
266int
267mixer_reinit(device_t dev)
268{
269 struct snd_mixer *m;
b13267a5 270 cdev_t pdev;
984263bc
MD
271 int i;
272
273 pdev = mixer_get_devt(dev);
274 m = pdev->si_drv1;
275 snd_mtxlock(m->lock);
276
277 i = MIXER_REINIT(m);
278 if (i) {
279 snd_mtxunlock(m->lock);
280 return i;
281 }
282
283 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
284 mixer_set(m, i, m->level[i]);
285
286 mixer_setrecsrc(m, m->recsrc);
287 snd_mtxunlock(m->lock);
288
289 return 0;
290}
291
292#ifdef SND_DYNSYSCTL
293static int
294sysctl_hw_snd_hwvol_mixer(SYSCTL_HANDLER_ARGS)
295{
296 char devname[32];
297 int error, dev;
298 struct snd_mixer *m;
299
300 m = oidp->oid_arg1;
301 snd_mtxlock(m->lock);
302 strncpy(devname, snd_mixernames[m->hwvol_mixer], sizeof(devname));
303 error = sysctl_handle_string(oidp, &devname[0], sizeof(devname), req);
304 if (error == 0 && req->newptr != NULL) {
305 dev = mixer_lookup(devname);
306 if (dev == -1) {
307 snd_mtxunlock(m->lock);
308 return EINVAL;
309 }
310 else if (dev != m->hwvol_mixer) {
311 m->hwvol_mixer = dev;
312 m->hwvol_muted = 0;
313 }
314 }
315 snd_mtxunlock(m->lock);
316 return error;
317}
318#endif
319
320int
321mixer_hwvol_init(device_t dev)
322{
323 struct snd_mixer *m;
b13267a5 324 cdev_t pdev;
984263bc
MD
325
326 pdev = mixer_get_devt(dev);
327 m = pdev->si_drv1;
328 snd_mtxlock(m->lock);
329
330 m->hwvol_mixer = SOUND_MIXER_VOLUME;
331 m->hwvol_step = 5;
332#ifdef SND_DYNSYSCTL
333 SYSCTL_ADD_INT(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
334 OID_AUTO, "hwvol_step", CTLFLAG_RW, &m->hwvol_step, 0, "");
335 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
336 OID_AUTO, "hwvol_mixer", CTLTYPE_STRING | CTLFLAG_RW, m, 0,
337 sysctl_hw_snd_hwvol_mixer, "A", "")
338#endif
339 snd_mtxunlock(m->lock);
340 return 0;
341}
342
343void
344mixer_hwvol_mute(device_t dev)
345{
346 struct snd_mixer *m;
b13267a5 347 cdev_t pdev;
984263bc
MD
348
349 pdev = mixer_get_devt(dev);
350 m = pdev->si_drv1;
351 snd_mtxlock(m->lock);
352 if (m->hwvol_muted) {
353 m->hwvol_muted = 0;
354 mixer_set(m, m->hwvol_mixer, m->hwvol_mute_level);
355 } else {
356 m->hwvol_muted++;
357 m->hwvol_mute_level = mixer_get(m, m->hwvol_mixer);
358 mixer_set(m, m->hwvol_mixer, 0);
359 }
360 snd_mtxunlock(m->lock);
361}
362
363void
364mixer_hwvol_step(device_t dev, int left_step, int right_step)
365{
366 struct snd_mixer *m;
367 int level, left, right;
b13267a5 368 cdev_t pdev;
984263bc
MD
369
370 pdev = mixer_get_devt(dev);
371 m = pdev->si_drv1;
372 snd_mtxlock(m->lock);
373 if (m->hwvol_muted) {
374 m->hwvol_muted = 0;
375 level = m->hwvol_mute_level;
376 } else
377 level = mixer_get(m, m->hwvol_mixer);
378 if (level != -1) {
379 left = level & 0xff;
380 right = level >> 8;
381 left += left_step * m->hwvol_step;
382 if (left < 0)
383 left = 0;
384 right += right_step * m->hwvol_step;
385 if (right < 0)
386 right = 0;
387 mixer_set(m, m->hwvol_mixer, left | right << 8);
388 }
389 snd_mtxunlock(m->lock);
390}
391
392/* ----------------------------------------------------------------------- */
393
394static int
fef8985e 395mixer_open(struct dev_open_args *ap)
984263bc 396{
b13267a5 397 cdev_t dev = ap->a_head.a_dev;
984263bc 398 struct snd_mixer *m;
984263bc 399
fef8985e 400 m = dev->si_drv1;
b6d92ffb 401 crit_enter();
984263bc
MD
402 snd_mtxlock(m->lock);
403
404 m->busy++;
405
406 snd_mtxunlock(m->lock);
b6d92ffb 407 crit_exit();
984263bc
MD
408 return 0;
409}
410
411static int
fef8985e 412mixer_close(struct dev_close_args *ap)
984263bc 413{
b13267a5 414 cdev_t dev = ap->a_head.a_dev;
984263bc 415 struct snd_mixer *m;
984263bc 416
fef8985e 417 m = dev->si_drv1;
b6d92ffb 418 crit_enter();
984263bc
MD
419 snd_mtxlock(m->lock);
420
421 if (!m->busy) {
422 snd_mtxunlock(m->lock);
b6d92ffb 423 crit_exit();
984263bc
MD
424 return EBADF;
425 }
426 m->busy--;
427
428 snd_mtxunlock(m->lock);
b6d92ffb 429 crit_exit();
984263bc
MD
430 return 0;
431}
432
433int
fef8985e 434mixer_ioctl(struct dev_ioctl_args *ap)
984263bc 435{
b13267a5 436 cdev_t dev = ap->a_head.a_dev;
fef8985e 437 caddr_t arg = ap->a_data;
984263bc 438 struct snd_mixer *m;
984263bc 439 int ret, *arg_i = (int *)arg;
fef8985e 440 int v = -1, j = ap->a_cmd & 0xff;
984263bc 441
fef8985e 442 m = dev->si_drv1;
984263bc
MD
443 if (!m->busy)
444 return EBADF;
445
b6d92ffb 446 crit_enter();
984263bc 447 snd_mtxlock(m->lock);
fef8985e 448 if ((ap->a_cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
984263bc
MD
449 if (j == SOUND_MIXER_RECSRC)
450 ret = mixer_setrecsrc(m, *arg_i);
451 else
452 ret = mixer_set(m, j, *arg_i);
453 snd_mtxunlock(m->lock);
b6d92ffb 454 crit_exit();
984263bc
MD
455 return (ret == 0)? 0 : ENXIO;
456 }
457
fef8985e 458 if ((ap->a_cmd & MIXER_READ(0)) == MIXER_READ(0)) {
984263bc
MD
459 switch (j) {
460 case SOUND_MIXER_DEVMASK:
461 case SOUND_MIXER_CAPS:
462 case SOUND_MIXER_STEREODEVS:
463 v = mix_getdevs(m);
464 break;
465
466 case SOUND_MIXER_RECMASK:
467 v = mix_getrecdevs(m);
468 break;
469
470 case SOUND_MIXER_RECSRC:
471 v = mixer_getrecsrc(m);
472 break;
473
474 default:
475 v = mixer_get(m, j);
476 }
477 *arg_i = v;
478 snd_mtxunlock(m->lock);
1a38bc90 479 crit_exit();
984263bc
MD
480 return (v != -1)? 0 : ENXIO;
481 }
482 snd_mtxunlock(m->lock);
b6d92ffb 483 crit_exit();
984263bc
MD
484 return ENXIO;
485}
486
487#ifdef USING_DEVFS
488static void
b13267a5 489mixer_clone(void *arg, char *name, int namelen, cdev_t *dev)
984263bc 490{
b13267a5 491 cdev_t pdev;
984263bc 492
5711af4f 493 if (*dev != NOCDEV)
984263bc
MD
494 return;
495 if (strcmp(name, "mixer") == 0) {
fef8985e 496 pdev = make_adhoc_dev(&mixer_ops,
e4c9c0c8 497 PCMMKMINOR(snd_unit, SND_DEV_CTL, 0));
984263bc
MD
498 if (pdev->si_flags & SI_NAMED)
499 *dev = pdev;
500 }
501}
502
503static void
504mixer_sysinit(void *p)
505{
506 mixer_ehtag = EVENTHANDLER_REGISTER(dev_clone, mixer_clone, 0, 1000);
507}
508
509static void
510mixer_sysuninit(void *p)
511{
512 if (mixer_ehtag != NULL)
513 EVENTHANDLER_DEREGISTER(dev_clone, mixer_ehtag);
514}
515
516SYSINIT(mixer_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysinit, NULL);
517SYSUNINIT(mixer_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, mixer_sysuninit, NULL);
518#endif
519
520